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 #define LTTNG_MOD_REQUIRED 1
30 #define LTTNG_MOD_OPTIONAL 0
32 /* LTTng kernel tracer mandatory core modules list */
33 /* TODO: the new trigger client might not be present in previous lttng-modules
34 * should it be optional?
35 * Can we reuse this to also know of the trigger feature is supported?
37 struct kern_modules_param kern_modules_control_core
[] = {
38 { (char *) "lttng-ring-buffer-client-discard" },
39 { (char *) "lttng-ring-buffer-client-overwrite" },
40 { (char *) "lttng-ring-buffer-metadata-client" },
41 { (char *) "lttng-ring-buffer-client-mmap-discard" },
42 { (char *) "lttng-ring-buffer-client-mmap-overwrite" },
43 { (char *) "lttng-ring-buffer-metadata-mmap-client" },
44 { (char *) "lttng-ring-buffer-trigger-client" },
47 /* LTTng kernel tracer probe modules list */
48 struct kern_modules_param kern_modules_probes_default
[] = {
49 { (char *) "lttng-probe-asoc" },
50 { (char *) "lttng-probe-block" },
51 { (char *) "lttng-probe-btrfs" },
52 { (char *) "lttng-probe-compaction" },
53 { (char *) "lttng-probe-ext3" },
54 { (char *) "lttng-probe-ext4" },
55 { (char *) "lttng-probe-gpio" },
56 { (char *) "lttng-probe-i2c" },
57 { (char *) "lttng-probe-irq" },
58 { (char *) "lttng-probe-jbd" },
59 { (char *) "lttng-probe-jbd2" },
60 { (char *) "lttng-probe-kmem" },
61 { (char *) "lttng-probe-kvm" },
62 { (char *) "lttng-probe-kvm-x86" },
63 { (char *) "lttng-probe-kvm-x86-mmu" },
64 { (char *) "lttng-probe-lock" },
65 { (char *) "lttng-probe-module" },
66 { (char *) "lttng-probe-napi" },
67 { (char *) "lttng-probe-net" },
68 { (char *) "lttng-probe-power" },
69 { (char *) "lttng-probe-preemptirq" },
70 { (char *) "lttng-probe-printk" },
71 { (char *) "lttng-probe-random" },
72 { (char *) "lttng-probe-rcu" },
73 { (char *) "lttng-probe-regmap" },
74 { (char *) "lttng-probe-regulator" },
75 { (char *) "lttng-probe-rpm" },
76 { (char *) "lttng-probe-sched" },
77 { (char *) "lttng-probe-scsi" },
78 { (char *) "lttng-probe-signal" },
79 { (char *) "lttng-probe-skb" },
80 { (char *) "lttng-probe-sock" },
81 { (char *) "lttng-probe-statedump" },
82 { (char *) "lttng-probe-sunrpc" },
83 { (char *) "lttng-probe-timer" },
84 { (char *) "lttng-probe-udp" },
85 { (char *) "lttng-probe-vmscan" },
86 { (char *) "lttng-probe-v4l2" },
87 { (char *) "lttng-probe-workqueue" },
88 { (char *) "lttng-probe-writeback" },
89 { (char *) "lttng-probe-x86-irq-vectors" },
90 { (char *) "lttng-probe-x86-exceptions" },
93 /* dynamic probe modules list */
94 static struct kern_modules_param
*probes
;
96 static int probes_capacity
;
102 * @brief Logging function for libkmod integration.
104 static void log_kmod(void *data
, int priority
, const char *file
, int line
,
105 const char *fn
, const char *format
, va_list args
)
109 if (vasprintf(&str
, format
, args
) < 0) {
113 DBG("libkmod: %s", str
);
118 * @brief Setup the libkmod context.
120 * Create the context, add a custom logging function and preload the
121 * ressources for faster operation.
123 * @returns \c 0 on success
126 static int setup_kmod_ctx(struct kmod_ctx
**ctx
)
130 *ctx
= kmod_new(NULL
, NULL
);
132 PERROR("Unable to create kmod library context");
137 kmod_set_log_fn(*ctx
, log_kmod
, NULL
);
138 ret
= kmod_load_resources(*ctx
);
140 ERR("Failed to load kmod library resources");
149 * @brief Loads the kernel modules in \p modules
151 * @param modules List of modules to load
152 * @param entries Number of modules in the list
153 * @param required Are the modules required or optionnal
155 * If the modules are required, we will return with error after the
156 * first failed module load, otherwise we continue loading.
158 * @returns \c 0 on success
161 static int modprobe_lttng(struct kern_modules_param
*modules
,
162 int entries
, int required
)
165 struct kmod_ctx
*ctx
;
167 ret
= setup_kmod_ctx(&ctx
);
172 for (i
= 0; i
< entries
; i
++) {
173 struct kmod_module
*mod
= NULL
;
175 ret
= kmod_module_new_from_name(ctx
, modules
[i
].name
, &mod
);
177 PERROR("Failed to create kmod module for %s", modules
[i
].name
);
181 ret
= kmod_module_probe_insert_module(mod
, 0,
182 NULL
, NULL
, NULL
, NULL
);
183 if (ret
== -EEXIST
) {
184 DBG("Module %s is already loaded", modules
[i
].name
);
186 } else if (ret
< 0) {
188 ERR("Unable to load required module %s",
192 DBG("Unable to load optional module %s; continuing",
197 DBG("Modprobe successfully %s", modules
[i
].name
);
198 modules
[i
].loaded
= true;
201 kmod_module_unref(mod
);
212 * @brief Recursively unload modules.
214 * This function implements the same modules unloading behavior as
215 * 'modprobe -r' or rmmod, it will recursevily go trought the \p module
216 * dependencies and unload modules with a refcount of 0.
218 * @param mod The module to unload
220 * @returns \c 0 on success
223 static int rmmod_recurse(struct kmod_module
*mod
) {
225 struct kmod_list
*deps
, *itr
;
227 if (kmod_module_get_initstate(mod
) == KMOD_MODULE_BUILTIN
) {
228 DBG("Module %s is builtin", kmod_module_get_name(mod
));
232 ret
= kmod_module_remove_module(mod
, 0);
234 deps
= kmod_module_get_dependencies(mod
);
236 kmod_list_foreach(itr
, deps
) {
237 struct kmod_module
*dep
= kmod_module_get_module(itr
);
238 if (kmod_module_get_refcnt(dep
) == 0) {
239 DBG("Recursive remove module %s",
240 kmod_module_get_name(dep
));
243 kmod_module_unref(dep
);
245 kmod_module_unref_list(deps
);
252 * @brief Unloads the kernel modules in \p modules
254 * @param modules List of modules to unload
255 * @param entries Number of modules in the list
256 * @param required Are the modules required or optionnal
259 static void modprobe_remove_lttng(const struct kern_modules_param
*modules
,
260 int entries
, int required
)
263 struct kmod_ctx
*ctx
;
265 ret
= setup_kmod_ctx(&ctx
);
270 for (i
= entries
- 1; i
>= 0; i
--) {
271 struct kmod_module
*mod
= NULL
;
273 if (!modules
[i
].loaded
) {
277 ret
= kmod_module_new_from_name(ctx
, modules
[i
].name
, &mod
);
279 PERROR("Failed to create kmod module for %s", modules
[i
].name
);
283 ret
= rmmod_recurse(mod
);
284 if (ret
== -EEXIST
) {
285 DBG("Module %s is not in kernel.", modules
[i
].name
);
286 } else if (required
&& ret
< 0) {
287 ERR("Unable to remove module %s", modules
[i
].name
);
289 DBG("Modprobe removal successful %s",
293 kmod_module_unref(mod
);
302 #else /* HAVE_KMOD */
304 static int modprobe_lttng(struct kern_modules_param
*modules
,
305 int entries
, int required
)
310 for (i
= 0; i
< entries
; i
++) {
311 ret
= snprintf(modprobe
, sizeof(modprobe
),
312 "/sbin/modprobe %s%s",
313 required
? "" : "-q ",
316 PERROR("snprintf modprobe");
319 modprobe
[sizeof(modprobe
) - 1] = '\0';
320 ret
= system(modprobe
);
323 ERR("Unable to launch modprobe for required module %s",
327 DBG("Unable to launch modprobe for optional module %s; continuing",
331 } else if (WEXITSTATUS(ret
) != 0) {
333 ERR("Unable to load required module %s",
337 DBG("Unable to load optional module %s; continuing",
342 DBG("Modprobe successfully %s", modules
[i
].name
);
343 modules
[i
].loaded
= true;
351 static void modprobe_remove_lttng(const struct kern_modules_param
*modules
,
352 int entries
, int required
)
357 for (i
= entries
- 1; i
>= 0; i
--) {
358 if (!modules
[i
].loaded
) {
361 ret
= snprintf(modprobe
, sizeof(modprobe
),
362 "/sbin/modprobe -r -q %s",
365 PERROR("snprintf modprobe -r");
368 modprobe
[sizeof(modprobe
) - 1] = '\0';
369 ret
= system(modprobe
);
371 ERR("Unable to launch modprobe -r for module %s",
373 } else if (required
&& WEXITSTATUS(ret
) != 0) {
374 ERR("Unable to remove module %s",
377 DBG("Modprobe removal successful %s",
383 #endif /* HAVE_KMOD */
386 * Remove control kernel module(s) in reverse load order.
388 void modprobe_remove_lttng_control(void)
390 modprobe_remove_lttng(kern_modules_control_core
,
391 ARRAY_SIZE(kern_modules_control_core
),
395 static void free_probes(void)
402 for (i
= 0; i
< nr_probes
; ++i
) {
403 free(probes
[i
].name
);
411 * Remove data kernel modules in reverse load order.
413 void modprobe_remove_lttng_data(void)
418 modprobe_remove_lttng(probes
, nr_probes
, LTTNG_MOD_OPTIONAL
);
423 * Remove all kernel modules in reverse order.
425 void modprobe_remove_lttng_all(void)
427 modprobe_remove_lttng_data();
428 modprobe_remove_lttng_control();
432 * Load control kernel module(s).
434 int modprobe_lttng_control(void)
438 ret
= modprobe_lttng(kern_modules_control_core
,
439 ARRAY_SIZE(kern_modules_control_core
),
445 * Grow global list of probes (double capacity or set it to 1 if
446 * currently 0 and copy existing data).
448 static int grow_probes(void)
451 struct kern_modules_param
*tmp_probes
;
453 /* Initialize capacity to 1 if 0. */
454 if (probes_capacity
== 0) {
455 probes
= zmalloc(sizeof(*probes
));
457 PERROR("malloc probe list");
466 probes_capacity
*= 2;
468 tmp_probes
= zmalloc(sizeof(*tmp_probes
) * probes_capacity
);
470 PERROR("malloc probe list");
474 for (i
= 0; i
< nr_probes
; ++i
) {
475 /* Move name pointer. */
476 tmp_probes
[i
].name
= probes
[i
].name
;
479 /* Replace probes with larger copy. */
487 * Appends a comma-separated list of probes to the global list
490 static int append_list_to_probes(const char *list
)
494 char *tmp_list
, *cur_list
;
498 cur_list
= tmp_list
= strdup(list
);
500 PERROR("strdup temp list");
506 struct kern_modules_param
*cur_mod
;
508 next
= strtok(cur_list
, ",");
514 /* filter leading spaces */
515 while (*next
== ' ') {
519 if (probes_capacity
<= nr_probes
) {
526 /* Length 13 is "lttng-probe-" + \0 */
527 name_len
= strlen(next
) + 13;
529 cur_mod
= &probes
[nr_probes
];
530 cur_mod
->name
= zmalloc(name_len
);
531 if (!cur_mod
->name
) {
532 PERROR("malloc probe list");
537 ret
= snprintf(cur_mod
->name
, name_len
, "lttng-probe-%s", next
);
539 PERROR("snprintf modprobe name");
557 * Load data kernel module(s).
559 int modprobe_lttng_data(void)
565 * Base probes: either from command line option, environment
566 * variable or default list.
568 list
= config
.kmod_probes_list
.value
;
570 /* User-specified probes. */
571 ret
= append_list_to_probes(list
);
576 /* Default probes. */
577 int def_len
= ARRAY_SIZE(kern_modules_probes_default
);
579 probes
= zmalloc(sizeof(*probes
) * def_len
);
581 PERROR("malloc probe list");
585 nr_probes
= probes_capacity
= def_len
;
587 for (i
= 0; i
< def_len
; ++i
) {
588 char* name
= strdup(kern_modules_probes_default
[i
].name
);
591 PERROR("strdup probe item");
596 probes
[i
].name
= name
;
601 * Extra modules? Append them to current probes list.
603 list
= config
.kmod_extra_probes_list
.value
;
605 ret
= append_list_to_probes(list
);
612 * Load probes modules now.
614 ret
= modprobe_lttng(probes
, nr_probes
, LTTNG_MOD_OPTIONAL
);