Skip to contentSkip to conversation

mm: align up min_free_kbytes to multipy of 4

PATCH In review ChenGang 3 participants 3 messages Jun 9, 2019 – Jun 10, 2019

Subsystems (2)
lore
Share & tools
lore

ChenGang

author
#0 lore raw

PATCH 1/1 mm: align up min_free_kbytes to multipy of 4

Usually the value of min_free_kbytes is multiply of 4,
and in this case ,the right shift is ok.
But if it's not, the right-shifting operation will lose the low 2 bits, and this cause kernel don't reserve enough memory.
So it's necessary to align the value of min_free_kbytes to multiply of 4. For example, if min_free_kbytes is 64, then should keep 16 pages, but if min_free_kbytes is 65 or 66, then should keep 17 pages.

---
 mm/page_alloc.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/page_alloc.c +2 -1
index d66bc8a..1baeeba 100644
@@ -7611,7 +7611,8 @@ static void setup_per_zone_lowmem_reserve(void)
 
 static void __setup_per_zone_wmarks(void)
 {
-	unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
+	unsigned long pages_min =
+		(PAGE_ALIGN(min_free_kbytes * 1024) / 1024) >> (PAGE_SHIFT - 10);
 	unsigned long lowmem_pages = 0;
 	struct zone *zone;
 	unsigned long flags;
Signature · 1 line
1.8.5.6
#1 lore raw
Quoted 3 lines · ChenGang

Usually the value of min_free_kbytes is multiply of 4,
and in this case ,the right shift is ok.
But if it's not, the right-shifting operation will lose the low 2 bits,

But PAGE_SHIFT is not always 12.

Quoted 15 lines

and this cause kernel don't reserve enough memory.
So it's necessary to align the value of min_free_kbytes to multiply of 4. For example, if min_free_kbytes is 64, then should keep 16 pages, but if min_free_kbytes is 65 or 66, then should keep 17 pages.

---
mm/page_alloc.c | 3 ++-

1 file changed, 2 insertions(+), 1 deletion(-)
mm/page_alloc.c +0 -0
index d66bc8a..1baeeba 100644

mm/page_alloc.c static void setup_per_zone_lowmem_reserve(void) · line 7615

Quoted 6 lines
mm/page_alloc.c +2 -1
@@ -7611,7 +7611,8 @@ static void setup_per_zone_lowmem_reserve(void)
 
 static void __setup_per_zone_wmarks(void)
 {
-	unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
+	unsigned long pages_min =
+		(PAGE_ALIGN(min_free_kbytes * 1024) / 1024) >> (PAGE_SHIFT - 10);

In my mind, pages_min is an estimated value. Do we need to be so precise?

Quoted 5 lines
mm/page_alloc.c +0 -0
 	unsigned long lowmem_pages = 0;
 	struct zone *zone;
 	unsigned long flags;
Signature · 1 line
1.8.5.6
Signature · 2 lines
Wei Yang
Help you, Help me
#2 lore raw
In reply to ChenGang
Quoted 7 lines · ChenGang

Usually the value of min_free_kbytes is multiply of 4,
and in this case ,the right shift is ok.
But if it's not, the right-shifting operation will lose the low 2 bits, and this cause kernel don't reserve enough memory.
So it's necessary to align the value of min_free_kbytes to multiply of 4. For example, if min_free_kbytes is 64, then should keep 16 pages, but if min_free_kbytes is 65 or 66, then should keep 17 pages.

Could you describe the actual problem? Do we ever generate min_free_kbytes that would lead to unexpected reserves or is this trying to compensate for those values being configured from the userspace? If later why do we care at all?

Have you seen this to be an actual problem or is this mostly motivated by the code reading?

Quoted 22 lines
---
 mm/page_alloc.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/page_alloc.c +2 -1
index d66bc8a..1baeeba 100644
@@ -7611,7 +7611,8 @@ static void setup_per_zone_lowmem_reserve(void)
 
 static void __setup_per_zone_wmarks(void)
 {
-	unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
+	unsigned long pages_min =
+		(PAGE_ALIGN(min_free_kbytes * 1024) / 1024) >> (PAGE_SHIFT - 10);
 	unsigned long lowmem_pages = 0;
 	struct zone *zone;
 	unsigned long flags;
Signature · 1 line
1.8.5.6
Signature · 2 lines
Michal Hocko
SUSE Labs