linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm: ksm: Remove unnecessary try_to_freeze()
@ 2023-12-13  9:09 Kevin Hao
  2023-12-13 11:17 ` Rafael J. Wysocki
  2023-12-13 11:37 ` David Hildenbrand
  0 siblings, 2 replies; 5+ messages in thread
From: Kevin Hao @ 2023-12-13  9:09 UTC (permalink / raw)
  To: linux-mm; +Cc: Andrea Arcangeli, Andrew Morton, Rafael J. Wysocki, Pavel Machek

A freezable kernel thread can enter frozen state during freezing by
either calling try_to_freeze() or using wait_event_freezable() and its
variants. However, there is no need to use both methods simultaneously.

Signed-off-by: Kevin Hao <haokexin@gmail.com>
Cc: Andrea Arcangeli <aarcange@redhat.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: "Rafael J. Wysocki" <rafael@kernel.org>
Cc: Pavel Machek <pavel@ucw.cz>
---
 mm/ksm.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/mm/ksm.c b/mm/ksm.c
index 16532fa85a46..e2ce850c2739 100644
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -2604,11 +2604,9 @@ static int ksm_scan_thread(void *nothing)
 			ksm_do_scan(ksm_thread_pages_to_scan);
 		mutex_unlock(&ksm_thread_mutex);
 
-		try_to_freeze();
-
 		if (ksmd_should_run()) {
 			sleep_ms = READ_ONCE(ksm_thread_sleep_millisecs);
-			wait_event_interruptible_timeout(ksm_iter_wait,
+			wait_event_freezable_timeout(ksm_iter_wait,
 				sleep_ms != READ_ONCE(ksm_thread_sleep_millisecs),
 				msecs_to_jiffies(sleep_ms));
 		} else {
-- 
2.39.2



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

* Re: [PATCH] mm: ksm: Remove unnecessary try_to_freeze()
  2023-12-13  9:09 [PATCH] mm: ksm: Remove unnecessary try_to_freeze() Kevin Hao
@ 2023-12-13 11:17 ` Rafael J. Wysocki
  2023-12-13 11:28   ` David Hildenbrand
  2023-12-13 11:37 ` David Hildenbrand
  1 sibling, 1 reply; 5+ messages in thread
From: Rafael J. Wysocki @ 2023-12-13 11:17 UTC (permalink / raw)
  To: Kevin Hao
  Cc: linux-mm, Andrea Arcangeli, Andrew Morton, Rafael J. Wysocki,
	Pavel Machek

On Wed, Dec 13, 2023 at 10:09 AM Kevin Hao <haokexin@gmail.com> wrote:
>
> A freezable kernel thread can enter frozen state during freezing by
> either calling try_to_freeze() or using wait_event_freezable() and its
> variants. However, there is no need to use both methods simultaneously.

The changelog doesn't match the patch IMV.

> Signed-off-by: Kevin Hao <haokexin@gmail.com>
> Cc: Andrea Arcangeli <aarcange@redhat.com>
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: "Rafael J. Wysocki" <rafael@kernel.org>
> Cc: Pavel Machek <pavel@ucw.cz>
> ---
>  mm/ksm.c | 4 +---
>  1 file changed, 1 insertion(+), 3 deletions(-)
>
> diff --git a/mm/ksm.c b/mm/ksm.c
> index 16532fa85a46..e2ce850c2739 100644
> --- a/mm/ksm.c
> +++ b/mm/ksm.c
> @@ -2604,11 +2604,9 @@ static int ksm_scan_thread(void *nothing)
>                         ksm_do_scan(ksm_thread_pages_to_scan);
>                 mutex_unlock(&ksm_thread_mutex);
>
> -               try_to_freeze();
> -
>                 if (ksmd_should_run()) {
>                         sleep_ms = READ_ONCE(ksm_thread_sleep_millisecs);
> -                       wait_event_interruptible_timeout(ksm_iter_wait,
> +                       wait_event_freezable_timeout(ksm_iter_wait,
>                                 sleep_ms != READ_ONCE(ksm_thread_sleep_millisecs),
>                                 msecs_to_jiffies(sleep_ms));
>                 } else {
> --
> 2.39.2
>


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

* Re: [PATCH] mm: ksm: Remove unnecessary try_to_freeze()
  2023-12-13 11:17 ` Rafael J. Wysocki
@ 2023-12-13 11:28   ` David Hildenbrand
  2023-12-13 11:35     ` Kevin Hao
  0 siblings, 1 reply; 5+ messages in thread
From: David Hildenbrand @ 2023-12-13 11:28 UTC (permalink / raw)
  To: Rafael J. Wysocki, Kevin Hao
  Cc: linux-mm, Andrea Arcangeli, Andrew Morton, Pavel Machek

On 13.12.23 12:17, Rafael J. Wysocki wrote:
> On Wed, Dec 13, 2023 at 10:09 AM Kevin Hao <haokexin@gmail.com> wrote:
>>
>> A freezable kernel thread can enter frozen state during freezing by
>> either calling try_to_freeze() or using wait_event_freezable() and its
>> variants. However, there is no need to use both methods simultaneously.
> 
> The changelog doesn't match the patch IMV.
> 

Staring at the code, I think it makes sense once one spots the existing 
"wait_event_freezable()" on the else path.

-- 
Cheers,

David / dhildenb



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

* Re: [PATCH] mm: ksm: Remove unnecessary try_to_freeze()
  2023-12-13 11:28   ` David Hildenbrand
@ 2023-12-13 11:35     ` Kevin Hao
  0 siblings, 0 replies; 5+ messages in thread
From: Kevin Hao @ 2023-12-13 11:35 UTC (permalink / raw)
  To: David Hildenbrand
  Cc: Rafael J. Wysocki, linux-mm, Andrea Arcangeli, Andrew Morton,
	Pavel Machek

[-- Attachment #1: Type: text/plain, Size: 1549 bytes --]

On Wed, Dec 13, 2023 at 12:28:21PM +0100, David Hildenbrand wrote:
> On 13.12.23 12:17, Rafael J. Wysocki wrote:
> > On Wed, Dec 13, 2023 at 10:09 AM Kevin Hao <haokexin@gmail.com> wrote:
> > > 
> > > A freezable kernel thread can enter frozen state during freezing by
> > > either calling try_to_freeze() or using wait_event_freezable() and its
> > > variants. However, there is no need to use both methods simultaneously.
> > 
> > The changelog doesn't match the patch IMV.
> > 
> 
> Staring at the code, I think it makes sense once one spots the existing
> "wait_event_freezable()" on the else path.

Yes. The change from wait_event_interruptible_timeout() to
wait_event_freezable_timeout() led to some confusion. In the else path, there
is indeed a wait_event_freezable(). The complete code is as follows:

static int ksm_scan_thread(void *nothing)
{
	unsigned int sleep_ms;

	set_freezable();
	set_user_nice(current, 5);

	while (!kthread_should_stop()) {
		mutex_lock(&ksm_thread_mutex);
		wait_while_offlining();
		if (ksmd_should_run())
			ksm_do_scan(ksm_thread_pages_to_scan);
		mutex_unlock(&ksm_thread_mutex);

		try_to_freeze();

		if (ksmd_should_run()) {
			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());
		}
	}
	return 0;
}

Thanks,
Kevin

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

* Re: [PATCH] mm: ksm: Remove unnecessary try_to_freeze()
  2023-12-13  9:09 [PATCH] mm: ksm: Remove unnecessary try_to_freeze() Kevin Hao
  2023-12-13 11:17 ` Rafael J. Wysocki
@ 2023-12-13 11:37 ` David Hildenbrand
  1 sibling, 0 replies; 5+ messages in thread
From: David Hildenbrand @ 2023-12-13 11:37 UTC (permalink / raw)
  To: Kevin Hao, linux-mm
  Cc: Andrea Arcangeli, Andrew Morton, Rafael J. Wysocki, Pavel Machek

On 13.12.23 10:09, Kevin Hao wrote:
> A freezable kernel thread can enter frozen state during freezing by
> either calling try_to_freeze() or using wait_event_freezable() and its
> variants. However, there is no need to use both methods simultaneously.
> 
> Signed-off-by: Kevin Hao <haokexin@gmail.com>
> Cc: Andrea Arcangeli <aarcange@redhat.com>
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: "Rafael J. Wysocki" <rafael@kernel.org>
> Cc: Pavel Machek <pavel@ucw.cz>
> ---
>   mm/ksm.c | 4 +---
>   1 file changed, 1 insertion(+), 3 deletions(-)
> 
> diff --git a/mm/ksm.c b/mm/ksm.c
> index 16532fa85a46..e2ce850c2739 100644
> --- a/mm/ksm.c
> +++ b/mm/ksm.c
> @@ -2604,11 +2604,9 @@ static int ksm_scan_thread(void *nothing)
>   			ksm_do_scan(ksm_thread_pages_to_scan);
>   		mutex_unlock(&ksm_thread_mutex);
>   
> -		try_to_freeze();
> -
>   		if (ksmd_should_run()) {
>   			sleep_ms = READ_ONCE(ksm_thread_sleep_millisecs);
> -			wait_event_interruptible_timeout(ksm_iter_wait,
> +			wait_event_freezable_timeout(ksm_iter_wait,
>   				sleep_ms != READ_ONCE(ksm_thread_sleep_millisecs),
>   				msecs_to_jiffies(sleep_ms));
>   		} else {

I'm not an expert on task freezing, but staring at 
Documentation/power/freezing-of-tasks.txt

"freezable kernel threads need to call it
explicitly in suitable places or use the wait_event_freezable() or
wait_event_freezable_timeout() macros (defined in 
include/linux/freezer.h) that combine interruptible sleep with checking 
if the task is to be frozen and calling try_to_freeze()."

So this cleanup makes sense to me.

Acked-by: David Hildenbrand <david@redhat.com>

-- 
Cheers,

David / dhildenb



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

end of thread, other threads:[~2023-12-13 11:37 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-13  9:09 [PATCH] mm: ksm: Remove unnecessary try_to_freeze() Kevin Hao
2023-12-13 11:17 ` Rafael J. Wysocki
2023-12-13 11:28   ` David Hildenbrand
2023-12-13 11:35     ` Kevin Hao
2023-12-13 11:37 ` David Hildenbrand

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox