Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp2300200ybl; Thu, 19 Dec 2019 11:13:12 -0800 (PST) X-Google-Smtp-Source: APXvYqxZAXOm/fV0jvgU70QP1eCtGFNARFmUyLg/8rqY1X/nmL7OVB3bsqJ33jTQnxeBlhqoaqLS X-Received: by 2002:aca:568f:: with SMTP id k137mr3052123oib.63.1576782792591; Thu, 19 Dec 2019 11:13:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1576782792; cv=none; d=google.com; s=arc-20160816; b=0x0sekZpZS5M9vsthCJlFPfuZ2NjHRaAprp0vVCI/GG6Uc7vUlQ3VvEbZqpWqxnscQ TaK4tI5ST73RU2cFpMWvRo/4jSOOvbh9RE/dt2mw0+ajKvOwymLbQ48bszaway7DDBU8 tK3p6mxyHcCb+6OlARSGTS90lC4xzy/GUWY5FvrMiHRKtFlmfOy1LuYWHWqbTco8Lpii SC5KIx1mgUp0FcdjmQsD+bE5z3tNuRMvuDZ6vEqhvr2ZnqaTMwZ3jy0EfX1a9xdqy2kA ij4rwvfH9+WGJ7yyXWZcMsorMBj8o17aRPC/j/pV8pTENANEdJ1kQ8Zc9J5zPN6TCLeM 5dsw== 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=hWfcD7lfE4kaBzJeVUujOs8AnCXe3kpS9/VM1zkekh0=; b=cxm98F1+pgGltBkBpifXauoSb0kfCRGjFS1Ey8g00BGJfDL3BE5ocDh++0+xP6p6br I+byeO6wycIpomSiZi9WX2MVcMkmvEL6wtRqAUPIg5KC6o6IeY89aQxitjRKzztaI8M+ LknLh+WqMpWKvCsLeaICmxjga4MrQmqx/8SbQcbmU3ik5YBK/X2TkPeST8vEMomnnYlA gkYhS3ZdaCmIefAhlHPUK4f+14Qnsz0U4Z5p/aqCEsavKh71kmtyCYaA7rD47Ch33JZB Ac7gqwvbKus9MANw4ShbavoNZYHTdZopYgm0u2BrOnkv6WKnvOu9diad1cmz5iFyLrvr MSGA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Xnu5nD02; 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 e190si2839791oib.113.2019.12.19.11.12.59; Thu, 19 Dec 2019 11:13:12 -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=Xnu5nD02; 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 S1728192AbfLSTMM (ORCPT + 99 others); Thu, 19 Dec 2019 14:12:12 -0500 Received: from mail.kernel.org ([198.145.29.99]:54636 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727777AbfLSShO (ORCPT ); Thu, 19 Dec 2019 13:37:14 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (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 E9475222C2; Thu, 19 Dec 2019 18:37:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1576780632; bh=aDbn1bwYxv0Fr5AI2V6CwZjwn0FgbesOi5xWBwJ9Abs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Xnu5nD02Kl+NNvqy7XqBpRhakr/iPb3WbvgQTdDmcA54D9pPf5QoKzUl6LGnGFjIG EjicQbf21CJmq89wJO7cGFkzrITyz2If1i9yBWxdJqNJ2lvXkc/V6UfJfKitVMt84E metVJgW4LFPN6xjRCi3rbZ8oldHrD7BEfzxW7TGA= 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 Subject: [PATCH 4.4 053/162] sched/fair: Scale bandwidth quota and period without losing quota/period ratio precision Date: Thu, 19 Dec 2019 19:32:41 +0100 Message-Id: <20191219183211.113172209@linuxfoundation.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20191219183150.477687052@linuxfoundation.org> References: <20191219183150.477687052@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 commit 4929a4e6faa0f13289a67cae98139e727f0d4a97 upstream. 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: Greg Kroah-Hartman --- kernel/sched/fair.c | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -4055,20 +4055,28 @@ static enum hrtimer_restart sched_cfs_pe if (++count > 3) { u64 new, old = ktime_to_ns(cfs_b->period); - new = (old * 147) / 128; /* ~115% */ - new = min(new, max_cfs_quota_period); + /* + * 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; - 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)); + 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;