From nobody Sun Dec 14 21:56:07 2025 Received: from mail-pj1-f53.google.com (mail-pj1-f53.google.com [209.85.216.53]) (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 2EE9D1F756A for ; Tue, 21 Jan 2025 19:11:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737486671; cv=none; b=hstgojylPowJCmGfGi57+WXyQ5K2BJBAOjWlhX2z+s7E3Vh9dmYE+Vi6OQJwPeNudOA5IeQmPPoRQPSF4/z9nOJtqhIPCCVy2N7aEwjSKzBIDqO5q1fDC26D0jQKb8tCxjrqDBZBja+1Tw7TwMohGWykDDyO6BEKwFK9AzwsWC4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737486671; c=relaxed/simple; bh=36m+Ryb9CJuvJR9nbvQowFH7ClbOSrNUnAqBaTNmIQY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=a0/Sa68vWieCvs+mYyE+6Q4U0IuU9HutTmyu9FYOewwbHj3u1b0dF/PnHrl3+9ZrdRxHzR62nNKhdXZrg9Y/sgQy7XpjswA3q6FFnHSIWEWZ+aNfc86eY7Z+CEMTUJzqGhnduoWiQOD+DJ3MvyrzHlzU/6ZvrB03wvageoFlm0Q= 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=X422TL3N; arc=none smtp.client-ip=209.85.216.53 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="X422TL3N" Received: by mail-pj1-f53.google.com with SMTP id 98e67ed59e1d1-2ee989553c1so10393029a91.3 for ; Tue, 21 Jan 2025 11:11:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fastly.com; s=google; t=1737486669; x=1738091469; 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=FDwiQXOb0dtbRJ3DmhupJOiqoOo1ySphxgwOZ65b5Lc=; b=X422TL3NgiDRJB9TLcq0u5EBu2w1v5ZZuFriMvZtJRVPqlQxCpFEvdkuozAlgADo09 wrb0sKV4WC9ZNLGRbw7W4vYgPAn2rVo0JLUNJ87Fe9XyeyP9qBsPpvuz/K/ND6pG3KaF XzomIJxyA9JzADSCTyp9SgLZo2+8jQppuyxc4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737486669; x=1738091469; 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=FDwiQXOb0dtbRJ3DmhupJOiqoOo1ySphxgwOZ65b5Lc=; b=PBnGZ32MeS/8bPAO4nJrZU5hQF1svRv1KqUe1K0AcmiZmLaSMY8NQtd5vmIILwBvxd gdhcOzoPkdr+2OOgKiqrLL+aGdc478Qokatgm1VNjbp9TGZ8VJET4SyFTE1tjwDzozwm Lf6Jkm7+P0/Ts+7B8LlbaSKYqkkKjsN5VH/mIRfsT2pITDXI+c36ds+drrN8GYTEso00 8t87Qe0vTyVWrnlle//Jlhwub2W5ZMdw+fHy+yngB/JNnNnH7bAIRtKQu2FZBc1+mtcm iPezFUO+pa2PIU9tEZLiS7TuY+PZqjozyhnHjMOluavjRNlJAIuFCkptlXjJbO8BIubR jnKw== X-Forwarded-Encrypted: i=1; AJvYcCVUVQFr3oJXc/XsjtwhrhuHiksWQMuxLCc9nFr9lV2tEKgDaD/lVD6TeopN0oPBOK+59YX5FUQ/eiztVEM=@vger.kernel.org X-Gm-Message-State: AOJu0YyQEd/hixuU98gGXOJ8Q/pyaRfKQzhEai40y3xj9YvEcNd7RYVo g8US8yG8+zC2OnVn3GyRlREJswYhfTszimsVbg7k64FHG2oiNY86/SHi4mFsIo8= X-Gm-Gg: ASbGncvVSoK7Ir+BGGeNnsdDPZUGaSndwruphPyx/vU7+BxFnAel6dW3kfxQ0+A5a2s 0uzoYrgOFQFOObnQyiYBkuMNmB7cpFDnBjsTbNgDFDOxC5xu34MllPRzKK1Osh7EQ2FA3FpQmFx i4P5yIUaZY+OcymAwMw0Ay2XP+HM+i/QcrE3bPFrY0YFbdvB5uW3PNl+IHNJaok+M1+6Z07v/a0 YLbI1KOLUvqfa6AjyLOmPQ5UQTNK58BEAbszMjNyY32h0+h6kMukK1+KOwJ1O8NmKZFJ2/+jTco GA== X-Google-Smtp-Source: AGHT+IFrbRJ/R0m0SceTG3oQbb0XNatc3hOdnfunYdBKaQ+YC8e4bymwDNEoE5iJLWgFoSXDDDTmQQ== X-Received: by 2002:a17:90a:d60f:b0:2ef:31a9:95af with SMTP id 98e67ed59e1d1-2f782d32956mr26450648a91.27.1737486668611; Tue, 21 Jan 2025 11:11:08 -0800 (PST) Received: from localhost.localdomain ([2620:11a:c019:0:65e:3115:2f58:c5fd]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2f7db6ab125sm1793440a91.26.2025.01.21.11.11.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Jan 2025 11:11:08 -0800 (PST) From: Joe Damato To: netdev@vger.kernel.org Cc: gerhard@engleder-embedded.com, jasowang@redhat.com, leiyang@redhat.com, xuanzhuo@linux.alibaba.com, mkarsten@uwaterloo.ca, Jakub Kicinski , Joe Damato , Andrew Lunn , "David S. Miller" , Eric Dumazet , Paolo Abeni , Simon Horman , Kuniyuki Iwashima , Sebastian Andrzej Siewior , Lorenzo Bianconi , Alexander Lobakin , linux-kernel@vger.kernel.org (open list) Subject: [RFC net-next v3 1/4] net: protect queue -> napi linking with netdev_lock() Date: Tue, 21 Jan 2025 19:10:41 +0000 Message-Id: <20250121191047.269844-2-jdamato@fastly.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20250121191047.269844-1-jdamato@fastly.com> References: <20250121191047.269844-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 8da4c61f97b9..4709d16bada5 100644 --- a/include/linux/netdevice.h +++ b/include/linux/netdevice.h @@ -691,7 +691,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 @@ -2467,7 +2467,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. */ @@ -2694,6 +2695,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 afa2282f2604..ab361fd9efd9 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -6842,14 +6842,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: @@ -6864,7 +6874,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