Skip to contentSkip to conversation

Page Alloc Hogger

RFC PATCH In review Juan Yescas 4 participants 24 messages Aug 6 – Aug 11

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover Page Alloc Hogger

This patch series introduces the Page Alloc Hogger. The Page Alloc Hogger allows you to allocate memory pages from specific nodes, zones, migration types, and orders directly via debugfs. This provides key benefits for testing and debugging:

- Reproduce low-memory conditions: Easily trigger and inspect kernel mechanisms like direct reclaim, kswapd, the OOM killer, and allocation fallbacks.

- Simplify memory pressure debugging: Debug issues that only manifest under memory stress without needing custom kernel drivers or userspace programs to allocate memory.

- Simplify unit testing: Verify that memory management subsystems (direct reclaim, OOM killer, kswapd) trigger as expected in test suites.

- Measure performance under stress: Evaluate how applications behave and perform during severe memory pressure.

Usage:

1. To trigger the allocation, navigate to the debugfs path corresponding to your target node, memory zone, allocation order, and migration type, then write the requested allocation count to nr_pages_allocs.

For example, to make 3 allocs of order 9, Migrate Type Movable, Zone Normal and Node 0, run:

   $ echo 3 > /sys/kernel/debug/mm/node-0/zone-Normal/order-9/migrate-Movable/nr_pages_allocs

2. For each allocation created, a corresponding file named sequentially (1, 2, n) will appear in that directory.

   $ ls /sys/kernel/debug/mm/node-0/zone-Normal/order-9/migrate-Movable/
   1   2   3  nr_pages_allocs

3. To free the allocation, write the allocation file name in /sys/kernel/debug/mm/free. For example, to release the 2nd allocation run:

   $ echo 2 > /sys/kernel/debug/mm/free

Example:

$ cat /proc/pagetypeinfo
Page block order: 9
Pages per block:  512
Free pages count per migrate type at order 0  1  2  3  4  5  6  7  8  9   10
Node 0, zone    DMA, type   Unmovable      0  0  0  0  0  0  0  0  0  0    0
Node 0, zone    DMA, type     Movable      1  1  1  0  1  1  2  2  1  3  732
Node 0, zone    DMA, type Reclaimable      0  0  0  0  0  0  0  0  0  0    0
Node 0, zone    DMA, type  HighAtomic      0  0  0  0  0  0  0  0  0  0    0
Node 0, zone    DMA, type         CMA      0  0  0  0  0  0  0  0  0  1    7
Node 0, zone    DMA, type     Isolate      0  0  0  0  0  0  0  0  0  0    0
Node 0, zone Normal, type   Unmovable     13  7  0  1  0  0  1  0  1  0    0
Node 0, zone Normal, type     Movable      1  1  1  1  1  1  1  0  1  1 1221
Node 0, zone Normal, type Reclaimable      1  0  0  0  0  0  1  0  0  1    0
Node 0, zone Normal, type  HighAtomic      0  0  0  0  0  0  0  0  0  0    0
Node 0, zone Normal, type         CMA      0  0  0  0  0  0  0  0  0  1    7
Node 0, zone Normal, type     Isolate      0  0  0  0  0  0  0  0  0  0    0

Number of blocks type Unmovable Movable Reclaimable HighAtomic CMA Isolate

Node 0, zone      DMA          0     1520           0          0  16       0
Node 0, zone   Normal         12     2530           2          0  16       0
$ echo 1200 > /sys/kernel/debug/mm/node-0/zone-Normal/order-10/migrate-Movable/nr_pages_allocs
$ cat /proc/pagetypeinfo
Page block order: 9
Pages per block:  512
Free pages count per migrate type at order 0  1  2  3  4  5  6  7  8  9   10
Node 0, zone    DMA, type   Unmovable      0  0  0  0  0  0  0  0  0  0    0
Node 0, zone    DMA, type     Movable      1  1  1  0  1  1  2  2  1  3  732
Node 0, zone    DMA, type Reclaimable      0  0  0  0  0  0  0  0  0  0    0
Node 0, zone    DMA, type  HighAtomic      0  0  0  0  0  0  0  0  0  0    0
Node 0, zone    DMA, type         CMA      0  0  0  0  0  0  0  0  0  1    7
Node 0, zone    DMA, type     Isolate      0  0  0  0  0  0  0  0  0  0    0
Node 0, zone Normal, type   Unmovable     13  7  1  1  0  0  0  0  1  0    0
Node 0, zone Normal, type     Movable      1  1  1  1  1  1  1  0  1  1   21
Node 0, zone Normal, type Reclaimable      0  1  0  0  1  0  0  0  1  0    0
Node 0, zone Normal, type  HighAtomic      0  0  0  0  0  0  0  0  0  0    0
Node 0, zone Normal, type         CMA      0  0  0  0  0  0  0  0  0  1    7
Node 0, zone Normal, type     Isolate      0  0  0  0  0  0  0  0  0  0    0

Number of blocks type Unmovable Movable Reclaimable HighAtomic CMA Isolate

Node 0, zone      DMA          0     1520           0          0  16       0
Node 0, zone   Normal         12     2530           2          0  16       0
$ for i in `seq 1 1200`; do echo $i > /sys/kernel/debug/mm/free; done
$ cat /proc/pagetypeinfo
Page block order: 9
Pages per block:  512
Free pages count per migrate type at order 0  1  2  3  4  5  6  7  8  9   10
Node 0, zone    DMA, type   Unmovable      0  0  0  0  0  0  0  0  0  0    0
Node 0, zone    DMA, type     Movable      1  1  1  0  1  1  2  2  1  3  732
Node 0, zone    DMA, type Reclaimable      0  0  0  0  0  0  0  0  0  0    0
Node 0, zone    DMA, type  HighAtomic      0  0  0  0  0  0  0  0  0  0    0
Node 0, zone    DMA, type         CMA      0  0  0  0  0  0  0  0  0  1    7
Node 0, zone    DMA, type     Isolate      0  0  0  0  0  0  0  0  0  0    0
Node 0, zone Normal, type   Unmovable     13  7  1  1  0  0  0  0  1  0    0
Node 0, zone Normal, type     Movable      1  1  1  1  1  1  1  0  1  1 1221
Node 0, zone Normal, type Reclaimable      0  1  0  0  1  0  0  0  1  0    0
Node 0, zone Normal, type  HighAtomic      0  0  0  0  0  0  0  0  0  0    0
Node 0, zone Normal, type         CMA      0  0  0  0  0  0  0  0  0  1    7
Node 0, zone Normal, type     Isolate      0  0  0  0  0  0  0  0  0  0    0

Number of blocks type Unmovable Movable Reclaimable HighAtomic CMA Isolate

Node 0, zone      DMA          0     1520           0          0  16       0
Node 0, zone   Normal         12     2530           2          0  16       0

Notes:

- The only zone that is not supported is ZONE_DEVICE.
- MIGRATE_CMA will be supported soon

Juan Yescas (16):
mm: Page Alloc Hogger module
mm: Define structs for allocation requests and store allocations
mm: Define the caches for the structs req_alloc and page_alloc
mm: Define function to create a dir for each online node
mm: Define function to create a dir for populated zone
mm: Define function to create a dir for each page order
mm: Define function to create a dir for each migrate type
mm: Define function that creates the "nr_pages_allocs" file
mm: Read the nr_pages_allocs value given by user
mm: Set the selected zone in gfp_t flags
mm: Sets the migrate type in gfp_t flags
mm: Define the make_alloc() function
mm: Create the file associated with an allocation
mm: Free pages, remove files and clean cache when one alloc fails
mm: Create the "free" file to release the previously allocated pages
mm: Release resources when the page alloc hogger module exits

 mm/Kconfig.debug       |  12 +
 mm/Makefile            |   1 +
 mm/page_alloc_hogger.c | 610 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 623 insertions(+)
 create mode 100644 mm/page_alloc_hogger.c
Signature · 1 line
2.55.0.629.g250fe7f194-goog
#1 lore raw

RFC PATCH v2 1/16 mm: Page Alloc Hogger module

This patch contains the skeleton for the Page Alloc Hogger. It creates the "mm" dir that will be the base directory that will contain the "free" file and the "node-<x>" dirs.

---

Changes in v2:
- Change to a tristate module.
- Fix typos
- Use static for the mmdir declaration.

 mm/Kconfig.debug       |  12 +++++
 mm/Makefile            |   1 +
 mm/page_alloc_hogger.c | 115 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 128 insertions(+)
 create mode 100644 mm/page_alloc_hogger.c
mm/Kconfig.debug +12 -0
index 7638d75b27db..bad8a606152c 100644
@@ -309,3 +309,15 @@ config PER_VMA_LOCK_STATS
 	  overhead in the page fault path.
 
 	  If in doubt, say N.
+
+config PAGEALLOC_HOGGER
+	tristate "Alloc pages from different nodes, zones, migrate types and orders"
+	depends on DEBUG_FS
+	help
+	  Say Y to alloc pages from different nodes, zones, migrate type and
+	  orders using the debug fs filesystem. This helps to generate memory
+	  pressure easily. This is useful to debug memory reclaims issues,
+	  OOM triggers, page migration issues, etc.
+
+	  If in doubt, say N.
+
mm/Makefile +1 -0
index 2d0570a16e5b..b48f2ac6ee80 100644
@@ -133,6 +133,7 @@ obj-$(CONFIG_IDLE_PAGE_TRACKING) += page_idle.o
 obj-$(CONFIG_DEBUG_PAGEALLOC) += debug_page_alloc.o
 obj-$(CONFIG_DEBUG_PAGE_REF) += debug_page_ref.o
 obj-$(CONFIG_DAMON) += damon/
+obj-$(CONFIG_PAGEALLOC_HOGGER) += page_alloc_hogger.o
 obj-$(CONFIG_HARDENED_USERCOPY) += usercopy.o
 obj-$(CONFIG_PERCPU_STATS) += percpu-stats.o
 obj-$(CONFIG_ZONE_DEVICE) += memremap.o
mm/page_alloc_hogger.c new file +115 -0

115 lines not on this page · raw message

Signature · 1 line
2.55.0.629.g250fe7f194-goog
#2 lore raw

RFC PATCH v2 2/16 mm: Define structs for allocation requests and store allocations

In reply to Juan Yescas

This change introduces the next structs:

- struct req_alloc to keep the request info for the allocation.

- struct page_alloc to store a pointer to the page that was allocated, the request info and the file that represents this allocation. This file will be used to free the allocation when it is not longer needed.

---

Changes in v2:
- Fix comment block. Add an extra * at the beginning.

 mm/page_alloc_hogger.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
mm/page_alloc_hogger.c +28 -0
index 1d31e981920e..caeb2e5e8579 100644
@@ -91,6 +91,34 @@
 
 static struct dentry *mmdir;
 
+/**
+ * struct req_alloc - Represents the requested allocation
+ * @node_idx: The Node index to allocate from.
+ * @zone_idx: The Zone index to allocate from.
+ * @order: The Order of pages to allocate.
+ * @migrate_type: The Migrate type to allocate from.
+ * @parentdir: The parent dir where the file representing the alloc was created.
+ */
+struct req_alloc {
+	int node_idx;
+	int zone_idx;
+	int order;
+	int migrate_type;
+	struct dentry *parentdir;
+};
+
+/**
+ * struct page_alloc - Represents the page allocation.
+ * @page: The pointer to the page(s) allocated.
+ * @req_alloc: The details of the allocation.
+ * @alloc_dentry: The pointer to the file that represents the allocation.
+ */
+struct page_alloc {
+	struct page *page;
+	struct req_alloc *req_alloc;
+	struct dentry *alloc_dentry;
+};
+
 static int __init page_alloc_hogger_debugfs_init(void)
 {
 	mmdir = debugfs_create_dir("mm", NULL);
Signature · 1 line
2.55.0.629.g250fe7f194-goog
#3 lore raw

RFC PATCH v2 3/16 mm: Define the caches for the structs req_alloc and page_alloc

In reply to Juan Yescas

This change defines and initializes the caches for the structs req_alloc and page_alloc.

---

Changes in v2:
- Use static for the kmem_cache variables.
- Use \n for the pr_err messages.

 mm/page_alloc_hogger.c | 40 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 39 insertions(+), 1 deletion(-)
mm/page_alloc_hogger.c +39 -1
index caeb2e5e8579..175f479c487b 100644
@@ -85,9 +85,12 @@
 
 #include <linux/errno.h>
 #include <linux/debugfs.h>
+#include <linux/gfp.h>
+#include <linux/gfp_types.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/printk.h>
+#include <linux/slab.h>
 
 static struct dentry *mmdir;
 
@@ -119,20 +122,55 @@ struct page_alloc {
 	struct dentry *alloc_dentry;
 };
 
+static struct kmem_cache *req_alloc_cache;
+static struct kmem_cache *page_alloc_cache;
+
 static int __init page_alloc_hogger_debugfs_init(void)
 {
+	int ret;
+
+	req_alloc_cache = kmem_cache_create(
+		"req_alloc_cache", sizeof(struct req_alloc), 0,
+		SLAB_HWCACHE_ALIGN, NULL);
+	if (!req_alloc_cache) {
+		pr_err("The req_alloc_cache couldn't be created\n");
+		ret = -ENOMEM;
+		goto error_exit;
+	}
+
+	page_alloc_cache = kmem_cache_create(
+		"page_alloc_cache", sizeof(struct page_alloc), 0,
+		SLAB_HWCACHE_ALIGN, NULL);
+	if (!page_alloc_cache) {
+		pr_err("The page_alloc_cache couldn't be created\n");
+		ret = -ENOMEM;
+		goto clean_req_alloc_cache;
+	}
+
 	mmdir = debugfs_create_dir("mm", NULL);
 	if (IS_ERR(mmdir)) {
 		pr_err("Unable to create mm directory\n");
-		return PTR_ERR(mmdir);
+		ret = PTR_ERR(mmdir);
+		goto clean_page_alloc_cache;
 	}
 
 	return 0;
+
+clean_page_alloc_cache:
+	kmem_cache_destroy(page_alloc_cache);
+
+clean_req_alloc_cache:
+	kmem_cache_destroy(req_alloc_cache);
+
+error_exit:
+	return ret;
 }
 
 static void __exit page_alloc_hogger_debugfs_exit(void)
 {
 	debugfs_remove_recursive(mmdir);
+	kmem_cache_destroy(req_alloc_cache);
+	kmem_cache_destroy(page_alloc_cache);
 }
 
 module_init(page_alloc_hogger_debugfs_init);
Signature · 1 line
2.55.0.629.g250fe7f194-goog
#4 lore raw

RFC PATCH v2 4/16 mm: Define function to create a dir for each online node

In reply to Juan Yescas

This change creates a directory for each online node in the system.

---

Changes in v2:
- Fix comments.
- Use for_each_node_state(nid, N_MEMORY) instead.

 mm/page_alloc_hogger.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
mm/page_alloc_hogger.c +28 -0
index 175f479c487b..5765ecc43567 100644
@@ -89,6 +89,7 @@
 #include <linux/gfp_types.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
+#include <linux/nodemask.h>
 #include <linux/printk.h>
 #include <linux/slab.h>
 
@@ -125,6 +126,26 @@ struct page_alloc {
 static struct kmem_cache *req_alloc_cache;
 static struct kmem_cache *page_alloc_cache;
 
+/**
+ * create_nodes_subdirs() - Creates a directory for each node that contains
+ * memory.
+ */
+static inline int create_nodes_subdirs(struct dentry *mmdir)
+{
+	struct dentry *nodedir;
+	int nid;
+	char dirname[12];
+
+	for_each_node_state(nid, N_MEMORY) {
+		snprintf(dirname, sizeof(dirname), "node-%d", nid);
+		nodedir = debugfs_create_dir(dirname, mmdir);
+		if (IS_ERR(nodedir))
+			return PTR_ERR(nodedir);
+	}
+
+	return 0;
+}
+
 static int __init page_alloc_hogger_debugfs_init(void)
 {
 	int ret;
@@ -154,8 +175,15 @@ static int __init page_alloc_hogger_debugfs_init(void)
 		goto clean_page_alloc_cache;
 	}
 
+	ret = create_nodes_subdirs(mmdir);
+	if (ret)
+		goto clean_dir;
+
 	return 0;
 
+clean_dir:
+	debugfs_remove_recursive(mmdir);
+
 clean_page_alloc_cache:
 	kmem_cache_destroy(page_alloc_cache);
 
Signature · 1 line
2.55.0.629.g250fe7f194-goog
#5 lore raw

RFC PATCH v2 5/16 mm: Define function to create a dir for populated zone

In reply to Juan Yescas

This change creates a directory for each populated zone in the node.

Note: ZONE_DEVICE pages won't be exposed in this driver due they are not managed by the Buddy Allocator.

---

Changes in v2:
- Update comments.
- Remove unused variables.

 mm/page_alloc_hogger.c | 46 ++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 44 insertions(+), 2 deletions(-)
mm/page_alloc_hogger.c +44 -2
index 5765ecc43567..e8944080e82e 100644
@@ -88,6 +88,7 @@
 #include <linux/gfp.h>
 #include <linux/gfp_types.h>
 #include <linux/kernel.h>
+#include <linux/mmzone.h>
 #include <linux/module.h>
 #include <linux/nodemask.h>
 #include <linux/printk.h>
@@ -126,21 +127,62 @@ struct page_alloc {
 static struct kmem_cache *req_alloc_cache;
 static struct kmem_cache *page_alloc_cache;
 
+
+/**
+ * create_zones_subdirs() - Creates a directory for each populated zone in the
+ * node.
+ *
+ * Note: ZONE_DEVICE pages won't be allocated using this driver. The main reason
+ * is because they are not managed by the Buddy Allocator or CMA allocator.
+ */
+static inline int create_zones_subdirs(struct dentry *nodedir,
+					struct pglist_data *pgdata)
+{
+	struct dentry *zonedir;
+	struct zone *zone;
+	struct zone *node_zones = pgdata->node_zones;
+	char dirname[24];
+
+	for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
+		if (!populated_zone(zone))
+			continue;
+
+		/* ZONE_DEVICE pages won't be allocated using this driver. */
+		if (zone_is_zone_device(zone))
+			continue;
+
+		snprintf(dirname, sizeof(dirname), "zone-%s", zone->name);
+		zonedir = debugfs_create_dir(dirname, nodedir);
+		if (IS_ERR(zonedir))
+			return PTR_ERR(zonedir);
+	}
+
+	return 0;
+}
+
 /**
  * create_nodes_subdirs() - Creates a directory for each node that contains
- * memory.
+ * memory. Once that the node directory is created, it creates a directory for
+ * each zone in the node.
  */
 static inline int create_nodes_subdirs(struct dentry *mmdir)
 {
 	struct dentry *nodedir;
-	int nid;
 	char dirname[12];
+	int nid;
+	int ret;
 
 	for_each_node_state(nid, N_MEMORY) {
+		struct pglist_data *pgdata = NODE_DATA(nid);
+
 		snprintf(dirname, sizeof(dirname), "node-%d", nid);
 		nodedir = debugfs_create_dir(dirname, mmdir);
 		if (IS_ERR(nodedir))
 			return PTR_ERR(nodedir);
+
+		ret = create_zones_subdirs(nodedir, pgdata);
+		if (ret)
+			return ret;
 	}
 
 	return 0;
Signature · 1 line
2.55.0.629.g250fe7f194-goog
#6 lore raw

RFC PATCH v2 6/16 mm: Define function to create a dir for each page order

In reply to Juan Yescas

This change creates a directory for each page order inside the zone directory.

---

Changes in v2:
- Update comments.
- Remove unused parameters in function.

 mm/page_alloc_hogger.c | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)
mm/page_alloc_hogger.c +25 -1
index e8944080e82e..24856f7ea5d7 100644
@@ -127,10 +127,29 @@ struct page_alloc {
 static struct kmem_cache *req_alloc_cache;
 static struct kmem_cache *page_alloc_cache;
 
+/**
+ * create_page_orders_subdirs() - Creates a directory for each page order inside
+ * the zone directory.
+ */
+static inline int create_page_orders_subdirs(struct dentry *zonedir)
+{
+	struct dentry *orderdir;
+	char dirname[12];
+
+	for (int order = 0; order < NR_PAGE_ORDERS; order++) {
+		snprintf(dirname, sizeof(dirname), "order-%d", order);
+		orderdir = debugfs_create_dir(dirname, zonedir);
+		if (IS_ERR(orderdir))
+			return PTR_ERR(orderdir);
+	}
+
+	return 0;
+}
 
 /**
  * create_zones_subdirs() - Creates a directory for each populated zone in the
- * node.
+ * node. Once that the zone directory is created, it creates a directory for
+ * each order supported.
  *
  * Note: ZONE_DEVICE pages won't be allocated using this driver. The main reason
  * is because they are not managed by the Buddy Allocator or CMA allocator.
@@ -142,6 +161,7 @@ static inline int create_zones_subdirs(struct dentry *nodedir,
 	struct zone *zone;
 	struct zone *node_zones = pgdata->node_zones;
 	char dirname[24];
+	int ret;
 
 	for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
 		if (!populated_zone(zone))
@@ -155,6 +175,10 @@ static inline int create_zones_subdirs(struct dentry *nodedir,
 		zonedir = debugfs_create_dir(dirname, nodedir);
 		if (IS_ERR(zonedir))
 			return PTR_ERR(zonedir);
+
+		ret = create_page_orders_subdirs(zonedir);
+		if (ret)
+			return ret;
 	}
 
 	return 0;
Signature · 1 line
2.55.0.629.g250fe7f194-goog
#7 lore raw

RFC PATCH v2 7/16 mm: Define function to create a dir for each migrate type

In reply to Juan Yescas

This change creates a directory for each migrate type inside the order directory.

This function won't create a directory for MIGRATE_ISOLATE due they can't be allocated using the Buddy Allocator.

Note: The MIGRATE_CMA directory won't be created yet. It will be supported in the future.

---

Changes in v2:
- Update comments.
- Remove unused parameters.

 mm/page_alloc_hogger.c | 36 +++++++++++++++++++++++++++++++++++-
 1 file changed, 35 insertions(+), 1 deletion(-)
mm/page_alloc_hogger.c +35 -1
index 24856f7ea5d7..ab39ea300704 100644
@@ -127,20 +127,54 @@ struct page_alloc {
 static struct kmem_cache *req_alloc_cache;
 static struct kmem_cache *page_alloc_cache;
 
+/**
+ * create_migrate_type_subdirs() - Creates a directory for each migrate type
+ * inside the order directory.
+ */
+static inline int create_migrate_type_subdirs(struct dentry *orderdir)
+{
+	struct dentry *migratedir;
+	char dirname[24];
+
+	for (int mtype = 0; mtype < MIGRATE_TYPES; mtype++) {
+#ifdef CONFIG_CMA
+		if (mtype == MIGRATE_CMA) /* CMA allocs are not supported yet*/
+			continue;
+#endif
+#ifdef CONFIG_MEMORY_ISOLATION
+		if (mtype == MIGRATE_ISOLATE) /* can't allocate from here */
+			continue;
+#endif
+		snprintf(dirname, sizeof(dirname), "migrate-%s",
+			 migratetype_names[mtype]);
+		migratedir = debugfs_create_dir(dirname, orderdir);
+		if (IS_ERR(migratedir))
+			return PTR_ERR(migratedir);
+	}
+
+	return 0;
+}
+
 /**
  * create_page_orders_subdirs() - Creates a directory for each page order inside
- * the zone directory.
+ * the zone directory. Once that the page order directory is created, it creates
+ * a directory for each migrate type.
  */
 static inline int create_page_orders_subdirs(struct dentry *zonedir)
 {
 	struct dentry *orderdir;
 	char dirname[12];
+	int ret;
 
 	for (int order = 0; order < NR_PAGE_ORDERS; order++) {
 		snprintf(dirname, sizeof(dirname), "order-%d", order);
 		orderdir = debugfs_create_dir(dirname, zonedir);
 		if (IS_ERR(orderdir))
 			return PTR_ERR(orderdir);
+
+		ret = create_migrate_type_subdirs(orderdir);
+		if (ret)
+			return ret;
 	}
 
 	return 0;
Signature · 1 line
2.55.0.629.g250fe7f194-goog
#8 lore raw

RFC PATCH v2 8/16 mm: Define function that creates the "nr_pages_allocs" file

In reply to Juan Yescas

This change implements the function that creates the "nr_pages_allocs". This file will be used to make the allocations.

When the user wants to make an allocation, it will write
the number of allocations to this file. For example, if want to make 56 allocations of order 9 in the Node 0, Normal Zone and Migrate type Movable, we execute this command:

echo 56 > <sysfs>/mm/node-0/zone-Normal/order-9/migrate-Movable/nr_pages_allocs

---

Changes in v2:
- Update file permissions to 0200.
- Free req_alloc.

 mm/page_alloc_hogger.c | 81 +++++++++++++++++++++++++++++++++++++-----
 1 file changed, 73 insertions(+), 8 deletions(-)
mm/page_alloc_hogger.c +73 -8
index ab39ea300704..ded3f0fc6641 100644
@@ -127,14 +127,70 @@ struct page_alloc {
 static struct kmem_cache *req_alloc_cache;
 static struct kmem_cache *page_alloc_cache;
 
+/**
+ * req_page_alloc_write() - Allocates the pages on the requested node, zone,
+ * order and migrate type. Once the allocation is performed, a file is created
+ * to free the allocation later.
+ */
+static ssize_t req_page_alloc_write(struct file *file, const char __user *ubuf,
+				     size_t cnt, loff_t *ppos)
+{
+	return cnt;
+}
+
+static const struct file_operations req_page_alloc_fops = {
+	.owner = THIS_MODULE,
+	.open = simple_open,
+	.write = req_page_alloc_write,
+};
+
+/**
+ * create_nr_pages_allocs_file() - Creates the file "nr_pages_allocs".
+ *
+ * The "nr_pages_allocs" file will be used to write the number of allocations
+ * that will be performed. When the allocations are performed, a new file for
+ * each allocation will be created in @migratedir.
+ */
+static inline int create_nr_pages_allocs_file(struct dentry *migratedir,
+					      int nid, int zone_idx,
+					      int order, int mtype)
+{
+	struct req_alloc *req;
+	struct dentry *nr_pages;
+
+	req = kmem_cache_alloc(req_alloc_cache, GFP_KERNEL);
+	if (!req) {
+		pr_err("Failed to create nr_pages_allocs_info");
+		return -ENOMEM;
+	}
+
+	req->node_idx = nid;
+	req->zone_idx = zone_idx;
+	req->order = order;
+	req->migrate_type = mtype;
+	req->parentdir = migratedir;
+
+	nr_pages = debugfs_create_file("nr_pages_allocs", 0200, migratedir, req,
+				       &req_page_alloc_fops);
+	if (IS_ERR(nr_pages)) {
+		kmem_cache_free(req_alloc_cache, req);
+		return PTR_ERR(nr_pages);
+	}
+
+	return 0;
+}
+
 /**
  * create_migrate_type_subdirs() - Creates a directory for each migrate type
- * inside the order directory.
+ * inside the order directory. Once the migrate type directory is created, it
+ * creates the "nr_pages_allocs" file.
  */
-static inline int create_migrate_type_subdirs(struct dentry *orderdir)
+static inline int create_migrate_type_subdirs(struct dentry *orderdir, int nid,
+					int zone_idx, int order)
 {
 	struct dentry *migratedir;
 	char dirname[24];
+	int ret;
 
 	for (int mtype = 0; mtype < MIGRATE_TYPES; mtype++) {
 #ifdef CONFIG_CMA
@@ -150,6 +206,11 @@ static inline int create_migrate_type_subdirs(struct dentry *orderdir)
 		migratedir = debugfs_create_dir(dirname, orderdir);
 		if (IS_ERR(migratedir))
 			return PTR_ERR(migratedir);
+
+		ret = create_nr_pages_allocs_file(migratedir, nid, zone_idx,
+						order, mtype);
+		if (ret)
+			return ret;
 	}
 
 	return 0;
@@ -160,7 +221,8 @@ static inline int create_migrate_type_subdirs(struct dentry *orderdir)
  * the zone directory. Once that the page order directory is created, it creates
  * a directory for each migrate type.
  */
-static inline int create_page_orders_subdirs(struct dentry *zonedir)
+static inline int create_page_orders_subdirs(struct dentry *zonedir, int nid,
+					int zone_idx)
 {
 	struct dentry *orderdir;
 	char dirname[12];
@@ -172,7 +234,8 @@ static inline int create_page_orders_subdirs(struct dentry *zonedir)
 		if (IS_ERR(orderdir))
 			return PTR_ERR(orderdir);
 
-		ret = create_migrate_type_subdirs(orderdir);
+		ret = create_migrate_type_subdirs(orderdir, nid, zone_idx,
+						order);
 		if (ret)
 			return ret;
 	}
@@ -189,15 +252,17 @@ static inline int create_page_orders_subdirs(struct dentry *zonedir)
  * is because they are not managed by the Buddy Allocator or CMA allocator.
  */
 static inline int create_zones_subdirs(struct dentry *nodedir,
-					struct pglist_data *pgdata)
+					struct pglist_data *pgdata, int nid)
 {
 	struct dentry *zonedir;
 	struct zone *zone;
 	struct zone *node_zones = pgdata->node_zones;
+	int zone_idx;
 	char dirname[24];
 	int ret;
 
-	for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
+	for (zone = node_zones, zone_idx = 0; zone - node_zones < MAX_NR_ZONES;
+			++zone, ++zone_idx) {
 		if (!populated_zone(zone))
 			continue;
 
@@ -210,7 +275,7 @@ static inline int create_zones_subdirs(struct dentry *nodedir,
 		if (IS_ERR(zonedir))
 			return PTR_ERR(zonedir);
 
-		ret = create_page_orders_subdirs(zonedir);
+		ret = create_page_orders_subdirs(zonedir, nid, zone_idx);
 		if (ret)
 			return ret;
 	}
@@ -238,7 +303,7 @@ static inline int create_nodes_subdirs(struct dentry *mmdir)
 		if (IS_ERR(nodedir))
 			return PTR_ERR(nodedir);
 
-		ret = create_zones_subdirs(nodedir, pgdata);
+		ret = create_zones_subdirs(nodedir, pgdata, nid);
 		if (ret)
 			return ret;
 	}
Signature · 1 line
2.55.0.629.g250fe7f194-goog
#9 lore raw

RFC PATCH v2 9/16 mm: Read the nr_pages_allocs value given by user

In reply to Juan Yescas

This change reads the nr_page_allocs value given by the user and validates that is an unsigned long.

If the input is valid, it will allocate an array of nr_page_allocs size. This array will store the allocation ids. These allocation ids will be used to free the allocations.

---

Changes in v2:
- Free allocs_ids.

 mm/page_alloc_hogger.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
mm/page_alloc_hogger.c +15 -0
index ded3f0fc6641..9b1eaf3814e1 100644
@@ -135,6 +135,21 @@ static struct kmem_cache *page_alloc_cache;
 static ssize_t req_page_alloc_write(struct file *file, const char __user *ubuf,
 				     size_t cnt, loff_t *ppos)
 {
+	unsigned long nr_pages_allocs;
+	unsigned long *allocs_ids;
+	int ret;
+
+	ret = kstrtoul_from_user(ubuf, cnt, 10, &nr_pages_allocs);
+	if (ret)
+		return ret;
+
+	allocs_ids = kmalloc_array(nr_pages_allocs, sizeof(unsigned long),
+					GFP_KERNEL);
+	if (!allocs_ids)
+		return -ENOMEM;
+
+	kfree(allocs_ids);
+
 	return cnt;
 }
 
Signature · 1 line
2.55.0.629.g250fe7f194-goog
#10 lore raw

RFC PATCH v2 10/16 mm: Set the selected zone in gfp_t flags

In reply to Juan Yescas

This change sets the selected zone in gfp_t flags.

---

Changes in v2:
- Use static for the set_zone_to_alloc_from function.

 mm/page_alloc_hogger.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
mm/page_alloc_hogger.c +32 -0
index 9b1eaf3814e1..0e9ac43e9868 100644
@@ -127,6 +127,34 @@ struct page_alloc {
 static struct kmem_cache *req_alloc_cache;
 static struct kmem_cache *page_alloc_cache;
 
+static inline void set_zone_to_alloc_from(int zone_idx, gfp_t *flags_ptr)
+{
+	switch (zone_idx) {
+#ifdef CONFIG_ZONE_DMA
+	case ZONE_DMA:
+		*flags_ptr |= __GFP_DMA;
+		break;
+#endif
+#ifdef CONFIG_ZONE_DMA32
+	case ZONE_DMA32:
+		*flags_ptr |= __GFP_DMA32;
+		break;
+#endif
+#ifdef CONFIG_HIGHMEM
+	case ZONE_HIGHMEM:
+		*flags_ptr |= __GFP_HIGHMEM;
+		break;
+#endif
+	case ZONE_MOVABLE:
+		*flags_ptr |= __GFP_MOVABLE;
+		break;
+	case ZONE_NORMAL:
+	default:
+		*flags_ptr |= 0;
+		break;
+	}
+}
+
 /**
  * req_page_alloc_write() - Allocates the pages on the requested node, zone,
  * order and migrate type. Once the allocation is performed, a file is created
@@ -135,8 +163,10 @@ static struct kmem_cache *page_alloc_cache;
 static ssize_t req_page_alloc_write(struct file *file, const char __user *ubuf,
 				     size_t cnt, loff_t *ppos)
 {
+	struct req_alloc *req = file->private_data;
 	unsigned long nr_pages_allocs;
 	unsigned long *allocs_ids;
+	gfp_t flags = 0;
 	int ret;
 
 	ret = kstrtoul_from_user(ubuf, cnt, 10, &nr_pages_allocs);
@@ -148,6 +178,8 @@ static ssize_t req_page_alloc_write(struct file *file, const char __user *ubuf,
 	if (!allocs_ids)
 		return -ENOMEM;
 
+	set_zone_to_alloc_from(req->zone_idx, &flags);
+
 	kfree(allocs_ids);
 
 	return cnt;
Signature · 1 line
2.55.0.629.g250fe7f194-goog
#11 lore raw

RFC PATCH v2 11/16 mm: Sets the migrate type in gfp_t flags

In reply to Juan Yescas

This change sets the selected migrate type in gfp_t flags.

---

Changes in v2:
- Make sure that ZONE_MOVABLE is only used by MIGRATE_MOVABLE requests.

 mm/page_alloc_hogger.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
mm/page_alloc_hogger.c +22 -0
index 0e9ac43e9868..4b72acfbc087 100644
@@ -155,6 +155,24 @@ static inline void set_zone_to_alloc_from(int zone_idx, gfp_t *flags_ptr)
 	}
 }
 
+static inline void set_migrate_type_to_alloc_from(int migrate_type, gfp_t *flags_ptr)
+{
+	switch (migrate_type) {
+	case MIGRATE_UNMOVABLE:
+		*flags_ptr &= ~(__GFP_RECLAIMABLE | __GFP_MOVABLE);
+		break;
+	case MIGRATE_RECLAIMABLE:
+		*flags_ptr |= __GFP_RECLAIMABLE;
+		break;
+	case MIGRATE_HIGHATOMIC:
+		*flags_ptr |= __GFP_HIGH;
+		break;
+	case MIGRATE_MOVABLE: default:
+		*flags_ptr |= __GFP_MOVABLE;
+		break;
+	}
+}
+
 /**
  * req_page_alloc_write() - Allocates the pages on the requested node, zone,
  * order and migrate type. Once the allocation is performed, a file is created
@@ -179,6 +197,7 @@ static ssize_t req_page_alloc_write(struct file *file, const char __user *ubuf,
 		return -ENOMEM;
 
 	set_zone_to_alloc_from(req->zone_idx, &flags);
+	set_migrate_type_to_alloc_from(req->migrate_type, &flags);
 
 	kfree(allocs_ids);
 
@@ -248,6 +267,9 @@ static inline int create_migrate_type_subdirs(struct dentry *orderdir, int nid,
 		if (mtype == MIGRATE_ISOLATE) /* can't allocate from here */
 			continue;
 #endif
+		if (zone_idx == ZONE_MOVABLE && mtype != MIGRATE_MOVABLE)
+			continue;
+
 		snprintf(dirname, sizeof(dirname), "migrate-%s",
 			 migratetype_names[mtype]);
 		migratedir = debugfs_create_dir(dirname, orderdir);
Signature · 1 line
2.55.0.629.g250fe7f194-goog
#12 lore raw

RFC PATCH v2 12/16 mm: Define the make_alloc() function

In reply to Juan Yescas

The make_alloc() is the core function that performs the allocation.

The tasks of this function are:

- Call the Buddy allocator to make the allocation.
- Store the details of the allocation in struct page_alloc.
- Insert the struct page_alloc in an xarray using the allocation id as key.

If something fails during the allocation, the pages and cache will be freed.

---

Changes in v2:
- Once that page_alloc is initialized, insert it in xarray.
- Use 20 characters for the file name instead of 12.
- Fix use-after-free issue while releasing the pages.

 mm/page_alloc_hogger.c | 72 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 72 insertions(+)
mm/page_alloc_hogger.c +72 -0
index 4b72acfbc087..e22247b7bd98 100644
@@ -96,6 +96,18 @@
 
 static struct dentry *mmdir;
 
+/**
+ * atomic_long_t allocs_file_seq - Represents the naming sequence used for
+ * allocation files.
+ */
+static atomic_long_t allocs_file_seq = ATOMIC_INIT(0);
+
+/**
+ * allocs_xa - Represent the xarray that contains the actual allocations perform
+ * by this driver.
+ */
+static DEFINE_XARRAY(allocs_xa);
+
 /**
  * struct req_alloc - Represents the requested allocation
  * @node_idx: The Node index to allocate from.
@@ -173,6 +185,47 @@ static inline void set_migrate_type_to_alloc_from(int migrate_type, gfp_t *flags
 	}
 }
 
+static int make_alloc(struct req_alloc *req,
+			    gfp_t flags, unsigned long *alloc_id)
+{
+	struct page_alloc *pa;
+	struct page *page;
+	char new_alloc_name[20];
+	int ret;
+
+	page = alloc_pages_node_noprof(req->node_idx, flags, req->order);
+	if (page) {
+		pa = kmem_cache_alloc(page_alloc_cache, GFP_KERNEL);
+		if (!pa) {
+			ret = -ENOMEM;
+			goto free_pages;
+		}
+
+		*alloc_id = atomic_long_inc_return(&allocs_file_seq);
+		snprintf(new_alloc_name, sizeof(new_alloc_name), "%lu",
+			 *alloc_id);
+
+		pa->req_alloc = req;
+		pa->page = page;
+
+		ret = xa_insert(&allocs_xa, *alloc_id, pa, GFP_KERNEL);
+		if (ret)
+			goto free_page_alloc;
+	} else {
+		return -ENOMEM;
+	}
+
+	return 0;
+
+free_page_alloc:
+	kmem_cache_free(page_alloc_cache, pa);
+
+free_pages:
+	__free_pages(page, req->order);
+
+	return ret;
+}
+
 /**
  * req_page_alloc_write() - Allocates the pages on the requested node, zone,
  * order and migrate type. Once the allocation is performed, a file is created
@@ -182,10 +235,12 @@ static ssize_t req_page_alloc_write(struct file *file, const char __user *ubuf,
 				     size_t cnt, loff_t *ppos)
 {
 	struct req_alloc *req = file->private_data;
+	unsigned long alloc_id;
 	unsigned long nr_pages_allocs;
 	unsigned long *allocs_ids;
 	gfp_t flags = 0;
 	int ret;
+	int i;
 
 	ret = kstrtoul_from_user(ubuf, cnt, 10, &nr_pages_allocs);
 	if (ret)
@@ -199,9 +254,26 @@ static ssize_t req_page_alloc_write(struct file *file, const char __user *ubuf,
 	set_zone_to_alloc_from(req->zone_idx, &flags);
 	set_migrate_type_to_alloc_from(req->migrate_type, &flags);
 
+	for (i = 0; i < nr_pages_allocs; i++) {
+		ret = make_alloc(req, flags, &alloc_id);
+		if (ret)
+			goto free_allocs;
+
+		allocs_ids[i] = alloc_id;
+	}
+
 	kfree(allocs_ids);
 
 	return cnt;
+
+free_allocs:
+	/*
+	 * A proper clean up of the pages and page_alloc allocations will
+	 * be done in a follow up patch of this topic.
+	 */
+	kfree(allocs_ids);
+
+	return ret;
 }
 
 static const struct file_operations req_page_alloc_fops = {
Signature · 1 line
2.55.0.629.g250fe7f194-goog
#13 lore raw

RFC PATCH v2 13/16 mm: Create the file associated with an allocation

In reply to Juan Yescas

This change creates the file associated with an allocation. The name of the file will be given by incrementing the atomic long "allocs_file_seq".

This file will be used to free the allocation when the user requests it. The implementation will be included in a follow-up patch of this topic.

---

Changes in v2:
- Update commit message.
- Set file permissions to 0400 instead of 0644.

 mm/page_alloc_hogger.c | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)
mm/page_alloc_hogger.c +27 -1
index e22247b7bd98..a70c924e4172 100644
@@ -185,12 +185,26 @@ static inline void set_migrate_type_to_alloc_from(int migrate_type, gfp_t *flags
 	}
 }
 
+static ssize_t page_alloc_read(struct file *file, char __user *ubuf,
+				    size_t cnt, loff_t *ppos)
+{
+	/*  Which information should be print about the page or allocation? */
+	return 0;
+}
+
+static const struct file_operations page_alloc_fops = {
+	.owner = THIS_MODULE,
+	.open = simple_open,
+	.read = page_alloc_read,
+};
+
 static int make_alloc(struct req_alloc *req,
 			    gfp_t flags, unsigned long *alloc_id)
 {
 	struct page_alloc *pa;
 	struct page *page;
 	char new_alloc_name[20];
+	struct dentry *new_alloc_dentry;
 	int ret;
 
 	page = alloc_pages_node_noprof(req->node_idx, flags, req->order);
@@ -205,18 +219,30 @@ static int make_alloc(struct req_alloc *req,
 		snprintf(new_alloc_name, sizeof(new_alloc_name), "%lu",
 			 *alloc_id);
 
+		new_alloc_dentry = debugfs_create_file(
+			new_alloc_name, 0400, req->parentdir,
+			pa, &page_alloc_fops);
+		if (IS_ERR(new_alloc_dentry)) {
+			ret = PTR_ERR(new_alloc_dentry);
+			goto free_page_alloc;
+		}
+
 		pa->req_alloc = req;
 		pa->page = page;
+		pa->alloc_dentry = new_alloc_dentry;
 
 		ret = xa_insert(&allocs_xa, *alloc_id, pa, GFP_KERNEL);
 		if (ret)
-			goto free_page_alloc;
+			goto remove_alloc_dentry;
 	} else {
 		return -ENOMEM;
 	}
 
 	return 0;
 
+remove_alloc_dentry:
+	debugfs_remove(new_alloc_dentry);
+
 free_page_alloc:
 	kmem_cache_free(page_alloc_cache, pa);
 
Signature · 1 line
2.55.0.629.g250fe7f194-goog
#14 lore raw

RFC PATCH v2 14/16 mm: Free pages, remove files and clean cache when one alloc fails

In reply to Juan Yescas

When one of the requested allocations failed, do

- free the pages that were previously allocated as part of the request.
- remove the files that were created as part of the request.
- clean the kmem cache.
- Erase the struct page_alloc objects from the xarray that were inserted as part of the request.

---

Changes in v2:
- Remove comment about the future implementation.

 mm/page_alloc_hogger.c | 31 +++++++++++++++++++++++++++----
 1 file changed, 27 insertions(+), 4 deletions(-)
mm/page_alloc_hogger.c +27 -4
index a70c924e4172..ce53a17ac790 100644
@@ -93,6 +93,7 @@
 #include <linux/nodemask.h>
 #include <linux/printk.h>
 #include <linux/slab.h>
+#include <linux/xarray.h>
 
 static struct dentry *mmdir;
 
@@ -252,6 +253,23 @@ static int make_alloc(struct req_alloc *req,
 	return ret;
 }
 
+static int free_alloc_helper(unsigned long alloc_id)
+{
+	struct page_alloc *pa;
+
+	pa = xa_erase(&allocs_xa, alloc_id);
+	if (!pa) {
+		pr_err("The alloc_id %lu was not found!", alloc_id);
+		return -EINVAL;
+	}
+
+	__free_pages(pa->page, pa->req_alloc->order);
+	debugfs_remove(pa->alloc_dentry);
+	kmem_cache_free(page_alloc_cache, pa);
+
+	return 0;
+}
+
 /**
  * req_page_alloc_write() - Allocates the pages on the requested node, zone,
  * order and migrate type. Once the allocation is performed, a file is created
@@ -293,10 +311,15 @@ static ssize_t req_page_alloc_write(struct file *file, const char __user *ubuf,
 	return cnt;
 
 free_allocs:
-	/*
-	 * A proper clean up of the pages and page_alloc allocations will
-	 * be done in a follow up patch of this topic.
-	 */
+	/* Free all the pages and resources previously allocated. */
+	for (int j = 0; j < i; j++) {
+		int ret2 = free_alloc_helper(allocs_ids[j]);
+
+		if (ret2)
+			pr_err("Unable to free pages associated with file %lu",
+			       allocs_ids[j]);
+	}
+
 	kfree(allocs_ids);
 
 	return ret;
Signature · 1 line
2.55.0.629.g250fe7f194-goog
#15 lore raw

RFC PATCH v2 15/16 mm: Create the "free" file to release the previously allocated pages

In reply to Juan Yescas

This change creates the "free" file and defines its file_operations to release the previously allocated pages. To release an allocation, the user will run:

echo <file name> > /sys/kernel/debug/mm/free

---

Changes in v2:
- Set the file permissions to 0200.

 mm/page_alloc_hogger.c | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
mm/page_alloc_hogger.c +40 -0
index ce53a17ac790..6a6fbb78513a 100644
@@ -83,6 +83,7 @@
 
 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
+#include <linux/atomic.h>
 #include <linux/errno.h>
 #include <linux/debugfs.h>
 #include <linux/gfp.h>
@@ -331,6 +332,37 @@ static const struct file_operations req_page_alloc_fops = {
 	.write = req_page_alloc_write,
 };
 
+/**
+ * free_page_alloc_write() - free the previously allocated pages.
+ * @file: File where the user is writing.
+ * @ubuf: Contains the name of the file the user wants to delete and have the
+ *       memory associated with that file freed.
+ * @cnt: Size of @ubuf.
+ * @ppos: Current position in the file.
+ */
+static ssize_t free_page_alloc_write(struct file *file,
+				      const char __user *ubuf, size_t cnt,
+				      loff_t *ppos)
+{
+	int ret;
+	unsigned long alloc_id;
+
+	ret = kstrtoul_from_user(ubuf, cnt, 10, &alloc_id);
+	if (ret)
+		return ret;
+
+	ret = free_alloc_helper(alloc_id);
+	if (ret)
+		return ret;
+
+	return cnt;
+}
+
+static const struct file_operations free_page_alloc_fops = {
+	.owner = THIS_MODULE,
+	.write = free_page_alloc_write,
+};
+
 /**
  * create_nr_pages_allocs_file() - Creates the file "nr_pages_allocs".
  *
@@ -503,6 +535,7 @@ static inline int create_nodes_subdirs(struct dentry *mmdir)
 
 static int __init page_alloc_hogger_debugfs_init(void)
 {
+	struct dentry *free_file;
 	int ret;
 
 	req_alloc_cache = kmem_cache_create(
@@ -534,6 +567,13 @@ static int __init page_alloc_hogger_debugfs_init(void)
 	if (ret)
 		goto clean_dir;
 
+	free_file = debugfs_create_file("free", 0200, mmdir, NULL,
+					&free_page_alloc_fops);
+	if (IS_ERR(free_file)) {
+		ret = PTR_ERR(free_file);
+		goto clean_dir;
+	}
+
 	return 0;
 
 clean_dir:
Signature · 1 line
2.55.0.629.g250fe7f194-goog
#16 lore raw

RFC PATCH v2 16/16 mm: Release resources when the page alloc hogger module exits

In reply to Juan Yescas

When the page alloc hogger module exits:

- free all the previously allocated pages
- Erase all the entries from the xarray

---

Changes in v2:
- Refactor the exit method.
- Release the page_alloc object.

 mm/page_alloc_hogger.c | 6 ++++++
 1 file changed, 6 insertions(+)
mm/page_alloc_hogger.c +6 -0
index 6a6fbb78513a..72c0505a6f5a 100644
@@ -591,6 +591,12 @@ static int __init page_alloc_hogger_debugfs_init(void)
 
 static void __exit page_alloc_hogger_debugfs_exit(void)
 {
+	struct page_alloc *pa;
+	unsigned long idx;
+
+	xa_for_each(&allocs_xa, idx, pa)
+		free_alloc_helper(idx);
+
 	debugfs_remove_recursive(mmdir);
 	kmem_cache_destroy(req_alloc_cache);
 	kmem_cache_destroy(page_alloc_cache);
Signature · 1 line
2.55.0.629.g250fe7f194-goog
maintainer
#17 lore raw
In reply to Juan Yescas
Quoted 4 lines · On Wed, 5 Aug 2026 18:09:01 -0700 Juan Yescas <[email protected]> wrote:

This patch series introduces the Page Alloc Hogger. The Page Alloc Hogger allows you to allocate memory pages from specific nodes, zones, migration types, and orders directly via debugfs. This provides key benefits for testing and debugging:

OK, so this is targeted at kernel developers. And they may indeed find it useful. I see value in us developing a common way for developers to apply well-targeted stress to MM.

Probably everyone has their own favorite memory stresstest suite. Most of these will be in userspace[*] but I see there is merit in doing it in-kernel. Any perspective you can add to this choice would be interesting?

Some usage scenarioizing would help. Is this being used within google? If so, for what purpose and with what results? Sell it to us - help your audience understand what benefit it offers to them.

If this proposal has legs then we should Document/ it separately - that big block comment in page_alloc_hogger.c will become unweildy.

One could consider plumbing this into selftests/ in some fashion, but I wouldn't encourage that - longrunning torture tests aren't appropriate for selftests, which are nice and snappy. Perhaps a new tools/testing/stresstests will one day appear.

Quoted 1 line

+obj-$(CONFIG_PAGEALLOC_HOGGER) += page_alloc_hogger.o

page_alloc or pagealloc. Choose only one, lest you drive people crazy for ever.

Sashiko went totally nuts. Have fun with that ;) But I wouldn't do a ton of work on this until you've heard positive noises from the MM team, Guys, poke. wdyt, is there potential here?

	https://sashiko.dev/#/patchset/[email protected]

[*] Back in the days when I was trying to get redhat ext3 and 2.5.x
MM to do something other than lock up or crash, I wrote a userspace thing called "usemem". In recent times I've seen people quoting usemem results and wondered "is that my thing". So I looked it up. It is! And it's now quite unrecognizable.

Because, obviously, people found it useful and so they used it and added to it and added to it and more.

And I expect the same will occur with "Page Alloc Hogger" (terrible name, btw. How about "pagehog"? "memhog"), if it is adopted. People will use it and will add to it.

#18 lore raw
Quoted 13 lines · Andrew Morton
Quoted 4 lines · On Wed, 5 Aug 2026 18:09:01 -0700 Juan Yescas <[email protected]> wrote:

This patch series introduces the Page Alloc Hogger. The Page Alloc Hogger allows you to allocate memory pages from specific nodes, zones, migration types, and orders directly via debugfs. This provides key benefits for testing and debugging:

OK, so this is targeted at kernel developers. And they may indeed find it useful. I see value in us developing a common way for developers to apply well-targeted stress to MM.

Quoted 5 lines

Probably everyone has their own favorite memory stresstest suite. Most of these will be in userspace[*] but I see there is merit in doing it in-kernel. Any perspective you can add to this choice would be interesting?

Thanks for your comments and apologies for the delay in getting back to you, I was gathering some examples.

I think some scenarios and tests are hard to reproduce using userspace memory stresstests. For example memory fragmentation in one node or fragmentation
in one zone, or fragmentation by migrate type. Testing memory fallbacks, etc.

This proposal makes these scenarios easy to reproduce.

Quoted 2 lines

Some usage scenarioizing would help. Is this being used within google?

We are planning to add kselftests that create memory pressure in the system and test that kswapd, oom, lmkd, compaction, etc., are called accordingly.

This tool will also be used to run benchmarks comparing 4KB vs 16kb pagesizes under
various conditions (low memory, high memory fragmentation). Currently, in order to create memory pressure, we modify the dts or set the mem kernel parameter to reduce the available RAM and then cycle through apps for hours to get the memory fragmented. With this tool, we can create memory pressure and memory fragmentation pretty straightforward and then run the benchmarks under these conditions.

Quoted 3 lines

If so, for what purpose and with what results? Sell it to us - help your audience understand what benefit it offers to them.

That is a good point. Although this tool was written for devices using less than 32 GBs
of memory, it could be used for servers. For example, if you want to create memory
pressure in the different Nodes or zones in the server, you can easily do it by running

# Alloc 500 GiB node 0 zone DMA
echo 128000 > /sys/kernel/debug/mm/node-0/zone-DMA/order-10/migrate-Reclaimable/nr_pages_allocs

# Alloc 1000 GiB node 1 zone Normal
echo 256000 > /sys/kernel/debug/mm/node-1/zone-Normal/order-10/migrate-Reclaimable/nr_pages_allocs

# Alloc 500 GiB node 2 zone Normal
echo 128000 > /sys/kernel/debug/mm/node-1/zone-Normal/order-10/migrate-Reclaimable/nr_pages_allocs

or you can run this script to allocate 500GiB across all nodes in the system:

for node in set 0 16; do echo 128000 > /sys/kernel/debug/mm/node-$node/zone-Normal/order-10/migrate-Reclaimable/nr_pages_allocs; done

When my team want to reproduce memory issues due to fragmentation, it is not straightforward to set the memory into this state consistently with our current tools.

Let's say that we are debugging an issue where we have enough memory but the high
high-order allocations are failing due to memory fragmentation which was caused by
pinned pages in the different page blocks of the zone. How could we reproduce this fragmentation
artificially in Node 0/Zone Normal?

# cat /proc/pagetypeinfo
Page block order: 9
Pages per block:  512

Free pages count per migrate type at order 0 1 2 3 4 5
6 7 8 9 10

Node 0, zone    DMA, type   Unmovable      0   0    0    0    0   0
0   0   0   0   0
Node 0, zone    DMA, type     Movable      1   1    1    0    1   1
2   2   1   3 708
Node 0, zone    DMA, type Reclaimable      0   0    0    0    0   0
0   0   0   0   0
Node 0, zone    DMA, type  HighAtomic      0   0    0    0    0   0
0   0   0   0   0
Node 0, zone    DMA, type         CMA      0   0    0    0    0   0

0 0 0 1 31
Node 0, zone DMA, type Isolate 0 0 0 0 0 0
0 0 0 0 0
Node 0, zone Normal, type Unmovable 492 445 301 31 6 3
1 0 1 1 0
Node 0, zone Normal, type Movable 387 366 357 345 341 341
329 303 291 278 559
Node 0, zone Normal, type Reclaimable 861 825 1516 1387 1176 832
473 371 361 226 10
Node 0, zone Normal, type HighAtomic 0 0 0 0 0 0
0 0 0 0 0
Node 0, zone Normal, type CMA 0 0 0 0 0 0
0 0 0 1 7
Node 0, zone Normal, type Isolate 0 0 0 0 0 0
0 0 0 0 0

Number of blocks type Unmovable Movable Reclaimable
HighAtomic CMA Isolate
Node 0, zone DMA 0 1472 0 0
64 0
Node 0, zone Normal 20 1796 728 0
16 0

We could write a custom program that mmap/mlock enough memory, and then release all pages except one in each page block. What if we want to fragment also the zone DMA?

Using the driver proposed here, we could fragment the memory with this script:

# Allocate memory from Node 0/Zone Normal/Order 0/Migrate Reclaimable
echo 307200 > /sys/kernel/debug/mm/node-0/zone-Normal/order-0/migrate-Reclaimable/nr_pages_allocs for i in seq 1 307200; do \
v=$(expr $i % 1024 != 0); \
# Release everything but one page from each page block
if [ $v = 1 ]; then echo $i > /sys/kernel/debug/mm/free; echo
"Releasing $i"; fi; \
done

# Allocate memory from Node 0/Zone Normal/Order 0/Migrate Movable
echo 307200 > /sys/kernel/debug/mm/node-0/zone-Normal/order-0/migrate-Movable/nr_pages_allocs for i in seq 307201 614400; do \
v=$(expr $i % 1024 != 0); \
if [ $v = 1 ]; then echo $i > /sys/kernel/debug/mm/free; echo
"Releasing $i"; fi; \
done

After running the shell script, we have fragmented the memory;

# cat /proc/pagetypeinfo
Page block order: 9
Pages per block:  512

Free pages count per migrate type at order 0 1 2 3 4 5
6 7 8 9 10

Node 0, zone    DMA, type   Unmovable      0   0    0    0    0   0
0   0   0   0   0
Node 0, zone    DMA, type     Movable      1   1    1    0    1   1
2   2   1   3 708
Node 0, zone    DMA, type Reclaimable      0   0    0    0    0   0
0   0   0   0   0
Node 0, zone    DMA, type  HighAtomic      0   0    0    0    0   0
0   0   0   0   0
Node 0, zone    DMA, type         CMA      0   0    0    0    0   0

0 0 0 1 31
Node 0, zone DMA, type Isolate 0 0 0 0 0 0
0 0 0 0 0
Node 0, zone Normal, type Unmovable 492 445 301 31 6 3
1 0 1 1 0
Node 0, zone Normal, type Movable 387 366 357 345 341 341
329 303 291 278 559
Node 0, zone Normal, type Reclaimable 861 825 1516 1387 1176 832
473 371 361 226 10
Node 0, zone Normal, type HighAtomic 0 0 0 0 0 0
0 0 0 0 0
Node 0, zone Normal, type CMA 0 0 0 0 0 0
0 0 0 1 7
Node 0, zone Normal, type Isolate 0 0 0 0 0 0
0 0 0 0 0

Number of blocks type Unmovable Movable Reclaimable
HighAtomic CMA Isolate
Node 0, zone DMA 0 1472 0 0
64 0
Node 0, zone Normal 20 1796 728 0
16 0

If we want to do the same with Node 0/Zone DMA, we only execute:

echo 307200 > /sys/kernel/debug/mm/node-0/zone-DMA/order-0/migrate-Reclaimable/nr_pages_allocs for i in seq 614401 921600; do \
v=$(expr $i % 1024 != 0); \
if [ $v = 1 ]; then echo $i > /sys/kernel/debug/mm/free; echo
"Releasing $i"; fi; \
done

# cat /proc/pagetypeinfo
Page block order: 9
Pages per block:  512

Free pages count per migrate type at order 0 1 2 3 4 5
6 7 8 9 10
Node 0, zone DMA, type Unmovable 0 0 0 0 0 0
0 0 0 0 0
Node 0, zone DMA, type Movable 1 1 1 0 1 1
2 2 1 3 406
Node 0, zone DMA, type Reclaimable 293 293 292 292 292 293
293 293 293 292 2
Node 0, zone DMA, type HighAtomic 0 0 0 0 0 0
0 0 0 0 0

Node 0, zone    DMA, type         CMA       0    0    0    0    0   0
 0   0   0   1  31
Node 0, zone    DMA, type     Isolate       0    0    0    0    0   0

0 0 0 0 0
Node 0, zone Normal, type Unmovable 566 377 261 33 7 3
1 0 1 1 1
Node 0, zone Normal, type Movable 290 352 344 336 330 329
321 301 296 279 559
Node 0, zone Normal, type Reclaimable 1109 1024 1928 1641 1168 521
498 374 361 226 9
Node 0, zone Normal, type HighAtomic 0 0 0 0 0 0
0 0 0 0 0
Node 0, zone Normal, type CMA 0 0 0 0 0 0
0 0 0 1 7
Node 0, zone Normal, type Isolate 0 0 0 0 0 0
0 0 0 0 0

Number of blocks type Unmovable Movable Reclaimable
HighAtomic CMA Isolate
Node 0, zone DMA 0 868 604 0

     64    0
Node 0, zone Normal      22           1796       726           0
     16    0

As we can see, it is pretty straightforward to artificially reproduce the fragmentation using
a script.

Quoted 3 lines

If this proposal has legs then we should Document/ it separately - that big block comment in page_alloc_hogger.c will become unweildy.

I agree, I am happy to move it under Document/ if the tool is useful for other kernel developers.

Quoted 8 lines

One could consider plumbing this into selftests/ in some fashion, but I wouldn't encourage that - longrunning torture tests aren't appropriate for selftests, which are nice and snappy. Perhaps a new tools/testing/stresstests will one day appear.

Quoted 5 lines
Quoted 1 line

+obj-$(CONFIG_PAGEALLOC_HOGGER) += page_alloc_hogger.o

page_alloc or pagealloc. Choose only one, lest you drive people crazy for ever.

Good point, I can do that. Thanks for pointing it out.

Quoted 15 lines

Sashiko went totally nuts. Have fun with that ;) But I wouldn't do a ton of work on this until you've heard positive noises from the MM team, Guys, poke. wdyt, is there potential here?

        https://sashiko.dev/#/patchset/[email protected]

[*] Back in the days when I was trying to get redhat ext3 and 2.5.x
MM to do something other than lock up or crash, I wrote a userspace thing called "usemem". In recent times I've seen people quoting usemem results and wondered "is that my thing". So I looked it up. It is! And it's now quite unrecognizable.

Quoted 7 lines

Because, obviously, people found it useful and so they used it and added to it and added to it and more.

And I expect the same will occur with "Page Alloc Hogger" (terrible name, btw. How about "pagehog"? "memhog"), if it is adopted. People will use it and will add to it.

I agree that the name is not good. I like "pagehog".

Thanks for taking the time to review it.

Greetings
Juan

Quoted 2 lines
#19 lore raw
In reply to Juan Yescas

Hi Juan,

While I think there's possibly value in people not re-implemented their own allocation injector, I don't think there's value in having this be part of core-mm.

I really don't want this to form part of any contract or to be blessed in any way by core mm as it exposes internal implementation details and I really want to avoid adding extra maintainer workload here.

I'd suggest keeping it as an out-of-tree module as David proposed previously ([0]).

Alternatively, keeping it as part of tooling (e.g. in tools/testing) could also work.

(This is no comment on how useful it might be for debugging or testing scenarios :)

--
Cheers, Lorenzo

[0]:https://lore.kernel.org/all/[email protected]/

#20 lore raw
Quoted 10 lines · Lorenzo Stoakes (ARM)

Hi Juan,

While I think there's possibly value in people not re-implemented their own allocation injector, I don't think there's value in having this be part of core-mm.

I really don't want this to form part of any contract or to be blessed in any way by core mm as it exposes internal implementation details and I really want to avoid adding extra maintainer workload here.

Quoted 5 lines

I'd suggest keeping it as an out-of-tree module as David proposed previously ([0]).

Alternatively, keeping it as part of tooling (e.g. in tools/testing) could also work.

Yes, I would prefer if all MM-related testing modules actually go somewhere in tools/testing.

I commented on that recently when finding some of them in lib (and we also have another GUP one at least in mm). Stuff that doesn't ultimately need to be there should just be somewhere else.

Signature · 3 lines
Cheers,

David
#21 lore raw
Quoted 12 lines · David Hildenbrand (Arm)
Quoted 10 lines · Lorenzo Stoakes (ARM)

Hi Juan,

While I think there's possibly value in people not re-implemented their own allocation injector, I don't think there's value in having this be part of core-mm.

I really don't want this to form part of any contract or to be blessed in any way by core mm as it exposes internal implementation details and I really want to avoid adding extra maintainer workload here.

Quoted 8 lines
Quoted 5 lines

I'd suggest keeping it as an out-of-tree module as David proposed previously ([0]).

Alternatively, keeping it as part of tooling (e.g. in tools/testing) could also work.

Yes, I would prefer if all MM-related testing modules actually go somewhere in tools/testing.

Thanks David, apologies for the delay.

Quoted 5 lines

I commented on that recently when finding some of them in lib (and we also have another GUP one at least in mm). Stuff that doesn't ultimately need to be there should just be somewhere else.

I think that works too, if this module is useful for kernel developers, we could place it
in tools/testing.

Thanks
Juan

Quoted 4 lines

--
Cheers,

David

#22 lore raw
In reply to Lorenzo Stoakes (ARM)
Quoted 7 lines · Lorenzo Stoakes (ARM)

Hi Juan,

While I think there's possibly value in people not re-implemented their own allocation injector, I don't think there's value in having this be part of core-mm.

Thanks Lorenzo for your comments. Apologies for the delay.

Quoted 4 lines

I really don't want this to form part of any contract or to be blessed in any way by core mm as it exposes internal implementation details and I really want to avoid adding extra maintainer workload here.

I understand and completely respect your concern regarding maintainer overhead and internal boundaries.

Do you see a way to provide similar functionality without exposing the internal implementation?

Quoted 5 lines

I'd suggest keeping it as an out-of-tree module as David proposed previously ([0]).

Alternatively, keeping it as part of tooling (e.g. in tools/testing) could also work.

That would also work.

Quoted 3 lines

(This is no comment on how useful it might be for debugging or testing scenarios :)

Thanks again for your feedback
Juan

Quoted 4 lines

--
Cheers, Lorenzo

[0]:https://lore.kernel.org/all/[email protected]/

#23 lore raw
Quoted 23 lines · Juan Yescas
Quoted 7 lines · Lorenzo Stoakes (ARM)

Hi Juan,

While I think there's possibly value in people not re-implemented their own allocation injector, I don't think there's value in having this be part of core-mm.

Thanks Lorenzo for your comments. Apologies for the delay.

Quoted 4 lines

I really don't want this to form part of any contract or to be blessed in any way by core mm as it exposes internal implementation details and I really want to avoid adding extra maintainer workload here.

I understand and completely respect your concern regarding maintainer overhead and internal boundaries.

Do you see a way to provide similar functionality without exposing the internal implementation?

Quoted 7 lines
Quoted 5 lines

I'd suggest keeping it as an out-of-tree module as David proposed previously ([0]).

Alternatively, keeping it as part of tooling (e.g. in tools/testing) could also work.

That would also work.

Yeah David and I are agreed this would be the best place for it - it gives you everything you need and upstream while keeping the separation we want _and_ it allows us to write better, more thorough tests (and I see in your reply to Andrew you are planning on adding a bunch of tests which I love to hear :)

So this would be great thanks!

Quoted 12 lines
Quoted 4 lines

(This is no comment on how useful it might be for debugging or testing scenarios :)

Thanks again for your feedback
Juan

Quoted 4 lines

--
Cheers, Lorenzo

[0]:https://lore.kernel.org/all/[email protected]/

--
Cheers, Lorenzo