Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp3740663ybk; Tue, 19 May 2020 11:49:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJywo293b027bmb/EbDcH8zQrCjHYQunE5yVozybl1NM+8F47Z8h6+L/ke/344HP6XqAm5SO X-Received: by 2002:a50:88c6:: with SMTP id d64mr283216edd.324.1589914158069; Tue, 19 May 2020 11:49:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589914158; cv=none; d=google.com; s=arc-20160816; b=MeQL9GewFOTxnFBfn3M7NZP0cc+LW0U68UkxOnM6xZK0xqXDEz63SWPGVwPIBiNhib iE7MVZPGZSGsZllYcQDYPVwhh3QOjK9eI7Gavvbl40kl4Joxuh0wFGiu6oRC9Rwe44ws jXpNqiFvP7rc/pgrWOJM8/RUFnN0o+XdZ7EyqNGFliDM2sfdvD8Pfs7Ai4MoDpt74Iwp v14Lf//3fUO6HMQARksEcLQm1OmGWAqv0Nslev4oZGeKfP2A5hJqH5uAUyiUz0Eh+aPD 2z+n6p5zNYSyiMwPrCSkJunKTYw99QYyj28ejHuQMnh+NbW6TCpyR2mw4/OOsXdmFi33 aPCQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :robot-unsubscribe:robot-id:message-id:mime-version:references :in-reply-to:cc:subject:to:reply-to:from:date; bh=ioqWgJePBdjFR+Ig2QyTh09jUJVXuxafOMEhB+RLpJw=; b=hsUAoPkZE4+1q2DDpbqI84oAmOGLwp4YZM2pBhj429H6ZXkFFRiaSIhkS+nyPl1hOh MMh1udPzJxoEABabr64jlkSRmsGNc64JvakqnVXoss0mrg6LgLhGllq/V/7/XIbZ2Dzf MA9VOSt8BFVzlBppRDT1vpx4voItUIp9Pgpt4ySLwBHpm3SJxQNhYREm23j8WWJ21C/v XyusdXH0BhK+bZ84WaJ+kqmPuVmHCnyCp70H691AaF4VUKBBj0nTZw5ksjx3f318RrBc 0jwz+m/JbAMjECXuRv0+mozoyv8I1xsal+sxHS2rj3hEaHoeRojQXhR+x34NcKJp0lJf DP0g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i6si422549eje.512.2020.05.19.11.48.55; Tue, 19 May 2020 11:49:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727854AbgESSra (ORCPT + 99 others); Tue, 19 May 2020 14:47:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40690 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726290AbgESSra (ORCPT ); Tue, 19 May 2020 14:47:30 -0400 Received: from Galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C74BAC08C5C0; Tue, 19 May 2020 11:47:29 -0700 (PDT) Received: from [5.158.153.53] (helo=tip-bot2.lab.linutronix.de) by Galois.linutronix.de with esmtpsa (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1jb7Dq-0006cE-MY; Tue, 19 May 2020 20:44:14 +0200 Received: from [127.0.1.1] (localhost [IPv6:::1]) by tip-bot2.lab.linutronix.de (Postfix) with ESMTP id 50DA11C047E; Tue, 19 May 2020 20:44:14 +0200 (CEST) Date: Tue, 19 May 2020 18:44:14 -0000 From: "tip-bot2 for Huaixin Chang" Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: sched/core] sched: Defend cfs and rt bandwidth quota against overflow Cc: Huaixin Chang , "Peter Zijlstra (Intel)" , Ben Segall , x86 , LKML In-Reply-To: <20200425105248.60093-1-changhuaixin@linux.alibaba.com> References: <20200425105248.60093-1-changhuaixin@linux.alibaba.com> MIME-Version: 1.0 Message-ID: <158991385425.17951.13987676828295873063.tip-bot2@tip-bot2> X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Linutronix-Spam-Score: -1.0 X-Linutronix-Spam-Level: - X-Linutronix-Spam-Status: No , -1.0 points, 5.0 required, ALL_TRUSTED=-1,SHORTCIRCUIT=-0.0001 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The following commit has been merged into the sched/core branch of tip: Commit-ID: d505b8af58912ae1e1a211fabc9995b19bd40828 Gitweb: https://git.kernel.org/tip/d505b8af58912ae1e1a211fabc9995b19bd40828 Author: Huaixin Chang AuthorDate: Sat, 25 Apr 2020 18:52:48 +08:00 Committer: Peter Zijlstra CommitterDate: Tue, 19 May 2020 20:34:14 +02:00 sched: Defend cfs and rt bandwidth quota against overflow When users write some huge number into cpu.cfs_quota_us or cpu.rt_runtime_us, overflow might happen during to_ratio() shifts of schedulable checks. to_ratio() could be altered to avoid unnecessary internal overflow, but min_cfs_quota_period is less than 1 << BW_SHIFT, so a cutoff would still be needed. Set a cap MAX_BW for cfs_quota_us and rt_runtime_us to prevent overflow. Signed-off-by: Huaixin Chang Signed-off-by: Peter Zijlstra (Intel) Reviewed-by: Ben Segall Link: https://lkml.kernel.org/r/20200425105248.60093-1-changhuaixin@linux.alibaba.com --- kernel/sched/core.c | 8 ++++++++ kernel/sched/rt.c | 12 +++++++++++- kernel/sched/sched.h | 2 ++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 74fb89b..fa905b6 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -7379,6 +7379,8 @@ static DEFINE_MUTEX(cfs_constraints_mutex); const u64 max_cfs_quota_period = 1 * NSEC_PER_SEC; /* 1s */ static const u64 min_cfs_quota_period = 1 * NSEC_PER_MSEC; /* 1ms */ +/* More than 203 days if BW_SHIFT equals 20. */ +static const u64 max_cfs_runtime = MAX_BW * NSEC_PER_USEC; static int __cfs_schedulable(struct task_group *tg, u64 period, u64 runtime); @@ -7407,6 +7409,12 @@ static int tg_set_cfs_bandwidth(struct task_group *tg, u64 period, u64 quota) return -EINVAL; /* + * Bound quota to defend quota against overflow during bandwidth shift. + */ + if (quota != RUNTIME_INF && quota > max_cfs_runtime) + return -EINVAL; + + /* * Prevent race between setting of cfs_rq->runtime_enabled and * unthrottle_offline_cfs_rqs(). */ diff --git a/kernel/sched/rt.c b/kernel/sched/rt.c index df11d88..6d60ba2 100644 --- a/kernel/sched/rt.c +++ b/kernel/sched/rt.c @@ -9,6 +9,8 @@ int sched_rr_timeslice = RR_TIMESLICE; int sysctl_sched_rr_timeslice = (MSEC_PER_SEC / HZ) * RR_TIMESLICE; +/* More than 4 hours if BW_SHIFT equals 20. */ +static const u64 max_rt_runtime = MAX_BW; static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun); @@ -2585,6 +2587,12 @@ static int tg_set_rt_bandwidth(struct task_group *tg, if (rt_period == 0) return -EINVAL; + /* + * Bound quota to defend quota against overflow during bandwidth shift. + */ + if (rt_runtime != RUNTIME_INF && rt_runtime > max_rt_runtime) + return -EINVAL; + mutex_lock(&rt_constraints_mutex); err = __rt_schedulable(tg, rt_period, rt_runtime); if (err) @@ -2702,7 +2710,9 @@ static int sched_rt_global_validate(void) return -EINVAL; if ((sysctl_sched_rt_runtime != RUNTIME_INF) && - (sysctl_sched_rt_runtime > sysctl_sched_rt_period)) + ((sysctl_sched_rt_runtime > sysctl_sched_rt_period) || + ((u64)sysctl_sched_rt_runtime * + NSEC_PER_USEC > max_rt_runtime))) return -EINVAL; return 0; diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index 2bd2a22..f7ab633 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -1915,6 +1915,8 @@ extern void init_dl_inactive_task_timer(struct sched_dl_entity *dl_se); #define BW_SHIFT 20 #define BW_UNIT (1 << BW_SHIFT) #define RATIO_SHIFT 8 +#define MAX_BW_BITS (64 - BW_SHIFT) +#define MAX_BW ((1ULL << MAX_BW_BITS) - 1) unsigned long to_ratio(u64 period, u64 runtime); extern void init_entity_runnable_average(struct sched_entity *se);