From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 85405C433B4 for ; Mon, 12 Apr 2021 00:55:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 51D2661074 for ; Mon, 12 Apr 2021 00:55:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236167AbhDLAz1 (ORCPT ); Sun, 11 Apr 2021 20:55:27 -0400 Received: from mga09.intel.com ([134.134.136.24]:12362 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235388AbhDLAzZ (ORCPT ); Sun, 11 Apr 2021 20:55:25 -0400 IronPort-SDR: 6nh6IxeGRSHOzk95MTlgjvllmI9QROPNINfWv5EPoZwpL4yx+3pvqKBlK6ZpcHu60ew8Fd2X8R iSy9u56Gyz7Q== X-IronPort-AV: E=McAfee;i="6000,8403,9951"; a="194198797" X-IronPort-AV: E=Sophos;i="5.82,214,1613462400"; d="scan'208";a="194198797" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Apr 2021 17:55:07 -0700 IronPort-SDR: T0w+1dSLSNJceumKgMBiMnlQebHoIE4I9Fv+9sQrumPYKc3AGx5lqaQAKY713tP7MbcVtqlLzc 5H30oT+UFXUg== X-IronPort-AV: E=Sophos;i="5.82,214,1613462400"; d="scan'208";a="459960660" Received: from yhuang6-desk1.sh.intel.com (HELO yhuang6-desk1.ccr.corp.intel.com) ([10.239.13.1]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Apr 2021 17:55:03 -0700 From: "Huang, Ying" To: Miaohe Lin Cc: , , , , , , , , , , , , Subject: Re: [PATCH 4/5] mm/swap_state: fix potential faulted in race in swap_ra_info() References: <20210408130820.48233-1-linmiaohe@huawei.com> <20210408130820.48233-5-linmiaohe@huawei.com> <874kgfyh85.fsf@yhuang6-desk1.ccr.corp.intel.com> Date: Mon, 12 Apr 2021 08:55:01 +0800 In-Reply-To: (Miaohe Lin's message of "Fri, 9 Apr 2021 17:00:02 +0800") Message-ID: <87v98swcd6.fsf@yhuang6-desk1.ccr.corp.intel.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=ascii Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Miaohe Lin writes: > On 2021/4/9 16:50, Huang, Ying wrote: >> Miaohe Lin writes: >> >>> While we released the pte lock, somebody else might faulted in this pte. >>> So we should check whether it's swap pte first to guard against such race >>> or swp_type would be unexpected. And we can also avoid some unnecessary >>> readahead cpu cycles possibly. >>> >>> Fixes: ec560175c0b6 ("mm, swap: VMA based swap readahead") >>> Signed-off-by: Miaohe Lin >>> --- >>> mm/swap_state.c | 13 +++++++++---- >>> 1 file changed, 9 insertions(+), 4 deletions(-) >>> >>> diff --git a/mm/swap_state.c b/mm/swap_state.c >>> index 709c260d644a..3bf0d0c297bc 100644 >>> --- a/mm/swap_state.c >>> +++ b/mm/swap_state.c >>> @@ -724,10 +724,10 @@ static void swap_ra_info(struct vm_fault *vmf, >>> { >>> struct vm_area_struct *vma = vmf->vma; >>> unsigned long ra_val; >>> - swp_entry_t entry; >>> + swp_entry_t swap_entry; >>> unsigned long faddr, pfn, fpfn; >>> unsigned long start, end; >>> - pte_t *pte, *orig_pte; >>> + pte_t *pte, *orig_pte, entry; >>> unsigned int max_win, hits, prev_win, win, left; >>> #ifndef CONFIG_64BIT >>> pte_t *tpte; >>> @@ -742,8 +742,13 @@ static void swap_ra_info(struct vm_fault *vmf, >>> >>> faddr = vmf->address; >>> orig_pte = pte = pte_offset_map(vmf->pmd, faddr); >>> - entry = pte_to_swp_entry(*pte); >>> - if ((unlikely(non_swap_entry(entry)))) { >>> + entry = *pte; >>> + if (unlikely(!is_swap_pte(entry))) { >>> + pte_unmap(orig_pte); >>> + return; >>> + } >>> + swap_entry = pte_to_swp_entry(entry); >>> + if ((unlikely(non_swap_entry(swap_entry)))) { >>> pte_unmap(orig_pte); >>> return; >>> } >> >> This isn't a real issue. entry or swap_entry isn't used in this > > Agree. It seems the entry or swap_entry here is just used for check whether > pte is still valid swap_entry. If you check the git history, you will find that the check has been necessary before. Because the function is used earlier in do_swap_page() at that time. Best Regards, Huang, Ying From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CD089C433ED for ; Mon, 12 Apr 2021 00:55:13 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 3C41261206 for ; Mon, 12 Apr 2021 00:55:13 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 3C41261206 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=intel.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 4A66D6B0036; Sun, 11 Apr 2021 20:55:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 456C66B006C; Sun, 11 Apr 2021 20:55:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2CFAA6B006E; Sun, 11 Apr 2021 20:55:12 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0044.hostedemail.com [216.40.44.44]) by kanga.kvack.org (Postfix) with ESMTP id 0C84A6B0036 for ; Sun, 11 Apr 2021 20:55:12 -0400 (EDT) Received: from smtpin20.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id A36731803017E for ; Mon, 12 Apr 2021 00:55:11 +0000 (UTC) X-FDA: 78021896022.20.C836B47 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by imf25.hostedemail.com (Postfix) with ESMTP id E4AE26000109 for ; Mon, 12 Apr 2021 00:55:07 +0000 (UTC) IronPort-SDR: z65p8w1AITsrhSlynjlCSlMFnPEGFw11a9CaDFPUYbGLGYxIqubvOK6IndB87ZqyZF2nnMeF8D 8oJZYz+PGdBQ== X-IronPort-AV: E=McAfee;i="6000,8403,9951"; a="214543834" X-IronPort-AV: E=Sophos;i="5.82,214,1613462400"; d="scan'208";a="214543834" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Apr 2021 17:55:07 -0700 IronPort-SDR: T0w+1dSLSNJceumKgMBiMnlQebHoIE4I9Fv+9sQrumPYKc3AGx5lqaQAKY713tP7MbcVtqlLzc 5H30oT+UFXUg== X-IronPort-AV: E=Sophos;i="5.82,214,1613462400"; d="scan'208";a="459960660" Received: from yhuang6-desk1.sh.intel.com (HELO yhuang6-desk1.ccr.corp.intel.com) ([10.239.13.1]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Apr 2021 17:55:03 -0700 From: "Huang, Ying" To: Miaohe Lin Cc: , , , , , , , , , , , , Subject: Re: [PATCH 4/5] mm/swap_state: fix potential faulted in race in swap_ra_info() References: <20210408130820.48233-1-linmiaohe@huawei.com> <20210408130820.48233-5-linmiaohe@huawei.com> <874kgfyh85.fsf@yhuang6-desk1.ccr.corp.intel.com> Date: Mon, 12 Apr 2021 08:55:01 +0800 In-Reply-To: (Miaohe Lin's message of "Fri, 9 Apr 2021 17:00:02 +0800") Message-ID: <87v98swcd6.fsf@yhuang6-desk1.ccr.corp.intel.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=ascii X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: E4AE26000109 X-Stat-Signature: of8pcg656qi5gbmnwngb5bea1kk1e1nx Received-SPF: none (intel.com>: No applicable sender policy available) receiver=imf25; identity=mailfrom; envelope-from=""; helo=mga01.intel.com; client-ip=192.55.52.88 X-HE-DKIM-Result: none/none X-HE-Tag: 1618188907-640592 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Miaohe Lin writes: > On 2021/4/9 16:50, Huang, Ying wrote: >> Miaohe Lin writes: >> >>> While we released the pte lock, somebody else might faulted in this pte. >>> So we should check whether it's swap pte first to guard against such race >>> or swp_type would be unexpected. And we can also avoid some unnecessary >>> readahead cpu cycles possibly. >>> >>> Fixes: ec560175c0b6 ("mm, swap: VMA based swap readahead") >>> Signed-off-by: Miaohe Lin >>> --- >>> mm/swap_state.c | 13 +++++++++---- >>> 1 file changed, 9 insertions(+), 4 deletions(-) >>> >>> diff --git a/mm/swap_state.c b/mm/swap_state.c >>> index 709c260d644a..3bf0d0c297bc 100644 >>> --- a/mm/swap_state.c >>> +++ b/mm/swap_state.c >>> @@ -724,10 +724,10 @@ static void swap_ra_info(struct vm_fault *vmf, >>> { >>> struct vm_area_struct *vma = vmf->vma; >>> unsigned long ra_val; >>> - swp_entry_t entry; >>> + swp_entry_t swap_entry; >>> unsigned long faddr, pfn, fpfn; >>> unsigned long start, end; >>> - pte_t *pte, *orig_pte; >>> + pte_t *pte, *orig_pte, entry; >>> unsigned int max_win, hits, prev_win, win, left; >>> #ifndef CONFIG_64BIT >>> pte_t *tpte; >>> @@ -742,8 +742,13 @@ static void swap_ra_info(struct vm_fault *vmf, >>> >>> faddr = vmf->address; >>> orig_pte = pte = pte_offset_map(vmf->pmd, faddr); >>> - entry = pte_to_swp_entry(*pte); >>> - if ((unlikely(non_swap_entry(entry)))) { >>> + entry = *pte; >>> + if (unlikely(!is_swap_pte(entry))) { >>> + pte_unmap(orig_pte); >>> + return; >>> + } >>> + swap_entry = pte_to_swp_entry(entry); >>> + if ((unlikely(non_swap_entry(swap_entry)))) { >>> pte_unmap(orig_pte); >>> return; >>> } >> >> This isn't a real issue. entry or swap_entry isn't used in this > > Agree. It seems the entry or swap_entry here is just used for check whether > pte is still valid swap_entry. If you check the git history, you will find that the check has been necessary before. Because the function is used earlier in do_swap_page() at that time. Best Regards, Huang, Ying