1 // SPDX-License-Identifier: GPL-2.0
3 * Lockless hierarchical page accounting & limiting
5 * Copyright (C) 2014 Red Hat, Inc., Johannes Weiner
8 #include <linux/page_counter.h>
9 #include <linux/atomic.h>
10 #include <linux/kernel.h>
11 #include <linux/string.h>
12 #include <linux/sched.h>
13 #include <linux/bug.h>
16 static void propagate_protected_usage(struct page_counter
*c
,
19 unsigned long protected, old_protected
;
20 unsigned long low
, min
;
26 min
= READ_ONCE(c
->min
);
27 if (min
|| atomic_long_read(&c
->min_usage
)) {
28 protected = min(usage
, min
);
29 old_protected
= atomic_long_xchg(&c
->min_usage
, protected);
30 delta
= protected - old_protected
;
32 atomic_long_add(delta
, &c
->parent
->children_min_usage
);
35 low
= READ_ONCE(c
->low
);
36 if (low
|| atomic_long_read(&c
->low_usage
)) {
37 protected = min(usage
, low
);
38 old_protected
= atomic_long_xchg(&c
->low_usage
, protected);
39 delta
= protected - old_protected
;
41 atomic_long_add(delta
, &c
->parent
->children_low_usage
);
46 * page_counter_cancel - take pages out of the local counter
48 * @nr_pages: number of pages to cancel
50 void page_counter_cancel(struct page_counter
*counter
, unsigned long nr_pages
)
54 new = atomic_long_sub_return(nr_pages
, &counter
->usage
);
55 propagate_protected_usage(counter
, new);
56 /* More uncharges than charges? */
57 WARN_ON_ONCE(new < 0);
61 * page_counter_charge - hierarchically charge pages
63 * @nr_pages: number of pages to charge
65 * NOTE: This does not consider any configured counter limits.
67 void page_counter_charge(struct page_counter
*counter
, unsigned long nr_pages
)
69 struct page_counter
*c
;
71 for (c
= counter
; c
; c
= c
->parent
) {
74 new = atomic_long_add_return(nr_pages
, &c
->usage
);
75 propagate_protected_usage(c
, new);
77 * This is indeed racy, but we can live with some
78 * inaccuracy in the watermark.
80 if (new > READ_ONCE(c
->watermark
))
81 WRITE_ONCE(c
->watermark
, new);
86 * page_counter_try_charge - try to hierarchically charge pages
88 * @nr_pages: number of pages to charge
89 * @fail: points first counter to hit its limit, if any
91 * Returns %true on success, or %false and @fail if the counter or one
92 * of its ancestors has hit its configured limit.
94 bool page_counter_try_charge(struct page_counter
*counter
,
95 unsigned long nr_pages
,
96 struct page_counter
**fail
)
98 struct page_counter
*c
;
100 for (c
= counter
; c
; c
= c
->parent
) {
103 * Charge speculatively to avoid an expensive CAS. If
104 * a bigger charge fails, it might falsely lock out a
105 * racing smaller charge and send it into reclaim
106 * early, but the error is limited to the difference
107 * between the two sizes, which is less than 2M/4M in
108 * case of a THP locking out a regular page charge.
110 * The atomic_long_add_return() implies a full memory
111 * barrier between incrementing the count and reading
112 * the limit. When racing with page_counter_limit(),
113 * we either see the new limit or the setter sees the
114 * counter has changed and retries.
116 new = atomic_long_add_return(nr_pages
, &c
->usage
);
118 atomic_long_sub(nr_pages
, &c
->usage
);
119 propagate_protected_usage(c
, new);
121 * This is racy, but we can live with some
122 * inaccuracy in the failcnt which is only used
125 data_race(c
->failcnt
++);
129 propagate_protected_usage(c
, new);
131 * Just like with failcnt, we can live with some
132 * inaccuracy in the watermark.
134 if (new > READ_ONCE(c
->watermark
))
135 WRITE_ONCE(c
->watermark
, new);
140 for (c
= counter
; c
!= *fail
; c
= c
->parent
)
141 page_counter_cancel(c
, nr_pages
);
147 * page_counter_uncharge - hierarchically uncharge pages
149 * @nr_pages: number of pages to uncharge
151 void page_counter_uncharge(struct page_counter
*counter
, unsigned long nr_pages
)
153 struct page_counter
*c
;
155 for (c
= counter
; c
; c
= c
->parent
)
156 page_counter_cancel(c
, nr_pages
);
160 * page_counter_set_max - set the maximum number of pages allowed
162 * @nr_pages: limit to set
164 * Returns 0 on success, -EBUSY if the current number of pages on the
165 * counter already exceeds the specified limit.
167 * The caller must serialize invocations on the same counter.
169 int page_counter_set_max(struct page_counter
*counter
, unsigned long nr_pages
)
176 * Update the limit while making sure that it's not
177 * below the concurrently-changing counter value.
179 * The xchg implies two full memory barriers before
180 * and after, so the read-swap-read is ordered and
181 * ensures coherency with page_counter_try_charge():
182 * that function modifies the count before checking
183 * the limit, so if it sees the old limit, we see the
184 * modified counter and retry.
186 usage
= atomic_long_read(&counter
->usage
);
188 if (usage
> nr_pages
)
191 old
= xchg(&counter
->max
, nr_pages
);
193 if (atomic_long_read(&counter
->usage
) <= usage
)
202 * page_counter_set_min - set the amount of protected memory
204 * @nr_pages: value to set
206 * The caller must serialize invocations on the same counter.
208 void page_counter_set_min(struct page_counter
*counter
, unsigned long nr_pages
)
210 struct page_counter
*c
;
212 WRITE_ONCE(counter
->min
, nr_pages
);
214 for (c
= counter
; c
; c
= c
->parent
)
215 propagate_protected_usage(c
, atomic_long_read(&c
->usage
));
219 * page_counter_set_low - set the amount of protected memory
221 * @nr_pages: value to set
223 * The caller must serialize invocations on the same counter.
225 void page_counter_set_low(struct page_counter
*counter
, unsigned long nr_pages
)
227 struct page_counter
*c
;
229 WRITE_ONCE(counter
->low
, nr_pages
);
231 for (c
= counter
; c
; c
= c
->parent
)
232 propagate_protected_usage(c
, atomic_long_read(&c
->usage
));
236 * page_counter_memparse - memparse() for page counter limits
237 * @buf: string to parse
238 * @max: string meaning maximum possible value
239 * @nr_pages: returns the result in number of pages
241 * Returns -EINVAL, or 0 and @nr_pages on success. @nr_pages will be
242 * limited to %PAGE_COUNTER_MAX.
244 int page_counter_memparse(const char *buf
, const char *max
,
245 unsigned long *nr_pages
)
250 if (!strcmp(buf
, max
)) {
251 *nr_pages
= PAGE_COUNTER_MAX
;
255 bytes
= memparse(buf
, &end
);
259 *nr_pages
= min(bytes
/ PAGE_SIZE
, (u64
)PAGE_COUNTER_MAX
);