From patchwork Fri May 27 09:26:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miaohe Lin X-Patchwork-Id: 12863177 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9AF76C433EF for ; Fri, 27 May 2022 09:26:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E5DFC8D0003; Fri, 27 May 2022 05:26:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E34AD8D0002; Fri, 27 May 2022 05:26:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C874F8D0003; Fri, 27 May 2022 05:26:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id B9FF38D0002 for ; Fri, 27 May 2022 05:26:18 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay13.hostedemail.com (Postfix) with ESMTP id 8D163608FF for ; Fri, 27 May 2022 09:26:18 +0000 (UTC) X-FDA: 79510992036.23.FF0DB00 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf08.hostedemail.com (Postfix) with ESMTP id 14F50160042 for ; Fri, 27 May 2022 09:25:54 +0000 (UTC) Received: from canpemm500002.china.huawei.com (unknown [172.30.72.54]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4L8fXm4YHvzcfdn; Fri, 27 May 2022 17:25:12 +0800 (CST) Received: from huawei.com (10.175.124.27) by canpemm500002.china.huawei.com (7.192.104.244) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Fri, 27 May 2022 17:26:14 +0800 From: Miaohe Lin To: CC: , , Subject: [PATCH 1/3] mm/swapfile: make security_vm_enough_memory_mm() work as expected Date: Fri, 27 May 2022 17:26:24 +0800 Message-ID: <20220527092626.31883-2-linmiaohe@huawei.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20220527092626.31883-1-linmiaohe@huawei.com> References: <20220527092626.31883-1-linmiaohe@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.124.27] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To canpemm500002.china.huawei.com (7.192.104.244) X-CFilter-Loop: Reflected Authentication-Results: imf08.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf08.hostedemail.com: domain of linmiaohe@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=linmiaohe@huawei.com X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 14F50160042 X-Stat-Signature: ou3wmdkdrdpne3a85zgsw6nghw1u3oo8 X-HE-Tag: 1653643554-603342 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: security_vm_enough_memory_mm() checks whether a process has enough memory to allocate a new virtual mapping. And total_swap_pages is considered as available memory while swapoff tries to make sure there's enough memory that can hold the swapped out memory. But total_swap_pages contains the swap space that is being swapoff. So security_vm_enough_memory_mm() will success even if there's no memory to hold the swapped out memory because total_swap_pages always greater than or equal to p->pages. In order to fix it, p->pages should be retracted from total_swap_pages first and then check whether there's enough memory for inuse swap pages. Signed-off-by: Miaohe Lin --- mm/swapfile.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index a2e66d855b19..960d14a4b19e 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -2396,6 +2396,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) struct filename *pathname; int err, found = 0; unsigned int old_block_size; + unsigned int inuse_pages; if (!capable(CAP_SYS_ADMIN)) return -EPERM; @@ -2426,9 +2427,13 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) spin_unlock(&swap_lock); goto out_dput; } - if (!security_vm_enough_memory_mm(current->mm, p->pages)) - vm_unacct_memory(p->pages); + + total_swap_pages -= p->pages; + inuse_pages = READ_ONCE(p->inuse_pages); + if (!security_vm_enough_memory_mm(current->mm, inuse_pages)) + vm_unacct_memory(inuse_pages); else { + total_swap_pages += p->pages; err = -ENOMEM; spin_unlock(&swap_lock); goto out_dput; @@ -2451,7 +2456,6 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) } plist_del(&p->list, &swap_active_head); atomic_long_sub(p->pages, &nr_swap_pages); - total_swap_pages -= p->pages; p->flags &= ~SWP_WRITEOK; spin_unlock(&p->lock); spin_unlock(&swap_lock); From patchwork Fri May 27 09:26:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miaohe Lin X-Patchwork-Id: 12863180 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A7BEFC433F5 for ; Fri, 27 May 2022 09:26:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 80F118D0005; Fri, 27 May 2022 05:26:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7BAAF8D0008; Fri, 27 May 2022 05:26:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 541C88D0005; Fri, 27 May 2022 05:26:19 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 3DAF28D0006 for ; Fri, 27 May 2022 05:26:19 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 1272635B59 for ; Fri, 27 May 2022 09:26:19 +0000 (UTC) X-FDA: 79510992078.10.4FDC0A0 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by imf08.hostedemail.com (Postfix) with ESMTP id 979D916003F for ; Fri, 27 May 2022 09:25:55 +0000 (UTC) Received: from canpemm500002.china.huawei.com (unknown [172.30.72.54]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4L8fXB4Q2Kz1JByS; Fri, 27 May 2022 17:24:42 +0800 (CST) Received: from huawei.com (10.175.124.27) by canpemm500002.china.huawei.com (7.192.104.244) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Fri, 27 May 2022 17:26:15 +0800 From: Miaohe Lin To: CC: , , Subject: [PATCH 2/3] mm/swapfile: avoid confusing swap cache statistics Date: Fri, 27 May 2022 17:26:25 +0800 Message-ID: <20220527092626.31883-3-linmiaohe@huawei.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20220527092626.31883-1-linmiaohe@huawei.com> References: <20220527092626.31883-1-linmiaohe@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.124.27] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To canpemm500002.china.huawei.com (7.192.104.244) X-CFilter-Loop: Reflected X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 979D916003F X-Stat-Signature: buicewmtm3tiua9jf1im7143ro5qzns8 Authentication-Results: imf08.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf08.hostedemail.com: domain of linmiaohe@huawei.com designates 45.249.212.255 as permitted sender) smtp.mailfrom=linmiaohe@huawei.com X-Rspam-User: X-HE-Tag: 1653643555-674646 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: At swapoff time, we're going to swap in the pages continuously. So calling lookup_swap_cache would confuse statistics. We should use find_get_page directly here. Signed-off-by: Miaohe Lin --- mm/swapfile.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 960d14a4b19e..e033a53a99df 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1865,7 +1865,12 @@ static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd, offset = swp_offset(entry); pte_unmap(pte); swap_map = &si->swap_map[offset]; - page = lookup_swap_cache(entry, vma, addr); + /* + * Since we're going to swap in the pages continuously, + * calling lookup_swap_cache() would confuse statistics. + */ + page = find_get_page(swap_address_space(entry), + swp_offset(entry)); if (!page) { struct vm_fault vmf = { .vma = vma, From patchwork Fri May 27 09:26:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miaohe Lin X-Patchwork-Id: 12863179 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 59158C433FE for ; Fri, 27 May 2022 09:26:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 656AF8D0007; Fri, 27 May 2022 05:26:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 605538D0006; Fri, 27 May 2022 05:26:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 47D678D0007; Fri, 27 May 2022 05:26:19 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 350A18D0005 for ; Fri, 27 May 2022 05:26:19 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 0488120A14 for ; Fri, 27 May 2022 09:26:18 +0000 (UTC) X-FDA: 79510992078.15.AE5DCAD Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf29.hostedemail.com (Postfix) with ESMTP id AAB0C120046 for ; Fri, 27 May 2022 09:26:06 +0000 (UTC) Received: from canpemm500002.china.huawei.com (unknown [172.30.72.53]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4L8fVT6PKNzRhSQ; Fri, 27 May 2022 17:23:13 +0800 (CST) Received: from huawei.com (10.175.124.27) by canpemm500002.china.huawei.com (7.192.104.244) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Fri, 27 May 2022 17:26:15 +0800 From: Miaohe Lin To: CC: , , Subject: [PATCH 3/3] mm/swapfile: fix possible data races of inuse_pages Date: Fri, 27 May 2022 17:26:26 +0800 Message-ID: <20220527092626.31883-4-linmiaohe@huawei.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20220527092626.31883-1-linmiaohe@huawei.com> References: <20220527092626.31883-1-linmiaohe@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.124.27] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To canpemm500002.china.huawei.com (7.192.104.244) X-CFilter-Loop: Reflected X-Stat-Signature: so36g4fs4r3dz8ucpxgdnoy878zfh4j3 X-Rspam-User: Authentication-Results: imf29.hostedemail.com; dkim=none; spf=pass (imf29.hostedemail.com: domain of linmiaohe@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=linmiaohe@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: AAB0C120046 X-HE-Tag: 1653643566-588691 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: si->inuse_pages could still be accessed concurrently now. The plain reads outside si->lock critical section, i.e. swap_show and si_swapinfo, which results in data races. But these should be ok because they're just used for showing swap info. Signed-off-by: Miaohe Lin Reviewed-by: David Hildenbrand --- mm/swapfile.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index e033a53a99df..29d7ca9eaa3c 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -2649,7 +2649,7 @@ static int swap_show(struct seq_file *swap, void *v) } bytes = si->pages << (PAGE_SHIFT - 10); - inuse = si->inuse_pages << (PAGE_SHIFT - 10); + inuse = READ_ONCE(si->inuse_pages) << (PAGE_SHIFT - 10); file = si->swap_file; len = seq_file_path(swap, file, " \t\n\\"); @@ -3268,7 +3268,7 @@ void si_swapinfo(struct sysinfo *val) struct swap_info_struct *si = swap_info[type]; if ((si->flags & SWP_USED) && !(si->flags & SWP_WRITEOK)) - nr_to_be_unused += si->inuse_pages; + nr_to_be_unused += READ_ONCE(si->inuse_pages); } val->freeswap = atomic_long_read(&nr_swap_pages) + nr_to_be_unused; val->totalswap = total_swap_pages + nr_to_be_unused;