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.h>
22 #include <common/utils.h>
25 #include "kern-modules.h"
26 #include "lttng-sessiond.h"
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
,
35 .name
= (char *) "lttng-ring-buffer-client-overwrite",
36 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
,
39 .name
= (char *) "lttng-ring-buffer-metadata-client",
40 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
,
43 .name
= (char *) "lttng-ring-buffer-client-mmap-discard",
44 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
,
47 .name
= (char *) "lttng-ring-buffer-client-mmap-overwrite",
48 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
,
51 .name
= (char *) "lttng-ring-buffer-metadata-mmap-client",
52 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
,
55 .name
= (char *) "lttng-ring-buffer-event_notifier-client",
56 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
59 .name
= (char *) "lttng-counter-client-percpu-64-modular",
60 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
63 .name
= (char *) "lttng-counter-client-percpu-32-modular",
64 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
68 /* LTTng kerneltracer probe modules list */
69 struct kern_modules_param kern_modules_probes_default
[] = {
71 .name
= (char *) "lttng-probe-asoc",
72 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
75 .name
= (char *) "lttng-probe-block",
76 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
79 .name
= (char *) "lttng-probe-btrfs",
80 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
83 .name
= (char *) "lttng-probe-compaction",
84 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
87 .name
= (char *) "lttng-probe-ext3",
88 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
91 .name
= (char *) "lttng-probe-ext4",
92 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
95 .name
= (char *) "lttng-probe-gpio",
96 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
99 .name
= (char *) "lttng-probe-i2c",
100 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
103 .name
= (char *) "lttng-probe-irq",
104 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
107 .name
= (char *) "lttng-probe-jbd",
108 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
111 .name
= (char *) "lttng-probe-jbd2",
112 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
115 .name
= (char *) "lttng-probe-kmem",
116 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
119 .name
= (char *) "lttng-probe-kvm",
120 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
123 .name
= (char *) "lttng-probe-kvm-x86",
124 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
127 .name
= (char *) "lttng-probe-kvm-x86-mmu",
128 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
131 .name
= (char *) "lttng-probe-lock",
132 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
135 .name
= (char *) "lttng-probe-module",
136 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
139 .name
= (char *) "lttng-probe-napi",
140 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
143 .name
= (char *) "lttng-probe-net",
144 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
147 .name
= (char *) "lttng-probe-power",
148 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
151 .name
= (char *) "lttng-probe-preemptirq",
152 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
155 .name
= (char *) "lttng-probe-printk",
156 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
159 .name
= (char *) "lttng-probe-random",
160 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
163 .name
= (char *) "lttng-probe-rcu",
164 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
167 .name
= (char *) "lttng-probe-regmap",
168 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
171 .name
= (char *) "lttng-probe-regulator",
172 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
175 .name
= (char *) "lttng-probe-rpm",
176 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
179 .name
= (char *) "lttng-probe-sched",
180 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
183 .name
= (char *) "lttng-probe-scsi",
184 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
187 .name
= (char *) "lttng-probe-signal",
188 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
191 .name
= (char *) "lttng-probe-skb",
192 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
195 .name
= (char *) "lttng-probe-sock",
196 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
199 .name
= (char *) "lttng-probe-statedump",
200 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
203 .name
= (char *) "lttng-probe-sunrpc",
204 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
207 .name
= (char *) "lttng-probe-timer",
208 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
211 .name
= (char *) "lttng-probe-udp",
212 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
215 .name
= (char *) "lttng-probe-vmscan",
216 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
219 .name
= (char *) "lttng-probe-v4l2",
220 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
223 .name
= (char *) "lttng-probe-workqueue",
224 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
227 .name
= (char *) "lttng-probe-writeback",
228 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
231 .name
= (char *) "lttng-probe-x86-irq-vectors",
232 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
235 .name
= (char *) "lttng-probe-x86-exceptions",
236 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
240 /* dynamic probe modules list */
241 static struct kern_modules_param
*probes
;
242 static int nr_probes
;
243 static int probes_capacity
;
249 * @brief Logging function for libkmod integration.
251 static ATTR_FORMAT_PRINTF(6, 0)
252 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");
286 * Parameter 2 of kmod_set_log_fn generates a
287 * -Wsuggest-attribute=formatkmod_set_log_fn warning that we can't fix,
291 DIAGNOSTIC_IGNORE_SUGGEST_ATTRIBUTE_FORMAT
292 kmod_set_log_fn(*ctx
, log_kmod
, NULL
);
294 ret
= kmod_load_resources(*ctx
);
296 ERR("Failed to load kmod library resources");
305 * @brief Loads the kernel modules in \p modules
307 * @param modules List of modules to load
308 * @param entries Number of modules in the list
310 * If the modules are required, we will return with error after the
311 * first failed module load, otherwise we continue loading.
313 * @returns \c 0 on success
316 static int modprobe_lttng(struct kern_modules_param
*modules
,
320 struct kmod_ctx
*ctx
;
322 ret
= setup_kmod_ctx(&ctx
);
327 for (i
= 0; i
< entries
; i
++) {
328 struct kmod_module
*mod
= NULL
;
330 ret
= kmod_module_new_from_name(ctx
, modules
[i
].name
, &mod
);
332 PERROR("Failed to create kmod module for %s", modules
[i
].name
);
336 ret
= kmod_module_probe_insert_module(mod
, 0,
337 NULL
, NULL
, NULL
, NULL
);
338 if (ret
== -EEXIST
) {
339 DBG("Module %s is already loaded", modules
[i
].name
);
341 } else if (ret
< 0) {
342 if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
) {
343 ERR("Unable to load required module %s",
347 DBG("Unable to load optional module %s; continuing",
352 DBG("Modprobe successfully %s", modules
[i
].name
);
353 modules
[i
].loaded
= true;
356 kmod_module_unref(mod
);
367 * @brief Recursively unload modules.
369 * This function implements the same modules unloading behavior as
370 * 'modprobe -r' or rmmod, it will recursevily go trought the \p module
371 * dependencies and unload modules with a refcount of 0.
373 * @param mod The module to unload
375 * @returns \c 0 on success
378 static int rmmod_recurse(struct kmod_module
*mod
) {
380 struct kmod_list
*deps
, *itr
;
382 if (kmod_module_get_initstate(mod
) == KMOD_MODULE_BUILTIN
) {
383 DBG("Module %s is builtin", kmod_module_get_name(mod
));
387 ret
= kmod_module_remove_module(mod
, 0);
389 deps
= kmod_module_get_dependencies(mod
);
391 kmod_list_foreach(itr
, deps
) {
392 struct kmod_module
*dep
= kmod_module_get_module(itr
);
393 if (kmod_module_get_refcnt(dep
) == 0) {
394 DBG("Recursive remove module %s",
395 kmod_module_get_name(dep
));
398 kmod_module_unref(dep
);
400 kmod_module_unref_list(deps
);
407 * @brief Unloads the kernel modules in \p modules
409 * @param modules List of modules to unload
410 * @param entries Number of modules in the list
413 static void modprobe_remove_lttng(const struct kern_modules_param
*modules
,
417 struct kmod_ctx
*ctx
;
419 ret
= setup_kmod_ctx(&ctx
);
424 for (i
= entries
- 1; i
>= 0; i
--) {
425 struct kmod_module
*mod
= NULL
;
427 if (!modules
[i
].loaded
) {
431 ret
= kmod_module_new_from_name(ctx
, modules
[i
].name
, &mod
);
433 PERROR("Failed to create kmod module for %s", modules
[i
].name
);
437 ret
= rmmod_recurse(mod
);
438 if (ret
== -EEXIST
) {
439 DBG("Module %s is not in kernel.", modules
[i
].name
);
440 } else if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
&& ret
< 0) {
441 ERR("Unable to remove module %s", modules
[i
].name
);
443 DBG("Modprobe removal successful %s",
447 kmod_module_unref(mod
);
456 #else /* HAVE_KMOD */
458 static int modprobe_lttng(struct kern_modules_param
*modules
,
464 for (i
= 0; i
< entries
; i
++) {
465 ret
= snprintf(modprobe
, sizeof(modprobe
),
466 "/sbin/modprobe %s%s",
467 modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
? "" : "-q ",
470 PERROR("snprintf modprobe");
473 modprobe
[sizeof(modprobe
) - 1] = '\0';
474 ret
= system(modprobe
);
476 if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
) {
477 ERR("Unable to launch modprobe for required module %s",
481 DBG("Unable to launch modprobe for optional module %s; continuing",
485 } else if (WEXITSTATUS(ret
) != 0) {
486 if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
) {
487 ERR("Unable to load required module %s",
491 DBG("Unable to load optional module %s; continuing",
496 DBG("Modprobe successfully %s", modules
[i
].name
);
497 modules
[i
].loaded
= true;
505 static void modprobe_remove_lttng(const struct kern_modules_param
*modules
,
511 for (i
= entries
- 1; i
>= 0; i
--) {
512 if (!modules
[i
].loaded
) {
515 ret
= snprintf(modprobe
, sizeof(modprobe
),
516 "/sbin/modprobe -r -q %s",
519 PERROR("snprintf modprobe -r");
522 modprobe
[sizeof(modprobe
) - 1] = '\0';
523 ret
= system(modprobe
);
525 if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
) {
526 ERR("Unable to launch modprobe -r for required module %s",
529 DBG("Unable to launch modprobe -r for optional module %s",
532 } else if (WEXITSTATUS(ret
) != 0) {
533 if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
) {
534 ERR("Unable to remove required module %s",
537 DBG("Unable to remove optional module %s",
541 DBG("Modprobe removal successful %s", modules
[i
].name
);
546 #endif /* HAVE_KMOD */
549 * Remove control kernel module(s) in reverse load order.
551 void modprobe_remove_lttng_control(void)
553 modprobe_remove_lttng(kern_modules_control_core
,
554 ARRAY_SIZE(kern_modules_control_core
));
557 static void free_probes(void)
564 for (i
= 0; i
< nr_probes
; ++i
) {
565 free(probes
[i
].name
);
573 * Remove data kernel modules in reverse load order.
575 void modprobe_remove_lttng_data(void)
581 modprobe_remove_lttng(probes
, nr_probes
);
586 * Remove all kernel modules in reverse order.
588 void modprobe_remove_lttng_all(void)
590 modprobe_remove_lttng_data();
591 modprobe_remove_lttng_control();
595 * Load control kernel module(s).
597 int modprobe_lttng_control(void)
599 return modprobe_lttng(kern_modules_control_core
,
600 ARRAY_SIZE(kern_modules_control_core
));
604 * Grow global list of probes (double capacity or set it to 1 if
605 * currently 0 and copy existing data).
607 static int grow_probes(void)
610 struct kern_modules_param
*tmp_probes
;
612 /* Initialize capacity to 1 if 0. */
613 if (probes_capacity
== 0) {
614 probes
= (kern_modules_param
*) zmalloc(sizeof(*probes
));
616 PERROR("malloc probe list");
625 probes_capacity
*= 2;
627 tmp_probes
= (kern_modules_param
*) zmalloc(sizeof(*tmp_probes
) * probes_capacity
);
629 PERROR("malloc probe list");
633 for (i
= 0; i
< nr_probes
; ++i
) {
634 /* Ownership of 'name' field is transferred. */
635 tmp_probes
[i
] = probes
[i
];
638 /* Replace probes with larger copy. */
646 * Appends a comma-separated list of probes to the global list
649 static int append_list_to_probes(const char *list
)
653 char *tmp_list
, *cur_list
, *saveptr
;
657 cur_list
= tmp_list
= strdup(list
);
659 PERROR("strdup temp list");
665 struct kern_modules_param
*cur_mod
;
667 next
= strtok_r(cur_list
, ",", &saveptr
);
673 /* filter leading spaces */
674 while (*next
== ' ') {
678 if (probes_capacity
<= nr_probes
) {
685 /* Length 13 is "lttng-probe-" + \0 */
686 name_len
= strlen(next
) + 13;
688 cur_mod
= &probes
[nr_probes
];
689 cur_mod
->name
= (char *) zmalloc(name_len
);
690 if (!cur_mod
->name
) {
691 PERROR("malloc probe list");
696 ret
= snprintf(cur_mod
->name
, name_len
, "lttng-probe-%s", next
);
698 PERROR("snprintf modprobe name");
703 cur_mod
->load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
;
718 * Load data kernel module(s).
720 int modprobe_lttng_data(void)
726 * Base probes: either from command line option, environment
727 * variable or default list.
729 list
= the_config
.kmod_probes_list
.value
;
731 /* User-specified probes. */
732 ret
= append_list_to_probes(list
);
737 /* Default probes. */
738 int def_len
= ARRAY_SIZE(kern_modules_probes_default
);
740 probes
= (kern_modules_param
*) zmalloc(sizeof(*probes
) * def_len
);
742 PERROR("malloc probe list");
746 nr_probes
= probes_capacity
= def_len
;
748 for (i
= 0; i
< def_len
; ++i
) {
749 char* name
= strdup(kern_modules_probes_default
[i
].name
);
752 PERROR("strdup probe item");
757 probes
[i
].name
= name
;
758 probes
[i
].load_policy
= kern_modules_probes_default
[i
].load_policy
;
763 * Extra modules? Append them to current probes list.
765 list
= the_config
.kmod_extra_probes_list
.value
;
767 ret
= append_list_to_probes(list
);
774 * Load probes modules now.
776 ret
= modprobe_lttng(probes
, nr_probes
);