Received: by 2002:ab2:69cc:0:b0:1f4:be93:e15a with SMTP id n12csp1000659lqp; Sun, 14 Apr 2024 09:13:15 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXoUBsR4ApR/NuWjnAmEfUknrTWDpplknoUueNL7ZV8ZhwQ/i3i7C4gFeWDhOHm/mwULoxc7BO8oSvZehlRgJpBUxrqTJhmwJeT7DbRbw== X-Google-Smtp-Source: AGHT+IEIKsCpu2QtZOHrk57jPtvDr4z8oN0+zwJi70WILmXJx+adS0esPrjvDkjFdnl54MsBa/t0 X-Received: by 2002:a17:906:a107:b0:a52:4cd0:2124 with SMTP id t7-20020a170906a10700b00a524cd02124mr3176847ejy.7.1713111195853; Sun, 14 Apr 2024 09:13:15 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713111195; cv=pass; d=google.com; s=arc-20160816; b=aOE2e6iUsf/pN7wQKXOW2vKYRCOscViIIEZ5ifz0Ie+2n6X1HWTWu7K0e59djOtuqD G4qfTuQlHDg6ZKielRr3PM+pm90n3olODuKvAD09QMqVUoBNNoWBdlN0XGDG29/0dp8e xrnxx3OiLSpEMoqBn1HOsECPBNLLyyoZF5yNdnybl5N3VrE4nVWQm9gi0tlgxm7gMjtI AGNz0eJo143EBPRmp6FqumWTMlEloHtmhRB0gvx8NMizWj5+90PcF0+SgA0HiMlvbJGE aEF4RLGAnDx23tOK2KaPWaLspDlcGobhARU3gLCRN2BSGUjsX/vYcx+LC2X6lcB6WQXE jROg== 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:message-id:date:subject:cc:to :from:dkim-signature; bh=5pb6u2iKzO91RpMLMUiyKz4o6sFnm+wegDY6BclSI7s=; fh=rg5bR24ysZ3dd9Hod7kHkwCOEE54QOtZ8kQ3eQ5P/o8=; b=SfdCjF0V7kOz1p/YtNMee5tc/H8PKDfkk7Ykj29YNIRSntBjHPYxOlkx7AmGEil2g4 3Rya7xssduLlTCJHT3mjNe0JDTMIyzmcchn95+BVOR85GaKZOPvLuMBDYQwu5W+F/L+J WRx0uMFR5r3/GxfRchw2HVff1agEFWEla0H0txtE4LY8xIDb9a5SS/kqKNQ5vn7oaddt q1y2kGBXP4zKxkmNyQ+aobbw0PlZbGCGH3ormPoehAzKiiI9fnBv2s5vxVR7lei/AB3s 1uMACQ+PI+6iyFmVCDjMkxEWKsAVWrQy2ju2WMavL2LNBmcO2Q5AJI0OXI0V7/njMmqt Om0g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=KAMyS3YQ; 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-144260-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-144260-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id g19-20020a1709067c5300b00a46629b7ff4si3749682ejp.966.2024.04.14.09.13.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Apr 2024 09:13:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-144260-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=KAMyS3YQ; 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-144260-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-144260-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 2E5A21F220EB for ; Sun, 14 Apr 2024 16:13:15 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 04DAA1350E8; Sun, 14 Apr 2024 16:13:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="KAMyS3YQ" Received: from mail-ej1-f47.google.com (mail-ej1-f47.google.com [209.85.218.47]) (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 750071350C8 for ; Sun, 14 Apr 2024 16:13:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.47 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713111189; cv=none; b=Eti5H/PQ3iWv6ia7Y7eaTW/mpyp2C5GWUSa2jFWou5cZyU55+Ies53Fgv2hjZ5aycpoYkENSFAX1KT2ii3MfRBpin5KjgYa/YIK9SRz8WIMmY7qXSApB0gJyq7IMGdDe2imWyObMkq997QDAvYdYGKcY/kvhIYEjNmRLOLy53H0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713111189; c=relaxed/simple; bh=9oRFAYGbDDOoaLx4QcYNAokeXsnrRne20HKc37iwr4E=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=WPwuIo7JoCye6b3yWiTJr2ndWb48/03b8CFCpUTU5xZmpUzgAtw3KjipgyFBP2t5zVw9aI8mqpU6J72sxqQJITSe0d1iVaKhC8G7lJaydx4ydyB8v/3/OIFvefAWl744JUwKKd+tcWEYujD3a3nuzwYIVTx8tbT0p4GR4wQT6Vw= 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=KAMyS3YQ; arc=none smtp.client-ip=209.85.218.47 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-ej1-f47.google.com with SMTP id a640c23a62f3a-a523dad53e0so270058466b.1 for ; Sun, 14 Apr 2024 09:13:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713111186; x=1713715986; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=5pb6u2iKzO91RpMLMUiyKz4o6sFnm+wegDY6BclSI7s=; b=KAMyS3YQAQpCQf5XqP/nGKMVNNglguN2oIFIVQXPIOiOvdnhmRIXgYLbjuy3j6xP2w A62jbwSRvX70w5YU7cBQ5ealNgZijxSMs9VO6nSA/mb8cveVUnY5GSoFV7JZCGfKgs/Y bfCx4T46MJhA4KuRTF1bmUuc6pAUJ4QqESPOtiNfS4MhPAGbCpO0MFDJuf0IxGpSLuHv 1QGFgvYXbtKBaPkFOiAiZtuZ/r5DXIXnitAoPRn6Zqiv5XU+rSXMaCtKXrvq+YwcEsRw 9NfnkehNT+f6SSWg8D2uAc8usjpQSjL3iTQuHTs7hAdUnmIhFRVlESqPsNhJ27fLU+0d M5SQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713111186; x=1713715986; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=5pb6u2iKzO91RpMLMUiyKz4o6sFnm+wegDY6BclSI7s=; b=k2uqBKKs3DD3vMgJJEqEXPleHa6zD3FcY5rSgXTmfACK57iZxT7Achb/Bao7X0rFg/ aBa/2+ukCaa1WlRebOSZ6v0lORvUPJLlB3mMbPVNsz1nfFkCIAka28Luk+8YMzu9VU9Q AfzJvy5NrwEpue/cx+2f8nhz9pluDN5Nngwn2LeFdZKdSYXLFbjiKvmvGjZbcuV70GZV XG0OmCjfo7nQfI1R5XwjqvKQ6NMUzsKbVN1lTg2A6MjLR48FNxAwundHpWKjuKxqqqRf AL7BHRv95CiIkKUx1OYQHJNyh5EzNY2szT0CazxVGirhXY+CVfHiH5MgXM90lfYPdnGz 0cNA== X-Forwarded-Encrypted: i=1; AJvYcCVxzS513xCUR0aZkA+6c5Ua0cPUZrHHMm8H2gbmHtt/vgu6UahD5LwwYmEPg8XLNfUilkHx5QXst61Jg1KWgELvTmlYBB3nePQI6IA9 X-Gm-Message-State: AOJu0YzxIhSldTbQCmryR0H6LKDpcaP791TLLQJyh/vfJfgpzo9kHNAY VSxjmFM9MSleTqCKRnmMvOCZe8jQUAsxgkGMJKU8i9NvDYmskdR3 X-Received: by 2002:a17:907:9715:b0:a52:1bba:7458 with SMTP id jg21-20020a170907971500b00a521bba7458mr10378489ejc.0.1713111185429; Sun, 14 Apr 2024 09:13:05 -0700 (PDT) Received: from localhost.localdomain ([46.248.82.114]) by smtp.gmail.com with ESMTPSA id hd7-20020a170907968700b00a524dda47c0sm2085808ejc.143.2024.04.14.09.13.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Apr 2024 09:13:04 -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] locking/atomic/x86: Introduce arch_try_cmpxchg64_local() Date: Sun, 14 Apr 2024 18:12:43 +0200 Message-ID: <20240414161257.49145-1-ubizjak@gmail.com> X-Mailer: git-send-email 2.42.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Introduce arch_try_cmpxchg64_local() for 64-bit and 32-bit targets to improve code using cmpxchg64_local(). On 64-bit targets, the generated assembly improves from: 3e28: 31 c0 xor %eax,%eax 3e2a: 4d 0f b1 7d 00 cmpxchg %r15,0x0(%r13) 3e2f: 48 85 c0 test %rax,%rax 3e32: 0f 85 9f 00 00 00 jne 3ed7 <...> to: 3e28: 31 c0 xor %eax,%eax 3e2a: 4d 0f b1 7d 00 cmpxchg %r15,0x0(%r13) 3e2f: 0f 85 9f 00 00 00 jne 3ed4 <...> where a compare instruction after cmpxchg is saved. The improvements for 32-bit targets are even more noticeable, because double-word compare after cmpxchg8b gets eliminated. 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 | 34 +++++++++++++++++++++++++++++++ arch/x86/include/asm/cmpxchg_64.h | 6 ++++++ 2 files changed, 40 insertions(+) diff --git a/arch/x86/include/asm/cmpxchg_32.h b/arch/x86/include/asm/cmpxchg_32.h index 9e0d330dd5d0..9dedc13d5a77 100644 --- a/arch/x86/include/asm/cmpxchg_32.h +++ b/arch/x86/include/asm/cmpxchg_32.h @@ -64,6 +64,11 @@ static __always_inline bool __try_cmpxchg64(volatile u64 *ptr, u64 *oldp, u64 ne return __arch_try_cmpxchg64(ptr, oldp, new, LOCK_PREFIX); } +static __always_inline bool __try_cmpxchg64_local(volatile u64 *ptr, u64 *oldp, u64 new) +{ + return __arch_try_cmpxchg64(ptr, oldp, new,); +} + #ifdef CONFIG_X86_CMPXCHG64 #define arch_cmpxchg64 __cmpxchg64 @@ -72,6 +77,8 @@ static __always_inline bool __try_cmpxchg64(volatile u64 *ptr, u64 *oldp, u64 ne #define arch_try_cmpxchg64 __try_cmpxchg64 +#define arch_try_cmpxchg64_local __try_cmpxchg64_local + #else /* @@ -150,6 +157,33 @@ static __always_inline bool arch_try_cmpxchg64(volatile u64 *ptr, u64 *oldp, u64 } #define arch_try_cmpxchg64 arch_try_cmpxchg64 +#define __arch_try_cmpxchg64_emu_local(_ptr, _oldp, _new) \ +({ \ + union __u64_halves o = { .full = *(_oldp), }, \ + n = { .full = (_new), }; \ + bool ret; \ + \ + asm volatile(ALTERNATIVE("call cmpxchg8b_emu", \ + "cmpxchg8b %[ptr]", X86_FEATURE_CX8) \ + CC_SET(e) \ + : CC_OUT(e) (ret), \ + [ptr] "+m" (*(_ptr)), \ + "+a" (o.low), "+d" (o.high) \ + : "b" (n.low), "c" (n.high), "S" (_ptr) \ + : "memory"); \ + \ + if (unlikely(!ret)) \ + *(_oldp) = o.full; \ + \ + likely(ret); \ +}) + +static __always_inline bool arch_try_cmpxchg64_local(volatile u64 *ptr, u64 *oldp, u64 new) +{ + return __arch_try_cmpxchg64_emu_local(ptr, oldp, new); +} +#define arch_try_cmpxchg64_local arch_try_cmpxchg64_local + #endif #define system_has_cmpxchg64() 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 c1d6cd58f809..5e241306db26 100644 --- a/arch/x86/include/asm/cmpxchg_64.h +++ b/arch/x86/include/asm/cmpxchg_64.h @@ -20,6 +20,12 @@ arch_try_cmpxchg((ptr), (po), (n)); \ }) +#define arch_try_cmpxchg64_local(ptr, po, n) \ +({ \ + BUILD_BUG_ON(sizeof(*(ptr)) != 8); \ + arch_try_cmpxchg_local((ptr), (po), (n)); \ +}) + union __u128_halves { u128 full; struct { -- 2.42.0