Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp4489150ybx; Mon, 4 Nov 2019 14:16:16 -0800 (PST) X-Google-Smtp-Source: APXvYqwxCK/ufehkCpHsJ5RVGtyr+0/NYRUxJRwYp5yFfryveU1Gnfshq1C4k+MSxqbwl+zyVNtO X-Received: by 2002:a17:906:b856:: with SMTP id ga22mr20819158ejb.231.1572905776852; Mon, 04 Nov 2019 14:16:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1572905776; cv=none; d=google.com; s=arc-20160816; b=pqM+Vv9s6gq+ond/Qfo4IFTGP2dCn6vgOHzopa84LnUNFOXn0Ww/5+KJyREp2PfHdG MG5tTYgKdKog+/9w8c99wbqYpA4YSDBPRFLZgDwXpm0ZEVi0Mf6WOgXoTgZMjliOiN3W PmL48cUZEgYJ2DuNM+g7YgrCj0wayFoJ4vk5WZRe3tVwiCChE0D7fBbgesOv2troE10q 6VJetzw1yM4kexIOPADY09ZFCHZe+GQuy5KK+on172hYOquG7GAuhna+dos5mmRsY85L xhMWtEvgf7cRWIZqN12aEfRF54ohHguwB9179cmqCyXapzfMUSu/d9CblZIhwEOyiSdV J08Q== 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:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=ufI7FiooH3oBXzmcow9InPs2qzdORn698joK/tNI5k0=; b=UDp5ZZaXEZStSzT2LenymeW1/bXy0lvtoYiqo1MM4i0pJwTCERWD5FMwfwMs4JJHvL xwcZrY9SNXxzQyzk+q2RsNaXrFAVgnTfdPC6Rv+LrMkA09l139Sr9psqkH4KuE+IDfY1 5jzQLrRHDRyaExUy5P5MMU4umly0OT9OdKcXP7z7eL9tuEGeqW/owrexudEuzCMcMfNy Sh0N28D1k0fya1ekJk6nuys02mUjZ2jb8swUo9kXRvTsDhTFsTjGPeY0RhlxEFTuKvwZ BZpgB2+VIBJGYHw4uW4PpUzdfIheiol3l3FolDaeJGceb0MYIZIqE+Be6/6WoRRFa2Ue AdJQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=iHORfobp; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z3si11736994ejb.42.2019.11.04.14.15.43; Mon, 04 Nov 2019 14:16:16 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=iHORfobp; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389410AbfKDWPH (ORCPT + 99 others); Mon, 4 Nov 2019 17:15:07 -0500 Received: from mail.kernel.org ([198.145.29.99]:38776 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388851AbfKDWGo (ORCPT ); Mon, 4 Nov 2019 17:06:44 -0500 Received: from localhost (6.204-14-84.ripe.coltfrance.com [84.14.204.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 0EF71217F4; Mon, 4 Nov 2019 22:06:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1572905203; bh=jn36PS6JD/UCan94jV0PXlMdlJkaCs61c+4OM1G1KZo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iHORfobpQSOBnICn5WOowlXS6OseSUT38JZv4LxOSNpDYK6tF8KrpDy8JlQUMs2+E hoNSFI01iWz5IYGMQ0W+EqDrBhEZ26QMe5XHKczK7gVFnsKa588Cv01MbSISFJ3ACM 9eAEtjzpcbCrOK1gb0OTV00Gpq+ud0XRtTttyc4A= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Phil Auld , Xuewei Zhang , "Peter Zijlstra (Intel)" , Anton Blanchard , Ben Segall , Dietmar Eggemann , Juri Lelli , Linus Torvalds , Mel Gorman , Steven Rostedt , Thomas Gleixner , Vincent Guittot , Ingo Molnar , Sasha Levin Subject: [PATCH 5.3 065/163] sched/fair: Scale bandwidth quota and period without losing quota/period ratio precision Date: Mon, 4 Nov 2019 22:44:15 +0100 Message-Id: <20191104212144.712060978@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191104212140.046021995@linuxfoundation.org> References: <20191104212140.046021995@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Xuewei Zhang [ Upstream commit 4929a4e6faa0f13289a67cae98139e727f0d4a97 ] The quota/period ratio is used to ensure a child task group won't get more bandwidth than the parent task group, and is calculated as: normalized_cfs_quota() = [(quota_us << 20) / period_us] If the quota/period ratio was changed during this scaling due to precision loss, it will cause inconsistency between parent and child task groups. See below example: A userspace container manager (kubelet) does three operations: 1) Create a parent cgroup, set quota to 1,000us and period to 10,000us. 2) Create a few children cgroups. 3) Set quota to 1,000us and period to 10,000us on a child cgroup. These operations are expected to succeed. However, if the scaling of 147/128 happens before step 3, quota and period of the parent cgroup will be changed: new_quota: 1148437ns, 1148us new_period: 11484375ns, 11484us And when step 3 comes in, the ratio of the child cgroup will be 104857, which will be larger than the parent cgroup ratio (104821), and will fail. Scaling them by a factor of 2 will fix the problem. Tested-by: Phil Auld Signed-off-by: Xuewei Zhang Signed-off-by: Peter Zijlstra (Intel) Acked-by: Phil Auld Cc: Anton Blanchard Cc: Ben Segall Cc: Dietmar Eggemann Cc: Juri Lelli Cc: Linus Torvalds Cc: Mel Gorman Cc: Peter Zijlstra Cc: Steven Rostedt Cc: Thomas Gleixner Cc: Vincent Guittot Fixes: 2e8e19226398 ("sched/fair: Limit sched_cfs_period_timer() loop to avoid hard lockup") Link: https://lkml.kernel.org/r/20191004001243.140897-1-xueweiz@google.com Signed-off-by: Ingo Molnar Signed-off-by: Sasha Levin --- kernel/sched/fair.c | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 86cfc5d5129ce..16b5d29bd7300 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -4995,20 +4995,28 @@ static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer) if (++count > 3) { u64 new, old = ktime_to_ns(cfs_b->period); - new = (old * 147) / 128; /* ~115% */ - new = min(new, max_cfs_quota_period); - - cfs_b->period = ns_to_ktime(new); - - /* since max is 1s, this is limited to 1e9^2, which fits in u64 */ - cfs_b->quota *= new; - cfs_b->quota = div64_u64(cfs_b->quota, old); - - pr_warn_ratelimited( - "cfs_period_timer[cpu%d]: period too short, scaling up (new cfs_period_us %lld, cfs_quota_us = %lld)\n", - smp_processor_id(), - div_u64(new, NSEC_PER_USEC), - div_u64(cfs_b->quota, NSEC_PER_USEC)); + /* + * Grow period by a factor of 2 to avoid losing precision. + * Precision loss in the quota/period ratio can cause __cfs_schedulable + * to fail. + */ + new = old * 2; + if (new < max_cfs_quota_period) { + cfs_b->period = ns_to_ktime(new); + cfs_b->quota *= 2; + + pr_warn_ratelimited( + "cfs_period_timer[cpu%d]: period too short, scaling up (new cfs_period_us = %lld, cfs_quota_us = %lld)\n", + smp_processor_id(), + div_u64(new, NSEC_PER_USEC), + div_u64(cfs_b->quota, NSEC_PER_USEC)); + } else { + pr_warn_ratelimited( + "cfs_period_timer[cpu%d]: period too short, but cannot scale up without losing precision (cfs_period_us = %lld, cfs_quota_us = %lld)\n", + smp_processor_id(), + div_u64(old, NSEC_PER_USEC), + div_u64(cfs_b->quota, NSEC_PER_USEC)); + } /* reset count so we don't come right back in here */ count = 0; -- 2.20.1