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
__attribute__((unused
)),
253 int priority
__attribute__((unused
)),
254 const char *file
__attribute__((unused
)),
255 int line
__attribute__((unused
)),
256 const char *fn
__attribute__((unused
)),
257 const char *format
, va_list args
)
261 if (vasprintf(&str
, format
, args
) < 0) {
265 DBG("libkmod: %s", str
);
270 * @brief Setup the libkmod context.
272 * Create the context, add a custom logging function and preload the
273 * ressources for faster operation.
275 * @returns \c 0 on success
278 static int setup_kmod_ctx(struct kmod_ctx
**ctx
)
282 *ctx
= kmod_new(NULL
, NULL
);
284 PERROR("Unable to create kmod library context");
290 * Parameter 2 of kmod_set_log_fn generates a
291 * -Wsuggest-attribute=formatkmod_set_log_fn warning that we can't fix,
295 DIAGNOSTIC_IGNORE_SUGGEST_ATTRIBUTE_FORMAT
296 kmod_set_log_fn(*ctx
, log_kmod
, NULL
);
298 ret
= kmod_load_resources(*ctx
);
300 ERR("Failed to load kmod library resources");
309 * @brief Loads the kernel modules in \p modules
311 * @param modules List of modules to load
312 * @param entries Number of modules in the list
314 * If the modules are required, we will return with error after the
315 * first failed module load, otherwise we continue loading.
317 * @returns \c 0 on success
320 static int modprobe_lttng(struct kern_modules_param
*modules
,
324 struct kmod_ctx
*ctx
;
326 ret
= setup_kmod_ctx(&ctx
);
331 for (i
= 0; i
< entries
; i
++) {
332 struct kmod_module
*mod
= NULL
;
334 ret
= kmod_module_new_from_name(ctx
, modules
[i
].name
, &mod
);
336 PERROR("Failed to create kmod module for %s", modules
[i
].name
);
340 ret
= kmod_module_probe_insert_module(mod
, 0,
341 NULL
, NULL
, NULL
, NULL
);
342 if (ret
== -EEXIST
) {
343 DBG("Module %s is already loaded", modules
[i
].name
);
345 } else if (ret
< 0) {
346 if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
) {
347 ERR("Unable to load required module %s",
351 DBG("Unable to load optional module %s; continuing",
356 DBG("Modprobe successfully %s", modules
[i
].name
);
357 modules
[i
].loaded
= true;
360 kmod_module_unref(mod
);
371 * @brief Recursively unload modules.
373 * This function implements the same modules unloading behavior as
374 * 'modprobe -r' or rmmod, it will recursevily go trought the \p module
375 * dependencies and unload modules with a refcount of 0.
377 * @param mod The module to unload
379 * @returns \c 0 on success
382 static int rmmod_recurse(struct kmod_module
*mod
) {
384 struct kmod_list
*deps
, *itr
;
386 if (kmod_module_get_initstate(mod
) == KMOD_MODULE_BUILTIN
) {
387 DBG("Module %s is builtin", kmod_module_get_name(mod
));
391 ret
= kmod_module_remove_module(mod
, 0);
393 deps
= kmod_module_get_dependencies(mod
);
395 kmod_list_foreach(itr
, deps
) {
396 struct kmod_module
*dep
= kmod_module_get_module(itr
);
397 if (kmod_module_get_refcnt(dep
) == 0) {
398 DBG("Recursive remove module %s",
399 kmod_module_get_name(dep
));
402 kmod_module_unref(dep
);
404 kmod_module_unref_list(deps
);
411 * @brief Unloads the kernel modules in \p modules
413 * @param modules List of modules to unload
414 * @param entries Number of modules in the list
417 static void modprobe_remove_lttng(const struct kern_modules_param
*modules
,
421 struct kmod_ctx
*ctx
;
423 ret
= setup_kmod_ctx(&ctx
);
428 for (i
= entries
- 1; i
>= 0; i
--) {
429 struct kmod_module
*mod
= NULL
;
431 if (!modules
[i
].loaded
) {
435 ret
= kmod_module_new_from_name(ctx
, modules
[i
].name
, &mod
);
437 PERROR("Failed to create kmod module for %s", modules
[i
].name
);
441 ret
= rmmod_recurse(mod
);
442 if (ret
== -EEXIST
) {
443 DBG("Module %s is not in kernel.", modules
[i
].name
);
444 } else if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
&& ret
< 0) {
445 ERR("Unable to remove module %s", modules
[i
].name
);
447 DBG("Modprobe removal successful %s",
451 kmod_module_unref(mod
);
460 #else /* HAVE_KMOD */
462 static int modprobe_lttng(struct kern_modules_param
*modules
,
468 for (i
= 0; i
< entries
; i
++) {
469 ret
= snprintf(modprobe
, sizeof(modprobe
),
470 "/sbin/modprobe %s%s",
471 modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
? "" : "-q ",
474 PERROR("snprintf modprobe");
477 modprobe
[sizeof(modprobe
) - 1] = '\0';
478 ret
= system(modprobe
);
480 if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
) {
481 ERR("Unable to launch modprobe for required module %s",
485 DBG("Unable to launch modprobe for optional module %s; continuing",
489 } else if (WEXITSTATUS(ret
) != 0) {
490 if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
) {
491 ERR("Unable to load required module %s",
495 DBG("Unable to load optional module %s; continuing",
500 DBG("Modprobe successfully %s", modules
[i
].name
);
501 modules
[i
].loaded
= true;
509 static void modprobe_remove_lttng(const struct kern_modules_param
*modules
,
515 for (i
= entries
- 1; i
>= 0; i
--) {
516 if (!modules
[i
].loaded
) {
519 ret
= snprintf(modprobe
, sizeof(modprobe
),
520 "/sbin/modprobe -r -q %s",
523 PERROR("snprintf modprobe -r");
526 modprobe
[sizeof(modprobe
) - 1] = '\0';
527 ret
= system(modprobe
);
529 if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
) {
530 ERR("Unable to launch modprobe -r for required module %s",
533 DBG("Unable to launch modprobe -r for optional module %s",
536 } else if (WEXITSTATUS(ret
) != 0) {
537 if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
) {
538 ERR("Unable to remove required module %s",
541 DBG("Unable to remove optional module %s",
545 DBG("Modprobe removal successful %s", modules
[i
].name
);
550 #endif /* HAVE_KMOD */
553 * Remove control kernel module(s) in reverse load order.
555 void modprobe_remove_lttng_control(void)
557 modprobe_remove_lttng(kern_modules_control_core
,
558 ARRAY_SIZE(kern_modules_control_core
));
561 static void free_probes(void)
568 for (i
= 0; i
< nr_probes
; ++i
) {
569 free(probes
[i
].name
);
577 * Remove data kernel modules in reverse load order.
579 void modprobe_remove_lttng_data(void)
585 modprobe_remove_lttng(probes
, nr_probes
);
590 * Remove all kernel modules in reverse order.
592 void modprobe_remove_lttng_all(void)
594 modprobe_remove_lttng_data();
595 modprobe_remove_lttng_control();
599 * Load control kernel module(s).
601 int modprobe_lttng_control(void)
603 return modprobe_lttng(kern_modules_control_core
,
604 ARRAY_SIZE(kern_modules_control_core
));
608 * Grow global list of probes (double capacity or set it to 1 if
609 * currently 0 and copy existing data).
611 static int grow_probes(void)
614 struct kern_modules_param
*tmp_probes
;
616 /* Initialize capacity to 1 if 0. */
617 if (probes_capacity
== 0) {
618 probes
= (kern_modules_param
*) zmalloc(sizeof(*probes
));
620 PERROR("malloc probe list");
629 probes_capacity
*= 2;
631 tmp_probes
= (kern_modules_param
*) zmalloc(sizeof(*tmp_probes
) * probes_capacity
);
633 PERROR("malloc probe list");
637 for (i
= 0; i
< nr_probes
; ++i
) {
638 /* Ownership of 'name' field is transferred. */
639 tmp_probes
[i
] = probes
[i
];
642 /* Replace probes with larger copy. */
650 * Appends a comma-separated list of probes to the global list
653 static int append_list_to_probes(const char *list
)
657 char *tmp_list
, *cur_list
, *saveptr
;
661 cur_list
= tmp_list
= strdup(list
);
663 PERROR("strdup temp list");
669 struct kern_modules_param
*cur_mod
;
671 next
= strtok_r(cur_list
, ",", &saveptr
);
677 /* filter leading spaces */
678 while (*next
== ' ') {
682 if (probes_capacity
<= nr_probes
) {
689 /* Length 13 is "lttng-probe-" + \0 */
690 name_len
= strlen(next
) + 13;
692 cur_mod
= &probes
[nr_probes
];
693 cur_mod
->name
= (char *) zmalloc(name_len
);
694 if (!cur_mod
->name
) {
695 PERROR("malloc probe list");
700 ret
= snprintf(cur_mod
->name
, name_len
, "lttng-probe-%s", next
);
702 PERROR("snprintf modprobe name");
707 cur_mod
->load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
;
722 * Load data kernel module(s).
724 int modprobe_lttng_data(void)
730 * Base probes: either from command line option, environment
731 * variable or default list.
733 list
= the_config
.kmod_probes_list
.value
;
735 /* User-specified probes. */
736 ret
= append_list_to_probes(list
);
741 /* Default probes. */
742 int def_len
= ARRAY_SIZE(kern_modules_probes_default
);
744 probes
= (kern_modules_param
*) zmalloc(sizeof(*probes
) * def_len
);
746 PERROR("malloc probe list");
750 nr_probes
= probes_capacity
= def_len
;
752 for (i
= 0; i
< def_len
; ++i
) {
753 char* name
= strdup(kern_modules_probes_default
[i
].name
);
756 PERROR("strdup probe item");
761 probes
[i
].name
= name
;
762 probes
[i
].load_policy
= kern_modules_probes_default
[i
].load_policy
;
767 * Extra modules? Append them to current probes list.
769 list
= the_config
.kmod_extra_probes_list
.value
;
771 ret
= append_list_to_probes(list
);
778 * Load probes modules now.
780 ret
= modprobe_lttng(probes
, nr_probes
);