From: Chris Bannister <kernel@cbannister.uk>
To: Eric Dumazet <edumazet@kernel.org>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
"David S. Miller" <davem@davemloft.net>,
netdev@vger.kernel.org
Cc: Willem de Bruijn <willemb@google.com>,
Simon Horman <horms@kernel.org>,
Kuniyuki Iwashima <kuniyu@google.com>,
Shuah Khan <shuah@kernel.org>,
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 [thread overview]
Message-ID: <20261129223628.2167924-4-kernel@cbannister.uk> (raw)
In-Reply-To: <20261129223628.2167924-1-kernel@cbannister.uk>
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 <kernel@cbannister.uk>
---
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 <errno.h>
+#include <fcntl.h>
+#include <poll.h>
+#include <pthread.h>
+#include <sched.h>
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+#include <net/if.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/socket.h>
+#include <sys/syscall.h>
+#include <linux/netlink.h>
+#include <linux/rtnetlink.h>
+#include <linux/userfaultfd.h>
+
+#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
prev parent reply other threads:[~2026-11-29 22:38 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-29 22:36 [PATCH net 0/3] rtnetlink: build full rtnl_getlink() replies in one RTNL section again Chris Bannister
2026-11-29 22:36 ` [PATCH net 1/3] rtnetlink: look the device up under RTNL in full rtnl_getlink() requests Chris Bannister
2026-11-29 22:36 ` [PATCH net 2/3] rtnetlink: queue the rtnl_getlink() reply before releasing RTNL Chris Bannister
2026-11-29 22:36 ` Chris Bannister [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261129223628.2167924-4-kernel@cbannister.uk \
--to=kernel@cbannister.uk \
--cc=davem@davemloft.net \
--cc=edumazet@kernel.org \
--cc=horms@kernel.org \
--cc=kuba@kernel.org \
--cc=kuniyu@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=regressions@lists.linux.dev \
--cc=shuah@kernel.org \
--cc=willemb@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®