From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.170 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796568309; b=sYwey9HqatBQTTMuTpBp/H/g9QbjWaK1ryKDUxN/o6ZxU8DlulBvBmJm8hNabT7TT9qk cZeBGhUZhCUs2XS0m2BYlWiuSKlqFKgKlJr1rBgZnvN/u91Ftz0t8FzOM81lATzucbyZc BMSiJL3PPd1rA45Sh6vCOt2nFcd97V3dD/c/WdgG7sYPzRgTZJR1TfsJZYV+70YH5BK90 lmXP/kK4RZSkWkXRhtLth6RRSgbsWDEs4CHsOj6XAsJvZdK9zHUCLMP/aAIhzuoY3sME4 QpEGvRMRVzyVG/RXkiddqhowMCkceCZ38NY1xN3GIkxFjjv4kOlonQUoIoFB7OX84pA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796568309; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:Date:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding: Message-Id:References:In-Reply-To:To:Cc:X-Mailer; bh=oqNmH15lUVbdkY/+iYhCM1FdRdfg1Cd1JlhWSjgy8uY=; b=ReoHEVoe2epSiHR9glm2Z7YretQ//KPKYMFhy7THU7HVOXoY/iR/9PfWL4Bg5E05xDiG ZE6Fggd7eORaROj8IeZvWQN3/evX8UnERnaSKkz+AQEmJySbiG+xmWa8Msl2CiHqUlxao ZCC5YrJuZ3pcP2SLUho1LDfhCG+MWrtLBxO9w0ghqcLZ8AxH7cF7KX1PMvzKeaHD2KXz7 sKaOrolw8ywYKT4dz8McW89Lyr8o4f3ys5p6C6DTbtsOCcHXslmsXyskySBidummsKiTB 6nXAt21wu/jchf7tQJpNoTkYYzFKtKU1fOLhw/bRMzoq93qfUsiCiM2WXo7CT0XwJZw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=jJlA7KAG; arc=none smtp.remote-ip=74.125.82.170 Received: from mail-dy1-f170.google.com (mail-dy1-f170.google.com [74.125.82.170]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8E15C606A32F for ; Sun, 6 Dec 2026 14:45:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8E15C606A32F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=jJlA7KAG Received: by mail-dy1-f170.google.com with SMTP id 5a478bee46e88-386d1859e1cso2435552eec.0 for ; Sun, 06 Dec 2026 06:45:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796568309; x=1797173109; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=oqNmH15lUVbdkY/+iYhCM1FdRdfg1Cd1JlhWSjgy8uY=; b=jJlA7KAGIGAOF2cirXQ+J9x7M5pqXJ/zgPlA4N8oHsEcJ7T+fNGLSDLwEnL4KroJVk WUII6ZlmyMU/jnbEKjJULhC9/uYHQoOuG0kPdTaPB4AUrf7a/e+umJqegBcVgy9hmTVv RBwN81QtWRQX/oibTkJ/afEaww6X3ddP9xfD0PwFNjE2pOPKW3rMwxm3Y5SEU2nAzHKF ApMeFbhzGoDOZLtxBafVnL+7yXOaPdFulCqdikBVPtDNC42UMHxsuv4r26hC7pQ/yOXE lwjWjEM6mmLSoB6eQ5KGNh4hgUb5as21ME5tF0EcS6wMxijU8MWWQHdnNp7NZtOevTAI 2j2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796568309; x=1797173109; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=oqNmH15lUVbdkY/+iYhCM1FdRdfg1Cd1JlhWSjgy8uY=; b=mnfaEf+Kbmo1Pw9jejt7q2uDAxTvrXt6qbooSZnhHFXbJmtiCpD20lYty3eAvNS0qP USRRgdzKoyybj5PKmM6QBQqqplT2vv2J3LJ59VS/GSgmfK243d2XXY3x16wOpKUhZQF8 thk73BFaQsqP0bfGDVIHNj3K/OTQtitzrC2Rp6bRuextE/UTZ9f1N+1oIAXNdlnrJ/SQ +FrtLgIVy6XoVLdhpmN9kfT2o23q9GV+3WV/B3Xr24sFFJyqsJVExGnuu9IxKGWpUGbf Pfe/4oSivELYQib+dG4ihpzvEwHPPgBk5w+4ue8PRYPN1AhHXjYJAku486xboaKE+Hv1 jKEA== X-Forwarded-Encrypted: i=1; AL+hErHUXzU68NUj1uibL9ZVuDSkqBVlQ2COG1YX5XvP2gf5mHQtJCKoqZHzT/N5tuHZfUBtOKyw+oj2jIztFew=@vger.kernel.org X-Gm-Message-State: AB4q+uEDLE4hSFdy3l2vWXq+tJu9GeYTKLhJz7j/G5+5cf0nm7cXpUWf lt4/HbYzRRDG4+qiqOSiGZYNaQBobnRZu6sGi7m23NV8VMUZmaqqjOq+ X-Gm-Gg: AZFdTZffuW3cOnyOyXKeBJIlpDnsuntuvjharXEwAp0Or0MVD7gn+FKGMDlkauHC8eP IdvMnDM2O1BwT/XGE4fI0j/UI/HuLXdOxj/5L4cATc05HTiFkuR5a5WJH0iOvHyGWUVxaqZXAPc Tm0INSzUUyjEH9Kke+OLTaXE+JNDFjqkkK1T6yrlXB3YwWN64mjyVCVT2U1AP9OBl6cNRj9weUq jBAeMHH3KcXRxu7ximdn6A31GSVGx4zuIvynf9dx0E2C1201coSGURm3DdcDjUw6beFei12KXAj Jy6jM0Zlv97uZBAYWYXk/N6zvS0tirTPgmKMLG0ialw59+U/Tgte4wSCNvpqeoQPBmezbBC88ld 92CamooBr/i4qQAbVY+B8eY+h/t9lTRktr9ZLCC9/TMioWB+zIfVha5WT543FeaFyNv6pnvsxhE rfZYRUC1lQcCXVze5JZ8SDeENpEjdsWqlZRqrgEO6sh2j5gZdvXd3FyslpalY2dsJYuaH0UPeOE /bJfCFjvTQyvTAKKRJcYBiOkTfy5MuI/aFDzcsRAyM0HqIFxBKL9F3eTZPYsL3ycrJcN7WKduBL 3qKgdDIaKG3DpYfyaHXq9HLs3JaxeVA6ND2aUxzQQlqiQ9Kuau3QEZWobNBCdDJd3oGrcoz5TWD 02rbLDR74AdbG3KHsOaSE6QWsSdp4TuW42vFqTIuF2J+SIQjnGhBPpiL92ayYmUhEmabvThtC/0 Bq3ZVO+x5pBBk+yzF6nrE4BTlXNlsgrukR X-Received: by 2002:a05:7301:923:b0:383:9f52:e54d with SMTP id 5a478bee46e88-387520ed6aemr8215366eec.8.1796568308364; Sun, 06 Dec 2026 06:45:08 -0800 (PST) Received: from SeungJong-Mac-mini.local ([210.117.237.143]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-387521e1c55sm28623252eec.16.2026.12.06.06.45.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 06 Dec 2026 06:45:08 -0800 (PST) From: SeungJong Ha Date: Sun, 06 Dec 2026 23:44:45 +0900 Subject: [PATCH RFC net-next 4/5] net: resize tun and tap rings from rtnl_link_ops X-Mailing-List: linux-kernel@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="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261206-tc1-txqlen-commit-v1-4-ec9f34dce973@gmail.com> References: <20261206-tc1-txqlen-commit-v1-0-ec9f34dce973@gmail.com> In-Reply-To: <20261206-tc1-txqlen-commit-v1-0-ec9f34dce973@gmail.com> To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Simon Horman , Jamal Hadi Salim , Jiri Pirko , Andrew Lunn , Willem de Bruijn , Jason Wang Cc: "Michael S. Tsirkin" , John Fastabend , linux-kernel@vger.kernel.org, netdev@vger.kernel.org, SeungJong Ha X-Mailer: b4 0.15.2 tun, macvtap and ipvtap resize their rings when they get NETDEV_CHANGE_TX_QUEUE_LEN, and refuse the change if the resize fails. This has two problems: - The event goes to every listener, but each of these handlers returns early unless the device is its own. The notifier is only used as a way to call the driver of the device. - rtnetlink listens to the same event and runs before them. When the driver fails, RTM_NEWLINK has already announced the length that is then refused. Add rtnl_link_ops->change_tx_queue_len() and call it from netif_change_tx_queue_len() before the notifier. The three drivers move their resize there. A ndo would not do: macvtap and ipvtap use the net_device_ops of macvlan and ipvlan. The rings are still resized after dev->tx_queue_len is set and before the qdiscs are. When the resize fails, userspace sees two changes: no RTM_NEWLINK with the refused length is sent, and the error is -ENOMEM instead of the -EPERM that NOTIFY_BAD turned into. The patch was written with Claude Code (Claude Opus 5.5). Signed-off-by: SeungJong Ha --- drivers/net/ipvlan/ipvtap.c | 13 +++++--- drivers/net/macvtap.c | 13 +++++--- drivers/net/tap.c | 7 ++--- drivers/net/tun.c | 73 ++++++++++++++++++++++----------------------- include/linux/if_tap.h | 4 +-- include/net/rtnetlink.h | 5 ++++ net/core/dev.c | 6 ++++ 7 files changed, 69 insertions(+), 52 deletions(-) diff --git a/drivers/net/ipvlan/ipvtap.c b/drivers/net/ipvlan/ipvtap.c index 901697ff2169..41b538ff26f1 100644 --- a/drivers/net/ipvlan/ipvtap.c +++ b/drivers/net/ipvlan/ipvtap.c @@ -146,11 +146,20 @@ static void ipvtap_setup(struct net_device *dev) mutex_init(&vlantap->tap.lock); } +static int ipvtap_change_tx_queue_len(struct net_device *dev, + unsigned int new_len) +{ + struct ipvtap_dev *vlantap = netdev_priv(dev); + + return tap_queue_resize(&vlantap->tap, new_len); +} + static struct rtnl_link_ops ipvtap_link_ops __read_mostly = { .kind = "ipvtap", .setup = ipvtap_setup, .newlink = ipvtap_newlink, .dellink = ipvtap_dellink, + .change_tx_queue_len = ipvtap_change_tx_queue_len, .priv_size = sizeof(struct ipvtap_dev), }; @@ -206,10 +215,6 @@ static int ipvtap_device_event(struct notifier_block *unused, device_destroy(&ipvtap_class, devt); tap_free_minor(ipvtap_major, &vlantap->tap); break; - case NETDEV_CHANGE_TX_QUEUE_LEN: - if (tap_queue_resize(&vlantap->tap)) - return NOTIFY_BAD; - break; } return NOTIFY_DONE; diff --git a/drivers/net/macvtap.c b/drivers/net/macvtap.c index 32e6797014d6..0f851edfa455 100644 --- a/drivers/net/macvtap.c +++ b/drivers/net/macvtap.c @@ -147,12 +147,21 @@ static struct net *macvtap_link_net(const struct net_device *dev) return dev_net(macvlan_dev_real_dev(dev)); } +static int macvtap_change_tx_queue_len(struct net_device *dev, + unsigned int new_len) +{ + struct macvtap_dev *vlantap = netdev_priv(dev); + + return tap_queue_resize(&vlantap->tap, new_len); +} + static struct rtnl_link_ops macvtap_link_ops __read_mostly = { .kind = "macvtap", .setup = macvtap_setup, .newlink = macvtap_newlink, .dellink = macvtap_dellink, .get_link_net = macvtap_link_net, + .change_tx_queue_len = macvtap_change_tx_queue_len, .priv_size = sizeof(struct macvtap_dev), }; @@ -208,10 +217,6 @@ static int macvtap_device_event(struct notifier_block *unused, device_destroy(&macvtap_class, devt); tap_free_minor(macvtap_major, &vlantap->tap); break; - case NETDEV_CHANGE_TX_QUEUE_LEN: - if (tap_queue_resize(&vlantap->tap)) - return NOTIFY_BAD; - break; } return NOTIFY_DONE; diff --git a/drivers/net/tap.c b/drivers/net/tap.c index 3028bf90c6b4..be82458ae7da 100644 --- a/drivers/net/tap.c +++ b/drivers/net/tap.c @@ -1227,9 +1227,8 @@ struct ptr_ring *tap_get_ptr_ring(struct file *file) } EXPORT_SYMBOL_GPL(tap_get_ptr_ring); -int tap_queue_resize(struct tap_dev *tap) +int tap_queue_resize(struct tap_dev *tap, unsigned int new_len) { - struct net_device *dev = tap->dev; struct tap_queue *q; struct ptr_ring **rings; int ret = -ENOMEM; @@ -1245,10 +1244,10 @@ int tap_queue_resize(struct tap_dev *tap) rings[i++] = &q->ring; ret = ptr_ring_resize_multiple_bh(rings, n, - dev->tx_queue_len, GFP_KERNEL, + new_len, GFP_KERNEL, __skb_array_destroy_skb); if (!ret) - tap->ring_size = dev->tx_queue_len; + tap->ring_size = new_len; kfree(rings); unlock: diff --git a/drivers/net/tun.c b/drivers/net/tun.c index a644fab4321f..a4d419cfbf16 100644 --- a/drivers/net/tun.c +++ b/drivers/net/tun.c @@ -2544,6 +2544,40 @@ static int tun_fill_info(struct sk_buff *skb, const struct net_device *dev) return -EMSGSIZE; } +static int tun_queue_resize(struct net_device *dev, unsigned int new_len) +{ + struct tun_struct *tun = netdev_priv(dev); + struct tun_file *tfile; + struct ptr_ring **rings; + int n = tun->numqueues + tun->numdisabled; + int ret, i; + + rings = kmalloc_objs(*rings, n); + if (!rings) + return -ENOMEM; + + for (i = 0; i < tun->numqueues; i++) { + tfile = rtnl_dereference(tun->tfiles[i]); + rings[i] = &tfile->tx_ring; + } + list_for_each_entry(tfile, &tun->disabled, next) + rings[i++] = &tfile->tx_ring; + + ret = ptr_ring_resize_multiple_bh(rings, n, + new_len, GFP_KERNEL, + tun_ptr_free); + + if (!ret) { + for (i = 0; i < tun->numqueues; i++) { + tfile = rtnl_dereference(tun->tfiles[i]); + tun_force_wake_queue(tun, tfile); + } + } + + kfree(rings); + return ret; +} + static struct rtnl_link_ops tun_link_ops __read_mostly = { .kind = DRV_NAME, .priv_size = sizeof(struct tun_struct), @@ -2551,6 +2585,7 @@ static struct rtnl_link_ops tun_link_ops __read_mostly = { .validate = tun_validate, .get_size = tun_get_size, .fill_info = tun_fill_info, + .change_tx_queue_len = tun_queue_resize, }; static void tun_sock_write_space(struct sock *sk) @@ -3844,40 +3879,6 @@ static const struct ethtool_ops tun_ethtool_ops = { .set_link_ksettings = tun_set_link_ksettings, }; -static int tun_queue_resize(struct tun_struct *tun) -{ - struct net_device *dev = tun->dev; - struct tun_file *tfile; - struct ptr_ring **rings; - int n = tun->numqueues + tun->numdisabled; - int ret, i; - - rings = kmalloc_objs(*rings, n); - if (!rings) - return -ENOMEM; - - for (i = 0; i < tun->numqueues; i++) { - tfile = rtnl_dereference(tun->tfiles[i]); - rings[i] = &tfile->tx_ring; - } - list_for_each_entry(tfile, &tun->disabled, next) - rings[i++] = &tfile->tx_ring; - - ret = ptr_ring_resize_multiple_bh(rings, n, - dev->tx_queue_len, GFP_KERNEL, - tun_ptr_free); - - if (!ret) { - for (i = 0; i < tun->numqueues; i++) { - tfile = rtnl_dereference(tun->tfiles[i]); - tun_force_wake_queue(tun, tfile); - } - } - - kfree(rings); - return ret; -} - static int tun_device_event(struct notifier_block *unused, unsigned long event, void *ptr) { @@ -3889,10 +3890,6 @@ static int tun_device_event(struct notifier_block *unused, return NOTIFY_DONE; switch (event) { - case NETDEV_CHANGE_TX_QUEUE_LEN: - if (tun_queue_resize(tun)) - return NOTIFY_BAD; - break; case NETDEV_UP: for (i = 0; i < tun->numqueues; i++) { struct tun_file *tfile; diff --git a/include/linux/if_tap.h b/include/linux/if_tap.h index f2507fd41b9c..5b8d59e19c1c 100644 --- a/include/linux/if_tap.h +++ b/include/linux/if_tap.h @@ -48,7 +48,7 @@ struct tap_dev { int numqueues; /* Size of the ptr_ring of all queues, as last set by * tap_queue_resize(). Can differ from dev->tx_queue_len - * if a change of the latter failed after our notifier ran. + * if a change of the latter failed after tap_queue_resize() ran. */ unsigned int ring_size; netdev_features_t tap_features; @@ -89,7 +89,7 @@ rx_handler_result_t tap_handle_frame(struct sk_buff **pskb); void tap_del_queues(struct tap_dev *tap); int tap_get_minor(dev_t major, struct tap_dev *tap); void tap_free_minor(dev_t major, struct tap_dev *tap); -int tap_queue_resize(struct tap_dev *tap); +int tap_queue_resize(struct tap_dev *tap, unsigned int new_len); int tap_create_cdev(struct cdev *tap_cdev, dev_t *tap_major, const char *device_name, struct module *module); void tap_destroy_cdev(dev_t major, struct cdev *tap_cdev); diff --git a/include/net/rtnetlink.h b/include/net/rtnetlink.h index 2bff41aacc98..1a4e001df7a9 100644 --- a/include/net/rtnetlink.h +++ b/include/net/rtnetlink.h @@ -132,6 +132,9 @@ static inline struct net *rtnl_newlink_peer_net(struct rtnl_newlink_params *p) * to create when creating a new device. * @get_num_rx_queues: Function to determine number of receive queues * to create when creating a new device. + * @change_tx_queue_len: Function to resize device rings to @new_len + * when dev->tx_queue_len changes. Called under + * RTNL, may sleep. Must change all of them or none. * @get_link_net: Function to get the i/o netns of the device * @get_linkxstats_size: Function to calculate the required room for * dumping device-specific extended link stats @@ -178,6 +181,8 @@ struct rtnl_link_ops { const struct net_device *dev); unsigned int (*get_num_tx_queues)(void); unsigned int (*get_num_rx_queues)(void); + int (*change_tx_queue_len)(struct net_device *dev, + unsigned int new_len); unsigned int slave_maxtype; const struct nla_policy *slave_policy; diff --git a/net/core/dev.c b/net/core/dev.c index b7757f91fdf9..9a9bfc4815e6 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -10261,6 +10261,7 @@ EXPORT_SYMBOL(netif_set_mtu); int netif_change_tx_queue_len(struct net_device *dev, unsigned long new_len) { + const struct rtnl_link_ops *ops = dev->rtnl_link_ops; unsigned int orig_len = dev->tx_queue_len; int res; @@ -10269,6 +10270,11 @@ int netif_change_tx_queue_len(struct net_device *dev, unsigned long new_len) if (new_len != orig_len) { WRITE_ONCE(dev->tx_queue_len, new_len); + if (ops && ops->change_tx_queue_len) { + res = ops->change_tx_queue_len(dev, new_len); + if (res) + goto err_rollback; + } res = call_netdevice_notifiers(NETDEV_CHANGE_TX_QUEUE_LEN, dev); res = notifier_to_errno(res); if (res) -- 2.54.0