Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 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 { |
Roman Gushchin | bbec2e1 | 2018-06-07 17:06:18 -0700 | [diff] [blame] | 10 | atomic_long_t usage; |
Roman Gushchin | bf8d5d5 | 2018-06-07 17:07:46 -0700 | [diff] [blame] | 11 | unsigned long min; |
Roman Gushchin | 2306715 | 2018-06-07 17:06:22 -0700 | [diff] [blame] | 12 | unsigned long low; |
Roman Gushchin | bf8d5d5 | 2018-06-07 17:07:46 -0700 | [diff] [blame] | 13 | unsigned long max; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 14 | struct page_counter *parent; |
| 15 | |
Roman Gushchin | bf8d5d5 | 2018-06-07 17:07:46 -0700 | [diff] [blame] | 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 | |
Roman Gushchin | 2306715 | 2018-06-07 17:06:22 -0700 | [diff] [blame] | 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 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 26 | /* legacy */ |
| 27 | unsigned long watermark; |
| 28 | unsigned long failcnt; |
| 29 | }; |
| 30 | |
| 31 | #if BITS_PER_LONG == 32 |
| 32 | #define PAGE_COUNTER_MAX LONG_MAX |
| 33 | #else |
| 34 | #define PAGE_COUNTER_MAX (LONG_MAX / PAGE_SIZE) |
| 35 | #endif |
| 36 | |
| 37 | static inline void page_counter_init(struct page_counter *counter, |
| 38 | struct page_counter *parent) |
| 39 | { |
Roman Gushchin | bbec2e1 | 2018-06-07 17:06:18 -0700 | [diff] [blame] | 40 | atomic_long_set(&counter->usage, 0); |
| 41 | counter->max = PAGE_COUNTER_MAX; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 42 | counter->parent = parent; |
| 43 | } |
| 44 | |
| 45 | static inline unsigned long page_counter_read(struct page_counter *counter) |
| 46 | { |
Roman Gushchin | bbec2e1 | 2018-06-07 17:06:18 -0700 | [diff] [blame] | 47 | return atomic_long_read(&counter->usage); |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Johannes Weiner | 64f2199 | 2014-12-10 15:42:45 -0800 | [diff] [blame] | 50 | void page_counter_cancel(struct page_counter *counter, unsigned long nr_pages); |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 51 | void page_counter_charge(struct page_counter *counter, unsigned long nr_pages); |
Johannes Weiner | 6071ca5 | 2015-11-05 18:50:26 -0800 | [diff] [blame] | 52 | bool page_counter_try_charge(struct page_counter *counter, |
| 53 | unsigned long nr_pages, |
| 54 | struct page_counter **fail); |
Johannes Weiner | 64f2199 | 2014-12-10 15:42:45 -0800 | [diff] [blame] | 55 | void page_counter_uncharge(struct page_counter *counter, unsigned long nr_pages); |
Roman Gushchin | bf8d5d5 | 2018-06-07 17:07:46 -0700 | [diff] [blame] | 56 | void page_counter_set_min(struct page_counter *counter, unsigned long nr_pages); |
Roman Gushchin | 2306715 | 2018-06-07 17:06:22 -0700 | [diff] [blame] | 57 | void page_counter_set_low(struct page_counter *counter, unsigned long nr_pages); |
Roman Gushchin | bf8d5d5 | 2018-06-07 17:07:46 -0700 | [diff] [blame] | 58 | int page_counter_set_max(struct page_counter *counter, unsigned long nr_pages); |
Johannes Weiner | 650c5e5 | 2015-02-11 15:26:03 -0800 | [diff] [blame] | 59 | int page_counter_memparse(const char *buf, const char *max, |
| 60 | unsigned long *nr_pages); |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 61 | |
| 62 | static inline void page_counter_reset_watermark(struct page_counter *counter) |
| 63 | { |
| 64 | counter->watermark = page_counter_read(counter); |
| 65 | } |
| 66 | |
| 67 | #endif /* _LINUX_PAGE_COUNTER_H */ |