Skip to content

Commit eb608e3

Browse files
jankaraFengguang Wu
authored andcommitted
block: Convert BDI proportion calculations to flexible proportions
Convert calculations of proportion of writeback each bdi does to new flexible proportion code. That allows us to use aging period of fixed wallclock time which gives better proportion estimates given the hugely varying throughput of different devices. Acked-by: Peter Zijlstra <a.p.zijlstra@chello.nl> Signed-off-by: Jan Kara <jack@suse.cz> Signed-off-by: Fengguang Wu <fengguang.wu@intel.com>
1 parent e78d483 commit eb608e3

3 files changed

Lines changed: 69 additions & 44 deletions

File tree

‎include/linux/backing-dev.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
#include <linux/percpu_counter.h>
1212
#include <linux/log2.h>
13-
#include <linux/proportions.h>
13+
#include <linux/flex_proportions.h>
1414
#include <linux/kernel.h>
1515
#include <linux/fs.h>
1616
#include <linux/sched.h>
@@ -89,7 +89,7 @@ struct backing_dev_info {
8989
unsigned long dirty_ratelimit;
9090
unsigned long balanced_dirty_ratelimit;
9191

92-
struct prop_local_percpu completions;
92+
struct fprop_local_percpu completions;
9393
int dirty_exceeded;
9494

9595
unsigned int min_ratio;

‎mm/backing-dev.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ int bdi_init(struct backing_dev_info *bdi)
677677

678678
bdi->min_ratio = 0;
679679
bdi->max_ratio = 100;
680-
bdi->max_prop_frac = PROP_FRAC_BASE;
680+
bdi->max_prop_frac = FPROP_FRAC_BASE;
681681
spin_lock_init(&bdi->wb_lock);
682682
INIT_LIST_HEAD(&bdi->bdi_list);
683683
INIT_LIST_HEAD(&bdi->work_list);
@@ -700,7 +700,7 @@ int bdi_init(struct backing_dev_info *bdi)
700700
bdi->write_bandwidth = INIT_BW;
701701
bdi->avg_write_bandwidth = INIT_BW;
702702

703-
err = prop_local_init_percpu(&bdi->completions);
703+
err = fprop_local_init_percpu(&bdi->completions);
704704

705705
if (err) {
706706
err:
@@ -744,7 +744,7 @@ void bdi_destroy(struct backing_dev_info *bdi)
744744
for (i = 0; i < NR_BDI_STAT_ITEMS; i++)
745745
percpu_counter_destroy(&bdi->bdi_stat[i]);
746746

747-
prop_local_destroy_percpu(&bdi->completions);
747+
fprop_local_destroy_percpu(&bdi->completions);
748748
}
749749
EXPORT_SYMBOL(bdi_destroy);
750750

‎mm/page-writeback.c‎

Lines changed: 64 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <linux/syscalls.h>
3535
#include <linux/buffer_head.h> /* __set_page_dirty_buffers */
3636
#include <linux/pagevec.h>
37+
#include <linux/timer.h>
3738
#include <trace/events/writeback.h>
3839

3940
/*
@@ -135,7 +136,20 @@ unsigned long global_dirty_limit;
135136
* measured in page writeback completions.
136137
*
137138
*/
138-
static struct prop_descriptor vm_completions;
139+
static struct fprop_global writeout_completions;
140+
141+
static void writeout_period(unsigned long t);
142+
/* Timer for aging of writeout_completions */
143+
static struct timer_list writeout_period_timer =
144+
TIMER_DEFERRED_INITIALIZER(writeout_period, 0, 0);
145+
static unsigned long writeout_period_time = 0;
146+
147+
/*
148+
* Length of period for aging writeout fractions of bdis. This is an
149+
* arbitrarily chosen number. The longer the period, the slower fractions will
150+
* reflect changes in current writeout rate.
151+
*/
152+
#define VM_COMPLETIONS_PERIOD_LEN (3*HZ)
139153

140154
/*
141155
* Work out the current dirty-memory clamping and background writeout
@@ -322,34 +336,6 @@ bool zone_dirty_ok(struct zone *zone)
322336
zone_page_state(zone, NR_WRITEBACK) <= limit;
323337
}
324338

325-
/*
326-
* couple the period to the dirty_ratio:
327-
*
328-
* period/2 ~ roundup_pow_of_two(dirty limit)
329-
*/
330-
static int calc_period_shift(void)
331-
{
332-
unsigned long dirty_total;
333-
334-
if (vm_dirty_bytes)
335-
dirty_total = vm_dirty_bytes / PAGE_SIZE;
336-
else
337-
dirty_total = (vm_dirty_ratio * global_dirtyable_memory()) /
338-
100;
339-
return 2 + ilog2(dirty_total - 1);
340-
}
341-
342-
/*
343-
* update the period when the dirty threshold changes.
344-
*/
345-
static void update_completion_period(void)
346-
{
347-
int shift = calc_period_shift();
348-
prop_change_shift(&vm_completions, shift);
349-
350-
writeback_set_ratelimit();
351-
}
352-
353339
int dirty_background_ratio_handler(struct ctl_table *table, int write,
354340
void __user *buffer, size_t *lenp,
355341
loff_t *ppos)
@@ -383,7 +369,7 @@ int dirty_ratio_handler(struct ctl_table *table, int write,
383369

384370
ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
385371
if (ret == 0 && write && vm_dirty_ratio != old_ratio) {
386-
update_completion_period();
372+
writeback_set_ratelimit();
387373
vm_dirty_bytes = 0;
388374
}
389375
return ret;
@@ -398,21 +384,41 @@ int dirty_bytes_handler(struct ctl_table *table, int write,
398384

399385
ret = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
400386
if (ret == 0 && write && vm_dirty_bytes != old_bytes) {
401-
update_completion_period();
387+
writeback_set_ratelimit();
402388
vm_dirty_ratio = 0;
403389
}
404390
return ret;
405391
}
406392

393+
static unsigned long wp_next_time(unsigned long cur_time)
394+
{
395+
cur_time += VM_COMPLETIONS_PERIOD_LEN;
396+
/* 0 has a special meaning... */
397+
if (!cur_time)
398+
return 1;
399+
return cur_time;
400+
}
401+
407402
/*
408403
* Increment the BDI's writeout completion count and the global writeout
409404
* completion count. Called from test_clear_page_writeback().
410405
*/
411406
static inline void __bdi_writeout_inc(struct backing_dev_info *bdi)
412407
{
413408
__inc_bdi_stat(bdi, BDI_WRITTEN);
414-
__prop_inc_percpu_max(&vm_completions, &bdi->completions,
415-
bdi->max_prop_frac);
409+
__fprop_inc_percpu_max(&writeout_completions, &bdi->completions,
410+
bdi->max_prop_frac);
411+
/* First event after period switching was turned off? */
412+
if (!unlikely(writeout_period_time)) {
413+
/*
414+
* We can race with other __bdi_writeout_inc calls here but
415+
* it does not cause any harm since the resulting time when
416+
* timer will fire and what is in writeout_period_time will be
417+
* roughly the same.
418+
*/
419+
writeout_period_time = wp_next_time(jiffies);
420+
mod_timer(&writeout_period_timer, writeout_period_time);
421+
}
416422
}
417423

418424
void bdi_writeout_inc(struct backing_dev_info *bdi)
@@ -431,10 +437,32 @@ EXPORT_SYMBOL_GPL(bdi_writeout_inc);
431437
static void bdi_writeout_fraction(struct backing_dev_info *bdi,
432438
long *numerator, long *denominator)
433439
{
434-
prop_fraction_percpu(&vm_completions, &bdi->completions,
440+
fprop_fraction_percpu(&writeout_completions, &bdi->completions,
435441
numerator, denominator);
436442
}
437443

444+
/*
445+
* On idle system, we can be called long after we scheduled because we use
446+
* deferred timers so count with missed periods.
447+
*/
448+
static void writeout_period(unsigned long t)
449+
{
450+
int miss_periods = (jiffies - writeout_period_time) /
451+
VM_COMPLETIONS_PERIOD_LEN;
452+
453+
if (fprop_new_period(&writeout_completions, miss_periods + 1)) {
454+
writeout_period_time = wp_next_time(writeout_period_time +
455+
miss_periods * VM_COMPLETIONS_PERIOD_LEN);
456+
mod_timer(&writeout_period_timer, writeout_period_time);
457+
} else {
458+
/*
459+
* Aging has zeroed all fractions. Stop wasting CPU on period
460+
* updates.
461+
*/
462+
writeout_period_time = 0;
463+
}
464+
}
465+
438466
/*
439467
* bdi_min_ratio keeps the sum of the minimum dirty shares of all
440468
* registered backing devices, which, for obvious reasons, can not
@@ -475,7 +503,7 @@ int bdi_set_max_ratio(struct backing_dev_info *bdi, unsigned max_ratio)
475503
ret = -EINVAL;
476504
} else {
477505
bdi->max_ratio = max_ratio;
478-
bdi->max_prop_frac = (PROP_FRAC_BASE * max_ratio) / 100;
506+
bdi->max_prop_frac = (FPROP_FRAC_BASE * max_ratio) / 100;
479507
}
480508
spin_unlock_bh(&bdi_lock);
481509

@@ -1606,13 +1634,10 @@ static struct notifier_block __cpuinitdata ratelimit_nb = {
16061634
*/
16071635
void __init page_writeback_init(void)
16081636
{
1609-
int shift;
1610-
16111637
writeback_set_ratelimit();
16121638
register_cpu_notifier(&ratelimit_nb);
16131639

1614-
shift = calc_period_shift();
1615-
prop_descriptor_init(&vm_completions, shift);
1640+
fprop_global_init(&writeout_completions);
16161641
}
16171642

16181643
/**

0 commit comments

Comments
 (0)