af_unix: Fix inq_len update issue
Subsystems (4)
v2 cover af_unix: Fix inq_len update issue
From: Jianyu Li <[email protected]>
This series fix the problem that inq_len is inconsistent with actual remaining byte count when only part of a skb is consumed.
Changes:
v2:
- Patch 1: Improve lock usage in unix_stream_read_generic()
- Patch 2: Follow reverse xmas tree ordering in partial_read test case
v1: https://lore.kernel.org/netdev/[email protected]/
Jianyu Li (2):
af_unix: Fix inq_len update problem in partial read
af_unix: Add test for SCM_INQ on partial read
net/unix/af_unix.c | 11 ++-- tools/testing/selftests/net/af_unix/scm_inq.c | 54 ++++++++++++++++++- 2 files changed, 58 insertions(+), 7 deletions(-)
Signature · 1 line
2.45.2
PATCH v2 1/2 af_unix: Fix inq_len update problem in partial read
From: Jianyu Li <[email protected]>
Currently inq_len is updated only when the whole skb is consumed. If only part of the data is read, following SIOCINQ query would get value greater than what actually left.
This change update inq_len timely in unix_stream_read_generic(), and adjust unix_stream_read_skb() accordingly to prevent repetitive update.
---
v2: Improve lock usage in unix_stream_read_generic()
--- net/unix/af_unix.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
net/unix/af_unix.c +6 -5
@@ -2886,7 +2886,7 @@ static int unix_stream_read_skb(struct sock *sk, skb_read_actor_t recv_actor)return -EAGAIN ; } -WRITE_ONCE (u->inq_len, u->inq_len - skb->len); +WRITE_ONCE (u->inq_len, u->inq_len -unix_skb_len (skb));#if IS_ENABLED (CONFIG_AF_UNIX_OOB )if (skb == u->oob_skb) {
@@ -3063,11 +3063,12 @@ static int unix_stream_read_generic(struct unix_stream_read_state *state,unix_detach_fds (&scm, skb); } -if (unix_skb_len (skb)) -break ; -spin_lock (&sk->sk_receive_queue.lock); -WRITE_ONCE (u->inq_len, u->inq_len - skb->len); +WRITE_ONCE (u->inq_len, u->inq_len - chunk); +if (unix_skb_len (skb)) { +spin_unlock (&sk->sk_receive_queue.lock); +break ; + }__skb_unlink (skb, &sk->sk_receive_queue);spin_unlock (&sk->sk_receive_queue.lock);
Signature · 1 line
2.45.2
Re: [PATCH net v2 1/2] af_unix: Fix inq_len update problem in partial read
Quoted 7 lines · On Thu, May 28, 2026 at 4:00 AM <[email protected]> wrote:
From: Jianyu Li <[email protected]>
Currently
inq_lenis updated only when the whole skb is consumed. If only part of the data is read, following SIOCINQ query would get value greater than what actually left.
Quoted 6 lines
This change update
inq_lentimely inunix_stream_read_generic(), and adjustunix_stream_read_skb()accordingly to prevent repetitive update.Fixes f4e1fb04c123 ("af_unix: Use cached value for SOCK_STREAM in unix_inq_len().")Signed-off-by Jianyu Li <[email protected]>
PATCH v2 2/2 af_unix: Add test for SCM_INQ on partial read
In reply to [email protected]From: Jianyu Li <[email protected]>
Add test to verify that when a skb is partially consumed, unix_inq_len() return correct remaining byte count.
Before:
# RUN scm_inq.stream.partial_read ...
# scm_inq.c:165:partial_read:Expected remain (512) == *(int *)CMSG_DATA(cmsg) (768) # partial_read: Test terminated by assertion
# FAIL scm_inq.stream.partial_read
not ok 2 scm_inq.stream.partial_read
After:
# RUN scm_inq.stream.partial_read ...
# OK scm_inq.stream.partial_read
ok 2 scm_inq.stream.partial_read
---
v2: Follow reverse xmas tree ordering in partial_read test case
--- tools/testing/selftests/net/af_unix/scm_inq.c | 54 ++++++++++++++++++- 1 file changed, 52 insertions(+), 2 deletions(-)
tools/testing/selftests/net/af_unix/scm_inq.c +52 -2
@@ -8,8 +8,9 @@#include "kselftest_harness.h" -#define NR_CHUNKS 100 -#define MSG_LEN 256 +#define NR_CHUNKS 100 +#define MSG_LEN 256 +#define NR_PARTIAL_READS 3FIXTURE (scm_inq) {
@@ -120,4 +121,53 @@ TEST_F(scm_inq, basic)recv_chunks (_metadata, self); } +TEST_F(scm_inq, partial_read) +{ +char buf[MSG_LEN *NR_PARTIAL_READS ] = {}; +char cmsg_buf[CMSG_SPACE (sizeof (int ))]; +struct msghdr msg = {}; +struct iovec iov = {}; +struct cmsghdr *cmsg; +int err, inq, ret, i; +int remain; + + err =setsockopt (self->fd[1 ],SOL_SOCKET ,SO_INQ , &(int ){1 },sizeof (int )); +if (variant->type !=SOCK_STREAM ) { +ASSERT_EQ (-ENOPROTOOPT , -errno); +return ; + } +ASSERT_EQ (0 , err); + + ret =send (self->fd[0 ], buf,sizeof (buf),0 ); +ASSERT_EQ (sizeof (buf), ret); + + msg.msg_iov = &iov; + msg.msg_iovlen =1 ; + msg.msg_control = cmsg_buf; + msg.msg_controllen =sizeof (cmsg_buf); + + iov.iov_base = buf; + iov.iov_len =MSG_LEN ; + +for (i =0 ; i <NR_PARTIAL_READS ; i++) { + remain =MSG_LEN * (NR_PARTIAL_READS -1 - i); + +memset (buf,0 ,MSG_LEN ); +memset (cmsg_buf,0 ,sizeof (cmsg_buf)); + ret =recvmsg (self->fd[1 ], &msg,0 ); +ASSERT_EQ (MSG_LEN , ret); + + cmsg =CMSG_FIRSTHDR (&msg); +ASSERT_NE (NULL , cmsg); +ASSERT_EQ (CMSG_LEN (sizeof (int )), cmsg->cmsg_len); +ASSERT_EQ (SOL_SOCKET , cmsg->cmsg_level); +ASSERT_EQ (SCM_INQ , cmsg->cmsg_type); +ASSERT_EQ (remain, *(int *)CMSG_DATA (cmsg)); + + ret =ioctl (self->fd[1 ],SIOCINQ , &inq); +ASSERT_EQ (0 , ret); +ASSERT_EQ (remain, inq); + } +} +TEST_HARNESS_MAIN
Signature · 1 line
2.45.2
Re: [PATCH net v2 2/2] af_unix: Add test for SCM_INQ on partial read
Quoted 32 lines · On Thu, May 28, 2026 at 4:00 AM <[email protected]> wrote:
From: Jianyu Li <[email protected]>
Add test to verify that when a skb is partially consumed,
unix_inq_len()return correct remaining byte count.Before:
# RUN scm_inq.stream.partial_read ...
# scm_inq.c:165:partial_read:Expected remain (512) == *(int *)CMSG_DATA(cmsg) (768) #partial_read: Test terminated by assertion
# FAIL scm_inq.stream.partial_read
not ok 2 scm_inq.stream.partial_readAfter:
# RUN scm_inq.stream.partial_read ...
# OK scm_inq.stream.partial_read
ok 2 scm_inq.stream.partial_readSigned-off-by Jianyu Li <[email protected]>---
v2: Follow reverse xmas tree ordering inpartial_readtest case--- tools/testing/selftests/net/af_unix/scm_inq.c | 54 ++++++++++++++++++- 1 file changed, 52 insertions(+), 2 deletions(-)tools/testing/selftests/net/af_unix/scm_inq.c +0
-0
tools/testing/selftests/net/af_unix/scm_inq.c line 12
Quoted 7 lines
tools/testing/selftests/net/af_unix/scm_inq.c +2
-2@@ -8,8 +8,9 @@#include "kselftest_harness.h" -#define NR_CHUNKS 100 -#define MSG_LEN 256 +#define NR_CHUNKS 100 +#define MSG_LEN 256
nit: please align the indentation of the above two lines with the line below.
Sorry, I should've looked closer.
tools/testing/selftests/net/af_unix/scm_inq.c line 13
Quoted 1 line
tools/testing/selftests/net/af_unix/scm_inq.c +1
-0+#define NR_PARTIAL_READS 3
Otherwise looks good.
Thanks
Re: [PATCH net v2 2/2] af_unix: Add test for SCM_INQ on partial read
Quoted 49 lines · Kuniyuki Iwashima
Quoted 35 lines · On Thu, May 28, 2026 at 4:00 AM <[email protected]> wrote:
From: Jianyu Li <[email protected]>
Add test to verify that when a skb is partially consumed,
unix_inq_len()return correct remaining byte count.Before:
# RUN scm_inq.stream.partial_read ... # scm_inq.c:165:partial_read:Expected remain (512) == *(int *)CMSG_DATA(cmsg) (768)
#partial_read: Test terminated by assertion
# FAIL scm_inq.stream.partial_read
not ok 2 scm_inq.stream.partial_readAfter:
# RUN scm_inq.stream.partial_read ... # OK scm_inq.stream.partial_read ok 2 scm_inq.stream.partial_read
Signed-off-by Jianyu Li <[email protected]>---
v2: Follow reverse xmas tree ordering inpartial_readtest case
---
tools/testing/selftests/net/af_unix/scm_inq.c | 54
++++++++++++++++++-1 file changed, 52 insertions(+), 2 deletions(-)tools/testing/selftests/net/af_unix/scm_inq.c +0
-0tools/testing/selftests/net/af_unix/scm_inq.c line 12
Quoted 7 lines
tools/testing/selftests/net/af_unix/scm_inq.c +2
-2@@ -8,8 +8,9 @@ #include "kselftest_harness.h" -#define NR_CHUNKS 100 -#define MSG_LEN 256 +#define NR_CHUNKS 100 +#define MSG_LEN 256nit: please align the indentation of the above two lines with the line below.
Sorry, I should've looked closer.
Quoted 7 lines
tools/testing/selftests/net/af_unix/scm_inq.c line 13
Quoted 1 line
tools/testing/selftests/net/af_unix/scm_inq.c +1
-0+#define NR_PARTIAL_READS 3Otherwise looks good.
Reviewed-by Kuniyuki Iwashima <[email protected]>Thanks
Thanks Kuniyuki, my indent width setting was 4 so I did not notice it. I will fix it in next patch.
Best Regards,
Jianyu