1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_PAGE_COUNTER_H
3 #define _LINUX_PAGE_COUNTER_H
5 #include <linux/atomic.h>
6 #include <linux/cache.h>
7 #include <linux/kernel.h>
12 * Make sure 'usage' does not share cacheline with any other field. The
13 * memcg->memory.usage is a hot member of struct mem_cgroup.
16 CACHELINE_PADDING(_pad1_);
18 /* effective memory.min and memory.min usage tracking */
20 atomic_long_t min_usage;
21 atomic_long_t children_min_usage;
23 /* effective memory.low and memory.low usage tracking */
25 atomic_long_t low_usage;
26 atomic_long_t children_low_usage;
28 unsigned long watermark;
29 unsigned long failcnt;
31 /* Keep all the read most fields in a separete cacheline. */
32 CACHELINE_PADDING(_pad2_);
38 struct page_counter *parent;
39 } ____cacheline_internodealigned_in_smp;
41 #if BITS_PER_LONG == 32
42 #define PAGE_COUNTER_MAX LONG_MAX
44 #define PAGE_COUNTER_MAX (LONG_MAX / PAGE_SIZE)
47 static inline void page_counter_init(struct page_counter *counter,
48 struct page_counter *parent)
50 atomic_long_set(&counter->usage, 0);
51 counter->max = PAGE_COUNTER_MAX;
52 counter->parent = parent;
55 static inline unsigned long page_counter_read(struct page_counter *counter)
57 return atomic_long_read(&counter->usage);
60 void page_counter_cancel(struct page_counter *counter, unsigned long nr_pages);
61 void page_counter_charge(struct page_counter *counter, unsigned long nr_pages);
62 bool page_counter_try_charge(struct page_counter *counter,
63 unsigned long nr_pages,
64 struct page_counter **fail);
65 void page_counter_uncharge(struct page_counter *counter, unsigned long nr_pages);
66 void page_counter_set_min(struct page_counter *counter, unsigned long nr_pages);
67 void page_counter_set_low(struct page_counter *counter, unsigned long nr_pages);
69 static inline void page_counter_set_high(struct page_counter *counter,
70 unsigned long nr_pages)
72 WRITE_ONCE(counter->high, nr_pages);
75 int page_counter_set_max(struct page_counter *counter, unsigned long nr_pages);
76 int page_counter_memparse(const char *buf, const char *max,
77 unsigned long *nr_pages);
79 static inline void page_counter_reset_watermark(struct page_counter *counter)
81 counter->watermark = page_counter_read(counter);
84 #endif /* _LINUX_PAGE_COUNTER_H */