Skip to content

Commit 073f11b

Browse files
Benjamin TissoiresAlexei Starovoitov
authored andcommitted
bpf: replace bpf_timer_set_callback with a generic helper
In the same way we have a generic __bpf_async_init(), we also need to share code between timer and workqueue for the set_callback call. We just add an unused flags parameter, as it will be used for workqueues. Signed-off-by: Benjamin Tissoires <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 56b4a17 commit 073f11b

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

kernel/bpf/helpers.c

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,22 +1262,23 @@ static const struct bpf_func_proto bpf_timer_init_proto = {
12621262
.arg3_type = ARG_ANYTHING,
12631263
};
12641264

1265-
BPF_CALL_3(bpf_timer_set_callback, struct bpf_async_kern *, timer, void *, callback_fn,
1266-
struct bpf_prog_aux *, aux)
1265+
static int __bpf_async_set_callback(struct bpf_async_kern *async, void *callback_fn,
1266+
struct bpf_prog_aux *aux, unsigned int flags,
1267+
enum bpf_async_type type)
12671268
{
12681269
struct bpf_prog *prev, *prog = aux->prog;
1269-
struct bpf_hrtimer *t;
1270+
struct bpf_async_cb *cb;
12701271
int ret = 0;
12711272

12721273
if (in_nmi())
12731274
return -EOPNOTSUPP;
1274-
__bpf_spin_lock_irqsave(&timer->lock);
1275-
t = timer->timer;
1276-
if (!t) {
1275+
__bpf_spin_lock_irqsave(&async->lock);
1276+
cb = async->cb;
1277+
if (!cb) {
12771278
ret = -EINVAL;
12781279
goto out;
12791280
}
1280-
if (!atomic64_read(&t->cb.map->usercnt)) {
1281+
if (!atomic64_read(&cb->map->usercnt)) {
12811282
/* maps with timers must be either held by user space
12821283
* or pinned in bpffs. Otherwise timer might still be
12831284
* running even when bpf prog is detached and user space
@@ -1286,7 +1287,7 @@ BPF_CALL_3(bpf_timer_set_callback, struct bpf_async_kern *, timer, void *, callb
12861287
ret = -EPERM;
12871288
goto out;
12881289
}
1289-
prev = t->cb.prog;
1290+
prev = cb->prog;
12901291
if (prev != prog) {
12911292
/* Bump prog refcnt once. Every bpf_timer_set_callback()
12921293
* can pick different callback_fn-s within the same prog.
@@ -1299,14 +1300,20 @@ BPF_CALL_3(bpf_timer_set_callback, struct bpf_async_kern *, timer, void *, callb
12991300
if (prev)
13001301
/* Drop prev prog refcnt when swapping with new prog */
13011302
bpf_prog_put(prev);
1302-
t->cb.prog = prog;
1303+
cb->prog = prog;
13031304
}
1304-
rcu_assign_pointer(t->cb.callback_fn, callback_fn);
1305+
rcu_assign_pointer(cb->callback_fn, callback_fn);
13051306
out:
1306-
__bpf_spin_unlock_irqrestore(&timer->lock);
1307+
__bpf_spin_unlock_irqrestore(&async->lock);
13071308
return ret;
13081309
}
13091310

1311+
BPF_CALL_3(bpf_timer_set_callback, struct bpf_async_kern *, timer, void *, callback_fn,
1312+
struct bpf_prog_aux *, aux)
1313+
{
1314+
return __bpf_async_set_callback(timer, callback_fn, aux, 0, BPF_ASYNC_TYPE_TIMER);
1315+
}
1316+
13101317
static const struct bpf_func_proto bpf_timer_set_callback_proto = {
13111318
.func = bpf_timer_set_callback,
13121319
.gpl_only = true,

0 commit comments

Comments
 (0)