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=-16.8 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 CD709C43461 for ; Sun, 25 Apr 2021 02:38:39 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 5D6F4611C1 for ; Sun, 25 Apr 2021 02:38:39 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5D6F4611C1 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 3909A8D0003; Sat, 24 Apr 2021 22:38:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2EF0E8D0001; Sat, 24 Apr 2021 22:38:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1DF818D0003; Sat, 24 Apr 2021 22:38:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0121.hostedemail.com [216.40.44.121]) by kanga.kvack.org (Postfix) with ESMTP id EC5328D0001 for ; Sat, 24 Apr 2021 22:38:32 -0400 (EDT) Received: from smtpin38.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id A24188248047 for ; Sun, 25 Apr 2021 02:38:32 +0000 (UTC) X-FDA: 78069330864.38.8241EC3 Received: from szxga04-in.huawei.com (szxga04-in.huawei.com [45.249.212.190]) by imf24.hostedemail.com (Postfix) with ESMTP id 74A23A0003A2 for ; Sun, 25 Apr 2021 02:38:22 +0000 (UTC) Received: from DGGEMS411-HUB.china.huawei.com (unknown [172.30.72.59]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4FSXFv2qxDz17Rm8; Sun, 25 Apr 2021 10:36:03 +0800 (CST) Received: from huawei.com (10.175.104.174) by DGGEMS411-HUB.china.huawei.com (10.3.19.211) with Microsoft SMTP Server id 14.3.498.0; Sun, 25 Apr 2021 10:38:19 +0800 From: Miaohe Lin To: CC: , , , , , , , , , , , , , , , Subject: [PATCH v4 4/4] mm/shmem: fix shmem_swapin() race with swapoff Date: Sun, 25 Apr 2021 10:38:06 +0800 Message-ID: <20210425023806.3537283-5-linmiaohe@huawei.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20210425023806.3537283-1-linmiaohe@huawei.com> References: <20210425023806.3537283-1-linmiaohe@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.175.104.174] X-CFilter-Loop: Reflected X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 74A23A0003A2 X-Stat-Signature: x518jroimbqiks7coq3e1dzfopbu18e8 Received-SPF: none (huawei.com>: No applicable sender policy available) receiver=imf24; identity=mailfrom; envelope-from=""; helo=szxga04-in.huawei.com; client-ip=45.249.212.190 X-HE-DKIM-Result: none/none X-HE-Tag: 1619318302-293929 Content-Transfer-Encoding: quoted-printable 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: When I was investigating the swap code, I found the below possible race window: CPU 1 CPU 2 ----- ----- shmem_swapin swap_cluster_readahead if (likely(si->flags & (SWP_BLKDEV | SWP_FS_OPS))) { swapoff .. si->swap_file =3D NULL; .. struct inode *inode =3D si->swap_file->f_mapping->host;[oops!] Close this race window by using get/put_swap_device() to guard against concurrent swapoff. Fixes: 8fd2e0b505d1 ("mm: swap: check if swap backing device is congested= or not") Signed-off-by: Miaohe Lin --- mm/shmem.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/mm/shmem.c b/mm/shmem.c index 26c76b13ad23..be388d0cf8b5 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1696,6 +1696,7 @@ static int shmem_swapin_page(struct inode *inode, p= goff_t index, struct address_space *mapping =3D inode->i_mapping; struct shmem_inode_info *info =3D SHMEM_I(inode); struct mm_struct *charge_mm =3D vma ? vma->vm_mm : current->mm; + struct swap_info_struct *si; struct page *page; swp_entry_t swap; int error; @@ -1704,6 +1705,12 @@ static int shmem_swapin_page(struct inode *inode, = pgoff_t index, swap =3D radix_to_swp_entry(*pagep); *pagep =3D NULL; =20 + /* Prevent swapoff from happening to us. */ + si =3D get_swap_device(swap); + if (unlikely(!si)) { + error =3D EINVAL; + goto failed; + } /* Look it up and read it in.. */ page =3D lookup_swap_cache(swap, NULL, 0); if (!page) { @@ -1720,6 +1727,7 @@ static int shmem_swapin_page(struct inode *inode, p= goff_t index, goto failed; } } + put_swap_device(si); =20 /* We have to do this with page locked to prevent races */ lock_page(page); @@ -1775,6 +1783,9 @@ static int shmem_swapin_page(struct inode *inode, p= goff_t index, put_page(page); } =20 + if (si) + put_swap_device(si); + return error; } =20 --=20 2.19.1