Skip to content

Commit fe15b50

Browse files
author
Paul E. McKenney
committed
srcu: Allocate per-CPU data for DEFINE_SRCU() in modules
Adding DEFINE_SRCU() or DEFINE_STATIC_SRCU() to a loadable module requires that the size of the reserved region be increased, which is not something we want to be doing all that often. One approach would be to require that loadable modules define an srcu_struct and invoke init_srcu_struct() from their module_init function and cleanup_srcu_struct() from their module_exit function. However, this is more than a bit user unfriendly. This commit therefore creates an ___srcu_struct_ptrs linker section, and pointers to srcu_struct structures created by DEFINE_SRCU() and DEFINE_STATIC_SRCU() within a module are placed into that module's ___srcu_struct_ptrs section. The required init_srcu_struct() and cleanup_srcu_struct() functions are then automatically invoked as needed when that module is loaded and unloaded, thus allowing modules to continue to use DEFINE_SRCU() and DEFINE_STATIC_SRCU() while avoiding the need to increase the size of the reserved region. Many of the algorithms and some of the code was cheerfully cherry-picked from other code making use of linker sections, perhaps most notably from tracepoints. All bugs are nevertheless the sole property of the author. Suggested-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> [ paulmck: Use __section() and use "default" in srcu_module_notify()'s "switch" statement as suggested by Joel Fernandes. ] Signed-off-by: Paul E. McKenney <paulmck@linux.ibm.com> Tested-by: Joel Fernandes (Google) <joel@joelfernandes.org>
1 parent a188339 commit fe15b50

5 files changed

Lines changed: 90 additions & 3 deletions

File tree

‎include/asm-generic/vmlinux.lds.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,10 @@
337337
KEEP(*(__tracepoints_ptrs)) /* Tracepoints: pointer array */ \
338338
__stop___tracepoints_ptrs = .; \
339339
*(__tracepoints_strings)/* Tracepoints: strings */ \
340+
. = ALIGN(8); \
341+
__start___srcu_struct = .; \
342+
*(___srcu_struct_ptrs) \
343+
__end___srcu_struct = .; \
340344
} \
341345
\
342346
.rodata1 : AT(ADDR(.rodata1) - LOAD_OFFSET) { \

‎include/linux/module.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <linux/rbtree_latch.h>
2222
#include <linux/error-injection.h>
2323
#include <linux/tracepoint-defs.h>
24+
#include <linux/srcu.h>
2425

2526
#include <linux/percpu.h>
2627
#include <asm/module.h>
@@ -450,6 +451,10 @@ struct module {
450451
unsigned int num_tracepoints;
451452
tracepoint_ptr_t *tracepoints_ptrs;
452453
#endif
454+
#ifdef CONFIG_TREE_SRCU
455+
unsigned int num_srcu_structs;
456+
struct srcu_struct **srcu_struct_ptrs;
457+
#endif
453458
#ifdef CONFIG_BPF_EVENTS
454459
unsigned int num_bpf_raw_events;
455460
struct bpf_raw_event_map *bpf_raw_events;

‎include/linux/srcutree.h‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,17 @@ struct srcu_struct {
120120
*
121121
* See include/linux/percpu-defs.h for the rules on per-CPU variables.
122122
*/
123-
#define __DEFINE_SRCU(name, is_static) \
124-
static DEFINE_PER_CPU(struct srcu_data, name##_srcu_data);\
125-
is_static struct srcu_struct name = __SRCU_STRUCT_INIT(name, name##_srcu_data)
123+
#ifdef MODULE
124+
# define __DEFINE_SRCU(name, is_static) \
125+
is_static struct srcu_struct name; \
126+
struct srcu_struct *__srcu_struct_##name \
127+
__section("___srcu_struct_ptrs") = &name
128+
#else
129+
# define __DEFINE_SRCU(name, is_static) \
130+
static DEFINE_PER_CPU(struct srcu_data, name##_srcu_data); \
131+
is_static struct srcu_struct name = \
132+
__SRCU_STRUCT_INIT(name, name##_srcu_data)
133+
#endif
126134
#define DEFINE_SRCU(name) __DEFINE_SRCU(name, /* not static */)
127135
#define DEFINE_STATIC_SRCU(name) __DEFINE_SRCU(name, static)
128136

‎kernel/module.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3095,6 +3095,11 @@ static int find_module_sections(struct module *mod, struct load_info *info)
30953095
sizeof(*mod->tracepoints_ptrs),
30963096
&mod->num_tracepoints);
30973097
#endif
3098+
#ifdef CONFIG_TREE_SRCU
3099+
mod->srcu_struct_ptrs = section_objs(info, "___srcu_struct_ptrs",
3100+
sizeof(*mod->srcu_struct_ptrs),
3101+
&mod->num_srcu_structs);
3102+
#endif
30983103
#ifdef CONFIG_BPF_EVENTS
30993104
mod->bpf_raw_events = section_objs(info, "__bpf_raw_tp_map",
31003105
sizeof(*mod->bpf_raw_events),

‎kernel/rcu/srcutree.c‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,3 +1310,68 @@ void __init srcu_init(void)
13101310
queue_work(rcu_gp_wq, &ssp->work.work);
13111311
}
13121312
}
1313+
1314+
#ifdef CONFIG_MODULES
1315+
1316+
/* Initialize any global-scope srcu_struct structures used by this module. */
1317+
static int srcu_module_coming(struct module *mod)
1318+
{
1319+
int i;
1320+
struct srcu_struct **sspp = mod->srcu_struct_ptrs;
1321+
int ret;
1322+
1323+
for (i = 0; i < mod->num_srcu_structs; i++) {
1324+
ret = init_srcu_struct(*(sspp++));
1325+
if (WARN_ON_ONCE(ret))
1326+
return ret;
1327+
}
1328+
return 0;
1329+
}
1330+
1331+
/* Clean up any global-scope srcu_struct structures used by this module. */
1332+
static void srcu_module_going(struct module *mod)
1333+
{
1334+
int i;
1335+
struct srcu_struct **sspp = mod->srcu_struct_ptrs;
1336+
1337+
for (i = 0; i < mod->num_srcu_structs; i++)
1338+
cleanup_srcu_struct(*(sspp++));
1339+
}
1340+
1341+
/* Handle one module, either coming or going. */
1342+
static int srcu_module_notify(struct notifier_block *self,
1343+
unsigned long val, void *data)
1344+
{
1345+
struct module *mod = data;
1346+
int ret = 0;
1347+
1348+
switch (val) {
1349+
case MODULE_STATE_COMING:
1350+
ret = srcu_module_coming(mod);
1351+
break;
1352+
case MODULE_STATE_GOING:
1353+
srcu_module_going(mod);
1354+
break;
1355+
default:
1356+
break;
1357+
}
1358+
return ret;
1359+
}
1360+
1361+
static struct notifier_block srcu_module_nb = {
1362+
.notifier_call = srcu_module_notify,
1363+
.priority = 0,
1364+
};
1365+
1366+
static __init int init_srcu_module_notifier(void)
1367+
{
1368+
int ret;
1369+
1370+
ret = register_module_notifier(&srcu_module_nb);
1371+
if (ret)
1372+
pr_warn("Failed to register srcu module notifier\n");
1373+
return ret;
1374+
}
1375+
late_initcall(init_srcu_module_notifier);
1376+
1377+
#endif /* #ifdef CONFIG_MODULES */

0 commit comments

Comments
 (0)