blob: c15ab80ad32de4387879d29421b77521a73556e4 [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 {
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
25static 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
33static inline unsigned long page_counter_read(struct page_counter *counter)
34{
35 return atomic_long_read(&counter->count);
36}
37
Johannes Weiner64f21992014-12-10 15:42:45 -080038void page_counter_cancel(struct page_counter *counter, unsigned long nr_pages);
Johannes Weiner3e32cb22014-12-10 15:42:31 -080039void page_counter_charge(struct page_counter *counter, unsigned long nr_pages);
Johannes Weiner6071ca52015-11-05 18:50:26 -080040bool page_counter_try_charge(struct page_counter *counter,
41 unsigned long nr_pages,
42 struct page_counter **fail);
Johannes Weiner64f21992014-12-10 15:42:45 -080043void page_counter_uncharge(struct page_counter *counter, unsigned long nr_pages);
Johannes Weiner3e32cb22014-12-10 15:42:31 -080044int page_counter_limit(struct page_counter *counter, unsigned long limit);
Johannes Weiner650c5e52015-02-11 15:26:03 -080045int page_counter_memparse(const char *buf, const char *max,
46 unsigned long *nr_pages);
Johannes Weiner3e32cb22014-12-10 15:42:31 -080047
48static 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 */