From: Thomas Gleixner <tglx@linutronix.de>
To: LKML <linux-kernel@vger.kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>,
Dmitry Vyukov <dvyukov@google.com>
Subject: [patch 01/11] hrtimer: Add a mechanism to catch runaway timers
Date: Thu, 23 Sep 2021 18:04:20 +0200 (CEST) [thread overview]
Message-ID: <20210923153339.437521634@linutronix.de> (raw)
In-Reply-To: <20210923153311.225307347@linutronix.de>
A recent report from syzbot unearthed a problem with self rearming timers
which fire late and try to catch up to now with a short period. That causes
the timer to be rearmed in the past until it eventually catches up with
now. If that rearming happens from the timer callback the hard or soft
interrupt expiry loop can run for a long time with either interrupts or
bottom halves disabled which causes RCU stalls and other lockups.
There is no safety net to stop or at least identify such runaway timers.
Detection is trivial. Cache the pointer to the last expired timer. The next
invocation from the same loop compares the pointer with the next expiring
hrtimer pointer and if they match 10 times in a row (in the same hard or
soft interrupt expiry instance) then it's reasonable to declare it as a
runaway.
In that case emit a warning and skip the callback invocation which stops
the misbehaving timer right there.
It's obviously incomplete, but it's definitely better than nothing and would
have caught the reported issue in mac80211_hwsim.
Suggested-by: Dmitry Vyukov <dvyukov@google.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
kernel/time/hrtimer.c | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)
--- a/kernel/time/hrtimer.c
+++ b/kernel/time/hrtimer.c
@@ -1714,6 +1714,13 @@ static void __run_hrtimer(struct hrtimer
base->running = NULL;
}
+static void hrtimer_del_runaway(struct hrtimer_clock_base *base,
+ struct hrtimer *timer)
+{
+ __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE, 0);
+ pr_warn("Runaway hrtimer %p %ps stopped\n", timer, timer->function);
+}
+
static void __hrtimer_run_queues(struct hrtimer_cpu_base *cpu_base, ktime_t now,
unsigned long flags, unsigned int active_mask)
{
@@ -1722,6 +1729,8 @@ static void __hrtimer_run_queues(struct
for_each_active_base(base, cpu_base, active) {
struct timerqueue_node *node;
+ struct hrtimer *last = NULL;
+ unsigned int cnt = 0;
ktime_t basenow;
basenow = ktime_add(now, base->offset);
@@ -1732,6 +1741,22 @@ static void __hrtimer_run_queues(struct
timer = container_of(node, struct hrtimer, node);
/*
+ * Catch timers which rearm themself with a expiry
+ * time in the past over and over which makes this
+ * loop run forever.
+ */
+ if (IS_ENABLED(CONFIG_DEBUG_OBJECTS_TIMERS)) {
+ if (unlikely(last == timer)) {
+ if (++cnt == 10) {
+ hrtimer_del_runaway(base, timer);
+ continue;
+ }
+ }
+ last = timer;
+ cnt = 0;
+ }
+
+ /*
* The immediate goal for using the softexpires is
* minimizing wakeups, not running timers at the
* earliest interrupt after their soft expiration.
next prev parent reply other threads:[~2021-09-23 16:04 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-09-23 16:04 [patch 00/11] hrtimers: Cleanup hrtimer_forward() [ab]use Thomas Gleixner
2021-09-23 16:04 ` Thomas Gleixner [this message]
2021-09-24 8:16 ` [patch 01/11] hrtimer: Add a mechanism to catch runaway timers Dmitry Vyukov
2021-09-23 16:04 ` [patch 02/11] mac80211-hwsim: Fix late beacon hrtimer handling Thomas Gleixner
2021-09-23 16:04 ` [patch 03/11] net: iosm: Use hrtimer_forward_now() Thomas Gleixner
2021-09-23 16:04 ` [patch 04/11] ALSA: pcsp: Make hrtimer forwarding more robust Thomas Gleixner
2021-09-28 8:58 ` Takashi Iwai
2021-09-23 16:04 ` [patch 05/11] can: bcm: Use hrtimer_forward_now() Thomas Gleixner
2021-10-13 13:59 ` Marc Kleine-Budde
2021-09-23 16:04 ` [patch 06/11] power: reset: ltc2952: " Thomas Gleixner
2021-09-27 12:34 ` Sebastian Reichel
2021-09-23 16:04 ` [patch 07/11] drm/i915/pmu: " Thomas Gleixner
2021-09-24 9:03 ` [Intel-gfx] " Tvrtko Ursulin
2021-09-23 16:04 ` [patch 08/11] signal: Move itimer rearming into itimer code Thomas Gleixner
2021-09-23 16:04 ` [patch 09/11] posix-timers: Fixup stale commnt and reduce ifdeffery Thomas Gleixner
2021-09-23 16:04 ` [patch 10/11] posix-timers: Use hrtimer_forward_now() Thomas Gleixner
2021-09-23 16:04 ` [patch 11/11] hrtimer: Make hrtimer_forward() private to core timer code Thomas Gleixner
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=20210923153339.437521634@linutronix.de \
--to=tglx@linutronix.de \
--cc=dvyukov@google.com \
--cc=linux-kernel@vger.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®