blob: ceb66e960376eb0a89af3ea7b513d6d530ba329e [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Christoph Lameterf6ac2352006-06-30 01:55:32 -07002/*
3 * linux/mm/vmstat.c
4 *
5 * Manages VM statistics
6 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
Christoph Lameter2244b952006-06-30 01:55:33 -07007 *
8 * zoned VM statistics
9 * Copyright (C) 2006 Silicon Graphics, Inc.,
10 * Christoph Lameter <christoph@lameter.com>
Christoph Lameter7cc36bb2014-10-09 15:29:43 -070011 * Copyright (C) 2008-2014 Christoph Lameter
Christoph Lameterf6ac2352006-06-30 01:55:32 -070012 */
Alexey Dobriyan8f32f7e2008-10-06 04:13:52 +040013#include <linux/fs.h>
Christoph Lameterf6ac2352006-06-30 01:55:32 -070014#include <linux/mm.h>
Alexey Dobriyan4e950f62007-07-30 02:36:13 +040015#include <linux/err.h>
Christoph Lameter2244b952006-06-30 01:55:33 -070016#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Christoph Lameterdf9ecab2006-08-31 21:27:35 -070018#include <linux/cpu.h>
Christoph Lameter7cc36bb2014-10-09 15:29:43 -070019#include <linux/cpumask.h>
Adrian Bunkc748e132008-07-23 21:27:03 -070020#include <linux/vmstat.h>
Andrew Morton3c486872015-02-10 14:09:43 -080021#include <linux/proc_fs.h>
22#include <linux/seq_file.h>
23#include <linux/debugfs.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040024#include <linux/sched.h>
Mel Gormanf1a5ab12010-05-24 14:32:26 -070025#include <linux/math64.h>
Michael Rubin79da8262010-10-26 14:21:36 -070026#include <linux/writeback.h>
Namhyung Kim36deb0b2010-10-26 14:22:04 -070027#include <linux/compaction.h>
Lisa Du6e543d52013-09-11 14:22:36 -070028#include <linux/mm_inline.h>
Joonsoo Kim48c96a32014-12-12 16:56:01 -080029#include <linux/page_ext.h>
30#include <linux/page_owner.h>
Lisa Du6e543d52013-09-11 14:22:36 -070031
32#include "internal.h"
Christoph Lameterf6ac2352006-06-30 01:55:32 -070033
Kemi Wang1d90ca82017-09-08 16:12:52 -070034#define NUMA_STATS_THRESHOLD (U16_MAX - 2)
35
Kemi Wang45180852017-11-15 17:38:22 -080036#ifdef CONFIG_NUMA
37int sysctl_vm_numa_stat = ENABLE_NUMA_STAT;
38
39/* zero numa counters within a zone */
40static void zero_zone_numa_counters(struct zone *zone)
41{
42 int item, cpu;
43
44 for (item = 0; item < NR_VM_NUMA_STAT_ITEMS; item++) {
45 atomic_long_set(&zone->vm_numa_stat[item], 0);
46 for_each_online_cpu(cpu)
47 per_cpu_ptr(zone->pageset, cpu)->vm_numa_stat_diff[item]
48 = 0;
49 }
50}
51
52/* zero numa counters of all the populated zones */
53static void zero_zones_numa_counters(void)
54{
55 struct zone *zone;
56
57 for_each_populated_zone(zone)
58 zero_zone_numa_counters(zone);
59}
60
61/* zero global numa counters */
62static void zero_global_numa_counters(void)
63{
64 int item;
65
66 for (item = 0; item < NR_VM_NUMA_STAT_ITEMS; item++)
67 atomic_long_set(&vm_numa_stat[item], 0);
68}
69
70static void invalid_numa_statistics(void)
71{
72 zero_zones_numa_counters();
73 zero_global_numa_counters();
74}
75
76static DEFINE_MUTEX(vm_numa_stat_lock);
77
78int sysctl_vm_numa_stat_handler(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +020079 void *buffer, size_t *length, loff_t *ppos)
Kemi Wang45180852017-11-15 17:38:22 -080080{
81 int ret, oldval;
82
83 mutex_lock(&vm_numa_stat_lock);
84 if (write)
85 oldval = sysctl_vm_numa_stat;
86 ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
87 if (ret || !write)
88 goto out;
89
90 if (oldval == sysctl_vm_numa_stat)
91 goto out;
92 else if (sysctl_vm_numa_stat == ENABLE_NUMA_STAT) {
93 static_branch_enable(&vm_numa_stat_key);
94 pr_info("enable numa statistics\n");
95 } else {
96 static_branch_disable(&vm_numa_stat_key);
97 invalid_numa_statistics();
98 pr_info("disable numa statistics, and clear numa counters\n");
99 }
100
101out:
102 mutex_unlock(&vm_numa_stat_lock);
103 return ret;
104}
105#endif
106
Christoph Lameterf8891e52006-06-30 01:55:45 -0700107#ifdef CONFIG_VM_EVENT_COUNTERS
108DEFINE_PER_CPU(struct vm_event_state, vm_event_states) = {{0}};
109EXPORT_PER_CPU_SYMBOL(vm_event_states);
110
Minchan Kim31f961a2010-08-09 17:18:59 -0700111static void sum_vm_events(unsigned long *ret)
Christoph Lameterf8891e52006-06-30 01:55:45 -0700112{
Christoph Lameter9eccf2a2008-02-04 22:29:22 -0800113 int cpu;
Christoph Lameterf8891e52006-06-30 01:55:45 -0700114 int i;
115
116 memset(ret, 0, NR_VM_EVENT_ITEMS * sizeof(unsigned long));
117
Minchan Kim31f961a2010-08-09 17:18:59 -0700118 for_each_online_cpu(cpu) {
Christoph Lameterf8891e52006-06-30 01:55:45 -0700119 struct vm_event_state *this = &per_cpu(vm_event_states, cpu);
120
Christoph Lameterf8891e52006-06-30 01:55:45 -0700121 for (i = 0; i < NR_VM_EVENT_ITEMS; i++)
122 ret[i] += this->event[i];
123 }
124}
125
126/*
127 * Accumulate the vm event counters across all CPUs.
128 * The result is unavoidably approximate - it can change
129 * during and after execution of this function.
130*/
131void all_vm_events(unsigned long *ret)
132{
KOSAKI Motohirob5be1132008-05-12 14:02:06 -0700133 get_online_cpus();
Minchan Kim31f961a2010-08-09 17:18:59 -0700134 sum_vm_events(ret);
KOSAKI Motohirob5be1132008-05-12 14:02:06 -0700135 put_online_cpus();
Christoph Lameterf8891e52006-06-30 01:55:45 -0700136}
Heiko Carstens32dd66f2006-07-10 04:44:31 -0700137EXPORT_SYMBOL_GPL(all_vm_events);
Christoph Lameterf8891e52006-06-30 01:55:45 -0700138
Christoph Lameterf8891e52006-06-30 01:55:45 -0700139/*
140 * Fold the foreign cpu events into our own.
141 *
142 * This is adding to the events on one processor
143 * but keeps the global counts constant.
144 */
145void vm_events_fold_cpu(int cpu)
146{
147 struct vm_event_state *fold_state = &per_cpu(vm_event_states, cpu);
148 int i;
149
150 for (i = 0; i < NR_VM_EVENT_ITEMS; i++) {
151 count_vm_events(i, fold_state->event[i]);
152 fold_state->event[i] = 0;
153 }
154}
Christoph Lameterf8891e52006-06-30 01:55:45 -0700155
156#endif /* CONFIG_VM_EVENT_COUNTERS */
157
Christoph Lameter2244b952006-06-30 01:55:33 -0700158/*
159 * Manage combined zone based / global counters
160 *
161 * vm_stat contains the global counters
162 */
Mel Gorman75ef7182016-07-28 15:45:24 -0700163atomic_long_t vm_zone_stat[NR_VM_ZONE_STAT_ITEMS] __cacheline_aligned_in_smp;
Kemi Wang3a321d22017-09-08 16:12:48 -0700164atomic_long_t vm_numa_stat[NR_VM_NUMA_STAT_ITEMS] __cacheline_aligned_in_smp;
Mel Gorman75ef7182016-07-28 15:45:24 -0700165atomic_long_t vm_node_stat[NR_VM_NODE_STAT_ITEMS] __cacheline_aligned_in_smp;
166EXPORT_SYMBOL(vm_zone_stat);
Kemi Wang3a321d22017-09-08 16:12:48 -0700167EXPORT_SYMBOL(vm_numa_stat);
Mel Gorman75ef7182016-07-28 15:45:24 -0700168EXPORT_SYMBOL(vm_node_stat);
Christoph Lameter2244b952006-06-30 01:55:33 -0700169
170#ifdef CONFIG_SMP
171
Mel Gormanb44129b2011-01-13 15:45:43 -0800172int calculate_pressure_threshold(struct zone *zone)
Mel Gorman88f5acf2011-01-13 15:45:41 -0800173{
174 int threshold;
175 int watermark_distance;
176
177 /*
178 * As vmstats are not up to date, there is drift between the estimated
179 * and real values. For high thresholds and a high number of CPUs, it
180 * is possible for the min watermark to be breached while the estimated
181 * value looks fine. The pressure threshold is a reduced value such
182 * that even the maximum amount of drift will not accidentally breach
183 * the min watermark
184 */
185 watermark_distance = low_wmark_pages(zone) - min_wmark_pages(zone);
186 threshold = max(1, (int)(watermark_distance / num_online_cpus()));
187
188 /*
189 * Maximum threshold is 125
190 */
191 threshold = min(125, threshold);
192
193 return threshold;
194}
195
Mel Gormanb44129b2011-01-13 15:45:43 -0800196int calculate_normal_threshold(struct zone *zone)
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700197{
198 int threshold;
199 int mem; /* memory in 128 MB units */
200
201 /*
202 * The threshold scales with the number of processors and the amount
203 * of memory per zone. More memory means that we can defer updates for
204 * longer, more processors could lead to more contention.
205 * fls() is used to have a cheap way of logarithmic scaling.
206 *
207 * Some sample thresholds:
208 *
209 * Threshold Processors (fls) Zonesize fls(mem+1)
210 * ------------------------------------------------------------------
211 * 8 1 1 0.9-1 GB 4
212 * 16 2 2 0.9-1 GB 4
213 * 20 2 2 1-2 GB 5
214 * 24 2 2 2-4 GB 6
215 * 28 2 2 4-8 GB 7
216 * 32 2 2 8-16 GB 8
217 * 4 2 2 <128M 1
218 * 30 4 3 2-4 GB 5
219 * 48 4 3 8-16 GB 8
220 * 32 8 4 1-2 GB 4
221 * 32 8 4 0.9-1GB 4
222 * 10 16 5 <128M 1
223 * 40 16 5 900M 4
224 * 70 64 7 2-4 GB 5
225 * 84 64 7 4-8 GB 6
226 * 108 512 9 4-8 GB 6
227 * 125 1024 10 8-16 GB 8
228 * 125 1024 10 16-32 GB 9
229 */
230
Arun KS9705bea2018-12-28 00:34:24 -0800231 mem = zone_managed_pages(zone) >> (27 - PAGE_SHIFT);
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700232
233 threshold = 2 * fls(num_online_cpus()) * (1 + fls(mem));
234
235 /*
236 * Maximum threshold is 125
237 */
238 threshold = min(125, threshold);
239
240 return threshold;
241}
Christoph Lameter2244b952006-06-30 01:55:33 -0700242
243/*
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700244 * Refresh the thresholds for each zone.
Christoph Lameter2244b952006-06-30 01:55:33 -0700245 */
KOSAKI Motohiroa6cccdc2011-05-24 17:11:33 -0700246void refresh_zone_stat_thresholds(void)
Christoph Lameter2244b952006-06-30 01:55:33 -0700247{
Mel Gorman75ef7182016-07-28 15:45:24 -0700248 struct pglist_data *pgdat;
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700249 struct zone *zone;
250 int cpu;
251 int threshold;
252
Mel Gorman75ef7182016-07-28 15:45:24 -0700253 /* Zero current pgdat thresholds */
254 for_each_online_pgdat(pgdat) {
255 for_each_online_cpu(cpu) {
256 per_cpu_ptr(pgdat->per_cpu_nodestats, cpu)->stat_threshold = 0;
257 }
258 }
259
KOSAKI Motohiroee99c712009-03-31 15:19:31 -0700260 for_each_populated_zone(zone) {
Mel Gorman75ef7182016-07-28 15:45:24 -0700261 struct pglist_data *pgdat = zone->zone_pgdat;
Christoph Lameteraa454842010-09-09 16:38:17 -0700262 unsigned long max_drift, tolerate_drift;
263
Mel Gormanb44129b2011-01-13 15:45:43 -0800264 threshold = calculate_normal_threshold(zone);
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700265
Mel Gorman75ef7182016-07-28 15:45:24 -0700266 for_each_online_cpu(cpu) {
267 int pgdat_threshold;
268
Christoph Lameter99dcc3e2010-01-05 15:34:51 +0900269 per_cpu_ptr(zone->pageset, cpu)->stat_threshold
270 = threshold;
Kemi Wang1d90ca82017-09-08 16:12:52 -0700271
Mel Gorman75ef7182016-07-28 15:45:24 -0700272 /* Base nodestat threshold on the largest populated zone. */
273 pgdat_threshold = per_cpu_ptr(pgdat->per_cpu_nodestats, cpu)->stat_threshold;
274 per_cpu_ptr(pgdat->per_cpu_nodestats, cpu)->stat_threshold
275 = max(threshold, pgdat_threshold);
276 }
277
Christoph Lameteraa454842010-09-09 16:38:17 -0700278 /*
279 * Only set percpu_drift_mark if there is a danger that
280 * NR_FREE_PAGES reports the low watermark is ok when in fact
281 * the min watermark could be breached by an allocation
282 */
283 tolerate_drift = low_wmark_pages(zone) - min_wmark_pages(zone);
284 max_drift = num_online_cpus() * threshold;
285 if (max_drift > tolerate_drift)
286 zone->percpu_drift_mark = high_wmark_pages(zone) +
287 max_drift;
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700288 }
Christoph Lameter2244b952006-06-30 01:55:33 -0700289}
290
Mel Gormanb44129b2011-01-13 15:45:43 -0800291void set_pgdat_percpu_threshold(pg_data_t *pgdat,
292 int (*calculate_pressure)(struct zone *))
Mel Gorman88f5acf2011-01-13 15:45:41 -0800293{
294 struct zone *zone;
295 int cpu;
296 int threshold;
297 int i;
298
Mel Gorman88f5acf2011-01-13 15:45:41 -0800299 for (i = 0; i < pgdat->nr_zones; i++) {
300 zone = &pgdat->node_zones[i];
301 if (!zone->percpu_drift_mark)
302 continue;
303
Mel Gormanb44129b2011-01-13 15:45:43 -0800304 threshold = (*calculate_pressure)(zone);
Kemi Wang1d90ca82017-09-08 16:12:52 -0700305 for_each_online_cpu(cpu)
Mel Gorman88f5acf2011-01-13 15:45:41 -0800306 per_cpu_ptr(zone->pageset, cpu)->stat_threshold
307 = threshold;
308 }
Mel Gorman88f5acf2011-01-13 15:45:41 -0800309}
310
Christoph Lameter2244b952006-06-30 01:55:33 -0700311/*
Jianyu Zhanbea04b02014-06-04 16:09:51 -0700312 * For use when we know that interrupts are disabled,
313 * or when we know that preemption is disabled and that
314 * particular counter cannot be updated from interrupt context.
Christoph Lameter2244b952006-06-30 01:55:33 -0700315 */
316void __mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
Heiko Carstens6cdb18a2015-12-29 14:54:32 -0800317 long delta)
Christoph Lameter2244b952006-06-30 01:55:33 -0700318{
Christoph Lameter12938a92010-12-06 11:16:20 -0600319 struct per_cpu_pageset __percpu *pcp = zone->pageset;
320 s8 __percpu *p = pcp->vm_stat_diff + item;
Christoph Lameter2244b952006-06-30 01:55:33 -0700321 long x;
Christoph Lameter12938a92010-12-06 11:16:20 -0600322 long t;
Christoph Lameter2244b952006-06-30 01:55:33 -0700323
Christoph Lameter12938a92010-12-06 11:16:20 -0600324 x = delta + __this_cpu_read(*p);
Christoph Lameter2244b952006-06-30 01:55:33 -0700325
Christoph Lameter12938a92010-12-06 11:16:20 -0600326 t = __this_cpu_read(pcp->stat_threshold);
327
Miaohe Lin40610072020-10-15 20:07:36 -0700328 if (unlikely(abs(x) > t)) {
Christoph Lameter2244b952006-06-30 01:55:33 -0700329 zone_page_state_add(x, zone, item);
330 x = 0;
331 }
Christoph Lameter12938a92010-12-06 11:16:20 -0600332 __this_cpu_write(*p, x);
Christoph Lameter2244b952006-06-30 01:55:33 -0700333}
334EXPORT_SYMBOL(__mod_zone_page_state);
335
Mel Gorman75ef7182016-07-28 15:45:24 -0700336void __mod_node_page_state(struct pglist_data *pgdat, enum node_stat_item item,
337 long delta)
338{
339 struct per_cpu_nodestat __percpu *pcp = pgdat->per_cpu_nodestats;
340 s8 __percpu *p = pcp->vm_node_stat_diff + item;
341 long x;
342 long t;
343
Roman Gushchinea426c22020-08-06 23:20:35 -0700344 if (vmstat_item_in_bytes(item)) {
345 VM_WARN_ON_ONCE(delta & (PAGE_SIZE - 1));
346 delta >>= PAGE_SHIFT;
347 }
348
Mel Gorman75ef7182016-07-28 15:45:24 -0700349 x = delta + __this_cpu_read(*p);
350
351 t = __this_cpu_read(pcp->stat_threshold);
352
Miaohe Lin40610072020-10-15 20:07:36 -0700353 if (unlikely(abs(x) > t)) {
Mel Gorman75ef7182016-07-28 15:45:24 -0700354 node_page_state_add(x, pgdat, item);
355 x = 0;
356 }
357 __this_cpu_write(*p, x);
358}
359EXPORT_SYMBOL(__mod_node_page_state);
360
Christoph Lameter2244b952006-06-30 01:55:33 -0700361/*
Christoph Lameter2244b952006-06-30 01:55:33 -0700362 * Optimized increment and decrement functions.
363 *
364 * These are only for a single page and therefore can take a struct page *
365 * argument instead of struct zone *. This allows the inclusion of the code
366 * generated for page_zone(page) into the optimized functions.
367 *
368 * No overflow check is necessary and therefore the differential can be
369 * incremented or decremented in place which may allow the compilers to
370 * generate better code.
Christoph Lameter2244b952006-06-30 01:55:33 -0700371 * The increment or decrement is known and therefore one boundary check can
372 * be omitted.
373 *
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700374 * NOTE: These functions are very performance sensitive. Change only
375 * with care.
376 *
Christoph Lameter2244b952006-06-30 01:55:33 -0700377 * Some processors have inc/dec instructions that are atomic vs an interrupt.
378 * However, the code must first determine the differential location in a zone
379 * based on the processor number and then inc/dec the counter. There is no
380 * guarantee without disabling preemption that the processor will not change
381 * in between and therefore the atomicity vs. interrupt cannot be exploited
382 * in a useful way here.
383 */
Christoph Lameterc8785382007-02-10 01:43:01 -0800384void __inc_zone_state(struct zone *zone, enum zone_stat_item item)
Christoph Lameter2244b952006-06-30 01:55:33 -0700385{
Christoph Lameter12938a92010-12-06 11:16:20 -0600386 struct per_cpu_pageset __percpu *pcp = zone->pageset;
387 s8 __percpu *p = pcp->vm_stat_diff + item;
388 s8 v, t;
Christoph Lameter2244b952006-06-30 01:55:33 -0700389
Christoph Lameter908ee0f2010-12-06 11:40:02 -0600390 v = __this_cpu_inc_return(*p);
Christoph Lameter12938a92010-12-06 11:16:20 -0600391 t = __this_cpu_read(pcp->stat_threshold);
392 if (unlikely(v > t)) {
393 s8 overstep = t >> 1;
Christoph Lameter2244b952006-06-30 01:55:33 -0700394
Christoph Lameter12938a92010-12-06 11:16:20 -0600395 zone_page_state_add(v + overstep, zone, item);
396 __this_cpu_write(*p, -overstep);
Christoph Lameter2244b952006-06-30 01:55:33 -0700397 }
398}
Christoph Lameterca889e62006-06-30 01:55:44 -0700399
Mel Gorman75ef7182016-07-28 15:45:24 -0700400void __inc_node_state(struct pglist_data *pgdat, enum node_stat_item item)
401{
402 struct per_cpu_nodestat __percpu *pcp = pgdat->per_cpu_nodestats;
403 s8 __percpu *p = pcp->vm_node_stat_diff + item;
404 s8 v, t;
405
Roman Gushchinea426c22020-08-06 23:20:35 -0700406 VM_WARN_ON_ONCE(vmstat_item_in_bytes(item));
407
Mel Gorman75ef7182016-07-28 15:45:24 -0700408 v = __this_cpu_inc_return(*p);
409 t = __this_cpu_read(pcp->stat_threshold);
410 if (unlikely(v > t)) {
411 s8 overstep = t >> 1;
412
413 node_page_state_add(v + overstep, pgdat, item);
414 __this_cpu_write(*p, -overstep);
415 }
416}
417
Christoph Lameterca889e62006-06-30 01:55:44 -0700418void __inc_zone_page_state(struct page *page, enum zone_stat_item item)
419{
420 __inc_zone_state(page_zone(page), item);
421}
Christoph Lameter2244b952006-06-30 01:55:33 -0700422EXPORT_SYMBOL(__inc_zone_page_state);
423
Mel Gorman75ef7182016-07-28 15:45:24 -0700424void __inc_node_page_state(struct page *page, enum node_stat_item item)
425{
426 __inc_node_state(page_pgdat(page), item);
427}
428EXPORT_SYMBOL(__inc_node_page_state);
429
Christoph Lameterc8785382007-02-10 01:43:01 -0800430void __dec_zone_state(struct zone *zone, enum zone_stat_item item)
Christoph Lameter2244b952006-06-30 01:55:33 -0700431{
Christoph Lameter12938a92010-12-06 11:16:20 -0600432 struct per_cpu_pageset __percpu *pcp = zone->pageset;
433 s8 __percpu *p = pcp->vm_stat_diff + item;
434 s8 v, t;
Christoph Lameter2244b952006-06-30 01:55:33 -0700435
Christoph Lameter908ee0f2010-12-06 11:40:02 -0600436 v = __this_cpu_dec_return(*p);
Christoph Lameter12938a92010-12-06 11:16:20 -0600437 t = __this_cpu_read(pcp->stat_threshold);
438 if (unlikely(v < - t)) {
439 s8 overstep = t >> 1;
Christoph Lameter2244b952006-06-30 01:55:33 -0700440
Christoph Lameter12938a92010-12-06 11:16:20 -0600441 zone_page_state_add(v - overstep, zone, item);
442 __this_cpu_write(*p, overstep);
Christoph Lameter2244b952006-06-30 01:55:33 -0700443 }
444}
Christoph Lameterc8785382007-02-10 01:43:01 -0800445
Mel Gorman75ef7182016-07-28 15:45:24 -0700446void __dec_node_state(struct pglist_data *pgdat, enum node_stat_item item)
447{
448 struct per_cpu_nodestat __percpu *pcp = pgdat->per_cpu_nodestats;
449 s8 __percpu *p = pcp->vm_node_stat_diff + item;
450 s8 v, t;
451
Roman Gushchinea426c22020-08-06 23:20:35 -0700452 VM_WARN_ON_ONCE(vmstat_item_in_bytes(item));
453
Mel Gorman75ef7182016-07-28 15:45:24 -0700454 v = __this_cpu_dec_return(*p);
455 t = __this_cpu_read(pcp->stat_threshold);
456 if (unlikely(v < - t)) {
457 s8 overstep = t >> 1;
458
459 node_page_state_add(v - overstep, pgdat, item);
460 __this_cpu_write(*p, overstep);
461 }
462}
463
Christoph Lameterc8785382007-02-10 01:43:01 -0800464void __dec_zone_page_state(struct page *page, enum zone_stat_item item)
465{
466 __dec_zone_state(page_zone(page), item);
467}
Christoph Lameter2244b952006-06-30 01:55:33 -0700468EXPORT_SYMBOL(__dec_zone_page_state);
469
Mel Gorman75ef7182016-07-28 15:45:24 -0700470void __dec_node_page_state(struct page *page, enum node_stat_item item)
471{
472 __dec_node_state(page_pgdat(page), item);
473}
474EXPORT_SYMBOL(__dec_node_page_state);
475
Heiko Carstens41561532012-01-12 17:17:30 -0800476#ifdef CONFIG_HAVE_CMPXCHG_LOCAL
Christoph Lameter7c839122010-12-14 10:28:46 -0600477/*
478 * If we have cmpxchg_local support then we do not need to incur the overhead
479 * that comes with local_irq_save/restore if we use this_cpu_cmpxchg.
480 *
481 * mod_state() modifies the zone counter state through atomic per cpu
482 * operations.
483 *
484 * Overstep mode specifies how overstep should handled:
485 * 0 No overstepping
486 * 1 Overstepping half of threshold
487 * -1 Overstepping minus half of threshold
488*/
Mel Gorman75ef7182016-07-28 15:45:24 -0700489static inline void mod_zone_state(struct zone *zone,
490 enum zone_stat_item item, long delta, int overstep_mode)
Christoph Lameter7c839122010-12-14 10:28:46 -0600491{
492 struct per_cpu_pageset __percpu *pcp = zone->pageset;
493 s8 __percpu *p = pcp->vm_stat_diff + item;
494 long o, n, t, z;
495
496 do {
497 z = 0; /* overflow to zone counters */
498
499 /*
500 * The fetching of the stat_threshold is racy. We may apply
501 * a counter threshold to the wrong the cpu if we get
Christoph Lameterd3bc2362011-04-14 15:21:58 -0700502 * rescheduled while executing here. However, the next
503 * counter update will apply the threshold again and
504 * therefore bring the counter under the threshold again.
505 *
506 * Most of the time the thresholds are the same anyways
507 * for all cpus in a zone.
Christoph Lameter7c839122010-12-14 10:28:46 -0600508 */
509 t = this_cpu_read(pcp->stat_threshold);
510
511 o = this_cpu_read(*p);
512 n = delta + o;
513
Miaohe Lin40610072020-10-15 20:07:36 -0700514 if (abs(n) > t) {
Christoph Lameter7c839122010-12-14 10:28:46 -0600515 int os = overstep_mode * (t >> 1) ;
516
517 /* Overflow must be added to zone counters */
518 z = n + os;
519 n = -os;
520 }
521 } while (this_cpu_cmpxchg(*p, o, n) != o);
522
523 if (z)
524 zone_page_state_add(z, zone, item);
525}
526
527void mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
Heiko Carstens6cdb18a2015-12-29 14:54:32 -0800528 long delta)
Christoph Lameter7c839122010-12-14 10:28:46 -0600529{
Mel Gorman75ef7182016-07-28 15:45:24 -0700530 mod_zone_state(zone, item, delta, 0);
Christoph Lameter7c839122010-12-14 10:28:46 -0600531}
532EXPORT_SYMBOL(mod_zone_page_state);
533
Christoph Lameter7c839122010-12-14 10:28:46 -0600534void inc_zone_page_state(struct page *page, enum zone_stat_item item)
535{
Mel Gorman75ef7182016-07-28 15:45:24 -0700536 mod_zone_state(page_zone(page), item, 1, 1);
Christoph Lameter7c839122010-12-14 10:28:46 -0600537}
538EXPORT_SYMBOL(inc_zone_page_state);
539
540void dec_zone_page_state(struct page *page, enum zone_stat_item item)
541{
Mel Gorman75ef7182016-07-28 15:45:24 -0700542 mod_zone_state(page_zone(page), item, -1, -1);
Christoph Lameter7c839122010-12-14 10:28:46 -0600543}
544EXPORT_SYMBOL(dec_zone_page_state);
Mel Gorman75ef7182016-07-28 15:45:24 -0700545
546static inline void mod_node_state(struct pglist_data *pgdat,
547 enum node_stat_item item, int delta, int overstep_mode)
548{
549 struct per_cpu_nodestat __percpu *pcp = pgdat->per_cpu_nodestats;
550 s8 __percpu *p = pcp->vm_node_stat_diff + item;
551 long o, n, t, z;
552
Roman Gushchinea426c22020-08-06 23:20:35 -0700553 if (vmstat_item_in_bytes(item)) {
554 VM_WARN_ON_ONCE(delta & (PAGE_SIZE - 1));
555 delta >>= PAGE_SHIFT;
556 }
557
Mel Gorman75ef7182016-07-28 15:45:24 -0700558 do {
559 z = 0; /* overflow to node counters */
560
561 /*
562 * The fetching of the stat_threshold is racy. We may apply
563 * a counter threshold to the wrong the cpu if we get
564 * rescheduled while executing here. However, the next
565 * counter update will apply the threshold again and
566 * therefore bring the counter under the threshold again.
567 *
568 * Most of the time the thresholds are the same anyways
569 * for all cpus in a node.
570 */
571 t = this_cpu_read(pcp->stat_threshold);
572
573 o = this_cpu_read(*p);
574 n = delta + o;
575
Miaohe Lin40610072020-10-15 20:07:36 -0700576 if (abs(n) > t) {
Mel Gorman75ef7182016-07-28 15:45:24 -0700577 int os = overstep_mode * (t >> 1) ;
578
579 /* Overflow must be added to node counters */
580 z = n + os;
581 n = -os;
582 }
583 } while (this_cpu_cmpxchg(*p, o, n) != o);
584
585 if (z)
586 node_page_state_add(z, pgdat, item);
587}
588
589void mod_node_page_state(struct pglist_data *pgdat, enum node_stat_item item,
590 long delta)
591{
592 mod_node_state(pgdat, item, delta, 0);
593}
594EXPORT_SYMBOL(mod_node_page_state);
595
596void inc_node_state(struct pglist_data *pgdat, enum node_stat_item item)
597{
598 mod_node_state(pgdat, item, 1, 1);
599}
600
601void inc_node_page_state(struct page *page, enum node_stat_item item)
602{
603 mod_node_state(page_pgdat(page), item, 1, 1);
604}
605EXPORT_SYMBOL(inc_node_page_state);
606
607void dec_node_page_state(struct page *page, enum node_stat_item item)
608{
609 mod_node_state(page_pgdat(page), item, -1, -1);
610}
611EXPORT_SYMBOL(dec_node_page_state);
Christoph Lameter7c839122010-12-14 10:28:46 -0600612#else
613/*
614 * Use interrupt disable to serialize counter updates
615 */
616void mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
Heiko Carstens6cdb18a2015-12-29 14:54:32 -0800617 long delta)
Christoph Lameter7c839122010-12-14 10:28:46 -0600618{
619 unsigned long flags;
620
621 local_irq_save(flags);
622 __mod_zone_page_state(zone, item, delta);
623 local_irq_restore(flags);
624}
625EXPORT_SYMBOL(mod_zone_page_state);
626
Christoph Lameter2244b952006-06-30 01:55:33 -0700627void inc_zone_page_state(struct page *page, enum zone_stat_item item)
628{
629 unsigned long flags;
630 struct zone *zone;
Christoph Lameter2244b952006-06-30 01:55:33 -0700631
632 zone = page_zone(page);
633 local_irq_save(flags);
Christoph Lameterca889e62006-06-30 01:55:44 -0700634 __inc_zone_state(zone, item);
Christoph Lameter2244b952006-06-30 01:55:33 -0700635 local_irq_restore(flags);
636}
637EXPORT_SYMBOL(inc_zone_page_state);
638
639void dec_zone_page_state(struct page *page, enum zone_stat_item item)
640{
641 unsigned long flags;
Christoph Lameter2244b952006-06-30 01:55:33 -0700642
Christoph Lameter2244b952006-06-30 01:55:33 -0700643 local_irq_save(flags);
Christoph Lametera302eb42006-08-31 21:27:34 -0700644 __dec_zone_page_state(page, item);
Christoph Lameter2244b952006-06-30 01:55:33 -0700645 local_irq_restore(flags);
646}
647EXPORT_SYMBOL(dec_zone_page_state);
648
Mel Gorman75ef7182016-07-28 15:45:24 -0700649void inc_node_state(struct pglist_data *pgdat, enum node_stat_item item)
650{
651 unsigned long flags;
652
653 local_irq_save(flags);
654 __inc_node_state(pgdat, item);
655 local_irq_restore(flags);
656}
657EXPORT_SYMBOL(inc_node_state);
658
659void mod_node_page_state(struct pglist_data *pgdat, enum node_stat_item item,
660 long delta)
661{
662 unsigned long flags;
663
664 local_irq_save(flags);
665 __mod_node_page_state(pgdat, item, delta);
666 local_irq_restore(flags);
667}
668EXPORT_SYMBOL(mod_node_page_state);
669
670void inc_node_page_state(struct page *page, enum node_stat_item item)
671{
672 unsigned long flags;
673 struct pglist_data *pgdat;
674
675 pgdat = page_pgdat(page);
676 local_irq_save(flags);
677 __inc_node_state(pgdat, item);
678 local_irq_restore(flags);
679}
680EXPORT_SYMBOL(inc_node_page_state);
681
682void dec_node_page_state(struct page *page, enum node_stat_item item)
683{
684 unsigned long flags;
685
686 local_irq_save(flags);
687 __dec_node_page_state(page, item);
688 local_irq_restore(flags);
689}
690EXPORT_SYMBOL(dec_node_page_state);
691#endif
Christoph Lameter7cc36bb2014-10-09 15:29:43 -0700692
693/*
694 * Fold a differential into the global counters.
695 * Returns the number of counters updated.
696 */
Kemi Wang3a321d22017-09-08 16:12:48 -0700697#ifdef CONFIG_NUMA
698static int fold_diff(int *zone_diff, int *numa_diff, int *node_diff)
699{
700 int i;
701 int changes = 0;
702
703 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
704 if (zone_diff[i]) {
705 atomic_long_add(zone_diff[i], &vm_zone_stat[i]);
706 changes++;
707 }
708
709 for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++)
710 if (numa_diff[i]) {
711 atomic_long_add(numa_diff[i], &vm_numa_stat[i]);
712 changes++;
713 }
714
715 for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++)
716 if (node_diff[i]) {
717 atomic_long_add(node_diff[i], &vm_node_stat[i]);
718 changes++;
719 }
720 return changes;
721}
722#else
Mel Gorman75ef7182016-07-28 15:45:24 -0700723static int fold_diff(int *zone_diff, int *node_diff)
Christoph Lameter4edb0742013-09-11 14:21:31 -0700724{
725 int i;
Christoph Lameter7cc36bb2014-10-09 15:29:43 -0700726 int changes = 0;
Christoph Lameter4edb0742013-09-11 14:21:31 -0700727
728 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
Mel Gorman75ef7182016-07-28 15:45:24 -0700729 if (zone_diff[i]) {
730 atomic_long_add(zone_diff[i], &vm_zone_stat[i]);
731 changes++;
732 }
733
734 for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++)
735 if (node_diff[i]) {
736 atomic_long_add(node_diff[i], &vm_node_stat[i]);
Christoph Lameter7cc36bb2014-10-09 15:29:43 -0700737 changes++;
738 }
739 return changes;
Christoph Lameter4edb0742013-09-11 14:21:31 -0700740}
Kemi Wang3a321d22017-09-08 16:12:48 -0700741#endif /* CONFIG_NUMA */
Christoph Lameter4edb0742013-09-11 14:21:31 -0700742
Christoph Lameter2244b952006-06-30 01:55:33 -0700743/*
Christoph Lameter2bb921e2013-09-11 14:21:30 -0700744 * Update the zone counters for the current cpu.
Christoph Lametera7f75e22008-02-04 22:29:16 -0800745 *
Christoph Lameter4037d452007-05-09 02:35:14 -0700746 * Note that refresh_cpu_vm_stats strives to only access
747 * node local memory. The per cpu pagesets on remote zones are placed
748 * in the memory local to the processor using that pageset. So the
749 * loop over all zones will access a series of cachelines local to
750 * the processor.
751 *
752 * The call to zone_page_state_add updates the cachelines with the
753 * statistics in the remote zone struct as well as the global cachelines
754 * with the global counters. These could cause remote node cache line
755 * bouncing and will have to be only done when necessary.
Christoph Lameter7cc36bb2014-10-09 15:29:43 -0700756 *
757 * The function returns the number of global counters updated.
Christoph Lameter2244b952006-06-30 01:55:33 -0700758 */
Christoph Lameter0eb77e92016-01-14 15:21:40 -0800759static int refresh_cpu_vm_stats(bool do_pagesets)
Christoph Lameter2244b952006-06-30 01:55:33 -0700760{
Mel Gorman75ef7182016-07-28 15:45:24 -0700761 struct pglist_data *pgdat;
Christoph Lameter2244b952006-06-30 01:55:33 -0700762 struct zone *zone;
763 int i;
Mel Gorman75ef7182016-07-28 15:45:24 -0700764 int global_zone_diff[NR_VM_ZONE_STAT_ITEMS] = { 0, };
Kemi Wang3a321d22017-09-08 16:12:48 -0700765#ifdef CONFIG_NUMA
766 int global_numa_diff[NR_VM_NUMA_STAT_ITEMS] = { 0, };
767#endif
Mel Gorman75ef7182016-07-28 15:45:24 -0700768 int global_node_diff[NR_VM_NODE_STAT_ITEMS] = { 0, };
Christoph Lameter7cc36bb2014-10-09 15:29:43 -0700769 int changes = 0;
Christoph Lameter2244b952006-06-30 01:55:33 -0700770
KOSAKI Motohiroee99c712009-03-31 15:19:31 -0700771 for_each_populated_zone(zone) {
Christoph Lameterfbc2edb2013-09-11 14:21:32 -0700772 struct per_cpu_pageset __percpu *p = zone->pageset;
Christoph Lameter2244b952006-06-30 01:55:33 -0700773
Christoph Lameterfbc2edb2013-09-11 14:21:32 -0700774 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++) {
775 int v;
Christoph Lameter2244b952006-06-30 01:55:33 -0700776
Christoph Lameterfbc2edb2013-09-11 14:21:32 -0700777 v = this_cpu_xchg(p->vm_stat_diff[i], 0);
778 if (v) {
Christoph Lametera7f75e22008-02-04 22:29:16 -0800779
Christoph Lametera7f75e22008-02-04 22:29:16 -0800780 atomic_long_add(v, &zone->vm_stat[i]);
Mel Gorman75ef7182016-07-28 15:45:24 -0700781 global_zone_diff[i] += v;
Christoph Lameter4037d452007-05-09 02:35:14 -0700782#ifdef CONFIG_NUMA
783 /* 3 seconds idle till flush */
Christoph Lameterfbc2edb2013-09-11 14:21:32 -0700784 __this_cpu_write(p->expire, 3);
Christoph Lameter4037d452007-05-09 02:35:14 -0700785#endif
Christoph Lameter2244b952006-06-30 01:55:33 -0700786 }
Christoph Lameterfbc2edb2013-09-11 14:21:32 -0700787 }
Christoph Lameter4037d452007-05-09 02:35:14 -0700788#ifdef CONFIG_NUMA
Kemi Wang3a321d22017-09-08 16:12:48 -0700789 for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++) {
790 int v;
791
792 v = this_cpu_xchg(p->vm_numa_stat_diff[i], 0);
793 if (v) {
794
795 atomic_long_add(v, &zone->vm_numa_stat[i]);
796 global_numa_diff[i] += v;
797 __this_cpu_write(p->expire, 3);
798 }
799 }
800
Christoph Lameter0eb77e92016-01-14 15:21:40 -0800801 if (do_pagesets) {
802 cond_resched();
803 /*
804 * Deal with draining the remote pageset of this
805 * processor
806 *
807 * Check if there are pages remaining in this pageset
808 * if not then there is nothing to expire.
809 */
810 if (!__this_cpu_read(p->expire) ||
Christoph Lameterfbc2edb2013-09-11 14:21:32 -0700811 !__this_cpu_read(p->pcp.count))
Christoph Lameter0eb77e92016-01-14 15:21:40 -0800812 continue;
Christoph Lameter4037d452007-05-09 02:35:14 -0700813
Christoph Lameter0eb77e92016-01-14 15:21:40 -0800814 /*
815 * We never drain zones local to this processor.
816 */
817 if (zone_to_nid(zone) == numa_node_id()) {
818 __this_cpu_write(p->expire, 0);
819 continue;
820 }
Christoph Lameter4037d452007-05-09 02:35:14 -0700821
Christoph Lameter0eb77e92016-01-14 15:21:40 -0800822 if (__this_cpu_dec_return(p->expire))
823 continue;
Christoph Lameter4037d452007-05-09 02:35:14 -0700824
Christoph Lameter0eb77e92016-01-14 15:21:40 -0800825 if (__this_cpu_read(p->pcp.count)) {
826 drain_zone_pages(zone, this_cpu_ptr(&p->pcp));
827 changes++;
828 }
Christoph Lameter7cc36bb2014-10-09 15:29:43 -0700829 }
Christoph Lameter4037d452007-05-09 02:35:14 -0700830#endif
Christoph Lameter2244b952006-06-30 01:55:33 -0700831 }
Mel Gorman75ef7182016-07-28 15:45:24 -0700832
833 for_each_online_pgdat(pgdat) {
834 struct per_cpu_nodestat __percpu *p = pgdat->per_cpu_nodestats;
835
836 for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++) {
837 int v;
838
839 v = this_cpu_xchg(p->vm_node_stat_diff[i], 0);
840 if (v) {
841 atomic_long_add(v, &pgdat->vm_stat[i]);
842 global_node_diff[i] += v;
843 }
844 }
845 }
846
Kemi Wang3a321d22017-09-08 16:12:48 -0700847#ifdef CONFIG_NUMA
848 changes += fold_diff(global_zone_diff, global_numa_diff,
849 global_node_diff);
850#else
Mel Gorman75ef7182016-07-28 15:45:24 -0700851 changes += fold_diff(global_zone_diff, global_node_diff);
Kemi Wang3a321d22017-09-08 16:12:48 -0700852#endif
Christoph Lameter7cc36bb2014-10-09 15:29:43 -0700853 return changes;
Christoph Lameter2244b952006-06-30 01:55:33 -0700854}
855
Cody P Schafer40f4b1e2013-04-29 15:08:38 -0700856/*
Christoph Lameter2bb921e2013-09-11 14:21:30 -0700857 * Fold the data for an offline cpu into the global array.
858 * There cannot be any access by the offline cpu and therefore
859 * synchronization is simplified.
860 */
861void cpu_vm_stats_fold(int cpu)
862{
Mel Gorman75ef7182016-07-28 15:45:24 -0700863 struct pglist_data *pgdat;
Christoph Lameter2bb921e2013-09-11 14:21:30 -0700864 struct zone *zone;
865 int i;
Mel Gorman75ef7182016-07-28 15:45:24 -0700866 int global_zone_diff[NR_VM_ZONE_STAT_ITEMS] = { 0, };
Kemi Wang3a321d22017-09-08 16:12:48 -0700867#ifdef CONFIG_NUMA
868 int global_numa_diff[NR_VM_NUMA_STAT_ITEMS] = { 0, };
869#endif
Mel Gorman75ef7182016-07-28 15:45:24 -0700870 int global_node_diff[NR_VM_NODE_STAT_ITEMS] = { 0, };
Christoph Lameter2bb921e2013-09-11 14:21:30 -0700871
872 for_each_populated_zone(zone) {
873 struct per_cpu_pageset *p;
874
875 p = per_cpu_ptr(zone->pageset, cpu);
876
877 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
878 if (p->vm_stat_diff[i]) {
879 int v;
880
881 v = p->vm_stat_diff[i];
882 p->vm_stat_diff[i] = 0;
883 atomic_long_add(v, &zone->vm_stat[i]);
Mel Gorman75ef7182016-07-28 15:45:24 -0700884 global_zone_diff[i] += v;
Christoph Lameter2bb921e2013-09-11 14:21:30 -0700885 }
Kemi Wang3a321d22017-09-08 16:12:48 -0700886
887#ifdef CONFIG_NUMA
888 for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++)
889 if (p->vm_numa_stat_diff[i]) {
890 int v;
891
892 v = p->vm_numa_stat_diff[i];
893 p->vm_numa_stat_diff[i] = 0;
894 atomic_long_add(v, &zone->vm_numa_stat[i]);
895 global_numa_diff[i] += v;
896 }
897#endif
Christoph Lameter2bb921e2013-09-11 14:21:30 -0700898 }
899
Mel Gorman75ef7182016-07-28 15:45:24 -0700900 for_each_online_pgdat(pgdat) {
901 struct per_cpu_nodestat *p;
902
903 p = per_cpu_ptr(pgdat->per_cpu_nodestats, cpu);
904
905 for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++)
906 if (p->vm_node_stat_diff[i]) {
907 int v;
908
909 v = p->vm_node_stat_diff[i];
910 p->vm_node_stat_diff[i] = 0;
911 atomic_long_add(v, &pgdat->vm_stat[i]);
912 global_node_diff[i] += v;
913 }
914 }
915
Kemi Wang3a321d22017-09-08 16:12:48 -0700916#ifdef CONFIG_NUMA
917 fold_diff(global_zone_diff, global_numa_diff, global_node_diff);
918#else
Mel Gorman75ef7182016-07-28 15:45:24 -0700919 fold_diff(global_zone_diff, global_node_diff);
Kemi Wang3a321d22017-09-08 16:12:48 -0700920#endif
Christoph Lameter2bb921e2013-09-11 14:21:30 -0700921}
922
923/*
Cody P Schafer40f4b1e2013-04-29 15:08:38 -0700924 * this is only called if !populated_zone(zone), which implies no other users of
925 * pset->vm_stat_diff[] exsist.
926 */
Minchan Kim5a883812012-10-08 16:33:39 -0700927void drain_zonestat(struct zone *zone, struct per_cpu_pageset *pset)
928{
929 int i;
930
931 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
932 if (pset->vm_stat_diff[i]) {
933 int v = pset->vm_stat_diff[i];
934 pset->vm_stat_diff[i] = 0;
935 atomic_long_add(v, &zone->vm_stat[i]);
Mel Gorman75ef7182016-07-28 15:45:24 -0700936 atomic_long_add(v, &vm_zone_stat[i]);
Minchan Kim5a883812012-10-08 16:33:39 -0700937 }
Kemi Wang3a321d22017-09-08 16:12:48 -0700938
939#ifdef CONFIG_NUMA
940 for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++)
941 if (pset->vm_numa_stat_diff[i]) {
942 int v = pset->vm_numa_stat_diff[i];
943
944 pset->vm_numa_stat_diff[i] = 0;
945 atomic_long_add(v, &zone->vm_numa_stat[i]);
946 atomic_long_add(v, &vm_numa_stat[i]);
947 }
948#endif
Minchan Kim5a883812012-10-08 16:33:39 -0700949}
Christoph Lameter2244b952006-06-30 01:55:33 -0700950#endif
951
Christoph Lameterca889e62006-06-30 01:55:44 -0700952#ifdef CONFIG_NUMA
Kemi Wang3a321d22017-09-08 16:12:48 -0700953void __inc_numa_state(struct zone *zone,
954 enum numa_stat_item item)
955{
956 struct per_cpu_pageset __percpu *pcp = zone->pageset;
Kemi Wang1d90ca82017-09-08 16:12:52 -0700957 u16 __percpu *p = pcp->vm_numa_stat_diff + item;
958 u16 v;
Kemi Wang3a321d22017-09-08 16:12:48 -0700959
960 v = __this_cpu_inc_return(*p);
Kemi Wang3a321d22017-09-08 16:12:48 -0700961
Kemi Wang1d90ca82017-09-08 16:12:52 -0700962 if (unlikely(v > NUMA_STATS_THRESHOLD)) {
963 zone_numa_state_add(v, zone, item);
964 __this_cpu_write(*p, 0);
Kemi Wang3a321d22017-09-08 16:12:48 -0700965 }
966}
967
Christoph Lameterca889e62006-06-30 01:55:44 -0700968/*
Mel Gorman75ef7182016-07-28 15:45:24 -0700969 * Determine the per node value of a stat item. This function
970 * is called frequently in a NUMA machine, so try to be as
971 * frugal as possible.
Andrew Mortonc2d42c12015-11-05 18:48:43 -0800972 */
Mel Gorman75ef7182016-07-28 15:45:24 -0700973unsigned long sum_zone_node_page_state(int node,
974 enum zone_stat_item item)
Andrew Mortonc2d42c12015-11-05 18:48:43 -0800975{
976 struct zone *zones = NODE_DATA(node)->node_zones;
Joonsoo Kime87d59f2016-05-19 17:12:29 -0700977 int i;
978 unsigned long count = 0;
Andrew Mortonc2d42c12015-11-05 18:48:43 -0800979
Joonsoo Kime87d59f2016-05-19 17:12:29 -0700980 for (i = 0; i < MAX_NR_ZONES; i++)
981 count += zone_page_state(zones + i, item);
982
983 return count;
Andrew Mortonc2d42c12015-11-05 18:48:43 -0800984}
985
Kemi Wang63803222017-09-08 16:12:55 -0700986/*
987 * Determine the per node value of a numa stat item. To avoid deviation,
988 * the per cpu stat number in vm_numa_stat_diff[] is also included.
989 */
Kemi Wang3a321d22017-09-08 16:12:48 -0700990unsigned long sum_zone_numa_state(int node,
991 enum numa_stat_item item)
992{
993 struct zone *zones = NODE_DATA(node)->node_zones;
994 int i;
995 unsigned long count = 0;
996
997 for (i = 0; i < MAX_NR_ZONES; i++)
Kemi Wang63803222017-09-08 16:12:55 -0700998 count += zone_numa_state_snapshot(zones + i, item);
Kemi Wang3a321d22017-09-08 16:12:48 -0700999
1000 return count;
1001}
1002
Mel Gorman75ef7182016-07-28 15:45:24 -07001003/*
1004 * Determine the per node value of a stat item.
1005 */
Roman Gushchinea426c22020-08-06 23:20:35 -07001006unsigned long node_page_state_pages(struct pglist_data *pgdat,
1007 enum node_stat_item item)
Mel Gorman75ef7182016-07-28 15:45:24 -07001008{
1009 long x = atomic_long_read(&pgdat->vm_stat[item]);
1010#ifdef CONFIG_SMP
1011 if (x < 0)
1012 x = 0;
1013#endif
1014 return x;
1015}
Roman Gushchinea426c22020-08-06 23:20:35 -07001016
1017unsigned long node_page_state(struct pglist_data *pgdat,
1018 enum node_stat_item item)
1019{
1020 VM_WARN_ON_ONCE(vmstat_item_in_bytes(item));
1021
1022 return node_page_state_pages(pgdat, item);
1023}
Christoph Lameterca889e62006-06-30 01:55:44 -07001024#endif
1025
Mel Gormand7a57522010-05-24 14:32:25 -07001026#ifdef CONFIG_COMPACTION
Namhyung Kim36deb0b2010-10-26 14:22:04 -07001027
Mel Gormand7a57522010-05-24 14:32:25 -07001028struct contig_page_info {
1029 unsigned long free_pages;
1030 unsigned long free_blocks_total;
1031 unsigned long free_blocks_suitable;
1032};
1033
1034/*
1035 * Calculate the number of free pages in a zone, how many contiguous
1036 * pages are free and how many are large enough to satisfy an allocation of
1037 * the target size. Note that this function makes no attempt to estimate
1038 * how many suitable free blocks there *might* be if MOVABLE pages were
1039 * migrated. Calculating that is possible, but expensive and can be
1040 * figured out from userspace
1041 */
1042static void fill_contig_page_info(struct zone *zone,
1043 unsigned int suitable_order,
1044 struct contig_page_info *info)
1045{
1046 unsigned int order;
1047
1048 info->free_pages = 0;
1049 info->free_blocks_total = 0;
1050 info->free_blocks_suitable = 0;
1051
1052 for (order = 0; order < MAX_ORDER; order++) {
1053 unsigned long blocks;
1054
1055 /* Count number of free blocks */
1056 blocks = zone->free_area[order].nr_free;
1057 info->free_blocks_total += blocks;
1058
1059 /* Count free base pages */
1060 info->free_pages += blocks << order;
1061
1062 /* Count the suitable free blocks */
1063 if (order >= suitable_order)
1064 info->free_blocks_suitable += blocks <<
1065 (order - suitable_order);
1066 }
1067}
Mel Gormanf1a5ab12010-05-24 14:32:26 -07001068
1069/*
1070 * A fragmentation index only makes sense if an allocation of a requested
1071 * size would fail. If that is true, the fragmentation index indicates
1072 * whether external fragmentation or a lack of memory was the problem.
1073 * The value can be used to determine if page reclaim or compaction
1074 * should be used
1075 */
Mel Gorman56de7262010-05-24 14:32:30 -07001076static int __fragmentation_index(unsigned int order, struct contig_page_info *info)
Mel Gormanf1a5ab12010-05-24 14:32:26 -07001077{
1078 unsigned long requested = 1UL << order;
1079
Wen Yang88d6ac42017-09-06 16:24:06 -07001080 if (WARN_ON_ONCE(order >= MAX_ORDER))
1081 return 0;
1082
Mel Gormanf1a5ab12010-05-24 14:32:26 -07001083 if (!info->free_blocks_total)
1084 return 0;
1085
1086 /* Fragmentation index only makes sense when a request would fail */
1087 if (info->free_blocks_suitable)
1088 return -1000;
1089
1090 /*
1091 * Index is between 0 and 1 so return within 3 decimal places
1092 *
1093 * 0 => allocation would fail due to lack of memory
1094 * 1 => allocation would fail due to fragmentation
1095 */
1096 return 1000 - div_u64( (1000+(div_u64(info->free_pages * 1000ULL, requested))), info->free_blocks_total);
1097}
Mel Gorman56de7262010-05-24 14:32:30 -07001098
Nitin Guptafacdaa92020-08-11 18:31:00 -07001099/*
1100 * Calculates external fragmentation within a zone wrt the given order.
1101 * It is defined as the percentage of pages found in blocks of size
1102 * less than 1 << order. It returns values in range [0, 100].
1103 */
Nitin Guptad34c0a72020-08-11 18:31:07 -07001104unsigned int extfrag_for_order(struct zone *zone, unsigned int order)
Nitin Guptafacdaa92020-08-11 18:31:00 -07001105{
1106 struct contig_page_info info;
1107
1108 fill_contig_page_info(zone, order, &info);
1109 if (info.free_pages == 0)
1110 return 0;
1111
1112 return div_u64((info.free_pages -
1113 (info.free_blocks_suitable << order)) * 100,
1114 info.free_pages);
1115}
1116
Mel Gorman56de7262010-05-24 14:32:30 -07001117/* Same as __fragmentation index but allocs contig_page_info on stack */
1118int fragmentation_index(struct zone *zone, unsigned int order)
1119{
1120 struct contig_page_info info;
1121
1122 fill_contig_page_info(zone, order, &info);
1123 return __fragmentation_index(order, &info);
1124}
Mel Gormand7a57522010-05-24 14:32:25 -07001125#endif
1126
Konstantin Khlebnikovebc5d83d2019-12-04 16:49:53 -08001127#if defined(CONFIG_PROC_FS) || defined(CONFIG_SYSFS) || \
1128 defined(CONFIG_NUMA) || defined(CONFIG_MEMCG)
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001129#ifdef CONFIG_ZONE_DMA
1130#define TEXT_FOR_DMA(xx) xx "_dma",
1131#else
1132#define TEXT_FOR_DMA(xx)
1133#endif
1134
1135#ifdef CONFIG_ZONE_DMA32
1136#define TEXT_FOR_DMA32(xx) xx "_dma32",
1137#else
1138#define TEXT_FOR_DMA32(xx)
1139#endif
1140
1141#ifdef CONFIG_HIGHMEM
1142#define TEXT_FOR_HIGHMEM(xx) xx "_high",
1143#else
1144#define TEXT_FOR_HIGHMEM(xx)
1145#endif
1146
1147#define TEXTS_FOR_ZONES(xx) TEXT_FOR_DMA(xx) TEXT_FOR_DMA32(xx) xx "_normal", \
1148 TEXT_FOR_HIGHMEM(xx) xx "_movable",
1149
1150const char * const vmstat_text[] = {
NeilBrown8d928902020-06-01 21:48:21 -07001151 /* enum zone_stat_item counters */
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001152 "nr_free_pages",
Minchan Kim71c799f2016-07-28 15:47:26 -07001153 "nr_zone_inactive_anon",
1154 "nr_zone_active_anon",
1155 "nr_zone_inactive_file",
1156 "nr_zone_active_file",
1157 "nr_zone_unevictable",
Mel Gorman5a1c84b2016-07-28 15:47:31 -07001158 "nr_zone_write_pending",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001159 "nr_mlock",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001160 "nr_page_table_pages",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001161 "nr_bounce",
Minchan Kim91537fe2016-07-26 15:24:45 -07001162 "nr_zspages",
Kemi Wang3a321d22017-09-08 16:12:48 -07001163 "nr_free_cma",
1164
1165 /* enum numa_stat_item counters */
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001166#ifdef CONFIG_NUMA
1167 "numa_hit",
1168 "numa_miss",
1169 "numa_foreign",
1170 "numa_interleave",
1171 "numa_local",
1172 "numa_other",
1173#endif
Konstantin Khlebnikov09316c02014-10-09 15:29:32 -07001174
Konstantin Khlebnikov9d7ea9a2019-12-04 16:49:50 -08001175 /* enum node_stat_item counters */
Mel Gorman599d0c92016-07-28 15:45:31 -07001176 "nr_inactive_anon",
1177 "nr_active_anon",
1178 "nr_inactive_file",
1179 "nr_active_file",
1180 "nr_unevictable",
Johannes Weiner385386c2017-07-06 15:40:43 -07001181 "nr_slab_reclaimable",
1182 "nr_slab_unreclaimable",
Mel Gorman599d0c92016-07-28 15:45:31 -07001183 "nr_isolated_anon",
1184 "nr_isolated_file",
Johannes Weiner68d48e62018-10-26 15:06:39 -07001185 "workingset_nodes",
Joonsoo Kim170b04b72020-08-11 18:30:43 -07001186 "workingset_refault_anon",
1187 "workingset_refault_file",
1188 "workingset_activate_anon",
1189 "workingset_activate_file",
1190 "workingset_restore_anon",
1191 "workingset_restore_file",
Mel Gorman1e6b10852016-07-28 15:46:08 -07001192 "workingset_nodereclaim",
Mel Gorman50658e22016-07-28 15:46:14 -07001193 "nr_anon_pages",
1194 "nr_mapped",
Mel Gorman11fb9982016-07-28 15:46:20 -07001195 "nr_file_pages",
1196 "nr_dirty",
1197 "nr_writeback",
1198 "nr_writeback_temp",
1199 "nr_shmem",
1200 "nr_shmem_hugepages",
1201 "nr_shmem_pmdmapped",
Song Liu60fbf0a2019-09-23 15:37:54 -07001202 "nr_file_hugepages",
1203 "nr_file_pmdmapped",
Mel Gorman11fb9982016-07-28 15:46:20 -07001204 "nr_anon_transparent_hugepages",
Mel Gormanc4a25632016-07-28 15:46:23 -07001205 "nr_vmscan_write",
1206 "nr_vmscan_immediate_reclaim",
1207 "nr_dirtied",
1208 "nr_written",
Vlastimil Babkab29940c2018-10-26 15:05:46 -07001209 "nr_kernel_misc_reclaimable",
John Hubbard1970dc62020-04-01 21:05:37 -07001210 "nr_foll_pin_acquired",
1211 "nr_foll_pin_released",
Shakeel Butt991e7672020-08-06 23:21:37 -07001212 "nr_kernel_stack",
1213#if IS_ENABLED(CONFIG_SHADOW_CALL_STACK)
1214 "nr_shadow_call_stack",
1215#endif
Mel Gorman599d0c92016-07-28 15:45:31 -07001216
Konstantin Khlebnikov09316c02014-10-09 15:29:32 -07001217 /* enum writeback_stat_item counters */
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001218 "nr_dirty_threshold",
1219 "nr_dirty_background_threshold",
1220
Konstantin Khlebnikovebc5d83d2019-12-04 16:49:53 -08001221#if defined(CONFIG_VM_EVENT_COUNTERS) || defined(CONFIG_MEMCG)
Konstantin Khlebnikov09316c02014-10-09 15:29:32 -07001222 /* enum vm_event_item counters */
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001223 "pgpgin",
1224 "pgpgout",
1225 "pswpin",
1226 "pswpout",
1227
1228 TEXTS_FOR_ZONES("pgalloc")
Mel Gorman7cc30fc2016-07-28 15:46:59 -07001229 TEXTS_FOR_ZONES("allocstall")
1230 TEXTS_FOR_ZONES("pgskip")
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001231
1232 "pgfree",
1233 "pgactivate",
1234 "pgdeactivate",
Shaohua Lif7ad2a62017-05-03 14:52:29 -07001235 "pglazyfree",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001236
1237 "pgfault",
1238 "pgmajfault",
Minchan Kim854e9ed2016-01-15 16:54:53 -08001239 "pglazyfreed",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001240
Mel Gorman599d0c92016-07-28 15:45:31 -07001241 "pgrefill",
Peter Xu798a6b82020-08-21 19:49:58 -04001242 "pgreuse",
Mel Gorman599d0c92016-07-28 15:45:31 -07001243 "pgsteal_kswapd",
1244 "pgsteal_direct",
1245 "pgscan_kswapd",
1246 "pgscan_direct",
Mel Gorman68243e72012-07-31 16:44:39 -07001247 "pgscan_direct_throttle",
Johannes Weiner497a6c12020-06-03 16:02:34 -07001248 "pgscan_anon",
1249 "pgscan_file",
1250 "pgsteal_anon",
1251 "pgsteal_file",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001252
1253#ifdef CONFIG_NUMA
1254 "zone_reclaim_failed",
1255#endif
1256 "pginodesteal",
1257 "slabs_scanned",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001258 "kswapd_inodesteal",
1259 "kswapd_low_wmark_hit_quickly",
1260 "kswapd_high_wmark_hit_quickly",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001261 "pageoutrun",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001262
1263 "pgrotated",
1264
Dave Hansen5509a5d2014-04-03 14:48:19 -07001265 "drop_pagecache",
1266 "drop_slab",
Konstantin Khlebnikov8e675f72017-07-06 15:40:28 -07001267 "oom_kill",
Dave Hansen5509a5d2014-04-03 14:48:19 -07001268
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001269#ifdef CONFIG_NUMA_BALANCING
1270 "numa_pte_updates",
Mel Gorman72403b42013-11-12 15:08:32 -08001271 "numa_huge_pte_updates",
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001272 "numa_hint_faults",
1273 "numa_hint_faults_local",
1274 "numa_pages_migrated",
1275#endif
Mel Gorman5647bc22012-10-19 10:46:20 +01001276#ifdef CONFIG_MIGRATION
1277 "pgmigrate_success",
1278 "pgmigrate_fail",
Anshuman Khandual1a5bae22020-08-11 18:31:51 -07001279 "thp_migration_success",
1280 "thp_migration_fail",
1281 "thp_migration_split",
Mel Gorman5647bc22012-10-19 10:46:20 +01001282#endif
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001283#ifdef CONFIG_COMPACTION
Mel Gorman397487d2012-10-19 12:00:10 +01001284 "compact_migrate_scanned",
1285 "compact_free_scanned",
1286 "compact_isolated",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001287 "compact_stall",
1288 "compact_fail",
1289 "compact_success",
Vlastimil Babka698b1b32016-03-17 14:18:08 -07001290 "compact_daemon_wake",
David Rientjes7f354a52017-02-22 15:44:50 -08001291 "compact_daemon_migrate_scanned",
1292 "compact_daemon_free_scanned",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001293#endif
1294
1295#ifdef CONFIG_HUGETLB_PAGE
1296 "htlb_buddy_alloc_success",
1297 "htlb_buddy_alloc_fail",
1298#endif
1299 "unevictable_pgs_culled",
1300 "unevictable_pgs_scanned",
1301 "unevictable_pgs_rescued",
1302 "unevictable_pgs_mlocked",
1303 "unevictable_pgs_munlocked",
1304 "unevictable_pgs_cleared",
1305 "unevictable_pgs_stranded",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001306
1307#ifdef CONFIG_TRANSPARENT_HUGEPAGE
1308 "thp_fault_alloc",
1309 "thp_fault_fallback",
David Rientjes85b9f462020-04-06 20:04:28 -07001310 "thp_fault_fallback_charge",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001311 "thp_collapse_alloc",
1312 "thp_collapse_alloc_failed",
Kirill A. Shutemov95ecedc2016-07-26 15:25:31 -07001313 "thp_file_alloc",
David Rientjesdcdf11e2020-04-06 20:04:25 -07001314 "thp_file_fallback",
David Rientjes85b9f462020-04-06 20:04:28 -07001315 "thp_file_fallback_charge",
Kirill A. Shutemov95ecedc2016-07-26 15:25:31 -07001316 "thp_file_mapped",
Kirill A. Shutemov122afea2016-01-15 16:52:46 -08001317 "thp_split_page",
1318 "thp_split_page_failed",
Kirill A. Shutemovf9719a02016-03-17 14:18:45 -07001319 "thp_deferred_split_page",
Kirill A. Shutemov122afea2016-01-15 16:52:46 -08001320 "thp_split_pmd",
Yisheng Xiece9311c2017-03-09 16:17:00 -08001321#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
1322 "thp_split_pud",
1323#endif
Kirill A. Shutemovd8a8e1f2012-12-12 13:51:09 -08001324 "thp_zero_page_alloc",
1325 "thp_zero_page_alloc_failed",
Huang Ying225311a2017-09-06 16:22:30 -07001326 "thp_swpout",
Huang Yingfe490cc2017-09-06 16:22:52 -07001327 "thp_swpout_fallback",
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001328#endif
Konstantin Khlebnikov09316c02014-10-09 15:29:32 -07001329#ifdef CONFIG_MEMORY_BALLOON
1330 "balloon_inflate",
1331 "balloon_deflate",
1332#ifdef CONFIG_BALLOON_COMPACTION
1333 "balloon_migrate",
1334#endif
1335#endif /* CONFIG_MEMORY_BALLOON */
Mel Gormanec659932014-01-21 14:33:16 -08001336#ifdef CONFIG_DEBUG_TLBFLUSH
Dave Hansen9824cf92013-09-11 14:20:23 -07001337 "nr_tlb_remote_flush",
1338 "nr_tlb_remote_flush_received",
1339 "nr_tlb_local_flush_all",
1340 "nr_tlb_local_flush_one",
Mel Gormanec659932014-01-21 14:33:16 -08001341#endif /* CONFIG_DEBUG_TLBFLUSH */
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001342
Davidlohr Bueso4f115142014-06-04 16:06:46 -07001343#ifdef CONFIG_DEBUG_VM_VMACACHE
1344 "vmacache_find_calls",
1345 "vmacache_find_hits",
1346#endif
Huang Yingcbc65df2017-09-06 16:24:29 -07001347#ifdef CONFIG_SWAP
1348 "swap_ra",
1349 "swap_ra_hit",
1350#endif
Laurent Dufourcaf05c02018-04-17 16:33:29 +02001351#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
1352 "speculative_pgfault"
1353#endif
Konstantin Khlebnikovebc5d83d2019-12-04 16:49:53 -08001354#endif /* CONFIG_VM_EVENT_COUNTERS || CONFIG_MEMCG */
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001355};
Konstantin Khlebnikovebc5d83d2019-12-04 16:49:53 -08001356#endif /* CONFIG_PROC_FS || CONFIG_SYSFS || CONFIG_NUMA || CONFIG_MEMCG */
KOSAKI Motohirofa25c502011-05-24 17:11:28 -07001357
Andrew Morton3c486872015-02-10 14:09:43 -08001358#if (defined(CONFIG_DEBUG_FS) && defined(CONFIG_COMPACTION)) || \
1359 defined(CONFIG_PROC_FS)
1360static void *frag_start(struct seq_file *m, loff_t *pos)
1361{
1362 pg_data_t *pgdat;
1363 loff_t node = *pos;
1364
1365 for (pgdat = first_online_pgdat();
1366 pgdat && node;
1367 pgdat = next_online_pgdat(pgdat))
1368 --node;
1369
1370 return pgdat;
1371}
1372
1373static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
1374{
1375 pg_data_t *pgdat = (pg_data_t *)arg;
1376
1377 (*pos)++;
1378 return next_online_pgdat(pgdat);
1379}
1380
1381static void frag_stop(struct seq_file *m, void *arg)
1382{
1383}
1384
David Rientjesb2bd8592017-05-03 14:52:59 -07001385/*
1386 * Walk zones in a node and print using a callback.
1387 * If @assert_populated is true, only use callback for zones that are populated.
1388 */
Andrew Morton3c486872015-02-10 14:09:43 -08001389static void walk_zones_in_node(struct seq_file *m, pg_data_t *pgdat,
Vinayak Menon727c0802017-07-10 15:49:17 -07001390 bool assert_populated, bool nolock,
Andrew Morton3c486872015-02-10 14:09:43 -08001391 void (*print)(struct seq_file *m, pg_data_t *, struct zone *))
1392{
1393 struct zone *zone;
1394 struct zone *node_zones = pgdat->node_zones;
1395 unsigned long flags;
1396
1397 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
David Rientjesb2bd8592017-05-03 14:52:59 -07001398 if (assert_populated && !populated_zone(zone))
Andrew Morton3c486872015-02-10 14:09:43 -08001399 continue;
1400
Vinayak Menon727c0802017-07-10 15:49:17 -07001401 if (!nolock)
1402 spin_lock_irqsave(&zone->lock, flags);
Andrew Morton3c486872015-02-10 14:09:43 -08001403 print(m, pgdat, zone);
Vinayak Menon727c0802017-07-10 15:49:17 -07001404 if (!nolock)
1405 spin_unlock_irqrestore(&zone->lock, flags);
Andrew Morton3c486872015-02-10 14:09:43 -08001406 }
1407}
1408#endif
1409
Mel Gormand7a57522010-05-24 14:32:25 -07001410#ifdef CONFIG_PROC_FS
Mel Gorman467c9962007-10-16 01:26:02 -07001411static void frag_show_print(struct seq_file *m, pg_data_t *pgdat,
1412 struct zone *zone)
1413{
1414 int order;
1415
1416 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
1417 for (order = 0; order < MAX_ORDER; ++order)
1418 seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
1419 seq_putc(m, '\n');
1420}
1421
1422/*
1423 * This walks the free areas for each zone.
1424 */
1425static int frag_show(struct seq_file *m, void *arg)
1426{
1427 pg_data_t *pgdat = (pg_data_t *)arg;
Vinayak Menon727c0802017-07-10 15:49:17 -07001428 walk_zones_in_node(m, pgdat, true, false, frag_show_print);
Mel Gorman467c9962007-10-16 01:26:02 -07001429 return 0;
1430}
1431
1432static void pagetypeinfo_showfree_print(struct seq_file *m,
1433 pg_data_t *pgdat, struct zone *zone)
1434{
1435 int order, mtype;
1436
1437 for (mtype = 0; mtype < MIGRATE_TYPES; mtype++) {
1438 seq_printf(m, "Node %4d, zone %8s, type %12s ",
1439 pgdat->node_id,
1440 zone->name,
1441 migratetype_names[mtype]);
1442 for (order = 0; order < MAX_ORDER; ++order) {
1443 unsigned long freecount = 0;
1444 struct free_area *area;
1445 struct list_head *curr;
Michal Hocko93b3a672019-11-05 21:16:44 -08001446 bool overflow = false;
Mel Gorman467c9962007-10-16 01:26:02 -07001447
1448 area = &(zone->free_area[order]);
1449
Michal Hocko93b3a672019-11-05 21:16:44 -08001450 list_for_each(curr, &area->free_list[mtype]) {
1451 /*
1452 * Cap the free_list iteration because it might
1453 * be really large and we are under a spinlock
1454 * so a long time spent here could trigger a
1455 * hard lockup detector. Anyway this is a
1456 * debugging tool so knowing there is a handful
1457 * of pages of this order should be more than
1458 * sufficient.
1459 */
1460 if (++freecount >= 100000) {
1461 overflow = true;
1462 break;
1463 }
1464 }
1465 seq_printf(m, "%s%6lu ", overflow ? ">" : "", freecount);
1466 spin_unlock_irq(&zone->lock);
1467 cond_resched();
1468 spin_lock_irq(&zone->lock);
Mel Gorman467c9962007-10-16 01:26:02 -07001469 }
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001470 seq_putc(m, '\n');
1471 }
Mel Gorman467c9962007-10-16 01:26:02 -07001472}
1473
1474/* Print out the free pages at each order for each migatetype */
1475static int pagetypeinfo_showfree(struct seq_file *m, void *arg)
1476{
1477 int order;
1478 pg_data_t *pgdat = (pg_data_t *)arg;
1479
1480 /* Print header */
1481 seq_printf(m, "%-43s ", "Free pages count per migrate type at order");
1482 for (order = 0; order < MAX_ORDER; ++order)
1483 seq_printf(m, "%6d ", order);
1484 seq_putc(m, '\n');
1485
Vinayak Menon727c0802017-07-10 15:49:17 -07001486 walk_zones_in_node(m, pgdat, true, false, pagetypeinfo_showfree_print);
Mel Gorman467c9962007-10-16 01:26:02 -07001487
1488 return 0;
1489}
1490
1491static void pagetypeinfo_showblockcount_print(struct seq_file *m,
1492 pg_data_t *pgdat, struct zone *zone)
1493{
1494 int mtype;
1495 unsigned long pfn;
1496 unsigned long start_pfn = zone->zone_start_pfn;
Cody P Schafer108bcc92013-02-22 16:35:23 -08001497 unsigned long end_pfn = zone_end_pfn(zone);
Mel Gorman467c9962007-10-16 01:26:02 -07001498 unsigned long count[MIGRATE_TYPES] = { 0, };
1499
1500 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
1501 struct page *page;
1502
Michal Hockod336e942017-07-06 15:38:07 -07001503 page = pfn_to_online_page(pfn);
1504 if (!page)
Mel Gorman467c9962007-10-16 01:26:02 -07001505 continue;
1506
Mel Gormaneb335752009-05-13 17:34:48 +01001507 /* Watch for unexpected holes punched in the memmap */
1508 if (!memmap_valid_within(pfn, page, zone))
Mel Gormane80d6a22008-08-14 11:10:14 +01001509 continue;
Mel Gormaneb335752009-05-13 17:34:48 +01001510
Joonsoo Kima91c43c2016-05-19 17:12:10 -07001511 if (page_zone(page) != zone)
1512 continue;
1513
Mel Gorman467c9962007-10-16 01:26:02 -07001514 mtype = get_pageblock_migratetype(page);
1515
Mel Gormane80d6a22008-08-14 11:10:14 +01001516 if (mtype < MIGRATE_TYPES)
1517 count[mtype]++;
Mel Gorman467c9962007-10-16 01:26:02 -07001518 }
1519
1520 /* Print counts */
1521 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
1522 for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
1523 seq_printf(m, "%12lu ", count[mtype]);
1524 seq_putc(m, '\n');
1525}
1526
SeongJae Parkf113e642017-09-06 16:24:23 -07001527/* Print out the number of pageblocks for each migratetype */
Mel Gorman467c9962007-10-16 01:26:02 -07001528static int pagetypeinfo_showblockcount(struct seq_file *m, void *arg)
1529{
1530 int mtype;
1531 pg_data_t *pgdat = (pg_data_t *)arg;
1532
1533 seq_printf(m, "\n%-23s", "Number of blocks type ");
1534 for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
1535 seq_printf(m, "%12s ", migratetype_names[mtype]);
1536 seq_putc(m, '\n');
Vinayak Menon727c0802017-07-10 15:49:17 -07001537 walk_zones_in_node(m, pgdat, true, false,
1538 pagetypeinfo_showblockcount_print);
Mel Gorman467c9962007-10-16 01:26:02 -07001539
1540 return 0;
1541}
1542
Joonsoo Kim48c96a32014-12-12 16:56:01 -08001543/*
1544 * Print out the number of pageblocks for each migratetype that contain pages
1545 * of other types. This gives an indication of how well fallbacks are being
1546 * contained by rmqueue_fallback(). It requires information from PAGE_OWNER
1547 * to determine what is going on
1548 */
1549static void pagetypeinfo_showmixedcount(struct seq_file *m, pg_data_t *pgdat)
1550{
1551#ifdef CONFIG_PAGE_OWNER
1552 int mtype;
1553
Vlastimil Babka7dd80b82016-03-15 14:56:12 -07001554 if (!static_branch_unlikely(&page_owner_inited))
Joonsoo Kim48c96a32014-12-12 16:56:01 -08001555 return;
1556
1557 drain_all_pages(NULL);
1558
1559 seq_printf(m, "\n%-23s", "Number of mixed blocks ");
1560 for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
1561 seq_printf(m, "%12s ", migratetype_names[mtype]);
1562 seq_putc(m, '\n');
1563
Vinayak Menon727c0802017-07-10 15:49:17 -07001564 walk_zones_in_node(m, pgdat, true, true,
1565 pagetypeinfo_showmixedcount_print);
Joonsoo Kim48c96a32014-12-12 16:56:01 -08001566#endif /* CONFIG_PAGE_OWNER */
1567}
1568
Mel Gorman467c9962007-10-16 01:26:02 -07001569/*
1570 * This prints out statistics in relation to grouping pages by mobility.
1571 * It is expensive to collect so do not constantly read the file.
1572 */
1573static int pagetypeinfo_show(struct seq_file *m, void *arg)
1574{
1575 pg_data_t *pgdat = (pg_data_t *)arg;
1576
KOSAKI Motohiro41b25a32008-04-30 00:52:13 -07001577 /* check memoryless node */
Lai Jiangshana47b53c2012-12-12 13:51:37 -08001578 if (!node_state(pgdat->node_id, N_MEMORY))
KOSAKI Motohiro41b25a32008-04-30 00:52:13 -07001579 return 0;
1580
Mel Gorman467c9962007-10-16 01:26:02 -07001581 seq_printf(m, "Page block order: %d\n", pageblock_order);
1582 seq_printf(m, "Pages per block: %lu\n", pageblock_nr_pages);
1583 seq_putc(m, '\n');
1584 pagetypeinfo_showfree(m, pgdat);
1585 pagetypeinfo_showblockcount(m, pgdat);
Joonsoo Kim48c96a32014-12-12 16:56:01 -08001586 pagetypeinfo_showmixedcount(m, pgdat);
Mel Gorman467c9962007-10-16 01:26:02 -07001587
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001588 return 0;
1589}
1590
Alexey Dobriyan8f32f7e2008-10-06 04:13:52 +04001591static const struct seq_operations fragmentation_op = {
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001592 .start = frag_start,
1593 .next = frag_next,
1594 .stop = frag_stop,
1595 .show = frag_show,
1596};
1597
Alexey Dobriyan74e2e8e2008-10-06 04:15:36 +04001598static const struct seq_operations pagetypeinfo_op = {
Mel Gorman467c9962007-10-16 01:26:02 -07001599 .start = frag_start,
1600 .next = frag_next,
1601 .stop = frag_stop,
1602 .show = pagetypeinfo_show,
1603};
1604
Mel Gormane2ecc8a2016-07-28 15:47:02 -07001605static bool is_zone_first_populated(pg_data_t *pgdat, struct zone *zone)
1606{
1607 int zid;
1608
1609 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
1610 struct zone *compare = &pgdat->node_zones[zid];
1611
1612 if (populated_zone(compare))
1613 return zone == compare;
1614 }
1615
Mel Gormane2ecc8a2016-07-28 15:47:02 -07001616 return false;
1617}
1618
Mel Gorman467c9962007-10-16 01:26:02 -07001619static void zoneinfo_show_print(struct seq_file *m, pg_data_t *pgdat,
1620 struct zone *zone)
1621{
1622 int i;
1623 seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
Mel Gormane2ecc8a2016-07-28 15:47:02 -07001624 if (is_zone_first_populated(pgdat, zone)) {
1625 seq_printf(m, "\n per-node stats");
1626 for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++) {
Konstantin Khlebnikov9d7ea9a2019-12-04 16:49:50 -08001627 seq_printf(m, "\n %-12s %lu", node_stat_name(i),
Roman Gushchinea426c22020-08-06 23:20:35 -07001628 node_page_state_pages(pgdat, i));
Mel Gormane2ecc8a2016-07-28 15:47:02 -07001629 }
1630 }
Mel Gorman467c9962007-10-16 01:26:02 -07001631 seq_printf(m,
1632 "\n pages free %lu"
1633 "\n min %lu"
1634 "\n low %lu"
1635 "\n high %lu"
Mel Gorman467c9962007-10-16 01:26:02 -07001636 "\n spanned %lu"
Jiang Liu9feedc92012-12-12 13:52:12 -08001637 "\n present %lu"
David Hildenbrandcd6aa992021-02-25 17:16:40 -08001638 "\n managed %lu"
1639 "\n cma %lu",
Mel Gorman88f5acf2011-01-13 15:45:41 -08001640 zone_page_state(zone, NR_FREE_PAGES),
Mel Gorman41858962009-06-16 15:32:12 -07001641 min_wmark_pages(zone),
1642 low_wmark_pages(zone),
1643 high_wmark_pages(zone),
Mel Gorman467c9962007-10-16 01:26:02 -07001644 zone->spanned_pages,
Jiang Liu9feedc92012-12-12 13:52:12 -08001645 zone->present_pages,
David Hildenbrandcd6aa992021-02-25 17:16:40 -08001646 zone_managed_pages(zone),
1647 zone_cma_pages(zone));
Mel Gorman467c9962007-10-16 01:26:02 -07001648
Mel Gorman467c9962007-10-16 01:26:02 -07001649 seq_printf(m,
Mel Gorman3484b2d2014-08-06 16:07:14 -07001650 "\n protection: (%ld",
Mel Gorman467c9962007-10-16 01:26:02 -07001651 zone->lowmem_reserve[0]);
1652 for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
Mel Gorman3484b2d2014-08-06 16:07:14 -07001653 seq_printf(m, ", %ld", zone->lowmem_reserve[i]);
David Rientjes7dfb8bf2017-05-03 14:53:02 -07001654 seq_putc(m, ')');
1655
Baoquan Hea8a4b7a2020-08-14 17:30:07 -07001656 /* If unpopulated, no other information is useful */
1657 if (!populated_zone(zone)) {
1658 seq_putc(m, '\n');
1659 return;
1660 }
1661
David Rientjes7dfb8bf2017-05-03 14:53:02 -07001662 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
Konstantin Khlebnikov9d7ea9a2019-12-04 16:49:50 -08001663 seq_printf(m, "\n %-12s %lu", zone_stat_name(i),
1664 zone_page_state(zone, i));
David Rientjes7dfb8bf2017-05-03 14:53:02 -07001665
Kemi Wang3a321d22017-09-08 16:12:48 -07001666#ifdef CONFIG_NUMA
1667 for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++)
Konstantin Khlebnikov9d7ea9a2019-12-04 16:49:50 -08001668 seq_printf(m, "\n %-12s %lu", numa_stat_name(i),
1669 zone_numa_state_snapshot(zone, i));
Kemi Wang3a321d22017-09-08 16:12:48 -07001670#endif
1671
David Rientjes7dfb8bf2017-05-03 14:53:02 -07001672 seq_printf(m, "\n pagesets");
Mel Gorman467c9962007-10-16 01:26:02 -07001673 for_each_online_cpu(i) {
1674 struct per_cpu_pageset *pageset;
Mel Gorman467c9962007-10-16 01:26:02 -07001675
Christoph Lameter99dcc3e2010-01-05 15:34:51 +09001676 pageset = per_cpu_ptr(zone->pageset, i);
Christoph Lameter3dfa5722008-02-04 22:29:19 -08001677 seq_printf(m,
1678 "\n cpu: %i"
1679 "\n count: %i"
1680 "\n high: %i"
1681 "\n batch: %i",
1682 i,
1683 pageset->pcp.count,
1684 pageset->pcp.high,
1685 pageset->pcp.batch);
Mel Gorman467c9962007-10-16 01:26:02 -07001686#ifdef CONFIG_SMP
1687 seq_printf(m, "\n vm stats threshold: %d",
1688 pageset->stat_threshold);
1689#endif
1690 }
1691 seq_printf(m,
Mel Gorman599d0c92016-07-28 15:45:31 -07001692 "\n node_unreclaimable: %u"
Andrey Ryabinin3a50d142017-11-15 17:34:15 -08001693 "\n start_pfn: %lu",
Johannes Weinerc73322d2017-05-03 14:51:51 -07001694 pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES,
Andrey Ryabinin3a50d142017-11-15 17:34:15 -08001695 zone->zone_start_pfn);
Mel Gorman467c9962007-10-16 01:26:02 -07001696 seq_putc(m, '\n');
1697}
1698
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001699/*
David Rientjesb2bd8592017-05-03 14:52:59 -07001700 * Output information about zones in @pgdat. All zones are printed regardless
1701 * of whether they are populated or not: lowmem_reserve_ratio operates on the
1702 * set of all zones and userspace would not be aware of such zones if they are
1703 * suppressed here (zoneinfo displays the effect of lowmem_reserve_ratio).
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001704 */
1705static int zoneinfo_show(struct seq_file *m, void *arg)
1706{
Mel Gorman467c9962007-10-16 01:26:02 -07001707 pg_data_t *pgdat = (pg_data_t *)arg;
Vinayak Menon727c0802017-07-10 15:49:17 -07001708 walk_zones_in_node(m, pgdat, false, false, zoneinfo_show_print);
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001709 return 0;
1710}
1711
Alexey Dobriyan5c9fe622008-10-06 04:19:42 +04001712static const struct seq_operations zoneinfo_op = {
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001713 .start = frag_start, /* iterate over all zones. The same as in
1714 * fragmentation. */
1715 .next = frag_next,
1716 .stop = frag_stop,
1717 .show = zoneinfo_show,
1718};
1719
Konstantin Khlebnikov9d7ea9a2019-12-04 16:49:50 -08001720#define NR_VMSTAT_ITEMS (NR_VM_ZONE_STAT_ITEMS + \
1721 NR_VM_NUMA_STAT_ITEMS + \
1722 NR_VM_NODE_STAT_ITEMS + \
1723 NR_VM_WRITEBACK_STAT_ITEMS + \
1724 (IS_ENABLED(CONFIG_VM_EVENT_COUNTERS) ? \
1725 NR_VM_EVENT_ITEMS : 0))
Michael Rubin79da8262010-10-26 14:21:36 -07001726
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001727static void *vmstat_start(struct seq_file *m, loff_t *pos)
1728{
Christoph Lameter2244b952006-06-30 01:55:33 -07001729 unsigned long *v;
Konstantin Khlebnikov9d7ea9a2019-12-04 16:49:50 -08001730 int i;
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001731
Konstantin Khlebnikov9d7ea9a2019-12-04 16:49:50 -08001732 if (*pos >= NR_VMSTAT_ITEMS)
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001733 return NULL;
1734
Konstantin Khlebnikov9d7ea9a2019-12-04 16:49:50 -08001735 BUILD_BUG_ON(ARRAY_SIZE(vmstat_text) < NR_VMSTAT_ITEMS);
1736 v = kmalloc_array(NR_VMSTAT_ITEMS, sizeof(unsigned long), GFP_KERNEL);
Christoph Lameter2244b952006-06-30 01:55:33 -07001737 m->private = v;
1738 if (!v)
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001739 return ERR_PTR(-ENOMEM);
Christoph Lameter2244b952006-06-30 01:55:33 -07001740 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
Michal Hockoc41f0122017-09-06 16:23:36 -07001741 v[i] = global_zone_page_state(i);
Michael Rubin79da8262010-10-26 14:21:36 -07001742 v += NR_VM_ZONE_STAT_ITEMS;
1743
Kemi Wang3a321d22017-09-08 16:12:48 -07001744#ifdef CONFIG_NUMA
1745 for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++)
1746 v[i] = global_numa_state(i);
1747 v += NR_VM_NUMA_STAT_ITEMS;
1748#endif
1749
Mel Gorman75ef7182016-07-28 15:45:24 -07001750 for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++)
Roman Gushchinea426c22020-08-06 23:20:35 -07001751 v[i] = global_node_page_state_pages(i);
Mel Gorman75ef7182016-07-28 15:45:24 -07001752 v += NR_VM_NODE_STAT_ITEMS;
1753
Michael Rubin79da8262010-10-26 14:21:36 -07001754 global_dirty_limits(v + NR_DIRTY_BG_THRESHOLD,
1755 v + NR_DIRTY_THRESHOLD);
1756 v += NR_VM_WRITEBACK_STAT_ITEMS;
1757
Christoph Lameterf8891e52006-06-30 01:55:45 -07001758#ifdef CONFIG_VM_EVENT_COUNTERS
Michael Rubin79da8262010-10-26 14:21:36 -07001759 all_vm_events(v);
1760 v[PGPGIN] /= 2; /* sectors -> kbytes */
1761 v[PGPGOUT] /= 2;
Christoph Lameterf8891e52006-06-30 01:55:45 -07001762#endif
Wu Fengguangff8b16d2010-11-04 01:56:49 +08001763 return (unsigned long *)m->private + *pos;
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001764}
1765
1766static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
1767{
1768 (*pos)++;
Konstantin Khlebnikov9d7ea9a2019-12-04 16:49:50 -08001769 if (*pos >= NR_VMSTAT_ITEMS)
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001770 return NULL;
1771 return (unsigned long *)m->private + *pos;
1772}
1773
1774static int vmstat_show(struct seq_file *m, void *arg)
1775{
1776 unsigned long *l = arg;
1777 unsigned long off = l - (unsigned long *)m->private;
Alexey Dobriyan68ba0322016-10-07 17:02:14 -07001778
1779 seq_puts(m, vmstat_text[off]);
Joe Perches75ba1d02016-10-07 17:02:20 -07001780 seq_put_decimal_ull(m, " ", *l);
Alexey Dobriyan68ba0322016-10-07 17:02:14 -07001781 seq_putc(m, '\n');
NeilBrown8d928902020-06-01 21:48:21 -07001782
1783 if (off == NR_VMSTAT_ITEMS - 1) {
1784 /*
1785 * We've come to the end - add any deprecated counters to avoid
1786 * breaking userspace which might depend on them being present.
1787 */
1788 seq_puts(m, "nr_unstable 0\n");
1789 }
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001790 return 0;
1791}
1792
1793static void vmstat_stop(struct seq_file *m, void *arg)
1794{
1795 kfree(m->private);
1796 m->private = NULL;
1797}
1798
Alexey Dobriyanb6aa44a2008-10-06 04:17:48 +04001799static const struct seq_operations vmstat_op = {
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001800 .start = vmstat_start,
1801 .next = vmstat_next,
1802 .stop = vmstat_stop,
1803 .show = vmstat_show,
1804};
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001805#endif /* CONFIG_PROC_FS */
1806
Christoph Lameterdf9ecab2006-08-31 21:27:35 -07001807#ifdef CONFIG_SMP
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001808static DEFINE_PER_CPU(struct delayed_work, vmstat_work);
Christoph Lameter77461ab2007-05-09 02:35:13 -07001809int sysctl_stat_interval __read_mostly = HZ;
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001810
Hugh Dickins52b6f462016-05-19 17:12:50 -07001811#ifdef CONFIG_PROC_FS
1812static void refresh_vm_stats(struct work_struct *work)
1813{
1814 refresh_cpu_vm_stats(true);
1815}
1816
1817int vmstat_refresh(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001818 void *buffer, size_t *lenp, loff_t *ppos)
Hugh Dickins52b6f462016-05-19 17:12:50 -07001819{
1820 long val;
1821 int err;
1822 int i;
1823
1824 /*
1825 * The regular update, every sysctl_stat_interval, may come later
1826 * than expected: leaving a significant amount in per_cpu buckets.
1827 * This is particularly misleading when checking a quantity of HUGE
1828 * pages, immediately after running a test. /proc/sys/vm/stat_refresh,
1829 * which can equally be echo'ed to or cat'ted from (by root),
1830 * can be used to update the stats just before reading them.
1831 *
Michal Hockoc41f0122017-09-06 16:23:36 -07001832 * Oh, and since global_zone_page_state() etc. are so careful to hide
Hugh Dickins52b6f462016-05-19 17:12:50 -07001833 * transiently negative values, report an error here if any of
1834 * the stats is negative, so we know to go looking for imbalance.
1835 */
1836 err = schedule_on_each_cpu(refresh_vm_stats);
1837 if (err)
1838 return err;
1839 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++) {
Mel Gorman75ef7182016-07-28 15:45:24 -07001840 val = atomic_long_read(&vm_zone_stat[i]);
Hugh Dickins52b6f462016-05-19 17:12:50 -07001841 if (val < 0) {
Johannes Weinerc822f622017-05-03 14:52:10 -07001842 pr_warn("%s: %s %ld\n",
Konstantin Khlebnikov9d7ea9a2019-12-04 16:49:50 -08001843 __func__, zone_stat_name(i), val);
Johannes Weinerc822f622017-05-03 14:52:10 -07001844 err = -EINVAL;
Hugh Dickins52b6f462016-05-19 17:12:50 -07001845 }
1846 }
Kemi Wang3a321d22017-09-08 16:12:48 -07001847#ifdef CONFIG_NUMA
1848 for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++) {
1849 val = atomic_long_read(&vm_numa_stat[i]);
1850 if (val < 0) {
1851 pr_warn("%s: %s %ld\n",
Konstantin Khlebnikov9d7ea9a2019-12-04 16:49:50 -08001852 __func__, numa_stat_name(i), val);
Kemi Wang3a321d22017-09-08 16:12:48 -07001853 err = -EINVAL;
1854 }
1855 }
1856#endif
Hugh Dickins52b6f462016-05-19 17:12:50 -07001857 if (err)
1858 return err;
1859 if (write)
1860 *ppos += *lenp;
1861 else
1862 *lenp = 0;
1863 return 0;
1864}
1865#endif /* CONFIG_PROC_FS */
1866
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001867static void vmstat_update(struct work_struct *w)
1868{
Christoph Lameter0eb77e92016-01-14 15:21:40 -08001869 if (refresh_cpu_vm_stats(true)) {
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001870 /*
1871 * Counters were updated so we expect more updates
1872 * to occur in the future. Keep on running the
1873 * update worker thread.
1874 */
Michal Hockoce612872017-04-07 16:05:05 -07001875 queue_delayed_work_on(smp_processor_id(), mm_percpu_wq,
Michal Hockof01f17d2016-02-05 15:36:24 -08001876 this_cpu_ptr(&vmstat_work),
1877 round_jiffies_relative(sysctl_stat_interval));
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001878 }
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001879}
1880
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001881/*
Christoph Lameter0eb77e92016-01-14 15:21:40 -08001882 * Switch off vmstat processing and then fold all the remaining differentials
1883 * until the diffs stay at zero. The function is used by NOHZ and can only be
1884 * invoked when tick processing is not active.
1885 */
Christoph Lameter0eb77e92016-01-14 15:21:40 -08001886/*
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001887 * Check if the diffs for a certain cpu indicate that
1888 * an update is needed.
1889 */
1890static bool need_update(int cpu)
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001891{
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001892 struct zone *zone;
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001893
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001894 for_each_populated_zone(zone) {
1895 struct per_cpu_pageset *p = per_cpu_ptr(zone->pageset, cpu);
1896
1897 BUILD_BUG_ON(sizeof(p->vm_stat_diff[0]) != 1);
Kemi Wang3a321d22017-09-08 16:12:48 -07001898#ifdef CONFIG_NUMA
Kemi Wang1d90ca82017-09-08 16:12:52 -07001899 BUILD_BUG_ON(sizeof(p->vm_numa_stat_diff[0]) != 2);
Kemi Wang3a321d22017-09-08 16:12:48 -07001900#endif
Kemi Wang63803222017-09-08 16:12:55 -07001901
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001902 /*
1903 * The fast way of checking if there are any vmstat diffs.
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001904 */
Janne Huttunen13c9aaf2018-11-16 15:08:32 -08001905 if (memchr_inv(p->vm_stat_diff, 0, NR_VM_ZONE_STAT_ITEMS *
1906 sizeof(p->vm_stat_diff[0])))
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001907 return true;
Kemi Wang3a321d22017-09-08 16:12:48 -07001908#ifdef CONFIG_NUMA
Janne Huttunen13c9aaf2018-11-16 15:08:32 -08001909 if (memchr_inv(p->vm_numa_stat_diff, 0, NR_VM_NUMA_STAT_ITEMS *
1910 sizeof(p->vm_numa_stat_diff[0])))
Kemi Wang3a321d22017-09-08 16:12:48 -07001911 return true;
1912#endif
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001913 }
1914 return false;
1915}
1916
Christoph Lameter7b8da4c2016-05-20 16:58:21 -07001917/*
1918 * Switch off vmstat processing and then fold all the remaining differentials
1919 * until the diffs stay at zero. The function is used by NOHZ and can only be
1920 * invoked when tick processing is not active.
1921 */
Michal Hockof01f17d2016-02-05 15:36:24 -08001922void quiet_vmstat(void)
1923{
1924 if (system_state != SYSTEM_RUNNING)
1925 return;
1926
Christoph Lameter7b8da4c2016-05-20 16:58:21 -07001927 if (!delayed_work_pending(this_cpu_ptr(&vmstat_work)))
Michal Hockof01f17d2016-02-05 15:36:24 -08001928 return;
1929
1930 if (!need_update(smp_processor_id()))
1931 return;
1932
1933 /*
1934 * Just refresh counters and do not care about the pending delayed
1935 * vmstat_update. It doesn't fire that often to matter and canceling
1936 * it would be too expensive from this path.
1937 * vmstat_shepherd will take care about that for us.
1938 */
1939 refresh_cpu_vm_stats(false);
1940}
1941
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001942/*
1943 * Shepherd worker thread that checks the
1944 * differentials of processors that have their worker
1945 * threads for vm statistics updates disabled because of
1946 * inactivity.
1947 */
1948static void vmstat_shepherd(struct work_struct *w);
1949
Christoph Lameter0eb77e92016-01-14 15:21:40 -08001950static DECLARE_DEFERRABLE_WORK(shepherd, vmstat_shepherd);
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001951
1952static void vmstat_shepherd(struct work_struct *w)
1953{
1954 int cpu;
1955
1956 get_online_cpus();
1957 /* Check processors whose vmstat worker threads have been disabled */
Christoph Lameter7b8da4c2016-05-20 16:58:21 -07001958 for_each_online_cpu(cpu) {
Michal Hockof01f17d2016-02-05 15:36:24 -08001959 struct delayed_work *dw = &per_cpu(vmstat_work, cpu);
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001960
Christoph Lameter7b8da4c2016-05-20 16:58:21 -07001961 if (!delayed_work_pending(dw) && need_update(cpu))
Michal Hockoce612872017-04-07 16:05:05 -07001962 queue_delayed_work_on(cpu, mm_percpu_wq, dw, 0);
Michal Hockof01f17d2016-02-05 15:36:24 -08001963 }
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001964 put_online_cpus();
1965
1966 schedule_delayed_work(&shepherd,
1967 round_jiffies_relative(sysctl_stat_interval));
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001968}
1969
1970static void __init start_shepherd_timer(void)
1971{
1972 int cpu;
1973
1974 for_each_possible_cpu(cpu)
Michal Hockoccde8bd2016-02-05 15:36:27 -08001975 INIT_DEFERRABLE_WORK(per_cpu_ptr(&vmstat_work, cpu),
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001976 vmstat_update);
1977
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07001978 schedule_delayed_work(&shepherd,
1979 round_jiffies_relative(sysctl_stat_interval));
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001980}
1981
Tim Chen03e86db2016-10-07 17:00:02 -07001982static void __init init_cpu_node_state(void)
1983{
Sebastian Andrzej Siewior4c501322016-11-29 15:51:14 +01001984 int node;
Tim Chen03e86db2016-10-07 17:00:02 -07001985
Sebastian Andrzej Siewior4c501322016-11-29 15:51:14 +01001986 for_each_online_node(node) {
1987 if (cpumask_weight(cpumask_of_node(node)) > 0)
1988 node_set_state(node, N_CPU);
1989 }
Tim Chen03e86db2016-10-07 17:00:02 -07001990}
1991
Sebastian Andrzej Siewior5438da92016-11-29 15:52:21 +01001992static int vmstat_cpu_online(unsigned int cpu)
1993{
1994 refresh_zone_stat_thresholds();
1995 node_set_state(cpu_to_node(cpu), N_CPU);
1996 return 0;
1997}
1998
1999static int vmstat_cpu_down_prep(unsigned int cpu)
2000{
2001 cancel_delayed_work_sync(&per_cpu(vmstat_work, cpu));
2002 return 0;
2003}
2004
2005static int vmstat_cpu_dead(unsigned int cpu)
Toshi Kani807a1bd2013-11-12 15:08:13 -08002006{
Sebastian Andrzej Siewior4c501322016-11-29 15:51:14 +01002007 const struct cpumask *node_cpus;
Sebastian Andrzej Siewior5438da92016-11-29 15:52:21 +01002008 int node;
Toshi Kani807a1bd2013-11-12 15:08:13 -08002009
Sebastian Andrzej Siewior5438da92016-11-29 15:52:21 +01002010 node = cpu_to_node(cpu);
2011
2012 refresh_zone_stat_thresholds();
Sebastian Andrzej Siewior4c501322016-11-29 15:51:14 +01002013 node_cpus = cpumask_of_node(node);
2014 if (cpumask_weight(node_cpus) > 0)
Sebastian Andrzej Siewior5438da92016-11-29 15:52:21 +01002015 return 0;
Toshi Kani807a1bd2013-11-12 15:08:13 -08002016
2017 node_clear_state(node, N_CPU);
Sebastian Andrzej Siewior5438da92016-11-29 15:52:21 +01002018 return 0;
Toshi Kani807a1bd2013-11-12 15:08:13 -08002019}
2020
Alexey Dobriyan8f32f7e2008-10-06 04:13:52 +04002021#endif
Christoph Lameterdf9ecab2006-08-31 21:27:35 -07002022
Michal Hockoce612872017-04-07 16:05:05 -07002023struct workqueue_struct *mm_percpu_wq;
2024
Michal Hocko597b7302017-03-31 15:11:47 -07002025void __init init_mm_internals(void)
Christoph Lameterdf9ecab2006-08-31 21:27:35 -07002026{
Michal Hockoce612872017-04-07 16:05:05 -07002027 int ret __maybe_unused;
Sebastian Andrzej Siewior5438da92016-11-29 15:52:21 +01002028
Michal Hocko80d136e2017-04-19 09:52:46 +02002029 mm_percpu_wq = alloc_workqueue("mm_percpu_wq", WQ_MEM_RECLAIM, 0);
Michal Hockoce612872017-04-07 16:05:05 -07002030
2031#ifdef CONFIG_SMP
Sebastian Andrzej Siewior5438da92016-11-29 15:52:21 +01002032 ret = cpuhp_setup_state_nocalls(CPUHP_MM_VMSTAT_DEAD, "mm/vmstat:dead",
2033 NULL, vmstat_cpu_dead);
2034 if (ret < 0)
2035 pr_err("vmstat: failed to register 'dead' hotplug state\n");
2036
2037 ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "mm/vmstat:online",
2038 vmstat_cpu_online,
2039 vmstat_cpu_down_prep);
2040 if (ret < 0)
2041 pr_err("vmstat: failed to register 'online' hotplug state\n");
2042
2043 get_online_cpus();
Tim Chen03e86db2016-10-07 17:00:02 -07002044 init_cpu_node_state();
Sebastian Andrzej Siewior5438da92016-11-29 15:52:21 +01002045 put_online_cpus();
Christoph Lameterd1187ed2007-05-09 02:35:12 -07002046
Christoph Lameter7cc36bb2014-10-09 15:29:43 -07002047 start_shepherd_timer();
Alexey Dobriyan8f32f7e2008-10-06 04:13:52 +04002048#endif
2049#ifdef CONFIG_PROC_FS
Christoph Hellwigfddda2b2018-04-13 19:44:18 +02002050 proc_create_seq("buddyinfo", 0444, NULL, &fragmentation_op);
Michal Hockoabaed012019-11-05 21:16:40 -08002051 proc_create_seq("pagetypeinfo", 0400, NULL, &pagetypeinfo_op);
Christoph Hellwigfddda2b2018-04-13 19:44:18 +02002052 proc_create_seq("vmstat", 0444, NULL, &vmstat_op);
2053 proc_create_seq("zoneinfo", 0444, NULL, &zoneinfo_op);
Alexey Dobriyan8f32f7e2008-10-06 04:13:52 +04002054#endif
Christoph Lameterdf9ecab2006-08-31 21:27:35 -07002055}
Mel Gormand7a57522010-05-24 14:32:25 -07002056
2057#if defined(CONFIG_DEBUG_FS) && defined(CONFIG_COMPACTION)
Mel Gormand7a57522010-05-24 14:32:25 -07002058
2059/*
2060 * Return an index indicating how much of the available free memory is
2061 * unusable for an allocation of the requested size.
2062 */
2063static int unusable_free_index(unsigned int order,
2064 struct contig_page_info *info)
2065{
2066 /* No free memory is interpreted as all free memory is unusable */
2067 if (info->free_pages == 0)
2068 return 1000;
2069
2070 /*
2071 * Index should be a value between 0 and 1. Return a value to 3
2072 * decimal places.
2073 *
2074 * 0 => no fragmentation
2075 * 1 => high fragmentation
2076 */
2077 return div_u64((info->free_pages - (info->free_blocks_suitable << order)) * 1000ULL, info->free_pages);
2078
2079}
2080
2081static void unusable_show_print(struct seq_file *m,
2082 pg_data_t *pgdat, struct zone *zone)
2083{
2084 unsigned int order;
2085 int index;
2086 struct contig_page_info info;
2087
2088 seq_printf(m, "Node %d, zone %8s ",
2089 pgdat->node_id,
2090 zone->name);
2091 for (order = 0; order < MAX_ORDER; ++order) {
2092 fill_contig_page_info(zone, order, &info);
2093 index = unusable_free_index(order, &info);
2094 seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
2095 }
2096
2097 seq_putc(m, '\n');
2098}
2099
2100/*
2101 * Display unusable free space index
2102 *
2103 * The unusable free space index measures how much of the available free
2104 * memory cannot be used to satisfy an allocation of a given size and is a
2105 * value between 0 and 1. The higher the value, the more of free memory is
2106 * unusable and by implication, the worse the external fragmentation is. This
2107 * can be expressed as a percentage by multiplying by 100.
2108 */
2109static int unusable_show(struct seq_file *m, void *arg)
2110{
2111 pg_data_t *pgdat = (pg_data_t *)arg;
2112
2113 /* check memoryless node */
Lai Jiangshana47b53c2012-12-12 13:51:37 -08002114 if (!node_state(pgdat->node_id, N_MEMORY))
Mel Gormand7a57522010-05-24 14:32:25 -07002115 return 0;
2116
Vinayak Menon727c0802017-07-10 15:49:17 -07002117 walk_zones_in_node(m, pgdat, true, false, unusable_show_print);
Mel Gormand7a57522010-05-24 14:32:25 -07002118
2119 return 0;
2120}
2121
Kefeng Wang01a99562020-06-04 16:51:08 -07002122static const struct seq_operations unusable_sops = {
Mel Gormand7a57522010-05-24 14:32:25 -07002123 .start = frag_start,
2124 .next = frag_next,
2125 .stop = frag_stop,
2126 .show = unusable_show,
2127};
2128
Kefeng Wang01a99562020-06-04 16:51:08 -07002129DEFINE_SEQ_ATTRIBUTE(unusable);
Mel Gormand7a57522010-05-24 14:32:25 -07002130
Mel Gormanf1a5ab12010-05-24 14:32:26 -07002131static void extfrag_show_print(struct seq_file *m,
2132 pg_data_t *pgdat, struct zone *zone)
2133{
2134 unsigned int order;
2135 int index;
2136
2137 /* Alloc on stack as interrupts are disabled for zone walk */
2138 struct contig_page_info info;
2139
2140 seq_printf(m, "Node %d, zone %8s ",
2141 pgdat->node_id,
2142 zone->name);
2143 for (order = 0; order < MAX_ORDER; ++order) {
2144 fill_contig_page_info(zone, order, &info);
Mel Gorman56de7262010-05-24 14:32:30 -07002145 index = __fragmentation_index(order, &info);
Mel Gormanf1a5ab12010-05-24 14:32:26 -07002146 seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
2147 }
2148
2149 seq_putc(m, '\n');
2150}
2151
2152/*
2153 * Display fragmentation index for orders that allocations would fail for
2154 */
2155static int extfrag_show(struct seq_file *m, void *arg)
2156{
2157 pg_data_t *pgdat = (pg_data_t *)arg;
2158
Vinayak Menon727c0802017-07-10 15:49:17 -07002159 walk_zones_in_node(m, pgdat, true, false, extfrag_show_print);
Mel Gormanf1a5ab12010-05-24 14:32:26 -07002160
2161 return 0;
2162}
2163
Kefeng Wang01a99562020-06-04 16:51:08 -07002164static const struct seq_operations extfrag_sops = {
Mel Gormanf1a5ab12010-05-24 14:32:26 -07002165 .start = frag_start,
2166 .next = frag_next,
2167 .stop = frag_stop,
2168 .show = extfrag_show,
2169};
2170
Kefeng Wang01a99562020-06-04 16:51:08 -07002171DEFINE_SEQ_ATTRIBUTE(extfrag);
Mel Gormanf1a5ab12010-05-24 14:32:26 -07002172
Mel Gormand7a57522010-05-24 14:32:25 -07002173static int __init extfrag_debug_init(void)
2174{
Sasikantha babubde8bd82012-05-29 15:06:22 -07002175 struct dentry *extfrag_debug_root;
2176
Mel Gormand7a57522010-05-24 14:32:25 -07002177 extfrag_debug_root = debugfs_create_dir("extfrag", NULL);
Mel Gormand7a57522010-05-24 14:32:25 -07002178
Greg Kroah-Hartmand9f79792019-03-05 15:46:09 -08002179 debugfs_create_file("unusable_index", 0444, extfrag_debug_root, NULL,
Kefeng Wang01a99562020-06-04 16:51:08 -07002180 &unusable_fops);
Mel Gormand7a57522010-05-24 14:32:25 -07002181
Greg Kroah-Hartmand9f79792019-03-05 15:46:09 -08002182 debugfs_create_file("extfrag_index", 0444, extfrag_debug_root, NULL,
Kefeng Wang01a99562020-06-04 16:51:08 -07002183 &extfrag_fops);
Mel Gormanf1a5ab12010-05-24 14:32:26 -07002184
Mel Gormand7a57522010-05-24 14:32:25 -07002185 return 0;
2186}
2187
2188module_init(extfrag_debug_init);
2189#endif