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-irq" },
61 { "lttng-probe-jbd" },
62 { "lttng-probe-jbd2" },
63 { "lttng-probe-kmem" },
64 { "lttng-probe-kvm" },
65 { "lttng-probe-kvm-x86" },
66 { "lttng-probe-kvm-x86-mmu" },
67 { "lttng-probe-lock" },
68 { "lttng-probe-module" },
69 { "lttng-probe-napi" },
70 { "lttng-probe-net" },
71 { "lttng-probe-power" },
72 { "lttng-probe-printk" },
73 { "lttng-probe-random" },
74 { "lttng-probe-rcu" },
75 { "lttng-probe-regmap" },
76 { "lttng-probe-regulator" },
77 { "lttng-probe-rpm" },
78 { "lttng-probe-sched" },
79 { "lttng-probe-scsi" },
80 { "lttng-probe-signal" },
81 { "lttng-probe-skb" },
82 { "lttng-probe-sock" },
83 { "lttng-probe-statedump" },
84 { "lttng-probe-sunrpc" },
85 { "lttng-probe-timer" },
86 { "lttng-probe-udp" },
87 { "lttng-probe-vmscan" },
88 { "lttng-probe-v4l2" },
89 { "lttng-probe-workqueue" },
90 { "lttng-probe-writeback" },
91 { "lttng-probe-x86-irq-vectors" },
92 { "lttng-probe-x86-exceptions" },
95 /* dynamic probe modules list */
96 static struct kern_modules_param
*probes
;
98 static int probes_capacity
;
104 * @brief Logging function for libkmod integration.
106 static void log_kmod(void *data
, int priority
, const char *file
, int line
,
107 const char *fn
, const char *format
, va_list args
)
111 if (vasprintf(&str
, format
, args
) < 0) {
115 DBG("libkmod: %s", str
);
120 * @brief Setup the libkmod context.
122 * Create the context, add a custom logging function and preload the
123 * ressources for faster operation.
125 * @returns \c 0 on success
128 static int setup_kmod_ctx(struct kmod_ctx
**ctx
)
132 *ctx
= kmod_new(NULL
, NULL
);
134 PERROR("Unable to create kmod library context");
139 kmod_set_log_fn(*ctx
, log_kmod
, NULL
);
140 ret
= kmod_load_resources(*ctx
);
142 ERR("Failed to load kmod library resources");
151 * @brief Loads the kernel modules in \p modules
153 * @param modules List of modules to load
154 * @param entries Number of modules in the list
155 * @param required Are the modules required or optionnal
157 * If the modules are required, we will return with error after the
158 * first failed module load, otherwise we continue loading.
160 * @returns \c 0 on success
163 static int modprobe_lttng(struct kern_modules_param
*modules
,
164 int entries
, int required
)
167 struct kmod_ctx
*ctx
;
169 ret
= setup_kmod_ctx(&ctx
);
174 for (i
= 0; i
< entries
; i
++) {
175 struct kmod_module
*mod
= NULL
;
177 ret
= kmod_module_new_from_name(ctx
, modules
[i
].name
, &mod
);
179 PERROR("Failed to create kmod module for %s", modules
[i
].name
);
183 ret
= kmod_module_probe_insert_module(mod
, KMOD_PROBE_IGNORE_LOADED
,
184 NULL
, NULL
, NULL
, NULL
);
187 ERR("Unable to load required module %s",
191 DBG("Unable to load optional module %s; continuing",
196 DBG("Modprobe successfully %s", modules
[i
].name
);
199 kmod_module_unref(mod
);
210 * @brief Recursively unload modules.
212 * This function implements the same modules unloading behavior as
213 * 'modprobe -r' or rmmod, it will recursevily go trought the \p module
214 * dependencies and unload modules with a refcount of 0.
216 * @param mod The module to unload
218 * @returns \c 0 on success
221 static int rmmod_recurse(struct kmod_module
*mod
) {
223 struct kmod_list
*deps
, *itr
;
225 if (kmod_module_get_initstate(mod
) == KMOD_MODULE_BUILTIN
) {
226 DBG("Module %s is builtin", kmod_module_get_name(mod
));
230 ret
= kmod_module_remove_module(mod
, 0);
232 deps
= kmod_module_get_dependencies(mod
);
234 kmod_list_foreach(itr
, deps
) {
235 struct kmod_module
*dep
= kmod_module_get_module(itr
);
236 if (kmod_module_get_refcnt(dep
) == 0) {
237 DBG("Recursive remove module %s",
238 kmod_module_get_name(dep
));
241 kmod_module_unref(dep
);
243 kmod_module_unref_list(deps
);
250 * @brief Unloads the kernel modules in \p modules
252 * @param modules List of modules to unload
253 * @param entries Number of modules in the list
254 * @param required Are the modules required or optionnal
257 static void modprobe_remove_lttng(const struct kern_modules_param
*modules
,
258 int entries
, int required
)
261 struct kmod_ctx
*ctx
;
263 ret
= setup_kmod_ctx(&ctx
);
268 for (i
= entries
- 1; i
>= 0; i
--) {
269 struct kmod_module
*mod
= NULL
;
271 ret
= kmod_module_new_from_name(ctx
, modules
[i
].name
, &mod
);
273 PERROR("Failed to create kmod module for %s", modules
[i
].name
);
277 ret
= rmmod_recurse(mod
);
278 if (ret
== -EEXIST
) {
279 DBG("Module %s is not in kernel.", modules
[i
].name
);
280 } else if (required
&& ret
< 0) {
281 ERR("Unable to remove module %s", modules
[i
].name
);
283 DBG("Modprobe removal successful %s",
287 kmod_module_unref(mod
);
296 #else /* HAVE_KMOD */
298 static int modprobe_lttng(struct kern_modules_param
*modules
,
299 int entries
, int required
)
304 for (i
= 0; i
< entries
; i
++) {
305 ret
= snprintf(modprobe
, sizeof(modprobe
),
306 "/sbin/modprobe %s%s",
307 required
? "" : "-q ",
310 PERROR("snprintf modprobe");
313 modprobe
[sizeof(modprobe
) - 1] = '\0';
314 ret
= system(modprobe
);
317 ERR("Unable to launch modprobe for required module %s",
321 DBG("Unable to launch modprobe for optional module %s; continuing",
325 } else if (WEXITSTATUS(ret
) != 0) {
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
);
344 static void modprobe_remove_lttng(const struct kern_modules_param
*modules
,
345 int entries
, int required
)
350 for (i
= entries
- 1; i
>= 0; i
--) {
351 ret
= snprintf(modprobe
, sizeof(modprobe
),
352 "/sbin/modprobe -r -q %s",
355 PERROR("snprintf modprobe -r");
358 modprobe
[sizeof(modprobe
) - 1] = '\0';
359 ret
= system(modprobe
);
361 ERR("Unable to launch modprobe -r for module %s",
363 } else if (required
&& WEXITSTATUS(ret
) != 0) {
364 ERR("Unable to remove module %s",
367 DBG("Modprobe removal successful %s",
373 #endif /* HAVE_KMOD */
376 * Remove control kernel module(s) in reverse load order.
378 void modprobe_remove_lttng_control(void)
380 modprobe_remove_lttng(kern_modules_control_core
,
381 ARRAY_SIZE(kern_modules_control_core
),
385 static void free_probes(void)
392 for (i
= 0; i
< nr_probes
; ++i
) {
393 free(probes
[i
].name
);
401 * Remove data kernel modules in reverse load order.
403 void modprobe_remove_lttng_data(void)
408 modprobe_remove_lttng(probes
, nr_probes
, LTTNG_MOD_OPTIONAL
);
413 * Remove all kernel modules in reverse order.
415 void modprobe_remove_lttng_all(void)
417 modprobe_remove_lttng_data();
418 modprobe_remove_lttng_control();
422 * Load control kernel module(s).
424 int modprobe_lttng_control(void)
428 ret
= modprobe_lttng(kern_modules_control_core
,
429 ARRAY_SIZE(kern_modules_control_core
),
435 * Grow global list of probes (double capacity or set it to 1 if
436 * currently 0 and copy existing data).
438 static int grow_probes(void)
441 struct kern_modules_param
*tmp_probes
;
443 /* Initialize capacity to 1 if 0. */
444 if (probes_capacity
== 0) {
445 probes
= zmalloc(sizeof(*probes
));
447 PERROR("malloc probe list");
456 probes_capacity
*= 2;
458 tmp_probes
= zmalloc(sizeof(*tmp_probes
) * probes_capacity
);
460 PERROR("malloc probe list");
464 for (i
= 0; i
< nr_probes
; ++i
) {
465 /* Move name pointer. */
466 tmp_probes
[i
].name
= probes
[i
].name
;
469 /* Replace probes with larger copy. */
477 * Appends a comma-separated list of probes to the global list
480 static int append_list_to_probes(const char *list
)
484 char *tmp_list
, *cur_list
;
488 cur_list
= tmp_list
= strdup(list
);
490 PERROR("strdup temp list");
496 struct kern_modules_param
*cur_mod
;
498 next
= strtok(cur_list
, ",");
504 /* filter leading spaces */
505 while (*next
== ' ') {
509 if (probes_capacity
<= nr_probes
) {
516 /* Length 13 is "lttng-probe-" + \0 */
517 name_len
= strlen(next
) + 13;
519 cur_mod
= &probes
[nr_probes
];
520 cur_mod
->name
= zmalloc(name_len
);
521 if (!cur_mod
->name
) {
522 PERROR("malloc probe list");
527 ret
= snprintf(cur_mod
->name
, name_len
, "lttng-probe-%s", next
);
529 PERROR("snprintf modprobe name");
547 * Load data kernel module(s).
549 int modprobe_lttng_data(void)
555 * Base probes: either from command line option, environment
556 * variable or default list.
558 if (kmod_probes_list
) {
559 list
= kmod_probes_list
;
561 list
= utils_get_kmod_probes_list();
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 if (kmod_extra_probes_list
) {
599 list
= kmod_extra_probes_list
;
601 list
= utils_get_extra_kmod_probes_list();
605 ret
= append_list_to_probes(list
);
612 * Load probes modules now.
614 ret
= modprobe_lttng(probes
, nr_probes
, LTTNG_MOD_OPTIONAL
);