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.
21 #include <common/common.hpp>
22 #include <common/utils.hpp>
24 #include "modprobe.hpp"
25 #include "kern-modules.hpp"
26 #include "lttng-sessiond.hpp"
28 /* LTTng kernel tracer mandatory core modules list */
29 struct kern_modules_param kern_modules_control_core
[] = {
31 .name
= (char *) "lttng-ring-buffer-client-discard",
32 .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
,
41 .name
= (char *) "lttng-ring-buffer-metadata-client",
42 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
,
46 .name
= (char *) "lttng-ring-buffer-client-mmap-discard",
47 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
,
51 .name
= (char *) "lttng-ring-buffer-client-mmap-overwrite",
52 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
,
56 .name
= (char *) "lttng-ring-buffer-metadata-mmap-client",
57 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
,
61 .name
= (char *) "lttng-ring-buffer-event_notifier-client",
62 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
66 .name
= (char *) "lttng-counter-client-percpu-64-modular",
67 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
71 .name
= (char *) "lttng-counter-client-percpu-32-modular",
72 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
77 /* LTTng kerneltracer probe modules list */
78 struct kern_modules_param kern_modules_probes_default
[] = {
80 .name
= (char *) "lttng-probe-asoc",
81 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
85 .name
= (char *) "lttng-probe-block",
86 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
90 .name
= (char *) "lttng-probe-btrfs",
91 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
95 .name
= (char *) "lttng-probe-compaction",
96 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
100 .name
= (char *) "lttng-probe-ext3",
101 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
105 .name
= (char *) "lttng-probe-ext4",
106 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
110 .name
= (char *) "lttng-probe-gpio",
111 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
115 .name
= (char *) "lttng-probe-i2c",
116 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
120 .name
= (char *) "lttng-probe-irq",
121 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
125 .name
= (char *) "lttng-probe-jbd",
126 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
130 .name
= (char *) "lttng-probe-jbd2",
131 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
135 .name
= (char *) "lttng-probe-kmem",
136 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
140 .name
= (char *) "lttng-probe-kvm",
141 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
145 .name
= (char *) "lttng-probe-kvm-x86",
146 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
150 .name
= (char *) "lttng-probe-kvm-x86-mmu",
151 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
155 .name
= (char *) "lttng-probe-lock",
156 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
160 .name
= (char *) "lttng-probe-module",
161 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
165 .name
= (char *) "lttng-probe-napi",
166 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
170 .name
= (char *) "lttng-probe-net",
171 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
175 .name
= (char *) "lttng-probe-power",
176 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
180 .name
= (char *) "lttng-probe-preemptirq",
181 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
185 .name
= (char *) "lttng-probe-printk",
186 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
190 .name
= (char *) "lttng-probe-random",
191 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
195 .name
= (char *) "lttng-probe-rcu",
196 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
200 .name
= (char *) "lttng-probe-regmap",
201 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
205 .name
= (char *) "lttng-probe-regulator",
206 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
210 .name
= (char *) "lttng-probe-rpm",
211 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
215 .name
= (char *) "lttng-probe-sched",
216 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
220 .name
= (char *) "lttng-probe-scsi",
221 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
225 .name
= (char *) "lttng-probe-signal",
226 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
230 .name
= (char *) "lttng-probe-skb",
231 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
235 .name
= (char *) "lttng-probe-sock",
236 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
240 .name
= (char *) "lttng-probe-statedump",
241 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
245 .name
= (char *) "lttng-probe-sunrpc",
246 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
250 .name
= (char *) "lttng-probe-timer",
251 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
255 .name
= (char *) "lttng-probe-udp",
256 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
260 .name
= (char *) "lttng-probe-vmscan",
261 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
265 .name
= (char *) "lttng-probe-v4l2",
266 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
270 .name
= (char *) "lttng-probe-workqueue",
271 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
275 .name
= (char *) "lttng-probe-writeback",
276 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
280 .name
= (char *) "lttng-probe-x86-irq-vectors",
281 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
285 .name
= (char *) "lttng-probe-x86-exceptions",
286 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
291 /* dynamic probe modules list */
292 static struct kern_modules_param
*probes
;
293 static int nr_probes
;
294 static int probes_capacity
;
300 * @brief Logging function for libkmod integration.
302 static ATTR_FORMAT_PRINTF(6, 0)
303 void log_kmod(void *data
__attribute__((unused
)),
304 int priority
__attribute__((unused
)),
305 const char *file
__attribute__((unused
)),
306 int line
__attribute__((unused
)),
307 const char *fn
__attribute__((unused
)),
308 const char *format
, va_list args
)
312 if (vasprintf(&str
, format
, args
) < 0) {
316 DBG("libkmod: %s", str
);
321 * @brief Setup the libkmod context.
323 * Create the context, add a custom logging function and preload the
324 * ressources for faster operation.
326 * @returns \c 0 on success
329 static int setup_kmod_ctx(struct kmod_ctx
**ctx
)
333 *ctx
= kmod_new(NULL
, NULL
);
335 PERROR("Unable to create kmod library context");
341 * Parameter 2 of kmod_set_log_fn generates a
342 * -Wsuggest-attribute=formatkmod_set_log_fn warning that we can't fix,
346 DIAGNOSTIC_IGNORE_SUGGEST_ATTRIBUTE_FORMAT
347 kmod_set_log_fn(*ctx
, log_kmod
, NULL
);
349 ret
= kmod_load_resources(*ctx
);
351 ERR("Failed to load kmod library resources");
360 * @brief Loads the kernel modules in \p modules
362 * @param modules List of modules to load
363 * @param entries Number of modules in the list
365 * If the modules are required, we will return with error after the
366 * first failed module load, otherwise we continue loading.
368 * @returns \c 0 on success
371 static int modprobe_lttng(struct kern_modules_param
*modules
,
375 struct kmod_ctx
*ctx
;
377 ret
= setup_kmod_ctx(&ctx
);
382 for (i
= 0; i
< entries
; i
++) {
383 struct kmod_module
*mod
= NULL
;
385 ret
= kmod_module_new_from_name(ctx
, modules
[i
].name
, &mod
);
387 PERROR("Failed to create kmod module for %s", modules
[i
].name
);
391 ret
= kmod_module_probe_insert_module(mod
, 0,
392 NULL
, NULL
, NULL
, NULL
);
393 if (ret
== -EEXIST
) {
394 DBG("Module %s is already loaded", modules
[i
].name
);
396 } else if (ret
< 0) {
397 if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
) {
398 ERR("Unable to load required module %s",
402 DBG("Unable to load optional module %s; continuing",
407 DBG("Modprobe successfully %s", modules
[i
].name
);
408 modules
[i
].loaded
= true;
411 kmod_module_unref(mod
);
422 * @brief Recursively unload modules.
424 * This function implements the same modules unloading behavior as
425 * 'modprobe -r' or rmmod, it will recursevily go trought the \p module
426 * dependencies and unload modules with a refcount of 0.
428 * @param mod The module to unload
430 * @returns \c 0 on success
433 static int rmmod_recurse(struct kmod_module
*mod
) {
435 struct kmod_list
*deps
, *itr
;
437 if (kmod_module_get_initstate(mod
) == KMOD_MODULE_BUILTIN
) {
438 DBG("Module %s is builtin", kmod_module_get_name(mod
));
442 ret
= kmod_module_remove_module(mod
, 0);
444 deps
= kmod_module_get_dependencies(mod
);
446 kmod_list_foreach(itr
, deps
) {
447 struct kmod_module
*dep
= kmod_module_get_module(itr
);
448 if (kmod_module_get_refcnt(dep
) == 0) {
449 DBG("Recursive remove module %s",
450 kmod_module_get_name(dep
));
453 kmod_module_unref(dep
);
455 kmod_module_unref_list(deps
);
462 * @brief Unloads the kernel modules in \p modules
464 * @param modules List of modules to unload
465 * @param entries Number of modules in the list
468 static void modprobe_remove_lttng(const struct kern_modules_param
*modules
,
472 struct kmod_ctx
*ctx
;
474 ret
= setup_kmod_ctx(&ctx
);
479 for (i
= entries
- 1; i
>= 0; i
--) {
480 struct kmod_module
*mod
= NULL
;
482 if (!modules
[i
].loaded
) {
486 ret
= kmod_module_new_from_name(ctx
, modules
[i
].name
, &mod
);
488 PERROR("Failed to create kmod module for %s", modules
[i
].name
);
492 ret
= rmmod_recurse(mod
);
493 if (ret
== -EEXIST
) {
494 DBG("Module %s is not in kernel.", modules
[i
].name
);
495 } else if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
&& ret
< 0) {
496 ERR("Unable to remove module %s", modules
[i
].name
);
498 DBG("Modprobe removal successful %s",
502 kmod_module_unref(mod
);
511 #else /* HAVE_KMOD */
513 static int modprobe_lttng(struct kern_modules_param
*modules
,
519 for (i
= 0; i
< entries
; i
++) {
520 ret
= snprintf(modprobe
, sizeof(modprobe
),
521 "/sbin/modprobe %s%s",
522 modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
? "" : "-q ",
525 PERROR("snprintf modprobe");
528 modprobe
[sizeof(modprobe
) - 1] = '\0';
529 ret
= system(modprobe
);
531 if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
) {
532 ERR("Unable to launch modprobe for required module %s",
536 DBG("Unable to launch modprobe for optional module %s; continuing",
540 } else if (WEXITSTATUS(ret
) != 0) {
541 if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
) {
542 ERR("Unable to load required module %s",
546 DBG("Unable to load optional module %s; continuing",
551 DBG("Modprobe successfully %s", modules
[i
].name
);
552 modules
[i
].loaded
= true;
560 static void modprobe_remove_lttng(const struct kern_modules_param
*modules
,
566 for (i
= entries
- 1; i
>= 0; i
--) {
567 if (!modules
[i
].loaded
) {
570 ret
= snprintf(modprobe
, sizeof(modprobe
),
571 "/sbin/modprobe -r -q %s",
574 PERROR("snprintf modprobe -r");
577 modprobe
[sizeof(modprobe
) - 1] = '\0';
578 ret
= system(modprobe
);
580 if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
) {
581 ERR("Unable to launch modprobe -r for required module %s",
584 DBG("Unable to launch modprobe -r for optional module %s",
587 } else if (WEXITSTATUS(ret
) != 0) {
588 if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
) {
589 ERR("Unable to remove required module %s",
592 DBG("Unable to remove optional module %s",
596 DBG("Modprobe removal successful %s", modules
[i
].name
);
601 #endif /* HAVE_KMOD */
604 * Remove control kernel module(s) in reverse load order.
606 void modprobe_remove_lttng_control(void)
608 modprobe_remove_lttng(kern_modules_control_core
,
609 ARRAY_SIZE(kern_modules_control_core
));
612 static void free_probes(void)
619 for (i
= 0; i
< nr_probes
; ++i
) {
620 free(probes
[i
].name
);
628 * Remove data kernel modules in reverse load order.
630 void modprobe_remove_lttng_data(void)
636 modprobe_remove_lttng(probes
, nr_probes
);
641 * Remove all kernel modules in reverse order.
643 void modprobe_remove_lttng_all(void)
645 modprobe_remove_lttng_data();
646 modprobe_remove_lttng_control();
650 * Load control kernel module(s).
652 int modprobe_lttng_control(void)
654 return modprobe_lttng(kern_modules_control_core
,
655 ARRAY_SIZE(kern_modules_control_core
));
659 * Grow global list of probes (double capacity or set it to 1 if
660 * currently 0 and copy existing data).
662 static int grow_probes(void)
665 struct kern_modules_param
*tmp_probes
;
667 /* Initialize capacity to 1 if 0. */
668 if (probes_capacity
== 0) {
669 probes
= zmalloc
<kern_modules_param
>();
671 PERROR("malloc probe list");
680 probes_capacity
*= 2;
682 tmp_probes
= calloc
<kern_modules_param
>(probes_capacity
);
684 PERROR("malloc probe list");
688 for (i
= 0; i
< nr_probes
; ++i
) {
689 /* Ownership of 'name' field is transferred. */
690 tmp_probes
[i
] = probes
[i
];
693 /* Replace probes with larger copy. */
701 * Appends a comma-separated list of probes to the global list
704 static int append_list_to_probes(const char *list
)
708 char *tmp_list
, *cur_list
, *saveptr
;
712 cur_list
= tmp_list
= strdup(list
);
714 PERROR("strdup temp list");
720 struct kern_modules_param
*cur_mod
;
722 next
= strtok_r(cur_list
, ",", &saveptr
);
728 /* filter leading spaces */
729 while (*next
== ' ') {
733 if (probes_capacity
<= nr_probes
) {
740 /* Length 13 is "lttng-probe-" + \0 */
741 name_len
= strlen(next
) + 13;
743 cur_mod
= &probes
[nr_probes
];
744 cur_mod
->name
= calloc
<char>(name_len
);
745 if (!cur_mod
->name
) {
746 PERROR("malloc probe list");
751 ret
= snprintf(cur_mod
->name
, name_len
, "lttng-probe-%s", next
);
753 PERROR("snprintf modprobe name");
758 cur_mod
->load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
;
773 * Load data kernel module(s).
775 int modprobe_lttng_data(void)
781 * Base probes: either from command line option, environment
782 * variable or default list.
784 list
= the_config
.kmod_probes_list
.value
;
786 /* User-specified probes. */
787 ret
= append_list_to_probes(list
);
792 /* Default probes. */
793 int def_len
= ARRAY_SIZE(kern_modules_probes_default
);
795 probes
= calloc
<kern_modules_param
>(def_len
);
797 PERROR("malloc probe list");
801 nr_probes
= probes_capacity
= def_len
;
803 for (i
= 0; i
< def_len
; ++i
) {
804 char* name
= strdup(kern_modules_probes_default
[i
].name
);
807 PERROR("strdup probe item");
812 probes
[i
].name
= name
;
813 probes
[i
].load_policy
= kern_modules_probes_default
[i
].load_policy
;
818 * Extra modules? Append them to current probes list.
820 list
= the_config
.kmod_extra_probes_list
.value
;
822 ret
= append_list_to_probes(list
);
829 * Load probes modules now.
831 ret
= modprobe_lttng(probes
, nr_probes
);