Skip to contentSkip to conversation

mm/memblock: introduce a new helper memblock_estimated_nr_free_pages()

PATCH Applied Wei Yang 4 participants 9 messages Aug 8, 2024 – Aug 11, 2024

Subsystems (7)
lore
Share & tools
lore

Wei Yang

author
#0 lore raw

PATCH v6 1/3 mm/memblock: introduce a new helper memblock_estimated_nr_free_pages()

During bootup, system may need the number of free pages in the whole system to do some calculation before all pages are freed to buddy system. Usually this number is get from totalram_pages(). Since we plan to move the free pages accounting in __free_pages_core(), this value may not represent total free pages at the early stage, especially when CONFIG_DEFERRED_STRUCT_PAGE_INIT is enabled.

Instead of using raw memblock api, let's introduce a new helper for user to get the estimated number of free pages from memblock point of view.

---
v6: fix memblock test
v5: cleanup the stale name
v4: adjust comment per david's suggestion

---
 include/linux/memblock.h  |  1 +
 mm/memblock.c             | 17 +++++++++++++++++
 tools/include/linux/pfn.h |  1 +
 3 files changed, 19 insertions(+)
include/linux/memblock.h +1 -0
index fc4d75c6cec3..673d5cae7c81 100644
@@ -467,6 +467,7 @@ static inline __init_memblock bool memblock_bottom_up(void)
 
 phys_addr_t memblock_phys_mem_size(void);
 phys_addr_t memblock_reserved_size(void);
+unsigned long memblock_estimated_nr_free_pages(void);
 phys_addr_t memblock_start_of_DRAM(void);
 phys_addr_t memblock_end_of_DRAM(void);
 void memblock_enforce_memory_limit(phys_addr_t memory_limit);
mm/memblock.c +17 -0
index 3b9dc2d89b8a..213057603b65 100644
@@ -1731,6 +1731,23 @@ phys_addr_t __init_memblock memblock_reserved_size(void)
 	return memblock.reserved.total_size;
 }
 
+/**
+ * memblock_estimated_nr_free_pages - return estimated number of free pages
+ * from memblock point of view
+ *
+ * During bootup, subsystems might need a rough estimate of the number of free
+ * pages in the whole system, before precise numbers are available from the
+ * buddy. Especially with CONFIG_DEFERRED_STRUCT_PAGE_INIT, the numbers
+ * obtained from the buddy might be very imprecise during bootup.
+ *
+ * Return:
+ * An estimated number of free pages from memblock point of view.
+ */
+unsigned long __init memblock_estimated_nr_free_pages(void)
+{
+	return PHYS_PFN(memblock_phys_mem_size() - memblock_reserved_size());
+}
+
 /* lowest address */
 phys_addr_t __init_memblock memblock_start_of_DRAM(void)
 {
tools/include/linux/pfn.h +1 -0
index 7512a58189eb..f77a30d70152 100644
@@ -7,4 +7,5 @@
 #define PFN_UP(x)	(((x) + PAGE_SIZE - 1) >> PAGE_SHIFT)
 #define PFN_DOWN(x)	((x) >> PAGE_SHIFT)
 #define PFN_PHYS(x)	((phys_addr_t)(x) << PAGE_SHIFT)
+#define PHYS_PFN(x)	((unsigned long)((x) >> PAGE_SHIFT))
 #endif
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v6 2/3 kernel/fork.c: get estimated free pages by memblock api

Instead of getting estimated free pages from memblock directly, we have introduced an API, memblock_estimated_nr_free_pages(), which is more friendly for users.

Just replace it with new API, no functional change.

CC Mike Rapoport (IBM) <[email protected]>
CC David Hildenbrand <[email protected]>
CC Oleg Nesterov <[email protected]>
---
 kernel/fork.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/fork.c +1 -1
index cc760491f201..d99f148d818b 100644
@@ -997,7 +997,7 @@ void __init __weak arch_task_cache_init(void) { }
 static void __init set_max_threads(unsigned int max_threads_suggested)
 {
 	u64 threads;
-	unsigned long nr_pages = PHYS_PFN(memblock_phys_mem_size() - memblock_reserved_size());
+	unsigned long nr_pages = memblock_estimated_nr_free_pages();
 
 	/*
 	 * The number of threads shall be limited such that the thread
Signature · 1 line
2.34.1
Acked-by on v6 2/3
#2 lore raw

Re: [PATCH v6 2/3] kernel/fork.c: get estimated free pages by memblock api

Quoted 11 lines · Wei Yang

Instead of getting estimated free pages from memblock directly, we have introduced an API, memblock_estimated_nr_free_pages(), which is more friendly for users.

Just replace it with new API, no functional change.

CC Mike Rapoport (IBM) <[email protected]>
CC David Hildenbrand <[email protected]>
CC Oleg Nesterov <[email protected]>

---

Signature · 3 lines
Cheers,

David / dhildenb
author
#3 lore raw

PATCH v6 3/3 s390/mm: get estimated free pages by memblock api

In reply to Wei Yang

Instead of getting estimated free pages from memblock directly, we have introduced an API, memblock_estimated_nr_free_pages(), which is more friendly for users.

Just replace it with new API, no functional change.

CC Mike Rapoport (IBM) <[email protected]>
CC David Hildenbrand <[email protected]>
---
 arch/s390/mm/init.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/s390/mm/init.c +1 -1
index e3d258f9e726..651344206294 100644
@@ -62,7 +62,7 @@ EXPORT_SYMBOL(zero_page_mask);
 
 static void __init setup_zero_pages(void)
 {
-	unsigned long total_pages = PHYS_PFN(memblock_phys_mem_size() - memblock_reserved_size());
+	unsigned long total_pages = memblock_estimated_nr_free_pages();
 	unsigned int order;
 	struct page *page;
 	int i;
Signature · 1 line
2.34.1
Acked-by on v6 3/3
#4 lore raw

Re: [PATCH v6 3/3] s390/mm: get estimated free pages by memblock api

Quoted 10 lines · Wei Yang

Instead of getting estimated free pages from memblock directly, we have introduced an API, memblock_estimated_nr_free_pages(), which is more friendly for users.

Just replace it with new API, no functional change.

CC Mike Rapoport (IBM) <[email protected]>
CC David Hildenbrand <[email protected]>

---

Signature · 3 lines
Cheers,

David / dhildenb
#5 lore raw
In reply to Wei Yang

On Thu, Aug 08, 2024 at 12:14:13AM +0000, Wei Yang wrote:

Hi Wei,

...

mm/memblock.c phys_addr_t __init_memblock memblock_reserved_size(void) · line 1749

Quoted 7 lines

+ * Return:
+ * An estimated number of free pages from memblock point of view.
+ */

mm/memblock.c +4 -0
+unsigned long __init memblock_estimated_nr_free_pages(void)
+{
+	return PHYS_PFN(memblock_phys_mem_size() - memblock_reserved_size());
+}

This could possibly be short on up to two pages due to lack of alignment. The current uses are okay, but since you make it generic it probably matters.

Also, the returned value is not an estimation. Meaning the function name is rather unfortunate AFAICT.

Quoted 1 line

+#define PHYS_PFN(x) ((unsigned long)((x) >> PAGE_SHIFT))

Thanks!

author
#6 lore raw
Quoted 5 lines · Alexander Gordeev

On Thu, Aug 08, 2024 at 12:14:13AM +0000, Wei Yang wrote:

Hi Wei,

...

Quoted 11 lines

mm/memblock.c phys_addr_t __init_memblock memblock_reserved_size(void) · line 1749

Quoted 7 lines

+ * Return:
+ * An estimated number of free pages from memblock point of view.
+ */

mm/memblock.c +4 -0
+unsigned long __init memblock_estimated_nr_free_pages(void)
+{
+	return PHYS_PFN(memblock_phys_mem_size() - memblock_reserved_size());
+}

This could possibly be short on up to two pages due to lack of alignment. The current uses are okay, but since you make it generic it probably matters.

I don't follow, would you mind giving more detail?

Quoted 2 lines

Also, the returned value is not an estimation. Meaning the function name is rather unfortunate AFAICT.

From my point of view, this is an estimation for two reasons:

* value from memblock_xxx is not page size aligned
* reserved memory maybe released during boot stage

It is not that easy to get the exact number of free pages here. Do I miss something?

Quoted 4 lines
Quoted 2 lines

+#define PHYS_PFN(x) ((unsigned long)((x) >> PAGE_SHIFT))

Thanks!

Signature · 2 lines
Wei Yang
Help you, Help me
#7 lore raw
Quoted 11 lines · Wei Yang
Quoted 10 lines

mm/memblock.c phys_addr_t __init_memblock memblock_reserved_size(void) · line 1749

Quoted 6 lines

+ * An estimated number of free pages from memblock point of view.
+ */

mm/memblock.c +4 -0
+unsigned long __init memblock_estimated_nr_free_pages(void)
+{
+	return PHYS_PFN(memblock_phys_mem_size() - memblock_reserved_size());
+}

This could possibly be short on up to two pages due to lack of alignment. The current uses are okay, but since you make it generic it probably matters.

Quoted 1 line

I don't follow, would you mind giving more detail?

memblock_estimated_nr_free_pages() returns number of pages, not bytes. Yet, both memblock_phys_mem_size() and memblock_reserved_size() return a value which is not aligned on PAGE_SIZE. Therefore, the result of PHYS_PFN() applied to the difference between the two functions might be short on one (two?) page(s).

Quoted 10 lines
Quoted 2 lines

Also, the returned value is not an estimation. Meaning the function name is rather unfortunate AFAICT.

From my point of view, this is an estimation for two reasons:

* value from memblock_xxx is not page size aligned
* reserved memory maybe released during boot stage

It is not that easy to get the exact number of free pages here. Do I miss something?

No, with this reasoning it makes sense to me.

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

Thank you for the clarification!

maintainer Applied
#8 lore raw
In reply to Wei Yang

From: Mike Rapoport (Microsoft) <[email protected]>

Quoted 8 lines · Wei Yang

During bootup, system may need the number of free pages in the whole system to do some calculation before all pages are freed to buddy system. Usually this number is get from totalram_pages(). Since we plan to move the free pages accounting in __free_pages_core(), this value may not represent total free pages at the early stage, especially when CONFIG_DEFERRED_STRUCT_PAGE_INIT is enabled.

[...]

Applied to for-next branch of memblock.git tree, thanks!

[1/3] mm/memblock: introduce a new helper memblock_estimated_nr_free_pages()

      commit: d0f8a8973f265f6a276f99d091af99edfb2b87de
[2/3] kernel/fork.c: get estimated free pages by memblock api
      commit: 0910bf0ef85c5404aac94394cb31e076e4eb03f1
[3/3] s390/mm: get estimated free pages by memblock api
      commit: cb088e38aab4c7e9ce711c18c66e851c8f4227bb

tree: https://git.kernel.org/pub/scm/linux/kernel/git/rppt/memblock
branch: for-next

--
Sincerely yours,
Mike.