Skip to contentSkip to conversation

null_blk: fix handling big requests with small mbps limit

PATCH New Konstantin Khlebnikov 1 participant 2 messages Jul 8, 2019 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 null_blk: fix handling big requests with small mbps limit

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")
---
 drivers/block/null_blk_main.c |   24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)
drivers/block/null_blk_main.c +19 -5
index 447d635c79a2..15925b355965 100644
@@ -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);
#1 lore raw

PATCH 2/2 null_blk: fix race and oops at removing device with bandwidth limit

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")
---
 drivers/block/null_blk_main.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/block/null_blk_main.c +1 -1
index 15925b355965..2d4ba7b05e2f 100644
@@ -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);
 	}