Skip to content

bpf: fix deadlock in special field destruction in NMI#12120

Open
RazeLighter777 wants to merge 1 commit into
kernel-patches:bpf-next_basefrom
RazeLighter777:recycler-nmi-dtor
Open

bpf: fix deadlock in special field destruction in NMI#12120
RazeLighter777 wants to merge 1 commit into
kernel-patches:bpf-next_basefrom
RazeLighter777:recycler-nmi-dtor

Conversation

@RazeLighter777
Copy link
Copy Markdown

Relax the behavior of bpf_obj_free_fields to only cancel/free async work if irqs_disabled, and do not run unsafe free operations such as kptr dtors directly in those contexts.

The switch statement on the field type is split into two switch blocks; the first half containing types that are safe to run the free fields operations for in irq_disabled context. The breaks in the first block are changed to continues to avoid redundant checking / warning from the second block. A default case for the first switch block operation continues past the second block if irqs_disabled().

The second switch statement is the portion of the original block containing the free fields operations that are unsafe if irqs_disabled(). This switch statement is never reached if any of the cases in the first block matched, or if irqs_disabled is true.

This changes the default behavior of bpf_obj_free_fields() to lazy, allocator driven freeing in irqs_disabled contexts for those specific field types in the first block. The fields will be recycled by the allocator, until it calls bpf_obj_free_fields in a safe context.

Fixes: 14a324f ("bpf: Wire up freeing of referenced kptr")
Reported-by: Justin Suess utilityemal77@gmail.com
Closes: https://lore.kernel.org/bpf/20260421201035.1729473-1-utilityemal77@gmail.com/
Suggested-by: Alexei Starovoitov alexei.starovoitov@gmail.com
Suggested-by: Kumar Kartikeya Dwivedi memxor@gmail.com
Cc: Mykyta Yatsenko mykyta.yatsenko5@gmail.com
Link: https://lore.kernel.org/bpf/DIG0ONMVOP0L.3QFYUPWFSKWI4@gmail.com/

Relax bpf_obj_free_fields to only cancel/free async work in irq_disabled
contexts and defer unsafe free operations such as kptr dtors, list head
and rb root destruction to a later non-irq_disabled call driven by the
allocator or map free.

Detect fields that are unsafe to free under irqs_disabled at htab
creation time. When creating a hashtab with these fields, forcibly set
BPF_F_NO_PREALLOC and use the bpf memory allocator instead.

This must happen after the fields are checked, so convert the map to a
non-prealloc one if the special fields are present, but before the map
has been fully initialized.

Enable this fix for regular, percpu, and lru hashtabs.

Fixes: 14a324f ("bpf: Wire up freeing of referenced kptr")
Reported-by: Justin Suess <utilityemal77@gmail.com>
Closes: https://lore.kernel.org/bpf/20260421201035.1729473-1-utilityemal77@gmail.com/
Suggested-by: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Suggested-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Cc: Mykyta Yatsenko <mykyta.yatsenko5@gmail.com>
Link: https://lore.kernel.org/bpf/DIG0ONMVOP0L.3QFYUPWFSKWI4@gmail.com/
Signed-off-by: Justin Suess <utilityemal77@gmail.com>
@kernel-patches-daemon-bpf kernel-patches-daemon-bpf Bot force-pushed the bpf-next_base branch 3 times, most recently from ec31e3e to b3beebb Compare May 21, 2026 10:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants