Skip to content

Commit d4f7dae

Browse files
Frederic WeisbeckerKAGA-KOKO
authored andcommitted
timers: Spare timer softirq until next expiry
Now that the core timer infrastructure doesn't depend anymore on periodic base->clk increments, even when the CPU is not in NO_HZ mode, timer softirqs can be skipped until there are timers to expire. Some spurious softirqs can still remain since base->next_expiry doesn't keep track of canceled timers but this still reduces the number of softirqs significantly: ~15 times less for HZ=1000 and ~5 times less for HZ=100. Signed-off-by: Frederic Weisbecker <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Tested-by: Juri Lelli <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 1f8a421 commit d4f7dae

File tree

1 file changed

+8
-41
lines changed

1 file changed

+8
-41
lines changed

kernel/time/timer.c

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,10 +1458,10 @@ static void expire_timers(struct timer_base *base, struct hlist_head *head)
14581458
}
14591459
}
14601460

1461-
static int __collect_expired_timers(struct timer_base *base,
1462-
struct hlist_head *heads)
1461+
static int collect_expired_timers(struct timer_base *base,
1462+
struct hlist_head *heads)
14631463
{
1464-
unsigned long clk = base->clk;
1464+
unsigned long clk = base->clk = base->next_expiry;
14651465
struct hlist_head *vec;
14661466
int i, levels = 0;
14671467
unsigned int idx;
@@ -1684,40 +1684,6 @@ void timer_clear_idle(void)
16841684
*/
16851685
base->is_idle = false;
16861686
}
1687-
1688-
static int collect_expired_timers(struct timer_base *base,
1689-
struct hlist_head *heads)
1690-
{
1691-
unsigned long now = READ_ONCE(jiffies);
1692-
1693-
/*
1694-
* NOHZ optimization. After a long idle sleep we need to forward the
1695-
* base to current jiffies. Avoid a loop by searching the bitfield for
1696-
* the next expiring timer.
1697-
*/
1698-
if ((long)(now - base->clk) > 2) {
1699-
/*
1700-
* If the next timer is ahead of time forward to current
1701-
* jiffies, otherwise forward to the next expiry time:
1702-
*/
1703-
if (time_after(base->next_expiry, now)) {
1704-
/*
1705-
* The call site will increment base->clk and then
1706-
* terminate the expiry loop immediately.
1707-
*/
1708-
base->clk = now;
1709-
return 0;
1710-
}
1711-
base->clk = base->next_expiry;
1712-
}
1713-
return __collect_expired_timers(base, heads);
1714-
}
1715-
#else
1716-
static inline int collect_expired_timers(struct timer_base *base,
1717-
struct hlist_head *heads)
1718-
{
1719-
return __collect_expired_timers(base, heads);
1720-
}
17211687
#endif
17221688

17231689
/*
@@ -1750,7 +1716,7 @@ static inline void __run_timers(struct timer_base *base)
17501716
struct hlist_head heads[LVL_DEPTH];
17511717
int levels;
17521718

1753-
if (!time_after_eq(jiffies, base->clk))
1719+
if (time_before(jiffies, base->next_expiry))
17541720
return;
17551721

17561722
timer_base_lock_expiry(base);
@@ -1763,7 +1729,8 @@ static inline void __run_timers(struct timer_base *base)
17631729
*/
17641730
base->must_forward_clk = false;
17651731

1766-
while (time_after_eq(jiffies, base->clk)) {
1732+
while (time_after_eq(jiffies, base->clk) &&
1733+
time_after_eq(jiffies, base->next_expiry)) {
17671734

17681735
levels = collect_expired_timers(base, heads);
17691736
base->clk++;
@@ -1798,12 +1765,12 @@ void run_local_timers(void)
17981765

17991766
hrtimer_run_queues();
18001767
/* Raise the softirq only if required. */
1801-
if (time_before(jiffies, base->clk)) {
1768+
if (time_before(jiffies, base->next_expiry)) {
18021769
if (!IS_ENABLED(CONFIG_NO_HZ_COMMON))
18031770
return;
18041771
/* CPU is awake, so check the deferrable base. */
18051772
base++;
1806-
if (time_before(jiffies, base->clk))
1773+
if (time_before(jiffies, base->next_expiry))
18071774
return;
18081775
}
18091776
raise_softirq(TIMER_SOFTIRQ);

0 commit comments

Comments
 (0)