tcp: make sure EPOLLOUT wont be missed
[linux-stable.git] / mm / vmstat.c
blob28c45c26f901b7b0de57d93b73bfe0fbff73793b
1 /*
2 * linux/mm/vmstat.c
4 * Manages VM statistics
5 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
7 * zoned VM statistics
8 * Copyright (C) 2006 Silicon Graphics, Inc.,
9 * Christoph Lameter <christoph@lameter.com>
10 * Copyright (C) 2008-2014 Christoph Lameter
12 #include <linux/fs.h>
13 #include <linux/mm.h>
14 #include <linux/err.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/cpu.h>
18 #include <linux/cpumask.h>
19 #include <linux/vmstat.h>
20 #include <linux/proc_fs.h>
21 #include <linux/seq_file.h>
22 #include <linux/debugfs.h>
23 #include <linux/sched.h>
24 #include <linux/math64.h>
25 #include <linux/writeback.h>
26 #include <linux/compaction.h>
27 #include <linux/mm_inline.h>
28 #include <linux/page_ext.h>
29 #include <linux/page_owner.h>
31 #include "internal.h"
33 #define NUMA_STATS_THRESHOLD (U16_MAX - 2)
35 #ifdef CONFIG_VM_EVENT_COUNTERS
36 DEFINE_PER_CPU(struct vm_event_state, vm_event_states) = {{0}};
37 EXPORT_PER_CPU_SYMBOL(vm_event_states);
39 static void sum_vm_events(unsigned long *ret)
41 int cpu;
42 int i;
44 memset(ret, 0, NR_VM_EVENT_ITEMS * sizeof(unsigned long));
46 for_each_online_cpu(cpu) {
47 struct vm_event_state *this = &per_cpu(vm_event_states, cpu);
49 for (i = 0; i < NR_VM_EVENT_ITEMS; i++)
50 ret[i] += this->event[i];
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.
59 void all_vm_events(unsigned long *ret)
61 get_online_cpus();
62 sum_vm_events(ret);
63 put_online_cpus();
65 EXPORT_SYMBOL_GPL(all_vm_events);
68 * Fold the foreign cpu events into our own.
70 * This is adding to the events on one processor
71 * but keeps the global counts constant.
73 void vm_events_fold_cpu(int cpu)
75 struct vm_event_state *fold_state = &per_cpu(vm_event_states, cpu);
76 int i;
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;
84 #endif /* CONFIG_VM_EVENT_COUNTERS */
87 * Manage combined zone based / global counters
89 * vm_stat contains the global counters
91 atomic_long_t vm_zone_stat[NR_VM_ZONE_STAT_ITEMS] __cacheline_aligned_in_smp;
92 atomic_long_t vm_numa_stat[NR_VM_NUMA_STAT_ITEMS] __cacheline_aligned_in_smp;
93 atomic_long_t vm_node_stat[NR_VM_NODE_STAT_ITEMS] __cacheline_aligned_in_smp;
94 EXPORT_SYMBOL(vm_zone_stat);
95 EXPORT_SYMBOL(vm_numa_stat);
96 EXPORT_SYMBOL(vm_node_stat);
98 #ifdef CONFIG_SMP
100 int calculate_pressure_threshold(struct zone *zone)
102 int threshold;
103 int watermark_distance;
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
113 watermark_distance = low_wmark_pages(zone) - min_wmark_pages(zone);
114 threshold = max(1, (int)(watermark_distance / num_online_cpus()));
117 * Maximum threshold is 125
119 threshold = min(125, threshold);
121 return threshold;
124 int calculate_normal_threshold(struct zone *zone)
126 int threshold;
127 int mem; /* memory in 128 MB units */
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.
135 * Some sample thresholds:
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
159 mem = zone->managed_pages >> (27 - PAGE_SHIFT);
161 threshold = 2 * fls(num_online_cpus()) * (1 + fls(mem));
164 * Maximum threshold is 125
166 threshold = min(125, threshold);
168 return threshold;
172 * Refresh the thresholds for each zone.
174 void refresh_zone_stat_thresholds(void)
176 struct pglist_data *pgdat;
177 struct zone *zone;
178 int cpu;
179 int threshold;
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;
188 for_each_populated_zone(zone) {
189 struct pglist_data *pgdat = zone->zone_pgdat;
190 unsigned long max_drift, tolerate_drift;
192 threshold = calculate_normal_threshold(zone);
194 for_each_online_cpu(cpu) {
195 int pgdat_threshold;
197 per_cpu_ptr(zone->pageset, cpu)->stat_threshold
198 = threshold;
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);
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
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;
219 void set_pgdat_percpu_threshold(pg_data_t *pgdat,
220 int (*calculate_pressure)(struct zone *))
222 struct zone *zone;
223 int cpu;
224 int threshold;
225 int i;
227 for (i = 0; i < pgdat->nr_zones; i++) {
228 zone = &pgdat->node_zones[i];
229 if (!zone->percpu_drift_mark)
230 continue;
232 threshold = (*calculate_pressure)(zone);
233 for_each_online_cpu(cpu)
234 per_cpu_ptr(zone->pageset, cpu)->stat_threshold
235 = threshold;
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.
244 void __mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
245 long delta)
247 struct per_cpu_pageset __percpu *pcp = zone->pageset;
248 s8 __percpu *p = pcp->vm_stat_diff + item;
249 long x;
250 long t;
252 x = delta + __this_cpu_read(*p);
254 t = __this_cpu_read(pcp->stat_threshold);
256 if (unlikely(x > t || x < -t)) {
257 zone_page_state_add(x, zone, item);
258 x = 0;
260 __this_cpu_write(*p, x);
262 EXPORT_SYMBOL(__mod_zone_page_state);
264 void __mod_node_page_state(struct pglist_data *pgdat, enum node_stat_item item,
265 long delta)
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;
272 x = delta + __this_cpu_read(*p);
274 t = __this_cpu_read(pcp->stat_threshold);
276 if (unlikely(x > t || x < -t)) {
277 node_page_state_add(x, pgdat, item);
278 x = 0;
280 __this_cpu_write(*p, x);
282 EXPORT_SYMBOL(__mod_node_page_state);
285 * Optimized increment and decrement functions.
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.
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.
294 * The increment or decrement is known and therefore one boundary check can
295 * be omitted.
297 * NOTE: These functions are very performance sensitive. Change only
298 * with care.
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.
307 void __inc_zone_state(struct zone *zone, enum zone_stat_item item)
309 struct per_cpu_pageset __percpu *pcp = zone->pageset;
310 s8 __percpu *p = pcp->vm_stat_diff + item;
311 s8 v, t;
313 v = __this_cpu_inc_return(*p);
314 t = __this_cpu_read(pcp->stat_threshold);
315 if (unlikely(v > t)) {
316 s8 overstep = t >> 1;
318 zone_page_state_add(v + overstep, zone, item);
319 __this_cpu_write(*p, -overstep);
323 void __inc_node_state(struct pglist_data *pgdat, enum node_stat_item item)
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;
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;
334 node_page_state_add(v + overstep, pgdat, item);
335 __this_cpu_write(*p, -overstep);
339 void __inc_zone_page_state(struct page *page, enum zone_stat_item item)
341 __inc_zone_state(page_zone(page), item);
343 EXPORT_SYMBOL(__inc_zone_page_state);
345 void __inc_node_page_state(struct page *page, enum node_stat_item item)
347 __inc_node_state(page_pgdat(page), item);
349 EXPORT_SYMBOL(__inc_node_page_state);
351 void __dec_zone_state(struct zone *zone, enum zone_stat_item item)
353 struct per_cpu_pageset __percpu *pcp = zone->pageset;
354 s8 __percpu *p = pcp->vm_stat_diff + item;
355 s8 v, t;
357 v = __this_cpu_dec_return(*p);
358 t = __this_cpu_read(pcp->stat_threshold);
359 if (unlikely(v < - t)) {
360 s8 overstep = t >> 1;
362 zone_page_state_add(v - overstep, zone, item);
363 __this_cpu_write(*p, overstep);
367 void __dec_node_state(struct pglist_data *pgdat, enum node_stat_item item)
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;
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;
378 node_page_state_add(v - overstep, pgdat, item);
379 __this_cpu_write(*p, overstep);
383 void __dec_zone_page_state(struct page *page, enum zone_stat_item item)
385 __dec_zone_state(page_zone(page), item);
387 EXPORT_SYMBOL(__dec_zone_page_state);
389 void __dec_node_page_state(struct page *page, enum node_stat_item item)
391 __dec_node_state(page_pgdat(page), item);
393 EXPORT_SYMBOL(__dec_node_page_state);
395 #ifdef CONFIG_HAVE_CMPXCHG_LOCAL
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.
400 * mod_state() modifies the zone counter state through atomic per cpu
401 * operations.
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
408 static inline void mod_zone_state(struct zone *zone,
409 enum zone_stat_item item, long delta, int overstep_mode)
411 struct per_cpu_pageset __percpu *pcp = zone->pageset;
412 s8 __percpu *p = pcp->vm_stat_diff + item;
413 long o, n, t, z;
415 do {
416 z = 0; /* overflow to zone counters */
419 * The fetching of the stat_threshold is racy. We may apply
420 * a counter threshold to the wrong the cpu if we get
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.
425 * Most of the time the thresholds are the same anyways
426 * for all cpus in a zone.
428 t = this_cpu_read(pcp->stat_threshold);
430 o = this_cpu_read(*p);
431 n = delta + o;
433 if (n > t || n < -t) {
434 int os = overstep_mode * (t >> 1) ;
436 /* Overflow must be added to zone counters */
437 z = n + os;
438 n = -os;
440 } while (this_cpu_cmpxchg(*p, o, n) != o);
442 if (z)
443 zone_page_state_add(z, zone, item);
446 void mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
447 long delta)
449 mod_zone_state(zone, item, delta, 0);
451 EXPORT_SYMBOL(mod_zone_page_state);
453 void inc_zone_page_state(struct page *page, enum zone_stat_item item)
455 mod_zone_state(page_zone(page), item, 1, 1);
457 EXPORT_SYMBOL(inc_zone_page_state);
459 void dec_zone_page_state(struct page *page, enum zone_stat_item item)
461 mod_zone_state(page_zone(page), item, -1, -1);
463 EXPORT_SYMBOL(dec_zone_page_state);
465 static inline void mod_node_state(struct pglist_data *pgdat,
466 enum node_stat_item item, int delta, int overstep_mode)
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;
472 do {
473 z = 0; /* overflow to node counters */
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.
482 * Most of the time the thresholds are the same anyways
483 * for all cpus in a node.
485 t = this_cpu_read(pcp->stat_threshold);
487 o = this_cpu_read(*p);
488 n = delta + o;
490 if (n > t || n < -t) {
491 int os = overstep_mode * (t >> 1) ;
493 /* Overflow must be added to node counters */
494 z = n + os;
495 n = -os;
497 } while (this_cpu_cmpxchg(*p, o, n) != o);
499 if (z)
500 node_page_state_add(z, pgdat, item);
503 void mod_node_page_state(struct pglist_data *pgdat, enum node_stat_item item,
504 long delta)
506 mod_node_state(pgdat, item, delta, 0);
508 EXPORT_SYMBOL(mod_node_page_state);
510 void inc_node_state(struct pglist_data *pgdat, enum node_stat_item item)
512 mod_node_state(pgdat, item, 1, 1);
515 void inc_node_page_state(struct page *page, enum node_stat_item item)
517 mod_node_state(page_pgdat(page), item, 1, 1);
519 EXPORT_SYMBOL(inc_node_page_state);
521 void dec_node_page_state(struct page *page, enum node_stat_item item)
523 mod_node_state(page_pgdat(page), item, -1, -1);
525 EXPORT_SYMBOL(dec_node_page_state);
526 #else
528 * Use interrupt disable to serialize counter updates
530 void mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
531 long delta)
533 unsigned long flags;
535 local_irq_save(flags);
536 __mod_zone_page_state(zone, item, delta);
537 local_irq_restore(flags);
539 EXPORT_SYMBOL(mod_zone_page_state);
541 void inc_zone_page_state(struct page *page, enum zone_stat_item item)
543 unsigned long flags;
544 struct zone *zone;
546 zone = page_zone(page);
547 local_irq_save(flags);
548 __inc_zone_state(zone, item);
549 local_irq_restore(flags);
551 EXPORT_SYMBOL(inc_zone_page_state);
553 void dec_zone_page_state(struct page *page, enum zone_stat_item item)
555 unsigned long flags;
557 local_irq_save(flags);
558 __dec_zone_page_state(page, item);
559 local_irq_restore(flags);
561 EXPORT_SYMBOL(dec_zone_page_state);
563 void inc_node_state(struct pglist_data *pgdat, enum node_stat_item item)
565 unsigned long flags;
567 local_irq_save(flags);
568 __inc_node_state(pgdat, item);
569 local_irq_restore(flags);
571 EXPORT_SYMBOL(inc_node_state);
573 void mod_node_page_state(struct pglist_data *pgdat, enum node_stat_item item,
574 long delta)
576 unsigned long flags;
578 local_irq_save(flags);
579 __mod_node_page_state(pgdat, item, delta);
580 local_irq_restore(flags);
582 EXPORT_SYMBOL(mod_node_page_state);
584 void inc_node_page_state(struct page *page, enum node_stat_item item)
586 unsigned long flags;
587 struct pglist_data *pgdat;
589 pgdat = page_pgdat(page);
590 local_irq_save(flags);
591 __inc_node_state(pgdat, item);
592 local_irq_restore(flags);
594 EXPORT_SYMBOL(inc_node_page_state);
596 void dec_node_page_state(struct page *page, enum node_stat_item item)
598 unsigned long flags;
600 local_irq_save(flags);
601 __dec_node_page_state(page, item);
602 local_irq_restore(flags);
604 EXPORT_SYMBOL(dec_node_page_state);
605 #endif
608 * Fold a differential into the global counters.
609 * Returns the number of counters updated.
611 #ifdef CONFIG_NUMA
612 static int fold_diff(int *zone_diff, int *numa_diff, int *node_diff)
614 int i;
615 int changes = 0;
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++;
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++;
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++;
634 return changes;
636 #else
637 static int fold_diff(int *zone_diff, int *node_diff)
639 int i;
640 int changes = 0;
642 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
643 if (zone_diff[i]) {
644 atomic_long_add(zone_diff[i], &vm_zone_stat[i]);
645 changes++;
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]);
651 changes++;
653 return changes;
655 #endif /* CONFIG_NUMA */
658 * Update the zone counters for the current cpu.
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.
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.
671 * The function returns the number of global counters updated.
673 static int refresh_cpu_vm_stats(bool do_pagesets)
675 struct pglist_data *pgdat;
676 struct zone *zone;
677 int i;
678 int global_zone_diff[NR_VM_ZONE_STAT_ITEMS] = { 0, };
679 #ifdef CONFIG_NUMA
680 int global_numa_diff[NR_VM_NUMA_STAT_ITEMS] = { 0, };
681 #endif
682 int global_node_diff[NR_VM_NODE_STAT_ITEMS] = { 0, };
683 int changes = 0;
685 for_each_populated_zone(zone) {
686 struct per_cpu_pageset __percpu *p = zone->pageset;
688 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++) {
689 int v;
691 v = this_cpu_xchg(p->vm_stat_diff[i], 0);
692 if (v) {
694 atomic_long_add(v, &zone->vm_stat[i]);
695 global_zone_diff[i] += v;
696 #ifdef CONFIG_NUMA
697 /* 3 seconds idle till flush */
698 __this_cpu_write(p->expire, 3);
699 #endif
702 #ifdef CONFIG_NUMA
703 for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++) {
704 int v;
706 v = this_cpu_xchg(p->vm_numa_stat_diff[i], 0);
707 if (v) {
709 atomic_long_add(v, &zone->vm_numa_stat[i]);
710 global_numa_diff[i] += v;
711 __this_cpu_write(p->expire, 3);
715 if (do_pagesets) {
716 cond_resched();
718 * Deal with draining the remote pageset of this
719 * processor
721 * Check if there are pages remaining in this pageset
722 * if not then there is nothing to expire.
724 if (!__this_cpu_read(p->expire) ||
725 !__this_cpu_read(p->pcp.count))
726 continue;
729 * We never drain zones local to this processor.
731 if (zone_to_nid(zone) == numa_node_id()) {
732 __this_cpu_write(p->expire, 0);
733 continue;
736 if (__this_cpu_dec_return(p->expire))
737 continue;
739 if (__this_cpu_read(p->pcp.count)) {
740 drain_zone_pages(zone, this_cpu_ptr(&p->pcp));
741 changes++;
744 #endif
747 for_each_online_pgdat(pgdat) {
748 struct per_cpu_nodestat __percpu *p = pgdat->per_cpu_nodestats;
750 for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++) {
751 int v;
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;
761 #ifdef CONFIG_NUMA
762 changes += fold_diff(global_zone_diff, global_numa_diff,
763 global_node_diff);
764 #else
765 changes += fold_diff(global_zone_diff, global_node_diff);
766 #endif
767 return changes;
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.
775 void cpu_vm_stats_fold(int cpu)
777 struct pglist_data *pgdat;
778 struct zone *zone;
779 int i;
780 int global_zone_diff[NR_VM_ZONE_STAT_ITEMS] = { 0, };
781 #ifdef CONFIG_NUMA
782 int global_numa_diff[NR_VM_NUMA_STAT_ITEMS] = { 0, };
783 #endif
784 int global_node_diff[NR_VM_NODE_STAT_ITEMS] = { 0, };
786 for_each_populated_zone(zone) {
787 struct per_cpu_pageset *p;
789 p = per_cpu_ptr(zone->pageset, cpu);
791 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
792 if (p->vm_stat_diff[i]) {
793 int v;
795 v = p->vm_stat_diff[i];
796 p->vm_stat_diff[i] = 0;
797 atomic_long_add(v, &zone->vm_stat[i]);
798 global_zone_diff[i] += v;
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;
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;
811 #endif
814 for_each_online_pgdat(pgdat) {
815 struct per_cpu_nodestat *p;
817 p = per_cpu_ptr(pgdat->per_cpu_nodestats, cpu);
819 for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++)
820 if (p->vm_node_stat_diff[i]) {
821 int v;
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;
830 #ifdef CONFIG_NUMA
831 fold_diff(global_zone_diff, global_numa_diff, global_node_diff);
832 #else
833 fold_diff(global_zone_diff, global_node_diff);
834 #endif
838 * this is only called if !populated_zone(zone), which implies no other users of
839 * pset->vm_stat_diff[] exsist.
841 void drain_zonestat(struct zone *zone, struct per_cpu_pageset *pset)
843 int i;
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]);
850 atomic_long_add(v, &vm_zone_stat[i]);
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];
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]);
862 #endif
864 #endif
866 #ifdef CONFIG_NUMA
867 void __inc_numa_state(struct zone *zone,
868 enum numa_stat_item item)
870 struct per_cpu_pageset __percpu *pcp = zone->pageset;
871 u16 __percpu *p = pcp->vm_numa_stat_diff + item;
872 u16 v;
874 v = __this_cpu_inc_return(*p);
876 if (unlikely(v > NUMA_STATS_THRESHOLD)) {
877 zone_numa_state_add(v, zone, item);
878 __this_cpu_write(*p, 0);
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.
887 unsigned long sum_zone_node_page_state(int node,
888 enum zone_stat_item item)
890 struct zone *zones = NODE_DATA(node)->node_zones;
891 int i;
892 unsigned long count = 0;
894 for (i = 0; i < MAX_NR_ZONES; i++)
895 count += zone_page_state(zones + i, item);
897 return count;
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.
904 unsigned long sum_zone_numa_state(int node,
905 enum numa_stat_item item)
907 struct zone *zones = NODE_DATA(node)->node_zones;
908 int i;
909 unsigned long count = 0;
911 for (i = 0; i < MAX_NR_ZONES; i++)
912 count += zone_numa_state_snapshot(zones + i, item);
914 return count;
918 * Determine the per node value of a stat item.
920 unsigned long node_page_state(struct pglist_data *pgdat,
921 enum node_stat_item item)
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;
930 #endif
932 #ifdef CONFIG_COMPACTION
934 struct contig_page_info {
935 unsigned long free_pages;
936 unsigned long free_blocks_total;
937 unsigned long free_blocks_suitable;
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
948 static void fill_contig_page_info(struct zone *zone,
949 unsigned int suitable_order,
950 struct contig_page_info *info)
952 unsigned int order;
954 info->free_pages = 0;
955 info->free_blocks_total = 0;
956 info->free_blocks_suitable = 0;
958 for (order = 0; order < MAX_ORDER; order++) {
959 unsigned long blocks;
961 /* Count number of free blocks */
962 blocks = zone->free_area[order].nr_free;
963 info->free_blocks_total += blocks;
965 /* Count free base pages */
966 info->free_pages += blocks << order;
968 /* Count the suitable free blocks */
969 if (order >= suitable_order)
970 info->free_blocks_suitable += blocks <<
971 (order - suitable_order);
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
982 static int __fragmentation_index(unsigned int order, struct contig_page_info *info)
984 unsigned long requested = 1UL << order;
986 if (WARN_ON_ONCE(order >= MAX_ORDER))
987 return 0;
989 if (!info->free_blocks_total)
990 return 0;
992 /* Fragmentation index only makes sense when a request would fail */
993 if (info->free_blocks_suitable)
994 return -1000;
997 * Index is between 0 and 1 so return within 3 decimal places
999 * 0 => allocation would fail due to lack of memory
1000 * 1 => allocation would fail due to fragmentation
1002 return 1000 - div_u64( (1000+(div_u64(info->free_pages * 1000ULL, requested))), info->free_blocks_total);
1005 /* Same as __fragmentation index but allocs contig_page_info on stack */
1006 int fragmentation_index(struct zone *zone, unsigned int order)
1008 struct contig_page_info info;
1010 fill_contig_page_info(zone, order, &info);
1011 return __fragmentation_index(order, &info);
1013 #endif
1015 #if defined(CONFIG_PROC_FS) || defined(CONFIG_SYSFS) || defined(CONFIG_NUMA)
1016 #ifdef CONFIG_ZONE_DMA
1017 #define TEXT_FOR_DMA(xx) xx "_dma",
1018 #else
1019 #define TEXT_FOR_DMA(xx)
1020 #endif
1022 #ifdef CONFIG_ZONE_DMA32
1023 #define TEXT_FOR_DMA32(xx) xx "_dma32",
1024 #else
1025 #define TEXT_FOR_DMA32(xx)
1026 #endif
1028 #ifdef CONFIG_HIGHMEM
1029 #define TEXT_FOR_HIGHMEM(xx) xx "_high",
1030 #else
1031 #define TEXT_FOR_HIGHMEM(xx)
1032 #endif
1034 #define TEXTS_FOR_ZONES(xx) TEXT_FOR_DMA(xx) TEXT_FOR_DMA32(xx) xx "_normal", \
1035 TEXT_FOR_HIGHMEM(xx) xx "_movable",
1037 const char * const vmstat_text[] = {
1038 /* enum zone_stat_item countes */
1039 "nr_free_pages",
1040 "nr_zone_inactive_anon",
1041 "nr_zone_active_anon",
1042 "nr_zone_inactive_file",
1043 "nr_zone_active_file",
1044 "nr_zone_unevictable",
1045 "nr_zone_write_pending",
1046 "nr_mlock",
1047 "nr_page_table_pages",
1048 "nr_kernel_stack",
1049 "nr_bounce",
1050 #if IS_ENABLED(CONFIG_ZSMALLOC)
1051 "nr_zspages",
1052 #endif
1053 "nr_free_cma",
1055 /* enum numa_stat_item counters */
1056 #ifdef CONFIG_NUMA
1057 "numa_hit",
1058 "numa_miss",
1059 "numa_foreign",
1060 "numa_interleave",
1061 "numa_local",
1062 "numa_other",
1063 #endif
1065 /* Node-based counters */
1066 "nr_inactive_anon",
1067 "nr_active_anon",
1068 "nr_inactive_file",
1069 "nr_active_file",
1070 "nr_unevictable",
1071 "nr_slab_reclaimable",
1072 "nr_slab_unreclaimable",
1073 "nr_isolated_anon",
1074 "nr_isolated_file",
1075 "workingset_refault",
1076 "workingset_activate",
1077 "workingset_nodereclaim",
1078 "nr_anon_pages",
1079 "nr_mapped",
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",
1089 "nr_vmscan_write",
1090 "nr_vmscan_immediate_reclaim",
1091 "nr_dirtied",
1092 "nr_written",
1093 "", /* nr_indirectly_reclaimable */
1095 /* enum writeback_stat_item counters */
1096 "nr_dirty_threshold",
1097 "nr_dirty_background_threshold",
1099 #ifdef CONFIG_VM_EVENT_COUNTERS
1100 /* enum vm_event_item counters */
1101 "pgpgin",
1102 "pgpgout",
1103 "pswpin",
1104 "pswpout",
1106 TEXTS_FOR_ZONES("pgalloc")
1107 TEXTS_FOR_ZONES("allocstall")
1108 TEXTS_FOR_ZONES("pgskip")
1110 "pgfree",
1111 "pgactivate",
1112 "pgdeactivate",
1113 "pglazyfree",
1115 "pgfault",
1116 "pgmajfault",
1117 "pglazyfreed",
1119 "pgrefill",
1120 "pgsteal_kswapd",
1121 "pgsteal_direct",
1122 "pgscan_kswapd",
1123 "pgscan_direct",
1124 "pgscan_direct_throttle",
1126 #ifdef CONFIG_NUMA
1127 "zone_reclaim_failed",
1128 #endif
1129 "pginodesteal",
1130 "slabs_scanned",
1131 "kswapd_inodesteal",
1132 "kswapd_low_wmark_hit_quickly",
1133 "kswapd_high_wmark_hit_quickly",
1134 "pageoutrun",
1136 "pgrotated",
1138 "drop_pagecache",
1139 "drop_slab",
1140 "oom_kill",
1142 #ifdef CONFIG_NUMA_BALANCING
1143 "numa_pte_updates",
1144 "numa_huge_pte_updates",
1145 "numa_hint_faults",
1146 "numa_hint_faults_local",
1147 "numa_pages_migrated",
1148 #endif
1149 #ifdef CONFIG_MIGRATION
1150 "pgmigrate_success",
1151 "pgmigrate_fail",
1152 #endif
1153 #ifdef CONFIG_COMPACTION
1154 "compact_migrate_scanned",
1155 "compact_free_scanned",
1156 "compact_isolated",
1157 "compact_stall",
1158 "compact_fail",
1159 "compact_success",
1160 "compact_daemon_wake",
1161 "compact_daemon_migrate_scanned",
1162 "compact_daemon_free_scanned",
1163 #endif
1165 #ifdef CONFIG_HUGETLB_PAGE
1166 "htlb_buddy_alloc_success",
1167 "htlb_buddy_alloc_fail",
1168 #endif
1169 "unevictable_pgs_culled",
1170 "unevictable_pgs_scanned",
1171 "unevictable_pgs_rescued",
1172 "unevictable_pgs_mlocked",
1173 "unevictable_pgs_munlocked",
1174 "unevictable_pgs_cleared",
1175 "unevictable_pgs_stranded",
1177 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1178 "thp_fault_alloc",
1179 "thp_fault_fallback",
1180 "thp_collapse_alloc",
1181 "thp_collapse_alloc_failed",
1182 "thp_file_alloc",
1183 "thp_file_mapped",
1184 "thp_split_page",
1185 "thp_split_page_failed",
1186 "thp_deferred_split_page",
1187 "thp_split_pmd",
1188 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
1189 "thp_split_pud",
1190 #endif
1191 "thp_zero_page_alloc",
1192 "thp_zero_page_alloc_failed",
1193 "thp_swpout",
1194 "thp_swpout_fallback",
1195 #endif
1196 #ifdef CONFIG_MEMORY_BALLOON
1197 "balloon_inflate",
1198 "balloon_deflate",
1199 #ifdef CONFIG_BALLOON_COMPACTION
1200 "balloon_migrate",
1201 #endif
1202 #endif /* CONFIG_MEMORY_BALLOON */
1203 #ifdef CONFIG_DEBUG_TLBFLUSH
1204 "nr_tlb_remote_flush",
1205 "nr_tlb_remote_flush_received",
1206 "nr_tlb_local_flush_all",
1207 "nr_tlb_local_flush_one",
1208 #endif /* CONFIG_DEBUG_TLBFLUSH */
1210 #ifdef CONFIG_DEBUG_VM_VMACACHE
1211 "vmacache_find_calls",
1212 "vmacache_find_hits",
1213 #endif
1214 #ifdef CONFIG_SWAP
1215 "swap_ra",
1216 "swap_ra_hit",
1217 #endif
1218 #endif /* CONFIG_VM_EVENTS_COUNTERS */
1220 #endif /* CONFIG_PROC_FS || CONFIG_SYSFS || CONFIG_NUMA */
1222 #if (defined(CONFIG_DEBUG_FS) && defined(CONFIG_COMPACTION)) || \
1223 defined(CONFIG_PROC_FS)
1224 static void *frag_start(struct seq_file *m, loff_t *pos)
1226 pg_data_t *pgdat;
1227 loff_t node = *pos;
1229 for (pgdat = first_online_pgdat();
1230 pgdat && node;
1231 pgdat = next_online_pgdat(pgdat))
1232 --node;
1234 return pgdat;
1237 static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
1239 pg_data_t *pgdat = (pg_data_t *)arg;
1241 (*pos)++;
1242 return next_online_pgdat(pgdat);
1245 static void frag_stop(struct seq_file *m, void *arg)
1250 * Walk zones in a node and print using a callback.
1251 * If @assert_populated is true, only use callback for zones that are populated.
1253 static void walk_zones_in_node(struct seq_file *m, pg_data_t *pgdat,
1254 bool assert_populated, bool nolock,
1255 void (*print)(struct seq_file *m, pg_data_t *, struct zone *))
1257 struct zone *zone;
1258 struct zone *node_zones = pgdat->node_zones;
1259 unsigned long flags;
1261 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
1262 if (assert_populated && !populated_zone(zone))
1263 continue;
1265 if (!nolock)
1266 spin_lock_irqsave(&zone->lock, flags);
1267 print(m, pgdat, zone);
1268 if (!nolock)
1269 spin_unlock_irqrestore(&zone->lock, flags);
1272 #endif
1274 #ifdef CONFIG_PROC_FS
1275 static void frag_show_print(struct seq_file *m, pg_data_t *pgdat,
1276 struct zone *zone)
1278 int order;
1280 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
1281 for (order = 0; order < MAX_ORDER; ++order)
1282 seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
1283 seq_putc(m, '\n');
1287 * This walks the free areas for each zone.
1289 static int frag_show(struct seq_file *m, void *arg)
1291 pg_data_t *pgdat = (pg_data_t *)arg;
1292 walk_zones_in_node(m, pgdat, true, false, frag_show_print);
1293 return 0;
1296 static void pagetypeinfo_showfree_print(struct seq_file *m,
1297 pg_data_t *pgdat, struct zone *zone)
1299 int order, mtype;
1301 for (mtype = 0; mtype < MIGRATE_TYPES; mtype++) {
1302 seq_printf(m, "Node %4d, zone %8s, type %12s ",
1303 pgdat->node_id,
1304 zone->name,
1305 migratetype_names[mtype]);
1306 for (order = 0; order < MAX_ORDER; ++order) {
1307 unsigned long freecount = 0;
1308 struct free_area *area;
1309 struct list_head *curr;
1311 area = &(zone->free_area[order]);
1313 list_for_each(curr, &area->free_list[mtype])
1314 freecount++;
1315 seq_printf(m, "%6lu ", freecount);
1317 seq_putc(m, '\n');
1321 /* Print out the free pages at each order for each migatetype */
1322 static int pagetypeinfo_showfree(struct seq_file *m, void *arg)
1324 int order;
1325 pg_data_t *pgdat = (pg_data_t *)arg;
1327 /* Print header */
1328 seq_printf(m, "%-43s ", "Free pages count per migrate type at order");
1329 for (order = 0; order < MAX_ORDER; ++order)
1330 seq_printf(m, "%6d ", order);
1331 seq_putc(m, '\n');
1333 walk_zones_in_node(m, pgdat, true, false, pagetypeinfo_showfree_print);
1335 return 0;
1338 static void pagetypeinfo_showblockcount_print(struct seq_file *m,
1339 pg_data_t *pgdat, struct zone *zone)
1341 int mtype;
1342 unsigned long pfn;
1343 unsigned long start_pfn = zone->zone_start_pfn;
1344 unsigned long end_pfn = zone_end_pfn(zone);
1345 unsigned long count[MIGRATE_TYPES] = { 0, };
1347 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
1348 struct page *page;
1350 page = pfn_to_online_page(pfn);
1351 if (!page)
1352 continue;
1354 /* Watch for unexpected holes punched in the memmap */
1355 if (!memmap_valid_within(pfn, page, zone))
1356 continue;
1358 if (page_zone(page) != zone)
1359 continue;
1361 mtype = get_pageblock_migratetype(page);
1363 if (mtype < MIGRATE_TYPES)
1364 count[mtype]++;
1367 /* Print counts */
1368 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
1369 for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
1370 seq_printf(m, "%12lu ", count[mtype]);
1371 seq_putc(m, '\n');
1374 /* Print out the number of pageblocks for each migratetype */
1375 static int pagetypeinfo_showblockcount(struct seq_file *m, void *arg)
1377 int mtype;
1378 pg_data_t *pgdat = (pg_data_t *)arg;
1380 seq_printf(m, "\n%-23s", "Number of blocks type ");
1381 for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
1382 seq_printf(m, "%12s ", migratetype_names[mtype]);
1383 seq_putc(m, '\n');
1384 walk_zones_in_node(m, pgdat, true, false,
1385 pagetypeinfo_showblockcount_print);
1387 return 0;
1391 * Print out the number of pageblocks for each migratetype that contain pages
1392 * of other types. This gives an indication of how well fallbacks are being
1393 * contained by rmqueue_fallback(). It requires information from PAGE_OWNER
1394 * to determine what is going on
1396 static void pagetypeinfo_showmixedcount(struct seq_file *m, pg_data_t *pgdat)
1398 #ifdef CONFIG_PAGE_OWNER
1399 int mtype;
1401 if (!static_branch_unlikely(&page_owner_inited))
1402 return;
1404 drain_all_pages(NULL);
1406 seq_printf(m, "\n%-23s", "Number of mixed blocks ");
1407 for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
1408 seq_printf(m, "%12s ", migratetype_names[mtype]);
1409 seq_putc(m, '\n');
1411 walk_zones_in_node(m, pgdat, true, true,
1412 pagetypeinfo_showmixedcount_print);
1413 #endif /* CONFIG_PAGE_OWNER */
1417 * This prints out statistics in relation to grouping pages by mobility.
1418 * It is expensive to collect so do not constantly read the file.
1420 static int pagetypeinfo_show(struct seq_file *m, void *arg)
1422 pg_data_t *pgdat = (pg_data_t *)arg;
1424 /* check memoryless node */
1425 if (!node_state(pgdat->node_id, N_MEMORY))
1426 return 0;
1428 seq_printf(m, "Page block order: %d\n", pageblock_order);
1429 seq_printf(m, "Pages per block: %lu\n", pageblock_nr_pages);
1430 seq_putc(m, '\n');
1431 pagetypeinfo_showfree(m, pgdat);
1432 pagetypeinfo_showblockcount(m, pgdat);
1433 pagetypeinfo_showmixedcount(m, pgdat);
1435 return 0;
1438 static const struct seq_operations fragmentation_op = {
1439 .start = frag_start,
1440 .next = frag_next,
1441 .stop = frag_stop,
1442 .show = frag_show,
1445 static int fragmentation_open(struct inode *inode, struct file *file)
1447 return seq_open(file, &fragmentation_op);
1450 static const struct file_operations buddyinfo_file_operations = {
1451 .open = fragmentation_open,
1452 .read = seq_read,
1453 .llseek = seq_lseek,
1454 .release = seq_release,
1457 static const struct seq_operations pagetypeinfo_op = {
1458 .start = frag_start,
1459 .next = frag_next,
1460 .stop = frag_stop,
1461 .show = pagetypeinfo_show,
1464 static int pagetypeinfo_open(struct inode *inode, struct file *file)
1466 return seq_open(file, &pagetypeinfo_op);
1469 static const struct file_operations pagetypeinfo_file_operations = {
1470 .open = pagetypeinfo_open,
1471 .read = seq_read,
1472 .llseek = seq_lseek,
1473 .release = seq_release,
1476 static bool is_zone_first_populated(pg_data_t *pgdat, struct zone *zone)
1478 int zid;
1480 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
1481 struct zone *compare = &pgdat->node_zones[zid];
1483 if (populated_zone(compare))
1484 return zone == compare;
1487 return false;
1490 static void zoneinfo_show_print(struct seq_file *m, pg_data_t *pgdat,
1491 struct zone *zone)
1493 int i;
1494 seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
1495 if (is_zone_first_populated(pgdat, zone)) {
1496 seq_printf(m, "\n per-node stats");
1497 for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++) {
1498 /* Skip hidden vmstat items. */
1499 if (*vmstat_text[i + NR_VM_ZONE_STAT_ITEMS +
1500 NR_VM_NUMA_STAT_ITEMS] == '\0')
1501 continue;
1502 seq_printf(m, "\n %-12s %lu",
1503 vmstat_text[i + NR_VM_ZONE_STAT_ITEMS +
1504 NR_VM_NUMA_STAT_ITEMS],
1505 node_page_state(pgdat, i));
1508 seq_printf(m,
1509 "\n pages free %lu"
1510 "\n min %lu"
1511 "\n low %lu"
1512 "\n high %lu"
1513 "\n spanned %lu"
1514 "\n present %lu"
1515 "\n managed %lu",
1516 zone_page_state(zone, NR_FREE_PAGES),
1517 min_wmark_pages(zone),
1518 low_wmark_pages(zone),
1519 high_wmark_pages(zone),
1520 zone->spanned_pages,
1521 zone->present_pages,
1522 zone->managed_pages);
1524 seq_printf(m,
1525 "\n protection: (%ld",
1526 zone->lowmem_reserve[0]);
1527 for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
1528 seq_printf(m, ", %ld", zone->lowmem_reserve[i]);
1529 seq_putc(m, ')');
1531 /* If unpopulated, no other information is useful */
1532 if (!populated_zone(zone)) {
1533 seq_putc(m, '\n');
1534 return;
1537 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
1538 seq_printf(m, "\n %-12s %lu", vmstat_text[i],
1539 zone_page_state(zone, i));
1541 #ifdef CONFIG_NUMA
1542 for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++)
1543 seq_printf(m, "\n %-12s %lu",
1544 vmstat_text[i + NR_VM_ZONE_STAT_ITEMS],
1545 zone_numa_state_snapshot(zone, i));
1546 #endif
1548 seq_printf(m, "\n pagesets");
1549 for_each_online_cpu(i) {
1550 struct per_cpu_pageset *pageset;
1552 pageset = per_cpu_ptr(zone->pageset, i);
1553 seq_printf(m,
1554 "\n cpu: %i"
1555 "\n count: %i"
1556 "\n high: %i"
1557 "\n batch: %i",
1559 pageset->pcp.count,
1560 pageset->pcp.high,
1561 pageset->pcp.batch);
1562 #ifdef CONFIG_SMP
1563 seq_printf(m, "\n vm stats threshold: %d",
1564 pageset->stat_threshold);
1565 #endif
1567 seq_printf(m,
1568 "\n node_unreclaimable: %u"
1569 "\n start_pfn: %lu"
1570 "\n node_inactive_ratio: %u",
1571 pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES,
1572 zone->zone_start_pfn,
1573 zone->zone_pgdat->inactive_ratio);
1574 seq_putc(m, '\n');
1578 * Output information about zones in @pgdat. All zones are printed regardless
1579 * of whether they are populated or not: lowmem_reserve_ratio operates on the
1580 * set of all zones and userspace would not be aware of such zones if they are
1581 * suppressed here (zoneinfo displays the effect of lowmem_reserve_ratio).
1583 static int zoneinfo_show(struct seq_file *m, void *arg)
1585 pg_data_t *pgdat = (pg_data_t *)arg;
1586 walk_zones_in_node(m, pgdat, false, false, zoneinfo_show_print);
1587 return 0;
1590 static const struct seq_operations zoneinfo_op = {
1591 .start = frag_start, /* iterate over all zones. The same as in
1592 * fragmentation. */
1593 .next = frag_next,
1594 .stop = frag_stop,
1595 .show = zoneinfo_show,
1598 static int zoneinfo_open(struct inode *inode, struct file *file)
1600 return seq_open(file, &zoneinfo_op);
1603 static const struct file_operations zoneinfo_file_operations = {
1604 .open = zoneinfo_open,
1605 .read = seq_read,
1606 .llseek = seq_lseek,
1607 .release = seq_release,
1610 enum writeback_stat_item {
1611 NR_DIRTY_THRESHOLD,
1612 NR_DIRTY_BG_THRESHOLD,
1613 NR_VM_WRITEBACK_STAT_ITEMS,
1616 static void *vmstat_start(struct seq_file *m, loff_t *pos)
1618 unsigned long *v;
1619 int i, stat_items_size;
1621 if (*pos >= ARRAY_SIZE(vmstat_text))
1622 return NULL;
1623 stat_items_size = NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long) +
1624 NR_VM_NUMA_STAT_ITEMS * sizeof(unsigned long) +
1625 NR_VM_NODE_STAT_ITEMS * sizeof(unsigned long) +
1626 NR_VM_WRITEBACK_STAT_ITEMS * sizeof(unsigned long);
1628 #ifdef CONFIG_VM_EVENT_COUNTERS
1629 stat_items_size += sizeof(struct vm_event_state);
1630 #endif
1632 v = kmalloc(stat_items_size, GFP_KERNEL);
1633 m->private = v;
1634 if (!v)
1635 return ERR_PTR(-ENOMEM);
1636 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
1637 v[i] = global_zone_page_state(i);
1638 v += NR_VM_ZONE_STAT_ITEMS;
1640 #ifdef CONFIG_NUMA
1641 for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++)
1642 v[i] = global_numa_state(i);
1643 v += NR_VM_NUMA_STAT_ITEMS;
1644 #endif
1646 for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++)
1647 v[i] = global_node_page_state(i);
1648 v += NR_VM_NODE_STAT_ITEMS;
1650 global_dirty_limits(v + NR_DIRTY_BG_THRESHOLD,
1651 v + NR_DIRTY_THRESHOLD);
1652 v += NR_VM_WRITEBACK_STAT_ITEMS;
1654 #ifdef CONFIG_VM_EVENT_COUNTERS
1655 all_vm_events(v);
1656 v[PGPGIN] /= 2; /* sectors -> kbytes */
1657 v[PGPGOUT] /= 2;
1658 #endif
1659 return (unsigned long *)m->private + *pos;
1662 static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
1664 (*pos)++;
1665 if (*pos >= ARRAY_SIZE(vmstat_text))
1666 return NULL;
1667 return (unsigned long *)m->private + *pos;
1670 static int vmstat_show(struct seq_file *m, void *arg)
1672 unsigned long *l = arg;
1673 unsigned long off = l - (unsigned long *)m->private;
1675 /* Skip hidden vmstat items. */
1676 if (*vmstat_text[off] == '\0')
1677 return 0;
1679 seq_puts(m, vmstat_text[off]);
1680 seq_put_decimal_ull(m, " ", *l);
1681 seq_putc(m, '\n');
1682 return 0;
1685 static void vmstat_stop(struct seq_file *m, void *arg)
1687 kfree(m->private);
1688 m->private = NULL;
1691 static const struct seq_operations vmstat_op = {
1692 .start = vmstat_start,
1693 .next = vmstat_next,
1694 .stop = vmstat_stop,
1695 .show = vmstat_show,
1698 static int vmstat_open(struct inode *inode, struct file *file)
1700 return seq_open(file, &vmstat_op);
1703 static const struct file_operations vmstat_file_operations = {
1704 .open = vmstat_open,
1705 .read = seq_read,
1706 .llseek = seq_lseek,
1707 .release = seq_release,
1709 #endif /* CONFIG_PROC_FS */
1711 #ifdef CONFIG_SMP
1712 static DEFINE_PER_CPU(struct delayed_work, vmstat_work);
1713 int sysctl_stat_interval __read_mostly = HZ;
1715 #ifdef CONFIG_PROC_FS
1716 static void refresh_vm_stats(struct work_struct *work)
1718 refresh_cpu_vm_stats(true);
1721 int vmstat_refresh(struct ctl_table *table, int write,
1722 void __user *buffer, size_t *lenp, loff_t *ppos)
1724 long val;
1725 int err;
1726 int i;
1729 * The regular update, every sysctl_stat_interval, may come later
1730 * than expected: leaving a significant amount in per_cpu buckets.
1731 * This is particularly misleading when checking a quantity of HUGE
1732 * pages, immediately after running a test. /proc/sys/vm/stat_refresh,
1733 * which can equally be echo'ed to or cat'ted from (by root),
1734 * can be used to update the stats just before reading them.
1736 * Oh, and since global_zone_page_state() etc. are so careful to hide
1737 * transiently negative values, report an error here if any of
1738 * the stats is negative, so we know to go looking for imbalance.
1740 err = schedule_on_each_cpu(refresh_vm_stats);
1741 if (err)
1742 return err;
1743 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++) {
1744 val = atomic_long_read(&vm_zone_stat[i]);
1745 if (val < 0) {
1746 pr_warn("%s: %s %ld\n",
1747 __func__, vmstat_text[i], val);
1748 err = -EINVAL;
1751 #ifdef CONFIG_NUMA
1752 for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++) {
1753 val = atomic_long_read(&vm_numa_stat[i]);
1754 if (val < 0) {
1755 pr_warn("%s: %s %ld\n",
1756 __func__, vmstat_text[i + NR_VM_ZONE_STAT_ITEMS], val);
1757 err = -EINVAL;
1760 #endif
1761 if (err)
1762 return err;
1763 if (write)
1764 *ppos += *lenp;
1765 else
1766 *lenp = 0;
1767 return 0;
1769 #endif /* CONFIG_PROC_FS */
1771 static void vmstat_update(struct work_struct *w)
1773 if (refresh_cpu_vm_stats(true)) {
1775 * Counters were updated so we expect more updates
1776 * to occur in the future. Keep on running the
1777 * update worker thread.
1779 queue_delayed_work_on(smp_processor_id(), mm_percpu_wq,
1780 this_cpu_ptr(&vmstat_work),
1781 round_jiffies_relative(sysctl_stat_interval));
1786 * Switch off vmstat processing and then fold all the remaining differentials
1787 * until the diffs stay at zero. The function is used by NOHZ and can only be
1788 * invoked when tick processing is not active.
1791 * Check if the diffs for a certain cpu indicate that
1792 * an update is needed.
1794 static bool need_update(int cpu)
1796 struct zone *zone;
1798 for_each_populated_zone(zone) {
1799 struct per_cpu_pageset *p = per_cpu_ptr(zone->pageset, cpu);
1801 BUILD_BUG_ON(sizeof(p->vm_stat_diff[0]) != 1);
1802 #ifdef CONFIG_NUMA
1803 BUILD_BUG_ON(sizeof(p->vm_numa_stat_diff[0]) != 2);
1804 #endif
1807 * The fast way of checking if there are any vmstat diffs.
1808 * This works because the diffs are byte sized items.
1810 if (memchr_inv(p->vm_stat_diff, 0, NR_VM_ZONE_STAT_ITEMS))
1811 return true;
1812 #ifdef CONFIG_NUMA
1813 if (memchr_inv(p->vm_numa_stat_diff, 0, NR_VM_NUMA_STAT_ITEMS))
1814 return true;
1815 #endif
1817 return false;
1821 * Switch off vmstat processing and then fold all the remaining differentials
1822 * until the diffs stay at zero. The function is used by NOHZ and can only be
1823 * invoked when tick processing is not active.
1825 void quiet_vmstat(void)
1827 if (system_state != SYSTEM_RUNNING)
1828 return;
1830 if (!delayed_work_pending(this_cpu_ptr(&vmstat_work)))
1831 return;
1833 if (!need_update(smp_processor_id()))
1834 return;
1837 * Just refresh counters and do not care about the pending delayed
1838 * vmstat_update. It doesn't fire that often to matter and canceling
1839 * it would be too expensive from this path.
1840 * vmstat_shepherd will take care about that for us.
1842 refresh_cpu_vm_stats(false);
1846 * Shepherd worker thread that checks the
1847 * differentials of processors that have their worker
1848 * threads for vm statistics updates disabled because of
1849 * inactivity.
1851 static void vmstat_shepherd(struct work_struct *w);
1853 static DECLARE_DEFERRABLE_WORK(shepherd, vmstat_shepherd);
1855 static void vmstat_shepherd(struct work_struct *w)
1857 int cpu;
1859 get_online_cpus();
1860 /* Check processors whose vmstat worker threads have been disabled */
1861 for_each_online_cpu(cpu) {
1862 struct delayed_work *dw = &per_cpu(vmstat_work, cpu);
1864 if (!delayed_work_pending(dw) && need_update(cpu))
1865 queue_delayed_work_on(cpu, mm_percpu_wq, dw, 0);
1867 put_online_cpus();
1869 schedule_delayed_work(&shepherd,
1870 round_jiffies_relative(sysctl_stat_interval));
1873 static void __init start_shepherd_timer(void)
1875 int cpu;
1877 for_each_possible_cpu(cpu)
1878 INIT_DEFERRABLE_WORK(per_cpu_ptr(&vmstat_work, cpu),
1879 vmstat_update);
1881 schedule_delayed_work(&shepherd,
1882 round_jiffies_relative(sysctl_stat_interval));
1885 static void __init init_cpu_node_state(void)
1887 int node;
1889 for_each_online_node(node) {
1890 if (cpumask_weight(cpumask_of_node(node)) > 0)
1891 node_set_state(node, N_CPU);
1895 static int vmstat_cpu_online(unsigned int cpu)
1897 refresh_zone_stat_thresholds();
1898 node_set_state(cpu_to_node(cpu), N_CPU);
1899 return 0;
1902 static int vmstat_cpu_down_prep(unsigned int cpu)
1904 cancel_delayed_work_sync(&per_cpu(vmstat_work, cpu));
1905 return 0;
1908 static int vmstat_cpu_dead(unsigned int cpu)
1910 const struct cpumask *node_cpus;
1911 int node;
1913 node = cpu_to_node(cpu);
1915 refresh_zone_stat_thresholds();
1916 node_cpus = cpumask_of_node(node);
1917 if (cpumask_weight(node_cpus) > 0)
1918 return 0;
1920 node_clear_state(node, N_CPU);
1921 return 0;
1924 #endif
1926 struct workqueue_struct *mm_percpu_wq;
1928 void __init init_mm_internals(void)
1930 int ret __maybe_unused;
1932 mm_percpu_wq = alloc_workqueue("mm_percpu_wq", WQ_MEM_RECLAIM, 0);
1934 #ifdef CONFIG_SMP
1935 ret = cpuhp_setup_state_nocalls(CPUHP_MM_VMSTAT_DEAD, "mm/vmstat:dead",
1936 NULL, vmstat_cpu_dead);
1937 if (ret < 0)
1938 pr_err("vmstat: failed to register 'dead' hotplug state\n");
1940 ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "mm/vmstat:online",
1941 vmstat_cpu_online,
1942 vmstat_cpu_down_prep);
1943 if (ret < 0)
1944 pr_err("vmstat: failed to register 'online' hotplug state\n");
1946 get_online_cpus();
1947 init_cpu_node_state();
1948 put_online_cpus();
1950 start_shepherd_timer();
1951 #endif
1952 #ifdef CONFIG_PROC_FS
1953 proc_create("buddyinfo", 0444, NULL, &buddyinfo_file_operations);
1954 proc_create("pagetypeinfo", 0444, NULL, &pagetypeinfo_file_operations);
1955 proc_create("vmstat", 0444, NULL, &vmstat_file_operations);
1956 proc_create("zoneinfo", 0444, NULL, &zoneinfo_file_operations);
1957 #endif
1960 #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_COMPACTION)
1963 * Return an index indicating how much of the available free memory is
1964 * unusable for an allocation of the requested size.
1966 static int unusable_free_index(unsigned int order,
1967 struct contig_page_info *info)
1969 /* No free memory is interpreted as all free memory is unusable */
1970 if (info->free_pages == 0)
1971 return 1000;
1974 * Index should be a value between 0 and 1. Return a value to 3
1975 * decimal places.
1977 * 0 => no fragmentation
1978 * 1 => high fragmentation
1980 return div_u64((info->free_pages - (info->free_blocks_suitable << order)) * 1000ULL, info->free_pages);
1984 static void unusable_show_print(struct seq_file *m,
1985 pg_data_t *pgdat, struct zone *zone)
1987 unsigned int order;
1988 int index;
1989 struct contig_page_info info;
1991 seq_printf(m, "Node %d, zone %8s ",
1992 pgdat->node_id,
1993 zone->name);
1994 for (order = 0; order < MAX_ORDER; ++order) {
1995 fill_contig_page_info(zone, order, &info);
1996 index = unusable_free_index(order, &info);
1997 seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
2000 seq_putc(m, '\n');
2004 * Display unusable free space index
2006 * The unusable free space index measures how much of the available free
2007 * memory cannot be used to satisfy an allocation of a given size and is a
2008 * value between 0 and 1. The higher the value, the more of free memory is
2009 * unusable and by implication, the worse the external fragmentation is. This
2010 * can be expressed as a percentage by multiplying by 100.
2012 static int unusable_show(struct seq_file *m, void *arg)
2014 pg_data_t *pgdat = (pg_data_t *)arg;
2016 /* check memoryless node */
2017 if (!node_state(pgdat->node_id, N_MEMORY))
2018 return 0;
2020 walk_zones_in_node(m, pgdat, true, false, unusable_show_print);
2022 return 0;
2025 static const struct seq_operations unusable_op = {
2026 .start = frag_start,
2027 .next = frag_next,
2028 .stop = frag_stop,
2029 .show = unusable_show,
2032 static int unusable_open(struct inode *inode, struct file *file)
2034 return seq_open(file, &unusable_op);
2037 static const struct file_operations unusable_file_ops = {
2038 .open = unusable_open,
2039 .read = seq_read,
2040 .llseek = seq_lseek,
2041 .release = seq_release,
2044 static void extfrag_show_print(struct seq_file *m,
2045 pg_data_t *pgdat, struct zone *zone)
2047 unsigned int order;
2048 int index;
2050 /* Alloc on stack as interrupts are disabled for zone walk */
2051 struct contig_page_info info;
2053 seq_printf(m, "Node %d, zone %8s ",
2054 pgdat->node_id,
2055 zone->name);
2056 for (order = 0; order < MAX_ORDER; ++order) {
2057 fill_contig_page_info(zone, order, &info);
2058 index = __fragmentation_index(order, &info);
2059 seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
2062 seq_putc(m, '\n');
2066 * Display fragmentation index for orders that allocations would fail for
2068 static int extfrag_show(struct seq_file *m, void *arg)
2070 pg_data_t *pgdat = (pg_data_t *)arg;
2072 walk_zones_in_node(m, pgdat, true, false, extfrag_show_print);
2074 return 0;
2077 static const struct seq_operations extfrag_op = {
2078 .start = frag_start,
2079 .next = frag_next,
2080 .stop = frag_stop,
2081 .show = extfrag_show,
2084 static int extfrag_open(struct inode *inode, struct file *file)
2086 return seq_open(file, &extfrag_op);
2089 static const struct file_operations extfrag_file_ops = {
2090 .open = extfrag_open,
2091 .read = seq_read,
2092 .llseek = seq_lseek,
2093 .release = seq_release,
2096 static int __init extfrag_debug_init(void)
2098 struct dentry *extfrag_debug_root;
2100 extfrag_debug_root = debugfs_create_dir("extfrag", NULL);
2101 if (!extfrag_debug_root)
2102 return -ENOMEM;
2104 if (!debugfs_create_file("unusable_index", 0444,
2105 extfrag_debug_root, NULL, &unusable_file_ops))
2106 goto fail;
2108 if (!debugfs_create_file("extfrag_index", 0444,
2109 extfrag_debug_root, NULL, &extfrag_file_ops))
2110 goto fail;
2112 return 0;
2113 fail:
2114 debugfs_remove_recursive(extfrag_debug_root);
2115 return -ENOMEM;
2118 module_init(extfrag_debug_init);
2119 #endif