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-
353339int 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 */
411406static 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
418424void bdi_writeout_inc (struct backing_dev_info * bdi )
@@ -431,10 +437,32 @@ EXPORT_SYMBOL_GPL(bdi_writeout_inc);
431437static 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 */
16071635void __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