Netdev List
 help / color / mirror / Atom feed
From: Steffen Klassert <steffen.klassert@secunet.com>
To: Jianbo Liu <jianbol@nvidia.com>
Cc: <netdev@vger.kernel.org>, <ipsec@lists.linux.dev>,
	Herbert Xu <herbert@gondor.apana.org.au>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Simon Horman <horms@kernel.org>,
	Leon Romanovsky <leon@kernel.org>, Raed Salem <raeds@nvidia.com>,
	Saeed Mahameed <saeedm@nvidia.com>,
	Cen Zhang <zzzccc427@gmail.com>, <linux-kernel@vger.kernel.org>,
	Carolina Jubran <cjubran@nvidia.com>
Subject: Re: [PATCH ipsec] xfrm: Serialize offload stats updates with x->lock
Date: Tue, 1 Dec 2026 07:34:58 +0100	[thread overview]
Message-ID: <aw5qktTFvRuukgT_@gauss4> (raw)
In-Reply-To: <20261125020835.1132797-1-jianbol@nvidia.com>

On Wed, Nov 25, 2026 at 04:08:35AM +0200, Jianbo Liu wrote:
> xfrm_dev_state_update_stats() calls into the driver, which adds to
> x->curlft and x->stats what the device counted since the last call.
> Callers use different locks, so two of them can refresh the same
> state at once and count the same packets twice, and a dump can copy
> the counters while another caller updates them.
> 
> Serialize all device stats refreshes with x->lock and assert it in
> xfrm_dev_state_update_stats(). Build the SA message of a netlink
> dump, get or notification under the same lock.
> 
> The SA dump and xfrm_state_update_stats() walk the states under
> xfrm_state_lock, where x->lock cannot be taken: the lock order is
> x->lock, then xfrm_state_lock. Add xfrm_state_walk_batch(), which
> takes references on at most 32 states per xfrm_state_lock hold, and
> handle each state under x->lock after the list lock is dropped.
> States rejected by the dump filter count against the 32, so a filter
> that matches few states cannot keep the list lock held over the whole
> list. xfrm_state_walk() cannot give this bound: it calls back only
> for states that pass the filter. Walk markers and states being
> deleted do not count, so a call always covers 32 states or reaches
> the end of the list.
> 
> Keep the refresh right before each state is copied, so every part of
> a multi-part dump still has current counters. When a state does not
> fit into the skb, put the walk marker back in front of it and drop
> the references, so no state stays referenced between two dump calls.
> If the state was unlinked meanwhile, skip it and go on.
> 
> copy_to_user_state() cannot take x->lock itself: build_expire() calls
> it with the lock held. Move the refresh out of it and let each caller
> refresh under x->lock. PF_KEY never refreshed and does not change.
> 
> Fixes: c9fa320b00cf ("xfrm: copy_to_user_state fetch offloaded SA packets/bytes statistics")
> Fixes: f9f221c98fd8 ("xfrm: get global statistics from the offloaded device")
> Signed-off-by: Jianbo Liu <jianbol@nvidia.com>
> Reviewed-by: Carolina Jubran <cjubran@nvidia.com>
> ---
> Cen Zhang's "[PATCH ipsec v2 1/2] xfrm: protect offload statistics
> updates from device teardown" [1] adds x->lock around the same two
> calls in xfrm_state_netlink() and xfrm_notify_sa(), so only one of
> the two patches can be applied. This patch takes the lock in the SA
> dump and in xfrm_state_update_stats() as well.
> 
> [1] https://lore.kernel.org/netdev/pm-xfrm-core-objects-candidate-0050-v4-3c6b8c5369a802e9be0e-1@gmail.com/
> 
>  include/net/xfrm.h    | 12 ++++++
>  net/xfrm/xfrm_state.c | 87 +++++++++++++++++++++++++++++++++++++++----
>  net/xfrm/xfrm_user.c  | 45 ++++++++++++++++++++--
>  3 files changed, 133 insertions(+), 11 deletions(-)

Applied, thanks a lot!

      parent reply	other threads:[~2026-12-01  6:35 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25  2:08 [PATCH ipsec] xfrm: Serialize offload stats updates with x->lock Jianbo Liu
2026-11-25  2:12 ` netdev-bot+sinfo
2026-12-01  6:34 ` Steffen Klassert [this message]

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=aw5qktTFvRuukgT_@gauss4 \
    --to=steffen.klassert@secunet.com \
    --cc=cjubran@nvidia.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=herbert@gondor.apana.org.au \
    --cc=horms@kernel.org \
    --cc=ipsec@lists.linux.dev \
    --cc=jianbol@nvidia.com \
    --cc=kuba@kernel.org \
    --cc=leon@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=raeds@nvidia.com \
    --cc=saeedm@nvidia.com \
    --cc=zzzccc427@gmail.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox