]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - mm/vmstat.c
mm: remove unused pgdat->inactive_ratio
[mirror_ubuntu-bionic-kernel.git] / mm / vmstat.c
CommitLineData
f6ac2354
CL
1/*
2 * linux/mm/vmstat.c
3 *
4 * Manages VM statistics
5 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
2244b95a
CL
6 *
7 * zoned VM statistics
8 * Copyright (C) 2006 Silicon Graphics, Inc.,
9 * Christoph Lameter <christoph@lameter.com>
7cc36bbd 10 * Copyright (C) 2008-2014 Christoph Lameter
f6ac2354 11 */
8f32f7e5 12#include <linux/fs.h>
f6ac2354 13#include <linux/mm.h>
4e950f6f 14#include <linux/err.h>
2244b95a 15#include <linux/module.h>
5a0e3ad6 16#include <linux/slab.h>
df9ecaba 17#include <linux/cpu.h>
7cc36bbd 18#include <linux/cpumask.h>
c748e134 19#include <linux/vmstat.h>
3c486871
AM
20#include <linux/proc_fs.h>
21#include <linux/seq_file.h>
22#include <linux/debugfs.h>
e8edc6e0 23#include <linux/sched.h>
f1a5ab12 24#include <linux/math64.h>
79da826a 25#include <linux/writeback.h>
36deb0be 26#include <linux/compaction.h>
6e543d57 27#include <linux/mm_inline.h>
48c96a36
JK
28#include <linux/page_ext.h>
29#include <linux/page_owner.h>
6e543d57
LD
30
31#include "internal.h"
f6ac2354 32
1d90ca89
KW
33#define NUMA_STATS_THRESHOLD (U16_MAX - 2)
34
f8891e5e
CL
35#ifdef CONFIG_VM_EVENT_COUNTERS
36DEFINE_PER_CPU(struct vm_event_state, vm_event_states) = {{0}};
37EXPORT_PER_CPU_SYMBOL(vm_event_states);
38
31f961a8 39static void sum_vm_events(unsigned long *ret)
f8891e5e 40{
9eccf2a8 41 int cpu;
f8891e5e
CL
42 int i;
43
44 memset(ret, 0, NR_VM_EVENT_ITEMS * sizeof(unsigned long));
45
31f961a8 46 for_each_online_cpu(cpu) {
f8891e5e
CL
47 struct vm_event_state *this = &per_cpu(vm_event_states, cpu);
48
f8891e5e
CL
49 for (i = 0; i < NR_VM_EVENT_ITEMS; i++)
50 ret[i] += this->event[i];
51 }
52}
53
54/*
55 * Accumulate the vm event counters across all CPUs.
56 * The result is unavoidably approximate - it can change
57 * during and after execution of this function.
58*/
59void all_vm_events(unsigned long *ret)
60{
b5be1132 61 get_online_cpus();
31f961a8 62 sum_vm_events(ret);
b5be1132 63 put_online_cpus();
f8891e5e 64}
32dd66fc 65EXPORT_SYMBOL_GPL(all_vm_events);
f8891e5e 66
f8891e5e
CL
67/*
68 * Fold the foreign cpu events into our own.
69 *
70 * This is adding to the events on one processor
71 * but keeps the global counts constant.
72 */
73void vm_events_fold_cpu(int cpu)
74{
75 struct vm_event_state *fold_state = &per_cpu(vm_event_states, cpu);
76 int i;
77
78 for (i = 0; i < NR_VM_EVENT_ITEMS; i++) {
79 count_vm_events(i, fold_state->event[i]);
80 fold_state->event[i] = 0;
81 }
82}
f8891e5e
CL
83
84#endif /* CONFIG_VM_EVENT_COUNTERS */
85
2244b95a
CL
86/*
87 * Manage combined zone based / global counters
88 *
89 * vm_stat contains the global counters
90 */
75ef7184 91atomic_long_t vm_zone_stat[NR_VM_ZONE_STAT_ITEMS] __cacheline_aligned_in_smp;
3a321d2a 92atomic_long_t vm_numa_stat[NR_VM_NUMA_STAT_ITEMS] __cacheline_aligned_in_smp;
75ef7184
MG
93atomic_long_t vm_node_stat[NR_VM_NODE_STAT_ITEMS] __cacheline_aligned_in_smp;
94EXPORT_SYMBOL(vm_zone_stat);
3a321d2a 95EXPORT_SYMBOL(vm_numa_stat);
75ef7184 96EXPORT_SYMBOL(vm_node_stat);
2244b95a
CL
97
98#ifdef CONFIG_SMP
99
b44129b3 100int calculate_pressure_threshold(struct zone *zone)
88f5acf8
MG
101{
102 int threshold;
103 int watermark_distance;
104
105 /*
106 * As vmstats are not up to date, there is drift between the estimated
107 * and real values. For high thresholds and a high number of CPUs, it
108 * is possible for the min watermark to be breached while the estimated
109 * value looks fine. The pressure threshold is a reduced value such
110 * that even the maximum amount of drift will not accidentally breach
111 * the min watermark
112 */
113 watermark_distance = low_wmark_pages(zone) - min_wmark_pages(zone);
114 threshold = max(1, (int)(watermark_distance / num_online_cpus()));
115
116 /*
117 * Maximum threshold is 125
118 */
119 threshold = min(125, threshold);
120
121 return threshold;
122}
123
b44129b3 124int calculate_normal_threshold(struct zone *zone)
df9ecaba
CL
125{
126 int threshold;
127 int mem; /* memory in 128 MB units */
128
129 /*
130 * The threshold scales with the number of processors and the amount
131 * of memory per zone. More memory means that we can defer updates for
132 * longer, more processors could lead to more contention.
133 * fls() is used to have a cheap way of logarithmic scaling.
134 *
135 * Some sample thresholds:
136 *
137 * Threshold Processors (fls) Zonesize fls(mem+1)
138 * ------------------------------------------------------------------
139 * 8 1 1 0.9-1 GB 4
140 * 16 2 2 0.9-1 GB 4
141 * 20 2 2 1-2 GB 5
142 * 24 2 2 2-4 GB 6
143 * 28 2 2 4-8 GB 7
144 * 32 2 2 8-16 GB 8
145 * 4 2 2 <128M 1
146 * 30 4 3 2-4 GB 5
147 * 48 4 3 8-16 GB 8
148 * 32 8 4 1-2 GB 4
149 * 32 8 4 0.9-1GB 4
150 * 10 16 5 <128M 1
151 * 40 16 5 900M 4
152 * 70 64 7 2-4 GB 5
153 * 84 64 7 4-8 GB 6
154 * 108 512 9 4-8 GB 6
155 * 125 1024 10 8-16 GB 8
156 * 125 1024 10 16-32 GB 9
157 */
158
b40da049 159 mem = zone->managed_pages >> (27 - PAGE_SHIFT);
df9ecaba
CL
160
161 threshold = 2 * fls(num_online_cpus()) * (1 + fls(mem));
162
163 /*
164 * Maximum threshold is 125
165 */
166 threshold = min(125, threshold);
167
168 return threshold;
169}
2244b95a
CL
170
171/*
df9ecaba 172 * Refresh the thresholds for each zone.
2244b95a 173 */
a6cccdc3 174void refresh_zone_stat_thresholds(void)
2244b95a 175{
75ef7184 176 struct pglist_data *pgdat;
df9ecaba
CL
177 struct zone *zone;
178 int cpu;
179 int threshold;
180
75ef7184
MG
181 /* Zero current pgdat thresholds */
182 for_each_online_pgdat(pgdat) {
183 for_each_online_cpu(cpu) {
184 per_cpu_ptr(pgdat->per_cpu_nodestats, cpu)->stat_threshold = 0;
185 }
186 }
187
ee99c71c 188 for_each_populated_zone(zone) {
75ef7184 189 struct pglist_data *pgdat = zone->zone_pgdat;
aa454840
CL
190 unsigned long max_drift, tolerate_drift;
191
b44129b3 192 threshold = calculate_normal_threshold(zone);
df9ecaba 193
75ef7184
MG
194 for_each_online_cpu(cpu) {
195 int pgdat_threshold;
196
99dcc3e5
CL
197 per_cpu_ptr(zone->pageset, cpu)->stat_threshold
198 = threshold;
1d90ca89 199
75ef7184
MG
200 /* Base nodestat threshold on the largest populated zone. */
201 pgdat_threshold = per_cpu_ptr(pgdat->per_cpu_nodestats, cpu)->stat_threshold;
202 per_cpu_ptr(pgdat->per_cpu_nodestats, cpu)->stat_threshold
203 = max(threshold, pgdat_threshold);
204 }
205
aa454840
CL
206 /*
207 * Only set percpu_drift_mark if there is a danger that
208 * NR_FREE_PAGES reports the low watermark is ok when in fact
209 * the min watermark could be breached by an allocation
210 */
211 tolerate_drift = low_wmark_pages(zone) - min_wmark_pages(zone);
212 max_drift = num_online_cpus() * threshold;
213 if (max_drift > tolerate_drift)
214 zone->percpu_drift_mark = high_wmark_pages(zone) +
215 max_drift;
df9ecaba 216 }
2244b95a
CL
217}
218
b44129b3
MG
219void set_pgdat_percpu_threshold(pg_data_t *pgdat,
220 int (*calculate_pressure)(struct zone *))
88f5acf8
MG
221{
222 struct zone *zone;
223 int cpu;
224 int threshold;
225 int i;
226
88f5acf8
MG
227 for (i = 0; i < pgdat->nr_zones; i++) {
228 zone = &pgdat->node_zones[i];
229 if (!zone->percpu_drift_mark)
230 continue;
231
b44129b3 232 threshold = (*calculate_pressure)(zone);
1d90ca89 233 for_each_online_cpu(cpu)
88f5acf8
MG
234 per_cpu_ptr(zone->pageset, cpu)->stat_threshold
235 = threshold;
236 }
88f5acf8
MG
237}
238
2244b95a 239/*
bea04b07
JZ
240 * For use when we know that interrupts are disabled,
241 * or when we know that preemption is disabled and that
242 * particular counter cannot be updated from interrupt context.
2244b95a
CL
243 */
244void __mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
6cdb18ad 245 long delta)
2244b95a 246{
12938a92
CL
247 struct per_cpu_pageset __percpu *pcp = zone->pageset;
248 s8 __percpu *p = pcp->vm_stat_diff + item;
2244b95a 249 long x;
12938a92
CL
250 long t;
251
252 x = delta + __this_cpu_read(*p);
2244b95a 253
12938a92 254 t = __this_cpu_read(pcp->stat_threshold);
2244b95a 255
12938a92 256 if (unlikely(x > t || x < -t)) {
2244b95a
CL
257 zone_page_state_add(x, zone, item);
258 x = 0;
259 }
12938a92 260 __this_cpu_write(*p, x);
2244b95a
CL
261}
262EXPORT_SYMBOL(__mod_zone_page_state);
263
75ef7184
MG
264void __mod_node_page_state(struct pglist_data *pgdat, enum node_stat_item item,
265 long delta)
266{
267 struct per_cpu_nodestat __percpu *pcp = pgdat->per_cpu_nodestats;
268 s8 __percpu *p = pcp->vm_node_stat_diff + item;
269 long x;
270 long t;
271
272 x = delta + __this_cpu_read(*p);
273
274 t = __this_cpu_read(pcp->stat_threshold);
275
276 if (unlikely(x > t || x < -t)) {
277 node_page_state_add(x, pgdat, item);
278 x = 0;
279 }
280 __this_cpu_write(*p, x);
281}
282EXPORT_SYMBOL(__mod_node_page_state);
283
2244b95a
CL
284/*
285 * Optimized increment and decrement functions.
286 *
287 * These are only for a single page and therefore can take a struct page *
288 * argument instead of struct zone *. This allows the inclusion of the code
289 * generated for page_zone(page) into the optimized functions.
290 *
291 * No overflow check is necessary and therefore the differential can be
292 * incremented or decremented in place which may allow the compilers to
293 * generate better code.
2244b95a
CL
294 * The increment or decrement is known and therefore one boundary check can
295 * be omitted.
296 *
df9ecaba
CL
297 * NOTE: These functions are very performance sensitive. Change only
298 * with care.
299 *
2244b95a
CL
300 * Some processors have inc/dec instructions that are atomic vs an interrupt.
301 * However, the code must first determine the differential location in a zone
302 * based on the processor number and then inc/dec the counter. There is no
303 * guarantee without disabling preemption that the processor will not change
304 * in between and therefore the atomicity vs. interrupt cannot be exploited
305 * in a useful way here.
306 */
c8785385 307void __inc_zone_state(struct zone *zone, enum zone_stat_item item)
2244b95a 308{
12938a92
CL
309 struct per_cpu_pageset __percpu *pcp = zone->pageset;
310 s8 __percpu *p = pcp->vm_stat_diff + item;
311 s8 v, t;
2244b95a 312
908ee0f1 313 v = __this_cpu_inc_return(*p);
12938a92
CL
314 t = __this_cpu_read(pcp->stat_threshold);
315 if (unlikely(v > t)) {
316 s8 overstep = t >> 1;
df9ecaba 317
12938a92
CL
318 zone_page_state_add(v + overstep, zone, item);
319 __this_cpu_write(*p, -overstep);
2244b95a
CL
320 }
321}
ca889e6c 322
75ef7184
MG
323void __inc_node_state(struct pglist_data *pgdat, enum node_stat_item item)
324{
325 struct per_cpu_nodestat __percpu *pcp = pgdat->per_cpu_nodestats;
326 s8 __percpu *p = pcp->vm_node_stat_diff + item;
327 s8 v, t;
328
329 v = __this_cpu_inc_return(*p);
330 t = __this_cpu_read(pcp->stat_threshold);
331 if (unlikely(v > t)) {
332 s8 overstep = t >> 1;
333
334 node_page_state_add(v + overstep, pgdat, item);
335 __this_cpu_write(*p, -overstep);
336 }
337}
338
ca889e6c
CL
339void __inc_zone_page_state(struct page *page, enum zone_stat_item item)
340{
341 __inc_zone_state(page_zone(page), item);
342}
2244b95a
CL
343EXPORT_SYMBOL(__inc_zone_page_state);
344
75ef7184
MG
345void __inc_node_page_state(struct page *page, enum node_stat_item item)
346{
347 __inc_node_state(page_pgdat(page), item);
348}
349EXPORT_SYMBOL(__inc_node_page_state);
350
c8785385 351void __dec_zone_state(struct zone *zone, enum zone_stat_item item)
2244b95a 352{
12938a92
CL
353 struct per_cpu_pageset __percpu *pcp = zone->pageset;
354 s8 __percpu *p = pcp->vm_stat_diff + item;
355 s8 v, t;
2244b95a 356
908ee0f1 357 v = __this_cpu_dec_return(*p);
12938a92
CL
358 t = __this_cpu_read(pcp->stat_threshold);
359 if (unlikely(v < - t)) {
360 s8 overstep = t >> 1;
2244b95a 361
12938a92
CL
362 zone_page_state_add(v - overstep, zone, item);
363 __this_cpu_write(*p, overstep);
2244b95a
CL
364 }
365}
c8785385 366
75ef7184
MG
367void __dec_node_state(struct pglist_data *pgdat, enum node_stat_item item)
368{
369 struct per_cpu_nodestat __percpu *pcp = pgdat->per_cpu_nodestats;
370 s8 __percpu *p = pcp->vm_node_stat_diff + item;
371 s8 v, t;
372
373 v = __this_cpu_dec_return(*p);
374 t = __this_cpu_read(pcp->stat_threshold);
375 if (unlikely(v < - t)) {
376 s8 overstep = t >> 1;
377
378 node_page_state_add(v - overstep, pgdat, item);
379 __this_cpu_write(*p, overstep);
380 }
381}
382
c8785385
CL
383void __dec_zone_page_state(struct page *page, enum zone_stat_item item)
384{
385 __dec_zone_state(page_zone(page), item);
386}
2244b95a
CL
387EXPORT_SYMBOL(__dec_zone_page_state);
388
75ef7184
MG
389void __dec_node_page_state(struct page *page, enum node_stat_item item)
390{
391 __dec_node_state(page_pgdat(page), item);
392}
393EXPORT_SYMBOL(__dec_node_page_state);
394
4156153c 395#ifdef CONFIG_HAVE_CMPXCHG_LOCAL
7c839120
CL
396/*
397 * If we have cmpxchg_local support then we do not need to incur the overhead
398 * that comes with local_irq_save/restore if we use this_cpu_cmpxchg.
399 *
400 * mod_state() modifies the zone counter state through atomic per cpu
401 * operations.
402 *
403 * Overstep mode specifies how overstep should handled:
404 * 0 No overstepping
405 * 1 Overstepping half of threshold
406 * -1 Overstepping minus half of threshold
407*/
75ef7184
MG
408static inline void mod_zone_state(struct zone *zone,
409 enum zone_stat_item item, long delta, int overstep_mode)
7c839120
CL
410{
411 struct per_cpu_pageset __percpu *pcp = zone->pageset;
412 s8 __percpu *p = pcp->vm_stat_diff + item;
413 long o, n, t, z;
414
415 do {
416 z = 0; /* overflow to zone counters */
417
418 /*
419 * The fetching of the stat_threshold is racy. We may apply
420 * a counter threshold to the wrong the cpu if we get
d3bc2367
CL
421 * rescheduled while executing here. However, the next
422 * counter update will apply the threshold again and
423 * therefore bring the counter under the threshold again.
424 *
425 * Most of the time the thresholds are the same anyways
426 * for all cpus in a zone.
7c839120
CL
427 */
428 t = this_cpu_read(pcp->stat_threshold);
429
430 o = this_cpu_read(*p);
431 n = delta + o;
432
433 if (n > t || n < -t) {
434 int os = overstep_mode * (t >> 1) ;
435
436 /* Overflow must be added to zone counters */
437 z = n + os;
438 n = -os;
439 }
440 } while (this_cpu_cmpxchg(*p, o, n) != o);
441
442 if (z)
443 zone_page_state_add(z, zone, item);
444}
445
446void mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
6cdb18ad 447 long delta)
7c839120 448{
75ef7184 449 mod_zone_state(zone, item, delta, 0);
7c839120
CL
450}
451EXPORT_SYMBOL(mod_zone_page_state);
452
7c839120
CL
453void inc_zone_page_state(struct page *page, enum zone_stat_item item)
454{
75ef7184 455 mod_zone_state(page_zone(page), item, 1, 1);
7c839120
CL
456}
457EXPORT_SYMBOL(inc_zone_page_state);
458
459void dec_zone_page_state(struct page *page, enum zone_stat_item item)
460{
75ef7184 461 mod_zone_state(page_zone(page), item, -1, -1);
7c839120
CL
462}
463EXPORT_SYMBOL(dec_zone_page_state);
75ef7184
MG
464
465static inline void mod_node_state(struct pglist_data *pgdat,
466 enum node_stat_item item, int delta, int overstep_mode)
467{
468 struct per_cpu_nodestat __percpu *pcp = pgdat->per_cpu_nodestats;
469 s8 __percpu *p = pcp->vm_node_stat_diff + item;
470 long o, n, t, z;
471
472 do {
473 z = 0; /* overflow to node counters */
474
475 /*
476 * The fetching of the stat_threshold is racy. We may apply
477 * a counter threshold to the wrong the cpu if we get
478 * rescheduled while executing here. However, the next
479 * counter update will apply the threshold again and
480 * therefore bring the counter under the threshold again.
481 *
482 * Most of the time the thresholds are the same anyways
483 * for all cpus in a node.
484 */
485 t = this_cpu_read(pcp->stat_threshold);
486
487 o = this_cpu_read(*p);
488 n = delta + o;
489
490 if (n > t || n < -t) {
491 int os = overstep_mode * (t >> 1) ;
492
493 /* Overflow must be added to node counters */
494 z = n + os;
495 n = -os;
496 }
497 } while (this_cpu_cmpxchg(*p, o, n) != o);
498
499 if (z)
500 node_page_state_add(z, pgdat, item);
501}
502
503void mod_node_page_state(struct pglist_data *pgdat, enum node_stat_item item,
504 long delta)
505{
506 mod_node_state(pgdat, item, delta, 0);
507}
508EXPORT_SYMBOL(mod_node_page_state);
509
510void inc_node_state(struct pglist_data *pgdat, enum node_stat_item item)
511{
512 mod_node_state(pgdat, item, 1, 1);
513}
514
515void inc_node_page_state(struct page *page, enum node_stat_item item)
516{
517 mod_node_state(page_pgdat(page), item, 1, 1);
518}
519EXPORT_SYMBOL(inc_node_page_state);
520
521void dec_node_page_state(struct page *page, enum node_stat_item item)
522{
523 mod_node_state(page_pgdat(page), item, -1, -1);
524}
525EXPORT_SYMBOL(dec_node_page_state);
7c839120
CL
526#else
527/*
528 * Use interrupt disable to serialize counter updates
529 */
530void mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
6cdb18ad 531 long delta)
7c839120
CL
532{
533 unsigned long flags;
534
535 local_irq_save(flags);
536 __mod_zone_page_state(zone, item, delta);
537 local_irq_restore(flags);
538}
539EXPORT_SYMBOL(mod_zone_page_state);
540
2244b95a
CL
541void inc_zone_page_state(struct page *page, enum zone_stat_item item)
542{
543 unsigned long flags;
544 struct zone *zone;
2244b95a
CL
545
546 zone = page_zone(page);
547 local_irq_save(flags);
ca889e6c 548 __inc_zone_state(zone, item);
2244b95a
CL
549 local_irq_restore(flags);
550}
551EXPORT_SYMBOL(inc_zone_page_state);
552
553void dec_zone_page_state(struct page *page, enum zone_stat_item item)
554{
555 unsigned long flags;
2244b95a 556
2244b95a 557 local_irq_save(flags);
a302eb4e 558 __dec_zone_page_state(page, item);
2244b95a
CL
559 local_irq_restore(flags);
560}
561EXPORT_SYMBOL(dec_zone_page_state);
562
75ef7184
MG
563void inc_node_state(struct pglist_data *pgdat, enum node_stat_item item)
564{
565 unsigned long flags;
566
567 local_irq_save(flags);
568 __inc_node_state(pgdat, item);
569 local_irq_restore(flags);
570}
571EXPORT_SYMBOL(inc_node_state);
572
573void mod_node_page_state(struct pglist_data *pgdat, enum node_stat_item item,
574 long delta)
575{
576 unsigned long flags;
577
578 local_irq_save(flags);
579 __mod_node_page_state(pgdat, item, delta);
580 local_irq_restore(flags);
581}
582EXPORT_SYMBOL(mod_node_page_state);
583
584void inc_node_page_state(struct page *page, enum node_stat_item item)
585{
586 unsigned long flags;
587 struct pglist_data *pgdat;
588
589 pgdat = page_pgdat(page);
590 local_irq_save(flags);
591 __inc_node_state(pgdat, item);
592 local_irq_restore(flags);
593}
594EXPORT_SYMBOL(inc_node_page_state);
595
596void dec_node_page_state(struct page *page, enum node_stat_item item)
597{
598 unsigned long flags;
599
600 local_irq_save(flags);
601 __dec_node_page_state(page, item);
602 local_irq_restore(flags);
603}
604EXPORT_SYMBOL(dec_node_page_state);
605#endif
7cc36bbd
CL
606
607/*
608 * Fold a differential into the global counters.
609 * Returns the number of counters updated.
610 */
3a321d2a
KW
611#ifdef CONFIG_NUMA
612static int fold_diff(int *zone_diff, int *numa_diff, int *node_diff)
613{
614 int i;
615 int changes = 0;
616
617 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
618 if (zone_diff[i]) {
619 atomic_long_add(zone_diff[i], &vm_zone_stat[i]);
620 changes++;
621 }
622
623 for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++)
624 if (numa_diff[i]) {
625 atomic_long_add(numa_diff[i], &vm_numa_stat[i]);
626 changes++;
627 }
628
629 for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++)
630 if (node_diff[i]) {
631 atomic_long_add(node_diff[i], &vm_node_stat[i]);
632 changes++;
633 }
634 return changes;
635}
636#else
75ef7184 637static int fold_diff(int *zone_diff, int *node_diff)
4edb0748
CL
638{
639 int i;
7cc36bbd 640 int changes = 0;
4edb0748
CL
641
642 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
75ef7184
MG
643 if (zone_diff[i]) {
644 atomic_long_add(zone_diff[i], &vm_zone_stat[i]);
645 changes++;
646 }
647
648 for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++)
649 if (node_diff[i]) {
650 atomic_long_add(node_diff[i], &vm_node_stat[i]);
7cc36bbd
CL
651 changes++;
652 }
653 return changes;
4edb0748 654}
3a321d2a 655#endif /* CONFIG_NUMA */
4edb0748 656
2244b95a 657/*
2bb921e5 658 * Update the zone counters for the current cpu.
a7f75e25 659 *
4037d452
CL
660 * Note that refresh_cpu_vm_stats strives to only access
661 * node local memory. The per cpu pagesets on remote zones are placed
662 * in the memory local to the processor using that pageset. So the
663 * loop over all zones will access a series of cachelines local to
664 * the processor.
665 *
666 * The call to zone_page_state_add updates the cachelines with the
667 * statistics in the remote zone struct as well as the global cachelines
668 * with the global counters. These could cause remote node cache line
669 * bouncing and will have to be only done when necessary.
7cc36bbd
CL
670 *
671 * The function returns the number of global counters updated.
2244b95a 672 */
0eb77e98 673static int refresh_cpu_vm_stats(bool do_pagesets)
2244b95a 674{
75ef7184 675 struct pglist_data *pgdat;
2244b95a
CL
676 struct zone *zone;
677 int i;
75ef7184 678 int global_zone_diff[NR_VM_ZONE_STAT_ITEMS] = { 0, };
3a321d2a
KW
679#ifdef CONFIG_NUMA
680 int global_numa_diff[NR_VM_NUMA_STAT_ITEMS] = { 0, };
681#endif
75ef7184 682 int global_node_diff[NR_VM_NODE_STAT_ITEMS] = { 0, };
7cc36bbd 683 int changes = 0;
2244b95a 684
ee99c71c 685 for_each_populated_zone(zone) {
fbc2edb0 686 struct per_cpu_pageset __percpu *p = zone->pageset;
2244b95a 687
fbc2edb0
CL
688 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++) {
689 int v;
2244b95a 690
fbc2edb0
CL
691 v = this_cpu_xchg(p->vm_stat_diff[i], 0);
692 if (v) {
a7f75e25 693
a7f75e25 694 atomic_long_add(v, &zone->vm_stat[i]);
75ef7184 695 global_zone_diff[i] += v;
4037d452
CL
696#ifdef CONFIG_NUMA
697 /* 3 seconds idle till flush */
fbc2edb0 698 __this_cpu_write(p->expire, 3);
4037d452 699#endif
2244b95a 700 }
fbc2edb0 701 }
4037d452 702#ifdef CONFIG_NUMA
3a321d2a
KW
703 for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++) {
704 int v;
705
706 v = this_cpu_xchg(p->vm_numa_stat_diff[i], 0);
707 if (v) {
708
709 atomic_long_add(v, &zone->vm_numa_stat[i]);
710 global_numa_diff[i] += v;
711 __this_cpu_write(p->expire, 3);
712 }
713 }
714
0eb77e98
CL
715 if (do_pagesets) {
716 cond_resched();
717 /*
718 * Deal with draining the remote pageset of this
719 * processor
720 *
721 * Check if there are pages remaining in this pageset
722 * if not then there is nothing to expire.
723 */
724 if (!__this_cpu_read(p->expire) ||
fbc2edb0 725 !__this_cpu_read(p->pcp.count))
0eb77e98 726 continue;
4037d452 727
0eb77e98
CL
728 /*
729 * We never drain zones local to this processor.
730 */
731 if (zone_to_nid(zone) == numa_node_id()) {
732 __this_cpu_write(p->expire, 0);
733 continue;
734 }
4037d452 735
0eb77e98
CL
736 if (__this_cpu_dec_return(p->expire))
737 continue;
4037d452 738
0eb77e98
CL
739 if (__this_cpu_read(p->pcp.count)) {
740 drain_zone_pages(zone, this_cpu_ptr(&p->pcp));
741 changes++;
742 }
7cc36bbd 743 }
4037d452 744#endif
2244b95a 745 }
75ef7184
MG
746
747 for_each_online_pgdat(pgdat) {
748 struct per_cpu_nodestat __percpu *p = pgdat->per_cpu_nodestats;
749
750 for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++) {
751 int v;
752
753 v = this_cpu_xchg(p->vm_node_stat_diff[i], 0);
754 if (v) {
755 atomic_long_add(v, &pgdat->vm_stat[i]);
756 global_node_diff[i] += v;
757 }
758 }
759 }
760
3a321d2a
KW
761#ifdef CONFIG_NUMA
762 changes += fold_diff(global_zone_diff, global_numa_diff,
763 global_node_diff);
764#else
75ef7184 765 changes += fold_diff(global_zone_diff, global_node_diff);
3a321d2a 766#endif
7cc36bbd 767 return changes;
2244b95a
CL
768}
769
2bb921e5
CL
770/*
771 * Fold the data for an offline cpu into the global array.
772 * There cannot be any access by the offline cpu and therefore
773 * synchronization is simplified.
774 */
775void cpu_vm_stats_fold(int cpu)
776{
75ef7184 777 struct pglist_data *pgdat;
2bb921e5
CL
778 struct zone *zone;
779 int i;
75ef7184 780 int global_zone_diff[NR_VM_ZONE_STAT_ITEMS] = { 0, };
3a321d2a
KW
781#ifdef CONFIG_NUMA
782 int global_numa_diff[NR_VM_NUMA_STAT_ITEMS] = { 0, };
783#endif
75ef7184 784 int global_node_diff[NR_VM_NODE_STAT_ITEMS] = { 0, };
2bb921e5
CL
785
786 for_each_populated_zone(zone) {
787 struct per_cpu_pageset *p;
788
789 p = per_cpu_ptr(zone->pageset, cpu);
790
791 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
792 if (p->vm_stat_diff[i]) {
793 int v;
794
795 v = p->vm_stat_diff[i];
796 p->vm_stat_diff[i] = 0;
797 atomic_long_add(v, &zone->vm_stat[i]);
75ef7184 798 global_zone_diff[i] += v;
2bb921e5 799 }
3a321d2a
KW
800
801#ifdef CONFIG_NUMA
802 for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++)
803 if (p->vm_numa_stat_diff[i]) {
804 int v;
805
806 v = p->vm_numa_stat_diff[i];
807 p->vm_numa_stat_diff[i] = 0;
808 atomic_long_add(v, &zone->vm_numa_stat[i]);
809 global_numa_diff[i] += v;
810 }
811#endif
2bb921e5
CL
812 }
813
75ef7184
MG
814 for_each_online_pgdat(pgdat) {
815 struct per_cpu_nodestat *p;
816
817 p = per_cpu_ptr(pgdat->per_cpu_nodestats, cpu);
818
819 for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++)
820 if (p->vm_node_stat_diff[i]) {
821 int v;
822
823 v = p->vm_node_stat_diff[i];
824 p->vm_node_stat_diff[i] = 0;
825 atomic_long_add(v, &pgdat->vm_stat[i]);
826 global_node_diff[i] += v;
827 }
828 }
829
3a321d2a
KW
830#ifdef CONFIG_NUMA
831 fold_diff(global_zone_diff, global_numa_diff, global_node_diff);
832#else
75ef7184 833 fold_diff(global_zone_diff, global_node_diff);
3a321d2a 834#endif
2bb921e5
CL
835}
836
40f4b1ea
CS
837/*
838 * this is only called if !populated_zone(zone), which implies no other users of
839 * pset->vm_stat_diff[] exsist.
840 */
5a883813
MK
841void drain_zonestat(struct zone *zone, struct per_cpu_pageset *pset)
842{
843 int i;
844
845 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
846 if (pset->vm_stat_diff[i]) {
847 int v = pset->vm_stat_diff[i];
848 pset->vm_stat_diff[i] = 0;
849 atomic_long_add(v, &zone->vm_stat[i]);
75ef7184 850 atomic_long_add(v, &vm_zone_stat[i]);
5a883813 851 }
3a321d2a
KW
852
853#ifdef CONFIG_NUMA
854 for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++)
855 if (pset->vm_numa_stat_diff[i]) {
856 int v = pset->vm_numa_stat_diff[i];
857
858 pset->vm_numa_stat_diff[i] = 0;
859 atomic_long_add(v, &zone->vm_numa_stat[i]);
860 atomic_long_add(v, &vm_numa_stat[i]);
861 }
862#endif
5a883813 863}
2244b95a
CL
864#endif
865
ca889e6c 866#ifdef CONFIG_NUMA
3a321d2a
KW
867void __inc_numa_state(struct zone *zone,
868 enum numa_stat_item item)
869{
870 struct per_cpu_pageset __percpu *pcp = zone->pageset;
1d90ca89
KW
871 u16 __percpu *p = pcp->vm_numa_stat_diff + item;
872 u16 v;
3a321d2a
KW
873
874 v = __this_cpu_inc_return(*p);
3a321d2a 875
1d90ca89
KW
876 if (unlikely(v > NUMA_STATS_THRESHOLD)) {
877 zone_numa_state_add(v, zone, item);
878 __this_cpu_write(*p, 0);
3a321d2a
KW
879 }
880}
881
c2d42c16 882/*
75ef7184
MG
883 * Determine the per node value of a stat item. This function
884 * is called frequently in a NUMA machine, so try to be as
885 * frugal as possible.
c2d42c16 886 */
75ef7184
MG
887unsigned long sum_zone_node_page_state(int node,
888 enum zone_stat_item item)
c2d42c16
AM
889{
890 struct zone *zones = NODE_DATA(node)->node_zones;
e87d59f7
JK
891 int i;
892 unsigned long count = 0;
c2d42c16 893
e87d59f7
JK
894 for (i = 0; i < MAX_NR_ZONES; i++)
895 count += zone_page_state(zones + i, item);
896
897 return count;
c2d42c16
AM
898}
899
63803222
KW
900/*
901 * Determine the per node value of a numa stat item. To avoid deviation,
902 * the per cpu stat number in vm_numa_stat_diff[] is also included.
903 */
3a321d2a
KW
904unsigned long sum_zone_numa_state(int node,
905 enum numa_stat_item item)
906{
907 struct zone *zones = NODE_DATA(node)->node_zones;
908 int i;
909 unsigned long count = 0;
910
911 for (i = 0; i < MAX_NR_ZONES; i++)
63803222 912 count += zone_numa_state_snapshot(zones + i, item);
3a321d2a
KW
913
914 return count;
915}
916
75ef7184
MG
917/*
918 * Determine the per node value of a stat item.
919 */
920unsigned long node_page_state(struct pglist_data *pgdat,
921 enum node_stat_item item)
922{
923 long x = atomic_long_read(&pgdat->vm_stat[item]);
924#ifdef CONFIG_SMP
925 if (x < 0)
926 x = 0;
927#endif
928 return x;
929}
ca889e6c
CL
930#endif
931
d7a5752c 932#ifdef CONFIG_COMPACTION
36deb0be 933
d7a5752c
MG
934struct contig_page_info {
935 unsigned long free_pages;
936 unsigned long free_blocks_total;
937 unsigned long free_blocks_suitable;
938};
939
940/*
941 * Calculate the number of free pages in a zone, how many contiguous
942 * pages are free and how many are large enough to satisfy an allocation of
943 * the target size. Note that this function makes no attempt to estimate
944 * how many suitable free blocks there *might* be if MOVABLE pages were
945 * migrated. Calculating that is possible, but expensive and can be
946 * figured out from userspace
947 */
948static void fill_contig_page_info(struct zone *zone,
949 unsigned int suitable_order,
950 struct contig_page_info *info)
951{
952 unsigned int order;
953
954 info->free_pages = 0;
955 info->free_blocks_total = 0;
956 info->free_blocks_suitable = 0;
957
958 for (order = 0; order < MAX_ORDER; order++) {
959 unsigned long blocks;
960
961 /* Count number of free blocks */
962 blocks = zone->free_area[order].nr_free;
963 info->free_blocks_total += blocks;
964
965 /* Count free base pages */
966 info->free_pages += blocks << order;
967
968 /* Count the suitable free blocks */
969 if (order >= suitable_order)
970 info->free_blocks_suitable += blocks <<
971 (order - suitable_order);
972 }
973}
f1a5ab12
MG
974
975/*
976 * A fragmentation index only makes sense if an allocation of a requested
977 * size would fail. If that is true, the fragmentation index indicates
978 * whether external fragmentation or a lack of memory was the problem.
979 * The value can be used to determine if page reclaim or compaction
980 * should be used
981 */
56de7263 982static int __fragmentation_index(unsigned int order, struct contig_page_info *info)
f1a5ab12
MG
983{
984 unsigned long requested = 1UL << order;
985
88d6ac40
WY
986 if (WARN_ON_ONCE(order >= MAX_ORDER))
987 return 0;
988
f1a5ab12
MG
989 if (!info->free_blocks_total)
990 return 0;
991
992 /* Fragmentation index only makes sense when a request would fail */
993 if (info->free_blocks_suitable)
994 return -1000;
995
996 /*
997 * Index is between 0 and 1 so return within 3 decimal places
998 *
999 * 0 => allocation would fail due to lack of memory
1000 * 1 => allocation would fail due to fragmentation
1001 */
1002 return 1000 - div_u64( (1000+(div_u64(info->free_pages * 1000ULL, requested))), info->free_blocks_total);
1003}
56de7263
MG
1004
1005/* Same as __fragmentation index but allocs contig_page_info on stack */
1006int fragmentation_index(struct zone *zone, unsigned int order)
1007{
1008 struct contig_page_info info;
1009
1010 fill_contig_page_info(zone, order, &info);
1011 return __fragmentation_index(order, &info);
1012}
d7a5752c
MG
1013#endif
1014
0d6617c7 1015#if defined(CONFIG_PROC_FS) || defined(CONFIG_SYSFS) || defined(CONFIG_NUMA)
fa25c503
KM
1016#ifdef CONFIG_ZONE_DMA
1017#define TEXT_FOR_DMA(xx) xx "_dma",
1018#else
1019#define TEXT_FOR_DMA(xx)
1020#endif
1021
1022#ifdef CONFIG_ZONE_DMA32
1023#define TEXT_FOR_DMA32(xx) xx "_dma32",
1024#else
1025#define TEXT_FOR_DMA32(xx)
1026#endif
1027
1028#ifdef CONFIG_HIGHMEM
1029#define TEXT_FOR_HIGHMEM(xx) xx "_high",
1030#else
1031#define TEXT_FOR_HIGHMEM(xx)
1032#endif
1033
1034#define TEXTS_FOR_ZONES(xx) TEXT_FOR_DMA(xx) TEXT_FOR_DMA32(xx) xx "_normal", \
1035 TEXT_FOR_HIGHMEM(xx) xx "_movable",
1036
1037const char * const vmstat_text[] = {
09316c09 1038 /* enum zone_stat_item countes */
fa25c503 1039 "nr_free_pages",
71c799f4
MK
1040 "nr_zone_inactive_anon",
1041 "nr_zone_active_anon",
1042 "nr_zone_inactive_file",
1043 "nr_zone_active_file",
1044 "nr_zone_unevictable",
5a1c84b4 1045 "nr_zone_write_pending",
fa25c503 1046 "nr_mlock",
fa25c503
KM
1047 "nr_page_table_pages",
1048 "nr_kernel_stack",
fa25c503 1049 "nr_bounce",
91537fee
MK
1050#if IS_ENABLED(CONFIG_ZSMALLOC)
1051 "nr_zspages",
1052#endif
3a321d2a
KW
1053 "nr_free_cma",
1054
1055 /* enum numa_stat_item counters */
fa25c503
KM
1056#ifdef CONFIG_NUMA
1057 "numa_hit",
1058 "numa_miss",
1059 "numa_foreign",
1060 "numa_interleave",
1061 "numa_local",
1062 "numa_other",
1063#endif
09316c09 1064
599d0c95
MG
1065 /* Node-based counters */
1066 "nr_inactive_anon",
1067 "nr_active_anon",
1068 "nr_inactive_file",
1069 "nr_active_file",
1070 "nr_unevictable",
385386cf
JW
1071 "nr_slab_reclaimable",
1072 "nr_slab_unreclaimable",
599d0c95
MG
1073 "nr_isolated_anon",
1074 "nr_isolated_file",
1e6b1085
MG
1075 "workingset_refault",
1076 "workingset_activate",
1077 "workingset_nodereclaim",
50658e2e
MG
1078 "nr_anon_pages",
1079 "nr_mapped",
11fb9989
MG
1080 "nr_file_pages",
1081 "nr_dirty",
1082 "nr_writeback",
1083 "nr_writeback_temp",
1084 "nr_shmem",
1085 "nr_shmem_hugepages",
1086 "nr_shmem_pmdmapped",
1087 "nr_anon_transparent_hugepages",
1088 "nr_unstable",
c4a25635
MG
1089 "nr_vmscan_write",
1090 "nr_vmscan_immediate_reclaim",
1091 "nr_dirtied",
1092 "nr_written",
599d0c95 1093
09316c09 1094 /* enum writeback_stat_item counters */
fa25c503
KM
1095 "nr_dirty_threshold",
1096 "nr_dirty_background_threshold",
1097
1098#ifdef CONFIG_VM_EVENT_COUNTERS
09316c09 1099 /* enum vm_event_item counters */
fa25c503
KM
1100 "pgpgin",
1101 "pgpgout",
1102 "pswpin",
1103 "pswpout",
1104
1105 TEXTS_FOR_ZONES("pgalloc")
7cc30fcf
MG
1106 TEXTS_FOR_ZONES("allocstall")
1107 TEXTS_FOR_ZONES("pgskip")
fa25c503
KM
1108
1109 "pgfree",
1110 "pgactivate",
1111 "pgdeactivate",
f7ad2a6c 1112 "pglazyfree",
fa25c503
KM
1113
1114 "pgfault",
1115 "pgmajfault",
854e9ed0 1116 "pglazyfreed",
fa25c503 1117
599d0c95
MG
1118 "pgrefill",
1119 "pgsteal_kswapd",
1120 "pgsteal_direct",
1121 "pgscan_kswapd",
1122 "pgscan_direct",
68243e76 1123 "pgscan_direct_throttle",
fa25c503
KM
1124
1125#ifdef CONFIG_NUMA
1126 "zone_reclaim_failed",
1127#endif
1128 "pginodesteal",
1129 "slabs_scanned",
fa25c503
KM
1130 "kswapd_inodesteal",
1131 "kswapd_low_wmark_hit_quickly",
1132 "kswapd_high_wmark_hit_quickly",
fa25c503 1133 "pageoutrun",
fa25c503
KM
1134
1135 "pgrotated",
1136
5509a5d2
DH
1137 "drop_pagecache",
1138 "drop_slab",
8e675f7a 1139 "oom_kill",
5509a5d2 1140
03c5a6e1
MG
1141#ifdef CONFIG_NUMA_BALANCING
1142 "numa_pte_updates",
72403b4a 1143 "numa_huge_pte_updates",
03c5a6e1
MG
1144 "numa_hint_faults",
1145 "numa_hint_faults_local",
1146 "numa_pages_migrated",
1147#endif
5647bc29
MG
1148#ifdef CONFIG_MIGRATION
1149 "pgmigrate_success",
1150 "pgmigrate_fail",
1151#endif
fa25c503 1152#ifdef CONFIG_COMPACTION
397487db
MG
1153 "compact_migrate_scanned",
1154 "compact_free_scanned",
1155 "compact_isolated",
fa25c503
KM
1156 "compact_stall",
1157 "compact_fail",
1158 "compact_success",
698b1b30 1159 "compact_daemon_wake",
7f354a54
DR
1160 "compact_daemon_migrate_scanned",
1161 "compact_daemon_free_scanned",
fa25c503
KM
1162#endif
1163
1164#ifdef CONFIG_HUGETLB_PAGE
1165 "htlb_buddy_alloc_success",
1166 "htlb_buddy_alloc_fail",
1167#endif
1168 "unevictable_pgs_culled",
1169 "unevictable_pgs_scanned",
1170 "unevictable_pgs_rescued",
1171 "unevictable_pgs_mlocked",
1172 "unevictable_pgs_munlocked",
1173 "unevictable_pgs_cleared",
1174 "unevictable_pgs_stranded",
fa25c503
KM
1175
1176#ifdef CONFIG_TRANSPARENT_HUGEPAGE
1177 "thp_fault_alloc",
1178 "thp_fault_fallback",
1179 "thp_collapse_alloc",
1180 "thp_collapse_alloc_failed",
95ecedcd
KS
1181 "thp_file_alloc",
1182 "thp_file_mapped",
122afea9
KS
1183 "thp_split_page",
1184 "thp_split_page_failed",
f9719a03 1185 "thp_deferred_split_page",
122afea9 1186 "thp_split_pmd",
ce9311cf
YX
1187#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
1188 "thp_split_pud",
1189#endif
d8a8e1f0
KS
1190 "thp_zero_page_alloc",
1191 "thp_zero_page_alloc_failed",
225311a4 1192 "thp_swpout",
fe490cc0 1193 "thp_swpout_fallback",
fa25c503 1194#endif
09316c09
KK
1195#ifdef CONFIG_MEMORY_BALLOON
1196 "balloon_inflate",
1197 "balloon_deflate",
1198#ifdef CONFIG_BALLOON_COMPACTION
1199 "balloon_migrate",
1200#endif
1201#endif /* CONFIG_MEMORY_BALLOON */
ec659934 1202#ifdef CONFIG_DEBUG_TLBFLUSH
6df46865 1203#ifdef CONFIG_SMP
9824cf97
DH
1204 "nr_tlb_remote_flush",
1205 "nr_tlb_remote_flush_received",
ec659934 1206#endif /* CONFIG_SMP */
9824cf97
DH
1207 "nr_tlb_local_flush_all",
1208 "nr_tlb_local_flush_one",
ec659934 1209#endif /* CONFIG_DEBUG_TLBFLUSH */
fa25c503 1210
4f115147
DB
1211#ifdef CONFIG_DEBUG_VM_VMACACHE
1212 "vmacache_find_calls",
1213 "vmacache_find_hits",
f5f302e2 1214 "vmacache_full_flushes",
4f115147 1215#endif
cbc65df2
HY
1216#ifdef CONFIG_SWAP
1217 "swap_ra",
1218 "swap_ra_hit",
1219#endif
fa25c503
KM
1220#endif /* CONFIG_VM_EVENTS_COUNTERS */
1221};
0d6617c7 1222#endif /* CONFIG_PROC_FS || CONFIG_SYSFS || CONFIG_NUMA */
fa25c503 1223
3c486871
AM
1224#if (defined(CONFIG_DEBUG_FS) && defined(CONFIG_COMPACTION)) || \
1225 defined(CONFIG_PROC_FS)
1226static void *frag_start(struct seq_file *m, loff_t *pos)
1227{
1228 pg_data_t *pgdat;
1229 loff_t node = *pos;
1230
1231 for (pgdat = first_online_pgdat();
1232 pgdat && node;
1233 pgdat = next_online_pgdat(pgdat))
1234 --node;
1235
1236 return pgdat;
1237}
1238
1239static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
1240{
1241 pg_data_t *pgdat = (pg_data_t *)arg;
1242
1243 (*pos)++;
1244 return next_online_pgdat(pgdat);
1245}
1246
1247static void frag_stop(struct seq_file *m, void *arg)
1248{
1249}
1250
b2bd8598
DR
1251/*
1252 * Walk zones in a node and print using a callback.
1253 * If @assert_populated is true, only use callback for zones that are populated.
1254 */
3c486871 1255static void walk_zones_in_node(struct seq_file *m, pg_data_t *pgdat,
727c080f 1256 bool assert_populated, bool nolock,
3c486871
AM
1257 void (*print)(struct seq_file *m, pg_data_t *, struct zone *))
1258{
1259 struct zone *zone;
1260 struct zone *node_zones = pgdat->node_zones;
1261 unsigned long flags;
1262
1263 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
b2bd8598 1264 if (assert_populated && !populated_zone(zone))
3c486871
AM
1265 continue;
1266
727c080f
VM
1267 if (!nolock)
1268 spin_lock_irqsave(&zone->lock, flags);
3c486871 1269 print(m, pgdat, zone);
727c080f
VM
1270 if (!nolock)
1271 spin_unlock_irqrestore(&zone->lock, flags);
3c486871
AM
1272 }
1273}
1274#endif
1275
d7a5752c 1276#ifdef CONFIG_PROC_FS
467c996c
MG
1277static void frag_show_print(struct seq_file *m, pg_data_t *pgdat,
1278 struct zone *zone)
1279{
1280 int order;
1281
1282 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
1283 for (order = 0; order < MAX_ORDER; ++order)
1284 seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
1285 seq_putc(m, '\n');
1286}
1287
1288/*
1289 * This walks the free areas for each zone.
1290 */
1291static int frag_show(struct seq_file *m, void *arg)
1292{
1293 pg_data_t *pgdat = (pg_data_t *)arg;
727c080f 1294 walk_zones_in_node(m, pgdat, true, false, frag_show_print);
467c996c
MG
1295 return 0;
1296}
1297
1298static void pagetypeinfo_showfree_print(struct seq_file *m,
1299 pg_data_t *pgdat, struct zone *zone)
1300{
1301 int order, mtype;
1302
1303 for (mtype = 0; mtype < MIGRATE_TYPES; mtype++) {
1304 seq_printf(m, "Node %4d, zone %8s, type %12s ",
1305 pgdat->node_id,
1306 zone->name,
1307 migratetype_names[mtype]);
1308 for (order = 0; order < MAX_ORDER; ++order) {
1309 unsigned long freecount = 0;
1310 struct free_area *area;
1311 struct list_head *curr;
1312
1313 area = &(zone->free_area[order]);
1314
1315 list_for_each(curr, &area->free_list[mtype])
1316 freecount++;
1317 seq_printf(m, "%6lu ", freecount);
1318 }
f6ac2354
CL
1319 seq_putc(m, '\n');
1320 }
467c996c
MG
1321}
1322
1323/* Print out the free pages at each order for each migatetype */
1324static int pagetypeinfo_showfree(struct seq_file *m, void *arg)
1325{
1326 int order;
1327 pg_data_t *pgdat = (pg_data_t *)arg;
1328
1329 /* Print header */
1330 seq_printf(m, "%-43s ", "Free pages count per migrate type at order");
1331 for (order = 0; order < MAX_ORDER; ++order)
1332 seq_printf(m, "%6d ", order);
1333 seq_putc(m, '\n');
1334
727c080f 1335 walk_zones_in_node(m, pgdat, true, false, pagetypeinfo_showfree_print);
467c996c
MG
1336
1337 return 0;
1338}
1339
1340static void pagetypeinfo_showblockcount_print(struct seq_file *m,
1341 pg_data_t *pgdat, struct zone *zone)
1342{
1343 int mtype;
1344 unsigned long pfn;
1345 unsigned long start_pfn = zone->zone_start_pfn;
108bcc96 1346 unsigned long end_pfn = zone_end_pfn(zone);
467c996c
MG
1347 unsigned long count[MIGRATE_TYPES] = { 0, };
1348
1349 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
1350 struct page *page;
1351
d336e94e
MH
1352 page = pfn_to_online_page(pfn);
1353 if (!page)
467c996c
MG
1354 continue;
1355
eb33575c
MG
1356 /* Watch for unexpected holes punched in the memmap */
1357 if (!memmap_valid_within(pfn, page, zone))
e80d6a24 1358 continue;
eb33575c 1359
a91c43c7
JK
1360 if (page_zone(page) != zone)
1361 continue;
1362
467c996c
MG
1363 mtype = get_pageblock_migratetype(page);
1364
e80d6a24
MG
1365 if (mtype < MIGRATE_TYPES)
1366 count[mtype]++;
467c996c
MG
1367 }
1368
1369 /* Print counts */
1370 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
1371 for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
1372 seq_printf(m, "%12lu ", count[mtype]);
1373 seq_putc(m, '\n');
1374}
1375
f113e641 1376/* Print out the number of pageblocks for each migratetype */
467c996c
MG
1377static int pagetypeinfo_showblockcount(struct seq_file *m, void *arg)
1378{
1379 int mtype;
1380 pg_data_t *pgdat = (pg_data_t *)arg;
1381
1382 seq_printf(m, "\n%-23s", "Number of blocks type ");
1383 for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
1384 seq_printf(m, "%12s ", migratetype_names[mtype]);
1385 seq_putc(m, '\n');
727c080f
VM
1386 walk_zones_in_node(m, pgdat, true, false,
1387 pagetypeinfo_showblockcount_print);
467c996c
MG
1388
1389 return 0;
1390}
1391
48c96a36
JK
1392/*
1393 * Print out the number of pageblocks for each migratetype that contain pages
1394 * of other types. This gives an indication of how well fallbacks are being
1395 * contained by rmqueue_fallback(). It requires information from PAGE_OWNER
1396 * to determine what is going on
1397 */
1398static void pagetypeinfo_showmixedcount(struct seq_file *m, pg_data_t *pgdat)
1399{
1400#ifdef CONFIG_PAGE_OWNER
1401 int mtype;
1402
7dd80b8a 1403 if (!static_branch_unlikely(&page_owner_inited))
48c96a36
JK
1404 return;
1405
1406 drain_all_pages(NULL);
1407
1408 seq_printf(m, "\n%-23s", "Number of mixed blocks ");
1409 for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
1410 seq_printf(m, "%12s ", migratetype_names[mtype]);
1411 seq_putc(m, '\n');
1412
727c080f
VM
1413 walk_zones_in_node(m, pgdat, true, true,
1414 pagetypeinfo_showmixedcount_print);
48c96a36
JK
1415#endif /* CONFIG_PAGE_OWNER */
1416}
1417
467c996c
MG
1418/*
1419 * This prints out statistics in relation to grouping pages by mobility.
1420 * It is expensive to collect so do not constantly read the file.
1421 */
1422static int pagetypeinfo_show(struct seq_file *m, void *arg)
1423{
1424 pg_data_t *pgdat = (pg_data_t *)arg;
1425
41b25a37 1426 /* check memoryless node */
a47b53c5 1427 if (!node_state(pgdat->node_id, N_MEMORY))
41b25a37
KM
1428 return 0;
1429
467c996c
MG
1430 seq_printf(m, "Page block order: %d\n", pageblock_order);
1431 seq_printf(m, "Pages per block: %lu\n", pageblock_nr_pages);
1432 seq_putc(m, '\n');
1433 pagetypeinfo_showfree(m, pgdat);
1434 pagetypeinfo_showblockcount(m, pgdat);
48c96a36 1435 pagetypeinfo_showmixedcount(m, pgdat);
467c996c 1436
f6ac2354
CL
1437 return 0;
1438}
1439
8f32f7e5 1440static const struct seq_operations fragmentation_op = {
f6ac2354
CL
1441 .start = frag_start,
1442 .next = frag_next,
1443 .stop = frag_stop,
1444 .show = frag_show,
1445};
1446
8f32f7e5
AD
1447static int fragmentation_open(struct inode *inode, struct file *file)
1448{
1449 return seq_open(file, &fragmentation_op);
1450}
1451
9d85e15f 1452static const struct file_operations buddyinfo_file_operations = {
8f32f7e5
AD
1453 .open = fragmentation_open,
1454 .read = seq_read,
1455 .llseek = seq_lseek,
1456 .release = seq_release,
1457};
1458
74e2e8e8 1459static const struct seq_operations pagetypeinfo_op = {
467c996c
MG
1460 .start = frag_start,
1461 .next = frag_next,
1462 .stop = frag_stop,
1463 .show = pagetypeinfo_show,
1464};
1465
74e2e8e8
AD
1466static int pagetypeinfo_open(struct inode *inode, struct file *file)
1467{
1468 return seq_open(file, &pagetypeinfo_op);
1469}
1470
9d85e15f 1471static const struct file_operations pagetypeinfo_file_operations = {
74e2e8e8
AD
1472 .open = pagetypeinfo_open,
1473 .read = seq_read,
1474 .llseek = seq_lseek,
1475 .release = seq_release,
1476};
1477
e2ecc8a7
MG
1478static bool is_zone_first_populated(pg_data_t *pgdat, struct zone *zone)
1479{
1480 int zid;
1481
1482 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
1483 struct zone *compare = &pgdat->node_zones[zid];
1484
1485 if (populated_zone(compare))
1486 return zone == compare;
1487 }
1488
e2ecc8a7
MG
1489 return false;
1490}
1491
467c996c
MG
1492static void zoneinfo_show_print(struct seq_file *m, pg_data_t *pgdat,
1493 struct zone *zone)
f6ac2354 1494{
467c996c
MG
1495 int i;
1496 seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
e2ecc8a7
MG
1497 if (is_zone_first_populated(pgdat, zone)) {
1498 seq_printf(m, "\n per-node stats");
1499 for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++) {
1500 seq_printf(m, "\n %-12s %lu",
3a321d2a
KW
1501 vmstat_text[i + NR_VM_ZONE_STAT_ITEMS +
1502 NR_VM_NUMA_STAT_ITEMS],
e2ecc8a7
MG
1503 node_page_state(pgdat, i));
1504 }
1505 }
467c996c
MG
1506 seq_printf(m,
1507 "\n pages free %lu"
1508 "\n min %lu"
1509 "\n low %lu"
1510 "\n high %lu"
467c996c 1511 "\n spanned %lu"
9feedc9d
JL
1512 "\n present %lu"
1513 "\n managed %lu",
88f5acf8 1514 zone_page_state(zone, NR_FREE_PAGES),
41858966
MG
1515 min_wmark_pages(zone),
1516 low_wmark_pages(zone),
1517 high_wmark_pages(zone),
467c996c 1518 zone->spanned_pages,
9feedc9d
JL
1519 zone->present_pages,
1520 zone->managed_pages);
467c996c 1521
467c996c 1522 seq_printf(m,
3484b2de 1523 "\n protection: (%ld",
467c996c
MG
1524 zone->lowmem_reserve[0]);
1525 for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
3484b2de 1526 seq_printf(m, ", %ld", zone->lowmem_reserve[i]);
7dfb8bf3
DR
1527 seq_putc(m, ')');
1528
1529 /* If unpopulated, no other information is useful */
1530 if (!populated_zone(zone)) {
1531 seq_putc(m, '\n');
1532 return;
1533 }
1534
1535 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
1536 seq_printf(m, "\n %-12s %lu", vmstat_text[i],
1537 zone_page_state(zone, i));
1538
3a321d2a
KW
1539#ifdef CONFIG_NUMA
1540 for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++)
1541 seq_printf(m, "\n %-12s %lu",
1542 vmstat_text[i + NR_VM_ZONE_STAT_ITEMS],
63803222 1543 zone_numa_state_snapshot(zone, i));
3a321d2a
KW
1544#endif
1545
7dfb8bf3 1546 seq_printf(m, "\n pagesets");
467c996c
MG
1547 for_each_online_cpu(i) {
1548 struct per_cpu_pageset *pageset;
467c996c 1549
99dcc3e5 1550 pageset = per_cpu_ptr(zone->pageset, i);
3dfa5721
CL
1551 seq_printf(m,
1552 "\n cpu: %i"
1553 "\n count: %i"
1554 "\n high: %i"
1555 "\n batch: %i",
1556 i,
1557 pageset->pcp.count,
1558 pageset->pcp.high,
1559 pageset->pcp.batch);
df9ecaba 1560#ifdef CONFIG_SMP
467c996c
MG
1561 seq_printf(m, "\n vm stats threshold: %d",
1562 pageset->stat_threshold);
df9ecaba 1563#endif
f6ac2354 1564 }
467c996c 1565 seq_printf(m,
599d0c95 1566 "\n node_unreclaimable: %u"
3a50d14d 1567 "\n start_pfn: %lu",
c73322d0 1568 pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES,
3a50d14d 1569 zone->zone_start_pfn);
467c996c
MG
1570 seq_putc(m, '\n');
1571}
1572
1573/*
b2bd8598
DR
1574 * Output information about zones in @pgdat. All zones are printed regardless
1575 * of whether they are populated or not: lowmem_reserve_ratio operates on the
1576 * set of all zones and userspace would not be aware of such zones if they are
1577 * suppressed here (zoneinfo displays the effect of lowmem_reserve_ratio).
467c996c
MG
1578 */
1579static int zoneinfo_show(struct seq_file *m, void *arg)
1580{
1581 pg_data_t *pgdat = (pg_data_t *)arg;
727c080f 1582 walk_zones_in_node(m, pgdat, false, false, zoneinfo_show_print);
f6ac2354
CL
1583 return 0;
1584}
1585
5c9fe628 1586static const struct seq_operations zoneinfo_op = {
f6ac2354
CL
1587 .start = frag_start, /* iterate over all zones. The same as in
1588 * fragmentation. */
1589 .next = frag_next,
1590 .stop = frag_stop,
1591 .show = zoneinfo_show,
1592};
1593
5c9fe628
AD
1594static int zoneinfo_open(struct inode *inode, struct file *file)
1595{
1596 return seq_open(file, &zoneinfo_op);
1597}
1598
9d85e15f 1599static const struct file_operations zoneinfo_file_operations = {
5c9fe628
AD
1600 .open = zoneinfo_open,
1601 .read = seq_read,
1602 .llseek = seq_lseek,
1603 .release = seq_release,
1604};
1605
79da826a
MR
1606enum writeback_stat_item {
1607 NR_DIRTY_THRESHOLD,
1608 NR_DIRTY_BG_THRESHOLD,
1609 NR_VM_WRITEBACK_STAT_ITEMS,
1610};
1611
f6ac2354
CL
1612static void *vmstat_start(struct seq_file *m, loff_t *pos)
1613{
2244b95a 1614 unsigned long *v;
79da826a 1615 int i, stat_items_size;
f6ac2354
CL
1616
1617 if (*pos >= ARRAY_SIZE(vmstat_text))
1618 return NULL;
79da826a 1619 stat_items_size = NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long) +
3a321d2a 1620 NR_VM_NUMA_STAT_ITEMS * sizeof(unsigned long) +
75ef7184 1621 NR_VM_NODE_STAT_ITEMS * sizeof(unsigned long) +
79da826a 1622 NR_VM_WRITEBACK_STAT_ITEMS * sizeof(unsigned long);
f6ac2354 1623
f8891e5e 1624#ifdef CONFIG_VM_EVENT_COUNTERS
79da826a 1625 stat_items_size += sizeof(struct vm_event_state);
f8891e5e 1626#endif
79da826a
MR
1627
1628 v = kmalloc(stat_items_size, GFP_KERNEL);
2244b95a
CL
1629 m->private = v;
1630 if (!v)
f6ac2354 1631 return ERR_PTR(-ENOMEM);
2244b95a 1632 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
c41f012a 1633 v[i] = global_zone_page_state(i);
79da826a
MR
1634 v += NR_VM_ZONE_STAT_ITEMS;
1635
3a321d2a
KW
1636#ifdef CONFIG_NUMA
1637 for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++)
1638 v[i] = global_numa_state(i);
1639 v += NR_VM_NUMA_STAT_ITEMS;
1640#endif
1641
75ef7184
MG
1642 for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++)
1643 v[i] = global_node_page_state(i);
1644 v += NR_VM_NODE_STAT_ITEMS;
1645
79da826a
MR
1646 global_dirty_limits(v + NR_DIRTY_BG_THRESHOLD,
1647 v + NR_DIRTY_THRESHOLD);
1648 v += NR_VM_WRITEBACK_STAT_ITEMS;
1649
f8891e5e 1650#ifdef CONFIG_VM_EVENT_COUNTERS
79da826a
MR
1651 all_vm_events(v);
1652 v[PGPGIN] /= 2; /* sectors -> kbytes */
1653 v[PGPGOUT] /= 2;
f8891e5e 1654#endif
ff8b16d7 1655 return (unsigned long *)m->private + *pos;
f6ac2354
CL
1656}
1657
1658static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
1659{
1660 (*pos)++;
1661 if (*pos >= ARRAY_SIZE(vmstat_text))
1662 return NULL;
1663 return (unsigned long *)m->private + *pos;
1664}
1665
1666static int vmstat_show(struct seq_file *m, void *arg)
1667{
1668 unsigned long *l = arg;
1669 unsigned long off = l - (unsigned long *)m->private;
68ba0326
AD
1670
1671 seq_puts(m, vmstat_text[off]);
75ba1d07 1672 seq_put_decimal_ull(m, " ", *l);
68ba0326 1673 seq_putc(m, '\n');
f6ac2354
CL
1674 return 0;
1675}
1676
1677static void vmstat_stop(struct seq_file *m, void *arg)
1678{
1679 kfree(m->private);
1680 m->private = NULL;
1681}
1682
b6aa44ab 1683static const struct seq_operations vmstat_op = {
f6ac2354
CL
1684 .start = vmstat_start,
1685 .next = vmstat_next,
1686 .stop = vmstat_stop,
1687 .show = vmstat_show,
1688};
1689
b6aa44ab
AD
1690static int vmstat_open(struct inode *inode, struct file *file)
1691{
1692 return seq_open(file, &vmstat_op);
1693}
1694
9d85e15f 1695static const struct file_operations vmstat_file_operations = {
b6aa44ab
AD
1696 .open = vmstat_open,
1697 .read = seq_read,
1698 .llseek = seq_lseek,
1699 .release = seq_release,
1700};
f6ac2354
CL
1701#endif /* CONFIG_PROC_FS */
1702
df9ecaba 1703#ifdef CONFIG_SMP
d1187ed2 1704static DEFINE_PER_CPU(struct delayed_work, vmstat_work);
77461ab3 1705int sysctl_stat_interval __read_mostly = HZ;
d1187ed2 1706
52b6f46b
HD
1707#ifdef CONFIG_PROC_FS
1708static void refresh_vm_stats(struct work_struct *work)
1709{
1710 refresh_cpu_vm_stats(true);
1711}
1712
1713int vmstat_refresh(struct ctl_table *table, int write,
1714 void __user *buffer, size_t *lenp, loff_t *ppos)
1715{
1716 long val;
1717 int err;
1718 int i;
1719
1720 /*
1721 * The regular update, every sysctl_stat_interval, may come later
1722 * than expected: leaving a significant amount in per_cpu buckets.
1723 * This is particularly misleading when checking a quantity of HUGE
1724 * pages, immediately after running a test. /proc/sys/vm/stat_refresh,
1725 * which can equally be echo'ed to or cat'ted from (by root),
1726 * can be used to update the stats just before reading them.
1727 *
c41f012a 1728 * Oh, and since global_zone_page_state() etc. are so careful to hide
52b6f46b
HD
1729 * transiently negative values, report an error here if any of
1730 * the stats is negative, so we know to go looking for imbalance.
1731 */
1732 err = schedule_on_each_cpu(refresh_vm_stats);
1733 if (err)
1734 return err;
1735 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++) {
75ef7184 1736 val = atomic_long_read(&vm_zone_stat[i]);
52b6f46b 1737 if (val < 0) {
c822f622
JW
1738 pr_warn("%s: %s %ld\n",
1739 __func__, vmstat_text[i], val);
1740 err = -EINVAL;
52b6f46b
HD
1741 }
1742 }
3a321d2a
KW
1743#ifdef CONFIG_NUMA
1744 for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++) {
1745 val = atomic_long_read(&vm_numa_stat[i]);
1746 if (val < 0) {
1747 pr_warn("%s: %s %ld\n",
1748 __func__, vmstat_text[i + NR_VM_ZONE_STAT_ITEMS], val);
1749 err = -EINVAL;
1750 }
1751 }
1752#endif
52b6f46b
HD
1753 if (err)
1754 return err;
1755 if (write)
1756 *ppos += *lenp;
1757 else
1758 *lenp = 0;
1759 return 0;
1760}
1761#endif /* CONFIG_PROC_FS */
1762
d1187ed2
CL
1763static void vmstat_update(struct work_struct *w)
1764{
0eb77e98 1765 if (refresh_cpu_vm_stats(true)) {
7cc36bbd
CL
1766 /*
1767 * Counters were updated so we expect more updates
1768 * to occur in the future. Keep on running the
1769 * update worker thread.
1770 */
ce612879 1771 queue_delayed_work_on(smp_processor_id(), mm_percpu_wq,
f01f17d3
MH
1772 this_cpu_ptr(&vmstat_work),
1773 round_jiffies_relative(sysctl_stat_interval));
7cc36bbd
CL
1774 }
1775}
1776
0eb77e98
CL
1777/*
1778 * Switch off vmstat processing and then fold all the remaining differentials
1779 * until the diffs stay at zero. The function is used by NOHZ and can only be
1780 * invoked when tick processing is not active.
1781 */
7cc36bbd
CL
1782/*
1783 * Check if the diffs for a certain cpu indicate that
1784 * an update is needed.
1785 */
1786static bool need_update(int cpu)
1787{
1788 struct zone *zone;
1789
1790 for_each_populated_zone(zone) {
1791 struct per_cpu_pageset *p = per_cpu_ptr(zone->pageset, cpu);
1792
1793 BUILD_BUG_ON(sizeof(p->vm_stat_diff[0]) != 1);
3a321d2a 1794#ifdef CONFIG_NUMA
1d90ca89 1795 BUILD_BUG_ON(sizeof(p->vm_numa_stat_diff[0]) != 2);
3a321d2a 1796#endif
63803222 1797
7cc36bbd
CL
1798 /*
1799 * The fast way of checking if there are any vmstat diffs.
1800 * This works because the diffs are byte sized items.
1801 */
1802 if (memchr_inv(p->vm_stat_diff, 0, NR_VM_ZONE_STAT_ITEMS))
1803 return true;
3a321d2a
KW
1804#ifdef CONFIG_NUMA
1805 if (memchr_inv(p->vm_numa_stat_diff, 0, NR_VM_NUMA_STAT_ITEMS))
1806 return true;
1807#endif
7cc36bbd
CL
1808 }
1809 return false;
1810}
1811
7b8da4c7
CL
1812/*
1813 * Switch off vmstat processing and then fold all the remaining differentials
1814 * until the diffs stay at zero. The function is used by NOHZ and can only be
1815 * invoked when tick processing is not active.
1816 */
f01f17d3
MH
1817void quiet_vmstat(void)
1818{
1819 if (system_state != SYSTEM_RUNNING)
1820 return;
1821
7b8da4c7 1822 if (!delayed_work_pending(this_cpu_ptr(&vmstat_work)))
f01f17d3
MH
1823 return;
1824
1825 if (!need_update(smp_processor_id()))
1826 return;
1827
1828 /*
1829 * Just refresh counters and do not care about the pending delayed
1830 * vmstat_update. It doesn't fire that often to matter and canceling
1831 * it would be too expensive from this path.
1832 * vmstat_shepherd will take care about that for us.
1833 */
1834 refresh_cpu_vm_stats(false);
1835}
1836
7cc36bbd
CL
1837/*
1838 * Shepherd worker thread that checks the
1839 * differentials of processors that have their worker
1840 * threads for vm statistics updates disabled because of
1841 * inactivity.
1842 */
1843static void vmstat_shepherd(struct work_struct *w);
1844
0eb77e98 1845static DECLARE_DEFERRABLE_WORK(shepherd, vmstat_shepherd);
7cc36bbd
CL
1846
1847static void vmstat_shepherd(struct work_struct *w)
1848{
1849 int cpu;
1850
1851 get_online_cpus();
1852 /* Check processors whose vmstat worker threads have been disabled */
7b8da4c7 1853 for_each_online_cpu(cpu) {
f01f17d3 1854 struct delayed_work *dw = &per_cpu(vmstat_work, cpu);
7cc36bbd 1855
7b8da4c7 1856 if (!delayed_work_pending(dw) && need_update(cpu))
ce612879 1857 queue_delayed_work_on(cpu, mm_percpu_wq, dw, 0);
f01f17d3 1858 }
7cc36bbd
CL
1859 put_online_cpus();
1860
1861 schedule_delayed_work(&shepherd,
98f4ebb2 1862 round_jiffies_relative(sysctl_stat_interval));
d1187ed2
CL
1863}
1864
7cc36bbd 1865static void __init start_shepherd_timer(void)
d1187ed2 1866{
7cc36bbd
CL
1867 int cpu;
1868
1869 for_each_possible_cpu(cpu)
ccde8bd4 1870 INIT_DEFERRABLE_WORK(per_cpu_ptr(&vmstat_work, cpu),
7cc36bbd
CL
1871 vmstat_update);
1872
7cc36bbd
CL
1873 schedule_delayed_work(&shepherd,
1874 round_jiffies_relative(sysctl_stat_interval));
d1187ed2
CL
1875}
1876
03e86dba
TC
1877static void __init init_cpu_node_state(void)
1878{
4c501327 1879 int node;
03e86dba 1880
4c501327
SAS
1881 for_each_online_node(node) {
1882 if (cpumask_weight(cpumask_of_node(node)) > 0)
1883 node_set_state(node, N_CPU);
1884 }
03e86dba
TC
1885}
1886
5438da97
SAS
1887static int vmstat_cpu_online(unsigned int cpu)
1888{
1889 refresh_zone_stat_thresholds();
1890 node_set_state(cpu_to_node(cpu), N_CPU);
1891 return 0;
1892}
1893
1894static int vmstat_cpu_down_prep(unsigned int cpu)
1895{
1896 cancel_delayed_work_sync(&per_cpu(vmstat_work, cpu));
1897 return 0;
1898}
1899
1900static int vmstat_cpu_dead(unsigned int cpu)
807a1bd2 1901{
4c501327 1902 const struct cpumask *node_cpus;
5438da97 1903 int node;
807a1bd2 1904
5438da97
SAS
1905 node = cpu_to_node(cpu);
1906
1907 refresh_zone_stat_thresholds();
4c501327
SAS
1908 node_cpus = cpumask_of_node(node);
1909 if (cpumask_weight(node_cpus) > 0)
5438da97 1910 return 0;
807a1bd2
TK
1911
1912 node_clear_state(node, N_CPU);
5438da97 1913 return 0;
807a1bd2
TK
1914}
1915
8f32f7e5 1916#endif
df9ecaba 1917
ce612879
MH
1918struct workqueue_struct *mm_percpu_wq;
1919
597b7305 1920void __init init_mm_internals(void)
df9ecaba 1921{
ce612879 1922 int ret __maybe_unused;
5438da97 1923
80d136e1 1924 mm_percpu_wq = alloc_workqueue("mm_percpu_wq", WQ_MEM_RECLAIM, 0);
ce612879
MH
1925
1926#ifdef CONFIG_SMP
5438da97
SAS
1927 ret = cpuhp_setup_state_nocalls(CPUHP_MM_VMSTAT_DEAD, "mm/vmstat:dead",
1928 NULL, vmstat_cpu_dead);
1929 if (ret < 0)
1930 pr_err("vmstat: failed to register 'dead' hotplug state\n");
1931
1932 ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "mm/vmstat:online",
1933 vmstat_cpu_online,
1934 vmstat_cpu_down_prep);
1935 if (ret < 0)
1936 pr_err("vmstat: failed to register 'online' hotplug state\n");
1937
1938 get_online_cpus();
03e86dba 1939 init_cpu_node_state();
5438da97 1940 put_online_cpus();
d1187ed2 1941
7cc36bbd 1942 start_shepherd_timer();
8f32f7e5
AD
1943#endif
1944#ifdef CONFIG_PROC_FS
9d85e15f
AK
1945 proc_create("buddyinfo", 0444, NULL, &buddyinfo_file_operations);
1946 proc_create("pagetypeinfo", 0444, NULL, &pagetypeinfo_file_operations);
1947 proc_create("vmstat", 0444, NULL, &vmstat_file_operations);
1948 proc_create("zoneinfo", 0444, NULL, &zoneinfo_file_operations);
8f32f7e5 1949#endif
df9ecaba 1950}
d7a5752c
MG
1951
1952#if defined(CONFIG_DEBUG_FS) && defined(CONFIG_COMPACTION)
d7a5752c
MG
1953
1954/*
1955 * Return an index indicating how much of the available free memory is
1956 * unusable for an allocation of the requested size.
1957 */
1958static int unusable_free_index(unsigned int order,
1959 struct contig_page_info *info)
1960{
1961 /* No free memory is interpreted as all free memory is unusable */
1962 if (info->free_pages == 0)
1963 return 1000;
1964
1965 /*
1966 * Index should be a value between 0 and 1. Return a value to 3
1967 * decimal places.
1968 *
1969 * 0 => no fragmentation
1970 * 1 => high fragmentation
1971 */
1972 return div_u64((info->free_pages - (info->free_blocks_suitable << order)) * 1000ULL, info->free_pages);
1973
1974}
1975
1976static void unusable_show_print(struct seq_file *m,
1977 pg_data_t *pgdat, struct zone *zone)
1978{
1979 unsigned int order;
1980 int index;
1981 struct contig_page_info info;
1982
1983 seq_printf(m, "Node %d, zone %8s ",
1984 pgdat->node_id,
1985 zone->name);
1986 for (order = 0; order < MAX_ORDER; ++order) {
1987 fill_contig_page_info(zone, order, &info);
1988 index = unusable_free_index(order, &info);
1989 seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
1990 }
1991
1992 seq_putc(m, '\n');
1993}
1994
1995/*
1996 * Display unusable free space index
1997 *
1998 * The unusable free space index measures how much of the available free
1999 * memory cannot be used to satisfy an allocation of a given size and is a
2000 * value between 0 and 1. The higher the value, the more of free memory is
2001 * unusable and by implication, the worse the external fragmentation is. This
2002 * can be expressed as a percentage by multiplying by 100.
2003 */
2004static int unusable_show(struct seq_file *m, void *arg)
2005{
2006 pg_data_t *pgdat = (pg_data_t *)arg;
2007
2008 /* check memoryless node */
a47b53c5 2009 if (!node_state(pgdat->node_id, N_MEMORY))
d7a5752c
MG
2010 return 0;
2011
727c080f 2012 walk_zones_in_node(m, pgdat, true, false, unusable_show_print);
d7a5752c
MG
2013
2014 return 0;
2015}
2016
2017static const struct seq_operations unusable_op = {
2018 .start = frag_start,
2019 .next = frag_next,
2020 .stop = frag_stop,
2021 .show = unusable_show,
2022};
2023
2024static int unusable_open(struct inode *inode, struct file *file)
2025{
2026 return seq_open(file, &unusable_op);
2027}
2028
2029static const struct file_operations unusable_file_ops = {
2030 .open = unusable_open,
2031 .read = seq_read,
2032 .llseek = seq_lseek,
2033 .release = seq_release,
2034};
2035
f1a5ab12
MG
2036static void extfrag_show_print(struct seq_file *m,
2037 pg_data_t *pgdat, struct zone *zone)
2038{
2039 unsigned int order;
2040 int index;
2041
2042 /* Alloc on stack as interrupts are disabled for zone walk */
2043 struct contig_page_info info;
2044
2045 seq_printf(m, "Node %d, zone %8s ",
2046 pgdat->node_id,
2047 zone->name);
2048 for (order = 0; order < MAX_ORDER; ++order) {
2049 fill_contig_page_info(zone, order, &info);
56de7263 2050 index = __fragmentation_index(order, &info);
f1a5ab12
MG
2051 seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
2052 }
2053
2054 seq_putc(m, '\n');
2055}
2056
2057/*
2058 * Display fragmentation index for orders that allocations would fail for
2059 */
2060static int extfrag_show(struct seq_file *m, void *arg)
2061{
2062 pg_data_t *pgdat = (pg_data_t *)arg;
2063
727c080f 2064 walk_zones_in_node(m, pgdat, true, false, extfrag_show_print);
f1a5ab12
MG
2065
2066 return 0;
2067}
2068
2069static const struct seq_operations extfrag_op = {
2070 .start = frag_start,
2071 .next = frag_next,
2072 .stop = frag_stop,
2073 .show = extfrag_show,
2074};
2075
2076static int extfrag_open(struct inode *inode, struct file *file)
2077{
2078 return seq_open(file, &extfrag_op);
2079}
2080
2081static const struct file_operations extfrag_file_ops = {
2082 .open = extfrag_open,
2083 .read = seq_read,
2084 .llseek = seq_lseek,
2085 .release = seq_release,
2086};
2087
d7a5752c
MG
2088static int __init extfrag_debug_init(void)
2089{
bde8bd8a
S
2090 struct dentry *extfrag_debug_root;
2091
d7a5752c
MG
2092 extfrag_debug_root = debugfs_create_dir("extfrag", NULL);
2093 if (!extfrag_debug_root)
2094 return -ENOMEM;
2095
2096 if (!debugfs_create_file("unusable_index", 0444,
2097 extfrag_debug_root, NULL, &unusable_file_ops))
bde8bd8a 2098 goto fail;
d7a5752c 2099
f1a5ab12
MG
2100 if (!debugfs_create_file("extfrag_index", 0444,
2101 extfrag_debug_root, NULL, &extfrag_file_ops))
bde8bd8a 2102 goto fail;
f1a5ab12 2103
d7a5752c 2104 return 0;
bde8bd8a
S
2105fail:
2106 debugfs_remove_recursive(extfrag_debug_root);
2107 return -ENOMEM;
d7a5752c
MG
2108}
2109
2110module_init(extfrag_debug_init);
2111#endif