bpf: Free struct bpf_cpumask in call_rcu handler
authorDavid Vernet <void@manifault.com>
Thu, 16 Mar 2023 05:40:24 +0000 (00:40 -0500)
committerAlexei Starovoitov <ast@kernel.org>
Thu, 16 Mar 2023 19:28:30 +0000 (12:28 -0700)
commit77473d1a962f3d4f7ba48324502b6d27b8ef2591
tree765f0db068a9faa06796325dad035071deb3132a
parent6cb9430be1471d631e1b6b138e6d26657a9caa81
bpf: Free struct bpf_cpumask in call_rcu handler

The struct bpf_cpumask type uses the bpf_mem_cache_{alloc,free}() APIs
to allocate and free its cpumasks. The bpf_mem allocator may currently
immediately reuse some memory when its freed, without waiting for an RCU
read cycle to elapse. We want to be able to treat struct bpf_cpumask
objects as completely RCU safe.

This is necessary for two reasons:

1. bpf_cpumask_kptr_get() currently does an RCU-protected
   refcnt_inc_not_zero(). This of course assumes that the underlying
   memory is not reused, and is therefore unsafe in its current form.

2. We want to be able to get rid of bpf_cpumask_kptr_get() entirely, and
   intead use the superior kptr RCU semantics now afforded by the
   verifier.

This patch fixes (1), and enables (2), by making struct bpf_cpumask RCU
safe. A subsequent patch will update the verifier to allow struct
bpf_cpumask * pointers to be passed to KF_RCU kfuncs, and then a latter
patch will remove bpf_cpumask_kptr_get().

Fixes: 516f4d3397c9 ("bpf: Enable cpumasks to be queried and used as kptrs")
Signed-off-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/r/20230316054028.88924-2-void@manifault.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
kernel/bpf/cpumask.c