[RHEL7,COMMIT] ms/ksm: React on changing "sleep_millisecs" parameter faster

Submitted by Konstantin Khorenko on Dec. 12, 2018, 1:28 p.m.


Message ID 201812121328.wBCDS9Uw010208@finist-ce7.sw.ru
State New
Series "ksm: React on changing "sleep_millisecs" parameter faster"
Headers show

Commit Message

Konstantin Khorenko Dec. 12, 2018, 1:28 p.m.
The commit is pushed to "branch-rh7-3.10.0-957.1.3.vz7.83.x-ovz" and will appear at https://src.openvz.org/scm/ovz/vzkernel.git
after rh7-3.10.0-957.1.3.vz7.83.2
commit c84fa0de2fecf3e680ea7f746bf7fe63f5198f3c
Author: Kirill Tkhai <ktkhai@virtuozzo.com>
Date:   Wed Dec 12 16:28:09 2018 +0300

    ms/ksm: React on changing "sleep_millisecs" parameter faster
    ksm thread unconditionally sleeps in ksm_scan_thread()
    after each iteration:
    The timeout is configured in /sys/kernel/mm/ksm/sleep_millisecs.
    In case of user writes a big value by a mistake, and the thread
    enters into schedule_timeout_interruptible(), it's not possible
    to cancel the sleep by writing a new smaler value; the thread
    is just sleeping till timeout expires.
    The patch fixes the problem by waking the thread each time
    after the value is updated.
    This also may be useful for debug purposes; and also for userspace
    daemons, which change sleep_millisecs value in dependence of
    system load.
    Link: http://lkml.kernel.org/r/154454107680.3258.3558002210423531566.stgit@localhost.localdomain
    Signed-off-by: Kirill Tkhai <ktkhai@virtuozzo.com>
    Acked-by: Cyrill Gorcunov <gorcunov@gmail.com>
    Reviewed-by: Andrew Morton <akpm@linux-foundation.org>
    Cc: Michal Hocko <mhocko@suse.com>
    Cc: Hugh Dickins <hughd@google.com>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
 mm/ksm.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

Patch hide | download patch | download mbox

diff --git a/mm/ksm.c b/mm/ksm.c
index fe8a6e1a2940..dde691cd6a54 100644
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -269,6 +269,7 @@  static unsigned long ksm_run = KSM_RUN_STOP;
 static void wait_while_offlining(void);
 static DECLARE_WAIT_QUEUE_HEAD(ksm_thread_wait);
+static DECLARE_WAIT_QUEUE_HEAD(ksm_iter_wait);
 static DEFINE_MUTEX(ksm_thread_mutex);
 static DEFINE_SPINLOCK(ksm_mmlist_lock);
@@ -2321,6 +2322,8 @@  static int ksmd_should_run(void)
 static int ksm_scan_thread(void *nothing)
+	unsigned int sleep_ms;
 	set_user_nice(current, 5);
@@ -2334,8 +2337,10 @@  static int ksm_scan_thread(void *nothing)
 		if (ksmd_should_run()) {
-			schedule_timeout_interruptible(
-				msecs_to_jiffies(ksm_thread_sleep_millisecs));
+			sleep_ms = READ_ONCE(ksm_thread_sleep_millisecs);
+			wait_event_interruptible_timeout(ksm_iter_wait,
+				sleep_ms != READ_ONCE(ksm_thread_sleep_millisecs),
+				msecs_to_jiffies(sleep_ms));
 		} else {
 				ksmd_should_run() || kthread_should_stop());
@@ -2749,6 +2754,7 @@  static ssize_t sleep_millisecs_store(struct kobject *kobj,
 		return -EINVAL;
 	ksm_thread_sleep_millisecs = msecs;
+	wake_up_interruptible(&ksm_iter_wait);
 	return count;