xen: RCU: Improve the idle timer handling
v3 cover xen: RCU: Improve the idle timer handling
And now take 3.
v1:
https://lists.xen.org/archives/html/xen-devel/2017-09/msg01855.html
v2:
https://lists.xen.org/archives/html/xen-devel/2017-09/msg03515.html
I've just took care of Jan's comments to v2. Details in single patches.
Thanks and Regards,
Dario
---
Dario Faggioli (3):
xen: RCU: let the RCU idle timer handler run
xen: RCU: make the period of the idle timer configurable.
xen: RCU: make the period of the idle timer adaptive.
docs/misc/xen-command-line.markdown | 10 +++++ xen/common/rcupdate.c | 67 ++++++++++++++++++++++++++++++++--- xen/common/timer.c | 17 +++++++++ xen/include/xen/timer.h | 5 +++ 4 files changed, 93 insertions(+), 6 deletions(-)
--
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://about.me/dario.faggioli
Senior Software Engineer, Citrix Systems R&D Ltd., Cambridge (UK)
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH v3 1/3 xen: RCU: let the RCU idle timer handler run
If stop_timer() is called between when the RCU idle timer's interrupt arrives (and TIMER_SOFTIRQ is raised) and when softirqs are checked and handled, the timer is deactivated, and the handler never runs.
This happens to the RCU idle timer because stop_timer() is called on it during the wakeup from idle (e.g., C-states, on x86) path.
To fix that, we avoid calling stop_timer(), in case we see that the timer itself is:
- still active,
- expired (i.e., it's expiry time is in the past).
In fact, that indicates (for this particular timer) that it has fired, and we are just about to handle the TIMER_SOFTIRQ (which will perform the timer deactivation and run its handler).
---
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Tim Deegan <[email protected]>
---
Changes from v2:
- improved comment in rcu_idle_timer_stop();
- introduce a more generic timer_expires_before() function, and make timer_is_expired() a macro which does not take any time parameter.
Changes from v1:
- logic changed completely: instead of avoiding deactivate_timer() in stop_timer(), we avoid stop_timer() in rcu_idle_timer_stop() (if appropriate, of course).
--- xen/common/rcupdate.c | 19 ++++++++++++++++++- xen/common/timer.c | 17 +++++++++++++++++ xen/include/xen/timer.h | 5 +++++ 3 files changed, 40 insertions(+), 1 deletion(-)
xen/common/rcupdate.c +18 -1
@@ -465,7 +465,24 @@ void rcu_idle_timer_stop()return ; rdp->idle_timer_active = false; -stop_timer (&rdp->idle_timer); + +/* +* In general, as the CPU is becoming active again, we don't need the +* idle timer, and so we want to stop it. +* +* However, in case we are here because idle_timer has (just) fired and +* has woken up the CPU, we skip stop_timer() now. In fact, when a CPU +* wakes up from idle, this code always runs before do_softirq() has the +* chance to check and deal with TIMER_SOFTIRQ. And if we stop the timer +* now, the TIMER_SOFTIRQ handler will see it as inactive, and will not +* call rcu_idle_timer_handler(). +* +* Therefore, if we see that the timer is expired already, we leave it +* alone. The TIMER_SOFTIRQ handler will then run the timer routine, and +* deactivate it. +*/ +if ( !timer_is_expired (&rdp->idle_timer) ) +stop_timer (&rdp->idle_timer); }static void rcu_idle_timer_handler (void * data)
xen/common/timer.c +17 -0
@@ -332,6 +332,23 @@ void stop_timer(struct timer *timer) } +bool timer_expires_before (struct timer *timer,s_time_t t) +{ +unsigned long flags; +bool ret = false; + +if ( !timer_lock_irqsave (timer, flags) ) +return ret; + +if (active_timer (timer) && timer->expires <= t ) + ret = true; + +timer_unlock_irqrestore (timer, flags); + +return ret; +} + +void migrate_timer (struct timer *timer,unsigned int new_cpu) {unsigned int old_cpu;
xen/include/xen/timer.h +5 -0
@@ -70,6 +70,11 @@ void set_timer(struct timer *timer, s_time_t expires);*/ void stop_timer (struct timer *timer); +/* True if a timer is active, and its expiry time is earlier than t. */ +bool timer_expires_before (struct timer *timer,s_time_t t); + +#define timer_is_expired (t) timer_expires_before(t, NOW()) +/* Migrate a timer to a different CPU. The timer may be currently active. */ void migrate_timer (struct timer *timer,unsigned int new_cpu);
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v3 1/3] xen: RCU: let the RCU idle timer handler run
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 28.09.17 at 19:06, <[email protected]> wrote:
xen/common/timer.c void stop_timer(struct timer *timer) · line 341
Quoted 10 lines
xen/common/timer.c +7
-0@@ -332,6 +332,23 @@ void stop_timer(struct timer *timer) } +bool timer_expires_before (struct timer *timer,s_time_t t) +{ +unsigned long flags; +bool ret = false; + +if ( !timer_lock_irqsave (timer, flags) ) +return ret;
I think it would be easier to follow if you used just "false" here.
xen/common/timer.c void stop_timer(struct timer *timer) · line 344
Quoted 2 lines
xen/common/timer.c +2
-0+if (active_timer (timer) && timer->expires <= t ) + ret = true;
In which case this could then be a simple assignment, with the variable's initializer dropped.
xen/common/timer.c void stop_timer(struct timer *timer) · line 352
Quoted 7 lines
xen/common/timer.c +6
-0+timer_unlock_irqrestore (timer, flags); + +return ret; +} + +void migrate_timer (struct timer *timer,unsigned int new_cpu)
Please don't introduce further double blank lines. Instead, insert the new function between the two existing ones.
With at least the latter addressed
Of course both should be easy to take care of while committing, should no other reason arise for sending v4.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v3 1/3] xen: RCU: let the RCU idle timer handler run
Quoted 7 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 28.09.17 at 19:06, <[email protected]> wrote:
With at least the latter addressed
Reviewed-by Jan Beulich <[email protected]>Of course both should be easy to take care of while committing, should no other reason arise for sending v4.
Which, AFAICS, you've done, and checked in the series.
Thanks!
Dario
Signature · 3 lines
<<This happens because I choose it to happen!>> (Raistlin Majere) ----------------------------------------------------------------- Dario Faggioli, Ph.D, http://about.me/dario.faggioli
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH v3 2/3 xen: RCU: make the period of the idle timer configurable.
In reply to Dario FaggioliMake it possible for the user to specify, with the boot time parameter rcu-idle-timer-period-ms, how frequently a CPU that went idle with pending RCU callbacks should be woken up to check if the grace period ended.
Typical values (i.e., some of the values used by Linux as the tick frequency) are 10, 4 or 1 ms. Default valus (used when this parameter is not specified) is 10ms. Maximum is 100ms.
---
Cc: Jan Beulich <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Stefano Stabellini <[email protected]>,
Cc: Tim Deegan <[email protected]>
---
Changes from v2:
- use '!= 0' and "(0,..]", while sanitizing the boot parameter value;
- move the param variable, as well as the integer_param() inside rcu_init().
Changes from v1:
- "-" instead of "_" in the boot parameter name;
- enforce a minimum value as well;
- use integer_param(), instead of custom_param().
--- docs/misc/xen-command-line.markdown | 10 ++++++++++ xen/common/rcupdate.c | 28 ++++++++++++++++++++++++---- 2 files changed, 34 insertions(+), 4 deletions(-)
docs/misc/xen-command-line.markdown +10 -0
@@ -1422,6 +1422,16 @@ The following resources are available: sum of CBMs is fixed, that means actual `cos_max` in use will automatically reduce to half when CDP is enabled. +### rcu-idle-timer-period-ms +> `= <integer>` + +> Default: `10` + +How frequently a CPU which has gone idle, but with pending RCU callbacks, +should be woken up to check if the grace period has completed, and the +callbacks are safe to be executed. Expressed in milliseconds; maximum is +100, and it can't be 0. + ### reboot > `= t[riple] | k[bd] | a[cpi] | p[ci] | P[ower] | e[fi] | n[o] [, [w]arm | [c]old]`
xen/common/rcupdate.c +24 -4
@@ -110,10 +110,16 @@ struct rcu_data {* About how far in the future the timer should be programmed each time, * it's hard to tell (guess!!). Since this mimics Linux's periodic timer * tick, take values used there as an indication. In Linux 2.6.21, tick -* period can be 10ms, 4ms, 3.33ms or 1ms. Let's use 10ms, to enable -* at least some power saving on the CPU that is going idle. +* period can be 10ms, 4ms, 3.33ms or 1ms. +* +* By default, we use 10ms, to enable at least some power saving on the +* CPU that is going idle. The user can change this, via a boot time +* parameter, but only up to 100ms. */ -#define RCU_IDLE_TIMER_PERIOD MILLISECS(10) +#define IDLE_TIMER_PERIOD_MAX MILLISECS(100) +#define IDLE_TIMER_PERIOD_DEFAULT MILLISECS(10) + +static s_time_t __read_mostly idle_timer_period; static DEFINE_PER_CPU(struct rcu_data, rcu_data);
@@ -453,7 +459,7 @@ void rcu_idle_timer_start()if (likely (!rdp->curlist))return ; -set_timer (&rdp->idle_timer,NOW () +RCU_IDLE_TIMER_PERIOD ); +set_timer (&rdp->idle_timer,NOW () + idle_timer_period); rdp->idle_timer_active = true; }
@@ -571,6 +577,20 @@ static struct notifier_block cpu_nfb = {void __init rcu_init (void ) {void *cpu = (void *)(long )smp_processor_id (); +static unsigned int __initdata idle_timer_period_ms = +IDLE_TIMER_PERIOD_DEFAULT /MILLISECS (1 ); +integer_param ("rcu-idle-timer-period-ms" , idle_timer_period_ms); + +/* We don't allow 0, or anything higher than IDLE_TIMER_PERIOD_MAX */ +if ( idle_timer_period_ms ==0 || + idle_timer_period_ms >IDLE_TIMER_PERIOD_MAX /MILLISECS (1 ) ) + { + idle_timer_period_ms =IDLE_TIMER_PERIOD_DEFAULT /MILLISECS (1 ); +printk ("WARNING: rcu-idle-timer-period-ms outside of " +"(0,%" PRI_stime"]. Resetting it to %u.\n" , +IDLE_TIMER_PERIOD_MAX /MILLISECS (1 ), idle_timer_period_ms); + } + idle_timer_period =MILLISECS (idle_timer_period_ms);cpumask_clear (&rcu_ctrlblk.idle_cpumask);cpu_callback (&cpu_nfb,CPU_UP_PREPARE , cpu);
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v3 2/3] xen: RCU: make the period of the idle timer configurable.
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 28.09.17 at 19:06, <[email protected]> wrote:
Make it possible for the user to specify, with the boot time parameter rcu-idle-timer-period-ms, how frequently a CPU that went idle with pending RCU callbacks should be woken up to check if the grace period ended.
Quoted 6 lines
Typical values (i.e., some of the values used by Linux as the tick frequency) are 10, 4 or 1 ms. Default valus (used when this parameter is not specified) is 10ms. Maximum is 100ms.
Signed-off-by Dario Faggioli <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH v3 3/3 xen: RCU: make the period of the idle timer adaptive.
In reply to Dario FaggioliBasically, if the RCU idle timer, when (if!) it fires, finds that the grace period isn't over, we increase the timer's period (i.e., it will fire later, next time). If, OTOH, it finds the grace period is already finished, we decrease the timer's period (i.e., it will fire a bit earlier next time).
The goal is to let the period timer sefl-adjust to a number of 'misses', of the order of 1%.
---
Cc: Julien Grall <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Stefano Stabellini <[email protected]>,
Cc: Tim Deegan <[email protected]>
---
Changes from v1:
- removed pointless braches;
- switched from min_t() to min(), and from max_t() to max().
--- xen/common/rcupdate.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-)
xen/common/rcupdate.c +19 -1
@@ -118,9 +118,21 @@ struct rcu_data {*/ #define IDLE_TIMER_PERIOD_MAX MILLISECS(100)#define IDLE_TIMER_PERIOD_DEFAULT MILLISECS(10) +#define IDLE_TIMER_PERIOD_MIN MICROSECS(100)static s_time_t __read_mostly idle_timer_period; +/* +* Increment and decrement values for the idle timer handler. The algorithm +* works as follows: +* - if the timer actually fires, and it finds out that the grace period isn't +* over yet, we add IDLE_TIMER_PERIOD_INCR to the timer's period; +* - if the timer actually fires and it finds the grace period over, we +* subtract IDLE_TIMER_PERIOD_DECR from the timer's period. +*/ +#define IDLE_TIMER_PERIOD_INCR MILLISECS(10) +#define IDLE_TIMER_PERIOD_DECR MICROSECS(100) + static DEFINE_PER_CPU(struct rcu_data, rcu_data);static int blimit =10 ;
@@ -493,8 +505,14 @@ void rcu_idle_timer_stop()static void rcu_idle_timer_handler (void * data) { -/* Nothing, really... Just count the number of times we fire */ perfc_incr (rcu_idle_timer); + +if ( !cpumask_empty (&rcu_ctrlblk.cpumask) ) + idle_timer_period =min (idle_timer_period +IDLE_TIMER_PERIOD_INCR , +IDLE_TIMER_PERIOD_MAX ); +else + idle_timer_period =max (idle_timer_period -IDLE_TIMER_PERIOD_DECR , +IDLE_TIMER_PERIOD_MIN ); }void rcu_check_callbacks (int cpu)
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel