Skip to contentSkip to conversation

kthread: fix task state in kthread worker if being frozen

PATCH Superseded Chen Yu 2 participants 3 messages Aug 19, 2024 – Aug 20, 2024

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Chen Yu

author
#0 lore raw

PATCH 1/1 kthread: fix task state in kthread worker if being frozen

It was reported that during cpu hotplug test, the following error was triggered:

do not call blocking ops when !TASK_RUNNING; state=1 set at kthread_worker_fn (kernel/kthread.c:?)

 WARNING: CPU: 1 PID: 674 at kernel/sched/core.c:8469 __might_sleep

handle_bug
exc_invalid_op
asm_exc_invalid_op
__might_sleep
__might_sleep
kthread_worker_fn
kthread_worker_fn
kthread
__cfi_kthread_worker_fn
ret_from_fork
__cfi_kthread
ret_from_fork_asm

Peter pointed out that there is a race condition when the kworker is being frozen and falls into try_to_freeze() with TASK_INTERRUPTIBLE, which triggeres the warning.

Fix this by explicitly set the TASK_RUNNING before entering try_to_freeze().

---
 kernel/kthread.c | 6 ++++++
 1 file changed, 6 insertions(+)
kernel/kthread.c +6 -0
index f7be976ff88a..06ab3ada9cf1 100644
@@ -848,6 +848,12 @@ int kthread_worker_fn(void *worker_ptr)
 	} else if (!freezing(current))
 		schedule();
 
+	/*
+	 * Explicitly set the running state in case we are being
+	 * frozen and skip the schedule() above. try_to_freeze()
+	 * expects the current task to be in the running state.
+	 */
+	__set_current_state(TASK_RUNNING);
 	try_to_freeze();
 	cond_resched();
 	goto repeat;
Signature · 1 line
2.25.1
#1 lore raw
Quoted 19 lines · On Mon, 19 Aug 2024 22:15:51 +0800 Chen Yu <[email protected]> wrote:

It was reported that during cpu hotplug test, the following error was triggered:

do not call blocking ops when !TASK_RUNNING; state=1 set at kthread_worker_fn (kernel/kthread.c:?)

 WARNING: CPU: 1 PID: 674 at kernel/sched/core.c:8469 __might_sleep

handle_bug
exc_invalid_op
asm_exc_invalid_op
__might_sleep
__might_sleep
kthread_worker_fn
kthread_worker_fn
kthread
__cfi_kthread_worker_fn
ret_from_fork
__cfi_kthread
ret_from_fork_asm

Quoted 3 lines

Peter pointed out that there is a race condition when the kworker is being frozen and falls into try_to_freeze() with TASK_INTERRUPTIBLE, which triggeres the warning.

OK. A full description of this race would be better than simply asserting that it exists, please.

Quoted 1 line

Fix this by explicitly set the TASK_RUNNING before entering try_to_freeze().

OK.

Quoted 5 lines
kernel/kthread.c +0 -0
@@ -848,6 +848,12 @@ int kthread_worker_fn(void *worker_ptr)
 	} else if (!freezing(current))
 		schedule();

kernel/kthread.c int kthread_worker_fn(void *worker_ptr) · line 859

Quoted 10 lines
kernel/kthread.c +6 -0
 
+	/*
+	 * Explicitly set the running state in case we are being
+	 * frozen and skip the schedule() above. try_to_freeze()
+	 * expects the current task to be in the running state.
+	 */
+	__set_current_state(TASK_RUNNING);
 	try_to_freeze();
 	cond_resched();
 	goto repeat;

Comment is helpful, but why express in a comment that which can be expressed in code?

kernel/kthread.c renamed from kernel/kthread.c~kthread-fix-task-state-in-kthread-worker-if-being-frozen +6 -0
@@ -847,6 +847,12 @@ repeat:
 		trace_sched_kthread_work_execute_end(work, func);
 	} else if (!freezing(current))
 		schedule();
+	} else {
+		/*
+		 * Handle the case where X happens
+		 */
+		__set_current_state(TASK_RUNNING);
+	}
 
 	try_to_freeze();
 	cond_resched();

_

author
#2 lore raw

Hi Andrew,

Quoted 21 lines · Andrew Morton
Quoted 19 lines · On Mon, 19 Aug 2024 22:15:51 +0800 Chen Yu <[email protected]> wrote:

It was reported that during cpu hotplug test, the following error was triggered:

do not call blocking ops when !TASK_RUNNING; state=1 set at kthread_worker_fn (kernel/kthread.c:?)

 WARNING: CPU: 1 PID: 674 at kernel/sched/core.c:8469 __might_sleep

handle_bug
exc_invalid_op
asm_exc_invalid_op
__might_sleep
__might_sleep
kthread_worker_fn
kthread_worker_fn
kthread
__cfi_kthread_worker_fn
ret_from_fork
__cfi_kthread
ret_from_fork_asm

Quoted 7 lines
Quoted 3 lines

Peter pointed out that there is a race condition when the kworker is being frozen and falls into try_to_freeze() with TASK_INTERRUPTIBLE, which triggeres the warning.

OK. A full description of this race would be better than simply asserting that it exists, please.

OK, will write a description for this.

Quoted 10 lines
Quoted 1 line

Fix this by explicitly set the TASK_RUNNING before entering try_to_freeze().

OK.

Quoted 6 lines
kernel/kthread.c +0 -0
@@ -848,6 +848,12 @@ int kthread_worker_fn(void *worker_ptr)
 	} else if (!freezing(current))
 		schedule();
 
Quoted 13 lines

kernel/kthread.c repeat:

Quoted 9 lines
kernel/kthread.c +6 -0
+	/*
+	 * Explicitly set the running state in case we are being
+	 * frozen and skip the schedule() above. try_to_freeze()
+	 * expects the current task to be in the running state.
+	 */
+	__set_current_state(TASK_RUNNING);
 	try_to_freeze();
 	cond_resched();
 	goto repeat;

Comment is helpful, but why express in a comment that which can be expressed in code?

OK, will do in next version.

thanks,
Chenyu

Quoted 17 lines
kernel/kthread.c renamed from kernel/kthread.c~kthread-fix-task-state-in-kthread-worker-if-being-frozen +6 -0
@@ -847,6 +847,12 @@ repeat:
 		trace_sched_kthread_work_execute_end(work, func);
 	} else if (!freezing(current))
 		schedule();
+	} else {
+		/*
+		 * Handle the case where X happens
+		 */
+		__set_current_state(TASK_RUNNING);
+	}
 
 	try_to_freeze();
 	cond_resched();

_