[PATCH 52/60] mm: count memory with userspace placement as general purpose
From: Gregory Price via B4 Relay
Date: Fri Nov 20 2026 - 13:18:54 EST
From: "Gregory Price (Meta)" <gourry@xxxxxxxxxx>
Userspace places ordinary data on N_MEMORY_MEMPOLICY nodes, and neither
the commit charge nor a task's RSS can tell nodes apart. Treat their
memory as general purpose: totalram_pages() and MemFree count it, and so
do the limits sized from them (overcommit, OOM capacity).
Other defaults sized from totalram_pages(), such as the zswap pool and
tmpfs sizes, also see the larger total, as they already do for memory
the kernel cannot allocate from in ZONE_MOVABLE.
Free memory on these nodes serves only private bindings and migration,
so counting it in MemAvailable or the global dirtyable memory would
overstate both. Add common_free_pages() and use it there.
MemAvailable also assumes about half of the file LRU can be reclaimed.
Reclaiming file pages on these nodes frees only private memory, so
count only the file LRU on common nodes, via common_file_pages().
File pages that reach a private node through migration stay covered
by the dirty limits' file LRU term.
Signed-off-by: Gregory Price (Meta) <gourry@xxxxxxxxxx>
---
mm/internal.h | 26 ++++++++++++++++++++++++--
mm/page-writeback.c | 7 ++++++-
mm/show_mem.c | 24 ++++++++++++++++++++----
3 files changed, 50 insertions(+), 7 deletions(-)
diff --git a/mm/internal.h b/mm/internal.h
index 4bac570bb29f..c83c100a5253 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -793,11 +793,33 @@ void set_recommended_min_free_kbytes(void);
extern char * const zone_names[MAX_NR_ZONES];
-/* true if the node is capable of servicing general system allocations */
+/*
+ * true if the node is capable of servicing general system allocations or
+ * userspace directed allocations (mempolicy, migration)
+ */
static inline bool node_memory_is_general_purpose(int nid)
{
return READ_ONCE(NODE_DATA(nid)->memory_features) &
- NODE_MEMORY_FEAT_COMMON;
+ NODE_MEMORY_FEAT_MEMPOLICY;
+}
+
+unsigned long common_free_pages(void);
+
+/* File LRU pages on common nodes, whose reclaim frees common memory. */
+static inline unsigned long common_file_pages(void)
+{
+ unsigned long nr = global_node_page_state(NR_ACTIVE_FILE) +
+ global_node_page_state(NR_INACTIVE_FILE);
+ int nid;
+
+ if (!IS_ENABLED(CONFIG_NUMA))
+ return nr;
+
+ for_each_node_state(nid, N_MEMORY_PRIVATE) {
+ nr -= min(nr, node_page_state(NODE_DATA(nid), NR_ACTIVE_FILE) +
+ node_page_state(NODE_DATA(nid), NR_INACTIVE_FILE));
+ }
+ return nr;
}
extern int min_free_kbytes;
diff --git a/mm/page-writeback.c b/mm/page-writeback.c
index 00293a64fcea..6181266bf159 100644
--- a/mm/page-writeback.c
+++ b/mm/page-writeback.c
@@ -317,7 +317,12 @@ static unsigned long global_dirtyable_memory(void)
{
unsigned long x;
- x = nr_free_pages();
+ /*
+ * Free private memory serves only private bindings and migration.
+ * File pages that reach private memory are counted with the file
+ * LRU below.
+ */
+ x = common_free_pages();
/*
* Pages reserved for the kernel should not be considered
* dirtyable, to prevent a situation where reclaim has to
diff --git a/mm/show_mem.c b/mm/show_mem.c
index 24dd33ba783b..de4769e06a53 100644
--- a/mm/show_mem.c
+++ b/mm/show_mem.c
@@ -46,17 +46,18 @@ long si_mem_available(void)
/*
* Estimate the amount of memory available for userspace allocations,
- * without causing swapping or OOM.
+ * without causing swapping or OOM. Only free common memory serves
+ * ordinary allocations.
*/
- available = nr_free_pages() - totalreserve_pages;
+ available = common_free_pages() - totalreserve_pages;
/*
* Not all the page cache can be freed, otherwise the system will
* start swapping or thrashing. Assume at least half of the page
* cache, or the low watermark worth of cache, needs to stay.
*/
- pagecache = global_node_page_state(NR_ACTIVE_FILE) +
- global_node_page_state(NR_INACTIVE_FILE);
+ /* Reclaiming file pages on private nodes frees only private memory. */
+ pagecache = common_file_pages();
pagecache -= min(pagecache / 2, wmark_low);
available += pagecache;
@@ -92,6 +93,21 @@ unsigned long nr_free_pages(void)
return nr;
}
+/* Free pages on common nodes, the free memory ordinary allocations can use. */
+unsigned long common_free_pages(void)
+{
+ unsigned long nr = global_zone_page_state(NR_FREE_PAGES);
+ int nid;
+
+ if (!IS_ENABLED(CONFIG_NUMA))
+ return nr;
+
+ for_each_node_state(nid, N_MEMORY_PRIVATE) {
+ nr -= min(nr, sum_zone_node_page_state(nid, NR_FREE_PAGES));
+ }
+ return nr;
+}
+
void si_meminfo(struct sysinfo *val)
{
val->totalram = totalram_pages();
--
2.56.1