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