res_counter.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * resource cgroups
  3. *
  4. * Copyright 2007 OpenVZ SWsoft Inc
  5. *
  6. * Author: Pavel Emelianov <xemul@openvz.org>
  7. *
  8. */
  9. #include <linux/types.h>
  10. #include <linux/parser.h>
  11. #include <linux/fs.h>
  12. #include <linux/res_counter.h>
  13. #include <linux/uaccess.h>
  14. #include <linux/mm.h>
  15. void res_counter_init(struct res_counter *counter, struct res_counter *parent)
  16. {
  17. spin_lock_init(&counter->lock);
  18. counter->limit = RESOURCE_MAX;
  19. counter->soft_limit = RESOURCE_MAX;
  20. counter->parent = parent;
  21. }
  22. int res_counter_charge_locked(struct res_counter *counter, unsigned long val)
  23. {
  24. if (counter->usage + val > counter->limit) {
  25. counter->failcnt++;
  26. return -ENOMEM;
  27. }
  28. counter->usage += val;
  29. if (counter->usage > counter->max_usage)
  30. counter->max_usage = counter->usage;
  31. return 0;
  32. }
  33. int res_counter_charge(struct res_counter *counter, unsigned long val,
  34. struct res_counter **limit_fail_at)
  35. {
  36. int ret;
  37. unsigned long flags;
  38. struct res_counter *c, *u;
  39. *limit_fail_at = NULL;
  40. local_irq_save(flags);
  41. for (c = counter; c != NULL; c = c->parent) {
  42. spin_lock(&c->lock);
  43. ret = res_counter_charge_locked(c, val);
  44. spin_unlock(&c->lock);
  45. if (ret < 0) {
  46. *limit_fail_at = c;
  47. goto undo;
  48. }
  49. }
  50. ret = 0;
  51. goto done;
  52. undo:
  53. for (u = counter; u != c; u = u->parent) {
  54. spin_lock(&u->lock);
  55. res_counter_uncharge_locked(u, val);
  56. spin_unlock(&u->lock);
  57. }
  58. done:
  59. local_irq_restore(flags);
  60. return ret;
  61. }
  62. int res_counter_charge_nofail(struct res_counter *counter, unsigned long val,
  63. struct res_counter **limit_fail_at)
  64. {
  65. int ret, r;
  66. unsigned long flags;
  67. struct res_counter *c;
  68. r = ret = 0;
  69. *limit_fail_at = NULL;
  70. local_irq_save(flags);
  71. for (c = counter; c != NULL; c = c->parent) {
  72. spin_lock(&c->lock);
  73. r = res_counter_charge_locked(c, val);
  74. if (r)
  75. c->usage += val;
  76. spin_unlock(&c->lock);
  77. if (r < 0 && ret == 0) {
  78. *limit_fail_at = c;
  79. ret = r;
  80. }
  81. }
  82. local_irq_restore(flags);
  83. return ret;
  84. }
  85. void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val)
  86. {
  87. if (WARN_ON(counter->usage < val))
  88. val = counter->usage;
  89. counter->usage -= val;
  90. }
  91. void res_counter_uncharge(struct res_counter *counter, unsigned long val)
  92. {
  93. unsigned long flags;
  94. struct res_counter *c;
  95. local_irq_save(flags);
  96. for (c = counter; c != NULL; c = c->parent) {
  97. spin_lock(&c->lock);
  98. res_counter_uncharge_locked(c, val);
  99. spin_unlock(&c->lock);
  100. }
  101. local_irq_restore(flags);
  102. }
  103. static inline unsigned long long *
  104. res_counter_member(struct res_counter *counter, int member)
  105. {
  106. switch (member) {
  107. case RES_USAGE:
  108. return &counter->usage;
  109. case RES_MAX_USAGE:
  110. return &counter->max_usage;
  111. case RES_LIMIT:
  112. return &counter->limit;
  113. case RES_FAILCNT:
  114. return &counter->failcnt;
  115. case RES_SOFT_LIMIT:
  116. return &counter->soft_limit;
  117. };
  118. BUG();
  119. return NULL;
  120. }
  121. ssize_t res_counter_read(struct res_counter *counter, int member,
  122. const char __user *userbuf, size_t nbytes, loff_t *pos,
  123. int (*read_strategy)(unsigned long long val, char *st_buf))
  124. {
  125. unsigned long long *val;
  126. char buf[64], *s;
  127. s = buf;
  128. val = res_counter_member(counter, member);
  129. if (read_strategy)
  130. s += read_strategy(*val, s);
  131. else
  132. s += sprintf(s, "%llu\n", *val);
  133. return simple_read_from_buffer((void __user *)userbuf, nbytes,
  134. pos, buf, s - buf);
  135. }
  136. #if BITS_PER_LONG == 32
  137. u64 res_counter_read_u64(struct res_counter *counter, int member)
  138. {
  139. unsigned long flags;
  140. u64 ret;
  141. spin_lock_irqsave(&counter->lock, flags);
  142. ret = *res_counter_member(counter, member);
  143. spin_unlock_irqrestore(&counter->lock, flags);
  144. return ret;
  145. }
  146. #else
  147. u64 res_counter_read_u64(struct res_counter *counter, int member)
  148. {
  149. return *res_counter_member(counter, member);
  150. }
  151. #endif
  152. int res_counter_memparse_write_strategy(const char *buf,
  153. unsigned long long *res)
  154. {
  155. char *end;
  156. /* return RESOURCE_MAX(unlimited) if "-1" is specified */
  157. if (*buf == '-') {
  158. *res = simple_strtoull(buf + 1, &end, 10);
  159. if (*res != 1 || *end != '\0')
  160. return -EINVAL;
  161. *res = RESOURCE_MAX;
  162. return 0;
  163. }
  164. *res = memparse(buf, &end);
  165. if (*end != '\0')
  166. return -EINVAL;
  167. *res = PAGE_ALIGN(*res);
  168. return 0;
  169. }
  170. int res_counter_write(struct res_counter *counter, int member,
  171. const char *buf, write_strategy_fn write_strategy)
  172. {
  173. char *end;
  174. unsigned long flags;
  175. unsigned long long tmp, *val;
  176. if (write_strategy) {
  177. if (write_strategy(buf, &tmp))
  178. return -EINVAL;
  179. } else {
  180. tmp = simple_strtoull(buf, &end, 10);
  181. if (*end != '\0')
  182. return -EINVAL;
  183. }
  184. spin_lock_irqsave(&counter->lock, flags);
  185. val = res_counter_member(counter, member);
  186. *val = tmp;
  187. spin_unlock_irqrestore(&counter->lock, flags);
  188. return 0;
  189. }