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