Skip to contentSkip to conversation

mm: Support memblock alloc on the exact node for sparse_buffer_init()

PATCH Superseded Yunfeng Ye 3 participants 9 messages Sep 18, 2019 – Sep 19, 2019

Subsystems (3)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Yunfeng Ye

author
#0 lore raw

PATCH 1/1 mm: Support memblock alloc on the exact node for sparse_buffer_init()

Currently, when memblock_find_in_range_node() fail on the exact node, it will use %NUMA_NO_NODE to find memblock from other nodes. At present, the work is good, but when the large memory is insufficient and the small memory is enough, we want to allocate the small memory of this node first, and do not need to allocate large memory from other nodes.

In sparse_buffer_init(), it will prepare large chunks of memory for page structure. The page management structure requires a lot of memory, but if the node does not have enough memory, it can be converted to a small memory allocation without having to allocate it from other nodes.

Add %MEMBLOCK_ALLOC_EXACT_NODE flag for this situation. Normally, the behavior is the same with %MEMBLOCK_ALLOC_ACCESSIBLE, only that it will not allocate from other nodes when a single node fails to allocate.

If large contiguous block memory allocated fail in sparse_buffer_init(), it will allocates small block memmory section by section later.

---
 include/linux/memblock.h | 1 +
 mm/memblock.c            | 3 ++-
 mm/sparse.c              | 2 +-
 3 files changed, 4 insertions(+), 2 deletions(-)
include/linux/memblock.h +1 -0
index f491690..9a81d9c 100644
@@ -339,6 +339,7 @@ static inline int memblock_get_region_node(const struct memblock_region *r)
 #define MEMBLOCK_ALLOC_ANYWHERE	(~(phys_addr_t)0)
 #define MEMBLOCK_ALLOC_ACCESSIBLE	0
 #define MEMBLOCK_ALLOC_KASAN		1
+#define MEMBLOCK_ALLOC_EXACT_NODE	2
 
 /* We are using top down, so it is safe to use 0 here */
 #define MEMBLOCK_LOW_LIMIT 0
mm/memblock.c +2 -1
index 7d4f61a..dbd52c3c 100644
@@ -277,6 +277,7 @@ static phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size,
 
 	/* pump up @end */
 	if (end == MEMBLOCK_ALLOC_ACCESSIBLE ||
+	    end == MEMBLOCK_ALLOC_EXACT_NODE ||
 	    end == MEMBLOCK_ALLOC_KASAN)
 		end = memblock.current_limit;
 
@@ -1365,7 +1366,7 @@ static phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
 	if (found && !memblock_reserve(found, size))
 		goto done;
 
-	if (nid != NUMA_NO_NODE) {
+	if (end != MEMBLOCK_ALLOC_EXACT_NODE && nid != NUMA_NO_NODE) {
 		found = memblock_find_in_range_node(size, align, start,
 						    end, NUMA_NO_NODE,
 						    flags);
mm/sparse.c +1 -1
index 72f010d..828db46 100644
@@ -477,7 +477,7 @@ static void __init sparse_buffer_init(unsigned long size, int nid)
 	sparsemap_buf =
 		memblock_alloc_try_nid_raw(size, PAGE_SIZE,
 						addr,
-						MEMBLOCK_ALLOC_ACCESSIBLE, nid);
+						MEMBLOCK_ALLOC_EXACT_NODE, nid);
 	sparsemap_buf_end = sparsemap_buf + size;
 }
 
Signature · 1 line
2.7.4.huawei.3
#1 lore raw
Quoted 11 lines · Yunfeng Ye

Currently, when memblock_find_in_range_node() fail on the exact node, it will use %NUMA_NO_NODE to find memblock from other nodes. At present, the work is good, but when the large memory is insufficient and the small memory is enough, we want to allocate the small memory of this node first, and do not need to allocate large memory from other nodes.

In sparse_buffer_init(), it will prepare large chunks of memory for page structure. The page management structure requires a lot of memory, but if the node does not have enough memory, it can be converted to a small memory allocation without having to allocate it from other nodes.

Quoted 7 lines

Add %MEMBLOCK_ALLOC_EXACT_NODE flag for this situation. Normally, the behavior is the same with %MEMBLOCK_ALLOC_ACCESSIBLE, only that it will not allocate from other nodes when a single node fails to allocate.

If large contiguous block memory allocated fail in sparse_buffer_init(), it will allocates small block memmory section by section later.

Looks this changes current behavior even it fall back to section based allocation.

Quoted 56 lines

---

include/linux/memblock.h | 1 +
mm/memblock.c            | 3 ++-
mm/sparse.c              | 2 +-
3 files changed, 4 insertions(+), 2 deletions(-)
include/linux/memblock.h +1 -0
index f491690..9a81d9c 100644
@@ -339,6 +339,7 @@ static inline int memblock_get_region_node(const struct memblock_region *r)
 #define MEMBLOCK_ALLOC_ANYWHERE	(~(phys_addr_t)0)
 #define MEMBLOCK_ALLOC_ACCESSIBLE	0
 #define MEMBLOCK_ALLOC_KASAN		1
+#define MEMBLOCK_ALLOC_EXACT_NODE	2
 
 /* We are using top down, so it is safe to use 0 here */
 #define MEMBLOCK_LOW_LIMIT 0
mm/memblock.c +2 -1
index 7d4f61a..dbd52c3c 100644
@@ -277,6 +277,7 @@ static phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size,
 
 	/* pump up @end */
 	if (end == MEMBLOCK_ALLOC_ACCESSIBLE ||
+	    end == MEMBLOCK_ALLOC_EXACT_NODE ||
 	    end == MEMBLOCK_ALLOC_KASAN)
 		end = memblock.current_limit;
 
@@ -1365,7 +1366,7 @@ static phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
 	if (found && !memblock_reserve(found, size))
 		goto done;
 
-	if (nid != NUMA_NO_NODE) {
+	if (end != MEMBLOCK_ALLOC_EXACT_NODE && nid != NUMA_NO_NODE) {
 		found = memblock_find_in_range_node(size, align, start,
 						    end, NUMA_NO_NODE,
 						    flags);
mm/sparse.c +1 -1
index 72f010d..828db46 100644
@@ -477,7 +477,7 @@ static void __init sparse_buffer_init(unsigned long size, int nid)
 	sparsemap_buf =
 		memblock_alloc_try_nid_raw(size, PAGE_SIZE,
 						addr,
-						MEMBLOCK_ALLOC_ACCESSIBLE, nid);
+						MEMBLOCK_ALLOC_EXACT_NODE, nid);
 	sparsemap_buf_end = sparsemap_buf + size;
 }
 
Signature · 1 line
2.7.4.huawei.3
Signature · 2 lines
Wei Yang
Help you, Help me
author
#2 lore raw
Quoted 16 lines · Wei Yang
Quoted 15 lines · Yunfeng Ye

Currently, when memblock_find_in_range_node() fail on the exact node, it will use %NUMA_NO_NODE to find memblock from other nodes. At present, the work is good, but when the large memory is insufficient and the small memory is enough, we want to allocate the small memory of this node first, and do not need to allocate large memory from other nodes.

In sparse_buffer_init(), it will prepare large chunks of memory for page structure. The page management structure requires a lot of memory, but if the node does not have enough memory, it can be converted to a small memory allocation without having to allocate it from other nodes.

Add %MEMBLOCK_ALLOC_EXACT_NODE flag for this situation. Normally, the behavior is the same with %MEMBLOCK_ALLOC_ACCESSIBLE, only that it will not allocate from other nodes when a single node fails to allocate.

Quoted 7 lines
Quoted 3 lines

If large contiguous block memory allocated fail in sparse_buffer_init(), it will allocates small block memmory section by section later.

Looks this changes current behavior even it fall back to section based allocation.

When fall back to section allocation, it still use %MEMBLOCK_ALLOC_ACCESSIBLE ,I think the behavior is not change, Can you tell me the detail about the changes. thanks.

Quoted 57 lines
Quoted 56 lines

---

include/linux/memblock.h | 1 +
mm/memblock.c            | 3 ++-
mm/sparse.c              | 2 +-
3 files changed, 4 insertions(+), 2 deletions(-)
include/linux/memblock.h +1 -0
index f491690..9a81d9c 100644
@@ -339,6 +339,7 @@ static inline int memblock_get_region_node(const struct memblock_region *r)
 #define MEMBLOCK_ALLOC_ANYWHERE	(~(phys_addr_t)0)
 #define MEMBLOCK_ALLOC_ACCESSIBLE	0
 #define MEMBLOCK_ALLOC_KASAN		1
+#define MEMBLOCK_ALLOC_EXACT_NODE	2
 
 /* We are using top down, so it is safe to use 0 here */
 #define MEMBLOCK_LOW_LIMIT 0
mm/memblock.c +2 -1
index 7d4f61a..dbd52c3c 100644
@@ -277,6 +277,7 @@ static phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size,
 
 	/* pump up @end */
 	if (end == MEMBLOCK_ALLOC_ACCESSIBLE ||
+	    end == MEMBLOCK_ALLOC_EXACT_NODE ||
 	    end == MEMBLOCK_ALLOC_KASAN)
 		end = memblock.current_limit;
 
@@ -1365,7 +1366,7 @@ static phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
 	if (found && !memblock_reserve(found, size))
 		goto done;
 
-	if (nid != NUMA_NO_NODE) {
+	if (end != MEMBLOCK_ALLOC_EXACT_NODE && nid != NUMA_NO_NODE) {
 		found = memblock_find_in_range_node(size, align, start,
 						    end, NUMA_NO_NODE,
 						    flags);
mm/sparse.c +1 -1
index 72f010d..828db46 100644
@@ -477,7 +477,7 @@ static void __init sparse_buffer_init(unsigned long size, int nid)
 	sparsemap_buf =
 		memblock_alloc_try_nid_raw(size, PAGE_SIZE,
 						addr,
-						MEMBLOCK_ALLOC_ACCESSIBLE, nid);
+						MEMBLOCK_ALLOC_EXACT_NODE, nid);
 	sparsemap_buf_end = sparsemap_buf + size;
 }
 
Signature · 1 line
2.7.4.huawei.3
#3 lore raw
Quoted 23 lines · Yunfeng Ye
Quoted 20 lines · Wei Yang
Quoted 18 lines · Yunfeng Ye

Currently, when memblock_find_in_range_node() fail on the exact node, it will use %NUMA_NO_NODE to find memblock from other nodes. At present, the work is good, but when the large memory is insufficient and the small memory is enough, we want to allocate the small memory of this node first, and do not need to allocate large memory from other nodes.

In sparse_buffer_init(), it will prepare large chunks of memory for page structure. The page management structure requires a lot of memory, but if the node does not have enough memory, it can be converted to a small memory allocation without having to allocate it from other nodes.

Add %MEMBLOCK_ALLOC_EXACT_NODE flag for this situation. Normally, the behavior is the same with %MEMBLOCK_ALLOC_ACCESSIBLE, only that it will not allocate from other nodes when a single node fails to allocate.

If large contiguous block memory allocated fail in sparse_buffer_init(), it will allocates small block memmory section by section later.

Quoted 7 lines
Quoted 3 lines

Looks this changes current behavior even it fall back to section based allocation.

When fall back to section allocation, it still use %MEMBLOCK_ALLOC_ACCESSIBLE ,I think the behavior is not change, Can you tell me the detail about the changes. thanks.

You pass MEMBLOCK_ALLOC_EXACT_NODE for the first round allocation, which forbid it allocates from other node. This is different from current behavior. Am I right?

#5 lore raw
In reply to Yunfeng Ye

Hi,

Quoted 11 lines · Yunfeng Ye

Currently, when memblock_find_in_range_node() fail on the exact node, it will use %NUMA_NO_NODE to find memblock from other nodes. At present, the work is good, but when the large memory is insufficient and the small memory is enough, we want to allocate the small memory of this node first, and do not need to allocate large memory from other nodes.

In sparse_buffer_init(), it will prepare large chunks of memory for page structure. The page management structure requires a lot of memory, but if the node does not have enough memory, it can be converted to a small memory allocation without having to allocate it from other nodes.

Quoted 6 lines

Add %MEMBLOCK_ALLOC_EXACT_NODE flag for this situation. Normally, the behavior is the same with %MEMBLOCK_ALLOC_ACCESSIBLE, only that it will not allocate from other nodes when a single node fails to allocate.

If large contiguous block memory allocated fail in sparse_buffer_init(), it will allocates small block memmory section by section later.

Did you see the sparse_buffer_init() actually falling back to allocate from a different node? If a node does not have enough memory to hold it's own memory map, filling only it with parts of the memory map will not make such node usable.

Quoted 57 lines
---
 include/linux/memblock.h | 1 +
 mm/memblock.c            | 3 ++-
 mm/sparse.c              | 2 +-
 3 files changed, 4 insertions(+), 2 deletions(-)
include/linux/memblock.h +1 -0
index f491690..9a81d9c 100644
@@ -339,6 +339,7 @@ static inline int memblock_get_region_node(const struct memblock_region *r)
 #define MEMBLOCK_ALLOC_ANYWHERE	(~(phys_addr_t)0)
 #define MEMBLOCK_ALLOC_ACCESSIBLE	0
 #define MEMBLOCK_ALLOC_KASAN		1
+#define MEMBLOCK_ALLOC_EXACT_NODE	2
 
 /* We are using top down, so it is safe to use 0 here */
 #define MEMBLOCK_LOW_LIMIT 0
mm/memblock.c +2 -1
index 7d4f61a..dbd52c3c 100644
@@ -277,6 +277,7 @@ static phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size,
 
 	/* pump up @end */
 	if (end == MEMBLOCK_ALLOC_ACCESSIBLE ||
+	    end == MEMBLOCK_ALLOC_EXACT_NODE ||
 	    end == MEMBLOCK_ALLOC_KASAN)
 		end = memblock.current_limit;
 
@@ -1365,7 +1366,7 @@ static phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
 	if (found && !memblock_reserve(found, size))
 		goto done;
 
-	if (nid != NUMA_NO_NODE) {
+	if (end != MEMBLOCK_ALLOC_EXACT_NODE && nid != NUMA_NO_NODE) {
 		found = memblock_find_in_range_node(size, align, start,
 						    end, NUMA_NO_NODE,
 						    flags);
mm/sparse.c +1 -1
index 72f010d..828db46 100644
@@ -477,7 +477,7 @@ static void __init sparse_buffer_init(unsigned long size, int nid)
 	sparsemap_buf =
 		memblock_alloc_try_nid_raw(size, PAGE_SIZE,
 						addr,
-						MEMBLOCK_ALLOC_ACCESSIBLE, nid);
+						MEMBLOCK_ALLOC_EXACT_NODE, nid);
 	sparsemap_buf_end = sparsemap_buf + size;
 }
 
Signature · 1 line
2.7.4.huawei.3
Signature · 2 lines
Sincerely yours,
Mike.
author
#6 lore raw
Quoted 18 lines · Mike Rapoport

Hi,

Quoted 15 lines · Yunfeng Ye

Currently, when memblock_find_in_range_node() fail on the exact node, it will use %NUMA_NO_NODE to find memblock from other nodes. At present, the work is good, but when the large memory is insufficient and the small memory is enough, we want to allocate the small memory of this node first, and do not need to allocate large memory from other nodes.

In sparse_buffer_init(), it will prepare large chunks of memory for page structure. The page management structure requires a lot of memory, but if the node does not have enough memory, it can be converted to a small memory allocation without having to allocate it from other nodes.

Add %MEMBLOCK_ALLOC_EXACT_NODE flag for this situation. Normally, the behavior is the same with %MEMBLOCK_ALLOC_ACCESSIBLE, only that it will not allocate from other nodes when a single node fails to allocate.

Quoted 8 lines
Quoted 2 lines

If large contiguous block memory allocated fail in sparse_buffer_init(), it will allocates small block memmory section by section later.

Did you see the sparse_buffer_init() actually falling back to allocate from a different node? If a node does not have enough memory to hold it's own memory map, filling only it with parts of the memory map will not make such node usable.

Normally, it won't happen that sparse_buffer_init() falling back from a different node, because page structure size is 64 bytes per 4KB of memory, no more than 2% of total available memory. But in the special cases, for eaxmple, memory address is isolated by BIOS when memory failure, split the total memory many pieces, although we have enough memory, but no large contiguous block memory in one node. sparse_buffer_init() needs large contiguous block memory to be alloc in one time,

Eg, the size of memory is 1TB, sparse_buffer_init() need 1TB * 64/4096 = 16GB, but we have 100 blocks memory which every block only have 10GB, although total memory have almost 100*10GB=1TB, but no contiguous 16GB block.

Before commit 2a3cb8baef71 ("mm/sparse: delete old sparse_init and enable new one"), we have %CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER config to meeting this situation, after that, it fall back to allocate memory from other nodes, so have the performance impact by remote numa access.

commit 85c77f791390 ("mm/sparse: add new sparse_init_nid() and sparse_init()") wrote that:
"
sparse_init_nid(), which only
operates within one memory node, and thus allocates memory either in large contiguous block or allocates section by section
"
it means that allocates section by section is a normal choice too, so I think add %MEMBLOCK_ALLOC_EXACT_NODE is also a choice for this situation. Most cases, sparse_buffer_init() works good and not allocated from other nodes at present.

thanks.
Yunfeng Ye

Quoted 58 lines
Quoted 57 lines
---
 include/linux/memblock.h | 1 +
 mm/memblock.c            | 3 ++-
 mm/sparse.c              | 2 +-
 3 files changed, 4 insertions(+), 2 deletions(-)
include/linux/memblock.h +1 -0
index f491690..9a81d9c 100644
@@ -339,6 +339,7 @@ static inline int memblock_get_region_node(const struct memblock_region *r)
 #define MEMBLOCK_ALLOC_ANYWHERE	(~(phys_addr_t)0)
 #define MEMBLOCK_ALLOC_ACCESSIBLE	0
 #define MEMBLOCK_ALLOC_KASAN		1
+#define MEMBLOCK_ALLOC_EXACT_NODE	2
 
 /* We are using top down, so it is safe to use 0 here */
 #define MEMBLOCK_LOW_LIMIT 0
mm/memblock.c +2 -1
index 7d4f61a..dbd52c3c 100644
@@ -277,6 +277,7 @@ static phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size,
 
 	/* pump up @end */
 	if (end == MEMBLOCK_ALLOC_ACCESSIBLE ||
+	    end == MEMBLOCK_ALLOC_EXACT_NODE ||
 	    end == MEMBLOCK_ALLOC_KASAN)
 		end = memblock.current_limit;
 
@@ -1365,7 +1366,7 @@ static phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
 	if (found && !memblock_reserve(found, size))
 		goto done;
 
-	if (nid != NUMA_NO_NODE) {
+	if (end != MEMBLOCK_ALLOC_EXACT_NODE && nid != NUMA_NO_NODE) {
 		found = memblock_find_in_range_node(size, align, start,
 						    end, NUMA_NO_NODE,
 						    flags);
mm/sparse.c +1 -1
index 72f010d..828db46 100644
@@ -477,7 +477,7 @@ static void __init sparse_buffer_init(unsigned long size, int nid)
 	sparsemap_buf =
 		memblock_alloc_try_nid_raw(size, PAGE_SIZE,
 						addr,
-						MEMBLOCK_ALLOC_ACCESSIBLE, nid);
+						MEMBLOCK_ALLOC_EXACT_NODE, nid);
 	sparsemap_buf_end = sparsemap_buf + size;
 }
 
Signature · 1 line
2.7.4.huawei.3
#7 lore raw
Quoted 36 lines · Yunfeng Ye
Quoted 18 lines · Mike Rapoport

Hi,

Quoted 15 lines · Yunfeng Ye

Currently, when memblock_find_in_range_node() fail on the exact node, it will use %NUMA_NO_NODE to find memblock from other nodes. At present, the work is good, but when the large memory is insufficient and the small memory is enough, we want to allocate the small memory of this node first, and do not need to allocate large memory from other nodes.

In sparse_buffer_init(), it will prepare large chunks of memory for page structure. The page management structure requires a lot of memory, but if the node does not have enough memory, it can be converted to a small memory allocation without having to allocate it from other nodes.

Add %MEMBLOCK_ALLOC_EXACT_NODE flag for this situation. Normally, the behavior is the same with %MEMBLOCK_ALLOC_ACCESSIBLE, only that it will not allocate from other nodes when a single node fails to allocate.

Quoted 8 lines
Quoted 2 lines

If large contiguous block memory allocated fail in sparse_buffer_init(), it will allocates small block memmory section by section later.

Did you see the sparse_buffer_init() actually falling back to allocate from a different node? If a node does not have enough memory to hold it's own memory map, filling only it with parts of the memory map will not make such node usable.

Normally, it won't happen that sparse_buffer_init() falling back from a different node, because page structure size is 64 bytes per 4KB of memory, no more than 2% of total available memory. But in the special cases, for eaxmple, memory address is isolated by BIOS when memory failure, split the total memory many pieces, although we have enough memory, but no large contiguous block memory in one node. sparse_buffer_init() needs large contiguous block memory to be alloc in one time,

Quoted 3 lines

Eg, the size of memory is 1TB, sparse_buffer_init() need 1TB * 64/4096 = 16GB, but we have 100 blocks memory which every block only have 10GB, although total memory have almost 100*10GB=1TB, but no contiguous 16GB block.

An explanation that a node memory may become highly fragmented should be a part of the changelog.

Quoted 5 lines

Before commit 2a3cb8baef71 ("mm/sparse: delete old sparse_init and enable new one"), we have %CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER config to meeting this situation, after that, it fall back to allocate memory from other nodes, so have the performance impact by remote numa access.

Quoted 10 lines

commit 85c77f791390 ("mm/sparse: add new sparse_init_nid() and sparse_init()") wrote that:
"
sparse_init_nid(), which only
operates within one memory node, and thus allocates memory either in large contiguous block or allocates section by section
"
it means that allocates section by section is a normal choice too, so I think add %MEMBLOCK_ALLOC_EXACT_NODE is also a choice for this situation. Most cases, sparse_buffer_init() works good and not allocated from other nodes at present.

I'd prefer to see memblock_alloc_exact_nid_raw() wrapper for memblock_find_in_range_node() rather than using a flag.

Quoted 62 lines

thanks.
Yunfeng Ye

Quoted 58 lines
Quoted 57 lines
---
 include/linux/memblock.h | 1 +
 mm/memblock.c            | 3 ++-
 mm/sparse.c              | 2 +-
 3 files changed, 4 insertions(+), 2 deletions(-)
include/linux/memblock.h +1 -0
index f491690..9a81d9c 100644
@@ -339,6 +339,7 @@ static inline int memblock_get_region_node(const struct memblock_region *r)
 #define MEMBLOCK_ALLOC_ANYWHERE	(~(phys_addr_t)0)
 #define MEMBLOCK_ALLOC_ACCESSIBLE	0
 #define MEMBLOCK_ALLOC_KASAN		1
+#define MEMBLOCK_ALLOC_EXACT_NODE	2
 
 /* We are using top down, so it is safe to use 0 here */
 #define MEMBLOCK_LOW_LIMIT 0
mm/memblock.c +2 -1
index 7d4f61a..dbd52c3c 100644
@@ -277,6 +277,7 @@ static phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size,
 
 	/* pump up @end */
 	if (end == MEMBLOCK_ALLOC_ACCESSIBLE ||
+	    end == MEMBLOCK_ALLOC_EXACT_NODE ||
 	    end == MEMBLOCK_ALLOC_KASAN)
 		end = memblock.current_limit;
 
@@ -1365,7 +1366,7 @@ static phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
 	if (found && !memblock_reserve(found, size))
 		goto done;
 
-	if (nid != NUMA_NO_NODE) {
+	if (end != MEMBLOCK_ALLOC_EXACT_NODE && nid != NUMA_NO_NODE) {
 		found = memblock_find_in_range_node(size, align, start,
 						    end, NUMA_NO_NODE,
 						    flags);
mm/sparse.c +1 -1
index 72f010d..828db46 100644
@@ -477,7 +477,7 @@ static void __init sparse_buffer_init(unsigned long size, int nid)
 	sparsemap_buf =
 		memblock_alloc_try_nid_raw(size, PAGE_SIZE,
 						addr,
-						MEMBLOCK_ALLOC_ACCESSIBLE, nid);
+						MEMBLOCK_ALLOC_EXACT_NODE, nid);
 	sparsemap_buf_end = sparsemap_buf + size;
 }
 
Signature · 1 line
2.7.4.huawei.3
Signature · 2 lines
Sincerely yours,
Mike.
author
#4 lore raw
In reply to Wei Yang
Quoted 27 lines · Wei Yang
Quoted 26 lines · Yunfeng Ye
Quoted 23 lines · Wei Yang
Quoted 11 lines · Yunfeng Ye

Currently, when memblock_find_in_range_node() fail on the exact node, it will use %NUMA_NO_NODE to find memblock from other nodes. At present, the work is good, but when the large memory is insufficient and the small memory is enough, we want to allocate the small memory of this node first, and do not need to allocate large memory from other nodes.

In sparse_buffer_init(), it will prepare large chunks of memory for page structure. The page management structure requires a lot of memory, but if the node does not have enough memory, it can be converted to a small memory allocation without having to allocate it from other nodes.

Quoted 7 lines

Add %MEMBLOCK_ALLOC_EXACT_NODE flag for this situation. Normally, the behavior is the same with %MEMBLOCK_ALLOC_ACCESSIBLE, only that it will not allocate from other nodes when a single node fails to allocate.

If large contiguous block memory allocated fail in sparse_buffer_init(), it will allocates small block memmory section by section later.

Looks this changes current behavior even it fall back to section based allocation.

Quoted 9 lines
Quoted 4 lines

When fall back to section allocation, it still use %MEMBLOCK_ALLOC_ACCESSIBLE ,I think the behavior is not change, Can you tell me the detail about the changes. thanks.

You pass MEMBLOCK_ALLOC_EXACT_NODE for the first round allocation, which forbid it allocates from other node. This is different from current behavior. Am I right?

Most cases, it will not go to the %MEMBLOCK_ALLOC_EXACT_NODE allocate routine. so the behavior will not change.
If current node have no large contiguous block memory, it will fall back to allocate memory based section. this is the different from current behavior.

thanks.

Quoted 2 lines

.

author
#8 lore raw
In reply to Mike Rapoport
Quoted 37 lines · Mike Rapoport
Quoted 36 lines · Yunfeng Ye
Quoted 18 lines · Mike Rapoport

Hi,

Quoted 15 lines · Yunfeng Ye

Currently, when memblock_find_in_range_node() fail on the exact node, it will use %NUMA_NO_NODE to find memblock from other nodes. At present, the work is good, but when the large memory is insufficient and the small memory is enough, we want to allocate the small memory of this node first, and do not need to allocate large memory from other nodes.

In sparse_buffer_init(), it will prepare large chunks of memory for page structure. The page management structure requires a lot of memory, but if the node does not have enough memory, it can be converted to a small memory allocation without having to allocate it from other nodes.

Add %MEMBLOCK_ALLOC_EXACT_NODE flag for this situation. Normally, the behavior is the same with %MEMBLOCK_ALLOC_ACCESSIBLE, only that it will not allocate from other nodes when a single node fails to allocate.

Quoted 8 lines
Quoted 2 lines

If large contiguous block memory allocated fail in sparse_buffer_init(), it will allocates small block memmory section by section later.

Did you see the sparse_buffer_init() actually falling back to allocate from a different node? If a node does not have enough memory to hold it's own memory map, filling only it with parts of the memory map will not make such node usable.

Normally, it won't happen that sparse_buffer_init() falling back from a different node, because page structure size is 64 bytes per 4KB of memory, no more than 2% of total available memory. But in the special cases, for eaxmple, memory address is isolated by BIOS when memory failure, split the total memory many pieces, although we have enough memory, but no large contiguous block memory in one node. sparse_buffer_init() needs large contiguous block memory to be alloc in one time,

Quoted 7 lines
Quoted 3 lines

Eg, the size of memory is 1TB, sparse_buffer_init() need 1TB * 64/4096 = 16GB, but we have 100 blocks memory which every block only have 10GB, although total memory have almost 100*10GB=1TB, but no contiguous 16GB block.

An explanation that a node memory may become highly fragmented should be a part of the changelog.

ok, thanks for your advice.

Quoted 16 lines
Quoted 5 lines

Before commit 2a3cb8baef71 ("mm/sparse: delete old sparse_init and enable new one"), we have %CONFIG_SPARSEMEM_ALLOC_MEM_MAP_TOGETHER config to meeting this situation, after that, it fall back to allocate memory from other nodes, so have the performance impact by remote numa access.

Quoted 10 lines

commit 85c77f791390 ("mm/sparse: add new sparse_init_nid() and sparse_init()") wrote that:
"
sparse_init_nid(), which only
operates within one memory node, and thus allocates memory either in large contiguous block or allocates section by section
"
it means that allocates section by section is a normal choice too, so I think add %MEMBLOCK_ALLOC_EXACT_NODE is also a choice for this situation. Most cases, sparse_buffer_init() works good and not allocated from other nodes at present.

Quoted 3 lines

I'd prefer to see memblock_alloc_exact_nid_raw() wrapper for memblock_find_in_range_node() rather than using a flag.

I've also thought about this modification method, I will modify as you suggest. thanks.

Quoted 63 lines
Quoted 62 lines

thanks.
Yunfeng Ye

Quoted 58 lines
Quoted 57 lines
---
 include/linux/memblock.h | 1 +
 mm/memblock.c            | 3 ++-
 mm/sparse.c              | 2 +-
 3 files changed, 4 insertions(+), 2 deletions(-)
include/linux/memblock.h +1 -0
index f491690..9a81d9c 100644
@@ -339,6 +339,7 @@ static inline int memblock_get_region_node(const struct memblock_region *r)
 #define MEMBLOCK_ALLOC_ANYWHERE	(~(phys_addr_t)0)
 #define MEMBLOCK_ALLOC_ACCESSIBLE	0
 #define MEMBLOCK_ALLOC_KASAN		1
+#define MEMBLOCK_ALLOC_EXACT_NODE	2
 
 /* We are using top down, so it is safe to use 0 here */
 #define MEMBLOCK_LOW_LIMIT 0
mm/memblock.c +2 -1
index 7d4f61a..dbd52c3c 100644
@@ -277,6 +277,7 @@ static phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size,
 
 	/* pump up @end */
 	if (end == MEMBLOCK_ALLOC_ACCESSIBLE ||
+	    end == MEMBLOCK_ALLOC_EXACT_NODE ||
 	    end == MEMBLOCK_ALLOC_KASAN)
 		end = memblock.current_limit;
 
@@ -1365,7 +1366,7 @@ static phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
 	if (found && !memblock_reserve(found, size))
 		goto done;
 
-	if (nid != NUMA_NO_NODE) {
+	if (end != MEMBLOCK_ALLOC_EXACT_NODE && nid != NUMA_NO_NODE) {
 		found = memblock_find_in_range_node(size, align, start,
 						    end, NUMA_NO_NODE,
 						    flags);
mm/sparse.c +1 -1
index 72f010d..828db46 100644
@@ -477,7 +477,7 @@ static void __init sparse_buffer_init(unsigned long size, int nid)
 	sparsemap_buf =
 		memblock_alloc_try_nid_raw(size, PAGE_SIZE,
 						addr,
-						MEMBLOCK_ALLOC_ACCESSIBLE, nid);
+						MEMBLOCK_ALLOC_EXACT_NODE, nid);
 	sparsemap_buf_end = sparsemap_buf + size;
 }
 
Signature · 1 line
2.7.4.huawei.3