Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp141467pxf; Tue, 30 Mar 2021 22:34:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw3tNI3Fz3+yXcUJb590xRpgPlbPjbqrNCdJpqAEp1D/248JJ0sO6CVi252zTh/vcA/6Glh X-Received: by 2002:aa7:cf17:: with SMTP id a23mr1640950edy.30.1617168881690; Tue, 30 Mar 2021 22:34:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617168881; cv=none; d=google.com; s=arc-20160816; b=LNNuHxFKvvMpNefvvPnzO9Ijh+oniZx+eJUbTUnR8RxzsScELAyJhMOlKsDwBpjRXG oUaA6eN3TfI2WpLghff/CrumrAo9Woy9K9ZV20RyfkhxdHQbvNjmvhWC9af1s3QYLB6w BL5J8xl7mfeA8uFpfdG/x/i1XzCjiZm2eBaJF0wot5a3wGCqKmSo3BDcW3kO6kwxLJcJ M9sl30kchTwCRpEL+EEU2Tajp2pznQBpSeLKiONsJ49SrbMiHqsOVPtXEIQLQCLuaKxL wNYKrHqkjvRbFLAq7Ioa7ZWFHwNZYdzWHAedIfLMXcRaUm9UcWlQ+9/41ulaNkerZHTs 1idA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:organization:message-id:date:subject:cc:to :from:dkim-signature; bh=FXgyDK8HaAZ+Nc4iB+GC6jvOWWlR3QxAnX+r+74mycw=; b=OJUvkAS36RI9pKj2r9PR8RqooXFuhEuvMX6NtPkIzi6A23ehfje3Pzx+b46Z7XCR/T p0M5dAbva0JMWBkBn2FSJRfgx+2ZLhvssMxDCcLyVltPXRIq15t0Kju47pc+IhoxxLV0 5vqMXclLeQdopwHI3vwysyZs/WL81H8WxmG6AcsvSJSMiIknIFvYw9l0zt6xSYEfaOJY CQOElG47tSrcl2I1VdMsVzkSwfQRSQXuVsD9Lv0l/OH6mzA+HrEsvfYGSZ/u2tQkBcOW MqBLbgiwf0ElWQlktvHRxP7Dcu20zygXIRGz11yztKkWCDJaab4mNiPvpIpqem1yk4Xv zp6Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="WojBFC/j"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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. [23.128.96.18]) by mx.google.com with ESMTP id m12si813858ejx.720.2021.03.30.22.34.19; Tue, 30 Mar 2021 22:34:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="WojBFC/j"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S233661AbhCaFdY (ORCPT + 99 others); Wed, 31 Mar 2021 01:33:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42590 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229959AbhCaFdQ (ORCPT ); Wed, 31 Mar 2021 01:33:16 -0400 Received: from mail-pj1-x1034.google.com (mail-pj1-x1034.google.com [IPv6:2607:f8b0:4864:20::1034]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 45A04C061574; Tue, 30 Mar 2021 22:33:16 -0700 (PDT) Received: by mail-pj1-x1034.google.com with SMTP id kk2-20020a17090b4a02b02900c777aa746fso629404pjb.3; Tue, 30 Mar 2021 22:33:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:organization:in-reply-to :references:mime-version:content-transfer-encoding; bh=FXgyDK8HaAZ+Nc4iB+GC6jvOWWlR3QxAnX+r+74mycw=; b=WojBFC/jd/BJWPXfRIuEsMiF5Z1TVSvCgju7IUpBWGuzOlLkM6KdCcNrD1HvwbeVLU sHIyEO45ILd6GHJt0ZxsOfxbhz5BqEaQBMMMTrnGwHSE/QJa8kqLTqCVmYVleuYDu60g gJfSK57NpaFWngP+Wcg7zcbhfWA/61MXwOy1wLiAMSRsr9maNjwR0nkAMOb9WHzEg3Be jko8RNKLNRZUtLaVuSVNB1CQZKj3yiL0sFZNoSw9Mfpo9dbLL1R4jicLEIGycw0vwbFu hhiB0VxcLzn4czYME/gnOEHaVGJknQgqiYWbflUwMRl+lntt8PLoFv201e+yf/3gDxCQ CkZw== 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:organization :in-reply-to:references:mime-version:content-transfer-encoding; bh=FXgyDK8HaAZ+Nc4iB+GC6jvOWWlR3QxAnX+r+74mycw=; b=VFU6DF66r6twcCzhEGFEAZRsawWZxw+7ITLGepFMxIDe9S/2p5YsnbmV5HVwlx+tHC iPlWYviIn9NUICbDqiL33nfxhss4ltGF6kI4IScqPFFT3zPJ8Xp03X+nBrldHEj3fayp QdJgr4Dj+L+ahXQxFcpNCWhKiuX7+v/0qSuV9cTKMuilldFHLyoBP4htdW40ZFdNw6u4 XiJirTFkqlGjOs+4GxdjnmMbGUuP0W27JPK7WWa+roKQHTfpeSI/ufiwJpiV/CEKbK6d uYg1C9MT6tXNnVM92E6mLnarv3KA48y5SGsasozrN4UkmPzlT4JtHt7CO85SqspNadLN VanA== X-Gm-Message-State: AOAM533mHtfNe26Bzfu+aQhpWL3976tjz+V68H0BFbPUMZ8IoIKYGbH1 C6uFlFqrCS1hhgKVwV9gP4A= X-Received: by 2002:a17:90a:fe93:: with SMTP id co19mr1810470pjb.142.1617168795740; Tue, 30 Mar 2021 22:33:15 -0700 (PDT) Received: from rata.localnet (fred.taniwha.com. [203.86.204.69]) by smtp.gmail.com with ESMTPSA id y66sm974623pgb.78.2021.03.30.22.33.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Mar 2021 22:33:15 -0700 (PDT) From: Paul Campbell To: Arnd Bergmann , linux-riscv@lists.infradead.org Cc: Peter Zijlstra , linux-riscv , Linux Kernel Mailing List , linux-csky@vger.kernel.org, linux-arch , Guo Ren , Will Deacon , Ingo Molnar , Waiman Long , Anup Patel , Sebastian Andrzej Siewior , Guo Ren Subject: Re: [PATCH v4 3/4] locking/qspinlock: Add ARCH_USE_QUEUED_SPINLOCKS_XCHG32 Date: Wed, 31 Mar 2021 18:33:07 +1300 Message-ID: <1706037.TLkxdtWsSY@rata> Organization: Moonbase Otago In-Reply-To: References: <1616868399-82848-1-git-send-email-guoren@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wednesday, 31 March 2021 5:18:56 PM NZDT Guo Ren wrote: > > > [1] > > > https://github.com/c-sky/csky-linux/commit/e837aad23148542771794d8a2fcc > > > 52afd0fcbf88> > > > > > It also seems that the current "amoswap" based implementation > > > > would be reliable independent of RsrvEventual/RsrvNonEventual. > > > > > > Yes, the hardware implementation of AMO could be different from LR/SC. > > > AMO could use ACE snoop holding to lock the bus in hw coherency > > > design, but LR/SC uses an exclusive monitor without locking the bus. > > > > > > RISC-V hasn't CAS instructions, and it uses LR/SC for cmpxchg. I don't > > > think LR/SC would be slower than CAS, and CAS is just good for code > > > size. > > > > What I meant here is that the current spinlock uses a simple amoswap, > > which presumably does not suffer from the lack of forward process you > > described. > > Does that mean we should prevent using LR/SC (if RsrvNonEventual)? Let me provide another data-point, I'm working on a high-end highly speculative implementation with many concurrent instructions in flight - from my point of view both sorts of AMO (LR/SC and swap/add/etc) require me to grab a cache line in an exclusive modifiable state (so no difference there). More importantly both sorts of AMO instructions (unlike most loads and stores) can't be speculated (not even LR because it changes hidden state, I found this out the hard way bringing up the kernel). This means that both LR AND SC individually can't be executed until all speculation is resolved (that means that they happen really late in the execute path and block the resolution of the speculation of subsequent instructions) - equally a single amoswap/add/etc instruction can't happen until late in the execute path - so both require the same cache line state, but one of these sorts of events is better than two of them. Which in short means that amoswap/add/etc is better for small things - small buzzy lock loops, while LR/SC is better for more complex things with actual processing between the LR and SC. ---- Another issue here is to consider is what happens when you hit one of these tight spinlocks when the branch target cache is empty and they fail (ie loop back and try again) - the default branch prediction, and resulting speculation, is (very) likely to be looping back, while hopefully most locks are not contended when you hit them and that speculation would be wrong - a spinlock like this may not be so good: li a0, 1 loop: amoswap a1, a0, (a2) beqz a1, loop ..... subsequent code In my world with no BTC info the pipe fills with dozens of amoswaps, rather than the 'subsequent code'. While (in my world) code like this: li a0, 1 loop: amoswap a1, a0, (a2) bnez a1, 1f .... subsequent code 1: j loop would actually be better (in my world unconditional jump instructions are folded early and never see execution so they're sort of free, though they mess with the issue/decode rate). Smart compilers could move the "j loop" out of the way, while the double branch on failure is not a big deal since either the lock is still held (and you don't care if it's slow) or it's been released in which case the cache line has been stolen and the refetch of that cache line is going to dominate the next time around the loop I need to stress here that this is how my architecture works, other's will of course be different though I expect that other heavily speculative architectures to have similar issues :-) Paul Campbell Moonbase Otago