From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.1 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 05518C43603 for ; Fri, 20 Dec 2019 14:38:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id CD2B5222C2 for ; Fri, 20 Dec 2019 14:38:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1576852691; bh=Jk/Fp54pKF09pvwjzon7f27VFJRrXW+Yq1xkymArVS4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=MXE27AnG3YgH84L6Ez+neHR/NtdeqJGFaBH/+Z5dIj0owHO4HqOQHLdSG0tZFRS9h io3VsqdVs9zkHV+NmpjmlckzCQfelEZwl/vUbisXofrHws//xN89GShl853TU5IRSl MUBJWn0O97kkmXRmRy+AusUWuRbSSjvNXcMhgzXU= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727909AbfLTOiL (ORCPT ); Fri, 20 Dec 2019 09:38:11 -0500 Received: from mail.kernel.org ([198.145.29.99]:42266 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728415AbfLTOiG (ORCPT ); Fri, 20 Dec 2019 09:38:06 -0500 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id B0AC424682; Fri, 20 Dec 2019 14:38:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1576852685; bh=Jk/Fp54pKF09pvwjzon7f27VFJRrXW+Yq1xkymArVS4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LLAMh5YKBCRIjHqZycNkB3e8f0Z+/wnFH3TIPfZkol5BPZdj5GHG3fO5QrwZ0RzKM tPwfmSNRTBWDMRQyEAJ1Jv/ZAGwuo4o25qr1U/FczcSL+S67X/EpGoMGeR7o8ZduI4 nTn0wd3TyC/+PKBdJtD8Df2m7mUJBS99tn5nOmvg= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Andy Whitcroft , Andrea Righi , "Rafael J . Wysocki" , Sasha Levin , linux-pm@vger.kernel.org Subject: [PATCH AUTOSEL 4.14 19/19] PM / hibernate: memory_bm_find_bit(): Tighten node optimisation Date: Fri, 20 Dec 2019 09:37:40 -0500 Message-Id: <20191220143741.10220-19-sashal@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191220143741.10220-1-sashal@kernel.org> References: <20191220143741.10220-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore 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 0972a8e09d082..ff2aabb70de93 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