Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp1848900ybp; Fri, 11 Oct 2019 22:51:25 -0700 (PDT) X-Google-Smtp-Source: APXvYqzsjMSZr8FVsEJRkPeOl5M9RuV3G7ia6hJs0L3zSFV1jUVxW/vYTkDgCTf5Oqv+nzPwK2qJ X-Received: by 2002:a17:907:2172:: with SMTP id rl18mr17924678ejb.125.1570859485747; Fri, 11 Oct 2019 22:51:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570859485; cv=none; d=google.com; s=arc-20160816; b=YshnD60wHCtVE06aEJPVNu1m9hTwburFrJ3dWkuoqI1iQTKMeucFKJB2PfL8z3PrQ4 5W+hL/DolmEJsqo9tq2YgHSPz/ruT7aXl9laYZvUvdkcPrcbbgt2hJxRARhyPx03vZ2a BzKfpcGzZYiJOznpU8nF4YInDW1FHP94UGA588BpFmD0Mg6WGwlI3pPedEvR0lIDI93S 0ZByO2hlr0wN7s96TZZfl93auUcth2+h4ez8l/o+VHAX+0hgdtdjxuCBkZdtv412giW4 CSpxwhjZrPHq45gaqxGi5w7xnp6F/EUtYv0azWUIOQglAA/T4LdSdjrjFBbZUttStbTE O0wA== 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=hCNAY2B4GBIEKWr43sCTvg0r8VIO3WoulIoqc6vcbII=; b=N1ZpJ9eScGra3pcGSStsqvZDFlTA2NFqbw2yYjtHqeqnhPV1akCWsmkZiebpDI8mKK lQuzr/CtYHfqlThYDna1zyisW+4DV2izI3Y8glDYNOFYmttshT/q31+Q2e4mlsb67G+H FiWBnJldJ/BWhz0oM+QqoSxQg2NYLmqqnslGvVklkRZXlY70BdWXVJL7P77h71XOcrda dRJcbsp9PDbBSAxMIuDRgXxYWuru1ImWDs5cYv44HOA140DKNxdadiU5kf50UvV13s5h vu1/tvxuW9Iun7o3bxLDW/UtygABqy1Jk1kmKUVsgRQYkWz9MMnKGEm0hKcRk5zKrmMp Z0+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@colorfullife-com.20150623.gappssmtp.com header.s=20150623 header.b="BUejGE/Y"; 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 s21si6814589eja.226.2019.10.11.22.50.33; Fri, 11 Oct 2019 22:51:25 -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=@colorfullife-com.20150623.gappssmtp.com header.s=20150623 header.b="BUejGE/Y"; 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 S1728636AbfJLFuM (ORCPT + 99 others); Sat, 12 Oct 2019 01:50:12 -0400 Received: from mail-wm1-f67.google.com ([209.85.128.67]:36606 "EHLO mail-wm1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728111AbfJLFuM (ORCPT ); Sat, 12 Oct 2019 01:50:12 -0400 Received: by mail-wm1-f67.google.com with SMTP id m18so11985966wmc.1 for ; Fri, 11 Oct 2019 22:50:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=colorfullife-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=hCNAY2B4GBIEKWr43sCTvg0r8VIO3WoulIoqc6vcbII=; b=BUejGE/YewtG8QRFa4GDkLjSw/OD4LH50xhdOaOYjdzPaj2XHySFdxP7PZANsQFkAF 5n2evKkK+65beOfaMT2hsDTt+iHliNx1sKU/7W/dk4dgoQwAolTotPaNW8mbmWLDy3Gz BsJjfBOAulLe4nSiKkztTTSy8zb88yHgo07H3B07JE5r1xw1iPt6H8eed2h1my1lxD3D 86aZZOUqNuHJl5wqARnTSPI+2BB7Ox2C1ahFFpq+34Mn6b/PHcvof9xUDqkWKJVnPuTX OcHaQ+IQIY8MECOsa7oOka3eJ0LlRblKJu4cS2vtRzx5nrMC8pGVEQCZhRM6Q/Vk0LyH btLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=hCNAY2B4GBIEKWr43sCTvg0r8VIO3WoulIoqc6vcbII=; b=d6MiQMwEjfDn42dfuQrmIPJhDHcbV6Fl7Ue+Hu1rI0x9VtArFGcj3FQQ9Iu71mOHUt MjSb7AjSjWsz+DFMiyecRw4+3cRDsFUMGoHHCUVYiiewf7jX26x3hfaIxnHValx+xQnx 6owitlPmp8AlZLOHTLCsx9oXSxxN534L9segRQRhAUWR2CBZJAsaPGBaam8JsLa5kf4B GDMMdRpTH59pOMttwUnlpE7QnKjXgQm36/yZGk66KLTKqlpffmm6f2XqRWutzEusWbw9 G6R96paBkLrAI023Zi5W5uP0+xm4WXDN23fTgAHbAF6XU8Fx+ADyUaerfleS16v4DnxB MQ5A== X-Gm-Message-State: APjAAAUEv95gLLcVSmCsWftXY7JNlwZJ196+vnUI8Usp6HZB/hbJ6DWf a60BmeYAKQYOjbOekoxCqUMS+9gBjcqB0Q== X-Received: by 2002:a05:600c:2549:: with SMTP id e9mr5812884wma.64.1570859409766; Fri, 11 Oct 2019 22:50:09 -0700 (PDT) Received: from linux.fritz.box (p200300D9973AD600F159A589C745B52A.dip0.t-ipconnect.de. [2003:d9:973a:d600:f159:a589:c745:b52a]) by smtp.googlemail.com with ESMTPSA id z4sm9344955wrh.93.2019.10.11.22.50.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Oct 2019 22:50:09 -0700 (PDT) From: Manfred Spraul To: LKML , Davidlohr Bueso , Waiman Long Cc: 1vier1@web.de, Andrew Morton , Peter Zijlstra , Jonathan Corbet , Manfred Spraul Subject: [PATCH 1/6] wake_q: Cleanup + Documentation update. Date: Sat, 12 Oct 2019 07:49:53 +0200 Message-Id: <20191012054958.3624-2-manfred@colorfullife.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191012054958.3624-1-manfred@colorfullife.com> References: <20191012054958.3624-1-manfred@colorfullife.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 1) wake_q_add() contains a memory barrier, and callers such as ipc/mqueue.c rely on this barrier. Unfortunately, this is documented in ipc/mqueue.c, and not in the description of wake_q_add(). Therefore: Update the documentation. Removing/updating ipc/mqueue.c will happen with the next patch in the series. 2) wake_q_add() ends with get_task_struct(), which is an unordered refcount increase. Add a clear comment that the callers are responsible for a barrier: most likely spin_unlock() or smp_store_release(). 3) wake_up_q() relies on the memory barrier in try_to_wake_up(). Add a comment, to simplify searching. 4) wake_q.next is accessed without synchroniyation by wake_q_add(), using cmpxchg_relaxed(), and by wake_up_q(). Therefore: Use WRITE_ONCE in wake_up_q(), to ensure that the compiler doesn't perform any tricks. Signed-off-by: Manfred Spraul Cc: Davidlohr Bueso --- kernel/sched/core.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index dd05a378631a..60ae574317fd 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -440,8 +440,16 @@ static bool __wake_q_add(struct wake_q_head *head, struct task_struct *task) * @task: the task to queue for 'later' wakeup * * Queue a task for later wakeup, most likely by the wake_up_q() call in the - * same context, _HOWEVER_ this is not guaranteed, the wakeup can come - * instantly. + * same context, _HOWEVER_ this is not guaranteed. Especially, the wakeup + * may happen before the function returns. + * + * What is guaranteed is that there is a memory barrier before the wakeup, + * callers may rely on this barrier. + * + * On the other hand, the caller must guarantee that @task does not disappear + * before wake_q_add() completed. wake_q_add() does not contain any memory + * barrier to ensure ordering, thus the caller may need to use + * smp_store_release(). * * This function must be used as-if it were wake_up_process(); IOW the task * must be ready to be woken at this location. @@ -486,11 +494,14 @@ void wake_up_q(struct wake_q_head *head) BUG_ON(!task); /* Task can safely be re-inserted now: */ node = node->next; - task->wake_q.next = NULL; + + WRITE_ONCE(task->wake_q.next, NULL); /* * wake_up_process() executes a full barrier, which pairs with * the queueing in wake_q_add() so as not to miss wakeups. + * The barrier is the smp_mb__after_spinlock() in + * try_to_wake_up(). */ wake_up_process(task); put_task_struct(task); -- 2.21.0