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