]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
3e32cb2e JW |
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 { | |
bbec2e15 | 10 | atomic_long_t usage; |
bf8d5d52 | 11 | unsigned long min; |
23067153 | 12 | unsigned long low; |
d1663a90 | 13 | unsigned long high; |
bf8d5d52 | 14 | unsigned long max; |
3e32cb2e | 15 | |
bf8d5d52 RG |
16 | /* effective memory.min and memory.min usage tracking */ |
17 | unsigned long emin; | |
18 | atomic_long_t min_usage; | |
19 | atomic_long_t children_min_usage; | |
20 | ||
23067153 RG |
21 | /* effective memory.low and memory.low usage tracking */ |
22 | unsigned long elow; | |
23 | atomic_long_t low_usage; | |
24 | atomic_long_t children_low_usage; | |
25 | ||
3e32cb2e JW |
26 | /* legacy */ |
27 | unsigned long watermark; | |
28 | unsigned long failcnt; | |
802f1d52 FT |
29 | |
30 | /* | |
31 | * 'parent' is placed here to be far from 'usage' to reduce | |
32 | * cache false sharing, as 'usage' is written mostly while | |
33 | * parent is frequently read for cgroup's hierarchical | |
34 | * counting nature. | |
35 | */ | |
36 | struct page_counter *parent; | |
3e32cb2e JW |
37 | }; |
38 | ||
39 | #if BITS_PER_LONG == 32 | |
40 | #define PAGE_COUNTER_MAX LONG_MAX | |
41 | #else | |
42 | #define PAGE_COUNTER_MAX (LONG_MAX / PAGE_SIZE) | |
43 | #endif | |
44 | ||
45 | static inline void page_counter_init(struct page_counter *counter, | |
46 | struct page_counter *parent) | |
47 | { | |
bbec2e15 RG |
48 | atomic_long_set(&counter->usage, 0); |
49 | counter->max = PAGE_COUNTER_MAX; | |
3e32cb2e JW |
50 | counter->parent = parent; |
51 | } | |
52 | ||
53 | static inline unsigned long page_counter_read(struct page_counter *counter) | |
54 | { | |
bbec2e15 | 55 | return atomic_long_read(&counter->usage); |
3e32cb2e JW |
56 | } |
57 | ||
64f21993 | 58 | void page_counter_cancel(struct page_counter *counter, unsigned long nr_pages); |
3e32cb2e | 59 | void page_counter_charge(struct page_counter *counter, unsigned long nr_pages); |
6071ca52 JW |
60 | bool page_counter_try_charge(struct page_counter *counter, |
61 | unsigned long nr_pages, | |
62 | struct page_counter **fail); | |
64f21993 | 63 | void page_counter_uncharge(struct page_counter *counter, unsigned long nr_pages); |
bf8d5d52 | 64 | void page_counter_set_min(struct page_counter *counter, unsigned long nr_pages); |
23067153 | 65 | void page_counter_set_low(struct page_counter *counter, unsigned long nr_pages); |
d1663a90 JK |
66 | |
67 | static inline void page_counter_set_high(struct page_counter *counter, | |
68 | unsigned long nr_pages) | |
69 | { | |
70 | WRITE_ONCE(counter->high, nr_pages); | |
71 | } | |
72 | ||
bf8d5d52 | 73 | int page_counter_set_max(struct page_counter *counter, unsigned long nr_pages); |
650c5e56 JW |
74 | int page_counter_memparse(const char *buf, const char *max, |
75 | unsigned long *nr_pages); | |
3e32cb2e JW |
76 | |
77 | static inline void page_counter_reset_watermark(struct page_counter *counter) | |
78 | { | |
79 | counter->watermark = page_counter_read(counter); | |
80 | } | |
81 | ||
82 | #endif /* _LINUX_PAGE_COUNTER_H */ |