From patchwork Wed Aug 11 08:55:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geert Uytterhoeven X-Patchwork-Id: 12430499 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=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,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 1BCFFC4338F for ; Wed, 11 Aug 2021 08:55:24 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 7E75D604AC for ; Wed, 11 Aug 2021 08:55:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 7E75D604AC Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=glider.be Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id CE8196B0072; Wed, 11 Aug 2021 04:55:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C97468D0001; Wed, 11 Aug 2021 04:55:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B86216B0074; Wed, 11 Aug 2021 04:55:22 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0053.hostedemail.com [216.40.44.53]) by kanga.kvack.org (Postfix) with ESMTP id 9E6FE6B0072 for ; Wed, 11 Aug 2021 04:55:22 -0400 (EDT) Received: from smtpin37.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 452E28249980 for ; Wed, 11 Aug 2021 08:55:22 +0000 (UTC) X-FDA: 78462190884.37.7A153AE Received: from albert.telenet-ops.be (albert.telenet-ops.be [195.130.137.90]) by imf21.hostedemail.com (Postfix) with ESMTP id 9FD05D00D59D for ; Wed, 11 Aug 2021 08:55:21 +0000 (UTC) Received: from ramsan.of.borg ([IPv6:2a02:1810:ac12:ed20:438:1ff1:1071:f524]) by albert.telenet-ops.be with bizsmtp id g8vL250031gJxCh068vLJa; Wed, 11 Aug 2021 10:55:20 +0200 Received: from rox.of.borg ([192.168.97.57]) by ramsan.of.borg with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.93) (envelope-from ) id 1mDk1A-001yjD-03; Wed, 11 Aug 2021 10:55:20 +0200 Received: from geert by rox.of.borg with local (Exim 4.93) (envelope-from ) id 1mDk19-0059Gs-IB; Wed, 11 Aug 2021 10:55:19 +0200 From: Geert Uytterhoeven To: Mike Rapoport , Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Geert Uytterhoeven Subject: [PATCH] memblock: Check memory add/cap ordering Date: Wed, 11 Aug 2021 10:55:18 +0200 Message-Id: X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Authentication-Results: imf21.hostedemail.com; dkim=none; spf=none (imf21.hostedemail.com: domain of geert@linux-m68k.org has no SPF policy when checking 195.130.137.90) smtp.mailfrom=geert@linux-m68k.org; dmarc=none X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 9FD05D00D59D X-Stat-Signature: rx514dywqp6bofkc14ufb8wabhbm19pp X-HE-Tag: 1628672121-847319 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: For memblock_cap_memory_range() to work properly, it should be called after memory is detected and added to memblock with memblock_add() or memblock_add_node(). If memblock_cap_memory_range() would be called before memory is registered, we may silently corrupt memory later because the crash kernel will see all memory as available. Print a warning and bail out if ordering is not satisfied. Suggested-by: Mike Rapoport Signed-off-by: Geert Uytterhoeven Reviewed-by: David Hildenbrand --- mm/memblock.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/mm/memblock.c b/mm/memblock.c index 57a9849a5d820c34..e2ca8ddc8ebebf4e 100644 --- a/mm/memblock.c +++ b/mm/memblock.c @@ -1685,6 +1685,11 @@ void __init memblock_cap_memory_range(phys_addr_t base, phys_addr_t size) if (!size) return; + if (memblock.memory.cnt <= 1) { + pr_warn("%s: No memory registered yet\n", __func__); + return; + } + ret = memblock_isolate_range(&memblock.memory, base, size, &start_rgn, &end_rgn); if (ret)