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
9 struct page_counter {
10 atomic_long_t usage;
11 unsigned long min;
12 unsigned long low;
13 unsigned long high;
14 unsigned long max;
15 struct page_counter *parent;
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 };
31
32 #if BITS_PER_LONG == 32
33 #define PAGE_COUNTER_MAX LONG_MAX
34 #else
35 #define PAGE_COUNTER_MAX (LONG_MAX / PAGE_SIZE)
36 #endif
37
page_counter_init(struct page_counter * counter,struct page_counter * parent)38 static inline void page_counter_init(struct page_counter *counter,
39 struct page_counter *parent)
40 {
41 atomic_long_set(&counter->usage, 0);
42 counter->max = PAGE_COUNTER_MAX;
43 counter->parent = parent;
44 }
45
page_counter_read(struct page_counter * counter)46 static inline unsigned long page_counter_read(struct page_counter *counter)
47 {
48 return atomic_long_read(&counter->usage);
49 }
50
51 void page_counter_cancel(struct page_counter *counter, unsigned long nr_pages);
52 void page_counter_charge(struct page_counter *counter, unsigned long nr_pages);
53 bool page_counter_try_charge(struct page_counter *counter,
54 unsigned long nr_pages,
55 struct page_counter **fail);
56 void page_counter_uncharge(struct page_counter *counter, unsigned long nr_pages);
57 void page_counter_set_min(struct page_counter *counter, unsigned long nr_pages);
58 void page_counter_set_low(struct page_counter *counter, unsigned long nr_pages);
59
page_counter_set_high(struct page_counter * counter,unsigned long nr_pages)60 static inline void page_counter_set_high(struct page_counter *counter,
61 unsigned long nr_pages)
62 {
63 WRITE_ONCE(counter->high, nr_pages);
64 }
65
66 int page_counter_set_max(struct page_counter *counter, unsigned long nr_pages);
67 int page_counter_memparse(const char *buf, const char *max,
68 unsigned long *nr_pages);
69
page_counter_reset_watermark(struct page_counter * counter)70 static inline void page_counter_reset_watermark(struct page_counter *counter)
71 {
72 counter->watermark = page_counter_read(counter);
73 }
74
75 #endif /* _LINUX_PAGE_COUNTER_H */
76