1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <linux/kernel.h>
15#include <linux/export.h>
16#include <linux/spinlock.h>
17#include <linux/fs.h>
18#include <linux/mm.h>
19#include <linux/swap.h>
20#include <linux/slab.h>
21#include <linux/pagemap.h>
22#include <linux/writeback.h>
23#include <linux/init.h>
24#include <linux/backing-dev.h>
25#include <linux/task_io_accounting_ops.h>
26#include <linux/blkdev.h>
27#include <linux/mpage.h>
28#include <linux/rmap.h>
29#include <linux/percpu.h>
30#include <linux/notifier.h>
31#include <linux/smp.h>
32#include <linux/sysctl.h>
33#include <linux/cpu.h>
34#include <linux/syscalls.h>
35#include <linux/buffer_head.h>
36#include <linux/pagevec.h>
37#include <trace/events/writeback.h>
38
39
40
41
42#define MAX_PAUSE max(HZ/5, 1)
43
44
45
46
47
48#define DIRTY_POLL_THRESH (128 >> (PAGE_SHIFT - 10))
49
50
51
52
53#define BANDWIDTH_INTERVAL max(HZ/5, 1)
54
55#define RATELIMIT_CALC_SHIFT 10
56
57
58
59
60
61static long ratelimit_pages = 32;
62
63
64
65
66
67
68int dirty_background_ratio = 10;
69
70
71
72
73
74unsigned long dirty_background_bytes;
75
76
77
78
79
80int vm_highmem_is_dirtyable;
81
82
83
84
85int vm_dirty_ratio = 20;
86
87
88
89
90
91unsigned long vm_dirty_bytes;
92
93
94
95
96unsigned int dirty_writeback_interval = 5 * 100;
97
98
99
100
101unsigned int dirty_expire_interval = 30 * 100;
102
103
104
105
106int block_dump;
107
108
109
110
111
112int laptop_mode;
113
114EXPORT_SYMBOL(laptop_mode);
115
116
117
118unsigned long global_dirty_limit;
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136static struct prop_descriptor vm_completions;
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174static unsigned long highmem_dirtyable_memory(unsigned long total)
175{
176#ifdef CONFIG_HIGHMEM
177 int node;
178 unsigned long x = 0;
179
180 for_each_node_state(node, N_HIGH_MEMORY) {
181 struct zone *z =
182 &NODE_DATA(node)->node_zones[ZONE_HIGHMEM];
183
184 x += zone_page_state(z, NR_FREE_PAGES) +
185 zone_reclaimable_pages(z) - z->dirty_balance_reserve;
186 }
187
188
189
190
191
192
193 return min(x, total);
194#else
195 return 0;
196#endif
197}
198
199
200
201
202
203
204
205unsigned long global_dirtyable_memory(void)
206{
207 unsigned long x;
208
209 x = global_page_state(NR_FREE_PAGES) + global_reclaimable_pages() -
210 dirty_balance_reserve;
211
212 if (!vm_highmem_is_dirtyable)
213 x -= highmem_dirtyable_memory(x);
214
215 return x + 1;
216}
217
218
219
220
221
222
223
224
225
226
227void global_dirty_limits(unsigned long *pbackground, unsigned long *pdirty)
228{
229 unsigned long background;
230 unsigned long dirty;
231 unsigned long uninitialized_var(available_memory);
232 struct task_struct *tsk;
233
234 if (!vm_dirty_bytes || !dirty_background_bytes)
235 available_memory = global_dirtyable_memory();
236
237 if (vm_dirty_bytes)
238 dirty = DIV_ROUND_UP(vm_dirty_bytes, PAGE_SIZE);
239 else
240 dirty = (vm_dirty_ratio * available_memory) / 100;
241
242 if (dirty_background_bytes)
243 background = DIV_ROUND_UP(dirty_background_bytes, PAGE_SIZE);
244 else
245 background = (dirty_background_ratio * available_memory) / 100;
246
247 if (background >= dirty)
248 background = dirty / 2;
249 tsk = current;
250 if (tsk->flags & PF_LESS_THROTTLE || rt_task(tsk)) {
251 background += background / 4;
252 dirty += dirty / 4;
253 }
254 *pbackground = background;
255 *pdirty = dirty;
256 trace_global_dirty_state(background, dirty);
257}
258
259
260
261
262
263
264
265
266static unsigned long zone_dirtyable_memory(struct zone *zone)
267{
268
269
270
271
272
273
274
275
276
277 return zone_page_state(zone, NR_FREE_PAGES) +
278 zone_reclaimable_pages(zone) -
279 zone->dirty_balance_reserve;
280}
281
282
283
284
285
286
287
288
289static unsigned long zone_dirty_limit(struct zone *zone)
290{
291 unsigned long zone_memory = zone_dirtyable_memory(zone);
292 struct task_struct *tsk = current;
293 unsigned long dirty;
294
295 if (vm_dirty_bytes)
296 dirty = DIV_ROUND_UP(vm_dirty_bytes, PAGE_SIZE) *
297 zone_memory / global_dirtyable_memory();
298 else
299 dirty = vm_dirty_ratio * zone_memory / 100;
300
301 if (tsk->flags & PF_LESS_THROTTLE || rt_task(tsk))
302 dirty += dirty / 4;
303
304 return dirty;
305}
306
307
308
309
310
311
312
313
314bool zone_dirty_ok(struct zone *zone)
315{
316 unsigned long limit = zone_dirty_limit(zone);
317
318 return zone_page_state(zone, NR_FILE_DIRTY) +
319 zone_page_state(zone, NR_UNSTABLE_NFS) +
320 zone_page_state(zone, NR_WRITEBACK) <= limit;
321}
322
323
324
325
326
327
328static int calc_period_shift(void)
329{
330 unsigned long dirty_total;
331
332 if (vm_dirty_bytes)
333 dirty_total = vm_dirty_bytes / PAGE_SIZE;
334 else
335 dirty_total = (vm_dirty_ratio * global_dirtyable_memory()) /
336 100;
337 return 2 + ilog2(dirty_total - 1);
338}
339
340
341
342
343static void update_completion_period(void)
344{
345 int shift = calc_period_shift();
346 prop_change_shift(&vm_completions, shift);
347
348 writeback_set_ratelimit();
349}
350
351int dirty_background_ratio_handler(struct ctl_table *table, int write,
352 void __user *buffer, size_t *lenp,
353 loff_t *ppos)
354{
355 int ret;
356
357 ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
358 if (ret == 0 && write)
359 dirty_background_bytes = 0;
360 return ret;
361}
362
363int dirty_background_bytes_handler(struct ctl_table *table, int write,
364 void __user *buffer, size_t *lenp,
365 loff_t *ppos)
366{
367 int ret;
368
369 ret = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
370 if (ret == 0 && write)
371 dirty_background_ratio = 0;
372 return ret;
373}
374
375int dirty_ratio_handler(struct ctl_table *table, int write,
376 void __user *buffer, size_t *lenp,
377 loff_t *ppos)
378{
379 int old_ratio = vm_dirty_ratio;
380 int ret;
381
382 ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
383 if (ret == 0 && write && vm_dirty_ratio != old_ratio) {
384 update_completion_period();
385 vm_dirty_bytes = 0;
386 }
387 return ret;
388}
389
390int dirty_bytes_handler(struct ctl_table *table, int write,
391 void __user *buffer, size_t *lenp,
392 loff_t *ppos)
393{
394 unsigned long old_bytes = vm_dirty_bytes;
395 int ret;
396
397 ret = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
398 if (ret == 0 && write && vm_dirty_bytes != old_bytes) {
399 update_completion_period();
400 vm_dirty_ratio = 0;
401 }
402 return ret;
403}
404
405
406
407
408
409static inline void __bdi_writeout_inc(struct backing_dev_info *bdi)
410{
411 __inc_bdi_stat(bdi, BDI_WRITTEN);
412 __prop_inc_percpu_max(&vm_completions, &bdi->completions,
413 bdi->max_prop_frac);
414}
415
416void bdi_writeout_inc(struct backing_dev_info *bdi)
417{
418 unsigned long flags;
419
420 local_irq_save(flags);
421 __bdi_writeout_inc(bdi);
422 local_irq_restore(flags);
423}
424EXPORT_SYMBOL_GPL(bdi_writeout_inc);
425
426
427
428
429static void bdi_writeout_fraction(struct backing_dev_info *bdi,
430 long *numerator, long *denominator)
431{
432 prop_fraction_percpu(&vm_completions, &bdi->completions,
433 numerator, denominator);
434}
435
436
437
438
439
440
441static unsigned int bdi_min_ratio;
442
443int bdi_set_min_ratio(struct backing_dev_info *bdi, unsigned int min_ratio)
444{
445 int ret = 0;
446
447 spin_lock_bh(&bdi_lock);
448 if (min_ratio > bdi->max_ratio) {
449 ret = -EINVAL;
450 } else {
451 min_ratio -= bdi->min_ratio;
452 if (bdi_min_ratio + min_ratio < 100) {
453 bdi_min_ratio += min_ratio;
454 bdi->min_ratio += min_ratio;
455 } else {
456 ret = -EINVAL;
457 }
458 }
459 spin_unlock_bh(&bdi_lock);
460
461 return ret;
462}
463
464int bdi_set_max_ratio(struct backing_dev_info *bdi, unsigned max_ratio)
465{
466 int ret = 0;
467
468 if (max_ratio > 100)
469 return -EINVAL;
470
471 spin_lock_bh(&bdi_lock);
472 if (bdi->min_ratio > max_ratio) {
473 ret = -EINVAL;
474 } else {
475 bdi->max_ratio = max_ratio;
476 bdi->max_prop_frac = (PROP_FRAC_BASE * max_ratio) / 100;
477 }
478 spin_unlock_bh(&bdi_lock);
479
480 return ret;
481}
482EXPORT_SYMBOL(bdi_set_max_ratio);
483
484static unsigned long dirty_freerun_ceiling(unsigned long thresh,
485 unsigned long bg_thresh)
486{
487 return (thresh + bg_thresh) / 2;
488}
489
490static unsigned long hard_dirty_limit(unsigned long thresh)
491{
492 return max(thresh, global_dirty_limit);
493}
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517unsigned long bdi_dirty_limit(struct backing_dev_info *bdi, unsigned long dirty)
518{
519 u64 bdi_dirty;
520 long numerator, denominator;
521
522
523
524
525 bdi_writeout_fraction(bdi, &numerator, &denominator);
526
527 bdi_dirty = (dirty * (100 - bdi_min_ratio)) / 100;
528 bdi_dirty *= numerator;
529 do_div(bdi_dirty, denominator);
530
531 bdi_dirty += (dirty * bdi->min_ratio) / 100;
532 if (bdi_dirty > (dirty * bdi->max_ratio) / 100)
533 bdi_dirty = dirty * bdi->max_ratio / 100;
534
535 return bdi_dirty;
536}
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613static unsigned long bdi_position_ratio(struct backing_dev_info *bdi,
614 unsigned long thresh,
615 unsigned long bg_thresh,
616 unsigned long dirty,
617 unsigned long bdi_thresh,
618 unsigned long bdi_dirty)
619{
620 unsigned long write_bw = bdi->avg_write_bandwidth;
621 unsigned long freerun = dirty_freerun_ceiling(thresh, bg_thresh);
622 unsigned long limit = hard_dirty_limit(thresh);
623 unsigned long x_intercept;
624 unsigned long setpoint;
625 unsigned long bdi_setpoint;
626 unsigned long span;
627 long long pos_ratio;
628 long x;
629
630 if (unlikely(dirty >= limit))
631 return 0;
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649 setpoint = (freerun + limit) / 2;
650 x = div_s64((setpoint - dirty) << RATELIMIT_CALC_SHIFT,
651 limit - setpoint + 1);
652 pos_ratio = x;
653 pos_ratio = pos_ratio * x >> RATELIMIT_CALC_SHIFT;
654 pos_ratio = pos_ratio * x >> RATELIMIT_CALC_SHIFT;
655 pos_ratio += 1 << RATELIMIT_CALC_SHIFT;
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688 if (unlikely(bdi_thresh > thresh))
689 bdi_thresh = thresh;
690
691
692
693
694
695
696
697 bdi_thresh = max(bdi_thresh, (limit - dirty) / 8);
698
699
700
701
702 x = div_u64((u64)bdi_thresh << 16, thresh + 1);
703 bdi_setpoint = setpoint * (u64)x >> 16;
704
705
706
707
708
709
710
711
712 span = (thresh - bdi_thresh + 8 * write_bw) * (u64)x >> 16;
713 x_intercept = bdi_setpoint + span;
714
715 if (bdi_dirty < x_intercept - span / 4) {
716 pos_ratio = div_u64(pos_ratio * (x_intercept - bdi_dirty),
717 x_intercept - bdi_setpoint + 1);
718 } else
719 pos_ratio /= 4;
720
721
722
723
724
725
726 x_intercept = bdi_thresh / 2;
727 if (bdi_dirty < x_intercept) {
728 if (bdi_dirty > x_intercept / 8)
729 pos_ratio = div_u64(pos_ratio * x_intercept, bdi_dirty);
730 else
731 pos_ratio *= 8;
732 }
733
734 return pos_ratio;
735}
736
737static void bdi_update_write_bandwidth(struct backing_dev_info *bdi,
738 unsigned long elapsed,
739 unsigned long written)
740{
741 const unsigned long period = roundup_pow_of_two(3 * HZ);
742 unsigned long avg = bdi->avg_write_bandwidth;
743 unsigned long old = bdi->write_bandwidth;
744 u64 bw;
745
746
747
748
749
750
751
752
753 bw = written - bdi->written_stamp;
754 bw *= HZ;
755 if (unlikely(elapsed > period)) {
756 do_div(bw, elapsed);
757 avg = bw;
758 goto out;
759 }
760 bw += (u64)bdi->write_bandwidth * (period - elapsed);
761 bw >>= ilog2(period);
762
763
764
765
766 if (avg > old && old >= (unsigned long)bw)
767 avg -= (avg - old) >> 3;
768
769 if (avg < old && old <= (unsigned long)bw)
770 avg += (old - avg) >> 3;
771
772out:
773 bdi->write_bandwidth = bw;
774 bdi->avg_write_bandwidth = avg;
775}
776
777
778
779
780
781
782
783
784
785static void update_dirty_limit(unsigned long thresh, unsigned long dirty)
786{
787 unsigned long limit = global_dirty_limit;
788
789
790
791
792 if (limit < thresh) {
793 limit = thresh;
794 goto update;
795 }
796
797
798
799
800
801
802 thresh = max(thresh, dirty);
803 if (limit > thresh) {
804 limit -= (limit - thresh) >> 5;
805 goto update;
806 }
807 return;
808update:
809 global_dirty_limit = limit;
810}
811
812static void global_update_bandwidth(unsigned long thresh,
813 unsigned long dirty,
814 unsigned long now)
815{
816 static DEFINE_SPINLOCK(dirty_lock);
817 static unsigned long update_time;
818
819
820
821
822 if (time_before(now, update_time + BANDWIDTH_INTERVAL))
823 return;
824
825 spin_lock(&dirty_lock);
826 if (time_after_eq(now, update_time + BANDWIDTH_INTERVAL)) {
827 update_dirty_limit(thresh, dirty);
828 update_time = now;
829 }
830 spin_unlock(&dirty_lock);
831}
832
833
834
835
836
837
838
839static void bdi_update_dirty_ratelimit(struct backing_dev_info *bdi,
840 unsigned long thresh,
841 unsigned long bg_thresh,
842 unsigned long dirty,
843 unsigned long bdi_thresh,
844 unsigned long bdi_dirty,
845 unsigned long dirtied,
846 unsigned long elapsed)
847{
848 unsigned long freerun = dirty_freerun_ceiling(thresh, bg_thresh);
849 unsigned long limit = hard_dirty_limit(thresh);
850 unsigned long setpoint = (freerun + limit) / 2;
851 unsigned long write_bw = bdi->avg_write_bandwidth;
852 unsigned long dirty_ratelimit = bdi->dirty_ratelimit;
853 unsigned long dirty_rate;
854 unsigned long task_ratelimit;
855 unsigned long balanced_dirty_ratelimit;
856 unsigned long pos_ratio;
857 unsigned long step;
858 unsigned long x;
859
860
861
862
863
864 dirty_rate = (dirtied - bdi->dirtied_stamp) * HZ / elapsed;
865
866 pos_ratio = bdi_position_ratio(bdi, thresh, bg_thresh, dirty,
867 bdi_thresh, bdi_dirty);
868
869
870
871 task_ratelimit = (u64)dirty_ratelimit *
872 pos_ratio >> RATELIMIT_CALC_SHIFT;
873 task_ratelimit++;
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905 balanced_dirty_ratelimit = div_u64((u64)task_ratelimit * write_bw,
906 dirty_rate | 1);
907
908
909
910 if (unlikely(balanced_dirty_ratelimit > write_bw))
911 balanced_dirty_ratelimit = write_bw;
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947 step = 0;
948 if (dirty < setpoint) {
949 x = min(bdi->balanced_dirty_ratelimit,
950 min(balanced_dirty_ratelimit, task_ratelimit));
951 if (dirty_ratelimit < x)
952 step = x - dirty_ratelimit;
953 } else {
954 x = max(bdi->balanced_dirty_ratelimit,
955 max(balanced_dirty_ratelimit, task_ratelimit));
956 if (dirty_ratelimit > x)
957 step = dirty_ratelimit - x;
958 }
959
960
961
962
963
964
965 step >>= dirty_ratelimit / (2 * step + 1);
966
967
968
969 step = (step + 7) / 8;
970
971 if (dirty_ratelimit < balanced_dirty_ratelimit)
972 dirty_ratelimit += step;
973 else
974 dirty_ratelimit -= step;
975
976 bdi->dirty_ratelimit = max(dirty_ratelimit, 1UL);
977 bdi->balanced_dirty_ratelimit = balanced_dirty_ratelimit;
978
979 trace_bdi_dirty_ratelimit(bdi, dirty_rate, task_ratelimit);
980}
981
982void __bdi_update_bandwidth(struct backing_dev_info *bdi,
983 unsigned long thresh,
984 unsigned long bg_thresh,
985 unsigned long dirty,
986 unsigned long bdi_thresh,
987 unsigned long bdi_dirty,
988 unsigned long start_time)
989{
990 unsigned long now = jiffies;
991 unsigned long elapsed = now - bdi->bw_time_stamp;
992 unsigned long dirtied;
993 unsigned long written;
994
995
996
997
998 if (elapsed < BANDWIDTH_INTERVAL)
999 return;
1000
1001 dirtied = percpu_counter_read(&bdi->bdi_stat[BDI_DIRTIED]);
1002 written = percpu_counter_read(&bdi->bdi_stat[BDI_WRITTEN]);
1003
1004
1005
1006
1007
1008 if (elapsed > HZ && time_before(bdi->bw_time_stamp, start_time))
1009 goto snapshot;
1010
1011 if (thresh) {
1012 global_update_bandwidth(thresh, dirty, now);
1013 bdi_update_dirty_ratelimit(bdi, thresh, bg_thresh, dirty,
1014 bdi_thresh, bdi_dirty,
1015 dirtied, elapsed);
1016 }
1017 bdi_update_write_bandwidth(bdi, elapsed, written);
1018
1019snapshot:
1020 bdi->dirtied_stamp = dirtied;
1021 bdi->written_stamp = written;
1022 bdi->bw_time_stamp = now;
1023}
1024
1025static void bdi_update_bandwidth(struct backing_dev_info *bdi,
1026 unsigned long thresh,
1027 unsigned long bg_thresh,
1028 unsigned long dirty,
1029 unsigned long bdi_thresh,
1030 unsigned long bdi_dirty,
1031 unsigned long start_time)
1032{
1033 if (time_is_after_eq_jiffies(bdi->bw_time_stamp + BANDWIDTH_INTERVAL))
1034 return;
1035 spin_lock(&bdi->wb.list_lock);
1036 __bdi_update_bandwidth(bdi, thresh, bg_thresh, dirty,
1037 bdi_thresh, bdi_dirty, start_time);
1038 spin_unlock(&bdi->wb.list_lock);
1039}
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049static unsigned long dirty_poll_interval(unsigned long dirty,
1050 unsigned long thresh)
1051{
1052 if (thresh > dirty)
1053 return 1UL << (ilog2(thresh - dirty) >> 1);
1054
1055 return 1;
1056}
1057
1058static long bdi_max_pause(struct backing_dev_info *bdi,
1059 unsigned long bdi_dirty)
1060{
1061 long bw = bdi->avg_write_bandwidth;
1062 long t;
1063
1064
1065
1066
1067
1068
1069
1070
1071 t = bdi_dirty / (1 + bw / roundup_pow_of_two(1 + HZ / 8));
1072 t++;
1073
1074 return min_t(long, t, MAX_PAUSE);
1075}
1076
1077static long bdi_min_pause(struct backing_dev_info *bdi,
1078 long max_pause,
1079 unsigned long task_ratelimit,
1080 unsigned long dirty_ratelimit,
1081 int *nr_dirtied_pause)
1082{
1083 long hi = ilog2(bdi->avg_write_bandwidth);
1084 long lo = ilog2(bdi->dirty_ratelimit);
1085 long t;
1086 long pause;
1087 int pages;
1088
1089
1090 t = max(1, HZ / 100);
1091
1092
1093
1094
1095
1096
1097
1098 if (hi > lo)
1099 t += (hi - lo) * (10 * HZ) / 1024;
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119 t = min(t, 1 + max_pause / 2);
1120 pages = dirty_ratelimit * t / roundup_pow_of_two(HZ);
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130 if (pages < DIRTY_POLL_THRESH) {
1131 t = max_pause;
1132 pages = dirty_ratelimit * t / roundup_pow_of_two(HZ);
1133 if (pages > DIRTY_POLL_THRESH) {
1134 pages = DIRTY_POLL_THRESH;
1135 t = HZ * DIRTY_POLL_THRESH / dirty_ratelimit;
1136 }
1137 }
1138
1139 pause = HZ * pages / (task_ratelimit + 1);
1140 if (pause > max_pause) {
1141 t = max_pause;
1142 pages = task_ratelimit * t / roundup_pow_of_two(HZ);
1143 }
1144
1145 *nr_dirtied_pause = pages;
1146
1147
1148
1149 return pages >= DIRTY_POLL_THRESH ? 1 + t / 2 : t;
1150}
1151
1152
1153
1154
1155
1156
1157
1158
1159static void balance_dirty_pages(struct address_space *mapping,
1160 unsigned long pages_dirtied)
1161{
1162 unsigned long nr_reclaimable;
1163 unsigned long bdi_reclaimable;
1164 unsigned long nr_dirty;
1165 unsigned long bdi_dirty;
1166 unsigned long freerun;
1167 unsigned long background_thresh;
1168 unsigned long dirty_thresh;
1169 unsigned long bdi_thresh;
1170 long period;
1171 long pause;
1172 long max_pause;
1173 long min_pause;
1174 int nr_dirtied_pause;
1175 bool dirty_exceeded = false;
1176 unsigned long task_ratelimit;
1177 unsigned long dirty_ratelimit;
1178 unsigned long pos_ratio;
1179 struct backing_dev_info *bdi = mapping->backing_dev_info;
1180 unsigned long start_time = jiffies;
1181
1182 for (;;) {
1183 unsigned long now = jiffies;
1184
1185
1186
1187
1188
1189
1190
1191 nr_reclaimable = global_page_state(NR_FILE_DIRTY) +
1192 global_page_state(NR_UNSTABLE_NFS);
1193 nr_dirty = nr_reclaimable + global_page_state(NR_WRITEBACK);
1194
1195 global_dirty_limits(&background_thresh, &dirty_thresh);
1196
1197
1198
1199
1200
1201
1202 freerun = dirty_freerun_ceiling(dirty_thresh,
1203 background_thresh);
1204 if (nr_dirty <= freerun) {
1205 current->dirty_paused_when = now;
1206 current->nr_dirtied = 0;
1207 current->nr_dirtied_pause =
1208 dirty_poll_interval(nr_dirty, dirty_thresh);
1209 break;
1210 }
1211
1212 if (unlikely(!writeback_in_progress(bdi)))
1213 bdi_start_background_writeback(bdi);
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228 bdi_thresh = bdi_dirty_limit(bdi, dirty_thresh);
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240 if (bdi_thresh < 2 * bdi_stat_error(bdi)) {
1241 bdi_reclaimable = bdi_stat_sum(bdi, BDI_RECLAIMABLE);
1242 bdi_dirty = bdi_reclaimable +
1243 bdi_stat_sum(bdi, BDI_WRITEBACK);
1244 } else {
1245 bdi_reclaimable = bdi_stat(bdi, BDI_RECLAIMABLE);
1246 bdi_dirty = bdi_reclaimable +
1247 bdi_stat(bdi, BDI_WRITEBACK);
1248 }
1249
1250 dirty_exceeded = (bdi_dirty > bdi_thresh) &&
1251 (nr_dirty > dirty_thresh);
1252 if (dirty_exceeded && !bdi->dirty_exceeded)
1253 bdi->dirty_exceeded = 1;
1254
1255 bdi_update_bandwidth(bdi, dirty_thresh, background_thresh,
1256 nr_dirty, bdi_thresh, bdi_dirty,
1257 start_time);
1258
1259 dirty_ratelimit = bdi->dirty_ratelimit;
1260 pos_ratio = bdi_position_ratio(bdi, dirty_thresh,
1261 background_thresh, nr_dirty,
1262 bdi_thresh, bdi_dirty);
1263 task_ratelimit = ((u64)dirty_ratelimit * pos_ratio) >>
1264 RATELIMIT_CALC_SHIFT;
1265 max_pause = bdi_max_pause(bdi, bdi_dirty);
1266 min_pause = bdi_min_pause(bdi, max_pause,
1267 task_ratelimit, dirty_ratelimit,
1268 &nr_dirtied_pause);
1269
1270 if (unlikely(task_ratelimit == 0)) {
1271 period = max_pause;
1272 pause = max_pause;
1273 goto pause;
1274 }
1275 period = HZ * pages_dirtied / task_ratelimit;
1276 pause = period;
1277 if (current->dirty_paused_when)
1278 pause -= now - current->dirty_paused_when;
1279
1280
1281
1282
1283
1284
1285
1286 if (pause < min_pause) {
1287 trace_balance_dirty_pages(bdi,
1288 dirty_thresh,
1289 background_thresh,
1290 nr_dirty,
1291 bdi_thresh,
1292 bdi_dirty,
1293 dirty_ratelimit,
1294 task_ratelimit,
1295 pages_dirtied,
1296 period,
1297 min(pause, 0L),
1298 start_time);
1299 if (pause < -HZ) {
1300 current->dirty_paused_when = now;
1301 current->nr_dirtied = 0;
1302 } else if (period) {
1303 current->dirty_paused_when += period;
1304 current->nr_dirtied = 0;
1305 } else if (current->nr_dirtied_pause <= pages_dirtied)
1306 current->nr_dirtied_pause += pages_dirtied;
1307 break;
1308 }
1309 if (unlikely(pause > max_pause)) {
1310
1311 now += min(pause - max_pause, max_pause);
1312 pause = max_pause;
1313 }
1314
1315pause:
1316 trace_balance_dirty_pages(bdi,
1317 dirty_thresh,
1318 background_thresh,
1319 nr_dirty,
1320 bdi_thresh,
1321 bdi_dirty,
1322 dirty_ratelimit,
1323 task_ratelimit,
1324 pages_dirtied,
1325 period,
1326 pause,
1327 start_time);
1328 __set_current_state(TASK_KILLABLE);
1329 io_schedule_timeout(pause);
1330
1331 current->dirty_paused_when = now + pause;
1332 current->nr_dirtied = 0;
1333 current->nr_dirtied_pause = nr_dirtied_pause;
1334
1335
1336
1337
1338
1339 if (task_ratelimit)
1340 break;
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352 if (bdi_dirty <= bdi_stat_error(bdi))
1353 break;
1354
1355 if (fatal_signal_pending(current))
1356 break;
1357 }
1358
1359 if (!dirty_exceeded && bdi->dirty_exceeded)
1360 bdi->dirty_exceeded = 0;
1361
1362 if (writeback_in_progress(bdi))
1363 return;
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373 if (laptop_mode)
1374 return;
1375
1376 if (nr_reclaimable > background_thresh)
1377 bdi_start_background_writeback(bdi);
1378}
1379
1380void set_page_dirty_balance(struct page *page, int page_mkwrite)
1381{
1382 if (set_page_dirty(page) || page_mkwrite) {
1383 struct address_space *mapping = page_mapping(page);
1384
1385 if (mapping)
1386 balance_dirty_pages_ratelimited(mapping);
1387 }
1388}
1389
1390static DEFINE_PER_CPU(int, bdp_ratelimits);
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406DEFINE_PER_CPU(int, dirty_throttle_leaks) = 0;
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422void balance_dirty_pages_ratelimited_nr(struct address_space *mapping,
1423 unsigned long nr_pages_dirtied)
1424{
1425 struct backing_dev_info *bdi = mapping->backing_dev_info;
1426 int ratelimit;
1427 int *p;
1428
1429 if (!bdi_cap_account_dirty(bdi))
1430 return;
1431
1432 ratelimit = current->nr_dirtied_pause;
1433 if (bdi->dirty_exceeded)
1434 ratelimit = min(ratelimit, 32 >> (PAGE_SHIFT - 10));
1435
1436 preempt_disable();
1437
1438
1439
1440
1441
1442
1443 p = &__get_cpu_var(bdp_ratelimits);
1444 if (unlikely(current->nr_dirtied >= ratelimit))
1445 *p = 0;
1446 else if (unlikely(*p >= ratelimit_pages)) {
1447 *p = 0;
1448 ratelimit = 0;
1449 }
1450
1451
1452
1453
1454
1455 p = &__get_cpu_var(dirty_throttle_leaks);
1456 if (*p > 0 && current->nr_dirtied < ratelimit) {
1457 nr_pages_dirtied = min(*p, ratelimit - current->nr_dirtied);
1458 *p -= nr_pages_dirtied;
1459 current->nr_dirtied += nr_pages_dirtied;
1460 }
1461 preempt_enable();
1462
1463 if (unlikely(current->nr_dirtied >= ratelimit))
1464 balance_dirty_pages(mapping, current->nr_dirtied);
1465}
1466EXPORT_SYMBOL(balance_dirty_pages_ratelimited_nr);
1467
1468void throttle_vm_writeout(gfp_t gfp_mask)
1469{
1470 unsigned long background_thresh;
1471 unsigned long dirty_thresh;
1472
1473 for ( ; ; ) {
1474 global_dirty_limits(&background_thresh, &dirty_thresh);
1475
1476
1477
1478
1479
1480 dirty_thresh += dirty_thresh / 10;
1481
1482 if (global_page_state(NR_UNSTABLE_NFS) +
1483 global_page_state(NR_WRITEBACK) <= dirty_thresh)
1484 break;
1485 congestion_wait(BLK_RW_ASYNC, HZ/10);
1486
1487
1488
1489
1490
1491
1492 if ((gfp_mask & (__GFP_FS|__GFP_IO)) != (__GFP_FS|__GFP_IO))
1493 break;
1494 }
1495}
1496
1497
1498
1499
1500int dirty_writeback_centisecs_handler(ctl_table *table, int write,
1501 void __user *buffer, size_t *length, loff_t *ppos)
1502{
1503 proc_dointvec(table, write, buffer, length, ppos);
1504 bdi_arm_supers_timer();
1505 return 0;
1506}
1507
1508#ifdef CONFIG_BLOCK
1509void laptop_mode_timer_fn(unsigned long data)
1510{
1511 struct request_queue *q = (struct request_queue *)data;
1512 int nr_pages = global_page_state(NR_FILE_DIRTY) +
1513 global_page_state(NR_UNSTABLE_NFS);
1514
1515
1516
1517
1518
1519 if (bdi_has_dirty_io(&q->backing_dev_info))
1520 bdi_start_writeback(&q->backing_dev_info, nr_pages,
1521 WB_REASON_LAPTOP_TIMER);
1522}
1523
1524
1525
1526
1527
1528
1529void laptop_io_completion(struct backing_dev_info *info)
1530{
1531 mod_timer(&info->laptop_mode_wb_timer, jiffies + laptop_mode);
1532}
1533
1534
1535
1536
1537
1538
1539void laptop_sync_completion(void)
1540{
1541 struct backing_dev_info *bdi;
1542
1543 rcu_read_lock();
1544
1545 list_for_each_entry_rcu(bdi, &bdi_list, bdi_list)
1546 del_timer(&bdi->laptop_mode_wb_timer);
1547
1548 rcu_read_unlock();
1549}
1550#endif
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563void writeback_set_ratelimit(void)
1564{
1565 unsigned long background_thresh;
1566 unsigned long dirty_thresh;
1567 global_dirty_limits(&background_thresh, &dirty_thresh);
1568 ratelimit_pages = dirty_thresh / (num_online_cpus() * 32);
1569 if (ratelimit_pages < 16)
1570 ratelimit_pages = 16;
1571}
1572
1573static int __cpuinit
1574ratelimit_handler(struct notifier_block *self, unsigned long u, void *v)
1575{
1576 writeback_set_ratelimit();
1577 return NOTIFY_DONE;
1578}
1579
1580static struct notifier_block __cpuinitdata ratelimit_nb = {
1581 .notifier_call = ratelimit_handler,
1582 .next = NULL,
1583};
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603void __init page_writeback_init(void)
1604{
1605 int shift;
1606
1607 writeback_set_ratelimit();
1608 register_cpu_notifier(&ratelimit_nb);
1609
1610 shift = calc_period_shift();
1611 prop_descriptor_init(&vm_completions, shift);
1612}
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631void tag_pages_for_writeback(struct address_space *mapping,
1632 pgoff_t start, pgoff_t end)
1633{
1634#define WRITEBACK_TAG_BATCH 4096
1635 unsigned long tagged;
1636
1637 do {
1638 spin_lock_irq(&mapping->tree_lock);
1639 tagged = radix_tree_range_tag_if_tagged(&mapping->page_tree,
1640 &start, end, WRITEBACK_TAG_BATCH,
1641 PAGECACHE_TAG_DIRTY, PAGECACHE_TAG_TOWRITE);
1642 spin_unlock_irq(&mapping->tree_lock);
1643 WARN_ON_ONCE(tagged > WRITEBACK_TAG_BATCH);
1644 cond_resched();
1645
1646 } while (tagged >= WRITEBACK_TAG_BATCH && start);
1647}
1648EXPORT_SYMBOL(tag_pages_for_writeback);
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672int write_cache_pages(struct address_space *mapping,
1673 struct writeback_control *wbc, writepage_t writepage,
1674 void *data)
1675{
1676 int ret = 0;
1677 int done = 0;
1678 struct pagevec pvec;
1679 int nr_pages;
1680 pgoff_t uninitialized_var(writeback_index);
1681 pgoff_t index;
1682 pgoff_t end;
1683 pgoff_t done_index;
1684 int cycled;
1685 int range_whole = 0;
1686 int tag;
1687
1688 pagevec_init(&pvec, 0);
1689 if (wbc->range_cyclic) {
1690 writeback_index = mapping->writeback_index;
1691 index = writeback_index;
1692 if (index == 0)
1693 cycled = 1;
1694 else
1695 cycled = 0;
1696 end = -1;
1697 } else {
1698 index = wbc->range_start >> PAGE_CACHE_SHIFT;
1699 end = wbc->range_end >> PAGE_CACHE_SHIFT;
1700 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
1701 range_whole = 1;
1702 cycled = 1;
1703 }
1704 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1705 tag = PAGECACHE_TAG_TOWRITE;
1706 else
1707 tag = PAGECACHE_TAG_DIRTY;
1708retry:
1709 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1710 tag_pages_for_writeback(mapping, index, end);
1711 done_index = index;
1712 while (!done && (index <= end)) {
1713 int i;
1714
1715 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
1716 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1717 if (nr_pages == 0)
1718 break;
1719
1720 for (i = 0; i < nr_pages; i++) {
1721 struct page *page = pvec.pages[i];
1722
1723
1724
1725
1726
1727
1728
1729
1730 if (page->index > end) {
1731
1732
1733
1734
1735 done = 1;
1736 break;
1737 }
1738
1739 done_index = page->index;
1740
1741 lock_page(page);
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751 if (unlikely(page->mapping != mapping)) {
1752continue_unlock:
1753 unlock_page(page);
1754 continue;
1755 }
1756
1757 if (!PageDirty(page)) {
1758
1759 goto continue_unlock;
1760 }
1761
1762 if (PageWriteback(page)) {
1763 if (wbc->sync_mode != WB_SYNC_NONE)
1764 wait_on_page_writeback(page);
1765 else
1766 goto continue_unlock;
1767 }
1768
1769 BUG_ON(PageWriteback(page));
1770 if (!clear_page_dirty_for_io(page))
1771 goto continue_unlock;
1772
1773 trace_wbc_writepage(wbc, mapping->backing_dev_info);
1774 ret = (*writepage)(page, wbc, data);
1775 if (unlikely(ret)) {
1776 if (ret == AOP_WRITEPAGE_ACTIVATE) {
1777 unlock_page(page);
1778 ret = 0;
1779 } else {
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789 done_index = page->index + 1;
1790 done = 1;
1791 break;
1792 }
1793 }
1794
1795
1796
1797
1798
1799
1800
1801 if (--wbc->nr_to_write <= 0 &&
1802 wbc->sync_mode == WB_SYNC_NONE) {
1803 done = 1;
1804 break;
1805 }
1806 }
1807 pagevec_release(&pvec);
1808 cond_resched();
1809 }
1810 if (!cycled && !done) {
1811
1812
1813
1814
1815
1816 cycled = 1;
1817 index = 0;
1818 end = writeback_index - 1;
1819 goto retry;
1820 }
1821 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1822 mapping->writeback_index = done_index;
1823
1824 return ret;
1825}
1826EXPORT_SYMBOL(write_cache_pages);
1827
1828
1829
1830
1831
1832static int __writepage(struct page *page, struct writeback_control *wbc,
1833 void *data)
1834{
1835 struct address_space *mapping = data;
1836 int ret = mapping->a_ops->writepage(page, wbc);
1837 mapping_set_error(mapping, ret);
1838 return ret;
1839}
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849int generic_writepages(struct address_space *mapping,
1850 struct writeback_control *wbc)
1851{
1852 struct blk_plug plug;
1853 int ret;
1854
1855
1856 if (!mapping->a_ops->writepage)
1857 return 0;
1858
1859 blk_start_plug(&plug);
1860 ret = write_cache_pages(mapping, wbc, __writepage, mapping);
1861 blk_finish_plug(&plug);
1862 return ret;
1863}
1864
1865EXPORT_SYMBOL(generic_writepages);
1866
1867int do_writepages(struct address_space *mapping, struct writeback_control *wbc)
1868{
1869 int ret;
1870
1871 if (wbc->nr_to_write <= 0)
1872 return 0;
1873 if (mapping->a_ops->writepages)
1874 ret = mapping->a_ops->writepages(mapping, wbc);
1875 else
1876 ret = generic_writepages(mapping, wbc);
1877 return ret;
1878}
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889int write_one_page(struct page *page, int wait)
1890{
1891 struct address_space *mapping = page->mapping;
1892 int ret = 0;
1893 struct writeback_control wbc = {
1894 .sync_mode = WB_SYNC_ALL,
1895 .nr_to_write = 1,
1896 };
1897
1898 BUG_ON(!PageLocked(page));
1899
1900 if (wait)
1901 wait_on_page_writeback(page);
1902
1903 if (clear_page_dirty_for_io(page)) {
1904 page_cache_get(page);
1905 ret = mapping->a_ops->writepage(page, &wbc);
1906 if (ret == 0 && wait) {
1907 wait_on_page_writeback(page);
1908 if (PageError(page))
1909 ret = -EIO;
1910 }
1911 page_cache_release(page);
1912 } else {
1913 unlock_page(page);
1914 }
1915 return ret;
1916}
1917EXPORT_SYMBOL(write_one_page);
1918
1919
1920
1921
1922int __set_page_dirty_no_writeback(struct page *page)
1923{
1924 if (!PageDirty(page))
1925 return !TestSetPageDirty(page);
1926 return 0;
1927}
1928
1929
1930
1931
1932
1933void account_page_dirtied(struct page *page, struct address_space *mapping)
1934{
1935 if (mapping_cap_account_dirty(mapping)) {
1936 __inc_zone_page_state(page, NR_FILE_DIRTY);
1937 __inc_zone_page_state(page, NR_DIRTIED);
1938 __inc_bdi_stat(mapping->backing_dev_info, BDI_RECLAIMABLE);
1939 __inc_bdi_stat(mapping->backing_dev_info, BDI_DIRTIED);
1940 task_io_account_write(PAGE_CACHE_SIZE);
1941 current->nr_dirtied++;
1942 this_cpu_inc(bdp_ratelimits);
1943 }
1944}
1945EXPORT_SYMBOL(account_page_dirtied);
1946
1947
1948
1949
1950
1951
1952void account_page_writeback(struct page *page)
1953{
1954 inc_zone_page_state(page, NR_WRITEBACK);
1955}
1956EXPORT_SYMBOL(account_page_writeback);
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973int __set_page_dirty_nobuffers(struct page *page)
1974{
1975 if (!TestSetPageDirty(page)) {
1976 struct address_space *mapping = page_mapping(page);
1977 struct address_space *mapping2;
1978
1979 if (!mapping)
1980 return 1;
1981
1982 spin_lock_irq(&mapping->tree_lock);
1983 mapping2 = page_mapping(page);
1984 if (mapping2) {
1985 BUG_ON(mapping2 != mapping);
1986 WARN_ON_ONCE(!PagePrivate(page) && !PageUptodate(page));
1987 account_page_dirtied(page, mapping);
1988 radix_tree_tag_set(&mapping->page_tree,
1989 page_index(page), PAGECACHE_TAG_DIRTY);
1990 }
1991 spin_unlock_irq(&mapping->tree_lock);
1992 if (mapping->host) {
1993
1994 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
1995 }
1996 return 1;
1997 }
1998 return 0;
1999}
2000EXPORT_SYMBOL(__set_page_dirty_nobuffers);
2001
2002
2003
2004
2005
2006
2007
2008
2009void account_page_redirty(struct page *page)
2010{
2011 struct address_space *mapping = page->mapping;
2012 if (mapping && mapping_cap_account_dirty(mapping)) {
2013 current->nr_dirtied--;
2014 dec_zone_page_state(page, NR_DIRTIED);
2015 dec_bdi_stat(mapping->backing_dev_info, BDI_DIRTIED);
2016 }
2017}
2018EXPORT_SYMBOL(account_page_redirty);
2019
2020
2021
2022
2023
2024
2025int redirty_page_for_writepage(struct writeback_control *wbc, struct page *page)
2026{
2027 wbc->pages_skipped++;
2028 account_page_redirty(page);
2029 return __set_page_dirty_nobuffers(page);
2030}
2031EXPORT_SYMBOL(redirty_page_for_writepage);
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044int set_page_dirty(struct page *page)
2045{
2046 struct address_space *mapping = page_mapping(page);
2047
2048 if (likely(mapping)) {
2049 int (*spd)(struct page *) = mapping->a_ops->set_page_dirty;
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060 ClearPageReclaim(page);
2061#ifdef CONFIG_BLOCK
2062 if (!spd)
2063 spd = __set_page_dirty_buffers;
2064#endif
2065 return (*spd)(page);
2066 }
2067 if (!PageDirty(page)) {
2068 if (!TestSetPageDirty(page))
2069 return 1;
2070 }
2071 return 0;
2072}
2073EXPORT_SYMBOL(set_page_dirty);
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085int set_page_dirty_lock(struct page *page)
2086{
2087 int ret;
2088
2089 lock_page(page);
2090 ret = set_page_dirty(page);
2091 unlock_page(page);
2092 return ret;
2093}
2094EXPORT_SYMBOL(set_page_dirty_lock);
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110int clear_page_dirty_for_io(struct page *page)
2111{
2112 struct address_space *mapping = page_mapping(page);
2113
2114 BUG_ON(!PageLocked(page));
2115
2116 if (mapping && mapping_cap_account_dirty(mapping)) {
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142 if (page_mkclean(page))
2143 set_page_dirty(page);
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154 if (TestClearPageDirty(page)) {
2155 dec_zone_page_state(page, NR_FILE_DIRTY);
2156 dec_bdi_stat(mapping->backing_dev_info,
2157 BDI_RECLAIMABLE);
2158 return 1;
2159 }
2160 return 0;
2161 }
2162 return TestClearPageDirty(page);
2163}
2164EXPORT_SYMBOL(clear_page_dirty_for_io);
2165
2166int test_clear_page_writeback(struct page *page)
2167{
2168 struct address_space *mapping = page_mapping(page);
2169 int ret;
2170
2171 if (mapping) {
2172 struct backing_dev_info *bdi = mapping->backing_dev_info;
2173 unsigned long flags;
2174
2175 spin_lock_irqsave(&mapping->tree_lock, flags);
2176 ret = TestClearPageWriteback(page);
2177 if (ret) {
2178 radix_tree_tag_clear(&mapping->page_tree,
2179 page_index(page),
2180 PAGECACHE_TAG_WRITEBACK);
2181 if (bdi_cap_account_writeback(bdi)) {
2182 __dec_bdi_stat(bdi, BDI_WRITEBACK);
2183 __bdi_writeout_inc(bdi);
2184 }
2185 }
2186 spin_unlock_irqrestore(&mapping->tree_lock, flags);
2187 } else {
2188 ret = TestClearPageWriteback(page);
2189 }
2190 if (ret) {
2191 dec_zone_page_state(page, NR_WRITEBACK);
2192 inc_zone_page_state(page, NR_WRITTEN);
2193 }
2194 return ret;
2195}
2196
2197int test_set_page_writeback(struct page *page)
2198{
2199 struct address_space *mapping = page_mapping(page);
2200 int ret;
2201
2202 if (mapping) {
2203 struct backing_dev_info *bdi = mapping->backing_dev_info;
2204 unsigned long flags;
2205
2206 spin_lock_irqsave(&mapping->tree_lock, flags);
2207 ret = TestSetPageWriteback(page);
2208 if (!ret) {
2209 radix_tree_tag_set(&mapping->page_tree,
2210 page_index(page),
2211 PAGECACHE_TAG_WRITEBACK);
2212 if (bdi_cap_account_writeback(bdi))
2213 __inc_bdi_stat(bdi, BDI_WRITEBACK);
2214 }
2215 if (!PageDirty(page))
2216 radix_tree_tag_clear(&mapping->page_tree,
2217 page_index(page),
2218 PAGECACHE_TAG_DIRTY);
2219 radix_tree_tag_clear(&mapping->page_tree,
2220 page_index(page),
2221 PAGECACHE_TAG_TOWRITE);
2222 spin_unlock_irqrestore(&mapping->tree_lock, flags);
2223 } else {
2224 ret = TestSetPageWriteback(page);
2225 }
2226 if (!ret)
2227 account_page_writeback(page);
2228 return ret;
2229
2230}
2231EXPORT_SYMBOL(test_set_page_writeback);
2232
2233
2234
2235
2236
2237int mapping_tagged(struct address_space *mapping, int tag)
2238{
2239 return radix_tree_tagged(&mapping->page_tree, tag);
2240}
2241EXPORT_SYMBOL(mapping_tagged);
2242