Skip to content

Commit 1da08a4

Browse files
Ming Leigregkh
authored andcommitted
block: fix race between adding/removing rq qos and normal IO
[ Upstream commit 2cafe29 ] Yi reported several kernel panics on: [16687.001777] Unable to handle kernel NULL pointer dereference at virtual address 0000000000000008 ... [16687.163549] pc : __rq_qos_track+0x38/0x60 or [ 997.690455] Unable to handle kernel NULL pointer dereference at virtual address 0000000000000020 ... [ 997.850347] pc : __rq_qos_done+0x2c/0x50 Turns out it is caused by race between adding rq qos(wbt) and normal IO because rq_qos_add can be run when IO is being submitted, fix this issue by freezing queue before adding/deleting rq qos to queue. rq_qos_exit() needn't to freeze queue because it is called after queue has been frozen. iolatency calls rq_qos_add() during allocating queue, so freezing won't add delay because queue usage refcount works at atomic mode at that time. iocost calls rq_qos_add() when writing cgroup attribute file, that is fine to freeze queue at that time since we usually freeze queue when storing to queue sysfs attribute, meantime iocost only exists on the root cgroup. wbt_init calls it in blk_register_queue() and queue sysfs attribute store(queue_wb_lat_store() when write it 1st time in case of !BLK_WBT_MQ), the following patch will speedup the queue freezing in wbt_init. Reported-by: Yi Zhang <yi.zhang@redhat.com> Cc: Bart Van Assche <bvanassche@acm.org> Signed-off-by: Ming Lei <ming.lei@redhat.com> Reviewed-by: Bart Van Assche <bvanassche@acm.org> Tested-by: Yi Zhang <yi.zhang@redhat.com> Link: https://lore.kernel.org/r/20210609015822.103433-2-ming.lei@redhat.com Signed-off-by: Jens Axboe <axboe@kernel.dk> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 555dba7 commit 1da08a4

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

‎block/blk-rq-qos.h‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <linux/blk_types.h>
88
#include <linux/atomic.h>
99
#include <linux/wait.h>
10+
#include <linux/blk-mq.h>
1011

1112
#include "blk-mq-debugfs.h"
1213

@@ -99,8 +100,21 @@ static inline void rq_wait_init(struct rq_wait *rq_wait)
99100

100101
static inline void rq_qos_add(struct request_queue *q, struct rq_qos *rqos)
101102
{
103+
/*
104+
* No IO can be in-flight when adding rqos, so freeze queue, which
105+
* is fine since we only support rq_qos for blk-mq queue.
106+
*
107+
* Reuse ->queue_lock for protecting against other concurrent
108+
* rq_qos adding/deleting
109+
*/
110+
blk_mq_freeze_queue(q);
111+
112+
spin_lock_irq(&q->queue_lock);
102113
rqos->next = q->rq_qos;
103114
q->rq_qos = rqos;
115+
spin_unlock_irq(&q->queue_lock);
116+
117+
blk_mq_unfreeze_queue(q);
104118

105119
if (rqos->ops->debugfs_attrs)
106120
blk_mq_debugfs_register_rqos(rqos);
@@ -110,12 +124,22 @@ static inline void rq_qos_del(struct request_queue *q, struct rq_qos *rqos)
110124
{
111125
struct rq_qos **cur;
112126

127+
/*
128+
* See comment in rq_qos_add() about freezing queue & using
129+
* ->queue_lock.
130+
*/
131+
blk_mq_freeze_queue(q);
132+
133+
spin_lock_irq(&q->queue_lock);
113134
for (cur = &q->rq_qos; *cur; cur = &(*cur)->next) {
114135
if (*cur == rqos) {
115136
*cur = rqos->next;
116137
break;
117138
}
118139
}
140+
spin_unlock_irq(&q->queue_lock);
141+
142+
blk_mq_unfreeze_queue(q);
119143

120144
blk_mq_debugfs_unregister_rqos(rqos);
121145
}

0 commit comments

Comments
 (0)