1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* memcontrol.h - Memory Controller 3 * 4 * Copyright IBM Corporation, 2007 5 * Author Balbir Singh <[email protected]> 6 * 7 * Copyright 2007 OpenVZ SWsoft Inc 8 * Author: Pavel Emelianov <[email protected]> 9 */ 10 11 #ifndef _LINUX_MEMCONTROL_H 12 #define _LINUX_MEMCONTROL_H 13 #include <linux/cgroup.h> 14 #include <linux/vm_event_item.h> 15 #include <linux/hardirq.h> 16 #include <linux/jump_label.h> 17 #include <linux/page_counter.h> 18 #include <linux/vmpressure.h> 19 #include <linux/eventfd.h> 20 #include <linux/mm.h> 21 #include <linux/vmstat.h> 22 #include <linux/writeback.h> 23 #include <linux/page-flags.h> 24 25 struct mem_cgroup; 26 struct obj_cgroup; 27 struct page; 28 struct mm_struct; 29 struct kmem_cache; 30 31 /* Cgroup-specific page state, on top of universal node page state */ 32 enum memcg_stat_item { 33 MEMCG_SWAP = NR_VM_NODE_STAT_ITEMS, 34 MEMCG_SOCK, 35 MEMCG_PERCPU_B, 36 MEMCG_VMALLOC, 37 MEMCG_KMEM, 38 MEMCG_ZSWAP_B, 39 MEMCG_ZSWAPPED, 40 MEMCG_NR_STAT, 41 }; 42 43 enum memcg_memory_event { 44 MEMCG_LOW, 45 MEMCG_HIGH, 46 MEMCG_MAX, 47 MEMCG_OOM, 48 MEMCG_OOM_KILL, 49 MEMCG_OOM_GROUP_KILL, 50 MEMCG_SWAP_HIGH, 51 MEMCG_SWAP_MAX, 52 MEMCG_SWAP_FAIL, 53 MEMCG_NR_MEMORY_EVENTS, 54 }; 55 56 struct mem_cgroup_reclaim_cookie { 57 pg_data_t *pgdat; 58 unsigned int generation; 59 }; 60 61 #ifdef CONFIG_MEMCG 62 63 #define MEM_CGROUP_ID_SHIFT 16 64 65 struct mem_cgroup_id { 66 int id; 67 refcount_t ref; 68 }; 69 70 /* 71 * Per memcg event counter is incremented at every pagein/pageout. With THP, 72 * it will be incremented by the number of pages. This counter is used 73 * to trigger some periodic events. This is straightforward and better 74 * than using jiffies etc. to handle periodic memcg event. 75 */ 76 enum mem_cgroup_events_target { 77 MEM_CGROUP_TARGET_THRESH, 78 MEM_CGROUP_TARGET_SOFTLIMIT, 79 MEM_CGROUP_NTARGETS, 80 }; 81 82 struct memcg_vmstats_percpu; 83 struct memcg_vmstats; 84 85 struct mem_cgroup_reclaim_iter { 86 struct mem_cgroup *position; 87 /* scan generation, increased every round-trip */ 88 unsigned int generation; 89 }; 90 91 /* 92 * Bitmap and deferred work of shrinker::id corresponding to memcg-aware 93 * shrinkers, which have elements charged to this memcg. 94 */ 95 struct shrinker_info { 96 struct rcu_head rcu; 97 atomic_long_t *nr_deferred; 98 unsigned long *map; 99 int map_nr_max; 100 }; 101 102 struct lruvec_stats_percpu { 103 /* Local (CPU and cgroup) state */ 104 long state[NR_VM_NODE_STAT_ITEMS]; 105 106 /* Delta calculation for lockless upward propagation */ 107 long state_prev[NR_VM_NODE_STAT_ITEMS]; 108 }; 109 110 struct lruvec_stats { 111 /* Aggregated (CPU and subtree) state */ 112 long state[NR_VM_NODE_STAT_ITEMS]; 113 114 /* Pending child counts during tree propagation */ 115 long state_pending[NR_VM_NODE_STAT_ITEMS]; 116 }; 117 118 /* 119 * per-node information in memory controller. 120 */ 121 struct mem_cgroup_per_node { 122 struct lruvec lruvec; 123 124 struct lruvec_stats_percpu __percpu *lruvec_stats_percpu; 125 struct lruvec_stats lruvec_stats; 126 127 unsigned long lru_zone_size[MAX_NR_ZONES][NR_LRU_LISTS]; 128 129 struct mem_cgroup_reclaim_iter iter; 130 131 struct shrinker_info __rcu *shrinker_info; 132 133 struct rb_node tree_node; /* RB tree node */ 134 unsigned long usage_in_excess;/* Set to the value by which */ 135 /* the soft limit is exceeded*/ 136 bool on_tree; 137 struct mem_cgroup *memcg; /* Back pointer, we cannot */ 138 /* use container_of */ 139 }; 140 141 struct mem_cgroup_threshold { 142 struct eventfd_ctx *eventfd; 143 unsigned long threshold; 144 }; 145 146 /* For threshold */ 147 struct mem_cgroup_threshold_ary { 148 /* An array index points to threshold just below or equal to usage. */ 149 int current_threshold; 150 /* Size of entries[] */ 151 unsigned int size; 152 /* Array of thresholds */ 153 struct mem_cgroup_threshold entries[]; 154 }; 155 156 struct mem_cgroup_thresholds { 157 /* Primary thresholds array */ 158 struct mem_cgroup_threshold_ary *primary; 159 /* 160 * Spare threshold array. 161 * This is needed to make mem_cgroup_unregister_event() "never fail". 162 * It must be able to store at least primary->size - 1 entries. 163 */ 164 struct mem_cgroup_threshold_ary *spare; 165 }; 166 167 /* 168 * Remember four most recent foreign writebacks with dirty pages in this 169 * cgroup. Inode sharing is expected to be uncommon and, even if we miss 170 * one in a given round, we're likely to catch it later if it keeps 171 * foreign-dirtying, so a fairly low count should be enough. 172 * 173 * See mem_cgroup_track_foreign_dirty_slowpath() for details. 174 */ 175 #define MEMCG_CGWB_FRN_CNT 4 176 177 struct memcg_cgwb_frn { 178 u64 bdi_id; /* bdi->id of the foreign inode */ 179 int memcg_id; /* memcg->css.id of foreign inode */ 180 u64 at; /* jiffies_64 at the time of dirtying */ 181 struct wb_completion done; /* tracks in-flight foreign writebacks */ 182 }; 183 184 /* 185 * Bucket for arbitrarily byte-sized objects charged to a memory 186 * cgroup. The bucket can be reparented in one piece when the cgroup 187 * is destroyed, without having to round up the individual references 188 * of all live memory objects in the wild. 189 */ 190 struct obj_cgroup { 191 struct percpu_ref refcnt; 192 struct mem_cgroup *memcg; 193 atomic_t nr_charged_bytes; 194 union { 195 struct list_head list; /* protected by objcg_lock */ 196 struct rcu_head rcu; 197 }; 198 }; 199 200 /* 201 * The memory controller data structure. The memory controller controls both 202 * page cache and RSS per cgroup. We would eventually like to provide 203 * statistics based on the statistics developed by Rik Van Riel for clock-pro, 204 * to help the administrator determine what knobs to tune. 205 */ 206 struct mem_cgroup { 207 struct cgroup_subsys_state css; 208 209 /* Private memcg ID. Used to ID objects that outlive the cgroup */ 210 struct mem_cgroup_id id; 211 212 /* Accounted resources */ 213 struct page_counter memory; /* Both v1 & v2 */ 214 215 union { 216 struct page_counter swap; /* v2 only */ 217 struct page_counter memsw; /* v1 only */ 218 }; 219 220 /* Legacy consumer-oriented counters */ 221 struct page_counter kmem; /* v1 only */ 222 struct page_counter tcpmem; /* v1 only */ 223 224 /* Range enforcement for interrupt charges */ 225 struct work_struct high_work; 226 227 #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP) 228 unsigned long zswap_max; 229 #endif 230 231 unsigned long soft_limit; 232 233 /* vmpressure notifications */ 234 struct vmpressure vmpressure; 235 236 /* 237 * Should the OOM killer kill all belonging tasks, had it kill one? 238 */ 239 bool oom_group; 240 241 /* protected by memcg_oom_lock */ 242 bool oom_lock; 243 int under_oom; 244 245 int swappiness; 246 /* OOM-Killer disable */ 247 int oom_kill_disable; 248 249 /* memory.events and memory.events.local */ 250 struct cgroup_file events_file; 251 struct cgroup_file events_local_file; 252 253 /* handle for "memory.swap.events" */ 254 struct cgroup_file swap_events_file; 255 256 /* protect arrays of thresholds */ 257 struct mutex thresholds_lock; 258 259 /* thresholds for memory usage. RCU-protected */ 260 struct mem_cgroup_thresholds thresholds; 261 262 /* thresholds for mem+swap usage. RCU-protected */ 263 struct mem_cgroup_thresholds memsw_thresholds; 264 265 /* For oom notifier event fd */ 266 struct list_head oom_notify; 267 268 /* 269 * Should we move charges of a task when a task is moved into this 270 * mem_cgroup ? And what type of charges should we move ? 271 */ 272 unsigned long move_charge_at_immigrate; 273 /* taken only while moving_account > 0 */ 274 spinlock_t move_lock; 275 unsigned long move_lock_flags; 276 277 CACHELINE_PADDING(_pad1_); 278 279 /* memory.stat */ 280 struct memcg_vmstats *vmstats; 281 282 /* memory.events */ 283 atomic_long_t memory_events[MEMCG_NR_MEMORY_EVENTS]; 284 atomic_long_t memory_events_local[MEMCG_NR_MEMORY_EVENTS]; 285 286 unsigned long socket_pressure; 287 288 /* Legacy tcp memory accounting */ 289 bool tcpmem_active; 290 int tcpmem_pressure; 291 292 #ifdef CONFIG_MEMCG_KMEM 293 int kmemcg_id; 294 struct obj_cgroup __rcu *objcg; 295 /* list of inherited objcgs, protected by objcg_lock */ 296 struct list_head objcg_list; 297 #endif 298 299 CACHELINE_PADDING(_pad2_); 300 301 /* 302 * set > 0 if pages under this cgroup are moving to other cgroup. 303 */ 304 atomic_t moving_account; 305 struct task_struct *move_lock_task; 306 307 struct memcg_vmstats_percpu __percpu *vmstats_percpu; 308 309 #ifdef CONFIG_CGROUP_WRITEBACK 310 struct list_head cgwb_list; 311 struct wb_domain cgwb_domain; 312 struct memcg_cgwb_frn cgwb_frn[MEMCG_CGWB_FRN_CNT]; 313 #endif 314 315 /* List of events which userspace want to receive */ 316 struct list_head event_list; 317 spinlock_t event_list_lock; 318 319 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 320 struct deferred_split deferred_split_queue; 321 #endif 322 323 #ifdef CONFIG_LRU_GEN 324 /* per-memcg mm_struct list */ 325 struct lru_gen_mm_list mm_list; 326 #endif 327 328 struct mem_cgroup_per_node *nodeinfo[]; 329 }; 330 331 /* 332 * size of first charge trial. 333 * TODO: maybe necessary to use big numbers in big irons or dynamic based of the 334 * workload. 335 */ 336 #define MEMCG_CHARGE_BATCH 64U 337 338 extern struct mem_cgroup *root_mem_cgroup; 339 340 enum page_memcg_data_flags { 341 /* page->memcg_data is a pointer to an objcgs vector */ 342 MEMCG_DATA_OBJCGS = (1UL << 0), 343 /* page has been accounted as a non-slab kernel page */ 344 MEMCG_DATA_KMEM = (1UL << 1), 345 /* the next bit after the last actual flag */ 346 __NR_MEMCG_DATA_FLAGS = (1UL << 2), 347 }; 348 349 #define MEMCG_DATA_FLAGS_MASK (__NR_MEMCG_DATA_FLAGS - 1) 350 351 static inline bool folio_memcg_kmem(struct folio *folio); 352 353 /* 354 * After the initialization objcg->memcg is always pointing at 355 * a valid memcg, but can be atomically swapped to the parent memcg. 356 * 357 * The caller must ensure that the returned memcg won't be released: 358 * e.g. acquire the rcu_read_lock or css_set_lock. 359 */ 360 static inline struct mem_cgroup *obj_cgroup_memcg(struct obj_cgroup *objcg) 361 { 362 return READ_ONCE(objcg->memcg); 363 } 364 365 /* 366 * __folio_memcg - Get the memory cgroup associated with a non-kmem folio 367 * @folio: Pointer to the folio. 368 * 369 * Returns a pointer to the memory cgroup associated with the folio, 370 * or NULL. This function assumes that the folio is known to have a 371 * proper memory cgroup pointer. It's not safe to call this function 372 * against some type of folios, e.g. slab folios or ex-slab folios or 373 * kmem folios. 374 */ 375 static inline struct mem_cgroup *__folio_memcg(struct folio *folio) 376 { 377 unsigned long memcg_data = folio->memcg_data; 378 379 VM_BUG_ON_FOLIO(folio_test_slab(folio), folio); 380 VM_BUG_ON_FOLIO(memcg_data & MEMCG_DATA_OBJCGS, folio); 381 VM_BUG_ON_FOLIO(memcg_data & MEMCG_DATA_KMEM, folio); 382 383 return (struct mem_cgroup *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK); 384 } 385 386 /* 387 * __folio_objcg - get the object cgroup associated with a kmem folio. 388 * @folio: Pointer to the folio. 389 * 390 * Returns a pointer to the object cgroup associated with the folio, 391 * or NULL. This function assumes that the folio is known to have a 392 * proper object cgroup pointer. It's not safe to call this function 393 * against some type of folios, e.g. slab folios or ex-slab folios or 394 * LRU folios. 395 */ 396 static inline struct obj_cgroup *__folio_objcg(struct folio *folio) 397 { 398 unsigned long memcg_data = folio->memcg_data; 399 400 VM_BUG_ON_FOLIO(folio_test_slab(folio), folio); 401 VM_BUG_ON_FOLIO(memcg_data & MEMCG_DATA_OBJCGS, folio); 402 VM_BUG_ON_FOLIO(!(memcg_data & MEMCG_DATA_KMEM), folio); 403 404 return (struct obj_cgroup *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK); 405 } 406 407 /* 408 * folio_memcg - Get the memory cgroup associated with a folio. 409 * @folio: Pointer to the folio. 410 * 411 * Returns a pointer to the memory cgroup associated with the folio, 412 * or NULL. This function assumes that the folio is known to have a 413 * proper memory cgroup pointer. It's not safe to call this function 414 * against some type of folios, e.g. slab folios or ex-slab folios. 415 * 416 * For a non-kmem folio any of the following ensures folio and memcg binding 417 * stability: 418 * 419 * - the folio lock 420 * - LRU isolation 421 * - folio_memcg_lock() 422 * - exclusive reference 423 * - mem_cgroup_trylock_pages() 424 * 425 * For a kmem folio a caller should hold an rcu read lock to protect memcg 426 * associated with a kmem folio from being released. 427 */ 428 static inline struct mem_cgroup *folio_memcg(struct folio *folio) 429 { 430 if (folio_memcg_kmem(folio)) 431 return obj_cgroup_memcg(__folio_objcg(folio)); 432 return __folio_memcg(folio); 433 } 434 435 static inline struct mem_cgroup *page_memcg(struct page *page) 436 { 437 return folio_memcg(page_folio(page)); 438 } 439 440 /** 441 * folio_memcg_rcu - Locklessly get the memory cgroup associated with a folio. 442 * @folio: Pointer to the folio. 443 * 444 * This function assumes that the folio is known to have a 445 * proper memory cgroup pointer. It's not safe to call this function 446 * against some type of folios, e.g. slab folios or ex-slab folios. 447 * 448 * Return: A pointer to the memory cgroup associated with the folio, 449 * or NULL. 450 */ 451 static inline struct mem_cgroup *folio_memcg_rcu(struct folio *folio) 452 { 453 unsigned long memcg_data = READ_ONCE(folio->memcg_data); 454 455 VM_BUG_ON_FOLIO(folio_test_slab(folio), folio); 456 WARN_ON_ONCE(!rcu_read_lock_held()); 457 458 if (memcg_data & MEMCG_DATA_KMEM) { 459 struct obj_cgroup *objcg; 460 461 objcg = (void *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK); 462 return obj_cgroup_memcg(objcg); 463 } 464 465 return (struct mem_cgroup *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK); 466 } 467 468 /* 469 * folio_memcg_check - Get the memory cgroup associated with a folio. 470 * @folio: Pointer to the folio. 471 * 472 * Returns a pointer to the memory cgroup associated with the folio, 473 * or NULL. This function unlike folio_memcg() can take any folio 474 * as an argument. It has to be used in cases when it's not known if a folio 475 * has an associated memory cgroup pointer or an object cgroups vector or 476 * an object cgroup. 477 * 478 * For a non-kmem folio any of the following ensures folio and memcg binding 479 * stability: 480 * 481 * - the folio lock 482 * - LRU isolation 483 * - lock_folio_memcg() 484 * - exclusive reference 485 * - mem_cgroup_trylock_pages() 486 * 487 * For a kmem folio a caller should hold an rcu read lock to protect memcg 488 * associated with a kmem folio from being released. 489 */ 490 static inline struct mem_cgroup *folio_memcg_check(struct folio *folio) 491 { 492 /* 493 * Because folio->memcg_data might be changed asynchronously 494 * for slabs, READ_ONCE() should be used here. 495 */ 496 unsigned long memcg_data = READ_ONCE(folio->memcg_data); 497 498 if (memcg_data & MEMCG_DATA_OBJCGS) 499 return NULL; 500 501 if (memcg_data & MEMCG_DATA_KMEM) { 502 struct obj_cgroup *objcg; 503 504 objcg = (void *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK); 505 return obj_cgroup_memcg(objcg); 506 } 507 508 return (struct mem_cgroup *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK); 509 } 510 511 static inline struct mem_cgroup *page_memcg_check(struct page *page) 512 { 513 if (PageTail(page)) 514 return NULL; 515 return folio_memcg_check((struct folio *)page); 516 } 517 518 static inline struct mem_cgroup *get_mem_cgroup_from_objcg(struct obj_cgroup *objcg) 519 { 520 struct mem_cgroup *memcg; 521 522 rcu_read_lock(); 523 retry: 524 memcg = obj_cgroup_memcg(objcg); 525 if (unlikely(!css_tryget(&memcg->css))) 526 goto retry; 527 rcu_read_unlock(); 528 529 return memcg; 530 } 531 532 #ifdef CONFIG_MEMCG_KMEM 533 /* 534 * folio_memcg_kmem - Check if the folio has the memcg_kmem flag set. 535 * @folio: Pointer to the folio. 536 * 537 * Checks if the folio has MemcgKmem flag set. The caller must ensure 538 * that the folio has an associated memory cgroup. It's not safe to call 539 * this function against some types of folios, e.g. slab folios. 540 */ 541 static inline bool folio_memcg_kmem(struct folio *folio) 542 { 543 VM_BUG_ON_PGFLAGS(PageTail(&folio->page), &folio->page); 544 VM_BUG_ON_FOLIO(folio->memcg_data & MEMCG_DATA_OBJCGS, folio); 545 return folio->memcg_data & MEMCG_DATA_KMEM; 546 } 547 548 549 #else 550 static inline bool folio_memcg_kmem(struct folio *folio) 551 { 552 return false; 553 } 554 555 #endif 556 557 static inline bool PageMemcgKmem(struct page *page) 558 { 559 return folio_memcg_kmem(page_folio(page)); 560 } 561 562 static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg) 563 { 564 return (memcg == root_mem_cgroup); 565 } 566 567 static inline bool mem_cgroup_disabled(void) 568 { 569 return !cgroup_subsys_enabled(memory_cgrp_subsys); 570 } 571 572 static inline void mem_cgroup_protection(struct mem_cgroup *root, 573 struct mem_cgroup *memcg, 574 unsigned long *min, 575 unsigned long *low) 576 { 577 *min = *low = 0; 578 579 if (mem_cgroup_disabled()) 580 return; 581 582 /* 583 * There is no reclaim protection applied to a targeted reclaim. 584 * We are special casing this specific case here because 585 * mem_cgroup_protected calculation is not robust enough to keep 586 * the protection invariant for calculated effective values for 587 * parallel reclaimers with different reclaim target. This is 588 * especially a problem for tail memcgs (as they have pages on LRU) 589 * which would want to have effective values 0 for targeted reclaim 590 * but a different value for external reclaim. 591 * 592 * Example 593 * Let's have global and A's reclaim in parallel: 594 * | 595 * A (low=2G, usage = 3G, max = 3G, children_low_usage = 1.5G) 596 * |\ 597 * | C (low = 1G, usage = 2.5G) 598 * B (low = 1G, usage = 0.5G) 599 * 600 * For the global reclaim 601 * A.elow = A.low 602 * B.elow = min(B.usage, B.low) because children_low_usage <= A.elow 603 * C.elow = min(C.usage, C.low) 604 * 605 * With the effective values resetting we have A reclaim 606 * A.elow = 0 607 * B.elow = B.low 608 * C.elow = C.low 609 * 610 * If the global reclaim races with A's reclaim then 611 * B.elow = C.elow = 0 because children_low_usage > A.elow) 612 * is possible and reclaiming B would be violating the protection. 613 * 614 */ 615 if (root == memcg) 616 return; 617 618 *min = READ_ONCE(memcg->memory.emin); 619 *low = READ_ONCE(memcg->memory.elow); 620 } 621 622 void mem_cgroup_calculate_protection(struct mem_cgroup *root, 623 struct mem_cgroup *memcg); 624 625 static inline bool mem_cgroup_unprotected(struct mem_cgroup *target, 626 struct mem_cgroup *memcg) 627 { 628 /* 629 * The root memcg doesn't account charges, and doesn't support 630 * protection. The target memcg's protection is ignored, see 631 * mem_cgroup_calculate_protection() and mem_cgroup_protection() 632 */ 633 return mem_cgroup_disabled() || mem_cgroup_is_root(memcg) || 634 memcg == target; 635 } 636 637 static inline bool mem_cgroup_below_low(struct mem_cgroup *target, 638 struct mem_cgroup *memcg) 639 { 640 if (mem_cgroup_unprotected(target, memcg)) 641 return false; 642 643 return READ_ONCE(memcg->memory.elow) >= 644 page_counter_read(&memcg->memory); 645 } 646 647 static inline bool mem_cgroup_below_min(struct mem_cgroup *target, 648 struct mem_cgroup *memcg) 649 { 650 if (mem_cgroup_unprotected(target, memcg)) 651 return false; 652 653 return READ_ONCE(memcg->memory.emin) >= 654 page_counter_read(&memcg->memory); 655 } 656 657 int __mem_cgroup_charge(struct folio *folio, struct mm_struct *mm, gfp_t gfp); 658 659 /** 660 * mem_cgroup_charge - Charge a newly allocated folio to a cgroup. 661 * @folio: Folio to charge. 662 * @mm: mm context of the allocating task. 663 * @gfp: Reclaim mode. 664 * 665 * Try to charge @folio to the memcg that @mm belongs to, reclaiming 666 * pages according to @gfp if necessary. If @mm is NULL, try to 667 * charge to the active memcg. 668 * 669 * Do not use this for folios allocated for swapin. 670 * 671 * Return: 0 on success. Otherwise, an error code is returned. 672 */ 673 static inline int mem_cgroup_charge(struct folio *folio, struct mm_struct *mm, 674 gfp_t gfp) 675 { 676 if (mem_cgroup_disabled()) 677 return 0; 678 return __mem_cgroup_charge(folio, mm, gfp); 679 } 680 681 int mem_cgroup_swapin_charge_folio(struct folio *folio, struct mm_struct *mm, 682 gfp_t gfp, swp_entry_t entry); 683 void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry); 684 685 void __mem_cgroup_uncharge(struct folio *folio); 686 687 /** 688 * mem_cgroup_uncharge - Uncharge a folio. 689 * @folio: Folio to uncharge. 690 * 691 * Uncharge a folio previously charged with mem_cgroup_charge(). 692 */ 693 static inline void mem_cgroup_uncharge(struct folio *folio) 694 { 695 if (mem_cgroup_disabled()) 696 return; 697 __mem_cgroup_uncharge(folio); 698 } 699 700 void __mem_cgroup_uncharge_list(struct list_head *page_list); 701 static inline void mem_cgroup_uncharge_list(struct list_head *page_list) 702 { 703 if (mem_cgroup_disabled()) 704 return; 705 __mem_cgroup_uncharge_list(page_list); 706 } 707 708 void mem_cgroup_migrate(struct folio *old, struct folio *new); 709 710 /** 711 * mem_cgroup_lruvec - get the lru list vector for a memcg & node 712 * @memcg: memcg of the wanted lruvec 713 * @pgdat: pglist_data 714 * 715 * Returns the lru list vector holding pages for a given @memcg & 716 * @pgdat combination. This can be the node lruvec, if the memory 717 * controller is disabled. 718 */ 719 static inline struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg, 720 struct pglist_data *pgdat) 721 { 722 struct mem_cgroup_per_node *mz; 723 struct lruvec *lruvec; 724 725 if (mem_cgroup_disabled()) { 726 lruvec = &pgdat->__lruvec; 727 goto out; 728 } 729 730 if (!memcg) 731 memcg = root_mem_cgroup; 732 733 mz = memcg->nodeinfo[pgdat->node_id]; 734 lruvec = &mz->lruvec; 735 out: 736 /* 737 * Since a node can be onlined after the mem_cgroup was created, 738 * we have to be prepared to initialize lruvec->pgdat here; 739 * and if offlined then reonlined, we need to reinitialize it. 740 */ 741 if (unlikely(lruvec->pgdat != pgdat)) 742 lruvec->pgdat = pgdat; 743 return lruvec; 744 } 745 746 /** 747 * folio_lruvec - return lruvec for isolating/putting an LRU folio 748 * @folio: Pointer to the folio. 749 * 750 * This function relies on folio->mem_cgroup being stable. 751 */ 752 static inline struct lruvec *folio_lruvec(struct folio *folio) 753 { 754 struct mem_cgroup *memcg = folio_memcg(folio); 755 756 VM_WARN_ON_ONCE_FOLIO(!memcg && !mem_cgroup_disabled(), folio); 757 return mem_cgroup_lruvec(memcg, folio_pgdat(folio)); 758 } 759 760 struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p); 761 762 struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm); 763 764 struct lruvec *folio_lruvec_lock(struct folio *folio); 765 struct lruvec *folio_lruvec_lock_irq(struct folio *folio); 766 struct lruvec *folio_lruvec_lock_irqsave(struct folio *folio, 767 unsigned long *flags); 768 769 #ifdef CONFIG_DEBUG_VM 770 void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio); 771 #else 772 static inline 773 void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio) 774 { 775 } 776 #endif 777 778 static inline 779 struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css){ 780 return css ? container_of(css, struct mem_cgroup, css) : NULL; 781 } 782 783 static inline bool obj_cgroup_tryget(struct obj_cgroup *objcg) 784 { 785 return percpu_ref_tryget(&objcg->refcnt); 786 } 787 788 static inline void obj_cgroup_get(struct obj_cgroup *objcg) 789 { 790 percpu_ref_get(&objcg->refcnt); 791 } 792 793 static inline void obj_cgroup_get_many(struct obj_cgroup *objcg, 794 unsigned long nr) 795 { 796 percpu_ref_get_many(&objcg->refcnt, nr); 797 } 798 799 static inline void obj_cgroup_put(struct obj_cgroup *objcg) 800 { 801 percpu_ref_put(&objcg->refcnt); 802 } 803 804 static inline bool mem_cgroup_tryget(struct mem_cgroup *memcg) 805 { 806 return !memcg || css_tryget(&memcg->css); 807 } 808 809 static inline void mem_cgroup_put(struct mem_cgroup *memcg) 810 { 811 if (memcg) 812 css_put(&memcg->css); 813 } 814 815 #define mem_cgroup_from_counter(counter, member) \ 816 container_of(counter, struct mem_cgroup, member) 817 818 struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *, 819 struct mem_cgroup *, 820 struct mem_cgroup_reclaim_cookie *); 821 void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *); 822 void mem_cgroup_scan_tasks(struct mem_cgroup *memcg, 823 int (*)(struct task_struct *, void *), void *arg); 824 825 static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg) 826 { 827 if (mem_cgroup_disabled()) 828 return 0; 829 830 return memcg->id.id; 831 } 832 struct mem_cgroup *mem_cgroup_from_id(unsigned short id); 833 834 #ifdef CONFIG_SHRINKER_DEBUG 835 static inline unsigned long mem_cgroup_ino(struct mem_cgroup *memcg) 836 { 837 return memcg ? cgroup_ino(memcg->css.cgroup) : 0; 838 } 839 840 struct mem_cgroup *mem_cgroup_get_from_ino(unsigned long ino); 841 #endif 842 843 static inline struct mem_cgroup *mem_cgroup_from_seq(struct seq_file *m) 844 { 845 return mem_cgroup_from_css(seq_css(m)); 846 } 847 848 static inline struct mem_cgroup *lruvec_memcg(struct lruvec *lruvec) 849 { 850 struct mem_cgroup_per_node *mz; 851 852 if (mem_cgroup_disabled()) 853 return NULL; 854 855 mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec); 856 return mz->memcg; 857 } 858 859 /** 860 * parent_mem_cgroup - find the accounting parent of a memcg 861 * @memcg: memcg whose parent to find 862 * 863 * Returns the parent memcg, or NULL if this is the root or the memory 864 * controller is in legacy no-hierarchy mode. 865 */ 866 static inline struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg) 867 { 868 return mem_cgroup_from_css(memcg->css.parent); 869 } 870 871 static inline bool mem_cgroup_is_descendant(struct mem_cgroup *memcg, 872 struct mem_cgroup *root) 873 { 874 if (root == memcg) 875 return true; 876 return cgroup_is_descendant(memcg->css.cgroup, root->css.cgroup); 877 } 878 879 static inline bool mm_match_cgroup(struct mm_struct *mm, 880 struct mem_cgroup *memcg) 881 { 882 struct mem_cgroup *task_memcg; 883 bool match = false; 884 885 rcu_read_lock(); 886 task_memcg = mem_cgroup_from_task(rcu_dereference(mm->owner)); 887 if (task_memcg) 888 match = mem_cgroup_is_descendant(task_memcg, memcg); 889 rcu_read_unlock(); 890 return match; 891 } 892 893 struct cgroup_subsys_state *mem_cgroup_css_from_folio(struct folio *folio); 894 ino_t page_cgroup_ino(struct page *page); 895 896 static inline bool mem_cgroup_online(struct mem_cgroup *memcg) 897 { 898 if (mem_cgroup_disabled()) 899 return true; 900 return !!(memcg->css.flags & CSS_ONLINE); 901 } 902 903 void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru, 904 int zid, int nr_pages); 905 906 static inline 907 unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec, 908 enum lru_list lru, int zone_idx) 909 { 910 struct mem_cgroup_per_node *mz; 911 912 mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec); 913 return READ_ONCE(mz->lru_zone_size[zone_idx][lru]); 914 } 915 916 void mem_cgroup_handle_over_high(void); 917 918 unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg); 919 920 unsigned long mem_cgroup_size(struct mem_cgroup *memcg); 921 922 void mem_cgroup_print_oom_context(struct mem_cgroup *memcg, 923 struct task_struct *p); 924 925 void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg); 926 927 static inline void mem_cgroup_enter_user_fault(void) 928 { 929 WARN_ON(current->in_user_fault); 930 current->in_user_fault = 1; 931 } 932 933 static inline void mem_cgroup_exit_user_fault(void) 934 { 935 WARN_ON(!current->in_user_fault); 936 current->in_user_fault = 0; 937 } 938 939 static inline bool task_in_memcg_oom(struct task_struct *p) 940 { 941 return p->memcg_in_oom; 942 } 943 944 bool mem_cgroup_oom_synchronize(bool wait); 945 struct mem_cgroup *mem_cgroup_get_oom_group(struct task_struct *victim, 946 struct mem_cgroup *oom_domain); 947 void mem_cgroup_print_oom_group(struct mem_cgroup *memcg); 948 949 void folio_memcg_lock(struct folio *folio); 950 void folio_memcg_unlock(struct folio *folio); 951 952 void __mod_memcg_state(struct mem_cgroup *memcg, int idx, int val); 953 954 /* try to stablize folio_memcg() for all the pages in a memcg */ 955 static inline bool mem_cgroup_trylock_pages(struct mem_cgroup *memcg) 956 { 957 rcu_read_lock(); 958 959 if (mem_cgroup_disabled() || !atomic_read(&memcg->moving_account)) 960 return true; 961 962 rcu_read_unlock(); 963 return false; 964 } 965 966 static inline void mem_cgroup_unlock_pages(void) 967 { 968 rcu_read_unlock(); 969 } 970 971 /* idx can be of type enum memcg_stat_item or node_stat_item */ 972 static inline void mod_memcg_state(struct mem_cgroup *memcg, 973 int idx, int val) 974 { 975 unsigned long flags; 976 977 local_irq_save(flags); 978 __mod_memcg_state(memcg, idx, val); 979 local_irq_restore(flags); 980 } 981 982 static inline void mod_memcg_page_state(struct page *page, 983 int idx, int val) 984 { 985 struct mem_cgroup *memcg; 986 987 if (mem_cgroup_disabled()) 988 return; 989 990 rcu_read_lock(); 991 memcg = page_memcg(page); 992 if (memcg) 993 mod_memcg_state(memcg, idx, val); 994 rcu_read_unlock(); 995 } 996 997 unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx); 998 999 static inline unsigned long lruvec_page_state(struct lruvec *lruvec, 1000 enum node_stat_item idx) 1001 { 1002 struct mem_cgroup_per_node *pn; 1003 long x; 1004 1005 if (mem_cgroup_disabled()) 1006 return node_page_state(lruvec_pgdat(lruvec), idx); 1007 1008 pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec); 1009 x = READ_ONCE(pn->lruvec_stats.state[idx]); 1010 #ifdef CONFIG_SMP 1011 if (x < 0) 1012 x = 0; 1013 #endif 1014 return x; 1015 } 1016 1017 static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec, 1018 enum node_stat_item idx) 1019 { 1020 struct mem_cgroup_per_node *pn; 1021 long x = 0; 1022 int cpu; 1023 1024 if (mem_cgroup_disabled()) 1025 return node_page_state(lruvec_pgdat(lruvec), idx); 1026 1027 pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec); 1028 for_each_possible_cpu(cpu) 1029 x += per_cpu(pn->lruvec_stats_percpu->state[idx], cpu); 1030 #ifdef CONFIG_SMP 1031 if (x < 0) 1032 x = 0; 1033 #endif 1034 return x; 1035 } 1036 1037 void mem_cgroup_flush_stats(void); 1038 void mem_cgroup_flush_stats_ratelimited(void); 1039 1040 void __mod_memcg_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx, 1041 int val); 1042 void __mod_lruvec_kmem_state(void *p, enum node_stat_item idx, int val); 1043 1044 static inline void mod_lruvec_kmem_state(void *p, enum node_stat_item idx, 1045 int val) 1046 { 1047 unsigned long flags; 1048 1049 local_irq_save(flags); 1050 __mod_lruvec_kmem_state(p, idx, val); 1051 local_irq_restore(flags); 1052 } 1053 1054 static inline void mod_memcg_lruvec_state(struct lruvec *lruvec, 1055 enum node_stat_item idx, int val) 1056 { 1057 unsigned long flags; 1058 1059 local_irq_save(flags); 1060 __mod_memcg_lruvec_state(lruvec, idx, val); 1061 local_irq_restore(flags); 1062 } 1063 1064 void __count_memcg_events(struct mem_cgroup *memcg, enum vm_event_item idx, 1065 unsigned long count); 1066 1067 static inline void count_memcg_events(struct mem_cgroup *memcg, 1068 enum vm_event_item idx, 1069 unsigned long count) 1070 { 1071 unsigned long flags; 1072 1073 local_irq_save(flags); 1074 __count_memcg_events(memcg, idx, count); 1075 local_irq_restore(flags); 1076 } 1077 1078 static inline void count_memcg_page_event(struct page *page, 1079 enum vm_event_item idx) 1080 { 1081 struct mem_cgroup *memcg = page_memcg(page); 1082 1083 if (memcg) 1084 count_memcg_events(memcg, idx, 1); 1085 } 1086 1087 static inline void count_memcg_folio_events(struct folio *folio, 1088 enum vm_event_item idx, unsigned long nr) 1089 { 1090 struct mem_cgroup *memcg = folio_memcg(folio); 1091 1092 if (memcg) 1093 count_memcg_events(memcg, idx, nr); 1094 } 1095 1096 static inline void count_memcg_event_mm(struct mm_struct *mm, 1097 enum vm_event_item idx) 1098 { 1099 struct mem_cgroup *memcg; 1100 1101 if (mem_cgroup_disabled()) 1102 return; 1103 1104 rcu_read_lock(); 1105 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner)); 1106 if (likely(memcg)) 1107 count_memcg_events(memcg, idx, 1); 1108 rcu_read_unlock(); 1109 } 1110 1111 static inline void memcg_memory_event(struct mem_cgroup *memcg, 1112 enum memcg_memory_event event) 1113 { 1114 bool swap_event = event == MEMCG_SWAP_HIGH || event == MEMCG_SWAP_MAX || 1115 event == MEMCG_SWAP_FAIL; 1116 1117 atomic_long_inc(&memcg->memory_events_local[event]); 1118 if (!swap_event) 1119 cgroup_file_notify(&memcg->events_local_file); 1120 1121 do { 1122 atomic_long_inc(&memcg->memory_events[event]); 1123 if (swap_event) 1124 cgroup_file_notify(&memcg->swap_events_file); 1125 else 1126 cgroup_file_notify(&memcg->events_file); 1127 1128 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) 1129 break; 1130 if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_LOCAL_EVENTS) 1131 break; 1132 } while ((memcg = parent_mem_cgroup(memcg)) && 1133 !mem_cgroup_is_root(memcg)); 1134 } 1135 1136 static inline void memcg_memory_event_mm(struct mm_struct *mm, 1137 enum memcg_memory_event event) 1138 { 1139 struct mem_cgroup *memcg; 1140 1141 if (mem_cgroup_disabled()) 1142 return; 1143 1144 rcu_read_lock(); 1145 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner)); 1146 if (likely(memcg)) 1147 memcg_memory_event(memcg, event); 1148 rcu_read_unlock(); 1149 } 1150 1151 void split_page_memcg(struct page *head, unsigned int nr); 1152 1153 unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order, 1154 gfp_t gfp_mask, 1155 unsigned long *total_scanned); 1156 1157 #else /* CONFIG_MEMCG */ 1158 1159 #define MEM_CGROUP_ID_SHIFT 0 1160 1161 static inline struct mem_cgroup *folio_memcg(struct folio *folio) 1162 { 1163 return NULL; 1164 } 1165 1166 static inline struct mem_cgroup *page_memcg(struct page *page) 1167 { 1168 return NULL; 1169 } 1170 1171 static inline struct mem_cgroup *folio_memcg_rcu(struct folio *folio) 1172 { 1173 WARN_ON_ONCE(!rcu_read_lock_held()); 1174 return NULL; 1175 } 1176 1177 static inline struct mem_cgroup *folio_memcg_check(struct folio *folio) 1178 { 1179 return NULL; 1180 } 1181 1182 static inline struct mem_cgroup *page_memcg_check(struct page *page) 1183 { 1184 return NULL; 1185 } 1186 1187 static inline bool folio_memcg_kmem(struct folio *folio) 1188 { 1189 return false; 1190 } 1191 1192 static inline bool PageMemcgKmem(struct page *page) 1193 { 1194 return false; 1195 } 1196 1197 static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg) 1198 { 1199 return true; 1200 } 1201 1202 static inline bool mem_cgroup_disabled(void) 1203 { 1204 return true; 1205 } 1206 1207 static inline void memcg_memory_event(struct mem_cgroup *memcg, 1208 enum memcg_memory_event event) 1209 { 1210 } 1211 1212 static inline void memcg_memory_event_mm(struct mm_struct *mm, 1213 enum memcg_memory_event event) 1214 { 1215 } 1216 1217 static inline void mem_cgroup_protection(struct mem_cgroup *root, 1218 struct mem_cgroup *memcg, 1219 unsigned long *min, 1220 unsigned long *low) 1221 { 1222 *min = *low = 0; 1223 } 1224 1225 static inline void mem_cgroup_calculate_protection(struct mem_cgroup *root, 1226 struct mem_cgroup *memcg) 1227 { 1228 } 1229 1230 static inline bool mem_cgroup_unprotected(struct mem_cgroup *target, 1231 struct mem_cgroup *memcg) 1232 { 1233 return true; 1234 } 1235 static inline bool mem_cgroup_below_low(struct mem_cgroup *target, 1236 struct mem_cgroup *memcg) 1237 { 1238 return false; 1239 } 1240 1241 static inline bool mem_cgroup_below_min(struct mem_cgroup *target, 1242 struct mem_cgroup *memcg) 1243 { 1244 return false; 1245 } 1246 1247 static inline int mem_cgroup_charge(struct folio *folio, 1248 struct mm_struct *mm, gfp_t gfp) 1249 { 1250 return 0; 1251 } 1252 1253 static inline int mem_cgroup_swapin_charge_folio(struct folio *folio, 1254 struct mm_struct *mm, gfp_t gfp, swp_entry_t entry) 1255 { 1256 return 0; 1257 } 1258 1259 static inline void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry) 1260 { 1261 } 1262 1263 static inline void mem_cgroup_uncharge(struct folio *folio) 1264 { 1265 } 1266 1267 static inline void mem_cgroup_uncharge_list(struct list_head *page_list) 1268 { 1269 } 1270 1271 static inline void mem_cgroup_migrate(struct folio *old, struct folio *new) 1272 { 1273 } 1274 1275 static inline struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg, 1276 struct pglist_data *pgdat) 1277 { 1278 return &pgdat->__lruvec; 1279 } 1280 1281 static inline struct lruvec *folio_lruvec(struct folio *folio) 1282 { 1283 struct pglist_data *pgdat = folio_pgdat(folio); 1284 return &pgdat->__lruvec; 1285 } 1286 1287 static inline 1288 void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio) 1289 { 1290 } 1291 1292 static inline struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg) 1293 { 1294 return NULL; 1295 } 1296 1297 static inline bool mm_match_cgroup(struct mm_struct *mm, 1298 struct mem_cgroup *memcg) 1299 { 1300 return true; 1301 } 1302 1303 static inline struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm) 1304 { 1305 return NULL; 1306 } 1307 1308 static inline 1309 struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css) 1310 { 1311 return NULL; 1312 } 1313 1314 static inline void obj_cgroup_put(struct obj_cgroup *objcg) 1315 { 1316 } 1317 1318 static inline bool mem_cgroup_tryget(struct mem_cgroup *memcg) 1319 { 1320 return true; 1321 } 1322 1323 static inline void mem_cgroup_put(struct mem_cgroup *memcg) 1324 { 1325 } 1326 1327 static inline struct lruvec *folio_lruvec_lock(struct folio *folio) 1328 { 1329 struct pglist_data *pgdat = folio_pgdat(folio); 1330 1331 spin_lock(&pgdat->__lruvec.lru_lock); 1332 return &pgdat->__lruvec; 1333 } 1334 1335 static inline struct lruvec *folio_lruvec_lock_irq(struct folio *folio) 1336 { 1337 struct pglist_data *pgdat = folio_pgdat(folio); 1338 1339 spin_lock_irq(&pgdat->__lruvec.lru_lock); 1340 return &pgdat->__lruvec; 1341 } 1342 1343 static inline struct lruvec *folio_lruvec_lock_irqsave(struct folio *folio, 1344 unsigned long *flagsp) 1345 { 1346 struct pglist_data *pgdat = folio_pgdat(folio); 1347 1348 spin_lock_irqsave(&pgdat->__lruvec.lru_lock, *flagsp); 1349 return &pgdat->__lruvec; 1350 } 1351 1352 static inline struct mem_cgroup * 1353 mem_cgroup_iter(struct mem_cgroup *root, 1354 struct mem_cgroup *prev, 1355 struct mem_cgroup_reclaim_cookie *reclaim) 1356 { 1357 return NULL; 1358 } 1359 1360 static inline void mem_cgroup_iter_break(struct mem_cgroup *root, 1361 struct mem_cgroup *prev) 1362 { 1363 } 1364 1365 static inline void mem_cgroup_scan_tasks(struct mem_cgroup *memcg, 1366 int (*fn)(struct task_struct *, void *), void *arg) 1367 { 1368 } 1369 1370 static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg) 1371 { 1372 return 0; 1373 } 1374 1375 static inline struct mem_cgroup *mem_cgroup_from_id(unsigned short id) 1376 { 1377 WARN_ON_ONCE(id); 1378 /* XXX: This should always return root_mem_cgroup */ 1379 return NULL; 1380 } 1381 1382 #ifdef CONFIG_SHRINKER_DEBUG 1383 static inline unsigned long mem_cgroup_ino(struct mem_cgroup *memcg) 1384 { 1385 return 0; 1386 } 1387 1388 static inline struct mem_cgroup *mem_cgroup_get_from_ino(unsigned long ino) 1389 { 1390 return NULL; 1391 } 1392 #endif 1393 1394 static inline struct mem_cgroup *mem_cgroup_from_seq(struct seq_file *m) 1395 { 1396 return NULL; 1397 } 1398 1399 static inline struct mem_cgroup *lruvec_memcg(struct lruvec *lruvec) 1400 { 1401 return NULL; 1402 } 1403 1404 static inline bool mem_cgroup_online(struct mem_cgroup *memcg) 1405 { 1406 return true; 1407 } 1408 1409 static inline 1410 unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec, 1411 enum lru_list lru, int zone_idx) 1412 { 1413 return 0; 1414 } 1415 1416 static inline unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg) 1417 { 1418 return 0; 1419 } 1420 1421 static inline unsigned long mem_cgroup_size(struct mem_cgroup *memcg) 1422 { 1423 return 0; 1424 } 1425 1426 static inline void 1427 mem_cgroup_print_oom_context(struct mem_cgroup *memcg, struct task_struct *p) 1428 { 1429 } 1430 1431 static inline void 1432 mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg) 1433 { 1434 } 1435 1436 static inline void folio_memcg_lock(struct folio *folio) 1437 { 1438 } 1439 1440 static inline void folio_memcg_unlock(struct folio *folio) 1441 { 1442 } 1443 1444 static inline bool mem_cgroup_trylock_pages(struct mem_cgroup *memcg) 1445 { 1446 /* to match folio_memcg_rcu() */ 1447 rcu_read_lock(); 1448 return true; 1449 } 1450 1451 static inline void mem_cgroup_unlock_pages(void) 1452 { 1453 rcu_read_unlock(); 1454 } 1455 1456 static inline void mem_cgroup_handle_over_high(void) 1457 { 1458 } 1459 1460 static inline void mem_cgroup_enter_user_fault(void) 1461 { 1462 } 1463 1464 static inline void mem_cgroup_exit_user_fault(void) 1465 { 1466 } 1467 1468 static inline bool task_in_memcg_oom(struct task_struct *p) 1469 { 1470 return false; 1471 } 1472 1473 static inline bool mem_cgroup_oom_synchronize(bool wait) 1474 { 1475 return false; 1476 } 1477 1478 static inline struct mem_cgroup *mem_cgroup_get_oom_group( 1479 struct task_struct *victim, struct mem_cgroup *oom_domain) 1480 { 1481 return NULL; 1482 } 1483 1484 static inline void mem_cgroup_print_oom_group(struct mem_cgroup *memcg) 1485 { 1486 } 1487 1488 static inline void __mod_memcg_state(struct mem_cgroup *memcg, 1489 int idx, 1490 int nr) 1491 { 1492 } 1493 1494 static inline void mod_memcg_state(struct mem_cgroup *memcg, 1495 int idx, 1496 int nr) 1497 { 1498 } 1499 1500 static inline void mod_memcg_page_state(struct page *page, 1501 int idx, int val) 1502 { 1503 } 1504 1505 static inline unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx) 1506 { 1507 return 0; 1508 } 1509 1510 static inline unsigned long lruvec_page_state(struct lruvec *lruvec, 1511 enum node_stat_item idx) 1512 { 1513 return node_page_state(lruvec_pgdat(lruvec), idx); 1514 } 1515 1516 static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec, 1517 enum node_stat_item idx) 1518 { 1519 return node_page_state(lruvec_pgdat(lruvec), idx); 1520 } 1521 1522 static inline void mem_cgroup_flush_stats(void) 1523 { 1524 } 1525 1526 static inline void mem_cgroup_flush_stats_ratelimited(void) 1527 { 1528 } 1529 1530 static inline void __mod_memcg_lruvec_state(struct lruvec *lruvec, 1531 enum node_stat_item idx, int val) 1532 { 1533 } 1534 1535 static inline void __mod_lruvec_kmem_state(void *p, enum node_stat_item idx, 1536 int val) 1537 { 1538 struct page *page = virt_to_head_page(p); 1539 1540 __mod_node_page_state(page_pgdat(page), idx, val); 1541 } 1542 1543 static inline void mod_lruvec_kmem_state(void *p, enum node_stat_item idx, 1544 int val) 1545 { 1546 struct page *page = virt_to_head_page(p); 1547 1548 mod_node_page_state(page_pgdat(page), idx, val); 1549 } 1550 1551 static inline void count_memcg_events(struct mem_cgroup *memcg, 1552 enum vm_event_item idx, 1553 unsigned long count) 1554 { 1555 } 1556 1557 static inline void __count_memcg_events(struct mem_cgroup *memcg, 1558 enum vm_event_item idx, 1559 unsigned long count) 1560 { 1561 } 1562 1563 static inline void count_memcg_page_event(struct page *page, 1564 int idx) 1565 { 1566 } 1567 1568 static inline void count_memcg_folio_events(struct folio *folio, 1569 enum vm_event_item idx, unsigned long nr) 1570 { 1571 } 1572 1573 static inline 1574 void count_memcg_event_mm(struct mm_struct *mm, enum vm_event_item idx) 1575 { 1576 } 1577 1578 static inline void split_page_memcg(struct page *head, unsigned int nr) 1579 { 1580 } 1581 1582 static inline 1583 unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order, 1584 gfp_t gfp_mask, 1585 unsigned long *total_scanned) 1586 { 1587 return 0; 1588 } 1589 #endif /* CONFIG_MEMCG */ 1590 1591 static inline void __inc_lruvec_kmem_state(void *p, enum node_stat_item idx) 1592 { 1593 __mod_lruvec_kmem_state(p, idx, 1); 1594 } 1595 1596 static inline void __dec_lruvec_kmem_state(void *p, enum node_stat_item idx) 1597 { 1598 __mod_lruvec_kmem_state(p, idx, -1); 1599 } 1600 1601 static inline struct lruvec *parent_lruvec(struct lruvec *lruvec) 1602 { 1603 struct mem_cgroup *memcg; 1604 1605 memcg = lruvec_memcg(lruvec); 1606 if (!memcg) 1607 return NULL; 1608 memcg = parent_mem_cgroup(memcg); 1609 if (!memcg) 1610 return NULL; 1611 return mem_cgroup_lruvec(memcg, lruvec_pgdat(lruvec)); 1612 } 1613 1614 static inline void unlock_page_lruvec(struct lruvec *lruvec) 1615 { 1616 spin_unlock(&lruvec->lru_lock); 1617 } 1618 1619 static inline void unlock_page_lruvec_irq(struct lruvec *lruvec) 1620 { 1621 spin_unlock_irq(&lruvec->lru_lock); 1622 } 1623 1624 static inline void unlock_page_lruvec_irqrestore(struct lruvec *lruvec, 1625 unsigned long flags) 1626 { 1627 spin_unlock_irqrestore(&lruvec->lru_lock, flags); 1628 } 1629 1630 /* Test requires a stable page->memcg binding, see page_memcg() */ 1631 static inline bool folio_matches_lruvec(struct folio *folio, 1632 struct lruvec *lruvec) 1633 { 1634 return lruvec_pgdat(lruvec) == folio_pgdat(folio) && 1635 lruvec_memcg(lruvec) == folio_memcg(folio); 1636 } 1637 1638 /* Don't lock again iff page's lruvec locked */ 1639 static inline struct lruvec *folio_lruvec_relock_irq(struct folio *folio, 1640 struct lruvec *locked_lruvec) 1641 { 1642 if (locked_lruvec) { 1643 if (folio_matches_lruvec(folio, locked_lruvec)) 1644 return locked_lruvec; 1645 1646 unlock_page_lruvec_irq(locked_lruvec); 1647 } 1648 1649 return folio_lruvec_lock_irq(folio); 1650 } 1651 1652 /* Don't lock again iff page's lruvec locked */ 1653 static inline struct lruvec *folio_lruvec_relock_irqsave(struct folio *folio, 1654 struct lruvec *locked_lruvec, unsigned long *flags) 1655 { 1656 if (locked_lruvec) { 1657 if (folio_matches_lruvec(folio, locked_lruvec)) 1658 return locked_lruvec; 1659 1660 unlock_page_lruvec_irqrestore(locked_lruvec, *flags); 1661 } 1662 1663 return folio_lruvec_lock_irqsave(folio, flags); 1664 } 1665 1666 #ifdef CONFIG_CGROUP_WRITEBACK 1667 1668 struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb); 1669 void mem_cgroup_wb_stats(struct bdi_writeback *wb, unsigned long *pfilepages, 1670 unsigned long *pheadroom, unsigned long *pdirty, 1671 unsigned long *pwriteback); 1672 1673 void mem_cgroup_track_foreign_dirty_slowpath(struct folio *folio, 1674 struct bdi_writeback *wb); 1675 1676 static inline void mem_cgroup_track_foreign_dirty(struct folio *folio, 1677 struct bdi_writeback *wb) 1678 { 1679 struct mem_cgroup *memcg; 1680 1681 if (mem_cgroup_disabled()) 1682 return; 1683 1684 memcg = folio_memcg(folio); 1685 if (unlikely(memcg && &memcg->css != wb->memcg_css)) 1686 mem_cgroup_track_foreign_dirty_slowpath(folio, wb); 1687 } 1688 1689 void mem_cgroup_flush_foreign(struct bdi_writeback *wb); 1690 1691 #else /* CONFIG_CGROUP_WRITEBACK */ 1692 1693 static inline struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb) 1694 { 1695 return NULL; 1696 } 1697 1698 static inline void mem_cgroup_wb_stats(struct bdi_writeback *wb, 1699 unsigned long *pfilepages, 1700 unsigned long *pheadroom, 1701 unsigned long *pdirty, 1702 unsigned long *pwriteback) 1703 { 1704 } 1705 1706 static inline void mem_cgroup_track_foreign_dirty(struct folio *folio, 1707 struct bdi_writeback *wb) 1708 { 1709 } 1710 1711 static inline void mem_cgroup_flush_foreign(struct bdi_writeback *wb) 1712 { 1713 } 1714 1715 #endif /* CONFIG_CGROUP_WRITEBACK */ 1716 1717 struct sock; 1718 bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages, 1719 gfp_t gfp_mask); 1720 void mem_cgroup_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages); 1721 #ifdef CONFIG_MEMCG 1722 extern struct static_key_false memcg_sockets_enabled_key; 1723 #define mem_cgroup_sockets_enabled static_branch_unlikely(&memcg_sockets_enabled_key) 1724 void mem_cgroup_sk_alloc(struct sock *sk); 1725 void mem_cgroup_sk_free(struct sock *sk); 1726 static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg) 1727 { 1728 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && memcg->tcpmem_pressure) 1729 return true; 1730 do { 1731 if (time_before(jiffies, READ_ONCE(memcg->socket_pressure))) 1732 return true; 1733 } while ((memcg = parent_mem_cgroup(memcg))); 1734 return false; 1735 } 1736 1737 int alloc_shrinker_info(struct mem_cgroup *memcg); 1738 void free_shrinker_info(struct mem_cgroup *memcg); 1739 void set_shrinker_bit(struct mem_cgroup *memcg, int nid, int shrinker_id); 1740 void reparent_shrinker_deferred(struct mem_cgroup *memcg); 1741 #else 1742 #define mem_cgroup_sockets_enabled 0 1743 static inline void mem_cgroup_sk_alloc(struct sock *sk) { }; 1744 static inline void mem_cgroup_sk_free(struct sock *sk) { }; 1745 static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg) 1746 { 1747 return false; 1748 } 1749 1750 static inline void set_shrinker_bit(struct mem_cgroup *memcg, 1751 int nid, int shrinker_id) 1752 { 1753 } 1754 #endif 1755 1756 #ifdef CONFIG_MEMCG_KMEM 1757 bool mem_cgroup_kmem_disabled(void); 1758 int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, int order); 1759 void __memcg_kmem_uncharge_page(struct page *page, int order); 1760 1761 struct obj_cgroup *get_obj_cgroup_from_current(void); 1762 struct obj_cgroup *get_obj_cgroup_from_page(struct page *page); 1763 1764 int obj_cgroup_charge(struct obj_cgroup *objcg, gfp_t gfp, size_t size); 1765 void obj_cgroup_uncharge(struct obj_cgroup *objcg, size_t size); 1766 1767 extern struct static_key_false memcg_bpf_enabled_key; 1768 static inline bool memcg_bpf_enabled(void) 1769 { 1770 return static_branch_likely(&memcg_bpf_enabled_key); 1771 } 1772 1773 extern struct static_key_false memcg_kmem_online_key; 1774 1775 static inline bool memcg_kmem_online(void) 1776 { 1777 return static_branch_likely(&memcg_kmem_online_key); 1778 } 1779 1780 static inline int memcg_kmem_charge_page(struct page *page, gfp_t gfp, 1781 int order) 1782 { 1783 if (memcg_kmem_online()) 1784 return __memcg_kmem_charge_page(page, gfp, order); 1785 return 0; 1786 } 1787 1788 static inline void memcg_kmem_uncharge_page(struct page *page, int order) 1789 { 1790 if (memcg_kmem_online()) 1791 __memcg_kmem_uncharge_page(page, order); 1792 } 1793 1794 /* 1795 * A helper for accessing memcg's kmem_id, used for getting 1796 * corresponding LRU lists. 1797 */ 1798 static inline int memcg_kmem_id(struct mem_cgroup *memcg) 1799 { 1800 return memcg ? memcg->kmemcg_id : -1; 1801 } 1802 1803 struct mem_cgroup *mem_cgroup_from_obj(void *p); 1804 struct mem_cgroup *mem_cgroup_from_slab_obj(void *p); 1805 1806 static inline void count_objcg_event(struct obj_cgroup *objcg, 1807 enum vm_event_item idx) 1808 { 1809 struct mem_cgroup *memcg; 1810 1811 if (!memcg_kmem_online()) 1812 return; 1813 1814 rcu_read_lock(); 1815 memcg = obj_cgroup_memcg(objcg); 1816 count_memcg_events(memcg, idx, 1); 1817 rcu_read_unlock(); 1818 } 1819 1820 #else 1821 static inline bool mem_cgroup_kmem_disabled(void) 1822 { 1823 return true; 1824 } 1825 1826 static inline int memcg_kmem_charge_page(struct page *page, gfp_t gfp, 1827 int order) 1828 { 1829 return 0; 1830 } 1831 1832 static inline void memcg_kmem_uncharge_page(struct page *page, int order) 1833 { 1834 } 1835 1836 static inline int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, 1837 int order) 1838 { 1839 return 0; 1840 } 1841 1842 static inline void __memcg_kmem_uncharge_page(struct page *page, int order) 1843 { 1844 } 1845 1846 static inline struct obj_cgroup *get_obj_cgroup_from_page(struct page *page) 1847 { 1848 return NULL; 1849 } 1850 1851 static inline bool memcg_bpf_enabled(void) 1852 { 1853 return false; 1854 } 1855 1856 static inline bool memcg_kmem_online(void) 1857 { 1858 return false; 1859 } 1860 1861 static inline int memcg_kmem_id(struct mem_cgroup *memcg) 1862 { 1863 return -1; 1864 } 1865 1866 static inline struct mem_cgroup *mem_cgroup_from_obj(void *p) 1867 { 1868 return NULL; 1869 } 1870 1871 static inline struct mem_cgroup *mem_cgroup_from_slab_obj(void *p) 1872 { 1873 return NULL; 1874 } 1875 1876 static inline void count_objcg_event(struct obj_cgroup *objcg, 1877 enum vm_event_item idx) 1878 { 1879 } 1880 1881 #endif /* CONFIG_MEMCG_KMEM */ 1882 1883 #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP) 1884 bool obj_cgroup_may_zswap(struct obj_cgroup *objcg); 1885 void obj_cgroup_charge_zswap(struct obj_cgroup *objcg, size_t size); 1886 void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg, size_t size); 1887 #else 1888 static inline bool obj_cgroup_may_zswap(struct obj_cgroup *objcg) 1889 { 1890 return true; 1891 } 1892 static inline void obj_cgroup_charge_zswap(struct obj_cgroup *objcg, 1893 size_t size) 1894 { 1895 } 1896 static inline void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg, 1897 size_t size) 1898 { 1899 } 1900 #endif 1901 1902 #endif /* _LINUX_MEMCONTROL_H */ 1903