af_vsock: separate wait data loop

Wait loop for data could be shared between SEQPACKET and STREAM
sockets, so move it to dedicated function. While moving the code
around, let's update an old comment.

Signed-off-by: Arseny Krasnov <arseny.krasnov@kaspersky.com>
Reviewed-by: Stefano Garzarella <sgarzare@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
Arseny Krasnov 2021-06-11 14:10:07 +03:00 committed by David S. Miller
parent a9e29e5511
commit b3f7fd5488
1 changed files with 87 additions and 75 deletions

View File

@ -1833,6 +1833,69 @@ out:
return err;
}
static int vsock_wait_data(struct sock *sk, struct wait_queue_entry *wait,
long timeout,
struct vsock_transport_recv_notify_data *recv_data,
size_t target)
{
const struct vsock_transport *transport;
struct vsock_sock *vsk;
s64 data;
int err;
vsk = vsock_sk(sk);
err = 0;
transport = vsk->transport;
while ((data = vsock_stream_has_data(vsk)) == 0) {
prepare_to_wait(sk_sleep(sk), wait, TASK_INTERRUPTIBLE);
if (sk->sk_err != 0 ||
(sk->sk_shutdown & RCV_SHUTDOWN) ||
(vsk->peer_shutdown & SEND_SHUTDOWN)) {
break;
}
/* Don't wait for non-blocking sockets. */
if (timeout == 0) {
err = -EAGAIN;
break;
}
if (recv_data) {
err = transport->notify_recv_pre_block(vsk, target, recv_data);
if (err < 0)
break;
}
release_sock(sk);
timeout = schedule_timeout(timeout);
lock_sock(sk);
if (signal_pending(current)) {
err = sock_intr_errno(timeout);
break;
} else if (timeout == 0) {
err = -EAGAIN;
break;
}
}
finish_wait(sk_sleep(sk), wait);
if (err)
return err;
/* Internal transport error when checking for available
* data. XXX This should be changed to a connection
* reset in a later change.
*/
if (data < 0)
return -ENOMEM;
return data;
}
static int
vsock_connectible_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
int flags)
@ -1912,67 +1975,18 @@ vsock_connectible_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
while (1) {
s64 ready;
prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
ready = vsock_stream_has_data(vsk);
if (ready == 0) {
if (sk->sk_err != 0 ||
(sk->sk_shutdown & RCV_SHUTDOWN) ||
(vsk->peer_shutdown & SEND_SHUTDOWN)) {
finish_wait(sk_sleep(sk), &wait);
break;
}
/* Don't wait for non-blocking sockets. */
if (timeout == 0) {
err = -EAGAIN;
finish_wait(sk_sleep(sk), &wait);
break;
}
err = transport->notify_recv_pre_block(
vsk, target, &recv_data);
if (err < 0) {
finish_wait(sk_sleep(sk), &wait);
break;
}
release_sock(sk);
timeout = schedule_timeout(timeout);
lock_sock(sk);
if (signal_pending(current)) {
err = sock_intr_errno(timeout);
finish_wait(sk_sleep(sk), &wait);
break;
} else if (timeout == 0) {
err = -EAGAIN;
finish_wait(sk_sleep(sk), &wait);
break;
}
} else {
ssize_t read;
finish_wait(sk_sleep(sk), &wait);
err = vsock_wait_data(sk, &wait, timeout, &recv_data, target);
if (err <= 0)
break;
if (ready < 0) {
/* Invalid queue pair content. XXX This should
* be changed to a connection reset in a later
* change.
*/
err = -ENOMEM;
goto out;
}
err = transport->notify_recv_pre_dequeue(
vsk, target, &recv_data);
err = transport->notify_recv_pre_dequeue(vsk, target,
&recv_data);
if (err < 0)
break;
read = transport->stream_dequeue(
vsk, msg,
len - copied, flags);
read = transport->stream_dequeue(vsk, msg, len - copied, flags);
if (read < 0) {
err = -ENOMEM;
break;
@ -1980,8 +1994,7 @@ vsock_connectible_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
copied += read;
err = transport->notify_recv_post_dequeue(
vsk, target, read,
err = transport->notify_recv_post_dequeue(vsk, target, read,
!(flags & MSG_PEEK), &recv_data);
if (err < 0)
goto out;
@ -1991,7 +2004,6 @@ vsock_connectible_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
target -= read;
}
}
if (sk->sk_err)
err = -sk->sk_err;