2019-04-30 18:42:43 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
2018-07-03 15:32:35 +00:00
|
|
|
#include "blk-rq-qos.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Increment 'v', if 'v' is below 'below'. Returns true if we succeeded,
|
|
|
|
* false if 'v' + 1 would be bigger than 'below'.
|
|
|
|
*/
|
2018-07-20 01:42:13 +00:00
|
|
|
static bool atomic_inc_below(atomic_t *v, unsigned int below)
|
2018-07-03 15:32:35 +00:00
|
|
|
{
|
2018-07-20 01:42:13 +00:00
|
|
|
unsigned int cur = atomic_read(v);
|
2018-07-03 15:32:35 +00:00
|
|
|
|
2022-07-12 15:05:47 +00:00
|
|
|
do {
|
2018-07-03 15:32:35 +00:00
|
|
|
if (cur >= below)
|
|
|
|
return false;
|
2022-07-12 15:05:47 +00:00
|
|
|
} while (!atomic_try_cmpxchg(v, &cur, cur + 1));
|
2018-07-03 15:32:35 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-20 01:42:13 +00:00
|
|
|
bool rq_wait_inc_below(struct rq_wait *rq_wait, unsigned int limit)
|
2018-07-03 15:32:35 +00:00
|
|
|
{
|
|
|
|
return atomic_inc_below(&rq_wait->inflight, limit);
|
|
|
|
}
|
|
|
|
|
2018-11-15 19:25:10 +00:00
|
|
|
void __rq_qos_cleanup(struct rq_qos *rqos, struct bio *bio)
|
2018-07-03 15:32:35 +00:00
|
|
|
{
|
2018-11-15 19:25:10 +00:00
|
|
|
do {
|
2018-07-03 15:32:35 +00:00
|
|
|
if (rqos->ops->cleanup)
|
2018-07-03 15:14:59 +00:00
|
|
|
rqos->ops->cleanup(rqos, bio);
|
2018-11-15 19:25:10 +00:00
|
|
|
rqos = rqos->next;
|
|
|
|
} while (rqos);
|
2018-07-03 15:32:35 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 19:25:10 +00:00
|
|
|
void __rq_qos_done(struct rq_qos *rqos, struct request *rq)
|
2018-07-03 15:32:35 +00:00
|
|
|
{
|
2018-11-15 19:25:10 +00:00
|
|
|
do {
|
2018-07-03 15:32:35 +00:00
|
|
|
if (rqos->ops->done)
|
|
|
|
rqos->ops->done(rqos, rq);
|
2018-11-15 19:25:10 +00:00
|
|
|
rqos = rqos->next;
|
|
|
|
} while (rqos);
|
2018-07-03 15:32:35 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 19:25:10 +00:00
|
|
|
void __rq_qos_issue(struct rq_qos *rqos, struct request *rq)
|
2018-07-03 15:32:35 +00:00
|
|
|
{
|
2018-11-15 19:25:10 +00:00
|
|
|
do {
|
2018-07-03 15:32:35 +00:00
|
|
|
if (rqos->ops->issue)
|
|
|
|
rqos->ops->issue(rqos, rq);
|
2018-11-15 19:25:10 +00:00
|
|
|
rqos = rqos->next;
|
|
|
|
} while (rqos);
|
2018-07-03 15:32:35 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 19:25:10 +00:00
|
|
|
void __rq_qos_requeue(struct rq_qos *rqos, struct request *rq)
|
2018-07-03 15:32:35 +00:00
|
|
|
{
|
2018-11-15 19:25:10 +00:00
|
|
|
do {
|
2018-07-03 15:32:35 +00:00
|
|
|
if (rqos->ops->requeue)
|
|
|
|
rqos->ops->requeue(rqos, rq);
|
2018-11-15 19:25:10 +00:00
|
|
|
rqos = rqos->next;
|
|
|
|
} while (rqos);
|
2018-07-03 15:32:35 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 19:25:10 +00:00
|
|
|
void __rq_qos_throttle(struct rq_qos *rqos, struct bio *bio)
|
2018-07-03 15:32:35 +00:00
|
|
|
{
|
2018-11-15 19:25:10 +00:00
|
|
|
do {
|
2018-07-03 15:32:35 +00:00
|
|
|
if (rqos->ops->throttle)
|
2018-11-14 16:02:09 +00:00
|
|
|
rqos->ops->throttle(rqos, bio);
|
2018-11-15 19:25:10 +00:00
|
|
|
rqos = rqos->next;
|
|
|
|
} while (rqos);
|
2018-07-03 15:14:59 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 19:25:10 +00:00
|
|
|
void __rq_qos_track(struct rq_qos *rqos, struct request *rq, struct bio *bio)
|
2018-07-03 15:14:59 +00:00
|
|
|
{
|
2018-11-15 19:25:10 +00:00
|
|
|
do {
|
2018-07-03 15:14:59 +00:00
|
|
|
if (rqos->ops->track)
|
|
|
|
rqos->ops->track(rqos, rq, bio);
|
2019-08-28 22:05:54 +00:00
|
|
|
rqos = rqos->next;
|
|
|
|
} while (rqos);
|
|
|
|
}
|
|
|
|
|
|
|
|
void __rq_qos_merge(struct rq_qos *rqos, struct request *rq, struct bio *bio)
|
|
|
|
{
|
|
|
|
do {
|
|
|
|
if (rqos->ops->merge)
|
|
|
|
rqos->ops->merge(rqos, rq, bio);
|
2018-11-15 19:25:10 +00:00
|
|
|
rqos = rqos->next;
|
|
|
|
} while (rqos);
|
2018-07-03 15:32:35 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 19:25:10 +00:00
|
|
|
void __rq_qos_done_bio(struct rq_qos *rqos, struct bio *bio)
|
2018-07-03 15:15:00 +00:00
|
|
|
{
|
2018-11-15 19:25:10 +00:00
|
|
|
do {
|
2018-07-03 15:15:00 +00:00
|
|
|
if (rqos->ops->done_bio)
|
|
|
|
rqos->ops->done_bio(rqos, bio);
|
2018-11-15 19:25:10 +00:00
|
|
|
rqos = rqos->next;
|
|
|
|
} while (rqos);
|
2018-07-03 15:15:00 +00:00
|
|
|
}
|
|
|
|
|
2019-08-28 22:05:55 +00:00
|
|
|
void __rq_qos_queue_depth_changed(struct rq_qos *rqos)
|
|
|
|
{
|
|
|
|
do {
|
|
|
|
if (rqos->ops->queue_depth_changed)
|
|
|
|
rqos->ops->queue_depth_changed(rqos);
|
|
|
|
rqos = rqos->next;
|
|
|
|
} while (rqos);
|
|
|
|
}
|
|
|
|
|
2018-07-03 15:32:35 +00:00
|
|
|
/*
|
|
|
|
* Return true, if we can't increase the depth further by scaling
|
|
|
|
*/
|
|
|
|
bool rq_depth_calc_max_depth(struct rq_depth *rqd)
|
|
|
|
{
|
|
|
|
unsigned int depth;
|
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For QD=1 devices, this is a special case. It's important for those
|
|
|
|
* to have one request ready when one completes, so force a depth of
|
|
|
|
* 2 for those devices. On the backend, it'll be a depth of 1 anyway,
|
|
|
|
* since the device can't have more than that in flight. If we're
|
|
|
|
* scaling down, then keep a setting of 1/1/1.
|
|
|
|
*/
|
|
|
|
if (rqd->queue_depth == 1) {
|
|
|
|
if (rqd->scale_step > 0)
|
|
|
|
rqd->max_depth = 1;
|
|
|
|
else {
|
|
|
|
rqd->max_depth = 2;
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* scale_step == 0 is our default state. If we have suffered
|
|
|
|
* latency spikes, step will be > 0, and we shrink the
|
|
|
|
* allowed write depths. If step is < 0, we're only doing
|
|
|
|
* writes, and we allow a temporarily higher depth to
|
|
|
|
* increase performance.
|
|
|
|
*/
|
|
|
|
depth = min_t(unsigned int, rqd->default_depth,
|
|
|
|
rqd->queue_depth);
|
|
|
|
if (rqd->scale_step > 0)
|
|
|
|
depth = 1 + ((depth - 1) >> min(31, rqd->scale_step));
|
|
|
|
else if (rqd->scale_step < 0) {
|
|
|
|
unsigned int maxd = 3 * rqd->queue_depth / 4;
|
|
|
|
|
|
|
|
depth = 1 + ((depth - 1) << -rqd->scale_step);
|
|
|
|
if (depth > maxd) {
|
|
|
|
depth = maxd;
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rqd->max_depth = depth;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-10-05 18:59:27 +00:00
|
|
|
/* Returns true on success and false if scaling up wasn't possible */
|
|
|
|
bool rq_depth_scale_up(struct rq_depth *rqd)
|
2018-07-03 15:32:35 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Hit max in previous round, stop here
|
|
|
|
*/
|
|
|
|
if (rqd->scaled_max)
|
2019-10-05 18:59:27 +00:00
|
|
|
return false;
|
2018-07-03 15:32:35 +00:00
|
|
|
|
|
|
|
rqd->scale_step--;
|
|
|
|
|
|
|
|
rqd->scaled_max = rq_depth_calc_max_depth(rqd);
|
2019-10-05 18:59:27 +00:00
|
|
|
return true;
|
2018-07-03 15:32:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Scale rwb down. If 'hard_throttle' is set, do it quicker, since we
|
2019-10-05 18:59:27 +00:00
|
|
|
* had a latency violation. Returns true on success and returns false if
|
|
|
|
* scaling down wasn't possible.
|
2018-07-03 15:32:35 +00:00
|
|
|
*/
|
2019-10-05 18:59:27 +00:00
|
|
|
bool rq_depth_scale_down(struct rq_depth *rqd, bool hard_throttle)
|
2018-07-03 15:32:35 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Stop scaling down when we've hit the limit. This also prevents
|
|
|
|
* ->scale_step from going to crazy values, if the device can't
|
|
|
|
* keep up.
|
|
|
|
*/
|
|
|
|
if (rqd->max_depth == 1)
|
2019-10-05 18:59:27 +00:00
|
|
|
return false;
|
2018-07-03 15:32:35 +00:00
|
|
|
|
|
|
|
if (rqd->scale_step < 0 && hard_throttle)
|
|
|
|
rqd->scale_step = 0;
|
|
|
|
else
|
|
|
|
rqd->scale_step++;
|
|
|
|
|
|
|
|
rqd->scaled_max = false;
|
|
|
|
rq_depth_calc_max_depth(rqd);
|
2019-10-05 18:59:27 +00:00
|
|
|
return true;
|
2018-07-03 15:32:35 +00:00
|
|
|
}
|
|
|
|
|
2018-12-04 17:59:02 +00:00
|
|
|
struct rq_qos_wait_data {
|
|
|
|
struct wait_queue_entry wq;
|
|
|
|
struct task_struct *task;
|
|
|
|
struct rq_wait *rqw;
|
|
|
|
acquire_inflight_cb_t *cb;
|
|
|
|
void *private_data;
|
|
|
|
bool got_token;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int rq_qos_wake_function(struct wait_queue_entry *curr,
|
|
|
|
unsigned int mode, int wake_flags, void *key)
|
|
|
|
{
|
|
|
|
struct rq_qos_wait_data *data = container_of(curr,
|
|
|
|
struct rq_qos_wait_data,
|
|
|
|
wq);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we fail to get a budget, return -1 to interrupt the wake up loop
|
|
|
|
* in __wake_up_common.
|
|
|
|
*/
|
|
|
|
if (!data->cb(data->rqw, data->private_data))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
data->got_token = true;
|
2019-07-16 20:19:29 +00:00
|
|
|
smp_wmb();
|
2018-12-04 17:59:02 +00:00
|
|
|
wake_up_process(data->task);
|
blk-rq-qos: fix crash on rq_qos_wait vs. rq_qos_wake_function race
We're seeing crashes from rq_qos_wake_function that look like this:
BUG: unable to handle page fault for address: ffffafe180a40084
#PF: supervisor write access in kernel mode
#PF: error_code(0x0002) - not-present page
PGD 100000067 P4D 100000067 PUD 10027c067 PMD 10115d067 PTE 0
Oops: Oops: 0002 [#1] PREEMPT SMP PTI
CPU: 17 UID: 0 PID: 0 Comm: swapper/17 Not tainted 6.12.0-rc3-00013-geca631b8fe80 #11
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.16.0-0-gd239552ce722-prebuilt.qemu.org 04/01/2014
RIP: 0010:_raw_spin_lock_irqsave+0x1d/0x40
Code: 90 90 90 90 90 90 90 90 90 90 90 90 90 f3 0f 1e fa 0f 1f 44 00 00 41 54 9c 41 5c fa 65 ff 05 62 97 30 4c 31 c0 ba 01 00 00 00 <f0> 0f b1 17 75 0a 4c 89 e0 41 5c c3 cc cc cc cc 89 c6 e8 2c 0b 00
RSP: 0018:ffffafe180580ca0 EFLAGS: 00010046
RAX: 0000000000000000 RBX: ffffafe180a3f7a8 RCX: 0000000000000011
RDX: 0000000000000001 RSI: 0000000000000003 RDI: ffffafe180a40084
RBP: 0000000000000000 R08: 00000000001e7240 R09: 0000000000000011
R10: 0000000000000028 R11: 0000000000000888 R12: 0000000000000002
R13: ffffafe180a40084 R14: 0000000000000000 R15: 0000000000000003
FS: 0000000000000000(0000) GS:ffff9aaf1f280000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: ffffafe180a40084 CR3: 000000010e428002 CR4: 0000000000770ef0
DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
PKRU: 55555554
Call Trace:
<IRQ>
try_to_wake_up+0x5a/0x6a0
rq_qos_wake_function+0x71/0x80
__wake_up_common+0x75/0xa0
__wake_up+0x36/0x60
scale_up.part.0+0x50/0x110
wb_timer_fn+0x227/0x450
...
So rq_qos_wake_function() calls wake_up_process(data->task), which calls
try_to_wake_up(), which faults in raw_spin_lock_irqsave(&p->pi_lock).
p comes from data->task, and data comes from the waitqueue entry, which
is stored on the waiter's stack in rq_qos_wait(). Analyzing the core
dump with drgn, I found that the waiter had already woken up and moved
on to a completely unrelated code path, clobbering what was previously
data->task. Meanwhile, the waker was passing the clobbered garbage in
data->task to wake_up_process(), leading to the crash.
What's happening is that in between rq_qos_wake_function() deleting the
waitqueue entry and calling wake_up_process(), rq_qos_wait() is finding
that it already got a token and returning. The race looks like this:
rq_qos_wait() rq_qos_wake_function()
==============================================================
prepare_to_wait_exclusive()
data->got_token = true;
list_del_init(&curr->entry);
if (data.got_token)
break;
finish_wait(&rqw->wait, &data.wq);
^- returns immediately because
list_empty_careful(&wq_entry->entry)
is true
... return, go do something else ...
wake_up_process(data->task)
(NO LONGER VALID!)-^
Normally, finish_wait() is supposed to synchronize against the waker.
But, as noted above, it is returning immediately because the waitqueue
entry has already been removed from the waitqueue.
The bug is that rq_qos_wake_function() is accessing the waitqueue entry
AFTER deleting it. Note that autoremove_wake_function() wakes the waiter
and THEN deletes the waitqueue entry, which is the proper order.
Fix it by swapping the order. We also need to use
list_del_init_careful() to match the list_empty_careful() in
finish_wait().
Fixes: 38cfb5a45ee0 ("blk-wbt: improve waking of tasks")
Cc: stable@vger.kernel.org
Signed-off-by: Omar Sandoval <osandov@fb.com>
Acked-by: Tejun Heo <tj@kernel.org>
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Link: https://lore.kernel.org/r/d3bee2463a67b1ee597211823bf7ad3721c26e41.1729014591.git.osandov@fb.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2024-10-15 17:59:46 +00:00
|
|
|
list_del_init_careful(&curr->entry);
|
2018-12-04 17:59:02 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* rq_qos_wait - throttle on a rqw if we need to
|
2019-05-31 00:00:50 +00:00
|
|
|
* @rqw: rqw to throttle on
|
|
|
|
* @private_data: caller provided specific data
|
|
|
|
* @acquire_inflight_cb: inc the rqw->inflight counter if we can
|
|
|
|
* @cleanup_cb: the callback to cleanup in case we race with a waker
|
2018-12-04 17:59:02 +00:00
|
|
|
*
|
|
|
|
* This provides a uniform place for the rq_qos users to do their throttling.
|
|
|
|
* Since you can end up with a lot of things sleeping at once, this manages the
|
|
|
|
* waking up based on the resources available. The acquire_inflight_cb should
|
|
|
|
* inc the rqw->inflight if we have the ability to do so, or return false if not
|
|
|
|
* and then we will sleep until the room becomes available.
|
|
|
|
*
|
|
|
|
* cleanup_cb is in case that we race with a waker and need to cleanup the
|
|
|
|
* inflight count accordingly.
|
|
|
|
*/
|
|
|
|
void rq_qos_wait(struct rq_wait *rqw, void *private_data,
|
|
|
|
acquire_inflight_cb_t *acquire_inflight_cb,
|
|
|
|
cleanup_cb_t *cleanup_cb)
|
|
|
|
{
|
|
|
|
struct rq_qos_wait_data data = {
|
|
|
|
.wq = {
|
|
|
|
.func = rq_qos_wake_function,
|
|
|
|
.entry = LIST_HEAD_INIT(data.wq.entry),
|
|
|
|
},
|
|
|
|
.task = current,
|
|
|
|
.rqw = rqw,
|
|
|
|
.cb = acquire_inflight_cb,
|
|
|
|
.private_data = private_data,
|
|
|
|
};
|
|
|
|
bool has_sleeper;
|
|
|
|
|
|
|
|
has_sleeper = wq_has_sleeper(&rqw->wait);
|
|
|
|
if (!has_sleeper && acquire_inflight_cb(rqw, private_data))
|
|
|
|
return;
|
|
|
|
|
2021-06-07 11:26:13 +00:00
|
|
|
has_sleeper = !prepare_to_wait_exclusive(&rqw->wait, &data.wq,
|
|
|
|
TASK_UNINTERRUPTIBLE);
|
2018-12-04 17:59:02 +00:00
|
|
|
do {
|
2024-09-03 17:22:14 +00:00
|
|
|
/* The memory barrier in set_current_state saves us here. */
|
2018-12-04 17:59:02 +00:00
|
|
|
if (data.got_token)
|
|
|
|
break;
|
|
|
|
if (!has_sleeper && acquire_inflight_cb(rqw, private_data)) {
|
|
|
|
finish_wait(&rqw->wait, &data.wq);
|
|
|
|
|
|
|
|
/*
|
2023-09-14 09:15:08 +00:00
|
|
|
* We raced with rq_qos_wake_function() getting a token,
|
2018-12-04 17:59:02 +00:00
|
|
|
* which means we now have two. Put our local token
|
|
|
|
* and wake anyone else potentially waiting for one.
|
|
|
|
*/
|
2019-07-16 20:19:29 +00:00
|
|
|
smp_rmb();
|
2018-12-04 17:59:02 +00:00
|
|
|
if (data.got_token)
|
|
|
|
cleanup_cb(rqw, private_data);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
io_schedule();
|
2019-07-16 20:19:27 +00:00
|
|
|
has_sleeper = true;
|
2019-07-16 20:19:28 +00:00
|
|
|
set_current_state(TASK_UNINTERRUPTIBLE);
|
2018-12-04 17:59:02 +00:00
|
|
|
} while (1);
|
|
|
|
finish_wait(&rqw->wait, &data.wq);
|
|
|
|
}
|
|
|
|
|
2018-07-03 15:32:35 +00:00
|
|
|
void rq_qos_exit(struct request_queue *q)
|
|
|
|
{
|
block/rq_qos: protect rq_qos apis with a new lock
commit 50e34d78815e ("block: disable the elevator int del_gendisk")
move rq_qos_exit() from disk_release() to del_gendisk(), this will
introduce some problems:
1) If rq_qos_add() is triggered by enabling iocost/iolatency through
cgroupfs, then it can concurrent with del_gendisk(), it's not safe to
write 'q->rq_qos' concurrently.
2) Activate cgroup policy that is relied on rq_qos will call
rq_qos_add() and blkcg_activate_policy(), and if rq_qos_exit() is
called in the middle, null-ptr-dereference will be triggered in
blkcg_activate_policy().
3) blkg_conf_open_bdev() can call blkdev_get_no_open() first to find the
disk, then if rq_qos_exit() from del_gendisk() is done before
rq_qos_add(), then memory will be leaked.
This patch add a new disk level mutex 'rq_qos_mutex':
1) The lock will protect rq_qos_exit() directly.
2) For wbt that doesn't relied on blk-cgroup, rq_qos_add() can only be
called from disk initialization for now because wbt can't be
destructed until rq_qos_exit(), so it's safe not to protect wbt for
now. Hoever, in case that rq_qos dynamically destruction is supported
in the furture, this patch also protect rq_qos_add() from wbt_init()
directly, this is enough because blk-sysfs already synchronize
writers with disk removal.
3) For iocost and iolatency, in order to synchronize disk removal and
cgroup configuration, the lock is held after blkdev_get_no_open()
from blkg_conf_open_bdev(), and is released in blkg_conf_exit().
In order to fix the above memory leak, disk_live() is checked after
holding the new lock.
Fixes: 50e34d78815e ("block: disable the elevator int del_gendisk")
Signed-off-by: Yu Kuai <yukuai3@huawei.com>
Acked-by: Tejun Heo <tj@kernel.org>
Link: https://lore.kernel.org/r/20230414084008.2085155-1-yukuai1@huaweicloud.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2023-04-14 08:40:08 +00:00
|
|
|
mutex_lock(&q->rq_qos_mutex);
|
2018-07-03 15:32:35 +00:00
|
|
|
while (q->rq_qos) {
|
|
|
|
struct rq_qos *rqos = q->rq_qos;
|
|
|
|
q->rq_qos = rqos->next;
|
|
|
|
rqos->ops->exit(rqos);
|
|
|
|
}
|
block/rq_qos: protect rq_qos apis with a new lock
commit 50e34d78815e ("block: disable the elevator int del_gendisk")
move rq_qos_exit() from disk_release() to del_gendisk(), this will
introduce some problems:
1) If rq_qos_add() is triggered by enabling iocost/iolatency through
cgroupfs, then it can concurrent with del_gendisk(), it's not safe to
write 'q->rq_qos' concurrently.
2) Activate cgroup policy that is relied on rq_qos will call
rq_qos_add() and blkcg_activate_policy(), and if rq_qos_exit() is
called in the middle, null-ptr-dereference will be triggered in
blkcg_activate_policy().
3) blkg_conf_open_bdev() can call blkdev_get_no_open() first to find the
disk, then if rq_qos_exit() from del_gendisk() is done before
rq_qos_add(), then memory will be leaked.
This patch add a new disk level mutex 'rq_qos_mutex':
1) The lock will protect rq_qos_exit() directly.
2) For wbt that doesn't relied on blk-cgroup, rq_qos_add() can only be
called from disk initialization for now because wbt can't be
destructed until rq_qos_exit(), so it's safe not to protect wbt for
now. Hoever, in case that rq_qos dynamically destruction is supported
in the furture, this patch also protect rq_qos_add() from wbt_init()
directly, this is enough because blk-sysfs already synchronize
writers with disk removal.
3) For iocost and iolatency, in order to synchronize disk removal and
cgroup configuration, the lock is held after blkdev_get_no_open()
from blkg_conf_open_bdev(), and is released in blkg_conf_exit().
In order to fix the above memory leak, disk_live() is checked after
holding the new lock.
Fixes: 50e34d78815e ("block: disable the elevator int del_gendisk")
Signed-off-by: Yu Kuai <yukuai3@huawei.com>
Acked-by: Tejun Heo <tj@kernel.org>
Link: https://lore.kernel.org/r/20230414084008.2085155-1-yukuai1@huaweicloud.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2023-04-14 08:40:08 +00:00
|
|
|
mutex_unlock(&q->rq_qos_mutex);
|
2018-07-03 15:32:35 +00:00
|
|
|
}
|
2023-02-03 15:03:53 +00:00
|
|
|
|
2023-02-03 15:03:54 +00:00
|
|
|
int rq_qos_add(struct rq_qos *rqos, struct gendisk *disk, enum rq_qos_id id,
|
2023-02-03 15:03:55 +00:00
|
|
|
const struct rq_qos_ops *ops)
|
2023-02-03 15:03:53 +00:00
|
|
|
{
|
2023-02-03 15:03:54 +00:00
|
|
|
struct request_queue *q = disk->queue;
|
|
|
|
|
block/rq_qos: protect rq_qos apis with a new lock
commit 50e34d78815e ("block: disable the elevator int del_gendisk")
move rq_qos_exit() from disk_release() to del_gendisk(), this will
introduce some problems:
1) If rq_qos_add() is triggered by enabling iocost/iolatency through
cgroupfs, then it can concurrent with del_gendisk(), it's not safe to
write 'q->rq_qos' concurrently.
2) Activate cgroup policy that is relied on rq_qos will call
rq_qos_add() and blkcg_activate_policy(), and if rq_qos_exit() is
called in the middle, null-ptr-dereference will be triggered in
blkcg_activate_policy().
3) blkg_conf_open_bdev() can call blkdev_get_no_open() first to find the
disk, then if rq_qos_exit() from del_gendisk() is done before
rq_qos_add(), then memory will be leaked.
This patch add a new disk level mutex 'rq_qos_mutex':
1) The lock will protect rq_qos_exit() directly.
2) For wbt that doesn't relied on blk-cgroup, rq_qos_add() can only be
called from disk initialization for now because wbt can't be
destructed until rq_qos_exit(), so it's safe not to protect wbt for
now. Hoever, in case that rq_qos dynamically destruction is supported
in the furture, this patch also protect rq_qos_add() from wbt_init()
directly, this is enough because blk-sysfs already synchronize
writers with disk removal.
3) For iocost and iolatency, in order to synchronize disk removal and
cgroup configuration, the lock is held after blkdev_get_no_open()
from blkg_conf_open_bdev(), and is released in blkg_conf_exit().
In order to fix the above memory leak, disk_live() is checked after
holding the new lock.
Fixes: 50e34d78815e ("block: disable the elevator int del_gendisk")
Signed-off-by: Yu Kuai <yukuai3@huawei.com>
Acked-by: Tejun Heo <tj@kernel.org>
Link: https://lore.kernel.org/r/20230414084008.2085155-1-yukuai1@huaweicloud.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2023-04-14 08:40:08 +00:00
|
|
|
lockdep_assert_held(&q->rq_qos_mutex);
|
|
|
|
|
2023-02-03 15:03:56 +00:00
|
|
|
rqos->disk = disk;
|
2023-02-03 15:03:54 +00:00
|
|
|
rqos->id = id;
|
|
|
|
rqos->ops = ops;
|
|
|
|
|
2023-02-03 15:03:53 +00:00
|
|
|
/*
|
|
|
|
* No IO can be in-flight when adding rqos, so freeze queue, which
|
|
|
|
* is fine since we only support rq_qos for blk-mq queue.
|
|
|
|
*/
|
|
|
|
blk_mq_freeze_queue(q);
|
|
|
|
|
|
|
|
if (rq_qos_id(q, rqos->id))
|
|
|
|
goto ebusy;
|
|
|
|
rqos->next = q->rq_qos;
|
|
|
|
q->rq_qos = rqos;
|
|
|
|
|
|
|
|
blk_mq_unfreeze_queue(q);
|
|
|
|
|
|
|
|
if (rqos->ops->debugfs_attrs) {
|
|
|
|
mutex_lock(&q->debugfs_mutex);
|
|
|
|
blk_mq_debugfs_register_rqos(rqos);
|
|
|
|
mutex_unlock(&q->debugfs_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
ebusy:
|
|
|
|
blk_mq_unfreeze_queue(q);
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
2023-02-03 15:03:54 +00:00
|
|
|
void rq_qos_del(struct rq_qos *rqos)
|
2023-02-03 15:03:53 +00:00
|
|
|
{
|
2023-02-03 15:03:56 +00:00
|
|
|
struct request_queue *q = rqos->disk->queue;
|
2023-02-03 15:03:53 +00:00
|
|
|
struct rq_qos **cur;
|
|
|
|
|
block/rq_qos: protect rq_qos apis with a new lock
commit 50e34d78815e ("block: disable the elevator int del_gendisk")
move rq_qos_exit() from disk_release() to del_gendisk(), this will
introduce some problems:
1) If rq_qos_add() is triggered by enabling iocost/iolatency through
cgroupfs, then it can concurrent with del_gendisk(), it's not safe to
write 'q->rq_qos' concurrently.
2) Activate cgroup policy that is relied on rq_qos will call
rq_qos_add() and blkcg_activate_policy(), and if rq_qos_exit() is
called in the middle, null-ptr-dereference will be triggered in
blkcg_activate_policy().
3) blkg_conf_open_bdev() can call blkdev_get_no_open() first to find the
disk, then if rq_qos_exit() from del_gendisk() is done before
rq_qos_add(), then memory will be leaked.
This patch add a new disk level mutex 'rq_qos_mutex':
1) The lock will protect rq_qos_exit() directly.
2) For wbt that doesn't relied on blk-cgroup, rq_qos_add() can only be
called from disk initialization for now because wbt can't be
destructed until rq_qos_exit(), so it's safe not to protect wbt for
now. Hoever, in case that rq_qos dynamically destruction is supported
in the furture, this patch also protect rq_qos_add() from wbt_init()
directly, this is enough because blk-sysfs already synchronize
writers with disk removal.
3) For iocost and iolatency, in order to synchronize disk removal and
cgroup configuration, the lock is held after blkdev_get_no_open()
from blkg_conf_open_bdev(), and is released in blkg_conf_exit().
In order to fix the above memory leak, disk_live() is checked after
holding the new lock.
Fixes: 50e34d78815e ("block: disable the elevator int del_gendisk")
Signed-off-by: Yu Kuai <yukuai3@huawei.com>
Acked-by: Tejun Heo <tj@kernel.org>
Link: https://lore.kernel.org/r/20230414084008.2085155-1-yukuai1@huaweicloud.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2023-04-14 08:40:08 +00:00
|
|
|
lockdep_assert_held(&q->rq_qos_mutex);
|
2023-02-03 15:03:53 +00:00
|
|
|
|
block/rq_qos: protect rq_qos apis with a new lock
commit 50e34d78815e ("block: disable the elevator int del_gendisk")
move rq_qos_exit() from disk_release() to del_gendisk(), this will
introduce some problems:
1) If rq_qos_add() is triggered by enabling iocost/iolatency through
cgroupfs, then it can concurrent with del_gendisk(), it's not safe to
write 'q->rq_qos' concurrently.
2) Activate cgroup policy that is relied on rq_qos will call
rq_qos_add() and blkcg_activate_policy(), and if rq_qos_exit() is
called in the middle, null-ptr-dereference will be triggered in
blkcg_activate_policy().
3) blkg_conf_open_bdev() can call blkdev_get_no_open() first to find the
disk, then if rq_qos_exit() from del_gendisk() is done before
rq_qos_add(), then memory will be leaked.
This patch add a new disk level mutex 'rq_qos_mutex':
1) The lock will protect rq_qos_exit() directly.
2) For wbt that doesn't relied on blk-cgroup, rq_qos_add() can only be
called from disk initialization for now because wbt can't be
destructed until rq_qos_exit(), so it's safe not to protect wbt for
now. Hoever, in case that rq_qos dynamically destruction is supported
in the furture, this patch also protect rq_qos_add() from wbt_init()
directly, this is enough because blk-sysfs already synchronize
writers with disk removal.
3) For iocost and iolatency, in order to synchronize disk removal and
cgroup configuration, the lock is held after blkdev_get_no_open()
from blkg_conf_open_bdev(), and is released in blkg_conf_exit().
In order to fix the above memory leak, disk_live() is checked after
holding the new lock.
Fixes: 50e34d78815e ("block: disable the elevator int del_gendisk")
Signed-off-by: Yu Kuai <yukuai3@huawei.com>
Acked-by: Tejun Heo <tj@kernel.org>
Link: https://lore.kernel.org/r/20230414084008.2085155-1-yukuai1@huaweicloud.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2023-04-14 08:40:08 +00:00
|
|
|
blk_mq_freeze_queue(q);
|
2023-02-03 15:03:53 +00:00
|
|
|
for (cur = &q->rq_qos; *cur; cur = &(*cur)->next) {
|
|
|
|
if (*cur == rqos) {
|
|
|
|
*cur = rqos->next;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
blk_mq_unfreeze_queue(q);
|
|
|
|
|
|
|
|
mutex_lock(&q->debugfs_mutex);
|
|
|
|
blk_mq_debugfs_unregister_rqos(rqos);
|
|
|
|
mutex_unlock(&q->debugfs_mutex);
|
|
|
|
}
|