| 1 |
/* |
| 2 |
* stp_tracepoint.c |
| 3 |
* |
| 4 |
* SystemTap layer for registering various kernel tracepoint APIs. |
| 5 |
* |
| 6 |
* Copyright (C) 2014-2019 Red Hat Inc. |
| 7 |
* |
| 8 |
* Initially derived from lttng-tracepoints.c: |
| 9 |
* LTTng adaptation layer for Linux kernel 3.15+ tracepoints. |
| 10 |
* |
| 11 |
* Copyright (C) 2014 Mathieu Desnoyers <[email protected]> |
| 12 |
* |
| 13 |
* This library is free software; you can redistribute it and/or |
| 14 |
* modify it under the terms of the GNU Lesser General Public |
| 15 |
* License as published by the Free Software Foundation; only |
| 16 |
* version 2.1 of the License. |
| 17 |
* |
| 18 |
* This library is distributed in the hope that it will be useful, |
| 19 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 21 |
* Lesser General Public License for more details. |
| 22 |
* |
| 23 |
* You should have received a copy of the GNU Lesser General Public |
| 24 |
* License along with this library; if not, write to the Free Software |
| 25 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 26 |
*/ |
| 27 |
|
| 28 |
#include <linux/list.h> |
| 29 |
#include <linux/mutex.h> |
| 30 |
#include <linux/err.h> |
| 31 |
#include <linux/notifier.h> |
| 32 |
#include <linux/tracepoint.h> |
| 33 |
#include <linux/slab.h> |
| 34 |
#include <linux/jhash.h> |
| 35 |
#include <linux/module.h> |
| 36 |
|
| 37 |
#include "stp_tracepoint.h" |
| 38 |
|
| 39 |
/* |
| 40 |
* Protect the tracepoint table. stp_tracepoint_mutex nests within |
| 41 |
* kernel/tracepoint.c tp_modlist_mutex. kernel/tracepoint.c |
| 42 |
* tracepoint_mutex nests within stp_tracepoint_mutex. |
| 43 |
*/ |
| 44 |
static |
| 45 |
DEFINE_MUTEX(stp_tracepoint_mutex); |
| 46 |
|
| 47 |
#define TRACEPOINT_HASH_BITS 6 |
| 48 |
#define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS) |
| 49 |
static |
| 50 |
struct hlist_head tracepoint_table[TRACEPOINT_TABLE_SIZE]; |
| 51 |
|
| 52 |
/* |
| 53 |
* The tracepoint entry is the node contained within the hash table. It |
| 54 |
* is a mapping from the "string" key to the struct tracepoint pointer. |
| 55 |
*/ |
| 56 |
struct tracepoint_entry { |
| 57 |
struct hlist_node hlist; |
| 58 |
struct tracepoint *tp; |
| 59 |
int refcount; |
| 60 |
struct list_head probes; |
| 61 |
char name[]; |
| 62 |
}; |
| 63 |
|
| 64 |
struct stp_tp_probe { |
| 65 |
struct tracepoint_func tp_func; |
| 66 |
struct list_head list; |
| 67 |
}; |
| 68 |
|
| 69 |
static |
| 70 |
int add_probe(struct tracepoint_entry *e, void *probe, void *data) |
| 71 |
{ |
| 72 |
struct stp_tp_probe *p; |
| 73 |
int found = 0; |
| 74 |
|
| 75 |
list_for_each_entry(p, &e->probes, list) { |
| 76 |
if (p->tp_func.func == probe && p->tp_func.data == data) { |
| 77 |
found = 1; |
| 78 |
break; |
| 79 |
} |
| 80 |
} |
| 81 |
if (found) |
| 82 |
return -EEXIST; |
| 83 |
p = _stp_kmalloc_gfp(sizeof(struct stp_tp_probe), |
| 84 |
STP_ALLOC_SLEEP_FLAGS); |
| 85 |
if (!p) |
| 86 |
return -ENOMEM; |
| 87 |
p->tp_func.func = probe; |
| 88 |
p->tp_func.data = data; |
| 89 |
list_add(&p->list, &e->probes); |
| 90 |
return 0; |
| 91 |
} |
| 92 |
|
| 93 |
static |
| 94 |
int remove_probe(struct tracepoint_entry *e, void *probe, void *data) |
| 95 |
{ |
| 96 |
struct stp_tp_probe *p; |
| 97 |
int found = 0; |
| 98 |
|
| 99 |
list_for_each_entry(p, &e->probes, list) { |
| 100 |
if (p->tp_func.func == probe && p->tp_func.data == data) { |
| 101 |
found = 1; |
| 102 |
break; |
| 103 |
} |
| 104 |
} |
| 105 |
if (found) { |
| 106 |
list_del(&p->list); |
| 107 |
_stp_kfree(p); |
| 108 |
return 0; |
| 109 |
} else { |
| 110 |
return -ENOENT; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
/* |
| 115 |
* Get tracepoint if the tracepoint is present in the tracepoint hash table. |
| 116 |
* Must be called with stp_tracepoint_mutex held. |
| 117 |
* Returns NULL if not present. |
| 118 |
*/ |
| 119 |
static |
| 120 |
struct tracepoint_entry *get_tracepoint(const char *name) |
| 121 |
{ |
| 122 |
struct hlist_head *head; |
| 123 |
struct tracepoint_entry *e; |
| 124 |
u32 hash = jhash(name, strlen(name), 0); |
| 125 |
|
| 126 |
head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)]; |
| 127 |
hlist_for_each_entry(e, head, hlist) { |
| 128 |
if (!strcmp(name, e->name)) |
| 129 |
return e; |
| 130 |
} |
| 131 |
return NULL; |
| 132 |
} |
| 133 |
|
| 134 |
/* |
| 135 |
* Add the tracepoint to the tracepoint hash table. Must be called with |
| 136 |
* stp_tracepoint_mutex held. |
| 137 |
*/ |
| 138 |
static |
| 139 |
struct tracepoint_entry *add_tracepoint(const char *name) |
| 140 |
{ |
| 141 |
struct hlist_head *head; |
| 142 |
struct tracepoint_entry *e; |
| 143 |
size_t name_len = strlen(name) + 1; |
| 144 |
u32 hash = jhash(name, name_len - 1, 0); |
| 145 |
|
| 146 |
head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)]; |
| 147 |
hlist_for_each_entry(e, head, hlist) { |
| 148 |
if (!strcmp(name, e->name)) { |
| 149 |
return ERR_PTR(-EEXIST); /* Already there */ |
| 150 |
} |
| 151 |
} |
| 152 |
/* |
| 153 |
* Using kmalloc here to allocate a variable length element. Could |
| 154 |
* cause some memory fragmentation if overused. |
| 155 |
*/ |
| 156 |
e = _stp_kmalloc_gfp(sizeof(struct tracepoint_entry) + name_len, |
| 157 |
STP_ALLOC_SLEEP_FLAGS); |
| 158 |
if (!e) |
| 159 |
return ERR_PTR(-ENOMEM); |
| 160 |
memcpy(&e->name[0], name, name_len); |
| 161 |
e->tp = NULL; |
| 162 |
e->refcount = 0; |
| 163 |
INIT_LIST_HEAD(&e->probes); |
| 164 |
hlist_add_head(&e->hlist, head); |
| 165 |
return e; |
| 166 |
} |
| 167 |
|
| 168 |
/* |
| 169 |
* Remove the tracepoint from the tracepoint hash table. Must be called |
| 170 |
* with stp_tracepoint_mutex held. |
| 171 |
*/ |
| 172 |
static |
| 173 |
void remove_tracepoint(struct tracepoint_entry *e) |
| 174 |
{ |
| 175 |
hlist_del(&e->hlist); |
| 176 |
_stp_kfree(e); |
| 177 |
} |
| 178 |
|
| 179 |
static |
| 180 |
void do_tracepoint_probe_register(struct tracepoint *tp, |
| 181 |
void *probe, void *data) |
| 182 |
{ |
| 183 |
int ret = tracepoint_probe_register(tp, probe, data); |
| 184 |
if (ret) { |
| 185 |
dbug_tp(1, "error (%d) registering probe '%s'\n", |
| 186 |
ret, tp->name); |
| 187 |
} else { |
| 188 |
dbug_tp(2, "registered probe '%s'\n", tp->name); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
static |
| 193 |
void do_tracepoint_probe_unregister(struct tracepoint *tp, |
| 194 |
void *probe, void *data) |
| 195 |
{ |
| 196 |
int ret = tracepoint_probe_unregister(tp, probe, data); |
| 197 |
if (ret) { |
| 198 |
dbug_tp(1, "error (%d) unregistering probe '%s'\n", |
| 199 |
ret, tp->name); |
| 200 |
} else { |
| 201 |
dbug_tp(2, "unregistered probe '%s'\n", tp->name); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
int stp_tracepoint_probe_register(const char *name, void *probe, void *data) |
| 206 |
{ |
| 207 |
struct tracepoint_entry *e; |
| 208 |
int ret = 0; |
| 209 |
|
| 210 |
might_sleep(); |
| 211 |
mutex_lock(&stp_tracepoint_mutex); |
| 212 |
e = get_tracepoint(name); |
| 213 |
if (!e) { |
| 214 |
e = add_tracepoint(name); |
| 215 |
if (IS_ERR(e)) { |
| 216 |
ret = PTR_ERR(e); |
| 217 |
goto end; |
| 218 |
} |
| 219 |
} |
| 220 |
/* add (probe, data) to entry */ |
| 221 |
ret = add_probe(e, probe, data); |
| 222 |
if (ret) { |
| 223 |
if (!e->refcount) |
| 224 |
remove_tracepoint(e); |
| 225 |
goto end; |
| 226 |
} |
| 227 |
e->refcount++; |
| 228 |
dbug_tp(2, "added probe on '%s'\n", name); |
| 229 |
if (e->tp) { |
| 230 |
do_tracepoint_probe_register(e->tp, probe, data); |
| 231 |
} |
| 232 |
end: |
| 233 |
mutex_unlock(&stp_tracepoint_mutex); |
| 234 |
if (ret) { |
| 235 |
dbug_tp(1, "error (%d) adding probe on '%s'\n", ret, name); |
| 236 |
} |
| 237 |
return ret; |
| 238 |
} |
| 239 |
|
| 240 |
int stp_tracepoint_probe_unregister(const char *name, void *probe, void *data) |
| 241 |
{ |
| 242 |
struct tracepoint_entry *e; |
| 243 |
int ret = 0; |
| 244 |
|
| 245 |
might_sleep(); |
| 246 |
mutex_lock(&stp_tracepoint_mutex); |
| 247 |
e = get_tracepoint(name); |
| 248 |
if (!e) { |
| 249 |
ret = -ENOENT; |
| 250 |
goto end; |
| 251 |
} |
| 252 |
/* remove (probe, data) from entry */ |
| 253 |
ret = remove_probe(e, probe, data); |
| 254 |
if (ret) |
| 255 |
goto end; |
| 256 |
if (e->tp) { |
| 257 |
do_tracepoint_probe_unregister(e->tp, probe, data); |
| 258 |
} |
| 259 |
if (!--e->refcount) |
| 260 |
remove_tracepoint(e); |
| 261 |
dbug_tp(2, "removed probe on '%s'\n", name); |
| 262 |
end: |
| 263 |
mutex_unlock(&stp_tracepoint_mutex); |
| 264 |
if (ret) { |
| 265 |
dbug_tp(1, "error (%d) removing probe on '%s'\n", ret, name); |
| 266 |
} |
| 267 |
return ret; |
| 268 |
} |
| 269 |
|
| 270 |
#ifdef CONFIG_MODULES |
| 271 |
|
| 272 |
static |
| 273 |
int stp_tracepoint_coming(struct tp_module *tp_mod) |
| 274 |
{ |
| 275 |
int i; |
| 276 |
|
| 277 |
might_sleep(); |
| 278 |
mutex_lock(&stp_tracepoint_mutex); |
| 279 |
for (i = 0; i < tp_mod->mod->num_tracepoints; i++) { |
| 280 |
struct tracepoint *tp; |
| 281 |
struct tracepoint_entry *e; |
| 282 |
struct stp_tp_probe *p; |
| 283 |
|
| 284 |
#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS |
| 285 |
tp = offset_to_ptr(&(((const int*)(tp_mod->mod->tracepoints_ptrs))[i])); |
| 286 |
#else |
| 287 |
tp = tp_mod->mod->tracepoints_ptrs[i]; |
| 288 |
#endif |
| 289 |
e = get_tracepoint(tp->name); |
| 290 |
if (!e) { |
| 291 |
e = add_tracepoint(tp->name); |
| 292 |
if (IS_ERR(e)) { |
| 293 |
dbug_tp(1, "error (%ld) adding %s:%s\n", |
| 294 |
PTR_ERR(e), tp_mod->mod->name, tp->name); |
| 295 |
continue; |
| 296 |
} |
| 297 |
} |
| 298 |
/* If already enabled, just check consistency */ |
| 299 |
if (e->tp) { |
| 300 |
if (e->tp != tp) { |
| 301 |
dbug_tp(1, "found duplicate '%s'\n", tp->name); |
| 302 |
} |
| 303 |
continue; |
| 304 |
} |
| 305 |
e->tp = tp; |
| 306 |
e->refcount++; |
| 307 |
dbug_tp(2, "added %s:%s\n", tp_mod->mod->name, tp->name); |
| 308 |
/* register each (probe, data) */ |
| 309 |
list_for_each_entry(p, &e->probes, list) { |
| 310 |
do_tracepoint_probe_register(e->tp, |
| 311 |
p->tp_func.func, p->tp_func.data); |
| 312 |
} |
| 313 |
} |
| 314 |
mutex_unlock(&stp_tracepoint_mutex); |
| 315 |
return NOTIFY_OK; /* may be more appropriate than NOTIFY_DONE=0; https://lkml.org/lkml/2019/6/24/880 */ |
| 316 |
} |
| 317 |
|
| 318 |
static |
| 319 |
int stp_tracepoint_going(struct tp_module *tp_mod) |
| 320 |
{ |
| 321 |
int i; |
| 322 |
|
| 323 |
might_sleep(); |
| 324 |
mutex_lock(&stp_tracepoint_mutex); |
| 325 |
for (i = 0; i < tp_mod->mod->num_tracepoints; i++) { |
| 326 |
struct tracepoint *tp; |
| 327 |
struct tracepoint_entry *e; |
| 328 |
struct stp_tp_probe *p; |
| 329 |
|
| 330 |
#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS |
| 331 |
tp = offset_to_ptr(&(((const int*)(tp_mod->mod->tracepoints_ptrs))[i])); |
| 332 |
#else |
| 333 |
tp = tp_mod->mod->tracepoints_ptrs[i]; |
| 334 |
#endif |
| 335 |
e = get_tracepoint(tp->name); |
| 336 |
if (!e || !e->tp) |
| 337 |
continue; |
| 338 |
/* unregister each (probe, data) */ |
| 339 |
list_for_each_entry(p, &e->probes, list) { |
| 340 |
do_tracepoint_probe_unregister(e->tp, |
| 341 |
p->tp_func.func, p->tp_func.data); |
| 342 |
} |
| 343 |
e->tp = NULL; |
| 344 |
if (!--e->refcount) |
| 345 |
remove_tracepoint(e); |
| 346 |
dbug_tp(2, "removed %s:%s\n", tp_mod->mod->name, tp->name); |
| 347 |
} |
| 348 |
mutex_unlock(&stp_tracepoint_mutex); |
| 349 |
return 0; |
| 350 |
} |
| 351 |
|
| 352 |
static |
| 353 |
int stp_tracepoint_notify(struct notifier_block *self, |
| 354 |
unsigned long val, void *data) |
| 355 |
{ |
| 356 |
struct tp_module *tp_mod = data; |
| 357 |
int ret = 0; |
| 358 |
|
| 359 |
switch (val) { |
| 360 |
case MODULE_STATE_COMING: |
| 361 |
ret = stp_tracepoint_coming(tp_mod); |
| 362 |
break; |
| 363 |
case MODULE_STATE_GOING: |
| 364 |
ret = stp_tracepoint_going(tp_mod); |
| 365 |
break; |
| 366 |
default: |
| 367 |
break; |
| 368 |
} |
| 369 |
return ret; |
| 370 |
} |
| 371 |
|
| 372 |
static |
| 373 |
struct notifier_block stp_tracepoint_notifier = { |
| 374 |
.notifier_call = stp_tracepoint_notify, |
| 375 |
.priority = 0, |
| 376 |
}; |
| 377 |
|
| 378 |
static |
| 379 |
int stp_tracepoint_module_init(void) |
| 380 |
{ |
| 381 |
return register_tracepoint_module_notifier(&stp_tracepoint_notifier); |
| 382 |
} |
| 383 |
|
| 384 |
static |
| 385 |
void stp_tracepoint_module_exit(void) |
| 386 |
{ |
| 387 |
(void)unregister_tracepoint_module_notifier(&stp_tracepoint_notifier); |
| 388 |
} |
| 389 |
|
| 390 |
#else /* #ifdef CONFIG_MODULES */ |
| 391 |
|
| 392 |
static |
| 393 |
int stp_tracepoint_module_init(void) |
| 394 |
{ |
| 395 |
return 0; |
| 396 |
} |
| 397 |
|
| 398 |
static |
| 399 |
void stp_tracepoint_module_exit(void) |
| 400 |
{ |
| 401 |
} |
| 402 |
|
| 403 |
#endif /* #else #ifdef CONFIG_MODULES */ |
| 404 |
|
| 405 |
static |
| 406 |
void stp_kernel_tracepoint_add(struct tracepoint *tp, void *priv) |
| 407 |
{ |
| 408 |
struct tracepoint_entry *e; |
| 409 |
struct stp_tp_probe *p; |
| 410 |
int *ret = priv; |
| 411 |
|
| 412 |
might_sleep(); |
| 413 |
mutex_lock(&stp_tracepoint_mutex); |
| 414 |
e = get_tracepoint(tp->name); |
| 415 |
if (!e) { |
| 416 |
e = add_tracepoint(tp->name); |
| 417 |
if (IS_ERR(e)) { |
| 418 |
dbug_tp(1, "error (%ld) adding kernel:%s\n", |
| 419 |
PTR_ERR(e), tp->name); |
| 420 |
*ret = (int) PTR_ERR(e); |
| 421 |
goto end; |
| 422 |
} |
| 423 |
} |
| 424 |
/* If already enabled, just check consistency */ |
| 425 |
if (e->tp) { |
| 426 |
if (e->tp != tp) { |
| 427 |
dbug_tp(1, "found duplicate '%s'\n", tp->name); |
| 428 |
} |
| 429 |
goto end; |
| 430 |
} |
| 431 |
e->tp = tp; |
| 432 |
e->refcount++; |
| 433 |
dbug_tp(2, "added kernel:%s\n", tp->name); |
| 434 |
/* register each (probe, data) */ |
| 435 |
list_for_each_entry(p, &e->probes, list) { |
| 436 |
do_tracepoint_probe_register(e->tp, |
| 437 |
p->tp_func.func, p->tp_func.data); |
| 438 |
} |
| 439 |
end: |
| 440 |
mutex_unlock(&stp_tracepoint_mutex); |
| 441 |
} |
| 442 |
|
| 443 |
static |
| 444 |
void stp_kernel_tracepoint_remove(struct tracepoint *tp, void *priv) |
| 445 |
{ |
| 446 |
struct tracepoint_entry *e; |
| 447 |
int *ret = priv; |
| 448 |
|
| 449 |
might_sleep(); |
| 450 |
mutex_lock(&stp_tracepoint_mutex); |
| 451 |
e = get_tracepoint(tp->name); |
| 452 |
if (!e || e->refcount != 1 || !list_empty(&e->probes)) { |
| 453 |
*ret = -EINVAL; |
| 454 |
goto end; |
| 455 |
} |
| 456 |
remove_tracepoint(e); |
| 457 |
dbug_tp(2, "removed kernel:%s\n", tp->name); |
| 458 |
end: |
| 459 |
mutex_unlock(&stp_tracepoint_mutex); |
| 460 |
} |
| 461 |
|
| 462 |
static |
| 463 |
int stp_tracepoint_init(void) |
| 464 |
{ |
| 465 |
int error_ret = 0; |
| 466 |
int ret = 0; |
| 467 |
|
| 468 |
for_each_kernel_tracepoint(stp_kernel_tracepoint_add, &ret); |
| 469 |
if (!ret) |
| 470 |
ret = stp_tracepoint_module_init(); |
| 471 |
if (!ret) |
| 472 |
return 0; |
| 473 |
|
| 474 |
for_each_kernel_tracepoint(stp_kernel_tracepoint_remove, &error_ret); |
| 475 |
return ret; |
| 476 |
} |
| 477 |
|
| 478 |
static |
| 479 |
void stp_tracepoint_exit(void) |
| 480 |
{ |
| 481 |
int i, ret = 0; |
| 482 |
|
| 483 |
stp_tracepoint_module_exit(); |
| 484 |
for_each_kernel_tracepoint(stp_kernel_tracepoint_remove, &ret); |
| 485 |
might_sleep(); |
| 486 |
mutex_lock(&stp_tracepoint_mutex); |
| 487 |
for (i = 0; i < TRACEPOINT_TABLE_SIZE; i++) { |
| 488 |
struct hlist_head *head = &tracepoint_table[i]; |
| 489 |
|
| 490 |
/* All tracepoints should be removed */ |
| 491 |
if (!hlist_empty(head)) { |
| 492 |
dbug_tp(1, "tracepoint_table[%d] is not empty!\n", i); |
| 493 |
} |
| 494 |
} |
| 495 |
mutex_unlock(&stp_tracepoint_mutex); |
| 496 |
} |