[PATCH net 1/4] netfilter: flowtable: do not queue offload work after DESTROY

From: Florian Fainelli

Date: Wed Nov 18 2026 - 15:14:15 EST


nf_flow_offload_refresh() can be reached by a packet path reader which
looked the flow up before it was torn down. NF_FLOW_PENDING keeps the
DESTROY work from being queued while another work item is pending, but
once the DESTROY work has run and cleared the bit, a reader can take it
again and queue a REPLACE (or STATS) work for a flow which is already
being removed.

GC removes a hardware flow after NF_FLOW_HW_DEAD is set, and frees it after
an RCU grace period, which the reader can outlive in a preemptible RCU
read-side section. The queued work then dereferences a freed flow and
the flowtable, and can program the hardware again with a rule keyed by
the freed flow's cookie.

NF_FLOW_HW_DYING is set when DESTROY is queued, and DESTROY needs
NF_FLOW_PENDING, so a work item which holds the pending bit and finds
NF_FLOW_HW_DYING clear prevents DESTROY from being queued until it
completes. Check it after taking the pending bit, and do not queue
anything other than DESTROY when it is set.

Not reproduced: this is a race between a long RCU read-side section and
the GC and offload workers. Found by review of a related change.

Fixes: c29f74e0df7a ("netfilter: nf_flow_table: hardware offload support")
Signed-off-by: Florian Fainelli <florian.fainelli@xxxxxxxxxxxx>
---
net/netfilter/nf_flow_table_offload.c | 13 +++++++++++++
1 file changed, 13 insertions(+)

diff --git a/net/netfilter/nf_flow_table_offload.c b/net/netfilter/nf_flow_table_offload.c
index 4365859220e6..62f460d38efa 100644
--- a/net/netfilter/nf_flow_table_offload.c
+++ b/net/netfilter/nf_flow_table_offload.c
@@ -1088,6 +1088,19 @@ nf_flow_offload_work_alloc(struct nf_flowtable *flowtable,
if (test_and_set_bit(NF_FLOW_PENDING, &flow->flags))
return NULL;

+ /* GC only removes a hardware flow after its DESTROY work has run, and
+ * NF_FLOW_PENDING keeps another one from being queued, so nothing can
+ * free the flow while this work is pending, unless DESTROY was queued
+ * before we took the pending bit. A reader which found the flow before
+ * it was torn down must not queue more work then, as the flow could be
+ * freed before that work runs.
+ */
+ if (cmd != FLOW_CLS_DESTROY &&
+ test_bit(NF_FLOW_HW_DYING, &flow->flags)) {
+ clear_bit(NF_FLOW_PENDING, &flow->flags);
+ return NULL;
+ }
+
offload = kmalloc_obj(struct flow_offload_work, GFP_ATOMIC);
if (!offload) {
clear_bit(NF_FLOW_PENDING, &flow->flags);
--
2.53.0