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