Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp3384611pxb; Wed, 14 Apr 2021 04:27:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzZMRY+VmoW0IDEnZ5bs0wlQT2gQmbpvf4ErRc7hl/+ISfM+cycQXuWAhQXucmMH6mUgpX0 X-Received: by 2002:a17:906:ecb8:: with SMTP id qh24mr38318433ejb.162.1618399624994; Wed, 14 Apr 2021 04:27:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618399624; cv=none; d=google.com; s=arc-20160816; b=exe5rASW+0Lqo8WTbnxyWXuF7YGwLzdBe2l8x5+xdC7hFO/QLlkBdzwW7Ah6jiZRwi xb6msGCqqKo9GfLjEj59Dg3Fw3uyemYNUgt2E8q7/GDHluJaYiC0A42OWgDtuYubLLbO nZ1h51aFB44LzstNGuzLQsMDuciquoSzf2GYqaf8A5LfHDHR7BLdODWj1+ss/NnhjP/D XZa4ckue7HlwTJPWLEyHFkvt4qcIYMqw6cWljk3Piqumz0jsqE4pDptraGfUqxGXAwao jXgeHgjNcBNjyLEYTkxVubtXuuEzez2drQIyOXrfoivJ5qbguongU0/RoA6sS317wkbM aP6g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=xOchrbki+75UNK0eGQq7LwuVpILXhI2TTr8G83u5JsA=; b=kGX2c4HRZHPzrEXAOrAOPNFMDZgsEoxGyCBilRi/yejGKJa8Em3zpKcG24S8A5oEWC Z3FhAiHancs/u5+0QPpPApsGXlY60NGKPHAj4OpUBwph6fyr0mL7/f89D0t+2Ci5+2dr YvIG6ZCkkJUBDDZq3JCYGfAm/MPqiLmk6SPn+VZDckD6ornC6FR6NPAdMHvX9XYdwcCC RQJVIWzb1PwK/fFZaxgvZP4Fg9DZGo/oLleHFzYWpKHd/FFpXvIkZRaEBFW08aDVMNen Y7a3C9q61Kky/3dguswudeqB35ewTdjGwIqpMVwNyF7mcuUAaVLkFWPRbjYh0wbG+xI3 vqNA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=BHzyfHdb; 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=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r13si1116518edw.386.2021.04.14.04.26.40; Wed, 14 Apr 2021 04:27:04 -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=@kernel.org header.s=k20201202 header.b=BHzyfHdb; 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=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232241AbhDNC1e (ORCPT + 99 others); Tue, 13 Apr 2021 22:27:34 -0400 Received: from mail.kernel.org ([198.145.29.99]:48584 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231379AbhDNC1c (ORCPT ); Tue, 13 Apr 2021 22:27:32 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 68A21613D0 for ; Wed, 14 Apr 2021 02:27:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1618367232; bh=vR+xGgJ1ZvdzE9ADVoHb98+6LJMTwciDUAVKDrWvdhI=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=BHzyfHdblEjat8zBIbZgTT0PLHhMEb8vwhiy0IFBjsvH2dchUibRpAVrZM6KIdVeu jfXYuT0yymaRrKXTBjPIrIoZzHIRP3Z5eNGMgrsR97ou4KqGrMyhpNeqwon7s7Mcvg tbT8ELBtmzlmpxngU36zG+IkWbm3zZK0wQwzO3r77tq7TPpK90Ojt6cqSKIuo2+XUM ynCMaPpYYdsPzWQzFx2OX94Hh/kitZLAx7yR8F/t7UpH/toS8ZvCkbzmBGzDk8PJDK WkIo6V8prGyHnUIui/3cj0sNT82zGsyxSiF3BAtwbpOwsBm9WHpx36JRsunJPqrHJw 4Su1BFaczsHjQ== Received: by mail-lf1-f48.google.com with SMTP id w8so22084975lfr.0 for ; Tue, 13 Apr 2021 19:27:12 -0700 (PDT) X-Gm-Message-State: AOAM530l53t/+TWFZdJ13Vqk/ENJhUabTq1RTmvB5Do8O/kcWoUP2xfc q0iulUhm50fNLY+WTjcYXr/bWpxVd48S4O7fNNY= X-Received: by 2002:ac2:4e84:: with SMTP id o4mr1971646lfr.557.1618367230801; Tue, 13 Apr 2021 19:27:10 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Guo Ren Date: Wed, 14 Apr 2021 10:26:57 +0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] riscv: locks: introduce ticket-based spinlock implementation To: Peter Zijlstra Cc: =?UTF-8?Q?Christoph_M=C3=BCllner?= , Palmer Dabbelt , Anup Patel , linux-riscv , Linux Kernel Mailing List , Guo Ren , Catalin Marinas , Will Deacon , Arnd Bergmann Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Thx Peter, On Tue, Apr 13, 2021 at 4:17 PM Peter Zijlstra wrote: > > On Tue, Apr 13, 2021 at 10:03:01AM +0200, Peter Zijlstra wrote: > > > For ticket locks you really only needs atomic_fetch_add() and > > smp_store_release() and an architectural guarantees that the > > atomic_fetch_add() has fwd progress under contention and that a sub-word > > store (through smp_store_release()) will fail the SC. > > > > Then you can do something like: > > > > void lock(atomic_t *lock) > > { > > u32 val = atomic_fetch_add(1<<16, lock); /* SC, gives us RCsc */ > > u16 ticket = val >> 16; > > > > for (;;) { > > if (ticket == (u16)val) > > break; > > cpu_relax(); > > val = atomic_read_acquire(lock); > > } Should it be? for (;;) { if (ticket == (u16)val) { __atomic_acquire_fence(); break; } > > A possibly better might be: > > if (ticket == (u16)val) > return; Should it be? if (ticket == (u16)val) { __atomic_acquire_fence(); return; } > > atomic_cond_read_acquire(lock, ticket == (u16)VAL); > > Since that allows architectures to use WFE like constructs. > > > } > > > > void unlock(atomic_t *lock) > > { > > u16 *ptr = (u16 *)lock + (!!__BIG_ENDIAN__); > > u32 val = atomic_read(lock); > > > > smp_store_release(ptr, (u16)val + 1); > > } > > > > That's _almost_ as simple as a test-and-set :-) It isn't quite optimal > > on x86 for not being allowed to use a memop on unlock, since its being > > forced into a load-store because of all the volatile, but whatever. -- Best Regards Guo Ren ML: https://lore.kernel.org/linux-csky/