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