Received: by 10.223.185.116 with SMTP id b49csp18341wrg; Tue, 13 Feb 2018 15:42:27 -0800 (PST) X-Google-Smtp-Source: AH8x225IwOJ5IhSsEB0QaweU9oCwNNmkoTNfAoG28H5XIaoltjof1OFtkuYGR291yto7MTNyrBWo X-Received: by 10.101.80.69 with SMTP id k5mr2274608pgo.425.1518565347176; Tue, 13 Feb 2018 15:42:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518565347; cv=none; d=google.com; s=arc-20160816; b=IukJiF0OnrY9CVN4FiNlxQWmUBTuC13rlmHEbTHZJp8nE92NU9LPbWhJpbxN9Lnieo h3EJhVdYkuu5EUzDNNOopOVJc6B0yHqeYwI/Zyvh8U139ALmD97ROQ/hOiazPiurJnQ6 Pb0EOdg4xLxUDsUj8qr2zxNM40C7ihH7iOeG3W7N+/wlg2i3UETTMGIWEh7KL3TVBaaV aOmt8/yyFDj6FjGZsxO8LBNRisHW/shw/wtJbr2RdQ7zbIXSa1i5QmdOcmV+68wPOwON uKKOHpVfFT+Re7qoXvAVmcu5DLqygz2MlbVVPQXKYnZDOFAK4IjX52s9Iv0ZZfTnU1La 5fMg== 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=UQgxf3Xu2+9MGE5yCvKPQ+jxAd+DkJmK0aFgrq+6+QM=; b=HmEqX/CmntzODj4jEXD1eigVF9wX9kE9GSSTyZ+EwqX/nF3BBG0Dvp0Nk7kfubRADS oglURoYWr2U6tGajGkTZnjEWwQ5TRf4HGH0iyZwgNhj5GHLfwbiE0m4npQCl4BoMSntf cnQ44JvMkxFo7GRLdtrRURT5cX3CiL27MR1UUQY1NC8vmx+XOaj9V1Q2N9gwVwNJqUL+ kRY3iUoo0Q/+++xuVLvY1eab02oKy0f7lbqS2T5aoNRAPbT1SfTUKtj/37ngd3TstAF0 oztGlpKaM+bQMPMqUf7nvuGhZKhvLOc0Mip1IBx9GXFCR/wEfl9zCASpdIFY2/a8kyvq xMJg== 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 h62si7141242pge.13.2018.02.13.15.42.12; Tue, 13 Feb 2018 15:42:27 -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 S966166AbeBMXla (ORCPT + 99 others); Tue, 13 Feb 2018 18:41:30 -0500 Received: from mail.linuxfoundation.org ([140.211.169.12]:35290 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S966064AbeBMXl0 (ORCPT ); Tue, 13 Feb 2018 18:41:26 -0500 Received: from akpm3.svl.corp.google.com (unknown [104.133.9.92]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 2C049CB0; Tue, 13 Feb 2018 23:41:25 +0000 (UTC) Date: Tue, 13 Feb 2018 15:41:23 -0800 From: Andrew Morton To: "Huang, Ying" Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Hugh Dickins , "Paul E . McKenney" , Minchan Kim , Johannes Weiner , Tim Chen , Shaohua Li , Mel Gorman , =?ISO-8859-1?Q?J=E9r=F4me?= Glisse , 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: <20180213154123.9f4ef9e406ea8365ca46d9c5@linux-foundation.org> In-Reply-To: <20180213014220.2464-1-ying.huang@intel.com> References: <20180213014220.2464-1-ying.huang@intel.com> X-Mailer: Sylpheed 3.4.1 (GTK+ 2.24.23; 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 Tue, 13 Feb 2018 09:42:20 +0800 "Huang, Ying" wrote: > From: Huang Ying > > When the swapin is performed, after getting the swap entry information > from the page table, system will swap in the swap entry, without any > lock held to prevent the swap device from being swapoff. This may > cause the race like below, Sigh. In terms of putting all the work into the swapoff path and avoiding overheads in the hot paths, I guess this is about as good as it will get. It's a very low-priority fix so I'd prefer to keep the patch in -mm until Hugh has had an opportunity to think about it. > ... > > +/* > + * Check whether swap entry is valid in the swap device. If so, > + * return pointer to swap_info_struct, and keep the swap entry valid > + * via preventing the swap device from being swapoff, until > + * put_swap_device() is called. Otherwise return NULL. > + */ > +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? > + if (!(si->flags & SWP_VALID)) > + goto unlock_out; > + offset = swp_offset(entry); > + if (offset >= si->max) > + goto unlock_out; > + > + return si; > +bad_nofile: > + pr_err("%s: %s%08lx\n", __func__, Bad_file, entry.val); > +out: > + return NULL; > +unlock_out: > + preempt_enable(); > + return NULL; > +}