Skip to content

Commit 74d7667

Browse files
theihorKernel Patches Daemon
authored andcommitted
bpf: Re-define bpf_stream_vprintk as a magic kfunc
* void *aux__prog => struct bpf_prog_aux *aux__magic * Set KF_MAGIC_ARGS flag * Add relevant symbols to magic_kfuncs list * Update selftests to use the new signature bpf_stream_vprintk macro is changed to use bpf_stream_vprintk_impl, and the extern definition of bpf_stream_vprintk is replaced with _impl version in bpf_helpers.h This should help with backwards compatibility, as the API of bpf_stream_vprintk macro hasn't changed. Signed-off-by: Ihor Solodrai <[email protected]>
1 parent e7321c7 commit 74d7667

File tree

5 files changed

+16
-10
lines changed

5 files changed

+16
-10
lines changed

kernel/bpf/helpers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4528,7 +4528,7 @@ BTF_ID_FLAGS(func, bpf_strncasestr);
45284528
#if defined(CONFIG_BPF_LSM) && defined(CONFIG_CGROUPS)
45294529
BTF_ID_FLAGS(func, bpf_cgroup_read_xattr, KF_RCU)
45304530
#endif
4531-
BTF_ID_FLAGS(func, bpf_stream_vprintk, KF_TRUSTED_ARGS)
4531+
BTF_ID_FLAGS(func, bpf_stream_vprintk, KF_MAGIC_ARGS | KF_TRUSTED_ARGS)
45324532
BTF_ID_FLAGS(func, bpf_task_work_schedule_signal, KF_MAGIC_ARGS | KF_TRUSTED_ARGS)
45334533
BTF_ID_FLAGS(func, bpf_task_work_schedule_resume, KF_MAGIC_ARGS | KF_TRUSTED_ARGS)
45344534
BTF_ID_FLAGS(func, bpf_dynptr_from_file, KF_TRUSTED_ARGS)

kernel/bpf/stream.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,19 +212,22 @@ __bpf_kfunc_start_defs();
212212
* Avoid using enum bpf_stream_id so that kfunc users don't have to pull in the
213213
* enum in headers.
214214
*/
215-
__bpf_kfunc int bpf_stream_vprintk(int stream_id, const char *fmt__str, const void *args, u32 len__sz, void *aux__prog)
215+
__bpf_kfunc int bpf_stream_vprintk(int stream_id,
216+
const char *fmt__str,
217+
const void *args,
218+
u32 len__sz,
219+
struct bpf_prog_aux *aux__magic)
216220
{
217221
struct bpf_bprintf_data data = {
218222
.get_bin_args = true,
219223
.get_buf = true,
220224
};
221-
struct bpf_prog_aux *aux = aux__prog;
222225
u32 fmt_size = strlen(fmt__str) + 1;
223226
struct bpf_stream *stream;
224227
u32 data_len = len__sz;
225228
int ret, num_args;
226229

227-
stream = bpf_stream_get(stream_id, aux);
230+
stream = bpf_stream_get(stream_id, aux__magic);
228231
if (!stream)
229232
return -ENOENT;
230233

kernel/bpf/verifier.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3273,6 +3273,8 @@ BTF_ID(func, bpf_task_work_schedule_signal)
32733273
BTF_ID(func, bpf_task_work_schedule_signal_impl)
32743274
BTF_ID(func, bpf_task_work_schedule_resume)
32753275
BTF_ID(func, bpf_task_work_schedule_resume_impl)
3276+
BTF_ID(func, bpf_stream_vprintk)
3277+
BTF_ID(func, bpf_stream_vprintk_impl)
32763278
BTF_ID_LIST_END(magic_kfuncs)
32773279

32783280
static s32 magic_kfunc_by_impl(s32 impl_func_id)

tools/lib/bpf/bpf_helpers.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,9 @@ enum libbpf_tristate {
315315
___param, sizeof(___param)); \
316316
})
317317

318-
extern int bpf_stream_vprintk(int stream_id, const char *fmt__str, const void *args,
319-
__u32 len__sz, void *aux__prog) __weak __ksym;
318+
struct bpf_prog_aux;
319+
extern int bpf_stream_vprintk_impl(int stream_id, const char *fmt__str, const void *args,
320+
__u32 len__sz, struct bpf_prog_aux *aux__magic) __weak __ksym;
320321

321322
#define bpf_stream_printk(stream_id, fmt, args...) \
322323
({ \
@@ -328,7 +329,7 @@ extern int bpf_stream_vprintk(int stream_id, const char *fmt__str, const void *a
328329
___bpf_fill(___param, args); \
329330
_Pragma("GCC diagnostic pop") \
330331
\
331-
bpf_stream_vprintk(stream_id, ___fmt, ___param, sizeof(___param), NULL);\
332+
bpf_stream_vprintk_impl(stream_id, ___fmt, ___param, sizeof(___param), NULL);\
332333
})
333334

334335
/* Use __bpf_printk when bpf_printk call has 3 or fewer fmt args

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,23 @@ SEC("syscall")
1010
__failure __msg("Possibly NULL pointer passed")
1111
int stream_vprintk_null_arg(void *ctx)
1212
{
13-
bpf_stream_vprintk(BPF_STDOUT, "", NULL, 0, NULL);
13+
bpf_stream_vprintk(BPF_STDOUT, "", NULL, 0);
1414
return 0;
1515
}
1616

1717
SEC("syscall")
1818
__failure __msg("R3 type=scalar expected=")
1919
int stream_vprintk_scalar_arg(void *ctx)
2020
{
21-
bpf_stream_vprintk(BPF_STDOUT, "", (void *)46, 0, NULL);
21+
bpf_stream_vprintk(BPF_STDOUT, "", (void *)46, 0);
2222
return 0;
2323
}
2424

2525
SEC("syscall")
2626
__failure __msg("arg#1 doesn't point to a const string")
2727
int stream_vprintk_string_arg(void *ctx)
2828
{
29-
bpf_stream_vprintk(BPF_STDOUT, ctx, NULL, 0, NULL);
29+
bpf_stream_vprintk(BPF_STDOUT, ctx, NULL, 0);
3030
return 0;
3131
}
3232

0 commit comments

Comments
 (0)