All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH RFC net-next 0/2] af_unix: Per-socket datagram queue length
@ 2026-12-02  7:09 ` Ishu Brar
  0 siblings, 0 replies; 8+ messages in thread
From: Ishu Brar via B4 Relay @ 2026-12-02  7:09 UTC (permalink / raw)
  To: Richard Henderson, Matt Turner, Magnus Lindholm,
	Thomas Bogendoerfer, James E.J. Bottomley, Helge Deller,
	David S. Miller, Andreas Larsson, Arnd Bergmann,
	Kuniyuki Iwashima, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Willem de Bruijn, Simon Horman, Shuah Khan
  Cc: linux-alpha, linux-mips, linux-parisc, sparclinux, linux-arch,
	netdev, linux-kernel, linux-kselftest, Christian Brauner,
	Ishu Brar

Datagram queue length of AF_UNIX socket is copied from
net.unix.max_dgram_qlen at creation, can't be changed later. journald's
/dev/log is created by PID 1 before sysctl.d runs, and kept across
journald restarts, so admin's value never reaches it, all syslog(3)
callers share one queue, systemd works around it with global sysctl bump
early in boot [1]. Lennart asked for socket option instead [1], it's
also KF.71 on UAPI group wishlist [2].

Patch 1 adds SO_MAX_DGRAM_QLEN for SOCK_DGRAM sockets, patch 2 adds
selftest. Tested in virtme-ng VM (x86_64, clang, PROVE_LOCKING), 6/6
pass, no lockdep reports, both wakeup cases fail when wake_up call is
removed.

Questions for this RFC:

1. Permission, going above netns sysctl needs CAP_NET_ADMIN in user ns
   owning netns, since sysctl can be set there after commit cec16052d5a7
   ("net: Enable max_dgram_qlen unix sysctl to be configurable by
   non-init user namespaces"). Or init_user_ns, like SO_RCVBUFFORCE?

2. Read-back, no getsockopt(), af_unix has no unix_getsockopt() yet, and
   sock_diag shows sk_max_ack_backlog only for listening sockets. Add
   one of them?

SO_RCVBUF is no alternative, AF_UNIX datagram sockets don't check
receiver's sk_rcvbuf.

AI was used to write selftest and edit this message

[1] https://github.com/systemd/systemd/issues/45144
[2] https://github.com/uapi-group/kernel-features

Signed-off-by: Ishu Brar <patches@brar.si>
---
Ishu Brar (2):
      af_unix: Add SO_MAX_DGRAM_QLEN for per-socket datagram queue length
      selftests: af_unix: Add test for SO_MAX_DGRAM_QLEN

 arch/alpha/include/uapi/asm/socket.h             |   2 +
 arch/mips/include/uapi/asm/socket.h              |   2 +
 arch/parisc/include/uapi/asm/socket.h            |   2 +
 arch/sparc/include/uapi/asm/socket.h             |   2 +
 include/uapi/asm-generic/socket.h                |   2 +
 net/unix/af_unix.c                               |  27 ++-
 tools/testing/selftests/net/af_unix/Makefile     |   1 +
 tools/testing/selftests/net/af_unix/dgram_qlen.c | 243 +++++++++++++++++++++++
 8 files changed, 280 insertions(+), 1 deletion(-)
---
base-commit: 0a837a30e55bbea2afb01cf0b3ec0c1d789a73ad
change-id: 20261202-af-unix-dgram-qlen-1112c811d5cf

Best regards,
--  
Ishu Brar <patches@brar.si>



^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH RFC net-next 0/2] af_unix: Per-socket datagram queue length
@ 2026-12-02  7:09 ` Ishu Brar
  0 siblings, 0 replies; 8+ messages in thread
From: Ishu Brar @ 2026-12-02  7:09 UTC (permalink / raw)
  To: Richard Henderson, Matt Turner, Magnus Lindholm,
	Thomas Bogendoerfer, James E.J. Bottomley, Helge Deller,
	David S. Miller, Andreas Larsson, Arnd Bergmann,
	Kuniyuki Iwashima, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Willem de Bruijn, Simon Horman, Shuah Khan
  Cc: linux-alpha, linux-mips, linux-parisc, sparclinux, linux-arch,
	netdev, linux-kernel, linux-kselftest, Christian Brauner,
	Ishu Brar

Datagram queue length of AF_UNIX socket is copied from
net.unix.max_dgram_qlen at creation, can't be changed later. journald's
/dev/log is created by PID 1 before sysctl.d runs, and kept across
journald restarts, so admin's value never reaches it, all syslog(3)
callers share one queue, systemd works around it with global sysctl bump
early in boot [1]. Lennart asked for socket option instead [1], it's
also KF.71 on UAPI group wishlist [2].

Patch 1 adds SO_MAX_DGRAM_QLEN for SOCK_DGRAM sockets, patch 2 adds
selftest. Tested in virtme-ng VM (x86_64, clang, PROVE_LOCKING), 6/6
pass, no lockdep reports, both wakeup cases fail when wake_up call is
removed.

Questions for this RFC:

1. Permission, going above netns sysctl needs CAP_NET_ADMIN in user ns
   owning netns, since sysctl can be set there after commit cec16052d5a7
   ("net: Enable max_dgram_qlen unix sysctl to be configurable by
   non-init user namespaces"). Or init_user_ns, like SO_RCVBUFFORCE?

2. Read-back, no getsockopt(), af_unix has no unix_getsockopt() yet, and
   sock_diag shows sk_max_ack_backlog only for listening sockets. Add
   one of them?

SO_RCVBUF is no alternative, AF_UNIX datagram sockets don't check
receiver's sk_rcvbuf.

AI was used to write selftest and edit this message

[1] https://github.com/systemd/systemd/issues/45144
[2] https://github.com/uapi-group/kernel-features

Signed-off-by: Ishu Brar <patches@brar.si>
---
Ishu Brar (2):
      af_unix: Add SO_MAX_DGRAM_QLEN for per-socket datagram queue length
      selftests: af_unix: Add test for SO_MAX_DGRAM_QLEN

 arch/alpha/include/uapi/asm/socket.h             |   2 +
 arch/mips/include/uapi/asm/socket.h              |   2 +
 arch/parisc/include/uapi/asm/socket.h            |   2 +
 arch/sparc/include/uapi/asm/socket.h             |   2 +
 include/uapi/asm-generic/socket.h                |   2 +
 net/unix/af_unix.c                               |  27 ++-
 tools/testing/selftests/net/af_unix/Makefile     |   1 +
 tools/testing/selftests/net/af_unix/dgram_qlen.c | 243 +++++++++++++++++++++++
 8 files changed, 280 insertions(+), 1 deletion(-)
---
base-commit: 0a837a30e55bbea2afb01cf0b3ec0c1d789a73ad
change-id: 20261202-af-unix-dgram-qlen-1112c811d5cf

Best regards,
--  
Ishu Brar <patches@brar.si>


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH RFC net-next 1/2] af_unix: Add SO_MAX_DGRAM_QLEN for per-socket datagram queue length
  2026-12-02  7:09 ` Ishu Brar
@ 2026-12-02  7:09   ` Ishu Brar
  -1 siblings, 0 replies; 8+ messages in thread
From: Ishu Brar via B4 Relay @ 2026-12-02  7:09 UTC (permalink / raw)
  To: Richard Henderson, Matt Turner, Magnus Lindholm,
	Thomas Bogendoerfer, James E.J. Bottomley, Helge Deller,
	David S. Miller, Andreas Larsson, Arnd Bergmann,
	Kuniyuki Iwashima, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Willem de Bruijn, Simon Horman, Shuah Khan
  Cc: linux-alpha, linux-mips, linux-parisc, sparclinux, linux-arch,
	netdev, linux-kernel, linux-kselftest, Christian Brauner,
	Ishu Brar

From: Ishu Brar <patches@brar.si>

Datagram queue length of AF_UNIX socket is sk_max_ack_backlog, copied
from net.unix.max_dgram_qlen at creation, can't be changed after,
listen() is refused on datagram sockets.

This hurts long-lived sockets created early in boot, like journald's
/dev/log. PID 1 creates it before sysctl.d is applied, keeps it across
journald restarts, so admin's value never reaches it. All syslog(3)
callers share that queue, when it's full, blocking senders stall,
non-blocking ones drop messages. Only way today is changing sysctl
globally before socket is created, which hits every other socket created
meanwhile too.

SO_RCVBUF doesn't help, AF_UNIX datagram sockets never check receiver's
sk_rcvbuf, so queue length is only limit receiver has.

Add SO_MAX_DGRAM_QLEN to set it per socket:

 - SOCK_DGRAM only, -ENOPROTOOPT otherwise.
 - negative value gives -EINVAL, same as sysctl.
 - going above sysctl needs CAP_NET_ADMIN in user ns owning socket's
   netns, sysctl itself can be set there.
 - same meaning as sysctl, queue is full when it holds more than that
   many datagrams.
 - lowering keeps what's already queued.
 - raising wakes all senders blocked in sendmsg() or waiting in poll(),
   no recvmsg() will do it for them.

sk_max_ack_backlog is now written while senders read it without lock in
unix_recvq_full_lockless(), so annotate both sides.

Idea is KF.71 on UAPI group kernel wishlist.

Link: https://github.com/uapi-group/kernel-features
Link: https://github.com/systemd/systemd/issues/45144
Signed-off-by: Ishu Brar <patches@brar.si>
---
 arch/alpha/include/uapi/asm/socket.h  |  2 ++
 arch/mips/include/uapi/asm/socket.h   |  2 ++
 arch/parisc/include/uapi/asm/socket.h |  2 ++
 arch/sparc/include/uapi/asm/socket.h  |  2 ++
 include/uapi/asm-generic/socket.h     |  2 ++
 net/unix/af_unix.c                    | 27 ++++++++++++++++++++++++++-
 6 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/arch/alpha/include/uapi/asm/socket.h b/arch/alpha/include/uapi/asm/socket.h
index 430ef65e51ea..189acf751a66 100644
--- a/arch/alpha/include/uapi/asm/socket.h
+++ b/arch/alpha/include/uapi/asm/socket.h
@@ -163,6 +163,8 @@
 
 #define SO_PASSCRED_THREAD     88
 
+#define SO_MAX_DGRAM_QLEN     89
+
 #if !defined(__KERNEL__)
 
 #if __BITS_PER_LONG == 64
diff --git a/arch/mips/include/uapi/asm/socket.h b/arch/mips/include/uapi/asm/socket.h
index 7e49b24170d2..93e5a056e6d1 100644
--- a/arch/mips/include/uapi/asm/socket.h
+++ b/arch/mips/include/uapi/asm/socket.h
@@ -174,6 +174,8 @@
 
 #define SO_PASSCRED_THREAD     88
 
+#define SO_MAX_DGRAM_QLEN     89
+
 #if !defined(__KERNEL__)
 
 #if __BITS_PER_LONG == 64
diff --git a/arch/parisc/include/uapi/asm/socket.h b/arch/parisc/include/uapi/asm/socket.h
index e414c2397224..f79087bfbf4f 100644
--- a/arch/parisc/include/uapi/asm/socket.h
+++ b/arch/parisc/include/uapi/asm/socket.h
@@ -155,6 +155,8 @@
 
 #define SO_PASSCRED_THREAD	0x4056
 
+#define SO_MAX_DGRAM_QLEN	0x4057
+
 #if !defined(__KERNEL__)
 
 #if __BITS_PER_LONG == 64
diff --git a/arch/sparc/include/uapi/asm/socket.h b/arch/sparc/include/uapi/asm/socket.h
index 9ca7ec9ba450..fee3b12313b7 100644
--- a/arch/sparc/include/uapi/asm/socket.h
+++ b/arch/sparc/include/uapi/asm/socket.h
@@ -156,6 +156,8 @@
 
 #define SO_PASSCRED_THREAD       0x0061
 
+#define SO_MAX_DGRAM_QLEN       0x0062
+
 #if !defined(__KERNEL__)
 
 
diff --git a/include/uapi/asm-generic/socket.h b/include/uapi/asm-generic/socket.h
index 2e5d9db1c9fa..0b392c252307 100644
--- a/include/uapi/asm-generic/socket.h
+++ b/include/uapi/asm-generic/socket.h
@@ -158,6 +158,8 @@
 
 #define SO_PASSCRED_THREAD	88
 
+#define SO_MAX_DGRAM_QLEN	89
+
 #if !defined(__KERNEL__)
 
 #if __BITS_PER_LONG == 64 || (defined(__x86_64__) && defined(__ILP32__))
diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
index 53a5553bfb66..2afa958c8e2b 100644
--- a/net/unix/af_unix.c
+++ b/net/unix/af_unix.c
@@ -287,7 +287,8 @@ static inline int unix_may_send(struct sock *sk, struct sock *osk)
 
 static inline int unix_recvq_full_lockless(const struct sock *sk)
 {
-	return skb_queue_len_lockless(&sk->sk_receive_queue) > sk->sk_max_ack_backlog;
+	return skb_queue_len_lockless(&sk->sk_receive_queue) >
+	       READ_ONCE(sk->sk_max_ack_backlog);
 }
 
 struct sock *unix_peer_get(struct sock *s)
@@ -945,6 +946,7 @@ static bool unix_custom_sockopt(int optname)
 	switch (optname) {
 	case SO_INQ:
 	case SO_RIGHTS_NOTRUNC:
+	case SO_MAX_DGRAM_QLEN:
 		return true;
 	default:
 		return false;
@@ -988,6 +990,29 @@ static int unix_setsockopt(struct socket *sock, int level, int optname,
 		WRITE_ONCE(u->scm_rights_notrunc, val);
 		break;
 
+	case SO_MAX_DGRAM_QLEN: {
+		struct net *net = sock_net(sk);
+		u32 old;
+
+		if (sk->sk_type != SOCK_DGRAM)
+			return -ENOPROTOOPT;
+
+		if (val < 0)
+			return -EINVAL;
+
+		if (val > READ_ONCE(net->unx.sysctl_max_dgram_qlen) &&
+		    !sockopt_ns_capable(net->user_ns, CAP_NET_ADMIN))
+			return -EPERM;
+
+		old = READ_ONCE(sk->sk_max_ack_backlog);
+		WRITE_ONCE(sk->sk_max_ack_backlog, val);
+
+		/* Raising frees slots without a read: wake all senders. */
+		if (val > old)
+			wake_up_interruptible_all(&u->peer_wait);
+		break;
+	}
+
 	default:
 		return -ENOPROTOOPT;
 	}

-- 
2.56.1



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH RFC net-next 1/2] af_unix: Add SO_MAX_DGRAM_QLEN for per-socket datagram queue length
@ 2026-12-02  7:09   ` Ishu Brar
  0 siblings, 0 replies; 8+ messages in thread
From: Ishu Brar @ 2026-12-02  7:09 UTC (permalink / raw)
  To: Richard Henderson, Matt Turner, Magnus Lindholm,
	Thomas Bogendoerfer, James E.J. Bottomley, Helge Deller,
	David S. Miller, Andreas Larsson, Arnd Bergmann,
	Kuniyuki Iwashima, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Willem de Bruijn, Simon Horman, Shuah Khan
  Cc: linux-alpha, linux-mips, linux-parisc, sparclinux, linux-arch,
	netdev, linux-kernel, linux-kselftest, Christian Brauner,
	Ishu Brar

Datagram queue length of AF_UNIX socket is sk_max_ack_backlog, copied
from net.unix.max_dgram_qlen at creation, can't be changed after,
listen() is refused on datagram sockets.

This hurts long-lived sockets created early in boot, like journald's
/dev/log. PID 1 creates it before sysctl.d is applied, keeps it across
journald restarts, so admin's value never reaches it. All syslog(3)
callers share that queue, when it's full, blocking senders stall,
non-blocking ones drop messages. Only way today is changing sysctl
globally before socket is created, which hits every other socket created
meanwhile too.

SO_RCVBUF doesn't help, AF_UNIX datagram sockets never check receiver's
sk_rcvbuf, so queue length is only limit receiver has.

Add SO_MAX_DGRAM_QLEN to set it per socket:

 - SOCK_DGRAM only, -ENOPROTOOPT otherwise.
 - negative value gives -EINVAL, same as sysctl.
 - going above sysctl needs CAP_NET_ADMIN in user ns owning socket's
   netns, sysctl itself can be set there.
 - same meaning as sysctl, queue is full when it holds more than that
   many datagrams.
 - lowering keeps what's already queued.
 - raising wakes all senders blocked in sendmsg() or waiting in poll(),
   no recvmsg() will do it for them.

sk_max_ack_backlog is now written while senders read it without lock in
unix_recvq_full_lockless(), so annotate both sides.

Idea is KF.71 on UAPI group kernel wishlist.

Link: https://github.com/uapi-group/kernel-features
Link: https://github.com/systemd/systemd/issues/45144
Signed-off-by: Ishu Brar <patches@brar.si>
---
 arch/alpha/include/uapi/asm/socket.h  |  2 ++
 arch/mips/include/uapi/asm/socket.h   |  2 ++
 arch/parisc/include/uapi/asm/socket.h |  2 ++
 arch/sparc/include/uapi/asm/socket.h  |  2 ++
 include/uapi/asm-generic/socket.h     |  2 ++
 net/unix/af_unix.c                    | 27 ++++++++++++++++++++++++++-
 6 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/arch/alpha/include/uapi/asm/socket.h b/arch/alpha/include/uapi/asm/socket.h
index 430ef65e51ea..189acf751a66 100644
--- a/arch/alpha/include/uapi/asm/socket.h
+++ b/arch/alpha/include/uapi/asm/socket.h
@@ -163,6 +163,8 @@
 
 #define SO_PASSCRED_THREAD     88
 
+#define SO_MAX_DGRAM_QLEN     89
+
 #if !defined(__KERNEL__)
 
 #if __BITS_PER_LONG == 64
diff --git a/arch/mips/include/uapi/asm/socket.h b/arch/mips/include/uapi/asm/socket.h
index 7e49b24170d2..93e5a056e6d1 100644
--- a/arch/mips/include/uapi/asm/socket.h
+++ b/arch/mips/include/uapi/asm/socket.h
@@ -174,6 +174,8 @@
 
 #define SO_PASSCRED_THREAD     88
 
+#define SO_MAX_DGRAM_QLEN     89
+
 #if !defined(__KERNEL__)
 
 #if __BITS_PER_LONG == 64
diff --git a/arch/parisc/include/uapi/asm/socket.h b/arch/parisc/include/uapi/asm/socket.h
index e414c2397224..f79087bfbf4f 100644
--- a/arch/parisc/include/uapi/asm/socket.h
+++ b/arch/parisc/include/uapi/asm/socket.h
@@ -155,6 +155,8 @@
 
 #define SO_PASSCRED_THREAD	0x4056
 
+#define SO_MAX_DGRAM_QLEN	0x4057
+
 #if !defined(__KERNEL__)
 
 #if __BITS_PER_LONG == 64
diff --git a/arch/sparc/include/uapi/asm/socket.h b/arch/sparc/include/uapi/asm/socket.h
index 9ca7ec9ba450..fee3b12313b7 100644
--- a/arch/sparc/include/uapi/asm/socket.h
+++ b/arch/sparc/include/uapi/asm/socket.h
@@ -156,6 +156,8 @@
 
 #define SO_PASSCRED_THREAD       0x0061
 
+#define SO_MAX_DGRAM_QLEN       0x0062
+
 #if !defined(__KERNEL__)
 
 
diff --git a/include/uapi/asm-generic/socket.h b/include/uapi/asm-generic/socket.h
index 2e5d9db1c9fa..0b392c252307 100644
--- a/include/uapi/asm-generic/socket.h
+++ b/include/uapi/asm-generic/socket.h
@@ -158,6 +158,8 @@
 
 #define SO_PASSCRED_THREAD	88
 
+#define SO_MAX_DGRAM_QLEN	89
+
 #if !defined(__KERNEL__)
 
 #if __BITS_PER_LONG == 64 || (defined(__x86_64__) && defined(__ILP32__))
diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
index 53a5553bfb66..2afa958c8e2b 100644
--- a/net/unix/af_unix.c
+++ b/net/unix/af_unix.c
@@ -287,7 +287,8 @@ static inline int unix_may_send(struct sock *sk, struct sock *osk)
 
 static inline int unix_recvq_full_lockless(const struct sock *sk)
 {
-	return skb_queue_len_lockless(&sk->sk_receive_queue) > sk->sk_max_ack_backlog;
+	return skb_queue_len_lockless(&sk->sk_receive_queue) >
+	       READ_ONCE(sk->sk_max_ack_backlog);
 }
 
 struct sock *unix_peer_get(struct sock *s)
@@ -945,6 +946,7 @@ static bool unix_custom_sockopt(int optname)
 	switch (optname) {
 	case SO_INQ:
 	case SO_RIGHTS_NOTRUNC:
+	case SO_MAX_DGRAM_QLEN:
 		return true;
 	default:
 		return false;
@@ -988,6 +990,29 @@ static int unix_setsockopt(struct socket *sock, int level, int optname,
 		WRITE_ONCE(u->scm_rights_notrunc, val);
 		break;
 
+	case SO_MAX_DGRAM_QLEN: {
+		struct net *net = sock_net(sk);
+		u32 old;
+
+		if (sk->sk_type != SOCK_DGRAM)
+			return -ENOPROTOOPT;
+
+		if (val < 0)
+			return -EINVAL;
+
+		if (val > READ_ONCE(net->unx.sysctl_max_dgram_qlen) &&
+		    !sockopt_ns_capable(net->user_ns, CAP_NET_ADMIN))
+			return -EPERM;
+
+		old = READ_ONCE(sk->sk_max_ack_backlog);
+		WRITE_ONCE(sk->sk_max_ack_backlog, val);
+
+		/* Raising frees slots without a read: wake all senders. */
+		if (val > old)
+			wake_up_interruptible_all(&u->peer_wait);
+		break;
+	}
+
 	default:
 		return -ENOPROTOOPT;
 	}

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH RFC net-next 2/2] selftests: af_unix: Add test for SO_MAX_DGRAM_QLEN
  2026-12-02  7:09 ` Ishu Brar
@ 2026-12-02  7:09   ` Ishu Brar via B4 Relay
  -1 siblings, 0 replies; 8+ messages in thread
From: Ishu Brar @ 2026-12-02  7:09 UTC (permalink / raw)
  To: Richard Henderson, Matt Turner, Magnus Lindholm,
	Thomas Bogendoerfer, James E.J. Bottomley, Helge Deller,
	David S. Miller, Andreas Larsson, Arnd Bergmann,
	Kuniyuki Iwashima, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Willem de Bruijn, Simon Horman, Shuah Khan
  Cc: linux-alpha, linux-mips, linux-parisc, sparclinux, linux-arch,
	netdev, linux-kernel, linux-kselftest, Christian Brauner,
	Ishu Brar

Check SO_MAX_DGRAM_QLEN rejects bad values and non-datagram sockets,
limit applies, lowering keeps queued datagrams, raising wakes sender
blocked in send() and one waiting in poll() within 1s, and unprivileged
user can't go above sysctl.

Receiver is bound socket, not socketpair(), receiver whose peer is
sender is exempt from queue limit.

Wakeup cases checked to fail with wake_up call removed from kernel side.

Claude wrote these tests

Signed-off-by: Ishu Brar <patches@brar.si>
---
 tools/testing/selftests/net/af_unix/Makefile     |   1 +
 tools/testing/selftests/net/af_unix/dgram_qlen.c | 243 +++++++++++++++++++++++
 2 files changed, 244 insertions(+)

diff --git a/tools/testing/selftests/net/af_unix/Makefile b/tools/testing/selftests/net/af_unix/Makefile
index fb2e09f0f44b..110cc339598b 100644
--- a/tools/testing/selftests/net/af_unix/Makefile
+++ b/tools/testing/selftests/net/af_unix/Makefile
@@ -6,6 +6,7 @@ cc-option = $(call __cc-option, $(CC),,$(1),$(2))
 CFLAGS += $(KHDR_INCLUDES) -Wall $(call cc-option,-Wflex-array-member-not-at-end)
 
 TEST_GEN_PROGS := \
+	dgram_qlen \
 	diag_uid \
 	scm_inq \
 	scm_pidfd \
diff --git a/tools/testing/selftests/net/af_unix/dgram_qlen.c b/tools/testing/selftests/net/af_unix/dgram_qlen.c
new file mode 100644
index 000000000000..7a5f42339731
--- /dev/null
+++ b/tools/testing/selftests/net/af_unix/dgram_qlen.c
@@ -0,0 +1,243 @@
+// SPDX-License-Identifier: GPL-2.0
+/* SO_MAX_DGRAM_QLEN: per-socket datagram queue length for AF_UNIX. */
+
+#define _GNU_SOURCE
+#include <errno.h>
+#include <poll.h>
+#include <stdio.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <sys/un.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+
+#include "kselftest_harness.h"
+
+#ifndef SO_MAX_DGRAM_QLEN
+#define SO_MAX_DGRAM_QLEN	89
+#endif
+
+FIXTURE(dgram_qlen)
+{
+	struct sockaddr_un addr;
+	socklen_t addrlen;
+	int rcv;
+};
+
+FIXTURE_SETUP(dgram_qlen)
+{
+	/* A bound receiver, not a socketpair: a receiver whose peer is the
+	 * sender is exempt from the queue limit in unix_dgram_sendmsg().
+	 */
+	self->addr.sun_family = AF_UNIX;
+	self->addrlen = offsetof(struct sockaddr_un, sun_path) + 1 +
+		snprintf(self->addr.sun_path + 1,
+			 sizeof(self->addr.sun_path) - 1,
+			 "dgram_qlen.%d", getpid());
+
+	self->rcv = socket(AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0);
+	ASSERT_LE(0, self->rcv);
+	ASSERT_EQ(0, bind(self->rcv, (struct sockaddr *)&self->addr,
+			  self->addrlen));
+}
+
+FIXTURE_TEARDOWN(dgram_qlen)
+{
+	close(self->rcv);
+}
+
+static int sender(FIXTURE_DATA(dgram_qlen) *self, int flags)
+{
+	int fd = socket(AF_UNIX, SOCK_DGRAM | flags, 0);
+
+	if (fd < 0)
+		return -1;
+	if (connect(fd, (struct sockaddr *)&self->addr, self->addrlen)) {
+		close(fd);
+		return -1;
+	}
+	return fd;
+}
+
+static int set_qlen(int fd, int val)
+{
+	return setsockopt(fd, SOL_SOCKET, SO_MAX_DGRAM_QLEN, &val, sizeof(val));
+}
+
+/* Send 1-byte datagrams until the queue is full; return how many fit. */
+static int fill(int fd)
+{
+	int n = 0;
+
+	while (send(fd, "x", 1, 0) == 1)
+		n++;
+	return errno == EAGAIN ? n : -errno;
+}
+
+static int drain(int fd)
+{
+	char c;
+	int n = 0;
+
+	while (recv(fd, &c, 1, 0) == 1)
+		n++;
+	return n;
+}
+
+/* Milliseconds since @start. */
+static long since_ms(const struct timespec *start)
+{
+	struct timespec now;
+
+	clock_gettime(CLOCK_MONOTONIC, &now);
+	return (now.tv_sec - start->tv_sec) * 1000 +
+	       (now.tv_nsec - start->tv_nsec) / 1000000;
+}
+
+static int sysctl_qlen(void)
+{
+	FILE *f = fopen("/proc/sys/net/unix/max_dgram_qlen", "r");
+	int val = -1;
+
+	if (f) {
+		if (fscanf(f, "%d", &val) != 1)
+			val = -1;
+		fclose(f);
+	}
+	return val;
+}
+
+TEST_F(dgram_qlen, bad_values)
+{
+	int stream = socket(AF_UNIX, SOCK_STREAM, 0);
+
+	ASSERT_EQ(-1, set_qlen(self->rcv, -1));
+	ASSERT_EQ(EINVAL, errno);
+
+	ASSERT_EQ(-1, set_qlen(stream, 4));
+	ASSERT_EQ(ENOPROTOOPT, errno);
+	close(stream);
+}
+
+/* Same off-by-one as the sysctl: a limit of N admits N + 1 datagrams. */
+TEST_F(dgram_qlen, limit_applies)
+{
+	int fd = sender(self, SOCK_NONBLOCK);
+
+	ASSERT_LE(0, fd);
+	ASSERT_EQ(0, set_qlen(self->rcv, 4));
+	ASSERT_EQ(5, fill(fd));
+	close(fd);
+}
+
+TEST_F(dgram_qlen, lower_keeps_queue)
+{
+	int fd = sender(self, SOCK_NONBLOCK);
+
+	ASSERT_LE(0, fd);
+	ASSERT_EQ(0, set_qlen(self->rcv, 8));
+	ASSERT_EQ(9, fill(fd));
+
+	ASSERT_EQ(0, set_qlen(self->rcv, 2));
+	ASSERT_EQ(0, fill(fd));
+	ASSERT_EQ(9, drain(self->rcv));
+	ASSERT_EQ(3, fill(fd));
+	close(fd);
+}
+
+/* Raising the limit must wake a sender blocked in send(), with no recv(). */
+TEST_F(dgram_qlen, raise_wakes_blocked_sender)
+{
+	struct timeval tv = { .tv_sec = 3 };
+	int fd = sender(self, 0), status;
+	pid_t pid;
+
+	ASSERT_LE(0, fd);
+	ASSERT_EQ(0, set_qlen(self->rcv, 2));
+	ASSERT_EQ(0, setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)));
+	while (send(fd, "x", 1, MSG_DONTWAIT) == 1)
+		;
+
+	pid = fork();
+	ASSERT_LE(0, pid);
+	if (pid == 0) {
+		struct timespec t;
+		int ret;
+
+		/* Without a wakeup, send() sleeps out the 3 s timeout. */
+		clock_gettime(CLOCK_MONOTONIC, &t);
+		ret = send(fd, "x", 1, 0);
+		ksft_print_msg("send() returned %d after %ld ms\n",
+			       ret, since_ms(&t));
+		_exit(ret == 1 && since_ms(&t) < 1000 ? 0 : 1);
+	}
+
+	usleep(200 * 1000);
+	ASSERT_EQ(0, set_qlen(self->rcv, 100));
+	ASSERT_EQ(pid, waitpid(pid, &status, 0));
+	ASSERT_EQ(0, WEXITSTATUS(status));
+	close(fd);
+}
+
+/* Same for a sender waiting in poll() for POLLOUT. */
+TEST_F(dgram_qlen, raise_wakes_poller)
+{
+	int fd = sender(self, SOCK_NONBLOCK), status;
+	struct pollfd pfd = { .events = POLLOUT };
+	pid_t pid;
+
+	ASSERT_LE(0, fd);
+	ASSERT_EQ(0, set_qlen(self->rcv, 2));
+	ASSERT_EQ(3, fill(fd));
+
+	pid = fork();
+	ASSERT_LE(0, pid);
+	if (pid == 0) {
+		struct timespec t;
+		int ret;
+
+		pfd.fd = fd;
+		clock_gettime(CLOCK_MONOTONIC, &t);
+		ret = poll(&pfd, 1, 3000);
+		ksft_print_msg("poll() returned %d revents %#x after %ld ms\n",
+			       ret, pfd.revents, since_ms(&t));
+		_exit(ret == 1 && (pfd.revents & POLLOUT) &&
+		      since_ms(&t) < 1000 ? 0 : 1);
+	}
+
+	usleep(200 * 1000);
+	ASSERT_EQ(0, set_qlen(self->rcv, 100));
+	ASSERT_EQ(pid, waitpid(pid, &status, 0));
+	ASSERT_EQ(0, WEXITSTATUS(status));
+	close(fd);
+}
+
+/* Without CAP_NET_ADMIN, the sysctl value is the ceiling. */
+TEST_F(dgram_qlen, unprivileged_ceiling)
+{
+	int max = sysctl_qlen(), status;
+	pid_t pid;
+
+	if (geteuid())
+		SKIP(return, "needs root to drop to nobody");
+	ASSERT_LE(0, max);
+
+	pid = fork();
+	ASSERT_LE(0, pid);
+	if (pid == 0) {
+		if (setresuid(65534, 65534, 65534))
+			_exit(10);
+		if (set_qlen(self->rcv, max))
+			_exit(11);
+		if (set_qlen(self->rcv, max + 1) != -1 || errno != EPERM)
+			_exit(12);
+		_exit(0);
+	}
+	ASSERT_EQ(pid, waitpid(pid, &status, 0));
+	ASSERT_EQ(0, WEXITSTATUS(status));
+
+	ASSERT_EQ(0, set_qlen(self->rcv, max + 1));	/* root may */
+}
+
+TEST_HARNESS_MAIN

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH RFC net-next 2/2] selftests: af_unix: Add test for SO_MAX_DGRAM_QLEN
@ 2026-12-02  7:09   ` Ishu Brar via B4 Relay
  0 siblings, 0 replies; 8+ messages in thread
From: Ishu Brar via B4 Relay @ 2026-12-02  7:09 UTC (permalink / raw)
  To: Richard Henderson, Matt Turner, Magnus Lindholm,
	Thomas Bogendoerfer, James E.J. Bottomley, Helge Deller,
	David S. Miller, Andreas Larsson, Arnd Bergmann,
	Kuniyuki Iwashima, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Willem de Bruijn, Simon Horman, Shuah Khan
  Cc: linux-alpha, linux-mips, linux-parisc, sparclinux, linux-arch,
	netdev, linux-kernel, linux-kselftest, Christian Brauner,
	Ishu Brar

From: Ishu Brar <patches@brar.si>

Check SO_MAX_DGRAM_QLEN rejects bad values and non-datagram sockets,
limit applies, lowering keeps queued datagrams, raising wakes sender
blocked in send() and one waiting in poll() within 1s, and unprivileged
user can't go above sysctl.

Receiver is bound socket, not socketpair(), receiver whose peer is
sender is exempt from queue limit.

Wakeup cases checked to fail with wake_up call removed from kernel side.

Claude wrote these tests

Signed-off-by: Ishu Brar <patches@brar.si>
---
 tools/testing/selftests/net/af_unix/Makefile     |   1 +
 tools/testing/selftests/net/af_unix/dgram_qlen.c | 243 +++++++++++++++++++++++
 2 files changed, 244 insertions(+)

diff --git a/tools/testing/selftests/net/af_unix/Makefile b/tools/testing/selftests/net/af_unix/Makefile
index fb2e09f0f44b..110cc339598b 100644
--- a/tools/testing/selftests/net/af_unix/Makefile
+++ b/tools/testing/selftests/net/af_unix/Makefile
@@ -6,6 +6,7 @@ cc-option = $(call __cc-option, $(CC),,$(1),$(2))
 CFLAGS += $(KHDR_INCLUDES) -Wall $(call cc-option,-Wflex-array-member-not-at-end)
 
 TEST_GEN_PROGS := \
+	dgram_qlen \
 	diag_uid \
 	scm_inq \
 	scm_pidfd \
diff --git a/tools/testing/selftests/net/af_unix/dgram_qlen.c b/tools/testing/selftests/net/af_unix/dgram_qlen.c
new file mode 100644
index 000000000000..7a5f42339731
--- /dev/null
+++ b/tools/testing/selftests/net/af_unix/dgram_qlen.c
@@ -0,0 +1,243 @@
+// SPDX-License-Identifier: GPL-2.0
+/* SO_MAX_DGRAM_QLEN: per-socket datagram queue length for AF_UNIX. */
+
+#define _GNU_SOURCE
+#include <errno.h>
+#include <poll.h>
+#include <stdio.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <sys/un.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+
+#include "kselftest_harness.h"
+
+#ifndef SO_MAX_DGRAM_QLEN
+#define SO_MAX_DGRAM_QLEN	89
+#endif
+
+FIXTURE(dgram_qlen)
+{
+	struct sockaddr_un addr;
+	socklen_t addrlen;
+	int rcv;
+};
+
+FIXTURE_SETUP(dgram_qlen)
+{
+	/* A bound receiver, not a socketpair: a receiver whose peer is the
+	 * sender is exempt from the queue limit in unix_dgram_sendmsg().
+	 */
+	self->addr.sun_family = AF_UNIX;
+	self->addrlen = offsetof(struct sockaddr_un, sun_path) + 1 +
+		snprintf(self->addr.sun_path + 1,
+			 sizeof(self->addr.sun_path) - 1,
+			 "dgram_qlen.%d", getpid());
+
+	self->rcv = socket(AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0);
+	ASSERT_LE(0, self->rcv);
+	ASSERT_EQ(0, bind(self->rcv, (struct sockaddr *)&self->addr,
+			  self->addrlen));
+}
+
+FIXTURE_TEARDOWN(dgram_qlen)
+{
+	close(self->rcv);
+}
+
+static int sender(FIXTURE_DATA(dgram_qlen) *self, int flags)
+{
+	int fd = socket(AF_UNIX, SOCK_DGRAM | flags, 0);
+
+	if (fd < 0)
+		return -1;
+	if (connect(fd, (struct sockaddr *)&self->addr, self->addrlen)) {
+		close(fd);
+		return -1;
+	}
+	return fd;
+}
+
+static int set_qlen(int fd, int val)
+{
+	return setsockopt(fd, SOL_SOCKET, SO_MAX_DGRAM_QLEN, &val, sizeof(val));
+}
+
+/* Send 1-byte datagrams until the queue is full; return how many fit. */
+static int fill(int fd)
+{
+	int n = 0;
+
+	while (send(fd, "x", 1, 0) == 1)
+		n++;
+	return errno == EAGAIN ? n : -errno;
+}
+
+static int drain(int fd)
+{
+	char c;
+	int n = 0;
+
+	while (recv(fd, &c, 1, 0) == 1)
+		n++;
+	return n;
+}
+
+/* Milliseconds since @start. */
+static long since_ms(const struct timespec *start)
+{
+	struct timespec now;
+
+	clock_gettime(CLOCK_MONOTONIC, &now);
+	return (now.tv_sec - start->tv_sec) * 1000 +
+	       (now.tv_nsec - start->tv_nsec) / 1000000;
+}
+
+static int sysctl_qlen(void)
+{
+	FILE *f = fopen("/proc/sys/net/unix/max_dgram_qlen", "r");
+	int val = -1;
+
+	if (f) {
+		if (fscanf(f, "%d", &val) != 1)
+			val = -1;
+		fclose(f);
+	}
+	return val;
+}
+
+TEST_F(dgram_qlen, bad_values)
+{
+	int stream = socket(AF_UNIX, SOCK_STREAM, 0);
+
+	ASSERT_EQ(-1, set_qlen(self->rcv, -1));
+	ASSERT_EQ(EINVAL, errno);
+
+	ASSERT_EQ(-1, set_qlen(stream, 4));
+	ASSERT_EQ(ENOPROTOOPT, errno);
+	close(stream);
+}
+
+/* Same off-by-one as the sysctl: a limit of N admits N + 1 datagrams. */
+TEST_F(dgram_qlen, limit_applies)
+{
+	int fd = sender(self, SOCK_NONBLOCK);
+
+	ASSERT_LE(0, fd);
+	ASSERT_EQ(0, set_qlen(self->rcv, 4));
+	ASSERT_EQ(5, fill(fd));
+	close(fd);
+}
+
+TEST_F(dgram_qlen, lower_keeps_queue)
+{
+	int fd = sender(self, SOCK_NONBLOCK);
+
+	ASSERT_LE(0, fd);
+	ASSERT_EQ(0, set_qlen(self->rcv, 8));
+	ASSERT_EQ(9, fill(fd));
+
+	ASSERT_EQ(0, set_qlen(self->rcv, 2));
+	ASSERT_EQ(0, fill(fd));
+	ASSERT_EQ(9, drain(self->rcv));
+	ASSERT_EQ(3, fill(fd));
+	close(fd);
+}
+
+/* Raising the limit must wake a sender blocked in send(), with no recv(). */
+TEST_F(dgram_qlen, raise_wakes_blocked_sender)
+{
+	struct timeval tv = { .tv_sec = 3 };
+	int fd = sender(self, 0), status;
+	pid_t pid;
+
+	ASSERT_LE(0, fd);
+	ASSERT_EQ(0, set_qlen(self->rcv, 2));
+	ASSERT_EQ(0, setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)));
+	while (send(fd, "x", 1, MSG_DONTWAIT) == 1)
+		;
+
+	pid = fork();
+	ASSERT_LE(0, pid);
+	if (pid == 0) {
+		struct timespec t;
+		int ret;
+
+		/* Without a wakeup, send() sleeps out the 3 s timeout. */
+		clock_gettime(CLOCK_MONOTONIC, &t);
+		ret = send(fd, "x", 1, 0);
+		ksft_print_msg("send() returned %d after %ld ms\n",
+			       ret, since_ms(&t));
+		_exit(ret == 1 && since_ms(&t) < 1000 ? 0 : 1);
+	}
+
+	usleep(200 * 1000);
+	ASSERT_EQ(0, set_qlen(self->rcv, 100));
+	ASSERT_EQ(pid, waitpid(pid, &status, 0));
+	ASSERT_EQ(0, WEXITSTATUS(status));
+	close(fd);
+}
+
+/* Same for a sender waiting in poll() for POLLOUT. */
+TEST_F(dgram_qlen, raise_wakes_poller)
+{
+	int fd = sender(self, SOCK_NONBLOCK), status;
+	struct pollfd pfd = { .events = POLLOUT };
+	pid_t pid;
+
+	ASSERT_LE(0, fd);
+	ASSERT_EQ(0, set_qlen(self->rcv, 2));
+	ASSERT_EQ(3, fill(fd));
+
+	pid = fork();
+	ASSERT_LE(0, pid);
+	if (pid == 0) {
+		struct timespec t;
+		int ret;
+
+		pfd.fd = fd;
+		clock_gettime(CLOCK_MONOTONIC, &t);
+		ret = poll(&pfd, 1, 3000);
+		ksft_print_msg("poll() returned %d revents %#x after %ld ms\n",
+			       ret, pfd.revents, since_ms(&t));
+		_exit(ret == 1 && (pfd.revents & POLLOUT) &&
+		      since_ms(&t) < 1000 ? 0 : 1);
+	}
+
+	usleep(200 * 1000);
+	ASSERT_EQ(0, set_qlen(self->rcv, 100));
+	ASSERT_EQ(pid, waitpid(pid, &status, 0));
+	ASSERT_EQ(0, WEXITSTATUS(status));
+	close(fd);
+}
+
+/* Without CAP_NET_ADMIN, the sysctl value is the ceiling. */
+TEST_F(dgram_qlen, unprivileged_ceiling)
+{
+	int max = sysctl_qlen(), status;
+	pid_t pid;
+
+	if (geteuid())
+		SKIP(return, "needs root to drop to nobody");
+	ASSERT_LE(0, max);
+
+	pid = fork();
+	ASSERT_LE(0, pid);
+	if (pid == 0) {
+		if (setresuid(65534, 65534, 65534))
+			_exit(10);
+		if (set_qlen(self->rcv, max))
+			_exit(11);
+		if (set_qlen(self->rcv, max + 1) != -1 || errno != EPERM)
+			_exit(12);
+		_exit(0);
+	}
+	ASSERT_EQ(pid, waitpid(pid, &status, 0));
+	ASSERT_EQ(0, WEXITSTATUS(status));
+
+	ASSERT_EQ(0, set_qlen(self->rcv, max + 1));	/* root may */
+}
+
+TEST_HARNESS_MAIN

-- 
2.56.1



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH RFC net-next 1/2] af_unix: Add SO_MAX_DGRAM_QLEN for per-socket datagram queue length
  2026-12-02  7:09   ` Ishu Brar
  (?)
@ 2026-12-03  7:09   ` sashiko-bot
  -1 siblings, 0 replies; 8+ messages in thread
From: sashiko-bot @ 2026-12-03  7:09 UTC (permalink / raw)
  To: Ishu Brar; +Cc: sparclinux

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] af_unix: non-atomic sk_max_ack_backlog update in
  unix_setsockopt()
--

commit 823d8b4920f03f27df72c9003c7366a3f59c5428
Author: Ishu Brar <patches@brar.si>

af_unix: Add SO_MAX_DGRAM_QLEN for per-socket datagram queue length

This commit adds the SO_MAX_DGRAM_QLEN socket option to configure the
datagram receive queue length (sk_max_ack_backlog) per AF_UNIX SOCK_DGRAM
socket and wakes waiting senders when the limit is raised.

Link: https://github.com/uapi-group/kernel-features
Link: https://github.com/systemd/systemd/issues/45144

> diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
> index 53a5553bfb667..2afa958c8e2ba 100644
> --- a/net/unix/af_unix.c
> +++ b/net/unix/af_unix.c
[ ... ]
> @@ -988,6 +990,29 @@ static int unix_setsockopt(struct socket *sock, int level, int optname,
>  		WRITE_ONCE(u->scm_rights_notrunc, val);
>  		break;
>  
> +	case SO_MAX_DGRAM_QLEN: {
> +		struct net *net = sock_net(sk);
> +		u32 old;
> +
> +		if (sk->sk_type != SOCK_DGRAM)
> +			return -ENOPROTOOPT;
> +
> +		if (val < 0)
> +			return -EINVAL;
> +
> +		if (val > READ_ONCE(net->unx.sysctl_max_dgram_qlen) &&
> +		    !sockopt_ns_capable(net->user_ns, CAP_NET_ADMIN))
> +			return -EPERM;
> +
> +		old = READ_ONCE(sk->sk_max_ack_backlog);
> +		WRITE_ONCE(sk->sk_max_ack_backlog, val);
> +
> +		/* Raising frees slots without a read: wake all senders. */
> +		if (val > old)
> +			wake_up_interruptible_all(&u->peer_wait);
> +		break;
> +	}
> +
>  	default:
>  		return -ENOPROTOOPT;
>  	}

[Severity: Medium]
Can concurrent SO_MAX_DGRAM_QLEN updates miss the wakeup on u->peer_wait?

unix_setsockopt() runs without holding a socket lock.  If sk_max_ack_backlog
starts at 10 with 5 datagrams queued, a lowering call can take effect
between another call's READ_ONCE() and WRITE_ONCE():

CPU0                                  CPU1
unix_setsockopt(..., val = 8)
  old = READ_ONCE(sk_max_ack_backlog)
  /* old == 10 */
                                      unix_setsockopt(..., val = 2)
                                        WRITE_ONCE(sk_max_ack_backlog, 2)
                                      unix_dgram_sendmsg()
                                        /* qlen 5 > 2: sleeps on peer_wait */
  WRITE_ONCE(sk_max_ack_backlog, 8)
  if (val > old) /* 8 > 10 is false */
    /* wakeup skipped */              <-- sender remains blocked

The queue limit ends at 8 with only 5 datagrams queued, leaving the sender
stalled in sendmsg() or poll() with slots available.  Using xchg() to
update sk->sk_max_ack_backlog or serializing under unix_state_lock(sk)
(as unix_listen() does) would ensure old reflects the value actually
replaced.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261202-af-unix-dgram-qlen-v1-0-043723f83d6d@brar.si?part=1

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH RFC net-next 2/2] selftests: af_unix: Add test for SO_MAX_DGRAM_QLEN
  2026-12-02  7:09   ` Ishu Brar via B4 Relay
  (?)
@ 2026-12-03  7:09   ` sashiko-bot
  -1 siblings, 0 replies; 8+ messages in thread
From: sashiko-bot @ 2026-12-03  7:09 UTC (permalink / raw)
  To: Ishu Brar; +Cc: sparclinux

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] selftests: af_unix: missing stddef.h include for offsetof()
  in dgram_qlen.c
- [Low] selftests: af_unix: unprivileged EPERM in dgram_qlen wakeup
  tests
- [Low] selftests: af_unix: missing dgram_qlen entry in .gitignore
--

commit 945f90bd1ff3e537024ec7cf72598717163e9cd7
Author: Ishu Brar <patches@brar.si>

selftests: af_unix: Add test for SO_MAX_DGRAM_QLEN

This commit adds a kselftest covering the SO_MAX_DGRAM_QLEN socket option
for AF_UNIX datagram sockets.

> diff --git a/tools/testing/selftests/net/af_unix/Makefile b/tools/testing/selftests/net/af_unix/Makefile
> index fb2e09f0f44bb..110cc339598bc 100644
> --- a/tools/testing/selftests/net/af_unix/Makefile
> +++ b/tools/testing/selftests/net/af_unix/Makefile
> @@ -6,6 +6,7 @@ cc-option = $(call __cc-option, $(CC),,$(1),$(2))
>  CFLAGS += $(KHDR_INCLUDES) -Wall $(call cc-option,-Wflex-array-member-not-at-end)
>  
>  TEST_GEN_PROGS := \
> +	dgram_qlen \
>  	diag_uid \
>  	scm_inq \
>  	scm_pidfd \

[Severity: Low]
Should dgram_qlen also be added to
tools/testing/selftests/net/af_unix/.gitignore alongside the other binaries
in TEST_GEN_PROGS?

> diff --git a/tools/testing/selftests/net/af_unix/dgram_qlen.c b/tools/testing/selftests/net/af_unix/dgram_qlen.c
> new file mode 100644
> index 0000000000000..7a5f423397311
> --- /dev/null
> +++ b/tools/testing/selftests/net/af_unix/dgram_qlen.c
> @@ -0,0 +1,243 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/* SO_MAX_DGRAM_QLEN: per-socket datagram queue length for AF_UNIX. */
> +
> +#define _GNU_SOURCE
> +#include <errno.h>
> +#include <poll.h>
> +#include <stdio.h>
> +#include <sys/socket.h>
> +#include <sys/types.h>
> +#include <sys/un.h>
> +#include <sys/wait.h>
> +#include <time.h>
> +#include <unistd.h>
> +
> +#include "kselftest_harness.h"

[ ... ]

> +FIXTURE_SETUP(dgram_qlen)
> +{
> +	/* A bound receiver, not a socketpair: a receiver whose peer is the
> +	 * sender is exempt from the queue limit in unix_dgram_sendmsg().
> +	 */
> +	self->addr.sun_family = AF_UNIX;
> +	self->addrlen = offsetof(struct sockaddr_un, sun_path) + 1 +
> +		snprintf(self->addr.sun_path + 1,
> +			 sizeof(self->addr.sun_path) - 1,
> +			 "dgram_qlen.%d", getpid());

[Severity: Medium]
Does this fail to build because stddef.h is not included for offsetof()?
None of the included headers or kselftest_harness.h pull in offsetof() on
glibc or musl, so compiling dgram_qlen.c fails with an undeclared identifier
error.

[ ... ]

> +/* Raising the limit must wake a sender blocked in send(), with no recv(). */
> +TEST_F(dgram_qlen, raise_wakes_blocked_sender)
> +{

[ ... ]

> +	usleep(200 * 1000);
> +	ASSERT_EQ(0, set_qlen(self->rcv, 100));
> +	ASSERT_EQ(pid, waitpid(pid, &status, 0));
> +	ASSERT_EQ(0, WEXITSTATUS(status));
> +	close(fd);
> +}
> +
> +/* Same for a sender waiting in poll() for POLLOUT. */
> +TEST_F(dgram_qlen, raise_wakes_poller)
> +{

[ ... ]

> +	usleep(200 * 1000);
> +	ASSERT_EQ(0, set_qlen(self->rcv, 100));
> +	ASSERT_EQ(pid, waitpid(pid, &status, 0));
> +	ASSERT_EQ(0, WEXITSTATUS(status));
> +	close(fd);
> +}

[Severity: Low]
Will set_qlen(self->rcv, 100) fail with EPERM in raise_wakes_blocked_sender
and raise_wakes_poller when the test runs without CAP_NET_ADMIN?

The default net.unix.max_dgram_qlen sysctl is 10, so raising the limit to 100
requires CAP_NET_ADMIN and aborts via ASSERT_EQ() before waitpid() reaps the
child process.  Raising the limit from 2 to a value at or below 10 (such as 4
or 8, as in limit_applies and lower_keeps_queue) would wake the child without
requiring privileges.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261202-af-unix-dgram-qlen-v1-0-043723f83d6d@brar.si?part=2

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2026-12-03  7:09 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02  7:09 [PATCH RFC net-next 0/2] af_unix: Per-socket datagram queue length Ishu Brar via B4 Relay
2026-12-02  7:09 ` Ishu Brar
2026-12-02  7:09 ` [PATCH RFC net-next 1/2] af_unix: Add SO_MAX_DGRAM_QLEN for per-socket " Ishu Brar via B4 Relay
2026-12-02  7:09   ` Ishu Brar
2026-12-03  7:09   ` sashiko-bot
2026-12-02  7:09 ` [PATCH RFC net-next 2/2] selftests: af_unix: Add test for SO_MAX_DGRAM_QLEN Ishu Brar
2026-12-02  7:09   ` Ishu Brar via B4 Relay
2026-12-03  7:09   ` sashiko-bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.