Skip to contentSkip to conversation

memblock: add no-map alloc functions

PATCH Superseded [email protected] 3 participants 5 messages Apr 15, 2024 – Apr 17, 2024

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 memblock: add no-map alloc functions

From: Daero Lee <[email protected]>

Like reserved-memory with the no-map property, there are memory regions need to be allocated in memblock.memory marked with the MEMBLOCK_NOMAP flag, but sholud not be allocated in memblock.reserved.

So, functions were added that find the required memory area in memblock.memory, but do not allocate it to memblock.reserved.

The early_init_dt_alloc_reserved_memory_arch function was modified using the no-map alloc function.

---
 drivers/of/of_reserved_mem.c |  9 +++--
 mm/memblock.c                | 78 ++++++++++++++++++++++++++++++++++++
 2 files changed, 84 insertions(+), 3 deletions(-)
drivers/of/of_reserved_mem.c +6 -3
index 8236ecae2953..504f2f60689c 100644
@@ -40,15 +40,18 @@ static int __init early_init_dt_alloc_reserved_memory_arch(phys_addr_t size,
 
 	end = !end ? MEMBLOCK_ALLOC_ANYWHERE : end;
 	align = !align ? SMP_CACHE_BYTES : align;
-	base = memblock_phys_alloc_range(size, align, start, end);
+	if (nomap) {
+		base = memblock_phys_alloc_range_nomap(size, align, start, end);
+	} else {
+		base = memblock_phys_alloc_range(size, align, start, end);
+	}
+	
 	if (!base)
 		return -ENOMEM;
 
 	*res_base = base;
 	if (nomap) {
 		err = memblock_mark_nomap(base, size);
-		if (err)
-			memblock_phys_free(base, size);
 	}
 
 	kmemleak_ignore_phys(base);
mm/memblock.c +78 -0

90 lines not on this page · raw message

Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 5 lines · On Mon, Apr 15, 2024 at 11:24:48PM +0900, [email protected] wrote:

From: Daero Lee <[email protected]>

Like reserved-memory with the no-map property, there are memory regions need to be allocated in memblock.memory marked with the MEMBLOCK_NOMAP flag, but sholud not be allocated in memblock.reserved.

Can you please explain your use case?
Why do you need this functionality?

Quoted 17 lines

So, functions were added that find the required memory area in memblock.memory, but do not allocate it to memblock.reserved.

The early_init_dt_alloc_reserved_memory_arch function was modified using the no-map alloc function.

---
 drivers/of/of_reserved_mem.c |  9 +++--
 mm/memblock.c                | 78 ++++++++++++++++++++++++++++++++++++
 2 files changed, 84 insertions(+), 3 deletions(-)
drivers/of/of_reserved_mem.c +0 -0
index 8236ecae2953..504f2f60689c 100644

drivers/of/of_reserved_mem.c static int __init early_init_dt_alloc_reserved_memory_arch(phys_addr_t size, · line 48

Quoted 10 lines
drivers/of/of_reserved_mem.c +6 -1
@@ -40,15 +40,18 @@ static int __init early_init_dt_alloc_reserved_memory_arch(phys_addr_t size,
 
 	end = !end ? MEMBLOCK_ALLOC_ANYWHERE : end;
 	align = !align ? SMP_CACHE_BYTES : align;
-	base = memblock_phys_alloc_range(size, align, start, end);
+	if (nomap) {
+		base = memblock_phys_alloc_range_nomap(size, align, start, end);
+	} else {
+		base = memblock_phys_alloc_range(size, align, start, end);
+	}
+	

This changes behaviour of internal function, what effect will it have on the users?

Quoted 11 lines
drivers/of/of_reserved_mem.c +0 -2
 	if (!base)
 		return -ENOMEM;
 
 	*res_base = base;
 	if (nomap) {
 		err = memblock_mark_nomap(base, size);
-		if (err)
-			memblock_phys_free(base, size);
 	}
 
 	kmemleak_ignore_phys(base);
Signature · 2 lines
Sincerely yours,
Mike.
#2 lore raw

On Mon, Apr 15, 2024 at 06:26:55PM +0300, Mike Rapoport wrote:
[...]

Quoted 11 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+	

This changes behaviour of internal function, what effect will it have on the users?

Quoted 6 lines
drivers/of/of_reserved_mem.c +0 -0
 	if (!base)
 		return -ENOMEM;
 
 	*res_base = base;
 	if (nomap) {
 		err = memblock_mark_nomap(base, size);

Mike

One question may not directly relevant to this thread.

NOMAP doesn't apply to all arch? I took a look into the direct mapping function on x86, memory_map_top_down(). It seems iterate all available pfn instead of skipping NOMAP range.

Quoted 9 lines
Quoted 5 lines
drivers/of/of_reserved_mem.c +0 -2
-		if (err)
-			memblock_phys_free(base, size);
 	}
 
 	kmemleak_ignore_phys(base);
Signature · 2 lines
Sincerely yours,
Mike.
Signature · 2 lines
Wei Yang
Help you, Help me
maintainer
#3 lore raw
Quoted 14 lines · Wei Yang

On Mon, Apr 15, 2024 at 06:26:55PM +0300, Mike Rapoport wrote:
[...]

Quoted 11 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+	

This changes behaviour of internal function, what effect will it have on the users?

Quoted 6 lines
drivers/of/of_reserved_mem.c +0 -0
 	if (!base)
 		return -ENOMEM;
 
 	*res_base = base;
 	if (nomap) {
 		err = memblock_mark_nomap(base, size);
Quoted 7 lines

Mike

One question may not directly relevant to this thread.

NOMAP doesn't apply to all arch? I took a look into the direct mapping function on x86, memory_map_top_down(). It seems iterate all available pfn instead of skipping NOMAP range.

Right, x86 does not use NOMAP.
NOMAP was introduced to ensure that device/firmware memory on arm64 does not have an alias mapping in the direct map because MMU does not allow alias mapping with different caching modes.

Quoted 13 lines
Quoted 9 lines
Quoted 5 lines
drivers/of/of_reserved_mem.c +0 -2
-		if (err)
-			memblock_phys_free(base, size);
 	}
 
 	kmemleak_ignore_phys(base);
Signature · 2 lines
Sincerely yours,
Mike.
Signature · 2 lines
Wei Yang
Help you, Help me
Signature · 2 lines
Sincerely yours,
Mike.
#4 lore raw
In reply to [email protected]
Quoted 64 lines · On Mon, Apr 15, 2024 at 11:24:48PM +0900, [email protected] wrote:

From: Daero Lee <[email protected]>

Like reserved-memory with the no-map property, there are memory regions need to be allocated in memblock.memory marked with the MEMBLOCK_NOMAP flag, but sholud not be allocated in memblock.reserved.

So, functions were added that find the required memory area in memblock.memory, but do not allocate it to memblock.reserved.

The early_init_dt_alloc_reserved_memory_arch function was modified using the no-map alloc function.

---
drivers/of/of_reserved_mem.c | 9 +++--
mm/memblock.c | 78 ++++++++++++++++++++++++++++++++++++

2 files changed, 84 insertions(+), 3 deletions(-)
drivers/of/of_reserved_mem.c +6 -3
index 8236ecae2953..504f2f60689c 100644
@@ -40,15 +40,18 @@ static int __init early_init_dt_alloc_reserved_memory_arch(phys_addr_t size,
 
 	end = !end ? MEMBLOCK_ALLOC_ANYWHERE : end;
 	align = !align ? SMP_CACHE_BYTES : align;
-	base = memblock_phys_alloc_range(size, align, start, end);
+	if (nomap) {
+		base = memblock_phys_alloc_range_nomap(size, align, start, end);
+	} else {
+		base = memblock_phys_alloc_range(size, align, start, end);
+	}
+	
 	if (!base)
 		return -ENOMEM;
 
 	*res_base = base;
 	if (nomap) {
 		err = memblock_mark_nomap(base, size);
-		if (err)
-			memblock_phys_free(base, size);
 	}
 
 	kmemleak_ignore_phys(base);
mm/memblock.c +12 -0
index d09136e040d3..f103f1ecbfad 100644
@@ -1506,6 +1506,72 @@ phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
 	return found;
 }
 
+phys_addr_t __init memblock_alloc_range_nid_nomap(phys_addr_t size,
+                                        phys_addr_t align, phys_addr_t start,
+                                        phys_addr_t end, int nid,
+                                        bool exact_nid)
+{
+        enum memblock_flags flags = choose_memblock_flags();
+        phys_addr_t found;
+
+        if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
+                nid = NUMA_NO_NODE;
+
+        if (!align) {

mm/memblock.c phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size, · line 1530

Quoted 10 lines
mm/memblock.c +10 -0
+                /* Can't use WARNs this early in boot on powerpc */
+                dump_stack();
+                align = SMP_CACHE_BYTES;
+        }
+
+again:
+        found = memblock_find_in_range_node(size, align, start, end, nid,
+                                            flags);
+        if (found)
+                goto done;

Only find without reserve has no effect on memblock meta data. It seems run but nothing happens.

What you want to achieve for this function?

Signature · 2 lines
Wei Yang
Help you, Help me