forked from Minki/linux
372ee16386
Inside the kafs filesystem it is possible to occasionally have a call processed and terminated before we've had a chance to check whether we need to clean up the rx queue for that call because afs_send_simple_reply() ends the call when it is done, but this is done in a workqueue item that might happen to run to completion before afs_deliver_to_call() completes. Further, it is possible for rxrpc_kernel_send_data() to be called to send a reply before the last request-phase data skb is released. The rxrpc skb destructor is where the ACK processing is done and the call state is advanced upon release of the last skb. ACK generation is also deferred to a work item because it's possible that the skb destructor is not called in a context where kernel_sendmsg() can be invoked. To this end, the following changes are made: (1) kernel_rxrpc_data_consumed() is added. This should be called whenever an skb is emptied so as to crank the ACK and call states. This does not release the skb, however. kernel_rxrpc_free_skb() must now be called to achieve that. These together replace rxrpc_kernel_data_delivered(). (2) kernel_rxrpc_data_consumed() is wrapped by afs_data_consumed(). This makes afs_deliver_to_call() easier to work as the skb can simply be discarded unconditionally here without trying to work out what the return value of the ->deliver() function means. The ->deliver() functions can, via afs_data_complete(), afs_transfer_reply() and afs_extract_data() mark that an skb has been consumed (thereby cranking the state) without the need to conditionally free the skb to make sure the state is correct on an incoming call for when the call processor tries to send the reply. (3) rxrpc_recvmsg() now has to call kernel_rxrpc_data_consumed() when it has finished with a packet and MSG_PEEK isn't set. (4) rxrpc_packet_destructor() no longer calls rxrpc_hard_ACK_data(). Because of this, we no longer need to clear the destructor and put the call before we free the skb in cases where we don't want the ACK/call state to be cranked. (5) The ->deliver() call-type callbacks are made to return -EAGAIN rather than 0 if they expect more data (afs_extract_data() returns -EAGAIN to the delivery function already), and the caller is now responsible for producing an abort if that was the last packet. (6) There are many bits of unmarshalling code where: ret = afs_extract_data(call, skb, last, ...); switch (ret) { case 0: break; case -EAGAIN: return 0; default: return ret; } is to be found. As -EAGAIN can now be passed back to the caller, we now just return if ret < 0: ret = afs_extract_data(call, skb, last, ...); if (ret < 0) return ret; (7) Checks for trailing data and empty final data packets has been consolidated as afs_data_complete(). So: if (skb->len > 0) return -EBADMSG; if (!last) return 0; becomes: ret = afs_data_complete(call, skb, last); if (ret < 0) return ret; (8) afs_transfer_reply() now checks the amount of data it has against the amount of data desired and the amount of data in the skb and returns an error to induce an abort if we don't get exactly what we want. Without these changes, the following oops can occasionally be observed, particularly if some printks are inserted into the delivery path: general protection fault: 0000 [#1] SMP Modules linked in: kafs(E) af_rxrpc(E) [last unloaded: af_rxrpc] CPU: 0 PID: 1305 Comm: kworker/u8:3 Tainted: G E 4.7.0-fsdevel+ #1303 Hardware name: ASUS All Series/H97-PLUS, BIOS 2306 10/09/2014 Workqueue: kafsd afs_async_workfn [kafs] task: ffff88040be041c0 ti: ffff88040c070000 task.ti: ffff88040c070000 RIP: 0010:[<ffffffff8108fd3c>] [<ffffffff8108fd3c>] __lock_acquire+0xcf/0x15a1 RSP: 0018:ffff88040c073bc0 EFLAGS: 00010002 RAX: 6b6b6b6b6b6b6b6b RBX: 0000000000000000 RCX: ffff88040d29a710 RDX: 0000000000000000 RSI: 0000000000000000 RDI: ffff88040d29a710 RBP: ffff88040c073c70 R08: 0000000000000001 R09: 0000000000000001 R10: 0000000000000001 R11: 0000000000000000 R12: 0000000000000000 R13: 0000000000000000 R14: ffff88040be041c0 R15: ffffffff814c928f FS: 0000000000000000(0000) GS:ffff88041fa00000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 00007fa4595f4750 CR3: 0000000001c14000 CR4: 00000000001406f0 Stack: 0000000000000006 000000000be04930 0000000000000000 ffff880400000000 ffff880400000000 ffffffff8108f847 ffff88040be041c0 ffffffff81050446 ffff8803fc08a920 ffff8803fc08a958 ffff88040be041c0 ffff88040c073c38 Call Trace: [<ffffffff8108f847>] ? mark_held_locks+0x5e/0x74 [<ffffffff81050446>] ? __local_bh_enable_ip+0x9b/0xa1 [<ffffffff8108f9ca>] ? trace_hardirqs_on_caller+0x16d/0x189 [<ffffffff810915f4>] lock_acquire+0x122/0x1b6 [<ffffffff810915f4>] ? lock_acquire+0x122/0x1b6 [<ffffffff814c928f>] ? skb_dequeue+0x18/0x61 [<ffffffff81609dbf>] _raw_spin_lock_irqsave+0x35/0x49 [<ffffffff814c928f>] ? skb_dequeue+0x18/0x61 [<ffffffff814c928f>] skb_dequeue+0x18/0x61 [<ffffffffa009aa92>] afs_deliver_to_call+0x344/0x39d [kafs] [<ffffffffa009ab37>] afs_process_async_call+0x4c/0xd5 [kafs] [<ffffffffa0099e9c>] afs_async_workfn+0xe/0x10 [kafs] [<ffffffff81063a3a>] process_one_work+0x29d/0x57c [<ffffffff81064ac2>] worker_thread+0x24a/0x385 [<ffffffff81064878>] ? rescuer_thread+0x2d0/0x2d0 [<ffffffff810696f5>] kthread+0xf3/0xfb [<ffffffff8160a6ff>] ret_from_fork+0x1f/0x40 [<ffffffff81069602>] ? kthread_create_on_node+0x1cf/0x1cf Signed-off-by: David Howells <dhowells@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net>
418 lines
10 KiB
C
418 lines
10 KiB
C
/* RxRPC recvmsg() implementation
|
|
*
|
|
* Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
|
|
* Written by David Howells (dhowells@redhat.com)
|
|
*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public License
|
|
* as published by the Free Software Foundation; either version
|
|
* 2 of the License, or (at your option) any later version.
|
|
*/
|
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
#include <linux/net.h>
|
|
#include <linux/skbuff.h>
|
|
#include <linux/export.h>
|
|
#include <net/sock.h>
|
|
#include <net/af_rxrpc.h>
|
|
#include "ar-internal.h"
|
|
|
|
/*
|
|
* removal a call's user ID from the socket tree to make the user ID available
|
|
* again and so that it won't be seen again in association with that call
|
|
*/
|
|
void rxrpc_remove_user_ID(struct rxrpc_sock *rx, struct rxrpc_call *call)
|
|
{
|
|
_debug("RELEASE CALL %d", call->debug_id);
|
|
|
|
if (test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
|
|
write_lock_bh(&rx->call_lock);
|
|
rb_erase(&call->sock_node, &call->socket->calls);
|
|
clear_bit(RXRPC_CALL_HAS_USERID, &call->flags);
|
|
write_unlock_bh(&rx->call_lock);
|
|
}
|
|
|
|
read_lock_bh(&call->state_lock);
|
|
if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
|
|
!test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events))
|
|
rxrpc_queue_call(call);
|
|
read_unlock_bh(&call->state_lock);
|
|
}
|
|
|
|
/*
|
|
* receive a message from an RxRPC socket
|
|
* - we need to be careful about two or more threads calling recvmsg
|
|
* simultaneously
|
|
*/
|
|
int rxrpc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
|
|
int flags)
|
|
{
|
|
struct rxrpc_skb_priv *sp;
|
|
struct rxrpc_call *call = NULL, *continue_call = NULL;
|
|
struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
|
|
struct sk_buff *skb;
|
|
long timeo;
|
|
int copy, ret, ullen, offset, copied = 0;
|
|
u32 abort_code;
|
|
|
|
DEFINE_WAIT(wait);
|
|
|
|
_enter(",,,%zu,%d", len, flags);
|
|
|
|
if (flags & (MSG_OOB | MSG_TRUNC))
|
|
return -EOPNOTSUPP;
|
|
|
|
ullen = msg->msg_flags & MSG_CMSG_COMPAT ? 4 : sizeof(unsigned long);
|
|
|
|
timeo = sock_rcvtimeo(&rx->sk, flags & MSG_DONTWAIT);
|
|
msg->msg_flags |= MSG_MORE;
|
|
|
|
lock_sock(&rx->sk);
|
|
|
|
for (;;) {
|
|
/* return immediately if a client socket has no outstanding
|
|
* calls */
|
|
if (RB_EMPTY_ROOT(&rx->calls)) {
|
|
if (copied)
|
|
goto out;
|
|
if (rx->sk.sk_state != RXRPC_SERVER_LISTENING) {
|
|
release_sock(&rx->sk);
|
|
if (continue_call)
|
|
rxrpc_put_call(continue_call);
|
|
return -ENODATA;
|
|
}
|
|
}
|
|
|
|
/* get the next message on the Rx queue */
|
|
skb = skb_peek(&rx->sk.sk_receive_queue);
|
|
if (!skb) {
|
|
/* nothing remains on the queue */
|
|
if (copied &&
|
|
(flags & MSG_PEEK || timeo == 0))
|
|
goto out;
|
|
|
|
/* wait for a message to turn up */
|
|
release_sock(&rx->sk);
|
|
prepare_to_wait_exclusive(sk_sleep(&rx->sk), &wait,
|
|
TASK_INTERRUPTIBLE);
|
|
ret = sock_error(&rx->sk);
|
|
if (ret)
|
|
goto wait_error;
|
|
|
|
if (skb_queue_empty(&rx->sk.sk_receive_queue)) {
|
|
if (signal_pending(current))
|
|
goto wait_interrupted;
|
|
timeo = schedule_timeout(timeo);
|
|
}
|
|
finish_wait(sk_sleep(&rx->sk), &wait);
|
|
lock_sock(&rx->sk);
|
|
continue;
|
|
}
|
|
|
|
peek_next_packet:
|
|
sp = rxrpc_skb(skb);
|
|
call = sp->call;
|
|
ASSERT(call != NULL);
|
|
|
|
_debug("next pkt %s", rxrpc_pkts[sp->hdr.type]);
|
|
|
|
/* make sure we wait for the state to be updated in this call */
|
|
spin_lock_bh(&call->lock);
|
|
spin_unlock_bh(&call->lock);
|
|
|
|
if (test_bit(RXRPC_CALL_RELEASED, &call->flags)) {
|
|
_debug("packet from released call");
|
|
if (skb_dequeue(&rx->sk.sk_receive_queue) != skb)
|
|
BUG();
|
|
rxrpc_free_skb(skb);
|
|
continue;
|
|
}
|
|
|
|
/* determine whether to continue last data receive */
|
|
if (continue_call) {
|
|
_debug("maybe cont");
|
|
if (call != continue_call ||
|
|
skb->mark != RXRPC_SKB_MARK_DATA) {
|
|
release_sock(&rx->sk);
|
|
rxrpc_put_call(continue_call);
|
|
_leave(" = %d [noncont]", copied);
|
|
return copied;
|
|
}
|
|
}
|
|
|
|
rxrpc_get_call(call);
|
|
|
|
/* copy the peer address and timestamp */
|
|
if (!continue_call) {
|
|
if (msg->msg_name) {
|
|
size_t len =
|
|
sizeof(call->conn->params.peer->srx);
|
|
memcpy(msg->msg_name,
|
|
&call->conn->params.peer->srx, len);
|
|
msg->msg_namelen = len;
|
|
}
|
|
sock_recv_timestamp(msg, &rx->sk, skb);
|
|
}
|
|
|
|
/* receive the message */
|
|
if (skb->mark != RXRPC_SKB_MARK_DATA)
|
|
goto receive_non_data_message;
|
|
|
|
_debug("recvmsg DATA #%u { %d, %d }",
|
|
sp->hdr.seq, skb->len, sp->offset);
|
|
|
|
if (!continue_call) {
|
|
/* only set the control data once per recvmsg() */
|
|
ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID,
|
|
ullen, &call->user_call_ID);
|
|
if (ret < 0)
|
|
goto copy_error;
|
|
ASSERT(test_bit(RXRPC_CALL_HAS_USERID, &call->flags));
|
|
}
|
|
|
|
ASSERTCMP(sp->hdr.seq, >=, call->rx_data_recv);
|
|
ASSERTCMP(sp->hdr.seq, <=, call->rx_data_recv + 1);
|
|
call->rx_data_recv = sp->hdr.seq;
|
|
|
|
ASSERTCMP(sp->hdr.seq, >, call->rx_data_eaten);
|
|
|
|
offset = sp->offset;
|
|
copy = skb->len - offset;
|
|
if (copy > len - copied)
|
|
copy = len - copied;
|
|
|
|
ret = skb_copy_datagram_msg(skb, offset, msg, copy);
|
|
|
|
if (ret < 0)
|
|
goto copy_error;
|
|
|
|
/* handle piecemeal consumption of data packets */
|
|
_debug("copied %d+%d", copy, copied);
|
|
|
|
offset += copy;
|
|
copied += copy;
|
|
|
|
if (!(flags & MSG_PEEK))
|
|
sp->offset = offset;
|
|
|
|
if (sp->offset < skb->len) {
|
|
_debug("buffer full");
|
|
ASSERTCMP(copied, ==, len);
|
|
break;
|
|
}
|
|
|
|
/* we transferred the whole data packet */
|
|
if (!(flags & MSG_PEEK))
|
|
rxrpc_kernel_data_consumed(call, skb);
|
|
|
|
if (sp->hdr.flags & RXRPC_LAST_PACKET) {
|
|
_debug("last");
|
|
if (rxrpc_conn_is_client(call->conn)) {
|
|
/* last byte of reply received */
|
|
ret = copied;
|
|
goto terminal_message;
|
|
}
|
|
|
|
/* last bit of request received */
|
|
if (!(flags & MSG_PEEK)) {
|
|
_debug("eat packet");
|
|
if (skb_dequeue(&rx->sk.sk_receive_queue) !=
|
|
skb)
|
|
BUG();
|
|
rxrpc_free_skb(skb);
|
|
}
|
|
msg->msg_flags &= ~MSG_MORE;
|
|
break;
|
|
}
|
|
|
|
/* move on to the next data message */
|
|
_debug("next");
|
|
if (!continue_call)
|
|
continue_call = sp->call;
|
|
else
|
|
rxrpc_put_call(call);
|
|
call = NULL;
|
|
|
|
if (flags & MSG_PEEK) {
|
|
_debug("peek next");
|
|
skb = skb->next;
|
|
if (skb == (struct sk_buff *) &rx->sk.sk_receive_queue)
|
|
break;
|
|
goto peek_next_packet;
|
|
}
|
|
|
|
_debug("eat packet");
|
|
if (skb_dequeue(&rx->sk.sk_receive_queue) != skb)
|
|
BUG();
|
|
rxrpc_free_skb(skb);
|
|
}
|
|
|
|
/* end of non-terminal data packet reception for the moment */
|
|
_debug("end rcv data");
|
|
out:
|
|
release_sock(&rx->sk);
|
|
if (call)
|
|
rxrpc_put_call(call);
|
|
if (continue_call)
|
|
rxrpc_put_call(continue_call);
|
|
_leave(" = %d [data]", copied);
|
|
return copied;
|
|
|
|
/* handle non-DATA messages such as aborts, incoming connections and
|
|
* final ACKs */
|
|
receive_non_data_message:
|
|
_debug("non-data");
|
|
|
|
if (skb->mark == RXRPC_SKB_MARK_NEW_CALL) {
|
|
_debug("RECV NEW CALL");
|
|
ret = put_cmsg(msg, SOL_RXRPC, RXRPC_NEW_CALL, 0, &abort_code);
|
|
if (ret < 0)
|
|
goto copy_error;
|
|
if (!(flags & MSG_PEEK)) {
|
|
if (skb_dequeue(&rx->sk.sk_receive_queue) != skb)
|
|
BUG();
|
|
rxrpc_free_skb(skb);
|
|
}
|
|
goto out;
|
|
}
|
|
|
|
ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID,
|
|
ullen, &call->user_call_ID);
|
|
if (ret < 0)
|
|
goto copy_error;
|
|
ASSERT(test_bit(RXRPC_CALL_HAS_USERID, &call->flags));
|
|
|
|
switch (skb->mark) {
|
|
case RXRPC_SKB_MARK_DATA:
|
|
BUG();
|
|
case RXRPC_SKB_MARK_FINAL_ACK:
|
|
ret = put_cmsg(msg, SOL_RXRPC, RXRPC_ACK, 0, &abort_code);
|
|
break;
|
|
case RXRPC_SKB_MARK_BUSY:
|
|
ret = put_cmsg(msg, SOL_RXRPC, RXRPC_BUSY, 0, &abort_code);
|
|
break;
|
|
case RXRPC_SKB_MARK_REMOTE_ABORT:
|
|
abort_code = call->remote_abort;
|
|
ret = put_cmsg(msg, SOL_RXRPC, RXRPC_ABORT, 4, &abort_code);
|
|
break;
|
|
case RXRPC_SKB_MARK_LOCAL_ABORT:
|
|
abort_code = call->local_abort;
|
|
ret = put_cmsg(msg, SOL_RXRPC, RXRPC_ABORT, 4, &abort_code);
|
|
break;
|
|
case RXRPC_SKB_MARK_NET_ERROR:
|
|
_debug("RECV NET ERROR %d", sp->error);
|
|
abort_code = sp->error;
|
|
ret = put_cmsg(msg, SOL_RXRPC, RXRPC_NET_ERROR, 4, &abort_code);
|
|
break;
|
|
case RXRPC_SKB_MARK_LOCAL_ERROR:
|
|
_debug("RECV LOCAL ERROR %d", sp->error);
|
|
abort_code = sp->error;
|
|
ret = put_cmsg(msg, SOL_RXRPC, RXRPC_LOCAL_ERROR, 4,
|
|
&abort_code);
|
|
break;
|
|
default:
|
|
pr_err("Unknown packet mark %u\n", skb->mark);
|
|
BUG();
|
|
break;
|
|
}
|
|
|
|
if (ret < 0)
|
|
goto copy_error;
|
|
|
|
terminal_message:
|
|
_debug("terminal");
|
|
msg->msg_flags &= ~MSG_MORE;
|
|
msg->msg_flags |= MSG_EOR;
|
|
|
|
if (!(flags & MSG_PEEK)) {
|
|
_net("free terminal skb %p", skb);
|
|
if (skb_dequeue(&rx->sk.sk_receive_queue) != skb)
|
|
BUG();
|
|
rxrpc_free_skb(skb);
|
|
rxrpc_remove_user_ID(rx, call);
|
|
}
|
|
|
|
release_sock(&rx->sk);
|
|
rxrpc_put_call(call);
|
|
if (continue_call)
|
|
rxrpc_put_call(continue_call);
|
|
_leave(" = %d", ret);
|
|
return ret;
|
|
|
|
copy_error:
|
|
_debug("copy error");
|
|
release_sock(&rx->sk);
|
|
rxrpc_put_call(call);
|
|
if (continue_call)
|
|
rxrpc_put_call(continue_call);
|
|
_leave(" = %d", ret);
|
|
return ret;
|
|
|
|
wait_interrupted:
|
|
ret = sock_intr_errno(timeo);
|
|
wait_error:
|
|
finish_wait(sk_sleep(&rx->sk), &wait);
|
|
if (continue_call)
|
|
rxrpc_put_call(continue_call);
|
|
if (copied)
|
|
copied = ret;
|
|
_leave(" = %d [waitfail %d]", copied, ret);
|
|
return copied;
|
|
|
|
}
|
|
|
|
/**
|
|
* rxrpc_kernel_is_data_last - Determine if data message is last one
|
|
* @skb: Message holding data
|
|
*
|
|
* Determine if data message is last one for the parent call.
|
|
*/
|
|
bool rxrpc_kernel_is_data_last(struct sk_buff *skb)
|
|
{
|
|
struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
|
|
|
|
ASSERTCMP(skb->mark, ==, RXRPC_SKB_MARK_DATA);
|
|
|
|
return sp->hdr.flags & RXRPC_LAST_PACKET;
|
|
}
|
|
|
|
EXPORT_SYMBOL(rxrpc_kernel_is_data_last);
|
|
|
|
/**
|
|
* rxrpc_kernel_get_abort_code - Get the abort code from an RxRPC abort message
|
|
* @skb: Message indicating an abort
|
|
*
|
|
* Get the abort code from an RxRPC abort message.
|
|
*/
|
|
u32 rxrpc_kernel_get_abort_code(struct sk_buff *skb)
|
|
{
|
|
struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
|
|
|
|
switch (skb->mark) {
|
|
case RXRPC_SKB_MARK_REMOTE_ABORT:
|
|
return sp->call->remote_abort;
|
|
case RXRPC_SKB_MARK_LOCAL_ABORT:
|
|
return sp->call->local_abort;
|
|
default:
|
|
BUG();
|
|
}
|
|
}
|
|
|
|
EXPORT_SYMBOL(rxrpc_kernel_get_abort_code);
|
|
|
|
/**
|
|
* rxrpc_kernel_get_error - Get the error number from an RxRPC error message
|
|
* @skb: Message indicating an error
|
|
*
|
|
* Get the error number from an RxRPC error message.
|
|
*/
|
|
int rxrpc_kernel_get_error_number(struct sk_buff *skb)
|
|
{
|
|
struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
|
|
|
|
return sp->error;
|
|
}
|
|
|
|
EXPORT_SYMBOL(rxrpc_kernel_get_error_number);
|