Skip to content

Commit ef01f4e

Browse files
pcmooreAlexei Starovoitov
authored andcommitted
bpf: restore the ebpf program ID for BPF_AUDIT_UNLOAD and PERF_BPF_EVENT_PROG_UNLOAD
When changing the ebpf program put() routines to support being called from within IRQ context the program ID was reset to zero prior to calling the perf event and audit UNLOAD record generators, which resulted in problems as the ebpf program ID was bogus (always zero). This patch addresses this problem by removing an unnecessary call to bpf_prog_free_id() in __bpf_prog_offload_destroy() and adjusting __bpf_prog_put() to only call bpf_prog_free_id() after audit and perf have finished their bpf program unload tasks in bpf_prog_put_deferred(). For the record, no one can determine, or remember, why it was necessary to free the program ID, and remove it from the IDR, prior to executing bpf_prog_put_deferred(); regardless, both Stanislav and Alexei agree that the approach in this patch should be safe. It is worth noting that when moving the bpf_prog_free_id() call, the do_idr_lock parameter was forced to true as the ebpf devs determined this was the correct as the do_idr_lock should always be true. The do_idr_lock parameter will be removed in a follow-up patch, but it was kept here to keep the patch small in an effort to ease any stable backports. I also modified the bpf_audit_prog() logic used to associate the AUDIT_BPF record with other associated records, e.g. @ctx != NULL. Instead of keying off the operation, it now keys off the execution context, e.g. '!in_irg && !irqs_disabled()', which is much more appropriate and should help better connect the UNLOAD operations with the associated audit state (other audit records). Cc: [email protected] Fixes: d809e13 ("bpf: Prepare bpf_prog_put() to be called from irq context.") Reported-by: Burn Alting <[email protected]> Reported-by: Jiri Olsa <[email protected]> Suggested-by: Stanislav Fomichev <[email protected]> Suggested-by: Alexei Starovoitov <[email protected]> Signed-off-by: Paul Moore <[email protected]> Acked-by: Stanislav Fomichev <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent a3d81bc commit ef01f4e

File tree

2 files changed

+2
-7
lines changed

2 files changed

+2
-7
lines changed

kernel/bpf/offload.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,6 @@ static void __bpf_prog_offload_destroy(struct bpf_prog *prog)
216216
if (offload->dev_state)
217217
offload->offdev->ops->destroy(prog);
218218

219-
/* Make sure BPF_PROG_GET_NEXT_ID can't find this dead program */
220-
bpf_prog_free_id(prog, true);
221-
222219
list_del_init(&offload->offloads);
223220
kfree(offload);
224221
prog->aux->offload = NULL;

kernel/bpf/syscall.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1972,7 +1972,7 @@ static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op)
19721972
return;
19731973
if (audit_enabled == AUDIT_OFF)
19741974
return;
1975-
if (op == BPF_AUDIT_LOAD)
1975+
if (!in_irq() && !irqs_disabled())
19761976
ctx = audit_context();
19771977
ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF);
19781978
if (unlikely(!ab))
@@ -2067,6 +2067,7 @@ static void bpf_prog_put_deferred(struct work_struct *work)
20672067
prog = aux->prog;
20682068
perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
20692069
bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
2070+
bpf_prog_free_id(prog, true);
20702071
__bpf_prog_put_noref(prog, true);
20712072
}
20722073

@@ -2075,9 +2076,6 @@ static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
20752076
struct bpf_prog_aux *aux = prog->aux;
20762077

20772078
if (atomic64_dec_and_test(&aux->refcnt)) {
2078-
/* bpf_prog_free_id() must be called first */
2079-
bpf_prog_free_id(prog, do_idr_lock);
2080-
20812079
if (in_irq() || irqs_disabled()) {
20822080
INIT_WORK(&aux->work, bpf_prog_put_deferred);
20832081
schedule_work(&aux->work);

0 commit comments

Comments
 (0)