Skip to content

Commit 33b4405

Browse files
kkdwvdKernel Patches Daemon
authored andcommitted
bpf: Add support for KF_RET_RCU flag
Add a kfunc annotation 'KF_RET_RCU' to signal that the return type must be marked MEM_RCU, to return objects that are RCU protected. Naturally, this must imply that the kfunc is invoked in an RCU critical section, and thus the presence of this flag implies the presence of the KF_RCU_PROTECTED flag. Upcoming kfunc scx_bpf_cpu_curr() [0] will be made to make use of this flag. [0]: https://lore.kernel.org/all/[email protected] Signed-off-by: Kumar Kartikeya Dwivedi <[email protected]>
1 parent d5438fd commit 33b4405

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

Documentation/bpf/kfuncs.rst

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,10 +346,19 @@ arguments are at least RCU protected pointers. This may transitively imply that
346346
RCU protection is ensured, but it does not work in cases of kfuncs which require
347347
RCU protection but do not take RCU protected arguments.
348348

349+
2.4.9 KF_RET_RCU flag
350+
---------------------
351+
352+
The KF_RET_RCU flag is used for kfuncs which return pointers to RCU protected
353+
objects. Since this only works when the invocation of the kfunc is made in an
354+
active RCU critical section, the usage of this flag implies ``KF_RCU_PROTECTED``
355+
flag automatically. This flag may be combined with other return value modifiers,
356+
such as ``KF_RET_NULL``.
357+
349358
.. _KF_deprecated_flag:
350359

351-
2.4.9 KF_DEPRECATED flag
352-
------------------------
360+
2.4.10 KF_DEPRECATED flag
361+
-------------------------
353362

354363
The KF_DEPRECATED flag is used for kfuncs which are scheduled to be
355364
changed or removed in a subsequent kernel release. A kfunc that is

include/linux/btf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
#define KF_ARENA_RET (1 << 13) /* kfunc returns an arena pointer */
8080
#define KF_ARENA_ARG1 (1 << 14) /* kfunc takes an arena pointer as its first argument */
8181
#define KF_ARENA_ARG2 (1 << 15) /* kfunc takes an arena pointer as its second argument */
82+
#define KF_RET_RCU ((1 << 16) | KF_RCU_PROTECTED) /* kfunc returns an RCU protected pointer, implies KF_RCU_PROTECTED */
8283

8384
/*
8485
* Tag marking a kernel function as a kfunc. This is meant to minimize the

kernel/bpf/verifier.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12342,6 +12342,11 @@ static bool is_kfunc_ret_null(struct bpf_kfunc_call_arg_meta *meta)
1234212342
return meta->kfunc_flags & KF_RET_NULL;
1234312343
}
1234412344

12345+
static bool is_kfunc_ret_rcu(struct bpf_kfunc_call_arg_meta *meta)
12346+
{
12347+
return meta->kfunc_flags & KF_RET_RCU;
12348+
}
12349+
1234512350
static bool is_kfunc_bpf_rcu_read_lock(struct bpf_kfunc_call_arg_meta *meta)
1234612351
{
1234712352
return meta->func_id == special_kfunc_list[KF_bpf_rcu_read_lock];
@@ -14042,6 +14047,8 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
1404214047

1404314048
if (meta.func_id == special_kfunc_list[KF_bpf_get_kmem_cache])
1404414049
regs[BPF_REG_0].type |= PTR_UNTRUSTED;
14050+
else if (is_kfunc_ret_rcu(&meta))
14051+
regs[BPF_REG_0].type |= MEM_RCU;
1404514052

1404614053
if (is_iter_next_kfunc(&meta)) {
1404714054
struct bpf_reg_state *cur_iter;

0 commit comments

Comments
 (0)