bpf: Add KF_RCU flag to bpf_refcount_acquire_impl
authorDave Marchevsky <davemarchevsky@fb.com>
Tue, 7 Nov 2023 08:56:34 +0000 (00:56 -0800)
committerAlexei Starovoitov <ast@kernel.org>
Fri, 10 Nov 2023 03:07:51 +0000 (19:07 -0800)
commit1500a5d9f49cb66906d3ea1c9158df25cc41dd40
treee0fbb55fc5153e5187e424e4db11f2eb5aa889f4
parent82ce364c6087e31ff9837380a4641a856284064c
bpf: Add KF_RCU flag to bpf_refcount_acquire_impl

Refcounted local kptrs are kptrs to user-defined types with a
bpf_refcount field. Recent commits ([0], [1]) modified the lifetime of
refcounted local kptrs such that the underlying memory is not reused
until RCU grace period has elapsed.

Separately, verification of bpf_refcount_acquire calls currently
succeeds for MAYBE_NULL non-owning reference input, which is a problem
as bpf_refcount_acquire_impl has no handling for this case.

This patch takes advantage of aforementioned lifetime changes to tag
bpf_refcount_acquire_impl kfunc KF_RCU, thereby preventing MAYBE_NULL
input to the kfunc. The KF_RCU flag applies to all kfunc params; it's
fine for it to apply to the void *meta__ign param as that's populated by
the verifier and is tagged __ign regardless.

  [0]: commit 7e26cd12ad1c ("bpf: Use bpf_mem_free_rcu when
       bpf_obj_dropping refcounted nodes") is the actual change to
       allocation behaivor
  [1]: commit 0816b8c6bf7f ("bpf: Consider non-owning refs to refcounted
       nodes RCU protected") modified verifier understanding of
       refcounted local kptrs to match [0]'s changes

Signed-off-by: Dave Marchevsky <davemarchevsky@fb.com>
Fixes: 7c50b1cb76ac ("bpf: Add bpf_refcount_acquire kfunc")
Link: https://lore.kernel.org/r/20231107085639.3016113-2-davemarchevsky@fb.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
kernel/bpf/helpers.c