nf_flow_offload_gc_step() checks NF_FLOW_PENDING once at entry, before the teardown decision that may set NF_FLOW_TEARDOWN and free the flow via flow_offload_del(). A concurrent caller can set NF_FLOW_PENDING and queue a FLOW_CLS_REPLACE work item holding a raw pointer to the flow after the early check but before teardown is decided, so the flow is freed via RCU while the queued work item can still dereference and clear NF_FLOW_PENDING on offload->flow. Move the NF_FLOW_PENDING check after the teardown decision and pair it with a full memory barrier, so a caller that sets the pending bit after the barrier is guaranteed to observe NF_FLOW_TEARDOWN set and bail out. This complements commit 0abb54363974 ("netfilter: flowtable: do not hw offload flows in teardown state"), which provides the caller-side teardown check in nf_flow_offload_refresh(). Found with an LLM-assisted code review tool and verified by inspection. Build-tested only; reproducing the race at runtime requires a hardware-offloaded flowtable device. Fixes: 7c549fb7eecd ("netfilter: flowtable: generalize pending status bit") Cc: stable@vger.kernel.org Signed-off-by: Qican Ma --- v4: drop the work_alloc()-side teardown check, which duplicates the caller-side check added by 0abb54363974 in net-next, per Pablo's review; keep only the GC-side interlock. Generated against net-next. v3: rebase onto net-next with 0abb54363974 included; note how the check inside work_alloc() complements the refresh-side check and why the GC-side barrier is still needed. No code changes. v2: resend via direct SMTP without line-wrapping corruption in the diff. net/netfilter/nf_flow_table_core.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/net/netfilter/nf_flow_table_core.c b/net/netfilter/nf_flow_table_core.c index d5652e45ee04..7b11c4935aa5 100644 --- a/net/netfilter/nf_flow_table_core.c +++ b/net/netfilter/nf_flow_table_core.c @@ -580,9 +580,6 @@ static void nf_flow_offload_gc_step(struct nf_flowtable *flow_table, { bool teardown; - if (test_bit(NF_FLOW_PENDING, &flow->flags)) - return; - teardown = test_bit(NF_FLOW_TEARDOWN, &flow->flags); if (nf_flow_has_expired(flow) || @@ -596,6 +593,11 @@ static void nf_flow_offload_gc_step(struct nf_flowtable *flow_table, nf_flow_table_extend_ct_timeout(flow->ct); } + /* Pairs with test_and_set_bit(NF_FLOW_PENDING) in work_alloc(). */ + smp_mb(); + if (test_bit(NF_FLOW_PENDING, &flow->flags)) + return; + if (teardown) { if (test_bit(NF_FLOW_HW, &flow->flags)) { if (!test_bit(NF_FLOW_HW_DYING, &flow->flags)) -- 2.50.1 (Apple Git-155)