From: Bryam Vargas <hexl…@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 <wint…@linux.ibm.com>
Link: https://lore.kernel.org/all/2026…@proton.me/
Cc: stab…@vger.kernel.org # 6.18.x: f38bce9af9e2: net/iucv: take a private, writable frame before rewriting it in place
Cc: stab…@vger.kernel.org # 6.18.x
Signed-off-by: Bryam Vargas <hexl…@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/2026…@proton.me/
v3: https://lore.kernel.org/netdev/2026…@proton.me/
v2: https://lore.kernel.org/netdev/2026…@proton.me/
v1: https://lore.kernel.org/netdev/2026…@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