From patchwork Thu Apr 8 13:08:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miaohe Lin X-Patchwork-Id: 12191037 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=-16.7 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT 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 EFE92C433B4 for ; Thu, 8 Apr 2021 13:09:28 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 867F861074 for ; Thu, 8 Apr 2021 13:09:28 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 867F861074 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=huawei.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id ADF8C6B0081; Thu, 8 Apr 2021 09:09:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A8FF66B0082; Thu, 8 Apr 2021 09:09:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 97ECA6B0083; Thu, 8 Apr 2021 09:09:25 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0104.hostedemail.com [216.40.44.104]) by kanga.kvack.org (Postfix) with ESMTP id 73FC86B0081 for ; Thu, 8 Apr 2021 09:09:25 -0400 (EDT) Received: from smtpin06.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 33EDD82499B9 for ; Thu, 8 Apr 2021 13:09:25 +0000 (UTC) X-FDA: 78009231090.06.E78048B Received: from szxga06-in.huawei.com (szxga06-in.huawei.com [45.249.212.32]) by imf26.hostedemail.com (Postfix) with ESMTP id E06EC40002DB for ; Thu, 8 Apr 2021 13:09:21 +0000 (UTC) Received: from DGGEMS413-HUB.china.huawei.com (unknown [172.30.72.59]) by szxga06-in.huawei.com (SkyGuard) with ESMTP id 4FGM4L1YkKzkjgH; Thu, 8 Apr 2021 21:07:30 +0800 (CST) Received: from huawei.com (10.175.104.175) by DGGEMS413-HUB.china.huawei.com (10.3.19.213) with Microsoft SMTP Server id 14.3.498.0; Thu, 8 Apr 2021 21:09:09 +0800 From: Miaohe Lin To: CC: , , , , , , , , , , , , , Subject: [PATCH 3/5] mm/swap_state: fix get_shadow_from_swap_cache() race with swapoff Date: Thu, 8 Apr 2021 09:08:18 -0400 Message-ID: <20210408130820.48233-4-linmiaohe@huawei.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20210408130820.48233-1-linmiaohe@huawei.com> References: <20210408130820.48233-1-linmiaohe@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.104.175] X-CFilter-Loop: Reflected X-Rspamd-Queue-Id: E06EC40002DB X-Stat-Signature: ergxnd1sden9ecxmxijinjtc4yug3uqg X-Rspamd-Server: rspam02 Received-SPF: none (huawei.com>: No applicable sender policy available) receiver=imf26; identity=mailfrom; envelope-from=""; helo=szxga06-in.huawei.com; client-ip=45.249.212.32 X-HE-DKIM-Result: none/none X-HE-Tag: 1617887361-639569 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: The function get_shadow_from_swap_cache() can race with swapoff, though it's only called by do_swap_page() now. Fixes: aae466b0052e ("mm/swap: implement workingset detection for anonymous LRU") Signed-off-by: Miaohe Lin --- mm/swap_state.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/mm/swap_state.c b/mm/swap_state.c index 272ea2108c9d..709c260d644a 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -83,11 +83,14 @@ void show_swap_cache_info(void) void *get_shadow_from_swap_cache(swp_entry_t entry) { - struct address_space *address_space = swap_address_space(entry); - pgoff_t idx = swp_offset(entry); + struct swap_info_struct *si; struct page *page; - page = xa_load(&address_space->i_pages, idx); + si = get_swap_device(entry); + if (!si) + return NULL; + page = xa_load(&swap_address_space(entry)->i_pages, swp_offset(entry)); + put_swap_device(si); if (xa_is_value(page)) return page; return NULL; From patchwork Thu Apr 8 13:08:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miaohe Lin X-Patchwork-Id: 12191035 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=-16.7 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT 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 3F0FFC43462 for ; Thu, 8 Apr 2021 13:09:27 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id A238361153 for ; Thu, 8 Apr 2021 13:09:26 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A238361153 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=huawei.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 628216B0080; Thu, 8 Apr 2021 09:09:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 388936B0081; Thu, 8 Apr 2021 09:09:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 13FE16B0083; Thu, 8 Apr 2021 09:09:23 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0103.hostedemail.com [216.40.44.103]) by kanga.kvack.org (Postfix) with ESMTP id E60796B0080 for ; Thu, 8 Apr 2021 09:09:22 -0400 (EDT) Received: from smtpin03.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id A8C0282499B9 for ; Thu, 8 Apr 2021 13:09:22 +0000 (UTC) X-FDA: 78009230964.03.EEB4902 Received: from szxga06-in.huawei.com (szxga06-in.huawei.com [45.249.212.32]) by imf20.hostedemail.com (Postfix) with ESMTP id 2F847F7 for ; Thu, 8 Apr 2021 13:09:19 +0000 (UTC) Received: from DGGEMS413-HUB.china.huawei.com (unknown [172.30.72.59]) by szxga06-in.huawei.com (SkyGuard) with ESMTP id 4FGM4L1CWSzkjgG; Thu, 8 Apr 2021 21:07:30 +0800 (CST) Received: from huawei.com (10.175.104.175) by DGGEMS413-HUB.china.huawei.com (10.3.19.213) with Microsoft SMTP Server id 14.3.498.0; Thu, 8 Apr 2021 21:09:10 +0800 From: Miaohe Lin To: CC: , , , , , , , , , , , , , Subject: [PATCH 4/5] mm/swap_state: fix potential faulted in race in swap_ra_info() Date: Thu, 8 Apr 2021 09:08:19 -0400 Message-ID: <20210408130820.48233-5-linmiaohe@huawei.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20210408130820.48233-1-linmiaohe@huawei.com> References: <20210408130820.48233-1-linmiaohe@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.104.175] X-CFilter-Loop: Reflected X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 2F847F7 X-Stat-Signature: ugrj48d45xbw81f7w7qicuidj5qzcjcg Received-SPF: none (huawei.com>: No applicable sender policy available) receiver=imf20; identity=mailfrom; envelope-from=""; helo=szxga06-in.huawei.com; client-ip=45.249.212.32 X-HE-DKIM-Result: none/none X-HE-Tag: 1617887359-222318 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: 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; } From patchwork Thu Apr 8 13:08:20 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miaohe Lin X-Patchwork-Id: 12191031 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=-16.7 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT 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 22C4FC43461 for ; Thu, 8 Apr 2021 13:09:24 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 9B04561151 for ; Thu, 8 Apr 2021 13:09:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 9B04561151 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=huawei.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 1B6456B0078; Thu, 8 Apr 2021 09:09:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 18C866B0072; Thu, 8 Apr 2021 09:09:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EFC9E6B0082; Thu, 8 Apr 2021 09:09:22 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0165.hostedemail.com [216.40.44.165]) by kanga.kvack.org (Postfix) with ESMTP id D32326B0072 for ; Thu, 8 Apr 2021 09:09:22 -0400 (EDT) Received: from smtpin18.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 8E08F62CC for ; Thu, 8 Apr 2021 13:09:22 +0000 (UTC) X-FDA: 78009230964.18.0A19B44 Received: from szxga06-in.huawei.com (szxga06-in.huawei.com [45.249.212.32]) by imf06.hostedemail.com (Postfix) with ESMTP id E50B6C0007C9 for ; Thu, 8 Apr 2021 13:09:22 +0000 (UTC) Received: from DGGEMS413-HUB.china.huawei.com (unknown [172.30.72.59]) by szxga06-in.huawei.com (SkyGuard) with ESMTP id 4FGM4L0gbqzkjg8; Thu, 8 Apr 2021 21:07:30 +0800 (CST) Received: from huawei.com (10.175.104.175) by DGGEMS413-HUB.china.huawei.com (10.3.19.213) with Microsoft SMTP Server id 14.3.498.0; Thu, 8 Apr 2021 21:09:11 +0800 From: Miaohe Lin To: CC: , , , , , , , , , , , , , Subject: [PATCH 5/5] mm/swap_state: fix swap_cluster_readahead() race with swapoff Date: Thu, 8 Apr 2021 09:08:20 -0400 Message-ID: <20210408130820.48233-6-linmiaohe@huawei.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20210408130820.48233-1-linmiaohe@huawei.com> References: <20210408130820.48233-1-linmiaohe@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.104.175] X-CFilter-Loop: Reflected X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: E50B6C0007C9 X-Stat-Signature: acy3n89at748csj6ej4ow96jb1qcogz4 Received-SPF: none (huawei.com>: No applicable sender policy available) receiver=imf06; identity=mailfrom; envelope-from=""; helo=szxga06-in.huawei.com; client-ip=45.249.212.32 X-HE-DKIM-Result: none/none X-HE-Tag: 1617887362-71778 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: swap_cluster_readahead() could race with swapoff and might dereference si->swap_file after it's released by swapoff. Close this race window by using get/put_swap_device() pair. Signed-off-by: Miaohe Lin --- mm/swap_state.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/mm/swap_state.c b/mm/swap_state.c index 3bf0d0c297bc..eba6b0cf6cf9 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -626,12 +626,17 @@ struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask, unsigned long offset = entry_offset; unsigned long start_offset, end_offset; unsigned long mask; - struct swap_info_struct *si = swp_swap_info(entry); + struct swap_info_struct *si; struct blk_plug plug; bool do_poll = true, page_allocated; struct vm_area_struct *vma = vmf->vma; unsigned long addr = vmf->address; + si = get_swap_device(entry); + /* In case we raced with swapoff. */ + if (!si) + return NULL; + mask = swapin_nr_pages(offset) - 1; if (!mask) goto skip; @@ -673,7 +678,9 @@ struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask, lru_add_drain(); /* Push any new pages onto the LRU now */ skip: - return read_swap_cache_async(entry, gfp_mask, vma, addr, do_poll); + page = read_swap_cache_async(entry, gfp_mask, vma, addr, do_poll); + put_swap_device(si); + return page; } int init_swap_address_space(unsigned int type, unsigned long nr_pages)