* [PATCH RFC net v5 01/10] net/iucv: record that a HiperSockets socket sent its SYN
2026-12-04 3:02 [PATCH RFC net v5 00/10] net/iucv: serialize the HiperSockets receive path with the socket lock Bryam Vargas via B4 Relay
@ 2026-12-04 3:02 ` Bryam Vargas via B4 Relay
2026-12-04 18:58 ` Alexandra Winter
2026-12-04 3:02 ` [PATCH RFC net v5 02/10] net/iucv: do not clear SEND_SHUTDOWN to send a control frame Bryam Vargas via B4 Relay
` (9 subsequent siblings)
10 siblings, 1 reply; 13+ messages in thread
From: Bryam Vargas via B4 Relay @ 2026-12-04 3:02 UTC (permalink / raw)
To: Paolo Abeni, David S. Miller, Hidayath Khan, Willem de Bruijn,
Alexandra Winter, Jakub Kicinski, Eric Dumazet
Cc: netdev, linux-kernel, Nagamani PV, linux-s390, Simon Horman,
Thorsten Winkler
From: Bryam Vargas <hexlabsecurity@proton.me>
A HiperSockets socket is IUCV_BOUND from bind() until the SYN|ACK
arrives, with or without a SYN on its way, and nothing tells the two
apart:
- SO_MSGLIMIT is accepted after the SYN carried msglimit to the peer as
the receive window, while recvmsg() counts the frames it reads against
the new value. When the limit is 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: WARN_ON(), -EFAULT, the rest of the
stream lost. When it is raised far enough, no window update is sent
and the peer's writer waits for one that only a data frame from us
would carry.
- A later connect() sends another SYN: one restarted after a signal, or
one that follows a non-blocking or timed-out connect(). The listener
makes a second child with the same names.
- A SYN|ACK or SYN|FIN is accepted by a socket that sent no SYN.
Record that the SYN was handed to the device. From then on SO_MSGLIMIT
fails with -EINVAL, as on a connected socket, and connect() sends no
other SYN: it waits for the reply to the first, or fails with -EALREADY
if it names another peer. A SYN|ACK or SYN|FIN for a socket that sent no
SYN is dropped. A SYN that was not sent records nothing.
Fixes: 3881ac441f64 ("af_iucv: add HiperSockets transport")
Fixes: 800c5eb7b5eb ("af_iucv: change net_device handling for HS transport")
Suggested-by: Alexandra Winter <wintera@linux.ibm.com>
Cc: stable@vger.kernel.org # 6.18.x
Signed-off-by: Bryam Vargas <hexlabsecurity@proton.me>
---
v5 (Alexandra's review of v4 1/8, [8] in the cover letter):
- new patch in this place. v4 1/8 kept the window given to the peer in
a field of its own. This one records only that the SYN left, as
Alexandra proposed: once it has, SO_MSGLIMIT fails, connect() sends no
other SYN, and a SYN|ACK or SYN|FIN for a socket that sent no SYN is
dropped. The window field and its helper are gone. 28 lines added and
3 removed, against 33 and 3
- a later connect() to the same peer waits for the reply to the first
SYN. Alexandra preferred that to refusing it [9]
v4: https://lore.kernel.org/netdev/20261114-b4-disp-fc0634a3-v4-1-f96c506447ce@proton.me/
v3: https://lore.kernel.org/netdev/20261110-b4-disp-2a6b4364-v3-1-35677b68e697@proton.me/
It goes first because the backlog of 6/10 counts frames against
msglimit, which must then be the window the SYN gave.
Emulated setup, with the reply to the SYN held back in the adapter
where the case needs it:
- SO_MSGLIMIT 8 when the SYN goes out, lowered to 2, minimum SO_RCVBUF,
the peer sends 8 frames. net reads 2, then takes the WARN_ON() in
iucv_sock_recvmsg() and returns -EFAULT. With the patch setsockopt()
fails with -EINVAL and all 8 are read.
- SO_MSGLIMIT 2 when the SYN goes out, raised to 8. On net the peer's
writer stops after 2 frames. With the patch setsockopt() fails and
all frames arrive.
- The same without a hold: a non-blocking connect() and SO_MSGLIMIT
right after it, 20 times, with the adapter interruption delayed. net
accepts the option with the SYN out in 20 of 20; with the series it
fails in 20 of 20, in one of them with the reply already in. With no
delay the reply is in before connect() returns and the option fails
on both, as on a connected socket.
- A second connect() to the same peer before the reply. net sends a
second SYN and the listener makes two children; with the patch one
SYN and one child, and the non-blocking call returns -EAGAIN as on
net.
- connect() interrupted by a signal and called again, connect()
restarted by the kernel (SO_SNDTIMEO 0, SA_RESTART), and connect()
called again after its timeout: two children on net in the first two
and three in the last. With the patch one child in each, and the
later call returns 0 when the SYN|ACK comes in.
- A second connect() that names another peer: net sends a SYN to each;
with the patch it fails with -EALREADY, no SYN goes to the second
peer and the first still connects.
- A forged SYN|ACK for a socket that only called bind(): net connects
it and reads the byte sent after it; with the patch the frame is
dropped and the socket stays IUCV_BOUND. A forged SYN|FIN: net leaves
the socket in IUCV_DISCONN and listen() fails with -EINVAL; with the
patch listen() works.
- connect() with the interface down: -ENETDOWN, nothing is recorded,
and SO_MSGLIMIT is still accepted afterwards, as on net.
With the refusal instead of the wait (-EALREADY for any connect() once
the SYN is out): the call made again after -EINTR or after the timeout
returns -EALREADY and the socket connects later; with SO_SNDTIMEO 0 and
SA_RESTART the one connect() the caller made returns -EALREADY. One
child in each.
Six pieces of this patch were each taken out or changed in a kernel of
its own, once on top of the patch alone and once on top of the series:
twelve kernels, which the cover letter counts as 6. Each of the twelve
fails the case that covers its piece.
What this patch costs: a socket sends one SYN, so a socket whose SYN
got no reply cannot connect any more. With the first SYN dropped in the
adapter and SO_SNDTIMEO 300 ms, net connects on the second connect();
with the patch the second call times out as well, and a new socket
bound to the same name connects. 10/10 gives the retry back when the
device reports that the SYN had no peer. A SYN that was delivered and
not answered, or lost with no report, still needs a new socket.
Not closed by this patch alone: a reply that comes in while the transmit
of the SYN fails is still accepted until 6/10 serializes the two.
connect() still tests sk_state before lock_sock(). A socket that got
connected in between finds its SYN recorded and sends no other.
---
include/net/iucv/af_iucv.h | 1 +
net/iucv/af_iucv.c | 30 +++++++++++++++++++++++++++---
2 files changed, 28 insertions(+), 3 deletions(-)
diff --git a/include/net/iucv/af_iucv.h b/include/net/iucv/af_iucv.h
index df85d19fbf84..ecddce19dcb2 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;
+ bool syn_sent; /* HiperSockets: our SYN left */
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..0c69fd661431 100644
--- a/net/iucv/af_iucv.c
+++ b/net/iucv/af_iucv.c
@@ -255,11 +255,17 @@ static int afiucv_hs_send(struct iucv_message *imsg, struct sock *sock,
phs_hdr->flags = phs_hdr->flags | AF_IUCV_FLAG_WIN;
}
+ /* Set before the transmit: the reply can be in before it returns */
+ if (flags == AF_IUCV_FLAG_SYN)
+ WRITE_ONCE(iucv->syn_sent, 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 (flags == AF_IUCV_FLAG_SYN)
+ WRITE_ONCE(iucv->syn_sent, false);
}
return net_xmit_eval(err);
@@ -770,6 +776,15 @@ static int iucv_sock_connect(struct socket *sock, struct sockaddr_unsized *addr,
lock_sock(sk);
+ /* A HiperSockets socket sends one SYN: wait for the reply to it */
+ if (iucv->syn_sent) {
+ err = 0;
+ if (memcmp(iucv->dst_user_id, sa->siucv_user_id, 8) ||
+ memcmp(iucv->dst_name, sa->siucv_name, 8))
+ err = -EALREADY;
+ goto sent;
+ }
+
/* Set the destination information */
memcpy(iucv->dst_user_id, sa->siucv_user_id, 8);
memcpy(iucv->dst_name, sa->siucv_name, 8);
@@ -778,6 +793,7 @@ static int iucv_sock_connect(struct socket *sock, struct sockaddr_unsized *addr,
err = iucv_send_ctrl(sock->sk, AF_IUCV_FLAG_SYN);
else
err = afiucv_path_connect(sock, addr);
+sent:
if (err)
goto done;
@@ -1544,8 +1560,14 @@ static int iucv_sock_setsockopt(struct socket *sock, int level, int optname,
break;
case SO_MSGLIMIT:
switch (sk->sk_state) {
- case IUCV_OPEN:
case IUCV_BOUND:
+ /* the SYN gave the peer msglimit as its window */
+ if (iucv->syn_sent) {
+ rc = -EINVAL;
+ break;
+ }
+ fallthrough;
+ case IUCV_OPEN:
if (val < 1 || val > U16_MAX)
rc = -EINVAL;
else
@@ -1947,7 +1969,8 @@ 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 (!iucv || sk->sk_state != IUCV_BOUND ||
+ !READ_ONCE(iucv->syn_sent)) {
kfree_skb(skb);
return NET_RX_SUCCESS;
}
@@ -1968,7 +1991,8 @@ 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 (!iucv || sk->sk_state != IUCV_BOUND ||
+ !READ_ONCE(iucv->syn_sent)) {
kfree_skb(skb);
return NET_RX_SUCCESS;
}
--
2.56.1
^ permalink raw reply related [flat|nested] 13+ messages in thread* Re: [PATCH RFC net v5 01/10] net/iucv: record that a HiperSockets socket sent its SYN
2026-12-04 3:02 ` [PATCH RFC net v5 01/10] net/iucv: record that a HiperSockets socket sent its SYN Bryam Vargas via B4 Relay
@ 2026-12-04 18:58 ` Alexandra Winter
0 siblings, 0 replies; 13+ messages in thread
From: Alexandra Winter @ 2026-12-04 18:58 UTC (permalink / raw)
To: hexlabsecurity, Paolo Abeni, David S. Miller, Hidayath Khan,
Willem de Bruijn, Jakub Kicinski, Eric Dumazet
Cc: netdev, linux-kernel, Nagamani PV, linux-s390, Simon Horman,
Thorsten Winkler
On 04.12.26 04:02, Bryam Vargas via B4 Relay wrote:
> From: Bryam Vargas <hexlabsecurity@proton.me>
>
> A HiperSockets socket is IUCV_BOUND from bind() until the SYN|ACK
> arrives, with or without a SYN on its way, and nothing tells the two
> apart:
Maybe sth like this is even clearer?:
..until the SYN|ACK arrives. The sk_state is the same whether or not a
SYN has already been sent. This leads to the following problems:
No strong feeling, but as this is still an RFC...
>
> - SO_MSGLIMIT is accepted after the SYN carried msglimit to the peer as
> the receive window, while recvmsg() counts the frames it reads against
> the new value. When the limit is 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: WARN_ON(), -EFAULT, the rest of the
> stream lost. When it is raised far enough, no window update is sent
> and the peer's writer waits for one that only a data frame from us
> would carry.
> - A later connect() sends another SYN: one restarted after a signal, or
> one that follows a non-blocking or timed-out connect(). The listener
> makes a second child with the same names.
> - A SYN|ACK or SYN|FIN is accepted by a socket that sent no SYN.
>
> Record that the SYN was handed to the device. From then on SO_MSGLIMIT
> fails with -EINVAL, as on a connected socket, and connect() sends no
> other SYN: it waits for the reply to the first, or fails with -EALREADY
> if it names another peer. A SYN|ACK or SYN|FIN for a socket that sent no
> SYN is dropped. A SYN that was not sent records nothing.
>
> Fixes: 3881ac441f64 ("af_iucv: add HiperSockets transport")
> Fixes: 800c5eb7b5eb ("af_iucv: change net_device handling for HS transport")
> Suggested-by: Alexandra Winter <wintera@linux.ibm.com>
> Cc: stable@vger.kernel.org # 6.18.x
> Signed-off-by: Bryam Vargas <hexlabsecurity@proton.me>
> ---
I tested the "SO_MSGLIMIT after connect()" issues and the both went away
with this fix.
I also reproduced the "Second SYN after non-blocking connect()", and it
also went away with this fix.
Tested-by: Alexandra Winter <wintera@linux.ibm.com>
Reviewed-by: Alexandra Winter <wintera@linux.ibm.com>
^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH RFC net v5 02/10] net/iucv: do not clear SEND_SHUTDOWN to send a control frame
2026-12-04 3:02 [PATCH RFC net v5 00/10] net/iucv: serialize the HiperSockets receive path with the socket lock Bryam Vargas via B4 Relay
2026-12-04 3:02 ` [PATCH RFC net v5 01/10] net/iucv: record that a HiperSockets socket sent its SYN Bryam Vargas via B4 Relay
@ 2026-12-04 3:02 ` Bryam Vargas via B4 Relay
2026-12-04 3:02 ` [PATCH RFC net v5 03/10] net/iucv: fail sendmsg() with EPIPE once sending is shut down Bryam Vargas via B4 Relay
` (8 subsequent siblings)
10 siblings, 0 replies; 13+ messages in thread
From: Bryam Vargas via B4 Relay @ 2026-12-04 3:02 UTC (permalink / raw)
To: Paolo Abeni, David S. Miller, Hidayath Khan, Willem de Bruijn,
Alexandra Winter, Jakub Kicinski, Eric Dumazet
Cc: netdev, linux-kernel, Nagamani PV, linux-s390, Simon Horman,
Thorsten Winkler
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>
---
v5: unchanged.
v4: https://lore.kernel.org/netdev/20261114-b4-disp-fc0634a3-v4-2-f96c506447ce@proton.me/
AI review of v3 3/7: https://lore.kernel.org/all/sashiko-outbox-210385@kernel.org/
New in v4. iucv_send_ctrl() clearing SEND_SHUTDOWN is what the AI
review of v3 3/7 is about (link above), 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 after
Alexandra's review. This is only the part about sk_shutdown; v2's
allocation, which ignored sk_sndbuf for a FIN and the first SHT, stays
out (cover letter, "Not in v5"). It goes ahead of 3/10 and 4/10: the
check 3/10 adds and the wait 4/10 moves out of the socket lock both rely
on the bit staying set.
The first item of the commit message, in the emulated setup: a task
reads from 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 8 to 55 cycles of a run on net and with 1/10, and in none
with this patch or with the series.
The wake-up the AI review describes is the one for 4/10's memory wait.
It can only be missed when two waits overlap, so a test kernel on top of
the series makes them overlap. The writer sleeps 1000 ms between
releasing the socket and its wait for send memory, and a window update
waits 1500 ms in iucv_send_ctrl() ahead of its allocation. 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, the writer sleeps
until its timeout, 9129 ms. Without the reader the writer returns on
both kernels when the 1000 ms are over.
By inspection:
- the second item of the commit 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
- of Hidayath's KCSAN 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
A KCSAN build of an earlier draft of v5 printed the first of those
pairs 3 times, all on a kernel without this patch and all on the clear
this patch removes. It never printed it with this patch, nor on net,
which has the same clear: too few cycles to show that the report is
gone.
---
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 0c69fd661431..4e69c7d6e3fb 100644
--- a/net/iucv/af_iucv.c
+++ b/net/iucv/af_iucv.c
@@ -380,26 +380,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.1
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH RFC net v5 03/10] net/iucv: fail sendmsg() with EPIPE once sending is shut down
2026-12-04 3:02 [PATCH RFC net v5 00/10] net/iucv: serialize the HiperSockets receive path with the socket lock Bryam Vargas via B4 Relay
2026-12-04 3:02 ` [PATCH RFC net v5 01/10] net/iucv: record that a HiperSockets socket sent its SYN Bryam Vargas via B4 Relay
2026-12-04 3:02 ` [PATCH RFC net v5 02/10] net/iucv: do not clear SEND_SHUTDOWN to send a control frame Bryam Vargas via B4 Relay
@ 2026-12-04 3:02 ` Bryam Vargas via B4 Relay
2026-12-04 3:02 ` [PATCH RFC net v5 04/10] net/iucv: run recvmsg() under the socket lock Bryam Vargas via B4 Relay
` (7 subsequent siblings)
10 siblings, 0 replies; 13+ messages in thread
From: Bryam Vargas via B4 Relay @ 2026-12-04 3:02 UTC (permalink / raw)
To: Paolo Abeni, David S. Miller, Hidayath Khan, Willem de Bruijn,
Alexandra Winter, Jakub Kicinski, Eric Dumazet
Cc: netdev, linux-kernel, Nagamani PV, linux-s390, Simon Horman,
Thorsten Winkler
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>
---
v5: unchanged.
v4: unchanged, was 2/7. 2/10 closes what v3's note left open here: a
caller of iucv_send_ctrl() without the socket lock hiding
SEND_SHUTDOWN from the new check.
v4: https://lore.kernel.org/netdev/20261114-b4-disp-fc0634a3-v4-3-f96c506447ce@proton.me/
v3: https://lore.kernel.org/netdev/20261110-b4-disp-2a6b4364-v3-2-35677b68e697@proton.me/
It goes before 4/10, 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 new SEND_SHUTDOWN check after the wait, it waits out SO_SNDTIMEO
again. With that check placed after the sk_state check, 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 4e69c7d6e3fb..c955ecd0c522 100644
--- a/net/iucv/af_iucv.c
+++ b/net/iucv/af_iucv.c
@@ -1053,10 +1053,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.1
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH RFC net v5 04/10] net/iucv: run recvmsg() under the socket lock
2026-12-04 3:02 [PATCH RFC net v5 00/10] net/iucv: serialize the HiperSockets receive path with the socket lock Bryam Vargas via B4 Relay
` (2 preceding siblings ...)
2026-12-04 3:02 ` [PATCH RFC net v5 03/10] net/iucv: fail sendmsg() with EPIPE once sending is shut down Bryam Vargas via B4 Relay
@ 2026-12-04 3:02 ` Bryam Vargas via B4 Relay
2026-12-04 3:02 ` [PATCH RFC net v5 05/10] net/iucv: factor out the HiperSockets frame dispatch and state changes Bryam Vargas via B4 Relay
` (6 subsequent siblings)
10 siblings, 0 replies; 13+ messages in thread
From: Bryam Vargas via B4 Relay @ 2026-12-04 3:02 UTC (permalink / raw)
To: Paolo Abeni, David S. Miller, Hidayath Khan, Willem de Bruijn,
Alexandra Winter, Jakub Kicinski, Eric Dumazet
Cc: netdev, linux-kernel, Nagamani PV, linux-s390, Simon Horman,
Thorsten Winkler
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>
---
v5: no change of its own. recvmsg() counts the frames it reads against
msglimit again, as on net: 1/10 has no field for the window.
v4: no code change, was 3/7. Two changes to the commit message, both
for what 2/10 does under this patch:
- the second item was the sk_shutdown bit lost around a window update.
That is 2/10'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 3/7, link below)
v3: new as a patch of its own, the half of v2's lock patch that does
not touch the softirq side. Without 6/10 the softirq still changes
sk_state under a task that owns the socket, as on net.
v4: https://lore.kernel.org/netdev/20261114-b4-disp-fc0634a3-v4-4-f96c506447ce@proton.me/
v3: https://lore.kernel.org/netdev/20261110-b4-disp-2a6b4364-v3-3-35677b68e697@proton.me/
AI review of v3 3/7: https://lore.kernel.org/all/sashiko-outbox-210385@kernel.org/
In the emulated setup: two readers on one socket under MTTCG, three runs
of 838 messages each. On net, on the kernel with 1/10 and 2/10 and on
the one with 1/10 to 3/10, every run has reads that go backwards, 12 to
25 of them; on the one with 1/10 to 4/10 and with the series there are
none.
The second item of the commit message is by inspection. The test that
loses the bit on net (notes of 2/10) loses none with 2/10 or with the
series, 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/10, with 1/10 and
2/10 and with 1/10 to 3/10, 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/10 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, 6209 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/10, with 1/10 and 2/10 and with 1/10 to
3/10, the shutdown() waits for the socket lock until the writer's
timeout, 8022 to 8297 ms, and the writer returns -EAGAIN. With 1/10 to
4/10 and with the series, shutdown() returns at once and the writer
returns -EPIPE, 201 to 203 ms after it blocked. The case where the
wake-up could be missed is under 2/10.
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/10.
---
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 c955ecd0c522..9c4bac738e67 100644
--- a/net/iucv/af_iucv.c
+++ b/net/iucv/af_iucv.c
@@ -1039,7 +1039,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)
@@ -1266,6 +1276,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)
{
@@ -1277,24 +1335,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;
@@ -1343,8 +1402,9 @@ static int iucv_sock_recvmsg(struct socket *sock, struct msghdr *msg,
atomic_inc(&iucv->msg_recv);
if (atomic_read(&iucv->msg_recv) > iucv->msglimit) {
WARN_ON(1);
- iucv_sock_close(sk);
- return -EFAULT;
+ __iucv_sock_close(sk);
+ err = -EFAULT;
+ goto err_unlock;
}
}
@@ -1386,7 +1446,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:
@@ -1394,7 +1455,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.1
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH RFC net v5 05/10] net/iucv: factor out the HiperSockets frame dispatch and state changes
2026-12-04 3:02 [PATCH RFC net v5 00/10] net/iucv: serialize the HiperSockets receive path with the socket lock Bryam Vargas via B4 Relay
` (3 preceding siblings ...)
2026-12-04 3:02 ` [PATCH RFC net v5 04/10] net/iucv: run recvmsg() under the socket lock Bryam Vargas via B4 Relay
@ 2026-12-04 3:02 ` Bryam Vargas via B4 Relay
2026-12-04 3:02 ` [PATCH RFC net v5 06/10] net/iucv: serialize HiperSockets receive with the socket lock Bryam Vargas via B4 Relay
` (5 subsequent siblings)
10 siblings, 0 replies; 13+ messages in thread
From: Bryam Vargas via B4 Relay @ 2026-12-04 3:02 UTC (permalink / raw)
To: Paolo Abeni, David S. Miller, Hidayath Khan, Willem de Bruijn,
Alexandra Winter, Jakub Kicinski, Eric Dumazet
Cc: netdev, linux-kernel, Nagamani PV, linux-s390, Simon Horman,
Thorsten Winkler
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>
---
v5: no change of its own. The SYN|ACK and SYN|FIN callbacks it touches
carry the test that 1/10 adds.
v4: unchanged, was 4/7.
v4: https://lore.kernel.org/netdev/20261114-b4-disp-fc0634a3-v4-5-f96c506447ce@proton.me/
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/10 applies there.
The kernel with 1/10 to 5/10 gives the same result as the one with 1/10
to 4/10 in every test: the 53 default ones and the 28 opt-in ones
(window, strays, connect(), leaks).
---
net/iucv/af_iucv.c | 155 ++++++++++++++++++++++++++++++++++-------------------
1 file changed, 99 insertions(+), 56 deletions(-)
diff --git a/net/iucv/af_iucv.c b/net/iucv/af_iucv.c
index 9c4bac738e67..bc60fcaca1b0 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,
@@ -2034,18 +2072,14 @@ 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 ||
- !READ_ONCE(iucv->syn_sent)) {
+ if (!sk || sk->sk_state != IUCV_BOUND ||
+ !READ_ONCE(iucv_sk(sk)->syn_sent)) {
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;
@@ -2056,17 +2090,14 @@ 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 ||
- !READ_ONCE(iucv->syn_sent)) {
+ if (!sk || sk->sk_state != IUCV_BOUND ||
+ !READ_ONCE(iucv_sk(sk)->syn_sent)) {
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;
@@ -2077,19 +2108,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;
@@ -2157,6 +2184,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
@@ -2237,42 +2315,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.1
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH RFC net v5 06/10] net/iucv: serialize HiperSockets receive with the socket lock
2026-12-04 3:02 [PATCH RFC net v5 00/10] net/iucv: serialize the HiperSockets receive path with the socket lock Bryam Vargas via B4 Relay
` (4 preceding siblings ...)
2026-12-04 3:02 ` [PATCH RFC net v5 05/10] net/iucv: factor out the HiperSockets frame dispatch and state changes Bryam Vargas via B4 Relay
@ 2026-12-04 3:02 ` Bryam Vargas via B4 Relay
2026-12-04 3:02 ` [PATCH RFC net v5 07/10] net/iucv: set RCV_SHUTDOWN under the socket spinlock Bryam Vargas via B4 Relay
` (4 subsequent siblings)
10 siblings, 0 replies; 13+ messages in thread
From: Bryam Vargas via B4 Relay @ 2026-12-04 3:02 UTC (permalink / raw)
To: Paolo Abeni, David S. Miller, Hidayath Khan, Willem de Bruijn,
Alexandra Winter, Jakub Kicinski, Eric Dumazet
Cc: netdev, linux-kernel, Nagamani PV, linux-s390, Simon Horman,
Thorsten Winkler
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 each time its wait tests the
state, so a connect() that is non-blocking, or whose wait ends on a
timeout or a signal, still sees a reply that is already in.
sendmsg() applies a recorded FIN the same way in its wait for the
window, so it does not send to a peer whose FIN is 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 a
SYN|ACK 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: any frame for a closed socket; a SYN|ACK
or SYN|FIN outside IUCV_BOUND or for a socket that sent no SYN; a FIN,
data, SHT or WIN frame for a socket neither connected nor about to be;
and data, SHT and WIN frames behind a recorded FIN. The frames freed are
NO_SOCKET drops, except a FIN and a WIN without data, which are
consumed. 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>
---
v5 (Hidayath's review of v4, [10] in the cover letter):
- netdev_put() is under if (dev) in afiucv_hs_backlog_rcv(). A data,
SHT or WIN frame from the backlog has no device held, and v4 passed
the address of a tracker that was never set
- sk_max_ack_backlog is read with READ_ONCE() in
afiucv_hs_backlog_add() and written with WRITE_ONCE() in
iucv_sock_listen(): the softirq reads it while a task owns the
listener. Annotation only
v5 (changes that follow from the new 1/10):
- a SYN|ACK or SYN|FIN for a socket that sent no SYN is a stray, with
or without an owner. afiucv_hs_apply() drops a recorded reply if the
SYN is no longer recorded: its transmit failed after the reply came
in
- the backlog counts frames against msglimit; v4 used the window field
of its 1/8 there
v5 (AI review of v4, [7] in the cover letter):
- connect() applies a reply recorded for its SYN each time its wait
tests the state. v4 applied it once, before the wait. A reply
recorded after the task woke up for the last time, on a timeout or a
signal, was left for the release_sock() at the end of connect(), and
by then connect() had chosen -EAGAIN or -EINTR
- sendmsg() applies a recorded FIN each time its wait for the window
tests the state. v4 tested sk_state with the FIN still recorded and
sent its data to a peer whose FIN was already in
- data, SHT and WIN frames are strays unless the socket is connected
or a SYN|ACK is recorded for it, the rule v3 gave the FIN. v4 let them
wait in the backlog of a socket in IUCV_BOUND with a SYN|FIN recorded,
and in the backlog of a listener behind a waiting SYN, and freed them
when the owner released the socket. afiucv_hs_defer() now looks at a
recorded SYN|ACK, not at any recorded reply. With or without an owner,
such a frame is now a NO_SOCKET drop in afiucv_hs_rcv(); net and v4
free it in the data callback with a plain kfree_skb(). A WIN without
data is the exception, see the fifth item
- every frame for a closed socket is a stray. In v4, as on net, a
SYN|ACK that had found a socket in IUCV_BOUND before its close() still
connected it after the close(), when no task owned the socket
- a stray WIN without data is consumed like a stray FIN, not dropped.
With the third item it would otherwise show as a NO_SOCKET drop
whenever the peer's window update races with a close()
- the commit message names the drop reason of a stray. The notes below
list what a caller sees differently from net. The two cases v4 left
at "by inspection" are now tested: a pfmemalloc skb refused by the
backlog, and the SOCK_ZAPPED check in afiucv_hs_apply()
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/10 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 reply recorded for 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, while 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
- based on net, with nothing under it
v4: https://lore.kernel.org/netdev/20261114-b4-disp-fc0634a3-v4-6-f96c506447ce@proton.me/
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/
What a caller sees differently from net. A control frame that comes in
while a call owns the socket is applied when the call releases it;
connect() itself applies the reply recorded for its SYN, and sendmsg()
the peer's FIN. The other calls go on with the state they read, and so
does connect() for a FIN. The cases measured, on net and on v5:
- accept() returns -EAGAIN for a SYN that came in during a non-blocking
call, or in the last pass of a wait whose timeout expired, and the
next accept() returns the child; net returns the child from the
first call
- connect() returns 0 when the peer's FIN came in behind its SYN|ACK,
and the next recv() returns EOF; net returns -ECONNREFUSED
- shutdown(SHUT_WR) returns 0 and its SHT leaves after the peer's FIN;
net returns -ENOTCONN and sends none
- shutdown(SHUT_RD) returns 0 for the peer's FIN and purges what the
peer sent before it; net returns -ENOTCONN and the data can still be
read
- shutdown(SHUT_WR) on a socket whose SYN is out returns 0 and sends no
SHT, whether the reply that comes in is a SYN|ACK or a SYN|FIN; net
connects the socket and sends the SHT, or returns -ENOTCONN. With the
SYN|ACK, the peer's child gets no EOF from the shutdown()
- listen() on a socket whose SYN is out returns 0 and the socket
listens, whether the reply that comes in is a SYN|ACK or a SYN|FIN;
net returns -EINVAL
- send() and a blocking recv() on a socket whose SYN is out see
IUCV_BOUND and return -ENOTCONN; on net, send() sends and recv()
returns the peer's data. For a refused SYN, recv() returns -ENOTCONN
here too, and EOF on net
- setsockopt(SO_MSGLIMIT) on a socket whose SYN is out returns -EINVAL
on both: here from 1/10's check, on net because the socket is no
longer IUCV_BOUND by then
- send() on a connected socket returns -ECONNRESET for the peer's FIN;
net returns -ENOTCONN. No data leaves on either
- a non-blocking recv() returns -EAGAIN for the peer's FIN and the next
recv() EOF; net returns EOF at once
- close() of a socket whose SYN is out sends no FIN for a SYN|ACK that
came in during the call, and the peer keeps a connected child; net
sees a connected socket and sends its FIN. close() of a connected
socket sends its FIN to a peer whose FIN came in during the call; net
sends none
- a SYN in a frame that the device took from the memory reserves is
refused while a task owns the listener; net answers it
- data, SHT and WIN frames that the socket cannot use are NO_SOCKET
drops; net frees them with a plain kfree_skb()
For all but the last two, the frame has to come in after the call took
the socket and before it checked the state. When the frame comes in
after the call has returned, net gives the same result as the series,
except for SO_MSGLIMIT, which 1/10 refuses once the SYN is out. In the
shutdown(SHUT_WR) test with the peer's FIN and in the accept() test with
a timeout, net already gives the series' result when the frame comes in
right after the call has checked the state. close() is covered by its
own test below. The tests are below, after those for the items of the AI
review.
The items of the AI review of v4 repro on v4 as sent and are gone here.
net passes the connect(), sendmsg() and data tests below, since it
applies each frame as it arrives, and fails the closed-socket test as v4
does.
connect(): a test kernel delays the connecting task for 600 ms after the
lock_sock() of a wait that is about to end, and the emulated adapter
delivers the reply to the SYN during that delay. With SO_SNDTIMEO
expired, v4 returns -EAGAIN and the socket is connected; with a signal
pending, -EINTR and connected; with a SYN|FIN as the reply, -EAGAIN and
the socket is disconnected (send() returns -ENOTCONN, recv() 0). The
series returns 0, 0 and -ECONNREFUSED. With the reply applied only once,
before the wait, or not by connect() at all, all three results are the
same as on v4. A reply that comes in after the wait's last test of the
state still connects the socket, after connect() has failed: with the
same delay put after that test, connect() returns -EAGAIN and a byte
then goes each way, on net, on v4 and here.
sendmsg(): a send() of one page, a userfaultfd page, parks in its copy
from user space, and the peer closes meanwhile. v4 returns 4096 and the
page leaves after the peer's FIN came in. The series returns -ECONNRESET
and sends nothing. The same send(), non-blocking and with the window
full, returns -EAGAIN on v4, and -ECONNRESET on net and with the series.
A blocking send() that waits for the window has the 600 ms delay of the
connect() test above in the last pass of its wait, and the peer closes
during the delay: v4 returns -EAGAIN with SO_SNDTIMEO expired and -EINTR
with a signal pending; net and the series return -ECONNRESET for both.
In the three window-full cases, no data leaves and the next
send() returns -ENOTCONN. With the FIN applied only once, after the
wait, the three return what v4 returns.
Data for a socket that is not connected, with the socket held by a task
parked in setsockopt(): three one-byte frames behind a SYN|FIN to a
socket in IUCV_BOUND, and three to a listener behind a SYN. v4 keeps
them in the backlog and frees them when the task releases the socket.
The series frees them on arrival, as NO_SOCKET drops, and counts none in
the backlog. With one forged SHT in place of the three bytes behind the
SYN|FIN, the SHT is a NO_SOCKET drop on arrival too; net frees it with a
plain kfree_skb(). On v4 and with the series, connect() returns
-ECONNREFUSED and the listener accepts its child. With only the stray
rule taken out, the three for the listener wait again, and the three
behind the SYN|FIN are freed on arrival by the data callback, since
afiucv_hs_defer() no longer looks at a SYN|FIN.
The closed socket: a test kernel delays a SYN|ACK for 400 ms between the
lookup and bh_lock_sock(), and the socket is closed 150 ms into the
delay. On net and v4 the socket is IUCV_CONNECTED when it is destroyed.
With the series the SYN|ACK is a NO_SOCKET drop and the socket is
destroyed in IUCV_BOUND; the same with the two vCPUs under MTTCG, three
boots of each kernel. This needs the lookup to come before close()
clears hs_dev: a SYN|ACK that comes later finds no socket, on any
kernel.
The tests behind the list "What a caller sees differently from net".
A read parked in its copy to user space is not in the list: with a
userfaultfd page again, the peer closes; the read returns its byte and
the next one EOF. The window update it owes is sent after the
peer's FIN; net sends that frame after a FIN too (cover letter, "Not in
v5").
The last pass of a wait whose timeout expired: a test kernel delays the
task for 600 ms right after it takes the lock again, and the frame
arrives during that delay. recvmsg() returns -EAGAIN for a FIN and the
next call EOF at once. accept() returns -EAGAIN for a SYN; the peer's
connect() returns 0 once the lock is released, and the next accept()
returns the child. net answers that SYN under the owner, so there the
first accept() returns the child. With the SYN arriving after accept()
found its queue empty, right before its test of the timeout, net
returns -EAGAIN too and the next accept() returns the child. For both
calls -EAGAIN means "not yet" and the next call sees the frame. The
series changes sendmsg()'s wait but not these two. sendmsg() has to
apply the FIN before it sends anyway. And its wait comes after the
copy from user space, so the state it tested could be as old as the
copy. recvmsg() and accept() test the state right after they take the
lock again.
The frame can also come in after the task has set itself to
TASK_INTERRUPTIBLE in the wait and before it first tests the wait's
condition; that test now applies a recorded frame. A 600 ms delay
there: sendmsg() returns -ECONNRESET for the peer's FIN and connect() 0
for the reply to its SYN, on net and here, with no atomic-sleep or
lockdep report. In the sendmsg() test the task was still
TASK_INTERRUPTIBLE when the condition applied the FIN; in the connect()
test it had been woken during the delay.
connect() again, with the 600 ms delay after the lock_sock() of a wait
that is about to end, and a peer that accepts and closes at once, so
that the peer's SYN|ACK and FIN both arrive during the delay: net
returns -ECONNREFUSED, v4 -EAGAIN, and the series 0; on all three the
next recv() returns EOF and send() -ENOTCONN. With the FIN sent after
connect() has returned, all three return 0 and then EOF.
The same 600 ms right after lock_sock(), with the frame arriving during
the delay:
- shutdown(SHUT_WR) returns 0 and its SHT leaves after the peer's FIN,
while net returns -ENOTCONN and sends none. With the delay put after
net's tests of the state, right before it sends the SHT, net returns 0
too and its SHT leaves after the FIN
- send() and recv() on a socket whose SYN is out: send() returns
-ENOTCONN and sends nothing, recv() -ENOTCONN. The socket connects
when the call releases it, and the next recv() returns the byte the
peer sent. On net, send() sends its byte and recv() returns the
peer's byte. setsockopt(SO_MSGLIMIT) with a new limit of 2 returns
-EINVAL here and on net; the limit stays 8 and the peer's next five
messages all arrive
- shutdown(SHUT_WR) on a socket whose SYN is out: 0 and no SHT leaves.
The socket connects when the call releases it, and a recv() of the
peer's child with a 300 ms timeout returns -EAGAIN. On net the SHT
leaves and that recv() returns 0
- send() on a connected socket, with the peer's FIN: -ECONNRESET, and
-ENOTCONN on net; no data leaves on either
- a non-blocking recv() with the peer's FIN: -EAGAIN and the next one
0; net returns 0 at once
- a non-blocking accept() with a SYN: -EAGAIN, the peer's connect()
returns once the lock is released, and the next accept() returns the
child; net returns the child from the first call
- listen() on a socket whose SYN is out returns 0 and the socket
listens; the SYN|ACK is not applied and the peer's next frame is a
NO_SOCKET drop. net connects the socket there and listen() returns
-EINVAL. With the SYN|ACK arriving after listen() has returned, net
and the series give the same result
- shutdown(SHUT_RD) with the peer's byte and FIN: 0, and the next
recv() returns 0 without the byte. net returns -ENOTCONN, and recv()
returns the byte, then 0
- the calls on a socket whose SYN is out, with the peer refusing the SYN
(a SYN|FIN reply): shutdown(SHUT_WR) 0 with no SHT, and a later send()
-EPIPE; listen() 0, and the socket listens; a blocking recv()
-ENOTCONN, then EOF; setsockopt() -EINVAL. net returns -ENOTCONN,
-EINVAL, EOF and -EINVAL
A 400 ms delay in close() before it looks at sk_state, with the SYN|ACK
arriving during the delay: net sees a connected socket and sends its
FIN; here the reply is recorded, close() sends no FIN and the peer keeps
a connected child. On net, a SYN|ACK still connects the socket when it
finds it after close() has looked at the state (the closed-socket test
above: IUCV_CONNECTED when destroyed). In that test no FIN leaves, on
net or here, and the peer's child stays connected. With the socket
connected and the peer's FIN arriving during the delay in close(),
close() here sends its own FIN to a peer that has closed; net sends
none.
A frame that the device took from the memory reserves (pfmemalloc). A
test kernel marks every frame for one socket that way in
afiucv_hs_rcv(), and turns on the static key that the first
SOCK_MEMALLOC socket turns on. A SYN for a listener that a task owns is
refused on arrival and connect() returns -ECONNREFUSED, while net
answers it. A data byte behind a recorded SYN|ACK is a SOCKET_BACKLOG
drop on arrival; the SYN|ACK, marked too, is recorded and connect()
returns 0. With the check in afiucv_hs_backlog_add() taken out, that
kernel stops at the BUG_ON() in __sk_backlog_rcv() when the socket is
released. The BUG_ON() needs the key. With the key off, the backlog does
not look at the mark. With the key on, the core drops a marked frame of
this protocol before afiucv_hs_rcv() sees it, so only a frame queued
before the key was turned on reaches the BUG_ON() (by inspection). The
check in afiucv_hs_backlog_add() is the one sk_add_backlog() makes.
With the SOCK_ZAPPED check in afiucv_hs_apply() taken out and the delay
in close() above, the socket is IUCV_CONNECTED when it is destroyed.
Strays are freed in afiucv_hs_rcv(): a FIN and a WIN without data with
consume_skb(), as net frees them, 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, and neither does a WIN
without data sent the same way; with either one dropped like the other
strays, the series leaves one NO_SOCKET drop. A data byte sent the same
way is a drop on every kernel: a plain kfree_skb() on net and on v4,
NO_SOCKET here.
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 at all, and connect() applying it in its
wait and not only once before it
- sendmsg() applying the FIN at all, and sendmsg() applying it in its
wait and not only once after it
- in afiucv_hs_stray(): either half of its rule for a FIN, its rule for
data that the socket cannot use, that rule and the change to
afiucv_hs_defer() together, its rule for frames behind a FIN, its
SOCK_ZAPPED check, and the whole function
- the SOCK_ZAPPED check in afiucv_hs_apply()
- afiucv_hs_apply() dropping a recorded reply whose SYN is no longer
recorded
- the pfmemalloc check of the backlog
- the stray FIN consumed, and the stray WIN consumed
For seven of them, the test that fails runs on a test kernel or with a
delay: the two for data the socket cannot use, on a kernel that counts
the strays and the frames that wait in the backlog; the pfmemalloc
check, on the kernel that marks the frames; the two SOCK_ZAPPED checks
and connect() applying the reply in its wait, with the delays above;
and the reply whose SYN is no longer recorded, on a kernel that holds
the SYN for 600 ms before its transmit and then drops it, with a forged
SYN|ACK sent in meanwhile.
Of the 53 default tests, the kernel with 1/10 to 6/10 fails six: the
five in the cover letter and the parked reader's -ENOTCONN, which 8/10
changes.
The tests in v4's notes were run again on net and on v5, with the same
results except in one test, described last: the listener in the first
paragraph of the commit message, the SYN for a socket that does not
listen, a listener whose accept queue is full (1056 SYNs refused leave
1056 sockets in use, on net and with the series), the two items of the
AI review of v2, and the device reference of a waiting SYN on a build
with CONFIG_NET_DEV_REFCNT_TRACKER=y. v4's notes are at the link above.
The test that gives a different result now is the test of the backlog's
limit: its three bytes came behind a SYN|ACK that answers no SYN, and
since 1/10 that frame and the bytes behind it are dropped. The limit is
msglimit plus one. It is reached only on the kernel without 1/10's
SO_MSGLIMIT check, with msglimit lowered from 8 to 2 after the SYN: 5 of
the peer's 8 frames are SOCKET_BACKLOG drops there.
---
include/net/iucv/af_iucv.h | 7 +
net/iucv/af_iucv.c | 412 ++++++++++++++++++++++++++++++++++++++++++---
2 files changed, 391 insertions(+), 28 deletions(-)
diff --git a/include/net/iucv/af_iucv.h b/include/net/iucv/af_iucv.h
index ecddce19dcb2..a94d52df44e9 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 hs_pending_window;
bool syn_sent; /* HiperSockets: our SYN left */
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 bc60fcaca1b0..bf97b8c62eb2 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,81 @@ 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. A socket whose SYN could
+ * not be sent after all takes no reply to it.
+ *
+ * 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 (!iucv->syn_sent)
+ pending &= ~AF_IUCV_PEND_SYNREPLY;
+ 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_apply_now() - apply recorded control frames before the release
+ * @sk: the socket, owned by the caller
+ * @mask: the AF_IUCV_PEND_* bits to apply
+ *
+ * For an owner about to test sk_state: a frame recorded since it took the
+ * socket is not in sk_state yet. The bits of @mask are cleared even if
+ * sk_state does not let them apply. A caller that passes the FIN alone must
+ * know that no SYN|ACK is recorded, or the FIN is lost, and that no data
+ * waits in the backlog, or the FIN overtakes it: see iucv_sock_send_ready().
+ *
+ * Context: Process context, socket owned by the caller.
+ */
+static void afiucv_hs_apply_now(struct sock *sk, u8 mask)
+{
+ spin_lock_bh(&sk->sk_lock.slock);
+ afiucv_hs_apply(sk, mask);
+ spin_unlock_bh(&sk->sk_lock.slock);
+}
+
+/**
+ * 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 +203,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),
@@ -193,6 +266,24 @@ static int iucv_sock_in_state(struct sock *sk, int state, int state2)
return (sk->sk_state == state || sk->sk_state == state2);
}
+/**
+ * iucv_sock_connect_done() - test whether connect() has its answer
+ * @sk: the socket, owned by the caller
+ *
+ * The reply to our SYN may be recorded and not applied: connect() owns the
+ * socket while it sends the SYN, and again each time its wait wakes up.
+ * Apply the reply first, or connect() fails with it already in.
+ *
+ * Context: Process context, socket owned by the caller.
+ * Return: true if the socket is connected or was refused.
+ */
+static bool iucv_sock_connect_done(struct sock *sk)
+{
+ if (iucv_sk(sk)->transport == AF_IUCV_TRANS_HIPER)
+ afiucv_hs_apply_now(sk, AF_IUCV_PEND_SYNREPLY);
+ return iucv_sock_in_state(sk, IUCV_CONNECTED, IUCV_DISCONN);
+}
+
/**
* iucv_below_msglim() - function to check if messages can be sent
* @sk: sock structure
@@ -214,6 +305,28 @@ static inline int iucv_below_msglim(struct sock *sk)
(atomic_read(&iucv->pendings) <= 0));
}
+/**
+ * iucv_sock_send_ready() - test whether sendmsg() can stop waiting
+ * @sk: the socket, owned by the caller
+ *
+ * The peer's FIN may be recorded and not applied: sendmsg() owns the socket
+ * while it copies the message, and again each time its wait wakes up. Apply
+ * the FIN first: otherwise sendmsg() sends behind it, or gives up its wait
+ * with the FIN already in. The FIN overtakes no data: the backlog was empty
+ * when sendmsg() took the socket, and a connected socket handles data on
+ * arrival. Nor is a SYN|ACK recorded: sendmsg() saw the socket connected.
+ *
+ * Context: Process context, socket owned by the caller.
+ * Return: true if the window has room, the socket is not connected or
+ * sending is shut down.
+ */
+static bool iucv_sock_send_ready(struct sock *sk)
+{
+ if (iucv_sk(sk)->transport == AF_IUCV_TRANS_HIPER)
+ afiucv_hs_apply_now(sk, AF_IUCV_PEND_FIN);
+ return iucv_below_msglim(sk) || (sk->sk_shutdown & SEND_SHUTDOWN);
+}
+
/*
* iucv_sock_wake_msglim() - Wake up thread waiting on msg limit
*/
@@ -550,6 +663,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);
@@ -835,8 +950,7 @@ static int iucv_sock_connect(struct socket *sock, struct sockaddr_unsized *addr,
goto done;
if (sk->sk_state != IUCV_CONNECTED)
- err = iucv_sock_wait(sk, iucv_sock_in_state(sk, IUCV_CONNECTED,
- IUCV_DISCONN),
+ err = iucv_sock_wait(sk, iucv_sock_connect_done(sk),
sock_sndtimeo(sk, flags & O_NONBLOCK));
if (sk->sk_state == IUCV_DISCONN || sk->sk_state == IUCV_CLOSED)
@@ -865,7 +979,8 @@ static int iucv_sock_listen(struct socket *sock, int backlog)
if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
goto done;
- sk->sk_max_ack_backlog = backlog;
+ /* read in softirq by afiucv_hs_backlog_add() */
+ WRITE_ONCE(sk->sk_max_ack_backlog, backlog);
sk->sk_ack_backlog = 0;
sk->sk_state = IUCV_LISTEN;
err = 0;
@@ -1101,8 +1216,7 @@ 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) ||
- (sk->sk_shutdown & SEND_SHUTDOWN), timeo);
+ err = iucv_sock_wait(sk, iucv_sock_send_ready(sk), timeo);
if (err)
goto fail;
@@ -1322,7 +1436,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
@@ -2021,7 +2136,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) {
@@ -2031,7 +2145,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;
}
@@ -2061,7 +2174,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;
@@ -2078,9 +2190,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;
}
@@ -2096,9 +2206,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;
}
@@ -2114,10 +2222,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;
}
@@ -2132,6 +2238,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;
@@ -2152,6 +2259,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;
@@ -2184,12 +2292,184 @@ 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. Every frame for a closed socket is
+ * one: the test is SOCK_ZAPPED, since __iucv_sock_close() leaves IUCV_BOUND
+ * as it is. A frame finds such a socket only if it was looked up before
+ * close() cleared hs_dev. A SYN is one outside IUCV_LISTEN; a full accept
+ * queue is not tested here. A SYN|ACK or SYN|FIN is one outside IUCV_BOUND
+ * or for a socket that sent no SYN.
+ * A FIN, data, SHT or WIN frame is one unless the socket is connected or a
+ * recorded SYN|ACK is about to connect it. Data, SHT and WIN are strays too
+ * once the peer's FIN is recorded: the socket stays IUCV_CONNECTED until
+ * the FIN is applied. A frame with any other flags is one in every state.
+ *
+ * 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);
+
+ if (sock_flag(sk, SOCK_ZAPPED))
+ return true;
+
+ switch (flags) {
+ case 0:
+ case AF_IUCV_FLAG_SHT:
+ case AF_IUCV_FLAG_WIN:
+ if (iucv->hs_pending & AF_IUCV_PEND_FIN)
+ return true;
+ fallthrough;
+ 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 ||
+ !READ_ONCE(iucv->syn_sent);
+ }
+ 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:
+ *
+ * - a SYN|ACK 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_SYNACK) ||
+ 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 SYN|ACK 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 = READ_ONCE(sk->sk_max_ack_backlog) + 1;
+ } else {
+ count = &iucv->hs_backlog_data;
+ limit = iucv->msglimit + 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)
@@ -2235,6 +2515,73 @@ 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;
+
+ afiucv_hs_apply_now(sk, AF_IUCV_PEND_SYNREPLY);
+ 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();
+ if (dev)
+ 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
@@ -2306,18 +2653,27 @@ 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 ||
+ (trans_hdr->flags == AF_IUCV_FLAG_WIN &&
+ skb->len == sizeof(struct af_iucv_trans_hdr)))
+ consume_skb(skb); /* ignored, not dropped */
+ 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.1
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH RFC net v5 07/10] net/iucv: set RCV_SHUTDOWN under the socket spinlock
2026-12-04 3:02 [PATCH RFC net v5 00/10] net/iucv: serialize the HiperSockets receive path with the socket lock Bryam Vargas via B4 Relay
` (5 preceding siblings ...)
2026-12-04 3:02 ` [PATCH RFC net v5 06/10] net/iucv: serialize HiperSockets receive with the socket lock Bryam Vargas via B4 Relay
@ 2026-12-04 3:02 ` Bryam Vargas via B4 Relay
2026-12-04 3:02 ` [PATCH RFC net v5 08/10] net/iucv: return EOF to a reader parked when the peer closes Bryam Vargas via B4 Relay
` (3 subsequent siblings)
10 siblings, 0 replies; 13+ messages in thread
From: Bryam Vargas via B4 Relay @ 2026-12-04 3:02 UTC (permalink / raw)
To: Paolo Abeni, David S. Miller, Hidayath Khan, Willem de Bruijn,
Alexandra Winter, Jakub Kicinski, Eric Dumazet
Cc: netdev, linux-kernel, Nagamani PV, linux-s390, Simon Horman,
Thorsten Winkler
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>
---
v5: unchanged. The counts below are from a run of v5. One more test:
with no udelay() added to the kernel, and with the adapter interruption
delayed by 20 to 200 us and taken on a CPU picked at random, the
shutdown(SHUT_RD) test read data after the shutdown once in 800 cycles
on v1, once in 1600 with 1/10 to 6/10 of v5, and in none of 1600 with
the series.
v4: the commit message no longer says that iucv_send_ctrl() clears and
restores SEND_SHUTDOWN in this byte; since 2/10 it does not. No code
change.
v4: https://lore.kernel.org/netdev/20261114-b4-disp-fc0634a3-v4-7-f96c506447ce@proton.me/
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 race there, on hs_dev,
is fixed by 1/2 of the two fixes posted for net on 28 Oct ([3] 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/10, with 1/10 and 2/10, with 1/10 to 3/10 and with 1/10 to 4/10 read
data after shutdown(SHUT_RD) in 0 to 8 cycles of a run, 58 of 4000 in
all. The series reads none. Nor does the series minus this spinlock:
since 6/10, only a frame that takes sk_lock.slock between shutdown()'s
lock_sock() and its write gets through, and none of these runs had one;
the run with 1/10 to 6/10 and the interruption delayed, above, had one
in 1600 cycles. With udelay(50) added after lock_sock() in shutdown()
and after the frame's test, the series minus this spinlock reads late
data in 5 to 18 of 200 cycles and the series in none.
On the hardware, Hidayath could not hit that race 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, what shows the spinlock at work is the udelay() kernels
above and that one late read in 1600 cycles.
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 bf97b8c62eb2..b9eecebbdff1 100644
--- a/net/iucv/af_iucv.c
+++ b/net/iucv/af_iucv.c
@@ -1513,7 +1513,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)) {
@@ -1715,16 +1715,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 */
@@ -2265,7 +2277,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.1
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH RFC net v5 08/10] net/iucv: return EOF to a reader parked when the peer closes
2026-12-04 3:02 [PATCH RFC net v5 00/10] net/iucv: serialize the HiperSockets receive path with the socket lock Bryam Vargas via B4 Relay
` (6 preceding siblings ...)
2026-12-04 3:02 ` [PATCH RFC net v5 07/10] net/iucv: set RCV_SHUTDOWN under the socket spinlock Bryam Vargas via B4 Relay
@ 2026-12-04 3:02 ` Bryam Vargas via B4 Relay
2026-12-04 3:02 ` [PATCH RFC net v5 09/10] net/iucv: decrement pendings when a pending frame's transmit fails Bryam Vargas via B4 Relay
` (2 subsequent siblings)
10 siblings, 0 replies; 13+ messages in thread
From: Bryam Vargas via B4 Relay @ 2026-12-04 3:02 UTC (permalink / raw)
To: Paolo Abeni, David S. Miller, Hidayath Khan, Willem de Bruijn,
Alexandra Winter, Jakub Kicinski, Eric Dumazet
Cc: netdev, linux-kernel, Nagamani PV, linux-s390, Simon Horman,
Thorsten Winkler
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>
---
v5: no code change. The last paragraph below names sendmsg() too: with
6/10 of v5 it applies a recorded FIN. The -ENOTCONN counts of the first
test are from this version's run; half of its cycles race the close, so
the counts move from run to run.
v4: unchanged, was 7/7. The last paragraph of v3's note is gone: since
2/10 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 behavior
are in one patch: the wait returns EOF, and the FIN sets RCV_SHUTDOWN.
v4: https://lore.kernel.org/netdev/20261114-b4-disp-fc0634a3-v4-8-f96c506447ce@proton.me/
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 check
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 34 on
net, 28 with 1/10 to 3/10 and 24 with 1/10 to 7/10; EOF in all 40 with
the series
- 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 the series each case reports the same EPOLLIN,
EPOLLRDHUP and EPOLLHUP bits as an AF_UNIX socketpair does 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 the series
The interface going down and the TX error change sk_state with no socket
lock, so the check 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 check, the
first call returns 0 with the byte still queued. FWIW, by inspection
net returns -ENOTCONN the same way when the skb lands between its own
two checks, a narrower race window. The window update that could not be
sent is by inspection too.
After 6/10, the FIN is applied under sk_lock.slock on every path: by
the softirq when there is no owner, or by the owner from release_cb or
from sendmsg(). 7/10 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 b9eecebbdff1..8336f7056d49 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);
}
@@ -1440,8 +1442,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)
{
@@ -1459,7 +1462,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.1
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH RFC net v5 09/10] net/iucv: decrement pendings when a pending frame's transmit fails
2026-12-04 3:02 [PATCH RFC net v5 00/10] net/iucv: serialize the HiperSockets receive path with the socket lock Bryam Vargas via B4 Relay
` (7 preceding siblings ...)
2026-12-04 3:02 ` [PATCH RFC net v5 08/10] net/iucv: return EOF to a reader parked when the peer closes Bryam Vargas via B4 Relay
@ 2026-12-04 3:02 ` Bryam Vargas via B4 Relay
2026-12-04 3:02 ` [PATCH RFC net v5 10/10] net/iucv: forget a SYN that the device reports unreachable Bryam Vargas via B4 Relay
2026-12-04 11:05 ` [PATCH RFC net v5 00/10] net/iucv: serialize the HiperSockets receive path with the socket lock Hidayath Khan
10 siblings, 0 replies; 13+ messages in thread
From: Bryam Vargas via B4 Relay @ 2026-12-04 3:02 UTC (permalink / raw)
To: Paolo Abeni, David S. Miller, Hidayath Khan, Willem de Bruijn,
Alexandra Winter, Jakub Kicinski, Eric Dumazet
Cc: netdev, linux-kernel, Nagamani PV, linux-s390, Simon Horman,
Thorsten Winkler
From: Bryam Vargas <hexlabsecurity@proton.me>
When the HiperSockets device keeps a frame for later, qeth reports it as
TX_NOTIFY_PENDING and af_iucv counts it in iucv->pendings: sendmsg()
sends nothing while that count is above zero. Only TX_NOTIFY_DELAYED_OK
takes the count down. When the device ends such a frame with an error,
the count stays up. On a connected socket, that changes nothing, since
the error disconnects it. When the frame is a SYN, the socket stays
IUCV_BOUND and a later connect() can still connect it. For as long as it
is connected, sendmsg() returns -EAGAIN, at once or after the send
timeout, or blocks if there is no timeout, and poll() does not report
EPOLLOUT.
Take the count down for the two delayed error notifications as well.
A pending frame that qeth gives up on when it drains its queues is
reported as TX_NOTIFY_GENERALERROR, like a frame that was never
pending, and is still not counted down.
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>
---
v5: new. The bug was found while reviewing 10/10, which makes it
reachable again; net has it too. The patch applies to net without the
rest of the series.
Emulated setup. The adapter is told to report the SYN as pending and to
end it later with an error, so that qeth gives TX_NOTIFY_PENDING and
then the delayed notification. The first connect() times out. The peer
is listening the whole time. After the second connect() the socket
sends one byte, with a send timeout of 1 s:
- pending, then unreachable (completion code 16). net: the second
connect() returns 0 and send() returns -EAGAIN; the count is still 1.
net with this patch alone: the byte arrives. With 1/10 to 8/10, and
with 1/10 to 9/10, the second connect() sends no SYN and returns
-EAGAIN. With the series the byte arrives. With the series but
without this patch, send() returns -EAGAIN again
- pending, then another error (completion code 1). net: send() returns
-EAGAIN. net with this patch alone: the byte arrives. With only the
TX_NOTIFY_DELAYED_UNREACHABLE label of this patch: -EAGAIN again.
With the series the second connect() sends no SYN: 10/10 forgets a
SYN only when it is reported unreachable
I can't tell whether a real device reports a SYN as pending and then
fails it. If it does not, the patch changes nothing that can be seen:
on a connected socket the error disconnects it, and the count is not
read again.
Not fixed: when qeth drains its pending buffers it reports them with
TX_NOTIFY_GENERALERROR, the same as a frame that was never pending, so
af_iucv cannot take the count down there. Tested with a device recovery
(1 written to its recover attribute) while the SYN is pending: on net,
with or without this patch, the second connect() returns 0 and send()
returns -EAGAIN.
---
net/iucv/af_iucv.c | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/net/iucv/af_iucv.c b/net/iucv/af_iucv.c
index 8336f7056d49..1f1f6f698648 100644
--- a/net/iucv/af_iucv.c
+++ b/net/iucv/af_iucv.c
@@ -2725,6 +2725,11 @@ static void afiucv_hs_callback_txnotify(struct sock *sk, enum iucv_tx_notify n)
if (atomic_dec_return(&iucv->pendings) <= 0)
iucv_sock_wake_msglim(sk);
break;
+ case TX_NOTIFY_DELAYED_UNREACHABLE:
+ case TX_NOTIFY_DELAYED_GENERALERROR:
+ /* counted when the device reported the frame as pending */
+ atomic_dec(&iucv->pendings);
+ fallthrough;
default:
atomic_dec(&iucv->skbs_in_xmit);
if (sk->sk_state == IUCV_CONNECTED) {
--
2.56.1
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH RFC net v5 10/10] net/iucv: forget a SYN that the device reports unreachable
2026-12-04 3:02 [PATCH RFC net v5 00/10] net/iucv: serialize the HiperSockets receive path with the socket lock Bryam Vargas via B4 Relay
` (8 preceding siblings ...)
2026-12-04 3:02 ` [PATCH RFC net v5 09/10] net/iucv: decrement pendings when a pending frame's transmit fails Bryam Vargas via B4 Relay
@ 2026-12-04 3:02 ` Bryam Vargas via B4 Relay
2026-12-04 11:05 ` [PATCH RFC net v5 00/10] net/iucv: serialize the HiperSockets receive path with the socket lock Hidayath Khan
10 siblings, 0 replies; 13+ messages in thread
From: Bryam Vargas via B4 Relay @ 2026-12-04 3:02 UTC (permalink / raw)
To: Paolo Abeni, David S. Miller, Hidayath Khan, Willem de Bruijn,
Alexandra Winter, Jakub Kicinski, Eric Dumazet
Cc: netdev, linux-kernel, Nagamani PV, linux-s390, Simon Horman,
Thorsten Winkler
From: Bryam Vargas <hexlabsecurity@proton.me>
When a HiperSockets device cannot reach the target of a frame, qeth
reports TX_NOTIFY_UNREACHABLE, or its delayed form. af_iucv acts on it
only for a connected socket. For a SYN it is ignored: connect() waits
until its timeout and returns -EAGAIN. A HiperSockets socket now sends
only one SYN ("net/iucv: record that a HiperSockets socket sent its
SYN"), so a later connect() on it sends nothing and waits again, for a
reply that cannot come.
Clear syn_sent when the SYN is reported unreachable. A socket in
IUCV_BOUND has sent no other frame, so the report is about the SYN. The
next connect() sends a SYN again, and SO_MSGLIMIT can be set before it.
The connect() that sent the SYN still waits for its timeout. Any other
notification of a transmit error leaves the flag set: it does not say
whether the peer got the SYN, and a second SYN could make a second
child.
The flag is now also written outside the socket lock: read it with
READ_ONCE() where the owner reads it.
Cc: stable@vger.kernel.org # 6.18.x, with the whole series
Signed-off-by: Bryam Vargas <hexlabsecurity@proton.me>
---
v5: new. It is a patch of its own, and last, so that 1/10 stays as
small as Alexandra asked. It needs 1/10. Without 9/10 it still applies,
but then a SYN that was first reported as pending leaves a socket that
connects and cannot send.
checkpatch asks for a Fixes: tag because of the stable line. There is
none to give: what this patch repairs comes with 1/10, and on net
without 1/10 it changes nothing a caller can see.
Why: with one SYN per socket (1/10), a socket whose SYN was not answered
cannot connect again. The device reports one cause of that: qeth
maps the completion codes 4, 16, 17 and 18 to TX_NOTIFY_UNREACHABLE,
and af_iucv ignores the notification unless the socket is connected.
What I could find about callers that retry on the same socket: the
z/VSE Linux Fast Path has Linux on the listening side; iucvconn in
s390-tools calls connect() once and exits on any error; af_iucv(7)
says of -EAGAIN that "the application might try again after some
time", and a connect() that times out returns -EAGAIN. After a
SYN|FIN the socket is IUCV_DISCONN and cannot connect again on net
either.
Emulated setup. The peer's user ID is not on the LAN at first, so the
adapter reports the SYN unreachable (completion code 4). The first
connect() has a timeout of 300 ms. "Without this patch" is 1/10 to
9/10; 1/10 to 8/10 gives the same results:
- two connect() calls, the peer never there. Two SYNs leave the socket
on net and with the series, one without this patch. Both calls
return -EAGAIN on every kernel
- the peer's device comes up and the peer listens, then connect()
again on the same socket. net and the series: it returns 0, the peer
has one child, and a byte sent afterwards arrives. Without this
patch: -EAGAIN, no second SYN, no child
- the SYN reported as pending first and then unreachable (the setup
under 9/10, the peer listening): with the series the second connect()
returns 0 and the byte arrives. With the series but without 9/10 it
returns 0 and send() returns -EAGAIN
- the first connect() takes its whole timeout on every kernel, 300 to
307 ms. The notification does not wake it
- SO_MSGLIMIT lowered from 8 to 2 after the notification, then the
peer comes up as above: setsockopt() returns 0 on net and with the
series, and the second connect() gives the peer a window of 2.
Without this patch: -EINVAL
- a forged SYN|ACK and one byte of data after the notification, and no
second connect(). net, and the series without this patch: the socket
is connected and reads the byte. With the series the SYN|ACK answers
no SYN and is dropped, and the socket stays IUCV_BOUND
- TX_NOTIFY_GENERALERROR for the SYN (the adapter refuses it with
another code): the flag stays. Two connect() calls send one SYN with
the series, as without this patch; net sends two. That notification
does not say whether the peer got the frame
- two kernels with this patch changed. One also forgets on
TX_NOTIFY_OK: connect() to a listening peer times out, because the
SYN|ACK finds no SYN recorded, and two calls leave the peer with two
children. The other forgets on any transmit error: it sends a second
SYN after TX_NOTIFY_GENERALERROR
- two tests under 1/10, a second connect() before the reply and
connect() restarted by the kernel, give one SYN and one child with
this patch as without it
- a KCSAN build of the series ran the first three cases above, and the
case of 9/10 with the other error, once each: no report
I can't tell from here which of the four codes a real device gives for a
user ID that is not on the LAN, nor whether it gives one when the peer's
device is there and af_iucv is not loaded. Alexandra, Hidayath: could
one of you check a connect() to a user ID that does not exist?
Not changed: a SYN that the device delivered and nobody answered, or
one lost with no report, still leaves a socket that cannot connect.
connect() still waits for its timeout after the notification; it is
not woken.
---
net/iucv/af_iucv.c | 22 +++++++++++++++-------
1 file changed, 15 insertions(+), 7 deletions(-)
diff --git a/net/iucv/af_iucv.c b/net/iucv/af_iucv.c
index 1f1f6f698648..8f0bc2d806e6 100644
--- a/net/iucv/af_iucv.c
+++ b/net/iucv/af_iucv.c
@@ -93,8 +93,8 @@ static void afiucv_hs_fin(struct sock *sk)
* @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. A socket whose SYN could
- * not be sent after all takes no reply to it.
+ * __iucv_sock_close() leaves IUCV_BOUND as it is. A socket that has no SYN
+ * out takes no reply to one.
*
* Context: Process context, under sk_lock.slock.
*/
@@ -106,7 +106,7 @@ static void afiucv_hs_apply(struct sock *sk, u8 mask)
iucv->hs_pending &= ~pending;
if (sock_flag(sk, SOCK_ZAPPED))
return;
- if (!iucv->syn_sent)
+ if (!READ_ONCE(iucv->syn_sent))
pending &= ~AF_IUCV_PEND_SYNREPLY;
if ((pending & AF_IUCV_PEND_SYNACK) && sk->sk_state == IUCV_BOUND)
afiucv_hs_synack(sk, iucv->hs_pending_window);
@@ -930,8 +930,8 @@ static int iucv_sock_connect(struct socket *sock, struct sockaddr_unsized *addr,
lock_sock(sk);
- /* A HiperSockets socket sends one SYN: wait for the reply to it */
- if (iucv->syn_sent) {
+ /* The SYN is out: wait for the reply to it */
+ if (READ_ONCE(iucv->syn_sent)) {
err = 0;
if (memcmp(iucv->dst_user_id, sa->siucv_user_id, 8) ||
memcmp(iucv->dst_name, sa->siucv_name, 8))
@@ -1806,7 +1806,7 @@ static int iucv_sock_setsockopt(struct socket *sock, int level, int optname,
switch (sk->sk_state) {
case IUCV_BOUND:
/* the SYN gave the peer msglimit as its window */
- if (iucv->syn_sent) {
+ if (READ_ONCE(iucv->syn_sent)) {
rc = -EINVAL;
break;
}
@@ -2326,7 +2326,7 @@ static int afiucv_hs_callback_rx(struct sock *sk, struct sk_buff *skb)
* as it is. A frame finds such a socket only if it was looked up before
* close() cleared hs_dev. A SYN is one outside IUCV_LISTEN; a full accept
* queue is not tested here. A SYN|ACK or SYN|FIN is one outside IUCV_BOUND
- * or for a socket that sent no SYN.
+ * or for a socket that has no SYN out.
* A FIN, data, SHT or WIN frame is one unless the socket is connected or a
* recorded SYN|ACK is about to connect it. Data, SHT and WIN are strays too
* once the peer's FIN is recorded: the socket stays IUCV_CONNECTED until
@@ -2735,6 +2735,14 @@ static void afiucv_hs_callback_txnotify(struct sock *sk, enum iucv_tx_notify n)
if (sk->sk_state == IUCV_CONNECTED) {
sk->sk_state = IUCV_DISCONN;
sk->sk_state_change(sk);
+ } else if ((n == TX_NOTIFY_UNREACHABLE ||
+ n == TX_NOTIFY_DELAYED_UNREACHABLE) &&
+ sk->sk_state == IUCV_BOUND) {
+ /* An IUCV_BOUND socket has sent nothing but its SYN.
+ * Forget a SYN that is reported unreachable: the next
+ * connect() sends another.
+ */
+ WRITE_ONCE(iucv->syn_sent, false);
}
}
--
2.56.1
^ permalink raw reply related [flat|nested] 13+ messages in thread* Re: [PATCH RFC net v5 00/10] net/iucv: serialize the HiperSockets receive path with the socket lock
2026-12-04 3:02 [PATCH RFC net v5 00/10] net/iucv: serialize the HiperSockets receive path with the socket lock Bryam Vargas via B4 Relay
` (9 preceding siblings ...)
2026-12-04 3:02 ` [PATCH RFC net v5 10/10] net/iucv: forget a SYN that the device reports unreachable Bryam Vargas via B4 Relay
@ 2026-12-04 11:05 ` Hidayath Khan
10 siblings, 0 replies; 13+ messages in thread
From: Hidayath Khan @ 2026-12-04 11:05 UTC (permalink / raw)
To: hexlabsecurity, Paolo Abeni, David S. Miller, Willem de Bruijn,
Alexandra Winter, Jakub Kicinski, Eric Dumazet
Cc: netdev, linux-kernel, Nagamani PV, linux-s390, Simon Horman,
Thorsten Winkler
Hi Bryam,
The v4 patch series was clean on my z/VM runs, as posted. For v5, the
plan is a
full pass on a z/VM guest and on an LPAR, and I would like to hold any
respin
until those numbers and this review are both in.
Thanks for the two 6/10 changes I asked for: netdev_put() under if (dev),
and the READ_ONCE()/WRITE_ONCE() pair on sk_max_ack_backlog. Both look
right to me now.
On your asks: Alexandra is re-running the two cases she reproduced with
1/10, and I will take the connect() to a user ID that does not exist - the
scenario does not exist yet.
Let us keep the series to the stage 1 socket locking work, the pre-existing
and out-of-scope items can be taken up separately once this has landed.
One question on 1/10, for you and Alexandra. A SYN lost with no device
report leaves a socket that cannot connect again, and af_iucv(7) tells
applications
to retry after -EAGAIN. Should a connect() that times out clear syn_sent?
Thanks,
Hidayath
^ permalink raw reply [flat|nested] 13+ messages in thread