From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=103.168.172.146 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795988748; b=C2zJMvzJ0jnGwRiMumXGlhvQjGjntzUbt1uVZ/NptOAu3xYQUImh9S8/nuQSgMniOqlY CQJm0oUCE2+xqo8JIGqdoDGYGHYBhRWy2M1pxAmur47rjn0vB/x6DVT7cTkNbyDU+KpaW 2r9i5mLVvZMRNk7VZd07wJZEgmBuLwNfbragMeVAjOmN9gFGUKB9GxDCFS+Iq3eVTBis3 a44rIMZ2GZc2IuGzpvzuSLy8Z+yvAAiVqtzok5uauA24S++Dryx8ZuiJUg7Y9s1Rd4tgi UA6+l4BoY0D1H1ohvTTaB7BQxRBpzaRW6CmEbWexnvfuYVmg+qR3DBoZ66IxE2EtYOA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795988748; h=DMARC-Filter:Received:Received:DKIM-Signature:DKIM-Signature: X-DKIM2-Info:DKIM2-Signature:X-DKIM2-Info:Message-Instance: X-ME-Sender:X-ME-Received:X-ME-Proxy-Cause:X-ME-Proxy:Feedback-ID: Received:From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Type:Content-Transfer-Encoding; bh=bHBqpJ5uKtc5CqKW55f919R4F+bR0WHApxOFogEl+34=; b=GxHCwo7yTlP/YdAtG3BJILlq39ETVI4udLAhIAKXpJDrUQkynedMeDVShiBV9lD4fwtU E7nsFhlvef4Fhp3BdLPwcowaJb6fwcUbRfbPu6agRN+3vMY7EkRSWXKsoKCwpeNJFGcxa ACTiu0Bo03bVGn65Ap0h/Kcu4NWCrS1gwYEvOLrRzq64bH/QsrhdMj67XI1aog+Ji2O7p 2N1LQ4dp2tw1KfITQBK77QVJWQyActD3A2iD3/9f95kk7kglwEnCppWmvbsMTvdn9UPH8 ClRL08awzAEF5UYfDXsxe6ebvhw4LzjPEx9AzxRsti+P1hFAbWXChErEbo8rXYzqqHA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=cbannister.uk; spf=pass smtp.mailfrom=cbannister.uk; dkim=pass header.d=cbannister.uk header.i=@cbannister.uk header.a=rsa-sha256 header.s=fm3 header.b=TjEQrGQr; dkim=pass header.d=messagingengine.com header.i=@messagingengine.com header.a=rsa-sha256 header.s=fm3 header.b=SEFVcAVk; arc=none smtp.remote-ip=103.168.172.146 Received: from fout-a3-smtp.messagingengine.com (fout-a3-smtp.messagingengine.com [103.168.172.146]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A288C606A327 for ; Sun, 29 Nov 2026 21:45:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A288C606A327 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=cbannister.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=cbannister.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=cbannister.uk header.i=@cbannister.uk header.a=rsa-sha256 header.s=fm3 header.b=TjEQrGQr; dkim=pass (2048-bit key, unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.a=rsa-sha256 header.s=fm3 header.b=SEFVcAVk Received: from phl-compute-01.internal (phl-compute-01.internal [10.202.2.41]) by mailfout.phl.internal (Postfix) with ESMTP id B99D9EC09E0 for ; Sun, 29 Nov 2026 16:45:46 -0500 (EST) Received: from phl-frontend-04 ([10.202.2.163]) by phl-compute-01.internal (MEProxy); Sun, 29 Nov 2026 16:45:46 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cbannister.uk; h=cc:cc:content-transfer-encoding:content-type:content-type :date:date:from:from:in-reply-to:message-id:mime-version :reply-to:subject:subject:to:to; s=fm3; t=1795988746; x= 1796075146; bh=bHBqpJ5uKtc5CqKW55f919R4F+bR0WHApxOFogEl+34=; b=T jEQrGQr81roZdGkcw0VdbU0sh+km5R8b9QlEM5itj9KTU36O/9lJQ9GV4lm3ocvC nBkPdAntX7ifRWb5u4Z+L43b0030qyIwHwdBPJLR/kKDVUTiRtze49n6DBOWyl1C AO1spNODpq6jM6MHV9TmHsq0bPjy+xoFymWzkAFVrDwGNRPcghSdO8PDN6GLtrKE jvqHuCmEsBZcaTNtv8oWUK01FzXDwM3le7REg0PlwXQ0nZ5XDChg3wncrdPAEFMT o2jmQ5O5vVNS1D5pUwO0maRqs3c1rufrGGyev77RM2nX8W3Tc7czmYtInEBskyAw MhvHZpHcOLMF9eL6nIeSA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:message-id:mime-version:reply-to:subject :subject:to:to:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; t=1795988746; x=1796075146; bh=bHBqpJ5uKtc5CqKW55f919R4F+bR 0WHApxOFogEl+34=; b=SEFVcAVkTrQsAMUetIfh6Zqi7umUALU+yzYrZ7sErD1M xPcXszgVq74/A8drtEPP8zvikSaXEP2FVDdADG/t0ABBQqtpgdN39Cg011f9rPCZ THR6K3z8qLt54NmR9LVH2V2B3+jsN1dIk7YmhW5UhyDZ31P7tmxE05PikIUIpAPq AZjqPpuloP+A/B9OuwZa3cDqBV/L5WSI4nB9ngXfq610YPgctNLz7tFNjEMcVLE4 i+sUiQl9/hfKEcJRpsr/mS5w2tE82K11/CXvqR8RzeaApxzHJk5ZQfISKRPW0j8t aGyu2XF6OrYqVnYDuW4yZyRuNHWDeTSrOzcsXvY2pw== X-DKIM2-Info: draft=ietf-dkim-dkim2-spec-09; repo=github.com/dkim2wg/interop; date=2026-11-03; sw=lmtpprox; action=sign d=cbannister.uk a=rsa-sha256; DKIM2-Signature: i=1; m=1; t=1795988746; d=cbannister.uk; mf=PGtlcm5lbEBjYmFubmlzdGVyLnVrPg==; rt=PG5ldGRldkB2Z2VyLmtlcm5lbC5vcmc+; s=fm3:rsa-sha256:ZkNwBLo59EZCJ9Dw23rzViHzOef83Z4LDccnKiiHKkS9sDA wMeL6ASh9pE/b3Jld00DXQy8UG0Fp7ynP2sGcW0VxNnAtgYNvq8bDAzmf5sVGWDv RZhsKRd7S/wR2pA8ZZDG8JzU9bgDGuPFYu+Hvb3QG2Nz7VwA8woeLexmdjZXxKxX H9uNpqzFHCk1hm2dxsVWkyxTfsIeQvtMdmNRv/gtbu1CnrdulArqK3yYQcD/R/uC rtPwsbCDywi/+7aR+Mg5CknI6IPvU/t9ffVDCnO+YrAhWLyaHYUGSz8/AU8ZNF3H UItliiVGv+2dovkxdcfeylDxZYb3BY4IVKKUMQw==; X-DKIM2-Info: draft=ietf-dkim-dkim2-spec-09; repo=github.com/dkim2wg/interop; date=2026-11-03; sw=lmtpprox; action=mi-m=1; hc=10; hn=cc,content-transfer-encoding,content-type,date,feedback-id, from,message-id,mime-version,subject,to; Message-Instance: m=1; h=sha256:dziD+fIEjcuYqH8LDvtzwX5GBBnazrqlkqf4I8PAyAU=:wMo4Ly5VIkpxC2IpgT/DGLV/eLvLy4ObnhDmMAyGwdQ=; X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: dmFkZTG0GK6ooCbIswyYKWBiMGP0I7y7uN8MOW5P8uXoR1SQohZRip2CY2z3Vj2MI3E4q8 eFcdEqNPM0fB/BowdcX0ozBhQsAvHshHwucwIA8MQCCfM9wVls9V0Xyq8FNZvwmZf4ZdCC WmeN65c3HnZTjouLouSqlcydzMvBagLU/aik2kyeF5JZXtFcr6qpiVIY9YPX96d8PTBiKd 47qujjIYCIZ7TKcbmKe5K0sIsbI6glAvLB6SmUpXCUNd83QQIHmFubvsAkARGk+2tFbXCH hESojjdCzVyJ+EbMRs5AXwPVzk63RmOMAqQYpYUdzNzfuxI+kA5RHwGvKYo+mitxZ7aoEk jlTCoLaSR/UwmoJCIdwE9dp0NqTdjv2Mhe5r83R/j0V7czU5g0AxwFhQFeFP6ND7rD/GgX 5YDxhfBbE5Tu3feRnc+Hc3AVpOz2iMAIPDBKHALSUTzg6Xua5ovLFlrE2ljNxAPkBvIK2A 9N99kgOH5d2bCle7X+J63zPX6eLc2My7ie666r15APL8oJ+z3fHJ7qJ8xVnNEKbazdiP/4 asAWtc7qpkvUthqwLKksf49oDEdo2wBLRnX5KGCTbYjWluo+LD8+y7UBILmi0n+vLsHuOn mTPWFtv6etd2Hss5sc9pIR3McmPc6kajU9yYLbpVLjSdL+sftfYc9j4kKUmw X-ME-Proxy: Feedback-ID: ic4a04a9c:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sun, 29 Nov 2026 16:45:45 -0500 (EST) From: Chris Bannister To: Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , "David S. Miller" , netdev@vger.kernel.org Cc: regressions@lists.linux.dev, linux-kernel@vger.kernel.org, Simon Horman , Kuniyuki Iwashima Subject: [REGRESSION] rtnetlink: RTM_GETLINK reply can be queued after a newer RTM_NEWLINK since v7.2 Date: Sun, 29 Nov 2026 21:45:27 +0000 Message-ID: <20261129214529.4059732-1-kernel@cbannister.uk> X-Mailer: git-send-email 2.56.0 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-Transfer-Encoding: 8bit Hi, I diagnosed a repeating failure of systemd-networkd 262.1 in a personal project's CI to a regression introduced during the 7.2 cycle, where a full RTM_GETLINK reply can arrive out of order, causing systemd-networkd to log "Lost carrier" and stay there even though the kernel's view was that the link had carrier. This was traced to rtnl_getlink(). For a full RTM_GETLINK it used to read the link state and queue the reply with the RTNL lock held. It now queues the reply after releasing the lock, which means a reply can be queued after a notification of a later change to the same link. It bisects to e896e5c0734b ("rtnetlink: do not acquire RTNL in rtnl_getlink() with RTEXT_FILTER_NAME_ONLY") [1], patch 3 of the series merged as aa064a614efc: clean at patch 2 (00888feb6014), reproduces at patch 3. v7.3.6 carries the change, and it is unfixed in mainline as of v7.4-rc4. #regzbot introduced: e896e5c0734b The reproducers, kernel configs, run records and the fix patches are in a bundle [2], and paths below are relative to it. The i.MX8QXP FlexCAN report [3] describes a register access that faults while rtnl_dump_ifinfo() is filling a link dump. The ordering failure here reproduces on a dummy interface: a full rtnl_getlink() reply reaches the socket after a newer notification for the same link. [1] https://lore.kernel.org/all/20260525083542.1565964-4-edumazet@google.com/ [2] https://github.com/zariel/linux-rtnetlink-getlink-regressions [3] https://lore.kernel.org/netdev/20261027065012.191348-1-chunyuzhiqiang@yinhe.ht/ The sections below were drafted by AI tools from the investigation records. I have reviewed them, and the numbers are from the run records in the bundle. What was observed ----------------- This was found while testing a Kubernetes node image. On Fedora guests running 7.3.4-300.fc45 (virtio-net, on a nested or busy KVM host), networkd logs "Link UP" and "Gained carrier", and then "Lost carrier" a few milliseconds later. In CI there were 4 hits in 12,798 link-ups on 7.3.4-300.fc45, and none in 12,521 on 6.18.65. Those counts come from the project's CI records and are not reproduced in the bundle. About 1,000 boots on a bare-metal host showed no hit. The failure was seen with networkd 262.1 on Fedora, and with 255.4 on stock Ubuntu 24.04.5 running a modified kernel (described below). In CI the loss persisted and DHCP never started (console excerpt in /evidence/ci-console/), although the kernel had carrier: networkd later logged "Gained IPv6LL". One hit was frozen in the lab. Its kernel state was correct: NOCARRIER clear, operstate UP, carrier counters 1/1 and linkwatch idle. That guest recovered only after it was saved and restored. Restored guests heal, probably through QEMU's announce (an inference that was not tested), so this hit says nothing about whether the loss would have persisted. No hit from an ordinary boot was captured with tracing on. Which networkd message carried the stale state is therefore inferred from the networkd source, not observed. Architecture and compiler: x86_64, gcc 16.2.0 (from the "Linux version" lines under /evidence/). Kernel configs are under /kernels/configs/. Cause ----- Before e896e5c0734b, the rtnetlink dispatcher held RTNL across the whole handler, so a reply was queued before any later notification, for example one from linkwatch. That commit made RTM_GETLINK RTNL_FLAG_DOIT_UNLOCKED. For a full request the reply is still filled under RTNL, but it is sent after the lock is released (master, v7.4-rc4): unlock: if (need_rtnl) rtnl_unlock(); if (err < 0) { ... } else { err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); } A task delayed between rtnl_unlock() and rtnl_unicast() can be overtaken by linkwatch, which sends a newer RTM_NEWLINK to the same socket first. The older reply is then queued last. linkwatch_sync_dev() runs before the fill, so a stale reply needs netif_carrier_on() to fire after the fill. Its linkwatch work then takes RTNL as getlink drops it. The follow-up f3c496105472 ("rtnetlink: use dev_isalive() in rtnl_getlink()") does not change this ordering. The property that broke: a reply to RTM_GETLINK must not be older than notifications already queued to the same socket. Evidence -------- The evidence for this report comes from vanilla kernel.org builds with taint 0 (small config, x86_64). Every row below, with its host configuration and run IDs, is in /reports/results.md. The Go reproducer, nlrace, subscribes one NETLINK_ROUTE socket to RTNLGRP_LINK and floods RTM_GETLINK on that socket from several threads, while a second socket toggles the carrier of a dummy device. It compares the last message received with a fresh read. Violations out of iterations. Pass 1 used a 12 vCPU host with mitigations on, and pass 2 a 20 vCPU host with mitigations off. Both ran five concurrent guests per kernel, with no build running: series-base 2bcf59eefb9f (pass 1): 0 / 30,000 series-merge aa064a614efc (pass 1): 3 / 30,000 patch 2 00888feb6014 (pass 1): 0 / 30,000 patch 3 e896e5c0734b (pass 1): 4 / 30,000 v7.3.6 (pass 2): 1 / 30,000 v7.4-rc4 (pass 2): 3 / 30,000 v6.18.65 LTS (pass 2): 0 / 30,000 no flooders, every kernel above: 0 / 10,000 nlrace.c, the C reproducer, ran in one guest alone (20 vCPU host, mitigations off), five runs of 60 s with `-f 2`: 00888feb6014: 0 violations in every run e896e5c0734b: 20 to 31 per run v7.4-rc4: 8 to 20 per run v7.4-rc4 plus the fix: 0 in every run The numbers in results.md from the first nlrace.c binary are qualitative only. The fix queues the reply before releasing RTNL. The two v7.4-rc4 rows used the same host, config and binaries. A v7.4-rc4 kernel with the fix, lockdep and KASAN ran the same reproducers with no lock-order, KASAN, RCU or might-sleep report, and taint 0. Test environment ---------------- The guests (L2) ran under QEMU/KVM with virtio-net. Their host (L1) ran Linux 6.18.64 and was itself a KVM guest (cpu=host, nested virtualisation) on an AMD EPYC 7302P (16C/32T) with ECC memory and power-loss-protected SSDs, running Proxmox VE's 7.0.14 kernel (L0). Pass 1 gave L1 12 vCPUs with default mitigations; pass 2 gave it 20 vCPUs with mitigations=off. The bare-metal nlrace.c runs used an Intel Core i5-13600K (20 threads) on a distribution 7.3.5 kernel. Supporting evidence (modified diagnostic kernels) ------------------------------------------------- To check the effect on networkd, a diagnostic kernel widened the window: kernel.org 7.3.4 with the Fedora config and a 50 ms sleep between rtnl_unlock() and rtnl_unicast() in rtnl_getlink(). On a guest with Fedora userspace it gave 4 persistent networkd hits in 20 boots (Gained carrier, Lost carrier, IPv6LL, no lease), against 0 in 20 for the same source without the sleep. These runs were made before test runs were serialised against kernel builds, so a build may have overlapped them. On a stock Ubuntu 24.04.5 guest (networkd 255.4), with a 20 vCPU host, mitigations off and the two kernels booted strictly in alternation, v7.3.6 plus the 50 ms delay gave 17 persistent hits in 100 boots, and v7.3.6 plus the fix series and the same delay gave 0 in 100. Taint was checked on a separate sample with the same kernels and guest: 10 boots of the fixed kernel and 10 of the delayed kernel. It was 0 on every read, early (about 8 s) and at the end, with no kernel warning on the console. Taint could be read only on boots without a hit, because a hit ends the guest before the taint lines print; the delayed kernel had 3 hits in that sample. Distribution kernels, as supporting evidence, with the Go reproducer in nested guests: Fedora 7.3.4-300.fc45: 55 / 30,000 7.3.3: 19 / 5,000 7.2.11: 14 / 5,000 6.18.60 LTS: 0 / 30,000 Reproducer ---------- nlrace.c is a single C file. It is appended at the end of this mail and is also at /nlrace/c/nlrace.c: cc -O2 -Wall -Wextra -pthread -o nlrace nlrace.c unshare -Urn ./nlrace -t 60 -f 2 It exits 1 on a violation, 0 on none and 125 on a setup error. A violation prints, for example, "round N: notification carrier=0, then reply seq=S carrier=1 (stale)". Run it five times; any violation means the kernel is affected. Zero violations in one run proves nothing, because outside a guest the rate can be about one per minute or lower. As supporting examples, a bare-metal distribution 7.3.5 kernel gave 0 in 4.68M rounds in one run and 5 violations in another, and my own four `-t 60 -f 2` runs of an earlier revision gave 1 each. Questions --------- A fix series follows this report. It has three patches: a fix for this bug, a fix for a second bug from the same commit, and a selftest for the second bug. The patches are also in the bundle. A report of the second bug follows separately. - Should the RTEXT_FILTER_NAME_ONLY uAPI comment say that name-only replies are not ordered against notifications, and should that path report IFF_LOWER_UP without a linkwatch sync? I am happy to test other approaches. Disclosure: this investigation was carried out by AI agents under my direction. Claude Opus 5.5 coordinated it, designed the experiments, minimised the reproducer and wrote its review fixes and reader comments; Claude Fable 5.2 designed the offered fixes and tests and reviewed the package; Claude Sonnet 5.5 ran the test host, built the kernels, wrote the first nlrace.c and drafted the sections below the introduction of this mail; OpenAI gpt-6.2-sol and gpt-6.2-astra analysed the memory dump, wrote the first harness and Go reproducer, analysed the source and reviewed the fixes. The reproducers and the offered fix were run on the kernels listed above. My own part: I ran nlrace.c on my development machine and saw the reported failures. I reviewed nlrace.c, where I found races that the agents then fixed, and I read rtnl_getlink() and the fix patches. I searched lore for related bugs, reports and patches, and I reviewed this mail before sending it. Per-agent detail: /reports/provenance.md. Thanks, Chris Bannister ---- 8< ---- nlrace.c ---- 8< ---- // SPDX-License-Identifier: GPL-2.0 /* * nlrace: can the RTM_NEWLINK notification of a carrier change reach a * NETLINK_ROUTE socket before an RTM_GETLINK reply filled before that change? * * Since e896e5c0734b ("rtnetlink: do not acquire RTNL in rtnl_getlink() with * RTEXT_FILTER_NAME_ONLY", v7.2), rtnl_getlink() fills a full reply under * RTNL, calls rtnl_unlock(), and only then calls rtnl_unicast(). A carrier * change that takes RTNL in that window has its notification (linkwatch, * netif_state_change(), rtmsg_ifinfo(), rtnl_notify()) queued on the socket * first, so the older reply arrives last. A client that applies messages in * arrival order then keeps the old carrier. Before that commit, the fill and * the unicast happened under one RTNL hold. * * One socket S joins RTNLGRP_LINK and also sends the GETLINK requests, so * replies and notifications share one receive queue. Threads: * - main, the carrier toggler: each round sets IFLA_CARRIER on the dummy * link nlrace0 to the opposite value, using a socket of its own, keeps * the flooders running until S has the notification of the new value * (flooding stops after a spin limit, and the run stops if the * notification is still missing 5 s later), waits up to 5 s for every * reply, then applies the oracle. * - flooders (-f): the requesters. They send RTM_GETLINK for nlrace0 on S, * with at most about -f requests in flight. A request is counted in flight * before it is sent, and is not sent once flooding has stopped, so the * toggler's wait covers every request of the round. * - receiver, the observer: it reads S and records whether the last * RTM_NEWLINK of the round was a reply or a notification, and its carrier. * * Violation: a round counts as a violation when all of these hold: * - the last message S received was a GETLINK reply; * - that reply carries a carrier different from the value just set; * - S had already received a notification that carries the value just set; * - a fresh GETLINK on another socket confirms that value is current. * * Why a correct kernel cannot produce this: the change and its notification * both run under RTNL. The linkwatch work sends the notification, or * linkwatch_sync_dev() sends it at the start of a later rtnl_getlink(), * before that request fills its reply. So a reply that carries the old value * was filled before the change. Under one RTNL hold it is also queued before * the change, ahead of the notification, so it cannot be the last message. * The confirming read rules out a change after the round. This rests on * three assumptions: * - only messages for nlrace0's ifindex count; the receiver ignores * RTM_NEWLINK for any other link; * - a round ends only after S has the notification of the value just set, * so no round can take an earlier round's queued notification as its own; * if it does not arrive, the run stops instead of starting another round; * - the private network namespace means nothing else sends link * notifications for nlrace0 or changes its carrier. * * A stale reply counts only when it is the last message of its round. A * fresher reply that arrives after it hides it, so the count is a lower * bound. With "-f 0" no reply is ever received and the count is 0 on any * kernel; that run checks only that the toggler and receiver work. * * Build: cc -O2 -Wall -Wextra -pthread -o nlrace nlrace.c * Run: unshare -Urn ./nlrace [-t SECONDS] [-f FLOODERS] * Exit: 0 no violations, 1 violations, 125 the run gave no verdict (usage, * setup or netlink failure, ENOBUFS, or a notification or reply that * did not arrive in time), so "git bisect run" skips that commit. * unshare itself exits 1 when it cannot create the namespaces, so a * bisect script must check unshare separately (see README.md). */ #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define DEV "nlrace0" #define SKIP 125 /* exit status: no verdict; "git bisect run" skips the commit */ static int nflood = 2, seconds = 30, ifindex; static int sub; /* subscriber socket S */ static atomic_int flooding, inflight, stop; static atomic_uint seqno = 1; /* * State of S for the current round, written by the receiver thread under mu. * The toggler also polls saw_notif without the lock. */ static pthread_mutex_t mu = PTHREAD_MUTEX_INITIALIZER; static int last_reply = -1, last_carrier = -1; static atomic_int saw_notif[2]; static unsigned last_seq; static void die(const char *what) { fprintf(stderr, "nlrace: %s: %s\n", what, strerror(errno)); exit(SKIP); } /* Next flooder seq. Never 0, which marks a notification. */ static unsigned next_seq(void) { unsigned s; while ((s = atomic_fetch_add(&seqno, 1)) == 0) ; return s; } static int nlsock(int group) { int fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE); int big = 1 << 24; struct timeval tv = { 2, 0 }; struct sockaddr_nl sa = { .nl_family = AF_NETLINK }; if (fd < 0) die("socket"); setsockopt(fd, SOL_SOCKET, SO_RCVBUFFORCE, &big, sizeof(big)); setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); if (bind(fd, (void *)&sa, sizeof(sa))) die("bind"); if (group && setsockopt(fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &group, sizeof(group))) die("join group"); return fd; } /* Send one rtnetlink request: ifinfomsg for ifindex, then optional attributes. */ static void request(int fd, int type, int flags, unsigned seq, int idx, unsigned ifi_flags, unsigned ifi_change, const void *attrs, int alen) { struct { struct nlmsghdr h; struct ifinfomsg i; char a[128]; } m = {0}; m.h.nlmsg_type = type; m.h.nlmsg_flags = NLM_F_REQUEST | flags; m.h.nlmsg_seq = seq; m.i.ifi_index = idx; m.i.ifi_flags = ifi_flags; m.i.ifi_change = ifi_change; if (alen) memcpy(m.a, attrs, alen); m.h.nlmsg_len = NLMSG_LENGTH(sizeof(m.i)) + alen; if (send(fd, &m, m.h.nlmsg_len, 0) < 0) die("send"); } /* Carrier in an RTM_NEWLINK message, or -1 if it has none. */ static int carrier_of(struct nlmsghdr *h) { struct ifinfomsg *i = NLMSG_DATA(h); struct rtattr *a = IFLA_RTA(i); int len = IFLA_PAYLOAD(h); for (; RTA_OK(a, len); a = RTA_NEXT(a, len)) if (a->rta_type == IFLA_CARRIER) return *(unsigned char *)RTA_DATA(a); return -1; } /* Wait for the ack of a request on fd. */ static void wait_ack(int fd) { char buf[4096]; struct nlmsghdr *h = (void *)buf; if (recv(fd, buf, sizeof(buf), 0) < 0) die("recv ack"); if (h->nlmsg_type != NLMSG_ERROR || ((struct nlmsgerr *)NLMSG_DATA(h))->error) { errno = h->nlmsg_type == NLMSG_ERROR ? -((struct nlmsgerr *)NLMSG_DATA(h))->error : EPROTO; die("netlink ack"); } } static void set_carrier(int fd, int on) { struct { struct rtattr a; unsigned char v; char pad[3]; } c = { { RTA_LENGTH(1), IFLA_CARRIER }, on, {0} }; request(fd, RTM_NEWLINK, NLM_F_ACK, 0, ifindex, 0, 0, &c, sizeof(c)); wait_ack(fd); } /* The current carrier, read by a GETLINK on a socket that has no other traffic. */ static int true_carrier(int fd) { char buf[8192]; struct nlmsghdr *h = (void *)buf; request(fd, RTM_GETLINK, 0, 1, ifindex, 0, 0, NULL, 0); if (recv(fd, buf, sizeof(buf), 0) < 0) die("recv truth"); return h->nlmsg_type == RTM_NEWLINK ? carrier_of(h) : -1; } /* * Observer: reads S and remembers the kind and carrier of the last RTM_NEWLINK * for nlrace0 of the round. A reply, or an NLMSG_ERROR for a flooder request, * also completes one in-flight request. */ static void *receiver(void *arg) { static char buf[1 << 16]; (void)arg; while (!atomic_load(&stop)) { int n = recv(sub, buf, sizeof(buf), 0); if (n < 0) { if (errno == EAGAIN || errno == EINTR) continue; die("recv subscriber"); } pthread_mutex_lock(&mu); for (struct nlmsghdr *h = (void *)buf; NLMSG_OK(h, n); h = NLMSG_NEXT(h, n)) { int c; if (h->nlmsg_type == NLMSG_ERROR && h->nlmsg_seq != 0) { fprintf(stderr, "nlrace: GETLINK seq=%u failed: %s\n", h->nlmsg_seq, strerror(-((struct nlmsgerr *)NLMSG_DATA(h))->error)); atomic_fetch_sub(&inflight, 1); continue; } if (h->nlmsg_type != RTM_NEWLINK || ((struct ifinfomsg *)NLMSG_DATA(h))->ifi_index != ifindex || (c = carrier_of(h)) < 0) continue; /* Notifications carry seq 0; flooder requests start at 1. */ last_reply = h->nlmsg_seq != 0; last_seq = h->nlmsg_seq; last_carrier = c; if (!last_reply) atomic_store(&saw_notif[c & 1], 1); else atomic_fetch_sub(&inflight, 1); } pthread_mutex_unlock(&mu); } return NULL; } /* * Requester: while flooding is set, sends GETLINK by ifindex on S whenever * fewer than nflood requests are in flight. The check and the increment are * not atomic together, so a few more can be in flight. flooding is read again * after the increment: the toggler clears flooding before it reads inflight, * so either it waits for this request or the request is not sent. */ static void *flooder(void *arg) { (void)arg; while (!atomic_load(&stop)) { if (!atomic_load(&flooding) || atomic_load(&inflight) >= nflood) { sched_yield(); continue; } atomic_fetch_add(&inflight, 1); if (!atomic_load(&flooding)) { atomic_fetch_sub(&inflight, 1); continue; } request(sub, RTM_GETLINK, 0, next_seq(), ifindex, 0, 0, NULL, 0); } return NULL; } static double since(struct timespec *t) { struct timespec now; clock_gettime(CLOCK_MONOTONIC, &now); return (now.tv_sec - t->tv_sec) * 1e3 + (now.tv_nsec - t->tv_nsec) / 1e6; } int main(int argc, char **argv) { int opt, ctl, tru, rounds = 0, bad = 0; struct timespec start; pthread_t rx, *fl; struct utsname u; while ((opt = getopt(argc, argv, "t:f:")) != -1) { if (opt == 't') seconds = atoi(optarg); else if (opt == 'f') nflood = atoi(optarg); else { fprintf(stderr, "usage: %s [-t SECONDS] [-f FLOODERS]\n", argv[0]); return SKIP; } } /* Create and raise the dummy link, then find its index. */ ctl = nlsock(0); tru = nlsock(0); { struct { struct rtattr n; char name[8]; struct rtattr li; struct rtattr k; char kind[8]; } a = { { RTA_LENGTH(sizeof(DEV)), IFLA_IFNAME }, DEV, { RTA_LENGTH(RTA_LENGTH(6)), IFLA_LINKINFO | NLA_F_NESTED }, { RTA_LENGTH(6), IFLA_INFO_KIND }, "dummy" }; request(ctl, RTM_NEWLINK, NLM_F_ACK | NLM_F_CREATE | NLM_F_EXCL, 0, 0, 0, 0, &a, sizeof(a)); wait_ack(ctl); } if (!(ifindex = if_nametoindex(DEV))) die("if_nametoindex"); /* Linkwatch sends a carrier notification only for a link that is up. */ request(ctl, RTM_NEWLINK, NLM_F_ACK, 0, ifindex, IFF_UP, IFF_UP, NULL, 0); wait_ack(ctl); sub = nlsock(RTNLGRP_LINK); if (nflood < 0) nflood = 0; if (!(fl = calloc(nflood + 1, sizeof(*fl)))) die("calloc"); if ((errno = pthread_create(&rx, NULL, receiver, NULL))) die("pthread_create"); for (int i = 0; i < nflood; i++) if ((errno = pthread_create(&fl[i], NULL, flooder, NULL))) die("pthread_create"); clock_gettime(CLOCK_MONOTONIC, &start); while (since(&start) < seconds * 1e3) { int suspect, want = rounds & 1, carrier_seen; unsigned seq_seen; struct timespec drain; pthread_mutex_lock(&mu); last_reply = last_carrier = -1; atomic_store(&saw_notif[0], 0); atomic_store(&saw_notif[1], 0); pthread_mutex_unlock(&mu); atomic_store(&flooding, 1); set_carrier(ctl, want); /* * The ack does not wait for the notification: linkwatch sends it * later, or a flooder request sends it through * linkwatch_sync_dev(). Keep flooding until S has it. A round * must not end without it, or a later round could take it as * its own. */ for (int spin = 0; spin < 1000000 && !atomic_load(&saw_notif[want]); spin++) sched_yield(); atomic_store(&flooding, 0); /* * Without flooders linkwatch sends the notification on its own * schedule, which can be about a second later, so wait for it * with a time bound. */ clock_gettime(CLOCK_MONOTONIC, &drain); while (!atomic_load(&saw_notif[want])) { if (since(&drain) > 5e3) { fprintf(stderr, "nlrace: round %d: no notification of carrier=%d after 5 s\n", rounds, want); exit(SKIP); } sched_yield(); } /* Wait for all replies. */ clock_gettime(CLOCK_MONOTONIC, &drain); while (atomic_load(&inflight) > 0) { if (since(&drain) > 5e3) { fprintf(stderr, "nlrace: round %d: %d GETLINK replies missing after 5 s\n", rounds, atomic_load(&inflight)); exit(SKIP); } sched_yield(); } /* The oracle from the header comment. */ pthread_mutex_lock(&mu); suspect = last_reply == 1 && last_carrier != want && atomic_load(&saw_notif[want]); seq_seen = last_seq; carrier_seen = last_carrier; pthread_mutex_unlock(&mu); if (suspect && true_carrier(tru) == want && ++bad <= 5) printf("round %d: notification carrier=%d, then reply seq=%u carrier=%d (stale)\n", rounds, want, seq_seen, carrier_seen); rounds++; } atomic_store(&stop, 1); for (int i = 0; i < nflood; i++) pthread_join(fl[i], NULL); request(ctl, RTM_DELLINK, NLM_F_ACK, 0, ifindex, 0, 0, NULL, 0); wait_ack(ctl); /* The RTM_DELLINK notification on S wakes the receiver, which then sees stop. */ pthread_join(rx, NULL); free(fl); uname(&u); printf("%s: %d rounds, %d violations\n", u.release, rounds, bad); return bad ? 1 : 0; }