1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_VMSTAT_H
3 #define _LINUX_VMSTAT_H
5 #include <linux/types.h>
6 #include <linux/percpu.h>
7 #include <linux/mmzone.h>
8 #include <linux/vm_event_item.h>
9 #include <linux/atomic.h>
10 #include <linux/static_key.h>
11 #include <linux/mmdebug.h>
13 extern int sysctl_stat_interval;
16 #define ENABLE_NUMA_STAT 1
17 #define DISABLE_NUMA_STAT 0
18 extern int sysctl_vm_numa_stat;
19 DECLARE_STATIC_KEY_TRUE(vm_numa_stat_key);
20 int sysctl_vm_numa_stat_handler(struct ctl_table *table, int write,
21 void *buffer, size_t *length, loff_t *ppos);
26 unsigned nr_unqueued_dirty;
27 unsigned nr_congested;
28 unsigned nr_writeback;
29 unsigned nr_immediate;
31 unsigned nr_activate[ANON_AND_FILE];
33 unsigned nr_unmap_fail;
34 unsigned nr_lazyfree_fail;
37 enum writeback_stat_item {
39 NR_DIRTY_BG_THRESHOLD,
40 NR_VM_WRITEBACK_STAT_ITEMS,
43 #ifdef CONFIG_VM_EVENT_COUNTERS
45 * Light weight per cpu counter implementation.
47 * Counters should only be incremented and no critical kernel component
48 * should rely on the counter values.
50 * Counters are handled completely inline. On many platforms the code
51 * generated will simply be the increment of a global address.
54 struct vm_event_state {
55 unsigned long event[NR_VM_EVENT_ITEMS];
58 DECLARE_PER_CPU(struct vm_event_state, vm_event_states);
61 * vm counters are allowed to be racy. Use raw_cpu_ops to avoid the
62 * local_irq_disable overhead.
64 static inline void __count_vm_event(enum vm_event_item item)
66 raw_cpu_inc(vm_event_states.event[item]);
69 static inline void count_vm_event(enum vm_event_item item)
71 this_cpu_inc(vm_event_states.event[item]);
74 static inline void __count_vm_events(enum vm_event_item item, long delta)
76 raw_cpu_add(vm_event_states.event[item], delta);
79 static inline void count_vm_events(enum vm_event_item item, long delta)
81 this_cpu_add(vm_event_states.event[item], delta);
84 extern void all_vm_events(unsigned long *);
86 extern void vm_events_fold_cpu(int cpu);
90 /* Disable counters */
91 static inline void count_vm_event(enum vm_event_item item)
94 static inline void count_vm_events(enum vm_event_item item, long delta)
97 static inline void __count_vm_event(enum vm_event_item item)
100 static inline void __count_vm_events(enum vm_event_item item, long delta)
103 static inline void all_vm_events(unsigned long *ret)
106 static inline void vm_events_fold_cpu(int cpu)
110 #endif /* CONFIG_VM_EVENT_COUNTERS */
112 #ifdef CONFIG_NUMA_BALANCING
113 #define count_vm_numa_event(x) count_vm_event(x)
114 #define count_vm_numa_events(x, y) count_vm_events(x, y)
116 #define count_vm_numa_event(x) do {} while (0)
117 #define count_vm_numa_events(x, y) do { (void)(y); } while (0)
118 #endif /* CONFIG_NUMA_BALANCING */
120 #ifdef CONFIG_DEBUG_TLBFLUSH
121 #define count_vm_tlb_event(x) count_vm_event(x)
122 #define count_vm_tlb_events(x, y) count_vm_events(x, y)
124 #define count_vm_tlb_event(x) do {} while (0)
125 #define count_vm_tlb_events(x, y) do { (void)(y); } while (0)
128 #ifdef CONFIG_DEBUG_VM_VMACACHE
129 #define count_vm_vmacache_event(x) count_vm_event(x)
131 #define count_vm_vmacache_event(x) do {} while (0)
134 #define __count_zid_vm_events(item, zid, delta) \
135 __count_vm_events(item##_NORMAL - ZONE_NORMAL + zid, delta)
138 * Zone and node-based page accounting with per cpu differentials.
140 extern atomic_long_t vm_zone_stat[NR_VM_ZONE_STAT_ITEMS];
141 extern atomic_long_t vm_numa_stat[NR_VM_NUMA_STAT_ITEMS];
142 extern atomic_long_t vm_node_stat[NR_VM_NODE_STAT_ITEMS];
145 static inline void zone_numa_state_add(long x, struct zone *zone,
146 enum numa_stat_item item)
148 atomic_long_add(x, &zone->vm_numa_stat[item]);
149 atomic_long_add(x, &vm_numa_stat[item]);
152 static inline unsigned long global_numa_state(enum numa_stat_item item)
154 long x = atomic_long_read(&vm_numa_stat[item]);
159 static inline unsigned long zone_numa_state_snapshot(struct zone *zone,
160 enum numa_stat_item item)
162 long x = atomic_long_read(&zone->vm_numa_stat[item]);
165 for_each_online_cpu(cpu)
166 x += per_cpu_ptr(zone->pageset, cpu)->vm_numa_stat_diff[item];
170 #endif /* CONFIG_NUMA */
172 static inline void zone_page_state_add(long x, struct zone *zone,
173 enum zone_stat_item item)
175 atomic_long_add(x, &zone->vm_stat[item]);
176 atomic_long_add(x, &vm_zone_stat[item]);
179 static inline void node_page_state_add(long x, struct pglist_data *pgdat,
180 enum node_stat_item item)
182 atomic_long_add(x, &pgdat->vm_stat[item]);
183 atomic_long_add(x, &vm_node_stat[item]);
186 static inline unsigned long global_zone_page_state(enum zone_stat_item item)
188 long x = atomic_long_read(&vm_zone_stat[item]);
197 unsigned long global_node_page_state_pages(enum node_stat_item item)
199 long x = atomic_long_read(&vm_node_stat[item]);
207 static inline unsigned long global_node_page_state(enum node_stat_item item)
209 VM_WARN_ON_ONCE(vmstat_item_in_bytes(item));
211 return global_node_page_state_pages(item);
214 static inline unsigned long zone_page_state(struct zone *zone,
215 enum zone_stat_item item)
217 long x = atomic_long_read(&zone->vm_stat[item]);
226 * More accurate version that also considers the currently pending
227 * deltas. For that we need to loop over all cpus to find the current
228 * deltas. There is no synchronization so the result cannot be
229 * exactly accurate either.
231 static inline unsigned long zone_page_state_snapshot(struct zone *zone,
232 enum zone_stat_item item)
234 long x = atomic_long_read(&zone->vm_stat[item]);
238 for_each_online_cpu(cpu)
239 x += per_cpu_ptr(zone->pageset, cpu)->vm_stat_diff[item];
248 extern void __inc_numa_state(struct zone *zone, enum numa_stat_item item);
249 extern unsigned long sum_zone_node_page_state(int node,
250 enum zone_stat_item item);
251 extern unsigned long sum_zone_numa_state(int node, enum numa_stat_item item);
252 extern unsigned long node_page_state(struct pglist_data *pgdat,
253 enum node_stat_item item);
254 extern unsigned long node_page_state_pages(struct pglist_data *pgdat,
255 enum node_stat_item item);
257 #define sum_zone_node_page_state(node, item) global_zone_page_state(item)
258 #define node_page_state(node, item) global_node_page_state(item)
259 #define node_page_state_pages(node, item) global_node_page_state_pages(item)
260 #endif /* CONFIG_NUMA */
263 void __mod_zone_page_state(struct zone *, enum zone_stat_item item, long);
264 void __inc_zone_page_state(struct page *, enum zone_stat_item);
265 void __dec_zone_page_state(struct page *, enum zone_stat_item);
267 void __mod_node_page_state(struct pglist_data *, enum node_stat_item item, long);
268 void __inc_node_page_state(struct page *, enum node_stat_item);
269 void __dec_node_page_state(struct page *, enum node_stat_item);
271 void mod_zone_page_state(struct zone *, enum zone_stat_item, long);
272 void inc_zone_page_state(struct page *, enum zone_stat_item);
273 void dec_zone_page_state(struct page *, enum zone_stat_item);
275 void mod_node_page_state(struct pglist_data *, enum node_stat_item, long);
276 void inc_node_page_state(struct page *, enum node_stat_item);
277 void dec_node_page_state(struct page *, enum node_stat_item);
279 extern void inc_node_state(struct pglist_data *, enum node_stat_item);
280 extern void __inc_zone_state(struct zone *, enum zone_stat_item);
281 extern void __inc_node_state(struct pglist_data *, enum node_stat_item);
282 extern void dec_zone_state(struct zone *, enum zone_stat_item);
283 extern void __dec_zone_state(struct zone *, enum zone_stat_item);
284 extern void __dec_node_state(struct pglist_data *, enum node_stat_item);
286 void quiet_vmstat(void);
287 void cpu_vm_stats_fold(int cpu);
288 void refresh_zone_stat_thresholds(void);
291 int vmstat_refresh(struct ctl_table *, int write, void *buffer, size_t *lenp,
294 void drain_zonestat(struct zone *zone, struct per_cpu_pageset *);
296 int calculate_pressure_threshold(struct zone *zone);
297 int calculate_normal_threshold(struct zone *zone);
298 void set_pgdat_percpu_threshold(pg_data_t *pgdat,
299 int (*calculate_pressure)(struct zone *));
300 #else /* CONFIG_SMP */
303 * We do not maintain differentials in a single processor configuration.
304 * The functions directly modify the zone and global counters.
306 static inline void __mod_zone_page_state(struct zone *zone,
307 enum zone_stat_item item, long delta)
309 zone_page_state_add(delta, zone, item);
312 static inline void __mod_node_page_state(struct pglist_data *pgdat,
313 enum node_stat_item item, int delta)
315 if (vmstat_item_in_bytes(item)) {
317 * Only cgroups use subpage accounting right now; at
318 * the global level, these items still change in
319 * multiples of whole pages. Store them as pages
320 * internally to keep the per-cpu counters compact.
322 VM_WARN_ON_ONCE(delta & (PAGE_SIZE - 1));
323 delta >>= PAGE_SHIFT;
326 node_page_state_add(delta, pgdat, item);
329 static inline void __inc_zone_state(struct zone *zone, enum zone_stat_item item)
331 atomic_long_inc(&zone->vm_stat[item]);
332 atomic_long_inc(&vm_zone_stat[item]);
335 static inline void __inc_node_state(struct pglist_data *pgdat, enum node_stat_item item)
337 atomic_long_inc(&pgdat->vm_stat[item]);
338 atomic_long_inc(&vm_node_stat[item]);
341 static inline void __dec_zone_state(struct zone *zone, enum zone_stat_item item)
343 atomic_long_dec(&zone->vm_stat[item]);
344 atomic_long_dec(&vm_zone_stat[item]);
347 static inline void __dec_node_state(struct pglist_data *pgdat, enum node_stat_item item)
349 atomic_long_dec(&pgdat->vm_stat[item]);
350 atomic_long_dec(&vm_node_stat[item]);
353 static inline void __inc_zone_page_state(struct page *page,
354 enum zone_stat_item item)
356 __inc_zone_state(page_zone(page), item);
359 static inline void __inc_node_page_state(struct page *page,
360 enum node_stat_item item)
362 __inc_node_state(page_pgdat(page), item);
366 static inline void __dec_zone_page_state(struct page *page,
367 enum zone_stat_item item)
369 __dec_zone_state(page_zone(page), item);
372 static inline void __dec_node_page_state(struct page *page,
373 enum node_stat_item item)
375 __dec_node_state(page_pgdat(page), item);
380 * We only use atomic operations to update counters. So there is no need to
381 * disable interrupts.
383 #define inc_zone_page_state __inc_zone_page_state
384 #define dec_zone_page_state __dec_zone_page_state
385 #define mod_zone_page_state __mod_zone_page_state
387 #define inc_node_page_state __inc_node_page_state
388 #define dec_node_page_state __dec_node_page_state
389 #define mod_node_page_state __mod_node_page_state
391 #define inc_zone_state __inc_zone_state
392 #define inc_node_state __inc_node_state
393 #define dec_zone_state __dec_zone_state
395 #define set_pgdat_percpu_threshold(pgdat, callback) { }
397 static inline void refresh_zone_stat_thresholds(void) { }
398 static inline void cpu_vm_stats_fold(int cpu) { }
399 static inline void quiet_vmstat(void) { }
401 static inline void drain_zonestat(struct zone *zone,
402 struct per_cpu_pageset *pset) { }
403 #endif /* CONFIG_SMP */
405 static inline void __mod_zone_freepage_state(struct zone *zone, int nr_pages,
408 __mod_zone_page_state(zone, NR_FREE_PAGES, nr_pages);
409 if (is_migrate_cma(migratetype))
410 __mod_zone_page_state(zone, NR_FREE_CMA_PAGES, nr_pages);
413 extern const char * const vmstat_text[];
415 static inline const char *zone_stat_name(enum zone_stat_item item)
417 return vmstat_text[item];
421 static inline const char *numa_stat_name(enum numa_stat_item item)
423 return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
426 #endif /* CONFIG_NUMA */
428 static inline const char *node_stat_name(enum node_stat_item item)
430 return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
431 NR_VM_NUMA_STAT_ITEMS +
435 static inline const char *lru_list_name(enum lru_list lru)
437 return node_stat_name(NR_LRU_BASE + lru) + 3; // skip "nr_"
440 static inline const char *writeback_stat_name(enum writeback_stat_item item)
442 return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
443 NR_VM_NUMA_STAT_ITEMS +
444 NR_VM_NODE_STAT_ITEMS +
448 #if defined(CONFIG_VM_EVENT_COUNTERS) || defined(CONFIG_MEMCG)
449 static inline const char *vm_event_name(enum vm_event_item item)
451 return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
452 NR_VM_NUMA_STAT_ITEMS +
453 NR_VM_NODE_STAT_ITEMS +
454 NR_VM_WRITEBACK_STAT_ITEMS +
457 #endif /* CONFIG_VM_EVENT_COUNTERS || CONFIG_MEMCG */
461 void __mod_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx,
464 static inline void mod_lruvec_state(struct lruvec *lruvec,
465 enum node_stat_item idx, int val)
469 local_irq_save(flags);
470 __mod_lruvec_state(lruvec, idx, val);
471 local_irq_restore(flags);
474 void __mod_lruvec_page_state(struct page *page,
475 enum node_stat_item idx, int val);
477 static inline void mod_lruvec_page_state(struct page *page,
478 enum node_stat_item idx, int val)
482 local_irq_save(flags);
483 __mod_lruvec_page_state(page, idx, val);
484 local_irq_restore(flags);
489 static inline void __mod_lruvec_state(struct lruvec *lruvec,
490 enum node_stat_item idx, int val)
492 __mod_node_page_state(lruvec_pgdat(lruvec), idx, val);
495 static inline void mod_lruvec_state(struct lruvec *lruvec,
496 enum node_stat_item idx, int val)
498 mod_node_page_state(lruvec_pgdat(lruvec), idx, val);
501 static inline void __mod_lruvec_page_state(struct page *page,
502 enum node_stat_item idx, int val)
504 __mod_node_page_state(page_pgdat(page), idx, val);
507 static inline void mod_lruvec_page_state(struct page *page,
508 enum node_stat_item idx, int val)
510 mod_node_page_state(page_pgdat(page), idx, val);
513 #endif /* CONFIG_MEMCG */
515 static inline void __inc_lruvec_state(struct lruvec *lruvec,
516 enum node_stat_item idx)
518 __mod_lruvec_state(lruvec, idx, 1);
521 static inline void __dec_lruvec_state(struct lruvec *lruvec,
522 enum node_stat_item idx)
524 __mod_lruvec_state(lruvec, idx, -1);
527 static inline void __inc_lruvec_page_state(struct page *page,
528 enum node_stat_item idx)
530 __mod_lruvec_page_state(page, idx, 1);
533 static inline void __dec_lruvec_page_state(struct page *page,
534 enum node_stat_item idx)
536 __mod_lruvec_page_state(page, idx, -1);
539 static inline void inc_lruvec_state(struct lruvec *lruvec,
540 enum node_stat_item idx)
542 mod_lruvec_state(lruvec, idx, 1);
545 static inline void dec_lruvec_state(struct lruvec *lruvec,
546 enum node_stat_item idx)
548 mod_lruvec_state(lruvec, idx, -1);
551 static inline void inc_lruvec_page_state(struct page *page,
552 enum node_stat_item idx)
554 mod_lruvec_page_state(page, idx, 1);
557 static inline void dec_lruvec_page_state(struct page *page,
558 enum node_stat_item idx)
560 mod_lruvec_page_state(page, idx, -1);
563 #endif /* _LINUX_VMSTAT_H */