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"
37 #include "lttng-sessiond.h"
39 #define LTTNG_MOD_REQUIRED 1
40 #define LTTNG_MOD_OPTIONAL 0
42 /* LTTng kernel tracer mandatory core modules list */
43 struct kern_modules_param kern_modules_control_core
[] = {
44 { "lttng-ring-buffer-client-discard" },
45 { "lttng-ring-buffer-client-overwrite" },
46 { "lttng-ring-buffer-metadata-client" },
47 { "lttng-ring-buffer-client-mmap-discard" },
48 { "lttng-ring-buffer-client-mmap-overwrite" },
49 { "lttng-ring-buffer-metadata-mmap-client" },
52 /* LTTng kernel tracer probe modules list */
53 struct kern_modules_param kern_modules_probes_default
[] = {
54 { "lttng-probe-asoc" },
55 { "lttng-probe-block" },
56 { "lttng-probe-btrfs" },
57 { "lttng-probe-compaction" },
58 { "lttng-probe-ext3" },
59 { "lttng-probe-ext4" },
60 { "lttng-probe-gpio" },
61 { "lttng-probe-i2c" },
62 { "lttng-probe-irq" },
63 { "lttng-probe-jbd" },
64 { "lttng-probe-jbd2" },
65 { "lttng-probe-kmem" },
66 { "lttng-probe-kvm" },
67 { "lttng-probe-kvm-x86" },
68 { "lttng-probe-kvm-x86-mmu" },
69 { "lttng-probe-lock" },
70 { "lttng-probe-module" },
71 { "lttng-probe-napi" },
72 { "lttng-probe-net" },
73 { "lttng-probe-power" },
74 { "lttng-probe-preemptirq" },
75 { "lttng-probe-printk" },
76 { "lttng-probe-random" },
77 { "lttng-probe-rcu" },
78 { "lttng-probe-regmap" },
79 { "lttng-probe-regulator" },
80 { "lttng-probe-rpm" },
81 { "lttng-probe-sched" },
82 { "lttng-probe-scsi" },
83 { "lttng-probe-signal" },
84 { "lttng-probe-skb" },
85 { "lttng-probe-sock" },
86 { "lttng-probe-statedump" },
87 { "lttng-probe-sunrpc" },
88 { "lttng-probe-timer" },
89 { "lttng-probe-udp" },
90 { "lttng-probe-vmscan" },
91 { "lttng-probe-v4l2" },
92 { "lttng-probe-workqueue" },
93 { "lttng-probe-writeback" },
94 { "lttng-probe-x86-irq-vectors" },
95 { "lttng-probe-x86-exceptions" },
98 /* dynamic probe modules list */
99 static struct kern_modules_param
*probes
;
100 static int nr_probes
;
101 static int probes_capacity
;
107 * @brief Logging function for libkmod integration.
109 static void log_kmod(void *data
, int priority
, const char *file
, int line
,
110 const char *fn
, const char *format
, va_list args
)
114 if (vasprintf(&str
, format
, args
) < 0) {
118 DBG("libkmod: %s", str
);
123 * @brief Setup the libkmod context.
125 * Create the context, add a custom logging function and preload the
126 * ressources for faster operation.
128 * @returns \c 0 on success
131 static int setup_kmod_ctx(struct kmod_ctx
**ctx
)
135 *ctx
= kmod_new(NULL
, NULL
);
137 PERROR("Unable to create kmod library context");
142 kmod_set_log_fn(*ctx
, log_kmod
, NULL
);
143 ret
= kmod_load_resources(*ctx
);
145 ERR("Failed to load kmod library resources");
154 * @brief Loads the kernel modules in \p modules
156 * @param modules List of modules to load
157 * @param entries Number of modules in the list
158 * @param required Are the modules required or optionnal
160 * If the modules are required, we will return with error after the
161 * first failed module load, otherwise we continue loading.
163 * @returns \c 0 on success
166 static int modprobe_lttng(struct kern_modules_param
*modules
,
167 int entries
, int required
)
170 struct kmod_ctx
*ctx
;
172 ret
= setup_kmod_ctx(&ctx
);
177 for (i
= 0; i
< entries
; i
++) {
178 struct kmod_module
*mod
= NULL
;
180 ret
= kmod_module_new_from_name(ctx
, modules
[i
].name
, &mod
);
182 PERROR("Failed to create kmod module for %s", modules
[i
].name
);
186 ret
= kmod_module_probe_insert_module(mod
, 0,
187 NULL
, NULL
, NULL
, NULL
);
188 if (ret
== -EEXIST
) {
189 DBG("Module %s is already loaded", modules
[i
].name
);
191 } else if (ret
< 0) {
193 ERR("Unable to load required module %s",
197 DBG("Unable to load optional module %s; continuing",
202 DBG("Modprobe successfully %s", modules
[i
].name
);
203 modules
[i
].loaded
= true;
206 kmod_module_unref(mod
);
217 * @brief Recursively unload modules.
219 * This function implements the same modules unloading behavior as
220 * 'modprobe -r' or rmmod, it will recursevily go trought the \p module
221 * dependencies and unload modules with a refcount of 0.
223 * @param mod The module to unload
225 * @returns \c 0 on success
228 static int rmmod_recurse(struct kmod_module
*mod
) {
230 struct kmod_list
*deps
, *itr
;
232 if (kmod_module_get_initstate(mod
) == KMOD_MODULE_BUILTIN
) {
233 DBG("Module %s is builtin", kmod_module_get_name(mod
));
237 ret
= kmod_module_remove_module(mod
, 0);
239 deps
= kmod_module_get_dependencies(mod
);
241 kmod_list_foreach(itr
, deps
) {
242 struct kmod_module
*dep
= kmod_module_get_module(itr
);
243 if (kmod_module_get_refcnt(dep
) == 0) {
244 DBG("Recursive remove module %s",
245 kmod_module_get_name(dep
));
248 kmod_module_unref(dep
);
250 kmod_module_unref_list(deps
);
257 * @brief Unloads the kernel modules in \p modules
259 * @param modules List of modules to unload
260 * @param entries Number of modules in the list
261 * @param required Are the modules required or optionnal
264 static void modprobe_remove_lttng(const struct kern_modules_param
*modules
,
265 int entries
, int required
)
268 struct kmod_ctx
*ctx
;
270 ret
= setup_kmod_ctx(&ctx
);
275 for (i
= entries
- 1; i
>= 0; i
--) {
276 struct kmod_module
*mod
= NULL
;
278 if (!modules
[i
].loaded
) {
282 ret
= kmod_module_new_from_name(ctx
, modules
[i
].name
, &mod
);
284 PERROR("Failed to create kmod module for %s", modules
[i
].name
);
288 ret
= rmmod_recurse(mod
);
289 if (ret
== -EEXIST
) {
290 DBG("Module %s is not in kernel.", modules
[i
].name
);
291 } else if (required
&& ret
< 0) {
292 ERR("Unable to remove module %s", modules
[i
].name
);
294 DBG("Modprobe removal successful %s",
298 kmod_module_unref(mod
);
307 #else /* HAVE_KMOD */
309 static int modprobe_lttng(struct kern_modules_param
*modules
,
310 int entries
, int required
)
315 for (i
= 0; i
< entries
; i
++) {
316 ret
= snprintf(modprobe
, sizeof(modprobe
),
317 "/sbin/modprobe %s%s",
318 required
? "" : "-q ",
321 PERROR("snprintf modprobe");
324 modprobe
[sizeof(modprobe
) - 1] = '\0';
325 ret
= system(modprobe
);
328 ERR("Unable to launch modprobe for required module %s",
332 DBG("Unable to launch modprobe for optional module %s; continuing",
336 } else if (WEXITSTATUS(ret
) != 0) {
338 ERR("Unable to load required module %s",
342 DBG("Unable to load optional module %s; continuing",
347 DBG("Modprobe successfully %s", modules
[i
].name
);
348 modules
[i
].loaded
= true;
356 static void modprobe_remove_lttng(const struct kern_modules_param
*modules
,
357 int entries
, int required
)
362 for (i
= entries
- 1; i
>= 0; i
--) {
363 if (!modules
[i
].loaded
) {
366 ret
= snprintf(modprobe
, sizeof(modprobe
),
367 "/sbin/modprobe -r -q %s",
370 PERROR("snprintf modprobe -r");
373 modprobe
[sizeof(modprobe
) - 1] = '\0';
374 ret
= system(modprobe
);
376 ERR("Unable to launch modprobe -r for module %s",
378 } else if (required
&& WEXITSTATUS(ret
) != 0) {
379 ERR("Unable to remove module %s",
382 DBG("Modprobe removal successful %s",
388 #endif /* HAVE_KMOD */
391 * Remove control kernel module(s) in reverse load order.
393 void modprobe_remove_lttng_control(void)
395 modprobe_remove_lttng(kern_modules_control_core
,
396 ARRAY_SIZE(kern_modules_control_core
),
400 static void free_probes(void)
407 for (i
= 0; i
< nr_probes
; ++i
) {
408 free(probes
[i
].name
);
416 * Remove data kernel modules in reverse load order.
418 void modprobe_remove_lttng_data(void)
423 modprobe_remove_lttng(probes
, nr_probes
, LTTNG_MOD_OPTIONAL
);
428 * Remove all kernel modules in reverse order.
430 void modprobe_remove_lttng_all(void)
432 modprobe_remove_lttng_data();
433 modprobe_remove_lttng_control();
437 * Load control kernel module(s).
439 int modprobe_lttng_control(void)
443 ret
= modprobe_lttng(kern_modules_control_core
,
444 ARRAY_SIZE(kern_modules_control_core
),
450 * Grow global list of probes (double capacity or set it to 1 if
451 * currently 0 and copy existing data).
453 static int grow_probes(void)
456 struct kern_modules_param
*tmp_probes
;
458 /* Initialize capacity to 1 if 0. */
459 if (probes_capacity
== 0) {
460 probes
= zmalloc(sizeof(*probes
));
462 PERROR("malloc probe list");
471 probes_capacity
*= 2;
473 tmp_probes
= zmalloc(sizeof(*tmp_probes
) * probes_capacity
);
475 PERROR("malloc probe list");
479 for (i
= 0; i
< nr_probes
; ++i
) {
480 /* Move name pointer. */
481 tmp_probes
[i
].name
= probes
[i
].name
;
484 /* Replace probes with larger copy. */
492 * Appends a comma-separated list of probes to the global list
495 static int append_list_to_probes(const char *list
)
499 char *tmp_list
, *cur_list
;
503 cur_list
= tmp_list
= strdup(list
);
505 PERROR("strdup temp list");
511 struct kern_modules_param
*cur_mod
;
513 next
= strtok(cur_list
, ",");
519 /* filter leading spaces */
520 while (*next
== ' ') {
524 if (probes_capacity
<= nr_probes
) {
531 /* Length 13 is "lttng-probe-" + \0 */
532 name_len
= strlen(next
) + 13;
534 cur_mod
= &probes
[nr_probes
];
535 cur_mod
->name
= zmalloc(name_len
);
536 if (!cur_mod
->name
) {
537 PERROR("malloc probe list");
542 ret
= snprintf(cur_mod
->name
, name_len
, "lttng-probe-%s", next
);
544 PERROR("snprintf modprobe name");
562 * Load data kernel module(s).
564 int modprobe_lttng_data(void)
570 * Base probes: either from command line option, environment
571 * variable or default list.
573 list
= config
.kmod_probes_list
.value
;
575 /* User-specified probes. */
576 ret
= append_list_to_probes(list
);
581 /* Default probes. */
582 int def_len
= ARRAY_SIZE(kern_modules_probes_default
);
584 probes
= zmalloc(sizeof(*probes
) * def_len
);
586 PERROR("malloc probe list");
590 nr_probes
= probes_capacity
= def_len
;
592 for (i
= 0; i
< def_len
; ++i
) {
593 char* name
= strdup(kern_modules_probes_default
[i
].name
);
596 PERROR("strdup probe item");
601 probes
[i
].name
= name
;
606 * Extra modules? Append them to current probes list.
608 list
= config
.kmod_extra_probes_list
.value
;
610 ret
= append_list_to_probes(list
);
617 * Load probes modules now.
619 ret
= modprobe_lttng(probes
, nr_probes
, LTTNG_MOD_OPTIONAL
);