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