Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp2756748pxb; Tue, 13 Apr 2021 09:25:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxVXza/cJw2txBwjwrxnkM+GCg/aDtsIDfNBEThnBb14rKtViRCIGdq9LupsxWHek0ne2bh X-Received: by 2002:a17:906:8a6e:: with SMTP id hy14mr33477750ejc.356.1618331138770; Tue, 13 Apr 2021 09:25:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618331138; cv=none; d=google.com; s=arc-20160816; b=sAxmQoZBu8vulSgrNk3lOVQN43i15KneBQiorwLsEcSkVB1iQ0VcgS3vOXpxnrYYzL OC/Wj7cXPPI1M+MrCBesGADV6ZXJ9ot0OivrFcZarUUbPt48zjEKw5Or8N4f8+0DfaSM Uh685pc4V7wlPNp1RP0zxxDmyfK1ec5xuyH8udJjpBACLptTTgyxPD2nO/kBBbqno3QB iUSEJ2Wl++FJLJii57f7S4IjkCxHBLHL1defjS1gqwH2IOfOvUnTGpClji2LpkOAFcBm HJhbGkYhiMf3TLwSF0sKqkeX46HApf9snOHnGgbhYagi1fwMXmsNzmy4PGM88iihCk9G VaxA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=BKvyQwqP1CGF2nxDVwcn8Ks7rJV6J2Q0Qlvov3Bj5XM=; b=Qvy9429R3iNhPsmK6Dr+NzwBIbVWb+M/mX/mt6jk5kZ8y33yOhXnFk7b235s8+9lxK pFQaaVqWdK1lVIocmiYZVtPh8nuFQjOKB/bvATmACFBAylgmJCOPVboQjK6yEjVLbbsc YtHbRWhUtMzerxwEzautMPML5khtEd9DZYw/D4u7HkKe6EWyRBWFsoaNUhutDFgux/h7 nblEMEMibVUnm6L4P5CiLB3r15foWkpWxlubiTVv9ZgO5QaSJCrFjJEN+agH4CMVDezp +a5o4KufMFE5eWpQUt1sYyEodfbIKOn2nhjevmOXhJhaZdPhygPc4jgpnb0XJRJo0aED Y2Mw== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id yj16si10914223ejb.413.2021.04.13.09.25.15; Tue, 13 Apr 2021 09:25:38 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343709AbhDMKp1 (ORCPT + 99 others); Tue, 13 Apr 2021 06:45:27 -0400 Received: from mail.kernel.org ([198.145.29.99]:46752 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236770AbhDMKp1 (ORCPT ); Tue, 13 Apr 2021 06:45:27 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 4E2C461278; Tue, 13 Apr 2021 10:45:06 +0000 (UTC) Date: Tue, 13 Apr 2021 11:45:03 +0100 From: Catalin Marinas To: Christoph =?iso-8859-1?Q?M=FCllner?= Cc: Peter Zijlstra , Palmer Dabbelt , Anup Patel , Guo Ren , linux-riscv , Linux Kernel Mailing List , Guo Ren , will.deacon@arm.com, Arnd Bergmann Subject: Re: [PATCH] riscv: locks: introduce ticket-based spinlock implementation Message-ID: <20210413104503.GD15806@arm.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Apr 13, 2021 at 12:25:00PM +0200, Christoph M?llner wrote: > On Tue, Apr 13, 2021 at 11:37 AM Peter Zijlstra wrote: > > On Tue, Apr 13, 2021 at 11:22:40AM +0200, Christoph M?llner wrote: > > > What about trylock()? > > > I.e. one could implement trylock() without a loop, by letting > > > trylock() fail if the SC fails. > > > That looks safe on first view, but nobody does this right now. > > > > Generic code has to use cmpxchg(), and then you get something like this: > > > > bool trylock(atomic_t *lock) > > { > > u32 old = atomic_read(lock); > > > > if ((old >> 16) != (old & 0xffff)) > > return false; > > > > return atomic_try_cmpxchg(lock, &old, old + (1<<16)); /* SC, for RCsc */ > > } > > This approach requires two loads (atomic_read() and cmpxchg()), which > is not required. > Detecting this pattern and optimizing it in a compiler is quite unlikely. > > A bit less generic solution would be to wrap the LL/SC (would be > mandatory in this case) > instructions and do something like this: > > uint32_t __smp_load_acquire_reserved(void*); > int __smp_store_release_conditional(void*, uint32_t); > > typedef union { > uint32_t v32; > struct { > uint16_t owner; > uint16_t next; > }; > } arch_spinlock_t; > > int trylock(arch_spinlock_t *lock) > { > arch_spinlock_t l; > int success; > do { > l.v32 = __smp_load_acquire_reserved(lock); > if (l.owner != l.next) > return 0; > l.next++; > success = __smp_store_release_conditional(lock, l.v32); > } while (!success); > return success; > } > > But here we can't tell the compiler to optimize the code between LL and SC... This indeed needs some care. IIUC RISC-V has similar restrictions as arm here, no load/store instructions are allowed between LR and SC. You can't guarantee that the compiler won't spill some variable onto the stack. BTW, I think the SC doesn't need release semantics above, only the LR needs acquire semantics. -- Catalin