page_counter.c 5.0 KB

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