Skip to content

Commit 8a32c44

Browse files
committed
freezer: implement and use kthread_freezable_should_stop()
Writeback and thinkpad_acpi have been using thaw_process() to prevent deadlock between the freezer and kthread_stop(); unfortunately, this is inherently racy - nothing prevents freezing from happening between thaw_process() and kthread_stop(). This patch implements kthread_freezable_should_stop() which enters refrigerator if necessary but is guaranteed to return if kthread_stop() is invoked. Both thaw_process() users are converted to use the new function. Note that this deadlock condition exists for many of freezable kthreads. They need to be converted to use the new should_stop or freezable workqueue. Tested with synthetic test case. Signed-off-by: Tejun Heo <tj@kernel.org> Acked-by: Henrique de Moraes Holschuh <ibm-acpi@hmh.eng.br> Cc: Jens Axboe <axboe@kernel.dk> Cc: Oleg Nesterov <oleg@redhat.com>
1 parent a0acae0 commit 8a32c44

7 files changed

Lines changed: 42 additions & 23 deletions

File tree

‎drivers/platform/x86/thinkpad_acpi.c‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2456,8 +2456,9 @@ static int hotkey_kthread(void *data)
24562456
u32 poll_mask, event_mask;
24572457
unsigned int si, so;
24582458
unsigned long t;
2459-
unsigned int change_detector, must_reset;
2459+
unsigned int change_detector;
24602460
unsigned int poll_freq;
2461+
bool was_frozen;
24612462

24622463
mutex_lock(&hotkey_thread_mutex);
24632464

@@ -2488,14 +2489,14 @@ static int hotkey_kthread(void *data)
24882489
t = 100; /* should never happen... */
24892490
}
24902491
t = msleep_interruptible(t);
2491-
if (unlikely(kthread_should_stop()))
2492+
if (unlikely(kthread_freezable_should_stop(&was_frozen)))
24922493
break;
2493-
must_reset = try_to_freeze();
2494-
if (t > 0 && !must_reset)
2494+
2495+
if (t > 0 && !was_frozen)
24952496
continue;
24962497

24972498
mutex_lock(&hotkey_thread_data_mutex);
2498-
if (must_reset || hotkey_config_change != change_detector) {
2499+
if (was_frozen || hotkey_config_change != change_detector) {
24992500
/* forget old state on thaw or config change */
25002501
si = so;
25012502
t = 0;
@@ -2528,10 +2529,6 @@ static int hotkey_kthread(void *data)
25282529
static void hotkey_poll_stop_sync(void)
25292530
{
25302531
if (tpacpi_hotkey_task) {
2531-
if (frozen(tpacpi_hotkey_task) ||
2532-
freezing(tpacpi_hotkey_task))
2533-
thaw_process(tpacpi_hotkey_task);
2534-
25352532
kthread_stop(tpacpi_hotkey_task);
25362533
tpacpi_hotkey_task = NULL;
25372534
mutex_lock(&hotkey_thread_mutex);

‎fs/fs-writeback.c‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ int bdi_writeback_thread(void *data)
947947

948948
trace_writeback_thread_start(bdi);
949949

950-
while (!kthread_should_stop()) {
950+
while (!kthread_freezable_should_stop(NULL)) {
951951
/*
952952
* Remove own delayed wake-up timer, since we are already awake
953953
* and we'll take care of the preriodic write-back.
@@ -977,8 +977,6 @@ int bdi_writeback_thread(void *data)
977977
*/
978978
schedule();
979979
}
980-
981-
try_to_freeze();
982980
}
983981

984982
/* Flush any work that raced with us exiting */

‎include/linux/freezer.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ static inline bool should_send_signal(struct task_struct *p)
4747
/* Takes and releases task alloc lock using task_lock() */
4848
extern int thaw_process(struct task_struct *p);
4949

50-
extern bool __refrigerator(void);
50+
extern bool __refrigerator(bool check_kthr_stop);
5151
extern int freeze_processes(void);
5252
extern int freeze_kernel_threads(void);
5353
extern void thaw_processes(void);
@@ -57,7 +57,7 @@ static inline bool try_to_freeze(void)
5757
might_sleep();
5858
if (likely(!freezing(current)))
5959
return false;
60-
return __refrigerator();
60+
return __refrigerator(false);
6161
}
6262

6363
extern bool freeze_task(struct task_struct *p, bool sig_only);
@@ -180,7 +180,7 @@ static inline void set_freeze_flag(struct task_struct *p) {}
180180
static inline void clear_freeze_flag(struct task_struct *p) {}
181181
static inline int thaw_process(struct task_struct *p) { return 1; }
182182

183-
static inline bool __refrigerator(void) { return false; }
183+
static inline bool __refrigerator(bool check_kthr_stop) { return false; }
184184
static inline int freeze_processes(void) { return -ENOSYS; }
185185
static inline int freeze_kernel_threads(void) { return -ENOSYS; }
186186
static inline void thaw_processes(void) {}

‎include/linux/kthread.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
3535
void kthread_bind(struct task_struct *k, unsigned int cpu);
3636
int kthread_stop(struct task_struct *k);
3737
int kthread_should_stop(void);
38+
bool kthread_freezable_should_stop(bool *was_frozen);
3839
void *kthread_data(struct task_struct *k);
3940

4041
int kthreadd(void *unused);

‎kernel/freezer.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <linux/export.h>
1010
#include <linux/syscalls.h>
1111
#include <linux/freezer.h>
12+
#include <linux/kthread.h>
1213

1314
/*
1415
* freezing is complete, mark current process as frozen
@@ -23,7 +24,7 @@ static inline void frozen_process(void)
2324
}
2425

2526
/* Refrigerator is place where frozen processes are stored :-). */
26-
bool __refrigerator(void)
27+
bool __refrigerator(bool check_kthr_stop)
2728
{
2829
/* Hmm, should we be allowed to suspend when there are realtime
2930
processes around? */
@@ -50,7 +51,8 @@ bool __refrigerator(void)
5051

5152
for (;;) {
5253
set_current_state(TASK_UNINTERRUPTIBLE);
53-
if (!frozen(current))
54+
if (!frozen(current) ||
55+
(check_kthr_stop && kthread_should_stop()))
5456
break;
5557
was_frozen = true;
5658
schedule();

‎kernel/kthread.c‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,31 @@ int kthread_should_stop(void)
5858
}
5959
EXPORT_SYMBOL(kthread_should_stop);
6060

61+
/**
62+
* kthread_freezable_should_stop - should this freezable kthread return now?
63+
* @was_frozen: optional out parameter, indicates whether %current was frozen
64+
*
65+
* kthread_should_stop() for freezable kthreads, which will enter
66+
* refrigerator if necessary. This function is safe from kthread_stop() /
67+
* freezer deadlock and freezable kthreads should use this function instead
68+
* of calling try_to_freeze() directly.
69+
*/
70+
bool kthread_freezable_should_stop(bool *was_frozen)
71+
{
72+
bool frozen = false;
73+
74+
might_sleep();
75+
76+
if (unlikely(freezing(current)))
77+
frozen = __refrigerator(true);
78+
79+
if (was_frozen)
80+
*was_frozen = frozen;
81+
82+
return kthread_should_stop();
83+
}
84+
EXPORT_SYMBOL_GPL(kthread_freezable_should_stop);
85+
6186
/**
6287
* kthread_data - return data value specified on kthread creation
6388
* @task: kthread task in question

‎mm/backing-dev.c‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -600,14 +600,10 @@ static void bdi_wb_shutdown(struct backing_dev_info *bdi)
600600

601601
/*
602602
* Finally, kill the kernel thread. We don't need to be RCU
603-
* safe anymore, since the bdi is gone from visibility. Force
604-
* unfreeze of the thread before calling kthread_stop(), otherwise
605-
* it would never exet if it is currently stuck in the refrigerator.
603+
* safe anymore, since the bdi is gone from visibility.
606604
*/
607-
if (bdi->wb.task) {
608-
thaw_process(bdi->wb.task);
605+
if (bdi->wb.task)
609606
kthread_stop(bdi->wb.task);
610-
}
611607
}
612608

613609
/*

0 commit comments

Comments
 (0)