From patchwork Wed Jul 7 19:04:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Phil Auld X-Patchwork-Id: 471202 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=-17.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE autolearn=ham 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 37B2CC07E9C for ; Wed, 7 Jul 2021 19:05:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1707360FE3 for ; Wed, 7 Jul 2021 19:05:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230500AbhGGTHw (ORCPT ); Wed, 7 Jul 2021 15:07:52 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:41964 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231142AbhGGTHw (ORCPT ); Wed, 7 Jul 2021 15:07:52 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1625684711; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=Q4SO5ZfrTWxlwKe3MYbD+wDiZHgLaanRMH/JOhXsvA8=; b=AguPQptmrzGpQVAgAUPFZ8/VvnJigtq37e/A0cBq0PFMnRaeOWhQVG8Ns5I/7mwkDAJYUQ yvLdQVP0DVTE3vQXHAa4p5dv3DSOVMJbWn0lxKyM5EI0ahI2sEcK+x4OX/tQ3XmnPtwxk0 kMiJC/oj6DFwqcyYnj8o4+II47PtbU4= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-340-wOkl9LZTOaaT6-PggZHJgA-1; Wed, 07 Jul 2021 15:05:08 -0400 X-MC-Unique: wOkl9LZTOaaT6-PggZHJgA-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 0925556B43; Wed, 7 Jul 2021 19:05:07 +0000 (UTC) Received: from lorien.usersys.redhat.com (ovpn-116-12.rdu2.redhat.com [10.10.116.12]) by smtp.corp.redhat.com (Postfix) with ESMTP id 472A760843; Wed, 7 Jul 2021 19:04:58 +0000 (UTC) From: Phil Auld To: linux-kernel Cc: Peter Zijlstra , Waiman Long , Ingo Molnar , Juri Lelli , Vincent Guittot , stable@vger.kernel.org, Phil Auld Subject: [PATCH] sched: Fix nr_uninterruptible race causing increasing load average Date: Wed, 7 Jul 2021 15:04:57 -0400 Message-Id: <20210707190457.60521-1-pauld@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org 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(). Fixes: dbfb089d360b ("sched: Fix loadavg accounting race") Cc: Peter Zijlstra Cc: Ingo Molnar Cc: Juri Lelli Cc: Vincent Guittot Cc: Waiman Long Cc: Signed-off-by: Phil Auld --- kernel/sched/core.c | 7 +++++++ 1 file changed, 7 insertions(+) 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--; @@ -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++;