2020-04-28 20:12:32

by Prasad Sodagudi

[permalink] [raw]
Subject: [PATCH v2 1/2] timer: make deferrable cpu unbound timers really not bound to a cpu

From: Joonwoo Park <[email protected]>

When a deferrable work (INIT_DEFERRABLE_WORK, etc.) is queued via
queue_delayed_work() it's probably intended to run the work item on any
CPU that isn't idle. However, we queue the work to run at a later time
by starting a deferrable timer that binds to whatever CPU the work is
queued on which is same with queue_delayed_work_on(smp_processor_id())
effectively.

As a result WORK_CPU_UNBOUND work items aren't really cpu unbound now.
In fact this is perfectly fine with UP kernel and also won't affect much a
system without dyntick with SMP kernel too as every cpus run timers
periodically. But on SMP systems with dyntick current implementation leads
deferrable timers not very scalable because the timer's base which has
queued the deferrable timer won't wake up till next non-deferrable timer
expires even though there are possible other non idle cpus are running
which are able to run expired deferrable timers.

The deferrable work is a good example of the current implementation's
victim like below.

INIT_DEFERRABLE_WORK(&dwork, fn);
CPU 0 CPU 1
queue_delayed_work(wq, &dwork, HZ);
queue_delayed_work_on(WORK_CPU_UNBOUND);
...
__mod_timer() -> queues timer to the
current cpu's timer
base.
...
tick_nohz_idle_enter() -> cpu enters idle.
A second later
cpu 0 is now in idle. cpu 1 exits idle or wasn't in idle so
now it's in active but won't
cpu 0 won't wake up till next handle cpu unbound deferrable timer
non-deferrable timer expires. as it's in cpu 0's timer base.

To make all cpu unbound deferrable timers are scalable, introduce a common
timer base which is only for cpu unbound deferrable timers to make those
are indeed cpu unbound so that can be scheduled by any of non idle cpus.
This common timer fixes scalability issue of delayed work and all other cpu
unbound deferrable timer using implementations.

Signed-off-by: Joonwoo Park <[email protected]>
Signed-off-by: Prasad Sodagudi <[email protected]>
---
kernel/time/timer.c | 36 +++++++++++++++++++++++++++++++-----
1 file changed, 31 insertions(+), 5 deletions(-)

diff --git a/kernel/time/timer.c b/kernel/time/timer.c
index a5221ab..5ab8e33 100644
--- a/kernel/time/timer.c
+++ b/kernel/time/timer.c
@@ -220,6 +220,7 @@ static void timer_update_keys(struct work_struct *work);
static DECLARE_WORK(timer_update_work, timer_update_keys);

#ifdef CONFIG_SMP
+struct timer_base timer_base_deferrable;
unsigned int sysctl_timer_migration = 1;

DEFINE_STATIC_KEY_FALSE(timers_migration_enabled);
@@ -841,8 +842,14 @@ static inline struct timer_base *get_timer_cpu_base(u32 tflags, u32 cpu)
* If the timer is deferrable and NO_HZ_COMMON is set then we need
* to use the deferrable base.
*/
- if (IS_ENABLED(CONFIG_NO_HZ_COMMON) && (tflags & TIMER_DEFERRABLE))
- base = per_cpu_ptr(&timer_bases[BASE_DEF], cpu);
+ if (IS_ENABLED(CONFIG_NO_HZ_COMMON) && (tflags & TIMER_DEFERRABLE)) {
+#ifdef CONFIG_SMP
+ base = &timer_base_deferrable;
+#endif
+ if (tflags & TIMER_PINNED)
+ base = per_cpu_ptr(&timer_bases[BASE_DEF], cpu);
+ }
+
return base;
}

@@ -854,8 +861,14 @@ static inline struct timer_base *get_timer_this_cpu_base(u32 tflags)
* If the timer is deferrable and NO_HZ_COMMON is set then we need
* to use the deferrable base.
*/
- if (IS_ENABLED(CONFIG_NO_HZ_COMMON) && (tflags & TIMER_DEFERRABLE))
- base = this_cpu_ptr(&timer_bases[BASE_DEF]);
+ if (IS_ENABLED(CONFIG_NO_HZ_COMMON) && (tflags & TIMER_DEFERRABLE)) {
+#ifdef CONFIG_SMP
+ base = &timer_base_deferrable;
+#endif
+ if (tflags & TIMER_PINNED)
+ base = this_cpu_ptr(&timer_bases[BASE_DEF]);
+ }
+
return base;
}

@@ -1785,8 +1798,12 @@ static __latent_entropy void run_timer_softirq(struct softirq_action *h)
struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);

__run_timers(base);
- if (IS_ENABLED(CONFIG_NO_HZ_COMMON))
+ if (IS_ENABLED(CONFIG_NO_HZ_COMMON)) {
__run_timers(this_cpu_ptr(&timer_bases[BASE_DEF]));
+ if (tick_do_timer_cpu == TICK_DO_TIMER_NONE ||
+ tick_do_timer_cpu == smp_processor_id())
+ __run_timers(&timer_base_deferrable);
+ }
}

/*
@@ -2025,6 +2042,14 @@ static void __init init_timer_cpu(int cpu)
}
}

+static void __init init_timer_deferrable_global(void)
+{
+ timer_base_deferrable.cpu = nr_cpu_ids;
+ raw_spin_lock_init(&timer_base_deferrable.lock);
+ timer_base_deferrable.clk = jiffies;
+ timer_base_init_expiry_lock(&timer_base_deferrable);
+}
+
static void __init init_timer_cpus(void)
{
int cpu;
@@ -2036,6 +2061,7 @@ static void __init init_timer_cpus(void)
void __init init_timers(void)
{
init_timer_cpus();
+ init_timer_deferrable_global();
open_softirq(TIMER_SOFTIRQ, run_timer_softirq);
}

--
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project


2020-04-28 23:47:01

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH v2 1/2] timer: make deferrable cpu unbound timers really not bound to a cpu

Hi Prasad,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on tip/timers/core]
[also build test ERROR on tip/auto-latest tip/timers/nohz v5.7-rc3 next-20200428]
[if your patch is applied to the wrong git tree, please drop us a note to help
improve the system. BTW, we also suggest to use '--base' option to specify the
base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url: https://github.com/0day-ci/linux/commits/Prasad-Sodagudi/timer-make-deferrable-cpu-unbound-timers-really-not-bound-to-a-cpu/20200429-060558
base: https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git 4479730e9263befbb9ce9563a09563db2acb8f7c
config: i386-tinyconfig (attached as .config)
compiler: gcc-7 (Ubuntu 7.5.0-6ubuntu2) 7.5.0
reproduce:
# save the attached .config to linux build tree
make ARCH=i386

If you fix the issue, kindly add following tag as appropriate
Reported-by: kbuild test robot <[email protected]>

All errors (new ones prefixed by >>):

kernel/time/timer.c: In function 'run_timer_softirq':
>> kernel/time/timer.c:1804:18: error: 'timer_base_deferrable' undeclared (first use in this function); did you mean 'timer_base_lock_expiry'?
__run_timers(&timer_base_deferrable);
^~~~~~~~~~~~~~~~~~~~~
timer_base_lock_expiry
kernel/time/timer.c:1804:18: note: each undeclared identifier is reported only once for each function it appears in
kernel/time/timer.c: In function 'init_timer_deferrable_global':
kernel/time/timer.c:2046:2: error: 'timer_base_deferrable' undeclared (first use in this function); did you mean 'timer_base_lock_expiry'?
timer_base_deferrable.cpu = nr_cpu_ids;
^~~~~~~~~~~~~~~~~~~~~
timer_base_lock_expiry

vim +1804 kernel/time/timer.c

1791
1792 /*
1793 * This function runs timers and the timer-tq in bottom half context.
1794 */
1795 static __latent_entropy void run_timer_softirq(struct softirq_action *h)
1796 {
1797 struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
1798
1799 __run_timers(base);
1800 if (IS_ENABLED(CONFIG_NO_HZ_COMMON)) {
1801 __run_timers(this_cpu_ptr(&timer_bases[BASE_DEF]));
1802 if (tick_do_timer_cpu == TICK_DO_TIMER_NONE ||
1803 tick_do_timer_cpu == smp_processor_id())
> 1804 __run_timers(&timer_base_deferrable);
1805 }
1806 }
1807

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]


Attachments:
(No filename) (2.52 kB)
.config.gz (7.07 kB)
Download all attachments