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
,
57 /* LTTng kerneltracer probe modules list */
58 struct kern_modules_param kern_modules_probes_default
[] = {
60 .name
= (char *) "lttng-probe-asoc",
61 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
64 .name
= (char *) "lttng-probe-block",
65 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
68 .name
= (char *) "lttng-probe-btrfs",
69 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
72 .name
= (char *) "lttng-probe-compaction",
73 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
76 .name
= (char *) "lttng-probe-ext3",
77 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
80 .name
= (char *) "lttng-probe-ext4",
81 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
84 .name
= (char *) "lttng-probe-gpio",
85 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
88 .name
= (char *) "lttng-probe-i2c",
89 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
92 .name
= (char *) "lttng-probe-irq",
93 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
96 .name
= (char *) "lttng-probe-jbd",
97 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
100 .name
= (char *) "lttng-probe-jbd2",
101 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
104 .name
= (char *) "lttng-probe-kmem",
105 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
108 .name
= (char *) "lttng-probe-kvm",
109 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
112 .name
= (char *) "lttng-probe-kvm-x86",
113 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
116 .name
= (char *) "lttng-probe-kvm-x86-mmu",
117 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
120 .name
= (char *) "lttng-probe-lock",
121 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
124 .name
= (char *) "lttng-probe-module",
125 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
128 .name
= (char *) "lttng-probe-napi",
129 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
132 .name
= (char *) "lttng-probe-net",
133 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
136 .name
= (char *) "lttng-probe-power",
137 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
140 .name
= (char *) "lttng-probe-preemptirq",
141 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
144 .name
= (char *) "lttng-probe-printk",
145 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
148 .name
= (char *) "lttng-probe-random",
149 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
152 .name
= (char *) "lttng-probe-rcu",
153 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
156 .name
= (char *) "lttng-probe-regmap",
157 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
160 .name
= (char *) "lttng-probe-regulator",
161 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
164 .name
= (char *) "lttng-probe-rpm",
165 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
168 .name
= (char *) "lttng-probe-sched",
169 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
172 .name
= (char *) "lttng-probe-scsi",
173 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
176 .name
= (char *) "lttng-probe-signal",
177 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
180 .name
= (char *) "lttng-probe-skb",
181 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
184 .name
= (char *) "lttng-probe-sock",
185 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
188 .name
= (char *) "lttng-probe-statedump",
189 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
192 .name
= (char *) "lttng-probe-sunrpc",
193 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
196 .name
= (char *) "lttng-probe-timer",
197 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
200 .name
= (char *) "lttng-probe-udp",
201 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
204 .name
= (char *) "lttng-probe-vmscan",
205 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
208 .name
= (char *) "lttng-probe-v4l2",
209 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
212 .name
= (char *) "lttng-probe-workqueue",
213 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
216 .name
= (char *) "lttng-probe-writeback",
217 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
220 .name
= (char *) "lttng-probe-x86-irq-vectors",
221 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
224 .name
= (char *) "lttng-probe-x86-exceptions",
225 .load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
,
229 /* dynamic probe modules list */
230 static struct kern_modules_param
*probes
;
231 static int nr_probes
;
232 static int probes_capacity
;
238 * @brief Logging function for libkmod integration.
240 static void log_kmod(void *data
, int priority
, const char *file
, int line
,
241 const char *fn
, const char *format
, va_list args
)
245 if (vasprintf(&str
, format
, args
) < 0) {
249 DBG("libkmod: %s", str
);
254 * @brief Setup the libkmod context.
256 * Create the context, add a custom logging function and preload the
257 * ressources for faster operation.
259 * @returns \c 0 on success
262 static int setup_kmod_ctx(struct kmod_ctx
**ctx
)
266 *ctx
= kmod_new(NULL
, NULL
);
268 PERROR("Unable to create kmod library context");
273 kmod_set_log_fn(*ctx
, log_kmod
, NULL
);
274 ret
= kmod_load_resources(*ctx
);
276 ERR("Failed to load kmod library resources");
285 * @brief Loads the kernel modules in \p modules
287 * @param modules List of modules to load
288 * @param entries Number of modules in the list
290 * If the modules are required, we will return with error after the
291 * first failed module load, otherwise we continue loading.
293 * @returns \c 0 on success
296 static int modprobe_lttng(struct kern_modules_param
*modules
,
300 struct kmod_ctx
*ctx
;
302 ret
= setup_kmod_ctx(&ctx
);
307 for (i
= 0; i
< entries
; i
++) {
308 struct kmod_module
*mod
= NULL
;
310 ret
= kmod_module_new_from_name(ctx
, modules
[i
].name
, &mod
);
312 PERROR("Failed to create kmod module for %s", modules
[i
].name
);
316 ret
= kmod_module_probe_insert_module(mod
, 0,
317 NULL
, NULL
, NULL
, NULL
);
318 if (ret
== -EEXIST
) {
319 DBG("Module %s is already loaded", modules
[i
].name
);
321 } else if (ret
< 0) {
322 if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
) {
323 ERR("Unable to load required module %s",
327 DBG("Unable to load optional module %s; continuing",
332 DBG("Modprobe successfully %s", modules
[i
].name
);
333 modules
[i
].loaded
= true;
336 kmod_module_unref(mod
);
347 * @brief Recursively unload modules.
349 * This function implements the same modules unloading behavior as
350 * 'modprobe -r' or rmmod, it will recursevily go trought the \p module
351 * dependencies and unload modules with a refcount of 0.
353 * @param mod The module to unload
355 * @returns \c 0 on success
358 static int rmmod_recurse(struct kmod_module
*mod
) {
360 struct kmod_list
*deps
, *itr
;
362 if (kmod_module_get_initstate(mod
) == KMOD_MODULE_BUILTIN
) {
363 DBG("Module %s is builtin", kmod_module_get_name(mod
));
367 ret
= kmod_module_remove_module(mod
, 0);
369 deps
= kmod_module_get_dependencies(mod
);
371 kmod_list_foreach(itr
, deps
) {
372 struct kmod_module
*dep
= kmod_module_get_module(itr
);
373 if (kmod_module_get_refcnt(dep
) == 0) {
374 DBG("Recursive remove module %s",
375 kmod_module_get_name(dep
));
378 kmod_module_unref(dep
);
380 kmod_module_unref_list(deps
);
387 * @brief Unloads the kernel modules in \p modules
389 * @param modules List of modules to unload
390 * @param entries Number of modules in the list
393 static void modprobe_remove_lttng(const struct kern_modules_param
*modules
,
397 struct kmod_ctx
*ctx
;
399 ret
= setup_kmod_ctx(&ctx
);
404 for (i
= entries
- 1; i
>= 0; i
--) {
405 struct kmod_module
*mod
= NULL
;
407 if (!modules
[i
].loaded
) {
411 ret
= kmod_module_new_from_name(ctx
, modules
[i
].name
, &mod
);
413 PERROR("Failed to create kmod module for %s", modules
[i
].name
);
417 ret
= rmmod_recurse(mod
);
418 if (ret
== -EEXIST
) {
419 DBG("Module %s is not in kernel.", modules
[i
].name
);
420 } else if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
&& ret
< 0) {
421 ERR("Unable to remove module %s", modules
[i
].name
);
423 DBG("Modprobe removal successful %s",
427 kmod_module_unref(mod
);
436 #else /* HAVE_KMOD */
438 static int modprobe_lttng(struct kern_modules_param
*modules
,
444 for (i
= 0; i
< entries
; i
++) {
445 ret
= snprintf(modprobe
, sizeof(modprobe
),
446 "/sbin/modprobe %s%s",
447 modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
? "" : "-q ",
450 PERROR("snprintf modprobe");
453 modprobe
[sizeof(modprobe
) - 1] = '\0';
454 ret
= system(modprobe
);
456 if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
) {
457 ERR("Unable to launch modprobe for required module %s",
461 DBG("Unable to launch modprobe for optional module %s; continuing",
465 } else if (WEXITSTATUS(ret
) != 0) {
466 if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
) {
467 ERR("Unable to load required module %s",
471 DBG("Unable to load optional module %s; continuing",
476 DBG("Modprobe successfully %s", modules
[i
].name
);
477 modules
[i
].loaded
= true;
485 static void modprobe_remove_lttng(const struct kern_modules_param
*modules
,
491 for (i
= entries
- 1; i
>= 0; i
--) {
492 if (!modules
[i
].loaded
) {
495 ret
= snprintf(modprobe
, sizeof(modprobe
),
496 "/sbin/modprobe -r -q %s",
499 PERROR("snprintf modprobe -r");
502 modprobe
[sizeof(modprobe
) - 1] = '\0';
503 ret
= system(modprobe
);
505 ERR("Unable to launch modprobe -r for module %s",
507 } else if (modules
[i
].load_policy
== KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED
&& WEXITSTATUS(ret
) != 0) {
508 ERR("Unable to remove module %s",
511 DBG("Modprobe removal successful %s",
517 #endif /* HAVE_KMOD */
520 * Remove control kernel module(s) in reverse load order.
522 void modprobe_remove_lttng_control(void)
524 modprobe_remove_lttng(kern_modules_control_core
,
525 ARRAY_SIZE(kern_modules_control_core
));
528 static void free_probes(void)
535 for (i
= 0; i
< nr_probes
; ++i
) {
536 free(probes
[i
].name
);
544 * Remove data kernel modules in reverse load order.
546 void modprobe_remove_lttng_data(void)
552 modprobe_remove_lttng(probes
, nr_probes
);
557 * Remove all kernel modules in reverse order.
559 void modprobe_remove_lttng_all(void)
561 modprobe_remove_lttng_data();
562 modprobe_remove_lttng_control();
566 * Load control kernel module(s).
568 int modprobe_lttng_control(void)
570 return modprobe_lttng(kern_modules_control_core
,
571 ARRAY_SIZE(kern_modules_control_core
));
575 * Grow global list of probes (double capacity or set it to 1 if
576 * currently 0 and copy existing data).
578 static int grow_probes(void)
581 struct kern_modules_param
*tmp_probes
;
583 /* Initialize capacity to 1 if 0. */
584 if (probes_capacity
== 0) {
585 probes
= zmalloc(sizeof(*probes
));
587 PERROR("malloc probe list");
596 probes_capacity
*= 2;
598 tmp_probes
= zmalloc(sizeof(*tmp_probes
) * probes_capacity
);
600 PERROR("malloc probe list");
604 for (i
= 0; i
< nr_probes
; ++i
) {
605 /* Ownership of 'name' field is transferred. */
606 tmp_probes
[i
] = probes
[i
];
609 /* Replace probes with larger copy. */
617 * Appends a comma-separated list of probes to the global list
620 static int append_list_to_probes(const char *list
)
624 char *tmp_list
, *cur_list
;
628 cur_list
= tmp_list
= strdup(list
);
630 PERROR("strdup temp list");
636 struct kern_modules_param
*cur_mod
;
638 next
= strtok(cur_list
, ",");
644 /* filter leading spaces */
645 while (*next
== ' ') {
649 if (probes_capacity
<= nr_probes
) {
656 /* Length 13 is "lttng-probe-" + \0 */
657 name_len
= strlen(next
) + 13;
659 cur_mod
= &probes
[nr_probes
];
660 cur_mod
->name
= zmalloc(name_len
);
661 if (!cur_mod
->name
) {
662 PERROR("malloc probe list");
667 ret
= snprintf(cur_mod
->name
, name_len
, "lttng-probe-%s", next
);
669 PERROR("snprintf modprobe name");
674 cur_mod
->load_policy
= KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL
;
689 * Load data kernel module(s).
691 int modprobe_lttng_data(void)
697 * Base probes: either from command line option, environment
698 * variable or default list.
700 list
= config
.kmod_probes_list
.value
;
702 /* User-specified probes. */
703 ret
= append_list_to_probes(list
);
708 /* Default probes. */
709 int def_len
= ARRAY_SIZE(kern_modules_probes_default
);
711 probes
= zmalloc(sizeof(*probes
) * def_len
);
713 PERROR("malloc probe list");
717 nr_probes
= probes_capacity
= def_len
;
719 for (i
= 0; i
< def_len
; ++i
) {
720 char* name
= strdup(kern_modules_probes_default
[i
].name
);
723 PERROR("strdup probe item");
728 probes
[i
].name
= name
;
729 probes
[i
].load_policy
= kern_modules_probes_default
[i
].load_policy
;
734 * Extra modules? Append them to current probes list.
736 list
= config
.kmod_extra_probes_list
.value
;
738 ret
= append_list_to_probes(list
);
745 * Load probes modules now.
747 ret
= modprobe_lttng(probes
, nr_probes
);