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
);
203 kmod_module_unref(mod
);
214 * @brief Recursively unload modules.
216 * This function implements the same modules unloading behavior as
217 * 'modprobe -r' or rmmod, it will recursevily go trought the \p module
218 * dependencies and unload modules with a refcount of 0.
220 * @param mod The module to unload
222 * @returns \c 0 on success
225 static int rmmod_recurse(struct kmod_module
*mod
) {
227 struct kmod_list
*deps
, *itr
;
229 if (kmod_module_get_initstate(mod
) == KMOD_MODULE_BUILTIN
) {
230 DBG("Module %s is builtin", kmod_module_get_name(mod
));
234 ret
= kmod_module_remove_module(mod
, 0);
236 deps
= kmod_module_get_dependencies(mod
);
238 kmod_list_foreach(itr
, deps
) {
239 struct kmod_module
*dep
= kmod_module_get_module(itr
);
240 if (kmod_module_get_refcnt(dep
) == 0) {
241 DBG("Recursive remove module %s",
242 kmod_module_get_name(dep
));
245 kmod_module_unref(dep
);
247 kmod_module_unref_list(deps
);
254 * @brief Unloads the kernel modules in \p modules
256 * @param modules List of modules to unload
257 * @param entries Number of modules in the list
258 * @param required Are the modules required or optionnal
261 static void modprobe_remove_lttng(const struct kern_modules_param
*modules
,
262 int entries
, int required
)
265 struct kmod_ctx
*ctx
;
267 ret
= setup_kmod_ctx(&ctx
);
272 for (i
= entries
- 1; i
>= 0; i
--) {
273 struct kmod_module
*mod
= NULL
;
275 ret
= kmod_module_new_from_name(ctx
, modules
[i
].name
, &mod
);
277 PERROR("Failed to create kmod module for %s", modules
[i
].name
);
281 ret
= rmmod_recurse(mod
);
282 if (ret
== -EEXIST
) {
283 DBG("Module %s is not in kernel.", modules
[i
].name
);
284 } else if (required
&& ret
< 0) {
285 ERR("Unable to remove module %s", modules
[i
].name
);
287 DBG("Modprobe removal successful %s",
291 kmod_module_unref(mod
);
300 #else /* HAVE_KMOD */
302 static int modprobe_lttng(struct kern_modules_param
*modules
,
303 int entries
, int required
)
308 for (i
= 0; i
< entries
; i
++) {
309 ret
= snprintf(modprobe
, sizeof(modprobe
),
310 "/sbin/modprobe %s%s",
311 required
? "" : "-q ",
314 PERROR("snprintf modprobe");
317 modprobe
[sizeof(modprobe
) - 1] = '\0';
318 ret
= system(modprobe
);
321 ERR("Unable to launch modprobe for required module %s",
325 DBG("Unable to launch modprobe for optional module %s; continuing",
329 } else if (WEXITSTATUS(ret
) != 0) {
331 ERR("Unable to load required module %s",
335 DBG("Unable to load optional module %s; continuing",
340 DBG("Modprobe successfully %s", modules
[i
].name
);
348 static void modprobe_remove_lttng(const struct kern_modules_param
*modules
,
349 int entries
, int required
)
354 for (i
= entries
- 1; i
>= 0; i
--) {
355 ret
= snprintf(modprobe
, sizeof(modprobe
),
356 "/sbin/modprobe -r -q %s",
359 PERROR("snprintf modprobe -r");
362 modprobe
[sizeof(modprobe
) - 1] = '\0';
363 ret
= system(modprobe
);
365 ERR("Unable to launch modprobe -r for module %s",
367 } else if (required
&& WEXITSTATUS(ret
) != 0) {
368 ERR("Unable to remove module %s",
371 DBG("Modprobe removal successful %s",
377 #endif /* HAVE_KMOD */
380 * Remove control kernel module(s) in reverse load order.
382 void modprobe_remove_lttng_control(void)
384 modprobe_remove_lttng(kern_modules_control_core
,
385 ARRAY_SIZE(kern_modules_control_core
),
389 static void free_probes(void)
396 for (i
= 0; i
< nr_probes
; ++i
) {
397 free(probes
[i
].name
);
405 * Remove data kernel modules in reverse load order.
407 void modprobe_remove_lttng_data(void)
412 modprobe_remove_lttng(probes
, nr_probes
, LTTNG_MOD_OPTIONAL
);
417 * Remove all kernel modules in reverse order.
419 void modprobe_remove_lttng_all(void)
421 modprobe_remove_lttng_data();
422 modprobe_remove_lttng_control();
426 * Load control kernel module(s).
428 int modprobe_lttng_control(void)
432 ret
= modprobe_lttng(kern_modules_control_core
,
433 ARRAY_SIZE(kern_modules_control_core
),
439 * Grow global list of probes (double capacity or set it to 1 if
440 * currently 0 and copy existing data).
442 static int grow_probes(void)
445 struct kern_modules_param
*tmp_probes
;
447 /* Initialize capacity to 1 if 0. */
448 if (probes_capacity
== 0) {
449 probes
= zmalloc(sizeof(*probes
));
451 PERROR("malloc probe list");
460 probes_capacity
*= 2;
462 tmp_probes
= zmalloc(sizeof(*tmp_probes
) * probes_capacity
);
464 PERROR("malloc probe list");
468 for (i
= 0; i
< nr_probes
; ++i
) {
469 /* Move name pointer. */
470 tmp_probes
[i
].name
= probes
[i
].name
;
473 /* Replace probes with larger copy. */
481 * Appends a comma-separated list of probes to the global list
484 static int append_list_to_probes(const char *list
)
488 char *tmp_list
, *cur_list
;
492 cur_list
= tmp_list
= strdup(list
);
494 PERROR("strdup temp list");
500 struct kern_modules_param
*cur_mod
;
502 next
= strtok(cur_list
, ",");
508 /* filter leading spaces */
509 while (*next
== ' ') {
513 if (probes_capacity
<= nr_probes
) {
520 /* Length 13 is "lttng-probe-" + \0 */
521 name_len
= strlen(next
) + 13;
523 cur_mod
= &probes
[nr_probes
];
524 cur_mod
->name
= zmalloc(name_len
);
525 if (!cur_mod
->name
) {
526 PERROR("malloc probe list");
531 ret
= snprintf(cur_mod
->name
, name_len
, "lttng-probe-%s", next
);
533 PERROR("snprintf modprobe name");
551 * Load data kernel module(s).
553 int modprobe_lttng_data(void)
559 * Base probes: either from command line option, environment
560 * variable or default list.
562 if (kmod_probes_list
) {
563 list
= kmod_probes_list
;
565 list
= utils_get_kmod_probes_list();
569 /* User-specified probes. */
570 ret
= append_list_to_probes(list
);
575 /* Default probes. */
576 int def_len
= ARRAY_SIZE(kern_modules_probes_default
);
578 probes
= zmalloc(sizeof(*probes
) * def_len
);
580 PERROR("malloc probe list");
584 nr_probes
= probes_capacity
= def_len
;
586 for (i
= 0; i
< def_len
; ++i
) {
587 char* name
= strdup(kern_modules_probes_default
[i
].name
);
590 PERROR("strdup probe item");
595 probes
[i
].name
= name
;
600 * Extra modules? Append them to current probes list.
602 if (kmod_extra_probes_list
) {
603 list
= kmod_extra_probes_list
;
605 list
= utils_get_extra_kmod_probes_list();
609 ret
= append_list_to_probes(list
);
616 * Load probes modules now.
618 ret
= modprobe_lttng(probes
, nr_probes
, LTTNG_MOD_OPTIONAL
);