xref: /openbmc/linux/include/linux/page_counter.h (revision e6ad640b)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_PAGE_COUNTER_H
3 #define _LINUX_PAGE_COUNTER_H
4 
5 #include <linux/atomic.h>
6 #include <linux/cache.h>
7 #include <linux/kernel.h>
8 #include <asm/page.h>
9 
10 struct page_counter {
11 	/*
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.
14 	 */
15 	atomic_long_t usage;
16 	CACHELINE_PADDING(_pad1_);
17 
18 	/* effective memory.min and memory.min usage tracking */
19 	unsigned long emin;
20 	atomic_long_t min_usage;
21 	atomic_long_t children_min_usage;
22 
23 	/* effective memory.low and memory.low usage tracking */
24 	unsigned long elow;
25 	atomic_long_t low_usage;
26 	atomic_long_t children_low_usage;
27 
28 	unsigned long watermark;
29 	unsigned long failcnt;
30 
31 	/* Keep all the read most fields in a separete cacheline. */
32 	CACHELINE_PADDING(_pad2_);
33 
34 	unsigned long min;
35 	unsigned long low;
36 	unsigned long high;
37 	unsigned long max;
38 	struct page_counter *parent;
39 } ____cacheline_internodealigned_in_smp;
40 
41 #if BITS_PER_LONG == 32
42 #define PAGE_COUNTER_MAX LONG_MAX
43 #else
44 #define PAGE_COUNTER_MAX (LONG_MAX / PAGE_SIZE)
45 #endif
46 
page_counter_init(struct page_counter * counter,struct page_counter * parent)47 static inline void page_counter_init(struct page_counter *counter,
48 				     struct page_counter *parent)
49 {
50 	atomic_long_set(&counter->usage, 0);
51 	counter->max = PAGE_COUNTER_MAX;
52 	counter->parent = parent;
53 }
54 
page_counter_read(struct page_counter * counter)55 static inline unsigned long page_counter_read(struct page_counter *counter)
56 {
57 	return atomic_long_read(&counter->usage);
58 }
59 
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);
68 
page_counter_set_high(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)
71 {
72 	WRITE_ONCE(counter->high, nr_pages);
73 }
74 
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);
78 
page_counter_reset_watermark(struct page_counter * counter)79 static inline void page_counter_reset_watermark(struct page_counter *counter)
80 {
81 	counter->watermark = page_counter_read(counter);
82 }
83 
84 #endif /* _LINUX_PAGE_COUNTER_H */
85