linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] ksm: React on changing "sleep_millisecs" parameter faster
@ 2018-12-10 16:06 Kirill Tkhai
  2018-12-10 20:10 ` Cyrill Gorcunov
  0 siblings, 1 reply; 11+ messages in thread
From: Kirill Tkhai @ 2018-12-10 16:06 UTC (permalink / raw)
  To: akpm, mhocko, ktkhai, linux-mm, linux-kernel, gorcunov

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.

Signed-off-by: Kirill Tkhai <ktkhai@virtuozzo.com>
---
 mm/ksm.c |   11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/mm/ksm.c b/mm/ksm.c
index 723bd32d4dd0..31452122e52b 100644
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -294,6 +294,7 @@ static int ksm_nr_node_ids = 1;
 #define KSM_RUN_OFFLINE	4
 static unsigned long ksm_run = KSM_RUN_STOP;
 static void wait_while_offlining(void);
+static struct task_struct *ksm_task = NULL;
 
 static DECLARE_WAIT_QUEUE_HEAD(ksm_thread_wait);
 static DEFINE_MUTEX(ksm_thread_mutex);
@@ -2435,8 +2436,9 @@ static int ksm_scan_thread(void *nothing)
 	set_freezable();
 	set_user_nice(current, 5);
 
+	mutex_lock(&ksm_thread_mutex);
+	ksm_task = current;
 	while (!kthread_should_stop()) {
-		mutex_lock(&ksm_thread_mutex);
 		wait_while_offlining();
 		if (ksmd_should_run())
 			ksm_do_scan(ksm_thread_pages_to_scan);
@@ -2451,7 +2453,10 @@ static int ksm_scan_thread(void *nothing)
 			wait_event_freezable(ksm_thread_wait,
 				ksmd_should_run() || kthread_should_stop());
 		}
+		mutex_lock(&ksm_thread_mutex);
 	}
+	ksm_task = NULL;
+	mutex_unlock(&ksm_thread_mutex);
 	return 0;
 }
 
@@ -2864,7 +2869,11 @@ static ssize_t sleep_millisecs_store(struct kobject *kobj,
 	if (err || msecs > UINT_MAX)
 		return -EINVAL;
 
+	mutex_lock(&ksm_thread_mutex);
+	if (ksm_task)
+		wake_up_state(ksm_task, TASK_INTERRUPTIBLE);
 	ksm_thread_sleep_millisecs = msecs;
+	mutex_unlock(&ksm_thread_mutex);
 
 	return count;
 }


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH] ksm: React on changing "sleep_millisecs" parameter faster
  2018-12-10 16:06 [PATCH] ksm: React on changing "sleep_millisecs" parameter faster Kirill Tkhai
@ 2018-12-10 20:10 ` Cyrill Gorcunov
  2018-12-11  9:23   ` Kirill Tkhai
  0 siblings, 1 reply; 11+ messages in thread
From: Cyrill Gorcunov @ 2018-12-10 20:10 UTC (permalink / raw)
  To: Kirill Tkhai; +Cc: akpm, mhocko, linux-mm, linux-kernel, gorcunov

On Mon, Dec 10, 2018 at 07:06:18PM +0300, Kirill Tkhai wrote:
> 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.
> 
> Signed-off-by: Kirill Tkhai <ktkhai@virtuozzo.com>

Kirill, can we rather reuse @ksm_thread variable from ksm_init
(by moving it to static file level variable). Also wakening up
unconditionally on write looks somehow suspicious to me
though I don't have a precise argument against.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] ksm: React on changing "sleep_millisecs" parameter faster
  2018-12-10 20:10 ` Cyrill Gorcunov
@ 2018-12-11  9:23   ` Kirill Tkhai
  2018-12-11 10:03     ` Cyrill Gorcunov
  0 siblings, 1 reply; 11+ messages in thread
From: Kirill Tkhai @ 2018-12-11  9:23 UTC (permalink / raw)
  To: Cyrill Gorcunov; +Cc: akpm, mhocko, linux-mm, linux-kernel, gorcunov

Hi, Cyrill,

On 10.12.2018 23:10, Cyrill Gorcunov wrote:
> On Mon, Dec 10, 2018 at 07:06:18PM +0300, Kirill Tkhai wrote:
>> 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.
>>
>> Signed-off-by: Kirill Tkhai <ktkhai@virtuozzo.com>
> 
> Kirill, can we rather reuse @ksm_thread variable from ksm_init
> (by moving it to static file level variable).

I've considered using it, but this is not looks good for me.
The problem is ksm thread may be parked, or it even may fail
to start. But at the same time, parallel writes to "sleep_millisecs"
are possible. There is a place for races, so to use the local
variable in ksm_init() (like we have at the moment) looks better
for me. At the patch the mutex protects against any races.

> Also wakening up
> unconditionally on write looks somehow suspicious to me
> though I don't have a precise argument against.

The conditional wait requires one more wait_queue. This is
the thing I tried to avoid. But. I also had doubts about
this, so you are already the second person, who worries :)
It looks like we really need to change this.

How are you about something like the below?

diff --git a/mm/ksm.c b/mm/ksm.c
index 723bd32d4dd0..66d962a227e7 100644
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -296,6 +296,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);
 
@@ -2432,6 +2433,8 @@ static int ksmd_should_run(void)
 
 static int ksm_scan_thread(void *nothing)
 {
+	unsigned int sleep_ms;
+
 	set_freezable();
 	set_user_nice(current, 5);
 
@@ -2440,13 +2443,15 @@ static int ksm_scan_thread(void *nothing)
 		wait_while_offlining();
 		if (ksmd_should_run())
 			ksm_do_scan(ksm_thread_pages_to_scan);
+		sleep_ms = ksm_thread_sleep_millisecs;
 		mutex_unlock(&ksm_thread_mutex);
 
 		try_to_freeze();
 
 		if (ksmd_should_run()) {
-			schedule_timeout_interruptible(
-				msecs_to_jiffies(ksm_thread_sleep_millisecs));
+			wait_event_interruptible_timeout(ksm_iter_wait,
+					sleep_ms != ksm_thread_sleep_millisecs,
+					msecs_to_jiffies(sleep_ms));
 		} else {
 			wait_event_freezable(ksm_thread_wait,
 				ksmd_should_run() || kthread_should_stop());
@@ -2864,7 +2869,10 @@ static ssize_t sleep_millisecs_store(struct kobject *kobj,
 	if (err || msecs > UINT_MAX)
 		return -EINVAL;
 
+	mutex_lock(&ksm_thread_mutex);
 	ksm_thread_sleep_millisecs = msecs;
+	mutex_unlock(&ksm_thread_mutex);
+	wake_up_interruptible(&ksm_iter_wait);
 
 	return count;
 }

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH] ksm: React on changing "sleep_millisecs" parameter faster
  2018-12-11  9:23   ` Kirill Tkhai
@ 2018-12-11 10:03     ` Cyrill Gorcunov
  2018-12-11 10:26       ` [PATCH v2] " Kirill Tkhai
  0 siblings, 1 reply; 11+ messages in thread
From: Cyrill Gorcunov @ 2018-12-11 10:03 UTC (permalink / raw)
  To: Kirill Tkhai; +Cc: akpm, mhocko, linux-mm, linux-kernel, gorcunov

On Tue, Dec 11, 2018 at 12:23:11PM +0300, Kirill Tkhai wrote:
...
> > Kirill, can we rather reuse @ksm_thread variable from ksm_init
> > (by moving it to static file level variable).
> 
> I've considered using it, but this is not looks good for me.
> The problem is ksm thread may be parked, or it even may fail
> to start. But at the same time, parallel writes to "sleep_millisecs"
> are possible. There is a place for races, so to use the local
> variable in ksm_init() (like we have at the moment) looks better
> for me. At the patch the mutex protects against any races.
> 
> > Also wakening up
> > unconditionally on write looks somehow suspicious to me
> > though I don't have a precise argument against.
> 
> The conditional wait requires one more wait_queue. This is
> the thing I tried to avoid. But. I also had doubts about
> this, so you are already the second person, who worries :)
> It looks like we really need to change this.
> 
> How are you about something like the below?

I see. The code below looks a way better for me, thanks!

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v2] ksm: React on changing "sleep_millisecs" parameter faster
  2018-12-11 10:03     ` Cyrill Gorcunov
@ 2018-12-11 10:26       ` Kirill Tkhai
  2018-12-11 11:13         ` Cyrill Gorcunov
  0 siblings, 1 reply; 11+ messages in thread
From: Kirill Tkhai @ 2018-12-11 10:26 UTC (permalink / raw)
  To: akpm, mhocko, ktkhai, linux-mm, linux-kernel, gorcunov

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.

Signed-off-by: Kirill Tkhai <ktkhai@virtuozzo.com>

v2: Use wait_event_interruptible_timeout() instead of unconditional
    schedule_timeout().
---
 mm/ksm.c |   12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/mm/ksm.c b/mm/ksm.c
index daa14db01e4d..b8e0b8413d51 100644
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -296,6 +296,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);
 
@@ -2412,6 +2413,8 @@ static int ksmd_should_run(void)
 
 static int ksm_scan_thread(void *nothing)
 {
+	unsigned int sleep_ms;
+
 	set_freezable();
 	set_user_nice(current, 5);
 
@@ -2420,13 +2423,15 @@ static int ksm_scan_thread(void *nothing)
 		wait_while_offlining();
 		if (ksmd_should_run())
 			ksm_do_scan(ksm_thread_pages_to_scan);
+		sleep_ms = ksm_thread_sleep_millisecs;
 		mutex_unlock(&ksm_thread_mutex);
 
 		try_to_freeze();
 
 		if (ksmd_should_run()) {
-			schedule_timeout_interruptible(
-				msecs_to_jiffies(ksm_thread_sleep_millisecs));
+			wait_event_interruptible_timeout(ksm_iter_wait,
+					sleep_ms != ksm_thread_sleep_millisecs,
+					msecs_to_jiffies(sleep_ms));
 		} else {
 			wait_event_freezable(ksm_thread_wait,
 				ksmd_should_run() || kthread_should_stop());
@@ -2844,7 +2849,10 @@ static ssize_t sleep_millisecs_store(struct kobject *kobj,
 	if (err || msecs > UINT_MAX)
 		return -EINVAL;
 
+	mutex_lock(&ksm_thread_mutex);
 	ksm_thread_sleep_millisecs = msecs;
+	mutex_unlock(&ksm_thread_mutex);
+	wake_up_interruptible(&ksm_iter_wait);
 
 	return count;
 }


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH v2] ksm: React on changing "sleep_millisecs" parameter faster
  2018-12-11 10:26       ` [PATCH v2] " Kirill Tkhai
@ 2018-12-11 11:13         ` Cyrill Gorcunov
  2018-12-11 12:22           ` Kirill Tkhai
  0 siblings, 1 reply; 11+ messages in thread
From: Cyrill Gorcunov @ 2018-12-11 11:13 UTC (permalink / raw)
  To: Kirill Tkhai; +Cc: akpm, mhocko, linux-mm, linux-kernel, gorcunov

On Tue, Dec 11, 2018 at 01:26:59PM +0300, Kirill Tkhai wrote:
> 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.
> 
> Signed-off-by: Kirill Tkhai <ktkhai@virtuozzo.com>
> 
> v2: Use wait_event_interruptible_timeout() instead of unconditional
>     schedule_timeout().
...
> @@ -2844,7 +2849,10 @@ static ssize_t sleep_millisecs_store(struct kobject *kobj,
>  	if (err || msecs > UINT_MAX)
>  		return -EINVAL;
>  
> +	mutex_lock(&ksm_thread_mutex);
>  	ksm_thread_sleep_millisecs = msecs;
> +	mutex_unlock(&ksm_thread_mutex);
> +	wake_up_interruptible(&ksm_iter_wait);

Btw, just thought -- if we start using this mutex here don't we
open a window for force attack on the thread self execution,
iow if there gonna be a million of writers do we have a guarantee
thread ksm_scan_thread will grab the mutex earlier than writers
(or somewhere inbetween)?

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2] ksm: React on changing "sleep_millisecs" parameter faster
  2018-12-11 11:13         ` Cyrill Gorcunov
@ 2018-12-11 12:22           ` Kirill Tkhai
  2018-12-11 12:34             ` Cyrill Gorcunov
  0 siblings, 1 reply; 11+ messages in thread
From: Kirill Tkhai @ 2018-12-11 12:22 UTC (permalink / raw)
  To: Cyrill Gorcunov; +Cc: akpm, mhocko, linux-mm, linux-kernel, gorcunov

On 11.12.2018 14:13, Cyrill Gorcunov wrote:
> On Tue, Dec 11, 2018 at 01:26:59PM +0300, Kirill Tkhai wrote:
>> 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.
>>
>> Signed-off-by: Kirill Tkhai <ktkhai@virtuozzo.com>
>>
>> v2: Use wait_event_interruptible_timeout() instead of unconditional
>>     schedule_timeout().
> ...
>> @@ -2844,7 +2849,10 @@ static ssize_t sleep_millisecs_store(struct kobject *kobj,
>>  	if (err || msecs > UINT_MAX)
>>  		return -EINVAL;
>>  
>> +	mutex_lock(&ksm_thread_mutex);
>>  	ksm_thread_sleep_millisecs = msecs;
>> +	mutex_unlock(&ksm_thread_mutex);
>> +	wake_up_interruptible(&ksm_iter_wait);
> 
> Btw, just thought -- if we start using this mutex here don't we
> open a window for force attack on the thread self execution,
> iow if there gonna be a million of writers do we have a guarantee
> thread ksm_scan_thread will grab the mutex earlier than writers
> (or somewhere inbetween)?

This file is permitted for global root only. I don't think there is
a problem.

If someone wants to make ksm helpless, a person may just write a big
"sleep_millisecs" value. KSM thread won't be executed almost all the time
in this case.

Kirill

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2] ksm: React on changing "sleep_millisecs" parameter faster
  2018-12-11 12:22           ` Kirill Tkhai
@ 2018-12-11 12:34             ` Cyrill Gorcunov
  2018-12-11 14:15               ` Kirill Tkhai
  0 siblings, 1 reply; 11+ messages in thread
From: Cyrill Gorcunov @ 2018-12-11 12:34 UTC (permalink / raw)
  To: Kirill Tkhai; +Cc: akpm, mhocko, linux-mm, linux-kernel, gorcunov

On Tue, Dec 11, 2018 at 03:22:42PM +0300, Kirill Tkhai wrote:
> On 11.12.2018 14:13, Cyrill Gorcunov wrote:
> > On Tue, Dec 11, 2018 at 01:26:59PM +0300, Kirill Tkhai wrote:
> >> 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.
> >>
> >> Signed-off-by: Kirill Tkhai <ktkhai@virtuozzo.com>
> >>
> >> v2: Use wait_event_interruptible_timeout() instead of unconditional
> >>     schedule_timeout().
> > ...
> >> @@ -2844,7 +2849,10 @@ static ssize_t sleep_millisecs_store(struct kobject *kobj,
> >>  	if (err || msecs > UINT_MAX)
> >>  		return -EINVAL;
> >>  
> >> +	mutex_lock(&ksm_thread_mutex);
> >>  	ksm_thread_sleep_millisecs = msecs;
> >> +	mutex_unlock(&ksm_thread_mutex);
> >> +	wake_up_interruptible(&ksm_iter_wait);
> > 
> > Btw, just thought -- if we start using this mutex here don't we
> > open a window for force attack on the thread self execution,
> > iow if there gonna be a million of writers do we have a guarantee
> > thread ksm_scan_thread will grab the mutex earlier than writers
> > (or somewhere inbetween)?
> 
> This file is permitted for global root only. I don't think there is
> a problem.
> 
> If someone wants to make ksm helpless, a person may just write a big
> "sleep_millisecs" value. KSM thread won't be executed almost all the time
> in this case.

True. Still I think if we can leave without taking a lock it a rule of thumb.
Something like

	if (msecs != ksm_thread_sleep_millisecs)
		wake_up_interruptable(&ksm_iter_wait);

Thoughts?

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2] ksm: React on changing "sleep_millisecs" parameter faster
  2018-12-11 12:34             ` Cyrill Gorcunov
@ 2018-12-11 14:15               ` Kirill Tkhai
  2018-12-11 15:11                 ` [PATCH v3] " Kirill Tkhai
  0 siblings, 1 reply; 11+ messages in thread
From: Kirill Tkhai @ 2018-12-11 14:15 UTC (permalink / raw)
  To: Cyrill Gorcunov; +Cc: akpm, mhocko, linux-mm, linux-kernel, gorcunov

On 11.12.2018 15:34, Cyrill Gorcunov wrote:
> On Tue, Dec 11, 2018 at 03:22:42PM +0300, Kirill Tkhai wrote:
>> On 11.12.2018 14:13, Cyrill Gorcunov wrote:
>>> On Tue, Dec 11, 2018 at 01:26:59PM +0300, Kirill Tkhai wrote:
>>>> 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.
>>>>
>>>> Signed-off-by: Kirill Tkhai <ktkhai@virtuozzo.com>
>>>>
>>>> v2: Use wait_event_interruptible_timeout() instead of unconditional
>>>>     schedule_timeout().
>>> ...
>>>> @@ -2844,7 +2849,10 @@ static ssize_t sleep_millisecs_store(struct kobject *kobj,
>>>>  	if (err || msecs > UINT_MAX)
>>>>  		return -EINVAL;
>>>>  
>>>> +	mutex_lock(&ksm_thread_mutex);
>>>>  	ksm_thread_sleep_millisecs = msecs;
>>>> +	mutex_unlock(&ksm_thread_mutex);
>>>> +	wake_up_interruptible(&ksm_iter_wait);
>>>
>>> Btw, just thought -- if we start using this mutex here don't we
>>> open a window for force attack on the thread self execution,
>>> iow if there gonna be a million of writers do we have a guarantee
>>> thread ksm_scan_thread will grab the mutex earlier than writers
>>> (or somewhere inbetween)?
>>
>> This file is permitted for global root only. I don't think there is
>> a problem.
>>
>> If someone wants to make ksm helpless, a person may just write a big
>> "sleep_millisecs" value. KSM thread won't be executed almost all the time
>> in this case.
> 
> True. Still I think if we can leave without taking a lock it a rule of thumb.
> Something like
> 
> 	if (msecs != ksm_thread_sleep_millisecs)
> 		wake_up_interruptable(&ksm_iter_wait);
> 
> Thoughts?

Ok, good idea.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v3] ksm: React on changing "sleep_millisecs" parameter faster
  2018-12-11 14:15               ` Kirill Tkhai
@ 2018-12-11 15:11                 ` Kirill Tkhai
  2018-12-11 15:19                   ` Cyrill Gorcunov
  0 siblings, 1 reply; 11+ messages in thread
From: Kirill Tkhai @ 2018-12-11 15:11 UTC (permalink / raw)
  To: akpm, mhocko, ktkhai, linux-mm, linux-kernel, gorcunov

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.

Signed-off-by: Kirill Tkhai <ktkhai@virtuozzo.com>

v3: Do not use mutex: to acquire it may take much time in case long
    list of ksm'able mm and pages.
v2: Use wait_event_interruptible_timeout() instead of unconditional
    schedule_timeout().
---
 mm/ksm.c |   10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/mm/ksm.c b/mm/ksm.c
index daa14db01e4d..383f961e577a 100644
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -296,6 +296,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);
 
@@ -2412,6 +2413,8 @@ static int ksmd_should_run(void)
 
 static int ksm_scan_thread(void *nothing)
 {
+	unsigned int sleep_ms;
+
 	set_freezable();
 	set_user_nice(current, 5);
 
@@ -2425,8 +2428,10 @@ static int ksm_scan_thread(void *nothing)
 		try_to_freeze();
 
 		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 {
 			wait_event_freezable(ksm_thread_wait,
 				ksmd_should_run() || kthread_should_stop());
@@ -2845,6 +2850,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;
 }


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH v3] ksm: React on changing "sleep_millisecs" parameter faster
  2018-12-11 15:11                 ` [PATCH v3] " Kirill Tkhai
@ 2018-12-11 15:19                   ` Cyrill Gorcunov
  0 siblings, 0 replies; 11+ messages in thread
From: Cyrill Gorcunov @ 2018-12-11 15:19 UTC (permalink / raw)
  To: Kirill Tkhai; +Cc: akpm, mhocko, linux-mm, linux-kernel, gorcunov

On Tue, Dec 11, 2018 at 06:11:25PM +0300, Kirill Tkhai wrote:
> 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.
> 
> Signed-off-by: Kirill Tkhai <ktkhai@virtuozzo.com>
> 
> v3: Do not use mutex: to acquire it may take much time in case long
>     list of ksm'able mm and pages.
> v2: Use wait_event_interruptible_timeout() instead of unconditional
>     schedule_timeout().
Looks ok to me, thanks!

Acked-by: Cyrill Gorcunov <gorcunov@gmail.com>

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2018-12-11 15:20 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-12-10 16:06 [PATCH] ksm: React on changing "sleep_millisecs" parameter faster Kirill Tkhai
2018-12-10 20:10 ` Cyrill Gorcunov
2018-12-11  9:23   ` Kirill Tkhai
2018-12-11 10:03     ` Cyrill Gorcunov
2018-12-11 10:26       ` [PATCH v2] " Kirill Tkhai
2018-12-11 11:13         ` Cyrill Gorcunov
2018-12-11 12:22           ` Kirill Tkhai
2018-12-11 12:34             ` Cyrill Gorcunov
2018-12-11 14:15               ` Kirill Tkhai
2018-12-11 15:11                 ` [PATCH v3] " Kirill Tkhai
2018-12-11 15:19                   ` Cyrill Gorcunov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).