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 struct kern_modules_param kern_modules_control_core
[] = {
34 { (char *) "lttng-ring-buffer-client-discard" },
35 { (char *) "lttng-ring-buffer-client-overwrite" },
36 { (char *) "lttng-ring-buffer-metadata-client" },
37 { (char *) "lttng-ring-buffer-client-mmap-discard" },
38 { (char *) "lttng-ring-buffer-client-mmap-overwrite" },
39 { (char *) "lttng-ring-buffer-metadata-mmap-client" },
42 /* LTTng kernel tracer probe modules list */
43 struct kern_modules_param kern_modules_probes_default
[] = {
44 { (char *) "lttng-probe-asoc" },
45 { (char *) "lttng-probe-block" },
46 { (char *) "lttng-probe-btrfs" },
47 { (char *) "lttng-probe-compaction" },
48 { (char *) "lttng-probe-ext3" },
49 { (char *) "lttng-probe-ext4" },
50 { (char *) "lttng-probe-gpio" },
51 { (char *) "lttng-probe-i2c" },
52 { (char *) "lttng-probe-irq" },
53 { (char *) "lttng-probe-jbd" },
54 { (char *) "lttng-probe-jbd2" },
55 { (char *) "lttng-probe-kmem" },
56 { (char *) "lttng-probe-kvm" },
57 { (char *) "lttng-probe-kvm-x86" },
58 { (char *) "lttng-probe-kvm-x86-mmu" },
59 { (char *) "lttng-probe-lock" },
60 { (char *) "lttng-probe-module" },
61 { (char *) "lttng-probe-napi" },
62 { (char *) "lttng-probe-net" },
63 { (char *) "lttng-probe-power" },
64 { (char *) "lttng-probe-preemptirq" },
65 { (char *) "lttng-probe-printk" },
66 { (char *) "lttng-probe-random" },
67 { (char *) "lttng-probe-rcu" },
68 { (char *) "lttng-probe-regmap" },
69 { (char *) "lttng-probe-regulator" },
70 { (char *) "lttng-probe-rpm" },
71 { (char *) "lttng-probe-sched" },
72 { (char *) "lttng-probe-scsi" },
73 { (char *) "lttng-probe-signal" },
74 { (char *) "lttng-probe-skb" },
75 { (char *) "lttng-probe-sock" },
76 { (char *) "lttng-probe-statedump" },
77 { (char *) "lttng-probe-sunrpc" },
78 { (char *) "lttng-probe-timer" },
79 { (char *) "lttng-probe-udp" },
80 { (char *) "lttng-probe-vmscan" },
81 { (char *) "lttng-probe-v4l2" },
82 { (char *) "lttng-probe-workqueue" },
83 { (char *) "lttng-probe-writeback" },
84 { (char *) "lttng-probe-x86-irq-vectors" },
85 { (char *) "lttng-probe-x86-exceptions" },
88 /* dynamic probe modules list */
89 static struct kern_modules_param
*probes
;
91 static int probes_capacity
;
97 * @brief Logging function for libkmod integration.
99 static void log_kmod(void *data
, int priority
, const char *file
, int line
,
100 const char *fn
, const char *format
, va_list args
)
104 if (vasprintf(&str
, format
, args
) < 0) {
108 DBG("libkmod: %s", str
);
113 * @brief Setup the libkmod context.
115 * Create the context, add a custom logging function and preload the
116 * ressources for faster operation.
118 * @returns \c 0 on success
121 static int setup_kmod_ctx(struct kmod_ctx
**ctx
)
125 *ctx
= kmod_new(NULL
, NULL
);
127 PERROR("Unable to create kmod library context");
132 kmod_set_log_fn(*ctx
, log_kmod
, NULL
);
133 ret
= kmod_load_resources(*ctx
);
135 ERR("Failed to load kmod library resources");
144 * @brief Loads the kernel modules in \p modules
146 * @param modules List of modules to load
147 * @param entries Number of modules in the list
148 * @param required Are the modules required or optionnal
150 * If the modules are required, we will return with error after the
151 * first failed module load, otherwise we continue loading.
153 * @returns \c 0 on success
156 static int modprobe_lttng(struct kern_modules_param
*modules
,
157 int entries
, int required
)
160 struct kmod_ctx
*ctx
;
162 ret
= setup_kmod_ctx(&ctx
);
167 for (i
= 0; i
< entries
; i
++) {
168 struct kmod_module
*mod
= NULL
;
170 ret
= kmod_module_new_from_name(ctx
, modules
[i
].name
, &mod
);
172 PERROR("Failed to create kmod module for %s", modules
[i
].name
);
176 ret
= kmod_module_probe_insert_module(mod
, 0,
177 NULL
, NULL
, NULL
, NULL
);
178 if (ret
== -EEXIST
) {
179 DBG("Module %s is already loaded", modules
[i
].name
);
181 } else if (ret
< 0) {
183 ERR("Unable to load required module %s",
187 DBG("Unable to load optional module %s; continuing",
192 DBG("Modprobe successfully %s", modules
[i
].name
);
193 modules
[i
].loaded
= true;
196 kmod_module_unref(mod
);
207 * @brief Recursively unload modules.
209 * This function implements the same modules unloading behavior as
210 * 'modprobe -r' or rmmod, it will recursevily go trought the \p module
211 * dependencies and unload modules with a refcount of 0.
213 * @param mod The module to unload
215 * @returns \c 0 on success
218 static int rmmod_recurse(struct kmod_module
*mod
) {
220 struct kmod_list
*deps
, *itr
;
222 if (kmod_module_get_initstate(mod
) == KMOD_MODULE_BUILTIN
) {
223 DBG("Module %s is builtin", kmod_module_get_name(mod
));
227 ret
= kmod_module_remove_module(mod
, 0);
229 deps
= kmod_module_get_dependencies(mod
);
231 kmod_list_foreach(itr
, deps
) {
232 struct kmod_module
*dep
= kmod_module_get_module(itr
);
233 if (kmod_module_get_refcnt(dep
) == 0) {
234 DBG("Recursive remove module %s",
235 kmod_module_get_name(dep
));
238 kmod_module_unref(dep
);
240 kmod_module_unref_list(deps
);
247 * @brief Unloads the kernel modules in \p modules
249 * @param modules List of modules to unload
250 * @param entries Number of modules in the list
251 * @param required Are the modules required or optionnal
254 static void modprobe_remove_lttng(const struct kern_modules_param
*modules
,
255 int entries
, int required
)
258 struct kmod_ctx
*ctx
;
260 ret
= setup_kmod_ctx(&ctx
);
265 for (i
= entries
- 1; i
>= 0; i
--) {
266 struct kmod_module
*mod
= NULL
;
268 if (!modules
[i
].loaded
) {
272 ret
= kmod_module_new_from_name(ctx
, modules
[i
].name
, &mod
);
274 PERROR("Failed to create kmod module for %s", modules
[i
].name
);
278 ret
= rmmod_recurse(mod
);
279 if (ret
== -EEXIST
) {
280 DBG("Module %s is not in kernel.", modules
[i
].name
);
281 } else if (required
&& ret
< 0) {
282 ERR("Unable to remove module %s", modules
[i
].name
);
284 DBG("Modprobe removal successful %s",
288 kmod_module_unref(mod
);
297 #else /* HAVE_KMOD */
299 static int modprobe_lttng(struct kern_modules_param
*modules
,
300 int entries
, int required
)
305 for (i
= 0; i
< entries
; i
++) {
306 ret
= snprintf(modprobe
, sizeof(modprobe
),
307 "/sbin/modprobe %s%s",
308 required
? "" : "-q ",
311 PERROR("snprintf modprobe");
314 modprobe
[sizeof(modprobe
) - 1] = '\0';
315 ret
= system(modprobe
);
318 ERR("Unable to launch modprobe for required module %s",
322 DBG("Unable to launch modprobe for optional module %s; continuing",
326 } else if (WEXITSTATUS(ret
) != 0) {
328 ERR("Unable to load required module %s",
332 DBG("Unable to load optional module %s; continuing",
337 DBG("Modprobe successfully %s", modules
[i
].name
);
338 modules
[i
].loaded
= true;
346 static void modprobe_remove_lttng(const struct kern_modules_param
*modules
,
347 int entries
, int required
)
352 for (i
= entries
- 1; i
>= 0; i
--) {
353 if (!modules
[i
].loaded
) {
356 ret
= snprintf(modprobe
, sizeof(modprobe
),
357 "/sbin/modprobe -r -q %s",
360 PERROR("snprintf modprobe -r");
363 modprobe
[sizeof(modprobe
) - 1] = '\0';
364 ret
= system(modprobe
);
366 ERR("Unable to launch modprobe -r for module %s",
368 } else if (required
&& WEXITSTATUS(ret
) != 0) {
369 ERR("Unable to remove module %s",
372 DBG("Modprobe removal successful %s",
378 #endif /* HAVE_KMOD */
381 * Remove control kernel module(s) in reverse load order.
383 void modprobe_remove_lttng_control(void)
385 modprobe_remove_lttng(kern_modules_control_core
,
386 ARRAY_SIZE(kern_modules_control_core
),
390 static void free_probes(void)
397 for (i
= 0; i
< nr_probes
; ++i
) {
398 free(probes
[i
].name
);
406 * Remove data kernel modules in reverse load order.
408 void modprobe_remove_lttng_data(void)
413 modprobe_remove_lttng(probes
, nr_probes
, LTTNG_MOD_OPTIONAL
);
418 * Remove all kernel modules in reverse order.
420 void modprobe_remove_lttng_all(void)
422 modprobe_remove_lttng_data();
423 modprobe_remove_lttng_control();
427 * Load control kernel module(s).
429 int modprobe_lttng_control(void)
433 ret
= modprobe_lttng(kern_modules_control_core
,
434 ARRAY_SIZE(kern_modules_control_core
),
440 * Grow global list of probes (double capacity or set it to 1 if
441 * currently 0 and copy existing data).
443 static int grow_probes(void)
446 struct kern_modules_param
*tmp_probes
;
448 /* Initialize capacity to 1 if 0. */
449 if (probes_capacity
== 0) {
450 probes
= zmalloc(sizeof(*probes
));
452 PERROR("malloc probe list");
461 probes_capacity
*= 2;
463 tmp_probes
= zmalloc(sizeof(*tmp_probes
) * probes_capacity
);
465 PERROR("malloc probe list");
469 for (i
= 0; i
< nr_probes
; ++i
) {
470 /* Move name pointer. */
471 tmp_probes
[i
].name
= probes
[i
].name
;
474 /* Replace probes with larger copy. */
482 * Appends a comma-separated list of probes to the global list
485 static int append_list_to_probes(const char *list
)
489 char *tmp_list
, *cur_list
;
493 cur_list
= tmp_list
= strdup(list
);
495 PERROR("strdup temp list");
501 struct kern_modules_param
*cur_mod
;
503 next
= strtok(cur_list
, ",");
509 /* filter leading spaces */
510 while (*next
== ' ') {
514 if (probes_capacity
<= nr_probes
) {
521 /* Length 13 is "lttng-probe-" + \0 */
522 name_len
= strlen(next
) + 13;
524 cur_mod
= &probes
[nr_probes
];
525 cur_mod
->name
= zmalloc(name_len
);
526 if (!cur_mod
->name
) {
527 PERROR("malloc probe list");
532 ret
= snprintf(cur_mod
->name
, name_len
, "lttng-probe-%s", next
);
534 PERROR("snprintf modprobe name");
552 * Load data kernel module(s).
554 int modprobe_lttng_data(void)
560 * Base probes: either from command line option, environment
561 * variable or default list.
563 list
= config
.kmod_probes_list
.value
;
565 /* User-specified probes. */
566 ret
= append_list_to_probes(list
);
571 /* Default probes. */
572 int def_len
= ARRAY_SIZE(kern_modules_probes_default
);
574 probes
= zmalloc(sizeof(*probes
) * def_len
);
576 PERROR("malloc probe list");
580 nr_probes
= probes_capacity
= def_len
;
582 for (i
= 0; i
< def_len
; ++i
) {
583 char* name
= strdup(kern_modules_probes_default
[i
].name
);
586 PERROR("strdup probe item");
591 probes
[i
].name
= name
;
596 * Extra modules? Append them to current probes list.
598 list
= config
.kmod_extra_probes_list
.value
;
600 ret
= append_list_to_probes(list
);
607 * Load probes modules now.
609 ret
= modprobe_lttng(probes
, nr_probes
, LTTNG_MOD_OPTIONAL
);