Skip to content

Commit e260be6

Browse files
paulmckIngo Molnar
authored andcommitted
Preempt-RCU: implementation
This patch implements a new version of RCU which allows its read-side critical sections to be preempted. It uses a set of counter pairs to keep track of the read-side critical sections and flips them when all tasks exit read-side critical section. The details of this implementation can be found in this paper - http://www.rdrop.com/users/paulmck/RCU/OLSrtRCU.2006.08.11a.pdf and the article- http://lwn.net/Articles/253651/ This patch was developed as a part of the -rt kernel development and meant to provide better latencies when read-side critical sections of RCU don't disable preemption. As a consequence of keeping track of RCU readers, the readers have a slight overhead (optimizations in the paper). This implementation co-exists with the "classic" RCU implementations and can be switched to at compiler. Also includes RCU tracing summarized in debugfs. [ akpm@linux-foundation.org: build fixes on non-preempt architectures ] Signed-off-by: Gautham R Shenoy <ego@in.ibm.com> Signed-off-by: Dipankar Sarma <dipankar@in.ibm.com> Signed-off-by: Paul E. McKenney <paulmck@us.ibm.com> Reviewed-by: Steven Rostedt <srostedt@redhat.com> Signed-off-by: Ingo Molnar <mingo@elte.hu>
1 parent e0ecfa7 commit e260be6

13 files changed

Lines changed: 1394 additions & 7 deletions

File tree

‎fs/Kconfig‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2130,4 +2130,3 @@ source "fs/nls/Kconfig"
21302130
source "fs/dlm/Kconfig"
21312131

21322132
endmenu
2133-

‎include/linux/rcuclassic.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,5 +157,8 @@ extern void __rcu_init(void);
157157
extern void rcu_check_callbacks(int cpu, int user);
158158
extern void rcu_restart_cpu(int cpu);
159159

160+
extern long rcu_batches_completed(void);
161+
extern long rcu_batches_completed_bh(void);
162+
160163
#endif /* __KERNEL__ */
161164
#endif /* __LINUX_RCUCLASSIC_H */

‎include/linux/rcupdate.h‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ struct rcu_head {
5353
void (*func)(struct rcu_head *head);
5454
};
5555

56+
#ifdef CONFIG_CLASSIC_RCU
5657
#include <linux/rcuclassic.h>
58+
#else /* #ifdef CONFIG_CLASSIC_RCU */
59+
#include <linux/rcupreempt.h>
60+
#endif /* #else #ifdef CONFIG_CLASSIC_RCU */
5761

5862
#define RCU_HEAD_INIT { .next = NULL, .func = NULL }
5963
#define RCU_HEAD(head) struct rcu_head head = RCU_HEAD_INIT
@@ -231,13 +235,12 @@ extern void call_rcu_bh(struct rcu_head *head,
231235
/* Exported common interfaces */
232236
extern void synchronize_rcu(void);
233237
extern void rcu_barrier(void);
238+
extern long rcu_batches_completed(void);
239+
extern long rcu_batches_completed_bh(void);
234240

235241
/* Internal to kernel */
236242
extern void rcu_init(void);
237-
extern void rcu_check_callbacks(int cpu, int user);
238-
239-
extern long rcu_batches_completed(void);
240-
extern long rcu_batches_completed_bh(void);
243+
extern int rcu_needs_cpu(int cpu);
241244

242245
#endif /* __KERNEL__ */
243246
#endif /* __LINUX_RCUPDATE_H */

‎include/linux/rcupreempt.h‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Read-Copy Update mechanism for mutual exclusion (RT implementation)
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17+
*
18+
* Copyright (C) IBM Corporation, 2006
19+
*
20+
* Author: Paul McKenney <paulmck@us.ibm.com>
21+
*
22+
* Based on the original work by Paul McKenney <paul.mckenney@us.ibm.com>
23+
* and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen.
24+
* Papers:
25+
* http://www.rdrop.com/users/paulmck/paper/rclockpdcsproof.pdf
26+
* http://lse.sourceforge.net/locking/rclock_OLS.2001.05.01c.sc.pdf (OLS2001)
27+
*
28+
* For detailed explanation of Read-Copy Update mechanism see -
29+
* Documentation/RCU
30+
*
31+
*/
32+
33+
#ifndef __LINUX_RCUPREEMPT_H
34+
#define __LINUX_RCUPREEMPT_H
35+
36+
#ifdef __KERNEL__
37+
38+
#include <linux/cache.h>
39+
#include <linux/spinlock.h>
40+
#include <linux/threads.h>
41+
#include <linux/percpu.h>
42+
#include <linux/cpumask.h>
43+
#include <linux/seqlock.h>
44+
45+
#define rcu_qsctr_inc(cpu)
46+
#define rcu_bh_qsctr_inc(cpu)
47+
#define call_rcu_bh(head, rcu) call_rcu(head, rcu)
48+
49+
extern void __rcu_read_lock(void);
50+
extern void __rcu_read_unlock(void);
51+
extern int rcu_pending(int cpu);
52+
extern int rcu_needs_cpu(int cpu);
53+
54+
#define __rcu_read_lock_bh() { rcu_read_lock(); local_bh_disable(); }
55+
#define __rcu_read_unlock_bh() { local_bh_enable(); rcu_read_unlock(); }
56+
57+
extern void __synchronize_sched(void);
58+
59+
extern void __rcu_init(void);
60+
extern void rcu_check_callbacks(int cpu, int user);
61+
extern void rcu_restart_cpu(int cpu);
62+
extern long rcu_batches_completed(void);
63+
64+
/*
65+
* Return the number of RCU batches processed thus far. Useful for debug
66+
* and statistic. The _bh variant is identifcal to straight RCU
67+
*/
68+
static inline long rcu_batches_completed_bh(void)
69+
{
70+
return rcu_batches_completed();
71+
}
72+
73+
#ifdef CONFIG_RCU_TRACE
74+
struct rcupreempt_trace;
75+
extern long *rcupreempt_flipctr(int cpu);
76+
extern long rcupreempt_data_completed(void);
77+
extern int rcupreempt_flip_flag(int cpu);
78+
extern int rcupreempt_mb_flag(int cpu);
79+
extern char *rcupreempt_try_flip_state_name(void);
80+
extern struct rcupreempt_trace *rcupreempt_trace_cpu(int cpu);
81+
#endif
82+
83+
struct softirq_action;
84+
85+
#endif /* __KERNEL__ */
86+
#endif /* __LINUX_RCUPREEMPT_H */

‎include/linux/rcupreempt_trace.h‎

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Read-Copy Update mechanism for mutual exclusion (RT implementation)
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17+
*
18+
* Copyright (C) IBM Corporation, 2006
19+
*
20+
* Author: Paul McKenney <paulmck@us.ibm.com>
21+
*
22+
* Based on the original work by Paul McKenney <paul.mckenney@us.ibm.com>
23+
* and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen.
24+
* Papers:
25+
* http://www.rdrop.com/users/paulmck/paper/rclockpdcsproof.pdf
26+
* http://lse.sourceforge.net/locking/rclock_OLS.2001.05.01c.sc.pdf (OLS2001)
27+
*
28+
* For detailed explanation of the Preemptible Read-Copy Update mechanism see -
29+
* http://lwn.net/Articles/253651/
30+
*/
31+
32+
#ifndef __LINUX_RCUPREEMPT_TRACE_H
33+
#define __LINUX_RCUPREEMPT_TRACE_H
34+
35+
#ifdef __KERNEL__
36+
#include <linux/types.h>
37+
#include <linux/kernel.h>
38+
39+
#include <asm/atomic.h>
40+
41+
/*
42+
* PREEMPT_RCU data structures.
43+
*/
44+
45+
struct rcupreempt_trace {
46+
long next_length;
47+
long next_add;
48+
long wait_length;
49+
long wait_add;
50+
long done_length;
51+
long done_add;
52+
long done_remove;
53+
atomic_t done_invoked;
54+
long rcu_check_callbacks;
55+
atomic_t rcu_try_flip_1;
56+
atomic_t rcu_try_flip_e1;
57+
long rcu_try_flip_i1;
58+
long rcu_try_flip_ie1;
59+
long rcu_try_flip_g1;
60+
long rcu_try_flip_a1;
61+
long rcu_try_flip_ae1;
62+
long rcu_try_flip_a2;
63+
long rcu_try_flip_z1;
64+
long rcu_try_flip_ze1;
65+
long rcu_try_flip_z2;
66+
long rcu_try_flip_m1;
67+
long rcu_try_flip_me1;
68+
long rcu_try_flip_m2;
69+
};
70+
71+
#ifdef CONFIG_RCU_TRACE
72+
#define RCU_TRACE(fn, arg) fn(arg);
73+
#else
74+
#define RCU_TRACE(fn, arg)
75+
#endif
76+
77+
extern void rcupreempt_trace_move2done(struct rcupreempt_trace *trace);
78+
extern void rcupreempt_trace_move2wait(struct rcupreempt_trace *trace);
79+
extern void rcupreempt_trace_try_flip_1(struct rcupreempt_trace *trace);
80+
extern void rcupreempt_trace_try_flip_e1(struct rcupreempt_trace *trace);
81+
extern void rcupreempt_trace_try_flip_i1(struct rcupreempt_trace *trace);
82+
extern void rcupreempt_trace_try_flip_ie1(struct rcupreempt_trace *trace);
83+
extern void rcupreempt_trace_try_flip_g1(struct rcupreempt_trace *trace);
84+
extern void rcupreempt_trace_try_flip_a1(struct rcupreempt_trace *trace);
85+
extern void rcupreempt_trace_try_flip_ae1(struct rcupreempt_trace *trace);
86+
extern void rcupreempt_trace_try_flip_a2(struct rcupreempt_trace *trace);
87+
extern void rcupreempt_trace_try_flip_z1(struct rcupreempt_trace *trace);
88+
extern void rcupreempt_trace_try_flip_ze1(struct rcupreempt_trace *trace);
89+
extern void rcupreempt_trace_try_flip_z2(struct rcupreempt_trace *trace);
90+
extern void rcupreempt_trace_try_flip_m1(struct rcupreempt_trace *trace);
91+
extern void rcupreempt_trace_try_flip_me1(struct rcupreempt_trace *trace);
92+
extern void rcupreempt_trace_try_flip_m2(struct rcupreempt_trace *trace);
93+
extern void rcupreempt_trace_check_callbacks(struct rcupreempt_trace *trace);
94+
extern void rcupreempt_trace_done_remove(struct rcupreempt_trace *trace);
95+
extern void rcupreempt_trace_invoke(struct rcupreempt_trace *trace);
96+
extern void rcupreempt_trace_next_add(struct rcupreempt_trace *trace);
97+
98+
#endif /* __KERNEL__ */
99+
#endif /* __LINUX_RCUPREEMPT_TRACE_H */

‎include/linux/sched.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,11 @@ struct task_struct {
974974
int nr_cpus_allowed;
975975
unsigned int time_slice;
976976

977+
#ifdef CONFIG_PREEMPT_RCU
978+
int rcu_read_lock_nesting;
979+
int rcu_flipctr_idx;
980+
#endif /* #ifdef CONFIG_PREEMPT_RCU */
981+
977982
#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
978983
struct sched_info sched_info;
979984
#endif

‎init/Kconfig‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,3 +763,31 @@ source "block/Kconfig"
763763

764764
config PREEMPT_NOTIFIERS
765765
bool
766+
767+
choice
768+
prompt "RCU implementation type:"
769+
default CLASSIC_RCU
770+
771+
config CLASSIC_RCU
772+
bool "Classic RCU"
773+
help
774+
This option selects the classic RCU implementation that is
775+
designed for best read-side performance on non-realtime
776+
systems.
777+
778+
Say Y if you are unsure.
779+
780+
config PREEMPT_RCU
781+
bool "Preemptible RCU"
782+
depends on PREEMPT
783+
help
784+
This option reduces the latency of the kernel by making certain
785+
RCU sections preemptible. Normally RCU code is non-preemptible, if
786+
this option is selected then read-only RCU sections become
787+
preemptible. This helps latency, but may expose bugs due to
788+
now-naive assumptions about each RCU read-side critical section
789+
remaining on a given CPU through its execution.
790+
791+
Say N if you are unsure.
792+
793+
endchoice

‎kernel/Kconfig.preempt‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,13 @@ config PREEMPT_BKL
6363
Say Y here if you are building a kernel for a desktop system.
6464
Say N if you are unsure.
6565

66+
config RCU_TRACE
67+
bool "Enable tracing for RCU - currently stats in debugfs"
68+
select DEBUG_FS
69+
default y
70+
help
71+
This option provides tracing in RCU which presents stats
72+
in debugfs for debugging RCU implementation.
73+
74+
Say Y here if you want to enable RCU tracing
75+
Say N if you are unsure.

‎kernel/Makefile‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ obj-y = sched.o fork.o exec_domain.o panic.o printk.o profile.o \
66
exit.o itimer.o time.o softirq.o resource.o \
77
sysctl.o capability.o ptrace.o timer.o user.o user_namespace.o \
88
signal.o sys.o kmod.o workqueue.o pid.o \
9-
rcupdate.o rcuclassic.o extable.o params.o posix-timers.o \
9+
rcupdate.o extable.o params.o posix-timers.o \
1010
kthread.o wait.o kfifo.o sys_ni.o posix-cpu-timers.o mutex.o \
1111
hrtimer.o rwsem.o latency.o nsproxy.o srcu.o \
1212
utsname.o notifier.o
@@ -52,6 +52,11 @@ obj-$(CONFIG_DETECT_SOFTLOCKUP) += softlockup.o
5252
obj-$(CONFIG_GENERIC_HARDIRQS) += irq/
5353
obj-$(CONFIG_SECCOMP) += seccomp.o
5454
obj-$(CONFIG_RCU_TORTURE_TEST) += rcutorture.o
55+
obj-$(CONFIG_CLASSIC_RCU) += rcuclassic.o
56+
obj-$(CONFIG_PREEMPT_RCU) += rcupreempt.o
57+
ifeq ($(CONFIG_PREEMPT_RCU),y)
58+
obj-$(CONFIG_RCU_TRACE) += rcupreempt_trace.o
59+
endif
5560
obj-$(CONFIG_RELAY) += relay.o
5661
obj-$(CONFIG_SYSCTL) += utsname_sysctl.o
5762
obj-$(CONFIG_TASK_DELAY_ACCT) += delayacct.o

‎kernel/fork.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,10 @@ static struct task_struct *copy_process(unsigned long clone_flags,
10451045
copy_flags(clone_flags, p);
10461046
INIT_LIST_HEAD(&p->children);
10471047
INIT_LIST_HEAD(&p->sibling);
1048+
#ifdef CONFIG_PREEMPT_RCU
1049+
p->rcu_read_lock_nesting = 0;
1050+
p->rcu_flipctr_idx = 0;
1051+
#endif /* #ifdef CONFIG_PREEMPT_RCU */
10481052
p->vfork_done = NULL;
10491053
spin_lock_init(&p->alloc_lock);
10501054

0 commit comments

Comments
 (0)