Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp429427pxf; Thu, 8 Apr 2021 06:10:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxWkU/Fsedb6HcZdU2xO9ozhz3vnC2Z1sf7zFA1trC31iidYCZuRDtu30mLJk6TJh+cCniR X-Received: by 2002:a05:6512:21a:: with SMTP id a26mr6563132lfo.507.1617887449511; Thu, 08 Apr 2021 06:10:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617887449; cv=none; d=google.com; s=arc-20160816; b=iH8gKPXB2Na+Gjq8SE9iIaZ0lY/JoVt6lmDZ3B1fQsAh+zqTwMxpUL300qgiPlRRG8 XPm7q9S77P2mXEZvElyaDFiz9rJcgVl0uSCVF3MRKBX8/st9nK6xp4vIdFgru5trpmz4 /Vsqf6TSxcnQeUQ5MpVOI3alUR01jarsVExUmwUM+F5fxnUUNn/HPVUPj+sDs5f4WY/p Q/IWyKGKw0F8bRLNYpParolOJY5mVRc/Q/14VbOQ5XTRUi9TBISaawjuOndE34BsYkb2 Bs75Geyen1OLNHAhTu+fvqPE1nqnFaOQ55r935cqOKDNxbcJ31WGPqLefaflaT+XWqIa UCdw== 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=w+W615PTUQ4/rD9oKfQESSXndvJb9LwiI9oB0qsRs+0=; b=n8Cnw6r4EBu0WftohFLZLKQGRpLUAjoxAtuPbCd3FsNWBfXQA7AfBdyiqTLbPE996W JHBu2ihDQqJ5L2w8SxoaLRSmp9i5WY3znpc8OQWBGWzahdUPwMFc/hy0+sI+aSbtvbt1 YfDxtW/7N5L/Vprq/r8E9OQmfiHXBCTBToLF+tV8LQ1JA5O/fykjCXbzH4OKrHlAUcQg NhtyT8n5xpdx8K10oQEJy1JD+VWbhoOo5tZaIIIDLSKo8i9VGu3Pq6hJQVe6iIPPeyE+ SpJSe1XLlrT8Bkc9IRf5j73vNpMZMorrVrrqPzGCdyT8/HVXtQ1VGVmvf2JGbzAlVFDN kmlQ== 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 p13si22399595edt.516.2021.04.08.06.10.21; Thu, 08 Apr 2021 06:10:49 -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 S231628AbhDHNJc (ORCPT + 99 others); Thu, 8 Apr 2021 09:09:32 -0400 Received: from szxga06-in.huawei.com ([45.249.212.32]:16415 "EHLO szxga06-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231158AbhDHNJa (ORCPT ); Thu, 8 Apr 2021 09:09:30 -0400 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 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.175.104.175] X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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) -- 2.19.1