blob: 527ae727d547e5cf5a8fc2349478b8d258560a2b [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
Kemi Wang1d90ca82017-09-08 16:12:52 -070033#define NUMA_STATS_THRESHOLD (U16_MAX - 2)
34
Christoph Lameterf8891e52006-06-30 01:55:45 -070035#ifdef CONFIG_VM_EVENT_COUNTERS
36DEFINE_PER_CPU(struct vm_event_state, vm_event_states) = {{0}};
37EXPORT_PER_CPU_SYMBOL(vm_event_states);
38
Minchan Kim31f961a2010-08-09 17:18:59 -070039static void sum_vm_events(unsigned long *ret)
Christoph Lameterf8891e52006-06-30 01:55:45 -070040{
Christoph Lameter9eccf2a2008-02-04 22:29:22 -080041 int cpu;
Christoph Lameterf8891e52006-06-30 01:55:45 -070042 int i;
43
44 memset(ret, 0, NR_VM_EVENT_ITEMS * sizeof(unsigned long));
45
Minchan Kim31f961a2010-08-09 17:18:59 -070046 for_each_online_cpu(cpu) {
Christoph Lameterf8891e52006-06-30 01:55:45 -070047 struct vm_event_state *this = &per_cpu(vm_event_states, cpu);
48
Christoph Lameterf8891e52006-06-30 01:55:45 -070049 for (i = 0; i < NR_VM_EVENT_ITEMS; i++)
50 ret[i] += this->event[i];
51 }
52}
53
54/*
55 * Accumulate the vm event counters across all CPUs.
56 * The result is unavoidably approximate - it can change
57 * during and after execution of this function.
58*/
59void all_vm_events(unsigned long *ret)
60{
KOSAKI Motohirob5be1132008-05-12 14:02:06 -070061 get_online_cpus();
Minchan Kim31f961a2010-08-09 17:18:59 -070062 sum_vm_events(ret);
KOSAKI Motohirob5be1132008-05-12 14:02:06 -070063 put_online_cpus();
Christoph Lameterf8891e52006-06-30 01:55:45 -070064}
Heiko Carstens32dd66f2006-07-10 04:44:31 -070065EXPORT_SYMBOL_GPL(all_vm_events);
Christoph Lameterf8891e52006-06-30 01:55:45 -070066
Christoph Lameterf8891e52006-06-30 01:55:45 -070067/*
68 * Fold the foreign cpu events into our own.
69 *
70 * This is adding to the events on one processor
71 * but keeps the global counts constant.
72 */
73void vm_events_fold_cpu(int cpu)
74{
75 struct vm_event_state *fold_state = &per_cpu(vm_event_states, cpu);
76 int i;
77
78 for (i = 0; i < NR_VM_EVENT_ITEMS; i++) {
79 count_vm_events(i, fold_state->event[i]);
80 fold_state->event[i] = 0;
81 }
82}
Christoph Lameterf8891e52006-06-30 01:55:45 -070083
84#endif /* CONFIG_VM_EVENT_COUNTERS */
85
Christoph Lameter2244b952006-06-30 01:55:33 -070086/*
87 * Manage combined zone based / global counters
88 *
89 * vm_stat contains the global counters
90 */
Mel Gorman75ef7182016-07-28 15:45:24 -070091atomic_long_t vm_zone_stat[NR_VM_ZONE_STAT_ITEMS] __cacheline_aligned_in_smp;
Kemi Wang3a321d22017-09-08 16:12:48 -070092atomic_long_t vm_numa_stat[NR_VM_NUMA_STAT_ITEMS] __cacheline_aligned_in_smp;
Mel Gorman75ef7182016-07-28 15:45:24 -070093atomic_long_t vm_node_stat[NR_VM_NODE_STAT_ITEMS] __cacheline_aligned_in_smp;
94EXPORT_SYMBOL(vm_zone_stat);
Kemi Wang3a321d22017-09-08 16:12:48 -070095EXPORT_SYMBOL(vm_numa_stat);
Mel Gorman75ef7182016-07-28 15:45:24 -070096EXPORT_SYMBOL(vm_node_stat);
Christoph Lameter2244b952006-06-30 01:55:33 -070097
98#ifdef CONFIG_SMP
99
Mel Gormanb44129b2011-01-13 15:45:43 -0800100int calculate_pressure_threshold(struct zone *zone)
Mel Gorman88f5acf2011-01-13 15:45:41 -0800101{
102 int threshold;
103 int watermark_distance;
104
105 /*
106 * As vmstats are not up to date, there is drift between the estimated
107 * and real values. For high thresholds and a high number of CPUs, it
108 * is possible for the min watermark to be breached while the estimated
109 * value looks fine. The pressure threshold is a reduced value such
110 * that even the maximum amount of drift will not accidentally breach
111 * the min watermark
112 */
113 watermark_distance = low_wmark_pages(zone) - min_wmark_pages(zone);
114 threshold = max(1, (int)(watermark_distance / num_online_cpus()));
115
116 /*
117 * Maximum threshold is 125
118 */
119 threshold = min(125, threshold);
120
121 return threshold;
122}
123
Mel Gormanb44129b2011-01-13 15:45:43 -0800124int calculate_normal_threshold(struct zone *zone)
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700125{
126 int threshold;
127 int mem; /* memory in 128 MB units */
128
129 /*
130 * The threshold scales with the number of processors and the amount
131 * of memory per zone. More memory means that we can defer updates for
132 * longer, more processors could lead to more contention.
133 * fls() is used to have a cheap way of logarithmic scaling.
134 *
135 * Some sample thresholds:
136 *
137 * Threshold Processors (fls) Zonesize fls(mem+1)
138 * ------------------------------------------------------------------
139 * 8 1 1 0.9-1 GB 4
140 * 16 2 2 0.9-1 GB 4
141 * 20 2 2 1-2 GB 5
142 * 24 2 2 2-4 GB 6
143 * 28 2 2 4-8 GB 7
144 * 32 2 2 8-16 GB 8
145 * 4 2 2 <128M 1
146 * 30 4 3 2-4 GB 5
147 * 48 4 3 8-16 GB 8
148 * 32 8 4 1-2 GB 4
149 * 32 8 4 0.9-1GB 4
150 * 10 16 5 <128M 1
151 * 40 16 5 900M 4
152 * 70 64 7 2-4 GB 5
153 * 84 64 7 4-8 GB 6
154 * 108 512 9 4-8 GB 6
155 * 125 1024 10 8-16 GB 8
156 * 125 1024 10 16-32 GB 9
157 */
158
Jiang Liub40da042013-02-22 16:33:52 -0800159 mem = zone->managed_pages >> (27 - PAGE_SHIFT);
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700160
161 threshold = 2 * fls(num_online_cpus()) * (1 + fls(mem));
162
163 /*
164 * Maximum threshold is 125
165 */
166 threshold = min(125, threshold);
167
168 return threshold;
169}
Christoph Lameter2244b952006-06-30 01:55:33 -0700170
171/*
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700172 * Refresh the thresholds for each zone.
Christoph Lameter2244b952006-06-30 01:55:33 -0700173 */
KOSAKI Motohiroa6cccdc2011-05-24 17:11:33 -0700174void refresh_zone_stat_thresholds(void)
Christoph Lameter2244b952006-06-30 01:55:33 -0700175{
Mel Gorman75ef7182016-07-28 15:45:24 -0700176 struct pglist_data *pgdat;
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700177 struct zone *zone;
178 int cpu;
179 int threshold;
180
Mel Gorman75ef7182016-07-28 15:45:24 -0700181 /* Zero current pgdat thresholds */
182 for_each_online_pgdat(pgdat) {
183 for_each_online_cpu(cpu) {
184 per_cpu_ptr(pgdat->per_cpu_nodestats, cpu)->stat_threshold = 0;
185 }
186 }
187
KOSAKI Motohiroee99c712009-03-31 15:19:31 -0700188 for_each_populated_zone(zone) {
Mel Gorman75ef7182016-07-28 15:45:24 -0700189 struct pglist_data *pgdat = zone->zone_pgdat;
Christoph Lameteraa454842010-09-09 16:38:17 -0700190 unsigned long max_drift, tolerate_drift;
191
Mel Gormanb44129b2011-01-13 15:45:43 -0800192 threshold = calculate_normal_threshold(zone);
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700193
Mel Gorman75ef7182016-07-28 15:45:24 -0700194 for_each_online_cpu(cpu) {
195 int pgdat_threshold;
196
Christoph Lameter99dcc3e2010-01-05 15:34:51 +0900197 per_cpu_ptr(zone->pageset, cpu)->stat_threshold
198 = threshold;
Kemi Wang1d90ca82017-09-08 16:12:52 -0700199
Mel Gorman75ef7182016-07-28 15:45:24 -0700200 /* Base nodestat threshold on the largest populated zone. */
201 pgdat_threshold = per_cpu_ptr(pgdat->per_cpu_nodestats, cpu)->stat_threshold;
202 per_cpu_ptr(pgdat->per_cpu_nodestats, cpu)->stat_threshold
203 = max(threshold, pgdat_threshold);
204 }
205
Christoph Lameteraa454842010-09-09 16:38:17 -0700206 /*
207 * Only set percpu_drift_mark if there is a danger that
208 * NR_FREE_PAGES reports the low watermark is ok when in fact
209 * the min watermark could be breached by an allocation
210 */
211 tolerate_drift = low_wmark_pages(zone) - min_wmark_pages(zone);
212 max_drift = num_online_cpus() * threshold;
213 if (max_drift > tolerate_drift)
214 zone->percpu_drift_mark = high_wmark_pages(zone) +
215 max_drift;
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700216 }
Christoph Lameter2244b952006-06-30 01:55:33 -0700217}
218
Mel Gormanb44129b2011-01-13 15:45:43 -0800219void set_pgdat_percpu_threshold(pg_data_t *pgdat,
220 int (*calculate_pressure)(struct zone *))
Mel Gorman88f5acf2011-01-13 15:45:41 -0800221{
222 struct zone *zone;
223 int cpu;
224 int threshold;
225 int i;
226
Mel Gorman88f5acf2011-01-13 15:45:41 -0800227 for (i = 0; i < pgdat->nr_zones; i++) {
228 zone = &pgdat->node_zones[i];
229 if (!zone->percpu_drift_mark)
230 continue;
231
Mel Gormanb44129b2011-01-13 15:45:43 -0800232 threshold = (*calculate_pressure)(zone);
Kemi Wang1d90ca82017-09-08 16:12:52 -0700233 for_each_online_cpu(cpu)
Mel Gorman88f5acf2011-01-13 15:45:41 -0800234 per_cpu_ptr(zone->pageset, cpu)->stat_threshold
235 = threshold;
236 }
Mel Gorman88f5acf2011-01-13 15:45:41 -0800237}
238
Christoph Lameter2244b952006-06-30 01:55:33 -0700239/*
Jianyu Zhanbea04b02014-06-04 16:09:51 -0700240 * For use when we know that interrupts are disabled,
241 * or when we know that preemption is disabled and that
242 * particular counter cannot be updated from interrupt context.
Christoph Lameter2244b952006-06-30 01:55:33 -0700243 */
244void __mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
Heiko Carstens6cdb18a2015-12-29 14:54:32 -0800245 long delta)
Christoph Lameter2244b952006-06-30 01:55:33 -0700246{
Christoph Lameter12938a92010-12-06 11:16:20 -0600247 struct per_cpu_pageset __percpu *pcp = zone->pageset;
248 s8 __percpu *p = pcp->vm_stat_diff + item;
Christoph Lameter2244b952006-06-30 01:55:33 -0700249 long x;
Christoph Lameter12938a92010-12-06 11:16:20 -0600250 long t;
Christoph Lameter2244b952006-06-30 01:55:33 -0700251
Christoph Lameter12938a92010-12-06 11:16:20 -0600252 x = delta + __this_cpu_read(*p);
Christoph Lameter2244b952006-06-30 01:55:33 -0700253
Christoph Lameter12938a92010-12-06 11:16:20 -0600254 t = __this_cpu_read(pcp->stat_threshold);
255
256 if (unlikely(x > t || x < -t)) {
Christoph Lameter2244b952006-06-30 01:55:33 -0700257 zone_page_state_add(x, zone, item);
258 x = 0;
259 }
Christoph Lameter12938a92010-12-06 11:16:20 -0600260 __this_cpu_write(*p, x);
Christoph Lameter2244b952006-06-30 01:55:33 -0700261}
262EXPORT_SYMBOL(__mod_zone_page_state);
263
Mel Gorman75ef7182016-07-28 15:45:24 -0700264void __mod_node_page_state(struct pglist_data *pgdat, enum node_stat_item item,
265 long delta)
266{
267 struct per_cpu_nodestat __percpu *pcp = pgdat->per_cpu_nodestats;
268 s8 __percpu *p = pcp->vm_node_stat_diff + item;
269 long x;
270 long t;
271
272 x = delta + __this_cpu_read(*p);
273
274 t = __this_cpu_read(pcp->stat_threshold);
275
276 if (unlikely(x > t || x < -t)) {
277 node_page_state_add(x, pgdat, item);
278 x = 0;
279 }
280 __this_cpu_write(*p, x);
281}
282EXPORT_SYMBOL(__mod_node_page_state);
283
Christoph Lameter2244b952006-06-30 01:55:33 -0700284/*
Christoph Lameter2244b952006-06-30 01:55:33 -0700285 * Optimized increment and decrement functions.
286 *
287 * These are only for a single page and therefore can take a struct page *
288 * argument instead of struct zone *. This allows the inclusion of the code
289 * generated for page_zone(page) into the optimized functions.
290 *
291 * No overflow check is necessary and therefore the differential can be
292 * incremented or decremented in place which may allow the compilers to
293 * generate better code.
Christoph Lameter2244b952006-06-30 01:55:33 -0700294 * The increment or decrement is known and therefore one boundary check can
295 * be omitted.
296 *
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700297 * NOTE: These functions are very performance sensitive. Change only
298 * with care.
299 *
Christoph Lameter2244b952006-06-30 01:55:33 -0700300 * Some processors have inc/dec instructions that are atomic vs an interrupt.
301 * However, the code must first determine the differential location in a zone
302 * based on the processor number and then inc/dec the counter. There is no
303 * guarantee without disabling preemption that the processor will not change
304 * in between and therefore the atomicity vs. interrupt cannot be exploited
305 * in a useful way here.
306 */
Christoph Lameterc8785382007-02-10 01:43:01 -0800307void __inc_zone_state(struct zone *zone, enum zone_stat_item item)
Christoph Lameter2244b952006-06-30 01:55:33 -0700308{
Christoph Lameter12938a92010-12-06 11:16:20 -0600309 struct per_cpu_pageset __percpu *pcp = zone->pageset;
310 s8 __percpu *p = pcp->vm_stat_diff + item;
311 s8 v, t;
Christoph Lameter2244b952006-06-30 01:55:33 -0700312
Christoph Lameter908ee0f2010-12-06 11:40:02 -0600313 v = __this_cpu_inc_return(*p);
Christoph Lameter12938a92010-12-06 11:16:20 -0600314 t = __this_cpu_read(pcp->stat_threshold);
315 if (unlikely(v > t)) {
316 s8 overstep = t >> 1;
Christoph Lameter2244b952006-06-30 01:55:33 -0700317
Christoph Lameter12938a92010-12-06 11:16:20 -0600318 zone_page_state_add(v + overstep, zone, item);
319 __this_cpu_write(*p, -overstep);
Christoph Lameter2244b952006-06-30 01:55:33 -0700320 }
321}
Christoph Lameterca889e62006-06-30 01:55:44 -0700322
Mel Gorman75ef7182016-07-28 15:45:24 -0700323void __inc_node_state(struct pglist_data *pgdat, enum node_stat_item item)
324{
325 struct per_cpu_nodestat __percpu *pcp = pgdat->per_cpu_nodestats;
326 s8 __percpu *p = pcp->vm_node_stat_diff + item;
327 s8 v, t;
328
329 v = __this_cpu_inc_return(*p);
330 t = __this_cpu_read(pcp->stat_threshold);
331 if (unlikely(v > t)) {
332 s8 overstep = t >> 1;
333
334 node_page_state_add(v + overstep, pgdat, item);
335 __this_cpu_write(*p, -overstep);
336 }
337}
338
Christoph Lameterca889e62006-06-30 01:55:44 -0700339void __inc_zone_page_state(struct page *page, enum zone_stat_item item)
340{
341 __inc_zone_state(page_zone(page), item);
342}
Christoph Lameter2244b952006-06-30 01:55:33 -0700343EXPORT_SYMBOL(__inc_zone_page_state);
344
Mel Gorman75ef7182016-07-28 15:45:24 -0700345void __inc_node_page_state(struct page *page, enum node_stat_item item)
346{
347 __inc_node_state(page_pgdat(page), item);
348}
349EXPORT_SYMBOL(__inc_node_page_state);
350
Christoph Lameterc8785382007-02-10 01:43:01 -0800351void __dec_zone_state(struct zone *zone, enum zone_stat_item item)
Christoph Lameter2244b952006-06-30 01:55:33 -0700352{
Christoph Lameter12938a92010-12-06 11:16:20 -0600353 struct per_cpu_pageset __percpu *pcp = zone->pageset;
354 s8 __percpu *p = pcp->vm_stat_diff + item;
355 s8 v, t;
Christoph Lameter2244b952006-06-30 01:55:33 -0700356
Christoph Lameter908ee0f2010-12-06 11:40:02 -0600357 v = __this_cpu_dec_return(*p);
Christoph Lameter12938a92010-12-06 11:16:20 -0600358 t = __this_cpu_read(pcp->stat_threshold);
359 if (unlikely(v < - t)) {
360 s8 overstep = t >> 1;
Christoph Lameter2244b952006-06-30 01:55:33 -0700361
Christoph Lameter12938a92010-12-06 11:16:20 -0600362 zone_page_state_add(v - overstep, zone, item);
363 __this_cpu_write(*p, overstep);
Christoph Lameter2244b952006-06-30 01:55:33 -0700364 }
365}
Christoph Lameterc8785382007-02-10 01:43:01 -0800366
Mel Gorman75ef7182016-07-28 15:45:24 -0700367void __dec_node_state(struct pglist_data *pgdat, enum node_stat_item item)
368{
369 struct per_cpu_nodestat __percpu *pcp = pgdat->per_cpu_nodestats;
370 s8 __percpu *p = pcp->vm_node_stat_diff + item;
371 s8 v, t;
372
373 v = __this_cpu_dec_return(*p);
374 t = __this_cpu_read(pcp->stat_threshold);
375 if (unlikely(v < - t)) {
376 s8 overstep = t >> 1;
377
378 node_page_state_add(v - overstep, pgdat, item);
379 __this_cpu_write(*p, overstep);
380 }
381}
382
Christoph Lameterc8785382007-02-10 01:43:01 -0800383void __dec_zone_page_state(struct page *page, enum zone_stat_item item)
384{
385 __dec_zone_state(page_zone(page), item);
386}
Christoph Lameter2244b952006-06-30 01:55:33 -0700387EXPORT_SYMBOL(__dec_zone_page_state);
388
Mel Gorman75ef7182016-07-28 15:45:24 -0700389void __dec_node_page_state(struct page *page, enum node_stat_item item)
390{
391 __dec_node_state(page_pgdat(page), item);
392}
393EXPORT_SYMBOL(__dec_node_page_state);
394
Heiko Carstens41561532012-01-12 17:17:30 -0800395#ifdef CONFIG_HAVE_CMPXCHG_LOCAL
Christoph Lameter7c839122010-12-14 10:28:46 -0600396/*
397 * If we have cmpxchg_local support then we do not need to incur the overhead
398 * that comes with local_irq_save/restore if we use this_cpu_cmpxchg.
399 *
400 * mod_state() modifies the zone counter state through atomic per cpu
401 * operations.
402 *
403 * Overstep mode specifies how overstep should handled:
404 * 0 No overstepping
405 * 1 Overstepping half of threshold
406 * -1 Overstepping minus half of threshold
407*/
Mel Gorman75ef7182016-07-28 15:45:24 -0700408static inline void mod_zone_state(struct zone *zone,
409 enum zone_stat_item item, long delta, int overstep_mode)
Christoph Lameter7c839122010-12-14 10:28:46 -0600410{
411 struct per_cpu_pageset __percpu *pcp = zone->pageset;
412 s8 __percpu *p = pcp->vm_stat_diff + item;
413 long o, n, t, z;
414
415 do {
416 z = 0; /* overflow to zone counters */
417
418 /*
419 * The fetching of the stat_threshold is racy. We may apply
420 * a counter threshold to the wrong the cpu if we get
Christoph Lameterd3bc2362011-04-14 15:21:58 -0700421 * rescheduled while executing here. However, the next
422 * counter update will apply the threshold again and
423 * therefore bring the counter under the threshold again.
424 *
425 * Most of the time the thresholds are the same anyways
426 * for all cpus in a zone.
Christoph Lameter7c839122010-12-14 10:28:46 -0600427 */
428 t = this_cpu_read(pcp->stat_threshold);
429
430 o = this_cpu_read(*p);
431 n = delta + o;
432
433 if (n > t || n < -t) {
434 int os = overstep_mode * (t >> 1) ;
435
436 /* Overflow must be added to zone counters */
437 z = n + os;
438 n = -os;
439 }
440 } while (this_cpu_cmpxchg(*p, o, n) != o);
441
442 if (z)
443 zone_page_state_add(z, zone, item);
444}
445
446void mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
Heiko Carstens6cdb18a2015-12-29 14:54:32 -0800447 long delta)
Christoph Lameter7c839122010-12-14 10:28:46 -0600448{
Mel Gorman75ef7182016-07-28 15:45:24 -0700449 mod_zone_state(zone, item, delta, 0);
Christoph Lameter7c839122010-12-14 10:28:46 -0600450}
451EXPORT_SYMBOL(mod_zone_page_state);
452
Christoph Lameter7c839122010-12-14 10:28:46 -0600453void inc_zone_page_state(struct page *page, enum zone_stat_item item)
454{
Mel Gorman75ef7182016-07-28 15:45:24 -0700455 mod_zone_state(page_zone(page), item, 1, 1);
Christoph Lameter7c839122010-12-14 10:28:46 -0600456}
457EXPORT_SYMBOL(inc_zone_page_state);
458
459void dec_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(dec_zone_page_state);
Mel Gorman75ef7182016-07-28 15:45:24 -0700464
465static inline void mod_node_state(struct pglist_data *pgdat,
466 enum node_stat_item item, int delta, int overstep_mode)
467{
468 struct per_cpu_nodestat __percpu *pcp = pgdat->per_cpu_nodestats;
469 s8 __percpu *p = pcp->vm_node_stat_diff + item;
470 long o, n, t, z;
471
472 do {
473 z = 0; /* overflow to node counters */
474
475 /*
476 * The fetching of the stat_threshold is racy. We may apply
477 * a counter threshold to the wrong the cpu if we get
478 * rescheduled while executing here. However, the next
479 * counter update will apply the threshold again and
480 * therefore bring the counter under the threshold again.
481 *
482 * Most of the time the thresholds are the same anyways
483 * for all cpus in a node.
484 */
485 t = this_cpu_read(pcp->stat_threshold);
486
487 o = this_cpu_read(*p);
488 n = delta + o;
489
490 if (n > t || n < -t) {
491 int os = overstep_mode * (t >> 1) ;
492
493 /* Overflow must be added to node counters */
494 z = n + os;
495 n = -os;
496 }
497 } while (this_cpu_cmpxchg(*p, o, n) != o);
498
499 if (z)
500 node_page_state_add(z, pgdat, item);
501}
502
503void mod_node_page_state(struct pglist_data *pgdat, enum node_stat_item item,
504 long delta)
505{
506 mod_node_state(pgdat, item, delta, 0);
507}
508EXPORT_SYMBOL(mod_node_page_state);
509
510void inc_node_state(struct pglist_data *pgdat, enum node_stat_item item)
511{
512 mod_node_state(pgdat, item, 1, 1);
513}
514
515void inc_node_page_state(struct page *page, enum node_stat_item item)
516{
517 mod_node_state(page_pgdat(page), item, 1, 1);
518}
519EXPORT_SYMBOL(inc_node_page_state);
520
521void dec_node_page_state(struct page *page, enum node_stat_item item)
522{
523 mod_node_state(page_pgdat(page), item, -1, -1);
524}
525EXPORT_SYMBOL(dec_node_page_state);
Christoph Lameter7c839122010-12-14 10:28:46 -0600526#else
527/*
528 * Use interrupt disable to serialize counter updates
529 */
530void mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
Heiko Carstens6cdb18a2015-12-29 14:54:32 -0800531 long delta)
Christoph Lameter7c839122010-12-14 10:28:46 -0600532{
533 unsigned long flags;
534
535 local_irq_save(flags);
536 __mod_zone_page_state(zone, item, delta);
537 local_irq_restore(flags);
538}
539EXPORT_SYMBOL(mod_zone_page_state);
540
Christoph Lameter2244b952006-06-30 01:55:33 -0700541void inc_zone_page_state(struct page *page, enum zone_stat_item item)
542{
543 unsigned long flags;
544 struct zone *zone;
Christoph Lameter2244b952006-06-30 01:55:33 -0700545
546 zone = page_zone(page);
547 local_irq_save(flags);
Christoph Lameterca889e62006-06-30 01:55:44 -0700548 __inc_zone_state(zone, item);
Christoph Lameter2244b952006-06-30 01:55:33 -0700549 local_irq_restore(flags);
550}
551EXPORT_SYMBOL(inc_zone_page_state);
552
553void dec_zone_page_state(struct page *page, enum zone_stat_item item)
554{
555 unsigned long flags;
Christoph Lameter2244b952006-06-30 01:55:33 -0700556
Christoph Lameter2244b952006-06-30 01:55:33 -0700557 local_irq_save(flags);
Christoph Lametera302eb42006-08-31 21:27:34 -0700558 __dec_zone_page_state(page, item);
Christoph Lameter2244b952006-06-30 01:55:33 -0700559 local_irq_restore(flags);
560}
561EXPORT_SYMBOL(dec_zone_page_state);
562
Mel Gorman75ef7182016-07-28 15:45:24 -0700563void inc_node_state(struct pglist_data *pgdat, enum node_stat_item item)
564{
565 unsigned long flags;
566
567 local_irq_save(flags);
568 __inc_node_state(pgdat, item);
569 local_irq_restore(flags);
570}
571EXPORT_SYMBOL(inc_node_state);
572
573void mod_node_page_state(struct pglist_data *pgdat, enum node_stat_item item,
574 long delta)
575{
576 unsigned long flags;
577
578 local_irq_save(flags);
579 __mod_node_page_state(pgdat, item, delta);
580 local_irq_restore(flags);
581}
582EXPORT_SYMBOL(mod_node_page_state);
583
584void inc_node_page_state(struct page *page, enum node_stat_item item)
585{
586 unsigned long flags;
587 struct pglist_data *pgdat;
588
589 pgdat = page_pgdat(page);
590 local_irq_save(flags);
591 __inc_node_state(pgdat, item);
592 local_irq_restore(flags);
593}
594EXPORT_SYMBOL(inc_node_page_state);
595
596void dec_node_page_state(struct page *page, enum node_stat_item item)
597{
598 unsigned long flags;
599
600 local_irq_save(flags);
601 __dec_node_page_state(page, item);
602 local_irq_restore(flags);
603}
604EXPORT_SYMBOL(dec_node_page_state);
605#endif
Christoph Lameter7cc36bb2014-10-09 15:29:43 -0700606
607/*
608 * Fold a differential into the global counters.
609 * Returns the number of counters updated.
610 */
Kemi Wang3a321d22017-09-08 16:12:48 -0700611#ifdef CONFIG_NUMA
612static int fold_diff(int *zone_diff, int *numa_diff, int *node_diff)
613{
614 int i;
615 int changes = 0;
616
617 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
618 if (zone_diff[i]) {
619 atomic_long_add(zone_diff[i], &vm_zone_stat[i]);
620 changes++;
621 }
622
623 for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++)
624 if (numa_diff[i]) {
625 atomic_long_add(numa_diff[i], &vm_numa_stat[i]);
626 changes++;
627 }
628
629 for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++)
630 if (node_diff[i]) {
631 atomic_long_add(node_diff[i], &vm_node_stat[i]);
632 changes++;
633 }
634 return changes;
635}
636#else
Mel Gorman75ef7182016-07-28 15:45:24 -0700637static int fold_diff(int *zone_diff, int *node_diff)
Christoph Lameter4edb0742013-09-11 14:21:31 -0700638{
639 int i;
Christoph Lameter7cc36bb2014-10-09 15:29:43 -0700640 int changes = 0;
Christoph Lameter4edb0742013-09-11 14:21:31 -0700641
642 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
Mel Gorman75ef7182016-07-28 15:45:24 -0700643 if (zone_diff[i]) {
644 atomic_long_add(zone_diff[i], &vm_zone_stat[i]);
645 changes++;
646 }
647
648 for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++)
649 if (node_diff[i]) {
650 atomic_long_add(node_diff[i], &vm_node_stat[i]);
Christoph Lameter7cc36bb2014-10-09 15:29:43 -0700651 changes++;
652 }
653 return changes;
Christoph Lameter4edb0742013-09-11 14:21:31 -0700654}
Kemi Wang3a321d22017-09-08 16:12:48 -0700655#endif /* CONFIG_NUMA */
Christoph Lameter4edb0742013-09-11 14:21:31 -0700656
Christoph Lameter2244b952006-06-30 01:55:33 -0700657/*
Christoph Lameter2bb921e2013-09-11 14:21:30 -0700658 * Update the zone counters for the current cpu.
Christoph Lametera7f75e22008-02-04 22:29:16 -0800659 *
Christoph Lameter4037d452007-05-09 02:35:14 -0700660 * Note that refresh_cpu_vm_stats strives to only access
661 * node local memory. The per cpu pagesets on remote zones are placed
662 * in the memory local to the processor using that pageset. So the
663 * loop over all zones will access a series of cachelines local to
664 * the processor.
665 *
666 * The call to zone_page_state_add updates the cachelines with the
667 * statistics in the remote zone struct as well as the global cachelines
668 * with the global counters. These could cause remote node cache line
669 * bouncing and will have to be only done when necessary.
Christoph Lameter7cc36bb2014-10-09 15:29:43 -0700670 *
671 * The function returns the number of global counters updated.
Christoph Lameter2244b952006-06-30 01:55:33 -0700672 */
Christoph Lameter0eb77e92016-01-14 15:21:40 -0800673static int refresh_cpu_vm_stats(bool do_pagesets)
Christoph Lameter2244b952006-06-30 01:55:33 -0700674{
Mel Gorman75ef7182016-07-28 15:45:24 -0700675 struct pglist_data *pgdat;
Christoph Lameter2244b952006-06-30 01:55:33 -0700676 struct zone *zone;
677 int i;
Mel Gorman75ef7182016-07-28 15:45:24 -0700678 int global_zone_diff[NR_VM_ZONE_STAT_ITEMS] = { 0, };
Kemi Wang3a321d22017-09-08 16:12:48 -0700679#ifdef CONFIG_NUMA
680 int global_numa_diff[NR_VM_NUMA_STAT_ITEMS] = { 0, };
681#endif
Mel Gorman75ef7182016-07-28 15:45:24 -0700682 int global_node_diff[NR_VM_NODE_STAT_ITEMS] = { 0, };
Christoph Lameter7cc36bb2014-10-09 15:29:43 -0700683 int changes = 0;
Christoph Lameter2244b952006-06-30 01:55:33 -0700684
KOSAKI Motohiroee99c712009-03-31 15:19:31 -0700685 for_each_populated_zone(zone) {
Christoph Lameterfbc2edb2013-09-11 14:21:32 -0700686 struct per_cpu_pageset __percpu *p = zone->pageset;
Christoph Lameter2244b952006-06-30 01:55:33 -0700687
Christoph Lameterfbc2edb2013-09-11 14:21:32 -0700688 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++) {
689 int v;
Christoph Lameter2244b952006-06-30 01:55:33 -0700690
Christoph Lameterfbc2edb2013-09-11 14:21:32 -0700691 v = this_cpu_xchg(p->vm_stat_diff[i], 0);
692 if (v) {
Christoph Lametera7f75e22008-02-04 22:29:16 -0800693
Christoph Lametera7f75e22008-02-04 22:29:16 -0800694 atomic_long_add(v, &zone->vm_stat[i]);
Mel Gorman75ef7182016-07-28 15:45:24 -0700695 global_zone_diff[i] += v;
Christoph Lameter4037d452007-05-09 02:35:14 -0700696#ifdef CONFIG_NUMA
697 /* 3 seconds idle till flush */
Christoph Lameterfbc2edb2013-09-11 14:21:32 -0700698 __this_cpu_write(p->expire, 3);
Christoph Lameter4037d452007-05-09 02:35:14 -0700699#endif
Christoph Lameter2244b952006-06-30 01:55:33 -0700700 }
Christoph Lameterfbc2edb2013-09-11 14:21:32 -0700701 }
Christoph Lameter4037d452007-05-09 02:35:14 -0700702#ifdef CONFIG_NUMA
Kemi Wang3a321d22017-09-08 16:12:48 -0700703 for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++) {
704 int v;
705
706 v = this_cpu_xchg(p->vm_numa_stat_diff[i], 0);
707 if (v) {
708
709 atomic_long_add(v, &zone->vm_numa_stat[i]);
710 global_numa_diff[i] += v;
711 __this_cpu_write(p->expire, 3);
712 }
713 }
714
Christoph Lameter0eb77e92016-01-14 15:21:40 -0800715 if (do_pagesets) {
716 cond_resched();
717 /*
718 * Deal with draining the remote pageset of this
719 * processor
720 *
721 * Check if there are pages remaining in this pageset
722 * if not then there is nothing to expire.
723 */
724 if (!__this_cpu_read(p->expire) ||
Christoph Lameterfbc2edb2013-09-11 14:21:32 -0700725 !__this_cpu_read(p->pcp.count))
Christoph Lameter0eb77e92016-01-14 15:21:40 -0800726 continue;
Christoph Lameter4037d452007-05-09 02:35:14 -0700727
Christoph Lameter0eb77e92016-01-14 15:21:40 -0800728 /*
729 * We never drain zones local to this processor.
730 */
731 if (zone_to_nid(zone) == numa_node_id()) {
732 __this_cpu_write(p->expire, 0);
733 continue;
734 }
Christoph Lameter4037d452007-05-09 02:35:14 -0700735
Christoph Lameter0eb77e92016-01-14 15:21:40 -0800736 if (__this_cpu_dec_return(p->expire))
737 continue;
Christoph Lameter4037d452007-05-09 02:35:14 -0700738
Christoph Lameter0eb77e92016-01-14 15:21:40 -0800739 if (__this_cpu_read(p->pcp.count)) {
740 drain_zone_pages(zone, this_cpu_ptr(&p->pcp));
741 changes++;
742 }
Christoph Lameter7cc36bb2014-10-09 15:29:43 -0700743 }
Christoph Lameter4037d452007-05-09 02:35:14 -0700744#endif
Christoph Lameter2244b952006-06-30 01:55:33 -0700745 }
Mel Gorman75ef7182016-07-28 15:45:24 -0700746
747 for_each_online_pgdat(pgdat) {
748 struct per_cpu_nodestat __percpu *p = pgdat->per_cpu_nodestats;
749
750 for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++) {
751 int v;
752
753 v = this_cpu_xchg(p->vm_node_stat_diff[i], 0);
754 if (v) {
755 atomic_long_add(v, &pgdat->vm_stat[i]);
756 global_node_diff[i] += v;
757 }
758 }
759 }
760
Kemi Wang3a321d22017-09-08 16:12:48 -0700761#ifdef CONFIG_NUMA
762 changes += fold_diff(global_zone_diff, global_numa_diff,
763 global_node_diff);
764#else
Mel Gorman75ef7182016-07-28 15:45:24 -0700765 changes += fold_diff(global_zone_diff, global_node_diff);
Kemi Wang3a321d22017-09-08 16:12:48 -0700766#endif
Christoph Lameter7cc36bb2014-10-09 15:29:43 -0700767 return changes;
Christoph Lameter2244b952006-06-30 01:55:33 -0700768}
769
Cody P Schafer40f4b1e2013-04-29 15:08:38 -0700770/*
Christoph Lameter2bb921e2013-09-11 14:21:30 -0700771 * Fold the data for an offline cpu into the global array.
772 * There cannot be any access by the offline cpu and therefore
773 * synchronization is simplified.
774 */
775void cpu_vm_stats_fold(int cpu)
776{
Mel Gorman75ef7182016-07-28 15:45:24 -0700777 struct pglist_data *pgdat;
Christoph Lameter2bb921e2013-09-11 14:21:30 -0700778 struct zone *zone;
779 int i;
Mel Gorman75ef7182016-07-28 15:45:24 -0700780 int global_zone_diff[NR_VM_ZONE_STAT_ITEMS] = { 0, };
Kemi Wang3a321d22017-09-08 16:12:48 -0700781#ifdef CONFIG_NUMA
782 int global_numa_diff[NR_VM_NUMA_STAT_ITEMS] = { 0, };
783#endif
Mel Gorman75ef7182016-07-28 15:45:24 -0700784 int global_node_diff[NR_VM_NODE_STAT_ITEMS] = { 0, };
Christoph Lameter2bb921e2013-09-11 14:21:30 -0700785
786 for_each_populated_zone(zone) {
787 struct per_cpu_pageset *p;
788
789 p = per_cpu_ptr(zone->pageset, cpu);
790
791 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
792 if (p->vm_stat_diff[i]) {
793 int v;
794
795 v = p->vm_stat_diff[i];
796 p->vm_stat_diff[i] = 0;
797 atomic_long_add(v, &zone->vm_stat[i]);
Mel Gorman75ef7182016-07-28 15:45:24 -0700798 global_zone_diff[i] += v;
Christoph Lameter2bb921e2013-09-11 14:21:30 -0700799 }
Kemi Wang3a321d22017-09-08 16:12:48 -0700800
801#ifdef CONFIG_NUMA
802 for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++)
803 if (p->vm_numa_stat_diff[i]) {
804 int v;
805
806 v = p->vm_numa_stat_diff[i];
807 p->vm_numa_stat_diff[i] = 0;
808 atomic_long_add(v, &zone->vm_numa_stat[i]);
809 global_numa_diff[i] += v;
810 }
811#endif
Christoph Lameter2bb921e2013-09-11 14:21:30 -0700812 }
813
Mel Gorman75ef7182016-07-28 15:45:24 -0700814 for_each_online_pgdat(pgdat) {
815 struct per_cpu_nodestat *p;
816
817 p = per_cpu_ptr(pgdat->per_cpu_nodestats, cpu);
818
819 for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++)
820 if (p->vm_node_stat_diff[i]) {
821 int v;
822
823 v = p->vm_node_stat_diff[i];
824 p->vm_node_stat_diff[i] = 0;
825 atomic_long_add(v, &pgdat->vm_stat[i]);
826 global_node_diff[i] += v;
827 }
828 }
829
Kemi Wang3a321d22017-09-08 16:12:48 -0700830#ifdef CONFIG_NUMA
831 fold_diff(global_zone_diff, global_numa_diff, global_node_diff);
832#else
Mel Gorman75ef7182016-07-28 15:45:24 -0700833 fold_diff(global_zone_diff, global_node_diff);
Kemi Wang3a321d22017-09-08 16:12:48 -0700834#endif
Christoph Lameter2bb921e2013-09-11 14:21:30 -0700835}
836
837/*
Cody P Schafer40f4b1e2013-04-29 15:08:38 -0700838 * this is only called if !populated_zone(zone), which implies no other users of
839 * pset->vm_stat_diff[] exsist.
840 */
Minchan Kim5a883812012-10-08 16:33:39 -0700841void drain_zonestat(struct zone *zone, struct per_cpu_pageset *pset)
842{
843 int i;
844
845 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
846 if (pset->vm_stat_diff[i]) {
847 int v = pset->vm_stat_diff[i];
848 pset->vm_stat_diff[i] = 0;
849 atomic_long_add(v, &zone->vm_stat[i]);
Mel Gorman75ef7182016-07-28 15:45:24 -0700850 atomic_long_add(v, &vm_zone_stat[i]);
Minchan Kim5a883812012-10-08 16:33:39 -0700851 }
Kemi Wang3a321d22017-09-08 16:12:48 -0700852
853#ifdef CONFIG_NUMA
854 for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++)
855 if (pset->vm_numa_stat_diff[i]) {
856 int v = pset->vm_numa_stat_diff[i];
857
858 pset->vm_numa_stat_diff[i] = 0;
859 atomic_long_add(v, &zone->vm_numa_stat[i]);
860 atomic_long_add(v, &vm_numa_stat[i]);
861 }
862#endif
Minchan Kim5a883812012-10-08 16:33:39 -0700863}
Christoph Lameter2244b952006-06-30 01:55:33 -0700864#endif
865
Christoph Lameterca889e62006-06-30 01:55:44 -0700866#ifdef CONFIG_NUMA
Kemi Wang3a321d22017-09-08 16:12:48 -0700867void __inc_numa_state(struct zone *zone,
868 enum numa_stat_item item)
869{
870 struct per_cpu_pageset __percpu *pcp = zone->pageset;
Kemi Wang1d90ca82017-09-08 16:12:52 -0700871 u16 __percpu *p = pcp->vm_numa_stat_diff + item;
872 u16 v;
Kemi Wang3a321d22017-09-08 16:12:48 -0700873
874 v = __this_cpu_inc_return(*p);
Kemi Wang3a321d22017-09-08 16:12:48 -0700875
Kemi Wang1d90ca82017-09-08 16:12:52 -0700876 if (unlikely(v > NUMA_STATS_THRESHOLD)) {
877 zone_numa_state_add(v, zone, item);
878 __this_cpu_write(*p, 0);
Kemi Wang3a321d22017-09-08 16:12:48 -0700879 }
880}
881
Christoph Lameterca889e62006-06-30 01:55:44 -0700882/*
Mel Gorman75ef7182016-07-28 15:45:24 -0700883 * Determine the per node value of a stat item. This function
884 * is called frequently in a NUMA machine, so try to be as
885 * frugal as possible.
Andrew Mortonc2d42c12015-11-05 18:48:43 -0800886 */
Mel Gorman75ef7182016-07-28 15:45:24 -0700887unsigned long sum_zone_node_page_state(int node,
888 enum zone_stat_item item)
Andrew Mortonc2d42c12015-11-05 18:48:43 -0800889{
890 struct zone *zones = NODE_DATA(node)->node_zones;
Joonsoo Kime87d59f2016-05-19 17:12:29 -0700891 int i;
892 unsigned long count = 0;
Andrew Mortonc2d42c12015-11-05 18:48:43 -0800893
Joonsoo Kime87d59f2016-05-19 17:12:29 -0700894 for (i = 0; i < MAX_NR_ZONES; i++)
895 count += zone_page_state(zones + i, item);
896
897 return count;
Andrew Mortonc2d42c12015-11-05 18:48:43 -0800898}
899
Kemi Wang63803222017-09-08 16:12:55 -0700900/*
901 * Determine the per node value of a numa stat item. To avoid deviation,
902 * the per cpu stat number in vm_numa_stat_diff[] is also included.
903 */
Kemi Wang3a321d22017-09-08 16:12:48 -0700904unsigned long sum_zone_numa_state(int node,
905 enum numa_stat_item item)
906{
907 struct zone *zones = NODE_DATA(node)->node_zones;
908 int i;
909 unsigned long count = 0;
910
911 for (i = 0; i < MAX_NR_ZONES; i++)
Kemi Wang63803222017-09-08 16:12:55 -0700912 count += zone_numa_state_snapshot(zones + i, item);
Kemi Wang3a321d22017-09-08 16:12:48 -0700913
914 return count;
915}
916
Mel Gorman75ef7182016-07-28 15:45:24 -0700917/*
918 * Determine the per node value of a stat item.
919 */
920unsigned long node_page_state(struct pglist_data *pgdat,
921 enum node_stat_item item)
922{
923 long x = atomic_long_read(&pgdat->vm_stat[item]);
924#ifdef CONFIG_SMP
925 if (x < 0)
926 x = 0;
927#endif
928 return x;
929}
Christoph Lameterca889e62006-06-30 01:55:44 -0700930#endif
931
Mel Gormand7a57522010-05-24 14:32:25 -0700932#ifdef CONFIG_COMPACTION
Namhyung Kim36deb0b2010-10-26 14:22:04 -0700933
Mel Gormand7a57522010-05-24 14:32:25 -0700934struct contig_page_info {
935 unsigned long free_pages;
936 unsigned long free_blocks_total;
937 unsigned long free_blocks_suitable;
938};
939
940/*
941 * Calculate the number of free pages in a zone, how many contiguous
942 * pages are free and how many are large enough to satisfy an allocation of
943 * the target size. Note that this function makes no attempt to estimate
944 * how many suitable free blocks there *might* be if MOVABLE pages were
945 * migrated. Calculating that is possible, but expensive and can be
946 * figured out from userspace
947 */
948static void fill_contig_page_info(struct zone *zone,
949 unsigned int suitable_order,
950 struct contig_page_info *info)
951{
952 unsigned int order;
953
954 info->free_pages = 0;
955 info->free_blocks_total = 0;
956 info->free_blocks_suitable = 0;
957
958 for (order = 0; order < MAX_ORDER; order++) {
959 unsigned long blocks;
960
961 /* Count number of free blocks */
962 blocks = zone->free_area[order].nr_free;
963 info->free_blocks_total += blocks;
964
965 /* Count free base pages */
966 info->free_pages += blocks << order;
967
968 /* Count the suitable free blocks */
969 if (order >= suitable_order)
970 info->free_blocks_suitable += blocks <<
971 (order - suitable_order);
972 }
973}
Mel Gormanf1a5ab12010-05-24 14:32:26 -0700974
975/*
976 * A fragmentation index only makes sense if an allocation of a requested
977 * size would fail. If that is true, the fragmentation index indicates
978 * whether external fragmentation or a lack of memory was the problem.
979 * The value can be used to determine if page reclaim or compaction
980 * should be used
981 */
Mel Gorman56de7262010-05-24 14:32:30 -0700982static int __fragmentation_index(unsigned int order, struct contig_page_info *info)
Mel Gormanf1a5ab12010-05-24 14:32:26 -0700983{
984 unsigned long requested = 1UL << order;
985
Wen Yang88d6ac42017-09-06 16:24:06 -0700986 if (WARN_ON_ONCE(order >= MAX_ORDER))
987 return 0;
988
Mel Gormanf1a5ab12010-05-24 14:32:26 -0700989 if (!info->free_blocks_total)
990 return 0;
991
992 /* Fragmentation index only makes sense when a request would fail */
993 if (info->free_blocks_suitable)
994 return -1000;
995
996 /*
997 * Index is between 0 and 1 so return within 3 decimal places
998 *
999 * 0 => allocation would fail due to lack of memory
1000 * 1 => allocation would fail due to fragmentation
1001 */
1002 return 1000 - div_u64( (1000+(div_u64(info->free_pages * 1000ULL, requested))), info->free_blocks_total);
1003}
Mel Gorman56de7262010-05-24 14:32:30 -07001004
1005/* Same as __fragmentation index but allocs contig_page_info on stack */
1006int fragmentation_index(struct zone *zone, unsigned int order)
1007{
1008 struct contig_page_info info;
1009
1010 fill_contig_page_info(zone, order, &info);
1011 return __fragmentation_index(order, &info);
1012}
Mel Gormand7a57522010-05-24 14:32:25 -07001013#endif
1014
David Rientjes0d6617c2011-09-14 16:21:05 -07001015#if defined(CONFIG_PROC_FS) || defined(CONFIG_SYSFS) || defined(CONFIG_NUMA)
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001016#ifdef CONFIG_ZONE_DMA
1017#define TEXT_FOR_DMA(xx) xx "_dma",
1018#else
1019#define TEXT_FOR_DMA(xx)
1020#endif
1021
1022#ifdef CONFIG_ZONE_DMA32
1023#define TEXT_FOR_DMA32(xx) xx "_dma32",
1024#else
1025#define TEXT_FOR_DMA32(xx)
1026#endif
1027
1028#ifdef CONFIG_HIGHMEM
1029#define TEXT_FOR_HIGHMEM(xx) xx "_high",
1030#else
1031#define TEXT_FOR_HIGHMEM(xx)
1032#endif
1033
1034#define TEXTS_FOR_ZONES(xx) TEXT_FOR_DMA(xx) TEXT_FOR_DMA32(xx) xx "_normal", \
1035 TEXT_FOR_HIGHMEM(xx) xx "_movable",
1036
1037const char * const vmstat_text[] = {
Konstantin Khlebnikov09316c02014-10-09 15:29:32 -07001038 /* enum zone_stat_item countes */
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001039 "nr_free_pages",
Minchan Kim71c799f2016-07-28 15:47:26 -07001040 "nr_zone_inactive_anon",
1041 "nr_zone_active_anon",
1042 "nr_zone_inactive_file",
1043 "nr_zone_active_file",
1044 "nr_zone_unevictable",
Mel Gorman5a1c84b2016-07-28 15:47:31 -07001045 "nr_zone_write_pending",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001046 "nr_mlock",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001047 "nr_page_table_pages",
1048 "nr_kernel_stack",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001049 "nr_bounce",
Minchan Kim91537fe2016-07-26 15:24:45 -07001050#if IS_ENABLED(CONFIG_ZSMALLOC)
1051 "nr_zspages",
1052#endif
Kemi Wang3a321d22017-09-08 16:12:48 -07001053 "nr_free_cma",
1054
1055 /* enum numa_stat_item counters */
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001056#ifdef CONFIG_NUMA
1057 "numa_hit",
1058 "numa_miss",
1059 "numa_foreign",
1060 "numa_interleave",
1061 "numa_local",
1062 "numa_other",
1063#endif
Konstantin Khlebnikov09316c02014-10-09 15:29:32 -07001064
Mel Gorman599d0c92016-07-28 15:45:31 -07001065 /* Node-based counters */
1066 "nr_inactive_anon",
1067 "nr_active_anon",
1068 "nr_inactive_file",
1069 "nr_active_file",
1070 "nr_unevictable",
Johannes Weiner385386c2017-07-06 15:40:43 -07001071 "nr_slab_reclaimable",
1072 "nr_slab_unreclaimable",
Mel Gorman599d0c92016-07-28 15:45:31 -07001073 "nr_isolated_anon",
1074 "nr_isolated_file",
Mel Gorman1e6b10852016-07-28 15:46:08 -07001075 "workingset_refault",
1076 "workingset_activate",
1077 "workingset_nodereclaim",
Mel Gorman50658e22016-07-28 15:46:14 -07001078 "nr_anon_pages",
1079 "nr_mapped",
Mel Gorman11fb9982016-07-28 15:46:20 -07001080 "nr_file_pages",
1081 "nr_dirty",
1082 "nr_writeback",
1083 "nr_writeback_temp",
1084 "nr_shmem",
1085 "nr_shmem_hugepages",
1086 "nr_shmem_pmdmapped",
1087 "nr_anon_transparent_hugepages",
1088 "nr_unstable",
Mel Gormanc4a25632016-07-28 15:46:23 -07001089 "nr_vmscan_write",
1090 "nr_vmscan_immediate_reclaim",
1091 "nr_dirtied",
1092 "nr_written",
Roman Gushchind62b8ac2018-05-11 16:01:53 -07001093 "", /* nr_indirectly_reclaimable */
Mel Gorman599d0c92016-07-28 15:45:31 -07001094
Konstantin Khlebnikov09316c02014-10-09 15:29:32 -07001095 /* enum writeback_stat_item counters */
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001096 "nr_dirty_threshold",
1097 "nr_dirty_background_threshold",
1098
1099#ifdef CONFIG_VM_EVENT_COUNTERS
Konstantin Khlebnikov09316c02014-10-09 15:29:32 -07001100 /* enum vm_event_item counters */
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001101 "pgpgin",
1102 "pgpgout",
1103 "pswpin",
1104 "pswpout",
1105
1106 TEXTS_FOR_ZONES("pgalloc")
Mel Gorman7cc30fc2016-07-28 15:46:59 -07001107 TEXTS_FOR_ZONES("allocstall")
1108 TEXTS_FOR_ZONES("pgskip")
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001109
1110 "pgfree",
1111 "pgactivate",
1112 "pgdeactivate",
Shaohua Lif7ad2a62017-05-03 14:52:29 -07001113 "pglazyfree",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001114
1115 "pgfault",
1116 "pgmajfault",
Minchan Kim854e9ed2016-01-15 16:54:53 -08001117 "pglazyfreed",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001118
Mel Gorman599d0c92016-07-28 15:45:31 -07001119 "pgrefill",
1120 "pgsteal_kswapd",
1121 "pgsteal_direct",
1122 "pgscan_kswapd",
1123 "pgscan_direct",
Mel Gorman68243e72012-07-31 16:44:39 -07001124 "pgscan_direct_throttle",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001125
1126#ifdef CONFIG_NUMA
1127 "zone_reclaim_failed",
1128#endif
1129 "pginodesteal",
1130 "slabs_scanned",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001131 "kswapd_inodesteal",
1132 "kswapd_low_wmark_hit_quickly",
1133 "kswapd_high_wmark_hit_quickly",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001134 "pageoutrun",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001135
1136 "pgrotated",
1137
Dave Hansen5509a5d2014-04-03 14:48:19 -07001138 "drop_pagecache",
1139 "drop_slab",
Konstantin Khlebnikov8e675f72017-07-06 15:40:28 -07001140 "oom_kill",
Dave Hansen5509a5d2014-04-03 14:48:19 -07001141
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001142#ifdef CONFIG_NUMA_BALANCING
1143 "numa_pte_updates",
Mel Gorman72403b42013-11-12 15:08:32 -08001144 "numa_huge_pte_updates",
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001145 "numa_hint_faults",
1146 "numa_hint_faults_local",
1147 "numa_pages_migrated",
1148#endif
Mel Gorman5647bc22012-10-19 10:46:20 +01001149#ifdef CONFIG_MIGRATION
1150 "pgmigrate_success",
1151 "pgmigrate_fail",
1152#endif
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001153#ifdef CONFIG_COMPACTION
Mel Gorman397487d2012-10-19 12:00:10 +01001154 "compact_migrate_scanned",
1155 "compact_free_scanned",
1156 "compact_isolated",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001157 "compact_stall",
1158 "compact_fail",
1159 "compact_success",
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001160 "compact_daemon_wake",
David Rientjes7f354a52017-02-22 15:44:50 -08001161 "compact_daemon_migrate_scanned",
1162 "compact_daemon_free_scanned",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001163#endif
1164
1165#ifdef CONFIG_HUGETLB_PAGE
1166 "htlb_buddy_alloc_success",
1167 "htlb_buddy_alloc_fail",
1168#endif
1169 "unevictable_pgs_culled",
1170 "unevictable_pgs_scanned",
1171 "unevictable_pgs_rescued",
1172 "unevictable_pgs_mlocked",
1173 "unevictable_pgs_munlocked",
1174 "unevictable_pgs_cleared",
1175 "unevictable_pgs_stranded",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001176
1177#ifdef CONFIG_TRANSPARENT_HUGEPAGE
1178 "thp_fault_alloc",
1179 "thp_fault_fallback",
1180 "thp_collapse_alloc",
1181 "thp_collapse_alloc_failed",
Kirill A. Shutemov95ecedc2016-07-26 15:25:31 -07001182 "thp_file_alloc",
1183 "thp_file_mapped",
Kirill A. Shutemov122afea2016-01-15 16:52:46 -08001184 "thp_split_page",
1185 "thp_split_page_failed",
Kirill A. Shutemovf9719a02016-03-17 14:18:45 -07001186 "thp_deferred_split_page",
Kirill A. Shutemov122afea2016-01-15 16:52:46 -08001187 "thp_split_pmd",
Yisheng Xiece9311c2017-03-09 16:17:00 -08001188#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
1189 "thp_split_pud",
1190#endif
Kirill A. Shutemovd8a8e1f2012-12-12 13:51:09 -08001191 "thp_zero_page_alloc",
1192 "thp_zero_page_alloc_failed",
Huang Ying225311a2017-09-06 16:22:30 -07001193 "thp_swpout",
Huang Yingfe490cc2017-09-06 16:22:52 -07001194 "thp_swpout_fallback",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001195#endif
Konstantin Khlebnikov09316c02014-10-09 15:29:32 -07001196#ifdef CONFIG_MEMORY_BALLOON
1197 "balloon_inflate",
1198 "balloon_deflate",
1199#ifdef CONFIG_BALLOON_COMPACTION
1200 "balloon_migrate",
1201#endif
1202#endif /* CONFIG_MEMORY_BALLOON */
Mel Gormanec659932014-01-21 14:33:16 -08001203#ifdef CONFIG_DEBUG_TLBFLUSH
Dave Hansen6df46862013-09-11 14:20:24 -07001204#ifdef CONFIG_SMP
Dave Hansen9824cf92013-09-11 14:20:23 -07001205 "nr_tlb_remote_flush",
1206 "nr_tlb_remote_flush_received",
Jann Horn51787162018-10-05 15:52:07 -07001207#else
1208 "", /* 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",
1218#endif
Huang Yingcbc65df2017-09-06 16:24:29 -07001219#ifdef CONFIG_SWAP
1220 "swap_ra",
1221 "swap_ra_hit",
1222#endif
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001223#endif /* CONFIG_VM_EVENTS_COUNTERS */
1224};
David Rientjes0d6617c2011-09-14 16:21:05 -07001225#endif /* CONFIG_PROC_FS || CONFIG_SYSFS || CONFIG_NUMA */
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001226
Andrew Morton3c486872015-02-10 14:09:43 -08001227#if (defined(CONFIG_DEBUG_FS) && defined(CONFIG_COMPACTION)) || \
1228 defined(CONFIG_PROC_FS)
1229static void *frag_start(struct seq_file *m, loff_t *pos)
1230{
1231 pg_data_t *pgdat;
1232 loff_t node = *pos;
1233
1234 for (pgdat = first_online_pgdat();
1235 pgdat && node;
1236 pgdat = next_online_pgdat(pgdat))
1237 --node;
1238
1239 return pgdat;
1240}
1241
1242static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
1243{
1244 pg_data_t *pgdat = (pg_data_t *)arg;
1245
1246 (*pos)++;
1247 return next_online_pgdat(pgdat);
1248}
1249
1250static void frag_stop(struct seq_file *m, void *arg)
1251{
1252}
1253
David Rientjesb2bd8592017-05-03 14:52:59 -07001254/*
1255 * Walk zones in a node and print using a callback.
1256 * If @assert_populated is true, only use callback for zones that are populated.
1257 */
Andrew Morton3c486872015-02-10 14:09:43 -08001258static void walk_zones_in_node(struct seq_file *m, pg_data_t *pgdat,
Vinayak Menon727c0802017-07-10 15:49:17 -07001259 bool assert_populated, bool nolock,
Andrew Morton3c486872015-02-10 14:09:43 -08001260 void (*print)(struct seq_file *m, pg_data_t *, struct zone *))
1261{
1262 struct zone *zone;
1263 struct zone *node_zones = pgdat->node_zones;
1264 unsigned long flags;
1265
1266 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
David Rientjesb2bd8592017-05-03 14:52:59 -07001267 if (assert_populated && !populated_zone(zone))
Andrew Morton3c486872015-02-10 14:09:43 -08001268 continue;
1269
Vinayak Menon727c0802017-07-10 15:49:17 -07001270 if (!nolock)
1271 spin_lock_irqsave(&zone->lock, flags);
Andrew Morton3c486872015-02-10 14:09:43 -08001272 print(m, pgdat, zone);
Vinayak Menon727c0802017-07-10 15:49:17 -07001273 if (!nolock)
1274 spin_unlock_irqrestore(&zone->lock, flags);
Andrew Morton3c486872015-02-10 14:09:43 -08001275 }
1276}
1277#endif
1278
Mel Gormand7a57522010-05-24 14:32:25 -07001279#ifdef CONFIG_PROC_FS
Mel Gorman467c9962007-10-16 01:26:02 -07001280static void frag_show_print(struct seq_file *m, pg_data_t *pgdat,
1281 struct zone *zone)
1282{
1283 int order;
1284
1285 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
1286 for (order = 0; order < MAX_ORDER; ++order)
1287 seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
1288 seq_putc(m, '\n');
1289}
1290
1291/*
1292 * This walks the free areas for each zone.
1293 */
1294static int frag_show(struct seq_file *m, void *arg)
1295{
1296 pg_data_t *pgdat = (pg_data_t *)arg;
Vinayak Menon727c0802017-07-10 15:49:17 -07001297 walk_zones_in_node(m, pgdat, true, false, frag_show_print);
Mel Gorman467c9962007-10-16 01:26:02 -07001298 return 0;
1299}
1300
1301static void pagetypeinfo_showfree_print(struct seq_file *m,
1302 pg_data_t *pgdat, struct zone *zone)
1303{
1304 int order, mtype;
1305
1306 for (mtype = 0; mtype < MIGRATE_TYPES; mtype++) {
1307 seq_printf(m, "Node %4d, zone %8s, type %12s ",
1308 pgdat->node_id,
1309 zone->name,
1310 migratetype_names[mtype]);
1311 for (order = 0; order < MAX_ORDER; ++order) {
1312 unsigned long freecount = 0;
1313 struct free_area *area;
1314 struct list_head *curr;
1315
1316 area = &(zone->free_area[order]);
1317
1318 list_for_each(curr, &area->free_list[mtype])
1319 freecount++;
1320 seq_printf(m, "%6lu ", freecount);
1321 }
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001322 seq_putc(m, '\n');
1323 }
Mel Gorman467c9962007-10-16 01:26:02 -07001324}
1325
1326/* Print out the free pages at each order for each migatetype */
1327static int pagetypeinfo_showfree(struct seq_file *m, void *arg)
1328{
1329 int order;
1330 pg_data_t *pgdat = (pg_data_t *)arg;
1331
1332 /* Print header */
1333 seq_printf(m, "%-43s ", "Free pages count per migrate type at order");
1334 for (order = 0; order < MAX_ORDER; ++order)
1335 seq_printf(m, "%6d ", order);
1336 seq_putc(m, '\n');
1337
Vinayak Menon727c0802017-07-10 15:49:17 -07001338 walk_zones_in_node(m, pgdat, true, false, pagetypeinfo_showfree_print);
Mel Gorman467c9962007-10-16 01:26:02 -07001339
1340 return 0;
1341}
1342
1343static void pagetypeinfo_showblockcount_print(struct seq_file *m,
1344 pg_data_t *pgdat, struct zone *zone)
1345{
1346 int mtype;
1347 unsigned long pfn;
1348 unsigned long start_pfn = zone->zone_start_pfn;
Cody P Schafer108bcc92013-02-22 16:35:23 -08001349 unsigned long end_pfn = zone_end_pfn(zone);
Mel Gorman467c9962007-10-16 01:26:02 -07001350 unsigned long count[MIGRATE_TYPES] = { 0, };
1351
1352 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
1353 struct page *page;
1354
Michal Hockod336e942017-07-06 15:38:07 -07001355 page = pfn_to_online_page(pfn);
1356 if (!page)
Mel Gorman467c9962007-10-16 01:26:02 -07001357 continue;
1358
Mel Gormaneb335752009-05-13 17:34:48 +01001359 /* Watch for unexpected holes punched in the memmap */
1360 if (!memmap_valid_within(pfn, page, zone))
Mel Gormane80d6a22008-08-14 11:10:14 +01001361 continue;
Mel Gormaneb335752009-05-13 17:34:48 +01001362
Joonsoo Kima91c43c2016-05-19 17:12:10 -07001363 if (page_zone(page) != zone)
1364 continue;
1365
Mel Gorman467c9962007-10-16 01:26:02 -07001366 mtype = get_pageblock_migratetype(page);
1367
Mel Gormane80d6a22008-08-14 11:10:14 +01001368 if (mtype < MIGRATE_TYPES)
1369 count[mtype]++;
Mel Gorman467c9962007-10-16 01:26:02 -07001370 }
1371
1372 /* Print counts */
1373 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
1374 for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
1375 seq_printf(m, "%12lu ", count[mtype]);
1376 seq_putc(m, '\n');
1377}
1378
SeongJae Parkf113e642017-09-06 16:24:23 -07001379/* Print out the number of pageblocks for each migratetype */
Mel Gorman467c9962007-10-16 01:26:02 -07001380static int pagetypeinfo_showblockcount(struct seq_file *m, void *arg)
1381{
1382 int mtype;
1383 pg_data_t *pgdat = (pg_data_t *)arg;
1384
1385 seq_printf(m, "\n%-23s", "Number of blocks type ");
1386 for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
1387 seq_printf(m, "%12s ", migratetype_names[mtype]);
1388 seq_putc(m, '\n');
Vinayak Menon727c0802017-07-10 15:49:17 -07001389 walk_zones_in_node(m, pgdat, true, false,
1390 pagetypeinfo_showblockcount_print);
Mel Gorman467c9962007-10-16 01:26:02 -07001391
1392 return 0;
1393}
1394
Joonsoo Kim48c96a32014-12-12 16:56:01 -08001395/*
1396 * Print out the number of pageblocks for each migratetype that contain pages
1397 * of other types. This gives an indication of how well fallbacks are being
1398 * contained by rmqueue_fallback(). It requires information from PAGE_OWNER
1399 * to determine what is going on
1400 */
1401static void pagetypeinfo_showmixedcount(struct seq_file *m, pg_data_t *pgdat)
1402{
1403#ifdef CONFIG_PAGE_OWNER
1404 int mtype;
1405
Vlastimil Babka7dd80b82016-03-15 14:56:12 -07001406 if (!static_branch_unlikely(&page_owner_inited))
Joonsoo Kim48c96a32014-12-12 16:56:01 -08001407 return;
1408
1409 drain_all_pages(NULL);
1410
1411 seq_printf(m, "\n%-23s", "Number of mixed blocks ");
1412 for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
1413 seq_printf(m, "%12s ", migratetype_names[mtype]);
1414 seq_putc(m, '\n');
1415
Vinayak Menon727c0802017-07-10 15:49:17 -07001416 walk_zones_in_node(m, pgdat, true, true,
1417 pagetypeinfo_showmixedcount_print);
Joonsoo Kim48c96a32014-12-12 16:56:01 -08001418#endif /* CONFIG_PAGE_OWNER */
1419}
1420
Mel Gorman467c9962007-10-16 01:26:02 -07001421/*
1422 * This prints out statistics in relation to grouping pages by mobility.
1423 * It is expensive to collect so do not constantly read the file.
1424 */
1425static int pagetypeinfo_show(struct seq_file *m, void *arg)
1426{
1427 pg_data_t *pgdat = (pg_data_t *)arg;
1428
KOSAKI Motohiro41b25a32008-04-30 00:52:13 -07001429 /* check memoryless node */
Lai Jiangshana47b53c2012-12-12 13:51:37 -08001430 if (!node_state(pgdat->node_id, N_MEMORY))
KOSAKI Motohiro41b25a32008-04-30 00:52:13 -07001431 return 0;
1432
Mel Gorman467c9962007-10-16 01:26:02 -07001433 seq_printf(m, "Page block order: %d\n", pageblock_order);
1434 seq_printf(m, "Pages per block: %lu\n", pageblock_nr_pages);
1435 seq_putc(m, '\n');
1436 pagetypeinfo_showfree(m, pgdat);
1437 pagetypeinfo_showblockcount(m, pgdat);
Joonsoo Kim48c96a32014-12-12 16:56:01 -08001438 pagetypeinfo_showmixedcount(m, pgdat);
Mel Gorman467c9962007-10-16 01:26:02 -07001439
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001440 return 0;
1441}
1442
Alexey Dobriyan8f32f7e2008-10-06 04:13:52 +04001443static const struct seq_operations fragmentation_op = {
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001444 .start = frag_start,
1445 .next = frag_next,
1446 .stop = frag_stop,
1447 .show = frag_show,
1448};
1449
Alexey Dobriyan8f32f7e2008-10-06 04:13:52 +04001450static int fragmentation_open(struct inode *inode, struct file *file)
1451{
1452 return seq_open(file, &fragmentation_op);
1453}
1454
Anshuman Khandual9d85e152017-07-06 15:37:15 -07001455static const struct file_operations buddyinfo_file_operations = {
Alexey Dobriyan8f32f7e2008-10-06 04:13:52 +04001456 .open = fragmentation_open,
1457 .read = seq_read,
1458 .llseek = seq_lseek,
1459 .release = seq_release,
1460};
1461
Alexey Dobriyan74e2e8e2008-10-06 04:15:36 +04001462static const struct seq_operations pagetypeinfo_op = {
Mel Gorman467c9962007-10-16 01:26:02 -07001463 .start = frag_start,
1464 .next = frag_next,
1465 .stop = frag_stop,
1466 .show = pagetypeinfo_show,
1467};
1468
Alexey Dobriyan74e2e8e2008-10-06 04:15:36 +04001469static int pagetypeinfo_open(struct inode *inode, struct file *file)
1470{
1471 return seq_open(file, &pagetypeinfo_op);
1472}
1473
Anshuman Khandual9d85e152017-07-06 15:37:15 -07001474static const struct file_operations pagetypeinfo_file_operations = {
Alexey Dobriyan74e2e8e2008-10-06 04:15:36 +04001475 .open = pagetypeinfo_open,
1476 .read = seq_read,
1477 .llseek = seq_lseek,
1478 .release = seq_release,
1479};
1480
Mel Gormane2ecc8a2016-07-28 15:47:02 -07001481static bool is_zone_first_populated(pg_data_t *pgdat, struct zone *zone)
1482{
1483 int zid;
1484
1485 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
1486 struct zone *compare = &pgdat->node_zones[zid];
1487
1488 if (populated_zone(compare))
1489 return zone == compare;
1490 }
1491
Mel Gormane2ecc8a2016-07-28 15:47:02 -07001492 return false;
1493}
1494
Mel Gorman467c9962007-10-16 01:26:02 -07001495static void zoneinfo_show_print(struct seq_file *m, pg_data_t *pgdat,
1496 struct zone *zone)
1497{
1498 int i;
1499 seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
Mel Gormane2ecc8a2016-07-28 15:47:02 -07001500 if (is_zone_first_populated(pgdat, zone)) {
1501 seq_printf(m, "\n per-node stats");
1502 for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++) {
1503 seq_printf(m, "\n %-12s %lu",
Kemi Wang3a321d22017-09-08 16:12:48 -07001504 vmstat_text[i + NR_VM_ZONE_STAT_ITEMS +
1505 NR_VM_NUMA_STAT_ITEMS],
Mel Gormane2ecc8a2016-07-28 15:47:02 -07001506 node_page_state(pgdat, i));
1507 }
1508 }
Mel Gorman467c9962007-10-16 01:26:02 -07001509 seq_printf(m,
1510 "\n pages free %lu"
1511 "\n min %lu"
1512 "\n low %lu"
1513 "\n high %lu"
Mel Gorman467c9962007-10-16 01:26:02 -07001514 "\n spanned %lu"
Jiang Liu9feedc92012-12-12 13:52:12 -08001515 "\n present %lu"
1516 "\n managed %lu",
Mel Gorman88f5acf2011-01-13 15:45:41 -08001517 zone_page_state(zone, NR_FREE_PAGES),
Mel Gorman41858962009-06-16 15:32:12 -07001518 min_wmark_pages(zone),
1519 low_wmark_pages(zone),
1520 high_wmark_pages(zone),
Mel Gorman467c9962007-10-16 01:26:02 -07001521 zone->spanned_pages,
Jiang Liu9feedc92012-12-12 13:52:12 -08001522 zone->present_pages,
1523 zone->managed_pages);
Mel Gorman467c9962007-10-16 01:26:02 -07001524
Mel Gorman467c9962007-10-16 01:26:02 -07001525 seq_printf(m,
Mel Gorman3484b2d2014-08-06 16:07:14 -07001526 "\n protection: (%ld",
Mel Gorman467c9962007-10-16 01:26:02 -07001527 zone->lowmem_reserve[0]);
1528 for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
Mel Gorman3484b2d2014-08-06 16:07:14 -07001529 seq_printf(m, ", %ld", zone->lowmem_reserve[i]);
David Rientjes7dfb8bf2017-05-03 14:53:02 -07001530 seq_putc(m, ')');
1531
1532 /* If unpopulated, no other information is useful */
1533 if (!populated_zone(zone)) {
1534 seq_putc(m, '\n');
1535 return;
1536 }
1537
1538 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
1539 seq_printf(m, "\n %-12s %lu", vmstat_text[i],
1540 zone_page_state(zone, i));
1541
Kemi Wang3a321d22017-09-08 16:12:48 -07001542#ifdef CONFIG_NUMA
1543 for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++)
1544 seq_printf(m, "\n %-12s %lu",
1545 vmstat_text[i + NR_VM_ZONE_STAT_ITEMS],
Kemi Wang63803222017-09-08 16:12:55 -07001546 zone_numa_state_snapshot(zone, i));
Kemi Wang3a321d22017-09-08 16:12:48 -07001547#endif
1548
David Rientjes7dfb8bf2017-05-03 14:53:02 -07001549 seq_printf(m, "\n pagesets");
Mel Gorman467c9962007-10-16 01:26:02 -07001550 for_each_online_cpu(i) {
1551 struct per_cpu_pageset *pageset;
Mel Gorman467c9962007-10-16 01:26:02 -07001552
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09001553 pageset = per_cpu_ptr(zone->pageset, i);
Christoph Lameter3dfa5722008-02-04 22:29:19 -08001554 seq_printf(m,
1555 "\n cpu: %i"
1556 "\n count: %i"
1557 "\n high: %i"
1558 "\n batch: %i",
1559 i,
1560 pageset->pcp.count,
1561 pageset->pcp.high,
1562 pageset->pcp.batch);
Mel Gorman467c9962007-10-16 01:26:02 -07001563#ifdef CONFIG_SMP
1564 seq_printf(m, "\n vm stats threshold: %d",
1565 pageset->stat_threshold);
1566#endif
1567 }
1568 seq_printf(m,
Mel Gorman599d0c92016-07-28 15:45:31 -07001569 "\n node_unreclaimable: %u"
1570 "\n start_pfn: %lu"
1571 "\n node_inactive_ratio: %u",
Johannes Weinerc73322d2017-05-03 14:51:51 -07001572 pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES,
Rik van Riel556adec2008-10-18 20:26:34 -07001573 zone->zone_start_pfn,
Mel Gorman599d0c92016-07-28 15:45:31 -07001574 zone->zone_pgdat->inactive_ratio);
Mel Gorman467c9962007-10-16 01:26:02 -07001575 seq_putc(m, '\n');
1576}
1577
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001578/*
David Rientjesb2bd8592017-05-03 14:52:59 -07001579 * Output information about zones in @pgdat. All zones are printed regardless
1580 * of whether they are populated or not: lowmem_reserve_ratio operates on the
1581 * set of all zones and userspace would not be aware of such zones if they are
1582 * suppressed here (zoneinfo displays the effect of lowmem_reserve_ratio).
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001583 */
1584static int zoneinfo_show(struct seq_file *m, void *arg)
1585{
Mel Gorman467c9962007-10-16 01:26:02 -07001586 pg_data_t *pgdat = (pg_data_t *)arg;
Vinayak Menon727c0802017-07-10 15:49:17 -07001587 walk_zones_in_node(m, pgdat, false, false, zoneinfo_show_print);
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001588 return 0;
1589}
1590
Alexey Dobriyan5c9fe622008-10-06 04:19:42 +04001591static const struct seq_operations zoneinfo_op = {
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001592 .start = frag_start, /* iterate over all zones. The same as in
1593 * fragmentation. */
1594 .next = frag_next,
1595 .stop = frag_stop,
1596 .show = zoneinfo_show,
1597};
1598
Alexey Dobriyan5c9fe622008-10-06 04:19:42 +04001599static int zoneinfo_open(struct inode *inode, struct file *file)
1600{
1601 return seq_open(file, &zoneinfo_op);
1602}
1603
Anshuman Khandual9d85e152017-07-06 15:37:15 -07001604static const struct file_operations zoneinfo_file_operations = {
Alexey Dobriyan5c9fe622008-10-06 04:19:42 +04001605 .open = zoneinfo_open,
1606 .read = seq_read,
1607 .llseek = seq_lseek,
1608 .release = seq_release,
1609};
1610
Michael Rubin79da8262010-10-26 14:21:36 -07001611enum writeback_stat_item {
1612 NR_DIRTY_THRESHOLD,
1613 NR_DIRTY_BG_THRESHOLD,
1614 NR_VM_WRITEBACK_STAT_ITEMS,
1615};
1616
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001617static void *vmstat_start(struct seq_file *m, loff_t *pos)
1618{
Christoph Lameter2244b952006-06-30 01:55:33 -07001619 unsigned long *v;
Michael Rubin79da8262010-10-26 14:21:36 -07001620 int i, stat_items_size;
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001621
1622 if (*pos >= ARRAY_SIZE(vmstat_text))
1623 return NULL;
Michael Rubin79da8262010-10-26 14:21:36 -07001624 stat_items_size = NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long) +
Kemi Wang3a321d22017-09-08 16:12:48 -07001625 NR_VM_NUMA_STAT_ITEMS * sizeof(unsigned long) +
Mel Gorman75ef7182016-07-28 15:45:24 -07001626 NR_VM_NODE_STAT_ITEMS * sizeof(unsigned long) +
Michael Rubin79da8262010-10-26 14:21:36 -07001627 NR_VM_WRITEBACK_STAT_ITEMS * sizeof(unsigned long);
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001628
Christoph Lameterf8891e52006-06-30 01:55:45 -07001629#ifdef CONFIG_VM_EVENT_COUNTERS
Michael Rubin79da8262010-10-26 14:21:36 -07001630 stat_items_size += sizeof(struct vm_event_state);
Christoph Lameterf8891e52006-06-30 01:55:45 -07001631#endif
Michael Rubin79da8262010-10-26 14:21:36 -07001632
1633 v = kmalloc(stat_items_size, GFP_KERNEL);
Christoph Lameter2244b952006-06-30 01:55:33 -07001634 m->private = v;
1635 if (!v)
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001636 return ERR_PTR(-ENOMEM);
Christoph Lameter2244b952006-06-30 01:55:33 -07001637 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
Michal Hockoc41f0122017-09-06 16:23:36 -07001638 v[i] = global_zone_page_state(i);
Michael Rubin79da8262010-10-26 14:21:36 -07001639 v += NR_VM_ZONE_STAT_ITEMS;
1640
Kemi Wang3a321d22017-09-08 16:12:48 -07001641#ifdef CONFIG_NUMA
1642 for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++)
1643 v[i] = global_numa_state(i);
1644 v += NR_VM_NUMA_STAT_ITEMS;
1645#endif
1646
Mel Gorman75ef7182016-07-28 15:45:24 -07001647 for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++)
1648 v[i] = global_node_page_state(i);
1649 v += NR_VM_NODE_STAT_ITEMS;
1650
Michael Rubin79da8262010-10-26 14:21:36 -07001651 global_dirty_limits(v + NR_DIRTY_BG_THRESHOLD,
1652 v + NR_DIRTY_THRESHOLD);
1653 v += NR_VM_WRITEBACK_STAT_ITEMS;
1654
Christoph Lameterf8891e52006-06-30 01:55:45 -07001655#ifdef CONFIG_VM_EVENT_COUNTERS
Michael Rubin79da8262010-10-26 14:21:36 -07001656 all_vm_events(v);
1657 v[PGPGIN] /= 2; /* sectors -> kbytes */
1658 v[PGPGOUT] /= 2;
Christoph Lameterf8891e52006-06-30 01:55:45 -07001659#endif
Wu Fengguangff8b16d2010-11-04 01:56:49 +08001660 return (unsigned long *)m->private + *pos;
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001661}
1662
1663static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
1664{
1665 (*pos)++;
1666 if (*pos >= ARRAY_SIZE(vmstat_text))
1667 return NULL;
1668 return (unsigned long *)m->private + *pos;
1669}
1670
1671static int vmstat_show(struct seq_file *m, void *arg)
1672{
1673 unsigned long *l = arg;
1674 unsigned long off = l - (unsigned long *)m->private;
Alexey Dobriyan68ba0322016-10-07 17:02:14 -07001675
Roman Gushchind62b8ac2018-05-11 16:01:53 -07001676 /* Skip hidden vmstat items. */
1677 if (*vmstat_text[off] == '\0')
1678 return 0;
1679
Alexey Dobriyan68ba0322016-10-07 17:02:14 -07001680 seq_puts(m, vmstat_text[off]);
Joe Perches75ba1d02016-10-07 17:02:20 -07001681 seq_put_decimal_ull(m, " ", *l);
Alexey Dobriyan68ba0322016-10-07 17:02:14 -07001682 seq_putc(m, '\n');
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001683 return 0;
1684}
1685
1686static void vmstat_stop(struct seq_file *m, void *arg)
1687{
1688 kfree(m->private);
1689 m->private = NULL;
1690}
1691
Alexey Dobriyanb6aa44a2008-10-06 04:17:48 +04001692static const struct seq_operations vmstat_op = {
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001693 .start = vmstat_start,
1694 .next = vmstat_next,
1695 .stop = vmstat_stop,
1696 .show = vmstat_show,
1697};
1698
Alexey Dobriyanb6aa44a2008-10-06 04:17:48 +04001699static int vmstat_open(struct inode *inode, struct file *file)
1700{
1701 return seq_open(file, &vmstat_op);
1702}
1703
Anshuman Khandual9d85e152017-07-06 15:37:15 -07001704static const struct file_operations vmstat_file_operations = {
Alexey Dobriyanb6aa44a2008-10-06 04:17:48 +04001705 .open = vmstat_open,
1706 .read = seq_read,
1707 .llseek = seq_lseek,
1708 .release = seq_release,
1709};
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001710#endif /* CONFIG_PROC_FS */
1711
Christoph Lameterdf9ecab2006-08-31 21:27:35 -07001712#ifdef CONFIG_SMP
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001713static DEFINE_PER_CPU(struct delayed_work, vmstat_work);
Christoph Lameter77461ab2007-05-09 02:35:13 -07001714int sysctl_stat_interval __read_mostly = HZ;
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001715
Hugh Dickins52b6f462016-05-19 17:12:50 -07001716#ifdef CONFIG_PROC_FS
1717static void refresh_vm_stats(struct work_struct *work)
1718{
1719 refresh_cpu_vm_stats(true);
1720}
1721
1722int vmstat_refresh(struct ctl_table *table, int write,
1723 void __user *buffer, size_t *lenp, loff_t *ppos)
1724{
1725 long val;
1726 int err;
1727 int i;
1728
1729 /*
1730 * The regular update, every sysctl_stat_interval, may come later
1731 * than expected: leaving a significant amount in per_cpu buckets.
1732 * This is particularly misleading when checking a quantity of HUGE
1733 * pages, immediately after running a test. /proc/sys/vm/stat_refresh,
1734 * which can equally be echo'ed to or cat'ted from (by root),
1735 * can be used to update the stats just before reading them.
1736 *
Michal Hockoc41f0122017-09-06 16:23:36 -07001737 * Oh, and since global_zone_page_state() etc. are so careful to hide
Hugh Dickins52b6f462016-05-19 17:12:50 -07001738 * transiently negative values, report an error here if any of
1739 * the stats is negative, so we know to go looking for imbalance.
1740 */
1741 err = schedule_on_each_cpu(refresh_vm_stats);
1742 if (err)
1743 return err;
1744 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++) {
Mel Gorman75ef7182016-07-28 15:45:24 -07001745 val = atomic_long_read(&vm_zone_stat[i]);
Hugh Dickins52b6f462016-05-19 17:12:50 -07001746 if (val < 0) {
Johannes Weinerc822f622017-05-03 14:52:10 -07001747 pr_warn("%s: %s %ld\n",
1748 __func__, vmstat_text[i], val);
1749 err = -EINVAL;
Hugh Dickins52b6f462016-05-19 17:12:50 -07001750 }
1751 }
Kemi Wang3a321d22017-09-08 16:12:48 -07001752#ifdef CONFIG_NUMA
1753 for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++) {
1754 val = atomic_long_read(&vm_numa_stat[i]);
1755 if (val < 0) {
1756 pr_warn("%s: %s %ld\n",
1757 __func__, vmstat_text[i + NR_VM_ZONE_STAT_ITEMS], val);
1758 err = -EINVAL;
1759 }
1760 }
1761#endif
Hugh Dickins52b6f462016-05-19 17:12:50 -07001762 if (err)
1763 return err;
1764 if (write)
1765 *ppos += *lenp;
1766 else
1767 *lenp = 0;
1768 return 0;
1769}
1770#endif /* CONFIG_PROC_FS */
1771
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001772static void vmstat_update(struct work_struct *w)
1773{
Christoph Lameter0eb77e92016-01-14 15:21:40 -08001774 if (refresh_cpu_vm_stats(true)) {
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001775 /*
1776 * Counters were updated so we expect more updates
1777 * to occur in the future. Keep on running the
1778 * update worker thread.
1779 */
Michal Hockoce612872017-04-07 16:05:05 -07001780 queue_delayed_work_on(smp_processor_id(), mm_percpu_wq,
Michal Hockof01f17d2016-02-05 15:36:24 -08001781 this_cpu_ptr(&vmstat_work),
1782 round_jiffies_relative(sysctl_stat_interval));
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001783 }
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001784}
1785
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001786/*
Christoph Lameter0eb77e92016-01-14 15:21:40 -08001787 * Switch off vmstat processing and then fold all the remaining differentials
1788 * until the diffs stay at zero. The function is used by NOHZ and can only be
1789 * invoked when tick processing is not active.
1790 */
Christoph Lameter0eb77e92016-01-14 15:21:40 -08001791/*
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001792 * Check if the diffs for a certain cpu indicate that
1793 * an update is needed.
1794 */
1795static bool need_update(int cpu)
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001796{
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001797 struct zone *zone;
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001798
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001799 for_each_populated_zone(zone) {
1800 struct per_cpu_pageset *p = per_cpu_ptr(zone->pageset, cpu);
1801
1802 BUILD_BUG_ON(sizeof(p->vm_stat_diff[0]) != 1);
Kemi Wang3a321d22017-09-08 16:12:48 -07001803#ifdef CONFIG_NUMA
Kemi Wang1d90ca82017-09-08 16:12:52 -07001804 BUILD_BUG_ON(sizeof(p->vm_numa_stat_diff[0]) != 2);
Kemi Wang3a321d22017-09-08 16:12:48 -07001805#endif
Kemi Wang63803222017-09-08 16:12:55 -07001806
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001807 /*
1808 * The fast way of checking if there are any vmstat diffs.
1809 * This works because the diffs are byte sized items.
1810 */
1811 if (memchr_inv(p->vm_stat_diff, 0, NR_VM_ZONE_STAT_ITEMS))
1812 return true;
Kemi Wang3a321d22017-09-08 16:12:48 -07001813#ifdef CONFIG_NUMA
1814 if (memchr_inv(p->vm_numa_stat_diff, 0, NR_VM_NUMA_STAT_ITEMS))
1815 return true;
1816#endif
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001817 }
1818 return false;
1819}
1820
Christoph Lameter7b8da4c2016-05-20 16:58:21 -07001821/*
1822 * Switch off vmstat processing and then fold all the remaining differentials
1823 * until the diffs stay at zero. The function is used by NOHZ and can only be
1824 * invoked when tick processing is not active.
1825 */
Michal Hockof01f17d2016-02-05 15:36:24 -08001826void quiet_vmstat(void)
1827{
1828 if (system_state != SYSTEM_RUNNING)
1829 return;
1830
Christoph Lameter7b8da4c2016-05-20 16:58:21 -07001831 if (!delayed_work_pending(this_cpu_ptr(&vmstat_work)))
Michal Hockof01f17d2016-02-05 15:36:24 -08001832 return;
1833
1834 if (!need_update(smp_processor_id()))
1835 return;
1836
1837 /*
1838 * Just refresh counters and do not care about the pending delayed
1839 * vmstat_update. It doesn't fire that often to matter and canceling
1840 * it would be too expensive from this path.
1841 * vmstat_shepherd will take care about that for us.
1842 */
1843 refresh_cpu_vm_stats(false);
1844}
1845
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001846/*
1847 * Shepherd worker thread that checks the
1848 * differentials of processors that have their worker
1849 * threads for vm statistics updates disabled because of
1850 * inactivity.
1851 */
1852static void vmstat_shepherd(struct work_struct *w);
1853
Christoph Lameter0eb77e92016-01-14 15:21:40 -08001854static DECLARE_DEFERRABLE_WORK(shepherd, vmstat_shepherd);
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001855
1856static void vmstat_shepherd(struct work_struct *w)
1857{
1858 int cpu;
1859
1860 get_online_cpus();
1861 /* Check processors whose vmstat worker threads have been disabled */
Christoph Lameter7b8da4c2016-05-20 16:58:21 -07001862 for_each_online_cpu(cpu) {
Michal Hockof01f17d2016-02-05 15:36:24 -08001863 struct delayed_work *dw = &per_cpu(vmstat_work, cpu);
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001864
Christoph Lameter7b8da4c2016-05-20 16:58:21 -07001865 if (!delayed_work_pending(dw) && need_update(cpu))
Michal Hockoce612872017-04-07 16:05:05 -07001866 queue_delayed_work_on(cpu, mm_percpu_wq, dw, 0);
Michal Hockof01f17d2016-02-05 15:36:24 -08001867 }
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001868 put_online_cpus();
1869
1870 schedule_delayed_work(&shepherd,
1871 round_jiffies_relative(sysctl_stat_interval));
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001872}
1873
1874static void __init start_shepherd_timer(void)
1875{
1876 int cpu;
1877
1878 for_each_possible_cpu(cpu)
Michal Hockoccde8bd2016-02-05 15:36:27 -08001879 INIT_DEFERRABLE_WORK(per_cpu_ptr(&vmstat_work, cpu),
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001880 vmstat_update);
1881
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001882 schedule_delayed_work(&shepherd,
1883 round_jiffies_relative(sysctl_stat_interval));
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001884}
1885
Tim Chen03e86db2016-10-07 17:00:02 -07001886static void __init init_cpu_node_state(void)
1887{
Sebastian Andrzej Siewior4c501322016-11-29 15:51:14 +01001888 int node;
Tim Chen03e86db2016-10-07 17:00:02 -07001889
Sebastian Andrzej Siewior4c501322016-11-29 15:51:14 +01001890 for_each_online_node(node) {
1891 if (cpumask_weight(cpumask_of_node(node)) > 0)
1892 node_set_state(node, N_CPU);
1893 }
Tim Chen03e86db2016-10-07 17:00:02 -07001894}
1895
Sebastian Andrzej Siewior5438da92016-11-29 15:52:21 +01001896static int vmstat_cpu_online(unsigned int cpu)
1897{
1898 refresh_zone_stat_thresholds();
1899 node_set_state(cpu_to_node(cpu), N_CPU);
1900 return 0;
1901}
1902
1903static int vmstat_cpu_down_prep(unsigned int cpu)
1904{
1905 cancel_delayed_work_sync(&per_cpu(vmstat_work, cpu));
1906 return 0;
1907}
1908
1909static int vmstat_cpu_dead(unsigned int cpu)
Toshi Kani807a1bd2013-11-12 15:08:13 -08001910{
Sebastian Andrzej Siewior4c501322016-11-29 15:51:14 +01001911 const struct cpumask *node_cpus;
Sebastian Andrzej Siewior5438da92016-11-29 15:52:21 +01001912 int node;
Toshi Kani807a1bd2013-11-12 15:08:13 -08001913
Sebastian Andrzej Siewior5438da92016-11-29 15:52:21 +01001914 node = cpu_to_node(cpu);
1915
1916 refresh_zone_stat_thresholds();
Sebastian Andrzej Siewior4c501322016-11-29 15:51:14 +01001917 node_cpus = cpumask_of_node(node);
1918 if (cpumask_weight(node_cpus) > 0)
Sebastian Andrzej Siewior5438da92016-11-29 15:52:21 +01001919 return 0;
Toshi Kani807a1bd2013-11-12 15:08:13 -08001920
1921 node_clear_state(node, N_CPU);
Sebastian Andrzej Siewior5438da92016-11-29 15:52:21 +01001922 return 0;
Toshi Kani807a1bd2013-11-12 15:08:13 -08001923}
1924
Alexey Dobriyan8f32f7e2008-10-06 04:13:52 +04001925#endif
Christoph Lameterdf9ecab2006-08-31 21:27:35 -07001926
Michal Hockoce612872017-04-07 16:05:05 -07001927struct workqueue_struct *mm_percpu_wq;
1928
Michal Hocko597b7302017-03-31 15:11:47 -07001929void __init init_mm_internals(void)
Christoph Lameterdf9ecab2006-08-31 21:27:35 -07001930{
Michal Hockoce612872017-04-07 16:05:05 -07001931 int ret __maybe_unused;
Sebastian Andrzej Siewior5438da92016-11-29 15:52:21 +01001932
Michal Hocko80d136e2017-04-19 09:52:46 +02001933 mm_percpu_wq = alloc_workqueue("mm_percpu_wq", WQ_MEM_RECLAIM, 0);
Michal Hockoce612872017-04-07 16:05:05 -07001934
1935#ifdef CONFIG_SMP
Sebastian Andrzej Siewior5438da92016-11-29 15:52:21 +01001936 ret = cpuhp_setup_state_nocalls(CPUHP_MM_VMSTAT_DEAD, "mm/vmstat:dead",
1937 NULL, vmstat_cpu_dead);
1938 if (ret < 0)
1939 pr_err("vmstat: failed to register 'dead' hotplug state\n");
1940
1941 ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "mm/vmstat:online",
1942 vmstat_cpu_online,
1943 vmstat_cpu_down_prep);
1944 if (ret < 0)
1945 pr_err("vmstat: failed to register 'online' hotplug state\n");
1946
1947 get_online_cpus();
Tim Chen03e86db2016-10-07 17:00:02 -07001948 init_cpu_node_state();
Sebastian Andrzej Siewior5438da92016-11-29 15:52:21 +01001949 put_online_cpus();
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001950
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001951 start_shepherd_timer();
Alexey Dobriyan8f32f7e2008-10-06 04:13:52 +04001952#endif
1953#ifdef CONFIG_PROC_FS
Anshuman Khandual9d85e152017-07-06 15:37:15 -07001954 proc_create("buddyinfo", 0444, NULL, &buddyinfo_file_operations);
1955 proc_create("pagetypeinfo", 0444, NULL, &pagetypeinfo_file_operations);
1956 proc_create("vmstat", 0444, NULL, &vmstat_file_operations);
1957 proc_create("zoneinfo", 0444, NULL, &zoneinfo_file_operations);
Alexey Dobriyan8f32f7e2008-10-06 04:13:52 +04001958#endif
Christoph Lameterdf9ecab2006-08-31 21:27:35 -07001959}
Mel Gormand7a57522010-05-24 14:32:25 -07001960
1961#if defined(CONFIG_DEBUG_FS) && defined(CONFIG_COMPACTION)
Mel Gormand7a57522010-05-24 14:32:25 -07001962
1963/*
1964 * Return an index indicating how much of the available free memory is
1965 * unusable for an allocation of the requested size.
1966 */
1967static int unusable_free_index(unsigned int order,
1968 struct contig_page_info *info)
1969{
1970 /* No free memory is interpreted as all free memory is unusable */
1971 if (info->free_pages == 0)
1972 return 1000;
1973
1974 /*
1975 * Index should be a value between 0 and 1. Return a value to 3
1976 * decimal places.
1977 *
1978 * 0 => no fragmentation
1979 * 1 => high fragmentation
1980 */
1981 return div_u64((info->free_pages - (info->free_blocks_suitable << order)) * 1000ULL, info->free_pages);
1982
1983}
1984
1985static void unusable_show_print(struct seq_file *m,
1986 pg_data_t *pgdat, struct zone *zone)
1987{
1988 unsigned int order;
1989 int index;
1990 struct contig_page_info info;
1991
1992 seq_printf(m, "Node %d, zone %8s ",
1993 pgdat->node_id,
1994 zone->name);
1995 for (order = 0; order < MAX_ORDER; ++order) {
1996 fill_contig_page_info(zone, order, &info);
1997 index = unusable_free_index(order, &info);
1998 seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
1999 }
2000
2001 seq_putc(m, '\n');
2002}
2003
2004/*
2005 * Display unusable free space index
2006 *
2007 * The unusable free space index measures how much of the available free
2008 * memory cannot be used to satisfy an allocation of a given size and is a
2009 * value between 0 and 1. The higher the value, the more of free memory is
2010 * unusable and by implication, the worse the external fragmentation is. This
2011 * can be expressed as a percentage by multiplying by 100.
2012 */
2013static int unusable_show(struct seq_file *m, void *arg)
2014{
2015 pg_data_t *pgdat = (pg_data_t *)arg;
2016
2017 /* check memoryless node */
Lai Jiangshana47b53c2012-12-12 13:51:37 -08002018 if (!node_state(pgdat->node_id, N_MEMORY))
Mel Gormand7a57522010-05-24 14:32:25 -07002019 return 0;
2020
Vinayak Menon727c0802017-07-10 15:49:17 -07002021 walk_zones_in_node(m, pgdat, true, false, unusable_show_print);
Mel Gormand7a57522010-05-24 14:32:25 -07002022
2023 return 0;
2024}
2025
2026static const struct seq_operations unusable_op = {
2027 .start = frag_start,
2028 .next = frag_next,
2029 .stop = frag_stop,
2030 .show = unusable_show,
2031};
2032
2033static int unusable_open(struct inode *inode, struct file *file)
2034{
2035 return seq_open(file, &unusable_op);
2036}
2037
2038static const struct file_operations unusable_file_ops = {
2039 .open = unusable_open,
2040 .read = seq_read,
2041 .llseek = seq_lseek,
2042 .release = seq_release,
2043};
2044
Mel Gormanf1a5ab12010-05-24 14:32:26 -07002045static void extfrag_show_print(struct seq_file *m,
2046 pg_data_t *pgdat, struct zone *zone)
2047{
2048 unsigned int order;
2049 int index;
2050
2051 /* Alloc on stack as interrupts are disabled for zone walk */
2052 struct contig_page_info info;
2053
2054 seq_printf(m, "Node %d, zone %8s ",
2055 pgdat->node_id,
2056 zone->name);
2057 for (order = 0; order < MAX_ORDER; ++order) {
2058 fill_contig_page_info(zone, order, &info);
Mel Gorman56de7262010-05-24 14:32:30 -07002059 index = __fragmentation_index(order, &info);
Mel Gormanf1a5ab12010-05-24 14:32:26 -07002060 seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
2061 }
2062
2063 seq_putc(m, '\n');
2064}
2065
2066/*
2067 * Display fragmentation index for orders that allocations would fail for
2068 */
2069static int extfrag_show(struct seq_file *m, void *arg)
2070{
2071 pg_data_t *pgdat = (pg_data_t *)arg;
2072
Vinayak Menon727c0802017-07-10 15:49:17 -07002073 walk_zones_in_node(m, pgdat, true, false, extfrag_show_print);
Mel Gormanf1a5ab12010-05-24 14:32:26 -07002074
2075 return 0;
2076}
2077
2078static const struct seq_operations extfrag_op = {
2079 .start = frag_start,
2080 .next = frag_next,
2081 .stop = frag_stop,
2082 .show = extfrag_show,
2083};
2084
2085static int extfrag_open(struct inode *inode, struct file *file)
2086{
2087 return seq_open(file, &extfrag_op);
2088}
2089
2090static const struct file_operations extfrag_file_ops = {
2091 .open = extfrag_open,
2092 .read = seq_read,
2093 .llseek = seq_lseek,
2094 .release = seq_release,
2095};
2096
Mel Gormand7a57522010-05-24 14:32:25 -07002097static int __init extfrag_debug_init(void)
2098{
Sasikantha babubde8bd82012-05-29 15:06:22 -07002099 struct dentry *extfrag_debug_root;
2100
Mel Gormand7a57522010-05-24 14:32:25 -07002101 extfrag_debug_root = debugfs_create_dir("extfrag", NULL);
2102 if (!extfrag_debug_root)
2103 return -ENOMEM;
2104
2105 if (!debugfs_create_file("unusable_index", 0444,
2106 extfrag_debug_root, NULL, &unusable_file_ops))
Sasikantha babubde8bd82012-05-29 15:06:22 -07002107 goto fail;
Mel Gormand7a57522010-05-24 14:32:25 -07002108
Mel Gormanf1a5ab12010-05-24 14:32:26 -07002109 if (!debugfs_create_file("extfrag_index", 0444,
2110 extfrag_debug_root, NULL, &extfrag_file_ops))
Sasikantha babubde8bd82012-05-29 15:06:22 -07002111 goto fail;
Mel Gormanf1a5ab12010-05-24 14:32:26 -07002112
Mel Gormand7a57522010-05-24 14:32:25 -07002113 return 0;
Sasikantha babubde8bd82012-05-29 15:06:22 -07002114fail:
2115 debugfs_remove_recursive(extfrag_debug_root);
2116 return -ENOMEM;
Mel Gormand7a57522010-05-24 14:32:25 -07002117}
2118
2119module_init(extfrag_debug_init);
2120#endif