res_counter.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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/slab.h>
  13. #include <linux/res_counter.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/mm.h>
  16. void res_counter_init(struct res_counter *counter, struct res_counter *parent)
  17. {
  18. spin_lock_init(&counter->lock);
  19. counter->limit = RESOURCE_MAX;
  20. counter->soft_limit = RESOURCE_MAX;
  21. counter->parent = parent;
  22. }
  23. int res_counter_charge_locked(struct res_counter *counter, unsigned long val)
  24. {
  25. if (counter->usage + val > counter->limit) {
  26. counter->failcnt++;
  27. return -ENOMEM;
  28. }
  29. counter->usage += val;
  30. if (counter->usage > counter->max_usage)
  31. counter->max_usage = counter->usage;
  32. return 0;
  33. }
  34. int res_counter_charge(struct res_counter *counter, unsigned long val,
  35. struct res_counter **limit_fail_at,
  36. struct res_counter **soft_limit_fail_at)
  37. {
  38. int ret;
  39. unsigned long flags;
  40. struct res_counter *c, *u;
  41. *limit_fail_at = NULL;
  42. if (soft_limit_fail_at)
  43. *soft_limit_fail_at = NULL;
  44. local_irq_save(flags);
  45. for (c = counter; c != NULL; c = c->parent) {
  46. spin_lock(&c->lock);
  47. ret = res_counter_charge_locked(c, val);
  48. /*
  49. * With soft limits, we return the highest ancestor
  50. * that exceeds its soft limit
  51. */
  52. if (soft_limit_fail_at &&
  53. !res_counter_soft_limit_check_locked(c))
  54. *soft_limit_fail_at = c;
  55. spin_unlock(&c->lock);
  56. if (ret < 0) {
  57. *limit_fail_at = c;
  58. goto undo;
  59. }
  60. }
  61. ret = 0;
  62. goto done;
  63. undo:
  64. for (u = counter; u != c; u = u->parent) {
  65. spin_lock(&u->lock);
  66. res_counter_uncharge_locked(u, val);
  67. spin_unlock(&u->lock);
  68. }
  69. done:
  70. local_irq_restore(flags);
  71. return ret;
  72. }
  73. void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val)
  74. {
  75. if (WARN_ON(counter->usage < val))
  76. val = counter->usage;
  77. counter->usage -= val;
  78. }
  79. void res_counter_uncharge(struct res_counter *counter, unsigned long val,
  80. bool *was_soft_limit_excess)
  81. {
  82. unsigned long flags;
  83. struct res_counter *c;
  84. local_irq_save(flags);
  85. for (c = counter; c != NULL; c = c->parent) {
  86. spin_lock(&c->lock);
  87. if (was_soft_limit_excess)
  88. *was_soft_limit_excess =
  89. !res_counter_soft_limit_check_locked(c);
  90. res_counter_uncharge_locked(c, val);
  91. spin_unlock(&c->lock);
  92. }
  93. local_irq_restore(flags);
  94. }
  95. static inline unsigned long long *
  96. res_counter_member(struct res_counter *counter, int member)
  97. {
  98. switch (member) {
  99. case RES_USAGE:
  100. return &counter->usage;
  101. case RES_MAX_USAGE:
  102. return &counter->max_usage;
  103. case RES_LIMIT:
  104. return &counter->limit;
  105. case RES_FAILCNT:
  106. return &counter->failcnt;
  107. case RES_SOFT_LIMIT:
  108. return &counter->soft_limit;
  109. };
  110. BUG();
  111. return NULL;
  112. }
  113. ssize_t res_counter_read(struct res_counter *counter, int member,
  114. const char __user *userbuf, size_t nbytes, loff_t *pos,
  115. int (*read_strategy)(unsigned long long val, char *st_buf))
  116. {
  117. unsigned long long *val;
  118. char buf[64], *s;
  119. s = buf;
  120. val = res_counter_member(counter, member);
  121. if (read_strategy)
  122. s += read_strategy(*val, s);
  123. else
  124. s += sprintf(s, "%llu\n", *val);
  125. return simple_read_from_buffer((void __user *)userbuf, nbytes,
  126. pos, buf, s - buf);
  127. }
  128. u64 res_counter_read_u64(struct res_counter *counter, int member)
  129. {
  130. return *res_counter_member(counter, member);
  131. }
  132. int res_counter_memparse_write_strategy(const char *buf,
  133. unsigned long long *res)
  134. {
  135. char *end;
  136. /* return RESOURCE_MAX(unlimited) if "-1" is specified */
  137. if (*buf == '-') {
  138. *res = simple_strtoull(buf + 1, &end, 10);
  139. if (*res != 1 || *end != '\0')
  140. return -EINVAL;
  141. *res = RESOURCE_MAX;
  142. return 0;
  143. }
  144. /* FIXME - make memparse() take const char* args */
  145. *res = memparse((char *)buf, &end);
  146. if (*end != '\0')
  147. return -EINVAL;
  148. *res = PAGE_ALIGN(*res);
  149. return 0;
  150. }
  151. int res_counter_write(struct res_counter *counter, int member,
  152. const char *buf, write_strategy_fn write_strategy)
  153. {
  154. char *end;
  155. unsigned long flags;
  156. unsigned long long tmp, *val;
  157. if (write_strategy) {
  158. if (write_strategy(buf, &tmp))
  159. return -EINVAL;
  160. } else {
  161. tmp = simple_strtoull(buf, &end, 10);
  162. if (*end != '\0')
  163. return -EINVAL;
  164. }
  165. spin_lock_irqsave(&counter->lock, flags);
  166. val = res_counter_member(counter, member);
  167. *val = tmp;
  168. spin_unlock_irqrestore(&counter->lock, flags);
  169. return 0;
  170. }