From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=103.168.172.153 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795991899; b=1+mqV1y+xyDPGk/1GUd0bXR2Sq/Y2LJEC/WchpZfZp97S9OLQfmDvBYgX3KIWYZu73Is 4hoyB11Z1QoFfqkSNWkkUjMbVnANLubFVmSQaBvv/G+4IwARNdUpB8nzmMeNPA3j0lu8o 1KQwE+xZ7PCilOgGst6REZnJmZAt/iGQdPWhWBvyhnZUPpzRAc3nyUbN8XlWYthjpcmFF hHVdZWErfVw0HY47lR84SjDnQn+1pUX2IwsoJ7MdHLWvSvyiJ1WPVtTu6f1jh/gp3xUCQ bVGyuQJ5MqPJ3onyYqEwTuFqxjYiBHZvVJqdAVhxKhojNhCOEyl9wHmcTIOdcbLxNGw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795991899; 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:In-Reply-To: References:MIME-Version:Content-Transfer-Encoding; bh=qDuVAdbF3m6lAdcEpQQDMa4LF4+jNnZPAlPT5mOEs3E=; b=cTvNpIo7Hv+/gVrtZA/PC+/xyuTUQYYbXBAY2x7Ue42x9/k0l6CKi+PYodUvi5tMkYMc FEC3cB5wqeQseQn7j4+tPpqv2f3vL5/3m3FqelZbczGyf+vU1p1ZD8MWwp5yeuMFwMeqe KXVHsIwm63LiqnHE2l4usmQlZAaEFZnl0BqjdALBsD/OHDXNQVGSBkOKUIIciQNj/ho4q behydS3hUhhQPghve23dHq1NxCA3v4im5y/r64of3owuRhzaa7jz6baOjVUitGFcAvAUn vtBXF9HXupAadq0tHHn7el7QYJk4T+PcUmozY2WlA12YklVHdaqlaiNCRdcewa8S4tw== 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=bl+R+9ou; dkim=pass header.d=messagingengine.com header.i=@messagingengine.com header.a=rsa-sha256 header.s=fm3 header.b=sEz2w9Ak; arc=none smtp.remote-ip=103.168.172.153 Received: from fhigh-a2-smtp.messagingengine.com (fhigh-a2-smtp.messagingengine.com [103.168.172.153]) (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 EF15E606A334 for ; Sun, 29 Nov 2026 22:38:18 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org EF15E606A334 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=bl+R+9ou; dkim=pass (2048-bit key, unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.a=rsa-sha256 header.s=fm3 header.b=sEz2w9Ak Received: from phl-compute-10.internal (phl-compute-10.internal [10.202.2.50]) by mailfhigh.phl.internal (Postfix) with ESMTP id 3B65D1400229 for ; Sun, 29 Nov 2026 17:38:18 -0500 (EST) Received: from phl-frontend-04 ([10.202.2.163]) by phl-compute-10.internal (MEProxy); Sun, 29 Nov 2026 17:38:18 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cbannister.uk; h=cc:cc:content-transfer-encoding:content-type:date:date:from :from:in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to; s=fm3; t=1795991898; x= 1796078298; bh=qDuVAdbF3m6lAdcEpQQDMa4LF4+jNnZPAlPT5mOEs3E=; b=b l+R+9ouaXjmjHe5oA+kL6RzfaW2lSK5GClYx8c6B4d2xKpITgG5a6W1rYmLtEvTd ynhA8ZNxuXnmm6zy1V1DCcDUNNjknjCTyAWfSY6W4h5qGXwXfnAtxnkB/X6u2UOn hpMmWBPyJMuOfRiMiATeMO23efadTOsURI/IBESLCRt332kfYCiR0ZniCykekvE8 bUibokYNzGlOhN/HBwMKBnecdbkmuDSCiv6PU9jZWpzVfZuP1rEF/TA04jyGqI2I RcrMJYoNB1diVYB0Dq45vDXXrPd3sf36ECIpKu5AjzHlcEYK3f82/hxlkXBDF5rE NX58OI2M/OZEr9CgFmCtw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; t=1795991898; x=1796078298; bh=q DuVAdbF3m6lAdcEpQQDMa4LF4+jNnZPAlPT5mOEs3E=; b=sEz2w9Ak21f/I7yjD PrO9gAQF0Uu27dUM6wmV4jNn75g/Ms3knZ7tA+CH+G1k0a+fka4ery0c58NGVltE Sw8CjLcbNb1rWT+5cxiCmjvgVOcD8BtDcUQr8999WgqJrEQkMRZM+bf98Z8C80ZU Dv2Ng1Ye4sbX9t0O2Hvnytj1w/Z9q+ZisTgdzmvINV7hk4k0oA/uNb4RQyttd0Lv e/nqS2y4Tzo8X3EhOA3cwhU4wqBD8KRGxhuZqKVmBexZv2/+Wukhz8Er9PextU36 ydUEPzGfCEsC+lCH4CWXMzEt46gk09S7kIMdj6zdJRfqmN23gRlOpv8hl1Hi+UBD O2sRw== 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=1795991898; d=cbannister.uk; mf=PGtlcm5lbEBjYmFubmlzdGVyLnVrPg==; rt=PGxpbnV4LWtlcm5lbEB2Z2VyLmtlcm5lbC5vcmc+; s=fm3:rsa-sha256:gPnh8FydJ9Sy8teQn1lpE+S86z/wcXPt2b9Qajmkuh7aX6x 7znZnIAzK4hKuVN0M2oryEUKI/2uvf7Gld8eQEwN3t6bHhOy8AYR9Nydn0NOzw9W CNG/iH2kjmV1Apo5K6+NY6UfeYLsPgfb//ZWc08xiDdRtl4pxDsHBUjNKqhluQpr um7qcAIEPNCLbiEa/2Fa22ugZtnz/hrCSJWFdzUW5BHmJiODa2RO0UhWfvw0ivZP 6duj2Lqgc0ySwhOD5XHJaj47s1WtlTn9OoNI/jh7/28w9UVb/GtSV2iQ590vlK/3 IAi577xEm6IGjDJPE8pSoHDTVECYofxrlBkKdEQ==; 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=11; hn=cc,content-transfer-encoding,date,feedback-id,from, in-reply-to,message-id,mime-version,references,subject,to; Message-Instance: m=1; h=sha256:1U0FQZQH/cXBEZwoP4b3mXZgyFogSkbHdflrlCyjP8E=:VdvLpYm6LC9MsmR0/RBu72+kI2pkIxn0E1eZ6OHUbyM=; X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: dmFkZTGFDTGt8nbI6iVm57N6PAIh1slMh0EkB5ZiFDJOGarvPKBWisXSq50G3ccQRfNJHU hqqsH11casMMYnGiNBrXt2ait8du5N/iNjAonxHGmdhcBOu3NUqKLW/n4UVAlsWfCMMZuD UZEn+OYV1DnpX8An8mJaWcXG/it/ChyhO87sLJqdMtV+mdnVDCXZCHiRaBEu45tEbkXUEJ vSX36cJySvzayqmVL+BwB391ZdMQLorR/edmxJL+0B4y1ZJgXz4Qb04kEohISBjcb551Mm jTsxXoBUJ+QvpBaAOHqw95fNfTnusKElZpdFzkKPymlBS0FUkMqLGHACzxYJEIn0VjN4np eJygAaa1Q61H9VrZHX+fucK0zTzIXT5SRzgTsy6s+EfPXxpzXSXBh3NzRdO0ZX05HL58Jy MARE2PZabTPNALGwaDJF/HvqUANWVTWROJyKk3vMv7hjVw8Gi2q4Q6cGB98R2rs9V5srn1 1Fw5MBJyKx5+UuZpm4NfhPYIMtWv6iXaUTZ0nhgyyatWHNYCoKNlI5ehY/X0ne6e95ugMg 0IRfPKG+Pc2fo10pK3CVYrN+nGacucRKcLdgLj+auZeSQxsOCWwrYd80+ZNpQg56B9dFLi Nz7jxJ7S8c/uqNJb5RDYyQscpwwNGFJBGVlx7nvKxEY+/psl3PHBAZIJdfaQ X-ME-Proxy: Feedback-ID: ic4a04a9c:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sun, 29 Nov 2026 17:38:16 -0500 (EST) From: Chris Bannister To: Eric Dumazet , Jakub Kicinski , Paolo Abeni , "David S. Miller" , netdev@vger.kernel.org Cc: Willem de Bruijn , Simon Horman , Kuniyuki Iwashima , Shuah Khan , linux-kselftest@vger.kernel.org, regressions@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH net 3/3] selftests: net: test that RTM_GETLINK answers from the target netns Date: Sun, 29 Nov 2026 22:36:28 +0000 Message-ID: <20261129223628.2167924-4-kernel@cbannister.uk> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261129223628.2167924-1-kernel@cbannister.uk> References: <20261129223628.2167924-1-kernel@cbannister.uk> 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-Transfer-Encoding: 8bit Add a test for a property that RTM_GETLINK replies built under RTNL lost in commit e896e5c0734b ("rtnetlink: do not acquire RTNL in rtnl_getlink() with RTEXT_FILTER_NAME_ONLY"): a request by ifindex is not answered with a device that has left the netns. The test does not need to win a race. It holds RTNL, queues the netns move and the request behind it in that order, and goes on only when it has seen each of them wait for the lock, so a kernel that looks the device up before taking RTNL fails on every run. A SIOCGIFCONF ioctl holds RTNL for the test: dev_ifconf() copies to the user buffer under RTNL, and the buffer is a userfaultfd page, so the ioctl sleeps there until the test resolves the fault. The test then sends the RTM_SETLINK that moves a dummy device to another netns under another ifindex, waits until that thread sleeps uninterruptibly, sends the RTM_GETLINK and waits for it in the same way. Both are then on the wait list of the RTNL mutex in this order, and the request has done any lookup it does before taking RTNL. Once the fault is resolved the device moves first, and the request must fail with -ENODEV. With a lookup before RTNL it succeeds, and carries the ifindex the device has in the other netns. The order relies on the mutex waking its waiters in the order in which they slept, and on RTNL being the only place where the two requests sleep uninterruptibly. If the request is nevertheless answered with the device as it was before the move, the test is skipped. The test needs CONFIG_USERFAULTFD, which is added to the config, and is skipped without it. Testing: on vanilla kernel.org builds in a VM with CONFIG_USERFAULTFD=y, the test failed in each of 20 runs on that commit and on v7.4-rc4, and passed in each of 20 runs on its parent and on v7.4-rc4 with 1/3 and 2/3 applied. None of the 80 runs was skipped. AI assistance: this test and this commit message were produced by AI tools directed by the submitter: Claude Opus 5.5, Claude Fable 5.2 and Claude Sonnet 5.5 (Anthropic), and OpenAI gpt-6.2-sol and gpt-6.2-astra via Codex. Claude Fable 5.2 designed the test. The submitter read the test and follows its method, and has not run it. Signed-off-by: Chris Bannister --- tools/testing/selftests/net/.gitignore | 1 + tools/testing/selftests/net/Makefile | 2 + tools/testing/selftests/net/config | 1 + tools/testing/selftests/net/rtnl_getlink.c | 356 +++++++++++++++++++++ 4 files changed, 360 insertions(+) create mode 100644 tools/testing/selftests/net/rtnl_getlink.c diff --git a/tools/testing/selftests/net/.gitignore b/tools/testing/selftests/net/.gitignore index 27761239d069..78dc4b9c9c9a 100644 --- a/tools/testing/selftests/net/.gitignore +++ b/tools/testing/selftests/net/.gitignore @@ -33,6 +33,7 @@ reuseport_bpf reuseport_bpf_cpu reuseport_bpf_numa reuseport_dualstack +rtnl_getlink rxtimestamp sctp_hello sctp_ifwdtsn diff --git a/tools/testing/selftests/net/Makefile b/tools/testing/selftests/net/Makefile index baea3e7f0f8f..ea5cea74c649 100644 --- a/tools/testing/selftests/net/Makefile +++ b/tools/testing/selftests/net/Makefile @@ -198,6 +198,7 @@ TEST_GEN_PROGS := \ reuseport_bpf_cpu \ reuseport_bpf_numa \ reuseport_dualstack \ + rtnl_getlink \ sk_bind_sendto_listen \ sk_connect_zero_addr \ sk_so_peek_off \ @@ -245,6 +246,7 @@ include ynl.mk $(OUTPUT)/epoll_busy_poll: LDLIBS += -lcap $(OUTPUT)/reuseport_bpf_numa: LDLIBS += -lnuma +$(OUTPUT)/rtnl_getlink: LDLIBS += -lpthread $(OUTPUT)/tcp_mmap: LDLIBS += -lpthread -lcrypto $(OUTPUT)/tcp_inq: LDLIBS += -lpthread $(OUTPUT)/bind_bhash: LDLIBS += -lpthread diff --git a/tools/testing/selftests/net/config b/tools/testing/selftests/net/config index 3e7ce41bd64d..a10c6e7220d4 100644 --- a/tools/testing/selftests/net/config +++ b/tools/testing/selftests/net/config @@ -134,6 +134,7 @@ CONFIG_TEST_BLACKHOLE_DEV=m CONFIG_TLS=m CONFIG_TRACEPOINTS=y CONFIG_TUN=y +CONFIG_USERFAULTFD=y CONFIG_USER_NS=y CONFIG_VETH=y CONFIG_VLAN_8021Q=y diff --git a/tools/testing/selftests/net/rtnl_getlink.c b/tools/testing/selftests/net/rtnl_getlink.c new file mode 100644 index 000000000000..03d578870673 --- /dev/null +++ b/tools/testing/selftests/net/rtnl_getlink.c @@ -0,0 +1,356 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * An RTM_GETLINK request without RTEXT_FILTER_NAME_ONLY is answered under + * RTNL, with a device that is in the target netns when the reply is built. + * + * The test does not depend on winning a race. It holds RTNL in a SIOCGIFCONF + * ioctl whose copy_to_user() is stopped by userfaultfd, queues a netns move + * and then the request behind it, and goes on only after it has seen each of + * them wait for the lock. + */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "kselftest_harness.h" + +#define DEV_NAME "getlink0" +#define WAIT_MS 10000 + +struct req { + struct nlmsghdr nlh; + struct ifinfomsg ifm; + char attrs[128]; +}; + +static void req_init(struct req *req, int type, int flags, __u32 seq, + int ifindex) +{ + memset(req, 0, sizeof(*req)); + req->nlh.nlmsg_len = NLMSG_LENGTH(sizeof(req->ifm)); + req->nlh.nlmsg_type = type; + req->nlh.nlmsg_flags = NLM_F_REQUEST | flags; + req->nlh.nlmsg_seq = seq; + req->ifm.ifi_family = AF_UNSPEC; + req->ifm.ifi_index = ifindex; +} + +static struct rtattr *req_put(struct req *req, int type, const void *data, + int len) +{ + struct rtattr *rta; + + rta = (void *)req + NLMSG_ALIGN(req->nlh.nlmsg_len); + rta->rta_type = type; + rta->rta_len = RTA_LENGTH(len); + if (len) + memcpy(RTA_DATA(rta), data, len); + req->nlh.nlmsg_len = NLMSG_ALIGN(req->nlh.nlmsg_len) + + RTA_ALIGN(rta->rta_len); + return rta; +} + +static int rtnl_open(__u32 groups) +{ + struct sockaddr_nl addr = { + .nl_family = AF_NETLINK, + .nl_groups = groups, + }; + int fd; + + fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE); + if (fd < 0) + return -1; + if (bind(fd, (struct sockaddr *)&addr, sizeof(addr))) { + close(fd); + return -1; + } + return fd; +} + +/* Returns 0 or the negative errno carried by the kernel's ack. */ +static int rtnl_ack(int fd) +{ + char buf[4096]; + struct nlmsghdr *nlh = (void *)buf; + int len; + + len = recv(fd, buf, sizeof(buf), 0); + if (len < 0) + return -errno; + if (!NLMSG_OK(nlh, len) || nlh->nlmsg_type != NLMSG_ERROR) + return -EPROTO; + return ((struct nlmsgerr *)NLMSG_DATA(nlh))->error; +} + +static int rtnl_talk(int fd, struct req *req) +{ + req->nlh.nlmsg_flags |= NLM_F_ACK; + if (send(fd, req, req->nlh.nlmsg_len, 0) < 0) + return -errno; + return rtnl_ack(fd); +} + +static int dummy_add(int fd) +{ + struct rtattr *linkinfo; + struct req req; + + req_init(&req, RTM_NEWLINK, NLM_F_CREATE | NLM_F_EXCL, 1, 0); + req_put(&req, IFLA_IFNAME, DEV_NAME, sizeof(DEV_NAME)); + linkinfo = req_put(&req, IFLA_LINKINFO, NULL, 0); + req_put(&req, IFLA_INFO_KIND, "dummy", sizeof("dummy")); + linkinfo->rta_len = (void *)&req + req.nlh.nlmsg_len - (void *)linkinfo; + + return rtnl_talk(fd, &req); +} + +static long now_ms(void) +{ + struct timespec ts; + + clock_gettime(CLOCK_MONOTONIC, &ts); + return ts.tv_sec * 1000 + ts.tv_nsec / 1000000; +} + +static void sleep_ms(int ms) +{ + struct timespec ts = { .tv_nsec = ms * 1000000 }; + + nanosleep(&ts, NULL); +} + +struct sender { + int fd; + struct req req; + pid_t tid; + int err; + char reply[8192]; + int reply_len; +}; + +/* Sends one request, which blocks in the kernel for as long as the request + * is being handled, and reads the answer. + */ +static void *sender_thread(void *arg) +{ + struct sender *s = arg; + + __atomic_store_n(&s->tid, syscall(SYS_gettid), __ATOMIC_RELEASE); + if (send(s->fd, &s->req, s->req.nlh.nlmsg_len, 0) < 0) { + s->err = errno; + return NULL; + } + s->reply_len = recv(s->fd, s->reply, sizeof(s->reply), 0); + if (s->reply_len < 0) + s->err = errno; + return NULL; +} + +static int netns_open(void) +{ + return open("/proc/thread-self/ns/net", O_RDONLY | O_CLOEXEC); +} + +static int lo_up(int fd) +{ + struct ifreq ifr = { .ifr_name = "lo" }; + + if (ioctl(fd, SIOCGIFFLAGS, &ifr)) + return -1; + ifr.ifr_flags |= IFF_UP; + return ioctl(fd, SIOCSIFFLAGS, &ifr); +} + +struct gifconf { + int fd; + struct ifconf ifc; + int ret; +}; + +/* dev_ifconf() copies one ifreq per IPv4 address to ifc_buf under RTNL. */ +static void *gifconf_thread(void *arg) +{ + struct gifconf *g = arg; + + g->ret = ioctl(g->fd, SIOCGIFCONF, &g->ifc); + return NULL; +} + +static char task_state(pid_t tid) +{ + char path[64], buf[512], *p; + int fd, len; + + snprintf(path, sizeof(path), "/proc/self/task/%d/stat", tid); + fd = open(path, O_RDONLY | O_CLOEXEC); + if (fd < 0) + return 0; + len = read(fd, buf, sizeof(buf) - 1); + close(fd); + if (len <= 0) + return 0; + buf[len] = 0; + p = strrchr(buf, ')'); + return p && p[1] == ' ' ? p[2] : 0; +} + +/* Waits until the thread sleeps uninterruptibly, as a task waiting for RTNL + * does once the lock's owner is itself asleep. + */ +static int wait_blocked(struct sender *s) +{ + long start = now_ms(); + pid_t tid; + + while (now_ms() - start < WAIT_MS) { + tid = __atomic_load_n(&s->tid, __ATOMIC_ACQUIRE); + if (tid && task_state(tid) == 'D') + return 0; + sleep_ms(1); + } + return -1; +} + +TEST(reply_from_target_netns) +{ + long page_size = sysconf(_SC_PAGESIZE); + struct uffdio_api api = { .api = UFFD_API }; + struct sender move = {}, get = {}; + struct pollfd pfd = { .events = POLLIN }; + pthread_t gifconf_t, move_t, get_t; + struct uffdio_register reg = {}; + struct uffdio_copy copy = {}; + struct gifconf gifconf = {}; + int ifindex, new_ifindex; + int other_ns, inet; + struct nlmsghdr *nlh; + struct uffd_msg msg; + void *page, *zeroes; + int uffd; + + if (unshare(CLONE_NEWNET)) + SKIP(return, "unshare(CLONE_NEWNET): %s", strerror(errno)); + other_ns = netns_open(); + ASSERT_LE(0, other_ns); + ASSERT_EQ(0, unshare(CLONE_NEWNET)); + + move.fd = rtnl_open(0); + ASSERT_LE(0, move.fd); + get.fd = rtnl_open(0); + ASSERT_LE(0, get.fd); + if (dummy_add(move.fd)) + SKIP(return, "cannot create a dummy device"); + ifindex = if_nametoindex(DEV_NAME); + ASSERT_LT(0, ifindex); + new_ifindex = ifindex + 1000; + + /* SIOCGIFCONF needs an address to report: 127.0.0.1 on lo. */ + inet = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); + ASSERT_LE(0, inet); + ASSERT_EQ(0, lo_up(inet)); + gifconf.fd = inet; + ASSERT_EQ(0, ioctl(inet, SIOCGIFCONF, &gifconf.ifc)); + ASSERT_LE(sizeof(struct ifreq), gifconf.ifc.ifc_len); + + uffd = syscall(SYS_userfaultfd, O_CLOEXEC); + if (uffd < 0) + SKIP(return, "userfaultfd: %s", strerror(errno)); + ASSERT_EQ(0, ioctl(uffd, UFFDIO_API, &api)); + page = mmap(NULL, page_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + ASSERT_NE(MAP_FAILED, page); + zeroes = mmap(NULL, page_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0); + ASSERT_NE(MAP_FAILED, zeroes); + reg.range.start = (unsigned long)page; + reg.range.len = page_size; + reg.mode = UFFDIO_REGISTER_MODE_MISSING; + ASSERT_EQ(0, ioctl(uffd, UFFDIO_REGISTER, ®)); + + /* Hold RTNL: the ioctl faults on the page in copy_to_user(), and + * sleeps there until the fault is resolved. + */ + gifconf.ifc.ifc_len = page_size; + gifconf.ifc.ifc_buf = page; + ASSERT_EQ(0, pthread_create(&gifconf_t, NULL, gifconf_thread, + &gifconf)); + pfd.fd = uffd; + ASSERT_EQ(1, poll(&pfd, 1, WAIT_MS)); + ASSERT_EQ(sizeof(msg), read(uffd, &msg, sizeof(msg))); + ASSERT_EQ(UFFD_EVENT_PAGEFAULT, msg.event); + + /* Queue the move to the other netns, where the device gets another + * ifindex, as the first waiter for RTNL. + */ + req_init(&move.req, RTM_SETLINK, NLM_F_ACK, 2, ifindex); + req_put(&move.req, IFLA_NET_NS_FD, &other_ns, sizeof(other_ns)); + req_put(&move.req, IFLA_NEW_IFINDEX, &new_ifindex, + sizeof(new_ifindex)); + ASSERT_EQ(0, pthread_create(&move_t, NULL, sender_thread, &move)); + ASSERT_EQ(0, wait_blocked(&move)) { + TH_LOG("the move never waited for RTNL"); + } + + /* Queue the request behind it. The device is still in this netns, + * so a lookup done before taking RTNL finds it. + */ + req_init(&get.req, RTM_GETLINK, 0, 3, ifindex); + ASSERT_EQ(0, pthread_create(&get_t, NULL, sender_thread, &get)); + ASSERT_EQ(0, wait_blocked(&get)) { + TH_LOG("the request never waited for RTNL"); + } + + /* Release RTNL. The mutex wakes its waiters in the order in which + * they arrived: the device moves, then the request is answered. + */ + copy.dst = (unsigned long)page; + copy.src = (unsigned long)zeroes; + copy.len = page_size; + ASSERT_EQ(0, ioctl(uffd, UFFDIO_COPY, ©)); + + ASSERT_EQ(0, pthread_join(gifconf_t, NULL)); + ASSERT_EQ(0, pthread_join(move_t, NULL)); + ASSERT_EQ(0, pthread_join(get_t, NULL)); + ASSERT_EQ(0, gifconf.ret); + ASSERT_EQ(0, move.err); + ASSERT_EQ(0, get.err); + + nlh = (void *)move.reply; + ASSERT_TRUE(NLMSG_OK(nlh, move.reply_len)); + ASSERT_EQ(NLMSG_ERROR, nlh->nlmsg_type); + ASSERT_EQ(0, ((struct nlmsgerr *)NLMSG_DATA(nlh))->error); + ASSERT_EQ(0, if_nametoindex(DEV_NAME)); + + nlh = (void *)get.reply; + ASSERT_TRUE(NLMSG_OK(nlh, get.reply_len)); + if (nlh->nlmsg_type == RTM_NEWLINK) { + struct ifinfomsg *ifm = NLMSG_DATA(nlh); + + if (ifm->ifi_index == ifindex) + SKIP(return, "the request was answered before the move"); + ASSERT_EQ(ifindex, ifm->ifi_index) { + TH_LOG("ifindex %d answered with the device in the other netns, ifindex %d", + ifindex, ifm->ifi_index); + } + } + ASSERT_EQ(NLMSG_ERROR, nlh->nlmsg_type); + EXPECT_EQ(-ENODEV, ((struct nlmsgerr *)NLMSG_DATA(nlh))->error); +} + +TEST_HARNESS_MAIN -- 2.56.0