bpf: Refactor {acquire,release}_reference_state
authorKumar Kartikeya Dwivedi <memxor@gmail.com>
Wed, 4 Dec 2024 03:03:55 +0000 (19:03 -0800)
committerAlexei Starovoitov <ast@kernel.org>
Wed, 4 Dec 2024 16:38:29 +0000 (08:38 -0800)
commit769b0f1c821455ab29baf42491e1ea1d726451fa
treec83cb4b7218bcf6f4c26baa819369474eb6671f0
parent1995edc5f9089ecb8b77a34f21e4abd8f887b856
bpf: Refactor {acquire,release}_reference_state

In preparation for introducing support for more reference types which
have to add and remove reference state, refactor the
acquire_reference_state and release_reference_state functions to share
common logic.

The acquire_reference_state function simply handles growing the acquired
refs and returning the pointer to the new uninitialized element, which
can be filled in by the caller.

The release_reference_state function simply erases a reference state
entry in the acquired_refs array and shrinks it. The callers are
responsible for finding the suitable element by matching on various
fields of the reference state and requesting deletion through this
function. It is not supposed to be called directly.

Existing callers of release_reference_state were using it to find and
remove state for a given ref_obj_id without scrubbing the associated
registers in the verifier state. Introduce release_reference_nomark to
provide this functionality and convert callers. We now use this new
release_reference_nomark function within release_reference as well.
It needs to operate on a verifier state instead of taking verifier env
as mark_ptr_or_null_regs requires operating on verifier state of the
two branches of a NULL condition check, therefore env->cur_state cannot
be used directly.

Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Link: https://lore.kernel.org/r/20241204030400.208005-3-memxor@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
kernel/bpf/verifier.c