public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Kirill Tkhai <tkhai@yandex.ru>
To: Peter Zijlstra <peterz@infradead.org>
Cc: "umgwanakikbuti@gmail.com" <umgwanakikbuti@gmail.com>,
	"mingo@elte.hu" <mingo@elte.hu>,
	"ktkhai@parallels.com" <ktkhai@parallels.com>,
	"rostedt@goodmis.org" <rostedt@goodmis.org>,
	"tglx@linutronix.de" <tglx@linutronix.de>,
	"juri.lelli@gmail.com" <juri.lelli@gmail.com>,
	"pang.xunlei@linaro.org" <pang.xunlei@linaro.org>,
	"oleg@redhat.com" <oleg@redhat.com>,
	"wanpeng.li@linux.intel.com" <wanpeng.li@linux.intel.com>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH 8/9] hrtimer: Allow hrtimer::function() to free the timer
Date: Thu, 04 Jun 2015 12:07:03 +0300	[thread overview]
Message-ID: <2134411433408823@web8j.yandex.ru> (raw)
In-Reply-To: <20150603211324.GC3644@twins.programming.kicks-ass.net>

В Ср, 03/06/2015 в 23:13 +0200, Peter Zijlstra пишет:
On Wed, Jun 03, 2015 at 07:26:00PM +0300, Kirill Tkhai wrote:
> > > @@ -402,7 +394,8 @@ extern u64 hrtimer_get_next_event(void);
> > >   */
> > >  static inline int hrtimer_active(const struct hrtimer *timer)
> > >  {
> > > - return timer->state != HRTIMER_STATE_INACTIVE;
> > > + return timer->state != HRTIMER_STATE_INACTIVE ||
> > > + timer->base->running == timer;
> > >  }
> > 
> > It seems to be not good, because hrtimer_active() check stops
> > to be atomic. So the things like hrtimer_try_to_cancel() race
> > with a callback of self-rearming timer and may return a false
> > positive result.
> 
> Hurm.. the below isn't really pretty either I suppose. The best I can
> say about it is that's its not too expensive on x86.
> 
> I should probably go sleep..
> 
> --- a/include/linux/hrtimer.h
> +++ b/include/linux/hrtimer.h
> @@ -391,11 +391,25 @@ extern u64 hrtimer_get_next_event(void);
>   * A timer is active, when it is enqueued into the rbtree or the
>   * callback function is running or it's in the state of being migrated
>   * to another cpu.
> + *
> + * See __run_hrtimer().
>   */
> -static inline int hrtimer_active(const struct hrtimer *timer)
> +static inline bool hrtimer_active(const struct hrtimer *timer)
>  {
> -	return timer->state != HRTIMER_STATE_INACTIVE ||
> -		timer->base->running == timer;
> +	if (timer->state != HRTIMER_STATE_INACTIVE)
> +		return true;
> +
> +	smp_rmb(); /* C matches A */
> +
> +	if (timer->base->running == timer)
> +		return true;
> +
> +	smp_rmb(); /* D matches B */
> +
> +	if (timer->state != HRTIMER_STATE_INACTIVE)
> +		return true;
> +
> +	return false;

This races with two sequential timer handlers. hrtimer_active()
is preemptible everywhere, and no guarantees that all three "if"
conditions check the same timer tick.

How about transformation of hrtimer_bases.lock: raw_spinlock_t --> seqlock_t?

>  }
>  
>  /*
> --- a/kernel/time/hrtimer.c
> +++ b/kernel/time/hrtimer.c
> @@ -1122,6 +1122,20 @@ static void __run_hrtimer(struct hrtimer
>  
>  	debug_deactivate(timer);
>  	base->running = timer;
> +
> +	/*
> +	 * Pairs with hrtimer_active().
> +	 *
> +	 *	[S] base->running = timer	[L] timer->state
> +	 *	    WMB 			    RMB
> +	 *	[S] timer->state = INACTIVE	[L] base->running
> +	 *
> +	 * BUG_ON(base->running != timer && timer->state != INACTIVE)
> +	 *
> +	 * If we observe INACTIVE we must observe base->running == timer.
> +	 */
> +	smp_wmb(); /* A matches C */
> +
>  	__remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE, 0);
>  	timer_stats_account_hrtimer(timer);
>  	fn = timer->function;
> @@ -1150,6 +1164,20 @@ static void __run_hrtimer(struct hrtimer
>  	    !(timer->state & HRTIMER_STATE_ENQUEUED))
>  		enqueue_hrtimer(timer, base);
>  
> +	/*
> +	 * Pairs with hrtimer_active().
> +	 *
> +	 *	[S] timer->state = ENQUEUED	[L] base->running
> +	 *	    WMB 			    RMB
> +	 *	[S] base->running = NULL	[L] timer->state
> +	 *
> +	 * BUG_ON(base->running == NULL && timer->state == INACTIVE)
> +	 *
> +	 * If we observe base->running == NULL, we must observe any preceding
> +	 * enqueue.
> +	 */
> +	smp_wmb(); /* B matches D */
> +
>  	WARN_ON_ONCE(base->running != timer);
>  	base->running = NULL;
>  }
> 

  reply	other threads:[~2015-06-04  9:15 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-03 13:29 [PATCH 0/9] sched: balance callbacks Peter Zijlstra
2015-06-03 13:29 ` [PATCH 1/9] sched: Replace post_schedule with a balance callback list Peter Zijlstra
2015-06-03 13:29 ` [PATCH 2/9] sched: Use replace normalize_task() with __sched_setscheduler() Peter Zijlstra
2015-06-03 13:29 ` [PATCH 3/9] sched: Allow balance callbacks for check_class_changed() Peter Zijlstra
2015-06-03 13:29 ` [PATCH 4/9] sched,rt: Remove return value from pull_rt_task() Peter Zijlstra
2015-06-03 13:29 ` [PATCH 5/9] sched,rt: Convert switched_{from,to}_rt() / prio_changed_rt() to balance callbacks Peter Zijlstra
2015-06-03 13:29 ` [PATCH 6/9] sched,dl: Remove return value from pull_dl_task() Peter Zijlstra
2015-06-03 13:29 ` [PATCH 7/9] sched,dl: Convert switched_{from,to}_dl() / prio_changed_dl() to balance callbacks Peter Zijlstra
2015-06-03 13:29 ` [PATCH 8/9] hrtimer: Allow hrtimer::function() to free the timer Peter Zijlstra
2015-06-03 16:26   ` Kirill Tkhai
2015-06-03 21:13     ` Peter Zijlstra
2015-06-04  9:07       ` Kirill Tkhai [this message]
2015-06-04 10:49         ` Peter Zijlstra
2015-06-04 10:55           ` Peter Zijlstra
2015-06-04 10:58             ` Peter Zijlstra
2015-06-05  9:02           ` Kirill Tkhai
2015-06-05  9:03             ` Kirill Tkhai
2015-06-05  9:11               ` Peter Zijlstra
2015-06-05  9:10             ` Peter Zijlstra
2015-06-05  9:27               ` Kirill Tkhai
2015-06-03 17:41   ` Thomas Gleixner
2015-06-03 21:29     ` Peter Zijlstra
2015-06-04  5:59       ` Ingo Molnar
2015-06-04 10:07         ` Peter Zijlstra
2015-06-04 12:37           ` Ingo Molnar
2015-06-03 13:29 ` [PATCH 9/9] sched,dl: Fix sched class hopping CBS hole Peter Zijlstra

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=2134411433408823@web8j.yandex.ru \
    --to=tkhai@yandex.ru \
    --cc=juri.lelli@gmail.com \
    --cc=ktkhai@parallels.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=oleg@redhat.com \
    --cc=pang.xunlei@linaro.org \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=tglx@linutronix.de \
    --cc=umgwanakikbuti@gmail.com \
    --cc=wanpeng.li@linux.intel.com \
    /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