double ->queue_rq() because of timeout in ->queue_rq()
Subsystems (3)
Hi,
David Jeffery found one double ->queue_rq() issue, so far it can be triggered in the following two cases:
1) scsi driver in guest kernel
- the story could be long vmexit latency or long preempt latency of vCPU pthread, then IO req is timed out before queuing the request to hardware but after calling blk_mq_start_request() during ->queue_rq(), then timeout handler handles it by requeue, then double ->queue_rq() is caused, and kernel panic
2) burst of kernel messages from irq handler
For 1), I think it is one reasonable case, given latency from host side can come anytime in theory because vCPU is emulated by one normal host pthread which can be preempted anywhere. For 2), I guess kernel message is supposed to be rate limited.
Firstly, is this kind of so long(30sec) random latency when running kernel code something normal? Or do we need to take care of it? IMO, it looks reasonable in case of VM, but our VM experts may have better idea about this situation. Also the default 30sec timeout could be reduced via sysfs or drivers.
Suppose it is one reasonable report to fix, what is the preferred solution?
So far, it is driver's responsibility to cover the race between timeout and completion, so it is supposed to be solved in driver in theory, given driver has enough knowledge.
But it is really one common problem, lots of driver could have similar issue, and could be hard to fix all affected drivers, so David suggests the following patch by draining in-progress ->queue_rq() for this issue. And the patch looks reasonable too.
Any comments for this issue and the solution?
block/blk-mq.c +24 -12
@@ -1523,7 +1523,12 @@ static void blk_mq_rq_timed_out(struct request *req)blk_add_timer (req); } -static bool blk_mq_req_expired (struct request *rq,unsigned long *next) +struct blk_expired_data { +unsigned long next; +unsigned long now; +}; + +static bool blk_mq_req_expired (struct request *rq,struct blk_expired_data *expired) {unsigned long deadline;
@@ -1533,13 +1538,13 @@ static bool blk_mq_req_expired(struct request *rq, unsigned long *next)return false; deadline =READ_ONCE (rq->deadline); -if (time_after_eq (jiffies, deadline)) +if (time_after_eq (expired->now, deadline))return true; -if (*next ==0 ) - *next = deadline; -else if (time_after (*next, deadline)) - *next = deadline; +if (expired->next ==0 ) + expired->next = deadline; +else if (time_after (expired->next, deadline)) + expired->next = deadline;return false; }
@@ -1555,7 +1560,7 @@ void blk_mq_put_rq_ref(struct request *rq)static bool blk_mq_check_expired (struct request *rq,void *priv) { -unsigned long *next = priv; +struct blk_expired_data *expired = priv;/* * blk_mq_queue_tag_busy_iter() has locked the request, so it cannot
@@ -1564,7 +1569,7 @@ static bool blk_mq_check_expired(struct request *rq, void *priv)* it was completed and reallocated as a new request after returning * from blk_mq_check_expired(). */ -if (blk_mq_req_expired (rq, next)) +if (blk_mq_req_expired (rq, expired))blk_mq_rq_timed_out (rq);return true; }
@@ -1573,7 +1578,7 @@ static void blk_mq_timeout_work(struct work_struct *work) {struct request_queue *q = container_of(work, struct request_queue, timeout_work); -unsigned long next =0 ; +struct blk_expired_data expired = {.next =0 , .now = jiffies};struct blk_mq_hw_ctx *hctx;unsigned long i;
@@ -1593,10 +1598,17 @@ static void blk_mq_timeout_work(struct work_struct *work)if (!percpu_ref_tryget (&q->q_usage_counter))return ; -blk_mq_queue_tag_busy_iter (q, blk_mq_check_expired, &next); +/* Before walking tags, we must ensure any submit started before the +* current time has finished. Since the submit uses srcu or rcu, wait +* for a synchronization point to ensure all running submits have +* finished +*/ +blk_mq_wait_quiesce_done (q); + +blk_mq_queue_tag_busy_iter (q, blk_mq_check_expired, &expired); -if (next !=0 ) { -mod_timer (&q->timeout, next); +if (expired.next !=0 ) { +mod_timer (&q->timeout, expired.next); }else {/* * Request timeouts are handled as a forward rolling timer. If
Thanks,
Ming
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Quoted 20 lines · Ming Lei
Hi,
David Jeffery found one double ->queue_rq() issue, so far it can be triggered in the following two cases:
1) scsi driver in guest kernel
- the story could be long vmexit latency or long preempt latency of vCPU pthread, then IO req is timed out before queuing the request to hardware but after calling
blk_mq_start_request()during ->queue_rq(), then timeout handler handles it by requeue, then double ->queue_rq() is caused, and kernel panic2) burst of kernel messages from irq handler
For 1), I think it is one reasonable case, given latency from host side can come anytime in theory because vCPU is emulated by one normal host pthread which can be preempted anywhere. For 2), I guess kernel message is supposed to be rate limited.
Quoted 5 lines
Firstly, is this kind of so long(30sec) random latency when running kernel code something normal? Or do we need to take care of it? IMO, it looks reasonable in case of VM, but our VM experts may have better idea about this situation. Also the default 30sec timeout could be reduced via sysfs or drivers.
30 seconds is a long latency that does not occur during normal operation, but unfortunately does happen on occasion.
I think there's an interest in understanding the root cause and solving long latencies (if possible) in the QEMU/KVM communities. We can investigate specific cases on [email protected] and/or [email protected].
The kernel should be robust in the face of long latencies even if they are due to issues with hardware or the hypervisor. I'm not familiar enough with the Linux block layer to say whether the patch below is correct, but having a solution in place would be good.
Quoted 103 lines
Suppose it is one reasonable report to fix, what is the preferred solution?
So far, it is driver's responsibility to cover the race between timeout and completion, so it is supposed to be solved in driver in theory, given driver has enough knowledge.
But it is really one common problem, lots of driver could have similar issue, and could be hard to fix all affected drivers, so David suggests the following patch by draining in-progress ->queue_rq() for this issue. And the patch looks reasonable too.
Any comments for this issue and the solution?
block/blk-mq.c +24
-12@@ -1523,7 +1523,12 @@ static void blk_mq_rq_timed_out(struct request *req)blk_add_timer (req); } -static bool blk_mq_req_expired (struct request *rq,unsigned long *next) +struct blk_expired_data { +unsigned long next; +unsigned long now; +}; + +static bool blk_mq_req_expired (struct request *rq,struct blk_expired_data *expired) {unsigned long deadline;@@ -1533,13 +1538,13 @@ static bool blk_mq_req_expired(struct request *rq, unsigned long *next)return false; deadline =READ_ONCE (rq->deadline); -if (time_after_eq (jiffies, deadline)) +if (time_after_eq (expired->now, deadline))return true; -if (*next ==0 ) - *next = deadline; -else if (time_after (*next, deadline)) - *next = deadline; +if (expired->next ==0 ) + expired->next = deadline; +else if (time_after (expired->next, deadline)) + expired->next = deadline;return false; }@@ -1555,7 +1560,7 @@ void blk_mq_put_rq_ref(struct request *rq)static bool blk_mq_check_expired (struct request *rq,void *priv) { -unsigned long *next = priv; +struct blk_expired_data *expired = priv;/* * blk_mq_queue_tag_busy_iter() has locked the request, so it cannot @@ -1564,7 +1569,7 @@ static bool blk_mq_check_expired(struct request *rq, void *priv)* it was completed and reallocated as a new request after returning * from blk_mq_check_expired(). */ -if (blk_mq_req_expired (rq, next)) +if (blk_mq_req_expired (rq, expired))blk_mq_rq_timed_out (rq);return true; }@@ -1573,7 +1578,7 @@ static void blk_mq_timeout_work(struct work_struct *work) {struct request_queue *q = container_of(work, struct request_queue, timeout_work); -unsigned long next =0 ; +struct blk_expired_data expired = {.next =0 , .now = jiffies};struct blk_mq_hw_ctx *hctx;unsigned long i;@@ -1593,10 +1598,17 @@ static void blk_mq_timeout_work(struct work_struct *work)if (!percpu_ref_tryget (&q->q_usage_counter))return ; -blk_mq_queue_tag_busy_iter (q, blk_mq_check_expired, &next); +/* Before walking tags, we must ensure any submit started before the +* current time has finished. Since the submit uses srcu or rcu, wait +* for a synchronization point to ensure all running submits have +* finished +*/ +blk_mq_wait_quiesce_done (q); + +blk_mq_queue_tag_busy_iter (q, blk_mq_check_expired, &expired); -if (next !=0 ) { -mod_timer (&q->timeout, next); +if (expired.next !=0 ) { +mod_timer (&q->timeout, expired.next); }else {/* * Request timeouts are handled as a forward rolling timer. If
Quoted 3 lines
Thanks,
Ming
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Quoted 1 line · Ming Lei
[ ... ]
Hi Ming,
Fixing this in the block layer seems fine to me. A few comments:
block/blk-mq.c static void blk_mq_timeout_work(struct work_struct *work) · line 1605
Quoted 5 lines
block/blk-mq.c +5
-0+/* Before walking tags, we must ensure any submit started before the +* current time has finished. Since the submit uses srcu or rcu, wait +* for a synchronization point to ensure all running submits have +* finished +*/
Should the above comment follow the style of other comments in the block layer?
block/blk-mq.c static void blk_mq_timeout_work(struct work_struct *work) · line 1608
Quoted 3 lines
block/blk-mq.c +3
-0+blk_mq_wait_quiesce_done (q); + +blk_mq_queue_tag_busy_iter (q, blk_mq_check_expired, &expired);
The above doesn't look sufficient to me since .queue_rq() may be called while blk_mq_queue_tag_busy_iter() is in progress. How about moving the blk_mq_wait_quiesce_done() call into blk_mq_check_expired() and preventing new .queue_rq() calls before the timeout handler is called?
Thanks,
Bart.
Quoted 7 lines · Bart Van Assche
Quoted 1 line · Ming Lei
[ ... ]
Hi Ming,
Fixing this in the block layer seems fine to me. A few comments:
Quoted 8 lines
block/blk-mq.c static void blk_mq_timeout_work(struct work_struct *work) · line 1605
Quoted 5 lines
block/blk-mq.c +5
-0+/* Before walking tags, we must ensure any submit started before the +* current time has finished. Since the submit uses srcu or rcu, wait +* for a synchronization point to ensure all running submits have +* finished +*/ Should the above comment follow the style of other comments in the block layer?
OK.
Quoted 9 lines
block/blk-mq.c static void blk_mq_timeout_work(struct work_struct *work) · line 1608
Quoted 4 lines
block/blk-mq.c +3
-0+blk_mq_wait_quiesce_done (q); + +blk_mq_queue_tag_busy_iter (q, blk_mq_check_expired, &expired);The above doesn't look sufficient to me since .queue_rq() may be called while
blk_mq_queue_tag_busy_iter()is in progress. How about moving theblk_mq_wait_quiesce_done()call intoblk_mq_check_expired()and preventing new .queue_rq() calls before the timeout handler is called?
blk_mq_timeout_work() records the time before calling blk_mq_wait_quiesce_done(), and only handle requests which is timed out before the recorded jiffies, so new queued request won't be covered in this time.
Thanks,
Ming
Quoted 21 lines · Stefan Hajnoczi
Quoted 20 lines · Ming Lei
Hi,
David Jeffery found one double ->queue_rq() issue, so far it can be triggered in the following two cases:
1) scsi driver in guest kernel
- the story could be long vmexit latency or long preempt latency of vCPU pthread, then IO req is timed out before queuing the request to hardware but after calling
blk_mq_start_request()during ->queue_rq(), then timeout handler handles it by requeue, then double ->queue_rq() is caused, and kernel panic2) burst of kernel messages from irq handler
For 1), I think it is one reasonable case, given latency from host side can come anytime in theory because vCPU is emulated by one normal host pthread which can be preempted anywhere. For 2), I guess kernel message is supposed to be rate limited.
Quoted 8 lines
Quoted 5 lines
Firstly, is this kind of so long(30sec) random latency when running kernel code something normal? Or do we need to take care of it? IMO, it looks reasonable in case of VM, but our VM experts may have better idea about this situation. Also the default 30sec timeout could be reduced via sysfs or drivers.
30 seconds is a long latency that does not occur during normal operation, but unfortunately does happen on occasion.
Thanks for the confirmation!
Quoted 5 lines
I think there's an interest in understanding the root cause and solving long latencies (if possible) in the QEMU/KVM communities. We can investigate specific cases on [email protected] and/or [email protected].
The issue was original reported on VMware VM, but maybe David can figure out how to trigger it on QEMU/KVM.
Thanks,
Ming
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
block/blk-mq.c static void blk_mq_timeout_work(struct work_struct *work) · line 1608
Quoted 13 lines · Ming Lei
block/blk-mq.c +8
-1@@ -1593,10 +1598,17 @@ static void blk_mq_timeout_work(struct work_struct *work)if (!percpu_ref_tryget (&q->q_usage_counter))return ; -blk_mq_queue_tag_busy_iter (q, blk_mq_check_expired, &next); +/* Before walking tags, we must ensure any submit started before the +* current time has finished. Since the submit uses srcu or rcu, wait +* for a synchronization point to ensure all running submits have +* finished +*/ +blk_mq_wait_quiesce_done (q); + +blk_mq_queue_tag_busy_iter (q, blk_mq_check_expired, &expired);
The blk_mq_wait_quiesce_done() will only wait for tasks that entered just before calling that function. It will not wait for tasks that entered immediately after.
If I correctly understand the problem you're describing, the hypervisor may prevent any guest process from running. If so, the timeout work may be stalled after the quiesce, and if a queue_rq() process also stalled after starting quiesce_done(), then we're in the same situation you're trying to prevent, right?
I agree with your idea that this is a lower level driver responsibility:
it should reclaim all started requests before allowing new queuing. Perhaps the block layer should also raise a clear warning if it's queueing a request that's already started.
Quoted 5 lines · Keith Busch
Quoted 4 lines · Ming Lei
block/blk-mq.c +0
-0@@ -1593,10 +1598,17 @@ static void blk_mq_timeout_work(struct work_struct *work)if (!percpu_ref_tryget (&q->q_usage_counter))return ;
Quoted 13 lines
block/blk-mq.c static void blk_mq_timeout_work(struct work_struct *work) · line 1608
Quoted 9 lines
block/blk-mq.c +8
-1-blk_mq_queue_tag_busy_iter (q, blk_mq_check_expired, &next); +/* Before walking tags, we must ensure any submit started before the +* current time has finished. Since the submit uses srcu or rcu, wait +* for a synchronization point to ensure all running submits have +* finished +*/ +blk_mq_wait_quiesce_done (q); + +blk_mq_queue_tag_busy_iter (q, blk_mq_check_expired, &expired);The
blk_mq_wait_quiesce_done()will only wait for tasks that entered just before calling that function. It will not wait for tasks that entered immediately after.
Yeah, but the patch records the jiffies before calling blk_mq_wait_quiesce_done, and only time out requests which are timed out before the recorded time, so it is fine to use blk_mq_wait_quiesce_done in this way.
Quoted 6 lines
If I correctly understand the problem you're describing, the hypervisor may prevent any guest process from running. If so, the timeout work may be stalled after the quiesce, and if a
queue_rq()process also stalled after startingquiesce_done(), then we're in the same situation you're trying to prevent, right?
No, the stall just happens on one vCPU, and other vCPUs may run smoothly.
1) vmexit, which only stalls one vCPU, some vmexit could come anytime, such as external interrupt
2) vCPU is emulated by pthread usually, and the pthread is just one normal host userspace pthread, which can be preempted anytime, and the preempt latency could be long enough when the system load is heavy.
And it is like random stall added when running any instruction of VM kernel code.
Quoted 5 lines
I agree with your idea that this is a lower level driver responsibility:
it should reclaim all started requests before allowing new queuing. Perhaps the block layer should also raise a clear warning if it's queueing a request that's already started.
The thing is that it is one generic issue, lots of VM drivers could be affected, and it may not be easy for drivers to handle the race too.
Thanks,
Ming
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Quoted 4 lines · Keith Busch
The
blk_mq_wait_quiesce_done()will only wait for tasks that entered just before calling that function. It will not wait for tasks that entered immediately after.
Right. The design was depending on saving a jiffies value before the blk_mq_wait_quiesce_done() call and deadline behavior to avoid having to care about queue_rq calls starting after blk_mq_wait_quiesce_done().
Quoted 5 lines
If I correctly understand the problem you're describing, the hypervisor may prevent any guest process from running. If so, the timeout work may be stalled after the quiesce, and if a
queue_rq()process also stalled after startingquiesce_done(), then we're in the same situation you're trying to prevent, right?
For handling this condition and avoiding the issue, the code was depending on the deadline value and rq->state value. blk_mq_start_request sets a deadline then sets MQ_RQ_IN_FLIGHT, while blk_mq_req_expired checks for MQ_RQ_IN_FLIGHT then checks the deadline. A new queue_rq call would then not count as expired from these checks from either its deadline being updated to be in the future or from missing MQ_RQ_IN_FLIGHT .
However, there are no memory barriers ensuring this ordering. So it looks like it should prevent an issue with a new queue_rq on x86 cpus but could race on cpus with weaker memory ordering.
Quoted 5 lines
I agree with your idea that this is a lower level driver responsibility:
it should reclaim all started requests before allowing new queuing. Perhaps the block layer should also raise a clear warning if it's queueing a request that's already started.
If I understand what you are saying, that is roughly how the old scsi timeout handling used to work before the async-abort changes. It prevented new requests and drained everything (though HBA-wide not just request_queue-wide) before doing anything with timed out commands. The current implementation makes avoiding race conditions more difficult but reduces the performance impact of a lone misbehaving request.
So a driver under the block layer could potentially avoid the current races by properly quiescing the request_queue before handling any timed out requests. But I was trying to come up with a solution which avoided that requirement.
David Jeffery
Quoted 11 lines · Ming Lei
Quoted 5 lines · Keith Busch
I agree with your idea that this is a lower level driver responsibility:
it should reclaim all started requests before allowing new queuing. Perhaps the block layer should also raise a clear warning if it's queueing a request that's already started.The thing is that it is one generic issue, lots of VM drivers could be affected, and it may not be easy for drivers to handle the race too.
While virtual systems are a common source of the problem, fully preempt kernels (with or without real-time patches) can also trigger this condition rather simply with a poorly behaved real-time task. The involuntary preemption means the queue_rq call can be stopped to let another task run. Poorly behaving tasks claiming the CPU for longer than the request timeout when preempting a task in a queue_rq function could cause the condition on real or virtual hardware. So it's not just VM related drivers that are affected by the race.
David Jeffery
Quoted 13 lines · David Jeffery
Quoted 11 lines · Ming Lei
Quoted 5 lines · Keith Busch
I agree with your idea that this is a lower level driver responsibility:
it should reclaim all started requests before allowing new queuing. Perhaps the block layer should also raise a clear warning if it's queueing a request that's already started.The thing is that it is one generic issue, lots of VM drivers could be affected, and it may not be easy for drivers to handle the race too.
Quoted 8 lines
While virtual systems are a common source of the problem, fully preempt kernels (with or without real-time patches) can also trigger this condition rather simply with a poorly behaved real-time task. The involuntary preemption means the
queue_rqcall can be stopped to let another task run. Poorly behaving tasks claiming the CPU for longer than the request timeout when preempting a task in aqueue_rqfunction could cause the condition on real or virtual hardware. So it's not just VM related drivers that are affected by the race.
In theory, yes. But ->queue_rq() is in rcu read critical area, and usually CONFIG_RCU_BOOST is enabled for covering this problem otherwise OOM can be triggered easily too.
I guess it is hard to trigger it in real hardware with preempt kernel.
Thanks,
Ming
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Quoted 34 lines · Ming Lei
Quoted 21 lines · Stefan Hajnoczi
Quoted 20 lines · Ming Lei
Hi,
David Jeffery found one double ->queue_rq() issue, so far it can be triggered in the following two cases:
1) scsi driver in guest kernel
- the story could be long vmexit latency or long preempt latency of vCPU pthread, then IO req is timed out before queuing the request to hardware but after calling
blk_mq_start_request()during ->queue_rq(), then timeout handler handles it by requeue, then double ->queue_rq() is caused, and kernel panic2) burst of kernel messages from irq handler
For 1), I think it is one reasonable case, given latency from host side can come anytime in theory because vCPU is emulated by one normal host pthread which can be preempted anywhere. For 2), I guess kernel message is supposed to be rate limited.
Quoted 8 lines
Quoted 5 lines
Firstly, is this kind of so long(30sec) random latency when running kernel code something normal? Or do we need to take care of it? IMO, it looks reasonable in case of VM, but our VM experts may have better idea about this situation. Also the default 30sec timeout could be reduced via sysfs or drivers.
30 seconds is a long latency that does not occur during normal operation, but unfortunately does happen on occasion.
Thanks for the confirmation!
Quoted 1 line
Quoted 7 lines
Quoted 4 lines
I think there's an interest in understanding the root cause and solving long latencies (if possible) in the QEMU/KVM communities. We can investigate specific cases on [email protected] and/or [email protected].
The issue was original reported on VMware VM, but maybe David can figure out how to trigger it on QEMU/KVM.
A very basic question:
The virtio_blk driver has no q->mq_ops->timeout() callback. Why does the block layer still enable the timeout mechanism when the driver doesn't implement ->timeout()?
I saw there was some "idle" hctx logic and I guess the requests are resubmitted (although it wasn't obvious to me how that happens in the code)? Maybe that's why the timer is still used if the driver doesn't care about timeouts...
Stefan
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Quoted 40 lines · Stefan Hajnoczi
Quoted 39 lines · Ming Lei
Quoted 21 lines · Stefan Hajnoczi
Quoted 20 lines · Ming Lei
Hi,
David Jeffery found one double ->queue_rq() issue, so far it can be triggered in the following two cases:
1) scsi driver in guest kernel
- the story could be long vmexit latency or long preempt latency of vCPU pthread, then IO req is timed out before queuing the request to hardware but after calling
blk_mq_start_request()during ->queue_rq(), then timeout handler handles it by requeue, then double ->queue_rq() is caused, and kernel panic2) burst of kernel messages from irq handler
For 1), I think it is one reasonable case, given latency from host side can come anytime in theory because vCPU is emulated by one normal host pthread which can be preempted anywhere. For 2), I guess kernel message is supposed to be rate limited.
Quoted 8 lines
Quoted 5 lines
Firstly, is this kind of so long(30sec) random latency when running kernel code something normal? Or do we need to take care of it? IMO, it looks reasonable in case of VM, but our VM experts may have better idea about this situation. Also the default 30sec timeout could be reduced via sysfs or drivers.
30 seconds is a long latency that does not occur during normal operation, but unfortunately does happen on occasion.
Thanks for the confirmation!
Quoted 5 lines
I think there's an interest in understanding the root cause and solving long latencies (if possible) in the QEMU/KVM communities. We can investigate specific cases on [email protected] and/or [email protected].
Quoted 8 lines
Quoted 2 lines
The issue was original reported on VMware VM, but maybe David can figure out how to trigger it on QEMU/KVM.
A very basic question:
The
virtio_blkdriver has noq->mq_ops->timeout()callback. Why does the block layer still enable the timeout mechanism when the driver doesn't implement ->timeout()?
No matter if ->timeout() is implemented or not, request still may be timed out, and it is better for block layer to find such issue and simply reset timer in case of no ->timeout().
Quoted 2 lines
I saw there was some "idle" hctx logic and I guess the requests are
timeout timer is reused for idle hctx detection.
Quoted 3 lines
resubmitted (although it wasn't obvious to me how that happens in the code)? Maybe that's why the timer is still used if the driver doesn't care about timeouts...
Timeout handling is totally decided by driver's ->timeout() callback. If driver doesn't implement ->timeout(), the request's timer is reset.
Thanks
Ming
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization