alsa-devel.alsa-project.org archive mirror
 help / color / mirror / Atom feed
* [patch 00/11] hrtimers: Cleanup hrtimer_forward() [ab]use
@ 2021-09-23 16:04 Thomas Gleixner
  2021-09-23 16:04 ` [patch 04/11] ALSA: pcsp: Make hrtimer forwarding more robust Thomas Gleixner
  0 siblings, 1 reply; 3+ messages in thread
From: Thomas Gleixner @ 2021-09-23 16:04 UTC (permalink / raw)
  To: LKML
  Cc: alsa-devel, Sergey Ryazanov, Peter Zijlstra, Joonas Lahtinen,
	dri-devel, Intel Corporation, David Airlie, Jakub Kicinski,
	Johannes Berg, linux-pm, intel-gfx, Jani Nikula, linux-can,
	Marc Kleine-Budde, Sebastian Reichel, Rodrigo Vivi, Dmitry Vyukov,
	Loic Poulain, netdev, Takashi Iwai, Eric W. Biederman,
	Daniel Vetter, Oliver Hartkopp, M Chetan Kumar, Johannes Berg,
	David S. Miller

A recent syzbot report unearthed abuse of hrtimer_forward() which can cause
runaway timers hogging the CPU in timer expiry context by rearming the
timer in the past over and over.

This happens when the caller uses timer->expiry for the 'now' argument of
hrtimer_forward(). That works as long as the timer expiry is on time, but
can cause a long period of rearm/fire loops which hog the CPU. Expiring
late can have various causes, but obviously virtualization is prone to that
due to VCPU scheduling.

The correct usage of hrtimer_forward() is to hand the current time to the
'now' argument which ensures that the next event on the periodic time line
is past now. This is what hrtimer_forward_now() provides.

The following series addresses this:

    1) Add a debug mechanism to the hrtimer expiry loop

    2) Convert all hrtimer_forward() usage outside of kernel/time/ to
       use hrtimer_forward_now().

    3) Confine hrtimer_forward() to kernel/time/ core code.

The mac80211_hwsim patch has already been picked up by the wireless
maintainer and all other patches which affect usage outside the core code
can be picked up by the relevant subsystems. If a maintainer wants me to
pick a particular patch up, please let me know.

The last patch which confines hrtimer_forward() will be postponed until all
other patches have been merged into Linus tree.

The series is also available from git:

    git://git.kernel.org/pub/scm/linux/kernel/git/tglx/devel.git hrtimer

Thanks,

	tglx
---
 drivers/gpu/drm/i915/i915_pmu.c        |    2 -
 drivers/net/wireless/mac80211_hwsim.c  |    4 +-
 drivers/net/wwan/iosm/iosm_ipc_imem.c  |    4 +-
 drivers/power/reset/ltc2952-poweroff.c |    4 --
 include/linux/hrtimer.h                |   26 -----------------
 include/linux/posix-timers.h           |    3 ++
 kernel/signal.c                        |   14 +--------
 kernel/time/hrtimer.c                  |   48 ++++++++++++++++++++++++++++++++-
 kernel/time/itimer.c                   |   13 ++++++++
 kernel/time/posix-timers.c             |   42 +++++++++++-----------------
 kernel/time/tick-internal.h            |    1 
 net/can/bcm.c                          |    2 -
 sound/drivers/pcsp/pcsp_lib.c          |    2 -
 13 files changed, 92 insertions(+), 73 deletions(-)


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

* [patch 04/11] ALSA: pcsp: Make hrtimer forwarding more robust
  2021-09-23 16:04 [patch 00/11] hrtimers: Cleanup hrtimer_forward() [ab]use Thomas Gleixner
@ 2021-09-23 16:04 ` Thomas Gleixner
  2021-09-28  8:58   ` Takashi Iwai
  0 siblings, 1 reply; 3+ messages in thread
From: Thomas Gleixner @ 2021-09-23 16:04 UTC (permalink / raw)
  To: LKML; +Cc: Peter Zijlstra, alsa-devel, Takashi Iwai

The hrtimer callback pcsp_do_timer() prepares rearming of the timer with
hrtimer_forward(). hrtimer_forward() is intended to provide a mechanism to
forward the expiry time of the hrtimer by a multiple of the period argument
so that the expiry time greater than the time provided in the 'now'
argument.

pcsp_do_timer() invokes hrtimer_forward() with the current timer expiry
time as 'now' argument. That's providing a periodic timer expiry, but is
not really robust when the timer callback is delayed so that the resulting
new expiry time is already in the past which causes the callback to be
invoked immediately again. If the timer is delayed then the back to back
invocation is not really making it better than skipping the missed
periods. Sound is distorted in any case.

Use hrtimer_forward_now() which ensures that the next expiry is in the
future. This prevents hogging the CPU in the timer expiry code and allows
later on to remove hrtimer_forward() from the public interfaces.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Cc: alsa-devel@alsa-project.org
Cc: Takashi Iwai <tiwai@suse.com>
Cc: Jaroslav Kysela <perex@perex.cz>
---
 sound/drivers/pcsp/pcsp_lib.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

--- a/sound/drivers/pcsp/pcsp_lib.c
+++ b/sound/drivers/pcsp/pcsp_lib.c
@@ -143,7 +143,7 @@ enum hrtimer_restart pcsp_do_timer(struc
 	if (pointer_update)
 		pcsp_pointer_update(chip);
 
-	hrtimer_forward(handle, hrtimer_get_expires(handle), ns_to_ktime(ns));
+	hrtimer_forward_now(handle, ns_to_ktime(ns));
 
 	return HRTIMER_RESTART;
 }


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

* Re: [patch 04/11] ALSA: pcsp: Make hrtimer forwarding more robust
  2021-09-23 16:04 ` [patch 04/11] ALSA: pcsp: Make hrtimer forwarding more robust Thomas Gleixner
@ 2021-09-28  8:58   ` Takashi Iwai
  0 siblings, 0 replies; 3+ messages in thread
From: Takashi Iwai @ 2021-09-28  8:58 UTC (permalink / raw)
  To: Thomas Gleixner; +Cc: Peter Zijlstra, alsa-devel, LKML, Takashi Iwai

On Thu, 23 Sep 2021 18:04:25 +0200,
Thomas Gleixner wrote:
> 
> The hrtimer callback pcsp_do_timer() prepares rearming of the timer with
> hrtimer_forward(). hrtimer_forward() is intended to provide a mechanism to
> forward the expiry time of the hrtimer by a multiple of the period argument
> so that the expiry time greater than the time provided in the 'now'
> argument.
> 
> pcsp_do_timer() invokes hrtimer_forward() with the current timer expiry
> time as 'now' argument. That's providing a periodic timer expiry, but is
> not really robust when the timer callback is delayed so that the resulting
> new expiry time is already in the past which causes the callback to be
> invoked immediately again. If the timer is delayed then the back to back
> invocation is not really making it better than skipping the missed
> periods. Sound is distorted in any case.
> 
> Use hrtimer_forward_now() which ensures that the next expiry is in the
> future. This prevents hogging the CPU in the timer expiry code and allows
> later on to remove hrtimer_forward() from the public interfaces.
> 
> Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
> Cc: alsa-devel@alsa-project.org
> Cc: Takashi Iwai <tiwai@suse.com>
> Cc: Jaroslav Kysela <perex@perex.cz>

Thanks, applied now to sound git tree.


Takashi

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

end of thread, other threads:[~2021-09-28  8:59 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-09-23 16:04 [patch 00/11] hrtimers: Cleanup hrtimer_forward() [ab]use 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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).