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 * Returns whether there are remaining pages in the counter.
22 int page_counter_cancel(struct page_counter
*counter
, unsigned long nr_pages
)
26 new = atomic_long_sub_return(nr_pages
, &counter
->count
);
28 /* More uncharges than charges? */
29 WARN_ON_ONCE(new < 0);
35 * page_counter_charge - hierarchically charge pages
37 * @nr_pages: number of pages to charge
39 * NOTE: This does not consider any configured counter limits.
41 void page_counter_charge(struct page_counter
*counter
, unsigned long nr_pages
)
43 struct page_counter
*c
;
45 for (c
= counter
; c
; c
= c
->parent
) {
48 new = atomic_long_add_return(nr_pages
, &c
->count
);
50 * This is indeed racy, but we can live with some
51 * inaccuracy in the watermark.
53 if (new > c
->watermark
)
59 * page_counter_try_charge - try to hierarchically charge pages
61 * @nr_pages: number of pages to charge
62 * @fail: points first counter to hit its limit, if any
64 * Returns 0 on success, or -ENOMEM and @fail if the counter or one of
65 * its ancestors has hit its configured limit.
67 int page_counter_try_charge(struct page_counter
*counter
,
68 unsigned long nr_pages
,
69 struct page_counter
**fail
)
71 struct page_counter
*c
;
73 for (c
= counter
; c
; c
= c
->parent
) {
76 * Charge speculatively to avoid an expensive CAS. If
77 * a bigger charge fails, it might falsely lock out a
78 * racing smaller charge and send it into reclaim
79 * early, but the error is limited to the difference
80 * between the two sizes, which is less than 2M/4M in
81 * case of a THP locking out a regular page charge.
83 * The atomic_long_add_return() implies a full memory
84 * barrier between incrementing the count and reading
85 * the limit. When racing with page_counter_limit(),
86 * we either see the new limit or the setter sees the
87 * counter has changed and retries.
89 new = atomic_long_add_return(nr_pages
, &c
->count
);
91 atomic_long_sub(nr_pages
, &c
->count
);
93 * This is racy, but we can live with some
94 * inaccuracy in the failcnt.
101 * Just like with failcnt, we can live with some
102 * inaccuracy in the watermark.
104 if (new > c
->watermark
)
110 for (c
= counter
; c
!= *fail
; c
= c
->parent
)
111 page_counter_cancel(c
, nr_pages
);
117 * page_counter_uncharge - hierarchically uncharge pages
119 * @nr_pages: number of pages to uncharge
121 * Returns whether there are remaining charges in @counter.
123 int page_counter_uncharge(struct page_counter
*counter
, unsigned long nr_pages
)
125 struct page_counter
*c
;
128 for (c
= counter
; c
; c
= c
->parent
) {
131 remainder
= page_counter_cancel(c
, nr_pages
);
132 if (c
== counter
&& !remainder
)
140 * page_counter_limit - limit the number of pages allowed
142 * @limit: limit to set
144 * Returns 0 on success, -EBUSY if the current number of pages on the
145 * counter already exceeds the specified limit.
147 * The caller must serialize invocations on the same counter.
149 int page_counter_limit(struct page_counter
*counter
, unsigned long limit
)
156 * Update the limit while making sure that it's not
157 * below the concurrently-changing counter value.
159 * The xchg implies two full memory barriers before
160 * and after, so the read-swap-read is ordered and
161 * ensures coherency with page_counter_try_charge():
162 * that function modifies the count before checking
163 * the limit, so if it sees the old limit, we see the
164 * modified counter and retry.
166 count
= atomic_long_read(&counter
->count
);
171 old
= xchg(&counter
->limit
, limit
);
173 if (atomic_long_read(&counter
->count
) <= count
)
176 counter
->limit
= old
;
182 * page_counter_memparse - memparse() for page counter limits
183 * @buf: string to parse
184 * @nr_pages: returns the result in number of pages
186 * Returns -EINVAL, or 0 and @nr_pages on success. @nr_pages will be
187 * limited to %PAGE_COUNTER_MAX.
189 int page_counter_memparse(const char *buf
, unsigned long *nr_pages
)
191 char unlimited
[] = "-1";
195 if (!strncmp(buf
, unlimited
, sizeof(unlimited
))) {
196 *nr_pages
= PAGE_COUNTER_MAX
;
200 bytes
= memparse(buf
, &end
);
204 *nr_pages
= min(bytes
/ PAGE_SIZE
, (u64
)PAGE_COUNTER_MAX
);
This page took 0.034628 seconds and 5 git commands to generate.