| 1 | /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only) |
| 2 | * |
| 3 | * lttng-probes.c |
| 4 | * |
| 5 | * Holds LTTng probes registry. |
| 6 | * |
| 7 | * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 8 | */ |
| 9 | |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/list.h> |
| 12 | #include <linux/mutex.h> |
| 13 | #include <linux/seq_file.h> |
| 14 | |
| 15 | #include <lttng/events.h> |
| 16 | |
| 17 | /* |
| 18 | * probe list is protected by sessions lock. |
| 19 | */ |
| 20 | static LIST_HEAD(_probe_list); |
| 21 | |
| 22 | /* |
| 23 | * List of probes registered by not yet processed. |
| 24 | */ |
| 25 | static LIST_HEAD(lazy_probe_init); |
| 26 | |
| 27 | /* |
| 28 | * lazy_nesting counter ensures we don't event_notifier lazy probe registration |
| 29 | * fixup while we are performing the fixup. It is protected by the |
| 30 | * sessions lock. |
| 31 | */ |
| 32 | static int lazy_nesting; |
| 33 | |
| 34 | DEFINE_PER_CPU(struct lttng_dynamic_len_stack, lttng_dynamic_len_stack); |
| 35 | |
| 36 | EXPORT_PER_CPU_SYMBOL_GPL(lttng_dynamic_len_stack); |
| 37 | |
| 38 | /* |
| 39 | * Called under sessions lock. |
| 40 | */ |
| 41 | static |
| 42 | int check_event_provider(struct lttng_probe_desc *desc) |
| 43 | { |
| 44 | int i; |
| 45 | size_t provider_name_len; |
| 46 | |
| 47 | provider_name_len = strnlen(desc->provider, |
| 48 | LTTNG_KERNEL_SYM_NAME_LEN - 1); |
| 49 | for (i = 0; i < desc->nr_events; i++) { |
| 50 | if (strncmp(desc->event_desc[i]->name, |
| 51 | desc->provider, |
| 52 | provider_name_len)) |
| 53 | return 0; /* provider mismatch */ |
| 54 | /* |
| 55 | * The event needs to contain at least provider name + _ + |
| 56 | * one or more letter. |
| 57 | */ |
| 58 | if (strlen(desc->event_desc[i]->name) <= provider_name_len + 1) |
| 59 | return 0; /* provider mismatch */ |
| 60 | if (desc->event_desc[i]->name[provider_name_len] != '_') |
| 61 | return 0; /* provider mismatch */ |
| 62 | } |
| 63 | return 1; |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | * Called under sessions lock. |
| 68 | */ |
| 69 | static |
| 70 | void lttng_lazy_probe_register(struct lttng_probe_desc *desc) |
| 71 | { |
| 72 | struct lttng_probe_desc *iter; |
| 73 | struct list_head *probe_list; |
| 74 | |
| 75 | /* |
| 76 | * Each provider enforce that every event name begins with the |
| 77 | * provider name. Check this in an assertion for extra |
| 78 | * carefulness. This ensures we cannot have duplicate event |
| 79 | * names across providers. |
| 80 | */ |
| 81 | WARN_ON_ONCE(!check_event_provider(desc)); |
| 82 | |
| 83 | /* |
| 84 | * The provider ensures there are no duplicate event names. |
| 85 | * Duplicated TRACEPOINT_EVENT event names would generate a |
| 86 | * compile-time error due to duplicated symbol names. |
| 87 | */ |
| 88 | |
| 89 | /* |
| 90 | * We sort the providers by struct lttng_probe_desc pointer |
| 91 | * address. |
| 92 | */ |
| 93 | probe_list = &_probe_list; |
| 94 | list_for_each_entry_reverse(iter, probe_list, head) { |
| 95 | BUG_ON(iter == desc); /* Should never be in the list twice */ |
| 96 | if (iter < desc) { |
| 97 | /* We belong to the location right after iter. */ |
| 98 | list_add(&desc->head, &iter->head); |
| 99 | goto desc_added; |
| 100 | } |
| 101 | } |
| 102 | /* We should be added at the head of the list */ |
| 103 | list_add(&desc->head, probe_list); |
| 104 | desc_added: |
| 105 | pr_debug("LTTng: just registered probe %s containing %u events\n", |
| 106 | desc->provider, desc->nr_events); |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * Called under sessions lock. |
| 111 | */ |
| 112 | static |
| 113 | void fixup_lazy_probes(void) |
| 114 | { |
| 115 | struct lttng_probe_desc *iter, *tmp; |
| 116 | int ret; |
| 117 | |
| 118 | lazy_nesting++; |
| 119 | list_for_each_entry_safe(iter, tmp, |
| 120 | &lazy_probe_init, lazy_init_head) { |
| 121 | lttng_lazy_probe_register(iter); |
| 122 | iter->lazy = 0; |
| 123 | list_del(&iter->lazy_init_head); |
| 124 | } |
| 125 | ret = lttng_fix_pending_events(); |
| 126 | WARN_ON_ONCE(ret); |
| 127 | ret = lttng_fix_pending_event_notifiers(); |
| 128 | WARN_ON_ONCE(ret); |
| 129 | lazy_nesting--; |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * Called under sessions lock. |
| 134 | */ |
| 135 | struct list_head *lttng_get_probe_list_head(void) |
| 136 | { |
| 137 | if (!lazy_nesting && !list_empty(&lazy_probe_init)) |
| 138 | fixup_lazy_probes(); |
| 139 | return &_probe_list; |
| 140 | } |
| 141 | |
| 142 | static |
| 143 | const struct lttng_probe_desc *find_provider(const char *provider) |
| 144 | { |
| 145 | struct lttng_probe_desc *iter; |
| 146 | struct list_head *probe_list; |
| 147 | |
| 148 | probe_list = lttng_get_probe_list_head(); |
| 149 | list_for_each_entry(iter, probe_list, head) { |
| 150 | if (!strcmp(iter->provider, provider)) |
| 151 | return iter; |
| 152 | } |
| 153 | return NULL; |
| 154 | } |
| 155 | |
| 156 | int lttng_probe_register(struct lttng_probe_desc *desc) |
| 157 | { |
| 158 | int ret = 0; |
| 159 | |
| 160 | lttng_lock_sessions(); |
| 161 | |
| 162 | /* |
| 163 | * Check if the provider has already been registered. |
| 164 | */ |
| 165 | if (find_provider(desc->provider)) { |
| 166 | ret = -EEXIST; |
| 167 | goto end; |
| 168 | } |
| 169 | list_add(&desc->lazy_init_head, &lazy_probe_init); |
| 170 | desc->lazy = 1; |
| 171 | pr_debug("LTTng: adding probe %s containing %u events to lazy registration list\n", |
| 172 | desc->provider, desc->nr_events); |
| 173 | /* |
| 174 | * If there is at least one active session, we need to register |
| 175 | * the probe immediately, since we cannot delay event |
| 176 | * registration because they are needed ASAP. |
| 177 | */ |
| 178 | if (lttng_session_active() || lttng_event_notifier_active()) |
| 179 | fixup_lazy_probes(); |
| 180 | end: |
| 181 | lttng_unlock_sessions(); |
| 182 | return ret; |
| 183 | } |
| 184 | EXPORT_SYMBOL_GPL(lttng_probe_register); |
| 185 | |
| 186 | void lttng_probe_unregister(struct lttng_probe_desc *desc) |
| 187 | { |
| 188 | lttng_lock_sessions(); |
| 189 | if (!desc->lazy) |
| 190 | list_del(&desc->head); |
| 191 | else |
| 192 | list_del(&desc->lazy_init_head); |
| 193 | pr_debug("LTTng: just unregistered probe %s\n", desc->provider); |
| 194 | lttng_unlock_sessions(); |
| 195 | } |
| 196 | EXPORT_SYMBOL_GPL(lttng_probe_unregister); |
| 197 | |
| 198 | /* |
| 199 | * TODO: this is O(nr_probes * nb_events), could be faster. |
| 200 | * Called with sessions lock held. |
| 201 | */ |
| 202 | static |
| 203 | const struct lttng_event_desc *find_event_desc(const char *name) |
| 204 | { |
| 205 | struct lttng_probe_desc *probe_desc; |
| 206 | int i; |
| 207 | |
| 208 | list_for_each_entry(probe_desc, &_probe_list, head) { |
| 209 | for (i = 0; i < probe_desc->nr_events; i++) { |
| 210 | if (!strcmp(probe_desc->event_desc[i]->name, name)) |
| 211 | return probe_desc->event_desc[i]; |
| 212 | } |
| 213 | } |
| 214 | return NULL; |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * Called with sessions lock held. |
| 219 | */ |
| 220 | const struct lttng_event_desc *lttng_event_desc_get(const char *name) |
| 221 | { |
| 222 | const struct lttng_event_desc *event_desc; |
| 223 | int ret; |
| 224 | |
| 225 | event_desc = find_event_desc(name); |
| 226 | if (!event_desc) |
| 227 | return NULL; |
| 228 | ret = try_module_get(event_desc->owner); |
| 229 | WARN_ON_ONCE(!ret); |
| 230 | return event_desc; |
| 231 | } |
| 232 | EXPORT_SYMBOL_GPL(lttng_event_desc_get); |
| 233 | |
| 234 | /* |
| 235 | * Called with sessions lock held. |
| 236 | */ |
| 237 | void lttng_event_desc_put(const struct lttng_event_desc *event_desc) |
| 238 | { |
| 239 | module_put(event_desc->owner); |
| 240 | } |
| 241 | EXPORT_SYMBOL_GPL(lttng_event_desc_put); |
| 242 | |
| 243 | static |
| 244 | void *tp_list_start(struct seq_file *m, loff_t *pos) |
| 245 | { |
| 246 | struct lttng_probe_desc *probe_desc; |
| 247 | struct list_head *probe_list; |
| 248 | int iter = 0, i; |
| 249 | |
| 250 | lttng_lock_sessions(); |
| 251 | probe_list = lttng_get_probe_list_head(); |
| 252 | list_for_each_entry(probe_desc, probe_list, head) { |
| 253 | for (i = 0; i < probe_desc->nr_events; i++) { |
| 254 | if (iter++ >= *pos) |
| 255 | return (void *) probe_desc->event_desc[i]; |
| 256 | } |
| 257 | } |
| 258 | /* End of list */ |
| 259 | return NULL; |
| 260 | } |
| 261 | |
| 262 | static |
| 263 | void *tp_list_next(struct seq_file *m, void *p, loff_t *ppos) |
| 264 | { |
| 265 | struct lttng_probe_desc *probe_desc; |
| 266 | struct list_head *probe_list; |
| 267 | int iter = 0, i; |
| 268 | |
| 269 | (*ppos)++; |
| 270 | probe_list = lttng_get_probe_list_head(); |
| 271 | list_for_each_entry(probe_desc, probe_list, head) { |
| 272 | for (i = 0; i < probe_desc->nr_events; i++) { |
| 273 | if (iter++ >= *ppos) |
| 274 | return (void *) probe_desc->event_desc[i]; |
| 275 | } |
| 276 | } |
| 277 | /* End of list */ |
| 278 | return NULL; |
| 279 | } |
| 280 | |
| 281 | static |
| 282 | void tp_list_stop(struct seq_file *m, void *p) |
| 283 | { |
| 284 | lttng_unlock_sessions(); |
| 285 | } |
| 286 | |
| 287 | static |
| 288 | int tp_list_show(struct seq_file *m, void *p) |
| 289 | { |
| 290 | const struct lttng_event_desc *probe_desc = p; |
| 291 | |
| 292 | seq_printf(m, "event { name = %s; };\n", |
| 293 | probe_desc->name); |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | static |
| 298 | const struct seq_operations lttng_tracepoint_list_seq_ops = { |
| 299 | .start = tp_list_start, |
| 300 | .next = tp_list_next, |
| 301 | .stop = tp_list_stop, |
| 302 | .show = tp_list_show, |
| 303 | }; |
| 304 | |
| 305 | static |
| 306 | int lttng_tracepoint_list_open(struct inode *inode, struct file *file) |
| 307 | { |
| 308 | return seq_open(file, <tng_tracepoint_list_seq_ops); |
| 309 | } |
| 310 | |
| 311 | const struct file_operations lttng_tracepoint_list_fops = { |
| 312 | .owner = THIS_MODULE, |
| 313 | .open = lttng_tracepoint_list_open, |
| 314 | .read = seq_read, |
| 315 | .llseek = seq_lseek, |
| 316 | .release = seq_release, |
| 317 | }; |
| 318 | |
| 319 | int lttng_probes_init(void) |
| 320 | { |
| 321 | int cpu; |
| 322 | |
| 323 | for_each_possible_cpu(cpu) |
| 324 | per_cpu_ptr(<tng_dynamic_len_stack, cpu)->offset = 0; |
| 325 | return 0; |
| 326 | } |