Skip to content

Commit b0b8c1a

Browse files
borkmannrtg-canonical
authored andcommitted
bpf, testing: Convert prog tests to probe_read_{user, kernel}{, _str} helper
BugLink: https://bugs.launchpad.net/bugs/1990009 Use probe read *_{kernel,user}{,_str}() helpers instead of bpf_probe_read() or bpf_probe_read_user_str() for program tests where appropriate. Signed-off-by: Daniel Borkmann <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/4a61d4b71ce3765587d8ef5cb93afa18515e5b3e.1572649915.git.daniel@iogearbox.net (backported from commit 50f9aa4) [rtg - dropped tools/testing/selftests/bpf/progs/kfree_skb.c] Signed-off-by: Tim Gardner <[email protected]> Acked-by: Cengiz Can <[email protected]> Acked-by: Joseph Salisbury <[email protected]> Signed-off-by: Tim Gardner <[email protected]>
1 parent 24d28be commit b0b8c1a

File tree

3 files changed

+55
-50
lines changed

3 files changed

+55
-50
lines changed

tools/testing/selftests/bpf/progs/pyperf.h

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -72,41 +72,43 @@ static __always_inline void *get_thread_state(void *tls_base, PidData *pidData)
7272
void* thread_state;
7373
int key;
7474

75-
bpf_probe_read(&key, sizeof(key), (void*)(long)pidData->tls_key_addr);
76-
bpf_probe_read(&thread_state, sizeof(thread_state),
77-
tls_base + 0x310 + key * 0x10 + 0x08);
75+
bpf_probe_read_user(&key, sizeof(key), (void*)(long)pidData->tls_key_addr);
76+
bpf_probe_read_user(&thread_state, sizeof(thread_state),
77+
tls_base + 0x310 + key * 0x10 + 0x08);
7878
return thread_state;
7979
}
8080

8181
static __always_inline bool get_frame_data(void *frame_ptr, PidData *pidData,
8282
FrameData *frame, Symbol *symbol)
8383
{
8484
// read data from PyFrameObject
85-
bpf_probe_read(&frame->f_back,
86-
sizeof(frame->f_back),
87-
frame_ptr + pidData->offsets.PyFrameObject_back);
88-
bpf_probe_read(&frame->f_code,
89-
sizeof(frame->f_code),
90-
frame_ptr + pidData->offsets.PyFrameObject_code);
85+
bpf_probe_read_user(&frame->f_back,
86+
sizeof(frame->f_back),
87+
frame_ptr + pidData->offsets.PyFrameObject_back);
88+
bpf_probe_read_user(&frame->f_code,
89+
sizeof(frame->f_code),
90+
frame_ptr + pidData->offsets.PyFrameObject_code);
9191

9292
// read data from PyCodeObject
9393
if (!frame->f_code)
9494
return false;
95-
bpf_probe_read(&frame->co_filename,
96-
sizeof(frame->co_filename),
97-
frame->f_code + pidData->offsets.PyCodeObject_filename);
98-
bpf_probe_read(&frame->co_name,
99-
sizeof(frame->co_name),
100-
frame->f_code + pidData->offsets.PyCodeObject_name);
95+
bpf_probe_read_user(&frame->co_filename,
96+
sizeof(frame->co_filename),
97+
frame->f_code + pidData->offsets.PyCodeObject_filename);
98+
bpf_probe_read_user(&frame->co_name,
99+
sizeof(frame->co_name),
100+
frame->f_code + pidData->offsets.PyCodeObject_name);
101101
// read actual names into symbol
102102
if (frame->co_filename)
103-
bpf_probe_read_str(&symbol->file,
104-
sizeof(symbol->file),
105-
frame->co_filename + pidData->offsets.String_data);
103+
bpf_probe_read_user_str(&symbol->file,
104+
sizeof(symbol->file),
105+
frame->co_filename +
106+
pidData->offsets.String_data);
106107
if (frame->co_name)
107-
bpf_probe_read_str(&symbol->name,
108-
sizeof(symbol->name),
109-
frame->co_name + pidData->offsets.String_data);
108+
bpf_probe_read_user_str(&symbol->name,
109+
sizeof(symbol->name),
110+
frame->co_name +
111+
pidData->offsets.String_data);
110112
return true;
111113
}
112114

@@ -174,9 +176,9 @@ static __always_inline int __on_event(struct pt_regs *ctx)
174176
event->kernel_stack_id = bpf_get_stackid(ctx, &stackmap, 0);
175177

176178
void* thread_state_current = (void*)0;
177-
bpf_probe_read(&thread_state_current,
178-
sizeof(thread_state_current),
179-
(void*)(long)pidData->current_state_addr);
179+
bpf_probe_read_user(&thread_state_current,
180+
sizeof(thread_state_current),
181+
(void*)(long)pidData->current_state_addr);
180182

181183
struct task_struct* task = (struct task_struct*)bpf_get_current_task();
182184
void* tls_base = (void*)task;
@@ -188,11 +190,13 @@ static __always_inline int __on_event(struct pt_regs *ctx)
188190
if (pidData->use_tls) {
189191
uint64_t pthread_created;
190192
uint64_t pthread_self;
191-
bpf_probe_read(&pthread_self, sizeof(pthread_self), tls_base + 0x10);
193+
bpf_probe_read_user(&pthread_self, sizeof(pthread_self),
194+
tls_base + 0x10);
192195

193-
bpf_probe_read(&pthread_created,
194-
sizeof(pthread_created),
195-
thread_state + pidData->offsets.PyThreadState_thread);
196+
bpf_probe_read_user(&pthread_created,
197+
sizeof(pthread_created),
198+
thread_state +
199+
pidData->offsets.PyThreadState_thread);
196200
event->pthread_match = pthread_created == pthread_self;
197201
} else {
198202
event->pthread_match = 1;
@@ -204,9 +208,10 @@ static __always_inline int __on_event(struct pt_regs *ctx)
204208
Symbol sym = {};
205209
int cur_cpu = bpf_get_smp_processor_id();
206210

207-
bpf_probe_read(&frame_ptr,
208-
sizeof(frame_ptr),
209-
thread_state + pidData->offsets.PyThreadState_frame);
211+
bpf_probe_read_user(&frame_ptr,
212+
sizeof(frame_ptr),
213+
thread_state +
214+
pidData->offsets.PyThreadState_frame);
210215

211216
int32_t* symbol_counter = bpf_map_lookup_elem(&symbolmap, &sym);
212217
if (symbol_counter == NULL)

tools/testing/selftests/bpf/progs/strobemeta.h

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ struct strobe_map_raw {
9898
/*
9999
* having volatile doesn't change anything on BPF side, but clang
100100
* emits warnings for passing `volatile const char *` into
101-
* bpf_probe_read_str that expects just `const char *`
101+
* bpf_probe_read_user_str that expects just `const char *`
102102
*/
103103
const char* tag;
104104
/*
@@ -309,18 +309,18 @@ static __always_inline void *calc_location(struct strobe_value_loc *loc,
309309
dtv_t *dtv;
310310
void *tls_ptr;
311311

312-
bpf_probe_read(&tls_index, sizeof(struct tls_index),
313-
(void *)loc->offset);
312+
bpf_probe_read_user(&tls_index, sizeof(struct tls_index),
313+
(void *)loc->offset);
314314
/* valid module index is always positive */
315315
if (tls_index.module > 0) {
316316
/* dtv = ((struct tcbhead *)tls_base)->dtv[tls_index.module] */
317-
bpf_probe_read(&dtv, sizeof(dtv),
318-
&((struct tcbhead *)tls_base)->dtv);
317+
bpf_probe_read_user(&dtv, sizeof(dtv),
318+
&((struct tcbhead *)tls_base)->dtv);
319319
dtv += tls_index.module;
320320
} else {
321321
dtv = NULL;
322322
}
323-
bpf_probe_read(&tls_ptr, sizeof(void *), dtv);
323+
bpf_probe_read_user(&tls_ptr, sizeof(void *), dtv);
324324
/* if pointer has (void *)-1 value, then TLS wasn't initialized yet */
325325
return tls_ptr && tls_ptr != (void *)-1
326326
? tls_ptr + tls_index.offset
@@ -336,7 +336,7 @@ static __always_inline void read_int_var(struct strobemeta_cfg *cfg,
336336
if (!location)
337337
return;
338338

339-
bpf_probe_read(value, sizeof(struct strobe_value_generic), location);
339+
bpf_probe_read_user(value, sizeof(struct strobe_value_generic), location);
340340
data->int_vals[idx] = value->val;
341341
if (value->header.len)
342342
data->int_vals_set_mask |= (1 << idx);
@@ -356,13 +356,13 @@ static __always_inline uint64_t read_str_var(struct strobemeta_cfg *cfg,
356356
if (!location)
357357
return 0;
358358

359-
bpf_probe_read(value, sizeof(struct strobe_value_generic), location);
360-
len = bpf_probe_read_str(payload, STROBE_MAX_STR_LEN, value->ptr);
359+
bpf_probe_read_user(value, sizeof(struct strobe_value_generic), location);
360+
len = bpf_probe_read_user_str(payload, STROBE_MAX_STR_LEN, value->ptr);
361361
/*
362-
* if bpf_probe_read_str returns error (<0), due to casting to
362+
* if bpf_probe_read_user_str returns error (<0), due to casting to
363363
* unsinged int, it will become big number, so next check is
364364
* sufficient to check for errors AND prove to BPF verifier, that
365-
* bpf_probe_read_str won't return anything bigger than
365+
* bpf_probe_read_user_str won't return anything bigger than
366366
* STROBE_MAX_STR_LEN
367367
*/
368368
if (len > STROBE_MAX_STR_LEN)
@@ -391,8 +391,8 @@ static __always_inline void *read_map_var(struct strobemeta_cfg *cfg,
391391
if (!location)
392392
return payload;
393393

394-
bpf_probe_read(value, sizeof(struct strobe_value_generic), location);
395-
if (bpf_probe_read(&map, sizeof(struct strobe_map_raw), value->ptr))
394+
bpf_probe_read_user(value, sizeof(struct strobe_value_generic), location);
395+
if (bpf_probe_read_user(&map, sizeof(struct strobe_map_raw), value->ptr))
396396
return payload;
397397

398398
descr->id = map.id;
@@ -402,7 +402,7 @@ static __always_inline void *read_map_var(struct strobemeta_cfg *cfg,
402402
data->req_meta_valid = 1;
403403
}
404404

405-
len = bpf_probe_read_str(payload, STROBE_MAX_STR_LEN, map.tag);
405+
len = bpf_probe_read_user_str(payload, STROBE_MAX_STR_LEN, map.tag);
406406
if (len <= STROBE_MAX_STR_LEN) {
407407
descr->tag_len = len;
408408
payload += len;
@@ -418,15 +418,15 @@ static __always_inline void *read_map_var(struct strobemeta_cfg *cfg,
418418
break;
419419

420420
descr->key_lens[i] = 0;
421-
len = bpf_probe_read_str(payload, STROBE_MAX_STR_LEN,
422-
map.entries[i].key);
421+
len = bpf_probe_read_user_str(payload, STROBE_MAX_STR_LEN,
422+
map.entries[i].key);
423423
if (len <= STROBE_MAX_STR_LEN) {
424424
descr->key_lens[i] = len;
425425
payload += len;
426426
}
427427
descr->val_lens[i] = 0;
428-
len = bpf_probe_read_str(payload, STROBE_MAX_STR_LEN,
429-
map.entries[i].val);
428+
len = bpf_probe_read_user_str(payload, STROBE_MAX_STR_LEN,
429+
map.entries[i].val);
430430
if (len <= STROBE_MAX_STR_LEN) {
431431
descr->val_lens[i] = len;
432432
payload += len;

tools/testing/selftests/bpf/progs/test_tcp_estats.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
#include <sys/socket.h>
3939
#include "bpf_helpers.h"
4040

41-
#define _(P) ({typeof(P) val = 0; bpf_probe_read(&val, sizeof(val), &P); val;})
41+
#define _(P) ({typeof(P) val = 0; bpf_probe_read_kernel(&val, sizeof(val), &P); val;})
4242
#define TCP_ESTATS_MAGIC 0xBAADBEEF
4343

4444
/* This test case needs "sock" and "pt_regs" data structure.

0 commit comments

Comments
 (0)