res_counter.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. {
  37. int ret;
  38. unsigned long flags;
  39. struct res_counter *c, *u;
  40. *limit_fail_at = NULL;
  41. local_irq_save(flags);
  42. for (c = counter; c != NULL; c = c->parent) {
  43. spin_lock(&c->lock);
  44. ret = res_counter_charge_locked(c, val);
  45. spin_unlock(&c->lock);
  46. if (ret < 0) {
  47. *limit_fail_at = c;
  48. goto undo;
  49. }
  50. }
  51. ret = 0;
  52. goto done;
  53. undo:
  54. for (u = counter; u != c; u = u->parent) {
  55. spin_lock(&u->lock);
  56. res_counter_uncharge_locked(u, val);
  57. spin_unlock(&u->lock);
  58. }
  59. done:
  60. local_irq_restore(flags);
  61. return ret;
  62. }
  63. void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val)
  64. {
  65. if (WARN_ON(counter->usage < val))
  66. val = counter->usage;
  67. counter->usage -= val;
  68. }
  69. void res_counter_uncharge(struct res_counter *counter, unsigned long val)
  70. {
  71. unsigned long flags;
  72. struct res_counter *c;
  73. local_irq_save(flags);
  74. for (c = counter; c != NULL; c = c->parent) {
  75. spin_lock(&c->lock);
  76. res_counter_uncharge_locked(c, val);
  77. spin_unlock(&c->lock);
  78. }
  79. local_irq_restore(flags);
  80. }
  81. static inline unsigned long long *
  82. res_counter_member(struct res_counter *counter, int member)
  83. {
  84. switch (member) {
  85. case RES_USAGE:
  86. return &counter->usage;
  87. case RES_MAX_USAGE:
  88. return &counter->max_usage;
  89. case RES_LIMIT:
  90. return &counter->limit;
  91. case RES_FAILCNT:
  92. return &counter->failcnt;
  93. case RES_SOFT_LIMIT:
  94. return &counter->soft_limit;
  95. };
  96. BUG();
  97. return NULL;
  98. }
  99. ssize_t res_counter_read(struct res_counter *counter, int member,
  100. const char __user *userbuf, size_t nbytes, loff_t *pos,
  101. int (*read_strategy)(unsigned long long val, char *st_buf))
  102. {
  103. unsigned long long *val;
  104. char buf[64], *s;
  105. s = buf;
  106. val = res_counter_member(counter, member);
  107. if (read_strategy)
  108. s += read_strategy(*val, s);
  109. else
  110. s += sprintf(s, "%llu\n", *val);
  111. return simple_read_from_buffer((void __user *)userbuf, nbytes,
  112. pos, buf, s - buf);
  113. }
  114. u64 res_counter_read_u64(struct res_counter *counter, int member)
  115. {
  116. return *res_counter_member(counter, member);
  117. }
  118. int res_counter_memparse_write_strategy(const char *buf,
  119. unsigned long long *res)
  120. {
  121. char *end;
  122. /* return RESOURCE_MAX(unlimited) if "-1" is specified */
  123. if (*buf == '-') {
  124. *res = simple_strtoull(buf + 1, &end, 10);
  125. if (*res != 1 || *end != '\0')
  126. return -EINVAL;
  127. *res = RESOURCE_MAX;
  128. return 0;
  129. }
  130. /* FIXME - make memparse() take const char* args */
  131. *res = memparse((char *)buf, &end);
  132. if (*end != '\0')
  133. return -EINVAL;
  134. *res = PAGE_ALIGN(*res);
  135. return 0;
  136. }
  137. int res_counter_write(struct res_counter *counter, int member,
  138. const char *buf, write_strategy_fn write_strategy)
  139. {
  140. char *end;
  141. unsigned long flags;
  142. unsigned long long tmp, *val;
  143. if (write_strategy) {
  144. if (write_strategy(buf, &tmp))
  145. return -EINVAL;
  146. } else {
  147. tmp = simple_strtoull(buf, &end, 10);
  148. if (*end != '\0')
  149. return -EINVAL;
  150. }
  151. spin_lock_irqsave(&counter->lock, flags);
  152. val = res_counter_member(counter, member);
  153. *val = tmp;
  154. spin_unlock_irqrestore(&counter->lock, flags);
  155. return 0;
  156. }