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/kernel.h>
7 #include <asm/page.h>
8 #include <linux/android_vendor.h>
9
10 struct page_counter {
11 atomic_long_t usage;
12 unsigned long min;
13 unsigned long low;
14 unsigned long high;
15 unsigned long max;
16
17 /* effective memory.min and memory.min usage tracking */
18 unsigned long emin;
19 atomic_long_t min_usage;
20 atomic_long_t children_min_usage;
21
22 /* effective memory.low and memory.low usage tracking */
23 unsigned long elow;
24 atomic_long_t low_usage;
25 atomic_long_t children_low_usage;
26
27 /* legacy */
28 unsigned long watermark;
29 unsigned long failcnt;
30 ANDROID_VENDOR_DATA(1);
31
32 /*
33 * 'parent' is placed here to be far from 'usage' to reduce
34 * cache false sharing, as 'usage' is written mostly while
35 * parent is frequently read for cgroup's hierarchical
36 * counting nature.
37 */
38 struct page_counter *parent;
39 };
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