2 * Lockless hierarchical page accounting & limiting
4 * Copyright (C) 2014 Red Hat, Inc., Johannes Weiner
7 #include <linux/page_counter.h>
8 #include <linux/atomic.h>
9 #include <linux/kernel.h>
10 #include <linux/string.h>
11 #include <linux/sched.h>
12 #include <linux/bug.h>
16 * page_counter_cancel - take pages out of the local counter
18 * @nr_pages: number of pages to cancel
20 void page_counter_cancel(struct page_counter *counter, unsigned long nr_pages)
24 new = atomic_long_sub_return(nr_pages, &counter->count);
25 /* More uncharges than charges? */
26 WARN_ON_ONCE(new < 0);
30 * page_counter_charge - hierarchically charge pages
32 * @nr_pages: number of pages to charge
34 * NOTE: This does not consider any configured counter limits.
36 void page_counter_charge(struct page_counter *counter, unsigned long nr_pages)
38 struct page_counter *c;
40 for (c = counter; c; c = c->parent) {
43 new = atomic_long_add_return(nr_pages, &c->count);
45 * This is indeed racy, but we can live with some
46 * inaccuracy in the watermark.
48 if (new > c->watermark)
54 * page_counter_try_charge - try to hierarchically charge pages
56 * @nr_pages: number of pages to charge
57 * @fail: points first counter to hit its limit, if any
59 * Returns 0 on success, or -ENOMEM and @fail if the counter or one of
60 * its ancestors has hit its configured limit.
62 int page_counter_try_charge(struct page_counter *counter,
63 unsigned long nr_pages,
64 struct page_counter **fail)
66 struct page_counter *c;
68 for (c = counter; c; c = c->parent) {
71 * Charge speculatively to avoid an expensive CAS. If
72 * a bigger charge fails, it might falsely lock out a
73 * racing smaller charge and send it into reclaim
74 * early, but the error is limited to the difference
75 * between the two sizes, which is less than 2M/4M in
76 * case of a THP locking out a regular page charge.
78 * The atomic_long_add_return() implies a full memory
79 * barrier between incrementing the count and reading
80 * the limit. When racing with page_counter_limit(),
81 * we either see the new limit or the setter sees the
82 * counter has changed and retries.
84 new = atomic_long_add_return(nr_pages, &c->count);
86 atomic_long_sub(nr_pages, &c->count);
88 * This is racy, but we can live with some
89 * inaccuracy in the failcnt.
96 * Just like with failcnt, we can live with some
97 * inaccuracy in the watermark.
99 if (new > c->watermark)
105 for (c = counter; c != *fail; c = c->parent)
106 page_counter_cancel(c, nr_pages);
112 * page_counter_uncharge - hierarchically uncharge pages
114 * @nr_pages: number of pages to uncharge
116 void page_counter_uncharge(struct page_counter *counter, unsigned long nr_pages)
118 struct page_counter *c;
120 for (c = counter; c; c = c->parent)
121 page_counter_cancel(c, nr_pages);
125 * page_counter_limit - limit the number of pages allowed
127 * @limit: limit to set
129 * Returns 0 on success, -EBUSY if the current number of pages on the
130 * counter already exceeds the specified limit.
132 * The caller must serialize invocations on the same counter.
134 int page_counter_limit(struct page_counter *counter, unsigned long limit)
141 * Update the limit while making sure that it's not
142 * below the concurrently-changing counter value.
144 * The xchg implies two full memory barriers before
145 * and after, so the read-swap-read is ordered and
146 * ensures coherency with page_counter_try_charge():
147 * that function modifies the count before checking
148 * the limit, so if it sees the old limit, we see the
149 * modified counter and retry.
151 count = atomic_long_read(&counter->count);
156 old = xchg(&counter->limit, limit);
158 if (atomic_long_read(&counter->count) <= count)
161 counter->limit = old;
167 * page_counter_memparse - memparse() for page counter limits
168 * @buf: string to parse
169 * @max: string meaning maximum possible value
170 * @nr_pages: returns the result in number of pages
172 * Returns -EINVAL, or 0 and @nr_pages on success. @nr_pages will be
173 * limited to %PAGE_COUNTER_MAX.
175 int page_counter_memparse(const char *buf, const char *max,
176 unsigned long *nr_pages)
181 if (!strcmp(buf, max)) {
182 *nr_pages = PAGE_COUNTER_MAX;
186 bytes = memparse(buf, &end);
190 *nr_pages = min(bytes / PAGE_SIZE, (u64)PAGE_COUNTER_MAX);