Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp6014356ybc; Wed, 27 Nov 2019 13:25:01 -0800 (PST) X-Google-Smtp-Source: APXvYqzz+VgBY8T6on3wmwTAR92Lfkofy7e3KZqS2smQPH0+ndgpgUCPTgX/gwgE7liwpZdi+XHN X-Received: by 2002:a17:906:4d58:: with SMTP id b24mr51002879ejv.277.1574889901423; Wed, 27 Nov 2019 13:25:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574889901; cv=none; d=google.com; s=arc-20160816; b=zUnrPEuh8M1n8tnvUgOjG8tuz/3loZNQbLEHE8ymlum9ZIPSmkWKVNVYhYdqoYjX6d UoJPwr7Pm9MNpChA99BFYpi1ozWkrwUtMGTR1Ximmjog6CEEFloiJ9WpvNOhv2hjoMT4 vaKcYBLoMlqurzh+eAtHkVJBHx01QRH+RH1yDdM7R8lQ9qzqP68VpbYiWSGcsIjbZE3/ bcCH8QdKW/a2SYKX0MwGChBBnzfs9x2Bjt9F05bpBCjUAcfdRTwAyaO9gYGufQTKkllN /NAKnt8w/HaBgWqEqS50o+bjjmiGoXgPavEB1Pg8GqFmYr7mxGC8Ha05TRsTej/C9Qpn yQSQ== 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=YAavrT+qw4jpFoR2O1kplx+Di0wHGGBs7/8+fWocTH0=; b=de7vrTABK0+Oqqmk2v0WozwU9y2NIbmGjAi7YR1rgPm8szn44B8xvrPN5kWegC+v86 OcQ5Cm4oB5LBrw8npLnaeEMGcYWYdpQGMdRhI6pDnzT/nqGLt5y5RW6+Ju7mQBmKBhEa dmbsuitmAz4MI9GLnjh8zganJk6lXpWwKTJPmoHOqB7gQg1YMk2nVyCuMHdbfZdqF6a/ kb5rh0FYUc8JP8j3kNyTLtnF7ecfBfOOuO/bcFhNG7LYp5IKdBomb1DHewm0Na3sQozN vL2A3TAw90MPKIMEIRU39S6tB+m0o5jUkTfaSN20u5zsk1bKUgxA51dhYEOxCEBRuazm nP6g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=C648szzb; 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 c29si12402836ede.50.2019.11.27.13.24.37; Wed, 27 Nov 2019 13:25:01 -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=@kernel.org header.s=default header.b=C648szzb; 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 S1732351AbfK0VFy (ORCPT + 99 others); Wed, 27 Nov 2019 16:05:54 -0500 Received: from mail.kernel.org ([198.145.29.99]:59710 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732065AbfK0VFw (ORCPT ); Wed, 27 Nov 2019 16:05:52 -0500 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 10C682176D; Wed, 27 Nov 2019 21:05:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1574888751; bh=GsNJSubEqy9092/kPFVh7cTNvDDqTlvKsL8ToKkBp3A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=C648szzbFwBduCYjhuJFYmrNeuFZpwe28LCe12sTTzvBWCxjYhxuTpuSMJ0ssghoY Um9YzGy+k3wxl+Di755f7uREcgjiyqMrY0bw3/k0a811HZz5amvSPK/NKg5hFMcBo5 BNiREDSZa+4gwp2veMdSMVhP8ZAclRPavXgYIMwc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Vinayak Menon , Minchan Kim , Minchan Kim , Michal Hocko , Hugh Dickins , Andrew Morton , Linus Torvalds , Sasha Levin Subject: [PATCH 4.19 255/306] mm/page_io.c: do not free shared swap slots Date: Wed, 27 Nov 2019 21:31:45 +0100 Message-Id: <20191127203133.499090401@linuxfoundation.org> X-Mailer: git-send-email 2.24.0 In-Reply-To: <20191127203114.766709977@linuxfoundation.org> References: <20191127203114.766709977@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 From: Vinayak Menon [ Upstream commit 5df373e95689b9519b8557da7c5bd0db0856d776 ] The following race is observed due to which a processes faulting on a swap entry, finds the page neither in swapcache nor swap. This causes zram to give a zero filled page that gets mapped to the process, resulting in a user space crash later. Consider parent and child processes Pa and Pb sharing the same swap slot with swap_count 2. Swap is on zram with SWP_SYNCHRONOUS_IO set. Virtual address 'VA' of Pa and Pb points to the shared swap entry. Pa Pb fault on VA fault on VA do_swap_page do_swap_page lookup_swap_cache fails lookup_swap_cache fails Pb scheduled out swapin_readahead (deletes zram entry) swap_free (makes swap_count 1) Pb scheduled in swap_readpage (swap_count == 1) Takes SWP_SYNCHRONOUS_IO path zram enrty absent zram gives a zero filled page Fix this by making sure that swap slot is freed only when swap count drops down to one. Link: http://lkml.kernel.org/r/1571743294-14285-1-git-send-email-vinmenon@codeaurora.org Fixes: aa8d22a11da9 ("mm: swap: SWP_SYNCHRONOUS_IO: skip swapcache only if swapped page has no other reference") Signed-off-by: Vinayak Menon Suggested-by: Minchan Kim Acked-by: Minchan Kim Cc: Michal Hocko Cc: Hugh Dickins Cc: Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- mm/page_io.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/mm/page_io.c b/mm/page_io.c index aafd19ec1db46..08d2eae58fcee 100644 --- a/mm/page_io.c +++ b/mm/page_io.c @@ -76,6 +76,7 @@ static void swap_slot_free_notify(struct page *page) { struct swap_info_struct *sis; struct gendisk *disk; + swp_entry_t entry; /* * There is no guarantee that the page is in swap cache - the software @@ -107,11 +108,11 @@ static void swap_slot_free_notify(struct page *page) * we again wish to reclaim it. */ disk = sis->bdev->bd_disk; - if (disk->fops->swap_slot_free_notify) { - swp_entry_t entry; + entry.val = page_private(page); + if (disk->fops->swap_slot_free_notify && + __swap_count(sis, entry) == 1) { unsigned long offset; - entry.val = page_private(page); offset = swp_offset(entry); SetPageDirty(page); -- 2.20.1