All of lore.kernel.org
 help / color / mirror / Atom feed
From: Pablo Neira Ayuso <pablo@netfilter.org>
To: Qican Ma <goonsomeway@gmail.com>
Cc: fw@strlen.de, phil@nwl.cc, davem@davemloft.net,
	edumazet@kernel.org, kuba@kernel.org, pabeni@redhat.com,
	horms@kernel.org, netfilter-devel@vger.kernel.org,
	coreteam@netfilter.org, netdev@vger.kernel.org
Subject: Re: [PATCH nf v3] netfilter: flowtable: interlock NF_FLOW_TEARDOWN with NF_FLOW_PENDING
Date: Wed, 25 Nov 2026 10:05:13 +0100	[thread overview]
Message-ID: <awakyVQMHrobvvAL@chamomile> (raw)
In-Reply-To: <179559147736.38007.14255585896242834600@gmail.com>

On Wed, Nov 25, 2026 at 03:24:37PM +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.
> 
> This complements commit 0abb54363974 ("netfilter: flowtable: do not hw
> offload flows in teardown state"), which checks the teardown bit in the
> nf_flow_offload_refresh() caller: checking inside
> nf_flow_offload_work_alloc() covers all FLOW_CLS_REPLACE callers,
> including flow_offload_add(), and the GC-side barrier closes the
> remaining window where GC reads NF_FLOW_PENDING at entry before
> NF_FLOW_TEARDOWN is set.
> 
> 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>
> ---
> 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 +++++---
>  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;
> +	}

Not correct, this is duplicating an already existing check 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)
> 

  reply	other threads:[~2026-11-25  9:05 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25  7:24 [PATCH nf v3] netfilter: flowtable: interlock NF_FLOW_TEARDOWN with NF_FLOW_PENDING Qican Ma
2026-11-25  9:05 ` Pablo Neira Ayuso [this message]
2026-11-25 11:04   ` Qican Ma

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=awakyVQMHrobvvAL@chamomile \
    --to=pablo@netfilter.org \
    --cc=coreteam@netfilter.org \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=fw@strlen.de \
    --cc=goonsomeway@gmail.com \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=phil@nwl.cc \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.