[PATCH] w1: Fix warning on module removal

From: Zheyu Ma
Date: Thu Apr 07 2022 - 23:19:09 EST


This happens because w1_process() can leave the kthread in
TASK_INTERRUPTIBLE state when the kthread is being stopped. Since
kthread_exit() might sleep in exit_signals(), the warning is printed.

Fix this by set the current state to running before exiting.

The following log can reveal it:

[ 26.713749] ------------[ cut here ]------------
[ 26.714135] do not call blocking ops when !TASK_RUNNING; state=1 set at [<ffffffff83c59b0f>] w1_process+0x22f/0x370
[ 26.715053] WARNING: CPU: 4 PID: 282 at kernel/sched/core.c:9660 __might_sleep+0x96/0xb0
[ 26.719007] RIP: 0010:__might_sleep+0x96/0xb0
[ 26.727411] Call Trace:
[ 26.727644] <TASK>
[ 26.727832] exit_signals+0x2f/0x3b0
[ 26.728143] do_exit+0xcd/0x18c0
[ 26.728423] ? kthread_should_stop+0x5f/0xa0
[ 26.728801] ? w1_process+0x34e/0x370
[ 26.729115] kthread_exit+0x42/0x60
[ 26.729407] ? w1_process_callbacks+0x130/0x130
[ 26.729804] kthread+0x272/0x2e0
[ 26.730083] ? w1_process_callbacks+0x130/0x130
[ 26.730464] ? kthread_blkcg+0xa0/0xa0
[ 26.730797] ret_from_fork+0x22/0x30
[ 26.731109] </TASK>

Signed-off-by: Zheyu Ma <zheyuma97@xxxxxxxxx>
---
drivers/w1/w1.c | 1 +
1 file changed, 1 insertion(+)

diff --git a/drivers/w1/w1.c b/drivers/w1/w1.c
index f2ae2e563dc5..ae169f3ee941 100644
--- a/drivers/w1/w1.c
+++ b/drivers/w1/w1.c
@@ -1206,6 +1206,7 @@ int w1_process(void *data)
schedule();
}

+ __set_current_state(TASK_RUNNING);
atomic_dec(&dev->refcnt);

return 0;
--
2.25.1