All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] kernel:time fix race condition in alaramtimer.c
@ 2013-07-06 16:07 Marcus Gelderie
  2013-07-08  5:31 ` Srivatsa S. Bhat
  0 siblings, 1 reply; 3+ messages in thread
From: Marcus Gelderie @ 2013-07-06 16:07 UTC (permalink / raw)
  To: artem.bityutskiy, herbert; +Cc: linux-kernel, Marcus Gelderie

This patch fixes a race condition whereby the process can be caused to
sleep indefinitely. The problem occurs when the process is preempted
after having set its state to TASK_INTERRUPTIBLE but before starting the
alarm.

Disabling preemption until the alarm has been set solves this problem.

A second problem may occur if the process reaches the alarm_cancel call
while still being in TASK_INTERRUPTIBLE. I do not see how this could
possibly happen (if schedule returns, the state is TASK_RUNNING; if
schedule is never called, the wakeup callback must have woken the
process (data==NULL) thereby setting its state to TASK_RUNNING) but
the code is clearly written under the assumption that it can happen.
In this case, the state must be set to TASK_RUNNING after the alarm
has been canceled for the same reasons as above.

Signed-off-by: Marcus Gelderie <redmnic@gmail.com>
---
 kernel/time/alarmtimer.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/kernel/time/alarmtimer.c b/kernel/time/alarmtimer.c
index f11d83b..81c8b31 100644
--- a/kernel/time/alarmtimer.c
+++ b/kernel/time/alarmtimer.c
@@ -585,15 +585,19 @@ static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp)
 {
 	alarm->data = (void *)current;
 	do {
+		preempt_disable();
 		set_current_state(TASK_INTERRUPTIBLE);
 		alarm_start(alarm, absexp);
+		preempt_enable_no_resched();
 		if (likely(alarm->data))
 			schedule();
 
+		preempt_disable();
 		alarm_cancel(alarm);
+		__set_current_state(TASK_RUNNING);
+		preempt_enable_no_resched();
 	} while (alarm->data && !signal_pending(current));
 
-	__set_current_state(TASK_RUNNING);
 
 	return (alarm->data == NULL);
 }
-- 
1.8.1.5


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

* Re: [PATCH] kernel:time fix race condition in alaramtimer.c
  2013-07-06 16:07 [PATCH] kernel:time fix race condition in alaramtimer.c Marcus Gelderie
@ 2013-07-08  5:31 ` Srivatsa S. Bhat
  2013-07-08  9:20   ` Marcus Gelderie
  0 siblings, 1 reply; 3+ messages in thread
From: Srivatsa S. Bhat @ 2013-07-08  5:31 UTC (permalink / raw)
  To: Marcus Gelderie; +Cc: artem.bityutskiy, herbert, linux-kernel

On 07/06/2013 09:37 PM, Marcus Gelderie wrote:
> This patch fixes a race condition whereby the process can be caused to
> sleep indefinitely. The problem occurs when the process is preempted
> after having set its state to TASK_INTERRUPTIBLE but before starting the
> alarm.
> 

I don't think there is any such problem. The __schedule() function does
this check before dequeuing a task:

if (prev->state && !(preempt_count() & PREEMPT_ACTIVE))

The latter part helps prevent the kind of indefinite sleeping you described
above. Also, take a look at preempt_schedule(), preempt_schedule_irq()
and friends to see how in-kernel preemption is dealt with, by using
PREEMPT_ACTIVE. That would clarify why there is no bug here.

Regards,
Srivatsa S. Bhat

> ---
>  kernel/time/alarmtimer.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/kernel/time/alarmtimer.c b/kernel/time/alarmtimer.c
> index f11d83b..81c8b31 100644
> --- a/kernel/time/alarmtimer.c
> +++ b/kernel/time/alarmtimer.c
> @@ -585,15 +585,19 @@ static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp)
>  {
>  	alarm->data = (void *)current;
>  	do {
> +		preempt_disable();
>  		set_current_state(TASK_INTERRUPTIBLE);
>  		alarm_start(alarm, absexp);
> +		preempt_enable_no_resched();
>  		if (likely(alarm->data))
>  			schedule();
> 
> +		preempt_disable();
>  		alarm_cancel(alarm);
> +		__set_current_state(TASK_RUNNING);
> +		preempt_enable_no_resched();
>  	} while (alarm->data && !signal_pending(current));
> 
> -	__set_current_state(TASK_RUNNING);
> 
>  	return (alarm->data == NULL);
>  }
> 


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

* Re: [PATCH] kernel:time fix race condition in alaramtimer.c
  2013-07-08  5:31 ` Srivatsa S. Bhat
@ 2013-07-08  9:20   ` Marcus Gelderie
  0 siblings, 0 replies; 3+ messages in thread
From: Marcus Gelderie @ 2013-07-08  9:20 UTC (permalink / raw)
  To: Srivatsa S. Bhat; +Cc: artem.bityutskiy, herbert, linux-kernel

Ok you're right, that prevents it.

Sorry, I missed this check somehow.

Cheers
Marcus

On 07/08/2013 07:31 AM, Srivatsa S. Bhat wrote:
> On 07/06/2013 09:37 PM, Marcus Gelderie wrote:
>> This patch fixes a race condition whereby the process can be caused to
>> sleep indefinitely. The problem occurs when the process is preempted
>> after having set its state to TASK_INTERRUPTIBLE but before starting the
>> alarm.
>>
> 
> I don't think there is any such problem. The __schedule() function does
> this check before dequeuing a task:
> 
> if (prev->state && !(preempt_count() & PREEMPT_ACTIVE))
> 
> The latter part helps prevent the kind of indefinite sleeping you described
> above. Also, take a look at preempt_schedule(), preempt_schedule_irq()
> and friends to see how in-kernel preemption is dealt with, by using
> PREEMPT_ACTIVE. That would clarify why there is no bug here.
> 
> Regards,
> Srivatsa S. Bhat
> 
>> ---
>>  kernel/time/alarmtimer.c | 6 +++++-
>>  1 file changed, 5 insertions(+), 1 deletion(-)
>>
>> diff --git a/kernel/time/alarmtimer.c b/kernel/time/alarmtimer.c
>> index f11d83b..81c8b31 100644
>> --- a/kernel/time/alarmtimer.c
>> +++ b/kernel/time/alarmtimer.c
>> @@ -585,15 +585,19 @@ static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp)
>>  {
>>  	alarm->data = (void *)current;
>>  	do {
>> +		preempt_disable();
>>  		set_current_state(TASK_INTERRUPTIBLE);
>>  		alarm_start(alarm, absexp);
>> +		preempt_enable_no_resched();
>>  		if (likely(alarm->data))
>>  			schedule();
>>
>> +		preempt_disable();
>>  		alarm_cancel(alarm);
>> +		__set_current_state(TASK_RUNNING);
>> +		preempt_enable_no_resched();
>>  	} while (alarm->data && !signal_pending(current));
>>
>> -	__set_current_state(TASK_RUNNING);
>>
>>  	return (alarm->data == NULL);
>>  }
>>
> 


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

end of thread, other threads:[~2013-07-08  9:17 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-07-06 16:07 [PATCH] kernel:time fix race condition in alaramtimer.c Marcus Gelderie
2013-07-08  5:31 ` Srivatsa S. Bhat
2013-07-08  9:20   ` Marcus Gelderie

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.