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 <[email protected]>
---
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
On 3/30/23 13:49, Kirill A. Shutemov wrote:
> 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 <[email protected]>
Somewhat similarly to patch 3, I'd think we don't need this patch in
mainline without clear usecases first, although it's good to post for
testing/debugging.
> ---
> 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);
On Mon, Apr 03, 2023 at 03:43:32PM +0200, Vlastimil Babka wrote:
> On 3/30/23 13:49, Kirill A. Shutemov wrote:
> > 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 <[email protected]>
>
> Somewhat similarly to patch 3, I'd think we don't need this patch in
> mainline without clear usecases first, although it's good to post for
> testing/debugging.
I thought about it as a way to implement #3 from 02/14 commit message:
users who want to accept all memory in background can use the mechanism.
But, again, we can leave out until later.
--
Kiryl Shutsemau / Kirill A. Shutemov