2 * Copyright (C) 2011 - David Goulet <dgoulet@efficios.com>
3 * 2014 - Jan Glauber <jan.glauber@gmail.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 * @brief modprobe related functions.
32 #include <common/common.h>
33 #include <common/utils.h>
36 #include "kern-modules.h"
38 #define LTTNG_MOD_REQUIRED 1
39 #define LTTNG_MOD_OPTIONAL 0
41 /* LTTng kernel tracer mandatory core modules list */
42 struct kern_modules_param kern_modules_control_core
[] = {
43 { "lttng-ring-buffer-client-discard" },
44 { "lttng-ring-buffer-client-overwrite" },
45 { "lttng-ring-buffer-metadata-client" },
46 { "lttng-ring-buffer-client-mmap-discard" },
47 { "lttng-ring-buffer-client-mmap-overwrite" },
48 { "lttng-ring-buffer-metadata-mmap-client" },
51 /* LTTng kernel tracer probe modules list */
52 struct kern_modules_param kern_modules_probes_default
[] = {
53 { "lttng-probe-asoc" },
54 { "lttng-probe-block" },
55 { "lttng-probe-btrfs" },
56 { "lttng-probe-compaction" },
57 { "lttng-probe-ext3" },
58 { "lttng-probe-ext4" },
59 { "lttng-probe-gpio" },
60 { "lttng-probe-i2c" },
61 { "lttng-probe-irq" },
62 { "lttng-probe-jbd" },
63 { "lttng-probe-jbd2" },
64 { "lttng-probe-kmem" },
65 { "lttng-probe-kvm" },
66 { "lttng-probe-kvm-x86" },
67 { "lttng-probe-kvm-x86-mmu" },
68 { "lttng-probe-lock" },
69 { "lttng-probe-module" },
70 { "lttng-probe-napi" },
71 { "lttng-probe-net" },
72 { "lttng-probe-power" },
73 { "lttng-probe-printk" },
74 { "lttng-probe-random" },
75 { "lttng-probe-rcu" },
76 { "lttng-probe-regmap" },
77 { "lttng-probe-regulator" },
78 { "lttng-probe-rpm" },
79 { "lttng-probe-sched" },
80 { "lttng-probe-scsi" },
81 { "lttng-probe-signal" },
82 { "lttng-probe-skb" },
83 { "lttng-probe-sock" },
84 { "lttng-probe-statedump" },
85 { "lttng-probe-sunrpc" },
86 { "lttng-probe-timer" },
87 { "lttng-probe-udp" },
88 { "lttng-probe-vmscan" },
89 { "lttng-probe-v4l2" },
90 { "lttng-probe-workqueue" },
91 { "lttng-probe-writeback" },
92 { "lttng-probe-x86-irq-vectors" },
93 { "lttng-probe-x86-exceptions" },
96 /* dynamic probe modules list */
97 static struct kern_modules_param
*probes
;
99 static int probes_capacity
;
105 * @brief Logging function for libkmod integration.
107 static void log_kmod(void *data
, int priority
, const char *file
, int line
,
108 const char *fn
, const char *format
, va_list args
)
112 if (vasprintf(&str
, format
, args
) < 0) {
116 DBG("libkmod: %s", str
);
121 * @brief Setup the libkmod context.
123 * Create the context, add a custom logging function and preload the
124 * ressources for faster operation.
126 * @returns \c 0 on success
129 static int setup_kmod_ctx(struct kmod_ctx
**ctx
)
133 *ctx
= kmod_new(NULL
, NULL
);
135 PERROR("Unable to create kmod library context");
140 kmod_set_log_fn(*ctx
, log_kmod
, NULL
);
141 ret
= kmod_load_resources(*ctx
);
143 ERR("Failed to load kmod library resources");
152 * @brief Loads the kernel modules in \p modules
154 * @param modules List of modules to load
155 * @param entries Number of modules in the list
156 * @param required Are the modules required or optionnal
158 * If the modules are required, we will return with error after the
159 * first failed module load, otherwise we continue loading.
161 * @returns \c 0 on success
164 static int modprobe_lttng(struct kern_modules_param
*modules
,
165 int entries
, int required
)
168 struct kmod_ctx
*ctx
;
170 ret
= setup_kmod_ctx(&ctx
);
175 for (i
= 0; i
< entries
; i
++) {
176 struct kmod_module
*mod
= NULL
;
178 ret
= kmod_module_new_from_name(ctx
, modules
[i
].name
, &mod
);
180 PERROR("Failed to create kmod module for %s", modules
[i
].name
);
184 ret
= kmod_module_probe_insert_module(mod
, 0,
185 NULL
, NULL
, NULL
, NULL
);
186 if (ret
== -EEXIST
) {
187 DBG("Module %s is already loaded", modules
[i
].name
);
189 } else if (ret
< 0) {
191 ERR("Unable to load required module %s",
195 DBG("Unable to load optional module %s; continuing",
200 DBG("Modprobe successfully %s", modules
[i
].name
);
201 modules
[i
].loaded
= true;
204 kmod_module_unref(mod
);
215 * @brief Recursively unload modules.
217 * This function implements the same modules unloading behavior as
218 * 'modprobe -r' or rmmod, it will recursevily go trought the \p module
219 * dependencies and unload modules with a refcount of 0.
221 * @param mod The module to unload
223 * @returns \c 0 on success
226 static int rmmod_recurse(struct kmod_module
*mod
) {
228 struct kmod_list
*deps
, *itr
;
230 if (kmod_module_get_initstate(mod
) == KMOD_MODULE_BUILTIN
) {
231 DBG("Module %s is builtin", kmod_module_get_name(mod
));
235 ret
= kmod_module_remove_module(mod
, 0);
237 deps
= kmod_module_get_dependencies(mod
);
239 kmod_list_foreach(itr
, deps
) {
240 struct kmod_module
*dep
= kmod_module_get_module(itr
);
241 if (kmod_module_get_refcnt(dep
) == 0) {
242 DBG("Recursive remove module %s",
243 kmod_module_get_name(dep
));
246 kmod_module_unref(dep
);
248 kmod_module_unref_list(deps
);
255 * @brief Unloads the kernel modules in \p modules
257 * @param modules List of modules to unload
258 * @param entries Number of modules in the list
259 * @param required Are the modules required or optionnal
262 static void modprobe_remove_lttng(const struct kern_modules_param
*modules
,
263 int entries
, int required
)
266 struct kmod_ctx
*ctx
;
268 ret
= setup_kmod_ctx(&ctx
);
273 for (i
= entries
- 1; i
>= 0; i
--) {
274 struct kmod_module
*mod
= NULL
;
276 if (!modules
[i
].loaded
) {
280 ret
= kmod_module_new_from_name(ctx
, modules
[i
].name
, &mod
);
282 PERROR("Failed to create kmod module for %s", modules
[i
].name
);
286 ret
= rmmod_recurse(mod
);
287 if (ret
== -EEXIST
) {
288 DBG("Module %s is not in kernel.", modules
[i
].name
);
289 } else if (required
&& ret
< 0) {
290 ERR("Unable to remove module %s", modules
[i
].name
);
292 DBG("Modprobe removal successful %s",
296 kmod_module_unref(mod
);
305 #else /* HAVE_KMOD */
307 static int modprobe_lttng(struct kern_modules_param
*modules
,
308 int entries
, int required
)
313 for (i
= 0; i
< entries
; i
++) {
314 ret
= snprintf(modprobe
, sizeof(modprobe
),
315 "/sbin/modprobe %s%s",
316 required
? "" : "-q ",
319 PERROR("snprintf modprobe");
322 modprobe
[sizeof(modprobe
) - 1] = '\0';
323 ret
= system(modprobe
);
326 ERR("Unable to launch modprobe for required module %s",
330 DBG("Unable to launch modprobe for optional module %s; continuing",
334 } else if (WEXITSTATUS(ret
) != 0) {
336 ERR("Unable to load required module %s",
340 DBG("Unable to load optional module %s; continuing",
345 DBG("Modprobe successfully %s", modules
[i
].name
);
346 modules
[i
].loaded
= true;
354 static void modprobe_remove_lttng(const struct kern_modules_param
*modules
,
355 int entries
, int required
)
360 for (i
= entries
- 1; i
>= 0; i
--) {
361 if (!modules
[i
].loaded
) {
364 ret
= snprintf(modprobe
, sizeof(modprobe
),
365 "/sbin/modprobe -r -q %s",
368 PERROR("snprintf modprobe -r");
371 modprobe
[sizeof(modprobe
) - 1] = '\0';
372 ret
= system(modprobe
);
374 ERR("Unable to launch modprobe -r for module %s",
376 } else if (required
&& WEXITSTATUS(ret
) != 0) {
377 ERR("Unable to remove module %s",
380 DBG("Modprobe removal successful %s",
386 #endif /* HAVE_KMOD */
389 * Remove control kernel module(s) in reverse load order.
391 void modprobe_remove_lttng_control(void)
393 modprobe_remove_lttng(kern_modules_control_core
,
394 ARRAY_SIZE(kern_modules_control_core
),
398 static void free_probes(void)
405 for (i
= 0; i
< nr_probes
; ++i
) {
406 free(probes
[i
].name
);
414 * Remove data kernel modules in reverse load order.
416 void modprobe_remove_lttng_data(void)
421 modprobe_remove_lttng(probes
, nr_probes
, LTTNG_MOD_OPTIONAL
);
426 * Remove all kernel modules in reverse order.
428 void modprobe_remove_lttng_all(void)
430 modprobe_remove_lttng_data();
431 modprobe_remove_lttng_control();
435 * Load control kernel module(s).
437 int modprobe_lttng_control(void)
441 ret
= modprobe_lttng(kern_modules_control_core
,
442 ARRAY_SIZE(kern_modules_control_core
),
448 * Grow global list of probes (double capacity or set it to 1 if
449 * currently 0 and copy existing data).
451 static int grow_probes(void)
454 struct kern_modules_param
*tmp_probes
;
456 /* Initialize capacity to 1 if 0. */
457 if (probes_capacity
== 0) {
458 probes
= zmalloc(sizeof(*probes
));
460 PERROR("malloc probe list");
469 probes_capacity
*= 2;
471 tmp_probes
= zmalloc(sizeof(*tmp_probes
) * probes_capacity
);
473 PERROR("malloc probe list");
477 for (i
= 0; i
< nr_probes
; ++i
) {
478 /* Move name pointer. */
479 tmp_probes
[i
].name
= probes
[i
].name
;
482 /* Replace probes with larger copy. */
490 * Appends a comma-separated list of probes to the global list
493 static int append_list_to_probes(const char *list
)
497 char *tmp_list
, *cur_list
;
501 cur_list
= tmp_list
= strdup(list
);
503 PERROR("strdup temp list");
509 struct kern_modules_param
*cur_mod
;
511 next
= strtok(cur_list
, ",");
517 /* filter leading spaces */
518 while (*next
== ' ') {
522 if (probes_capacity
<= nr_probes
) {
529 /* Length 13 is "lttng-probe-" + \0 */
530 name_len
= strlen(next
) + 13;
532 cur_mod
= &probes
[nr_probes
];
533 cur_mod
->name
= zmalloc(name_len
);
534 if (!cur_mod
->name
) {
535 PERROR("malloc probe list");
540 ret
= snprintf(cur_mod
->name
, name_len
, "lttng-probe-%s", next
);
542 PERROR("snprintf modprobe name");
560 * Load data kernel module(s).
562 int modprobe_lttng_data(void)
568 * Base probes: either from command line option, environment
569 * variable or default list.
571 if (kmod_probes_list
) {
572 list
= kmod_probes_list
;
574 list
= utils_get_kmod_probes_list();
578 /* User-specified probes. */
579 ret
= append_list_to_probes(list
);
584 /* Default probes. */
585 int def_len
= ARRAY_SIZE(kern_modules_probes_default
);
587 probes
= zmalloc(sizeof(*probes
) * def_len
);
589 PERROR("malloc probe list");
593 nr_probes
= probes_capacity
= def_len
;
595 for (i
= 0; i
< def_len
; ++i
) {
596 char* name
= strdup(kern_modules_probes_default
[i
].name
);
599 PERROR("strdup probe item");
604 probes
[i
].name
= name
;
609 * Extra modules? Append them to current probes list.
611 if (kmod_extra_probes_list
) {
612 list
= kmod_extra_probes_list
;
614 list
= utils_get_extra_kmod_probes_list();
618 ret
= append_list_to_probes(list
);
625 * Load probes modules now.
627 ret
= modprobe_lttng(probes
, nr_probes
, LTTNG_MOD_OPTIONAL
);