1 // SPDX-License-Identifier: GPL-2.0 2 3 #include <test_progs.h> 4 #include <bpf/btf.h> 5 #include <search.h> 6 #include "bpf/libbpf_internal.h" 7 #include "tracing_multi.skel.h" 8 #include "tracing_multi_module.skel.h" 9 #include "tracing_multi_intersect.skel.h" 10 #include "tracing_multi_session.skel.h" 11 #include "tracing_multi_fail.skel.h" 12 #include "tracing_multi_verifier.skel.h" 13 #include "tracing_multi_bench.skel.h" 14 #include "tracing_multi_rollback.skel.h" 15 #include "trace_helpers.h" 16 17 static __u64 bpf_fentry_test_cookies[] = { 18 8, /* bpf_fentry_test1 */ 19 9, /* bpf_fentry_test2 */ 20 7, /* bpf_fentry_test3 */ 21 5, /* bpf_fentry_test4 */ 22 4, /* bpf_fentry_test5 */ 23 2, /* bpf_fentry_test6 */ 24 3, /* bpf_fentry_test7 */ 25 1, /* bpf_fentry_test8 */ 26 10, /* bpf_fentry_test9 */ 27 6, /* bpf_fentry_test10 */ 28 }; 29 30 static const char * const bpf_fentry_test[] = { 31 "bpf_fentry_test1", 32 "bpf_fentry_test2", 33 "bpf_fentry_test3", 34 "bpf_fentry_test4", 35 "bpf_fentry_test5", 36 "bpf_fentry_test6", 37 "bpf_fentry_test7", 38 "bpf_fentry_test8", 39 "bpf_fentry_test9", 40 "bpf_fentry_test10", 41 }; 42 43 static const char * const bpf_testmod_fentry_test[] = { 44 "bpf_testmod_fentry_test1", 45 "bpf_testmod_fentry_test2", 46 "bpf_testmod_fentry_test3", 47 "bpf_testmod_fentry_test7", 48 "bpf_testmod_fentry_test11", 49 }; 50 51 #define FUNCS_CNT (ARRAY_SIZE(bpf_fentry_test)) 52 53 static int get_random_funcs(const char **funcs) 54 { 55 int i, cnt = 0; 56 57 for (i = 0; i < FUNCS_CNT; i++) { 58 if (rand() % 2) 59 funcs[cnt++] = bpf_fentry_test[i]; 60 } 61 /* we always need at least one.. */ 62 if (!cnt) 63 funcs[cnt++] = bpf_fentry_test[rand() % FUNCS_CNT]; 64 return cnt; 65 } 66 67 static int compare(const void *ppa, const void *ppb) 68 { 69 const char *pa = *(const char **) ppa; 70 const char *pb = *(const char **) ppb; 71 72 return strcmp(pa, pb); 73 } 74 75 static void tdestroy_free_nop(void *ptr) 76 { 77 } 78 79 static __u32 *get_ids(const char * const funcs[], int funcs_cnt, const char *mod) 80 { 81 struct btf *btf, *vmlinux_btf = NULL; 82 __u32 nr, type_id, cnt = 0; 83 void *root = NULL; 84 __u32 *ids = NULL; 85 int i, err = 0; 86 87 btf = btf__load_vmlinux_btf(); 88 if (!ASSERT_OK_PTR(btf, "btf__load_vmlinux_btf")) 89 return NULL; 90 91 if (mod) { 92 vmlinux_btf = btf; 93 btf = btf__load_module_btf(mod, vmlinux_btf); 94 if (!ASSERT_OK_PTR(btf, "btf__load_module_btf")) { 95 btf__free(vmlinux_btf); 96 return NULL; 97 } 98 } 99 100 ids = calloc(funcs_cnt, sizeof(ids[0])); 101 if (!ids) 102 goto out; 103 104 /* 105 * We sort function names by name and search them 106 * below for each function. 107 */ 108 for (i = 0; i < funcs_cnt; i++) { 109 if (!tsearch(&funcs[i], &root, compare)) { 110 ASSERT_FAIL("tsearch failed"); 111 err = -1; 112 goto error; 113 } 114 } 115 116 nr = btf__type_cnt(btf); 117 for (type_id = 1; type_id < nr && cnt < funcs_cnt; type_id++) { 118 const struct btf_type *type; 119 const char *str, ***val; 120 unsigned int idx; 121 122 type = btf__type_by_id(btf, type_id); 123 if (!type) { 124 err = -1; 125 break; 126 } 127 128 if (BTF_INFO_KIND(type->info) != BTF_KIND_FUNC) 129 continue; 130 131 str = btf__name_by_offset(btf, type->name_off); 132 if (!str) { 133 err = -1; 134 break; 135 } 136 137 val = tfind(&str, &root, compare); 138 if (!val) 139 continue; 140 141 /* 142 * We keep pointer for each function name so we can get the original 143 * array index and have the resulting ids array matching the original 144 * function array. 145 * 146 * Doing it this way allow us to easily test the cookies support, 147 * because each cookie is attached to particular function/id. 148 */ 149 idx = *val - funcs; 150 ids[idx] = type_id; 151 cnt++; 152 } 153 154 error: 155 if (err) { 156 free(ids); 157 ids = NULL; 158 } 159 160 out: 161 tdestroy(root, tdestroy_free_nop); 162 btf__free(vmlinux_btf); 163 btf__free(btf); 164 return ids; 165 } 166 167 static void tracing_multi_test_run(struct tracing_multi *skel) 168 { 169 LIBBPF_OPTS(bpf_test_run_opts, topts); 170 int err, prog_fd; 171 172 prog_fd = bpf_program__fd(skel->progs.test_fentry); 173 err = bpf_prog_test_run_opts(prog_fd, &topts); 174 ASSERT_OK(err, "test_run"); 175 176 /* extra +1 count for sleepable programs */ 177 ASSERT_EQ(skel->bss->test_result_fentry, FUNCS_CNT + 1, "test_result_fentry"); 178 ASSERT_EQ(skel->bss->test_result_fexit, FUNCS_CNT + 1, "test_result_fexit"); 179 } 180 181 static void test_skel_api(void) 182 { 183 struct tracing_multi *skel; 184 int err; 185 186 skel = tracing_multi__open_and_load(); 187 if (!ASSERT_OK_PTR(skel, "tracing_multi__open_and_load")) 188 return; 189 190 skel->bss->pid = getpid(); 191 192 err = tracing_multi__attach(skel); 193 if (!ASSERT_OK(err, "tracing_multi__attach")) 194 goto cleanup; 195 196 tracing_multi_test_run(skel); 197 198 cleanup: 199 tracing_multi__destroy(skel); 200 } 201 202 static void test_link_api_pattern(void) 203 { 204 struct tracing_multi *skel; 205 206 skel = tracing_multi__open_and_load(); 207 if (!ASSERT_OK_PTR(skel, "tracing_multi__open_and_load")) 208 return; 209 210 skel->bss->pid = getpid(); 211 212 skel->links.test_fentry = bpf_program__attach_tracing_multi(skel->progs.test_fentry, 213 "bpf_fentry_test*", NULL); 214 if (!ASSERT_OK_PTR(skel->links.test_fentry, "bpf_program__attach_tracing_multi")) 215 goto cleanup; 216 217 skel->links.test_fexit = bpf_program__attach_tracing_multi(skel->progs.test_fexit, 218 "bpf_fentry_test*", NULL); 219 if (!ASSERT_OK_PTR(skel->links.test_fexit, "bpf_program__attach_tracing_multi")) 220 goto cleanup; 221 222 skel->links.test_fentry_s = bpf_program__attach_tracing_multi(skel->progs.test_fentry_s, 223 "bpf_fentry_test1", NULL); 224 if (!ASSERT_OK_PTR(skel->links.test_fentry_s, "bpf_program__attach_tracing_multi")) 225 goto cleanup; 226 227 skel->links.test_fexit_s = bpf_program__attach_tracing_multi(skel->progs.test_fexit_s, 228 "bpf_fentry_test1", NULL); 229 if (!ASSERT_OK_PTR(skel->links.test_fexit_s, "bpf_program__attach_tracing_multi")) 230 goto cleanup; 231 232 tracing_multi_test_run(skel); 233 234 cleanup: 235 tracing_multi__destroy(skel); 236 } 237 238 static void test_link_api_ids(bool test_cookies) 239 { 240 LIBBPF_OPTS(bpf_tracing_multi_opts, opts); 241 struct tracing_multi *skel; 242 size_t cnt = FUNCS_CNT; 243 __u32 *ids; 244 245 skel = tracing_multi__open_and_load(); 246 if (!ASSERT_OK_PTR(skel, "tracing_multi__open_and_load")) 247 return; 248 249 skel->bss->pid = getpid(); 250 skel->bss->test_cookies = test_cookies; 251 252 ids = get_ids(bpf_fentry_test, cnt, NULL); 253 if (!ASSERT_OK_PTR(ids, "get_ids")) 254 goto cleanup; 255 256 opts.ids = ids; 257 opts.cnt = cnt; 258 259 if (test_cookies) 260 opts.cookies = bpf_fentry_test_cookies; 261 262 skel->links.test_fentry = bpf_program__attach_tracing_multi(skel->progs.test_fentry, 263 NULL, &opts); 264 if (!ASSERT_OK_PTR(skel->links.test_fentry, "bpf_program__attach_tracing_multi")) 265 goto cleanup; 266 267 skel->links.test_fexit = bpf_program__attach_tracing_multi(skel->progs.test_fexit, 268 NULL, &opts); 269 if (!ASSERT_OK_PTR(skel->links.test_fexit, "bpf_program__attach_tracing_multi")) 270 goto cleanup; 271 272 /* Only bpf_fentry_test1 is allowed for sleepable programs. */ 273 opts.cnt = 1; 274 skel->links.test_fentry_s = bpf_program__attach_tracing_multi(skel->progs.test_fentry_s, 275 NULL, &opts); 276 if (!ASSERT_OK_PTR(skel->links.test_fentry_s, "bpf_program__attach_tracing_multi")) 277 goto cleanup; 278 279 skel->links.test_fexit_s = bpf_program__attach_tracing_multi(skel->progs.test_fexit_s, 280 NULL, &opts); 281 if (!ASSERT_OK_PTR(skel->links.test_fexit_s, "bpf_program__attach_tracing_multi")) 282 goto cleanup; 283 284 tracing_multi_test_run(skel); 285 286 cleanup: 287 tracing_multi__destroy(skel); 288 free(ids); 289 } 290 291 static void test_module_skel_api(void) 292 { 293 struct tracing_multi_module *skel = NULL; 294 int err; 295 296 skel = tracing_multi_module__open_and_load(); 297 if (!ASSERT_OK_PTR(skel, "tracing_multi__open_and_load")) 298 return; 299 300 skel->bss->pid = getpid(); 301 302 err = tracing_multi_module__attach(skel); 303 if (!ASSERT_OK(err, "tracing_multi__attach")) 304 goto cleanup; 305 306 ASSERT_OK(trigger_module_test_read(1), "trigger_read"); 307 ASSERT_EQ(skel->bss->test_result_fentry, 5, "test_result_fentry"); 308 ASSERT_EQ(skel->bss->test_result_fexit, 5, "test_result_fexit"); 309 310 cleanup: 311 tracing_multi_module__destroy(skel); 312 } 313 314 static void test_module_link_api_pattern(void) 315 { 316 struct tracing_multi_module *skel = NULL; 317 318 skel = tracing_multi_module__open_and_load(); 319 if (!ASSERT_OK_PTR(skel, "tracing_multi_module__open_and_load")) 320 return; 321 322 skel->bss->pid = getpid(); 323 324 skel->links.test_fentry = bpf_program__attach_tracing_multi(skel->progs.test_fentry, 325 "bpf_testmod:bpf_testmod_fentry_test*", NULL); 326 if (!ASSERT_OK_PTR(skel->links.test_fentry, "bpf_program__attach_tracing_multi")) 327 goto cleanup; 328 329 skel->links.test_fexit = bpf_program__attach_tracing_multi(skel->progs.test_fexit, 330 "bpf_testmod:bpf_testmod_fentry_test*", NULL); 331 if (!ASSERT_OK_PTR(skel->links.test_fexit, "bpf_program__attach_tracing_multi")) 332 goto cleanup; 333 334 ASSERT_OK(trigger_module_test_read(1), "trigger_read"); 335 ASSERT_EQ(skel->bss->test_result_fentry, 5, "test_result_fentry"); 336 ASSERT_EQ(skel->bss->test_result_fexit, 5, "test_result_fexit"); 337 338 cleanup: 339 tracing_multi_module__destroy(skel); 340 } 341 342 static void test_module_link_api_ids(void) 343 { 344 size_t cnt = ARRAY_SIZE(bpf_testmod_fentry_test); 345 LIBBPF_OPTS(bpf_tracing_multi_opts, opts); 346 struct tracing_multi_module *skel = NULL; 347 __u32 *ids; 348 349 skel = tracing_multi_module__open_and_load(); 350 if (!ASSERT_OK_PTR(skel, "tracing_multi_module__open_and_load")) 351 return; 352 353 skel->bss->pid = getpid(); 354 355 ids = get_ids(bpf_testmod_fentry_test, cnt, "bpf_testmod"); 356 if (!ASSERT_OK_PTR(ids, "get_ids")) 357 goto cleanup; 358 359 opts.ids = ids; 360 opts.cnt = cnt; 361 362 skel->links.test_fentry = bpf_program__attach_tracing_multi(skel->progs.test_fentry, 363 NULL, &opts); 364 if (!ASSERT_OK_PTR(skel->links.test_fentry, "bpf_program__attach_tracing_multi")) 365 goto cleanup; 366 367 skel->links.test_fexit = bpf_program__attach_tracing_multi(skel->progs.test_fexit, 368 NULL, &opts); 369 if (!ASSERT_OK_PTR(skel->links.test_fexit, "bpf_program__attach_tracing_multi")) 370 goto cleanup; 371 372 ASSERT_OK(trigger_module_test_read(1), "trigger_read"); 373 ASSERT_EQ(skel->bss->test_result_fentry, 5, "test_result_fentry"); 374 ASSERT_EQ(skel->bss->test_result_fexit, 5, "test_result_fexit"); 375 376 cleanup: 377 tracing_multi_module__destroy(skel); 378 free(ids); 379 } 380 381 static bool is_set(__u32 mask, __u32 bit) 382 { 383 return (1 << bit) & mask; 384 } 385 386 static void __test_intersect(__u32 mask, const struct bpf_program *progs[4], __u64 *test_results[4]) 387 { 388 LIBBPF_OPTS(bpf_tracing_multi_opts, opts); 389 LIBBPF_OPTS(bpf_test_run_opts, topts); 390 struct bpf_link *links[4] = { NULL }; 391 const char *funcs[FUNCS_CNT]; 392 __u64 expected[4]; 393 __u32 *ids, i; 394 int err, cnt; 395 396 /* 397 * We have 4 programs in progs and the mask bits pick which 398 * of them gets attached to randomly chosen functions. 399 */ 400 for (i = 0; i < 4; i++) { 401 if (!is_set(mask, i)) 402 continue; 403 404 cnt = get_random_funcs(funcs); 405 ids = get_ids(funcs, cnt, NULL); 406 if (!ASSERT_OK_PTR(ids, "get_ids")) 407 goto cleanup; 408 409 opts.ids = ids; 410 opts.cnt = cnt; 411 links[i] = bpf_program__attach_tracing_multi(progs[i], NULL, &opts); 412 free(ids); 413 414 if (!ASSERT_OK_PTR(links[i], "bpf_program__attach_tracing_multi")) 415 goto cleanup; 416 417 expected[i] = *test_results[i] + cnt; 418 } 419 420 err = bpf_prog_test_run_opts(bpf_program__fd(progs[0]), &topts); 421 ASSERT_OK(err, "test_run"); 422 423 for (i = 0; i < 4; i++) { 424 if (!is_set(mask, i)) 425 continue; 426 ASSERT_EQ(*test_results[i], expected[i], "test_results"); 427 } 428 429 cleanup: 430 for (i = 0; i < 4; i++) 431 bpf_link__destroy(links[i]); 432 } 433 434 static void test_intersect(void) 435 { 436 struct tracing_multi_intersect *skel; 437 const struct bpf_program *progs[4]; 438 __u64 *test_results[4]; 439 __u32 i; 440 441 skel = tracing_multi_intersect__open_and_load(); 442 if (!ASSERT_OK_PTR(skel, "tracing_multi_intersect__open_and_load")) 443 return; 444 445 skel->bss->pid = getpid(); 446 447 progs[0] = skel->progs.fentry_1; 448 progs[1] = skel->progs.fexit_1; 449 progs[2] = skel->progs.fentry_2; 450 progs[3] = skel->progs.fexit_2; 451 452 test_results[0] = &skel->bss->test_result_fentry_1; 453 test_results[1] = &skel->bss->test_result_fexit_1; 454 test_results[2] = &skel->bss->test_result_fentry_2; 455 test_results[3] = &skel->bss->test_result_fexit_2; 456 457 for (i = 1; i < 16; i++) 458 __test_intersect(i, progs, test_results); 459 460 tracing_multi_intersect__destroy(skel); 461 } 462 463 static void test_fentry_after_multi(void) 464 { 465 static const char * const funcs[] = { 466 "bpf_fentry_test1", 467 }; 468 struct bpf_link *fentry_link = NULL, *multi_link = NULL; 469 struct tracing_multi_intersect *skel = NULL; 470 LIBBPF_OPTS(bpf_tracing_multi_opts, opts); 471 LIBBPF_OPTS(bpf_test_run_opts, topts); 472 __u32 *ids = NULL; 473 int err; 474 475 skel = tracing_multi_intersect__open_and_load(); 476 if (!ASSERT_OK_PTR(skel, "tracing_multi_intersect__open_and_load")) 477 return; 478 479 skel->bss->pid = getpid(); 480 481 ids = get_ids(funcs, ARRAY_SIZE(funcs), NULL); 482 if (!ASSERT_OK_PTR(ids, "get_ids")) 483 goto cleanup; 484 485 opts.ids = ids; 486 opts.cnt = ARRAY_SIZE(funcs); 487 multi_link = bpf_program__attach_tracing_multi(skel->progs.fentry_1, NULL, &opts); 488 if (!ASSERT_OK_PTR(multi_link, "attach_multi")) 489 goto cleanup; 490 491 fentry_link = bpf_program__attach(skel->progs.fentry); 492 if (!ASSERT_OK_PTR(fentry_link, "attach_fentry")) 493 goto cleanup; 494 495 err = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.fentry_1), &topts); 496 if (!ASSERT_OK(err, "test_run")) 497 goto cleanup; 498 ASSERT_EQ(skel->bss->test_result_fentry_1, 1, "multi_fentry"); 499 ASSERT_EQ(skel->bss->test_result_fentry, 1, "fentry"); 500 501 err = bpf_link__destroy(fentry_link); 502 fentry_link = NULL; 503 if (!ASSERT_OK(err, "destroy_fentry")) 504 goto cleanup; 505 506 err = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.fentry_1), &topts); 507 if (!ASSERT_OK(err, "test_run_multi")) 508 goto cleanup; 509 ASSERT_EQ(skel->bss->test_result_fentry_1, 2, "multi_fentry_only"); 510 ASSERT_EQ(skel->bss->test_result_fentry, 1, "fentry_detached"); 511 512 err = bpf_link__destroy(multi_link); 513 multi_link = NULL; 514 if (!ASSERT_OK(err, "destroy_multi")) 515 goto cleanup; 516 517 err = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.fentry_1), &topts); 518 if (!ASSERT_OK(err, "test_run_detached")) 519 goto cleanup; 520 ASSERT_EQ(skel->bss->test_result_fentry_1, 2, "multi_fentry_detached"); 521 ASSERT_EQ(skel->bss->test_result_fentry, 1, "fentry_still_detached"); 522 523 cleanup: 524 bpf_link__destroy(fentry_link); 525 bpf_link__destroy(multi_link); 526 free(ids); 527 tracing_multi_intersect__destroy(skel); 528 } 529 530 static void test_session(void) 531 { 532 LIBBPF_OPTS(bpf_test_run_opts, topts); 533 struct tracing_multi_session *skel; 534 int err, prog_fd; 535 536 skel = tracing_multi_session__open_and_load(); 537 if (!ASSERT_OK_PTR(skel, "tracing_multi_session__open_and_load")) 538 return; 539 540 skel->bss->pid = getpid(); 541 542 err = tracing_multi_session__attach(skel); 543 if (!ASSERT_OK(err, "tracing_multi_session__attach")) 544 goto cleanup; 545 546 /* execute kernel session */ 547 prog_fd = bpf_program__fd(skel->progs.test_session_1); 548 err = bpf_prog_test_run_opts(prog_fd, &topts); 549 ASSERT_OK(err, "test_run"); 550 551 /* 10 for test_session_1, 1 for test_fsession_s */ 552 ASSERT_EQ(skel->bss->test_result_fentry, 11, "test_result_fentry"); 553 /* extra count (+1 for each fexit execution) for test_result_fexit cookie check/inc */ 554 ASSERT_EQ(skel->bss->test_result_fexit, 22, "test_result_fexit"); 555 556 skel->bss->test_result_fentry = 0; 557 skel->bss->test_result_fexit = 0; 558 559 /* execute bpf_testmo.ko session */ 560 ASSERT_OK(trigger_module_test_read(1), "trigger_read"); 561 562 /* 5 for test_session_2 */ 563 ASSERT_EQ(skel->bss->test_result_fentry, 5, "test_result_fentry"); 564 /* extra count (+1 for each fexit execution) for test_result_fexit cookie */ 565 ASSERT_EQ(skel->bss->test_result_fexit, 10, "test_result_fexit"); 566 567 568 cleanup: 569 tracing_multi_session__destroy(skel); 570 } 571 572 static void test_attach_api_fails(void) 573 { 574 LIBBPF_OPTS(bpf_tracing_multi_opts, opts); 575 static const char * const func[] = { 576 "bpf_fentry_test2", 577 }; 578 struct tracing_multi_fail *skel = NULL; 579 __u32 ids[2] = {}, *ids2 = NULL; 580 __u64 cookies[2]; 581 582 skel = tracing_multi_fail__open_and_load(); 583 if (!ASSERT_OK_PTR(skel, "tracing_multi_fail__open_and_load")) 584 return; 585 586 /* fail#1 (libbpf) pattern and opts NULL */ 587 skel->links.test_fentry = bpf_program__attach_tracing_multi(skel->progs.test_fentry, 588 NULL, NULL); 589 if (!ASSERT_EQ(libbpf_get_error(skel->links.test_fentry), -EINVAL, "fail_1")) 590 goto cleanup; 591 592 /* fail#2 (libbpf) pattern and ids */ 593 LIBBPF_OPTS_RESET(opts, 594 .ids = ids, 595 .cnt = 2, 596 ); 597 598 skel->links.test_fentry = bpf_program__attach_tracing_multi(skel->progs.test_fentry, 599 "bpf_fentry_test*", &opts); 600 if (!ASSERT_EQ(libbpf_get_error(skel->links.test_fentry), -EINVAL, "fail_2")) 601 goto cleanup; 602 603 /* fail#3 (libbpf) pattern and cookies */ 604 LIBBPF_OPTS_RESET(opts, 605 .ids = NULL, 606 .cnt = 2, 607 .cookies = cookies, 608 ); 609 610 skel->links.test_fentry = bpf_program__attach_tracing_multi(skel->progs.test_fentry, 611 "bpf_fentry_test*", &opts); 612 if (!ASSERT_EQ(libbpf_get_error(skel->links.test_fentry), -EINVAL, "fail_3")) 613 goto cleanup; 614 615 /* fail#4 (libbpf) bogus pattern */ 616 skel->links.test_fentry = bpf_program__attach_tracing_multi(skel->progs.test_fentry, 617 "bpf_not_really_a_function*", NULL); 618 if (!ASSERT_EQ(libbpf_get_error(skel->links.test_fentry), -EINVAL, "fail_4")) 619 goto cleanup; 620 621 /* fail#5 (kernel) abnormal cnt */ 622 LIBBPF_OPTS_RESET(opts, 623 .ids = ids, 624 .cnt = INT_MAX, 625 ); 626 627 skel->links.test_fentry = bpf_program__attach_tracing_multi(skel->progs.test_fentry, 628 NULL, &opts); 629 if (!ASSERT_EQ(libbpf_get_error(skel->links.test_fentry), -E2BIG, "fail_5")) 630 goto cleanup; 631 632 /* fail#6 (kernel) attach sleepable program to not-allowed function */ 633 ids2 = get_ids(func, 1, NULL); 634 if (!ASSERT_OK_PTR(ids2, "get_ids")) 635 goto cleanup; 636 637 LIBBPF_OPTS_RESET(opts, 638 .ids = ids2, 639 .cnt = 1, 640 ); 641 642 skel->links.test_fentry_s = bpf_program__attach_tracing_multi(skel->progs.test_fentry_s, 643 NULL, &opts); 644 if (!ASSERT_EQ(libbpf_get_error(skel->links.test_fentry_s), -EINVAL, "fail_6")) 645 goto cleanup; 646 647 /* fail#7 (kernel) attach with duplicate id */ 648 ids[0] = ids2[0]; 649 ids[1] = ids2[0]; 650 651 LIBBPF_OPTS_RESET(opts, 652 .ids = ids, 653 .cnt = 2, 654 ); 655 656 skel->links.test_fentry = bpf_program__attach_tracing_multi(skel->progs.test_fentry, 657 NULL, &opts); 658 ASSERT_EQ(libbpf_get_error(skel->links.test_fentry), -EINVAL, "fail_7"); 659 660 cleanup: 661 tracing_multi_fail__destroy(skel); 662 free(ids2); 663 } 664 665 /* 666 * Find two BTF ids that resolve to the same address. The stub of a syscall 667 * without arguments is an alias of its __do_sys_ function. 668 */ 669 static bool get_same_addr_ids(__u32 *ids) 670 { 671 const char *prefix = "__x64_sys_"; 672 __u32 nr, type_id; 673 bool found = false; 674 struct btf *btf; 675 char alias[128]; 676 677 btf = btf__load_vmlinux_btf(); 678 if (!ASSERT_OK_PTR(btf, "btf__load_vmlinux_btf")) 679 return false; 680 681 nr = btf__type_cnt(btf); 682 for (type_id = 1; type_id < nr && !found; type_id++) { 683 const struct btf_type *type = btf__type_by_id(btf, type_id); 684 const char *str; 685 long addr; 686 __s32 id; 687 688 if (!btf_is_func(type)) 689 continue; 690 691 str = btf__name_by_offset(btf, type->name_off); 692 if (!str || strncmp(str, prefix, strlen(prefix))) 693 continue; 694 695 snprintf(alias, sizeof(alias), "__do_sys_%s", str + strlen(prefix)); 696 id = btf__find_by_name_kind(btf, alias, BTF_KIND_FUNC); 697 if (id <= 0) 698 continue; 699 700 addr = ksym_get_addr(str); 701 if (!addr || addr != ksym_get_addr(alias)) 702 continue; 703 704 ids[0] = type_id; 705 ids[1] = id; 706 found = true; 707 } 708 709 btf__free(btf); 710 return found; 711 } 712 713 static void test_attach_same_addr(void) 714 { 715 LIBBPF_OPTS(bpf_tracing_multi_opts, opts); 716 struct tracing_multi_fail *skel; 717 struct bpf_link *link; 718 __u32 ids[2]; 719 int i; 720 721 if (!ASSERT_OK(load_kallsyms(), "load_kallsyms")) 722 return; 723 724 if (!get_same_addr_ids(ids)) { 725 test__skip(); 726 return; 727 } 728 729 skel = tracing_multi_fail__open_and_load(); 730 if (!ASSERT_OK_PTR(skel, "tracing_multi_fail__open_and_load")) 731 return; 732 733 /* Each id attaches fine on its own.. */ 734 for (i = 0; i < 2; i++) { 735 opts.ids = &ids[i]; 736 opts.cnt = 1; 737 738 link = bpf_program__attach_tracing_multi(skel->progs.test_fentry, NULL, &opts); 739 if (!ASSERT_OK_PTR(link, "attach_single")) 740 goto cleanup; 741 bpf_link__destroy(link); 742 } 743 744 /* ..but there's only one call site, so not both of them. */ 745 opts.ids = ids; 746 opts.cnt = 2; 747 748 link = bpf_program__attach_tracing_multi(skel->progs.test_fentry, NULL, &opts); 749 if (!ASSERT_EQ(libbpf_get_error(link), -EINVAL, "attach_same_addr")) 750 bpf_link__destroy(link); 751 752 cleanup: 753 tracing_multi_fail__destroy(skel); 754 } 755 756 void serial_test_tracing_multi_bench_attach(void) 757 { 758 LIBBPF_OPTS(bpf_tracing_multi_opts, opts); 759 struct tracing_multi_bench *skel = NULL; 760 long attach_start_ns, attach_end_ns; 761 long detach_start_ns, detach_end_ns; 762 double attach_delta, detach_delta; 763 struct bpf_link *link = NULL; 764 size_t i, cap = 0, cnt = 0; 765 struct ksyms *ksyms = NULL; 766 void *root = NULL; 767 void *dups = NULL; 768 __u32 *ids = NULL; 769 __u32 nr, type_id; 770 struct btf *btf; 771 int err; 772 773 #ifndef __x86_64__ 774 test__skip(); 775 return; 776 #endif 777 778 btf = btf__load_vmlinux_btf(); 779 if (!ASSERT_OK_PTR(btf, "btf__load_vmlinux_btf")) 780 return; 781 782 skel = tracing_multi_bench__open_and_load(); 783 if (!ASSERT_OK_PTR(skel, "tracing_multi_bench__open_and_load")) 784 goto cleanup; 785 786 if (!ASSERT_OK(bpf_get_ksyms(&ksyms, true), "get_syms")) 787 goto cleanup; 788 789 /* Get all ftrace 'safe' symbols.. */ 790 for (i = 0; i < ksyms->filtered_cnt; i++) { 791 if (!tsearch(&ksyms->filtered_syms[i], &root, compare)) { 792 ASSERT_FAIL("tsearch failed"); 793 goto cleanup; 794 } 795 } 796 797 /* 798 * Collect names that are not unique in kallsyms. The kernel resolves a 799 * tracing-multi BTF id to an address with kallsyms_lookup_name(), which 800 * returns the first symbol of that name. For a duplicate name that may 801 * be a different (non-ftrace-able) instance than the ftrace-able one in 802 * available_filter_functions, so attaching to it by BTF id fails with 803 * -ENOENT (e.g. t_start/t_next/t_stop). ksyms->syms is sorted by name, 804 * so equal names are adjacent. 805 */ 806 for (i = 1; i < ksyms->sym_cnt; i++) { 807 if (strcmp(ksyms->syms[i].name, ksyms->syms[i - 1].name)) 808 continue; 809 if (!tsearch(&ksyms->syms[i].name, &dups, compare)) { 810 ASSERT_FAIL("tsearch failed"); 811 goto cleanup; 812 } 813 } 814 815 /* ..and filter them through BTF and btf_type_is_traceable_func. */ 816 nr = btf__type_cnt(btf); 817 for (type_id = 1; type_id < nr; type_id++) { 818 const struct btf_type *type; 819 const char *str; 820 821 type = btf__type_by_id(btf, type_id); 822 if (!type) 823 break; 824 825 if (BTF_INFO_KIND(type->info) != BTF_KIND_FUNC) 826 continue; 827 828 str = btf__name_by_offset(btf, type->name_off); 829 if (!str) 830 break; 831 832 if (!tfind(&str, &root, compare)) 833 continue; 834 835 /* Skip names that are not unique in kallsyms, see above. */ 836 if (tfind(&str, &dups, compare)) 837 continue; 838 839 if (!btf_type_is_traceable_func(btf, type)) 840 continue; 841 842 /* 843 * BTF can have more functions of the same name and they all 844 * resolve to the same address, take just the first one. 845 */ 846 tdelete(&str, &root, compare); 847 848 err = libbpf_ensure_mem((void **) &ids, &cap, sizeof(*ids), cnt + 1); 849 if (err) 850 goto cleanup; 851 852 ids[cnt++] = type_id; 853 } 854 855 opts.ids = ids; 856 opts.cnt = cnt; 857 858 attach_start_ns = get_time_ns(); 859 link = bpf_program__attach_tracing_multi(skel->progs.bench, NULL, &opts); 860 attach_end_ns = get_time_ns(); 861 862 if (!ASSERT_OK_PTR(link, "bpf_program__attach_tracing_multi")) 863 goto cleanup; 864 865 detach_start_ns = get_time_ns(); 866 bpf_link__destroy(link); 867 detach_end_ns = get_time_ns(); 868 869 attach_delta = (attach_end_ns - attach_start_ns) / 1000000000.0; 870 detach_delta = (detach_end_ns - detach_start_ns) / 1000000000.0; 871 872 printf("%s: found %lu functions\n", __func__, cnt); 873 printf("%s: attached in %7.3lfs\n", __func__, attach_delta); 874 printf("%s: detached in %7.3lfs\n", __func__, detach_delta); 875 876 cleanup: 877 tracing_multi_bench__destroy(skel); 878 tdestroy(root, tdestroy_free_nop); 879 tdestroy(dups, tdestroy_free_nop); 880 free_kallsyms_local(ksyms); 881 free(ids); 882 btf__free(btf); 883 } 884 885 static void tracing_multi_rollback_run(struct tracing_multi_rollback *skel) 886 { 887 LIBBPF_OPTS(bpf_test_run_opts, topts); 888 int err, prog_fd; 889 890 prog_fd = bpf_program__fd(skel->progs.test_fentry); 891 err = bpf_prog_test_run_opts(prog_fd, &topts); 892 ASSERT_OK(err, "test_run"); 893 894 /* make sure the rollback code did not leave any program attached */ 895 ASSERT_EQ(skel->bss->test_result_fentry, 0, "test_result_fentry"); 896 ASSERT_EQ(skel->bss->test_result_fexit, 0, "test_result_fexit"); 897 } 898 899 static void test_rollback_put(void) 900 { 901 LIBBPF_OPTS(bpf_tracing_multi_opts, opts); 902 struct tracing_multi_rollback *skel = NULL; 903 size_t cnt = FUNCS_CNT; 904 __u32 *ids = NULL; 905 int err; 906 907 skel = tracing_multi_rollback__open(); 908 if (!ASSERT_OK_PTR(skel, "tracing_multi_rollback__open")) 909 return; 910 911 bpf_program__set_autoload(skel->progs.test_fentry, true); 912 bpf_program__set_autoload(skel->progs.test_fexit, true); 913 914 err = tracing_multi_rollback__load(skel); 915 if (!ASSERT_OK(err, "tracing_multi_rollback__load")) 916 goto cleanup; 917 918 ids = get_ids(bpf_fentry_test, cnt, NULL); 919 if (!ASSERT_OK_PTR(ids, "get_ids")) 920 goto cleanup; 921 922 /* 923 * Mangle last id to trigger rollback, which needs to do put 924 * on get-ed trampolines. 925 */ 926 ids[9] = 0; 927 928 opts.ids = ids; 929 opts.cnt = cnt; 930 931 skel->bss->pid = getpid(); 932 933 skel->links.test_fentry = bpf_program__attach_tracing_multi(skel->progs.test_fentry, 934 NULL, &opts); 935 if (!ASSERT_ERR_PTR(skel->links.test_fentry, "bpf_program__attach_tracing_multi")) 936 goto cleanup; 937 938 skel->links.test_fexit = bpf_program__attach_tracing_multi(skel->progs.test_fexit, 939 NULL, &opts); 940 if (!ASSERT_ERR_PTR(skel->links.test_fexit, "bpf_program__attach_tracing_multi")) 941 goto cleanup; 942 943 /* We don't really attach any program, but let's make sure. */ 944 tracing_multi_rollback_run(skel); 945 946 cleanup: 947 tracing_multi_rollback__destroy(skel); 948 free(ids); 949 } 950 951 static void fillers_cleanup(struct tracing_multi_rollback **skels, int cnt) 952 { 953 int i; 954 955 for (i = 0; i < cnt; i++) 956 tracing_multi_rollback__destroy(skels[i]); 957 958 free(skels); 959 } 960 961 static struct tracing_multi_rollback *extra_load_and_link(void) 962 { 963 struct tracing_multi_rollback *skel; 964 int err; 965 966 skel = tracing_multi_rollback__open(); 967 if (!ASSERT_OK_PTR(skel, "tracing_multi_rollback__open")) 968 goto cleanup; 969 970 bpf_program__set_autoload(skel->progs.extra, true); 971 972 err = tracing_multi_rollback__load(skel); 973 if (!ASSERT_OK(err, "tracing_multi_rollback__load")) 974 goto cleanup; 975 976 skel->links.extra = bpf_program__attach_trace(skel->progs.extra); 977 if (!ASSERT_OK_PTR(skel->links.extra, "bpf_program__attach_trace")) 978 goto cleanup; 979 980 return skel; 981 982 cleanup: 983 tracing_multi_rollback__destroy(skel); 984 return NULL; 985 } 986 987 static struct tracing_multi_rollback **fillers_load_and_link(int max) 988 { 989 struct tracing_multi_rollback **skels, *skel; 990 int i, err; 991 992 skels = calloc(max + 1, sizeof(*skels)); 993 if (!ASSERT_OK_PTR(skels, "calloc")) 994 return NULL; 995 996 for (i = 0; i < max; i++) { 997 skel = skels[i] = tracing_multi_rollback__open(); 998 if (!ASSERT_OK_PTR(skels[i], "tracing_multi_rollback__open")) 999 goto cleanup; 1000 1001 bpf_program__set_autoload(skel->progs.filler, true); 1002 1003 err = tracing_multi_rollback__load(skel); 1004 if (!ASSERT_OK(err, "tracing_multi_rollback__load")) 1005 goto cleanup; 1006 1007 skel->links.filler = bpf_program__attach_trace(skel->progs.filler); 1008 if (!ASSERT_OK_PTR(skels[i]->links.filler, "bpf_program__attach_trace")) 1009 goto cleanup; 1010 } 1011 1012 return skels; 1013 1014 cleanup: 1015 fillers_cleanup(skels, i + 1); 1016 return NULL; 1017 } 1018 1019 static void test_rollback_unlink(void) 1020 { 1021 struct tracing_multi_rollback *skel = NULL, *extra; 1022 LIBBPF_OPTS(bpf_tracing_multi_opts, opts); 1023 struct tracing_multi_rollback **fillers; 1024 size_t cnt = FUNCS_CNT; 1025 __u32 *ids = NULL; 1026 int err, max; 1027 1028 max = get_bpf_max_tramp_links(); 1029 if (!ASSERT_GE(max, 1, "bpf_max_tramp_links")) 1030 return; 1031 1032 /* Attach maximum allowed programs to bpf_fentry_test10 */ 1033 fillers = fillers_load_and_link(max); 1034 if (!ASSERT_OK_PTR(fillers, "fillers_load_and_link")) 1035 return; 1036 1037 extra = extra_load_and_link(); 1038 if (!ASSERT_OK_PTR(extra, "extra_load_and_link")) 1039 goto cleanup; 1040 1041 skel = tracing_multi_rollback__open(); 1042 if (!ASSERT_OK_PTR(skel, "tracing_multi_rollback__open")) 1043 goto cleanup; 1044 1045 bpf_program__set_autoload(skel->progs.test_fentry, true); 1046 bpf_program__set_autoload(skel->progs.test_fexit, true); 1047 1048 /* 1049 * Attach tracing_multi link on bpf_fentry_test1-10, which will 1050 * fail on bpf_fentry_test10 function, because it already has 1051 * maximum allowed programs attached. 1052 * 1053 * The rollback needs to unlink already link-ed trampolines and 1054 * put all of them. 1055 */ 1056 err = tracing_multi_rollback__load(skel); 1057 if (!ASSERT_OK(err, "tracing_multi_rollback__load")) 1058 goto cleanup; 1059 1060 ids = get_ids(bpf_fentry_test, cnt, NULL); 1061 if (!ASSERT_OK_PTR(ids, "get_ids")) 1062 goto cleanup; 1063 1064 opts.ids = ids; 1065 opts.cnt = cnt; 1066 1067 skel->bss->pid = getpid(); 1068 1069 skel->links.test_fentry = bpf_program__attach_tracing_multi(skel->progs.test_fentry, 1070 NULL, &opts); 1071 if (!ASSERT_ERR_PTR(skel->links.test_fentry, "bpf_program__attach_tracing_multi")) 1072 goto cleanup; 1073 1074 skel->links.test_fexit = bpf_program__attach_tracing_multi(skel->progs.test_fexit, 1075 NULL, &opts); 1076 if (!ASSERT_ERR_PTR(skel->links.test_fexit, "bpf_program__attach_tracing_multi")) 1077 goto cleanup; 1078 1079 tracing_multi_rollback_run(skel); 1080 1081 cleanup: 1082 fillers_cleanup(fillers, max); 1083 tracing_multi_rollback__destroy(extra); 1084 tracing_multi_rollback__destroy(skel); 1085 free(ids); 1086 } 1087 1088 void serial_test_tracing_multi_attach_rollback(void) 1089 { 1090 if (test__start_subtest("put")) 1091 test_rollback_put(); 1092 if (test__start_subtest("unlink")) 1093 test_rollback_unlink(); 1094 } 1095 1096 void test_tracing_multi_test(void) 1097 { 1098 #ifndef __x86_64__ 1099 test__skip(); 1100 return; 1101 #endif 1102 1103 if (test__start_subtest("skel_api")) 1104 test_skel_api(); 1105 if (test__start_subtest("link_api_pattern")) 1106 test_link_api_pattern(); 1107 if (test__start_subtest("link_api_ids")) 1108 test_link_api_ids(false); 1109 if (test__start_subtest("module_skel_api")) 1110 test_module_skel_api(); 1111 if (test__start_subtest("module_link_api_pattern")) 1112 test_module_link_api_pattern(); 1113 if (test__start_subtest("module_link_api_ids")) 1114 test_module_link_api_ids(); 1115 if (test__start_subtest("intersect")) 1116 test_intersect(); 1117 if (test__start_subtest("cookies")) 1118 test_link_api_ids(true); 1119 if (test__start_subtest("session")) 1120 test_session(); 1121 if (test__start_subtest("attach_api_fails")) 1122 test_attach_api_fails(); 1123 if (test__start_subtest("attach_same_addr")) 1124 test_attach_same_addr(); 1125 RUN_TESTS(tracing_multi_verifier); 1126 if (test__start_subtest("fentry_after_multi")) 1127 test_fentry_after_multi(); 1128 } 1129