Update for kernel 5.7: use vmalloc_sync_mappings on kernels >= 5.7
[lttng-modules.git] / probes / lttng-kprobes.c
1 /* SPDX-License-Identifier: (GPL-2.0 or LGPL-2.1)
2 *
3 * probes/lttng-kprobes.c
4 *
5 * LTTng kprobes integration module.
6 *
7 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 */
9
10 #include <linux/module.h>
11 #include <linux/kprobes.h>
12 #include <linux/slab.h>
13 #include <lttng-events.h>
14 #include <wrapper/ringbuffer/frontend_types.h>
15 #include <wrapper/vmalloc.h>
16 #include <wrapper/irqflags.h>
17 #include <lttng-tracer.h>
18 #include <blacklist/kprobes.h>
19
20 static
21 int lttng_kprobes_handler_pre(struct kprobe *p, struct pt_regs *regs)
22 {
23 struct lttng_event *event =
24 container_of(p, struct lttng_event, u.kprobe.kp);
25 struct lttng_probe_ctx lttng_probe_ctx = {
26 .event = event,
27 .interruptible = !lttng_regs_irqs_disabled(regs),
28 };
29 struct lttng_channel *chan = event->chan;
30 struct lib_ring_buffer_ctx ctx;
31 int ret;
32 unsigned long data = (unsigned long) p->addr;
33
34 if (unlikely(!READ_ONCE(chan->session->active)))
35 return 0;
36 if (unlikely(!READ_ONCE(chan->enabled)))
37 return 0;
38 if (unlikely(!READ_ONCE(event->enabled)))
39 return 0;
40
41 lib_ring_buffer_ctx_init(&ctx, chan->chan, &lttng_probe_ctx, sizeof(data),
42 lttng_alignof(data), -1);
43 ret = chan->ops->event_reserve(&ctx, event->id);
44 if (ret < 0)
45 return 0;
46 lib_ring_buffer_align_ctx(&ctx, lttng_alignof(data));
47 chan->ops->event_write(&ctx, &data, sizeof(data));
48 chan->ops->event_commit(&ctx);
49 return 0;
50 }
51
52 /*
53 * Create event description
54 */
55 static
56 int lttng_create_kprobe_event(const char *name, struct lttng_event *event)
57 {
58 struct lttng_event_field *field;
59 struct lttng_event_desc *desc;
60 int ret;
61
62 desc = kzalloc(sizeof(*event->desc), GFP_KERNEL);
63 if (!desc)
64 return -ENOMEM;
65 desc->name = kstrdup(name, GFP_KERNEL);
66 if (!desc->name) {
67 ret = -ENOMEM;
68 goto error_str;
69 }
70 desc->nr_fields = 1;
71 desc->fields = field =
72 kzalloc(1 * sizeof(struct lttng_event_field), GFP_KERNEL);
73 if (!field) {
74 ret = -ENOMEM;
75 goto error_field;
76 }
77 field->name = "ip";
78 field->type.atype = atype_integer;
79 field->type.u.basic.integer.size = sizeof(unsigned long) * CHAR_BIT;
80 field->type.u.basic.integer.alignment = lttng_alignof(unsigned long) * CHAR_BIT;
81 field->type.u.basic.integer.signedness = lttng_is_signed_type(unsigned long);
82 field->type.u.basic.integer.reverse_byte_order = 0;
83 field->type.u.basic.integer.base = 16;
84 field->type.u.basic.integer.encoding = lttng_encode_none;
85 desc->owner = THIS_MODULE;
86 event->desc = desc;
87
88 return 0;
89
90 error_field:
91 kfree(desc->name);
92 error_str:
93 kfree(desc);
94 return ret;
95 }
96
97 int lttng_kprobes_register(const char *name,
98 const char *symbol_name,
99 uint64_t offset,
100 uint64_t addr,
101 struct lttng_event *event)
102 {
103 int ret;
104
105 /* Kprobes expects a NULL symbol name if unused */
106 if (symbol_name[0] == '\0')
107 symbol_name = NULL;
108
109 ret = lttng_create_kprobe_event(name, event);
110 if (ret)
111 goto error;
112 memset(&event->u.kprobe.kp, 0, sizeof(event->u.kprobe.kp));
113 event->u.kprobe.kp.pre_handler = lttng_kprobes_handler_pre;
114 if (symbol_name) {
115 event->u.kprobe.symbol_name =
116 kzalloc(LTTNG_KERNEL_SYM_NAME_LEN * sizeof(char),
117 GFP_KERNEL);
118 if (!event->u.kprobe.symbol_name) {
119 ret = -ENOMEM;
120 goto name_error;
121 }
122 memcpy(event->u.kprobe.symbol_name, symbol_name,
123 LTTNG_KERNEL_SYM_NAME_LEN * sizeof(char));
124 event->u.kprobe.kp.symbol_name =
125 event->u.kprobe.symbol_name;
126 }
127 event->u.kprobe.kp.offset = offset;
128 event->u.kprobe.kp.addr = (void *) (unsigned long) addr;
129
130 /*
131 * Ensure the memory we just allocated don't trigger page faults.
132 * Well.. kprobes itself puts the page fault handler on the blacklist,
133 * but we can never be too careful.
134 */
135 wrapper_vmalloc_sync_mappings();
136
137 ret = register_kprobe(&event->u.kprobe.kp);
138 if (ret)
139 goto register_error;
140 return 0;
141
142 register_error:
143 kfree(event->u.kprobe.symbol_name);
144 name_error:
145 kfree(event->desc->fields);
146 kfree(event->desc->name);
147 kfree(event->desc);
148 error:
149 return ret;
150 }
151 EXPORT_SYMBOL_GPL(lttng_kprobes_register);
152
153 void lttng_kprobes_unregister(struct lttng_event *event)
154 {
155 unregister_kprobe(&event->u.kprobe.kp);
156 }
157 EXPORT_SYMBOL_GPL(lttng_kprobes_unregister);
158
159 void lttng_kprobes_destroy_private(struct lttng_event *event)
160 {
161 kfree(event->u.kprobe.symbol_name);
162 kfree(event->desc->fields);
163 kfree(event->desc->name);
164 kfree(event->desc);
165 }
166 EXPORT_SYMBOL_GPL(lttng_kprobes_destroy_private);
167
168 MODULE_LICENSE("GPL and additional rights");
169 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
170 MODULE_DESCRIPTION("LTTng kprobes probes");
171 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "."
172 __stringify(LTTNG_MODULES_MINOR_VERSION) "."
173 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION)
174 LTTNG_MODULES_EXTRAVERSION);
This page took 0.040688 seconds and 4 git commands to generate.