From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=62.96.220.36 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796106903; b=MHKyv38yt4I9C+Oti801K1cjdwPNrH5JvzGGtI+BJhSycKZYQzGw0hi69caiikaUIXSm gF3+zWvZlTgO2J27Ddae2kU42DxJ2vJLqs3T+Saw4NRobKyhRSWtZLGNZ0ayQWWjqUvHu /z9LyFj+zQnkcLoWca52MevQD/zyIc3NvFN9qXBFIdjrMwngEnkYnlrJGSzj0MdSRFn5B twg3lPIJz3JiUioUdMi5xbfC132AyQ87yE6jY8eemHvp1c4NX0BjGGnknxG/hY/MtEsaR qCS1iA2KrnCbI/o7zFoUSxIYNUV8fV02Olu8gMTETHlYkaaxNZqMtmpaD5Z7ghmbK7g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796106903; h=DMARC-Filter:Received:X-Virus-Scanned:Received:Received:DKIM-Filter: DKIM-Signature:Received:Received:Date:From:To:CC:Subject:Message-ID: References:MIME-Version:Content-Type:Content-Disposition:In-Reply-To: X-ClientProxiedBy; bh=QDq+ALc/96PAN/wlzFMiyODso/vyAdhmaHNBrJ1AEEI=; b=fa/tu/obWFJ/1k6GasPF5EbhnT5Rn7k7THGIen5mQ6rQfScPIoavfgJs3FCmbzKUFtfX w0gGFDUq0gSQhHymhQwnBRexDfYXLS9GSi3rUkoMTTiFvSDGZlfXvC1umnv1xqxkQjYps g59F9hL+2tZcfE34yTBAdblD7pak7wrl5nC2psIchjBEL8qv4dkYATk3rZxxFnk6igUES vwyyIUX8B0S5MvytZaocWiQdkWCM9x+FBNFsFMaMf9utBroGZ2Ry6vbqgPkiCPsbvvHU+ /2gNKB4MEhweooBmEsl9uuhGDx36dOhZ01gjGv6dmYDBXmkeFEVM56w8xEPglYKNrdw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=secunet.com; spf=pass smtp.mailfrom=secunet.com; dkim=pass header.d=secunet.com header.i=@secunet.com header.a=rsa-sha256 header.s=202301 header.b=c+uwUA7w; arc=none smtp.remote-ip=62.96.220.36 Received: from mx1.secunet.com (mx1.secunet.com [62.96.220.36]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2F7A4606A336; Tue, 1 Dec 2026 06:35:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2F7A4606A336 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=secunet.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=secunet.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=secunet.com header.i=@secunet.com header.a=rsa-sha256 header.s=202301 header.b=c+uwUA7w Received: from localhost (localhost [127.0.0.1]) by mx1.secunet.com (Postfix) with ESMTP id D94D520684; Tue, 1 Dec 2026 07:35:00 +0100 (CET) X-Virus-Scanned: by secunet Received: from mx1.secunet.com ([127.0.0.1]) by localhost (mx1.secunet.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id L0sHlidqSDrr; Tue, 1 Dec 2026 07:34:59 +0100 (CET) Received: from EXCH-01.secunet.de (rl1.secunet.de [10.32.0.231]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.secunet.com (Postfix) with ESMTPS id D61B120590; Tue, 1 Dec 2026 07:34:59 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.secunet.com D61B120590 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=secunet.com; s=202301; t=1796106899; bh=QDq+ALc/96PAN/wlzFMiyODso/vyAdhmaHNBrJ1AEEI=; h=Date:From:To:CC:Subject:References:In-Reply-To:From; b=c+uwUA7wqAxS4Y8H2HnD4abDMPK/CaXICqDGyF/4kr/EYbJ/3m8wur8ffZt9AH3P3 hOY+bsEHnUF+z44vRIxFZdKo9S0i1cA1dM0iysUxC2BQzNJ3iUujsqpOtTXZOxCbpo Jv6FStWr3WveG0gDegrQ/TJWPjNc3Gjel//3WEFg0N6VBhjz7IotTi0YhwY/+oopGH xcA0w/+ALgmzLcPOU+3GqlxDJ991wSEW8UoGM83ZZaDkEM0XGtAFEtww6hFXBM99Qx /5gGieYAgcRKXSaat3XHs0RkTKdYiMzENdnRKihT6GsT2JeEMvBaeO7dMYJiytW7jF 32DqiFdyXHfrg== Received: from gauss4 (10.182.7.102) by EXCH-01.secunet.de (10.32.0.171) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.49; Tue, 1 Dec 2026 07:34:59 +0100 Received: (nullmailer pid 779723 invoked by uid 1000); Tue, 01 Dec 2026 06:34:58 -0000 Date: Tue, 1 Dec 2026 07:34:58 +0100 From: Steffen Klassert To: Jianbo Liu CC: , , Herbert Xu , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Leon Romanovsky , Raed Salem , Saeed Mahameed , Cen Zhang , , Carolina Jubran Subject: Re: [PATCH ipsec] xfrm: Serialize offload stats updates with x->lock Message-ID: References: <20261125020835.1132797-1-jianbol@nvidia.com> X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Disposition: inline In-Reply-To: <20261125020835.1132797-1-jianbol@nvidia.com> X-ClientProxiedBy: EXCH-02.secunet.de (10.32.0.172) To EXCH-01.secunet.de (10.32.0.171) 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 > Reviewed-by: Carolina Jubran > --- > 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!