Received: by 2002:ab2:3350:0:b0:1f4:6588:b3a7 with SMTP id o16csp1220361lqe; Mon, 8 Apr 2024 02:22:46 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWRhEMbrLhp98ts/Lp0XlxbbdAvLONKeaYjfxQvjCrYF8CZjYcyg5CVq4NHy0b8RCHk5HSQh6ICDg/hidReKtBNCg/q8csPtiZwWk7uuA== X-Google-Smtp-Source: AGHT+IF7gcQaRIiqUKPVvpY38w2naesBW3BD1n3Yt2FnjdlRim42vzOPvzof2EOEQ31qLtD+HIHn X-Received: by 2002:aca:d0f:0:b0:3c5:f39c:9181 with SMTP id 15-20020aca0d0f000000b003c5f39c9181mr2557414oin.48.1712568166454; Mon, 08 Apr 2024 02:22:46 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712568166; cv=pass; d=google.com; s=arc-20160816; b=IevwTL32fO+1rCOp3CrLLuuBfydTZtIPx5en62ZVGwnmPqKI+6VCff6oCQ+IAbN2f6 1wYnS+N++uTmUp8WLJM0KoXqanhfBn5i8Kpn+fXmVNcs4dhLJw13MfxzMHQsXMUWxDts 7HfycqW7SspukbXmx5+mIer3unAd1MSODQ1SuCnkMLokLSuW9FTbC7RVx3uAmIvD20n1 Ze/efCpM3FmH/vK1kn3tUC5p3i8oXFGQcMli/W/pIVM5ZsEdLtMnZ42+WFMKpL69owBU 6+bz2TIOx5Z8FgQIHMt9DuQVmOZNQEaCEx1i1fGOALkFsguFoA8AHvJoBTSgVP7ZkTdr FQ3Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=Lly84JUd9nQjiRLZt1VPtBdBs8JWG4Lo21me51Cb3p8=; fh=lpKdmlvufNMxu13qHoWf6jepXOAF8PBOZbL0IjCQmWA=; b=ZmnB9utoSfnMKWOM+NyOGFqFebHSqJ1uWt3KPhxzCaPs3RAu6ALptAzdJpM4Z+5NJ2 CvPVfhYSMY6oiuy/3o8Z3NTHCMQ4okSP2GPETows8NieoJfaBtq3ldZc6tJmzDD/s7sQ SgL7a2lgVZXAosszpG8wF3y8XMj2/Ru/Gv6MJvqHuntheAREKHaVd5MKe1vp8hRb3+Sr 9byzqSf1tx7ov4A3/SKe30MPc0WlmUL6c76kqhhJkZrEBi8gU73jYiyxW4/m8b//u9/l aNf6hu09L14vUddTBUBf9cfNFN5re4UTPpfMYFQIGqR6Enf87xMYdRElfXydQOi/+SAL v2sg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=OA8Lv5Ok; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-135126-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-135126-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id s4-20020a62e704000000b006ea78a806f0si5938898pfh.269.2024.04.08.02.22.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Apr 2024 02:22:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-135126-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=OA8Lv5Ok; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-135126-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-135126-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id E4DF4B211F9 for ; Mon, 8 Apr 2024 09:17:02 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 035B04DA11; Mon, 8 Apr 2024 09:15:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="OA8Lv5Ok" Received: from mail-lf1-f44.google.com (mail-lf1-f44.google.com [209.85.167.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E92F0482EA for ; Mon, 8 Apr 2024 09:15:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.44 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712567756; cv=none; b=Zf2w+ZALt3sOkga6IKc+tO9V3rDgAq7thqMTezGKFMrTEolojRhoHf71qU2IoLhr9rhpV3aQFuIPNQglnw9K24b3ZLW4d1AU2vTMRvF20dyb2GRY62NmbLQ8Ca0r3v4DvtScgbYP6OquOgu+Uj3K1a4wjBooczpukXFwl4V9Gp4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712567756; c=relaxed/simple; bh=ajtEuYhg6tg/Ve6M8YmoGbWiyVZbMBY8VxkpitJpcxE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YIB8FK6ydpagaGVTVAdUq51HJv5JVDFysHKur/dYE/a+JU0f5QwoPsVV5RCb9USU/L9PSZ0vv6scN4Z1Vpur985DsZncjh4wEgLmSdo6U124hHFfdhpFJzRqdAwwxOJ456ATvUJkJPgCoLNF3N1LVDn6WAgdVI87Gl/5m3mBdqE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=OA8Lv5Ok; arc=none smtp.client-ip=209.85.167.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-lf1-f44.google.com with SMTP id 2adb3069b0e04-516dbc36918so2088585e87.0 for ; Mon, 08 Apr 2024 02:15:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712567753; x=1713172553; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Lly84JUd9nQjiRLZt1VPtBdBs8JWG4Lo21me51Cb3p8=; b=OA8Lv5OkEs6fqGWarE34I01fYv6IGo24ofV0HGGWqxXqvTGbAsKti8IA2a4gjfEIYv BvpSjGtEtTaUmTae06kTuILgldueAazqQM+iqbfhocVvA82eTAtbTyMFA2BGmufieRp8 wEIB2L9dRO2uQ3DlKrAogGa1Iw3+pL1OL9TFDo3cMUjv1ANlJdhdR0w5BVhVxWh2og94 nCtn/nk/8SBDgsw1Rh/Wz+vDa/oHG7KGkn8NbFYtLALyaUvNrO74+Ykiabe00lcwwEpv rAOnEVeBymFiaq0knykkp+ZaIcmmCbQTuQprpunQSm7az4faFEWh3w51ZiktjLAbyP1A WYpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712567753; x=1713172553; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Lly84JUd9nQjiRLZt1VPtBdBs8JWG4Lo21me51Cb3p8=; b=DASegVMFvUkcx5GTOOf7MK6VMUUmucdPZ32z86Damad30yMnV1c+iGdUw3FNloE2DQ Q6tZrjoZWAB3NDG1FEj3CoC3HOzfA0eE9II4GDhQjgorGIPXThso1/ChJNaYVjeH71Ix 1WEdJpSDtQtpO7wAxdNNU4aoLPDY3j2Cc+GgJ49dt9eT201sO+gKabK6KebS2oeEDVsy 4XunDjAI82dE6b0vhzynox/Mlvsf4htWqhQHxaA+6UPxDvEVwIgcRisG729z1KAHG0sc PMN04GH02HZriyaKY75z3hPyhXzHxeewXdPr4zke8J9RXojdUlwBA5ZVLIGsckynJMG6 F6Hw== X-Forwarded-Encrypted: i=1; AJvYcCXh1AvkCabczcxKV/GoCPPuEhnyGBxHVOTB1NsRCVW9N0u50jdaVa0rHANTAJoMAT8KicLpJA8xGdS15XGCkNaurvHoxHYyH8kJBWCu X-Gm-Message-State: AOJu0YwKenumgphtyDQN1UqnY3zQPJimsvDVvDGgWSeUs8uBlLV86Cc1 fARMe4tRunO2TXtQlVjoV9Gnv6QqjqKo0RHOOKOyc6PoBhDAm/Fk X-Received: by 2002:ac2:539c:0:b0:515:9185:652f with SMTP id g28-20020ac2539c000000b005159185652fmr4919406lfh.33.1712567753010; Mon, 08 Apr 2024 02:15:53 -0700 (PDT) Received: from fedora.iskraemeco.si ([193.77.86.250]) by smtp.gmail.com with ESMTPSA id n2-20020a056402434200b0056de7bee45bsm3854201edc.18.2024.04.08.02.15.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Apr 2024 02:15:52 -0700 (PDT) From: Uros Bizjak To: x86@kernel.org, linux-kernel@vger.kernel.org Cc: Uros Bizjak , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Peter Zijlstra Subject: [PATCH 2/3] locking/atomic/x86: Modernize x86_32 arch_{,try_}_cmpxchg64{,_local}() Date: Mon, 8 Apr 2024 11:13:57 +0200 Message-ID: <20240408091547.90111-3-ubizjak@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240408091547.90111-1-ubizjak@gmail.com> References: <20240408091547.90111-1-ubizjak@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Commit: b23e139d0b66 ("arch: Introduce arch_{,try_}_cmpxchg128{,_local}()") introduced arch_{,try_}_cmpxchg128{,_local}() for x86_64 targets. Modernize existing x86_32 arch_{,try_}_cmpxchg64{,_local}() definitions to follow the same structure as the definitions introduced by the above commit. No functional changes intended. Signed-off-by: Uros Bizjak Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Dave Hansen Cc: "H. Peter Anvin" Cc: Peter Zijlstra --- arch/x86/include/asm/cmpxchg_32.h | 179 +++++++++++++++++------------- 1 file changed, 100 insertions(+), 79 deletions(-) diff --git a/arch/x86/include/asm/cmpxchg_32.h b/arch/x86/include/asm/cmpxchg_32.h index b5731c51f0f4..fe40d0681ea8 100644 --- a/arch/x86/include/asm/cmpxchg_32.h +++ b/arch/x86/include/asm/cmpxchg_32.h @@ -3,103 +3,124 @@ #define _ASM_X86_CMPXCHG_32_H /* - * Note: if you use set64_bit(), __cmpxchg64(), or their variants, + * Note: if you use __cmpxchg64(), or their variants, * you need to test for the feature in boot_cpu_data. */ -#ifdef 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), \ - (unsigned long long)(n))) -#define arch_try_cmpxchg64(ptr, po, n) \ - __try_cmpxchg64((ptr), (unsigned long long *)(po), \ - (unsigned long long)(n)) -#endif +union __u64_halves { + u64 full; + struct { + u32 low, high; + }; +}; + +#define __arch_cmpxchg64(_ptr, _old, _new, _lock) \ +({ \ + union __u64_halves o = { .full = (_old), }, \ + n = { .full = (_new), }; \ + \ + asm volatile(_lock "cmpxchg8b %[ptr]" \ + : [ptr] "+m" (*(_ptr)), \ + "+a" (o.low), "+d" (o.high) \ + : "b" (n.low), "c" (n.high) \ + : "memory"); \ + \ + o.full; \ +}) -static inline u64 __cmpxchg64(volatile u64 *ptr, u64 old, u64 new) + +static __always_inline u64 __cmpxchg64(volatile u64 *ptr, u64 old, u64 new) { - u64 prev; - asm volatile(LOCK_PREFIX "cmpxchg8b %1" - : "=A" (prev), - "+m" (*ptr) - : "b" ((u32)new), - "c" ((u32)(new >> 32)), - "0" (old) - : "memory"); - return prev; + return __arch_cmpxchg64(ptr, old, new, LOCK_PREFIX); } -static inline u64 __cmpxchg64_local(volatile u64 *ptr, u64 old, u64 new) +static __always_inline u64 __cmpxchg64_local(volatile u64 *ptr, u64 old, u64 new) { - u64 prev; - asm volatile("cmpxchg8b %1" - : "=A" (prev), - "+m" (*ptr) - : "b" ((u32)new), - "c" ((u32)(new >> 32)), - "0" (old) - : "memory"); - return prev; + return __arch_cmpxchg64(ptr, old, new,); } -static inline bool __try_cmpxchg64(volatile u64 *ptr, u64 *pold, u64 new) +#define __arch_try_cmpxchg64(_ptr, _oldp, _new, _lock) \ +({ \ + union __u64_halves o = { .full = *(_oldp), }, \ + n = { .full = (_new), }; \ + bool ret; \ + \ + asm volatile(_lock "cmpxchg8b %[ptr]" \ + CC_SET(e) \ + : CC_OUT(e) (ret), \ + [ptr] "+m" (*(_ptr)), \ + "+a" (o.low), "+d" (o.high) \ + : "b" (n.low), "c" (n.high) \ + : "memory"); \ + \ + if (unlikely(!ret)) \ + *(_oldp) = o.full; \ + \ + likely(ret); \ +}) + +static __always_inline bool __try_cmpxchg64(volatile u64 *ptr, u64 *oldp, u64 new) { - bool success; - u64 old = *pold; - asm volatile(LOCK_PREFIX "cmpxchg8b %[ptr]" - CC_SET(z) - : CC_OUT(z) (success), - [ptr] "+m" (*ptr), - "+A" (old) - : "b" ((u32)new), - "c" ((u32)(new >> 32)) - : "memory"); - - if (unlikely(!success)) - *pold = old; - return success; + return __arch_try_cmpxchg64(ptr, oldp, new, LOCK_PREFIX); } -#ifndef CONFIG_X86_CMPXCHG64 +#ifdef CONFIG_X86_CMPXCHG64 + +#define arch_cmpxchg64 __cmpxchg64 + +#define arch_cmpxchg64_local __cmpxchg64_local + +#define arch_try_cmpxchg64 __try_cmpxchg64 + +#else + /* * Building a kernel capable running on 80386 and 80486. It may be necessary * to simulate the cmpxchg8b on the 80386 and 80486 CPU. */ -#define arch_cmpxchg64(ptr, o, n) \ -({ \ - __typeof__(*(ptr)) __ret; \ - __typeof__(*(ptr)) __old = (o); \ - __typeof__(*(ptr)) __new = (n); \ - alternative_io(LOCK_PREFIX_HERE \ - "call cmpxchg8b_emu", \ - "lock; cmpxchg8b (%%esi)" , \ - X86_FEATURE_CX8, \ - "=A" (__ret), \ - "S" ((ptr)), "0" (__old), \ - "b" ((unsigned int)__new), \ - "c" ((unsigned int)(__new>>32)) \ - : "memory"); \ - __ret; }) - - -#define arch_cmpxchg64_local(ptr, o, n) \ -({ \ - __typeof__(*(ptr)) __ret; \ - __typeof__(*(ptr)) __old = (o); \ - __typeof__(*(ptr)) __new = (n); \ - alternative_io("call cmpxchg8b_emu", \ - "cmpxchg8b (%%esi)" , \ - X86_FEATURE_CX8, \ - "=A" (__ret), \ - "S" ((ptr)), "0" (__old), \ - "b" ((unsigned int)__new), \ - "c" ((unsigned int)(__new>>32)) \ - : "memory"); \ - __ret; }) +#define __arch_cmpxchg64_emu(_ptr, _old, _new) \ +({ \ + union __u64_halves o = { .full = (_old), }, \ + n = { .full = (_new), }; \ + \ + asm volatile(ALTERNATIVE(LOCK_PREFIX_HERE \ + "call cmpxchg8b_emu", \ + "lock; cmpxchg8b %[ptr]", X86_FEATURE_CX8) \ + : [ptr] "+m" (*(_ptr)), \ + "+a" (o.low), "+d" (o.high) \ + : "b" (n.low), "c" (n.high), "S" (_ptr) \ + : "memory"); \ + \ + o.full; \ +}) + +static __always_inline u64 arch_cmpxchg64(volatile u64 *ptr, u64 old, u64 new) +{ + return __arch_cmpxchg64_emu(ptr, old, new); +} +#define arch_cmpxchg64 arch_cmpxchg64 + +#define __arch_cmpxchg64_emu_local(_ptr, _old, _new) \ +({ \ + union __u64_halves o = { .full = (_old), }, \ + n = { .full = (_new), }; \ + \ + asm volatile(ALTERNATIVE("call cmpxchg8b_emu", \ + "cmpxchg8b %[ptr]", X86_FEATURE_CX8) \ + : [ptr] "+m" (*(_ptr)), \ + "+a" (o.low), "+d" (o.high) \ + : "b" (n.low), "c" (n.high), "S" (_ptr) \ + : "memory"); \ + \ + o.full; \ +}) + +static __always_inline u64 arch_cmpxchg64_local(volatile u64 *ptr, u64 old, u64 new) +{ + return __arch_cmpxchg64_emu_local(ptr, old, new); +} +#define arch_cmpxchg64_local arch_cmpxchg64_local #endif -- 2.44.0