Skip to content

Commit 9195494

Browse files
viktormalikfmaurer-rh
authored andcommitted
bpf: Free struct bpf_cpumask in call_rcu handler
Bugzilla: https://bugzilla.redhat.com/2178930 commit 77473d1 Author: David Vernet <void@manifault.com> Date: Thu Mar 16 00:40:24 2023 -0500 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: 516f4d3 ("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> Signed-off-by: Viktor Malik <vmalik@redhat.com>
1 parent d13a2d3 commit 9195494

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

kernel/bpf/cpumask.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
/**
1010
* struct bpf_cpumask - refcounted BPF cpumask wrapper structure
1111
* @cpumask: The actual cpumask embedded in the struct.
12+
* @rcu: The RCU head used to free the cpumask with RCU safety.
1213
* @usage: Object reference counter. When the refcount goes to 0, the
1314
* memory is released back to the BPF allocator, which provides
1415
* RCU safety.
@@ -24,6 +25,7 @@
2425
*/
2526
struct bpf_cpumask {
2627
cpumask_t cpumask;
28+
struct rcu_head rcu;
2729
refcount_t usage;
2830
};
2931

@@ -108,6 +110,16 @@ __bpf_kfunc struct bpf_cpumask *bpf_cpumask_kptr_get(struct bpf_cpumask **cpumas
108110
return cpumask;
109111
}
110112

113+
static void cpumask_free_cb(struct rcu_head *head)
114+
{
115+
struct bpf_cpumask *cpumask;
116+
117+
cpumask = container_of(head, struct bpf_cpumask, rcu);
118+
migrate_disable();
119+
bpf_mem_cache_free(&bpf_cpumask_ma, cpumask);
120+
migrate_enable();
121+
}
122+
111123
/**
112124
* bpf_cpumask_release() - Release a previously acquired BPF cpumask.
113125
* @cpumask: The cpumask being released.
@@ -121,11 +133,8 @@ __bpf_kfunc void bpf_cpumask_release(struct bpf_cpumask *cpumask)
121133
if (!cpumask)
122134
return;
123135

124-
if (refcount_dec_and_test(&cpumask->usage)) {
125-
migrate_disable();
126-
bpf_mem_cache_free(&bpf_cpumask_ma, cpumask);
127-
migrate_enable();
128-
}
136+
if (refcount_dec_and_test(&cpumask->usage))
137+
call_rcu(&cpumask->rcu, cpumask_free_cb);
129138
}
130139

131140
/**

0 commit comments

Comments
 (0)