From: Stefan Roesch <shr@devkernel.io>
To: kernel-team@fb.com
Cc: shr@devkernel.io, akpm@linux-foundation.org, david@redhat.com,
hannes@cmpxchg.org, riel@surriel.com,
linux-kernel@vger.kernel.org, linux-mm@kvack.org
Subject: [PATCH v1 2/4] mm/ksm: add sysfs knobs for advisor
Date: Wed, 4 Oct 2023 12:02:47 -0700 [thread overview]
Message-ID: <20231004190249.829015-3-shr@devkernel.io> (raw)
In-Reply-To: <20231004190249.829015-1-shr@devkernel.io>
This adds four new knobs for the KSM advisor to influence its behaviour.
The knobs are:
- advisor_mode:
0: no advisor (default)
1: scan time advisor
- advisor_min_pages: 500 (default)
- advisor_max_pages: 5000 (default)
- advisor_target_scan_time: 200 (default in seconds)
The new values will take effect on the next scan round.
Signed-off-by: Stefan Roesch <shr@devkernel.io>
---
mm/ksm.c | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 108 insertions(+)
diff --git a/mm/ksm.c b/mm/ksm.c
index c9edfb293024..12e70f806b2b 100644
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -341,6 +341,14 @@ static void init_advisor(void)
advisor_ctx.change = 0;
}
+static void set_advisor_defaults(void)
+{
+ if (ksm_advisor == KSM_ADVISOR_NONE)
+ ksm_thread_pages_to_scan = DEFAULT_PAGES_TO_SCAN;
+ else if (ksm_advisor == KSM_ADVISOR_SCAN_TIME)
+ ksm_thread_pages_to_scan = ksm_advisor_min_pages;
+}
+
/*
* Use previous scan time if available, otherwise use current scan time as an
* approximation for the previous scan time.
@@ -3692,6 +3700,102 @@ static ssize_t smart_scan_store(struct kobject *kobj,
}
KSM_ATTR(smart_scan);
+static ssize_t advisor_mode_show(struct kobject *kobj,
+ struct kobj_attribute *attr, char *buf)
+{
+ return sysfs_emit(buf, "%u\n", ksm_advisor);
+}
+
+static ssize_t advisor_mode_store(struct kobject *kobj,
+ struct kobj_attribute *attr, const char *buf,
+ size_t count)
+{
+ unsigned int mode;
+ int err;
+
+ err = kstrtouint(buf, 10, &mode);
+ if (err)
+ return -EINVAL;
+ if (mode > KSM_ADVISOR_LAST)
+ return -EINVAL;
+
+ /* Set advisor default values */
+ ksm_advisor = mode;
+ init_advisor();
+ set_advisor_defaults();
+
+ return count;
+}
+KSM_ATTR(advisor_mode);
+
+static ssize_t advisor_min_pages_show(struct kobject *kobj,
+ struct kobj_attribute *attr, char *buf)
+{
+ return sysfs_emit(buf, "%lu\n", ksm_advisor_min_pages);
+}
+
+static ssize_t advisor_min_pages_store(struct kobject *kobj,
+ struct kobj_attribute *attr,
+ const char *buf, size_t count)
+{
+ int err;
+ unsigned long value;
+
+ err = kstrtoul(buf, 10, &value);
+ if (err)
+ return -EINVAL;
+
+ ksm_advisor_min_pages = value;
+ return count;
+}
+KSM_ATTR(advisor_min_pages);
+
+static ssize_t advisor_max_pages_show(struct kobject *kobj,
+ struct kobj_attribute *attr, char *buf)
+{
+ return sysfs_emit(buf, "%lu\n", ksm_advisor_max_pages);
+}
+
+static ssize_t advisor_max_pages_store(struct kobject *kobj,
+ struct kobj_attribute *attr,
+ const char *buf, size_t count)
+{
+ int err;
+ unsigned long value;
+
+ err = kstrtoul(buf, 10, &value);
+ if (err)
+ return -EINVAL;
+
+ ksm_advisor_max_pages = value;
+ return count;
+}
+KSM_ATTR(advisor_max_pages);
+
+static ssize_t advisor_target_scan_time_show(struct kobject *kobj,
+ struct kobj_attribute *attr, char *buf)
+{
+ return sysfs_emit(buf, "%lu\n", ksm_advisor_target_scan_time);
+}
+
+static ssize_t advisor_target_scan_time_store(struct kobject *kobj,
+ struct kobj_attribute *attr,
+ const char *buf, size_t count)
+{
+ int err;
+ unsigned long value;
+
+ err = kstrtoul(buf, 10, &value);
+ if (err)
+ return -EINVAL;
+ if (value < 1)
+ return -EINVAL;
+
+ ksm_advisor_target_scan_time = value;
+ return count;
+}
+KSM_ATTR(advisor_target_scan_time);
+
static struct attribute *ksm_attrs[] = {
&sleep_millisecs_attr.attr,
&pages_to_scan_attr.attr,
@@ -3714,6 +3818,10 @@ static struct attribute *ksm_attrs[] = {
&use_zero_pages_attr.attr,
&general_profit_attr.attr,
&smart_scan_attr.attr,
+ &advisor_mode_attr.attr,
+ &advisor_min_pages_attr.attr,
+ &advisor_max_pages_attr.attr,
+ &advisor_target_scan_time_attr.attr,
NULL,
};
--
2.39.3
next prev parent reply other threads:[~2023-10-04 19:03 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-04 19:02 [PATCH v1 0/4] mm/ksm: Add ksm advisor Stefan Roesch
2023-10-04 19:02 ` [PATCH v1 1/4] mm/ksm: add " Stefan Roesch
2023-10-04 19:02 ` Stefan Roesch [this message]
2023-10-05 17:57 ` [PATCH v1 2/4] mm/ksm: add sysfs knobs for advisor kernel test robot
2023-10-05 21:36 ` kernel test robot
2023-10-04 19:02 ` [PATCH v1 3/4] mm/ksm: add tracepoint for ksm advisor Stefan Roesch
2023-10-04 19:02 ` [PATCH v1 4/4] mm/ksm: document ksm advisor and its sysfs knobs Stefan Roesch
2023-10-06 12:01 ` [PATCH v1 0/4] mm/ksm: Add ksm advisor David Hildenbrand
2023-10-06 16:17 ` Stefan Roesch
2023-10-09 9:48 ` David Hildenbrand
2023-10-10 16:02 ` Stefan Roesch
2023-10-17 15:28 ` David Hildenbrand
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231004190249.829015-3-shr@devkernel.io \
--to=shr@devkernel.io \
--cc=akpm@linux-foundation.org \
--cc=david@redhat.com \
--cc=hannes@cmpxchg.org \
--cc=kernel-team@fb.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=riel@surriel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.