Skip to content

Commit 82e8c56

Browse files
oleg-nesterovpaulmck
authored andcommitted
rcu_sync: Simplify rcu_sync using new rcu_sync_ops structure
This commit adds the new struct rcu_sync_ops which holds sync/call methods, and turns the function pointers in rcu_sync_struct into an array of struct rcu_sync_ops. This simplifies the "init" helpers by collapsing a switch statement and explicit multiple definitions into a simple assignment and a helper macro, respectively. Signed-off-by: Oleg Nesterov <oleg@redhat.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com> Reviewed-by: Josh Triplett <josh@joshtriplett.org>
1 parent cc44ca8 commit 82e8c56

2 files changed

Lines changed: 45 additions & 57 deletions

File tree

‎include/linux/rcu_sync.h‎

Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include <linux/wait.h>
2727
#include <linux/rcupdate.h>
2828

29+
enum rcu_sync_type { RCU_SYNC, RCU_SCHED_SYNC, RCU_BH_SYNC };
30+
2931
/* Structure to mediate between updaters and fastpath-using readers. */
3032
struct rcu_sync {
3133
int gp_state;
@@ -35,43 +37,9 @@ struct rcu_sync {
3537
int cb_state;
3638
struct rcu_head cb_head;
3739

38-
void (*sync)(void);
39-
void (*call)(struct rcu_head *, void (*)(struct rcu_head *));
40+
enum rcu_sync_type gp_type;
4041
};
4142

42-
#define ___RCU_SYNC_INIT(name) \
43-
.gp_state = 0, \
44-
.gp_count = 0, \
45-
.gp_wait = __WAIT_QUEUE_HEAD_INITIALIZER(name.gp_wait), \
46-
.cb_state = 0
47-
48-
#define __RCU_SCHED_SYNC_INIT(name) { \
49-
___RCU_SYNC_INIT(name), \
50-
.sync = synchronize_sched, \
51-
.call = call_rcu_sched, \
52-
}
53-
54-
#define __RCU_BH_SYNC_INIT(name) { \
55-
___RCU_SYNC_INIT(name), \
56-
.sync = synchronize_rcu_bh, \
57-
.call = call_rcu_bh, \
58-
}
59-
60-
#define __RCU_SYNC_INIT(name) { \
61-
___RCU_SYNC_INIT(name), \
62-
.sync = synchronize_rcu, \
63-
.call = call_rcu, \
64-
}
65-
66-
#define DEFINE_RCU_SCHED_SYNC(name) \
67-
struct rcu_sync name = __RCU_SCHED_SYNC_INIT(name)
68-
69-
#define DEFINE_RCU_BH_SYNC(name) \
70-
struct rcu_sync name = __RCU_BH_SYNC_INIT(name)
71-
72-
#define DEFINE_RCU_SYNC(name) \
73-
struct rcu_sync name = __RCU_SYNC_INIT(name)
74-
7543
/**
7644
* rcu_sync_is_idle() - Are readers permitted to use their fastpaths?
7745
* @rsp: Pointer to rcu_sync structure to use for synchronization
@@ -85,10 +53,28 @@ static inline bool rcu_sync_is_idle(struct rcu_sync *rsp)
8553
return !rsp->gp_state; /* GP_IDLE */
8654
}
8755

88-
enum rcu_sync_type { RCU_SYNC, RCU_SCHED_SYNC, RCU_BH_SYNC };
89-
9056
extern void rcu_sync_init(struct rcu_sync *, enum rcu_sync_type);
9157
extern void rcu_sync_enter(struct rcu_sync *);
9258
extern void rcu_sync_exit(struct rcu_sync *);
9359

60+
#define __RCU_SYNC_INITIALIZER(name, type) { \
61+
.gp_state = 0, \
62+
.gp_count = 0, \
63+
.gp_wait = __WAIT_QUEUE_HEAD_INITIALIZER(name.gp_wait), \
64+
.cb_state = 0, \
65+
.gp_type = type, \
66+
}
67+
68+
#define __DEFINE_RCU_SYNC(name, type) \
69+
struct rcu_sync_struct name = __RCU_SYNC_INITIALIZER(name, type)
70+
71+
#define DEFINE_RCU_SYNC(name) \
72+
__DEFINE_RCU_SYNC(name, RCU_SYNC)
73+
74+
#define DEFINE_RCU_SCHED_SYNC(name) \
75+
__DEFINE_RCU_SYNC(name, RCU_SCHED_SYNC)
76+
77+
#define DEFINE_RCU_BH_SYNC(name) \
78+
__DEFINE_RCU_SYNC(name, RCU_BH_SYNC)
79+
9480
#endif /* _LINUX_RCU_SYNC_H_ */

‎kernel/rcu/sync.c‎

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,24 @@
2323
#include <linux/rcu_sync.h>
2424
#include <linux/sched.h>
2525

26+
static const struct {
27+
void (*sync)(void);
28+
void (*call)(struct rcu_head *, void (*)(struct rcu_head *));
29+
} gp_ops[] = {
30+
[RCU_SYNC] = {
31+
.sync = synchronize_rcu,
32+
.call = call_rcu,
33+
},
34+
[RCU_SCHED_SYNC] = {
35+
.sync = synchronize_sched,
36+
.call = call_rcu_sched,
37+
},
38+
[RCU_BH_SYNC] = {
39+
.sync = synchronize_rcu_bh,
40+
.call = call_rcu_bh,
41+
},
42+
};
43+
2644
enum { GP_IDLE = 0, GP_PENDING, GP_PASSED };
2745
enum { CB_IDLE = 0, CB_PENDING, CB_REPLAY };
2846

@@ -37,23 +55,7 @@ void rcu_sync_init(struct rcu_sync *rsp, enum rcu_sync_type type)
3755
{
3856
memset(rsp, 0, sizeof(*rsp));
3957
init_waitqueue_head(&rsp->gp_wait);
40-
41-
switch (type) {
42-
case RCU_SYNC:
43-
rsp->sync = synchronize_rcu;
44-
rsp->call = call_rcu;
45-
break;
46-
47-
case RCU_SCHED_SYNC:
48-
rsp->sync = synchronize_sched;
49-
rsp->call = call_rcu_sched;
50-
break;
51-
52-
case RCU_BH_SYNC:
53-
rsp->sync = synchronize_rcu_bh;
54-
rsp->call = call_rcu_bh;
55-
break;
56-
}
58+
rsp->gp_type = type;
5759
}
5860

5961
/**
@@ -85,7 +87,7 @@ void rcu_sync_enter(struct rcu_sync *rsp)
8587
BUG_ON(need_wait && need_sync);
8688

8789
if (need_sync) {
88-
rsp->sync();
90+
gp_ops[rsp->gp_type].sync();
8991
rsp->gp_state = GP_PASSED;
9092
wake_up_all(&rsp->gp_wait);
9193
} else if (need_wait) {
@@ -138,7 +140,7 @@ static void rcu_sync_func(struct rcu_head *rcu)
138140
* to catch a later GP.
139141
*/
140142
rsp->cb_state = CB_PENDING;
141-
rsp->call(&rsp->cb_head, rcu_sync_func);
143+
gp_ops[rsp->gp_type].call(&rsp->cb_head, rcu_sync_func);
142144
} else {
143145
/*
144146
* We're at least a GP after rcu_sync_exit(); eveybody will now
@@ -166,7 +168,7 @@ void rcu_sync_exit(struct rcu_sync *rsp)
166168
if (!--rsp->gp_count) {
167169
if (rsp->cb_state == CB_IDLE) {
168170
rsp->cb_state = CB_PENDING;
169-
rsp->call(&rsp->cb_head, rcu_sync_func);
171+
gp_ops[rsp->gp_type].call(&rsp->cb_head, rcu_sync_func);
170172
} else if (rsp->cb_state == CB_PENDING) {
171173
rsp->cb_state = CB_REPLAY;
172174
}

0 commit comments

Comments
 (0)