Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp3807156ybi; Mon, 29 Jul 2019 13:01:41 -0700 (PDT) X-Google-Smtp-Source: APXvYqzvytCR0EmojNhRpkYUC4P+K7HLO7I+3FXsKSykp9n5ojrep8E1NvXfRDIZALi/i+u+N+fA X-Received: by 2002:a17:90a:8985:: with SMTP id v5mr111995710pjn.136.1564430500917; Mon, 29 Jul 2019 13:01:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564430500; cv=none; d=google.com; s=arc-20160816; b=OSK33187OgHogQCsceVFuRHVIBSUNAKShFacmerfvl9fPXewMh0n8sprVjuRZcmlvi vDVY+sMiTM6GKXV2/e/TKpROkmfXbMMQtPXgTlEs49SvrA2No8fJhnkdzbSsJQYfb8MH FK0KayI3wtHXS+PzOY6msgoayKksCA2GeVX+ZMsF15JdZt0Y29yUx9QpxjMl0fUuOnk9 qfAsZOKZKQnGQrMREjc3SF/X96AB1HrCu4NYznrp1SNOs24g9t4XOpHay68lYVNpBS3a 8QhmY1On0D6gsX3naflu6uZq5qdWRHVTdCKIbcXXBOFbqzGubIoK1nLWM2qOnfV/pC0+ rRog== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=hUv5hfA/dRMarSVbhc7WLUfzLTjQlRKOa0Vf7FPB0uo=; b=sLCRG+5dlWRX3Zac7tebXK72Cly7xUU7KBwJnN7awaM9Asl1Y/hFKeonk+cQJpFy72 QJLBlERBMqbf35LXYQGjD81rF/eQmweCxhFBtg4yIBe/aEwXo4nD6pjCXGKrJvLQO61g QAmsqgAHl5QS8+lgZZcPowT9PIbrYyjz119nblRFdfU4Dpa3uZyGVpNWzUhT4AAUDN08 WznodEbi31D92AAY1Tk6Zfr9l9CrUaV9CtlKS5tkVUBwAYw/VHnLr/FU0rRezob94cdm 2Ae2ufPBamfRspEcOrDhNGZRtebi+pQ37kbBuYydybmBb/eKtMq7wgDbJaF1IGCWpLOA QdYg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=UOZJBu4e; 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 w10si28752773pgt.451.2019.07.29.13.01.26; Mon, 29 Jul 2019 13:01:40 -0700 (PDT) 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=@kernel.org header.s=default header.b=UOZJBu4e; 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 S2391003AbfG2UAH (ORCPT + 99 others); Mon, 29 Jul 2019 16:00:07 -0400 Received: from mail.kernel.org ([198.145.29.99]:44668 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2403754AbfG2Tws (ORCPT ); Mon, 29 Jul 2019 15:52:48 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id B3D9B21773; Mon, 29 Jul 2019 19:52:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1564429967; bh=7gI2jRdswUqOZikL5g6HJFqgYXJPBbm5N+9BTbWMDY0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UOZJBu4erYTGVEzWfZiOHHNnaGZPNxKRwS1YJphXCKeRdwArOrpQ/rl+fcVfeAp1d Kh5agMQjBeF966n+oOOYR3IrCiHkDqsEgqUD0FetlcWbSCjrXn1tZ0+raI6Egp5TEb xGal4d8uBAwcU0hmUZzAEsxV0TsrVfPmH/pWQSP4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, "Huang, Ying" , Andrew Morton , Michal Hocko , Hugh Dickins , "Paul E. McKenney" , Minchan Kim , Johannes Weiner , Tim Chen , Mel Gorman , =?UTF-8?q?J=C3=A9r=C3=B4me=20Glisse?= , Andrea Arcangeli , Yang Shi , David Rientjes , Rik van Riel , Jan Kara , Dave Jiang , Daniel Jordan , Andrea Parri , Linus Torvalds , Sasha Levin Subject: [PATCH 5.2 152/215] mm/mincore.c: fix race between swapoff and mincore Date: Mon, 29 Jul 2019 21:22:28 +0200 Message-Id: <20190729190806.168930852@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190729190739.971253303@linuxfoundation.org> References: <20190729190739.971253303@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org [ Upstream commit aeb309b81c6bada783c3695528a3e10748e97285 ] Via commit 4b3ef9daa4fc ("mm/swap: split swap cache into 64MB trunks"), after swapoff, the address_space associated with the swap device will be freed. So swap_address_space() users which touch the address_space need some kind of mechanism to prevent the address_space from being freed during accessing. When mincore processes an unmapped range for swapped shmem pages, it doesn't hold the lock to prevent swap device from being swapped off. So the following race is possible: CPU1 CPU2 do_mincore() swapoff() walk_page_range() mincore_unmapped_range() __mincore_unmapped_range mincore_page as = swap_address_space() ... exit_swap_address_space() ... kvfree(spaces) find_get_page(as) The address space may be accessed after being freed. To fix the race, get_swap_device()/put_swap_device() is used to enclose find_get_page() to check whether the swap entry is valid and prevent the swap device from being swapoff during accessing. Link: http://lkml.kernel.org/r/20190611020510.28251-1-ying.huang@intel.com Fixes: 4b3ef9daa4fc ("mm/swap: split swap cache into 64MB trunks") Signed-off-by: "Huang, Ying" Reviewed-by: Andrew Morton Acked-by: Michal Hocko Cc: Hugh Dickins Cc: Paul E. McKenney Cc: Minchan Kim Cc: Johannes Weiner Cc: Tim Chen Cc: Mel Gorman Cc: Jérôme Glisse Cc: Andrea Arcangeli Cc: Yang Shi Cc: David Rientjes Cc: Rik van Riel Cc: Jan Kara Cc: Dave Jiang Cc: Daniel Jordan Cc: Andrea Parri Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- mm/mincore.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/mm/mincore.c b/mm/mincore.c index c3f058bd0faf..4fe91d497436 100644 --- a/mm/mincore.c +++ b/mm/mincore.c @@ -68,8 +68,16 @@ static unsigned char mincore_page(struct address_space *mapping, pgoff_t pgoff) */ if (xa_is_value(page)) { swp_entry_t swp = radix_to_swp_entry(page); - page = find_get_page(swap_address_space(swp), - swp_offset(swp)); + struct swap_info_struct *si; + + /* Prevent swap device to being swapoff under us */ + si = get_swap_device(swp); + if (si) { + page = find_get_page(swap_address_space(swp), + swp_offset(swp)); + put_swap_device(si); + } else + page = NULL; } } else page = find_get_page(mapping, pgoff); -- 2.20.1