Skip to contentSkip to conversation

optimize memblock_next_valid_pfn and early_pfn_valid

PATCH Superseded Jia He 3 participants 25 messages Mar 26, 2018 – Apr 3, 2018

Subsystems (7)
Memory management x86 MM core arm64 +3 more subsystems

v3 of 11 Superseded Latest is v11 →

lore
Share & tools
lore

Jia He

author
#0 lore raw

v3 cover optimize memblock_next_valid_pfn and early_pfn_valid

Commit b92df1de5d28 ("mm: page_alloc: skip over regions of invalid pfns where possible") tried to optimize the loop in memmap_init_zone(). But there is still some room for improvement.

Patch 1 remain the memblock_next_valid_pfn when CONFIG_HAVE_ARCH_PFN_VALID
is enabled
Patch 2 optimizes the memblock_next_valid_pfn()
Patch 3~5 optimizes the early_pfn_valid(), I have to split it into parts
because the changes are located across subsystems.

I tested the pfn loop process in memmap_init(), the same as before. As for the performance improvement, after this set, I can see the time overhead of memmap_init() is reduced from 41313 us to 24345 us in my armv8a server(QDF2400 with 96G memory).

Attached the memblock region information in my server.

[   86.956758] Zone ranges:
[   86.959452]   DMA      [mem 0x0000000000200000-0x00000000ffffffff]
[   86.966041]   Normal   [mem 0x0000000100000000-0x00000017ffffffff]
[   86.972631] Movable zone start for each node
[   86.977179] Early memory node ranges
[   86.980985]   node   0: [mem 0x0000000000200000-0x000000000021ffff]
[   86.987666]   node   0: [mem 0x0000000000820000-0x000000000307ffff]
[   86.994348]   node   0: [mem 0x0000000003080000-0x000000000308ffff]
[   87.001029]   node   0: [mem 0x0000000003090000-0x00000000031fffff]
[   87.007710]   node   0: [mem 0x0000000003200000-0x00000000033fffff]
[   87.014392]   node   0: [mem 0x0000000003410000-0x000000000563ffff]
[   87.021073]   node   0: [mem 0x0000000005640000-0x000000000567ffff]
[   87.027754]   node   0: [mem 0x0000000005680000-0x00000000056dffff]
[   87.034435]   node   0: [mem 0x00000000056e0000-0x00000000086fffff]
[   87.041117]   node   0: [mem 0x0000000008700000-0x000000000871ffff]
[   87.047798]   node   0: [mem 0x0000000008720000-0x000000000894ffff]
[   87.054479]   node   0: [mem 0x0000000008950000-0x0000000008baffff]
[   87.061161]   node   0: [mem 0x0000000008bb0000-0x0000000008bcffff]
[   87.067842]   node   0: [mem 0x0000000008bd0000-0x0000000008c4ffff]
[   87.074524]   node   0: [mem 0x0000000008c50000-0x0000000008e2ffff]
[   87.081205]   node   0: [mem 0x0000000008e30000-0x0000000008e4ffff]
[   87.087886]   node   0: [mem 0x0000000008e50000-0x0000000008fcffff]
[   87.094568]   node   0: [mem 0x0000000008fd0000-0x000000000910ffff]
[   87.101249]   node   0: [mem 0x0000000009110000-0x00000000092effff]
[   87.107930]   node   0: [mem 0x00000000092f0000-0x000000000930ffff]
[   87.114612]   node   0: [mem 0x0000000009310000-0x000000000963ffff]
[   87.121293]   node   0: [mem 0x0000000009640000-0x000000000e61ffff]
[   87.127975]   node   0: [mem 0x000000000e620000-0x000000000e64ffff]
[   87.134657]   node   0: [mem 0x000000000e650000-0x000000000fffffff]
[   87.141338]   node   0: [mem 0x0000000010800000-0x0000000017feffff]
[   87.148019]   node   0: [mem 0x000000001c000000-0x000000001c00ffff]
[   87.154701]   node   0: [mem 0x000000001c010000-0x000000001c7fffff]
[   87.161383]   node   0: [mem 0x000000001c810000-0x000000007efbffff]
[   87.168064]   node   0: [mem 0x000000007efc0000-0x000000007efdffff]
[   87.174746]   node   0: [mem 0x000000007efe0000-0x000000007efeffff]
[   87.181427]   node   0: [mem 0x000000007eff0000-0x000000007effffff]
[   87.188108]   node   0: [mem 0x000000007f000000-0x00000017ffffffff]
[   87.194791] Initmem setup node 0 [mem 0x0000000000200000-0x00000017ffffffff]

Without this patchset:

[  117.106153] Initmem setup node 0 [mem 0x0000000000200000-0x00000017ffffffff]
[  117.113677] before memmap_init
[  117.118195] after  memmap_init
Quoted 1 line
Quoted 1 line
Quoted 1 line

memmap_init takes 4518 us

[  117.121446] before memmap_init
[  117.154992] after  memmap_init
Quoted 1 line
Quoted 1 line
Quoted 1 line

memmap_init takes 33546 us

[  117.158241] before memmap_init
[  117.161490] after  memmap_init
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

memmap_init takes 3249 us
totally takes 41313 us

With this patchset:

[   87.194791] Initmem setup node 0 [mem 0x0000000000200000-0x00000017ffffffff]
[   87.202314] before memmap_init
[   87.206164] after  memmap_init
Quoted 1 line
Quoted 1 line
Quoted 1 line

memmap_init takes 3850 us

[   87.209416] before memmap_init
[   87.226662] after  memmap_init
Quoted 1 line
Quoted 1 line
Quoted 1 line

memmap_init takes 17246 us

[   87.229911] before memmap_init
[   87.233160] after  memmap_init
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

memmap_init takes 3249 us
totally takes 24345 us

Changelog:
V3: - fix 2 issues reported by kbuild test robot
V2: - rebase to mmotm latest
- remain memblock_next_valid_pfn on arm64
- refine memblock_search_pfn_regions and pfn_valid_region

Jia He (5):
mm: page_alloc: remain memblock_next_valid_pfn() when
CONFIG_HAVE_ARCH_PFN_VALID is enable
mm: page_alloc: reduce unnecessary binary search in
memblock_next_valid_pfn()
mm/memblock: introduce memblock_search_pfn_regions()
arm64: introduce pfn_valid_region()
mm: page_alloc: reduce unnecessary binary search in early_pfn_valid()

 arch/arm64/include/asm/page.h    |  3 ++-
 arch/arm64/mm/init.c             | 25 ++++++++++++++++++-
 arch/x86/include/asm/mmzone_32.h |  2 +-
 include/linux/memblock.h         |  6 +++++
 include/linux/mmzone.h           | 12 ++++++---
 mm/memblock.c                    | 53 ++++++++++++++++++++++++++++++++++++++++
 mm/page_alloc.c                  | 12 ++++++++-
 7 files changed, 106 insertions(+), 7 deletions(-)
Signature · 1 line
2.7.4
author
#1 lore raw

PATCH v3 1/5 mm: page_alloc: remain memblock_next_valid_pfn() when CONFIG_HAVE_ARCH_PFN_VALID is enable

Commit b92df1de5d28 ("mm: page_alloc: skip over regions of invalid pfns where possible") optimized the loop in memmap_init_zone(). But it causes possible panic bug. So Daniel Vacek reverted it later.

But memblock_next_valid_pfn is valid when CONFIG_HAVE_ARCH_PFN_VALID is enable. And as verified by Eugeniu Rosca, arm can benifit from this commit. So remain the memblock_next_valid_pfn.

---
 include/linux/memblock.h |  4 ++++
 mm/memblock.c            | 29 +++++++++++++++++++++++++++++
 mm/page_alloc.c          | 11 ++++++++++-
 3 files changed, 43 insertions(+), 1 deletion(-)
include/linux/memblock.h +4 -0
index 0257aee..efbbe4b 100644
@@ -203,6 +203,10 @@ void __next_mem_pfn_range(int *idx, int nid, unsigned long *out_start_pfn,
 	     i >= 0; __next_mem_pfn_range(&i, nid, p_start, p_end, p_nid))
 #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
 
+#ifdef CONFIG_HAVE_ARCH_PFN_VALID
+unsigned long memblock_next_valid_pfn(unsigned long pfn);
+#endif
+
 /**
  * for_each_free_mem_range - iterate through free memblock areas
  * @i: u64 used as loop variable
mm/memblock.c +29 -0
index ba7c878..bea5a9c 100644
@@ -1102,6 +1102,35 @@ void __init_memblock __next_mem_pfn_range(int *idx, int nid,
 		*out_nid = r->nid;
 }
 
+#ifdef CONFIG_HAVE_ARCH_PFN_VALID
+unsigned long __init_memblock memblock_next_valid_pfn(unsigned long pfn)
+{
+	struct memblock_type *type = &memblock.memory;
+	unsigned int right = type->cnt;
+	unsigned int mid, left = 0;
+	phys_addr_t addr = PFN_PHYS(++pfn);
+
+	do {
+		mid = (right + left) / 2;
+
+		if (addr < type->regions[mid].base)
+			right = mid;
+		else if (addr >= (type->regions[mid].base +
+				  type->regions[mid].size))
+			left = mid + 1;
+		else {
+			/* addr is within the region, so pfn is valid */
+			return pfn;
+		}
+	} while (left < right);
+
+	if (right == type->cnt)
+		return -1UL;
+	else
+		return PHYS_PFN(type->regions[right].base);
+}
+#endif /*CONFIG_HAVE_ARCH_PFN_VALID*/
+
 /**
  * memblock_set_node - set node ID on memblock regions
  * @base: base of area to set node ID for
mm/page_alloc.c +10 -1
index c19f5ac..2a967f7 100644
@@ -5483,8 +5483,17 @@ void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
 		if (context != MEMMAP_EARLY)
 			goto not_early;
 
-		if (!early_pfn_valid(pfn))
+		if (!early_pfn_valid(pfn)) {
+#if (defined CONFIG_HAVE_MEMBLOCK) && (defined CONFIG_HAVE_ARCH_PFN_VALID)
+			/*
+			 * Skip to the pfn preceding the next valid one (or
+			 * end_pfn), such that we hit a valid pfn (or end_pfn)
+			 * on our next iteration of the loop.
+			 */
+			pfn = memblock_next_valid_pfn(pfn) - 1;
+#endif
 			continue;
+		}
 		if (!early_pfn_in_nid(pfn, nid))
 			continue;
 		if (!update_defer_init(pgdat, pfn, end_pfn, &nr_initialised))
Signature · 1 line
2.7.4
author
#7 lore raw

PATCH v3 2/5 mm: page_alloc: reduce unnecessary binary search in memblock_next_valid_pfn()

In reply to Jia He

Commit b92df1de5d28 ("mm: page_alloc: skip over regions of invalid pfns where possible") optimized the loop in memmap_init_zone(). But there is still some room for improvement. E.g. if pfn and pfn+1 are in the same memblock region, we can simply pfn++ instead of doing the binary search in memblock_next_valid_pfn. This patch only works when CONFIG_HAVE_ARCH_PFN_VALID is enable.

---
 include/linux/memblock.h |  2 +-
 mm/memblock.c            | 73 +++++++++++++++++++++++++++++-------------------
 mm/page_alloc.c          |  3 +-
 3 files changed, 47 insertions(+), 31 deletions(-)
include/linux/memblock.h +1 -1
index efbbe4b..a8fb2ab 100644
@@ -204,7 +204,7 @@ void __next_mem_pfn_range(int *idx, int nid, unsigned long *out_start_pfn,
 #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
 
 #ifdef CONFIG_HAVE_ARCH_PFN_VALID
-unsigned long memblock_next_valid_pfn(unsigned long pfn);
+unsigned long memblock_next_valid_pfn(unsigned long pfn, int *idx);
 #endif
 
 /**
mm/memblock.c +44 -29

85 lines not on this page · raw message

mm/page_alloc.c +2 -1
index 2a967f7..0bb0274 100644
@@ -5459,6 +5459,7 @@ void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
 	unsigned long end_pfn = start_pfn + size;
 	pg_data_t *pgdat = NODE_DATA(nid);
 	unsigned long pfn;
+	int idx = -1;
 	unsigned long nr_initialised = 0;
 	struct page *page;
 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
@@ -5490,7 +5491,7 @@ void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
 			 * end_pfn), such that we hit a valid pfn (or end_pfn)
 			 * on our next iteration of the loop.
 			 */
-			pfn = memblock_next_valid_pfn(pfn) - 1;
+			pfn = memblock_next_valid_pfn(pfn, &idx) - 1;
 #endif
 			continue;
 		}
Signature · 1 line
2.7.4
author
#14 lore raw

PATCH v3 3/5 mm/memblock: introduce memblock_search_pfn_regions()

In reply to Jia He

This api is the preparation for further optimizing early_pfn_valid

---
 include/linux/memblock.h | 2 ++
 mm/memblock.c            | 9 +++++++++
 2 files changed, 11 insertions(+)
include/linux/memblock.h +2 -0
index a8fb2ab..104bca6 100644
@@ -207,6 +207,8 @@ void __next_mem_pfn_range(int *idx, int nid, unsigned long *out_start_pfn,
 unsigned long memblock_next_valid_pfn(unsigned long pfn, int *idx);
 #endif
 
+int memblock_search_pfn_regions(unsigned long pfn);
+
 /**
  * for_each_free_mem_range - iterate through free memblock areas
  * @i: u64 used as loop variable
mm/memblock.c +9 -0
index 06c1a08..15fcde2 100644
@@ -1661,6 +1661,15 @@ static int __init_memblock memblock_search(struct memblock_type *type, phys_addr
 	return -1;
 }
 
+/* search memblock with the input pfn, return the region idx */
+int __init_memblock memblock_search_pfn_regions(unsigned long pfn)
+{
+	struct memblock_type *type = &memblock.memory;
+	int mid = memblock_search(type, PFN_PHYS(pfn));
+
+	return mid;
+}
+
 bool __init memblock_is_reserved(phys_addr_t addr)
 {
 	return memblock_search(&memblock.reserved, addr) != -1;
Signature · 1 line
2.7.4
author
#15 lore raw

PATCH v3 4/5 arm64: introduce pfn_valid_region()

In reply to Jia He

This is the preparation for further optimizing in early_pfn_valid on arm64.

---
 arch/arm64/include/asm/page.h |  3 ++-
 arch/arm64/mm/init.c          | 25 ++++++++++++++++++++++++-
 2 files changed, 26 insertions(+), 2 deletions(-)
arch/arm64/include/asm/page.h +2 -1
index 60d02c8..da2cba3 100644
@@ -38,7 +38,8 @@ extern void clear_page(void *to);
 typedef struct page *pgtable_t;
 
 #ifdef CONFIG_HAVE_ARCH_PFN_VALID
-extern int pfn_valid(unsigned long);
+extern int pfn_valid(unsigned long pfn);
+extern int pfn_valid_region(unsigned long pfn, int *last_idx);
 #endif
 
 #include <asm/memory.h>
arch/arm64/mm/init.c +24 -1
index 00e7b90..06433d5 100644
@@ -290,7 +290,30 @@ int pfn_valid(unsigned long pfn)
 	return memblock_is_map_memory(pfn << PAGE_SHIFT);
 }
 EXPORT_SYMBOL(pfn_valid);
-#endif
+
+int pfn_valid_region(unsigned long pfn, int *last_idx)
+{
+	unsigned long start_pfn, end_pfn;
+	struct memblock_type *type = &memblock.memory;
+
+	if (*last_idx != -1) {
+		start_pfn = PFN_DOWN(type->regions[*last_idx].base);
+		end_pfn= PFN_DOWN(type->regions[*last_idx].base +
+					type->regions[*last_idx].size);
+
+		if (pfn >= start_pfn && pfn < end_pfn)
+			return !memblock_is_nomap(
+				&memblock.memory.regions[*last_idx]);
+	}
+
+	*last_idx = memblock_search_pfn_regions(pfn);
+	if (*last_idx == -1)
+		return false;
+
+	return !memblock_is_nomap(&memblock.memory.regions[*last_idx]);
+}
+EXPORT_SYMBOL(pfn_valid_region);
+#endif /*CONFIG_HAVE_ARCH_PFN_VALID*/
 
 #ifndef CONFIG_SPARSEMEM
 static void __init arm64_memory_present(void)
Signature · 1 line
2.7.4
author
#17 lore raw

PATCH v3 5/5 mm: page_alloc: reduce unnecessary binary search in early_pfn_valid()

In reply to Jia He

Commit b92df1de5d28 ("mm: page_alloc: skip over regions of invalid pfns where possible") optimized the loop in memmap_init_zone(). But there is still some room for improvement. E.g. in early_pfn_valid(), if pfn and pfn+1 are in the same memblock region, we can record the last returned memblock region index and check check pfn++ is still in the same region.

Currently it only improve the performance on arm64 and will have no impact on other arches.

---
 arch/x86/include/asm/mmzone_32.h |  2 +-
 include/linux/mmzone.h           | 12 +++++++++---
 mm/page_alloc.c                  |  2 +-
 3 files changed, 11 insertions(+), 5 deletions(-)
arch/x86/include/asm/mmzone_32.h +1 -1
index 73d8dd1..329d3ba 100644
@@ -49,7 +49,7 @@ static inline int pfn_valid(int pfn)
 	return 0;
 }
 
-#define early_pfn_valid(pfn)	pfn_valid((pfn))
+#define early_pfn_valid(pfn, last_region_idx)	pfn_valid((pfn))
 
 #endif /* CONFIG_DISCONTIGMEM */
 
include/linux/mmzone.h +9 -3
index d797716..3a686af 100644
@@ -1267,9 +1267,15 @@ static inline int pfn_present(unsigned long pfn)
 })
 #else
 #define pfn_to_nid(pfn)		(0)
-#endif
+#endif /*CONFIG_NUMA*/
+
+#ifdef CONFIG_HAVE_ARCH_PFN_VALID
+#define early_pfn_valid(pfn, last_region_idx) \
+				pfn_valid_region(pfn, last_region_idx)
+#else
+#define early_pfn_valid(pfn, last_region_idx)	pfn_valid(pfn)
+#endif /*CONFIG_HAVE_ARCH_PFN_VALID*/
 
-#define early_pfn_valid(pfn)	pfn_valid(pfn)
 void sparse_init(void);
 #else
 #define sparse_init()	do {} while (0)
@@ -1288,7 +1294,7 @@ struct mminit_pfnnid_cache {
 };
 
 #ifndef early_pfn_valid
-#define early_pfn_valid(pfn)	(1)
+#define early_pfn_valid(pfn, last_region_idx)	(1)
 #endif
 
 void memory_present(int nid, unsigned long start, unsigned long end);
mm/page_alloc.c +1 -1
index 0bb0274..debccf3 100644
@@ -5484,7 +5484,7 @@ void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
 		if (context != MEMMAP_EARLY)
 			goto not_early;
 
-		if (!early_pfn_valid(pfn)) {
+		if (!early_pfn_valid(pfn, &idx)) {
 #if (defined CONFIG_HAVE_MEMBLOCK) && (defined CONFIG_HAVE_ARCH_PFN_VALID)
 			/*
 			 * Skip to the pfn preceding the next valid one (or
Signature · 1 line
2.7.4
#20 lore raw
In reply to Jia He
Quoted 45 lines · Jia He

Commit b92df1de5d28 ("mm: page_alloc: skip over regions of invalid pfns where possible") tried to optimize the loop in memmap_init_zone(). But there is still some room for improvement.

Patch 1 remain the memblock_next_valid_pfn when CONFIG_HAVE_ARCH_PFN_VALID
is enabled
Patch 2 optimizes the memblock_next_valid_pfn()
Patch 3~5 optimizes the early_pfn_valid(), I have to split it into parts
because the changes are located across subsystems.

I tested the pfn loop process in memmap_init(), the same as before. As for the performance improvement, after this set, I can see the time overhead of memmap_init() is reduced from 41313 us to 24345 us in my armv8a server(QDF2400 with 96G memory).

Attached the memblock region information in my server.

[   86.956758] Zone ranges:
[   86.959452]   DMA      [mem 0x0000000000200000-0x00000000ffffffff]
[   86.966041]   Normal   [mem 0x0000000100000000-0x00000017ffffffff]
[   86.972631] Movable zone start for each node
[   86.977179] Early memory node ranges
[   86.980985]   node   0: [mem 0x0000000000200000-0x000000000021ffff]
[   86.987666]   node   0: [mem 0x0000000000820000-0x000000000307ffff]
[   86.994348]   node   0: [mem 0x0000000003080000-0x000000000308ffff]
[   87.001029]   node   0: [mem 0x0000000003090000-0x00000000031fffff]
[   87.007710]   node   0: [mem 0x0000000003200000-0x00000000033fffff]
[   87.014392]   node   0: [mem 0x0000000003410000-0x000000000563ffff]
[   87.021073]   node   0: [mem 0x0000000005640000-0x000000000567ffff]
[   87.027754]   node   0: [mem 0x0000000005680000-0x00000000056dffff]
[   87.034435]   node   0: [mem 0x00000000056e0000-0x00000000086fffff]
[   87.041117]   node   0: [mem 0x0000000008700000-0x000000000871ffff]
[   87.047798]   node   0: [mem 0x0000000008720000-0x000000000894ffff]
[   87.054479]   node   0: [mem 0x0000000008950000-0x0000000008baffff]
[   87.061161]   node   0: [mem 0x0000000008bb0000-0x0000000008bcffff]
[   87.067842]   node   0: [mem 0x0000000008bd0000-0x0000000008c4ffff]
[   87.074524]   node   0: [mem 0x0000000008c50000-0x0000000008e2ffff]
[   87.081205]   node   0: [mem 0x0000000008e30000-0x0000000008e4ffff]
[   87.087886]   node   0: [mem 0x0000000008e50000-0x0000000008fcffff]
[   87.094568]   node   0: [mem 0x0000000008fd0000-0x000000000910ffff]
[   87.101249]   node   0: [mem 0x0000000009110000-0x00000000092effff]
[   87.107930]   node   0: [mem 0x00000000092f0000-0x000000000930ffff]
[   87.114612]   node   0: [mem 0x0000000009310000-0x000000000963ffff]
[   87.121293]   node   0: [mem 0x0000000009640000-0x000000000e61ffff]
[   87.127975]   node   0: [mem 0x000000000e620000-0x000000000e64ffff]
[   87.134657]   node   0: [mem 0x000000000e650000-0x000000000fffffff]
Quoted 8 lines
[   87.141338]   node   0: [mem 0x0000000010800000-0x0000000017feffff]
[   87.148019]   node   0: [mem 0x000000001c000000-0x000000001c00ffff]
[   87.154701]   node   0: [mem 0x000000001c010000-0x000000001c7fffff]
[   87.161383]   node   0: [mem 0x000000001c810000-0x000000007efbffff]
[   87.168064]   node   0: [mem 0x000000007efc0000-0x000000007efdffff]
[   87.174746]   node   0: [mem 0x000000007efe0000-0x000000007efeffff]
[   87.181427]   node   0: [mem 0x000000007eff0000-0x000000007effffff]
[   87.188108]   node   0: [mem 0x000000007f000000-0x00000017ffffffff]

Hi, Jia

I haven't taken a deep look into your code, just one curious question on your memory layout.

The log above is printed out in free_area_init_nodes(), which iterates on memblock.memory and prints them. If I am not wrong, memory regions added to memblock.memory are ordered and merged if possible.

While from your log, I see many regions could be merged but are isolated. For example, the last two region:

node 0: [mem 0x000000007eff0000-0x000000007effffff]
node 0: [mem 0x000000007f000000-0x00000017ffffffff]

So I am curious why they are isolated instead of combined to one.

Quoted 1 line

From the code, the possible reason is the region's flag differs from each

other. If you have time, would you mind taking a look into this?

Signature · 2 lines
Wei Yang
Help you, Help me
author
#21 lore raw

On 3/27/2018 9:02 AM, Wei Yang Wrote:

Quoted 66 lines · Jia He
Quoted 45 lines

Commit b92df1de5d28 ("mm: page_alloc: skip over regions of invalid pfns where possible") tried to optimize the loop in memmap_init_zone(). But there is still some room for improvement.

Patch 1 remain the memblock_next_valid_pfn when CONFIG_HAVE_ARCH_PFN_VALID
is enabled
Patch 2 optimizes the memblock_next_valid_pfn()
Patch 3~5 optimizes the early_pfn_valid(), I have to split it into parts
because the changes are located across subsystems.

I tested the pfn loop process in memmap_init(), the same as before. As for the performance improvement, after this set, I can see the time overhead of memmap_init() is reduced from 41313 us to 24345 us in my armv8a server(QDF2400 with 96G memory).

Attached the memblock region information in my server.

[   86.956758] Zone ranges:
[   86.959452]   DMA      [mem 0x0000000000200000-0x00000000ffffffff]
[   86.966041]   Normal   [mem 0x0000000100000000-0x00000017ffffffff]
[   86.972631] Movable zone start for each node
[   86.977179] Early memory node ranges
[   86.980985]   node   0: [mem 0x0000000000200000-0x000000000021ffff]
[   86.987666]   node   0: [mem 0x0000000000820000-0x000000000307ffff]
[   86.994348]   node   0: [mem 0x0000000003080000-0x000000000308ffff]
[   87.001029]   node   0: [mem 0x0000000003090000-0x00000000031fffff]
[   87.007710]   node   0: [mem 0x0000000003200000-0x00000000033fffff]
[   87.014392]   node   0: [mem 0x0000000003410000-0x000000000563ffff]
[   87.021073]   node   0: [mem 0x0000000005640000-0x000000000567ffff]
[   87.027754]   node   0: [mem 0x0000000005680000-0x00000000056dffff]
[   87.034435]   node   0: [mem 0x00000000056e0000-0x00000000086fffff]
[   87.041117]   node   0: [mem 0x0000000008700000-0x000000000871ffff]
[   87.047798]   node   0: [mem 0x0000000008720000-0x000000000894ffff]
[   87.054479]   node   0: [mem 0x0000000008950000-0x0000000008baffff]
[   87.061161]   node   0: [mem 0x0000000008bb0000-0x0000000008bcffff]
[   87.067842]   node   0: [mem 0x0000000008bd0000-0x0000000008c4ffff]
[   87.074524]   node   0: [mem 0x0000000008c50000-0x0000000008e2ffff]
[   87.081205]   node   0: [mem 0x0000000008e30000-0x0000000008e4ffff]
[   87.087886]   node   0: [mem 0x0000000008e50000-0x0000000008fcffff]
[   87.094568]   node   0: [mem 0x0000000008fd0000-0x000000000910ffff]
[   87.101249]   node   0: [mem 0x0000000009110000-0x00000000092effff]
[   87.107930]   node   0: [mem 0x00000000092f0000-0x000000000930ffff]
[   87.114612]   node   0: [mem 0x0000000009310000-0x000000000963ffff]
[   87.121293]   node   0: [mem 0x0000000009640000-0x000000000e61ffff]
[   87.127975]   node   0: [mem 0x000000000e620000-0x000000000e64ffff]
[   87.134657]   node   0: [mem 0x000000000e650000-0x000000000fffffff]
Quoted 8 lines
[   87.141338]   node   0: [mem 0x0000000010800000-0x0000000017feffff]
[   87.148019]   node   0: [mem 0x000000001c000000-0x000000001c00ffff]
[   87.154701]   node   0: [mem 0x000000001c010000-0x000000001c7fffff]
[   87.161383]   node   0: [mem 0x000000001c810000-0x000000007efbffff]
[   87.168064]   node   0: [mem 0x000000007efc0000-0x000000007efdffff]
[   87.174746]   node   0: [mem 0x000000007efe0000-0x000000007efeffff]
[   87.181427]   node   0: [mem 0x000000007eff0000-0x000000007effffff]
[   87.188108]   node   0: [mem 0x000000007f000000-0x00000017ffffffff]

Hi, Jia

I haven't taken a deep look into your code, just one curious question on your memory layout.

The log above is printed out in free_area_init_nodes(), which iterates on memblock.memory and prints them. If I am not wrong, memory regions added to memblock.memory are ordered and merged if possible.

While from your log, I see many regions could be merged but are isolated. For example, the last two region:

Quoted 8 lines
   node   0: [mem 0x000000007eff0000-0x000000007effffff]
   node   0: [mem 0x000000007f000000-0x00000017ffffffff]

So I am curious why they are isolated instead of combined to one.

Quoted 1 line

From the code, the possible reason is the region's flag differs from each

other. If you have time, would you mind taking a look into this?

Hi Wei
I thought these 2 have different flags
[    0.000000] idx=30,region [7eff0000:10000]flag=4     <--- aka MEMBLOCK_NOMAP [    0.000000]   node   0: [mem 0x000000007eff0000-0x000000007effffff] [    0.000000] idx=31,region [7f000000:81000000]flag=0 <--- aka MEMBLOCK_NONE [    0.000000]   node   0: [mem 0x000000007f000000-0x00000017ffffffff]

Signature · 2 lines
Cheers,
Jia
#8 lore raw

Re: [PATCH v3 2/5] mm: page_alloc: reduce unnecessary binary search in memblock_next_valid_pfn()

In reply to Jia He
Quoted 140 lines · Jia He

Commit b92df1de5d28 ("mm: page_alloc: skip over regions of invalid pfns where possible") optimized the loop in memmap_init_zone(). But there is still some room for improvement. E.g. if pfn and pfn+1 are in the same memblock region, we can simply pfn++ instead of doing the binary search in memblock_next_valid_pfn. This patch only works when CONFIG_HAVE_ARCH_PFN_VALID is enable.

---
 include/linux/memblock.h |  2 +-
 mm/memblock.c            | 73 +++++++++++++++++++++++++++++-------------------
 mm/page_alloc.c          |  3 +-
 3 files changed, 47 insertions(+), 31 deletions(-)
include/linux/memblock.h +1 -1
index efbbe4b..a8fb2ab 100644
@@ -204,7 +204,7 @@ void __next_mem_pfn_range(int *idx, int nid, unsigned long *out_start_pfn,
 #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
 
 #ifdef CONFIG_HAVE_ARCH_PFN_VALID
-unsigned long memblock_next_valid_pfn(unsigned long pfn);
+unsigned long memblock_next_valid_pfn(unsigned long pfn, int *idx);
 #endif
 
 /**
mm/memblock.c +44 -29
index bea5a9c..06c1a08 100644
@@ -1102,35 +1102,6 @@ void __init_memblock __next_mem_pfn_range(int *idx, int nid,
                *out_nid = r->nid;
 }
 
-#ifdef CONFIG_HAVE_ARCH_PFN_VALID
-unsigned long __init_memblock memblock_next_valid_pfn(unsigned long pfn)
-{
-       struct memblock_type *type = &memblock.memory;
-       unsigned int right = type->cnt;
-       unsigned int mid, left = 0;
-       phys_addr_t addr = PFN_PHYS(++pfn);
-
-       do {
-               mid = (right + left) / 2;
-
-               if (addr < type->regions[mid].base)
-                       right = mid;
-               else if (addr >= (type->regions[mid].base +
-                                 type->regions[mid].size))
-                       left = mid + 1;
-               else {
-                       /* addr is within the region, so pfn is valid */
-                       return pfn;
-               }
-       } while (left < right);
-
-       if (right == type->cnt)
-               return -1UL;
-       else
-               return PHYS_PFN(type->regions[right].base);
-}
-#endif /*CONFIG_HAVE_ARCH_PFN_VALID*/
-
 /**
  * memblock_set_node - set node ID on memblock regions
  * @base: base of area to set node ID for
@@ -1162,6 +1133,50 @@ int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
 }
 #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
 
+#ifdef CONFIG_HAVE_ARCH_PFN_VALID
+unsigned long __init_memblock memblock_next_valid_pfn(unsigned long pfn,
+                                                       int *last_idx)
+{
+       struct memblock_type *type = &memblock.memory;
+       unsigned int right = type->cnt;
+       unsigned int mid, left = 0;
+       unsigned long start_pfn, end_pfn;
+       phys_addr_t addr = PFN_PHYS(++pfn);
+
+       /* fast path, return pfh+1 if next pfn is in the same region */
+       if (*last_idx != -1) {
+               start_pfn = PFN_DOWN(type->regions[*last_idx].base);
+               end_pfn = PFN_DOWN(type->regions[*last_idx].base +
+                               type->regions[*last_idx].size);
+
+               if (pfn < end_pfn && pfn > start_pfn)
+                       return pfn;
+       }
+
+       /* slow path, do the binary searching */
+       do {
+               mid = (right + left) / 2;
+
+               if (addr < type->regions[mid].base)
+                       right = mid;
+               else if (addr >= (type->regions[mid].base +
+                                 type->regions[mid].size))
+                       left = mid + 1;
+               else {
+                       *last_idx = mid;
+                       return pfn;
+               }
+       } while (left < right);
+
+       if (right == type->cnt)
+               return -1UL;
+
+       *last_idx = right;
+
+       return PHYS_PFN(type->regions[*last_idx].base);
+}
+#endif /*CONFIG_HAVE_ARCH_PFN_VALID*/
+
 static phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
                                        phys_addr_t align, phys_addr_t start,
                                        phys_addr_t end, int nid, ulong flags)
mm/page_alloc.c +2 -2
index 2a967f7..0bb0274 100644
@@ -5459,6 +5459,7 @@ void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
        unsigned long end_pfn = start_pfn + size;
        pg_data_t *pgdat = NODE_DATA(nid);
        unsigned long pfn;
+       int idx = -1;
        unsigned long nr_initialised = 0;
        struct page *page;
 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
@@ -5490,7 +5491,7 @@ void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
                         * end_pfn), such that we hit a valid pfn (or end_pfn)
                         * on our next iteration of the loop.
                         */
-                       pfn = memblock_next_valid_pfn(pfn) - 1;
+                       pfn = memblock_next_valid_pfn(pfn, &idx) - 1;
 #endif
                        continue;
                }
--
Quoted 2 lines

2.7.4

So the function is only defined with CONFIG_HAVE_ARCH_PFN_VALID but it's called with CONFIG_HAVE_MEMBLOCK_NODE_MAP? The definition should likely depend on both options as the function really depends on both conditions. Otherwise it should be defined nop.

--nX

#18 lore raw

Re: [PATCH v3 5/5] mm: page_alloc: reduce unnecessary binary search in early_pfn_valid()

In reply to Jia He
Quoted 74 lines · Jia He

Commit b92df1de5d28 ("mm: page_alloc: skip over regions of invalid pfns where possible") optimized the loop in memmap_init_zone(). But there is still some room for improvement. E.g. in early_pfn_valid(), if pfn and pfn+1 are in the same memblock region, we can record the last returned memblock region index and check check pfn++ is still in the same region.

Currently it only improve the performance on arm64 and will have no impact on other arches.

---
 arch/x86/include/asm/mmzone_32.h |  2 +-
 include/linux/mmzone.h           | 12 +++++++++---
 mm/page_alloc.c                  |  2 +-
 3 files changed, 11 insertions(+), 5 deletions(-)
arch/x86/include/asm/mmzone_32.h +1 -1
index 73d8dd1..329d3ba 100644
@@ -49,7 +49,7 @@ static inline int pfn_valid(int pfn)
        return 0;
 }
 
-#define early_pfn_valid(pfn)   pfn_valid((pfn))
+#define early_pfn_valid(pfn, last_region_idx)  pfn_valid((pfn))
 
 #endif /* CONFIG_DISCONTIGMEM */
 
include/linux/mmzone.h +9 -3
index d797716..3a686af 100644
@@ -1267,9 +1267,15 @@ static inline int pfn_present(unsigned long pfn)
 })
 #else
 #define pfn_to_nid(pfn)                (0)
-#endif
+#endif /*CONFIG_NUMA*/
+
+#ifdef CONFIG_HAVE_ARCH_PFN_VALID
+#define early_pfn_valid(pfn, last_region_idx) \
+                               pfn_valid_region(pfn, last_region_idx)
+#else
+#define early_pfn_valid(pfn, last_region_idx)  pfn_valid(pfn)
+#endif /*CONFIG_HAVE_ARCH_PFN_VALID*/
 
-#define early_pfn_valid(pfn)   pfn_valid(pfn)
 void sparse_init(void);
 #else
 #define sparse_init()  do {} while (0)
@@ -1288,7 +1294,7 @@ struct mminit_pfnnid_cache {
 };
 
 #ifndef early_pfn_valid
-#define early_pfn_valid(pfn)   (1)
+#define early_pfn_valid(pfn, last_region_idx)  (1)
 #endif
 
 void memory_present(int nid, unsigned long start, unsigned long end);
mm/page_alloc.c +1 -2
index 0bb0274..debccf3 100644
@@ -5484,7 +5484,7 @@ void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
                if (context != MEMMAP_EARLY)
                        goto not_early;
 
-               if (!early_pfn_valid(pfn)) {
+               if (!early_pfn_valid(pfn, &idx)) {
 #if (defined CONFIG_HAVE_MEMBLOCK) && (defined CONFIG_HAVE_ARCH_PFN_VALID)
                        /*
                         * Skip to the pfn preceding the next valid one (or
--
Quoted 2 lines

2.7.4

Hmm, what about making index global variable instead of changing all the prototypes? Similar to early_pfnnid_cache for example. Something like:

#ifdef CONFIG_HAVE_ARCH_PFN_VALID
extern int early_region_idx __meminitdata;
#define early_pfn_valid(pfn) \
pfn_valid_region(pfn, &early_region_idx)
#else
#define early_pfn_valid(pfn) pfn_valid(pfn)
#endif /*CONFIG_HAVE_ARCH_PFN_VALID*/

And move this to arch/arm64/include/asm/page.h ?

--nX

#22 lore raw
In reply to Jia He
Quoted 77 lines · Jia He

On 3/27/2018 9:02 AM, Wei Yang Wrote:

Quoted 66 lines · Jia He
Quoted 45 lines

Commit b92df1de5d28 ("mm: page_alloc: skip over regions of invalid pfns where possible") tried to optimize the loop in memmap_init_zone(). But there is still some room for improvement.

Patch 1 remain the memblock_next_valid_pfn when CONFIG_HAVE_ARCH_PFN_VALID
is enabled
Patch 2 optimizes the memblock_next_valid_pfn()
Patch 3~5 optimizes the early_pfn_valid(), I have to split it into parts
because the changes are located across subsystems.

I tested the pfn loop process in memmap_init(), the same as before. As for the performance improvement, after this set, I can see the time overhead of memmap_init() is reduced from 41313 us to 24345 us in my armv8a server(QDF2400 with 96G memory).

Attached the memblock region information in my server.

[   86.956758] Zone ranges:
[   86.959452]   DMA      [mem 0x0000000000200000-0x00000000ffffffff]
[   86.966041]   Normal   [mem 0x0000000100000000-0x00000017ffffffff]
[   86.972631] Movable zone start for each node
[   86.977179] Early memory node ranges
[   86.980985]   node   0: [mem 0x0000000000200000-0x000000000021ffff]
[   86.987666]   node   0: [mem 0x0000000000820000-0x000000000307ffff]
[   86.994348]   node   0: [mem 0x0000000003080000-0x000000000308ffff]
[   87.001029]   node   0: [mem 0x0000000003090000-0x00000000031fffff]
[   87.007710]   node   0: [mem 0x0000000003200000-0x00000000033fffff]
[   87.014392]   node   0: [mem 0x0000000003410000-0x000000000563ffff]
[   87.021073]   node   0: [mem 0x0000000005640000-0x000000000567ffff]
[   87.027754]   node   0: [mem 0x0000000005680000-0x00000000056dffff]
[   87.034435]   node   0: [mem 0x00000000056e0000-0x00000000086fffff]
[   87.041117]   node   0: [mem 0x0000000008700000-0x000000000871ffff]
[   87.047798]   node   0: [mem 0x0000000008720000-0x000000000894ffff]
[   87.054479]   node   0: [mem 0x0000000008950000-0x0000000008baffff]
[   87.061161]   node   0: [mem 0x0000000008bb0000-0x0000000008bcffff]
[   87.067842]   node   0: [mem 0x0000000008bd0000-0x0000000008c4ffff]
[   87.074524]   node   0: [mem 0x0000000008c50000-0x0000000008e2ffff]
[   87.081205]   node   0: [mem 0x0000000008e30000-0x0000000008e4ffff]
[   87.087886]   node   0: [mem 0x0000000008e50000-0x0000000008fcffff]
[   87.094568]   node   0: [mem 0x0000000008fd0000-0x000000000910ffff]
[   87.101249]   node   0: [mem 0x0000000009110000-0x00000000092effff]
[   87.107930]   node   0: [mem 0x00000000092f0000-0x000000000930ffff]
[   87.114612]   node   0: [mem 0x0000000009310000-0x000000000963ffff]
[   87.121293]   node   0: [mem 0x0000000009640000-0x000000000e61ffff]
[   87.127975]   node   0: [mem 0x000000000e620000-0x000000000e64ffff]
[   87.134657]   node   0: [mem 0x000000000e650000-0x000000000fffffff]
Quoted 8 lines
[   87.141338]   node   0: [mem 0x0000000010800000-0x0000000017feffff]
[   87.148019]   node   0: [mem 0x000000001c000000-0x000000001c00ffff]
[   87.154701]   node   0: [mem 0x000000001c010000-0x000000001c7fffff]
[   87.161383]   node   0: [mem 0x000000001c810000-0x000000007efbffff]
[   87.168064]   node   0: [mem 0x000000007efc0000-0x000000007efdffff]
[   87.174746]   node   0: [mem 0x000000007efe0000-0x000000007efeffff]
[   87.181427]   node   0: [mem 0x000000007eff0000-0x000000007effffff]
[   87.188108]   node   0: [mem 0x000000007f000000-0x00000017ffffffff]

Hi, Jia

I haven't taken a deep look into your code, just one curious question on your memory layout.

The log above is printed out in free_area_init_nodes(), which iterates on memblock.memory and prints them. If I am not wrong, memory regions added to memblock.memory are ordered and merged if possible.

While from your log, I see many regions could be merged but are isolated. For example, the last two region:

Quoted 8 lines
   node   0: [mem 0x000000007eff0000-0x000000007effffff]
   node   0: [mem 0x000000007f000000-0x00000017ffffffff]

So I am curious why they are isolated instead of combined to one.

Quoted 1 line

From the code, the possible reason is the region's flag differs from each

other. If you have time, would you mind taking a look into this?

Quoted 7 lines

Hi Wei
I thought these 2 have different flags
[    0.000000] idx=30,region [7eff0000:10000]flag=4     <--- aka MEMBLOCK_NOMAP
[    0.000000]   node   0: [mem 0x000000007eff0000-0x000000007effffff] [    0.000000] idx=31,region [7f000000:81000000]flag=0 <--- aka MEMBLOCK_NONE [    0.000000]   node   0: [mem 0x000000007f000000-0x00000017ffffffff]

Thanks.

Hmm, I am not that familiar with those flags, while they look like to indicate the physical capability of this range.

MEMBLOCK_NONE no special
MEMBLOCK_HOTPLUG hotplug-able
MEMBLOCK_MIRROR high reliable
MEMBLOCK_NOMAP no direct map

While these flags are not there when they are first added into the memory region. When you look at the memblock_add_range(), the last parameter passed is always 0. This means current several separated ranges reflect the physical memory capability layout.

Then, why this layout is so scattered? As you can see several ranges are less than 1M.

If, just my assumption, we could merge some of them, we could have a better performance. Less ranges, less searching time.

Quoted 4 lines
Signature · 3 lines
-- 
Cheers,
Jia
Signature · 2 lines
Wei Yang
Help you, Help me
author
#23 lore raw

On 3/28/2018 8:30 AM, Wei Yang Wrote:

Quoted 99 lines · Jia He
Quoted 76 lines

On 3/27/2018 9:02 AM, Wei Yang Wrote:

Quoted 66 lines · Jia He
Quoted 45 lines

Commit b92df1de5d28 ("mm: page_alloc: skip over regions of invalid pfns where possible") tried to optimize the loop in memmap_init_zone(). But there is still some room for improvement.

Patch 1 remain the memblock_next_valid_pfn when CONFIG_HAVE_ARCH_PFN_VALID
is enabled
Patch 2 optimizes the memblock_next_valid_pfn()
Patch 3~5 optimizes the early_pfn_valid(), I have to split it into parts
because the changes are located across subsystems.

I tested the pfn loop process in memmap_init(), the same as before. As for the performance improvement, after this set, I can see the time overhead of memmap_init() is reduced from 41313 us to 24345 us in my armv8a server(QDF2400 with 96G memory).

Attached the memblock region information in my server.

[   86.956758] Zone ranges:
[   86.959452]   DMA      [mem 0x0000000000200000-0x00000000ffffffff]
[   86.966041]   Normal   [mem 0x0000000100000000-0x00000017ffffffff]
[   86.972631] Movable zone start for each node
[   86.977179] Early memory node ranges
[   86.980985]   node   0: [mem 0x0000000000200000-0x000000000021ffff]
[   86.987666]   node   0: [mem 0x0000000000820000-0x000000000307ffff]
[   86.994348]   node   0: [mem 0x0000000003080000-0x000000000308ffff]
[   87.001029]   node   0: [mem 0x0000000003090000-0x00000000031fffff]
[   87.007710]   node   0: [mem 0x0000000003200000-0x00000000033fffff]
[   87.014392]   node   0: [mem 0x0000000003410000-0x000000000563ffff]
[   87.021073]   node   0: [mem 0x0000000005640000-0x000000000567ffff]
[   87.027754]   node   0: [mem 0x0000000005680000-0x00000000056dffff]
[   87.034435]   node   0: [mem 0x00000000056e0000-0x00000000086fffff]
[   87.041117]   node   0: [mem 0x0000000008700000-0x000000000871ffff]
[   87.047798]   node   0: [mem 0x0000000008720000-0x000000000894ffff]
[   87.054479]   node   0: [mem 0x0000000008950000-0x0000000008baffff]
[   87.061161]   node   0: [mem 0x0000000008bb0000-0x0000000008bcffff]
[   87.067842]   node   0: [mem 0x0000000008bd0000-0x0000000008c4ffff]
[   87.074524]   node   0: [mem 0x0000000008c50000-0x0000000008e2ffff]
[   87.081205]   node   0: [mem 0x0000000008e30000-0x0000000008e4ffff]
[   87.087886]   node   0: [mem 0x0000000008e50000-0x0000000008fcffff]
[   87.094568]   node   0: [mem 0x0000000008fd0000-0x000000000910ffff]
[   87.101249]   node   0: [mem 0x0000000009110000-0x00000000092effff]
[   87.107930]   node   0: [mem 0x00000000092f0000-0x000000000930ffff]
[   87.114612]   node   0: [mem 0x0000000009310000-0x000000000963ffff]
[   87.121293]   node   0: [mem 0x0000000009640000-0x000000000e61ffff]
[   87.127975]   node   0: [mem 0x000000000e620000-0x000000000e64ffff]
[   87.134657]   node   0: [mem 0x000000000e650000-0x000000000fffffff]
Quoted 8 lines
[   87.141338]   node   0: [mem 0x0000000010800000-0x0000000017feffff]
[   87.148019]   node   0: [mem 0x000000001c000000-0x000000001c00ffff]
[   87.154701]   node   0: [mem 0x000000001c010000-0x000000001c7fffff]
[   87.161383]   node   0: [mem 0x000000001c810000-0x000000007efbffff]
[   87.168064]   node   0: [mem 0x000000007efc0000-0x000000007efdffff]
[   87.174746]   node   0: [mem 0x000000007efe0000-0x000000007efeffff]
[   87.181427]   node   0: [mem 0x000000007eff0000-0x000000007effffff]
[   87.188108]   node   0: [mem 0x000000007f000000-0x00000017ffffffff]

Hi, Jia

I haven't taken a deep look into your code, just one curious question on your memory layout.

The log above is printed out in free_area_init_nodes(), which iterates on memblock.memory and prints them. If I am not wrong, memory regions added to memblock.memory are ordered and merged if possible.

While from your log, I see many regions could be merged but are isolated. For example, the last two region:

Quoted 8 lines
    node   0: [mem 0x000000007eff0000-0x000000007effffff]
    node   0: [mem 0x000000007f000000-0x00000017ffffffff]

So I am curious why they are isolated instead of combined to one.

Quoted 1 line

From the code, the possible reason is the region's flag differs from each

other. If you have time, would you mind taking a look into this?

Quoted 7 lines

Hi Wei
I thought these 2 have different flags
[    0.000000] idx=30,region [7eff0000:10000]flag=4     <--- aka MEMBLOCK_NOMAP
[    0.000000]   node   0: [mem 0x000000007eff0000-0x000000007effffff] [    0.000000] idx=31,region [7f000000:81000000]flag=0 <--- aka MEMBLOCK_NONE [    0.000000]   node   0: [mem 0x000000007f000000-0x00000017ffffffff]

Thanks.

Hmm, I am not that familiar with those flags, while they look like to indicate the physical capability of this range.

MEMBLOCK_NONE no special
MEMBLOCK_HOTPLUG hotplug-able
MEMBLOCK_MIRROR high reliable
MEMBLOCK_NOMAP no direct map

While these flags are not there when they are first added into the memory region. When you look at the memblock_add_range(), the last parameter passed is always 0. This means current several separated ranges reflect the physical memory capability layout.

Quoted 5 lines

Then, why this layout is so scattered? As you can see several ranges are less than 1M.

If, just my assumption, we could merge some of them, we could have a better performance. Less ranges, less searching time.

Thanks for your suggestions, Wei
Need further digging and will consider to improve it in another patchset.

Signature · 2 lines
Cheers,
Jia
author
#9 lore raw

Re: [PATCH v3 2/5] mm: page_alloc: reduce unnecessary binary search in memblock_next_valid_pfn()

In reply to Daniel Vacek

On 3/28/2018 1:17 AM, Daniel Vacek Wrote:

Quoted 148 lines · Jia He
Quoted 140 lines

Commit b92df1de5d28 ("mm: page_alloc: skip over regions of invalid pfns where possible") optimized the loop in memmap_init_zone(). But there is still some room for improvement. E.g. if pfn and pfn+1 are in the same memblock region, we can simply pfn++ instead of doing the binary search in memblock_next_valid_pfn. This patch only works when CONFIG_HAVE_ARCH_PFN_VALID is enable.

---
  include/linux/memblock.h |  2 +-
  mm/memblock.c            | 73 +++++++++++++++++++++++++++++-------------------
  mm/page_alloc.c          |  3 +-
  3 files changed, 47 insertions(+), 31 deletions(-)
include/linux/memblock.h +1 -1
index efbbe4b..a8fb2ab 100644
@@ -204,7 +204,7 @@ void __next_mem_pfn_range(int *idx, int nid, unsigned long *out_start_pfn,
  #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
 
  #ifdef CONFIG_HAVE_ARCH_PFN_VALID
-unsigned long memblock_next_valid_pfn(unsigned long pfn);
+unsigned long memblock_next_valid_pfn(unsigned long pfn, int *idx);
  #endif
 
  /**
mm/memblock.c +44 -29
index bea5a9c..06c1a08 100644
@@ -1102,35 +1102,6 @@ void __init_memblock __next_mem_pfn_range(int *idx, int nid,
                 *out_nid = r->nid;
  }
 
-#ifdef CONFIG_HAVE_ARCH_PFN_VALID
-unsigned long __init_memblock memblock_next_valid_pfn(unsigned long pfn)
-{
-       struct memblock_type *type = &memblock.memory;
-       unsigned int right = type->cnt;
-       unsigned int mid, left = 0;
-       phys_addr_t addr = PFN_PHYS(++pfn);
-
-       do {
-               mid = (right + left) / 2;
-
-               if (addr < type->regions[mid].base)
-                       right = mid;
-               else if (addr >= (type->regions[mid].base +
-                                 type->regions[mid].size))
-                       left = mid + 1;
-               else {
-                       /* addr is within the region, so pfn is valid */
-                       return pfn;
-               }
-       } while (left < right);
-
-       if (right == type->cnt)
-               return -1UL;
-       else
-               return PHYS_PFN(type->regions[right].base);
-}
-#endif /*CONFIG_HAVE_ARCH_PFN_VALID*/
-
  /**
   * memblock_set_node - set node ID on memblock regions
   * @base: base of area to set node ID for
@@ -1162,6 +1133,50 @@ int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
  }
  #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
 
+#ifdef CONFIG_HAVE_ARCH_PFN_VALID
+unsigned long __init_memblock memblock_next_valid_pfn(unsigned long pfn,
+                                                       int *last_idx)
+{
+       struct memblock_type *type = &memblock.memory;
+       unsigned int right = type->cnt;
+       unsigned int mid, left = 0;
+       unsigned long start_pfn, end_pfn;
+       phys_addr_t addr = PFN_PHYS(++pfn);
+
+       /* fast path, return pfh+1 if next pfn is in the same region */
+       if (*last_idx != -1) {
+               start_pfn = PFN_DOWN(type->regions[*last_idx].base);
+               end_pfn = PFN_DOWN(type->regions[*last_idx].base +
+                               type->regions[*last_idx].size);
+
+               if (pfn < end_pfn && pfn > start_pfn)
+                       return pfn;
+       }
+
+       /* slow path, do the binary searching */
+       do {
+               mid = (right + left) / 2;
+
+               if (addr < type->regions[mid].base)
+                       right = mid;
+               else if (addr >= (type->regions[mid].base +
+                                 type->regions[mid].size))
+                       left = mid + 1;
+               else {
+                       *last_idx = mid;
+                       return pfn;
+               }
+       } while (left < right);
+
+       if (right == type->cnt)
+               return -1UL;
+
+       *last_idx = right;
+
+       return PHYS_PFN(type->regions[*last_idx].base);
+}
+#endif /*CONFIG_HAVE_ARCH_PFN_VALID*/
+
  static phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
                                         phys_addr_t align, phys_addr_t start,
                                         phys_addr_t end, int nid, ulong flags)
mm/page_alloc.c +2 -2
index 2a967f7..0bb0274 100644
@@ -5459,6 +5459,7 @@ void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
         unsigned long end_pfn = start_pfn + size;
         pg_data_t *pgdat = NODE_DATA(nid);
         unsigned long pfn;
+       int idx = -1;
         unsigned long nr_initialised = 0;
         struct page *page;
  #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
@@ -5490,7 +5491,7 @@ void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
                          * end_pfn), such that we hit a valid pfn (or end_pfn)
                          * on our next iteration of the loop.
                          */
-                       pfn = memblock_next_valid_pfn(pfn) - 1;
+                       pfn = memblock_next_valid_pfn(pfn, &idx) - 1;
  #endif
                         continue;
                 }
--
Quoted 2 lines

2.7.4

So the function is only defined with CONFIG_HAVE_ARCH_PFN_VALID but it's called with CONFIG_HAVE_MEMBLOCK_NODE_MAP? The definition should likely depend on both options as the function really depends on both conditions. Otherwise it should be defined nop.

Quoted 2 lines

--nX

Yes, thanks

Signature · 2 lines
Cheers,
Jia
author
#19 lore raw

Re: [PATCH v3 5/5] mm: page_alloc: reduce unnecessary binary search in early_pfn_valid()

In reply to Daniel Vacek

On 3/28/2018 1:51 AM, Daniel Vacek Wrote:

Quoted 89 lines · Jia He
Quoted 74 lines

Commit b92df1de5d28 ("mm: page_alloc: skip over regions of invalid pfns where possible") optimized the loop in memmap_init_zone(). But there is still some room for improvement. E.g. in early_pfn_valid(), if pfn and pfn+1 are in the same memblock region, we can record the last returned memblock region index and check check pfn++ is still in the same region.

Currently it only improve the performance on arm64 and will have no impact on other arches.

---
  arch/x86/include/asm/mmzone_32.h |  2 +-
  include/linux/mmzone.h           | 12 +++++++++---
  mm/page_alloc.c                  |  2 +-
  3 files changed, 11 insertions(+), 5 deletions(-)
arch/x86/include/asm/mmzone_32.h +1 -1
index 73d8dd1..329d3ba 100644
@@ -49,7 +49,7 @@ static inline int pfn_valid(int pfn)
         return 0;
  }
 
-#define early_pfn_valid(pfn)   pfn_valid((pfn))
+#define early_pfn_valid(pfn, last_region_idx)  pfn_valid((pfn))
 
  #endif /* CONFIG_DISCONTIGMEM */
 
include/linux/mmzone.h +9 -3
index d797716..3a686af 100644
@@ -1267,9 +1267,15 @@ static inline int pfn_present(unsigned long pfn)
  })
  #else
  #define pfn_to_nid(pfn)                (0)
-#endif
+#endif /*CONFIG_NUMA*/
+
+#ifdef CONFIG_HAVE_ARCH_PFN_VALID
+#define early_pfn_valid(pfn, last_region_idx) \
+                               pfn_valid_region(pfn, last_region_idx)
+#else
+#define early_pfn_valid(pfn, last_region_idx)  pfn_valid(pfn)
+#endif /*CONFIG_HAVE_ARCH_PFN_VALID*/
 
-#define early_pfn_valid(pfn)   pfn_valid(pfn)
  void sparse_init(void);
  #else
  #define sparse_init()  do {} while (0)
@@ -1288,7 +1294,7 @@ struct mminit_pfnnid_cache {
  };
 
  #ifndef early_pfn_valid
-#define early_pfn_valid(pfn)   (1)
+#define early_pfn_valid(pfn, last_region_idx)  (1)
  #endif
 
  void memory_present(int nid, unsigned long start, unsigned long end);
mm/page_alloc.c +1 -2
index 0bb0274..debccf3 100644
@@ -5484,7 +5484,7 @@ void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
                 if (context != MEMMAP_EARLY)
                         goto not_early;
 
-               if (!early_pfn_valid(pfn)) {
+               if (!early_pfn_valid(pfn, &idx)) {
  #if (defined CONFIG_HAVE_MEMBLOCK) && (defined CONFIG_HAVE_ARCH_PFN_VALID)
                         /*
                          * Skip to the pfn preceding the next valid one (or
--
Quoted 2 lines

2.7.4

Hmm, what about making index global variable instead of changing all the prototypes? Similar to early_pfnnid_cache for example. Something like:

#ifdef CONFIG_HAVE_ARCH_PFN_VALID
extern int early_region_idx __meminitdata;
#define early_pfn_valid(pfn) \
pfn_valid_region(pfn, &early_region_idx)
#else
#define early_pfn_valid(pfn) pfn_valid(pfn)
#endif /*CONFIG_HAVE_ARCH_PFN_VALID*/

Quoted 4 lines

And move this to arch/arm64/include/asm/page.h ?

--nX

Yes. ok with me

Signature · 2 lines
Cheers,
Jia
#24 lore raw
In reply to Jia He
Quoted 102 lines · Jia He

On 3/28/2018 8:30 AM, Wei Yang Wrote:

Quoted 99 lines · Jia He
Quoted 76 lines

On 3/27/2018 9:02 AM, Wei Yang Wrote:

Quoted 66 lines · Jia He
Quoted 45 lines

Commit b92df1de5d28 ("mm: page_alloc: skip over regions of invalid pfns where possible") tried to optimize the loop in memmap_init_zone(). But there is still some room for improvement.

Patch 1 remain the memblock_next_valid_pfn when CONFIG_HAVE_ARCH_PFN_VALID
is enabled
Patch 2 optimizes the memblock_next_valid_pfn()
Patch 3~5 optimizes the early_pfn_valid(), I have to split it into parts
because the changes are located across subsystems.

I tested the pfn loop process in memmap_init(), the same as before. As for the performance improvement, after this set, I can see the time overhead of memmap_init() is reduced from 41313 us to 24345 us in my armv8a server(QDF2400 with 96G memory).

Attached the memblock region information in my server.

[   86.956758] Zone ranges:
[   86.959452]   DMA      [mem 0x0000000000200000-0x00000000ffffffff]
[   86.966041]   Normal   [mem 0x0000000100000000-0x00000017ffffffff]
[   86.972631] Movable zone start for each node
[   86.977179] Early memory node ranges
[   86.980985]   node   0: [mem 0x0000000000200000-0x000000000021ffff]
[   86.987666]   node   0: [mem 0x0000000000820000-0x000000000307ffff]
[   86.994348]   node   0: [mem 0x0000000003080000-0x000000000308ffff]
[   87.001029]   node   0: [mem 0x0000000003090000-0x00000000031fffff]
[   87.007710]   node   0: [mem 0x0000000003200000-0x00000000033fffff]
[   87.014392]   node   0: [mem 0x0000000003410000-0x000000000563ffff]
[   87.021073]   node   0: [mem 0x0000000005640000-0x000000000567ffff]
[   87.027754]   node   0: [mem 0x0000000005680000-0x00000000056dffff]
[   87.034435]   node   0: [mem 0x00000000056e0000-0x00000000086fffff]
[   87.041117]   node   0: [mem 0x0000000008700000-0x000000000871ffff]
[   87.047798]   node   0: [mem 0x0000000008720000-0x000000000894ffff]
[   87.054479]   node   0: [mem 0x0000000008950000-0x0000000008baffff]
[   87.061161]   node   0: [mem 0x0000000008bb0000-0x0000000008bcffff]
[   87.067842]   node   0: [mem 0x0000000008bd0000-0x0000000008c4ffff]
[   87.074524]   node   0: [mem 0x0000000008c50000-0x0000000008e2ffff]
[   87.081205]   node   0: [mem 0x0000000008e30000-0x0000000008e4ffff]
[   87.087886]   node   0: [mem 0x0000000008e50000-0x0000000008fcffff]
[   87.094568]   node   0: [mem 0x0000000008fd0000-0x000000000910ffff]
[   87.101249]   node   0: [mem 0x0000000009110000-0x00000000092effff]
[   87.107930]   node   0: [mem 0x00000000092f0000-0x000000000930ffff]
[   87.114612]   node   0: [mem 0x0000000009310000-0x000000000963ffff]
[   87.121293]   node   0: [mem 0x0000000009640000-0x000000000e61ffff]
[   87.127975]   node   0: [mem 0x000000000e620000-0x000000000e64ffff]
[   87.134657]   node   0: [mem 0x000000000e650000-0x000000000fffffff]
Quoted 8 lines
[   87.141338]   node   0: [mem 0x0000000010800000-0x0000000017feffff]
[   87.148019]   node   0: [mem 0x000000001c000000-0x000000001c00ffff]
[   87.154701]   node   0: [mem 0x000000001c010000-0x000000001c7fffff]
[   87.161383]   node   0: [mem 0x000000001c810000-0x000000007efbffff]
[   87.168064]   node   0: [mem 0x000000007efc0000-0x000000007efdffff]
[   87.174746]   node   0: [mem 0x000000007efe0000-0x000000007efeffff]
[   87.181427]   node   0: [mem 0x000000007eff0000-0x000000007effffff]
[   87.188108]   node   0: [mem 0x000000007f000000-0x00000017ffffffff]

Hi, Jia

I haven't taken a deep look into your code, just one curious question on your memory layout.

The log above is printed out in free_area_init_nodes(), which iterates on memblock.memory and prints them. If I am not wrong, memory regions added to memblock.memory are ordered and merged if possible.

While from your log, I see many regions could be merged but are isolated. For example, the last two region:

Quoted 8 lines
    node   0: [mem 0x000000007eff0000-0x000000007effffff]
    node   0: [mem 0x000000007f000000-0x00000017ffffffff]

So I am curious why they are isolated instead of combined to one.

Quoted 1 line

From the code, the possible reason is the region's flag differs from each

other. If you have time, would you mind taking a look into this?

Quoted 7 lines

Hi Wei
I thought these 2 have different flags
[    0.000000] idx=30,region [7eff0000:10000]flag=4     <--- aka MEMBLOCK_NOMAP
[    0.000000]   node   0: [mem 0x000000007eff0000-0x000000007effffff] [    0.000000] idx=31,region [7f000000:81000000]flag=0 <--- aka MEMBLOCK_NONE [    0.000000]   node   0: [mem 0x000000007f000000-0x00000017ffffffff]

Thanks.

Hmm, I am not that familiar with those flags, while they look like to indicate the physical capability of this range.

MEMBLOCK_NONE no special
MEMBLOCK_HOTPLUG hotplug-able
MEMBLOCK_MIRROR high reliable
MEMBLOCK_NOMAP no direct map

While these flags are not there when they are first added into the memory region. When you look at the memblock_add_range(), the last parameter passed is always 0. This means current several separated ranges reflect the physical memory capability layout.

Quoted 8 lines
Quoted 5 lines

Then, why this layout is so scattered? As you can see several ranges are less than 1M.

If, just my assumption, we could merge some of them, we could have a better performance. Less ranges, less searching time.

Thanks for your suggestions, Wei
Need further digging and will consider to improve it in another patchset.

You are welcome :-)

I am glad to see your further patchset or investigation, if you are willing me to involve.

Quoted 3 lines
Signature · 2 lines
Cheers,
Jia
Signature · 2 lines
Wei Yang
Help you, Help me
#2 lore raw

Re: [PATCH v3 1/5] mm: page_alloc: remain memblock_next_valid_pfn() when CONFIG_HAVE_ARCH_PFN_VALID is enable

In reply to Jia He

Oops, I should reply this thread. Forget about the reply on another thread.

Quoted 4 lines · Jia He

Commit b92df1de5d28 ("mm: page_alloc: skip over regions of invalid pfns where possible") optimized the loop in memmap_init_zone(). But it causes possible panic bug. So Daniel Vacek reverted it later.

Why this has a bug? Do you have some link about it?

If the audience could know the potential risk, it would be helpful to review the code and decide whether to take it back.

Quoted 71 lines

But memblock_next_valid_pfn is valid when CONFIG_HAVE_ARCH_PFN_VALID is enable. And as verified by Eugeniu Rosca, arm can benifit from this commit. So remain the memblock_next_valid_pfn.

---

include/linux/memblock.h |  4 ++++
mm/memblock.c            | 29 +++++++++++++++++++++++++++++
mm/page_alloc.c          | 11 ++++++++++-
3 files changed, 43 insertions(+), 1 deletion(-)
include/linux/memblock.h +4 -0
index 0257aee..efbbe4b 100644
@@ -203,6 +203,10 @@ void __next_mem_pfn_range(int *idx, int nid, unsigned long *out_start_pfn,
 	     i >= 0; __next_mem_pfn_range(&i, nid, p_start, p_end, p_nid))
 #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
 
+#ifdef CONFIG_HAVE_ARCH_PFN_VALID
+unsigned long memblock_next_valid_pfn(unsigned long pfn);
+#endif
+
 /**
 * for_each_free_mem_range - iterate through free memblock areas
 * @i: u64 used as loop variable
mm/memblock.c +29 -0
index ba7c878..bea5a9c 100644
@@ -1102,6 +1102,35 @@ void __init_memblock __next_mem_pfn_range(int *idx, int nid,
 		*out_nid = r->nid;
 }
 
+#ifdef CONFIG_HAVE_ARCH_PFN_VALID
+unsigned long __init_memblock memblock_next_valid_pfn(unsigned long pfn)
+{
+	struct memblock_type *type = &memblock.memory;
+	unsigned int right = type->cnt;
+	unsigned int mid, left = 0;
+	phys_addr_t addr = PFN_PHYS(++pfn);
+
+	do {
+		mid = (right + left) / 2;
+
+		if (addr < type->regions[mid].base)
+			right = mid;
+		else if (addr >= (type->regions[mid].base +
+				  type->regions[mid].size))
+			left = mid + 1;
+		else {
+			/* addr is within the region, so pfn is valid */
+			return pfn;
+		}
+	} while (left < right);
+
+	if (right == type->cnt)
+		return -1UL;
+	else
+		return PHYS_PFN(type->regions[right].base);
+}
+#endif /*CONFIG_HAVE_ARCH_PFN_VALID*/
+
 /**
 * memblock_set_node - set node ID on memblock regions
 * @base: base of area to set node ID for
mm/page_alloc.c +0 -0
index c19f5ac..2a967f7 100644

mm/page_alloc.c void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone, · line 5487

Quoted 6 lines
mm/page_alloc.c +2 -1
@@ -5483,8 +5483,17 @@ void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
 		if (context != MEMMAP_EARLY)
 			goto not_early;
 
-		if (!early_pfn_valid(pfn))
+		if (!early_pfn_valid(pfn)) {
+#if (defined CONFIG_HAVE_MEMBLOCK) && (defined CONFIG_HAVE_ARCH_PFN_VALID)

In commit b92df1de5d28, it use CONFIG_HAVE_MEMBLOCK_NODE_MAP.

Not get the point of your change.

Quoted 14 lines
mm/page_alloc.c +8 -0
+			/*
+			 * Skip to the pfn preceding the next valid one (or
+			 * end_pfn), such that we hit a valid pfn (or end_pfn)
+			 * on our next iteration of the loop.
+			 */
+			pfn = memblock_next_valid_pfn(pfn) - 1;
+#endif
 			continue;
+		}
 		if (!early_pfn_in_nid(pfn, nid))
 			continue;
 		if (!update_defer_init(pgdat, pfn, end_pfn, &nr_initialised))
Signature · 1 line
2.7.4
Signature · 2 lines
Wei Yang
Help you, Help me
#10 lore raw

Re: [PATCH v3 2/5] mm: page_alloc: reduce unnecessary binary search in memblock_next_valid_pfn()

In reply to Jia He
Quoted 72 lines · Jia He

Commit b92df1de5d28 ("mm: page_alloc: skip over regions of invalid pfns where possible") optimized the loop in memmap_init_zone(). But there is still some room for improvement. E.g. if pfn and pfn+1 are in the same memblock region, we can simply pfn++ instead of doing the binary search in memblock_next_valid_pfn. This patch only works when CONFIG_HAVE_ARCH_PFN_VALID is enable.

---

include/linux/memblock.h |  2 +-
mm/memblock.c            | 73 +++++++++++++++++++++++++++++-------------------
mm/page_alloc.c          |  3 +-
3 files changed, 47 insertions(+), 31 deletions(-)
include/linux/memblock.h +1 -1
index efbbe4b..a8fb2ab 100644
@@ -204,7 +204,7 @@ void __next_mem_pfn_range(int *idx, int nid, unsigned long *out_start_pfn,
 #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
 
 #ifdef CONFIG_HAVE_ARCH_PFN_VALID
-unsigned long memblock_next_valid_pfn(unsigned long pfn);
+unsigned long memblock_next_valid_pfn(unsigned long pfn, int *idx);
 #endif
 
 /**
mm/memblock.c +1 -29
index bea5a9c..06c1a08 100644
@@ -1102,35 +1102,6 @@ void __init_memblock __next_mem_pfn_range(int *idx, int nid,
 		*out_nid = r->nid;
 }
 
-#ifdef CONFIG_HAVE_ARCH_PFN_VALID
-unsigned long __init_memblock memblock_next_valid_pfn(unsigned long pfn)
-{
-	struct memblock_type *type = &memblock.memory;
-	unsigned int right = type->cnt;
-	unsigned int mid, left = 0;
-	phys_addr_t addr = PFN_PHYS(++pfn);
-
-	do {
-		mid = (right + left) / 2;
-
-		if (addr < type->regions[mid].base)
-			right = mid;
-		else if (addr >= (type->regions[mid].base +
-				  type->regions[mid].size))
-			left = mid + 1;
-		else {
-			/* addr is within the region, so pfn is valid */
-			return pfn;
-		}
-	} while (left < right);
-
-	if (right == type->cnt)
-		return -1UL;
-	else
-		return PHYS_PFN(type->regions[right].base);
-}
-#endif /*CONFIG_HAVE_ARCH_PFN_VALID*/
-
 /**
 * memblock_set_node - set node ID on memblock regions
 * @base: base of area to set node ID for
@@ -1162,6 +1133,50 @@ int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
 }
 #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
 
+#ifdef CONFIG_HAVE_ARCH_PFN_VALID

mm/memblock.c int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size, · line 1146

Quoted 10 lines
mm/memblock.c +10 -0
+unsigned long __init_memblock memblock_next_valid_pfn(unsigned long pfn,
+							int *last_idx)
+{
+	struct memblock_type *type = &memblock.memory;
+	unsigned int right = type->cnt;
+	unsigned int mid, left = 0;
+	unsigned long start_pfn, end_pfn;
+	phys_addr_t addr = PFN_PHYS(++pfn);
+
+	/* fast path, return pfh+1 if next pfn is in the same region */
                             ^^^  pfn

mm/memblock.c int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size, · line 1148

Quoted 2 lines
mm/memblock.c +2 -0
+	if (*last_idx != -1) {
+		start_pfn = PFN_DOWN(type->regions[*last_idx].base);

To me, it should be PFN_UP().

mm/memblock.c int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size, · line 1152

Quoted 4 lines
mm/memblock.c +4 -0
+		end_pfn = PFN_DOWN(type->regions[*last_idx].base +
+				type->regions[*last_idx].size);
+
+		if (pfn < end_pfn && pfn > start_pfn)

Could be (pfn < end_pfn && pfn >= start_pfn)?

pfn == start_pfn is also a valid address.

Quoted 16 lines
mm/memblock.c +16 -0
+			return pfn;
+	}
+
+	/* slow path, do the binary searching */
+	do {
+		mid = (right + left) / 2;
+
+		if (addr < type->regions[mid].base)
+			right = mid;
+		else if (addr >= (type->regions[mid].base +
+				  type->regions[mid].size))
+			left = mid + 1;
+		else {
+			*last_idx = mid;
+			return pfn;
+		}

mm/memblock.c int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size, · line 1178

Quoted 10 lines
mm/memblock.c +10 -0
+	} while (left < right);
+
+	if (right == type->cnt)
+		return -1UL;
+
+	*last_idx = right;
+
+	return PHYS_PFN(type->regions[*last_idx].base);
+}
+#endif /*CONFIG_HAVE_ARCH_PFN_VALID*/

The same comment as Daniel, you are moving the function out of CONFIG_HAVE_MEMBLOCK_NODE_MAP.

Quoted 27 lines
mm/memblock.c +1 -0
+
 static phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
 					phys_addr_t align, phys_addr_t start,
 					phys_addr_t end, int nid, ulong flags)
mm/page_alloc.c +2 -1
index 2a967f7..0bb0274 100644
@@ -5459,6 +5459,7 @@ void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
 	unsigned long end_pfn = start_pfn + size;
 	pg_data_t *pgdat = NODE_DATA(nid);
 	unsigned long pfn;
+	int idx = -1;
 	unsigned long nr_initialised = 0;
 	struct page *page;
 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
@@ -5490,7 +5491,7 @@ void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
 			 * end_pfn), such that we hit a valid pfn (or end_pfn)
 			 * on our next iteration of the loop.
 			 */
-			pfn = memblock_next_valid_pfn(pfn) - 1;
+			pfn = memblock_next_valid_pfn(pfn, &idx) - 1;
 #endif
 			continue;
 		}
Signature · 1 line
2.7.4
Signature · 2 lines
Wei Yang
Help you, Help me
#16 lore raw

Re: [PATCH v3 4/5] arm64: introduce pfn_valid_region()

In reply to Jia He
Quoted 30 lines · Jia He

This is the preparation for further optimizing in early_pfn_valid on arm64.

---
arch/arm64/include/asm/page.h | 3 ++-
arch/arm64/mm/init.c | 25 ++++++++++++++++++++++++-

2 files changed, 26 insertions(+), 2 deletions(-)
arch/arm64/include/asm/page.h +2 -1
index 60d02c8..da2cba3 100644
@@ -38,7 +38,8 @@ extern void clear_page(void *to);
 typedef struct page *pgtable_t;
 
 #ifdef CONFIG_HAVE_ARCH_PFN_VALID
-extern int pfn_valid(unsigned long);
+extern int pfn_valid(unsigned long pfn);
+extern int pfn_valid_region(unsigned long pfn, int *last_idx);
 #endif
 
 #include <asm/memory.h>
arch/arm64/mm/init.c +0 -0
index 00e7b90..06433d5 100644
@@ -290,7 +290,30 @@ int pfn_valid(unsigned long pfn)
 	return memblock_is_map_memory(pfn << PAGE_SHIFT);
 }

arch/arm64/mm/init.c int pfn_valid(unsigned long pfn) · line 300

Quoted 10 lines
arch/arm64/mm/init.c +8 -1
 EXPORT_SYMBOL(pfn_valid);
-#endif
+
+int pfn_valid_region(unsigned long pfn, int *last_idx)
+{
+	unsigned long start_pfn, end_pfn;
+	struct memblock_type *type = &memblock.memory;
+
+	if (*last_idx != -1) {
+		start_pfn = PFN_DOWN(type->regions[*last_idx].base);

PFN_UP() should be used.

arch/arm64/mm/init.c int pfn_valid(unsigned long pfn) · line 306

Quoted 6 lines
arch/arm64/mm/init.c +6 -0
+		end_pfn= PFN_DOWN(type->regions[*last_idx].base +
+					type->regions[*last_idx].size);
+
+		if (pfn >= start_pfn && pfn < end_pfn)
+			return !memblock_is_nomap(
+				&memblock.memory.regions[*last_idx]);

Could use type->regions directly.

arch/arm64/mm/init.c int pfn_valid(unsigned long pfn) · line 313

Quoted 7 lines
arch/arm64/mm/init.c +7 -0
+	}
+
+	*last_idx = memblock_search_pfn_regions(pfn);
+	if (*last_idx == -1)
+		return false;
+
+	return !memblock_is_nomap(&memblock.memory.regions[*last_idx]);

Could use type->regions directly.

Well, since your check memblock.memory.regions, how about use a variable equals memblock.memory.regions directly instead of type->regions?

For example:

struct memblock_region *regions = memblock.memory.regions;

Quoted 8 lines
arch/arm64/mm/init.c +3 -0
+}
+EXPORT_SYMBOL(pfn_valid_region);
+#endif /*CONFIG_HAVE_ARCH_PFN_VALID*/
 
 #ifndef CONFIG_SPARSEMEM
 static void __init arm64_memory_present(void)
Signature · 1 line
2.7.4
Signature · 2 lines
Wei Yang
Help you, Help me
author
#3 lore raw

Re: [PATCH v3 1/5] mm: page_alloc: remain memblock_next_valid_pfn() when CONFIG_HAVE_ARCH_PFN_VALID is enable

In reply to Wei Yang

On 3/28/2018 5:18 PM, Wei Yang Wrote:

Quoted 11 lines

Oops, I should reply this thread. Forget about the reply on another thread.

Quoted 4 lines · Jia He

Commit b92df1de5d28 ("mm: page_alloc: skip over regions of invalid pfns where possible") optimized the loop in memmap_init_zone(). But it causes possible panic bug. So Daniel Vacek reverted it later.

Why this has a bug? Do you have some link about it?

If the audience could know the potential risk, it would be helpful to review the code and decide whether to take it back.

Hi Wei
Paul firstly submit a commit b92df1de5 to improve the loop in memmap_init_zone. And Daniel tried to fix a bug_on panic issue on X86 in commit 864b75f9d6b because there is evidence that this bug_on was caused by b92df1de5 [1].

But things didn't get better, 864b75f9d6b caused booting hang issue on arm{64} [2] So maintainer decided to reverted both b92df1de5 and 864b75f9d6b

[1] https://patchwork.kernel.org/patch/10251145/
[2] https://lkml.org/lkml/2018/3/14/469

Quoted 75 lines
Quoted 75 lines

But memblock_next_valid_pfn is valid when CONFIG_HAVE_ARCH_PFN_VALID is enable. And as verified by Eugeniu Rosca, arm can benifit from this commit. So remain the memblock_next_valid_pfn.

---

include/linux/memblock.h |  4 ++++
mm/memblock.c            | 29 +++++++++++++++++++++++++++++
mm/page_alloc.c          | 11 ++++++++++-
3 files changed, 43 insertions(+), 1 deletion(-)
include/linux/memblock.h +4 -0
index 0257aee..efbbe4b 100644
@@ -203,6 +203,10 @@ void __next_mem_pfn_range(int *idx, int nid, unsigned long *out_start_pfn,
 	     i >= 0; __next_mem_pfn_range(&i, nid, p_start, p_end, p_nid))
 #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
 
+#ifdef CONFIG_HAVE_ARCH_PFN_VALID
+unsigned long memblock_next_valid_pfn(unsigned long pfn);
+#endif
+
 /**
  * for_each_free_mem_range - iterate through free memblock areas
  * @i: u64 used as loop variable
mm/memblock.c +29 -0
index ba7c878..bea5a9c 100644
@@ -1102,6 +1102,35 @@ void __init_memblock __next_mem_pfn_range(int *idx, int nid,
 		*out_nid = r->nid;
 }
 
+#ifdef CONFIG_HAVE_ARCH_PFN_VALID
+unsigned long __init_memblock memblock_next_valid_pfn(unsigned long pfn)
+{
+	struct memblock_type *type = &memblock.memory;
+	unsigned int right = type->cnt;
+	unsigned int mid, left = 0;
+	phys_addr_t addr = PFN_PHYS(++pfn);
+
+	do {
+		mid = (right + left) / 2;
+
+		if (addr < type->regions[mid].base)
+			right = mid;
+		else if (addr >= (type->regions[mid].base +
+				  type->regions[mid].size))
+			left = mid + 1;
+		else {
+			/* addr is within the region, so pfn is valid */
+			return pfn;
+		}
+	} while (left < right);
+
+	if (right == type->cnt)
+		return -1UL;
+	else
+		return PHYS_PFN(type->regions[right].base);
+}
+#endif /*CONFIG_HAVE_ARCH_PFN_VALID*/
+
 /**
  * memblock_set_node - set node ID on memblock regions
  * @base: base of area to set node ID for
mm/page_alloc.c +0 -0
index c19f5ac..2a967f7 100644
@@ -5483,8 +5483,17 @@ void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
 		if (context != MEMMAP_EARLY)
 			goto not_early;
 
Quoted 6 lines

mm/page_alloc.c void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone, · line 5487

Quoted 3 lines
mm/page_alloc.c +2 -1
-		if (!early_pfn_valid(pfn))
+		if (!early_pfn_valid(pfn)) {
+#if (defined CONFIG_HAVE_MEMBLOCK) && (defined CONFIG_HAVE_ARCH_PFN_VALID)

In commit b92df1de5d28, it use CONFIG_HAVE_MEMBLOCK_NODE_MAP.

Not get the point of your change.

Please get more information about the reason why using CONFIG_HAVE_MEMBLOCK in d49d47e mm: page_alloc: skip over regions of invalid pfns on UMA

And this commit is dependent on b92df1de, so it is also reverted.

Cheers,
Jia

Quoted 15 lines
Quoted 15 lines
mm/page_alloc.c +8 -0
+			/*
+			 * Skip to the pfn preceding the next valid one (or
+			 * end_pfn), such that we hit a valid pfn (or end_pfn)
+			 * on our next iteration of the loop.
+			 */
+			pfn = memblock_next_valid_pfn(pfn) - 1;
+#endif
 			continue;
+		}
 		if (!early_pfn_in_nid(pfn, nid))
 			continue;
 		if (!update_defer_init(pgdat, pfn, end_pfn, &nr_initialised))
Signature · 1 line
2.7.4
Signature · 2 lines
Cheers,
Jia
author
#11 lore raw

Re: [PATCH v3 2/5] mm: page_alloc: reduce unnecessary binary search in memblock_next_valid_pfn()

In reply to Wei Yang

On 3/28/2018 5:26 PM, Wei Yang Wrote:

Quoted 72 lines · Jia He
Quoted 71 lines

Commit b92df1de5d28 ("mm: page_alloc: skip over regions of invalid pfns where possible") optimized the loop in memmap_init_zone(). But there is still some room for improvement. E.g. if pfn and pfn+1 are in the same memblock region, we can simply pfn++ instead of doing the binary search in memblock_next_valid_pfn. This patch only works when CONFIG_HAVE_ARCH_PFN_VALID is enable.

---

include/linux/memblock.h |  2 +-
mm/memblock.c            | 73 +++++++++++++++++++++++++++++-------------------
mm/page_alloc.c          |  3 +-
3 files changed, 47 insertions(+), 31 deletions(-)
include/linux/memblock.h +1 -1
index efbbe4b..a8fb2ab 100644
@@ -204,7 +204,7 @@ void __next_mem_pfn_range(int *idx, int nid, unsigned long *out_start_pfn,
 #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
 
 #ifdef CONFIG_HAVE_ARCH_PFN_VALID
-unsigned long memblock_next_valid_pfn(unsigned long pfn);
+unsigned long memblock_next_valid_pfn(unsigned long pfn, int *idx);
 #endif
 
 /**
mm/memblock.c +0 -29
index bea5a9c..06c1a08 100644
@@ -1102,35 +1102,6 @@ void __init_memblock __next_mem_pfn_range(int *idx, int nid,
 		*out_nid = r->nid;
 }
 
-#ifdef CONFIG_HAVE_ARCH_PFN_VALID
-unsigned long __init_memblock memblock_next_valid_pfn(unsigned long pfn)
-{
-	struct memblock_type *type = &memblock.memory;
-	unsigned int right = type->cnt;
-	unsigned int mid, left = 0;
-	phys_addr_t addr = PFN_PHYS(++pfn);
-
-	do {
-		mid = (right + left) / 2;
-
-		if (addr < type->regions[mid].base)
-			right = mid;
-		else if (addr >= (type->regions[mid].base +
-				  type->regions[mid].size))
-			left = mid + 1;
-		else {
-			/* addr is within the region, so pfn is valid */
-			return pfn;
-		}
-	} while (left < right);
-
-	if (right == type->cnt)
-		return -1UL;
-	else
-		return PHYS_PFN(type->regions[right].base);
-}
-#endif /*CONFIG_HAVE_ARCH_PFN_VALID*/
-
 /**
  * memblock_set_node - set node ID on memblock regions
  * @base: base of area to set node ID for
@@ -1162,6 +1133,50 @@ int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
 }
 #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
 
Quoted 12 lines

mm/memblock.c int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size, · line 1146

Quoted 11 lines
mm/memblock.c +11 -0
+#ifdef CONFIG_HAVE_ARCH_PFN_VALID
+unsigned long __init_memblock memblock_next_valid_pfn(unsigned long pfn,
+							int *last_idx)
+{
+	struct memblock_type *type = &memblock.memory;
+	unsigned int right = type->cnt;
+	unsigned int mid, left = 0;
+	unsigned long start_pfn, end_pfn;
+	phys_addr_t addr = PFN_PHYS(++pfn);
+
+	/* fast path, return pfh+1 if next pfn is in the same region */
                              ^^^  pfn

Thanks

Quoted 3 lines

mm/memblock.c int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size, · line 1148

Quoted 2 lines
mm/memblock.c +2 -0
+	if (*last_idx != -1) {
+		start_pfn = PFN_DOWN(type->regions[*last_idx].base);

To me, it should be PFN_UP().

hmm.., seems all the base of memory region is pfn aligned (0x10000 aligned). So

PFN_UP is the same as PFN_DOWN here?
I got this logic from memblock_search_pfn_nid()

Cheers,
Jia

Quoted 8 lines

mm/memblock.c int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size, · line 1152

Quoted 5 lines
mm/memblock.c +4 -0
+		end_pfn = PFN_DOWN(type->regions[*last_idx].base +
+				type->regions[*last_idx].size);
+
+		if (pfn < end_pfn && pfn > start_pfn)

Could be (pfn < end_pfn && pfn >= start_pfn)?

pfn == start_pfn is also a valid address.

No, pfn=pfn+1 at the beginning, so pfn != start_pfn

Quoted 56 lines
Quoted 17 lines
mm/memblock.c +16 -0
+			return pfn;
+	}
+
+	/* slow path, do the binary searching */
+	do {
+		mid = (right + left) / 2;
+
+		if (addr < type->regions[mid].base)
+			right = mid;
+		else if (addr >= (type->regions[mid].base +
+				  type->regions[mid].size))
+			left = mid + 1;
+		else {
+			*last_idx = mid;
+			return pfn;
+		}

mm/memblock.c int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size, · line 1178

Quoted 10 lines
mm/memblock.c +10 -0
+	} while (left < right);
+
+	if (right == type->cnt)
+		return -1UL;
+
+	*last_idx = right;
+
+	return PHYS_PFN(type->regions[*last_idx].base);
+}
+#endif /*CONFIG_HAVE_ARCH_PFN_VALID*/

The same comment as Daniel, you are moving the function out of CONFIG_HAVE_MEMBLOCK_NODE_MAP.

Quoted 27 lines
mm/memblock.c +1 -0
+
 static phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
 					phys_addr_t align, phys_addr_t start,
 					phys_addr_t end, int nid, ulong flags)
mm/page_alloc.c +2 -1
index 2a967f7..0bb0274 100644
@@ -5459,6 +5459,7 @@ void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
 	unsigned long end_pfn = start_pfn + size;
 	pg_data_t *pgdat = NODE_DATA(nid);
 	unsigned long pfn;
+	int idx = -1;
 	unsigned long nr_initialised = 0;
 	struct page *page;
 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
@@ -5490,7 +5491,7 @@ void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
 			 * end_pfn), such that we hit a valid pfn (or end_pfn)
 			 * on our next iteration of the loop.
 			 */
-			pfn = memblock_next_valid_pfn(pfn) - 1;
+			pfn = memblock_next_valid_pfn(pfn, &idx) - 1;
 #endif
 			continue;
 		}
Signature · 1 line
2.7.4
#12 lore raw

Re: [PATCH v3 2/5] mm: page_alloc: reduce unnecessary binary search in memblock_next_valid_pfn()

Quoted 88 lines · Jia He

On 3/28/2018 5:26 PM, Wei Yang Wrote:

Quoted 72 lines · Jia He
Quoted 71 lines

Commit b92df1de5d28 ("mm: page_alloc: skip over regions of invalid pfns where possible") optimized the loop in memmap_init_zone(). But there is still some room for improvement. E.g. if pfn and pfn+1 are in the same memblock region, we can simply pfn++ instead of doing the binary search in memblock_next_valid_pfn. This patch only works when CONFIG_HAVE_ARCH_PFN_VALID is enable.

---

include/linux/memblock.h |  2 +-
mm/memblock.c            | 73 +++++++++++++++++++++++++++++-------------------
mm/page_alloc.c          |  3 +-
3 files changed, 47 insertions(+), 31 deletions(-)
include/linux/memblock.h +1 -1
index efbbe4b..a8fb2ab 100644
@@ -204,7 +204,7 @@ void __next_mem_pfn_range(int *idx, int nid, unsigned long *out_start_pfn,
 #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
 
 #ifdef CONFIG_HAVE_ARCH_PFN_VALID
-unsigned long memblock_next_valid_pfn(unsigned long pfn);
+unsigned long memblock_next_valid_pfn(unsigned long pfn, int *idx);
 #endif
 
 /**
mm/memblock.c +0 -29
index bea5a9c..06c1a08 100644
@@ -1102,35 +1102,6 @@ void __init_memblock __next_mem_pfn_range(int *idx, int nid,
 		*out_nid = r->nid;
 }
 
-#ifdef CONFIG_HAVE_ARCH_PFN_VALID
-unsigned long __init_memblock memblock_next_valid_pfn(unsigned long pfn)
-{
-	struct memblock_type *type = &memblock.memory;
-	unsigned int right = type->cnt;
-	unsigned int mid, left = 0;
-	phys_addr_t addr = PFN_PHYS(++pfn);
-
-	do {
-		mid = (right + left) / 2;
-
-		if (addr < type->regions[mid].base)
-			right = mid;
-		else if (addr >= (type->regions[mid].base +
-				  type->regions[mid].size))
-			left = mid + 1;
-		else {
-			/* addr is within the region, so pfn is valid */
-			return pfn;
-		}
-	} while (left < right);
-
-	if (right == type->cnt)
-		return -1UL;
-	else
-		return PHYS_PFN(type->regions[right].base);
-}
-#endif /*CONFIG_HAVE_ARCH_PFN_VALID*/
-
 /**
  * memblock_set_node - set node ID on memblock regions
  * @base: base of area to set node ID for
@@ -1162,6 +1133,50 @@ int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
 }
 #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
 
Quoted 12 lines

mm/memblock.c int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size, · line 1146

Quoted 11 lines
mm/memblock.c +11 -0
+#ifdef CONFIG_HAVE_ARCH_PFN_VALID
+unsigned long __init_memblock memblock_next_valid_pfn(unsigned long pfn,
+							int *last_idx)
+{
+	struct memblock_type *type = &memblock.memory;
+	unsigned int right = type->cnt;
+	unsigned int mid, left = 0;
+	unsigned long start_pfn, end_pfn;
+	phys_addr_t addr = PFN_PHYS(++pfn);
+
+	/* fast path, return pfh+1 if next pfn is in the same region */
                              ^^^  pfn

Thanks

Quoted 8 lines
Quoted 3 lines

mm/memblock.c int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size, · line 1148

Quoted 2 lines
mm/memblock.c +2 -0
+	if (*last_idx != -1) {
+		start_pfn = PFN_DOWN(type->regions[*last_idx].base);

To me, it should be PFN_UP().

hmm.., seems all the base of memory region is pfn aligned (0x10000 aligned). So

PFN_UP is the same as PFN_DOWN here?
I got this logic from memblock_search_pfn_nid()

Ok, I guess here hide some buggy code.

When you look at __next_mem_pfn_range(), it uses PFN_UP() for base. The reason is try to clip some un-page-aligned memory. While PFN_DOWN() will introduce some unavailable memory to system.

Even mostly those address are page-aligned, we need to be careful for this.

Let me drop a patch to fix the original one.

Quoted 5 lines

Cheers,
Jia

Quoted 1 line
Quoted 8 lines
Quoted 7 lines

mm/memblock.c int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size, · line 1152

Quoted 4 lines
mm/memblock.c +4 -0
+		end_pfn = PFN_DOWN(type->regions[*last_idx].base +
+				type->regions[*last_idx].size);
+
+		if (pfn < end_pfn && pfn > start_pfn)

Could be (pfn < end_pfn && pfn >= start_pfn)?

pfn == start_pfn is also a valid address.

No, pfn=pfn+1 at the beginning, so pfn != start_pfn

This is a little bit tricky.

There is no requirement to pass a valid pfn to memblock_next_valid_pfn(). So suppose we have memory layout like this:

    [0x100, 0x1ff]
    [0x300, 0x3ff]

And I call memblock_next_valid_pfn(0x2ff, 1), would this fits the fast path logic?

Well, since memblock_next_valid_pfn() only used memmap_init_zone(), the situation as I mentioned seems will not happen.

Even though, I suggest to chagne this, otherwise your logic in slow path and fast path differs. In the case above, your slow path returns 0x300 at last.

Quoted 56 lines
Quoted 56 lines
Quoted 17 lines
mm/memblock.c +16 -0
+			return pfn;
+	}
+
+	/* slow path, do the binary searching */
+	do {
+		mid = (right + left) / 2;
+
+		if (addr < type->regions[mid].base)
+			right = mid;
+		else if (addr >= (type->regions[mid].base +
+				  type->regions[mid].size))
+			left = mid + 1;
+		else {
+			*last_idx = mid;
+			return pfn;
+		}

mm/memblock.c int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size, · line 1178

Quoted 10 lines
mm/memblock.c +10 -0
+	} while (left < right);
+
+	if (right == type->cnt)
+		return -1UL;
+
+	*last_idx = right;
+
+	return PHYS_PFN(type->regions[*last_idx].base);
+}
+#endif /*CONFIG_HAVE_ARCH_PFN_VALID*/

The same comment as Daniel, you are moving the function out of CONFIG_HAVE_MEMBLOCK_NODE_MAP.

Quoted 27 lines
mm/memblock.c +1 -0
+
 static phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
 					phys_addr_t align, phys_addr_t start,
 					phys_addr_t end, int nid, ulong flags)
mm/page_alloc.c +2 -1
index 2a967f7..0bb0274 100644
@@ -5459,6 +5459,7 @@ void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
 	unsigned long end_pfn = start_pfn + size;
 	pg_data_t *pgdat = NODE_DATA(nid);
 	unsigned long pfn;
+	int idx = -1;
 	unsigned long nr_initialised = 0;
 	struct page *page;
 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
@@ -5490,7 +5491,7 @@ void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
 			 * end_pfn), such that we hit a valid pfn (or end_pfn)
 			 * on our next iteration of the loop.
 			 */
-			pfn = memblock_next_valid_pfn(pfn) - 1;
+			pfn = memblock_next_valid_pfn(pfn, &idx) - 1;
 #endif
 			continue;
 		}
Signature · 1 line
2.7.4
Signature · 2 lines
Wei Yang
Help you, Help me
author
#13 lore raw

Re: [PATCH v3 2/5] mm: page_alloc: reduce unnecessary binary search in memblock_next_valid_pfn()

On 3/30/2018 9:43 AM, Wei Yang Wrote:

Quoted 98 lines · Jia He
Quoted 87 lines

On 3/28/2018 5:26 PM, Wei Yang Wrote:

Quoted 72 lines · Jia He
Quoted 71 lines

Commit b92df1de5d28 ("mm: page_alloc: skip over regions of invalid pfns where possible") optimized the loop in memmap_init_zone(). But there is still some room for improvement. E.g. if pfn and pfn+1 are in the same memblock region, we can simply pfn++ instead of doing the binary search in memblock_next_valid_pfn. This patch only works when CONFIG_HAVE_ARCH_PFN_VALID is enable.

---

include/linux/memblock.h |  2 +-
mm/memblock.c            | 73 +++++++++++++++++++++++++++++-------------------
mm/page_alloc.c          |  3 +-
3 files changed, 47 insertions(+), 31 deletions(-)
include/linux/memblock.h +1 -1
index efbbe4b..a8fb2ab 100644
@@ -204,7 +204,7 @@ void __next_mem_pfn_range(int *idx, int nid, unsigned long *out_start_pfn,
 #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
 
 #ifdef CONFIG_HAVE_ARCH_PFN_VALID
-unsigned long memblock_next_valid_pfn(unsigned long pfn);
+unsigned long memblock_next_valid_pfn(unsigned long pfn, int *idx);
 #endif
 
 /**
mm/memblock.c +0 -29
index bea5a9c..06c1a08 100644
@@ -1102,35 +1102,6 @@ void __init_memblock __next_mem_pfn_range(int *idx, int nid,
 		*out_nid = r->nid;
 }
 
-#ifdef CONFIG_HAVE_ARCH_PFN_VALID
-unsigned long __init_memblock memblock_next_valid_pfn(unsigned long pfn)
-{
-	struct memblock_type *type = &memblock.memory;
-	unsigned int right = type->cnt;
-	unsigned int mid, left = 0;
-	phys_addr_t addr = PFN_PHYS(++pfn);
-
-	do {
-		mid = (right + left) / 2;
-
-		if (addr < type->regions[mid].base)
-			right = mid;
-		else if (addr >= (type->regions[mid].base +
-				  type->regions[mid].size))
-			left = mid + 1;
-		else {
-			/* addr is within the region, so pfn is valid */
-			return pfn;
-		}
-	} while (left < right);
-
-	if (right == type->cnt)
-		return -1UL;
-	else
-		return PHYS_PFN(type->regions[right].base);
-}
-#endif /*CONFIG_HAVE_ARCH_PFN_VALID*/
-
 /**
   * memblock_set_node - set node ID on memblock regions
   * @base: base of area to set node ID for
@@ -1162,6 +1133,50 @@ int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
 }
 #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
 
Quoted 12 lines

mm/memblock.c int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size, · line 1146

Quoted 11 lines
mm/memblock.c +11 -0
+#ifdef CONFIG_HAVE_ARCH_PFN_VALID
+unsigned long __init_memblock memblock_next_valid_pfn(unsigned long pfn,
+							int *last_idx)
+{
+	struct memblock_type *type = &memblock.memory;
+	unsigned int right = type->cnt;
+	unsigned int mid, left = 0;
+	unsigned long start_pfn, end_pfn;
+	phys_addr_t addr = PFN_PHYS(++pfn);
+
+	/* fast path, return pfh+1 if next pfn is in the same region */
                               ^^^  pfn

Thanks

Quoted 8 lines
Quoted 3 lines

mm/memblock.c int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size, · line 1148

Quoted 2 lines
mm/memblock.c +2 -0
+	if (*last_idx != -1) {
+		start_pfn = PFN_DOWN(type->regions[*last_idx].base);

To me, it should be PFN_UP().

hmm.., seems all the base of memory region is pfn aligned (0x10000 aligned). So

PFN_UP is the same as PFN_DOWN here?
I got this logic from memblock_search_pfn_nid()

Ok, I guess here hide some buggy code.

Quoted 7 lines

When you look at __next_mem_pfn_range(), it uses PFN_UP() for base. The reason is try to clip some un-page-aligned memory. While PFN_DOWN() will introduce some unavailable memory to system.

Even mostly those address are page-aligned, we need to be careful for this.

Let me drop a patch to fix the original one.

Ok, please cc me, I will change the related code when your patch is accepted. ;-)

Quoted 19 lines
Quoted 11 lines

Cheers,
Jia

Quoted 7 lines

mm/memblock.c int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size, · line 1152

Quoted 4 lines
mm/memblock.c +4 -0
+		end_pfn = PFN_DOWN(type->regions[*last_idx].base +
+				type->regions[*last_idx].size);
+
+		if (pfn < end_pfn && pfn > start_pfn)

Could be (pfn < end_pfn && pfn >= start_pfn)?

pfn == start_pfn is also a valid address.

No, pfn=pfn+1 at the beginning, so pfn != start_pfn

This is a little bit tricky.

There is no requirement to pass a valid pfn to memblock_next_valid_pfn(). So suppose we have memory layout like this:

     [0x100, 0x1ff]
     [0x300, 0x3ff]
Quoted 8 lines

And I call memblock_next_valid_pfn(0x2ff, 1), would this fits the fast path logic?

Well, since memblock_next_valid_pfn() only used memmap_init_zone(), the situation as I mentioned seems will not happen.

Even though, I suggest to chagne this, otherwise your logic in slow path and fast path differs. In the case above, your slow path returns 0x300 at last.

ok. looks like it does no harm, even I thought the code will guarantee it to skip from 0x1ff to 0x300 directly.
I will change it after fuctional test.

--
Cheers,
Jia

Quoted 55 lines
Quoted 55 lines
Quoted 55 lines
Quoted 16 lines
mm/memblock.c +16 -0
+			return pfn;
+	}
+
+	/* slow path, do the binary searching */
+	do {
+		mid = (right + left) / 2;
+
+		if (addr < type->regions[mid].base)
+			right = mid;
+		else if (addr >= (type->regions[mid].base +
+				  type->regions[mid].size))
+			left = mid + 1;
+		else {
+			*last_idx = mid;
+			return pfn;
+		}

mm/memblock.c int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size, · line 1178

Quoted 10 lines
mm/memblock.c +10 -0
+	} while (left < right);
+
+	if (right == type->cnt)
+		return -1UL;
+
+	*last_idx = right;
+
+	return PHYS_PFN(type->regions[*last_idx].base);
+}
+#endif /*CONFIG_HAVE_ARCH_PFN_VALID*/

The same comment as Daniel, you are moving the function out of CONFIG_HAVE_MEMBLOCK_NODE_MAP.

Quoted 27 lines
mm/memblock.c +1 -0
+
 static phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
 					phys_addr_t align, phys_addr_t start,
 					phys_addr_t end, int nid, ulong flags)
mm/page_alloc.c +2 -1
index 2a967f7..0bb0274 100644
@@ -5459,6 +5459,7 @@ void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
 	unsigned long end_pfn = start_pfn + size;
 	pg_data_t *pgdat = NODE_DATA(nid);
 	unsigned long pfn;
+	int idx = -1;
 	unsigned long nr_initialised = 0;
 	struct page *page;
 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
@@ -5490,7 +5491,7 @@ void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
 			 * end_pfn), such that we hit a valid pfn (or end_pfn)
 			 * on our next iteration of the loop.
 			 */
-			pfn = memblock_next_valid_pfn(pfn) - 1;
+			pfn = memblock_next_valid_pfn(pfn, &idx) - 1;
 #endif
 			continue;
 		}
Signature · 1 line
2.7.4
#4 lore raw

Re: [PATCH v3 1/5] mm: page_alloc: remain memblock_next_valid_pfn() when CONFIG_HAVE_ARCH_PFN_VALID is enable

In reply to Jia He
Quoted 21 lines · Jia He

On 3/28/2018 5:18 PM, Wei Yang Wrote:

Quoted 11 lines

Oops, I should reply this thread. Forget about the reply on another thread.

Quoted 4 lines · Jia He

Commit b92df1de5d28 ("mm: page_alloc: skip over regions of invalid pfns where possible") optimized the loop in memmap_init_zone(). But it causes possible panic bug. So Daniel Vacek reverted it later.

Why this has a bug? Do you have some link about it?

If the audience could know the potential risk, it would be helpful to review the code and decide whether to take it back.

Hi Wei
Paul firstly submit a commit b92df1de5 to improve the loop in memmap_init_zone.
And Daniel tried to fix a bug_on panic issue on X86 in commit 864b75f9d6b because
there is evidence that this bug_on was caused by b92df1de5 [1].

Quoted 6 lines

But things didn't get better, 864b75f9d6b caused booting hang issue on arm{64} [2]
So maintainer decided to reverted both b92df1de5 and 864b75f9d6b

[1] https://patchwork.kernel.org/patch/10251145/
[2] https://lkml.org/lkml/2018/3/14/469

I took some time to look into the discussion, while the root cause seems not clear now?

Signature · 2 lines
Wei Yang
Help you, Help me
author
#5 lore raw

Re: [PATCH v3 1/5] mm: page_alloc: remain memblock_next_valid_pfn() when CONFIG_HAVE_ARCH_PFN_VALID is enable

On 4/2/2018 4:12 PM, Wei Yang Wrote:

Quoted 21 lines · Jia He
Quoted 20 lines

On 3/28/2018 5:18 PM, Wei Yang Wrote:

Quoted 11 lines

Oops, I should reply this thread. Forget about the reply on another thread.

Quoted 4 lines · Jia He

Commit b92df1de5d28 ("mm: page_alloc: skip over regions of invalid pfns where possible") optimized the loop in memmap_init_zone(). But it causes possible panic bug. So Daniel Vacek reverted it later.

Why this has a bug? Do you have some link about it?

If the audience could know the potential risk, it would be helpful to review the code and decide whether to take it back.

Hi Wei
Paul firstly submit a commit b92df1de5 to improve the loop in memmap_init_zone.
And Daniel tried to fix a bug_on panic issue on X86 in commit 864b75f9d6b because
there is evidence that this bug_on was caused by b92df1de5 [1].

Quoted 9 lines
Quoted 6 lines

But things didn't get better, 864b75f9d6b caused booting hang issue on arm{64} [2]
So maintainer decided to reverted both b92df1de5 and 864b75f9d6b

[1] https://patchwork.kernel.org/patch/10251145/
[2] https://lkml.org/lkml/2018/3/14/469

I took some time to look into the discussion, while the root cause seems not clear now?

Frankly speaking, to me the root cause of that bug_on is not completedly clear :-) Daniel ever gave me some hints as followed, but currently I have no x86 platform to understand the details.

"On arm and arm64, memblock is used by default. But generic version of pfn_valid() is based on mem sections and memblock_next_valid_pfn() does not always return the next valid one but skips more resulting in some valid frames to be skipped (as if they were invalid). And that's why kernel was eventually crashing on some !arm machines."

Signature · 2 lines
Cheers,
Jia
#6 lore raw

Re: [PATCH v3 1/5] mm: page_alloc: remain memblock_next_valid_pfn() when CONFIG_HAVE_ARCH_PFN_VALID is enable

Quoted 37 lines · Jia He

On 4/2/2018 4:12 PM, Wei Yang Wrote:

Quoted 21 lines · Jia He
Quoted 20 lines

On 3/28/2018 5:18 PM, Wei Yang Wrote:

Quoted 11 lines

Oops, I should reply this thread. Forget about the reply on another thread.

Quoted 4 lines · Jia He

Commit b92df1de5d28 ("mm: page_alloc: skip over regions of invalid pfns where possible") optimized the loop in memmap_init_zone(). But it causes possible panic bug. So Daniel Vacek reverted it later.

Why this has a bug? Do you have some link about it?

If the audience could know the potential risk, it would be helpful to review the code and decide whether to take it back.

Hi Wei
Paul firstly submit a commit b92df1de5 to improve the loop in memmap_init_zone.
And Daniel tried to fix a bug_on panic issue on X86 in commit 864b75f9d6b because
there is evidence that this bug_on was caused by b92df1de5 [1].

Quoted 9 lines
Quoted 6 lines

But things didn't get better, 864b75f9d6b caused booting hang issue on arm{64} [2]
So maintainer decided to reverted both b92df1de5 and 864b75f9d6b

[1] https://patchwork.kernel.org/patch/10251145/
[2] https://lkml.org/lkml/2018/3/14/469

I took some time to look into the discussion, while the root cause seems not clear now?

Frankly speaking, to me the root cause of that bug_on is not completedly clear :-) Daniel ever gave me some hints as followed, but currently I have no x86 platform to understand the details.

Quoted 6 lines

"On arm and arm64, memblock is used by default. But generic version of pfn_valid() is based on mem sections and memblock_next_valid_pfn() does not always return the next valid one but skips more resulting in some valid frames to be skipped (as if they were invalid). And that's why kernel was eventually crashing on some !arm machines."

This means a system with memblock is safe to use this function?

As I know, mem_section is based on memblock, so in which case memblock_next_valid_pfn() skips a valid pfn? A little confused.

Quoted 3 lines
Signature · 2 lines
Cheers,
Jia
Signature · 2 lines
Wei Yang
Help you, Help me