Skip to content

Commit d8be817

Browse files
committed
srcu: Create a tiny SRCU
In response to automated complaints about modifications to SRCU increasing its size, this commit creates a tiny SRCU that is used in SMP=n && PREEMPT=n builds. Signed-off-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
1 parent dde8da6 commit d8be817

7 files changed

Lines changed: 411 additions & 62 deletions

File tree

‎include/linux/srcu.h‎

Lines changed: 8 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -34,28 +34,7 @@
3434
#include <linux/workqueue.h>
3535
#include <linux/rcu_segcblist.h>
3636

37-
struct srcu_array {
38-
unsigned long lock_count[2];
39-
unsigned long unlock_count[2];
40-
};
41-
42-
struct srcu_struct {
43-
unsigned long completed;
44-
unsigned long srcu_gp_seq;
45-
atomic_t srcu_exp_cnt;
46-
struct srcu_array __percpu *per_cpu_ref;
47-
spinlock_t queue_lock; /* protect ->srcu_cblist */
48-
struct rcu_segcblist srcu_cblist;
49-
struct delayed_work work;
50-
#ifdef CONFIG_DEBUG_LOCK_ALLOC
51-
struct lockdep_map dep_map;
52-
#endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
53-
};
54-
55-
/* Values for -> state variable. */
56-
#define SRCU_STATE_IDLE 0
57-
#define SRCU_STATE_SCAN1 1
58-
#define SRCU_STATE_SCAN2 2
37+
struct srcu_struct;
5938

6039
#ifdef CONFIG_DEBUG_LOCK_ALLOC
6140

@@ -77,42 +56,13 @@ int init_srcu_struct(struct srcu_struct *sp);
7756
#define __SRCU_DEP_MAP_INIT(srcu_name)
7857
#endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
7958

80-
void process_srcu(struct work_struct *work);
81-
82-
#define __SRCU_STRUCT_INIT(name) \
83-
{ \
84-
.completed = -300, \
85-
.per_cpu_ref = &name##_srcu_array, \
86-
.queue_lock = __SPIN_LOCK_UNLOCKED(name.queue_lock), \
87-
.srcu_cblist = RCU_SEGCBLIST_INITIALIZER(name.srcu_cblist),\
88-
.work = __DELAYED_WORK_INITIALIZER(name.work, process_srcu, 0),\
89-
__SRCU_DEP_MAP_INIT(name) \
90-
}
91-
92-
/*
93-
* Define and initialize a srcu struct at build time.
94-
* Do -not- call init_srcu_struct() nor cleanup_srcu_struct() on it.
95-
*
96-
* Note that although DEFINE_STATIC_SRCU() hides the name from other
97-
* files, the per-CPU variable rules nevertheless require that the
98-
* chosen name be globally unique. These rules also prohibit use of
99-
* DEFINE_STATIC_SRCU() within a function. If these rules are too
100-
* restrictive, declare the srcu_struct manually. For example, in
101-
* each file:
102-
*
103-
* static struct srcu_struct my_srcu;
104-
*
105-
* Then, before the first use of each my_srcu, manually initialize it:
106-
*
107-
* init_srcu_struct(&my_srcu);
108-
*
109-
* See include/linux/percpu-defs.h for the rules on per-CPU variables.
110-
*/
111-
#define __DEFINE_SRCU(name, is_static) \
112-
static DEFINE_PER_CPU(struct srcu_array, name##_srcu_array);\
113-
is_static struct srcu_struct name = __SRCU_STRUCT_INIT(name)
114-
#define DEFINE_SRCU(name) __DEFINE_SRCU(name, /* not static */)
115-
#define DEFINE_STATIC_SRCU(name) __DEFINE_SRCU(name, static)
59+
#ifdef CONFIG_TINY_SRCU
60+
#include <linux/srcutiny.h>
61+
#elif defined(CONFIG_TREE_SRCU)
62+
#include <linux/srcutree.h>
63+
#else
64+
#error "Unknown SRCU implementation specified to kernel configuration"
65+
#endif
11666

11767
/**
11868
* call_srcu() - Queue a callback for invocation after an SRCU grace period
@@ -138,9 +88,6 @@ void cleanup_srcu_struct(struct srcu_struct *sp);
13888
int __srcu_read_lock(struct srcu_struct *sp) __acquires(sp);
13989
void __srcu_read_unlock(struct srcu_struct *sp, int idx) __releases(sp);
14090
void synchronize_srcu(struct srcu_struct *sp);
141-
void synchronize_srcu_expedited(struct srcu_struct *sp);
142-
unsigned long srcu_batches_completed(struct srcu_struct *sp);
143-
void srcu_barrier(struct srcu_struct *sp);
14491

14592
#ifdef CONFIG_DEBUG_LOCK_ALLOC
14693

‎include/linux/srcutiny.h‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Sleepable Read-Copy Update mechanism for mutual exclusion,
3+
* tiny variant.
4+
*
5+
* This program is free software; you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation; either version 2 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program; if not, you can access it online at
17+
* http://www.gnu.org/licenses/gpl-2.0.html.
18+
*
19+
* Copyright (C) IBM Corporation, 2017
20+
*
21+
* Author: Paul McKenney <paulmck@us.ibm.com>
22+
*/
23+
24+
#ifndef _LINUX_SRCU_TINY_H
25+
#define _LINUX_SRCU_TINY_H
26+
27+
#include <linux/swait.h>
28+
29+
struct srcu_struct {
30+
int srcu_lock_nesting[2]; /* srcu_read_lock() nesting depth. */
31+
struct swait_queue_head srcu_wq;
32+
/* Last srcu_read_unlock() wakes GP. */
33+
unsigned long srcu_gp_seq; /* GP seq # for callback tagging. */
34+
struct rcu_segcblist srcu_cblist;
35+
/* Pending SRCU callbacks. */
36+
int srcu_idx; /* Current reader array element. */
37+
bool srcu_gp_running; /* GP workqueue running? */
38+
bool srcu_gp_waiting; /* GP waiting for readers? */
39+
struct work_struct srcu_work; /* For driving grace periods. */
40+
#ifdef CONFIG_DEBUG_LOCK_ALLOC
41+
struct lockdep_map dep_map;
42+
#endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
43+
};
44+
45+
void srcu_drive_gp(struct work_struct *wp);
46+
47+
#define __SRCU_STRUCT_INIT(name) \
48+
{ \
49+
.srcu_wq = __SWAIT_QUEUE_HEAD_INITIALIZER(name.srcu_wq), \
50+
.srcu_cblist = RCU_SEGCBLIST_INITIALIZER(name.srcu_cblist), \
51+
.srcu_work = __WORK_INITIALIZER(name.srcu_work, srcu_drive_gp), \
52+
__SRCU_DEP_MAP_INIT(name) \
53+
}
54+
55+
/*
56+
* This odd _STATIC_ arrangement is needed for API compatibility with
57+
* Tree SRCU, which needs some per-CPU data.
58+
*/
59+
#define DEFINE_SRCU(name) \
60+
struct srcu_struct name = __SRCU_STRUCT_INIT(name)
61+
#define DEFINE_STATIC_SRCU(name) \
62+
static struct srcu_struct name = __SRCU_STRUCT_INIT(name)
63+
64+
void synchronize_srcu(struct srcu_struct *sp);
65+
66+
static inline void synchronize_srcu_expedited(struct srcu_struct *sp)
67+
{
68+
synchronize_srcu(sp);
69+
}
70+
71+
static inline void srcu_barrier(struct srcu_struct *sp)
72+
{
73+
synchronize_srcu(sp);
74+
}
75+
76+
static inline unsigned long srcu_batches_completed(struct srcu_struct *sp)
77+
{
78+
return 0;
79+
}
80+
81+
#endif

‎include/linux/srcutree.h‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Sleepable Read-Copy Update mechanism for mutual exclusion,
3+
* tree variant.
4+
*
5+
* This program is free software; you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation; either version 2 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program; if not, you can access it online at
17+
* http://www.gnu.org/licenses/gpl-2.0.html.
18+
*
19+
* Copyright (C) IBM Corporation, 2017
20+
*
21+
* Author: Paul McKenney <paulmck@us.ibm.com>
22+
*/
23+
24+
#ifndef _LINUX_SRCU_TREE_H
25+
#define _LINUX_SRCU_TREE_H
26+
27+
struct srcu_array {
28+
unsigned long lock_count[2];
29+
unsigned long unlock_count[2];
30+
};
31+
32+
struct srcu_struct {
33+
unsigned long completed;
34+
unsigned long srcu_gp_seq;
35+
atomic_t srcu_exp_cnt;
36+
struct srcu_array __percpu *per_cpu_ref;
37+
spinlock_t queue_lock; /* protect ->srcu_cblist */
38+
struct rcu_segcblist srcu_cblist;
39+
struct delayed_work work;
40+
#ifdef CONFIG_DEBUG_LOCK_ALLOC
41+
struct lockdep_map dep_map;
42+
#endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
43+
};
44+
45+
/* Values for -> state variable. */
46+
#define SRCU_STATE_IDLE 0
47+
#define SRCU_STATE_SCAN1 1
48+
#define SRCU_STATE_SCAN2 2
49+
50+
void process_srcu(struct work_struct *work);
51+
52+
#define __SRCU_STRUCT_INIT(name) \
53+
{ \
54+
.completed = -300, \
55+
.per_cpu_ref = &name##_srcu_array, \
56+
.queue_lock = __SPIN_LOCK_UNLOCKED(name.queue_lock), \
57+
.srcu_cblist = RCU_SEGCBLIST_INITIALIZER(name.srcu_cblist),\
58+
.work = __DELAYED_WORK_INITIALIZER(name.work, process_srcu, 0),\
59+
__SRCU_DEP_MAP_INIT(name) \
60+
}
61+
62+
/*
63+
* Define and initialize a srcu struct at build time.
64+
* Do -not- call init_srcu_struct() nor cleanup_srcu_struct() on it.
65+
*
66+
* Note that although DEFINE_STATIC_SRCU() hides the name from other
67+
* files, the per-CPU variable rules nevertheless require that the
68+
* chosen name be globally unique. These rules also prohibit use of
69+
* DEFINE_STATIC_SRCU() within a function. If these rules are too
70+
* restrictive, declare the srcu_struct manually. For example, in
71+
* each file:
72+
*
73+
* static struct srcu_struct my_srcu;
74+
*
75+
* Then, before the first use of each my_srcu, manually initialize it:
76+
*
77+
* init_srcu_struct(&my_srcu);
78+
*
79+
* See include/linux/percpu-defs.h for the rules on per-CPU variables.
80+
*/
81+
#define __DEFINE_SRCU(name, is_static) \
82+
static DEFINE_PER_CPU(struct srcu_array, name##_srcu_array);\
83+
is_static struct srcu_struct name = __SRCU_STRUCT_INIT(name)
84+
#define DEFINE_SRCU(name) __DEFINE_SRCU(name, /* not static */)
85+
#define DEFINE_STATIC_SRCU(name) __DEFINE_SRCU(name, static)
86+
87+
void synchronize_srcu_expedited(struct srcu_struct *sp);
88+
void srcu_barrier(struct srcu_struct *sp);
89+
unsigned long srcu_batches_completed(struct srcu_struct *sp);
90+
91+
#endif

‎init/Kconfig‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,18 @@ config SRCU
526526
permits arbitrary sleeping or blocking within RCU read-side critical
527527
sections.
528528

529+
config TINY_SRCU
530+
bool
531+
default y if TINY_RCU
532+
help
533+
This option selects the single-CPU non-preemptible version of SRCU.
534+
535+
config TREE_SRCU
536+
bool
537+
default y if !TINY_RCU
538+
help
539+
This option selects the full-fledged version of SRCU.
540+
529541
config TASKS_RCU
530542
bool
531543
default n

‎kernel/rcu/Makefile‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
KCOV_INSTRUMENT := n
44

55
obj-y += update.o sync.o
6-
obj-$(CONFIG_SRCU) += srcu.o
6+
obj-$(CONFIG_TREE_SRCU) += srcu.o
7+
obj-$(CONFIG_TINY_SRCU) += srcutiny.o
78
obj-$(CONFIG_RCU_TORTURE_TEST) += rcutorture.o
89
obj-$(CONFIG_RCU_PERF_TEST) += rcuperf.o
910
obj-$(CONFIG_TREE_RCU) += tree.o

‎kernel/rcu/rcutorture.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,7 @@ static void srcu_torture_barrier(void)
559559

560560
static void srcu_torture_stats(void)
561561
{
562+
#ifdef CONFIG_TREE_SRCU
562563
int cpu;
563564
int idx = srcu_ctlp->completed & 0x1;
564565

@@ -587,6 +588,7 @@ static void srcu_torture_stats(void)
587588
pr_cont(" %d(%ld,%ld)", cpu, c0, c1);
588589
}
589590
pr_cont("\n");
591+
#endif
590592
}
591593

592594
static void srcu_torture_synchronize_expedited(void)

0 commit comments

Comments
 (0)