Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp3663124pxu; Tue, 15 Dec 2020 12:22:12 -0800 (PST) X-Google-Smtp-Source: ABdhPJzvfZCXZTRVROK5w8e4EPjOWJkdUDyjNCnSOsfYwPqfwmEXHTb2oeMYnBEpCAZ47HeBjaL3 X-Received: by 2002:a17:906:9605:: with SMTP id s5mr14694976ejx.179.1608063732255; Tue, 15 Dec 2020 12:22:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1608063732; cv=none; d=google.com; s=arc-20160816; b=GYolIGN99vm/1Aic0FkzdKpk00xXiCI2UKGZLxRwPkgIQS0ZOYoVkQiychV5Ax28V1 Q8qPEJ+RTU4q0BEGIhJTDzRtBhaiFBbRfrjmMyJpuwHJuBAKY5USEA2B9uG5/M4aCeAu lkvqbzC5yPOQB8PmD82Jw6Rb0jS+baxRfRBC8fmfd8JQ7Y/R3HrQ2/Hr0jmiLxcsFuE6 b6nZOUq26wkyc8UGWNXJQc+k1FOi1dNtmskhK+MBlEVaWRf6mrZNHhxMWo9FOIrncd6V 41e/5wjsulHgqppodCgtggY6rztDYRcI+GPymb8+NLGQ+T4fGAir/1fdiNcCK9uP0rp3 CF6g== 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=yYo6OawOM+iYHfsWvLFs9OA8sMFU25tBpReHtKNVFkc=; b=piQihA7Y2p3sKbBJv88EbDUUTL6PZZmHplLY6pbIqLCcnGQ9fnTUPrTjkTbkNAEoRj QISgInHnGROKVwN+pVZk8S7sVg3LxxIuRIaR2EGaJEPwsihlJNgncjeKMOl7vrPyOxLJ d+eWA9VIIeZDYsvYyL/SL8dQ6SvDlLgasj5C3HwXGLJaPKOZDrlTD0iY/Yr+UcDu65eR HuSYW2/ZcdOR4MUU0FMENy0NU3rq7IyYZbFZVW+sfCKbuNnx7ijeXCWMQbOUy+bD9qfb U4GoDdbO9pYv9DqsyxUPUb35w5l7dAhCX93TcwCh2QZMOHz7fsabJ8JwO9RAXC1UNXsv MSSQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="HdUPaWo/"; 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 t19si1493805edt.253.2020.12.15.12.21.47; Tue, 15 Dec 2020 12:22:12 -0800 (PST) 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="HdUPaWo/"; 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 S1729555AbgLOUKC (ORCPT + 99 others); Tue, 15 Dec 2020 15:10:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38568 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727290AbgLOUJs (ORCPT ); Tue, 15 Dec 2020 15:09:48 -0500 Received: from mail-qt1-x843.google.com (mail-qt1-x843.google.com [IPv6:2607:f8b0:4864:20::843]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 47F50C06179C; Tue, 15 Dec 2020 12:09:08 -0800 (PST) Received: by mail-qt1-x843.google.com with SMTP id c14so15625530qtn.0; Tue, 15 Dec 2020 12:09:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=yYo6OawOM+iYHfsWvLFs9OA8sMFU25tBpReHtKNVFkc=; b=HdUPaWo/44g1/ab/u/lCxL39OMeOuEcfxPk2Mvo8WlLVWcxbuoK6cKSUhBHQE/lEBC k+rJseJhnQYPF2ycS9qtRk1DGTk6MXydkyqwaLMjfQfKtTd1VO5TBGCAYk+HMoemr1Rt 9436dQFFu84trhmMJWkIQAlTDZy10hFvuqAVymR4AZY4KBD/OxQfKaN/z0zk3hzFbo7T a97kUKbl1PlLHbq4k1aw2ra8AyCmvUyr/AW7KWdWBiN1C4M9rJgC9eQrtYl/98Ljq1ns mNbNNKaXbHvwSnc0En3eH6kEGNSY0RzKxw8emNfM5a4ePz+MbryiGUzAqFaA45ibysbn NpWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=yYo6OawOM+iYHfsWvLFs9OA8sMFU25tBpReHtKNVFkc=; b=XEBbpJVltgL0smq41vogJe6/apKNy4qd9jjGEybSoJLJV/zM6PfHM7VgqUhyj5YDGl dm7Lpn22pRKZRraiTYviQQUvFEBg299nTE/o26AA3Fy6lng/Y24GQ+IFbS9Xca0CQXoJ lRHc1wbFN558P6rJzUwzqjPsh0tfK6BYnoTaWPwY9iNcIXFE5MeKmvc1yznRHSTY2Z9M IhnM0kJg8tDn8KXmBhqLi1Up5ga7FmMvEMnuPSAz919VlmTW7DjKPZKBNHYkIM4Kml0V OOMSm7XhDy70Cc2iyxOdKo7vpL6XahJXPWZrhS+6kYp/bdgnGJkTmz4rUE6KAF/kYAsd T54A== X-Gm-Message-State: AOAM533ASNm/IHnXdu1YVup1IkQuUbo+9bJLmne7lR/r1Qbo98n/aYcu uhazxifHZZVehQxUsOejLG5NaIPXH2GjyoZE1qb1sxuGzbEIFQ== X-Received: by 2002:ac8:3a63:: with SMTP id w90mr38565905qte.225.1608062947366; Tue, 15 Dec 2020 12:09:07 -0800 (PST) MIME-Version: 1.0 References: <20201215182805.53913-1-ubizjak@gmail.com> <20201215182805.53913-3-ubizjak@gmail.com> In-Reply-To: <20201215182805.53913-3-ubizjak@gmail.com> From: Uros Bizjak Date: Tue, 15 Dec 2020 21:08:58 +0100 Message-ID: Subject: Re: [PATCH 2/3] locking/atomic/x86: Introduce arch_try_cmpxchg64() To: X86 ML , kvm@vger.kernel.org, LKML Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Dec 15, 2020 at 7:28 PM Uros Bizjak wrote: > > Add arch_try_cmpxchg64(), similar to arch_try_cmpxchg(), that > operates with 64 bit operands. This function provides the same > interface for 32 bit and 64 bit targets. > > Signed-off-by: Uros Bizjak > Cc: Thomas Gleixner > Cc: Ingo Molnar > Cc: Borislav Petkov > Cc: "H. Peter Anvin" > --- > arch/x86/include/asm/cmpxchg_32.h | 62 ++++++++++++++++++++++++++----- > arch/x86/include/asm/cmpxchg_64.h | 6 +++ > 2 files changed, 59 insertions(+), 9 deletions(-) > > diff --git a/arch/x86/include/asm/cmpxchg_32.h b/arch/x86/include/asm/cmpxchg_32.h > index 0a7fe0321613..8dcde400244e 100644 > --- a/arch/x86/include/asm/cmpxchg_32.h > +++ b/arch/x86/include/asm/cmpxchg_32.h > @@ -35,15 +35,6 @@ static inline void set_64bit(volatile u64 *ptr, u64 value) > : "memory"); > } > > -#ifdef CONFIG_X86_CMPXCHG64 Oops, I didn't notice that I had left a reversed #ifdef condition in the patch (to test 32 bit target without X86_CMPXCHG64). Obviously, CONFIG_X86_CMPXCHG64 has to be defined to use CMPXCHG8B, so please use #ifdef here. Uros. > -#define arch_cmpxchg64(ptr, o, n) \ > - ((__typeof__(*(ptr)))__cmpxchg64((ptr), (unsigned long long)(o), \ > - (unsigned long long)(n))) > -#define arch_cmpxchg64_local(ptr, o, n) \ > - ((__typeof__(*(ptr)))__cmpxchg64_local((ptr), (unsigned long long)(o), \ > - (unsigned long long)(n))) > -#endif > - > static inline u64 __cmpxchg64(volatile u64 *ptr, u64 old, u64 new) > { > u64 prev; > @@ -71,6 +62,39 @@ static inline u64 __cmpxchg64_local(volatile u64 *ptr, u64 old, u64 new) > } > > #ifndef CONFIG_X86_CMPXCHG64 > +#define arch_cmpxchg64(ptr, o, n) \ > + ((__typeof__(*(ptr)))__cmpxchg64((ptr), (unsigned long long)(o), \ > + (unsigned long long)(n))) > +#define arch_cmpxchg64_local(ptr, o, n) \ > + ((__typeof__(*(ptr)))__cmpxchg64_local((ptr), (unsigned long long)(o), \ > + > +#define __raw_try_cmpxchg64(_ptr, _pold, _new, lock) \ > +({ \ > + bool success; \ > + __typeof__(_ptr) _old = (__typeof__(_ptr))(_pold); \ > + __typeof__(*(_ptr)) __old = *_old; \ > + __typeof__(*(_ptr)) __new = (_new); \ > + asm volatile(lock "cmpxchg8b %1" \ > + CC_SET(z) \ > + : CC_OUT(z) (success), \ > + "+m" (*_ptr), \ > + "+A" (__old) \ > + : "b" ((unsigned int)__new), \ > + "c" ((unsigned int)(__new>>32)) \ > + : "memory"); \ > + if (unlikely(!success)) \ > + *_old = __old; \ > + likely(success); \ > +}) > + > +#define __try_cmpxchg64(ptr, pold, new) \ > + __raw_try_cmpxchg64((ptr), (pold), (new), LOCK_PREFIX) > + > +#define arch_try_cmpxchg64(ptr, pold, new) \ > + __try_cmpxchg64((ptr), (pold), (new)) > + > +#else > + > /* > * Building a kernel capable running on 80386 and 80486. It may be necessary > * to simulate the cmpxchg8b on the 80386 and 80486 CPU. > @@ -108,6 +132,26 @@ static inline u64 __cmpxchg64_local(volatile u64 *ptr, u64 old, u64 new) > : "memory"); \ > __ret; }) > > +#define arch_try_cmpxchg64(ptr, po, n) \ > +({ \ > + bool success; \ > + __typeof__(ptr) _old = (__typeof__(ptr))(po); \ > + __typeof__(*(ptr)) __old = *_old; \ > + __typeof__(*(ptr)) __new = (n); \ > + alternative_io(LOCK_PREFIX_HERE \ > + "call cmpxchg8b_emu", \ > + "lock; cmpxchg8b (%%esi)" , \ > + X86_FEATURE_CX8, \ > + "+A" (__old), \ > + "S" ((ptr)), \ > + "b" ((unsigned int)__new), \ > + "c" ((unsigned int)(__new>>32)) \ > + : "memory"); \ > + success = (__old == *_old); \ > + if (unlikely(!success)) \ > + *_old = __old; \ > + likely(success); \ > +}) > #endif > > #define system_has_cmpxchg_double() boot_cpu_has(X86_FEATURE_CX8) > diff --git a/arch/x86/include/asm/cmpxchg_64.h b/arch/x86/include/asm/cmpxchg_64.h > index 072e5459fe2f..250187ac8248 100644 > --- a/arch/x86/include/asm/cmpxchg_64.h > +++ b/arch/x86/include/asm/cmpxchg_64.h > @@ -19,6 +19,12 @@ static inline void set_64bit(volatile u64 *ptr, u64 val) > arch_cmpxchg_local((ptr), (o), (n)); \ > }) > > +#define arch_try_cmpxchg64(ptr, po, n) \ > +({ \ > + BUILD_BUG_ON(sizeof(*(ptr)) != 8); \ > + arch_try_cmpxchg((ptr), (po), (n)); \ > +}) > + > #define system_has_cmpxchg_double() boot_cpu_has(X86_FEATURE_CX16) > > #endif /* _ASM_X86_CMPXCHG_64_H */ > -- > 2.26.2 >