diff mbox series

[RFC,6/8] rxrpc: Use selected call in recvmsg()

Message ID 165599097309.1827880.4896993880358856739.stgit@warthog.procyon.org.uk (mailing list archive)
State RFC
Delegated to: Netdev Maintainers
Headers show
Series rxrpc: Multiqueue, sendfile, splice and call security | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for net, async
netdev/fixes_present fail Series targets non-next tree, but doesn't contain any Fixes tags
netdev/subject_prefix success Link
netdev/cover_letter success Series has a cover letter
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 3 maintainers not CCed: pabeni@redhat.com kuba@kernel.org edumazet@google.com
netdev/build_clang success Errors and warnings before: 3 this patch: 3
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch warning WARNING: 'retreive' may be misspelled - perhaps 'retrieve'? WARNING: Missing a blank line after declarations
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

David Howells June 23, 2022, 1:29 p.m. UTC
Allow an AF_RXRPC socket to be limited temporarily such that recvmsg() will
only retreive messages and data pertaining to one particular call.  The
call to be read from must be preselected with setsockopt():

	setsockopt(client, SOL_RXRPC, RXRPC_SELECT_CALL_FOR_RECV,
		   &call_id, sizeof(call_id));
	ret = recvmsg(client, &msg, 0);

The preselected call can be cleared by giving it a call ID of 0.  The
preselection is also automatically cleared if recvmsg returns the
completion state of the call (MSG_EOR will be set).

Signed-off-by: David Howells <dhowells@redhat.com>
---

 net/rxrpc/recvmsg.c |   59 ++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 56 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/net/rxrpc/recvmsg.c b/net/rxrpc/recvmsg.c
index 2b596e2172ce..3fc6bf8b1ff2 100644
--- a/net/rxrpc/recvmsg.c
+++ b/net/rxrpc/recvmsg.c
@@ -540,6 +540,51 @@  int rxrpc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
 		return -EAGAIN;
 	}
 
+	if (rx->selected_recv_call) {
+		/* The call to receive from was dictated by
+		 * setsockopt(RXRPC_SELECT_CALL_FOR_RECV).
+		 */
+		call = rx->selected_recv_call;
+		rxrpc_get_call(call, rxrpc_call_got);
+		if (!list_empty(&call->recvmsg_link)) {
+			write_lock_bh(&rx->recvmsg_lock);
+			goto use_this_call;
+		}
+
+		if (timeo == 0) {
+			ret = -EWOULDBLOCK;
+			call = NULL;
+			goto error_no_call;
+		}
+
+		release_sock(&rx->sk);
+
+		/* Wait for something to happen */
+		prepare_to_wait_exclusive(sk_sleep(&rx->sk), &wait,
+					  TASK_INTERRUPTIBLE);
+		for (;;) {
+			ret = sock_error(&rx->sk);
+			if (ret) {
+				rxrpc_put_call(call, rxrpc_call_put);
+				goto wait_error;
+			}
+
+			if (!list_empty(&call->recvmsg_link))
+				break;
+
+			if (signal_pending(current)) {
+				rxrpc_put_call(call, rxrpc_call_put);
+				goto wait_interrupted;
+			}
+			trace_rxrpc_recvmsg(NULL, rxrpc_recvmsg_wait,
+					    0, 0, 0, 0);
+			timeo = schedule_timeout(timeo);
+		}
+		finish_wait(sk_sleep(&rx->sk), &wait);
+		write_lock_bh(&rx->recvmsg_lock);
+		goto use_this_call;
+	}
+
 	if (list_empty(&rx->recvmsg_q)) {
 		ret = -EWOULDBLOCK;
 		if (timeo == 0) {
@@ -573,10 +618,11 @@  int rxrpc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
 	write_lock_bh(&rx->recvmsg_lock);
 	l = rx->recvmsg_q.next;
 	call = list_entry(l, struct rxrpc_call, recvmsg_link);
+	if (flags & MSG_PEEK)
+		rxrpc_get_call(call, rxrpc_call_got);
+use_this_call:
 	if (!(flags & MSG_PEEK))
 		list_del_init(&call->recvmsg_link);
-	else
-		rxrpc_get_call(call, rxrpc_call_got);
 	write_unlock_bh(&rx->recvmsg_lock);
 
 	trace_rxrpc_recvmsg(call, rxrpc_recvmsg_dequeue, 0, 0, 0, 0);
@@ -654,8 +700,15 @@  int rxrpc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
 		ret = rxrpc_recvmsg_term(call, msg);
 		if (ret < 0)
 			goto error_unlock_call;
-		if (!(flags & MSG_PEEK))
+		if (!(flags & MSG_PEEK)) {
+			struct rxrpc_call *old = call;
+			if (try_cmpxchg(&rx->selected_recv_call, &old, NULL))
+				rxrpc_put_call(call, rxrpc_call_put);
+			old = call;
+			if (try_cmpxchg(&rx->selected_send_call, &old, NULL))
+				rxrpc_put_call(call, rxrpc_call_put);
 			rxrpc_release_call(call);
+		}
 		msg->msg_flags |= MSG_EOR;
 		ret = 1;
 	}