[PATCH 1/2] mm: extract common code for calculating total memory size

From: Yang Shi
Date: Wed Oct 25 2017 - 18:49:56 EST


Total memory size is needed by unreclaimable slub oom to check if
significant memory is used by a single slab. But, the caculation work is
done in show_mem(), so extracting the common code in order to share with
others.

Signed-off-by: Yang Shi <yang.s@xxxxxxxxxxxxxxx>
---
include/linux/mm.h | 25 +++++++++++++++++++++++++
lib/show_mem.c | 20 +-------------------
2 files changed, 26 insertions(+), 19 deletions(-)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index 935c4d4..e21b81e 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -2050,6 +2050,31 @@ extern int __meminit __early_pfn_to_nid(unsigned long pfn,
static inline void zero_resv_unavail(void) {}
#endif

+static inline void calc_mem_size(unsigned long *total, unsigned long *reserved,
+ unsigned long *highmem)
+{
+ pg_data_t *pgdat;
+
+ for_each_online_pgdat(pgdat) {
+ unsigned long flags;
+ int zoneid;
+
+ pgdat_resize_lock(pgdat, &flags);
+ for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
+ struct zone *zone = &pgdat->node_zones[zoneid];
+ if (!populated_zone(zone))
+ continue;
+
+ *total += zone->present_pages;
+ *reserved += zone->present_pages - zone->managed_pages;
+
+ if (is_highmem_idx(zoneid))
+ *highmem += zone->present_pages;
+ }
+ pgdat_resize_unlock(pgdat, &flags);
+ }
+}
+
extern void set_dma_reserve(unsigned long new_dma_reserve);
extern void memmap_init_zone(unsigned long, int, unsigned long,
unsigned long, enum memmap_context);
diff --git a/lib/show_mem.c b/lib/show_mem.c
index 0beaa1d..115475e 100644
--- a/lib/show_mem.c
+++ b/lib/show_mem.c
@@ -11,30 +11,12 @@

void show_mem(unsigned int filter, nodemask_t *nodemask)
{
- pg_data_t *pgdat;
unsigned long total = 0, reserved = 0, highmem = 0;

printk("Mem-Info:\n");
show_free_areas(filter, nodemask);

- for_each_online_pgdat(pgdat) {
- unsigned long flags;
- int zoneid;
-
- pgdat_resize_lock(pgdat, &flags);
- for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
- struct zone *zone = &pgdat->node_zones[zoneid];
- if (!populated_zone(zone))
- continue;
-
- total += zone->present_pages;
- reserved += zone->present_pages - zone->managed_pages;
-
- if (is_highmem_idx(zoneid))
- highmem += zone->present_pages;
- }
- pgdat_resize_unlock(pgdat, &flags);
- }
+ calc_mem_size(&total, &reserved, &highmem);

printk("%lu pages RAM\n", total);
printk("%lu pages HighMem/MovableOnly\n", highmem);
--
1.8.3.1