RCU: reimplement RCU barrier to avoid deadlock
PATCH 1/1 RCU: reimplement RCU barrier to avoid deadlock
The existing RCU barrier implementation is prone to a deadlock scenario due to IRQs being re-enabled inside stopmachine context. If due to a race IRQs are re-enabled on some of CPUs and softirqs are allowed to be processed in stopmachine, i.e. what currently happens in rcu_barrier(), timer interrupt is able to invoke TSC synchronization rendezvous. At this moment sending TSC synchronization IPI will stall waiting for other CPUs to synchronize while they in turn are waiting in stopmachine busy loop with IRQs disabled.
To avoid the scenario above - reimplement rcu_barrier() in a way where IRQs are not being disabled at any moment. The proposed implementation is just a simplified and specialized version of stopmachine. The semantic of the call is preserved.
---
This change has been stress tested by doing actions invoking rcu_barrier() functionality and didn't show any issues.
--- xen/common/rcupdate.c | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-)
xen/common/rcupdate.c +26 -10
@@ -145,6 +145,9 @@ struct rcu_barrier_data {atomic_t *cpu_count; }; +static DEFINE_PER_CPU(struct tasklet, rcu_barrier_tasklet); +static atomic_t rcu_barrier_cpu_count, rcu_barrier_cpu_done; +static void rcu_barrier_callback (struct rcu_head *head) { struct rcu_barrier_data *data = container_of(
@@ -152,12 +155,9 @@ static void rcu_barrier_callback(struct rcu_head *head)atomic_inc (data->cpu_count); } -static int rcu_barrier_action (void *_cpu_count) +static void rcu_barrier_action (void *unused) { -struct rcu_barrier_data data = { .cpu_count = _cpu_count }; - -ASSERT (!local_irq_is_enabled ()); -local_irq_enable (); +struct rcu_barrier_data data = { .cpu_count = &rcu_barrier_cpu_count };/* * When callback is executed, all previously-queued RCU work on this CPU
@@ -172,15 +172,30 @@ static int rcu_barrier_action(void *_cpu_count)cpu_relax (); } -local_irq_disable (); - -return 0 ; +atomic_inc (&rcu_barrier_cpu_done); }int rcu_barrier (void ) { -atomic_t cpu_count =ATOMIC_INIT (0 ); -return stop_machine_run (rcu_barrier_action, &cpu_count,NR_CPUS ); +unsigned int i; + +if ( !get_cpu_maps () ) +return -EBUSY ; + +atomic_set (&rcu_barrier_cpu_count,0 ); +atomic_set (&rcu_barrier_cpu_done,0 ); + +for_each_online_cpu ( i ) + if ( i != smp_processor_id() ) + tasklet_schedule_on_cpu(&per_cpu(rcu_barrier_tasklet, i), i); + +rcu_barrier_action (NULL); + +while (atomic_read (&rcu_barrier_cpu_done) !=num_online_cpus () ) +cpu_relax (); + +put_cpu_maps (); +return 0 ; }/* Is batch a before batch b ? */
@@ -564,6 +579,7 @@ static void rcu_init_percpu_data(int cpu, struct rcu_ctrlblk *rcp, rdp->cpu = cpu; rdp->blimit = blimit;init_timer (&rdp->idle_timer, rcu_idle_timer_handler, rdp, cpu); +tasklet_init (&per_cpu (rcu_barrier_tasklet, cpu), rcu_barrier_action,NULL ); } static int cpu_callback(
Signature · 7 lines
2.7.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 9 lines · Igor Druzhinin
The existing RCU barrier implementation is prone to a deadlock scenario due to IRQs being re-enabled inside stopmachine context. If due to a race IRQs are re-enabled on some of CPUs and softirqs are allowed to be processed in stopmachine, i.e. what currently happens in
rcu_barrier(), timer interrupt is able to invoke TSC synchronization rendezvous. At this moment sending TSC synchronization IPI will stall waiting for other CPUs to synchronize while they in turn are waiting in stopmachine busy loop with IRQs disabled.
Quoted 4 lines
To avoid the scenario above - reimplement
rcu_barrier()in a way where IRQs are not being disabled at any moment. The proposed implementation is just a simplified and specialized version of stopmachine. The semantic of the call is preserved.
stop_machine_run() is used in a few places within Xen. Why is this a problem for rcu_barrier() and not for the other callers?
Quoted 40 lines
Signed-off-by Igor Druzhinin <[email protected]>---
This change has been stress tested by doing actions invokingrcu_barrier()functionality and didn't show any issues.--- xen/common/rcupdate.c | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-)xen/common/rcupdate.c +5
-5@@ -145,6 +145,9 @@ struct rcu_barrier_data {atomic_t *cpu_count; }; +static DEFINE_PER_CPU(struct tasklet, rcu_barrier_tasklet); +static atomic_t rcu_barrier_cpu_count, rcu_barrier_cpu_done; +static void rcu_barrier_callback (struct rcu_head *head) { struct rcu_barrier_data *data = container_of(@@ -152,12 +155,9 @@ static void rcu_barrier_callback(struct rcu_head *head)atomic_inc (data->cpu_count); } -static int rcu_barrier_action (void *_cpu_count) +static void rcu_barrier_action (void *unused) { -struct rcu_barrier_data data = { .cpu_count = _cpu_count }; - -ASSERT (!local_irq_is_enabled ()); -local_irq_enable (); +struct rcu_barrier_data data = { .cpu_count = &rcu_barrier_cpu_count };/* * When callback is executed, all previously-queued RCU work on this CPU @@ -172,15 +172,30 @@ static int rcu_barrier_action(void *_cpu_count)cpu_relax ();
xen/common/rcupdate.c static int rcu_barrier_action(void *_cpu_count) · line 179
Quoted 10 lines
xen/common/rcupdate.c +1
-3} -local_irq_disable (); - -return 0 ; +atomic_inc (&rcu_barrier_cpu_done); }int rcu_barrier (void ) {
stop_machine_run() requires the interrupts to be enabled when called. Is this requirement still the same here? If so, can we document it and add an ASSERT?
xen/common/rcupdate.c static int rcu_barrier_action(void *_cpu_count) · line 183
Quoted 6 lines
xen/common/rcupdate.c +4
-2-atomic_t cpu_count =ATOMIC_INIT (0 ); -return stop_machine_run (rcu_barrier_action, &cpu_count,NR_CPUS ); +unsigned int i; + +if ( !get_cpu_maps () ) +return -EBUSY ;
I realize this is also present in the current implementation. However, nobody seems to check the return of the barrier. What would happen if you continue without synchronizing the RCU?
Quoted 27 lines
xen/common/rcupdate.c +16
-0+ +atomic_set (&rcu_barrier_cpu_count,0 ); +atomic_set (&rcu_barrier_cpu_done,0 ); + +for_each_online_cpu ( i ) + if ( i != smp_processor_id() ) + tasklet_schedule_on_cpu(&per_cpu(rcu_barrier_tasklet, i), i); + +rcu_barrier_action (NULL); + +while (atomic_read (&rcu_barrier_cpu_done) !=num_online_cpus () ) +cpu_relax (); + +put_cpu_maps (); +return 0 ; }/* Is batch a before batch b ? */ @@ -564,6 +579,7 @@ static void rcu_init_percpu_data(int cpu, struct rcu_ctrlblk *rcp, rdp->cpu = cpu; rdp->blimit = blimit;init_timer (&rdp->idle_timer, rcu_idle_timer_handler, rdp, cpu); +tasklet_init (&per_cpu (rcu_barrier_tasklet, cpu), rcu_barrier_action,NULL ); } static int cpu_callback(
Signature · 6 lines
Julien Grall _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 10 lines · Julien Grall
Quoted 9 lines · Igor Druzhinin
The existing RCU barrier implementation is prone to a deadlock scenario due to IRQs being re-enabled inside stopmachine context. If due to a race IRQs are re-enabled on some of CPUs and softirqs are allowed to be processed in stopmachine, i.e. what currently happens in
rcu_barrier(), timer interrupt is able to invoke TSC synchronization rendezvous. At this moment sending TSC synchronization IPI will stall waiting for other CPUs to synchronize while they in turn are waiting in stopmachine busy loop with IRQs disabled.
Quoted 5 lines
Quoted 4 lines
To avoid the scenario above - reimplement
rcu_barrier()in a way where IRQs are not being disabled at any moment. The proposed implementation is just a simplified and specialized version of stopmachine. The semantic of the call is preserved.
stop_machine_run()is used in a few places within Xen. Why is this a problem forrcu_barrier()and not for the other callers?
It's true that some of them do re-enable interrupts (__cpu_disable). The reason they are not prone to the described issue is that currently there is no, likely, interrupt handler that might lockup the system. Nevertheless, there are softirq handlers that do this (TSC sync) and rcu_barrier() has to call process_pending_softirqs() inside stopmachine context due to the nature of its implementation.
Quoted 38 lines
Quoted 38 lines
Signed-off-by Igor Druzhinin <[email protected]>---
This change has been stress tested by doing actions invokingrcu_barrier()functionality and didn't show any issues.
---
xen/common/rcupdate.c| 36 ++++++++++++++++++++++++++----------1 file changed, 26 insertions(+), 10 deletions(-)xen/common/rcupdate.c +2
-0@@ -145,6 +145,9 @@ struct rcu_barrier_data { atomic_t *cpu_count; }; +static DEFINE_PER_CPU(struct tasklet, rcu_barrier_tasklet); +static atomic_t rcu_barrier_cpu_count, rcu_barrier_cpu_done; + static void rcu_barrier_callback(struct rcu_head *head) {struct
rcu_barrier_data*data = container_of(xen/common/rcupdate.c +2
-4@@ -152,12 +155,9 @@ static void rcu_barrier_callback(struct rcu_head *head) atomic_inc(data->cpu_count); } -static int rcu_barrier_action(void *_cpu_count) +static voidrcu_barrier_action (void *unused) { - struct rcu_barrier_data data = { .cpu_count = _cpu_count }; - - ASSERT(!local_irq_is_enabled()); - local_irq_enable(); + struct rcu_barrier_data data = { .cpu_count = &rcu_barrier_cpu_count }; /** When callback is executed, all previously-queued RCU work on this CPU @@ -172,15 +172,30 @@ static int rcu_barrier_action(void *_cpu_count) cpu_relax(); } - local_irq_disable();
Quoted 8 lines
xen/common/rcupdate.c static int rcu_barrier_action(void *_cpu_count) · line 179
Quoted 6 lines
xen/common/rcupdate.c +1
-2- - return 0; + atomic_inc(&rcu_barrier_cpu_done); } int rcu_barrier(void) {
stop_machine_run()requires the interrupts to be enabled when called. Is this requirement still the same here? If so, can we document it and add an ASSERT?
Sure, will add.
Quoted 8 lines
xen/common/rcupdate.c static int rcu_barrier_action(void *_cpu_count) · line 183
Quoted 6 lines
xen/common/rcupdate.c +4
-2- atomic_t cpu_count = ATOMIC_INIT(0); - return stop_machine_run(rcu_barrier_action, &cpu_count, NR_CPUS); + unsigned int i; + + if ( !get_cpu_maps() ) + return -EBUSY;I realize this is also present in the current implementation. However, nobody seems to check the return of the barrier. What would happen if you continue without synchronizing the RCU?
Probably a crash, as from what I saw the existing callers rely on it finishing the task. I either need to change the semantics of the call or fix up the callers that might be affected. I'd prefer to do the latter in a follow up patch.
Igor
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Hi,
Quoted 15 lines · Igor Druzhinin
Quoted 14 lines · Julien Grall
Quoted 13 lines · Igor Druzhinin
The existing RCU barrier implementation is prone to a deadlock scenario due to IRQs being re-enabled inside stopmachine context. If due to a race IRQs are re-enabled on some of CPUs and softirqs are allowed to be processed in stopmachine, i.e. what currently happens in
rcu_barrier(), timer interrupt is able to invoke TSC synchronization rendezvous. At this moment sending TSC synchronization IPI will stall waiting for other CPUs to synchronize while they in turn are waiting in stopmachine busy loop with IRQs disabled.To avoid the scenario above - reimplement
rcu_barrier()in a way where IRQs are not being disabled at any moment. The proposed implementation is just a simplified and specialized version of stopmachine. The semantic of the call is preserved.
Quoted 8 lines
Quoted 1 line
stop_machine_run()is used in a few places within Xen. Why is this a problem forrcu_barrier()and not for the other callers?It's true that some of them do re-enable interrupts (
__cpu_disable). The reason they are not prone to the described issue is that currently there is no, likely, interrupt handler that might lockup the system. Nevertheless, there are softirq handlers that do this (TSC sync) andrcu_barrier()has to callprocess_pending_softirqs()inside stopmachine context due to the nature of its implementation.
Thank you for the information. It feels to me we need to document the expectation of stop_machine_run() so people doesn't hit the problem.
Quoted 57 lines
Quoted 39 lines
Quoted 39 lines
Signed-off-by Igor Druzhinin <[email protected]>---
This change has been stress tested by doing actions invokingrcu_barrier()functionality and didn't show any issues.--- xen/common/rcupdate.c | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-)xen/common/rcupdate.c +4
-4@@ -145,6 +145,9 @@ struct rcu_barrier_data { atomic_t *cpu_count; }; +static DEFINE_PER_CPU(struct tasklet, rcu_barrier_tasklet); +static atomic_t rcu_barrier_cpu_count, rcu_barrier_cpu_done; + static void rcu_barrier_callback(struct rcu_head *head) { struct rcu_barrier_data *data = container_of(@@ -152,12 +155,9 @@ static void rcu_barrier_callback(struct rcu_head *head) atomic_inc(data->cpu_count); } -static int rcu_barrier_action(void *_cpu_count) +static voidrcu_barrier_action (void *unused) { - struct rcu_barrier_data data = { .cpu_count = _cpu_count }; - - ASSERT(!local_irq_is_enabled()); - local_irq_enable(); + struct rcu_barrier_data data = { .cpu_count = &rcu_barrier_cpu_count }; /** When callback is executed, all previously-queued RCU work on this CPU @@ -172,15 +172,30 @@ static int rcu_barrier_action(void *_cpu_count) cpu_relax(); } - local_irq_disable();Quoted 8 lines
xen/common/rcupdate.c static int rcu_barrier_action(void *_cpu_count) · line 179
Quoted 6 lines
xen/common/rcupdate.c +1
-2- - return 0; + atomic_inc(&rcu_barrier_cpu_done); } int rcu_barrier(void) {
stop_machine_run()requires the interrupts to be enabled when called. Is this requirement still the same here? If so, can we document it and add an ASSERT?Sure, will add.
Quoted 7 lines
Quoted 6 lines
xen/common/rcupdate.c +4
-2- atomic_t cpu_count = ATOMIC_INIT(0); - return stop_machine_run(rcu_barrier_action, &cpu_count, NR_CPUS); + unsigned int i; + + if ( !get_cpu_maps() ) + return -EBUSY;
Quoted 6 lines
Quoted 1 line
I realize this is also present in the current implementation. However, nobody seems to check the return of the barrier. What would happen if you continue without synchronizing the RCU?
Probably a crash, as from what I saw the existing callers rely on it finishing the task. I either need to change the semantics of the call or fix up the callers that might be affected. I'd prefer to do the latter in a follow up patch.
Yes, this is a separate issue. But I thought I would point out :).
Cheers,
Signature · 6 lines
Julien Grall _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel