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-printk" },
75 { "lttng-probe-random" },
76 { "lttng-probe-rcu" },
77 { "lttng-probe-regmap" },
78 { "lttng-probe-regulator" },
79 { "lttng-probe-rpm" },
80 { "lttng-probe-sched" },
81 { "lttng-probe-scsi" },
82 { "lttng-probe-signal" },
83 { "lttng-probe-skb" },
84 { "lttng-probe-sock" },
85 { "lttng-probe-statedump" },
86 { "lttng-probe-sunrpc" },
87 { "lttng-probe-timer" },
88 { "lttng-probe-udp" },
89 { "lttng-probe-vmscan" },
90 { "lttng-probe-v4l2" },
91 { "lttng-probe-workqueue" },
92 { "lttng-probe-writeback" },
93 { "lttng-probe-x86-irq-vectors" },
94 { "lttng-probe-x86-exceptions" },
97 /* dynamic probe modules list */
98 static struct kern_modules_param
*probes
;
100 static int probes_capacity
;
106 * @brief Logging function for libkmod integration.
108 static void log_kmod(void *data
, int priority
, const char *file
, int line
,
109 const char *fn
, const char *format
, va_list args
)
113 if (vasprintf(&str
, format
, args
) < 0) {
117 DBG("libkmod: %s", str
);
122 * @brief Setup the libkmod context.
124 * Create the context, add a custom logging function and preload the
125 * ressources for faster operation.
127 * @returns \c 0 on success
130 static int setup_kmod_ctx(struct kmod_ctx
**ctx
)
134 *ctx
= kmod_new(NULL
, NULL
);
136 PERROR("Unable to create kmod library context");
141 kmod_set_log_fn(*ctx
, log_kmod
, NULL
);
142 ret
= kmod_load_resources(*ctx
);
144 ERR("Failed to load kmod library resources");
153 * @brief Loads the kernel modules in \p modules
155 * @param modules List of modules to load
156 * @param entries Number of modules in the list
157 * @param required Are the modules required or optionnal
159 * If the modules are required, we will return with error after the
160 * first failed module load, otherwise we continue loading.
162 * @returns \c 0 on success
165 static int modprobe_lttng(struct kern_modules_param
*modules
,
166 int entries
, int required
)
169 struct kmod_ctx
*ctx
;
171 ret
= setup_kmod_ctx(&ctx
);
176 for (i
= 0; i
< entries
; i
++) {
177 struct kmod_module
*mod
= NULL
;
179 ret
= kmod_module_new_from_name(ctx
, modules
[i
].name
, &mod
);
181 PERROR("Failed to create kmod module for %s", modules
[i
].name
);
185 ret
= kmod_module_probe_insert_module(mod
, 0,
186 NULL
, NULL
, NULL
, NULL
);
187 if (ret
== -EEXIST
) {
188 DBG("Module %s is already loaded", modules
[i
].name
);
190 } else if (ret
< 0) {
192 ERR("Unable to load required module %s",
196 DBG("Unable to load optional module %s; continuing",
201 DBG("Modprobe successfully %s", modules
[i
].name
);
202 modules
[i
].loaded
= true;
205 kmod_module_unref(mod
);
216 * @brief Recursively unload modules.
218 * This function implements the same modules unloading behavior as
219 * 'modprobe -r' or rmmod, it will recursevily go trought the \p module
220 * dependencies and unload modules with a refcount of 0.
222 * @param mod The module to unload
224 * @returns \c 0 on success
227 static int rmmod_recurse(struct kmod_module
*mod
) {
229 struct kmod_list
*deps
, *itr
;
231 if (kmod_module_get_initstate(mod
) == KMOD_MODULE_BUILTIN
) {
232 DBG("Module %s is builtin", kmod_module_get_name(mod
));
236 ret
= kmod_module_remove_module(mod
, 0);
238 deps
= kmod_module_get_dependencies(mod
);
240 kmod_list_foreach(itr
, deps
) {
241 struct kmod_module
*dep
= kmod_module_get_module(itr
);
242 if (kmod_module_get_refcnt(dep
) == 0) {
243 DBG("Recursive remove module %s",
244 kmod_module_get_name(dep
));
247 kmod_module_unref(dep
);
249 kmod_module_unref_list(deps
);
256 * @brief Unloads the kernel modules in \p modules
258 * @param modules List of modules to unload
259 * @param entries Number of modules in the list
260 * @param required Are the modules required or optionnal
263 static void modprobe_remove_lttng(const struct kern_modules_param
*modules
,
264 int entries
, int required
)
267 struct kmod_ctx
*ctx
;
269 ret
= setup_kmod_ctx(&ctx
);
274 for (i
= entries
- 1; i
>= 0; i
--) {
275 struct kmod_module
*mod
= NULL
;
277 if (!modules
[i
].loaded
) {
281 ret
= kmod_module_new_from_name(ctx
, modules
[i
].name
, &mod
);
283 PERROR("Failed to create kmod module for %s", modules
[i
].name
);
287 ret
= rmmod_recurse(mod
);
288 if (ret
== -EEXIST
) {
289 DBG("Module %s is not in kernel.", modules
[i
].name
);
290 } else if (required
&& ret
< 0) {
291 ERR("Unable to remove module %s", modules
[i
].name
);
293 DBG("Modprobe removal successful %s",
297 kmod_module_unref(mod
);
306 #else /* HAVE_KMOD */
308 static int modprobe_lttng(struct kern_modules_param
*modules
,
309 int entries
, int required
)
314 for (i
= 0; i
< entries
; i
++) {
315 ret
= snprintf(modprobe
, sizeof(modprobe
),
316 "/sbin/modprobe %s%s",
317 required
? "" : "-q ",
320 PERROR("snprintf modprobe");
323 modprobe
[sizeof(modprobe
) - 1] = '\0';
324 ret
= system(modprobe
);
327 ERR("Unable to launch modprobe for required module %s",
331 DBG("Unable to launch modprobe for optional module %s; continuing",
335 } else if (WEXITSTATUS(ret
) != 0) {
337 ERR("Unable to load required module %s",
341 DBG("Unable to load optional module %s; continuing",
346 DBG("Modprobe successfully %s", modules
[i
].name
);
347 modules
[i
].loaded
= true;
355 static void modprobe_remove_lttng(const struct kern_modules_param
*modules
,
356 int entries
, int required
)
361 for (i
= entries
- 1; i
>= 0; i
--) {
362 if (!modules
[i
].loaded
) {
365 ret
= snprintf(modprobe
, sizeof(modprobe
),
366 "/sbin/modprobe -r -q %s",
369 PERROR("snprintf modprobe -r");
372 modprobe
[sizeof(modprobe
) - 1] = '\0';
373 ret
= system(modprobe
);
375 ERR("Unable to launch modprobe -r for module %s",
377 } else if (required
&& WEXITSTATUS(ret
) != 0) {
378 ERR("Unable to remove module %s",
381 DBG("Modprobe removal successful %s",
387 #endif /* HAVE_KMOD */
390 * Remove control kernel module(s) in reverse load order.
392 void modprobe_remove_lttng_control(void)
394 modprobe_remove_lttng(kern_modules_control_core
,
395 ARRAY_SIZE(kern_modules_control_core
),
399 static void free_probes(void)
406 for (i
= 0; i
< nr_probes
; ++i
) {
407 free(probes
[i
].name
);
415 * Remove data kernel modules in reverse load order.
417 void modprobe_remove_lttng_data(void)
422 modprobe_remove_lttng(probes
, nr_probes
, LTTNG_MOD_OPTIONAL
);
427 * Remove all kernel modules in reverse order.
429 void modprobe_remove_lttng_all(void)
431 modprobe_remove_lttng_data();
432 modprobe_remove_lttng_control();
436 * Load control kernel module(s).
438 int modprobe_lttng_control(void)
442 ret
= modprobe_lttng(kern_modules_control_core
,
443 ARRAY_SIZE(kern_modules_control_core
),
449 * Grow global list of probes (double capacity or set it to 1 if
450 * currently 0 and copy existing data).
452 static int grow_probes(void)
455 struct kern_modules_param
*tmp_probes
;
457 /* Initialize capacity to 1 if 0. */
458 if (probes_capacity
== 0) {
459 probes
= zmalloc(sizeof(*probes
));
461 PERROR("malloc probe list");
470 probes_capacity
*= 2;
472 tmp_probes
= zmalloc(sizeof(*tmp_probes
) * probes_capacity
);
474 PERROR("malloc probe list");
478 for (i
= 0; i
< nr_probes
; ++i
) {
479 /* Move name pointer. */
480 tmp_probes
[i
].name
= probes
[i
].name
;
483 /* Replace probes with larger copy. */
491 * Appends a comma-separated list of probes to the global list
494 static int append_list_to_probes(const char *list
)
498 char *tmp_list
, *cur_list
;
502 cur_list
= tmp_list
= strdup(list
);
504 PERROR("strdup temp list");
510 struct kern_modules_param
*cur_mod
;
512 next
= strtok(cur_list
, ",");
518 /* filter leading spaces */
519 while (*next
== ' ') {
523 if (probes_capacity
<= nr_probes
) {
530 /* Length 13 is "lttng-probe-" + \0 */
531 name_len
= strlen(next
) + 13;
533 cur_mod
= &probes
[nr_probes
];
534 cur_mod
->name
= zmalloc(name_len
);
535 if (!cur_mod
->name
) {
536 PERROR("malloc probe list");
541 ret
= snprintf(cur_mod
->name
, name_len
, "lttng-probe-%s", next
);
543 PERROR("snprintf modprobe name");
561 * Load data kernel module(s).
563 int modprobe_lttng_data(void)
569 * Base probes: either from command line option, environment
570 * variable or default list.
572 list
= config
.kmod_probes_list
.value
;
574 /* User-specified probes. */
575 ret
= append_list_to_probes(list
);
580 /* Default probes. */
581 int def_len
= ARRAY_SIZE(kern_modules_probes_default
);
583 probes
= zmalloc(sizeof(*probes
) * def_len
);
585 PERROR("malloc probe list");
589 nr_probes
= probes_capacity
= def_len
;
591 for (i
= 0; i
< def_len
; ++i
) {
592 char* name
= strdup(kern_modules_probes_default
[i
].name
);
595 PERROR("strdup probe item");
600 probes
[i
].name
= name
;
605 * Extra modules? Append them to current probes list.
607 list
= config
.kmod_extra_probes_list
.value
;
609 ret
= append_list_to_probes(list
);
616 * Load probes modules now.
618 ret
= modprobe_lttng(probes
, nr_probes
, LTTNG_MOD_OPTIONAL
);