Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp206912imj; Thu, 14 Feb 2019 18:41:39 -0800 (PST) X-Google-Smtp-Source: AHgI3IYsj0M7Rtgrv9O1/F9VW0U7dZuTkhenYKbikgexTzfENbl5U+NqluwyNkHqtKIBLRf7ySBC X-Received: by 2002:a63:c04b:: with SMTP id z11mr3061701pgi.135.1550198499556; Thu, 14 Feb 2019 18:41:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1550198499; cv=none; d=google.com; s=arc-20160816; b=qn97V8JZISxbQTBenuJoLwHGDTGQxQ8tggWUPlnlce4FtiRSWgIZNyVD13xde81U9I hra/ikUCnFOq4nLU9LTiPyHdl18Lf7HsFC/lmbTH4XDq2GmvTCit1eP/APTTiEc8GE6b xerEkHRVrp55ni1OveWQmZVH2QStiE4qf+ffZMfDp+Cnevt0Yjhxf1N3nNZ73QlSLPmh mdNmKx6rDRViVWRdltnG55XNG1p7ciGwj+Ab3VKPhi8ncgxcuPiGrvYwFnakRVFv4QY8 WEfci7IojRX1JeTHABStNUaSDcJw6f+7wqbcvgDmordRhS6dK1qbU8Nfy8lUZeWMi5Uz 76PA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=nSfxq7xEkN8nQeKd2Zvt3VQyhltpfikUhNiCmDUxztA=; b=Ktl2w10xDuZ9+Cff7n5vhf9iV1LLpRQm6dgEvSsEOv9d04AaDbz2LhFAA8CruH+alE kJZigWE2PdFU/HMGx7WjTcU8QkfUD98SSkRqsjZLYBoylzBz+vnhV2iGVxGRTaV5vZQL pPXJNy6WIrZQ2aMbNylK9Qm1oPebtrGZQgzIo7eXbJRXVE4AGwR6rchBCF/zE+r+i6YJ uuBQCEvOAZx+xFb4P9TMPDkACati+F+Bq+sdvEzhRyMhhy/zsqEm3v/ZRafO2Rrr6d9a nQ0nWvPo6b68Hh0vtVkmHXnZg8esHNy5CjVte7Iz6ELFQ+T/ECipmrWkvNxp9bo42LD/ 0aAQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=ROj70EkU; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j2si4065033pfb.214.2019.02.14.18.41.23; Thu, 14 Feb 2019 18:41:39 -0800 (PST) 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=ROj70EkU; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730445AbfBOCkP (ORCPT + 99 others); Thu, 14 Feb 2019 21:40:15 -0500 Received: from mail.kernel.org ([198.145.29.99]:50216 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389899AbfBOCKD (ORCPT ); Thu, 14 Feb 2019 21:10:03 -0500 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 0D3AA206BA; Fri, 15 Feb 2019 02:10:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1550196603; bh=U4eNktDgDLUWZA4GxgdtHM+pvUcg9TM72SYwVJUk5oQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ROj70EkUeS3YweNXAdukca4H8Yhyhenrsk+K/v1OOz169wndSxFJVxMECVxGBfSbl VKeT/blGzOMoJKp5BSbiX5nORc/Sv8QTkAv4/a7/6HAc9BSnUcoYJFbmcKsCFiC/wn zEedfarCUiK7uiwROLsNmCiap9iZ+cdDVnazYzvs= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Peter Zijlstra , Davidlohr Bueso , Linus Torvalds , Thomas Gleixner , Waiman Long , Will Deacon , Ingo Molnar , Sasha Levin Subject: [PATCH AUTOSEL 4.20 38/77] sched/wake_q: Fix wakeup ordering for wake_q Date: Thu, 14 Feb 2019 21:08:16 -0500 Message-Id: <20190215020855.176727-38-sashal@kernel.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190215020855.176727-1-sashal@kernel.org> References: <20190215020855.176727-1-sashal@kernel.org> MIME-Version: 1.0 X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Peter Zijlstra [ Upstream commit 4c4e3731564c8945ac5ac90fc2a1e1f21cb79c92 ] Notable cmpxchg() does not provide ordering when it fails, however wake_q_add() requires ordering in this specific case too. Without this it would be possible for the concurrent wakeup to not observe our prior state. Andrea Parri provided: C wake_up_q-wake_q_add { int next = 0; int y = 0; } P0(int *next, int *y) { int r0; /* in wake_up_q() */ WRITE_ONCE(*next, 1); /* node->next = NULL */ smp_mb(); /* implied by wake_up_process() */ r0 = READ_ONCE(*y); } P1(int *next, int *y) { int r1; /* in wake_q_add() */ WRITE_ONCE(*y, 1); /* wake_cond = true */ smp_mb__before_atomic(); r1 = cmpxchg_relaxed(next, 1, 2); } exists (0:r0=0 /\ 1:r1=0) This "exists" clause cannot be satisfied according to the LKMM: Test wake_up_q-wake_q_add Allowed States 3 0:r0=0; 1:r1=1; 0:r0=1; 1:r1=0; 0:r0=1; 1:r1=1; No Witnesses Positive: 0 Negative: 3 Condition exists (0:r0=0 /\ 1:r1=0) Observation wake_up_q-wake_q_add Never 0 3 Reported-by: Yongji Xie Signed-off-by: Peter Zijlstra (Intel) Cc: Davidlohr Bueso Cc: Linus Torvalds Cc: Peter Zijlstra Cc: Thomas Gleixner Cc: Waiman Long Cc: Will Deacon Signed-off-by: Ingo Molnar Signed-off-by: Sasha Levin --- kernel/sched/core.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 6fedf3a98581..463af32de32c 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -405,10 +405,11 @@ void wake_q_add(struct wake_q_head *head, struct task_struct *task) * its already queued (either by us or someone else) and will get the * wakeup due to that. * - * This cmpxchg() executes a full barrier, which pairs with the full - * barrier executed by the wakeup in wake_up_q(). + * In order to ensure that a pending wakeup will observe our pending + * state, even in the failed case, an explicit smp_mb() must be used. */ - if (cmpxchg(&node->next, NULL, WAKE_Q_TAIL)) + smp_mb__before_atomic(); + if (cmpxchg_relaxed(&node->next, NULL, WAKE_Q_TAIL)) return; get_task_struct(task); -- 2.19.1