From nobody Wed Feb 11 11:05:55 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1A3A6C761A6 for ; Thu, 6 Apr 2023 15:15:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239727AbjDFPPG (ORCPT ); Thu, 6 Apr 2023 11:15:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40698 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239726AbjDFPOt (ORCPT ); Thu, 6 Apr 2023 11:14:49 -0400 Received: from mail-wm1-f42.google.com (mail-wm1-f42.google.com [209.85.128.42]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 67FA449FF; Thu, 6 Apr 2023 08:14:47 -0700 (PDT) Received: by mail-wm1-f42.google.com with SMTP id p34so22773574wms.3; Thu, 06 Apr 2023 08:14:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680794086; x=1683386086; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=KTGfYrqfzS07zpfH0JC+lthwHQN3E94OuA9UZu3JUac=; b=Q83y9iB1IJZ0G79EXIxMQMOIU9BpXDuLAjUf7pEp5b++Ovms/y1InFmGHEXQytR2GE p0QInp2rW1Gfd4q/g1PsKInyLIDBKOeCTPEym/P8Dvjkna6aj5On33YyxfiX/8sbiYDt hzOMbtAGSwk2GpGsfFr9uwPoZTrXVOl+r40IkAZfm+oKZnBvzJj7bm5RRYqEHi7GrNy+ WZVisxxRRovcdMuCRiyDVdtS/ENoj3v3+vmr3lvRkpeQY/xSaCTNabZ5EfWTKbV5AEAb 2ky8vG5Ibt2Ls69jcQPUhLTFgtykHaU74NCj2GVLBi+RDYDznzlg0YLnG5y6+rMPhzb8 38PA== X-Gm-Message-State: AAQBX9cYDWyvf/wo70LLYkoYY4Mq49ClfpRD6UM5JmvDPuKWbpOcR8E3 Rhia8c1dk2NorNOZFCZRLYE= X-Google-Smtp-Source: AKy350bioooJgfp2IytRIDWd0TtyTY5KBmKx4pJ+VtEwGO+IsLJWXioFGYateblkCLF62e/3sqxzDw== X-Received: by 2002:a7b:c38a:0:b0:3ed:e715:1784 with SMTP id s10-20020a7bc38a000000b003ede7151784mr8689914wmj.15.1680794085640; Thu, 06 Apr 2023 08:14:45 -0700 (PDT) Received: from ryzen.lan (cpc87451-finc19-2-0-cust61.4-2.cable.virginm.net. [82.11.51.62]) by smtp.gmail.com with ESMTPSA id fj12-20020a05600c0c8c00b003ef67848a21sm5560213wmb.13.2023.04.06.08.14.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Apr 2023 08:14:45 -0700 (PDT) From: Lucas Tanure To: Rob Herring , Frank Rowand , Mike Rapoport , Andrew Morton Cc: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, jbrunet@baylibre.com, linux-amlogic@lists.infradead.org, linux-arm-kernel@lists.infradead.org, martin.blumenstingl@googlemail.com, narmstrong@baylibre.com, stefan@agner.ch, Lucas Tanure Subject: [PATCH 1/2] memblock: Differentiate regions overlap from both regions being the same Date: Thu, 6 Apr 2023 16:14:28 +0100 Message-Id: <20230406151429.524591-2-tanure@linux.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230406151429.524591-1-tanure@linux.com> References: <20230406151429.524591-1-tanure@linux.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add support for memblock_addrs_overlap to return a different value when both regions are exactly the same region, where base and size are equal between the regions. Signed-off-by: Lucas Tanure --- include/linux/memblock.h | 18 +++++++++++++++--- mm/memblock.c | 37 ++++++++++++++++++++++++------------- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/include/linux/memblock.h b/include/linux/memblock.h index 50ad19662a32..c7ba8b01a637 100644 --- a/include/linux/memblock.h +++ b/include/linux/memblock.h @@ -49,6 +49,18 @@ enum memblock_flags { MEMBLOCK_DRIVER_MANAGED =3D 0x8, /* always detected via a driver */ }; =20 +/** + * enum memblock_overlap_type - result of comparison between two memory re= gions + * @MEMBLOCK_NO_OVERLAPS: there is no overlap between the two regions + * @MEMBLOCK_OVERLAPS: the two regions overlap each other, but are not the= same + * @MEMBLOCK_EQUAL: both bases and sizes are equal, so the two regions are= exactly the same + */ +enum memblock_overlap_type { + MEMBLOCK_NO_OVERLAPS, + MEMBLOCK_OVERLAPS, + MEMBLOCK_EQUAL, +}; + /** * struct memblock_region - represents a memory region * @base: base address of the region @@ -118,8 +130,8 @@ int memblock_reserve(phys_addr_t base, phys_addr_t size= ); int memblock_physmem_add(phys_addr_t base, phys_addr_t size); #endif void memblock_trim_memory(phys_addr_t align); -bool memblock_overlaps_region(struct memblock_type *type, - phys_addr_t base, phys_addr_t size); +unsigned int memblock_overlaps_region(struct memblock_type *type, + phys_addr_t base, phys_addr_t size); int memblock_mark_hotplug(phys_addr_t base, phys_addr_t size); int memblock_clear_hotplug(phys_addr_t base, phys_addr_t size); int memblock_mark_mirror(phys_addr_t base, phys_addr_t size); @@ -486,7 +498,7 @@ bool memblock_is_memory(phys_addr_t addr); bool memblock_is_map_memory(phys_addr_t addr); bool memblock_is_region_memory(phys_addr_t base, phys_addr_t size); bool memblock_is_reserved(phys_addr_t addr); -bool memblock_is_region_reserved(phys_addr_t base, phys_addr_t size); +unsigned int memblock_is_region_reserved(phys_addr_t base, phys_addr_t siz= e); =20 void memblock_dump_all(void); =20 diff --git a/mm/memblock.c b/mm/memblock.c index 25fd0626a9e7..948cc1bc3edf 100644 --- a/mm/memblock.c +++ b/mm/memblock.c @@ -175,24 +175,33 @@ static inline phys_addr_t memblock_cap_size(phys_addr= _t base, phys_addr_t *size) /* * Address comparison utilities */ -static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t ba= se1, phys_addr_t size1, - phys_addr_t base2, phys_addr_t size2) +static unsigned int __init_memblock memblock_addrs_overlap(phys_addr_t bas= e1, phys_addr_t size1, + phys_addr_t base2, phys_addr_t size2) { - return ((base1 < (base2 + size2)) && (base2 < (base1 + size1))); + if (base1 =3D=3D base2 && size1 =3D=3D size2) + return MEMBLOCK_EQUAL; + + if ((base1 < (base2 + size2)) && (base2 < (base1 + size1))) + return MEMBLOCK_OVERLAPS; + + return MEMBLOCK_NO_OVERLAPS; } =20 -bool __init_memblock memblock_overlaps_region(struct memblock_type *type, - phys_addr_t base, phys_addr_t size) +unsigned int __init_memblock memblock_overlaps_region(struct memblock_type= *type, + phys_addr_t base, phys_addr_t size) { - unsigned long i; + unsigned long i, ret; =20 memblock_cap_size(base, &size); =20 - for (i =3D 0; i < type->cnt; i++) - if (memblock_addrs_overlap(base, size, type->regions[i].base, - type->regions[i].size)) - break; - return i < type->cnt; + for (i =3D 0; i < type->cnt; i++) { + ret =3D memblock_addrs_overlap(base, size, type->regions[i].base, + type->regions[i].size); + if (ret) + return ret; + } + + return MEMBLOCK_NO_OVERLAPS; } =20 /** @@ -1857,9 +1866,11 @@ bool __init_memblock memblock_is_region_memory(phys_= addr_t base, phys_addr_t siz * memory block. * * Return: - * True if they intersect, false if not. + * MEMBLOCK_NO_OVERLAPS if there is no intersection, + * MEMBLOCK_OVERLAPS if they only intersect, + * MEMBLOCK_EQUAL if the region matches base and size to an reserved memor= y. */ -bool __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_ad= dr_t size) +unsigned int __init_memblock memblock_is_region_reserved(phys_addr_t base,= phys_addr_t size) { return memblock_overlaps_region(&memblock.reserved, base, size); } --=20 2.40.0 From nobody Wed Feb 11 11:05:55 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 22411C77B6F for ; Thu, 6 Apr 2023 15:15:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239088AbjDFPPD (ORCPT ); Thu, 6 Apr 2023 11:15:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40700 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239727AbjDFPOt (ORCPT ); Thu, 6 Apr 2023 11:14:49 -0400 Received: from mail-wm1-f46.google.com (mail-wm1-f46.google.com [209.85.128.46]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2E4E81980; Thu, 6 Apr 2023 08:14:48 -0700 (PDT) Received: by mail-wm1-f46.google.com with SMTP id d11-20020a05600c3acb00b003ef6e6754c5so20277344wms.5; Thu, 06 Apr 2023 08:14:48 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680794086; x=1683386086; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=VKATmeJF+Xd6RfEgdIKd/kdycc50pAq48rMDI6zu3z4=; b=dt+SmTvy2gx2thRpX0PbgTQkGnR50qRgsPB8Eh9n0afq9RSCNEQn4GbNyOvTfjqUJm W7zZ2I2n230HU6Q/ozN+wVs4VClgTivAw1HfR1Df9ELv0UBLnN9+NVrQueFfOAcgxAl3 uYh2WZR9loalROn20tVp655TpDJAHKnc4EKUoFBvWJnv0Q1EiC9lR7KjDJGggFYiwIvn C9eo0+KrPfLgBu5A40eOmGznzSkgmwHZstIdZ96MSQYh014T2GZNXab/fhTWtPKjnRTF Cq70AtNfaMM24LLI6x2Iy488+X3O0lqsoAgA/wjmsBnP7FIqEl6FZ3Fhb65I8fxV/Xqv WhWw== X-Gm-Message-State: AAQBX9cc2txjeWIOQcER1H7lZT8S6AYWZhAieFKIJ0a5IDWpZpOoWc8U w68K6QUz2zyaGeYM9PRSP+k= X-Google-Smtp-Source: AKy350YOU24o0+DjVUVccG/oqlcWRmAeru9w+HwMxOWGCplLCkcR3X6veQF5xP1/2B5vxx8vQzUciA== X-Received: by 2002:a7b:c850:0:b0:3ef:6ea4:a675 with SMTP id c16-20020a7bc850000000b003ef6ea4a675mr7787556wml.36.1680794086552; Thu, 06 Apr 2023 08:14:46 -0700 (PDT) Received: from ryzen.lan (cpc87451-finc19-2-0-cust61.4-2.cable.virginm.net. [82.11.51.62]) by smtp.gmail.com with ESMTPSA id fj12-20020a05600c0c8c00b003ef67848a21sm5560213wmb.13.2023.04.06.08.14.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Apr 2023 08:14:46 -0700 (PDT) From: Lucas Tanure To: Rob Herring , Frank Rowand , Mike Rapoport , Andrew Morton Cc: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, jbrunet@baylibre.com, linux-amlogic@lists.infradead.org, linux-arm-kernel@lists.infradead.org, martin.blumenstingl@googlemail.com, narmstrong@baylibre.com, stefan@agner.ch, Lucas Tanure Subject: [PATCH 2/2] of: fdt: Allow the kernel to mark nomap regions received from fdt Date: Thu, 6 Apr 2023 16:14:29 +0100 Message-Id: <20230406151429.524591-3-tanure@linux.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230406151429.524591-1-tanure@linux.com> References: <20230406151429.524591-1-tanure@linux.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Reserved regions can be described in FDT and device trees, but FDT doesn't provide the related flags, like nomap. So allow the kernel to mark regions where the base and size received from the device tree are the same as the base and region on FDT. Here we trust that the device tree has a more updated description of the region than the one received from FDT. Signed-off-by: Lucas Tanure --- drivers/of/fdt.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c index d1a68b6d03b3..754a7ea4f45c 100644 --- a/drivers/of/fdt.c +++ b/drivers/of/fdt.c @@ -482,11 +482,13 @@ static int __init early_init_dt_reserve_memory(phys_a= ddr_t base, if (nomap) { /* * If the memory is already reserved (by another region), we - * should not allow it to be marked nomap, but don't worry - * if the region isn't memory as it won't be mapped. + * should not allow it to be marked nomap, unless is the exact same regi= on + * (same base and size), which the kernel knows better and should be all= owed to mark + * it as nomap. + * But don't worry if the region isn't memory as it won't be mapped. */ - if (memblock_overlaps_region(&memblock.memory, base, size) && - memblock_is_region_reserved(base, size)) + if (memblock_overlaps_region(&memblock.memory, base, size) =3D=3D MEMBLO= CK_OVERLAPS && + memblock_is_region_reserved(base, size) =3D=3D MEMBLOCK_OVERLAPS) return -EBUSY; =20 return memblock_mark_nomap(base, size); --=20 2.40.0