Received: by 10.192.165.148 with SMTP id m20csp1023217imm; Sat, 5 May 2018 03:13:33 -0700 (PDT) X-Google-Smtp-Source: AB8JxZruamMUXVkqy0JkuLknPcWpGgbO7YGVtnpmHFEGEdgSFun+VGmXFDM/kGSA8rab/EjhZ7fr X-Received: by 10.98.11.210 with SMTP id 79mr29917620pfl.4.1525515213409; Sat, 05 May 2018 03:13:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525515213; cv=none; d=google.com; s=arc-20160816; b=TQQgnlAZHcAWTRrREyUjLyPCzGiEA424ZgZJkdBnogdd3HWVy0Bhrd30jdfCTtWu6Q Cgn1hrANWWyQqy/sS7SzaBmdAbieQ1sfgKQxNctaubEm80VAn1KBbPlkcotithv2TSwP VTyAm8sMdqiWTK8MF0yohi+wUriGGKw1PRqnb4pGwY9mMIjlqXx/Y9U0Uk2xb/51VsCG 1QYIwn1DF7mE2z2ZRw/f057+uPI0eF5oxLz2Br3QsNG3HLCxVRcxKvnYZwc1eG9xgJkT jexPYG9ri3DHnwzMHDTSu0mrEUN2epzV7P4bdRV2gyxP7ZKK8CCuZ38Ydi/HJRfP/T/l 56Ug== 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=jDYJWjVvgIkMytdkSiJYjVLez/Fewlg1B++l5cXEBrE=; b=HN0Gc0qpUdbVtIBZVFY4adwcfSdS7w4DSVnDUxh17tLJCgcSKHnfrnfxPtzav0Ep6g j7QQBzLGU+Y/uIrLdRMdxLXq4U3qY7WmcYmVdr1Ok4fqr50OYVxOHLXz5EEbYG0agos5 Jj5yk/bR0XMKOZlyVSSdDTut19TERtFAf65aJdvTvZp4hPTptLmFWNgFn1nKA0Y9Q7Om hZ1zugUYWpd++BDpR9Amjb+YUgIeU5NGAmndPvVPb6yAWMNz0VtG39e6ceqDuZsgJAYU HrAFfg92gdRA/P4+xAFYRhA0nWZ6YuBjUbiUjXT7XE/00/bj52rUaNmJ5TagxtYs+GQM EPaA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=JeDCwogq; 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 bi10-v6si16730755plb.399.2018.05.05.03.13.16; Sat, 05 May 2018 03:13:33 -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=JeDCwogq; 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 S1751270AbeEEKLs (ORCPT + 99 others); Sat, 5 May 2018 06:11:48 -0400 Received: from mail-qk0-f174.google.com ([209.85.220.174]:41853 "EHLO mail-qk0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750821AbeEEKLq (ORCPT ); Sat, 5 May 2018 06:11:46 -0400 Received: by mail-qk0-f174.google.com with SMTP id d125so18528129qkb.8 for ; Sat, 05 May 2018 03:11:46 -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=jDYJWjVvgIkMytdkSiJYjVLez/Fewlg1B++l5cXEBrE=; b=JeDCwogqcE4wRjzW855F8MUSUalC58CbiUhjW4dDRZV3ppudUOmUD5BcR0Lzqz660j xJ4yRqkHAIoEo+oLskLmW1qKiNX7I8cgCCyd7SUFoEFg03YiwIfKu+3PLdvziTVc21Ps 8L3ELh0TP8kyWVeWDQzw4A3fzVHj1naa9zIo8VODv/3bV8PtKmZfgvgiSrB5QnesQApJ U3qluxFEispgy2CNCt4dDCsSoZA4dbq/KQBKsd/tUfc3QcXK/XIYFpsCNMIq8qhixj31 sykpp3Jg1jghV8fg2qA9A+NXvTK70fLwOZ7PknlKsRF5FGqMKpbs0+CQTCwjK9y9MROp qNNg== 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=jDYJWjVvgIkMytdkSiJYjVLez/Fewlg1B++l5cXEBrE=; b=dwptthZVHhpvvN2QRpPHGSN2Uw1YnjjQwtuxQyhAB7JnvIvPc99htuwfSOL3UIwhfc MOeyAmpLrRf4FaHDbXhfQGPcZYYzhFSyt/hOphFImlmaUcyX5LSiywR6eattT/lrQafm letQJM4UZtRz6UnPlctdxpShLsbtW+tgKsztIzH6sTYRz1mqczdszd1TLGb7bELflAdj htI5XTfpi5oKj4r+R1n0YoMAWyMggPGaWwJoKs5ZcUlZP4RoQQy6YIbA1DWf//HQi0tm nDC25abZ3CcrJJIOLk0UVcn0bS/Qsl8Et2C684wn905409n/MF0CM4h0QPYwySJmIYAG cG3A== X-Gm-Message-State: ALQs6tATPAfdbyU7jOSzTWsmJqfnthTyzlGbcBfbf0o+a3DJ82lf9ODf 4sjoDyXVC38nmpQtj8T9Qco= X-Received: by 10.55.11.65 with SMTP id 62mr24781768qkl.287.1525515105977; Sat, 05 May 2018 03:11:45 -0700 (PDT) Received: from auth2-smtp.messagingengine.com (auth2-smtp.messagingengine.com. [66.111.4.228]) by smtp.gmail.com with ESMTPSA id j42-v6sm17552377qtj.46.2018.05.05.03.11.45 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 05 May 2018 03:11:45 -0700 (PDT) Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailauth.nyi.internal (Postfix) with ESMTP id C1F6E2255D; Sat, 5 May 2018 06:11:43 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Sat, 05 May 2018 06:11:43 -0400 X-ME-Sender: Received: from localhost (unknown [45.32.128.109]) by mail.messagingengine.com (Postfix) with ESMTPA id 9076C1025D; Sat, 5 May 2018 06:11:42 -0400 (EDT) Date: Sat, 5 May 2018 18:16:09 +0800 From: Boqun Feng To: Ingo Molnar Cc: Peter Zijlstra , Mark Rutland , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, aryabinin@virtuozzo.com, catalin.marinas@arm.com, dvyukov@google.com, will.deacon@arm.com Subject: Re: [PATCH] locking/atomics: Clean up the atomic.h maze of #defines Message-ID: <20180505101609.5wb56j4mspjkokmw@tardis> References: <20180504173937.25300-1-mark.rutland@arm.com> <20180504173937.25300-2-mark.rutland@arm.com> <20180504180105.GS12217@hirez.programming.kicks-ass.net> <20180504180909.dnhfflibjwywnm4l@lakrids.cambridge.arm.com> <20180505081100.nsyrqrpzq2vd27bk@gmail.com> <20180505084721.GA32344@noisy.programming.kicks-ass.net> <20180505090403.p2ywuen42rnlwizq@gmail.com> <20180505093829.xfylnedwd5nonhae@gmail.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="46lmvgfbnthzcywx" Content-Disposition: inline In-Reply-To: <20180505093829.xfylnedwd5nonhae@gmail.com> User-Agent: NeoMutt/20171215 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org --46lmvgfbnthzcywx Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Sat, May 05, 2018 at 11:38:29AM +0200, Ingo Molnar wrote: >=20 > * Ingo Molnar wrote: >=20 > > * Peter Zijlstra wrote: > >=20 > > > > So we could do the following simplification on top of that: > > > >=20 > > > > #ifndef atomic_fetch_dec_relaxed > > > > # ifndef atomic_fetch_dec > > > > # define atomic_fetch_dec(v) atomic_fetch_sub(1, (v)) > > > > # define atomic_fetch_dec_relaxed(v) atomic_fetch_sub_relaxed(1, = (v)) > > > > # define atomic_fetch_dec_acquire(v) atomic_fetch_sub_acquire(1, = (v)) > > > > # define atomic_fetch_dec_release(v) atomic_fetch_sub_release(1, = (v)) > > > > # else > > > > # define atomic_fetch_dec_relaxed atomic_fetch_dec > > > > # define atomic_fetch_dec_acquire atomic_fetch_dec > > > > # define atomic_fetch_dec_release atomic_fetch_dec > > > > # endif > > > > #else > > > > # ifndef atomic_fetch_dec > > > > # define atomic_fetch_dec(...) __atomic_op_fence(atomic_fetch_de= c, __VA_ARGS__) > > > > # define atomic_fetch_dec_acquire(...) __atomic_op_acquire(atomic= _fetch_dec, __VA_ARGS__) > > > > # define atomic_fetch_dec_release(...) __atomic_op_release(atomic= _fetch_dec, __VA_ARGS__) > > > > # endif > > > > #endif > > >=20 > > > This would disallow an architecture to override just fetch_dec_releas= e for > > > instance. > >=20 > > Couldn't such a crazy arch just define _all_ the 3 APIs in this group? > > That's really a small price and makes the place pay the complexity > > price that does the weirdness... > >=20 > > > I don't think there currently is any architecture that does that, but= the > > > intent was to allow it to override anything and only provide defaults= where it > > > does not. > >=20 > > I'd argue that if a new arch only defines one of these APIs that's prob= ably a bug.=20 > > If they absolutely want to do it, they still can - by defining all 3 AP= Is. > >=20 > > So there's no loss in arch flexibility. >=20 > BTW., PowerPC for example is already in such a situation, it does not def= ine=20 > atomic_cmpxchg_release(), only the other APIs: >=20 > #define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n))) > #define atomic_cmpxchg_relaxed(v, o, n) \ > cmpxchg_relaxed(&((v)->counter), (o), (n)) > #define atomic_cmpxchg_acquire(v, o, n) \ > cmpxchg_acquire(&((v)->counter), (o), (n)) >=20 > Was it really the intention on the PowerPC side that the generic code fal= ls back=20 > to cmpxchg(), i.e.: >=20 > # define atomic_cmpxchg_release(...) __atomic_op_release(atomi= c_cmpxchg, __VA_ARGS__) >=20 So ppc has its own definition __atomic_op_release() in arch/powerpc/include/asm/atomic.h: #define __atomic_op_release(op, args...) \ ({ \ __asm__ __volatile__(PPC_RELEASE_BARRIER "" : : : "memory"); \ op##_relaxed(args); \ }) , and PPC_RELEASE_BARRIER is lwsync, so we map to lwsync(); atomic_cmpxchg_relaxed(v, o, n); And the reason, why we don't define atomic_cmpxchg_release() but define atomic_cmpxchg_acquire() is that, atomic_cmpxchg_*() could provide no ordering guarantee if the cmp fails, we did this for atomic_cmpxchg_acquire() but not for atomic_cmpxchg_release(), because doing so may introduce a memory barrier inside a ll/sc critical section, please see the comment before __cmpxchg_u32_acquire() in arch/powerpc/include/asm/cmpxchg.h: /* * cmpxchg family don't have order guarantee if cmp part fails, therefore = we * can avoid superfluous barriers if we use assembly code to implement * cmpxchg() and cmpxchg_acquire(), however we don't do the similar for * cmpxchg_release() because that will result in putting a barrier in the * middle of a ll/sc loop, which is probably a bad idea. For example, this * might cause the conditional store more likely to fail. */ Regards, Boqun > Which after macro expansion becomes: >=20 > smp_mb__before_atomic(); > atomic_cmpxchg_relaxed(v, o, n); >=20 > smp_mb__before_atomic() on PowerPC falls back to the generic __smp_mb(), = which=20 > falls back to mb(), which on PowerPC is the 'sync' instruction. >=20 > Isn't this a inefficiency bug? >=20 > While I'm pretty clueless about PowerPC low level cmpxchg atomics, they a= ppear to=20 > have the following basic structure: >=20 > full cmpxchg(): >=20 > PPC_ATOMIC_ENTRY_BARRIER # sync > ldarx + stdcx > PPC_ATOMIC_EXIT_BARRIER # sync >=20 > cmpxchg_relaxed(): >=20 > ldarx + stdcx >=20 > cmpxchg_acquire(): >=20 > ldarx + stdcx > PPC_ACQUIRE_BARRIER # lwsync >=20 > The logical extension for cmpxchg_release() would be: >=20 > cmpxchg_release(): >=20 > PPC_RELEASE_BARRIER # lwsync > ldarx + stdcx >=20 > But instead we silently get the generic fallback, which does: >=20 > smp_mb__before_atomic(); > atomic_cmpxchg_relaxed(v, o, n); >=20 > Which maps to: >=20 > sync > ldarx + stdcx >=20 > Note that it uses a full barrier instead of lwsync (does that stand for= =20 > 'lightweight sync'?). >=20 > Even if it turns out we need the full barrier, with the overly finegraine= d=20 > structure of the atomics this detail is totally undocumented and non-obvi= ous. >=20 > Thanks, >=20 > Ingo --46lmvgfbnthzcywx Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQEzBAABCAAdFiEEj5IosQTPz8XU1wRHSXnow7UH+rgFAlrthGYACgkQSXnow7UH +rhBBwf+O8n+9mXYGlMSnV/abToUz8WiRyzrngOJnBqwCAqnVQmEfGzMBEvh6pNU UetaAk/irO1mi30ldVlEaYODWOmL4IJS5N0WDydV8cQ6BYzBs/rlvDBCjCdmXOaL 6Rd4cVciMpwbFMOBoDfix+fRAQE4TpjC09KQNzmCb4zYsPWrhRzLPsz2to0c+sQ8 gTLYNK350kH6Xv9JV8kg4594Ef8rpzx7EE16Wt6PsPbB/PBStcxz4YIM01ZwgTeM LlouyB/Zu4uOyPYld6gnXcp3Xon0G7IBhlNLJUt1RqM8Dgw2k7gC5iwpYRxJ688D hISRfAFtyNCpkENY/9jYdnUVNNzkRg== =u+QW -----END PGP SIGNATURE----- --46lmvgfbnthzcywx--