aboutsummaryrefslogtreecommitdiffstats
path: root/mm/page-writeback.c
diff options
context:
space:
mode:
Diffstat (limited to 'mm/page-writeback.c')
-rw-r--r--mm/page-writeback.c731
1 files changed, 562 insertions, 169 deletions
diff --git a/mm/page-writeback.c b/mm/page-writeback.c
index 0e309cd1b5b9..71252486bc6f 100644
--- a/mm/page-writeback.c
+++ b/mm/page-writeback.c
@@ -12,7 +12,7 @@
12 */ 12 */
13 13
14#include <linux/kernel.h> 14#include <linux/kernel.h>
15#include <linux/module.h> 15#include <linux/export.h>
16#include <linux/spinlock.h> 16#include <linux/spinlock.h>
17#include <linux/fs.h> 17#include <linux/fs.h>
18#include <linux/mm.h> 18#include <linux/mm.h>
@@ -46,26 +46,14 @@
46 */ 46 */
47#define BANDWIDTH_INTERVAL max(HZ/5, 1) 47#define BANDWIDTH_INTERVAL max(HZ/5, 1)
48 48
49#define RATELIMIT_CALC_SHIFT 10
50
49/* 51/*
50 * After a CPU has dirtied this many pages, balance_dirty_pages_ratelimited 52 * After a CPU has dirtied this many pages, balance_dirty_pages_ratelimited
51 * will look to see if it needs to force writeback or throttling. 53 * will look to see if it needs to force writeback or throttling.
52 */ 54 */
53static long ratelimit_pages = 32; 55static long ratelimit_pages = 32;
54 56
55/*
56 * When balance_dirty_pages decides that the caller needs to perform some
57 * non-background writeback, this is how many pages it will attempt to write.
58 * It should be somewhat larger than dirtied pages to ensure that reasonably
59 * large amounts of I/O are submitted.
60 */
61static inline long sync_writeback_pages(unsigned long dirtied)
62{
63 if (dirtied < ratelimit_pages)
64 dirtied = ratelimit_pages;
65
66 return dirtied + dirtied / 2;
67}
68
69/* The following parameters are exported via /proc/sys/vm */ 57/* The following parameters are exported via /proc/sys/vm */
70 58
71/* 59/*
@@ -140,7 +128,6 @@ unsigned long global_dirty_limit;
140 * 128 *
141 */ 129 */
142static struct prop_descriptor vm_completions; 130static struct prop_descriptor vm_completions;
143static struct prop_descriptor vm_dirties;
144 131
145/* 132/*
146 * couple the period to the dirty_ratio: 133 * couple the period to the dirty_ratio:
@@ -166,7 +153,8 @@ static void update_completion_period(void)
166{ 153{
167 int shift = calc_period_shift(); 154 int shift = calc_period_shift();
168 prop_change_shift(&vm_completions, shift); 155 prop_change_shift(&vm_completions, shift);
169 prop_change_shift(&vm_dirties, shift); 156
157 writeback_set_ratelimit();
170} 158}
171 159
172int dirty_background_ratio_handler(struct ctl_table *table, int write, 160int dirty_background_ratio_handler(struct ctl_table *table, int write,
@@ -245,11 +233,6 @@ void bdi_writeout_inc(struct backing_dev_info *bdi)
245} 233}
246EXPORT_SYMBOL_GPL(bdi_writeout_inc); 234EXPORT_SYMBOL_GPL(bdi_writeout_inc);
247 235
248void task_dirty_inc(struct task_struct *tsk)
249{
250 prop_inc_single(&vm_dirties, &tsk->dirties);
251}
252
253/* 236/*
254 * Obtain an accurate fraction of the BDI's portion. 237 * Obtain an accurate fraction of the BDI's portion.
255 */ 238 */
@@ -260,52 +243,10 @@ static void bdi_writeout_fraction(struct backing_dev_info *bdi,
260 numerator, denominator); 243 numerator, denominator);
261} 244}
262 245
263static inline void task_dirties_fraction(struct task_struct *tsk,
264 long *numerator, long *denominator)
265{
266 prop_fraction_single(&vm_dirties, &tsk->dirties,
267 numerator, denominator);
268}
269
270/* 246/*
271 * task_dirty_limit - scale down dirty throttling threshold for one task 247 * bdi_min_ratio keeps the sum of the minimum dirty shares of all
272 * 248 * registered backing devices, which, for obvious reasons, can not
273 * task specific dirty limit: 249 * exceed 100%.
274 *
275 * dirty -= (dirty/8) * p_{t}
276 *
277 * To protect light/slow dirtying tasks from heavier/fast ones, we start
278 * throttling individual tasks before reaching the bdi dirty limit.
279 * Relatively low thresholds will be allocated to heavy dirtiers. So when
280 * dirty pages grow large, heavy dirtiers will be throttled first, which will
281 * effectively curb the growth of dirty pages. Light dirtiers with high enough
282 * dirty threshold may never get throttled.
283 */
284#define TASK_LIMIT_FRACTION 8
285static unsigned long task_dirty_limit(struct task_struct *tsk,
286 unsigned long bdi_dirty)
287{
288 long numerator, denominator;
289 unsigned long dirty = bdi_dirty;
290 u64 inv = dirty / TASK_LIMIT_FRACTION;
291
292 task_dirties_fraction(tsk, &numerator, &denominator);
293 inv *= numerator;
294 do_div(inv, denominator);
295
296 dirty -= inv;
297
298 return max(dirty, bdi_dirty/2);
299}
300
301/* Minimum limit for any task */
302static unsigned long task_min_dirty_limit(unsigned long bdi_dirty)
303{
304 return bdi_dirty - bdi_dirty / TASK_LIMIT_FRACTION;
305}
306
307/*
308 *
309 */ 250 */
310static unsigned int bdi_min_ratio; 251static unsigned int bdi_min_ratio;
311 252
@@ -411,6 +352,12 @@ unsigned long determine_dirtyable_memory(void)
411 return x + 1; /* Ensure that we never return 0 */ 352 return x + 1; /* Ensure that we never return 0 */
412} 353}
413 354
355static unsigned long dirty_freerun_ceiling(unsigned long thresh,
356 unsigned long bg_thresh)
357{
358 return (thresh + bg_thresh) / 2;
359}
360
414static unsigned long hard_dirty_limit(unsigned long thresh) 361static unsigned long hard_dirty_limit(unsigned long thresh)
415{ 362{
416 return max(thresh, global_dirty_limit); 363 return max(thresh, global_dirty_limit);
@@ -495,6 +442,198 @@ unsigned long bdi_dirty_limit(struct backing_dev_info *bdi, unsigned long dirty)
495 return bdi_dirty; 442 return bdi_dirty;
496} 443}
497 444
445/*
446 * Dirty position control.
447 *
448 * (o) global/bdi setpoints
449 *
450 * We want the dirty pages be balanced around the global/bdi setpoints.
451 * When the number of dirty pages is higher/lower than the setpoint, the
452 * dirty position control ratio (and hence task dirty ratelimit) will be
453 * decreased/increased to bring the dirty pages back to the setpoint.
454 *
455 * pos_ratio = 1 << RATELIMIT_CALC_SHIFT
456 *
457 * if (dirty < setpoint) scale up pos_ratio
458 * if (dirty > setpoint) scale down pos_ratio
459 *
460 * if (bdi_dirty < bdi_setpoint) scale up pos_ratio
461 * if (bdi_dirty > bdi_setpoint) scale down pos_ratio
462 *
463 * task_ratelimit = dirty_ratelimit * pos_ratio >> RATELIMIT_CALC_SHIFT
464 *
465 * (o) global control line
466 *
467 * ^ pos_ratio
468 * |
469 * | |<===== global dirty control scope ======>|
470 * 2.0 .............*
471 * | .*
472 * | . *
473 * | . *
474 * | . *
475 * | . *
476 * | . *
477 * 1.0 ................................*
478 * | . . *
479 * | . . *
480 * | . . *
481 * | . . *
482 * | . . *
483 * 0 +------------.------------------.----------------------*------------->
484 * freerun^ setpoint^ limit^ dirty pages
485 *
486 * (o) bdi control line
487 *
488 * ^ pos_ratio
489 * |
490 * | *
491 * | *
492 * | *
493 * | *
494 * | * |<=========== span ============>|
495 * 1.0 .......................*
496 * | . *
497 * | . *
498 * | . *
499 * | . *
500 * | . *
501 * | . *
502 * | . *
503 * | . *
504 * | . *
505 * | . *
506 * | . *
507 * 1/4 ...............................................* * * * * * * * * * * *
508 * | . .
509 * | . .
510 * | . .
511 * 0 +----------------------.-------------------------------.------------->
512 * bdi_setpoint^ x_intercept^
513 *
514 * The bdi control line won't drop below pos_ratio=1/4, so that bdi_dirty can
515 * be smoothly throttled down to normal if it starts high in situations like
516 * - start writing to a slow SD card and a fast disk at the same time. The SD
517 * card's bdi_dirty may rush to many times higher than bdi_setpoint.
518 * - the bdi dirty thresh drops quickly due to change of JBOD workload
519 */
520static unsigned long bdi_position_ratio(struct backing_dev_info *bdi,
521 unsigned long thresh,
522 unsigned long bg_thresh,
523 unsigned long dirty,
524 unsigned long bdi_thresh,
525 unsigned long bdi_dirty)
526{
527 unsigned long write_bw = bdi->avg_write_bandwidth;
528 unsigned long freerun = dirty_freerun_ceiling(thresh, bg_thresh);
529 unsigned long limit = hard_dirty_limit(thresh);
530 unsigned long x_intercept;
531 unsigned long setpoint; /* dirty pages' target balance point */
532 unsigned long bdi_setpoint;
533 unsigned long span;
534 long long pos_ratio; /* for scaling up/down the rate limit */
535 long x;
536
537 if (unlikely(dirty >= limit))
538 return 0;
539
540 /*
541 * global setpoint
542 *
543 * setpoint - dirty 3
544 * f(dirty) := 1.0 + (----------------)
545 * limit - setpoint
546 *
547 * it's a 3rd order polynomial that subjects to
548 *
549 * (1) f(freerun) = 2.0 => rampup dirty_ratelimit reasonably fast
550 * (2) f(setpoint) = 1.0 => the balance point
551 * (3) f(limit) = 0 => the hard limit
552 * (4) df/dx <= 0 => negative feedback control
553 * (5) the closer to setpoint, the smaller |df/dx| (and the reverse)
554 * => fast response on large errors; small oscillation near setpoint
555 */
556 setpoint = (freerun + limit) / 2;
557 x = div_s64((setpoint - dirty) << RATELIMIT_CALC_SHIFT,
558 limit - setpoint + 1);
559 pos_ratio = x;
560 pos_ratio = pos_ratio * x >> RATELIMIT_CALC_SHIFT;
561 pos_ratio = pos_ratio * x >> RATELIMIT_CALC_SHIFT;
562 pos_ratio += 1 << RATELIMIT_CALC_SHIFT;
563
564 /*
565 * We have computed basic pos_ratio above based on global situation. If
566 * the bdi is over/under its share of dirty pages, we want to scale
567 * pos_ratio further down/up. That is done by the following mechanism.
568 */
569
570 /*
571 * bdi setpoint
572 *
573 * f(bdi_dirty) := 1.0 + k * (bdi_dirty - bdi_setpoint)
574 *
575 * x_intercept - bdi_dirty
576 * := --------------------------
577 * x_intercept - bdi_setpoint
578 *
579 * The main bdi control line is a linear function that subjects to
580 *
581 * (1) f(bdi_setpoint) = 1.0
582 * (2) k = - 1 / (8 * write_bw) (in single bdi case)
583 * or equally: x_intercept = bdi_setpoint + 8 * write_bw
584 *
585 * For single bdi case, the dirty pages are observed to fluctuate
586 * regularly within range
587 * [bdi_setpoint - write_bw/2, bdi_setpoint + write_bw/2]
588 * for various filesystems, where (2) can yield in a reasonable 12.5%
589 * fluctuation range for pos_ratio.
590 *
591 * For JBOD case, bdi_thresh (not bdi_dirty!) could fluctuate up to its
592 * own size, so move the slope over accordingly and choose a slope that
593 * yields 100% pos_ratio fluctuation on suddenly doubled bdi_thresh.
594 */
595 if (unlikely(bdi_thresh > thresh))
596 bdi_thresh = thresh;
597 bdi_thresh = max(bdi_thresh, (limit - dirty) / 8);
598 /*
599 * scale global setpoint to bdi's:
600 * bdi_setpoint = setpoint * bdi_thresh / thresh
601 */
602 x = div_u64((u64)bdi_thresh << 16, thresh + 1);
603 bdi_setpoint = setpoint * (u64)x >> 16;
604 /*
605 * Use span=(8*write_bw) in single bdi case as indicated by
606 * (thresh - bdi_thresh ~= 0) and transit to bdi_thresh in JBOD case.
607 *
608 * bdi_thresh thresh - bdi_thresh
609 * span = ---------- * (8 * write_bw) + ------------------- * bdi_thresh
610 * thresh thresh
611 */
612 span = (thresh - bdi_thresh + 8 * write_bw) * (u64)x >> 16;
613 x_intercept = bdi_setpoint + span;
614
615 if (bdi_dirty < x_intercept - span / 4) {
616 pos_ratio = div_u64(pos_ratio * (x_intercept - bdi_dirty),
617 x_intercept - bdi_setpoint + 1);
618 } else
619 pos_ratio /= 4;
620
621 /*
622 * bdi reserve area, safeguard against dirty pool underrun and disk idle
623 * It may push the desired control point of global dirty pages higher
624 * than setpoint.
625 */
626 x_intercept = bdi_thresh / 2;
627 if (bdi_dirty < x_intercept) {
628 if (bdi_dirty > x_intercept / 8)
629 pos_ratio = div_u64(pos_ratio * x_intercept, bdi_dirty);
630 else
631 pos_ratio *= 8;
632 }
633
634 return pos_ratio;
635}
636
498static void bdi_update_write_bandwidth(struct backing_dev_info *bdi, 637static void bdi_update_write_bandwidth(struct backing_dev_info *bdi,
499 unsigned long elapsed, 638 unsigned long elapsed,
500 unsigned long written) 639 unsigned long written)
@@ -591,8 +730,153 @@ static void global_update_bandwidth(unsigned long thresh,
591 spin_unlock(&dirty_lock); 730 spin_unlock(&dirty_lock);
592} 731}
593 732
733/*
734 * Maintain bdi->dirty_ratelimit, the base dirty throttle rate.
735 *
736 * Normal bdi tasks will be curbed at or below it in long term.
737 * Obviously it should be around (write_bw / N) when there are N dd tasks.
738 */
739static void bdi_update_dirty_ratelimit(struct backing_dev_info *bdi,
740 unsigned long thresh,
741 unsigned long bg_thresh,
742 unsigned long dirty,
743 unsigned long bdi_thresh,
744 unsigned long bdi_dirty,
745 unsigned long dirtied,
746 unsigned long elapsed)
747{
748 unsigned long freerun = dirty_freerun_ceiling(thresh, bg_thresh);
749 unsigned long limit = hard_dirty_limit(thresh);
750 unsigned long setpoint = (freerun + limit) / 2;
751 unsigned long write_bw = bdi->avg_write_bandwidth;
752 unsigned long dirty_ratelimit = bdi->dirty_ratelimit;
753 unsigned long dirty_rate;
754 unsigned long task_ratelimit;
755 unsigned long balanced_dirty_ratelimit;
756 unsigned long pos_ratio;
757 unsigned long step;
758 unsigned long x;
759
760 /*
761 * The dirty rate will match the writeout rate in long term, except
762 * when dirty pages are truncated by userspace or re-dirtied by FS.
763 */
764 dirty_rate = (dirtied - bdi->dirtied_stamp) * HZ / elapsed;
765
766 pos_ratio = bdi_position_ratio(bdi, thresh, bg_thresh, dirty,
767 bdi_thresh, bdi_dirty);
768 /*
769 * task_ratelimit reflects each dd's dirty rate for the past 200ms.
770 */
771 task_ratelimit = (u64)dirty_ratelimit *
772 pos_ratio >> RATELIMIT_CALC_SHIFT;
773 task_ratelimit++; /* it helps rampup dirty_ratelimit from tiny values */
774
775 /*
776 * A linear estimation of the "balanced" throttle rate. The theory is,
777 * if there are N dd tasks, each throttled at task_ratelimit, the bdi's
778 * dirty_rate will be measured to be (N * task_ratelimit). So the below
779 * formula will yield the balanced rate limit (write_bw / N).
780 *
781 * Note that the expanded form is not a pure rate feedback:
782 * rate_(i+1) = rate_(i) * (write_bw / dirty_rate) (1)
783 * but also takes pos_ratio into account:
784 * rate_(i+1) = rate_(i) * (write_bw / dirty_rate) * pos_ratio (2)
785 *
786 * (1) is not realistic because pos_ratio also takes part in balancing
787 * the dirty rate. Consider the state
788 * pos_ratio = 0.5 (3)
789 * rate = 2 * (write_bw / N) (4)
790 * If (1) is used, it will stuck in that state! Because each dd will
791 * be throttled at
792 * task_ratelimit = pos_ratio * rate = (write_bw / N) (5)
793 * yielding
794 * dirty_rate = N * task_ratelimit = write_bw (6)
795 * put (6) into (1) we get
796 * rate_(i+1) = rate_(i) (7)
797 *
798 * So we end up using (2) to always keep
799 * rate_(i+1) ~= (write_bw / N) (8)
800 * regardless of the value of pos_ratio. As long as (8) is satisfied,
801 * pos_ratio is able to drive itself to 1.0, which is not only where
802 * the dirty count meet the setpoint, but also where the slope of
803 * pos_ratio is most flat and hence task_ratelimit is least fluctuated.
804 */
805 balanced_dirty_ratelimit = div_u64((u64)task_ratelimit * write_bw,
806 dirty_rate | 1);
807
808 /*
809 * We could safely do this and return immediately:
810 *
811 * bdi->dirty_ratelimit = balanced_dirty_ratelimit;
812 *
813 * However to get a more stable dirty_ratelimit, the below elaborated
814 * code makes use of task_ratelimit to filter out sigular points and
815 * limit the step size.
816 *
817 * The below code essentially only uses the relative value of
818 *
819 * task_ratelimit - dirty_ratelimit
820 * = (pos_ratio - 1) * dirty_ratelimit
821 *
822 * which reflects the direction and size of dirty position error.
823 */
824
825 /*
826 * dirty_ratelimit will follow balanced_dirty_ratelimit iff
827 * task_ratelimit is on the same side of dirty_ratelimit, too.
828 * For example, when
829 * - dirty_ratelimit > balanced_dirty_ratelimit
830 * - dirty_ratelimit > task_ratelimit (dirty pages are above setpoint)
831 * lowering dirty_ratelimit will help meet both the position and rate
832 * control targets. Otherwise, don't update dirty_ratelimit if it will
833 * only help meet the rate target. After all, what the users ultimately
834 * feel and care are stable dirty rate and small position error.
835 *
836 * |task_ratelimit - dirty_ratelimit| is used to limit the step size
837 * and filter out the sigular points of balanced_dirty_ratelimit. Which
838 * keeps jumping around randomly and can even leap far away at times
839 * due to the small 200ms estimation period of dirty_rate (we want to
840 * keep that period small to reduce time lags).
841 */
842 step = 0;
843 if (dirty < setpoint) {
844 x = min(bdi->balanced_dirty_ratelimit,
845 min(balanced_dirty_ratelimit, task_ratelimit));
846 if (dirty_ratelimit < x)
847 step = x - dirty_ratelimit;
848 } else {
849 x = max(bdi->balanced_dirty_ratelimit,
850 max(balanced_dirty_ratelimit, task_ratelimit));
851 if (dirty_ratelimit > x)
852 step = dirty_ratelimit - x;
853 }
854
855 /*
856 * Don't pursue 100% rate matching. It's impossible since the balanced
857 * rate itself is constantly fluctuating. So decrease the track speed
858 * when it gets close to the target. Helps eliminate pointless tremors.
859 */
860 step >>= dirty_ratelimit / (2 * step + 1);
861 /*
862 * Limit the tracking speed to avoid overshooting.
863 */
864 step = (step + 7) / 8;
865
866 if (dirty_ratelimit < balanced_dirty_ratelimit)
867 dirty_ratelimit += step;
868 else
869 dirty_ratelimit -= step;
870
871 bdi->dirty_ratelimit = max(dirty_ratelimit, 1UL);
872 bdi->balanced_dirty_ratelimit = balanced_dirty_ratelimit;
873
874 trace_bdi_dirty_ratelimit(bdi, dirty_rate, task_ratelimit);
875}
876
594void __bdi_update_bandwidth(struct backing_dev_info *bdi, 877void __bdi_update_bandwidth(struct backing_dev_info *bdi,
595 unsigned long thresh, 878 unsigned long thresh,
879 unsigned long bg_thresh,
596 unsigned long dirty, 880 unsigned long dirty,
597 unsigned long bdi_thresh, 881 unsigned long bdi_thresh,
598 unsigned long bdi_dirty, 882 unsigned long bdi_dirty,
@@ -600,6 +884,7 @@ void __bdi_update_bandwidth(struct backing_dev_info *bdi,
600{ 884{
601 unsigned long now = jiffies; 885 unsigned long now = jiffies;
602 unsigned long elapsed = now - bdi->bw_time_stamp; 886 unsigned long elapsed = now - bdi->bw_time_stamp;
887 unsigned long dirtied;
603 unsigned long written; 888 unsigned long written;
604 889
605 /* 890 /*
@@ -608,6 +893,7 @@ void __bdi_update_bandwidth(struct backing_dev_info *bdi,
608 if (elapsed < BANDWIDTH_INTERVAL) 893 if (elapsed < BANDWIDTH_INTERVAL)
609 return; 894 return;
610 895
896 dirtied = percpu_counter_read(&bdi->bdi_stat[BDI_DIRTIED]);
611 written = percpu_counter_read(&bdi->bdi_stat[BDI_WRITTEN]); 897 written = percpu_counter_read(&bdi->bdi_stat[BDI_WRITTEN]);
612 898
613 /* 899 /*
@@ -617,18 +903,23 @@ void __bdi_update_bandwidth(struct backing_dev_info *bdi,
617 if (elapsed > HZ && time_before(bdi->bw_time_stamp, start_time)) 903 if (elapsed > HZ && time_before(bdi->bw_time_stamp, start_time))
618 goto snapshot; 904 goto snapshot;
619 905
620 if (thresh) 906 if (thresh) {
621 global_update_bandwidth(thresh, dirty, now); 907 global_update_bandwidth(thresh, dirty, now);
622 908 bdi_update_dirty_ratelimit(bdi, thresh, bg_thresh, dirty,
909 bdi_thresh, bdi_dirty,
910 dirtied, elapsed);
911 }
623 bdi_update_write_bandwidth(bdi, elapsed, written); 912 bdi_update_write_bandwidth(bdi, elapsed, written);
624 913
625snapshot: 914snapshot:
915 bdi->dirtied_stamp = dirtied;
626 bdi->written_stamp = written; 916 bdi->written_stamp = written;
627 bdi->bw_time_stamp = now; 917 bdi->bw_time_stamp = now;
628} 918}
629 919
630static void bdi_update_bandwidth(struct backing_dev_info *bdi, 920static void bdi_update_bandwidth(struct backing_dev_info *bdi,
631 unsigned long thresh, 921 unsigned long thresh,
922 unsigned long bg_thresh,
632 unsigned long dirty, 923 unsigned long dirty,
633 unsigned long bdi_thresh, 924 unsigned long bdi_thresh,
634 unsigned long bdi_dirty, 925 unsigned long bdi_dirty,
@@ -637,37 +928,99 @@ static void bdi_update_bandwidth(struct backing_dev_info *bdi,
637 if (time_is_after_eq_jiffies(bdi->bw_time_stamp + BANDWIDTH_INTERVAL)) 928 if (time_is_after_eq_jiffies(bdi->bw_time_stamp + BANDWIDTH_INTERVAL))
638 return; 929 return;
639 spin_lock(&bdi->wb.list_lock); 930 spin_lock(&bdi->wb.list_lock);
640 __bdi_update_bandwidth(bdi, thresh, dirty, bdi_thresh, bdi_dirty, 931 __bdi_update_bandwidth(bdi, thresh, bg_thresh, dirty,
641 start_time); 932 bdi_thresh, bdi_dirty, start_time);
642 spin_unlock(&bdi->wb.list_lock); 933 spin_unlock(&bdi->wb.list_lock);
643} 934}
644 935
645/* 936/*
937 * After a task dirtied this many pages, balance_dirty_pages_ratelimited_nr()
938 * will look to see if it needs to start dirty throttling.
939 *
940 * If dirty_poll_interval is too low, big NUMA machines will call the expensive
941 * global_page_state() too often. So scale it near-sqrt to the safety margin
942 * (the number of pages we may dirty without exceeding the dirty limits).
943 */
944static unsigned long dirty_poll_interval(unsigned long dirty,
945 unsigned long thresh)
946{
947 if (thresh > dirty)
948 return 1UL << (ilog2(thresh - dirty) >> 1);
949
950 return 1;
951}
952
953static unsigned long bdi_max_pause(struct backing_dev_info *bdi,
954 unsigned long bdi_dirty)
955{
956 unsigned long bw = bdi->avg_write_bandwidth;
957 unsigned long hi = ilog2(bw);
958 unsigned long lo = ilog2(bdi->dirty_ratelimit);
959 unsigned long t;
960
961 /* target for 20ms max pause on 1-dd case */
962 t = HZ / 50;
963
964 /*
965 * Scale up pause time for concurrent dirtiers in order to reduce CPU
966 * overheads.
967 *
968 * (N * 20ms) on 2^N concurrent tasks.
969 */
970 if (hi > lo)
971 t += (hi - lo) * (20 * HZ) / 1024;
972
973 /*
974 * Limit pause time for small memory systems. If sleeping for too long
975 * time, a small pool of dirty/writeback pages may go empty and disk go
976 * idle.
977 *
978 * 8 serves as the safety ratio.
979 */
980 if (bdi_dirty)
981 t = min(t, bdi_dirty * HZ / (8 * bw + 1));
982
983 /*
984 * The pause time will be settled within range (max_pause/4, max_pause).
985 * Apply a minimal value of 4 to get a non-zero max_pause/4.
986 */
987 return clamp_val(t, 4, MAX_PAUSE);
988}
989
990/*
646 * balance_dirty_pages() must be called by processes which are generating dirty 991 * balance_dirty_pages() must be called by processes which are generating dirty
647 * data. It looks at the number of dirty pages in the machine and will force 992 * data. It looks at the number of dirty pages in the machine and will force
648 * the caller to perform writeback if the system is over `vm_dirty_ratio'. 993 * the caller to wait once crossing the (background_thresh + dirty_thresh) / 2.
649 * If we're over `background_thresh' then the writeback threads are woken to 994 * If we're over `background_thresh' then the writeback threads are woken to
650 * perform some writeout. 995 * perform some writeout.
651 */ 996 */
652static void balance_dirty_pages(struct address_space *mapping, 997static void balance_dirty_pages(struct address_space *mapping,
653 unsigned long write_chunk) 998 unsigned long pages_dirtied)
654{ 999{
655 unsigned long nr_reclaimable, bdi_nr_reclaimable; 1000 unsigned long nr_reclaimable; /* = file_dirty + unstable_nfs */
1001 unsigned long bdi_reclaimable;
656 unsigned long nr_dirty; /* = file_dirty + writeback + unstable_nfs */ 1002 unsigned long nr_dirty; /* = file_dirty + writeback + unstable_nfs */
657 unsigned long bdi_dirty; 1003 unsigned long bdi_dirty;
1004 unsigned long freerun;
658 unsigned long background_thresh; 1005 unsigned long background_thresh;
659 unsigned long dirty_thresh; 1006 unsigned long dirty_thresh;
660 unsigned long bdi_thresh; 1007 unsigned long bdi_thresh;
661 unsigned long task_bdi_thresh; 1008 long pause = 0;
662 unsigned long min_task_bdi_thresh; 1009 long uninitialized_var(max_pause);
663 unsigned long pages_written = 0;
664 unsigned long pause = 1;
665 bool dirty_exceeded = false; 1010 bool dirty_exceeded = false;
666 bool clear_dirty_exceeded = true; 1011 unsigned long task_ratelimit;
1012 unsigned long uninitialized_var(dirty_ratelimit);
1013 unsigned long pos_ratio;
667 struct backing_dev_info *bdi = mapping->backing_dev_info; 1014 struct backing_dev_info *bdi = mapping->backing_dev_info;
668 unsigned long start_time = jiffies; 1015 unsigned long start_time = jiffies;
669 1016
670 for (;;) { 1017 for (;;) {
1018 /*
1019 * Unstable writes are a feature of certain networked
1020 * filesystems (i.e. NFS) in which data may have been
1021 * written to the server's write cache, but has not yet
1022 * been flushed to permanent storage.
1023 */
671 nr_reclaimable = global_page_state(NR_FILE_DIRTY) + 1024 nr_reclaimable = global_page_state(NR_FILE_DIRTY) +
672 global_page_state(NR_UNSTABLE_NFS); 1025 global_page_state(NR_UNSTABLE_NFS);
673 nr_dirty = nr_reclaimable + global_page_state(NR_WRITEBACK); 1026 nr_dirty = nr_reclaimable + global_page_state(NR_WRITEBACK);
@@ -679,12 +1032,28 @@ static void balance_dirty_pages(struct address_space *mapping,
679 * catch-up. This avoids (excessively) small writeouts 1032 * catch-up. This avoids (excessively) small writeouts
680 * when the bdi limits are ramping up. 1033 * when the bdi limits are ramping up.
681 */ 1034 */
682 if (nr_dirty <= (background_thresh + dirty_thresh) / 2) 1035 freerun = dirty_freerun_ceiling(dirty_thresh,
1036 background_thresh);
1037 if (nr_dirty <= freerun)
683 break; 1038 break;
684 1039
1040 if (unlikely(!writeback_in_progress(bdi)))
1041 bdi_start_background_writeback(bdi);
1042
1043 /*
1044 * bdi_thresh is not treated as some limiting factor as
1045 * dirty_thresh, due to reasons
1046 * - in JBOD setup, bdi_thresh can fluctuate a lot
1047 * - in a system with HDD and USB key, the USB key may somehow
1048 * go into state (bdi_dirty >> bdi_thresh) either because
1049 * bdi_dirty starts high, or because bdi_thresh drops low.
1050 * In this case we don't want to hard throttle the USB key
1051 * dirtiers for 100 seconds until bdi_dirty drops under
1052 * bdi_thresh. Instead the auxiliary bdi control line in
1053 * bdi_position_ratio() will let the dirtier task progress
1054 * at some rate <= (write_bw / 2) for bringing down bdi_dirty.
1055 */
685 bdi_thresh = bdi_dirty_limit(bdi, dirty_thresh); 1056 bdi_thresh = bdi_dirty_limit(bdi, dirty_thresh);
686 min_task_bdi_thresh = task_min_dirty_limit(bdi_thresh);
687 task_bdi_thresh = task_dirty_limit(current, bdi_thresh);
688 1057
689 /* 1058 /*
690 * In order to avoid the stacked BDI deadlock we need 1059 * In order to avoid the stacked BDI deadlock we need
@@ -696,82 +1065,101 @@ static void balance_dirty_pages(struct address_space *mapping,
696 * actually dirty; with m+n sitting in the percpu 1065 * actually dirty; with m+n sitting in the percpu
697 * deltas. 1066 * deltas.
698 */ 1067 */
699 if (task_bdi_thresh < 2 * bdi_stat_error(bdi)) { 1068 if (bdi_thresh < 2 * bdi_stat_error(bdi)) {
700 bdi_nr_reclaimable = bdi_stat_sum(bdi, BDI_RECLAIMABLE); 1069 bdi_reclaimable = bdi_stat_sum(bdi, BDI_RECLAIMABLE);
701 bdi_dirty = bdi_nr_reclaimable + 1070 bdi_dirty = bdi_reclaimable +
702 bdi_stat_sum(bdi, BDI_WRITEBACK); 1071 bdi_stat_sum(bdi, BDI_WRITEBACK);
703 } else { 1072 } else {
704 bdi_nr_reclaimable = bdi_stat(bdi, BDI_RECLAIMABLE); 1073 bdi_reclaimable = bdi_stat(bdi, BDI_RECLAIMABLE);
705 bdi_dirty = bdi_nr_reclaimable + 1074 bdi_dirty = bdi_reclaimable +
706 bdi_stat(bdi, BDI_WRITEBACK); 1075 bdi_stat(bdi, BDI_WRITEBACK);
707 } 1076 }
708 1077
709 /* 1078 dirty_exceeded = (bdi_dirty > bdi_thresh) ||
710 * The bdi thresh is somehow "soft" limit derived from the
711 * global "hard" limit. The former helps to prevent heavy IO
712 * bdi or process from holding back light ones; The latter is
713 * the last resort safeguard.
714 */
715 dirty_exceeded = (bdi_dirty > task_bdi_thresh) ||
716 (nr_dirty > dirty_thresh); 1079 (nr_dirty > dirty_thresh);
717 clear_dirty_exceeded = (bdi_dirty <= min_task_bdi_thresh) && 1080 if (dirty_exceeded && !bdi->dirty_exceeded)
718 (nr_dirty <= dirty_thresh);
719
720 if (!dirty_exceeded)
721 break;
722
723 if (!bdi->dirty_exceeded)
724 bdi->dirty_exceeded = 1; 1081 bdi->dirty_exceeded = 1;
725 1082
726 bdi_update_bandwidth(bdi, dirty_thresh, nr_dirty, 1083 bdi_update_bandwidth(bdi, dirty_thresh, background_thresh,
727 bdi_thresh, bdi_dirty, start_time); 1084 nr_dirty, bdi_thresh, bdi_dirty,
728 1085 start_time);
729 /* Note: nr_reclaimable denotes nr_dirty + nr_unstable. 1086
730 * Unstable writes are a feature of certain networked 1087 max_pause = bdi_max_pause(bdi, bdi_dirty);
731 * filesystems (i.e. NFS) in which data may have been 1088
732 * written to the server's write cache, but has not yet 1089 dirty_ratelimit = bdi->dirty_ratelimit;
733 * been flushed to permanent storage. 1090 pos_ratio = bdi_position_ratio(bdi, dirty_thresh,
734 * Only move pages to writeback if this bdi is over its 1091 background_thresh, nr_dirty,
735 * threshold otherwise wait until the disk writes catch 1092 bdi_thresh, bdi_dirty);
736 * up. 1093 task_ratelimit = ((u64)dirty_ratelimit * pos_ratio) >>
737 */ 1094 RATELIMIT_CALC_SHIFT;
738 trace_balance_dirty_start(bdi); 1095 if (unlikely(task_ratelimit == 0)) {
739 if (bdi_nr_reclaimable > task_bdi_thresh) { 1096 pause = max_pause;
740 pages_written += writeback_inodes_wb(&bdi->wb, 1097 goto pause;
741 write_chunk); 1098 }
742 trace_balance_dirty_written(bdi, pages_written); 1099 pause = HZ * pages_dirtied / task_ratelimit;
743 if (pages_written >= write_chunk) 1100 if (unlikely(pause <= 0)) {
744 break; /* We've done our duty */ 1101 trace_balance_dirty_pages(bdi,
1102 dirty_thresh,
1103 background_thresh,
1104 nr_dirty,
1105 bdi_thresh,
1106 bdi_dirty,
1107 dirty_ratelimit,
1108 task_ratelimit,
1109 pages_dirtied,
1110 pause,
1111 start_time);
1112 pause = 1; /* avoid resetting nr_dirtied_pause below */
1113 break;
745 } 1114 }
746 __set_current_state(TASK_UNINTERRUPTIBLE); 1115 pause = min(pause, max_pause);
1116
1117pause:
1118 trace_balance_dirty_pages(bdi,
1119 dirty_thresh,
1120 background_thresh,
1121 nr_dirty,
1122 bdi_thresh,
1123 bdi_dirty,
1124 dirty_ratelimit,
1125 task_ratelimit,
1126 pages_dirtied,
1127 pause,
1128 start_time);
1129 __set_current_state(TASK_KILLABLE);
747 io_schedule_timeout(pause); 1130 io_schedule_timeout(pause);
748 trace_balance_dirty_wait(bdi);
749 1131
750 dirty_thresh = hard_dirty_limit(dirty_thresh);
751 /* 1132 /*
752 * max-pause area. If dirty exceeded but still within this 1133 * This is typically equal to (nr_dirty < dirty_thresh) and can
753 * area, no need to sleep for more than 200ms: (a) 8 pages per 1134 * also keep "1000+ dd on a slow USB stick" under control.
754 * 200ms is typically more than enough to curb heavy dirtiers;
755 * (b) the pause time limit makes the dirtiers more responsive.
756 */ 1135 */
757 if (nr_dirty < dirty_thresh && 1136 if (task_ratelimit)
758 bdi_dirty < (task_bdi_thresh + bdi_thresh) / 2 &&
759 time_after(jiffies, start_time + MAX_PAUSE))
760 break; 1137 break;
761 1138
762 /* 1139 if (fatal_signal_pending(current))
763 * Increase the delay for each loop, up to our previous 1140 break;
764 * default of taking a 100ms nap.
765 */
766 pause <<= 1;
767 if (pause > HZ / 10)
768 pause = HZ / 10;
769 } 1141 }
770 1142
771 /* Clear dirty_exceeded flag only when no task can exceed the limit */ 1143 if (!dirty_exceeded && bdi->dirty_exceeded)
772 if (clear_dirty_exceeded && bdi->dirty_exceeded)
773 bdi->dirty_exceeded = 0; 1144 bdi->dirty_exceeded = 0;
774 1145
1146 current->nr_dirtied = 0;
1147 if (pause == 0) { /* in freerun area */
1148 current->nr_dirtied_pause =
1149 dirty_poll_interval(nr_dirty, dirty_thresh);
1150 } else if (pause <= max_pause / 4 &&
1151 pages_dirtied >= current->nr_dirtied_pause) {
1152 current->nr_dirtied_pause = clamp_val(
1153 dirty_ratelimit * (max_pause / 2) / HZ,
1154 pages_dirtied + pages_dirtied / 8,
1155 pages_dirtied * 4);
1156 } else if (pause >= max_pause) {
1157 current->nr_dirtied_pause = 1 | clamp_val(
1158 dirty_ratelimit * (max_pause / 2) / HZ,
1159 pages_dirtied / 4,
1160 pages_dirtied - pages_dirtied / 8);
1161 }
1162
775 if (writeback_in_progress(bdi)) 1163 if (writeback_in_progress(bdi))
776 return; 1164 return;
777 1165
@@ -783,8 +1171,10 @@ static void balance_dirty_pages(struct address_space *mapping,
783 * In normal mode, we start background writeout at the lower 1171 * In normal mode, we start background writeout at the lower
784 * background_thresh, to keep the amount of dirty memory low. 1172 * background_thresh, to keep the amount of dirty memory low.
785 */ 1173 */
786 if ((laptop_mode && pages_written) || 1174 if (laptop_mode)
787 (!laptop_mode && (nr_reclaimable > background_thresh))) 1175 return;
1176
1177 if (nr_reclaimable > background_thresh)
788 bdi_start_background_writeback(bdi); 1178 bdi_start_background_writeback(bdi);
789} 1179}
790 1180
@@ -798,7 +1188,7 @@ void set_page_dirty_balance(struct page *page, int page_mkwrite)
798 } 1188 }
799} 1189}
800 1190
801static DEFINE_PER_CPU(unsigned long, bdp_ratelimits) = 0; 1191static DEFINE_PER_CPU(int, bdp_ratelimits);
802 1192
803/** 1193/**
804 * balance_dirty_pages_ratelimited_nr - balance dirty memory state 1194 * balance_dirty_pages_ratelimited_nr - balance dirty memory state
@@ -818,31 +1208,39 @@ void balance_dirty_pages_ratelimited_nr(struct address_space *mapping,
818 unsigned long nr_pages_dirtied) 1208 unsigned long nr_pages_dirtied)
819{ 1209{
820 struct backing_dev_info *bdi = mapping->backing_dev_info; 1210 struct backing_dev_info *bdi = mapping->backing_dev_info;
821 unsigned long ratelimit; 1211 int ratelimit;
822 unsigned long *p; 1212 int *p;
823 1213
824 if (!bdi_cap_account_dirty(bdi)) 1214 if (!bdi_cap_account_dirty(bdi))
825 return; 1215 return;
826 1216
827 ratelimit = ratelimit_pages; 1217 ratelimit = current->nr_dirtied_pause;
828 if (mapping->backing_dev_info->dirty_exceeded) 1218 if (bdi->dirty_exceeded)
829 ratelimit = 8; 1219 ratelimit = min(ratelimit, 32 >> (PAGE_SHIFT - 10));
1220
1221 current->nr_dirtied += nr_pages_dirtied;
830 1222
1223 preempt_disable();
831 /* 1224 /*
832 * Check the rate limiting. Also, we do not want to throttle real-time 1225 * This prevents one CPU to accumulate too many dirtied pages without
833 * tasks in balance_dirty_pages(). Period. 1226 * calling into balance_dirty_pages(), which can happen when there are
1227 * 1000+ tasks, all of them start dirtying pages at exactly the same
1228 * time, hence all honoured too large initial task->nr_dirtied_pause.
834 */ 1229 */
835 preempt_disable();
836 p = &__get_cpu_var(bdp_ratelimits); 1230 p = &__get_cpu_var(bdp_ratelimits);
837 *p += nr_pages_dirtied; 1231 if (unlikely(current->nr_dirtied >= ratelimit))
838 if (unlikely(*p >= ratelimit)) {
839 ratelimit = sync_writeback_pages(*p);
840 *p = 0; 1232 *p = 0;
841 preempt_enable(); 1233 else {
842 balance_dirty_pages(mapping, ratelimit); 1234 *p += nr_pages_dirtied;
843 return; 1235 if (unlikely(*p >= ratelimit_pages)) {
1236 *p = 0;
1237 ratelimit = 0;
1238 }
844 } 1239 }
845 preempt_enable(); 1240 preempt_enable();
1241
1242 if (unlikely(current->nr_dirtied >= ratelimit))
1243 balance_dirty_pages(mapping, current->nr_dirtied);
846} 1244}
847EXPORT_SYMBOL(balance_dirty_pages_ratelimited_nr); 1245EXPORT_SYMBOL(balance_dirty_pages_ratelimited_nr);
848 1246
@@ -898,7 +1296,8 @@ void laptop_mode_timer_fn(unsigned long data)
898 * threshold 1296 * threshold
899 */ 1297 */
900 if (bdi_has_dirty_io(&q->backing_dev_info)) 1298 if (bdi_has_dirty_io(&q->backing_dev_info))
901 bdi_start_writeback(&q->backing_dev_info, nr_pages); 1299 bdi_start_writeback(&q->backing_dev_info, nr_pages,
1300 WB_REASON_LAPTOP_TIMER);
902} 1301}
903 1302
904/* 1303/*
@@ -937,22 +1336,17 @@ void laptop_sync_completion(void)
937 * 1336 *
938 * Here we set ratelimit_pages to a level which ensures that when all CPUs are 1337 * Here we set ratelimit_pages to a level which ensures that when all CPUs are
939 * dirtying in parallel, we cannot go more than 3% (1/32) over the dirty memory 1338 * dirtying in parallel, we cannot go more than 3% (1/32) over the dirty memory
940 * thresholds before writeback cuts in. 1339 * thresholds.
941 *
942 * But the limit should not be set too high. Because it also controls the
943 * amount of memory which the balance_dirty_pages() caller has to write back.
944 * If this is too large then the caller will block on the IO queue all the
945 * time. So limit it to four megabytes - the balance_dirty_pages() caller
946 * will write six megabyte chunks, max.
947 */ 1340 */
948 1341
949void writeback_set_ratelimit(void) 1342void writeback_set_ratelimit(void)
950{ 1343{
951 ratelimit_pages = vm_total_pages / (num_online_cpus() * 32); 1344 unsigned long background_thresh;
1345 unsigned long dirty_thresh;
1346 global_dirty_limits(&background_thresh, &dirty_thresh);
1347 ratelimit_pages = dirty_thresh / (num_online_cpus() * 32);
952 if (ratelimit_pages < 16) 1348 if (ratelimit_pages < 16)
953 ratelimit_pages = 16; 1349 ratelimit_pages = 16;
954 if (ratelimit_pages * PAGE_CACHE_SIZE > 4096 * 1024)
955 ratelimit_pages = (4096 * 1024) / PAGE_CACHE_SIZE;
956} 1350}
957 1351
958static int __cpuinit 1352static int __cpuinit
@@ -994,7 +1388,6 @@ void __init page_writeback_init(void)
994 1388
995 shift = calc_period_shift(); 1389 shift = calc_period_shift();
996 prop_descriptor_init(&vm_completions, shift); 1390 prop_descriptor_init(&vm_completions, shift);
997 prop_descriptor_init(&vm_dirties, shift);
998} 1391}
999 1392
1000/** 1393/**
@@ -1322,7 +1715,7 @@ void account_page_dirtied(struct page *page, struct address_space *mapping)
1322 __inc_zone_page_state(page, NR_FILE_DIRTY); 1715 __inc_zone_page_state(page, NR_FILE_DIRTY);
1323 __inc_zone_page_state(page, NR_DIRTIED); 1716 __inc_zone_page_state(page, NR_DIRTIED);
1324 __inc_bdi_stat(mapping->backing_dev_info, BDI_RECLAIMABLE); 1717 __inc_bdi_stat(mapping->backing_dev_info, BDI_RECLAIMABLE);
1325 task_dirty_inc(current); 1718 __inc_bdi_stat(mapping->backing_dev_info, BDI_DIRTIED);
1326 task_io_account_write(PAGE_CACHE_SIZE); 1719 task_io_account_write(PAGE_CACHE_SIZE);
1327 } 1720 }
1328} 1721}