Received: by 2002:ac0:a874:0:0:0:0:0 with SMTP id c49csp448101ima; Fri, 15 Mar 2019 06:31:50 -0700 (PDT) X-Google-Smtp-Source: APXvYqz/WgoxqIo55hHQwoNI+khxsEJrJYSIxWMsTpltrmFvGCW0gohkK/vpKQxsXDyUz1bbyUid X-Received: by 2002:a17:902:6f08:: with SMTP id w8mr4302323plk.5.1552656710586; Fri, 15 Mar 2019 06:31:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1552656710; cv=none; d=google.com; s=arc-20160816; b=rILWn5eyZU9eWhanBWG07QRKoRkGXi0NY86Pew5ndgI+o35HZC1i7DFpsE1Qg+316Z v0YbCHsc1P6ABOrwJ5ULlHqiuPwvkyoShI2ytR7UhIuMuObth+RpvHXSI1YeEu+esyln fvKw7RGunanOeN+ezc0EctA4yUzjgAMtHbPBvMliH3u2LhbbP7NQ0X6w+qtQIxT8jPU4 /jxeu3BNbv9cFXL/zXVUXOz4abvABAz7HWN4Or1xhktbBRFFmh5sZstWiAKp9T4tRFaS L/YJmgunZ2nkzx3hASKZSl49sU2Jw3hTUGiusDaYu2o3cHudS4NiqpHnmVRX8dfcBlFH N03w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=fd9NIEs2hsycsd21e7a8wr81ZzPCzDJ6g62SHB+gFdg=; b=lrz3Po2V7dCnLfnIquNrMEkvWz0UiGv4UuanERQuqJg5byucyIBxeDBPu3z4bVNdrZ qYqpV7x1jpusnHJ3wR95Lyjz5h8hFg1EVQ4vzTwJkDC4U0mG0gEgV2n/uP8yYApvjLbm NtCHHjYu+1tKEoj6f7kukySCeNa+vrRTV2n20Q6/fwh61Q0qXScY8W97Rvf5OurBESXz 5KMtGEKKjGP+kDzsJyqO/YBRGckus+MlTEYYj3tWdO+lIs79EA67IAUsjYLTnGJF8p30 3I43a/tCPJrt9txA4pot/N1PULkSkiwVoSZaND/m+/rOJwpc6stmnwo79pcE2zSyXvff HOOA== ARC-Authentication-Results: i=1; mx.google.com; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z192si1792001pgd.500.2019.03.15.06.31.34; Fri, 15 Mar 2019 06:31:50 -0700 (PDT) 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; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729105AbfCONap (ORCPT + 99 others); Fri, 15 Mar 2019 09:30:45 -0400 Received: from mx1.redhat.com ([209.132.183.28]:40722 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727370AbfCONap (ORCPT ); Fri, 15 Mar 2019 09:30:45 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D36443016643; Fri, 15 Mar 2019 13:30:44 +0000 (UTC) Received: from pauld.bos.csb (dhcp-17-51.bos.redhat.com [10.18.17.51]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 4C3481001DC9; Fri, 15 Mar 2019 13:30:44 +0000 (UTC) Date: Fri, 15 Mar 2019 09:30:42 -0400 From: Phil Auld To: Peter Zijlstra Cc: linux-kernel@vger.kernel.org, Ben Segall , Ingo Molnar Subject: Re: [PATCH] sched/fair: Limit sched_cfs_period_timer loop to avoid hard lockup Message-ID: <20190315133042.GB27131@pauld.bos.csb> References: <20190313150826.16862-1-pauld@redhat.com> <20190315101150.GV5996@hirez.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190315101150.GV5996@hirez.programming.kicks-ass.net> User-Agent: Mutt/1.5.21 (2010-09-15) X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.42]); Fri, 15 Mar 2019 13:30:44 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Mar 15, 2019 at 11:11:50AM +0100 Peter Zijlstra wrote: > On Wed, Mar 13, 2019 at 11:08:26AM -0400, Phil Auld wrote: > > diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c > > index 310d0637fe4b..90cc67bbf592 100644 > > --- a/kernel/sched/fair.c > > +++ b/kernel/sched/fair.c > > @@ -4859,19 +4859,51 @@ static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer) > > return HRTIMER_NORESTART; > > } > > > > +extern const u64 max_cfs_quota_period; > > +int cfs_period_autotune_loop_limit = 8; > > +int cfs_period_autotune_cushion_pct = 15; /* percentage added to period recalculation */ > > static const ? > > > + > > static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer) > > { > > struct cfs_bandwidth *cfs_b = > > container_of(timer, struct cfs_bandwidth, period_timer); > > + s64 nsstart, nsnow, new_period; > > u64 > > > int overrun; > > int idle = 0; > > + int count = 0; > > > > raw_spin_lock(&cfs_b->lock); > > + nsstart = ktime_to_ns(hrtimer_cb_get_time(timer)); > > we really should kill ktime :/ Anyway, you now do two indirect timer > calls back to back :/ > > And this is unconditional overhead. > > > for (;;) { > > overrun = hrtimer_forward_now(timer, cfs_b->period); > > if (!overrun) > > break; > > > > + if (++count > cfs_period_autotune_loop_limit) { > > + ktime_t old_period = ktime_to_ns(cfs_b->period); > > + > > + nsnow = ktime_to_ns(hrtimer_cb_get_time(timer)); > > + new_period = (nsnow - nsstart)/cfs_period_autotune_loop_limit; > > + > > + /* Make sure new period will be larger than old. */ > > + if (new_period < old_period) { > > + new_period = old_period; > > + } > > + new_period += (new_period * cfs_period_autotune_cushion_pct) / 100; > > Computers _suck_ at /100. And since you're free to pick the constant, > pick a power of two, computers love those. > Fair enough, I was thinking percents. And also that once we get in here it's not a really hot path. > > + > > + if (new_period > max_cfs_quota_period) > > + new_period = max_cfs_quota_period; > > + > > + cfs_b->period = ns_to_ktime(new_period); > > + cfs_b->quota += (cfs_b->quota * ((new_period - old_period) * 100)/old_period)/100; > > srsly!? Again, you can pick the constant to be anything, and you pick > such a horrid number?! > Same as above :) > > + 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(), cfs_b->period/NSEC_PER_USEC, cfs_b->quota/NSEC_PER_USEC); > > period was ktime_t, remember... > > > + > > And these here lines all all waaay too long. > > Also, is that complexity really needed? > > > + /* reset count so we don't come right back in here */ > > + count = 0; > > + } > > + > > idle = do_sched_cfs_period_timer(cfs_b, overrun); > > } > > if (idle) > > > Would not something simpler like the below also work? > I expect it would.... but the original concept increased the period to a bit more (15%) than the average time it was taking to go around the loop. This version will run the loop a lot longer as it's only increasing by 15% each time. In my setup it would go from ~2000 to ~11000 and be done. This one will go off, raise 2000 to 2300, then fire again, raise to 2645 , etc. Unless it's getting reset this would still be a one time thing so that may not matter. The math calculations do look better though. I knew there had to be a better way to scale up the quota. It just wasn't jumping out at me :) Thanks, Phil > > diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c > index ea74d43924b2..b71557be6b42 100644 > --- a/kernel/sched/fair.c > +++ b/kernel/sched/fair.c > @@ -4885,6 +4885,8 @@ static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer) > return HRTIMER_NORESTART; > } > > +extern const u64 max_cfs_quota_period; > + > static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer) > { > struct cfs_bandwidth *cfs_b = > @@ -4892,6 +4894,7 @@ static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer) > unsigned long flags; > int overrun; > int idle = 0; > + int count = 0; > > raw_spin_lock_irqsave(&cfs_b->lock, flags); > for (;;) { > @@ -4899,6 +4902,28 @@ static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer) > if (!overrun) > break; > > + 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 /= 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(), > + new/NSEC_PER_USEC, > + cfs_b->quota/NSEC_PER_USEC); > + > + /* reset count so we don't come right back in here */ > + count = 0; > + } > + > idle = do_sched_cfs_period_timer(cfs_b, overrun, flags); > } > if (idle) --