Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * Miscellaneous cgroup controller.
0004  *
0005  * Copyright 2020 Google LLC
0006  * Author: Vipin Sharma <vipinsh@google.com>
0007  */
0008 #ifndef _MISC_CGROUP_H_
0009 #define _MISC_CGROUP_H_
0010 
0011 /**
0012  * Types of misc cgroup entries supported by the host.
0013  */
0014 enum misc_res_type {
0015 #ifdef CONFIG_KVM_AMD_SEV
0016     /* AMD SEV ASIDs resource */
0017     MISC_CG_RES_SEV,
0018     /* AMD SEV-ES ASIDs resource */
0019     MISC_CG_RES_SEV_ES,
0020 #endif
0021     MISC_CG_RES_TYPES
0022 };
0023 
0024 struct misc_cg;
0025 
0026 #ifdef CONFIG_CGROUP_MISC
0027 
0028 #include <linux/cgroup.h>
0029 
0030 /**
0031  * struct misc_res: Per cgroup per misc type resource
0032  * @max: Maximum limit on the resource.
0033  * @usage: Current usage of the resource.
0034  * @failed: True if charged failed for the resource in a cgroup.
0035  */
0036 struct misc_res {
0037     unsigned long max;
0038     atomic_long_t usage;
0039     atomic_long_t events;
0040 };
0041 
0042 /**
0043  * struct misc_cg - Miscellaneous controller's cgroup structure.
0044  * @css: cgroup subsys state object.
0045  * @res: Array of misc resources usage in the cgroup.
0046  */
0047 struct misc_cg {
0048     struct cgroup_subsys_state css;
0049 
0050     /* misc.events */
0051     struct cgroup_file events_file;
0052 
0053     struct misc_res res[MISC_CG_RES_TYPES];
0054 };
0055 
0056 unsigned long misc_cg_res_total_usage(enum misc_res_type type);
0057 int misc_cg_set_capacity(enum misc_res_type type, unsigned long capacity);
0058 int misc_cg_try_charge(enum misc_res_type type, struct misc_cg *cg,
0059                unsigned long amount);
0060 void misc_cg_uncharge(enum misc_res_type type, struct misc_cg *cg,
0061               unsigned long amount);
0062 
0063 /**
0064  * css_misc() - Get misc cgroup from the css.
0065  * @css: cgroup subsys state object.
0066  *
0067  * Context: Any context.
0068  * Return:
0069  * * %NULL - If @css is null.
0070  * * struct misc_cg* - misc cgroup pointer of the passed css.
0071  */
0072 static inline struct misc_cg *css_misc(struct cgroup_subsys_state *css)
0073 {
0074     return css ? container_of(css, struct misc_cg, css) : NULL;
0075 }
0076 
0077 /*
0078  * get_current_misc_cg() - Find and get the misc cgroup of the current task.
0079  *
0080  * Returned cgroup has its ref count increased by 1. Caller must call
0081  * put_misc_cg() to return the reference.
0082  *
0083  * Return: Misc cgroup to which the current task belongs to.
0084  */
0085 static inline struct misc_cg *get_current_misc_cg(void)
0086 {
0087     return css_misc(task_get_css(current, misc_cgrp_id));
0088 }
0089 
0090 /*
0091  * put_misc_cg() - Put the misc cgroup and reduce its ref count.
0092  * @cg - cgroup to put.
0093  */
0094 static inline void put_misc_cg(struct misc_cg *cg)
0095 {
0096     if (cg)
0097         css_put(&cg->css);
0098 }
0099 
0100 #else /* !CONFIG_CGROUP_MISC */
0101 
0102 static inline unsigned long misc_cg_res_total_usage(enum misc_res_type type)
0103 {
0104     return 0;
0105 }
0106 
0107 static inline int misc_cg_set_capacity(enum misc_res_type type,
0108                        unsigned long capacity)
0109 {
0110     return 0;
0111 }
0112 
0113 static inline int misc_cg_try_charge(enum misc_res_type type,
0114                      struct misc_cg *cg,
0115                      unsigned long amount)
0116 {
0117     return 0;
0118 }
0119 
0120 static inline void misc_cg_uncharge(enum misc_res_type type,
0121                     struct misc_cg *cg,
0122                     unsigned long amount)
0123 {
0124 }
0125 
0126 static inline struct misc_cg *get_current_misc_cg(void)
0127 {
0128     return NULL;
0129 }
0130 
0131 static inline void put_misc_cg(struct misc_cg *cg)
0132 {
0133 }
0134 
0135 #endif /* CONFIG_CGROUP_MISC */
0136 #endif /* _MISC_CGROUP_H_ */