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.
17 #include "kern-modules.hpp"
18 #include "lttng-sessiond.hpp"
19 #include "modprobe.hpp"
21 #include <common/common.hpp>
22 #include <common/utils.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) void log_kmod(void *data
__attribute__((unused
)),
303 int priority
__attribute__((unused
)),
304 const char *file
__attribute__((unused
)),
305 int line
__attribute__((unused
)),
306 const char *fn
__attribute__((unused
)),
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(nullptr, nullptr);
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
, nullptr);
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
, int entries
)
374 struct kmod_ctx
*ctx
;
376 ret
= setup_kmod_ctx(&ctx
);
381 for (i
= 0; i
< entries
; i
++) {
382 struct kmod_module
*mod
= nullptr;
384 ret
= kmod_module_new_from_name(ctx
, modules
[i
].name
, &mod
);
386 PERROR("Failed to create kmod module for %s", modules
[i
].name
);
390 ret
= kmod_module_probe_insert_module(mod
, 0, nullptr, nullptr, nullptr, nullptr);
391 if (ret
== -EEXIST
) {
392 DBG("Module %s is already loaded", modules
[i
].name
);
394 } else if (ret
< 0) {
395 if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
) {
396 ERR("Unable to load required module %s", modules
[i
].name
);
399 DBG("Unable to load optional module %s; continuing",
404 DBG("Modprobe successfully %s", modules
[i
].name
);
405 modules
[i
].loaded
= true;
408 kmod_module_unref(mod
);
419 * @brief Recursively unload modules.
421 * This function implements the same modules unloading behavior as
422 * 'modprobe -r' or rmmod, it will recursevily go trought the \p module
423 * dependencies and unload modules with a refcount of 0.
425 * @param mod The module to unload
427 * @returns \c 0 on success
430 static int rmmod_recurse(struct kmod_module
*mod
)
433 struct kmod_list
*deps
, *itr
;
435 if (kmod_module_get_initstate(mod
) == KMOD_MODULE_BUILTIN
) {
436 DBG("Module %s is builtin", kmod_module_get_name(mod
));
440 ret
= kmod_module_remove_module(mod
, 0);
442 deps
= kmod_module_get_dependencies(mod
);
443 if (deps
!= nullptr) {
444 kmod_list_foreach(itr
, deps
)
446 struct kmod_module
*dep
= kmod_module_get_module(itr
);
447 if (kmod_module_get_refcnt(dep
) == 0) {
448 DBG("Recursive remove module %s", kmod_module_get_name(dep
));
451 kmod_module_unref(dep
);
453 kmod_module_unref_list(deps
);
460 * @brief Unloads the kernel modules in \p modules
462 * @param modules List of modules to unload
463 * @param entries Number of modules in the list
466 static void modprobe_remove_lttng(const struct kern_modules_param
*modules
, int entries
)
469 struct kmod_ctx
*ctx
;
471 ret
= setup_kmod_ctx(&ctx
);
476 for (i
= entries
- 1; i
>= 0; i
--) {
477 struct kmod_module
*mod
= nullptr;
479 if (!modules
[i
].loaded
) {
483 ret
= kmod_module_new_from_name(ctx
, modules
[i
].name
, &mod
);
485 PERROR("Failed to create kmod module for %s", modules
[i
].name
);
489 ret
= rmmod_recurse(mod
);
490 if (ret
== -EEXIST
) {
491 DBG("Module %s is not in kernel.", modules
[i
].name
);
492 } else if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
&&
494 ERR("Unable to remove module %s", modules
[i
].name
);
496 DBG("Modprobe removal successful %s", modules
[i
].name
);
499 kmod_module_unref(mod
);
508 #else /* HAVE_KMOD */
510 static int modprobe_lttng(struct kern_modules_param
*modules
, int entries
)
515 for (i
= 0; i
< entries
; i
++) {
516 ret
= snprintf(modprobe
,
518 "/sbin/modprobe %s%s",
519 modules
[i
].load_policy
==
520 KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
?
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", modules
[i
].name
);
545 DBG("Unable to load optional module %s; continuing",
550 DBG("Modprobe successfully %s", modules
[i
].name
);
551 modules
[i
].loaded
= true;
559 static void modprobe_remove_lttng(const struct kern_modules_param
*modules
, int entries
)
564 for (i
= entries
- 1; i
>= 0; i
--) {
565 if (!modules
[i
].loaded
) {
569 modprobe
, sizeof(modprobe
), "/sbin/modprobe -r -q %s", modules
[i
].name
);
571 PERROR("snprintf modprobe -r");
574 modprobe
[sizeof(modprobe
) - 1] = '\0';
575 ret
= system(modprobe
);
577 if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
) {
578 ERR("Unable to launch modprobe -r for required module %s",
581 DBG("Unable to launch modprobe -r for optional module %s",
584 } else if (WEXITSTATUS(ret
) != 0) {
585 if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
) {
586 ERR("Unable to remove required module %s", modules
[i
].name
);
588 DBG("Unable to remove optional module %s", modules
[i
].name
);
591 DBG("Modprobe removal successful %s", modules
[i
].name
);
596 #endif /* HAVE_KMOD */
599 * Remove control kernel module(s) in reverse load order.
601 void modprobe_remove_lttng_control()
603 modprobe_remove_lttng(kern_modules_control_core
, ARRAY_SIZE(kern_modules_control_core
));
606 static void free_probes()
613 for (i
= 0; i
< nr_probes
; ++i
) {
614 free(probes
[i
].name
);
622 * Remove data kernel modules in reverse load order.
624 void modprobe_remove_lttng_data()
630 modprobe_remove_lttng(probes
, nr_probes
);
635 * Remove all kernel modules in reverse order.
637 void modprobe_remove_lttng_all()
639 modprobe_remove_lttng_data();
640 modprobe_remove_lttng_control();
644 * Load control kernel module(s).
646 int modprobe_lttng_control()
648 return modprobe_lttng(kern_modules_control_core
, ARRAY_SIZE(kern_modules_control_core
));
652 * Grow global list of probes (double capacity or set it to 1 if
653 * currently 0 and copy existing data).
655 static int grow_probes()
658 struct kern_modules_param
*tmp_probes
;
660 /* Initialize capacity to 1 if 0. */
661 if (probes_capacity
== 0) {
662 probes
= zmalloc
<kern_modules_param
>();
664 PERROR("malloc probe list");
673 probes_capacity
*= 2;
675 tmp_probes
= calloc
<kern_modules_param
>(probes_capacity
);
677 PERROR("malloc probe list");
681 for (i
= 0; i
< nr_probes
; ++i
) {
682 /* Ownership of 'name' field is transferred. */
683 tmp_probes
[i
] = probes
[i
];
686 /* Replace probes with larger copy. */
694 * Appends a comma-separated list of probes to the global list
697 static int append_list_to_probes(const char *list
)
701 char *tmp_list
, *cur_list
, *saveptr
;
705 cur_list
= tmp_list
= strdup(list
);
707 PERROR("strdup temp list");
713 struct kern_modules_param
*cur_mod
;
715 next
= strtok_r(cur_list
, ",", &saveptr
);
721 /* filter leading spaces */
722 while (*next
== ' ') {
726 if (probes_capacity
<= nr_probes
) {
733 /* Length 13 is "lttng-probe-" + \0 */
734 name_len
= strlen(next
) + 13;
736 cur_mod
= &probes
[nr_probes
];
737 cur_mod
->name
= calloc
<char>(name_len
);
738 if (!cur_mod
->name
) {
739 PERROR("malloc probe list");
744 ret
= snprintf(cur_mod
->name
, name_len
, "lttng-probe-%s", next
);
746 PERROR("snprintf modprobe name");
751 cur_mod
->load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
;
766 * Load data kernel module(s).
768 int modprobe_lttng_data()
774 * Base probes: either from command line option, environment
775 * variable or default list.
777 list
= the_config
.kmod_probes_list
.value
;
779 /* User-specified probes. */
780 ret
= append_list_to_probes(list
);
785 /* Default probes. */
786 int def_len
= ARRAY_SIZE(kern_modules_probes_default
);
788 probes
= calloc
<kern_modules_param
>(def_len
);
790 PERROR("malloc probe list");
794 nr_probes
= probes_capacity
= def_len
;
796 for (i
= 0; i
< def_len
; ++i
) {
797 char *name
= strdup(kern_modules_probes_default
[i
].name
);
800 PERROR("strdup probe item");
805 probes
[i
].name
= name
;
806 probes
[i
].load_policy
= kern_modules_probes_default
[i
].load_policy
;
811 * Extra modules? Append them to current probes list.
813 list
= the_config
.kmod_extra_probes_list
.value
;
815 ret
= append_list_to_probes(list
);
822 * Load probes modules now.
824 ret
= modprobe_lttng(probes
, nr_probes
);