bpf: Initialize the bpf_run_ctx in bpf_iter_run_prog()
authorMartin KaFai Lau <kafai@fb.com>
Wed, 17 Aug 2022 06:17:51 +0000 (23:17 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Fri, 19 Aug 2022 00:06:13 +0000 (17:06 -0700)
The bpf-iter-prog for tcp and unix sk can do bpf_setsockopt()
which needs has_current_bpf_ctx() to decide if it is called by a
bpf prog.  This patch initializes the bpf_run_ctx in
bpf_iter_run_prog() for the has_current_bpf_ctx() to use.

Acked-by: Andrii Nakryiko <andrii@kernel.org>
Reviewed-by: Stanislav Fomichev <sdf@google.com>
Signed-off-by: Martin KaFai Lau <kafai@fb.com>
Link: https://lore.kernel.org/r/20220817061751.4177657-1-kafai@fb.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
kernel/bpf/bpf_iter.c

index 97bb57493ed5fdc232a4bf5d06c67e68f07ad077..5dc307bdeaebc7e9789f7162a1d2243f57747e69 100644 (file)
@@ -694,19 +694,24 @@ struct bpf_prog *bpf_iter_get_info(struct bpf_iter_meta *meta, bool in_stop)
 
 int bpf_iter_run_prog(struct bpf_prog *prog, void *ctx)
 {
+       struct bpf_run_ctx run_ctx, *old_run_ctx;
        int ret;
 
        if (prog->aux->sleepable) {
                rcu_read_lock_trace();
                migrate_disable();
                might_fault();
+               old_run_ctx = bpf_set_run_ctx(&run_ctx);
                ret = bpf_prog_run(prog, ctx);
+               bpf_reset_run_ctx(old_run_ctx);
                migrate_enable();
                rcu_read_unlock_trace();
        } else {
                rcu_read_lock();
                migrate_disable();
+               old_run_ctx = bpf_set_run_ctx(&run_ctx);
                ret = bpf_prog_run(prog, ctx);
+               bpf_reset_run_ctx(old_run_ctx);
                migrate_enable();
                rcu_read_unlock();
        }