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 slabobj_ext vector */ 353 MEMCG_DATA_OBJEXTS = (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 __FIRST_OBJEXT_FLAG __NR_MEMCG_DATA_FLAGS 361 362 #else /* CONFIG_MEMCG */ 363 364 #define __FIRST_OBJEXT_FLAG (1UL << 0) 365 366 #endif /* CONFIG_MEMCG */ 367 368 enum objext_flags { 369 /* slabobj_ext vector failed to allocate */ 370 OBJEXTS_ALLOC_FAIL = __FIRST_OBJEXT_FLAG, 371 /* the next bit after the last actual flag */ 372 __NR_OBJEXTS_FLAGS = (__FIRST_OBJEXT_FLAG << 1), 373 }; 374 375 #define OBJEXTS_FLAGS_MASK (__NR_OBJEXTS_FLAGS - 1) 376 377 #ifdef CONFIG_MEMCG 378 379 static inline bool folio_memcg_kmem(struct folio *folio); 380 381 /* 382 * After the initialization objcg->memcg is always pointing at 383 * a valid memcg, but can be atomically swapped to the parent memcg. 384 * 385 * The caller must ensure that the returned memcg won't be released: 386 * e.g. acquire the rcu_read_lock or css_set_lock. 387 */ 388 static inline struct mem_cgroup *obj_cgroup_memcg(struct obj_cgroup *objcg) 389 { 390 return READ_ONCE(objcg->memcg); 391 } 392 393 /* 394 * __folio_memcg - Get the memory cgroup associated with a non-kmem folio 395 * @folio: Pointer to the folio. 396 * 397 * Returns a pointer to the memory cgroup associated with the folio, 398 * or NULL. This function assumes that the folio is known to have a 399 * proper memory cgroup pointer. It's not safe to call this function 400 * against some type of folios, e.g. slab folios or ex-slab folios or 401 * kmem folios. 402 */ 403 static inline struct mem_cgroup *__folio_memcg(struct folio *folio) 404 { 405 unsigned long memcg_data = folio->memcg_data; 406 407 VM_BUG_ON_FOLIO(folio_test_slab(folio), folio); 408 VM_BUG_ON_FOLIO(memcg_data & MEMCG_DATA_OBJEXTS, folio); 409 VM_BUG_ON_FOLIO(memcg_data & MEMCG_DATA_KMEM, folio); 410 411 return (struct mem_cgroup *)(memcg_data & ~OBJEXTS_FLAGS_MASK); 412 } 413 414 /* 415 * __folio_objcg - get the object cgroup associated with a kmem folio. 416 * @folio: Pointer to the folio. 417 * 418 * Returns a pointer to the object cgroup associated with the folio, 419 * or NULL. This function assumes that the folio is known to have a 420 * proper object cgroup pointer. It's not safe to call this function 421 * against some type of folios, e.g. slab folios or ex-slab folios or 422 * LRU folios. 423 */ 424 static inline struct obj_cgroup *__folio_objcg(struct folio *folio) 425 { 426 unsigned long memcg_data = folio->memcg_data; 427 428 VM_BUG_ON_FOLIO(folio_test_slab(folio), folio); 429 VM_BUG_ON_FOLIO(memcg_data & MEMCG_DATA_OBJEXTS, folio); 430 VM_BUG_ON_FOLIO(!(memcg_data & MEMCG_DATA_KMEM), folio); 431 432 return (struct obj_cgroup *)(memcg_data & ~OBJEXTS_FLAGS_MASK); 433 } 434 435 /* 436 * folio_memcg - Get the memory cgroup associated with a folio. 437 * @folio: Pointer to the folio. 438 * 439 * Returns a pointer to the memory cgroup associated with the folio, 440 * or NULL. This function assumes that the folio is known to have a 441 * proper memory cgroup pointer. It's not safe to call this function 442 * against some type of folios, e.g. slab folios or ex-slab folios. 443 * 444 * For a non-kmem folio any of the following ensures folio and memcg binding 445 * stability: 446 * 447 * - the folio lock 448 * - LRU isolation 449 * - folio_memcg_lock() 450 * - exclusive reference 451 * - mem_cgroup_trylock_pages() 452 * 453 * For a kmem folio a caller should hold an rcu read lock to protect memcg 454 * associated with a kmem folio from being released. 455 */ 456 static inline struct mem_cgroup *folio_memcg(struct folio *folio) 457 { 458 if (folio_memcg_kmem(folio)) 459 return obj_cgroup_memcg(__folio_objcg(folio)); 460 return __folio_memcg(folio); 461 } 462 463 static inline struct mem_cgroup *page_memcg(struct page *page) 464 { 465 return folio_memcg(page_folio(page)); 466 } 467 468 /** 469 * folio_memcg_rcu - Locklessly get the memory cgroup associated with a folio. 470 * @folio: Pointer to the folio. 471 * 472 * This function assumes that the folio is known to have a 473 * proper memory cgroup pointer. It's not safe to call this function 474 * against some type of folios, e.g. slab folios or ex-slab folios. 475 * 476 * Return: A pointer to the memory cgroup associated with the folio, 477 * or NULL. 478 */ 479 static inline struct mem_cgroup *folio_memcg_rcu(struct folio *folio) 480 { 481 unsigned long memcg_data = READ_ONCE(folio->memcg_data); 482 483 VM_BUG_ON_FOLIO(folio_test_slab(folio), folio); 484 WARN_ON_ONCE(!rcu_read_lock_held()); 485 486 if (memcg_data & MEMCG_DATA_KMEM) { 487 struct obj_cgroup *objcg; 488 489 objcg = (void *)(memcg_data & ~OBJEXTS_FLAGS_MASK); 490 return obj_cgroup_memcg(objcg); 491 } 492 493 return (struct mem_cgroup *)(memcg_data & ~OBJEXTS_FLAGS_MASK); 494 } 495 496 /* 497 * folio_memcg_check - Get the memory cgroup associated with a folio. 498 * @folio: Pointer to the folio. 499 * 500 * Returns a pointer to the memory cgroup associated with the folio, 501 * or NULL. This function unlike folio_memcg() can take any folio 502 * as an argument. It has to be used in cases when it's not known if a folio 503 * has an associated memory cgroup pointer or an object cgroups vector or 504 * an object cgroup. 505 * 506 * For a non-kmem folio any of the following ensures folio and memcg binding 507 * stability: 508 * 509 * - the folio lock 510 * - LRU isolation 511 * - lock_folio_memcg() 512 * - exclusive reference 513 * - mem_cgroup_trylock_pages() 514 * 515 * For a kmem folio a caller should hold an rcu read lock to protect memcg 516 * associated with a kmem folio from being released. 517 */ 518 static inline struct mem_cgroup *folio_memcg_check(struct folio *folio) 519 { 520 /* 521 * Because folio->memcg_data might be changed asynchronously 522 * for slabs, READ_ONCE() should be used here. 523 */ 524 unsigned long memcg_data = READ_ONCE(folio->memcg_data); 525 526 if (memcg_data & MEMCG_DATA_OBJEXTS) 527 return NULL; 528 529 if (memcg_data & MEMCG_DATA_KMEM) { 530 struct obj_cgroup *objcg; 531 532 objcg = (void *)(memcg_data & ~OBJEXTS_FLAGS_MASK); 533 return obj_cgroup_memcg(objcg); 534 } 535 536 return (struct mem_cgroup *)(memcg_data & ~OBJEXTS_FLAGS_MASK); 537 } 538 539 static inline struct mem_cgroup *page_memcg_check(struct page *page) 540 { 541 if (PageTail(page)) 542 return NULL; 543 return folio_memcg_check((struct folio *)page); 544 } 545 546 static inline struct mem_cgroup *get_mem_cgroup_from_objcg(struct obj_cgroup *objcg) 547 { 548 struct mem_cgroup *memcg; 549 550 rcu_read_lock(); 551 retry: 552 memcg = obj_cgroup_memcg(objcg); 553 if (unlikely(!css_tryget(&memcg->css))) 554 goto retry; 555 rcu_read_unlock(); 556 557 return memcg; 558 } 559 560 #ifdef CONFIG_MEMCG_KMEM 561 /* 562 * folio_memcg_kmem - Check if the folio has the memcg_kmem flag set. 563 * @folio: Pointer to the folio. 564 * 565 * Checks if the folio has MemcgKmem flag set. The caller must ensure 566 * that the folio has an associated memory cgroup. It's not safe to call 567 * this function against some types of folios, e.g. slab folios. 568 */ 569 static inline bool folio_memcg_kmem(struct folio *folio) 570 { 571 VM_BUG_ON_PGFLAGS(PageTail(&folio->page), &folio->page); 572 VM_BUG_ON_FOLIO(folio->memcg_data & MEMCG_DATA_OBJEXTS, folio); 573 return folio->memcg_data & MEMCG_DATA_KMEM; 574 } 575 576 577 #else 578 static inline bool folio_memcg_kmem(struct folio *folio) 579 { 580 return false; 581 } 582 583 #endif 584 585 static inline bool PageMemcgKmem(struct page *page) 586 { 587 return folio_memcg_kmem(page_folio(page)); 588 } 589 590 static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg) 591 { 592 return (memcg == root_mem_cgroup); 593 } 594 595 static inline bool mem_cgroup_disabled(void) 596 { 597 return !cgroup_subsys_enabled(memory_cgrp_subsys); 598 } 599 600 static inline void mem_cgroup_protection(struct mem_cgroup *root, 601 struct mem_cgroup *memcg, 602 unsigned long *min, 603 unsigned long *low) 604 { 605 *min = *low = 0; 606 607 if (mem_cgroup_disabled()) 608 return; 609 610 /* 611 * There is no reclaim protection applied to a targeted reclaim. 612 * We are special casing this specific case here because 613 * mem_cgroup_calculate_protection is not robust enough to keep 614 * the protection invariant for calculated effective values for 615 * parallel reclaimers with different reclaim target. This is 616 * especially a problem for tail memcgs (as they have pages on LRU) 617 * which would want to have effective values 0 for targeted reclaim 618 * but a different value for external reclaim. 619 * 620 * Example 621 * Let's have global and A's reclaim in parallel: 622 * | 623 * A (low=2G, usage = 3G, max = 3G, children_low_usage = 1.5G) 624 * |\ 625 * | C (low = 1G, usage = 2.5G) 626 * B (low = 1G, usage = 0.5G) 627 * 628 * For the global reclaim 629 * A.elow = A.low 630 * B.elow = min(B.usage, B.low) because children_low_usage <= A.elow 631 * C.elow = min(C.usage, C.low) 632 * 633 * With the effective values resetting we have A reclaim 634 * A.elow = 0 635 * B.elow = B.low 636 * C.elow = C.low 637 * 638 * If the global reclaim races with A's reclaim then 639 * B.elow = C.elow = 0 because children_low_usage > A.elow) 640 * is possible and reclaiming B would be violating the protection. 641 * 642 */ 643 if (root == memcg) 644 return; 645 646 *min = READ_ONCE(memcg->memory.emin); 647 *low = READ_ONCE(memcg->memory.elow); 648 } 649 650 void mem_cgroup_calculate_protection(struct mem_cgroup *root, 651 struct mem_cgroup *memcg); 652 653 static inline bool mem_cgroup_unprotected(struct mem_cgroup *target, 654 struct mem_cgroup *memcg) 655 { 656 /* 657 * The root memcg doesn't account charges, and doesn't support 658 * protection. The target memcg's protection is ignored, see 659 * mem_cgroup_calculate_protection() and mem_cgroup_protection() 660 */ 661 return mem_cgroup_disabled() || mem_cgroup_is_root(memcg) || 662 memcg == target; 663 } 664 665 static inline bool mem_cgroup_below_low(struct mem_cgroup *target, 666 struct mem_cgroup *memcg) 667 { 668 if (mem_cgroup_unprotected(target, memcg)) 669 return false; 670 671 return READ_ONCE(memcg->memory.elow) >= 672 page_counter_read(&memcg->memory); 673 } 674 675 static inline bool mem_cgroup_below_min(struct mem_cgroup *target, 676 struct mem_cgroup *memcg) 677 { 678 if (mem_cgroup_unprotected(target, memcg)) 679 return false; 680 681 return READ_ONCE(memcg->memory.emin) >= 682 page_counter_read(&memcg->memory); 683 } 684 685 void mem_cgroup_commit_charge(struct folio *folio, struct mem_cgroup *memcg); 686 687 int __mem_cgroup_charge(struct folio *folio, struct mm_struct *mm, gfp_t gfp); 688 689 /** 690 * mem_cgroup_charge - Charge a newly allocated folio to a cgroup. 691 * @folio: Folio to charge. 692 * @mm: mm context of the allocating task. 693 * @gfp: Reclaim mode. 694 * 695 * Try to charge @folio to the memcg that @mm belongs to, reclaiming 696 * pages according to @gfp if necessary. If @mm is NULL, try to 697 * charge to the active memcg. 698 * 699 * Do not use this for folios allocated for swapin. 700 * 701 * Return: 0 on success. Otherwise, an error code is returned. 702 */ 703 static inline int mem_cgroup_charge(struct folio *folio, struct mm_struct *mm, 704 gfp_t gfp) 705 { 706 if (mem_cgroup_disabled()) 707 return 0; 708 return __mem_cgroup_charge(folio, mm, gfp); 709 } 710 711 int mem_cgroup_hugetlb_try_charge(struct mem_cgroup *memcg, gfp_t gfp, 712 long nr_pages); 713 714 int mem_cgroup_swapin_charge_folio(struct folio *folio, struct mm_struct *mm, 715 gfp_t gfp, swp_entry_t entry); 716 void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry); 717 718 void __mem_cgroup_uncharge(struct folio *folio); 719 720 /** 721 * mem_cgroup_uncharge - Uncharge a folio. 722 * @folio: Folio to uncharge. 723 * 724 * Uncharge a folio previously charged with mem_cgroup_charge(). 725 */ 726 static inline void mem_cgroup_uncharge(struct folio *folio) 727 { 728 if (mem_cgroup_disabled()) 729 return; 730 __mem_cgroup_uncharge(folio); 731 } 732 733 void __mem_cgroup_uncharge_folios(struct folio_batch *folios); 734 static inline void mem_cgroup_uncharge_folios(struct folio_batch *folios) 735 { 736 if (mem_cgroup_disabled()) 737 return; 738 __mem_cgroup_uncharge_folios(folios); 739 } 740 741 void mem_cgroup_cancel_charge(struct mem_cgroup *memcg, unsigned int nr_pages); 742 void mem_cgroup_replace_folio(struct folio *old, struct folio *new); 743 void mem_cgroup_migrate(struct folio *old, struct folio *new); 744 745 /** 746 * mem_cgroup_lruvec - get the lru list vector for a memcg & node 747 * @memcg: memcg of the wanted lruvec 748 * @pgdat: pglist_data 749 * 750 * Returns the lru list vector holding pages for a given @memcg & 751 * @pgdat combination. This can be the node lruvec, if the memory 752 * controller is disabled. 753 */ 754 static inline struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg, 755 struct pglist_data *pgdat) 756 { 757 struct mem_cgroup_per_node *mz; 758 struct lruvec *lruvec; 759 760 if (mem_cgroup_disabled()) { 761 lruvec = &pgdat->__lruvec; 762 goto out; 763 } 764 765 if (!memcg) 766 memcg = root_mem_cgroup; 767 768 mz = memcg->nodeinfo[pgdat->node_id]; 769 lruvec = &mz->lruvec; 770 out: 771 /* 772 * Since a node can be onlined after the mem_cgroup was created, 773 * we have to be prepared to initialize lruvec->pgdat here; 774 * and if offlined then reonlined, we need to reinitialize it. 775 */ 776 if (unlikely(lruvec->pgdat != pgdat)) 777 lruvec->pgdat = pgdat; 778 return lruvec; 779 } 780 781 /** 782 * folio_lruvec - return lruvec for isolating/putting an LRU folio 783 * @folio: Pointer to the folio. 784 * 785 * This function relies on folio->mem_cgroup being stable. 786 */ 787 static inline struct lruvec *folio_lruvec(struct folio *folio) 788 { 789 struct mem_cgroup *memcg = folio_memcg(folio); 790 791 VM_WARN_ON_ONCE_FOLIO(!memcg && !mem_cgroup_disabled(), folio); 792 return mem_cgroup_lruvec(memcg, folio_pgdat(folio)); 793 } 794 795 struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p); 796 797 struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm); 798 799 struct mem_cgroup *get_mem_cgroup_from_current(void); 800 801 struct lruvec *folio_lruvec_lock(struct folio *folio); 802 struct lruvec *folio_lruvec_lock_irq(struct folio *folio); 803 struct lruvec *folio_lruvec_lock_irqsave(struct folio *folio, 804 unsigned long *flags); 805 806 #ifdef CONFIG_DEBUG_VM 807 void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio); 808 #else 809 static inline 810 void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio) 811 { 812 } 813 #endif 814 815 static inline 816 struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css){ 817 return css ? container_of(css, struct mem_cgroup, css) : NULL; 818 } 819 820 static inline bool obj_cgroup_tryget(struct obj_cgroup *objcg) 821 { 822 return percpu_ref_tryget(&objcg->refcnt); 823 } 824 825 static inline void obj_cgroup_get(struct obj_cgroup *objcg) 826 { 827 percpu_ref_get(&objcg->refcnt); 828 } 829 830 static inline void obj_cgroup_get_many(struct obj_cgroup *objcg, 831 unsigned long nr) 832 { 833 percpu_ref_get_many(&objcg->refcnt, nr); 834 } 835 836 static inline void obj_cgroup_put(struct obj_cgroup *objcg) 837 { 838 if (objcg) 839 percpu_ref_put(&objcg->refcnt); 840 } 841 842 static inline bool mem_cgroup_tryget(struct mem_cgroup *memcg) 843 { 844 return !memcg || css_tryget(&memcg->css); 845 } 846 847 static inline bool mem_cgroup_tryget_online(struct mem_cgroup *memcg) 848 { 849 return !memcg || css_tryget_online(&memcg->css); 850 } 851 852 static inline void mem_cgroup_put(struct mem_cgroup *memcg) 853 { 854 if (memcg) 855 css_put(&memcg->css); 856 } 857 858 #define mem_cgroup_from_counter(counter, member) \ 859 container_of(counter, struct mem_cgroup, member) 860 861 struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *, 862 struct mem_cgroup *, 863 struct mem_cgroup_reclaim_cookie *); 864 void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *); 865 void mem_cgroup_scan_tasks(struct mem_cgroup *memcg, 866 int (*)(struct task_struct *, void *), void *arg); 867 868 static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg) 869 { 870 if (mem_cgroup_disabled()) 871 return 0; 872 873 return memcg->id.id; 874 } 875 struct mem_cgroup *mem_cgroup_from_id(unsigned short id); 876 877 #ifdef CONFIG_SHRINKER_DEBUG 878 static inline unsigned long mem_cgroup_ino(struct mem_cgroup *memcg) 879 { 880 return memcg ? cgroup_ino(memcg->css.cgroup) : 0; 881 } 882 883 struct mem_cgroup *mem_cgroup_get_from_ino(unsigned long ino); 884 #endif 885 886 static inline struct mem_cgroup *mem_cgroup_from_seq(struct seq_file *m) 887 { 888 return mem_cgroup_from_css(seq_css(m)); 889 } 890 891 static inline struct mem_cgroup *lruvec_memcg(struct lruvec *lruvec) 892 { 893 struct mem_cgroup_per_node *mz; 894 895 if (mem_cgroup_disabled()) 896 return NULL; 897 898 mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec); 899 return mz->memcg; 900 } 901 902 /** 903 * parent_mem_cgroup - find the accounting parent of a memcg 904 * @memcg: memcg whose parent to find 905 * 906 * Returns the parent memcg, or NULL if this is the root. 907 */ 908 static inline struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg) 909 { 910 return mem_cgroup_from_css(memcg->css.parent); 911 } 912 913 static inline bool mem_cgroup_is_descendant(struct mem_cgroup *memcg, 914 struct mem_cgroup *root) 915 { 916 if (root == memcg) 917 return true; 918 return cgroup_is_descendant(memcg->css.cgroup, root->css.cgroup); 919 } 920 921 static inline bool mm_match_cgroup(struct mm_struct *mm, 922 struct mem_cgroup *memcg) 923 { 924 struct mem_cgroup *task_memcg; 925 bool match = false; 926 927 rcu_read_lock(); 928 task_memcg = mem_cgroup_from_task(rcu_dereference(mm->owner)); 929 if (task_memcg) 930 match = mem_cgroup_is_descendant(task_memcg, memcg); 931 rcu_read_unlock(); 932 return match; 933 } 934 935 struct cgroup_subsys_state *mem_cgroup_css_from_folio(struct folio *folio); 936 ino_t page_cgroup_ino(struct page *page); 937 938 static inline bool mem_cgroup_online(struct mem_cgroup *memcg) 939 { 940 if (mem_cgroup_disabled()) 941 return true; 942 return !!(memcg->css.flags & CSS_ONLINE); 943 } 944 945 void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru, 946 int zid, int nr_pages); 947 948 static inline 949 unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec, 950 enum lru_list lru, int zone_idx) 951 { 952 struct mem_cgroup_per_node *mz; 953 954 mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec); 955 return READ_ONCE(mz->lru_zone_size[zone_idx][lru]); 956 } 957 958 void mem_cgroup_handle_over_high(gfp_t gfp_mask); 959 960 unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg); 961 962 unsigned long mem_cgroup_size(struct mem_cgroup *memcg); 963 964 void mem_cgroup_print_oom_context(struct mem_cgroup *memcg, 965 struct task_struct *p); 966 967 void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg); 968 969 static inline void mem_cgroup_enter_user_fault(void) 970 { 971 WARN_ON(current->in_user_fault); 972 current->in_user_fault = 1; 973 } 974 975 static inline void mem_cgroup_exit_user_fault(void) 976 { 977 WARN_ON(!current->in_user_fault); 978 current->in_user_fault = 0; 979 } 980 981 static inline bool task_in_memcg_oom(struct task_struct *p) 982 { 983 return p->memcg_in_oom; 984 } 985 986 bool mem_cgroup_oom_synchronize(bool wait); 987 struct mem_cgroup *mem_cgroup_get_oom_group(struct task_struct *victim, 988 struct mem_cgroup *oom_domain); 989 void mem_cgroup_print_oom_group(struct mem_cgroup *memcg); 990 991 void folio_memcg_lock(struct folio *folio); 992 void folio_memcg_unlock(struct folio *folio); 993 994 void __mod_memcg_state(struct mem_cgroup *memcg, int idx, int val); 995 996 /* try to stablize folio_memcg() for all the pages in a memcg */ 997 static inline bool mem_cgroup_trylock_pages(struct mem_cgroup *memcg) 998 { 999 rcu_read_lock(); 1000 1001 if (mem_cgroup_disabled() || !atomic_read(&memcg->moving_account)) 1002 return true; 1003 1004 rcu_read_unlock(); 1005 return false; 1006 } 1007 1008 static inline void mem_cgroup_unlock_pages(void) 1009 { 1010 rcu_read_unlock(); 1011 } 1012 1013 /* idx can be of type enum memcg_stat_item or node_stat_item */ 1014 static inline void mod_memcg_state(struct mem_cgroup *memcg, 1015 int idx, int val) 1016 { 1017 unsigned long flags; 1018 1019 local_irq_save(flags); 1020 __mod_memcg_state(memcg, idx, val); 1021 local_irq_restore(flags); 1022 } 1023 1024 static inline void mod_memcg_page_state(struct page *page, 1025 int idx, int val) 1026 { 1027 struct mem_cgroup *memcg; 1028 1029 if (mem_cgroup_disabled()) 1030 return; 1031 1032 rcu_read_lock(); 1033 memcg = page_memcg(page); 1034 if (memcg) 1035 mod_memcg_state(memcg, idx, val); 1036 rcu_read_unlock(); 1037 } 1038 1039 unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx); 1040 1041 static inline unsigned long lruvec_page_state(struct lruvec *lruvec, 1042 enum node_stat_item idx) 1043 { 1044 struct mem_cgroup_per_node *pn; 1045 long x; 1046 1047 if (mem_cgroup_disabled()) 1048 return node_page_state(lruvec_pgdat(lruvec), idx); 1049 1050 pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec); 1051 x = READ_ONCE(pn->lruvec_stats.state[idx]); 1052 #ifdef CONFIG_SMP 1053 if (x < 0) 1054 x = 0; 1055 #endif 1056 return x; 1057 } 1058 1059 static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec, 1060 enum node_stat_item idx) 1061 { 1062 struct mem_cgroup_per_node *pn; 1063 long x = 0; 1064 1065 if (mem_cgroup_disabled()) 1066 return node_page_state(lruvec_pgdat(lruvec), idx); 1067 1068 pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec); 1069 x = READ_ONCE(pn->lruvec_stats.state_local[idx]); 1070 #ifdef CONFIG_SMP 1071 if (x < 0) 1072 x = 0; 1073 #endif 1074 return x; 1075 } 1076 1077 void mem_cgroup_flush_stats(struct mem_cgroup *memcg); 1078 void mem_cgroup_flush_stats_ratelimited(struct mem_cgroup *memcg); 1079 1080 void __mod_memcg_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx, 1081 int val); 1082 void __mod_lruvec_kmem_state(void *p, enum node_stat_item idx, int val); 1083 1084 static inline void mod_lruvec_kmem_state(void *p, enum node_stat_item idx, 1085 int val) 1086 { 1087 unsigned long flags; 1088 1089 local_irq_save(flags); 1090 __mod_lruvec_kmem_state(p, idx, val); 1091 local_irq_restore(flags); 1092 } 1093 1094 static inline void mod_memcg_lruvec_state(struct lruvec *lruvec, 1095 enum node_stat_item idx, int val) 1096 { 1097 unsigned long flags; 1098 1099 local_irq_save(flags); 1100 __mod_memcg_lruvec_state(lruvec, idx, val); 1101 local_irq_restore(flags); 1102 } 1103 1104 void __count_memcg_events(struct mem_cgroup *memcg, enum vm_event_item idx, 1105 unsigned long count); 1106 1107 static inline void count_memcg_events(struct mem_cgroup *memcg, 1108 enum vm_event_item idx, 1109 unsigned long count) 1110 { 1111 unsigned long flags; 1112 1113 local_irq_save(flags); 1114 __count_memcg_events(memcg, idx, count); 1115 local_irq_restore(flags); 1116 } 1117 1118 static inline void count_memcg_folio_events(struct folio *folio, 1119 enum vm_event_item idx, unsigned long nr) 1120 { 1121 struct mem_cgroup *memcg = folio_memcg(folio); 1122 1123 if (memcg) 1124 count_memcg_events(memcg, idx, nr); 1125 } 1126 1127 static inline void count_memcg_event_mm(struct mm_struct *mm, 1128 enum vm_event_item idx) 1129 { 1130 struct mem_cgroup *memcg; 1131 1132 if (mem_cgroup_disabled()) 1133 return; 1134 1135 rcu_read_lock(); 1136 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner)); 1137 if (likely(memcg)) 1138 count_memcg_events(memcg, idx, 1); 1139 rcu_read_unlock(); 1140 } 1141 1142 static inline void memcg_memory_event(struct mem_cgroup *memcg, 1143 enum memcg_memory_event event) 1144 { 1145 bool swap_event = event == MEMCG_SWAP_HIGH || event == MEMCG_SWAP_MAX || 1146 event == MEMCG_SWAP_FAIL; 1147 1148 atomic_long_inc(&memcg->memory_events_local[event]); 1149 if (!swap_event) 1150 cgroup_file_notify(&memcg->events_local_file); 1151 1152 do { 1153 atomic_long_inc(&memcg->memory_events[event]); 1154 if (swap_event) 1155 cgroup_file_notify(&memcg->swap_events_file); 1156 else 1157 cgroup_file_notify(&memcg->events_file); 1158 1159 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) 1160 break; 1161 if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_LOCAL_EVENTS) 1162 break; 1163 } while ((memcg = parent_mem_cgroup(memcg)) && 1164 !mem_cgroup_is_root(memcg)); 1165 } 1166 1167 static inline void memcg_memory_event_mm(struct mm_struct *mm, 1168 enum memcg_memory_event event) 1169 { 1170 struct mem_cgroup *memcg; 1171 1172 if (mem_cgroup_disabled()) 1173 return; 1174 1175 rcu_read_lock(); 1176 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner)); 1177 if (likely(memcg)) 1178 memcg_memory_event(memcg, event); 1179 rcu_read_unlock(); 1180 } 1181 1182 void split_page_memcg(struct page *head, int old_order, int new_order); 1183 1184 unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order, 1185 gfp_t gfp_mask, 1186 unsigned long *total_scanned); 1187 1188 #else /* CONFIG_MEMCG */ 1189 1190 #define MEM_CGROUP_ID_SHIFT 0 1191 1192 static inline struct mem_cgroup *folio_memcg(struct folio *folio) 1193 { 1194 return NULL; 1195 } 1196 1197 static inline struct mem_cgroup *page_memcg(struct page *page) 1198 { 1199 return NULL; 1200 } 1201 1202 static inline struct mem_cgroup *folio_memcg_rcu(struct folio *folio) 1203 { 1204 WARN_ON_ONCE(!rcu_read_lock_held()); 1205 return NULL; 1206 } 1207 1208 static inline struct mem_cgroup *folio_memcg_check(struct folio *folio) 1209 { 1210 return NULL; 1211 } 1212 1213 static inline struct mem_cgroup *page_memcg_check(struct page *page) 1214 { 1215 return NULL; 1216 } 1217 1218 static inline struct mem_cgroup *get_mem_cgroup_from_objcg(struct obj_cgroup *objcg) 1219 { 1220 return NULL; 1221 } 1222 1223 static inline bool folio_memcg_kmem(struct folio *folio) 1224 { 1225 return false; 1226 } 1227 1228 static inline bool PageMemcgKmem(struct page *page) 1229 { 1230 return false; 1231 } 1232 1233 static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg) 1234 { 1235 return true; 1236 } 1237 1238 static inline bool mem_cgroup_disabled(void) 1239 { 1240 return true; 1241 } 1242 1243 static inline void memcg_memory_event(struct mem_cgroup *memcg, 1244 enum memcg_memory_event event) 1245 { 1246 } 1247 1248 static inline void memcg_memory_event_mm(struct mm_struct *mm, 1249 enum memcg_memory_event event) 1250 { 1251 } 1252 1253 static inline void mem_cgroup_protection(struct mem_cgroup *root, 1254 struct mem_cgroup *memcg, 1255 unsigned long *min, 1256 unsigned long *low) 1257 { 1258 *min = *low = 0; 1259 } 1260 1261 static inline void mem_cgroup_calculate_protection(struct mem_cgroup *root, 1262 struct mem_cgroup *memcg) 1263 { 1264 } 1265 1266 static inline bool mem_cgroup_unprotected(struct mem_cgroup *target, 1267 struct mem_cgroup *memcg) 1268 { 1269 return true; 1270 } 1271 static inline bool mem_cgroup_below_low(struct mem_cgroup *target, 1272 struct mem_cgroup *memcg) 1273 { 1274 return false; 1275 } 1276 1277 static inline bool mem_cgroup_below_min(struct mem_cgroup *target, 1278 struct mem_cgroup *memcg) 1279 { 1280 return false; 1281 } 1282 1283 static inline void mem_cgroup_commit_charge(struct folio *folio, 1284 struct mem_cgroup *memcg) 1285 { 1286 } 1287 1288 static inline int mem_cgroup_charge(struct folio *folio, 1289 struct mm_struct *mm, gfp_t gfp) 1290 { 1291 return 0; 1292 } 1293 1294 static inline int mem_cgroup_hugetlb_try_charge(struct mem_cgroup *memcg, 1295 gfp_t gfp, long nr_pages) 1296 { 1297 return 0; 1298 } 1299 1300 static inline int mem_cgroup_swapin_charge_folio(struct folio *folio, 1301 struct mm_struct *mm, gfp_t gfp, swp_entry_t entry) 1302 { 1303 return 0; 1304 } 1305 1306 static inline void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry) 1307 { 1308 } 1309 1310 static inline void mem_cgroup_uncharge(struct folio *folio) 1311 { 1312 } 1313 1314 static inline void mem_cgroup_uncharge_folios(struct folio_batch *folios) 1315 { 1316 } 1317 1318 static inline void mem_cgroup_cancel_charge(struct mem_cgroup *memcg, 1319 unsigned int nr_pages) 1320 { 1321 } 1322 1323 static inline void mem_cgroup_replace_folio(struct folio *old, 1324 struct folio *new) 1325 { 1326 } 1327 1328 static inline void mem_cgroup_migrate(struct folio *old, struct folio *new) 1329 { 1330 } 1331 1332 static inline struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg, 1333 struct pglist_data *pgdat) 1334 { 1335 return &pgdat->__lruvec; 1336 } 1337 1338 static inline struct lruvec *folio_lruvec(struct folio *folio) 1339 { 1340 struct pglist_data *pgdat = folio_pgdat(folio); 1341 return &pgdat->__lruvec; 1342 } 1343 1344 static inline 1345 void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio) 1346 { 1347 } 1348 1349 static inline struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg) 1350 { 1351 return NULL; 1352 } 1353 1354 static inline bool mm_match_cgroup(struct mm_struct *mm, 1355 struct mem_cgroup *memcg) 1356 { 1357 return true; 1358 } 1359 1360 static inline struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm) 1361 { 1362 return NULL; 1363 } 1364 1365 static inline struct mem_cgroup *get_mem_cgroup_from_current(void) 1366 { 1367 return NULL; 1368 } 1369 1370 static inline 1371 struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css) 1372 { 1373 return NULL; 1374 } 1375 1376 static inline void obj_cgroup_put(struct obj_cgroup *objcg) 1377 { 1378 } 1379 1380 static inline bool mem_cgroup_tryget(struct mem_cgroup *memcg) 1381 { 1382 return true; 1383 } 1384 1385 static inline bool mem_cgroup_tryget_online(struct mem_cgroup *memcg) 1386 { 1387 return true; 1388 } 1389 1390 static inline void mem_cgroup_put(struct mem_cgroup *memcg) 1391 { 1392 } 1393 1394 static inline struct lruvec *folio_lruvec_lock(struct folio *folio) 1395 { 1396 struct pglist_data *pgdat = folio_pgdat(folio); 1397 1398 spin_lock(&pgdat->__lruvec.lru_lock); 1399 return &pgdat->__lruvec; 1400 } 1401 1402 static inline struct lruvec *folio_lruvec_lock_irq(struct folio *folio) 1403 { 1404 struct pglist_data *pgdat = folio_pgdat(folio); 1405 1406 spin_lock_irq(&pgdat->__lruvec.lru_lock); 1407 return &pgdat->__lruvec; 1408 } 1409 1410 static inline struct lruvec *folio_lruvec_lock_irqsave(struct folio *folio, 1411 unsigned long *flagsp) 1412 { 1413 struct pglist_data *pgdat = folio_pgdat(folio); 1414 1415 spin_lock_irqsave(&pgdat->__lruvec.lru_lock, *flagsp); 1416 return &pgdat->__lruvec; 1417 } 1418 1419 static inline struct mem_cgroup * 1420 mem_cgroup_iter(struct mem_cgroup *root, 1421 struct mem_cgroup *prev, 1422 struct mem_cgroup_reclaim_cookie *reclaim) 1423 { 1424 return NULL; 1425 } 1426 1427 static inline void mem_cgroup_iter_break(struct mem_cgroup *root, 1428 struct mem_cgroup *prev) 1429 { 1430 } 1431 1432 static inline void mem_cgroup_scan_tasks(struct mem_cgroup *memcg, 1433 int (*fn)(struct task_struct *, void *), void *arg) 1434 { 1435 } 1436 1437 static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg) 1438 { 1439 return 0; 1440 } 1441 1442 static inline struct mem_cgroup *mem_cgroup_from_id(unsigned short id) 1443 { 1444 WARN_ON_ONCE(id); 1445 /* XXX: This should always return root_mem_cgroup */ 1446 return NULL; 1447 } 1448 1449 #ifdef CONFIG_SHRINKER_DEBUG 1450 static inline unsigned long mem_cgroup_ino(struct mem_cgroup *memcg) 1451 { 1452 return 0; 1453 } 1454 1455 static inline struct mem_cgroup *mem_cgroup_get_from_ino(unsigned long ino) 1456 { 1457 return NULL; 1458 } 1459 #endif 1460 1461 static inline struct mem_cgroup *mem_cgroup_from_seq(struct seq_file *m) 1462 { 1463 return NULL; 1464 } 1465 1466 static inline struct mem_cgroup *lruvec_memcg(struct lruvec *lruvec) 1467 { 1468 return NULL; 1469 } 1470 1471 static inline bool mem_cgroup_online(struct mem_cgroup *memcg) 1472 { 1473 return true; 1474 } 1475 1476 static inline 1477 unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec, 1478 enum lru_list lru, int zone_idx) 1479 { 1480 return 0; 1481 } 1482 1483 static inline unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg) 1484 { 1485 return 0; 1486 } 1487 1488 static inline unsigned long mem_cgroup_size(struct mem_cgroup *memcg) 1489 { 1490 return 0; 1491 } 1492 1493 static inline void 1494 mem_cgroup_print_oom_context(struct mem_cgroup *memcg, struct task_struct *p) 1495 { 1496 } 1497 1498 static inline void 1499 mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg) 1500 { 1501 } 1502 1503 static inline void folio_memcg_lock(struct folio *folio) 1504 { 1505 } 1506 1507 static inline void folio_memcg_unlock(struct folio *folio) 1508 { 1509 } 1510 1511 static inline bool mem_cgroup_trylock_pages(struct mem_cgroup *memcg) 1512 { 1513 /* to match folio_memcg_rcu() */ 1514 rcu_read_lock(); 1515 return true; 1516 } 1517 1518 static inline void mem_cgroup_unlock_pages(void) 1519 { 1520 rcu_read_unlock(); 1521 } 1522 1523 static inline void mem_cgroup_handle_over_high(gfp_t gfp_mask) 1524 { 1525 } 1526 1527 static inline void mem_cgroup_enter_user_fault(void) 1528 { 1529 } 1530 1531 static inline void mem_cgroup_exit_user_fault(void) 1532 { 1533 } 1534 1535 static inline bool task_in_memcg_oom(struct task_struct *p) 1536 { 1537 return false; 1538 } 1539 1540 static inline bool mem_cgroup_oom_synchronize(bool wait) 1541 { 1542 return false; 1543 } 1544 1545 static inline struct mem_cgroup *mem_cgroup_get_oom_group( 1546 struct task_struct *victim, struct mem_cgroup *oom_domain) 1547 { 1548 return NULL; 1549 } 1550 1551 static inline void mem_cgroup_print_oom_group(struct mem_cgroup *memcg) 1552 { 1553 } 1554 1555 static inline void __mod_memcg_state(struct mem_cgroup *memcg, 1556 int idx, 1557 int nr) 1558 { 1559 } 1560 1561 static inline void mod_memcg_state(struct mem_cgroup *memcg, 1562 int idx, 1563 int nr) 1564 { 1565 } 1566 1567 static inline void mod_memcg_page_state(struct page *page, 1568 int idx, int val) 1569 { 1570 } 1571 1572 static inline unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx) 1573 { 1574 return 0; 1575 } 1576 1577 static inline unsigned long lruvec_page_state(struct lruvec *lruvec, 1578 enum node_stat_item idx) 1579 { 1580 return node_page_state(lruvec_pgdat(lruvec), idx); 1581 } 1582 1583 static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec, 1584 enum node_stat_item idx) 1585 { 1586 return node_page_state(lruvec_pgdat(lruvec), idx); 1587 } 1588 1589 static inline void mem_cgroup_flush_stats(struct mem_cgroup *memcg) 1590 { 1591 } 1592 1593 static inline void mem_cgroup_flush_stats_ratelimited(struct mem_cgroup *memcg) 1594 { 1595 } 1596 1597 static inline void __mod_memcg_lruvec_state(struct lruvec *lruvec, 1598 enum node_stat_item idx, int val) 1599 { 1600 } 1601 1602 static inline void __mod_lruvec_kmem_state(void *p, enum node_stat_item idx, 1603 int val) 1604 { 1605 struct page *page = virt_to_head_page(p); 1606 1607 __mod_node_page_state(page_pgdat(page), idx, val); 1608 } 1609 1610 static inline void mod_lruvec_kmem_state(void *p, enum node_stat_item idx, 1611 int val) 1612 { 1613 struct page *page = virt_to_head_page(p); 1614 1615 mod_node_page_state(page_pgdat(page), idx, val); 1616 } 1617 1618 static inline void count_memcg_events(struct mem_cgroup *memcg, 1619 enum vm_event_item idx, 1620 unsigned long count) 1621 { 1622 } 1623 1624 static inline void __count_memcg_events(struct mem_cgroup *memcg, 1625 enum vm_event_item idx, 1626 unsigned long count) 1627 { 1628 } 1629 1630 static inline void count_memcg_folio_events(struct folio *folio, 1631 enum vm_event_item idx, unsigned long nr) 1632 { 1633 } 1634 1635 static inline 1636 void count_memcg_event_mm(struct mm_struct *mm, enum vm_event_item idx) 1637 { 1638 } 1639 1640 static inline void split_page_memcg(struct page *head, int old_order, int new_order) 1641 { 1642 } 1643 1644 static inline 1645 unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order, 1646 gfp_t gfp_mask, 1647 unsigned long *total_scanned) 1648 { 1649 return 0; 1650 } 1651 #endif /* CONFIG_MEMCG */ 1652 1653 /* 1654 * Extended information for slab objects stored as an array in page->memcg_data 1655 * if MEMCG_DATA_OBJEXTS is set. 1656 */ 1657 struct slabobj_ext { 1658 #ifdef CONFIG_MEMCG_KMEM 1659 struct obj_cgroup *objcg; 1660 #endif 1661 #ifdef CONFIG_MEM_ALLOC_PROFILING 1662 union codetag_ref ref; 1663 #endif 1664 } __aligned(8); 1665 1666 static inline void __inc_lruvec_kmem_state(void *p, enum node_stat_item idx) 1667 { 1668 __mod_lruvec_kmem_state(p, idx, 1); 1669 } 1670 1671 static inline void __dec_lruvec_kmem_state(void *p, enum node_stat_item idx) 1672 { 1673 __mod_lruvec_kmem_state(p, idx, -1); 1674 } 1675 1676 static inline struct lruvec *parent_lruvec(struct lruvec *lruvec) 1677 { 1678 struct mem_cgroup *memcg; 1679 1680 memcg = lruvec_memcg(lruvec); 1681 if (!memcg) 1682 return NULL; 1683 memcg = parent_mem_cgroup(memcg); 1684 if (!memcg) 1685 return NULL; 1686 return mem_cgroup_lruvec(memcg, lruvec_pgdat(lruvec)); 1687 } 1688 1689 static inline void unlock_page_lruvec(struct lruvec *lruvec) 1690 { 1691 spin_unlock(&lruvec->lru_lock); 1692 } 1693 1694 static inline void unlock_page_lruvec_irq(struct lruvec *lruvec) 1695 { 1696 spin_unlock_irq(&lruvec->lru_lock); 1697 } 1698 1699 static inline void unlock_page_lruvec_irqrestore(struct lruvec *lruvec, 1700 unsigned long flags) 1701 { 1702 spin_unlock_irqrestore(&lruvec->lru_lock, flags); 1703 } 1704 1705 /* Test requires a stable page->memcg binding, see page_memcg() */ 1706 static inline bool folio_matches_lruvec(struct folio *folio, 1707 struct lruvec *lruvec) 1708 { 1709 return lruvec_pgdat(lruvec) == folio_pgdat(folio) && 1710 lruvec_memcg(lruvec) == folio_memcg(folio); 1711 } 1712 1713 /* Don't lock again iff page's lruvec locked */ 1714 static inline struct lruvec *folio_lruvec_relock_irq(struct folio *folio, 1715 struct lruvec *locked_lruvec) 1716 { 1717 if (locked_lruvec) { 1718 if (folio_matches_lruvec(folio, locked_lruvec)) 1719 return locked_lruvec; 1720 1721 unlock_page_lruvec_irq(locked_lruvec); 1722 } 1723 1724 return folio_lruvec_lock_irq(folio); 1725 } 1726 1727 /* Don't lock again iff folio's lruvec locked */ 1728 static inline void folio_lruvec_relock_irqsave(struct folio *folio, 1729 struct lruvec **lruvecp, unsigned long *flags) 1730 { 1731 if (*lruvecp) { 1732 if (folio_matches_lruvec(folio, *lruvecp)) 1733 return; 1734 1735 unlock_page_lruvec_irqrestore(*lruvecp, *flags); 1736 } 1737 1738 *lruvecp = folio_lruvec_lock_irqsave(folio, flags); 1739 } 1740 1741 #ifdef CONFIG_CGROUP_WRITEBACK 1742 1743 struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb); 1744 void mem_cgroup_wb_stats(struct bdi_writeback *wb, unsigned long *pfilepages, 1745 unsigned long *pheadroom, unsigned long *pdirty, 1746 unsigned long *pwriteback); 1747 1748 void mem_cgroup_track_foreign_dirty_slowpath(struct folio *folio, 1749 struct bdi_writeback *wb); 1750 1751 static inline void mem_cgroup_track_foreign_dirty(struct folio *folio, 1752 struct bdi_writeback *wb) 1753 { 1754 struct mem_cgroup *memcg; 1755 1756 if (mem_cgroup_disabled()) 1757 return; 1758 1759 memcg = folio_memcg(folio); 1760 if (unlikely(memcg && &memcg->css != wb->memcg_css)) 1761 mem_cgroup_track_foreign_dirty_slowpath(folio, wb); 1762 } 1763 1764 void mem_cgroup_flush_foreign(struct bdi_writeback *wb); 1765 1766 #else /* CONFIG_CGROUP_WRITEBACK */ 1767 1768 static inline struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb) 1769 { 1770 return NULL; 1771 } 1772 1773 static inline void mem_cgroup_wb_stats(struct bdi_writeback *wb, 1774 unsigned long *pfilepages, 1775 unsigned long *pheadroom, 1776 unsigned long *pdirty, 1777 unsigned long *pwriteback) 1778 { 1779 } 1780 1781 static inline void mem_cgroup_track_foreign_dirty(struct folio *folio, 1782 struct bdi_writeback *wb) 1783 { 1784 } 1785 1786 static inline void mem_cgroup_flush_foreign(struct bdi_writeback *wb) 1787 { 1788 } 1789 1790 #endif /* CONFIG_CGROUP_WRITEBACK */ 1791 1792 struct sock; 1793 bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages, 1794 gfp_t gfp_mask); 1795 void mem_cgroup_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages); 1796 #ifdef CONFIG_MEMCG 1797 extern struct static_key_false memcg_sockets_enabled_key; 1798 #define mem_cgroup_sockets_enabled static_branch_unlikely(&memcg_sockets_enabled_key) 1799 void mem_cgroup_sk_alloc(struct sock *sk); 1800 void mem_cgroup_sk_free(struct sock *sk); 1801 static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg) 1802 { 1803 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) 1804 return !!memcg->tcpmem_pressure; 1805 do { 1806 if (time_before(jiffies, READ_ONCE(memcg->socket_pressure))) 1807 return true; 1808 } while ((memcg = parent_mem_cgroup(memcg))); 1809 return false; 1810 } 1811 1812 int alloc_shrinker_info(struct mem_cgroup *memcg); 1813 void free_shrinker_info(struct mem_cgroup *memcg); 1814 void set_shrinker_bit(struct mem_cgroup *memcg, int nid, int shrinker_id); 1815 void reparent_shrinker_deferred(struct mem_cgroup *memcg); 1816 #else 1817 #define mem_cgroup_sockets_enabled 0 1818 static inline void mem_cgroup_sk_alloc(struct sock *sk) { }; 1819 static inline void mem_cgroup_sk_free(struct sock *sk) { }; 1820 static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg) 1821 { 1822 return false; 1823 } 1824 1825 static inline void set_shrinker_bit(struct mem_cgroup *memcg, 1826 int nid, int shrinker_id) 1827 { 1828 } 1829 #endif 1830 1831 #ifdef CONFIG_MEMCG_KMEM 1832 bool mem_cgroup_kmem_disabled(void); 1833 int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, int order); 1834 void __memcg_kmem_uncharge_page(struct page *page, int order); 1835 1836 /* 1837 * The returned objcg pointer is safe to use without additional 1838 * protection within a scope. The scope is defined either by 1839 * the current task (similar to the "current" global variable) 1840 * or by set_active_memcg() pair. 1841 * Please, use obj_cgroup_get() to get a reference if the pointer 1842 * needs to be used outside of the local scope. 1843 */ 1844 struct obj_cgroup *current_obj_cgroup(void); 1845 struct obj_cgroup *get_obj_cgroup_from_folio(struct folio *folio); 1846 1847 static inline struct obj_cgroup *get_obj_cgroup_from_current(void) 1848 { 1849 struct obj_cgroup *objcg = current_obj_cgroup(); 1850 1851 if (objcg) 1852 obj_cgroup_get(objcg); 1853 1854 return objcg; 1855 } 1856 1857 int obj_cgroup_charge(struct obj_cgroup *objcg, gfp_t gfp, size_t size); 1858 void obj_cgroup_uncharge(struct obj_cgroup *objcg, size_t size); 1859 1860 extern struct static_key_false memcg_bpf_enabled_key; 1861 static inline bool memcg_bpf_enabled(void) 1862 { 1863 return static_branch_likely(&memcg_bpf_enabled_key); 1864 } 1865 1866 extern struct static_key_false memcg_kmem_online_key; 1867 1868 static inline bool memcg_kmem_online(void) 1869 { 1870 return static_branch_likely(&memcg_kmem_online_key); 1871 } 1872 1873 static inline int memcg_kmem_charge_page(struct page *page, gfp_t gfp, 1874 int order) 1875 { 1876 if (memcg_kmem_online()) 1877 return __memcg_kmem_charge_page(page, gfp, order); 1878 return 0; 1879 } 1880 1881 static inline void memcg_kmem_uncharge_page(struct page *page, int order) 1882 { 1883 if (memcg_kmem_online()) 1884 __memcg_kmem_uncharge_page(page, order); 1885 } 1886 1887 /* 1888 * A helper for accessing memcg's kmem_id, used for getting 1889 * corresponding LRU lists. 1890 */ 1891 static inline int memcg_kmem_id(struct mem_cgroup *memcg) 1892 { 1893 return memcg ? memcg->kmemcg_id : -1; 1894 } 1895 1896 struct mem_cgroup *mem_cgroup_from_obj(void *p); 1897 struct mem_cgroup *mem_cgroup_from_slab_obj(void *p); 1898 1899 static inline void count_objcg_event(struct obj_cgroup *objcg, 1900 enum vm_event_item idx) 1901 { 1902 struct mem_cgroup *memcg; 1903 1904 if (!memcg_kmem_online()) 1905 return; 1906 1907 rcu_read_lock(); 1908 memcg = obj_cgroup_memcg(objcg); 1909 count_memcg_events(memcg, idx, 1); 1910 rcu_read_unlock(); 1911 } 1912 1913 #else 1914 static inline bool mem_cgroup_kmem_disabled(void) 1915 { 1916 return true; 1917 } 1918 1919 static inline int memcg_kmem_charge_page(struct page *page, gfp_t gfp, 1920 int order) 1921 { 1922 return 0; 1923 } 1924 1925 static inline void memcg_kmem_uncharge_page(struct page *page, int order) 1926 { 1927 } 1928 1929 static inline int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, 1930 int order) 1931 { 1932 return 0; 1933 } 1934 1935 static inline void __memcg_kmem_uncharge_page(struct page *page, int order) 1936 { 1937 } 1938 1939 static inline struct obj_cgroup *get_obj_cgroup_from_folio(struct folio *folio) 1940 { 1941 return NULL; 1942 } 1943 1944 static inline bool memcg_bpf_enabled(void) 1945 { 1946 return false; 1947 } 1948 1949 static inline bool memcg_kmem_online(void) 1950 { 1951 return false; 1952 } 1953 1954 static inline int memcg_kmem_id(struct mem_cgroup *memcg) 1955 { 1956 return -1; 1957 } 1958 1959 static inline struct mem_cgroup *mem_cgroup_from_obj(void *p) 1960 { 1961 return NULL; 1962 } 1963 1964 static inline struct mem_cgroup *mem_cgroup_from_slab_obj(void *p) 1965 { 1966 return NULL; 1967 } 1968 1969 static inline void count_objcg_event(struct obj_cgroup *objcg, 1970 enum vm_event_item idx) 1971 { 1972 } 1973 1974 #endif /* CONFIG_MEMCG_KMEM */ 1975 1976 #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP) 1977 bool obj_cgroup_may_zswap(struct obj_cgroup *objcg); 1978 void obj_cgroup_charge_zswap(struct obj_cgroup *objcg, size_t size); 1979 void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg, size_t size); 1980 bool mem_cgroup_zswap_writeback_enabled(struct mem_cgroup *memcg); 1981 #else 1982 static inline bool obj_cgroup_may_zswap(struct obj_cgroup *objcg) 1983 { 1984 return true; 1985 } 1986 static inline void obj_cgroup_charge_zswap(struct obj_cgroup *objcg, 1987 size_t size) 1988 { 1989 } 1990 static inline void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg, 1991 size_t size) 1992 { 1993 } 1994 static inline bool mem_cgroup_zswap_writeback_enabled(struct mem_cgroup *memcg) 1995 { 1996 /* if zswap is disabled, do not block pages going to the swapping device */ 1997 return true; 1998 } 1999 #endif 2000 2001 #endif /* _LINUX_MEMCONTROL_H */ 2002