From: Thomas Gleixner <tglx@linutronix.de>
To: Balasubramani Vivekanandan <balasubramani_vivekanandan@mentor.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>,
Ingo Molnar <mingo@kernel.org>,
erosca@de.adit-jv.com, LKML <linux-kernel@vger.kernel.org>,
Peter Zijlstra <peterz@infradead.org>
Subject: Re: [PATCH V1 1/1] tick: broadcast-hrtimer: Fix a race in bc_set_next
Date: Mon, 23 Sep 2019 21:51:54 +0200 (CEST) [thread overview]
Message-ID: <alpine.DEB.2.21.1909232041080.1934@nanos.tec.linutronix.de> (raw)
In-Reply-To: <20190918144138.24839-2-balasubramani_vivekanandan@mentor.com>
On Wed, 18 Sep 2019, Balasubramani Vivekanandan wrote:
>
> When there are no more cpus subscribed to broadcast, the timer callback
> might not set the expiry time for hrtimer. Therefore the callback timer
> function is modified to set the state of broadcast clock to
> CLOCK_EVT_STATE_ONESHOT_STOPPED which in turn will set the expiry time
> of hrtimer to KTIME_MAX.
That's an ugly layering violation, really.
Aside of that the whole try to cancel logic and the comment that the
hrtimer cannot be armed from within the callback is wrong. All of this can
go way.
Find a completely untested patch below.
Thanks,
tglx
8<---------------------
--- a/kernel/time/tick-broadcast-hrtimer.c
+++ b/kernel/time/tick-broadcast-hrtimer.c
@@ -42,39 +42,35 @@ static int bc_shutdown(struct clock_even
*/
static int bc_set_next(ktime_t expires, struct clock_event_device *bc)
{
- int bc_moved;
/*
- * We try to cancel the timer first. If the callback is on
- * flight on some other cpu then we let it handle it. If we
- * were able to cancel the timer nothing can rearm it as we
- * own broadcast_lock.
+ * This is called either from enter/exit idle code or from the
+ * broadcast handler. In all cases tick_broadcast_lock is held.
*
- * However we can also be called from the event handler of
- * ce_broadcast_hrtimer itself when it expires. We cannot
- * restart the timer because we are in the callback, but we
- * can set the expiry time and let the callback return
- * HRTIMER_RESTART.
+ * hrtimer_cancel() cannot be called here neither from the
+ * broadcast handler nor from the enter/exit idle code. The idle
+ * code can run into the problem described in bc_shutdown() and the
+ * broadcast handler cannot wait for itself to complete for obvious
+ * reasons.
*
- * Since we are in the idle loop at this point and because
- * hrtimer_{start/cancel} functions call into tracing,
- * calls to these functions must be bound within RCU_NONIDLE.
+ * Each caller tries to arm the hrtimer on its own CPU, but if the
+ * handler is currently running hrtimer_start() does not move
+ * it. It keeps it on the CPU on which it is assigned at the
+ * moment.
*/
- RCU_NONIDLE(
- {
- bc_moved = hrtimer_try_to_cancel(&bctimer) >= 0;
- if (bc_moved) {
- hrtimer_start(&bctimer, expires,
- HRTIMER_MODE_ABS_PINNED_HARD);
- }
- }
- );
-
- if (bc_moved) {
- /* Bind the "device" to the cpu */
- bc->bound_on = smp_processor_id();
- } else if (bc->bound_on == smp_processor_id()) {
- hrtimer_set_expires(&bctimer, expires);
- }
+ RCU_NONIDLE( {
+ hrtimer_start(&bctimer, expires, HRTIMER_MODE_ABS_PINNED_HARD);
+ /*
+ * The core tick broadcast mode expects bc->bound_on to be set
+ * correctly to prevent a CPU which has the broadcast hrtimer
+ * armed from going deep idle.
+ *
+ * As tick_broadcast_lock is held, nothing can change the
+ * cpu base which was just established in hrtimer_start()
+ * above. So the below access is safe even without holding
+ * the hrtimer base lock.
+ */
+ bc->bound_on = bctimer.base->cpu_base->cpu;
+ } );
return 0;
}
@@ -100,10 +96,6 @@ static enum hrtimer_restart bc_handler(s
{
ce_broadcast_hrtimer.event_handler(&ce_broadcast_hrtimer);
- if (clockevent_state_oneshot(&ce_broadcast_hrtimer))
- if (ce_broadcast_hrtimer.next_event != KTIME_MAX)
- return HRTIMER_RESTART;
-
return HRTIMER_NORESTART;
}
next prev parent reply other threads:[~2019-09-23 19:52 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-09-18 14:41 [PATCH V1 0/1] " Balasubramani Vivekanandan
2019-09-18 14:41 ` [PATCH V1 1/1] " Balasubramani Vivekanandan
2019-09-23 19:51 ` Thomas Gleixner [this message]
2019-09-24 17:14 ` Eugeniu Rosca
2019-09-25 11:55 ` [PATCH V2 " Balasubramani Vivekanandan
2019-09-25 11:55 ` Balasubramani Vivekanandan
2019-09-25 13:32 ` Balasubramani Vivekanandan
2019-09-25 14:20 ` Balasubramani Vivekanandan
2019-09-25 14:20 ` Balasubramani Vivekanandan
2019-09-26 10:01 ` Thomas Gleixner
2019-09-26 13:51 ` [PATCH V3 " Balasubramani Vivekanandan
2019-09-26 13:51 ` Balasubramani Vivekanandan
2019-09-26 15:43 ` Eugeniu Rosca
2019-09-27 12:48 ` [tip: timers/core] " tip-bot2 for Balasubramani Vivekanandan
2019-09-20 10:39 ` [PATCH V1 0/1] " Eugeniu Rosca
2019-09-20 10:58 ` Eugeniu Rosca
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=alpine.DEB.2.21.1909232041080.1934@nanos.tec.linutronix.de \
--to=tglx@linutronix.de \
--cc=balasubramani_vivekanandan@mentor.com \
--cc=erosca@de.adit-jv.com \
--cc=fweisbec@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@kernel.org \
--cc=peterz@infradead.org \
/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
all inboxes | Powered by JetHome®