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