[RESEND PATCH 06/10] memblock: recognize late freed size by checking PageReserved

Jaewon Kim
2024-05-21T02:40:17Z

There are some cases in which reserved pages are freed late after the
initial memblock_free_all of mem_init. We'd like to recognize this
late free pages, and update the memsize information.

Because additional job is needed to a no-map or reusable region, the
late free is usually done to a map and unusable region. So only for map
and unusable region, check if some pages within the region is freed. The
freed pages can be recoginzed by checking if PageReserved flag is clear.
To be fast, let's skip other pages within 64 KB range. And this check is
done when a user wants to see the memsize information.

This is an example. If all pages are freed the region size will be 0.

Before
0x0a2300000-0x0a2400000 0x00100000 ( 1024 KB ) map unusable latefree

After
0x0a2300000-0x0a2300000 0x00000000 ( 0 KB ) map unusable latefree

Signed-off-by: Jaewon Kim <jaewon31.kim@samsung.com>
---
mm/memblock.c | 34 ++++++++++++++++++++++++++++++++++
1 file changed, 34 insertions(+)

diff --git a/mm/memblock.c b/mm/memblock.c
index edb2575967ab..de49e7ce74f1 100644
--- a/mm/memblock.c
+++ b/mm/memblock.c
@@ -2608,6 +2608,39 @@ static int memsize_rgn_cmp(const void *a, const void *b)
return 0;
}

+/* assume that freed size is always 64 KB aligned */
+static inline void memblock_memsize_check_size(struct memsize_rgn_struct *rgn)
+{
+ phys_addr_t phy, end, freed = 0;
+ bool has_freed = false;
+ struct page *page;
+
+ if (rgn->reusable || rgn->nomap)
+ return;
+
+ /* check the first page of each 1 MB */
+ phy = rgn->base;
+ end = rgn->base + rgn->size;
+ while (phy < end) {
+ unsigned long pfn = __phys_to_pfn(phy);
+
+ if (!pfn_valid(pfn))
+ return;
+ page = pfn_to_page(pfn);
+ if (!has_freed && !PageReserved(page)) {
+ has_freed = true;
+ freed = phy;
+ } else if (has_freed && PageReserved(page)) {
+ has_freed = false;
+ memblock_memsize_free(freed, phy - freed);
+ }
+
+ if (has_freed && (phy + SZ_64K >= end))
+ memblock_memsize_free(freed, end - freed);
+ phy += SZ_64K;
+ }
+}
+
static int memblock_memsize_show(struct seq_file *m, void *private)
{
int i;
@@ -2621,6 +2654,7 @@ static int memblock_memsize_show(struct seq_file *m, void *private)
long size;

rgn = &memsize_rgn[i];
+ memblock_memsize_check_size(rgn);
base = rgn->base;
size = rgn->size;
end = base + size;
--
2.25.1