misc_cgroup.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Miscellaneous cgroup controller.
  4. *
  5. * Copyright 2020 Google LLC
  6. * Author: Vipin Sharma <[email protected]>
  7. */
  8. #ifndef _MISC_CGROUP_H_
  9. #define _MISC_CGROUP_H_
  10. /**
  11. * Types of misc cgroup entries supported by the host.
  12. */
  13. enum misc_res_type {
  14. #ifdef CONFIG_KVM_AMD_SEV
  15. /* AMD SEV ASIDs resource */
  16. MISC_CG_RES_SEV,
  17. /* AMD SEV-ES ASIDs resource */
  18. MISC_CG_RES_SEV_ES,
  19. #endif
  20. MISC_CG_RES_TYPES
  21. };
  22. struct misc_cg;
  23. #ifdef CONFIG_CGROUP_MISC
  24. #include <linux/cgroup.h>
  25. /**
  26. * struct misc_res: Per cgroup per misc type resource
  27. * @max: Maximum limit on the resource.
  28. * @usage: Current usage of the resource.
  29. * @failed: True if charged failed for the resource in a cgroup.
  30. */
  31. struct misc_res {
  32. unsigned long max;
  33. atomic_long_t usage;
  34. atomic_long_t events;
  35. };
  36. /**
  37. * struct misc_cg - Miscellaneous controller's cgroup structure.
  38. * @css: cgroup subsys state object.
  39. * @res: Array of misc resources usage in the cgroup.
  40. */
  41. struct misc_cg {
  42. struct cgroup_subsys_state css;
  43. /* misc.events */
  44. struct cgroup_file events_file;
  45. struct misc_res res[MISC_CG_RES_TYPES];
  46. };
  47. unsigned long misc_cg_res_total_usage(enum misc_res_type type);
  48. int misc_cg_set_capacity(enum misc_res_type type, unsigned long capacity);
  49. int misc_cg_try_charge(enum misc_res_type type, struct misc_cg *cg,
  50. unsigned long amount);
  51. void misc_cg_uncharge(enum misc_res_type type, struct misc_cg *cg,
  52. unsigned long amount);
  53. /**
  54. * css_misc() - Get misc cgroup from the css.
  55. * @css: cgroup subsys state object.
  56. *
  57. * Context: Any context.
  58. * Return:
  59. * * %NULL - If @css is null.
  60. * * struct misc_cg* - misc cgroup pointer of the passed css.
  61. */
  62. static inline struct misc_cg *css_misc(struct cgroup_subsys_state *css)
  63. {
  64. return css ? container_of(css, struct misc_cg, css) : NULL;
  65. }
  66. /*
  67. * get_current_misc_cg() - Find and get the misc cgroup of the current task.
  68. *
  69. * Returned cgroup has its ref count increased by 1. Caller must call
  70. * put_misc_cg() to return the reference.
  71. *
  72. * Return: Misc cgroup to which the current task belongs to.
  73. */
  74. static inline struct misc_cg *get_current_misc_cg(void)
  75. {
  76. return css_misc(task_get_css(current, misc_cgrp_id));
  77. }
  78. /*
  79. * put_misc_cg() - Put the misc cgroup and reduce its ref count.
  80. * @cg - cgroup to put.
  81. */
  82. static inline void put_misc_cg(struct misc_cg *cg)
  83. {
  84. if (cg)
  85. css_put(&cg->css);
  86. }
  87. #else /* !CONFIG_CGROUP_MISC */
  88. static inline unsigned long misc_cg_res_total_usage(enum misc_res_type type)
  89. {
  90. return 0;
  91. }
  92. static inline int misc_cg_set_capacity(enum misc_res_type type,
  93. unsigned long capacity)
  94. {
  95. return 0;
  96. }
  97. static inline int misc_cg_try_charge(enum misc_res_type type,
  98. struct misc_cg *cg,
  99. unsigned long amount)
  100. {
  101. return 0;
  102. }
  103. static inline void misc_cg_uncharge(enum misc_res_type type,
  104. struct misc_cg *cg,
  105. unsigned long amount)
  106. {
  107. }
  108. static inline struct misc_cg *get_current_misc_cg(void)
  109. {
  110. return NULL;
  111. }
  112. static inline void put_misc_cg(struct misc_cg *cg)
  113. {
  114. }
  115. #endif /* CONFIG_CGROUP_MISC */
  116. #endif /* _MISC_CGROUP_H_ */