Skip to content

Commit 50717a3

Browse files
Ursula Braundavem330
authored andcommitted
net/smc: nonblocking connect rework
For nonblocking sockets move the kernel_connect() from the connect worker into the initial smc_connect part to return kernel_connect() errors other than -EINPROGRESS to user space. Reviewed-by: Karsten Graul <[email protected]> Signed-off-by: Ursula Braun <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 6dc400a commit 50717a3

File tree

2 files changed

+47
-42
lines changed

2 files changed

+47
-42
lines changed

net/smc/af_smc.c

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,9 @@ static int smc_release(struct socket *sock)
134134
smc = smc_sk(sk);
135135

136136
/* cleanup for a dangling non-blocking connect */
137-
if (smc->connect_info && sk->sk_state == SMC_INIT)
137+
if (smc->connect_nonblock && sk->sk_state == SMC_INIT)
138138
tcp_abort(smc->clcsock->sk, ECONNABORTED);
139139
flush_work(&smc->connect_work);
140-
kfree(smc->connect_info);
141-
smc->connect_info = NULL;
142140

143141
if (sk->sk_state == SMC_LISTEN)
144142
/* smc_close_non_accepted() is called and acquires
@@ -452,6 +450,7 @@ static int smc_connect_fallback(struct smc_sock *smc, int reason_code)
452450
smc->use_fallback = true;
453451
smc->fallback_rsn = reason_code;
454452
smc_copy_sock_settings_to_clc(smc);
453+
smc->connect_nonblock = 0;
455454
if (smc->sk.sk_state == SMC_INIT)
456455
smc->sk.sk_state = SMC_ACTIVE;
457456
return 0;
@@ -491,6 +490,7 @@ static int smc_connect_abort(struct smc_sock *smc, int reason_code,
491490
mutex_unlock(&smc_client_lgr_pending);
492491

493492
smc_conn_free(&smc->conn);
493+
smc->connect_nonblock = 0;
494494
return reason_code;
495495
}
496496

@@ -633,6 +633,7 @@ static int smc_connect_rdma(struct smc_sock *smc,
633633
mutex_unlock(&smc_client_lgr_pending);
634634

635635
smc_copy_sock_settings_to_clc(smc);
636+
smc->connect_nonblock = 0;
636637
if (smc->sk.sk_state == SMC_INIT)
637638
smc->sk.sk_state = SMC_ACTIVE;
638639

@@ -671,6 +672,7 @@ static int smc_connect_ism(struct smc_sock *smc,
671672
mutex_unlock(&smc_server_lgr_pending);
672673

673674
smc_copy_sock_settings_to_clc(smc);
675+
smc->connect_nonblock = 0;
674676
if (smc->sk.sk_state == SMC_INIT)
675677
smc->sk.sk_state = SMC_ACTIVE;
676678

@@ -756,17 +758,30 @@ static void smc_connect_work(struct work_struct *work)
756758
{
757759
struct smc_sock *smc = container_of(work, struct smc_sock,
758760
connect_work);
759-
int rc;
761+
long timeo = smc->sk.sk_sndtimeo;
762+
int rc = 0;
760763

761-
lock_sock(&smc->sk);
762-
rc = kernel_connect(smc->clcsock, &smc->connect_info->addr,
763-
smc->connect_info->alen, smc->connect_info->flags);
764+
if (!timeo)
765+
timeo = MAX_SCHEDULE_TIMEOUT;
766+
lock_sock(smc->clcsock->sk);
764767
if (smc->clcsock->sk->sk_err) {
765768
smc->sk.sk_err = smc->clcsock->sk->sk_err;
766-
goto out;
767-
}
768-
if (rc < 0) {
769-
smc->sk.sk_err = -rc;
769+
} else if ((1 << smc->clcsock->sk->sk_state) &
770+
(TCPF_SYN_SENT | TCP_SYN_RECV)) {
771+
rc = sk_stream_wait_connect(smc->clcsock->sk, &timeo);
772+
if ((rc == -EPIPE) &&
773+
((1 << smc->clcsock->sk->sk_state) &
774+
(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)))
775+
rc = 0;
776+
}
777+
release_sock(smc->clcsock->sk);
778+
lock_sock(&smc->sk);
779+
if (rc != 0 || smc->sk.sk_err) {
780+
smc->sk.sk_state = SMC_CLOSED;
781+
if (rc == -EPIPE || rc == -EAGAIN)
782+
smc->sk.sk_err = EPIPE;
783+
else if (signal_pending(current))
784+
smc->sk.sk_err = -sock_intr_errno(timeo);
770785
goto out;
771786
}
772787

@@ -779,8 +794,6 @@ static void smc_connect_work(struct work_struct *work)
779794
smc->sk.sk_state_change(&smc->sk);
780795
else
781796
smc->sk.sk_write_space(&smc->sk);
782-
kfree(smc->connect_info);
783-
smc->connect_info = NULL;
784797
release_sock(&smc->sk);
785798
}
786799

@@ -813,26 +826,18 @@ static int smc_connect(struct socket *sock, struct sockaddr *addr,
813826

814827
smc_copy_sock_settings_to_clc(smc);
815828
tcp_sk(smc->clcsock->sk)->syn_smc = 1;
829+
if (smc->connect_nonblock) {
830+
rc = -EALREADY;
831+
goto out;
832+
}
833+
rc = kernel_connect(smc->clcsock, addr, alen, flags);
834+
if (rc && rc != -EINPROGRESS)
835+
goto out;
816836
if (flags & O_NONBLOCK) {
817-
if (smc->connect_info) {
818-
rc = -EALREADY;
819-
goto out;
820-
}
821-
smc->connect_info = kzalloc(alen + 2 * sizeof(int), GFP_KERNEL);
822-
if (!smc->connect_info) {
823-
rc = -ENOMEM;
824-
goto out;
825-
}
826-
smc->connect_info->alen = alen;
827-
smc->connect_info->flags = flags ^ O_NONBLOCK;
828-
memcpy(&smc->connect_info->addr, addr, alen);
829-
schedule_work(&smc->connect_work);
837+
if (schedule_work(&smc->connect_work))
838+
smc->connect_nonblock = 1;
830839
rc = -EINPROGRESS;
831840
} else {
832-
rc = kernel_connect(smc->clcsock, addr, alen, flags);
833-
if (rc)
834-
goto out;
835-
836841
rc = __smc_connect(smc);
837842
if (rc < 0)
838843
goto out;
@@ -1571,8 +1576,8 @@ static __poll_t smc_poll(struct file *file, struct socket *sock,
15711576
poll_table *wait)
15721577
{
15731578
struct sock *sk = sock->sk;
1574-
__poll_t mask = 0;
15751579
struct smc_sock *smc;
1580+
__poll_t mask = 0;
15761581

15771582
if (!sk)
15781583
return EPOLLNVAL;
@@ -1582,8 +1587,6 @@ static __poll_t smc_poll(struct file *file, struct socket *sock,
15821587
/* delegate to CLC child sock */
15831588
mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
15841589
sk->sk_err = smc->clcsock->sk->sk_err;
1585-
if (sk->sk_err)
1586-
mask |= EPOLLERR;
15871590
} else {
15881591
if (sk->sk_state != SMC_CLOSED)
15891592
sock_poll_wait(file, sock, wait);
@@ -1594,9 +1597,14 @@ static __poll_t smc_poll(struct file *file, struct socket *sock,
15941597
mask |= EPOLLHUP;
15951598
if (sk->sk_state == SMC_LISTEN) {
15961599
/* woken up by sk_data_ready in smc_listen_work() */
1597-
mask = smc_accept_poll(sk);
1600+
mask |= smc_accept_poll(sk);
1601+
} else if (smc->use_fallback) { /* as result of connect_work()*/
1602+
mask |= smc->clcsock->ops->poll(file, smc->clcsock,
1603+
wait);
1604+
sk->sk_err = smc->clcsock->sk->sk_err;
15981605
} else {
1599-
if (atomic_read(&smc->conn.sndbuf_space) ||
1606+
if ((sk->sk_state != SMC_INIT &&
1607+
atomic_read(&smc->conn.sndbuf_space)) ||
16001608
sk->sk_shutdown & SEND_SHUTDOWN) {
16011609
mask |= EPOLLOUT | EPOLLWRNORM;
16021610
} else {

net/smc/smc.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,18 +190,11 @@ struct smc_connection {
190190
u64 peer_token; /* SMC-D token of peer */
191191
};
192192

193-
struct smc_connect_info {
194-
int flags;
195-
int alen;
196-
struct sockaddr addr;
197-
};
198-
199193
struct smc_sock { /* smc sock container */
200194
struct sock sk;
201195
struct socket *clcsock; /* internal tcp socket */
202196
struct smc_connection conn; /* smc connection */
203197
struct smc_sock *listen_smc; /* listen parent */
204-
struct smc_connect_info *connect_info; /* connect address & flags */
205198
struct work_struct connect_work; /* handle non-blocking connect*/
206199
struct work_struct tcp_listen_work;/* handle tcp socket accepts */
207200
struct work_struct smc_listen_work;/* prepare new accept socket */
@@ -219,6 +212,10 @@ struct smc_sock { /* smc sock container */
219212
* started, waiting for unsent
220213
* data to be sent
221214
*/
215+
u8 connect_nonblock : 1;
216+
/* non-blocking connect in
217+
* flight
218+
*/
222219
struct mutex clcsock_release_lock;
223220
/* protects clcsock of a listen
224221
* socket

0 commit comments

Comments
 (0)