Skip to contentSkip to conversation

timer: Warn about schedule_timeout() called for tasks in TASK_RUNNING state

PATCH In review Alexander Popov 2 participants 4 messages Jan 16, 2020 – Jan 17, 2020 High-resolution timers, timer wheel, clockevents

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 timer: Warn about schedule_timeout() called for tasks in TASK_RUNNING state

When we were preparing the patch 6dcd5d7a7a29c1e, we made a mistake noticed by Linus: schedule_timeout() was called without setting the task state to anything particular. It calls the scheduler, but doesn't delay anything, because the task stays runnable. That happens because sched_submit_work() does nothing for tasks in TASK_RUNNING state.

Let's add a WARN_ONCE() under CONFIG_SCHED_DEBUG to detect such kernel API misuse.

---
 kernel/time/timer.c | 5 +++++
 1 file changed, 5 insertions(+)
kernel/time/timer.c +5 -0
index 4820823515e9..52ad2d6ce352 100644
@@ -1887,6 +1887,11 @@ signed long __sched schedule_timeout(signed long timeout)
 		}
 	}
 
+#ifdef CONFIG_SCHED_DEBUG
+	WARN_ONCE(current->state == TASK_RUNNING,
+			"schedule_timeout for TASK_RUNNING\n");
+#endif
+
 	expire = timeout + jiffies;
 
 	timer.task = current;
Signature · 1 line
2.24.1
#1 lore raw
Quoted 19 lines · On Thu, 16 Jan 2020 17:02:18 +0300 Alexander Popov <[email protected]> wrote:

When we were preparing the patch 6dcd5d7a7a29c1e, we made a mistake noticed by Linus: schedule_timeout() was called without setting the task state to anything particular. It calls the scheduler, but doesn't delay anything, because the task stays runnable. That happens because sched_submit_work() does nothing for tasks in TASK_RUNNING state.

Let's add a WARN_ONCE() under CONFIG_SCHED_DEBUG to detect such kernel API misuse.

---
 kernel/time/timer.c | 5 +++++
 1 file changed, 5 insertions(+)
kernel/time/timer.c +0 -0
index 4820823515e9..52ad2d6ce352 100644

kernel/time/timer.c signed long __sched schedule_timeout(signed long timeout) · line 1894

Quoted 8 lines
kernel/time/timer.c +5 -0
@@ -1887,6 +1887,11 @@ signed long __sched schedule_timeout(signed long timeout)
 		}
 	}
 
+#ifdef CONFIG_SCHED_DEBUG
+	WARN_ONCE(current->state == TASK_RUNNING,
+			"schedule_timeout for TASK_RUNNING\n");
+#endif
+

But this can trigger false warnings. For example, if we are waiting on an event with a timeout:

	DEFINE_WAIT(wait);

	for (;;) {
		prepare_to_wait(&waitq, &wait, TASK_UNINTERRUPTIBLE);
		if (event)
			break;
		timeout = schedule_timeout(timeout);
		if (!timeout)
			break;
	}
	finish_wait(&waitq, &wait);

If the event happens between "prepare_to_wait" and just before schedule_timeout(), the wait queue will set this task's state to TASK_RUNNING, which in turn triggers your warning.

-- Steve

Quoted 3 lines
kernel/time/timer.c +0 -0
 	expire = timeout + jiffies;
 
 	timer.task = current;
#2 lore raw
Quoted 6 lines · On Thu, 16 Jan 2020 09:52:20 -0500 Steven Rostedt <[email protected]> wrote:
Quoted 6 lines
kernel/time/timer.c +0 -0
@@ -1887,6 +1887,11 @@ signed long __sched schedule_timeout(signed long timeout)
 		}
 	}
 
Quoted 8 lines

kernel/time/timer.c signed long __sched schedule_timeout(signed long timeout) · line 1894

Quoted 5 lines
kernel/time/timer.c +5 -0
+#ifdef CONFIG_SCHED_DEBUG
+	WARN_ONCE(current->state == TASK_RUNNING,
+			"schedule_timeout for TASK_RUNNING\n");
+#endif
+  

But this can trigger false warnings. For example, if we are waiting on an event with a timeout:

Also, there are helpers here that you can use:

schedule_timeout_interruptible(signed long timeout); schedule_timeout_uninterruptible(signed long timeout)

-- Steve

#3 lore raw
In reply to Steven Rostedt
Quoted 48 lines · Steven Rostedt
Quoted 19 lines · On Thu, 16 Jan 2020 17:02:18 +0300 Alexander Popov <[email protected]> wrote:

When we were preparing the patch 6dcd5d7a7a29c1e, we made a mistake noticed by Linus: schedule_timeout() was called without setting the task state to anything particular. It calls the scheduler, but doesn't delay anything, because the task stays runnable. That happens because sched_submit_work() does nothing for tasks in TASK_RUNNING state.

Let's add a WARN_ONCE() under CONFIG_SCHED_DEBUG to detect such kernel API misuse.

---
 kernel/time/timer.c | 5 +++++
 1 file changed, 5 insertions(+)
kernel/time/timer.c +0 -0
index 4820823515e9..52ad2d6ce352 100644

kernel/time/timer.c signed long __sched schedule_timeout(signed long timeout) · line 1894

Quoted 8 lines
kernel/time/timer.c +5 -0
@@ -1887,6 +1887,11 @@ signed long __sched schedule_timeout(signed long timeout)
 		}
 	}
 
+#ifdef CONFIG_SCHED_DEBUG
+	WARN_ONCE(current->state == TASK_RUNNING,
+			"schedule_timeout for TASK_RUNNING\n");
+#endif
+

But this can trigger false warnings. For example, if we are waiting on an event with a timeout:

	DEFINE_WAIT(wait);

	for (;;) {
		prepare_to_wait(&waitq, &wait, TASK_UNINTERRUPTIBLE);
		if (event)
			break;
		timeout = schedule_timeout(timeout);
		if (!timeout)
			break;
	}
	finish_wait(&waitq, &wait);
Quoted 3 lines

If the event happens between "prepare_to_wait" and just before schedule_timeout(), the wait queue will set this task's state to TASK_RUNNING, which in turn triggers your warning.

Steven, thanks for the explanation.

If I understand you right, it is the intended behavior of schedule_timeout() in some sense.

So the best thing I can do here is adding an explanatory comment to the schedule_timeout() description.

Maybe that would help against such situations:
https://lore.kernel.org/lkml/CAHk-=wgE-veRb7+mw9oMmsD97BLnL+q8Gxu0QRrK65S2yQfMdQ@mail.gmail.com/#t

I'll come with the patch soon.

Best regards,
Alexander