-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
selftests/bpf: Add urandom_read shared lib and USDTs
Extend urandom_read helper binary to include USDTs of 4 combinations: semaphore/semaphoreless (refcounted and non-refcounted) and based in executable or shared library. We also extend urandom_read with ability to report it's own PID to parent process and wait for parent process to ready itself up for tracing urandom_read. We utilize popen() and underlying pipe properties for proper signaling. Once urandom_read is ready, we add few tests to validate that libbpf's USDT attachment handles all the above combinations of semaphore (or lack of it) and static or shared library USDTs. Also, we validate that libbpf handles shared libraries both with PID filter and without one (i.e., -1 for PID argument). Having the shared library case tested with and without PID is important because internal logic differs on kernels that don't support BPF cookies. On such older kernels, attaching to USDTs in shared libraries without specifying concrete PID doesn't work in principle, because it's impossible to determine shared library's load address to derive absolute IPs for uprobe attachments. Without absolute IPs, it's impossible to perform correct look up of USDT spec based on uprobe's absolute IP (the only kind available from BPF at runtime). This is not the problem on newer kernels with BPF cookie as we don't need IP-to-ID lookup because BPF cookie value *is* spec ID. So having those two situations as separate subtests is good because libbpf CI is able to test latest selftests against old kernels (e.g., 4.9 and 5.5), so we'll be able to disable PID-less shared lib attachment for old kernels, but will still leave PID-specific one enabled to validate this legacy logic is working correctly. Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Dave Marchevsky <davemarchevsky@fb.com> Link: https://lore.kernel.org/bpf/20220404234202.331384-8-andrii@kernel.org
- Loading branch information
Showing
8 changed files
with
275 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
/* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */ | ||
|
||
#include "vmlinux.h" | ||
#include <bpf/bpf_helpers.h> | ||
#include <bpf/usdt.bpf.h> | ||
|
||
int urand_pid; | ||
|
||
int urand_read_without_sema_call_cnt; | ||
int urand_read_without_sema_buf_sz_sum; | ||
|
||
SEC("usdt/./urandom_read:urand:read_without_sema") | ||
int BPF_USDT(urand_read_without_sema, int iter_num, int iter_cnt, int buf_sz) | ||
{ | ||
if (urand_pid != (bpf_get_current_pid_tgid() >> 32)) | ||
return 0; | ||
|
||
__sync_fetch_and_add(&urand_read_without_sema_call_cnt, 1); | ||
__sync_fetch_and_add(&urand_read_without_sema_buf_sz_sum, buf_sz); | ||
|
||
return 0; | ||
} | ||
|
||
int urand_read_with_sema_call_cnt; | ||
int urand_read_with_sema_buf_sz_sum; | ||
|
||
SEC("usdt/./urandom_read:urand:read_with_sema") | ||
int BPF_USDT(urand_read_with_sema, int iter_num, int iter_cnt, int buf_sz) | ||
{ | ||
if (urand_pid != (bpf_get_current_pid_tgid() >> 32)) | ||
return 0; | ||
|
||
__sync_fetch_and_add(&urand_read_with_sema_call_cnt, 1); | ||
__sync_fetch_and_add(&urand_read_with_sema_buf_sz_sum, buf_sz); | ||
|
||
return 0; | ||
} | ||
|
||
int urandlib_read_without_sema_call_cnt; | ||
int urandlib_read_without_sema_buf_sz_sum; | ||
|
||
SEC("usdt/./liburandom_read.so:urandlib:read_without_sema") | ||
int BPF_USDT(urandlib_read_without_sema, int iter_num, int iter_cnt, int buf_sz) | ||
{ | ||
if (urand_pid != (bpf_get_current_pid_tgid() >> 32)) | ||
return 0; | ||
|
||
__sync_fetch_and_add(&urandlib_read_without_sema_call_cnt, 1); | ||
__sync_fetch_and_add(&urandlib_read_without_sema_buf_sz_sum, buf_sz); | ||
|
||
return 0; | ||
} | ||
|
||
int urandlib_read_with_sema_call_cnt; | ||
int urandlib_read_with_sema_buf_sz_sum; | ||
|
||
SEC("usdt/./liburandom_read.so:urandlib:read_with_sema") | ||
int BPF_USDT(urandlib_read_with_sema, int iter_num, int iter_cnt, int buf_sz) | ||
{ | ||
if (urand_pid != (bpf_get_current_pid_tgid() >> 32)) | ||
return 0; | ||
|
||
__sync_fetch_and_add(&urandlib_read_with_sema_call_cnt, 1); | ||
__sync_fetch_and_add(&urandlib_read_with_sema_buf_sz_sum, buf_sz); | ||
|
||
return 0; | ||
} | ||
|
||
char _license[] SEC("license") = "GPL"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
/* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */ | ||
#include "sdt.h" | ||
|
||
void urand_read_without_sema(int iter_num, int iter_cnt, int read_sz) | ||
{ | ||
/* semaphore-less USDT */ | ||
STAP_PROBE3(urand, read_without_sema, iter_num, iter_cnt, read_sz); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
/* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */ | ||
#define _SDT_HAS_SEMAPHORES 1 | ||
#include "sdt.h" | ||
|
||
#define SEC(name) __attribute__((section(name), used)) | ||
|
||
unsigned short urandlib_read_with_sema_semaphore SEC(".probes"); | ||
|
||
void urandlib_read_with_sema(int iter_num, int iter_cnt, int read_sz) | ||
{ | ||
STAP_PROBE3(urandlib, read_with_sema, iter_num, iter_cnt, read_sz); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
/* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */ | ||
#include "sdt.h" | ||
|
||
void urandlib_read_without_sema(int iter_num, int iter_cnt, int read_sz) | ||
{ | ||
STAP_PROBE3(urandlib, read_without_sema, iter_num, iter_cnt, read_sz); | ||
} |