sessiond: kernel: make modules required/optional property per-module
[lttng-tools.git] / src / bin / lttng-sessiond / modprobe.c
1 /*
2 * Copyright (C) 2011 David Goulet <dgoulet@efficios.com>
3 * Copyright (C) 2014 Jan Glauber <jan.glauber@gmail.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 */
8
9 /**
10 * @file modprobe.c
11 *
12 * @brief modprobe related functions.
13 *
14 */
15
16 #define _LGPL_SOURCE
17 #include <assert.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <sys/wait.h>
21
22 #include <common/common.h>
23 #include <common/utils.h>
24
25 #include "modprobe.h"
26 #include "kern-modules.h"
27 #include "lttng-sessiond.h"
28
29 /* LTTng kernel tracer mandatory core modules list */
30 struct kern_modules_param kern_modules_control_core[] = {
31 {
32 .name = (char *) "lttng-ring-buffer-client-discard",
33 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED,
34 },
35 {
36 .name = (char *) "lttng-ring-buffer-client-overwrite",
37 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED,
38 },
39 {
40 .name = (char *) "lttng-ring-buffer-metadata-client",
41 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED,
42 },
43 {
44 .name = (char *) "lttng-ring-buffer-client-mmap-discard",
45 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED,
46 },
47 {
48 .name = (char *) "lttng-ring-buffer-client-mmap-overwrite",
49 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED,
50 },
51 {
52 .name = (char *) "lttng-ring-buffer-metadata-mmap-client",
53 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED,
54 },
55 };
56
57 /* LTTng kerneltracer probe modules list */
58 struct kern_modules_param kern_modules_probes_default[] = {
59 {
60 .name = (char *) "lttng-probe-asoc",
61 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
62 },
63 {
64 .name = (char *) "lttng-probe-block",
65 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
66 },
67 {
68 .name = (char *) "lttng-probe-btrfs",
69 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
70 },
71 {
72 .name = (char *) "lttng-probe-compaction",
73 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
74 },
75 {
76 .name = (char *) "lttng-probe-ext3",
77 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
78 },
79 {
80 .name = (char *) "lttng-probe-ext4",
81 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
82 },
83 {
84 .name = (char *) "lttng-probe-gpio",
85 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
86 },
87 {
88 .name = (char *) "lttng-probe-i2c",
89 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
90 },
91 {
92 .name = (char *) "lttng-probe-irq",
93 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
94 },
95 {
96 .name = (char *) "lttng-probe-jbd",
97 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
98 },
99 {
100 .name = (char *) "lttng-probe-jbd2",
101 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
102 },
103 {
104 .name = (char *) "lttng-probe-kmem",
105 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
106 },
107 {
108 .name = (char *) "lttng-probe-kvm",
109 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
110 },
111 {
112 .name = (char *) "lttng-probe-kvm-x86",
113 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
114 },
115 {
116 .name = (char *) "lttng-probe-kvm-x86-mmu",
117 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
118 },
119 {
120 .name = (char *) "lttng-probe-lock",
121 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
122 },
123 {
124 .name = (char *) "lttng-probe-module",
125 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
126 },
127 {
128 .name = (char *) "lttng-probe-napi",
129 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
130 },
131 {
132 .name = (char *) "lttng-probe-net",
133 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
134 },
135 {
136 .name = (char *) "lttng-probe-power",
137 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
138 },
139 {
140 .name = (char *) "lttng-probe-preemptirq",
141 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
142 },
143 {
144 .name = (char *) "lttng-probe-printk",
145 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
146 },
147 {
148 .name = (char *) "lttng-probe-random",
149 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
150 },
151 {
152 .name = (char *) "lttng-probe-rcu",
153 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
154 },
155 {
156 .name = (char *) "lttng-probe-regmap",
157 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
158 },
159 {
160 .name = (char *) "lttng-probe-regulator",
161 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
162 },
163 {
164 .name = (char *) "lttng-probe-rpm",
165 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
166 },
167 {
168 .name = (char *) "lttng-probe-sched",
169 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
170 },
171 {
172 .name = (char *) "lttng-probe-scsi",
173 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
174 },
175 {
176 .name = (char *) "lttng-probe-signal",
177 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
178 },
179 {
180 .name = (char *) "lttng-probe-skb",
181 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
182 },
183 {
184 .name = (char *) "lttng-probe-sock",
185 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
186 },
187 {
188 .name = (char *) "lttng-probe-statedump",
189 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
190 },
191 {
192 .name = (char *) "lttng-probe-sunrpc",
193 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
194 },
195 {
196 .name = (char *) "lttng-probe-timer",
197 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
198 },
199 {
200 .name = (char *) "lttng-probe-udp",
201 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
202 },
203 {
204 .name = (char *) "lttng-probe-vmscan",
205 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
206 },
207 {
208 .name = (char *) "lttng-probe-v4l2",
209 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
210 },
211 {
212 .name = (char *) "lttng-probe-workqueue",
213 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
214 },
215 {
216 .name = (char *) "lttng-probe-writeback",
217 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
218 },
219 {
220 .name = (char *) "lttng-probe-x86-irq-vectors",
221 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
222 },
223 {
224 .name = (char *) "lttng-probe-x86-exceptions",
225 .load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL,
226 },
227 };
228
229 /* dynamic probe modules list */
230 static struct kern_modules_param *probes;
231 static int nr_probes;
232 static int probes_capacity;
233
234 #if HAVE_KMOD
235 #include <libkmod.h>
236
237 /**
238 * @brief Logging function for libkmod integration.
239 */
240 static void log_kmod(void *data, int priority, const char *file, int line,
241 const char *fn, const char *format, va_list args)
242 {
243 char *str;
244
245 if (vasprintf(&str, format, args) < 0) {
246 return;
247 }
248
249 DBG("libkmod: %s", str);
250 free(str);
251 }
252
253 /**
254 * @brief Setup the libkmod context.
255 *
256 * Create the context, add a custom logging function and preload the
257 * ressources for faster operation.
258 *
259 * @returns \c 0 on success
260 * \c < 0 on error
261 */
262 static int setup_kmod_ctx(struct kmod_ctx **ctx)
263 {
264 int ret = 0;
265
266 *ctx = kmod_new(NULL, NULL);
267 if (!ctx) {
268 PERROR("Unable to create kmod library context");
269 ret = -ENOMEM;
270 goto error;
271 }
272
273 kmod_set_log_fn(*ctx, log_kmod, NULL);
274 ret = kmod_load_resources(*ctx);
275 if (ret < 0) {
276 ERR("Failed to load kmod library resources");
277 goto error;
278 }
279
280 error:
281 return ret;
282 }
283
284 /**
285 * @brief Loads the kernel modules in \p modules
286 *
287 * @param modules List of modules to load
288 * @param entries Number of modules in the list
289 *
290 * If the modules are required, we will return with error after the
291 * first failed module load, otherwise we continue loading.
292 *
293 * @returns \c 0 on success
294 * \c < 0 on error
295 */
296 static int modprobe_lttng(struct kern_modules_param *modules,
297 int entries)
298 {
299 int ret = 0, i;
300 struct kmod_ctx *ctx;
301
302 ret = setup_kmod_ctx(&ctx);
303 if (ret < 0) {
304 goto error;
305 }
306
307 for (i = 0; i < entries; i++) {
308 struct kmod_module *mod = NULL;
309
310 ret = kmod_module_new_from_name(ctx, modules[i].name, &mod);
311 if (ret < 0) {
312 PERROR("Failed to create kmod module for %s", modules[i].name);
313 goto error;
314 }
315
316 ret = kmod_module_probe_insert_module(mod, 0,
317 NULL, NULL, NULL, NULL);
318 if (ret == -EEXIST) {
319 DBG("Module %s is already loaded", modules[i].name);
320 ret = 0;
321 } else if (ret < 0) {
322 if (modules[i].load_policy == KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED) {
323 ERR("Unable to load required module %s",
324 modules[i].name);
325 goto error;
326 } else {
327 DBG("Unable to load optional module %s; continuing",
328 modules[i].name);
329 ret = 0;
330 }
331 } else {
332 DBG("Modprobe successfully %s", modules[i].name);
333 modules[i].loaded = true;
334 }
335
336 kmod_module_unref(mod);
337 }
338
339 error:
340 if (ctx) {
341 kmod_unref(ctx);
342 }
343 return ret;
344 }
345
346 /**
347 * @brief Recursively unload modules.
348 *
349 * This function implements the same modules unloading behavior as
350 * 'modprobe -r' or rmmod, it will recursevily go trought the \p module
351 * dependencies and unload modules with a refcount of 0.
352 *
353 * @param mod The module to unload
354 *
355 * @returns \c 0 on success
356 * \c < 0 on error
357 */
358 static int rmmod_recurse(struct kmod_module *mod) {
359 int ret = 0;
360 struct kmod_list *deps, *itr;
361
362 if (kmod_module_get_initstate(mod) == KMOD_MODULE_BUILTIN) {
363 DBG("Module %s is builtin", kmod_module_get_name(mod));
364 return ret;
365 }
366
367 ret = kmod_module_remove_module(mod, 0);
368
369 deps = kmod_module_get_dependencies(mod);
370 if (deps != NULL) {
371 kmod_list_foreach(itr, deps) {
372 struct kmod_module *dep = kmod_module_get_module(itr);
373 if (kmod_module_get_refcnt(dep) == 0) {
374 DBG("Recursive remove module %s",
375 kmod_module_get_name(dep));
376 rmmod_recurse(dep);
377 }
378 kmod_module_unref(dep);
379 }
380 kmod_module_unref_list(deps);
381 }
382
383 return ret;
384 }
385
386 /**
387 * @brief Unloads the kernel modules in \p modules
388 *
389 * @param modules List of modules to unload
390 * @param entries Number of modules in the list
391 *
392 */
393 static void modprobe_remove_lttng(const struct kern_modules_param *modules,
394 int entries)
395 {
396 int ret = 0, i;
397 struct kmod_ctx *ctx;
398
399 ret = setup_kmod_ctx(&ctx);
400 if (ret < 0) {
401 goto error;
402 }
403
404 for (i = entries - 1; i >= 0; i--) {
405 struct kmod_module *mod = NULL;
406
407 if (!modules[i].loaded) {
408 continue;
409 }
410
411 ret = kmod_module_new_from_name(ctx, modules[i].name, &mod);
412 if (ret < 0) {
413 PERROR("Failed to create kmod module for %s", modules[i].name);
414 goto error;
415 }
416
417 ret = rmmod_recurse(mod);
418 if (ret == -EEXIST) {
419 DBG("Module %s is not in kernel.", modules[i].name);
420 } else if (modules[i].load_policy == KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED && ret < 0) {
421 ERR("Unable to remove module %s", modules[i].name);
422 } else {
423 DBG("Modprobe removal successful %s",
424 modules[i].name);
425 }
426
427 kmod_module_unref(mod);
428 }
429
430 error:
431 if (ctx) {
432 kmod_unref(ctx);
433 }
434 }
435
436 #else /* HAVE_KMOD */
437
438 static int modprobe_lttng(struct kern_modules_param *modules,
439 int entries)
440 {
441 int ret = 0, i;
442 char modprobe[256];
443
444 for (i = 0; i < entries; i++) {
445 ret = snprintf(modprobe, sizeof(modprobe),
446 "/sbin/modprobe %s%s",
447 modules[i].load_policy == KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED ? "" : "-q ",
448 modules[i].name);
449 if (ret < 0) {
450 PERROR("snprintf modprobe");
451 goto error;
452 }
453 modprobe[sizeof(modprobe) - 1] = '\0';
454 ret = system(modprobe);
455 if (ret == -1) {
456 if (modules[i].load_policy == KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED) {
457 ERR("Unable to launch modprobe for required module %s",
458 modules[i].name);
459 goto error;
460 } else {
461 DBG("Unable to launch modprobe for optional module %s; continuing",
462 modules[i].name);
463 ret = 0;
464 }
465 } else if (WEXITSTATUS(ret) != 0) {
466 if (modules[i].load_policy == KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED) {
467 ERR("Unable to load required module %s",
468 modules[i].name);
469 goto error;
470 } else {
471 DBG("Unable to load optional module %s; continuing",
472 modules[i].name);
473 ret = 0;
474 }
475 } else {
476 DBG("Modprobe successfully %s", modules[i].name);
477 modules[i].loaded = true;
478 }
479 }
480
481 error:
482 return ret;
483 }
484
485 static void modprobe_remove_lttng(const struct kern_modules_param *modules,
486 int entries)
487 {
488 int ret = 0, i;
489 char modprobe[256];
490
491 for (i = entries - 1; i >= 0; i--) {
492 if (!modules[i].loaded) {
493 continue;
494 }
495 ret = snprintf(modprobe, sizeof(modprobe),
496 "/sbin/modprobe -r -q %s",
497 modules[i].name);
498 if (ret < 0) {
499 PERROR("snprintf modprobe -r");
500 return;
501 }
502 modprobe[sizeof(modprobe) - 1] = '\0';
503 ret = system(modprobe);
504 if (ret == -1) {
505 ERR("Unable to launch modprobe -r for module %s",
506 modules[i].name);
507 } else if (modules[i].load_policy == KERNEL_MODULE_PROPERTY_LOAD_POLICY_REQUIRED && WEXITSTATUS(ret) != 0) {
508 ERR("Unable to remove module %s",
509 modules[i].name);
510 } else {
511 DBG("Modprobe removal successful %s",
512 modules[i].name);
513 }
514 }
515 }
516
517 #endif /* HAVE_KMOD */
518
519 /*
520 * Remove control kernel module(s) in reverse load order.
521 */
522 void modprobe_remove_lttng_control(void)
523 {
524 modprobe_remove_lttng(kern_modules_control_core,
525 ARRAY_SIZE(kern_modules_control_core));
526 }
527
528 static void free_probes(void)
529 {
530 int i;
531
532 if (!probes) {
533 return;
534 }
535 for (i = 0; i < nr_probes; ++i) {
536 free(probes[i].name);
537 }
538 free(probes);
539 probes = NULL;
540 nr_probes = 0;
541 }
542
543 /*
544 * Remove data kernel modules in reverse load order.
545 */
546 void modprobe_remove_lttng_data(void)
547 {
548 if (!probes) {
549 return;
550 }
551
552 modprobe_remove_lttng(probes, nr_probes);
553 free_probes();
554 }
555
556 /*
557 * Remove all kernel modules in reverse order.
558 */
559 void modprobe_remove_lttng_all(void)
560 {
561 modprobe_remove_lttng_data();
562 modprobe_remove_lttng_control();
563 }
564
565 /*
566 * Load control kernel module(s).
567 */
568 int modprobe_lttng_control(void)
569 {
570 return modprobe_lttng(kern_modules_control_core,
571 ARRAY_SIZE(kern_modules_control_core));
572 }
573
574 /**
575 * Grow global list of probes (double capacity or set it to 1 if
576 * currently 0 and copy existing data).
577 */
578 static int grow_probes(void)
579 {
580 int i;
581 struct kern_modules_param *tmp_probes;
582
583 /* Initialize capacity to 1 if 0. */
584 if (probes_capacity == 0) {
585 probes = zmalloc(sizeof(*probes));
586 if (!probes) {
587 PERROR("malloc probe list");
588 return -ENOMEM;
589 }
590
591 probes_capacity = 1;
592 return 0;
593 }
594
595 /* Double size. */
596 probes_capacity *= 2;
597
598 tmp_probes = zmalloc(sizeof(*tmp_probes) * probes_capacity);
599 if (!tmp_probes) {
600 PERROR("malloc probe list");
601 return -ENOMEM;
602 }
603
604 for (i = 0; i < nr_probes; ++i) {
605 /* Ownership of 'name' field is transferred. */
606 tmp_probes[i] = probes[i];
607 }
608
609 /* Replace probes with larger copy. */
610 free(probes);
611 probes = tmp_probes;
612
613 return 0;
614 }
615
616 /*
617 * Appends a comma-separated list of probes to the global list
618 * of probes.
619 */
620 static int append_list_to_probes(const char *list)
621 {
622 char *next;
623 int ret;
624 char *tmp_list, *cur_list;
625
626 assert(list);
627
628 cur_list = tmp_list = strdup(list);
629 if (!tmp_list) {
630 PERROR("strdup temp list");
631 return -ENOMEM;
632 }
633
634 for (;;) {
635 size_t name_len;
636 struct kern_modules_param *cur_mod;
637
638 next = strtok(cur_list, ",");
639 if (!next) {
640 break;
641 }
642 cur_list = NULL;
643
644 /* filter leading spaces */
645 while (*next == ' ') {
646 next++;
647 }
648
649 if (probes_capacity <= nr_probes) {
650 ret = grow_probes();
651 if (ret) {
652 goto error;
653 }
654 }
655
656 /* Length 13 is "lttng-probe-" + \0 */
657 name_len = strlen(next) + 13;
658
659 cur_mod = &probes[nr_probes];
660 cur_mod->name = zmalloc(name_len);
661 if (!cur_mod->name) {
662 PERROR("malloc probe list");
663 ret = -ENOMEM;
664 goto error;
665 }
666
667 ret = snprintf(cur_mod->name, name_len, "lttng-probe-%s", next);
668 if (ret < 0) {
669 PERROR("snprintf modprobe name");
670 ret = -ENOMEM;
671 goto error;
672 }
673
674 cur_mod->load_policy = KERNEL_MODULE_PROPERTY_LOAD_POLICY_OPTIONAL;
675
676 nr_probes++;
677 }
678
679 free(tmp_list);
680 return 0;
681
682 error:
683 free(tmp_list);
684 free_probes();
685 return ret;
686 }
687
688 /*
689 * Load data kernel module(s).
690 */
691 int modprobe_lttng_data(void)
692 {
693 int ret, i;
694 char *list;
695
696 /*
697 * Base probes: either from command line option, environment
698 * variable or default list.
699 */
700 list = config.kmod_probes_list.value;
701 if (list) {
702 /* User-specified probes. */
703 ret = append_list_to_probes(list);
704 if (ret) {
705 return ret;
706 }
707 } else {
708 /* Default probes. */
709 int def_len = ARRAY_SIZE(kern_modules_probes_default);
710
711 probes = zmalloc(sizeof(*probes) * def_len);
712 if (!probes) {
713 PERROR("malloc probe list");
714 return -ENOMEM;
715 }
716
717 nr_probes = probes_capacity = def_len;
718
719 for (i = 0; i < def_len; ++i) {
720 char* name = strdup(kern_modules_probes_default[i].name);
721
722 if (!name) {
723 PERROR("strdup probe item");
724 ret = -ENOMEM;
725 goto error;
726 }
727
728 probes[i].name = name;
729 probes[i].load_policy = kern_modules_probes_default[i].load_policy;
730 }
731 }
732
733 /*
734 * Extra modules? Append them to current probes list.
735 */
736 list = config.kmod_extra_probes_list.value;
737 if (list) {
738 ret = append_list_to_probes(list);
739 if (ret) {
740 goto error;
741 }
742 }
743
744 /*
745 * Load probes modules now.
746 */
747 ret = modprobe_lttng(probes, nr_probes);
748 if (ret) {
749 goto error;
750 }
751 return ret;
752
753 error:
754 free_probes();
755 return ret;
756 }
This page took 0.044047 seconds and 4 git commands to generate.