From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-11.0 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 08435C07E9C for ; Thu, 8 Jul 2021 07:48:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E29A961CDE for ; Thu, 8 Jul 2021 07:48:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230522AbhGHHvZ (ORCPT ); Thu, 8 Jul 2021 03:51:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44180 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229851AbhGHHvX (ORCPT ); Thu, 8 Jul 2021 03:51:23 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 630A0C061574; Thu, 8 Jul 2021 00:48:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=vdXYS9ID+bf5JGS30Bu8QVJFnhfQslN7L9O4LZcVUew=; b=veP9x6J5Xx9infh1+qhJzFnMSP Ir16CeY5Js1kPyDUy3xOgKtgHD2L69JgD5gncBHTXV3RyGIiRbb2ker5Id0gd08ZD7q9GTkgqk8wM Nw0wdpD33M5Z3SZecihT87UaxWDQVeI5h1hSRMxjFWOuj7tzytvrZxYNX/BYIrwd37TIbypBSLZAO ADkn1UMIl9HgaiXMyzVp8zYDqx02nCr60ohiUo3D3msf1okSHPy9g6/q+BazID9ObSYGfrLkfUz1h UuJD/QbwPcvmhHHUEYrdoGLY5JsbW+va8MqrpncyveR8ZBIqhv3WzcJufbYj7DojRkq98uXJVNpgB LuYFwLmQ==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1m1OlP-00DCbv-Vf; Thu, 08 Jul 2021 07:48:15 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id 8236230007E; Thu, 8 Jul 2021 09:48:03 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 6980C2CB89AE0; Thu, 8 Jul 2021 09:48:03 +0200 (CEST) Date: Thu, 8 Jul 2021 09:48:03 +0200 From: Peter Zijlstra To: Phil Auld Cc: linux-kernel , Waiman Long , Ingo Molnar , Juri Lelli , Vincent Guittot , stable@vger.kernel.org Subject: Re: [PATCH] sched: Fix nr_uninterruptible race causing increasing load average Message-ID: References: <20210707190457.60521-1-pauld@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jul 08, 2021 at 09:26:26AM +0200, Peter Zijlstra wrote: > On Wed, Jul 07, 2021 at 03:04:57PM -0400, Phil Auld wrote: > > On systems with weaker memory ordering (e.g. power) commit dbfb089d360b > > ("sched: Fix loadavg accounting race") causes increasing values of load > > average (via rq->calc_load_active and calc_load_tasks) due to the wakeup > > CPU not always seeing the write to task->sched_contributes_to_load in > > __schedule(). Missing that we fail to decrement nr_uninterruptible when > > waking up a task which incremented nr_uninterruptible when it slept. > > > > The rq->lock serialization is insufficient across different rq->locks. > > > > Add smp_wmb() to schedule and smp_rmb() before the read in > > ttwu_do_activate(). > > > diff --git a/kernel/sched/core.c b/kernel/sched/core.c > > index 4ca80df205ce..ced7074716eb 100644 > > --- a/kernel/sched/core.c > > +++ b/kernel/sched/core.c > > @@ -2992,6 +2992,8 @@ ttwu_do_activate(struct rq *rq, struct task_struct *p, int wake_flags, > > > > lockdep_assert_held(&rq->lock); > > > > + /* Pairs with smp_wmb in __schedule() */ > > + smp_rmb(); > > if (p->sched_contributes_to_load) > > rq->nr_uninterruptible--; > > > > Is this really needed ?! (this question is a big fat clue the comment is > insufficient). AFAICT try_to_wake_up() has a LOAD-ACQUIRE on p->on_rq > and hence the p->sched_contributed_to_load must already happen after. > > > @@ -5084,6 +5086,11 @@ static void __sched notrace __schedule(bool preempt) > > !(prev_state & TASK_NOLOAD) && > > !(prev->flags & PF_FROZEN); > > > > + /* > > + * Make sure the previous write is ordered before p->on_rq etc so > > + * that it is visible to other cpus in the wakeup path (ttwu_do_activate()). > > + */ > > + smp_wmb(); > > if (prev->sched_contributes_to_load) > > rq->nr_uninterruptible++; > > That comment is terrible, look at all the other barrier comments around > there for clues; in effect you're worrying about: > > p->sched_contributes_to_load = X R1 = p->on_rq > WMB RMB > p->on_rq = Y R2 = p->sched_contributes_to_load > > Right? > > > Bah bah bah.. I so detest having to add barriers here for silly > accounting. Let me think about this a little. I got the below: __schedule() ttwu() rq_lock() raw_spin_lock(&p->pi_lock) smp_mb__after_spinlock(); smp_mb__after_spinlock(); p->sched_contributes_to_load = X; if (READ_ONCE(p->on_rq) && ...) goto unlock; smp_acquire__after_ctrl_dep(); smp_cond_load_acquire(&p->on_cpu, !VAL) deactivate_task() p->on_rq = 0; context_switch() finish_task_switch() finish_task() smp_store_release(p->on_cpu, 0); ttwu_queue() rq_lock() ttwu_do_activate() if (p->sched_contributes_to_load) ... rq_unlock() raw_spin_unlock(&p->pi_lock); finish_lock_switch() rq_unlock(); The only way for ttwu() to end up in an enqueue, is if it did a LOAD-ACQUIRE on ->on_cpu, but that orders with the STORE-RELEASE on the same, which ensures the p->sched_contributes_to_load LOAD must happen after the STORE. What am I missing? Your Changelog/comments provide insufficient clues..