public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Oleg Nesterov <oleg@tv-sign.ru>
To: Thomas Gleixner <tglx@linutronix.de>
Cc: Jeremy Katz <jeremy.katz@windriver.com>,
	linux-kernel@vger.kernel.org, Andrew Morton <akpm@osdl.org>,
	Ingo Molnar <mingo@elte.hu>, Stable Team <stable@kernel.org>
Subject: Re: [PATCH] posix-timer: fix deletion race
Date: Tue, 17 Jul 2007 17:07:05 +0400	[thread overview]
Message-ID: <20070717130705.GA74@tv-sign.ru> (raw)
In-Reply-To: <1184662429.12353.426.camel@chaos>

On 07/17, Thomas Gleixner wrote:
>
> Jeremy Katz experienced a posix-timer related bug on 2.6.14. This is
> caused by a subtle race, which is there since the original posix timer
> commit and persists until today.
> 
> timer_delete does:
> lock_timer();
> timer->it_process = NULL;
> unlock_timer();
> release_posix_timer();
> 
> timer->it_process is checked in lock_timer() to prevent access to a
> timer, which is on the way to be deleted, but the check happens after
> idr_lock is dropped. This allows release_posix_timer() to delete the
> timer before the lock code can check the timer:
> 
> CPU 0				CPU 1
> lock_timer();			
> timer->it_process = NULL;
> unlock_timer();
> 				lock_timer()
> 					spin_lock(idr_lock);
> 					timer = idr_find();
> 					spin_lock(timer->lock);
> 					spin_unlock(idr_lock);
> release_posix_timer();
> 	spin_lock(idr_lock);
> 	idr_remove(timer);
> 	spin_unlock(idr_lock);
> 	free_timer(timer);
> 					if (timer->......)

This is funny. I do remember this bug was discussed a couple of years ago,
and the conclusion was "we can fix it later" :)

> --- a/kernel/posix-timers.c
> +++ b/kernel/posix-timers.c
> @@ -605,13 +605,14 @@ static struct k_itimer * lock_timer(timer_t timer_id, unsigned long *flags)
>  	timr = (struct k_itimer *) idr_find(&posix_timers_id, (int) timer_id);
>  	if (timr) {
>  		spin_lock(&timr->it_lock);
> -		spin_unlock(&idr_lock);
>  
>  		if ((timr->it_id != timer_id) || !(timr->it_process) ||
>  				timr->it_process->tgid != current->tgid) {
> -			unlock_timer(timr, *flags);
> +			spin_unlock(&timr->it_lock);
> +			spin_unlock_irqrestore(&idr_lock, *flags);
>  			timr = NULL;
> -		}
> +		} else
> +			spin_unlock(&idr_lock);
>  	} else
>  		spin_unlock_irqrestore(&idr_lock, *flags);

I think we can make a simpler patch,

--- posix-timers.c~	2007-06-29 14:45:04.000000000 +0400
+++ posix-timers.c	2007-07-17 16:59:45.000000000 +0400
@@ -449,6 +449,9 @@ static void release_posix_timer(struct k
 		idr_remove(&posix_timers_id, tmr->it_id);
 		spin_unlock_irqrestore(&idr_lock, flags);
 	}
+
+	spin_unlock_wait(tmr->it_lock);
+
 	sigqueue_free(tmr->sigq);
 	if (unlikely(tmr->it_process) &&
 	    tmr->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID))


What do you think? Instead of complicating the lock_timer(), release_posix_timer()
just makes sure that nobody can "use" tmr.

(Actually, I am not sure this is my idea, perhaps something like above was suggested
 by somebody else, I forgot the discussion completely).

Oleg.


  parent reply	other threads:[~2007-07-17 13:05 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-07-13 18:19 POSIX timer deletion race? Jeremy Katz
2007-07-17  8:53 ` [PATCH] posix-timer: fix deletion race Thomas Gleixner
2007-07-17  9:57   ` Ingo Molnar
2007-07-17 18:29     ` Jeremy Katz
2007-07-17 13:07   ` Oleg Nesterov [this message]
2007-07-17 17:02     ` Thomas Gleixner
2007-07-17 18:39   ` Jeremy Katz
2007-07-17 20:17     ` Thomas Gleixner
2007-07-17 23:12       ` Jeremy Katz
2007-07-17 23:58         ` Jeremy Katz
2007-07-18  6:05           ` Thomas Gleixner
2007-07-18  6:58             ` Thomas Gleixner
2007-07-18 19:02               ` Jeremy Katz
2007-07-18 23:43                 ` Jeremy Katz
2007-07-19  5:50                   ` Thomas Gleixner
2007-07-19 19:24                     ` Jeremy Katz
2007-07-18 16:11           ` Oleg Nesterov
2007-07-18 19:08             ` Jeremy Katz
2007-07-20 14:15               ` Oleg Nesterov
2007-07-24  2:07                 ` Jeremy Katz
2007-07-24 14:51                   ` Oleg Nesterov
2007-07-24 18:36                     ` Jeremy Katz
2007-07-24 20:43                       ` Oleg Nesterov
2007-07-24 21:19                         ` Jeremy Katz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20070717130705.GA74@tv-sign.ru \
    --to=oleg@tv-sign.ru \
    --cc=akpm@osdl.org \
    --cc=jeremy.katz@windriver.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=stable@kernel.org \
    --cc=tglx@linutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox