Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp3415864pxb; Wed, 14 Apr 2021 05:13:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyoGo9mjwm4GX9V/DuqT/Xzq/gmmD6FkpsydeG1nM+jNwAHMjJNZv80RJRKnu+iviNlTmIu X-Received: by 2002:a17:906:90d4:: with SMTP id v20mr10305317ejw.275.1618402396752; Wed, 14 Apr 2021 05:13:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618402396; cv=none; d=google.com; s=arc-20160816; b=kNjEnuzdXgcM94DFNl1cUDmnRGbo2iKmCxtsGkBB+TGX8JbrI/d5wywqyAzRzS1efB yo6cuTZr/IoEbUZNsipado+wAHQOgSW6CdPEIN3u+1exPGoVpxSt2aYLiipjlvHJkfj1 7jMeiJYT1jlJl1LYRJwFTGS3Qe6lKZrMUcM8JYiyoujZm0QaKVuWqZwKzRxgWheM1qiC NZwS3Tg0FKTlgWbPFz9K2Da6+YE2G4IKZpCPHTsKHGUOwrVYrlpypZTGwB5JRb9meN9F 50q32UIgY7F36SiJMxlKe1crAvEr7eLFgm7A1BHA8UP9UJUY7pbz6XO9yziq8q0r4gU+ ZvoA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=rsTfHmlmAeVGjVzlXsqGgt0UB3zM5Wms1bGFWvUzio4=; b=y83QZ/jDFCx8XVHdXb2ElhRMUzR/rqgfCbcLtN5a4/9phKIb5NcCvxYRb5q9Mss6ug GWtbc8sm/UDbSeITir1Jf1R8Tuz3E1/NdBbLm+SpADJ1fa/c5j+2BLWTZIUV5WvcgMcj 3aDY93voO8+VVjLQwMgGgzzfEoHvDSes9eGkSOxV2HZAf3QYPyMevWmPDauy7eCPxThH MfCd8EcAEUcqz2/QmUTtTBLjYFrgDrD+XnLi838E/gI/gNCOzysNkWqeWaYggo/2VeKB fPY7x5it0SkJ/VWJdlhlhRLThOjkDnG14yRtWJpaS0XYGWEWScS3iNO74oDYxPiY0CGy KW2g== 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 ne7si12649419ejc.604.2021.04.14.05.12.53; Wed, 14 Apr 2021 05:13:16 -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 S1343767AbhDNCnj (ORCPT + 99 others); Tue, 13 Apr 2021 22:43:39 -0400 Received: from szxga07-in.huawei.com ([45.249.212.35]:17328 "EHLO szxga07-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232317AbhDNCng (ORCPT ); Tue, 13 Apr 2021 22:43:36 -0400 Received: from DGGEMS412-HUB.china.huawei.com (unknown [172.30.72.58]) by szxga07-in.huawei.com (SkyGuard) with ESMTP id 4FKmtc6jqkzB0Sy; Wed, 14 Apr 2021 10:40:56 +0800 (CST) Received: from [10.174.176.162] (10.174.176.162) by DGGEMS412-HUB.china.huawei.com (10.3.19.212) with Microsoft SMTP Server id 14.3.498.0; Wed, 14 Apr 2021 10:43:10 +0800 Subject: Re: [PATCH 5/5] mm/swap_state: fix swap_cluster_readahead() race with swapoff To: "Huang, Ying" CC: , , , , , , , , , , , , References: <20210408130820.48233-1-linmiaohe@huawei.com> <20210408130820.48233-6-linmiaohe@huawei.com> <87eeffufst.fsf@yhuang6-desk1.ccr.corp.intel.com> From: Miaohe Lin Message-ID: <444ce6f6-9ba7-6161-6429-85b01561ad5e@huawei.com> Date: Wed, 14 Apr 2021 10:43:10 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 MIME-Version: 1.0 In-Reply-To: <87eeffufst.fsf@yhuang6-desk1.ccr.corp.intel.com> Content-Type: text/plain; charset="windows-1252" Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [10.174.176.162] X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2021/4/13 9:36, Huang, Ying wrote: > Miaohe Lin writes: > >> 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. > > I think we should fix the callers instead to reduce the overhead. Now, > do_swap_page() has been fixed. We need to fix shmem_swapin(). > Will do. Many thanks. > Best Regards, > Huang, Ying > >> 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) > . >