blob: daea02833e2eed3b38e36f81fa59ef62a3731cf5 [file] [log] [blame]
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001/*
2 * linux/mm/vmstat.c
3 *
4 * Manages VM statistics
5 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
Christoph Lameter2244b952006-06-30 01:55:33 -07006 *
7 * zoned VM statistics
8 * Copyright (C) 2006 Silicon Graphics, Inc.,
9 * Christoph Lameter <christoph@lameter.com>
Christoph Lameter7cc36bb2014-10-09 15:29:43 -070010 * Copyright (C) 2008-2014 Christoph Lameter
Christoph Lameterf6ac2352006-06-30 01:55:32 -070011 */
Alexey Dobriyan8f32f7e2008-10-06 04:13:52 +040012#include <linux/fs.h>
Christoph Lameterf6ac2352006-06-30 01:55:32 -070013#include <linux/mm.h>
Alexey Dobriyan4e950f62007-07-30 02:36:13 +040014#include <linux/err.h>
Christoph Lameter2244b952006-06-30 01:55:33 -070015#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Christoph Lameterdf9ecab2006-08-31 21:27:35 -070017#include <linux/cpu.h>
Christoph Lameter7cc36bb2014-10-09 15:29:43 -070018#include <linux/cpumask.h>
Adrian Bunkc748e132008-07-23 21:27:03 -070019#include <linux/vmstat.h>
Andrew Morton3c486872015-02-10 14:09:43 -080020#include <linux/proc_fs.h>
21#include <linux/seq_file.h>
22#include <linux/debugfs.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040023#include <linux/sched.h>
Mel Gormanf1a5ab12010-05-24 14:32:26 -070024#include <linux/math64.h>
Michael Rubin79da8262010-10-26 14:21:36 -070025#include <linux/writeback.h>
Namhyung Kim36deb0b2010-10-26 14:22:04 -070026#include <linux/compaction.h>
Lisa Du6e543d52013-09-11 14:22:36 -070027#include <linux/mm_inline.h>
Joonsoo Kim48c96a32014-12-12 16:56:01 -080028#include <linux/page_ext.h>
29#include <linux/page_owner.h>
Lisa Du6e543d52013-09-11 14:22:36 -070030
31#include "internal.h"
Christoph Lameterf6ac2352006-06-30 01:55:32 -070032
Christoph Lameterf8891e52006-06-30 01:55:45 -070033#ifdef CONFIG_VM_EVENT_COUNTERS
34DEFINE_PER_CPU(struct vm_event_state, vm_event_states) = {{0}};
35EXPORT_PER_CPU_SYMBOL(vm_event_states);
36
Minchan Kim31f961a2010-08-09 17:18:59 -070037static void sum_vm_events(unsigned long *ret)
Christoph Lameterf8891e52006-06-30 01:55:45 -070038{
Christoph Lameter9eccf2a2008-02-04 22:29:22 -080039 int cpu;
Christoph Lameterf8891e52006-06-30 01:55:45 -070040 int i;
41
42 memset(ret, 0, NR_VM_EVENT_ITEMS * sizeof(unsigned long));
43
Minchan Kim31f961a2010-08-09 17:18:59 -070044 for_each_online_cpu(cpu) {
Christoph Lameterf8891e52006-06-30 01:55:45 -070045 struct vm_event_state *this = &per_cpu(vm_event_states, cpu);
46
Christoph Lameterf8891e52006-06-30 01:55:45 -070047 for (i = 0; i < NR_VM_EVENT_ITEMS; i++)
48 ret[i] += this->event[i];
49 }
50}
51
52/*
53 * Accumulate the vm event counters across all CPUs.
54 * The result is unavoidably approximate - it can change
55 * during and after execution of this function.
56*/
57void all_vm_events(unsigned long *ret)
58{
KOSAKI Motohirob5be1132008-05-12 14:02:06 -070059 get_online_cpus();
Minchan Kim31f961a2010-08-09 17:18:59 -070060 sum_vm_events(ret);
KOSAKI Motohirob5be1132008-05-12 14:02:06 -070061 put_online_cpus();
Christoph Lameterf8891e52006-06-30 01:55:45 -070062}
Heiko Carstens32dd66f2006-07-10 04:44:31 -070063EXPORT_SYMBOL_GPL(all_vm_events);
Christoph Lameterf8891e52006-06-30 01:55:45 -070064
Christoph Lameterf8891e52006-06-30 01:55:45 -070065/*
66 * Fold the foreign cpu events into our own.
67 *
68 * This is adding to the events on one processor
69 * but keeps the global counts constant.
70 */
71void vm_events_fold_cpu(int cpu)
72{
73 struct vm_event_state *fold_state = &per_cpu(vm_event_states, cpu);
74 int i;
75
76 for (i = 0; i < NR_VM_EVENT_ITEMS; i++) {
77 count_vm_events(i, fold_state->event[i]);
78 fold_state->event[i] = 0;
79 }
80}
Christoph Lameterf8891e52006-06-30 01:55:45 -070081
82#endif /* CONFIG_VM_EVENT_COUNTERS */
83
Christoph Lameter2244b952006-06-30 01:55:33 -070084/*
85 * Manage combined zone based / global counters
86 *
87 * vm_stat contains the global counters
88 */
Mel Gorman75ef7182016-07-28 15:45:24 -070089atomic_long_t vm_zone_stat[NR_VM_ZONE_STAT_ITEMS] __cacheline_aligned_in_smp;
Kemi Wang3a321d22017-09-08 16:12:48 -070090atomic_long_t vm_numa_stat[NR_VM_NUMA_STAT_ITEMS] __cacheline_aligned_in_smp;
Mel Gorman75ef7182016-07-28 15:45:24 -070091atomic_long_t vm_node_stat[NR_VM_NODE_STAT_ITEMS] __cacheline_aligned_in_smp;
92EXPORT_SYMBOL(vm_zone_stat);
Kemi Wang3a321d22017-09-08 16:12:48 -070093EXPORT_SYMBOL(vm_numa_stat);
Mel Gorman75ef7182016-07-28 15:45:24 -070094EXPORT_SYMBOL(vm_node_stat);
Christoph Lameter2244b952006-06-30 01:55:33 -070095
96#ifdef CONFIG_SMP
97
Mel Gormanb44129b2011-01-13 15:45:43 -080098int calculate_pressure_threshold(struct zone *zone)
Mel Gorman88f5acf2011-01-13 15:45:41 -080099{
100 int threshold;
101 int watermark_distance;
102
103 /*
104 * As vmstats are not up to date, there is drift between the estimated
105 * and real values. For high thresholds and a high number of CPUs, it
106 * is possible for the min watermark to be breached while the estimated
107 * value looks fine. The pressure threshold is a reduced value such
108 * that even the maximum amount of drift will not accidentally breach
109 * the min watermark
110 */
111 watermark_distance = low_wmark_pages(zone) - min_wmark_pages(zone);
112 threshold = max(1, (int)(watermark_distance / num_online_cpus()));
113
114 /*
115 * Maximum threshold is 125
116 */
117 threshold = min(125, threshold);
118
119 return threshold;
120}
121
Mel Gormanb44129b2011-01-13 15:45:43 -0800122int calculate_normal_threshold(struct zone *zone)
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700123{
124 int threshold;
125 int mem; /* memory in 128 MB units */
126
127 /*
128 * The threshold scales with the number of processors and the amount
129 * of memory per zone. More memory means that we can defer updates for
130 * longer, more processors could lead to more contention.
131 * fls() is used to have a cheap way of logarithmic scaling.
132 *
133 * Some sample thresholds:
134 *
135 * Threshold Processors (fls) Zonesize fls(mem+1)
136 * ------------------------------------------------------------------
137 * 8 1 1 0.9-1 GB 4
138 * 16 2 2 0.9-1 GB 4
139 * 20 2 2 1-2 GB 5
140 * 24 2 2 2-4 GB 6
141 * 28 2 2 4-8 GB 7
142 * 32 2 2 8-16 GB 8
143 * 4 2 2 <128M 1
144 * 30 4 3 2-4 GB 5
145 * 48 4 3 8-16 GB 8
146 * 32 8 4 1-2 GB 4
147 * 32 8 4 0.9-1GB 4
148 * 10 16 5 <128M 1
149 * 40 16 5 900M 4
150 * 70 64 7 2-4 GB 5
151 * 84 64 7 4-8 GB 6
152 * 108 512 9 4-8 GB 6
153 * 125 1024 10 8-16 GB 8
154 * 125 1024 10 16-32 GB 9
155 */
156
Jiang Liub40da042013-02-22 16:33:52 -0800157 mem = zone->managed_pages >> (27 - PAGE_SHIFT);
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700158
159 threshold = 2 * fls(num_online_cpus()) * (1 + fls(mem));
160
161 /*
162 * Maximum threshold is 125
163 */
164 threshold = min(125, threshold);
165
166 return threshold;
167}
Christoph Lameter2244b952006-06-30 01:55:33 -0700168
169/*
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700170 * Refresh the thresholds for each zone.
Christoph Lameter2244b952006-06-30 01:55:33 -0700171 */
KOSAKI Motohiroa6cccdc2011-05-24 17:11:33 -0700172void refresh_zone_stat_thresholds(void)
Christoph Lameter2244b952006-06-30 01:55:33 -0700173{
Mel Gorman75ef7182016-07-28 15:45:24 -0700174 struct pglist_data *pgdat;
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700175 struct zone *zone;
176 int cpu;
177 int threshold;
178
Mel Gorman75ef7182016-07-28 15:45:24 -0700179 /* Zero current pgdat thresholds */
180 for_each_online_pgdat(pgdat) {
181 for_each_online_cpu(cpu) {
182 per_cpu_ptr(pgdat->per_cpu_nodestats, cpu)->stat_threshold = 0;
183 }
184 }
185
KOSAKI Motohiroee99c712009-03-31 15:19:31 -0700186 for_each_populated_zone(zone) {
Mel Gorman75ef7182016-07-28 15:45:24 -0700187 struct pglist_data *pgdat = zone->zone_pgdat;
Christoph Lameteraa454842010-09-09 16:38:17 -0700188 unsigned long max_drift, tolerate_drift;
189
Mel Gormanb44129b2011-01-13 15:45:43 -0800190 threshold = calculate_normal_threshold(zone);
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700191
Mel Gorman75ef7182016-07-28 15:45:24 -0700192 for_each_online_cpu(cpu) {
193 int pgdat_threshold;
194
Christoph Lameter99dcc3e2010-01-05 15:34:51 +0900195 per_cpu_ptr(zone->pageset, cpu)->stat_threshold
196 = threshold;
Kemi Wang3a321d22017-09-08 16:12:48 -0700197#ifdef CONFIG_NUMA
198 per_cpu_ptr(zone->pageset, cpu)->numa_stat_threshold
199 = threshold;
200#endif
Mel Gorman75ef7182016-07-28 15:45:24 -0700201 /* Base nodestat threshold on the largest populated zone. */
202 pgdat_threshold = per_cpu_ptr(pgdat->per_cpu_nodestats, cpu)->stat_threshold;
203 per_cpu_ptr(pgdat->per_cpu_nodestats, cpu)->stat_threshold
204 = max(threshold, pgdat_threshold);
205 }
206
Christoph Lameteraa454842010-09-09 16:38:17 -0700207 /*
208 * Only set percpu_drift_mark if there is a danger that
209 * NR_FREE_PAGES reports the low watermark is ok when in fact
210 * the min watermark could be breached by an allocation
211 */
212 tolerate_drift = low_wmark_pages(zone) - min_wmark_pages(zone);
213 max_drift = num_online_cpus() * threshold;
214 if (max_drift > tolerate_drift)
215 zone->percpu_drift_mark = high_wmark_pages(zone) +
216 max_drift;
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700217 }
Christoph Lameter2244b952006-06-30 01:55:33 -0700218}
219
Mel Gormanb44129b2011-01-13 15:45:43 -0800220void set_pgdat_percpu_threshold(pg_data_t *pgdat,
221 int (*calculate_pressure)(struct zone *))
Mel Gorman88f5acf2011-01-13 15:45:41 -0800222{
223 struct zone *zone;
224 int cpu;
225 int threshold;
226 int i;
227
Mel Gorman88f5acf2011-01-13 15:45:41 -0800228 for (i = 0; i < pgdat->nr_zones; i++) {
229 zone = &pgdat->node_zones[i];
230 if (!zone->percpu_drift_mark)
231 continue;
232
Mel Gormanb44129b2011-01-13 15:45:43 -0800233 threshold = (*calculate_pressure)(zone);
Kemi Wang3a321d22017-09-08 16:12:48 -0700234 for_each_online_cpu(cpu) {
Mel Gorman88f5acf2011-01-13 15:45:41 -0800235 per_cpu_ptr(zone->pageset, cpu)->stat_threshold
236 = threshold;
Kemi Wang3a321d22017-09-08 16:12:48 -0700237#ifdef CONFIG_NUMA
238 per_cpu_ptr(zone->pageset, cpu)->numa_stat_threshold
239 = threshold;
240#endif
241 }
Mel Gorman88f5acf2011-01-13 15:45:41 -0800242 }
Mel Gorman88f5acf2011-01-13 15:45:41 -0800243}
244
Christoph Lameter2244b952006-06-30 01:55:33 -0700245/*
Jianyu Zhanbea04b02014-06-04 16:09:51 -0700246 * For use when we know that interrupts are disabled,
247 * or when we know that preemption is disabled and that
248 * particular counter cannot be updated from interrupt context.
Christoph Lameter2244b952006-06-30 01:55:33 -0700249 */
250void __mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
Heiko Carstens6cdb18a2015-12-29 14:54:32 -0800251 long delta)
Christoph Lameter2244b952006-06-30 01:55:33 -0700252{
Christoph Lameter12938a92010-12-06 11:16:20 -0600253 struct per_cpu_pageset __percpu *pcp = zone->pageset;
254 s8 __percpu *p = pcp->vm_stat_diff + item;
Christoph Lameter2244b952006-06-30 01:55:33 -0700255 long x;
Christoph Lameter12938a92010-12-06 11:16:20 -0600256 long t;
Christoph Lameter2244b952006-06-30 01:55:33 -0700257
Christoph Lameter12938a92010-12-06 11:16:20 -0600258 x = delta + __this_cpu_read(*p);
Christoph Lameter2244b952006-06-30 01:55:33 -0700259
Christoph Lameter12938a92010-12-06 11:16:20 -0600260 t = __this_cpu_read(pcp->stat_threshold);
261
262 if (unlikely(x > t || x < -t)) {
Christoph Lameter2244b952006-06-30 01:55:33 -0700263 zone_page_state_add(x, zone, item);
264 x = 0;
265 }
Christoph Lameter12938a92010-12-06 11:16:20 -0600266 __this_cpu_write(*p, x);
Christoph Lameter2244b952006-06-30 01:55:33 -0700267}
268EXPORT_SYMBOL(__mod_zone_page_state);
269
Mel Gorman75ef7182016-07-28 15:45:24 -0700270void __mod_node_page_state(struct pglist_data *pgdat, enum node_stat_item item,
271 long delta)
272{
273 struct per_cpu_nodestat __percpu *pcp = pgdat->per_cpu_nodestats;
274 s8 __percpu *p = pcp->vm_node_stat_diff + item;
275 long x;
276 long t;
277
278 x = delta + __this_cpu_read(*p);
279
280 t = __this_cpu_read(pcp->stat_threshold);
281
282 if (unlikely(x > t || x < -t)) {
283 node_page_state_add(x, pgdat, item);
284 x = 0;
285 }
286 __this_cpu_write(*p, x);
287}
288EXPORT_SYMBOL(__mod_node_page_state);
289
Christoph Lameter2244b952006-06-30 01:55:33 -0700290/*
Christoph Lameter2244b952006-06-30 01:55:33 -0700291 * Optimized increment and decrement functions.
292 *
293 * These are only for a single page and therefore can take a struct page *
294 * argument instead of struct zone *. This allows the inclusion of the code
295 * generated for page_zone(page) into the optimized functions.
296 *
297 * No overflow check is necessary and therefore the differential can be
298 * incremented or decremented in place which may allow the compilers to
299 * generate better code.
Christoph Lameter2244b952006-06-30 01:55:33 -0700300 * The increment or decrement is known and therefore one boundary check can
301 * be omitted.
302 *
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700303 * NOTE: These functions are very performance sensitive. Change only
304 * with care.
305 *
Christoph Lameter2244b952006-06-30 01:55:33 -0700306 * Some processors have inc/dec instructions that are atomic vs an interrupt.
307 * However, the code must first determine the differential location in a zone
308 * based on the processor number and then inc/dec the counter. There is no
309 * guarantee without disabling preemption that the processor will not change
310 * in between and therefore the atomicity vs. interrupt cannot be exploited
311 * in a useful way here.
312 */
Christoph Lameterc8785382007-02-10 01:43:01 -0800313void __inc_zone_state(struct zone *zone, enum zone_stat_item item)
Christoph Lameter2244b952006-06-30 01:55:33 -0700314{
Christoph Lameter12938a92010-12-06 11:16:20 -0600315 struct per_cpu_pageset __percpu *pcp = zone->pageset;
316 s8 __percpu *p = pcp->vm_stat_diff + item;
317 s8 v, t;
Christoph Lameter2244b952006-06-30 01:55:33 -0700318
Christoph Lameter908ee0f2010-12-06 11:40:02 -0600319 v = __this_cpu_inc_return(*p);
Christoph Lameter12938a92010-12-06 11:16:20 -0600320 t = __this_cpu_read(pcp->stat_threshold);
321 if (unlikely(v > t)) {
322 s8 overstep = t >> 1;
Christoph Lameter2244b952006-06-30 01:55:33 -0700323
Christoph Lameter12938a92010-12-06 11:16:20 -0600324 zone_page_state_add(v + overstep, zone, item);
325 __this_cpu_write(*p, -overstep);
Christoph Lameter2244b952006-06-30 01:55:33 -0700326 }
327}
Christoph Lameterca889e62006-06-30 01:55:44 -0700328
Mel Gorman75ef7182016-07-28 15:45:24 -0700329void __inc_node_state(struct pglist_data *pgdat, enum node_stat_item item)
330{
331 struct per_cpu_nodestat __percpu *pcp = pgdat->per_cpu_nodestats;
332 s8 __percpu *p = pcp->vm_node_stat_diff + item;
333 s8 v, t;
334
335 v = __this_cpu_inc_return(*p);
336 t = __this_cpu_read(pcp->stat_threshold);
337 if (unlikely(v > t)) {
338 s8 overstep = t >> 1;
339
340 node_page_state_add(v + overstep, pgdat, item);
341 __this_cpu_write(*p, -overstep);
342 }
343}
344
Christoph Lameterca889e62006-06-30 01:55:44 -0700345void __inc_zone_page_state(struct page *page, enum zone_stat_item item)
346{
347 __inc_zone_state(page_zone(page), item);
348}
Christoph Lameter2244b952006-06-30 01:55:33 -0700349EXPORT_SYMBOL(__inc_zone_page_state);
350
Mel Gorman75ef7182016-07-28 15:45:24 -0700351void __inc_node_page_state(struct page *page, enum node_stat_item item)
352{
353 __inc_node_state(page_pgdat(page), item);
354}
355EXPORT_SYMBOL(__inc_node_page_state);
356
Christoph Lameterc8785382007-02-10 01:43:01 -0800357void __dec_zone_state(struct zone *zone, enum zone_stat_item item)
Christoph Lameter2244b952006-06-30 01:55:33 -0700358{
Christoph Lameter12938a92010-12-06 11:16:20 -0600359 struct per_cpu_pageset __percpu *pcp = zone->pageset;
360 s8 __percpu *p = pcp->vm_stat_diff + item;
361 s8 v, t;
Christoph Lameter2244b952006-06-30 01:55:33 -0700362
Christoph Lameter908ee0f2010-12-06 11:40:02 -0600363 v = __this_cpu_dec_return(*p);
Christoph Lameter12938a92010-12-06 11:16:20 -0600364 t = __this_cpu_read(pcp->stat_threshold);
365 if (unlikely(v < - t)) {
366 s8 overstep = t >> 1;
Christoph Lameter2244b952006-06-30 01:55:33 -0700367
Christoph Lameter12938a92010-12-06 11:16:20 -0600368 zone_page_state_add(v - overstep, zone, item);
369 __this_cpu_write(*p, overstep);
Christoph Lameter2244b952006-06-30 01:55:33 -0700370 }
371}
Christoph Lameterc8785382007-02-10 01:43:01 -0800372
Mel Gorman75ef7182016-07-28 15:45:24 -0700373void __dec_node_state(struct pglist_data *pgdat, enum node_stat_item item)
374{
375 struct per_cpu_nodestat __percpu *pcp = pgdat->per_cpu_nodestats;
376 s8 __percpu *p = pcp->vm_node_stat_diff + item;
377 s8 v, t;
378
379 v = __this_cpu_dec_return(*p);
380 t = __this_cpu_read(pcp->stat_threshold);
381 if (unlikely(v < - t)) {
382 s8 overstep = t >> 1;
383
384 node_page_state_add(v - overstep, pgdat, item);
385 __this_cpu_write(*p, overstep);
386 }
387}
388
Christoph Lameterc8785382007-02-10 01:43:01 -0800389void __dec_zone_page_state(struct page *page, enum zone_stat_item item)
390{
391 __dec_zone_state(page_zone(page), item);
392}
Christoph Lameter2244b952006-06-30 01:55:33 -0700393EXPORT_SYMBOL(__dec_zone_page_state);
394
Mel Gorman75ef7182016-07-28 15:45:24 -0700395void __dec_node_page_state(struct page *page, enum node_stat_item item)
396{
397 __dec_node_state(page_pgdat(page), item);
398}
399EXPORT_SYMBOL(__dec_node_page_state);
400
Heiko Carstens41561532012-01-12 17:17:30 -0800401#ifdef CONFIG_HAVE_CMPXCHG_LOCAL
Christoph Lameter7c839122010-12-14 10:28:46 -0600402/*
403 * If we have cmpxchg_local support then we do not need to incur the overhead
404 * that comes with local_irq_save/restore if we use this_cpu_cmpxchg.
405 *
406 * mod_state() modifies the zone counter state through atomic per cpu
407 * operations.
408 *
409 * Overstep mode specifies how overstep should handled:
410 * 0 No overstepping
411 * 1 Overstepping half of threshold
412 * -1 Overstepping minus half of threshold
413*/
Mel Gorman75ef7182016-07-28 15:45:24 -0700414static inline void mod_zone_state(struct zone *zone,
415 enum zone_stat_item item, long delta, int overstep_mode)
Christoph Lameter7c839122010-12-14 10:28:46 -0600416{
417 struct per_cpu_pageset __percpu *pcp = zone->pageset;
418 s8 __percpu *p = pcp->vm_stat_diff + item;
419 long o, n, t, z;
420
421 do {
422 z = 0; /* overflow to zone counters */
423
424 /*
425 * The fetching of the stat_threshold is racy. We may apply
426 * a counter threshold to the wrong the cpu if we get
Christoph Lameterd3bc2362011-04-14 15:21:58 -0700427 * rescheduled while executing here. However, the next
428 * counter update will apply the threshold again and
429 * therefore bring the counter under the threshold again.
430 *
431 * Most of the time the thresholds are the same anyways
432 * for all cpus in a zone.
Christoph Lameter7c839122010-12-14 10:28:46 -0600433 */
434 t = this_cpu_read(pcp->stat_threshold);
435
436 o = this_cpu_read(*p);
437 n = delta + o;
438
439 if (n > t || n < -t) {
440 int os = overstep_mode * (t >> 1) ;
441
442 /* Overflow must be added to zone counters */
443 z = n + os;
444 n = -os;
445 }
446 } while (this_cpu_cmpxchg(*p, o, n) != o);
447
448 if (z)
449 zone_page_state_add(z, zone, item);
450}
451
452void mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
Heiko Carstens6cdb18a2015-12-29 14:54:32 -0800453 long delta)
Christoph Lameter7c839122010-12-14 10:28:46 -0600454{
Mel Gorman75ef7182016-07-28 15:45:24 -0700455 mod_zone_state(zone, item, delta, 0);
Christoph Lameter7c839122010-12-14 10:28:46 -0600456}
457EXPORT_SYMBOL(mod_zone_page_state);
458
Christoph Lameter7c839122010-12-14 10:28:46 -0600459void inc_zone_page_state(struct page *page, enum zone_stat_item item)
460{
Mel Gorman75ef7182016-07-28 15:45:24 -0700461 mod_zone_state(page_zone(page), item, 1, 1);
Christoph Lameter7c839122010-12-14 10:28:46 -0600462}
463EXPORT_SYMBOL(inc_zone_page_state);
464
465void dec_zone_page_state(struct page *page, enum zone_stat_item item)
466{
Mel Gorman75ef7182016-07-28 15:45:24 -0700467 mod_zone_state(page_zone(page), item, -1, -1);
Christoph Lameter7c839122010-12-14 10:28:46 -0600468}
469EXPORT_SYMBOL(dec_zone_page_state);
Mel Gorman75ef7182016-07-28 15:45:24 -0700470
471static inline void mod_node_state(struct pglist_data *pgdat,
472 enum node_stat_item item, int delta, int overstep_mode)
473{
474 struct per_cpu_nodestat __percpu *pcp = pgdat->per_cpu_nodestats;
475 s8 __percpu *p = pcp->vm_node_stat_diff + item;
476 long o, n, t, z;
477
478 do {
479 z = 0; /* overflow to node counters */
480
481 /*
482 * The fetching of the stat_threshold is racy. We may apply
483 * a counter threshold to the wrong the cpu if we get
484 * rescheduled while executing here. However, the next
485 * counter update will apply the threshold again and
486 * therefore bring the counter under the threshold again.
487 *
488 * Most of the time the thresholds are the same anyways
489 * for all cpus in a node.
490 */
491 t = this_cpu_read(pcp->stat_threshold);
492
493 o = this_cpu_read(*p);
494 n = delta + o;
495
496 if (n > t || n < -t) {
497 int os = overstep_mode * (t >> 1) ;
498
499 /* Overflow must be added to node counters */
500 z = n + os;
501 n = -os;
502 }
503 } while (this_cpu_cmpxchg(*p, o, n) != o);
504
505 if (z)
506 node_page_state_add(z, pgdat, item);
507}
508
509void mod_node_page_state(struct pglist_data *pgdat, enum node_stat_item item,
510 long delta)
511{
512 mod_node_state(pgdat, item, delta, 0);
513}
514EXPORT_SYMBOL(mod_node_page_state);
515
516void inc_node_state(struct pglist_data *pgdat, enum node_stat_item item)
517{
518 mod_node_state(pgdat, item, 1, 1);
519}
520
521void inc_node_page_state(struct page *page, enum node_stat_item item)
522{
523 mod_node_state(page_pgdat(page), item, 1, 1);
524}
525EXPORT_SYMBOL(inc_node_page_state);
526
527void dec_node_page_state(struct page *page, enum node_stat_item item)
528{
529 mod_node_state(page_pgdat(page), item, -1, -1);
530}
531EXPORT_SYMBOL(dec_node_page_state);
Christoph Lameter7c839122010-12-14 10:28:46 -0600532#else
533/*
534 * Use interrupt disable to serialize counter updates
535 */
536void mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
Heiko Carstens6cdb18a2015-12-29 14:54:32 -0800537 long delta)
Christoph Lameter7c839122010-12-14 10:28:46 -0600538{
539 unsigned long flags;
540
541 local_irq_save(flags);
542 __mod_zone_page_state(zone, item, delta);
543 local_irq_restore(flags);
544}
545EXPORT_SYMBOL(mod_zone_page_state);
546
Christoph Lameter2244b952006-06-30 01:55:33 -0700547void inc_zone_page_state(struct page *page, enum zone_stat_item item)
548{
549 unsigned long flags;
550 struct zone *zone;
Christoph Lameter2244b952006-06-30 01:55:33 -0700551
552 zone = page_zone(page);
553 local_irq_save(flags);
Christoph Lameterca889e62006-06-30 01:55:44 -0700554 __inc_zone_state(zone, item);
Christoph Lameter2244b952006-06-30 01:55:33 -0700555 local_irq_restore(flags);
556}
557EXPORT_SYMBOL(inc_zone_page_state);
558
559void dec_zone_page_state(struct page *page, enum zone_stat_item item)
560{
561 unsigned long flags;
Christoph Lameter2244b952006-06-30 01:55:33 -0700562
Christoph Lameter2244b952006-06-30 01:55:33 -0700563 local_irq_save(flags);
Christoph Lametera302eb42006-08-31 21:27:34 -0700564 __dec_zone_page_state(page, item);
Christoph Lameter2244b952006-06-30 01:55:33 -0700565 local_irq_restore(flags);
566}
567EXPORT_SYMBOL(dec_zone_page_state);
568
Mel Gorman75ef7182016-07-28 15:45:24 -0700569void inc_node_state(struct pglist_data *pgdat, enum node_stat_item item)
570{
571 unsigned long flags;
572
573 local_irq_save(flags);
574 __inc_node_state(pgdat, item);
575 local_irq_restore(flags);
576}
577EXPORT_SYMBOL(inc_node_state);
578
579void mod_node_page_state(struct pglist_data *pgdat, enum node_stat_item item,
580 long delta)
581{
582 unsigned long flags;
583
584 local_irq_save(flags);
585 __mod_node_page_state(pgdat, item, delta);
586 local_irq_restore(flags);
587}
588EXPORT_SYMBOL(mod_node_page_state);
589
590void inc_node_page_state(struct page *page, enum node_stat_item item)
591{
592 unsigned long flags;
593 struct pglist_data *pgdat;
594
595 pgdat = page_pgdat(page);
596 local_irq_save(flags);
597 __inc_node_state(pgdat, item);
598 local_irq_restore(flags);
599}
600EXPORT_SYMBOL(inc_node_page_state);
601
602void dec_node_page_state(struct page *page, enum node_stat_item item)
603{
604 unsigned long flags;
605
606 local_irq_save(flags);
607 __dec_node_page_state(page, item);
608 local_irq_restore(flags);
609}
610EXPORT_SYMBOL(dec_node_page_state);
611#endif
Christoph Lameter7cc36bb2014-10-09 15:29:43 -0700612
613/*
614 * Fold a differential into the global counters.
615 * Returns the number of counters updated.
616 */
Kemi Wang3a321d22017-09-08 16:12:48 -0700617#ifdef CONFIG_NUMA
618static int fold_diff(int *zone_diff, int *numa_diff, int *node_diff)
619{
620 int i;
621 int changes = 0;
622
623 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
624 if (zone_diff[i]) {
625 atomic_long_add(zone_diff[i], &vm_zone_stat[i]);
626 changes++;
627 }
628
629 for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++)
630 if (numa_diff[i]) {
631 atomic_long_add(numa_diff[i], &vm_numa_stat[i]);
632 changes++;
633 }
634
635 for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++)
636 if (node_diff[i]) {
637 atomic_long_add(node_diff[i], &vm_node_stat[i]);
638 changes++;
639 }
640 return changes;
641}
642#else
Mel Gorman75ef7182016-07-28 15:45:24 -0700643static int fold_diff(int *zone_diff, int *node_diff)
Christoph Lameter4edb0742013-09-11 14:21:31 -0700644{
645 int i;
Christoph Lameter7cc36bb2014-10-09 15:29:43 -0700646 int changes = 0;
Christoph Lameter4edb0742013-09-11 14:21:31 -0700647
648 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
Mel Gorman75ef7182016-07-28 15:45:24 -0700649 if (zone_diff[i]) {
650 atomic_long_add(zone_diff[i], &vm_zone_stat[i]);
651 changes++;
652 }
653
654 for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++)
655 if (node_diff[i]) {
656 atomic_long_add(node_diff[i], &vm_node_stat[i]);
Christoph Lameter7cc36bb2014-10-09 15:29:43 -0700657 changes++;
658 }
659 return changes;
Christoph Lameter4edb0742013-09-11 14:21:31 -0700660}
Kemi Wang3a321d22017-09-08 16:12:48 -0700661#endif /* CONFIG_NUMA */
Christoph Lameter4edb0742013-09-11 14:21:31 -0700662
Christoph Lameter2244b952006-06-30 01:55:33 -0700663/*
Christoph Lameter2bb921e2013-09-11 14:21:30 -0700664 * Update the zone counters for the current cpu.
Christoph Lametera7f75e22008-02-04 22:29:16 -0800665 *
Christoph Lameter4037d452007-05-09 02:35:14 -0700666 * Note that refresh_cpu_vm_stats strives to only access
667 * node local memory. The per cpu pagesets on remote zones are placed
668 * in the memory local to the processor using that pageset. So the
669 * loop over all zones will access a series of cachelines local to
670 * the processor.
671 *
672 * The call to zone_page_state_add updates the cachelines with the
673 * statistics in the remote zone struct as well as the global cachelines
674 * with the global counters. These could cause remote node cache line
675 * bouncing and will have to be only done when necessary.
Christoph Lameter7cc36bb2014-10-09 15:29:43 -0700676 *
677 * The function returns the number of global counters updated.
Christoph Lameter2244b952006-06-30 01:55:33 -0700678 */
Christoph Lameter0eb77e92016-01-14 15:21:40 -0800679static int refresh_cpu_vm_stats(bool do_pagesets)
Christoph Lameter2244b952006-06-30 01:55:33 -0700680{
Mel Gorman75ef7182016-07-28 15:45:24 -0700681 struct pglist_data *pgdat;
Christoph Lameter2244b952006-06-30 01:55:33 -0700682 struct zone *zone;
683 int i;
Mel Gorman75ef7182016-07-28 15:45:24 -0700684 int global_zone_diff[NR_VM_ZONE_STAT_ITEMS] = { 0, };
Kemi Wang3a321d22017-09-08 16:12:48 -0700685#ifdef CONFIG_NUMA
686 int global_numa_diff[NR_VM_NUMA_STAT_ITEMS] = { 0, };
687#endif
Mel Gorman75ef7182016-07-28 15:45:24 -0700688 int global_node_diff[NR_VM_NODE_STAT_ITEMS] = { 0, };
Christoph Lameter7cc36bb2014-10-09 15:29:43 -0700689 int changes = 0;
Christoph Lameter2244b952006-06-30 01:55:33 -0700690
KOSAKI Motohiroee99c712009-03-31 15:19:31 -0700691 for_each_populated_zone(zone) {
Christoph Lameterfbc2edb2013-09-11 14:21:32 -0700692 struct per_cpu_pageset __percpu *p = zone->pageset;
Christoph Lameter2244b952006-06-30 01:55:33 -0700693
Christoph Lameterfbc2edb2013-09-11 14:21:32 -0700694 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++) {
695 int v;
Christoph Lameter2244b952006-06-30 01:55:33 -0700696
Christoph Lameterfbc2edb2013-09-11 14:21:32 -0700697 v = this_cpu_xchg(p->vm_stat_diff[i], 0);
698 if (v) {
Christoph Lametera7f75e22008-02-04 22:29:16 -0800699
Christoph Lametera7f75e22008-02-04 22:29:16 -0800700 atomic_long_add(v, &zone->vm_stat[i]);
Mel Gorman75ef7182016-07-28 15:45:24 -0700701 global_zone_diff[i] += v;
Christoph Lameter4037d452007-05-09 02:35:14 -0700702#ifdef CONFIG_NUMA
703 /* 3 seconds idle till flush */
Christoph Lameterfbc2edb2013-09-11 14:21:32 -0700704 __this_cpu_write(p->expire, 3);
Christoph Lameter4037d452007-05-09 02:35:14 -0700705#endif
Christoph Lameter2244b952006-06-30 01:55:33 -0700706 }
Christoph Lameterfbc2edb2013-09-11 14:21:32 -0700707 }
Christoph Lameter4037d452007-05-09 02:35:14 -0700708#ifdef CONFIG_NUMA
Kemi Wang3a321d22017-09-08 16:12:48 -0700709 for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++) {
710 int v;
711
712 v = this_cpu_xchg(p->vm_numa_stat_diff[i], 0);
713 if (v) {
714
715 atomic_long_add(v, &zone->vm_numa_stat[i]);
716 global_numa_diff[i] += v;
717 __this_cpu_write(p->expire, 3);
718 }
719 }
720
Christoph Lameter0eb77e92016-01-14 15:21:40 -0800721 if (do_pagesets) {
722 cond_resched();
723 /*
724 * Deal with draining the remote pageset of this
725 * processor
726 *
727 * Check if there are pages remaining in this pageset
728 * if not then there is nothing to expire.
729 */
730 if (!__this_cpu_read(p->expire) ||
Christoph Lameterfbc2edb2013-09-11 14:21:32 -0700731 !__this_cpu_read(p->pcp.count))
Christoph Lameter0eb77e92016-01-14 15:21:40 -0800732 continue;
Christoph Lameter4037d452007-05-09 02:35:14 -0700733
Christoph Lameter0eb77e92016-01-14 15:21:40 -0800734 /*
735 * We never drain zones local to this processor.
736 */
737 if (zone_to_nid(zone) == numa_node_id()) {
738 __this_cpu_write(p->expire, 0);
739 continue;
740 }
Christoph Lameter4037d452007-05-09 02:35:14 -0700741
Christoph Lameter0eb77e92016-01-14 15:21:40 -0800742 if (__this_cpu_dec_return(p->expire))
743 continue;
Christoph Lameter4037d452007-05-09 02:35:14 -0700744
Christoph Lameter0eb77e92016-01-14 15:21:40 -0800745 if (__this_cpu_read(p->pcp.count)) {
746 drain_zone_pages(zone, this_cpu_ptr(&p->pcp));
747 changes++;
748 }
Christoph Lameter7cc36bb2014-10-09 15:29:43 -0700749 }
Christoph Lameter4037d452007-05-09 02:35:14 -0700750#endif
Christoph Lameter2244b952006-06-30 01:55:33 -0700751 }
Mel Gorman75ef7182016-07-28 15:45:24 -0700752
753 for_each_online_pgdat(pgdat) {
754 struct per_cpu_nodestat __percpu *p = pgdat->per_cpu_nodestats;
755
756 for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++) {
757 int v;
758
759 v = this_cpu_xchg(p->vm_node_stat_diff[i], 0);
760 if (v) {
761 atomic_long_add(v, &pgdat->vm_stat[i]);
762 global_node_diff[i] += v;
763 }
764 }
765 }
766
Kemi Wang3a321d22017-09-08 16:12:48 -0700767#ifdef CONFIG_NUMA
768 changes += fold_diff(global_zone_diff, global_numa_diff,
769 global_node_diff);
770#else
Mel Gorman75ef7182016-07-28 15:45:24 -0700771 changes += fold_diff(global_zone_diff, global_node_diff);
Kemi Wang3a321d22017-09-08 16:12:48 -0700772#endif
Christoph Lameter7cc36bb2014-10-09 15:29:43 -0700773 return changes;
Christoph Lameter2244b952006-06-30 01:55:33 -0700774}
775
Cody P Schafer40f4b1e2013-04-29 15:08:38 -0700776/*
Christoph Lameter2bb921e2013-09-11 14:21:30 -0700777 * Fold the data for an offline cpu into the global array.
778 * There cannot be any access by the offline cpu and therefore
779 * synchronization is simplified.
780 */
781void cpu_vm_stats_fold(int cpu)
782{
Mel Gorman75ef7182016-07-28 15:45:24 -0700783 struct pglist_data *pgdat;
Christoph Lameter2bb921e2013-09-11 14:21:30 -0700784 struct zone *zone;
785 int i;
Mel Gorman75ef7182016-07-28 15:45:24 -0700786 int global_zone_diff[NR_VM_ZONE_STAT_ITEMS] = { 0, };
Kemi Wang3a321d22017-09-08 16:12:48 -0700787#ifdef CONFIG_NUMA
788 int global_numa_diff[NR_VM_NUMA_STAT_ITEMS] = { 0, };
789#endif
Mel Gorman75ef7182016-07-28 15:45:24 -0700790 int global_node_diff[NR_VM_NODE_STAT_ITEMS] = { 0, };
Christoph Lameter2bb921e2013-09-11 14:21:30 -0700791
792 for_each_populated_zone(zone) {
793 struct per_cpu_pageset *p;
794
795 p = per_cpu_ptr(zone->pageset, cpu);
796
797 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
798 if (p->vm_stat_diff[i]) {
799 int v;
800
801 v = p->vm_stat_diff[i];
802 p->vm_stat_diff[i] = 0;
803 atomic_long_add(v, &zone->vm_stat[i]);
Mel Gorman75ef7182016-07-28 15:45:24 -0700804 global_zone_diff[i] += v;
Christoph Lameter2bb921e2013-09-11 14:21:30 -0700805 }
Kemi Wang3a321d22017-09-08 16:12:48 -0700806
807#ifdef CONFIG_NUMA
808 for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++)
809 if (p->vm_numa_stat_diff[i]) {
810 int v;
811
812 v = p->vm_numa_stat_diff[i];
813 p->vm_numa_stat_diff[i] = 0;
814 atomic_long_add(v, &zone->vm_numa_stat[i]);
815 global_numa_diff[i] += v;
816 }
817#endif
Christoph Lameter2bb921e2013-09-11 14:21:30 -0700818 }
819
Mel Gorman75ef7182016-07-28 15:45:24 -0700820 for_each_online_pgdat(pgdat) {
821 struct per_cpu_nodestat *p;
822
823 p = per_cpu_ptr(pgdat->per_cpu_nodestats, cpu);
824
825 for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++)
826 if (p->vm_node_stat_diff[i]) {
827 int v;
828
829 v = p->vm_node_stat_diff[i];
830 p->vm_node_stat_diff[i] = 0;
831 atomic_long_add(v, &pgdat->vm_stat[i]);
832 global_node_diff[i] += v;
833 }
834 }
835
Kemi Wang3a321d22017-09-08 16:12:48 -0700836#ifdef CONFIG_NUMA
837 fold_diff(global_zone_diff, global_numa_diff, global_node_diff);
838#else
Mel Gorman75ef7182016-07-28 15:45:24 -0700839 fold_diff(global_zone_diff, global_node_diff);
Kemi Wang3a321d22017-09-08 16:12:48 -0700840#endif
Christoph Lameter2bb921e2013-09-11 14:21:30 -0700841}
842
843/*
Cody P Schafer40f4b1e2013-04-29 15:08:38 -0700844 * this is only called if !populated_zone(zone), which implies no other users of
845 * pset->vm_stat_diff[] exsist.
846 */
Minchan Kim5a883812012-10-08 16:33:39 -0700847void drain_zonestat(struct zone *zone, struct per_cpu_pageset *pset)
848{
849 int i;
850
851 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
852 if (pset->vm_stat_diff[i]) {
853 int v = pset->vm_stat_diff[i];
854 pset->vm_stat_diff[i] = 0;
855 atomic_long_add(v, &zone->vm_stat[i]);
Mel Gorman75ef7182016-07-28 15:45:24 -0700856 atomic_long_add(v, &vm_zone_stat[i]);
Minchan Kim5a883812012-10-08 16:33:39 -0700857 }
Kemi Wang3a321d22017-09-08 16:12:48 -0700858
859#ifdef CONFIG_NUMA
860 for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++)
861 if (pset->vm_numa_stat_diff[i]) {
862 int v = pset->vm_numa_stat_diff[i];
863
864 pset->vm_numa_stat_diff[i] = 0;
865 atomic_long_add(v, &zone->vm_numa_stat[i]);
866 atomic_long_add(v, &vm_numa_stat[i]);
867 }
868#endif
Minchan Kim5a883812012-10-08 16:33:39 -0700869}
Christoph Lameter2244b952006-06-30 01:55:33 -0700870#endif
871
Christoph Lameterca889e62006-06-30 01:55:44 -0700872#ifdef CONFIG_NUMA
Kemi Wang3a321d22017-09-08 16:12:48 -0700873void __inc_numa_state(struct zone *zone,
874 enum numa_stat_item item)
875{
876 struct per_cpu_pageset __percpu *pcp = zone->pageset;
877 s8 __percpu *p = pcp->vm_numa_stat_diff + item;
878 s8 v, t;
879
880 v = __this_cpu_inc_return(*p);
881 t = __this_cpu_read(pcp->numa_stat_threshold);
882 if (unlikely(v > t)) {
883 s8 overstep = t >> 1;
884
885 zone_numa_state_add(v + overstep, zone, item);
886 __this_cpu_write(*p, -overstep);
887 }
888}
889
Christoph Lameterca889e62006-06-30 01:55:44 -0700890/*
Mel Gorman75ef7182016-07-28 15:45:24 -0700891 * Determine the per node value of a stat item. This function
892 * is called frequently in a NUMA machine, so try to be as
893 * frugal as possible.
Andrew Mortonc2d42c12015-11-05 18:48:43 -0800894 */
Mel Gorman75ef7182016-07-28 15:45:24 -0700895unsigned long sum_zone_node_page_state(int node,
896 enum zone_stat_item item)
Andrew Mortonc2d42c12015-11-05 18:48:43 -0800897{
898 struct zone *zones = NODE_DATA(node)->node_zones;
Joonsoo Kime87d59f2016-05-19 17:12:29 -0700899 int i;
900 unsigned long count = 0;
Andrew Mortonc2d42c12015-11-05 18:48:43 -0800901
Joonsoo Kime87d59f2016-05-19 17:12:29 -0700902 for (i = 0; i < MAX_NR_ZONES; i++)
903 count += zone_page_state(zones + i, item);
904
905 return count;
Andrew Mortonc2d42c12015-11-05 18:48:43 -0800906}
907
Kemi Wang3a321d22017-09-08 16:12:48 -0700908unsigned long sum_zone_numa_state(int node,
909 enum numa_stat_item item)
910{
911 struct zone *zones = NODE_DATA(node)->node_zones;
912 int i;
913 unsigned long count = 0;
914
915 for (i = 0; i < MAX_NR_ZONES; i++)
916 count += zone_numa_state(zones + i, item);
917
918 return count;
919}
920
Mel Gorman75ef7182016-07-28 15:45:24 -0700921/*
922 * Determine the per node value of a stat item.
923 */
924unsigned long node_page_state(struct pglist_data *pgdat,
925 enum node_stat_item item)
926{
927 long x = atomic_long_read(&pgdat->vm_stat[item]);
928#ifdef CONFIG_SMP
929 if (x < 0)
930 x = 0;
931#endif
932 return x;
933}
Christoph Lameterca889e62006-06-30 01:55:44 -0700934#endif
935
Mel Gormand7a57522010-05-24 14:32:25 -0700936#ifdef CONFIG_COMPACTION
Namhyung Kim36deb0b2010-10-26 14:22:04 -0700937
Mel Gormand7a57522010-05-24 14:32:25 -0700938struct contig_page_info {
939 unsigned long free_pages;
940 unsigned long free_blocks_total;
941 unsigned long free_blocks_suitable;
942};
943
944/*
945 * Calculate the number of free pages in a zone, how many contiguous
946 * pages are free and how many are large enough to satisfy an allocation of
947 * the target size. Note that this function makes no attempt to estimate
948 * how many suitable free blocks there *might* be if MOVABLE pages were
949 * migrated. Calculating that is possible, but expensive and can be
950 * figured out from userspace
951 */
952static void fill_contig_page_info(struct zone *zone,
953 unsigned int suitable_order,
954 struct contig_page_info *info)
955{
956 unsigned int order;
957
958 info->free_pages = 0;
959 info->free_blocks_total = 0;
960 info->free_blocks_suitable = 0;
961
962 for (order = 0; order < MAX_ORDER; order++) {
963 unsigned long blocks;
964
965 /* Count number of free blocks */
966 blocks = zone->free_area[order].nr_free;
967 info->free_blocks_total += blocks;
968
969 /* Count free base pages */
970 info->free_pages += blocks << order;
971
972 /* Count the suitable free blocks */
973 if (order >= suitable_order)
974 info->free_blocks_suitable += blocks <<
975 (order - suitable_order);
976 }
977}
Mel Gormanf1a5ab12010-05-24 14:32:26 -0700978
979/*
980 * A fragmentation index only makes sense if an allocation of a requested
981 * size would fail. If that is true, the fragmentation index indicates
982 * whether external fragmentation or a lack of memory was the problem.
983 * The value can be used to determine if page reclaim or compaction
984 * should be used
985 */
Mel Gorman56de7262010-05-24 14:32:30 -0700986static int __fragmentation_index(unsigned int order, struct contig_page_info *info)
Mel Gormanf1a5ab12010-05-24 14:32:26 -0700987{
988 unsigned long requested = 1UL << order;
989
Wen Yang88d6ac42017-09-06 16:24:06 -0700990 if (WARN_ON_ONCE(order >= MAX_ORDER))
991 return 0;
992
Mel Gormanf1a5ab12010-05-24 14:32:26 -0700993 if (!info->free_blocks_total)
994 return 0;
995
996 /* Fragmentation index only makes sense when a request would fail */
997 if (info->free_blocks_suitable)
998 return -1000;
999
1000 /*
1001 * Index is between 0 and 1 so return within 3 decimal places
1002 *
1003 * 0 => allocation would fail due to lack of memory
1004 * 1 => allocation would fail due to fragmentation
1005 */
1006 return 1000 - div_u64( (1000+(div_u64(info->free_pages * 1000ULL, requested))), info->free_blocks_total);
1007}
Mel Gorman56de7262010-05-24 14:32:30 -07001008
1009/* Same as __fragmentation index but allocs contig_page_info on stack */
1010int fragmentation_index(struct zone *zone, unsigned int order)
1011{
1012 struct contig_page_info info;
1013
1014 fill_contig_page_info(zone, order, &info);
1015 return __fragmentation_index(order, &info);
1016}
Mel Gormand7a57522010-05-24 14:32:25 -07001017#endif
1018
David Rientjes0d6617c2011-09-14 16:21:05 -07001019#if defined(CONFIG_PROC_FS) || defined(CONFIG_SYSFS) || defined(CONFIG_NUMA)
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001020#ifdef CONFIG_ZONE_DMA
1021#define TEXT_FOR_DMA(xx) xx "_dma",
1022#else
1023#define TEXT_FOR_DMA(xx)
1024#endif
1025
1026#ifdef CONFIG_ZONE_DMA32
1027#define TEXT_FOR_DMA32(xx) xx "_dma32",
1028#else
1029#define TEXT_FOR_DMA32(xx)
1030#endif
1031
1032#ifdef CONFIG_HIGHMEM
1033#define TEXT_FOR_HIGHMEM(xx) xx "_high",
1034#else
1035#define TEXT_FOR_HIGHMEM(xx)
1036#endif
1037
1038#define TEXTS_FOR_ZONES(xx) TEXT_FOR_DMA(xx) TEXT_FOR_DMA32(xx) xx "_normal", \
1039 TEXT_FOR_HIGHMEM(xx) xx "_movable",
1040
1041const char * const vmstat_text[] = {
Konstantin Khlebnikov09316c02014-10-09 15:29:32 -07001042 /* enum zone_stat_item countes */
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001043 "nr_free_pages",
Minchan Kim71c799f2016-07-28 15:47:26 -07001044 "nr_zone_inactive_anon",
1045 "nr_zone_active_anon",
1046 "nr_zone_inactive_file",
1047 "nr_zone_active_file",
1048 "nr_zone_unevictable",
Mel Gorman5a1c84b2016-07-28 15:47:31 -07001049 "nr_zone_write_pending",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001050 "nr_mlock",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001051 "nr_page_table_pages",
1052 "nr_kernel_stack",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001053 "nr_bounce",
Minchan Kim91537fe2016-07-26 15:24:45 -07001054#if IS_ENABLED(CONFIG_ZSMALLOC)
1055 "nr_zspages",
1056#endif
Kemi Wang3a321d22017-09-08 16:12:48 -07001057 "nr_free_cma",
1058
1059 /* enum numa_stat_item counters */
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001060#ifdef CONFIG_NUMA
1061 "numa_hit",
1062 "numa_miss",
1063 "numa_foreign",
1064 "numa_interleave",
1065 "numa_local",
1066 "numa_other",
1067#endif
Konstantin Khlebnikov09316c02014-10-09 15:29:32 -07001068
Mel Gorman599d0c92016-07-28 15:45:31 -07001069 /* Node-based counters */
1070 "nr_inactive_anon",
1071 "nr_active_anon",
1072 "nr_inactive_file",
1073 "nr_active_file",
1074 "nr_unevictable",
Johannes Weiner385386c2017-07-06 15:40:43 -07001075 "nr_slab_reclaimable",
1076 "nr_slab_unreclaimable",
Mel Gorman599d0c92016-07-28 15:45:31 -07001077 "nr_isolated_anon",
1078 "nr_isolated_file",
Mel Gorman1e6b10852016-07-28 15:46:08 -07001079 "workingset_refault",
1080 "workingset_activate",
1081 "workingset_nodereclaim",
Mel Gorman50658e22016-07-28 15:46:14 -07001082 "nr_anon_pages",
1083 "nr_mapped",
Mel Gorman11fb9982016-07-28 15:46:20 -07001084 "nr_file_pages",
1085 "nr_dirty",
1086 "nr_writeback",
1087 "nr_writeback_temp",
1088 "nr_shmem",
1089 "nr_shmem_hugepages",
1090 "nr_shmem_pmdmapped",
1091 "nr_anon_transparent_hugepages",
1092 "nr_unstable",
Mel Gormanc4a25632016-07-28 15:46:23 -07001093 "nr_vmscan_write",
1094 "nr_vmscan_immediate_reclaim",
1095 "nr_dirtied",
1096 "nr_written",
Mel Gorman599d0c92016-07-28 15:45:31 -07001097
Konstantin Khlebnikov09316c02014-10-09 15:29:32 -07001098 /* enum writeback_stat_item counters */
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001099 "nr_dirty_threshold",
1100 "nr_dirty_background_threshold",
1101
1102#ifdef CONFIG_VM_EVENT_COUNTERS
Konstantin Khlebnikov09316c02014-10-09 15:29:32 -07001103 /* enum vm_event_item counters */
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001104 "pgpgin",
1105 "pgpgout",
1106 "pswpin",
1107 "pswpout",
1108
1109 TEXTS_FOR_ZONES("pgalloc")
Mel Gorman7cc30fc2016-07-28 15:46:59 -07001110 TEXTS_FOR_ZONES("allocstall")
1111 TEXTS_FOR_ZONES("pgskip")
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001112
1113 "pgfree",
1114 "pgactivate",
1115 "pgdeactivate",
Shaohua Lif7ad2a62017-05-03 14:52:29 -07001116 "pglazyfree",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001117
1118 "pgfault",
1119 "pgmajfault",
Minchan Kim854e9ed2016-01-15 16:54:53 -08001120 "pglazyfreed",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001121
Mel Gorman599d0c92016-07-28 15:45:31 -07001122 "pgrefill",
1123 "pgsteal_kswapd",
1124 "pgsteal_direct",
1125 "pgscan_kswapd",
1126 "pgscan_direct",
Mel Gorman68243e72012-07-31 16:44:39 -07001127 "pgscan_direct_throttle",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001128
1129#ifdef CONFIG_NUMA
1130 "zone_reclaim_failed",
1131#endif
1132 "pginodesteal",
1133 "slabs_scanned",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001134 "kswapd_inodesteal",
1135 "kswapd_low_wmark_hit_quickly",
1136 "kswapd_high_wmark_hit_quickly",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001137 "pageoutrun",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001138
1139 "pgrotated",
1140
Dave Hansen5509a5d2014-04-03 14:48:19 -07001141 "drop_pagecache",
1142 "drop_slab",
Konstantin Khlebnikov8e675f72017-07-06 15:40:28 -07001143 "oom_kill",
Dave Hansen5509a5d2014-04-03 14:48:19 -07001144
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001145#ifdef CONFIG_NUMA_BALANCING
1146 "numa_pte_updates",
Mel Gorman72403b42013-11-12 15:08:32 -08001147 "numa_huge_pte_updates",
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001148 "numa_hint_faults",
1149 "numa_hint_faults_local",
1150 "numa_pages_migrated",
1151#endif
Mel Gorman5647bc22012-10-19 10:46:20 +01001152#ifdef CONFIG_MIGRATION
1153 "pgmigrate_success",
1154 "pgmigrate_fail",
1155#endif
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001156#ifdef CONFIG_COMPACTION
Mel Gorman397487d2012-10-19 12:00:10 +01001157 "compact_migrate_scanned",
1158 "compact_free_scanned",
1159 "compact_isolated",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001160 "compact_stall",
1161 "compact_fail",
1162 "compact_success",
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001163 "compact_daemon_wake",
David Rientjes7f354a52017-02-22 15:44:50 -08001164 "compact_daemon_migrate_scanned",
1165 "compact_daemon_free_scanned",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001166#endif
1167
1168#ifdef CONFIG_HUGETLB_PAGE
1169 "htlb_buddy_alloc_success",
1170 "htlb_buddy_alloc_fail",
1171#endif
1172 "unevictable_pgs_culled",
1173 "unevictable_pgs_scanned",
1174 "unevictable_pgs_rescued",
1175 "unevictable_pgs_mlocked",
1176 "unevictable_pgs_munlocked",
1177 "unevictable_pgs_cleared",
1178 "unevictable_pgs_stranded",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001179
1180#ifdef CONFIG_TRANSPARENT_HUGEPAGE
1181 "thp_fault_alloc",
1182 "thp_fault_fallback",
1183 "thp_collapse_alloc",
1184 "thp_collapse_alloc_failed",
Kirill A. Shutemov95ecedc2016-07-26 15:25:31 -07001185 "thp_file_alloc",
1186 "thp_file_mapped",
Kirill A. Shutemov122afea2016-01-15 16:52:46 -08001187 "thp_split_page",
1188 "thp_split_page_failed",
Kirill A. Shutemovf9719a02016-03-17 14:18:45 -07001189 "thp_deferred_split_page",
Kirill A. Shutemov122afea2016-01-15 16:52:46 -08001190 "thp_split_pmd",
Yisheng Xiece9311c2017-03-09 16:17:00 -08001191#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
1192 "thp_split_pud",
1193#endif
Kirill A. Shutemovd8a8e1f2012-12-12 13:51:09 -08001194 "thp_zero_page_alloc",
1195 "thp_zero_page_alloc_failed",
Huang Ying225311a2017-09-06 16:22:30 -07001196 "thp_swpout",
Huang Yingfe490cc2017-09-06 16:22:52 -07001197 "thp_swpout_fallback",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001198#endif
Konstantin Khlebnikov09316c02014-10-09 15:29:32 -07001199#ifdef CONFIG_MEMORY_BALLOON
1200 "balloon_inflate",
1201 "balloon_deflate",
1202#ifdef CONFIG_BALLOON_COMPACTION
1203 "balloon_migrate",
1204#endif
1205#endif /* CONFIG_MEMORY_BALLOON */
Mel Gormanec659932014-01-21 14:33:16 -08001206#ifdef CONFIG_DEBUG_TLBFLUSH
Dave Hansen6df46862013-09-11 14:20:24 -07001207#ifdef CONFIG_SMP
Dave Hansen9824cf92013-09-11 14:20:23 -07001208 "nr_tlb_remote_flush",
1209 "nr_tlb_remote_flush_received",
Mel Gormanec659932014-01-21 14:33:16 -08001210#endif /* CONFIG_SMP */
Dave Hansen9824cf92013-09-11 14:20:23 -07001211 "nr_tlb_local_flush_all",
1212 "nr_tlb_local_flush_one",
Mel Gormanec659932014-01-21 14:33:16 -08001213#endif /* CONFIG_DEBUG_TLBFLUSH */
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001214
Davidlohr Bueso4f115142014-06-04 16:06:46 -07001215#ifdef CONFIG_DEBUG_VM_VMACACHE
1216 "vmacache_find_calls",
1217 "vmacache_find_hits",
Davidlohr Buesof5f302e2014-12-12 16:56:10 -08001218 "vmacache_full_flushes",
Davidlohr Bueso4f115142014-06-04 16:06:46 -07001219#endif
Huang Yingcbc65df2017-09-06 16:24:29 -07001220#ifdef CONFIG_SWAP
1221 "swap_ra",
1222 "swap_ra_hit",
1223#endif
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001224#endif /* CONFIG_VM_EVENTS_COUNTERS */
1225};
David Rientjes0d6617c2011-09-14 16:21:05 -07001226#endif /* CONFIG_PROC_FS || CONFIG_SYSFS || CONFIG_NUMA */
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001227
Andrew Morton3c486872015-02-10 14:09:43 -08001228#if (defined(CONFIG_DEBUG_FS) && defined(CONFIG_COMPACTION)) || \
1229 defined(CONFIG_PROC_FS)
1230static void *frag_start(struct seq_file *m, loff_t *pos)
1231{
1232 pg_data_t *pgdat;
1233 loff_t node = *pos;
1234
1235 for (pgdat = first_online_pgdat();
1236 pgdat && node;
1237 pgdat = next_online_pgdat(pgdat))
1238 --node;
1239
1240 return pgdat;
1241}
1242
1243static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
1244{
1245 pg_data_t *pgdat = (pg_data_t *)arg;
1246
1247 (*pos)++;
1248 return next_online_pgdat(pgdat);
1249}
1250
1251static void frag_stop(struct seq_file *m, void *arg)
1252{
1253}
1254
David Rientjesb2bd8592017-05-03 14:52:59 -07001255/*
1256 * Walk zones in a node and print using a callback.
1257 * If @assert_populated is true, only use callback for zones that are populated.
1258 */
Andrew Morton3c486872015-02-10 14:09:43 -08001259static void walk_zones_in_node(struct seq_file *m, pg_data_t *pgdat,
Vinayak Menon727c0802017-07-10 15:49:17 -07001260 bool assert_populated, bool nolock,
Andrew Morton3c486872015-02-10 14:09:43 -08001261 void (*print)(struct seq_file *m, pg_data_t *, struct zone *))
1262{
1263 struct zone *zone;
1264 struct zone *node_zones = pgdat->node_zones;
1265 unsigned long flags;
1266
1267 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
David Rientjesb2bd8592017-05-03 14:52:59 -07001268 if (assert_populated && !populated_zone(zone))
Andrew Morton3c486872015-02-10 14:09:43 -08001269 continue;
1270
Vinayak Menon727c0802017-07-10 15:49:17 -07001271 if (!nolock)
1272 spin_lock_irqsave(&zone->lock, flags);
Andrew Morton3c486872015-02-10 14:09:43 -08001273 print(m, pgdat, zone);
Vinayak Menon727c0802017-07-10 15:49:17 -07001274 if (!nolock)
1275 spin_unlock_irqrestore(&zone->lock, flags);
Andrew Morton3c486872015-02-10 14:09:43 -08001276 }
1277}
1278#endif
1279
Mel Gormand7a57522010-05-24 14:32:25 -07001280#ifdef CONFIG_PROC_FS
Mel Gorman467c9962007-10-16 01:26:02 -07001281static void frag_show_print(struct seq_file *m, pg_data_t *pgdat,
1282 struct zone *zone)
1283{
1284 int order;
1285
1286 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
1287 for (order = 0; order < MAX_ORDER; ++order)
1288 seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
1289 seq_putc(m, '\n');
1290}
1291
1292/*
1293 * This walks the free areas for each zone.
1294 */
1295static int frag_show(struct seq_file *m, void *arg)
1296{
1297 pg_data_t *pgdat = (pg_data_t *)arg;
Vinayak Menon727c0802017-07-10 15:49:17 -07001298 walk_zones_in_node(m, pgdat, true, false, frag_show_print);
Mel Gorman467c9962007-10-16 01:26:02 -07001299 return 0;
1300}
1301
1302static void pagetypeinfo_showfree_print(struct seq_file *m,
1303 pg_data_t *pgdat, struct zone *zone)
1304{
1305 int order, mtype;
1306
1307 for (mtype = 0; mtype < MIGRATE_TYPES; mtype++) {
1308 seq_printf(m, "Node %4d, zone %8s, type %12s ",
1309 pgdat->node_id,
1310 zone->name,
1311 migratetype_names[mtype]);
1312 for (order = 0; order < MAX_ORDER; ++order) {
1313 unsigned long freecount = 0;
1314 struct free_area *area;
1315 struct list_head *curr;
1316
1317 area = &(zone->free_area[order]);
1318
1319 list_for_each(curr, &area->free_list[mtype])
1320 freecount++;
1321 seq_printf(m, "%6lu ", freecount);
1322 }
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001323 seq_putc(m, '\n');
1324 }
Mel Gorman467c9962007-10-16 01:26:02 -07001325}
1326
1327/* Print out the free pages at each order for each migatetype */
1328static int pagetypeinfo_showfree(struct seq_file *m, void *arg)
1329{
1330 int order;
1331 pg_data_t *pgdat = (pg_data_t *)arg;
1332
1333 /* Print header */
1334 seq_printf(m, "%-43s ", "Free pages count per migrate type at order");
1335 for (order = 0; order < MAX_ORDER; ++order)
1336 seq_printf(m, "%6d ", order);
1337 seq_putc(m, '\n');
1338
Vinayak Menon727c0802017-07-10 15:49:17 -07001339 walk_zones_in_node(m, pgdat, true, false, pagetypeinfo_showfree_print);
Mel Gorman467c9962007-10-16 01:26:02 -07001340
1341 return 0;
1342}
1343
1344static void pagetypeinfo_showblockcount_print(struct seq_file *m,
1345 pg_data_t *pgdat, struct zone *zone)
1346{
1347 int mtype;
1348 unsigned long pfn;
1349 unsigned long start_pfn = zone->zone_start_pfn;
Cody P Schafer108bcc92013-02-22 16:35:23 -08001350 unsigned long end_pfn = zone_end_pfn(zone);
Mel Gorman467c9962007-10-16 01:26:02 -07001351 unsigned long count[MIGRATE_TYPES] = { 0, };
1352
1353 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
1354 struct page *page;
1355
Michal Hockod336e942017-07-06 15:38:07 -07001356 page = pfn_to_online_page(pfn);
1357 if (!page)
Mel Gorman467c9962007-10-16 01:26:02 -07001358 continue;
1359
Mel Gormaneb335752009-05-13 17:34:48 +01001360 /* Watch for unexpected holes punched in the memmap */
1361 if (!memmap_valid_within(pfn, page, zone))
Mel Gormane80d6a22008-08-14 11:10:14 +01001362 continue;
Mel Gormaneb335752009-05-13 17:34:48 +01001363
Joonsoo Kima91c43c2016-05-19 17:12:10 -07001364 if (page_zone(page) != zone)
1365 continue;
1366
Mel Gorman467c9962007-10-16 01:26:02 -07001367 mtype = get_pageblock_migratetype(page);
1368
Mel Gormane80d6a22008-08-14 11:10:14 +01001369 if (mtype < MIGRATE_TYPES)
1370 count[mtype]++;
Mel Gorman467c9962007-10-16 01:26:02 -07001371 }
1372
1373 /* Print counts */
1374 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
1375 for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
1376 seq_printf(m, "%12lu ", count[mtype]);
1377 seq_putc(m, '\n');
1378}
1379
SeongJae Parkf113e642017-09-06 16:24:23 -07001380/* Print out the number of pageblocks for each migratetype */
Mel Gorman467c9962007-10-16 01:26:02 -07001381static int pagetypeinfo_showblockcount(struct seq_file *m, void *arg)
1382{
1383 int mtype;
1384 pg_data_t *pgdat = (pg_data_t *)arg;
1385
1386 seq_printf(m, "\n%-23s", "Number of blocks type ");
1387 for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
1388 seq_printf(m, "%12s ", migratetype_names[mtype]);
1389 seq_putc(m, '\n');
Vinayak Menon727c0802017-07-10 15:49:17 -07001390 walk_zones_in_node(m, pgdat, true, false,
1391 pagetypeinfo_showblockcount_print);
Mel Gorman467c9962007-10-16 01:26:02 -07001392
1393 return 0;
1394}
1395
Joonsoo Kim48c96a32014-12-12 16:56:01 -08001396/*
1397 * Print out the number of pageblocks for each migratetype that contain pages
1398 * of other types. This gives an indication of how well fallbacks are being
1399 * contained by rmqueue_fallback(). It requires information from PAGE_OWNER
1400 * to determine what is going on
1401 */
1402static void pagetypeinfo_showmixedcount(struct seq_file *m, pg_data_t *pgdat)
1403{
1404#ifdef CONFIG_PAGE_OWNER
1405 int mtype;
1406
Vlastimil Babka7dd80b82016-03-15 14:56:12 -07001407 if (!static_branch_unlikely(&page_owner_inited))
Joonsoo Kim48c96a32014-12-12 16:56:01 -08001408 return;
1409
1410 drain_all_pages(NULL);
1411
1412 seq_printf(m, "\n%-23s", "Number of mixed blocks ");
1413 for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
1414 seq_printf(m, "%12s ", migratetype_names[mtype]);
1415 seq_putc(m, '\n');
1416
Vinayak Menon727c0802017-07-10 15:49:17 -07001417 walk_zones_in_node(m, pgdat, true, true,
1418 pagetypeinfo_showmixedcount_print);
Joonsoo Kim48c96a32014-12-12 16:56:01 -08001419#endif /* CONFIG_PAGE_OWNER */
1420}
1421
Mel Gorman467c9962007-10-16 01:26:02 -07001422/*
1423 * This prints out statistics in relation to grouping pages by mobility.
1424 * It is expensive to collect so do not constantly read the file.
1425 */
1426static int pagetypeinfo_show(struct seq_file *m, void *arg)
1427{
1428 pg_data_t *pgdat = (pg_data_t *)arg;
1429
KOSAKI Motohiro41b25a32008-04-30 00:52:13 -07001430 /* check memoryless node */
Lai Jiangshana47b53c2012-12-12 13:51:37 -08001431 if (!node_state(pgdat->node_id, N_MEMORY))
KOSAKI Motohiro41b25a32008-04-30 00:52:13 -07001432 return 0;
1433
Mel Gorman467c9962007-10-16 01:26:02 -07001434 seq_printf(m, "Page block order: %d\n", pageblock_order);
1435 seq_printf(m, "Pages per block: %lu\n", pageblock_nr_pages);
1436 seq_putc(m, '\n');
1437 pagetypeinfo_showfree(m, pgdat);
1438 pagetypeinfo_showblockcount(m, pgdat);
Joonsoo Kim48c96a32014-12-12 16:56:01 -08001439 pagetypeinfo_showmixedcount(m, pgdat);
Mel Gorman467c9962007-10-16 01:26:02 -07001440
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001441 return 0;
1442}
1443
Alexey Dobriyan8f32f7e2008-10-06 04:13:52 +04001444static const struct seq_operations fragmentation_op = {
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001445 .start = frag_start,
1446 .next = frag_next,
1447 .stop = frag_stop,
1448 .show = frag_show,
1449};
1450
Alexey Dobriyan8f32f7e2008-10-06 04:13:52 +04001451static int fragmentation_open(struct inode *inode, struct file *file)
1452{
1453 return seq_open(file, &fragmentation_op);
1454}
1455
Anshuman Khandual9d85e152017-07-06 15:37:15 -07001456static const struct file_operations buddyinfo_file_operations = {
Alexey Dobriyan8f32f7e2008-10-06 04:13:52 +04001457 .open = fragmentation_open,
1458 .read = seq_read,
1459 .llseek = seq_lseek,
1460 .release = seq_release,
1461};
1462
Alexey Dobriyan74e2e8e2008-10-06 04:15:36 +04001463static const struct seq_operations pagetypeinfo_op = {
Mel Gorman467c9962007-10-16 01:26:02 -07001464 .start = frag_start,
1465 .next = frag_next,
1466 .stop = frag_stop,
1467 .show = pagetypeinfo_show,
1468};
1469
Alexey Dobriyan74e2e8e2008-10-06 04:15:36 +04001470static int pagetypeinfo_open(struct inode *inode, struct file *file)
1471{
1472 return seq_open(file, &pagetypeinfo_op);
1473}
1474
Anshuman Khandual9d85e152017-07-06 15:37:15 -07001475static const struct file_operations pagetypeinfo_file_operations = {
Alexey Dobriyan74e2e8e2008-10-06 04:15:36 +04001476 .open = pagetypeinfo_open,
1477 .read = seq_read,
1478 .llseek = seq_lseek,
1479 .release = seq_release,
1480};
1481
Mel Gormane2ecc8a2016-07-28 15:47:02 -07001482static bool is_zone_first_populated(pg_data_t *pgdat, struct zone *zone)
1483{
1484 int zid;
1485
1486 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
1487 struct zone *compare = &pgdat->node_zones[zid];
1488
1489 if (populated_zone(compare))
1490 return zone == compare;
1491 }
1492
Mel Gormane2ecc8a2016-07-28 15:47:02 -07001493 return false;
1494}
1495
Mel Gorman467c9962007-10-16 01:26:02 -07001496static void zoneinfo_show_print(struct seq_file *m, pg_data_t *pgdat,
1497 struct zone *zone)
1498{
1499 int i;
1500 seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
Mel Gormane2ecc8a2016-07-28 15:47:02 -07001501 if (is_zone_first_populated(pgdat, zone)) {
1502 seq_printf(m, "\n per-node stats");
1503 for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++) {
1504 seq_printf(m, "\n %-12s %lu",
Kemi Wang3a321d22017-09-08 16:12:48 -07001505 vmstat_text[i + NR_VM_ZONE_STAT_ITEMS +
1506 NR_VM_NUMA_STAT_ITEMS],
Mel Gormane2ecc8a2016-07-28 15:47:02 -07001507 node_page_state(pgdat, i));
1508 }
1509 }
Mel Gorman467c9962007-10-16 01:26:02 -07001510 seq_printf(m,
1511 "\n pages free %lu"
1512 "\n min %lu"
1513 "\n low %lu"
1514 "\n high %lu"
Mel Gorman467c9962007-10-16 01:26:02 -07001515 "\n spanned %lu"
Jiang Liu9feedc92012-12-12 13:52:12 -08001516 "\n present %lu"
1517 "\n managed %lu",
Mel Gorman88f5acf2011-01-13 15:45:41 -08001518 zone_page_state(zone, NR_FREE_PAGES),
Mel Gorman41858962009-06-16 15:32:12 -07001519 min_wmark_pages(zone),
1520 low_wmark_pages(zone),
1521 high_wmark_pages(zone),
Mel Gorman467c9962007-10-16 01:26:02 -07001522 zone->spanned_pages,
Jiang Liu9feedc92012-12-12 13:52:12 -08001523 zone->present_pages,
1524 zone->managed_pages);
Mel Gorman467c9962007-10-16 01:26:02 -07001525
Mel Gorman467c9962007-10-16 01:26:02 -07001526 seq_printf(m,
Mel Gorman3484b2d2014-08-06 16:07:14 -07001527 "\n protection: (%ld",
Mel Gorman467c9962007-10-16 01:26:02 -07001528 zone->lowmem_reserve[0]);
1529 for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
Mel Gorman3484b2d2014-08-06 16:07:14 -07001530 seq_printf(m, ", %ld", zone->lowmem_reserve[i]);
David Rientjes7dfb8bf2017-05-03 14:53:02 -07001531 seq_putc(m, ')');
1532
1533 /* If unpopulated, no other information is useful */
1534 if (!populated_zone(zone)) {
1535 seq_putc(m, '\n');
1536 return;
1537 }
1538
1539 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
1540 seq_printf(m, "\n %-12s %lu", vmstat_text[i],
1541 zone_page_state(zone, i));
1542
Kemi Wang3a321d22017-09-08 16:12:48 -07001543#ifdef CONFIG_NUMA
1544 for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++)
1545 seq_printf(m, "\n %-12s %lu",
1546 vmstat_text[i + NR_VM_ZONE_STAT_ITEMS],
1547 zone_numa_state(zone, i));
1548#endif
1549
David Rientjes7dfb8bf2017-05-03 14:53:02 -07001550 seq_printf(m, "\n pagesets");
Mel Gorman467c9962007-10-16 01:26:02 -07001551 for_each_online_cpu(i) {
1552 struct per_cpu_pageset *pageset;
Mel Gorman467c9962007-10-16 01:26:02 -07001553
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09001554 pageset = per_cpu_ptr(zone->pageset, i);
Christoph Lameter3dfa5722008-02-04 22:29:19 -08001555 seq_printf(m,
1556 "\n cpu: %i"
1557 "\n count: %i"
1558 "\n high: %i"
1559 "\n batch: %i",
1560 i,
1561 pageset->pcp.count,
1562 pageset->pcp.high,
1563 pageset->pcp.batch);
Mel Gorman467c9962007-10-16 01:26:02 -07001564#ifdef CONFIG_SMP
1565 seq_printf(m, "\n vm stats threshold: %d",
1566 pageset->stat_threshold);
1567#endif
1568 }
1569 seq_printf(m,
Mel Gorman599d0c92016-07-28 15:45:31 -07001570 "\n node_unreclaimable: %u"
1571 "\n start_pfn: %lu"
1572 "\n node_inactive_ratio: %u",
Johannes Weinerc73322d2017-05-03 14:51:51 -07001573 pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES,
Rik van Riel556adec2008-10-18 20:26:34 -07001574 zone->zone_start_pfn,
Mel Gorman599d0c92016-07-28 15:45:31 -07001575 zone->zone_pgdat->inactive_ratio);
Mel Gorman467c9962007-10-16 01:26:02 -07001576 seq_putc(m, '\n');
1577}
1578
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001579/*
David Rientjesb2bd8592017-05-03 14:52:59 -07001580 * Output information about zones in @pgdat. All zones are printed regardless
1581 * of whether they are populated or not: lowmem_reserve_ratio operates on the
1582 * set of all zones and userspace would not be aware of such zones if they are
1583 * suppressed here (zoneinfo displays the effect of lowmem_reserve_ratio).
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001584 */
1585static int zoneinfo_show(struct seq_file *m, void *arg)
1586{
Mel Gorman467c9962007-10-16 01:26:02 -07001587 pg_data_t *pgdat = (pg_data_t *)arg;
Vinayak Menon727c0802017-07-10 15:49:17 -07001588 walk_zones_in_node(m, pgdat, false, false, zoneinfo_show_print);
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001589 return 0;
1590}
1591
Alexey Dobriyan5c9fe622008-10-06 04:19:42 +04001592static const struct seq_operations zoneinfo_op = {
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001593 .start = frag_start, /* iterate over all zones. The same as in
1594 * fragmentation. */
1595 .next = frag_next,
1596 .stop = frag_stop,
1597 .show = zoneinfo_show,
1598};
1599
Alexey Dobriyan5c9fe622008-10-06 04:19:42 +04001600static int zoneinfo_open(struct inode *inode, struct file *file)
1601{
1602 return seq_open(file, &zoneinfo_op);
1603}
1604
Anshuman Khandual9d85e152017-07-06 15:37:15 -07001605static const struct file_operations zoneinfo_file_operations = {
Alexey Dobriyan5c9fe622008-10-06 04:19:42 +04001606 .open = zoneinfo_open,
1607 .read = seq_read,
1608 .llseek = seq_lseek,
1609 .release = seq_release,
1610};
1611
Michael Rubin79da8262010-10-26 14:21:36 -07001612enum writeback_stat_item {
1613 NR_DIRTY_THRESHOLD,
1614 NR_DIRTY_BG_THRESHOLD,
1615 NR_VM_WRITEBACK_STAT_ITEMS,
1616};
1617
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001618static void *vmstat_start(struct seq_file *m, loff_t *pos)
1619{
Christoph Lameter2244b952006-06-30 01:55:33 -07001620 unsigned long *v;
Michael Rubin79da8262010-10-26 14:21:36 -07001621 int i, stat_items_size;
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001622
1623 if (*pos >= ARRAY_SIZE(vmstat_text))
1624 return NULL;
Michael Rubin79da8262010-10-26 14:21:36 -07001625 stat_items_size = NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long) +
Kemi Wang3a321d22017-09-08 16:12:48 -07001626 NR_VM_NUMA_STAT_ITEMS * sizeof(unsigned long) +
Mel Gorman75ef7182016-07-28 15:45:24 -07001627 NR_VM_NODE_STAT_ITEMS * sizeof(unsigned long) +
Michael Rubin79da8262010-10-26 14:21:36 -07001628 NR_VM_WRITEBACK_STAT_ITEMS * sizeof(unsigned long);
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001629
Christoph Lameterf8891e52006-06-30 01:55:45 -07001630#ifdef CONFIG_VM_EVENT_COUNTERS
Michael Rubin79da8262010-10-26 14:21:36 -07001631 stat_items_size += sizeof(struct vm_event_state);
Christoph Lameterf8891e52006-06-30 01:55:45 -07001632#endif
Michael Rubin79da8262010-10-26 14:21:36 -07001633
1634 v = kmalloc(stat_items_size, GFP_KERNEL);
Christoph Lameter2244b952006-06-30 01:55:33 -07001635 m->private = v;
1636 if (!v)
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001637 return ERR_PTR(-ENOMEM);
Christoph Lameter2244b952006-06-30 01:55:33 -07001638 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
Michal Hockoc41f0122017-09-06 16:23:36 -07001639 v[i] = global_zone_page_state(i);
Michael Rubin79da8262010-10-26 14:21:36 -07001640 v += NR_VM_ZONE_STAT_ITEMS;
1641
Kemi Wang3a321d22017-09-08 16:12:48 -07001642#ifdef CONFIG_NUMA
1643 for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++)
1644 v[i] = global_numa_state(i);
1645 v += NR_VM_NUMA_STAT_ITEMS;
1646#endif
1647
Mel Gorman75ef7182016-07-28 15:45:24 -07001648 for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++)
1649 v[i] = global_node_page_state(i);
1650 v += NR_VM_NODE_STAT_ITEMS;
1651
Michael Rubin79da8262010-10-26 14:21:36 -07001652 global_dirty_limits(v + NR_DIRTY_BG_THRESHOLD,
1653 v + NR_DIRTY_THRESHOLD);
1654 v += NR_VM_WRITEBACK_STAT_ITEMS;
1655
Christoph Lameterf8891e52006-06-30 01:55:45 -07001656#ifdef CONFIG_VM_EVENT_COUNTERS
Michael Rubin79da8262010-10-26 14:21:36 -07001657 all_vm_events(v);
1658 v[PGPGIN] /= 2; /* sectors -> kbytes */
1659 v[PGPGOUT] /= 2;
Christoph Lameterf8891e52006-06-30 01:55:45 -07001660#endif
Wu Fengguangff8b16d2010-11-04 01:56:49 +08001661 return (unsigned long *)m->private + *pos;
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001662}
1663
1664static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
1665{
1666 (*pos)++;
1667 if (*pos >= ARRAY_SIZE(vmstat_text))
1668 return NULL;
1669 return (unsigned long *)m->private + *pos;
1670}
1671
1672static int vmstat_show(struct seq_file *m, void *arg)
1673{
1674 unsigned long *l = arg;
1675 unsigned long off = l - (unsigned long *)m->private;
Alexey Dobriyan68ba0322016-10-07 17:02:14 -07001676
1677 seq_puts(m, vmstat_text[off]);
Joe Perches75ba1d02016-10-07 17:02:20 -07001678 seq_put_decimal_ull(m, " ", *l);
Alexey Dobriyan68ba0322016-10-07 17:02:14 -07001679 seq_putc(m, '\n');
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001680 return 0;
1681}
1682
1683static void vmstat_stop(struct seq_file *m, void *arg)
1684{
1685 kfree(m->private);
1686 m->private = NULL;
1687}
1688
Alexey Dobriyanb6aa44a2008-10-06 04:17:48 +04001689static const struct seq_operations vmstat_op = {
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001690 .start = vmstat_start,
1691 .next = vmstat_next,
1692 .stop = vmstat_stop,
1693 .show = vmstat_show,
1694};
1695
Alexey Dobriyanb6aa44a2008-10-06 04:17:48 +04001696static int vmstat_open(struct inode *inode, struct file *file)
1697{
1698 return seq_open(file, &vmstat_op);
1699}
1700
Anshuman Khandual9d85e152017-07-06 15:37:15 -07001701static const struct file_operations vmstat_file_operations = {
Alexey Dobriyanb6aa44a2008-10-06 04:17:48 +04001702 .open = vmstat_open,
1703 .read = seq_read,
1704 .llseek = seq_lseek,
1705 .release = seq_release,
1706};
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001707#endif /* CONFIG_PROC_FS */
1708
Christoph Lameterdf9ecab2006-08-31 21:27:35 -07001709#ifdef CONFIG_SMP
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001710static DEFINE_PER_CPU(struct delayed_work, vmstat_work);
Christoph Lameter77461ab2007-05-09 02:35:13 -07001711int sysctl_stat_interval __read_mostly = HZ;
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001712
Hugh Dickins52b6f462016-05-19 17:12:50 -07001713#ifdef CONFIG_PROC_FS
1714static void refresh_vm_stats(struct work_struct *work)
1715{
1716 refresh_cpu_vm_stats(true);
1717}
1718
1719int vmstat_refresh(struct ctl_table *table, int write,
1720 void __user *buffer, size_t *lenp, loff_t *ppos)
1721{
1722 long val;
1723 int err;
1724 int i;
1725
1726 /*
1727 * The regular update, every sysctl_stat_interval, may come later
1728 * than expected: leaving a significant amount in per_cpu buckets.
1729 * This is particularly misleading when checking a quantity of HUGE
1730 * pages, immediately after running a test. /proc/sys/vm/stat_refresh,
1731 * which can equally be echo'ed to or cat'ted from (by root),
1732 * can be used to update the stats just before reading them.
1733 *
Michal Hockoc41f0122017-09-06 16:23:36 -07001734 * Oh, and since global_zone_page_state() etc. are so careful to hide
Hugh Dickins52b6f462016-05-19 17:12:50 -07001735 * transiently negative values, report an error here if any of
1736 * the stats is negative, so we know to go looking for imbalance.
1737 */
1738 err = schedule_on_each_cpu(refresh_vm_stats);
1739 if (err)
1740 return err;
1741 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++) {
Mel Gorman75ef7182016-07-28 15:45:24 -07001742 val = atomic_long_read(&vm_zone_stat[i]);
Hugh Dickins52b6f462016-05-19 17:12:50 -07001743 if (val < 0) {
Johannes Weinerc822f622017-05-03 14:52:10 -07001744 pr_warn("%s: %s %ld\n",
1745 __func__, vmstat_text[i], val);
1746 err = -EINVAL;
Hugh Dickins52b6f462016-05-19 17:12:50 -07001747 }
1748 }
Kemi Wang3a321d22017-09-08 16:12:48 -07001749#ifdef CONFIG_NUMA
1750 for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++) {
1751 val = atomic_long_read(&vm_numa_stat[i]);
1752 if (val < 0) {
1753 pr_warn("%s: %s %ld\n",
1754 __func__, vmstat_text[i + NR_VM_ZONE_STAT_ITEMS], val);
1755 err = -EINVAL;
1756 }
1757 }
1758#endif
Hugh Dickins52b6f462016-05-19 17:12:50 -07001759 if (err)
1760 return err;
1761 if (write)
1762 *ppos += *lenp;
1763 else
1764 *lenp = 0;
1765 return 0;
1766}
1767#endif /* CONFIG_PROC_FS */
1768
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001769static void vmstat_update(struct work_struct *w)
1770{
Christoph Lameter0eb77e92016-01-14 15:21:40 -08001771 if (refresh_cpu_vm_stats(true)) {
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001772 /*
1773 * Counters were updated so we expect more updates
1774 * to occur in the future. Keep on running the
1775 * update worker thread.
1776 */
Michal Hockoce612872017-04-07 16:05:05 -07001777 queue_delayed_work_on(smp_processor_id(), mm_percpu_wq,
Michal Hockof01f17d2016-02-05 15:36:24 -08001778 this_cpu_ptr(&vmstat_work),
1779 round_jiffies_relative(sysctl_stat_interval));
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001780 }
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001781}
1782
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001783/*
Christoph Lameter0eb77e92016-01-14 15:21:40 -08001784 * Switch off vmstat processing and then fold all the remaining differentials
1785 * until the diffs stay at zero. The function is used by NOHZ and can only be
1786 * invoked when tick processing is not active.
1787 */
Christoph Lameter0eb77e92016-01-14 15:21:40 -08001788/*
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001789 * Check if the diffs for a certain cpu indicate that
1790 * an update is needed.
1791 */
1792static bool need_update(int cpu)
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001793{
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001794 struct zone *zone;
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001795
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001796 for_each_populated_zone(zone) {
1797 struct per_cpu_pageset *p = per_cpu_ptr(zone->pageset, cpu);
1798
1799 BUILD_BUG_ON(sizeof(p->vm_stat_diff[0]) != 1);
Kemi Wang3a321d22017-09-08 16:12:48 -07001800#ifdef CONFIG_NUMA
1801 BUILD_BUG_ON(sizeof(p->vm_numa_stat_diff[0]) != 1);
1802#endif
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001803 /*
1804 * The fast way of checking if there are any vmstat diffs.
1805 * This works because the diffs are byte sized items.
1806 */
1807 if (memchr_inv(p->vm_stat_diff, 0, NR_VM_ZONE_STAT_ITEMS))
1808 return true;
Kemi Wang3a321d22017-09-08 16:12:48 -07001809#ifdef CONFIG_NUMA
1810 if (memchr_inv(p->vm_numa_stat_diff, 0, NR_VM_NUMA_STAT_ITEMS))
1811 return true;
1812#endif
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001813 }
1814 return false;
1815}
1816
Christoph Lameter7b8da4c2016-05-20 16:58:21 -07001817/*
1818 * Switch off vmstat processing and then fold all the remaining differentials
1819 * until the diffs stay at zero. The function is used by NOHZ and can only be
1820 * invoked when tick processing is not active.
1821 */
Michal Hockof01f17d2016-02-05 15:36:24 -08001822void quiet_vmstat(void)
1823{
1824 if (system_state != SYSTEM_RUNNING)
1825 return;
1826
Christoph Lameter7b8da4c2016-05-20 16:58:21 -07001827 if (!delayed_work_pending(this_cpu_ptr(&vmstat_work)))
Michal Hockof01f17d2016-02-05 15:36:24 -08001828 return;
1829
1830 if (!need_update(smp_processor_id()))
1831 return;
1832
1833 /*
1834 * Just refresh counters and do not care about the pending delayed
1835 * vmstat_update. It doesn't fire that often to matter and canceling
1836 * it would be too expensive from this path.
1837 * vmstat_shepherd will take care about that for us.
1838 */
1839 refresh_cpu_vm_stats(false);
1840}
1841
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001842/*
1843 * Shepherd worker thread that checks the
1844 * differentials of processors that have their worker
1845 * threads for vm statistics updates disabled because of
1846 * inactivity.
1847 */
1848static void vmstat_shepherd(struct work_struct *w);
1849
Christoph Lameter0eb77e92016-01-14 15:21:40 -08001850static DECLARE_DEFERRABLE_WORK(shepherd, vmstat_shepherd);
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001851
1852static void vmstat_shepherd(struct work_struct *w)
1853{
1854 int cpu;
1855
1856 get_online_cpus();
1857 /* Check processors whose vmstat worker threads have been disabled */
Christoph Lameter7b8da4c2016-05-20 16:58:21 -07001858 for_each_online_cpu(cpu) {
Michal Hockof01f17d2016-02-05 15:36:24 -08001859 struct delayed_work *dw = &per_cpu(vmstat_work, cpu);
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001860
Christoph Lameter7b8da4c2016-05-20 16:58:21 -07001861 if (!delayed_work_pending(dw) && need_update(cpu))
Michal Hockoce612872017-04-07 16:05:05 -07001862 queue_delayed_work_on(cpu, mm_percpu_wq, dw, 0);
Michal Hockof01f17d2016-02-05 15:36:24 -08001863 }
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001864 put_online_cpus();
1865
1866 schedule_delayed_work(&shepherd,
1867 round_jiffies_relative(sysctl_stat_interval));
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001868}
1869
1870static void __init start_shepherd_timer(void)
1871{
1872 int cpu;
1873
1874 for_each_possible_cpu(cpu)
Michal Hockoccde8bd2016-02-05 15:36:27 -08001875 INIT_DEFERRABLE_WORK(per_cpu_ptr(&vmstat_work, cpu),
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001876 vmstat_update);
1877
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001878 schedule_delayed_work(&shepherd,
1879 round_jiffies_relative(sysctl_stat_interval));
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001880}
1881
Tim Chen03e86db2016-10-07 17:00:02 -07001882static void __init init_cpu_node_state(void)
1883{
Sebastian Andrzej Siewior4c501322016-11-29 15:51:14 +01001884 int node;
Tim Chen03e86db2016-10-07 17:00:02 -07001885
Sebastian Andrzej Siewior4c501322016-11-29 15:51:14 +01001886 for_each_online_node(node) {
1887 if (cpumask_weight(cpumask_of_node(node)) > 0)
1888 node_set_state(node, N_CPU);
1889 }
Tim Chen03e86db2016-10-07 17:00:02 -07001890}
1891
Sebastian Andrzej Siewior5438da92016-11-29 15:52:21 +01001892static int vmstat_cpu_online(unsigned int cpu)
1893{
1894 refresh_zone_stat_thresholds();
1895 node_set_state(cpu_to_node(cpu), N_CPU);
1896 return 0;
1897}
1898
1899static int vmstat_cpu_down_prep(unsigned int cpu)
1900{
1901 cancel_delayed_work_sync(&per_cpu(vmstat_work, cpu));
1902 return 0;
1903}
1904
1905static int vmstat_cpu_dead(unsigned int cpu)
Toshi Kani807a1bd2013-11-12 15:08:13 -08001906{
Sebastian Andrzej Siewior4c501322016-11-29 15:51:14 +01001907 const struct cpumask *node_cpus;
Sebastian Andrzej Siewior5438da92016-11-29 15:52:21 +01001908 int node;
Toshi Kani807a1bd2013-11-12 15:08:13 -08001909
Sebastian Andrzej Siewior5438da92016-11-29 15:52:21 +01001910 node = cpu_to_node(cpu);
1911
1912 refresh_zone_stat_thresholds();
Sebastian Andrzej Siewior4c501322016-11-29 15:51:14 +01001913 node_cpus = cpumask_of_node(node);
1914 if (cpumask_weight(node_cpus) > 0)
Sebastian Andrzej Siewior5438da92016-11-29 15:52:21 +01001915 return 0;
Toshi Kani807a1bd2013-11-12 15:08:13 -08001916
1917 node_clear_state(node, N_CPU);
Sebastian Andrzej Siewior5438da92016-11-29 15:52:21 +01001918 return 0;
Toshi Kani807a1bd2013-11-12 15:08:13 -08001919}
1920
Alexey Dobriyan8f32f7e2008-10-06 04:13:52 +04001921#endif
Christoph Lameterdf9ecab2006-08-31 21:27:35 -07001922
Michal Hockoce612872017-04-07 16:05:05 -07001923struct workqueue_struct *mm_percpu_wq;
1924
Michal Hocko597b7302017-03-31 15:11:47 -07001925void __init init_mm_internals(void)
Christoph Lameterdf9ecab2006-08-31 21:27:35 -07001926{
Michal Hockoce612872017-04-07 16:05:05 -07001927 int ret __maybe_unused;
Sebastian Andrzej Siewior5438da92016-11-29 15:52:21 +01001928
Michal Hocko80d136e2017-04-19 09:52:46 +02001929 mm_percpu_wq = alloc_workqueue("mm_percpu_wq", WQ_MEM_RECLAIM, 0);
Michal Hockoce612872017-04-07 16:05:05 -07001930
1931#ifdef CONFIG_SMP
Sebastian Andrzej Siewior5438da92016-11-29 15:52:21 +01001932 ret = cpuhp_setup_state_nocalls(CPUHP_MM_VMSTAT_DEAD, "mm/vmstat:dead",
1933 NULL, vmstat_cpu_dead);
1934 if (ret < 0)
1935 pr_err("vmstat: failed to register 'dead' hotplug state\n");
1936
1937 ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "mm/vmstat:online",
1938 vmstat_cpu_online,
1939 vmstat_cpu_down_prep);
1940 if (ret < 0)
1941 pr_err("vmstat: failed to register 'online' hotplug state\n");
1942
1943 get_online_cpus();
Tim Chen03e86db2016-10-07 17:00:02 -07001944 init_cpu_node_state();
Sebastian Andrzej Siewior5438da92016-11-29 15:52:21 +01001945 put_online_cpus();
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001946
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001947 start_shepherd_timer();
Alexey Dobriyan8f32f7e2008-10-06 04:13:52 +04001948#endif
1949#ifdef CONFIG_PROC_FS
Anshuman Khandual9d85e152017-07-06 15:37:15 -07001950 proc_create("buddyinfo", 0444, NULL, &buddyinfo_file_operations);
1951 proc_create("pagetypeinfo", 0444, NULL, &pagetypeinfo_file_operations);
1952 proc_create("vmstat", 0444, NULL, &vmstat_file_operations);
1953 proc_create("zoneinfo", 0444, NULL, &zoneinfo_file_operations);
Alexey Dobriyan8f32f7e2008-10-06 04:13:52 +04001954#endif
Christoph Lameterdf9ecab2006-08-31 21:27:35 -07001955}
Mel Gormand7a57522010-05-24 14:32:25 -07001956
1957#if defined(CONFIG_DEBUG_FS) && defined(CONFIG_COMPACTION)
Mel Gormand7a57522010-05-24 14:32:25 -07001958
1959/*
1960 * Return an index indicating how much of the available free memory is
1961 * unusable for an allocation of the requested size.
1962 */
1963static int unusable_free_index(unsigned int order,
1964 struct contig_page_info *info)
1965{
1966 /* No free memory is interpreted as all free memory is unusable */
1967 if (info->free_pages == 0)
1968 return 1000;
1969
1970 /*
1971 * Index should be a value between 0 and 1. Return a value to 3
1972 * decimal places.
1973 *
1974 * 0 => no fragmentation
1975 * 1 => high fragmentation
1976 */
1977 return div_u64((info->free_pages - (info->free_blocks_suitable << order)) * 1000ULL, info->free_pages);
1978
1979}
1980
1981static void unusable_show_print(struct seq_file *m,
1982 pg_data_t *pgdat, struct zone *zone)
1983{
1984 unsigned int order;
1985 int index;
1986 struct contig_page_info info;
1987
1988 seq_printf(m, "Node %d, zone %8s ",
1989 pgdat->node_id,
1990 zone->name);
1991 for (order = 0; order < MAX_ORDER; ++order) {
1992 fill_contig_page_info(zone, order, &info);
1993 index = unusable_free_index(order, &info);
1994 seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
1995 }
1996
1997 seq_putc(m, '\n');
1998}
1999
2000/*
2001 * Display unusable free space index
2002 *
2003 * The unusable free space index measures how much of the available free
2004 * memory cannot be used to satisfy an allocation of a given size and is a
2005 * value between 0 and 1. The higher the value, the more of free memory is
2006 * unusable and by implication, the worse the external fragmentation is. This
2007 * can be expressed as a percentage by multiplying by 100.
2008 */
2009static int unusable_show(struct seq_file *m, void *arg)
2010{
2011 pg_data_t *pgdat = (pg_data_t *)arg;
2012
2013 /* check memoryless node */
Lai Jiangshana47b53c2012-12-12 13:51:37 -08002014 if (!node_state(pgdat->node_id, N_MEMORY))
Mel Gormand7a57522010-05-24 14:32:25 -07002015 return 0;
2016
Vinayak Menon727c0802017-07-10 15:49:17 -07002017 walk_zones_in_node(m, pgdat, true, false, unusable_show_print);
Mel Gormand7a57522010-05-24 14:32:25 -07002018
2019 return 0;
2020}
2021
2022static const struct seq_operations unusable_op = {
2023 .start = frag_start,
2024 .next = frag_next,
2025 .stop = frag_stop,
2026 .show = unusable_show,
2027};
2028
2029static int unusable_open(struct inode *inode, struct file *file)
2030{
2031 return seq_open(file, &unusable_op);
2032}
2033
2034static const struct file_operations unusable_file_ops = {
2035 .open = unusable_open,
2036 .read = seq_read,
2037 .llseek = seq_lseek,
2038 .release = seq_release,
2039};
2040
Mel Gormanf1a5ab12010-05-24 14:32:26 -07002041static void extfrag_show_print(struct seq_file *m,
2042 pg_data_t *pgdat, struct zone *zone)
2043{
2044 unsigned int order;
2045 int index;
2046
2047 /* Alloc on stack as interrupts are disabled for zone walk */
2048 struct contig_page_info info;
2049
2050 seq_printf(m, "Node %d, zone %8s ",
2051 pgdat->node_id,
2052 zone->name);
2053 for (order = 0; order < MAX_ORDER; ++order) {
2054 fill_contig_page_info(zone, order, &info);
Mel Gorman56de7262010-05-24 14:32:30 -07002055 index = __fragmentation_index(order, &info);
Mel Gormanf1a5ab12010-05-24 14:32:26 -07002056 seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
2057 }
2058
2059 seq_putc(m, '\n');
2060}
2061
2062/*
2063 * Display fragmentation index for orders that allocations would fail for
2064 */
2065static int extfrag_show(struct seq_file *m, void *arg)
2066{
2067 pg_data_t *pgdat = (pg_data_t *)arg;
2068
Vinayak Menon727c0802017-07-10 15:49:17 -07002069 walk_zones_in_node(m, pgdat, true, false, extfrag_show_print);
Mel Gormanf1a5ab12010-05-24 14:32:26 -07002070
2071 return 0;
2072}
2073
2074static const struct seq_operations extfrag_op = {
2075 .start = frag_start,
2076 .next = frag_next,
2077 .stop = frag_stop,
2078 .show = extfrag_show,
2079};
2080
2081static int extfrag_open(struct inode *inode, struct file *file)
2082{
2083 return seq_open(file, &extfrag_op);
2084}
2085
2086static const struct file_operations extfrag_file_ops = {
2087 .open = extfrag_open,
2088 .read = seq_read,
2089 .llseek = seq_lseek,
2090 .release = seq_release,
2091};
2092
Mel Gormand7a57522010-05-24 14:32:25 -07002093static int __init extfrag_debug_init(void)
2094{
Sasikantha babubde8bd82012-05-29 15:06:22 -07002095 struct dentry *extfrag_debug_root;
2096
Mel Gormand7a57522010-05-24 14:32:25 -07002097 extfrag_debug_root = debugfs_create_dir("extfrag", NULL);
2098 if (!extfrag_debug_root)
2099 return -ENOMEM;
2100
2101 if (!debugfs_create_file("unusable_index", 0444,
2102 extfrag_debug_root, NULL, &unusable_file_ops))
Sasikantha babubde8bd82012-05-29 15:06:22 -07002103 goto fail;
Mel Gormand7a57522010-05-24 14:32:25 -07002104
Mel Gormanf1a5ab12010-05-24 14:32:26 -07002105 if (!debugfs_create_file("extfrag_index", 0444,
2106 extfrag_debug_root, NULL, &extfrag_file_ops))
Sasikantha babubde8bd82012-05-29 15:06:22 -07002107 goto fail;
Mel Gormanf1a5ab12010-05-24 14:32:26 -07002108
Mel Gormand7a57522010-05-24 14:32:25 -07002109 return 0;
Sasikantha babubde8bd82012-05-29 15:06:22 -07002110fail:
2111 debugfs_remove_recursive(extfrag_debug_root);
2112 return -ENOMEM;
Mel Gormand7a57522010-05-24 14:32:25 -07002113}
2114
2115module_init(extfrag_debug_init);
2116#endif