1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* memcontrol.c - 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 * Memory thresholds 11 * Copyright (C) 2009 Nokia Corporation 12 * Author: Kirill A. Shutemov 13 * 14 * Kernel Memory Controller 15 * Copyright (C) 2012 Parallels Inc. and Google Inc. 16 * Authors: Glauber Costa and Suleiman Souhlal 17 * 18 * Native page reclaim 19 * Charge lifetime sanitation 20 * Lockless page tracking & accounting 21 * Unified hierarchy configuration model 22 * Copyright (C) 2015 Red Hat, Inc., Johannes Weiner 23 * 24 * Per memcg lru locking 25 * Copyright (C) 2020 Alibaba, Inc, Alex Shi 26 */ 27 28 #include <linux/cgroup-defs.h> 29 #include <linux/page_counter.h> 30 #include <linux/memcontrol.h> 31 #include <linux/cgroup.h> 32 #include <linux/cpuset.h> 33 #include <linux/sched/mm.h> 34 #include <linux/shmem_fs.h> 35 #include <linux/hugetlb.h> 36 #include <linux/pagemap.h> 37 #include <linux/pagevec.h> 38 #include <linux/vm_event_item.h> 39 #include <linux/smp.h> 40 #include <linux/page-flags.h> 41 #include <linux/backing-dev.h> 42 #include <linux/bit_spinlock.h> 43 #include <linux/rcupdate.h> 44 #include <linux/limits.h> 45 #include <linux/export.h> 46 #include <linux/list.h> 47 #include <linux/mutex.h> 48 #include <linux/rbtree.h> 49 #include <linux/slab.h> 50 #include <linux/swapops.h> 51 #include <linux/spinlock.h> 52 #include <linux/fs.h> 53 #include <linux/seq_file.h> 54 #include <linux/vmpressure.h> 55 #include <linux/memremap.h> 56 #include <linux/mm_inline.h> 57 #include <linux/swap_cgroup.h> 58 #include <linux/cpu.h> 59 #include <linux/oom.h> 60 #include <linux/lockdep.h> 61 #include <linux/resume_user_mode.h> 62 #include <linux/psi.h> 63 #include <linux/seq_buf.h> 64 #include <linux/sched/isolation.h> 65 #include <linux/kmemleak.h> 66 #include "internal.h" 67 #include <net/sock.h> 68 #include <net/ip.h> 69 #include "slab.h" 70 #include "memcontrol-v1.h" 71 72 #include <linux/uaccess.h> 73 74 #define CREATE_TRACE_POINTS 75 #include <trace/events/memcg.h> 76 #undef CREATE_TRACE_POINTS 77 78 #include <trace/events/vmscan.h> 79 80 struct cgroup_subsys memory_cgrp_subsys __read_mostly; 81 EXPORT_SYMBOL(memory_cgrp_subsys); 82 83 struct mem_cgroup *root_mem_cgroup __read_mostly; 84 EXPORT_SYMBOL(root_mem_cgroup); 85 86 /* Active memory cgroup to use from an interrupt context */ 87 DEFINE_PER_CPU(struct mem_cgroup *, int_active_memcg); 88 EXPORT_PER_CPU_SYMBOL_GPL(int_active_memcg); 89 90 /* Socket memory accounting disabled? */ 91 static bool cgroup_memory_nosocket __ro_after_init; 92 93 /* Kernel memory accounting disabled? */ 94 static bool cgroup_memory_nokmem __ro_after_init; 95 96 /* BPF memory accounting disabled? */ 97 static bool cgroup_memory_nobpf __ro_after_init; 98 99 static struct kmem_cache *memcg_cachep; 100 static struct kmem_cache *memcg_pn_cachep; 101 102 #ifdef CONFIG_CGROUP_WRITEBACK 103 static DECLARE_WAIT_QUEUE_HEAD(memcg_cgwb_frn_waitq); 104 #endif 105 106 static inline bool task_is_dying(void) 107 { 108 return tsk_is_oom_victim(current) || fatal_signal_pending(current) || 109 (current->flags & PF_EXITING); 110 } 111 112 /* Some nice accessors for the vmpressure. */ 113 struct vmpressure *memcg_to_vmpressure(struct mem_cgroup *memcg) 114 { 115 if (!memcg) 116 memcg = root_mem_cgroup; 117 return &memcg->vmpressure; 118 } 119 120 struct mem_cgroup *vmpressure_to_memcg(struct vmpressure *vmpr) 121 { 122 return container_of(vmpr, struct mem_cgroup, vmpressure); 123 } 124 125 #define SEQ_BUF_SIZE SZ_4K 126 #define CURRENT_OBJCG_UPDATE_BIT 0 127 #define CURRENT_OBJCG_UPDATE_FLAG (1UL << CURRENT_OBJCG_UPDATE_BIT) 128 129 static DEFINE_SPINLOCK(objcg_lock); 130 131 bool mem_cgroup_kmem_disabled(void) 132 { 133 return cgroup_memory_nokmem; 134 } 135 136 static void memcg_uncharge(struct mem_cgroup *memcg, unsigned int nr_pages); 137 138 static void obj_cgroup_release(struct percpu_ref *ref) 139 { 140 struct obj_cgroup *objcg = container_of(ref, struct obj_cgroup, refcnt); 141 unsigned int nr_bytes; 142 unsigned int nr_pages; 143 unsigned long flags; 144 145 /* 146 * At this point all allocated objects are freed, and 147 * objcg->nr_charged_bytes can't have an arbitrary byte value. 148 * However, it can be PAGE_SIZE or (x * PAGE_SIZE). 149 * 150 * The following sequence can lead to it: 151 * 1) CPU0: objcg == stock->cached_objcg 152 * 2) CPU1: we do a small allocation (e.g. 92 bytes), 153 * PAGE_SIZE bytes are charged 154 * 3) CPU1: a process from another memcg is allocating something, 155 * the stock if flushed, 156 * objcg->nr_charged_bytes = PAGE_SIZE - 92 157 * 5) CPU0: we do release this object, 158 * 92 bytes are added to stock->nr_bytes 159 * 6) CPU0: stock is flushed, 160 * 92 bytes are added to objcg->nr_charged_bytes 161 * 162 * In the result, nr_charged_bytes == PAGE_SIZE. 163 * This page will be uncharged in obj_cgroup_release(). 164 */ 165 nr_bytes = atomic_read(&objcg->nr_charged_bytes); 166 WARN_ON_ONCE(nr_bytes & (PAGE_SIZE - 1)); 167 nr_pages = nr_bytes >> PAGE_SHIFT; 168 169 if (nr_pages) { 170 struct mem_cgroup *memcg; 171 172 memcg = get_mem_cgroup_from_objcg(objcg); 173 mod_memcg_state(memcg, MEMCG_KMEM, -nr_pages); 174 memcg1_account_kmem(memcg, -nr_pages); 175 if (!mem_cgroup_is_root(memcg)) 176 memcg_uncharge(memcg, nr_pages); 177 mem_cgroup_put(memcg); 178 } 179 180 spin_lock_irqsave(&objcg_lock, flags); 181 list_del(&objcg->list); 182 spin_unlock_irqrestore(&objcg_lock, flags); 183 184 percpu_ref_exit(ref); 185 kfree_rcu(objcg, rcu); 186 } 187 188 static struct obj_cgroup *obj_cgroup_alloc(void) 189 { 190 struct obj_cgroup *objcg; 191 int ret; 192 193 objcg = kzalloc(sizeof(struct obj_cgroup), GFP_KERNEL); 194 if (!objcg) 195 return NULL; 196 197 ret = percpu_ref_init(&objcg->refcnt, obj_cgroup_release, 0, 198 GFP_KERNEL); 199 if (ret) { 200 kfree(objcg); 201 return NULL; 202 } 203 INIT_LIST_HEAD(&objcg->list); 204 return objcg; 205 } 206 207 static void memcg_reparent_objcgs(struct mem_cgroup *memcg, 208 struct mem_cgroup *parent) 209 { 210 struct obj_cgroup *objcg, *iter; 211 212 objcg = rcu_replace_pointer(memcg->objcg, NULL, true); 213 214 spin_lock_irq(&objcg_lock); 215 216 /* 1) Ready to reparent active objcg. */ 217 list_add(&objcg->list, &memcg->objcg_list); 218 /* 2) Reparent active objcg and already reparented objcgs to parent. */ 219 list_for_each_entry(iter, &memcg->objcg_list, list) 220 WRITE_ONCE(iter->memcg, parent); 221 /* 3) Move already reparented objcgs to the parent's list */ 222 list_splice(&memcg->objcg_list, &parent->objcg_list); 223 224 spin_unlock_irq(&objcg_lock); 225 226 percpu_ref_kill(&objcg->refcnt); 227 } 228 229 /* 230 * A lot of the calls to the cache allocation functions are expected to be 231 * inlined by the compiler. Since the calls to memcg_slab_post_alloc_hook() are 232 * conditional to this static branch, we'll have to allow modules that does 233 * kmem_cache_alloc and the such to see this symbol as well 234 */ 235 DEFINE_STATIC_KEY_FALSE(memcg_kmem_online_key); 236 EXPORT_SYMBOL(memcg_kmem_online_key); 237 238 DEFINE_STATIC_KEY_FALSE(memcg_bpf_enabled_key); 239 EXPORT_SYMBOL(memcg_bpf_enabled_key); 240 241 /** 242 * mem_cgroup_css_from_folio - css of the memcg associated with a folio 243 * @folio: folio of interest 244 * 245 * If memcg is bound to the default hierarchy, css of the memcg associated 246 * with @folio is returned. The returned css remains associated with @folio 247 * until it is released. 248 * 249 * If memcg is bound to a traditional hierarchy, the css of root_mem_cgroup 250 * is returned. 251 */ 252 struct cgroup_subsys_state *mem_cgroup_css_from_folio(struct folio *folio) 253 { 254 struct mem_cgroup *memcg = folio_memcg(folio); 255 256 if (!memcg || !cgroup_subsys_on_dfl(memory_cgrp_subsys)) 257 memcg = root_mem_cgroup; 258 259 return &memcg->css; 260 } 261 262 /** 263 * page_cgroup_ino - return inode number of the memcg a page is charged to 264 * @page: the page 265 * 266 * Look up the closest online ancestor of the memory cgroup @page is charged to 267 * and return its inode number or 0 if @page is not charged to any cgroup. It 268 * is safe to call this function without holding a reference to @page. 269 * 270 * Note, this function is inherently racy, because there is nothing to prevent 271 * the cgroup inode from getting torn down and potentially reallocated a moment 272 * after page_cgroup_ino() returns, so it only should be used by callers that 273 * do not care (such as procfs interfaces). 274 */ 275 ino_t page_cgroup_ino(struct page *page) 276 { 277 struct mem_cgroup *memcg; 278 unsigned long ino = 0; 279 280 rcu_read_lock(); 281 /* page_folio() is racy here, but the entire function is racy anyway */ 282 memcg = folio_memcg_check(page_folio(page)); 283 284 while (memcg && !(memcg->css.flags & CSS_ONLINE)) 285 memcg = parent_mem_cgroup(memcg); 286 if (memcg) 287 ino = cgroup_ino(memcg->css.cgroup); 288 rcu_read_unlock(); 289 return ino; 290 } 291 EXPORT_SYMBOL_GPL(page_cgroup_ino); 292 293 /* Subset of node_stat_item for memcg stats */ 294 static const unsigned int memcg_node_stat_items[] = { 295 NR_INACTIVE_ANON, 296 NR_ACTIVE_ANON, 297 NR_INACTIVE_FILE, 298 NR_ACTIVE_FILE, 299 NR_UNEVICTABLE, 300 NR_SLAB_RECLAIMABLE_B, 301 NR_SLAB_UNRECLAIMABLE_B, 302 WORKINGSET_REFAULT_ANON, 303 WORKINGSET_REFAULT_FILE, 304 WORKINGSET_ACTIVATE_ANON, 305 WORKINGSET_ACTIVATE_FILE, 306 WORKINGSET_RESTORE_ANON, 307 WORKINGSET_RESTORE_FILE, 308 WORKINGSET_NODERECLAIM, 309 NR_ANON_MAPPED, 310 NR_FILE_MAPPED, 311 NR_FILE_PAGES, 312 NR_FILE_DIRTY, 313 NR_WRITEBACK, 314 NR_SHMEM, 315 NR_SHMEM_THPS, 316 NR_FILE_THPS, 317 NR_ANON_THPS, 318 NR_KERNEL_STACK_KB, 319 NR_PAGETABLE, 320 NR_SECONDARY_PAGETABLE, 321 #ifdef CONFIG_SWAP 322 NR_SWAPCACHE, 323 #endif 324 #ifdef CONFIG_NUMA_BALANCING 325 PGPROMOTE_SUCCESS, 326 #endif 327 PGDEMOTE_KSWAPD, 328 PGDEMOTE_DIRECT, 329 PGDEMOTE_KHUGEPAGED, 330 PGDEMOTE_PROACTIVE, 331 #ifdef CONFIG_HUGETLB_PAGE 332 NR_HUGETLB, 333 #endif 334 }; 335 336 static const unsigned int memcg_stat_items[] = { 337 MEMCG_SWAP, 338 MEMCG_SOCK, 339 MEMCG_PERCPU_B, 340 MEMCG_VMALLOC, 341 MEMCG_KMEM, 342 MEMCG_ZSWAP_B, 343 MEMCG_ZSWAPPED, 344 }; 345 346 #define NR_MEMCG_NODE_STAT_ITEMS ARRAY_SIZE(memcg_node_stat_items) 347 #define MEMCG_VMSTAT_SIZE (NR_MEMCG_NODE_STAT_ITEMS + \ 348 ARRAY_SIZE(memcg_stat_items)) 349 #define BAD_STAT_IDX(index) ((u32)(index) >= U8_MAX) 350 static u8 mem_cgroup_stats_index[MEMCG_NR_STAT] __read_mostly; 351 352 static void init_memcg_stats(void) 353 { 354 u8 i, j = 0; 355 356 BUILD_BUG_ON(MEMCG_NR_STAT >= U8_MAX); 357 358 memset(mem_cgroup_stats_index, U8_MAX, sizeof(mem_cgroup_stats_index)); 359 360 for (i = 0; i < NR_MEMCG_NODE_STAT_ITEMS; ++i, ++j) 361 mem_cgroup_stats_index[memcg_node_stat_items[i]] = j; 362 363 for (i = 0; i < ARRAY_SIZE(memcg_stat_items); ++i, ++j) 364 mem_cgroup_stats_index[memcg_stat_items[i]] = j; 365 } 366 367 static inline int memcg_stats_index(int idx) 368 { 369 return mem_cgroup_stats_index[idx]; 370 } 371 372 struct lruvec_stats_percpu { 373 /* Local (CPU and cgroup) state */ 374 long state[NR_MEMCG_NODE_STAT_ITEMS]; 375 376 /* Delta calculation for lockless upward propagation */ 377 long state_prev[NR_MEMCG_NODE_STAT_ITEMS]; 378 }; 379 380 struct lruvec_stats { 381 /* Aggregated (CPU and subtree) state */ 382 long state[NR_MEMCG_NODE_STAT_ITEMS]; 383 384 /* Non-hierarchical (CPU aggregated) state */ 385 long state_local[NR_MEMCG_NODE_STAT_ITEMS]; 386 387 /* Pending child counts during tree propagation */ 388 long state_pending[NR_MEMCG_NODE_STAT_ITEMS]; 389 }; 390 391 unsigned long lruvec_page_state(struct lruvec *lruvec, enum node_stat_item idx) 392 { 393 struct mem_cgroup_per_node *pn; 394 long x; 395 int i; 396 397 if (mem_cgroup_disabled()) 398 return node_page_state(lruvec_pgdat(lruvec), idx); 399 400 i = memcg_stats_index(idx); 401 if (WARN_ONCE(BAD_STAT_IDX(i), "%s: missing stat item %d\n", __func__, idx)) 402 return 0; 403 404 pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec); 405 x = READ_ONCE(pn->lruvec_stats->state[i]); 406 #ifdef CONFIG_SMP 407 if (x < 0) 408 x = 0; 409 #endif 410 return x; 411 } 412 413 unsigned long lruvec_page_state_local(struct lruvec *lruvec, 414 enum node_stat_item idx) 415 { 416 struct mem_cgroup_per_node *pn; 417 long x; 418 int i; 419 420 if (mem_cgroup_disabled()) 421 return node_page_state(lruvec_pgdat(lruvec), idx); 422 423 i = memcg_stats_index(idx); 424 if (WARN_ONCE(BAD_STAT_IDX(i), "%s: missing stat item %d\n", __func__, idx)) 425 return 0; 426 427 pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec); 428 x = READ_ONCE(pn->lruvec_stats->state_local[i]); 429 #ifdef CONFIG_SMP 430 if (x < 0) 431 x = 0; 432 #endif 433 return x; 434 } 435 436 /* Subset of vm_event_item to report for memcg event stats */ 437 static const unsigned int memcg_vm_event_stat[] = { 438 #ifdef CONFIG_MEMCG_V1 439 PGPGIN, 440 PGPGOUT, 441 #endif 442 PSWPIN, 443 PSWPOUT, 444 PGSCAN_KSWAPD, 445 PGSCAN_DIRECT, 446 PGSCAN_KHUGEPAGED, 447 PGSCAN_PROACTIVE, 448 PGSTEAL_KSWAPD, 449 PGSTEAL_DIRECT, 450 PGSTEAL_KHUGEPAGED, 451 PGSTEAL_PROACTIVE, 452 PGFAULT, 453 PGMAJFAULT, 454 PGREFILL, 455 PGACTIVATE, 456 PGDEACTIVATE, 457 PGLAZYFREE, 458 PGLAZYFREED, 459 #ifdef CONFIG_SWAP 460 SWPIN_ZERO, 461 SWPOUT_ZERO, 462 #endif 463 #ifdef CONFIG_ZSWAP 464 ZSWPIN, 465 ZSWPOUT, 466 ZSWPWB, 467 #endif 468 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 469 THP_FAULT_ALLOC, 470 THP_COLLAPSE_ALLOC, 471 THP_SWPOUT, 472 THP_SWPOUT_FALLBACK, 473 #endif 474 #ifdef CONFIG_NUMA_BALANCING 475 NUMA_PAGE_MIGRATE, 476 NUMA_PTE_UPDATES, 477 NUMA_HINT_FAULTS, 478 #endif 479 }; 480 481 #define NR_MEMCG_EVENTS ARRAY_SIZE(memcg_vm_event_stat) 482 static u8 mem_cgroup_events_index[NR_VM_EVENT_ITEMS] __read_mostly; 483 484 static void init_memcg_events(void) 485 { 486 u8 i; 487 488 BUILD_BUG_ON(NR_VM_EVENT_ITEMS >= U8_MAX); 489 490 memset(mem_cgroup_events_index, U8_MAX, 491 sizeof(mem_cgroup_events_index)); 492 493 for (i = 0; i < NR_MEMCG_EVENTS; ++i) 494 mem_cgroup_events_index[memcg_vm_event_stat[i]] = i; 495 } 496 497 static inline int memcg_events_index(enum vm_event_item idx) 498 { 499 return mem_cgroup_events_index[idx]; 500 } 501 502 struct memcg_vmstats_percpu { 503 /* Stats updates since the last flush */ 504 unsigned int stats_updates; 505 506 /* Cached pointers for fast iteration in memcg_rstat_updated() */ 507 struct memcg_vmstats_percpu __percpu *parent_pcpu; 508 struct memcg_vmstats *vmstats; 509 510 /* The above should fit a single cacheline for memcg_rstat_updated() */ 511 512 /* Local (CPU and cgroup) page state & events */ 513 long state[MEMCG_VMSTAT_SIZE]; 514 unsigned long events[NR_MEMCG_EVENTS]; 515 516 /* Delta calculation for lockless upward propagation */ 517 long state_prev[MEMCG_VMSTAT_SIZE]; 518 unsigned long events_prev[NR_MEMCG_EVENTS]; 519 } ____cacheline_aligned; 520 521 struct memcg_vmstats { 522 /* Aggregated (CPU and subtree) page state & events */ 523 long state[MEMCG_VMSTAT_SIZE]; 524 unsigned long events[NR_MEMCG_EVENTS]; 525 526 /* Non-hierarchical (CPU aggregated) page state & events */ 527 long state_local[MEMCG_VMSTAT_SIZE]; 528 unsigned long events_local[NR_MEMCG_EVENTS]; 529 530 /* Pending child counts during tree propagation */ 531 long state_pending[MEMCG_VMSTAT_SIZE]; 532 unsigned long events_pending[NR_MEMCG_EVENTS]; 533 534 /* Stats updates since the last flush */ 535 atomic_t stats_updates; 536 }; 537 538 /* 539 * memcg and lruvec stats flushing 540 * 541 * Many codepaths leading to stats update or read are performance sensitive and 542 * adding stats flushing in such codepaths is not desirable. So, to optimize the 543 * flushing the kernel does: 544 * 545 * 1) Periodically and asynchronously flush the stats every 2 seconds to not let 546 * rstat update tree grow unbounded. 547 * 548 * 2) Flush the stats synchronously on reader side only when there are more than 549 * (MEMCG_CHARGE_BATCH * nr_cpus) update events. Though this optimization 550 * will let stats be out of sync by atmost (MEMCG_CHARGE_BATCH * nr_cpus) but 551 * only for 2 seconds due to (1). 552 */ 553 static void flush_memcg_stats_dwork(struct work_struct *w); 554 static DECLARE_DEFERRABLE_WORK(stats_flush_dwork, flush_memcg_stats_dwork); 555 static u64 flush_last_time; 556 557 #define FLUSH_TIME (2UL*HZ) 558 559 static bool memcg_vmstats_needs_flush(struct memcg_vmstats *vmstats) 560 { 561 return atomic_read(&vmstats->stats_updates) > 562 MEMCG_CHARGE_BATCH * num_online_cpus(); 563 } 564 565 static inline void memcg_rstat_updated(struct mem_cgroup *memcg, int val, 566 int cpu) 567 { 568 struct memcg_vmstats_percpu __percpu *statc_pcpu; 569 struct memcg_vmstats_percpu *statc; 570 unsigned int stats_updates; 571 572 if (!val) 573 return; 574 575 css_rstat_updated(&memcg->css, cpu); 576 statc_pcpu = memcg->vmstats_percpu; 577 for (; statc_pcpu; statc_pcpu = statc->parent_pcpu) { 578 statc = this_cpu_ptr(statc_pcpu); 579 /* 580 * If @memcg is already flushable then all its ancestors are 581 * flushable as well and also there is no need to increase 582 * stats_updates. 583 */ 584 if (memcg_vmstats_needs_flush(statc->vmstats)) 585 break; 586 587 stats_updates = this_cpu_add_return(statc_pcpu->stats_updates, 588 abs(val)); 589 if (stats_updates < MEMCG_CHARGE_BATCH) 590 continue; 591 592 stats_updates = this_cpu_xchg(statc_pcpu->stats_updates, 0); 593 atomic_add(stats_updates, &statc->vmstats->stats_updates); 594 } 595 } 596 597 static void __mem_cgroup_flush_stats(struct mem_cgroup *memcg, bool force) 598 { 599 bool needs_flush = memcg_vmstats_needs_flush(memcg->vmstats); 600 601 trace_memcg_flush_stats(memcg, atomic_read(&memcg->vmstats->stats_updates), 602 force, needs_flush); 603 604 if (!force && !needs_flush) 605 return; 606 607 if (mem_cgroup_is_root(memcg)) 608 WRITE_ONCE(flush_last_time, jiffies_64); 609 610 css_rstat_flush(&memcg->css); 611 } 612 613 /* 614 * mem_cgroup_flush_stats - flush the stats of a memory cgroup subtree 615 * @memcg: root of the subtree to flush 616 * 617 * Flushing is serialized by the underlying global rstat lock. There is also a 618 * minimum amount of work to be done even if there are no stat updates to flush. 619 * Hence, we only flush the stats if the updates delta exceeds a threshold. This 620 * avoids unnecessary work and contention on the underlying lock. 621 */ 622 void mem_cgroup_flush_stats(struct mem_cgroup *memcg) 623 { 624 if (mem_cgroup_disabled()) 625 return; 626 627 if (!memcg) 628 memcg = root_mem_cgroup; 629 630 __mem_cgroup_flush_stats(memcg, false); 631 } 632 633 void mem_cgroup_flush_stats_ratelimited(struct mem_cgroup *memcg) 634 { 635 /* Only flush if the periodic flusher is one full cycle late */ 636 if (time_after64(jiffies_64, READ_ONCE(flush_last_time) + 2*FLUSH_TIME)) 637 mem_cgroup_flush_stats(memcg); 638 } 639 640 static void flush_memcg_stats_dwork(struct work_struct *w) 641 { 642 /* 643 * Deliberately ignore memcg_vmstats_needs_flush() here so that flushing 644 * in latency-sensitive paths is as cheap as possible. 645 */ 646 __mem_cgroup_flush_stats(root_mem_cgroup, true); 647 queue_delayed_work(system_unbound_wq, &stats_flush_dwork, FLUSH_TIME); 648 } 649 650 unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx) 651 { 652 long x; 653 int i = memcg_stats_index(idx); 654 655 if (WARN_ONCE(BAD_STAT_IDX(i), "%s: missing stat item %d\n", __func__, idx)) 656 return 0; 657 658 x = READ_ONCE(memcg->vmstats->state[i]); 659 #ifdef CONFIG_SMP 660 if (x < 0) 661 x = 0; 662 #endif 663 return x; 664 } 665 666 static int memcg_page_state_unit(int item); 667 668 /* 669 * Normalize the value passed into memcg_rstat_updated() to be in pages. Round 670 * up non-zero sub-page updates to 1 page as zero page updates are ignored. 671 */ 672 static int memcg_state_val_in_pages(int idx, int val) 673 { 674 int unit = memcg_page_state_unit(idx); 675 676 if (!val || unit == PAGE_SIZE) 677 return val; 678 else 679 return max(val * unit / PAGE_SIZE, 1UL); 680 } 681 682 /** 683 * mod_memcg_state - update cgroup memory statistics 684 * @memcg: the memory cgroup 685 * @idx: the stat item - can be enum memcg_stat_item or enum node_stat_item 686 * @val: delta to add to the counter, can be negative 687 */ 688 void mod_memcg_state(struct mem_cgroup *memcg, enum memcg_stat_item idx, 689 int val) 690 { 691 int i = memcg_stats_index(idx); 692 int cpu; 693 694 if (mem_cgroup_disabled()) 695 return; 696 697 if (WARN_ONCE(BAD_STAT_IDX(i), "%s: missing stat item %d\n", __func__, idx)) 698 return; 699 700 cpu = get_cpu(); 701 702 this_cpu_add(memcg->vmstats_percpu->state[i], val); 703 val = memcg_state_val_in_pages(idx, val); 704 memcg_rstat_updated(memcg, val, cpu); 705 trace_mod_memcg_state(memcg, idx, val); 706 707 put_cpu(); 708 } 709 710 #ifdef CONFIG_MEMCG_V1 711 /* idx can be of type enum memcg_stat_item or node_stat_item. */ 712 unsigned long memcg_page_state_local(struct mem_cgroup *memcg, int idx) 713 { 714 long x; 715 int i = memcg_stats_index(idx); 716 717 if (WARN_ONCE(BAD_STAT_IDX(i), "%s: missing stat item %d\n", __func__, idx)) 718 return 0; 719 720 x = READ_ONCE(memcg->vmstats->state_local[i]); 721 #ifdef CONFIG_SMP 722 if (x < 0) 723 x = 0; 724 #endif 725 return x; 726 } 727 #endif 728 729 static void mod_memcg_lruvec_state(struct lruvec *lruvec, 730 enum node_stat_item idx, 731 int val) 732 { 733 struct mem_cgroup_per_node *pn; 734 struct mem_cgroup *memcg; 735 int i = memcg_stats_index(idx); 736 int cpu; 737 738 if (WARN_ONCE(BAD_STAT_IDX(i), "%s: missing stat item %d\n", __func__, idx)) 739 return; 740 741 pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec); 742 memcg = pn->memcg; 743 744 cpu = get_cpu(); 745 746 /* Update memcg */ 747 this_cpu_add(memcg->vmstats_percpu->state[i], val); 748 749 /* Update lruvec */ 750 this_cpu_add(pn->lruvec_stats_percpu->state[i], val); 751 752 val = memcg_state_val_in_pages(idx, val); 753 memcg_rstat_updated(memcg, val, cpu); 754 trace_mod_memcg_lruvec_state(memcg, idx, val); 755 756 put_cpu(); 757 } 758 759 /** 760 * mod_lruvec_state - update lruvec memory statistics 761 * @lruvec: the lruvec 762 * @idx: the stat item 763 * @val: delta to add to the counter, can be negative 764 * 765 * The lruvec is the intersection of the NUMA node and a cgroup. This 766 * function updates the all three counters that are affected by a 767 * change of state at this level: per-node, per-cgroup, per-lruvec. 768 */ 769 void mod_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx, 770 int val) 771 { 772 /* Update node */ 773 mod_node_page_state(lruvec_pgdat(lruvec), idx, val); 774 775 /* Update memcg and lruvec */ 776 if (!mem_cgroup_disabled()) 777 mod_memcg_lruvec_state(lruvec, idx, val); 778 } 779 780 void lruvec_stat_mod_folio(struct folio *folio, enum node_stat_item idx, 781 int val) 782 { 783 struct mem_cgroup *memcg; 784 pg_data_t *pgdat = folio_pgdat(folio); 785 struct lruvec *lruvec; 786 787 rcu_read_lock(); 788 memcg = folio_memcg(folio); 789 /* Untracked pages have no memcg, no lruvec. Update only the node */ 790 if (!memcg) { 791 rcu_read_unlock(); 792 mod_node_page_state(pgdat, idx, val); 793 return; 794 } 795 796 lruvec = mem_cgroup_lruvec(memcg, pgdat); 797 mod_lruvec_state(lruvec, idx, val); 798 rcu_read_unlock(); 799 } 800 EXPORT_SYMBOL(lruvec_stat_mod_folio); 801 802 void mod_lruvec_kmem_state(void *p, enum node_stat_item idx, int val) 803 { 804 pg_data_t *pgdat = page_pgdat(virt_to_page(p)); 805 struct mem_cgroup *memcg; 806 struct lruvec *lruvec; 807 808 rcu_read_lock(); 809 memcg = mem_cgroup_from_slab_obj(p); 810 811 /* 812 * Untracked pages have no memcg, no lruvec. Update only the 813 * node. If we reparent the slab objects to the root memcg, 814 * when we free the slab object, we need to update the per-memcg 815 * vmstats to keep it correct for the root memcg. 816 */ 817 if (!memcg) { 818 mod_node_page_state(pgdat, idx, val); 819 } else { 820 lruvec = mem_cgroup_lruvec(memcg, pgdat); 821 mod_lruvec_state(lruvec, idx, val); 822 } 823 rcu_read_unlock(); 824 } 825 826 /** 827 * count_memcg_events - account VM events in a cgroup 828 * @memcg: the memory cgroup 829 * @idx: the event item 830 * @count: the number of events that occurred 831 */ 832 void count_memcg_events(struct mem_cgroup *memcg, enum vm_event_item idx, 833 unsigned long count) 834 { 835 int i = memcg_events_index(idx); 836 int cpu; 837 838 if (mem_cgroup_disabled()) 839 return; 840 841 if (WARN_ONCE(BAD_STAT_IDX(i), "%s: missing stat item %d\n", __func__, idx)) 842 return; 843 844 cpu = get_cpu(); 845 846 this_cpu_add(memcg->vmstats_percpu->events[i], count); 847 memcg_rstat_updated(memcg, count, cpu); 848 trace_count_memcg_events(memcg, idx, count); 849 850 put_cpu(); 851 } 852 853 unsigned long memcg_events(struct mem_cgroup *memcg, int event) 854 { 855 int i = memcg_events_index(event); 856 857 if (WARN_ONCE(BAD_STAT_IDX(i), "%s: missing stat item %d\n", __func__, event)) 858 return 0; 859 860 return READ_ONCE(memcg->vmstats->events[i]); 861 } 862 863 #ifdef CONFIG_MEMCG_V1 864 unsigned long memcg_events_local(struct mem_cgroup *memcg, int event) 865 { 866 int i = memcg_events_index(event); 867 868 if (WARN_ONCE(BAD_STAT_IDX(i), "%s: missing stat item %d\n", __func__, event)) 869 return 0; 870 871 return READ_ONCE(memcg->vmstats->events_local[i]); 872 } 873 #endif 874 875 struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p) 876 { 877 /* 878 * mm_update_next_owner() may clear mm->owner to NULL 879 * if it races with swapoff, page migration, etc. 880 * So this can be called with p == NULL. 881 */ 882 if (unlikely(!p)) 883 return NULL; 884 885 return mem_cgroup_from_css(task_css(p, memory_cgrp_id)); 886 } 887 EXPORT_SYMBOL(mem_cgroup_from_task); 888 889 static __always_inline struct mem_cgroup *active_memcg(void) 890 { 891 if (!in_task()) 892 return this_cpu_read(int_active_memcg); 893 else 894 return current->active_memcg; 895 } 896 897 /** 898 * get_mem_cgroup_from_mm: Obtain a reference on given mm_struct's memcg. 899 * @mm: mm from which memcg should be extracted. It can be NULL. 900 * 901 * Obtain a reference on mm->memcg and returns it if successful. If mm 902 * is NULL, then the memcg is chosen as follows: 903 * 1) The active memcg, if set. 904 * 2) current->mm->memcg, if available 905 * 3) root memcg 906 * If mem_cgroup is disabled, NULL is returned. 907 */ 908 struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm) 909 { 910 struct mem_cgroup *memcg; 911 912 if (mem_cgroup_disabled()) 913 return NULL; 914 915 /* 916 * Page cache insertions can happen without an 917 * actual mm context, e.g. during disk probing 918 * on boot, loopback IO, acct() writes etc. 919 * 920 * No need to css_get on root memcg as the reference 921 * counting is disabled on the root level in the 922 * cgroup core. See CSS_NO_REF. 923 */ 924 if (unlikely(!mm)) { 925 memcg = active_memcg(); 926 if (unlikely(memcg)) { 927 /* remote memcg must hold a ref */ 928 css_get(&memcg->css); 929 return memcg; 930 } 931 mm = current->mm; 932 if (unlikely(!mm)) 933 return root_mem_cgroup; 934 } 935 936 rcu_read_lock(); 937 do { 938 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner)); 939 if (unlikely(!memcg)) 940 memcg = root_mem_cgroup; 941 } while (!css_tryget(&memcg->css)); 942 rcu_read_unlock(); 943 return memcg; 944 } 945 EXPORT_SYMBOL(get_mem_cgroup_from_mm); 946 947 /** 948 * get_mem_cgroup_from_current - Obtain a reference on current task's memcg. 949 */ 950 struct mem_cgroup *get_mem_cgroup_from_current(void) 951 { 952 struct mem_cgroup *memcg; 953 954 if (mem_cgroup_disabled()) 955 return NULL; 956 957 again: 958 rcu_read_lock(); 959 memcg = mem_cgroup_from_task(current); 960 if (!css_tryget(&memcg->css)) { 961 rcu_read_unlock(); 962 goto again; 963 } 964 rcu_read_unlock(); 965 return memcg; 966 } 967 968 /** 969 * get_mem_cgroup_from_folio - Obtain a reference on a given folio's memcg. 970 * @folio: folio from which memcg should be extracted. 971 */ 972 struct mem_cgroup *get_mem_cgroup_from_folio(struct folio *folio) 973 { 974 struct mem_cgroup *memcg = folio_memcg(folio); 975 976 if (mem_cgroup_disabled()) 977 return NULL; 978 979 rcu_read_lock(); 980 if (!memcg || WARN_ON_ONCE(!css_tryget(&memcg->css))) 981 memcg = root_mem_cgroup; 982 rcu_read_unlock(); 983 return memcg; 984 } 985 986 /** 987 * mem_cgroup_iter - iterate over memory cgroup hierarchy 988 * @root: hierarchy root 989 * @prev: previously returned memcg, NULL on first invocation 990 * @reclaim: cookie for shared reclaim walks, NULL for full walks 991 * 992 * Returns references to children of the hierarchy below @root, or 993 * @root itself, or %NULL after a full round-trip. 994 * 995 * Caller must pass the return value in @prev on subsequent 996 * invocations for reference counting, or use mem_cgroup_iter_break() 997 * to cancel a hierarchy walk before the round-trip is complete. 998 * 999 * Reclaimers can specify a node in @reclaim to divide up the memcgs 1000 * in the hierarchy among all concurrent reclaimers operating on the 1001 * same node. 1002 */ 1003 struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root, 1004 struct mem_cgroup *prev, 1005 struct mem_cgroup_reclaim_cookie *reclaim) 1006 { 1007 struct mem_cgroup_reclaim_iter *iter; 1008 struct cgroup_subsys_state *css; 1009 struct mem_cgroup *pos; 1010 struct mem_cgroup *next; 1011 1012 if (mem_cgroup_disabled()) 1013 return NULL; 1014 1015 if (!root) 1016 root = root_mem_cgroup; 1017 1018 rcu_read_lock(); 1019 restart: 1020 next = NULL; 1021 1022 if (reclaim) { 1023 int gen; 1024 int nid = reclaim->pgdat->node_id; 1025 1026 iter = &root->nodeinfo[nid]->iter; 1027 gen = atomic_read(&iter->generation); 1028 1029 /* 1030 * On start, join the current reclaim iteration cycle. 1031 * Exit when a concurrent walker completes it. 1032 */ 1033 if (!prev) 1034 reclaim->generation = gen; 1035 else if (reclaim->generation != gen) 1036 goto out_unlock; 1037 1038 pos = READ_ONCE(iter->position); 1039 } else 1040 pos = prev; 1041 1042 css = pos ? &pos->css : NULL; 1043 1044 while ((css = css_next_descendant_pre(css, &root->css))) { 1045 /* 1046 * Verify the css and acquire a reference. The root 1047 * is provided by the caller, so we know it's alive 1048 * and kicking, and don't take an extra reference. 1049 */ 1050 if (css == &root->css || css_tryget(css)) 1051 break; 1052 } 1053 1054 next = mem_cgroup_from_css(css); 1055 1056 if (reclaim) { 1057 /* 1058 * The position could have already been updated by a competing 1059 * thread, so check that the value hasn't changed since we read 1060 * it to avoid reclaiming from the same cgroup twice. 1061 */ 1062 if (cmpxchg(&iter->position, pos, next) != pos) { 1063 if (css && css != &root->css) 1064 css_put(css); 1065 goto restart; 1066 } 1067 1068 if (!next) { 1069 atomic_inc(&iter->generation); 1070 1071 /* 1072 * Reclaimers share the hierarchy walk, and a 1073 * new one might jump in right at the end of 1074 * the hierarchy - make sure they see at least 1075 * one group and restart from the beginning. 1076 */ 1077 if (!prev) 1078 goto restart; 1079 } 1080 } 1081 1082 out_unlock: 1083 rcu_read_unlock(); 1084 if (prev && prev != root) 1085 css_put(&prev->css); 1086 1087 return next; 1088 } 1089 1090 /** 1091 * mem_cgroup_iter_break - abort a hierarchy walk prematurely 1092 * @root: hierarchy root 1093 * @prev: last visited hierarchy member as returned by mem_cgroup_iter() 1094 */ 1095 void mem_cgroup_iter_break(struct mem_cgroup *root, 1096 struct mem_cgroup *prev) 1097 { 1098 if (!root) 1099 root = root_mem_cgroup; 1100 if (prev && prev != root) 1101 css_put(&prev->css); 1102 } 1103 1104 static void __invalidate_reclaim_iterators(struct mem_cgroup *from, 1105 struct mem_cgroup *dead_memcg) 1106 { 1107 struct mem_cgroup_reclaim_iter *iter; 1108 struct mem_cgroup_per_node *mz; 1109 int nid; 1110 1111 for_each_node(nid) { 1112 mz = from->nodeinfo[nid]; 1113 iter = &mz->iter; 1114 cmpxchg(&iter->position, dead_memcg, NULL); 1115 } 1116 } 1117 1118 static void invalidate_reclaim_iterators(struct mem_cgroup *dead_memcg) 1119 { 1120 struct mem_cgroup *memcg = dead_memcg; 1121 struct mem_cgroup *last; 1122 1123 do { 1124 __invalidate_reclaim_iterators(memcg, dead_memcg); 1125 last = memcg; 1126 } while ((memcg = parent_mem_cgroup(memcg))); 1127 1128 /* 1129 * When cgroup1 non-hierarchy mode is used, 1130 * parent_mem_cgroup() does not walk all the way up to the 1131 * cgroup root (root_mem_cgroup). So we have to handle 1132 * dead_memcg from cgroup root separately. 1133 */ 1134 if (!mem_cgroup_is_root(last)) 1135 __invalidate_reclaim_iterators(root_mem_cgroup, 1136 dead_memcg); 1137 } 1138 1139 /** 1140 * mem_cgroup_scan_tasks - iterate over tasks of a memory cgroup hierarchy 1141 * @memcg: hierarchy root 1142 * @fn: function to call for each task 1143 * @arg: argument passed to @fn 1144 * 1145 * This function iterates over tasks attached to @memcg or to any of its 1146 * descendants and calls @fn for each task. If @fn returns a non-zero 1147 * value, the function breaks the iteration loop. Otherwise, it will iterate 1148 * over all tasks and return 0. 1149 * 1150 * This function must not be called for the root memory cgroup. 1151 */ 1152 void mem_cgroup_scan_tasks(struct mem_cgroup *memcg, 1153 int (*fn)(struct task_struct *, void *), void *arg) 1154 { 1155 struct mem_cgroup *iter; 1156 int ret = 0; 1157 1158 BUG_ON(mem_cgroup_is_root(memcg)); 1159 1160 for_each_mem_cgroup_tree(iter, memcg) { 1161 struct css_task_iter it; 1162 struct task_struct *task; 1163 1164 css_task_iter_start(&iter->css, CSS_TASK_ITER_PROCS, &it); 1165 while (!ret && (task = css_task_iter_next(&it))) { 1166 ret = fn(task, arg); 1167 /* Avoid potential softlockup warning */ 1168 cond_resched(); 1169 } 1170 css_task_iter_end(&it); 1171 if (ret) { 1172 mem_cgroup_iter_break(memcg, iter); 1173 break; 1174 } 1175 } 1176 } 1177 1178 #ifdef CONFIG_DEBUG_VM 1179 void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio) 1180 { 1181 struct mem_cgroup *memcg; 1182 1183 if (mem_cgroup_disabled()) 1184 return; 1185 1186 memcg = folio_memcg(folio); 1187 1188 if (!memcg) 1189 VM_BUG_ON_FOLIO(!mem_cgroup_is_root(lruvec_memcg(lruvec)), folio); 1190 else 1191 VM_BUG_ON_FOLIO(lruvec_memcg(lruvec) != memcg, folio); 1192 } 1193 #endif 1194 1195 /** 1196 * folio_lruvec_lock - Lock the lruvec for a folio. 1197 * @folio: Pointer to the folio. 1198 * 1199 * These functions are safe to use under any of the following conditions: 1200 * - folio locked 1201 * - folio_test_lru false 1202 * - folio frozen (refcount of 0) 1203 * 1204 * Return: The lruvec this folio is on with its lock held. 1205 */ 1206 struct lruvec *folio_lruvec_lock(struct folio *folio) 1207 { 1208 struct lruvec *lruvec = folio_lruvec(folio); 1209 1210 spin_lock(&lruvec->lru_lock); 1211 lruvec_memcg_debug(lruvec, folio); 1212 1213 return lruvec; 1214 } 1215 1216 /** 1217 * folio_lruvec_lock_irq - Lock the lruvec for a folio. 1218 * @folio: Pointer to the folio. 1219 * 1220 * These functions are safe to use under any of the following conditions: 1221 * - folio locked 1222 * - folio_test_lru false 1223 * - folio frozen (refcount of 0) 1224 * 1225 * Return: The lruvec this folio is on with its lock held and interrupts 1226 * disabled. 1227 */ 1228 struct lruvec *folio_lruvec_lock_irq(struct folio *folio) 1229 { 1230 struct lruvec *lruvec = folio_lruvec(folio); 1231 1232 spin_lock_irq(&lruvec->lru_lock); 1233 lruvec_memcg_debug(lruvec, folio); 1234 1235 return lruvec; 1236 } 1237 1238 /** 1239 * folio_lruvec_lock_irqsave - Lock the lruvec for a folio. 1240 * @folio: Pointer to the folio. 1241 * @flags: Pointer to irqsave flags. 1242 * 1243 * These functions are safe to use under any of the following conditions: 1244 * - folio locked 1245 * - folio_test_lru false 1246 * - folio frozen (refcount of 0) 1247 * 1248 * Return: The lruvec this folio is on with its lock held and interrupts 1249 * disabled. 1250 */ 1251 struct lruvec *folio_lruvec_lock_irqsave(struct folio *folio, 1252 unsigned long *flags) 1253 { 1254 struct lruvec *lruvec = folio_lruvec(folio); 1255 1256 spin_lock_irqsave(&lruvec->lru_lock, *flags); 1257 lruvec_memcg_debug(lruvec, folio); 1258 1259 return lruvec; 1260 } 1261 1262 /** 1263 * mem_cgroup_update_lru_size - account for adding or removing an lru page 1264 * @lruvec: mem_cgroup per zone lru vector 1265 * @lru: index of lru list the page is sitting on 1266 * @zid: zone id of the accounted pages 1267 * @nr_pages: positive when adding or negative when removing 1268 * 1269 * This function must be called under lru_lock, just before a page is added 1270 * to or just after a page is removed from an lru list. 1271 */ 1272 void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru, 1273 int zid, int nr_pages) 1274 { 1275 struct mem_cgroup_per_node *mz; 1276 unsigned long *lru_size; 1277 long size; 1278 1279 if (mem_cgroup_disabled()) 1280 return; 1281 1282 mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec); 1283 lru_size = &mz->lru_zone_size[zid][lru]; 1284 1285 if (nr_pages < 0) 1286 *lru_size += nr_pages; 1287 1288 size = *lru_size; 1289 if (WARN_ONCE(size < 0, 1290 "%s(%p, %d, %d): lru_size %ld\n", 1291 __func__, lruvec, lru, nr_pages, size)) { 1292 VM_BUG_ON(1); 1293 *lru_size = 0; 1294 } 1295 1296 if (nr_pages > 0) 1297 *lru_size += nr_pages; 1298 } 1299 1300 /** 1301 * mem_cgroup_margin - calculate chargeable space of a memory cgroup 1302 * @memcg: the memory cgroup 1303 * 1304 * Returns the maximum amount of memory @mem can be charged with, in 1305 * pages. 1306 */ 1307 static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg) 1308 { 1309 unsigned long margin = 0; 1310 unsigned long count; 1311 unsigned long limit; 1312 1313 count = page_counter_read(&memcg->memory); 1314 limit = READ_ONCE(memcg->memory.max); 1315 if (count < limit) 1316 margin = limit - count; 1317 1318 if (do_memsw_account()) { 1319 count = page_counter_read(&memcg->memsw); 1320 limit = READ_ONCE(memcg->memsw.max); 1321 if (count < limit) 1322 margin = min(margin, limit - count); 1323 else 1324 margin = 0; 1325 } 1326 1327 return margin; 1328 } 1329 1330 struct memory_stat { 1331 const char *name; 1332 unsigned int idx; 1333 }; 1334 1335 static const struct memory_stat memory_stats[] = { 1336 { "anon", NR_ANON_MAPPED }, 1337 { "file", NR_FILE_PAGES }, 1338 { "kernel", MEMCG_KMEM }, 1339 { "kernel_stack", NR_KERNEL_STACK_KB }, 1340 { "pagetables", NR_PAGETABLE }, 1341 { "sec_pagetables", NR_SECONDARY_PAGETABLE }, 1342 { "percpu", MEMCG_PERCPU_B }, 1343 { "sock", MEMCG_SOCK }, 1344 { "vmalloc", MEMCG_VMALLOC }, 1345 { "shmem", NR_SHMEM }, 1346 #ifdef CONFIG_ZSWAP 1347 { "zswap", MEMCG_ZSWAP_B }, 1348 { "zswapped", MEMCG_ZSWAPPED }, 1349 #endif 1350 { "file_mapped", NR_FILE_MAPPED }, 1351 { "file_dirty", NR_FILE_DIRTY }, 1352 { "file_writeback", NR_WRITEBACK }, 1353 #ifdef CONFIG_SWAP 1354 { "swapcached", NR_SWAPCACHE }, 1355 #endif 1356 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 1357 { "anon_thp", NR_ANON_THPS }, 1358 { "file_thp", NR_FILE_THPS }, 1359 { "shmem_thp", NR_SHMEM_THPS }, 1360 #endif 1361 { "inactive_anon", NR_INACTIVE_ANON }, 1362 { "active_anon", NR_ACTIVE_ANON }, 1363 { "inactive_file", NR_INACTIVE_FILE }, 1364 { "active_file", NR_ACTIVE_FILE }, 1365 { "unevictable", NR_UNEVICTABLE }, 1366 { "slab_reclaimable", NR_SLAB_RECLAIMABLE_B }, 1367 { "slab_unreclaimable", NR_SLAB_UNRECLAIMABLE_B }, 1368 #ifdef CONFIG_HUGETLB_PAGE 1369 { "hugetlb", NR_HUGETLB }, 1370 #endif 1371 1372 /* The memory events */ 1373 { "workingset_refault_anon", WORKINGSET_REFAULT_ANON }, 1374 { "workingset_refault_file", WORKINGSET_REFAULT_FILE }, 1375 { "workingset_activate_anon", WORKINGSET_ACTIVATE_ANON }, 1376 { "workingset_activate_file", WORKINGSET_ACTIVATE_FILE }, 1377 { "workingset_restore_anon", WORKINGSET_RESTORE_ANON }, 1378 { "workingset_restore_file", WORKINGSET_RESTORE_FILE }, 1379 { "workingset_nodereclaim", WORKINGSET_NODERECLAIM }, 1380 1381 { "pgdemote_kswapd", PGDEMOTE_KSWAPD }, 1382 { "pgdemote_direct", PGDEMOTE_DIRECT }, 1383 { "pgdemote_khugepaged", PGDEMOTE_KHUGEPAGED }, 1384 { "pgdemote_proactive", PGDEMOTE_PROACTIVE }, 1385 #ifdef CONFIG_NUMA_BALANCING 1386 { "pgpromote_success", PGPROMOTE_SUCCESS }, 1387 #endif 1388 }; 1389 1390 /* The actual unit of the state item, not the same as the output unit */ 1391 static int memcg_page_state_unit(int item) 1392 { 1393 switch (item) { 1394 case MEMCG_PERCPU_B: 1395 case MEMCG_ZSWAP_B: 1396 case NR_SLAB_RECLAIMABLE_B: 1397 case NR_SLAB_UNRECLAIMABLE_B: 1398 return 1; 1399 case NR_KERNEL_STACK_KB: 1400 return SZ_1K; 1401 default: 1402 return PAGE_SIZE; 1403 } 1404 } 1405 1406 /* Translate stat items to the correct unit for memory.stat output */ 1407 static int memcg_page_state_output_unit(int item) 1408 { 1409 /* 1410 * Workingset state is actually in pages, but we export it to userspace 1411 * as a scalar count of events, so special case it here. 1412 * 1413 * Demotion and promotion activities are exported in pages, consistent 1414 * with their global counterparts. 1415 */ 1416 switch (item) { 1417 case WORKINGSET_REFAULT_ANON: 1418 case WORKINGSET_REFAULT_FILE: 1419 case WORKINGSET_ACTIVATE_ANON: 1420 case WORKINGSET_ACTIVATE_FILE: 1421 case WORKINGSET_RESTORE_ANON: 1422 case WORKINGSET_RESTORE_FILE: 1423 case WORKINGSET_NODERECLAIM: 1424 case PGDEMOTE_KSWAPD: 1425 case PGDEMOTE_DIRECT: 1426 case PGDEMOTE_KHUGEPAGED: 1427 case PGDEMOTE_PROACTIVE: 1428 #ifdef CONFIG_NUMA_BALANCING 1429 case PGPROMOTE_SUCCESS: 1430 #endif 1431 return 1; 1432 default: 1433 return memcg_page_state_unit(item); 1434 } 1435 } 1436 1437 unsigned long memcg_page_state_output(struct mem_cgroup *memcg, int item) 1438 { 1439 return memcg_page_state(memcg, item) * 1440 memcg_page_state_output_unit(item); 1441 } 1442 1443 #ifdef CONFIG_MEMCG_V1 1444 unsigned long memcg_page_state_local_output(struct mem_cgroup *memcg, int item) 1445 { 1446 return memcg_page_state_local(memcg, item) * 1447 memcg_page_state_output_unit(item); 1448 } 1449 #endif 1450 1451 #ifdef CONFIG_HUGETLB_PAGE 1452 static bool memcg_accounts_hugetlb(void) 1453 { 1454 return cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_HUGETLB_ACCOUNTING; 1455 } 1456 #else /* CONFIG_HUGETLB_PAGE */ 1457 static bool memcg_accounts_hugetlb(void) 1458 { 1459 return false; 1460 } 1461 #endif /* CONFIG_HUGETLB_PAGE */ 1462 1463 static void memcg_stat_format(struct mem_cgroup *memcg, struct seq_buf *s) 1464 { 1465 int i; 1466 1467 /* 1468 * Provide statistics on the state of the memory subsystem as 1469 * well as cumulative event counters that show past behavior. 1470 * 1471 * This list is ordered following a combination of these gradients: 1472 * 1) generic big picture -> specifics and details 1473 * 2) reflecting userspace activity -> reflecting kernel heuristics 1474 * 1475 * Current memory state: 1476 */ 1477 mem_cgroup_flush_stats(memcg); 1478 1479 for (i = 0; i < ARRAY_SIZE(memory_stats); i++) { 1480 u64 size; 1481 1482 #ifdef CONFIG_HUGETLB_PAGE 1483 if (unlikely(memory_stats[i].idx == NR_HUGETLB) && 1484 !memcg_accounts_hugetlb()) 1485 continue; 1486 #endif 1487 size = memcg_page_state_output(memcg, memory_stats[i].idx); 1488 seq_buf_printf(s, "%s %llu\n", memory_stats[i].name, size); 1489 1490 if (unlikely(memory_stats[i].idx == NR_SLAB_UNRECLAIMABLE_B)) { 1491 size += memcg_page_state_output(memcg, 1492 NR_SLAB_RECLAIMABLE_B); 1493 seq_buf_printf(s, "slab %llu\n", size); 1494 } 1495 } 1496 1497 /* Accumulated memory events */ 1498 seq_buf_printf(s, "pgscan %lu\n", 1499 memcg_events(memcg, PGSCAN_KSWAPD) + 1500 memcg_events(memcg, PGSCAN_DIRECT) + 1501 memcg_events(memcg, PGSCAN_PROACTIVE) + 1502 memcg_events(memcg, PGSCAN_KHUGEPAGED)); 1503 seq_buf_printf(s, "pgsteal %lu\n", 1504 memcg_events(memcg, PGSTEAL_KSWAPD) + 1505 memcg_events(memcg, PGSTEAL_DIRECT) + 1506 memcg_events(memcg, PGSTEAL_PROACTIVE) + 1507 memcg_events(memcg, PGSTEAL_KHUGEPAGED)); 1508 1509 for (i = 0; i < ARRAY_SIZE(memcg_vm_event_stat); i++) { 1510 #ifdef CONFIG_MEMCG_V1 1511 if (memcg_vm_event_stat[i] == PGPGIN || 1512 memcg_vm_event_stat[i] == PGPGOUT) 1513 continue; 1514 #endif 1515 seq_buf_printf(s, "%s %lu\n", 1516 vm_event_name(memcg_vm_event_stat[i]), 1517 memcg_events(memcg, memcg_vm_event_stat[i])); 1518 } 1519 } 1520 1521 static void memory_stat_format(struct mem_cgroup *memcg, struct seq_buf *s) 1522 { 1523 if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) 1524 memcg_stat_format(memcg, s); 1525 else 1526 memcg1_stat_format(memcg, s); 1527 if (seq_buf_has_overflowed(s)) 1528 pr_warn("%s: Warning, stat buffer overflow, please report\n", __func__); 1529 } 1530 1531 /** 1532 * mem_cgroup_print_oom_context: Print OOM information relevant to 1533 * memory controller. 1534 * @memcg: The memory cgroup that went over limit 1535 * @p: Task that is going to be killed 1536 * 1537 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is 1538 * enabled 1539 */ 1540 void mem_cgroup_print_oom_context(struct mem_cgroup *memcg, struct task_struct *p) 1541 { 1542 rcu_read_lock(); 1543 1544 if (memcg) { 1545 pr_cont(",oom_memcg="); 1546 pr_cont_cgroup_path(memcg->css.cgroup); 1547 } else 1548 pr_cont(",global_oom"); 1549 if (p) { 1550 pr_cont(",task_memcg="); 1551 pr_cont_cgroup_path(task_cgroup(p, memory_cgrp_id)); 1552 } 1553 rcu_read_unlock(); 1554 } 1555 1556 /** 1557 * mem_cgroup_print_oom_meminfo: Print OOM memory information relevant to 1558 * memory controller. 1559 * @memcg: The memory cgroup that went over limit 1560 */ 1561 void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg) 1562 { 1563 /* Use static buffer, for the caller is holding oom_lock. */ 1564 static char buf[SEQ_BUF_SIZE]; 1565 struct seq_buf s; 1566 unsigned long memory_failcnt; 1567 1568 lockdep_assert_held(&oom_lock); 1569 1570 if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) 1571 memory_failcnt = atomic_long_read(&memcg->memory_events[MEMCG_MAX]); 1572 else 1573 memory_failcnt = memcg->memory.failcnt; 1574 1575 pr_info("memory: usage %llukB, limit %llukB, failcnt %lu\n", 1576 K((u64)page_counter_read(&memcg->memory)), 1577 K((u64)READ_ONCE(memcg->memory.max)), memory_failcnt); 1578 if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) 1579 pr_info("swap: usage %llukB, limit %llukB, failcnt %lu\n", 1580 K((u64)page_counter_read(&memcg->swap)), 1581 K((u64)READ_ONCE(memcg->swap.max)), 1582 atomic_long_read(&memcg->memory_events[MEMCG_SWAP_MAX])); 1583 #ifdef CONFIG_MEMCG_V1 1584 else { 1585 pr_info("memory+swap: usage %llukB, limit %llukB, failcnt %lu\n", 1586 K((u64)page_counter_read(&memcg->memsw)), 1587 K((u64)memcg->memsw.max), memcg->memsw.failcnt); 1588 pr_info("kmem: usage %llukB, limit %llukB, failcnt %lu\n", 1589 K((u64)page_counter_read(&memcg->kmem)), 1590 K((u64)memcg->kmem.max), memcg->kmem.failcnt); 1591 } 1592 #endif 1593 1594 pr_info("Memory cgroup stats for "); 1595 pr_cont_cgroup_path(memcg->css.cgroup); 1596 pr_cont(":"); 1597 seq_buf_init(&s, buf, SEQ_BUF_SIZE); 1598 memory_stat_format(memcg, &s); 1599 seq_buf_do_printk(&s, KERN_INFO); 1600 } 1601 1602 /* 1603 * Return the memory (and swap, if configured) limit for a memcg. 1604 */ 1605 unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg) 1606 { 1607 unsigned long max = READ_ONCE(memcg->memory.max); 1608 1609 if (do_memsw_account()) { 1610 if (mem_cgroup_swappiness(memcg)) { 1611 /* Calculate swap excess capacity from memsw limit */ 1612 unsigned long swap = READ_ONCE(memcg->memsw.max) - max; 1613 1614 max += min(swap, (unsigned long)total_swap_pages); 1615 } 1616 } else { 1617 if (mem_cgroup_swappiness(memcg)) 1618 max += min(READ_ONCE(memcg->swap.max), 1619 (unsigned long)total_swap_pages); 1620 } 1621 return max; 1622 } 1623 1624 unsigned long mem_cgroup_size(struct mem_cgroup *memcg) 1625 { 1626 return page_counter_read(&memcg->memory); 1627 } 1628 1629 void __memcg_memory_event(struct mem_cgroup *memcg, 1630 enum memcg_memory_event event, bool allow_spinning) 1631 { 1632 bool swap_event = event == MEMCG_SWAP_HIGH || event == MEMCG_SWAP_MAX || 1633 event == MEMCG_SWAP_FAIL; 1634 1635 /* For now only MEMCG_MAX can happen with !allow_spinning context. */ 1636 VM_WARN_ON_ONCE(!allow_spinning && event != MEMCG_MAX); 1637 1638 atomic_long_inc(&memcg->memory_events_local[event]); 1639 if (!swap_event && allow_spinning) 1640 cgroup_file_notify(&memcg->events_local_file); 1641 1642 do { 1643 atomic_long_inc(&memcg->memory_events[event]); 1644 if (allow_spinning) { 1645 if (swap_event) 1646 cgroup_file_notify(&memcg->swap_events_file); 1647 else 1648 cgroup_file_notify(&memcg->events_file); 1649 } 1650 1651 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) 1652 break; 1653 if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_LOCAL_EVENTS) 1654 break; 1655 } while ((memcg = parent_mem_cgroup(memcg)) && 1656 !mem_cgroup_is_root(memcg)); 1657 } 1658 EXPORT_SYMBOL_GPL(__memcg_memory_event); 1659 1660 static bool mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask, 1661 int order) 1662 { 1663 struct oom_control oc = { 1664 .zonelist = NULL, 1665 .nodemask = NULL, 1666 .memcg = memcg, 1667 .gfp_mask = gfp_mask, 1668 .order = order, 1669 }; 1670 bool ret = true; 1671 1672 if (mutex_lock_killable(&oom_lock)) 1673 return true; 1674 1675 if (mem_cgroup_margin(memcg) >= (1 << order)) 1676 goto unlock; 1677 1678 /* 1679 * A few threads which were not waiting at mutex_lock_killable() can 1680 * fail to bail out. Therefore, check again after holding oom_lock. 1681 */ 1682 ret = out_of_memory(&oc); 1683 1684 unlock: 1685 mutex_unlock(&oom_lock); 1686 return ret; 1687 } 1688 1689 /* 1690 * Returns true if successfully killed one or more processes. Though in some 1691 * corner cases it can return true even without killing any process. 1692 */ 1693 static bool mem_cgroup_oom(struct mem_cgroup *memcg, gfp_t mask, int order) 1694 { 1695 bool locked, ret; 1696 1697 if (order > PAGE_ALLOC_COSTLY_ORDER) 1698 return false; 1699 1700 memcg_memory_event(memcg, MEMCG_OOM); 1701 1702 if (!memcg1_oom_prepare(memcg, &locked)) 1703 return false; 1704 1705 ret = mem_cgroup_out_of_memory(memcg, mask, order); 1706 1707 memcg1_oom_finish(memcg, locked); 1708 1709 return ret; 1710 } 1711 1712 /** 1713 * mem_cgroup_get_oom_group - get a memory cgroup to clean up after OOM 1714 * @victim: task to be killed by the OOM killer 1715 * @oom_domain: memcg in case of memcg OOM, NULL in case of system-wide OOM 1716 * 1717 * Returns a pointer to a memory cgroup, which has to be cleaned up 1718 * by killing all belonging OOM-killable tasks. 1719 * 1720 * Caller has to call mem_cgroup_put() on the returned non-NULL memcg. 1721 */ 1722 struct mem_cgroup *mem_cgroup_get_oom_group(struct task_struct *victim, 1723 struct mem_cgroup *oom_domain) 1724 { 1725 struct mem_cgroup *oom_group = NULL; 1726 struct mem_cgroup *memcg; 1727 1728 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) 1729 return NULL; 1730 1731 if (!oom_domain) 1732 oom_domain = root_mem_cgroup; 1733 1734 rcu_read_lock(); 1735 1736 memcg = mem_cgroup_from_task(victim); 1737 if (mem_cgroup_is_root(memcg)) 1738 goto out; 1739 1740 /* 1741 * If the victim task has been asynchronously moved to a different 1742 * memory cgroup, we might end up killing tasks outside oom_domain. 1743 * In this case it's better to ignore memory.group.oom. 1744 */ 1745 if (unlikely(!mem_cgroup_is_descendant(memcg, oom_domain))) 1746 goto out; 1747 1748 /* 1749 * Traverse the memory cgroup hierarchy from the victim task's 1750 * cgroup up to the OOMing cgroup (or root) to find the 1751 * highest-level memory cgroup with oom.group set. 1752 */ 1753 for (; memcg; memcg = parent_mem_cgroup(memcg)) { 1754 if (READ_ONCE(memcg->oom_group)) 1755 oom_group = memcg; 1756 1757 if (memcg == oom_domain) 1758 break; 1759 } 1760 1761 if (oom_group) 1762 css_get(&oom_group->css); 1763 out: 1764 rcu_read_unlock(); 1765 1766 return oom_group; 1767 } 1768 1769 void mem_cgroup_print_oom_group(struct mem_cgroup *memcg) 1770 { 1771 pr_info("Tasks in "); 1772 pr_cont_cgroup_path(memcg->css.cgroup); 1773 pr_cont(" are going to be killed due to memory.oom.group set\n"); 1774 } 1775 1776 /* 1777 * The value of NR_MEMCG_STOCK is selected to keep the cached memcgs and their 1778 * nr_pages in a single cacheline. This may change in future. 1779 */ 1780 #define NR_MEMCG_STOCK 7 1781 #define FLUSHING_CACHED_CHARGE 0 1782 struct memcg_stock_pcp { 1783 local_trylock_t lock; 1784 uint8_t nr_pages[NR_MEMCG_STOCK]; 1785 struct mem_cgroup *cached[NR_MEMCG_STOCK]; 1786 1787 struct work_struct work; 1788 unsigned long flags; 1789 }; 1790 1791 static DEFINE_PER_CPU_ALIGNED(struct memcg_stock_pcp, memcg_stock) = { 1792 .lock = INIT_LOCAL_TRYLOCK(lock), 1793 }; 1794 1795 struct obj_stock_pcp { 1796 local_trylock_t lock; 1797 unsigned int nr_bytes; 1798 struct obj_cgroup *cached_objcg; 1799 struct pglist_data *cached_pgdat; 1800 int nr_slab_reclaimable_b; 1801 int nr_slab_unreclaimable_b; 1802 1803 struct work_struct work; 1804 unsigned long flags; 1805 }; 1806 1807 static DEFINE_PER_CPU_ALIGNED(struct obj_stock_pcp, obj_stock) = { 1808 .lock = INIT_LOCAL_TRYLOCK(lock), 1809 }; 1810 1811 static DEFINE_MUTEX(percpu_charge_mutex); 1812 1813 static void drain_obj_stock(struct obj_stock_pcp *stock); 1814 static bool obj_stock_flush_required(struct obj_stock_pcp *stock, 1815 struct mem_cgroup *root_memcg); 1816 1817 /** 1818 * consume_stock: Try to consume stocked charge on this cpu. 1819 * @memcg: memcg to consume from. 1820 * @nr_pages: how many pages to charge. 1821 * 1822 * Consume the cached charge if enough nr_pages are present otherwise return 1823 * failure. Also return failure for charge request larger than 1824 * MEMCG_CHARGE_BATCH or if the local lock is already taken. 1825 * 1826 * returns true if successful, false otherwise. 1827 */ 1828 static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages) 1829 { 1830 struct memcg_stock_pcp *stock; 1831 uint8_t stock_pages; 1832 bool ret = false; 1833 int i; 1834 1835 if (nr_pages > MEMCG_CHARGE_BATCH || 1836 !local_trylock(&memcg_stock.lock)) 1837 return ret; 1838 1839 stock = this_cpu_ptr(&memcg_stock); 1840 1841 for (i = 0; i < NR_MEMCG_STOCK; ++i) { 1842 if (memcg != READ_ONCE(stock->cached[i])) 1843 continue; 1844 1845 stock_pages = READ_ONCE(stock->nr_pages[i]); 1846 if (stock_pages >= nr_pages) { 1847 WRITE_ONCE(stock->nr_pages[i], stock_pages - nr_pages); 1848 ret = true; 1849 } 1850 break; 1851 } 1852 1853 local_unlock(&memcg_stock.lock); 1854 1855 return ret; 1856 } 1857 1858 static void memcg_uncharge(struct mem_cgroup *memcg, unsigned int nr_pages) 1859 { 1860 page_counter_uncharge(&memcg->memory, nr_pages); 1861 if (do_memsw_account()) 1862 page_counter_uncharge(&memcg->memsw, nr_pages); 1863 } 1864 1865 /* 1866 * Returns stocks cached in percpu and reset cached information. 1867 */ 1868 static void drain_stock(struct memcg_stock_pcp *stock, int i) 1869 { 1870 struct mem_cgroup *old = READ_ONCE(stock->cached[i]); 1871 uint8_t stock_pages; 1872 1873 if (!old) 1874 return; 1875 1876 stock_pages = READ_ONCE(stock->nr_pages[i]); 1877 if (stock_pages) { 1878 memcg_uncharge(old, stock_pages); 1879 WRITE_ONCE(stock->nr_pages[i], 0); 1880 } 1881 1882 css_put(&old->css); 1883 WRITE_ONCE(stock->cached[i], NULL); 1884 } 1885 1886 static void drain_stock_fully(struct memcg_stock_pcp *stock) 1887 { 1888 int i; 1889 1890 for (i = 0; i < NR_MEMCG_STOCK; ++i) 1891 drain_stock(stock, i); 1892 } 1893 1894 static void drain_local_memcg_stock(struct work_struct *dummy) 1895 { 1896 struct memcg_stock_pcp *stock; 1897 1898 if (WARN_ONCE(!in_task(), "drain in non-task context")) 1899 return; 1900 1901 local_lock(&memcg_stock.lock); 1902 1903 stock = this_cpu_ptr(&memcg_stock); 1904 drain_stock_fully(stock); 1905 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags); 1906 1907 local_unlock(&memcg_stock.lock); 1908 } 1909 1910 static void drain_local_obj_stock(struct work_struct *dummy) 1911 { 1912 struct obj_stock_pcp *stock; 1913 1914 if (WARN_ONCE(!in_task(), "drain in non-task context")) 1915 return; 1916 1917 local_lock(&obj_stock.lock); 1918 1919 stock = this_cpu_ptr(&obj_stock); 1920 drain_obj_stock(stock); 1921 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags); 1922 1923 local_unlock(&obj_stock.lock); 1924 } 1925 1926 static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages) 1927 { 1928 struct memcg_stock_pcp *stock; 1929 struct mem_cgroup *cached; 1930 uint8_t stock_pages; 1931 bool success = false; 1932 int empty_slot = -1; 1933 int i; 1934 1935 /* 1936 * For now limit MEMCG_CHARGE_BATCH to 127 and less. In future if we 1937 * decide to increase it more than 127 then we will need more careful 1938 * handling of nr_pages[] in struct memcg_stock_pcp. 1939 */ 1940 BUILD_BUG_ON(MEMCG_CHARGE_BATCH > S8_MAX); 1941 1942 VM_WARN_ON_ONCE(mem_cgroup_is_root(memcg)); 1943 1944 if (nr_pages > MEMCG_CHARGE_BATCH || 1945 !local_trylock(&memcg_stock.lock)) { 1946 /* 1947 * In case of larger than batch refill or unlikely failure to 1948 * lock the percpu memcg_stock.lock, uncharge memcg directly. 1949 */ 1950 memcg_uncharge(memcg, nr_pages); 1951 return; 1952 } 1953 1954 stock = this_cpu_ptr(&memcg_stock); 1955 for (i = 0; i < NR_MEMCG_STOCK; ++i) { 1956 cached = READ_ONCE(stock->cached[i]); 1957 if (!cached && empty_slot == -1) 1958 empty_slot = i; 1959 if (memcg == READ_ONCE(stock->cached[i])) { 1960 stock_pages = READ_ONCE(stock->nr_pages[i]) + nr_pages; 1961 WRITE_ONCE(stock->nr_pages[i], stock_pages); 1962 if (stock_pages > MEMCG_CHARGE_BATCH) 1963 drain_stock(stock, i); 1964 success = true; 1965 break; 1966 } 1967 } 1968 1969 if (!success) { 1970 i = empty_slot; 1971 if (i == -1) { 1972 i = get_random_u32_below(NR_MEMCG_STOCK); 1973 drain_stock(stock, i); 1974 } 1975 css_get(&memcg->css); 1976 WRITE_ONCE(stock->cached[i], memcg); 1977 WRITE_ONCE(stock->nr_pages[i], nr_pages); 1978 } 1979 1980 local_unlock(&memcg_stock.lock); 1981 } 1982 1983 static bool is_memcg_drain_needed(struct memcg_stock_pcp *stock, 1984 struct mem_cgroup *root_memcg) 1985 { 1986 struct mem_cgroup *memcg; 1987 bool flush = false; 1988 int i; 1989 1990 rcu_read_lock(); 1991 for (i = 0; i < NR_MEMCG_STOCK; ++i) { 1992 memcg = READ_ONCE(stock->cached[i]); 1993 if (!memcg) 1994 continue; 1995 1996 if (READ_ONCE(stock->nr_pages[i]) && 1997 mem_cgroup_is_descendant(memcg, root_memcg)) { 1998 flush = true; 1999 break; 2000 } 2001 } 2002 rcu_read_unlock(); 2003 return flush; 2004 } 2005 2006 /* 2007 * Drains all per-CPU charge caches for given root_memcg resp. subtree 2008 * of the hierarchy under it. 2009 */ 2010 void drain_all_stock(struct mem_cgroup *root_memcg) 2011 { 2012 int cpu, curcpu; 2013 2014 /* If someone's already draining, avoid adding running more workers. */ 2015 if (!mutex_trylock(&percpu_charge_mutex)) 2016 return; 2017 /* 2018 * Notify other cpus that system-wide "drain" is running 2019 * We do not care about races with the cpu hotplug because cpu down 2020 * as well as workers from this path always operate on the local 2021 * per-cpu data. CPU up doesn't touch memcg_stock at all. 2022 */ 2023 migrate_disable(); 2024 curcpu = smp_processor_id(); 2025 for_each_online_cpu(cpu) { 2026 struct memcg_stock_pcp *memcg_st = &per_cpu(memcg_stock, cpu); 2027 struct obj_stock_pcp *obj_st = &per_cpu(obj_stock, cpu); 2028 2029 if (!test_bit(FLUSHING_CACHED_CHARGE, &memcg_st->flags) && 2030 is_memcg_drain_needed(memcg_st, root_memcg) && 2031 !test_and_set_bit(FLUSHING_CACHED_CHARGE, 2032 &memcg_st->flags)) { 2033 if (cpu == curcpu) 2034 drain_local_memcg_stock(&memcg_st->work); 2035 else if (!cpu_is_isolated(cpu)) 2036 schedule_work_on(cpu, &memcg_st->work); 2037 } 2038 2039 if (!test_bit(FLUSHING_CACHED_CHARGE, &obj_st->flags) && 2040 obj_stock_flush_required(obj_st, root_memcg) && 2041 !test_and_set_bit(FLUSHING_CACHED_CHARGE, 2042 &obj_st->flags)) { 2043 if (cpu == curcpu) 2044 drain_local_obj_stock(&obj_st->work); 2045 else if (!cpu_is_isolated(cpu)) 2046 schedule_work_on(cpu, &obj_st->work); 2047 } 2048 } 2049 migrate_enable(); 2050 mutex_unlock(&percpu_charge_mutex); 2051 } 2052 2053 static int memcg_hotplug_cpu_dead(unsigned int cpu) 2054 { 2055 /* no need for the local lock */ 2056 drain_obj_stock(&per_cpu(obj_stock, cpu)); 2057 drain_stock_fully(&per_cpu(memcg_stock, cpu)); 2058 2059 return 0; 2060 } 2061 2062 static unsigned long reclaim_high(struct mem_cgroup *memcg, 2063 unsigned int nr_pages, 2064 gfp_t gfp_mask) 2065 { 2066 unsigned long nr_reclaimed = 0; 2067 2068 do { 2069 unsigned long pflags; 2070 2071 if (page_counter_read(&memcg->memory) <= 2072 READ_ONCE(memcg->memory.high)) 2073 continue; 2074 2075 memcg_memory_event(memcg, MEMCG_HIGH); 2076 2077 psi_memstall_enter(&pflags); 2078 nr_reclaimed += try_to_free_mem_cgroup_pages(memcg, nr_pages, 2079 gfp_mask, 2080 MEMCG_RECLAIM_MAY_SWAP, 2081 NULL); 2082 psi_memstall_leave(&pflags); 2083 } while ((memcg = parent_mem_cgroup(memcg)) && 2084 !mem_cgroup_is_root(memcg)); 2085 2086 return nr_reclaimed; 2087 } 2088 2089 static void high_work_func(struct work_struct *work) 2090 { 2091 struct mem_cgroup *memcg; 2092 2093 memcg = container_of(work, struct mem_cgroup, high_work); 2094 reclaim_high(memcg, MEMCG_CHARGE_BATCH, GFP_KERNEL); 2095 } 2096 2097 /* 2098 * Clamp the maximum sleep time per allocation batch to 2 seconds. This is 2099 * enough to still cause a significant slowdown in most cases, while still 2100 * allowing diagnostics and tracing to proceed without becoming stuck. 2101 */ 2102 #define MEMCG_MAX_HIGH_DELAY_JIFFIES (2UL*HZ) 2103 2104 /* 2105 * When calculating the delay, we use these either side of the exponentiation to 2106 * maintain precision and scale to a reasonable number of jiffies (see the table 2107 * below. 2108 * 2109 * - MEMCG_DELAY_PRECISION_SHIFT: Extra precision bits while translating the 2110 * overage ratio to a delay. 2111 * - MEMCG_DELAY_SCALING_SHIFT: The number of bits to scale down the 2112 * proposed penalty in order to reduce to a reasonable number of jiffies, and 2113 * to produce a reasonable delay curve. 2114 * 2115 * MEMCG_DELAY_SCALING_SHIFT just happens to be a number that produces a 2116 * reasonable delay curve compared to precision-adjusted overage, not 2117 * penalising heavily at first, but still making sure that growth beyond the 2118 * limit penalises misbehaviour cgroups by slowing them down exponentially. For 2119 * example, with a high of 100 megabytes: 2120 * 2121 * +-------+------------------------+ 2122 * | usage | time to allocate in ms | 2123 * +-------+------------------------+ 2124 * | 100M | 0 | 2125 * | 101M | 6 | 2126 * | 102M | 25 | 2127 * | 103M | 57 | 2128 * | 104M | 102 | 2129 * | 105M | 159 | 2130 * | 106M | 230 | 2131 * | 107M | 313 | 2132 * | 108M | 409 | 2133 * | 109M | 518 | 2134 * | 110M | 639 | 2135 * | 111M | 774 | 2136 * | 112M | 921 | 2137 * | 113M | 1081 | 2138 * | 114M | 1254 | 2139 * | 115M | 1439 | 2140 * | 116M | 1638 | 2141 * | 117M | 1849 | 2142 * | 118M | 2000 | 2143 * | 119M | 2000 | 2144 * | 120M | 2000 | 2145 * +-------+------------------------+ 2146 */ 2147 #define MEMCG_DELAY_PRECISION_SHIFT 20 2148 #define MEMCG_DELAY_SCALING_SHIFT 14 2149 2150 static u64 calculate_overage(unsigned long usage, unsigned long high) 2151 { 2152 u64 overage; 2153 2154 if (usage <= high) 2155 return 0; 2156 2157 /* 2158 * Prevent division by 0 in overage calculation by acting as if 2159 * it was a threshold of 1 page 2160 */ 2161 high = max(high, 1UL); 2162 2163 overage = usage - high; 2164 overage <<= MEMCG_DELAY_PRECISION_SHIFT; 2165 return div64_u64(overage, high); 2166 } 2167 2168 static u64 mem_find_max_overage(struct mem_cgroup *memcg) 2169 { 2170 u64 overage, max_overage = 0; 2171 2172 do { 2173 overage = calculate_overage(page_counter_read(&memcg->memory), 2174 READ_ONCE(memcg->memory.high)); 2175 max_overage = max(overage, max_overage); 2176 } while ((memcg = parent_mem_cgroup(memcg)) && 2177 !mem_cgroup_is_root(memcg)); 2178 2179 return max_overage; 2180 } 2181 2182 static u64 swap_find_max_overage(struct mem_cgroup *memcg) 2183 { 2184 u64 overage, max_overage = 0; 2185 2186 do { 2187 overage = calculate_overage(page_counter_read(&memcg->swap), 2188 READ_ONCE(memcg->swap.high)); 2189 if (overage) 2190 memcg_memory_event(memcg, MEMCG_SWAP_HIGH); 2191 max_overage = max(overage, max_overage); 2192 } while ((memcg = parent_mem_cgroup(memcg)) && 2193 !mem_cgroup_is_root(memcg)); 2194 2195 return max_overage; 2196 } 2197 2198 /* 2199 * Get the number of jiffies that we should penalise a mischievous cgroup which 2200 * is exceeding its memory.high by checking both it and its ancestors. 2201 */ 2202 static unsigned long calculate_high_delay(struct mem_cgroup *memcg, 2203 unsigned int nr_pages, 2204 u64 max_overage) 2205 { 2206 unsigned long penalty_jiffies; 2207 2208 if (!max_overage) 2209 return 0; 2210 2211 /* 2212 * We use overage compared to memory.high to calculate the number of 2213 * jiffies to sleep (penalty_jiffies). Ideally this value should be 2214 * fairly lenient on small overages, and increasingly harsh when the 2215 * memcg in question makes it clear that it has no intention of stopping 2216 * its crazy behaviour, so we exponentially increase the delay based on 2217 * overage amount. 2218 */ 2219 penalty_jiffies = max_overage * max_overage * HZ; 2220 penalty_jiffies >>= MEMCG_DELAY_PRECISION_SHIFT; 2221 penalty_jiffies >>= MEMCG_DELAY_SCALING_SHIFT; 2222 2223 /* 2224 * Factor in the task's own contribution to the overage, such that four 2225 * N-sized allocations are throttled approximately the same as one 2226 * 4N-sized allocation. 2227 * 2228 * MEMCG_CHARGE_BATCH pages is nominal, so work out how much smaller or 2229 * larger the current charge patch is than that. 2230 */ 2231 return penalty_jiffies * nr_pages / MEMCG_CHARGE_BATCH; 2232 } 2233 2234 /* 2235 * Reclaims memory over the high limit. Called directly from 2236 * try_charge() (context permitting), as well as from the userland 2237 * return path where reclaim is always able to block. 2238 */ 2239 void __mem_cgroup_handle_over_high(gfp_t gfp_mask) 2240 { 2241 unsigned long penalty_jiffies; 2242 unsigned long pflags; 2243 unsigned long nr_reclaimed; 2244 unsigned int nr_pages = current->memcg_nr_pages_over_high; 2245 int nr_retries = MAX_RECLAIM_RETRIES; 2246 struct mem_cgroup *memcg; 2247 bool in_retry = false; 2248 2249 memcg = get_mem_cgroup_from_mm(current->mm); 2250 current->memcg_nr_pages_over_high = 0; 2251 2252 retry_reclaim: 2253 /* 2254 * Bail if the task is already exiting. Unlike memory.max, 2255 * memory.high enforcement isn't as strict, and there is no 2256 * OOM killer involved, which means the excess could already 2257 * be much bigger (and still growing) than it could for 2258 * memory.max; the dying task could get stuck in fruitless 2259 * reclaim for a long time, which isn't desirable. 2260 */ 2261 if (task_is_dying()) 2262 goto out; 2263 2264 /* 2265 * The allocating task should reclaim at least the batch size, but for 2266 * subsequent retries we only want to do what's necessary to prevent oom 2267 * or breaching resource isolation. 2268 * 2269 * This is distinct from memory.max or page allocator behaviour because 2270 * memory.high is currently batched, whereas memory.max and the page 2271 * allocator run every time an allocation is made. 2272 */ 2273 nr_reclaimed = reclaim_high(memcg, 2274 in_retry ? SWAP_CLUSTER_MAX : nr_pages, 2275 gfp_mask); 2276 2277 /* 2278 * memory.high is breached and reclaim is unable to keep up. Throttle 2279 * allocators proactively to slow down excessive growth. 2280 */ 2281 penalty_jiffies = calculate_high_delay(memcg, nr_pages, 2282 mem_find_max_overage(memcg)); 2283 2284 penalty_jiffies += calculate_high_delay(memcg, nr_pages, 2285 swap_find_max_overage(memcg)); 2286 2287 /* 2288 * Clamp the max delay per usermode return so as to still keep the 2289 * application moving forwards and also permit diagnostics, albeit 2290 * extremely slowly. 2291 */ 2292 penalty_jiffies = min(penalty_jiffies, MEMCG_MAX_HIGH_DELAY_JIFFIES); 2293 2294 /* 2295 * Don't sleep if the amount of jiffies this memcg owes us is so low 2296 * that it's not even worth doing, in an attempt to be nice to those who 2297 * go only a small amount over their memory.high value and maybe haven't 2298 * been aggressively reclaimed enough yet. 2299 */ 2300 if (penalty_jiffies <= HZ / 100) 2301 goto out; 2302 2303 /* 2304 * If reclaim is making forward progress but we're still over 2305 * memory.high, we want to encourage that rather than doing allocator 2306 * throttling. 2307 */ 2308 if (nr_reclaimed || nr_retries--) { 2309 in_retry = true; 2310 goto retry_reclaim; 2311 } 2312 2313 /* 2314 * Reclaim didn't manage to push usage below the limit, slow 2315 * this allocating task down. 2316 * 2317 * If we exit early, we're guaranteed to die (since 2318 * schedule_timeout_killable sets TASK_KILLABLE). This means we don't 2319 * need to account for any ill-begotten jiffies to pay them off later. 2320 */ 2321 psi_memstall_enter(&pflags); 2322 schedule_timeout_killable(penalty_jiffies); 2323 psi_memstall_leave(&pflags); 2324 2325 out: 2326 css_put(&memcg->css); 2327 } 2328 2329 static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask, 2330 unsigned int nr_pages) 2331 { 2332 unsigned int batch = max(MEMCG_CHARGE_BATCH, nr_pages); 2333 int nr_retries = MAX_RECLAIM_RETRIES; 2334 struct mem_cgroup *mem_over_limit; 2335 struct page_counter *counter; 2336 unsigned long nr_reclaimed; 2337 bool passed_oom = false; 2338 unsigned int reclaim_options = MEMCG_RECLAIM_MAY_SWAP; 2339 bool drained = false; 2340 bool raised_max_event = false; 2341 unsigned long pflags; 2342 bool allow_spinning = gfpflags_allow_spinning(gfp_mask); 2343 2344 retry: 2345 if (consume_stock(memcg, nr_pages)) 2346 return 0; 2347 2348 if (!allow_spinning) 2349 /* Avoid the refill and flush of the older stock */ 2350 batch = nr_pages; 2351 2352 if (!do_memsw_account() || 2353 page_counter_try_charge(&memcg->memsw, batch, &counter)) { 2354 if (page_counter_try_charge(&memcg->memory, batch, &counter)) 2355 goto done_restock; 2356 if (do_memsw_account()) 2357 page_counter_uncharge(&memcg->memsw, batch); 2358 mem_over_limit = mem_cgroup_from_counter(counter, memory); 2359 } else { 2360 mem_over_limit = mem_cgroup_from_counter(counter, memsw); 2361 reclaim_options &= ~MEMCG_RECLAIM_MAY_SWAP; 2362 } 2363 2364 if (batch > nr_pages) { 2365 batch = nr_pages; 2366 goto retry; 2367 } 2368 2369 /* 2370 * Prevent unbounded recursion when reclaim operations need to 2371 * allocate memory. This might exceed the limits temporarily, 2372 * but we prefer facilitating memory reclaim and getting back 2373 * under the limit over triggering OOM kills in these cases. 2374 */ 2375 if (unlikely(current->flags & PF_MEMALLOC)) 2376 goto force; 2377 2378 if (unlikely(task_in_memcg_oom(current))) 2379 goto nomem; 2380 2381 if (!gfpflags_allow_blocking(gfp_mask)) 2382 goto nomem; 2383 2384 __memcg_memory_event(mem_over_limit, MEMCG_MAX, allow_spinning); 2385 raised_max_event = true; 2386 2387 psi_memstall_enter(&pflags); 2388 nr_reclaimed = try_to_free_mem_cgroup_pages(mem_over_limit, nr_pages, 2389 gfp_mask, reclaim_options, NULL); 2390 psi_memstall_leave(&pflags); 2391 2392 if (mem_cgroup_margin(mem_over_limit) >= nr_pages) 2393 goto retry; 2394 2395 if (!drained) { 2396 drain_all_stock(mem_over_limit); 2397 drained = true; 2398 goto retry; 2399 } 2400 2401 if (gfp_mask & __GFP_NORETRY) 2402 goto nomem; 2403 /* 2404 * Even though the limit is exceeded at this point, reclaim 2405 * may have been able to free some pages. Retry the charge 2406 * before killing the task. 2407 * 2408 * Only for regular pages, though: huge pages are rather 2409 * unlikely to succeed so close to the limit, and we fall back 2410 * to regular pages anyway in case of failure. 2411 */ 2412 if (nr_reclaimed && nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER)) 2413 goto retry; 2414 2415 if (nr_retries--) 2416 goto retry; 2417 2418 if (gfp_mask & __GFP_RETRY_MAYFAIL) 2419 goto nomem; 2420 2421 /* Avoid endless loop for tasks bypassed by the oom killer */ 2422 if (passed_oom && task_is_dying()) 2423 goto nomem; 2424 2425 /* 2426 * keep retrying as long as the memcg oom killer is able to make 2427 * a forward progress or bypass the charge if the oom killer 2428 * couldn't make any progress. 2429 */ 2430 if (mem_cgroup_oom(mem_over_limit, gfp_mask, 2431 get_order(nr_pages * PAGE_SIZE))) { 2432 passed_oom = true; 2433 nr_retries = MAX_RECLAIM_RETRIES; 2434 goto retry; 2435 } 2436 nomem: 2437 /* 2438 * Memcg doesn't have a dedicated reserve for atomic 2439 * allocations. But like the global atomic pool, we need to 2440 * put the burden of reclaim on regular allocation requests 2441 * and let these go through as privileged allocations. 2442 */ 2443 if (!(gfp_mask & (__GFP_NOFAIL | __GFP_HIGH))) 2444 return -ENOMEM; 2445 force: 2446 /* 2447 * If the allocation has to be enforced, don't forget to raise 2448 * a MEMCG_MAX event. 2449 */ 2450 if (!raised_max_event) 2451 __memcg_memory_event(mem_over_limit, MEMCG_MAX, allow_spinning); 2452 2453 /* 2454 * The allocation either can't fail or will lead to more memory 2455 * being freed very soon. Allow memory usage go over the limit 2456 * temporarily by force charging it. 2457 */ 2458 page_counter_charge(&memcg->memory, nr_pages); 2459 if (do_memsw_account()) 2460 page_counter_charge(&memcg->memsw, nr_pages); 2461 2462 return 0; 2463 2464 done_restock: 2465 if (batch > nr_pages) 2466 refill_stock(memcg, batch - nr_pages); 2467 2468 /* 2469 * If the hierarchy is above the normal consumption range, schedule 2470 * reclaim on returning to userland. We can perform reclaim here 2471 * if __GFP_RECLAIM but let's always punt for simplicity and so that 2472 * GFP_KERNEL can consistently be used during reclaim. @memcg is 2473 * not recorded as it most likely matches current's and won't 2474 * change in the meantime. As high limit is checked again before 2475 * reclaim, the cost of mismatch is negligible. 2476 */ 2477 do { 2478 bool mem_high, swap_high; 2479 2480 mem_high = page_counter_read(&memcg->memory) > 2481 READ_ONCE(memcg->memory.high); 2482 swap_high = page_counter_read(&memcg->swap) > 2483 READ_ONCE(memcg->swap.high); 2484 2485 /* Don't bother a random interrupted task */ 2486 if (!in_task()) { 2487 if (mem_high) { 2488 schedule_work(&memcg->high_work); 2489 break; 2490 } 2491 continue; 2492 } 2493 2494 if (mem_high || swap_high) { 2495 /* 2496 * The allocating tasks in this cgroup will need to do 2497 * reclaim or be throttled to prevent further growth 2498 * of the memory or swap footprints. 2499 * 2500 * Target some best-effort fairness between the tasks, 2501 * and distribute reclaim work and delay penalties 2502 * based on how much each task is actually allocating. 2503 */ 2504 current->memcg_nr_pages_over_high += batch; 2505 set_notify_resume(current); 2506 break; 2507 } 2508 } while ((memcg = parent_mem_cgroup(memcg))); 2509 2510 /* 2511 * Reclaim is set up above to be called from the userland 2512 * return path. But also attempt synchronous reclaim to avoid 2513 * excessive overrun while the task is still inside the 2514 * kernel. If this is successful, the return path will see it 2515 * when it rechecks the overage and simply bail out. 2516 */ 2517 if (current->memcg_nr_pages_over_high > MEMCG_CHARGE_BATCH && 2518 !(current->flags & PF_MEMALLOC) && 2519 gfpflags_allow_blocking(gfp_mask)) 2520 __mem_cgroup_handle_over_high(gfp_mask); 2521 return 0; 2522 } 2523 2524 static inline int try_charge(struct mem_cgroup *memcg, gfp_t gfp_mask, 2525 unsigned int nr_pages) 2526 { 2527 if (mem_cgroup_is_root(memcg)) 2528 return 0; 2529 2530 return try_charge_memcg(memcg, gfp_mask, nr_pages); 2531 } 2532 2533 static void commit_charge(struct folio *folio, struct mem_cgroup *memcg) 2534 { 2535 VM_BUG_ON_FOLIO(folio_memcg_charged(folio), folio); 2536 /* 2537 * Any of the following ensures page's memcg stability: 2538 * 2539 * - the page lock 2540 * - LRU isolation 2541 * - exclusive reference 2542 */ 2543 folio->memcg_data = (unsigned long)memcg; 2544 } 2545 2546 #ifdef CONFIG_MEMCG_NMI_SAFETY_REQUIRES_ATOMIC 2547 static inline void account_slab_nmi_safe(struct mem_cgroup *memcg, 2548 struct pglist_data *pgdat, 2549 enum node_stat_item idx, int nr) 2550 { 2551 struct lruvec *lruvec; 2552 2553 if (likely(!in_nmi())) { 2554 lruvec = mem_cgroup_lruvec(memcg, pgdat); 2555 mod_memcg_lruvec_state(lruvec, idx, nr); 2556 } else { 2557 struct mem_cgroup_per_node *pn = memcg->nodeinfo[pgdat->node_id]; 2558 2559 /* preemption is disabled in_nmi(). */ 2560 css_rstat_updated(&memcg->css, smp_processor_id()); 2561 if (idx == NR_SLAB_RECLAIMABLE_B) 2562 atomic_add(nr, &pn->slab_reclaimable); 2563 else 2564 atomic_add(nr, &pn->slab_unreclaimable); 2565 } 2566 } 2567 #else 2568 static inline void account_slab_nmi_safe(struct mem_cgroup *memcg, 2569 struct pglist_data *pgdat, 2570 enum node_stat_item idx, int nr) 2571 { 2572 struct lruvec *lruvec; 2573 2574 lruvec = mem_cgroup_lruvec(memcg, pgdat); 2575 mod_memcg_lruvec_state(lruvec, idx, nr); 2576 } 2577 #endif 2578 2579 static inline void mod_objcg_mlstate(struct obj_cgroup *objcg, 2580 struct pglist_data *pgdat, 2581 enum node_stat_item idx, int nr) 2582 { 2583 struct mem_cgroup *memcg; 2584 2585 rcu_read_lock(); 2586 memcg = obj_cgroup_memcg(objcg); 2587 account_slab_nmi_safe(memcg, pgdat, idx, nr); 2588 rcu_read_unlock(); 2589 } 2590 2591 static __always_inline 2592 struct mem_cgroup *mem_cgroup_from_obj_slab(struct slab *slab, void *p) 2593 { 2594 /* 2595 * Slab objects are accounted individually, not per-page. 2596 * Memcg membership data for each individual object is saved in 2597 * slab->obj_exts. 2598 */ 2599 struct slabobj_ext *obj_exts; 2600 unsigned int off; 2601 2602 obj_exts = slab_obj_exts(slab); 2603 if (!obj_exts) 2604 return NULL; 2605 2606 off = obj_to_index(slab->slab_cache, slab, p); 2607 if (obj_exts[off].objcg) 2608 return obj_cgroup_memcg(obj_exts[off].objcg); 2609 2610 return NULL; 2611 } 2612 2613 /* 2614 * Returns a pointer to the memory cgroup to which the kernel object is charged. 2615 * It is not suitable for objects allocated using vmalloc(). 2616 * 2617 * A passed kernel object must be a slab object or a generic kernel page. 2618 * 2619 * The caller must ensure the memcg lifetime, e.g. by taking rcu_read_lock(), 2620 * cgroup_mutex, etc. 2621 */ 2622 struct mem_cgroup *mem_cgroup_from_slab_obj(void *p) 2623 { 2624 struct slab *slab; 2625 2626 if (mem_cgroup_disabled()) 2627 return NULL; 2628 2629 slab = virt_to_slab(p); 2630 if (slab) 2631 return mem_cgroup_from_obj_slab(slab, p); 2632 return folio_memcg_check(virt_to_folio(p)); 2633 } 2634 2635 static struct obj_cgroup *__get_obj_cgroup_from_memcg(struct mem_cgroup *memcg) 2636 { 2637 struct obj_cgroup *objcg = NULL; 2638 2639 for (; !mem_cgroup_is_root(memcg); memcg = parent_mem_cgroup(memcg)) { 2640 objcg = rcu_dereference(memcg->objcg); 2641 if (likely(objcg && obj_cgroup_tryget(objcg))) 2642 break; 2643 objcg = NULL; 2644 } 2645 return objcg; 2646 } 2647 2648 static struct obj_cgroup *current_objcg_update(void) 2649 { 2650 struct mem_cgroup *memcg; 2651 struct obj_cgroup *old, *objcg = NULL; 2652 2653 do { 2654 /* Atomically drop the update bit. */ 2655 old = xchg(¤t->objcg, NULL); 2656 if (old) { 2657 old = (struct obj_cgroup *) 2658 ((unsigned long)old & ~CURRENT_OBJCG_UPDATE_FLAG); 2659 obj_cgroup_put(old); 2660 2661 old = NULL; 2662 } 2663 2664 /* If new objcg is NULL, no reason for the second atomic update. */ 2665 if (!current->mm || (current->flags & PF_KTHREAD)) 2666 return NULL; 2667 2668 /* 2669 * Release the objcg pointer from the previous iteration, 2670 * if try_cmpxcg() below fails. 2671 */ 2672 if (unlikely(objcg)) { 2673 obj_cgroup_put(objcg); 2674 objcg = NULL; 2675 } 2676 2677 /* 2678 * Obtain the new objcg pointer. The current task can be 2679 * asynchronously moved to another memcg and the previous 2680 * memcg can be offlined. So let's get the memcg pointer 2681 * and try get a reference to objcg under a rcu read lock. 2682 */ 2683 2684 rcu_read_lock(); 2685 memcg = mem_cgroup_from_task(current); 2686 objcg = __get_obj_cgroup_from_memcg(memcg); 2687 rcu_read_unlock(); 2688 2689 /* 2690 * Try set up a new objcg pointer atomically. If it 2691 * fails, it means the update flag was set concurrently, so 2692 * the whole procedure should be repeated. 2693 */ 2694 } while (!try_cmpxchg(¤t->objcg, &old, objcg)); 2695 2696 return objcg; 2697 } 2698 2699 __always_inline struct obj_cgroup *current_obj_cgroup(void) 2700 { 2701 struct mem_cgroup *memcg; 2702 struct obj_cgroup *objcg; 2703 2704 if (IS_ENABLED(CONFIG_MEMCG_NMI_UNSAFE) && in_nmi()) 2705 return NULL; 2706 2707 if (in_task()) { 2708 memcg = current->active_memcg; 2709 if (unlikely(memcg)) 2710 goto from_memcg; 2711 2712 objcg = READ_ONCE(current->objcg); 2713 if (unlikely((unsigned long)objcg & CURRENT_OBJCG_UPDATE_FLAG)) 2714 objcg = current_objcg_update(); 2715 /* 2716 * Objcg reference is kept by the task, so it's safe 2717 * to use the objcg by the current task. 2718 */ 2719 return objcg; 2720 } 2721 2722 memcg = this_cpu_read(int_active_memcg); 2723 if (unlikely(memcg)) 2724 goto from_memcg; 2725 2726 return NULL; 2727 2728 from_memcg: 2729 objcg = NULL; 2730 for (; !mem_cgroup_is_root(memcg); memcg = parent_mem_cgroup(memcg)) { 2731 /* 2732 * Memcg pointer is protected by scope (see set_active_memcg()) 2733 * and is pinning the corresponding objcg, so objcg can't go 2734 * away and can be used within the scope without any additional 2735 * protection. 2736 */ 2737 objcg = rcu_dereference_check(memcg->objcg, 1); 2738 if (likely(objcg)) 2739 break; 2740 } 2741 2742 return objcg; 2743 } 2744 2745 struct obj_cgroup *get_obj_cgroup_from_folio(struct folio *folio) 2746 { 2747 struct obj_cgroup *objcg; 2748 2749 if (!memcg_kmem_online()) 2750 return NULL; 2751 2752 if (folio_memcg_kmem(folio)) { 2753 objcg = __folio_objcg(folio); 2754 obj_cgroup_get(objcg); 2755 } else { 2756 struct mem_cgroup *memcg; 2757 2758 rcu_read_lock(); 2759 memcg = __folio_memcg(folio); 2760 if (memcg) 2761 objcg = __get_obj_cgroup_from_memcg(memcg); 2762 else 2763 objcg = NULL; 2764 rcu_read_unlock(); 2765 } 2766 return objcg; 2767 } 2768 2769 #ifdef CONFIG_MEMCG_NMI_SAFETY_REQUIRES_ATOMIC 2770 static inline void account_kmem_nmi_safe(struct mem_cgroup *memcg, int val) 2771 { 2772 if (likely(!in_nmi())) { 2773 mod_memcg_state(memcg, MEMCG_KMEM, val); 2774 } else { 2775 /* preemption is disabled in_nmi(). */ 2776 css_rstat_updated(&memcg->css, smp_processor_id()); 2777 atomic_add(val, &memcg->kmem_stat); 2778 } 2779 } 2780 #else 2781 static inline void account_kmem_nmi_safe(struct mem_cgroup *memcg, int val) 2782 { 2783 mod_memcg_state(memcg, MEMCG_KMEM, val); 2784 } 2785 #endif 2786 2787 /* 2788 * obj_cgroup_uncharge_pages: uncharge a number of kernel pages from a objcg 2789 * @objcg: object cgroup to uncharge 2790 * @nr_pages: number of pages to uncharge 2791 */ 2792 static void obj_cgroup_uncharge_pages(struct obj_cgroup *objcg, 2793 unsigned int nr_pages) 2794 { 2795 struct mem_cgroup *memcg; 2796 2797 memcg = get_mem_cgroup_from_objcg(objcg); 2798 2799 account_kmem_nmi_safe(memcg, -nr_pages); 2800 memcg1_account_kmem(memcg, -nr_pages); 2801 if (!mem_cgroup_is_root(memcg)) 2802 refill_stock(memcg, nr_pages); 2803 2804 css_put(&memcg->css); 2805 } 2806 2807 /* 2808 * obj_cgroup_charge_pages: charge a number of kernel pages to a objcg 2809 * @objcg: object cgroup to charge 2810 * @gfp: reclaim mode 2811 * @nr_pages: number of pages to charge 2812 * 2813 * Returns 0 on success, an error code on failure. 2814 */ 2815 static int obj_cgroup_charge_pages(struct obj_cgroup *objcg, gfp_t gfp, 2816 unsigned int nr_pages) 2817 { 2818 struct mem_cgroup *memcg; 2819 int ret; 2820 2821 memcg = get_mem_cgroup_from_objcg(objcg); 2822 2823 ret = try_charge_memcg(memcg, gfp, nr_pages); 2824 if (ret) 2825 goto out; 2826 2827 account_kmem_nmi_safe(memcg, nr_pages); 2828 memcg1_account_kmem(memcg, nr_pages); 2829 out: 2830 css_put(&memcg->css); 2831 2832 return ret; 2833 } 2834 2835 static struct obj_cgroup *page_objcg(const struct page *page) 2836 { 2837 unsigned long memcg_data = page->memcg_data; 2838 2839 if (mem_cgroup_disabled() || !memcg_data) 2840 return NULL; 2841 2842 VM_BUG_ON_PAGE((memcg_data & OBJEXTS_FLAGS_MASK) != MEMCG_DATA_KMEM, 2843 page); 2844 return (struct obj_cgroup *)(memcg_data - MEMCG_DATA_KMEM); 2845 } 2846 2847 static void page_set_objcg(struct page *page, const struct obj_cgroup *objcg) 2848 { 2849 page->memcg_data = (unsigned long)objcg | MEMCG_DATA_KMEM; 2850 } 2851 2852 /** 2853 * __memcg_kmem_charge_page: charge a kmem page to the current memory cgroup 2854 * @page: page to charge 2855 * @gfp: reclaim mode 2856 * @order: allocation order 2857 * 2858 * Returns 0 on success, an error code on failure. 2859 */ 2860 int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, int order) 2861 { 2862 struct obj_cgroup *objcg; 2863 int ret = 0; 2864 2865 objcg = current_obj_cgroup(); 2866 if (objcg) { 2867 ret = obj_cgroup_charge_pages(objcg, gfp, 1 << order); 2868 if (!ret) { 2869 obj_cgroup_get(objcg); 2870 page_set_objcg(page, objcg); 2871 return 0; 2872 } 2873 } 2874 return ret; 2875 } 2876 2877 /** 2878 * __memcg_kmem_uncharge_page: uncharge a kmem page 2879 * @page: page to uncharge 2880 * @order: allocation order 2881 */ 2882 void __memcg_kmem_uncharge_page(struct page *page, int order) 2883 { 2884 struct obj_cgroup *objcg = page_objcg(page); 2885 unsigned int nr_pages = 1 << order; 2886 2887 if (!objcg) 2888 return; 2889 2890 obj_cgroup_uncharge_pages(objcg, nr_pages); 2891 page->memcg_data = 0; 2892 obj_cgroup_put(objcg); 2893 } 2894 2895 static void __account_obj_stock(struct obj_cgroup *objcg, 2896 struct obj_stock_pcp *stock, int nr, 2897 struct pglist_data *pgdat, enum node_stat_item idx) 2898 { 2899 int *bytes; 2900 2901 /* 2902 * Save vmstat data in stock and skip vmstat array update unless 2903 * accumulating over a page of vmstat data or when pgdat changes. 2904 */ 2905 if (stock->cached_pgdat != pgdat) { 2906 /* Flush the existing cached vmstat data */ 2907 struct pglist_data *oldpg = stock->cached_pgdat; 2908 2909 if (stock->nr_slab_reclaimable_b) { 2910 mod_objcg_mlstate(objcg, oldpg, NR_SLAB_RECLAIMABLE_B, 2911 stock->nr_slab_reclaimable_b); 2912 stock->nr_slab_reclaimable_b = 0; 2913 } 2914 if (stock->nr_slab_unreclaimable_b) { 2915 mod_objcg_mlstate(objcg, oldpg, NR_SLAB_UNRECLAIMABLE_B, 2916 stock->nr_slab_unreclaimable_b); 2917 stock->nr_slab_unreclaimable_b = 0; 2918 } 2919 stock->cached_pgdat = pgdat; 2920 } 2921 2922 bytes = (idx == NR_SLAB_RECLAIMABLE_B) ? &stock->nr_slab_reclaimable_b 2923 : &stock->nr_slab_unreclaimable_b; 2924 /* 2925 * Even for large object >= PAGE_SIZE, the vmstat data will still be 2926 * cached locally at least once before pushing it out. 2927 */ 2928 if (!*bytes) { 2929 *bytes = nr; 2930 nr = 0; 2931 } else { 2932 *bytes += nr; 2933 if (abs(*bytes) > PAGE_SIZE) { 2934 nr = *bytes; 2935 *bytes = 0; 2936 } else { 2937 nr = 0; 2938 } 2939 } 2940 if (nr) 2941 mod_objcg_mlstate(objcg, pgdat, idx, nr); 2942 } 2943 2944 static bool consume_obj_stock(struct obj_cgroup *objcg, unsigned int nr_bytes, 2945 struct pglist_data *pgdat, enum node_stat_item idx) 2946 { 2947 struct obj_stock_pcp *stock; 2948 bool ret = false; 2949 2950 if (!local_trylock(&obj_stock.lock)) 2951 return ret; 2952 2953 stock = this_cpu_ptr(&obj_stock); 2954 if (objcg == READ_ONCE(stock->cached_objcg) && stock->nr_bytes >= nr_bytes) { 2955 stock->nr_bytes -= nr_bytes; 2956 ret = true; 2957 2958 if (pgdat) 2959 __account_obj_stock(objcg, stock, nr_bytes, pgdat, idx); 2960 } 2961 2962 local_unlock(&obj_stock.lock); 2963 2964 return ret; 2965 } 2966 2967 static void drain_obj_stock(struct obj_stock_pcp *stock) 2968 { 2969 struct obj_cgroup *old = READ_ONCE(stock->cached_objcg); 2970 2971 if (!old) 2972 return; 2973 2974 if (stock->nr_bytes) { 2975 unsigned int nr_pages = stock->nr_bytes >> PAGE_SHIFT; 2976 unsigned int nr_bytes = stock->nr_bytes & (PAGE_SIZE - 1); 2977 2978 if (nr_pages) { 2979 struct mem_cgroup *memcg; 2980 2981 memcg = get_mem_cgroup_from_objcg(old); 2982 2983 mod_memcg_state(memcg, MEMCG_KMEM, -nr_pages); 2984 memcg1_account_kmem(memcg, -nr_pages); 2985 if (!mem_cgroup_is_root(memcg)) 2986 memcg_uncharge(memcg, nr_pages); 2987 2988 css_put(&memcg->css); 2989 } 2990 2991 /* 2992 * The leftover is flushed to the centralized per-memcg value. 2993 * On the next attempt to refill obj stock it will be moved 2994 * to a per-cpu stock (probably, on an other CPU), see 2995 * refill_obj_stock(). 2996 * 2997 * How often it's flushed is a trade-off between the memory 2998 * limit enforcement accuracy and potential CPU contention, 2999 * so it might be changed in the future. 3000 */ 3001 atomic_add(nr_bytes, &old->nr_charged_bytes); 3002 stock->nr_bytes = 0; 3003 } 3004 3005 /* 3006 * Flush the vmstat data in current stock 3007 */ 3008 if (stock->nr_slab_reclaimable_b || stock->nr_slab_unreclaimable_b) { 3009 if (stock->nr_slab_reclaimable_b) { 3010 mod_objcg_mlstate(old, stock->cached_pgdat, 3011 NR_SLAB_RECLAIMABLE_B, 3012 stock->nr_slab_reclaimable_b); 3013 stock->nr_slab_reclaimable_b = 0; 3014 } 3015 if (stock->nr_slab_unreclaimable_b) { 3016 mod_objcg_mlstate(old, stock->cached_pgdat, 3017 NR_SLAB_UNRECLAIMABLE_B, 3018 stock->nr_slab_unreclaimable_b); 3019 stock->nr_slab_unreclaimable_b = 0; 3020 } 3021 stock->cached_pgdat = NULL; 3022 } 3023 3024 WRITE_ONCE(stock->cached_objcg, NULL); 3025 obj_cgroup_put(old); 3026 } 3027 3028 static bool obj_stock_flush_required(struct obj_stock_pcp *stock, 3029 struct mem_cgroup *root_memcg) 3030 { 3031 struct obj_cgroup *objcg = READ_ONCE(stock->cached_objcg); 3032 struct mem_cgroup *memcg; 3033 bool flush = false; 3034 3035 rcu_read_lock(); 3036 if (objcg) { 3037 memcg = obj_cgroup_memcg(objcg); 3038 if (memcg && mem_cgroup_is_descendant(memcg, root_memcg)) 3039 flush = true; 3040 } 3041 rcu_read_unlock(); 3042 3043 return flush; 3044 } 3045 3046 static void refill_obj_stock(struct obj_cgroup *objcg, unsigned int nr_bytes, 3047 bool allow_uncharge, int nr_acct, struct pglist_data *pgdat, 3048 enum node_stat_item idx) 3049 { 3050 struct obj_stock_pcp *stock; 3051 unsigned int nr_pages = 0; 3052 3053 if (!local_trylock(&obj_stock.lock)) { 3054 if (pgdat) 3055 mod_objcg_mlstate(objcg, pgdat, idx, nr_bytes); 3056 nr_pages = nr_bytes >> PAGE_SHIFT; 3057 nr_bytes = nr_bytes & (PAGE_SIZE - 1); 3058 atomic_add(nr_bytes, &objcg->nr_charged_bytes); 3059 goto out; 3060 } 3061 3062 stock = this_cpu_ptr(&obj_stock); 3063 if (READ_ONCE(stock->cached_objcg) != objcg) { /* reset if necessary */ 3064 drain_obj_stock(stock); 3065 obj_cgroup_get(objcg); 3066 stock->nr_bytes = atomic_read(&objcg->nr_charged_bytes) 3067 ? atomic_xchg(&objcg->nr_charged_bytes, 0) : 0; 3068 WRITE_ONCE(stock->cached_objcg, objcg); 3069 3070 allow_uncharge = true; /* Allow uncharge when objcg changes */ 3071 } 3072 stock->nr_bytes += nr_bytes; 3073 3074 if (pgdat) 3075 __account_obj_stock(objcg, stock, nr_acct, pgdat, idx); 3076 3077 if (allow_uncharge && (stock->nr_bytes > PAGE_SIZE)) { 3078 nr_pages = stock->nr_bytes >> PAGE_SHIFT; 3079 stock->nr_bytes &= (PAGE_SIZE - 1); 3080 } 3081 3082 local_unlock(&obj_stock.lock); 3083 out: 3084 if (nr_pages) 3085 obj_cgroup_uncharge_pages(objcg, nr_pages); 3086 } 3087 3088 static int obj_cgroup_charge_account(struct obj_cgroup *objcg, gfp_t gfp, size_t size, 3089 struct pglist_data *pgdat, enum node_stat_item idx) 3090 { 3091 unsigned int nr_pages, nr_bytes; 3092 int ret; 3093 3094 if (likely(consume_obj_stock(objcg, size, pgdat, idx))) 3095 return 0; 3096 3097 /* 3098 * In theory, objcg->nr_charged_bytes can have enough 3099 * pre-charged bytes to satisfy the allocation. However, 3100 * flushing objcg->nr_charged_bytes requires two atomic 3101 * operations, and objcg->nr_charged_bytes can't be big. 3102 * The shared objcg->nr_charged_bytes can also become a 3103 * performance bottleneck if all tasks of the same memcg are 3104 * trying to update it. So it's better to ignore it and try 3105 * grab some new pages. The stock's nr_bytes will be flushed to 3106 * objcg->nr_charged_bytes later on when objcg changes. 3107 * 3108 * The stock's nr_bytes may contain enough pre-charged bytes 3109 * to allow one less page from being charged, but we can't rely 3110 * on the pre-charged bytes not being changed outside of 3111 * consume_obj_stock() or refill_obj_stock(). So ignore those 3112 * pre-charged bytes as well when charging pages. To avoid a 3113 * page uncharge right after a page charge, we set the 3114 * allow_uncharge flag to false when calling refill_obj_stock() 3115 * to temporarily allow the pre-charged bytes to exceed the page 3116 * size limit. The maximum reachable value of the pre-charged 3117 * bytes is (sizeof(object) + PAGE_SIZE - 2) if there is no data 3118 * race. 3119 */ 3120 nr_pages = size >> PAGE_SHIFT; 3121 nr_bytes = size & (PAGE_SIZE - 1); 3122 3123 if (nr_bytes) 3124 nr_pages += 1; 3125 3126 ret = obj_cgroup_charge_pages(objcg, gfp, nr_pages); 3127 if (!ret && (nr_bytes || pgdat)) 3128 refill_obj_stock(objcg, nr_bytes ? PAGE_SIZE - nr_bytes : 0, 3129 false, size, pgdat, idx); 3130 3131 return ret; 3132 } 3133 3134 int obj_cgroup_charge(struct obj_cgroup *objcg, gfp_t gfp, size_t size) 3135 { 3136 return obj_cgroup_charge_account(objcg, gfp, size, NULL, 0); 3137 } 3138 3139 void obj_cgroup_uncharge(struct obj_cgroup *objcg, size_t size) 3140 { 3141 refill_obj_stock(objcg, size, true, 0, NULL, 0); 3142 } 3143 3144 static inline size_t obj_full_size(struct kmem_cache *s) 3145 { 3146 /* 3147 * For each accounted object there is an extra space which is used 3148 * to store obj_cgroup membership. Charge it too. 3149 */ 3150 return s->size + sizeof(struct obj_cgroup *); 3151 } 3152 3153 bool __memcg_slab_post_alloc_hook(struct kmem_cache *s, struct list_lru *lru, 3154 gfp_t flags, size_t size, void **p) 3155 { 3156 struct obj_cgroup *objcg; 3157 struct slab *slab; 3158 unsigned long off; 3159 size_t i; 3160 3161 /* 3162 * The obtained objcg pointer is safe to use within the current scope, 3163 * defined by current task or set_active_memcg() pair. 3164 * obj_cgroup_get() is used to get a permanent reference. 3165 */ 3166 objcg = current_obj_cgroup(); 3167 if (!objcg) 3168 return true; 3169 3170 /* 3171 * slab_alloc_node() avoids the NULL check, so we might be called with a 3172 * single NULL object. kmem_cache_alloc_bulk() aborts if it can't fill 3173 * the whole requested size. 3174 * return success as there's nothing to free back 3175 */ 3176 if (unlikely(*p == NULL)) 3177 return true; 3178 3179 flags &= gfp_allowed_mask; 3180 3181 if (lru) { 3182 int ret; 3183 struct mem_cgroup *memcg; 3184 3185 memcg = get_mem_cgroup_from_objcg(objcg); 3186 ret = memcg_list_lru_alloc(memcg, lru, flags); 3187 css_put(&memcg->css); 3188 3189 if (ret) 3190 return false; 3191 } 3192 3193 for (i = 0; i < size; i++) { 3194 slab = virt_to_slab(p[i]); 3195 3196 if (!slab_obj_exts(slab) && 3197 alloc_slab_obj_exts(slab, s, flags, false)) { 3198 continue; 3199 } 3200 3201 /* 3202 * if we fail and size is 1, memcg_alloc_abort_single() will 3203 * just free the object, which is ok as we have not assigned 3204 * objcg to its obj_ext yet 3205 * 3206 * for larger sizes, kmem_cache_free_bulk() will uncharge 3207 * any objects that were already charged and obj_ext assigned 3208 * 3209 * TODO: we could batch this until slab_pgdat(slab) changes 3210 * between iterations, with a more complicated undo 3211 */ 3212 if (obj_cgroup_charge_account(objcg, flags, obj_full_size(s), 3213 slab_pgdat(slab), cache_vmstat_idx(s))) 3214 return false; 3215 3216 off = obj_to_index(s, slab, p[i]); 3217 obj_cgroup_get(objcg); 3218 slab_obj_exts(slab)[off].objcg = objcg; 3219 } 3220 3221 return true; 3222 } 3223 3224 void __memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab, 3225 void **p, int objects, struct slabobj_ext *obj_exts) 3226 { 3227 size_t obj_size = obj_full_size(s); 3228 3229 for (int i = 0; i < objects; i++) { 3230 struct obj_cgroup *objcg; 3231 unsigned int off; 3232 3233 off = obj_to_index(s, slab, p[i]); 3234 objcg = obj_exts[off].objcg; 3235 if (!objcg) 3236 continue; 3237 3238 obj_exts[off].objcg = NULL; 3239 refill_obj_stock(objcg, obj_size, true, -obj_size, 3240 slab_pgdat(slab), cache_vmstat_idx(s)); 3241 obj_cgroup_put(objcg); 3242 } 3243 } 3244 3245 /* 3246 * The objcg is only set on the first page, so transfer it to all the 3247 * other pages. 3248 */ 3249 void split_page_memcg(struct page *page, unsigned order) 3250 { 3251 struct obj_cgroup *objcg = page_objcg(page); 3252 unsigned int i, nr = 1 << order; 3253 3254 if (!objcg) 3255 return; 3256 3257 for (i = 1; i < nr; i++) 3258 page_set_objcg(&page[i], objcg); 3259 3260 obj_cgroup_get_many(objcg, nr - 1); 3261 } 3262 3263 void folio_split_memcg_refs(struct folio *folio, unsigned old_order, 3264 unsigned new_order) 3265 { 3266 unsigned new_refs; 3267 3268 if (mem_cgroup_disabled() || !folio_memcg_charged(folio)) 3269 return; 3270 3271 new_refs = (1 << (old_order - new_order)) - 1; 3272 css_get_many(&__folio_memcg(folio)->css, new_refs); 3273 } 3274 3275 unsigned long mem_cgroup_usage(struct mem_cgroup *memcg, bool swap) 3276 { 3277 unsigned long val; 3278 3279 if (mem_cgroup_is_root(memcg)) { 3280 /* 3281 * Approximate root's usage from global state. This isn't 3282 * perfect, but the root usage was always an approximation. 3283 */ 3284 val = global_node_page_state(NR_FILE_PAGES) + 3285 global_node_page_state(NR_ANON_MAPPED); 3286 if (swap) 3287 val += total_swap_pages - get_nr_swap_pages(); 3288 } else { 3289 if (!swap) 3290 val = page_counter_read(&memcg->memory); 3291 else 3292 val = page_counter_read(&memcg->memsw); 3293 } 3294 return val; 3295 } 3296 3297 static int memcg_online_kmem(struct mem_cgroup *memcg) 3298 { 3299 struct obj_cgroup *objcg; 3300 3301 if (mem_cgroup_kmem_disabled()) 3302 return 0; 3303 3304 if (unlikely(mem_cgroup_is_root(memcg))) 3305 return 0; 3306 3307 objcg = obj_cgroup_alloc(); 3308 if (!objcg) 3309 return -ENOMEM; 3310 3311 objcg->memcg = memcg; 3312 rcu_assign_pointer(memcg->objcg, objcg); 3313 obj_cgroup_get(objcg); 3314 memcg->orig_objcg = objcg; 3315 3316 static_branch_enable(&memcg_kmem_online_key); 3317 3318 memcg->kmemcg_id = memcg->id.id; 3319 3320 return 0; 3321 } 3322 3323 static void memcg_offline_kmem(struct mem_cgroup *memcg) 3324 { 3325 struct mem_cgroup *parent; 3326 3327 if (mem_cgroup_kmem_disabled()) 3328 return; 3329 3330 if (unlikely(mem_cgroup_is_root(memcg))) 3331 return; 3332 3333 parent = parent_mem_cgroup(memcg); 3334 if (!parent) 3335 parent = root_mem_cgroup; 3336 3337 memcg_reparent_list_lrus(memcg, parent); 3338 3339 /* 3340 * Objcg's reparenting must be after list_lru's, make sure list_lru 3341 * helpers won't use parent's list_lru until child is drained. 3342 */ 3343 memcg_reparent_objcgs(memcg, parent); 3344 } 3345 3346 #ifdef CONFIG_CGROUP_WRITEBACK 3347 3348 #include <trace/events/writeback.h> 3349 3350 static int memcg_wb_domain_init(struct mem_cgroup *memcg, gfp_t gfp) 3351 { 3352 return wb_domain_init(&memcg->cgwb_domain, gfp); 3353 } 3354 3355 static void memcg_wb_domain_exit(struct mem_cgroup *memcg) 3356 { 3357 wb_domain_exit(&memcg->cgwb_domain); 3358 } 3359 3360 static void memcg_wb_domain_size_changed(struct mem_cgroup *memcg) 3361 { 3362 wb_domain_size_changed(&memcg->cgwb_domain); 3363 } 3364 3365 struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb) 3366 { 3367 struct mem_cgroup *memcg = mem_cgroup_from_css(wb->memcg_css); 3368 3369 if (!memcg->css.parent) 3370 return NULL; 3371 3372 return &memcg->cgwb_domain; 3373 } 3374 3375 /** 3376 * mem_cgroup_wb_stats - retrieve writeback related stats from its memcg 3377 * @wb: bdi_writeback in question 3378 * @pfilepages: out parameter for number of file pages 3379 * @pheadroom: out parameter for number of allocatable pages according to memcg 3380 * @pdirty: out parameter for number of dirty pages 3381 * @pwriteback: out parameter for number of pages under writeback 3382 * 3383 * Determine the numbers of file, headroom, dirty, and writeback pages in 3384 * @wb's memcg. File, dirty and writeback are self-explanatory. Headroom 3385 * is a bit more involved. 3386 * 3387 * A memcg's headroom is "min(max, high) - used". In the hierarchy, the 3388 * headroom is calculated as the lowest headroom of itself and the 3389 * ancestors. Note that this doesn't consider the actual amount of 3390 * available memory in the system. The caller should further cap 3391 * *@pheadroom accordingly. 3392 */ 3393 void mem_cgroup_wb_stats(struct bdi_writeback *wb, unsigned long *pfilepages, 3394 unsigned long *pheadroom, unsigned long *pdirty, 3395 unsigned long *pwriteback) 3396 { 3397 struct mem_cgroup *memcg = mem_cgroup_from_css(wb->memcg_css); 3398 struct mem_cgroup *parent; 3399 3400 mem_cgroup_flush_stats_ratelimited(memcg); 3401 3402 *pdirty = memcg_page_state(memcg, NR_FILE_DIRTY); 3403 *pwriteback = memcg_page_state(memcg, NR_WRITEBACK); 3404 *pfilepages = memcg_page_state(memcg, NR_INACTIVE_FILE) + 3405 memcg_page_state(memcg, NR_ACTIVE_FILE); 3406 3407 *pheadroom = PAGE_COUNTER_MAX; 3408 while ((parent = parent_mem_cgroup(memcg))) { 3409 unsigned long ceiling = min(READ_ONCE(memcg->memory.max), 3410 READ_ONCE(memcg->memory.high)); 3411 unsigned long used = page_counter_read(&memcg->memory); 3412 3413 *pheadroom = min(*pheadroom, ceiling - min(ceiling, used)); 3414 memcg = parent; 3415 } 3416 } 3417 3418 /* 3419 * Foreign dirty flushing 3420 * 3421 * There's an inherent mismatch between memcg and writeback. The former 3422 * tracks ownership per-page while the latter per-inode. This was a 3423 * deliberate design decision because honoring per-page ownership in the 3424 * writeback path is complicated, may lead to higher CPU and IO overheads 3425 * and deemed unnecessary given that write-sharing an inode across 3426 * different cgroups isn't a common use-case. 3427 * 3428 * Combined with inode majority-writer ownership switching, this works well 3429 * enough in most cases but there are some pathological cases. For 3430 * example, let's say there are two cgroups A and B which keep writing to 3431 * different but confined parts of the same inode. B owns the inode and 3432 * A's memory is limited far below B's. A's dirty ratio can rise enough to 3433 * trigger balance_dirty_pages() sleeps but B's can be low enough to avoid 3434 * triggering background writeback. A will be slowed down without a way to 3435 * make writeback of the dirty pages happen. 3436 * 3437 * Conditions like the above can lead to a cgroup getting repeatedly and 3438 * severely throttled after making some progress after each 3439 * dirty_expire_interval while the underlying IO device is almost 3440 * completely idle. 3441 * 3442 * Solving this problem completely requires matching the ownership tracking 3443 * granularities between memcg and writeback in either direction. However, 3444 * the more egregious behaviors can be avoided by simply remembering the 3445 * most recent foreign dirtying events and initiating remote flushes on 3446 * them when local writeback isn't enough to keep the memory clean enough. 3447 * 3448 * The following two functions implement such mechanism. When a foreign 3449 * page - a page whose memcg and writeback ownerships don't match - is 3450 * dirtied, mem_cgroup_track_foreign_dirty() records the inode owning 3451 * bdi_writeback on the page owning memcg. When balance_dirty_pages() 3452 * decides that the memcg needs to sleep due to high dirty ratio, it calls 3453 * mem_cgroup_flush_foreign() which queues writeback on the recorded 3454 * foreign bdi_writebacks which haven't expired. Both the numbers of 3455 * recorded bdi_writebacks and concurrent in-flight foreign writebacks are 3456 * limited to MEMCG_CGWB_FRN_CNT. 3457 * 3458 * The mechanism only remembers IDs and doesn't hold any object references. 3459 * As being wrong occasionally doesn't matter, updates and accesses to the 3460 * records are lockless and racy. 3461 */ 3462 void mem_cgroup_track_foreign_dirty_slowpath(struct folio *folio, 3463 struct bdi_writeback *wb) 3464 { 3465 struct mem_cgroup *memcg = folio_memcg(folio); 3466 struct memcg_cgwb_frn *frn; 3467 u64 now = get_jiffies_64(); 3468 u64 oldest_at = now; 3469 int oldest = -1; 3470 int i; 3471 3472 trace_track_foreign_dirty(folio, wb); 3473 3474 /* 3475 * Pick the slot to use. If there is already a slot for @wb, keep 3476 * using it. If not replace the oldest one which isn't being 3477 * written out. 3478 */ 3479 for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++) { 3480 frn = &memcg->cgwb_frn[i]; 3481 if (frn->bdi_id == wb->bdi->id && 3482 frn->memcg_id == wb->memcg_css->id) 3483 break; 3484 if (time_before64(frn->at, oldest_at) && 3485 atomic_read(&frn->done.cnt) == 1) { 3486 oldest = i; 3487 oldest_at = frn->at; 3488 } 3489 } 3490 3491 if (i < MEMCG_CGWB_FRN_CNT) { 3492 /* 3493 * Re-using an existing one. Update timestamp lazily to 3494 * avoid making the cacheline hot. We want them to be 3495 * reasonably up-to-date and significantly shorter than 3496 * dirty_expire_interval as that's what expires the record. 3497 * Use the shorter of 1s and dirty_expire_interval / 8. 3498 */ 3499 unsigned long update_intv = 3500 min_t(unsigned long, HZ, 3501 msecs_to_jiffies(dirty_expire_interval * 10) / 8); 3502 3503 if (time_before64(frn->at, now - update_intv)) 3504 frn->at = now; 3505 } else if (oldest >= 0) { 3506 /* replace the oldest free one */ 3507 frn = &memcg->cgwb_frn[oldest]; 3508 frn->bdi_id = wb->bdi->id; 3509 frn->memcg_id = wb->memcg_css->id; 3510 frn->at = now; 3511 } 3512 } 3513 3514 /* issue foreign writeback flushes for recorded foreign dirtying events */ 3515 void mem_cgroup_flush_foreign(struct bdi_writeback *wb) 3516 { 3517 struct mem_cgroup *memcg = mem_cgroup_from_css(wb->memcg_css); 3518 unsigned long intv = msecs_to_jiffies(dirty_expire_interval * 10); 3519 u64 now = jiffies_64; 3520 int i; 3521 3522 for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++) { 3523 struct memcg_cgwb_frn *frn = &memcg->cgwb_frn[i]; 3524 3525 /* 3526 * If the record is older than dirty_expire_interval, 3527 * writeback on it has already started. No need to kick it 3528 * off again. Also, don't start a new one if there's 3529 * already one in flight. 3530 */ 3531 if (time_after64(frn->at, now - intv) && 3532 atomic_read(&frn->done.cnt) == 1) { 3533 frn->at = 0; 3534 trace_flush_foreign(wb, frn->bdi_id, frn->memcg_id); 3535 cgroup_writeback_by_id(frn->bdi_id, frn->memcg_id, 3536 WB_REASON_FOREIGN_FLUSH, 3537 &frn->done); 3538 } 3539 } 3540 } 3541 3542 #else /* CONFIG_CGROUP_WRITEBACK */ 3543 3544 static int memcg_wb_domain_init(struct mem_cgroup *memcg, gfp_t gfp) 3545 { 3546 return 0; 3547 } 3548 3549 static void memcg_wb_domain_exit(struct mem_cgroup *memcg) 3550 { 3551 } 3552 3553 static void memcg_wb_domain_size_changed(struct mem_cgroup *memcg) 3554 { 3555 } 3556 3557 #endif /* CONFIG_CGROUP_WRITEBACK */ 3558 3559 /* 3560 * Private memory cgroup IDR 3561 * 3562 * Swap-out records and page cache shadow entries need to store memcg 3563 * references in constrained space, so we maintain an ID space that is 3564 * limited to 16 bit (MEM_CGROUP_ID_MAX), limiting the total number of 3565 * memory-controlled cgroups to 64k. 3566 * 3567 * However, there usually are many references to the offline CSS after 3568 * the cgroup has been destroyed, such as page cache or reclaimable 3569 * slab objects, that don't need to hang on to the ID. We want to keep 3570 * those dead CSS from occupying IDs, or we might quickly exhaust the 3571 * relatively small ID space and prevent the creation of new cgroups 3572 * even when there are much fewer than 64k cgroups - possibly none. 3573 * 3574 * Maintain a private 16-bit ID space for memcg, and allow the ID to 3575 * be freed and recycled when it's no longer needed, which is usually 3576 * when the CSS is offlined. 3577 * 3578 * The only exception to that are records of swapped out tmpfs/shmem 3579 * pages that need to be attributed to live ancestors on swapin. But 3580 * those references are manageable from userspace. 3581 */ 3582 3583 #define MEM_CGROUP_ID_MAX ((1UL << MEM_CGROUP_ID_SHIFT) - 1) 3584 static DEFINE_XARRAY_ALLOC1(mem_cgroup_ids); 3585 3586 static void mem_cgroup_id_remove(struct mem_cgroup *memcg) 3587 { 3588 if (memcg->id.id > 0) { 3589 xa_erase(&mem_cgroup_ids, memcg->id.id); 3590 memcg->id.id = 0; 3591 } 3592 } 3593 3594 void __maybe_unused mem_cgroup_id_get_many(struct mem_cgroup *memcg, 3595 unsigned int n) 3596 { 3597 refcount_add(n, &memcg->id.ref); 3598 } 3599 3600 static void mem_cgroup_id_put_many(struct mem_cgroup *memcg, unsigned int n) 3601 { 3602 if (refcount_sub_and_test(n, &memcg->id.ref)) { 3603 mem_cgroup_id_remove(memcg); 3604 3605 /* Memcg ID pins CSS */ 3606 css_put(&memcg->css); 3607 } 3608 } 3609 3610 static inline void mem_cgroup_id_put(struct mem_cgroup *memcg) 3611 { 3612 mem_cgroup_id_put_many(memcg, 1); 3613 } 3614 3615 struct mem_cgroup *mem_cgroup_id_get_online(struct mem_cgroup *memcg) 3616 { 3617 while (!refcount_inc_not_zero(&memcg->id.ref)) { 3618 /* 3619 * The root cgroup cannot be destroyed, so it's refcount must 3620 * always be >= 1. 3621 */ 3622 if (WARN_ON_ONCE(mem_cgroup_is_root(memcg))) { 3623 VM_BUG_ON(1); 3624 break; 3625 } 3626 memcg = parent_mem_cgroup(memcg); 3627 if (!memcg) 3628 memcg = root_mem_cgroup; 3629 } 3630 return memcg; 3631 } 3632 3633 /** 3634 * mem_cgroup_from_id - look up a memcg from a memcg id 3635 * @id: the memcg id to look up 3636 * 3637 * Caller must hold rcu_read_lock(). 3638 */ 3639 struct mem_cgroup *mem_cgroup_from_id(unsigned short id) 3640 { 3641 WARN_ON_ONCE(!rcu_read_lock_held()); 3642 return xa_load(&mem_cgroup_ids, id); 3643 } 3644 3645 #ifdef CONFIG_SHRINKER_DEBUG 3646 struct mem_cgroup *mem_cgroup_get_from_ino(unsigned long ino) 3647 { 3648 struct cgroup *cgrp; 3649 struct cgroup_subsys_state *css; 3650 struct mem_cgroup *memcg; 3651 3652 cgrp = cgroup_get_from_id(ino); 3653 if (IS_ERR(cgrp)) 3654 return ERR_CAST(cgrp); 3655 3656 css = cgroup_get_e_css(cgrp, &memory_cgrp_subsys); 3657 if (css) 3658 memcg = container_of(css, struct mem_cgroup, css); 3659 else 3660 memcg = ERR_PTR(-ENOENT); 3661 3662 cgroup_put(cgrp); 3663 3664 return memcg; 3665 } 3666 #endif 3667 3668 static void free_mem_cgroup_per_node_info(struct mem_cgroup_per_node *pn) 3669 { 3670 if (!pn) 3671 return; 3672 3673 free_percpu(pn->lruvec_stats_percpu); 3674 kfree(pn->lruvec_stats); 3675 kfree(pn); 3676 } 3677 3678 static bool alloc_mem_cgroup_per_node_info(struct mem_cgroup *memcg, int node) 3679 { 3680 struct mem_cgroup_per_node *pn; 3681 3682 pn = kmem_cache_alloc_node(memcg_pn_cachep, GFP_KERNEL | __GFP_ZERO, 3683 node); 3684 if (!pn) 3685 return false; 3686 3687 pn->lruvec_stats = kzalloc_node(sizeof(struct lruvec_stats), 3688 GFP_KERNEL_ACCOUNT, node); 3689 if (!pn->lruvec_stats) 3690 goto fail; 3691 3692 pn->lruvec_stats_percpu = alloc_percpu_gfp(struct lruvec_stats_percpu, 3693 GFP_KERNEL_ACCOUNT); 3694 if (!pn->lruvec_stats_percpu) 3695 goto fail; 3696 3697 lruvec_init(&pn->lruvec); 3698 pn->memcg = memcg; 3699 3700 memcg->nodeinfo[node] = pn; 3701 return true; 3702 fail: 3703 free_mem_cgroup_per_node_info(pn); 3704 return false; 3705 } 3706 3707 static void __mem_cgroup_free(struct mem_cgroup *memcg) 3708 { 3709 int node; 3710 3711 obj_cgroup_put(memcg->orig_objcg); 3712 3713 for_each_node(node) 3714 free_mem_cgroup_per_node_info(memcg->nodeinfo[node]); 3715 memcg1_free_events(memcg); 3716 kfree(memcg->vmstats); 3717 free_percpu(memcg->vmstats_percpu); 3718 kfree(memcg); 3719 } 3720 3721 static void mem_cgroup_free(struct mem_cgroup *memcg) 3722 { 3723 lru_gen_exit_memcg(memcg); 3724 memcg_wb_domain_exit(memcg); 3725 __mem_cgroup_free(memcg); 3726 } 3727 3728 static struct mem_cgroup *mem_cgroup_alloc(struct mem_cgroup *parent) 3729 { 3730 struct memcg_vmstats_percpu *statc; 3731 struct memcg_vmstats_percpu __percpu *pstatc_pcpu; 3732 struct mem_cgroup *memcg; 3733 int node, cpu; 3734 int __maybe_unused i; 3735 long error; 3736 3737 memcg = kmem_cache_zalloc(memcg_cachep, GFP_KERNEL); 3738 if (!memcg) 3739 return ERR_PTR(-ENOMEM); 3740 3741 error = xa_alloc(&mem_cgroup_ids, &memcg->id.id, NULL, 3742 XA_LIMIT(1, MEM_CGROUP_ID_MAX), GFP_KERNEL); 3743 if (error) 3744 goto fail; 3745 error = -ENOMEM; 3746 3747 memcg->vmstats = kzalloc(sizeof(struct memcg_vmstats), 3748 GFP_KERNEL_ACCOUNT); 3749 if (!memcg->vmstats) 3750 goto fail; 3751 3752 memcg->vmstats_percpu = alloc_percpu_gfp(struct memcg_vmstats_percpu, 3753 GFP_KERNEL_ACCOUNT); 3754 if (!memcg->vmstats_percpu) 3755 goto fail; 3756 3757 if (!memcg1_alloc_events(memcg)) 3758 goto fail; 3759 3760 for_each_possible_cpu(cpu) { 3761 if (parent) 3762 pstatc_pcpu = parent->vmstats_percpu; 3763 statc = per_cpu_ptr(memcg->vmstats_percpu, cpu); 3764 statc->parent_pcpu = parent ? pstatc_pcpu : NULL; 3765 statc->vmstats = memcg->vmstats; 3766 } 3767 3768 for_each_node(node) 3769 if (!alloc_mem_cgroup_per_node_info(memcg, node)) 3770 goto fail; 3771 3772 if (memcg_wb_domain_init(memcg, GFP_KERNEL)) 3773 goto fail; 3774 3775 INIT_WORK(&memcg->high_work, high_work_func); 3776 vmpressure_init(&memcg->vmpressure); 3777 INIT_LIST_HEAD(&memcg->memory_peaks); 3778 INIT_LIST_HEAD(&memcg->swap_peaks); 3779 spin_lock_init(&memcg->peaks_lock); 3780 memcg->socket_pressure = get_jiffies_64(); 3781 #if BITS_PER_LONG < 64 3782 seqlock_init(&memcg->socket_pressure_seqlock); 3783 #endif 3784 memcg1_memcg_init(memcg); 3785 memcg->kmemcg_id = -1; 3786 INIT_LIST_HEAD(&memcg->objcg_list); 3787 #ifdef CONFIG_CGROUP_WRITEBACK 3788 INIT_LIST_HEAD(&memcg->cgwb_list); 3789 for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++) 3790 memcg->cgwb_frn[i].done = 3791 __WB_COMPLETION_INIT(&memcg_cgwb_frn_waitq); 3792 #endif 3793 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 3794 spin_lock_init(&memcg->deferred_split_queue.split_queue_lock); 3795 INIT_LIST_HEAD(&memcg->deferred_split_queue.split_queue); 3796 memcg->deferred_split_queue.split_queue_len = 0; 3797 #endif 3798 lru_gen_init_memcg(memcg); 3799 return memcg; 3800 fail: 3801 mem_cgroup_id_remove(memcg); 3802 __mem_cgroup_free(memcg); 3803 return ERR_PTR(error); 3804 } 3805 3806 static struct cgroup_subsys_state * __ref 3807 mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) 3808 { 3809 struct mem_cgroup *parent = mem_cgroup_from_css(parent_css); 3810 struct mem_cgroup *memcg, *old_memcg; 3811 bool memcg_on_dfl = cgroup_subsys_on_dfl(memory_cgrp_subsys); 3812 3813 old_memcg = set_active_memcg(parent); 3814 memcg = mem_cgroup_alloc(parent); 3815 set_active_memcg(old_memcg); 3816 if (IS_ERR(memcg)) 3817 return ERR_CAST(memcg); 3818 3819 page_counter_set_high(&memcg->memory, PAGE_COUNTER_MAX); 3820 memcg1_soft_limit_reset(memcg); 3821 #ifdef CONFIG_ZSWAP 3822 memcg->zswap_max = PAGE_COUNTER_MAX; 3823 WRITE_ONCE(memcg->zswap_writeback, true); 3824 #endif 3825 page_counter_set_high(&memcg->swap, PAGE_COUNTER_MAX); 3826 if (parent) { 3827 WRITE_ONCE(memcg->swappiness, mem_cgroup_swappiness(parent)); 3828 3829 page_counter_init(&memcg->memory, &parent->memory, memcg_on_dfl); 3830 page_counter_init(&memcg->swap, &parent->swap, false); 3831 #ifdef CONFIG_MEMCG_V1 3832 memcg->memory.track_failcnt = !memcg_on_dfl; 3833 WRITE_ONCE(memcg->oom_kill_disable, READ_ONCE(parent->oom_kill_disable)); 3834 page_counter_init(&memcg->kmem, &parent->kmem, false); 3835 page_counter_init(&memcg->tcpmem, &parent->tcpmem, false); 3836 #endif 3837 } else { 3838 init_memcg_stats(); 3839 init_memcg_events(); 3840 page_counter_init(&memcg->memory, NULL, true); 3841 page_counter_init(&memcg->swap, NULL, false); 3842 #ifdef CONFIG_MEMCG_V1 3843 page_counter_init(&memcg->kmem, NULL, false); 3844 page_counter_init(&memcg->tcpmem, NULL, false); 3845 #endif 3846 root_mem_cgroup = memcg; 3847 return &memcg->css; 3848 } 3849 3850 if (memcg_on_dfl && !cgroup_memory_nosocket) 3851 static_branch_inc(&memcg_sockets_enabled_key); 3852 3853 if (!cgroup_memory_nobpf) 3854 static_branch_inc(&memcg_bpf_enabled_key); 3855 3856 return &memcg->css; 3857 } 3858 3859 static int mem_cgroup_css_online(struct cgroup_subsys_state *css) 3860 { 3861 struct mem_cgroup *memcg = mem_cgroup_from_css(css); 3862 3863 if (memcg_online_kmem(memcg)) 3864 goto remove_id; 3865 3866 /* 3867 * A memcg must be visible for expand_shrinker_info() 3868 * by the time the maps are allocated. So, we allocate maps 3869 * here, when for_each_mem_cgroup() can't skip it. 3870 */ 3871 if (alloc_shrinker_info(memcg)) 3872 goto offline_kmem; 3873 3874 if (unlikely(mem_cgroup_is_root(memcg)) && !mem_cgroup_disabled()) 3875 queue_delayed_work(system_unbound_wq, &stats_flush_dwork, 3876 FLUSH_TIME); 3877 lru_gen_online_memcg(memcg); 3878 3879 /* Online state pins memcg ID, memcg ID pins CSS */ 3880 refcount_set(&memcg->id.ref, 1); 3881 css_get(css); 3882 3883 /* 3884 * Ensure mem_cgroup_from_id() works once we're fully online. 3885 * 3886 * We could do this earlier and require callers to filter with 3887 * css_tryget_online(). But right now there are no users that 3888 * need earlier access, and the workingset code relies on the 3889 * cgroup tree linkage (mem_cgroup_get_nr_swap_pages()). So 3890 * publish it here at the end of onlining. This matches the 3891 * regular ID destruction during offlining. 3892 */ 3893 xa_store(&mem_cgroup_ids, memcg->id.id, memcg, GFP_KERNEL); 3894 3895 return 0; 3896 offline_kmem: 3897 memcg_offline_kmem(memcg); 3898 remove_id: 3899 mem_cgroup_id_remove(memcg); 3900 return -ENOMEM; 3901 } 3902 3903 static void mem_cgroup_css_offline(struct cgroup_subsys_state *css) 3904 { 3905 struct mem_cgroup *memcg = mem_cgroup_from_css(css); 3906 3907 memcg1_css_offline(memcg); 3908 3909 page_counter_set_min(&memcg->memory, 0); 3910 page_counter_set_low(&memcg->memory, 0); 3911 3912 zswap_memcg_offline_cleanup(memcg); 3913 3914 memcg_offline_kmem(memcg); 3915 reparent_deferred_split_queue(memcg); 3916 reparent_shrinker_deferred(memcg); 3917 wb_memcg_offline(memcg); 3918 lru_gen_offline_memcg(memcg); 3919 3920 drain_all_stock(memcg); 3921 3922 mem_cgroup_id_put(memcg); 3923 } 3924 3925 static void mem_cgroup_css_released(struct cgroup_subsys_state *css) 3926 { 3927 struct mem_cgroup *memcg = mem_cgroup_from_css(css); 3928 3929 invalidate_reclaim_iterators(memcg); 3930 lru_gen_release_memcg(memcg); 3931 } 3932 3933 static void mem_cgroup_css_free(struct cgroup_subsys_state *css) 3934 { 3935 struct mem_cgroup *memcg = mem_cgroup_from_css(css); 3936 int __maybe_unused i; 3937 3938 #ifdef CONFIG_CGROUP_WRITEBACK 3939 for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++) 3940 wb_wait_for_completion(&memcg->cgwb_frn[i].done); 3941 #endif 3942 if (cgroup_subsys_on_dfl(memory_cgrp_subsys) && !cgroup_memory_nosocket) 3943 static_branch_dec(&memcg_sockets_enabled_key); 3944 3945 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && memcg1_tcpmem_active(memcg)) 3946 static_branch_dec(&memcg_sockets_enabled_key); 3947 3948 if (!cgroup_memory_nobpf) 3949 static_branch_dec(&memcg_bpf_enabled_key); 3950 3951 vmpressure_cleanup(&memcg->vmpressure); 3952 cancel_work_sync(&memcg->high_work); 3953 memcg1_remove_from_trees(memcg); 3954 free_shrinker_info(memcg); 3955 mem_cgroup_free(memcg); 3956 } 3957 3958 /** 3959 * mem_cgroup_css_reset - reset the states of a mem_cgroup 3960 * @css: the target css 3961 * 3962 * Reset the states of the mem_cgroup associated with @css. This is 3963 * invoked when the userland requests disabling on the default hierarchy 3964 * but the memcg is pinned through dependency. The memcg should stop 3965 * applying policies and should revert to the vanilla state as it may be 3966 * made visible again. 3967 * 3968 * The current implementation only resets the essential configurations. 3969 * This needs to be expanded to cover all the visible parts. 3970 */ 3971 static void mem_cgroup_css_reset(struct cgroup_subsys_state *css) 3972 { 3973 struct mem_cgroup *memcg = mem_cgroup_from_css(css); 3974 3975 page_counter_set_max(&memcg->memory, PAGE_COUNTER_MAX); 3976 page_counter_set_max(&memcg->swap, PAGE_COUNTER_MAX); 3977 #ifdef CONFIG_MEMCG_V1 3978 page_counter_set_max(&memcg->kmem, PAGE_COUNTER_MAX); 3979 page_counter_set_max(&memcg->tcpmem, PAGE_COUNTER_MAX); 3980 #endif 3981 page_counter_set_min(&memcg->memory, 0); 3982 page_counter_set_low(&memcg->memory, 0); 3983 page_counter_set_high(&memcg->memory, PAGE_COUNTER_MAX); 3984 memcg1_soft_limit_reset(memcg); 3985 page_counter_set_high(&memcg->swap, PAGE_COUNTER_MAX); 3986 memcg_wb_domain_size_changed(memcg); 3987 } 3988 3989 struct aggregate_control { 3990 /* pointer to the aggregated (CPU and subtree aggregated) counters */ 3991 long *aggregate; 3992 /* pointer to the non-hierarchichal (CPU aggregated) counters */ 3993 long *local; 3994 /* pointer to the pending child counters during tree propagation */ 3995 long *pending; 3996 /* pointer to the parent's pending counters, could be NULL */ 3997 long *ppending; 3998 /* pointer to the percpu counters to be aggregated */ 3999 long *cstat; 4000 /* pointer to the percpu counters of the last aggregation*/ 4001 long *cstat_prev; 4002 /* size of the above counters */ 4003 int size; 4004 }; 4005 4006 static void mem_cgroup_stat_aggregate(struct aggregate_control *ac) 4007 { 4008 int i; 4009 long delta, delta_cpu, v; 4010 4011 for (i = 0; i < ac->size; i++) { 4012 /* 4013 * Collect the aggregated propagation counts of groups 4014 * below us. We're in a per-cpu loop here and this is 4015 * a global counter, so the first cycle will get them. 4016 */ 4017 delta = ac->pending[i]; 4018 if (delta) 4019 ac->pending[i] = 0; 4020 4021 /* Add CPU changes on this level since the last flush */ 4022 delta_cpu = 0; 4023 v = READ_ONCE(ac->cstat[i]); 4024 if (v != ac->cstat_prev[i]) { 4025 delta_cpu = v - ac->cstat_prev[i]; 4026 delta += delta_cpu; 4027 ac->cstat_prev[i] = v; 4028 } 4029 4030 /* Aggregate counts on this level and propagate upwards */ 4031 if (delta_cpu) 4032 ac->local[i] += delta_cpu; 4033 4034 if (delta) { 4035 ac->aggregate[i] += delta; 4036 if (ac->ppending) 4037 ac->ppending[i] += delta; 4038 } 4039 } 4040 } 4041 4042 #ifdef CONFIG_MEMCG_NMI_SAFETY_REQUIRES_ATOMIC 4043 static void flush_nmi_stats(struct mem_cgroup *memcg, struct mem_cgroup *parent, 4044 int cpu) 4045 { 4046 int nid; 4047 4048 if (atomic_read(&memcg->kmem_stat)) { 4049 int kmem = atomic_xchg(&memcg->kmem_stat, 0); 4050 int index = memcg_stats_index(MEMCG_KMEM); 4051 4052 memcg->vmstats->state[index] += kmem; 4053 if (parent) 4054 parent->vmstats->state_pending[index] += kmem; 4055 } 4056 4057 for_each_node_state(nid, N_MEMORY) { 4058 struct mem_cgroup_per_node *pn = memcg->nodeinfo[nid]; 4059 struct lruvec_stats *lstats = pn->lruvec_stats; 4060 struct lruvec_stats *plstats = NULL; 4061 4062 if (parent) 4063 plstats = parent->nodeinfo[nid]->lruvec_stats; 4064 4065 if (atomic_read(&pn->slab_reclaimable)) { 4066 int slab = atomic_xchg(&pn->slab_reclaimable, 0); 4067 int index = memcg_stats_index(NR_SLAB_RECLAIMABLE_B); 4068 4069 lstats->state[index] += slab; 4070 if (plstats) 4071 plstats->state_pending[index] += slab; 4072 } 4073 if (atomic_read(&pn->slab_unreclaimable)) { 4074 int slab = atomic_xchg(&pn->slab_unreclaimable, 0); 4075 int index = memcg_stats_index(NR_SLAB_UNRECLAIMABLE_B); 4076 4077 lstats->state[index] += slab; 4078 if (plstats) 4079 plstats->state_pending[index] += slab; 4080 } 4081 } 4082 } 4083 #else 4084 static void flush_nmi_stats(struct mem_cgroup *memcg, struct mem_cgroup *parent, 4085 int cpu) 4086 {} 4087 #endif 4088 4089 static void mem_cgroup_css_rstat_flush(struct cgroup_subsys_state *css, int cpu) 4090 { 4091 struct mem_cgroup *memcg = mem_cgroup_from_css(css); 4092 struct mem_cgroup *parent = parent_mem_cgroup(memcg); 4093 struct memcg_vmstats_percpu *statc; 4094 struct aggregate_control ac; 4095 int nid; 4096 4097 flush_nmi_stats(memcg, parent, cpu); 4098 4099 statc = per_cpu_ptr(memcg->vmstats_percpu, cpu); 4100 4101 ac = (struct aggregate_control) { 4102 .aggregate = memcg->vmstats->state, 4103 .local = memcg->vmstats->state_local, 4104 .pending = memcg->vmstats->state_pending, 4105 .ppending = parent ? parent->vmstats->state_pending : NULL, 4106 .cstat = statc->state, 4107 .cstat_prev = statc->state_prev, 4108 .size = MEMCG_VMSTAT_SIZE, 4109 }; 4110 mem_cgroup_stat_aggregate(&ac); 4111 4112 ac = (struct aggregate_control) { 4113 .aggregate = memcg->vmstats->events, 4114 .local = memcg->vmstats->events_local, 4115 .pending = memcg->vmstats->events_pending, 4116 .ppending = parent ? parent->vmstats->events_pending : NULL, 4117 .cstat = statc->events, 4118 .cstat_prev = statc->events_prev, 4119 .size = NR_MEMCG_EVENTS, 4120 }; 4121 mem_cgroup_stat_aggregate(&ac); 4122 4123 for_each_node_state(nid, N_MEMORY) { 4124 struct mem_cgroup_per_node *pn = memcg->nodeinfo[nid]; 4125 struct lruvec_stats *lstats = pn->lruvec_stats; 4126 struct lruvec_stats *plstats = NULL; 4127 struct lruvec_stats_percpu *lstatc; 4128 4129 if (parent) 4130 plstats = parent->nodeinfo[nid]->lruvec_stats; 4131 4132 lstatc = per_cpu_ptr(pn->lruvec_stats_percpu, cpu); 4133 4134 ac = (struct aggregate_control) { 4135 .aggregate = lstats->state, 4136 .local = lstats->state_local, 4137 .pending = lstats->state_pending, 4138 .ppending = plstats ? plstats->state_pending : NULL, 4139 .cstat = lstatc->state, 4140 .cstat_prev = lstatc->state_prev, 4141 .size = NR_MEMCG_NODE_STAT_ITEMS, 4142 }; 4143 mem_cgroup_stat_aggregate(&ac); 4144 4145 } 4146 WRITE_ONCE(statc->stats_updates, 0); 4147 /* We are in a per-cpu loop here, only do the atomic write once */ 4148 if (atomic_read(&memcg->vmstats->stats_updates)) 4149 atomic_set(&memcg->vmstats->stats_updates, 0); 4150 } 4151 4152 static void mem_cgroup_fork(struct task_struct *task) 4153 { 4154 /* 4155 * Set the update flag to cause task->objcg to be initialized lazily 4156 * on the first allocation. It can be done without any synchronization 4157 * because it's always performed on the current task, so does 4158 * current_objcg_update(). 4159 */ 4160 task->objcg = (struct obj_cgroup *)CURRENT_OBJCG_UPDATE_FLAG; 4161 } 4162 4163 static void mem_cgroup_exit(struct task_struct *task) 4164 { 4165 struct obj_cgroup *objcg = task->objcg; 4166 4167 objcg = (struct obj_cgroup *) 4168 ((unsigned long)objcg & ~CURRENT_OBJCG_UPDATE_FLAG); 4169 obj_cgroup_put(objcg); 4170 4171 /* 4172 * Some kernel allocations can happen after this point, 4173 * but let's ignore them. It can be done without any synchronization 4174 * because it's always performed on the current task, so does 4175 * current_objcg_update(). 4176 */ 4177 task->objcg = NULL; 4178 } 4179 4180 #ifdef CONFIG_LRU_GEN 4181 static void mem_cgroup_lru_gen_attach(struct cgroup_taskset *tset) 4182 { 4183 struct task_struct *task; 4184 struct cgroup_subsys_state *css; 4185 4186 /* find the first leader if there is any */ 4187 cgroup_taskset_for_each_leader(task, css, tset) 4188 break; 4189 4190 if (!task) 4191 return; 4192 4193 task_lock(task); 4194 if (task->mm && READ_ONCE(task->mm->owner) == task) 4195 lru_gen_migrate_mm(task->mm); 4196 task_unlock(task); 4197 } 4198 #else 4199 static void mem_cgroup_lru_gen_attach(struct cgroup_taskset *tset) {} 4200 #endif /* CONFIG_LRU_GEN */ 4201 4202 static void mem_cgroup_kmem_attach(struct cgroup_taskset *tset) 4203 { 4204 struct task_struct *task; 4205 struct cgroup_subsys_state *css; 4206 4207 cgroup_taskset_for_each(task, css, tset) { 4208 /* atomically set the update bit */ 4209 set_bit(CURRENT_OBJCG_UPDATE_BIT, (unsigned long *)&task->objcg); 4210 } 4211 } 4212 4213 static void mem_cgroup_attach(struct cgroup_taskset *tset) 4214 { 4215 mem_cgroup_lru_gen_attach(tset); 4216 mem_cgroup_kmem_attach(tset); 4217 } 4218 4219 static int seq_puts_memcg_tunable(struct seq_file *m, unsigned long value) 4220 { 4221 if (value == PAGE_COUNTER_MAX) 4222 seq_puts(m, "max\n"); 4223 else 4224 seq_printf(m, "%llu\n", (u64)value * PAGE_SIZE); 4225 4226 return 0; 4227 } 4228 4229 static u64 memory_current_read(struct cgroup_subsys_state *css, 4230 struct cftype *cft) 4231 { 4232 struct mem_cgroup *memcg = mem_cgroup_from_css(css); 4233 4234 return (u64)page_counter_read(&memcg->memory) * PAGE_SIZE; 4235 } 4236 4237 #define OFP_PEAK_UNSET (((-1UL))) 4238 4239 static int peak_show(struct seq_file *sf, void *v, struct page_counter *pc) 4240 { 4241 struct cgroup_of_peak *ofp = of_peak(sf->private); 4242 u64 fd_peak = READ_ONCE(ofp->value), peak; 4243 4244 /* User wants global or local peak? */ 4245 if (fd_peak == OFP_PEAK_UNSET) 4246 peak = pc->watermark; 4247 else 4248 peak = max(fd_peak, READ_ONCE(pc->local_watermark)); 4249 4250 seq_printf(sf, "%llu\n", peak * PAGE_SIZE); 4251 return 0; 4252 } 4253 4254 static int memory_peak_show(struct seq_file *sf, void *v) 4255 { 4256 struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(sf)); 4257 4258 return peak_show(sf, v, &memcg->memory); 4259 } 4260 4261 static int peak_open(struct kernfs_open_file *of) 4262 { 4263 struct cgroup_of_peak *ofp = of_peak(of); 4264 4265 ofp->value = OFP_PEAK_UNSET; 4266 return 0; 4267 } 4268 4269 static void peak_release(struct kernfs_open_file *of) 4270 { 4271 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); 4272 struct cgroup_of_peak *ofp = of_peak(of); 4273 4274 if (ofp->value == OFP_PEAK_UNSET) { 4275 /* fast path (no writes on this fd) */ 4276 return; 4277 } 4278 spin_lock(&memcg->peaks_lock); 4279 list_del(&ofp->list); 4280 spin_unlock(&memcg->peaks_lock); 4281 } 4282 4283 static ssize_t peak_write(struct kernfs_open_file *of, char *buf, size_t nbytes, 4284 loff_t off, struct page_counter *pc, 4285 struct list_head *watchers) 4286 { 4287 unsigned long usage; 4288 struct cgroup_of_peak *peer_ctx; 4289 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); 4290 struct cgroup_of_peak *ofp = of_peak(of); 4291 4292 spin_lock(&memcg->peaks_lock); 4293 4294 usage = page_counter_read(pc); 4295 WRITE_ONCE(pc->local_watermark, usage); 4296 4297 list_for_each_entry(peer_ctx, watchers, list) 4298 if (usage > peer_ctx->value) 4299 WRITE_ONCE(peer_ctx->value, usage); 4300 4301 /* initial write, register watcher */ 4302 if (ofp->value == OFP_PEAK_UNSET) 4303 list_add(&ofp->list, watchers); 4304 4305 WRITE_ONCE(ofp->value, usage); 4306 spin_unlock(&memcg->peaks_lock); 4307 4308 return nbytes; 4309 } 4310 4311 static ssize_t memory_peak_write(struct kernfs_open_file *of, char *buf, 4312 size_t nbytes, loff_t off) 4313 { 4314 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); 4315 4316 return peak_write(of, buf, nbytes, off, &memcg->memory, 4317 &memcg->memory_peaks); 4318 } 4319 4320 #undef OFP_PEAK_UNSET 4321 4322 static int memory_min_show(struct seq_file *m, void *v) 4323 { 4324 return seq_puts_memcg_tunable(m, 4325 READ_ONCE(mem_cgroup_from_seq(m)->memory.min)); 4326 } 4327 4328 static ssize_t memory_min_write(struct kernfs_open_file *of, 4329 char *buf, size_t nbytes, loff_t off) 4330 { 4331 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); 4332 unsigned long min; 4333 int err; 4334 4335 buf = strstrip(buf); 4336 err = page_counter_memparse(buf, "max", &min); 4337 if (err) 4338 return err; 4339 4340 page_counter_set_min(&memcg->memory, min); 4341 4342 return nbytes; 4343 } 4344 4345 static int memory_low_show(struct seq_file *m, void *v) 4346 { 4347 return seq_puts_memcg_tunable(m, 4348 READ_ONCE(mem_cgroup_from_seq(m)->memory.low)); 4349 } 4350 4351 static ssize_t memory_low_write(struct kernfs_open_file *of, 4352 char *buf, size_t nbytes, loff_t off) 4353 { 4354 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); 4355 unsigned long low; 4356 int err; 4357 4358 buf = strstrip(buf); 4359 err = page_counter_memparse(buf, "max", &low); 4360 if (err) 4361 return err; 4362 4363 page_counter_set_low(&memcg->memory, low); 4364 4365 return nbytes; 4366 } 4367 4368 static int memory_high_show(struct seq_file *m, void *v) 4369 { 4370 return seq_puts_memcg_tunable(m, 4371 READ_ONCE(mem_cgroup_from_seq(m)->memory.high)); 4372 } 4373 4374 static ssize_t memory_high_write(struct kernfs_open_file *of, 4375 char *buf, size_t nbytes, loff_t off) 4376 { 4377 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); 4378 unsigned int nr_retries = MAX_RECLAIM_RETRIES; 4379 bool drained = false; 4380 unsigned long high; 4381 int err; 4382 4383 buf = strstrip(buf); 4384 err = page_counter_memparse(buf, "max", &high); 4385 if (err) 4386 return err; 4387 4388 page_counter_set_high(&memcg->memory, high); 4389 4390 if (of->file->f_flags & O_NONBLOCK) 4391 goto out; 4392 4393 for (;;) { 4394 unsigned long nr_pages = page_counter_read(&memcg->memory); 4395 unsigned long reclaimed; 4396 4397 if (nr_pages <= high) 4398 break; 4399 4400 if (signal_pending(current)) 4401 break; 4402 4403 if (!drained) { 4404 drain_all_stock(memcg); 4405 drained = true; 4406 continue; 4407 } 4408 4409 reclaimed = try_to_free_mem_cgroup_pages(memcg, nr_pages - high, 4410 GFP_KERNEL, MEMCG_RECLAIM_MAY_SWAP, NULL); 4411 4412 if (!reclaimed && !nr_retries--) 4413 break; 4414 } 4415 out: 4416 memcg_wb_domain_size_changed(memcg); 4417 return nbytes; 4418 } 4419 4420 static int memory_max_show(struct seq_file *m, void *v) 4421 { 4422 return seq_puts_memcg_tunable(m, 4423 READ_ONCE(mem_cgroup_from_seq(m)->memory.max)); 4424 } 4425 4426 static ssize_t memory_max_write(struct kernfs_open_file *of, 4427 char *buf, size_t nbytes, loff_t off) 4428 { 4429 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); 4430 unsigned int nr_reclaims = MAX_RECLAIM_RETRIES; 4431 bool drained = false; 4432 unsigned long max; 4433 int err; 4434 4435 buf = strstrip(buf); 4436 err = page_counter_memparse(buf, "max", &max); 4437 if (err) 4438 return err; 4439 4440 xchg(&memcg->memory.max, max); 4441 4442 if (of->file->f_flags & O_NONBLOCK) 4443 goto out; 4444 4445 for (;;) { 4446 unsigned long nr_pages = page_counter_read(&memcg->memory); 4447 4448 if (nr_pages <= max) 4449 break; 4450 4451 if (signal_pending(current)) 4452 break; 4453 4454 if (!drained) { 4455 drain_all_stock(memcg); 4456 drained = true; 4457 continue; 4458 } 4459 4460 if (nr_reclaims) { 4461 if (!try_to_free_mem_cgroup_pages(memcg, nr_pages - max, 4462 GFP_KERNEL, MEMCG_RECLAIM_MAY_SWAP, NULL)) 4463 nr_reclaims--; 4464 continue; 4465 } 4466 4467 memcg_memory_event(memcg, MEMCG_OOM); 4468 if (!mem_cgroup_out_of_memory(memcg, GFP_KERNEL, 0)) 4469 break; 4470 cond_resched(); 4471 } 4472 out: 4473 memcg_wb_domain_size_changed(memcg); 4474 return nbytes; 4475 } 4476 4477 /* 4478 * Note: don't forget to update the 'samples/cgroup/memcg_event_listener' 4479 * if any new events become available. 4480 */ 4481 static void __memory_events_show(struct seq_file *m, atomic_long_t *events) 4482 { 4483 seq_printf(m, "low %lu\n", atomic_long_read(&events[MEMCG_LOW])); 4484 seq_printf(m, "high %lu\n", atomic_long_read(&events[MEMCG_HIGH])); 4485 seq_printf(m, "max %lu\n", atomic_long_read(&events[MEMCG_MAX])); 4486 seq_printf(m, "oom %lu\n", atomic_long_read(&events[MEMCG_OOM])); 4487 seq_printf(m, "oom_kill %lu\n", 4488 atomic_long_read(&events[MEMCG_OOM_KILL])); 4489 seq_printf(m, "oom_group_kill %lu\n", 4490 atomic_long_read(&events[MEMCG_OOM_GROUP_KILL])); 4491 seq_printf(m, "sock_throttled %lu\n", 4492 atomic_long_read(&events[MEMCG_SOCK_THROTTLED])); 4493 } 4494 4495 static int memory_events_show(struct seq_file *m, void *v) 4496 { 4497 struct mem_cgroup *memcg = mem_cgroup_from_seq(m); 4498 4499 __memory_events_show(m, memcg->memory_events); 4500 return 0; 4501 } 4502 4503 static int memory_events_local_show(struct seq_file *m, void *v) 4504 { 4505 struct mem_cgroup *memcg = mem_cgroup_from_seq(m); 4506 4507 __memory_events_show(m, memcg->memory_events_local); 4508 return 0; 4509 } 4510 4511 int memory_stat_show(struct seq_file *m, void *v) 4512 { 4513 struct mem_cgroup *memcg = mem_cgroup_from_seq(m); 4514 char *buf = kmalloc(SEQ_BUF_SIZE, GFP_KERNEL); 4515 struct seq_buf s; 4516 4517 if (!buf) 4518 return -ENOMEM; 4519 seq_buf_init(&s, buf, SEQ_BUF_SIZE); 4520 memory_stat_format(memcg, &s); 4521 seq_puts(m, buf); 4522 kfree(buf); 4523 return 0; 4524 } 4525 4526 #ifdef CONFIG_NUMA 4527 static inline unsigned long lruvec_page_state_output(struct lruvec *lruvec, 4528 int item) 4529 { 4530 return lruvec_page_state(lruvec, item) * 4531 memcg_page_state_output_unit(item); 4532 } 4533 4534 static int memory_numa_stat_show(struct seq_file *m, void *v) 4535 { 4536 int i; 4537 struct mem_cgroup *memcg = mem_cgroup_from_seq(m); 4538 4539 mem_cgroup_flush_stats(memcg); 4540 4541 for (i = 0; i < ARRAY_SIZE(memory_stats); i++) { 4542 int nid; 4543 4544 if (memory_stats[i].idx >= NR_VM_NODE_STAT_ITEMS) 4545 continue; 4546 4547 seq_printf(m, "%s", memory_stats[i].name); 4548 for_each_node_state(nid, N_MEMORY) { 4549 u64 size; 4550 struct lruvec *lruvec; 4551 4552 lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(nid)); 4553 size = lruvec_page_state_output(lruvec, 4554 memory_stats[i].idx); 4555 seq_printf(m, " N%d=%llu", nid, size); 4556 } 4557 seq_putc(m, '\n'); 4558 } 4559 4560 return 0; 4561 } 4562 #endif 4563 4564 static int memory_oom_group_show(struct seq_file *m, void *v) 4565 { 4566 struct mem_cgroup *memcg = mem_cgroup_from_seq(m); 4567 4568 seq_printf(m, "%d\n", READ_ONCE(memcg->oom_group)); 4569 4570 return 0; 4571 } 4572 4573 static ssize_t memory_oom_group_write(struct kernfs_open_file *of, 4574 char *buf, size_t nbytes, loff_t off) 4575 { 4576 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); 4577 int ret, oom_group; 4578 4579 buf = strstrip(buf); 4580 if (!buf) 4581 return -EINVAL; 4582 4583 ret = kstrtoint(buf, 0, &oom_group); 4584 if (ret) 4585 return ret; 4586 4587 if (oom_group != 0 && oom_group != 1) 4588 return -EINVAL; 4589 4590 WRITE_ONCE(memcg->oom_group, oom_group); 4591 4592 return nbytes; 4593 } 4594 4595 static ssize_t memory_reclaim(struct kernfs_open_file *of, char *buf, 4596 size_t nbytes, loff_t off) 4597 { 4598 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); 4599 int ret; 4600 4601 ret = user_proactive_reclaim(buf, memcg, NULL); 4602 if (ret) 4603 return ret; 4604 4605 return nbytes; 4606 } 4607 4608 static struct cftype memory_files[] = { 4609 { 4610 .name = "current", 4611 .flags = CFTYPE_NOT_ON_ROOT, 4612 .read_u64 = memory_current_read, 4613 }, 4614 { 4615 .name = "peak", 4616 .flags = CFTYPE_NOT_ON_ROOT, 4617 .open = peak_open, 4618 .release = peak_release, 4619 .seq_show = memory_peak_show, 4620 .write = memory_peak_write, 4621 }, 4622 { 4623 .name = "min", 4624 .flags = CFTYPE_NOT_ON_ROOT, 4625 .seq_show = memory_min_show, 4626 .write = memory_min_write, 4627 }, 4628 { 4629 .name = "low", 4630 .flags = CFTYPE_NOT_ON_ROOT, 4631 .seq_show = memory_low_show, 4632 .write = memory_low_write, 4633 }, 4634 { 4635 .name = "high", 4636 .flags = CFTYPE_NOT_ON_ROOT, 4637 .seq_show = memory_high_show, 4638 .write = memory_high_write, 4639 }, 4640 { 4641 .name = "max", 4642 .flags = CFTYPE_NOT_ON_ROOT, 4643 .seq_show = memory_max_show, 4644 .write = memory_max_write, 4645 }, 4646 { 4647 .name = "events", 4648 .flags = CFTYPE_NOT_ON_ROOT, 4649 .file_offset = offsetof(struct mem_cgroup, events_file), 4650 .seq_show = memory_events_show, 4651 }, 4652 { 4653 .name = "events.local", 4654 .flags = CFTYPE_NOT_ON_ROOT, 4655 .file_offset = offsetof(struct mem_cgroup, events_local_file), 4656 .seq_show = memory_events_local_show, 4657 }, 4658 { 4659 .name = "stat", 4660 .seq_show = memory_stat_show, 4661 }, 4662 #ifdef CONFIG_NUMA 4663 { 4664 .name = "numa_stat", 4665 .seq_show = memory_numa_stat_show, 4666 }, 4667 #endif 4668 { 4669 .name = "oom.group", 4670 .flags = CFTYPE_NOT_ON_ROOT | CFTYPE_NS_DELEGATABLE, 4671 .seq_show = memory_oom_group_show, 4672 .write = memory_oom_group_write, 4673 }, 4674 { 4675 .name = "reclaim", 4676 .flags = CFTYPE_NS_DELEGATABLE, 4677 .write = memory_reclaim, 4678 }, 4679 { } /* terminate */ 4680 }; 4681 4682 struct cgroup_subsys memory_cgrp_subsys = { 4683 .css_alloc = mem_cgroup_css_alloc, 4684 .css_online = mem_cgroup_css_online, 4685 .css_offline = mem_cgroup_css_offline, 4686 .css_released = mem_cgroup_css_released, 4687 .css_free = mem_cgroup_css_free, 4688 .css_reset = mem_cgroup_css_reset, 4689 .css_rstat_flush = mem_cgroup_css_rstat_flush, 4690 .attach = mem_cgroup_attach, 4691 .fork = mem_cgroup_fork, 4692 .exit = mem_cgroup_exit, 4693 .dfl_cftypes = memory_files, 4694 #ifdef CONFIG_MEMCG_V1 4695 .legacy_cftypes = mem_cgroup_legacy_files, 4696 #endif 4697 .early_init = 0, 4698 }; 4699 4700 /** 4701 * mem_cgroup_calculate_protection - check if memory consumption is in the normal range 4702 * @root: the top ancestor of the sub-tree being checked 4703 * @memcg: the memory cgroup to check 4704 * 4705 * WARNING: This function is not stateless! It can only be used as part 4706 * of a top-down tree iteration, not for isolated queries. 4707 */ 4708 void mem_cgroup_calculate_protection(struct mem_cgroup *root, 4709 struct mem_cgroup *memcg) 4710 { 4711 bool recursive_protection = 4712 cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_RECURSIVE_PROT; 4713 4714 if (mem_cgroup_disabled()) 4715 return; 4716 4717 if (!root) 4718 root = root_mem_cgroup; 4719 4720 page_counter_calculate_protection(&root->memory, &memcg->memory, recursive_protection); 4721 } 4722 4723 static int charge_memcg(struct folio *folio, struct mem_cgroup *memcg, 4724 gfp_t gfp) 4725 { 4726 int ret; 4727 4728 ret = try_charge(memcg, gfp, folio_nr_pages(folio)); 4729 if (ret) 4730 goto out; 4731 4732 css_get(&memcg->css); 4733 commit_charge(folio, memcg); 4734 memcg1_commit_charge(folio, memcg); 4735 out: 4736 return ret; 4737 } 4738 4739 int __mem_cgroup_charge(struct folio *folio, struct mm_struct *mm, gfp_t gfp) 4740 { 4741 struct mem_cgroup *memcg; 4742 int ret; 4743 4744 memcg = get_mem_cgroup_from_mm(mm); 4745 ret = charge_memcg(folio, memcg, gfp); 4746 css_put(&memcg->css); 4747 4748 return ret; 4749 } 4750 4751 /** 4752 * mem_cgroup_charge_hugetlb - charge the memcg for a hugetlb folio 4753 * @folio: folio being charged 4754 * @gfp: reclaim mode 4755 * 4756 * This function is called when allocating a huge page folio, after the page has 4757 * already been obtained and charged to the appropriate hugetlb cgroup 4758 * controller (if it is enabled). 4759 * 4760 * Returns ENOMEM if the memcg is already full. 4761 * Returns 0 if either the charge was successful, or if we skip the charging. 4762 */ 4763 int mem_cgroup_charge_hugetlb(struct folio *folio, gfp_t gfp) 4764 { 4765 struct mem_cgroup *memcg = get_mem_cgroup_from_current(); 4766 int ret = 0; 4767 4768 /* 4769 * Even memcg does not account for hugetlb, we still want to update 4770 * system-level stats via lruvec_stat_mod_folio. Return 0, and skip 4771 * charging the memcg. 4772 */ 4773 if (mem_cgroup_disabled() || !memcg_accounts_hugetlb() || 4774 !memcg || !cgroup_subsys_on_dfl(memory_cgrp_subsys)) 4775 goto out; 4776 4777 if (charge_memcg(folio, memcg, gfp)) 4778 ret = -ENOMEM; 4779 4780 out: 4781 mem_cgroup_put(memcg); 4782 return ret; 4783 } 4784 4785 /** 4786 * mem_cgroup_swapin_charge_folio - Charge a newly allocated folio for swapin. 4787 * @folio: folio to charge. 4788 * @mm: mm context of the victim 4789 * @gfp: reclaim mode 4790 * @entry: swap entry for which the folio is allocated 4791 * 4792 * This function charges a folio allocated for swapin. Please call this before 4793 * adding the folio to the swapcache. 4794 * 4795 * Returns 0 on success. Otherwise, an error code is returned. 4796 */ 4797 int mem_cgroup_swapin_charge_folio(struct folio *folio, struct mm_struct *mm, 4798 gfp_t gfp, swp_entry_t entry) 4799 { 4800 struct mem_cgroup *memcg; 4801 unsigned short id; 4802 int ret; 4803 4804 if (mem_cgroup_disabled()) 4805 return 0; 4806 4807 id = lookup_swap_cgroup_id(entry); 4808 rcu_read_lock(); 4809 memcg = mem_cgroup_from_id(id); 4810 if (!memcg || !css_tryget_online(&memcg->css)) 4811 memcg = get_mem_cgroup_from_mm(mm); 4812 rcu_read_unlock(); 4813 4814 ret = charge_memcg(folio, memcg, gfp); 4815 4816 css_put(&memcg->css); 4817 return ret; 4818 } 4819 4820 struct uncharge_gather { 4821 struct mem_cgroup *memcg; 4822 unsigned long nr_memory; 4823 unsigned long pgpgout; 4824 unsigned long nr_kmem; 4825 int nid; 4826 }; 4827 4828 static inline void uncharge_gather_clear(struct uncharge_gather *ug) 4829 { 4830 memset(ug, 0, sizeof(*ug)); 4831 } 4832 4833 static void uncharge_batch(const struct uncharge_gather *ug) 4834 { 4835 if (ug->nr_memory) { 4836 memcg_uncharge(ug->memcg, ug->nr_memory); 4837 if (ug->nr_kmem) { 4838 mod_memcg_state(ug->memcg, MEMCG_KMEM, -ug->nr_kmem); 4839 memcg1_account_kmem(ug->memcg, -ug->nr_kmem); 4840 } 4841 memcg1_oom_recover(ug->memcg); 4842 } 4843 4844 memcg1_uncharge_batch(ug->memcg, ug->pgpgout, ug->nr_memory, ug->nid); 4845 4846 /* drop reference from uncharge_folio */ 4847 css_put(&ug->memcg->css); 4848 } 4849 4850 static void uncharge_folio(struct folio *folio, struct uncharge_gather *ug) 4851 { 4852 long nr_pages; 4853 struct mem_cgroup *memcg; 4854 struct obj_cgroup *objcg; 4855 4856 VM_BUG_ON_FOLIO(folio_test_lru(folio), folio); 4857 4858 /* 4859 * Nobody should be changing or seriously looking at 4860 * folio memcg or objcg at this point, we have fully 4861 * exclusive access to the folio. 4862 */ 4863 if (folio_memcg_kmem(folio)) { 4864 objcg = __folio_objcg(folio); 4865 /* 4866 * This get matches the put at the end of the function and 4867 * kmem pages do not hold memcg references anymore. 4868 */ 4869 memcg = get_mem_cgroup_from_objcg(objcg); 4870 } else { 4871 memcg = __folio_memcg(folio); 4872 } 4873 4874 if (!memcg) 4875 return; 4876 4877 if (ug->memcg != memcg) { 4878 if (ug->memcg) { 4879 uncharge_batch(ug); 4880 uncharge_gather_clear(ug); 4881 } 4882 ug->memcg = memcg; 4883 ug->nid = folio_nid(folio); 4884 4885 /* pairs with css_put in uncharge_batch */ 4886 css_get(&memcg->css); 4887 } 4888 4889 nr_pages = folio_nr_pages(folio); 4890 4891 if (folio_memcg_kmem(folio)) { 4892 ug->nr_memory += nr_pages; 4893 ug->nr_kmem += nr_pages; 4894 4895 folio->memcg_data = 0; 4896 obj_cgroup_put(objcg); 4897 } else { 4898 /* LRU pages aren't accounted at the root level */ 4899 if (!mem_cgroup_is_root(memcg)) 4900 ug->nr_memory += nr_pages; 4901 ug->pgpgout++; 4902 4903 WARN_ON_ONCE(folio_unqueue_deferred_split(folio)); 4904 folio->memcg_data = 0; 4905 } 4906 4907 css_put(&memcg->css); 4908 } 4909 4910 void __mem_cgroup_uncharge(struct folio *folio) 4911 { 4912 struct uncharge_gather ug; 4913 4914 /* Don't touch folio->lru of any random page, pre-check: */ 4915 if (!folio_memcg_charged(folio)) 4916 return; 4917 4918 uncharge_gather_clear(&ug); 4919 uncharge_folio(folio, &ug); 4920 uncharge_batch(&ug); 4921 } 4922 4923 void __mem_cgroup_uncharge_folios(struct folio_batch *folios) 4924 { 4925 struct uncharge_gather ug; 4926 unsigned int i; 4927 4928 uncharge_gather_clear(&ug); 4929 for (i = 0; i < folios->nr; i++) 4930 uncharge_folio(folios->folios[i], &ug); 4931 if (ug.memcg) 4932 uncharge_batch(&ug); 4933 } 4934 4935 /** 4936 * mem_cgroup_replace_folio - Charge a folio's replacement. 4937 * @old: Currently circulating folio. 4938 * @new: Replacement folio. 4939 * 4940 * Charge @new as a replacement folio for @old. @old will 4941 * be uncharged upon free. 4942 * 4943 * Both folios must be locked, @new->mapping must be set up. 4944 */ 4945 void mem_cgroup_replace_folio(struct folio *old, struct folio *new) 4946 { 4947 struct mem_cgroup *memcg; 4948 long nr_pages = folio_nr_pages(new); 4949 4950 VM_BUG_ON_FOLIO(!folio_test_locked(old), old); 4951 VM_BUG_ON_FOLIO(!folio_test_locked(new), new); 4952 VM_BUG_ON_FOLIO(folio_test_anon(old) != folio_test_anon(new), new); 4953 VM_BUG_ON_FOLIO(folio_nr_pages(old) != nr_pages, new); 4954 4955 if (mem_cgroup_disabled()) 4956 return; 4957 4958 /* Page cache replacement: new folio already charged? */ 4959 if (folio_memcg_charged(new)) 4960 return; 4961 4962 memcg = folio_memcg(old); 4963 VM_WARN_ON_ONCE_FOLIO(!memcg, old); 4964 if (!memcg) 4965 return; 4966 4967 /* Force-charge the new page. The old one will be freed soon */ 4968 if (!mem_cgroup_is_root(memcg)) { 4969 page_counter_charge(&memcg->memory, nr_pages); 4970 if (do_memsw_account()) 4971 page_counter_charge(&memcg->memsw, nr_pages); 4972 } 4973 4974 css_get(&memcg->css); 4975 commit_charge(new, memcg); 4976 memcg1_commit_charge(new, memcg); 4977 } 4978 4979 /** 4980 * mem_cgroup_migrate - Transfer the memcg data from the old to the new folio. 4981 * @old: Currently circulating folio. 4982 * @new: Replacement folio. 4983 * 4984 * Transfer the memcg data from the old folio to the new folio for migration. 4985 * The old folio's data info will be cleared. Note that the memory counters 4986 * will remain unchanged throughout the process. 4987 * 4988 * Both folios must be locked, @new->mapping must be set up. 4989 */ 4990 void mem_cgroup_migrate(struct folio *old, struct folio *new) 4991 { 4992 struct mem_cgroup *memcg; 4993 4994 VM_BUG_ON_FOLIO(!folio_test_locked(old), old); 4995 VM_BUG_ON_FOLIO(!folio_test_locked(new), new); 4996 VM_BUG_ON_FOLIO(folio_test_anon(old) != folio_test_anon(new), new); 4997 VM_BUG_ON_FOLIO(folio_nr_pages(old) != folio_nr_pages(new), new); 4998 VM_BUG_ON_FOLIO(folio_test_lru(old), old); 4999 5000 if (mem_cgroup_disabled()) 5001 return; 5002 5003 memcg = folio_memcg(old); 5004 /* 5005 * Note that it is normal to see !memcg for a hugetlb folio. 5006 * For e.g, itt could have been allocated when memory_hugetlb_accounting 5007 * was not selected. 5008 */ 5009 VM_WARN_ON_ONCE_FOLIO(!folio_test_hugetlb(old) && !memcg, old); 5010 if (!memcg) 5011 return; 5012 5013 /* Transfer the charge and the css ref */ 5014 commit_charge(new, memcg); 5015 5016 /* Warning should never happen, so don't worry about refcount non-0 */ 5017 WARN_ON_ONCE(folio_unqueue_deferred_split(old)); 5018 old->memcg_data = 0; 5019 } 5020 5021 DEFINE_STATIC_KEY_FALSE(memcg_sockets_enabled_key); 5022 EXPORT_SYMBOL(memcg_sockets_enabled_key); 5023 5024 void mem_cgroup_sk_alloc(struct sock *sk) 5025 { 5026 struct mem_cgroup *memcg; 5027 5028 if (!mem_cgroup_sockets_enabled) 5029 return; 5030 5031 /* Do not associate the sock with unrelated interrupted task's memcg. */ 5032 if (!in_task()) 5033 return; 5034 5035 rcu_read_lock(); 5036 memcg = mem_cgroup_from_task(current); 5037 if (mem_cgroup_is_root(memcg)) 5038 goto out; 5039 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && !memcg1_tcpmem_active(memcg)) 5040 goto out; 5041 if (css_tryget(&memcg->css)) 5042 sk->sk_memcg = memcg; 5043 out: 5044 rcu_read_unlock(); 5045 } 5046 5047 void mem_cgroup_sk_free(struct sock *sk) 5048 { 5049 struct mem_cgroup *memcg = mem_cgroup_from_sk(sk); 5050 5051 if (memcg) 5052 css_put(&memcg->css); 5053 } 5054 5055 void mem_cgroup_sk_inherit(const struct sock *sk, struct sock *newsk) 5056 { 5057 struct mem_cgroup *memcg; 5058 5059 if (sk->sk_memcg == newsk->sk_memcg) 5060 return; 5061 5062 mem_cgroup_sk_free(newsk); 5063 5064 memcg = mem_cgroup_from_sk(sk); 5065 if (memcg) 5066 css_get(&memcg->css); 5067 5068 newsk->sk_memcg = sk->sk_memcg; 5069 } 5070 5071 /** 5072 * mem_cgroup_sk_charge - charge socket memory 5073 * @sk: socket in memcg to charge 5074 * @nr_pages: number of pages to charge 5075 * @gfp_mask: reclaim mode 5076 * 5077 * Charges @nr_pages to @memcg. Returns %true if the charge fit within 5078 * @memcg's configured limit, %false if it doesn't. 5079 */ 5080 bool mem_cgroup_sk_charge(const struct sock *sk, unsigned int nr_pages, 5081 gfp_t gfp_mask) 5082 { 5083 struct mem_cgroup *memcg = mem_cgroup_from_sk(sk); 5084 5085 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) 5086 return memcg1_charge_skmem(memcg, nr_pages, gfp_mask); 5087 5088 if (try_charge_memcg(memcg, gfp_mask, nr_pages) == 0) { 5089 mod_memcg_state(memcg, MEMCG_SOCK, nr_pages); 5090 return true; 5091 } 5092 5093 return false; 5094 } 5095 5096 /** 5097 * mem_cgroup_sk_uncharge - uncharge socket memory 5098 * @sk: socket in memcg to uncharge 5099 * @nr_pages: number of pages to uncharge 5100 */ 5101 void mem_cgroup_sk_uncharge(const struct sock *sk, unsigned int nr_pages) 5102 { 5103 struct mem_cgroup *memcg = mem_cgroup_from_sk(sk); 5104 5105 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) { 5106 memcg1_uncharge_skmem(memcg, nr_pages); 5107 return; 5108 } 5109 5110 mod_memcg_state(memcg, MEMCG_SOCK, -nr_pages); 5111 5112 refill_stock(memcg, nr_pages); 5113 } 5114 5115 static int __init cgroup_memory(char *s) 5116 { 5117 char *token; 5118 5119 while ((token = strsep(&s, ",")) != NULL) { 5120 if (!*token) 5121 continue; 5122 if (!strcmp(token, "nosocket")) 5123 cgroup_memory_nosocket = true; 5124 if (!strcmp(token, "nokmem")) 5125 cgroup_memory_nokmem = true; 5126 if (!strcmp(token, "nobpf")) 5127 cgroup_memory_nobpf = true; 5128 } 5129 return 1; 5130 } 5131 __setup("cgroup.memory=", cgroup_memory); 5132 5133 /* 5134 * Memory controller init before cgroup_init() initialize root_mem_cgroup. 5135 * 5136 * Some parts like memcg_hotplug_cpu_dead() have to be initialized from this 5137 * context because of lock dependencies (cgroup_lock -> cpu hotplug) but 5138 * basically everything that doesn't depend on a specific mem_cgroup structure 5139 * should be initialized from here. 5140 */ 5141 int __init mem_cgroup_init(void) 5142 { 5143 unsigned int memcg_size; 5144 int cpu; 5145 5146 /* 5147 * Currently s32 type (can refer to struct batched_lruvec_stat) is 5148 * used for per-memcg-per-cpu caching of per-node statistics. In order 5149 * to work fine, we should make sure that the overfill threshold can't 5150 * exceed S32_MAX / PAGE_SIZE. 5151 */ 5152 BUILD_BUG_ON(MEMCG_CHARGE_BATCH > S32_MAX / PAGE_SIZE); 5153 5154 cpuhp_setup_state_nocalls(CPUHP_MM_MEMCQ_DEAD, "mm/memctrl:dead", NULL, 5155 memcg_hotplug_cpu_dead); 5156 5157 for_each_possible_cpu(cpu) { 5158 INIT_WORK(&per_cpu_ptr(&memcg_stock, cpu)->work, 5159 drain_local_memcg_stock); 5160 INIT_WORK(&per_cpu_ptr(&obj_stock, cpu)->work, 5161 drain_local_obj_stock); 5162 } 5163 5164 memcg_size = struct_size_t(struct mem_cgroup, nodeinfo, nr_node_ids); 5165 memcg_cachep = kmem_cache_create("mem_cgroup", memcg_size, 0, 5166 SLAB_PANIC | SLAB_HWCACHE_ALIGN, NULL); 5167 5168 memcg_pn_cachep = KMEM_CACHE(mem_cgroup_per_node, 5169 SLAB_PANIC | SLAB_HWCACHE_ALIGN); 5170 5171 return 0; 5172 } 5173 5174 #ifdef CONFIG_SWAP 5175 /** 5176 * __mem_cgroup_try_charge_swap - try charging swap space for a folio 5177 * @folio: folio being added to swap 5178 * @entry: swap entry to charge 5179 * 5180 * Try to charge @folio's memcg for the swap space at @entry. 5181 * 5182 * Returns 0 on success, -ENOMEM on failure. 5183 */ 5184 int __mem_cgroup_try_charge_swap(struct folio *folio, swp_entry_t entry) 5185 { 5186 unsigned int nr_pages = folio_nr_pages(folio); 5187 struct page_counter *counter; 5188 struct mem_cgroup *memcg; 5189 5190 if (do_memsw_account()) 5191 return 0; 5192 5193 memcg = folio_memcg(folio); 5194 5195 VM_WARN_ON_ONCE_FOLIO(!memcg, folio); 5196 if (!memcg) 5197 return 0; 5198 5199 if (!entry.val) { 5200 memcg_memory_event(memcg, MEMCG_SWAP_FAIL); 5201 return 0; 5202 } 5203 5204 memcg = mem_cgroup_id_get_online(memcg); 5205 5206 if (!mem_cgroup_is_root(memcg) && 5207 !page_counter_try_charge(&memcg->swap, nr_pages, &counter)) { 5208 memcg_memory_event(memcg, MEMCG_SWAP_MAX); 5209 memcg_memory_event(memcg, MEMCG_SWAP_FAIL); 5210 mem_cgroup_id_put(memcg); 5211 return -ENOMEM; 5212 } 5213 5214 /* Get references for the tail pages, too */ 5215 if (nr_pages > 1) 5216 mem_cgroup_id_get_many(memcg, nr_pages - 1); 5217 mod_memcg_state(memcg, MEMCG_SWAP, nr_pages); 5218 5219 swap_cgroup_record(folio, mem_cgroup_id(memcg), entry); 5220 5221 return 0; 5222 } 5223 5224 /** 5225 * __mem_cgroup_uncharge_swap - uncharge swap space 5226 * @entry: swap entry to uncharge 5227 * @nr_pages: the amount of swap space to uncharge 5228 */ 5229 void __mem_cgroup_uncharge_swap(swp_entry_t entry, unsigned int nr_pages) 5230 { 5231 struct mem_cgroup *memcg; 5232 unsigned short id; 5233 5234 id = swap_cgroup_clear(entry, nr_pages); 5235 rcu_read_lock(); 5236 memcg = mem_cgroup_from_id(id); 5237 if (memcg) { 5238 if (!mem_cgroup_is_root(memcg)) { 5239 if (do_memsw_account()) 5240 page_counter_uncharge(&memcg->memsw, nr_pages); 5241 else 5242 page_counter_uncharge(&memcg->swap, nr_pages); 5243 } 5244 mod_memcg_state(memcg, MEMCG_SWAP, -nr_pages); 5245 mem_cgroup_id_put_many(memcg, nr_pages); 5246 } 5247 rcu_read_unlock(); 5248 } 5249 5250 long mem_cgroup_get_nr_swap_pages(struct mem_cgroup *memcg) 5251 { 5252 long nr_swap_pages = get_nr_swap_pages(); 5253 5254 if (mem_cgroup_disabled() || do_memsw_account()) 5255 return nr_swap_pages; 5256 for (; !mem_cgroup_is_root(memcg); memcg = parent_mem_cgroup(memcg)) 5257 nr_swap_pages = min_t(long, nr_swap_pages, 5258 READ_ONCE(memcg->swap.max) - 5259 page_counter_read(&memcg->swap)); 5260 return nr_swap_pages; 5261 } 5262 5263 bool mem_cgroup_swap_full(struct folio *folio) 5264 { 5265 struct mem_cgroup *memcg; 5266 5267 VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); 5268 5269 if (vm_swap_full()) 5270 return true; 5271 if (do_memsw_account()) 5272 return false; 5273 5274 memcg = folio_memcg(folio); 5275 if (!memcg) 5276 return false; 5277 5278 for (; !mem_cgroup_is_root(memcg); memcg = parent_mem_cgroup(memcg)) { 5279 unsigned long usage = page_counter_read(&memcg->swap); 5280 5281 if (usage * 2 >= READ_ONCE(memcg->swap.high) || 5282 usage * 2 >= READ_ONCE(memcg->swap.max)) 5283 return true; 5284 } 5285 5286 return false; 5287 } 5288 5289 static int __init setup_swap_account(char *s) 5290 { 5291 bool res; 5292 5293 if (!kstrtobool(s, &res) && !res) 5294 pr_warn_once("The swapaccount=0 commandline option is deprecated " 5295 "in favor of configuring swap control via cgroupfs. " 5296 "Please report your usecase to linux-mm@kvack.org if you " 5297 "depend on this functionality.\n"); 5298 return 1; 5299 } 5300 __setup("swapaccount=", setup_swap_account); 5301 5302 static u64 swap_current_read(struct cgroup_subsys_state *css, 5303 struct cftype *cft) 5304 { 5305 struct mem_cgroup *memcg = mem_cgroup_from_css(css); 5306 5307 return (u64)page_counter_read(&memcg->swap) * PAGE_SIZE; 5308 } 5309 5310 static int swap_peak_show(struct seq_file *sf, void *v) 5311 { 5312 struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(sf)); 5313 5314 return peak_show(sf, v, &memcg->swap); 5315 } 5316 5317 static ssize_t swap_peak_write(struct kernfs_open_file *of, char *buf, 5318 size_t nbytes, loff_t off) 5319 { 5320 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); 5321 5322 return peak_write(of, buf, nbytes, off, &memcg->swap, 5323 &memcg->swap_peaks); 5324 } 5325 5326 static int swap_high_show(struct seq_file *m, void *v) 5327 { 5328 return seq_puts_memcg_tunable(m, 5329 READ_ONCE(mem_cgroup_from_seq(m)->swap.high)); 5330 } 5331 5332 static ssize_t swap_high_write(struct kernfs_open_file *of, 5333 char *buf, size_t nbytes, loff_t off) 5334 { 5335 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); 5336 unsigned long high; 5337 int err; 5338 5339 buf = strstrip(buf); 5340 err = page_counter_memparse(buf, "max", &high); 5341 if (err) 5342 return err; 5343 5344 page_counter_set_high(&memcg->swap, high); 5345 5346 return nbytes; 5347 } 5348 5349 static int swap_max_show(struct seq_file *m, void *v) 5350 { 5351 return seq_puts_memcg_tunable(m, 5352 READ_ONCE(mem_cgroup_from_seq(m)->swap.max)); 5353 } 5354 5355 static ssize_t swap_max_write(struct kernfs_open_file *of, 5356 char *buf, size_t nbytes, loff_t off) 5357 { 5358 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); 5359 unsigned long max; 5360 int err; 5361 5362 buf = strstrip(buf); 5363 err = page_counter_memparse(buf, "max", &max); 5364 if (err) 5365 return err; 5366 5367 xchg(&memcg->swap.max, max); 5368 5369 return nbytes; 5370 } 5371 5372 static int swap_events_show(struct seq_file *m, void *v) 5373 { 5374 struct mem_cgroup *memcg = mem_cgroup_from_seq(m); 5375 5376 seq_printf(m, "high %lu\n", 5377 atomic_long_read(&memcg->memory_events[MEMCG_SWAP_HIGH])); 5378 seq_printf(m, "max %lu\n", 5379 atomic_long_read(&memcg->memory_events[MEMCG_SWAP_MAX])); 5380 seq_printf(m, "fail %lu\n", 5381 atomic_long_read(&memcg->memory_events[MEMCG_SWAP_FAIL])); 5382 5383 return 0; 5384 } 5385 5386 static struct cftype swap_files[] = { 5387 { 5388 .name = "swap.current", 5389 .flags = CFTYPE_NOT_ON_ROOT, 5390 .read_u64 = swap_current_read, 5391 }, 5392 { 5393 .name = "swap.high", 5394 .flags = CFTYPE_NOT_ON_ROOT, 5395 .seq_show = swap_high_show, 5396 .write = swap_high_write, 5397 }, 5398 { 5399 .name = "swap.max", 5400 .flags = CFTYPE_NOT_ON_ROOT, 5401 .seq_show = swap_max_show, 5402 .write = swap_max_write, 5403 }, 5404 { 5405 .name = "swap.peak", 5406 .flags = CFTYPE_NOT_ON_ROOT, 5407 .open = peak_open, 5408 .release = peak_release, 5409 .seq_show = swap_peak_show, 5410 .write = swap_peak_write, 5411 }, 5412 { 5413 .name = "swap.events", 5414 .flags = CFTYPE_NOT_ON_ROOT, 5415 .file_offset = offsetof(struct mem_cgroup, swap_events_file), 5416 .seq_show = swap_events_show, 5417 }, 5418 { } /* terminate */ 5419 }; 5420 5421 #ifdef CONFIG_ZSWAP 5422 /** 5423 * obj_cgroup_may_zswap - check if this cgroup can zswap 5424 * @objcg: the object cgroup 5425 * 5426 * Check if the hierarchical zswap limit has been reached. 5427 * 5428 * This doesn't check for specific headroom, and it is not atomic 5429 * either. But with zswap, the size of the allocation is only known 5430 * once compression has occurred, and this optimistic pre-check avoids 5431 * spending cycles on compression when there is already no room left 5432 * or zswap is disabled altogether somewhere in the hierarchy. 5433 */ 5434 bool obj_cgroup_may_zswap(struct obj_cgroup *objcg) 5435 { 5436 struct mem_cgroup *memcg, *original_memcg; 5437 bool ret = true; 5438 5439 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) 5440 return true; 5441 5442 original_memcg = get_mem_cgroup_from_objcg(objcg); 5443 for (memcg = original_memcg; !mem_cgroup_is_root(memcg); 5444 memcg = parent_mem_cgroup(memcg)) { 5445 unsigned long max = READ_ONCE(memcg->zswap_max); 5446 unsigned long pages; 5447 5448 if (max == PAGE_COUNTER_MAX) 5449 continue; 5450 if (max == 0) { 5451 ret = false; 5452 break; 5453 } 5454 5455 /* Force flush to get accurate stats for charging */ 5456 __mem_cgroup_flush_stats(memcg, true); 5457 pages = memcg_page_state(memcg, MEMCG_ZSWAP_B) / PAGE_SIZE; 5458 if (pages < max) 5459 continue; 5460 ret = false; 5461 break; 5462 } 5463 mem_cgroup_put(original_memcg); 5464 return ret; 5465 } 5466 5467 /** 5468 * obj_cgroup_charge_zswap - charge compression backend memory 5469 * @objcg: the object cgroup 5470 * @size: size of compressed object 5471 * 5472 * This forces the charge after obj_cgroup_may_zswap() allowed 5473 * compression and storage in zswap for this cgroup to go ahead. 5474 */ 5475 void obj_cgroup_charge_zswap(struct obj_cgroup *objcg, size_t size) 5476 { 5477 struct mem_cgroup *memcg; 5478 5479 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) 5480 return; 5481 5482 VM_WARN_ON_ONCE(!(current->flags & PF_MEMALLOC)); 5483 5484 /* PF_MEMALLOC context, charging must succeed */ 5485 if (obj_cgroup_charge(objcg, GFP_KERNEL, size)) 5486 VM_WARN_ON_ONCE(1); 5487 5488 rcu_read_lock(); 5489 memcg = obj_cgroup_memcg(objcg); 5490 mod_memcg_state(memcg, MEMCG_ZSWAP_B, size); 5491 mod_memcg_state(memcg, MEMCG_ZSWAPPED, 1); 5492 rcu_read_unlock(); 5493 } 5494 5495 /** 5496 * obj_cgroup_uncharge_zswap - uncharge compression backend memory 5497 * @objcg: the object cgroup 5498 * @size: size of compressed object 5499 * 5500 * Uncharges zswap memory on page in. 5501 */ 5502 void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg, size_t size) 5503 { 5504 struct mem_cgroup *memcg; 5505 5506 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) 5507 return; 5508 5509 obj_cgroup_uncharge(objcg, size); 5510 5511 rcu_read_lock(); 5512 memcg = obj_cgroup_memcg(objcg); 5513 mod_memcg_state(memcg, MEMCG_ZSWAP_B, -size); 5514 mod_memcg_state(memcg, MEMCG_ZSWAPPED, -1); 5515 rcu_read_unlock(); 5516 } 5517 5518 bool mem_cgroup_zswap_writeback_enabled(struct mem_cgroup *memcg) 5519 { 5520 /* if zswap is disabled, do not block pages going to the swapping device */ 5521 if (!zswap_is_enabled()) 5522 return true; 5523 5524 for (; memcg; memcg = parent_mem_cgroup(memcg)) 5525 if (!READ_ONCE(memcg->zswap_writeback)) 5526 return false; 5527 5528 return true; 5529 } 5530 5531 static u64 zswap_current_read(struct cgroup_subsys_state *css, 5532 struct cftype *cft) 5533 { 5534 struct mem_cgroup *memcg = mem_cgroup_from_css(css); 5535 5536 mem_cgroup_flush_stats(memcg); 5537 return memcg_page_state(memcg, MEMCG_ZSWAP_B); 5538 } 5539 5540 static int zswap_max_show(struct seq_file *m, void *v) 5541 { 5542 return seq_puts_memcg_tunable(m, 5543 READ_ONCE(mem_cgroup_from_seq(m)->zswap_max)); 5544 } 5545 5546 static ssize_t zswap_max_write(struct kernfs_open_file *of, 5547 char *buf, size_t nbytes, loff_t off) 5548 { 5549 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); 5550 unsigned long max; 5551 int err; 5552 5553 buf = strstrip(buf); 5554 err = page_counter_memparse(buf, "max", &max); 5555 if (err) 5556 return err; 5557 5558 xchg(&memcg->zswap_max, max); 5559 5560 return nbytes; 5561 } 5562 5563 static int zswap_writeback_show(struct seq_file *m, void *v) 5564 { 5565 struct mem_cgroup *memcg = mem_cgroup_from_seq(m); 5566 5567 seq_printf(m, "%d\n", READ_ONCE(memcg->zswap_writeback)); 5568 return 0; 5569 } 5570 5571 static ssize_t zswap_writeback_write(struct kernfs_open_file *of, 5572 char *buf, size_t nbytes, loff_t off) 5573 { 5574 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); 5575 int zswap_writeback; 5576 ssize_t parse_ret = kstrtoint(strstrip(buf), 0, &zswap_writeback); 5577 5578 if (parse_ret) 5579 return parse_ret; 5580 5581 if (zswap_writeback != 0 && zswap_writeback != 1) 5582 return -EINVAL; 5583 5584 WRITE_ONCE(memcg->zswap_writeback, zswap_writeback); 5585 return nbytes; 5586 } 5587 5588 static struct cftype zswap_files[] = { 5589 { 5590 .name = "zswap.current", 5591 .flags = CFTYPE_NOT_ON_ROOT, 5592 .read_u64 = zswap_current_read, 5593 }, 5594 { 5595 .name = "zswap.max", 5596 .flags = CFTYPE_NOT_ON_ROOT, 5597 .seq_show = zswap_max_show, 5598 .write = zswap_max_write, 5599 }, 5600 { 5601 .name = "zswap.writeback", 5602 .seq_show = zswap_writeback_show, 5603 .write = zswap_writeback_write, 5604 }, 5605 { } /* terminate */ 5606 }; 5607 #endif /* CONFIG_ZSWAP */ 5608 5609 static int __init mem_cgroup_swap_init(void) 5610 { 5611 if (mem_cgroup_disabled()) 5612 return 0; 5613 5614 WARN_ON(cgroup_add_dfl_cftypes(&memory_cgrp_subsys, swap_files)); 5615 #ifdef CONFIG_MEMCG_V1 5616 WARN_ON(cgroup_add_legacy_cftypes(&memory_cgrp_subsys, memsw_files)); 5617 #endif 5618 #ifdef CONFIG_ZSWAP 5619 WARN_ON(cgroup_add_dfl_cftypes(&memory_cgrp_subsys, zswap_files)); 5620 #endif 5621 return 0; 5622 } 5623 subsys_initcall(mem_cgroup_swap_init); 5624 5625 #endif /* CONFIG_SWAP */ 5626 5627 bool mem_cgroup_node_allowed(struct mem_cgroup *memcg, int nid) 5628 { 5629 return memcg ? cpuset_node_allowed(memcg->css.cgroup, nid) : true; 5630 } 5631 5632 void mem_cgroup_show_protected_memory(struct mem_cgroup *memcg) 5633 { 5634 if (mem_cgroup_disabled() || !cgroup_subsys_on_dfl(memory_cgrp_subsys)) 5635 return; 5636 5637 if (!memcg) 5638 memcg = root_mem_cgroup; 5639 5640 pr_warn("Memory cgroup min protection %lukB -- low protection %lukB", 5641 K(atomic_long_read(&memcg->memory.children_min_usage)*PAGE_SIZE), 5642 K(atomic_long_read(&memcg->memory.children_low_usage)*PAGE_SIZE)); 5643 } 5644