Skip to contentSkip to conversation

af_unix: Fix inq_len update issue

PATCH Applied [email protected] 3 participants 6 messages Jun 1 – Jun 4

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

v3 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:
v3:
- Patch 2: Align macro definitions
v2: https://lore.kernel.org/netdev/[email protected]/
- 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
#1 lore raw

PATCH v3 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.

Fixes f4e1fb04c123 ("af_unix: Use cached value for SOCK_STREAM in unix_inq_len().")

---
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
index dc71ed79be..0d9cd977c7 100644
@@ -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
maintainer Reviewed-by on v3 1/2
#2 lore raw

Re: [PATCH net v3 1/2] af_unix: Fix inq_len update problem in partial read

Quoted 7 lines · On Mon, Jun 1, 2026 at 4:37 AM <[email protected]> wrote:

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.

Quoted 8 lines

This change update inq_len timely in unix_stream_read_generic(), and adjust unix_stream_read_skb() accordingly to prevent repetitive update.

Fixes f4e1fb04c123 ("af_unix: Use cached value for SOCK_STREAM in unix_inq_len().")

---
v2: Improve lock usage in unix_stream_read_generic()

For future submissions, since this patch has no change since v2, you could carry my tag from v2.

Thanks !

#3 lore raw

PATCH v3 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

---
v3: Align macro definitions
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
index 3a86be9bda..6268b5bf50 100644
@@ -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	3
 
 FIXTURE(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
maintainer Reviewed-by on v3 2/2
#4 lore raw

Re: [PATCH net v3 2/2] af_unix: Add test for SCM_INQ on partial read

Quoted 14 lines · On Mon, Jun 1, 2026 at 4:37 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_read

Quoted 7 lines

After:

# RUN scm_inq.stream.partial_read ...
# OK scm_inq.stream.partial_read
ok 2 scm_inq.stream.partial_read

#5 lore raw
In reply to [email protected]
This series was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:

Quoted 13 lines · On Mon, 1 Jun 2026 19:36:38 +0800 you wrote:

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:
v3:
- Patch 2: Align macro definitions
v2: https://lore.kernel.org/netdev/[email protected]/
- 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]/

Quoted 1 line

[...]

Here is the summary with links:
- [net,v3,1/2] af_unix: Fix inq_len update problem in partial read

    https://git.kernel.org/netdev/net/c/c1f07a7f2d47
  - [net,v3,2/2] af_unix: Add test for SCM_INQ on partial read
    https://git.kernel.org/netdev/net/c/dd8975ad710e

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html