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 void log_kmod(void *data
, int priority
, const char *file
, int line
,
252 const char *fn
, const char *format
, va_list args
)
256 if (vasprintf(&str
, format
, args
) < 0) {
260 DBG("libkmod: %s", str
);
265 * @brief Setup the libkmod context.
267 * Create the context, add a custom logging function and preload the
268 * ressources for faster operation.
270 * @returns \c 0 on success
273 static int setup_kmod_ctx(struct kmod_ctx
**ctx
)
277 *ctx
= kmod_new(NULL
, NULL
);
279 PERROR("Unable to create kmod library context");
284 kmod_set_log_fn(*ctx
, log_kmod
, NULL
);
285 ret
= kmod_load_resources(*ctx
);
287 ERR("Failed to load kmod library resources");
296 * @brief Loads the kernel modules in \p modules
298 * @param modules List of modules to load
299 * @param entries Number of modules in the list
301 * If the modules are required, we will return with error after the
302 * first failed module load, otherwise we continue loading.
304 * @returns \c 0 on success
307 static int modprobe_lttng(struct kern_modules_param
*modules
,
311 struct kmod_ctx
*ctx
;
313 ret
= setup_kmod_ctx(&ctx
);
318 for (i
= 0; i
< entries
; i
++) {
319 struct kmod_module
*mod
= NULL
;
321 ret
= kmod_module_new_from_name(ctx
, modules
[i
].name
, &mod
);
323 PERROR("Failed to create kmod module for %s", modules
[i
].name
);
327 ret
= kmod_module_probe_insert_module(mod
, 0,
328 NULL
, NULL
, NULL
, NULL
);
329 if (ret
== -EEXIST
) {
330 DBG("Module %s is already loaded", modules
[i
].name
);
332 } else if (ret
< 0) {
333 if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
) {
334 ERR("Unable to load required module %s",
338 DBG("Unable to load optional module %s; continuing",
343 DBG("Modprobe successfully %s", modules
[i
].name
);
344 modules
[i
].loaded
= true;
347 kmod_module_unref(mod
);
358 * @brief Recursively unload modules.
360 * This function implements the same modules unloading behavior as
361 * 'modprobe -r' or rmmod, it will recursevily go trought the \p module
362 * dependencies and unload modules with a refcount of 0.
364 * @param mod The module to unload
366 * @returns \c 0 on success
369 static int rmmod_recurse(struct kmod_module
*mod
) {
371 struct kmod_list
*deps
, *itr
;
373 if (kmod_module_get_initstate(mod
) == KMOD_MODULE_BUILTIN
) {
374 DBG("Module %s is builtin", kmod_module_get_name(mod
));
378 ret
= kmod_module_remove_module(mod
, 0);
380 deps
= kmod_module_get_dependencies(mod
);
382 kmod_list_foreach(itr
, deps
) {
383 struct kmod_module
*dep
= kmod_module_get_module(itr
);
384 if (kmod_module_get_refcnt(dep
) == 0) {
385 DBG("Recursive remove module %s",
386 kmod_module_get_name(dep
));
389 kmod_module_unref(dep
);
391 kmod_module_unref_list(deps
);
398 * @brief Unloads the kernel modules in \p modules
400 * @param modules List of modules to unload
401 * @param entries Number of modules in the list
404 static void modprobe_remove_lttng(const struct kern_modules_param
*modules
,
408 struct kmod_ctx
*ctx
;
410 ret
= setup_kmod_ctx(&ctx
);
415 for (i
= entries
- 1; i
>= 0; i
--) {
416 struct kmod_module
*mod
= NULL
;
418 if (!modules
[i
].loaded
) {
422 ret
= kmod_module_new_from_name(ctx
, modules
[i
].name
, &mod
);
424 PERROR("Failed to create kmod module for %s", modules
[i
].name
);
428 ret
= rmmod_recurse(mod
);
429 if (ret
== -EEXIST
) {
430 DBG("Module %s is not in kernel.", modules
[i
].name
);
431 } else if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
&& ret
< 0) {
432 ERR("Unable to remove module %s", modules
[i
].name
);
434 DBG("Modprobe removal successful %s",
438 kmod_module_unref(mod
);
447 #else /* HAVE_KMOD */
449 static int modprobe_lttng(struct kern_modules_param
*modules
,
455 for (i
= 0; i
< entries
; i
++) {
456 ret
= snprintf(modprobe
, sizeof(modprobe
),
457 "/sbin/modprobe %s%s",
458 modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
? "" : "-q ",
461 PERROR("snprintf modprobe");
464 modprobe
[sizeof(modprobe
) - 1] = '\0';
465 ret
= system(modprobe
);
467 if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
) {
468 ERR("Unable to launch modprobe for required module %s",
472 DBG("Unable to launch modprobe for optional module %s; continuing",
476 } else if (WEXITSTATUS(ret
) != 0) {
477 if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
) {
478 ERR("Unable to load required module %s",
482 DBG("Unable to load optional module %s; continuing",
487 DBG("Modprobe successfully %s", modules
[i
].name
);
488 modules
[i
].loaded
= true;
496 static void modprobe_remove_lttng(const struct kern_modules_param
*modules
,
502 for (i
= entries
- 1; i
>= 0; i
--) {
503 if (!modules
[i
].loaded
) {
506 ret
= snprintf(modprobe
, sizeof(modprobe
),
507 "/sbin/modprobe -r -q %s",
510 PERROR("snprintf modprobe -r");
513 modprobe
[sizeof(modprobe
) - 1] = '\0';
514 ret
= system(modprobe
);
516 if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
) {
517 ERR("Unable to launch modprobe -r for required module %s",
520 DBG("Unable to launch modprobe -r for optional module %s",
523 } else if (WEXITSTATUS(ret
) != 0) {
524 if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
) {
525 ERR("Unable to remove required module %s",
528 DBG("Unable to remove optional module %s",
532 DBG("Modprobe removal successful %s", modules
[i
].name
);
537 #endif /* HAVE_KMOD */
540 * Remove control kernel module(s) in reverse load order.
542 void modprobe_remove_lttng_control(void)
544 modprobe_remove_lttng(kern_modules_control_core
,
545 ARRAY_SIZE(kern_modules_control_core
));
548 static void free_probes(void)
555 for (i
= 0; i
< nr_probes
; ++i
) {
556 free(probes
[i
].name
);
564 * Remove data kernel modules in reverse load order.
566 void modprobe_remove_lttng_data(void)
572 modprobe_remove_lttng(probes
, nr_probes
);
577 * Remove all kernel modules in reverse order.
579 void modprobe_remove_lttng_all(void)
581 modprobe_remove_lttng_data();
582 modprobe_remove_lttng_control();
586 * Load control kernel module(s).
588 int modprobe_lttng_control(void)
590 return modprobe_lttng(kern_modules_control_core
,
591 ARRAY_SIZE(kern_modules_control_core
));
595 * Grow global list of probes (double capacity or set it to 1 if
596 * currently 0 and copy existing data).
598 static int grow_probes(void)
601 struct kern_modules_param
*tmp_probes
;
603 /* Initialize capacity to 1 if 0. */
604 if (probes_capacity
== 0) {
605 probes
= zmalloc(sizeof(*probes
));
607 PERROR("malloc probe list");
616 probes_capacity
*= 2;
618 tmp_probes
= zmalloc(sizeof(*tmp_probes
) * probes_capacity
);
620 PERROR("malloc probe list");
624 for (i
= 0; i
< nr_probes
; ++i
) {
625 /* Ownership of 'name' field is transferred. */
626 tmp_probes
[i
] = probes
[i
];
629 /* Replace probes with larger copy. */
637 * Appends a comma-separated list of probes to the global list
640 static int append_list_to_probes(const char *list
)
644 char *tmp_list
, *cur_list
, *saveptr
;
648 cur_list
= tmp_list
= strdup(list
);
650 PERROR("strdup temp list");
656 struct kern_modules_param
*cur_mod
;
658 next
= strtok_r(cur_list
, ",", &saveptr
);
664 /* filter leading spaces */
665 while (*next
== ' ') {
669 if (probes_capacity
<= nr_probes
) {
676 /* Length 13 is "lttng-probe-" + \0 */
677 name_len
= strlen(next
) + 13;
679 cur_mod
= &probes
[nr_probes
];
680 cur_mod
->name
= zmalloc(name_len
);
681 if (!cur_mod
->name
) {
682 PERROR("malloc probe list");
687 ret
= snprintf(cur_mod
->name
, name_len
, "lttng-probe-%s", next
);
689 PERROR("snprintf modprobe name");
694 cur_mod
->load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
;
709 * Load data kernel module(s).
711 int modprobe_lttng_data(void)
717 * Base probes: either from command line option, environment
718 * variable or default list.
720 list
= the_config
.kmod_probes_list
.value
;
722 /* User-specified probes. */
723 ret
= append_list_to_probes(list
);
728 /* Default probes. */
729 int def_len
= ARRAY_SIZE(kern_modules_probes_default
);
731 probes
= zmalloc(sizeof(*probes
) * def_len
);
733 PERROR("malloc probe list");
737 nr_probes
= probes_capacity
= def_len
;
739 for (i
= 0; i
< def_len
; ++i
) {
740 char* name
= strdup(kern_modules_probes_default
[i
].name
);
743 PERROR("strdup probe item");
748 probes
[i
].name
= name
;
749 probes
[i
].load_policy
= kern_modules_probes_default
[i
].load_policy
;
754 * Extra modules? Append them to current probes list.
756 list
= the_config
.kmod_extra_probes_list
.value
;
758 ret
= append_list_to_probes(list
);
765 * Load probes modules now.
767 ret
= modprobe_lttng(probes
, nr_probes
);