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