Skip to contentSkip to conversation

hrtimer: Provide softirq context hrtimers

PATCH Superseded Anna-Maria Gleixner 11 participants 74 messages Aug 31, 2017 – Dec 20, 2017

Subsystems (11)
Networking Network drivers USB Sound +7 more subsystems

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Anna-Maria Gleixner

author maintainer
#0 lore raw

cover hrtimer: Provide softirq context hrtimers

There are quite some places in the kernel which use a combination of hrtimers and tasklets to make use of the precise expiry of hrtimers, which schedule a tasklet to bring the actual function into softirq context.

This was introduced when the previous hrtimer softirq code was removed. That code was implemented by expiring the timer in hard irq context and then deferring the execution of the callback into softirq context. That caused a lot of pointless shuffling between the rbtree and a linked list.

In recent discussions it turned out that more potential users of hrtimers in softirq context might come up. Aside of that the RT patches need this functionality as well to defer hrtimers into softirq context if their callbacks are not interrupt safe on RT.

This series implements a new approach by adding SOFT_* clock ids and instead of doing the list shuffle, timers started with these clock ids are put into separate soft expiry hrtimer queues. These queues are evaluated only when the hardirq context detects that the first expiring timer in the softirq queues has expired. That makes the overhead in the hardirq context minimal.

The series reworks the code to reuse as much as possible from the existing facilities for the new softirq hrtimers and integrates them with all flavours of hrtimers (HIGH_RES=y/n - NOHZ=y/n).

To achieve this quite some of the conditionals in the existing code are removed for the price of adding some pointless data and state tracking to the HIGH_RES=n case. That's minimal, but well worth it as it increases the readability and maintainability of the code.

The first part of the series implements the new functionality and the second part converts the hrtimer/tasklet users to make use of it and removes struct hrtimer_tasklet and the surrounding helper functions.

This series is available from git as well:

git://git.kernel.org/pub/scm/linux/kernel/git/bigeasy/linux-hrtimer.git soft_hrtimer
https://git.kernel.org/bigeasy/linux-hrtimer/h/soft_hrtimer

Thanks,

Anna-Maria

---
 drivers/net/usb/cdc_ncm.c             |   37 +-
 drivers/net/wireless/mac80211_hwsim.c |   44 +-
 drivers/usb/gadget/function/f_ncm.c   |   28 -
 include/linux/hrtimer.h               |   76 ++---
 include/linux/interrupt.h             |   25 -
 include/linux/usb/cdc_ncm.h           |    2 
 include/net/xfrm.h                    |    2 
 kernel/softirq.c                      |   51 ---
 kernel/time/hrtimer.c                 |  513 +++++++++++++++++++++-------------
 net/can/bcm.c                         |  150 +++------
 net/xfrm/xfrm_state.c                 |   29 +
 sound/drivers/dummy.c                 |   16 -
 12 files changed, 484 insertions(+), 489 deletions(-)
author maintainer
#1 lore raw

PATCH 1/25 hrtimer: Use predefined function for updating next_timer

From: Anna-Maria Gleixner <[email protected]>

There already exist a function for updating the next_timer hrtimer_update_next_timer().

---
 kernel/time/hrtimer.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/time/hrtimer.c +1 -1
@@ -630,7 +630,7 @@ static void hrtimer_reprogram(struct hrt
 		return;
 
 	/* Update the pointer to the next expiring timer */
-	cpu_base->next_timer = timer;
+	hrtimer_update_next_timer(cpu_base, timer);
 
 	/*
 	 * If a hang was detected in the last timer interrupt then we
author maintainer
#2 lore raw

PATCH 2/25 hrtimer: Correct blantanly wrong comment

In reply to Anna-Maria Gleixner

From: Thomas Gleixner <[email protected]>

The protection of a hrtimer which runs its callback against migration to a different CPU has nothing to do with hard interrupt context.

The protection against migration of a hrtimer running the expiry callback is the pointer in the cpu_base which holds a pointer to the currently running timer. This pointer is evaluated in the code which potentially switches the timer base and makes sure it's kept on the CPU on which the callback is running.

---
 kernel/time/hrtimer.c |    6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/time/hrtimer.c +3 -3
@@ -1204,9 +1204,9 @@ static void __run_hrtimer(struct hrtimer
 		timer->is_rel = false;
 
 	/*
-	 * Because we run timers from hardirq context, there is no chance
-	 * they get migrated to another cpu, therefore its safe to unlock
-	 * the timer base.
+	 * The timer is marked as running in the cpu base, so it is
+	 * protected against migration to a different CPU even if the lock
+	 * is dropped.
 	 */
 	raw_spin_unlock(&cpu_base->lock);
 	trace_hrtimer_expire_entry(timer, now);
author maintainer
#3 lore raw

PATCH 3/25 hrtimer: Fix kerneldoc for struct hrtimer_cpu_base

In reply to Anna-Maria Gleixner

From: Anna-Maria Gleixner <[email protected]>

The sequence '/**' marks the start of a struct description. Add the missing second asterisk. While at it adapt the ordering of the struct members to the struct definition and document the purpose of expires_next more precisely.

---
 include/linux/hrtimer.h |    8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
include/linux/hrtimer.h +4 -4
@@ -144,7 +144,7 @@ enum  hrtimer_base_type {
 	HRTIMER_MAX_CLOCK_BASES,
 };
 
-/*
+/**
  * struct hrtimer_cpu_base - the per cpu clock bases
  * @lock:		lock protecting the base and associated clock bases
  *			and timers
@@ -155,12 +155,12 @@ enum  hrtimer_base_type {
  * @clock_was_set_seq:	Sequence counter of clock was set events
  * @migration_enabled:	The migration of hrtimers to other cpus is enabled
  * @nohz_active:	The nohz functionality is enabled
- * @expires_next:	absolute time of the next event which was scheduled
- *			via clock_set_next_event()
- * @next_timer:		Pointer to the first expiring timer
  * @in_hrtirq:		hrtimer_interrupt() is currently executing
  * @hres_active:	State of high resolution mode
  * @hang_detected:	The last hrtimer interrupt detected a hang
+ * @expires_next:	absolute time of the next event, is required for remote
+ *			hrtimer enqueue
+ * @next_timer:		Pointer to the first expiring timer
  * @nr_events:		Total number of hrtimer interrupt events
  * @nr_retries:		Total number of hrtimer interrupt retries
  * @nr_hangs:		Total number of hrtimer interrupt hangs
author maintainer
#4 lore raw

PATCH 4/25 hrtimer: Cleanup clock argument in schedule_hrtimeout_range_clock()

In reply to Anna-Maria Gleixner

From: Anna-Maria Gleixner <[email protected]>

schedule_hrtimeout_range_clock() uses an integer for the clock id instead of the predefined type "clockid_t". The ID of the clock is indicated in hrtimer code as clock_id. Therefore change the name of the variable as well to make it consistent.

While at it, clean up the description for the function parameters clock_id and mode. The clock modes and the clock ids are not restricted as the comment suggests.

No functional change.

---
 include/linux/hrtimer.h |    2 +-
 kernel/time/hrtimer.c   |    8 ++++----
 2 files changed, 5 insertions(+), 5 deletions(-)
include/linux/hrtimer.h +1 -1
@@ -466,7 +466,7 @@ extern int schedule_hrtimeout_range(ktim
 extern int schedule_hrtimeout_range_clock(ktime_t *expires,
 					  u64 delta,
 					  const enum hrtimer_mode mode,
-					  int clock);
+					  clockid_t clock_id);
 extern int schedule_hrtimeout(ktime_t *expires, const enum hrtimer_mode mode);
 
 /* Soft interrupt function to run the hrtimer queues: */
kernel/time/hrtimer.c +4 -4
@@ -1671,12 +1671,12 @@ void __init hrtimers_init(void)
  * schedule_hrtimeout_range_clock - sleep until timeout
  * @expires:	timeout value (ktime_t)
  * @delta:	slack in expires timeout (ktime_t)
- * @mode:	timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
- * @clock:	timer clock, CLOCK_MONOTONIC or CLOCK_REALTIME
+ * @mode:	timer mode
+ * @clock_id:	timer clock to be used
  */
 int __sched
 schedule_hrtimeout_range_clock(ktime_t *expires, u64 delta,
-			       const enum hrtimer_mode mode, int clock)
+			       const enum hrtimer_mode mode, clockid_t clock_id)
 {
 	struct hrtimer_sleeper t;
 
@@ -1697,7 +1697,7 @@ schedule_hrtimeout_range_clock(ktime_t *
 		return -EINTR;
 	}
 
-	hrtimer_init_on_stack(&t.timer, clock, mode);
+	hrtimer_init_on_stack(&t.timer, clock_id, mode);
 	hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
 
 	hrtimer_init_sleeper(&t, current);
author maintainer
#5 lore raw

PATCH 5/25 hrtimer: Switch for loop to _ffs() evaluation

In reply to Anna-Maria Gleixner

From: Anna-Maria Gleixner <[email protected]>

Looping over all clock bases to find active bits is suboptimal if not all bases are active.

Avoid this by converting it to a __ffs() evaluation.

---
 kernel/time/hrtimer.c |   18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
kernel/time/hrtimer.c +10 -8
@@ -465,17 +465,18 @@ static inline void hrtimer_update_next_t
 
 static ktime_t __hrtimer_get_next_event(struct hrtimer_cpu_base *cpu_base)
 {
-	struct hrtimer_clock_base *base = cpu_base->clock_base;
 	unsigned int active = cpu_base->active_bases;
 	ktime_t expires, expires_next = KTIME_MAX;
 
 	hrtimer_update_next_timer(cpu_base, NULL);
-	for (; active; base++, active >>= 1) {
+	while (active) {
+		unsigned int id = __ffs(active);
+		struct hrtimer_clock_base *base;
 		struct timerqueue_node *next;
 		struct hrtimer *timer;
 
-		if (!(active & 0x01))
-			continue;
+		active &= ~(1U << id);
+		base = cpu_base->clock_base + id;
 
 		next = timerqueue_getnext(&base->active);
 		timer = container_of(next, struct hrtimer, node);
@@ -1242,15 +1243,16 @@ static void __run_hrtimer(struct hrtimer
 
 static void __hrtimer_run_queues(struct hrtimer_cpu_base *cpu_base, ktime_t now)
 {
-	struct hrtimer_clock_base *base = cpu_base->clock_base;
 	unsigned int active = cpu_base->active_bases;
 
-	for (; active; base++, active >>= 1) {
+	while (active) {
+		unsigned int id = __ffs(active);
+		struct hrtimer_clock_base *base;
 		struct timerqueue_node *node;
 		ktime_t basenow;
 
-		if (!(active & 0x01))
-			continue;
+		active &= ~(1U << id);
+		base = cpu_base->clock_base + id;
 
 		basenow = ktime_add(now, base->offset);
 
author maintainer
#6 lore raw

PATCH 6/25 hrtimer: Store running timer in hrtimer_clock_base

In reply to Anna-Maria Gleixner

From: Anna-Maria Gleixner <[email protected]>

The pointer to the currently running timer is stored in hrtimer_cpu_base before the base lock is dropped and the callback is invoked.

This results in two levels of indirections and the upcoming support for softirq based hrtimer requires splitting the "running" storage into soft and hard irq context expiry.

Storing both in the cpu base would require conditionals in all code paths accessing that information.

It's possible to have a per clock base sequence count and running pointer without changing the semantics of the related mechanisms because the timer base pointer cannot be changed while a timer is running the callback.

Unfortunately this makes cpu_clock base larger than 32 bytes on 32bit kernels. Instead of having huge gaps due to alignment, remove the alignment and let the compiler pack cpu base for 32bit. The resulting cache access patterns are fortunately not really different from the current behaviour. On 64bit kernels the 64byte alignment stays and the behaviour is unchanged.

---
 include/linux/hrtimer.h |   20 +++++++++-----------
 kernel/time/hrtimer.c   |   28 +++++++++++++---------------
 2 files changed, 22 insertions(+), 26 deletions(-)
include/linux/hrtimer.h +9 -11
@@ -112,9 +112,9 @@ struct hrtimer_sleeper {
 };
 
 #ifdef CONFIG_64BIT
-# define HRTIMER_CLOCK_BASE_ALIGN	64
+# define __hrtimer_clock_base_align	____cacheline_aligned
 #else
-# define HRTIMER_CLOCK_BASE_ALIGN	32
+# define __hrtimer_clock_base_align
 #endif
 
 /**
@@ -123,18 +123,22 @@ struct hrtimer_sleeper {
  * @index:		clock type index for per_cpu support when moving a
  *			timer to a base on another cpu.
  * @clockid:		clock id for per_cpu support
+ * @seq:		seqcount around __run_hrtimer
+ * @running:		pointer to the currently running hrtimer
  * @active:		red black tree root node for the active timers
  * @get_time:		function to retrieve the current time of the clock
  * @offset:		offset of this clock to the monotonic base
  */
 struct hrtimer_clock_base {
 	struct hrtimer_cpu_base	*cpu_base;
-	int			index;
+	unsigned int		index;
 	clockid_t		clockid;
+	seqcount_t		seq;
+	struct hrtimer		*running;
 	struct timerqueue_head	active;
 	ktime_t			(*get_time)(void);
 	ktime_t			offset;
-} __attribute__((__aligned__(HRTIMER_CLOCK_BASE_ALIGN)));
+} __hrtimer_clock_base_align;
 
 enum  hrtimer_base_type {
 	HRTIMER_BASE_MONOTONIC,
@@ -148,8 +152,6 @@ enum  hrtimer_base_type {
  * struct hrtimer_cpu_base - the per cpu clock bases
  * @lock:		lock protecting the base and associated clock bases
  *			and timers
- * @seq:		seqcount around __run_hrtimer
- * @running:		pointer to the currently running hrtimer
  * @cpu:		cpu number
  * @active_bases:	Bitfield to mark bases with active timers
  * @clock_was_set_seq:	Sequence counter of clock was set events
@@ -173,8 +175,6 @@ enum  hrtimer_base_type {
  */
 struct hrtimer_cpu_base {
 	raw_spinlock_t			lock;
-	seqcount_t			seq;
-	struct hrtimer			*running;
 	unsigned int			cpu;
 	unsigned int			active_bases;
 	unsigned int			clock_was_set_seq;
@@ -196,8 +196,6 @@ struct hrtimer_cpu_base {
 
 static inline void hrtimer_set_expires(struct hrtimer *timer, ktime_t time)
 {
-	BUILD_BUG_ON(sizeof(struct hrtimer_clock_base) > HRTIMER_CLOCK_BASE_ALIGN);
-
 	timer->node.expires = time;
 	timer->_softexpires = time;
 }
@@ -422,7 +420,7 @@ static inline int hrtimer_is_queued(stru
  */
 static inline int hrtimer_callback_running(struct hrtimer *timer)
 {
-	return timer->base->cpu_base->running == timer;
+	return timer->base->running == timer;
 }
 
 /* Forward a hrtimer so it expires after now: */
kernel/time/hrtimer.c +13 -15
@@ -70,7 +70,6 @@
 DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
 {
 	.lock = __RAW_SPIN_LOCK_UNLOCKED(hrtimer_bases.lock),
-	.seq = SEQCNT_ZERO(hrtimer_bases.seq),
 	.clock_base =
 	{
 		{
@@ -118,7 +117,6 @@ static const int hrtimer_clock_to_base_t
  * timer->base->cpu_base
  */
 static struct hrtimer_cpu_base migration_cpu_base = {
-	.seq = SEQCNT_ZERO(migration_cpu_base),
 	.clock_base = { { .cpu_base = &migration_cpu_base, }, },
 };
 
@@ -1136,19 +1134,19 @@ EXPORT_SYMBOL_GPL(hrtimer_init);
  */
 bool hrtimer_active(const struct hrtimer *timer)
 {
-	struct hrtimer_cpu_base *cpu_base;
+	struct hrtimer_clock_base *base;
 	unsigned int seq;
 
 	do {
-		cpu_base = READ_ONCE(timer->base->cpu_base);
-		seq = raw_read_seqcount_begin(&cpu_base->seq);
+		base = READ_ONCE(timer->base);
+		seq = raw_read_seqcount_begin(&base->seq);
 
 		if (timer->state != HRTIMER_STATE_INACTIVE ||
-		    cpu_base->running == timer)
+		    base->running == timer)
 			return true;
 
-	} while (read_seqcount_retry(&cpu_base->seq, seq) ||
-		 cpu_base != READ_ONCE(timer->base->cpu_base));
+	} while (read_seqcount_retry(&base->seq, seq) ||
+		 base != READ_ONCE(timer->base));
 
 	return false;
 }
@@ -1182,16 +1180,16 @@ static void __run_hrtimer(struct hrtimer
 	lockdep_assert_held(&cpu_base->lock);
 
 	debug_deactivate(timer);
-	cpu_base->running = timer;
+	base->running = timer;
 
 	/*
 	 * Separate the ->running assignment from the ->state assignment.
 	 *
 	 * As with a regular write barrier, this ensures the read side in
-	 * hrtimer_active() cannot observe cpu_base->running == NULL &&
+	 * hrtimer_active() cannot observe base->running == NULL &&
 	 * timer->state == INACTIVE.
 	 */
-	raw_write_seqcount_barrier(&cpu_base->seq);
+	raw_write_seqcount_barrier(&base->seq);
 
 	__remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE, 0);
 	fn = timer->function;
@@ -1232,13 +1230,13 @@ static void __run_hrtimer(struct hrtimer
 	 * Separate the ->running assignment from the ->state assignment.
 	 *
 	 * As with a regular write barrier, this ensures the read side in
-	 * hrtimer_active() cannot observe cpu_base->running == NULL &&
+	 * hrtimer_active() cannot observe base->running.timer == NULL &&
 	 * timer->state == INACTIVE.
 	 */
-	raw_write_seqcount_barrier(&cpu_base->seq);
+	raw_write_seqcount_barrier(&base->seq);
 
-	WARN_ON_ONCE(cpu_base->running != timer);
-	cpu_base->running = NULL;
+	WARN_ON_ONCE(base->running != timer);
+	base->running = NULL;
 }
 
 static void __hrtimer_run_queues(struct hrtimer_cpu_base *cpu_base, ktime_t now)
#7 lore raw

Re: [PATCH 06/25] hrtimer: Store running timer in hrtimer_clock_base

Quoted 5 lines · Anna-Maria Gleixner

Unfortunately this makes cpu_clock base larger than 32 bytes on 32bit kernels. Instead of having huge gaps due to alignment, remove the alignment and let the compiler pack cpu base for 32bit. The resulting cache access patterns are fortunately not really different from the current behaviour.

How did you determine this? Perf lacks much useful tooling right about there :/

#8 lore raw

Re: [PATCH 06/25] hrtimer: Store running timer in hrtimer_clock_base

Quoted 9 lines · Peter Zijlstra
Quoted 5 lines · Anna-Maria Gleixner

Unfortunately this makes cpu_clock base larger than 32 bytes on 32bit kernels. Instead of having huge gaps due to alignment, remove the alignment and let the compiler pack cpu base for 32bit. The resulting cache access patterns are fortunately not really different from the current behaviour.

How did you determine this? Perf lacks much useful tooling right about there :/

Just by analyzing the resulting layout and looking at the number of cache lines involved for the frequently used clocks (mono/real).

Thanks,

tglx

author maintainer
#9 lore raw

PATCH 7/25 hrtimer: Reduce conditional code (hres_active)

In reply to Anna-Maria Gleixner

The hrtimer_cpu_base struct has the CONFIG_HIGH_RES_TIMERS conditional struct member hres_active. All related functions to this member are conditional as well.

There is no functional change, when the hres_active member is unconditional with all related functions and is set to zero during initialization. This makes the code easier to read.

---
 include/linux/hrtimer.h |   17 ++++++-----------
 kernel/time/hrtimer.c   |   30 ++++++++++++++----------------
 2 files changed, 20 insertions(+), 27 deletions(-)
include/linux/hrtimer.h +6 -11
@@ -180,9 +180,9 @@ struct hrtimer_cpu_base {
 	unsigned int			clock_was_set_seq;
 	bool				migration_enabled;
 	bool				nohz_active;
+	unsigned int			hres_active	: 1;
 #ifdef CONFIG_HIGH_RES_TIMERS
 	unsigned int			in_hrtirq	: 1,
-					hres_active	: 1,
 					hang_detected	: 1;
 	ktime_t				expires_next;
 	struct hrtimer			*next_timer;
@@ -264,16 +264,16 @@ static inline ktime_t hrtimer_cb_get_tim
 	return timer->base->get_time();
 }
 
-#ifdef CONFIG_HIGH_RES_TIMERS
-struct clock_event_device;
-
-extern void hrtimer_interrupt(struct clock_event_device *dev);
-
 static inline int hrtimer_is_hres_active(struct hrtimer *timer)
 {
 	return timer->base->cpu_base->hres_active;
 }
 
+#ifdef CONFIG_HIGH_RES_TIMERS
+struct clock_event_device;
+
+extern void hrtimer_interrupt(struct clock_event_device *dev);
+
 /*
  * The resolution of the clocks. The resolution value is returned in
  * the clock_getres() system call to give application programmers an
@@ -296,11 +296,6 @@ extern unsigned int hrtimer_resolution;
 
 #define hrtimer_resolution	(unsigned int)LOW_RES_NSEC
 
-static inline int hrtimer_is_hres_active(struct hrtimer *timer)
-{
-	return 0;
-}
-
 static inline void clock_was_set_delayed(void) { }
 
 #endif
kernel/time/hrtimer.c +14 -16
@@ -505,6 +505,19 @@ static inline ktime_t hrtimer_update_bas
 					    offs_real, offs_boot, offs_tai);
 }
 
+/*
+ * Is the high resolution mode active ?
+ */
+static inline int __hrtimer_hres_active(struct hrtimer_cpu_base *cpu_base)
+{
+	return cpu_base->hres_active;
+}
+
+static inline int hrtimer_hres_active(void)
+{
+	return __hrtimer_hres_active(this_cpu_ptr(&hrtimer_bases));
+}
+
 /* High resolution timer related functions */
 #ifdef CONFIG_HIGH_RES_TIMERS
 
@@ -534,19 +547,6 @@ static inline int hrtimer_is_hres_enable
 }
 
 /*
- * Is the high resolution mode active ?
- */
-static inline int __hrtimer_hres_active(struct hrtimer_cpu_base *cpu_base)
-{
-	return cpu_base->hres_active;
-}
-
-static inline int hrtimer_hres_active(void)
-{
-	return __hrtimer_hres_active(this_cpu_ptr(&hrtimer_bases));
-}
-
-/*
  * Reprogram the event source with checking both queues for the
  * next event
  * Called with interrupts disabled and base->lock held
@@ -654,7 +654,6 @@ static void hrtimer_reprogram(struct hrt
 static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
 {
 	base->expires_next = KTIME_MAX;
-	base->hres_active = 0;
 }
 
 /*
@@ -713,8 +712,6 @@ void clock_was_set_delayed(void)
 
 #else
 
-static inline int __hrtimer_hres_active(struct hrtimer_cpu_base *b) { return 0; }
-static inline int hrtimer_hres_active(void) { return 0; }
 static inline int hrtimer_is_hres_enabled(void) { return 0; }
 static inline void hrtimer_switch_to_hres(void) { }
 static inline void
@@ -1592,6 +1589,7 @@ int hrtimers_prepare_cpu(unsigned int cp
 	}
 
 	cpu_base->cpu = cpu;
+	cpu_base->hres_active = 0;
 	hrtimer_init_hres(cpu_base);
 	return 0;
 }
#10 lore raw

Re: [PATCH 07/25] hrtimer: Reduce conditional code (hres_active)

Quoted 7 lines · Anna-Maria Gleixner

The hrtimer_cpu_base struct has the CONFIG_HIGH_RES_TIMERS conditional struct member hres_active. All related functions to this member are conditional as well.

There is no functional change, when the hres_active member is unconditional with all related functions and is set to zero during initialization. This makes the code easier to read.

But you make what was a compile time dead-code-elimination into a runtime load-and-branch.

Unless the compiler is overly clever and is able to deduce from the lack of assignments that it must always be 0, is it?

#11 lore raw

Re: [PATCH 07/25] hrtimer: Reduce conditional code (hres_active)

Quoted 9 lines · Peter Zijlstra
Quoted 7 lines · Anna-Maria Gleixner

The hrtimer_cpu_base struct has the CONFIG_HIGH_RES_TIMERS conditional struct member hres_active. All related functions to this member are conditional as well.

There is no functional change, when the hres_active member is unconditional with all related functions and is set to zero during initialization. This makes the code easier to read.

Quoted 5 lines

But you make what was a compile time dead-code-elimination into a runtime load-and-branch.

Unless the compiler is overly clever and is able to deduce from the lack of assignments that it must always be 0, is it?

Probably not. So the simple way to solve this is to make the new common

is_highres_active()

{
	return IS_ENABLED(CONFIG_HIGH_RES) ? base->hres_active : 0;
}

Thanks,

tglx

author maintainer
#12 lore raw

PATCH 8/25 hrtimer: Reduce conditional code (expires_next, next_timer)

In reply to Anna-Maria Gleixner

The hrtimer_cpu_base struct member expires_next and next_timer are conditional members (CONFIG_HIGH_RES_TIMERS). This makes the hrtimer code more complex and harder to understand than it actually is.

Reduce the conditionals related to those two struct members.

---
 include/linux/hrtimer.h |   10 +++++-----
 kernel/time/hrtimer.c   |   24 +++++-------------------
 2 files changed, 10 insertions(+), 24 deletions(-)
include/linux/hrtimer.h +5 -5
@@ -160,13 +160,13 @@ enum  hrtimer_base_type {
  * @in_hrtirq:		hrtimer_interrupt() is currently executing
  * @hres_active:	State of high resolution mode
  * @hang_detected:	The last hrtimer interrupt detected a hang
- * @expires_next:	absolute time of the next event, is required for remote
- *			hrtimer enqueue
- * @next_timer:		Pointer to the first expiring timer
  * @nr_events:		Total number of hrtimer interrupt events
  * @nr_retries:		Total number of hrtimer interrupt retries
  * @nr_hangs:		Total number of hrtimer interrupt hangs
  * @max_hang_time:	Maximum time spent in hrtimer_interrupt
+ * @expires_next:	absolute time of the next event, is required for remote
+ *			hrtimer enqueue
+ * @next_timer:		Pointer to the first expiring timer
  * @clock_base:		array of clock bases for this cpu
  *
  * Note: next_timer is just an optimization for __remove_hrtimer().
@@ -184,13 +184,13 @@ struct hrtimer_cpu_base {
 #ifdef CONFIG_HIGH_RES_TIMERS
 	unsigned int			in_hrtirq	: 1,
 					hang_detected	: 1;
-	ktime_t				expires_next;
-	struct hrtimer			*next_timer;
 	unsigned int			nr_events;
 	unsigned int			nr_retries;
 	unsigned int			nr_hangs;
 	unsigned int			max_hang_time;
 #endif
+	ktime_t				expires_next;
+	struct hrtimer			*next_timer;
 	struct hrtimer_clock_base	clock_base[HRTIMER_MAX_CLOCK_BASES];
 } ____cacheline_aligned;
 
kernel/time/hrtimer.c +5 -19
@@ -154,16 +154,16 @@ struct hrtimer_clock_base *lock_hrtimer_
 }
 
 /*
- * With HIGHRES=y we do not migrate the timer when it is expiring
- * before the next event on the target cpu because we cannot reprogram
- * the target cpu hardware and we would cause it to fire late.
+ * With high resolution timers enabled we do not migrate the timer
+ * when it is expiring before the next event on the target cpu because
+ * we cannot reprogram the target cpu hardware and we would cause it
+ * to fire late.
  *
  * Called with cpu_base->lock of target cpu held.
  */
 static int
 hrtimer_check_target(struct hrtimer *timer, struct hrtimer_clock_base *new_base)
 {
-#ifdef CONFIG_HIGH_RES_TIMERS
 	ktime_t expires;
 
 	if (!new_base->cpu_base->hres_active)
@@ -171,9 +171,6 @@ hrtimer_check_target(struct hrtimer *tim
 
 	expires = ktime_sub(hrtimer_get_expires(timer), new_base->offset);
 	return expires <= new_base->cpu_base->expires_next;
-#else
-	return 0;
-#endif
 }
 
 #ifdef CONFIG_NO_HZ_COMMON
@@ -456,9 +453,7 @@ static inline void debug_deactivate(stru
 static inline void hrtimer_update_next_timer(struct hrtimer_cpu_base *cpu_base,
 					     struct hrtimer *timer)
 {
-#ifdef CONFIG_HIGH_RES_TIMERS
 	cpu_base->next_timer = timer;
-#endif
 }
 
 static ktime_t __hrtimer_get_next_event(struct hrtimer_cpu_base *cpu_base)
@@ -649,14 +644,6 @@ static void hrtimer_reprogram(struct hrt
 }
 
 /*
- * Initialize the high resolution related parts of cpu_base
- */
-static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
-{
-	base->expires_next = KTIME_MAX;
-}
-
-/*
  * Retrigger next event is called after clock was set
  *
  * Called with interrupts disabled via on_each_cpu()
@@ -721,7 +708,6 @@ static inline int hrtimer_reprogram(stru
 {
 	return 0;
 }
-static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
 static inline void retrigger_next_event(void *arg) { }
 
 #endif /* CONFIG_HIGH_RES_TIMERS */
@@ -1590,7 +1576,7 @@ int hrtimers_prepare_cpu(unsigned int cp
 
 	cpu_base->cpu = cpu;
 	cpu_base->hres_active = 0;
-	hrtimer_init_hres(cpu_base);
+	cpu_base->expires_next = KTIME_MAX;
 	return 0;
 }
 
#13 lore raw

Re: [PATCH 08/25] hrtimer: Reduce conditional code (expires_next, next_timer)

Quoted 5 lines · Anna-Maria Gleixner

The hrtimer_cpu_base struct member expires_next and next_timer are conditional members (CONFIG_HIGH_RES_TIMERS). This makes the hrtimer code more complex and harder to understand than it actually is.

Reduce the conditionals related to those two struct members.

So this grows the data structure for !HIGH_RES for easier code? We no longer care about archs that don't support HIGH_RES?

#14 lore raw

Re: [PATCH 08/25] hrtimer: Reduce conditional code (expires_next, next_timer)

Quoted 9 lines · Peter Zijlstra
Quoted 5 lines · Anna-Maria Gleixner

The hrtimer_cpu_base struct member expires_next and next_timer are conditional members (CONFIG_HIGH_RES_TIMERS). This makes the hrtimer code more complex and harder to understand than it actually is.

Reduce the conditionals related to those two struct members.

So this grows the data structure for !HIGH_RES for easier code? We no longer care about archs that don't support HIGH_RES?

We care, but there is a tradeoff between the required conditional code and the marginal storage overhead. Easier to follow code which does a few superflous things for the !highres case is certainly preferred.

Thanks,

tglx

author maintainer
#15 lore raw

PATCH 9/25 hrtimer: Reduce conditional code (hrtimer_reprogram())

In reply to Anna-Maria Gleixner

The hrtimer_reprogram() is currently required only when CONFIG_HIGH_RES_TIMERS is set. Additional bitfields of hrtimer_cpu_base struct are high resolution timer specific as well.

To simplify the hrtimer code, the behaviour of CONFIG_HIGH_RES_TIMERS and !CONFIG_HIGH_RES_TIMERS should be similar. As preparation for this, the function hrtimer_reprogram() and required hrtimer_cpu_base struct members are moved outside the conditional area.

---
 include/linux/hrtimer.h |    6 +-
 kernel/time/hrtimer.c   |  131 +++++++++++++++++++++++-------------------------
 2 files changed, 66 insertions(+), 71 deletions(-)
include/linux/hrtimer.h +3 -3
@@ -180,10 +180,10 @@ struct hrtimer_cpu_base {
 	unsigned int			clock_was_set_seq;
 	bool				migration_enabled;
 	bool				nohz_active;
-	unsigned int			hres_active	: 1;
-#ifdef CONFIG_HIGH_RES_TIMERS
-	unsigned int			in_hrtirq	: 1,
+	unsigned int			hres_active	: 1,
+					in_hrtirq	: 1,
 					hang_detected	: 1;
+#ifdef CONFIG_HIGH_RES_TIMERS
 	unsigned int			nr_events;
 	unsigned int			nr_retries;
 	unsigned int			nr_hangs;
kernel/time/hrtimer.c +63 -68

160 lines not on this page · raw message

#16 lore raw

Re: [PATCH 09/25] hrtimer: Reduce conditional code (hrtimer_reprogram())

Quoted 8 lines · Anna-Maria Gleixner

The hrtimer_reprogram() is currently required only when CONFIG_HIGH_RES_TIMERS is set. Additional bitfields of hrtimer_cpu_base struct are high resolution timer specific as well.

To simplify the hrtimer code, the behaviour of CONFIG_HIGH_RES_TIMERS and !CONFIG_HIGH_RES_TIMERS should be similar. As preparation for this, the function hrtimer_reprogram() and required hrtimer_cpu_base struct members are moved outside the conditional area.

This Changelog is impenetrable. That is, I've no idea what you're trying to say.

As to the patch, that makes hrtimer_reprogram exist unconditionally, but the only callsite (so far) is condition on hrtimer_is_hres_active(), which can only be true if HIGH_RES.

So what, except grow the image size, does this patch achieve?

author maintainer
#17 lore raw

PATCH 10/25 hrtimer: Make handling of hrtimer reprogramming and enqueuing not conditional

In reply to Anna-Maria Gleixner

The hrtimer_reprogramming, remote timer enqueuing and handling of the hrtimer_cpu_base struct member expires_next depend on the active high resolution timers. This makes the code harder to understand.

To simplify the code, the hrtimer reprogramming is now executed independently except for the real reprogramming part. The expires_next stores now the first enqueued timer. Due to the adaption of the check_target function, remote enqueuing is now only possible when the expiry time is after the currently first expiry time independent of the active high resolution timers.

---
 kernel/time/hrtimer.c |   31 ++++++++++++-------------------
 1 file changed, 12 insertions(+), 19 deletions(-)
kernel/time/hrtimer.c +12 -19
@@ -154,10 +154,11 @@ struct hrtimer_clock_base *lock_hrtimer_
 }
 
 /*
- * With high resolution timers enabled we do not migrate the timer
- * when it is expiring before the next event on the target cpu because
- * we cannot reprogram the target cpu hardware and we would cause it
- * to fire late.
+ * We do not migrate the timer when it is expiring before the next
+ * event on the target cpu. When high resolution is enabled, we cannot
+ * reprogram the target cpu hardware and we would cause it to fire
+ * late. To keep it simple, we handle the high resolution enabled and
+ * disabled case similar.
  *
  * Called with cpu_base->lock of target cpu held.
  */
@@ -166,9 +167,6 @@ hrtimer_check_target(struct hrtimer *tim
 {
 	ktime_t expires;
 
-	if (!new_base->cpu_base->hres_active)
-		return 0;
-
 	expires = ktime_sub(hrtimer_get_expires(timer), new_base->offset);
 	return expires <= new_base->cpu_base->expires_next;
 }
@@ -689,21 +687,24 @@ static void hrtimer_reprogram(struct hrt
 
 	/* Update the pointer to the next expiring timer */
 	hrtimer_update_next_timer(cpu_base, timer);
+	cpu_base->expires_next = expires;
 
 	/*
+	 * If hres is not active, hardware does not have to be
+	 * programmed yet.
+	 *
 	 * If a hang was detected in the last timer interrupt then we
 	 * do not schedule a timer which is earlier than the expiry
 	 * which we enforced in the hang detection. We want the system
 	 * to make progress.
 	 */
-	if (cpu_base->hang_detected)
+	if (!__hrtimer_hres_active(cpu_base) || cpu_base->hang_detected)
 		return;
 
 	/*
 	 * Program the timer hardware. We enforce the expiry for
 	 * events which are already in the past.
 	 */
-	cpu_base->expires_next = expires;
 	tick_program_event(expires, 1);
 }
 
@@ -943,16 +944,8 @@ void hrtimer_start_range_ns(struct hrtim
 	if (!leftmost)
 		goto unlock;
 
-	if (!hrtimer_is_hres_active(timer)) {
-		/*
-		 * Kick to reschedule the next tick to handle the new timer
-		 * on dynticks target.
-		 */
-		if (new_base->cpu_base->nohz_active)
-			wake_up_nohz_cpu(new_base->cpu_base->cpu);
-	} else {
-		hrtimer_reprogram(timer, new_base);
-	}
+	hrtimer_reprogram(timer, new_base);
+
 unlock:
 	unlock_hrtimer_base(timer, &flags);
 }
#18 lore raw

Re: [PATCH 10/25] hrtimer: Make handling of hrtimer reprogramming and enqueuing not conditional

Quoted 10 lines · Anna-Maria Gleixner

The hrtimer_reprogramming, remote timer enqueuing and handling of the hrtimer_cpu_base struct member expires_next depend on the active high resolution timers. This makes the code harder to understand.

To simplify the code, the hrtimer reprogramming is now executed independently except for the real reprogramming part. The expires_next stores now the first enqueued timer. Due to the adaption of the check_target function, remote enqueuing is now only possible when the expiry time is after the currently first expiry time independent of the active high resolution timers.

Sorry, very hard to follow. What?

So we do this to unconditionally track expire_next, such that we can (later) use hrtimer_check_target()?

author maintainer
#19 lore raw

Re: [PATCH 10/25] hrtimer: Make handling of hrtimer reprogramming and enqueuing not conditional

Quoted 5 lines · Peter Zijlstra
Quoted 4 lines · Anna-Maria Gleixner

The hrtimer_reprogramming, remote timer enqueuing and handling of the hrtimer_cpu_base struct member expires_next depend on the active high resolution timers. This makes the code harder to understand.

Quoted 8 lines
Quoted 6 lines

To simplify the code, the hrtimer reprogramming is now executed independently except for the real reprogramming part. The expires_next stores now the first enqueued timer. Due to the adaption of the check_target function, remote enqueuing is now only possible when the expiry time is after the currently first expiry time independent of the active high resolution timers.

Sorry, very hard to follow. What?

I'm sorry, I have to rework the commit messages...

Quoted 4 lines

So we do this to unconditionally track expire_next, such that we can (later) use hrtimer_check_target()?

The main goal of this patch and the three patches before in the series is to reduce the conditional code. I tried to split it into pieces...

next_timer stores the pointer to the first expiring timer. In __remove_hrtimer() next_timer is compared to the removed timer; if the pointers match then the hardware needs to be reprogrammed. This is done to avoid the extra interrupt which was armed for the removed timer. So its a HIGH_RES only functionality.

For the softirq mode we need that pointer to have access to the first expiring timer unconditionally.

expires_next stores the next event armed in hardware. That's used to check whether a timer can be enqueued remotely. If the new timer is expiring before expires_next, then remote enqueue is not possible as we cannot access the remote timer hardware to reprogram it. This is currently conditional for the HIGH_RES case, but there is no reason to make this conditional. So I made HIGH_RES and !HIGH_RES behave the same way just to further reduce the ifdef and conditional zoo.

Anna-Maria

author maintainer
#20 lore raw

PATCH 11/25 hrtimer: Allow remote hrtimer enqueue with "expires_next" as expiry time

In reply to Anna-Maria Gleixner

When enqueuing a timer with expiry X into a timer queue, where already a timer with expriy X is queued, the new timer is queued on the right-hand side of the already queued timer.

Therefore it is no problem, to enqueue a hrtimer on a remote CPU with the same expiry time than the programmed expiry time (expires_next) on this CPU, because the reprogramming path is not executed - it is not the "leftmost" hrtimer.

---
 kernel/time/hrtimer.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/time/hrtimer.c +1 -1
@@ -168,7 +168,7 @@ hrtimer_check_target(struct hrtimer *tim
 	ktime_t expires;
 
 	expires = ktime_sub(hrtimer_get_expires(timer), new_base->offset);
-	return expires <= new_base->cpu_base->expires_next;
+	return expires < new_base->cpu_base->expires_next;
 }
 
 #ifdef CONFIG_NO_HZ_COMMON
author maintainer
#21 lore raw

PATCH 12/25 hrtimer: Simplify hrtimer_reprogram() call

In reply to Anna-Maria Gleixner

The hrtimer_reprogramm() call can be simplified by dereferencing the hrtimer clock base inside the function. It is a preparatory change for softirq based hrtimers.

---
 kernel/time/hrtimer.c |    6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/time/hrtimer.c +3 -3
@@ -650,10 +650,10 @@ static inline void retrigger_next_event(
  *
  * Called with interrupts disabled and base->cpu_base.lock held
  */
-static void hrtimer_reprogram(struct hrtimer *timer,
-			      struct hrtimer_clock_base *base)
+static void hrtimer_reprogram(struct hrtimer *timer)
 {
 	struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
+	struct hrtimer_clock_base *base = timer->base;
 	ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
 
 	WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
@@ -944,7 +944,7 @@ void hrtimer_start_range_ns(struct hrtim
 	if (!leftmost)
 		goto unlock;
 
-	hrtimer_reprogram(timer, new_base);
+	hrtimer_reprogram(timer);
 
 unlock:
 	unlock_hrtimer_base(timer, &flags);
author maintainer
#22 lore raw

PATCH 13/25 hrtimer: Split out code from hrtimer_start_range_ns() for reuse

In reply to Anna-Maria Gleixner

From: Anna-Maria Gleixner <[email protected]>

Preparatory patch for softirq based hrtimers. No functional change.

---
 kernel/time/hrtimer.c |   44 ++++++++++++++++++++++++--------------------
 1 file changed, 24 insertions(+), 20 deletions(-)
kernel/time/hrtimer.c +24 -20
@@ -910,22 +910,11 @@ static inline ktime_t hrtimer_update_low
 	return tim;
 }
 
-/**
- * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
- * @timer:	the timer to be added
- * @tim:	expiry time
- * @delta_ns:	"slack" range for the timer
- * @mode:	expiry mode: absolute (HRTIMER_MODE_ABS) or
- *		relative (HRTIMER_MODE_REL)
- */
-void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
-			    u64 delta_ns, const enum hrtimer_mode mode)
+static int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
+				    u64 delta_ns, const enum hrtimer_mode mode,
+				    struct hrtimer_clock_base *base)
 {
-	struct hrtimer_clock_base *base, *new_base;
-	unsigned long flags;
-	int leftmost;
-
-	base = lock_hrtimer_base(timer, &flags);
+	struct hrtimer_clock_base *new_base;
 
 	/* Remove an active timer from the queue: */
 	remove_hrtimer(timer, base, true);
@@ -940,13 +929,28 @@ void hrtimer_start_range_ns(struct hrtim
 	/* Switch the timer base, if necessary: */
 	new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
 
-	leftmost = enqueue_hrtimer(timer, new_base);
-	if (!leftmost)
-		goto unlock;
+	return enqueue_hrtimer(timer, new_base);
+}
+
+/**
+ * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
+ * @timer:	the timer to be added
+ * @tim:	expiry time
+ * @delta_ns:	"slack" range for the timer
+ * @mode:	expiry mode: absolute (HRTIMER_MODE_ABS) or
+ *		relative (HRTIMER_MODE_REL)
+ */
+void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
+			    u64 delta_ns, const enum hrtimer_mode mode)
+{
+	struct hrtimer_clock_base *base;
+	unsigned long flags;
+
+	base = lock_hrtimer_base(timer, &flags);
 
-	hrtimer_reprogram(timer);
+	if (__hrtimer_start_range_ns(timer, tim, delta_ns, mode, base))
+		hrtimer_reprogram(timer);
 
-unlock:
 	unlock_hrtimer_base(timer, &flags);
 }
 EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
author maintainer
#23 lore raw

PATCH 14/25 hrtimer: Split out code from __hrtimer_get_next_event() for reuse

In reply to Anna-Maria Gleixner

From: Anna-Maria Gleixner <[email protected]>

Preparatory patch for softirq based hrtimers. No functional change.

---
 kernel/time/hrtimer.c |   20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
kernel/time/hrtimer.c +16 -4
@@ -454,12 +454,12 @@ static inline void hrtimer_update_next_t
 }
 
 #if defined(CONFIG_NO_HZ_COMMON) || defined(CONFIG_HIGH_RES_TIMERS)
-static ktime_t __hrtimer_get_next_event(struct hrtimer_cpu_base *cpu_base)
+static ktime_t __hrtimer_next_event_base(struct hrtimer_cpu_base *cpu_base,
+					 unsigned int active,
+					 ktime_t expires_next)
 {
-	unsigned int active = cpu_base->active_bases;
-	ktime_t expires, expires_next = KTIME_MAX;
+	ktime_t expires;
 
-	hrtimer_update_next_timer(cpu_base, NULL);
 	while (active) {
 		unsigned int id = __ffs(active);
 		struct hrtimer_clock_base *base;
@@ -486,6 +486,18 @@ static ktime_t __hrtimer_get_next_event(
 		expires_next = 0;
 	return expires_next;
 }
+
+static ktime_t __hrtimer_get_next_event(struct hrtimer_cpu_base *cpu_base)
+{
+	unsigned int active = cpu_base->active_bases;
+	ktime_t expires_next = KTIME_MAX;
+
+	hrtimer_update_next_timer(cpu_base, NULL);
+
+	expires_next = __hrtimer_next_event_base(cpu_base, active, expires_next);
+
+	return expires_next;
+}
 #endif
 
 static inline ktime_t hrtimer_update_base(struct hrtimer_cpu_base *base)
author maintainer
#24 lore raw

PATCH 15/25 hrtimer: Add clock bases for soft irq context

In reply to Anna-Maria Gleixner

From: Anna-Maria Gleixner <[email protected]>

hrtimer callback functions are always executed in hard interrupt context. Users of hrtimer which need their timer function to be executed in soft interrupt context, make use of tasklets to get the proper context.

Add additional clock bases for timers which must expire in softirq context, so the detour via the tasklet can be avoided. This is also required for RT, where the majority of hrtimer is moved into softirq context.

Keep the new clockids internal to hrtimer for now, so they can't be accessed from other code until the rest of the changes is in place.

---
 include/linux/hrtimer.h |    4 +++
 kernel/time/hrtimer.c   |   56 ++++++++++++++++++++++++++++++++++++++++++------
 2 files changed, 54 insertions(+), 6 deletions(-)
include/linux/hrtimer.h +4 -0
@@ -145,6 +145,10 @@ enum  hrtimer_base_type {
 	HRTIMER_BASE_REALTIME,
 	HRTIMER_BASE_BOOTTIME,
 	HRTIMER_BASE_TAI,
+	HRTIMER_BASE_MONOTONIC_SOFT,
+	HRTIMER_BASE_REALTIME_SOFT,
+	HRTIMER_BASE_BOOTTIME_SOFT,
+	HRTIMER_BASE_TAI_SOFT,
 	HRTIMER_MAX_CLOCK_BASES,
 };
 
kernel/time/hrtimer.c +50 -6

79 lines not on this page · raw message

author maintainer
#25 lore raw

PATCH 16/25 hrtimer: Allow function reuse for softirq based hrtimer

In reply to Anna-Maria Gleixner

From: Anna-Maria Gleixner <[email protected]>

The softirq based hrtimer can utilize most of the existing hrtimer functions, but need to operate on a different data set. Add an active_mask argument to various functions so the hard and soft bases can be selected. Fixup the existing callers and hand in the ACTIVE_HARD mask.

---
 kernel/time/hrtimer.c |   20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
kernel/time/hrtimer.c +15 -5
@@ -72,6 +72,14 @@
 #define CLOCK_TAI_SOFT		(CLOCK_TAI	 | HRTIMER_BASE_SOFT_MASK)
 
 /*
+ * Masks for selecting the soft and hard context timers from
+ * cpu_base->active
+ */
+#define MASK_SHIFT		(HRTIMER_BASE_MONOTONIC_SOFT)
+#define HRTIMER_ACTIVE_HARD	((1U << MASK_SHIFT) - 1)
+#define HRTIMER_ACTIVE_SOFT	(HRTIMER_ACTIVE_HARD << MASK_SHIFT)
+
+/*
  * The timer bases:
  *
  * There are more clockids than hrtimer bases. Thus, we index
@@ -527,11 +535,12 @@ static ktime_t __hrtimer_next_event_base
 
 static ktime_t __hrtimer_get_next_event(struct hrtimer_cpu_base *cpu_base)
 {
-	unsigned int active = cpu_base->active_bases;
+	unsigned int active;
 	ktime_t expires_next = KTIME_MAX;
 
 	hrtimer_update_next_timer(cpu_base, NULL);
 
+	active = cpu_base->active_bases & HRTIMER_ACTIVE_HARD;
 	expires_next = __hrtimer_next_event_base(cpu_base, active, expires_next);
 
 	return expires_next;
@@ -1264,9 +1273,10 @@ static void __run_hrtimer(struct hrtimer
 	base->running = NULL;
 }
 
-static void __hrtimer_run_queues(struct hrtimer_cpu_base *cpu_base, ktime_t now)
+static void __hrtimer_run_queues(struct hrtimer_cpu_base *cpu_base, ktime_t now,
+				 unsigned int active_mask)
 {
-	unsigned int active = cpu_base->active_bases;
+	unsigned int active = cpu_base->active_bases & active_mask;
 
 	while (active) {
 		unsigned int id = __ffs(active);
@@ -1333,7 +1343,7 @@ void hrtimer_interrupt(struct clock_even
 	 */
 	cpu_base->expires_next = KTIME_MAX;
 
-	__hrtimer_run_queues(cpu_base, now);
+	__hrtimer_run_queues(cpu_base, now, HRTIMER_ACTIVE_HARD);
 
 	/* Reevaluate the clock bases for the next expiry */
 	expires_next = __hrtimer_get_next_event(cpu_base);
@@ -1438,7 +1448,7 @@ void hrtimer_run_queues(void)
 
 	raw_spin_lock(&cpu_base->lock);
 	now = hrtimer_update_base(cpu_base);
-	__hrtimer_run_queues(cpu_base, now);
+	__hrtimer_run_queues(cpu_base, now, HRTIMER_ACTIVE_HARD);
 	raw_spin_unlock(&cpu_base->lock);
 }
 
author maintainer
#26 lore raw

PATCH 17/25 hrtimer: Implementation of softirq hrtimer handling

In reply to Anna-Maria Gleixner

From: Anna-Maria Gleixner <[email protected]>

hrtimers are executed always in hard irq context. If a hrtimer callback function needs to be exectued in softirq context, the detour using tasklets is required. To facilitate this, also in regards to real time specific handling of hrtimers, new clock ids ease the use of hrtimers in softirq context.

Every clock ID is available for soft and hard hrtimers. The hrtimers are handled the same way when they are enqueued. When the hrtimer_interrupt raises, a check is implemented, if the HRTIMER_SOFTIRQ has to be raised as well. If it is raised, the soft hrtimers are not taken into account when for example _hrtimer_get_next_event() is called. At the end of the softirq, all hrtimer_cpu_base struct members are updated, so that the soft hrtimers are also taken into account.

---
 include/linux/hrtimer.h |    8 ++-
 kernel/time/hrtimer.c   |  125 ++++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 122 insertions(+), 11 deletions(-)
include/linux/hrtimer.h +7 -1
@@ -161,6 +161,8 @@ enum  hrtimer_base_type {
  * @clock_was_set_seq:	Sequence counter of clock was set events
  * @migration_enabled:	The migration of hrtimers to other cpus is enabled
  * @nohz_active:	The nohz functionality is enabled
+ * @softirq_activated:	displays, if the softirq is raised - update of softirq
+ *			related settings is not required then.
  * @in_hrtirq:		hrtimer_interrupt() is currently executing
  * @hres_active:	State of high resolution mode
  * @hang_detected:	The last hrtimer interrupt detected a hang
@@ -169,8 +171,10 @@ enum  hrtimer_base_type {
  * @nr_hangs:		Total number of hrtimer interrupt hangs
  * @max_hang_time:	Maximum time spent in hrtimer_interrupt
  * @expires_next:	absolute time of the next event, is required for remote
- *			hrtimer enqueue
+ *			hrtimer enqueue; it is the total first expiry time (hard
+ *			and soft hrtimer are taken into account)
  * @next_timer:		Pointer to the first expiring timer
+ * @softirq_expires_next: Time to check, if soft queues needs also to be expired
  * @clock_base:		array of clock bases for this cpu
  *
  * Note: next_timer is just an optimization for __remove_hrtimer().
@@ -184,6 +188,7 @@ struct hrtimer_cpu_base {
 	unsigned int			clock_was_set_seq;
 	bool				migration_enabled;
 	bool				nohz_active;
+	bool				softirq_activated;
 	unsigned int			hres_active	: 1,
 					in_hrtirq	: 1,
 					hang_detected	: 1;
@@ -195,6 +200,7 @@ struct hrtimer_cpu_base {
 #endif
 	ktime_t				expires_next;
 	struct hrtimer			*next_timer;
+	ktime_t				softirq_expires_next;
 	struct hrtimer_clock_base	clock_base[HRTIMER_MAX_CLOCK_BASES];
 } ____cacheline_aligned;
 
kernel/time/hrtimer.c +115 -10

223 lines not on this page · raw message

#27 lore raw

Re: [PATCH 17/25] hrtimer: Implementation of softirq hrtimer handling

include/linux/hrtimer.h struct hrtimer_cpu_base { · line 194

Quoted 8 lines · Anna-Maria Gleixner
include/linux/hrtimer.h +1 -0
@@ -184,6 +188,7 @@ struct hrtimer_cpu_base {
 	unsigned int			clock_was_set_seq;
 	bool				migration_enabled;
 	bool				nohz_active;
+	bool				softirq_activated;
 	unsigned int			hres_active	: 1,
 					in_hrtirq	: 1,
 					hang_detected	: 1;

Why do we have bools and a bitfield? Should that not all be part of the bitfield?

#28 lore raw

Re: [PATCH 17/25] hrtimer: Implementation of softirq hrtimer handling

In reply to Anna-Maria Gleixner
Quoted 18 lines · Anna-Maria Gleixner

static void __run_hrtimer(struct hrtimer_cpu_base *cpu_base,

kernel/time/hrtimer.c +7 -2
 			  struct hrtimer_clock_base *base,
-			  struct hrtimer *timer, ktime_t *now)
+			  struct hrtimer *timer, ktime_t *now,
+			  bool hardirq)
 {
 	enum hrtimer_restart (*fn)(struct hrtimer *);
 	int restart;
@@ -1241,11 +1298,19 @@ static void __run_hrtimer(struct hrtimer
 	 * protected against migration to a different CPU even if the lock
 	 * is dropped.
 	 */
-	raw_spin_unlock(&cpu_base->lock);
+	if (hardirq)
+		raw_spin_unlock(&cpu_base->lock);
+	else
+		raw_spin_unlock_irq(&cpu_base->lock);
+

kernel/time/hrtimer.c static void __run_hrtimer(struct hrtimer · line 1314

Quoted 10 lines
kernel/time/hrtimer.c +5 -1
 	trace_hrtimer_expire_entry(timer, now);
 	restart = fn(timer);
 	trace_hrtimer_expire_exit(timer);
-	raw_spin_lock(&cpu_base->lock);
+
+	if (hardirq)
+		raw_spin_lock(&cpu_base->lock);
+	else
+		raw_spin_lock_irq(&cpu_base->lock);
 

That's just nasty...

author maintainer
#29 lore raw

Re: [PATCH 17/25] hrtimer: Implementation of softirq hrtimer handling

Quoted 23 lines · Peter Zijlstra
Quoted 22 lines · Anna-Maria Gleixner

static void __run_hrtimer(struct hrtimer_cpu_base *cpu_base,

kernel/time/hrtimer.c +7 -3
 			  struct hrtimer_clock_base *base,
-			  struct hrtimer *timer, ktime_t *now)
+			  struct hrtimer *timer, ktime_t *now,
+			  bool hardirq)
 {
 	enum hrtimer_restart (*fn)(struct hrtimer *);
 	int restart;
@@ -1241,11 +1298,19 @@ static void __run_hrtimer(struct hrtimer
 	 * protected against migration to a different CPU even if the lock
 	 * is dropped.
 	 */
-	raw_spin_unlock(&cpu_base->lock);
+	if (hardirq)
+		raw_spin_unlock(&cpu_base->lock);
+	else
+		raw_spin_unlock_irq(&cpu_base->lock);
+
 	trace_hrtimer_expire_entry(timer, now);
 	restart = fn(timer);
 	trace_hrtimer_expire_exit(timer);
-	raw_spin_lock(&cpu_base->lock);
Quoted 9 lines

kernel/time/hrtimer.c static void __run_hrtimer(struct hrtimer · line 1314

Quoted 6 lines
kernel/time/hrtimer.c +5 -0
+
+	if (hardirq)
+		raw_spin_lock(&cpu_base->lock);
+	else
+		raw_spin_lock_irq(&cpu_base->lock);
 

That's just nasty...

I know and Thomas was unhappy about that as well, but we did not come up with a better solution.

The nasty alternative is:

static void __run_hrtimer(struct hrtimer_cpu_base *cpu_base,
struct hrtimer_clock_base *base,
- struct hrtimer *timer, ktime_t *now)
+ struct hrtimer *timer, ktime_t *now,
+ unsigned long flags)
...
- raw_spin_unlock(&cpu_base->lock);
+ raw_spin_unlock_irqrestore(&cpu_base->lock, flags);

...
- raw_spin_lock(&cpu_base->lock);
+ raw_spin_lock_irq(&cpu_base->lock, flags);

and hand in flags from the callsites via local_save_flags().

We wanted to avoid the pointless lock_irq for the interrupt context, but yes the conditional is equally bad.

Anna-Maria

#30 lore raw

Re: [PATCH 17/25] hrtimer: Implementation of softirq hrtimer handling

Quoted 18 lines · Anna-Maria Gleixner

I know and Thomas was unhappy about that as well, but we did not come up with a better solution.

The nasty alternative is:

static void __run_hrtimer(struct hrtimer_cpu_base *cpu_base,
struct hrtimer_clock_base *base,

(quoted diff) +4 -3
-			  struct hrtimer *timer, ktime_t *now)
+			  struct hrtimer *timer, ktime_t *now,
+                   	  unsigned long flags)
...
-	raw_spin_unlock(&cpu_base->lock);
+	raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
 
...
-	raw_spin_lock(&cpu_base->lock);
+	raw_spin_lock_irq(&cpu_base->lock, flags);
Quoted 1 line

and hand in flags from the callsites via local_save_flags().

Just push it up one more level and use raw_spin_lock_irqsave() in the (soft)irq handlers and then pass the flags around.

Thta's what I did in that patch I just send.

#31 lore raw

Re: [PATCH 17/25] hrtimer: Implementation of softirq hrtimer handling

In reply to Anna-Maria Gleixner

Review static ktime_t __hrtimer_get_next_event( · line 548

Quoted 11 lines · Anna-Maria Gleixner
(quoted diff) +7 -0
@@ -540,12 +539,23 @@ static ktime_t __hrtimer_get_next_event(
 
 	hrtimer_update_next_timer(cpu_base, NULL);
 
+	if (!cpu_base->softirq_activated) {
+		active = cpu_base->active_bases & HRTIMER_ACTIVE_SOFT;
+		expires_next = __hrtimer_next_event_base(cpu_base, active,
+							 expires_next);
+		cpu_base->softirq_expires_next = expires_next;
+	}
+

So this bugged the living daylight out of me. That's a very asymmetric thing to do. The normal rule is that get_next_event() updates cpu_base::next_timer and hrtimer_reprogram() updates cpu_base::expires_next. And here you make a giant mess of things.

The below is a fairly large diff on top of this patch which is _completely_ untested, but should hopefully restore sanity.

- fixes the mixing of bool and bitfields in cpu_base
- restores the whole get_next_timer() / reprogram() symmetry by adding cpu_base::softirq_next_timer.
- adds a comment hopefully explaining the why of that
- adds for_each_active_base() helper, we had two copies of that, and for a brief moment, I had another few, luckily those didn't survive :-)
- uses irqsave/irqrestore for the cpu_base->lock fiddling around __run_hrtimer().
- folded hrtimer_reprogram() and hrtimer_reprogram_softirq()
- removed superfluous local_bh_disable(), since local_irq_disable() already implies much the same.

Please consider...

---

include/linux/hrtimer.h +11 -6
@@ -186,21 +186,26 @@ struct hrtimer_cpu_base {
 	unsigned int			cpu;
 	unsigned int			active_bases;
 	unsigned int			clock_was_set_seq;
-	bool				migration_enabled;
-	bool				nohz_active;
-	bool				softirq_activated;
-	unsigned int			hres_active	: 1,
-					in_hrtirq	: 1,
-					hang_detected	: 1;
+
+	unsigned int			migration_enabled : 1;
+	unsigned int			nohz_active	  : 1;
+	unsigned int			softirq_activated : 1;
+	unsigned int			hres_active	  : 1;
+	unsigned int			in_hrtirq	  : 1;
+	unsigned int			hang_detected	  : 1;
+
 #ifdef CONFIG_HIGH_RES_TIMERS
 	unsigned int			nr_events;
 	unsigned int			nr_retries;
 	unsigned int			nr_hangs;
 	unsigned int			max_hang_time;
 #endif
+
 	ktime_t				expires_next;
 	struct hrtimer			*next_timer;
 	ktime_t				softirq_expires_next;
+	struct hrtimer			*softirq_next_timer;
+
 	struct hrtimer_clock_base	clock_base[HRTIMER_MAX_CLOCK_BASES];
 } ____cacheline_aligned;
 
kernel/time/hrtimer.c +113 -97

417 lines not on this page · raw message

#32 lore raw

Re: [PATCH 17/25] hrtimer: Implementation of softirq hrtimer handling

Quoted 18 lines · Peter Zijlstra

On Thu, Aug 31, 2017 at 12:23:42PM -0000, Anna-Maria Gleixner wrote:
static void __run_hrtimer(struct hrtimer_cpu_base *cpu_base,

kernel/time/hrtimer.c +2 -5
 			  struct hrtimer_clock_base *base,
 			  struct hrtimer *timer, ktime_t *now,
-			  bool hardirq)
+			  unsigned long flags)
 {
 	enum hrtimer_restart (*fn)(struct hrtimer *);
 	int restart;
@@ -1298,19 +1323,13 @@ static void __run_hrtimer(struct hrtimer
 	 * protected against migration to a different CPU even if the lock
 	 * is dropped.
 	 */
-	if (hardirq)
-		raw_spin_unlock(&cpu_base->lock);
-	else
-		raw_spin_unlock_irq(&cpu_base->lock);
+	raw_spin_unlock_irqrestore(&cpu_base->lock, flags);

kernel/time/hrtimer.c static void __run_hrtimer(struct hrtimer · line 1332

Quoted 10 lines
kernel/time/hrtimer.c +1 -4
 
 	trace_hrtimer_expire_entry(timer, now);
 	restart = fn(timer);
 	trace_hrtimer_expire_exit(timer);
 
-	if (hardirq)
-		raw_spin_lock(&cpu_base->lock);
-	else
-		raw_spin_lock_irq(&cpu_base->lock);
+	raw_spin_lock_irqsave(&cpu_base->lock, flags);

No point in irqsave() here. _irq() is sufficient as we leave the function right after that.

Thanks,

tglx

#33 lore raw

Re: [PATCH 17/25] hrtimer: Implementation of softirq hrtimer handling

Quoted 24 lines · Thomas Gleixner
Quoted 23 lines · Peter Zijlstra

On Thu, Aug 31, 2017 at 12:23:42PM -0000, Anna-Maria Gleixner wrote:
static void __run_hrtimer(struct hrtimer_cpu_base *cpu_base,

kernel/time/hrtimer.c +2 -5
 			  struct hrtimer_clock_base *base,
 			  struct hrtimer *timer, ktime_t *now,
-			  bool hardirq)
+			  unsigned long flags)
 {
 	enum hrtimer_restart (*fn)(struct hrtimer *);
 	int restart;
@@ -1298,19 +1323,13 @@ static void __run_hrtimer(struct hrtimer
 	 * protected against migration to a different CPU even if the lock
 	 * is dropped.
 	 */
-	if (hardirq)
-		raw_spin_unlock(&cpu_base->lock);
-	else
-		raw_spin_unlock_irq(&cpu_base->lock);
+	raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
 
 	trace_hrtimer_expire_entry(timer, now);
 	restart = fn(timer);
 	trace_hrtimer_expire_exit(timer);
 
Quoted 8 lines

kernel/time/hrtimer.c static void __run_hrtimer(struct hrtimer · line 1332

Quoted 5 lines
kernel/time/hrtimer.c +1 -4
-	if (hardirq)
-		raw_spin_lock(&cpu_base->lock);
-	else
-		raw_spin_lock_irq(&cpu_base->lock);
+	raw_spin_lock_irqsave(&cpu_base->lock, flags);

No point in irqsave() here. _irq() is sufficient as we leave the function right after that.

True, I wasn't entirely sure on the irq tracing assumptions (if any) and was too lazy to double check.

#34 lore raw

Re: [PATCH 17/25] hrtimer: Implementation of softirq hrtimer handling

In reply to Peter Zijlstra

this is late I know…

Quoted 2 lines · Peter Zijlstra

- removed superfluous local_bh_disable(), since local_irq_disable() already implies much the same.

it is not superfluous.

Quoted 11 lines

Please consider...

kernel/time/hrtimer.c +0 -1
@@ -1768,7 +1786,6 @@ int hrtimers_dead_cpu(unsigned int scpu)
 	BUG_ON(cpu_online(scpu));
 	tick_cancel_sched_timer(scpu);
 
-	local_bh_disable();
 	local_irq_disable();
 	old_base = &per_cpu(hrtimer_bases, scpu);
 	new_base = this_cpu_ptr(&hrtimer_bases);

kernel/time/hrtimer.c int hrtimers_dead_cpu(unsigned int scpu) · line 1817

Quoted 6 lines
kernel/time/hrtimer.c +0 -1
@@ -1796,7 +1813,6 @@ int hrtimers_dead_cpu(unsigned int scpu)
 	/* Check, if we got expired work to do */
 	__hrtimer_peek_ahead_timers();
 	local_irq_enable();
-	local_bh_enable();
 	return 0;
 }

we need in there. That local_bh_disable() is required in order to let raise_softirq_irqoff() not do anything stupid in particular we need !in_interrupt() defer wakeup_softirqd() until local_bh_enable(). Otherwise wakeup_softirqd() might actually try to wakeup the process and go after the pi_lock which can't happen while holding cpu_base->lock.

Sebastian

#35 lore raw

Re: [PATCH 17/25] hrtimer: Implementation of softirq hrtimer handling

Quoted 23 lines · Sebastian Andrzej Siewior

this is late I know…

Quoted 2 lines · Peter Zijlstra

- removed superfluous local_bh_disable(), since local_irq_disable() already implies much the same.

it is not superfluous.

Quoted 15 lines

Please consider...

kernel/time/hrtimer.c +0 -2
@@ -1768,7 +1786,6 @@ int hrtimers_dead_cpu(unsigned int scpu)
 	BUG_ON(cpu_online(scpu));
 	tick_cancel_sched_timer(scpu);
 
-	local_bh_disable();
 	local_irq_disable();
 	old_base = &per_cpu(hrtimer_bases, scpu);
 	new_base = this_cpu_ptr(&hrtimer_bases);
@@ -1796,7 +1813,6 @@ int hrtimers_dead_cpu(unsigned int scpu)
 	/* Check, if we got expired work to do */
 	__hrtimer_peek_ahead_timers();
 	local_irq_enable();
-	local_bh_enable();
Quoted 8 lines

kernel/time/hrtimer.c int hrtimers_dead_cpu(unsigned int scpu) · line 1817

Quoted 2 lines
kernel/time/hrtimer.c +0 -0
 	return 0;
 }

we need in there. That local_bh_disable() is required in order to let raise_softirq_irqoff() not do anything stupid in particular we need !in_interrupt() defer wakeup_softirqd() until local_bh_enable(). Otherwise wakeup_softirqd() might actually try to wakeup the process and go after the pi_lock which can't happen while holding cpu_base->lock.

Argh, that's horrible and definitely needs a comment.

author maintainer
#36 lore raw

Re: [PATCH 17/25] hrtimer: Implementation of softirq hrtimer handling

Quoted 33 lines · Peter Zijlstra
Quoted 23 lines · Sebastian Andrzej Siewior

this is late I know…

Quoted 2 lines · Peter Zijlstra

- removed superfluous local_bh_disable(), since local_irq_disable() already implies much the same.

it is not superfluous.

Quoted 15 lines

Please consider...

kernel/time/hrtimer.c +0 -2
@@ -1768,7 +1786,6 @@ int hrtimers_dead_cpu(unsigned int scpu)
 	BUG_ON(cpu_online(scpu));
 	tick_cancel_sched_timer(scpu);
 
-	local_bh_disable();
 	local_irq_disable();
 	old_base = &per_cpu(hrtimer_bases, scpu);
 	new_base = this_cpu_ptr(&hrtimer_bases);
@@ -1796,7 +1813,6 @@ int hrtimers_dead_cpu(unsigned int scpu)
 	/* Check, if we got expired work to do */
 	__hrtimer_peek_ahead_timers();
 	local_irq_enable();
-	local_bh_enable();
Quoted 8 lines

kernel/time/hrtimer.c int hrtimers_dead_cpu(unsigned int scpu) · line 1817

Quoted 2 lines
kernel/time/hrtimer.c +0 -0
 	return 0;
 }

we need in there. That local_bh_disable() is required in order to let raise_softirq_irqoff() not do anything stupid in particular we need !in_interrupt() defer wakeup_softirqd() until local_bh_enable(). Otherwise wakeup_softirqd() might actually try to wakeup the process and go after the pi_lock which can't happen while holding cpu_base->lock.

Quoted 2 lines

Argh, that's horrible and definitely needs a comment.

will add it in v4 with a comment.

Anna-Maria

author maintainer
#37 lore raw

PATCH 18/25 hrtimer: Enable soft and hard hrtimer

In reply to Anna-Maria Gleixner

From: Anna-Maria Gleixner <[email protected]>

Move the definition of the clock ids, to be available not only internal. The transition between clock id and hrtimer base is now expanded by the soft hrtimer bases and the corresponding clock ids. Update all hard hrtimer restricted queries to handle soft and hard hrtimers similarly.

---
 include/linux/hrtimer.h |   11 +++++++++++
 kernel/time/hrtimer.c   |   22 +++++++---------------
 2 files changed, 18 insertions(+), 15 deletions(-)
include/linux/hrtimer.h +11 -0
@@ -23,6 +23,17 @@
 #include <linux/timer.h>
 #include <linux/timerqueue.h>
 
+/*
+ * Clock ids for hrtimers which expire in softirq context. These clock ids
+ * are kernel internal and never exported to user space.
+ */
+#define HRTIMER_BASE_SOFT_MASK	MAX_CLOCKS
+
+#define CLOCK_REALTIME_SOFT	(CLOCK_REALTIME	 | HRTIMER_BASE_SOFT_MASK)
+#define CLOCK_MONOTONIC_SOFT	(CLOCK_MONOTONIC | HRTIMER_BASE_SOFT_MASK)
+#define CLOCK_BOOTTIME_SOFT	(CLOCK_BOOTTIME	 | HRTIMER_BASE_SOFT_MASK)
+#define CLOCK_TAI_SOFT		(CLOCK_TAI	 | HRTIMER_BASE_SOFT_MASK)
+
 struct hrtimer_clock_base;
 struct hrtimer_cpu_base;
 
kernel/time/hrtimer.c +7 -15
@@ -60,18 +60,6 @@
 #include "tick-internal.h"
 
 /*
- * Clock ids for timers which expire in softirq context. These clock ids
- * are kernel internal and never exported to user space. Kept internal
- * until the rest of the functionality is in place.
- */
-#define HRTIMER_BASE_SOFT_MASK	MAX_CLOCKS
-
-#define CLOCK_REALTIME_SOFT	(CLOCK_REALTIME	 | HRTIMER_BASE_SOFT_MASK)
-#define CLOCK_MONOTONIC_SOFT	(CLOCK_MONOTONIC | HRTIMER_BASE_SOFT_MASK)
-#define CLOCK_BOOTTIME_SOFT	(CLOCK_BOOTTIME	 | HRTIMER_BASE_SOFT_MASK)
-#define CLOCK_TAI_SOFT		(CLOCK_TAI	 | HRTIMER_BASE_SOFT_MASK)
-
-/*
  * Masks for selecting the soft and hard context timers from
  * cpu_base->active
  */
@@ -1173,7 +1161,7 @@ u64 hrtimer_get_next_event(void)
 
 static inline int hrtimer_clockid_to_base(clockid_t clock_id)
 {
-	if (likely(clock_id < MAX_CLOCKS)) {
+	if (likely(clock_id < MAX_CLOCKS_HRT)) {
 		int base = hrtimer_clock_to_base_table[clock_id];
 
 		if (likely(base != HRTIMER_MAX_CLOCK_BASES))
@@ -1193,8 +1181,12 @@ static void __hrtimer_init(struct hrtime
 
 	cpu_base = raw_cpu_ptr(&hrtimer_bases);
 
-	if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
-		clock_id = CLOCK_MONOTONIC;
+	if (mode != HRTIMER_MODE_ABS) {
+		if (clock_id == CLOCK_REALTIME)
+			clock_id = CLOCK_MONOTONIC;
+		else if (clock_id == CLOCK_REALTIME_SOFT)
+			clock_id = CLOCK_MONOTONIC_SOFT;
+	}
 
 	base = hrtimer_clockid_to_base(clock_id);
 	timer->base = &cpu_base->clock_base[base];
#38 lore raw

Re: [PATCH 18/25] hrtimer: Enable soft and hard hrtimer

Quoted 34 lines · Anna-Maria Gleixner
include/linux/hrtimer.h +11 -0
@@ -23,6 +23,17 @@
 #include <linux/timer.h>
 #include <linux/timerqueue.h>
 
+/*
+ * Clock ids for hrtimers which expire in softirq context. These clock ids
+ * are kernel internal and never exported to user space.
+ */
+#define HRTIMER_BASE_SOFT_MASK	MAX_CLOCKS
+
+#define CLOCK_REALTIME_SOFT	(CLOCK_REALTIME	 | HRTIMER_BASE_SOFT_MASK)
+#define CLOCK_MONOTONIC_SOFT	(CLOCK_MONOTONIC | HRTIMER_BASE_SOFT_MASK)
+#define CLOCK_BOOTTIME_SOFT	(CLOCK_BOOTTIME	 | HRTIMER_BASE_SOFT_MASK)
+#define CLOCK_TAI_SOFT		(CLOCK_TAI	 | HRTIMER_BASE_SOFT_MASK)
+
 struct hrtimer_clock_base;
 struct hrtimer_cpu_base;
 
kernel/time/hrtimer.c +1 -0
@@ -1173,7 +1161,7 @@ u64 hrtimer_get_next_event(void)
 
 static inline int hrtimer_clockid_to_base(clockid_t clock_id)
 {
+	if (likely(clock_id < MAX_CLOCKS_HRT)) {
 		int base = hrtimer_clock_to_base_table[clock_id];
 
 		if (likely(base != HRTIMER_MAX_CLOCK_BASES))
@@ -1193,8 +1181,12 @@ static void __hrtimer_init(struct hrtime
 
 	cpu_base = raw_cpu_ptr(&hrtimer_bases);

kernel/time/hrtimer.c static void __hrtimer_init(struct hrtime · line 1192

Quoted 10 lines
kernel/time/hrtimer.c +6 -0
 
+	if (mode != HRTIMER_MODE_ABS) {
+		if (clock_id == CLOCK_REALTIME)
+			clock_id = CLOCK_MONOTONIC;
+		else if (clock_id == CLOCK_REALTIME_SOFT)
+			clock_id = CLOCK_MONOTONIC_SOFT;
+	}
 
 	base = hrtimer_clockid_to_base(clock_id);
 	timer->base = &cpu_base->clock_base[base];

So why expose these extra bases at all, why not stick another flag in MODE? These extra bases is a pure implementation detail imo; you could equally implement the functionality without (albeit at extra cost).

author maintainer
#39 lore raw

Re: [PATCH 18/25] hrtimer: Enable soft and hard hrtimer

Quoted 43 lines · Peter Zijlstra
Quoted 42 lines · Anna-Maria Gleixner
include/linux/hrtimer.h +11 -0
@@ -23,6 +23,17 @@
 #include <linux/timer.h>
 #include <linux/timerqueue.h>
 
+/*
+ * Clock ids for hrtimers which expire in softirq context. These clock ids
+ * are kernel internal and never exported to user space.
+ */
+#define HRTIMER_BASE_SOFT_MASK	MAX_CLOCKS
+
+#define CLOCK_REALTIME_SOFT	(CLOCK_REALTIME	 | HRTIMER_BASE_SOFT_MASK)
+#define CLOCK_MONOTONIC_SOFT	(CLOCK_MONOTONIC | HRTIMER_BASE_SOFT_MASK)
+#define CLOCK_BOOTTIME_SOFT	(CLOCK_BOOTTIME	 | HRTIMER_BASE_SOFT_MASK)
+#define CLOCK_TAI_SOFT		(CLOCK_TAI	 | HRTIMER_BASE_SOFT_MASK)
+
 struct hrtimer_clock_base;
 struct hrtimer_cpu_base;
 
kernel/time/hrtimer.c +7 -0
@@ -1173,7 +1161,7 @@ u64 hrtimer_get_next_event(void)
 
 static inline int hrtimer_clockid_to_base(clockid_t clock_id)
 {
+	if (likely(clock_id < MAX_CLOCKS_HRT)) {
 		int base = hrtimer_clock_to_base_table[clock_id];
 
 		if (likely(base != HRTIMER_MAX_CLOCK_BASES))
@@ -1193,8 +1181,12 @@ static void __hrtimer_init(struct hrtime
 
 	cpu_base = raw_cpu_ptr(&hrtimer_bases);
 
+	if (mode != HRTIMER_MODE_ABS) {
+		if (clock_id == CLOCK_REALTIME)
+			clock_id = CLOCK_MONOTONIC;
+		else if (clock_id == CLOCK_REALTIME_SOFT)
+			clock_id = CLOCK_MONOTONIC_SOFT;
+	}
 
Quoted 7 lines

kernel/time/hrtimer.c static void __hrtimer_init(struct hrtime · line 1192

Quoted 2 lines
kernel/time/hrtimer.c +0 -0
 	base = hrtimer_clockid_to_base(clock_id);
 	timer->base = &cpu_base->clock_base[base];

So why expose these extra bases at all, why not stick another flag in MODE? These extra bases is a pure implementation detail imo; you could equally implement the functionality without (albeit at extra cost).

It was Thomas' request not to use a flag for this.

Anna-Maria

#40 lore raw

Re: [PATCH 18/25] hrtimer: Enable soft and hard hrtimer

Quoted 45 lines · Anna-Maria Gleixner
Quoted 43 lines · Peter Zijlstra
Quoted 42 lines · Anna-Maria Gleixner
include/linux/hrtimer.h +11 -0
@@ -23,6 +23,17 @@
 #include <linux/timer.h>
 #include <linux/timerqueue.h>
 
+/*
+ * Clock ids for hrtimers which expire in softirq context. These clock ids
+ * are kernel internal and never exported to user space.
+ */
+#define HRTIMER_BASE_SOFT_MASK	MAX_CLOCKS
+
+#define CLOCK_REALTIME_SOFT	(CLOCK_REALTIME	 | HRTIMER_BASE_SOFT_MASK)
+#define CLOCK_MONOTONIC_SOFT	(CLOCK_MONOTONIC | HRTIMER_BASE_SOFT_MASK)
+#define CLOCK_BOOTTIME_SOFT	(CLOCK_BOOTTIME	 | HRTIMER_BASE_SOFT_MASK)
+#define CLOCK_TAI_SOFT		(CLOCK_TAI	 | HRTIMER_BASE_SOFT_MASK)
+
 struct hrtimer_clock_base;
 struct hrtimer_cpu_base;
 
kernel/time/hrtimer.c +7 -0
@@ -1173,7 +1161,7 @@ u64 hrtimer_get_next_event(void)
 
 static inline int hrtimer_clockid_to_base(clockid_t clock_id)
 {
+	if (likely(clock_id < MAX_CLOCKS_HRT)) {
 		int base = hrtimer_clock_to_base_table[clock_id];
 
 		if (likely(base != HRTIMER_MAX_CLOCK_BASES))
@@ -1193,8 +1181,12 @@ static void __hrtimer_init(struct hrtime
 
 	cpu_base = raw_cpu_ptr(&hrtimer_bases);
 
+	if (mode != HRTIMER_MODE_ABS) {
+		if (clock_id == CLOCK_REALTIME)
+			clock_id = CLOCK_MONOTONIC;
+		else if (clock_id == CLOCK_REALTIME_SOFT)
+			clock_id = CLOCK_MONOTONIC_SOFT;
+	}
 
Quoted 6 lines
Quoted 6 lines

kernel/time/hrtimer.c static void __hrtimer_init(struct hrtime · line 1192

Quoted 2 lines
kernel/time/hrtimer.c +0 -0
 	base = hrtimer_clockid_to_base(clock_id);
 	timer->base = &cpu_base->clock_base[base];

So why expose these extra bases at all, why not stick another flag in MODE? These extra bases is a pure implementation detail imo; you could equally implement the functionality without (albeit at extra cost).

Right and that cost too high.

Quoted 3 lines
Quoted 1 line

It was Thomas' request not to use a flag for this.

We can make that a flag as well. There is no hard requirement for making it a CLOCK. Peter is right that it's a pure internal implementation detail and we can hide it from the outside world.

Thanks,

tglx

#41 lore raw

Re: [PATCH 18/25] hrtimer: Enable soft and hard hrtimer

Quoted 5 lines · Thomas Gleixner
Quoted 3 lines
Quoted 3 lines

So why expose these extra bases at all, why not stick another flag in MODE? These extra bases is a pure implementation detail imo; you could equally implement the functionality without (albeit at extra cost).

Right and that cost too high.

Yes, no question about that.

Quoted 5 lines
Quoted 1 line

It was Thomas' request not to use a flag for this.

We can make that a flag as well. There is no hard requirement for making it a CLOCK. Peter is right that it's a pure internal implementation detail and we can hide it from the outside world.

Yeah, that was my point.

author maintainer
#42 lore raw

PATCH 19/25 can/bcm: Replace hrtimer_tasklet with softirq based hrtimer

In reply to Anna-Maria Gleixner

From: Thomas Gleixner <[email protected]>

Switch the timer to CLOCK_MONOTONIC_SOFT, which executed the timer callback in softirq context and remove the hrtimer_tasklet.

---
 net/can/bcm.c |  150 ++++++++++++++++++----------------------------------------
 1 file changed, 49 insertions(+), 101 deletions(-)
net/can/bcm.c +49 -101
@@ -102,7 +102,6 @@ struct bcm_op {
 	unsigned long frames_abs, frames_filtered;
 	struct bcm_timeval ival1, ival2;
 	struct hrtimer timer, thrtimer;
-	struct tasklet_struct tsklet, thrtsklet;
 	ktime_t rx_stamp, kt_ival1, kt_ival2, kt_lastmsg;
 	int rx_ifindex;
 	int cfsiz;
@@ -364,25 +363,34 @@ static void bcm_send_to_user(struct bcm_
 	}
 }
 
-static void bcm_tx_start_timer(struct bcm_op *op)
+static bool bcm_tx_set_expiry(struct bcm_op *op, struct hrtimer *hrt)
 {
+	ktime_t ival;
+
 	if (op->kt_ival1 && op->count)
-		hrtimer_start(&op->timer,
-			      ktime_add(ktime_get(), op->kt_ival1),
-			      HRTIMER_MODE_ABS);
+		ival = op->kt_ival1;
 	else if (op->kt_ival2)
-		hrtimer_start(&op->timer,
-			      ktime_add(ktime_get(), op->kt_ival2),
-			      HRTIMER_MODE_ABS);
+		ival = op->kt_ival2;
+	else
+		return false;
+
+	hrtimer_set_expires(hrt, ktime_add(ktime_get(), ival));
+	return true;
 }
 
-static void bcm_tx_timeout_tsklet(unsigned long data)
+static void bcm_tx_start_timer(struct bcm_op *op)
 {
-	struct bcm_op *op = (struct bcm_op *)data;
+	if (bcm_tx_set_expiry(op, &op->timer))
+		hrtimer_start_expires(&op->timer, HRTIMER_MODE_ABS);
+}
+
+/* bcm_tx_timeout_handler - performs cyclic CAN frame transmissions */
+static enum hrtimer_restart bcm_tx_timeout_handler(struct hrtimer *hrtimer)
+{
+	struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
 	struct bcm_msg_head msg_head;
 
 	if (op->kt_ival1 && (op->count > 0)) {
-
 		op->count--;
 		if (!op->count && (op->flags & TX_COUNTEVT)) {
 
@@ -399,22 +407,12 @@ static void bcm_tx_timeout_tsklet(unsign
 		}
 		bcm_can_tx(op);
 
-	} else if (op->kt_ival2)
+	} else if (op->kt_ival2) {
 		bcm_can_tx(op);
+	}
 
-	bcm_tx_start_timer(op);
-}
-
-/*
- * bcm_tx_timeout_handler - performs cyclic CAN frame transmissions
- */
-static enum hrtimer_restart bcm_tx_timeout_handler(struct hrtimer *hrtimer)
-{
-	struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
-
-	tasklet_schedule(&op->tsklet);
-
-	return HRTIMER_NORESTART;
+	return bcm_tx_set_expiry(op, &op->timer) ?
+		HRTIMER_RESTART : HRTIMER_NORESTART;
 }
 
 /*
@@ -542,11 +540,18 @@ static void bcm_rx_starttimer(struct bcm
 		hrtimer_start(&op->timer, op->kt_ival1, HRTIMER_MODE_REL);
 }
 
-static void bcm_rx_timeout_tsklet(unsigned long data)
+/* bcm_rx_timeout_handler - when the (cyclic) CAN frame reception timed out */
+static enum hrtimer_restart bcm_rx_timeout_handler(struct hrtimer *hrtimer)
 {
-	struct bcm_op *op = (struct bcm_op *)data;
+	struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
 	struct bcm_msg_head msg_head;
 
+	/* if user wants to be informed, when cyclic CAN-Messages come back */
+	if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
+		/* clear received CAN frames to indicate 'nothing received' */
+		memset(op->last_frames, 0, op->nframes * op->cfsiz);
+	}
+
 	/* create notification to user */
 	msg_head.opcode  = RX_TIMEOUT;
 	msg_head.flags   = op->flags;
@@ -557,25 +562,6 @@ static void bcm_rx_timeout_tsklet(unsign
 	msg_head.nframes = 0;
 
 	bcm_send_to_user(op, &msg_head, NULL, 0);
-}
-
-/*
- * bcm_rx_timeout_handler - when the (cyclic) CAN frame reception timed out
- */
-static enum hrtimer_restart bcm_rx_timeout_handler(struct hrtimer *hrtimer)
-{
-	struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
-
-	/* schedule before NET_RX_SOFTIRQ */
-	tasklet_hi_schedule(&op->tsklet);
-
-	/* no restart of the timer is done here! */
-
-	/* if user wants to be informed, when cyclic CAN-Messages come back */
-	if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
-		/* clear received CAN frames to indicate 'nothing received' */
-		memset(op->last_frames, 0, op->nframes * op->cfsiz);
-	}
 
 	return HRTIMER_NORESTART;
 }
@@ -583,14 +569,12 @@ static enum hrtimer_restart bcm_rx_timeo
 /*
  * bcm_rx_do_flush - helper for bcm_rx_thr_flush
  */
-static inline int bcm_rx_do_flush(struct bcm_op *op, int update,
-				  unsigned int index)
+static inline int bcm_rx_do_flush(struct bcm_op *op, unsigned int index)
 {
 	struct canfd_frame *lcf = op->last_frames + op->cfsiz * index;
 
 	if ((op->last_frames) && (lcf->flags & RX_THR)) {
-		if (update)
-			bcm_rx_changed(op, lcf);
+		bcm_rx_changed(op, lcf);
 		return 1;
 	}
 	return 0;
@@ -598,11 +582,8 @@ static inline int bcm_rx_do_flush(struct
 
 /*
  * bcm_rx_thr_flush - Check for throttled data and send it to the userspace
- *
- * update == 0 : just check if throttled data is available  (any irq context)
- * update == 1 : check and send throttled data to userspace (soft_irq context)
  */
-static int bcm_rx_thr_flush(struct bcm_op *op, int update)
+static int bcm_rx_thr_flush(struct bcm_op *op)
 {
 	int updated = 0;
 
@@ -611,24 +592,16 @@ static int bcm_rx_thr_flush(struct bcm_o
 
 		/* for MUX filter we start at index 1 */
 		for (i = 1; i < op->nframes; i++)
-			updated += bcm_rx_do_flush(op, update, i);
+			updated += bcm_rx_do_flush(op, i);
 
 	} else {
 		/* for RX_FILTER_ID and simple filter */
-		updated += bcm_rx_do_flush(op, update, 0);
+		updated += bcm_rx_do_flush(op, 0);
 	}
 
 	return updated;
 }
 
-static void bcm_rx_thr_tsklet(unsigned long data)
-{
-	struct bcm_op *op = (struct bcm_op *)data;
-
-	/* push the changed data to the userspace */
-	bcm_rx_thr_flush(op, 1);
-}
-
 /*
  * bcm_rx_thr_handler - the time for blocked content updates is over now:
  *                      Check for throttled data and send it to the userspace
@@ -637,9 +610,7 @@ static enum hrtimer_restart bcm_rx_thr_h
 {
 	struct bcm_op *op = container_of(hrtimer, struct bcm_op, thrtimer);
 
-	tasklet_schedule(&op->thrtsklet);
-
-	if (bcm_rx_thr_flush(op, 0)) {
+	if (bcm_rx_thr_flush(op)) {
 		hrtimer_forward(hrtimer, ktime_get(), op->kt_ival2);
 		return HRTIMER_RESTART;
 	} else {
@@ -735,23 +706,8 @@ static struct bcm_op *bcm_find_op(struct
 
 static void bcm_remove_op(struct bcm_op *op)
 {
-	if (op->tsklet.func) {
-		while (test_bit(TASKLET_STATE_SCHED, &op->tsklet.state) ||
-		       test_bit(TASKLET_STATE_RUN, &op->tsklet.state) ||
-		       hrtimer_active(&op->timer)) {
-			hrtimer_cancel(&op->timer);
-			tasklet_kill(&op->tsklet);
-		}
-	}
-
-	if (op->thrtsklet.func) {
-		while (test_bit(TASKLET_STATE_SCHED, &op->thrtsklet.state) ||
-		       test_bit(TASKLET_STATE_RUN, &op->thrtsklet.state) ||
-		       hrtimer_active(&op->thrtimer)) {
-			hrtimer_cancel(&op->thrtimer);
-			tasklet_kill(&op->thrtsklet);
-		}
-	}
+	hrtimer_cancel(&op->timer);
+	hrtimer_cancel(&op->thrtimer);
 
 	if ((op->frames) && (op->frames != &op->sframe))
 		kfree(op->frames);
@@ -979,15 +935,13 @@ static int bcm_tx_setup(struct bcm_msg_h
 		op->ifindex = ifindex;
 
 		/* initialize uninitialized (kzalloc) structure */
-		hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
+		hrtimer_init(&op->timer, CLOCK_MONOTONIC_SOFT,
+			     HRTIMER_MODE_REL);
 		op->timer.function = bcm_tx_timeout_handler;
 
-		/* initialize tasklet for tx countevent notification */
-		tasklet_init(&op->tsklet, bcm_tx_timeout_tsklet,
-			     (unsigned long) op);
-
 		/* currently unused in tx_ops */
-		hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
+		hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC_SOFT,
+			     HRTIMER_MODE_REL);
 
 		/* add this bcm_op to the list of the tx_ops */
 		list_add(&op->list, &bo->tx_ops);
@@ -1150,20 +1104,14 @@ static int bcm_rx_setup(struct bcm_msg_h
 		op->rx_ifindex = ifindex;
 
 		/* initialize uninitialized (kzalloc) structure */
-		hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
+		hrtimer_init(&op->timer, CLOCK_MONOTONIC_SOFT,
+			     HRTIMER_MODE_REL);
 		op->timer.function = bcm_rx_timeout_handler;
 
-		/* initialize tasklet for rx timeout notification */
-		tasklet_init(&op->tsklet, bcm_rx_timeout_tsklet,
-			     (unsigned long) op);
-
-		hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
+		hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC_SOFT,
+			     HRTIMER_MODE_REL);
 		op->thrtimer.function = bcm_rx_thr_handler;
 
-		/* initialize tasklet for rx throttle handling */
-		tasklet_init(&op->thrtsklet, bcm_rx_thr_tsklet,
-			     (unsigned long) op);
-
 		/* add this bcm_op to the list of the rx_ops */
 		list_add(&op->list, &bo->rx_ops);
 
@@ -1209,7 +1157,7 @@ static int bcm_rx_setup(struct bcm_msg_h
 			 */
 			op->kt_lastmsg = 0;
 			hrtimer_cancel(&op->thrtimer);
-			bcm_rx_thr_flush(op, 1);
+			bcm_rx_thr_flush(op);
 		}
 
 		if ((op->flags & STARTTIMER) && op->kt_ival1)
maintainer
#43 lore raw

Re: [PATCH 19/25] can/bcm: Replace hrtimer_tasklet with softirq based hrtimer

Hi Anna-Maria & Thomas,

thanks for the effort!

I'm really happy that the hrtimer becomes softirq capable as it basically reverts this ugly commit from January 2009:

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit?id=6e5c172cf7ca1ab878cc6a6a4c1d52fef60f3ee0

From the first look the changes seem to be correct.

I'll pick the entire patchset for a test and give a Acked|Tested-by then.

Best regards,
Oliver

ps.

On 08/31/2017 02:23 PM, Anna-Maria Gleixner wrote:

(nothing here!)

Please send patches inline and not as attachments. Tnx!

#44 lore raw

Re: [PATCH 19/25] can/bcm: Replace hrtimer_tasklet with softirq based hrtimer

Quoted 16 lines · Oliver Hartkopp

Hi Anna-Maria & Thomas,

thanks for the effort!

I'm really happy that the hrtimer becomes softirq capable as it basically reverts this ugly commit from January 2009:

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit?id=6e5c172cf7ca1ab878cc6a6a4c1d52fef60f3ee0

From the first look the changes seem to be correct.

I'll pick the entire patchset for a test and give a Acked|Tested-by then.

Best regards,
Oliver

Quoted 7 lines

ps.

On 08/31/2017 02:23 PM, Anna-Maria Gleixner wrote:

(nothing here!)

Please send patches inline and not as attachments. Tnx!

PS:

Content-Type: text/plain; charset=UTF-8
Content-Disposition: inline;
filename=canbcm_Replace_hrtimer_tasklet_with_softirq_based_hrtimer.patch

Please get a proper mail client, which actually parses the Content-Disposition tag correcly

Thanks,

tglx

maintainer
#45 lore raw

Re: [PATCH 19/25] can/bcm: Replace hrtimer_tasklet with softirq based hrtimer

Quoted 9 lines · Thomas Gleixner
Quoted 6 lines · Oliver Hartkopp

On 08/31/2017 02:23 PM, Anna-Maria Gleixner wrote:

(nothing here!)

Please send patches inline and not as attachments. Tnx!

Quoted 9 lines

PS:

Content-Type: text/plain; charset=UTF-8
Content-Disposition: inline;
filename=canbcm_Replace_hrtimer_tasklet_with_softirq_based_hrtimer.patch

Please get a proper mail client, which actually parses the Content-Disposition tag correcly

Aah!

Setting

mail.reply_quote_inline = true

in the config editor fixes that issue when replying a quilt post with Thunderbird.

Tnx!

Best,
Oliver

maintainer Acked-by on v1 19/25
#46 lore raw

Re: [PATCH 19/25] can/bcm: Replace hrtimer_tasklet with softirq based hrtimer

In reply to Anna-Maria Gleixner
Quoted 8 lines · Anna-Maria Gleixner

From: Thomas Gleixner <[email protected]>

Switch the timer to CLOCK_MONOTONIC_SOFT, which executed the timer callback in softirq context and remove the hrtimer_tasklet.

Thanks,
Oliver

Quoted 285 lines
Cc Marc Kleine-Budde <[email protected]>
---
  net/can/bcm.c |  150 ++++++++++++++++++----------------------------------------
  1 file changed, 49 insertions(+), 101 deletions(-)
net/can/bcm.c +49 -101
@@ -102,7 +102,6 @@ struct bcm_op {
  	unsigned long frames_abs, frames_filtered;
  	struct bcm_timeval ival1, ival2;
  	struct hrtimer timer, thrtimer;
-	struct tasklet_struct tsklet, thrtsklet;
  	ktime_t rx_stamp, kt_ival1, kt_ival2, kt_lastmsg;
  	int rx_ifindex;
  	int cfsiz;
@@ -364,25 +363,34 @@ static void bcm_send_to_user(struct bcm_
  	}
  }
 
-static void bcm_tx_start_timer(struct bcm_op *op)
+static bool bcm_tx_set_expiry(struct bcm_op *op, struct hrtimer *hrt)
  {
+	ktime_t ival;
+
  	if (op->kt_ival1 && op->count)
-		hrtimer_start(&op->timer,
-			      ktime_add(ktime_get(), op->kt_ival1),
-			      HRTIMER_MODE_ABS);
+		ival = op->kt_ival1;
  	else if (op->kt_ival2)
-		hrtimer_start(&op->timer,
-			      ktime_add(ktime_get(), op->kt_ival2),
-			      HRTIMER_MODE_ABS);
+		ival = op->kt_ival2;
+	else
+		return false;
+
+	hrtimer_set_expires(hrt, ktime_add(ktime_get(), ival));
+	return true;
  }
 
-static void bcm_tx_timeout_tsklet(unsigned long data)
+static void bcm_tx_start_timer(struct bcm_op *op)
  {
-	struct bcm_op *op = (struct bcm_op *)data;
+	if (bcm_tx_set_expiry(op, &op->timer))
+		hrtimer_start_expires(&op->timer, HRTIMER_MODE_ABS);
+}
+
+/* bcm_tx_timeout_handler - performs cyclic CAN frame transmissions */
+static enum hrtimer_restart bcm_tx_timeout_handler(struct hrtimer *hrtimer)
+{
+	struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
  	struct bcm_msg_head msg_head;
 
  	if (op->kt_ival1 && (op->count > 0)) {
-
  		op->count--;
  		if (!op->count && (op->flags & TX_COUNTEVT)) {
 
@@ -399,22 +407,12 @@ static void bcm_tx_timeout_tsklet(unsign
  		}
  		bcm_can_tx(op);
 
-	} else if (op->kt_ival2)
+	} else if (op->kt_ival2) {
  		bcm_can_tx(op);
+	}
 
-	bcm_tx_start_timer(op);
-}
-
-/*
- * bcm_tx_timeout_handler - performs cyclic CAN frame transmissions
- */
-static enum hrtimer_restart bcm_tx_timeout_handler(struct hrtimer *hrtimer)
-{
-	struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
-
-	tasklet_schedule(&op->tsklet);
-
-	return HRTIMER_NORESTART;
+	return bcm_tx_set_expiry(op, &op->timer) ?
+		HRTIMER_RESTART : HRTIMER_NORESTART;
  }
 
  /*
@@ -542,11 +540,18 @@ static void bcm_rx_starttimer(struct bcm
  		hrtimer_start(&op->timer, op->kt_ival1, HRTIMER_MODE_REL);
  }
 
-static void bcm_rx_timeout_tsklet(unsigned long data)
+/* bcm_rx_timeout_handler - when the (cyclic) CAN frame reception timed out */
+static enum hrtimer_restart bcm_rx_timeout_handler(struct hrtimer *hrtimer)
  {
-	struct bcm_op *op = (struct bcm_op *)data;
+	struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
  	struct bcm_msg_head msg_head;
 
+	/* if user wants to be informed, when cyclic CAN-Messages come back */
+	if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
+		/* clear received CAN frames to indicate 'nothing received' */
+		memset(op->last_frames, 0, op->nframes * op->cfsiz);
+	}
+
  	/* create notification to user */
  	msg_head.opcode  = RX_TIMEOUT;
  	msg_head.flags   = op->flags;
@@ -557,25 +562,6 @@ static void bcm_rx_timeout_tsklet(unsign
  	msg_head.nframes = 0;
 
  	bcm_send_to_user(op, &msg_head, NULL, 0);
-}
-
-/*
- * bcm_rx_timeout_handler - when the (cyclic) CAN frame reception timed out
- */
-static enum hrtimer_restart bcm_rx_timeout_handler(struct hrtimer *hrtimer)
-{
-	struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
-
-	/* schedule before NET_RX_SOFTIRQ */
-	tasklet_hi_schedule(&op->tsklet);
-
-	/* no restart of the timer is done here! */
-
-	/* if user wants to be informed, when cyclic CAN-Messages come back */
-	if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
-		/* clear received CAN frames to indicate 'nothing received' */
-		memset(op->last_frames, 0, op->nframes * op->cfsiz);
-	}
 
  	return HRTIMER_NORESTART;
  }
@@ -583,14 +569,12 @@ static enum hrtimer_restart bcm_rx_timeo
  /*
   * bcm_rx_do_flush - helper for bcm_rx_thr_flush
   */
-static inline int bcm_rx_do_flush(struct bcm_op *op, int update,
-				  unsigned int index)
+static inline int bcm_rx_do_flush(struct bcm_op *op, unsigned int index)
  {
  	struct canfd_frame *lcf = op->last_frames + op->cfsiz * index;
 
  	if ((op->last_frames) && (lcf->flags & RX_THR)) {
-		if (update)
-			bcm_rx_changed(op, lcf);
+		bcm_rx_changed(op, lcf);
  		return 1;
  	}
  	return 0;
@@ -598,11 +582,8 @@ static inline int bcm_rx_do_flush(struct
 
  /*
   * bcm_rx_thr_flush - Check for throttled data and send it to the userspace
- *
- * update == 0 : just check if throttled data is available  (any irq context)
- * update == 1 : check and send throttled data to userspace (soft_irq context)
   */
-static int bcm_rx_thr_flush(struct bcm_op *op, int update)
+static int bcm_rx_thr_flush(struct bcm_op *op)
  {
  	int updated = 0;
 
@@ -611,24 +592,16 @@ static int bcm_rx_thr_flush(struct bcm_o
 
  		/* for MUX filter we start at index 1 */
  		for (i = 1; i < op->nframes; i++)
-			updated += bcm_rx_do_flush(op, update, i);
+			updated += bcm_rx_do_flush(op, i);
 
  	} else {
  		/* for RX_FILTER_ID and simple filter */
-		updated += bcm_rx_do_flush(op, update, 0);
+		updated += bcm_rx_do_flush(op, 0);
  	}
 
  	return updated;
  }
 
-static void bcm_rx_thr_tsklet(unsigned long data)
-{
-	struct bcm_op *op = (struct bcm_op *)data;
-
-	/* push the changed data to the userspace */
-	bcm_rx_thr_flush(op, 1);
-}
-
  /*
   * bcm_rx_thr_handler - the time for blocked content updates is over now:
   *                      Check for throttled data and send it to the userspace
@@ -637,9 +610,7 @@ static enum hrtimer_restart bcm_rx_thr_h
  {
  	struct bcm_op *op = container_of(hrtimer, struct bcm_op, thrtimer);
 
-	tasklet_schedule(&op->thrtsklet);
-
-	if (bcm_rx_thr_flush(op, 0)) {
+	if (bcm_rx_thr_flush(op)) {
  		hrtimer_forward(hrtimer, ktime_get(), op->kt_ival2);
  		return HRTIMER_RESTART;
  	} else {
@@ -735,23 +706,8 @@ static struct bcm_op *bcm_find_op(struct
 
  static void bcm_remove_op(struct bcm_op *op)
  {
-	if (op->tsklet.func) {
-		while (test_bit(TASKLET_STATE_SCHED, &op->tsklet.state) ||
-		       test_bit(TASKLET_STATE_RUN, &op->tsklet.state) ||
-		       hrtimer_active(&op->timer)) {
-			hrtimer_cancel(&op->timer);
-			tasklet_kill(&op->tsklet);
-		}
-	}
-
-	if (op->thrtsklet.func) {
-		while (test_bit(TASKLET_STATE_SCHED, &op->thrtsklet.state) ||
-		       test_bit(TASKLET_STATE_RUN, &op->thrtsklet.state) ||
-		       hrtimer_active(&op->thrtimer)) {
-			hrtimer_cancel(&op->thrtimer);
-			tasklet_kill(&op->thrtsklet);
-		}
-	}
+	hrtimer_cancel(&op->timer);
+	hrtimer_cancel(&op->thrtimer);
 
  	if ((op->frames) && (op->frames != &op->sframe))
  		kfree(op->frames);
@@ -979,15 +935,13 @@ static int bcm_tx_setup(struct bcm_msg_h
  		op->ifindex = ifindex;
 
  		/* initialize uninitialized (kzalloc) structure */
-		hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
+		hrtimer_init(&op->timer, CLOCK_MONOTONIC_SOFT,
+			     HRTIMER_MODE_REL);
  		op->timer.function = bcm_tx_timeout_handler;
 
-		/* initialize tasklet for tx countevent notification */
-		tasklet_init(&op->tsklet, bcm_tx_timeout_tsklet,
-			     (unsigned long) op);
-
  		/* currently unused in tx_ops */
-		hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
+		hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC_SOFT,
+			     HRTIMER_MODE_REL);
 
  		/* add this bcm_op to the list of the tx_ops */
  		list_add(&op->list, &bo->tx_ops);
@@ -1150,20 +1104,14 @@ static int bcm_rx_setup(struct bcm_msg_h
  		op->rx_ifindex = ifindex;
 
  		/* initialize uninitialized (kzalloc) structure */
-		hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
+		hrtimer_init(&op->timer, CLOCK_MONOTONIC_SOFT,
+			     HRTIMER_MODE_REL);
  		op->timer.function = bcm_rx_timeout_handler;
 
-		/* initialize tasklet for rx timeout notification */
-		tasklet_init(&op->tsklet, bcm_rx_timeout_tsklet,
-			     (unsigned long) op);
-
-		hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
+		hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC_SOFT,
+			     HRTIMER_MODE_REL);
  		op->thrtimer.function = bcm_rx_thr_handler;
 
-		/* initialize tasklet for rx throttle handling */
-		tasklet_init(&op->thrtsklet, bcm_rx_thr_tsklet,
-			     (unsigned long) op);
-
  		/* add this bcm_op to the list of the rx_ops */
  		list_add(&op->list, &bo->rx_ops);
 
@@ -1209,7 +1157,7 @@ static int bcm_rx_setup(struct bcm_msg_h
  			 */
  			op->kt_lastmsg = 0;
  			hrtimer_cancel(&op->thrtimer);
-			bcm_rx_thr_flush(op, 1);
+			bcm_rx_thr_flush(op);
  		}
 
  		if ((op->flags & STARTTIMER) && op->kt_ival1)
author maintainer
#47 lore raw

PATCH 20/25 mac80211_hwsim: Replace hrtimer tasklet with softirq hrtimer

In reply to Anna-Maria Gleixner

From: Thomas Gleixner <[email protected]>

Switch the timer to CLOCK_MONOTONIC_SOFT, which executed the timer callback in softirq context and remove the hrtimer_tasklet.

---
 drivers/net/wireless/mac80211_hwsim.c |   44 +++++++++++++++-------------------
 1 file changed, 20 insertions(+), 24 deletions(-)
drivers/net/wireless/mac80211_hwsim.c +20 -24
@@ -537,7 +537,7 @@ struct mac80211_hwsim_data {
 	unsigned int rx_filter;
 	bool started, idle, scanning;
 	struct mutex mutex;
-	struct tasklet_hrtimer beacon_timer;
+	struct hrtimer beacon_timer;
 	enum ps_mode {
 		PS_DISABLED, PS_ENABLED, PS_AUTO_POLL, PS_MANUAL_POLL
 	} ps;
@@ -1420,7 +1420,7 @@ static void mac80211_hwsim_stop(struct i
 {
 	struct mac80211_hwsim_data *data = hw->priv;
 	data->started = false;
-	tasklet_hrtimer_cancel(&data->beacon_timer);
+	hrtimer_cancel(&data->beacon_timer);
 	wiphy_debug(hw->wiphy, "%s\n", __func__);
 }
 
@@ -1543,14 +1543,12 @@ static enum hrtimer_restart
 mac80211_hwsim_beacon(struct hrtimer *timer)
 {
 	struct mac80211_hwsim_data *data =
-		container_of(timer, struct mac80211_hwsim_data,
-			     beacon_timer.timer);
+		container_of(timer, struct mac80211_hwsim_data, beacon_timer);
 	struct ieee80211_hw *hw = data->hw;
 	u64 bcn_int = data->beacon_int;
-	ktime_t next_bcn;
 
 	if (!data->started)
-		goto out;
+		return HRTIMER_NORESTART;
 
 	ieee80211_iterate_active_interfaces_atomic(
 		hw, IEEE80211_IFACE_ITER_NORMAL,
@@ -1562,11 +1560,9 @@ mac80211_hwsim_beacon(struct hrtimer *ti
 		data->bcn_delta = 0;
 	}
 
-	next_bcn = ktime_add(hrtimer_get_expires(timer),
-			     ns_to_ktime(bcn_int * 1000));
-	tasklet_hrtimer_start(&data->beacon_timer, next_bcn, HRTIMER_MODE_ABS);
-out:
-	return HRTIMER_NORESTART;
+	hrtimer_forward(&data->beacon_timer, hrtimer_get_expires(timer),
+			ns_to_ktime(bcn_int * NSEC_PER_USEC));
+	return HRTIMER_RESTART;
 }
 
 static const char * const hwsim_chanwidths[] = {
@@ -1640,15 +1636,15 @@ static int mac80211_hwsim_config(struct
 	mutex_unlock(&data->mutex);
 
 	if (!data->started || !data->beacon_int)
-		tasklet_hrtimer_cancel(&data->beacon_timer);
-	else if (!hrtimer_is_queued(&data->beacon_timer.timer)) {
+		hrtimer_cancel(&data->beacon_timer);
+	else if (!hrtimer_is_queued(&data->beacon_timer)) {
 		u64 tsf = mac80211_hwsim_get_tsf(hw, NULL);
 		u32 bcn_int = data->beacon_int;
 		u64 until_tbtt = bcn_int - do_div(tsf, bcn_int);
 
-		tasklet_hrtimer_start(&data->beacon_timer,
-				      ns_to_ktime(until_tbtt * 1000),
-				      HRTIMER_MODE_REL);
+		hrtimer_start(&data->beacon_timer,
+			      ns_to_ktime(until_tbtt * 1000),
+			      HRTIMER_MODE_REL);
 	}
 
 	return 0;
@@ -1711,7 +1707,7 @@ static void mac80211_hwsim_bss_info_chan
 			    info->enable_beacon, info->beacon_int);
 		vp->bcn_en = info->enable_beacon;
 		if (data->started &&
-		    !hrtimer_is_queued(&data->beacon_timer.timer) &&
+		    !hrtimer_is_queued(&data->beacon_timer) &&
 		    info->enable_beacon) {
 			u64 tsf, until_tbtt;
 			u32 bcn_int;
@@ -1719,9 +1715,9 @@ static void mac80211_hwsim_bss_info_chan
 			tsf = mac80211_hwsim_get_tsf(hw, vif);
 			bcn_int = data->beacon_int;
 			until_tbtt = bcn_int - do_div(tsf, bcn_int);
-			tasklet_hrtimer_start(&data->beacon_timer,
-					      ns_to_ktime(until_tbtt * 1000),
-					      HRTIMER_MODE_REL);
+			hrtimer_start(&data->beacon_timer,
+				      ns_to_ktime(until_tbtt * 1000),
+				      HRTIMER_MODE_REL);
 		} else if (!info->enable_beacon) {
 			unsigned int count = 0;
 			ieee80211_iterate_active_interfaces_atomic(
@@ -1730,7 +1726,7 @@ static void mac80211_hwsim_bss_info_chan
 			wiphy_debug(hw->wiphy, "  beaconing vifs remaining: %u",
 				    count);
 			if (count == 0) {
-				tasklet_hrtimer_cancel(&data->beacon_timer);
+				hrtimer_cancel(&data->beacon_timer);
 				data->beacon_int = 0;
 			}
 		}
@@ -2722,9 +2718,9 @@ static int mac80211_hwsim_new_radio(stru
 				    data->debugfs,
 				    data, &hwsim_simulate_radar);
 
-	tasklet_hrtimer_init(&data->beacon_timer,
-			     mac80211_hwsim_beacon,
-			     CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
+	hrtimer_init(&data->beacon_timer, CLOCK_MONOTONIC_SOFT,
+		     HRTIMER_MODE_ABS);
+	data->beacon_timer.function = mac80211_hwsim_beacon;
 
 	spin_lock_bh(&hwsim_radio_lock);
 	list_add_tail(&data->list, &hwsim_radios);
maintainer Reviewed-by on v1 20/25
#48 lore raw

Re: [PATCH 20/25] mac80211_hwsim: Replace hrtimer tasklet with softirq hrtimer

Quoted 11 lines · Anna-Maria Gleixner

From: Thomas Gleixner <[email protected]>

Switch the timer to CLOCK_MONOTONIC_SOFT, which executed the timer callback in softirq context and remove the hrtimer_tasklet.

This looks fine to me,

Are you planning to integrate all patches in the series through some other tree, perhaps to be able to get rid of the tasklet_hrtimer API, or should I apply this?

johannes

#49 lore raw

Re: [PATCH 20/25] mac80211_hwsim: Replace hrtimer tasklet with softirq hrtimer

Quoted 12 lines · Johannes Berg
Quoted 11 lines · Anna-Maria Gleixner

From: Thomas Gleixner <[email protected]>

Switch the timer to CLOCK_MONOTONIC_SOFT, which executed the timer callback in softirq context and remove the hrtimer_tasklet.

Quoted 7 lines

This looks fine to me,

Are you planning to integrate all patches in the series through some other tree, perhaps to be able to get rid of the tasklet_hrtimer API, or should I apply this?

The patch depends on the hrtimer core changes, so we either delay the removal for a release cycle or just take the whole lot through tip:timers/core and get rid of it in one go.

Thanks,

tglx

maintainer
#50 lore raw

Re: [PATCH 20/25] mac80211_hwsim: Replace hrtimer tasklet with softirq hrtimer

Quoted 8 lines · Thomas Gleixner
Quoted 4 lines

Are you planning to integrate all patches in the series through some other tree, perhaps to be able to get rid of the tasklet_hrtimer API, or should I apply this?

The patch depends on the hrtimer core changes, so we either delay the removal for a release cycle or just take the whole lot through tip:timers/core and get rid of it in one go.

Ok. For now then I'll assume that it'll go through tip; if not please resend when it's appropriate.

Thanks,
johannes

author maintainer
#51 lore raw

PATCH 21/25 xfrm: Replace hrtimer tasklet with softirq hrtimer

In reply to Anna-Maria Gleixner

From: Thomas Gleixner <[email protected]>

Switch the timer to CLOCK_MONOTONIC_SOFT, which executed the timer callback in softirq context and remove the hrtimer_tasklet.

---
 include/net/xfrm.h    |    2 +-
 net/xfrm/xfrm_state.c |   29 +++++++++++++++++------------
 2 files changed, 18 insertions(+), 13 deletions(-)
include/net/xfrm.h +1 -1
@@ -213,7 +213,7 @@ struct xfrm_state {
 	struct xfrm_stats	stats;
 
 	struct xfrm_lifetime_cur curlft;
-	struct tasklet_hrtimer	mtimer;
+	struct hrtimer		mtimer;
 
 	struct xfrm_state_offload xso;
 
net/xfrm/xfrm_state.c +17 -12
@@ -418,7 +418,7 @@ static void xfrm_put_mode(struct xfrm_mo
 
 static void xfrm_state_gc_destroy(struct xfrm_state *x)
 {
-	tasklet_hrtimer_cancel(&x->mtimer);
+	hrtimer_cancel(&x->mtimer);
 	del_timer_sync(&x->rtimer);
 	kfree(x->aead);
 	kfree(x->aalg);
@@ -463,8 +463,8 @@ static void xfrm_state_gc_task(struct wo
 
 static enum hrtimer_restart xfrm_timer_handler(struct hrtimer *me)
 {
-	struct tasklet_hrtimer *thr = container_of(me, struct tasklet_hrtimer, timer);
-	struct xfrm_state *x = container_of(thr, struct xfrm_state, mtimer);
+	struct xfrm_state *x = container_of(me, struct xfrm_state, mtimer);
+	enum hrtimer_restart ret = HRTIMER_NORESTART;
 	unsigned long now = get_seconds();
 	long next = LONG_MAX;
 	int warn = 0;
@@ -528,7 +528,8 @@ static enum hrtimer_restart xfrm_timer_h
 		km_state_expired(x, 0, 0);
 resched:
 	if (next != LONG_MAX) {
-		tasklet_hrtimer_start(&x->mtimer, ktime_set(next, 0), HRTIMER_MODE_REL);
+		hrtimer_forward_now(&x->mtimer, ktime_set(next, 0));
+		ret = HRTIMER_RESTART;
 	}
 
 	goto out;
@@ -545,7 +546,7 @@ static enum hrtimer_restart xfrm_timer_h
 
 out:
 	spin_unlock(&x->lock);
-	return HRTIMER_NORESTART;
+	return ret;
 }
 
 static void xfrm_replay_timer_handler(unsigned long data);
@@ -564,8 +565,8 @@ struct xfrm_state *xfrm_state_alloc(stru
 		INIT_HLIST_NODE(&x->bydst);
 		INIT_HLIST_NODE(&x->bysrc);
 		INIT_HLIST_NODE(&x->byspi);
-		tasklet_hrtimer_init(&x->mtimer, xfrm_timer_handler,
-					CLOCK_BOOTTIME, HRTIMER_MODE_ABS);
+		hrtimer_init(&x->mtimer, CLOCK_BOOTTIME_SOFT, HRTIMER_MODE_ABS);
+		x->mtimer.function = xfrm_timer_handler;
 		setup_timer(&x->rtimer, xfrm_replay_timer_handler,
 				(unsigned long)x);
 		x->curlft.add_time = get_seconds();
@@ -1021,7 +1022,9 @@ xfrm_state_find(const xfrm_address_t *da
 				hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
 			}
 			x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
-			tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
+			hrtimer_start(&x->mtimer,
+				      ktime_set(net->xfrm.sysctl_acq_expires, 0),
+				      HRTIMER_MODE_REL);
 			net->xfrm.state_num++;
 			xfrm_hash_grow_check(net, x->bydst.next != NULL);
 			spin_unlock_bh(&net->xfrm.xfrm_state_lock);
@@ -1132,7 +1135,7 @@ static void __xfrm_state_insert(struct x
 		hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
 	}
 
-	tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
+	hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
 	if (x->replay_maxage)
 		mod_timer(&x->rtimer, jiffies + x->replay_maxage);
 
@@ -1236,7 +1239,9 @@ static struct xfrm_state *__find_acq_cor
 		x->mark.m = m->m;
 		x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
 		xfrm_state_hold(x);
-		tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
+		hrtimer_start(&x->mtimer,
+			      ktime_set(net->xfrm.sysctl_acq_expires, 0),
+			      HRTIMER_MODE_REL);
 		list_add(&x->km.all, &net->xfrm.state_all);
 		hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
 		h = xfrm_src_hash(net, daddr, saddr, family);
@@ -1535,7 +1540,7 @@ int xfrm_state_update(struct xfrm_state
 		memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
 		x1->km.dying = 0;
 
-		tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
+		hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
 		if (x1->curlft.use_time)
 			xfrm_state_check_expire(x1);
 
@@ -1559,7 +1564,7 @@ int xfrm_state_check_expire(struct xfrm_
 	if (x->curlft.bytes >= x->lft.hard_byte_limit ||
 	    x->curlft.packets >= x->lft.hard_packet_limit) {
 		x->km.state = XFRM_STATE_EXPIRED;
-		tasklet_hrtimer_start(&x->mtimer, 0, HRTIMER_MODE_REL);
+		hrtimer_start(&x->mtimer, 0, HRTIMER_MODE_REL);
 		return -EINVAL;
 	}
 
author maintainer
#52 lore raw

PATCH 22/25 softirq: Remove tasklet_hrtimer

In reply to Anna-Maria Gleixner

From: Thomas Gleixner <[email protected]>

There are no more tasklet_hrtimer users of this interface. Remove it.

---
 include/linux/interrupt.h |   25 ----------------------
 kernel/softirq.c          |   51 ----------------------------------------------
 2 files changed, 76 deletions(-)
include/linux/interrupt.h +0 -25
@@ -631,31 +631,6 @@ extern void tasklet_kill_immediate(struc
 extern void tasklet_init(struct tasklet_struct *t,
 			 void (*func)(unsigned long), unsigned long data);
 
-struct tasklet_hrtimer {
-	struct hrtimer		timer;
-	struct tasklet_struct	tasklet;
-	enum hrtimer_restart	(*function)(struct hrtimer *);
-};
-
-extern void
-tasklet_hrtimer_init(struct tasklet_hrtimer *ttimer,
-		     enum hrtimer_restart (*function)(struct hrtimer *),
-		     clockid_t which_clock, enum hrtimer_mode mode);
-
-static inline
-void tasklet_hrtimer_start(struct tasklet_hrtimer *ttimer, ktime_t time,
-			   const enum hrtimer_mode mode)
-{
-	hrtimer_start(&ttimer->timer, time, mode);
-}
-
-static inline
-void tasklet_hrtimer_cancel(struct tasklet_hrtimer *ttimer)
-{
-	hrtimer_cancel(&ttimer->timer);
-	tasklet_kill(&ttimer->tasklet);
-}
-
 /*
  * Autoprobing for irqs:
  *
kernel/softirq.c +0 -51

57 lines not on this page · raw message

author maintainer
#53 lore raw

PATCH 23/25 ALSA/dummy: Replace tasklet with softirq hrtimer

In reply to Anna-Maria Gleixner

From: Thomas Gleixner <[email protected]>

The tasklet is used to defer the execution of snd_pcm_period_elapsed() to the softirq context. Using the CLOCK_MONOTONIC_SOFT base invokes the timer callback in softirq context as well which renders the tasklet useless.

---
 sound/drivers/dummy.c |   16 +++-------------
 1 file changed, 3 insertions(+), 13 deletions(-)
sound/drivers/dummy.c +3 -13
@@ -376,17 +376,9 @@ struct dummy_hrtimer_pcm {
 	ktime_t period_time;
 	atomic_t running;
 	struct hrtimer timer;
-	struct tasklet_struct tasklet;
 	struct snd_pcm_substream *substream;
 };
 
-static void dummy_hrtimer_pcm_elapsed(unsigned long priv)
-{
-	struct dummy_hrtimer_pcm *dpcm = (struct dummy_hrtimer_pcm *)priv;
-	if (atomic_read(&dpcm->running))
-		snd_pcm_period_elapsed(dpcm->substream);
-}
-
 static enum hrtimer_restart dummy_hrtimer_callback(struct hrtimer *timer)
 {
 	struct dummy_hrtimer_pcm *dpcm;
@@ -394,7 +386,8 @@ static enum hrtimer_restart dummy_hrtime
 	dpcm = container_of(timer, struct dummy_hrtimer_pcm, timer);
 	if (!atomic_read(&dpcm->running))
 		return HRTIMER_NORESTART;
-	tasklet_schedule(&dpcm->tasklet);
+
+	snd_pcm_period_elapsed(dpcm->substream);
 	hrtimer_forward_now(timer, dpcm->period_time);
 	return HRTIMER_RESTART;
 }
@@ -421,7 +414,6 @@ static int dummy_hrtimer_stop(struct snd
 static inline void dummy_hrtimer_sync(struct dummy_hrtimer_pcm *dpcm)
 {
 	hrtimer_cancel(&dpcm->timer);
-	tasklet_kill(&dpcm->tasklet);
 }
 
 static snd_pcm_uframes_t
@@ -466,12 +458,10 @@ static int dummy_hrtimer_create(struct s
 	if (!dpcm)
 		return -ENOMEM;
 	substream->runtime->private_data = dpcm;
-	hrtimer_init(&dpcm->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
+	hrtimer_init(&dpcm->timer, CLOCK_MONOTONIC_SOFT, HRTIMER_MODE_REL);
 	dpcm->timer.function = dummy_hrtimer_callback;
 	dpcm->substream = substream;
 	atomic_set(&dpcm->running, 0);
-	tasklet_init(&dpcm->tasklet, dummy_hrtimer_pcm_elapsed,
-		     (unsigned long)dpcm);
 	return 0;
 }
 
Reviewed-by on v1 23/25
#54 lore raw

Re: [PATCH 23/25] ALSA/dummy: Replace tasklet with softirq hrtimer

Hi,

Quoted 12 lines · Anna-Maria Gleixner

From: Thomas Gleixner <[email protected]>

The tasklet is used to defer the execution of snd_pcm_period_elapsed() to the softirq context. Using the CLOCK_MONOTONIC_SOFT base invokes the timer callback in softirq context as well which renders the tasklet useless.

Quoted 3 lines
---
 sound/drivers/dummy.c |   16 +++-------------
 1 file changed, 3 insertions(+), 13 deletions(-)

I prefer this patch as long as this driver can still receive callbacks from hrtimer subsystem.

Unfortunately, I have too poor machine to compile whole kernel now, thus didn't do any tests, sorry.

I note that ALSA pcsp driver uses a combination of hrtimer/tasklet for the same purpose. I think we can simplify it, too. Please refer to a patch in the end of this message. (But not tested yet for the above reason...)

Quoted 52 lines
sound/drivers/dummy.c +3 -13
@@ -376,17 +376,9 @@ struct dummy_hrtimer_pcm {
 	ktime_t period_time;
 	atomic_t running;
 	struct hrtimer timer;
-	struct tasklet_struct tasklet;
 	struct snd_pcm_substream *substream;
 };
 
-static void dummy_hrtimer_pcm_elapsed(unsigned long priv)
-{
-	struct dummy_hrtimer_pcm *dpcm = (struct dummy_hrtimer_pcm *)priv;
-	if (atomic_read(&dpcm->running))
-		snd_pcm_period_elapsed(dpcm->substream);
-}
-
 static enum hrtimer_restart dummy_hrtimer_callback(struct hrtimer *timer)
 {
 	struct dummy_hrtimer_pcm *dpcm;
@@ -394,7 +386,8 @@ static enum hrtimer_restart dummy_hrtime
 	dpcm = container_of(timer, struct dummy_hrtimer_pcm, timer);
 	if (!atomic_read(&dpcm->running))
 		return HRTIMER_NORESTART;
-	tasklet_schedule(&dpcm->tasklet);
+
+	snd_pcm_period_elapsed(dpcm->substream);
 	hrtimer_forward_now(timer, dpcm->period_time);
 	return HRTIMER_RESTART;
 }
@@ -421,7 +414,6 @@ static int dummy_hrtimer_stop(struct snd
 static inline void dummy_hrtimer_sync(struct dummy_hrtimer_pcm *dpcm)
 {
 	hrtimer_cancel(&dpcm->timer);
-	tasklet_kill(&dpcm->tasklet);
 }
 
 static snd_pcm_uframes_t
@@ -466,12 +458,10 @@ static int dummy_hrtimer_create(struct s
 	if (!dpcm)
 		return -ENOMEM;
 	substream->runtime->private_data = dpcm;
-	hrtimer_init(&dpcm->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
+	hrtimer_init(&dpcm->timer, CLOCK_MONOTONIC_SOFT, HRTIMER_MODE_REL);
 	dpcm->timer.function = dummy_hrtimer_callback;
 	dpcm->substream = substream;
 	atomic_set(&dpcm->running, 0);
-	tasklet_init(&dpcm->tasklet, dummy_hrtimer_pcm_elapsed,
-		     (unsigned long)dpcm);
 	return 0;
 }
 
Quoted 1 line

From b2417f83e0ccbdfe1fc6870817ff0a1bd9243c77 Mon Sep 17 00:00:00 2001

From: Takashi Sakamoto <[email protected]>
Date: Thu, 31 Aug 2017 22:52:33 +0900
Subject: [PATCH] ALSA: pcsp: code optimization for hrtimer in software IRQ
context

In a development period for v4.14, code refactoring was done for hrtimer subsystem. This enables to receive callbacks from hrtimer in software IRQ context.

Currently, ALSA pcsp driver uses hrtimer callback to handle period elapse of PCM buffer and actual calculation of pointer position is done in software IRQ context of tasklet. This can be simplified to the introduced hrtimer in software IRQ context.

This commit applies an optimization for the above reason.

---
 sound/drivers/pcsp/pcsp.c     |  2 +-
 sound/drivers/pcsp/pcsp_lib.c | 24 +++++-------------------
 2 files changed, 6 insertions(+), 20 deletions(-)
sound/drivers/pcsp/pcsp.c +1 -1
index 0dd3f46eb03e..8fac38b81c4f 100644
@@ -100,7 +100,7 @@ static int snd_card_pcsp_probe(int devnum, struct device *dev)
 	if (devnum != 0)
 		return -EINVAL;
 
-	hrtimer_init(&pcsp_chip.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
+	hrtimer_init(&pcsp_chip.timer, CLOCK_MONOTONIC_SOFT, HRTIMER_MODE_REL);
 	pcsp_chip.timer.function = pcsp_do_timer;
 
 	err = snd_card_new(dev, index, id, THIS_MODULE, 0, &card);
sound/drivers/pcsp/pcsp_lib.c +5 -19
index 2f5a35f38ce1..d2b67463ddd3 100644
@@ -21,22 +21,6 @@ MODULE_PARM_DESC(nforce_wa, "Apply NForce chipset workaround "
 
 #define DMIX_WANTS_S16	1
 
-/*
- * Call snd_pcm_period_elapsed in a tasklet
- * This avoids spinlock messes and long-running irq contexts
- */
-static void pcsp_call_pcm_elapsed(unsigned long priv)
-{
-	if (atomic_read(&pcsp_chip.timer_active)) {
-		struct snd_pcm_substream *substream;
-		substream = pcsp_chip.playback_substream;
-		if (substream)
-			snd_pcm_period_elapsed(substream);
-	}
-}
-
-static DECLARE_TASKLET(pcsp_pcm_tasklet, pcsp_call_pcm_elapsed, 0);
-
 /* write the port and returns the next expire time in ns;
  * called at the trigger-start and in hrtimer callback
  */
@@ -121,8 +105,11 @@ static void pcsp_pointer_update(struct snd_pcsp *chip)
 	}
 	spin_unlock_irqrestore(&chip->substream_lock, flags);
 
-	if (periods_elapsed)
-		tasklet_schedule(&pcsp_pcm_tasklet);
+	if (!periods_elapsed)
+		return;
+
+	if (atomic_read(&pcsp_chip.timer_active))
+		snd_pcm_period_elapsed(substream);
 }
 
 enum hrtimer_restart pcsp_do_timer(struct hrtimer *handle)
@@ -195,7 +182,6 @@ void pcsp_sync_stop(struct snd_pcsp *chip)
 	pcsp_stop_playing(chip);
 	local_irq_enable();
 	hrtimer_cancel(&chip->timer);
-	tasklet_kill(&pcsp_pcm_tasklet);
 }
 
 static int snd_pcsp_playback_close(struct snd_pcm_substream *substream)
Signature · 1 line
2.11.0
#55 lore raw

Re: [PATCH 23/25] ALSA/dummy: Replace tasklet with softirq hrtimer

Quoted 23 lines · Takashi Sakamoto

Hi,

Quoted 12 lines · Anna-Maria Gleixner

From: Thomas Gleixner <[email protected]>

The tasklet is used to defer the execution of snd_pcm_period_elapsed() to the softirq context. Using the CLOCK_MONOTONIC_SOFT base invokes the timer callback in softirq context as well which renders the tasklet useless.

Quoted 3 lines
---
 sound/drivers/dummy.c |   16 +++-------------
 1 file changed, 3 insertions(+), 13 deletions(-)

I prefer this patch as long as this driver can still receive callbacks from hrtimer subsystem.

Quoted 8 lines

Unfortunately, I have too poor machine to compile whole kernel now, thus didn't do any tests, sorry.

I note that ALSA pcsp driver uses a combination of hrtimer/tasklet for the same purpose. I think we can simplify it, too. Please refer to a patch in the end of this message. (But not tested yet for the above reason...)

The pcsp is a bit special. It's really high frequent irq calls for controlling the beep on/off, thus offloading the whole isn't good.

thanks,

Takashi

#56 lore raw

Re: [PATCH 23/25] ALSA/dummy: Replace tasklet with softirq hrtimer

In reply to Anna-Maria Gleixner
Quoted 7 lines · Anna-Maria Gleixner

From: Thomas Gleixner <[email protected]>

The tasklet is used to defer the execution of snd_pcm_period_elapsed() to the softirq context. Using the CLOCK_MONOTONIC_SOFT base invokes the timer callback in softirq context as well which renders the tasklet useless.

Quoted 6 lines

I gave it at try, but it caused a kernel hang, unfortunately.

The reason is that snd_pcm_period_elapased() may stop the stream (e.g. when reaching at the end). With this patchset, it'll lead to the call of hrtimer_cancel() from the hrtimer callback itself, thus it stalls.

Below is the additional fix over your patch for working around it. I believe it should cover most corner cases, and seems working fine through quick tests, so far.

thanks,

Takashi

---

sound/drivers/dummy.c +12 -1
index 273d60c42125..b5dd64e3dab1 100644
@@ -375,6 +375,7 @@ struct dummy_hrtimer_pcm {
 	ktime_t base_time;
 	ktime_t period_time;
 	atomic_t running;
+	atomic_t callback_running;
 	struct hrtimer timer;
 	struct snd_pcm_substream *substream;
 };
@@ -387,8 +388,15 @@ static enum hrtimer_restart dummy_hrtimer_callback(struct hrtimer *timer)
 	if (!atomic_read(&dpcm->running))
 		return HRTIMER_NORESTART;
 
+	atomic_inc(&dpcm->callback_running);
 	snd_pcm_period_elapsed(dpcm->substream);
+	atomic_dec(&dpcm->callback_running);
+	/* may be flipped during snd_pcm_period_elapsed() */
+	if (!atomic_read(&dpcm->running))
+		return HRTIMER_NORESTART;
+
 	hrtimer_forward_now(timer, dpcm->period_time);
+	atomic_dec(&dpcm->callback_running);
 	return HRTIMER_RESTART;
 }
 
@@ -407,7 +415,9 @@ static int dummy_hrtimer_stop(struct snd_pcm_substream *substream)
 	struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
 
 	atomic_set(&dpcm->running, 0);
-	hrtimer_cancel(&dpcm->timer);
+	/* issue hrtimer_cancel() only when called outside the callback */
+	if (!atomic_read(&dpcm->callback_running))
+		hrtimer_cancel(&dpcm->timer);
 	return 0;
 }
 
@@ -462,6 +472,7 @@ static int dummy_hrtimer_create(struct snd_pcm_substream *substream)
 	dpcm->timer.function = dummy_hrtimer_callback;
 	dpcm->substream = substream;
 	atomic_set(&dpcm->running, 0);
+	atomic_set(&dpcm->callback_running, 0);
 	return 0;
 }
 
#57 lore raw

Re: [PATCH 23/25] ALSA/dummy: Replace tasklet with softirq hrtimer

Hi,

Quoted 6 lines · Takashi Iwai

I gave it at try, but it caused a kernel hang, unfortunately.

The reason is that snd_pcm_period_elapased() may stop the stream (e.g. when reaching at the end). With this patchset, it'll lead to the call of hrtimer_cancel() from the hrtimer callback itself, thus it stalls.

I can reproduce this bug.

Quoted 3 lines

Below is the additional fix over your patch for working around it. I believe it should cover most corner cases, and seems working fine through quick tests, so far.

This patch looks good to me, too. But I have an alternative.

We can use 'hrtimer_callback_running()' to detect whether to be on hrtimer callback or not (please read '__run_hrtimer()' in 'kernel/time/hrtimer.c'). Usage of this helper function on .stop callback to skip cancellation can avoid the stall. In this case, after stopping PCM substream, the hrtimer callback should return HRTIMER_NORESTART to avoid restarting, as well as your patch. Please test a patch in this message.

Quoted 48 lines

---

sound/drivers/dummy.c +12 -1
index 273d60c42125..b5dd64e3dab1 100644
@@ -375,6 +375,7 @@ struct dummy_hrtimer_pcm {
  	ktime_t base_time;
  	ktime_t period_time;
  	atomic_t running;
+	atomic_t callback_running;
  	struct hrtimer timer;
  	struct snd_pcm_substream *substream;
  };
@@ -387,8 +388,15 @@ static enum hrtimer_restart dummy_hrtimer_callback(struct hrtimer *timer)
  	if (!atomic_read(&dpcm->running))
  		return HRTIMER_NORESTART;
 
+	atomic_inc(&dpcm->callback_running);
  	snd_pcm_period_elapsed(dpcm->substream);
+	atomic_dec(&dpcm->callback_running);
+	/* may be flipped during snd_pcm_period_elapsed() */
+	if (!atomic_read(&dpcm->running))
+		return HRTIMER_NORESTART;
+
  	hrtimer_forward_now(timer, dpcm->period_time);
+	atomic_dec(&dpcm->callback_running);
  	return HRTIMER_RESTART;
  }
 
@@ -407,7 +415,9 @@ static int dummy_hrtimer_stop(struct snd_pcm_substream *substream)
  	struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
 
  	atomic_set(&dpcm->running, 0);
-	hrtimer_cancel(&dpcm->timer);
+	/* issue hrtimer_cancel() only when called outside the callback */
+	if (!atomic_read(&dpcm->callback_running))
+		hrtimer_cancel(&dpcm->timer);
  	return 0;
  }
 
@@ -462,6 +472,7 @@ static int dummy_hrtimer_create(struct snd_pcm_substream *substream)
  	dpcm->timer.function = dummy_hrtimer_callback;
  	dpcm->substream = substream;
  	atomic_set(&dpcm->running, 0);
+	atomic_set(&dpcm->callback_running, 0);
  	return 0;
  }
 
Quoted 1 line

From 07d61ba2a1c0e06e914443225e194d99f2d8c58d Mon Sep 17 00:00:00 2001

From: Takashi Sakamoto <[email protected]>
Date: Fri, 1 Sep 2017 19:10:18 +0900
Subject: [PATCH] ALSA: dummy: avoid stall due to a call of hrtimer_cancel() on
a callback of hrtimer

A call of 'htrimer_cancel()' on a callback of hrtimer brings endless loop because 'struct hrtimer_clock_base.running' is not NULL on the callback. In hrtimer subsystem, this member is used to indicate the instance of hrtimer gets callbacks and there's a helper function, 'hrtimer_callback_running()' to check it.

ALSA dummy driver uses hrtimer to emulate hardware interrupt per period of PCM buffer. When XRUN occurs on PCM substream, in a call of 'snd_pcm_period_elapsed()', 'struct snd_pcm_ops.stop()' is called to stop the substream. In current implementation, 'hrtimer_cancel()' is used to wait for cancellation of hrtimer. However, as described, this brings endless loop.

For this problem, this commit uses 'hrtimer_callback_running()' to detect whether to be on a callback of hrtimer or not, then skip cancellation of hrtimer in hrtimer callbacks. Furthermore, at a case of XRUN, hrtimer callback returns HRTIMER_NORESTART after a call of 'snd_pcm_period_elapsed()' to discontinue hrtimr because cancellation is skipped.

---
 sound/drivers/dummy.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
sound/drivers/dummy.c +6 -1
index 273d60c42125..9caf754c6135 100644
@@ -387,7 +387,11 @@ static enum hrtimer_restart dummy_hrtimer_callback(struct hrtimer *timer)
 	if (!atomic_read(&dpcm->running))
 		return HRTIMER_NORESTART;
 
+	/* In a case of XRUN, this calls .trigger to stop PCM substream. */
 	snd_pcm_period_elapsed(dpcm->substream);
+	if (!atomic_read(&dpcm->running))
+		return HRTIMER_NORESTART;
+
 	hrtimer_forward_now(timer, dpcm->period_time);
 	return HRTIMER_RESTART;
 }
@@ -407,7 +411,8 @@ static int dummy_hrtimer_stop(struct snd_pcm_substream *substream)
 	struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
 
 	atomic_set(&dpcm->running, 0);
-	hrtimer_cancel(&dpcm->timer);
+	if (!hrtimer_callback_running(&dpcm->timer))
+		hrtimer_cancel(&dpcm->timer);
 	return 0;
 }
 
Signature · 6 lines
2.11.0


Regards

Takashi Sakamoto
#58 lore raw

Re: [PATCH 23/25] ALSA/dummy: Replace tasklet with softirq hrtimer

Quoted 13 lines · Takashi Sakamoto

Hi,

Quoted 6 lines · Takashi Iwai

I gave it at try, but it caused a kernel hang, unfortunately.

The reason is that snd_pcm_period_elapased() may stop the stream (e.g. when reaching at the end). With this patchset, it'll lead to the call of hrtimer_cancel() from the hrtimer callback itself, thus it stalls.

I can reproduce this bug.

Quoted 8 lines
Quoted 3 lines

Below is the additional fix over your patch for working around it. I believe it should cover most corner cases, and seems working fine through quick tests, so far.

This patch looks good to me, too. But I have an alternative.

We can use 'hrtimer_callback_running()' to detect whether to be on hrtimer callback or not (please read '__run_hrtimer()' in 'kernel/time/hrtimer.c').

A good point, this is a better choice.

Quoted 65 lines

Usage of this helper function on .stop callback to skip cancellation can avoid the stall. In this case, after stopping PCM substream, the hrtimer callback should return HRTIMER_NORESTART to avoid restarting, as well as your patch. Please test a patch in this message.

Quoted 48 lines

---

sound/drivers/dummy.c +12 -1
index 273d60c42125..b5dd64e3dab1 100644
@@ -375,6 +375,7 @@ struct dummy_hrtimer_pcm {
  	ktime_t base_time;
  	ktime_t period_time;
  	atomic_t running;
+	atomic_t callback_running;
  	struct hrtimer timer;
  	struct snd_pcm_substream *substream;
  };
@@ -387,8 +388,15 @@ static enum hrtimer_restart dummy_hrtimer_callback(struct hrtimer *timer)
  	if (!atomic_read(&dpcm->running))
  		return HRTIMER_NORESTART;
 
+	atomic_inc(&dpcm->callback_running);
  	snd_pcm_period_elapsed(dpcm->substream);
+	atomic_dec(&dpcm->callback_running);
+	/* may be flipped during snd_pcm_period_elapsed() */
+	if (!atomic_read(&dpcm->running))
+		return HRTIMER_NORESTART;
+
  	hrtimer_forward_now(timer, dpcm->period_time);
+	atomic_dec(&dpcm->callback_running);
  	return HRTIMER_RESTART;
  }
 
@@ -407,7 +415,9 @@ static int dummy_hrtimer_stop(struct snd_pcm_substream *substream)
  	struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
 
  	atomic_set(&dpcm->running, 0);
-	hrtimer_cancel(&dpcm->timer);
+	/* issue hrtimer_cancel() only when called outside the callback */
+	if (!atomic_read(&dpcm->callback_running))
+		hrtimer_cancel(&dpcm->timer);
  	return 0;
  }
 
@@ -462,6 +472,7 @@ static int dummy_hrtimer_create(struct snd_pcm_substream *substream)
  	dpcm->timer.function = dummy_hrtimer_callback;
  	dpcm->substream = substream;
  	atomic_set(&dpcm->running, 0);
+	atomic_set(&dpcm->callback_running, 0);
  	return 0;
  }
 
Quoted 1 line

From 07d61ba2a1c0e06e914443225e194d99f2d8c58d Mon Sep 17 00:00:00 2001

From: Takashi Sakamoto <[email protected]>
Date: Fri, 1 Sep 2017 19:10:18 +0900
Subject: [PATCH] ALSA: dummy: avoid stall due to a call of hrtimer_cancel() on
a callback of hrtimer

A call of 'htrimer_cancel()' on a callback of hrtimer brings endless loop because 'struct hrtimer_clock_base.running' is not NULL on the callback. In hrtimer subsystem, this member is used to indicate the instance of hrtimer gets callbacks and there's a helper function, 'hrtimer_callback_running()' to check it.

Quoted 6 lines

ALSA dummy driver uses hrtimer to emulate hardware interrupt per period of PCM buffer. When XRUN occurs on PCM substream, in a call of 'snd_pcm_period_elapsed()', 'struct snd_pcm_ops.stop()' is called to stop the substream. In current implementation, 'hrtimer_cancel()' is used to wait for cancellation of hrtimer. However, as described, this brings endless loop.

It's not only about XRUN. When the stream finishes the draining, it stops the stream gracefully -- that is the very normal operation.

Quoted 8 lines

For this problem, this commit uses 'hrtimer_callback_running()' to detect whether to be on a callback of hrtimer or not, then skip cancellation of hrtimer in hrtimer callbacks. Furthermore, at a case of XRUN, hrtimer callback returns HRTIMER_NORESTART after a call of 'snd_pcm_period_elapsed()' to discontinue hrtimr because cancellation is skipped.

It's better to fold the fix into the original patch instead of introducing a bug and fixing it.

Takashi

#59 lore raw

Re: [PATCH 23/25] ALSA/dummy: Replace tasklet with softirq hrtimer

Quoted 19 lines · Takashi Iwai
Quoted 12 lines
Quoted 1 line

From 07d61ba2a1c0e06e914443225e194d99f2d8c58d Mon Sep 17 00:00:00 2001

From: Takashi Sakamoto <[email protected]>
Date: Fri, 1 Sep 2017 19:10:18 +0900
Subject: [PATCH] ALSA: dummy: avoid stall due to a call of hrtimer_cancel() on
a callback of hrtimer

A call of 'htrimer_cancel()' on a callback of hrtimer brings endless loop because 'struct hrtimer_clock_base.running' is not NULL on the callback. In hrtimer subsystem, this member is used to indicate the instance of hrtimer gets callbacks and there's a helper function, 'hrtimer_callback_running()' to check it.

Quoted 6 lines

ALSA dummy driver uses hrtimer to emulate hardware interrupt per period of PCM buffer. When XRUN occurs on PCM substream, in a call of 'snd_pcm_period_elapsed()', 'struct snd_pcm_ops.stop()' is called to stop the substream. In current implementation, 'hrtimer_cancel()' is used to wait for cancellation of hrtimer. However, as described, this brings endless loop.

Quoted 2 lines

It's not only about XRUN. When the stream finishes the draining, it stops the stream gracefully -- that is the very normal operation.

I overlooked it. Thanks for your indication.

Quoted 11 lines
Quoted 8 lines

For this problem, this commit uses 'hrtimer_callback_running()' to detect whether to be on a callback of hrtimer or not, then skip cancellation of hrtimer in hrtimer callbacks. Furthermore, at a case of XRUN, hrtimer callback returns HRTIMER_NORESTART after a call of 'snd_pcm_period_elapsed()' to discontinue hrtimr because cancellation is skipped.

It's better to fold the fix into the original patch instead of introducing a bug and fixing it.

Yep. I request the authors to include this fix.

Well, in sound subsystem, there're a few drivers which uses hrtimer:
- snd-pcsp
- snd-sh-dac-audio
- snd-soc-imx-pcm-fiq

As a quick glance, 'snd-sh-dac-audio' includes the same bug, too. Additionally, 'snd-soc-imx-pcm-fiq' maintains hrtimer with loose manner in a point of state of PCM substream and it shall gain the same bug if improved. Later, I posted some patches for them.

Thanks

Takashi Sakamoto

#60 lore raw

Re: [PATCH 23/25] ALSA/dummy: Replace tasklet with softirq hrtimer

Hi,

Quoted 6 lines · Takashi Sakamoto

Well, in sound subsystem, there're a few drivers which uses hrtimer:
- snd-pcsp
- snd-sh-dac-audio
- snd-soc-imx-pcm-fiq

As a quick glance, 'snd-sh-dac-audio' includes the same bug, too. Additionally, 'snd-soc-imx-pcm-fiq' maintains hrtimer with loose manner in a point of state of PCM substream and it shall gain the same bug if improved. Later, I posted some patches for them.

After reading code thoroughly, I conclude that no need to fix these two drivers. They're programmed with own protections. The former (snd-sh-dac-audio) has 'struct snd_sh_dac.empty' and the latter (snd-soc-imx-pcm-fiq) has 'struct imx_pcm_runtime_data.playing' and '.capturing', to avoid cancellation of hrtimer on hrtimer callback.

These ways are not necessarily efficient but actually have no trouble. I leave them as is.

Regards

Takashi Sakamoto

#61 lore raw

PATCH v2 23/25 ALSA/dummy: Replace tasklet with softirq hrtimer

In reply to Takashi Sakamoto

From: Thomas Gleixner <[email protected]>

The tasklet is used to defer the execution of snd_pcm_period_elapsed() to the softirq context. Using the CLOCK_MONOTONIC_SOFT base invokes the timer callback in softirq context as well which renders the tasklet useless.

[o-takashi: avoid stall due to a call of hrtimer_cancel() on a callback
of hrtimer]

---

Quoted 1 line · Takashi Sakamoto

Yep. I request the authors to include this

Thank you for providing a fix.

v1…v2: merged Takashi Sakamoto fixup of the original patch into v2.

So this patch now is okay?

 sound/drivers/dummy.c | 23 +++++++++--------------
 1 file changed, 9 insertions(+), 14 deletions(-)
sound/drivers/dummy.c +9 -14
index dd5ed037adf2..3d01fe17ed36 100644
@@ -376,17 +376,9 @@ struct dummy_hrtimer_pcm {
 	ktime_t period_time;
 	atomic_t running;
 	struct hrtimer timer;
-	struct tasklet_struct tasklet;
 	struct snd_pcm_substream *substream;
 };
 
-static void dummy_hrtimer_pcm_elapsed(unsigned long priv)
-{
-	struct dummy_hrtimer_pcm *dpcm = (struct dummy_hrtimer_pcm *)priv;
-	if (atomic_read(&dpcm->running))
-		snd_pcm_period_elapsed(dpcm->substream);
-}
-
 static enum hrtimer_restart dummy_hrtimer_callback(struct hrtimer *timer)
 {
 	struct dummy_hrtimer_pcm *dpcm;
@@ -394,7 +386,12 @@ static enum hrtimer_restart dummy_hrtimer_callback(struct hrtimer *timer)
 	dpcm = container_of(timer, struct dummy_hrtimer_pcm, timer);
 	if (!atomic_read(&dpcm->running))
 		return HRTIMER_NORESTART;
-	tasklet_schedule(&dpcm->tasklet);
+
+	/* In a case of XRUN, this calls .trigger to stop PCM substream. */
+	snd_pcm_period_elapsed(dpcm->substream);
+	if (!atomic_read(&dpcm->running))
+		return HRTIMER_NORESTART;
+
 	hrtimer_forward_now(timer, dpcm->period_time);
 	return HRTIMER_RESTART;
 }
@@ -414,14 +411,14 @@ static int dummy_hrtimer_stop(struct snd_pcm_substream *substream)
 	struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
 
 	atomic_set(&dpcm->running, 0);
-	hrtimer_cancel(&dpcm->timer);
+	if (!hrtimer_callback_running(&dpcm->timer))
+		hrtimer_cancel(&dpcm->timer);
 	return 0;
 }
 
 static inline void dummy_hrtimer_sync(struct dummy_hrtimer_pcm *dpcm)
 {
 	hrtimer_cancel(&dpcm->timer);
-	tasklet_kill(&dpcm->tasklet);
 }
 
 static snd_pcm_uframes_t
@@ -466,12 +463,10 @@ static int dummy_hrtimer_create(struct snd_pcm_substream *substream)
 	if (!dpcm)
 		return -ENOMEM;
 	substream->runtime->private_data = dpcm;
-	hrtimer_init(&dpcm->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
+	hrtimer_init(&dpcm->timer, CLOCK_MONOTONIC_SOFT, HRTIMER_MODE_REL);
 	dpcm->timer.function = dummy_hrtimer_callback;
 	dpcm->substream = substream;
 	atomic_set(&dpcm->running, 0);
-	tasklet_init(&dpcm->tasklet, dummy_hrtimer_pcm_elapsed,
-		     (unsigned long)dpcm);
 	return 0;
 }
 
Signature · 6 lines
2.14.1

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel
#62 lore raw

Re: [PATCH 23/25 v2] ALSA/dummy: Replace tasklet with softirq hrtimer

Quoted 17 lines · Sebastian Andrzej Siewior

From: Thomas Gleixner <[email protected]>

The tasklet is used to defer the execution of snd_pcm_period_elapsed() to the softirq context. Using the CLOCK_MONOTONIC_SOFT base invokes the timer callback in softirq context as well which renders the tasklet useless.

[o-takashi: avoid stall due to a call of hrtimer_cancel() on a callback
of hrtimer]

Quoted 8 lines

---

Quoted 1 line · Takashi Sakamoto

Yep. I request the authors to include this

Thank you for providing a fix.

v1…v2: merged Takashi Sakamoto fixup of the original patch into v2.

So this patch now is okay?

Note that you can try it by yourself easily, as it's a dummy driver that doesn't need anything special. Just run aplay for that device (e.g. aplay -Dplughw:2 for card#2) can reproduce the original problem.

sound/drivers/dummy.c static enum hrtimer_restart dummy_hrtimer_callback(struct hrtimer *timer) · line 390

Quoted 7 lines
sound/drivers/dummy.c +2 -1
@@ -394,7 +386,12 @@ static enum hrtimer_restart dummy_hrtimer_callback(struct hrtimer *timer)
 	dpcm = container_of(timer, struct dummy_hrtimer_pcm, timer);
 	if (!atomic_read(&dpcm->running))
 		return HRTIMER_NORESTART;
-	tasklet_schedule(&dpcm->tasklet);
+
+	/* In a case of XRUN, this calls .trigger to stop PCM substream. */

As mentioned, the stop happens not only with XRUN but also in a normal situation by draining.

Other than that, looks OK to me (but not tested it).

thanks,

Takashi

Quoted 41 lines
sound/drivers/dummy.c +7 -5
+	snd_pcm_period_elapsed(dpcm->substream);
+	if (!atomic_read(&dpcm->running))
+		return HRTIMER_NORESTART;
+
 	hrtimer_forward_now(timer, dpcm->period_time);
 	return HRTIMER_RESTART;
 }
@@ -414,14 +411,14 @@ static int dummy_hrtimer_stop(struct snd_pcm_substream *substream)
 	struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
 
 	atomic_set(&dpcm->running, 0);
-	hrtimer_cancel(&dpcm->timer);
+	if (!hrtimer_callback_running(&dpcm->timer))
+		hrtimer_cancel(&dpcm->timer);
 	return 0;
 }
 
 static inline void dummy_hrtimer_sync(struct dummy_hrtimer_pcm *dpcm)
 {
 	hrtimer_cancel(&dpcm->timer);
-	tasklet_kill(&dpcm->tasklet);
 }
 
 static snd_pcm_uframes_t
@@ -466,12 +463,10 @@ static int dummy_hrtimer_create(struct snd_pcm_substream *substream)
 	if (!dpcm)
 		return -ENOMEM;
 	substream->runtime->private_data = dpcm;
-	hrtimer_init(&dpcm->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
+	hrtimer_init(&dpcm->timer, CLOCK_MONOTONIC_SOFT, HRTIMER_MODE_REL);
 	dpcm->timer.function = dummy_hrtimer_callback;
 	dpcm->substream = substream;
 	atomic_set(&dpcm->running, 0);
-	tasklet_init(&dpcm->tasklet, dummy_hrtimer_pcm_elapsed,
-		     (unsigned long)dpcm);
 	return 0;
 }
 
Signature · 1 line
2.14.1

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

#63 lore raw

Re: [PATCH 23/25 v2] ALSA/dummy: Replace tasklet with softirq hrtimer

In reply to Sebastian Andrzej Siewior
Quoted 16 lines · Sebastian Andrzej Siewior

From: Thomas Gleixner <[email protected]>

The tasklet is used to defer the execution of snd_pcm_period_elapsed() to the softirq context. Using the CLOCK_MONOTONIC_SOFT base invokes the timer callback in softirq context as well which renders the tasklet useless.

[o-takashi: avoid stall due to a call of hrtimer_cancel() on a callback
of hrtimer]

Quoted 8 lines

---

Quoted 1 line · Takashi Sakamoto

Yep. I request the authors to include this

Thank you for providing a fix.

v1…v2: merged Takashi Sakamoto fixup of the original patch into v2.

So this patch now is okay?

I have a small nitpick.

Quoted 26 lines
  sound/drivers/dummy.c | 23 +++++++++--------------
  1 file changed, 9 insertions(+), 14 deletions(-)
sound/drivers/dummy.c +0 -8
index dd5ed037adf2..3d01fe17ed36 100644
@@ -376,17 +376,9 @@ struct dummy_hrtimer_pcm {
  	ktime_t period_time;
  	atomic_t running;
  	struct hrtimer timer;
-	struct tasklet_struct tasklet;
  	struct snd_pcm_substream *substream;
  };
 
-static void dummy_hrtimer_pcm_elapsed(unsigned long priv)
-{
-	struct dummy_hrtimer_pcm *dpcm = (struct dummy_hrtimer_pcm *)priv;
-	if (atomic_read(&dpcm->running))
-		snd_pcm_period_elapsed(dpcm->substream);
-}
-
  static enum hrtimer_restart dummy_hrtimer_callback(struct hrtimer *timer)
  {
  	struct dummy_hrtimer_pcm *dpcm;

sound/drivers/dummy.c static enum hrtimer_restart dummy_hrtimer_callback(struct hrtimer *timer) · line 390

Quoted 6 lines
sound/drivers/dummy.c +2 -1
@@ -394,7 +386,12 @@ static enum hrtimer_restart dummy_hrtimer_callback(struct hrtimer *timer)
  	dpcm = container_of(timer, struct dummy_hrtimer_pcm, timer);
  	if (!atomic_read(&dpcm->running))
  		return HRTIMER_NORESTART;
-	tasklet_schedule(&dpcm->tasklet);
+
+	/* In a case of XRUN, this calls .trigger to stop PCM substream. */

As Iwai-san mentioned, in this function, .trigger can be called in two cases; XRUN occurs and draining is done. Thus, let you change the comment as 'In cases of XRUN and draining, this calls .trigger to stop PCM substream.'. I'm sorry to trouble you.

snd_pcm_period_elapsed()
->snd_pcm_update_hw_ptr0()
->snd_pcm_update_state()

    ->snd_pcm_drain_done()
      ...
      ->.trigger(TRIGGER_STOP)
    ->xrun()
      ->snd_pcm_stop()
        ...
        ->.trigger(TRIGGER_STOP)
Quoted 27 lines
sound/drivers/dummy.c +6 -2
+	snd_pcm_period_elapsed(dpcm->substream);
+	if (!atomic_read(&dpcm->running))
+		return HRTIMER_NORESTART;
+
  	hrtimer_forward_now(timer, dpcm->period_time);
  	return HRTIMER_RESTART;
  }
@@ -414,14 +411,14 @@ static int dummy_hrtimer_stop(struct snd_pcm_substream *substream)
  	struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
 
  	atomic_set(&dpcm->running, 0);
-	hrtimer_cancel(&dpcm->timer);
+	if (!hrtimer_callback_running(&dpcm->timer))
+		hrtimer_cancel(&dpcm->timer);
  	return 0;
  }
 
  static inline void dummy_hrtimer_sync(struct dummy_hrtimer_pcm *dpcm)
  {
  	hrtimer_cancel(&dpcm->timer);
-	tasklet_kill(&dpcm->tasklet);
  }
 
  static snd_pcm_uframes_t
@@ -466,12 +463,10 @@ static int dummy_hrtimer_create(struct snd_pcm_substream *substream)
  	if (!dpcm)
  		return -ENOMEM;

sound/drivers/dummy.c static int dummy_hrtimer_create(struct snd_pcm_substream *substream) · line 471

Quoted 10 lines
sound/drivers/dummy.c +1 -3
  	substream->runtime->private_data = dpcm;
-	hrtimer_init(&dpcm->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
+	hrtimer_init(&dpcm->timer, CLOCK_MONOTONIC_SOFT, HRTIMER_MODE_REL);
  	dpcm->timer.function = dummy_hrtimer_callback;
  	dpcm->substream = substream;
  	atomic_set(&dpcm->running, 0);
-	tasklet_init(&dpcm->tasklet, dummy_hrtimer_pcm_elapsed,
-		     (unsigned long)dpcm);
  	return 0;
  }

Regards

Takashi Sakamoto

#64 lore raw

PATCH v3 23/25 ALSA/dummy: Replace tasklet with softirq hrtimer

From: Thomas Gleixner <[email protected]>

The tasklet is used to defer the execution of snd_pcm_period_elapsed() to the softirq context. Using the CLOCK_MONOTONIC_SOFT base invokes the timer callback in softirq context as well which renders the tasklet useless.

[o-takashi: avoid stall due to a call of hrtimer_cancel() on a callback
of hrtimer]

---

Quoted 8 lines · Takashi Sakamoto

As Iwai-san mentioned, in this function, .trigger can be called in two cases; XRUN occurs and draining is done. Thus, let you change the comment as 'In cases of XRUN and draining, this calls .trigger to stop PCM substream.'. I'm sorry to trouble you.

snd_pcm_period_elapsed()
->snd_pcm_update_hw_ptr0()
->snd_pcm_update_state()

Quoted 8 lines
    ->snd_pcm_drain_done()
      ...
      ->.trigger(TRIGGER_STOP)
    ->xrun()
      ->snd_pcm_stop()
        ...
        ->.trigger(TRIGGER_STOP)

I think you asked me just to update the comment. Did I do it right?

v2…v3: updated the comment as per Takashi Sakamoto's suggestion. v1…v2: merged Takashi Sakamoto fixup of the original patch into v2.

 sound/drivers/dummy.c | 25 +++++++++++--------------
 1 file changed, 11 insertions(+), 14 deletions(-)
sound/drivers/dummy.c +11 -14
index dd5ed037adf2..cdd851286f92 100644
@@ -376,17 +376,9 @@ struct dummy_hrtimer_pcm {
 	ktime_t period_time;
 	atomic_t running;
 	struct hrtimer timer;
-	struct tasklet_struct tasklet;
 	struct snd_pcm_substream *substream;
 };
 
-static void dummy_hrtimer_pcm_elapsed(unsigned long priv)
-{
-	struct dummy_hrtimer_pcm *dpcm = (struct dummy_hrtimer_pcm *)priv;
-	if (atomic_read(&dpcm->running))
-		snd_pcm_period_elapsed(dpcm->substream);
-}
-
 static enum hrtimer_restart dummy_hrtimer_callback(struct hrtimer *timer)
 {
 	struct dummy_hrtimer_pcm *dpcm;
@@ -394,7 +386,14 @@ static enum hrtimer_restart dummy_hrtimer_callback(struct hrtimer *timer)
 	dpcm = container_of(timer, struct dummy_hrtimer_pcm, timer);
 	if (!atomic_read(&dpcm->running))
 		return HRTIMER_NORESTART;
-	tasklet_schedule(&dpcm->tasklet);
+	/*
+	 * In cases of XRUN and draining, this calls .trigger to stop PCM
+	 * substream.
+	 */
+	snd_pcm_period_elapsed(dpcm->substream);
+	if (!atomic_read(&dpcm->running))
+		return HRTIMER_NORESTART;
+
 	hrtimer_forward_now(timer, dpcm->period_time);
 	return HRTIMER_RESTART;
 }
@@ -414,14 +413,14 @@ static int dummy_hrtimer_stop(struct snd_pcm_substream *substream)
 	struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
 
 	atomic_set(&dpcm->running, 0);
-	hrtimer_cancel(&dpcm->timer);
+	if (!hrtimer_callback_running(&dpcm->timer))
+		hrtimer_cancel(&dpcm->timer);
 	return 0;
 }
 
 static inline void dummy_hrtimer_sync(struct dummy_hrtimer_pcm *dpcm)
 {
 	hrtimer_cancel(&dpcm->timer);
-	tasklet_kill(&dpcm->tasklet);
 }
 
 static snd_pcm_uframes_t
@@ -466,12 +465,10 @@ static int dummy_hrtimer_create(struct snd_pcm_substream *substream)
 	if (!dpcm)
 		return -ENOMEM;
 	substream->runtime->private_data = dpcm;
-	hrtimer_init(&dpcm->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
+	hrtimer_init(&dpcm->timer, CLOCK_MONOTONIC_SOFT, HRTIMER_MODE_REL);
 	dpcm->timer.function = dummy_hrtimer_callback;
 	dpcm->substream = substream;
 	atomic_set(&dpcm->running, 0);
-	tasklet_init(&dpcm->tasklet, dummy_hrtimer_pcm_elapsed,
-		     (unsigned long)dpcm);
 	return 0;
 }
 
Signature · 6 lines
2.14.1

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel
Tested-by on v3 23/25
#65 lore raw

Re: [PATCH 23/25 v3] ALSA/dummy: Replace tasklet with softirq hrtimer

Quoted 35 lines · Sebastian Andrzej Siewior

From: Thomas Gleixner <[email protected]>

The tasklet is used to defer the execution of snd_pcm_period_elapsed() to the softirq context. Using the CLOCK_MONOTONIC_SOFT base invokes the timer callback in softirq context as well which renders the tasklet useless.

[o-takashi: avoid stall due to a call of hrtimer_cancel() on a callback
of hrtimer]

---

Quoted 8 lines · Takashi Sakamoto

As Iwai-san mentioned, in this function, .trigger can be called in two cases; XRUN occurs and draining is done. Thus, let you change the comment as 'In cases of XRUN and draining, this calls .trigger to stop PCM substream.'. I'm sorry to trouble you.

snd_pcm_period_elapsed()
->snd_pcm_update_hw_ptr0()
->snd_pcm_update_state()

Quoted 8 lines
     ->snd_pcm_drain_done()
       ...
       ->.trigger(TRIGGER_STOP)
     ->xrun()
       ->snd_pcm_stop()
         ...
         ->.trigger(TRIGGER_STOP)
Quoted 7 lines

I think you asked me just to update the comment. Did I do it right?

v2…v3: updated the comment as per Takashi Sakamoto's suggestion. v1…v2: merged Takashi Sakamoto fixup of the original patch into v2.

  sound/drivers/dummy.c | 25 +++++++++++--------------
  1 file changed, 11 insertions(+), 14 deletions(-)

This Looks good to me. I did quick test and confirmed that it brings no stalls.

Quoted 58 lines
sound/drivers/dummy.c +10 -11
index dd5ed037adf2..cdd851286f92 100644
@@ -376,17 +376,9 @@ struct dummy_hrtimer_pcm {
  	ktime_t period_time;
  	atomic_t running;
  	struct hrtimer timer;
-	struct tasklet_struct tasklet;
  	struct snd_pcm_substream *substream;
  };
 
-static void dummy_hrtimer_pcm_elapsed(unsigned long priv)
-{
-	struct dummy_hrtimer_pcm *dpcm = (struct dummy_hrtimer_pcm *)priv;
-	if (atomic_read(&dpcm->running))
-		snd_pcm_period_elapsed(dpcm->substream);
-}
-
  static enum hrtimer_restart dummy_hrtimer_callback(struct hrtimer *timer)
  {
  	struct dummy_hrtimer_pcm *dpcm;
@@ -394,7 +386,14 @@ static enum hrtimer_restart dummy_hrtimer_callback(struct hrtimer *timer)
  	dpcm = container_of(timer, struct dummy_hrtimer_pcm, timer);
  	if (!atomic_read(&dpcm->running))
  		return HRTIMER_NORESTART;
-	tasklet_schedule(&dpcm->tasklet);
+	/*
+	 * In cases of XRUN and draining, this calls .trigger to stop PCM
+	 * substream.
+	 */
+	snd_pcm_period_elapsed(dpcm->substream);
+	if (!atomic_read(&dpcm->running))
+		return HRTIMER_NORESTART;
+
  	hrtimer_forward_now(timer, dpcm->period_time);
  	return HRTIMER_RESTART;
  }
@@ -414,14 +413,14 @@ static int dummy_hrtimer_stop(struct snd_pcm_substream *substream)
  	struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data;
 
  	atomic_set(&dpcm->running, 0);
-	hrtimer_cancel(&dpcm->timer);
+	if (!hrtimer_callback_running(&dpcm->timer))
+		hrtimer_cancel(&dpcm->timer);
  	return 0;
  }
 
  static inline void dummy_hrtimer_sync(struct dummy_hrtimer_pcm *dpcm)
  {
  	hrtimer_cancel(&dpcm->timer);
-	tasklet_kill(&dpcm->tasklet);
  }
 
  static snd_pcm_uframes_t
@@ -466,12 +465,10 @@ static int dummy_hrtimer_create(struct snd_pcm_substream *substream)
  	if (!dpcm)
  		return -ENOMEM;

sound/drivers/dummy.c static int dummy_hrtimer_create(struct snd_pcm_substream *substream) · line 473

Quoted 10 lines
sound/drivers/dummy.c +1 -3
  	substream->runtime->private_data = dpcm;
-	hrtimer_init(&dpcm->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
+	hrtimer_init(&dpcm->timer, CLOCK_MONOTONIC_SOFT, HRTIMER_MODE_REL);
  	dpcm->timer.function = dummy_hrtimer_callback;
  	dpcm->substream = substream;
  	atomic_set(&dpcm->running, 0);
-	tasklet_init(&dpcm->tasklet, dummy_hrtimer_pcm_elapsed,
-		     (unsigned long)dpcm);
  	return 0;
  }

Thanks

Takashi Sakamoto
_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

Reviewed-by
#66 lore raw

Re: [alsa-devel] [PATCH 23/25 v3] ALSA/dummy: Replace tasklet with softirq hrtimer

Quoted 42 lines · Takashi Sakamoto
Quoted 40 lines · Sebastian Andrzej Siewior

From: Thomas Gleixner <[email protected]>

The tasklet is used to defer the execution of snd_pcm_period_elapsed() to the softirq context. Using the CLOCK_MONOTONIC_SOFT base invokes the timer callback in softirq context as well which renders the tasklet useless.

[o-takashi: avoid stall due to a call of hrtimer_cancel() on a callback
of hrtimer]

---

Quoted 8 lines · Takashi Sakamoto

As Iwai-san mentioned, in this function, .trigger can be called in two cases; XRUN occurs and draining is done. Thus, let you change the comment as 'In cases of XRUN and draining, this calls .trigger to stop PCM substream.'. I'm sorry to trouble you.

snd_pcm_period_elapsed()
->snd_pcm_update_hw_ptr0()
->snd_pcm_update_state()

Quoted 8 lines
     ->snd_pcm_drain_done()
       ...
       ->.trigger(TRIGGER_STOP)
     ->xrun()
       ->snd_pcm_stop()
         ...
         ->.trigger(TRIGGER_STOP)

I think you asked me just to update the comment. Did I do it right?

v2…v3: updated the comment as per Takashi Sakamoto's suggestion. v1…v2: merged Takashi Sakamoto fixup of the original patch into v2.

Quoted 7 lines
Quoted 2 lines
  sound/drivers/dummy.c | 25 +++++++++++--------------
  1 file changed, 11 insertions(+), 14 deletions(-)

This Looks good to me. I did quick test and confirmed that it brings no stalls.

I suppose you'd apply all together in some tree like tip or whatever? In anyway, feel free to take my ack for this patch:

Thanks!

Takashi
_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

author maintainer
#67 lore raw

PATCH 24/25 net/cdc_ncm: Replace tasklet with softirq hrtimer

In reply to Anna-Maria Gleixner

From: Thomas Gleixner <[email protected]>

The bh tasklet is used in invoke the hrtimer (cdc_ncm_tx_timer_cb) in softirq context. This can be also achieved without the tasklet but with CLOCK_MONOTONIC_SOFT as hrtimer base.

---
 drivers/net/usb/cdc_ncm.c   |   37 ++++++++++++++++---------------------
 include/linux/usb/cdc_ncm.h |    2 +-
 2 files changed, 17 insertions(+), 22 deletions(-)
drivers/net/usb/cdc_ncm.c +16 -21
@@ -61,7 +61,6 @@ static bool prefer_mbim;
 module_param(prefer_mbim, bool, S_IRUGO | S_IWUSR);
 MODULE_PARM_DESC(prefer_mbim, "Prefer MBIM setting on dual NCM/MBIM functions");
 
-static void cdc_ncm_txpath_bh(unsigned long param);
 static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx);
 static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *hr_timer);
 static struct usb_driver cdc_ncm_driver;
@@ -777,10 +776,9 @@ int cdc_ncm_bind_common(struct usbnet *d
 	if (!ctx)
 		return -ENOMEM;
 
-	hrtimer_init(&ctx->tx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
+	hrtimer_init(&ctx->tx_timer, CLOCK_MONOTONIC_SOFT, HRTIMER_MODE_REL);
 	ctx->tx_timer.function = &cdc_ncm_tx_timer_cb;
-	ctx->bh.data = (unsigned long)dev;
-	ctx->bh.func = cdc_ncm_txpath_bh;
+	ctx->usbnet = dev;
 	atomic_set(&ctx->stop, 0);
 	spin_lock_init(&ctx->mtx);
 
@@ -967,10 +965,7 @@ void cdc_ncm_unbind(struct usbnet *dev,
 
 	atomic_set(&ctx->stop, 1);
 
-	if (hrtimer_active(&ctx->tx_timer))
-		hrtimer_cancel(&ctx->tx_timer);
-
-	tasklet_kill(&ctx->bh);
+	hrtimer_cancel(&ctx->tx_timer);
 
 	/* handle devices with combined control and data interface */
 	if (ctx->control == ctx->data)
@@ -1348,20 +1343,9 @@ static void cdc_ncm_tx_timeout_start(str
 				HRTIMER_MODE_REL);
 }
 
-static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *timer)
+static void cdc_ncm_txpath_bh(struct cdc_ncm_ctx *ctx)
 {
-	struct cdc_ncm_ctx *ctx =
-			container_of(timer, struct cdc_ncm_ctx, tx_timer);
-
-	if (!atomic_read(&ctx->stop))
-		tasklet_schedule(&ctx->bh);
-	return HRTIMER_NORESTART;
-}
-
-static void cdc_ncm_txpath_bh(unsigned long param)
-{
-	struct usbnet *dev = (struct usbnet *)param;
-	struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
+	struct usbnet *dev = ctx->usbnet;
 
 	spin_lock_bh(&ctx->mtx);
 	if (ctx->tx_timer_pending != 0) {
@@ -1379,6 +1363,17 @@ static void cdc_ncm_txpath_bh(unsigned l
 	}
 }
 
+static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *timer)
+{
+	struct cdc_ncm_ctx *ctx =
+			container_of(timer, struct cdc_ncm_ctx, tx_timer);
+
+	if (!atomic_read(&ctx->stop))
+		cdc_ncm_txpath_bh(ctx);
+
+	return HRTIMER_NORESTART;
+}
+
 struct sk_buff *
 cdc_ncm_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
 {
include/linux/usb/cdc_ncm.h +1 -1
@@ -92,7 +92,6 @@
 struct cdc_ncm_ctx {
 	struct usb_cdc_ncm_ntb_parameters ncm_parm;
 	struct hrtimer tx_timer;
-	struct tasklet_struct bh;
 
 	const struct usb_cdc_ncm_desc *func_desc;
 	const struct usb_cdc_mbim_desc *mbim_desc;
@@ -101,6 +100,7 @@ struct cdc_ncm_ctx {
 
 	struct usb_interface *control;
 	struct usb_interface *data;
+	struct usbnet *usbnet;
 
 	struct sk_buff *tx_curr_skb;
 	struct sk_buff *tx_rem_skb;
maintainer Acked-by on v1 24/25
#68 lore raw

Re: [PATCH 24/25] net/cdc_ncm: Replace tasklet with softirq hrtimer

Quoted 6 lines · Anna-Maria Gleixner

From: Thomas Gleixner <[email protected]>

The bh tasklet is used in invoke the hrtimer (cdc_ncm_tx_timer_cb) in softirq context. This can be also achieved without the tasklet but with CLOCK_MONOTONIC_SOFT as hrtimer base.

Quoted 6 lines
#69 lore raw

Re: [PATCH 24/25] net/cdc_ncm: Replace tasklet with softirq hrtimer

In reply to Anna-Maria Gleixner
Quoted 104 lines · Anna-Maria Gleixner

From: Thomas Gleixner <[email protected]>

The bh tasklet is used in invoke the hrtimer (cdc_ncm_tx_timer_cb) in softirq context. This can be also achieved without the tasklet but with CLOCK_MONOTONIC_SOFT as hrtimer base.

---
 drivers/net/usb/cdc_ncm.c   |   37 ++++++++++++++++---------------------
 include/linux/usb/cdc_ncm.h |    2 +-
 2 files changed, 17 insertions(+), 22 deletions(-)
drivers/net/usb/cdc_ncm.c +16 -21
@@ -61,7 +61,6 @@ static bool prefer_mbim;
 module_param(prefer_mbim, bool, S_IRUGO | S_IWUSR);
 MODULE_PARM_DESC(prefer_mbim, "Prefer MBIM setting on dual NCM/MBIM functions");
 
-static void cdc_ncm_txpath_bh(unsigned long param);
 static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx);
 static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *hr_timer);
 static struct usb_driver cdc_ncm_driver;
@@ -777,10 +776,9 @@ int cdc_ncm_bind_common(struct usbnet *d
 	if (!ctx)
 		return -ENOMEM;
 
-	hrtimer_init(&ctx->tx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
+	hrtimer_init(&ctx->tx_timer, CLOCK_MONOTONIC_SOFT, HRTIMER_MODE_REL);
 	ctx->tx_timer.function = &cdc_ncm_tx_timer_cb;
-	ctx->bh.data = (unsigned long)dev;
-	ctx->bh.func = cdc_ncm_txpath_bh;
+	ctx->usbnet = dev;
 	atomic_set(&ctx->stop, 0);
 	spin_lock_init(&ctx->mtx);
 
@@ -967,10 +965,7 @@ void cdc_ncm_unbind(struct usbnet *dev,
 
 	atomic_set(&ctx->stop, 1);
 
-	if (hrtimer_active(&ctx->tx_timer))
-		hrtimer_cancel(&ctx->tx_timer);
-
-	tasklet_kill(&ctx->bh);
+	hrtimer_cancel(&ctx->tx_timer);
 
 	/* handle devices with combined control and data interface */
 	if (ctx->control == ctx->data)
@@ -1348,20 +1343,9 @@ static void cdc_ncm_tx_timeout_start(str
 				HRTIMER_MODE_REL);
 }
 
-static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *timer)
+static void cdc_ncm_txpath_bh(struct cdc_ncm_ctx *ctx)
 {
-	struct cdc_ncm_ctx *ctx =
-			container_of(timer, struct cdc_ncm_ctx, tx_timer);
-
-	if (!atomic_read(&ctx->stop))
-		tasklet_schedule(&ctx->bh);
-	return HRTIMER_NORESTART;
-}
-
-static void cdc_ncm_txpath_bh(unsigned long param)
-{
-	struct usbnet *dev = (struct usbnet *)param;
-	struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
+	struct usbnet *dev = ctx->usbnet;
 
 	spin_lock_bh(&ctx->mtx);
 	if (ctx->tx_timer_pending != 0) {
@@ -1379,6 +1363,17 @@ static void cdc_ncm_txpath_bh(unsigned l
 	}
 }
 
+static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *timer)
+{
+	struct cdc_ncm_ctx *ctx =
+			container_of(timer, struct cdc_ncm_ctx, tx_timer);
+
+	if (!atomic_read(&ctx->stop))
+		cdc_ncm_txpath_bh(ctx);
+
+	return HRTIMER_NORESTART;
+}
+
 struct sk_buff *
 cdc_ncm_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
 {
include/linux/usb/cdc_ncm.h +0 -1
@@ -92,7 +92,6 @@
 struct cdc_ncm_ctx {
 	struct usb_cdc_ncm_ntb_parameters ncm_parm;
 	struct hrtimer tx_timer;
-	struct tasklet_struct bh;
 
 	const struct usb_cdc_ncm_desc *func_desc;
 	const struct usb_cdc_mbim_desc *mbim_desc;

include/linux/usb/cdc_ncm.h struct cdc_ncm_ctx { · line 106

Quoted 7 lines
include/linux/usb/cdc_ncm.h +1 -0
@@ -101,6 +100,7 @@ struct cdc_ncm_ctx {
 
 	struct usb_interface *control;
 	struct usb_interface *data;
+	struct usbnet *usbnet;
 
 	struct sk_buff *tx_curr_skb;
 	struct sk_buff *tx_rem_skb;

I believe the struct usbnet pointer is redundant. We already have lots of pointers back and forth here. This should work, but is not tested:

	struct usbnet *dev = usb_get_intfdata(ctx->control):

Bjørn

#70 lore raw

PATCH v2 24/25 net/cdc_ncm: Replace tasklet with softirq hrtimer

From: Thomas Gleixner <[email protected]>

The bh tasklet is used in invoke the hrtimer (cdc_ncm_tx_timer_cb) in softirq context. This can be also achieved without the tasklet but with CLOCK_MONOTONIC_SOFT as hrtimer base.

Cc Oliver Neukum <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
[bigeasy: using usb_get_intfdata() as suggested by Bjørn Mork]

---

Quoted 4 lines · Bjørn Mork

I believe the struct usbnet pointer is redundant. We already have lots of pointers back and forth here. This should work, but is not tested:

	struct usbnet *dev = usb_get_intfdata(ctx->control):

I think so, too. Still untested as I don't have a working gadget around.

v1…v2: Updated as suggested by Bjørn and added Greg's Acked-by.

 drivers/net/usb/cdc_ncm.c   | 36 +++++++++++++++---------------------
 include/linux/usb/cdc_ncm.h |  1 -
 2 files changed, 15 insertions(+), 22 deletions(-)
drivers/net/usb/cdc_ncm.c +15 -21
index 8f572b9f3625..42f7bd90e6a4 100644
@@ -61,7 +61,6 @@ static bool prefer_mbim;
 module_param(prefer_mbim, bool, S_IRUGO | S_IWUSR);
 MODULE_PARM_DESC(prefer_mbim, "Prefer MBIM setting on dual NCM/MBIM functions");
 
-static void cdc_ncm_txpath_bh(unsigned long param);
 static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx);
 static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *hr_timer);
 static struct usb_driver cdc_ncm_driver;
@@ -777,10 +776,8 @@ int cdc_ncm_bind_common(struct usbnet *dev, struct usb_interface *intf, u8 data_
 	if (!ctx)
 		return -ENOMEM;
 
-	hrtimer_init(&ctx->tx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
+	hrtimer_init(&ctx->tx_timer, CLOCK_MONOTONIC_SOFT, HRTIMER_MODE_REL);
 	ctx->tx_timer.function = &cdc_ncm_tx_timer_cb;
-	ctx->bh.data = (unsigned long)dev;
-	ctx->bh.func = cdc_ncm_txpath_bh;
 	atomic_set(&ctx->stop, 0);
 	spin_lock_init(&ctx->mtx);
 
@@ -967,10 +964,7 @@ void cdc_ncm_unbind(struct usbnet *dev, struct usb_interface *intf)
 
 	atomic_set(&ctx->stop, 1);
 
-	if (hrtimer_active(&ctx->tx_timer))
-		hrtimer_cancel(&ctx->tx_timer);
-
-	tasklet_kill(&ctx->bh);
+	hrtimer_cancel(&ctx->tx_timer);
 
 	/* handle devices with combined control and data interface */
 	if (ctx->control == ctx->data)
@@ -1348,20 +1342,9 @@ static void cdc_ncm_tx_timeout_start(struct cdc_ncm_ctx *ctx)
 				HRTIMER_MODE_REL);
 }
 
-static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *timer)
+static void cdc_ncm_txpath_bh(struct cdc_ncm_ctx *ctx)
 {
-	struct cdc_ncm_ctx *ctx =
-			container_of(timer, struct cdc_ncm_ctx, tx_timer);
-
-	if (!atomic_read(&ctx->stop))
-		tasklet_schedule(&ctx->bh);
-	return HRTIMER_NORESTART;
-}
-
-static void cdc_ncm_txpath_bh(unsigned long param)
-{
-	struct usbnet *dev = (struct usbnet *)param;
-	struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
+	struct usbnet *dev = usb_get_intfdata(ctx->control);
 
 	spin_lock_bh(&ctx->mtx);
 	if (ctx->tx_timer_pending != 0) {
@@ -1379,6 +1362,17 @@ static void cdc_ncm_txpath_bh(unsigned long param)
 	}
 }
 
+static enum hrtimer_restart cdc_ncm_tx_timer_cb(struct hrtimer *timer)
+{
+	struct cdc_ncm_ctx *ctx =
+			container_of(timer, struct cdc_ncm_ctx, tx_timer);
+
+	if (!atomic_read(&ctx->stop))
+		cdc_ncm_txpath_bh(ctx);
+
+	return HRTIMER_NORESTART;
+}
+
 struct sk_buff *
 cdc_ncm_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
 {
include/linux/usb/cdc_ncm.h +0 -1
index 1a59699cf82a..62b506fddf8d 100644
@@ -92,7 +92,6 @@
 struct cdc_ncm_ctx {
 	struct usb_cdc_ncm_ntb_parameters ncm_parm;
 	struct hrtimer tx_timer;
-	struct tasklet_struct bh;
 
 	const struct usb_cdc_ncm_desc *func_desc;
 	const struct usb_cdc_mbim_desc *mbim_desc;
Signature · 1 line
2.14.1
author maintainer
#71 lore raw

PATCH 25/25 usb/gadget/NCM: Replace tasklet with softirq hrtimer

In reply to Anna-Maria Gleixner

From: Thomas Gleixner <[email protected]>

The tx_tasklet tasklet is used in invoke the hrtimer (task_timer) in softirq context. This can be also achieved without the tasklet but with CLOCK_MONOTONIC_SOFT as hrtimer base.

---
 drivers/usb/gadget/function/f_ncm.c |   28 ++++++----------------------
 1 file changed, 6 insertions(+), 22 deletions(-)
drivers/usb/gadget/function/f_ncm.c +6 -22
@@ -77,9 +77,7 @@ struct f_ncm {
 	struct sk_buff			*skb_tx_ndp;
 	u16				ndp_dgram_count;
 	bool				timer_force_tx;
-	struct tasklet_struct		tx_tasklet;
 	struct hrtimer			task_timer;
-
 	bool				timer_stopping;
 };
 
@@ -1154,17 +1152,15 @@ static struct sk_buff *ncm_wrap_ntb(stru
 }
 
 /*
- * This transmits the NTB if there are frames waiting.
+ * The transmit should only be run if no skb data has been sent
+ * for a certain duration.
  */
-static void ncm_tx_tasklet(unsigned long data)
+static enum hrtimer_restart ncm_tx_timeout(struct hrtimer *data)
 {
-	struct f_ncm	*ncm = (void *)data;
-
-	if (ncm->timer_stopping)
-		return;
+	struct f_ncm *ncm = container_of(data, struct f_ncm, task_timer);
 
 	/* Only send if data is available. */
-	if (ncm->skb_tx_data) {
+	if (!ncm->timer_stopping && ncm->skb_tx_data) {
 		ncm->timer_force_tx = true;
 
 		/* XXX This allowance of a NULL skb argument to ndo_start_xmit
@@ -1177,16 +1173,6 @@ static void ncm_tx_tasklet(unsigned long
 
 		ncm->timer_force_tx = false;
 	}
-}
-
-/*
- * The transmit should only be run if no skb data has been sent
- * for a certain duration.
- */
-static enum hrtimer_restart ncm_tx_timeout(struct hrtimer *data)
-{
-	struct f_ncm *ncm = container_of(data, struct f_ncm, task_timer);
-	tasklet_schedule(&ncm->tx_tasklet);
 	return HRTIMER_NORESTART;
 }
 
@@ -1519,8 +1505,7 @@ static int ncm_bind(struct usb_configura
 	ncm->port.open = ncm_open;
 	ncm->port.close = ncm_close;
 
-	tasklet_init(&ncm->tx_tasklet, ncm_tx_tasklet, (unsigned long) ncm);
-	hrtimer_init(&ncm->task_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
+	hrtimer_init(&ncm->task_timer, CLOCK_MONOTONIC_SOFT, HRTIMER_MODE_REL);
 	ncm->task_timer.function = ncm_tx_timeout;
 
 	DBG(cdev, "CDC Network: %s speed IN/%s OUT/%s NOTIFY/%s\n",
@@ -1629,7 +1614,6 @@ static void ncm_unbind(struct usb_config
 	DBG(c->cdev, "ncm unbind\n");
 
 	hrtimer_cancel(&ncm->task_timer);
-	tasklet_kill(&ncm->tx_tasklet);
 
 	ncm_string_defs[0].id = 0;
 	usb_free_all_descriptors(f);
Acked-by on v1 25/25
#72 lore raw

Re: [PATCH 25/25] usb/gadget/NCM: Replace tasklet with softirq hrtimer

Quoted 10 lines · Anna-Maria Gleixner

From: Thomas Gleixner <[email protected]>

The tx_tasklet tasklet is used in invoke the hrtimer (task_timer) in softirq context. This can be also achieved without the tasklet but with CLOCK_MONOTONIC_SOFT as hrtimer base.

Signature · 1 line
balbi
author maintainer
#73 lore raw
In reply to Anna-Maria Gleixner

I'm sorry for the typo in the email address!

Anna-Maria

Quoted 61 lines · Anna-Maria Gleixner

There are quite some places in the kernel which use a combination of hrtimers and tasklets to make use of the precise expiry of hrtimers, which schedule a tasklet to bring the actual function into softirq context.

This was introduced when the previous hrtimer softirq code was removed. That code was implemented by expiring the timer in hard irq context and then deferring the execution of the callback into softirq context. That caused a lot of pointless shuffling between the rbtree and a linked list.

In recent discussions it turned out that more potential users of hrtimers in softirq context might come up. Aside of that the RT patches need this functionality as well to defer hrtimers into softirq context if their callbacks are not interrupt safe on RT.

This series implements a new approach by adding SOFT_* clock ids and instead of doing the list shuffle, timers started with these clock ids are put into separate soft expiry hrtimer queues. These queues are evaluated only when the hardirq context detects that the first expiring timer in the softirq queues has expired. That makes the overhead in the hardirq context minimal.

The series reworks the code to reuse as much as possible from the existing facilities for the new softirq hrtimers and integrates them with all flavours of hrtimers (HIGH_RES=y/n - NOHZ=y/n).

To achieve this quite some of the conditionals in the existing code are removed for the price of adding some pointless data and state tracking to the HIGH_RES=n case. That's minimal, but well worth it as it increases the readability and maintainability of the code.

The first part of the series implements the new functionality and the second part converts the hrtimer/tasklet users to make use of it and removes struct hrtimer_tasklet and the surrounding helper functions.

This series is available from git as well:

git://git.kernel.org/pub/scm/linux/kernel/git/bigeasy/linux-hrtimer.git soft_hrtimer
https://git.kernel.org/bigeasy/linux-hrtimer/h/soft_hrtimer

Thanks,

Anna-Maria

---
 drivers/net/usb/cdc_ncm.c             |   37 +-
 drivers/net/wireless/mac80211_hwsim.c |   44 +-
 drivers/usb/gadget/function/f_ncm.c   |   28 -
 include/linux/hrtimer.h               |   76 ++---
 include/linux/interrupt.h             |   25 -
 include/linux/usb/cdc_ncm.h           |    2 
 include/net/xfrm.h                    |    2 
 kernel/softirq.c                      |   51 ---
 kernel/time/hrtimer.c                 |  513 +++++++++++++++++++++-------------
 net/can/bcm.c                         |  150 +++------
 net/xfrm/xfrm_state.c                 |   29 +
 sound/drivers/dummy.c                 |   16 -
 12 files changed, 484 insertions(+), 489 deletions(-)