Skip to content

Commit fcf9a0e

Browse files
Kirill Tkhaitorvalds
authored andcommitted
ksm: react on changing "sleep_millisecs" parameter faster
ksm thread unconditionally sleeps in ksm_scan_thread() after each iteration: schedule_timeout_interruptible( msecs_to_jiffies(ksm_thread_sleep_millisecs)) 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> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent e0975b2 commit fcf9a0e

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

mm/ksm.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ static unsigned long ksm_run = KSM_RUN_STOP;
296296
static void wait_while_offlining(void);
297297

298298
static DECLARE_WAIT_QUEUE_HEAD(ksm_thread_wait);
299+
static DECLARE_WAIT_QUEUE_HEAD(ksm_iter_wait);
299300
static DEFINE_MUTEX(ksm_thread_mutex);
300301
static DEFINE_SPINLOCK(ksm_mmlist_lock);
301302

@@ -2388,6 +2389,8 @@ static int ksmd_should_run(void)
23882389

23892390
static int ksm_scan_thread(void *nothing)
23902391
{
2392+
unsigned int sleep_ms;
2393+
23912394
set_freezable();
23922395
set_user_nice(current, 5);
23932396

@@ -2401,8 +2404,10 @@ static int ksm_scan_thread(void *nothing)
24012404
try_to_freeze();
24022405

24032406
if (ksmd_should_run()) {
2404-
schedule_timeout_interruptible(
2405-
msecs_to_jiffies(ksm_thread_sleep_millisecs));
2407+
sleep_ms = READ_ONCE(ksm_thread_sleep_millisecs);
2408+
wait_event_interruptible_timeout(ksm_iter_wait,
2409+
sleep_ms != READ_ONCE(ksm_thread_sleep_millisecs),
2410+
msecs_to_jiffies(sleep_ms));
24062411
} else {
24072412
wait_event_freezable(ksm_thread_wait,
24082413
ksmd_should_run() || kthread_should_stop());
@@ -2821,6 +2826,7 @@ static ssize_t sleep_millisecs_store(struct kobject *kobj,
28212826
return -EINVAL;
28222827

28232828
ksm_thread_sleep_millisecs = msecs;
2829+
wake_up_interruptible(&ksm_iter_wait);
28242830

28252831
return count;
28262832
}

0 commit comments

Comments
 (0)