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