res_counter.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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)
  17. {
  18. spin_lock_init(&counter->lock);
  19. counter->limit = (unsigned long long)LLONG_MAX;
  20. }
  21. int res_counter_charge_locked(struct res_counter *counter, unsigned long val)
  22. {
  23. if (counter->usage + val > counter->limit) {
  24. counter->failcnt++;
  25. return -ENOMEM;
  26. }
  27. counter->usage += val;
  28. if (counter->usage > counter->max_usage)
  29. counter->max_usage = counter->usage;
  30. return 0;
  31. }
  32. int res_counter_charge(struct res_counter *counter, unsigned long val)
  33. {
  34. int ret;
  35. unsigned long flags;
  36. spin_lock_irqsave(&counter->lock, flags);
  37. ret = res_counter_charge_locked(counter, val);
  38. spin_unlock_irqrestore(&counter->lock, flags);
  39. return ret;
  40. }
  41. void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val)
  42. {
  43. if (WARN_ON(counter->usage < val))
  44. val = counter->usage;
  45. counter->usage -= val;
  46. }
  47. void res_counter_uncharge(struct res_counter *counter, unsigned long val)
  48. {
  49. unsigned long flags;
  50. spin_lock_irqsave(&counter->lock, flags);
  51. res_counter_uncharge_locked(counter, val);
  52. spin_unlock_irqrestore(&counter->lock, flags);
  53. }
  54. static inline unsigned long long *
  55. res_counter_member(struct res_counter *counter, int member)
  56. {
  57. switch (member) {
  58. case RES_USAGE:
  59. return &counter->usage;
  60. case RES_MAX_USAGE:
  61. return &counter->max_usage;
  62. case RES_LIMIT:
  63. return &counter->limit;
  64. case RES_FAILCNT:
  65. return &counter->failcnt;
  66. };
  67. BUG();
  68. return NULL;
  69. }
  70. ssize_t res_counter_read(struct res_counter *counter, int member,
  71. const char __user *userbuf, size_t nbytes, loff_t *pos,
  72. int (*read_strategy)(unsigned long long val, char *st_buf))
  73. {
  74. unsigned long long *val;
  75. char buf[64], *s;
  76. s = buf;
  77. val = res_counter_member(counter, member);
  78. if (read_strategy)
  79. s += read_strategy(*val, s);
  80. else
  81. s += sprintf(s, "%llu\n", *val);
  82. return simple_read_from_buffer((void __user *)userbuf, nbytes,
  83. pos, buf, s - buf);
  84. }
  85. u64 res_counter_read_u64(struct res_counter *counter, int member)
  86. {
  87. return *res_counter_member(counter, member);
  88. }
  89. int res_counter_memparse_write_strategy(const char *buf,
  90. unsigned long long *res)
  91. {
  92. char *end;
  93. /* FIXME - make memparse() take const char* args */
  94. *res = memparse((char *)buf, &end);
  95. if (*end != '\0')
  96. return -EINVAL;
  97. *res = PAGE_ALIGN(*res);
  98. return 0;
  99. }
  100. int res_counter_write(struct res_counter *counter, int member,
  101. const char *buf, write_strategy_fn write_strategy)
  102. {
  103. char *end;
  104. unsigned long flags;
  105. unsigned long long tmp, *val;
  106. if (write_strategy) {
  107. if (write_strategy(buf, &tmp))
  108. return -EINVAL;
  109. } else {
  110. tmp = simple_strtoull(buf, &end, 10);
  111. if (*end != '\0')
  112. return -EINVAL;
  113. }
  114. spin_lock_irqsave(&counter->lock, flags);
  115. val = res_counter_member(counter, member);
  116. *val = tmp;
  117. spin_unlock_irqrestore(&counter->lock, flags);
  118. return 0;
  119. }