Received: by 10.213.65.16 with SMTP id m16csp238705imf; Mon, 12 Mar 2018 01:53:55 -0700 (PDT) X-Google-Smtp-Source: AG47ELuNqlqZ+SkUzjrsS0NokVInnN4uup86hvgjQ/iV2iZt14bLqb4zrPUa+qaM+12kL2hKkq9+ X-Received: by 10.99.110.201 with SMTP id j192mr6045045pgc.59.1520844835627; Mon, 12 Mar 2018 01:53:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1520844835; cv=none; d=google.com; s=arc-20160816; b=xk7wY53w2Bug0Q2mq4YaCCl/QolXhnNLab57UvZGh8EJANluRmqTNB1XoswawOvqXL V5ByyNyNCKPlrfdcU5vEs5wvsbEgJv/i6w1tkWcbj5GoC/kz8LQpLbmTkUR3Wc8fTPXN rK98uGpibAbH+9Y3ZtwZ2Pz0B6x9UEZeuepK12fak5VIFkUdo78VvYbD5A5WQbBm0Wz/ wxjpy8kcioMtIXTI4HubQy9u4DChDEcmC6Twhe5PeePPAP2fDsSkfQEvZQt2dXQ5pXQJ jpoaxMsgUN43C1B/I5OdwJ+AEm6po9FfMkNtJwB18sTco21H6KTIWvQiH8HxDFpKi14l e5pQ== 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=lCiTMAjzRCX4dz4lMuo37uqQGcj/zzAeAGe0gOHLcpo=; b=tNK60MBYGBEv9bo1Tgt2kiNfU4+KCXZaPtOe7yRyzKFwFaxKOZCrdXhzatOHc6dkjs nFK4TEao0FsNQ1GZ3FNnPTarNvH2Mr/yu2gUecdvSmCA5MxyeWsTov2/WsvLl23Tbeuc ACP2NEXgfAQ1myyTLBy4gq2QEj3JwuyWihBiIlyhmNbFRqeaCurDJHesp0TnIDW+0Bpb hrX2GDMiMrTEiQRCm3QwqjVbJ4nd/4VneH+fIqm78yV1FIDKUB0yWmoVSV3Q5OQLExja 6DkMcv2OM9eTOSpY8f/UnmdX9tZ6HkdonOWzhKXqoVsQZBSLk7vwR5mNVYJteSkTtgmi UYmg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=iIomNDYs; 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 t79si5413414pfe.243.2018.03.12.01.53.41; Mon, 12 Mar 2018 01:53:55 -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=iIomNDYs; 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 S932244AbeCLIwY (ORCPT + 99 others); Mon, 12 Mar 2018 04:52:24 -0400 Received: from mail-wm0-f46.google.com ([74.125.82.46]:38766 "EHLO mail-wm0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932205AbeCLIwU (ORCPT ); Mon, 12 Mar 2018 04:52:20 -0400 Received: by mail-wm0-f46.google.com with SMTP id z9so14397303wmb.3 for ; Mon, 12 Mar 2018 01:52:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=lCiTMAjzRCX4dz4lMuo37uqQGcj/zzAeAGe0gOHLcpo=; b=iIomNDYs5VAdb7ASVG0kvlmKwwwH02HIkpiLcr0OKU1tnXFghjJkNXvBaPs53zDdMD j39/4TLmzj6u/aIMsMzsjRpBTZcOWiE63df8q/L05K+QVAQKHym8/jbY+zXnUlbyuIQm R9Oc11u1iV5q4OGdSZeAR0IqUOZC5X+MXYransXt+7CXXAfIe1JYxmyNkmPtjGhN5mtL Vh9U3TXgIgHQZJDsY+EXt2pX6YtPj7TLOVgRcn6k2o/+LU5H2M3+Tsd4i5qBkjvY7AYM DE9rk/TpNvHFXFLJNAQy1PdQjNlW/+chYDmP2FpUfZ8KVZEraPVSLFvu3O+w2Z52EVL3 j0qw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=lCiTMAjzRCX4dz4lMuo37uqQGcj/zzAeAGe0gOHLcpo=; b=cC4xYxjd9korjLM5Wif15zYleSs2QzLKurMmryMHbXCPDomlPQVtkKtvkoBj1XGbX6 6jzmuK9CabiqSw2hxVuEpKKIilyUYBbhbDSsH3Y918wDX5Qi5gMlWPvIC/LoqJVC2i+O LG//oXFtxGEvvSOS5999hZLISyp4VuX9RDN74xXFOA3i8fc6QRbENUnzIoNXZc2bIN2B jhnlP8cUYNA9GSxt3pi4tS5ekt2An6Y/hDaW/45LuOrdnKLU00amQv870P8vbYx5cnhu daOa+UHHQooypxI1oTMy66Cfd1y7FCrLG3I8xke+2nKKnKmjlSSwg5JYbX5gPP8OIfS0 eOMg== X-Gm-Message-State: AElRT7EkWgoxeoaQAeELESOjYBAKQQ+Ny4UXxZm80Nt9+RXISdWVMsPI XB5o+3R2p2/2OapDU4S3+j8= X-Received: by 10.80.136.85 with SMTP id c21mr9713196edc.259.1520844739569; Mon, 12 Mar 2018 01:52:19 -0700 (PDT) Received: from auth1-smtp.messagingengine.com (auth1-smtp.messagingengine.com. [66.111.4.227]) by smtp.gmail.com with ESMTPSA id m1sm4767069edd.75.2018.03.12.01.52.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 12 Mar 2018 01:52:18 -0700 (PDT) Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailauth.nyi.internal (Postfix) with ESMTP id E018320D3F; Mon, 12 Mar 2018 04:52:15 -0400 (EDT) Received: from frontend2 ([10.202.2.161]) by compute6.internal (MEProxy); Mon, 12 Mar 2018 04:52:15 -0400 X-ME-Sender: Received: from localhost (unknown [45.32.128.109]) by mail.messagingengine.com (Postfix) with ESMTPA id E7B50240B6; Mon, 12 Mar 2018 04:52:14 -0400 (EDT) Date: Mon, 12 Mar 2018 16:56:00 +0800 From: Boqun Feng To: =?utf-8?B?54Sm5pmT5Yas?= 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 Subject: Re: smp_mb__after_spinlock requirement too strong? Message-ID: <20180312085600.aczjkpn73axzs2sb@tardis> References: <20180312054412.yqyde34ly3kjoajj@tardis> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="lwj64r2euudstwu4" Content-Disposition: inline In-Reply-To: User-Agent: NeoMutt/20171215 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org --lwj64r2euudstwu4 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Mon, Mar 12, 2018 at 04:18:00PM +0800, =E7=84=A6=E6=99=93=E5=86=AC wrote: > >> Peter pointed out in this patch https://patchwork.kernel.org/patch/977= 1921/ > >> that the spinning-lock used at __schedule() should be RCsc to ensure > >> visibility of writes prior to __schedule when the task is to be migrat= ed 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 aga= inst 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 b= y other > >> * CPUs in spin-loops, without barriers, while being subject to schedu= ling. > >> * > >> * CPU0 CPU1 CPU2 > >> * > >> * for (;;) { > >> * if (READ_ONCE(X)) > >> * break; > >> * } > >> * X=3D1 > >> * > >> * > >> * r =3D X; > >> * > >> * without transitivity it could be that CPU1 observes X!=3D0 breaks t= he loop, > >> * we get migrated and CPU2 sees X=3D=3D0. > >> > >> 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 >=20 > Thank you for pointing out this case, Boqun. > But this is just one special case that acquire-release chains promise us. >=20 Ah.. right, because of A-Cumulative. > A=3DB=3D0 as initial >=20 > CPU0 CPU1 CPU2 CPU3 > write A=3D1 > read A=3D1 > write B=3D1 > release X > acquire X > read A=3D? > release Y >=20 > acquire Y >=20 > read B=3D? >=20 > assurance 1: CPU3 will surely see B=3D1 writing by CPU1, and > assurance 2: CPU2 will also see A=3D1 writing by CPU0 as a special case >=20 > The second assurance is both in theory and implemented by real hardware. >=20 > 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: >=20 > 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. >=20 > at > $1.10 rule 18, on page 14 > http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4296.pdf >=20 > 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: >=20 > A Tutorial Introduction to the ARM and POWER Relaxed Memory Models > https://www.cl.cam.ac.uk/~pes20/ppc-supplemental/test7.pdf >=20 > So, I think we may remove RCsc from smp_mb__after_spinlock which is > really confusing. >=20 So I think the purpose of smp_mb__after_spinlock() is to provide RCsc locks, it's just the comments before that may be misleading. We want RCsc locks in schedule code because we want writes in different critical section are ordered even outside the critical sections, for case like: CPU 0 CPU 1 CPU 2 {A =3D0 , B =3D 0} lock(rq0); write A=3D1; unlock(rq0); lock(rq0); read A=3D1; write B=3D2; unlock(rq0); read B=3D2; smp_rmb(); read A=3D1; I think we need to fix the comments rather than loose the requirement. Peter? Regards, Boqun > Best Regards, > Trol >=20 > > > >> 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? --lwj64r2euudstwu4 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQEzBAABCAAdFiEEj5IosQTPz8XU1wRHSXnow7UH+rgFAlqmQJ0ACgkQSXnow7UH +rg8RwgAnSFCdriWAMFnxMZwYCxsiirdmKfwiJk5bwGpYg7RD0B8drkCUsdBwnSm YbTSQcNB+0djhCnZ5u3T2NM5W3xn7b1pcdzMuQjopY5KDgh0xVhf9npBM40z9Oos JxM5xgtZ0VsU6dybqcM2zNyWKWv/YbDisL+otgWev185/78RBMcjfpVtFoq9SlST xgYH7/+8KeJXJhuOIqBB3w1WHCzqnCUxSXM992TIf25hDIHMiUOGCZS4WBjlwLRf gnk9L+/G9Zm9dB9ObMb/66AZyXt79J3qvxWwjzGoAFZkeLreTKG4Ki7yGPbV1VOK VXD2+8LyXDBO4l33JddsjxWUWOM64w== =E2BK -----END PGP SIGNATURE----- --lwj64r2euudstwu4--