Skip to content

Commit 87760e5

Browse files
committed
block: hook up writeback throttling
Enable throttling of buffered writeback to make it a lot more smooth, and has way less impact on other system activity. Background writeback should be, by definition, background activity. The fact that we flush huge bundles of it at the time means that it potentially has heavy impacts on foreground workloads, which isn't ideal. We can't easily limit the sizes of writes that we do, since that would impact file system layout in the presence of delayed allocation. So just throttle back buffered writeback, unless someone is waiting for it. The algorithm for when to throttle takes its inspiration in the CoDel networking scheduling algorithm. Like CoDel, blk-wb monitors the minimum latencies of requests over a window of time. In that window of time, if the minimum latency of any request exceeds a given target, then a scale count is incremented and the queue depth is shrunk. The next monitoring window is shrunk accordingly. Unlike CoDel, if we hit a window that exhibits good behavior, then we simply increment the scale count and re-calculate the limits for that scale value. This prevents us from oscillating between a close-to-ideal value and max all the time, instead remaining in the windows where we get good behavior. Unlike CoDel, blk-wb allows the scale count to to negative. This happens if we primarily have writes going on. Unlike positive scale counts, this doesn't change the size of the monitoring window. When the heavy writers finish, blk-bw quickly snaps back to it's stable state of a zero scale count. The patch registers a sysfs entry, 'wb_lat_usec'. This sets the latency target to me met. It defaults to 2 msec for non-rotational storage, and 75 msec for rotational storage. Setting this value to '0' disables blk-wb. Generally, a user would not have to touch this setting. We don't enable WBT on devices that are managed with CFQ, and have a non-root block cgroup attached. If we have a proportional share setup on this particular disk, then the wbt throttling will interfere with that. We don't have a strong need for wbt for that case, since we will rely on CFQ doing that for us. Signed-off-by: Jens Axboe <axboe@fb.com>
1 parent e34cbd3 commit 87760e5

8 files changed

Lines changed: 181 additions & 4 deletions

File tree

‎Documentation/block/queue-sysfs.txt‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,5 +169,12 @@ This is the number of bytes the device can write in a single write-same
169169
command. A value of '0' means write-same is not supported by this
170170
device.
171171

172+
wb_lat_usec (RW)
173+
----------------
174+
If the device is registered for writeback throttling, then this file shows
175+
the target minimum read latency. If this latency is exceeded in a given
176+
window of time (see wb_window_usec), then the writeback throttling will start
177+
scaling back writes.
178+
172179

173180
Jens Axboe <jens.axboe@oracle.com>, February 2009

‎block/Kconfig‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,32 @@ config BLK_CMDLINE_PARSER
121121

122122
See Documentation/block/cmdline-partition.txt for more information.
123123

124+
config BLK_WBT
125+
bool "Enable support for block device writeback throttling"
126+
default n
127+
---help---
128+
Enabling this option enables the block layer to throttle buffered
129+
background writeback from the VM, making it more smooth and having
130+
less impact on foreground operations. The throttling is done
131+
dynamically on an algorithm loosely based on CoDel, factoring in
132+
the realtime performance of the disk.
133+
134+
config BLK_WBT_SQ
135+
bool "Single queue writeback throttling"
136+
default n
137+
depends on BLK_WBT
138+
---help---
139+
Enable writeback throttling by default on legacy single queue devices
140+
141+
config BLK_WBT_MQ
142+
bool "Multiqueue writeback throttling"
143+
default y
144+
depends on BLK_WBT
145+
---help---
146+
Enable writeback throttling by default on multiqueue devices.
147+
Multiqueue currently doesn't have support for IO scheduling,
148+
enabling this option is recommended.
149+
124150
menu "Partition Types"
125151

126152
source "block/partitions/Kconfig"

‎block/blk-core.c‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
#include "blk.h"
4141
#include "blk-mq.h"
42+
#include "blk-wbt.h"
4243

4344
EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_remap);
4445
EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_remap);
@@ -882,6 +883,7 @@ blk_init_allocated_queue(struct request_queue *q, request_fn_proc *rfn,
882883

883884
fail:
884885
blk_free_flush_queue(q->fq);
886+
wbt_exit(q);
885887
return NULL;
886888
}
887889
EXPORT_SYMBOL(blk_init_allocated_queue);
@@ -1344,6 +1346,7 @@ void blk_requeue_request(struct request_queue *q, struct request *rq)
13441346
blk_delete_timer(rq);
13451347
blk_clear_rq_complete(rq);
13461348
trace_block_rq_requeue(q, rq);
1349+
wbt_requeue(q->rq_wb, &rq->issue_stat);
13471350

13481351
if (rq->rq_flags & RQF_QUEUED)
13491352
blk_queue_end_tag(q, rq);
@@ -1436,6 +1439,8 @@ void __blk_put_request(struct request_queue *q, struct request *req)
14361439
/* this is a bio leak */
14371440
WARN_ON(req->bio != NULL);
14381441

1442+
wbt_done(q->rq_wb, &req->issue_stat);
1443+
14391444
/*
14401445
* Request may not have originated from ll_rw_blk. if not,
14411446
* it didn't come out of our reserved rq pools
@@ -1663,6 +1668,7 @@ static blk_qc_t blk_queue_bio(struct request_queue *q, struct bio *bio)
16631668
int el_ret, where = ELEVATOR_INSERT_SORT;
16641669
struct request *req;
16651670
unsigned int request_count = 0;
1671+
unsigned int wb_acct;
16661672

16671673
/*
16681674
* low level driver can indicate that it wants pages above a
@@ -1715,17 +1721,22 @@ static blk_qc_t blk_queue_bio(struct request_queue *q, struct bio *bio)
17151721
}
17161722

17171723
get_rq:
1724+
wb_acct = wbt_wait(q->rq_wb, bio, q->queue_lock);
1725+
17181726
/*
17191727
* Grab a free request. This is might sleep but can not fail.
17201728
* Returns with the queue unlocked.
17211729
*/
17221730
req = get_request(q, bio->bi_opf, bio, GFP_NOIO);
17231731
if (IS_ERR(req)) {
1732+
__wbt_done(q->rq_wb, wb_acct);
17241733
bio->bi_error = PTR_ERR(req);
17251734
bio_endio(bio);
17261735
goto out_unlock;
17271736
}
17281737

1738+
wbt_track(&req->issue_stat, wb_acct);
1739+
17291740
/*
17301741
* After dropping the lock and possibly sleeping here, our request
17311742
* may now be mergeable after it had proven unmergeable (above).
@@ -2467,6 +2478,7 @@ void blk_start_request(struct request *req)
24672478
if (test_bit(QUEUE_FLAG_STATS, &req->q->queue_flags)) {
24682479
blk_stat_set_issue_time(&req->issue_stat);
24692480
req->rq_flags |= RQF_STATS;
2481+
wbt_issue(req->q->rq_wb, &req->issue_stat);
24702482
}
24712483

24722484
/*
@@ -2708,9 +2720,10 @@ void blk_finish_request(struct request *req, int error)
27082720

27092721
blk_account_io_done(req);
27102722

2711-
if (req->end_io)
2723+
if (req->end_io) {
2724+
wbt_done(req->q->rq_wb, &req->issue_stat);
27122725
req->end_io(req, error);
2713-
else {
2726+
} else {
27142727
if (blk_bidi_rq(req))
27152728
__blk_put_request(req->next_rq->q, req->next_rq);
27162729

‎block/blk-mq.c‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "blk-mq.h"
3232
#include "blk-mq-tag.h"
3333
#include "blk-stat.h"
34+
#include "blk-wbt.h"
3435

3536
static DEFINE_MUTEX(all_q_mutex);
3637
static LIST_HEAD(all_q_list);
@@ -326,6 +327,8 @@ static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
326327

327328
if (rq->rq_flags & RQF_MQ_INFLIGHT)
328329
atomic_dec(&hctx->nr_active);
330+
331+
wbt_done(q->rq_wb, &rq->issue_stat);
329332
rq->rq_flags = 0;
330333

331334
clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
@@ -354,6 +357,7 @@ inline void __blk_mq_end_request(struct request *rq, int error)
354357
blk_account_io_done(rq);
355358

356359
if (rq->end_io) {
360+
wbt_done(rq->q->rq_wb, &rq->issue_stat);
357361
rq->end_io(rq, error);
358362
} else {
359363
if (unlikely(blk_bidi_rq(rq)))
@@ -471,6 +475,7 @@ void blk_mq_start_request(struct request *rq)
471475
if (test_bit(QUEUE_FLAG_STATS, &q->queue_flags)) {
472476
blk_stat_set_issue_time(&rq->issue_stat);
473477
rq->rq_flags |= RQF_STATS;
478+
wbt_issue(q->rq_wb, &rq->issue_stat);
474479
}
475480

476481
blk_add_timer(rq);
@@ -508,6 +513,7 @@ static void __blk_mq_requeue_request(struct request *rq)
508513
struct request_queue *q = rq->q;
509514

510515
trace_block_rq_requeue(q, rq);
516+
wbt_requeue(q->rq_wb, &rq->issue_stat);
511517

512518
if (test_and_clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) {
513519
if (q->dma_drain_size && blk_rq_bytes(rq))
@@ -1339,6 +1345,7 @@ static blk_qc_t blk_mq_make_request(struct request_queue *q, struct bio *bio)
13391345
struct blk_plug *plug;
13401346
struct request *same_queue_rq = NULL;
13411347
blk_qc_t cookie;
1348+
unsigned int wb_acct;
13421349

13431350
blk_queue_bounce(q, &bio);
13441351

@@ -1353,9 +1360,15 @@ static blk_qc_t blk_mq_make_request(struct request_queue *q, struct bio *bio)
13531360
blk_attempt_plug_merge(q, bio, &request_count, &same_queue_rq))
13541361
return BLK_QC_T_NONE;
13551362

1363+
wb_acct = wbt_wait(q->rq_wb, bio, NULL);
1364+
13561365
rq = blk_mq_map_request(q, bio, &data);
1357-
if (unlikely(!rq))
1366+
if (unlikely(!rq)) {
1367+
__wbt_done(q->rq_wb, wb_acct);
13581368
return BLK_QC_T_NONE;
1369+
}
1370+
1371+
wbt_track(&rq->issue_stat, wb_acct);
13591372

13601373
cookie = blk_tag_to_qc_t(rq->tag, data.hctx->queue_num);
13611374

@@ -1439,6 +1452,7 @@ static blk_qc_t blk_sq_make_request(struct request_queue *q, struct bio *bio)
14391452
struct blk_mq_alloc_data data;
14401453
struct request *rq;
14411454
blk_qc_t cookie;
1455+
unsigned int wb_acct;
14421456

14431457
blk_queue_bounce(q, &bio);
14441458

@@ -1455,9 +1469,15 @@ static blk_qc_t blk_sq_make_request(struct request_queue *q, struct bio *bio)
14551469
} else
14561470
request_count = blk_plug_queued_count(q);
14571471

1472+
wb_acct = wbt_wait(q->rq_wb, bio, NULL);
1473+
14581474
rq = blk_mq_map_request(q, bio, &data);
1459-
if (unlikely(!rq))
1475+
if (unlikely(!rq)) {
1476+
__wbt_done(q->rq_wb, wb_acct);
14601477
return BLK_QC_T_NONE;
1478+
}
1479+
1480+
wbt_track(&rq->issue_stat, wb_acct);
14611481

14621482
cookie = blk_tag_to_qc_t(rq->tag, data.hctx->queue_num);
14631483

@@ -2139,6 +2159,8 @@ void blk_mq_free_queue(struct request_queue *q)
21392159
list_del_init(&q->all_q_node);
21402160
mutex_unlock(&all_q_mutex);
21412161

2162+
wbt_exit(q);
2163+
21422164
blk_mq_del_queue_tag_set(q);
21432165

21442166
blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);

‎block/blk-settings.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <linux/gfp.h>
1414

1515
#include "blk.h"
16+
#include "blk-wbt.h"
1617

1718
unsigned long blk_max_low_pfn;
1819
EXPORT_SYMBOL(blk_max_low_pfn);
@@ -845,6 +846,7 @@ EXPORT_SYMBOL_GPL(blk_queue_flush_queueable);
845846
void blk_set_queue_depth(struct request_queue *q, unsigned int depth)
846847
{
847848
q->queue_depth = depth;
849+
wbt_set_queue_depth(q->rq_wb, depth);
848850
}
849851
EXPORT_SYMBOL(blk_set_queue_depth);
850852

@@ -868,6 +870,8 @@ void blk_queue_write_cache(struct request_queue *q, bool wc, bool fua)
868870
else
869871
queue_flag_clear(QUEUE_FLAG_FUA, q);
870872
spin_unlock_irq(q->queue_lock);
873+
874+
wbt_set_write_cache(q->rq_wb, test_bit(QUEUE_FLAG_WC, &q->queue_flags));
871875
}
872876
EXPORT_SYMBOL_GPL(blk_queue_write_cache);
873877

‎block/blk-sysfs.c‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "blk.h"
1515
#include "blk-mq.h"
16+
#include "blk-wbt.h"
1617

1718
struct queue_sysfs_entry {
1819
struct attribute attr;
@@ -41,6 +42,19 @@ queue_var_store(unsigned long *var, const char *page, size_t count)
4142
return count;
4243
}
4344

45+
static ssize_t queue_var_store64(u64 *var, const char *page)
46+
{
47+
int err;
48+
u64 v;
49+
50+
err = kstrtou64(page, 10, &v);
51+
if (err < 0)
52+
return err;
53+
54+
*var = v;
55+
return 0;
56+
}
57+
4458
static ssize_t queue_requests_show(struct request_queue *q, char *page)
4559
{
4660
return queue_var_show(q->nr_requests, (page));
@@ -364,6 +378,32 @@ static ssize_t queue_poll_store(struct request_queue *q, const char *page,
364378
return ret;
365379
}
366380

381+
static ssize_t queue_wb_lat_show(struct request_queue *q, char *page)
382+
{
383+
if (!q->rq_wb)
384+
return -EINVAL;
385+
386+
return sprintf(page, "%llu\n", div_u64(q->rq_wb->min_lat_nsec, 1000));
387+
}
388+
389+
static ssize_t queue_wb_lat_store(struct request_queue *q, const char *page,
390+
size_t count)
391+
{
392+
ssize_t ret;
393+
u64 val;
394+
395+
if (!q->rq_wb)
396+
return -EINVAL;
397+
398+
ret = queue_var_store64(&val, page);
399+
if (ret < 0)
400+
return ret;
401+
402+
q->rq_wb->min_lat_nsec = val * 1000ULL;
403+
wbt_update_limits(q->rq_wb);
404+
return count;
405+
}
406+
367407
static ssize_t queue_wc_show(struct request_queue *q, char *page)
368408
{
369409
if (test_bit(QUEUE_FLAG_WC, &q->queue_flags))
@@ -578,6 +618,12 @@ static struct queue_sysfs_entry queue_stats_entry = {
578618
.show = queue_stats_show,
579619
};
580620

621+
static struct queue_sysfs_entry queue_wb_lat_entry = {
622+
.attr = {.name = "wbt_lat_usec", .mode = S_IRUGO | S_IWUSR },
623+
.show = queue_wb_lat_show,
624+
.store = queue_wb_lat_store,
625+
};
626+
581627
static struct attribute *default_attrs[] = {
582628
&queue_requests_entry.attr,
583629
&queue_ra_entry.attr,
@@ -608,6 +654,7 @@ static struct attribute *default_attrs[] = {
608654
&queue_wc_entry.attr,
609655
&queue_dax_entry.attr,
610656
&queue_stats_entry.attr,
657+
&queue_wb_lat_entry.attr,
611658
NULL,
612659
};
613660

@@ -682,6 +729,7 @@ static void blk_release_queue(struct kobject *kobj)
682729
struct request_queue *q =
683730
container_of(kobj, struct request_queue, kobj);
684731

732+
wbt_exit(q);
685733
bdi_exit(&q->backing_dev_info);
686734
blkcg_exit_queue(q);
687735

@@ -722,6 +770,44 @@ struct kobj_type blk_queue_ktype = {
722770
.release = blk_release_queue,
723771
};
724772

773+
static void blk_wb_stat_get(void *data, struct blk_rq_stat *stat)
774+
{
775+
blk_queue_stat_get(data, stat);
776+
}
777+
778+
static void blk_wb_stat_clear(void *data)
779+
{
780+
blk_stat_clear(data);
781+
}
782+
783+
static bool blk_wb_stat_is_current(struct blk_rq_stat *stat)
784+
{
785+
return blk_stat_is_current(stat);
786+
}
787+
788+
static struct wb_stat_ops wb_stat_ops = {
789+
.get = blk_wb_stat_get,
790+
.is_current = blk_wb_stat_is_current,
791+
.clear = blk_wb_stat_clear,
792+
};
793+
794+
static void blk_wb_init(struct request_queue *q)
795+
{
796+
#ifndef CONFIG_BLK_WBT_MQ
797+
if (q->mq_ops)
798+
return;
799+
#endif
800+
#ifndef CONFIG_BLK_WBT_SQ
801+
if (q->request_fn)
802+
return;
803+
#endif
804+
805+
/*
806+
* If this fails, we don't get throttling
807+
*/
808+
wbt_init(q, &wb_stat_ops);
809+
}
810+
725811
int blk_register_queue(struct gendisk *disk)
726812
{
727813
int ret;
@@ -761,6 +847,8 @@ int blk_register_queue(struct gendisk *disk)
761847
if (q->mq_ops)
762848
blk_mq_register_dev(dev, q);
763849

850+
blk_wb_init(q);
851+
764852
if (!q->request_fn)
765853
return 0;
766854

0 commit comments

Comments
 (0)