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