1 // SPDX-License-Identifier: GPL-2.0 2 #include <test_progs.h> 3 #include "kprobe_multi.skel.h" 4 #include "trace_helpers.h" 5 #include "kprobe_multi_empty.skel.h" 6 #include "kprobe_multi_override.skel.h" 7 #include "bpf/libbpf_internal.h" 8 #include "bpf/hashmap.h" 9 10 static void kprobe_multi_test_run(struct kprobe_multi *skel, bool test_return) 11 { 12 LIBBPF_OPTS(bpf_test_run_opts, topts); 13 int err, prog_fd; 14 15 prog_fd = bpf_program__fd(skel->progs.trigger); 16 err = bpf_prog_test_run_opts(prog_fd, &topts); 17 ASSERT_OK(err, "test_run"); 18 ASSERT_EQ(topts.retval, 0, "test_run"); 19 20 ASSERT_EQ(skel->bss->kprobe_test1_result, 1, "kprobe_test1_result"); 21 ASSERT_EQ(skel->bss->kprobe_test2_result, 1, "kprobe_test2_result"); 22 ASSERT_EQ(skel->bss->kprobe_test3_result, 1, "kprobe_test3_result"); 23 ASSERT_EQ(skel->bss->kprobe_test4_result, 1, "kprobe_test4_result"); 24 ASSERT_EQ(skel->bss->kprobe_test5_result, 1, "kprobe_test5_result"); 25 ASSERT_EQ(skel->bss->kprobe_test6_result, 1, "kprobe_test6_result"); 26 ASSERT_EQ(skel->bss->kprobe_test7_result, 1, "kprobe_test7_result"); 27 ASSERT_EQ(skel->bss->kprobe_test8_result, 1, "kprobe_test8_result"); 28 29 if (test_return) { 30 ASSERT_EQ(skel->bss->kretprobe_test1_result, 1, "kretprobe_test1_result"); 31 ASSERT_EQ(skel->bss->kretprobe_test2_result, 1, "kretprobe_test2_result"); 32 ASSERT_EQ(skel->bss->kretprobe_test3_result, 1, "kretprobe_test3_result"); 33 ASSERT_EQ(skel->bss->kretprobe_test4_result, 1, "kretprobe_test4_result"); 34 ASSERT_EQ(skel->bss->kretprobe_test5_result, 1, "kretprobe_test5_result"); 35 ASSERT_EQ(skel->bss->kretprobe_test6_result, 1, "kretprobe_test6_result"); 36 ASSERT_EQ(skel->bss->kretprobe_test7_result, 1, "kretprobe_test7_result"); 37 ASSERT_EQ(skel->bss->kretprobe_test8_result, 1, "kretprobe_test8_result"); 38 } 39 } 40 41 static void test_skel_api(void) 42 { 43 struct kprobe_multi *skel = NULL; 44 int err; 45 46 skel = kprobe_multi__open_and_load(); 47 if (!ASSERT_OK_PTR(skel, "kprobe_multi__open_and_load")) 48 goto cleanup; 49 50 skel->bss->pid = getpid(); 51 err = kprobe_multi__attach(skel); 52 if (!ASSERT_OK(err, "kprobe_multi__attach")) 53 goto cleanup; 54 55 kprobe_multi_test_run(skel, true); 56 57 cleanup: 58 kprobe_multi__destroy(skel); 59 } 60 61 static void test_link_api(struct bpf_link_create_opts *opts) 62 { 63 int prog_fd, link1_fd = -1, link2_fd = -1; 64 struct kprobe_multi *skel = NULL; 65 66 skel = kprobe_multi__open_and_load(); 67 if (!ASSERT_OK_PTR(skel, "fentry_raw_skel_load")) 68 goto cleanup; 69 70 skel->bss->pid = getpid(); 71 prog_fd = bpf_program__fd(skel->progs.test_kprobe); 72 link1_fd = bpf_link_create(prog_fd, 0, BPF_TRACE_KPROBE_MULTI, opts); 73 if (!ASSERT_GE(link1_fd, 0, "link_fd")) 74 goto cleanup; 75 76 opts->kprobe_multi.flags = BPF_F_KPROBE_MULTI_RETURN; 77 prog_fd = bpf_program__fd(skel->progs.test_kretprobe); 78 link2_fd = bpf_link_create(prog_fd, 0, BPF_TRACE_KPROBE_MULTI, opts); 79 if (!ASSERT_GE(link2_fd, 0, "link_fd")) 80 goto cleanup; 81 82 kprobe_multi_test_run(skel, true); 83 84 cleanup: 85 if (link1_fd != -1) 86 close(link1_fd); 87 if (link2_fd != -1) 88 close(link2_fd); 89 kprobe_multi__destroy(skel); 90 } 91 92 #define GET_ADDR(__sym, __addr) ({ \ 93 __addr = ksym_get_addr(__sym); \ 94 if (!ASSERT_NEQ(__addr, 0, "kallsyms load failed for " #__sym)) \ 95 return; \ 96 }) 97 98 static void test_link_api_addrs(void) 99 { 100 LIBBPF_OPTS(bpf_link_create_opts, opts); 101 unsigned long long addrs[8]; 102 103 GET_ADDR("bpf_fentry_test1", addrs[0]); 104 GET_ADDR("bpf_fentry_test2", addrs[1]); 105 GET_ADDR("bpf_fentry_test3", addrs[2]); 106 GET_ADDR("bpf_fentry_test4", addrs[3]); 107 GET_ADDR("bpf_fentry_test5", addrs[4]); 108 GET_ADDR("bpf_fentry_test6", addrs[5]); 109 GET_ADDR("bpf_fentry_test7", addrs[6]); 110 GET_ADDR("bpf_fentry_test8", addrs[7]); 111 112 opts.kprobe_multi.addrs = (const unsigned long*) addrs; 113 opts.kprobe_multi.cnt = ARRAY_SIZE(addrs); 114 test_link_api(&opts); 115 } 116 117 static void test_link_api_syms(void) 118 { 119 LIBBPF_OPTS(bpf_link_create_opts, opts); 120 const char *syms[8] = { 121 "bpf_fentry_test1", 122 "bpf_fentry_test2", 123 "bpf_fentry_test3", 124 "bpf_fentry_test4", 125 "bpf_fentry_test5", 126 "bpf_fentry_test6", 127 "bpf_fentry_test7", 128 "bpf_fentry_test8", 129 }; 130 131 opts.kprobe_multi.syms = syms; 132 opts.kprobe_multi.cnt = ARRAY_SIZE(syms); 133 test_link_api(&opts); 134 } 135 136 static void 137 test_attach_api(const char *pattern, struct bpf_kprobe_multi_opts *opts) 138 { 139 struct bpf_link *link1 = NULL, *link2 = NULL; 140 struct kprobe_multi *skel = NULL; 141 142 skel = kprobe_multi__open_and_load(); 143 if (!ASSERT_OK_PTR(skel, "fentry_raw_skel_load")) 144 goto cleanup; 145 146 skel->bss->pid = getpid(); 147 link1 = bpf_program__attach_kprobe_multi_opts(skel->progs.test_kprobe_manual, 148 pattern, opts); 149 if (!ASSERT_OK_PTR(link1, "bpf_program__attach_kprobe_multi_opts")) 150 goto cleanup; 151 152 if (opts) { 153 opts->retprobe = true; 154 link2 = bpf_program__attach_kprobe_multi_opts(skel->progs.test_kretprobe_manual, 155 pattern, opts); 156 if (!ASSERT_OK_PTR(link2, "bpf_program__attach_kprobe_multi_opts")) 157 goto cleanup; 158 } 159 160 kprobe_multi_test_run(skel, !!opts); 161 162 cleanup: 163 bpf_link__destroy(link2); 164 bpf_link__destroy(link1); 165 kprobe_multi__destroy(skel); 166 } 167 168 static void test_attach_api_pattern(void) 169 { 170 LIBBPF_OPTS(bpf_kprobe_multi_opts, opts); 171 172 test_attach_api("bpf_fentry_test*", &opts); 173 test_attach_api("bpf_fentry_test?", NULL); 174 } 175 176 static void test_attach_api_addrs(void) 177 { 178 LIBBPF_OPTS(bpf_kprobe_multi_opts, opts); 179 unsigned long long addrs[8]; 180 181 GET_ADDR("bpf_fentry_test1", addrs[0]); 182 GET_ADDR("bpf_fentry_test2", addrs[1]); 183 GET_ADDR("bpf_fentry_test3", addrs[2]); 184 GET_ADDR("bpf_fentry_test4", addrs[3]); 185 GET_ADDR("bpf_fentry_test5", addrs[4]); 186 GET_ADDR("bpf_fentry_test6", addrs[5]); 187 GET_ADDR("bpf_fentry_test7", addrs[6]); 188 GET_ADDR("bpf_fentry_test8", addrs[7]); 189 190 opts.addrs = (const unsigned long *) addrs; 191 opts.cnt = ARRAY_SIZE(addrs); 192 test_attach_api(NULL, &opts); 193 } 194 195 static void test_attach_api_syms(void) 196 { 197 LIBBPF_OPTS(bpf_kprobe_multi_opts, opts); 198 const char *syms[8] = { 199 "bpf_fentry_test1", 200 "bpf_fentry_test2", 201 "bpf_fentry_test3", 202 "bpf_fentry_test4", 203 "bpf_fentry_test5", 204 "bpf_fentry_test6", 205 "bpf_fentry_test7", 206 "bpf_fentry_test8", 207 }; 208 209 opts.syms = syms; 210 opts.cnt = ARRAY_SIZE(syms); 211 test_attach_api(NULL, &opts); 212 } 213 214 static void test_attach_api_fails(void) 215 { 216 LIBBPF_OPTS(bpf_kprobe_multi_opts, opts); 217 struct kprobe_multi *skel = NULL; 218 struct bpf_link *link = NULL; 219 unsigned long long addrs[2]; 220 const char *syms[2] = { 221 "bpf_fentry_test1", 222 "bpf_fentry_test2", 223 }; 224 __u64 cookies[2]; 225 int saved_error; 226 227 addrs[0] = ksym_get_addr("bpf_fentry_test1"); 228 addrs[1] = ksym_get_addr("bpf_fentry_test2"); 229 230 if (!ASSERT_FALSE(!addrs[0] || !addrs[1], "ksym_get_addr")) 231 goto cleanup; 232 233 skel = kprobe_multi__open_and_load(); 234 if (!ASSERT_OK_PTR(skel, "fentry_raw_skel_load")) 235 goto cleanup; 236 237 skel->bss->pid = getpid(); 238 239 /* fail_1 - pattern and opts NULL */ 240 link = bpf_program__attach_kprobe_multi_opts(skel->progs.test_kprobe_manual, 241 NULL, NULL); 242 saved_error = -errno; 243 if (!ASSERT_ERR_PTR(link, "fail_1")) 244 goto cleanup; 245 246 if (!ASSERT_EQ(saved_error, -EINVAL, "fail_1_error")) 247 goto cleanup; 248 249 /* fail_2 - both addrs and syms set */ 250 opts.addrs = (const unsigned long *) addrs; 251 opts.syms = syms; 252 opts.cnt = ARRAY_SIZE(syms); 253 opts.cookies = NULL; 254 255 link = bpf_program__attach_kprobe_multi_opts(skel->progs.test_kprobe_manual, 256 NULL, &opts); 257 saved_error = -errno; 258 if (!ASSERT_ERR_PTR(link, "fail_2")) 259 goto cleanup; 260 261 if (!ASSERT_EQ(saved_error, -EINVAL, "fail_2_error")) 262 goto cleanup; 263 264 /* fail_3 - pattern and addrs set */ 265 opts.addrs = (const unsigned long *) addrs; 266 opts.syms = NULL; 267 opts.cnt = ARRAY_SIZE(syms); 268 opts.cookies = NULL; 269 270 link = bpf_program__attach_kprobe_multi_opts(skel->progs.test_kprobe_manual, 271 "ksys_*", &opts); 272 saved_error = -errno; 273 if (!ASSERT_ERR_PTR(link, "fail_3")) 274 goto cleanup; 275 276 if (!ASSERT_EQ(saved_error, -EINVAL, "fail_3_error")) 277 goto cleanup; 278 279 /* fail_4 - pattern and cnt set */ 280 opts.addrs = NULL; 281 opts.syms = NULL; 282 opts.cnt = ARRAY_SIZE(syms); 283 opts.cookies = NULL; 284 285 link = bpf_program__attach_kprobe_multi_opts(skel->progs.test_kprobe_manual, 286 "ksys_*", &opts); 287 saved_error = -errno; 288 if (!ASSERT_ERR_PTR(link, "fail_4")) 289 goto cleanup; 290 291 if (!ASSERT_EQ(saved_error, -EINVAL, "fail_4_error")) 292 goto cleanup; 293 294 /* fail_5 - pattern and cookies */ 295 opts.addrs = NULL; 296 opts.syms = NULL; 297 opts.cnt = 0; 298 opts.cookies = cookies; 299 300 link = bpf_program__attach_kprobe_multi_opts(skel->progs.test_kprobe_manual, 301 "ksys_*", &opts); 302 saved_error = -errno; 303 if (!ASSERT_ERR_PTR(link, "fail_5")) 304 goto cleanup; 305 306 if (!ASSERT_EQ(saved_error, -EINVAL, "fail_5_error")) 307 goto cleanup; 308 309 /* fail_6 - abnormal cnt */ 310 opts.addrs = (const unsigned long *) addrs; 311 opts.syms = NULL; 312 opts.cnt = INT_MAX; 313 opts.cookies = NULL; 314 315 link = bpf_program__attach_kprobe_multi_opts(skel->progs.test_kprobe_manual, 316 NULL, &opts); 317 saved_error = -errno; 318 if (!ASSERT_ERR_PTR(link, "fail_6")) 319 goto cleanup; 320 321 if (!ASSERT_EQ(saved_error, -E2BIG, "fail_6_error")) 322 goto cleanup; 323 324 cleanup: 325 bpf_link__destroy(link); 326 kprobe_multi__destroy(skel); 327 } 328 329 static size_t symbol_hash(long key, void *ctx __maybe_unused) 330 { 331 return str_hash((const char *) key); 332 } 333 334 static bool symbol_equal(long key1, long key2, void *ctx __maybe_unused) 335 { 336 return strcmp((const char *) key1, (const char *) key2) == 0; 337 } 338 339 static bool is_invalid_entry(char *buf, bool kernel) 340 { 341 if (kernel && strchr(buf, '[')) 342 return true; 343 if (!kernel && !strchr(buf, '[')) 344 return true; 345 return false; 346 } 347 348 static bool skip_entry(char *name) 349 { 350 /* 351 * We attach to almost all kernel functions and some of them 352 * will cause 'suspicious RCU usage' when fprobe is attached 353 * to them. Filter out the current culprits - arch_cpu_idle 354 * default_idle and rcu_* functions. 355 */ 356 if (!strcmp(name, "arch_cpu_idle")) 357 return true; 358 if (!strcmp(name, "default_idle")) 359 return true; 360 if (!strncmp(name, "rcu_", 4)) 361 return true; 362 if (!strcmp(name, "bpf_dispatcher_xdp_func")) 363 return true; 364 if (!strncmp(name, "__ftrace_invalid_address__", 365 sizeof("__ftrace_invalid_address__") - 1)) 366 return true; 367 return false; 368 } 369 370 static int get_syms(char ***symsp, size_t *cntp, bool kernel) 371 { 372 size_t cap = 0, cnt = 0, i; 373 char *name = NULL, **syms = NULL; 374 struct hashmap *map; 375 char buf[256]; 376 FILE *f; 377 int err = 0; 378 379 /* 380 * The available_filter_functions contains many duplicates, 381 * but other than that all symbols are usable in kprobe multi 382 * interface. 383 * Filtering out duplicates by using hashmap__add, which won't 384 * add existing entry. 385 */ 386 387 if (access("/sys/kernel/tracing/trace", F_OK) == 0) 388 f = fopen("/sys/kernel/tracing/available_filter_functions", "r"); 389 else 390 f = fopen("/sys/kernel/debug/tracing/available_filter_functions", "r"); 391 392 if (!f) 393 return -EINVAL; 394 395 map = hashmap__new(symbol_hash, symbol_equal, NULL); 396 if (IS_ERR(map)) { 397 err = libbpf_get_error(map); 398 goto error; 399 } 400 401 while (fgets(buf, sizeof(buf), f)) { 402 if (is_invalid_entry(buf, kernel)) 403 continue; 404 405 free(name); 406 if (sscanf(buf, "%ms$*[^\n]\n", &name) != 1) 407 continue; 408 if (skip_entry(name)) 409 continue; 410 411 err = hashmap__add(map, name, 0); 412 if (err == -EEXIST) { 413 err = 0; 414 continue; 415 } 416 if (err) 417 goto error; 418 419 err = libbpf_ensure_mem((void **) &syms, &cap, 420 sizeof(*syms), cnt + 1); 421 if (err) 422 goto error; 423 424 syms[cnt++] = name; 425 name = NULL; 426 } 427 428 *symsp = syms; 429 *cntp = cnt; 430 431 error: 432 free(name); 433 fclose(f); 434 hashmap__free(map); 435 if (err) { 436 for (i = 0; i < cnt; i++) 437 free(syms[i]); 438 free(syms); 439 } 440 return err; 441 } 442 443 static void do_bench_test(struct kprobe_multi_empty *skel, struct bpf_kprobe_multi_opts *opts) 444 { 445 long attach_start_ns, attach_end_ns; 446 long detach_start_ns, detach_end_ns; 447 double attach_delta, detach_delta; 448 struct bpf_link *link = NULL; 449 450 attach_start_ns = get_time_ns(); 451 link = bpf_program__attach_kprobe_multi_opts(skel->progs.test_kprobe_empty, 452 NULL, opts); 453 attach_end_ns = get_time_ns(); 454 455 if (!ASSERT_OK_PTR(link, "bpf_program__attach_kprobe_multi_opts")) 456 return; 457 458 detach_start_ns = get_time_ns(); 459 bpf_link__destroy(link); 460 detach_end_ns = get_time_ns(); 461 462 attach_delta = (attach_end_ns - attach_start_ns) / 1000000000.0; 463 detach_delta = (detach_end_ns - detach_start_ns) / 1000000000.0; 464 465 printf("%s: found %lu functions\n", __func__, opts->cnt); 466 printf("%s: attached in %7.3lfs\n", __func__, attach_delta); 467 printf("%s: detached in %7.3lfs\n", __func__, detach_delta); 468 } 469 470 static void test_kprobe_multi_bench_attach(bool kernel) 471 { 472 LIBBPF_OPTS(bpf_kprobe_multi_opts, opts); 473 struct kprobe_multi_empty *skel = NULL; 474 char **syms = NULL; 475 size_t cnt = 0, i; 476 477 if (!ASSERT_OK(get_syms(&syms, &cnt, kernel), "get_syms")) 478 return; 479 480 skel = kprobe_multi_empty__open_and_load(); 481 if (!ASSERT_OK_PTR(skel, "kprobe_multi_empty__open_and_load")) 482 goto cleanup; 483 484 opts.syms = (const char **) syms; 485 opts.cnt = cnt; 486 487 do_bench_test(skel, &opts); 488 489 cleanup: 490 kprobe_multi_empty__destroy(skel); 491 if (syms) { 492 for (i = 0; i < cnt; i++) 493 free(syms[i]); 494 free(syms); 495 } 496 } 497 498 static void test_attach_override(void) 499 { 500 struct kprobe_multi_override *skel = NULL; 501 struct bpf_link *link = NULL; 502 503 skel = kprobe_multi_override__open_and_load(); 504 if (!ASSERT_OK_PTR(skel, "kprobe_multi_empty__open_and_load")) 505 goto cleanup; 506 507 /* The test_override calls bpf_override_return so it should fail 508 * to attach to bpf_fentry_test1 function, which is not on error 509 * injection list. 510 */ 511 link = bpf_program__attach_kprobe_multi_opts(skel->progs.test_override, 512 "bpf_fentry_test1", NULL); 513 if (!ASSERT_ERR_PTR(link, "override_attached_bpf_fentry_test1")) { 514 bpf_link__destroy(link); 515 goto cleanup; 516 } 517 518 /* The should_fail_bio function is on error injection list, 519 * attach should succeed. 520 */ 521 link = bpf_program__attach_kprobe_multi_opts(skel->progs.test_override, 522 "should_fail_bio", NULL); 523 if (!ASSERT_OK_PTR(link, "override_attached_should_fail_bio")) 524 goto cleanup; 525 526 bpf_link__destroy(link); 527 528 cleanup: 529 kprobe_multi_override__destroy(skel); 530 } 531 532 void serial_test_kprobe_multi_bench_attach(void) 533 { 534 if (test__start_subtest("kernel")) 535 test_kprobe_multi_bench_attach(true); 536 if (test__start_subtest("modules")) 537 test_kprobe_multi_bench_attach(false); 538 } 539 540 void test_kprobe_multi_test(void) 541 { 542 if (!ASSERT_OK(load_kallsyms(), "load_kallsyms")) 543 return; 544 545 if (test__start_subtest("skel_api")) 546 test_skel_api(); 547 if (test__start_subtest("link_api_addrs")) 548 test_link_api_syms(); 549 if (test__start_subtest("link_api_syms")) 550 test_link_api_addrs(); 551 if (test__start_subtest("attach_api_pattern")) 552 test_attach_api_pattern(); 553 if (test__start_subtest("attach_api_addrs")) 554 test_attach_api_addrs(); 555 if (test__start_subtest("attach_api_syms")) 556 test_attach_api_syms(); 557 if (test__start_subtest("attach_api_fails")) 558 test_attach_api_fails(); 559 if (test__start_subtest("attach_override")) 560 test_attach_override(); 561 } 562