Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp4272539pxt; Wed, 11 Aug 2021 01:59:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxcRCaqfKODq4CymXIoLro0qggC248veczmU7Jy01spDG6lYNBMvC4tXailH8fmyX716S/M X-Received: by 2002:a17:906:410c:: with SMTP id j12mr2584252ejk.553.1628672346234; Wed, 11 Aug 2021 01:59:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628672346; cv=none; d=google.com; s=arc-20160816; b=ADPXLTSAkgIRNKQZuzKvVSmQqi5OvIRDwq55sKGAVJh/X2QuJVf6dxfFa0Drr2OlTz cB5/4RdQ/4LmwaQqlepj7Fl6gFptu0O7bYHrchOJNjurwfMrmnqtYx3wraUpompKhzua emXumk6qvZUdIfDGkIIrWUX/OOsTD7QbjroWVoeyNcOxmF+MxR/g+tu3+4ycxX3YItrm NKHvfoivITMnhMyHGL6yszMbGZQhfEXqkWVTZPvXKNbtQUdoZYFApC5Y8DV5ryAJuYLi mlarJWA13yd0Ug5zdbUEV13uvugJQQhPzXuGSgrrHGYfJrMWZh43QugvGEHjhLlXdLN9 04iw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=4bVSo/rpXJ5f1HOsc8GMsWB+t/UpdcqJraOxa2KleSM=; b=DHNA/6zDxsmmxyXLVaZZ55FMr8Sp+ysIY7xM7s1t6W98HORsWNGspuWVujpAASf9Ck UySAUWdzmrExhtSOO/DdY7689W7hoiLUU3qoaIirPnQ6zTMv3b2ZNtb5+lGVjcWV71pA U2b+F+J6kRIuhxeMp5BjQ4+kek5RA9znjZvgqtPA+41AUuPGQpMuNoHdT8AvMA1Cinm7 xxdPyl1Lc/V3nUxYrFabjQJRDdIBtLgptvD30YZL21bx9+/tBnktamks+a5cdULkh5Bx tYYrtI19KQQi8ZZz2zgtrXs5+KBsaNc0FXgoR1ADyL6nMmmFkjbGETNSxgSNMGtvLJTg P0aw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c22si8591932ejb.187.2021.08.11.01.58.41; Wed, 11 Aug 2021 01:59:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236473AbhHKIzq (ORCPT + 99 others); Wed, 11 Aug 2021 04:55:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34614 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236460AbhHKIzq (ORCPT ); Wed, 11 Aug 2021 04:55:46 -0400 Received: from albert.telenet-ops.be (albert.telenet-ops.be [IPv6:2a02:1800:110:4::f00:1a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CFF95C061765 for ; Wed, 11 Aug 2021 01:55:21 -0700 (PDT) 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 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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) -- 2.25.1