af_unix: Use cached value for SOCK_STREAM in unix_inq_len().
authorKuniyuki Iwashima <kuniyu@google.com>
Wed, 2 Jul 2025 22:35:16 +0000 (22:35 +0000)
committerJakub Kicinski <kuba@kernel.org>
Wed, 9 Jul 2025 01:05:25 +0000 (18:05 -0700)
commitf4e1fb04c12384fb1b69a95c33527b515a652a74
tree153a0bb97038b6e4a5e3d9c619a63c2dc662688f
parentd0aac85449dec992bb8dc2503f2cb9e94ef436db
af_unix: Use cached value for SOCK_STREAM in unix_inq_len().

Compared to TCP, ioctl(SIOCINQ) for AF_UNIX SOCK_STREAM socket is more
expensive, as unix_inq_len() requires iterating through the receive queue
and accumulating skb->len.

Let's cache the value for SOCK_STREAM to a new field during sendmsg()
and recvmsg().

The field is protected by the receive queue lock.

Note that ioctl(SIOCINQ) for SOCK_DGRAM returns the length of the first
skb in the queue.

SOCK_SEQPACKET still requires iterating through the queue because we do
not touch functions shared with unix_dgram_ops.  But, if really needed,
we can support it by switching __skb_try_recv_datagram() to a custom
version.

Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
Reviewed-by: Willem de Bruijn <willemb@google.com>
Link: https://patch.msgid.link/20250702223606.1054680-5-kuniyu@google.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
include/net/af_unix.h
net/unix/af_unix.c