Received: by 10.213.65.16 with SMTP id m16csp227176imf; Mon, 12 Mar 2018 01:19:40 -0700 (PDT) X-Google-Smtp-Source: AG47ELvTuzMqZnqt3qQO4IxlUSVJ1UT1wbVfGBCvzEVntAQow/A/vJksBTQ+BTH6RbEt6zES50CV X-Received: by 10.99.120.13 with SMTP id t13mr6044599pgc.35.1520842780121; Mon, 12 Mar 2018 01:19:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1520842780; cv=none; d=google.com; s=arc-20160816; b=OUv0dAa2xByXaEDavHXtYUfbRiJseYu31JM1DCYBaLuGEAy2WkDeGd2fpkzOL3kzml PnefpvUWAJqzt49fEjfJGde2OgBZ3caz0n5TpX8ZoYo48dxpakxTfxpY8+UwWbMpik6T 7HailBbXcaG/LdlM3lAAYAsck+MEP1ykCWQE25LtpH+UrDW2nK7keELaM7CyKtj0C8IC PBdQTrMjhnddKMlsoJ5Z9TXGAc2OeEEvspDf+aIM7oB+rdxb9fjdAd12qnp832mQhgzQ DOnnCTHWjE+iaSVDp6KMGfnTnXz5ZW1Ce9uRmBljscq3tr8ABu/7lOo67hu/z82+M+uY Lfvg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature :arc-authentication-results; bh=RkifaUM3Ko7Fc8LoC/qTiNdGfcvXkG6GXZdwM9TKYO8=; b=hN30AUbJCaCP3LlOQdCj3SSqu6DaifvlXD1qdKYjkq06x8myZ0y0gupNo7qCEmxPUR VP5B3Szdxggro5ogOzGYf5M1urpLBRynUfkNOq6hskrISbI6CLXHuIUJg3KiXr6gvEmZ 9YU2WdYaOUsWU3bgGJF3Wyu19PBUG0vPuuFJt81YKpspnxzeX55mXiCET0m0HJt2jG/6 b0DpOpRqhsh1qZIIIA9C6DV6bnxP/sC08h3/poql199aVbSPSmb1mdzslnZu7i14aHI5 E+Dwrl55neDq9q1wXH6uzfG9KxBFU6ye8zVP8wuBqU6pRqXK+EeB89FSjnwHPozQk5H6 Dn6Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=J8CzPV+u; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x190si2236699pgx.159.2018.03.12.01.19.25; Mon, 12 Mar 2018 01:19:40 -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=@gmail.com header.s=20161025 header.b=J8CzPV+u; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751787AbeCLISE (ORCPT + 99 others); Mon, 12 Mar 2018 04:18:04 -0400 Received: from mail-oi0-f68.google.com ([209.85.218.68]:41175 "EHLO mail-oi0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750752AbeCLISC (ORCPT ); Mon, 12 Mar 2018 04:18:02 -0400 Received: by mail-oi0-f68.google.com with SMTP id g5so11590419oiy.8 for ; Mon, 12 Mar 2018 01:18:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=RkifaUM3Ko7Fc8LoC/qTiNdGfcvXkG6GXZdwM9TKYO8=; b=J8CzPV+u/hMS8o7I1STzgvhKaUxm6/0Vd1scYtu4jwkriusM2+7bCS89E7ABC/He6h z7p9D3u2eGGsYiDFkpwGasqrHBiSVUo/MQcmenP/yN//7npQtXIBRJ+Dr1kv71irkHHP 1383OMwJguV+Rf5pVd8ctCJXUnsIvwNScSug6Dv6SAulnUre0/fvb/+khip9eqjIkrXB ppnB7xw2Mfa8wKDLn1nZR5iGbKAmXcOKtYewN4cp1K1mBZXmx9vUlnetvm0XVJmsYpfX 3IXrmkbB8BU8/qi6Xboy1wcWHe3rpNtMjuwJ+07rhO5+gLWF+dKkwo6d9ujVGZPVya4+ y2TA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=RkifaUM3Ko7Fc8LoC/qTiNdGfcvXkG6GXZdwM9TKYO8=; b=ikQ/VAxpRzdylTsJnyQQ8A1c5bLhaDYdIVDEiukknIq8ZUjj9aJfJHkIBEA2ZxDJr9 El8bpLxGvTkWJJtxtfvX4ZKqGZ4IIOCrjU6u/j69LNpsve5wRDBciO1fG/K2yzBP221Z zEyQu2WblLN8DSBCLgMQS7pdJkyUIMh4nN//U1lY6TeZsjMJrcdrWRpRD9TnpaEmbuZ6 LXyCJzgB8aClxYnPqzhknmrtqPJC+8mPzjlV1TUHoaHDe+lx4GOpXmlHnlNxbNAS7xwW w2ONOMWlQrCEcPJ0LIIPKocgm+xN8n8kYbQq2oFR1qfTpuJFkjmIxayBB9WEWEsCb6PR KbsA== X-Gm-Message-State: AElRT7HCtiwyB0QqYraFkiB4b1NRrZOQ7/KepPP/5jk2HQ/ssFkOzDfH 0sSh9oWIJhS/J9J/ThQa/Fv4/YK7aVmYaNwFEU8= X-Received: by 10.202.213.5 with SMTP id m5mr4147889oig.134.1520842681378; Mon, 12 Mar 2018 01:18:01 -0700 (PDT) MIME-Version: 1.0 Received: by 10.74.151.230 with HTTP; Mon, 12 Mar 2018 01:18:00 -0700 (PDT) In-Reply-To: <20180312054412.yqyde34ly3kjoajj@tardis> References: <20180312054412.yqyde34ly3kjoajj@tardis> From: =?UTF-8?B?54Sm5pmT5Yas?= Date: Mon, 12 Mar 2018 16:18:00 +0800 Message-ID: Subject: Re: smp_mb__after_spinlock requirement too strong? To: Boqun Feng Cc: linux-kernel@vger.kernel.org, peterz@infradead.org, Alan Stern , will.deacon@arm.com, torvalds@linux-foundation.org, npiggin@gmail.com, mingo@kernel.org, mpe@ellerman.id.au, oleg@redhat.com, benh@kernel.crashing.org, Paul McKenney Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org >> Peter pointed out in this patch https://patchwork.kernel.org/patch/9771921/ >> that the spinning-lock used at __schedule() should be RCsc to ensure >> visibility of writes prior to __schedule when the task is to be migrated to >> another CPU. >> >> And this is emphasized at the comment of the newly introduced >> smp_mb__after_spinlock(), >> >> * This barrier must provide two things: >> * >> * - it must guarantee a STORE before the spin_lock() is ordered against a >> * LOAD after it, see the comments at its two usage sites. >> * >> * - it must ensure the critical section is RCsc. >> * >> * The latter is important for cases where we observe values written by other >> * CPUs in spin-loops, without barriers, while being subject to scheduling. >> * >> * CPU0 CPU1 CPU2 >> * >> * for (;;) { >> * if (READ_ONCE(X)) >> * break; >> * } >> * X=1 >> * >> * >> * r = X; >> * >> * without transitivity it could be that CPU1 observes X!=0 breaks the loop, >> * we get migrated and CPU2 sees X==0. >> >> which is used at, >> >> __schedule(bool preempt) { >> ... >> rq_lock(rq, &rf); >> smp_mb__after_spinlock(); >> ... >> } >> . >> >> If I didn't miss something, I found this kind of visibility is __not__ >> necessarily >> depends on the spinning-lock at __schedule being RCsc. >> >> In fact, as for runnable task A, the migration would be, >> >> CPU0 CPU1 CPU2 >> >> >> >> lock(rq0) >> schedule out A >> unock(rq0) >> >> lock(rq0) >> remove A from rq0 >> unlock(rq0) >> >> lock(rq2) >> add A into rq2 >> unlock(rq2) >> lock(rq2) >> schedule in A >> unlock(rq2) >> >> >> >> happens-before >> unlock(rq0) happends-before >> lock(rq0) happends-before >> unlock(rq2) happens-before >> lock(rq2) happens-before >> >> > > But without RCsc lock, you cannot guarantee that a write propagates to > CPU 0 and CPU 2 at the same time, so the same write may propagate to > CPU0 before but propagate to CPU 2 after > . So.. > > Regards, > Boqun Thank you for pointing out this case, Boqun. But this is just one special case that acquire-release chains promise us. A=B=0 as initial CPU0 CPU1 CPU2 CPU3 write A=1 read A=1 write B=1 release X acquire X read A=? release Y acquire Y read B=? assurance 1: CPU3 will surely see B=1 writing by CPU1, and assurance 2: CPU2 will also see A=1 writing by CPU0 as a special case The second assurance is both in theory and implemented by real hardware. As for theory, the C++11 memory model, which is a potential formal model for kernel memory model as http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0124r4.html descripes, states that: If a value computation A of an atomic object M happens before a value computation B of M, and A takes its value from a side effect X on M, then the value computed by B shall either be the value stored by X or the value stored by a side effect Y on M, where Y follows X in the modification order of M. at $1.10 rule 18, on page 14 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4296.pdf As for real hardware, Luc provided detailed test and explanation on ARM and POWER in 5.1 Cumulative Barriers for WRC on page 19 in this paper: A Tutorial Introduction to the ARM and POWER Relaxed Memory Models https://www.cl.cam.ac.uk/~pes20/ppc-supplemental/test7.pdf So, I think we may remove RCsc from smp_mb__after_spinlock which is really confusing. Best Regards, Trol > >> And for stopped tasks, >> >> CPU0 CPU1 CPU2 >> >> >> >> lock(rq0) >> schedule out A >> remove A from rq0 >> store-release(A->on_cpu) >> unock(rq0) >> >> load_acquire(A->on_cpu) >> set_task_cpu(A, 2) >> >> lock(rq2) >> add A into rq2 >> unlock(rq2) >> >> lock(rq2) >> schedule in A >> unlock(rq2) >> >> >> >> happens-before >> store-release(A->on_cpu) happens-before >> load_acquire(A->on_cpu) happens-before >> unlock(rq2) happens-before >> lock(rq2) happens-before >> >> >> So, I think the only requirement to smp_mb__after_spinlock is >> to guarantee a STORE before the spin_lock() is ordered >> against a LOAD after it. So we could remove the RCsc requirement >> to allow more efficient implementation. >> >> Did I miss something or this RCsc requirement does not really matter?