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