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.
22 #include <common/common.h>
23 #include <common/utils.h>
26 #include "kern-modules.h"
27 #include "lttng-sessiond.h"
29 /* LTTng kernel tracer mandatory core modules list */
30 struct kern_modules_param kern_modules_control_core
[] = {
32 .name
= (char *) "lttng-ring-buffer-client-discard",
33 .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
,
40 .name
= (char *) "lttng-ring-buffer-metadata-client",
41 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
,
44 .name
= (char *) "lttng-ring-buffer-client-mmap-discard",
45 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
,
48 .name
= (char *) "lttng-ring-buffer-client-mmap-overwrite",
49 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
,
52 .name
= (char *) "lttng-ring-buffer-metadata-mmap-client",
53 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
,
56 .name
= (char *) "lttng-ring-buffer-event_notifier-client",
57 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
61 /* LTTng kerneltracer probe modules list */
62 struct kern_modules_param kern_modules_probes_default
[] = {
64 .name
= (char *) "lttng-probe-asoc",
65 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
68 .name
= (char *) "lttng-probe-block",
69 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
72 .name
= (char *) "lttng-probe-btrfs",
73 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
76 .name
= (char *) "lttng-probe-compaction",
77 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
80 .name
= (char *) "lttng-probe-ext3",
81 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
84 .name
= (char *) "lttng-probe-ext4",
85 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
88 .name
= (char *) "lttng-probe-gpio",
89 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
92 .name
= (char *) "lttng-probe-i2c",
93 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
96 .name
= (char *) "lttng-probe-irq",
97 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
100 .name
= (char *) "lttng-probe-jbd",
101 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
104 .name
= (char *) "lttng-probe-jbd2",
105 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
108 .name
= (char *) "lttng-probe-kmem",
109 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
112 .name
= (char *) "lttng-probe-kvm",
113 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
116 .name
= (char *) "lttng-probe-kvm-x86",
117 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
120 .name
= (char *) "lttng-probe-kvm-x86-mmu",
121 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
124 .name
= (char *) "lttng-probe-lock",
125 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
128 .name
= (char *) "lttng-probe-module",
129 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
132 .name
= (char *) "lttng-probe-napi",
133 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
136 .name
= (char *) "lttng-probe-net",
137 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
140 .name
= (char *) "lttng-probe-power",
141 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
144 .name
= (char *) "lttng-probe-preemptirq",
145 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
148 .name
= (char *) "lttng-probe-printk",
149 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
152 .name
= (char *) "lttng-probe-random",
153 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
156 .name
= (char *) "lttng-probe-rcu",
157 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
160 .name
= (char *) "lttng-probe-regmap",
161 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
164 .name
= (char *) "lttng-probe-regulator",
165 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
168 .name
= (char *) "lttng-probe-rpm",
169 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
172 .name
= (char *) "lttng-probe-sched",
173 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
176 .name
= (char *) "lttng-probe-scsi",
177 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
180 .name
= (char *) "lttng-probe-signal",
181 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
184 .name
= (char *) "lttng-probe-skb",
185 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
188 .name
= (char *) "lttng-probe-sock",
189 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
192 .name
= (char *) "lttng-probe-statedump",
193 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
196 .name
= (char *) "lttng-probe-sunrpc",
197 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
200 .name
= (char *) "lttng-probe-timer",
201 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
204 .name
= (char *) "lttng-probe-udp",
205 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
208 .name
= (char *) "lttng-probe-vmscan",
209 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
212 .name
= (char *) "lttng-probe-v4l2",
213 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
216 .name
= (char *) "lttng-probe-workqueue",
217 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
220 .name
= (char *) "lttng-probe-writeback",
221 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
224 .name
= (char *) "lttng-probe-x86-irq-vectors",
225 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
228 .name
= (char *) "lttng-probe-x86-exceptions",
229 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
233 /* dynamic probe modules list */
234 static struct kern_modules_param
*probes
;
235 static int nr_probes
;
236 static int probes_capacity
;
242 * @brief Logging function for libkmod integration.
244 static void log_kmod(void *data
, int priority
, const char *file
, int line
,
245 const char *fn
, const char *format
, va_list args
)
249 if (vasprintf(&str
, format
, args
) < 0) {
253 DBG("libkmod: %s", str
);
258 * @brief Setup the libkmod context.
260 * Create the context, add a custom logging function and preload the
261 * ressources for faster operation.
263 * @returns \c 0 on success
266 static int setup_kmod_ctx(struct kmod_ctx
**ctx
)
270 *ctx
= kmod_new(NULL
, NULL
);
272 PERROR("Unable to create kmod library context");
277 kmod_set_log_fn(*ctx
, log_kmod
, NULL
);
278 ret
= kmod_load_resources(*ctx
);
280 ERR("Failed to load kmod library resources");
289 * @brief Loads the kernel modules in \p modules
291 * @param modules List of modules to load
292 * @param entries Number of modules in the list
294 * If the modules are required, we will return with error after the
295 * first failed module load, otherwise we continue loading.
297 * @returns \c 0 on success
300 static int modprobe_lttng(struct kern_modules_param
*modules
,
304 struct kmod_ctx
*ctx
;
306 ret
= setup_kmod_ctx(&ctx
);
311 for (i
= 0; i
< entries
; i
++) {
312 struct kmod_module
*mod
= NULL
;
314 ret
= kmod_module_new_from_name(ctx
, modules
[i
].name
, &mod
);
316 PERROR("Failed to create kmod module for %s", modules
[i
].name
);
320 ret
= kmod_module_probe_insert_module(mod
, 0,
321 NULL
, NULL
, NULL
, NULL
);
322 if (ret
== -EEXIST
) {
323 DBG("Module %s is already loaded", modules
[i
].name
);
325 } else if (ret
< 0) {
326 if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
) {
327 ERR("Unable to load required module %s",
331 DBG("Unable to load optional module %s; continuing",
336 DBG("Modprobe successfully %s", modules
[i
].name
);
337 modules
[i
].loaded
= true;
340 kmod_module_unref(mod
);
351 * @brief Recursively unload modules.
353 * This function implements the same modules unloading behavior as
354 * 'modprobe -r' or rmmod, it will recursevily go trought the \p module
355 * dependencies and unload modules with a refcount of 0.
357 * @param mod The module to unload
359 * @returns \c 0 on success
362 static int rmmod_recurse(struct kmod_module
*mod
) {
364 struct kmod_list
*deps
, *itr
;
366 if (kmod_module_get_initstate(mod
) == KMOD_MODULE_BUILTIN
) {
367 DBG("Module %s is builtin", kmod_module_get_name(mod
));
371 ret
= kmod_module_remove_module(mod
, 0);
373 deps
= kmod_module_get_dependencies(mod
);
375 kmod_list_foreach(itr
, deps
) {
376 struct kmod_module
*dep
= kmod_module_get_module(itr
);
377 if (kmod_module_get_refcnt(dep
) == 0) {
378 DBG("Recursive remove module %s",
379 kmod_module_get_name(dep
));
382 kmod_module_unref(dep
);
384 kmod_module_unref_list(deps
);
391 * @brief Unloads the kernel modules in \p modules
393 * @param modules List of modules to unload
394 * @param entries Number of modules in the list
397 static void modprobe_remove_lttng(const struct kern_modules_param
*modules
,
401 struct kmod_ctx
*ctx
;
403 ret
= setup_kmod_ctx(&ctx
);
408 for (i
= entries
- 1; i
>= 0; i
--) {
409 struct kmod_module
*mod
= NULL
;
411 if (!modules
[i
].loaded
) {
415 ret
= kmod_module_new_from_name(ctx
, modules
[i
].name
, &mod
);
417 PERROR("Failed to create kmod module for %s", modules
[i
].name
);
421 ret
= rmmod_recurse(mod
);
422 if (ret
== -EEXIST
) {
423 DBG("Module %s is not in kernel.", modules
[i
].name
);
424 } else if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
&& ret
< 0) {
425 ERR("Unable to remove module %s", modules
[i
].name
);
427 DBG("Modprobe removal successful %s",
431 kmod_module_unref(mod
);
440 #else /* HAVE_KMOD */
442 static int modprobe_lttng(struct kern_modules_param
*modules
,
448 for (i
= 0; i
< entries
; i
++) {
449 ret
= snprintf(modprobe
, sizeof(modprobe
),
450 "/sbin/modprobe %s%s",
451 modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
? "" : "-q ",
454 PERROR("snprintf modprobe");
457 modprobe
[sizeof(modprobe
) - 1] = '\0';
458 ret
= system(modprobe
);
460 if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
) {
461 ERR("Unable to launch modprobe for required module %s",
465 DBG("Unable to launch modprobe for optional module %s; continuing",
469 } else if (WEXITSTATUS(ret
) != 0) {
470 if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
) {
471 ERR("Unable to load required module %s",
475 DBG("Unable to load optional module %s; continuing",
480 DBG("Modprobe successfully %s", modules
[i
].name
);
481 modules
[i
].loaded
= true;
489 static void modprobe_remove_lttng(const struct kern_modules_param
*modules
,
495 for (i
= entries
- 1; i
>= 0; i
--) {
496 if (!modules
[i
].loaded
) {
499 ret
= snprintf(modprobe
, sizeof(modprobe
),
500 "/sbin/modprobe -r -q %s",
503 PERROR("snprintf modprobe -r");
506 modprobe
[sizeof(modprobe
) - 1] = '\0';
507 ret
= system(modprobe
);
509 ERR("Unable to launch modprobe -r for module %s",
511 } else if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
&& WEXITSTATUS(ret
) != 0) {
512 ERR("Unable to remove module %s",
515 DBG("Modprobe removal successful %s",
521 #endif /* HAVE_KMOD */
524 * Remove control kernel module(s) in reverse load order.
526 void modprobe_remove_lttng_control(void)
528 modprobe_remove_lttng(kern_modules_control_core
,
529 ARRAY_SIZE(kern_modules_control_core
));
532 static void free_probes(void)
539 for (i
= 0; i
< nr_probes
; ++i
) {
540 free(probes
[i
].name
);
548 * Remove data kernel modules in reverse load order.
550 void modprobe_remove_lttng_data(void)
556 modprobe_remove_lttng(probes
, nr_probes
);
561 * Remove all kernel modules in reverse order.
563 void modprobe_remove_lttng_all(void)
565 modprobe_remove_lttng_data();
566 modprobe_remove_lttng_control();
570 * Load control kernel module(s).
572 int modprobe_lttng_control(void)
574 return modprobe_lttng(kern_modules_control_core
,
575 ARRAY_SIZE(kern_modules_control_core
));
579 * Grow global list of probes (double capacity or set it to 1 if
580 * currently 0 and copy existing data).
582 static int grow_probes(void)
585 struct kern_modules_param
*tmp_probes
;
587 /* Initialize capacity to 1 if 0. */
588 if (probes_capacity
== 0) {
589 probes
= zmalloc(sizeof(*probes
));
591 PERROR("malloc probe list");
600 probes_capacity
*= 2;
602 tmp_probes
= zmalloc(sizeof(*tmp_probes
) * probes_capacity
);
604 PERROR("malloc probe list");
608 for (i
= 0; i
< nr_probes
; ++i
) {
609 /* Ownership of 'name' field is transferred. */
610 tmp_probes
[i
] = probes
[i
];
613 /* Replace probes with larger copy. */
621 * Appends a comma-separated list of probes to the global list
624 static int append_list_to_probes(const char *list
)
628 char *tmp_list
, *cur_list
;
632 cur_list
= tmp_list
= strdup(list
);
634 PERROR("strdup temp list");
640 struct kern_modules_param
*cur_mod
;
642 next
= strtok(cur_list
, ",");
648 /* filter leading spaces */
649 while (*next
== ' ') {
653 if (probes_capacity
<= nr_probes
) {
660 /* Length 13 is "lttng-probe-" + \0 */
661 name_len
= strlen(next
) + 13;
663 cur_mod
= &probes
[nr_probes
];
664 cur_mod
->name
= zmalloc(name_len
);
665 if (!cur_mod
->name
) {
666 PERROR("malloc probe list");
671 ret
= snprintf(cur_mod
->name
, name_len
, "lttng-probe-%s", next
);
673 PERROR("snprintf modprobe name");
678 cur_mod
->load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
;
693 * Load data kernel module(s).
695 int modprobe_lttng_data(void)
701 * Base probes: either from command line option, environment
702 * variable or default list.
704 list
= config
.kmod_probes_list
.value
;
706 /* User-specified probes. */
707 ret
= append_list_to_probes(list
);
712 /* Default probes. */
713 int def_len
= ARRAY_SIZE(kern_modules_probes_default
);
715 probes
= zmalloc(sizeof(*probes
) * def_len
);
717 PERROR("malloc probe list");
721 nr_probes
= probes_capacity
= def_len
;
723 for (i
= 0; i
< def_len
; ++i
) {
724 char* name
= strdup(kern_modules_probes_default
[i
].name
);
727 PERROR("strdup probe item");
732 probes
[i
].name
= name
;
733 probes
[i
].load_policy
= kern_modules_probes_default
[i
].load_policy
;
738 * Extra modules? Append them to current probes list.
740 list
= config
.kmod_extra_probes_list
.value
;
742 ret
= append_list_to_probes(list
);
749 * Load probes modules now.
751 ret
= modprobe_lttng(probes
, nr_probes
);