Skip to content

Commit d809e13

Browse files
Alexei Starovoitovborkmann
Alexei Starovoitov
authored andcommitted
bpf: Prepare bpf_prog_put() to be called from irq context.
Currently bpf_prog_put() is called from the task context only. With addition of bpf timers the timer related helpers will start calling bpf_prog_put() from irq-saved region and in rare cases might drop the refcnt to zero. To address this case, first, convert bpf_prog_free_id() to be irq-save (this is similar to bpf_map_free_id), and, second, defer non irq appropriate calls into work queue. For example: bpf_audit_prog() is calling kmalloc and wake_up_interruptible, bpf_prog_kallsyms_del_all()->bpf_ksym_del()->spin_unlock_bh(). They are not safe with irqs disabled. Signed-off-by: Alexei Starovoitov <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Martin KaFai Lau <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Acked-by: Toke Høiland-Jørgensen <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent de587d5 commit d809e13

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

kernel/bpf/syscall.c

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,6 +1699,8 @@ static int bpf_prog_alloc_id(struct bpf_prog *prog)
16991699

17001700
void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
17011701
{
1702+
unsigned long flags;
1703+
17021704
/* cBPF to eBPF migrations are currently not in the idr store.
17031705
* Offloaded programs are removed from the store when their device
17041706
* disappears - even if someone grabs an fd to them they are unusable,
@@ -1708,15 +1710,15 @@ void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
17081710
return;
17091711

17101712
if (do_idr_lock)
1711-
spin_lock_bh(&prog_idr_lock);
1713+
spin_lock_irqsave(&prog_idr_lock, flags);
17121714
else
17131715
__acquire(&prog_idr_lock);
17141716

17151717
idr_remove(&prog_idr, prog->aux->id);
17161718
prog->aux->id = 0;
17171719

17181720
if (do_idr_lock)
1719-
spin_unlock_bh(&prog_idr_lock);
1721+
spin_unlock_irqrestore(&prog_idr_lock, flags);
17201722
else
17211723
__release(&prog_idr_lock);
17221724
}
@@ -1752,14 +1754,32 @@ static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
17521754
}
17531755
}
17541756

1757+
static void bpf_prog_put_deferred(struct work_struct *work)
1758+
{
1759+
struct bpf_prog_aux *aux;
1760+
struct bpf_prog *prog;
1761+
1762+
aux = container_of(work, struct bpf_prog_aux, work);
1763+
prog = aux->prog;
1764+
perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
1765+
bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
1766+
__bpf_prog_put_noref(prog, true);
1767+
}
1768+
17551769
static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
17561770
{
1757-
if (atomic64_dec_and_test(&prog->aux->refcnt)) {
1758-
perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
1759-
bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
1771+
struct bpf_prog_aux *aux = prog->aux;
1772+
1773+
if (atomic64_dec_and_test(&aux->refcnt)) {
17601774
/* bpf_prog_free_id() must be called first */
17611775
bpf_prog_free_id(prog, do_idr_lock);
1762-
__bpf_prog_put_noref(prog, true);
1776+
1777+
if (in_irq() || irqs_disabled()) {
1778+
INIT_WORK(&aux->work, bpf_prog_put_deferred);
1779+
schedule_work(&aux->work);
1780+
} else {
1781+
bpf_prog_put_deferred(&aux->work);
1782+
}
17631783
}
17641784
}
17651785

0 commit comments

Comments
 (0)