From nobody Tue Apr 28 23:19:28 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D9285C433EF for ; Fri, 27 May 2022 09:26:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350485AbiE0J0f (ORCPT ); Fri, 27 May 2022 05:26:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46850 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350488AbiE0J0T (ORCPT ); Fri, 27 May 2022 05:26:19 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F36A8B7D for ; Fri, 27 May 2022 02:26:16 -0700 (PDT) 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 Content-Transfer-Encoding: quoted-printable 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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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 *, special= file) struct filename *pathname; int err, found =3D 0; unsigned int old_block_size; + unsigned int inuse_pages; =20 if (!capable(CAP_SYS_ADMIN)) return -EPERM; @@ -2426,9 +2427,13 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specia= lfile) 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 -=3D p->pages; + inuse_pages =3D READ_ONCE(p->inuse_pages); + if (!security_vm_enough_memory_mm(current->mm, inuse_pages)) + vm_unacct_memory(inuse_pages); else { + total_swap_pages +=3D p->pages; err =3D -ENOMEM; spin_unlock(&swap_lock); goto out_dput; @@ -2451,7 +2456,6 @@ SYSCALL_DEFINE1(swapoff, const char __user *, special= file) } plist_del(&p->list, &swap_active_head); atomic_long_sub(p->pages, &nr_swap_pages); - total_swap_pages -=3D p->pages; p->flags &=3D ~SWP_WRITEOK; spin_unlock(&p->lock); spin_unlock(&swap_lock); --=20 2.23.0 From nobody Tue Apr 28 23:19:28 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 50BA3C433EF for ; Fri, 27 May 2022 09:26:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240270AbiE0J0j (ORCPT ); Fri, 27 May 2022 05:26:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46860 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350490AbiE0J0T (ORCPT ); Fri, 27 May 2022 05:26:19 -0400 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 279B960E0 for ; Fri, 27 May 2022 02:26:17 -0700 (PDT) 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 Content-Transfer-Encoding: quoted-printable 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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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 *vm= a, pmd_t *pmd, offset =3D swp_offset(entry); pte_unmap(pte); swap_map =3D &si->swap_map[offset]; - page =3D lookup_swap_cache(entry, vma, addr); + /* + * Since we're going to swap in the pages continuously, + * calling lookup_swap_cache() would confuse statistics. + */ + page =3D find_get_page(swap_address_space(entry), + swp_offset(entry)); if (!page) { struct vm_fault vmf =3D { .vma =3D vma, --=20 2.23.0 From nobody Tue Apr 28 23:19:28 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D89B5C433F5 for ; Fri, 27 May 2022 09:26:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350500AbiE0J01 (ORCPT ); Fri, 27 May 2022 05:26:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46858 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350494AbiE0J0T (ORCPT ); Fri, 27 May 2022 05:26:19 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BC7FB6176 for ; Fri, 27 May 2022 02:26:17 -0700 (PDT) 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 Content-Transfer-Encoding: quoted-printable 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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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) } =20 bytes =3D si->pages << (PAGE_SHIFT - 10); - inuse =3D si->inuse_pages << (PAGE_SHIFT - 10); + inuse =3D READ_ONCE(si->inuse_pages) << (PAGE_SHIFT - 10); =20 file =3D si->swap_file; len =3D seq_file_path(swap, file, " \t\n\\"); @@ -3268,7 +3268,7 @@ void si_swapinfo(struct sysinfo *val) struct swap_info_struct *si =3D swap_info[type]; =20 if ((si->flags & SWP_USED) && !(si->flags & SWP_WRITEOK)) - nr_to_be_unused +=3D si->inuse_pages; + nr_to_be_unused +=3D READ_ONCE(si->inuse_pages); } val->freeswap =3D atomic_long_read(&nr_swap_pages) + nr_to_be_unused; val->totalswap =3D total_swap_pages + nr_to_be_unused; --=20 2.23.0