Received: by 10.223.185.116 with SMTP id b49csp341166wrg; Tue, 20 Feb 2018 22:30:02 -0800 (PST) X-Google-Smtp-Source: AH8x225OwA+XaO2k6aro1HSwgjhpX7qQvIQyhUKN/NMEfI4hmm4aj53ldAMuizwR04gcAXEiuuMw X-Received: by 10.99.7.15 with SMTP id 15mr1800765pgh.388.1519194602217; Tue, 20 Feb 2018 22:30:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519194602; cv=none; d=google.com; s=arc-20160816; b=b3OxFeLfS6W/h9KaXfitnXb6t9OZhoEM2ezTP1JoWnAXI6XYBNUNQV1XC01ikNbRsQ fAhCwBvKSjAyIJEvN5CeSyeMiDsMWT7IYA+704ZPsjAigNdPbGNpzBjgGr51wjWAMjca yjtLfaNU6OE1PWncHrgDpT27BlxoiApB9SyF2vjdKpjrU1e/87BB+i82v+6DZ1e7FQAo Wqhki/Yo7c1TL3U1KQein48o4kmYPTGiTSRd4EYVBPPkF0H4VXyeHztssQmxVvvvh/5n aY931pSeKLpL5q0/BT/4TcYKfT1eXjikFDR99Ze7AEYm+BTb3WZNAL2e2mE/NTmnTNgl OJAg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature :arc-authentication-results; bh=eIEhttXa0HtatRFsO0ZqnzwsXnIUib/7OxgvQD5R1Eo=; b=NFfb31caNb8W8KoAKjUcsmkG7y7Y1JlEmEUwlqIjVI6QJ/PKS7wk3KBkXeswhAikas GkBcLnGSA2ziIasG+zhq9JjWimuz88PpITl1ZLXApB9bAjfVD4GQQ/eEa9drNWMsS6Vk dbfk3YhxgEpmQWwLY7FD3DM9bT0DeVfNjBon+t+nhM7KmqS5nLFENrTGo2xMZqkPOvq0 uGO8z93iRhB5Os5aJWREb13n5vdU4s1tjP8bLobPyPhqKwE4Yq4Z8SPXMRwwzfRGoGpB 8a6GVVzrkfbs/6h+fJBWF5UaBQsLb2egE+b2KYxxuOCizRj97L1JsSe9Z2dRHLxCd33w GASA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=GQnBiY/X; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v34-v6si475040plg.803.2018.02.20.22.29.38; Tue, 20 Feb 2018 22:30:02 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=GQnBiY/X; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751412AbeBUG2L (ORCPT + 99 others); Wed, 21 Feb 2018 01:28:11 -0500 Received: from mail-qt0-f194.google.com ([209.85.216.194]:33535 "EHLO mail-qt0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750716AbeBUG2K (ORCPT ); Wed, 21 Feb 2018 01:28:10 -0500 Received: by mail-qt0-f194.google.com with SMTP id d8so690719qtm.0 for ; Tue, 20 Feb 2018 22:28:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=eIEhttXa0HtatRFsO0ZqnzwsXnIUib/7OxgvQD5R1Eo=; b=GQnBiY/Xqj0T5xQjn0jSTOCG4xC6egwUYCbVq5FLLKT8g9CuJ/6OdwTazOfsInxNSB Y06X5fcFZJcTCxx9GxueW1T9Jew0HO7DylNIhn75zOvRaVSPnYzfvjUErWkmp627E407 nMzkaNN6BHZCv8tcrFGLeREOBdltCw6mbYiVDuSdQwsaG3v7MSVIxciOgQo/9RLqysUo /wAhtMVgA2G0xiduEmx/lslxP23lBTHI1h3d03Ttx2LaZKoaXnOqfoRnjd4iOqRLcTcM nRhbrx2ZvUl9731eQ6xoeGMjqCUmw+RPJaps03U5taXvVuI5LG1DM3KeFa+CSZDzEI83 aFMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=eIEhttXa0HtatRFsO0ZqnzwsXnIUib/7OxgvQD5R1Eo=; b=il3BosXiTW67y2PFZT1v5W+NgFTBXpW4x038a6/NE3SnLjC0gcqD8zY2uIT2hv97kO /zpm5PSY4iJbO3ewiqRuNo+Qa8GZw1I4f1ZMSiBgKVVkrMYiS/h3g1bN1vP42GcY1/DZ VNzA9BrrhQuwRU0+4lMSadR2TKHUVcCTPxDNTIwioGHbBkiF9vnMzmmoXge9IzChdbHW qBg1aJDqn5dlNhwodWhUVDiPLkhqUtqjUXuVPF1sM3ELLEHAwBcq29sgnn4TFjQMWyww LUWjYOI4GoYlo+IRKwUzW3utgmVDsZrPB8fnxVshQ+kpOrXr7ElfeEaISEAe7ZuAmZtr GIng== X-Gm-Message-State: APf1xPBqJyGZTZhyxHStJVbyZe14eEN+/IxdLYAprIH/tnSkJN2x7sAJ AAX0ErR3gRcNhiUqQuKsbCfc4UZ4s8RXlIS2olU= X-Received: by 10.200.34.1 with SMTP id o1mr3714114qto.103.1519194489507; Tue, 20 Feb 2018 22:28:09 -0800 (PST) MIME-Version: 1.0 Received: by 10.200.27.235 with HTTP; Tue, 20 Feb 2018 22:28:08 -0800 (PST) In-Reply-To: <20180220153857.dee8f7d5c904cd7219529312@linux-foundation.org> References: <20180213014220.2464-1-ying.huang@intel.com> <20180213154123.9f4ef9e406ea8365ca46d9c5@linux-foundation.org> <87fu64jthz.fsf@yhuang-dev.intel.com> <20180216153823.ad74f1d2c157adc67ed2c970@linux-foundation.org> <20180220153857.dee8f7d5c904cd7219529312@linux-foundation.org> From: huang ying Date: Wed, 21 Feb 2018 14:28:08 +0800 Message-ID: Subject: Re: [PATCH -mm -v5 RESEND] mm, swap: Fix race between swapoff and some swap operations To: Andrew Morton Cc: "Huang, Ying" , linux-mm@kvack.org, LKML , Hugh Dickins , "Paul E . McKenney" , Minchan Kim , Johannes Weiner , Tim Chen , Shaohua Li , Mel Gorman , jglisse@redhat.com, Michal Hocko , Andrea Arcangeli , David Rientjes , Rik van Riel , Jan Kara , Dave Jiang , Aaron Lu Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Feb 21, 2018 at 7:38 AM, Andrew Morton wrote: > On Sun, 18 Feb 2018 09:06:47 +0800 huang ying wrote: > >> >> >> +struct swap_info_struct *get_swap_device(swp_entry_t entry) >> >> >> +{ >> >> >> + struct swap_info_struct *si; >> >> >> + unsigned long type, offset; >> >> >> + >> >> >> + if (!entry.val) >> >> >> + goto out; >> >> >> + type = swp_type(entry); >> >> >> + if (type >= nr_swapfiles) >> >> >> + goto bad_nofile; >> >> >> + si = swap_info[type]; >> >> >> + >> >> >> + preempt_disable(); >> >> > >> >> > This preempt_disable() is later than I'd expect. If a well-timed race >> >> > occurs, `si' could now be pointing at a defunct entry. If that >> >> > well-timed race include a swapoff AND a swapon, `si' could be pointing >> >> > at the info for a new device? >> >> >> >> struct swap_info_struct pointed to by swap_info[] will never be freed. >> >> During swapoff, we only free the memory pointed to by the fields of >> >> struct swap_info_struct. And when swapon, we will always reuse >> >> swap_info[type] if it's not NULL. So it should be safe to dereference >> >> swap_info[type] with preemption enabled. >> > >> > That's my point. If there's a race window during which there is a >> > parallel swapoff+swapon, this swap_info_struct may now be in use for a >> > different device? >> >> Yes. It's possible. And the caller of get_swap_device() can live >> with it if the swap_info_struct has been fully initialized. For >> example, for the race in the patch description, >> >> do_swap_page >> swapin_readahead >> __read_swap_cache_async >> swapcache_prepare >> __swap_duplicate >> >> in __swap_duplicate(), it's possible that the swap device returned by >> get_swap_device() is different from the swap device when >> __swap_duplicate() call get_swap_device(). But the struct_info_struct >> has been fully initialized, so __swap_duplicate() can reference >> si->swap_map[] safely. And we will check si->swap_map[] before any >> further operation. Even if the swap entry is swapped out again for >> the new swap device, we will check the page table again in >> do_swap_page(). So there is no functionality problem. > > That's rather revolting. Can we tighten this up? Or at least very > loudly document it? TBH, I think my original fix patch which uses a reference count in swap_info_struct is easier to be understood. But I understand it has its own drawbacks too. Anyway, unless there are some better ideas to resolve this, I will send out a new version with more document. Best Regards, Huang, Ying