From nobody Sun Dec 14 21:35:36 2025 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B11CD1917F1 for ; Thu, 16 Jan 2025 05:53:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737006808; cv=none; b=TPacdWQ3wbqGSmOvbjpS8HvynS5Owb/IKPyZ3QLtGgaXahud2RpnzRXQTzy8ztrKSMA519RHH6Y3n3PoDIJsC8C4P+LxPOUbcG7UiXxn/Ftf8yPJUuHQv3zGYQ4zqSMTd0ku+Z3NGsie3ovldTxZq7i20WYP3baWJdQB4CndR78= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737006808; c=relaxed/simple; bh=mVwktbWSoWFP1usMOPYKCXwzUeeubnCUrVQ2w9l1YuQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=gOx/mv41H+BVTz7grCnIfH+jDz+RjTIstk5Q1FoynOF4ll0d+jqMkt6qSfN4oU8xVRspGoUve4nTaV7ICC3eZtH2Y8Hu7iYgZIESkymP7biI24T2YDw7eaD5AYrL+YbkRliBKHgdOGOIEUO37WfgQBGT89fwnalKGwQepqKhIP0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=fastly.com; spf=pass smtp.mailfrom=fastly.com; dkim=pass (1024-bit key) header.d=fastly.com header.i=@fastly.com header.b=W8IuDHzq; arc=none smtp.client-ip=209.85.214.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=fastly.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=fastly.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=fastly.com header.i=@fastly.com header.b="W8IuDHzq" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-2166022c5caso7491885ad.2 for ; Wed, 15 Jan 2025 21:53:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fastly.com; s=google; t=1737006806; x=1737611606; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=bC/KHeKZrs1tP54amz5eEuxLD6Mq90VAvd7Bs8DlJ+8=; b=W8IuDHzqCQSok1vUWamvq63rULHrmt1A/F775C09EjKjhEwSa8c/zip+VYutvPsW77 zIFSpp3ShLwE1KuLMPbtgfq5+Zr2UZvSWa+P3PmBb2p2LcMR1VPKauF9Gs0P6GBYhjRY bzH4655x9QpzIpxWW/s2PO5BX/h8Aer6RdDMU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737006806; x=1737611606; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=bC/KHeKZrs1tP54amz5eEuxLD6Mq90VAvd7Bs8DlJ+8=; b=Y7IzN0vCuIPrPnCgFUEgWpuypOC8I4iGo/goa3pxKgvycy1EO13wZpt8WMdWXoA6VR GHVtdIq67/ukFH2FNwHXKvs+eqCmruSbYqv4REQnHZ0rhxcfN383SV4LoruDJAETmMAp V0gThj4pDCC081wCjzGe1ZEyia8PZfR9sWukLS3q1dG7VZ7UPTwvn2dbXTHowH6hA3oa Pp4NyXuYoj8WtHgORBd7Obt520ztajPhNihaztj2GAx0mOBKjioXpJ3yQs/PUuUFLRGY LhJ+L026Jx29010QCoV+iP3ZOFUEkALC6N8FbK8v38Me8k9EASPA9ABMWVDArZwiMhFZ EyZA== X-Forwarded-Encrypted: i=1; AJvYcCW3kTyo2Y882uAo8Nh3SbSK+gHQO/YT118Q7Vpxlh88zRlPSM/aFYXzz4E/toZ1dYUzcPXtQFVGjFjl/hc=@vger.kernel.org X-Gm-Message-State: AOJu0YxZXhB9Uh1O5gUOSbR4snsDDMcKLMzeWFnTMNodlL7fMJ0fAmn9 KnY+wS8VzBafglbWQI8wcjgv08PMU7YWFuhPirx7tLhsU94ayetRroWL2VsJW4o= X-Gm-Gg: ASbGncv2FsdG8ygMugpeqXTUkdqWwck2cLm74Gd6J75i7S6IGClyTTwu7jVOpODfEl+ UsVhZCbaAPqtPk/gvBMyEBZZJ5QIsgviv0+WubpVZJixBhk08ef1Q+UnEee0THYQnyOOpPGzxfZ C0Hx/LE82o+rqskAFxsun0f40KgBI7JvG97PC3k6lIAI4CHSntOau0ssToWC5PpOmGKpQF0CfcI 8qq1kll8ic4jpd6XpoHMN+u5NucQ8xWohiagRZu2yUwO5SErJMVFW67Dbb4PkMR X-Google-Smtp-Source: AGHT+IGntTkOMJEdO2lJ/RT1ILNh9iXtgR2mfbiueSHch1JtTC7D230UEJorte+y+OkqI+IHqifhdA== X-Received: by 2002:a17:902:e5c2:b0:215:b33b:e26d with SMTP id d9443c01a7336-21a83f55103mr529430015ad.21.1737006805997; Wed, 15 Jan 2025 21:53:25 -0800 (PST) Received: from localhost.localdomain ([2620:11a:c019:0:65e:3115:2f58:c5fd]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f22c991sm91249655ad.168.2025.01.15.21.53.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jan 2025 21:53:25 -0800 (PST) From: Joe Damato To: netdev@vger.kernel.org Cc: gerhard@engleder-embedded.com, jasowang@redhat.com, leiyang@redhat.com, mkarsten@uwaterloo.ca, Jakub Kicinski , Joe Damato , "David S. Miller" , Eric Dumazet , Paolo Abeni , Simon Horman , Andrew Lunn , Sebastian Andrzej Siewior , Lorenzo Bianconi , Alexander Lobakin , linux-kernel@vger.kernel.org (open list) Subject: [PATCH net-next v2 1/4] net: protect queue -> napi linking with netdev_lock() Date: Thu, 16 Jan 2025 05:52:56 +0000 Message-Id: <20250116055302.14308-2-jdamato@fastly.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20250116055302.14308-1-jdamato@fastly.com> References: <20250116055302.14308-1-jdamato@fastly.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Jakub Kicinski netdev netlink is the only reader of netdev_{,rx_}queue->napi, and it already holds netdev->lock. Switch protection of the writes to netdev->lock as well. Add netif_queue_set_napi_locked() for API completeness, but the expectation is that most current drivers won't have to worry about locking any more. Today they jump thru hoops to take rtnl_lock. Signed-off-by: Jakub Kicinski Reviewed-by: Joe Damato --- v2: - Added in v2 from Jakub. include/linux/netdevice.h | 9 +++++++-- include/net/netdev_rx_queue.h | 2 +- net/core/dev.c | 16 +++++++++++++--- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h index 8308d9c75918..c7201642e9fb 100644 --- a/include/linux/netdevice.h +++ b/include/linux/netdevice.h @@ -690,7 +690,7 @@ struct netdev_queue { * slow- / control-path part */ /* NAPI instance for the queue - * Readers and writers must hold RTNL + * Readers and writers must hold netdev->lock */ struct napi_struct *napi; =20 @@ -2458,7 +2458,8 @@ struct net_device { * Partially protects (writers must hold both @lock and rtnl_lock): * @up * - * Also protects some fields in struct napi_struct. + * Also protects some fields in: + * struct napi_struct, struct netdev_queue, struct netdev_rx_queue * * Ordering: take after rtnl_lock. */ @@ -2685,6 +2686,10 @@ static inline void *netdev_priv(const struct net_dev= ice *dev) void netif_queue_set_napi(struct net_device *dev, unsigned int queue_index, enum netdev_queue_type type, struct napi_struct *napi); +void netif_queue_set_napi_locked(struct net_device *dev, + unsigned int queue_index, + enum netdev_queue_type type, + struct napi_struct *napi); =20 static inline void netdev_lock(struct net_device *dev) { diff --git a/include/net/netdev_rx_queue.h b/include/net/netdev_rx_queue.h index 596836abf7bf..9fcac0b43b71 100644 --- a/include/net/netdev_rx_queue.h +++ b/include/net/netdev_rx_queue.h @@ -23,7 +23,7 @@ struct netdev_rx_queue { struct xsk_buff_pool *pool; #endif /* NAPI instance for the queue - * Readers and writers must hold RTNL + * Readers and writers must hold netdev->lock */ struct napi_struct *napi; struct pp_memory_provider_params mp_params; diff --git a/net/core/dev.c b/net/core/dev.c index 782ae3ff3f8d..528478cd8615 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -6851,14 +6851,24 @@ EXPORT_SYMBOL(dev_set_threaded); */ void netif_queue_set_napi(struct net_device *dev, unsigned int queue_index, enum netdev_queue_type type, struct napi_struct *napi) +{ + netdev_lock(dev); + netif_queue_set_napi_locked(dev, queue_index, type, napi); + netdev_unlock(dev); +} +EXPORT_SYMBOL(netif_queue_set_napi); + +void netif_queue_set_napi_locked(struct net_device *dev, + unsigned int queue_index, + enum netdev_queue_type type, + struct napi_struct *napi) { struct netdev_rx_queue *rxq; struct netdev_queue *txq; =20 if (WARN_ON_ONCE(napi && !napi->dev)) return; - if (dev->reg_state >=3D NETREG_REGISTERED) - ASSERT_RTNL(); + netdev_assert_locked_or_invisible(dev); =20 switch (type) { case NETDEV_QUEUE_TYPE_RX: @@ -6873,7 +6883,7 @@ void netif_queue_set_napi(struct net_device *dev, uns= igned int queue_index, return; } } -EXPORT_SYMBOL(netif_queue_set_napi); +EXPORT_SYMBOL(netif_queue_set_napi_locked); =20 static void napi_restore_config(struct napi_struct *n) { --=20 2.25.1