This patch introduces wait_event_no_hung() and
wb_wait_for_completion_no_hung() to make hung task detector
ignore the current task if it waits for a long time.
Signed-off-by: Julian Sun <sunjunchao@bytedance.com>
---
fs/fs-writeback.c | 15 +++++++++++++++
include/linux/backing-dev.h | 1 +
include/linux/wait.h | 15 +++++++++++++++
3 files changed, 31 insertions(+)
diff --git a/fs/fs-writeback.c b/fs/fs-writeback.c
index a07b8cf73ae2..eebb7f145734 100644
--- a/fs/fs-writeback.c
+++ b/fs/fs-writeback.c
@@ -216,6 +216,21 @@ void wb_wait_for_completion(struct wb_completion *done)
wait_event(*done->waitq, !atomic_read(&done->cnt));
}
+/*
+ * Same as wb_wait_for_completion() but hung task warning will not be
+ * triggered if it wait for a long time. Use this function with caution
+ * unless you are sure that the hung task is undesirable.
+ * When is this undesirable? From the kernel code perspective, there is
+ * no misbehavior and it has no impact on user behavior. For example, a
+ * *background* kthread/kworker used to clean resources while waiting a
+ * long time for writeback to finish.
+ */
+ void wb_wait_for_completion_no_hung(struct wb_completion *done)
+ {
+ atomic_dec(&done->cnt); /* put down the initial count */
+ wait_event_no_hung(*done->waitq, !atomic_read(&done->cnt));
+ }
+
#ifdef CONFIG_CGROUP_WRITEBACK
/*
diff --git a/include/linux/backing-dev.h b/include/linux/backing-dev.h
index e721148c95d0..9d3335866f6f 100644
--- a/include/linux/backing-dev.h
+++ b/include/linux/backing-dev.h
@@ -40,6 +40,7 @@ void wb_start_background_writeback(struct bdi_writeback *wb);
void wb_workfn(struct work_struct *work);
void wb_wait_for_completion(struct wb_completion *done);
+void wb_wait_for_completion_no_hung(struct wb_completion *done);
extern spinlock_t bdi_lock;
extern struct list_head bdi_list;
diff --git a/include/linux/wait.h b/include/linux/wait.h
index 09855d819418..8f05d0bb8db7 100644
--- a/include/linux/wait.h
+++ b/include/linux/wait.h
@@ -330,6 +330,21 @@ __out: __ret; \
(void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
schedule())
+#define __wait_event_no_hung(wq_head, condition) \
+ (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
+ current_set_flags(PF_DONT_HUNG); \
+ schedule(); \
+ current_clear_flags(PF_DONT_HUNG))
+
+#define wait_event_no_hung(wq_head, condition) \
+do { \
+ might_sleep(); \
+ if (condition) \
+ break; \
+ __wait_event_no_hung(wq_head, condition); \
+} while (0)
+
+
/**
* wait_event - sleep until a condition gets true
* @wq_head: the waitqueue to wait on
--
2.39.5