[PATCH net v3 4/4] selftests: net: add devmem RX and TX netdev unregister tests
From: Mina Almasry
Date: Wed Oct 14 2026 - 12:45:07 EST
Add devmem_bind_rx_unregister_check and devmem_bind_tx_unregister_check
to nl_netdev.py to verify that unregistering a netdevsim device while a
netlink socket holds an active RX or TX devmem binding synchronously
detaches the dma_buf attachment and cleanly closes the netlink socket
without use-after-free or page faults.
Add the dma-buf, udmabuf and devmem options the tests need to
tools/testing/selftests/net/config.
Cc: Tariq Toukan <tariqt@xxxxxxxxxx>
Cc: Kaifeng Wang <kaifengw@xxxxxxxxxx>
Assisted-by: LLM
Signed-off-by: Mina Almasry <almasrymina@xxxxxxxxxx>
Reviewed-by: Dragos Tatulea <dtatulea@xxxxxxxxxx>
---
v3:
- Count attachments of the test's own dma-buf only, and skip instead of
passing when debugfs bufinfo is not readable (Sashiko).
- Close the netlink socket after removing the device, clean up with
defer(), and skip on EOPNOTSUPP from bind-rx/bind-tx (Sashiko).
- Replace the hardcoded UDMABUF_CREATE value with a helper that computes
it for the running architecture (Sashiko, Dragos), and close the memfd
and /dev/udmabuf fds on errors (Sashiko).
- Add the needed options to tools/testing/selftests/net/config (Sashiko).
---
tools/testing/selftests/net/config | 4 +
tools/testing/selftests/net/nl_netdev.py | 125 ++++++++++++++++++++++-
2 files changed, 126 insertions(+), 3 deletions(-)
diff --git a/tools/testing/selftests/net/config b/tools/testing/selftests/net/config
index 737e7e6327b37..1e21f0055a2c0 100644
--- a/tools/testing/selftests/net/config
+++ b/tools/testing/selftests/net/config
@@ -11,6 +11,7 @@ CONFIG_CRYPTO_ARIA=y
CONFIG_CRYPTO_CHACHA20POLY1305=m
CONFIG_CRYPTO_SHA1=y
CONFIG_CRYPTO_SM4_GENERIC=y
+CONFIG_DEBUG_FS=y
CONFIG_DEBUG_INFO_BTF=y
CONFIG_DEBUG_INFO_BTF_MODULES=n
CONFIG_DUMMY=y
@@ -76,6 +77,7 @@ CONFIG_NET_CLS_BPF=m
CONFIG_NET_CLS_FLOWER=m
CONFIG_NET_CLS_MATCHALL=m
CONFIG_NET_CLS_U32=m
+CONFIG_NET_DEVMEM=y
CONFIG_NETDEVSIM=m
CONFIG_NET_DROP_MONITOR=m
CONFIG_NETFILTER=y
@@ -120,6 +122,7 @@ CONFIG_OPENVSWITCH=m
CONFIG_PAGE_POOL_STATS=y
CONFIG_PSAMPLE=m
CONFIG_RPS=y
+CONFIG_SYNC_FILE=y
CONFIG_SYN_COOKIES=y
CONFIG_SYSCTL=y
CONFIG_SYSFS=y
@@ -131,6 +134,7 @@ CONFIG_TEST_BPF=m
CONFIG_TLS=m
CONFIG_TRACEPOINTS=y
CONFIG_TUN=y
+CONFIG_UDMABUF=y
CONFIG_USER_NS=y
CONFIG_VETH=y
CONFIG_VLAN_8021Q=y
diff --git a/tools/testing/selftests/net/nl_netdev.py b/tools/testing/selftests/net/nl_netdev.py
index ceb44c8e1fec5..988878b71358c 100755
--- a/tools/testing/selftests/net/nl_netdev.py
+++ b/tools/testing/selftests/net/nl_netdev.py
@@ -6,10 +6,14 @@ Tests for the netdev netlink family.
"""
import errno
+import fcntl
+import mmap
+import os
+import struct
from os import system
-from lib.py import ksft_run, ksft_exit
+from lib.py import ksft_run, ksft_exit, KsftFailEx, KsftSkipEx
from lib.py import ksft_eq, ksft_ge, ksft_ne, ksft_raises, ksft_busy_wait
-from lib.py import NetdevFamily, NetdevSimDev, NlError, defer, ip
+from lib.py import EthtoolFamily, NetdevFamily, NetdevSimDev, NlError, defer, ip
def empty_check(nf) -> None:
@@ -366,6 +370,119 @@ def page_pool_stats_ifindex_check(nf) -> None:
ksft_eq(cm.exception.nl_msg.extack['bad-attr'], '.info.id')
+def _udmabuf_create_ioctl() -> int:
+ """Return UDMABUF_CREATE, _IOW('u', 0x42, struct udmabuf_create)."""
+ machine = os.uname().machine
+ if machine.startswith(("alpha", "mips", "ppc", "powerpc", "sparc")):
+ ioc_write = 4 << 29
+ elif machine.startswith("parisc"):
+ ioc_write = 2 << 30
+ else:
+ ioc_write = 1 << 30
+ return ioc_write | 24 << 16 | ord("u") << 8 | 0x42
+
+
+def _create_udmabuf(num_pages=64) -> int:
+ """Create a sealed memfd-backed udmabuf fd for devmem tests."""
+ if not os.path.exists("/dev/udmabuf"):
+ raise KsftSkipEx("/dev/udmabuf is not available")
+
+ size = num_pages * mmap.PAGESIZE
+ memfd = os.memfd_create("devmem-ksft", os.MFD_ALLOW_SEALING)
+ try:
+ os.ftruncate(memfd, size)
+ fcntl.fcntl(memfd, fcntl.F_ADD_SEALS, fcntl.F_SEAL_SHRINK)
+ devfd = os.open("/dev/udmabuf", os.O_RDWR)
+ try:
+ req = bytearray(struct.pack("IIQQ", memfd, 0, 0, size))
+ return fcntl.ioctl(devfd, _udmabuf_create_ioctl(), req)
+ finally:
+ os.close(devfd)
+ finally:
+ os.close(memfd)
+
+
+def _dmabuf_attach_count(dmabuf_fd) -> int:
+ """Return the number of devices attached to dmabuf_fd, from debugfs."""
+ path = "/sys/kernel/debug/dma_buf/bufinfo"
+ try:
+ with open(path, "r", encoding="utf-8") as f:
+ lines = f.read().splitlines()
+ except OSError as e:
+ raise KsftSkipEx(f"{path} is not readable: {e}") from e
+
+ ino = os.fstat(dmabuf_fd).st_ino
+ found = False
+ for line in lines:
+ fields = line.split("\t")
+ if not found:
+ found = (len(fields) == 7 and fields[5].isdigit() and
+ int(fields[5]) == ino)
+ elif line.startswith("Total "):
+ return int(line.split()[1])
+ raise KsftFailEx(f"dma-buf {ino} not found in {path}")
+
+
+def _devmem_bind(bind, attrs) -> None:
+ """Run a devmem bind op, skip the test if the device can't do devmem."""
+ try:
+ bind(attrs)
+ except NlError as e:
+ if e.error == errno.EOPNOTSUPP:
+ raise KsftSkipEx(f"devmem binding not supported: {e}") from e
+ raise
+
+
+def devmem_bind_rx_unregister_check(_nf) -> None:
+ """Verify RX devmem bindings detach dma_buf synchronously on netdev unregister."""
+ dmabuf_fd = _create_udmabuf()
+ defer(os.close, dmabuf_fd)
+ nf_priv = NetdevFamily()
+ defer(nf_priv.close)
+ nsimdev = NetdevSimDev(queue_count=2)
+ defer(nsimdev.remove)
+ nsim = nsimdev.nsims[0]
+
+ ip(f"link set dev {nsim.ifname} up")
+ with EthtoolFamily() as ef:
+ ef.rings_set({"header": {"dev-index": nsim.ifindex},
+ "tcp-data-split": "enabled"})
+ _devmem_bind(nf_priv.bind_rx, {
+ "ifindex": nsim.ifindex,
+ "fd": dmabuf_fd,
+ "queues": [{"id": 1, "type": "rx"}],
+ })
+ ksft_ge(_dmabuf_attach_count(dmabuf_fd), 1)
+
+ nsimdev.remove()
+ ksft_eq(_dmabuf_attach_count(dmabuf_fd), 0)
+
+ nf_priv.close()
+
+
+def devmem_bind_tx_unregister_check(_nf) -> None:
+ """Verify TX devmem bindings detach cleanly on netdev unregister without UAF."""
+ dmabuf_fd = _create_udmabuf()
+ defer(os.close, dmabuf_fd)
+ nf_priv = NetdevFamily()
+ defer(nf_priv.close)
+ nsimdev = NetdevSimDev(queue_count=2)
+ defer(nsimdev.remove)
+ nsim = nsimdev.nsims[0]
+
+ ip(f"link set dev {nsim.ifname} up")
+ _devmem_bind(nf_priv.bind_tx, {
+ "ifindex": nsim.ifindex,
+ "fd": dmabuf_fd,
+ })
+ ksft_ge(_dmabuf_attach_count(dmabuf_fd), 1)
+
+ nsimdev.remove()
+ ksft_eq(_dmabuf_attach_count(dmabuf_fd), 0)
+
+ nf_priv.close()
+
+
def main() -> None:
""" Ksft boiler plate main """
nf = NetdevFamily()
@@ -379,7 +496,9 @@ def main() -> None:
page_pool_check,
page_pool_dump_ifindex,
page_pool_ifindex_leak_check,
- page_pool_stats_ifindex_check
+ page_pool_stats_ifindex_check,
+ devmem_bind_rx_unregister_check,
+ devmem_bind_tx_unregister_check
],
args=(nf, ))
ksft_exit()
--
2.56.0.176.g3575e65682-goog