From: 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 --- 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