Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/pmladek...
[sfrench/cifs-2.6.git] / include / linux / page_counter.h
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 count;
11         unsigned long limit;
12         struct page_counter *parent;
13
14         /* legacy */
15         unsigned long watermark;
16         unsigned long failcnt;
17 };
18
19 #if BITS_PER_LONG == 32
20 #define PAGE_COUNTER_MAX LONG_MAX
21 #else
22 #define PAGE_COUNTER_MAX (LONG_MAX / PAGE_SIZE)
23 #endif
24
25 static inline void page_counter_init(struct page_counter *counter,
26                                      struct page_counter *parent)
27 {
28         atomic_long_set(&counter->count, 0);
29         counter->limit = PAGE_COUNTER_MAX;
30         counter->parent = parent;
31 }
32
33 static inline unsigned long page_counter_read(struct page_counter *counter)
34 {
35         return atomic_long_read(&counter->count);
36 }
37
38 void page_counter_cancel(struct page_counter *counter, unsigned long nr_pages);
39 void page_counter_charge(struct page_counter *counter, unsigned long nr_pages);
40 bool page_counter_try_charge(struct page_counter *counter,
41                              unsigned long nr_pages,
42                              struct page_counter **fail);
43 void page_counter_uncharge(struct page_counter *counter, unsigned long nr_pages);
44 int page_counter_limit(struct page_counter *counter, unsigned long limit);
45 int page_counter_memparse(const char *buf, const char *max,
46                           unsigned long *nr_pages);
47
48 static inline void page_counter_reset_watermark(struct page_counter *counter)
49 {
50         counter->watermark = page_counter_read(counter);
51 }
52
53 #endif /* _LINUX_PAGE_COUNTER_H */