1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _LINUX_VMSTAT_H 3 #define _LINUX_VMSTAT_H 4 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> 12 13 extern int sysctl_stat_interval; 14 15 #ifdef CONFIG_NUMA 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); 22 #endif 23 24 struct reclaim_stat { 25 unsigned nr_dirty; 26 unsigned nr_unqueued_dirty; 27 unsigned nr_congested; 28 unsigned nr_writeback; 29 unsigned nr_immediate; 30 unsigned nr_pageout; 31 unsigned nr_activate[ANON_AND_FILE]; 32 unsigned nr_ref_keep; 33 unsigned nr_unmap_fail; 34 unsigned nr_lazyfree_fail; 35 }; 36 37 enum writeback_stat_item { 38 NR_DIRTY_THRESHOLD, 39 NR_DIRTY_BG_THRESHOLD, 40 NR_VM_WRITEBACK_STAT_ITEMS, 41 }; 42 43 #ifdef CONFIG_VM_EVENT_COUNTERS 44 /* 45 * Light weight per cpu counter implementation. 46 * 47 * Counters should only be incremented and no critical kernel component 48 * should rely on the counter values. 49 * 50 * Counters are handled completely inline. On many platforms the code 51 * generated will simply be the increment of a global address. 52 */ 53 54 struct vm_event_state { 55 unsigned long event[NR_VM_EVENT_ITEMS]; 56 }; 57 58 DECLARE_PER_CPU(struct vm_event_state, vm_event_states); 59 60 /* 61 * vm counters are allowed to be racy. Use raw_cpu_ops to avoid the 62 * local_irq_disable overhead. 63 */ 64 static inline void __count_vm_event(enum vm_event_item item) 65 { 66 raw_cpu_inc(vm_event_states.event[item]); 67 } 68 69 static inline void count_vm_event(enum vm_event_item item) 70 { 71 this_cpu_inc(vm_event_states.event[item]); 72 } 73 74 static inline void __count_vm_events(enum vm_event_item item, long delta) 75 { 76 raw_cpu_add(vm_event_states.event[item], delta); 77 } 78 79 static inline void count_vm_events(enum vm_event_item item, long delta) 80 { 81 this_cpu_add(vm_event_states.event[item], delta); 82 } 83 84 extern void all_vm_events(unsigned long *); 85 86 extern void vm_events_fold_cpu(int cpu); 87 88 #else 89 90 /* Disable counters */ 91 static inline void count_vm_event(enum vm_event_item item) 92 { 93 } 94 static inline void count_vm_events(enum vm_event_item item, long delta) 95 { 96 } 97 static inline void __count_vm_event(enum vm_event_item item) 98 { 99 } 100 static inline void __count_vm_events(enum vm_event_item item, long delta) 101 { 102 } 103 static inline void all_vm_events(unsigned long *ret) 104 { 105 } 106 static inline void vm_events_fold_cpu(int cpu) 107 { 108 } 109 110 #endif /* CONFIG_VM_EVENT_COUNTERS */ 111 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) 115 #else 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 */ 119 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) 123 #else 124 #define count_vm_tlb_event(x) do {} while (0) 125 #define count_vm_tlb_events(x, y) do { (void)(y); } while (0) 126 #endif 127 128 #ifdef CONFIG_DEBUG_VM_VMACACHE 129 #define count_vm_vmacache_event(x) count_vm_event(x) 130 #else 131 #define count_vm_vmacache_event(x) do {} while (0) 132 #endif 133 134 #define __count_zid_vm_events(item, zid, delta) \ 135 __count_vm_events(item##_NORMAL - ZONE_NORMAL + zid, delta) 136 137 /* 138 * Zone and node-based page accounting with per cpu differentials. 139 */ 140 extern atomic_long_t vm_zone_stat[NR_VM_ZONE_STAT_ITEMS]; 141 extern atomic_long_t vm_node_stat[NR_VM_NODE_STAT_ITEMS]; 142 extern atomic_long_t vm_numa_event[NR_VM_NUMA_EVENT_ITEMS]; 143 144 #ifdef CONFIG_NUMA 145 static inline void zone_numa_event_add(long x, struct zone *zone, 146 enum numa_stat_item item) 147 { 148 atomic_long_add(x, &zone->vm_numa_event[item]); 149 atomic_long_add(x, &vm_numa_event[item]); 150 } 151 152 static inline unsigned long zone_numa_event_state(struct zone *zone, 153 enum numa_stat_item item) 154 { 155 return atomic_long_read(&zone->vm_numa_event[item]); 156 } 157 158 static inline unsigned long 159 global_numa_event_state(enum numa_stat_item item) 160 { 161 return atomic_long_read(&vm_numa_event[item]); 162 } 163 #endif /* CONFIG_NUMA */ 164 165 static inline void zone_page_state_add(long x, struct zone *zone, 166 enum zone_stat_item item) 167 { 168 atomic_long_add(x, &zone->vm_stat[item]); 169 atomic_long_add(x, &vm_zone_stat[item]); 170 } 171 172 static inline void node_page_state_add(long x, struct pglist_data *pgdat, 173 enum node_stat_item item) 174 { 175 atomic_long_add(x, &pgdat->vm_stat[item]); 176 atomic_long_add(x, &vm_node_stat[item]); 177 } 178 179 static inline unsigned long global_zone_page_state(enum zone_stat_item item) 180 { 181 long x = atomic_long_read(&vm_zone_stat[item]); 182 #ifdef CONFIG_SMP 183 if (x < 0) 184 x = 0; 185 #endif 186 return x; 187 } 188 189 static inline 190 unsigned long global_node_page_state_pages(enum node_stat_item item) 191 { 192 long x = atomic_long_read(&vm_node_stat[item]); 193 #ifdef CONFIG_SMP 194 if (x < 0) 195 x = 0; 196 #endif 197 return x; 198 } 199 200 static inline unsigned long global_node_page_state(enum node_stat_item item) 201 { 202 VM_WARN_ON_ONCE(vmstat_item_in_bytes(item)); 203 204 return global_node_page_state_pages(item); 205 } 206 207 static inline unsigned long zone_page_state(struct zone *zone, 208 enum zone_stat_item item) 209 { 210 long x = atomic_long_read(&zone->vm_stat[item]); 211 #ifdef CONFIG_SMP 212 if (x < 0) 213 x = 0; 214 #endif 215 return x; 216 } 217 218 /* 219 * More accurate version that also considers the currently pending 220 * deltas. For that we need to loop over all cpus to find the current 221 * deltas. There is no synchronization so the result cannot be 222 * exactly accurate either. 223 */ 224 static inline unsigned long zone_page_state_snapshot(struct zone *zone, 225 enum zone_stat_item item) 226 { 227 long x = atomic_long_read(&zone->vm_stat[item]); 228 229 #ifdef CONFIG_SMP 230 int cpu; 231 for_each_online_cpu(cpu) 232 x += per_cpu_ptr(zone->per_cpu_zonestats, cpu)->vm_stat_diff[item]; 233 234 if (x < 0) 235 x = 0; 236 #endif 237 return x; 238 } 239 240 #ifdef CONFIG_NUMA 241 extern void __count_numa_event(struct zone *zone, enum numa_stat_item item); 242 extern unsigned long sum_zone_node_page_state(int node, 243 enum zone_stat_item item); 244 extern unsigned long sum_zone_numa_event_state(int node, enum numa_stat_item item); 245 extern unsigned long node_page_state(struct pglist_data *pgdat, 246 enum node_stat_item item); 247 extern unsigned long node_page_state_pages(struct pglist_data *pgdat, 248 enum node_stat_item item); 249 extern void fold_vm_numa_events(void); 250 #else 251 #define sum_zone_node_page_state(node, item) global_zone_page_state(item) 252 #define node_page_state(node, item) global_node_page_state(item) 253 #define node_page_state_pages(node, item) global_node_page_state_pages(item) 254 static inline void fold_vm_numa_events(void) 255 { 256 } 257 #endif /* CONFIG_NUMA */ 258 259 #ifdef CONFIG_SMP 260 void __mod_zone_page_state(struct zone *, enum zone_stat_item item, long); 261 void __inc_zone_page_state(struct page *, enum zone_stat_item); 262 void __dec_zone_page_state(struct page *, enum zone_stat_item); 263 264 void __mod_node_page_state(struct pglist_data *, enum node_stat_item item, long); 265 void __inc_node_page_state(struct page *, enum node_stat_item); 266 void __dec_node_page_state(struct page *, enum node_stat_item); 267 268 void mod_zone_page_state(struct zone *, enum zone_stat_item, long); 269 void inc_zone_page_state(struct page *, enum zone_stat_item); 270 void dec_zone_page_state(struct page *, enum zone_stat_item); 271 272 void mod_node_page_state(struct pglist_data *, enum node_stat_item, long); 273 void inc_node_page_state(struct page *, enum node_stat_item); 274 void dec_node_page_state(struct page *, enum node_stat_item); 275 276 extern void inc_node_state(struct pglist_data *, enum node_stat_item); 277 extern void __inc_zone_state(struct zone *, enum zone_stat_item); 278 extern void __inc_node_state(struct pglist_data *, enum node_stat_item); 279 extern void dec_zone_state(struct zone *, enum zone_stat_item); 280 extern void __dec_zone_state(struct zone *, enum zone_stat_item); 281 extern void __dec_node_state(struct pglist_data *, enum node_stat_item); 282 283 void quiet_vmstat(void); 284 void cpu_vm_stats_fold(int cpu); 285 void refresh_zone_stat_thresholds(void); 286 287 struct ctl_table; 288 int vmstat_refresh(struct ctl_table *, int write, void *buffer, size_t *lenp, 289 loff_t *ppos); 290 291 void drain_zonestat(struct zone *zone, struct per_cpu_zonestat *); 292 293 int calculate_pressure_threshold(struct zone *zone); 294 int calculate_normal_threshold(struct zone *zone); 295 void set_pgdat_percpu_threshold(pg_data_t *pgdat, 296 int (*calculate_pressure)(struct zone *)); 297 #else /* CONFIG_SMP */ 298 299 /* 300 * We do not maintain differentials in a single processor configuration. 301 * The functions directly modify the zone and global counters. 302 */ 303 static inline void __mod_zone_page_state(struct zone *zone, 304 enum zone_stat_item item, long delta) 305 { 306 zone_page_state_add(delta, zone, item); 307 } 308 309 static inline void __mod_node_page_state(struct pglist_data *pgdat, 310 enum node_stat_item item, int delta) 311 { 312 if (vmstat_item_in_bytes(item)) { 313 /* 314 * Only cgroups use subpage accounting right now; at 315 * the global level, these items still change in 316 * multiples of whole pages. Store them as pages 317 * internally to keep the per-cpu counters compact. 318 */ 319 VM_WARN_ON_ONCE(delta & (PAGE_SIZE - 1)); 320 delta >>= PAGE_SHIFT; 321 } 322 323 node_page_state_add(delta, pgdat, item); 324 } 325 326 static inline void __inc_zone_state(struct zone *zone, enum zone_stat_item item) 327 { 328 atomic_long_inc(&zone->vm_stat[item]); 329 atomic_long_inc(&vm_zone_stat[item]); 330 } 331 332 static inline void __inc_node_state(struct pglist_data *pgdat, enum node_stat_item item) 333 { 334 atomic_long_inc(&pgdat->vm_stat[item]); 335 atomic_long_inc(&vm_node_stat[item]); 336 } 337 338 static inline void __dec_zone_state(struct zone *zone, enum zone_stat_item item) 339 { 340 atomic_long_dec(&zone->vm_stat[item]); 341 atomic_long_dec(&vm_zone_stat[item]); 342 } 343 344 static inline void __dec_node_state(struct pglist_data *pgdat, enum node_stat_item item) 345 { 346 atomic_long_dec(&pgdat->vm_stat[item]); 347 atomic_long_dec(&vm_node_stat[item]); 348 } 349 350 static inline void __inc_zone_page_state(struct page *page, 351 enum zone_stat_item item) 352 { 353 __inc_zone_state(page_zone(page), item); 354 } 355 356 static inline void __inc_node_page_state(struct page *page, 357 enum node_stat_item item) 358 { 359 __inc_node_state(page_pgdat(page), item); 360 } 361 362 363 static inline void __dec_zone_page_state(struct page *page, 364 enum zone_stat_item item) 365 { 366 __dec_zone_state(page_zone(page), item); 367 } 368 369 static inline void __dec_node_page_state(struct page *page, 370 enum node_stat_item item) 371 { 372 __dec_node_state(page_pgdat(page), item); 373 } 374 375 376 /* 377 * We only use atomic operations to update counters. So there is no need to 378 * disable interrupts. 379 */ 380 #define inc_zone_page_state __inc_zone_page_state 381 #define dec_zone_page_state __dec_zone_page_state 382 #define mod_zone_page_state __mod_zone_page_state 383 384 #define inc_node_page_state __inc_node_page_state 385 #define dec_node_page_state __dec_node_page_state 386 #define mod_node_page_state __mod_node_page_state 387 388 #define inc_zone_state __inc_zone_state 389 #define inc_node_state __inc_node_state 390 #define dec_zone_state __dec_zone_state 391 392 #define set_pgdat_percpu_threshold(pgdat, callback) { } 393 394 static inline void refresh_zone_stat_thresholds(void) { } 395 static inline void cpu_vm_stats_fold(int cpu) { } 396 static inline void quiet_vmstat(void) { } 397 398 static inline void drain_zonestat(struct zone *zone, 399 struct per_cpu_zonestat *pzstats) { } 400 #endif /* CONFIG_SMP */ 401 402 static inline void __mod_zone_freepage_state(struct zone *zone, int nr_pages, 403 int migratetype) 404 { 405 __mod_zone_page_state(zone, NR_FREE_PAGES, nr_pages); 406 if (is_migrate_cma(migratetype)) 407 __mod_zone_page_state(zone, NR_FREE_CMA_PAGES, nr_pages); 408 } 409 410 extern const char * const vmstat_text[]; 411 412 static inline const char *zone_stat_name(enum zone_stat_item item) 413 { 414 return vmstat_text[item]; 415 } 416 417 #ifdef CONFIG_NUMA 418 static inline const char *numa_stat_name(enum numa_stat_item item) 419 { 420 return vmstat_text[NR_VM_ZONE_STAT_ITEMS + 421 item]; 422 } 423 #endif /* CONFIG_NUMA */ 424 425 static inline const char *node_stat_name(enum node_stat_item item) 426 { 427 return vmstat_text[NR_VM_ZONE_STAT_ITEMS + 428 NR_VM_NUMA_EVENT_ITEMS + 429 item]; 430 } 431 432 static inline const char *lru_list_name(enum lru_list lru) 433 { 434 return node_stat_name(NR_LRU_BASE + lru) + 3; // skip "nr_" 435 } 436 437 static inline const char *writeback_stat_name(enum writeback_stat_item item) 438 { 439 return vmstat_text[NR_VM_ZONE_STAT_ITEMS + 440 NR_VM_NUMA_EVENT_ITEMS + 441 NR_VM_NODE_STAT_ITEMS + 442 item]; 443 } 444 445 #if defined(CONFIG_VM_EVENT_COUNTERS) || defined(CONFIG_MEMCG) 446 static inline const char *vm_event_name(enum vm_event_item item) 447 { 448 return vmstat_text[NR_VM_ZONE_STAT_ITEMS + 449 NR_VM_NUMA_EVENT_ITEMS + 450 NR_VM_NODE_STAT_ITEMS + 451 NR_VM_WRITEBACK_STAT_ITEMS + 452 item]; 453 } 454 #endif /* CONFIG_VM_EVENT_COUNTERS || CONFIG_MEMCG */ 455 456 #ifdef CONFIG_MEMCG 457 458 void __mod_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx, 459 int val); 460 461 static inline void mod_lruvec_state(struct lruvec *lruvec, 462 enum node_stat_item idx, int val) 463 { 464 unsigned long flags; 465 466 local_irq_save(flags); 467 __mod_lruvec_state(lruvec, idx, val); 468 local_irq_restore(flags); 469 } 470 471 void __mod_lruvec_page_state(struct page *page, 472 enum node_stat_item idx, int val); 473 474 static inline void mod_lruvec_page_state(struct page *page, 475 enum node_stat_item idx, int val) 476 { 477 unsigned long flags; 478 479 local_irq_save(flags); 480 __mod_lruvec_page_state(page, idx, val); 481 local_irq_restore(flags); 482 } 483 484 #else 485 486 static inline void __mod_lruvec_state(struct lruvec *lruvec, 487 enum node_stat_item idx, int val) 488 { 489 __mod_node_page_state(lruvec_pgdat(lruvec), idx, val); 490 } 491 492 static inline void mod_lruvec_state(struct lruvec *lruvec, 493 enum node_stat_item idx, int val) 494 { 495 mod_node_page_state(lruvec_pgdat(lruvec), idx, val); 496 } 497 498 static inline void __mod_lruvec_page_state(struct page *page, 499 enum node_stat_item idx, int val) 500 { 501 __mod_node_page_state(page_pgdat(page), idx, val); 502 } 503 504 static inline void mod_lruvec_page_state(struct page *page, 505 enum node_stat_item idx, int val) 506 { 507 mod_node_page_state(page_pgdat(page), idx, val); 508 } 509 510 #endif /* CONFIG_MEMCG */ 511 512 static inline void inc_lruvec_state(struct lruvec *lruvec, 513 enum node_stat_item idx) 514 { 515 mod_lruvec_state(lruvec, idx, 1); 516 } 517 518 static inline void __inc_lruvec_page_state(struct page *page, 519 enum node_stat_item idx) 520 { 521 __mod_lruvec_page_state(page, idx, 1); 522 } 523 524 static inline void __dec_lruvec_page_state(struct page *page, 525 enum node_stat_item idx) 526 { 527 __mod_lruvec_page_state(page, idx, -1); 528 } 529 530 static inline void inc_lruvec_page_state(struct page *page, 531 enum node_stat_item idx) 532 { 533 mod_lruvec_page_state(page, idx, 1); 534 } 535 536 static inline void dec_lruvec_page_state(struct page *page, 537 enum node_stat_item idx) 538 { 539 mod_lruvec_page_state(page, idx, -1); 540 } 541 542 #endif /* _LINUX_VMSTAT_H */ 543