Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

sched: Factor out cgroup weight conversion functions

Factor out sched_weight_from/to_cgroup() which convert between scheduler
shares and cgroup weight. No functional change. The factored out functions
will be used by a new BPF extensible sched_class so that the weights can be
exposed to the BPF programs in a way which is consistent cgroup weights and
easier to interpret.

The weight conversions will be used regardless of cgroup usage. It's just
borrowing the cgroup weight range as it's more intuitive.
CGROUP_WEIGHT_MIN/DFL/MAX constants are moved outside CONFIG_CGROUPS so that
the conversion helpers can always be defined.

v2: The helpers are now defined regardless of COFNIG_CGROUPS.

Signed-off-by: Tejun Heo <tj@kernel.org>
Reviewed-by: David Vernet <dvernet@meta.com>
Acked-by: Josh Don <joshdon@google.com>
Acked-by: Hao Luo <haoluo@google.com>
Acked-by: Barret Rhoden <brho@google.com>

+33 -17
+2 -2
include/linux/cgroup.h
··· 29 29 30 30 struct kernel_clone_args; 31 31 32 - #ifdef CONFIG_CGROUPS 33 - 34 32 /* 35 33 * All weight knobs on the default hierarchy should use the following min, 36 34 * default and max values. The default value is the logarithmic center of ··· 37 39 #define CGROUP_WEIGHT_MIN 1 38 40 #define CGROUP_WEIGHT_DFL 100 39 41 #define CGROUP_WEIGHT_MAX 10000 42 + 43 + #ifdef CONFIG_CGROUPS 40 44 41 45 enum { 42 46 CSS_TASK_ITER_PROCS = (1U << 0), /* walk only threadgroup leaders */
+13 -15
kernel/sched/core.c
··· 9552 9552 } 9553 9553 9554 9554 #ifdef CONFIG_FAIR_GROUP_SCHED 9555 + 9556 + static unsigned long tg_weight(struct task_group *tg) 9557 + { 9558 + return scale_load_down(tg->shares); 9559 + } 9560 + 9555 9561 static u64 cpu_weight_read_u64(struct cgroup_subsys_state *css, 9556 9562 struct cftype *cft) 9557 9563 { 9558 - struct task_group *tg = css_tg(css); 9559 - u64 weight = scale_load_down(tg->shares); 9560 - 9561 - return DIV_ROUND_CLOSEST_ULL(weight * CGROUP_WEIGHT_DFL, 1024); 9564 + return sched_weight_to_cgroup(tg_weight(css_tg(css))); 9562 9565 } 9563 9566 9564 9567 static int cpu_weight_write_u64(struct cgroup_subsys_state *css, 9565 - struct cftype *cft, u64 weight) 9568 + struct cftype *cft, u64 cgrp_weight) 9566 9569 { 9567 - /* 9568 - * cgroup weight knobs should use the common MIN, DFL and MAX 9569 - * values which are 1, 100 and 10000 respectively. While it loses 9570 - * a bit of range on both ends, it maps pretty well onto the shares 9571 - * value used by scheduler and the round-trip conversions preserve 9572 - * the original value over the entire range. 9573 - */ 9574 - if (weight < CGROUP_WEIGHT_MIN || weight > CGROUP_WEIGHT_MAX) 9570 + unsigned long weight; 9571 + 9572 + if (cgrp_weight < CGROUP_WEIGHT_MIN || cgrp_weight > CGROUP_WEIGHT_MAX) 9575 9573 return -ERANGE; 9576 9574 9577 - weight = DIV_ROUND_CLOSEST_ULL(weight * 1024, CGROUP_WEIGHT_DFL); 9575 + weight = sched_weight_from_cgroup(cgrp_weight); 9578 9576 9579 9577 return sched_group_set_shares(css_tg(css), scale_load(weight)); 9580 9578 } ··· 9580 9582 static s64 cpu_weight_nice_read_s64(struct cgroup_subsys_state *css, 9581 9583 struct cftype *cft) 9582 9584 { 9583 - unsigned long weight = scale_load_down(css_tg(css)->shares); 9585 + unsigned long weight = tg_weight(css_tg(css)); 9584 9586 int last_delta = INT_MAX; 9585 9587 int prio, delta; 9586 9588
+18
kernel/sched/sched.h
··· 245 245 (val >> min_t(typeof(shift), shift, BITS_PER_TYPE(typeof(val)) - 1)) 246 246 247 247 /* 248 + * cgroup weight knobs should use the common MIN, DFL and MAX values which are 249 + * 1, 100 and 10000 respectively. While it loses a bit of range on both ends, it 250 + * maps pretty well onto the shares value used by scheduler and the round-trip 251 + * conversions preserve the original value over the entire range. 252 + */ 253 + static inline unsigned long sched_weight_from_cgroup(unsigned long cgrp_weight) 254 + { 255 + return DIV_ROUND_CLOSEST_ULL(cgrp_weight * 1024, CGROUP_WEIGHT_DFL); 256 + } 257 + 258 + static inline unsigned long sched_weight_to_cgroup(unsigned long weight) 259 + { 260 + return clamp_t(unsigned long, 261 + DIV_ROUND_CLOSEST_ULL(weight * CGROUP_WEIGHT_DFL, 1024), 262 + CGROUP_WEIGHT_MIN, CGROUP_WEIGHT_MAX); 263 + } 264 + 265 + /* 248 266 * !! For sched_setattr_nocheck() (kernel) only !! 249 267 * 250 268 * This is actually gross. :(