blob: e59d4946093464aac2e9baaf2efc4df08f6e1e19 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/*
2 * linux/mm/vmstat.c
3 *
4 * Manages VM statistics
5 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
6 *
7 * zoned VM statistics
8 * Copyright (C) 2006 Silicon Graphics, Inc.,
9 * Christoph Lameter <christoph@lameter.com>
10 */
11#include <linux/fs.h>
12#include <linux/mm.h>
13#include <linux/err.h>
14#include <linux/module.h>
15#include <linux/slab.h>
16#include <linux/cpu.h>
17#include <linux/vmstat.h>
18#include <linux/sched.h>
19#include <linux/math64.h>
20#include <linux/writeback.h>
21#include <linux/compaction.h>
22
23#ifdef CONFIG_VM_EVENT_COUNTERS
24DEFINE_PER_CPU(struct vm_event_state, vm_event_states) = {{0}};
25EXPORT_PER_CPU_SYMBOL(vm_event_states);
26
27static void sum_vm_events(unsigned long *ret)
28{
29 int cpu;
30 int i;
31
32 memset(ret, 0, NR_VM_EVENT_ITEMS * sizeof(unsigned long));
33
34 for_each_online_cpu(cpu) {
35 struct vm_event_state *this = &per_cpu(vm_event_states, cpu);
36
37 for (i = 0; i < NR_VM_EVENT_ITEMS; i++)
38 ret[i] += this->event[i];
39 }
40}
41
42/*
43 * Accumulate the vm event counters across all CPUs.
44 * The result is unavoidably approximate - it can change
45 * during and after execution of this function.
46*/
47void all_vm_events(unsigned long *ret)
48{
49 get_online_cpus();
50 sum_vm_events(ret);
51 put_online_cpus();
52}
53EXPORT_SYMBOL_GPL(all_vm_events);
54
55#ifdef CONFIG_HOTPLUG
56/*
57 * Fold the foreign cpu events into our own.
58 *
59 * This is adding to the events on one processor
60 * but keeps the global counts constant.
61 */
62void vm_events_fold_cpu(int cpu)
63{
64 struct vm_event_state *fold_state = &per_cpu(vm_event_states, cpu);
65 int i;
66
67 for (i = 0; i < NR_VM_EVENT_ITEMS; i++) {
68 count_vm_events(i, fold_state->event[i]);
69 fold_state->event[i] = 0;
70 }
71}
72#endif /* CONFIG_HOTPLUG */
73
74#endif /* CONFIG_VM_EVENT_COUNTERS */
75
76/*
77 * Manage combined zone based / global counters
78 *
79 * vm_stat contains the global counters
80 */
81atomic_long_t vm_stat[NR_VM_ZONE_STAT_ITEMS] __cacheline_aligned_in_smp;
82EXPORT_SYMBOL(vm_stat);
83
84#ifdef CONFIG_SMP
85
86int calculate_pressure_threshold(struct zone *zone)
87{
88 int threshold;
89 int watermark_distance;
90
91 /*
92 * As vmstats are not up to date, there is drift between the estimated
93 * and real values. For high thresholds and a high number of CPUs, it
94 * is possible for the min watermark to be breached while the estimated
95 * value looks fine. The pressure threshold is a reduced value such
96 * that even the maximum amount of drift will not accidentally breach
97 * the min watermark
98 */
99 watermark_distance = low_wmark_pages(zone) - min_wmark_pages(zone);
100 threshold = max(1, (int)(watermark_distance / num_online_cpus()));
101
102 /*
103 * Maximum threshold is 125
104 */
105 threshold = min(125, threshold);
106
107 return threshold;
108}
109
110int calculate_normal_threshold(struct zone *zone)
111{
112 int threshold;
113 int mem; /* memory in 128 MB units */
114
115 /*
116 * The threshold scales with the number of processors and the amount
117 * of memory per zone. More memory means that we can defer updates for
118 * longer, more processors could lead to more contention.
119 * fls() is used to have a cheap way of logarithmic scaling.
120 *
121 * Some sample thresholds:
122 *
123 * Threshold Processors (fls) Zonesize fls(mem+1)
124 * ------------------------------------------------------------------
125 * 8 1 1 0.9-1 GB 4
126 * 16 2 2 0.9-1 GB 4
127 * 20 2 2 1-2 GB 5
128 * 24 2 2 2-4 GB 6
129 * 28 2 2 4-8 GB 7
130 * 32 2 2 8-16 GB 8
131 * 4 2 2 <128M 1
132 * 30 4 3 2-4 GB 5
133 * 48 4 3 8-16 GB 8
134 * 32 8 4 1-2 GB 4
135 * 32 8 4 0.9-1GB 4
136 * 10 16 5 <128M 1
137 * 40 16 5 900M 4
138 * 70 64 7 2-4 GB 5
139 * 84 64 7 4-8 GB 6
140 * 108 512 9 4-8 GB 6
141 * 125 1024 10 8-16 GB 8
142 * 125 1024 10 16-32 GB 9
143 */
144
145 mem = zone->present_pages >> (27 - PAGE_SHIFT);
146
147 threshold = 2 * fls(num_online_cpus()) * (1 + fls(mem));
148
149 /*
150 * Maximum threshold is 125
151 */
152 threshold = min(125, threshold);
153
154 return threshold;
155}
156
157/*
158 * Refresh the thresholds for each zone.
159 */
160void refresh_zone_stat_thresholds(void)
161{
162 struct zone *zone;
163 int cpu;
164 int threshold;
165
166 for_each_populated_zone(zone) {
167 unsigned long max_drift, tolerate_drift;
168
169 threshold = calculate_normal_threshold(zone);
170
171 for_each_online_cpu(cpu)
172 per_cpu_ptr(zone->pageset, cpu)->stat_threshold
173 = threshold;
174
175 /*
176 * Only set percpu_drift_mark if there is a danger that
177 * NR_FREE_PAGES reports the low watermark is ok when in fact
178 * the min watermark could be breached by an allocation
179 */
180 tolerate_drift = low_wmark_pages(zone) - min_wmark_pages(zone);
181 max_drift = num_online_cpus() * threshold;
182 if (max_drift > tolerate_drift)
183 zone->percpu_drift_mark = high_wmark_pages(zone) +
184 max_drift;
185 }
186}
187
188void set_pgdat_percpu_threshold(pg_data_t *pgdat,
189 int (*calculate_pressure)(struct zone *))
190{
191 struct zone *zone;
192 int cpu;
193 int threshold;
194 int i;
195
196 for (i = 0; i < pgdat->nr_zones; i++) {
197 zone = &pgdat->node_zones[i];
198 if (!zone->percpu_drift_mark)
199 continue;
200
201 threshold = (*calculate_pressure)(zone);
202 for_each_possible_cpu(cpu)
203 per_cpu_ptr(zone->pageset, cpu)->stat_threshold
204 = threshold;
205 }
206}
207
208/*
209 * For use when we know that interrupts are disabled.
210 */
211void __mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
212 int delta)
213{
214 struct per_cpu_pageset __percpu *pcp = zone->pageset;
215 s8 __percpu *p = pcp->vm_stat_diff + item;
216 long x;
217 long t;
218
219 preempt_disable_rt();
220 x = delta + __this_cpu_read(*p);
221
222 t = __this_cpu_read(pcp->stat_threshold);
223
224 if (unlikely(x > t || x < -t)) {
225 zone_page_state_add(x, zone, item);
226 x = 0;
227 }
228 __this_cpu_write(*p, x);
229 preempt_enable_rt();
230}
231EXPORT_SYMBOL(__mod_zone_page_state);
232
233/*
234 * Optimized increment and decrement functions.
235 *
236 * These are only for a single page and therefore can take a struct page *
237 * argument instead of struct zone *. This allows the inclusion of the code
238 * generated for page_zone(page) into the optimized functions.
239 *
240 * No overflow check is necessary and therefore the differential can be
241 * incremented or decremented in place which may allow the compilers to
242 * generate better code.
243 * The increment or decrement is known and therefore one boundary check can
244 * be omitted.
245 *
246 * NOTE: These functions are very performance sensitive. Change only
247 * with care.
248 *
249 * Some processors have inc/dec instructions that are atomic vs an interrupt.
250 * However, the code must first determine the differential location in a zone
251 * based on the processor number and then inc/dec the counter. There is no
252 * guarantee without disabling preemption that the processor will not change
253 * in between and therefore the atomicity vs. interrupt cannot be exploited
254 * in a useful way here.
255 */
256void __inc_zone_state(struct zone *zone, enum zone_stat_item item)
257{
258 struct per_cpu_pageset __percpu *pcp = zone->pageset;
259 s8 __percpu *p = pcp->vm_stat_diff + item;
260 s8 v, t;
261
262 preempt_disable_rt();
263 v = __this_cpu_inc_return(*p);
264 t = __this_cpu_read(pcp->stat_threshold);
265 if (unlikely(v > t)) {
266 s8 overstep = t >> 1;
267
268 zone_page_state_add(v + overstep, zone, item);
269 __this_cpu_write(*p, -overstep);
270 }
271 preempt_enable_rt();
272}
273
274void __inc_zone_page_state(struct page *page, enum zone_stat_item item)
275{
276 __inc_zone_state(page_zone(page), item);
277}
278EXPORT_SYMBOL(__inc_zone_page_state);
279
280void __dec_zone_state(struct zone *zone, enum zone_stat_item item)
281{
282 struct per_cpu_pageset __percpu *pcp = zone->pageset;
283 s8 __percpu *p = pcp->vm_stat_diff + item;
284 s8 v, t;
285
286 preempt_disable_rt();
287 v = __this_cpu_dec_return(*p);
288 t = __this_cpu_read(pcp->stat_threshold);
289 if (unlikely(v < - t)) {
290 s8 overstep = t >> 1;
291
292 zone_page_state_add(v - overstep, zone, item);
293 __this_cpu_write(*p, overstep);
294 }
295 preempt_enable_rt();
296}
297
298void __dec_zone_page_state(struct page *page, enum zone_stat_item item)
299{
300 __dec_zone_state(page_zone(page), item);
301}
302EXPORT_SYMBOL(__dec_zone_page_state);
303
304#ifdef CONFIG_HAVE_CMPXCHG_LOCAL
305/*
306 * If we have cmpxchg_local support then we do not need to incur the overhead
307 * that comes with local_irq_save/restore if we use this_cpu_cmpxchg.
308 *
309 * mod_state() modifies the zone counter state through atomic per cpu
310 * operations.
311 *
312 * Overstep mode specifies how overstep should handled:
313 * 0 No overstepping
314 * 1 Overstepping half of threshold
315 * -1 Overstepping minus half of threshold
316*/
317static inline void mod_state(struct zone *zone,
318 enum zone_stat_item item, int delta, int overstep_mode)
319{
320 struct per_cpu_pageset __percpu *pcp = zone->pageset;
321 s8 __percpu *p = pcp->vm_stat_diff + item;
322 long o, n, t, z;
323
324 do {
325 z = 0; /* overflow to zone counters */
326
327 /*
328 * The fetching of the stat_threshold is racy. We may apply
329 * a counter threshold to the wrong the cpu if we get
330 * rescheduled while executing here. However, the next
331 * counter update will apply the threshold again and
332 * therefore bring the counter under the threshold again.
333 *
334 * Most of the time the thresholds are the same anyways
335 * for all cpus in a zone.
336 */
337 t = this_cpu_read(pcp->stat_threshold);
338
339 o = this_cpu_read(*p);
340 n = delta + o;
341
342 if (n > t || n < -t) {
343 int os = overstep_mode * (t >> 1) ;
344
345 /* Overflow must be added to zone counters */
346 z = n + os;
347 n = -os;
348 }
349 } while (this_cpu_cmpxchg(*p, o, n) != o);
350
351 if (z)
352 zone_page_state_add(z, zone, item);
353}
354
355void mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
356 int delta)
357{
358 mod_state(zone, item, delta, 0);
359}
360EXPORT_SYMBOL(mod_zone_page_state);
361
362void inc_zone_state(struct zone *zone, enum zone_stat_item item)
363{
364 mod_state(zone, item, 1, 1);
365}
366
367void inc_zone_page_state(struct page *page, enum zone_stat_item item)
368{
369 mod_state(page_zone(page), item, 1, 1);
370}
371EXPORT_SYMBOL(inc_zone_page_state);
372
373void dec_zone_page_state(struct page *page, enum zone_stat_item item)
374{
375 mod_state(page_zone(page), item, -1, -1);
376}
377EXPORT_SYMBOL(dec_zone_page_state);
378#else
379/*
380 * Use interrupt disable to serialize counter updates
381 */
382void mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
383 int delta)
384{
385 unsigned long flags;
386
387 local_irq_save(flags);
388 __mod_zone_page_state(zone, item, delta);
389 local_irq_restore(flags);
390}
391EXPORT_SYMBOL(mod_zone_page_state);
392
393void inc_zone_state(struct zone *zone, enum zone_stat_item item)
394{
395 unsigned long flags;
396
397 local_irq_save(flags);
398 __inc_zone_state(zone, item);
399 local_irq_restore(flags);
400}
401
402void inc_zone_page_state(struct page *page, enum zone_stat_item item)
403{
404 unsigned long flags;
405 struct zone *zone;
406
407 zone = page_zone(page);
408 local_irq_save(flags);
409 __inc_zone_state(zone, item);
410 local_irq_restore(flags);
411}
412EXPORT_SYMBOL(inc_zone_page_state);
413
414void dec_zone_page_state(struct page *page, enum zone_stat_item item)
415{
416 unsigned long flags;
417
418 local_irq_save(flags);
419 __dec_zone_page_state(page, item);
420 local_irq_restore(flags);
421}
422EXPORT_SYMBOL(dec_zone_page_state);
423#endif
424
425/*
426 * Update the zone counters for one cpu.
427 *
428 * The cpu specified must be either the current cpu or a processor that
429 * is not online. If it is the current cpu then the execution thread must
430 * be pinned to the current cpu.
431 *
432 * Note that refresh_cpu_vm_stats strives to only access
433 * node local memory. The per cpu pagesets on remote zones are placed
434 * in the memory local to the processor using that pageset. So the
435 * loop over all zones will access a series of cachelines local to
436 * the processor.
437 *
438 * The call to zone_page_state_add updates the cachelines with the
439 * statistics in the remote zone struct as well as the global cachelines
440 * with the global counters. These could cause remote node cache line
441 * bouncing and will have to be only done when necessary.
442 */
443void refresh_cpu_vm_stats(int cpu)
444{
445 struct zone *zone;
446 int i;
447 int global_diff[NR_VM_ZONE_STAT_ITEMS] = { 0, };
448
449 for_each_populated_zone(zone) {
450 struct per_cpu_pageset *p;
451
452 p = per_cpu_ptr(zone->pageset, cpu);
453
454 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
455 if (p->vm_stat_diff[i]) {
456 unsigned long flags;
457 int v;
458
459 local_irq_save(flags);
460 v = p->vm_stat_diff[i];
461 p->vm_stat_diff[i] = 0;
462 local_irq_restore(flags);
463 atomic_long_add(v, &zone->vm_stat[i]);
464 global_diff[i] += v;
465#ifdef CONFIG_NUMA
466 /* 3 seconds idle till flush */
467 p->expire = 3;
468#endif
469 }
470 cond_resched();
471#ifdef CONFIG_NUMA
472 /*
473 * Deal with draining the remote pageset of this
474 * processor
475 *
476 * Check if there are pages remaining in this pageset
477 * if not then there is nothing to expire.
478 */
479 if (!p->expire || !p->pcp.count)
480 continue;
481
482 /*
483 * We never drain zones local to this processor.
484 */
485 if (zone_to_nid(zone) == numa_node_id()) {
486 p->expire = 0;
487 continue;
488 }
489
490 p->expire--;
491 if (p->expire)
492 continue;
493
494 if (p->pcp.count)
495 drain_zone_pages(zone, &p->pcp);
496#endif
497 }
498
499 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
500 if (global_diff[i])
501 atomic_long_add(global_diff[i], &vm_stat[i]);
502}
503
504#endif
505
506#ifdef CONFIG_NUMA
507/*
508 * zonelist = the list of zones passed to the allocator
509 * z = the zone from which the allocation occurred.
510 *
511 * Must be called with interrupts disabled.
512 *
513 * When __GFP_OTHER_NODE is set assume the node of the preferred
514 * zone is the local node. This is useful for daemons who allocate
515 * memory on behalf of other processes.
516 */
517void zone_statistics(struct zone *preferred_zone, struct zone *z, gfp_t flags)
518{
519 if (z->zone_pgdat == preferred_zone->zone_pgdat) {
520 __inc_zone_state(z, NUMA_HIT);
521 } else {
522 __inc_zone_state(z, NUMA_MISS);
523 __inc_zone_state(preferred_zone, NUMA_FOREIGN);
524 }
525 if (z->node == ((flags & __GFP_OTHER_NODE) ?
526 preferred_zone->node : numa_node_id()))
527 __inc_zone_state(z, NUMA_LOCAL);
528 else
529 __inc_zone_state(z, NUMA_OTHER);
530}
531#endif
532
533#ifdef CONFIG_COMPACTION
534
535struct contig_page_info {
536 unsigned long free_pages;
537 unsigned long free_blocks_total;
538 unsigned long free_blocks_suitable;
539};
540
541/*
542 * Calculate the number of free pages in a zone, how many contiguous
543 * pages are free and how many are large enough to satisfy an allocation of
544 * the target size. Note that this function makes no attempt to estimate
545 * how many suitable free blocks there *might* be if MOVABLE pages were
546 * migrated. Calculating that is possible, but expensive and can be
547 * figured out from userspace
548 */
549static void fill_contig_page_info(struct zone *zone,
550 unsigned int suitable_order,
551 struct contig_page_info *info)
552{
553 unsigned int order;
554
555 info->free_pages = 0;
556 info->free_blocks_total = 0;
557 info->free_blocks_suitable = 0;
558
559 for (order = 0; order < MAX_ORDER; order++) {
560 unsigned long blocks;
561
562 /* Count number of free blocks */
563 blocks = zone->free_area[order].nr_free;
564 info->free_blocks_total += blocks;
565
566 /* Count free base pages */
567 info->free_pages += blocks << order;
568
569 /* Count the suitable free blocks */
570 if (order >= suitable_order)
571 info->free_blocks_suitable += blocks <<
572 (order - suitable_order);
573 }
574}
575
576/*
577 * A fragmentation index only makes sense if an allocation of a requested
578 * size would fail. If that is true, the fragmentation index indicates
579 * whether external fragmentation or a lack of memory was the problem.
580 * The value can be used to determine if page reclaim or compaction
581 * should be used
582 */
583static int __fragmentation_index(unsigned int order, struct contig_page_info *info)
584{
585 unsigned long requested = 1UL << order;
586
587 if (!info->free_blocks_total)
588 return 0;
589
590 /* Fragmentation index only makes sense when a request would fail */
591 if (info->free_blocks_suitable)
592 return -1000;
593
594 /*
595 * Index is between 0 and 1 so return within 3 decimal places
596 *
597 * 0 => allocation would fail due to lack of memory
598 * 1 => allocation would fail due to fragmentation
599 */
600 return 1000 - div_u64( (1000+(div_u64(info->free_pages * 1000ULL, requested))), info->free_blocks_total);
601}
602
603/* Same as __fragmentation index but allocs contig_page_info on stack */
604int fragmentation_index(struct zone *zone, unsigned int order)
605{
606 struct contig_page_info info;
607
608 fill_contig_page_info(zone, order, &info);
609 return __fragmentation_index(order, &info);
610}
611#endif
612
613#if defined(CONFIG_PROC_FS) || defined(CONFIG_COMPACTION)
614#include <linux/proc_fs.h>
615#include <linux/seq_file.h>
616
617static char * const migratetype_names[MIGRATE_TYPES] = {
618 "Unmovable",
619 "Reclaimable",
620 "Movable",
621 "Reserve",
622 "Isolate",
623};
624
625static void *frag_start(struct seq_file *m, loff_t *pos)
626{
627 pg_data_t *pgdat;
628 loff_t node = *pos;
629 for (pgdat = first_online_pgdat();
630 pgdat && node;
631 pgdat = next_online_pgdat(pgdat))
632 --node;
633
634 return pgdat;
635}
636
637static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
638{
639 pg_data_t *pgdat = (pg_data_t *)arg;
640
641 (*pos)++;
642 return next_online_pgdat(pgdat);
643}
644
645static void frag_stop(struct seq_file *m, void *arg)
646{
647}
648
649/* Walk all the zones in a node and print using a callback */
650static void walk_zones_in_node(struct seq_file *m, pg_data_t *pgdat,
651 void (*print)(struct seq_file *m, pg_data_t *, struct zone *))
652{
653 struct zone *zone;
654 struct zone *node_zones = pgdat->node_zones;
655 unsigned long flags;
656
657 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
658 if (!populated_zone(zone))
659 continue;
660
661 spin_lock_irqsave(&zone->lock, flags);
662 print(m, pgdat, zone);
663 spin_unlock_irqrestore(&zone->lock, flags);
664 }
665}
666#endif
667
668#if defined(CONFIG_PROC_FS) || defined(CONFIG_SYSFS) || defined(CONFIG_NUMA)
669#ifdef CONFIG_ZONE_DMA
670#define TEXT_FOR_DMA(xx) xx "_dma",
671#else
672#define TEXT_FOR_DMA(xx)
673#endif
674
675#ifdef CONFIG_ZONE_DMA32
676#define TEXT_FOR_DMA32(xx) xx "_dma32",
677#else
678#define TEXT_FOR_DMA32(xx)
679#endif
680
681#ifdef CONFIG_HIGHMEM
682#define TEXT_FOR_HIGHMEM(xx) xx "_high",
683#else
684#define TEXT_FOR_HIGHMEM(xx)
685#endif
686
687#define TEXTS_FOR_ZONES(xx) TEXT_FOR_DMA(xx) TEXT_FOR_DMA32(xx) xx "_normal", \
688 TEXT_FOR_HIGHMEM(xx) xx "_movable",
689
690const char * const vmstat_text[] = {
691 /* Zoned VM counters */
692 "nr_free_pages",
693 "nr_inactive_anon",
694 "nr_active_anon",
695 "nr_inactive_file",
696 "nr_active_file",
697 "nr_unevictable",
698 "nr_mlock",
699 "nr_anon_pages",
700 "nr_mapped",
701 "nr_file_pages",
702 "nr_dirty",
703 "nr_writeback",
704 "nr_slab_reclaimable",
705 "nr_slab_unreclaimable",
706 "nr_page_table_pages",
707 "nr_kernel_stack",
708 "nr_unstable",
709 "nr_bounce",
710 "nr_vmscan_write",
711 "nr_vmscan_immediate_reclaim",
712 "nr_writeback_temp",
713 "nr_isolated_anon",
714 "nr_isolated_file",
715 "nr_shmem",
716 "nr_dirtied",
717 "nr_written",
718
719#ifdef CONFIG_NUMA
720 "numa_hit",
721 "numa_miss",
722 "numa_foreign",
723 "numa_interleave",
724 "numa_local",
725 "numa_other",
726#endif
727 "nr_anon_transparent_hugepages",
728 "nr_dirty_threshold",
729 "nr_dirty_background_threshold",
730
731#ifdef CONFIG_VM_EVENT_COUNTERS
732 "pgpgin",
733 "pgpgout",
734 "pswpin",
735 "pswpout",
736
737 TEXTS_FOR_ZONES("pgalloc")
738
739 "pgfree",
740 "pgactivate",
741 "pgdeactivate",
742
743 "pgfault",
744 "pgmajfault",
745
746 TEXTS_FOR_ZONES("pgrefill")
747 TEXTS_FOR_ZONES("pgsteal_kswapd")
748 TEXTS_FOR_ZONES("pgsteal_direct")
749 TEXTS_FOR_ZONES("pgscan_kswapd")
750 TEXTS_FOR_ZONES("pgscan_direct")
751
752#ifdef CONFIG_NUMA
753 "zone_reclaim_failed",
754#endif
755 "pginodesteal",
756 "slabs_scanned",
757 "kswapd_inodesteal",
758 "kswapd_low_wmark_hit_quickly",
759 "kswapd_high_wmark_hit_quickly",
760 "kswapd_skip_congestion_wait",
761 "pageoutrun",
762 "allocstall",
763
764 "pgrotated",
765
766#ifdef CONFIG_COMPACTION
767 "compact_blocks_moved",
768 "compact_pages_moved",
769 "compact_pagemigrate_failed",
770 "compact_stall",
771 "compact_fail",
772 "compact_success",
773#endif
774
775#ifdef CONFIG_HUGETLB_PAGE
776 "htlb_buddy_alloc_success",
777 "htlb_buddy_alloc_fail",
778#endif
779 "unevictable_pgs_culled",
780 "unevictable_pgs_scanned",
781 "unevictable_pgs_rescued",
782 "unevictable_pgs_mlocked",
783 "unevictable_pgs_munlocked",
784 "unevictable_pgs_cleared",
785 "unevictable_pgs_stranded",
786 "unevictable_pgs_mlockfreed",
787
788#ifdef CONFIG_TRANSPARENT_HUGEPAGE
789 "thp_fault_alloc",
790 "thp_fault_fallback",
791 "thp_collapse_alloc",
792 "thp_collapse_alloc_failed",
793 "thp_split",
794#endif
795
796#endif /* CONFIG_VM_EVENTS_COUNTERS */
797};
798#endif /* CONFIG_PROC_FS || CONFIG_SYSFS || CONFIG_NUMA */
799
800
801#ifdef CONFIG_PROC_FS
802static void frag_show_print(struct seq_file *m, pg_data_t *pgdat,
803 struct zone *zone)
804{
805 int order;
806
807 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
808 for (order = 0; order < MAX_ORDER; ++order)
809 seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
810 seq_putc(m, '\n');
811}
812
813/*
814 * This walks the free areas for each zone.
815 */
816static int frag_show(struct seq_file *m, void *arg)
817{
818 pg_data_t *pgdat = (pg_data_t *)arg;
819 walk_zones_in_node(m, pgdat, frag_show_print);
820 return 0;
821}
822
823static void pagetypeinfo_showfree_print(struct seq_file *m,
824 pg_data_t *pgdat, struct zone *zone)
825{
826 int order, mtype;
827
828 for (mtype = 0; mtype < MIGRATE_TYPES; mtype++) {
829 seq_printf(m, "Node %4d, zone %8s, type %12s ",
830 pgdat->node_id,
831 zone->name,
832 migratetype_names[mtype]);
833 for (order = 0; order < MAX_ORDER; ++order) {
834 unsigned long freecount = 0;
835 struct free_area *area;
836 struct list_head *curr;
837
838 area = &(zone->free_area[order]);
839
840 list_for_each(curr, &area->free_list[mtype])
841 freecount++;
842 seq_printf(m, "%6lu ", freecount);
843 }
844 seq_putc(m, '\n');
845 }
846}
847
848/* Print out the free pages at each order for each migatetype */
849static int pagetypeinfo_showfree(struct seq_file *m, void *arg)
850{
851 int order;
852 pg_data_t *pgdat = (pg_data_t *)arg;
853
854 /* Print header */
855 seq_printf(m, "%-43s ", "Free pages count per migrate type at order");
856 for (order = 0; order < MAX_ORDER; ++order)
857 seq_printf(m, "%6d ", order);
858 seq_putc(m, '\n');
859
860 walk_zones_in_node(m, pgdat, pagetypeinfo_showfree_print);
861
862 return 0;
863}
864
865static void pagetypeinfo_showblockcount_print(struct seq_file *m,
866 pg_data_t *pgdat, struct zone *zone)
867{
868 int mtype;
869 unsigned long pfn;
870 unsigned long start_pfn = zone->zone_start_pfn;
871 unsigned long end_pfn = start_pfn + zone->spanned_pages;
872 unsigned long count[MIGRATE_TYPES] = { 0, };
873
874 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
875 struct page *page;
876
877 if (!pfn_valid(pfn))
878 continue;
879
880 page = pfn_to_page(pfn);
881
882 /* Watch for unexpected holes punched in the memmap */
883 if (!memmap_valid_within(pfn, page, zone))
884 continue;
885
886 mtype = get_pageblock_migratetype(page);
887
888 if (mtype < MIGRATE_TYPES)
889 count[mtype]++;
890 }
891
892 /* Print counts */
893 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
894 for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
895 seq_printf(m, "%12lu ", count[mtype]);
896 seq_putc(m, '\n');
897}
898
899/* Print out the free pages at each order for each migratetype */
900static int pagetypeinfo_showblockcount(struct seq_file *m, void *arg)
901{
902 int mtype;
903 pg_data_t *pgdat = (pg_data_t *)arg;
904
905 seq_printf(m, "\n%-23s", "Number of blocks type ");
906 for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
907 seq_printf(m, "%12s ", migratetype_names[mtype]);
908 seq_putc(m, '\n');
909 walk_zones_in_node(m, pgdat, pagetypeinfo_showblockcount_print);
910
911 return 0;
912}
913
914/*
915 * This prints out statistics in relation to grouping pages by mobility.
916 * It is expensive to collect so do not constantly read the file.
917 */
918static int pagetypeinfo_show(struct seq_file *m, void *arg)
919{
920 pg_data_t *pgdat = (pg_data_t *)arg;
921
922 /* check memoryless node */
923 if (!node_state(pgdat->node_id, N_HIGH_MEMORY))
924 return 0;
925
926 seq_printf(m, "Page block order: %d\n", pageblock_order);
927 seq_printf(m, "Pages per block: %lu\n", pageblock_nr_pages);
928 seq_putc(m, '\n');
929 pagetypeinfo_showfree(m, pgdat);
930 pagetypeinfo_showblockcount(m, pgdat);
931
932 return 0;
933}
934
935static const struct seq_operations fragmentation_op = {
936 .start = frag_start,
937 .next = frag_next,
938 .stop = frag_stop,
939 .show = frag_show,
940};
941
942static int fragmentation_open(struct inode *inode, struct file *file)
943{
944 return seq_open(file, &fragmentation_op);
945}
946
947static const struct file_operations fragmentation_file_operations = {
948 .open = fragmentation_open,
949 .read = seq_read,
950 .llseek = seq_lseek,
951 .release = seq_release,
952};
953
954static const struct seq_operations pagetypeinfo_op = {
955 .start = frag_start,
956 .next = frag_next,
957 .stop = frag_stop,
958 .show = pagetypeinfo_show,
959};
960
961static int pagetypeinfo_open(struct inode *inode, struct file *file)
962{
963 return seq_open(file, &pagetypeinfo_op);
964}
965
966static const struct file_operations pagetypeinfo_file_ops = {
967 .open = pagetypeinfo_open,
968 .read = seq_read,
969 .llseek = seq_lseek,
970 .release = seq_release,
971};
972
973static void zoneinfo_show_print(struct seq_file *m, pg_data_t *pgdat,
974 struct zone *zone)
975{
976 int i;
977 seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
978 seq_printf(m,
979 "\n pages free %lu"
980 "\n min %lu"
981 "\n low %lu"
982 "\n high %lu"
983 "\n scanned %lu"
984 "\n spanned %lu"
985 "\n present %lu",
986 zone_page_state(zone, NR_FREE_PAGES),
987 min_wmark_pages(zone),
988 low_wmark_pages(zone),
989 high_wmark_pages(zone),
990 zone->pages_scanned,
991 zone->spanned_pages,
992 zone->present_pages);
993
994 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
995 seq_printf(m, "\n %-12s %lu", vmstat_text[i],
996 zone_page_state(zone, i));
997
998 seq_printf(m,
999 "\n protection: (%lu",
1000 zone->lowmem_reserve[0]);
1001 for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
1002 seq_printf(m, ", %lu", zone->lowmem_reserve[i]);
1003 seq_printf(m,
1004 ")"
1005 "\n pagesets");
1006 for_each_online_cpu(i) {
1007 struct per_cpu_pageset *pageset;
1008
1009 pageset = per_cpu_ptr(zone->pageset, i);
1010 seq_printf(m,
1011 "\n cpu: %i"
1012 "\n count: %i"
1013 "\n high: %i"
1014 "\n batch: %i",
1015 i,
1016 pageset->pcp.count,
1017 pageset->pcp.high,
1018 pageset->pcp.batch);
1019#ifdef CONFIG_SMP
1020 seq_printf(m, "\n vm stats threshold: %d",
1021 pageset->stat_threshold);
1022#endif
1023 }
1024 seq_printf(m,
1025 "\n all_unreclaimable: %u"
1026 "\n start_pfn: %lu"
1027 "\n inactive_ratio: %u",
1028 zone->all_unreclaimable,
1029 zone->zone_start_pfn,
1030 zone->inactive_ratio);
1031 seq_putc(m, '\n');
1032}
1033
1034/*
1035 * Output information about zones in @pgdat.
1036 */
1037static int zoneinfo_show(struct seq_file *m, void *arg)
1038{
1039 pg_data_t *pgdat = (pg_data_t *)arg;
1040 walk_zones_in_node(m, pgdat, zoneinfo_show_print);
1041 return 0;
1042}
1043
1044static const struct seq_operations zoneinfo_op = {
1045 .start = frag_start, /* iterate over all zones. The same as in
1046 * fragmentation. */
1047 .next = frag_next,
1048 .stop = frag_stop,
1049 .show = zoneinfo_show,
1050};
1051
1052static int zoneinfo_open(struct inode *inode, struct file *file)
1053{
1054 return seq_open(file, &zoneinfo_op);
1055}
1056
1057static const struct file_operations proc_zoneinfo_file_operations = {
1058 .open = zoneinfo_open,
1059 .read = seq_read,
1060 .llseek = seq_lseek,
1061 .release = seq_release,
1062};
1063
1064enum writeback_stat_item {
1065 NR_DIRTY_THRESHOLD,
1066 NR_DIRTY_BG_THRESHOLD,
1067 NR_VM_WRITEBACK_STAT_ITEMS,
1068};
1069
1070static void *vmstat_start(struct seq_file *m, loff_t *pos)
1071{
1072 unsigned long *v;
1073 int i, stat_items_size;
1074
1075 if (*pos >= ARRAY_SIZE(vmstat_text))
1076 return NULL;
1077 stat_items_size = NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long) +
1078 NR_VM_WRITEBACK_STAT_ITEMS * sizeof(unsigned long);
1079
1080#ifdef CONFIG_VM_EVENT_COUNTERS
1081 stat_items_size += sizeof(struct vm_event_state);
1082#endif
1083
1084 v = kmalloc(stat_items_size, GFP_KERNEL);
1085 m->private = v;
1086 if (!v)
1087 return ERR_PTR(-ENOMEM);
1088 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
1089 v[i] = global_page_state(i);
1090 v += NR_VM_ZONE_STAT_ITEMS;
1091
1092 global_dirty_limits(v + NR_DIRTY_BG_THRESHOLD,
1093 v + NR_DIRTY_THRESHOLD);
1094 v += NR_VM_WRITEBACK_STAT_ITEMS;
1095
1096#ifdef CONFIG_VM_EVENT_COUNTERS
1097 all_vm_events(v);
1098 v[PGPGIN] /= 2; /* sectors -> kbytes */
1099 v[PGPGOUT] /= 2;
1100#endif
1101 return (unsigned long *)m->private + *pos;
1102}
1103
1104static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
1105{
1106 (*pos)++;
1107 if (*pos >= ARRAY_SIZE(vmstat_text))
1108 return NULL;
1109 return (unsigned long *)m->private + *pos;
1110}
1111
1112static int vmstat_show(struct seq_file *m, void *arg)
1113{
1114 unsigned long *l = arg;
1115 unsigned long off = l - (unsigned long *)m->private;
1116
1117 seq_printf(m, "%s %lu\n", vmstat_text[off], *l);
1118 return 0;
1119}
1120
1121static void vmstat_stop(struct seq_file *m, void *arg)
1122{
1123 kfree(m->private);
1124 m->private = NULL;
1125}
1126
1127static const struct seq_operations vmstat_op = {
1128 .start = vmstat_start,
1129 .next = vmstat_next,
1130 .stop = vmstat_stop,
1131 .show = vmstat_show,
1132};
1133
1134static int vmstat_open(struct inode *inode, struct file *file)
1135{
1136 return seq_open(file, &vmstat_op);
1137}
1138
1139static const struct file_operations proc_vmstat_file_operations = {
1140 .open = vmstat_open,
1141 .read = seq_read,
1142 .llseek = seq_lseek,
1143 .release = seq_release,
1144};
1145#endif /* CONFIG_PROC_FS */
1146
1147#ifdef CONFIG_SMP
1148static DEFINE_PER_CPU(struct delayed_work, vmstat_work);
1149int sysctl_stat_interval __read_mostly = HZ;
1150
1151static void vmstat_update(struct work_struct *w)
1152{
1153 refresh_cpu_vm_stats(smp_processor_id());
1154 schedule_delayed_work(&__get_cpu_var(vmstat_work),
1155 round_jiffies_relative(sysctl_stat_interval));
1156}
1157
1158static void __cpuinit start_cpu_timer(int cpu)
1159{
1160 struct delayed_work *work = &per_cpu(vmstat_work, cpu);
1161
1162 INIT_DELAYED_WORK_DEFERRABLE(work, vmstat_update);
1163 schedule_delayed_work_on(cpu, work, __round_jiffies_relative(HZ, cpu));
1164}
1165
1166/*
1167 * Use the cpu notifier to insure that the thresholds are recalculated
1168 * when necessary.
1169 */
1170static int __cpuinit vmstat_cpuup_callback(struct notifier_block *nfb,
1171 unsigned long action,
1172 void *hcpu)
1173{
1174 long cpu = (long)hcpu;
1175
1176 switch (action) {
1177 case CPU_ONLINE:
1178 case CPU_ONLINE_FROZEN:
1179 refresh_zone_stat_thresholds();
1180 start_cpu_timer(cpu);
1181 node_set_state(cpu_to_node(cpu), N_CPU);
1182 break;
1183 case CPU_DOWN_PREPARE:
1184 case CPU_DOWN_PREPARE_FROZEN:
1185 cancel_delayed_work_sync(&per_cpu(vmstat_work, cpu));
1186 per_cpu(vmstat_work, cpu).work.func = NULL;
1187 break;
1188 case CPU_DOWN_FAILED:
1189 case CPU_DOWN_FAILED_FROZEN:
1190 start_cpu_timer(cpu);
1191 break;
1192 case CPU_DEAD:
1193 case CPU_DEAD_FROZEN:
1194 refresh_zone_stat_thresholds();
1195 break;
1196 default:
1197 break;
1198 }
1199 return NOTIFY_OK;
1200}
1201
1202static struct notifier_block __cpuinitdata vmstat_notifier =
1203 { &vmstat_cpuup_callback, NULL, 0 };
1204#endif
1205
1206static int __init setup_vmstat(void)
1207{
1208#ifdef CONFIG_SMP
1209 int cpu;
1210
1211 register_cpu_notifier(&vmstat_notifier);
1212
1213 for_each_online_cpu(cpu)
1214 start_cpu_timer(cpu);
1215#endif
1216#ifdef CONFIG_PROC_FS
1217 if (!IS_ENABLED(CONFIG_PROC_STRIPPED)) {
1218 proc_create("buddyinfo", S_IRUGO, NULL, &fragmentation_file_operations);
1219 proc_create("pagetypeinfo", S_IRUGO, NULL, &pagetypeinfo_file_ops);
1220 proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
1221 }
1222 proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations);
1223#endif
1224 return 0;
1225}
1226module_init(setup_vmstat)
1227
1228#if defined(CONFIG_DEBUG_FS) && defined(CONFIG_COMPACTION)
1229#include <linux/debugfs.h>
1230
1231static struct dentry *extfrag_debug_root;
1232
1233/*
1234 * Return an index indicating how much of the available free memory is
1235 * unusable for an allocation of the requested size.
1236 */
1237static int unusable_free_index(unsigned int order,
1238 struct contig_page_info *info)
1239{
1240 /* No free memory is interpreted as all free memory is unusable */
1241 if (info->free_pages == 0)
1242 return 1000;
1243
1244 /*
1245 * Index should be a value between 0 and 1. Return a value to 3
1246 * decimal places.
1247 *
1248 * 0 => no fragmentation
1249 * 1 => high fragmentation
1250 */
1251 return div_u64((info->free_pages - (info->free_blocks_suitable << order)) * 1000ULL, info->free_pages);
1252
1253}
1254
1255static void unusable_show_print(struct seq_file *m,
1256 pg_data_t *pgdat, struct zone *zone)
1257{
1258 unsigned int order;
1259 int index;
1260 struct contig_page_info info;
1261
1262 seq_printf(m, "Node %d, zone %8s ",
1263 pgdat->node_id,
1264 zone->name);
1265 for (order = 0; order < MAX_ORDER; ++order) {
1266 fill_contig_page_info(zone, order, &info);
1267 index = unusable_free_index(order, &info);
1268 seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
1269 }
1270
1271 seq_putc(m, '\n');
1272}
1273
1274/*
1275 * Display unusable free space index
1276 *
1277 * The unusable free space index measures how much of the available free
1278 * memory cannot be used to satisfy an allocation of a given size and is a
1279 * value between 0 and 1. The higher the value, the more of free memory is
1280 * unusable and by implication, the worse the external fragmentation is. This
1281 * can be expressed as a percentage by multiplying by 100.
1282 */
1283static int unusable_show(struct seq_file *m, void *arg)
1284{
1285 pg_data_t *pgdat = (pg_data_t *)arg;
1286
1287 /* check memoryless node */
1288 if (!node_state(pgdat->node_id, N_HIGH_MEMORY))
1289 return 0;
1290
1291 walk_zones_in_node(m, pgdat, unusable_show_print);
1292
1293 return 0;
1294}
1295
1296static const struct seq_operations unusable_op = {
1297 .start = frag_start,
1298 .next = frag_next,
1299 .stop = frag_stop,
1300 .show = unusable_show,
1301};
1302
1303static int unusable_open(struct inode *inode, struct file *file)
1304{
1305 return seq_open(file, &unusable_op);
1306}
1307
1308static const struct file_operations unusable_file_ops = {
1309 .open = unusable_open,
1310 .read = seq_read,
1311 .llseek = seq_lseek,
1312 .release = seq_release,
1313};
1314
1315static void extfrag_show_print(struct seq_file *m,
1316 pg_data_t *pgdat, struct zone *zone)
1317{
1318 unsigned int order;
1319 int index;
1320
1321 /* Alloc on stack as interrupts are disabled for zone walk */
1322 struct contig_page_info info;
1323
1324 seq_printf(m, "Node %d, zone %8s ",
1325 pgdat->node_id,
1326 zone->name);
1327 for (order = 0; order < MAX_ORDER; ++order) {
1328 fill_contig_page_info(zone, order, &info);
1329 index = __fragmentation_index(order, &info);
1330 seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
1331 }
1332
1333 seq_putc(m, '\n');
1334}
1335
1336/*
1337 * Display fragmentation index for orders that allocations would fail for
1338 */
1339static int extfrag_show(struct seq_file *m, void *arg)
1340{
1341 pg_data_t *pgdat = (pg_data_t *)arg;
1342
1343 walk_zones_in_node(m, pgdat, extfrag_show_print);
1344
1345 return 0;
1346}
1347
1348static const struct seq_operations extfrag_op = {
1349 .start = frag_start,
1350 .next = frag_next,
1351 .stop = frag_stop,
1352 .show = extfrag_show,
1353};
1354
1355static int extfrag_open(struct inode *inode, struct file *file)
1356{
1357 return seq_open(file, &extfrag_op);
1358}
1359
1360static const struct file_operations extfrag_file_ops = {
1361 .open = extfrag_open,
1362 .read = seq_read,
1363 .llseek = seq_lseek,
1364 .release = seq_release,
1365};
1366
1367static int __init extfrag_debug_init(void)
1368{
1369 extfrag_debug_root = debugfs_create_dir("extfrag", NULL);
1370 if (!extfrag_debug_root)
1371 return -ENOMEM;
1372
1373 if (!debugfs_create_file("unusable_index", 0444,
1374 extfrag_debug_root, NULL, &unusable_file_ops))
1375 return -ENOMEM;
1376
1377 if (!debugfs_create_file("extfrag_index", 0444,
1378 extfrag_debug_root, NULL, &extfrag_file_ops))
1379 return -ENOMEM;
1380
1381 return 0;
1382}
1383
1384module_init(extfrag_debug_init);
1385#endif