1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * mm/page-writeback.c
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2002, Linus Torvalds.
6*4882a593Smuzhiyun * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * Contains functions related to writing back dirty pages at the
9*4882a593Smuzhiyun * address_space level.
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * 10Apr2002 Andrew Morton
12*4882a593Smuzhiyun * Initial version
13*4882a593Smuzhiyun */
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun #include <linux/kernel.h>
16*4882a593Smuzhiyun #include <linux/export.h>
17*4882a593Smuzhiyun #include <linux/spinlock.h>
18*4882a593Smuzhiyun #include <linux/fs.h>
19*4882a593Smuzhiyun #include <linux/mm.h>
20*4882a593Smuzhiyun #include <linux/swap.h>
21*4882a593Smuzhiyun #include <linux/slab.h>
22*4882a593Smuzhiyun #include <linux/pagemap.h>
23*4882a593Smuzhiyun #include <linux/writeback.h>
24*4882a593Smuzhiyun #include <linux/init.h>
25*4882a593Smuzhiyun #include <linux/backing-dev.h>
26*4882a593Smuzhiyun #include <linux/task_io_accounting_ops.h>
27*4882a593Smuzhiyun #include <linux/blkdev.h>
28*4882a593Smuzhiyun #include <linux/mpage.h>
29*4882a593Smuzhiyun #include <linux/rmap.h>
30*4882a593Smuzhiyun #include <linux/percpu.h>
31*4882a593Smuzhiyun #include <linux/smp.h>
32*4882a593Smuzhiyun #include <linux/sysctl.h>
33*4882a593Smuzhiyun #include <linux/cpu.h>
34*4882a593Smuzhiyun #include <linux/syscalls.h>
35*4882a593Smuzhiyun #include <linux/buffer_head.h> /* __set_page_dirty_buffers */
36*4882a593Smuzhiyun #include <linux/pagevec.h>
37*4882a593Smuzhiyun #include <linux/timer.h>
38*4882a593Smuzhiyun #include <linux/sched/rt.h>
39*4882a593Smuzhiyun #include <linux/sched/signal.h>
40*4882a593Smuzhiyun #include <linux/mm_inline.h>
41*4882a593Smuzhiyun #include <trace/events/writeback.h>
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun #include "internal.h"
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun #undef CREATE_TRACE_POINT
46*4882a593Smuzhiyun #include <trace/hooks/mm.h>
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun /*
49*4882a593Smuzhiyun * Sleep at most 200ms at a time in balance_dirty_pages().
50*4882a593Smuzhiyun */
51*4882a593Smuzhiyun #define MAX_PAUSE max(HZ/5, 1)
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun /*
54*4882a593Smuzhiyun * Try to keep balance_dirty_pages() call intervals higher than this many pages
55*4882a593Smuzhiyun * by raising pause time to max_pause when falls below it.
56*4882a593Smuzhiyun */
57*4882a593Smuzhiyun #define DIRTY_POLL_THRESH (128 >> (PAGE_SHIFT - 10))
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun /*
60*4882a593Smuzhiyun * Estimate write bandwidth at 200ms intervals.
61*4882a593Smuzhiyun */
62*4882a593Smuzhiyun #define BANDWIDTH_INTERVAL max(HZ/5, 1)
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun #define RATELIMIT_CALC_SHIFT 10
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun /*
67*4882a593Smuzhiyun * After a CPU has dirtied this many pages, balance_dirty_pages_ratelimited
68*4882a593Smuzhiyun * will look to see if it needs to force writeback or throttling.
69*4882a593Smuzhiyun */
70*4882a593Smuzhiyun static long ratelimit_pages = 32;
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun /* The following parameters are exported via /proc/sys/vm */
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun /*
75*4882a593Smuzhiyun * Start background writeback (via writeback threads) at this percentage
76*4882a593Smuzhiyun */
77*4882a593Smuzhiyun int dirty_background_ratio = 10;
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun /*
80*4882a593Smuzhiyun * dirty_background_bytes starts at 0 (disabled) so that it is a function of
81*4882a593Smuzhiyun * dirty_background_ratio * the amount of dirtyable memory
82*4882a593Smuzhiyun */
83*4882a593Smuzhiyun unsigned long dirty_background_bytes;
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun /*
86*4882a593Smuzhiyun * free highmem will not be subtracted from the total free memory
87*4882a593Smuzhiyun * for calculating free ratios if vm_highmem_is_dirtyable is true
88*4882a593Smuzhiyun */
89*4882a593Smuzhiyun int vm_highmem_is_dirtyable;
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun /*
92*4882a593Smuzhiyun * The generator of dirty data starts writeback at this percentage
93*4882a593Smuzhiyun */
94*4882a593Smuzhiyun int vm_dirty_ratio = 20;
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun /*
97*4882a593Smuzhiyun * vm_dirty_bytes starts at 0 (disabled) so that it is a function of
98*4882a593Smuzhiyun * vm_dirty_ratio * the amount of dirtyable memory
99*4882a593Smuzhiyun */
100*4882a593Smuzhiyun unsigned long vm_dirty_bytes;
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun /*
103*4882a593Smuzhiyun * The interval between `kupdate'-style writebacks
104*4882a593Smuzhiyun */
105*4882a593Smuzhiyun unsigned int dirty_writeback_interval = 5 * 100; /* centiseconds */
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dirty_writeback_interval);
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun /*
110*4882a593Smuzhiyun * The longest time for which data is allowed to remain dirty
111*4882a593Smuzhiyun */
112*4882a593Smuzhiyun unsigned int dirty_expire_interval = 30 * 100; /* centiseconds */
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun /*
115*4882a593Smuzhiyun * Flag that makes the machine dump writes/reads and block dirtyings.
116*4882a593Smuzhiyun */
117*4882a593Smuzhiyun int block_dump;
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun /*
120*4882a593Smuzhiyun * Flag that puts the machine in "laptop mode". Doubles as a timeout in jiffies:
121*4882a593Smuzhiyun * a full sync is triggered after this time elapses without any disk activity.
122*4882a593Smuzhiyun */
123*4882a593Smuzhiyun int laptop_mode;
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun EXPORT_SYMBOL(laptop_mode);
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun /* End of sysctl-exported parameters */
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun struct wb_domain global_wb_domain;
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun /* consolidated parameters for balance_dirty_pages() and its subroutines */
132*4882a593Smuzhiyun struct dirty_throttle_control {
133*4882a593Smuzhiyun #ifdef CONFIG_CGROUP_WRITEBACK
134*4882a593Smuzhiyun struct wb_domain *dom;
135*4882a593Smuzhiyun struct dirty_throttle_control *gdtc; /* only set in memcg dtc's */
136*4882a593Smuzhiyun #endif
137*4882a593Smuzhiyun struct bdi_writeback *wb;
138*4882a593Smuzhiyun struct fprop_local_percpu *wb_completions;
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun unsigned long avail; /* dirtyable */
141*4882a593Smuzhiyun unsigned long dirty; /* file_dirty + write + nfs */
142*4882a593Smuzhiyun unsigned long thresh; /* dirty threshold */
143*4882a593Smuzhiyun unsigned long bg_thresh; /* dirty background threshold */
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun unsigned long wb_dirty; /* per-wb counterparts */
146*4882a593Smuzhiyun unsigned long wb_thresh;
147*4882a593Smuzhiyun unsigned long wb_bg_thresh;
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun unsigned long pos_ratio;
150*4882a593Smuzhiyun };
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun /*
153*4882a593Smuzhiyun * Length of period for aging writeout fractions of bdis. This is an
154*4882a593Smuzhiyun * arbitrarily chosen number. The longer the period, the slower fractions will
155*4882a593Smuzhiyun * reflect changes in current writeout rate.
156*4882a593Smuzhiyun */
157*4882a593Smuzhiyun #define VM_COMPLETIONS_PERIOD_LEN (3*HZ)
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun #ifdef CONFIG_CGROUP_WRITEBACK
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun #define GDTC_INIT(__wb) .wb = (__wb), \
162*4882a593Smuzhiyun .dom = &global_wb_domain, \
163*4882a593Smuzhiyun .wb_completions = &(__wb)->completions
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun #define GDTC_INIT_NO_WB .dom = &global_wb_domain
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun #define MDTC_INIT(__wb, __gdtc) .wb = (__wb), \
168*4882a593Smuzhiyun .dom = mem_cgroup_wb_domain(__wb), \
169*4882a593Smuzhiyun .wb_completions = &(__wb)->memcg_completions, \
170*4882a593Smuzhiyun .gdtc = __gdtc
171*4882a593Smuzhiyun
mdtc_valid(struct dirty_throttle_control * dtc)172*4882a593Smuzhiyun static bool mdtc_valid(struct dirty_throttle_control *dtc)
173*4882a593Smuzhiyun {
174*4882a593Smuzhiyun return dtc->dom;
175*4882a593Smuzhiyun }
176*4882a593Smuzhiyun
dtc_dom(struct dirty_throttle_control * dtc)177*4882a593Smuzhiyun static struct wb_domain *dtc_dom(struct dirty_throttle_control *dtc)
178*4882a593Smuzhiyun {
179*4882a593Smuzhiyun return dtc->dom;
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun
mdtc_gdtc(struct dirty_throttle_control * mdtc)182*4882a593Smuzhiyun static struct dirty_throttle_control *mdtc_gdtc(struct dirty_throttle_control *mdtc)
183*4882a593Smuzhiyun {
184*4882a593Smuzhiyun return mdtc->gdtc;
185*4882a593Smuzhiyun }
186*4882a593Smuzhiyun
wb_memcg_completions(struct bdi_writeback * wb)187*4882a593Smuzhiyun static struct fprop_local_percpu *wb_memcg_completions(struct bdi_writeback *wb)
188*4882a593Smuzhiyun {
189*4882a593Smuzhiyun return &wb->memcg_completions;
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun
wb_min_max_ratio(struct bdi_writeback * wb,unsigned long * minp,unsigned long * maxp)192*4882a593Smuzhiyun static void wb_min_max_ratio(struct bdi_writeback *wb,
193*4882a593Smuzhiyun unsigned long *minp, unsigned long *maxp)
194*4882a593Smuzhiyun {
195*4882a593Smuzhiyun unsigned long this_bw = wb->avg_write_bandwidth;
196*4882a593Smuzhiyun unsigned long tot_bw = atomic_long_read(&wb->bdi->tot_write_bandwidth);
197*4882a593Smuzhiyun unsigned long long min = wb->bdi->min_ratio;
198*4882a593Smuzhiyun unsigned long long max = wb->bdi->max_ratio;
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun /*
201*4882a593Smuzhiyun * @wb may already be clean by the time control reaches here and
202*4882a593Smuzhiyun * the total may not include its bw.
203*4882a593Smuzhiyun */
204*4882a593Smuzhiyun if (this_bw < tot_bw) {
205*4882a593Smuzhiyun if (min) {
206*4882a593Smuzhiyun min *= this_bw;
207*4882a593Smuzhiyun min = div64_ul(min, tot_bw);
208*4882a593Smuzhiyun }
209*4882a593Smuzhiyun if (max < 100) {
210*4882a593Smuzhiyun max *= this_bw;
211*4882a593Smuzhiyun max = div64_ul(max, tot_bw);
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun *minp = min;
216*4882a593Smuzhiyun *maxp = max;
217*4882a593Smuzhiyun }
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun #else /* CONFIG_CGROUP_WRITEBACK */
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun #define GDTC_INIT(__wb) .wb = (__wb), \
222*4882a593Smuzhiyun .wb_completions = &(__wb)->completions
223*4882a593Smuzhiyun #define GDTC_INIT_NO_WB
224*4882a593Smuzhiyun #define MDTC_INIT(__wb, __gdtc)
225*4882a593Smuzhiyun
mdtc_valid(struct dirty_throttle_control * dtc)226*4882a593Smuzhiyun static bool mdtc_valid(struct dirty_throttle_control *dtc)
227*4882a593Smuzhiyun {
228*4882a593Smuzhiyun return false;
229*4882a593Smuzhiyun }
230*4882a593Smuzhiyun
dtc_dom(struct dirty_throttle_control * dtc)231*4882a593Smuzhiyun static struct wb_domain *dtc_dom(struct dirty_throttle_control *dtc)
232*4882a593Smuzhiyun {
233*4882a593Smuzhiyun return &global_wb_domain;
234*4882a593Smuzhiyun }
235*4882a593Smuzhiyun
mdtc_gdtc(struct dirty_throttle_control * mdtc)236*4882a593Smuzhiyun static struct dirty_throttle_control *mdtc_gdtc(struct dirty_throttle_control *mdtc)
237*4882a593Smuzhiyun {
238*4882a593Smuzhiyun return NULL;
239*4882a593Smuzhiyun }
240*4882a593Smuzhiyun
wb_memcg_completions(struct bdi_writeback * wb)241*4882a593Smuzhiyun static struct fprop_local_percpu *wb_memcg_completions(struct bdi_writeback *wb)
242*4882a593Smuzhiyun {
243*4882a593Smuzhiyun return NULL;
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun
wb_min_max_ratio(struct bdi_writeback * wb,unsigned long * minp,unsigned long * maxp)246*4882a593Smuzhiyun static void wb_min_max_ratio(struct bdi_writeback *wb,
247*4882a593Smuzhiyun unsigned long *minp, unsigned long *maxp)
248*4882a593Smuzhiyun {
249*4882a593Smuzhiyun *minp = wb->bdi->min_ratio;
250*4882a593Smuzhiyun *maxp = wb->bdi->max_ratio;
251*4882a593Smuzhiyun }
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun #endif /* CONFIG_CGROUP_WRITEBACK */
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun /*
256*4882a593Smuzhiyun * In a memory zone, there is a certain amount of pages we consider
257*4882a593Smuzhiyun * available for the page cache, which is essentially the number of
258*4882a593Smuzhiyun * free and reclaimable pages, minus some zone reserves to protect
259*4882a593Smuzhiyun * lowmem and the ability to uphold the zone's watermarks without
260*4882a593Smuzhiyun * requiring writeback.
261*4882a593Smuzhiyun *
262*4882a593Smuzhiyun * This number of dirtyable pages is the base value of which the
263*4882a593Smuzhiyun * user-configurable dirty ratio is the effective number of pages that
264*4882a593Smuzhiyun * are allowed to be actually dirtied. Per individual zone, or
265*4882a593Smuzhiyun * globally by using the sum of dirtyable pages over all zones.
266*4882a593Smuzhiyun *
267*4882a593Smuzhiyun * Because the user is allowed to specify the dirty limit globally as
268*4882a593Smuzhiyun * absolute number of bytes, calculating the per-zone dirty limit can
269*4882a593Smuzhiyun * require translating the configured limit into a percentage of
270*4882a593Smuzhiyun * global dirtyable memory first.
271*4882a593Smuzhiyun */
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun /**
274*4882a593Smuzhiyun * node_dirtyable_memory - number of dirtyable pages in a node
275*4882a593Smuzhiyun * @pgdat: the node
276*4882a593Smuzhiyun *
277*4882a593Smuzhiyun * Return: the node's number of pages potentially available for dirty
278*4882a593Smuzhiyun * page cache. This is the base value for the per-node dirty limits.
279*4882a593Smuzhiyun */
node_dirtyable_memory(struct pglist_data * pgdat)280*4882a593Smuzhiyun static unsigned long node_dirtyable_memory(struct pglist_data *pgdat)
281*4882a593Smuzhiyun {
282*4882a593Smuzhiyun unsigned long nr_pages = 0;
283*4882a593Smuzhiyun int z;
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun for (z = 0; z < MAX_NR_ZONES; z++) {
286*4882a593Smuzhiyun struct zone *zone = pgdat->node_zones + z;
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun if (!populated_zone(zone))
289*4882a593Smuzhiyun continue;
290*4882a593Smuzhiyun
291*4882a593Smuzhiyun nr_pages += zone_page_state(zone, NR_FREE_PAGES);
292*4882a593Smuzhiyun }
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun /*
295*4882a593Smuzhiyun * Pages reserved for the kernel should not be considered
296*4882a593Smuzhiyun * dirtyable, to prevent a situation where reclaim has to
297*4882a593Smuzhiyun * clean pages in order to balance the zones.
298*4882a593Smuzhiyun */
299*4882a593Smuzhiyun nr_pages -= min(nr_pages, pgdat->totalreserve_pages);
300*4882a593Smuzhiyun
301*4882a593Smuzhiyun nr_pages += node_page_state(pgdat, NR_INACTIVE_FILE);
302*4882a593Smuzhiyun nr_pages += node_page_state(pgdat, NR_ACTIVE_FILE);
303*4882a593Smuzhiyun
304*4882a593Smuzhiyun return nr_pages;
305*4882a593Smuzhiyun }
306*4882a593Smuzhiyun
highmem_dirtyable_memory(unsigned long total)307*4882a593Smuzhiyun static unsigned long highmem_dirtyable_memory(unsigned long total)
308*4882a593Smuzhiyun {
309*4882a593Smuzhiyun #ifdef CONFIG_HIGHMEM
310*4882a593Smuzhiyun int node;
311*4882a593Smuzhiyun unsigned long x = 0;
312*4882a593Smuzhiyun int i;
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun for_each_node_state(node, N_HIGH_MEMORY) {
315*4882a593Smuzhiyun for (i = ZONE_NORMAL + 1; i < MAX_NR_ZONES; i++) {
316*4882a593Smuzhiyun struct zone *z;
317*4882a593Smuzhiyun unsigned long nr_pages;
318*4882a593Smuzhiyun
319*4882a593Smuzhiyun if (!is_highmem_idx(i))
320*4882a593Smuzhiyun continue;
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun z = &NODE_DATA(node)->node_zones[i];
323*4882a593Smuzhiyun if (!populated_zone(z))
324*4882a593Smuzhiyun continue;
325*4882a593Smuzhiyun
326*4882a593Smuzhiyun nr_pages = zone_page_state(z, NR_FREE_PAGES);
327*4882a593Smuzhiyun /* watch for underflows */
328*4882a593Smuzhiyun nr_pages -= min(nr_pages, high_wmark_pages(z));
329*4882a593Smuzhiyun nr_pages += zone_page_state(z, NR_ZONE_INACTIVE_FILE);
330*4882a593Smuzhiyun nr_pages += zone_page_state(z, NR_ZONE_ACTIVE_FILE);
331*4882a593Smuzhiyun x += nr_pages;
332*4882a593Smuzhiyun }
333*4882a593Smuzhiyun }
334*4882a593Smuzhiyun
335*4882a593Smuzhiyun /*
336*4882a593Smuzhiyun * Unreclaimable memory (kernel memory or anonymous memory
337*4882a593Smuzhiyun * without swap) can bring down the dirtyable pages below
338*4882a593Smuzhiyun * the zone's dirty balance reserve and the above calculation
339*4882a593Smuzhiyun * will underflow. However we still want to add in nodes
340*4882a593Smuzhiyun * which are below threshold (negative values) to get a more
341*4882a593Smuzhiyun * accurate calculation but make sure that the total never
342*4882a593Smuzhiyun * underflows.
343*4882a593Smuzhiyun */
344*4882a593Smuzhiyun if ((long)x < 0)
345*4882a593Smuzhiyun x = 0;
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun /*
348*4882a593Smuzhiyun * Make sure that the number of highmem pages is never larger
349*4882a593Smuzhiyun * than the number of the total dirtyable memory. This can only
350*4882a593Smuzhiyun * occur in very strange VM situations but we want to make sure
351*4882a593Smuzhiyun * that this does not occur.
352*4882a593Smuzhiyun */
353*4882a593Smuzhiyun return min(x, total);
354*4882a593Smuzhiyun #else
355*4882a593Smuzhiyun return 0;
356*4882a593Smuzhiyun #endif
357*4882a593Smuzhiyun }
358*4882a593Smuzhiyun
359*4882a593Smuzhiyun /**
360*4882a593Smuzhiyun * global_dirtyable_memory - number of globally dirtyable pages
361*4882a593Smuzhiyun *
362*4882a593Smuzhiyun * Return: the global number of pages potentially available for dirty
363*4882a593Smuzhiyun * page cache. This is the base value for the global dirty limits.
364*4882a593Smuzhiyun */
global_dirtyable_memory(void)365*4882a593Smuzhiyun static unsigned long global_dirtyable_memory(void)
366*4882a593Smuzhiyun {
367*4882a593Smuzhiyun unsigned long x;
368*4882a593Smuzhiyun
369*4882a593Smuzhiyun x = global_zone_page_state(NR_FREE_PAGES);
370*4882a593Smuzhiyun /*
371*4882a593Smuzhiyun * Pages reserved for the kernel should not be considered
372*4882a593Smuzhiyun * dirtyable, to prevent a situation where reclaim has to
373*4882a593Smuzhiyun * clean pages in order to balance the zones.
374*4882a593Smuzhiyun */
375*4882a593Smuzhiyun x -= min(x, totalreserve_pages);
376*4882a593Smuzhiyun
377*4882a593Smuzhiyun x += global_node_page_state(NR_INACTIVE_FILE);
378*4882a593Smuzhiyun x += global_node_page_state(NR_ACTIVE_FILE);
379*4882a593Smuzhiyun
380*4882a593Smuzhiyun if (!vm_highmem_is_dirtyable)
381*4882a593Smuzhiyun x -= highmem_dirtyable_memory(x);
382*4882a593Smuzhiyun
383*4882a593Smuzhiyun return x + 1; /* Ensure that we never return 0 */
384*4882a593Smuzhiyun }
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun /**
387*4882a593Smuzhiyun * domain_dirty_limits - calculate thresh and bg_thresh for a wb_domain
388*4882a593Smuzhiyun * @dtc: dirty_throttle_control of interest
389*4882a593Smuzhiyun *
390*4882a593Smuzhiyun * Calculate @dtc->thresh and ->bg_thresh considering
391*4882a593Smuzhiyun * vm_dirty_{bytes|ratio} and dirty_background_{bytes|ratio}. The caller
392*4882a593Smuzhiyun * must ensure that @dtc->avail is set before calling this function. The
393*4882a593Smuzhiyun * dirty limits will be lifted by 1/4 for real-time tasks.
394*4882a593Smuzhiyun */
domain_dirty_limits(struct dirty_throttle_control * dtc)395*4882a593Smuzhiyun static void domain_dirty_limits(struct dirty_throttle_control *dtc)
396*4882a593Smuzhiyun {
397*4882a593Smuzhiyun const unsigned long available_memory = dtc->avail;
398*4882a593Smuzhiyun struct dirty_throttle_control *gdtc = mdtc_gdtc(dtc);
399*4882a593Smuzhiyun unsigned long bytes = vm_dirty_bytes;
400*4882a593Smuzhiyun unsigned long bg_bytes = dirty_background_bytes;
401*4882a593Smuzhiyun /* convert ratios to per-PAGE_SIZE for higher precision */
402*4882a593Smuzhiyun unsigned long ratio = (vm_dirty_ratio * PAGE_SIZE) / 100;
403*4882a593Smuzhiyun unsigned long bg_ratio = (dirty_background_ratio * PAGE_SIZE) / 100;
404*4882a593Smuzhiyun unsigned long thresh;
405*4882a593Smuzhiyun unsigned long bg_thresh;
406*4882a593Smuzhiyun struct task_struct *tsk;
407*4882a593Smuzhiyun
408*4882a593Smuzhiyun /* gdtc is !NULL iff @dtc is for memcg domain */
409*4882a593Smuzhiyun if (gdtc) {
410*4882a593Smuzhiyun unsigned long global_avail = gdtc->avail;
411*4882a593Smuzhiyun
412*4882a593Smuzhiyun /*
413*4882a593Smuzhiyun * The byte settings can't be applied directly to memcg
414*4882a593Smuzhiyun * domains. Convert them to ratios by scaling against
415*4882a593Smuzhiyun * globally available memory. As the ratios are in
416*4882a593Smuzhiyun * per-PAGE_SIZE, they can be obtained by dividing bytes by
417*4882a593Smuzhiyun * number of pages.
418*4882a593Smuzhiyun */
419*4882a593Smuzhiyun if (bytes)
420*4882a593Smuzhiyun ratio = min(DIV_ROUND_UP(bytes, global_avail),
421*4882a593Smuzhiyun PAGE_SIZE);
422*4882a593Smuzhiyun if (bg_bytes)
423*4882a593Smuzhiyun bg_ratio = min(DIV_ROUND_UP(bg_bytes, global_avail),
424*4882a593Smuzhiyun PAGE_SIZE);
425*4882a593Smuzhiyun bytes = bg_bytes = 0;
426*4882a593Smuzhiyun }
427*4882a593Smuzhiyun
428*4882a593Smuzhiyun if (bytes)
429*4882a593Smuzhiyun thresh = DIV_ROUND_UP(bytes, PAGE_SIZE);
430*4882a593Smuzhiyun else
431*4882a593Smuzhiyun thresh = (ratio * available_memory) / PAGE_SIZE;
432*4882a593Smuzhiyun
433*4882a593Smuzhiyun if (bg_bytes)
434*4882a593Smuzhiyun bg_thresh = DIV_ROUND_UP(bg_bytes, PAGE_SIZE);
435*4882a593Smuzhiyun else
436*4882a593Smuzhiyun bg_thresh = (bg_ratio * available_memory) / PAGE_SIZE;
437*4882a593Smuzhiyun
438*4882a593Smuzhiyun if (bg_thresh >= thresh)
439*4882a593Smuzhiyun bg_thresh = thresh / 2;
440*4882a593Smuzhiyun tsk = current;
441*4882a593Smuzhiyun if (rt_task(tsk)) {
442*4882a593Smuzhiyun bg_thresh += bg_thresh / 4 + global_wb_domain.dirty_limit / 32;
443*4882a593Smuzhiyun thresh += thresh / 4 + global_wb_domain.dirty_limit / 32;
444*4882a593Smuzhiyun }
445*4882a593Smuzhiyun dtc->thresh = thresh;
446*4882a593Smuzhiyun dtc->bg_thresh = bg_thresh;
447*4882a593Smuzhiyun
448*4882a593Smuzhiyun /* we should eventually report the domain in the TP */
449*4882a593Smuzhiyun if (!gdtc)
450*4882a593Smuzhiyun trace_global_dirty_state(bg_thresh, thresh);
451*4882a593Smuzhiyun }
452*4882a593Smuzhiyun
453*4882a593Smuzhiyun /**
454*4882a593Smuzhiyun * global_dirty_limits - background-writeback and dirty-throttling thresholds
455*4882a593Smuzhiyun * @pbackground: out parameter for bg_thresh
456*4882a593Smuzhiyun * @pdirty: out parameter for thresh
457*4882a593Smuzhiyun *
458*4882a593Smuzhiyun * Calculate bg_thresh and thresh for global_wb_domain. See
459*4882a593Smuzhiyun * domain_dirty_limits() for details.
460*4882a593Smuzhiyun */
global_dirty_limits(unsigned long * pbackground,unsigned long * pdirty)461*4882a593Smuzhiyun void global_dirty_limits(unsigned long *pbackground, unsigned long *pdirty)
462*4882a593Smuzhiyun {
463*4882a593Smuzhiyun struct dirty_throttle_control gdtc = { GDTC_INIT_NO_WB };
464*4882a593Smuzhiyun
465*4882a593Smuzhiyun gdtc.avail = global_dirtyable_memory();
466*4882a593Smuzhiyun domain_dirty_limits(&gdtc);
467*4882a593Smuzhiyun
468*4882a593Smuzhiyun *pbackground = gdtc.bg_thresh;
469*4882a593Smuzhiyun *pdirty = gdtc.thresh;
470*4882a593Smuzhiyun }
471*4882a593Smuzhiyun
472*4882a593Smuzhiyun /**
473*4882a593Smuzhiyun * node_dirty_limit - maximum number of dirty pages allowed in a node
474*4882a593Smuzhiyun * @pgdat: the node
475*4882a593Smuzhiyun *
476*4882a593Smuzhiyun * Return: the maximum number of dirty pages allowed in a node, based
477*4882a593Smuzhiyun * on the node's dirtyable memory.
478*4882a593Smuzhiyun */
node_dirty_limit(struct pglist_data * pgdat)479*4882a593Smuzhiyun static unsigned long node_dirty_limit(struct pglist_data *pgdat)
480*4882a593Smuzhiyun {
481*4882a593Smuzhiyun unsigned long node_memory = node_dirtyable_memory(pgdat);
482*4882a593Smuzhiyun struct task_struct *tsk = current;
483*4882a593Smuzhiyun unsigned long dirty;
484*4882a593Smuzhiyun
485*4882a593Smuzhiyun if (vm_dirty_bytes)
486*4882a593Smuzhiyun dirty = DIV_ROUND_UP(vm_dirty_bytes, PAGE_SIZE) *
487*4882a593Smuzhiyun node_memory / global_dirtyable_memory();
488*4882a593Smuzhiyun else
489*4882a593Smuzhiyun dirty = vm_dirty_ratio * node_memory / 100;
490*4882a593Smuzhiyun
491*4882a593Smuzhiyun if (rt_task(tsk))
492*4882a593Smuzhiyun dirty += dirty / 4;
493*4882a593Smuzhiyun
494*4882a593Smuzhiyun return dirty;
495*4882a593Smuzhiyun }
496*4882a593Smuzhiyun
497*4882a593Smuzhiyun /**
498*4882a593Smuzhiyun * node_dirty_ok - tells whether a node is within its dirty limits
499*4882a593Smuzhiyun * @pgdat: the node to check
500*4882a593Smuzhiyun *
501*4882a593Smuzhiyun * Return: %true when the dirty pages in @pgdat are within the node's
502*4882a593Smuzhiyun * dirty limit, %false if the limit is exceeded.
503*4882a593Smuzhiyun */
node_dirty_ok(struct pglist_data * pgdat)504*4882a593Smuzhiyun bool node_dirty_ok(struct pglist_data *pgdat)
505*4882a593Smuzhiyun {
506*4882a593Smuzhiyun unsigned long limit = node_dirty_limit(pgdat);
507*4882a593Smuzhiyun unsigned long nr_pages = 0;
508*4882a593Smuzhiyun
509*4882a593Smuzhiyun nr_pages += node_page_state(pgdat, NR_FILE_DIRTY);
510*4882a593Smuzhiyun nr_pages += node_page_state(pgdat, NR_WRITEBACK);
511*4882a593Smuzhiyun
512*4882a593Smuzhiyun return nr_pages <= limit;
513*4882a593Smuzhiyun }
514*4882a593Smuzhiyun
dirty_background_ratio_handler(struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)515*4882a593Smuzhiyun int dirty_background_ratio_handler(struct ctl_table *table, int write,
516*4882a593Smuzhiyun void *buffer, size_t *lenp, loff_t *ppos)
517*4882a593Smuzhiyun {
518*4882a593Smuzhiyun int ret;
519*4882a593Smuzhiyun
520*4882a593Smuzhiyun ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
521*4882a593Smuzhiyun if (ret == 0 && write)
522*4882a593Smuzhiyun dirty_background_bytes = 0;
523*4882a593Smuzhiyun return ret;
524*4882a593Smuzhiyun }
525*4882a593Smuzhiyun
dirty_background_bytes_handler(struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)526*4882a593Smuzhiyun int dirty_background_bytes_handler(struct ctl_table *table, int write,
527*4882a593Smuzhiyun void *buffer, size_t *lenp, loff_t *ppos)
528*4882a593Smuzhiyun {
529*4882a593Smuzhiyun int ret;
530*4882a593Smuzhiyun
531*4882a593Smuzhiyun ret = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
532*4882a593Smuzhiyun if (ret == 0 && write)
533*4882a593Smuzhiyun dirty_background_ratio = 0;
534*4882a593Smuzhiyun return ret;
535*4882a593Smuzhiyun }
536*4882a593Smuzhiyun
dirty_ratio_handler(struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)537*4882a593Smuzhiyun int dirty_ratio_handler(struct ctl_table *table, int write, void *buffer,
538*4882a593Smuzhiyun size_t *lenp, loff_t *ppos)
539*4882a593Smuzhiyun {
540*4882a593Smuzhiyun int old_ratio = vm_dirty_ratio;
541*4882a593Smuzhiyun int ret;
542*4882a593Smuzhiyun
543*4882a593Smuzhiyun ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
544*4882a593Smuzhiyun if (ret == 0 && write && vm_dirty_ratio != old_ratio) {
545*4882a593Smuzhiyun writeback_set_ratelimit();
546*4882a593Smuzhiyun vm_dirty_bytes = 0;
547*4882a593Smuzhiyun }
548*4882a593Smuzhiyun return ret;
549*4882a593Smuzhiyun }
550*4882a593Smuzhiyun
dirty_bytes_handler(struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)551*4882a593Smuzhiyun int dirty_bytes_handler(struct ctl_table *table, int write,
552*4882a593Smuzhiyun void *buffer, size_t *lenp, loff_t *ppos)
553*4882a593Smuzhiyun {
554*4882a593Smuzhiyun unsigned long old_bytes = vm_dirty_bytes;
555*4882a593Smuzhiyun int ret;
556*4882a593Smuzhiyun
557*4882a593Smuzhiyun ret = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
558*4882a593Smuzhiyun if (ret == 0 && write && vm_dirty_bytes != old_bytes) {
559*4882a593Smuzhiyun writeback_set_ratelimit();
560*4882a593Smuzhiyun vm_dirty_ratio = 0;
561*4882a593Smuzhiyun }
562*4882a593Smuzhiyun return ret;
563*4882a593Smuzhiyun }
564*4882a593Smuzhiyun
wp_next_time(unsigned long cur_time)565*4882a593Smuzhiyun static unsigned long wp_next_time(unsigned long cur_time)
566*4882a593Smuzhiyun {
567*4882a593Smuzhiyun cur_time += VM_COMPLETIONS_PERIOD_LEN;
568*4882a593Smuzhiyun /* 0 has a special meaning... */
569*4882a593Smuzhiyun if (!cur_time)
570*4882a593Smuzhiyun return 1;
571*4882a593Smuzhiyun return cur_time;
572*4882a593Smuzhiyun }
573*4882a593Smuzhiyun
wb_domain_writeout_inc(struct wb_domain * dom,struct fprop_local_percpu * completions,unsigned int max_prop_frac)574*4882a593Smuzhiyun static void wb_domain_writeout_inc(struct wb_domain *dom,
575*4882a593Smuzhiyun struct fprop_local_percpu *completions,
576*4882a593Smuzhiyun unsigned int max_prop_frac)
577*4882a593Smuzhiyun {
578*4882a593Smuzhiyun __fprop_inc_percpu_max(&dom->completions, completions,
579*4882a593Smuzhiyun max_prop_frac);
580*4882a593Smuzhiyun /* First event after period switching was turned off? */
581*4882a593Smuzhiyun if (unlikely(!dom->period_time)) {
582*4882a593Smuzhiyun /*
583*4882a593Smuzhiyun * We can race with other __bdi_writeout_inc calls here but
584*4882a593Smuzhiyun * it does not cause any harm since the resulting time when
585*4882a593Smuzhiyun * timer will fire and what is in writeout_period_time will be
586*4882a593Smuzhiyun * roughly the same.
587*4882a593Smuzhiyun */
588*4882a593Smuzhiyun dom->period_time = wp_next_time(jiffies);
589*4882a593Smuzhiyun mod_timer(&dom->period_timer, dom->period_time);
590*4882a593Smuzhiyun }
591*4882a593Smuzhiyun }
592*4882a593Smuzhiyun
593*4882a593Smuzhiyun /*
594*4882a593Smuzhiyun * Increment @wb's writeout completion count and the global writeout
595*4882a593Smuzhiyun * completion count. Called from test_clear_page_writeback().
596*4882a593Smuzhiyun */
__wb_writeout_inc(struct bdi_writeback * wb)597*4882a593Smuzhiyun static inline void __wb_writeout_inc(struct bdi_writeback *wb)
598*4882a593Smuzhiyun {
599*4882a593Smuzhiyun struct wb_domain *cgdom;
600*4882a593Smuzhiyun
601*4882a593Smuzhiyun inc_wb_stat(wb, WB_WRITTEN);
602*4882a593Smuzhiyun wb_domain_writeout_inc(&global_wb_domain, &wb->completions,
603*4882a593Smuzhiyun wb->bdi->max_prop_frac);
604*4882a593Smuzhiyun
605*4882a593Smuzhiyun cgdom = mem_cgroup_wb_domain(wb);
606*4882a593Smuzhiyun if (cgdom)
607*4882a593Smuzhiyun wb_domain_writeout_inc(cgdom, wb_memcg_completions(wb),
608*4882a593Smuzhiyun wb->bdi->max_prop_frac);
609*4882a593Smuzhiyun }
610*4882a593Smuzhiyun
wb_writeout_inc(struct bdi_writeback * wb)611*4882a593Smuzhiyun void wb_writeout_inc(struct bdi_writeback *wb)
612*4882a593Smuzhiyun {
613*4882a593Smuzhiyun unsigned long flags;
614*4882a593Smuzhiyun
615*4882a593Smuzhiyun local_irq_save(flags);
616*4882a593Smuzhiyun __wb_writeout_inc(wb);
617*4882a593Smuzhiyun local_irq_restore(flags);
618*4882a593Smuzhiyun }
619*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(wb_writeout_inc);
620*4882a593Smuzhiyun
621*4882a593Smuzhiyun /*
622*4882a593Smuzhiyun * On idle system, we can be called long after we scheduled because we use
623*4882a593Smuzhiyun * deferred timers so count with missed periods.
624*4882a593Smuzhiyun */
writeout_period(struct timer_list * t)625*4882a593Smuzhiyun static void writeout_period(struct timer_list *t)
626*4882a593Smuzhiyun {
627*4882a593Smuzhiyun struct wb_domain *dom = from_timer(dom, t, period_timer);
628*4882a593Smuzhiyun int miss_periods = (jiffies - dom->period_time) /
629*4882a593Smuzhiyun VM_COMPLETIONS_PERIOD_LEN;
630*4882a593Smuzhiyun
631*4882a593Smuzhiyun if (fprop_new_period(&dom->completions, miss_periods + 1)) {
632*4882a593Smuzhiyun dom->period_time = wp_next_time(dom->period_time +
633*4882a593Smuzhiyun miss_periods * VM_COMPLETIONS_PERIOD_LEN);
634*4882a593Smuzhiyun mod_timer(&dom->period_timer, dom->period_time);
635*4882a593Smuzhiyun } else {
636*4882a593Smuzhiyun /*
637*4882a593Smuzhiyun * Aging has zeroed all fractions. Stop wasting CPU on period
638*4882a593Smuzhiyun * updates.
639*4882a593Smuzhiyun */
640*4882a593Smuzhiyun dom->period_time = 0;
641*4882a593Smuzhiyun }
642*4882a593Smuzhiyun }
643*4882a593Smuzhiyun
wb_domain_init(struct wb_domain * dom,gfp_t gfp)644*4882a593Smuzhiyun int wb_domain_init(struct wb_domain *dom, gfp_t gfp)
645*4882a593Smuzhiyun {
646*4882a593Smuzhiyun memset(dom, 0, sizeof(*dom));
647*4882a593Smuzhiyun
648*4882a593Smuzhiyun spin_lock_init(&dom->lock);
649*4882a593Smuzhiyun
650*4882a593Smuzhiyun timer_setup(&dom->period_timer, writeout_period, TIMER_DEFERRABLE);
651*4882a593Smuzhiyun
652*4882a593Smuzhiyun dom->dirty_limit_tstamp = jiffies;
653*4882a593Smuzhiyun
654*4882a593Smuzhiyun return fprop_global_init(&dom->completions, gfp);
655*4882a593Smuzhiyun }
656*4882a593Smuzhiyun
657*4882a593Smuzhiyun #ifdef CONFIG_CGROUP_WRITEBACK
wb_domain_exit(struct wb_domain * dom)658*4882a593Smuzhiyun void wb_domain_exit(struct wb_domain *dom)
659*4882a593Smuzhiyun {
660*4882a593Smuzhiyun del_timer_sync(&dom->period_timer);
661*4882a593Smuzhiyun fprop_global_destroy(&dom->completions);
662*4882a593Smuzhiyun }
663*4882a593Smuzhiyun #endif
664*4882a593Smuzhiyun
665*4882a593Smuzhiyun /*
666*4882a593Smuzhiyun * bdi_min_ratio keeps the sum of the minimum dirty shares of all
667*4882a593Smuzhiyun * registered backing devices, which, for obvious reasons, can not
668*4882a593Smuzhiyun * exceed 100%.
669*4882a593Smuzhiyun */
670*4882a593Smuzhiyun static unsigned int bdi_min_ratio;
671*4882a593Smuzhiyun
bdi_set_min_ratio(struct backing_dev_info * bdi,unsigned int min_ratio)672*4882a593Smuzhiyun int bdi_set_min_ratio(struct backing_dev_info *bdi, unsigned int min_ratio)
673*4882a593Smuzhiyun {
674*4882a593Smuzhiyun int ret = 0;
675*4882a593Smuzhiyun
676*4882a593Smuzhiyun spin_lock_bh(&bdi_lock);
677*4882a593Smuzhiyun if (min_ratio > bdi->max_ratio) {
678*4882a593Smuzhiyun ret = -EINVAL;
679*4882a593Smuzhiyun } else {
680*4882a593Smuzhiyun min_ratio -= bdi->min_ratio;
681*4882a593Smuzhiyun if (bdi_min_ratio + min_ratio < 100) {
682*4882a593Smuzhiyun bdi_min_ratio += min_ratio;
683*4882a593Smuzhiyun bdi->min_ratio += min_ratio;
684*4882a593Smuzhiyun } else {
685*4882a593Smuzhiyun ret = -EINVAL;
686*4882a593Smuzhiyun }
687*4882a593Smuzhiyun }
688*4882a593Smuzhiyun spin_unlock_bh(&bdi_lock);
689*4882a593Smuzhiyun
690*4882a593Smuzhiyun return ret;
691*4882a593Smuzhiyun }
692*4882a593Smuzhiyun
bdi_set_max_ratio(struct backing_dev_info * bdi,unsigned max_ratio)693*4882a593Smuzhiyun int bdi_set_max_ratio(struct backing_dev_info *bdi, unsigned max_ratio)
694*4882a593Smuzhiyun {
695*4882a593Smuzhiyun int ret = 0;
696*4882a593Smuzhiyun
697*4882a593Smuzhiyun if (max_ratio > 100)
698*4882a593Smuzhiyun return -EINVAL;
699*4882a593Smuzhiyun
700*4882a593Smuzhiyun spin_lock_bh(&bdi_lock);
701*4882a593Smuzhiyun if (bdi->min_ratio > max_ratio) {
702*4882a593Smuzhiyun ret = -EINVAL;
703*4882a593Smuzhiyun } else {
704*4882a593Smuzhiyun bdi->max_ratio = max_ratio;
705*4882a593Smuzhiyun bdi->max_prop_frac = (FPROP_FRAC_BASE * max_ratio) / 100;
706*4882a593Smuzhiyun }
707*4882a593Smuzhiyun spin_unlock_bh(&bdi_lock);
708*4882a593Smuzhiyun
709*4882a593Smuzhiyun return ret;
710*4882a593Smuzhiyun }
711*4882a593Smuzhiyun EXPORT_SYMBOL(bdi_set_max_ratio);
712*4882a593Smuzhiyun
dirty_freerun_ceiling(unsigned long thresh,unsigned long bg_thresh)713*4882a593Smuzhiyun static unsigned long dirty_freerun_ceiling(unsigned long thresh,
714*4882a593Smuzhiyun unsigned long bg_thresh)
715*4882a593Smuzhiyun {
716*4882a593Smuzhiyun return (thresh + bg_thresh) / 2;
717*4882a593Smuzhiyun }
718*4882a593Smuzhiyun
hard_dirty_limit(struct wb_domain * dom,unsigned long thresh)719*4882a593Smuzhiyun static unsigned long hard_dirty_limit(struct wb_domain *dom,
720*4882a593Smuzhiyun unsigned long thresh)
721*4882a593Smuzhiyun {
722*4882a593Smuzhiyun return max(thresh, dom->dirty_limit);
723*4882a593Smuzhiyun }
724*4882a593Smuzhiyun
725*4882a593Smuzhiyun /*
726*4882a593Smuzhiyun * Memory which can be further allocated to a memcg domain is capped by
727*4882a593Smuzhiyun * system-wide clean memory excluding the amount being used in the domain.
728*4882a593Smuzhiyun */
mdtc_calc_avail(struct dirty_throttle_control * mdtc,unsigned long filepages,unsigned long headroom)729*4882a593Smuzhiyun static void mdtc_calc_avail(struct dirty_throttle_control *mdtc,
730*4882a593Smuzhiyun unsigned long filepages, unsigned long headroom)
731*4882a593Smuzhiyun {
732*4882a593Smuzhiyun struct dirty_throttle_control *gdtc = mdtc_gdtc(mdtc);
733*4882a593Smuzhiyun unsigned long clean = filepages - min(filepages, mdtc->dirty);
734*4882a593Smuzhiyun unsigned long global_clean = gdtc->avail - min(gdtc->avail, gdtc->dirty);
735*4882a593Smuzhiyun unsigned long other_clean = global_clean - min(global_clean, clean);
736*4882a593Smuzhiyun
737*4882a593Smuzhiyun mdtc->avail = filepages + min(headroom, other_clean);
738*4882a593Smuzhiyun }
739*4882a593Smuzhiyun
740*4882a593Smuzhiyun /**
741*4882a593Smuzhiyun * __wb_calc_thresh - @wb's share of dirty throttling threshold
742*4882a593Smuzhiyun * @dtc: dirty_throttle_context of interest
743*4882a593Smuzhiyun *
744*4882a593Smuzhiyun * Note that balance_dirty_pages() will only seriously take it as a hard limit
745*4882a593Smuzhiyun * when sleeping max_pause per page is not enough to keep the dirty pages under
746*4882a593Smuzhiyun * control. For example, when the device is completely stalled due to some error
747*4882a593Smuzhiyun * conditions, or when there are 1000 dd tasks writing to a slow 10MB/s USB key.
748*4882a593Smuzhiyun * In the other normal situations, it acts more gently by throttling the tasks
749*4882a593Smuzhiyun * more (rather than completely block them) when the wb dirty pages go high.
750*4882a593Smuzhiyun *
751*4882a593Smuzhiyun * It allocates high/low dirty limits to fast/slow devices, in order to prevent
752*4882a593Smuzhiyun * - starving fast devices
753*4882a593Smuzhiyun * - piling up dirty pages (that will take long time to sync) on slow devices
754*4882a593Smuzhiyun *
755*4882a593Smuzhiyun * The wb's share of dirty limit will be adapting to its throughput and
756*4882a593Smuzhiyun * bounded by the bdi->min_ratio and/or bdi->max_ratio parameters, if set.
757*4882a593Smuzhiyun *
758*4882a593Smuzhiyun * Return: @wb's dirty limit in pages. The term "dirty" in the context of
759*4882a593Smuzhiyun * dirty balancing includes all PG_dirty and PG_writeback pages.
760*4882a593Smuzhiyun */
__wb_calc_thresh(struct dirty_throttle_control * dtc)761*4882a593Smuzhiyun static unsigned long __wb_calc_thresh(struct dirty_throttle_control *dtc)
762*4882a593Smuzhiyun {
763*4882a593Smuzhiyun struct wb_domain *dom = dtc_dom(dtc);
764*4882a593Smuzhiyun unsigned long thresh = dtc->thresh;
765*4882a593Smuzhiyun u64 wb_thresh;
766*4882a593Smuzhiyun unsigned long numerator, denominator;
767*4882a593Smuzhiyun unsigned long wb_min_ratio, wb_max_ratio;
768*4882a593Smuzhiyun
769*4882a593Smuzhiyun /*
770*4882a593Smuzhiyun * Calculate this BDI's share of the thresh ratio.
771*4882a593Smuzhiyun */
772*4882a593Smuzhiyun fprop_fraction_percpu(&dom->completions, dtc->wb_completions,
773*4882a593Smuzhiyun &numerator, &denominator);
774*4882a593Smuzhiyun
775*4882a593Smuzhiyun wb_thresh = (thresh * (100 - bdi_min_ratio)) / 100;
776*4882a593Smuzhiyun wb_thresh *= numerator;
777*4882a593Smuzhiyun wb_thresh = div64_ul(wb_thresh, denominator);
778*4882a593Smuzhiyun
779*4882a593Smuzhiyun wb_min_max_ratio(dtc->wb, &wb_min_ratio, &wb_max_ratio);
780*4882a593Smuzhiyun
781*4882a593Smuzhiyun wb_thresh += (thresh * wb_min_ratio) / 100;
782*4882a593Smuzhiyun if (wb_thresh > (thresh * wb_max_ratio) / 100)
783*4882a593Smuzhiyun wb_thresh = thresh * wb_max_ratio / 100;
784*4882a593Smuzhiyun
785*4882a593Smuzhiyun return wb_thresh;
786*4882a593Smuzhiyun }
787*4882a593Smuzhiyun
wb_calc_thresh(struct bdi_writeback * wb,unsigned long thresh)788*4882a593Smuzhiyun unsigned long wb_calc_thresh(struct bdi_writeback *wb, unsigned long thresh)
789*4882a593Smuzhiyun {
790*4882a593Smuzhiyun struct dirty_throttle_control gdtc = { GDTC_INIT(wb),
791*4882a593Smuzhiyun .thresh = thresh };
792*4882a593Smuzhiyun return __wb_calc_thresh(&gdtc);
793*4882a593Smuzhiyun }
794*4882a593Smuzhiyun
795*4882a593Smuzhiyun /*
796*4882a593Smuzhiyun * setpoint - dirty 3
797*4882a593Smuzhiyun * f(dirty) := 1.0 + (----------------)
798*4882a593Smuzhiyun * limit - setpoint
799*4882a593Smuzhiyun *
800*4882a593Smuzhiyun * it's a 3rd order polynomial that subjects to
801*4882a593Smuzhiyun *
802*4882a593Smuzhiyun * (1) f(freerun) = 2.0 => rampup dirty_ratelimit reasonably fast
803*4882a593Smuzhiyun * (2) f(setpoint) = 1.0 => the balance point
804*4882a593Smuzhiyun * (3) f(limit) = 0 => the hard limit
805*4882a593Smuzhiyun * (4) df/dx <= 0 => negative feedback control
806*4882a593Smuzhiyun * (5) the closer to setpoint, the smaller |df/dx| (and the reverse)
807*4882a593Smuzhiyun * => fast response on large errors; small oscillation near setpoint
808*4882a593Smuzhiyun */
pos_ratio_polynom(unsigned long setpoint,unsigned long dirty,unsigned long limit)809*4882a593Smuzhiyun static long long pos_ratio_polynom(unsigned long setpoint,
810*4882a593Smuzhiyun unsigned long dirty,
811*4882a593Smuzhiyun unsigned long limit)
812*4882a593Smuzhiyun {
813*4882a593Smuzhiyun long long pos_ratio;
814*4882a593Smuzhiyun long x;
815*4882a593Smuzhiyun
816*4882a593Smuzhiyun x = div64_s64(((s64)setpoint - (s64)dirty) << RATELIMIT_CALC_SHIFT,
817*4882a593Smuzhiyun (limit - setpoint) | 1);
818*4882a593Smuzhiyun pos_ratio = x;
819*4882a593Smuzhiyun pos_ratio = pos_ratio * x >> RATELIMIT_CALC_SHIFT;
820*4882a593Smuzhiyun pos_ratio = pos_ratio * x >> RATELIMIT_CALC_SHIFT;
821*4882a593Smuzhiyun pos_ratio += 1 << RATELIMIT_CALC_SHIFT;
822*4882a593Smuzhiyun
823*4882a593Smuzhiyun return clamp(pos_ratio, 0LL, 2LL << RATELIMIT_CALC_SHIFT);
824*4882a593Smuzhiyun }
825*4882a593Smuzhiyun
826*4882a593Smuzhiyun /*
827*4882a593Smuzhiyun * Dirty position control.
828*4882a593Smuzhiyun *
829*4882a593Smuzhiyun * (o) global/bdi setpoints
830*4882a593Smuzhiyun *
831*4882a593Smuzhiyun * We want the dirty pages be balanced around the global/wb setpoints.
832*4882a593Smuzhiyun * When the number of dirty pages is higher/lower than the setpoint, the
833*4882a593Smuzhiyun * dirty position control ratio (and hence task dirty ratelimit) will be
834*4882a593Smuzhiyun * decreased/increased to bring the dirty pages back to the setpoint.
835*4882a593Smuzhiyun *
836*4882a593Smuzhiyun * pos_ratio = 1 << RATELIMIT_CALC_SHIFT
837*4882a593Smuzhiyun *
838*4882a593Smuzhiyun * if (dirty < setpoint) scale up pos_ratio
839*4882a593Smuzhiyun * if (dirty > setpoint) scale down pos_ratio
840*4882a593Smuzhiyun *
841*4882a593Smuzhiyun * if (wb_dirty < wb_setpoint) scale up pos_ratio
842*4882a593Smuzhiyun * if (wb_dirty > wb_setpoint) scale down pos_ratio
843*4882a593Smuzhiyun *
844*4882a593Smuzhiyun * task_ratelimit = dirty_ratelimit * pos_ratio >> RATELIMIT_CALC_SHIFT
845*4882a593Smuzhiyun *
846*4882a593Smuzhiyun * (o) global control line
847*4882a593Smuzhiyun *
848*4882a593Smuzhiyun * ^ pos_ratio
849*4882a593Smuzhiyun * |
850*4882a593Smuzhiyun * | |<===== global dirty control scope ======>|
851*4882a593Smuzhiyun * 2.0 .............*
852*4882a593Smuzhiyun * | .*
853*4882a593Smuzhiyun * | . *
854*4882a593Smuzhiyun * | . *
855*4882a593Smuzhiyun * | . *
856*4882a593Smuzhiyun * | . *
857*4882a593Smuzhiyun * | . *
858*4882a593Smuzhiyun * 1.0 ................................*
859*4882a593Smuzhiyun * | . . *
860*4882a593Smuzhiyun * | . . *
861*4882a593Smuzhiyun * | . . *
862*4882a593Smuzhiyun * | . . *
863*4882a593Smuzhiyun * | . . *
864*4882a593Smuzhiyun * 0 +------------.------------------.----------------------*------------->
865*4882a593Smuzhiyun * freerun^ setpoint^ limit^ dirty pages
866*4882a593Smuzhiyun *
867*4882a593Smuzhiyun * (o) wb control line
868*4882a593Smuzhiyun *
869*4882a593Smuzhiyun * ^ pos_ratio
870*4882a593Smuzhiyun * |
871*4882a593Smuzhiyun * | *
872*4882a593Smuzhiyun * | *
873*4882a593Smuzhiyun * | *
874*4882a593Smuzhiyun * | *
875*4882a593Smuzhiyun * | * |<=========== span ============>|
876*4882a593Smuzhiyun * 1.0 .......................*
877*4882a593Smuzhiyun * | . *
878*4882a593Smuzhiyun * | . *
879*4882a593Smuzhiyun * | . *
880*4882a593Smuzhiyun * | . *
881*4882a593Smuzhiyun * | . *
882*4882a593Smuzhiyun * | . *
883*4882a593Smuzhiyun * | . *
884*4882a593Smuzhiyun * | . *
885*4882a593Smuzhiyun * | . *
886*4882a593Smuzhiyun * | . *
887*4882a593Smuzhiyun * | . *
888*4882a593Smuzhiyun * 1/4 ...............................................* * * * * * * * * * * *
889*4882a593Smuzhiyun * | . .
890*4882a593Smuzhiyun * | . .
891*4882a593Smuzhiyun * | . .
892*4882a593Smuzhiyun * 0 +----------------------.-------------------------------.------------->
893*4882a593Smuzhiyun * wb_setpoint^ x_intercept^
894*4882a593Smuzhiyun *
895*4882a593Smuzhiyun * The wb control line won't drop below pos_ratio=1/4, so that wb_dirty can
896*4882a593Smuzhiyun * be smoothly throttled down to normal if it starts high in situations like
897*4882a593Smuzhiyun * - start writing to a slow SD card and a fast disk at the same time. The SD
898*4882a593Smuzhiyun * card's wb_dirty may rush to many times higher than wb_setpoint.
899*4882a593Smuzhiyun * - the wb dirty thresh drops quickly due to change of JBOD workload
900*4882a593Smuzhiyun */
wb_position_ratio(struct dirty_throttle_control * dtc)901*4882a593Smuzhiyun static void wb_position_ratio(struct dirty_throttle_control *dtc)
902*4882a593Smuzhiyun {
903*4882a593Smuzhiyun struct bdi_writeback *wb = dtc->wb;
904*4882a593Smuzhiyun unsigned long write_bw = wb->avg_write_bandwidth;
905*4882a593Smuzhiyun unsigned long freerun = dirty_freerun_ceiling(dtc->thresh, dtc->bg_thresh);
906*4882a593Smuzhiyun unsigned long limit = hard_dirty_limit(dtc_dom(dtc), dtc->thresh);
907*4882a593Smuzhiyun unsigned long wb_thresh = dtc->wb_thresh;
908*4882a593Smuzhiyun unsigned long x_intercept;
909*4882a593Smuzhiyun unsigned long setpoint; /* dirty pages' target balance point */
910*4882a593Smuzhiyun unsigned long wb_setpoint;
911*4882a593Smuzhiyun unsigned long span;
912*4882a593Smuzhiyun long long pos_ratio; /* for scaling up/down the rate limit */
913*4882a593Smuzhiyun long x;
914*4882a593Smuzhiyun
915*4882a593Smuzhiyun dtc->pos_ratio = 0;
916*4882a593Smuzhiyun
917*4882a593Smuzhiyun if (unlikely(dtc->dirty >= limit))
918*4882a593Smuzhiyun return;
919*4882a593Smuzhiyun
920*4882a593Smuzhiyun /*
921*4882a593Smuzhiyun * global setpoint
922*4882a593Smuzhiyun *
923*4882a593Smuzhiyun * See comment for pos_ratio_polynom().
924*4882a593Smuzhiyun */
925*4882a593Smuzhiyun setpoint = (freerun + limit) / 2;
926*4882a593Smuzhiyun pos_ratio = pos_ratio_polynom(setpoint, dtc->dirty, limit);
927*4882a593Smuzhiyun
928*4882a593Smuzhiyun /*
929*4882a593Smuzhiyun * The strictlimit feature is a tool preventing mistrusted filesystems
930*4882a593Smuzhiyun * from growing a large number of dirty pages before throttling. For
931*4882a593Smuzhiyun * such filesystems balance_dirty_pages always checks wb counters
932*4882a593Smuzhiyun * against wb limits. Even if global "nr_dirty" is under "freerun".
933*4882a593Smuzhiyun * This is especially important for fuse which sets bdi->max_ratio to
934*4882a593Smuzhiyun * 1% by default. Without strictlimit feature, fuse writeback may
935*4882a593Smuzhiyun * consume arbitrary amount of RAM because it is accounted in
936*4882a593Smuzhiyun * NR_WRITEBACK_TEMP which is not involved in calculating "nr_dirty".
937*4882a593Smuzhiyun *
938*4882a593Smuzhiyun * Here, in wb_position_ratio(), we calculate pos_ratio based on
939*4882a593Smuzhiyun * two values: wb_dirty and wb_thresh. Let's consider an example:
940*4882a593Smuzhiyun * total amount of RAM is 16GB, bdi->max_ratio is equal to 1%, global
941*4882a593Smuzhiyun * limits are set by default to 10% and 20% (background and throttle).
942*4882a593Smuzhiyun * Then wb_thresh is 1% of 20% of 16GB. This amounts to ~8K pages.
943*4882a593Smuzhiyun * wb_calc_thresh(wb, bg_thresh) is about ~4K pages. wb_setpoint is
944*4882a593Smuzhiyun * about ~6K pages (as the average of background and throttle wb
945*4882a593Smuzhiyun * limits). The 3rd order polynomial will provide positive feedback if
946*4882a593Smuzhiyun * wb_dirty is under wb_setpoint and vice versa.
947*4882a593Smuzhiyun *
948*4882a593Smuzhiyun * Note, that we cannot use global counters in these calculations
949*4882a593Smuzhiyun * because we want to throttle process writing to a strictlimit wb
950*4882a593Smuzhiyun * much earlier than global "freerun" is reached (~23MB vs. ~2.3GB
951*4882a593Smuzhiyun * in the example above).
952*4882a593Smuzhiyun */
953*4882a593Smuzhiyun if (unlikely(wb->bdi->capabilities & BDI_CAP_STRICTLIMIT)) {
954*4882a593Smuzhiyun long long wb_pos_ratio;
955*4882a593Smuzhiyun
956*4882a593Smuzhiyun if (dtc->wb_dirty < 8) {
957*4882a593Smuzhiyun dtc->pos_ratio = min_t(long long, pos_ratio * 2,
958*4882a593Smuzhiyun 2 << RATELIMIT_CALC_SHIFT);
959*4882a593Smuzhiyun return;
960*4882a593Smuzhiyun }
961*4882a593Smuzhiyun
962*4882a593Smuzhiyun if (dtc->wb_dirty >= wb_thresh)
963*4882a593Smuzhiyun return;
964*4882a593Smuzhiyun
965*4882a593Smuzhiyun wb_setpoint = dirty_freerun_ceiling(wb_thresh,
966*4882a593Smuzhiyun dtc->wb_bg_thresh);
967*4882a593Smuzhiyun
968*4882a593Smuzhiyun if (wb_setpoint == 0 || wb_setpoint == wb_thresh)
969*4882a593Smuzhiyun return;
970*4882a593Smuzhiyun
971*4882a593Smuzhiyun wb_pos_ratio = pos_ratio_polynom(wb_setpoint, dtc->wb_dirty,
972*4882a593Smuzhiyun wb_thresh);
973*4882a593Smuzhiyun
974*4882a593Smuzhiyun /*
975*4882a593Smuzhiyun * Typically, for strictlimit case, wb_setpoint << setpoint
976*4882a593Smuzhiyun * and pos_ratio >> wb_pos_ratio. In the other words global
977*4882a593Smuzhiyun * state ("dirty") is not limiting factor and we have to
978*4882a593Smuzhiyun * make decision based on wb counters. But there is an
979*4882a593Smuzhiyun * important case when global pos_ratio should get precedence:
980*4882a593Smuzhiyun * global limits are exceeded (e.g. due to activities on other
981*4882a593Smuzhiyun * wb's) while given strictlimit wb is below limit.
982*4882a593Smuzhiyun *
983*4882a593Smuzhiyun * "pos_ratio * wb_pos_ratio" would work for the case above,
984*4882a593Smuzhiyun * but it would look too non-natural for the case of all
985*4882a593Smuzhiyun * activity in the system coming from a single strictlimit wb
986*4882a593Smuzhiyun * with bdi->max_ratio == 100%.
987*4882a593Smuzhiyun *
988*4882a593Smuzhiyun * Note that min() below somewhat changes the dynamics of the
989*4882a593Smuzhiyun * control system. Normally, pos_ratio value can be well over 3
990*4882a593Smuzhiyun * (when globally we are at freerun and wb is well below wb
991*4882a593Smuzhiyun * setpoint). Now the maximum pos_ratio in the same situation
992*4882a593Smuzhiyun * is 2. We might want to tweak this if we observe the control
993*4882a593Smuzhiyun * system is too slow to adapt.
994*4882a593Smuzhiyun */
995*4882a593Smuzhiyun dtc->pos_ratio = min(pos_ratio, wb_pos_ratio);
996*4882a593Smuzhiyun return;
997*4882a593Smuzhiyun }
998*4882a593Smuzhiyun
999*4882a593Smuzhiyun /*
1000*4882a593Smuzhiyun * We have computed basic pos_ratio above based on global situation. If
1001*4882a593Smuzhiyun * the wb is over/under its share of dirty pages, we want to scale
1002*4882a593Smuzhiyun * pos_ratio further down/up. That is done by the following mechanism.
1003*4882a593Smuzhiyun */
1004*4882a593Smuzhiyun
1005*4882a593Smuzhiyun /*
1006*4882a593Smuzhiyun * wb setpoint
1007*4882a593Smuzhiyun *
1008*4882a593Smuzhiyun * f(wb_dirty) := 1.0 + k * (wb_dirty - wb_setpoint)
1009*4882a593Smuzhiyun *
1010*4882a593Smuzhiyun * x_intercept - wb_dirty
1011*4882a593Smuzhiyun * := --------------------------
1012*4882a593Smuzhiyun * x_intercept - wb_setpoint
1013*4882a593Smuzhiyun *
1014*4882a593Smuzhiyun * The main wb control line is a linear function that subjects to
1015*4882a593Smuzhiyun *
1016*4882a593Smuzhiyun * (1) f(wb_setpoint) = 1.0
1017*4882a593Smuzhiyun * (2) k = - 1 / (8 * write_bw) (in single wb case)
1018*4882a593Smuzhiyun * or equally: x_intercept = wb_setpoint + 8 * write_bw
1019*4882a593Smuzhiyun *
1020*4882a593Smuzhiyun * For single wb case, the dirty pages are observed to fluctuate
1021*4882a593Smuzhiyun * regularly within range
1022*4882a593Smuzhiyun * [wb_setpoint - write_bw/2, wb_setpoint + write_bw/2]
1023*4882a593Smuzhiyun * for various filesystems, where (2) can yield in a reasonable 12.5%
1024*4882a593Smuzhiyun * fluctuation range for pos_ratio.
1025*4882a593Smuzhiyun *
1026*4882a593Smuzhiyun * For JBOD case, wb_thresh (not wb_dirty!) could fluctuate up to its
1027*4882a593Smuzhiyun * own size, so move the slope over accordingly and choose a slope that
1028*4882a593Smuzhiyun * yields 100% pos_ratio fluctuation on suddenly doubled wb_thresh.
1029*4882a593Smuzhiyun */
1030*4882a593Smuzhiyun if (unlikely(wb_thresh > dtc->thresh))
1031*4882a593Smuzhiyun wb_thresh = dtc->thresh;
1032*4882a593Smuzhiyun /*
1033*4882a593Smuzhiyun * It's very possible that wb_thresh is close to 0 not because the
1034*4882a593Smuzhiyun * device is slow, but that it has remained inactive for long time.
1035*4882a593Smuzhiyun * Honour such devices a reasonable good (hopefully IO efficient)
1036*4882a593Smuzhiyun * threshold, so that the occasional writes won't be blocked and active
1037*4882a593Smuzhiyun * writes can rampup the threshold quickly.
1038*4882a593Smuzhiyun */
1039*4882a593Smuzhiyun wb_thresh = max(wb_thresh, (limit - dtc->dirty) / 8);
1040*4882a593Smuzhiyun /*
1041*4882a593Smuzhiyun * scale global setpoint to wb's:
1042*4882a593Smuzhiyun * wb_setpoint = setpoint * wb_thresh / thresh
1043*4882a593Smuzhiyun */
1044*4882a593Smuzhiyun x = div_u64((u64)wb_thresh << 16, dtc->thresh | 1);
1045*4882a593Smuzhiyun wb_setpoint = setpoint * (u64)x >> 16;
1046*4882a593Smuzhiyun /*
1047*4882a593Smuzhiyun * Use span=(8*write_bw) in single wb case as indicated by
1048*4882a593Smuzhiyun * (thresh - wb_thresh ~= 0) and transit to wb_thresh in JBOD case.
1049*4882a593Smuzhiyun *
1050*4882a593Smuzhiyun * wb_thresh thresh - wb_thresh
1051*4882a593Smuzhiyun * span = --------- * (8 * write_bw) + ------------------ * wb_thresh
1052*4882a593Smuzhiyun * thresh thresh
1053*4882a593Smuzhiyun */
1054*4882a593Smuzhiyun span = (dtc->thresh - wb_thresh + 8 * write_bw) * (u64)x >> 16;
1055*4882a593Smuzhiyun x_intercept = wb_setpoint + span;
1056*4882a593Smuzhiyun
1057*4882a593Smuzhiyun if (dtc->wb_dirty < x_intercept - span / 4) {
1058*4882a593Smuzhiyun pos_ratio = div64_u64(pos_ratio * (x_intercept - dtc->wb_dirty),
1059*4882a593Smuzhiyun (x_intercept - wb_setpoint) | 1);
1060*4882a593Smuzhiyun } else
1061*4882a593Smuzhiyun pos_ratio /= 4;
1062*4882a593Smuzhiyun
1063*4882a593Smuzhiyun /*
1064*4882a593Smuzhiyun * wb reserve area, safeguard against dirty pool underrun and disk idle
1065*4882a593Smuzhiyun * It may push the desired control point of global dirty pages higher
1066*4882a593Smuzhiyun * than setpoint.
1067*4882a593Smuzhiyun */
1068*4882a593Smuzhiyun x_intercept = wb_thresh / 2;
1069*4882a593Smuzhiyun if (dtc->wb_dirty < x_intercept) {
1070*4882a593Smuzhiyun if (dtc->wb_dirty > x_intercept / 8)
1071*4882a593Smuzhiyun pos_ratio = div_u64(pos_ratio * x_intercept,
1072*4882a593Smuzhiyun dtc->wb_dirty);
1073*4882a593Smuzhiyun else
1074*4882a593Smuzhiyun pos_ratio *= 8;
1075*4882a593Smuzhiyun }
1076*4882a593Smuzhiyun
1077*4882a593Smuzhiyun dtc->pos_ratio = pos_ratio;
1078*4882a593Smuzhiyun }
1079*4882a593Smuzhiyun
wb_update_write_bandwidth(struct bdi_writeback * wb,unsigned long elapsed,unsigned long written)1080*4882a593Smuzhiyun static void wb_update_write_bandwidth(struct bdi_writeback *wb,
1081*4882a593Smuzhiyun unsigned long elapsed,
1082*4882a593Smuzhiyun unsigned long written)
1083*4882a593Smuzhiyun {
1084*4882a593Smuzhiyun const unsigned long period = roundup_pow_of_two(3 * HZ);
1085*4882a593Smuzhiyun unsigned long avg = wb->avg_write_bandwidth;
1086*4882a593Smuzhiyun unsigned long old = wb->write_bandwidth;
1087*4882a593Smuzhiyun u64 bw;
1088*4882a593Smuzhiyun
1089*4882a593Smuzhiyun /*
1090*4882a593Smuzhiyun * bw = written * HZ / elapsed
1091*4882a593Smuzhiyun *
1092*4882a593Smuzhiyun * bw * elapsed + write_bandwidth * (period - elapsed)
1093*4882a593Smuzhiyun * write_bandwidth = ---------------------------------------------------
1094*4882a593Smuzhiyun * period
1095*4882a593Smuzhiyun *
1096*4882a593Smuzhiyun * @written may have decreased due to account_page_redirty().
1097*4882a593Smuzhiyun * Avoid underflowing @bw calculation.
1098*4882a593Smuzhiyun */
1099*4882a593Smuzhiyun bw = written - min(written, wb->written_stamp);
1100*4882a593Smuzhiyun bw *= HZ;
1101*4882a593Smuzhiyun if (unlikely(elapsed > period)) {
1102*4882a593Smuzhiyun bw = div64_ul(bw, elapsed);
1103*4882a593Smuzhiyun avg = bw;
1104*4882a593Smuzhiyun goto out;
1105*4882a593Smuzhiyun }
1106*4882a593Smuzhiyun bw += (u64)wb->write_bandwidth * (period - elapsed);
1107*4882a593Smuzhiyun bw >>= ilog2(period);
1108*4882a593Smuzhiyun
1109*4882a593Smuzhiyun /*
1110*4882a593Smuzhiyun * one more level of smoothing, for filtering out sudden spikes
1111*4882a593Smuzhiyun */
1112*4882a593Smuzhiyun if (avg > old && old >= (unsigned long)bw)
1113*4882a593Smuzhiyun avg -= (avg - old) >> 3;
1114*4882a593Smuzhiyun
1115*4882a593Smuzhiyun if (avg < old && old <= (unsigned long)bw)
1116*4882a593Smuzhiyun avg += (old - avg) >> 3;
1117*4882a593Smuzhiyun
1118*4882a593Smuzhiyun out:
1119*4882a593Smuzhiyun /* keep avg > 0 to guarantee that tot > 0 if there are dirty wbs */
1120*4882a593Smuzhiyun avg = max(avg, 1LU);
1121*4882a593Smuzhiyun if (wb_has_dirty_io(wb)) {
1122*4882a593Smuzhiyun long delta = avg - wb->avg_write_bandwidth;
1123*4882a593Smuzhiyun WARN_ON_ONCE(atomic_long_add_return(delta,
1124*4882a593Smuzhiyun &wb->bdi->tot_write_bandwidth) <= 0);
1125*4882a593Smuzhiyun }
1126*4882a593Smuzhiyun wb->write_bandwidth = bw;
1127*4882a593Smuzhiyun wb->avg_write_bandwidth = avg;
1128*4882a593Smuzhiyun }
1129*4882a593Smuzhiyun
update_dirty_limit(struct dirty_throttle_control * dtc)1130*4882a593Smuzhiyun static void update_dirty_limit(struct dirty_throttle_control *dtc)
1131*4882a593Smuzhiyun {
1132*4882a593Smuzhiyun struct wb_domain *dom = dtc_dom(dtc);
1133*4882a593Smuzhiyun unsigned long thresh = dtc->thresh;
1134*4882a593Smuzhiyun unsigned long limit = dom->dirty_limit;
1135*4882a593Smuzhiyun
1136*4882a593Smuzhiyun /*
1137*4882a593Smuzhiyun * Follow up in one step.
1138*4882a593Smuzhiyun */
1139*4882a593Smuzhiyun if (limit < thresh) {
1140*4882a593Smuzhiyun limit = thresh;
1141*4882a593Smuzhiyun goto update;
1142*4882a593Smuzhiyun }
1143*4882a593Smuzhiyun
1144*4882a593Smuzhiyun /*
1145*4882a593Smuzhiyun * Follow down slowly. Use the higher one as the target, because thresh
1146*4882a593Smuzhiyun * may drop below dirty. This is exactly the reason to introduce
1147*4882a593Smuzhiyun * dom->dirty_limit which is guaranteed to lie above the dirty pages.
1148*4882a593Smuzhiyun */
1149*4882a593Smuzhiyun thresh = max(thresh, dtc->dirty);
1150*4882a593Smuzhiyun if (limit > thresh) {
1151*4882a593Smuzhiyun limit -= (limit - thresh) >> 5;
1152*4882a593Smuzhiyun goto update;
1153*4882a593Smuzhiyun }
1154*4882a593Smuzhiyun return;
1155*4882a593Smuzhiyun update:
1156*4882a593Smuzhiyun dom->dirty_limit = limit;
1157*4882a593Smuzhiyun }
1158*4882a593Smuzhiyun
domain_update_bandwidth(struct dirty_throttle_control * dtc,unsigned long now)1159*4882a593Smuzhiyun static void domain_update_bandwidth(struct dirty_throttle_control *dtc,
1160*4882a593Smuzhiyun unsigned long now)
1161*4882a593Smuzhiyun {
1162*4882a593Smuzhiyun struct wb_domain *dom = dtc_dom(dtc);
1163*4882a593Smuzhiyun
1164*4882a593Smuzhiyun /*
1165*4882a593Smuzhiyun * check locklessly first to optimize away locking for the most time
1166*4882a593Smuzhiyun */
1167*4882a593Smuzhiyun if (time_before(now, dom->dirty_limit_tstamp + BANDWIDTH_INTERVAL))
1168*4882a593Smuzhiyun return;
1169*4882a593Smuzhiyun
1170*4882a593Smuzhiyun spin_lock(&dom->lock);
1171*4882a593Smuzhiyun if (time_after_eq(now, dom->dirty_limit_tstamp + BANDWIDTH_INTERVAL)) {
1172*4882a593Smuzhiyun update_dirty_limit(dtc);
1173*4882a593Smuzhiyun dom->dirty_limit_tstamp = now;
1174*4882a593Smuzhiyun }
1175*4882a593Smuzhiyun spin_unlock(&dom->lock);
1176*4882a593Smuzhiyun }
1177*4882a593Smuzhiyun
1178*4882a593Smuzhiyun /*
1179*4882a593Smuzhiyun * Maintain wb->dirty_ratelimit, the base dirty throttle rate.
1180*4882a593Smuzhiyun *
1181*4882a593Smuzhiyun * Normal wb tasks will be curbed at or below it in long term.
1182*4882a593Smuzhiyun * Obviously it should be around (write_bw / N) when there are N dd tasks.
1183*4882a593Smuzhiyun */
wb_update_dirty_ratelimit(struct dirty_throttle_control * dtc,unsigned long dirtied,unsigned long elapsed)1184*4882a593Smuzhiyun static void wb_update_dirty_ratelimit(struct dirty_throttle_control *dtc,
1185*4882a593Smuzhiyun unsigned long dirtied,
1186*4882a593Smuzhiyun unsigned long elapsed)
1187*4882a593Smuzhiyun {
1188*4882a593Smuzhiyun struct bdi_writeback *wb = dtc->wb;
1189*4882a593Smuzhiyun unsigned long dirty = dtc->dirty;
1190*4882a593Smuzhiyun unsigned long freerun = dirty_freerun_ceiling(dtc->thresh, dtc->bg_thresh);
1191*4882a593Smuzhiyun unsigned long limit = hard_dirty_limit(dtc_dom(dtc), dtc->thresh);
1192*4882a593Smuzhiyun unsigned long setpoint = (freerun + limit) / 2;
1193*4882a593Smuzhiyun unsigned long write_bw = wb->avg_write_bandwidth;
1194*4882a593Smuzhiyun unsigned long dirty_ratelimit = wb->dirty_ratelimit;
1195*4882a593Smuzhiyun unsigned long dirty_rate;
1196*4882a593Smuzhiyun unsigned long task_ratelimit;
1197*4882a593Smuzhiyun unsigned long balanced_dirty_ratelimit;
1198*4882a593Smuzhiyun unsigned long step;
1199*4882a593Smuzhiyun unsigned long x;
1200*4882a593Smuzhiyun unsigned long shift;
1201*4882a593Smuzhiyun
1202*4882a593Smuzhiyun /*
1203*4882a593Smuzhiyun * The dirty rate will match the writeout rate in long term, except
1204*4882a593Smuzhiyun * when dirty pages are truncated by userspace or re-dirtied by FS.
1205*4882a593Smuzhiyun */
1206*4882a593Smuzhiyun dirty_rate = (dirtied - wb->dirtied_stamp) * HZ / elapsed;
1207*4882a593Smuzhiyun
1208*4882a593Smuzhiyun /*
1209*4882a593Smuzhiyun * task_ratelimit reflects each dd's dirty rate for the past 200ms.
1210*4882a593Smuzhiyun */
1211*4882a593Smuzhiyun task_ratelimit = (u64)dirty_ratelimit *
1212*4882a593Smuzhiyun dtc->pos_ratio >> RATELIMIT_CALC_SHIFT;
1213*4882a593Smuzhiyun task_ratelimit++; /* it helps rampup dirty_ratelimit from tiny values */
1214*4882a593Smuzhiyun
1215*4882a593Smuzhiyun /*
1216*4882a593Smuzhiyun * A linear estimation of the "balanced" throttle rate. The theory is,
1217*4882a593Smuzhiyun * if there are N dd tasks, each throttled at task_ratelimit, the wb's
1218*4882a593Smuzhiyun * dirty_rate will be measured to be (N * task_ratelimit). So the below
1219*4882a593Smuzhiyun * formula will yield the balanced rate limit (write_bw / N).
1220*4882a593Smuzhiyun *
1221*4882a593Smuzhiyun * Note that the expanded form is not a pure rate feedback:
1222*4882a593Smuzhiyun * rate_(i+1) = rate_(i) * (write_bw / dirty_rate) (1)
1223*4882a593Smuzhiyun * but also takes pos_ratio into account:
1224*4882a593Smuzhiyun * rate_(i+1) = rate_(i) * (write_bw / dirty_rate) * pos_ratio (2)
1225*4882a593Smuzhiyun *
1226*4882a593Smuzhiyun * (1) is not realistic because pos_ratio also takes part in balancing
1227*4882a593Smuzhiyun * the dirty rate. Consider the state
1228*4882a593Smuzhiyun * pos_ratio = 0.5 (3)
1229*4882a593Smuzhiyun * rate = 2 * (write_bw / N) (4)
1230*4882a593Smuzhiyun * If (1) is used, it will stuck in that state! Because each dd will
1231*4882a593Smuzhiyun * be throttled at
1232*4882a593Smuzhiyun * task_ratelimit = pos_ratio * rate = (write_bw / N) (5)
1233*4882a593Smuzhiyun * yielding
1234*4882a593Smuzhiyun * dirty_rate = N * task_ratelimit = write_bw (6)
1235*4882a593Smuzhiyun * put (6) into (1) we get
1236*4882a593Smuzhiyun * rate_(i+1) = rate_(i) (7)
1237*4882a593Smuzhiyun *
1238*4882a593Smuzhiyun * So we end up using (2) to always keep
1239*4882a593Smuzhiyun * rate_(i+1) ~= (write_bw / N) (8)
1240*4882a593Smuzhiyun * regardless of the value of pos_ratio. As long as (8) is satisfied,
1241*4882a593Smuzhiyun * pos_ratio is able to drive itself to 1.0, which is not only where
1242*4882a593Smuzhiyun * the dirty count meet the setpoint, but also where the slope of
1243*4882a593Smuzhiyun * pos_ratio is most flat and hence task_ratelimit is least fluctuated.
1244*4882a593Smuzhiyun */
1245*4882a593Smuzhiyun balanced_dirty_ratelimit = div_u64((u64)task_ratelimit * write_bw,
1246*4882a593Smuzhiyun dirty_rate | 1);
1247*4882a593Smuzhiyun /*
1248*4882a593Smuzhiyun * balanced_dirty_ratelimit ~= (write_bw / N) <= write_bw
1249*4882a593Smuzhiyun */
1250*4882a593Smuzhiyun if (unlikely(balanced_dirty_ratelimit > write_bw))
1251*4882a593Smuzhiyun balanced_dirty_ratelimit = write_bw;
1252*4882a593Smuzhiyun
1253*4882a593Smuzhiyun /*
1254*4882a593Smuzhiyun * We could safely do this and return immediately:
1255*4882a593Smuzhiyun *
1256*4882a593Smuzhiyun * wb->dirty_ratelimit = balanced_dirty_ratelimit;
1257*4882a593Smuzhiyun *
1258*4882a593Smuzhiyun * However to get a more stable dirty_ratelimit, the below elaborated
1259*4882a593Smuzhiyun * code makes use of task_ratelimit to filter out singular points and
1260*4882a593Smuzhiyun * limit the step size.
1261*4882a593Smuzhiyun *
1262*4882a593Smuzhiyun * The below code essentially only uses the relative value of
1263*4882a593Smuzhiyun *
1264*4882a593Smuzhiyun * task_ratelimit - dirty_ratelimit
1265*4882a593Smuzhiyun * = (pos_ratio - 1) * dirty_ratelimit
1266*4882a593Smuzhiyun *
1267*4882a593Smuzhiyun * which reflects the direction and size of dirty position error.
1268*4882a593Smuzhiyun */
1269*4882a593Smuzhiyun
1270*4882a593Smuzhiyun /*
1271*4882a593Smuzhiyun * dirty_ratelimit will follow balanced_dirty_ratelimit iff
1272*4882a593Smuzhiyun * task_ratelimit is on the same side of dirty_ratelimit, too.
1273*4882a593Smuzhiyun * For example, when
1274*4882a593Smuzhiyun * - dirty_ratelimit > balanced_dirty_ratelimit
1275*4882a593Smuzhiyun * - dirty_ratelimit > task_ratelimit (dirty pages are above setpoint)
1276*4882a593Smuzhiyun * lowering dirty_ratelimit will help meet both the position and rate
1277*4882a593Smuzhiyun * control targets. Otherwise, don't update dirty_ratelimit if it will
1278*4882a593Smuzhiyun * only help meet the rate target. After all, what the users ultimately
1279*4882a593Smuzhiyun * feel and care are stable dirty rate and small position error.
1280*4882a593Smuzhiyun *
1281*4882a593Smuzhiyun * |task_ratelimit - dirty_ratelimit| is used to limit the step size
1282*4882a593Smuzhiyun * and filter out the singular points of balanced_dirty_ratelimit. Which
1283*4882a593Smuzhiyun * keeps jumping around randomly and can even leap far away at times
1284*4882a593Smuzhiyun * due to the small 200ms estimation period of dirty_rate (we want to
1285*4882a593Smuzhiyun * keep that period small to reduce time lags).
1286*4882a593Smuzhiyun */
1287*4882a593Smuzhiyun step = 0;
1288*4882a593Smuzhiyun
1289*4882a593Smuzhiyun /*
1290*4882a593Smuzhiyun * For strictlimit case, calculations above were based on wb counters
1291*4882a593Smuzhiyun * and limits (starting from pos_ratio = wb_position_ratio() and up to
1292*4882a593Smuzhiyun * balanced_dirty_ratelimit = task_ratelimit * write_bw / dirty_rate).
1293*4882a593Smuzhiyun * Hence, to calculate "step" properly, we have to use wb_dirty as
1294*4882a593Smuzhiyun * "dirty" and wb_setpoint as "setpoint".
1295*4882a593Smuzhiyun *
1296*4882a593Smuzhiyun * We rampup dirty_ratelimit forcibly if wb_dirty is low because
1297*4882a593Smuzhiyun * it's possible that wb_thresh is close to zero due to inactivity
1298*4882a593Smuzhiyun * of backing device.
1299*4882a593Smuzhiyun */
1300*4882a593Smuzhiyun if (unlikely(wb->bdi->capabilities & BDI_CAP_STRICTLIMIT)) {
1301*4882a593Smuzhiyun dirty = dtc->wb_dirty;
1302*4882a593Smuzhiyun if (dtc->wb_dirty < 8)
1303*4882a593Smuzhiyun setpoint = dtc->wb_dirty + 1;
1304*4882a593Smuzhiyun else
1305*4882a593Smuzhiyun setpoint = (dtc->wb_thresh + dtc->wb_bg_thresh) / 2;
1306*4882a593Smuzhiyun }
1307*4882a593Smuzhiyun
1308*4882a593Smuzhiyun if (dirty < setpoint) {
1309*4882a593Smuzhiyun x = min3(wb->balanced_dirty_ratelimit,
1310*4882a593Smuzhiyun balanced_dirty_ratelimit, task_ratelimit);
1311*4882a593Smuzhiyun if (dirty_ratelimit < x)
1312*4882a593Smuzhiyun step = x - dirty_ratelimit;
1313*4882a593Smuzhiyun } else {
1314*4882a593Smuzhiyun x = max3(wb->balanced_dirty_ratelimit,
1315*4882a593Smuzhiyun balanced_dirty_ratelimit, task_ratelimit);
1316*4882a593Smuzhiyun if (dirty_ratelimit > x)
1317*4882a593Smuzhiyun step = dirty_ratelimit - x;
1318*4882a593Smuzhiyun }
1319*4882a593Smuzhiyun
1320*4882a593Smuzhiyun /*
1321*4882a593Smuzhiyun * Don't pursue 100% rate matching. It's impossible since the balanced
1322*4882a593Smuzhiyun * rate itself is constantly fluctuating. So decrease the track speed
1323*4882a593Smuzhiyun * when it gets close to the target. Helps eliminate pointless tremors.
1324*4882a593Smuzhiyun */
1325*4882a593Smuzhiyun shift = dirty_ratelimit / (2 * step + 1);
1326*4882a593Smuzhiyun if (shift < BITS_PER_LONG)
1327*4882a593Smuzhiyun step = DIV_ROUND_UP(step >> shift, 8);
1328*4882a593Smuzhiyun else
1329*4882a593Smuzhiyun step = 0;
1330*4882a593Smuzhiyun
1331*4882a593Smuzhiyun if (dirty_ratelimit < balanced_dirty_ratelimit)
1332*4882a593Smuzhiyun dirty_ratelimit += step;
1333*4882a593Smuzhiyun else
1334*4882a593Smuzhiyun dirty_ratelimit -= step;
1335*4882a593Smuzhiyun
1336*4882a593Smuzhiyun wb->dirty_ratelimit = max(dirty_ratelimit, 1UL);
1337*4882a593Smuzhiyun wb->balanced_dirty_ratelimit = balanced_dirty_ratelimit;
1338*4882a593Smuzhiyun
1339*4882a593Smuzhiyun trace_bdi_dirty_ratelimit(wb, dirty_rate, task_ratelimit);
1340*4882a593Smuzhiyun }
1341*4882a593Smuzhiyun
__wb_update_bandwidth(struct dirty_throttle_control * gdtc,struct dirty_throttle_control * mdtc,unsigned long start_time,bool update_ratelimit)1342*4882a593Smuzhiyun static void __wb_update_bandwidth(struct dirty_throttle_control *gdtc,
1343*4882a593Smuzhiyun struct dirty_throttle_control *mdtc,
1344*4882a593Smuzhiyun unsigned long start_time,
1345*4882a593Smuzhiyun bool update_ratelimit)
1346*4882a593Smuzhiyun {
1347*4882a593Smuzhiyun struct bdi_writeback *wb = gdtc->wb;
1348*4882a593Smuzhiyun unsigned long now = jiffies;
1349*4882a593Smuzhiyun unsigned long elapsed = now - wb->bw_time_stamp;
1350*4882a593Smuzhiyun unsigned long dirtied;
1351*4882a593Smuzhiyun unsigned long written;
1352*4882a593Smuzhiyun
1353*4882a593Smuzhiyun lockdep_assert_held(&wb->list_lock);
1354*4882a593Smuzhiyun
1355*4882a593Smuzhiyun /*
1356*4882a593Smuzhiyun * rate-limit, only update once every 200ms.
1357*4882a593Smuzhiyun */
1358*4882a593Smuzhiyun if (elapsed < BANDWIDTH_INTERVAL)
1359*4882a593Smuzhiyun return;
1360*4882a593Smuzhiyun
1361*4882a593Smuzhiyun dirtied = percpu_counter_read(&wb->stat[WB_DIRTIED]);
1362*4882a593Smuzhiyun written = percpu_counter_read(&wb->stat[WB_WRITTEN]);
1363*4882a593Smuzhiyun
1364*4882a593Smuzhiyun /*
1365*4882a593Smuzhiyun * Skip quiet periods when disk bandwidth is under-utilized.
1366*4882a593Smuzhiyun * (at least 1s idle time between two flusher runs)
1367*4882a593Smuzhiyun */
1368*4882a593Smuzhiyun if (elapsed > HZ && time_before(wb->bw_time_stamp, start_time))
1369*4882a593Smuzhiyun goto snapshot;
1370*4882a593Smuzhiyun
1371*4882a593Smuzhiyun if (update_ratelimit) {
1372*4882a593Smuzhiyun domain_update_bandwidth(gdtc, now);
1373*4882a593Smuzhiyun wb_update_dirty_ratelimit(gdtc, dirtied, elapsed);
1374*4882a593Smuzhiyun
1375*4882a593Smuzhiyun /*
1376*4882a593Smuzhiyun * @mdtc is always NULL if !CGROUP_WRITEBACK but the
1377*4882a593Smuzhiyun * compiler has no way to figure that out. Help it.
1378*4882a593Smuzhiyun */
1379*4882a593Smuzhiyun if (IS_ENABLED(CONFIG_CGROUP_WRITEBACK) && mdtc) {
1380*4882a593Smuzhiyun domain_update_bandwidth(mdtc, now);
1381*4882a593Smuzhiyun wb_update_dirty_ratelimit(mdtc, dirtied, elapsed);
1382*4882a593Smuzhiyun }
1383*4882a593Smuzhiyun }
1384*4882a593Smuzhiyun wb_update_write_bandwidth(wb, elapsed, written);
1385*4882a593Smuzhiyun
1386*4882a593Smuzhiyun snapshot:
1387*4882a593Smuzhiyun wb->dirtied_stamp = dirtied;
1388*4882a593Smuzhiyun wb->written_stamp = written;
1389*4882a593Smuzhiyun wb->bw_time_stamp = now;
1390*4882a593Smuzhiyun }
1391*4882a593Smuzhiyun
wb_update_bandwidth(struct bdi_writeback * wb,unsigned long start_time)1392*4882a593Smuzhiyun void wb_update_bandwidth(struct bdi_writeback *wb, unsigned long start_time)
1393*4882a593Smuzhiyun {
1394*4882a593Smuzhiyun struct dirty_throttle_control gdtc = { GDTC_INIT(wb) };
1395*4882a593Smuzhiyun
1396*4882a593Smuzhiyun __wb_update_bandwidth(&gdtc, NULL, start_time, false);
1397*4882a593Smuzhiyun }
1398*4882a593Smuzhiyun
1399*4882a593Smuzhiyun /*
1400*4882a593Smuzhiyun * After a task dirtied this many pages, balance_dirty_pages_ratelimited()
1401*4882a593Smuzhiyun * will look to see if it needs to start dirty throttling.
1402*4882a593Smuzhiyun *
1403*4882a593Smuzhiyun * If dirty_poll_interval is too low, big NUMA machines will call the expensive
1404*4882a593Smuzhiyun * global_zone_page_state() too often. So scale it near-sqrt to the safety margin
1405*4882a593Smuzhiyun * (the number of pages we may dirty without exceeding the dirty limits).
1406*4882a593Smuzhiyun */
dirty_poll_interval(unsigned long dirty,unsigned long thresh)1407*4882a593Smuzhiyun static unsigned long dirty_poll_interval(unsigned long dirty,
1408*4882a593Smuzhiyun unsigned long thresh)
1409*4882a593Smuzhiyun {
1410*4882a593Smuzhiyun if (thresh > dirty)
1411*4882a593Smuzhiyun return 1UL << (ilog2(thresh - dirty) >> 1);
1412*4882a593Smuzhiyun
1413*4882a593Smuzhiyun return 1;
1414*4882a593Smuzhiyun }
1415*4882a593Smuzhiyun
wb_max_pause(struct bdi_writeback * wb,unsigned long wb_dirty)1416*4882a593Smuzhiyun static unsigned long wb_max_pause(struct bdi_writeback *wb,
1417*4882a593Smuzhiyun unsigned long wb_dirty)
1418*4882a593Smuzhiyun {
1419*4882a593Smuzhiyun unsigned long bw = wb->avg_write_bandwidth;
1420*4882a593Smuzhiyun unsigned long t;
1421*4882a593Smuzhiyun
1422*4882a593Smuzhiyun /*
1423*4882a593Smuzhiyun * Limit pause time for small memory systems. If sleeping for too long
1424*4882a593Smuzhiyun * time, a small pool of dirty/writeback pages may go empty and disk go
1425*4882a593Smuzhiyun * idle.
1426*4882a593Smuzhiyun *
1427*4882a593Smuzhiyun * 8 serves as the safety ratio.
1428*4882a593Smuzhiyun */
1429*4882a593Smuzhiyun t = wb_dirty / (1 + bw / roundup_pow_of_two(1 + HZ / 8));
1430*4882a593Smuzhiyun t++;
1431*4882a593Smuzhiyun
1432*4882a593Smuzhiyun return min_t(unsigned long, t, MAX_PAUSE);
1433*4882a593Smuzhiyun }
1434*4882a593Smuzhiyun
wb_min_pause(struct bdi_writeback * wb,long max_pause,unsigned long task_ratelimit,unsigned long dirty_ratelimit,int * nr_dirtied_pause)1435*4882a593Smuzhiyun static long wb_min_pause(struct bdi_writeback *wb,
1436*4882a593Smuzhiyun long max_pause,
1437*4882a593Smuzhiyun unsigned long task_ratelimit,
1438*4882a593Smuzhiyun unsigned long dirty_ratelimit,
1439*4882a593Smuzhiyun int *nr_dirtied_pause)
1440*4882a593Smuzhiyun {
1441*4882a593Smuzhiyun long hi = ilog2(wb->avg_write_bandwidth);
1442*4882a593Smuzhiyun long lo = ilog2(wb->dirty_ratelimit);
1443*4882a593Smuzhiyun long t; /* target pause */
1444*4882a593Smuzhiyun long pause; /* estimated next pause */
1445*4882a593Smuzhiyun int pages; /* target nr_dirtied_pause */
1446*4882a593Smuzhiyun
1447*4882a593Smuzhiyun /* target for 10ms pause on 1-dd case */
1448*4882a593Smuzhiyun t = max(1, HZ / 100);
1449*4882a593Smuzhiyun
1450*4882a593Smuzhiyun /*
1451*4882a593Smuzhiyun * Scale up pause time for concurrent dirtiers in order to reduce CPU
1452*4882a593Smuzhiyun * overheads.
1453*4882a593Smuzhiyun *
1454*4882a593Smuzhiyun * (N * 10ms) on 2^N concurrent tasks.
1455*4882a593Smuzhiyun */
1456*4882a593Smuzhiyun if (hi > lo)
1457*4882a593Smuzhiyun t += (hi - lo) * (10 * HZ) / 1024;
1458*4882a593Smuzhiyun
1459*4882a593Smuzhiyun /*
1460*4882a593Smuzhiyun * This is a bit convoluted. We try to base the next nr_dirtied_pause
1461*4882a593Smuzhiyun * on the much more stable dirty_ratelimit. However the next pause time
1462*4882a593Smuzhiyun * will be computed based on task_ratelimit and the two rate limits may
1463*4882a593Smuzhiyun * depart considerably at some time. Especially if task_ratelimit goes
1464*4882a593Smuzhiyun * below dirty_ratelimit/2 and the target pause is max_pause, the next
1465*4882a593Smuzhiyun * pause time will be max_pause*2 _trimmed down_ to max_pause. As a
1466*4882a593Smuzhiyun * result task_ratelimit won't be executed faithfully, which could
1467*4882a593Smuzhiyun * eventually bring down dirty_ratelimit.
1468*4882a593Smuzhiyun *
1469*4882a593Smuzhiyun * We apply two rules to fix it up:
1470*4882a593Smuzhiyun * 1) try to estimate the next pause time and if necessary, use a lower
1471*4882a593Smuzhiyun * nr_dirtied_pause so as not to exceed max_pause. When this happens,
1472*4882a593Smuzhiyun * nr_dirtied_pause will be "dancing" with task_ratelimit.
1473*4882a593Smuzhiyun * 2) limit the target pause time to max_pause/2, so that the normal
1474*4882a593Smuzhiyun * small fluctuations of task_ratelimit won't trigger rule (1) and
1475*4882a593Smuzhiyun * nr_dirtied_pause will remain as stable as dirty_ratelimit.
1476*4882a593Smuzhiyun */
1477*4882a593Smuzhiyun t = min(t, 1 + max_pause / 2);
1478*4882a593Smuzhiyun pages = dirty_ratelimit * t / roundup_pow_of_two(HZ);
1479*4882a593Smuzhiyun
1480*4882a593Smuzhiyun /*
1481*4882a593Smuzhiyun * Tiny nr_dirtied_pause is found to hurt I/O performance in the test
1482*4882a593Smuzhiyun * case fio-mmap-randwrite-64k, which does 16*{sync read, async write}.
1483*4882a593Smuzhiyun * When the 16 consecutive reads are often interrupted by some dirty
1484*4882a593Smuzhiyun * throttling pause during the async writes, cfq will go into idles
1485*4882a593Smuzhiyun * (deadline is fine). So push nr_dirtied_pause as high as possible
1486*4882a593Smuzhiyun * until reaches DIRTY_POLL_THRESH=32 pages.
1487*4882a593Smuzhiyun */
1488*4882a593Smuzhiyun if (pages < DIRTY_POLL_THRESH) {
1489*4882a593Smuzhiyun t = max_pause;
1490*4882a593Smuzhiyun pages = dirty_ratelimit * t / roundup_pow_of_two(HZ);
1491*4882a593Smuzhiyun if (pages > DIRTY_POLL_THRESH) {
1492*4882a593Smuzhiyun pages = DIRTY_POLL_THRESH;
1493*4882a593Smuzhiyun t = HZ * DIRTY_POLL_THRESH / dirty_ratelimit;
1494*4882a593Smuzhiyun }
1495*4882a593Smuzhiyun }
1496*4882a593Smuzhiyun
1497*4882a593Smuzhiyun pause = HZ * pages / (task_ratelimit + 1);
1498*4882a593Smuzhiyun if (pause > max_pause) {
1499*4882a593Smuzhiyun t = max_pause;
1500*4882a593Smuzhiyun pages = task_ratelimit * t / roundup_pow_of_two(HZ);
1501*4882a593Smuzhiyun }
1502*4882a593Smuzhiyun
1503*4882a593Smuzhiyun *nr_dirtied_pause = pages;
1504*4882a593Smuzhiyun /*
1505*4882a593Smuzhiyun * The minimal pause time will normally be half the target pause time.
1506*4882a593Smuzhiyun */
1507*4882a593Smuzhiyun return pages >= DIRTY_POLL_THRESH ? 1 + t / 2 : t;
1508*4882a593Smuzhiyun }
1509*4882a593Smuzhiyun
wb_dirty_limits(struct dirty_throttle_control * dtc)1510*4882a593Smuzhiyun static inline void wb_dirty_limits(struct dirty_throttle_control *dtc)
1511*4882a593Smuzhiyun {
1512*4882a593Smuzhiyun struct bdi_writeback *wb = dtc->wb;
1513*4882a593Smuzhiyun unsigned long wb_reclaimable;
1514*4882a593Smuzhiyun
1515*4882a593Smuzhiyun /*
1516*4882a593Smuzhiyun * wb_thresh is not treated as some limiting factor as
1517*4882a593Smuzhiyun * dirty_thresh, due to reasons
1518*4882a593Smuzhiyun * - in JBOD setup, wb_thresh can fluctuate a lot
1519*4882a593Smuzhiyun * - in a system with HDD and USB key, the USB key may somehow
1520*4882a593Smuzhiyun * go into state (wb_dirty >> wb_thresh) either because
1521*4882a593Smuzhiyun * wb_dirty starts high, or because wb_thresh drops low.
1522*4882a593Smuzhiyun * In this case we don't want to hard throttle the USB key
1523*4882a593Smuzhiyun * dirtiers for 100 seconds until wb_dirty drops under
1524*4882a593Smuzhiyun * wb_thresh. Instead the auxiliary wb control line in
1525*4882a593Smuzhiyun * wb_position_ratio() will let the dirtier task progress
1526*4882a593Smuzhiyun * at some rate <= (write_bw / 2) for bringing down wb_dirty.
1527*4882a593Smuzhiyun */
1528*4882a593Smuzhiyun dtc->wb_thresh = __wb_calc_thresh(dtc);
1529*4882a593Smuzhiyun dtc->wb_bg_thresh = dtc->thresh ?
1530*4882a593Smuzhiyun div_u64((u64)dtc->wb_thresh * dtc->bg_thresh, dtc->thresh) : 0;
1531*4882a593Smuzhiyun
1532*4882a593Smuzhiyun /*
1533*4882a593Smuzhiyun * In order to avoid the stacked BDI deadlock we need
1534*4882a593Smuzhiyun * to ensure we accurately count the 'dirty' pages when
1535*4882a593Smuzhiyun * the threshold is low.
1536*4882a593Smuzhiyun *
1537*4882a593Smuzhiyun * Otherwise it would be possible to get thresh+n pages
1538*4882a593Smuzhiyun * reported dirty, even though there are thresh-m pages
1539*4882a593Smuzhiyun * actually dirty; with m+n sitting in the percpu
1540*4882a593Smuzhiyun * deltas.
1541*4882a593Smuzhiyun */
1542*4882a593Smuzhiyun if (dtc->wb_thresh < 2 * wb_stat_error()) {
1543*4882a593Smuzhiyun wb_reclaimable = wb_stat_sum(wb, WB_RECLAIMABLE);
1544*4882a593Smuzhiyun dtc->wb_dirty = wb_reclaimable + wb_stat_sum(wb, WB_WRITEBACK);
1545*4882a593Smuzhiyun } else {
1546*4882a593Smuzhiyun wb_reclaimable = wb_stat(wb, WB_RECLAIMABLE);
1547*4882a593Smuzhiyun dtc->wb_dirty = wb_reclaimable + wb_stat(wb, WB_WRITEBACK);
1548*4882a593Smuzhiyun }
1549*4882a593Smuzhiyun }
1550*4882a593Smuzhiyun
1551*4882a593Smuzhiyun /*
1552*4882a593Smuzhiyun * balance_dirty_pages() must be called by processes which are generating dirty
1553*4882a593Smuzhiyun * data. It looks at the number of dirty pages in the machine and will force
1554*4882a593Smuzhiyun * the caller to wait once crossing the (background_thresh + dirty_thresh) / 2.
1555*4882a593Smuzhiyun * If we're over `background_thresh' then the writeback threads are woken to
1556*4882a593Smuzhiyun * perform some writeout.
1557*4882a593Smuzhiyun */
balance_dirty_pages(struct bdi_writeback * wb,unsigned long pages_dirtied)1558*4882a593Smuzhiyun static void balance_dirty_pages(struct bdi_writeback *wb,
1559*4882a593Smuzhiyun unsigned long pages_dirtied)
1560*4882a593Smuzhiyun {
1561*4882a593Smuzhiyun struct dirty_throttle_control gdtc_stor = { GDTC_INIT(wb) };
1562*4882a593Smuzhiyun struct dirty_throttle_control mdtc_stor = { MDTC_INIT(wb, &gdtc_stor) };
1563*4882a593Smuzhiyun struct dirty_throttle_control * const gdtc = &gdtc_stor;
1564*4882a593Smuzhiyun struct dirty_throttle_control * const mdtc = mdtc_valid(&mdtc_stor) ?
1565*4882a593Smuzhiyun &mdtc_stor : NULL;
1566*4882a593Smuzhiyun struct dirty_throttle_control *sdtc;
1567*4882a593Smuzhiyun unsigned long nr_reclaimable; /* = file_dirty */
1568*4882a593Smuzhiyun long period;
1569*4882a593Smuzhiyun long pause;
1570*4882a593Smuzhiyun long max_pause;
1571*4882a593Smuzhiyun long min_pause;
1572*4882a593Smuzhiyun int nr_dirtied_pause;
1573*4882a593Smuzhiyun bool dirty_exceeded = false;
1574*4882a593Smuzhiyun unsigned long task_ratelimit;
1575*4882a593Smuzhiyun unsigned long dirty_ratelimit;
1576*4882a593Smuzhiyun struct backing_dev_info *bdi = wb->bdi;
1577*4882a593Smuzhiyun bool strictlimit = bdi->capabilities & BDI_CAP_STRICTLIMIT;
1578*4882a593Smuzhiyun unsigned long start_time = jiffies;
1579*4882a593Smuzhiyun
1580*4882a593Smuzhiyun for (;;) {
1581*4882a593Smuzhiyun unsigned long now = jiffies;
1582*4882a593Smuzhiyun unsigned long dirty, thresh, bg_thresh;
1583*4882a593Smuzhiyun unsigned long m_dirty = 0; /* stop bogus uninit warnings */
1584*4882a593Smuzhiyun unsigned long m_thresh = 0;
1585*4882a593Smuzhiyun unsigned long m_bg_thresh = 0;
1586*4882a593Smuzhiyun
1587*4882a593Smuzhiyun nr_reclaimable = global_node_page_state(NR_FILE_DIRTY);
1588*4882a593Smuzhiyun gdtc->avail = global_dirtyable_memory();
1589*4882a593Smuzhiyun gdtc->dirty = nr_reclaimable + global_node_page_state(NR_WRITEBACK);
1590*4882a593Smuzhiyun
1591*4882a593Smuzhiyun domain_dirty_limits(gdtc);
1592*4882a593Smuzhiyun
1593*4882a593Smuzhiyun if (unlikely(strictlimit)) {
1594*4882a593Smuzhiyun wb_dirty_limits(gdtc);
1595*4882a593Smuzhiyun
1596*4882a593Smuzhiyun dirty = gdtc->wb_dirty;
1597*4882a593Smuzhiyun thresh = gdtc->wb_thresh;
1598*4882a593Smuzhiyun bg_thresh = gdtc->wb_bg_thresh;
1599*4882a593Smuzhiyun } else {
1600*4882a593Smuzhiyun dirty = gdtc->dirty;
1601*4882a593Smuzhiyun thresh = gdtc->thresh;
1602*4882a593Smuzhiyun bg_thresh = gdtc->bg_thresh;
1603*4882a593Smuzhiyun }
1604*4882a593Smuzhiyun
1605*4882a593Smuzhiyun if (mdtc) {
1606*4882a593Smuzhiyun unsigned long filepages, headroom, writeback;
1607*4882a593Smuzhiyun
1608*4882a593Smuzhiyun /*
1609*4882a593Smuzhiyun * If @wb belongs to !root memcg, repeat the same
1610*4882a593Smuzhiyun * basic calculations for the memcg domain.
1611*4882a593Smuzhiyun */
1612*4882a593Smuzhiyun mem_cgroup_wb_stats(wb, &filepages, &headroom,
1613*4882a593Smuzhiyun &mdtc->dirty, &writeback);
1614*4882a593Smuzhiyun mdtc->dirty += writeback;
1615*4882a593Smuzhiyun mdtc_calc_avail(mdtc, filepages, headroom);
1616*4882a593Smuzhiyun
1617*4882a593Smuzhiyun domain_dirty_limits(mdtc);
1618*4882a593Smuzhiyun
1619*4882a593Smuzhiyun if (unlikely(strictlimit)) {
1620*4882a593Smuzhiyun wb_dirty_limits(mdtc);
1621*4882a593Smuzhiyun m_dirty = mdtc->wb_dirty;
1622*4882a593Smuzhiyun m_thresh = mdtc->wb_thresh;
1623*4882a593Smuzhiyun m_bg_thresh = mdtc->wb_bg_thresh;
1624*4882a593Smuzhiyun } else {
1625*4882a593Smuzhiyun m_dirty = mdtc->dirty;
1626*4882a593Smuzhiyun m_thresh = mdtc->thresh;
1627*4882a593Smuzhiyun m_bg_thresh = mdtc->bg_thresh;
1628*4882a593Smuzhiyun }
1629*4882a593Smuzhiyun }
1630*4882a593Smuzhiyun
1631*4882a593Smuzhiyun trace_android_vh_mm_dirty_limits(gdtc, strictlimit, dirty, bg_thresh,
1632*4882a593Smuzhiyun nr_reclaimable, pages_dirtied);
1633*4882a593Smuzhiyun
1634*4882a593Smuzhiyun /*
1635*4882a593Smuzhiyun * Throttle it only when the background writeback cannot
1636*4882a593Smuzhiyun * catch-up. This avoids (excessively) small writeouts
1637*4882a593Smuzhiyun * when the wb limits are ramping up in case of !strictlimit.
1638*4882a593Smuzhiyun *
1639*4882a593Smuzhiyun * In strictlimit case make decision based on the wb counters
1640*4882a593Smuzhiyun * and limits. Small writeouts when the wb limits are ramping
1641*4882a593Smuzhiyun * up are the price we consciously pay for strictlimit-ing.
1642*4882a593Smuzhiyun *
1643*4882a593Smuzhiyun * If memcg domain is in effect, @dirty should be under
1644*4882a593Smuzhiyun * both global and memcg freerun ceilings.
1645*4882a593Smuzhiyun */
1646*4882a593Smuzhiyun if (dirty <= dirty_freerun_ceiling(thresh, bg_thresh) &&
1647*4882a593Smuzhiyun (!mdtc ||
1648*4882a593Smuzhiyun m_dirty <= dirty_freerun_ceiling(m_thresh, m_bg_thresh))) {
1649*4882a593Smuzhiyun unsigned long intv;
1650*4882a593Smuzhiyun unsigned long m_intv;
1651*4882a593Smuzhiyun
1652*4882a593Smuzhiyun free_running:
1653*4882a593Smuzhiyun intv = dirty_poll_interval(dirty, thresh);
1654*4882a593Smuzhiyun m_intv = ULONG_MAX;
1655*4882a593Smuzhiyun
1656*4882a593Smuzhiyun current->dirty_paused_when = now;
1657*4882a593Smuzhiyun current->nr_dirtied = 0;
1658*4882a593Smuzhiyun if (mdtc)
1659*4882a593Smuzhiyun m_intv = dirty_poll_interval(m_dirty, m_thresh);
1660*4882a593Smuzhiyun current->nr_dirtied_pause = min(intv, m_intv);
1661*4882a593Smuzhiyun break;
1662*4882a593Smuzhiyun }
1663*4882a593Smuzhiyun
1664*4882a593Smuzhiyun if (unlikely(!writeback_in_progress(wb)))
1665*4882a593Smuzhiyun wb_start_background_writeback(wb);
1666*4882a593Smuzhiyun
1667*4882a593Smuzhiyun mem_cgroup_flush_foreign(wb);
1668*4882a593Smuzhiyun
1669*4882a593Smuzhiyun /*
1670*4882a593Smuzhiyun * Calculate global domain's pos_ratio and select the
1671*4882a593Smuzhiyun * global dtc by default.
1672*4882a593Smuzhiyun */
1673*4882a593Smuzhiyun if (!strictlimit) {
1674*4882a593Smuzhiyun wb_dirty_limits(gdtc);
1675*4882a593Smuzhiyun
1676*4882a593Smuzhiyun if ((current->flags & PF_LOCAL_THROTTLE) &&
1677*4882a593Smuzhiyun gdtc->wb_dirty <
1678*4882a593Smuzhiyun dirty_freerun_ceiling(gdtc->wb_thresh,
1679*4882a593Smuzhiyun gdtc->wb_bg_thresh))
1680*4882a593Smuzhiyun /*
1681*4882a593Smuzhiyun * LOCAL_THROTTLE tasks must not be throttled
1682*4882a593Smuzhiyun * when below the per-wb freerun ceiling.
1683*4882a593Smuzhiyun */
1684*4882a593Smuzhiyun goto free_running;
1685*4882a593Smuzhiyun }
1686*4882a593Smuzhiyun
1687*4882a593Smuzhiyun dirty_exceeded = (gdtc->wb_dirty > gdtc->wb_thresh) &&
1688*4882a593Smuzhiyun ((gdtc->dirty > gdtc->thresh) || strictlimit);
1689*4882a593Smuzhiyun
1690*4882a593Smuzhiyun wb_position_ratio(gdtc);
1691*4882a593Smuzhiyun sdtc = gdtc;
1692*4882a593Smuzhiyun
1693*4882a593Smuzhiyun if (mdtc) {
1694*4882a593Smuzhiyun /*
1695*4882a593Smuzhiyun * If memcg domain is in effect, calculate its
1696*4882a593Smuzhiyun * pos_ratio. @wb should satisfy constraints from
1697*4882a593Smuzhiyun * both global and memcg domains. Choose the one
1698*4882a593Smuzhiyun * w/ lower pos_ratio.
1699*4882a593Smuzhiyun */
1700*4882a593Smuzhiyun if (!strictlimit) {
1701*4882a593Smuzhiyun wb_dirty_limits(mdtc);
1702*4882a593Smuzhiyun
1703*4882a593Smuzhiyun if ((current->flags & PF_LOCAL_THROTTLE) &&
1704*4882a593Smuzhiyun mdtc->wb_dirty <
1705*4882a593Smuzhiyun dirty_freerun_ceiling(mdtc->wb_thresh,
1706*4882a593Smuzhiyun mdtc->wb_bg_thresh))
1707*4882a593Smuzhiyun /*
1708*4882a593Smuzhiyun * LOCAL_THROTTLE tasks must not be
1709*4882a593Smuzhiyun * throttled when below the per-wb
1710*4882a593Smuzhiyun * freerun ceiling.
1711*4882a593Smuzhiyun */
1712*4882a593Smuzhiyun goto free_running;
1713*4882a593Smuzhiyun }
1714*4882a593Smuzhiyun dirty_exceeded |= (mdtc->wb_dirty > mdtc->wb_thresh) &&
1715*4882a593Smuzhiyun ((mdtc->dirty > mdtc->thresh) || strictlimit);
1716*4882a593Smuzhiyun
1717*4882a593Smuzhiyun wb_position_ratio(mdtc);
1718*4882a593Smuzhiyun if (mdtc->pos_ratio < gdtc->pos_ratio)
1719*4882a593Smuzhiyun sdtc = mdtc;
1720*4882a593Smuzhiyun }
1721*4882a593Smuzhiyun
1722*4882a593Smuzhiyun if (dirty_exceeded && !wb->dirty_exceeded)
1723*4882a593Smuzhiyun wb->dirty_exceeded = 1;
1724*4882a593Smuzhiyun
1725*4882a593Smuzhiyun if (time_is_before_jiffies(wb->bw_time_stamp +
1726*4882a593Smuzhiyun BANDWIDTH_INTERVAL)) {
1727*4882a593Smuzhiyun spin_lock(&wb->list_lock);
1728*4882a593Smuzhiyun __wb_update_bandwidth(gdtc, mdtc, start_time, true);
1729*4882a593Smuzhiyun spin_unlock(&wb->list_lock);
1730*4882a593Smuzhiyun }
1731*4882a593Smuzhiyun
1732*4882a593Smuzhiyun /* throttle according to the chosen dtc */
1733*4882a593Smuzhiyun dirty_ratelimit = wb->dirty_ratelimit;
1734*4882a593Smuzhiyun task_ratelimit = ((u64)dirty_ratelimit * sdtc->pos_ratio) >>
1735*4882a593Smuzhiyun RATELIMIT_CALC_SHIFT;
1736*4882a593Smuzhiyun max_pause = wb_max_pause(wb, sdtc->wb_dirty);
1737*4882a593Smuzhiyun min_pause = wb_min_pause(wb, max_pause,
1738*4882a593Smuzhiyun task_ratelimit, dirty_ratelimit,
1739*4882a593Smuzhiyun &nr_dirtied_pause);
1740*4882a593Smuzhiyun
1741*4882a593Smuzhiyun if (unlikely(task_ratelimit == 0)) {
1742*4882a593Smuzhiyun period = max_pause;
1743*4882a593Smuzhiyun pause = max_pause;
1744*4882a593Smuzhiyun goto pause;
1745*4882a593Smuzhiyun }
1746*4882a593Smuzhiyun period = HZ * pages_dirtied / task_ratelimit;
1747*4882a593Smuzhiyun pause = period;
1748*4882a593Smuzhiyun if (current->dirty_paused_when)
1749*4882a593Smuzhiyun pause -= now - current->dirty_paused_when;
1750*4882a593Smuzhiyun /*
1751*4882a593Smuzhiyun * For less than 1s think time (ext3/4 may block the dirtier
1752*4882a593Smuzhiyun * for up to 800ms from time to time on 1-HDD; so does xfs,
1753*4882a593Smuzhiyun * however at much less frequency), try to compensate it in
1754*4882a593Smuzhiyun * future periods by updating the virtual time; otherwise just
1755*4882a593Smuzhiyun * do a reset, as it may be a light dirtier.
1756*4882a593Smuzhiyun */
1757*4882a593Smuzhiyun if (pause < min_pause) {
1758*4882a593Smuzhiyun trace_balance_dirty_pages(wb,
1759*4882a593Smuzhiyun sdtc->thresh,
1760*4882a593Smuzhiyun sdtc->bg_thresh,
1761*4882a593Smuzhiyun sdtc->dirty,
1762*4882a593Smuzhiyun sdtc->wb_thresh,
1763*4882a593Smuzhiyun sdtc->wb_dirty,
1764*4882a593Smuzhiyun dirty_ratelimit,
1765*4882a593Smuzhiyun task_ratelimit,
1766*4882a593Smuzhiyun pages_dirtied,
1767*4882a593Smuzhiyun period,
1768*4882a593Smuzhiyun min(pause, 0L),
1769*4882a593Smuzhiyun start_time);
1770*4882a593Smuzhiyun if (pause < -HZ) {
1771*4882a593Smuzhiyun current->dirty_paused_when = now;
1772*4882a593Smuzhiyun current->nr_dirtied = 0;
1773*4882a593Smuzhiyun } else if (period) {
1774*4882a593Smuzhiyun current->dirty_paused_when += period;
1775*4882a593Smuzhiyun current->nr_dirtied = 0;
1776*4882a593Smuzhiyun } else if (current->nr_dirtied_pause <= pages_dirtied)
1777*4882a593Smuzhiyun current->nr_dirtied_pause += pages_dirtied;
1778*4882a593Smuzhiyun break;
1779*4882a593Smuzhiyun }
1780*4882a593Smuzhiyun if (unlikely(pause > max_pause)) {
1781*4882a593Smuzhiyun /* for occasional dropped task_ratelimit */
1782*4882a593Smuzhiyun now += min(pause - max_pause, max_pause);
1783*4882a593Smuzhiyun pause = max_pause;
1784*4882a593Smuzhiyun }
1785*4882a593Smuzhiyun
1786*4882a593Smuzhiyun pause:
1787*4882a593Smuzhiyun trace_balance_dirty_pages(wb,
1788*4882a593Smuzhiyun sdtc->thresh,
1789*4882a593Smuzhiyun sdtc->bg_thresh,
1790*4882a593Smuzhiyun sdtc->dirty,
1791*4882a593Smuzhiyun sdtc->wb_thresh,
1792*4882a593Smuzhiyun sdtc->wb_dirty,
1793*4882a593Smuzhiyun dirty_ratelimit,
1794*4882a593Smuzhiyun task_ratelimit,
1795*4882a593Smuzhiyun pages_dirtied,
1796*4882a593Smuzhiyun period,
1797*4882a593Smuzhiyun pause,
1798*4882a593Smuzhiyun start_time);
1799*4882a593Smuzhiyun __set_current_state(TASK_KILLABLE);
1800*4882a593Smuzhiyun wb->dirty_sleep = now;
1801*4882a593Smuzhiyun io_schedule_timeout(pause);
1802*4882a593Smuzhiyun
1803*4882a593Smuzhiyun current->dirty_paused_when = now + pause;
1804*4882a593Smuzhiyun current->nr_dirtied = 0;
1805*4882a593Smuzhiyun current->nr_dirtied_pause = nr_dirtied_pause;
1806*4882a593Smuzhiyun
1807*4882a593Smuzhiyun /*
1808*4882a593Smuzhiyun * This is typically equal to (dirty < thresh) and can also
1809*4882a593Smuzhiyun * keep "1000+ dd on a slow USB stick" under control.
1810*4882a593Smuzhiyun */
1811*4882a593Smuzhiyun if (task_ratelimit)
1812*4882a593Smuzhiyun break;
1813*4882a593Smuzhiyun
1814*4882a593Smuzhiyun /*
1815*4882a593Smuzhiyun * In the case of an unresponding NFS server and the NFS dirty
1816*4882a593Smuzhiyun * pages exceeds dirty_thresh, give the other good wb's a pipe
1817*4882a593Smuzhiyun * to go through, so that tasks on them still remain responsive.
1818*4882a593Smuzhiyun *
1819*4882a593Smuzhiyun * In theory 1 page is enough to keep the consumer-producer
1820*4882a593Smuzhiyun * pipe going: the flusher cleans 1 page => the task dirties 1
1821*4882a593Smuzhiyun * more page. However wb_dirty has accounting errors. So use
1822*4882a593Smuzhiyun * the larger and more IO friendly wb_stat_error.
1823*4882a593Smuzhiyun */
1824*4882a593Smuzhiyun if (sdtc->wb_dirty <= wb_stat_error())
1825*4882a593Smuzhiyun break;
1826*4882a593Smuzhiyun
1827*4882a593Smuzhiyun if (fatal_signal_pending(current))
1828*4882a593Smuzhiyun break;
1829*4882a593Smuzhiyun }
1830*4882a593Smuzhiyun
1831*4882a593Smuzhiyun if (!dirty_exceeded && wb->dirty_exceeded)
1832*4882a593Smuzhiyun wb->dirty_exceeded = 0;
1833*4882a593Smuzhiyun
1834*4882a593Smuzhiyun if (writeback_in_progress(wb))
1835*4882a593Smuzhiyun return;
1836*4882a593Smuzhiyun
1837*4882a593Smuzhiyun /*
1838*4882a593Smuzhiyun * In laptop mode, we wait until hitting the higher threshold before
1839*4882a593Smuzhiyun * starting background writeout, and then write out all the way down
1840*4882a593Smuzhiyun * to the lower threshold. So slow writers cause minimal disk activity.
1841*4882a593Smuzhiyun *
1842*4882a593Smuzhiyun * In normal mode, we start background writeout at the lower
1843*4882a593Smuzhiyun * background_thresh, to keep the amount of dirty memory low.
1844*4882a593Smuzhiyun */
1845*4882a593Smuzhiyun if (laptop_mode)
1846*4882a593Smuzhiyun return;
1847*4882a593Smuzhiyun
1848*4882a593Smuzhiyun if (nr_reclaimable > gdtc->bg_thresh)
1849*4882a593Smuzhiyun wb_start_background_writeback(wb);
1850*4882a593Smuzhiyun }
1851*4882a593Smuzhiyun
1852*4882a593Smuzhiyun static DEFINE_PER_CPU(int, bdp_ratelimits);
1853*4882a593Smuzhiyun
1854*4882a593Smuzhiyun /*
1855*4882a593Smuzhiyun * Normal tasks are throttled by
1856*4882a593Smuzhiyun * loop {
1857*4882a593Smuzhiyun * dirty tsk->nr_dirtied_pause pages;
1858*4882a593Smuzhiyun * take a snap in balance_dirty_pages();
1859*4882a593Smuzhiyun * }
1860*4882a593Smuzhiyun * However there is a worst case. If every task exit immediately when dirtied
1861*4882a593Smuzhiyun * (tsk->nr_dirtied_pause - 1) pages, balance_dirty_pages() will never be
1862*4882a593Smuzhiyun * called to throttle the page dirties. The solution is to save the not yet
1863*4882a593Smuzhiyun * throttled page dirties in dirty_throttle_leaks on task exit and charge them
1864*4882a593Smuzhiyun * randomly into the running tasks. This works well for the above worst case,
1865*4882a593Smuzhiyun * as the new task will pick up and accumulate the old task's leaked dirty
1866*4882a593Smuzhiyun * count and eventually get throttled.
1867*4882a593Smuzhiyun */
1868*4882a593Smuzhiyun DEFINE_PER_CPU(int, dirty_throttle_leaks) = 0;
1869*4882a593Smuzhiyun
1870*4882a593Smuzhiyun /**
1871*4882a593Smuzhiyun * balance_dirty_pages_ratelimited - balance dirty memory state
1872*4882a593Smuzhiyun * @mapping: address_space which was dirtied
1873*4882a593Smuzhiyun *
1874*4882a593Smuzhiyun * Processes which are dirtying memory should call in here once for each page
1875*4882a593Smuzhiyun * which was newly dirtied. The function will periodically check the system's
1876*4882a593Smuzhiyun * dirty state and will initiate writeback if needed.
1877*4882a593Smuzhiyun *
1878*4882a593Smuzhiyun * On really big machines, get_writeback_state is expensive, so try to avoid
1879*4882a593Smuzhiyun * calling it too often (ratelimiting). But once we're over the dirty memory
1880*4882a593Smuzhiyun * limit we decrease the ratelimiting by a lot, to prevent individual processes
1881*4882a593Smuzhiyun * from overshooting the limit by (ratelimit_pages) each.
1882*4882a593Smuzhiyun */
balance_dirty_pages_ratelimited(struct address_space * mapping)1883*4882a593Smuzhiyun void balance_dirty_pages_ratelimited(struct address_space *mapping)
1884*4882a593Smuzhiyun {
1885*4882a593Smuzhiyun struct inode *inode = mapping->host;
1886*4882a593Smuzhiyun struct backing_dev_info *bdi = inode_to_bdi(inode);
1887*4882a593Smuzhiyun struct bdi_writeback *wb = NULL;
1888*4882a593Smuzhiyun int ratelimit;
1889*4882a593Smuzhiyun int *p;
1890*4882a593Smuzhiyun
1891*4882a593Smuzhiyun if (!(bdi->capabilities & BDI_CAP_WRITEBACK))
1892*4882a593Smuzhiyun return;
1893*4882a593Smuzhiyun
1894*4882a593Smuzhiyun if (inode_cgwb_enabled(inode))
1895*4882a593Smuzhiyun wb = wb_get_create_current(bdi, GFP_KERNEL);
1896*4882a593Smuzhiyun if (!wb)
1897*4882a593Smuzhiyun wb = &bdi->wb;
1898*4882a593Smuzhiyun
1899*4882a593Smuzhiyun ratelimit = current->nr_dirtied_pause;
1900*4882a593Smuzhiyun if (wb->dirty_exceeded)
1901*4882a593Smuzhiyun ratelimit = min(ratelimit, 32 >> (PAGE_SHIFT - 10));
1902*4882a593Smuzhiyun
1903*4882a593Smuzhiyun preempt_disable();
1904*4882a593Smuzhiyun /*
1905*4882a593Smuzhiyun * This prevents one CPU to accumulate too many dirtied pages without
1906*4882a593Smuzhiyun * calling into balance_dirty_pages(), which can happen when there are
1907*4882a593Smuzhiyun * 1000+ tasks, all of them start dirtying pages at exactly the same
1908*4882a593Smuzhiyun * time, hence all honoured too large initial task->nr_dirtied_pause.
1909*4882a593Smuzhiyun */
1910*4882a593Smuzhiyun p = this_cpu_ptr(&bdp_ratelimits);
1911*4882a593Smuzhiyun if (unlikely(current->nr_dirtied >= ratelimit))
1912*4882a593Smuzhiyun *p = 0;
1913*4882a593Smuzhiyun else if (unlikely(*p >= ratelimit_pages)) {
1914*4882a593Smuzhiyun *p = 0;
1915*4882a593Smuzhiyun ratelimit = 0;
1916*4882a593Smuzhiyun }
1917*4882a593Smuzhiyun /*
1918*4882a593Smuzhiyun * Pick up the dirtied pages by the exited tasks. This avoids lots of
1919*4882a593Smuzhiyun * short-lived tasks (eg. gcc invocations in a kernel build) escaping
1920*4882a593Smuzhiyun * the dirty throttling and livelock other long-run dirtiers.
1921*4882a593Smuzhiyun */
1922*4882a593Smuzhiyun p = this_cpu_ptr(&dirty_throttle_leaks);
1923*4882a593Smuzhiyun if (*p > 0 && current->nr_dirtied < ratelimit) {
1924*4882a593Smuzhiyun unsigned long nr_pages_dirtied;
1925*4882a593Smuzhiyun nr_pages_dirtied = min(*p, ratelimit - current->nr_dirtied);
1926*4882a593Smuzhiyun *p -= nr_pages_dirtied;
1927*4882a593Smuzhiyun current->nr_dirtied += nr_pages_dirtied;
1928*4882a593Smuzhiyun }
1929*4882a593Smuzhiyun preempt_enable();
1930*4882a593Smuzhiyun
1931*4882a593Smuzhiyun if (unlikely(current->nr_dirtied >= ratelimit))
1932*4882a593Smuzhiyun balance_dirty_pages(wb, current->nr_dirtied);
1933*4882a593Smuzhiyun
1934*4882a593Smuzhiyun wb_put(wb);
1935*4882a593Smuzhiyun }
1936*4882a593Smuzhiyun EXPORT_SYMBOL(balance_dirty_pages_ratelimited);
1937*4882a593Smuzhiyun
1938*4882a593Smuzhiyun /**
1939*4882a593Smuzhiyun * wb_over_bg_thresh - does @wb need to be written back?
1940*4882a593Smuzhiyun * @wb: bdi_writeback of interest
1941*4882a593Smuzhiyun *
1942*4882a593Smuzhiyun * Determines whether background writeback should keep writing @wb or it's
1943*4882a593Smuzhiyun * clean enough.
1944*4882a593Smuzhiyun *
1945*4882a593Smuzhiyun * Return: %true if writeback should continue.
1946*4882a593Smuzhiyun */
wb_over_bg_thresh(struct bdi_writeback * wb)1947*4882a593Smuzhiyun bool wb_over_bg_thresh(struct bdi_writeback *wb)
1948*4882a593Smuzhiyun {
1949*4882a593Smuzhiyun struct dirty_throttle_control gdtc_stor = { GDTC_INIT(wb) };
1950*4882a593Smuzhiyun struct dirty_throttle_control mdtc_stor = { MDTC_INIT(wb, &gdtc_stor) };
1951*4882a593Smuzhiyun struct dirty_throttle_control * const gdtc = &gdtc_stor;
1952*4882a593Smuzhiyun struct dirty_throttle_control * const mdtc = mdtc_valid(&mdtc_stor) ?
1953*4882a593Smuzhiyun &mdtc_stor : NULL;
1954*4882a593Smuzhiyun unsigned long reclaimable;
1955*4882a593Smuzhiyun unsigned long thresh;
1956*4882a593Smuzhiyun
1957*4882a593Smuzhiyun /*
1958*4882a593Smuzhiyun * Similar to balance_dirty_pages() but ignores pages being written
1959*4882a593Smuzhiyun * as we're trying to decide whether to put more under writeback.
1960*4882a593Smuzhiyun */
1961*4882a593Smuzhiyun gdtc->avail = global_dirtyable_memory();
1962*4882a593Smuzhiyun gdtc->dirty = global_node_page_state(NR_FILE_DIRTY);
1963*4882a593Smuzhiyun domain_dirty_limits(gdtc);
1964*4882a593Smuzhiyun
1965*4882a593Smuzhiyun if (gdtc->dirty > gdtc->bg_thresh)
1966*4882a593Smuzhiyun return true;
1967*4882a593Smuzhiyun
1968*4882a593Smuzhiyun thresh = wb_calc_thresh(gdtc->wb, gdtc->bg_thresh);
1969*4882a593Smuzhiyun if (thresh < 2 * wb_stat_error())
1970*4882a593Smuzhiyun reclaimable = wb_stat_sum(wb, WB_RECLAIMABLE);
1971*4882a593Smuzhiyun else
1972*4882a593Smuzhiyun reclaimable = wb_stat(wb, WB_RECLAIMABLE);
1973*4882a593Smuzhiyun
1974*4882a593Smuzhiyun if (reclaimable > thresh)
1975*4882a593Smuzhiyun return true;
1976*4882a593Smuzhiyun
1977*4882a593Smuzhiyun if (mdtc) {
1978*4882a593Smuzhiyun unsigned long filepages, headroom, writeback;
1979*4882a593Smuzhiyun
1980*4882a593Smuzhiyun mem_cgroup_wb_stats(wb, &filepages, &headroom, &mdtc->dirty,
1981*4882a593Smuzhiyun &writeback);
1982*4882a593Smuzhiyun mdtc_calc_avail(mdtc, filepages, headroom);
1983*4882a593Smuzhiyun domain_dirty_limits(mdtc); /* ditto, ignore writeback */
1984*4882a593Smuzhiyun
1985*4882a593Smuzhiyun if (mdtc->dirty > mdtc->bg_thresh)
1986*4882a593Smuzhiyun return true;
1987*4882a593Smuzhiyun
1988*4882a593Smuzhiyun thresh = wb_calc_thresh(mdtc->wb, mdtc->bg_thresh);
1989*4882a593Smuzhiyun if (thresh < 2 * wb_stat_error())
1990*4882a593Smuzhiyun reclaimable = wb_stat_sum(wb, WB_RECLAIMABLE);
1991*4882a593Smuzhiyun else
1992*4882a593Smuzhiyun reclaimable = wb_stat(wb, WB_RECLAIMABLE);
1993*4882a593Smuzhiyun
1994*4882a593Smuzhiyun if (reclaimable > thresh)
1995*4882a593Smuzhiyun return true;
1996*4882a593Smuzhiyun }
1997*4882a593Smuzhiyun
1998*4882a593Smuzhiyun return false;
1999*4882a593Smuzhiyun }
2000*4882a593Smuzhiyun
2001*4882a593Smuzhiyun /*
2002*4882a593Smuzhiyun * sysctl handler for /proc/sys/vm/dirty_writeback_centisecs
2003*4882a593Smuzhiyun */
dirty_writeback_centisecs_handler(struct ctl_table * table,int write,void * buffer,size_t * length,loff_t * ppos)2004*4882a593Smuzhiyun int dirty_writeback_centisecs_handler(struct ctl_table *table, int write,
2005*4882a593Smuzhiyun void *buffer, size_t *length, loff_t *ppos)
2006*4882a593Smuzhiyun {
2007*4882a593Smuzhiyun unsigned int old_interval = dirty_writeback_interval;
2008*4882a593Smuzhiyun int ret;
2009*4882a593Smuzhiyun
2010*4882a593Smuzhiyun ret = proc_dointvec(table, write, buffer, length, ppos);
2011*4882a593Smuzhiyun
2012*4882a593Smuzhiyun /*
2013*4882a593Smuzhiyun * Writing 0 to dirty_writeback_interval will disable periodic writeback
2014*4882a593Smuzhiyun * and a different non-zero value will wakeup the writeback threads.
2015*4882a593Smuzhiyun * wb_wakeup_delayed() would be more appropriate, but it's a pain to
2016*4882a593Smuzhiyun * iterate over all bdis and wbs.
2017*4882a593Smuzhiyun * The reason we do this is to make the change take effect immediately.
2018*4882a593Smuzhiyun */
2019*4882a593Smuzhiyun if (!ret && write && dirty_writeback_interval &&
2020*4882a593Smuzhiyun dirty_writeback_interval != old_interval)
2021*4882a593Smuzhiyun wakeup_flusher_threads(WB_REASON_PERIODIC);
2022*4882a593Smuzhiyun
2023*4882a593Smuzhiyun return ret;
2024*4882a593Smuzhiyun }
2025*4882a593Smuzhiyun
2026*4882a593Smuzhiyun #ifdef CONFIG_BLOCK
laptop_mode_timer_fn(struct timer_list * t)2027*4882a593Smuzhiyun void laptop_mode_timer_fn(struct timer_list *t)
2028*4882a593Smuzhiyun {
2029*4882a593Smuzhiyun struct backing_dev_info *backing_dev_info =
2030*4882a593Smuzhiyun from_timer(backing_dev_info, t, laptop_mode_wb_timer);
2031*4882a593Smuzhiyun
2032*4882a593Smuzhiyun wakeup_flusher_threads_bdi(backing_dev_info, WB_REASON_LAPTOP_TIMER);
2033*4882a593Smuzhiyun }
2034*4882a593Smuzhiyun
2035*4882a593Smuzhiyun /*
2036*4882a593Smuzhiyun * We've spun up the disk and we're in laptop mode: schedule writeback
2037*4882a593Smuzhiyun * of all dirty data a few seconds from now. If the flush is already scheduled
2038*4882a593Smuzhiyun * then push it back - the user is still using the disk.
2039*4882a593Smuzhiyun */
laptop_io_completion(struct backing_dev_info * info)2040*4882a593Smuzhiyun void laptop_io_completion(struct backing_dev_info *info)
2041*4882a593Smuzhiyun {
2042*4882a593Smuzhiyun mod_timer(&info->laptop_mode_wb_timer, jiffies + laptop_mode);
2043*4882a593Smuzhiyun }
2044*4882a593Smuzhiyun
2045*4882a593Smuzhiyun /*
2046*4882a593Smuzhiyun * We're in laptop mode and we've just synced. The sync's writes will have
2047*4882a593Smuzhiyun * caused another writeback to be scheduled by laptop_io_completion.
2048*4882a593Smuzhiyun * Nothing needs to be written back anymore, so we unschedule the writeback.
2049*4882a593Smuzhiyun */
laptop_sync_completion(void)2050*4882a593Smuzhiyun void laptop_sync_completion(void)
2051*4882a593Smuzhiyun {
2052*4882a593Smuzhiyun struct backing_dev_info *bdi;
2053*4882a593Smuzhiyun
2054*4882a593Smuzhiyun rcu_read_lock();
2055*4882a593Smuzhiyun
2056*4882a593Smuzhiyun list_for_each_entry_rcu(bdi, &bdi_list, bdi_list)
2057*4882a593Smuzhiyun del_timer(&bdi->laptop_mode_wb_timer);
2058*4882a593Smuzhiyun
2059*4882a593Smuzhiyun rcu_read_unlock();
2060*4882a593Smuzhiyun }
2061*4882a593Smuzhiyun #endif
2062*4882a593Smuzhiyun
2063*4882a593Smuzhiyun /*
2064*4882a593Smuzhiyun * If ratelimit_pages is too high then we can get into dirty-data overload
2065*4882a593Smuzhiyun * if a large number of processes all perform writes at the same time.
2066*4882a593Smuzhiyun * If it is too low then SMP machines will call the (expensive)
2067*4882a593Smuzhiyun * get_writeback_state too often.
2068*4882a593Smuzhiyun *
2069*4882a593Smuzhiyun * Here we set ratelimit_pages to a level which ensures that when all CPUs are
2070*4882a593Smuzhiyun * dirtying in parallel, we cannot go more than 3% (1/32) over the dirty memory
2071*4882a593Smuzhiyun * thresholds.
2072*4882a593Smuzhiyun */
2073*4882a593Smuzhiyun
writeback_set_ratelimit(void)2074*4882a593Smuzhiyun void writeback_set_ratelimit(void)
2075*4882a593Smuzhiyun {
2076*4882a593Smuzhiyun struct wb_domain *dom = &global_wb_domain;
2077*4882a593Smuzhiyun unsigned long background_thresh;
2078*4882a593Smuzhiyun unsigned long dirty_thresh;
2079*4882a593Smuzhiyun
2080*4882a593Smuzhiyun global_dirty_limits(&background_thresh, &dirty_thresh);
2081*4882a593Smuzhiyun dom->dirty_limit = dirty_thresh;
2082*4882a593Smuzhiyun ratelimit_pages = dirty_thresh / (num_online_cpus() * 32);
2083*4882a593Smuzhiyun if (ratelimit_pages < 16)
2084*4882a593Smuzhiyun ratelimit_pages = 16;
2085*4882a593Smuzhiyun }
2086*4882a593Smuzhiyun
page_writeback_cpu_online(unsigned int cpu)2087*4882a593Smuzhiyun static int page_writeback_cpu_online(unsigned int cpu)
2088*4882a593Smuzhiyun {
2089*4882a593Smuzhiyun writeback_set_ratelimit();
2090*4882a593Smuzhiyun return 0;
2091*4882a593Smuzhiyun }
2092*4882a593Smuzhiyun
2093*4882a593Smuzhiyun /*
2094*4882a593Smuzhiyun * Called early on to tune the page writeback dirty limits.
2095*4882a593Smuzhiyun *
2096*4882a593Smuzhiyun * We used to scale dirty pages according to how total memory
2097*4882a593Smuzhiyun * related to pages that could be allocated for buffers.
2098*4882a593Smuzhiyun *
2099*4882a593Smuzhiyun * However, that was when we used "dirty_ratio" to scale with
2100*4882a593Smuzhiyun * all memory, and we don't do that any more. "dirty_ratio"
2101*4882a593Smuzhiyun * is now applied to total non-HIGHPAGE memory, and as such we can't
2102*4882a593Smuzhiyun * get into the old insane situation any more where we had
2103*4882a593Smuzhiyun * large amounts of dirty pages compared to a small amount of
2104*4882a593Smuzhiyun * non-HIGHMEM memory.
2105*4882a593Smuzhiyun *
2106*4882a593Smuzhiyun * But we might still want to scale the dirty_ratio by how
2107*4882a593Smuzhiyun * much memory the box has..
2108*4882a593Smuzhiyun */
page_writeback_init(void)2109*4882a593Smuzhiyun void __init page_writeback_init(void)
2110*4882a593Smuzhiyun {
2111*4882a593Smuzhiyun BUG_ON(wb_domain_init(&global_wb_domain, GFP_KERNEL));
2112*4882a593Smuzhiyun
2113*4882a593Smuzhiyun cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "mm/writeback:online",
2114*4882a593Smuzhiyun page_writeback_cpu_online, NULL);
2115*4882a593Smuzhiyun cpuhp_setup_state(CPUHP_MM_WRITEBACK_DEAD, "mm/writeback:dead", NULL,
2116*4882a593Smuzhiyun page_writeback_cpu_online);
2117*4882a593Smuzhiyun }
2118*4882a593Smuzhiyun
2119*4882a593Smuzhiyun /**
2120*4882a593Smuzhiyun * tag_pages_for_writeback - tag pages to be written by write_cache_pages
2121*4882a593Smuzhiyun * @mapping: address space structure to write
2122*4882a593Smuzhiyun * @start: starting page index
2123*4882a593Smuzhiyun * @end: ending page index (inclusive)
2124*4882a593Smuzhiyun *
2125*4882a593Smuzhiyun * This function scans the page range from @start to @end (inclusive) and tags
2126*4882a593Smuzhiyun * all pages that have DIRTY tag set with a special TOWRITE tag. The idea is
2127*4882a593Smuzhiyun * that write_cache_pages (or whoever calls this function) will then use
2128*4882a593Smuzhiyun * TOWRITE tag to identify pages eligible for writeback. This mechanism is
2129*4882a593Smuzhiyun * used to avoid livelocking of writeback by a process steadily creating new
2130*4882a593Smuzhiyun * dirty pages in the file (thus it is important for this function to be quick
2131*4882a593Smuzhiyun * so that it can tag pages faster than a dirtying process can create them).
2132*4882a593Smuzhiyun */
tag_pages_for_writeback(struct address_space * mapping,pgoff_t start,pgoff_t end)2133*4882a593Smuzhiyun void tag_pages_for_writeback(struct address_space *mapping,
2134*4882a593Smuzhiyun pgoff_t start, pgoff_t end)
2135*4882a593Smuzhiyun {
2136*4882a593Smuzhiyun XA_STATE(xas, &mapping->i_pages, start);
2137*4882a593Smuzhiyun unsigned int tagged = 0;
2138*4882a593Smuzhiyun void *page;
2139*4882a593Smuzhiyun
2140*4882a593Smuzhiyun xas_lock_irq(&xas);
2141*4882a593Smuzhiyun xas_for_each_marked(&xas, page, end, PAGECACHE_TAG_DIRTY) {
2142*4882a593Smuzhiyun xas_set_mark(&xas, PAGECACHE_TAG_TOWRITE);
2143*4882a593Smuzhiyun if (++tagged % XA_CHECK_SCHED)
2144*4882a593Smuzhiyun continue;
2145*4882a593Smuzhiyun
2146*4882a593Smuzhiyun xas_pause(&xas);
2147*4882a593Smuzhiyun xas_unlock_irq(&xas);
2148*4882a593Smuzhiyun cond_resched();
2149*4882a593Smuzhiyun xas_lock_irq(&xas);
2150*4882a593Smuzhiyun }
2151*4882a593Smuzhiyun xas_unlock_irq(&xas);
2152*4882a593Smuzhiyun }
2153*4882a593Smuzhiyun EXPORT_SYMBOL(tag_pages_for_writeback);
2154*4882a593Smuzhiyun
2155*4882a593Smuzhiyun /**
2156*4882a593Smuzhiyun * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
2157*4882a593Smuzhiyun * @mapping: address space structure to write
2158*4882a593Smuzhiyun * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2159*4882a593Smuzhiyun * @writepage: function called for each page
2160*4882a593Smuzhiyun * @data: data passed to writepage function
2161*4882a593Smuzhiyun *
2162*4882a593Smuzhiyun * If a page is already under I/O, write_cache_pages() skips it, even
2163*4882a593Smuzhiyun * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2164*4882a593Smuzhiyun * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2165*4882a593Smuzhiyun * and msync() need to guarantee that all the data which was dirty at the time
2166*4882a593Smuzhiyun * the call was made get new I/O started against them. If wbc->sync_mode is
2167*4882a593Smuzhiyun * WB_SYNC_ALL then we were called for data integrity and we must wait for
2168*4882a593Smuzhiyun * existing IO to complete.
2169*4882a593Smuzhiyun *
2170*4882a593Smuzhiyun * To avoid livelocks (when other process dirties new pages), we first tag
2171*4882a593Smuzhiyun * pages which should be written back with TOWRITE tag and only then start
2172*4882a593Smuzhiyun * writing them. For data-integrity sync we have to be careful so that we do
2173*4882a593Smuzhiyun * not miss some pages (e.g., because some other process has cleared TOWRITE
2174*4882a593Smuzhiyun * tag we set). The rule we follow is that TOWRITE tag can be cleared only
2175*4882a593Smuzhiyun * by the process clearing the DIRTY tag (and submitting the page for IO).
2176*4882a593Smuzhiyun *
2177*4882a593Smuzhiyun * To avoid deadlocks between range_cyclic writeback and callers that hold
2178*4882a593Smuzhiyun * pages in PageWriteback to aggregate IO until write_cache_pages() returns,
2179*4882a593Smuzhiyun * we do not loop back to the start of the file. Doing so causes a page
2180*4882a593Smuzhiyun * lock/page writeback access order inversion - we should only ever lock
2181*4882a593Smuzhiyun * multiple pages in ascending page->index order, and looping back to the start
2182*4882a593Smuzhiyun * of the file violates that rule and causes deadlocks.
2183*4882a593Smuzhiyun *
2184*4882a593Smuzhiyun * Return: %0 on success, negative error code otherwise
2185*4882a593Smuzhiyun */
write_cache_pages(struct address_space * mapping,struct writeback_control * wbc,writepage_t writepage,void * data)2186*4882a593Smuzhiyun int write_cache_pages(struct address_space *mapping,
2187*4882a593Smuzhiyun struct writeback_control *wbc, writepage_t writepage,
2188*4882a593Smuzhiyun void *data)
2189*4882a593Smuzhiyun {
2190*4882a593Smuzhiyun int ret = 0;
2191*4882a593Smuzhiyun int done = 0;
2192*4882a593Smuzhiyun int error;
2193*4882a593Smuzhiyun struct pagevec pvec;
2194*4882a593Smuzhiyun int nr_pages;
2195*4882a593Smuzhiyun pgoff_t index;
2196*4882a593Smuzhiyun pgoff_t end; /* Inclusive */
2197*4882a593Smuzhiyun pgoff_t done_index;
2198*4882a593Smuzhiyun int range_whole = 0;
2199*4882a593Smuzhiyun xa_mark_t tag;
2200*4882a593Smuzhiyun
2201*4882a593Smuzhiyun pagevec_init(&pvec);
2202*4882a593Smuzhiyun if (wbc->range_cyclic) {
2203*4882a593Smuzhiyun index = mapping->writeback_index; /* prev offset */
2204*4882a593Smuzhiyun end = -1;
2205*4882a593Smuzhiyun } else {
2206*4882a593Smuzhiyun index = wbc->range_start >> PAGE_SHIFT;
2207*4882a593Smuzhiyun end = wbc->range_end >> PAGE_SHIFT;
2208*4882a593Smuzhiyun if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2209*4882a593Smuzhiyun range_whole = 1;
2210*4882a593Smuzhiyun }
2211*4882a593Smuzhiyun if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) {
2212*4882a593Smuzhiyun tag_pages_for_writeback(mapping, index, end);
2213*4882a593Smuzhiyun tag = PAGECACHE_TAG_TOWRITE;
2214*4882a593Smuzhiyun } else {
2215*4882a593Smuzhiyun tag = PAGECACHE_TAG_DIRTY;
2216*4882a593Smuzhiyun }
2217*4882a593Smuzhiyun done_index = index;
2218*4882a593Smuzhiyun while (!done && (index <= end)) {
2219*4882a593Smuzhiyun int i;
2220*4882a593Smuzhiyun
2221*4882a593Smuzhiyun nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
2222*4882a593Smuzhiyun tag);
2223*4882a593Smuzhiyun if (nr_pages == 0)
2224*4882a593Smuzhiyun break;
2225*4882a593Smuzhiyun
2226*4882a593Smuzhiyun for (i = 0; i < nr_pages; i++) {
2227*4882a593Smuzhiyun struct page *page = pvec.pages[i];
2228*4882a593Smuzhiyun
2229*4882a593Smuzhiyun done_index = page->index;
2230*4882a593Smuzhiyun
2231*4882a593Smuzhiyun lock_page(page);
2232*4882a593Smuzhiyun
2233*4882a593Smuzhiyun /*
2234*4882a593Smuzhiyun * Page truncated or invalidated. We can freely skip it
2235*4882a593Smuzhiyun * then, even for data integrity operations: the page
2236*4882a593Smuzhiyun * has disappeared concurrently, so there could be no
2237*4882a593Smuzhiyun * real expectation of this data interity operation
2238*4882a593Smuzhiyun * even if there is now a new, dirty page at the same
2239*4882a593Smuzhiyun * pagecache address.
2240*4882a593Smuzhiyun */
2241*4882a593Smuzhiyun if (unlikely(page->mapping != mapping)) {
2242*4882a593Smuzhiyun continue_unlock:
2243*4882a593Smuzhiyun unlock_page(page);
2244*4882a593Smuzhiyun continue;
2245*4882a593Smuzhiyun }
2246*4882a593Smuzhiyun
2247*4882a593Smuzhiyun if (!PageDirty(page)) {
2248*4882a593Smuzhiyun /* someone wrote it for us */
2249*4882a593Smuzhiyun goto continue_unlock;
2250*4882a593Smuzhiyun }
2251*4882a593Smuzhiyun
2252*4882a593Smuzhiyun if (PageWriteback(page)) {
2253*4882a593Smuzhiyun if (wbc->sync_mode != WB_SYNC_NONE)
2254*4882a593Smuzhiyun wait_on_page_writeback(page);
2255*4882a593Smuzhiyun else
2256*4882a593Smuzhiyun goto continue_unlock;
2257*4882a593Smuzhiyun }
2258*4882a593Smuzhiyun
2259*4882a593Smuzhiyun BUG_ON(PageWriteback(page));
2260*4882a593Smuzhiyun if (!clear_page_dirty_for_io(page))
2261*4882a593Smuzhiyun goto continue_unlock;
2262*4882a593Smuzhiyun
2263*4882a593Smuzhiyun trace_wbc_writepage(wbc, inode_to_bdi(mapping->host));
2264*4882a593Smuzhiyun error = (*writepage)(page, wbc, data);
2265*4882a593Smuzhiyun if (unlikely(error)) {
2266*4882a593Smuzhiyun /*
2267*4882a593Smuzhiyun * Handle errors according to the type of
2268*4882a593Smuzhiyun * writeback. There's no need to continue for
2269*4882a593Smuzhiyun * background writeback. Just push done_index
2270*4882a593Smuzhiyun * past this page so media errors won't choke
2271*4882a593Smuzhiyun * writeout for the entire file. For integrity
2272*4882a593Smuzhiyun * writeback, we must process the entire dirty
2273*4882a593Smuzhiyun * set regardless of errors because the fs may
2274*4882a593Smuzhiyun * still have state to clear for each page. In
2275*4882a593Smuzhiyun * that case we continue processing and return
2276*4882a593Smuzhiyun * the first error.
2277*4882a593Smuzhiyun */
2278*4882a593Smuzhiyun if (error == AOP_WRITEPAGE_ACTIVATE) {
2279*4882a593Smuzhiyun unlock_page(page);
2280*4882a593Smuzhiyun error = 0;
2281*4882a593Smuzhiyun } else if (wbc->sync_mode != WB_SYNC_ALL) {
2282*4882a593Smuzhiyun ret = error;
2283*4882a593Smuzhiyun done_index = page->index + 1;
2284*4882a593Smuzhiyun done = 1;
2285*4882a593Smuzhiyun break;
2286*4882a593Smuzhiyun }
2287*4882a593Smuzhiyun if (!ret)
2288*4882a593Smuzhiyun ret = error;
2289*4882a593Smuzhiyun }
2290*4882a593Smuzhiyun
2291*4882a593Smuzhiyun /*
2292*4882a593Smuzhiyun * We stop writing back only if we are not doing
2293*4882a593Smuzhiyun * integrity sync. In case of integrity sync we have to
2294*4882a593Smuzhiyun * keep going until we have written all the pages
2295*4882a593Smuzhiyun * we tagged for writeback prior to entering this loop.
2296*4882a593Smuzhiyun */
2297*4882a593Smuzhiyun if (--wbc->nr_to_write <= 0 &&
2298*4882a593Smuzhiyun wbc->sync_mode == WB_SYNC_NONE) {
2299*4882a593Smuzhiyun done = 1;
2300*4882a593Smuzhiyun break;
2301*4882a593Smuzhiyun }
2302*4882a593Smuzhiyun }
2303*4882a593Smuzhiyun pagevec_release(&pvec);
2304*4882a593Smuzhiyun cond_resched();
2305*4882a593Smuzhiyun }
2306*4882a593Smuzhiyun
2307*4882a593Smuzhiyun /*
2308*4882a593Smuzhiyun * If we hit the last page and there is more work to be done: wrap
2309*4882a593Smuzhiyun * back the index back to the start of the file for the next
2310*4882a593Smuzhiyun * time we are called.
2311*4882a593Smuzhiyun */
2312*4882a593Smuzhiyun if (wbc->range_cyclic && !done)
2313*4882a593Smuzhiyun done_index = 0;
2314*4882a593Smuzhiyun if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2315*4882a593Smuzhiyun mapping->writeback_index = done_index;
2316*4882a593Smuzhiyun
2317*4882a593Smuzhiyun return ret;
2318*4882a593Smuzhiyun }
2319*4882a593Smuzhiyun EXPORT_SYMBOL(write_cache_pages);
2320*4882a593Smuzhiyun
2321*4882a593Smuzhiyun /*
2322*4882a593Smuzhiyun * Function used by generic_writepages to call the real writepage
2323*4882a593Smuzhiyun * function and set the mapping flags on error
2324*4882a593Smuzhiyun */
__writepage(struct page * page,struct writeback_control * wbc,void * data)2325*4882a593Smuzhiyun static int __writepage(struct page *page, struct writeback_control *wbc,
2326*4882a593Smuzhiyun void *data)
2327*4882a593Smuzhiyun {
2328*4882a593Smuzhiyun struct address_space *mapping = data;
2329*4882a593Smuzhiyun int ret = mapping->a_ops->writepage(page, wbc);
2330*4882a593Smuzhiyun mapping_set_error(mapping, ret);
2331*4882a593Smuzhiyun return ret;
2332*4882a593Smuzhiyun }
2333*4882a593Smuzhiyun
2334*4882a593Smuzhiyun /**
2335*4882a593Smuzhiyun * generic_writepages - walk the list of dirty pages of the given address space and writepage() all of them.
2336*4882a593Smuzhiyun * @mapping: address space structure to write
2337*4882a593Smuzhiyun * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2338*4882a593Smuzhiyun *
2339*4882a593Smuzhiyun * This is a library function, which implements the writepages()
2340*4882a593Smuzhiyun * address_space_operation.
2341*4882a593Smuzhiyun *
2342*4882a593Smuzhiyun * Return: %0 on success, negative error code otherwise
2343*4882a593Smuzhiyun */
generic_writepages(struct address_space * mapping,struct writeback_control * wbc)2344*4882a593Smuzhiyun int generic_writepages(struct address_space *mapping,
2345*4882a593Smuzhiyun struct writeback_control *wbc)
2346*4882a593Smuzhiyun {
2347*4882a593Smuzhiyun struct blk_plug plug;
2348*4882a593Smuzhiyun int ret;
2349*4882a593Smuzhiyun
2350*4882a593Smuzhiyun /* deal with chardevs and other special file */
2351*4882a593Smuzhiyun if (!mapping->a_ops->writepage)
2352*4882a593Smuzhiyun return 0;
2353*4882a593Smuzhiyun
2354*4882a593Smuzhiyun blk_start_plug(&plug);
2355*4882a593Smuzhiyun ret = write_cache_pages(mapping, wbc, __writepage, mapping);
2356*4882a593Smuzhiyun blk_finish_plug(&plug);
2357*4882a593Smuzhiyun return ret;
2358*4882a593Smuzhiyun }
2359*4882a593Smuzhiyun
2360*4882a593Smuzhiyun EXPORT_SYMBOL(generic_writepages);
2361*4882a593Smuzhiyun
do_writepages(struct address_space * mapping,struct writeback_control * wbc)2362*4882a593Smuzhiyun int do_writepages(struct address_space *mapping, struct writeback_control *wbc)
2363*4882a593Smuzhiyun {
2364*4882a593Smuzhiyun int ret;
2365*4882a593Smuzhiyun
2366*4882a593Smuzhiyun if (wbc->nr_to_write <= 0)
2367*4882a593Smuzhiyun return 0;
2368*4882a593Smuzhiyun while (1) {
2369*4882a593Smuzhiyun if (mapping->a_ops->writepages)
2370*4882a593Smuzhiyun ret = mapping->a_ops->writepages(mapping, wbc);
2371*4882a593Smuzhiyun else
2372*4882a593Smuzhiyun ret = generic_writepages(mapping, wbc);
2373*4882a593Smuzhiyun if ((ret != -ENOMEM) || (wbc->sync_mode != WB_SYNC_ALL))
2374*4882a593Smuzhiyun break;
2375*4882a593Smuzhiyun cond_resched();
2376*4882a593Smuzhiyun congestion_wait(BLK_RW_ASYNC, HZ/50);
2377*4882a593Smuzhiyun }
2378*4882a593Smuzhiyun return ret;
2379*4882a593Smuzhiyun }
2380*4882a593Smuzhiyun
2381*4882a593Smuzhiyun /**
2382*4882a593Smuzhiyun * write_one_page - write out a single page and wait on I/O
2383*4882a593Smuzhiyun * @page: the page to write
2384*4882a593Smuzhiyun *
2385*4882a593Smuzhiyun * The page must be locked by the caller and will be unlocked upon return.
2386*4882a593Smuzhiyun *
2387*4882a593Smuzhiyun * Note that the mapping's AS_EIO/AS_ENOSPC flags will be cleared when this
2388*4882a593Smuzhiyun * function returns.
2389*4882a593Smuzhiyun *
2390*4882a593Smuzhiyun * Return: %0 on success, negative error code otherwise
2391*4882a593Smuzhiyun */
write_one_page(struct page * page)2392*4882a593Smuzhiyun int write_one_page(struct page *page)
2393*4882a593Smuzhiyun {
2394*4882a593Smuzhiyun struct address_space *mapping = page->mapping;
2395*4882a593Smuzhiyun int ret = 0;
2396*4882a593Smuzhiyun struct writeback_control wbc = {
2397*4882a593Smuzhiyun .sync_mode = WB_SYNC_ALL,
2398*4882a593Smuzhiyun .nr_to_write = 1,
2399*4882a593Smuzhiyun };
2400*4882a593Smuzhiyun
2401*4882a593Smuzhiyun BUG_ON(!PageLocked(page));
2402*4882a593Smuzhiyun
2403*4882a593Smuzhiyun wait_on_page_writeback(page);
2404*4882a593Smuzhiyun
2405*4882a593Smuzhiyun if (clear_page_dirty_for_io(page)) {
2406*4882a593Smuzhiyun get_page(page);
2407*4882a593Smuzhiyun ret = mapping->a_ops->writepage(page, &wbc);
2408*4882a593Smuzhiyun if (ret == 0)
2409*4882a593Smuzhiyun wait_on_page_writeback(page);
2410*4882a593Smuzhiyun put_page(page);
2411*4882a593Smuzhiyun } else {
2412*4882a593Smuzhiyun unlock_page(page);
2413*4882a593Smuzhiyun }
2414*4882a593Smuzhiyun
2415*4882a593Smuzhiyun if (!ret)
2416*4882a593Smuzhiyun ret = filemap_check_errors(mapping);
2417*4882a593Smuzhiyun return ret;
2418*4882a593Smuzhiyun }
2419*4882a593Smuzhiyun EXPORT_SYMBOL(write_one_page);
2420*4882a593Smuzhiyun
2421*4882a593Smuzhiyun /*
2422*4882a593Smuzhiyun * For address_spaces which do not use buffers nor write back.
2423*4882a593Smuzhiyun */
__set_page_dirty_no_writeback(struct page * page)2424*4882a593Smuzhiyun int __set_page_dirty_no_writeback(struct page *page)
2425*4882a593Smuzhiyun {
2426*4882a593Smuzhiyun if (!PageDirty(page))
2427*4882a593Smuzhiyun return !TestSetPageDirty(page);
2428*4882a593Smuzhiyun return 0;
2429*4882a593Smuzhiyun }
2430*4882a593Smuzhiyun
2431*4882a593Smuzhiyun /*
2432*4882a593Smuzhiyun * Helper function for set_page_dirty family.
2433*4882a593Smuzhiyun *
2434*4882a593Smuzhiyun * Caller must hold lock_page_memcg().
2435*4882a593Smuzhiyun *
2436*4882a593Smuzhiyun * NOTE: This relies on being atomic wrt interrupts.
2437*4882a593Smuzhiyun */
account_page_dirtied(struct page * page,struct address_space * mapping)2438*4882a593Smuzhiyun void account_page_dirtied(struct page *page, struct address_space *mapping)
2439*4882a593Smuzhiyun {
2440*4882a593Smuzhiyun struct inode *inode = mapping->host;
2441*4882a593Smuzhiyun
2442*4882a593Smuzhiyun trace_writeback_dirty_page(page, mapping);
2443*4882a593Smuzhiyun
2444*4882a593Smuzhiyun if (mapping_can_writeback(mapping)) {
2445*4882a593Smuzhiyun struct bdi_writeback *wb;
2446*4882a593Smuzhiyun
2447*4882a593Smuzhiyun inode_attach_wb(inode, page);
2448*4882a593Smuzhiyun wb = inode_to_wb(inode);
2449*4882a593Smuzhiyun
2450*4882a593Smuzhiyun __inc_lruvec_page_state(page, NR_FILE_DIRTY);
2451*4882a593Smuzhiyun __inc_zone_page_state(page, NR_ZONE_WRITE_PENDING);
2452*4882a593Smuzhiyun __inc_node_page_state(page, NR_DIRTIED);
2453*4882a593Smuzhiyun inc_wb_stat(wb, WB_RECLAIMABLE);
2454*4882a593Smuzhiyun inc_wb_stat(wb, WB_DIRTIED);
2455*4882a593Smuzhiyun task_io_account_write(PAGE_SIZE);
2456*4882a593Smuzhiyun current->nr_dirtied++;
2457*4882a593Smuzhiyun this_cpu_inc(bdp_ratelimits);
2458*4882a593Smuzhiyun
2459*4882a593Smuzhiyun mem_cgroup_track_foreign_dirty(page, wb);
2460*4882a593Smuzhiyun }
2461*4882a593Smuzhiyun }
2462*4882a593Smuzhiyun
2463*4882a593Smuzhiyun /*
2464*4882a593Smuzhiyun * Helper function for deaccounting dirty page without writeback.
2465*4882a593Smuzhiyun *
2466*4882a593Smuzhiyun * Caller must hold lock_page_memcg().
2467*4882a593Smuzhiyun */
account_page_cleaned(struct page * page,struct address_space * mapping,struct bdi_writeback * wb)2468*4882a593Smuzhiyun void account_page_cleaned(struct page *page, struct address_space *mapping,
2469*4882a593Smuzhiyun struct bdi_writeback *wb)
2470*4882a593Smuzhiyun {
2471*4882a593Smuzhiyun if (mapping_can_writeback(mapping)) {
2472*4882a593Smuzhiyun dec_lruvec_page_state(page, NR_FILE_DIRTY);
2473*4882a593Smuzhiyun dec_zone_page_state(page, NR_ZONE_WRITE_PENDING);
2474*4882a593Smuzhiyun dec_wb_stat(wb, WB_RECLAIMABLE);
2475*4882a593Smuzhiyun task_io_account_cancelled_write(PAGE_SIZE);
2476*4882a593Smuzhiyun }
2477*4882a593Smuzhiyun }
2478*4882a593Smuzhiyun
2479*4882a593Smuzhiyun /*
2480*4882a593Smuzhiyun * For address_spaces which do not use buffers. Just tag the page as dirty in
2481*4882a593Smuzhiyun * the xarray.
2482*4882a593Smuzhiyun *
2483*4882a593Smuzhiyun * This is also used when a single buffer is being dirtied: we want to set the
2484*4882a593Smuzhiyun * page dirty in that case, but not all the buffers. This is a "bottom-up"
2485*4882a593Smuzhiyun * dirtying, whereas __set_page_dirty_buffers() is a "top-down" dirtying.
2486*4882a593Smuzhiyun *
2487*4882a593Smuzhiyun * The caller must ensure this doesn't race with truncation. Most will simply
2488*4882a593Smuzhiyun * hold the page lock, but e.g. zap_pte_range() calls with the page mapped and
2489*4882a593Smuzhiyun * the pte lock held, which also locks out truncation.
2490*4882a593Smuzhiyun */
__set_page_dirty_nobuffers(struct page * page)2491*4882a593Smuzhiyun int __set_page_dirty_nobuffers(struct page *page)
2492*4882a593Smuzhiyun {
2493*4882a593Smuzhiyun lock_page_memcg(page);
2494*4882a593Smuzhiyun if (!TestSetPageDirty(page)) {
2495*4882a593Smuzhiyun struct address_space *mapping = page_mapping(page);
2496*4882a593Smuzhiyun unsigned long flags;
2497*4882a593Smuzhiyun
2498*4882a593Smuzhiyun if (!mapping) {
2499*4882a593Smuzhiyun unlock_page_memcg(page);
2500*4882a593Smuzhiyun return 1;
2501*4882a593Smuzhiyun }
2502*4882a593Smuzhiyun
2503*4882a593Smuzhiyun xa_lock_irqsave(&mapping->i_pages, flags);
2504*4882a593Smuzhiyun BUG_ON(page_mapping(page) != mapping);
2505*4882a593Smuzhiyun WARN_ON_ONCE(!PagePrivate(page) && !PageUptodate(page));
2506*4882a593Smuzhiyun account_page_dirtied(page, mapping);
2507*4882a593Smuzhiyun __xa_set_mark(&mapping->i_pages, page_index(page),
2508*4882a593Smuzhiyun PAGECACHE_TAG_DIRTY);
2509*4882a593Smuzhiyun xa_unlock_irqrestore(&mapping->i_pages, flags);
2510*4882a593Smuzhiyun unlock_page_memcg(page);
2511*4882a593Smuzhiyun
2512*4882a593Smuzhiyun if (mapping->host) {
2513*4882a593Smuzhiyun /* !PageAnon && !swapper_space */
2514*4882a593Smuzhiyun __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
2515*4882a593Smuzhiyun }
2516*4882a593Smuzhiyun return 1;
2517*4882a593Smuzhiyun }
2518*4882a593Smuzhiyun unlock_page_memcg(page);
2519*4882a593Smuzhiyun return 0;
2520*4882a593Smuzhiyun }
2521*4882a593Smuzhiyun EXPORT_SYMBOL(__set_page_dirty_nobuffers);
2522*4882a593Smuzhiyun
2523*4882a593Smuzhiyun /*
2524*4882a593Smuzhiyun * Call this whenever redirtying a page, to de-account the dirty counters
2525*4882a593Smuzhiyun * (NR_DIRTIED, WB_DIRTIED, tsk->nr_dirtied), so that they match the written
2526*4882a593Smuzhiyun * counters (NR_WRITTEN, WB_WRITTEN) in long term. The mismatches will lead to
2527*4882a593Smuzhiyun * systematic errors in balanced_dirty_ratelimit and the dirty pages position
2528*4882a593Smuzhiyun * control.
2529*4882a593Smuzhiyun */
account_page_redirty(struct page * page)2530*4882a593Smuzhiyun void account_page_redirty(struct page *page)
2531*4882a593Smuzhiyun {
2532*4882a593Smuzhiyun struct address_space *mapping = page->mapping;
2533*4882a593Smuzhiyun
2534*4882a593Smuzhiyun if (mapping && mapping_can_writeback(mapping)) {
2535*4882a593Smuzhiyun struct inode *inode = mapping->host;
2536*4882a593Smuzhiyun struct bdi_writeback *wb;
2537*4882a593Smuzhiyun struct wb_lock_cookie cookie = {};
2538*4882a593Smuzhiyun
2539*4882a593Smuzhiyun wb = unlocked_inode_to_wb_begin(inode, &cookie);
2540*4882a593Smuzhiyun current->nr_dirtied--;
2541*4882a593Smuzhiyun dec_node_page_state(page, NR_DIRTIED);
2542*4882a593Smuzhiyun dec_wb_stat(wb, WB_DIRTIED);
2543*4882a593Smuzhiyun unlocked_inode_to_wb_end(inode, &cookie);
2544*4882a593Smuzhiyun }
2545*4882a593Smuzhiyun }
2546*4882a593Smuzhiyun EXPORT_SYMBOL(account_page_redirty);
2547*4882a593Smuzhiyun
2548*4882a593Smuzhiyun /*
2549*4882a593Smuzhiyun * When a writepage implementation decides that it doesn't want to write this
2550*4882a593Smuzhiyun * page for some reason, it should redirty the locked page via
2551*4882a593Smuzhiyun * redirty_page_for_writepage() and it should then unlock the page and return 0
2552*4882a593Smuzhiyun */
redirty_page_for_writepage(struct writeback_control * wbc,struct page * page)2553*4882a593Smuzhiyun int redirty_page_for_writepage(struct writeback_control *wbc, struct page *page)
2554*4882a593Smuzhiyun {
2555*4882a593Smuzhiyun int ret;
2556*4882a593Smuzhiyun
2557*4882a593Smuzhiyun wbc->pages_skipped++;
2558*4882a593Smuzhiyun ret = __set_page_dirty_nobuffers(page);
2559*4882a593Smuzhiyun account_page_redirty(page);
2560*4882a593Smuzhiyun return ret;
2561*4882a593Smuzhiyun }
2562*4882a593Smuzhiyun EXPORT_SYMBOL(redirty_page_for_writepage);
2563*4882a593Smuzhiyun
2564*4882a593Smuzhiyun /*
2565*4882a593Smuzhiyun * Dirty a page.
2566*4882a593Smuzhiyun *
2567*4882a593Smuzhiyun * For pages with a mapping this should be done under the page lock
2568*4882a593Smuzhiyun * for the benefit of asynchronous memory errors who prefer a consistent
2569*4882a593Smuzhiyun * dirty state. This rule can be broken in some special cases,
2570*4882a593Smuzhiyun * but should be better not to.
2571*4882a593Smuzhiyun *
2572*4882a593Smuzhiyun * If the mapping doesn't provide a set_page_dirty a_op, then
2573*4882a593Smuzhiyun * just fall through and assume that it wants buffer_heads.
2574*4882a593Smuzhiyun */
set_page_dirty(struct page * page)2575*4882a593Smuzhiyun int set_page_dirty(struct page *page)
2576*4882a593Smuzhiyun {
2577*4882a593Smuzhiyun struct address_space *mapping = page_mapping(page);
2578*4882a593Smuzhiyun
2579*4882a593Smuzhiyun page = compound_head(page);
2580*4882a593Smuzhiyun if (likely(mapping)) {
2581*4882a593Smuzhiyun int (*spd)(struct page *) = mapping->a_ops->set_page_dirty;
2582*4882a593Smuzhiyun /*
2583*4882a593Smuzhiyun * readahead/lru_deactivate_page could remain
2584*4882a593Smuzhiyun * PG_readahead/PG_reclaim due to race with end_page_writeback
2585*4882a593Smuzhiyun * About readahead, if the page is written, the flags would be
2586*4882a593Smuzhiyun * reset. So no problem.
2587*4882a593Smuzhiyun * About lru_deactivate_page, if the page is redirty, the flag
2588*4882a593Smuzhiyun * will be reset. So no problem. but if the page is used by readahead
2589*4882a593Smuzhiyun * it will confuse readahead and make it restart the size rampup
2590*4882a593Smuzhiyun * process. But it's a trivial problem.
2591*4882a593Smuzhiyun */
2592*4882a593Smuzhiyun if (PageReclaim(page))
2593*4882a593Smuzhiyun ClearPageReclaim(page);
2594*4882a593Smuzhiyun #ifdef CONFIG_BLOCK
2595*4882a593Smuzhiyun if (!spd)
2596*4882a593Smuzhiyun spd = __set_page_dirty_buffers;
2597*4882a593Smuzhiyun #endif
2598*4882a593Smuzhiyun return (*spd)(page);
2599*4882a593Smuzhiyun }
2600*4882a593Smuzhiyun if (!PageDirty(page)) {
2601*4882a593Smuzhiyun if (!TestSetPageDirty(page))
2602*4882a593Smuzhiyun return 1;
2603*4882a593Smuzhiyun }
2604*4882a593Smuzhiyun return 0;
2605*4882a593Smuzhiyun }
2606*4882a593Smuzhiyun EXPORT_SYMBOL(set_page_dirty);
2607*4882a593Smuzhiyun
2608*4882a593Smuzhiyun /*
2609*4882a593Smuzhiyun * set_page_dirty() is racy if the caller has no reference against
2610*4882a593Smuzhiyun * page->mapping->host, and if the page is unlocked. This is because another
2611*4882a593Smuzhiyun * CPU could truncate the page off the mapping and then free the mapping.
2612*4882a593Smuzhiyun *
2613*4882a593Smuzhiyun * Usually, the page _is_ locked, or the caller is a user-space process which
2614*4882a593Smuzhiyun * holds a reference on the inode by having an open file.
2615*4882a593Smuzhiyun *
2616*4882a593Smuzhiyun * In other cases, the page should be locked before running set_page_dirty().
2617*4882a593Smuzhiyun */
set_page_dirty_lock(struct page * page)2618*4882a593Smuzhiyun int set_page_dirty_lock(struct page *page)
2619*4882a593Smuzhiyun {
2620*4882a593Smuzhiyun int ret;
2621*4882a593Smuzhiyun
2622*4882a593Smuzhiyun lock_page(page);
2623*4882a593Smuzhiyun ret = set_page_dirty(page);
2624*4882a593Smuzhiyun unlock_page(page);
2625*4882a593Smuzhiyun return ret;
2626*4882a593Smuzhiyun }
2627*4882a593Smuzhiyun EXPORT_SYMBOL(set_page_dirty_lock);
2628*4882a593Smuzhiyun
2629*4882a593Smuzhiyun /*
2630*4882a593Smuzhiyun * This cancels just the dirty bit on the kernel page itself, it does NOT
2631*4882a593Smuzhiyun * actually remove dirty bits on any mmap's that may be around. It also
2632*4882a593Smuzhiyun * leaves the page tagged dirty, so any sync activity will still find it on
2633*4882a593Smuzhiyun * the dirty lists, and in particular, clear_page_dirty_for_io() will still
2634*4882a593Smuzhiyun * look at the dirty bits in the VM.
2635*4882a593Smuzhiyun *
2636*4882a593Smuzhiyun * Doing this should *normally* only ever be done when a page is truncated,
2637*4882a593Smuzhiyun * and is not actually mapped anywhere at all. However, fs/buffer.c does
2638*4882a593Smuzhiyun * this when it notices that somebody has cleaned out all the buffers on a
2639*4882a593Smuzhiyun * page without actually doing it through the VM. Can you say "ext3 is
2640*4882a593Smuzhiyun * horribly ugly"? Thought you could.
2641*4882a593Smuzhiyun */
__cancel_dirty_page(struct page * page)2642*4882a593Smuzhiyun void __cancel_dirty_page(struct page *page)
2643*4882a593Smuzhiyun {
2644*4882a593Smuzhiyun struct address_space *mapping = page_mapping(page);
2645*4882a593Smuzhiyun
2646*4882a593Smuzhiyun if (mapping_can_writeback(mapping)) {
2647*4882a593Smuzhiyun struct inode *inode = mapping->host;
2648*4882a593Smuzhiyun struct bdi_writeback *wb;
2649*4882a593Smuzhiyun struct wb_lock_cookie cookie = {};
2650*4882a593Smuzhiyun
2651*4882a593Smuzhiyun lock_page_memcg(page);
2652*4882a593Smuzhiyun wb = unlocked_inode_to_wb_begin(inode, &cookie);
2653*4882a593Smuzhiyun
2654*4882a593Smuzhiyun if (TestClearPageDirty(page))
2655*4882a593Smuzhiyun account_page_cleaned(page, mapping, wb);
2656*4882a593Smuzhiyun
2657*4882a593Smuzhiyun unlocked_inode_to_wb_end(inode, &cookie);
2658*4882a593Smuzhiyun unlock_page_memcg(page);
2659*4882a593Smuzhiyun } else {
2660*4882a593Smuzhiyun ClearPageDirty(page);
2661*4882a593Smuzhiyun }
2662*4882a593Smuzhiyun }
2663*4882a593Smuzhiyun EXPORT_SYMBOL(__cancel_dirty_page);
2664*4882a593Smuzhiyun
2665*4882a593Smuzhiyun /*
2666*4882a593Smuzhiyun * Clear a page's dirty flag, while caring for dirty memory accounting.
2667*4882a593Smuzhiyun * Returns true if the page was previously dirty.
2668*4882a593Smuzhiyun *
2669*4882a593Smuzhiyun * This is for preparing to put the page under writeout. We leave the page
2670*4882a593Smuzhiyun * tagged as dirty in the xarray so that a concurrent write-for-sync
2671*4882a593Smuzhiyun * can discover it via a PAGECACHE_TAG_DIRTY walk. The ->writepage
2672*4882a593Smuzhiyun * implementation will run either set_page_writeback() or set_page_dirty(),
2673*4882a593Smuzhiyun * at which stage we bring the page's dirty flag and xarray dirty tag
2674*4882a593Smuzhiyun * back into sync.
2675*4882a593Smuzhiyun *
2676*4882a593Smuzhiyun * This incoherency between the page's dirty flag and xarray tag is
2677*4882a593Smuzhiyun * unfortunate, but it only exists while the page is locked.
2678*4882a593Smuzhiyun */
clear_page_dirty_for_io(struct page * page)2679*4882a593Smuzhiyun int clear_page_dirty_for_io(struct page *page)
2680*4882a593Smuzhiyun {
2681*4882a593Smuzhiyun struct address_space *mapping = page_mapping(page);
2682*4882a593Smuzhiyun int ret = 0;
2683*4882a593Smuzhiyun
2684*4882a593Smuzhiyun VM_BUG_ON_PAGE(!PageLocked(page), page);
2685*4882a593Smuzhiyun
2686*4882a593Smuzhiyun if (mapping && mapping_can_writeback(mapping)) {
2687*4882a593Smuzhiyun struct inode *inode = mapping->host;
2688*4882a593Smuzhiyun struct bdi_writeback *wb;
2689*4882a593Smuzhiyun struct wb_lock_cookie cookie = {};
2690*4882a593Smuzhiyun
2691*4882a593Smuzhiyun /*
2692*4882a593Smuzhiyun * Yes, Virginia, this is indeed insane.
2693*4882a593Smuzhiyun *
2694*4882a593Smuzhiyun * We use this sequence to make sure that
2695*4882a593Smuzhiyun * (a) we account for dirty stats properly
2696*4882a593Smuzhiyun * (b) we tell the low-level filesystem to
2697*4882a593Smuzhiyun * mark the whole page dirty if it was
2698*4882a593Smuzhiyun * dirty in a pagetable. Only to then
2699*4882a593Smuzhiyun * (c) clean the page again and return 1 to
2700*4882a593Smuzhiyun * cause the writeback.
2701*4882a593Smuzhiyun *
2702*4882a593Smuzhiyun * This way we avoid all nasty races with the
2703*4882a593Smuzhiyun * dirty bit in multiple places and clearing
2704*4882a593Smuzhiyun * them concurrently from different threads.
2705*4882a593Smuzhiyun *
2706*4882a593Smuzhiyun * Note! Normally the "set_page_dirty(page)"
2707*4882a593Smuzhiyun * has no effect on the actual dirty bit - since
2708*4882a593Smuzhiyun * that will already usually be set. But we
2709*4882a593Smuzhiyun * need the side effects, and it can help us
2710*4882a593Smuzhiyun * avoid races.
2711*4882a593Smuzhiyun *
2712*4882a593Smuzhiyun * We basically use the page "master dirty bit"
2713*4882a593Smuzhiyun * as a serialization point for all the different
2714*4882a593Smuzhiyun * threads doing their things.
2715*4882a593Smuzhiyun */
2716*4882a593Smuzhiyun if (page_mkclean(page))
2717*4882a593Smuzhiyun set_page_dirty(page);
2718*4882a593Smuzhiyun /*
2719*4882a593Smuzhiyun * We carefully synchronise fault handlers against
2720*4882a593Smuzhiyun * installing a dirty pte and marking the page dirty
2721*4882a593Smuzhiyun * at this point. We do this by having them hold the
2722*4882a593Smuzhiyun * page lock while dirtying the page, and pages are
2723*4882a593Smuzhiyun * always locked coming in here, so we get the desired
2724*4882a593Smuzhiyun * exclusion.
2725*4882a593Smuzhiyun */
2726*4882a593Smuzhiyun wb = unlocked_inode_to_wb_begin(inode, &cookie);
2727*4882a593Smuzhiyun if (TestClearPageDirty(page)) {
2728*4882a593Smuzhiyun dec_lruvec_page_state(page, NR_FILE_DIRTY);
2729*4882a593Smuzhiyun dec_zone_page_state(page, NR_ZONE_WRITE_PENDING);
2730*4882a593Smuzhiyun dec_wb_stat(wb, WB_RECLAIMABLE);
2731*4882a593Smuzhiyun ret = 1;
2732*4882a593Smuzhiyun }
2733*4882a593Smuzhiyun unlocked_inode_to_wb_end(inode, &cookie);
2734*4882a593Smuzhiyun return ret;
2735*4882a593Smuzhiyun }
2736*4882a593Smuzhiyun return TestClearPageDirty(page);
2737*4882a593Smuzhiyun }
2738*4882a593Smuzhiyun EXPORT_SYMBOL(clear_page_dirty_for_io);
2739*4882a593Smuzhiyun
test_clear_page_writeback(struct page * page)2740*4882a593Smuzhiyun int test_clear_page_writeback(struct page *page)
2741*4882a593Smuzhiyun {
2742*4882a593Smuzhiyun struct address_space *mapping = page_mapping(page);
2743*4882a593Smuzhiyun struct mem_cgroup *memcg;
2744*4882a593Smuzhiyun struct lruvec *lruvec;
2745*4882a593Smuzhiyun int ret;
2746*4882a593Smuzhiyun
2747*4882a593Smuzhiyun memcg = lock_page_memcg(page);
2748*4882a593Smuzhiyun lruvec = mem_cgroup_page_lruvec(page, page_pgdat(page));
2749*4882a593Smuzhiyun if (mapping && mapping_use_writeback_tags(mapping)) {
2750*4882a593Smuzhiyun struct inode *inode = mapping->host;
2751*4882a593Smuzhiyun struct backing_dev_info *bdi = inode_to_bdi(inode);
2752*4882a593Smuzhiyun unsigned long flags;
2753*4882a593Smuzhiyun
2754*4882a593Smuzhiyun xa_lock_irqsave(&mapping->i_pages, flags);
2755*4882a593Smuzhiyun ret = TestClearPageWriteback(page);
2756*4882a593Smuzhiyun if (ret) {
2757*4882a593Smuzhiyun __xa_clear_mark(&mapping->i_pages, page_index(page),
2758*4882a593Smuzhiyun PAGECACHE_TAG_WRITEBACK);
2759*4882a593Smuzhiyun if (bdi->capabilities & BDI_CAP_WRITEBACK_ACCT) {
2760*4882a593Smuzhiyun struct bdi_writeback *wb = inode_to_wb(inode);
2761*4882a593Smuzhiyun
2762*4882a593Smuzhiyun dec_wb_stat(wb, WB_WRITEBACK);
2763*4882a593Smuzhiyun __wb_writeout_inc(wb);
2764*4882a593Smuzhiyun }
2765*4882a593Smuzhiyun }
2766*4882a593Smuzhiyun
2767*4882a593Smuzhiyun if (mapping->host && !mapping_tagged(mapping,
2768*4882a593Smuzhiyun PAGECACHE_TAG_WRITEBACK))
2769*4882a593Smuzhiyun sb_clear_inode_writeback(mapping->host);
2770*4882a593Smuzhiyun
2771*4882a593Smuzhiyun xa_unlock_irqrestore(&mapping->i_pages, flags);
2772*4882a593Smuzhiyun } else {
2773*4882a593Smuzhiyun ret = TestClearPageWriteback(page);
2774*4882a593Smuzhiyun }
2775*4882a593Smuzhiyun if (ret) {
2776*4882a593Smuzhiyun dec_lruvec_state(lruvec, NR_WRITEBACK);
2777*4882a593Smuzhiyun dec_zone_page_state(page, NR_ZONE_WRITE_PENDING);
2778*4882a593Smuzhiyun inc_node_page_state(page, NR_WRITTEN);
2779*4882a593Smuzhiyun }
2780*4882a593Smuzhiyun __unlock_page_memcg(memcg);
2781*4882a593Smuzhiyun return ret;
2782*4882a593Smuzhiyun }
2783*4882a593Smuzhiyun
__test_set_page_writeback(struct page * page,bool keep_write)2784*4882a593Smuzhiyun int __test_set_page_writeback(struct page *page, bool keep_write)
2785*4882a593Smuzhiyun {
2786*4882a593Smuzhiyun struct address_space *mapping = page_mapping(page);
2787*4882a593Smuzhiyun int ret, access_ret;
2788*4882a593Smuzhiyun
2789*4882a593Smuzhiyun lock_page_memcg(page);
2790*4882a593Smuzhiyun if (mapping && mapping_use_writeback_tags(mapping)) {
2791*4882a593Smuzhiyun XA_STATE(xas, &mapping->i_pages, page_index(page));
2792*4882a593Smuzhiyun struct inode *inode = mapping->host;
2793*4882a593Smuzhiyun struct backing_dev_info *bdi = inode_to_bdi(inode);
2794*4882a593Smuzhiyun unsigned long flags;
2795*4882a593Smuzhiyun
2796*4882a593Smuzhiyun xas_lock_irqsave(&xas, flags);
2797*4882a593Smuzhiyun xas_load(&xas);
2798*4882a593Smuzhiyun ret = TestSetPageWriteback(page);
2799*4882a593Smuzhiyun if (!ret) {
2800*4882a593Smuzhiyun bool on_wblist;
2801*4882a593Smuzhiyun
2802*4882a593Smuzhiyun on_wblist = mapping_tagged(mapping,
2803*4882a593Smuzhiyun PAGECACHE_TAG_WRITEBACK);
2804*4882a593Smuzhiyun
2805*4882a593Smuzhiyun xas_set_mark(&xas, PAGECACHE_TAG_WRITEBACK);
2806*4882a593Smuzhiyun if (bdi->capabilities & BDI_CAP_WRITEBACK_ACCT)
2807*4882a593Smuzhiyun inc_wb_stat(inode_to_wb(inode), WB_WRITEBACK);
2808*4882a593Smuzhiyun
2809*4882a593Smuzhiyun /*
2810*4882a593Smuzhiyun * We can come through here when swapping anonymous
2811*4882a593Smuzhiyun * pages, so we don't necessarily have an inode to track
2812*4882a593Smuzhiyun * for sync.
2813*4882a593Smuzhiyun */
2814*4882a593Smuzhiyun if (mapping->host && !on_wblist)
2815*4882a593Smuzhiyun sb_mark_inode_writeback(mapping->host);
2816*4882a593Smuzhiyun }
2817*4882a593Smuzhiyun if (!PageDirty(page))
2818*4882a593Smuzhiyun xas_clear_mark(&xas, PAGECACHE_TAG_DIRTY);
2819*4882a593Smuzhiyun if (!keep_write)
2820*4882a593Smuzhiyun xas_clear_mark(&xas, PAGECACHE_TAG_TOWRITE);
2821*4882a593Smuzhiyun xas_unlock_irqrestore(&xas, flags);
2822*4882a593Smuzhiyun } else {
2823*4882a593Smuzhiyun ret = TestSetPageWriteback(page);
2824*4882a593Smuzhiyun }
2825*4882a593Smuzhiyun if (!ret) {
2826*4882a593Smuzhiyun inc_lruvec_page_state(page, NR_WRITEBACK);
2827*4882a593Smuzhiyun inc_zone_page_state(page, NR_ZONE_WRITE_PENDING);
2828*4882a593Smuzhiyun }
2829*4882a593Smuzhiyun unlock_page_memcg(page);
2830*4882a593Smuzhiyun access_ret = arch_make_page_accessible(page);
2831*4882a593Smuzhiyun /*
2832*4882a593Smuzhiyun * If writeback has been triggered on a page that cannot be made
2833*4882a593Smuzhiyun * accessible, it is too late to recover here.
2834*4882a593Smuzhiyun */
2835*4882a593Smuzhiyun VM_BUG_ON_PAGE(access_ret != 0, page);
2836*4882a593Smuzhiyun
2837*4882a593Smuzhiyun return ret;
2838*4882a593Smuzhiyun
2839*4882a593Smuzhiyun }
2840*4882a593Smuzhiyun EXPORT_SYMBOL(__test_set_page_writeback);
2841*4882a593Smuzhiyun
2842*4882a593Smuzhiyun /*
2843*4882a593Smuzhiyun * Wait for a page to complete writeback
2844*4882a593Smuzhiyun */
wait_on_page_writeback(struct page * page)2845*4882a593Smuzhiyun void wait_on_page_writeback(struct page *page)
2846*4882a593Smuzhiyun {
2847*4882a593Smuzhiyun while (PageWriteback(page)) {
2848*4882a593Smuzhiyun trace_wait_on_page_writeback(page, page_mapping(page));
2849*4882a593Smuzhiyun wait_on_page_bit(page, PG_writeback);
2850*4882a593Smuzhiyun }
2851*4882a593Smuzhiyun }
2852*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(wait_on_page_writeback);
2853*4882a593Smuzhiyun
2854*4882a593Smuzhiyun /**
2855*4882a593Smuzhiyun * wait_for_stable_page() - wait for writeback to finish, if necessary.
2856*4882a593Smuzhiyun * @page: The page to wait on.
2857*4882a593Smuzhiyun *
2858*4882a593Smuzhiyun * This function determines if the given page is related to a backing device
2859*4882a593Smuzhiyun * that requires page contents to be held stable during writeback. If so, then
2860*4882a593Smuzhiyun * it will wait for any pending writeback to complete.
2861*4882a593Smuzhiyun */
wait_for_stable_page(struct page * page)2862*4882a593Smuzhiyun void wait_for_stable_page(struct page *page)
2863*4882a593Smuzhiyun {
2864*4882a593Smuzhiyun page = thp_head(page);
2865*4882a593Smuzhiyun if (page->mapping->host->i_sb->s_iflags & SB_I_STABLE_WRITES)
2866*4882a593Smuzhiyun wait_on_page_writeback(page);
2867*4882a593Smuzhiyun }
2868*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(wait_for_stable_page);
2869