Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp435016pxb; Wed, 3 Mar 2021 07:01:35 -0800 (PST) X-Google-Smtp-Source: ABdhPJxldCM087r65ydsK3K/daHc9fi15RnoKMCfP228koXIJZr8lof/tCGCuBV4nx50oUd2iRT1 X-Received: by 2002:a05:6402:40b:: with SMTP id q11mr25604288edv.36.1614783695394; Wed, 03 Mar 2021 07:01:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614783695; cv=none; d=google.com; s=arc-20160816; b=VqmU9GgJHwBiRv49Ofrvn7kdVj7FHc+lxo9u92MplgqjWylHNRCUJRQSXODYEsY8J0 rwzvXBEtQCSF99Wzq1bQxuMm7t9DXD50uz/AdN0X/V+cDfQtVi+ppRpYK/FBvFQITVij gziwD7GfSnAobHZqqip8suFyfkW4XW1yxfoXYvXaROPjup5CERKUKWO2zW5eUXmiIg+f XX/sgzWXw+xgPl9D5BFOnzDeo/67sE0Lvw+xgx7MfANeGrh6DbPs4IfGWNarc+xvWpSg RRDI1ihR6dAjOJXu5tgJLJNTghMQlmC52/yspbvXaIYTtYufW6wNj75FDn3+QZ3BilpT 9dvA== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=zPVPyJ/o9srGTZvafPNUHRjt0CzljG9Y0QvXhvFNQ4w=; b=Qcq8Wq+syjNqUC3S2i+vE9AcS/MOUkwMO4VmrGaVq7XD73nnu1tlJ92vCTjJqYCgI0 D1tR4QHBYgcX2Am7B0WQMt2MzId5bh7QkzO/bjn5eLck78FAutdeCNoke64fD6+JUAym GlbSHmT2ul5VPXxxwdrFYazVUmApvSNrUnVtIeWKthb7Oxa5qKg0Su98s1T05QG3q23G y10ArrEEnYHFwq2mgzJibtSF+/6dkl2ALfKNKGvY4F8b4hv6JoKN1nDSnNurQl1aJyQv xtzbowOItyLdBG8bleg72h2hMpUTcJgPDkk0auC/eeffMKmXc7/zp4O6vMPozwMf2IhY 9OQg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=rXYj4BIv; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r12si9762115ejz.5.2021.03.03.07.01.09; Wed, 03 Mar 2021 07:01:35 -0800 (PST) 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; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=rXYj4BIv; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244905AbhCAWdF (ORCPT + 99 others); Mon, 1 Mar 2021 17:33:05 -0500 Received: from mail.kernel.org ([198.145.29.99]:57964 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238704AbhCARe6 (ORCPT ); Mon, 1 Mar 2021 12:34:58 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 2A1CD64F2A; Mon, 1 Mar 2021 16:54:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1614617640; bh=e5W47oaOkiWJ7YYlhxq/blAyjdYG6aEC6oV5+gcYiNo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rXYj4BIvhCG14gwUY+cZZI0ey2WJue15AOs23eazGoEs4z9BBbIUgE1WY1SFbKQGm yfu1hHj4YEAw3RZcPWfYI1pbZDupZiD4DAk9254qFyuT4CfWovCs1ZQQJaOKgNWS17 P71vQmUGS3TD/ETy4XX1HDf7x4uO4pX1iIMWiHQ0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Nicolas Boichat , Stephen Boyd , Quentin Perret , Rob Herring , Sasha Levin Subject: [PATCH 5.4 145/340] of/fdt: Make sure no-map does not remove already reserved regions Date: Mon, 1 Mar 2021 17:11:29 +0100 Message-Id: <20210301161055.453589682@linuxfoundation.org> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210301161048.294656001@linuxfoundation.org> References: <20210301161048.294656001@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Nicolas Boichat [ Upstream commit 8a5a75e5e9e55de1cef5d83ca3589cb4899193ef ] If the device tree is incorrectly configured, and attempts to define a "no-map" reserved memory that overlaps with the kernel data/code, the kernel would crash quickly after boot, with no obvious clue about the nature of the issue. For example, this would happen if we have the kernel mapped at these addresses (from /proc/iomem): 40000000-41ffffff : System RAM 40080000-40dfffff : Kernel code 40e00000-411fffff : reserved 41200000-413e0fff : Kernel data And we declare a no-map shared-dma-pool region at a fixed address within that range: mem_reserved: mem_region { compatible = "shared-dma-pool"; reg = <0 0x40000000 0 0x01A00000>; no-map; }; To fix this, when removing memory regions at early boot (which is what "no-map" regions do), we need to make sure that the memory is not already reserved. If we do, __reserved_mem_reserve_reg will throw an error: [ 0.000000] OF: fdt: Reserved memory: failed to reserve memory for node 'mem_region': base 0x0000000040000000, size 26 MiB and the code that will try to use the region should also fail, later on. We do not do anything for non-"no-map" regions, as memblock explicitly allows reserved regions to overlap, and the commit that this fixes removed the check for that precise reason. [ qperret: fixed conflicts caused by the usage of memblock_mark_nomap ] Fixes: 094cb98179f19b7 ("of/fdt: memblock_reserve /memreserve/ regions in the case of partial overlap") Signed-off-by: Nicolas Boichat Reviewed-by: Stephen Boyd Signed-off-by: Quentin Perret Link: https://lore.kernel.org/r/20210115114544.1830068-3-qperret@google.com Signed-off-by: Rob Herring Signed-off-by: Sasha Levin --- drivers/of/fdt.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c index 036af904e0cfa..fc24102e25ce7 100644 --- a/drivers/of/fdt.c +++ b/drivers/of/fdt.c @@ -1153,8 +1153,16 @@ int __init __weak early_init_dt_mark_hotplug_memory_arch(u64 base, u64 size) int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base, phys_addr_t size, bool nomap) { - if (nomap) + if (nomap) { + /* + * If the memory is already reserved (by another region), we + * should not allow it to be marked nomap. + */ + if (memblock_is_region_reserved(base, size)) + return -EBUSY; + return memblock_mark_nomap(base, size); + } return memblock_reserve(base, size); } -- 2.27.0