Skip to contentSkip to conversation

SUNRPC: Set tk_rpc_status when RPC_TASK_SIGNALLED is detected

PATCH In review Li Lingfeng 3 participants 8 messages Jan 14, 2025 – Jan 26, 2025

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 SUNRPC: Set tk_rpc_status when RPC_TASK_SIGNALLED is detected

Commit 39494194f93b("SUNRPC: Fix races with rpc_killall_tasks()") adds rpc_task_set_rpc_status before setting RPC_TASK_SIGNALLED in rpc_signal_task, ensuring that rpc_status is definitely set when RPC_TASK_SIGNALLED is set.
Therefore, it seems unnecessary to set rpc_status again after detecting RPC_TASK_SIGNALLED in rpc_check_timeout.

However, in some exceptional cases, invalid and endlessly looping rpc_tasks may be generated. The rpc_call_rpcerror in rpc_check_timeout can timely terminate such rpc_tasks. Removing rpc_call_rpcerror may cause the rpc_task to fall into an infinite loop.

For example, in the following situation:
nfs4_close_done
// get err from server
nfs4_async_handle_exception
// goto out_restart

                            // umount -f
                            nfs_umount_begin
                             rpc_killall_tasks
                              rpc_signal_task
                               rpc_task_set_rpc_status
                                task->tk_rpc_status = -ERESTARTSYS
                                set_bit
                                // set RPC_TASK_SIGNALLED to tk_runstate
 rpc_restart_call_prepare
  __rpc_restart_call
   task->tk_rpc_status = 0
   // clear tk_rpc_status
  ...
  rpc_prepare_task
   nfs4_close_prepare
    nfs4_setup_sequence
     rpc_call_start
      task->tk_action = call_start

At this point, an rpc_task with RPC_TASK_SIGNALLED set but tk_rpc_status as 0 will be generated. This rpc_task will fall into the following loop:
call_encode --> call_transmit --> call_transmit_status --> call_status --> call_encode.

Since RPC_TASK_SIGNALLED is set, no request will be sent in call_transmit. Similarly, since RPC_TASK_SIGNALLED is set, rq_majortimeo will not be updated in call_status --> rpc_check_timeout, which will cause -ETIMEDOUT to be directly set to tk_status in call_transmit_status --> xprt_request_wait_receive --> xprt_wait_for_reply_request_def --> rpc_sleep_on_timeout --> __rpc_sleep_on_priority_timeout.

Here is the state and loop process of the rpc_task:
tk_runstate:
RPC_TASK_RUNNING RPC_TASK_ACTIVE RPC_TASK_NEED_RECV RPC_TASK_SIGNALLED tk_xprt->state:
XPRT_CONNECTED XPRT_BOUND
tk_flags
RPC_TASK_ASYNC RPC_TASK_MOVEABLE RPC_TASK_DYNAMIC RPC_TASK_SOFT RPC_TASK_NO_RETRANS_TIMEOUT RPC_TASK_CRED_NOREF

call_encode
xprt_request_enqueue_transmit
set_bit // RPC_TASK_NEED_XMIT
task->tk_action = call_transmit

call_transmit
task->tk_action = call_transmit_status xprt_transmit
xprt_request_transmit
// check RPC_TASK_SIGNALLED and goto out_dequeue xprt_request_dequeue_transmit
xprt_request_dequeue_transmit_locked
test_and_clear_bit // RPC_TASK_NEED_XMIT

call_transmit_status
task->tk_action = call_status
xprt_request_wait_receive
xprt_wait_for_reply_request_def
xprt_request_timeout // get timeout

    req->rq_majortimeo // rq_majortimeo will not be updated
   rpc_sleep_on_timeout
    __rpc_sleep_on_priority_timeout
     task->tk_status = -ETIMEDOUT // set ETIMEDOUT

call_status
task->tk_action = call_encode
rpc_check_timeout
// check RPC_TASK_SIGNALLED and skip xprt_reset_majortimeo

Fix it by adding rpc_call_rpcerror back.

Fixes 39494194f93b ("SUNRPC: Fix races with rpc_killall_tasks()")
---
 net/sunrpc/clnt.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/sunrpc/clnt.c +3 -1
index 0090162ee8c3..0acdff19a37c 100644
@@ -2509,8 +2509,10 @@ rpc_check_timeout(struct rpc_task *task)
 {
 	struct rpc_clnt	*clnt = task->tk_client;
 
-	if (RPC_SIGNALLED(task))
+	if (RPC_SIGNALLED(task)) {
+		rpc_call_rpcerror(task, -ERESTARTSYS);
 		return;
+	}
 
 	if (xprt_adjust_timeout(task->tk_rqstp) == 0)
 		return;
Signature · 1 line
2.31.1
#1 lore raw

Hi,

Thanks for the patch.

Before 39494194f93b("SUNRPC: Fix races with rpc_killall_tasks()", every time we set RPC_TASK_SIGNALLED, when we go through __rpc_execute, this rpc_task will immediate break and exist.

However after that, __rpc_execute won't judge RPC_TASK_SIGNNALED, so for the case like you point out below, even after your commit rpc_check_timeout will help break and exist eventually, but this rpc_task has already do some work. I prefer reintroduce judging RPC_TASK_SIGNNALED in __rpc_execute to help exist immediatly.

Quoted 110 lines · Li Lingfeng

Commit 39494194f93b("SUNRPC: Fix races with rpc_killall_tasks()") adds rpc_task_set_rpc_status before setting RPC_TASK_SIGNALLED in rpc_signal_task, ensuring that rpc_status is definitely set when RPC_TASK_SIGNALLED is set.
Therefore, it seems unnecessary to set rpc_status again after detecting RPC_TASK_SIGNALLED in rpc_check_timeout.

However, in some exceptional cases, invalid and endlessly looping rpc_tasks may be generated. The rpc_call_rpcerror in rpc_check_timeout can timely terminate such rpc_tasks. Removing rpc_call_rpcerror may cause the rpc_task to fall into an infinite loop.

For example, in the following situation:
nfs4_close_done
// get err from server
nfs4_async_handle_exception
// goto out_restart

                             // umount -f
                             nfs_umount_begin
                              rpc_killall_tasks
                               rpc_signal_task
                                rpc_task_set_rpc_status
                                 task->tk_rpc_status = -ERESTARTSYS
                                 set_bit
                                 // set RPC_TASK_SIGNALLED to tk_runstate
  rpc_restart_call_prepare
   __rpc_restart_call
    task->tk_rpc_status = 0
    // clear tk_rpc_status
   ...
   rpc_prepare_task
    nfs4_close_prepare
     nfs4_setup_sequence
      rpc_call_start
       task->tk_action = call_start

At this point, an rpc_task with RPC_TASK_SIGNALLED set but tk_rpc_status as 0 will be generated. This rpc_task will fall into the following loop:
call_encode --> call_transmit --> call_transmit_status --> call_status --> call_encode.

Since RPC_TASK_SIGNALLED is set, no request will be sent in call_transmit. Similarly, since RPC_TASK_SIGNALLED is set, rq_majortimeo will not be updated in call_status --> rpc_check_timeout, which will cause -ETIMEDOUT to be directly set to tk_status in call_transmit_status --> xprt_request_wait_receive --> xprt_wait_for_reply_request_def --> rpc_sleep_on_timeout --> __rpc_sleep_on_priority_timeout.

Here is the state and loop process of the rpc_task:
tk_runstate:
RPC_TASK_RUNNING RPC_TASK_ACTIVE RPC_TASK_NEED_RECV RPC_TASK_SIGNALLED tk_xprt->state:
XPRT_CONNECTED XPRT_BOUND
tk_flags
RPC_TASK_ASYNC RPC_TASK_MOVEABLE RPC_TASK_DYNAMIC RPC_TASK_SOFT RPC_TASK_NO_RETRANS_TIMEOUT RPC_TASK_CRED_NOREF

call_encode
xprt_request_enqueue_transmit
set_bit // RPC_TASK_NEED_XMIT
task->tk_action = call_transmit

call_transmit
task->tk_action = call_transmit_status xprt_transmit
xprt_request_transmit
// check RPC_TASK_SIGNALLED and goto out_dequeue xprt_request_dequeue_transmit
xprt_request_dequeue_transmit_locked
test_and_clear_bit // RPC_TASK_NEED_XMIT

call_transmit_status
task->tk_action = call_status
xprt_request_wait_receive
xprt_wait_for_reply_request_def

    xprt_request_timeout // get timeout
     req->rq_majortimeo // rq_majortimeo will not be updated
    rpc_sleep_on_timeout
     __rpc_sleep_on_priority_timeout
      task->tk_status = -ETIMEDOUT // set ETIMEDOUT

call_status
task->tk_action = call_encode
rpc_check_timeout
// check RPC_TASK_SIGNALLED and skip xprt_reset_majortimeo

Fix it by adding rpc_call_rpcerror back.

Fixes 39494194f93b ("SUNRPC: Fix races with rpc_killall_tasks()")
---
  net/sunrpc/clnt.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
net/sunrpc/clnt.c +3 -1
index 0090162ee8c3..0acdff19a37c 100644
@@ -2509,8 +2509,10 @@ rpc_check_timeout(struct rpc_task *task)
  {
  	struct rpc_clnt	*clnt = task->tk_client;
 
-	if (RPC_SIGNALLED(task))
+	if (RPC_SIGNALLED(task)) {
+		rpc_call_rpcerror(task, -ERESTARTSYS);
  		return;
+	}
 
  	if (xprt_adjust_timeout(task->tk_rqstp) == 0)
  		return;
#2 lore raw
Quoted 8 lines · yangerkun

Hi,

Thanks for the patch.

Before 39494194f93b("SUNRPC: Fix races with rpc_killall_tasks()", every time we set RPC_TASK_SIGNALLED, when we go through __rpc_execute, this rpc_task will immediate break and exist.

Quoted 5 lines

However after that, __rpc_execute won't judge RPC_TASK_SIGNNALED, so for the case like you point out below, even after your commit rpc_check_timeout will help break and exist eventually, but this rpc_task has already do some work. I prefer reintroduce judging RPC_TASK_SIGNNALED in __rpc_execute to help exist immediatly.

Thanks for your advice, I will send v2 soon.

Quoted 111 lines · Li Lingfeng
Quoted 109 lines

Commit 39494194f93b("SUNRPC: Fix races with rpc_killall_tasks()") adds rpc_task_set_rpc_status before setting RPC_TASK_SIGNALLED in rpc_signal_task, ensuring that rpc_status is definitely set when RPC_TASK_SIGNALLED is set.
Therefore, it seems unnecessary to set rpc_status again after detecting RPC_TASK_SIGNALLED in rpc_check_timeout.

However, in some exceptional cases, invalid and endlessly looping rpc_tasks may be generated. The rpc_call_rpcerror in rpc_check_timeout can timely terminate such rpc_tasks. Removing rpc_call_rpcerror may cause the rpc_task to fall into an infinite loop.

For example, in the following situation:
nfs4_close_done
  // get err from server
  nfs4_async_handle_exception
  // goto out_restart
                             // umount -f
                             nfs_umount_begin rpc_killall_tasks rpc_signal_task rpc_task_set_rpc_status task->tk_rpc_status = -ERESTARTSYS set_bit
                                 // set RPC_TASK_SIGNALLED to tk_runstate rpc_restart_call_prepare
   __rpc_restart_call
    task->tk_rpc_status = 0
    // clear tk_rpc_status
   ...
   rpc_prepare_task
    nfs4_close_prepare
     nfs4_setup_sequence
      rpc_call_start
       task->tk_action = call_start

At this point, an rpc_task with RPC_TASK_SIGNALLED set but tk_rpc_status as 0 will be generated. This rpc_task will fall into the following loop:
call_encode --> call_transmit --> call_transmit_status --> call_status --> call_encode.

Since RPC_TASK_SIGNALLED is set, no request will be sent in call_transmit. Similarly, since RPC_TASK_SIGNALLED is set, rq_majortimeo will not be updated in call_status --> rpc_check_timeout, which will cause -ETIMEDOUT
to be directly set to tk_status in call_transmit_status --> xprt_request_wait_receive --> xprt_wait_for_reply_request_def --> rpc_sleep_on_timeout --> __rpc_sleep_on_priority_timeout.

Here is the state and loop process of the rpc_task:
tk_runstate:
RPC_TASK_RUNNING RPC_TASK_ACTIVE RPC_TASK_NEED_RECV RPC_TASK_SIGNALLED tk_xprt->state:
XPRT_CONNECTED XPRT_BOUND
tk_flags
RPC_TASK_ASYNC RPC_TASK_MOVEABLE RPC_TASK_DYNAMIC RPC_TASK_SOFT RPC_TASK_NO_RETRANS_TIMEOUT RPC_TASK_CRED_NOREF

call_encode
  xprt_request_enqueue_transmit
   set_bit // RPC_TASK_NEED_XMIT
  task->tk_action = call_transmit

call_transmit
  task->tk_action = call_transmit_status xprt_transmit
   xprt_request_transmit
    // check RPC_TASK_SIGNALLED and goto out_dequeue xprt_request_dequeue_transmit xprt_request_dequeue_transmit_locked test_and_clear_bit // RPC_TASK_NEED_XMIT

call_transmit_status
  task->tk_action = call_status xprt_request_wait_receive xprt_wait_for_reply_request_def xprt_request_timeout // get timeout req->rq_majortimeo // rq_majortimeo will not be updated rpc_sleep_on_timeout __rpc_sleep_on_priority_timeout task->tk_status = -ETIMEDOUT // set ETIMEDOUT

call_status
  task->tk_action = call_encode
  rpc_check_timeout
   // check RPC_TASK_SIGNALLED and skip xprt_reset_majortimeo

Fix it by adding rpc_call_rpcerror back.

Fixes 39494194f93b ("SUNRPC: Fix races with rpc_killall_tasks()")

---
  net/sunrpc/clnt.c | 4 +++-

  1 file changed, 3 insertions(+), 1 deletion(-)
net/sunrpc/clnt.c +3 -0
index 0090162ee8c3..0acdff19a37c 100644
@@ -2509,8 +2509,10 @@ rpc_check_timeout(struct rpc_task *task)
   {
       struct rpc_clnt    *clnt = task->tk_client;
   -    if (RPC_SIGNALLED(task))
+    if (RPC_SIGNALLED(task)) {
+        rpc_call_rpcerror(task, -ERESTARTSYS);
           return;
+    }
         if (xprt_adjust_timeout(task->tk_rqstp) == 0)
           return;
#3 lore raw
In reply to yangerkun
Quoted 10 lines · yangerkun

Hi,

Thanks for the patch.

Before 39494194f93b("SUNRPC: Fix races with rpc_killall_tasks()", every
time we set RPC_TASK_SIGNALLED, when we go through __rpc_execute, this
rpc_task will immediate break and exist.

Quoted 7 lines

However after that, __rpc_execute won't judge RPC_TASK_SIGNNALED, so for
the case like you point out below, even after your commit rpc_check_timeout will help break and exist eventually, but this rpc_task has already do some work. I prefer reintroduce judging RPC_TASK_SIGNNALED in __rpc_execute to help exist immediatly.

Better yet... Let's get rid of the RPC_TASK_SIGNALLED flag altogether and just replace

#define RPC_TASK_SIGNALLED(task) (READ_ONCE(task->tk_rpc_status) == -ERESTARTSYS)

There is no good reason to try to maintain two completely separate sources of truth to describe the same task state.

Signature · 3 lines
Trond Myklebust
Linux NFS client maintainer, Hammerspace
[email protected]
#4 lore raw
Quoted 19 lines · Trond Myklebust
Quoted 10 lines · yangerkun

Hi,

Thanks for the patch.

Before 39494194f93b("SUNRPC: Fix races with rpc_killall_tasks()", every
time we set RPC_TASK_SIGNALLED, when we go through __rpc_execute, this
rpc_task will immediate break and exist.

Quoted 7 lines

However after that, __rpc_execute won't judge RPC_TASK_SIGNNALED, so for
the case like you point out below, even after your commit rpc_check_timeout will help break and exist eventually, but this rpc_task has already do some work. I prefer reintroduce judging RPC_TASK_SIGNNALED in __rpc_execute to help exist immediatly.

Quoted 4 lines

Better yet... Let's get rid of the RPC_TASK_SIGNALLED flag altogether and just replace

#define RPC_TASK_SIGNALLED(task) (READ_ONCE(task->tk_rpc_status) == -ERESTARTSYS)

Hi,

Thanks for your reply! Yeah, if all the places where tk_rpc_status is
updated are by calling rpc_task_set_rpc_status, we can use
task->tk_rpc_status == -ERESTARTSYS to determine whether rpc_task is
RPC_TASK_SIGNALLED. But for the case like Li has provided,
__rpc_restart_call won't do this, and will overwrite tk_rpc_status unconditionally. This won't be a stable solution. Maybe it's better to change __rpc_restart_call calling rpc_task_set_rpc_status too? And
__rpc_execute will be enough to help solve this case.

Quoted 4 lines

There is no good reason to try to maintain two completely separate sources of truth to describe the same task state.

#5 lore raw
Quoted 32 lines · yangerkun
Quoted 20 lines · Trond Myklebust
Quoted 10 lines · yangerkun

Hi,

Thanks for the patch.

Before 39494194f93b("SUNRPC: Fix races with rpc_killall_tasks()", every
time we set RPC_TASK_SIGNALLED, when we go through __rpc_execute, this
rpc_task will immediate break and exist.

Quoted 8 lines

However after that, __rpc_execute won't judge RPC_TASK_SIGNNALED, so
for
the case like you point out below, even after your commit rpc_check_timeout will help break and exist eventually, but this rpc_task has already do some work. I prefer reintroduce judging RPC_TASK_SIGNNALED in __rpc_execute to help exist immediatly.

Quoted 6 lines

Better yet... Let's get rid of the RPC_TASK_SIGNALLED flag altogether
and just replace

#define RPC_TASK_SIGNALLED(task) (READ_ONCE(task->tk_rpc_status) == -ERESTARTSYS)

Hi,

Quoted 11 lines

Thanks for your reply! Yeah, if all the places where tk_rpc_status is updated are by calling rpc_task_set_rpc_status, we can use task->tk_rpc_status == -ERESTARTSYS to determine whether rpc_task is RPC_TASK_SIGNALLED. But for the case like Li has provided, __rpc_restart_call won't do this, and will overwrite tk_rpc_status unconditionally. This won't be a stable solution. Maybe it's better to
change __rpc_restart_call calling rpc_task_set_rpc_status too? And __rpc_execute will be enough to help solve this case.

That would break __rpc_restart_call() to the point of rendering it completely useless.
The whole purpose of that call is to give the NFS layer a chance to handle errors in the exit callback, and then kick off a fresh call. Your suggestion would mean that any RPC level error sticks around, and causes the new call to immediately fail.

I see no point in doing anything more than fixing the looping behaviour. Eliminating the redundant flag will do that.

Signature · 3 lines
Trond Myklebust
Linux NFS client maintainer, Hammerspace
[email protected]
#6 lore raw
Quoted 45 lines · Trond Myklebust
Quoted 32 lines · yangerkun
Quoted 20 lines · Trond Myklebust
Quoted 10 lines · yangerkun

Hi,

Thanks for the patch.

Before 39494194f93b("SUNRPC: Fix races with rpc_killall_tasks()", every
time we set RPC_TASK_SIGNALLED, when we go through __rpc_execute, this
rpc_task will immediate break and exist.

Quoted 8 lines

However after that, __rpc_execute won't judge RPC_TASK_SIGNNALED, so
for
the case like you point out below, even after your commit rpc_check_timeout will help break and exist eventually, but this rpc_task has already do some work. I prefer reintroduce judging RPC_TASK_SIGNNALED in __rpc_execute to help exist immediatly.

Quoted 6 lines

Better yet... Let's get rid of the RPC_TASK_SIGNALLED flag altogether
and just replace

#define RPC_TASK_SIGNALLED(task) (READ_ONCE(task->tk_rpc_status) == -ERESTARTSYS)

Hi,

Quoted 11 lines

Thanks for your reply! Yeah, if all the places where tk_rpc_status is updated are by calling rpc_task_set_rpc_status, we can use task->tk_rpc_status == -ERESTARTSYS to determine whether rpc_task is RPC_TASK_SIGNALLED. But for the case like Li has provided, __rpc_restart_call won't do this, and will overwrite tk_rpc_status unconditionally. This won't be a stable solution. Maybe it's better to
change __rpc_restart_call calling rpc_task_set_rpc_status too? And __rpc_execute will be enough to help solve this case.

Quoted 2 lines

That would break __rpc_restart_call() to the point of rendering it completely useless.

Aha, agree with you. There maybe case tk_rpc_status != 0 but rpc_restart_call can also rerun this rpc_task.

The only case we should consider is when we call rpc_signal_task, we should break this rpc_task unconditionally. Fixing it within __rpc_execute is enough.

Quoted 8 lines

The whole purpose of that call is to give the NFS layer a chance to handle errors in the exit callback, and then kick off a fresh call. Your suggestion would mean that any RPC level error sticks around, and causes the new call to immediately fail.

I see no point in doing anything more than fixing the looping behaviour. Eliminating the redundant flag will do that.

#7 lore raw
In reply to Trond Myklebust
Quoted 22 lines · Trond Myklebust
Quoted 21 lines · yangerkun
Quoted 19 lines · Trond Myklebust
Quoted 18 lines · yangerkun

Hi,

Thanks for the patch.

Before 39494194f93b("SUNRPC: Fix races with rpc_killall_tasks()", every
time we set RPC_TASK_SIGNALLED, when we go through __rpc_execute, this
rpc_task will immediate break and exist.

However after that, __rpc_execute won't judge RPC_TASK_SIGNNALED, so
for
the case like you point out below, even after your commit rpc_check_timeout will help break and exist eventually, but this rpc_task has already do some work. I prefer reintroduce judging RPC_TASK_SIGNNALED in __rpc_execute to help exist immediatly.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Better yet... Let's get rid of the RPC_TASK_SIGNALLED flag altogether
and just replace

#define RPC_TASK_SIGNALLED(task) (READ_ONCE(task->tk_rpc_status) == -ERESTARTSYS)

Hi,

I'm not quite clear on how this can resolve the issue.
If we remove the RPC_TASK_SIGNALLED flag and replace setting tk_runstate to RPC_TASK_SIGNALLED with setting tk_rpc_status to -ERESTARTSYS in rpc_signal_task, wouldn't setting tk_rpc_status back to 0 in __rpc_restart_call still lead to an infinite loop in the rpc_task? Could you please provide a more detailed explanation?

Thanks.

Quoted 23 lines
Quoted 13 lines

Hi,

Thanks for your reply! Yeah, if all the places where tk_rpc_status is updated are by calling rpc_task_set_rpc_status, we can use task->tk_rpc_status == -ERESTARTSYS to determine whether rpc_task is RPC_TASK_SIGNALLED. But for the case like Li has provided, __rpc_restart_call won't do this, and will overwrite tk_rpc_status unconditionally. This won't be a stable solution. Maybe it's better to
change __rpc_restart_call calling rpc_task_set_rpc_status too? And __rpc_execute will be enough to help solve this case.

That would break __rpc_restart_call() to the point of rendering it completely useless.
The whole purpose of that call is to give the NFS layer a chance to handle errors in the exit callback, and then kick off a fresh call. Your suggestion would mean that any RPC level error sticks around, and causes the new call to immediately fail.

I see no point in doing anything more than fixing the looping behaviour. Eliminating the redundant flag will do that.