LKML Archive on
 help / color / Atom feed
* [PATCH] kfence: Avoid stalling work queue task without allocations
@ 2020-11-10 13:53 Marco Elver
  2020-11-10 14:25 ` Dmitry Vyukov
  2020-11-10 23:23 ` Anders Roxell
  0 siblings, 2 replies; 63+ messages in thread
From: Marco Elver @ 2020-11-10 13:53 UTC (permalink / raw)
  To: elver, akpm
  Cc: glider, dvyukov, jannh, mark.rutland, linux-kernel, linux-mm,
	kasan-dev, Anders Roxell

To toggle the allocation gates, we set up a delayed work that calls
toggle_allocation_gate(). Here we use wait_event() to await an
allocation and subsequently disable the static branch again. However, if
the kernel has stopped doing allocations entirely, we'd wait
indefinitely, and stall the worker task. This may also result in the
appropriate warnings if CONFIG_DETECT_HUNG_TASK=y.

Therefore, introduce a 1 second timeout and use wait_event_timeout(). If
the timeout is reached, the static branch is disabled and a new delayed
work is scheduled to try setting up an allocation at a later time.

Note that, this scenario is very unlikely during normal workloads once
the kernel has booted and user space tasks are running. It can, however,
happen during early boot after KFENCE has been enabled, when e.g.
running tests that do not result in any allocations.

Reported-by: Anders Roxell <>
Signed-off-by: Marco Elver <>
 mm/kfence/core.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/mm/kfence/core.c b/mm/kfence/core.c
index 9358f42a9a9e..933b197b8634 100644
--- a/mm/kfence/core.c
+++ b/mm/kfence/core.c
@@ -592,7 +592,11 @@ static void toggle_allocation_gate(struct work_struct *work)
 	/* Enable static key, and await allocation to happen. */
 	atomic_set(&allocation_gate, 0);
-	wait_event(allocation_wait, atomic_read(&allocation_gate) != 0);
+	/*
+	 * Await an allocation. Timeout after 1 second, in case the kernel stops
+	 * doing allocations, to avoid stalling this worker task for too long.
+	 */
+	wait_event_timeout(allocation_wait, atomic_read(&allocation_gate) != 0, HZ);
 	/* Disable static key and reset timer. */

^ permalink raw reply	[flat|nested] 63+ messages in thread