Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp1748427ybl; Sat, 11 Jan 2020 02:02:27 -0800 (PST) X-Google-Smtp-Source: APXvYqxBGamrSwNBa1XKVzEe4Vc0oU3QrZ7OSJ9TYuZl/eD8LE1KYh1ghfESBjMgHdnQKlK5zjnZ X-Received: by 2002:aca:dc8b:: with SMTP id t133mr5899541oig.98.1578736947130; Sat, 11 Jan 2020 02:02:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1578736947; cv=none; d=google.com; s=arc-20160816; b=Z/RufUgrkZr4HEmOO+B5Bm8wDU0BZcgwyRr+gpUFcPfUTT+EycVfLLanTLNe3ALQOH bwWQ1piHCGubdJ1iERxr+vRZWp2WiNdDvZEQ4kgHF+k5F87EjCzodX4zs7TqRNgVFL0b pfFtCjA7dViVasPFYxV43hYTrzfMwp8ppOntN2iwQp2WwaJBgz9Dfpv5u4vrvAGtCRo/ ZZB54llNeYGJ6yWTG/ZTA4YZousp76iObORrGMlyyXzQc+C0PdL1em6i5Se/aUdy/oyo UOkq0IKWmC3KwaD3T9SPK8qxZ0j5fwtFwg7SxTmlcI9y3rBDnjnOnBIjSTbaCdvT1j6Y G77Q== 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=ep3CfHk4TsLsgBUBIKg+beD9LtNTC7OyCwlqCRbd8s4=; b=qV9qAu5zQ9F3biSexxDGaXJywxNlcGQcryhmmmgUK8v/ec33ZcplEU39X7mq9csECe vjzZKZru+ZCW8yfyh5bYdJk2KrVSkn4YvAtjKLV7uGiUaMb71+MmuTK+auW3bZSAe1sp ywYC8BuaGww9bW/ef4wnj3UumhgAjNiZKx3KMra87YO4A1yW1OG6Aq75W160nNIxCOPK GDo7yzsy0iy7ukjWVHtxTzuuVJDzD9zKZCX33m3ykIcmBNiXOMRU3XVxHB1Uq+e48K/F eZatzO1GCi2hiR8ZKXL7DIhAddFHKg08DvGPzhcKGUAUooNfNkj+RSSGU3YvQjCEkBFP 2bVg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=EizOxTpT; 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 v18si3323225otn.202.2020.01.11.02.02.15; Sat, 11 Jan 2020 02:02: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; dkim=pass header.i=@kernel.org header.s=default header.b=EizOxTpT; 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 S1729299AbgAKKAZ (ORCPT + 99 others); Sat, 11 Jan 2020 05:00:25 -0500 Received: from mail.kernel.org ([198.145.29.99]:56368 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728991AbgAKKAV (ORCPT ); Sat, 11 Jan 2020 05:00:21 -0500 Received: from localhost (unknown [62.119.166.9]) (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 E26222077C; Sat, 11 Jan 2020 10:00:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578736820; bh=J5k4I5PjDOISQmUJ+K3wvYYqJ/TLF/0wPiYvQi9KZAA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EizOxTpT1H3WTSyMQKaCyEPKQBqaKYYFr4/dvtC72Qg9PdJKQ7P0Y0d1IlIQm17Lq a/Fnhd+5Z3//bYMnOygNuSrQcUh+6yj3s+9alecWMc+X5ajJlajchCRoeYQpMOsMfH 7bFrjniYLmtHx4Shak0Czp/texS0tUuARYMGPr+A= 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 4.9 14/91] PM / hibernate: memory_bm_find_bit(): Tighten node optimisation Date: Sat, 11 Jan 2020 10:49:07 +0100 Message-Id: <20200111094848.319826655@linuxfoundation.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200111094844.748507863@linuxfoundation.org> References: <20200111094844.748507863@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 4f0f0604f1c4..5dfac92521fa 100644 --- a/kernel/power/snapshot.c +++ b/kernel/power/snapshot.c @@ -732,8 +732,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