From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796568314; b=m+dhZqIy2Wy6FX7CupAQYNosM9TADZ4l2qkM+Q7MUUbf+uOVgShW/nXYqgxeYUfeiULM EhTWyyGQFkSK3PM0qMlq6hhxp+iCdBTHVD3ngDlOHfJDDwSw8oEIFBcVn7rt7lVsceIQ9 /PZmlofqNPtoVrTAg/M3uETwQfhk7fYahus2VuiHIw0MR9RLuHkHT+eeeQbMqCZSlgiw/ zNpFHoSAlCfVMgglc7tL5glStQMD879sZksTiUJ5bdNgFoMNVvjIfviYKRklgXKK3Gc66 in7cVGDIA8TVMlGiocDJ46GStwEMPxjhBWQTOQNebYKJtvQL98n6u/ug6PiaIQE1eZw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796568314; 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=ai2tK9fEeBq31V9lSgX0VAB8rkTwFuo4HUjEvNZ5h04=; b=J0KNv4mN/6HE2QMi6Yl/yrlQChw4qzTxW0yUU0h5qFzw9td2zWVmqEmBcUX2n1IjRUU+ K8Npj6Z1JxZF0K8NVEuG0ygEsuKbNhcAPyZ8DfxaoAcvpgU/G493zQcnp+BdF8lXEIzUg w1R0qleOoGy7u6zYFoDNJqMkXv3VVGriXxmlDqAaiaQnLcPxZONUIsCnotjsfkAO4x6pB gmDtTxxtjqgAXLzAIVU1bKCeonN3QOsSIXtiHcd0uPSUMj/5JSSyffIkfmTuNOuf9t39e vV6nu3l/Mpo/4ShgYDjdj9IxvZFVyyQk8JUNEWE85Z3dedpeaVdJkgBH0BqAs/ATFEQ== 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=b2+lTuOc; arc=none smtp.remote-ip=74.125.82.172 Received: from mail-dy1-f172.google.com (mail-dy1-f172.google.com [74.125.82.172]) (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 93B4F606A31F for ; Sun, 6 Dec 2026 14:45:13 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 93B4F606A31F 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=b2+lTuOc Received: by mail-dy1-f172.google.com with SMTP id 5a478bee46e88-38431138772so3573016eec.1 for ; Sun, 06 Dec 2026 06:45:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796568313; x=1797173113; 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=ai2tK9fEeBq31V9lSgX0VAB8rkTwFuo4HUjEvNZ5h04=; b=b2+lTuOcAnlXMNI/1/RZ+H/BQ70zg7270pljXxGblC6fsSmUutWAVWNnPSGVMUowfL uTM9CLFUD4keCr+V5aEwyadCyfCzyTXZf4VWcpuc3vFsQ1a9LAIdM5CFrN7BfXzJvFSd F4HERzN7JhA4uOGks5wSVhZy0z1FBc5oMThj6/8LWpeYcmmk/MaeKeMsmyQe5j4WlRTA CCYREoSrjA7Cq9fvWfOrax3404/ztkQHx6o8zA7NMkYLZniLngjGP+OTTfEA/Jw9Saph KapFe2yFh96f3yJLHNis6v1RC5HpKd9mkjnToqPHAMzQrva3QrIgLq0TORH5VbNt1UTe BsUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796568313; x=1797173113; 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=ai2tK9fEeBq31V9lSgX0VAB8rkTwFuo4HUjEvNZ5h04=; b=FJOlv6w3yOjVK3+bX810y6lzkEZq+bV+P79RVh5DDk9SYBBqXRGZbOC6sO5DGCwV2B mboFN2413e0WnJcfqj+4NHlcSWPGEEp5vQQ5C7yWqBoEjiIx7yc08yxT+5V3hHzADlCx bauryK+H59hqB22so/ACvZvn0dVJ6jX/rx1aqq05M68ksI8DIYRbibtDe+wuwMLlxYgP 10gW0YPN3jtZe70TWPWx7nq/EazE2bUiWDHZ3Wbr2c633KW3MiONJ7Ooh6JmVueQTDFr VqIY1NkEx+Xb3HO82JSRGOj4qA/qbWXrX7GCes3X2CGGNbox2Ob7eIZz7jSh120BecbI A0eg== X-Forwarded-Encrypted: i=1; AL+hErE7w119QC0dgHKIo7wSIbAs1o46Elq7TI1YF+wowGquyu8DuZwS726P9f6N9n6AvpwwYzdv3d8=@vger.kernel.org X-Gm-Message-State: AB4q+uGNCDuVcbO/4DFjPT1nrfKNxl9gIseRrSdbxdtFsDNnpU/KqzsZ 3dyvHJMJUj+eqFEfwbsNwNhoeHuqM/YnqlJpdL0TXn19yziGcFMadqwl X-Gm-Gg: AZFdTZear43hGiYCh4mkzUtPCikdgku+lPO5dcssCKEXfHiFyIvsW3FL2AoUsNKUeOn LA0tq0zJeV2dPC0tPw3vk9kZcr/UHjeF7nUWGg5A0sAzRvTCjIlhUeE1N22pRCAeTdoifDZ6pgp E7CJuB9m0MwGJsUVhN5xQ0ebAhaDRyXPWX2u5bFx0Um8OiDNL6jHmZ2CzeVfBKwaD9bjuZkFWki IVr1rpfLeIFM92wC9ZFvlHDAO/jThUGSowHdNXW8NYsf5GyJZ/JhJzci5VBRxR826Ls5RJJZYzP 6Am5S6bH+YumBkob9ANCm39Tcqsf3dpWX/JG9nXc/S1THVpJYZwlB8WoM+UUHGyRTUUMEgwsvkz fSofJby8ZbOfAvIfoh8Ruz/kCgWuEVg1Rs3+/IDUhyQWgsFsE0eqla3gOWPv0CY+HTVDEI3Ooiv j/5h26QKIl1b+wJEvTEu31vc7Z0cZcBqvGiRa0/hNjIhTBvTFRoyOtSw7Oc/aC/A5e2ZPla/x81 edjgSxHHsqPpkVJEBxXaC+37guFtkNALL3F9y4zO9cKkwkhyz9tUCaMB+SbOcZaVu3dNUx43pWg BoLAuPRsRATe/TCcU4l+4Rus0XPT7LIsZGHtk+EmL9wmNE166OXy/ARg50BUkKqyIdVit1t+i8T MEjs456spv4YHpmLWmR3v9N4pdnO4atasWKQDXiw7SaD4dOocevO7sR4xpp/C6k6x6CtMm+JY2l LG6979KXUA3mg= X-Received: by 2002:a05:7301:1804:b0:376:203e:7d7b with SMTP id 5a478bee46e88-38752015b25mr8193345eec.2.1796568312499; Sun, 06 Dec 2026 06:45:12 -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.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 06 Dec 2026 06:45:12 -0800 (PST) From: SeungJong Ha Date: Sun, 06 Dec 2026 23:44:46 +0900 Subject: [PATCH RFC net-next 5/5] net: change tx_queue_len only after all allocations succeeded 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="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261206-tc1-txqlen-commit-v1-5-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 netif_change_tx_queue_len() sets the new length, resizes the device rings, notifies and then resizes the qdiscs. If the qdiscs fail, it restores dev->tx_queue_len, but the device rings keep the new length and RTM_NEWLINK has already reported it. Resizing the rings back on the way out does not help, as that would need to allocate again. Do it in this order instead: prepare the qdiscs may fail, leaves nothing behind resize the device rings may fail, changes all or none set the new length swap the qdisc rings in notify Whichever step fails, nothing has been changed yet, so there is nothing to revert. No in-tree listener of NETDEV_CHANGE_TX_QUEUE_LEN fails any more. Its return value is now ignored, as for NETDEV_UP and NETDEV_FEAT_CHANGE. Found with a prototype static analysis tool I am developing. The analysis, the reproducer and the fix were written with Claude Code (Claude Opus 5.5). Signed-off-by: SeungJong Ha --- include/linux/if_tap.h | 2 +- include/net/sch_generic.h | 5 ++++- net/core/dev.c | 22 +++++++++++----------- net/sched/sch_generic.c | 19 ++++--------------- 4 files changed, 20 insertions(+), 28 deletions(-) diff --git a/include/linux/if_tap.h b/include/linux/if_tap.h index 5b8d59e19c1c..2a8b1084122f 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 tap_queue_resize() ran. + * while the latter is being changed. */ unsigned int ring_size; netdev_features_t tap_features; diff --git a/include/net/sch_generic.h b/include/net/sch_generic.h index 8e0e774844c2..e406b32dfab9 100644 --- a/include/net/sch_generic.h +++ b/include/net/sch_generic.h @@ -723,7 +723,10 @@ void qdisc_class_hash_remove(struct Qdisc_class_hash *, void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *); void qdisc_class_hash_destroy(struct Qdisc_class_hash *); -int dev_qdisc_change_tx_queue_len(struct net_device *dev); +int dev_qdisc_prepare_tx_queue_len(struct net_device *dev, + unsigned int new_len); +void dev_qdisc_commit_tx_queue_len(struct net_device *dev); +void dev_qdisc_abort_tx_queue_len(struct net_device *dev); void dev_qdisc_change_real_num_tx(struct net_device *dev, unsigned int new_real_tx); void dev_init_scheduler(struct net_device *dev); diff --git a/net/core/dev.c b/net/core/dev.c index 9a9bfc4815e6..4a1ddafca036 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -10269,26 +10269,26 @@ int netif_change_tx_queue_len(struct net_device *dev, unsigned long new_len) return -ERANGE; if (new_len != orig_len) { - WRITE_ONCE(dev->tx_queue_len, new_len); + /* Nothing may fail once the new length is set. */ + res = dev_qdisc_prepare_tx_queue_len(dev, new_len); + if (res) + goto err; if (ops && ops->change_tx_queue_len) { res = ops->change_tx_queue_len(dev, new_len); if (res) - goto err_rollback; + goto err_abort; } - res = call_netdevice_notifiers(NETDEV_CHANGE_TX_QUEUE_LEN, dev); - res = notifier_to_errno(res); - if (res) - goto err_rollback; - res = dev_qdisc_change_tx_queue_len(dev); - if (res) - goto err_rollback; + WRITE_ONCE(dev->tx_queue_len, new_len); + dev_qdisc_commit_tx_queue_len(dev); + call_netdevice_notifiers(NETDEV_CHANGE_TX_QUEUE_LEN, dev); } return 0; -err_rollback: +err_abort: + dev_qdisc_abort_tx_queue_len(dev); +err: netdev_err(dev, "refused to change device tx_queue_len\n"); - WRITE_ONCE(dev->tx_queue_len, orig_len); return res; } diff --git a/net/sched/sch_generic.c b/net/sched/sch_generic.c index c7eb4ef5411f..b72d535729d0 100644 --- a/net/sched/sch_generic.c +++ b/net/sched/sch_generic.c @@ -1534,7 +1534,7 @@ void mq_change_real_num_tx(struct Qdisc *sch, unsigned int new_real_tx) } EXPORT_SYMBOL(mq_change_real_num_tx); -static void dev_qdisc_abort_tx_queue_len(struct net_device *dev) +void dev_qdisc_abort_tx_queue_len(struct net_device *dev) { struct Qdisc *qdisc; unsigned int i; @@ -1547,8 +1547,8 @@ static void dev_qdisc_abort_tx_queue_len(struct net_device *dev) } /* Allocate everything a tx_queue_len change needs, or nothing. */ -static int dev_qdisc_prepare_tx_queue_len(struct net_device *dev, - unsigned int new_len) +int dev_qdisc_prepare_tx_queue_len(struct net_device *dev, + unsigned int new_len) { struct Qdisc *qdisc; unsigned int i; @@ -1569,7 +1569,7 @@ static int dev_qdisc_prepare_tx_queue_len(struct net_device *dev, return 0; } -static void dev_qdisc_commit_tx_queue_len(struct net_device *dev) +void dev_qdisc_commit_tx_queue_len(struct net_device *dev) { bool up = dev->flags & IFF_UP; struct Qdisc *qdisc; @@ -1588,17 +1588,6 @@ static void dev_qdisc_commit_tx_queue_len(struct net_device *dev) dev_activate(dev); } -int dev_qdisc_change_tx_queue_len(struct net_device *dev) -{ - int ret; - - ret = dev_qdisc_prepare_tx_queue_len(dev, dev->tx_queue_len); - if (!ret) - dev_qdisc_commit_tx_queue_len(dev); - - return ret; -} - static void dev_init_scheduler_queue(struct net_device *dev, struct netdev_queue *dev_queue, void *_qdisc) -- 2.54.0