* [PATCH 1/2] null_blk: fix handling big requests with small mbps limit
@ 2019-07-08 15:31 Konstantin Khlebnikov
2019-07-08 15:31 ` [PATCH 2/2] null_blk: fix race and oops at removing device with bandwidth limit Konstantin Khlebnikov
0 siblings, 1 reply; 2+ messages in thread
From: Konstantin Khlebnikov @ 2019-07-08 15:31 UTC (permalink / raw)
To: linux-block, Jens Axboe, linux-kernel
Small mbps limit actually limits size of request which could be handled,
because 'cur_bytes' never might be bigger than bandwidth limit over 20ms.
For example with mbps=4 device cannot handle requests bigger than 81 KiB.
This patch allows to start request bigger than 'cur_bytes' but stops queue
if 'cur_bytes' is negative. Bandwidth timer takes this into account.
Fixes: eff2c4f10873 ("nullb: bandwidth control")
Signed-off-by: Konstantin Khlebnikov <khlebnikov@yandex-team.ru>
---
drivers/block/null_blk_main.c | 24 +++++++++++++++++++-----
1 file changed, 19 insertions(+), 5 deletions(-)
diff --git a/drivers/block/null_blk_main.c b/drivers/block/null_blk_main.c
index 447d635c79a2..15925b355965 100644
--- a/drivers/block/null_blk_main.c
+++ b/drivers/block/null_blk_main.c
@@ -1132,6 +1132,18 @@ static void null_restart_queue_async(struct nullb *nullb)
blk_mq_start_stopped_hw_queues(q, true);
}
+static inline bool atomic_long_sub_unless_negative(long i, atomic_long_t *v)
+{
+ long c = atomic_long_read(v);
+
+ do {
+ if (unlikely(c < 0))
+ return false;
+ } while (!atomic_long_try_cmpxchg(v, &c, c - i));
+
+ return true;
+}
+
static blk_status_t null_handle_cmd(struct nullb_cmd *cmd)
{
struct nullb_device *dev = cmd->nq->dev;
@@ -1144,8 +1156,8 @@ static blk_status_t null_handle_cmd(struct nullb_cmd *cmd)
if (!hrtimer_active(&nullb->bw_timer))
hrtimer_restart(&nullb->bw_timer);
- if (atomic_long_sub_return(blk_rq_bytes(rq),
- &nullb->cur_bytes) < 0) {
+ if (!atomic_long_sub_unless_negative(blk_rq_bytes(rq),
+ &nullb->cur_bytes)) {
null_stop_queue(nullb);
/* race with timer */
if (atomic_long_read(&nullb->cur_bytes) > 0)
@@ -1244,12 +1256,14 @@ static enum hrtimer_restart nullb_bwtimer_fn(struct hrtimer *timer)
{
struct nullb *nullb = container_of(timer, struct nullb, bw_timer);
ktime_t timer_interval = ktime_set(0, TIMER_INTERVAL);
- unsigned int mbps = nullb->dev->mbps;
+ long budget = atomic_long_read(&nullb->cur_bytes);
+ long limit = mb_per_tick(nullb->dev->mbps);
- if (atomic_long_read(&nullb->cur_bytes) == mb_per_tick(mbps))
+ if (budget == limit)
return HRTIMER_NORESTART;
- atomic_long_set(&nullb->cur_bytes, mb_per_tick(mbps));
+ atomic_long_set(&nullb->cur_bytes, limit + min(budget, 0L));
+
null_restart_queue_async(nullb);
hrtimer_forward_now(&nullb->bw_timer, timer_interval);
^ permalink raw reply related [flat|nested] 2+ messages in thread* [PATCH 2/2] null_blk: fix race and oops at removing device with bandwidth limit
2019-07-08 15:31 [PATCH 1/2] null_blk: fix handling big requests with small mbps limit Konstantin Khlebnikov
@ 2019-07-08 15:31 ` Konstantin Khlebnikov
0 siblings, 0 replies; 2+ messages in thread
From: Konstantin Khlebnikov @ 2019-07-08 15:31 UTC (permalink / raw)
To: linux-block, Jens Axboe, linux-kernel
Function null_del_dev should disable throttling before canceling timer,
otherwise timer could be restarted by null_handle_cmd().
Remove bump of cur_bytes - without NULLB_DEV_FL_THROTTLED it has no effect.
Fixes: eff2c4f10873 ("nullb: bandwidth control")
Signed-off-by: Konstantin Khlebnikov <khlebnikov@yandex-team.ru>
---
drivers/block/null_blk_main.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/block/null_blk_main.c b/drivers/block/null_blk_main.c
index 15925b355965..2d4ba7b05e2f 100644
--- a/drivers/block/null_blk_main.c
+++ b/drivers/block/null_blk_main.c
@@ -1398,8 +1398,8 @@ static void null_del_dev(struct nullb *nullb)
del_gendisk(nullb->disk);
if (test_bit(NULLB_DEV_FL_THROTTLED, &nullb->dev->flags)) {
+ clear_bit(NULLB_DEV_FL_THROTTLED, &nullb->dev->flags);
hrtimer_cancel(&nullb->bw_timer);
- atomic_long_set(&nullb->cur_bytes, LONG_MAX);
null_restart_queue_async(nullb);
}
^ permalink raw reply related [flat|nested] 2+ messages in thread
end of thread, other threads:[~2019-07-08 15:38 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-07-08 15:31 [PATCH 1/2] null_blk: fix handling big requests with small mbps limit Konstantin Khlebnikov
2019-07-08 15:31 ` [PATCH 2/2] null_blk: fix race and oops at removing device with bandwidth limit Konstantin Khlebnikov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox