[PATCHv9 04/14] mm/page_alloc: Add sysfs handle to accept accept_memory

Kirill A. Shutemov
2023-03-30T11:50:46Z

Write amount of memory to accept into the new sysfs handle
/sys/kernel/mm/page_alloc/accept_memory.

Write 'all' to the handle to accept all memory in the system.

It can be used to implement background memory accepting from userspace.
It is also useful for debugging.

Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
---
mm/page_alloc.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 64 insertions(+)

diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index 509a93b7e5af..07e16e9b49c4 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -7343,6 +7343,45 @@ static bool __free_unaccepted(struct page *page)
return true;
}

+static ssize_t accept_memory_store(struct kobject *kobj,
+ struct kobj_attribute *attr,
+ const char *buf, size_t count)
+{
+ unsigned long to_accept = 0;
+ struct zone *zone;
+ char *retptr;
+
+ if (sysfs_streq(buf, "all")) {
+ to_accept = ULONG_MAX;
+ } else {
+ to_accept = memparse(buf, &retptr);
+
+ /* Get rid of trailing whitespace, including '\n' */
+ retptr = skip_spaces(retptr);
+
+ if (*retptr != 0 || to_accept == 0)
+ return -EINVAL;
+ }
+
+ for_each_populated_zone(zone) {
+ while (try_to_accept_memory_one(zone)) {
+ if (to_accept <= PAGE_SIZE << MAX_ORDER)
+ return count;
+
+ to_accept -= PAGE_SIZE << MAX_ORDER;
+ }
+ }
+
+ return count;
+}
+
+static struct kobj_attribute accept_memory_attr = __ATTR_WO(accept_memory);
+
+static struct attribute *page_alloc_attr[] = {
+ &accept_memory_attr.attr,
+ NULL
+};
+
#else

static bool page_contains_unaccepted(struct page *page, unsigned int order)
@@ -7366,3 +7405,28 @@ static bool __free_unaccepted(struct page *page)
}

#endif /* CONFIG_UNACCEPTED_MEMORY */
+
+static const struct attribute_group page_alloc_attr_group = {
+#ifdef CONFIG_UNACCEPTED_MEMORY
+ .attrs = page_alloc_attr,
+#endif
+};
+
+static int __init page_alloc_init_sysfs(void)
+{
+ struct kobject *page_alloc_kobj;
+ int err;
+
+ page_alloc_kobj = kobject_create_and_add("page_alloc", mm_kobj);
+ if (!page_alloc_kobj)
+ return -ENOMEM;
+
+ err = sysfs_create_group(page_alloc_kobj, &page_alloc_attr_group);
+ if (err) {
+ kobject_put(page_alloc_kobj);
+ return err;
+ }
+
+ return 0;
+}
+late_initcall(page_alloc_init_sysfs);
--
2.39.2