1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /* memcontrol.h - Memory Controller
4 * Copyright IBM Corporation, 2007
5 * Author Balbir Singh <balbir@linux.vnet.ibm.com>
7 * Copyright 2007 OpenVZ SWsoft Inc
8 * Author: Pavel Emelianov <xemul@openvz.org>
11 #ifndef _LINUX_MEMCONTROL_H
12 #define _LINUX_MEMCONTROL_H
13 #include <linux/cgroup.h>
14 #include <linux/vm_event_item.h>
15 #include <linux/hardirq.h>
16 #include <linux/jump_label.h>
17 #include <linux/page_counter.h>
18 #include <linux/vmpressure.h>
19 #include <linux/eventfd.h>
21 #include <linux/vmstat.h>
22 #include <linux/writeback.h>
23 #include <linux/page-flags.h>
31 /* Cgroup-specific page state, on top of universal node page state */
32 enum memcg_stat_item
{
33 MEMCG_SWAP
= NR_VM_NODE_STAT_ITEMS
,
39 enum memcg_memory_event
{
48 MEMCG_NR_MEMORY_EVENTS
,
51 struct mem_cgroup_reclaim_cookie
{
53 unsigned int generation
;
58 #define MEM_CGROUP_ID_SHIFT 16
59 #define MEM_CGROUP_ID_MAX USHRT_MAX
61 struct mem_cgroup_id
{
67 * Per memcg event counter is incremented at every pagein/pageout. With THP,
68 * it will be incremented by the number of pages. This counter is used
69 * to trigger some periodic events. This is straightforward and better
70 * than using jiffies etc. to handle periodic memcg event.
72 enum mem_cgroup_events_target
{
73 MEM_CGROUP_TARGET_THRESH
,
74 MEM_CGROUP_TARGET_SOFTLIMIT
,
78 struct memcg_vmstats_percpu
{
79 /* Local (CPU and cgroup) page state & events */
80 long state
[MEMCG_NR_STAT
];
81 unsigned long events
[NR_VM_EVENT_ITEMS
];
83 /* Delta calculation for lockless upward propagation */
84 long state_prev
[MEMCG_NR_STAT
];
85 unsigned long events_prev
[NR_VM_EVENT_ITEMS
];
87 /* Cgroup1: threshold notifications & softlimit tree updates */
88 unsigned long nr_page_events
;
89 unsigned long targets
[MEM_CGROUP_NTARGETS
];
92 struct memcg_vmstats
{
93 /* Aggregated (CPU and subtree) page state & events */
94 long state
[MEMCG_NR_STAT
];
95 unsigned long events
[NR_VM_EVENT_ITEMS
];
97 /* Pending child counts during tree propagation */
98 long state_pending
[MEMCG_NR_STAT
];
99 unsigned long events_pending
[NR_VM_EVENT_ITEMS
];
102 struct mem_cgroup_reclaim_iter
{
103 struct mem_cgroup
*position
;
104 /* scan generation, increased every round-trip */
105 unsigned int generation
;
109 long count
[NR_VM_NODE_STAT_ITEMS
];
112 struct batched_lruvec_stat
{
113 s32 count
[NR_VM_NODE_STAT_ITEMS
];
117 * Bitmap of shrinker::id corresponding to memcg-aware shrinkers,
118 * which have elements charged to this memcg.
120 struct memcg_shrinker_map
{
126 * per-node information in memory controller.
128 struct mem_cgroup_per_node
{
129 struct lruvec lruvec
;
132 * Legacy local VM stats. This should be struct lruvec_stat and
133 * cannot be optimized to struct batched_lruvec_stat. Because
134 * the threshold of the lruvec_stat_cpu can be as big as
135 * MEMCG_CHARGE_BATCH * PAGE_SIZE. It can fit into s32. But this
136 * filed has no upper limit.
138 struct lruvec_stat __percpu
*lruvec_stat_local
;
140 /* Subtree VM stats (batched updates) */
141 struct batched_lruvec_stat __percpu
*lruvec_stat_cpu
;
142 atomic_long_t lruvec_stat
[NR_VM_NODE_STAT_ITEMS
];
144 unsigned long lru_zone_size
[MAX_NR_ZONES
][NR_LRU_LISTS
];
146 struct mem_cgroup_reclaim_iter iter
;
148 struct memcg_shrinker_map __rcu
*shrinker_map
;
150 struct rb_node tree_node
; /* RB tree node */
151 unsigned long usage_in_excess
;/* Set to the value by which */
152 /* the soft limit is exceeded*/
154 struct mem_cgroup
*memcg
; /* Back pointer, we cannot */
155 /* use container_of */
158 struct mem_cgroup_threshold
{
159 struct eventfd_ctx
*eventfd
;
160 unsigned long threshold
;
164 struct mem_cgroup_threshold_ary
{
165 /* An array index points to threshold just below or equal to usage. */
166 int current_threshold
;
167 /* Size of entries[] */
169 /* Array of thresholds */
170 struct mem_cgroup_threshold entries
[];
173 struct mem_cgroup_thresholds
{
174 /* Primary thresholds array */
175 struct mem_cgroup_threshold_ary
*primary
;
177 * Spare threshold array.
178 * This is needed to make mem_cgroup_unregister_event() "never fail".
179 * It must be able to store at least primary->size - 1 entries.
181 struct mem_cgroup_threshold_ary
*spare
;
184 enum memcg_kmem_state
{
190 #if defined(CONFIG_SMP)
191 struct memcg_padding
{
193 } ____cacheline_internodealigned_in_smp
;
194 #define MEMCG_PADDING(name) struct memcg_padding name;
196 #define MEMCG_PADDING(name)
200 * Remember four most recent foreign writebacks with dirty pages in this
201 * cgroup. Inode sharing is expected to be uncommon and, even if we miss
202 * one in a given round, we're likely to catch it later if it keeps
203 * foreign-dirtying, so a fairly low count should be enough.
205 * See mem_cgroup_track_foreign_dirty_slowpath() for details.
207 #define MEMCG_CGWB_FRN_CNT 4
209 struct memcg_cgwb_frn
{
210 u64 bdi_id
; /* bdi->id of the foreign inode */
211 int memcg_id
; /* memcg->css.id of foreign inode */
212 u64 at
; /* jiffies_64 at the time of dirtying */
213 struct wb_completion done
; /* tracks in-flight foreign writebacks */
217 * Bucket for arbitrarily byte-sized objects charged to a memory
218 * cgroup. The bucket can be reparented in one piece when the cgroup
219 * is destroyed, without having to round up the individual references
220 * of all live memory objects in the wild.
223 struct percpu_ref refcnt
;
224 struct mem_cgroup
*memcg
;
225 atomic_t nr_charged_bytes
;
227 struct list_head list
;
233 * The memory controller data structure. The memory controller controls both
234 * page cache and RSS per cgroup. We would eventually like to provide
235 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
236 * to help the administrator determine what knobs to tune.
239 struct cgroup_subsys_state css
;
241 /* Private memcg ID. Used to ID objects that outlive the cgroup */
242 struct mem_cgroup_id id
;
244 /* Accounted resources */
245 struct page_counter memory
; /* Both v1 & v2 */
248 struct page_counter swap
; /* v2 only */
249 struct page_counter memsw
; /* v1 only */
252 /* Legacy consumer-oriented counters */
253 struct page_counter kmem
; /* v1 only */
254 struct page_counter tcpmem
; /* v1 only */
256 /* Range enforcement for interrupt charges */
257 struct work_struct high_work
;
259 unsigned long soft_limit
;
261 /* vmpressure notifications */
262 struct vmpressure vmpressure
;
265 * Should the OOM killer kill all belonging tasks, had it kill one?
269 /* protected by memcg_oom_lock */
274 /* OOM-Killer disable */
275 int oom_kill_disable
;
277 /* memory.events and memory.events.local */
278 struct cgroup_file events_file
;
279 struct cgroup_file events_local_file
;
281 /* handle for "memory.swap.events" */
282 struct cgroup_file swap_events_file
;
284 /* protect arrays of thresholds */
285 struct mutex thresholds_lock
;
287 /* thresholds for memory usage. RCU-protected */
288 struct mem_cgroup_thresholds thresholds
;
290 /* thresholds for mem+swap usage. RCU-protected */
291 struct mem_cgroup_thresholds memsw_thresholds
;
293 /* For oom notifier event fd */
294 struct list_head oom_notify
;
297 * Should we move charges of a task when a task is moved into this
298 * mem_cgroup ? And what type of charges should we move ?
300 unsigned long move_charge_at_immigrate
;
301 /* taken only while moving_account > 0 */
302 spinlock_t move_lock
;
303 unsigned long move_lock_flags
;
305 MEMCG_PADDING(_pad1_
);
308 struct memcg_vmstats vmstats
;
311 atomic_long_t memory_events
[MEMCG_NR_MEMORY_EVENTS
];
312 atomic_long_t memory_events_local
[MEMCG_NR_MEMORY_EVENTS
];
314 unsigned long socket_pressure
;
316 /* Legacy tcp memory accounting */
320 #ifdef CONFIG_MEMCG_KMEM
322 enum memcg_kmem_state kmem_state
;
323 struct obj_cgroup __rcu
*objcg
;
324 struct list_head objcg_list
; /* list of inherited objcgs */
327 MEMCG_PADDING(_pad2_
);
330 * set > 0 if pages under this cgroup are moving to other cgroup.
332 atomic_t moving_account
;
333 struct task_struct
*move_lock_task
;
335 struct memcg_vmstats_percpu __percpu
*vmstats_percpu
;
337 #ifdef CONFIG_CGROUP_WRITEBACK
338 struct list_head cgwb_list
;
339 struct wb_domain cgwb_domain
;
340 struct memcg_cgwb_frn cgwb_frn
[MEMCG_CGWB_FRN_CNT
];
343 /* List of events which userspace want to receive */
344 struct list_head event_list
;
345 spinlock_t event_list_lock
;
347 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
348 struct deferred_split deferred_split_queue
;
351 struct mem_cgroup_per_node
*nodeinfo
[0];
352 /* WARNING: nodeinfo must be the last member here */
356 * size of first charge trial. "32" comes from vmscan.c's magic value.
357 * TODO: maybe necessary to use big numbers in big irons.
359 #define MEMCG_CHARGE_BATCH 32U
361 extern struct mem_cgroup
*root_mem_cgroup
;
363 enum page_memcg_data_flags
{
364 /* page->memcg_data is a pointer to an objcgs vector */
365 MEMCG_DATA_OBJCGS
= (1UL << 0),
366 /* page has been accounted as a non-slab kernel page */
367 MEMCG_DATA_KMEM
= (1UL << 1),
368 /* the next bit after the last actual flag */
369 __NR_MEMCG_DATA_FLAGS
= (1UL << 2),
372 #define MEMCG_DATA_FLAGS_MASK (__NR_MEMCG_DATA_FLAGS - 1)
374 static inline bool PageMemcgKmem(struct page
*page
);
377 * After the initialization objcg->memcg is always pointing at
378 * a valid memcg, but can be atomically swapped to the parent memcg.
380 * The caller must ensure that the returned memcg won't be released:
381 * e.g. acquire the rcu_read_lock or css_set_lock.
383 static inline struct mem_cgroup
*obj_cgroup_memcg(struct obj_cgroup
*objcg
)
385 return READ_ONCE(objcg
->memcg
);
389 * __page_memcg - get the memory cgroup associated with a non-kmem page
390 * @page: a pointer to the page struct
392 * Returns a pointer to the memory cgroup associated with the page,
393 * or NULL. This function assumes that the page is known to have a
394 * proper memory cgroup pointer. It's not safe to call this function
395 * against some type of pages, e.g. slab pages or ex-slab pages or
398 static inline struct mem_cgroup
*__page_memcg(struct page
*page
)
400 unsigned long memcg_data
= page
->memcg_data
;
402 VM_BUG_ON_PAGE(PageSlab(page
), page
);
403 VM_BUG_ON_PAGE(memcg_data
& MEMCG_DATA_OBJCGS
, page
);
404 VM_BUG_ON_PAGE(memcg_data
& MEMCG_DATA_KMEM
, page
);
406 return (struct mem_cgroup
*)(memcg_data
& ~MEMCG_DATA_FLAGS_MASK
);
410 * __page_objcg - get the object cgroup associated with a kmem page
411 * @page: a pointer to the page struct
413 * Returns a pointer to the object cgroup associated with the page,
414 * or NULL. This function assumes that the page is known to have a
415 * proper object cgroup pointer. It's not safe to call this function
416 * against some type of pages, e.g. slab pages or ex-slab pages or
419 static inline struct obj_cgroup
*__page_objcg(struct page
*page
)
421 unsigned long memcg_data
= page
->memcg_data
;
423 VM_BUG_ON_PAGE(PageSlab(page
), page
);
424 VM_BUG_ON_PAGE(memcg_data
& MEMCG_DATA_OBJCGS
, page
);
425 VM_BUG_ON_PAGE(!(memcg_data
& MEMCG_DATA_KMEM
), page
);
427 return (struct obj_cgroup
*)(memcg_data
& ~MEMCG_DATA_FLAGS_MASK
);
431 * page_memcg - get the memory cgroup associated with a page
432 * @page: a pointer to the page struct
434 * Returns a pointer to the memory cgroup associated with the page,
435 * or NULL. This function assumes that the page is known to have a
436 * proper memory cgroup pointer. It's not safe to call this function
437 * against some type of pages, e.g. slab pages or ex-slab pages.
439 * For a non-kmem page any of the following ensures page and memcg binding
444 * - lock_page_memcg()
445 * - exclusive reference
447 * For a kmem page a caller should hold an rcu read lock to protect memcg
448 * associated with a kmem page from being released.
450 static inline struct mem_cgroup
*page_memcg(struct page
*page
)
452 if (PageMemcgKmem(page
))
453 return obj_cgroup_memcg(__page_objcg(page
));
455 return __page_memcg(page
);
459 * page_memcg_rcu - locklessly get the memory cgroup associated with a page
460 * @page: a pointer to the page struct
462 * Returns a pointer to the memory cgroup associated with the page,
463 * or NULL. This function assumes that the page is known to have a
464 * proper memory cgroup pointer. It's not safe to call this function
465 * against some type of pages, e.g. slab pages or ex-slab pages.
467 static inline struct mem_cgroup
*page_memcg_rcu(struct page
*page
)
469 unsigned long memcg_data
= READ_ONCE(page
->memcg_data
);
471 VM_BUG_ON_PAGE(PageSlab(page
), page
);
472 WARN_ON_ONCE(!rcu_read_lock_held());
474 if (memcg_data
& MEMCG_DATA_KMEM
) {
475 struct obj_cgroup
*objcg
;
477 objcg
= (void *)(memcg_data
& ~MEMCG_DATA_FLAGS_MASK
);
478 return obj_cgroup_memcg(objcg
);
481 return (struct mem_cgroup
*)(memcg_data
& ~MEMCG_DATA_FLAGS_MASK
);
485 * page_memcg_check - get the memory cgroup associated with a page
486 * @page: a pointer to the page struct
488 * Returns a pointer to the memory cgroup associated with the page,
489 * or NULL. This function unlike page_memcg() can take any page
490 * as an argument. It has to be used in cases when it's not known if a page
491 * has an associated memory cgroup pointer or an object cgroups vector or
494 * For a non-kmem page any of the following ensures page and memcg binding
499 * - lock_page_memcg()
500 * - exclusive reference
502 * For a kmem page a caller should hold an rcu read lock to protect memcg
503 * associated with a kmem page from being released.
505 static inline struct mem_cgroup
*page_memcg_check(struct page
*page
)
508 * Because page->memcg_data might be changed asynchronously
509 * for slab pages, READ_ONCE() should be used here.
511 unsigned long memcg_data
= READ_ONCE(page
->memcg_data
);
513 if (memcg_data
& MEMCG_DATA_OBJCGS
)
516 if (memcg_data
& MEMCG_DATA_KMEM
) {
517 struct obj_cgroup
*objcg
;
519 objcg
= (void *)(memcg_data
& ~MEMCG_DATA_FLAGS_MASK
);
520 return obj_cgroup_memcg(objcg
);
523 return (struct mem_cgroup
*)(memcg_data
& ~MEMCG_DATA_FLAGS_MASK
);
526 #ifdef CONFIG_MEMCG_KMEM
528 * PageMemcgKmem - check if the page has MemcgKmem flag set
529 * @page: a pointer to the page struct
531 * Checks if the page has MemcgKmem flag set. The caller must ensure that
532 * the page has an associated memory cgroup. It's not safe to call this function
533 * against some types of pages, e.g. slab pages.
535 static inline bool PageMemcgKmem(struct page
*page
)
537 VM_BUG_ON_PAGE(page
->memcg_data
& MEMCG_DATA_OBJCGS
, page
);
538 return page
->memcg_data
& MEMCG_DATA_KMEM
;
542 * page_objcgs - get the object cgroups vector associated with a page
543 * @page: a pointer to the page struct
545 * Returns a pointer to the object cgroups vector associated with the page,
546 * or NULL. This function assumes that the page is known to have an
547 * associated object cgroups vector. It's not safe to call this function
548 * against pages, which might have an associated memory cgroup: e.g.
549 * kernel stack pages.
551 static inline struct obj_cgroup
**page_objcgs(struct page
*page
)
553 unsigned long memcg_data
= READ_ONCE(page
->memcg_data
);
555 VM_BUG_ON_PAGE(memcg_data
&& !(memcg_data
& MEMCG_DATA_OBJCGS
), page
);
556 VM_BUG_ON_PAGE(memcg_data
& MEMCG_DATA_KMEM
, page
);
558 return (struct obj_cgroup
**)(memcg_data
& ~MEMCG_DATA_FLAGS_MASK
);
562 * page_objcgs_check - get the object cgroups vector associated with a page
563 * @page: a pointer to the page struct
565 * Returns a pointer to the object cgroups vector associated with the page,
566 * or NULL. This function is safe to use if the page can be directly associated
567 * with a memory cgroup.
569 static inline struct obj_cgroup
**page_objcgs_check(struct page
*page
)
571 unsigned long memcg_data
= READ_ONCE(page
->memcg_data
);
573 if (!memcg_data
|| !(memcg_data
& MEMCG_DATA_OBJCGS
))
576 VM_BUG_ON_PAGE(memcg_data
& MEMCG_DATA_KMEM
, page
);
578 return (struct obj_cgroup
**)(memcg_data
& ~MEMCG_DATA_FLAGS_MASK
);
582 static inline bool PageMemcgKmem(struct page
*page
)
587 static inline struct obj_cgroup
**page_objcgs(struct page
*page
)
592 static inline struct obj_cgroup
**page_objcgs_check(struct page
*page
)
598 static __always_inline
bool memcg_stat_item_in_bytes(int idx
)
600 if (idx
== MEMCG_PERCPU_B
)
602 return vmstat_item_in_bytes(idx
);
605 static inline bool mem_cgroup_is_root(struct mem_cgroup
*memcg
)
607 return (memcg
== root_mem_cgroup
);
610 static inline bool mem_cgroup_disabled(void)
612 return !cgroup_subsys_enabled(memory_cgrp_subsys
);
615 static inline unsigned long mem_cgroup_protection(struct mem_cgroup
*root
,
616 struct mem_cgroup
*memcg
,
619 if (mem_cgroup_disabled())
623 * There is no reclaim protection applied to a targeted reclaim.
624 * We are special casing this specific case here because
625 * mem_cgroup_protected calculation is not robust enough to keep
626 * the protection invariant for calculated effective values for
627 * parallel reclaimers with different reclaim target. This is
628 * especially a problem for tail memcgs (as they have pages on LRU)
629 * which would want to have effective values 0 for targeted reclaim
630 * but a different value for external reclaim.
633 * Let's have global and A's reclaim in parallel:
635 * A (low=2G, usage = 3G, max = 3G, children_low_usage = 1.5G)
637 * | C (low = 1G, usage = 2.5G)
638 * B (low = 1G, usage = 0.5G)
640 * For the global reclaim
642 * B.elow = min(B.usage, B.low) because children_low_usage <= A.elow
643 * C.elow = min(C.usage, C.low)
645 * With the effective values resetting we have A reclaim
650 * If the global reclaim races with A's reclaim then
651 * B.elow = C.elow = 0 because children_low_usage > A.elow)
652 * is possible and reclaiming B would be violating the protection.
659 return READ_ONCE(memcg
->memory
.emin
);
661 return max(READ_ONCE(memcg
->memory
.emin
),
662 READ_ONCE(memcg
->memory
.elow
));
665 void mem_cgroup_calculate_protection(struct mem_cgroup
*root
,
666 struct mem_cgroup
*memcg
);
668 static inline bool mem_cgroup_supports_protection(struct mem_cgroup
*memcg
)
671 * The root memcg doesn't account charges, and doesn't support
674 return !mem_cgroup_disabled() && !mem_cgroup_is_root(memcg
);
678 static inline bool mem_cgroup_below_low(struct mem_cgroup
*memcg
)
680 if (!mem_cgroup_supports_protection(memcg
))
683 return READ_ONCE(memcg
->memory
.elow
) >=
684 page_counter_read(&memcg
->memory
);
687 static inline bool mem_cgroup_below_min(struct mem_cgroup
*memcg
)
689 if (!mem_cgroup_supports_protection(memcg
))
692 return READ_ONCE(memcg
->memory
.emin
) >=
693 page_counter_read(&memcg
->memory
);
696 int mem_cgroup_charge(struct page
*page
, struct mm_struct
*mm
, gfp_t gfp_mask
);
697 int mem_cgroup_swapin_charge_page(struct page
*page
, struct mm_struct
*mm
,
698 gfp_t gfp
, swp_entry_t entry
);
699 void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry
);
701 void mem_cgroup_uncharge(struct page
*page
);
702 void mem_cgroup_uncharge_list(struct list_head
*page_list
);
704 void mem_cgroup_migrate(struct page
*oldpage
, struct page
*newpage
);
707 * mem_cgroup_lruvec - get the lru list vector for a memcg & node
708 * @memcg: memcg of the wanted lruvec
709 * @pgdat: pglist_data
711 * Returns the lru list vector holding pages for a given @memcg &
712 * @pgdat combination. This can be the node lruvec, if the memory
713 * controller is disabled.
715 static inline struct lruvec
*mem_cgroup_lruvec(struct mem_cgroup
*memcg
,
716 struct pglist_data
*pgdat
)
718 struct mem_cgroup_per_node
*mz
;
719 struct lruvec
*lruvec
;
721 if (mem_cgroup_disabled()) {
722 lruvec
= &pgdat
->__lruvec
;
727 memcg
= root_mem_cgroup
;
729 mz
= memcg
->nodeinfo
[pgdat
->node_id
];
730 lruvec
= &mz
->lruvec
;
733 * Since a node can be onlined after the mem_cgroup was created,
734 * we have to be prepared to initialize lruvec->pgdat here;
735 * and if offlined then reonlined, we need to reinitialize it.
737 if (unlikely(lruvec
->pgdat
!= pgdat
))
738 lruvec
->pgdat
= pgdat
;
743 * mem_cgroup_page_lruvec - return lruvec for isolating/putting an LRU page
745 * @pgdat: pgdat of the page
747 * This function relies on page->mem_cgroup being stable.
749 static inline struct lruvec
*mem_cgroup_page_lruvec(struct page
*page
,
750 struct pglist_data
*pgdat
)
752 struct mem_cgroup
*memcg
= page_memcg(page
);
754 VM_WARN_ON_ONCE_PAGE(!memcg
&& !mem_cgroup_disabled(), page
);
755 return mem_cgroup_lruvec(memcg
, pgdat
);
758 static inline bool lruvec_holds_page_lru_lock(struct page
*page
,
759 struct lruvec
*lruvec
)
761 pg_data_t
*pgdat
= page_pgdat(page
);
762 const struct mem_cgroup
*memcg
;
763 struct mem_cgroup_per_node
*mz
;
765 if (mem_cgroup_disabled())
766 return lruvec
== &pgdat
->__lruvec
;
768 mz
= container_of(lruvec
, struct mem_cgroup_per_node
, lruvec
);
769 memcg
= page_memcg(page
) ? : root_mem_cgroup
;
771 return lruvec
->pgdat
== pgdat
&& mz
->memcg
== memcg
;
774 struct mem_cgroup
*mem_cgroup_from_task(struct task_struct
*p
);
776 struct mem_cgroup
*get_mem_cgroup_from_mm(struct mm_struct
*mm
);
778 struct lruvec
*lock_page_lruvec(struct page
*page
);
779 struct lruvec
*lock_page_lruvec_irq(struct page
*page
);
780 struct lruvec
*lock_page_lruvec_irqsave(struct page
*page
,
781 unsigned long *flags
);
783 #ifdef CONFIG_DEBUG_VM
784 void lruvec_memcg_debug(struct lruvec
*lruvec
, struct page
*page
);
786 static inline void lruvec_memcg_debug(struct lruvec
*lruvec
, struct page
*page
)
792 struct mem_cgroup
*mem_cgroup_from_css(struct cgroup_subsys_state
*css
){
793 return css
? container_of(css
, struct mem_cgroup
, css
) : NULL
;
796 static inline bool obj_cgroup_tryget(struct obj_cgroup
*objcg
)
798 return percpu_ref_tryget(&objcg
->refcnt
);
801 static inline void obj_cgroup_get(struct obj_cgroup
*objcg
)
803 percpu_ref_get(&objcg
->refcnt
);
806 static inline void obj_cgroup_get_many(struct obj_cgroup
*objcg
,
809 percpu_ref_get_many(&objcg
->refcnt
, nr
);
812 static inline void obj_cgroup_put(struct obj_cgroup
*objcg
)
814 percpu_ref_put(&objcg
->refcnt
);
817 static inline void mem_cgroup_put(struct mem_cgroup
*memcg
)
820 css_put(&memcg
->css
);
823 #define mem_cgroup_from_counter(counter, member) \
824 container_of(counter, struct mem_cgroup, member)
826 struct mem_cgroup
*mem_cgroup_iter(struct mem_cgroup
*,
828 struct mem_cgroup_reclaim_cookie
*);
829 void mem_cgroup_iter_break(struct mem_cgroup
*, struct mem_cgroup
*);
830 int mem_cgroup_scan_tasks(struct mem_cgroup
*,
831 int (*)(struct task_struct
*, void *), void *);
833 static inline unsigned short mem_cgroup_id(struct mem_cgroup
*memcg
)
835 if (mem_cgroup_disabled())
840 struct mem_cgroup
*mem_cgroup_from_id(unsigned short id
);
842 static inline struct mem_cgroup
*mem_cgroup_from_seq(struct seq_file
*m
)
844 return mem_cgroup_from_css(seq_css(m
));
847 static inline struct mem_cgroup
*lruvec_memcg(struct lruvec
*lruvec
)
849 struct mem_cgroup_per_node
*mz
;
851 if (mem_cgroup_disabled())
854 mz
= container_of(lruvec
, struct mem_cgroup_per_node
, lruvec
);
859 * parent_mem_cgroup - find the accounting parent of a memcg
860 * @memcg: memcg whose parent to find
862 * Returns the parent memcg, or NULL if this is the root or the memory
863 * controller is in legacy no-hierarchy mode.
865 static inline struct mem_cgroup
*parent_mem_cgroup(struct mem_cgroup
*memcg
)
867 if (!memcg
->memory
.parent
)
869 return mem_cgroup_from_counter(memcg
->memory
.parent
, memory
);
872 static inline bool mem_cgroup_is_descendant(struct mem_cgroup
*memcg
,
873 struct mem_cgroup
*root
)
877 return cgroup_is_descendant(memcg
->css
.cgroup
, root
->css
.cgroup
);
880 static inline bool mm_match_cgroup(struct mm_struct
*mm
,
881 struct mem_cgroup
*memcg
)
883 struct mem_cgroup
*task_memcg
;
887 task_memcg
= mem_cgroup_from_task(rcu_dereference(mm
->owner
));
889 match
= mem_cgroup_is_descendant(task_memcg
, memcg
);
894 struct cgroup_subsys_state
*mem_cgroup_css_from_page(struct page
*page
);
895 ino_t
page_cgroup_ino(struct page
*page
);
897 static inline bool mem_cgroup_online(struct mem_cgroup
*memcg
)
899 if (mem_cgroup_disabled())
901 return !!(memcg
->css
.flags
& CSS_ONLINE
);
905 * For memory reclaim.
907 int mem_cgroup_select_victim_node(struct mem_cgroup
*memcg
);
909 void mem_cgroup_update_lru_size(struct lruvec
*lruvec
, enum lru_list lru
,
910 int zid
, int nr_pages
);
913 unsigned long mem_cgroup_get_zone_lru_size(struct lruvec
*lruvec
,
914 enum lru_list lru
, int zone_idx
)
916 struct mem_cgroup_per_node
*mz
;
918 mz
= container_of(lruvec
, struct mem_cgroup_per_node
, lruvec
);
919 return READ_ONCE(mz
->lru_zone_size
[zone_idx
][lru
]);
922 void mem_cgroup_handle_over_high(void);
924 unsigned long mem_cgroup_get_max(struct mem_cgroup
*memcg
);
926 unsigned long mem_cgroup_size(struct mem_cgroup
*memcg
);
928 void mem_cgroup_print_oom_context(struct mem_cgroup
*memcg
,
929 struct task_struct
*p
);
931 void mem_cgroup_print_oom_meminfo(struct mem_cgroup
*memcg
);
933 static inline void mem_cgroup_enter_user_fault(void)
935 WARN_ON(current
->in_user_fault
);
936 current
->in_user_fault
= 1;
939 static inline void mem_cgroup_exit_user_fault(void)
941 WARN_ON(!current
->in_user_fault
);
942 current
->in_user_fault
= 0;
945 static inline bool task_in_memcg_oom(struct task_struct
*p
)
947 return p
->memcg_in_oom
;
950 bool mem_cgroup_oom_synchronize(bool wait
);
951 struct mem_cgroup
*mem_cgroup_get_oom_group(struct task_struct
*victim
,
952 struct mem_cgroup
*oom_domain
);
953 void mem_cgroup_print_oom_group(struct mem_cgroup
*memcg
);
955 #ifdef CONFIG_MEMCG_SWAP
956 extern bool cgroup_memory_noswap
;
959 void lock_page_memcg(struct page
*page
);
960 void unlock_page_memcg(struct page
*page
);
962 void __mod_memcg_state(struct mem_cgroup
*memcg
, int idx
, int val
);
964 /* idx can be of type enum memcg_stat_item or node_stat_item */
965 static inline void mod_memcg_state(struct mem_cgroup
*memcg
,
970 local_irq_save(flags
);
971 __mod_memcg_state(memcg
, idx
, val
);
972 local_irq_restore(flags
);
975 static inline unsigned long lruvec_page_state(struct lruvec
*lruvec
,
976 enum node_stat_item idx
)
978 struct mem_cgroup_per_node
*pn
;
981 if (mem_cgroup_disabled())
982 return node_page_state(lruvec_pgdat(lruvec
), idx
);
984 pn
= container_of(lruvec
, struct mem_cgroup_per_node
, lruvec
);
985 x
= atomic_long_read(&pn
->lruvec_stat
[idx
]);
993 static inline unsigned long lruvec_page_state_local(struct lruvec
*lruvec
,
994 enum node_stat_item idx
)
996 struct mem_cgroup_per_node
*pn
;
1000 if (mem_cgroup_disabled())
1001 return node_page_state(lruvec_pgdat(lruvec
), idx
);
1003 pn
= container_of(lruvec
, struct mem_cgroup_per_node
, lruvec
);
1004 for_each_possible_cpu(cpu
)
1005 x
+= per_cpu(pn
->lruvec_stat_local
->count
[idx
], cpu
);
1013 void __mod_memcg_lruvec_state(struct lruvec
*lruvec
, enum node_stat_item idx
,
1015 void __mod_lruvec_kmem_state(void *p
, enum node_stat_item idx
, int val
);
1017 static inline void mod_lruvec_kmem_state(void *p
, enum node_stat_item idx
,
1020 unsigned long flags
;
1022 local_irq_save(flags
);
1023 __mod_lruvec_kmem_state(p
, idx
, val
);
1024 local_irq_restore(flags
);
1027 static inline void mod_memcg_lruvec_state(struct lruvec
*lruvec
,
1028 enum node_stat_item idx
, int val
)
1030 unsigned long flags
;
1032 local_irq_save(flags
);
1033 __mod_memcg_lruvec_state(lruvec
, idx
, val
);
1034 local_irq_restore(flags
);
1037 void __count_memcg_events(struct mem_cgroup
*memcg
, enum vm_event_item idx
,
1038 unsigned long count
);
1040 static inline void count_memcg_events(struct mem_cgroup
*memcg
,
1041 enum vm_event_item idx
,
1042 unsigned long count
)
1044 unsigned long flags
;
1046 local_irq_save(flags
);
1047 __count_memcg_events(memcg
, idx
, count
);
1048 local_irq_restore(flags
);
1051 static inline void count_memcg_page_event(struct page
*page
,
1052 enum vm_event_item idx
)
1054 struct mem_cgroup
*memcg
= page_memcg(page
);
1057 count_memcg_events(memcg
, idx
, 1);
1060 static inline void count_memcg_event_mm(struct mm_struct
*mm
,
1061 enum vm_event_item idx
)
1063 struct mem_cgroup
*memcg
;
1065 if (mem_cgroup_disabled())
1069 memcg
= mem_cgroup_from_task(rcu_dereference(mm
->owner
));
1071 count_memcg_events(memcg
, idx
, 1);
1075 static inline void memcg_memory_event(struct mem_cgroup
*memcg
,
1076 enum memcg_memory_event event
)
1078 bool swap_event
= event
== MEMCG_SWAP_HIGH
|| event
== MEMCG_SWAP_MAX
||
1079 event
== MEMCG_SWAP_FAIL
;
1081 atomic_long_inc(&memcg
->memory_events_local
[event
]);
1083 cgroup_file_notify(&memcg
->events_local_file
);
1086 atomic_long_inc(&memcg
->memory_events
[event
]);
1088 cgroup_file_notify(&memcg
->swap_events_file
);
1090 cgroup_file_notify(&memcg
->events_file
);
1092 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys
))
1094 if (cgrp_dfl_root
.flags
& CGRP_ROOT_MEMORY_LOCAL_EVENTS
)
1096 } while ((memcg
= parent_mem_cgroup(memcg
)) &&
1097 !mem_cgroup_is_root(memcg
));
1100 static inline void memcg_memory_event_mm(struct mm_struct
*mm
,
1101 enum memcg_memory_event event
)
1103 struct mem_cgroup
*memcg
;
1105 if (mem_cgroup_disabled())
1109 memcg
= mem_cgroup_from_task(rcu_dereference(mm
->owner
));
1111 memcg_memory_event(memcg
, event
);
1115 void split_page_memcg(struct page
*head
, unsigned int nr
);
1117 unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t
*pgdat
, int order
,
1119 unsigned long *total_scanned
);
1121 #else /* CONFIG_MEMCG */
1123 #define MEM_CGROUP_ID_SHIFT 0
1124 #define MEM_CGROUP_ID_MAX 0
1126 static inline struct mem_cgroup
*page_memcg(struct page
*page
)
1131 static inline struct mem_cgroup
*page_memcg_rcu(struct page
*page
)
1133 WARN_ON_ONCE(!rcu_read_lock_held());
1137 static inline struct mem_cgroup
*page_memcg_check(struct page
*page
)
1142 static inline bool PageMemcgKmem(struct page
*page
)
1147 static inline bool mem_cgroup_is_root(struct mem_cgroup
*memcg
)
1152 static inline bool mem_cgroup_disabled(void)
1157 static inline void memcg_memory_event(struct mem_cgroup
*memcg
,
1158 enum memcg_memory_event event
)
1162 static inline void memcg_memory_event_mm(struct mm_struct
*mm
,
1163 enum memcg_memory_event event
)
1167 static inline unsigned long mem_cgroup_protection(struct mem_cgroup
*root
,
1168 struct mem_cgroup
*memcg
,
1169 bool in_low_reclaim
)
1174 static inline void mem_cgroup_calculate_protection(struct mem_cgroup
*root
,
1175 struct mem_cgroup
*memcg
)
1179 static inline bool mem_cgroup_below_low(struct mem_cgroup
*memcg
)
1184 static inline bool mem_cgroup_below_min(struct mem_cgroup
*memcg
)
1189 static inline int mem_cgroup_charge(struct page
*page
, struct mm_struct
*mm
,
1195 static inline int mem_cgroup_swapin_charge_page(struct page
*page
,
1196 struct mm_struct
*mm
, gfp_t gfp
, swp_entry_t entry
)
1201 static inline void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry
)
1205 static inline void mem_cgroup_uncharge(struct page
*page
)
1209 static inline void mem_cgroup_uncharge_list(struct list_head
*page_list
)
1213 static inline void mem_cgroup_migrate(struct page
*old
, struct page
*new)
1217 static inline struct lruvec
*mem_cgroup_lruvec(struct mem_cgroup
*memcg
,
1218 struct pglist_data
*pgdat
)
1220 return &pgdat
->__lruvec
;
1223 static inline struct lruvec
*mem_cgroup_page_lruvec(struct page
*page
,
1224 struct pglist_data
*pgdat
)
1226 return &pgdat
->__lruvec
;
1229 static inline bool lruvec_holds_page_lru_lock(struct page
*page
,
1230 struct lruvec
*lruvec
)
1232 pg_data_t
*pgdat
= page_pgdat(page
);
1234 return lruvec
== &pgdat
->__lruvec
;
1237 static inline void lruvec_memcg_debug(struct lruvec
*lruvec
, struct page
*page
)
1241 static inline struct mem_cgroup
*parent_mem_cgroup(struct mem_cgroup
*memcg
)
1246 static inline bool mm_match_cgroup(struct mm_struct
*mm
,
1247 struct mem_cgroup
*memcg
)
1252 static inline struct mem_cgroup
*get_mem_cgroup_from_mm(struct mm_struct
*mm
)
1257 static inline void mem_cgroup_put(struct mem_cgroup
*memcg
)
1261 static inline struct lruvec
*lock_page_lruvec(struct page
*page
)
1263 struct pglist_data
*pgdat
= page_pgdat(page
);
1265 spin_lock(&pgdat
->__lruvec
.lru_lock
);
1266 return &pgdat
->__lruvec
;
1269 static inline struct lruvec
*lock_page_lruvec_irq(struct page
*page
)
1271 struct pglist_data
*pgdat
= page_pgdat(page
);
1273 spin_lock_irq(&pgdat
->__lruvec
.lru_lock
);
1274 return &pgdat
->__lruvec
;
1277 static inline struct lruvec
*lock_page_lruvec_irqsave(struct page
*page
,
1278 unsigned long *flagsp
)
1280 struct pglist_data
*pgdat
= page_pgdat(page
);
1282 spin_lock_irqsave(&pgdat
->__lruvec
.lru_lock
, *flagsp
);
1283 return &pgdat
->__lruvec
;
1286 static inline struct mem_cgroup
*
1287 mem_cgroup_iter(struct mem_cgroup
*root
,
1288 struct mem_cgroup
*prev
,
1289 struct mem_cgroup_reclaim_cookie
*reclaim
)
1294 static inline void mem_cgroup_iter_break(struct mem_cgroup
*root
,
1295 struct mem_cgroup
*prev
)
1299 static inline int mem_cgroup_scan_tasks(struct mem_cgroup
*memcg
,
1300 int (*fn
)(struct task_struct
*, void *), void *arg
)
1305 static inline unsigned short mem_cgroup_id(struct mem_cgroup
*memcg
)
1310 static inline struct mem_cgroup
*mem_cgroup_from_id(unsigned short id
)
1313 /* XXX: This should always return root_mem_cgroup */
1317 static inline struct mem_cgroup
*mem_cgroup_from_seq(struct seq_file
*m
)
1322 static inline struct mem_cgroup
*lruvec_memcg(struct lruvec
*lruvec
)
1327 static inline bool mem_cgroup_online(struct mem_cgroup
*memcg
)
1333 unsigned long mem_cgroup_get_zone_lru_size(struct lruvec
*lruvec
,
1334 enum lru_list lru
, int zone_idx
)
1339 static inline unsigned long mem_cgroup_get_max(struct mem_cgroup
*memcg
)
1344 static inline unsigned long mem_cgroup_size(struct mem_cgroup
*memcg
)
1350 mem_cgroup_print_oom_context(struct mem_cgroup
*memcg
, struct task_struct
*p
)
1355 mem_cgroup_print_oom_meminfo(struct mem_cgroup
*memcg
)
1359 static inline void lock_page_memcg(struct page
*page
)
1363 static inline void unlock_page_memcg(struct page
*page
)
1367 static inline void mem_cgroup_handle_over_high(void)
1371 static inline void mem_cgroup_enter_user_fault(void)
1375 static inline void mem_cgroup_exit_user_fault(void)
1379 static inline bool task_in_memcg_oom(struct task_struct
*p
)
1384 static inline bool mem_cgroup_oom_synchronize(bool wait
)
1389 static inline struct mem_cgroup
*mem_cgroup_get_oom_group(
1390 struct task_struct
*victim
, struct mem_cgroup
*oom_domain
)
1395 static inline void mem_cgroup_print_oom_group(struct mem_cgroup
*memcg
)
1399 static inline void __mod_memcg_state(struct mem_cgroup
*memcg
,
1405 static inline void mod_memcg_state(struct mem_cgroup
*memcg
,
1411 static inline unsigned long lruvec_page_state(struct lruvec
*lruvec
,
1412 enum node_stat_item idx
)
1414 return node_page_state(lruvec_pgdat(lruvec
), idx
);
1417 static inline unsigned long lruvec_page_state_local(struct lruvec
*lruvec
,
1418 enum node_stat_item idx
)
1420 return node_page_state(lruvec_pgdat(lruvec
), idx
);
1423 static inline void __mod_memcg_lruvec_state(struct lruvec
*lruvec
,
1424 enum node_stat_item idx
, int val
)
1428 static inline void __mod_lruvec_kmem_state(void *p
, enum node_stat_item idx
,
1431 struct page
*page
= virt_to_head_page(p
);
1433 __mod_node_page_state(page_pgdat(page
), idx
, val
);
1436 static inline void mod_lruvec_kmem_state(void *p
, enum node_stat_item idx
,
1439 struct page
*page
= virt_to_head_page(p
);
1441 mod_node_page_state(page_pgdat(page
), idx
, val
);
1444 static inline void count_memcg_events(struct mem_cgroup
*memcg
,
1445 enum vm_event_item idx
,
1446 unsigned long count
)
1450 static inline void __count_memcg_events(struct mem_cgroup
*memcg
,
1451 enum vm_event_item idx
,
1452 unsigned long count
)
1456 static inline void count_memcg_page_event(struct page
*page
,
1462 void count_memcg_event_mm(struct mm_struct
*mm
, enum vm_event_item idx
)
1466 static inline void split_page_memcg(struct page
*head
, unsigned int nr
)
1471 unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t
*pgdat
, int order
,
1473 unsigned long *total_scanned
)
1477 #endif /* CONFIG_MEMCG */
1479 static inline void __inc_lruvec_kmem_state(void *p
, enum node_stat_item idx
)
1481 __mod_lruvec_kmem_state(p
, idx
, 1);
1484 static inline void __dec_lruvec_kmem_state(void *p
, enum node_stat_item idx
)
1486 __mod_lruvec_kmem_state(p
, idx
, -1);
1489 static inline struct lruvec
*parent_lruvec(struct lruvec
*lruvec
)
1491 struct mem_cgroup
*memcg
;
1493 memcg
= lruvec_memcg(lruvec
);
1496 memcg
= parent_mem_cgroup(memcg
);
1499 return mem_cgroup_lruvec(memcg
, lruvec_pgdat(lruvec
));
1502 static inline void unlock_page_lruvec(struct lruvec
*lruvec
)
1504 spin_unlock(&lruvec
->lru_lock
);
1507 static inline void unlock_page_lruvec_irq(struct lruvec
*lruvec
)
1509 spin_unlock_irq(&lruvec
->lru_lock
);
1512 static inline void unlock_page_lruvec_irqrestore(struct lruvec
*lruvec
,
1513 unsigned long flags
)
1515 spin_unlock_irqrestore(&lruvec
->lru_lock
, flags
);
1518 /* Don't lock again iff page's lruvec locked */
1519 static inline struct lruvec
*relock_page_lruvec_irq(struct page
*page
,
1520 struct lruvec
*locked_lruvec
)
1522 if (locked_lruvec
) {
1523 if (lruvec_holds_page_lru_lock(page
, locked_lruvec
))
1524 return locked_lruvec
;
1526 unlock_page_lruvec_irq(locked_lruvec
);
1529 return lock_page_lruvec_irq(page
);
1532 /* Don't lock again iff page's lruvec locked */
1533 static inline struct lruvec
*relock_page_lruvec_irqsave(struct page
*page
,
1534 struct lruvec
*locked_lruvec
, unsigned long *flags
)
1536 if (locked_lruvec
) {
1537 if (lruvec_holds_page_lru_lock(page
, locked_lruvec
))
1538 return locked_lruvec
;
1540 unlock_page_lruvec_irqrestore(locked_lruvec
, *flags
);
1543 return lock_page_lruvec_irqsave(page
, flags
);
1546 #ifdef CONFIG_CGROUP_WRITEBACK
1548 struct wb_domain
*mem_cgroup_wb_domain(struct bdi_writeback
*wb
);
1549 void mem_cgroup_wb_stats(struct bdi_writeback
*wb
, unsigned long *pfilepages
,
1550 unsigned long *pheadroom
, unsigned long *pdirty
,
1551 unsigned long *pwriteback
);
1553 void mem_cgroup_track_foreign_dirty_slowpath(struct page
*page
,
1554 struct bdi_writeback
*wb
);
1556 static inline void mem_cgroup_track_foreign_dirty(struct page
*page
,
1557 struct bdi_writeback
*wb
)
1559 if (mem_cgroup_disabled())
1562 if (unlikely(&page_memcg(page
)->css
!= wb
->memcg_css
))
1563 mem_cgroup_track_foreign_dirty_slowpath(page
, wb
);
1566 void mem_cgroup_flush_foreign(struct bdi_writeback
*wb
);
1568 #else /* CONFIG_CGROUP_WRITEBACK */
1570 static inline struct wb_domain
*mem_cgroup_wb_domain(struct bdi_writeback
*wb
)
1575 static inline void mem_cgroup_wb_stats(struct bdi_writeback
*wb
,
1576 unsigned long *pfilepages
,
1577 unsigned long *pheadroom
,
1578 unsigned long *pdirty
,
1579 unsigned long *pwriteback
)
1583 static inline void mem_cgroup_track_foreign_dirty(struct page
*page
,
1584 struct bdi_writeback
*wb
)
1588 static inline void mem_cgroup_flush_foreign(struct bdi_writeback
*wb
)
1592 #endif /* CONFIG_CGROUP_WRITEBACK */
1595 bool mem_cgroup_charge_skmem(struct mem_cgroup
*memcg
, unsigned int nr_pages
);
1596 void mem_cgroup_uncharge_skmem(struct mem_cgroup
*memcg
, unsigned int nr_pages
);
1598 extern struct static_key_false memcg_sockets_enabled_key
;
1599 #define mem_cgroup_sockets_enabled static_branch_unlikely(&memcg_sockets_enabled_key)
1600 void mem_cgroup_sk_alloc(struct sock
*sk
);
1601 void mem_cgroup_sk_free(struct sock
*sk
);
1602 static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup
*memcg
)
1604 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys
) && memcg
->tcpmem_pressure
)
1607 if (time_before(jiffies
, memcg
->socket_pressure
))
1609 } while ((memcg
= parent_mem_cgroup(memcg
)));
1613 int alloc_shrinker_maps(struct mem_cgroup
*memcg
);
1614 void free_shrinker_maps(struct mem_cgroup
*memcg
);
1615 void set_shrinker_bit(struct mem_cgroup
*memcg
, int nid
, int shrinker_id
);
1617 #define mem_cgroup_sockets_enabled 0
1618 static inline void mem_cgroup_sk_alloc(struct sock
*sk
) { };
1619 static inline void mem_cgroup_sk_free(struct sock
*sk
) { };
1620 static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup
*memcg
)
1625 static inline void set_shrinker_bit(struct mem_cgroup
*memcg
,
1626 int nid
, int shrinker_id
)
1631 #ifdef CONFIG_MEMCG_KMEM
1632 int __memcg_kmem_charge_page(struct page
*page
, gfp_t gfp
, int order
);
1633 void __memcg_kmem_uncharge_page(struct page
*page
, int order
);
1635 struct obj_cgroup
*get_obj_cgroup_from_current(void);
1637 int obj_cgroup_charge(struct obj_cgroup
*objcg
, gfp_t gfp
, size_t size
);
1638 void obj_cgroup_uncharge(struct obj_cgroup
*objcg
, size_t size
);
1640 extern struct static_key_false memcg_kmem_enabled_key
;
1642 extern int memcg_nr_cache_ids
;
1643 void memcg_get_cache_ids(void);
1644 void memcg_put_cache_ids(void);
1647 * Helper macro to loop through all memcg-specific caches. Callers must still
1648 * check if the cache is valid (it is either valid or NULL).
1649 * the slab_mutex must be held when looping through those caches
1651 #define for_each_memcg_cache_index(_idx) \
1652 for ((_idx) = 0; (_idx) < memcg_nr_cache_ids; (_idx)++)
1654 static inline bool memcg_kmem_enabled(void)
1656 return static_branch_likely(&memcg_kmem_enabled_key
);
1659 static inline int memcg_kmem_charge_page(struct page
*page
, gfp_t gfp
,
1662 if (memcg_kmem_enabled())
1663 return __memcg_kmem_charge_page(page
, gfp
, order
);
1667 static inline void memcg_kmem_uncharge_page(struct page
*page
, int order
)
1669 if (memcg_kmem_enabled())
1670 __memcg_kmem_uncharge_page(page
, order
);
1674 * A helper for accessing memcg's kmem_id, used for getting
1675 * corresponding LRU lists.
1677 static inline int memcg_cache_id(struct mem_cgroup
*memcg
)
1679 return memcg
? memcg
->kmemcg_id
: -1;
1682 struct mem_cgroup
*mem_cgroup_from_obj(void *p
);
1686 static inline int memcg_kmem_charge_page(struct page
*page
, gfp_t gfp
,
1692 static inline void memcg_kmem_uncharge_page(struct page
*page
, int order
)
1696 static inline int __memcg_kmem_charge_page(struct page
*page
, gfp_t gfp
,
1702 static inline void __memcg_kmem_uncharge_page(struct page
*page
, int order
)
1706 #define for_each_memcg_cache_index(_idx) \
1709 static inline bool memcg_kmem_enabled(void)
1714 static inline int memcg_cache_id(struct mem_cgroup
*memcg
)
1719 static inline void memcg_get_cache_ids(void)
1723 static inline void memcg_put_cache_ids(void)
1727 static inline struct mem_cgroup
*mem_cgroup_from_obj(void *p
)
1732 #endif /* CONFIG_MEMCG_KMEM */
1734 #endif /* _LINUX_MEMCONTROL_H */