Skip to contentSkip to conversation

mm/show_mem: Optimize si_meminfo_node by reducing redundant code

PATCH Applied Ye Liu 3 participants 5 messages Mar 25, 2025 – Mar 31, 2025

Subsystems (2)
lore
Share & tools
lore

Ye Liu

author
#0 lore raw

PATCH 1/1 mm/show_mem: Optimize si_meminfo_node by reducing redundant code

From: Ye Liu <[email protected]>

Refactors the si_meminfo_node() function by reducing redundant code and improving readability.

Moved the calculation of managed_pages inside the existing loop that processes pgdat->node_zones, eliminating the need for a separate loop.

Simplified the logic by removing unnecessary preprocessor conditionals.

Ensured that both totalram, totalhigh, and other memory statistics are consistently set without duplication.

This change results in cleaner and more efficient code without altering functionality.

---
 mm/show_mem.c | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)
mm/show_mem.c +5 -11
index 6af13bcd2ab3..ad373b4b6e39 100644
@@ -94,26 +94,20 @@ void si_meminfo_node(struct sysinfo *val, int nid)
 	unsigned long free_highpages = 0;
 	pg_data_t *pgdat = NODE_DATA(nid);
 
-	for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++)
-		managed_pages += zone_managed_pages(&pgdat->node_zones[zone_type]);
-	val->totalram = managed_pages;
-	val->sharedram = node_page_state(pgdat, NR_SHMEM);
-	val->freeram = sum_zone_node_page_state(nid, NR_FREE_PAGES);
-#ifdef CONFIG_HIGHMEM
 	for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
 		struct zone *zone = &pgdat->node_zones[zone_type];
-
+		managed_pages += zone_managed_pages(zone);
 		if (is_highmem(zone)) {
 			managed_highpages += zone_managed_pages(zone);
 			free_highpages += zone_page_state(zone, NR_FREE_PAGES);
 		}
 	}
+
+	val->totalram = managed_pages;
+	val->sharedram = node_page_state(pgdat, NR_SHMEM);
+	val->freeram = sum_zone_node_page_state(nid, NR_FREE_PAGES);
 	val->totalhigh = managed_highpages;
 	val->freehigh = free_highpages;
-#else
-	val->totalhigh = managed_highpages;
-	val->freehigh = free_highpages;
-#endif
 	val->mem_unit = PAGE_SIZE;
 }
 #endif
Signature · 1 line
2.25.1
#1 lore raw
Quoted 29 lines · Ye Liu

From: Ye Liu <[email protected]>

Refactors the si_meminfo_node() function by reducing redundant code and improving readability.

Moved the calculation of managed_pages inside the existing loop that processes pgdat->node_zones, eliminating the need for a separate loop.

Simplified the logic by removing unnecessary preprocessor conditionals.

Ensured that both totalram, totalhigh, and other memory statistics are consistently set without duplication.

This change results in cleaner and more efficient code without altering functionality.

---
 mm/show_mem.c | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)
mm/show_mem.c +0 -0
index 6af13bcd2ab3..ad373b4b6e39 100644
@@ -94,26 +94,20 @@ void si_meminfo_node(struct sysinfo *val, int nid)
 	unsigned long free_highpages = 0;
 	pg_data_t *pgdat = NODE_DATA(nid);
 

mm/show_mem.c void si_meminfo_node(struct sysinfo *val, int nid) · line 99

Quoted 10 lines
mm/show_mem.c +1 -7
-	for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++)
-		managed_pages += zone_managed_pages(&pgdat->node_zones[zone_type]);
-	val->totalram = managed_pages;
-	val->sharedram = node_page_state(pgdat, NR_SHMEM);
-	val->freeram = sum_zone_node_page_state(nid, NR_FREE_PAGES);
-#ifdef CONFIG_HIGHMEM
 	for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
 		struct zone *zone = &pgdat->node_zones[zone_type];
-
+		managed_pages += zone_managed_pages(zone);

nit: don't remove the empty line after the declaration

mm/show_mem.c void si_meminfo_node(struct sysinfo *val, int nid) · line 101

Quoted 2 lines
mm/show_mem.c +0 -0
 		if (is_highmem(zone)) {
 			managed_highpages += zone_managed_pages(zone);

Looks like highmem pages get counted twice, no?

Quoted 20 lines
mm/show_mem.c +4 -4
 			free_highpages += zone_page_state(zone, NR_FREE_PAGES);
 		}
 	}
+
+	val->totalram = managed_pages;
+	val->sharedram = node_page_state(pgdat, NR_SHMEM);
+	val->freeram = sum_zone_node_page_state(nid, NR_FREE_PAGES);
 	val->totalhigh = managed_highpages;
 	val->freehigh = free_highpages;
-#else
-	val->totalhigh = managed_highpages;
-	val->freehigh = free_highpages;
-#endif
 	val->mem_unit = PAGE_SIZE;
 }
 #endif
Signature · 1 line
2.25.1
Signature · 2 lines
Sincerely yours,
Mike.
author
#2 lore raw
Quoted 30 lines · Mike Rapoport
Quoted 29 lines · Ye Liu

From: Ye Liu <[email protected]>

Refactors the si_meminfo_node() function by reducing redundant code and improving readability.

Moved the calculation of managed_pages inside the existing loop that processes pgdat->node_zones, eliminating the need for a separate loop.

Simplified the logic by removing unnecessary preprocessor conditionals.

Ensured that both totalram, totalhigh, and other memory statistics are consistently set without duplication.

This change results in cleaner and more efficient code without altering functionality.

---
 mm/show_mem.c | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)
mm/show_mem.c +0 -0
index 6af13bcd2ab3..ad373b4b6e39 100644
@@ -94,26 +94,20 @@ void si_meminfo_node(struct sysinfo *val, int nid)
 	unsigned long free_highpages = 0;
 	pg_data_t *pgdat = NODE_DATA(nid);
 
Quoted 11 lines

mm/show_mem.c void si_meminfo_node(struct sysinfo *val, int nid) · line 99

Quoted 10 lines
mm/show_mem.c +1 -7
-	for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++)
-		managed_pages += zone_managed_pages(&pgdat->node_zones[zone_type]);
-	val->totalram = managed_pages;
-	val->sharedram = node_page_state(pgdat, NR_SHMEM);
-	val->freeram = sum_zone_node_page_state(nid, NR_FREE_PAGES);
-#ifdef CONFIG_HIGHMEM
 	for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
 		struct zone *zone = &pgdat->node_zones[zone_type];
-
+		managed_pages += zone_managed_pages(zone);

nit: don't remove the empty line after the declaration

Agree.

Quoted 4 lines

mm/show_mem.c void si_meminfo_node(struct sysinfo *val, int nid) · line 101

Quoted 3 lines
mm/show_mem.c +0 -0
 		if (is_highmem(zone)) {
 			managed_highpages += zone_managed_pages(zone);

Looks like highmem pages get counted twice, no?

twice? The original logic has not been changed.

managed_highpages is just high memory, managed_pages is all.

I don't understand what you mean.

Thanks,

Ye Liu

Quoted 21 lines
Quoted 21 lines
mm/show_mem.c +4 -4
 			free_highpages += zone_page_state(zone, NR_FREE_PAGES);
 		}
 	}
+
+	val->totalram = managed_pages;
+	val->sharedram = node_page_state(pgdat, NR_SHMEM);
+	val->freeram = sum_zone_node_page_state(nid, NR_FREE_PAGES);
 	val->totalhigh = managed_highpages;
 	val->freehigh = free_highpages;
-#else
-	val->totalhigh = managed_highpages;
-	val->freehigh = free_highpages;
-#endif
 	val->mem_unit = PAGE_SIZE;
 }
 #endif
Signature · 1 line
2.25.1
Acked-by
#3 lore raw

Please don't send html emails to the Linux kernel mailing lists.

Quoted 49 lines · Ye Liu

在 2025/3/27 03:29, Mike Rapoport 写道:

On Tue, Mar 25, 2025 at 03:38:03PM +0800, Ye Liu wrote:

From: Ye Liu <[email protected]>

Refactors the si_meminfo_node() function by reducing redundant code and improving readability.

Moved the calculation of managed_pages inside the existing loop that processes pgdat->node_zones, eliminating the need for a separate loop.

Simplified the logic by removing unnecessary preprocessor conditionals.

Ensured that both totalram, totalhigh, and other memory statistics are consistently set without duplication.

This change results in cleaner and more efficient code without altering functionality.

---
mm/show_mem.c | 16 +++++-----------
1 file changed, 5 insertions(+), 11 deletions(-)

diff --git a/mm/show_mem.c b/mm/show_mem.c
index 6af13bcd2ab3..ad373b4b6e39 100644
--- a/mm/show_mem.c
+++ b/mm/show_mem.c
@@ -94,26 +94,20 @@ void si_meminfo_node(struct sysinfo *val, int nid)
unsigned long free_highpages = 0;
pg_data_t *pgdat = NODE_DATA(nid);

- for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++)
- managed_pages += zone_managed_pages(&pgdat->node_zones[zone_type]);
- val->totalram = managed_pages;
- val->sharedram = node_page_state(pgdat, NR_SHMEM);
- val->freeram = sum_zone_node_page_state(nid, NR_FREE_PAGES);
-#ifdef CONFIG_HIGHMEM
for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
struct zone *zone = &pgdat->node_zones[zone_type];
-
+ managed_pages += zone_managed_pages(zone);

nit: don't remove the empty line after the declaration

Agree.

Quoted 8 lines
                        if (is_highmem(zone)) {
                                managed_highpages += zone_managed_pages(zone);

    Looks like highmem pages get counted twice, no?

twice? The original logic has not been changed.

managed_highpages is just high memory, managed_pages is all.

You are right, I misread the patch.

Feel free to add

Quoted 27 lines

Thanks,

Ye Liu

                                free_highpages += zone_page_state(zone, NR_FREE_PAGES);
                        }
                }
        +
        +       val->totalram = managed_pages;
        +       val->sharedram = node_page_state(pgdat, NR_SHMEM);
        +       val->freeram = sum_zone_node_page_state(nid, NR_FREE_PAGES);
                val->totalhigh = managed_highpages;
                val->freehigh = free_highpages;
        -#else
        -       val->totalhigh = managed_highpages;
        -       val->freehigh = free_highpages;
        -#endif
                val->mem_unit = PAGE_SIZE;
         }
         #endif
        --
        2.25.1
Signature · 2 lines
Sincerely yours,
Mike.
Reviewed-by on v1 1/1
#4 lore raw
In reply to Ye Liu
Quoted 10 lines · Ye Liu

From: Ye Liu <[email protected]>

Refactors the si_meminfo_node() function by reducing redundant code and improving readability.

Moved the calculation of managed_pages inside the existing loop that processes pgdat->node_zones, eliminating the need for a separate loop.

Simplified the logic by removing unnecessary preprocessor conditionals.

Quoted 8 lines

Ensured that both totalram, totalhigh, and other memory statistics are consistently set without duplication.

This change results in cleaner and more efficient code without altering functionality.

---

Looks good to me,

Quoted 43 lines
 mm/show_mem.c | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)
mm/show_mem.c +5 -11
index 6af13bcd2ab3..ad373b4b6e39 100644
@@ -94,26 +94,20 @@ void si_meminfo_node(struct sysinfo *val, int nid)
 	unsigned long free_highpages = 0;
 	pg_data_t *pgdat = NODE_DATA(nid);
 
-	for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++)
-		managed_pages += zone_managed_pages(&pgdat->node_zones[zone_type]);
-	val->totalram = managed_pages;
-	val->sharedram = node_page_state(pgdat, NR_SHMEM);
-	val->freeram = sum_zone_node_page_state(nid, NR_FREE_PAGES);
-#ifdef CONFIG_HIGHMEM
 	for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
 		struct zone *zone = &pgdat->node_zones[zone_type];
-
+		managed_pages += zone_managed_pages(zone);
 		if (is_highmem(zone)) {
 			managed_highpages += zone_managed_pages(zone);
 			free_highpages += zone_page_state(zone, NR_FREE_PAGES);
 		}
 	}
+
+	val->totalram = managed_pages;
+	val->sharedram = node_page_state(pgdat, NR_SHMEM);
+	val->freeram = sum_zone_node_page_state(nid, NR_FREE_PAGES);
 	val->totalhigh = managed_highpages;
 	val->freehigh = free_highpages;
-#else
-	val->totalhigh = managed_highpages;
-	val->freehigh = free_highpages;
-#endif
 	val->mem_unit = PAGE_SIZE;
 }
 #endif
Signature · 1 line
2.25.1
Signature · 2 lines
Cheers,
Harry (formerly known as Hyeonggon)