Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp431105rwi; Tue, 18 Oct 2022 20:34:14 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4lbnJkYbwhVm9vRLOYQY5NmrnUSjFnqzLkqTeKm41dy0wR+FES6swhTOkXmIbxkGMdqmXc X-Received: by 2002:a17:906:fe46:b0:730:ca2b:cb7b with SMTP id wz6-20020a170906fe4600b00730ca2bcb7bmr5094047ejb.703.1666150454019; Tue, 18 Oct 2022 20:34:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666150454; cv=none; d=google.com; s=arc-20160816; b=AYaBbEBAt8unGUikMpBLNMFZqlhIZQSVhK4w+fR8yVSHiiXdXrRQXpfg0KP211c+jX 7Rh64e7UnXaXm3uIvH3ftNGdAZLIAKcW5Sqs0YN2BBJHmzhUxACxOqXFSQGu8a14fuvl GN6TRd48hlB1wQW3WHIOVaKzNvhwd9Hlj+9sEZ3zYE5xm1sYuOTtEkTXsvrSXTI1vlRA ieZ9XXB+DJ24yjtrA8imq7pjN3n5g8dGhnJ9eiU0OSBvZR3fOGXkgrHP7vbA/z/aahhJ 1wSzUp4C83cnaUN8OuK82AqEs4E1A318UfM1JcLWfSW4ur4TxsFyAIbQpjstnJ5VIOOu wGow== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=83J0FWeQ22MhRLlQnaI+JzsDt7vTIzx9w1sgOTApdqk=; b=gbsMLveJTqLkvon74/s6ggdo2j8gQG+Id918DZZKjByqiS8lhokz+KHpwx0Dt64ynj 8lLRt79791FOO7nmUUCMtNkazg6SADVB38VA6XtbF3ZC9fXNNcLi6Pz2Yx19F5eZOWLh 69iQckXYSubENp+rVZv+/SSpaezpNMgPa9p1hoeb7Vve+Ds+z9pQKfacTwXJRtm46Xgt /fFkxrJUerDr5/3RYeHvF3YvgfTcxWY+qxJY8wPcID7NwxBe/vXTZbkeK94udgLbiwpE uQ/E59PaI7ZDYlpU3wt5kYA3YVTG/m3SBn3b5hvNYAb+XU+uqolSYUDi53kQ6BXl2hUS yzlw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=1fwPqOJJ; 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=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id qw22-20020a1709066a1600b0073d91a38b35si13441805ejc.789.2022.10.18.20.33.47; Tue, 18 Oct 2022 20:34:13 -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=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=1fwPqOJJ; 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=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229773AbiJSDQQ (ORCPT + 99 others); Tue, 18 Oct 2022 23:16:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52076 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229484AbiJSDQN (ORCPT ); Tue, 18 Oct 2022 23:16:13 -0400 Received: from mail-pf1-x430.google.com (mail-pf1-x430.google.com [IPv6:2607:f8b0:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2FB31D259C for ; Tue, 18 Oct 2022 20:16:11 -0700 (PDT) Received: by mail-pf1-x430.google.com with SMTP id 3so15987535pfw.4 for ; Tue, 18 Oct 2022 20:16:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=83J0FWeQ22MhRLlQnaI+JzsDt7vTIzx9w1sgOTApdqk=; b=1fwPqOJJ2N6WhOI8lMbkbqGZPwTcxZEmQSEj4yn4JR09qobPUr715RcR1jNSC9EtBY t49JAd312c6EIDDGtwL+ANn/vL9LTubcnia7gIPmE809/e5J2v9MmL9yAwMXGOkh98Ip BElTm90OcMgZ/aECd2iSFsYOftV4ZKfGzYxDYUZexz9JJtZ6XSFyClV65p6iXKTuvbug ORjkBIR7FpnpCXfxdFBDNuWbNN+hCc5XQ2ytR2TQlKo6n4U7LnEt9NFxjfm/0DiqQ4ZZ 6LLkVYGokcldu400CAfoV5LdvpUFTaaQousEfu2CNvfrAp7/uLTDzdE8S8zN+rPyisp5 Oljg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=83J0FWeQ22MhRLlQnaI+JzsDt7vTIzx9w1sgOTApdqk=; b=kuwzutkw9BkmKoluXHYdTudHCtOM6220yh/7c5vKERsyixj6voOwxHbSSXr+41vYYu W/qnz0BrkasS6Uz3gdjufAirLLPEo2Zed7LNAsGOoZyXhDq3f5jIz+AMbPgypIfWCbpc 1IbVgWtqHlr17aVicaWfHB5JFjZ65AYydlwdjcF/91nKxIq/NwL9KEdzH73iQ2f+BjPe /7WzLM7sJajae7N4/wgmCOutFt2ckq1ZfwuIbbnh3HicrTy/LdCjp6ReRBJr5YSYgubt bmRjcFM9Jp1JAzuJUtihkA5bkxhswKfhj3NRSMT0a7gnFcP9KfwZ/4bOsUABX5PpfFok cjFw== X-Gm-Message-State: ACrzQf2CjmoFmpFINOEqQstiy4u6xgEpxZJviFMylW7c82zqA+EtcQ67 TdDBn8t5++kIa4KXdYXUv8jEyw== X-Received: by 2002:a63:6b09:0:b0:453:88a9:1d18 with SMTP id g9-20020a636b09000000b0045388a91d18mr5285511pgc.41.1666149370436; Tue, 18 Oct 2022 20:16:10 -0700 (PDT) Received: from YGFVJ29LDD.bytedance.net ([139.177.225.225]) by smtp.gmail.com with ESMTPSA id s5-20020a625e05000000b005631f2b9ba2sm10284984pfb.14.2022.10.18.20.16.05 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 18 Oct 2022 20:16:09 -0700 (PDT) From: Chuyi Zhou To: mingo@redhat.com, peterz@infradead.org, juri.lelli@redhat.com, vincent.guittot@linaro.org Cc: linux-kernel@vger.kernel.org, htejun@gmail.com, lizefan.x@bytedance.com, vschneid@redhat.com, bsegall@google.com, Chuyi Zhou , Abel Wu Subject: [RESEND] sched/fair: Add min_ratio for cfs bandwidth_control Date: Wed, 19 Oct 2022 11:15:51 +0800 Message-Id: <20221019031551.24312-1-zhouchuyi@bytedance.com> X-Mailer: git-send-email 2.37.0 (Apple Git-136) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham 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 Tasks may be throttled when holding locks for a long time by current cfs bandwidth control mechanism once users set a too small quota/period ratio, which can result whole system get stuck[1]. In order to prevent the above situation from happening, this patch adds sysctl_sched_cfs_bandwidth_min_ratio in /proc/sys/kernel, which indicates the minimum percentage of quota/period users can set. The default value is zero and users can set quota and period without triggering this constraint. Link[1]:https://lore.kernel.org/lkml/5987be34-b527-4ff5-a17d-5f6f0dc94d6d@huawei.com/T/ Signed-off-by: Chuyi Zhou Suggested-by: Abel Wu --- include/linux/sched/sysctl.h | 4 ++++ kernel/sched/core.c | 23 +++++++++++++++++++++++ kernel/sysctl.c | 10 ++++++++++ 3 files changed, 37 insertions(+) diff --git a/include/linux/sched/sysctl.h b/include/linux/sched/sysctl.h index 303ee7dd0c7e..dedb18648f0e 100644 --- a/include/linux/sched/sysctl.h +++ b/include/linux/sched/sysctl.h @@ -21,6 +21,10 @@ enum sched_tunable_scaling { SCHED_TUNABLESCALING_END, }; +#ifdef CONFIG_CFS_BANDWIDTH +extern unsigned int sysctl_sched_cfs_bandwidth_min_ratio; +#endif + #define NUMA_BALANCING_DISABLED 0x0 #define NUMA_BALANCING_NORMAL 0x1 #define NUMA_BALANCING_MEMORY_TIERING 0x2 diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 5800b0623ff3..8f6cfd889e37 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -10504,6 +10504,12 @@ static u64 cpu_shares_read_u64(struct cgroup_subsys_state *css, } #ifdef CONFIG_CFS_BANDWIDTH +/* + * The minimum of quota/period ratio users can set, default is zero and users can set + * quota and period without triggering this constraint. + */ +unsigned int sysctl_sched_cfs_bandwidth_min_ratio; + static DEFINE_MUTEX(cfs_constraints_mutex); const u64 max_cfs_quota_period = 1 * NSEC_PER_SEC; /* 1s */ @@ -10513,6 +10519,20 @@ static const u64 max_cfs_runtime = MAX_BW * NSEC_PER_USEC; static int __cfs_schedulable(struct task_group *tg, u64 period, u64 runtime); +static int check_cfs_bandwidth_min_ratio(u64 period, u64 quota) +{ + u64 ratio; + + if (!sysctl_sched_cfs_bandwidth_min_ratio) + return 0; + + ratio = div64_u64(quota * 100, period); + if (ratio < sysctl_sched_cfs_bandwidth_min_ratio) + return -1; + + return 0; +} + static int tg_set_cfs_bandwidth(struct task_group *tg, u64 period, u64 quota, u64 burst) { @@ -10548,6 +10568,9 @@ static int tg_set_cfs_bandwidth(struct task_group *tg, u64 period, u64 quota, burst + quota > max_cfs_runtime)) return -EINVAL; + if (quota != RUNTIME_INF && check_cfs_bandwidth_min_ratio(period, quota)) + return -EINVAL; + /* * Prevent race between setting of cfs_rq->runtime_enabled and * unthrottle_offline_cfs_rqs(). diff --git a/kernel/sysctl.c b/kernel/sysctl.c index 188c305aeb8b..7d9743e8e514 100644 --- a/kernel/sysctl.c +++ b/kernel/sysctl.c @@ -1652,6 +1652,16 @@ static struct ctl_table kern_table[] = { .extra1 = SYSCTL_ZERO, }, #endif /* CONFIG_NUMA_BALANCING */ +#ifdef CONFIG_CFS_BANDWIDTH + { + .procname = "sched_cfs_bandwidth_min_ratio", + .data = &sysctl_sched_cfs_bandwidth_min_ratio, + .maxlen = sizeof(unsigned int), + .mode = 0644, + .proc_handler = proc_dointvec_minmax, + .extra1 = SYSCTL_ZERO, + }, +#endif /* CONFIG_CFS_BANDWIDTH */ { .procname = "panic", .data = &panic_timeout, -- 2.20.1