Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp1257745pxf; Fri, 19 Mar 2021 03:05:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwN9WCWt7mPuodgNDJMEzM1V8CoBps6dkcBSutuwEW2KFVCWj3yjl0mvuzUqtosU34ZGxyD X-Received: by 2002:a17:906:4ada:: with SMTP id u26mr3396551ejt.129.1616148328329; Fri, 19 Mar 2021 03:05:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616148328; cv=none; d=google.com; s=arc-20160816; b=mPGtmYlv+ZfZqVkS1oZoGo8d0yWGHIRNXg43jIbCThdWvJHiDI2kQ8yIOtRfLMd8Jy cIbNfub5zMwkoFMPZ2ain2Re4IZBViYLkoqOj5otpGnDadLMmT4NWZEVEk7yQ/oPZJED Y3owhqUSL/EtcGllXADHGInXKu/L2DyFX2MTmjhcq+F3gX6BBQk5+pZzNFqPyoQqRdrx PZHZIvGoaA+WHoXXaxK81JE9NCRZl/rrEHjc366n/65mRgUaqum9ouOQG8kvUpX1VNPw v5fAzZfmxUxZ1QLA1azPIpwvQa1M2WFAO8jKSEwY2JZR2BLq0u0dEsAe3dDxXvOVngfu oquQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=L8AAIWNPQ1dDcNSKWQN4i2g9ce1CACsE0X8GRkSHj90=; b=qpbE6f3d9PYmtHrCNp6kt6c8MXv+fFNHuoXY9v0lPi+giflf9tLwV/vV9mRhR7KIMU RSpJ7lZdJvZQBg0qgCwFP8xYqBQN6gDxB+xnjyVL+H9+VXX3aEz1+3aiVkuKOgZURqZg 1Pv0rP+vf/TnbEr+UpJjNbLCnYjkFRz+ieo+AtDz3BjOrrPw/0/p07DxMIalGZA9usSQ lojKac1wD4yjO4i/VQ3bfvaxlBCCFg9m0TIuRRmnU+xIB8zw3D5QBntYPA7k8sVApnWu g0lEWbiJ3+kHpU0yUz8osZD7nLJLQFhK+xNmlA+sLCDwEUFUL8V8z4TtCcCkw0QcAr0V FuJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Pcm6wU40; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id kk8si3636674ejc.404.2021.03.19.03.05.05; Fri, 19 Mar 2021 03:05:28 -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=@google.com header.s=20161025 header.b=Pcm6wU40; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230100AbhCSKCK (ORCPT + 99 others); Fri, 19 Mar 2021 06:02:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54030 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229954AbhCSKB5 (ORCPT ); Fri, 19 Mar 2021 06:01:57 -0400 Received: from mail-qv1-xf49.google.com (mail-qv1-xf49.google.com [IPv6:2607:f8b0:4864:20::f49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3DA00C06174A for ; Fri, 19 Mar 2021 03:01:57 -0700 (PDT) Received: by mail-qv1-xf49.google.com with SMTP id u6so22671389qvu.11 for ; Fri, 19 Mar 2021 03:01:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=L8AAIWNPQ1dDcNSKWQN4i2g9ce1CACsE0X8GRkSHj90=; b=Pcm6wU40nGG0YhjEol37j/ewblPwYvFhlLgWN91mTyD0kxTOBsq2a59qIxLHbYVy6G B5zS167jRT9oi0j2bp/x41+NrBkyHy86UU9pc6G+oANJZRyVzvlshJVeIl5l5TkevLD0 sbEu3TjHOIE55q7qxkgo+UKMOn959rWeI5xcWwZA6c1ttJD4tn+nSP7cwtKkEsO38SY5 n8c8Brt/uDvVt6+Ff+M35Q6fob9GMGj4Az+1Ri0hWLJWagZiq9pcPr4VDzYT3QkGwT+j 0yVyXH8ScOcsxF4/zeAalRj93GZonUpB37oUmmwGa8KWY3dqA6Xt8jb4RksP7LcGrTE0 uzxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=L8AAIWNPQ1dDcNSKWQN4i2g9ce1CACsE0X8GRkSHj90=; b=QzQE54dFZG1BO5DGBvZ9/7UEEQoRcP+W9bVYmes9Eh9lbGVvD3c8MTlUTW1b/P6RtU buPvEMuftMmAMOpJQh5JBeqk1azxD/HH/1ozE+twUcaVQSoqzI6Tjx/ismSMH49qDXAQ k3juToNzX0UIiFYyUOgf/rGic0vtIgeLfewP3hXReq+xu0UD1QODZj0ttIH0UywZ/hv3 N8pZzlN9w0GXbn4hwDpu8h5idsiVagV1iV91ZkVFftxwpzrbNOub66pxi0YwVeD/4l18 +K630dqvrblTYrSYV+O+BhXXRDlcAZSFvjftKndtavK4FWPiDhs84Pg9p+U/TDXeTm0c ZDuw== X-Gm-Message-State: AOAM531hL+VGgupxJuQXQG56kItPKFL9INIvjh7Ki98XdFCExvWT+Cdc SsHHeYrh2HA/QcmYfsVchgbPlaSYM0g0 X-Received: from r2d2-qp.c.googlers.com ([fda3:e722:ac3:10:28:9cb1:c0a8:1652]) (user=qperret job=sendgmr) by 2002:ad4:4ab0:: with SMTP id i16mr8495782qvx.1.1616148116390; Fri, 19 Mar 2021 03:01:56 -0700 (PDT) Date: Fri, 19 Mar 2021 10:01:11 +0000 In-Reply-To: <20210319100146.1149909-1-qperret@google.com> Message-Id: <20210319100146.1149909-4-qperret@google.com> Mime-Version: 1.0 References: <20210319100146.1149909-1-qperret@google.com> X-Mailer: git-send-email 2.31.0.rc2.261.g7f71774620-goog Subject: [PATCH v6 03/38] arm64: kvm: Add standalone ticket spinlock implementation for use at hyp From: Quentin Perret To: catalin.marinas@arm.com, will@kernel.org, maz@kernel.org, james.morse@arm.com, julien.thierry.kdev@gmail.com, suzuki.poulose@arm.com Cc: android-kvm@google.com, seanjc@google.com, mate.toth-pal@arm.com, linux-kernel@vger.kernel.org, robh+dt@kernel.org, linux-arm-kernel@lists.infradead.org, kernel-team@android.com, kvmarm@lists.cs.columbia.edu, tabba@google.com, ardb@kernel.org, mark.rutland@arm.com, dbrazdil@google.com, qperret@google.com Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Will Deacon We will soon need to synchronise multiple CPUs in the hyp text at EL2. The qspinlock-based locking used by the host is overkill for this purpose and relies on the kernel's "percpu" implementation for the MCS nodes. Implement a simple ticket locking scheme based heavily on the code removed by commit c11090474d70 ("arm64: locking: Replace ticket lock implementation with qspinlock"). Signed-off-by: Will Deacon Signed-off-by: Quentin Perret --- arch/arm64/kvm/hyp/include/nvhe/spinlock.h | 92 ++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 arch/arm64/kvm/hyp/include/nvhe/spinlock.h diff --git a/arch/arm64/kvm/hyp/include/nvhe/spinlock.h b/arch/arm64/kvm/hyp/include/nvhe/spinlock.h new file mode 100644 index 000000000000..76b537f8d1c6 --- /dev/null +++ b/arch/arm64/kvm/hyp/include/nvhe/spinlock.h @@ -0,0 +1,92 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * A stand-alone ticket spinlock implementation for use by the non-VHE + * KVM hypervisor code running at EL2. + * + * Copyright (C) 2020 Google LLC + * Author: Will Deacon + * + * Heavily based on the implementation removed by c11090474d70 which was: + * Copyright (C) 2012 ARM Ltd. + */ + +#ifndef __ARM64_KVM_NVHE_SPINLOCK_H__ +#define __ARM64_KVM_NVHE_SPINLOCK_H__ + +#include +#include + +typedef union hyp_spinlock { + u32 __val; + struct { +#ifdef __AARCH64EB__ + u16 next, owner; +#else + u16 owner, next; +#endif + }; +} hyp_spinlock_t; + +#define hyp_spin_lock_init(l) \ +do { \ + *(l) = (hyp_spinlock_t){ .__val = 0 }; \ +} while (0) + +static inline void hyp_spin_lock(hyp_spinlock_t *lock) +{ + u32 tmp; + hyp_spinlock_t lockval, newval; + + asm volatile( + /* Atomically increment the next ticket. */ + ARM64_LSE_ATOMIC_INSN( + /* LL/SC */ +" prfm pstl1strm, %3\n" +"1: ldaxr %w0, %3\n" +" add %w1, %w0, #(1 << 16)\n" +" stxr %w2, %w1, %3\n" +" cbnz %w2, 1b\n", + /* LSE atomics */ +" mov %w2, #(1 << 16)\n" +" ldadda %w2, %w0, %3\n" + __nops(3)) + + /* Did we get the lock? */ +" eor %w1, %w0, %w0, ror #16\n" +" cbz %w1, 3f\n" + /* + * No: spin on the owner. Send a local event to avoid missing an + * unlock before the exclusive load. + */ +" sevl\n" +"2: wfe\n" +" ldaxrh %w2, %4\n" +" eor %w1, %w2, %w0, lsr #16\n" +" cbnz %w1, 2b\n" + /* We got the lock. Critical section starts here. */ +"3:" + : "=&r" (lockval), "=&r" (newval), "=&r" (tmp), "+Q" (*lock) + : "Q" (lock->owner) + : "memory"); +} + +static inline void hyp_spin_unlock(hyp_spinlock_t *lock) +{ + u64 tmp; + + asm volatile( + ARM64_LSE_ATOMIC_INSN( + /* LL/SC */ + " ldrh %w1, %0\n" + " add %w1, %w1, #1\n" + " stlrh %w1, %0", + /* LSE atomics */ + " mov %w1, #1\n" + " staddlh %w1, %0\n" + __nops(1)) + : "=Q" (lock->owner), "=&r" (tmp) + : + : "memory"); +} + +#endif /* __ARM64_KVM_NVHE_SPINLOCK_H__ */ -- 2.31.0.rc2.261.g7f71774620-goog