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