2 * Copyright (C) 2011 David Goulet <dgoulet@efficios.com>
3 * Copyright (C) 2014 Jan Glauber <jan.glauber@gmail.com>
5 * SPDX-License-Identifier: GPL-2.0-only
12 * @brief modprobe related functions.
22 #include <common/common.h>
23 #include <common/utils.h>
26 #include "kern-modules.h"
27 #include "lttng-sessiond.h"
29 /* LTTng kernel tracer mandatory core modules list */
30 struct kern_modules_param kern_modules_control_core
[] = {
32 .name
= (char *) "lttng-ring-buffer-client-discard",
33 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
,
36 .name
= (char *) "lttng-ring-buffer-client-overwrite",
37 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
,
40 .name
= (char *) "lttng-ring-buffer-metadata-client",
41 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
,
44 .name
= (char *) "lttng-ring-buffer-client-mmap-discard",
45 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
,
48 .name
= (char *) "lttng-ring-buffer-client-mmap-overwrite",
49 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
,
52 .name
= (char *) "lttng-ring-buffer-metadata-mmap-client",
53 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
,
56 .name
= (char *) "lttng-ring-buffer-event_notifier-client",
57 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
60 .name
= (char *) "lttng-counter-client-percpu-64-modular",
61 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
64 .name
= (char *) "lttng-counter-client-percpu-32-modular",
65 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
69 /* LTTng kerneltracer probe modules list */
70 struct kern_modules_param kern_modules_probes_default
[] = {
72 .name
= (char *) "lttng-probe-asoc",
73 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
76 .name
= (char *) "lttng-probe-block",
77 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
80 .name
= (char *) "lttng-probe-btrfs",
81 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
84 .name
= (char *) "lttng-probe-compaction",
85 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
88 .name
= (char *) "lttng-probe-ext3",
89 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
92 .name
= (char *) "lttng-probe-ext4",
93 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
96 .name
= (char *) "lttng-probe-gpio",
97 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
100 .name
= (char *) "lttng-probe-i2c",
101 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
104 .name
= (char *) "lttng-probe-irq",
105 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
108 .name
= (char *) "lttng-probe-jbd",
109 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
112 .name
= (char *) "lttng-probe-jbd2",
113 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
116 .name
= (char *) "lttng-probe-kmem",
117 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
120 .name
= (char *) "lttng-probe-kvm",
121 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
124 .name
= (char *) "lttng-probe-kvm-x86",
125 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
128 .name
= (char *) "lttng-probe-kvm-x86-mmu",
129 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
132 .name
= (char *) "lttng-probe-lock",
133 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
136 .name
= (char *) "lttng-probe-module",
137 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
140 .name
= (char *) "lttng-probe-napi",
141 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
144 .name
= (char *) "lttng-probe-net",
145 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
148 .name
= (char *) "lttng-probe-power",
149 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
152 .name
= (char *) "lttng-probe-preemptirq",
153 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
156 .name
= (char *) "lttng-probe-printk",
157 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
160 .name
= (char *) "lttng-probe-random",
161 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
164 .name
= (char *) "lttng-probe-rcu",
165 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
168 .name
= (char *) "lttng-probe-regmap",
169 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
172 .name
= (char *) "lttng-probe-regulator",
173 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
176 .name
= (char *) "lttng-probe-rpm",
177 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
180 .name
= (char *) "lttng-probe-sched",
181 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
184 .name
= (char *) "lttng-probe-scsi",
185 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
188 .name
= (char *) "lttng-probe-signal",
189 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
192 .name
= (char *) "lttng-probe-skb",
193 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
196 .name
= (char *) "lttng-probe-sock",
197 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
200 .name
= (char *) "lttng-probe-statedump",
201 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
204 .name
= (char *) "lttng-probe-sunrpc",
205 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
208 .name
= (char *) "lttng-probe-timer",
209 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
212 .name
= (char *) "lttng-probe-udp",
213 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
216 .name
= (char *) "lttng-probe-vmscan",
217 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
220 .name
= (char *) "lttng-probe-v4l2",
221 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
224 .name
= (char *) "lttng-probe-workqueue",
225 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
228 .name
= (char *) "lttng-probe-writeback",
229 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
232 .name
= (char *) "lttng-probe-x86-irq-vectors",
233 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
236 .name
= (char *) "lttng-probe-x86-exceptions",
237 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
241 /* dynamic probe modules list */
242 static struct kern_modules_param
*probes
;
243 static int nr_probes
;
244 static int probes_capacity
;
250 * @brief Logging function for libkmod integration.
252 static void log_kmod(void *data
, int priority
, const char *file
, int line
,
253 const char *fn
, const char *format
, va_list args
)
257 if (vasprintf(&str
, format
, args
) < 0) {
261 DBG("libkmod: %s", str
);
266 * @brief Setup the libkmod context.
268 * Create the context, add a custom logging function and preload the
269 * ressources for faster operation.
271 * @returns \c 0 on success
274 static int setup_kmod_ctx(struct kmod_ctx
**ctx
)
278 *ctx
= kmod_new(NULL
, NULL
);
280 PERROR("Unable to create kmod library context");
285 kmod_set_log_fn(*ctx
, log_kmod
, NULL
);
286 ret
= kmod_load_resources(*ctx
);
288 ERR("Failed to load kmod library resources");
297 * @brief Loads the kernel modules in \p modules
299 * @param modules List of modules to load
300 * @param entries Number of modules in the list
302 * If the modules are required, we will return with error after the
303 * first failed module load, otherwise we continue loading.
305 * @returns \c 0 on success
308 static int modprobe_lttng(struct kern_modules_param
*modules
,
312 struct kmod_ctx
*ctx
;
314 ret
= setup_kmod_ctx(&ctx
);
319 for (i
= 0; i
< entries
; i
++) {
320 struct kmod_module
*mod
= NULL
;
322 ret
= kmod_module_new_from_name(ctx
, modules
[i
].name
, &mod
);
324 PERROR("Failed to create kmod module for %s", modules
[i
].name
);
328 ret
= kmod_module_probe_insert_module(mod
, 0,
329 NULL
, NULL
, NULL
, NULL
);
330 if (ret
== -EEXIST
) {
331 DBG("Module %s is already loaded", modules
[i
].name
);
333 } else if (ret
< 0) {
334 if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
) {
335 ERR("Unable to load required module %s",
339 DBG("Unable to load optional module %s; continuing",
344 DBG("Modprobe successfully %s", modules
[i
].name
);
345 modules
[i
].loaded
= true;
348 kmod_module_unref(mod
);
359 * @brief Recursively unload modules.
361 * This function implements the same modules unloading behavior as
362 * 'modprobe -r' or rmmod, it will recursevily go trought the \p module
363 * dependencies and unload modules with a refcount of 0.
365 * @param mod The module to unload
367 * @returns \c 0 on success
370 static int rmmod_recurse(struct kmod_module
*mod
) {
372 struct kmod_list
*deps
, *itr
;
374 if (kmod_module_get_initstate(mod
) == KMOD_MODULE_BUILTIN
) {
375 DBG("Module %s is builtin", kmod_module_get_name(mod
));
379 ret
= kmod_module_remove_module(mod
, 0);
381 deps
= kmod_module_get_dependencies(mod
);
383 kmod_list_foreach(itr
, deps
) {
384 struct kmod_module
*dep
= kmod_module_get_module(itr
);
385 if (kmod_module_get_refcnt(dep
) == 0) {
386 DBG("Recursive remove module %s",
387 kmod_module_get_name(dep
));
390 kmod_module_unref(dep
);
392 kmod_module_unref_list(deps
);
399 * @brief Unloads the kernel modules in \p modules
401 * @param modules List of modules to unload
402 * @param entries Number of modules in the list
405 static void modprobe_remove_lttng(const struct kern_modules_param
*modules
,
409 struct kmod_ctx
*ctx
;
411 ret
= setup_kmod_ctx(&ctx
);
416 for (i
= entries
- 1; i
>= 0; i
--) {
417 struct kmod_module
*mod
= NULL
;
419 if (!modules
[i
].loaded
) {
423 ret
= kmod_module_new_from_name(ctx
, modules
[i
].name
, &mod
);
425 PERROR("Failed to create kmod module for %s", modules
[i
].name
);
429 ret
= rmmod_recurse(mod
);
430 if (ret
== -EEXIST
) {
431 DBG("Module %s is not in kernel.", modules
[i
].name
);
432 } else if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
&& ret
< 0) {
433 ERR("Unable to remove module %s", modules
[i
].name
);
435 DBG("Modprobe removal successful %s",
439 kmod_module_unref(mod
);
448 #else /* HAVE_KMOD */
450 static int modprobe_lttng(struct kern_modules_param
*modules
,
456 for (i
= 0; i
< entries
; i
++) {
457 ret
= snprintf(modprobe
, sizeof(modprobe
),
458 "/sbin/modprobe %s%s",
459 modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
? "" : "-q ",
462 PERROR("snprintf modprobe");
465 modprobe
[sizeof(modprobe
) - 1] = '\0';
466 ret
= system(modprobe
);
468 if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
) {
469 ERR("Unable to launch modprobe for required module %s",
473 DBG("Unable to launch modprobe for optional module %s; continuing",
477 } else if (WEXITSTATUS(ret
) != 0) {
478 if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
) {
479 ERR("Unable to load required module %s",
483 DBG("Unable to load optional module %s; continuing",
488 DBG("Modprobe successfully %s", modules
[i
].name
);
489 modules
[i
].loaded
= true;
497 static void modprobe_remove_lttng(const struct kern_modules_param
*modules
,
503 for (i
= entries
- 1; i
>= 0; i
--) {
504 if (!modules
[i
].loaded
) {
507 ret
= snprintf(modprobe
, sizeof(modprobe
),
508 "/sbin/modprobe -r -q %s",
511 PERROR("snprintf modprobe -r");
514 modprobe
[sizeof(modprobe
) - 1] = '\0';
515 ret
= system(modprobe
);
517 if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
) {
518 ERR("Unable to launch modprobe -r for required module %s",
521 DBG("Unable to launch modprobe -r for optional module %s",
524 } else if (WEXITSTATUS(ret
) != 0) {
525 if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
) {
526 ERR("Unable to remove required module %s",
529 DBG("Unable to remove optional module %s",
533 DBG("Modprobe removal successful %s", modules
[i
].name
);
538 #endif /* HAVE_KMOD */
541 * Remove control kernel module(s) in reverse load order.
543 void modprobe_remove_lttng_control(void)
545 modprobe_remove_lttng(kern_modules_control_core
,
546 ARRAY_SIZE(kern_modules_control_core
));
549 static void free_probes(void)
556 for (i
= 0; i
< nr_probes
; ++i
) {
557 free(probes
[i
].name
);
565 * Remove data kernel modules in reverse load order.
567 void modprobe_remove_lttng_data(void)
573 modprobe_remove_lttng(probes
, nr_probes
);
578 * Remove all kernel modules in reverse order.
580 void modprobe_remove_lttng_all(void)
582 modprobe_remove_lttng_data();
583 modprobe_remove_lttng_control();
587 * Load control kernel module(s).
589 int modprobe_lttng_control(void)
591 return modprobe_lttng(kern_modules_control_core
,
592 ARRAY_SIZE(kern_modules_control_core
));
596 * Grow global list of probes (double capacity or set it to 1 if
597 * currently 0 and copy existing data).
599 static int grow_probes(void)
602 struct kern_modules_param
*tmp_probes
;
604 /* Initialize capacity to 1 if 0. */
605 if (probes_capacity
== 0) {
606 probes
= zmalloc(sizeof(*probes
));
608 PERROR("malloc probe list");
617 probes_capacity
*= 2;
619 tmp_probes
= zmalloc(sizeof(*tmp_probes
) * probes_capacity
);
621 PERROR("malloc probe list");
625 for (i
= 0; i
< nr_probes
; ++i
) {
626 /* Ownership of 'name' field is transferred. */
627 tmp_probes
[i
] = probes
[i
];
630 /* Replace probes with larger copy. */
638 * Appends a comma-separated list of probes to the global list
641 static int append_list_to_probes(const char *list
)
645 char *tmp_list
, *cur_list
, *saveptr
;
649 cur_list
= tmp_list
= strdup(list
);
651 PERROR("strdup temp list");
657 struct kern_modules_param
*cur_mod
;
659 next
= strtok_r(cur_list
, ",", &saveptr
);
665 /* filter leading spaces */
666 while (*next
== ' ') {
670 if (probes_capacity
<= nr_probes
) {
677 /* Length 13 is "lttng-probe-" + \0 */
678 name_len
= strlen(next
) + 13;
680 cur_mod
= &probes
[nr_probes
];
681 cur_mod
->name
= zmalloc(name_len
);
682 if (!cur_mod
->name
) {
683 PERROR("malloc probe list");
688 ret
= snprintf(cur_mod
->name
, name_len
, "lttng-probe-%s", next
);
690 PERROR("snprintf modprobe name");
695 cur_mod
->load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
;
710 * Load data kernel module(s).
712 int modprobe_lttng_data(void)
718 * Base probes: either from command line option, environment
719 * variable or default list.
721 list
= the_config
.kmod_probes_list
.value
;
723 /* User-specified probes. */
724 ret
= append_list_to_probes(list
);
729 /* Default probes. */
730 int def_len
= ARRAY_SIZE(kern_modules_probes_default
);
732 probes
= zmalloc(sizeof(*probes
) * def_len
);
734 PERROR("malloc probe list");
738 nr_probes
= probes_capacity
= def_len
;
740 for (i
= 0; i
< def_len
; ++i
) {
741 char* name
= strdup(kern_modules_probes_default
[i
].name
);
744 PERROR("strdup probe item");
749 probes
[i
].name
= name
;
750 probes
[i
].load_policy
= kern_modules_probes_default
[i
].load_policy
;
755 * Extra modules? Append them to current probes list.
757 list
= the_config
.kmod_extra_probes_list
.value
;
759 ret
= append_list_to_probes(list
);
766 * Load probes modules now.
768 ret
= modprobe_lttng(probes
, nr_probes
);