Skip to content

Commit 146d8fe

Browse files
dhowellsdavem330
authored andcommitted
rxrpc: Call state should be read with READ_ONCE() under some circumstances
The call state may be changed at any time by the data-ready routine in response to received packets, so if the call state is to be read and acted upon several times in a function, READ_ONCE() must be used unless the call state lock is held. Signed-off-by: David Howells <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 02b2faa commit 146d8fe

File tree

3 files changed

+39
-25
lines changed

3 files changed

+39
-25
lines changed

net/rxrpc/input.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,7 @@ static void rxrpc_input_data(struct rxrpc_call *call, struct sk_buff *skb,
420420
u16 skew)
421421
{
422422
struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
423+
enum rxrpc_call_state state;
423424
unsigned int offset = sizeof(struct rxrpc_wire_header);
424425
unsigned int ix;
425426
rxrpc_serial_t serial = sp->hdr.serial, ack_serial = 0;
@@ -434,14 +435,15 @@ static void rxrpc_input_data(struct rxrpc_call *call, struct sk_buff *skb,
434435
_proto("Rx DATA %%%u { #%u f=%02x }",
435436
sp->hdr.serial, seq, sp->hdr.flags);
436437

437-
if (call->state >= RXRPC_CALL_COMPLETE)
438+
state = READ_ONCE(call->state);
439+
if (state >= RXRPC_CALL_COMPLETE)
438440
return;
439441

440442
/* Received data implicitly ACKs all of the request packets we sent
441443
* when we're acting as a client.
442444
*/
443-
if ((call->state == RXRPC_CALL_CLIENT_SEND_REQUEST ||
444-
call->state == RXRPC_CALL_CLIENT_AWAIT_REPLY) &&
445+
if ((state == RXRPC_CALL_CLIENT_SEND_REQUEST ||
446+
state == RXRPC_CALL_CLIENT_AWAIT_REPLY) &&
445447
!rxrpc_receiving_reply(call))
446448
return;
447449

@@ -799,7 +801,7 @@ static void rxrpc_input_ack(struct rxrpc_call *call, struct sk_buff *skb,
799801
return rxrpc_proto_abort("AK0", call, 0);
800802

801803
/* Ignore ACKs unless we are or have just been transmitting. */
802-
switch (call->state) {
804+
switch (READ_ONCE(call->state)) {
803805
case RXRPC_CALL_CLIENT_SEND_REQUEST:
804806
case RXRPC_CALL_CLIENT_AWAIT_REPLY:
805807
case RXRPC_CALL_SERVER_SEND_REPLY:
@@ -940,7 +942,7 @@ static void rxrpc_input_call_packet(struct rxrpc_call *call,
940942
static void rxrpc_input_implicit_end_call(struct rxrpc_connection *conn,
941943
struct rxrpc_call *call)
942944
{
943-
switch (call->state) {
945+
switch (READ_ONCE(call->state)) {
944946
case RXRPC_CALL_SERVER_AWAIT_ACK:
945947
rxrpc_call_completed(call);
946948
break;

net/rxrpc/recvmsg.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ int rxrpc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
527527
msg->msg_namelen = len;
528528
}
529529

530-
switch (call->state) {
530+
switch (READ_ONCE(call->state)) {
531531
case RXRPC_CALL_SERVER_ACCEPTING:
532532
ret = rxrpc_recvmsg_new_call(rx, call, msg, flags);
533533
break;
@@ -640,7 +640,7 @@ int rxrpc_kernel_recv_data(struct socket *sock, struct rxrpc_call *call,
640640

641641
mutex_lock(&call->user_mutex);
642642

643-
switch (call->state) {
643+
switch (READ_ONCE(call->state)) {
644644
case RXRPC_CALL_CLIENT_RECV_REPLY:
645645
case RXRPC_CALL_SERVER_RECV_REQUEST:
646646
case RXRPC_CALL_SERVER_ACK_REQUEST:

net/rxrpc/sendmsg.c

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,7 @@ rxrpc_new_client_call_for_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg,
488488
int rxrpc_do_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg, size_t len)
489489
__releases(&rx->sk.sk_lock.slock)
490490
{
491+
enum rxrpc_call_state state;
491492
enum rxrpc_command cmd;
492493
struct rxrpc_call *call;
493494
unsigned long user_call_ID = 0;
@@ -526,13 +527,17 @@ int rxrpc_do_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg, size_t len)
526527
return PTR_ERR(call);
527528
/* ... and we have the call lock. */
528529
} else {
529-
ret = -EBUSY;
530-
if (call->state == RXRPC_CALL_UNINITIALISED ||
531-
call->state == RXRPC_CALL_CLIENT_AWAIT_CONN ||
532-
call->state == RXRPC_CALL_SERVER_PREALLOC ||
533-
call->state == RXRPC_CALL_SERVER_SECURING ||
534-
call->state == RXRPC_CALL_SERVER_ACCEPTING)
530+
switch (READ_ONCE(call->state)) {
531+
case RXRPC_CALL_UNINITIALISED:
532+
case RXRPC_CALL_CLIENT_AWAIT_CONN:
533+
case RXRPC_CALL_SERVER_PREALLOC:
534+
case RXRPC_CALL_SERVER_SECURING:
535+
case RXRPC_CALL_SERVER_ACCEPTING:
536+
ret = -EBUSY;
535537
goto error_release_sock;
538+
default:
539+
break;
540+
}
536541

537542
ret = mutex_lock_interruptible(&call->user_mutex);
538543
release_sock(&rx->sk);
@@ -542,10 +547,11 @@ int rxrpc_do_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg, size_t len)
542547
}
543548
}
544549

550+
state = READ_ONCE(call->state);
545551
_debug("CALL %d USR %lx ST %d on CONN %p",
546-
call->debug_id, call->user_call_ID, call->state, call->conn);
552+
call->debug_id, call->user_call_ID, state, call->conn);
547553

548-
if (call->state >= RXRPC_CALL_COMPLETE) {
554+
if (state >= RXRPC_CALL_COMPLETE) {
549555
/* it's too late for this call */
550556
ret = -ESHUTDOWN;
551557
} else if (cmd == RXRPC_CMD_SEND_ABORT) {
@@ -555,12 +561,12 @@ int rxrpc_do_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg, size_t len)
555561
} else if (cmd != RXRPC_CMD_SEND_DATA) {
556562
ret = -EINVAL;
557563
} else if (rxrpc_is_client_call(call) &&
558-
call->state != RXRPC_CALL_CLIENT_SEND_REQUEST) {
564+
state != RXRPC_CALL_CLIENT_SEND_REQUEST) {
559565
/* request phase complete for this client call */
560566
ret = -EPROTO;
561567
} else if (rxrpc_is_service_call(call) &&
562-
call->state != RXRPC_CALL_SERVER_ACK_REQUEST &&
563-
call->state != RXRPC_CALL_SERVER_SEND_REPLY) {
568+
state != RXRPC_CALL_SERVER_ACK_REQUEST &&
569+
state != RXRPC_CALL_SERVER_SEND_REPLY) {
564570
/* Reply phase not begun or not complete for service call. */
565571
ret = -EPROTO;
566572
} else {
@@ -605,14 +611,20 @@ int rxrpc_kernel_send_data(struct socket *sock, struct rxrpc_call *call,
605611
_debug("CALL %d USR %lx ST %d on CONN %p",
606612
call->debug_id, call->user_call_ID, call->state, call->conn);
607613

608-
if (call->state >= RXRPC_CALL_COMPLETE) {
609-
ret = -ESHUTDOWN; /* it's too late for this call */
610-
} else if (call->state != RXRPC_CALL_CLIENT_SEND_REQUEST &&
611-
call->state != RXRPC_CALL_SERVER_ACK_REQUEST &&
612-
call->state != RXRPC_CALL_SERVER_SEND_REPLY) {
613-
ret = -EPROTO; /* request phase complete for this client call */
614-
} else {
614+
switch (READ_ONCE(call->state)) {
615+
case RXRPC_CALL_CLIENT_SEND_REQUEST:
616+
case RXRPC_CALL_SERVER_ACK_REQUEST:
617+
case RXRPC_CALL_SERVER_SEND_REPLY:
615618
ret = rxrpc_send_data(rxrpc_sk(sock->sk), call, msg, len);
619+
break;
620+
case RXRPC_CALL_COMPLETE:
621+
/* It's too late for this call */
622+
ret = -ESHUTDOWN;
623+
break;
624+
default:
625+
/* Request phase complete for this client call */
626+
ret = -EPROTO;
627+
break;
616628
}
617629

618630
mutex_unlock(&call->user_mutex);

0 commit comments

Comments
 (0)