Skip to content

Commit 8d8771d

Browse files
mykyta5Alexei Starovoitov
authored andcommitted
bpf: add plumbing for file-backed dynptr
Add the necessary verifier plumbing for the new file-backed dynptr type. Introduce two kfuncs for its lifecycle management: * bpf_dynptr_from_file() for initialization * bpf_dynptr_file_discard() for destruction Currently there is no mechanism for kfunc to release dynptr, this patch add one: * Dynptr release function sets meta->release_regno * Call unmark_stack_slots_dynptr() if meta->release_regno is set and dynptr ref_obj_id is set as well. Signed-off-by: Mykyta Yatsenko <[email protected]> Acked-by: Eduard Zingerman <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 9cba966 commit 8d8771d

File tree

4 files changed

+45
-7
lines changed

4 files changed

+45
-7
lines changed

include/linux/bpf.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -792,12 +792,15 @@ enum bpf_type_flag {
792792
/* DYNPTR points to skb_metadata_end()-skb_metadata_len() */
793793
DYNPTR_TYPE_SKB_META = BIT(19 + BPF_BASE_TYPE_BITS),
794794

795+
/* DYNPTR points to file */
796+
DYNPTR_TYPE_FILE = BIT(20 + BPF_BASE_TYPE_BITS),
797+
795798
__BPF_TYPE_FLAG_MAX,
796799
__BPF_TYPE_LAST_FLAG = __BPF_TYPE_FLAG_MAX - 1,
797800
};
798801

799802
#define DYNPTR_TYPE_FLAG_MASK (DYNPTR_TYPE_LOCAL | DYNPTR_TYPE_RINGBUF | DYNPTR_TYPE_SKB \
800-
| DYNPTR_TYPE_XDP | DYNPTR_TYPE_SKB_META)
803+
| DYNPTR_TYPE_XDP | DYNPTR_TYPE_SKB_META | DYNPTR_TYPE_FILE)
801804

802805
/* Max number of base types. */
803806
#define BPF_BASE_TYPE_LIMIT (1UL << BPF_BASE_TYPE_BITS)
@@ -1385,6 +1388,8 @@ enum bpf_dynptr_type {
13851388
BPF_DYNPTR_TYPE_XDP,
13861389
/* Points to skb_metadata_end()-skb_metadata_len() */
13871390
BPF_DYNPTR_TYPE_SKB_META,
1391+
/* Underlying data is a file */
1392+
BPF_DYNPTR_TYPE_FILE,
13881393
};
13891394

13901395
int bpf_dynptr_check_size(u64 size);

kernel/bpf/helpers.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4252,6 +4252,16 @@ __bpf_kfunc int bpf_task_work_schedule_resume(struct task_struct *task, struct b
42524252
return bpf_task_work_schedule(task, tw, map__map, callback, aux__prog, TWA_RESUME);
42534253
}
42544254

4255+
__bpf_kfunc int bpf_dynptr_from_file(struct file *file, u32 flags, struct bpf_dynptr *ptr__uninit)
4256+
{
4257+
return 0;
4258+
}
4259+
4260+
__bpf_kfunc int bpf_dynptr_file_discard(struct bpf_dynptr *dynptr)
4261+
{
4262+
return 0;
4263+
}
4264+
42554265
__bpf_kfunc_end_defs();
42564266

42574267
static void bpf_task_work_cancel_scheduled(struct irq_work *irq_work)
@@ -4429,6 +4439,8 @@ BTF_ID_FLAGS(func, bpf_cgroup_read_xattr, KF_RCU)
44294439
BTF_ID_FLAGS(func, bpf_stream_vprintk, KF_TRUSTED_ARGS)
44304440
BTF_ID_FLAGS(func, bpf_task_work_schedule_signal, KF_TRUSTED_ARGS)
44314441
BTF_ID_FLAGS(func, bpf_task_work_schedule_resume, KF_TRUSTED_ARGS)
4442+
BTF_ID_FLAGS(func, bpf_dynptr_from_file, KF_TRUSTED_ARGS)
4443+
BTF_ID_FLAGS(func, bpf_dynptr_file_discard)
44324444
BTF_KFUNCS_END(common_btf_ids)
44334445

44344446
static const struct btf_kfunc_id_set common_kfunc_set = {

kernel/bpf/log.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,8 @@ const char *dynptr_type_str(enum bpf_dynptr_type type)
500500
return "xdp";
501501
case BPF_DYNPTR_TYPE_SKB_META:
502502
return "skb_meta";
503+
case BPF_DYNPTR_TYPE_FILE:
504+
return "file";
503505
case BPF_DYNPTR_TYPE_INVALID:
504506
return "<invalid>";
505507
default:

kernel/bpf/verifier.c

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,8 @@ static enum bpf_dynptr_type arg_to_dynptr_type(enum bpf_arg_type arg_type)
692692
return BPF_DYNPTR_TYPE_XDP;
693693
case DYNPTR_TYPE_SKB_META:
694694
return BPF_DYNPTR_TYPE_SKB_META;
695+
case DYNPTR_TYPE_FILE:
696+
return BPF_DYNPTR_TYPE_FILE;
695697
default:
696698
return BPF_DYNPTR_TYPE_INVALID;
697699
}
@@ -710,14 +712,16 @@ static enum bpf_type_flag get_dynptr_type_flag(enum bpf_dynptr_type type)
710712
return DYNPTR_TYPE_XDP;
711713
case BPF_DYNPTR_TYPE_SKB_META:
712714
return DYNPTR_TYPE_SKB_META;
715+
case BPF_DYNPTR_TYPE_FILE:
716+
return DYNPTR_TYPE_FILE;
713717
default:
714718
return 0;
715719
}
716720
}
717721

718722
static bool dynptr_type_refcounted(enum bpf_dynptr_type type)
719723
{
720-
return type == BPF_DYNPTR_TYPE_RINGBUF;
724+
return type == BPF_DYNPTR_TYPE_RINGBUF || type == BPF_DYNPTR_TYPE_FILE;
721725
}
722726

723727
static void __mark_dynptr_reg(struct bpf_reg_state *reg,
@@ -12291,6 +12295,8 @@ enum special_kfunc_type {
1229112295
KF_bpf_res_spin_unlock,
1229212296
KF_bpf_res_spin_lock_irqsave,
1229312297
KF_bpf_res_spin_unlock_irqrestore,
12298+
KF_bpf_dynptr_from_file,
12299+
KF_bpf_dynptr_file_discard,
1229412300
KF___bpf_trap,
1229512301
KF_bpf_task_work_schedule_signal,
1229612302
KF_bpf_task_work_schedule_resume,
@@ -12363,6 +12369,8 @@ BTF_ID(func, bpf_res_spin_lock)
1236312369
BTF_ID(func, bpf_res_spin_unlock)
1236412370
BTF_ID(func, bpf_res_spin_lock_irqsave)
1236512371
BTF_ID(func, bpf_res_spin_unlock_irqrestore)
12372+
BTF_ID(func, bpf_dynptr_from_file)
12373+
BTF_ID(func, bpf_dynptr_file_discard)
1236612374
BTF_ID(func, __bpf_trap)
1236712375
BTF_ID(func, bpf_task_work_schedule_signal)
1236812376
BTF_ID(func, bpf_task_work_schedule_resume)
@@ -13326,6 +13334,11 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_
1332613334
dynptr_arg_type |= DYNPTR_TYPE_XDP;
1332713335
} else if (meta->func_id == special_kfunc_list[KF_bpf_dynptr_from_skb_meta]) {
1332813336
dynptr_arg_type |= DYNPTR_TYPE_SKB_META;
13337+
} else if (meta->func_id == special_kfunc_list[KF_bpf_dynptr_from_file]) {
13338+
dynptr_arg_type |= DYNPTR_TYPE_FILE;
13339+
} else if (meta->func_id == special_kfunc_list[KF_bpf_dynptr_file_discard]) {
13340+
dynptr_arg_type |= DYNPTR_TYPE_FILE;
13341+
meta->release_regno = regno;
1332913342
} else if (meta->func_id == special_kfunc_list[KF_bpf_dynptr_clone] &&
1333013343
(dynptr_arg_type & MEM_UNINIT)) {
1333113344
enum bpf_dynptr_type parent_type = meta->initialized_dynptr.type;
@@ -14006,12 +14019,18 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
1400614019
* PTR_TO_BTF_ID in bpf_kfunc_arg_meta, do the release now.
1400714020
*/
1400814021
if (meta.release_regno) {
14009-
err = release_reference(env, regs[meta.release_regno].ref_obj_id);
14010-
if (err) {
14011-
verbose(env, "kfunc %s#%d reference has not been acquired before\n",
14012-
func_name, meta.func_id);
14013-
return err;
14022+
struct bpf_reg_state *reg = &regs[meta.release_regno];
14023+
14024+
if (meta.initialized_dynptr.ref_obj_id) {
14025+
err = unmark_stack_slots_dynptr(env, reg);
14026+
} else {
14027+
err = release_reference(env, reg->ref_obj_id);
14028+
if (err)
14029+
verbose(env, "kfunc %s#%d reference has not been acquired before\n",
14030+
func_name, meta.func_id);
1401414031
}
14032+
if (err)
14033+
return err;
1401514034
}
1401614035

1401714036
if (meta.func_id == special_kfunc_list[KF_bpf_list_push_front_impl] ||

0 commit comments

Comments
 (0)