Linux s390 Architecture development
 help / color / mirror / Atom feed
* [PATCH RFC net v4 0/8] net/iucv: serialize the HiperSockets receive path with the socket lock
@ 2026-11-15  1:29 Bryam Vargas via B4 Relay
  2026-11-15  1:29 ` [PATCH RFC net v4 1/8] net/iucv: keep the window given to the peer in a field of its own Bryam Vargas via B4 Relay
                   ` (7 more replies)
  0 siblings, 8 replies; 17+ messages in thread
From: Bryam Vargas via B4 Relay @ 2026-11-15  1:29 UTC (permalink / raw)
  To: Jakub Kicinski, Alexandra Winter, Paolo Abeni, David S. Miller,
	Hidayath Khan, Eric Dumazet
  Cc: Thorsten Winkler, Simon Horman, netdev, linux-kernel, Nagamani PV,
	linux-s390

v4 of stage 1 of the socket-locking rework [1]. It is v3 [2] with the
two findings of the AI review of v3 fixed ([3] on 1/7, [4] on 3/7) and
the dev_hold() the netdev CI flagged replaced. Nobody else has reviewed
v3 yet, so the rest is unchanged.

  1/8  a msglimit changed while the SYN is out no longer closes the
       socket on a peer that kept to its window, nor stalls that peer
  2/8  iucv_send_ctrl() leaves sk_shutdown alone (new)
  3/8  sendmsg() fails with -EPIPE when sending is shut down while it
       waits for the window
  4/8  recvmsg() under lock_sock()
  5/8  the frame dispatch and three state changes moved into functions
       of their own, no functional change
  6/8  the HiperSockets rx path under the socket lock: a control frame
       waits for the task that owns the socket
  7/8  shutdown(SHUT_RD) against a frame in flight
  8/8  EOF, not -ENOTCONN, for a reader parked when the peer closes

1/8 to 3/8 and 8/8 are fixes in their own right. 1/8 to 3/8 each apply
to net without the rest; 3/8 is complete only on top of 2/8. 8/8 is
written on top of 6/8: its hunks are in functions that 4/8 and 5/8 add.

Alexandra, 2/8 needs your call. v3 dropped the iucv_send_ctrl() rewrite
as you asked, and 2/8 brings back one part of it: the function no longer
clears SEND_SHUTDOWN around its allocation. That clear is behind the
finding on v3 3/7 [4], behind the bit my copy of Hidayath's sk_shutdown
test loses, and behind the two items v3's notes left open under 2/7 and
7/7. The rest stays out: the allocation still asks for room in
sk_sndbuf, for every control frame. Is the SEND_SHUTDOWN part fine as a
patch of its own? If not I can fold it into 4/8; 3/8 then has the gap
the note of v3 2/7 described, up to 4/8.

RFC because one thing is missing that I can't provide. Hidayath,
Alexandra: could one of you run v4 on z/VM the way Hidayath ran v1 [5]?
I depend on those numbers: my setup was tuned against that run, and the
series moves enough code to need one again before this is a PATCH. What
I'd compare is below, with net as the baseline (the numbers from [5] in
parentheses, on net-next unless marked). Each line names the patch where
the code changes; the kernels I ran for it are under that patch:

- recvrecv (3917 backwards reads): 0 from 4/8 on
- sk_shutdown (565 of 7281 cycles): 0 from 2/8 on, as in my setup, or
  from 4/8 if that test also loses the bit the way 4/8's second item
  describes
- shut_rd (32 of 212): as net up to 5/8, 0 from 6/8 on. 6/8 leaves the
  window KCSAN reported on v1 [6], which shut_rd did not hit there and
  hits here only with a udelay() added to widen it; 7/8 closes it
- the blocked reader (-ENOTCONN in 13627 of 13627): EOF in all with 8/8
- rxclose (51 of 906): 0 with 8/8
- SO_MSGLIMIT lowered in IUCV_BOUND (1 message lost in 6297 cycles on
  v1; none on net-next, which has no budget): none lost at any step.
  The budget comes back with 6/8 and counts against the window of 1/8
- KCSAN: the 29 reports on net-next gone, the 6 between afiucv_hs_rcv()
  and iucv_send_ctrl() and the 2 between iucv_send_ctrl() and
  iucv_sock_shutdown() with 2/8, the 19 between recvmsg() and
  iucv_sock_shutdown() with 4/8, the 2 between afiucv_hs_rcv() and
  iucv_sock_shutdown() with 7/8. Up to 7/8 the receive wait still reads
  sk_shutdown with READ_ONCE() against shutdown()'s plain write, so a
  report of that pair can stay until then, and on a z/VM socket after
  it: 7/8 marks the write on the HiperSockets branch only. The hs_dev
  report of the v1 run [6] is not one of the 29; 1/2 of the two fixes
  posted for net on 28 Oct [7] is for it, and is not in this series

My setup is QEMU TCG with an emulated HiperSockets adapter and CP, so
rates and real SMP come only from the hardware, and I have no KCSAN
build of it. Where the two disagree, the hardware is the reference.

Changes in v4 (details below the --- of each patch):

- 1/8: a second SYN of the socket carries the window the first one gave
  and records nothing, and the window is recorded when the first SYN is
  handed to the device, not while its header is built (AI review [3])
- 2/8 is new, see above. With it the writer that 4/8 lets wait for send
  memory without the socket lock returns -EPIPE when shutdown(SHUT_WR)
  runs meanwhile; in v3 it could sleep through that wake-up (AI review
  [4])
- 6/8: the SYN that waits in the backlog holds its device with
  netdev_hold() and netdev_put() (netdev CI)
- 4/8 and 7/8: message only, for what 2/8 changes under them

Not in v4, and what that costs: five of my 53 default tests (the set v2
was tested with) fail on v4, as they do on net. With the three pieces
v3 dropped applied on top again (the rest of the iucv_send_ctrl()
rewrite, the window update only while connected, the credit reset), all
53 pass.

- iucv_send_ctrl() still needs room in sk_sndbuf for every control
  frame. With the send memory used up, which on HiperSockets lasts until
  TX completion, shutdown(SHUT_WR) sends no SHT and the peer never reads
  EOF (two tests), and the FIN of NETDEV_GOING_DOWN does not go out
  (one)
- recvmsg() still sends a window update after the peer's FIN, a frame
  nobody uses (one); [7] 2/2 has the test for it
- a window update that finds no room leaves its credit, and the SHT read
  next is counted past the window: WARN_ON() and -EFAULT where EOF was
  due (one). [7] 2/2 ends that by not counting the SHT

net fails three more: two that 3/8 fixes and one that 8/8 fixes.

Tested on s390x under QEMU TCG, KASAN + PROVE_LOCKING +
DEBUG_ATOMIC_SLEEP, on net main, against the emulated adapter under the
real qeth and qdio drivers and the emulated CP, as v3 was. Two vCPUs
under -icount, which runs one at a time; the race tests also with seeded
short slices, and the two-reader test of 4/8 under MTTCG. No KASAN or
lockdep splat on net, at any step of the series or on the kernels that
follow. Of 25 kernels each with one piece of the series taken out or
changed (6 for 1/8, 1 for 2/8, 3 for 3/8, 1 for 4/8, 10 for 6/8, 1 for
7/8, 3 for 8/8; the tracker build under 6/8 is not one of them), 23 fail
a test the series passes; two of the 23 need a test kernel under them,
one that drops the first SYN in the device (1/8) and one that makes two
waits overlap (2/8). The other two show only with a delay added to hold
their window open: the one without 7/8's spinlock and the one without
the receive-queue test in 8/8's wait. The numbers are below the --- of
each patch. Logs on request.

Still open, as in v3: afiucv_hs_callback_txnotify() and the z/VM
callbacks change sk_state without checking whether a task owns the
socket, and so does afiucv_netdev_event(), whose fix Nagamani is holding
for this series [8]. backlog_skb_q stays unbounded for a peer that
ignores its window. A listener with a full accept queue still leaks the
child of the SYN it refuses (measured on net and with the series,
numbers under 6/8), and so does a SYN|ACK that fails to go out (by
inspection) [9]. connect() tests sk_state before lock_sock(), so a
connected socket can still send a SYN [3]. On a z/VM socket
iucv_sock_shutdown() and recvmsg() update sk_shutdown under lock_sock()
and iucv_callback_shutdown() under bh_lock_sock(), so either side can
lose its bit, and iucv_callback_rx() tests RCV_SHUTDOWN with no lock:
7/8 takes the spinlock on the HiperSockets branch only. And
iucv_callback_connreq() answers a path request for a listener that
close() is emptying, the race 6/8 closes for a HiperSockets SYN.

Seen while working on v4, in net as well and not touched here:

- a HiperSockets recv() that owes a window update while the send memory
  is full sets IUCV_DISCONN: iucv_send_ctrl() finds no room and
  recvmsg() takes that for a dead path. It is the same failed update as
  in the last item of "Not in v4": that item is what the next read does
  with the credit, this one the state the socket is left in. Measured on
  net and with the series
- a z/VM sendmsg() that waits for send memory is not ended when the
  peer severs the path: the memory is freed only at close(). By
  inspection
- afiucv_hs_send() passes NET_XMIT_DROP through, so a send() or
  connect() whose frame the qdisc dropped returns 1. By inspection for
  the qdisc; the test kernel of 1/8 that drops the SYN shows the
  connect() half

[1] https://lore.kernel.org/all/20260724222917.134769-1-hexlabsecurity@proton.me/
[2] https://lore.kernel.org/netdev/20261110-b4-disp-2a6b4364-v3-0-35677b68e697@proton.me/
[3] https://lore.kernel.org/all/sashiko-outbox-210383@kernel.org/
[4] https://lore.kernel.org/all/sashiko-outbox-210385@kernel.org/
[5] https://lore.kernel.org/netdev/51a4a40a-f34b-4d41-b049-69aa53f4dcf5@linux.ibm.com/
[6] https://lore.kernel.org/netdev/b3fea2ca-25ff-42c0-812c-f8cc6bb6667e@linux.ibm.com/
[7] https://lore.kernel.org/netdev/20261028-b4-disp-dd3e4ebb-v2-0-3747658d59bd@proton.me/
[8] https://lore.kernel.org/netdev/1d9d3cc1-24fb-40e0-a4c7-6fbfbcf4dc37@linux.ibm.com/
[9] https://lore.kernel.org/netdev/5f88daf4-cd8e-4464-b132-ac45be205121@linux.ibm.com/

---
Bryam Vargas (8):
      net/iucv: keep the window given to the peer in a field of its own
      net/iucv: do not clear SEND_SHUTDOWN to send a control frame
      net/iucv: fail sendmsg() with EPIPE once sending is shut down
      net/iucv: run recvmsg() under the socket lock
      net/iucv: factor out the HiperSockets frame dispatch and state changes
      net/iucv: serialize HiperSockets receive with the socket lock
      net/iucv: set RCV_SHUTDOWN under the socket spinlock
      net/iucv: return EOF to a reader parked when the peer closes

 include/net/iucv/af_iucv.h |   8 +
 net/iucv/af_iucv.c         | 687 +++++++++++++++++++++++++++++++++++++--------
 2 files changed, 579 insertions(+), 116 deletions(-)
---
base-commit: 8373c4e8863ac5f5120d200527aca9880330f343
change-id: 20261114-b4-disp-fc0634a3-91cb1515c961

Best regards,
--  
Bryam Vargas <hexlabsecurity@proton.me>



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

* [PATCH RFC net v4 1/8] net/iucv: keep the window given to the peer in a field of its own
  2026-11-15  1:29 [PATCH RFC net v4 0/8] net/iucv: serialize the HiperSockets receive path with the socket lock Bryam Vargas via B4 Relay
@ 2026-11-15  1:29 ` Bryam Vargas via B4 Relay
  2026-11-16  1:29   ` sashiko-bot
  2026-11-15  1:29 ` [PATCH RFC net v4 2/8] net/iucv: do not clear SEND_SHUTDOWN to send a control frame Bryam Vargas via B4 Relay
                   ` (6 subsequent siblings)
  7 siblings, 1 reply; 17+ messages in thread
From: Bryam Vargas via B4 Relay @ 2026-11-15  1:29 UTC (permalink / raw)
  To: Jakub Kicinski, Alexandra Winter, Paolo Abeni, David S. Miller,
	Hidayath Khan, Eric Dumazet
  Cc: Thorsten Winkler, Simon Horman, netdev, linux-kernel, Nagamani PV,
	linux-s390

From: Bryam Vargas <hexlabsecurity@proton.me>

A HiperSockets SYN carries msglimit as the receive window, and
SO_MSGLIMIT is accepted while the socket is IUCV_BOUND, which it is
until the SYN|ACK arrives. A msglimit changed in between leaves the peer
with the window the SYN carried, while recvmsg() counts the frames it
reads against the new value:

- Lowered, a peer that keeps to its window has the socket closed on it
  once the receive buffer is full and frames wait in backlog_skb_q,
  where no window update resets the count: WARN_ON(), -EFAULT, the rest
  of the stream lost.
- Raised far enough, the threshold of the window update lies above the
  window: the count never reaches it, the peer uses up its window and
  its writer waits for an update that only a data frame from us would
  carry.

Record the window when the first SYN of a socket leaves, and in a
listener's SYN|ACK, and let recvmsg() count frames against it and take
the window-update threshold from it. A SYN that is not sent records
nothing, and a later SYN of the socket carries the recorded window. A
msglimit set after the first SYN no longer changes anything for that
socket; getsockopt() still returns it. A socket connected by a SYN|ACK
without having sent a SYN keeps msglimit as its window.

Fixes: 3881ac441f64 ("af_iucv: add HiperSockets transport")
Fixes: 800c5eb7b5eb ("af_iucv: change net_device handling for HS transport")
Cc: stable@vger.kernel.org # 6.18.x
Signed-off-by: Bryam Vargas <hexlabsecurity@proton.me>
---
v4 (AI review of v3, [3] in the cover letter):
- a later SYN of the socket carries the window the first one gave and
  records nothing. v3 wrote the field for every SYN, so a second
  connect() in IUCV_BOUND after a SO_MSGLIMIT change left the peer of
  the first SYN with a window the socket no longer counted against
- the window is recorded when the first SYN is handed to the device,
  after the device and MTU tests, and forgotten if that transmit fails.
  v3 recorded it while building the header: a connect() that failed
  with -ENETDOWN left a window nobody had received
- not changed: connect() still tests sk_state before lock_sock(), the
  last case of that review. A SYN sent that way does not move the
  window: it is recorded by then, or the socket was connected by a
  SYN|ACK it never asked for and the SYN records the msglimit the
  socket already counts against
v3: https://lore.kernel.org/netdev/20261110-b4-disp-2a6b4364-v3-1-35677b68e697@proton.me/

It goes first because the backlog of 6/8 counts against this window: on
the live msglimit it would drop data without a trace, as in Hidayath's
SO_MSGLIMIT scenario on v1.

Both cases in the message repro in the emulated setup, with the SYN|ACK
held back in the adapter while the limit changes:

- SO_MSGLIMIT 8 when the SYN goes out, lowered to 2, minimum SO_RCVBUF,
  the peer sends its 8 frames. net reads 2, then takes the WARN_ON() in
  iucv_sock_recvmsg() and returns -EFAULT; with this patch all 8 arrive.
  With the default SO_RCVBUF net delivers the 8 as well: a window update
  goes out after each read and resets the count.
- SO_MSGLIMIT 2 when the SYN goes out, raised to 8. The peer sends 2 and
  the socket reads both. On net no window update leaves and the peer's
  next send returns -EAGAIN; with this patch the update leaves and the
  send goes through. With the limit left at 2 both kernels send the
  update.

The v4 items, same setup:

- The lowered case with a second connect() after the limit changed, so
  that a second SYN leaves with the SYN|ACK of the first still held. The
  child the first SYN made sends its 8 frames: net reads 2, WARN_ON()
  and -EFAULT; this patch reads all 8. With the field written by every
  SYN again the socket reads 2 and takes the -EFAULT whichever child
  sends. net and this patch read all 8 from the child the second SYN
  made; with the second SYN carrying the live msglimit that child stops
  after 2 frames, as the socket counts against 8 and owes it no update.
- connect() with the interface down and SO_MSGLIMIT 1: -ENETDOWN, no SYN
  leaves. Interface up, the limit raised to 8, then a forged SYN|ACK and
  three frames. net and this patch read all three; with the window
  recorded ahead of the device test, as in v3, the socket reads one and
  the next read takes the WARN_ON() and returns -EFAULT.
- For a transmit that fails after the record I have no qdisc that drops
  on demand, so a test kernel drops the SYN to a destination name that
  begins with DSY: it frees the skb where dev_queue_xmit() would be
  called and hands back NET_XMIT_DROP. The sequence above with that
  SYN: the three frames are read; with the record left in place the
  second read fails as above.

Not changed, and for the maintainers to call: once a first SYN has
left, SO_MSGLIMIT is still accepted while the socket is IUCV_BOUND and
no longer moves the window, even when the next connect() names another
peer. The window given and the window counted agree, which is all this
patch is after. Refusing SO_MSGLIMIT once a SYN has left, or forgetting
the window when the destination changes, would each be a change of its
own.

The last sentence of the message is the third item of the AI review of
v2. net connects an IUCV_BOUND socket on any SYN|ACK, whether or not it
sent a SYN. The series leaves that as it is. With a forged SYN|ACK and
one byte for a socket that never called connect(), net and this patch
return the byte; with the window left at 0, as in v2, the read takes
the WARN_ON() and returns -EFAULT.

With afiucv_hs_window() back on the live msglimit, the lowered case
loses data in the backlog of 6/8 and the raised one stalls as on net.
None of this shows in the 53 default tests: with this patch the same
eight fail as on net, with the same result.
---
 include/net/iucv/af_iucv.h |  1 +
 net/iucv/af_iucv.c         | 35 ++++++++++++++++++++++++++++++++---
 2 files changed, 33 insertions(+), 3 deletions(-)

diff --git a/include/net/iucv/af_iucv.h b/include/net/iucv/af_iucv.h
index df85d19fbf84..6decbb1f34b3 100644
--- a/include/net/iucv/af_iucv.h
+++ b/include/net/iucv/af_iucv.h
@@ -130,6 +130,7 @@ struct iucv_sock {
 	u8			flags;
 	u16			msglimit;
 	u16			msglimit_peer;
+	u16			msglimit_adv;	/* window given to the peer */
 	atomic_t		skbs_in_xmit;
 	atomic_t		msg_sent;
 	atomic_t		msg_recv;
diff --git a/net/iucv/af_iucv.c b/net/iucv/af_iucv.c
index ce13fe3c85b5..daed2a3cfd66 100644
--- a/net/iucv/af_iucv.c
+++ b/net/iucv/af_iucv.c
@@ -176,6 +176,22 @@ static inline int iucv_below_msglim(struct sock *sk)
 			(atomic_read(&iucv->pendings) <= 0));
 }
 
+/**
+ * afiucv_hs_window() - the window a HiperSockets socket gave its peer
+ * @iucv:	the socket
+ *
+ * The msglimit carried by its first SYN that was handed to the device, or
+ * by its SYN|ACK. A socket that sent neither has msglimit as its window:
+ * one about to send its first SYN, or one connected by a SYN|ACK it did not
+ * ask for.
+ *
+ * Return: the frames the peer may send before it needs a window update.
+ */
+static u16 afiucv_hs_window(const struct iucv_sock *iucv)
+{
+	return iucv->msglimit_adv ?: iucv->msglimit;
+}
+
 /*
  * iucv_sock_wake_msglim() - Wake up thread waiting on msg limit
  */
@@ -200,6 +216,7 @@ static int afiucv_hs_send(struct iucv_message *imsg, struct sock *sock,
 	struct iucv_sock *iucv = iucv_sk(sock);
 	struct af_iucv_trans_hdr *phs_hdr;
 	int err, confirm_recv = 0;
+	bool first_syn = false;
 
 	phs_hdr = skb_push(skb, sizeof(*phs_hdr));
 	memset(phs_hdr, 0, sizeof(*phs_hdr));
@@ -209,7 +226,7 @@ static int afiucv_hs_send(struct iucv_message *imsg, struct sock *sock,
 	phs_hdr->version = 1;
 	phs_hdr->flags = flags;
 	if (flags == AF_IUCV_FLAG_SYN)
-		phs_hdr->window = iucv->msglimit;
+		phs_hdr->window = afiucv_hs_window(iucv);
 	memcpy(phs_hdr->destUserID, iucv->dst_user_id, 8);
 	memcpy(phs_hdr->destAppName, iucv->dst_name, 8);
 	memcpy(phs_hdr->srcUserID, iucv->src_user_id, 8);
@@ -255,11 +272,21 @@ static int afiucv_hs_send(struct iucv_message *imsg, struct sock *sock,
 			phs_hdr->flags = phs_hdr->flags | AF_IUCV_FLAG_WIN;
 	}
 
+	/* The first SYN to leave: its window is what the peer may send us,
+	 * whatever msglimit becomes. A later SYN carries it.
+	 */
+	if (flags == AF_IUCV_FLAG_SYN && !iucv->msglimit_adv) {
+		iucv->msglimit_adv = iucv->msglimit;
+		first_syn = true;
+	}
+
 	atomic_inc(&iucv->skbs_in_xmit);
 	err = dev_queue_xmit(skb);
 	if (net_xmit_eval(err)) {
 		atomic_dec(&iucv->skbs_in_xmit);
 		atomic_add(confirm_recv, &iucv->msg_recv);
+		if (first_syn)
+			iucv->msglimit_adv = 0;
 	}
 	return net_xmit_eval(err);
 
@@ -1320,7 +1347,8 @@ static int iucv_sock_recvmsg(struct socket *sock, struct msghdr *msg,
 		consume_skb(skb);
 		if (iucv->transport == AF_IUCV_TRANS_HIPER) {
 			atomic_inc(&iucv->msg_recv);
-			if (atomic_read(&iucv->msg_recv) > iucv->msglimit) {
+			if (atomic_read(&iucv->msg_recv) >
+			    afiucv_hs_window(iucv)) {
 				WARN_ON(1);
 				iucv_sock_close(sk);
 				return -EFAULT;
@@ -1345,7 +1373,7 @@ static int iucv_sock_recvmsg(struct socket *sock, struct msghdr *msg,
 				iucv_process_message_q(sk);
 			if (iucv->transport == AF_IUCV_TRANS_HIPER &&
 			    atomic_read(&iucv->msg_recv) >=
-							iucv->msglimit / 2)
+						afiucv_hs_window(iucv) / 2)
 				send_win = true;
 		}
 		spin_unlock_bh(&iucv->message_q.lock);
@@ -1926,6 +1954,7 @@ static int afiucv_hs_callback_syn(struct sock *sk, struct sk_buff *skb)
 	afiucv_swap_src_dest(skb);
 	trans_hdr->flags = AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK;
 	trans_hdr->window = niucv->msglimit;
+	niucv->msglimit_adv = niucv->msglimit;
 	/* if receiver acks the xmit connection is established */
 	err = dev_queue_xmit(skb);
 	if (!err) {

-- 
2.56.0



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

* [PATCH RFC net v4 2/8] net/iucv: do not clear SEND_SHUTDOWN to send a control frame
  2026-11-15  1:29 [PATCH RFC net v4 0/8] net/iucv: serialize the HiperSockets receive path with the socket lock Bryam Vargas via B4 Relay
  2026-11-15  1:29 ` [PATCH RFC net v4 1/8] net/iucv: keep the window given to the peer in a field of its own Bryam Vargas via B4 Relay
@ 2026-11-15  1:29 ` Bryam Vargas via B4 Relay
  2026-11-16  1:29   ` sashiko-bot
  2026-11-15  1:29 ` [PATCH RFC net v4 3/8] net/iucv: fail sendmsg() with EPIPE once sending is shut down Bryam Vargas via B4 Relay
                   ` (5 subsequent siblings)
  7 siblings, 1 reply; 17+ messages in thread
From: Bryam Vargas via B4 Relay @ 2026-11-15  1:29 UTC (permalink / raw)
  To: Jakub Kicinski, Alexandra Winter, Paolo Abeni, David S. Miller,
	Hidayath Khan, Eric Dumazet
  Cc: Thorsten Winkler, Simon Horman, netdev, linux-kernel, Nagamani PV,
	linux-s390

From: Bryam Vargas <hexlabsecurity@proton.me>

iucv_send_ctrl() allocates with sock_alloc_send_skb(), which fails once
SEND_SHUTDOWN is set, so it clears the bit first and writes a copy of
sk_shutdown back afterwards. recvmsg() calls it for a window update and
the NETDEV_GOING_DOWN notifier for a FIN, both without the socket lock.
On a socket that has shut down sending:

- A shutdown(SHUT_RD) done in between is undone when the copy is
  written back: the call returned 0 and the socket still receives.
- sendmsg() can find SEND_SHUTDOWN clear and send data after the SHT of
  a shutdown(SHUT_WR) that already returned.

Allocate the control frame with sock_wmalloc() behind the test of
sk_sndbuf that sock_alloc_send_skb() makes, and leave sk_shutdown alone.

Fixes: 4e0ad3221691 ("Subject: [PATCH] af_iucv: enable control sends in case of SEND_SHUTDOWN")
Cc: stable@vger.kernel.org # 6.18.x
Signed-off-by: Bryam Vargas <hexlabsecurity@proton.me>
---
New in v4. iucv_send_ctrl() clearing SEND_SHUTDOWN is what the AI
review of v3 3/7 is about ([4] in the cover letter), what the notes of
v3 2/7 and 7/7 left open, and where my copy of Hidayath's sk_shutdown
test loses its bit. v2 rewrote the function and v3 dropped that on
Alexandra's review. This is only the part about sk_shutdown; the
allocation that ignores sk_sndbuf for a FIN and the first SHT stays out
(cover letter, "Not in v4"). It goes ahead of 3/8 and 4/8: the test 3/8
adds and the wait 4/8 moves out of the socket lock both rely on the bit
staying set.

The first item, in the emulated setup: a task reads on a socket shut
down for sending, with SO_MSGLIMIT 2 so that nearly every read sends a
window update, and another calls shutdown(SHUT_RD). Over four runs of
200 cycles poll() then shows no EPOLLRDHUP in 12 to 57 cycles of a run
on net and with 1/8, and in none from this patch on.

The wake-up the AI review describes is the one for the memory wait of
4/8, and missing it takes two waits that overlap, so a test kernel on
top of the series has them: the writer sleeps 1000 ms between releasing
the socket and its wait for send memory, a window update waits 1500 ms
in iucv_send_ctrl() ahead of its allocation, and the sk_state_change()
that recvmsg() calls when its window update finds no room is taken out,
as it would wake the writer. A writer blocks with the send memory full
and SO_SNDTIMEO 8 s, shutdown(SHUT_WR) follows at 200 ms, and at 300 ms
a recv() that owes a window update. The writer returns -EPIPE after
1803 ms, when the reader lets go of the socket lock. With the clear and
the restore put back around that delay it sleeps until its timeout,
9193 ms. Without the reader the writer returns on both kernels when the
1000 ms are over.

By inspection:
- the second item of the message
- poll() on a socket shut down both ways reported no EPOLLHUP while
  the bit was clear
- sock_alloc_send_skb() also returned and cleared a pending sk_err,
  which af_iucv sets only at the end of close(), after its FIN
- SOCK_NOSPACE is no longer set; SOCKWQ_ASYNC_NOSPACE still is, for a
  socket with O_ASYNC
- I have no KCSAN build: of Hidayath's reports on net-next, the 6
  between afiucv_hs_rcv() and iucv_send_ctrl() and the 2 between
  iucv_send_ctrl() and iucv_sock_shutdown() are on these writes
---
 net/iucv/af_iucv.c | 27 +++++++++++++--------------
 1 file changed, 13 insertions(+), 14 deletions(-)

diff --git a/net/iucv/af_iucv.c b/net/iucv/af_iucv.c
index daed2a3cfd66..92468293f7c1 100644
--- a/net/iucv/af_iucv.c
+++ b/net/iucv/af_iucv.c
@@ -401,26 +401,25 @@ static void iucv_sever_path(struct sock *sk, int with_user_data)
 static int iucv_send_ctrl(struct sock *sk, u8 flags)
 {
 	struct iucv_sock *iucv = iucv_sk(sk);
-	int err = 0;
 	int blen;
 	struct sk_buff *skb;
-	u8 shutdown = 0;
 
 	blen = sizeof(struct af_iucv_trans_hdr) +
 	       LL_RESERVED_SPACE(iucv->hs_dev);
-	if (sk->sk_shutdown & SEND_SHUTDOWN) {
-		/* controlling flags should be sent anyway */
-		shutdown = sk->sk_shutdown;
-		sk->sk_shutdown &= RCV_SHUTDOWN;
-	}
-	skb = sock_alloc_send_skb(sk, blen, 1, &err);
-	if (skb) {
-		skb_reserve(skb, blen);
-		err = afiucv_hs_send(NULL, sk, skb, flags);
+	/* Control frames go out after a shutdown too, so this is not
+	 * sock_alloc_send_skb(), which fails on SEND_SHUTDOWN. sk_shutdown is
+	 * left alone: it is read without the socket lock, and not every
+	 * caller here holds it.
+	 */
+	if (sk_wmem_alloc_get(sk) >= READ_ONCE(sk->sk_sndbuf)) {
+		sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
+		return -EAGAIN;
 	}
-	if (shutdown)
-		sk->sk_shutdown = shutdown;
-	return err;
+	skb = sock_wmalloc(sk, blen, 1, sk->sk_allocation);
+	if (!skb)
+		return -ENOBUFS;
+	skb_reserve(skb, blen);
+	return afiucv_hs_send(NULL, sk, skb, flags);
 }
 
 /**

-- 
2.56.0



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

* [PATCH RFC net v4 3/8] net/iucv: fail sendmsg() with EPIPE once sending is shut down
  2026-11-15  1:29 [PATCH RFC net v4 0/8] net/iucv: serialize the HiperSockets receive path with the socket lock Bryam Vargas via B4 Relay
  2026-11-15  1:29 ` [PATCH RFC net v4 1/8] net/iucv: keep the window given to the peer in a field of its own Bryam Vargas via B4 Relay
  2026-11-15  1:29 ` [PATCH RFC net v4 2/8] net/iucv: do not clear SEND_SHUTDOWN to send a control frame Bryam Vargas via B4 Relay
@ 2026-11-15  1:29 ` Bryam Vargas via B4 Relay
  2026-11-16  1:29   ` sashiko-bot
  2026-11-15  1:29 ` [PATCH RFC net v4 4/8] net/iucv: run recvmsg() under the socket lock Bryam Vargas via B4 Relay
                   ` (4 subsequent siblings)
  7 siblings, 1 reply; 17+ messages in thread
From: Bryam Vargas via B4 Relay @ 2026-11-15  1:29 UTC (permalink / raw)
  To: Jakub Kicinski, Alexandra Winter, Paolo Abeni, David S. Miller,
	Hidayath Khan, Eric Dumazet
  Cc: Thorsten Winkler, Simon Horman, netdev, linux-kernel, Nagamani PV,
	linux-s390

From: Bryam Vargas <hexlabsecurity@proton.me>

sendmsg() tests SEND_SHUTDOWN on entry only, then waits for the peer's
window without the socket lock. When sending is shut down during that
wait, by shutdown(SHUT_WR) in another thread or by the peer's quiesce
over z/VM, the wait does not end and the call does not fail: the writer
waits out SO_SNDTIMEO, or, when the window opens first, sends its
record after the socket's SHT and the peer reads data after EOF. With
the peer's FIN in as well it returns -ECONNRESET for a stream its own
socket shut down.

End the wait when sending is shut down and test SEND_SHUTDOWN again
after it, ahead of the sk_state test, so the call fails with -EPIPE as
it does on entry.

Fixes: 0ea920d211e0 ("af_iucv: Return -EAGAIN if iucv msg limit is exceeded")
Cc: stable@vger.kernel.org # 6.18.x
Signed-off-by: Bryam Vargas <hexlabsecurity@proton.me>
---
v4: unchanged, was 2/7. What v3's note left open here, a caller of
iucv_send_ctrl() without the socket lock hiding SEND_SHUTDOWN from the
new test, is closed by 2/8.
v3: https://lore.kernel.org/netdev/20261110-b4-disp-2a6b4364-v3-2-35677b68e697@proton.me/

It goes before 4/8, which lets sendmsg() wait for send memory without
the socket lock and would otherwise open the same gap a second time.

Three of the 53 default tests are about this, and net fails all three.
A writer parked on a full window sends its record after the socket's
own SHT when the window opens, so the peer reads data after EOF; it
waits out SO_SNDTIMEO when the window stays shut; and with the peer's
FIN in as well it gets -ECONNRESET. With this patch the first and the
third pass and the second returns -EPIPE; that test still fails, as its
SHT finds no room in sk_sndbuf (one of the five in the cover letter).
With only the wait changed the writer sends after the shutdown in the
first two and gets -ECONNRESET in the third; with only the test after
it, it waits out SO_SNDTIMEO again; with the test placed after the
state test it gets -ECONNRESET again.
---
 net/iucv/af_iucv.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/net/iucv/af_iucv.c b/net/iucv/af_iucv.c
index 92468293f7c1..d2d47f33d08b 100644
--- a/net/iucv/af_iucv.c
+++ b/net/iucv/af_iucv.c
@@ -1064,10 +1064,16 @@ static int iucv_sock_sendmsg(struct socket *sock, struct msghdr *msg,
 
 	/* wait if outstanding messages for iucv path has reached */
 	timeo = sock_sndtimeo(sk, noblock);
-	err = iucv_sock_wait(sk, iucv_below_msglim(sk), timeo);
+	err = iucv_sock_wait(sk, iucv_below_msglim(sk) ||
+			     (sk->sk_shutdown & SEND_SHUTDOWN), timeo);
 	if (err)
 		goto fail;
 
+	/* return -EPIPE if sending has been shut down meanwhile */
+	if (sk->sk_shutdown & SEND_SHUTDOWN) {
+		err = -EPIPE;
+		goto fail;
+	}
 	/* return -ECONNRESET if the socket is no longer connected */
 	if (sk->sk_state != IUCV_CONNECTED) {
 		err = -ECONNRESET;

-- 
2.56.0



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

* [PATCH RFC net v4 4/8] net/iucv: run recvmsg() under the socket lock
  2026-11-15  1:29 [PATCH RFC net v4 0/8] net/iucv: serialize the HiperSockets receive path with the socket lock Bryam Vargas via B4 Relay
                   ` (2 preceding siblings ...)
  2026-11-15  1:29 ` [PATCH RFC net v4 3/8] net/iucv: fail sendmsg() with EPIPE once sending is shut down Bryam Vargas via B4 Relay
@ 2026-11-15  1:29 ` Bryam Vargas via B4 Relay
  2026-11-16  1:29   ` sashiko-bot
  2026-11-15  1:29 ` [PATCH RFC net v4 5/8] net/iucv: factor out the HiperSockets frame dispatch and state changes Bryam Vargas via B4 Relay
                   ` (3 subsequent siblings)
  7 siblings, 1 reply; 17+ messages in thread
From: Bryam Vargas via B4 Relay @ 2026-11-15  1:29 UTC (permalink / raw)
  To: Jakub Kicinski, Alexandra Winter, Paolo Abeni, David S. Miller,
	Hidayath Khan, Eric Dumazet
  Cc: Thorsten Winkler, Simon Horman, netdev, linux-kernel, Nagamani PV,
	linux-s390

From: Bryam Vargas <hexlabsecurity@proton.me>

recvmsg() takes no socket lock, on either transport:

- Two threads reading one socket get its data out of order.
- A read that finds the peer's SHT sets RCV_SHUTDOWN with a plain
  read-modify-write of sk_shutdown, and can lose the SEND_SHUTDOWN that
  a shutdown(SHUT_WR) set meanwhile.
- A MSG_PEEK read leaves its skb on the receive queue and holds a
  reference to it. When a second reader consumes the skb meanwhile,
  that reference is the last one and skb_unref() frees nothing: the skb
  and its receive memory leak, and iucv_sock_destruct() warns.

All three need only a second thread at the wrong moment. No
memory-safety report is known from any of them.

Take lock_sock() in recvmsg(). It sleeps for data without the lock, in
sk_wait_event(), so a reader with nothing to read keeps no other call
on the socket waiting.

sendmsg() now waits for send memory without the socket lock as well:
recvmsg() needs that lock, and over z/VM the memory comes back only when
the peer reads, which may wait for our own reader. That wait is one more
point at which shutdown(SHUT_WR) can find the send memory used up:
iucv_send_ctrl() then has no room for the SHT and the peer reads no EOF,
as before this patch whenever the memory is used up when shutdown()
runs. The writer itself returns -EPIPE: iucv_send_ctrl() does not
clear SEND_SHUTDOWN, so the wait cannot miss it.

Fixes: eac3731bd04c ("[S390]: Add AF_IUCV socket support")
Suggested-by: Alexandra Winter <wintera@linux.ibm.com>
Link: https://lore.kernel.org/all/20260724222917.134769-1-hexlabsecurity@proton.me/
Cc: stable@vger.kernel.org # 6.18.x: aa5ec571b93f: net/iucv: Add lock nesting annotation in iucv_sock_close()
Cc: stable@vger.kernel.org # 6.18.x
Signed-off-by: Bryam Vargas <hexlabsecurity@proton.me>
---
v4: no code change, was 3/7. Two changes to the message, both for what
2/8 does under this patch:
- the second item was the sk_shutdown bit lost around a window update.
  That is 2/8's now; what is left here is recvmsg()'s own write of
  RCV_SHUTDOWN
- the last paragraph says what the writer gets when shutdown(SHUT_WR)
  runs during its memory wait. v3's note had that wait missing the
  wake-up, by inspection (AI review of v3, [4] in the cover letter)
v3: new as a patch of its own, the half of v2's lock patch that does
not touch the softirq side. Without 6/8 the softirq still changes
sk_state under a task that owns the socket, as on net.
v3: https://lore.kernel.org/netdev/20261110-b4-disp-2a6b4364-v3-3-35677b68e697@proton.me/

In the emulated setup: two readers on one socket under MTTCG, three runs
of 838 messages each. On net, on the kernel with 1/8 and 2/8 and on the
one with 1/8 to 3/8 every run has reads that go backwards, 9 to 20 of
them; on the one with 1/8 to 4/8 and with the series there are none.

The second item is by inspection. The test that loses the bit on net
(notes of 2/8) loses none from 2/8 on, and I have no test where the
peer's SHT is read while shutdown(SHUT_WR) runs.

Repro of the MSG_PEEK item: one process is parked in the copy of a
recv(MSG_PEEK), its buffer a userfaultfd page, and a second one reads
the same message. On net and on the kernels with 1/8, with 1/8 and 2/8
and with 1/8 to 3/8 the second read returns the byte while the first is
still parked; once both are back the socket has receive memory charged
and nothing to read, and iucv_sock_destruct() takes its WARN_ON() at
close. From 4/8 on the second reader waits for the first, then reads the
byte, and nothing stays charged. With the two reads one after the other
every kernel is clean.

sendmsg()'s memory wait: with it left under the socket lock, a
non-blocking read is held up for as long as a writer waits for send
memory, 5830 ms in the test.

shutdown(SHUT_WR) during that wait: a writer blocks with the send memory
full and SO_SNDTIMEO 8 s, and shutdown(SHUT_WR) is called 200 ms later.
On net and on the kernels with 1/8, with 1/8 and 2/8 and with 1/8 to 3/8
the shutdown() waits for the socket lock until the writer's timeout,
8028 to 8445 ms, and the writer returns -EAGAIN. With 1/8 to 4/8 and
with the series shutdown() returns at once and the writer
returns -EPIPE, 202 ms after it blocked. The case where the wake-up
could be missed is under 2/8.

The prerequisite on the stable line, aa5ec571b93f, brings
__iucv_sock_close(), which recvmsg() now calls with the lock held where
it used to close the socket on a peer that overran its window.

Hidayath's 19 KCSAN reports of recvmsg() against shutdown() on net-next
are what this patch should end; the READ_ONCE() in the receive wait
against shutdown()'s plain write stays reportable until 7/8 (cover
letter).
---
 net/iucv/af_iucv.c | 94 ++++++++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 78 insertions(+), 16 deletions(-)

diff --git a/net/iucv/af_iucv.c b/net/iucv/af_iucv.c
index d2d47f33d08b..80eb90b1ef3c 100644
--- a/net/iucv/af_iucv.c
+++ b/net/iucv/af_iucv.c
@@ -1050,7 +1050,17 @@ static int iucv_sock_sendmsg(struct socket *sock, struct msghdr *msg,
 		}
 	}
 	skb = sock_alloc_send_pskb(sk, headroom + linear, len - linear,
-				   noblock, &err, 0);
+				   1, &err, 0);
+	if (!skb && err == -EAGAIN && !noblock) {
+		/* Wait without the socket lock: recvmsg() takes it, and over
+		 * z/VM the memory comes back only when the peer reads, which
+		 * may wait for our own reader.
+		 */
+		release_sock(sk);
+		skb = sock_alloc_send_pskb(sk, headroom + linear,
+					   len - linear, 0, &err, 0);
+		lock_sock(sk);
+	}
 	if (!skb)
 		goto out;
 	if (headroom)
@@ -1277,6 +1287,54 @@ static void iucv_process_message_q(struct sock *sk)
 	}
 }
 
+/**
+ * iucv_sock_recv_skb() - get the next skb for recvmsg(), waiting for one
+ * @sk:		the socket, locked by the caller
+ * @flags:	recvmsg() flags; MSG_PEEK leaves the skb queued and returns
+ *		it with a reference
+ * @err:	set when no skb is returned
+ *
+ * skb_recv_datagram() would sleep with the socket lock held. This sleeps in
+ * sk_wait_event(), which drops it.
+ *
+ * Context: Process context, socket lock held; sleeps with it released.
+ * Return: the skb, or NULL with @err set. At RCV_SHUTDOWN the caller
+ * returns EOF whatever @err is.
+ */
+static struct sk_buff *iucv_sock_recv_skb(struct sock *sk, int flags, int *err)
+{
+	long timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
+	struct sk_buff_head *queue = &sk->sk_receive_queue;
+	DEFINE_WAIT_FUNC(wait, woken_wake_function);
+	struct sk_buff *skb;
+
+	for (;;) {
+		skb = skb_recv_datagram(sk, flags | MSG_DONTWAIT, err);
+		if (skb || *err != -EAGAIN)
+			break;
+		if (sk->sk_shutdown & RCV_SHUTDOWN)
+			break;
+		if (!timeo)
+			break;
+		if (sk->sk_state != IUCV_CONNECTED) {
+			*err = -ENOTCONN;
+			break;
+		}
+		if (signal_pending(current)) {
+			*err = sock_intr_errno(timeo);
+			break;
+		}
+		add_wait_queue(sk_sleep(sk), &wait);
+		sk_wait_event(sk, &timeo,
+			      !skb_queue_empty_lockless(queue) ||
+			      READ_ONCE(sk->sk_err) ||
+			      (READ_ONCE(sk->sk_shutdown) & RCV_SHUTDOWN) ||
+			      sk->sk_state != IUCV_CONNECTED, &wait);
+		remove_wait_queue(sk_sleep(sk), &wait);
+	}
+	return skb;
+}
+
 static int iucv_sock_recvmsg(struct socket *sock, struct msghdr *msg,
 			     size_t len, int flags)
 {
@@ -1288,24 +1346,25 @@ static int iucv_sock_recvmsg(struct socket *sock, struct msghdr *msg,
 	int err = 0;
 	u32 offset;
 
+	lock_sock(sk);
 	if ((sk->sk_state == IUCV_DISCONN) &&
 	    skb_queue_empty(&iucv->backlog_skb_q) &&
 	    skb_queue_empty(&sk->sk_receive_queue) &&
-	    list_empty(&iucv->message_q.list))
-		return 0;
+	    list_empty(&iucv->message_q.list)) {
+		copied = 0;
+		goto unlock;
+	}
 
-	if (flags & (MSG_OOB))
-		return -EOPNOTSUPP;
+	if (flags & (MSG_OOB)) {
+		err = -EOPNOTSUPP;
+		goto err_unlock;
+	}
 
-	/* receive/dequeue next skb:
-	 * the function understands MSG_PEEK and, thus, does not dequeue skb
-	 * only refcount is increased.
-	 */
-	skb = skb_recv_datagram(sk, flags, &err);
+	skb = iucv_sock_recv_skb(sk, flags, &err);
 	if (!skb) {
 		if (sk->sk_shutdown & RCV_SHUTDOWN)
-			return 0;
-		return err;
+			err = 0;
+		goto err_unlock;
 	}
 
 	offset = IUCV_SKB_CB(skb)->offset;
@@ -1355,8 +1414,9 @@ static int iucv_sock_recvmsg(struct socket *sock, struct msghdr *msg,
 			if (atomic_read(&iucv->msg_recv) >
 			    afiucv_hs_window(iucv)) {
 				WARN_ON(1);
-				iucv_sock_close(sk);
-				return -EFAULT;
+				__iucv_sock_close(sk);
+				err = -EFAULT;
+				goto err_unlock;
 			}
 		}
 
@@ -1398,7 +1458,8 @@ static int iucv_sock_recvmsg(struct socket *sock, struct msghdr *msg,
 		copied = rlen;
 	if (flags & MSG_PEEK)
 		skb_unref(skb);
-
+unlock:
+	release_sock(sk);
 	return copied;
 
 err_out:
@@ -1406,7 +1467,8 @@ static int iucv_sock_recvmsg(struct socket *sock, struct msghdr *msg,
 		skb_queue_head(&sk->sk_receive_queue, skb);
 	else
 		skb_unref(skb);
-
+err_unlock:
+	release_sock(sk);
 	return err;
 }
 

-- 
2.56.0



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

* [PATCH RFC net v4 5/8] net/iucv: factor out the HiperSockets frame dispatch and state changes
  2026-11-15  1:29 [PATCH RFC net v4 0/8] net/iucv: serialize the HiperSockets receive path with the socket lock Bryam Vargas via B4 Relay
                   ` (3 preceding siblings ...)
  2026-11-15  1:29 ` [PATCH RFC net v4 4/8] net/iucv: run recvmsg() under the socket lock Bryam Vargas via B4 Relay
@ 2026-11-15  1:29 ` Bryam Vargas via B4 Relay
  2026-11-16  1:29   ` sashiko-bot
  2026-11-15  1:29 ` [PATCH RFC net v4 6/8] net/iucv: serialize HiperSockets receive with the socket lock Bryam Vargas via B4 Relay
                   ` (2 subsequent siblings)
  7 siblings, 1 reply; 17+ messages in thread
From: Bryam Vargas via B4 Relay @ 2026-11-15  1:29 UTC (permalink / raw)
  To: Jakub Kicinski, Alexandra Winter, Paolo Abeni, David S. Miller,
	Hidayath Khan, Eric Dumazet
  Cc: Thorsten Winkler, Simon Horman, netdev, linux-kernel, Nagamani PV,
	linux-s390

From: Bryam Vargas <hexlabsecurity@proton.me>

Move the switch on the frame type out of afiucv_hs_rcv() into
afiucv_hs_dispatch(), and the state changes a SYN|ACK, a SYN|FIN and a
FIN make into afiucv_hs_synack(), afiucv_hs_synfin() and
afiucv_hs_fin(). The three are still called under the bh_lock_sock() of
their callbacks, which now test sk where they tested iucv_sk(sk), a
cast of it. They are defined ahead of iucv_proto: the next patch
reaches them from proto->release_cb. afiucv_hs_synfin() and
afiucv_hs_fin() are the same for now and stay two: a later patch sets
RCV_SHUTDOWN in the second only.

No functional change. The next patch runs the dispatch from a second
place and the three state changes from the task that owns the socket.

Cc: stable@vger.kernel.org # 6.18.x, needed by the next patch
Signed-off-by: Bryam Vargas <hexlabsecurity@proton.me>
---
v4: unchanged, was 4/7.
v3: https://lore.kernel.org/netdev/20261110-b4-disp-2a6b4364-v3-4-35677b68e697@proton.me/

checkpatch asks for a Fixes: tag because of the stable line. There is
none to give: the patch fixes nothing and is marked for stable only so
that 6/8 applies there.

The kernel with 1/8 to 5/8 gives the result of the one with 1/8 to 4/8
in every test: the 53 default ones and the 28 opt-in ones (window,
strays, connect(), leaks).
---
 net/iucv/af_iucv.c | 151 ++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 97 insertions(+), 54 deletions(-)

diff --git a/net/iucv/af_iucv.c b/net/iucv/af_iucv.c
index 80eb90b1ef3c..957315bec2b4 100644
--- a/net/iucv/af_iucv.c
+++ b/net/iucv/af_iucv.c
@@ -39,6 +39,44 @@
 
 static char iucv_userid[80];
 
+/**
+ * afiucv_hs_synack() - connect a socket, on the SYN|ACK to its SYN
+ * @sk:		the socket, in IUCV_BOUND
+ * @window:	the window in the SYN|ACK: the frames the peer takes from us
+ *
+ * Context: Softirq, under bh_lock_sock().
+ */
+static void afiucv_hs_synack(struct sock *sk, u16 window)
+{
+	iucv_sk(sk)->msglimit_peer = window;
+	sk->sk_state = IUCV_CONNECTED;
+	sk->sk_state_change(sk);
+}
+
+/**
+ * afiucv_hs_synfin() - refuse a socket, on the SYN|FIN to its SYN
+ * @sk:		the socket, in IUCV_BOUND
+ *
+ * Context: As afiucv_hs_synack().
+ */
+static void afiucv_hs_synfin(struct sock *sk)
+{
+	sk->sk_state = IUCV_DISCONN;
+	sk->sk_state_change(sk);
+}
+
+/**
+ * afiucv_hs_fin() - disconnect a socket, on the FIN of its peer
+ * @sk:		the socket, in IUCV_CONNECTED
+ *
+ * Context: As afiucv_hs_synack().
+ */
+static void afiucv_hs_fin(struct sock *sk)
+{
+	sk->sk_state = IUCV_DISCONN;
+	sk->sk_state_change(sk);
+}
+
 static struct proto iucv_proto = {
 	.name		= "AF_IUCV",
 	.owner		= THIS_MODULE,
@@ -2041,17 +2079,13 @@ static int afiucv_hs_callback_syn(struct sock *sk, struct sk_buff *skb)
  */
 static int afiucv_hs_callback_synack(struct sock *sk, struct sk_buff *skb)
 {
-	struct iucv_sock *iucv = iucv_sk(sk);
-
-	if (!iucv || sk->sk_state != IUCV_BOUND) {
+	if (!sk || sk->sk_state != IUCV_BOUND) {
 		kfree_skb(skb);
 		return NET_RX_SUCCESS;
 	}
 
 	bh_lock_sock(sk);
-	iucv->msglimit_peer = iucv_trans_hdr(skb)->window;
-	sk->sk_state = IUCV_CONNECTED;
-	sk->sk_state_change(sk);
+	afiucv_hs_synack(sk, iucv_trans_hdr(skb)->window);
 	bh_unlock_sock(sk);
 	consume_skb(skb);
 	return NET_RX_SUCCESS;
@@ -2062,16 +2096,13 @@ static int afiucv_hs_callback_synack(struct sock *sk, struct sk_buff *skb)
  */
 static int afiucv_hs_callback_synfin(struct sock *sk, struct sk_buff *skb)
 {
-	struct iucv_sock *iucv = iucv_sk(sk);
-
-	if (!iucv || sk->sk_state != IUCV_BOUND) {
+	if (!sk || sk->sk_state != IUCV_BOUND) {
 		kfree_skb(skb);
 		return NET_RX_SUCCESS;
 	}
 
 	bh_lock_sock(sk);
-	sk->sk_state = IUCV_DISCONN;
-	sk->sk_state_change(sk);
+	afiucv_hs_synfin(sk);
 	bh_unlock_sock(sk);
 	consume_skb(skb);
 	return NET_RX_SUCCESS;
@@ -2082,19 +2113,15 @@ static int afiucv_hs_callback_synfin(struct sock *sk, struct sk_buff *skb)
  */
 static int afiucv_hs_callback_fin(struct sock *sk, struct sk_buff *skb)
 {
-	struct iucv_sock *iucv = iucv_sk(sk);
-
 	/* other end of connection closed */
-	if (!iucv) {
+	if (!sk) {
 		kfree_skb(skb);
 		return NET_RX_SUCCESS;
 	}
 
 	bh_lock_sock(sk);
-	if (sk->sk_state == IUCV_CONNECTED) {
-		sk->sk_state = IUCV_DISCONN;
-		sk->sk_state_change(sk);
-	}
+	if (sk->sk_state == IUCV_CONNECTED)
+		afiucv_hs_fin(sk);
 	bh_unlock_sock(sk);
 	consume_skb(skb);
 	return NET_RX_SUCCESS;
@@ -2162,6 +2189,57 @@ static int afiucv_hs_callback_rx(struct sock *sk, struct sk_buff *skb)
 	return NET_RX_SUCCESS;
 }
 
+/**
+ * afiucv_hs_dispatch() - run the callback of a frame
+ * @sk:		the socket the frame is for, or NULL if there is none
+ * @skb:	the frame
+ *
+ * Context: Softirq.
+ * Return: NET_RX_SUCCESS.
+ */
+static int afiucv_hs_dispatch(struct sock *sk, struct sk_buff *skb)
+{
+	struct af_iucv_trans_hdr *trans_hdr = iucv_trans_hdr(skb);
+	int err = NET_RX_SUCCESS;
+
+	switch (trans_hdr->flags) {
+	case AF_IUCV_FLAG_SYN:
+		/* connect request */
+		err = afiucv_hs_callback_syn(sk, skb);
+		break;
+	case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK):
+		/* connect request confirmed */
+		err = afiucv_hs_callback_synack(sk, skb);
+		break;
+	case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN):
+		/* connect request refused */
+		err = afiucv_hs_callback_synfin(sk, skb);
+		break;
+	case (AF_IUCV_FLAG_FIN):
+		/* close request */
+		err = afiucv_hs_callback_fin(sk, skb);
+		break;
+	case (AF_IUCV_FLAG_WIN):
+		err = afiucv_hs_callback_win(sk, skb);
+		if (skb->len == sizeof(struct af_iucv_trans_hdr)) {
+			consume_skb(skb);
+			break;
+		}
+		fallthrough;	/* and receive non-zero length data */
+	case (AF_IUCV_FLAG_SHT):
+		/* shutdown request */
+		fallthrough;	/* and receive zero length data */
+	case 0:
+		/* plain data frame */
+		IUCV_SKB_CB(skb)->class = trans_hdr->iucv_hdr.class;
+		err = afiucv_hs_callback_rx(sk, skb);
+		break;
+	default:
+		kfree_skb(skb);
+	}
+	return err;
+}
+
 /*
  * afiucv_hs_rcv() - base function for arriving data through HiperSockets
  *                   transport
@@ -2242,42 +2320,7 @@ static int afiucv_hs_rcv(struct sk_buff *skb, struct net_device *dev,
 	 data -> send FIN
 	 SYN|ACK, SYN|FIN, FIN -> no action? */
 
-	switch (trans_hdr->flags) {
-	case AF_IUCV_FLAG_SYN:
-		/* connect request */
-		err = afiucv_hs_callback_syn(sk, skb);
-		break;
-	case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK):
-		/* connect request confirmed */
-		err = afiucv_hs_callback_synack(sk, skb);
-		break;
-	case (AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN):
-		/* connect request refused */
-		err = afiucv_hs_callback_synfin(sk, skb);
-		break;
-	case (AF_IUCV_FLAG_FIN):
-		/* close request */
-		err = afiucv_hs_callback_fin(sk, skb);
-		break;
-	case (AF_IUCV_FLAG_WIN):
-		err = afiucv_hs_callback_win(sk, skb);
-		if (skb->len == sizeof(struct af_iucv_trans_hdr)) {
-			consume_skb(skb);
-			break;
-		}
-		fallthrough;	/* and receive non-zero length data */
-	case (AF_IUCV_FLAG_SHT):
-		/* shutdown request */
-		fallthrough;	/* and receive zero length data */
-	case 0:
-		/* plain data frame */
-		IUCV_SKB_CB(skb)->class = trans_hdr->iucv_hdr.class;
-		err = afiucv_hs_callback_rx(sk, skb);
-		break;
-	default:
-		kfree_skb(skb);
-	}
-
+	err = afiucv_hs_dispatch(sk, skb);
 	if (sk)
 		sock_put(sk);
 	return err;

-- 
2.56.0



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

* [PATCH RFC net v4 6/8] net/iucv: serialize HiperSockets receive with the socket lock
  2026-11-15  1:29 [PATCH RFC net v4 0/8] net/iucv: serialize the HiperSockets receive path with the socket lock Bryam Vargas via B4 Relay
                   ` (4 preceding siblings ...)
  2026-11-15  1:29 ` [PATCH RFC net v4 5/8] net/iucv: factor out the HiperSockets frame dispatch and state changes Bryam Vargas via B4 Relay
@ 2026-11-15  1:29 ` Bryam Vargas via B4 Relay
  2026-11-16  1:29   ` sashiko-bot
  2026-11-15  1:29 ` [PATCH RFC net v4 7/8] net/iucv: set RCV_SHUTDOWN under the socket spinlock Bryam Vargas via B4 Relay
  2026-11-15  1:29 ` [PATCH RFC net v4 8/8] net/iucv: return EOF to a reader parked when the peer closes Bryam Vargas via B4 Relay
  7 siblings, 1 reply; 17+ messages in thread
From: Bryam Vargas via B4 Relay @ 2026-11-15  1:29 UTC (permalink / raw)
  To: Jakub Kicinski, Alexandra Winter, Paolo Abeni, David S. Miller,
	Hidayath Khan, Eric Dumazet
  Cc: Thorsten Winkler, Simon Horman, netdev, linux-kernel, Nagamani PV,
	linux-s390

From: Bryam Vargas <hexlabsecurity@proton.me>

The HiperSockets receive callbacks change sk_state under bh_lock_sock()
without testing sock_owned_by_user(). A SYN|ACK, SYN|FIN or FIN that
arrives during a system call on the socket is applied under the task
that holds lock_sock(), which goes on with the state it read. A SYN is
answered for a listener that close() is emptying: the child is left on
the accept queue of a socket that is then freed, and nothing can close
it. It keeps the listener's name, so bind() to that name fails with
-EADDRINUSE from then on, and it keeps its reference on the net device.

Either needs only a frame from the peer at the wrong moment. No
memory-safety report is known.

Take bh_lock_sock() once in afiucv_hs_rcv(), for every frame that finds
a socket. While a task owns it:

- A SYN|ACK, SYN|FIN or FIN is recorded in the socket and applied by
  the owner from proto->release_cb. It is not queued: nothing resends a
  control frame, and the socket backlog can refuse one under memory
  pressure, since it does not take a pfmemalloc skb.
  connect() applies a recorded reply before it tests the state, so a
  non-blocking connect() still sees a reply that is already in.
- Data, SHT and WIN frames do not change sk_state and are handled on
  arrival, as before. They wait in the socket backlog only while the
  reply to our SYN is recorded and not applied yet, and behind frames
  that wait already, up to the window given to the peer plus one.
- A SYN for a listener waits in the backlog, cut to its header, up to
  the accept queue plus one. Past that, or if the listener closed or
  its queue filled meanwhile, it is refused without a child.

A frame that no state of the socket can use is freed in afiucv_hs_rcv()
and such a SYN refused there: a SYN|ACK or SYN|FIN outside IUCV_BOUND,
a FIN for a socket neither connected nor about to be, and data, SHT and
WIN frames behind a recorded FIN. A SYN for a socket that does not
listen was refused only after a child had been allocated for it, and
that child was never freed; it is now refused without one. A listener
whose accept queue is full still leaks the child when no task owns it,
as before.

Fixes: 3881ac441f64 ("af_iucv: add HiperSockets transport")
Suggested-by: Alexandra Winter <wintera@linux.ibm.com>
Link: https://lore.kernel.org/all/20260724222917.134769-1-hexlabsecurity@proton.me/
Cc: stable@vger.kernel.org # 6.18.x: f38bce9af9e2: net/iucv: take a private, writable frame before rewriting it in place
Cc: stable@vger.kernel.org # 6.18.x
Signed-off-by: Bryam Vargas <hexlabsecurity@proton.me>
---
v4:
- the SYN that waits in the backlog holds its device with netdev_hold()
  and netdev_put(), the tracker in the skb's cb; v3 had dev_hold() and
  dev_put() there (the deprecated_api check of the netdev CI)
v3:
- only the softirq side of the lock is left here; the other patches
  took the rest. Up to this patch the parked reader still gets
  -ENOTCONN at the peer's FIN, as on net; 8/8 changes that
- afiucv_hs_stray() calls a FIN a stray unless the socket is
  IUCV_CONNECTED or a recorded SYN|ACK is about to connect it. v2
  recorded a FIN that reached a socket in IUCV_BOUND and applied it
  after the SYN|ACK (AI review of v2, first item)
- data, SHT and WIN frames behind a recorded FIN are strays too, as
  afiucv_hs_callback_rx() frees them once the FIN is applied. v2 handled
  them as connected (second item)
- connect() applies a recorded reply to its SYN before it tests the
  state. v1 and v2 left the reply for release_sock(): a non-blocking
  connect() returned -EAGAIN with the SYN|ACK, or the SYN|FIN, already
  in, where net returns 0 or -ECONNREFUSED
- kernel-doc on every function added, with its context; the comment on
  afiucv_hs_defer() is rewritten (the reason v2 gave there was stale),
  the one on afiucv_hs_backlog_add() says why frames are counted
- on net, with nothing under it
v3: https://lore.kernel.org/netdev/20261110-b4-disp-2a6b4364-v3-5-35677b68e697@proton.me/
v2: https://lore.kernel.org/netdev/20261022-b4-disp-1d26b87a-v2-1-7ee1264d812a@proton.me/
v1: https://lore.kernel.org/netdev/20261011-b4-disp-410518f4-v1-1-753bf3258292@proton.me/

Repro of the listener in the message's first paragraph. The window is a
few instructions wide, between the last child taken off the accept
queue and the listener's IUCV_CLOSED, so the test kernels have an
mdelay(400) there and a SYN is sent 100 ms into the close(). net with
the delay: connect() returns 0, nothing ever accepts or closes the
child, one socket stays in use after both ends are closed, and a new
bind() to the listener's name fails with -EADDRINUSE. The series with
the same delay: the SYN waits in the listener's backlog, is refused
when the close is done, and connect() returns -ECONNREFUSED; nothing is
left and the name binds again. With the SYN sent before the close()
both kernels free the child. No KASAN splat with the delay.

Strays are freed in afiucv_hs_rcv(): a FIN with consume_skb(), as
afiucv_hs_callback_fin() frees it on net, the rest as NO_SOCKET drops.
With the drop trace on, a second FIN for a socket the first one
disconnected leaves no kfree_skb event, on net and here; the series
with that FIN dropped like the other strays leaves one NO_SOCKET drop.
A data byte sent the same way is a drop on every kernel.

The SYN for a socket that does not listen, measured: 16 connect() calls
to a socket that is bound and does not listen, each refused. On net and
up to 5/8 they leave 16 sockets in use after everything is closed, from
this patch on none. The full accept queue is not fixed: 1056 SYNs
refused by a listener whose queue is full leave 1056 sockets in use, on
net and with the series.

The backlog's budget is the window of 1/8 plus one, so a socket
connected by a SYN|ACK without a SYN of its own counts against msglimit
there too. With the socket held, three one-byte messages behind such a
SYN|ACK are all read after the release. With the budget read from
msglimit_adv alone, one is read and two are SOCKET_BACKLOG drops.

The first two items of the AI review of v2 repro on v2 as sent and are
gone here. Both hold the socket with a task parked in setsockopt(), its
filter a userfaultfd page, and send it one forged frame over the
emulated adapter. First: the socket's SYN is held back, so it is
IUCV_BOUND and no SYN|ACK exists; a FIN arrives, then the SYN is
released and the SYN|ACK comes in. v2 connects and disconnects at the
release; the series frees the FIN as it arrives and a byte goes each way
afterwards. With the FIN sent after the SYN|ACK instead, both apply it.
Second: the peer sends one byte and closes, then a forged data frame
arrives behind its FIN. v2 delivers the forged byte after the real one;
the series frees it and the reader gets EOF. net and v1 behave as the
series in both. v1 queued control frames and ran them in order, so both
came in when v2 began to record them instead. With any FIN recorded
again the first test fails; with frames let through behind a recorded
FIN, the second.

With any one of these pieces taken out, at least one test fails: the
release callback that applies what was recorded, the wait for the
owner, the reply to our SYN applied before the backlog runs, connect()
applying that reply, either half of afiucv_hs_stray()'s rule for a FIN,
its rule for frames behind one, the whole of afiucv_hs_stray(), the
budget read through the window helper, and the stray FIN consumed. Of
the 53 default tests the kernel with 1/8 to 6/8 fails six: the five in
the cover letter and the parked reader's -ENOTCONN, which 8/8 changes.

The device reference of a waiting SYN, on a build with
CONFIG_NET_DEV_REFCNT_TRACKER=y: 600 SYNs wait in the backlog of a
listener that is closing and are refused when it is done, then the
device is removed. With the series the tracker reports nothing and the
device is freed. With the netdev_put() of a refused SYN taken out the
removal waits, "Usage count = 601", and ref_tracker lists 600 users
at afiucv_hs_rcv_owned(). One boot of each, read off the console; that
kernel is a build of its own, not one of the 25 in the cover letter.

By inspection only, no repro: a pfmemalloc skb refused by the backlog
and the SOCK_ZAPPED test in afiucv_hs_apply().
---
 include/net/iucv/af_iucv.h |   7 +
 net/iucv/af_iucv.c         | 338 ++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 322 insertions(+), 23 deletions(-)

diff --git a/include/net/iucv/af_iucv.h b/include/net/iucv/af_iucv.h
index 6decbb1f34b3..a48ec2022661 100644
--- a/include/net/iucv/af_iucv.h
+++ b/include/net/iucv/af_iucv.h
@@ -15,6 +15,7 @@
 #include <linux/list.h>
 #include <linux/poll.h>
 #include <linux/socket.h>
+#include <net/net_trackers.h>
 #include <net/iucv/iucv.h>
 
 #ifndef AF_IUCV
@@ -128,14 +129,19 @@ struct iucv_sock {
 	struct sock_msg_q	message_q;
 	unsigned int		send_tag;
 	u8			flags;
+	u8			hs_pending;	/* AF_IUCV_PEND_*, af_iucv.c */
 	u16			msglimit;
 	u16			msglimit_peer;
 	u16			msglimit_adv;	/* window given to the peer */
+	u16			hs_pending_window;
 	atomic_t		skbs_in_xmit;
 	atomic_t		msg_sent;
 	atomic_t		msg_recv;
 	atomic_t		pendings;
 	int			transport;
+	/* frames in sk_backlog since it was empty */
+	unsigned int		hs_backlog_data;
+	unsigned int		hs_backlog_syn;
 	void			(*sk_txnotify)(struct sock *sk,
 					       enum iucv_tx_notify n);
 };
@@ -144,6 +150,7 @@ struct iucv_skb_cb {
 	u32	class;		/* target class of message */
 	u32	tag;		/* tag associated with message */
 	u32	offset;		/* offset for skb receival */
+	netdevice_tracker	dev_tracker;	/* SYN in the socket backlog */
 };
 
 #define IUCV_SKB_CB(__skb)	((struct iucv_skb_cb *)&((__skb)->cb[0]))
diff --git a/net/iucv/af_iucv.c b/net/iucv/af_iucv.c
index 957315bec2b4..2dd9f12bce62 100644
--- a/net/iucv/af_iucv.c
+++ b/net/iucv/af_iucv.c
@@ -44,7 +44,8 @@ static char iucv_userid[80];
  * @sk:		the socket, in IUCV_BOUND
  * @window:	the window in the SYN|ACK: the frames the peer takes from us
  *
- * Context: Softirq, under bh_lock_sock().
+ * Context: sk_lock.slock held: softirq with no task owning the socket, or
+ * the owner in afiucv_hs_apply().
  */
 static void afiucv_hs_synack(struct sock *sk, u16 window)
 {
@@ -77,10 +78,58 @@ static void afiucv_hs_fin(struct sock *sk)
 	sk->sk_state_change(sk);
 }
 
+/* iucv_sock.hs_pending: control frames recorded for the owner */
+#define AF_IUCV_PEND_SYNACK	0x01
+#define AF_IUCV_PEND_SYNFIN	0x02
+#define AF_IUCV_PEND_SYNREPLY	(AF_IUCV_PEND_SYNACK | AF_IUCV_PEND_SYNFIN)
+#define AF_IUCV_PEND_FIN	0x04
+#define AF_IUCV_PEND_ALL	(AF_IUCV_PEND_SYNREPLY | AF_IUCV_PEND_FIN)
+
+/**
+ * afiucv_hs_apply() - apply the control frames afiucv_hs_record() kept
+ * @sk:		the socket, owned by the caller
+ * @mask:	the AF_IUCV_PEND_* bits to apply
+ *
+ * A socket closed meanwhile takes none. The test is SOCK_ZAPPED, since
+ * __iucv_sock_close() leaves IUCV_BOUND as it is.
+ *
+ * Context: Process context, under sk_lock.slock.
+ */
+static void afiucv_hs_apply(struct sock *sk, u8 mask)
+{
+	struct iucv_sock *iucv = iucv_sk(sk);
+	u8 pending = iucv->hs_pending & mask;
+
+	iucv->hs_pending &= ~pending;
+	if (sock_flag(sk, SOCK_ZAPPED))
+		return;
+	if ((pending & AF_IUCV_PEND_SYNACK) && sk->sk_state == IUCV_BOUND)
+		afiucv_hs_synack(sk, iucv->hs_pending_window);
+	if ((pending & AF_IUCV_PEND_SYNFIN) && sk->sk_state == IUCV_BOUND)
+		afiucv_hs_synfin(sk);
+	if ((pending & AF_IUCV_PEND_FIN) && sk->sk_state == IUCV_CONNECTED)
+		afiucv_hs_fin(sk);
+}
+
+/**
+ * afiucv_hs_release_cb() - proto->release_cb, apply what was recorded
+ * @sk:		the socket, still owned by the caller
+ *
+ * Runs after the backlog, so a recorded FIN follows the data before it.
+ *
+ * Context: Process context, under sk_lock.slock.
+ */
+static void afiucv_hs_release_cb(struct sock *sk)
+{
+	if (iucv_sk(sk)->hs_pending)
+		afiucv_hs_apply(sk, AF_IUCV_PEND_ALL);
+}
+
 static struct proto iucv_proto = {
 	.name		= "AF_IUCV",
 	.owner		= THIS_MODULE,
 	.obj_size	= sizeof(struct iucv_sock),
+	.release_cb	= afiucv_hs_release_cb,
 };
 
 static struct iucv_interface *pr_iucv;
@@ -131,6 +180,7 @@ static void iucv_sock_kill(struct sock *sk);
 static void __iucv_sock_close(struct sock *sk);
 
 static void afiucv_hs_callback_txnotify(struct sock *sk, enum iucv_tx_notify);
+static int afiucv_hs_backlog_rcv(struct sock *sk, struct sk_buff *skb);
 
 static struct iucv_sock_list iucv_sk_list = {
 	.lock = __RW_LOCK_UNLOCKED(iucv_sk_list.lock),
@@ -571,6 +621,8 @@ static struct sock *iucv_sock_alloc(struct socket *sock, int proto, gfp_t prio,
 		iucv->transport = AF_IUCV_TRANS_HIPER;
 
 	sk->sk_destruct = iucv_sock_destruct;
+	/* not from iucv_proto: sk_alloc() does not copy proto->backlog_rcv */
+	sk->sk_backlog_rcv = afiucv_hs_backlog_rcv;
 	sk->sk_sndtimeo = IUCV_CONN_TIMEOUT;
 
 	sock_reset_flag(sk, SOCK_ZAPPED);
@@ -845,6 +897,15 @@ static int iucv_sock_connect(struct socket *sock, struct sockaddr_unsized *addr,
 	if (err)
 		goto done;
 
+	/* The reply may be in already, recorded while we own the socket:
+	 * apply it, or a non-blocking connect() misses it.
+	 */
+	if (iucv->transport == AF_IUCV_TRANS_HIPER) {
+		spin_lock_bh(&sk->sk_lock.slock);
+		afiucv_hs_apply(sk, AF_IUCV_PEND_SYNREPLY);
+		spin_unlock_bh(&sk->sk_lock.slock);
+	}
+
 	if (sk->sk_state != IUCV_CONNECTED)
 		err = iucv_sock_wait(sk, iucv_sock_in_state(sk, IUCV_CONNECTED,
 							    IUCV_DISCONN),
@@ -1333,7 +1394,8 @@ static void iucv_process_message_q(struct sock *sk)
  * @err:	set when no skb is returned
  *
  * skb_recv_datagram() would sleep with the socket lock held. This sleeps in
- * sk_wait_event(), which drops it.
+ * sk_wait_event(), which drops it; a FIN recorded for the owner is applied
+ * then and ends the wait.
  *
  * Context: Process context, socket lock held; sleeps with it released.
  * Return: the skb, or NULL with @err set. At RCV_SHUTDOWN the caller
@@ -2027,7 +2089,6 @@ static int afiucv_hs_callback_syn(struct sock *sk, struct sk_buff *skb)
 	}
 
 	nsk = iucv_sock_alloc(NULL, sk->sk_protocol, GFP_ATOMIC, 0);
-	bh_lock_sock(sk);
 	if ((sk->sk_state != IUCV_LISTEN) ||
 	    sk_acceptq_is_full(sk) ||
 	    !nsk) {
@@ -2037,7 +2098,6 @@ static int afiucv_hs_callback_syn(struct sock *sk, struct sk_buff *skb)
 		err = dev_queue_xmit(skb);
 		if (nsk)
 			iucv_sock_kill(nsk);
-		bh_unlock_sock(sk);
 		goto out;
 	}
 
@@ -2068,7 +2128,6 @@ static int afiucv_hs_callback_syn(struct sock *sk, struct sk_buff *skb)
 		sk->sk_data_ready(sk);
 	} else
 		iucv_sock_kill(nsk);
-	bh_unlock_sock(sk);
 
 out:
 	return NET_RX_SUCCESS;
@@ -2084,9 +2143,7 @@ static int afiucv_hs_callback_synack(struct sock *sk, struct sk_buff *skb)
 		return NET_RX_SUCCESS;
 	}
 
-	bh_lock_sock(sk);
 	afiucv_hs_synack(sk, iucv_trans_hdr(skb)->window);
-	bh_unlock_sock(sk);
 	consume_skb(skb);
 	return NET_RX_SUCCESS;
 }
@@ -2101,9 +2158,7 @@ static int afiucv_hs_callback_synfin(struct sock *sk, struct sk_buff *skb)
 		return NET_RX_SUCCESS;
 	}
 
-	bh_lock_sock(sk);
 	afiucv_hs_synfin(sk);
-	bh_unlock_sock(sk);
 	consume_skb(skb);
 	return NET_RX_SUCCESS;
 }
@@ -2119,10 +2174,8 @@ static int afiucv_hs_callback_fin(struct sock *sk, struct sk_buff *skb)
 		return NET_RX_SUCCESS;
 	}
 
-	bh_lock_sock(sk);
 	if (sk->sk_state == IUCV_CONNECTED)
 		afiucv_hs_fin(sk);
-	bh_unlock_sock(sk);
 	consume_skb(skb);
 	return NET_RX_SUCCESS;
 }
@@ -2137,6 +2190,7 @@ static int afiucv_hs_callback_win(struct sock *sk, struct sk_buff *skb)
 	if (!iucv)
 		return NET_RX_SUCCESS;
 
+	/* also runs while a task owns the socket, see afiucv_hs_defer() */
 	if (sk->sk_state != IUCV_CONNECTED)
 		return NET_RX_SUCCESS;
 
@@ -2157,6 +2211,7 @@ static int afiucv_hs_callback_rx(struct sock *sk, struct sk_buff *skb)
 		return NET_RX_SUCCESS;
 	}
 
+	/* also runs while a task owns the socket, see afiucv_hs_defer() */
 	if (sk->sk_state != IUCV_CONNECTED) {
 		kfree_skb(skb);
 		return NET_RX_SUCCESS;
@@ -2189,12 +2244,174 @@ static int afiucv_hs_callback_rx(struct sock *sk, struct sk_buff *skb)
 	return NET_RX_SUCCESS;
 }
 
+/**
+ * afiucv_hs_stray() - test for a frame that no state of the socket can use
+ * @sk:		the socket the frame is for, under bh_lock_sock()
+ * @flags:	the flags of the frame
+ *
+ * A stray never waits for the owner. A SYN is one outside IUCV_LISTEN and a
+ * SYN|ACK or SYN|FIN outside IUCV_BOUND, the state tests of their
+ * callbacks; a full accept queue is not tested here. A FIN is one unless
+ * the socket is connected or a recorded SYN|ACK is about to connect it.
+ * Data, SHT and WIN are strays once the peer's FIN is recorded: the socket
+ * stays IUCV_CONNECTED until the owner applies it.
+ *
+ * Context: Softirq, under bh_lock_sock().
+ * Return: true if the frame is to be freed or, for a SYN, refused.
+ */
+static bool afiucv_hs_stray(struct sock *sk, u8 flags)
+{
+	struct iucv_sock *iucv = iucv_sk(sk);
+
+	switch (flags) {
+	case 0:
+	case AF_IUCV_FLAG_SHT:
+	case AF_IUCV_FLAG_WIN:
+		return iucv->hs_pending & AF_IUCV_PEND_FIN;
+	case AF_IUCV_FLAG_FIN:
+		return sk->sk_state != IUCV_CONNECTED &&
+		       !(iucv->hs_pending & AF_IUCV_PEND_SYNACK);
+	case AF_IUCV_FLAG_SYN:
+		return sk->sk_state != IUCV_LISTEN;
+	case AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK:
+	case AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN:
+		return sk->sk_state != IUCV_BOUND;
+	}
+	return true;
+}
+
+/**
+ * afiucv_hs_defer() - test whether a data, SHT or WIN frame has to wait
+ * @sk:		the socket, owned by a task, under bh_lock_sock()
+ *
+ * These frames do not change sk_state and are handled on arrival, also
+ * while a task owns the socket. They wait in the socket backlog only if:
+ *
+ * - the reply to our SYN is recorded and not applied yet: the socket is
+ *   still IUCV_BOUND, where data is freed and a WIN ignored;
+ * - frames wait already: a later one must not overtake them, and
+ *   sk_backlog.len stays non-zero until __release_sock() has run them all.
+ *
+ * Context: Softirq, under bh_lock_sock().
+ * Return: true if the frame has to wait in the backlog.
+ */
+static bool afiucv_hs_defer(struct sock *sk)
+{
+	return (iucv_sk(sk)->hs_pending & AF_IUCV_PEND_SYNREPLY) ||
+	       sk->sk_backlog.len != 0;
+}
+
+/**
+ * afiucv_hs_record() - keep a control frame for the task that owns the socket
+ * @sk:		the socket, owned by a task, under bh_lock_sock()
+ * @skb:	the frame, consumed here
+ * @flags:	the flags of the frame: SYN|ACK, SYN|FIN or FIN
+ *
+ * Not queued: nothing resends a control frame, and the socket backlog has
+ * to refuse a pfmemalloc skb. A bit in hs_pending is kept instead, and the
+ * window of a SYN|ACK. The first reply to our SYN decides; a FIN is applied
+ * after it.
+ *
+ * Context: Softirq, under bh_lock_sock().
+ */
+static void afiucv_hs_record(struct sock *sk, struct sk_buff *skb, u8 flags)
+{
+	struct iucv_sock *iucv = iucv_sk(sk);
+
+	switch (flags) {
+	case AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK:
+		if (!(iucv->hs_pending & AF_IUCV_PEND_SYNREPLY)) {
+			iucv->hs_pending_window = iucv_trans_hdr(skb)->window;
+			iucv->hs_pending |= AF_IUCV_PEND_SYNACK;
+		}
+		break;
+	case AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN:
+		if (!(iucv->hs_pending & AF_IUCV_PEND_SYNREPLY))
+			iucv->hs_pending |= AF_IUCV_PEND_SYNFIN;
+		break;
+	case AF_IUCV_FLAG_FIN:
+		iucv->hs_pending |= AF_IUCV_PEND_FIN;
+		break;
+	}
+	consume_skb(skb);
+}
+
+/**
+ * afiucv_hs_backlog_add() - make a frame wait for the task that owns the socket
+ * @sk:		the socket, owned by a task, under bh_lock_sock()
+ * @skb:	the frame
+ * @flags:	the flags of the frame: a SYN, or a data, SHT or WIN frame
+ *
+ * sk_add_backlog() is not used: its limit is in bytes and counts the
+ * receive queue too, while the window is in frames. So frames are counted:
+ *
+ * - data, SHT and WIN up to the window given to the peer plus one. They
+ *   wait only from the reply to our SYN until the owner has run them, and
+ *   a peer that keeps to its window sends at most that many data frames
+ *   and its SHT by then, and no WIN;
+ * - SYNs up to the accept queue plus the one sk_acceptq_is_full() admits.
+ *
+ * The counts restart only when __release_sock() has run every frame, so a
+ * peer cannot keep the owner in its loop. Past the limit a frame is
+ * dropped and a SYN refused; so is a pfmemalloc skb, on which
+ * __sk_backlog_rcv() BUG()s unless the socket is SOCK_MEMALLOC. A waiting
+ * SYN is cut to its header, all the reply needs, and holds its device:
+ * close() drops hs_dev before the release runs the SYN.
+ *
+ * Context: Softirq, under bh_lock_sock().
+ */
+static void afiucv_hs_backlog_add(struct sock *sk, struct sk_buff *skb,
+				  u8 flags)
+{
+	struct iucv_sock *iucv = iucv_sk(sk);
+	unsigned int *count;
+	unsigned int limit;
+
+	if (!sk->sk_backlog.len) {
+		iucv->hs_backlog_data = 0;
+		iucv->hs_backlog_syn = 0;
+	}
+	if (flags == AF_IUCV_FLAG_SYN) {
+		count = &iucv->hs_backlog_syn;
+		limit = sk->sk_max_ack_backlog + 1;
+	} else {
+		count = &iucv->hs_backlog_data;
+		limit = afiucv_hs_window(iucv) + 1;
+	}
+	if (*count >= limit ||
+	    (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC)))
+		goto refuse;
+	if (flags == AF_IUCV_FLAG_SYN) {
+		if (pskb_trim(skb, sizeof(struct af_iucv_trans_hdr)))
+			goto refuse;
+		BUILD_BUG_ON(sizeof(struct iucv_skb_cb) >
+			     sizeof_field(struct sk_buff, cb));
+		netdev_hold(skb->dev, &IUCV_SKB_CB(skb)->dev_tracker,
+			    GFP_ATOMIC);
+	}
+	__sk_add_backlog(sk, skb);
+	/* a window of 65535 frames of 64K each would wrap the int */
+	sk->sk_backlog.len = min_t(u32, sk->sk_backlog.len + skb->truesize,
+				   INT_MAX);
+	(*count)++;
+	return;
+
+refuse:
+	if (flags == AF_IUCV_FLAG_SYN) {
+		afiucv_hs_callback_syn(NULL, skb);
+	} else {
+		sk_drops_inc(sk);
+		kfree_skb_reason(skb, SKB_DROP_REASON_SOCKET_BACKLOG);
+	}
+}
+
 /**
  * afiucv_hs_dispatch() - run the callback of a frame
  * @sk:		the socket the frame is for, or NULL if there is none
  * @skb:	the frame
  *
- * Context: Softirq.
+ * Context: BH disabled. @sk is under bh_lock_sock(), or owned by the caller
+ * in afiucv_hs_backlog_rcv().
  * Return: NET_RX_SUCCESS.
  */
 static int afiucv_hs_dispatch(struct sock *sk, struct sk_buff *skb)
@@ -2240,6 +2457,74 @@ static int afiucv_hs_dispatch(struct sock *sk, struct sk_buff *skb)
 	return err;
 }
 
+/**
+ * afiucv_hs_backlog_rcv() - sk_backlog_rcv, run a frame that waited
+ * @sk:		the socket, owned by the caller
+ * @skb:	the frame
+ *
+ * Frames run in the order of arrival. The reply to our SYN came before any
+ * of them, so it is applied first. A SYN is refused if the listener was
+ * closed or its accept queue filled meanwhile.
+ *
+ * Context: Process context, socket owned by the caller, no sk_lock.slock.
+ * Return: 0.
+ */
+static int afiucv_hs_backlog_rcv(struct sock *sk, struct sk_buff *skb)
+{
+	struct net_device *dev = NULL;
+	netdevice_tracker tracker;
+
+	spin_lock_bh(&sk->sk_lock.slock);
+	afiucv_hs_apply(sk, AF_IUCV_PEND_SYNREPLY);
+	spin_unlock_bh(&sk->sk_lock.slock);
+	if (iucv_trans_hdr(skb)->flags == AF_IUCV_FLAG_SYN) {
+		/* held by afiucv_hs_backlog_add(); the reply uses up the skb */
+		dev = skb->dev;
+		tracker = IUCV_SKB_CB(skb)->dev_tracker;
+		if (sk->sk_state != IUCV_LISTEN || sk_acceptq_is_full(sk)) {
+			afiucv_hs_callback_syn(NULL, skb);
+			netdev_put(dev, &tracker);
+			return 0;
+		}
+	}
+	/* BH off: the callbacks take message_q.lock with spin_lock() */
+	local_bh_disable();
+	afiucv_hs_dispatch(sk, skb);
+	local_bh_enable();
+	netdev_put(dev, &tracker);
+	return 0;
+}
+
+/**
+ * afiucv_hs_rcv_owned() - handle a frame while a task owns the socket
+ * @sk:		the socket, under bh_lock_sock()
+ * @skb:	the frame
+ * @flags:	the flags of the frame
+ *
+ * A SYN|ACK, SYN|FIN or FIN is recorded, a SYN waits in the backlog, and a
+ * data, SHT or WIN frame is handled now unless afiucv_hs_defer() says wait.
+ *
+ * Context: Softirq, under bh_lock_sock().
+ * Return: NET_RX_SUCCESS.
+ */
+static int afiucv_hs_rcv_owned(struct sock *sk, struct sk_buff *skb, u8 flags)
+{
+	switch (flags) {
+	case AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK:
+	case AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN:
+	case AF_IUCV_FLAG_FIN:
+		afiucv_hs_record(sk, skb, flags);
+		return NET_RX_SUCCESS;
+	case AF_IUCV_FLAG_SYN:
+		break;
+	default:
+		if (!afiucv_hs_defer(sk))
+			return afiucv_hs_dispatch(sk, skb);
+	}
+	afiucv_hs_backlog_add(sk, skb, flags);
+	return NET_RX_SUCCESS;
+}
+
 /*
  * afiucv_hs_rcv() - base function for arriving data through HiperSockets
  *                   transport
@@ -2311,18 +2596,25 @@ static int afiucv_hs_rcv(struct sk_buff *skb, struct net_device *dev,
 	if (!iucv)
 		sk = NULL;
 
-	/* no sock
-	how should we send with no sock
-	1) send without sock no send rc checking?
-	2) introduce default sock to handle this cases
-
-	 SYN -> send SYN|ACK in good case, send SYN|FIN in bad case
-	 data -> send FIN
-	 SYN|ACK, SYN|FIN, FIN -> no action? */
+	/* no socket: a SYN is refused, anything else freed */
+	if (!sk)
+		return afiucv_hs_dispatch(NULL, skb);
 
-	err = afiucv_hs_dispatch(sk, skb);
-	if (sk)
-		sock_put(sk);
+	bh_lock_sock(sk);
+	if (afiucv_hs_stray(sk, trans_hdr->flags)) {
+		if (trans_hdr->flags == AF_IUCV_FLAG_SYN)
+			afiucv_hs_callback_syn(NULL, skb);
+		else if (trans_hdr->flags == AF_IUCV_FLAG_FIN)
+			consume_skb(skb);
+		else
+			kfree_skb_reason(skb, SKB_DROP_REASON_NO_SOCKET);
+	} else if (sock_owned_by_user(sk)) {
+		err = afiucv_hs_rcv_owned(sk, skb, trans_hdr->flags);
+	} else {
+		err = afiucv_hs_dispatch(sk, skb);
+	}
+	bh_unlock_sock(sk);
+	sock_put(sk);
 	return err;
 }
 

-- 
2.56.0



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

* [PATCH RFC net v4 7/8] net/iucv: set RCV_SHUTDOWN under the socket spinlock
  2026-11-15  1:29 [PATCH RFC net v4 0/8] net/iucv: serialize the HiperSockets receive path with the socket lock Bryam Vargas via B4 Relay
                   ` (5 preceding siblings ...)
  2026-11-15  1:29 ` [PATCH RFC net v4 6/8] net/iucv: serialize HiperSockets receive with the socket lock Bryam Vargas via B4 Relay
@ 2026-11-15  1:29 ` Bryam Vargas via B4 Relay
  2026-11-16  1:29   ` sashiko-bot
  2026-11-15  1:29 ` [PATCH RFC net v4 8/8] net/iucv: return EOF to a reader parked when the peer closes Bryam Vargas via B4 Relay
  7 siblings, 1 reply; 17+ messages in thread
From: Bryam Vargas via B4 Relay @ 2026-11-15  1:29 UTC (permalink / raw)
  To: Jakub Kicinski, Alexandra Winter, Paolo Abeni, David S. Miller,
	Hidayath Khan, Eric Dumazet
  Cc: Thorsten Winkler, Simon Horman, netdev, linux-kernel, Nagamani PV,
	linux-s390

From: Bryam Vargas <hexlabsecurity@proton.me>

shutdown(SHUT_RD) sets RCV_SHUTDOWN and purges the receive queue under
lock_sock() alone, and nothing orders that against a HiperSockets data
frame: a frame that tested RCV_SHUTDOWN before the bit was set is
queued after the purge, and recvmsg() returns its data after
shutdown(SHUT_RD) returned.

Since the previous patch such a frame is received under sk_lock.slock,
also while a task owns the socket; one that waited for the owner runs
when it releases the socket, which shutdown() does only after the purge.
On HiperSockets, set the bit and purge under that lock. recvmsg() sets
the same bit under lock_sock() only, when it reads a zero-length record,
which is how the peer's SHT arrives; that write and the frame's test are
marked WRITE_ONCE() and READ_ONCE(), and only a frame the peer sent
after that record can race with it. The z/VM branch is unchanged, less
a line that was commented out.

Without the previous patch this one changes nothing: the frame then
takes no lock that shutdown() could share with it.

Fixes: 82492a355fac ("af_iucv: add shutdown for HS transport")
Reported-by: Hidayath Khan <hidayath@linux.ibm.com>
Closes: https://lore.kernel.org/netdev/b3fea2ca-25ff-42c0-812c-f8cc6bb6667e@linux.ibm.com/
Cc: stable@vger.kernel.org # 6.18.x, with the previous patch only
Signed-off-by: Bryam Vargas <hexlabsecurity@proton.me>
---
v4: the message no longer says that iucv_send_ctrl() clears and restores
SEND_SHUTDOWN in this byte; since 2/8 it does not. No code change.
v3: https://lore.kernel.org/netdev/20261110-b4-disp-2a6b4364-v3-6-35677b68e697@proton.me/

This is the first of the two races KCSAN reported on v1 in the mail
the Closes: tag points at, afiucv_hs_callback_rx() against
iucv_sock_shutdown() on sk_shutdown. The second there, on hs_dev, is
fixed by 1/2 of the two fixes posted for net on 28 Oct ([7] in the
cover letter), which are not in this series.

In the emulated setup, 200 cycles a run and four runs a kernel (QEMU's
default slice and three seeded short ones): net and the kernels with
1/8, with 1/8 and 2/8, with 1/8 to 3/8 and with 1/8 to 4/8 read data
after shutdown(SHUT_RD) in 0 to 9 cycles of a run, 68 of 4000 in all.
The series reads none. Nor does the series minus this spinlock: since
6/8 only a frame that takes sk_lock.slock between shutdown()'s
lock_sock() and its write gets through, and no run had one. With
udelay(50) added after lock_sock() in shutdown() and after the frame's
test, the series minus this spinlock reads late data in 9 to 19 of 200
cycles and the series in none.

On the hardware Hidayath could not get that window to fire with v1
either, and KCSAN cannot show it closed: shutdown()'s write is now
WRITE_ONCE(), which the readers that take no lock need, and that alone
silences the report. IOW only the udelay() kernels above show what the
spinlock does.

The z/VM branch is unchanged; what is open there is under "Still open"
in the cover letter.
---
 net/iucv/af_iucv.c | 34 +++++++++++++++++++++++-----------
 1 file changed, 23 insertions(+), 11 deletions(-)

diff --git a/net/iucv/af_iucv.c b/net/iucv/af_iucv.c
index 2dd9f12bce62..7a55bf884bc7 100644
--- a/net/iucv/af_iucv.c
+++ b/net/iucv/af_iucv.c
@@ -1471,7 +1471,7 @@ static int iucv_sock_recvmsg(struct socket *sock, struct msghdr *msg,
 	rlen   = skb->len - offset;		/* real length of skb */
 	copied = min_t(unsigned int, rlen, len);
 	if (!rlen)
-		sk->sk_shutdown = sk->sk_shutdown | RCV_SHUTDOWN;
+		WRITE_ONCE(sk->sk_shutdown, sk->sk_shutdown | RCV_SHUTDOWN);
 
 	cskb = skb;
 	if (skb_copy_datagram_msg(cskb, offset, msg, copied)) {
@@ -1674,16 +1674,28 @@ static int iucv_sock_shutdown(struct socket *sock, int how)
 			iucv_send_ctrl(sk, AF_IUCV_FLAG_SHT);
 	}
 
-	sk->sk_shutdown |= how;
-	if (how == RCV_SHUTDOWN || how == SHUTDOWN_MASK) {
-		if ((iucv->transport == AF_IUCV_TRANS_IUCV) &&
-		    iucv->path) {
-			err = pr_iucv->path_quiesce(iucv->path, NULL);
-			if (err)
-				err = -ENOTCONN;
-/*			skb_queue_purge(&sk->sk_receive_queue); */
+	if (iucv->transport == AF_IUCV_TRANS_HIPER) {
+		/* Set the bit and purge under sk_lock.slock: a frame handled
+		 * on arrival tests RCV_SHUTDOWN under it, also while this
+		 * task owns the socket, and one that passed the test would
+		 * be queued after the purge. A frame that waited runs when
+		 * the owner releases the socket, not in between.
+		 */
+		spin_lock_bh(&sk->sk_lock.slock);
+		WRITE_ONCE(sk->sk_shutdown, sk->sk_shutdown | how);
+		if (how == RCV_SHUTDOWN || how == SHUTDOWN_MASK)
+			skb_queue_purge(&sk->sk_receive_queue);
+		spin_unlock_bh(&sk->sk_lock.slock);
+	} else {
+		sk->sk_shutdown |= how;
+		if (how == RCV_SHUTDOWN || how == SHUTDOWN_MASK) {
+			if (iucv->path) {
+				err = pr_iucv->path_quiesce(iucv->path, NULL);
+				if (err)
+					err = -ENOTCONN;
+			}
+			skb_queue_purge(&sk->sk_receive_queue);
 		}
-		skb_queue_purge(&sk->sk_receive_queue);
 	}
 
 	/* Wake up anyone sleeping in poll */
@@ -2217,7 +2229,7 @@ static int afiucv_hs_callback_rx(struct sock *sk, struct sk_buff *skb)
 		return NET_RX_SUCCESS;
 	}
 
-	if (sk->sk_shutdown & RCV_SHUTDOWN) {
+	if (READ_ONCE(sk->sk_shutdown) & RCV_SHUTDOWN) {
 		kfree_skb(skb);
 		return NET_RX_SUCCESS;
 	}

-- 
2.56.0



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

* [PATCH RFC net v4 8/8] net/iucv: return EOF to a reader parked when the peer closes
  2026-11-15  1:29 [PATCH RFC net v4 0/8] net/iucv: serialize the HiperSockets receive path with the socket lock Bryam Vargas via B4 Relay
                   ` (6 preceding siblings ...)
  2026-11-15  1:29 ` [PATCH RFC net v4 7/8] net/iucv: set RCV_SHUTDOWN under the socket spinlock Bryam Vargas via B4 Relay
@ 2026-11-15  1:29 ` Bryam Vargas via B4 Relay
  2026-11-16  1:29   ` sashiko-bot
  7 siblings, 1 reply; 17+ messages in thread
From: Bryam Vargas via B4 Relay @ 2026-11-15  1:29 UTC (permalink / raw)
  To: Jakub Kicinski, Alexandra Winter, Paolo Abeni, David S. Miller,
	Hidayath Khan, Eric Dumazet
  Cc: Thorsten Winkler, Simon Horman, netdev, linux-kernel, Nagamani PV,
	linux-s390

From: Bryam Vargas <hexlabsecurity@proton.me>

A recvmsg() that starts after the peer closed returns 0. One that was
already waiting returns -ENOTCONN: its wait ends on sk_state and does
not ask whether anything is left to read. In a z/VM guest over
HiperSockets that was every parked reader, 13627 of 13627 on net-next
[1]. poll() does not report EPOLLRDHUP for the close either.

Return EOF from the wait too when the socket is IUCV_DISCONN with
nothing left to read. As for a call that starts afterwards, it does not
matter how the socket got there: the peer's FIN, a severed z/VM path, a
transmit error, a window update that could not be sent or the device
going down. EOF does not say that all the peer sent was read: a severed
z/VM path drops the messages not yet received, for either call. Set
RCV_SHUTDOWN where the HiperSockets FIN is applied, as TCP does: poll()
then reports EPOLLRDHUP and EPOLLRDNORM for the close, and EPOLLHUP if
the socket had shut down its own sending. The other ways still report
none of the three.

Fixes: 561e036006dc ("[AF_IUCV]: Implementation of a skb backlog queue")
Fixes: 3881ac441f64 ("af_iucv: add HiperSockets transport")
Reported-by: Hidayath Khan <hidayath@linux.ibm.com>
Closes: https://lore.kernel.org/netdev/82691101-92d7-4838-b16e-2e41a83f65d0@linux.ibm.com/
Link: https://lore.kernel.org/netdev/51a4a40a-f34b-4d41-b049-69aa53f4dcf5@linux.ibm.com/ [1]
Cc: stable@vger.kernel.org # 6.18.x
Signed-off-by: Bryam Vargas <hexlabsecurity@proton.me>
---
v4: unchanged, was 7/7. The last paragraph of v3's note is gone: since
2/8 the NETDEV_GOING_DOWN notifier no longer writes sk_shutdown, so it
cannot lose the RCV_SHUTDOWN of a FIN.
v3: this was 2/2 of v2. The EOF in the receive wait, which v2 had in
the lock patch, is here now, so both halves of one change in behaviour
are in one patch: the wait returns EOF, and the FIN sets RCV_SHUTDOWN.
v3: https://lore.kernel.org/netdev/20261110-b4-disp-2a6b4364-v3-7-35677b68e697@proton.me/

Each half covers what the other does not. RCV_SHUTDOWN at the FIN gives
the parked reader EOF and poll() EPOLLRDHUP on HiperSockets, but a z/VM
path severed under a sleeping reader sets no RCV_SHUTDOWN; the test in
the wait covers the sever and gives poll() nothing.

In the emulated setup:

- the peer closes under a reader, 40 cycles, half with the reader
  parked and half with the close racing its recv(): -ENOTCONN in 29 on
  net, 35 with 1/8 to 3/8 and 29 with 1/8 to 7/8; EOF in all 40 with
  this patch
- poll() after the peer's close, 10 cycles of three cases (nothing
  queued, a message unread, the socket's own SHUT_WR first): on those
  three kernels no EPOLLRDHUP in any of the 30 and no EPOLLHUP in the
  10 with SHUT_WR; with this patch each case reports what an AF_UNIX
  socketpair reports on the same kernel. With RCV_SHUTDOWN at the FIN
  taken out again the reader still gets EOF, from the wait, and poll()
  reports as before the patch
- a reader parked while its own interface goes down, or while a send of
  its own fails in the adapter (SBALF15 0x10), and a z/VM reader whose
  path is severed (emulated CP): -ENOTCONN on those three kernels, and
  again with the EOF taken out of the wait; EOF with this patch

The interface going down and the TX error change sk_state with no
socket lock, so the test in the wait looks at the receive queue again.
A test kernel has an mdelay() that holds the reader between its look
at the queue and its look at the state; a byte arrives and the
interface goes down in that time. The reader's call returns -ENOTCONN
and its next two the byte and EOF. With the receive queue left out of
the test the first call returns 0 with the byte still queued. FWIW net
returns -ENOTCONN the same way when the skb lands between its own two
tests, a narrower window. The window update that could not be sent is
by inspection.

After 6/8 the FIN is applied under sk_lock.slock on every path, by the
softirq with no owner or by the owner from release_cb, and 7/8 has
shutdown() write the same field under that lock.
---
 net/iucv/af_iucv.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/net/iucv/af_iucv.c b/net/iucv/af_iucv.c
index 7a55bf884bc7..dda039bf5158 100644
--- a/net/iucv/af_iucv.c
+++ b/net/iucv/af_iucv.c
@@ -75,6 +75,8 @@ static void afiucv_hs_synfin(struct sock *sk)
 static void afiucv_hs_fin(struct sock *sk)
 {
 	sk->sk_state = IUCV_DISCONN;
+	/* as a TCP FIN: EOF for a reader, EPOLLRDHUP for poll() */
+	WRITE_ONCE(sk->sk_shutdown, sk->sk_shutdown | RCV_SHUTDOWN);
 	sk->sk_state_change(sk);
 }
 
@@ -1398,8 +1400,9 @@ static void iucv_process_message_q(struct sock *sk)
  * then and ends the wait.
  *
  * Context: Process context, socket lock held; sleeps with it released.
- * Return: the skb, or NULL with @err set. At RCV_SHUTDOWN the caller
- * returns EOF whatever @err is.
+ * Return: the skb, or NULL with @err set: 0 if the connection is gone with
+ * nothing left to read and the call could still wait. At RCV_SHUTDOWN the
+ * caller returns EOF whatever @err is.
  */
 static struct sk_buff *iucv_sock_recv_skb(struct sock *sk, int flags, int *err)
 {
@@ -1417,7 +1420,16 @@ static struct sk_buff *iucv_sock_recv_skb(struct sock *sk, int flags, int *err)
 		if (!timeo)
 			break;
 		if (sk->sk_state != IUCV_CONNECTED) {
-			*err = -ENOTCONN;
+			/* disconnected and nothing left to read: EOF, as at
+			 * the top of iucv_sock_recvmsg()
+			 */
+			if (sk->sk_state == IUCV_DISCONN &&
+			    skb_queue_empty(&iucv_sk(sk)->backlog_skb_q) &&
+			    skb_queue_empty(&sk->sk_receive_queue) &&
+			    list_empty(&iucv_sk(sk)->message_q.list))
+				*err = 0;
+			else
+				*err = -ENOTCONN;
 			break;
 		}
 		if (signal_pending(current)) {

-- 
2.56.0



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

* Re: [PATCH RFC net v4 1/8] net/iucv: keep the window given to the peer in a field of its own
  2026-11-15  1:29 ` [PATCH RFC net v4 1/8] net/iucv: keep the window given to the peer in a field of its own Bryam Vargas via B4 Relay
@ 2026-11-16  1:29   ` sashiko-bot
  0 siblings, 0 replies; 17+ messages in thread
From: sashiko-bot @ 2026-11-16  1:29 UTC (permalink / raw)
  To: Bryam Vargas
  Cc: linux-s390, Heiko Carstens, Vasily Gorbik, Alexander Gordeev,
	Christian Borntraeger

> From: Bryam Vargas <hexlabsecurity@proton.me>
> 
> A HiperSockets SYN carries msglimit as the receive window, and
> SO_MSGLIMIT is accepted while the socket is IUCV_BOUND, which it is
> until the SYN|ACK arrives. A msglimit changed in between leaves the peer
> with the window the SYN carried, while recvmsg() counts the frames it
> reads against the new value:
> 
> - Lowered, a peer that keeps to its window has the socket closed on it
>   once the receive buffer is full and frames wait in backlog_skb_q,
>   where no window update resets the count: WARN_ON(), -EFAULT, the rest
>   of the stream lost.
> - Raised far enough, the threshold of the window update lies above the
>   window: the count never reaches it, the peer uses up its window and
>   its writer waits for an update that only a data frame from us would
>   carry.
> 
> Record the window when the first SYN of a socket leaves, and in a
> listener's SYN|ACK, and let recvmsg() count frames against it and take
> the window-update threshold from it. A SYN that is not sent records
> nothing, and a later SYN of the socket carries the recorded window. A
> msglimit set after the first SYN no longer changes anything for that
> socket; getsockopt() still returns it. A socket connected by a SYN|ACK
> without having sent a SYN keeps msglimit as its window.
> 
> Fixes: 3881ac441f64 ("af_iucv: add HiperSockets transport")
> Fixes: 800c5eb7b5eb ("af_iucv: change net_device handling for HS transport")
> Cc: stable@vger.kernel.org # 6.18.x
> Signed-off-by: Bryam Vargas <hexlabsecurity@proton.me>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261114-b4-disp-fc0634a3-v4-0-f96c506447ce@proton.me?part=1


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

* Re: [PATCH RFC net v4 2/8] net/iucv: do not clear SEND_SHUTDOWN to send a control frame
  2026-11-15  1:29 ` [PATCH RFC net v4 2/8] net/iucv: do not clear SEND_SHUTDOWN to send a control frame Bryam Vargas via B4 Relay
@ 2026-11-16  1:29   ` sashiko-bot
  0 siblings, 0 replies; 17+ messages in thread
From: sashiko-bot @ 2026-11-16  1:29 UTC (permalink / raw)
  To: Bryam Vargas
  Cc: linux-s390, Heiko Carstens, Vasily Gorbik, Alexander Gordeev,
	Christian Borntraeger

> From: Bryam Vargas <hexlabsecurity@proton.me>
> 
> iucv_send_ctrl() allocates with sock_alloc_send_skb(), which fails once
> SEND_SHUTDOWN is set, so it clears the bit first and writes a copy of
> sk_shutdown back afterwards. recvmsg() calls it for a window update and
> the NETDEV_GOING_DOWN notifier for a FIN, both without the socket lock.
> On a socket that has shut down sending:
> 
> - A shutdown(SHUT_RD) done in between is undone when the copy is
>   written back: the call returned 0 and the socket still receives.
> - sendmsg() can find SEND_SHUTDOWN clear and send data after the SHT of
>   a shutdown(SHUT_WR) that already returned.
> 
> Allocate the control frame with sock_wmalloc() behind the test of
> sk_sndbuf that sock_alloc_send_skb() makes, and leave sk_shutdown alone.
> 
> Fixes: 4e0ad3221691 ("Subject: [PATCH] af_iucv: enable control sends in case of SEND_SHUTDOWN")
> Cc: stable@vger.kernel.org # 6.18.x
> Signed-off-by: Bryam Vargas <hexlabsecurity@proton.me>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261114-b4-disp-fc0634a3-v4-0-f96c506447ce@proton.me?part=2


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

* Re: [PATCH RFC net v4 3/8] net/iucv: fail sendmsg() with EPIPE once sending is shut down
  2026-11-15  1:29 ` [PATCH RFC net v4 3/8] net/iucv: fail sendmsg() with EPIPE once sending is shut down Bryam Vargas via B4 Relay
@ 2026-11-16  1:29   ` sashiko-bot
  0 siblings, 0 replies; 17+ messages in thread
From: sashiko-bot @ 2026-11-16  1:29 UTC (permalink / raw)
  To: Bryam Vargas
  Cc: linux-s390, Heiko Carstens, Vasily Gorbik, Alexander Gordeev,
	Christian Borntraeger

> From: Bryam Vargas <hexlabsecurity@proton.me>
> 
> sendmsg() tests SEND_SHUTDOWN on entry only, then waits for the peer's
> window without the socket lock. When sending is shut down during that
> wait, by shutdown(SHUT_WR) in another thread or by the peer's quiesce
> over z/VM, the wait does not end and the call does not fail: the writer
> waits out SO_SNDTIMEO, or, when the window opens first, sends its
> record after the socket's SHT and the peer reads data after EOF. With
> the peer's FIN in as well it returns -ECONNRESET for a stream its own
> socket shut down.
> 
> End the wait when sending is shut down and test SEND_SHUTDOWN again
> after it, ahead of the sk_state test, so the call fails with -EPIPE as
> it does on entry.
> 
> Fixes: 0ea920d211e0 ("af_iucv: Return -EAGAIN if iucv msg limit is exceeded")
> Cc: stable@vger.kernel.org # 6.18.x
> Signed-off-by: Bryam Vargas <hexlabsecurity@proton.me>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261114-b4-disp-fc0634a3-v4-0-f96c506447ce@proton.me?part=3


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

* Re: [PATCH RFC net v4 4/8] net/iucv: run recvmsg() under the socket lock
  2026-11-15  1:29 ` [PATCH RFC net v4 4/8] net/iucv: run recvmsg() under the socket lock Bryam Vargas via B4 Relay
@ 2026-11-16  1:29   ` sashiko-bot
  0 siblings, 0 replies; 17+ messages in thread
From: sashiko-bot @ 2026-11-16  1:29 UTC (permalink / raw)
  To: Bryam Vargas
  Cc: linux-s390, Heiko Carstens, Vasily Gorbik, Alexander Gordeev,
	Christian Borntraeger

> From: Bryam Vargas <hexlabsecurity@proton.me>
> 
> recvmsg() takes no socket lock, on either transport:
> 
> - Two threads reading one socket get its data out of order.
> - A read that finds the peer's SHT sets RCV_SHUTDOWN with a plain
>   read-modify-write of sk_shutdown, and can lose the SEND_SHUTDOWN that
>   a shutdown(SHUT_WR) set meanwhile.
> - A MSG_PEEK read leaves its skb on the receive queue and holds a
>   reference to it. When a second reader consumes the skb meanwhile,
>   that reference is the last one and skb_unref() frees nothing: the skb
>   and its receive memory leak, and iucv_sock_destruct() warns.
> 
> All three need only a second thread at the wrong moment. No
> memory-safety report is known from any of them.
> [ ... ]
> Suggested-by: Alexandra Winter <wintera@linux.ibm.com>
> Link: https://lore.kernel.org/all/20260724222917.134769-1-hexlabsecurity@proton.me/
> Cc: stable@vger.kernel.org # 6.18.x: aa5ec571b93f: net/iucv: Add lock nesting annotation in iucv_sock_close()
> Cc: stable@vger.kernel.org # 6.18.x
> Signed-off-by: Bryam Vargas <hexlabsecurity@proton.me>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261114-b4-disp-fc0634a3-v4-0-f96c506447ce@proton.me?part=4


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

* Re: [PATCH RFC net v4 5/8] net/iucv: factor out the HiperSockets frame dispatch and state changes
  2026-11-15  1:29 ` [PATCH RFC net v4 5/8] net/iucv: factor out the HiperSockets frame dispatch and state changes Bryam Vargas via B4 Relay
@ 2026-11-16  1:29   ` sashiko-bot
  0 siblings, 0 replies; 17+ messages in thread
From: sashiko-bot @ 2026-11-16  1:29 UTC (permalink / raw)
  To: Bryam Vargas
  Cc: linux-s390, Heiko Carstens, Vasily Gorbik, Alexander Gordeev,
	Christian Borntraeger

> From: Bryam Vargas <hexlabsecurity@proton.me>
> 
> Move the switch on the frame type out of afiucv_hs_rcv() into
> afiucv_hs_dispatch(), and the state changes a SYN|ACK, a SYN|FIN and a
> FIN make into afiucv_hs_synack(), afiucv_hs_synfin() and
> afiucv_hs_fin(). The three are still called under the bh_lock_sock() of
> their callbacks, which now test sk where they tested iucv_sk(sk), a
> cast of it. They are defined ahead of iucv_proto: the next patch
> reaches them from proto->release_cb. afiucv_hs_synfin() and
> afiucv_hs_fin() are the same for now and stay two: a later patch sets
> RCV_SHUTDOWN in the second only.
> 
> No functional change. The next patch runs the dispatch from a second
> place and the three state changes from the task that owns the socket.
> 
> Cc: stable@vger.kernel.org # 6.18.x, needed by the next patch
> Signed-off-by: Bryam Vargas <hexlabsecurity@proton.me>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261114-b4-disp-fc0634a3-v4-0-f96c506447ce@proton.me?part=5


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

* Re: [PATCH RFC net v4 6/8] net/iucv: serialize HiperSockets receive with the socket lock
  2026-11-15  1:29 ` [PATCH RFC net v4 6/8] net/iucv: serialize HiperSockets receive with the socket lock Bryam Vargas via B4 Relay
@ 2026-11-16  1:29   ` sashiko-bot
  0 siblings, 0 replies; 17+ messages in thread
From: sashiko-bot @ 2026-11-16  1:29 UTC (permalink / raw)
  To: Bryam Vargas
  Cc: linux-s390, Heiko Carstens, Vasily Gorbik, Alexander Gordeev,
	Christian Borntraeger

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] net/iucv: unapplied hs_pending control frames in
  iucv_sock_connect() and wait
- [Medium] net/iucv: afiucv_hs_stray() and afiucv_hs_defer() queue stray
  data frames
- [Low] net/iucv: missing SOCK_ZAPPED check in afiucv_hs_stray() for
  IUCV_BOUND sockets
--

commit c1273c23cdabb854289296a6ae44aec0ac20fc36
Author: Bryam Vargas <hexlabsecurity@proton.me>

net/iucv: serialize HiperSockets receive with the socket lock

This commit serializes HiperSockets receive handling with the socket lock by
taking bh_lock_sock() in afiucv_hs_rcv() and recording control frames or
queueing data and SYN frames in the socket backlog while a task owns the
socket.

Link: https://lore.kernel.org/all/20260724222917.134769-1-hexlabsecurity@proton.me/

> diff --git a/net/iucv/af_iucv.c b/net/iucv/af_iucv.c
> index 957315bec2b45..2dd9f12bce625 100644
> --- a/net/iucv/af_iucv.c
> +++ b/net/iucv/af_iucv.c

[ ... ]

> @@ -845,6 +897,15 @@ static int iucv_sock_connect(struct socket *sock, struct sockaddr_unsized *addr,
>  	if (err)
>  		goto done;
>  
> +	/* The reply may be in already, recorded while we own the socket:
> +	 * apply it, or a non-blocking connect() misses it.
> +	 */
> +	if (iucv->transport == AF_IUCV_TRANS_HIPER) {
> +		spin_lock_bh(&sk->sk_lock.slock);
> +		afiucv_hs_apply(sk, AF_IUCV_PEND_SYNREPLY);
> +		spin_unlock_bh(&sk->sk_lock.slock);
> +	}
> +
>  	if (sk->sk_state != IUCV_CONNECTED)
>  		err = iucv_sock_wait(sk, iucv_sock_in_state(sk, IUCV_CONNECTED,
>  							    IUCV_DISCONN),

[Severity: Medium]
Can iucv_sock_connect() miss a SYN|ACK or SYN|FIN recorded in
iucv->hs_pending after this single afiucv_hs_apply() call?

iucv_sock_connect()
  spin_lock_bh(&sk->sk_lock.slock)
  afiucv_hs_apply(sk, AF_IUCV_PEND_SYNREPLY)
  spin_unlock_bh(&sk->sk_lock.slock)
  iucv_sock_wait()
    release_sock(sk)
    schedule_timeout()
    lock_sock(sk)
    <softirq>
      afiucv_hs_rcv()
        afiucv_hs_record()
          iucv->hs_pending |= AF_IUCV_PEND_SYNACK
    while (!(condition))  <-- sk_state is still IUCV_BOUND
      ret = -EAGAIN / -ERESTARTSYS
      break
  release_sock(sk)
    afiucv_hs_release_cb()
      afiucv_hs_synack()
        sk->sk_state = IUCV_CONNECTED

If a reply arrives after spin_unlock_bh() on a non-blocking connect(), or
after lock_sock(sk) reacquires ownership inside __iucv_sock_wait() (for
example, when woken by TX completion of the SYN via
afiucv_hs_callback_txnotify() -> iucv_sock_wake_msglim(), a signal, or a
timeout), iucv_sock_in_state() and the post-wait sk->sk_state check still see
IUCV_BOUND.  iucv_sock_connect() then returns -EAGAIN or -ERESTARTSYS while
release_sock(sk) applies the recorded reply, leaving the socket in
IUCV_CONNECTED (or in IUCV_DISCONN without returning -ECONNREFUSED) so a
subsequent connect() retry fails with -EBADFD.

Similarly, in iucv_sock_sendmsg(), an incoming FIN recorded as
AF_IUCV_PEND_FIN while the task owns the socket is not applied before
iucv_below_msglim() or the sk->sk_state != IUCV_CONNECTED check.

[ ... ]

> @@ -2189,12 +2244,174 @@ static int afiucv_hs_callback_rx(struct sock *sk, struct sk_buff *skb)
>  	return NET_RX_SUCCESS;
>  }
>  
> +/**
> + * afiucv_hs_stray() - test for a frame that no state of the socket can use
> + * @sk:		the socket the frame is for, under bh_lock_sock()
> + * @flags:	the flags of the frame
> + *
> + * A stray never waits for the owner. A SYN is one outside IUCV_LISTEN and a
> + * SYN|ACK or SYN|FIN outside IUCV_BOUND, the state tests of their
> + * callbacks; a full accept queue is not tested here. A FIN is one unless
> + * the socket is connected or a recorded SYN|ACK is about to connect it.
> + * Data, SHT and WIN are strays once the peer's FIN is recorded: the socket
> + * stays IUCV_CONNECTED until the owner applies it.
> + *
> + * Context: Softirq, under bh_lock_sock().
> + * Return: true if the frame is to be freed or, for a SYN, refused.
> + */
> +static bool afiucv_hs_stray(struct sock *sk, u8 flags)
> +{
> +	struct iucv_sock *iucv = iucv_sk(sk);
> +
> +	switch (flags) {
> +	case 0:
> +	case AF_IUCV_FLAG_SHT:
> +	case AF_IUCV_FLAG_WIN:
> +		return iucv->hs_pending & AF_IUCV_PEND_FIN;
> +	case AF_IUCV_FLAG_FIN:
> +		return sk->sk_state != IUCV_CONNECTED &&
> +		       !(iucv->hs_pending & AF_IUCV_PEND_SYNACK);
> +	case AF_IUCV_FLAG_SYN:
> +		return sk->sk_state != IUCV_LISTEN;
> +	case AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_ACK:
> +	case AF_IUCV_FLAG_SYN | AF_IUCV_FLAG_FIN:
> +		return sk->sk_state != IUCV_BOUND;
> +	}
> +	return true;
> +}
> +
> +/**
> + * afiucv_hs_defer() - test whether a data, SHT or WIN frame has to wait
> + * @sk:		the socket, owned by a task, under bh_lock_sock()
> + *
> + * These frames do not change sk_state and are handled on arrival, also
> + * while a task owns the socket. They wait in the socket backlog only if:
> + *
> + * - the reply to our SYN is recorded and not applied yet: the socket is
> + *   still IUCV_BOUND, where data is freed and a WIN ignored;
> + * - frames wait already: a later one must not overtake them, and
> + *   sk_backlog.len stays non-zero until __release_sock() has run them all.
> + *
> + * Context: Softirq, under bh_lock_sock().
> + * Return: true if the frame has to wait in the backlog.
> + */
> +static bool afiucv_hs_defer(struct sock *sk)
> +{
> +	return (iucv_sk(sk)->hs_pending & AF_IUCV_PEND_SYNREPLY) ||
> +	       sk->sk_backlog.len != 0;
> +}

[Severity: Medium]
Do afiucv_hs_stray() and afiucv_hs_defer() allow data, SHT, and WIN frames
to be queued into sk->sk_backlog when the socket is neither connected nor
about to be connected by a recorded SYN|ACK?

For flags 0, AF_IUCV_FLAG_SHT, and AF_IUCV_FLAG_WIN, afiucv_hs_stray() only
checks iucv->hs_pending & AF_IUCV_PEND_FIN, and afiucv_hs_defer() tests
AF_IUCV_PEND_SYNREPLY (which includes AF_IUCV_PEND_SYNFIN) as well as
sk->sk_backlog.len != 0.  If a task owns an IUCV_BOUND socket that has
recorded a SYN|FIN, or an IUCV_LISTEN socket with a SYN in sk->sk_backlog,
subsequent data, SHT, and WIN frames pass afiucv_hs_stray() and are queued
into sk->sk_backlog by afiucv_hs_backlog_add() instead of being freed in
afiucv_hs_rcv().

[ ... ]

> @@ -2311,18 +2596,25 @@ static int afiucv_hs_rcv(struct sk_buff *skb, struct net_device *dev,
>  	if (!iucv)
>  		sk = NULL;
>  
> -	/* no sock
> -	how should we send with no sock
> -	1) send without sock no send rc checking?
> -	2) introduce default sock to handle this cases
> -
> -	 SYN -> send SYN|ACK in good case, send SYN|FIN in bad case
> -	 data -> send FIN
> -	 SYN|ACK, SYN|FIN, FIN -> no action? */
> +	/* no socket: a SYN is refused, anything else freed */
> +	if (!sk)
> +		return afiucv_hs_dispatch(NULL, skb);
>  
> -	err = afiucv_hs_dispatch(sk, skb);
> -	if (sk)
> -		sock_put(sk);
> +	bh_lock_sock(sk);
> +	if (afiucv_hs_stray(sk, trans_hdr->flags)) {
> +		if (trans_hdr->flags == AF_IUCV_FLAG_SYN)
> +			afiucv_hs_callback_syn(NULL, skb);
> +		else if (trans_hdr->flags == AF_IUCV_FLAG_FIN)
> +			consume_skb(skb);
> +		else
> +			kfree_skb_reason(skb, SKB_DROP_REASON_NO_SOCKET);
> +	} else if (sock_owned_by_user(sk)) {
> +		err = afiucv_hs_rcv_owned(sk, skb, trans_hdr->flags);
> +	} else {
> +		err = afiucv_hs_dispatch(sk, skb);
> +	}
> +	bh_unlock_sock(sk);
> +	sock_put(sk);
>  	return err;
>  }

[Severity: Low]
Should afiucv_hs_stray() also test sock_flag(sk, SOCK_ZAPPED) for SYN|ACK and
SYN|FIN frames?

CPU0                              CPU1 (<softirq>)
iucv_sock_close(sk)
  lock_sock_nested(sk, ...)
                                  afiucv_hs_rcv()
                                    read_lock(&iucv_sk_list.lock)
                                    sock_hold(sk)
                                    read_unlock(&iucv_sk_list.lock)
  __iucv_sock_close(sk)
    iucv->hs_dev = NULL
    sock_set_flag(sk, SOCK_ZAPPED)
  release_sock(sk)
                                    bh_lock_sock(sk)
                                    afiucv_hs_stray(sk, SYN|ACK) -> false
                                    sock_owned_by_user(sk) -> false
                                    afiucv_hs_dispatch(sk, skb)
                                      afiucv_hs_synack()  <-- connected after close

As noted in afiucv_hs_apply(), __iucv_sock_close() leaves sk->sk_state as
IUCV_BOUND and marks the socket with SOCK_ZAPPED.  If afiucv_hs_rcv() looks
up the socket just before iucv->hs_dev is cleared and acquires
bh_lock_sock(sk) after release_sock(sk), the unowned path bypasses
afiucv_hs_apply() and transitions the closed socket to IUCV_CONNECTED or
IUCV_DISCONN.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261114-b4-disp-fc0634a3-v4-0-f96c506447ce@proton.me?part=6

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

* Re: [PATCH RFC net v4 7/8] net/iucv: set RCV_SHUTDOWN under the socket spinlock
  2026-11-15  1:29 ` [PATCH RFC net v4 7/8] net/iucv: set RCV_SHUTDOWN under the socket spinlock Bryam Vargas via B4 Relay
@ 2026-11-16  1:29   ` sashiko-bot
  0 siblings, 0 replies; 17+ messages in thread
From: sashiko-bot @ 2026-11-16  1:29 UTC (permalink / raw)
  To: Bryam Vargas
  Cc: linux-s390, Heiko Carstens, Vasily Gorbik, Alexander Gordeev,
	Christian Borntraeger

> From: Bryam Vargas <hexlabsecurity@proton.me>
> 
> shutdown(SHUT_RD) sets RCV_SHUTDOWN and purges the receive queue under
> lock_sock() alone, and nothing orders that against a HiperSockets data
> frame: a frame that tested RCV_SHUTDOWN before the bit was set is
> queued after the purge, and recvmsg() returns its data after
> shutdown(SHUT_RD) returned.
> 
> Since the previous patch such a frame is received under sk_lock.slock,
> also while a task owns the socket; one that waited for the owner runs
> when it releases the socket, which shutdown() does only after the purge.
> On HiperSockets, set the bit and purge under that lock. recvmsg() sets
> the same bit under lock_sock() only, when it reads a zero-length record,
> which is how the peer's SHT arrives; that write and the frame's test are
> marked WRITE_ONCE() and READ_ONCE(), and only a frame the peer sent
> after that record can race with it. The z/VM branch is unchanged, less
> a line that was commented out.
> 
> Without the previous patch this one changes nothing: the frame then
> takes no lock that shutdown() could share with it.
> 
> Fixes: 82492a355fac ("af_iucv: add shutdown for HS transport")
> Reported-by: Hidayath Khan <hidayath@linux.ibm.com>
> Closes: https://lore.kernel.org/netdev/b3fea2ca-25ff-42c0-812c-f8cc6bb6667e@linux.ibm.com/
> Cc: stable@vger.kernel.org # 6.18.x, with the previous patch only
> Signed-off-by: Bryam Vargas <hexlabsecurity@proton.me>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261114-b4-disp-fc0634a3-v4-0-f96c506447ce@proton.me?part=7


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

* Re: [PATCH RFC net v4 8/8] net/iucv: return EOF to a reader parked when the peer closes
  2026-11-15  1:29 ` [PATCH RFC net v4 8/8] net/iucv: return EOF to a reader parked when the peer closes Bryam Vargas via B4 Relay
@ 2026-11-16  1:29   ` sashiko-bot
  0 siblings, 0 replies; 17+ messages in thread
From: sashiko-bot @ 2026-11-16  1:29 UTC (permalink / raw)
  To: Bryam Vargas
  Cc: linux-s390, Heiko Carstens, Vasily Gorbik, Alexander Gordeev,
	Christian Borntraeger

> From: Bryam Vargas <hexlabsecurity@proton.me>
> 
> A recvmsg() that starts after the peer closed returns 0. One that was
> already waiting returns -ENOTCONN: its wait ends on sk_state and does
> not ask whether anything is left to read. In a z/VM guest over
> HiperSockets that was every parked reader, 13627 of 13627 on net-next
> [1]. poll() does not report EPOLLRDHUP for the close either.
> 
> Return EOF from the wait too when the socket is IUCV_DISCONN with
> nothing left to read. As for a call that starts afterwards, it does not
> matter how the socket got there: the peer's FIN, a severed z/VM path, a
> transmit error, a window update that could not be sent or the device
> going down. EOF does not say that all the peer sent was read: a severed
> z/VM path drops the messages not yet received, for either call. Set
> RCV_SHUTDOWN where the HiperSockets FIN is applied, as TCP does: poll()
> then reports EPOLLRDHUP and EPOLLRDNORM for the close, and EPOLLHUP if
> the socket had shut down its own sending. The other ways still report
> none of the three.
> 
> Fixes: 561e036006dc ("[AF_IUCV]: Implementation of a skb backlog queue")
> Fixes: 3881ac441f64 ("af_iucv: add HiperSockets transport")
> Reported-by: Hidayath Khan <hidayath@linux.ibm.com>
> Closes: https://lore.kernel.org/netdev/82691101-92d7-4838-b16e-2e41a83f65d0@linux.ibm.com/
> Link: https://lore.kernel.org/netdev/51a4a40a-f34b-4d41-b049-69aa53f4dcf5@linux.ibm.com/ [1]
> Cc: stable@vger.kernel.org # 6.18.x
> Signed-off-by: Bryam Vargas <hexlabsecurity@proton.me>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261114-b4-disp-fc0634a3-v4-0-f96c506447ce@proton.me?part=8


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

end of thread, other threads:[~2026-11-16  1:29 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-15  1:29 [PATCH RFC net v4 0/8] net/iucv: serialize the HiperSockets receive path with the socket lock Bryam Vargas via B4 Relay
2026-11-15  1:29 ` [PATCH RFC net v4 1/8] net/iucv: keep the window given to the peer in a field of its own Bryam Vargas via B4 Relay
2026-11-16  1:29   ` sashiko-bot
2026-11-15  1:29 ` [PATCH RFC net v4 2/8] net/iucv: do not clear SEND_SHUTDOWN to send a control frame Bryam Vargas via B4 Relay
2026-11-16  1:29   ` sashiko-bot
2026-11-15  1:29 ` [PATCH RFC net v4 3/8] net/iucv: fail sendmsg() with EPIPE once sending is shut down Bryam Vargas via B4 Relay
2026-11-16  1:29   ` sashiko-bot
2026-11-15  1:29 ` [PATCH RFC net v4 4/8] net/iucv: run recvmsg() under the socket lock Bryam Vargas via B4 Relay
2026-11-16  1:29   ` sashiko-bot
2026-11-15  1:29 ` [PATCH RFC net v4 5/8] net/iucv: factor out the HiperSockets frame dispatch and state changes Bryam Vargas via B4 Relay
2026-11-16  1:29   ` sashiko-bot
2026-11-15  1:29 ` [PATCH RFC net v4 6/8] net/iucv: serialize HiperSockets receive with the socket lock Bryam Vargas via B4 Relay
2026-11-16  1:29   ` sashiko-bot
2026-11-15  1:29 ` [PATCH RFC net v4 7/8] net/iucv: set RCV_SHUTDOWN under the socket spinlock Bryam Vargas via B4 Relay
2026-11-16  1:29   ` sashiko-bot
2026-11-15  1:29 ` [PATCH RFC net v4 8/8] net/iucv: return EOF to a reader parked when the peer closes Bryam Vargas via B4 Relay
2026-11-16  1:29   ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox