Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp36127531rwd; Mon, 10 Jul 2023 18:40:27 -0700 (PDT) X-Google-Smtp-Source: APBJJlG7Yps6ZG0TaPGaCxAyOL4NMrU4bvzWOuIC3vfbD5v2fF9BZLuKLnxpjNTi1j8XgM2ehled X-Received: by 2002:a17:902:bf0b:b0:1b8:4f93:b210 with SMTP id bi11-20020a170902bf0b00b001b84f93b210mr12403603plb.45.1689039626925; Mon, 10 Jul 2023 18:40:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689039626; cv=none; d=google.com; s=arc-20160816; b=fVl7MNcMyLZ/shsfevqY616UrNj7JkrYgNvSjWYaLzVP+3tasOTZOyWdLglGBAJsKA HPgZCdK0hDo6IDk0S8bB/GhVkr2He8j3CWaHbAtkMIDZ/n8CJRmDtuOlUeWNlnG0aaRm EO8TWtbzDawxMGn04Xg7H4QVwreofSira6mrS+03J/WXnqOqGPkDoCCqsdUiZP7GlvFr Pgs2E5I39004cekjQSaMwjwgYuHItTbwD+z3pI9/58mLmfE2/IKjbCzgy7TjDKSg+lAX ChFhnnQ6oCJzxBMXDGlqo1J9hVzVlAAdxHJW4P0X9KlwQuJsJfLCZjeq1gQ14/xNDL/n exSA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from:sender :dkim-signature; bh=t4+VbIeEfVwr20uG8gTLNcC3Z+mkzkGxgxXAmQeKdtM=; fh=5bvFqXZy/VrzcWOXnXaaKVCt2r2P+2FQUI6EKkM71t8=; b=kyVZ89vk7etzh3jYIXYQyiyvKOKrCzXKg0I6AZxej4mWXfSmbr+cV7Hit/ExlUodNU HP4gWBN/ggDqibZ15GZB7+EvAA/cDsBBFj3ceAn8QY8HO4lZshN59cMDMseRQygSHvBt K9BxKKH529tVvWbYTKWbR72Dx1SxOaRUsbHy1G7vU+s1SFso/OmZK7BxZ2uV9YjDD9KT USU6ObIAoM7ZpstoRLxHGodI3/41S8wRCvM+zgGccLFfQVwCLYm5movs1kOe1w22gn1D TfIjCEf7aqsGRYXRItK8luybdA7MMOGjtnPHA75bWRZnEaBlMtIe6oGbSNJaqrYQtwLV s76g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=pkCKjnov; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b7-20020a170902d50700b001b89302ca7esi711077plg.574.2023.07.10.18.40.14; Mon, 10 Jul 2023 18:40:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=pkCKjnov; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231409AbjGKBOs (ORCPT + 99 others); Mon, 10 Jul 2023 21:14:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56352 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231272AbjGKBOk (ORCPT ); Mon, 10 Jul 2023 21:14:40 -0400 Received: from mail-pl1-x634.google.com (mail-pl1-x634.google.com [IPv6:2607:f8b0:4864:20::634]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C3CB4E64; Mon, 10 Jul 2023 18:14:28 -0700 (PDT) Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-1b89cfb4571so38557265ad.3; Mon, 10 Jul 2023 18:14:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1689038068; x=1691630068; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=t4+VbIeEfVwr20uG8gTLNcC3Z+mkzkGxgxXAmQeKdtM=; b=pkCKjnovq8uY4CapKGy+dh08FLGhT0DHbARTbykFluBi/Vv0wECWvvFhuIesG2fFcE r8XgIPCsaObjkVDolPtVo3wgMH7tMb9SvWP7uxAqP17DK9yXYd+XNXsB+0hEDJE3cNgI LFaR2ig/+dQmoZyx3wF16bhbtW4UYK/YO6KOzI4PBDeCHTEZlVFjXCFf12P2J8z1nAar wo4PP9jWfV8HatGz86mNiMxjMTHNtJggzl287/2Ue1GDsOXAGU3qlNB7yuinHL+ENrvg TmRe19bE/bfYKpul2FbBohoCFYLVt4Dr1kr/T5NutUJdp9aDqcgycU3pFV2yMwx+DSAf QhIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689038068; x=1691630068; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=t4+VbIeEfVwr20uG8gTLNcC3Z+mkzkGxgxXAmQeKdtM=; b=e+3ippNhwr+pMRCTRmUlWN3QHA2s4YiGD4yhAzMoEMFcuDs2q3up5/9C2hcXuQIZjz BuCOebq8b0o1ANxEHnvig59CQFmpKwufhd9ZSpAYkdAbokETqGLVo/D1ljflScYKXjHp X+sH0yb6acDA55eEslCmta+MHPT0afGfU3DZdpfzccqieGajMhG5KXwmIbfHGLL/IoqP qhpZflwlKwcbl4Vf2ZOgymPCy1RB0RqallXTDnI64z9BYSdtUXmKJLGKO9D/LKaCdwEe SkFvQc6lgr29iLVSWpG4fwMUPC9rG+1pBO4c59ijOUwJ+3GhNA8pcW4aVXWVTvF+dg62 yTQw== X-Gm-Message-State: ABy/qLaS3n8tGkXbqkJccJ/O4QVKGUxNSpEFeCZClH8Qnm5GbLqcLktf cXLbJgMEn5FBSDasXdtzG7k= X-Received: by 2002:a17:903:1d0:b0:1b9:d961:69b7 with SMTP id e16-20020a17090301d000b001b9d96169b7mr6732223plh.10.1689038068008; Mon, 10 Jul 2023 18:14:28 -0700 (PDT) Received: from localhost ([2620:10d:c090:400::5:e2fe]) by smtp.gmail.com with ESMTPSA id d12-20020a170902cb8c00b001b05e96d859sm510814ply.135.2023.07.10.18.14.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jul 2023 18:14:27 -0700 (PDT) Sender: Tejun Heo From: Tejun Heo To: torvalds@linux-foundation.org, mingo@redhat.com, peterz@infradead.org, juri.lelli@redhat.com, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de, bristot@redhat.com, vschneid@redhat.com, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@kernel.org, joshdon@google.com, brho@google.com, pjt@google.com, derkling@google.com, haoluo@google.com, dvernet@meta.com, dschatzberg@meta.com, dskarlat@cs.cmu.edu, riel@surriel.com Cc: linux-kernel@vger.kernel.org, bpf@vger.kernel.org, kernel-team@meta.com, Tejun Heo Subject: [PATCH 06/34] sched: Factor out cgroup weight conversion functions Date: Mon, 10 Jul 2023 15:13:24 -1000 Message-ID: <20230711011412.100319-7-tj@kernel.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230711011412.100319-1-tj@kernel.org> References: <20230711011412.100319-1-tj@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.5 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 Reviewed-by: David Vernet Acked-by: Josh Don Acked-by: Hao Luo Acked-by: Barret Rhoden --- include/linux/cgroup.h | 4 ++-- kernel/sched/core.c | 28 +++++++++++++--------------- kernel/sched/sched.h | 18 ++++++++++++++++++ 3 files changed, 33 insertions(+), 17 deletions(-) diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h index 5d1e8432d9af..96d81dfb143f 100644 --- a/include/linux/cgroup.h +++ b/include/linux/cgroup.h @@ -29,8 +29,6 @@ struct kernel_clone_args; -#ifdef CONFIG_CGROUPS - /* * All weight knobs on the default hierarchy should use the following min, * default and max values. The default value is the logarithmic center of @@ -40,6 +38,8 @@ struct kernel_clone_args; #define CGROUP_WEIGHT_DFL 100 #define CGROUP_WEIGHT_MAX 10000 +#ifdef CONFIG_CGROUPS + /* walk only threadgroup leaders */ #define CSS_TASK_ITER_PROCS (1U << 0) /* walk all threaded css_sets in the domain */ diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 29f75a31b414..00d6751b0f8d 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -11255,29 +11255,27 @@ static int cpu_extra_stat_show(struct seq_file *sf, } #ifdef CONFIG_FAIR_GROUP_SCHED + +static unsigned long tg_weight(struct task_group *tg) +{ + return scale_load_down(tg->shares); +} + static u64 cpu_weight_read_u64(struct cgroup_subsys_state *css, struct cftype *cft) { - struct task_group *tg = css_tg(css); - u64 weight = scale_load_down(tg->shares); - - return DIV_ROUND_CLOSEST_ULL(weight * CGROUP_WEIGHT_DFL, 1024); + return sched_weight_to_cgroup(tg_weight(css_tg(css))); } static int cpu_weight_write_u64(struct cgroup_subsys_state *css, - struct cftype *cft, u64 weight) + struct cftype *cft, u64 cgrp_weight) { - /* - * cgroup weight knobs should use the common MIN, DFL and MAX - * values which are 1, 100 and 10000 respectively. While it loses - * a bit of range on both ends, it maps pretty well onto the shares - * value used by scheduler and the round-trip conversions preserve - * the original value over the entire range. - */ - if (weight < CGROUP_WEIGHT_MIN || weight > CGROUP_WEIGHT_MAX) + unsigned long weight; + + if (cgrp_weight < CGROUP_WEIGHT_MIN || cgrp_weight > CGROUP_WEIGHT_MAX) return -ERANGE; - weight = DIV_ROUND_CLOSEST_ULL(weight * 1024, CGROUP_WEIGHT_DFL); + weight = sched_weight_from_cgroup(cgrp_weight); return sched_group_set_shares(css_tg(css), scale_load(weight)); } @@ -11285,7 +11283,7 @@ static int cpu_weight_write_u64(struct cgroup_subsys_state *css, static s64 cpu_weight_nice_read_s64(struct cgroup_subsys_state *css, struct cftype *cft) { - unsigned long weight = scale_load_down(css_tg(css)->shares); + unsigned long weight = tg_weight(css_tg(css)); int last_delta = INT_MAX; int prio, delta; diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index 0a25bcd8d9d1..2eb5759f4be9 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -232,6 +232,24 @@ static inline void update_avg(u64 *avg, u64 sample) #define shr_bound(val, shift) \ (val >> min_t(typeof(shift), shift, BITS_PER_TYPE(typeof(val)) - 1)) +/* + * cgroup weight knobs should use the common MIN, DFL and MAX values which are + * 1, 100 and 10000 respectively. While it loses a bit of range on both ends, it + * maps pretty well onto the shares value used by scheduler and the round-trip + * conversions preserve the original value over the entire range. + */ +static inline unsigned long sched_weight_from_cgroup(unsigned long cgrp_weight) +{ + return DIV_ROUND_CLOSEST_ULL(cgrp_weight * 1024, CGROUP_WEIGHT_DFL); +} + +static inline unsigned long sched_weight_to_cgroup(unsigned long weight) +{ + return clamp_t(unsigned long, + DIV_ROUND_CLOSEST_ULL(weight * CGROUP_WEIGHT_DFL, 1024), + CGROUP_WEIGHT_MIN, CGROUP_WEIGHT_MAX); +} + /* * !! For sched_setattr_nocheck() (kernel) only !! * -- 2.41.0