res_counter.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #ifndef __RES_COUNTER_H__
  2. #define __RES_COUNTER_H__
  3. /*
  4. * Resource Counters
  5. * Contain common data types and routines for resource accounting
  6. *
  7. * Copyright 2007 OpenVZ SWsoft Inc
  8. *
  9. * Author: Pavel Emelianov <xemul@openvz.org>
  10. *
  11. * See Documentation/controllers/resource_counter.txt for more
  12. * info about what this counter is.
  13. */
  14. #include <linux/cgroup.h>
  15. /*
  16. * The core object. the cgroup that wishes to account for some
  17. * resource may include this counter into its structures and use
  18. * the helpers described beyond
  19. */
  20. struct res_counter {
  21. /*
  22. * the current resource consumption level
  23. */
  24. unsigned long long usage;
  25. /*
  26. * the maximal value of the usage from the counter creation
  27. */
  28. unsigned long long max_usage;
  29. /*
  30. * the limit that usage cannot exceed
  31. */
  32. unsigned long long limit;
  33. /*
  34. * the number of unsuccessful attempts to consume the resource
  35. */
  36. unsigned long long failcnt;
  37. /*
  38. * the lock to protect all of the above.
  39. * the routines below consider this to be IRQ-safe
  40. */
  41. spinlock_t lock;
  42. /*
  43. * Parent counter, used for hierarchial resource accounting
  44. */
  45. struct res_counter *parent;
  46. };
  47. /**
  48. * Helpers to interact with userspace
  49. * res_counter_read_u64() - returns the value of the specified member.
  50. * res_counter_read/_write - put/get the specified fields from the
  51. * res_counter struct to/from the user
  52. *
  53. * @counter: the counter in question
  54. * @member: the field to work with (see RES_xxx below)
  55. * @buf: the buffer to opeate on,...
  56. * @nbytes: its size...
  57. * @pos: and the offset.
  58. */
  59. u64 res_counter_read_u64(struct res_counter *counter, int member);
  60. ssize_t res_counter_read(struct res_counter *counter, int member,
  61. const char __user *buf, size_t nbytes, loff_t *pos,
  62. int (*read_strategy)(unsigned long long val, char *s));
  63. typedef int (*write_strategy_fn)(const char *buf, unsigned long long *val);
  64. int res_counter_memparse_write_strategy(const char *buf,
  65. unsigned long long *res);
  66. int res_counter_write(struct res_counter *counter, int member,
  67. const char *buffer, write_strategy_fn write_strategy);
  68. /*
  69. * the field descriptors. one for each member of res_counter
  70. */
  71. enum {
  72. RES_USAGE,
  73. RES_MAX_USAGE,
  74. RES_LIMIT,
  75. RES_FAILCNT,
  76. };
  77. /*
  78. * helpers for accounting
  79. */
  80. void res_counter_init(struct res_counter *counter, struct res_counter *parent);
  81. /*
  82. * charge - try to consume more resource.
  83. *
  84. * @counter: the counter
  85. * @val: the amount of the resource. each controller defines its own
  86. * units, e.g. numbers, bytes, Kbytes, etc
  87. *
  88. * returns 0 on success and <0 if the counter->usage will exceed the
  89. * counter->limit _locked call expects the counter->lock to be taken
  90. */
  91. int __must_check res_counter_charge_locked(struct res_counter *counter,
  92. unsigned long val);
  93. int __must_check res_counter_charge(struct res_counter *counter,
  94. unsigned long val, struct res_counter **limit_fail_at);
  95. /*
  96. * uncharge - tell that some portion of the resource is released
  97. *
  98. * @counter: the counter
  99. * @val: the amount of the resource
  100. *
  101. * these calls check for usage underflow and show a warning on the console
  102. * _locked call expects the counter->lock to be taken
  103. */
  104. void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val);
  105. void res_counter_uncharge(struct res_counter *counter, unsigned long val);
  106. static inline bool res_counter_limit_check_locked(struct res_counter *cnt)
  107. {
  108. if (cnt->usage < cnt->limit)
  109. return true;
  110. return false;
  111. }
  112. /*
  113. * Helper function to detect if the cgroup is within it's limit or
  114. * not. It's currently called from cgroup_rss_prepare()
  115. */
  116. static inline bool res_counter_check_under_limit(struct res_counter *cnt)
  117. {
  118. bool ret;
  119. unsigned long flags;
  120. spin_lock_irqsave(&cnt->lock, flags);
  121. ret = res_counter_limit_check_locked(cnt);
  122. spin_unlock_irqrestore(&cnt->lock, flags);
  123. return ret;
  124. }
  125. static inline void res_counter_reset_max(struct res_counter *cnt)
  126. {
  127. unsigned long flags;
  128. spin_lock_irqsave(&cnt->lock, flags);
  129. cnt->max_usage = cnt->usage;
  130. spin_unlock_irqrestore(&cnt->lock, flags);
  131. }
  132. static inline void res_counter_reset_failcnt(struct res_counter *cnt)
  133. {
  134. unsigned long flags;
  135. spin_lock_irqsave(&cnt->lock, flags);
  136. cnt->failcnt = 0;
  137. spin_unlock_irqrestore(&cnt->lock, flags);
  138. }
  139. static inline int res_counter_set_limit(struct res_counter *cnt,
  140. unsigned long long limit)
  141. {
  142. unsigned long flags;
  143. int ret = -EBUSY;
  144. spin_lock_irqsave(&cnt->lock, flags);
  145. if (cnt->usage <= limit) {
  146. cnt->limit = limit;
  147. ret = 0;
  148. }
  149. spin_unlock_irqrestore(&cnt->lock, flags);
  150. return ret;
  151. }
  152. #endif