Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp28929ybl; Tue, 7 Jan 2020 13:27:03 -0800 (PST) X-Google-Smtp-Source: APXvYqz9V8glXRH/B5MT9tg1PZYZSm3ze/DC6v/3sY+JASTlm503Tu0yLyHAH0rCE4UdhqWm5fJX X-Received: by 2002:aca:dc04:: with SMTP id t4mr379039oig.51.1578432423720; Tue, 07 Jan 2020 13:27:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1578432423; cv=none; d=google.com; s=arc-20160816; b=XCItK7EmAwVdFlDgcnBcy52LWl6xL1f6wSvP2l46N/HB621bPOW7muBboxu0PjIuKU n3GlT5r9AVNwYAq2PLDjwFF6n7aAT3VZdKVY0Co0mK4AOrY5mR4mF8lu7+/DAVjZHeFP aN3HfTlwronKIIcNlAXBIKM7fIKxO2kYf8BNfdFW9gg2hVMQQYKK6SWUf8fwuENvRm3U pwYiQlsNq8ackGS3ENALYu7o33Ni325KKcpW8ziGn6H9Z02PK8mGqyWkxWIWzBu5Bp/p TAzY9+w4YTE1SfJgQdvP0vkc1rvoMO2u/yjQdyuBFKtwvxU6NBLApaKLL/ghZ2VftLhc rPng== 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=W+/729TycUVDJZsRB/7YKwV9HZ2N78+Y0nkwJxfszSk=; b=g2th0+EqsLLhzOW2o8CipvocOb/YcqrZuTQ9l5MC3oz/bFfC6QQLY0fb0i5cldUWLQ bbQ5bCSYcuRZjL8HPaGixvYLAUZaPKjVn2AuTRGXnSq3hKSmy7p32v3iShX6zKnXHPJh XJExORXFLpz00Zt6FhB5sXcwKS6UW9uwea+0MmIhkg0oVttOsIz7kfdI9RrJOd6j1F1p SWpogiJHy9mKRfjfwJWFeNOKbyVToWLOg2/5Uw10EMwTbetZwgrk/SeB5dZ23Vb85nuH eX5qLrz7DsilbaORDv64Pm+Q7lGt9eSdd/g0Grq8YKedVw+wlOZFSmQ+0fErYj2VFXBx 1qiA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=fP7NVOU0; 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 w81si638130oig.107.2020.01.07.13.26.51; Tue, 07 Jan 2020 13:27:03 -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=fP7NVOU0; 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 S1728539AbgAGVZ4 (ORCPT + 99 others); Tue, 7 Jan 2020 16:25:56 -0500 Received: from mail.kernel.org ([198.145.29.99]:60590 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727387AbgAGU73 (ORCPT ); Tue, 7 Jan 2020 15:59:29 -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 419932081E; Tue, 7 Jan 2020 20:59:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578430768; bh=25YGG4pEpfFDp5ONUl1u+Nokxny0vt3ZlgrL6GDtJyc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fP7NVOU0Q34dOMca8+Yeqj74d6bueXsq6+M2Obb8qsv7GcmtWqAz1OvOUrqXuV8qR hu/MWOPoADGJydgY1xAtCnYeD7Sn27JZofrRd7ZKoVaa4B45fQ4d+3iXkWX2ECROHu F6niOMRvs9ix80o+To2GAnvF4TMRd+G37hTQnS+E= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Andrea Righi , Andy Whitcroft , "Rafael J. Wysocki" , Sasha Levin Subject: [PATCH 5.4 052/191] PM / hibernate: memory_bm_find_bit(): Tighten node optimisation Date: Tue, 7 Jan 2020 21:52:52 +0100 Message-Id: <20200107205335.786051636@linuxfoundation.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200107205332.984228665@linuxfoundation.org> References: <20200107205332.984228665@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: Andy Whitcroft [ Upstream commit da6043fe85eb5ec621e34a92540735dcebbea134 ] When looking for a bit by number we make use of the cached result from the preceding lookup to speed up operation. Firstly we check if the requested pfn is within the cached zone and if not lookup the new zone. We then check if the offset for that pfn falls within the existing cached node. This happens regardless of whether the node is within the zone we are now scanning. With certain memory layouts it is possible for this to false trigger creating a temporary alias for the pfn to a different bit. This leads the hibernation code to free memory which it was never allocated with the expected fallout. Ensure the zone we are scanning matches the cached zone before considering the cached node. Deep thanks go to Andrea for many, many, many hours of hacking and testing that went into cornering this bug. Reported-by: Andrea Righi Tested-by: Andrea Righi Signed-off-by: Andy Whitcroft Signed-off-by: Rafael J. Wysocki Signed-off-by: Sasha Levin --- kernel/power/snapshot.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/kernel/power/snapshot.c b/kernel/power/snapshot.c index 83105874f255..26b9168321e7 100644 --- a/kernel/power/snapshot.c +++ b/kernel/power/snapshot.c @@ -734,8 +734,15 @@ static int memory_bm_find_bit(struct memory_bitmap *bm, unsigned long pfn, * We have found the zone. Now walk the radix tree to find the leaf node * for our PFN. */ + + /* + * If the zone we wish to scan is the the current zone and the + * pfn falls into the current node then we do not need to walk + * the tree. + */ node = bm->cur.node; - if (((pfn - zone->start_pfn) & ~BM_BLOCK_MASK) == bm->cur.node_pfn) + if (zone == bm->cur.zone && + ((pfn - zone->start_pfn) & ~BM_BLOCK_MASK) == bm->cur.node_pfn) goto node_found; node = zone->rtree; -- 2.20.1