Skip to contentSkip to conversation

mm: ksm: Remove unnecessary try_to_freeze()

PATCH Applied Kevin Hao 3 participants 5 messages Dec 13, 2023

Subsystems (2)
lore
Share & tools
lore

Kevin Hao

author
#0 lore raw

PATCH 1/1 mm: ksm: Remove unnecessary try_to_freeze()

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.

Cc Andrea Arcangeli <[email protected]>
Cc Andrew Morton <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>
Cc Pavel Machek <[email protected]>
---
 mm/ksm.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
mm/ksm.c +1 -3
index 16532fa85a46..e2ce850c2739 100644
@@ -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 {
Signature · 1 line
2.39.2
#1 lore raw
Quoted 4 lines · Kevin Hao

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.

Quoted 29 lines
Cc Andrea Arcangeli <[email protected]>
Cc Andrew Morton <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>
Cc Pavel Machek <[email protected]>
---
 mm/ksm.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
mm/ksm.c +1 -4
index 16532fa85a46..e2ce850c2739 100644
@@ -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

#2 lore raw
Quoted 8 lines · Rafael J. Wysocki
Quoted 4 lines · Kevin Hao

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.

Signature · 3 lines
Cheers,

David / dhildenb
author
#3 lore raw
Quoted 7 lines · David Hildenbrand
Quoted 6 lines · Rafael J. Wysocki
Quoted 4 lines · Kevin Hao

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.

Quoted 5 lines
Quoted 2 lines

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

Acked-by on v1 1/1
#4 lore raw
In reply to Kevin Hao
Quoted 20 lines · Kevin Hao

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.

Cc Andrea Arcangeli <[email protected]>
Cc Andrew Morton <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>
Cc Pavel Machek <[email protected]>
---
  mm/ksm.c | 4 +---
  1 file changed, 1 insertion(+), 3 deletions(-)
mm/ksm.c +0 -0
index 16532fa85a46..e2ce850c2739 100644
@@ -2604,11 +2604,9 @@ static int ksm_scan_thread(void *nothing)
  			ksm_do_scan(ksm_thread_pages_to_scan);
  		mutex_unlock(&ksm_thread_mutex);

mm/ksm.c static int ksm_scan_thread(void *nothing) · line 2612

Quoted 10 lines
mm/ksm.c +1 -3
 
-		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.

Signature · 3 lines
Cheers,

David / dhildenb