Received: by 10.223.185.116 with SMTP id b49csp49255wrg; Tue, 20 Feb 2018 15:40:06 -0800 (PST) X-Google-Smtp-Source: AH8x224q5H7NnvaxG1sjlag77XKjpYrRaohy4fIB8ssbRTMSYBvxhFQidLkXo41D7eA5sX1gsfQO X-Received: by 10.99.151.26 with SMTP id n26mr1059817pge.370.1519170006837; Tue, 20 Feb 2018 15:40:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519170006; cv=none; d=google.com; s=arc-20160816; b=XrFGE5Tv1wcCoV5FXF4Kp5ERgfp+mPBK6dcUApALAU+NVD/LgdN2XP/1dkmcaeaBCe UfwdlMw7b5+Dh2g8t0GJ5N/YMaC3RYN+FD2KkOfubNry8r6Z7k4thTp6U2GJASrWoUKP RdoEabvp6P2a/qTP+9xZJW8/7ASdsfZP4Ec64iCYN3Iil6dOzbKhBPBNZzc0oyGWR6gS iIbuEqvfh9gXGqw6VyQ/g9Akoo6xgYHPHRPHJ+WAuP7iZgkLpFs7Qbt+UepHOQZMMRUc V3I3xCv6Lg3tCwltVSWrTbXNP44o2I/Uk603TZBs7ufkyXNxqdNTUBa2vL9hbBrghxt6 29Yg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :arc-authentication-results; bh=UlHDy4HHbR5lldQEFXbqo0a/HI7Okcj5/KYJy4AQHmE=; b=zxJndz6ltpVS8vrv9YZNJQmJdeDjmQNn7kCcfEKqjpOkrCXSOtjYk1PLf74z5vLAML zNwsN0YCgBSNfngV3zbfJM8ytNmCAXnkWZGj9OeK05eXNDFfrRqbYO0kD3LuWxWb7l24 xk93825bf2FnjcNqgQSnl+9evknvjU3orkUfK1aUlAsJyIAZKiDGtFAN+5G8bfIaOE8S ufRwcRAdJrSlRBxbVSgnR69ysGWJNizLE4fCg6yCjGigazljSOB/FO9S/iPI54Y53qwm N8J/eMNCwbKp/yV4KAaq2mW/nCt9q7UbuA7eFFkYIFomdHRWUxssxtMxOS3fcQ27oGjM IRYg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e20si1818063pgn.546.2018.02.20.15.39.52; Tue, 20 Feb 2018 15:40:06 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751418AbeBTXjA (ORCPT + 99 others); Tue, 20 Feb 2018 18:39:00 -0500 Received: from mail.linuxfoundation.org ([140.211.169.12]:51786 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751372AbeBTXi7 (ORCPT ); Tue, 20 Feb 2018 18:38:59 -0500 Received: from akpm3.svl.corp.google.com (unknown [104.133.9.71]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id F3A2A10E3; Tue, 20 Feb 2018 23:38:57 +0000 (UTC) Date: Tue, 20 Feb 2018 15:38:57 -0800 From: Andrew Morton To: huang ying 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 Subject: Re: [PATCH -mm -v5 RESEND] mm, swap: Fix race between swapoff and some swap operations Message-Id: <20180220153857.dee8f7d5c904cd7219529312@linux-foundation.org> In-Reply-To: References: <20180213014220.2464-1-ying.huang@intel.com> <20180213154123.9f4ef9e406ea8365ca46d9c5@linux-foundation.org> <87fu64jthz.fsf@yhuang-dev.intel.com> <20180216153823.ad74f1d2c157adc67ed2c970@linux-foundation.org> X-Mailer: Sylpheed 3.6.0 (GTK+ 2.24.31; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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?