page_counter.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Lockless hierarchical page accounting & limiting
  3. *
  4. * Copyright (C) 2014 Red Hat, Inc., Johannes Weiner
  5. */
  6. #include <linux/page_counter.h>
  7. #include <linux/atomic.h>
  8. #include <linux/kernel.h>
  9. #include <linux/string.h>
  10. #include <linux/sched.h>
  11. #include <linux/bug.h>
  12. #include <asm/page.h>
  13. /**
  14. * page_counter_cancel - take pages out of the local counter
  15. * @counter: counter
  16. * @nr_pages: number of pages to cancel
  17. */
  18. void page_counter_cancel(struct page_counter *counter, unsigned long nr_pages)
  19. {
  20. long new;
  21. new = atomic_long_sub_return(nr_pages, &counter->count);
  22. /* More uncharges than charges? */
  23. WARN_ON_ONCE(new < 0);
  24. }
  25. /**
  26. * page_counter_charge - hierarchically charge pages
  27. * @counter: counter
  28. * @nr_pages: number of pages to charge
  29. *
  30. * NOTE: This does not consider any configured counter limits.
  31. */
  32. void page_counter_charge(struct page_counter *counter, unsigned long nr_pages)
  33. {
  34. struct page_counter *c;
  35. for (c = counter; c; c = c->parent) {
  36. long new;
  37. new = atomic_long_add_return(nr_pages, &c->count);
  38. /*
  39. * This is indeed racy, but we can live with some
  40. * inaccuracy in the watermark.
  41. */
  42. if (new > c->watermark)
  43. c->watermark = new;
  44. }
  45. }
  46. /**
  47. * page_counter_try_charge - try to hierarchically charge pages
  48. * @counter: counter
  49. * @nr_pages: number of pages to charge
  50. * @fail: points first counter to hit its limit, if any
  51. *
  52. * Returns %true on success, or %false and @fail if the counter or one
  53. * of its ancestors has hit its configured limit.
  54. */
  55. bool page_counter_try_charge(struct page_counter *counter,
  56. unsigned long nr_pages,
  57. struct page_counter **fail)
  58. {
  59. struct page_counter *c;
  60. for (c = counter; c; c = c->parent) {
  61. long new;
  62. /*
  63. * Charge speculatively to avoid an expensive CAS. If
  64. * a bigger charge fails, it might falsely lock out a
  65. * racing smaller charge and send it into reclaim
  66. * early, but the error is limited to the difference
  67. * between the two sizes, which is less than 2M/4M in
  68. * case of a THP locking out a regular page charge.
  69. *
  70. * The atomic_long_add_return() implies a full memory
  71. * barrier between incrementing the count and reading
  72. * the limit. When racing with page_counter_limit(),
  73. * we either see the new limit or the setter sees the
  74. * counter has changed and retries.
  75. */
  76. new = atomic_long_add_return(nr_pages, &c->count);
  77. if (new > c->limit) {
  78. atomic_long_sub(nr_pages, &c->count);
  79. /*
  80. * This is racy, but we can live with some
  81. * inaccuracy in the failcnt.
  82. */
  83. c->failcnt++;
  84. *fail = c;
  85. goto failed;
  86. }
  87. /*
  88. * Just like with failcnt, we can live with some
  89. * inaccuracy in the watermark.
  90. */
  91. if (new > c->watermark)
  92. c->watermark = new;
  93. }
  94. return true;
  95. failed:
  96. for (c = counter; c != *fail; c = c->parent)
  97. page_counter_cancel(c, nr_pages);
  98. return false;
  99. }
  100. /**
  101. * page_counter_uncharge - hierarchically uncharge pages
  102. * @counter: counter
  103. * @nr_pages: number of pages to uncharge
  104. */
  105. void page_counter_uncharge(struct page_counter *counter, unsigned long nr_pages)
  106. {
  107. struct page_counter *c;
  108. for (c = counter; c; c = c->parent)
  109. page_counter_cancel(c, nr_pages);
  110. }
  111. /**
  112. * page_counter_limit - limit the number of pages allowed
  113. * @counter: counter
  114. * @limit: limit to set
  115. *
  116. * Returns 0 on success, -EBUSY if the current number of pages on the
  117. * counter already exceeds the specified limit.
  118. *
  119. * The caller must serialize invocations on the same counter.
  120. */
  121. int page_counter_limit(struct page_counter *counter, unsigned long limit)
  122. {
  123. for (;;) {
  124. unsigned long old;
  125. long count;
  126. /*
  127. * Update the limit while making sure that it's not
  128. * below the concurrently-changing counter value.
  129. *
  130. * The xchg implies two full memory barriers before
  131. * and after, so the read-swap-read is ordered and
  132. * ensures coherency with page_counter_try_charge():
  133. * that function modifies the count before checking
  134. * the limit, so if it sees the old limit, we see the
  135. * modified counter and retry.
  136. */
  137. count = atomic_long_read(&counter->count);
  138. if (count > limit)
  139. return -EBUSY;
  140. old = xchg(&counter->limit, limit);
  141. if (atomic_long_read(&counter->count) <= count)
  142. return 0;
  143. counter->limit = old;
  144. cond_resched();
  145. }
  146. }
  147. /**
  148. * page_counter_memparse - memparse() for page counter limits
  149. * @buf: string to parse
  150. * @max: string meaning maximum possible value
  151. * @nr_pages: returns the result in number of pages
  152. *
  153. * Returns -EINVAL, or 0 and @nr_pages on success. @nr_pages will be
  154. * limited to %PAGE_COUNTER_MAX.
  155. */
  156. int page_counter_memparse(const char *buf, const char *max,
  157. unsigned long *nr_pages)
  158. {
  159. char *end;
  160. u64 bytes;
  161. if (!strcmp(buf, max)) {
  162. *nr_pages = PAGE_COUNTER_MAX;
  163. return 0;
  164. }
  165. bytes = memparse(buf, &end);
  166. if (*end != '\0')
  167. return -EINVAL;
  168. *nr_pages = min(bytes / PAGE_SIZE, (u64)PAGE_COUNTER_MAX);
  169. return 0;
  170. }