| 1 | /* SPDX-License-Identifier: (GPL-2.0 OR LGPL-2.1) |
| 2 | * |
| 3 | * probes/lttng-uprobes.c |
| 4 | * |
| 5 | * LTTng uprobes integration module. |
| 6 | * |
| 7 | * Copyright (C) 2013 Yannick Brosseau <yannick.brosseau@gmail.com> |
| 8 | * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 9 | * |
| 10 | */ |
| 11 | |
| 12 | #include <linux/fdtable.h> |
| 13 | #include <linux/list.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/namei.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/uaccess.h> |
| 18 | #include <lttng-events.h> |
| 19 | #include <lttng-tracer.h> |
| 20 | #include <wrapper/irqflags.h> |
| 21 | #include <wrapper/ringbuffer/frontend_types.h> |
| 22 | #include <wrapper/uprobes.h> |
| 23 | #include <wrapper/vmalloc.h> |
| 24 | |
| 25 | static |
| 26 | int lttng_uprobes_handler_pre(struct uprobe_consumer *uc, struct pt_regs *regs) |
| 27 | { |
| 28 | struct lttng_uprobe_handler *uprobe_handler = |
| 29 | container_of(uc, struct lttng_uprobe_handler, up_consumer); |
| 30 | struct lttng_event *event = uprobe_handler->event; |
| 31 | struct lttng_probe_ctx lttng_probe_ctx = { |
| 32 | .event = event, |
| 33 | .interruptible = !lttng_regs_irqs_disabled(regs), |
| 34 | }; |
| 35 | struct lttng_channel *chan = event->chan; |
| 36 | struct lib_ring_buffer_ctx ctx; |
| 37 | int ret; |
| 38 | |
| 39 | struct { |
| 40 | unsigned long ip; |
| 41 | } payload; |
| 42 | |
| 43 | if (unlikely(!LTTNG_READ_ONCE(chan->session->active))) |
| 44 | return 0; |
| 45 | if (unlikely(!LTTNG_READ_ONCE(chan->enabled))) |
| 46 | return 0; |
| 47 | if (unlikely(!LTTNG_READ_ONCE(event->enabled))) |
| 48 | return 0; |
| 49 | |
| 50 | lib_ring_buffer_ctx_init(&ctx, chan->chan, <tng_probe_ctx, |
| 51 | sizeof(payload), lttng_alignof(payload), -1); |
| 52 | |
| 53 | ret = chan->ops->event_reserve(&ctx, event->id); |
| 54 | if (ret < 0) |
| 55 | return 0; |
| 56 | |
| 57 | /* Event payload. */ |
| 58 | payload.ip = (unsigned long)instruction_pointer(regs); |
| 59 | |
| 60 | lib_ring_buffer_align_ctx(&ctx, lttng_alignof(payload)); |
| 61 | chan->ops->event_write(&ctx, &payload, sizeof(payload)); |
| 62 | chan->ops->event_commit(&ctx); |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | * Create event description. |
| 68 | */ |
| 69 | static |
| 70 | int lttng_create_uprobe_event(const char *name, struct lttng_event *event) |
| 71 | { |
| 72 | struct lttng_event_desc *desc; |
| 73 | struct lttng_event_field *fields; |
| 74 | int ret; |
| 75 | |
| 76 | desc = kzalloc(sizeof(*event->desc), GFP_KERNEL); |
| 77 | if (!desc) |
| 78 | return -ENOMEM; |
| 79 | desc->name = kstrdup(name, GFP_KERNEL); |
| 80 | if (!desc->name) { |
| 81 | ret = -ENOMEM; |
| 82 | goto error_str; |
| 83 | } |
| 84 | |
| 85 | desc->nr_fields = 1; |
| 86 | desc->fields = fields = |
| 87 | kzalloc(1 * sizeof(struct lttng_event_field), GFP_KERNEL); |
| 88 | |
| 89 | if (!desc->fields) { |
| 90 | ret = -ENOMEM; |
| 91 | goto error_fields; |
| 92 | } |
| 93 | fields[0].name = "ip"; |
| 94 | fields[0].type.atype = atype_integer; |
| 95 | fields[0].type.u.basic.integer.size = sizeof(unsigned long) * CHAR_BIT; |
| 96 | fields[0].type.u.basic.integer.alignment = lttng_alignof(unsigned long) * CHAR_BIT; |
| 97 | fields[0].type.u.basic.integer.signedness = lttng_is_signed_type(unsigned long); |
| 98 | fields[0].type.u.basic.integer.reverse_byte_order = 0; |
| 99 | fields[0].type.u.basic.integer.base = 16; |
| 100 | fields[0].type.u.basic.integer.encoding = lttng_encode_none; |
| 101 | |
| 102 | desc->owner = THIS_MODULE; |
| 103 | event->desc = desc; |
| 104 | |
| 105 | return 0; |
| 106 | |
| 107 | error_fields: |
| 108 | kfree(desc->name); |
| 109 | error_str: |
| 110 | kfree(desc); |
| 111 | return ret; |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * Returns the inode struct from the current task and an fd. The inode is |
| 116 | * grabbed by this function and must be put once we are done with it using |
| 117 | * iput(). |
| 118 | */ |
| 119 | static struct inode *get_inode_from_fd(int fd) |
| 120 | { |
| 121 | struct file *file; |
| 122 | struct inode *inode; |
| 123 | |
| 124 | rcu_read_lock(); |
| 125 | /* |
| 126 | * Returns the file backing the given fd. Needs to be done inside an RCU |
| 127 | * critical section. |
| 128 | */ |
| 129 | file = fcheck(fd); |
| 130 | if (file == NULL) { |
| 131 | printk(KERN_WARNING "Cannot access file backing the fd(%d)\n", fd); |
| 132 | inode = NULL; |
| 133 | goto error; |
| 134 | } |
| 135 | |
| 136 | /* Grab a reference on the inode. */ |
| 137 | inode = igrab(file->f_path.dentry->d_inode); |
| 138 | if (inode == NULL) |
| 139 | printk(KERN_WARNING "Cannot grab a reference on the inode.\n"); |
| 140 | error: |
| 141 | rcu_read_unlock(); |
| 142 | return inode; |
| 143 | } |
| 144 | |
| 145 | int lttng_uprobes_add_callsite(struct lttng_event *event, |
| 146 | struct lttng_kernel_event_callsite __user *callsite) |
| 147 | { |
| 148 | int ret = 0; |
| 149 | struct lttng_uprobe_handler *uprobe_handler; |
| 150 | |
| 151 | if (!event) { |
| 152 | ret = -EINVAL; |
| 153 | goto end; |
| 154 | } |
| 155 | |
| 156 | uprobe_handler = kzalloc(sizeof(struct lttng_uprobe_handler), GFP_KERNEL); |
| 157 | if (!uprobe_handler) { |
| 158 | printk(KERN_WARNING "Error allocating uprobe_uprobe_handlers"); |
| 159 | ret = -ENOMEM; |
| 160 | goto end; |
| 161 | } |
| 162 | |
| 163 | /* Ensure the memory we just allocated don't trigger page faults. */ |
| 164 | wrapper_vmalloc_sync_mappings(); |
| 165 | |
| 166 | uprobe_handler->event = event; |
| 167 | uprobe_handler->up_consumer.handler = lttng_uprobes_handler_pre; |
| 168 | |
| 169 | ret = copy_from_user(&uprobe_handler->offset, &callsite->u.uprobe.offset, sizeof(uint64_t)); |
| 170 | if (ret) { |
| 171 | goto register_error; |
| 172 | } |
| 173 | |
| 174 | ret = wrapper_uprobe_register(event->u.uprobe.inode, |
| 175 | uprobe_handler->offset, &uprobe_handler->up_consumer); |
| 176 | if (ret) { |
| 177 | printk(KERN_WARNING "Error registering probe on inode %lu " |
| 178 | "and offset 0x%llx\n", event->u.uprobe.inode->i_ino, |
| 179 | uprobe_handler->offset); |
| 180 | ret = -1; |
| 181 | goto register_error; |
| 182 | } |
| 183 | |
| 184 | list_add(&uprobe_handler->node, &event->u.uprobe.head); |
| 185 | |
| 186 | return ret; |
| 187 | |
| 188 | register_error: |
| 189 | kfree(uprobe_handler); |
| 190 | end: |
| 191 | return ret; |
| 192 | } |
| 193 | EXPORT_SYMBOL_GPL(lttng_uprobes_add_callsite); |
| 194 | |
| 195 | int lttng_uprobes_register(const char *name, int fd, struct lttng_event *event) |
| 196 | { |
| 197 | int ret = 0; |
| 198 | struct inode *inode; |
| 199 | |
| 200 | ret = lttng_create_uprobe_event(name, event); |
| 201 | if (ret) |
| 202 | goto error; |
| 203 | |
| 204 | inode = get_inode_from_fd(fd); |
| 205 | if (!inode) { |
| 206 | printk(KERN_WARNING "Cannot get inode from fd\n"); |
| 207 | ret = -EBADF; |
| 208 | goto inode_error; |
| 209 | } |
| 210 | event->u.uprobe.inode = inode; |
| 211 | INIT_LIST_HEAD(&event->u.uprobe.head); |
| 212 | |
| 213 | return 0; |
| 214 | |
| 215 | inode_error: |
| 216 | kfree(event->desc->name); |
| 217 | kfree(event->desc); |
| 218 | error: |
| 219 | return ret; |
| 220 | } |
| 221 | EXPORT_SYMBOL_GPL(lttng_uprobes_register); |
| 222 | |
| 223 | void lttng_uprobes_unregister(struct lttng_event *event) |
| 224 | { |
| 225 | struct lttng_uprobe_handler *iter, *tmp; |
| 226 | |
| 227 | /* |
| 228 | * Iterate over the list of handler, remove each handler from the list |
| 229 | * and free the struct. |
| 230 | */ |
| 231 | list_for_each_entry_safe(iter, tmp, &event->u.uprobe.head, node) { |
| 232 | wrapper_uprobe_unregister(event->u.uprobe.inode, iter->offset, |
| 233 | &iter->up_consumer); |
| 234 | list_del(&iter->node); |
| 235 | kfree(iter); |
| 236 | } |
| 237 | } |
| 238 | EXPORT_SYMBOL_GPL(lttng_uprobes_unregister); |
| 239 | |
| 240 | void lttng_uprobes_destroy_private(struct lttng_event *event) |
| 241 | { |
| 242 | iput(event->u.uprobe.inode); |
| 243 | kfree(event->desc->name); |
| 244 | kfree(event->desc); |
| 245 | } |
| 246 | EXPORT_SYMBOL_GPL(lttng_uprobes_destroy_private); |
| 247 | |
| 248 | MODULE_LICENSE("GPL and additional rights"); |
| 249 | MODULE_AUTHOR("Yannick Brosseau"); |
| 250 | MODULE_DESCRIPTION("Linux Trace Toolkit Uprobes Support"); |