// SPDX-License-Identifier: GPL-2.0 /* * Miscellaneous cgroup controller * * Copyright 2020 Google LLC * Author: Vipin Sharma <[email protected]> */ #include <linux/limits.h> #include <linux/cgroup.h> #include <linux/errno.h> #include <linux/atomic.h> #include <linux/slab.h> #include <linux/misc_cgroup.h> #define MAX_STR … #define MAX_NUM … /* Miscellaneous res name, keep it in sync with enum misc_res_type */ static const char *const misc_res_name[] = …; /* Root misc cgroup */ static struct misc_cg root_cg; /* * Miscellaneous resources capacity for the entire machine. 0 capacity means * resource is not initialized or not present in the host. * * root_cg.max and capacity are independent of each other. root_cg.max can be * more than the actual capacity. We are using Limits resource distribution * model of cgroup for miscellaneous controller. */ static u64 misc_res_capacity[MISC_CG_RES_TYPES]; /** * parent_misc() - Get the parent of the passed misc cgroup. * @cgroup: cgroup whose parent needs to be fetched. * * Context: Any context. * Return: * * struct misc_cg* - Parent of the @cgroup. * * %NULL - If @cgroup is null or the passed cgroup does not have a parent. */ static struct misc_cg *parent_misc(struct misc_cg *cgroup) { … } /** * valid_type() - Check if @type is valid or not. * @type: misc res type. * * Context: Any context. * Return: * * true - If valid type. * * false - If not valid type. */ static inline bool valid_type(enum misc_res_type type) { … } /** * misc_cg_res_total_usage() - Get the current total usage of the resource. * @type: misc res type. * * Context: Any context. * Return: Current total usage of the resource. */ u64 misc_cg_res_total_usage(enum misc_res_type type) { … } EXPORT_SYMBOL_GPL(…); /** * misc_cg_set_capacity() - Set the capacity of the misc cgroup res. * @type: Type of the misc res. * @capacity: Supported capacity of the misc res on the host. * * If capacity is 0 then the charging a misc cgroup fails for that type. * * Context: Any context. * Return: * * %0 - Successfully registered the capacity. * * %-EINVAL - If @type is invalid. */ int misc_cg_set_capacity(enum misc_res_type type, u64 capacity) { … } EXPORT_SYMBOL_GPL(…); /** * misc_cg_cancel_charge() - Cancel the charge from the misc cgroup. * @type: Misc res type in misc cg to cancel the charge from. * @cg: Misc cgroup to cancel charge from. * @amount: Amount to cancel. * * Context: Any context. */ static void misc_cg_cancel_charge(enum misc_res_type type, struct misc_cg *cg, u64 amount) { … } static void misc_cg_update_watermark(struct misc_res *res, u64 new_usage) { … } static void misc_cg_event(enum misc_res_type type, struct misc_cg *cg) { … } /** * misc_cg_try_charge() - Try charging the misc cgroup. * @type: Misc res type to charge. * @cg: Misc cgroup which will be charged. * @amount: Amount to charge. * * Charge @amount to the misc cgroup. Caller must use the same cgroup during * the uncharge call. * * Context: Any context. * Return: * * %0 - If successfully charged. * * -EINVAL - If @type is invalid or misc res has 0 capacity. * * -EBUSY - If max limit will be crossed or total usage will be more than the * capacity. */ int misc_cg_try_charge(enum misc_res_type type, struct misc_cg *cg, u64 amount) { … } EXPORT_SYMBOL_GPL(…); /** * misc_cg_uncharge() - Uncharge the misc cgroup. * @type: Misc res type which was charged. * @cg: Misc cgroup which will be uncharged. * @amount: Charged amount. * * Context: Any context. */ void misc_cg_uncharge(enum misc_res_type type, struct misc_cg *cg, u64 amount) { … } EXPORT_SYMBOL_GPL(…); /** * misc_cg_max_show() - Show the misc cgroup max limit. * @sf: Interface file * @v: Arguments passed * * Context: Any context. * Return: 0 to denote successful print. */ static int misc_cg_max_show(struct seq_file *sf, void *v) { … } /** * misc_cg_max_write() - Update the maximum limit of the cgroup. * @of: Handler for the file. * @buf: Data from the user. It should be either "max", 0, or a positive * integer. * @nbytes: Number of bytes of the data. * @off: Offset in the file. * * User can pass data like: * echo sev 23 > misc.max, OR * echo sev max > misc.max * * Context: Any context. * Return: * * >= 0 - Number of bytes processed in the input. * * -EINVAL - If buf is not valid. * * -ERANGE - If number is bigger than the u64 capacity. */ static ssize_t misc_cg_max_write(struct kernfs_open_file *of, char *buf, size_t nbytes, loff_t off) { … } /** * misc_cg_current_show() - Show the current usage of the misc cgroup. * @sf: Interface file * @v: Arguments passed * * Context: Any context. * Return: 0 to denote successful print. */ static int misc_cg_current_show(struct seq_file *sf, void *v) { … } /** * misc_cg_peak_show() - Show the peak usage of the misc cgroup. * @sf: Interface file * @v: Arguments passed * * Context: Any context. * Return: 0 to denote successful print. */ static int misc_cg_peak_show(struct seq_file *sf, void *v) { … } /** * misc_cg_capacity_show() - Show the total capacity of misc res on the host. * @sf: Interface file * @v: Arguments passed * * Only present in the root cgroup directory. * * Context: Any context. * Return: 0 to denote successful print. */ static int misc_cg_capacity_show(struct seq_file *sf, void *v) { … } static int __misc_events_show(struct seq_file *sf, bool local) { … } static int misc_events_show(struct seq_file *sf, void *v) { … } static int misc_events_local_show(struct seq_file *sf, void *v) { … } /* Misc cgroup interface files */ static struct cftype misc_cg_files[] = …; /** * misc_cg_alloc() - Allocate misc cgroup. * @parent_css: Parent cgroup. * * Context: Process context. * Return: * * struct cgroup_subsys_state* - css of the allocated cgroup. * * ERR_PTR(-ENOMEM) - No memory available to allocate. */ static struct cgroup_subsys_state * misc_cg_alloc(struct cgroup_subsys_state *parent_css) { … } /** * misc_cg_free() - Free the misc cgroup. * @css: cgroup subsys object. * * Context: Any context. */ static void misc_cg_free(struct cgroup_subsys_state *css) { … } /* Cgroup controller callbacks */ struct cgroup_subsys misc_cgrp_subsys = …;