Received: by 2002:ac0:a581:0:0:0:0:0 with SMTP id m1-v6csp4096714imm; Mon, 25 Jun 2018 09:39:07 -0700 (PDT) X-Google-Smtp-Source: ADUXVKIk01W00821g8bRi/q6CLIc+tohv4rb5OMjQWqjVlMt12uTXCpPpNaFM6ihXAazOaBBv+TF X-Received: by 2002:a62:da07:: with SMTP id c7-v6mr13808021pfh.106.1529944747505; Mon, 25 Jun 2018 09:39:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529944747; cv=none; d=google.com; s=arc-20160816; b=AJ7SEQsHmk6mny/YOcGPdq3aUOoV1NmYtVfY+nCdFj2Atod99imOkTCGvzLeKEqL4r BWmNZQX8+khfL9iHJTOjhAVXB+z3Ea1wfpK+i9bZh/4UlOhBKfwkTxejr/Ltpobk7m/x qubuMgtG1BzIcJxc5tX6nArVm+a5po4mMbn6XhkKdloKvHymX37IIXaIOWNBY/AFwtCP /yi3ErAkxNBu2AVbDIss1laUCy1XeD+evyh6c88r9ExmJeWqMX1hCqDb/QTf7hKk53pO slX+UeKKDIDItQbLpOgoHVWmOS2CMNg+yN6qE+tlxv/f/HCDueOk7fShNn51Byl2m08x lcbg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature:arc-authentication-results; bh=eMnUeejIFi+eaZLwGbCxGh0dPP1cnNKWEKY7p6Mk9+8=; b=PG8FohB1rszSWPoxNzFDAiZjz1h1oCEbkjrcSK6RrfeVmszORU9TKUyohyMzln1aq0 9rDWU10zJRKa2xKMejyA6l2q1lW9DcLCpjwzyM1IcjESnF9tAxB3C1AwJcyGl+iKZxHj 7ddJ34TiJxj35c4n2GtlU6I4aTOXfmjlSwNH1WIVhEU6Q+7gFUaTigFd9fLCiH/hLy8m V6bUHRnTmjHYnJt88kVYcrD5idnCknDnMjHxSTOxFlO4WY7gnT1ybD5SPeUM3U/XpBgS 7itkjFANHWf/jatrnpm0GhBXocs+amMw6+kaCfUgqf8Zzq4i8qDaLKLMY2omHaThsrxU lGuA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=Qb9TX39S; 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 q2-v6si5586661pli.86.2018.06.25.09.38.32; Mon, 25 Jun 2018 09:39:07 -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=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=Qb9TX39S; 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 S933624AbeFYQhY (ORCPT + 99 others); Mon, 25 Jun 2018 12:37:24 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:43674 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932438AbeFYQhW (ORCPT ); Mon, 25 Jun 2018 12:37:22 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.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:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=eMnUeejIFi+eaZLwGbCxGh0dPP1cnNKWEKY7p6Mk9+8=; b=Qb9TX39SdDViDOEIz548EQ8XH WzdWHUDPlWOI10jdd9jInWpp9yRsp9L1jDzt+oGQsIX3bFBcapmJhfVNXSwyjaK5bXgIooN2S1CyU yHBicuKCTeCJRC9cUKflotSWpX98AteLrRmHAf4o+hgs8dxv+WOiFMlqnyBMBr2apG5KPjN78QdMd gYklW2wIPgoaKnd3hYQNmowxpXVSKmJwJQAGiZs51gnqzqFg6Og4dU/xT3F0XUYcpVCCLGr14jvUQ umVyGabIBX91wxtrwCg/Nb+zCgnianfyvjVERiERzpSTVVY8oX/f8/SQoCA8w4W748C4I5Z53LAPH hN7ASS5hA==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=hirez.programming.kicks-ass.net) by bombadil.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1fXUUF-0003aq-LI; Mon, 25 Jun 2018 16:37:07 +0000 Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 621C42029FA0A; Mon, 25 Jun 2018 18:37:05 +0200 (CEST) Date: Mon, 25 Jun 2018 18:37:05 +0200 From: Peter Zijlstra To: Andrea Parri Cc: linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, Alan Stern , Will Deacon , Boqun Feng , Nicholas Piggin , David Howells , Jade Alglave , Luc Maranget , "Paul E. McKenney" , Akira Yokosawa , Daniel Lustig , Jonathan Corbet , Ingo Molnar , Randy Dunlap Subject: Re: [PATCH] doc: Update wake_up() & co. memory-barrier guarantees Message-ID: <20180625163705.GE2494@hirez.programming.kicks-ass.net> References: <1529918258-7295-1-git-send-email-andrea.parri@amarulasolutions.com> <20180625095031.GX2494@hirez.programming.kicks-ass.net> <20180625105618.GA12676@andrea> <20180625123121.GY2494@hirez.programming.kicks-ass.net> <20180625131643.GA15126@andrea> <20180625141830.GC2494@hirez.programming.kicks-ass.net> <20180625145611.GA16333@andrea> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180625145611.GA16333@andrea> User-Agent: Mutt/1.10.0 (2018-05-17) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jun 25, 2018 at 04:56:11PM +0200, Andrea Parri wrote: > Ah, before sending v2, I'd really appreciate some comments on the XXXs > I've added to wait_woken() as I'm not sure I understand the pattern in > questions. Oh man, lemme see if I can remember how all that was supposed to work. So the basic idea was that we cannot rely on the normal task->state rules because testing @condition can schedule itself. So instead we add more state. But then we need to ensure that if we either don't loose a wake or loose the wakeup state. > For example, the second comment says: > > /* > * The below implies an smp_mb(), it too pairs with the smp_wmb() from > * woken_wake_function() such that we must either observe the wait > * condition being true _OR_ WQ_FLAG_WOKEN such that we will not miss > * an event. > */ > > From this I understand: > > wq_entry->flags &= ~WQ_FLAG_WOKEN; condition = true; > smp_mb() // B smp_wmb(); // C > [next iteration of the loop] wq_entry->flags |= WQ_FLAG_WOKEN; > if (condition) > break; > > BUG_ON(!condition && !(wq_entry->flags & WQ_FLAG_WOKEN)) Right, basically if we get a spurious wakeup and our ttwu() 'fails', we must either see condition on the next iteration, or ensure the next iteration doesn't sleep, so we'll loop around and test condition yet again. > IOW, this is an R-like pattern: if this is the case, the smp_wmb() does > _not_ prevent the BUG_ON() from firing; according to LKMM (and powerpc) > a full barrier would be needed. Hmmm, how come? store-store collision? Yes I suppose you're right. > Same RFC for the first comment: > > /* > * The above implies an smp_mb(), which matches with the smp_wmb() from > * woken_wake_function() such that if we observe WQ_FLAG_WOKEN we must > * also observe all state before the wakeup. > */ > > What is the corresponding snippet & BUG_ON()? The comment there suggest: if (condition) break; set_current_state(UNINTERRUPTIBLE); condition = true; /* smp_mb() */ smp_wmb(); wq_entry->flags |= WQ_FLAG_WOKEN; if (!wq_entry->flags & WQ_FLAG_WOKEN) schedule(); BUG_ON((wq_entry->flags & WQ_FLAG_WOKEN) && !condition); But looking at that now, I think that's wrong. Because the the purpose was that, if we don't do the try_to_wake_up(), our task still needs to observe the condition store. But for that we need a barrier between the wq_entry->flags load and the second condition test, which would (again) be B, not A.