Thread View: linux.kernel.netdev
3 messages
3 total messages
Started by Qican Ma
Tue, 24 Nov 2026 07:54
[PATCH nf v2] netfilter: flowtable: interlock NF_FLOW_TEARDOWN with NF_FLOW_PENDING
Author: Qican Ma
Date: Tue, 24 Nov 2026 07:54
Date: Tue, 24 Nov 2026 07:54
79 lines
14070 bytes
14070 bytes
nf_flow_offload_gc_step() checks NF_FLOW_PENDING once at entry before
setting NF_FLOW_TEARDOWN and potentially freeing the flow via
flow_offload_del(). Concurrently, flow_offload_add() or a datapath caller
that looked up the flow prior to teardown can call
nf_flow_offload_work_alloc(), set NF_FLOW_PENDING, and queue a
FLOW_CLS_REPLACE work item holding a raw pointer to the flow without
rechecking NF_FLOW_TEARDOWN. If the pending bit is set after the GC check,
flow_offload_del() frees the flow via RCU while the queued work item can
still dereference and clear NF_FLOW_PENDING on offload->flow.
Interlock the two flags: in nf_flow_offload_gc_step(), check
NF_FLOW_PENDING after NF_FLOW_TEARDOWN has been observed or set, paired
with a full memory barrier; in nf_flow_offload_work_alloc(), recheck
NF_FLOW_TEARDOWN after test_and_set_bit(NF_FLOW_PENDING) for
FLOW_CLS_REPLACE and bail out if set. Restrict the teardown check to
FLOW_CLS_REPLACE because GC itself queues FLOW_CLS_DESTROY and
FLOW_CLS_STATS work on flows already marked for teardown.
Found with an LLM-assisted code review tool and verified by inspection on
v7.4-rc4 (b6a9fa34ce4). 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 <goonsomeway@gmail.com>
---
v2: resend via direct SMTP without line-wrapping corruption in the diff.
net/netfilter/nf_flow_table_core.c | 8 +++++---
net/netfilter/nf_flow_table_offload.c | 7 +++++++
2 files changed, 12 insertions(+), 3 deletions(-)
diff --git a/net/netfilter/nf_flow_table_core.c b/net/netfilter/nf_flow_table_core.c
index 2aa2647ed9d..fcd37f7a37f 100644
--- a/net/netfilter/nf_flow_table_core.c
+++ b/net/netfilter/nf_flow_table_core.c
@@ -578,9 +578,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) ||
@@ -594,6 +591,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))
diff --git a/net/netfilter/nf_flow_table_offload.c b/net/netfilter/nf_flow_table_offload.c
index 4365859220e..402a46e1645 100644
--- a/net/netfilter/nf_flow_table_offload.c
+++ b/net/netfilter/nf_flow_table_offload.c
@@ -1088,6 +1088,13 @@ nf_flow_offload_work_alloc(struct nf_flowtable *flowtable,
if (test_and_set_bit(NF_FLOW_PENDING, &flow->flags))
return NULL;
+ /* Pairs with smp_mb() in nf_flow_offload_gc_step(). */
+ if (cmd == FLOW_CLS_REPLACE &&
+ test_bit(NF_FLOW_TEARDOWN, &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.50.1 (Apple Git-155)
Re: [PATCH nf v2] netfilter: flowtable: interlock NF_FLOW_TEARDOWN with NF_FLOW_PENDING
Author: Pablo Neira Ayuso
Date: Tue, 24 Nov 2026 13:09
Date: Tue, 24 Nov 2026 13:09
95 lines
12019 bytes
12019 bytes
On Tue, Nov 24, 2026 at 03:54:16PM +0800, Qican Ma wrote:
> nf_flow_offload_gc_step() checks NF_FLOW_PENDING once at entry before
> setting NF_FLOW_TEARDOWN and potentially freeing the flow via
> flow_offload_del(). Concurrently, flow_offload_add() or a datapath caller
> that looked up the flow prior to teardown can call
> nf_flow_offload_work_alloc(), set NF_FLOW_PENDING, and queue a
> FLOW_CLS_REPLACE work item holding a raw pointer to the flow without
> rechecking NF_FLOW_TEARDOWN. If the pending bit is set after the GC check,
> flow_offload_del() frees the flow via RCU while the queued work item can
> still dereference and clear NF_FLOW_PENDING on offload->flow.
>
> Interlock the two flags: in nf_flow_offload_gc_step(), check
> NF_FLOW_PENDING after NF_FLOW_TEARDOWN has been observed or set, paired
> with a full memory barrier; in nf_flow_offload_work_alloc(), recheck
> NF_FLOW_TEARDOWN after test_and_set_bit(NF_FLOW_PENDING) for
> FLOW_CLS_REPLACE and bail out if set. Restrict the teardown check to
> FLOW_CLS_REPLACE because GC itself queues FLOW_CLS_DESTROY and
> FLOW_CLS_STATS work on flows already marked for teardown.
>
> Found with an LLM-assisted code review tool and verified by inspection on
> v7.4-rc4 (b6a9fa34ce4). Build-tested only; reproducing the race at runtime
> requires a hardware-offloaded flowtable device.
This is the refresh race.
> Fixes: 7c549fb7eecd ("netfilter: flowtable: generalize pending status bit")
> Cc: stable@vger.kernel.org
> Signed-off-by: Qican Ma <goonsomeway@gmail.com>
> ---
> v2: resend via direct SMTP without line-wrapping corruption in the diff.
>
> net/netfilter/nf_flow_table_core.c | 8 +++++---
> net/netfilter/nf_flow_table_offload.c | 7 +++++++
> 2 files changed, 12 insertions(+), 3 deletions(-)
>
> diff --git a/net/netfilter/nf_flow_table_core.c b/net/netfilter/nf_flow_table_core.c
> index 2aa2647ed9d..fcd37f7a37f 100644
> --- a/net/netfilter/nf_flow_table_core.c
> +++ b/net/netfilter/nf_flow_table_core.c
> @@ -578,9 +578,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) ||
> @@ -594,6 +591,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))
> diff --git a/net/netfilter/nf_flow_table_offload.c b/net/netfilter/nf_flow_table_offload.c
> index 4365859220e..402a46e1645 100644
> --- a/net/netfilter/nf_flow_table_offload.c
> +++ b/net/netfilter/nf_flow_table_offload.c
> @@ -1088,6 +1088,13 @@ nf_flow_offload_work_alloc(struct nf_flowtable *flowtable,
> if (test_and_set_bit(NF_FLOW_PENDING, &flow->flags))
> return NULL;
>
> + /* Pairs with smp_mb() in nf_flow_offload_gc_step(). */
> + if (cmd == FLOW_CLS_REPLACE &&
> + test_bit(NF_FLOW_TEARDOWN, &flow->flags)) {
> + clear_bit(NF_FLOW_PENDING, &flow->flags);
> + return NULL;
> + }
Do you have this patch in your tree?
commit 0abb543639741a3d1124e7a1a33fd9d3649d6079
Author: Pablo Neira Ayuso <pablo@netfilter.org>
Date: Fri Oct 16 11:16:37 2026 +0200
netfilter: flowtable: do not hw offload flows in teardown state
This patch is in net-next.
> +
> offload = kmalloc_obj(struct flow_offload_work, GFP_ATOMIC);
> if (!offload) {
> clear_bit(NF_FLOW_PENDING, &flow->flags);
> --
> 2.50.1 (Apple Git-155)
>
>
Re: [PATCH nf v2] netfilter: flowtable: interlock NF_FLOW_TEARDOWN with NF_FLOW_PENDING
Author: Qican Ma
Date: Wed, 25 Nov 2026 07:22
Date: Wed, 25 Nov 2026 07:22
30 lines
12228 bytes
12228 bytes
Hi Pablo, No - my tree is based on v7.4-rc4 mainline, which does not have 0abb54363974 yet. I have now stack-tested my patch on top of your commit on net-next: all hunks apply cleanly, and the two changes complement each other. Your commit rechecks NF_FLOW_TEARDOWN in the nf_flow_offload_refresh() caller after nf_flow_offload_work_alloc(). My patch performs the same check inside nf_flow_offload_work_alloc() for all FLOW_CLS_REPLACE callers, which covers nf_flow_offload_refresh() and also the flow_offload_add() path, and bails before the kmalloc. The other half of my patch adds the GC-side interlock that is still missing: gc_step() reads NF_FLOW_PENDING at function entry, before flow_offload_teardown() sets NF_FLOW_TEARDOWN later in the same pass. Without the barrier the following interleaving still frees the flow via RCU while a work item is queued on it: GC reads PENDING=0 -> refresh sets PENDING and reads TEARDOWN=0 -> queues work -> GC sets TEARDOWN -> flow_offload_del() I will rebase onto net-next with your commit included and send v3. Your refresh-side check still works as a second-chance bail there, or it can be folded into the work_alloc-side check - whichever you prefer. Thanks, Qican Ma
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads