Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp3244725pxb; Mon, 16 Nov 2020 09:20:15 -0800 (PST) X-Google-Smtp-Source: ABdhPJwAkwbTUMLXRW9VQKnt83AA7Rwy3jyTqB4THdSkf3tIRnccTlN+i8obYqq7rovBR8m2Qu77 X-Received: by 2002:a17:906:c836:: with SMTP id dd22mr14975801ejb.552.1605547215591; Mon, 16 Nov 2020 09:20:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605547215; cv=none; d=google.com; s=arc-20160816; b=FIEB77mIlZlRG2MyOkAsepgt9CfX2iLw/8GZ5f80lyDm8t2kwuzR4PGQoHEtOkDEt5 iTsX4OptE+GZnZ+O041rryHHcEHQNZlm17YorSDwD+uGyl2iZPrkp+a5BoswH8zH1VxV 6re53wjc9iDdYQhaaIlgAnO1EXSIiyy+rd/fd3wtmweoOHBN4G6ZF10Sih6M6I4XY6GX HEgnBJagd+9941s+3I/yqjjTKIqbuUdfYXWew9WZnFwpcTe9nAd5YSPeoPSsFYpgZqNA 5jp1ijH8f6tMoTdL6zNM4M1mqCr5SE7UNuXuozSgKmXKHOM/HRrjAi2Ii5Gndpx7kbMq DwBg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=eAHbNOUxBLYZx8sPTDx+9rPeQ810n55f1GXyO+BuDTs=; b=ohgOk1UT9oLvGkg+oTVxSGq4m/HJwpyWO0pgk5dhdQi+UCpoWAEnqi0ffD3UxGo//x T2yEm6Oxw8p6i9TSVkVovttaZHBpDuba0n9e9QIHryTGpGejbw+NSQncx763g3HJsQ+o dS4/9IVPbvN3eKAJkDbQTpaiLcncdL8WY3RJSMljML7OnKIrBsYV20tnwIuabGD1D/dp VvejojsvtAKal5cIcvIK0czWt+voe1raTL7IpMyTlc6uUNLtzYDgljumqP4gvK79j6yh lrGBvZpjIZ8BEp8F8NH+bpRmYz6QLCn+CDlPVl/AA7hsSUxLrF8yUSDlwWwNWFprB348 r+Yw== 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 bs2si12475734edb.559.2020.11.16.09.19.52; Mon, 16 Nov 2020 09:20:15 -0800 (PST) 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 S1732688AbgKPRQr (ORCPT + 99 others); Mon, 16 Nov 2020 12:16:47 -0500 Received: from outbound-smtp44.blacknight.com ([46.22.136.52]:48283 "EHLO outbound-smtp44.blacknight.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732627AbgKPRQr (ORCPT ); Mon, 16 Nov 2020 12:16:47 -0500 Received: from mail.blacknight.com (pemlinmail04.blacknight.ie [81.17.254.17]) by outbound-smtp44.blacknight.com (Postfix) with ESMTPS id B87CAF856F for ; Mon, 16 Nov 2020 17:16:44 +0000 (GMT) Received: (qmail 14252 invoked from network); 16 Nov 2020 17:16:44 -0000 Received: from unknown (HELO techsingularity.net) (mgorman@techsingularity.net@[84.203.22.4]) by 81.17.254.9 with ESMTPSA (AES256-SHA encrypted, authenticated); 16 Nov 2020 17:16:43 -0000 Date: Mon, 16 Nov 2020 17:16:41 +0000 From: Mel Gorman To: Peter Zijlstra Cc: Will Deacon , Davidlohr Bueso , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: Re: Loadavg accounting error on arm64 Message-ID: <20201116171641.GU3371@techsingularity.net> References: <20201116091054.GL3371@techsingularity.net> <20201116131102.GA29992@willie-the-truck> <20201116133721.GQ3371@techsingularity.net> <20201116142005.GE3121392@hirez.programming.kicks-ass.net> <20201116155232.GS3371@techsingularity.net> <20201116165415.GG3121392@hirez.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-15 Content-Disposition: inline In-Reply-To: <20201116165415.GG3121392@hirez.programming.kicks-ass.net> User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Nov 16, 2020 at 05:54:15PM +0100, Peter Zijlstra wrote: > > > And then the stores of X and Y clobber one another.. Hummph, seems > > > reasonable. One quick thing to test would be something like this: > > > > > > > > > diff --git a/include/linux/sched.h b/include/linux/sched.h > > > index 7abbdd7f3884..9844e541c94c 100644 > > > --- a/include/linux/sched.h > > > +++ b/include/linux/sched.h > > > @@ -775,7 +775,9 @@ struct task_struct { > > > unsigned sched_reset_on_fork:1; > > > unsigned sched_contributes_to_load:1; > > > unsigned sched_migrated:1; > > > + unsigned :0; > > > unsigned sched_remote_wakeup:1; > > > + unsigned :0; > > > #ifdef CONFIG_PSI > > > unsigned sched_psi_wake_requeue:1; > > > #endif > > > > I'll test this after the smp_wmb() test completes. While a clobbering may > > be the issue, I also think the gap between the rq->nr_uninterruptible++ > > and smp_store_release(prev->on_cpu, 0) is relevant and a better candidate. > > I really don't understand what you wrote in that email... Sorry :(. I tried writing a changelog showing where I think the race might be. I'll queue up your patch that is potentially sched_migrated and sched_remote_wakeup being clobbered. --8<-- sched: Fix loadavg accounting race on arm64 An internal bug report filed against a 5.8 and 5.9 kernel that loadavg was "exploding" on arm64 on a machines acting as a build servers. It happened on at least two different arm64 variants. That setup is complex to replicate but can be reproduced by running hackbench-process-pipes while heavily overcommitting a machine with 96 logical CPUs and then checking if loadavg drops afterwards. With an MMTests clone, reproduce it as follows ./run-mmtests.sh --config configs/config-workload-hackbench-process-pipes --no-monitor testrun; \ for i in `seq 1 60`; do cat /proc/loadavg; sleep 60; done The reproduction case simply hammers the case where a task can be descheduling while also being woken by another task at the same time. After the test completes, load avg should reach 0 within a few minutes. Commit dbfb089d360b ("sched: Fix loadavg accounting race") fixed a loadavg accounting race in the generic case. Later it was documented why the ordering of when p->sched_contributes_to_load is read/updated relative to p->on_cpu. This is critical when a task is descheduling at the same time it is being activated on another CPU. While the load/stores happen under the RQ lock, the RQ lock on its own does not give any guarantees on the task state. The problem appears to be because the schedule and wakeup paths rely on being ordered by ->on_cpu for some fields as documented in core.c under "Notes on Program-Order guarantees on SMP systems". However, this can happen CPU 0 CPU 1 CPU 2 __schedule() prev->sched_contributes_to_load = 1 rq->nr_uninterruptible++; rq_unlock_irq try_to_wake_up smp_load_acquire(&p->on_cpu) && ttwu_queue_wakelist(p) == true ttwu_queue_wakelist ttwu_queue_cond (true) __ttwu_queue_wakelist sched_ttwu_pending ttwu_do_activate if (p->sched_contributes_to_load) (wrong value observed, load drifts) finish_task smp_store_release(X->on_cpu, 0) There is a gap between when rq->nr_uninterruptible is written to before p->on_cpu is updated with smp_store_release(). During that window, a parallel waker may observe the incorrect value for p->sched_contributes_to_load and fail to decrement rq->nr_uninterruptible and the loadavg starts drifting. This patch adds a write barrier after nr_uninterruptible is updated with the matching read barrier done when reading p->on_cpu in the ttwu path. With the patch applied, the load averages taken every minute after the hackbench test case completes is 950.21 977.17 990.69 1/853 2117 349.00 799.32 928.69 1/859 2439 128.18 653.85 870.56 1/861 2736 47.08 534.84 816.08 1/860 3029 17.29 437.50 765.00 1/865 3357 6.35 357.87 717.13 1/865 3653 2.33 292.74 672.24 1/861 3709 0.85 239.46 630.17 1/859 3711 0.31 195.87 590.73 1/857 3713 0.11 160.22 553.76 1/853 3715 Without the patch, the load average stabilised at 244 on an otherwise idle machine. Fixes: c6e7bd7afaeb ("sched/core: Optimize ttwu() spinning on p->on_cpu") Signed-off-by: Mel Gorman Cc: stable@vger.kernel.org # v5.8+ diff --git a/kernel/sched/core.c b/kernel/sched/core.c index d2003a7d5ab5..877eaeba45ac 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -4459,14 +4459,26 @@ static void __sched notrace __schedule(bool preempt) if (signal_pending_state(prev_state, prev)) { prev->state = TASK_RUNNING; } else { - prev->sched_contributes_to_load = + int acct_load = (prev_state & TASK_UNINTERRUPTIBLE) && !(prev_state & TASK_NOLOAD) && !(prev->flags & PF_FROZEN); - if (prev->sched_contributes_to_load) + prev->sched_contributes_to_load = acct_load; + if (acct_load) { rq->nr_uninterruptible++; + /* + * Pairs with p->on_cpu ordering, either a + * smp_load_acquire or smp_cond_load_acquire + * in the ttwu path before ttwu_do_activate + * p->sched_contributes_to_load. It's only + * after the nr_interruptible update happens + * that the ordering is critical. + */ + smp_wmb(); + } + /* * __schedule() ttwu() * prev_state = prev->state; if (p->on_rq && ...) -- Mel Gorman SUSE Labs