Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp2447052yba; Mon, 15 Apr 2019 11:49:43 -0700 (PDT) X-Google-Smtp-Source: APXvYqyyaERMzJ7yvxRd25aDaE4/ZhMye8NmbEQ5LDTmmU2gVg7wJEMgE50G7rXJVbYyxObAybVu X-Received: by 2002:a17:902:5a45:: with SMTP id f5mr39727770plm.23.1555354183296; Mon, 15 Apr 2019 11:49:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555354183; cv=none; d=google.com; s=arc-20160816; b=ZhDoST4AyKu0W3z+o9yTJy71EBIqV4fCeVZ8vFTnx0F45r3LZzYZNqzUl3g5+bLVQU VTsetnC0Cap0nnESuJZEQPgii2TbQQWKD7XzFw8qKd7fAMQwUA9SUiKj5Jg+mnBz2R5U O/1Jk5ET6zmf/fNOm6FblNo5042LFfwqM5OboOUuEdNBGumunrbclWE2bMBvv9PQ4SIS JkNTaxdPgO0nFX5aXKDvNOoqWWfvFyxULMnCRT89l82WXNM32o8JTF85v7hR0/0+j/D3 tCplogfnplyVaJDCysXMJVrFFHkvT4KIqtxO1Tz3pHdlZ3DnQtodXYIfbj7zjTb2jEiL gADQ== 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=m2Q4UvrSVdfZHe7m6C2wbbpl44h1/lgVQTYU5th4ssc=; b=Cv58XPpsRy/QHPX3nhlgRswlTTCM4x0aiu0oV5aEj+rhIETY7FAGzPATrk3RwHQhRy f3CaKoczCn8G2eXRtxRcN+OV7HO97SLL8fx1dpmRxjXFzURpZD0LdrhU4y4pQMwxfPbM j3dFWLX9SqxAhFQ9Av/lVj7BxNt8vm9CafC7oCvppyrLdiFZdoE0jjxgUzZk+YERMaXN i2XGmBu+HJ7oTI36pH/MD+W4O3udtR/Vq0RqfTxI3agPwn3kp7R6DMAuWiBKpLQNk8XC BkcQtIGyhFONFzM089outtMB1xkJ9XlqQ9VYoX0BJ1aa2wYnHrGk6pp7FJhERls+I+XB tCBg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=US3Iuxs5; 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 11si45594265plb.330.2019.04.15.11.49.27; Mon, 15 Apr 2019 11:49:43 -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; dkim=pass header.i=@kernel.org header.s=default header.b=US3Iuxs5; 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 S1729082AbfDOSsL (ORCPT + 99 others); Mon, 15 Apr 2019 14:48:11 -0400 Received: from mail.kernel.org ([198.145.29.99]:51876 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729042AbfDOSsI (ORCPT ); Mon, 15 Apr 2019 14:48:08 -0400 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 247B32087C; Mon, 15 Apr 2019 18:48:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1555354087; bh=UOafVdwbqgjQsHeaOpf8h4tm7h3icaKv5TC4D32yQ2s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=US3Iuxs5dCtadIKC0JNtpm7jPZDFNAGSkecsawISFwF34KJ+nOn+0ipJ/pI5rBy3a yfwqJHrscs6i5/tReEkzZwNTRZlIW4z/6t2kPgUyVVgUu2lPbadYGA66NkfFZAjMOL P/87wY1o1o5Mbg1iRWyakhUgnT12etuoUVgzLzIs= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Mel Gorman , "Peter Zijlstra (Intel)" , Valentin Schneider , Linus Torvalds , Mike Galbraith , Thomas Gleixner , Ingo Molnar Subject: [PATCH 4.9 74/76] sched/fair: Do not re-read ->h_load_next during hierarchical load calculation Date: Mon, 15 Apr 2019 20:44:38 +0200 Message-Id: <20190415183729.498600996@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190415183707.712011689@linuxfoundation.org> References: <20190415183707.712011689@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: Mel Gorman commit 0e9f02450da07fc7b1346c8c32c771555173e397 upstream. A NULL pointer dereference bug was reported on a distribution kernel but the same issue should be present on mainline kernel. It occured on s390 but should not be arch-specific. A partial oops looks like: Unable to handle kernel pointer dereference in virtual kernel address space ... Call Trace: ... try_to_wake_up+0xfc/0x450 vhost_poll_wakeup+0x3a/0x50 [vhost] __wake_up_common+0xbc/0x178 __wake_up_common_lock+0x9e/0x160 __wake_up_sync_key+0x4e/0x60 sock_def_readable+0x5e/0x98 The bug hits any time between 1 hour to 3 days. The dereference occurs in update_cfs_rq_h_load when accumulating h_load. The problem is that cfq_rq->h_load_next is not protected by any locking and can be updated by parallel calls to task_h_load. Depending on the compiler, code may be generated that re-reads cfq_rq->h_load_next after the check for NULL and then oops when reading se->avg.load_avg. The dissassembly showed that it was possible to reread h_load_next after the check for NULL. While this does not appear to be an issue for later compilers, it's still an accident if the correct code is generated. Full locking in this path would have high overhead so this patch uses READ_ONCE to read h_load_next only once and check for NULL before dereferencing. It was confirmed that there were no further oops after 10 days of testing. As Peter pointed out, it is also necessary to use WRITE_ONCE() to avoid any potential problems with store tearing. Signed-off-by: Mel Gorman Signed-off-by: Peter Zijlstra (Intel) Reviewed-by: Valentin Schneider Cc: Linus Torvalds Cc: Mike Galbraith Cc: Peter Zijlstra Cc: Thomas Gleixner Cc: Fixes: 685207963be9 ("sched: Move h_load calculation to task_h_load()") Link: https://lkml.kernel.org/r/20190319123610.nsivgf3mjbjjesxb@techsingularity.net Signed-off-by: Ingo Molnar Signed-off-by: Greg Kroah-Hartman --- kernel/sched/fair.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -6634,10 +6634,10 @@ static void update_cfs_rq_h_load(struct if (cfs_rq->last_h_load_update == now) return; - cfs_rq->h_load_next = NULL; + WRITE_ONCE(cfs_rq->h_load_next, NULL); for_each_sched_entity(se) { cfs_rq = cfs_rq_of(se); - cfs_rq->h_load_next = se; + WRITE_ONCE(cfs_rq->h_load_next, se); if (cfs_rq->last_h_load_update == now) break; } @@ -6647,7 +6647,7 @@ static void update_cfs_rq_h_load(struct cfs_rq->last_h_load_update = now; } - while ((se = cfs_rq->h_load_next) != NULL) { + while ((se = READ_ONCE(cfs_rq->h_load_next)) != NULL) { load = cfs_rq->h_load; load = div64_ul(load * se->avg.load_avg, cfs_rq_load_avg(cfs_rq) + 1);