Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp3596499pxy; Mon, 26 Apr 2021 05:36:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwNso3bZq0lW9p/8hoOZXICoBjKPHlrYVLVEwdBLSp+fcvz+iTDd2mzEjtUv0QOZcXYvZwX X-Received: by 2002:a05:6a00:16d0:b029:249:adf8:6fb8 with SMTP id l16-20020a056a0016d0b0290249adf86fb8mr17123392pfc.58.1619440597397; Mon, 26 Apr 2021 05:36:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619440597; cv=none; d=google.com; s=arc-20160816; b=FKYi4amYJH+Q6Z7mBXOgJ854X1cOO0K7du3nhA4i3LrVzrRnOYikBFqCwtgm7+0BiZ xdIAVzd3rk0yctpygdMqOKnm3ve2a9mG5OuR29o397HZme1ICXCyeuJwddA5AyJk76FT pc9g/2S/ZjIUU/Qd/ggTvf+VkFJ9PgoLGy/vVJzj3WSh7aWzk7rYQ9NT+1WwOCrSunL9 1FY1Q5Y+YV+rYRbvgoNk+YAmZZmiXjGdOvvvoGRce7dgsnAP9V7+uZeGRVZ7m2aTS0v/ GU6KZiU4AxKGTPc1fj0GpZhqn/9s5aSib0kMsHndUvszJDul4A4YG3acmV0h6za4nkCz CI4Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=llhE/6P0U2hnoSRa0EYeEKH5NsfTBGOdghIFegz6l3k=; b=bz/3QKcQjYU7jSM9cqS4XkgS7AUEhE0BmH5uwgx/sRfGbQltS8Ph6H3AkoN3AFNKnm OX40jly73wPQI1ebV2obv+NpBLM8MnpKxuo+f/PPjsv6VTKMGT6MGw8j1TtrE9EoBKTN 1gzmAqM4ckvKPQyp9ItHvW1ZP/gWfOXFrKOD6Zee6dOKfeZR/ORTPdl3tyR6++JCK1Uj AHxG+6EjUzVZ9gMP54TOjfv/T1iiI0nD8SmMfhC2OvOQpnNrpjsk4Nj4K/JNsWj2wkpZ oJKPxocyKLaInYnYLj52PW6wWr87+jBU5ffiZ+6jR/zRXvRhmCR8OCYE+AdmA12TvKOx DsSg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y16si17985421pfc.318.2021.04.26.05.36.24; Mon, 26 Apr 2021 05:36:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233467AbhDZMei (ORCPT + 99 others); Mon, 26 Apr 2021 08:34:38 -0400 Received: from szxga04-in.huawei.com ([45.249.212.190]:17061 "EHLO szxga04-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233435AbhDZMeg (ORCPT ); Mon, 26 Apr 2021 08:34:36 -0400 Received: from DGGEMS403-HUB.china.huawei.com (unknown [172.30.72.59]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4FTPQQ3VvCz19MM8; Mon, 26 Apr 2021 20:31:26 +0800 (CST) Received: from huawei.com (10.175.104.170) by DGGEMS403-HUB.china.huawei.com (10.3.19.203) with Microsoft SMTP Server id 14.3.498.0; Mon, 26 Apr 2021 20:33:44 +0800 From: Miaohe Lin To: CC: , , , , , , , , , , , , , , , , Subject: [PATCH v6 4/4] mm/shmem: fix shmem_swapin() race with swapoff Date: Mon, 26 Apr 2021 20:33:16 +0800 Message-ID: <20210426123316.806267-5-linmiaohe@huawei.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20210426123316.806267-1-linmiaohe@huawei.com> References: <20210426123316.806267-1-linmiaohe@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.175.104.170] X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 = NULL; .. struct inode *inode = 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") Reviewed-by: "Huang, Ying" Signed-off-by: Miaohe Lin --- mm/shmem.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/mm/shmem.c b/mm/shmem.c index 26c76b13ad23..e5dd8fa51463 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1696,7 +1696,8 @@ static int shmem_swapin_page(struct inode *inode, pgoff_t index, struct address_space *mapping = inode->i_mapping; struct shmem_inode_info *info = SHMEM_I(inode); struct mm_struct *charge_mm = vma ? vma->vm_mm : current->mm; - struct page *page; + struct swap_info_struct *si; + struct page *page = NULL; swp_entry_t swap; int error; @@ -1704,6 +1705,12 @@ static int shmem_swapin_page(struct inode *inode, pgoff_t index, swap = radix_to_swp_entry(*pagep); *pagep = NULL; + /* Prevent swapoff from happening to us. */ + si = get_swap_device(swap); + if (!si) { + error = EINVAL; + goto failed; + } /* Look it up and read it in.. */ page = lookup_swap_cache(swap, NULL, 0); if (!page) { @@ -1765,6 +1772,8 @@ static int shmem_swapin_page(struct inode *inode, pgoff_t index, swap_free(swap); *pagep = page; + if (si) + put_swap_device(si); return 0; failed: if (!shmem_confirm_swap(mapping, index, swap)) @@ -1775,6 +1784,9 @@ static int shmem_swapin_page(struct inode *inode, pgoff_t index, put_page(page); } + if (si) + put_swap_device(si); + return error; } -- 2.23.0