blob: bab7e57f659b4c03d75548c32edcb12dcab86911 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002#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
9struct page_counter {
Roman Gushchinbbec2e12018-06-07 17:06:18 -070010 atomic_long_t usage;
Roman Gushchinbf8d5d52018-06-07 17:07:46 -070011 unsigned long min;
Roman Gushchin23067152018-06-07 17:06:22 -070012 unsigned long low;
Roman Gushchinbf8d5d52018-06-07 17:07:46 -070013 unsigned long max;
Johannes Weiner3e32cb22014-12-10 15:42:31 -080014 struct page_counter *parent;
15
Roman Gushchinbf8d5d52018-06-07 17:07:46 -070016 /* 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 Gushchin23067152018-06-07 17:06:22 -070021 /* 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 Weiner3e32cb22014-12-10 15:42:31 -080026 /* 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
37static inline void page_counter_init(struct page_counter *counter,
38 struct page_counter *parent)
39{
Roman Gushchinbbec2e12018-06-07 17:06:18 -070040 atomic_long_set(&counter->usage, 0);
41 counter->max = PAGE_COUNTER_MAX;
Johannes Weiner3e32cb22014-12-10 15:42:31 -080042 counter->parent = parent;
43}
44
45static inline unsigned long page_counter_read(struct page_counter *counter)
46{
Roman Gushchinbbec2e12018-06-07 17:06:18 -070047 return atomic_long_read(&counter->usage);
Johannes Weiner3e32cb22014-12-10 15:42:31 -080048}
49
Johannes Weiner64f21992014-12-10 15:42:45 -080050void page_counter_cancel(struct page_counter *counter, unsigned long nr_pages);
Johannes Weiner3e32cb22014-12-10 15:42:31 -080051void page_counter_charge(struct page_counter *counter, unsigned long nr_pages);
Johannes Weiner6071ca52015-11-05 18:50:26 -080052bool page_counter_try_charge(struct page_counter *counter,
53 unsigned long nr_pages,
54 struct page_counter **fail);
Johannes Weiner64f21992014-12-10 15:42:45 -080055void page_counter_uncharge(struct page_counter *counter, unsigned long nr_pages);
Roman Gushchinbf8d5d52018-06-07 17:07:46 -070056void page_counter_set_min(struct page_counter *counter, unsigned long nr_pages);
Roman Gushchin23067152018-06-07 17:06:22 -070057void page_counter_set_low(struct page_counter *counter, unsigned long nr_pages);
Roman Gushchinbf8d5d52018-06-07 17:07:46 -070058int page_counter_set_max(struct page_counter *counter, unsigned long nr_pages);
Johannes Weiner650c5e52015-02-11 15:26:03 -080059int page_counter_memparse(const char *buf, const char *max,
60 unsigned long *nr_pages);
Johannes Weiner3e32cb22014-12-10 15:42:31 -080061
62static 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 */