[RESEND PATCH 07/10] memblock: track kernel size on memsize

From: Jaewon Kim
Date: Mon May 20 2024 - 22:41:18 EST


Some memory regions are already being tracked by previous patches. But
there are many memory allocations from memblock and frees to memblock
during the boot time.

This patch tracks the memblock size used for the common kernel. To to
this, tracking memblock size is disabled for some memory handling logics
like early param, device tree, and default cma size.

For precise kernel size, this patch counts not actually freed size to
buddy at boot time, and does not count freed size from ramdisk and init
section.

Additionally this patch does one important thing. This patch blocks
memblock_add_range of memblock_remove_range not to update memsize if
free pages were already released to the buddy allocator.

This is an example. The kernel size is newly added by this patch.

.kernel : 135137 KB
.unusable : 788073 KB
.reusable : 294912 KB

Signed-off-by: Jaewon Kim <jaewon31.kim@xxxxxxxxxxx>
---
drivers/of/fdt.c | 6 ++++++
include/linux/memblock.h | 6 ++++++
kernel/dma/contiguous.c | 7 ++++--
mm/memblock.c | 46 ++++++++++++++++++++++++++++++++++++++++
mm/page_alloc.c | 10 ++++++++-
5 files changed, 72 insertions(+), 3 deletions(-)

diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c
index da82e5afed01..08638673e106 100644
--- a/drivers/of/fdt.c
+++ b/drivers/of/fdt.c
@@ -524,6 +524,8 @@ void __init early_init_fdt_scan_reserved_mem(void)
if (!initial_boot_params)
return;

+ memblock_memsize_disable_tracking();
+
fdt_scan_reserved_mem();
fdt_reserve_elfcorehdr();

@@ -537,6 +539,7 @@ void __init early_init_fdt_scan_reserved_mem(void)
}

fdt_init_reserved_mem();
+ memblock_memsize_enable_tracking();
}

/**
@@ -1189,12 +1192,15 @@ void __init early_init_dt_scan_nodes(void)
if (rc)
pr_warn("No chosen node found, continuing without\n");

+ memblock_memsize_disable_tracking();
+
/* Setup memory, calling early_init_dt_add_memory_arch */
early_init_dt_scan_memory();

/* Handle linux,usable-memory-range property */
early_init_dt_check_for_usable_mem_range();

+ memblock_memsize_enable_tracking();
memblock_memsize_detect_hole();
}

diff --git a/include/linux/memblock.h b/include/linux/memblock.h
index aef02c150f2c..a83ad98ac252 100644
--- a/include/linux/memblock.h
+++ b/include/linux/memblock.h
@@ -620,6 +620,9 @@ extern void memblock_memsize_record(const char *name, phys_addr_t base,
extern void memblock_memsize_detect_hole(void);
extern void memblock_memsize_set_name(const char *name);
extern void memblock_memsize_unset_name(void);
+extern void memblock_memsize_enable_tracking(void);
+extern void memblock_memsize_disable_tracking(void);
+extern void memblock_memsize_mod_kernel_size(long size);
#else
static inline void memblock_memsize_record(const char *name, phys_addr_t base,
phys_addr_t size, bool nomap,
@@ -627,6 +630,9 @@ static inline void memblock_memsize_record(const char *name, phys_addr_t base,
static inline void memblock_memsize_detect_hole(void) { }
static inline void memblock_memsize_set_name(const char *name) { }
static inline void memblock_memsize_unset_name(void) { }
+static inline void memblock_memsize_enable_tracking(void){ }
+static inline void memblock_memsize_disable_tracking(void){ }
+static inline void memblock_memsize_mod_kernel_size(long size) { }
#endif

#endif /* _LINUX_MEMBLOCK_H */
diff --git a/kernel/dma/contiguous.c b/kernel/dma/contiguous.c
index 437c85878280..e9c68b1ee975 100644
--- a/kernel/dma/contiguous.c
+++ b/kernel/dma/contiguous.c
@@ -277,10 +277,11 @@ int __init dma_contiguous_reserve_area(phys_addr_t size, phys_addr_t base,
{
int ret;

+ memblock_memsize_disable_tracking();
ret = cma_declare_contiguous(base, size, limit, 0, 0, fixed,
"reserved", res_cma);
if (ret)
- return ret;
+ goto out;

/* Architecture specific contiguous memory fixup. */
dma_contiguous_early_fixup(cma_get_base(*res_cma),
@@ -288,7 +289,9 @@ int __init dma_contiguous_reserve_area(phys_addr_t size, phys_addr_t base,

memblock_memsize_record("dma_cma", cma_get_base(*res_cma),
cma_get_size(*res_cma), false, true);
- return 0;
+out:
+ memblock_memsize_enable_tracking();
+ return ret;
}

/**
diff --git a/mm/memblock.c b/mm/memblock.c
index de49e7ce74f1..bb033c20ec43 100644
--- a/mm/memblock.c
+++ b/mm/memblock.c
@@ -2060,6 +2060,23 @@ struct memsize_rgn_struct {
static struct memsize_rgn_struct memsize_rgn[CONFIG_MAX_MEMBLOCK_MEMSIZE] __initdata_memblock;
static int memsize_rgn_count __initdata_memblock;
static const char *memblock_memsize_name __initdata_memblock;
+static long memsize_kinit __initdata_memblock;
+static bool memblock_memsize_tracking __initdata_memblock = true;
+
+void __init memblock_memsize_enable_tracking(void)
+{
+ memblock_memsize_tracking = true;
+}
+
+void __init memblock_memsize_disable_tracking(void)
+{
+ memblock_memsize_tracking = false;
+}
+
+void memblock_memsize_mod_kernel_size(long size)
+{
+ memsize_kinit += size;
+}

static void __init_memblock memsize_get_valid_name(char *valid_name, const char *name)
{
@@ -2313,6 +2330,12 @@ static void __init_memblock memblock_memsize_record_add(struct memblock_type *ty
base, size, false, false);
else if (type == &memblock.memory)
memblock_memsize_free(base, size);
+ } else if (memblock_memsize_tracking) {
+ if (type == &memblock.reserved) {
+ memblock_dbg("%s: kernel %lu %+ld\n", __func__,
+ memsize_kinit, (unsigned long)size);
+ memsize_kinit += size;
+ }
}
}

@@ -2325,6 +2348,12 @@ static void __init_memblock memblock_memsize_record_remove(struct memblock_type
else if (type == &memblock.memory)
memblock_memsize_record(memblock_memsize_name,
base, size, true, false);
+ } else if (memblock_memsize_tracking) {
+ if (type == &memblock.reserved) {
+ memblock_dbg("%s: kernel %lu %+ld\n", __func__,
+ memsize_kinit, (unsigned long)size);
+ memsize_kinit -= size;
+ }
}
}
#endif /* MEMBLOCK_MEMSIZE */
@@ -2442,6 +2471,19 @@ static unsigned long __init __free_memory_core(phys_addr_t start,
unsigned long end_pfn = min_t(unsigned long,
PFN_DOWN(end), max_low_pfn);

+#ifdef CONFIG_MEMBLOCK_MEMSIZE
+ unsigned long start_align_up = PFN_ALIGN(start);
+ unsigned long end_align_down = PFN_PHYS(end_pfn);
+
+ if (start_pfn >= end_pfn) {
+ memblock_memsize_mod_kernel_size(end - start);
+ } else {
+ if (start_align_up > start)
+ memblock_memsize_mod_kernel_size(start_align_up - start);
+ if (end_pfn != max_low_pfn && end_align_down < end)
+ memblock_memsize_mod_kernel_size(end - end_align_down);
+ }
+#endif
if (start_pfn >= end_pfn)
return 0;

@@ -2546,6 +2588,8 @@ void __init memblock_free_all(void)

pages = free_low_memory_core_early();
totalram_pages_add(pages);
+
+ memblock_memsize_disable_tracking();
}

#if defined(CONFIG_DEBUG_FS) && defined(CONFIG_ARCH_KEEP_MEMBLOCK)
@@ -2672,6 +2716,8 @@ static int memblock_memsize_show(struct seq_file *m, void *private)
}

seq_puts(m, "\n");
+ seq_printf(m, " .kernel : %7lu KB\n",
+ DIV_ROUND_UP(memsize_kinit, SZ_1K));
seq_printf(m, " .unusable : %7lu KB\n",
DIV_ROUND_UP(reserved, SZ_1K));
seq_printf(m, " .reusable : %7lu KB\n",
diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index 2e22ce5675ca..a4c692635e9b 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -20,6 +20,7 @@
#include <linux/highmem.h>
#include <linux/interrupt.h>
#include <linux/jiffies.h>
+#include <linux/memblock.h>
#include <linux/compiler.h>
#include <linux/kernel.h>
#include <linux/kasan.h>
@@ -5776,8 +5777,15 @@ unsigned long free_reserved_area(void *start, void *end, int poison, const char
free_reserved_page(page);
}

- if (pages && s)
+ if (pages && s) {
pr_info("Freeing %s memory: %ldK\n", s, K(pages));
+ if (!strcmp(s, "initrd") || !strcmp(s, "unused kernel")) {
+ long size;
+
+ size = -1 * (long)(pages << PAGE_SHIFT);
+ memblock_memsize_mod_kernel_size(size);
+ }
+ }

return pages;
}
--
2.25.1