From nobody Fri Apr 19 07:43:04 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1599840433; cv=none; d=zohomail.com; s=zohoarc; b=Mih7IhgGiKEHkQY0u/AOmgFjRbsbVmpqV08lUROpZYP87ls2ja34/5bm2Xw0gNlAa9VF+tbqmS2bzEiEeUW49po+K5MzB2wXOQarkr75z/xqOvSyGh1AK5Az/OtHvCW4+8OKfFrJoacI7+boWYbl2H9FMNPjYk7GHT4oyOtl254= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1599840433; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=Sa36iyZYfVN3Ys6U7bY6KqH1/AuEo45swTUKX5MoIIg=; b=PcSgnuUoeOUnZZo6V/6kXPTj39YTd1vGsn7Fv36e8m8MLuzPN9x7ZEEQEZ9ukOXx9R2nCrOa5J2rGaMvAx8l1m+yB+Aew/yb1o8YVGfWfsxhKyTVPsd5UXcZ2sVCpSaOCaJsCRuFSqUw5k7B5nzLZdlZ3lQDEPrfBCKUTZUQqCI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1599840433642326.06185406262284; Fri, 11 Sep 2020 09:07:13 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kGlZV-0001mS-Us; Fri, 11 Sep 2020 16:06:45 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kGlZU-0001m8-PN for xen-devel@lists.xenproject.org; Fri, 11 Sep 2020 16:06:44 +0000 Received: from mail.xenproject.org (unknown [104.130.215.37]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 7e7ef991-196e-4654-9658-b9b525448060; Fri, 11 Sep 2020 16:06:39 +0000 (UTC) Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kGlZP-0004xX-3D; Fri, 11 Sep 2020 16:06:39 +0000 Received: from 54-240-197-235.amazon.com ([54.240.197.235] helo=ufe34d9ed68d054.ant.amazon.com) by xenbits.xenproject.org with esmtpsa (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kGlZO-0001gv-Mg; Fri, 11 Sep 2020 16:06:38 +0000 X-Inumbo-ID: 7e7ef991-196e-4654-9658-b9b525448060 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xen.org; s=20200302mail; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=Sa36iyZYfVN3Ys6U7bY6KqH1/AuEo45swTUKX5MoIIg=; b=pAIkVHcO7XFosKsFxGePdmgZQ Y/xU2ZujOaxc8WI+pP6ACq2sR5kIcrHzjanSllQBXcivr3WWiK7aAoJiPRSKCiY/UsMFbpNb5rGQ/ tuKDpUCxOoXYzFXZynwO0utQE7/GW22d8NHmK4MgztGZLhnRJtIBhsPHoTvU0n7FfYrYo=; From: Julien Grall To: xen-devel@lists.xenproject.org Cc: oleksandr_tyshchenko@epam.com, Julien Grall , Stefano Stabellini , Julien Grall , Volodymyr Babchuk Subject: [PATCH v2 1/2] xen/arm: Remove cmpxchg_local() and drop _mb from the other helpers Date: Fri, 11 Sep 2020 17:06:21 +0100 Message-Id: <20200911160622.19721-2-julien@xen.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200911160622.19721-1-julien@xen.org> References: <20200911160622.19721-1-julien@xen.org> X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: pass (identity @xen.org) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Julien Grall The current set of helpers are quite confusing to follow as the naming is not very consistent. Given that cmpxchg_local() is not used in Xen, drop it completely. Furthermore, adopt a naming with _mb so all names are now consistent. Signed-off-by: Julien Grall Reviewed-by: Stefano Stabellini --- Changes in v2: - Patch added --- xen/include/asm-arm/arm32/cmpxchg.h | 31 ++++++----------------- xen/include/asm-arm/arm64/cmpxchg.h | 38 +++++++---------------------- xen/include/asm-arm/guest_atomics.h | 6 ++--- 3 files changed, 19 insertions(+), 56 deletions(-) diff --git a/xen/include/asm-arm/arm32/cmpxchg.h b/xen/include/asm-arm/arm3= 2/cmpxchg.h index 0770f272ee99..3ef1e5c63276 100644 --- a/xen/include/asm-arm/arm32/cmpxchg.h +++ b/xen/include/asm-arm/arm32/cmpxchg.h @@ -112,23 +112,12 @@ static always_inline unsigned long __cmpxchg(volatile= void *ptr, unsigned long new, int size) { + smp_mb(); if (!__int_cmpxchg(ptr, &old, new, size, false, 0)) ASSERT_UNREACHABLE(); - - return old; -} - -static always_inline unsigned long __cmpxchg_mb(volatile void *ptr, - unsigned long old, - unsigned long new, int siz= e) -{ - unsigned long ret; - - smp_mb(); - ret =3D __cmpxchg(ptr, old, new, size); smp_mb(); =20 - return ret; + return old; } =20 /* @@ -141,11 +130,11 @@ static always_inline unsigned long __cmpxchg_mb(volat= ile void *ptr, * The helper will return true when the update has succeeded (i.e no * timeout) and false if the update has failed. */ -static always_inline bool __cmpxchg_mb_timeout(volatile void *ptr, - unsigned long *old, - unsigned long new, - int size, - unsigned int max_try) +static always_inline bool __cmpxchg_timeout(volatile void *ptr, + unsigned long *old, + unsigned long new, + int size, + unsigned int max_try) { bool ret; =20 @@ -157,12 +146,6 @@ static always_inline bool __cmpxchg_mb_timeout(volatil= e void *ptr, } =20 #define cmpxchg(ptr,o,n) \ - ((__typeof__(*(ptr)))__cmpxchg_mb((ptr), \ - (unsigned long)(o), \ - (unsigned long)(n), \ - sizeof(*(ptr)))) - -#define cmpxchg_local(ptr,o,n) \ ((__typeof__(*(ptr)))__cmpxchg((ptr), \ (unsigned long)(o), \ (unsigned long)(n), \ diff --git a/xen/include/asm-arm/arm64/cmpxchg.h b/xen/include/asm-arm/arm6= 4/cmpxchg.h index fc5c60f0bd74..f4a8c1ccba80 100644 --- a/xen/include/asm-arm/arm64/cmpxchg.h +++ b/xen/include/asm-arm/arm64/cmpxchg.h @@ -125,23 +125,12 @@ static always_inline unsigned long __cmpxchg(volatile= void *ptr, unsigned long new, int size) { + smp_mb(); if (!__int_cmpxchg(ptr, &old, new, size, false, 0)) ASSERT_UNREACHABLE(); - - return old; -} - -static always_inline unsigned long __cmpxchg_mb(volatile void *ptr, - unsigned long old, - unsigned long new, int size) -{ - unsigned long ret; - - smp_mb(); - ret =3D __cmpxchg(ptr, old, new, size); smp_mb(); =20 - return ret; + return old; } =20 /* @@ -154,11 +143,11 @@ static always_inline unsigned long __cmpxchg_mb(volat= ile void *ptr, * The helper will return true when the update has succeeded (i.e no * timeout) and false if the update has failed. */ -static always_inline bool __cmpxchg_mb_timeout(volatile void *ptr, - unsigned long *old, - unsigned long new, - int size, - unsigned int max_try) +static always_inline bool __cmpxchg_timeout(volatile void *ptr, + unsigned long *old, + unsigned long new, + int size, + unsigned int max_try) { bool ret; =20 @@ -173,17 +162,8 @@ static always_inline bool __cmpxchg_mb_timeout(volatil= e void *ptr, ({ \ __typeof__(*(ptr)) __ret; \ __ret =3D (__typeof__(*(ptr))) \ - __cmpxchg_mb((ptr), (unsigned long)(o), (unsigned long)(n), \ - sizeof(*(ptr))); \ - __ret; \ -}) - -#define cmpxchg_local(ptr, o, n) \ -({ \ - __typeof__(*(ptr)) __ret; \ - __ret =3D (__typeof__(*(ptr))) \ - __cmpxchg((ptr), (unsigned long)(o), \ - (unsigned long)(n), sizeof(*(ptr))); \ + __cmpxchg((ptr), (unsigned long)(o), (unsigned long)(n), \ + sizeof(*(ptr))); \ __ret; \ }) =20 diff --git a/xen/include/asm-arm/guest_atomics.h b/xen/include/asm-arm/gues= t_atomics.h index af27cc627bf3..20347849e56c 100644 --- a/xen/include/asm-arm/guest_atomics.h +++ b/xen/include/asm-arm/guest_atomics.h @@ -96,14 +96,14 @@ static inline unsigned long __guest_cmpxchg(struct doma= in *d, =20 perfc_incr(atomics_guest); =20 - if ( __cmpxchg_mb_timeout(ptr, &oldval, new, size, - this_cpu(guest_safe_atomic_max)) ) + if ( __cmpxchg_timeout(ptr, &oldval, new, size, + this_cpu(guest_safe_atomic_max)) ) return oldval; =20 perfc_incr(atomics_guest_paused); =20 domain_pause_nosync(d); - oldval =3D __cmpxchg_mb(ptr, old, new, size); + oldval =3D __cmpxchg(ptr, old, new, size); domain_unpause(d); =20 return oldval; --=20 2.17.1 From nobody Fri Apr 19 07:43:04 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1599840432; cv=none; d=zohomail.com; s=zohoarc; b=VLNHhRiRtm8OAbAgNd4XDaJbFSA+K6YJ1NYo+SSmQ7UJd7ixvMql28KSPjEOc9KBe2XzTwJ+iO5/PwlEU+fKb+BvYd88l/eAYQ8QJp9yxwjbGFmaShNv377U+Vth9DGl+/1WXcpzOBAh5X/3jbLiCebak5D4HiAm7LFYwjE0W5g= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1599840432; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=HmVBoYD+AJa7li96NWso5BcK1WuNr2xh7fR0XTtGlHw=; b=HK7C1cstCa3S2nIdLIzVje0ajMnl4FlpfynLD+6UXR9mVIafbrEO0HtiCc1lz32UGdtGcujmjBj22x7qLP+aZrZ/r0X4I8dvLv8SQAw9qqzxSCeVDFhT7yQBu4zQK6IdzWUO7tpzFJmOWD8uCDTj9OGLYheleaBJVDy4CO+SkfE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1599840432851287.5307286641745; Fri, 11 Sep 2020 09:07:12 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kGlZb-0001mn-7F; Fri, 11 Sep 2020 16:06:51 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kGlZZ-0001m8-PO for xen-devel@lists.xenproject.org; Fri, 11 Sep 2020 16:06:49 +0000 Received: from mail.xenproject.org (unknown [104.130.215.37]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id ca5ff15d-c49d-477b-b00b-f3e84782ca56; Fri, 11 Sep 2020 16:06:41 +0000 (UTC) Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kGlZQ-0004xg-Lx; Fri, 11 Sep 2020 16:06:40 +0000 Received: from 54-240-197-235.amazon.com ([54.240.197.235] helo=ufe34d9ed68d054.ant.amazon.com) by xenbits.xenproject.org with esmtpsa (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kGlZQ-0001gv-CF; Fri, 11 Sep 2020 16:06:40 +0000 X-Inumbo-ID: ca5ff15d-c49d-477b-b00b-f3e84782ca56 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xen.org; s=20200302mail; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=HmVBoYD+AJa7li96NWso5BcK1WuNr2xh7fR0XTtGlHw=; b=CHmx9ZxEBBflTWjJwVkq2RcgZ E5BU6NQmnOT3mc08SnLWzeS3od0ZNMgGpsMsZD1RxjD8MPI3VJsq47f9gScBZAa2+xzC/3j6hNSe1 5eZ5vdVx4+uWspa/nkFA3KhQCtktvsNFfZy+dsHMc30kuvENVTs8H9QCk4hhnHdbssNi8=; From: Julien Grall To: xen-devel@lists.xenproject.org Cc: oleksandr_tyshchenko@epam.com, Julien Grall , Stefano Stabellini , Julien Grall , Volodymyr Babchuk , Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Wei Liu Subject: [PATCH v2 2/2] xen: Introduce cmpxchg64() and guest_cmpxchg64() Date: Fri, 11 Sep 2020 17:06:22 +0100 Message-Id: <20200911160622.19721-3-julien@xen.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200911160622.19721-1-julien@xen.org> References: <20200911160622.19721-1-julien@xen.org> X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: pass (identity @xen.org) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Julien Grall The IOREQ code is using cmpxchg() with 64-bit value. At the moment, this is x86 code, but there is plan to make it common. To cater 32-bit arch, introduce two new helpers to deal with 64-bit cmpxchg(). The Arm 32-bit implementation of cmpxchg64() is based on the __cmpxchg64 in Linux v5.8 (arch/arm/include/asm/cmpxchg.h). Note that only guest_cmpxchg64() is introduced on x86 so far. Signed-off-by: Julien Grall Acked-by: Jan Beulich Reviewed-by: Stefano Stabellini --- I have looked at whether we can extend cmpxchg() to support 64-bit on arm32 bit. However the resulting code is much worse as the compiler will use the stack more often to spill. Therefore the introduction of cmpxchg64() is better option. Changes in v2: - Remove extra teq in the arm32 cmpxchg implementation - Don't define cmpxchg64() on x86 - Drop _mb from the helpers name - Add missing barrier in the arm32 implementation --- xen/include/asm-arm/arm32/cmpxchg.h | 68 +++++++++++++++++++++++++++++ xen/include/asm-arm/arm64/cmpxchg.h | 5 +++ xen/include/asm-arm/guest_atomics.h | 22 ++++++++++ xen/include/asm-x86/guest_atomics.h | 1 + 4 files changed, 96 insertions(+) diff --git a/xen/include/asm-arm/arm32/cmpxchg.h b/xen/include/asm-arm/arm3= 2/cmpxchg.h index 3ef1e5c63276..b0bd1d8b685e 100644 --- a/xen/include/asm-arm/arm32/cmpxchg.h +++ b/xen/include/asm-arm/arm32/cmpxchg.h @@ -87,6 +87,37 @@ __CMPXCHG_CASE(b, 1) __CMPXCHG_CASE(h, 2) __CMPXCHG_CASE( , 4) =20 +static inline bool __cmpxchg_case_8(volatile uint64_t *ptr, + uint64_t *old, + uint64_t new, + bool timeout, + unsigned int max_try) +{ + uint64_t oldval; + uint64_t res; + + do { + asm volatile( + " ldrexd %1, %H1, [%3]\n" + " teq %1, %4\n" + " teqeq %H1, %H4\n" + " movne %0, #0\n" + " movne %H0, #0\n" + " bne 2f\n" + " strexd %0, %5, %H5, [%3]\n" + "2:" + : "=3D&r" (res), "=3D&r" (oldval), "+Qo" (*ptr) + : "r" (ptr), "r" (*old), "r" (new) + : "memory", "cc"); + if (!res) + break; + } while (!timeout || ((--max_try) > 0)); + + *old =3D oldval; + + return !res; +} + static always_inline bool __int_cmpxchg(volatile void *ptr, unsigned long = *old, unsigned long new, int size, bool timeout, unsigned int max_try) @@ -145,11 +176,48 @@ static always_inline bool __cmpxchg_timeout(volatile = void *ptr, return ret; } =20 +/* + * The helper may fail to update the memory if the action takes too long. + * + * @old: On call the value pointed contains the expected old value. It wil= l be + * updated to the actual old value. + * @max_try: Maximum number of iterations + * + * The helper will return true when the update has succeeded (i.e no + * timeout) and false if the update has failed. + */ +static always_inline bool __cmpxchg64_timeout(volatile uint64_t *ptr, + uint64_t *old, + uint64_t new, + unsigned int max_try) +{ + bool ret; + + smp_mb(); + ret =3D __cmpxchg_case_8(ptr, old, new, true, max_try); + smp_mb(); + + return ret; +} + #define cmpxchg(ptr,o,n) \ ((__typeof__(*(ptr)))__cmpxchg((ptr), \ (unsigned long)(o), \ (unsigned long)(n), \ sizeof(*(ptr)))) + +static inline uint64_t cmpxchg64(volatile uint64_t *ptr, + uint64_t old, + uint64_t new) +{ + smp_mb(); + if (!__cmpxchg_case_8(ptr, &old, new, false, 0)) + ASSERT_UNREACHABLE(); + smp_mb(); + + return old; +} + #endif /* * Local variables: diff --git a/xen/include/asm-arm/arm64/cmpxchg.h b/xen/include/asm-arm/arm6= 4/cmpxchg.h index f4a8c1ccba80..10e4edc022b7 100644 --- a/xen/include/asm-arm/arm64/cmpxchg.h +++ b/xen/include/asm-arm/arm64/cmpxchg.h @@ -167,6 +167,11 @@ static always_inline bool __cmpxchg_timeout(volatile v= oid *ptr, __ret; \ }) =20 +#define cmpxchg64(ptr, o, n) cmpxchg(ptr, o, n) + +#define __cmpxchg64_timeout(ptr, old, new, max_try) \ + __cmpxchg_timeout(ptr, old, new, 8, max_try) + #endif /* * Local variables: diff --git a/xen/include/asm-arm/guest_atomics.h b/xen/include/asm-arm/gues= t_atomics.h index 20347849e56c..9e2e96d4ff72 100644 --- a/xen/include/asm-arm/guest_atomics.h +++ b/xen/include/asm-arm/guest_atomics.h @@ -115,6 +115,28 @@ static inline unsigned long __guest_cmpxchg(struct dom= ain *d, (unsigned long)(n),\ sizeof (*(ptr)))) =20 +static inline uint64_t guest_cmpxchg64(struct domain *d, + volatile uint64_t *ptr, + uint64_t old, + uint64_t new) +{ + uint64_t oldval =3D old; + + perfc_incr(atomics_guest); + + if ( __cmpxchg64_timeout(ptr, &oldval, new, + this_cpu(guest_safe_atomic_max)) ) + return oldval; + + perfc_incr(atomics_guest_paused); + + domain_pause_nosync(d); + oldval =3D cmpxchg64(ptr, old, new); + domain_unpause(d); + + return oldval; +} + #endif /* _ARM_GUEST_ATOMICS_H */ /* * Local variables: diff --git a/xen/include/asm-x86/guest_atomics.h b/xen/include/asm-x86/gues= t_atomics.h index 029417c8ffc1..87e7075b7623 100644 --- a/xen/include/asm-x86/guest_atomics.h +++ b/xen/include/asm-x86/guest_atomics.h @@ -20,6 +20,7 @@ ((void)(d), test_and_change_bit(nr, p)) =20 #define guest_cmpxchg(d, ptr, o, n) ((void)(d), cmpxchg(ptr, o, n)) +#define guest_cmpxchg64(d, ptr, o, n) ((void)(d), cmpxchg(ptr, o, n)) =20 #endif /* _X86_GUEST_ATOMICS_H */ /* --=20 2.17.1