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