LKML Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH 1/1] timer: Warn about schedule_timeout() called for tasks in TASK_RUNNING state
@ 2020-01-16 14:02 Alexander Popov
  2020-01-16 14:52 ` Steven Rostedt
  0 siblings, 1 reply; 4+ messages in thread
From: Alexander Popov @ 2020-01-16 14:02 UTC (permalink / raw)
  To: Linus Torvalds, John Stultz, Thomas Gleixner, Stephen Boyd,
	Ingo Molnar, Peter Zijlstra, Juri Lelli, Vincent Guittot,
	Dietmar Eggemann, Steven Rostedt, Ben Segall, Mel Gorman,
	linux-kernel
  Cc: notify

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.

Signed-off-by: Alexander Popov <alex.popov@linux.com>
---
 kernel/time/timer.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/kernel/time/timer.c b/kernel/time/timer.c
index 4820823515e9..52ad2d6ce352 100644
--- a/kernel/time/timer.c
+++ b/kernel/time/timer.c
@@ -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;
-- 
2.24.1


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

* Re: [PATCH 1/1] timer: Warn about schedule_timeout() called for tasks in TASK_RUNNING state
  2020-01-16 14:02 [PATCH 1/1] timer: Warn about schedule_timeout() called for tasks in TASK_RUNNING state Alexander Popov
@ 2020-01-16 14:52 ` Steven Rostedt
  2020-01-16 14:55   ` Steven Rostedt
  2020-01-17 15:33   ` Alexander Popov
  0 siblings, 2 replies; 4+ messages in thread
From: Steven Rostedt @ 2020-01-16 14:52 UTC (permalink / raw)
  To: Alexander Popov
  Cc: Linus Torvalds, John Stultz, Thomas Gleixner, Stephen Boyd,
	Ingo Molnar, Peter Zijlstra, Juri Lelli, Vincent Guittot,
	Dietmar Eggemann, Ben Segall, Mel Gorman, linux-kernel, notify

On Thu, 16 Jan 2020 17:02:18 +0300
Alexander Popov <alex.popov@linux.com> 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.
> 
> Signed-off-by: Alexander Popov <alex.popov@linux.com>
> ---
>  kernel/time/timer.c | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/kernel/time/timer.c b/kernel/time/timer.c
> index 4820823515e9..52ad2d6ce352 100644
> --- a/kernel/time/timer.c
> +++ b/kernel/time/timer.c
> @@ -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


>  	expire = timeout + jiffies;
>  
>  	timer.task = current;


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

* Re: [PATCH 1/1] timer: Warn about schedule_timeout() called for tasks in TASK_RUNNING state
  2020-01-16 14:52 ` Steven Rostedt
@ 2020-01-16 14:55   ` Steven Rostedt
  2020-01-17 15:33   ` Alexander Popov
  1 sibling, 0 replies; 4+ messages in thread
From: Steven Rostedt @ 2020-01-16 14:55 UTC (permalink / raw)
  To: Alexander Popov
  Cc: Linus Torvalds, John Stultz, Thomas Gleixner, Stephen Boyd,
	Ingo Molnar, Peter Zijlstra, Juri Lelli, Vincent Guittot,
	Dietmar Eggemann, Ben Segall, Mel Gorman, linux-kernel, notify

On Thu, 16 Jan 2020 09:52:20 -0500
Steven Rostedt <rostedt@goodmis.org> wrote:

> > --- a/kernel/time/timer.c
> > +++ b/kernel/time/timer.c
> > @@ -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:


Also, there are helpers here that you can use:

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

-- Steve

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

* Re: [PATCH 1/1] timer: Warn about schedule_timeout() called for tasks in TASK_RUNNING state
  2020-01-16 14:52 ` Steven Rostedt
  2020-01-16 14:55   ` Steven Rostedt
@ 2020-01-17 15:33   ` Alexander Popov
  1 sibling, 0 replies; 4+ messages in thread
From: Alexander Popov @ 2020-01-17 15:33 UTC (permalink / raw)
  To: Steven Rostedt
  Cc: Linus Torvalds, John Stultz, Thomas Gleixner, Stephen Boyd,
	Ingo Molnar, Peter Zijlstra, Juri Lelli, Vincent Guittot,
	Dietmar Eggemann, Ben Segall, Mel Gorman, linux-kernel, notify

On 16.01.2020 17:52, Steven Rostedt wrote:
> On Thu, 16 Jan 2020 17:02:18 +0300
> Alexander Popov <alex.popov@linux.com> 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.
>>
>> Signed-off-by: Alexander Popov <alex.popov@linux.com>
>> ---
>>  kernel/time/timer.c | 5 +++++
>>  1 file changed, 5 insertions(+)
>>
>> diff --git a/kernel/time/timer.c b/kernel/time/timer.c
>> index 4820823515e9..52ad2d6ce352 100644
>> --- a/kernel/time/timer.c
>> +++ b/kernel/time/timer.c
>> @@ -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.

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

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

end of thread, back to index

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-16 14:02 [PATCH 1/1] timer: Warn about schedule_timeout() called for tasks in TASK_RUNNING state Alexander Popov
2020-01-16 14:52 ` Steven Rostedt
2020-01-16 14:55   ` Steven Rostedt
2020-01-17 15:33   ` Alexander Popov

LKML Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/lkml/0 lkml/git/0.git
	git clone --mirror https://lore.kernel.org/lkml/1 lkml/git/1.git
	git clone --mirror https://lore.kernel.org/lkml/2 lkml/git/2.git
	git clone --mirror https://lore.kernel.org/lkml/3 lkml/git/3.git
	git clone --mirror https://lore.kernel.org/lkml/4 lkml/git/4.git
	git clone --mirror https://lore.kernel.org/lkml/5 lkml/git/5.git
	git clone --mirror https://lore.kernel.org/lkml/6 lkml/git/6.git
	git clone --mirror https://lore.kernel.org/lkml/7 lkml/git/7.git
	git clone --mirror https://lore.kernel.org/lkml/8 lkml/git/8.git
	git clone --mirror https://lore.kernel.org/lkml/9 lkml/git/9.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 lkml lkml/ https://lore.kernel.org/lkml \
		linux-kernel@vger.kernel.org
	public-inbox-index lkml

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-kernel


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git