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