Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 1 | #ifndef _LINUX_PAGE_COUNTER_H |
| 2 | #define _LINUX_PAGE_COUNTER_H |
| 3 | |
| 4 | #include <linux/atomic.h> |
| 5 | #include <linux/kernel.h> |
| 6 | #include <asm/page.h> |
| 7 | |
| 8 | struct page_counter { |
| 9 | atomic_long_t count; |
| 10 | unsigned long limit; |
| 11 | struct page_counter *parent; |
| 12 | |
| 13 | /* legacy */ |
| 14 | unsigned long watermark; |
| 15 | unsigned long failcnt; |
| 16 | }; |
| 17 | |
| 18 | #if BITS_PER_LONG == 32 |
| 19 | #define PAGE_COUNTER_MAX LONG_MAX |
| 20 | #else |
| 21 | #define PAGE_COUNTER_MAX (LONG_MAX / PAGE_SIZE) |
| 22 | #endif |
| 23 | |
| 24 | static inline void page_counter_init(struct page_counter *counter, |
| 25 | struct page_counter *parent) |
| 26 | { |
| 27 | atomic_long_set(&counter->count, 0); |
| 28 | counter->limit = PAGE_COUNTER_MAX; |
| 29 | counter->parent = parent; |
| 30 | } |
| 31 | |
| 32 | static inline unsigned long page_counter_read(struct page_counter *counter) |
| 33 | { |
| 34 | return atomic_long_read(&counter->count); |
| 35 | } |
| 36 | |
Johannes Weiner | 64f2199 | 2014-12-10 15:42:45 -0800 | [diff] [blame] | 37 | void page_counter_cancel(struct page_counter *counter, unsigned long nr_pages); |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 38 | void page_counter_charge(struct page_counter *counter, unsigned long nr_pages); |
| 39 | int page_counter_try_charge(struct page_counter *counter, |
| 40 | unsigned long nr_pages, |
| 41 | struct page_counter **fail); |
Johannes Weiner | 64f2199 | 2014-12-10 15:42:45 -0800 | [diff] [blame] | 42 | void page_counter_uncharge(struct page_counter *counter, unsigned long nr_pages); |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 43 | int page_counter_limit(struct page_counter *counter, unsigned long limit); |
Johannes Weiner | 650c5e5 | 2015-02-11 15:26:03 -0800 | [diff] [blame] | 44 | int page_counter_memparse(const char *buf, const char *max, |
| 45 | unsigned long *nr_pages); |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 46 | |
| 47 | static inline void page_counter_reset_watermark(struct page_counter *counter) |
| 48 | { |
| 49 | counter->watermark = page_counter_read(counter); |
| 50 | } |
| 51 | |
| 52 | #endif /* _LINUX_PAGE_COUNTER_H */ |