[RFC PATCH v2 15/16] mm: Create the "free" file to release the previously allocated pages
From: Juan Yescas
Date: Wed Aug 05 2026 - 21:18:42 EST
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
Signed-off-by: Juan Yescas <jyescas@xxxxxxxxxx>
---
Changes in v2:
- Set the file permissions to 0200.
mm/page_alloc_hogger.c | 40 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 40 insertions(+)
diff --git a/mm/page_alloc_hogger.c b/mm/page_alloc_hogger.c
index ce53a17ac790..6a6fbb78513a 100644
--- a/mm/page_alloc_hogger.c
+++ b/mm/page_alloc_hogger.c
@@ -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:
--
2.55.0.629.g250fe7f194-goog