From patchwork Sun Oct 1 10:39:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Uros Bizjak X-Patchwork-Id: 13405353 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 3E67BE748F6 for ; Sun, 1 Oct 2023 10:41:35 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.610998.950432 (Exim 4.92) (envelope-from ) id 1qmtsp-0000CY-RJ; Sun, 01 Oct 2023 10:41:07 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 610998.950432; Sun, 01 Oct 2023 10:41:07 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qmtsp-0000CR-Od; Sun, 01 Oct 2023 10:41:07 +0000 Received: by outflank-mailman (input) for mailman id 610998; Sun, 01 Oct 2023 10:41:06 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qmtso-0000CG-Ra for xen-devel@lists.xenproject.org; Sun, 01 Oct 2023 10:41:06 +0000 Received: from mail-ej1-x633.google.com (mail-ej1-x633.google.com [2a00:1450:4864:20::633]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 0581e699-6047-11ee-878e-cb3800f73035; Sun, 01 Oct 2023 12:41:04 +0200 (CEST) Received: by mail-ej1-x633.google.com with SMTP id a640c23a62f3a-99c3c8adb27so2174763966b.1 for ; Sun, 01 Oct 2023 03:41:04 -0700 (PDT) Received: from localhost.localdomain ([46.248.82.114]) by smtp.gmail.com with ESMTPSA id jx14-20020a170906ca4e00b0099caf5bed64sm15333657ejb.57.2023.10.01.03.41.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Oct 2023 03:41:03 -0700 (PDT) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 0581e699-6047-11ee-878e-cb3800f73035 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696156864; x=1696761664; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=hd3IQ5VOgFAiZIrz9J6WO1XKlWIuJJ+vlxIzopklsp0=; b=LgEv3X/KiAiQ2EP9sTRdG6crDMhtnFEh+mRN1JVtnOukCD1JtJWZ9sr6dT/FZGAtI5 /uQ3k9dv17atpZ7uaf1RpXzWbLGPtG1SQa99IsRnKxWMTqCauh4Wh2P4RJ1+vJ16URGe uRbxFuLJoXPNtQq7h7BouSf+1ffGN4OJYSEeDJncfOyRcHWh+MeG6CZUCAThBj4xkEun ctDYY5P6xer+Fg1npDrGdpZCTiUQInbWAlkBXJDluhXgdHuzZ84AgRMhSQ6qduUTL8BR B/TvzP1/D53Nd2HhVNC+mc4Ql5FDnKBG2DJ+/pB1U7mIp3LvAeD07/o8Ms0OgP7J5OrT WFpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696156864; x=1696761664; 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=hd3IQ5VOgFAiZIrz9J6WO1XKlWIuJJ+vlxIzopklsp0=; b=NuhsdhmXowd1TuVY2GAKLw7z3GIHoStc8iwNHlhroeJe8amJoB87CZJfmmNh9tJSHV TDSV2rPvMVNmvvjQ6PivKkJxJltTRmPSF6hgWzESbv1Kg1Py45v/kCfz9ZeAP3Fic8+S ljsEIb5/8kKEDSHoD0IryC4CPaRWcLwaRKdrm+K5qOBZHTKnurTUbkJGgQmOnlgwHOLb AiiT8iEBBgCvnyd8lRmNV5AO3qXaU4C5BYE5q1TXJ7zBqIOi3Vh4/arUbOFQq2v3pSz9 c8SxevBHZJJok9S2Xq/mDIrJnP0P9I4XSGMSgE5BNQ9oj1JyVNro/R0nw05IiOT9ekmb HqYw== X-Gm-Message-State: AOJu0YzHZukMCnGVujhOBiPxLL1vOZw+WEW8Y0Jzk2aKVeBdPYs6qe5q gXANvrYpq3c4VSIgbahn4cs= X-Google-Smtp-Source: AGHT+IG9JZc6TmvZHbVpgjX2iUva5UEpfEXoJsk9uCuk4m2g+BHUY+ApqFLeLJsTAyFlG09evitVDA== X-Received: by 2002:a17:907:774f:b0:9ae:673a:88b9 with SMTP id kx15-20020a170907774f00b009ae673a88b9mr7550136ejc.22.1696156863846; Sun, 01 Oct 2023 03:41:03 -0700 (PDT) From: Uros Bizjak To: linux-kernel@vger.kernel.org, x86@kernel.org, xen-devel@lists.xenproject.org Cc: Uros Bizjak , Will Deacon , Peter Zijlstra , Ingo Molnar , Boqun Feng , Mark Rutland Subject: [PATCH v2 1/3] locking/generic: Add generic support for sync_try_cmpxchg and its fallback Date: Sun, 1 Oct 2023 12:39:09 +0200 Message-ID: <20231001104053.9644-1-ubizjak@gmail.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 Provide the generic sync_try_cmpxchg function from the raw_ prefixed version, also adding explicit instrumentation. The patch amends existing scripts to generate sync_try_cmpxchg locking primitive and its raw_sync_try_cmpxchg fallback, while leaving existing macros from try_cmpxchg family unchanged. The target can define its own arch_sync_try_cmpxchg to override the generic version of raw_sync_try_cmpxchg. This allows the target to generate more optimal assembly than the generic version. Additionally, the patch renames two scripts to better reflect whet they really do. Cc: Will Deacon Cc: Peter Zijlstra Cc: Ingo Molnar Cc: Boqun Feng Cc: Mark Rutland Signed-off-by: Uros Bizjak --- v2: Improve commit description. --- include/linux/atomic/atomic-arch-fallback.h | 15 +++++++++- include/linux/atomic/atomic-instrumented.h | 10 ++++++- scripts/atomic/gen-atomic-fallback.sh | 33 +++++++++++---------- scripts/atomic/gen-atomic-instrumented.sh | 3 +- 4 files changed, 43 insertions(+), 18 deletions(-) diff --git a/include/linux/atomic/atomic-arch-fallback.h b/include/linux/atomic/atomic-arch-fallback.h index b83ef19da13d..5e95faa959c4 100644 --- a/include/linux/atomic/atomic-arch-fallback.h +++ b/include/linux/atomic/atomic-arch-fallback.h @@ -428,6 +428,19 @@ extern void raw_cmpxchg128_relaxed_not_implemented(void); #define raw_sync_cmpxchg arch_sync_cmpxchg +#ifdef arch_sync_try_cmpxchg +#define raw_sync_try_cmpxchg arch_sync_try_cmpxchg +#else +#define raw_sync_try_cmpxchg(_ptr, _oldp, _new) \ +({ \ + typeof(*(_ptr)) *___op = (_oldp), ___o = *___op, ___r; \ + ___r = raw_sync_cmpxchg((_ptr), ___o, (_new)); \ + if (unlikely(___r != ___o)) \ + *___op = ___r; \ + likely(___r == ___o); \ +}) +#endif + /** * raw_atomic_read() - atomic load with relaxed ordering * @v: pointer to atomic_t @@ -4649,4 +4662,4 @@ raw_atomic64_dec_if_positive(atomic64_t *v) } #endif /* _LINUX_ATOMIC_FALLBACK_H */ -// 2fdd6702823fa842f9cea57a002e6e4476ae780c +// eec048affea735b8464f58e6d96992101f8f85f1 diff --git a/include/linux/atomic/atomic-instrumented.h b/include/linux/atomic/atomic-instrumented.h index d401b406ef7c..54d7bbe0aeaa 100644 --- a/include/linux/atomic/atomic-instrumented.h +++ b/include/linux/atomic/atomic-instrumented.h @@ -4998,6 +4998,14 @@ atomic_long_dec_if_positive(atomic_long_t *v) raw_try_cmpxchg128_local(__ai_ptr, __ai_oldp, __VA_ARGS__); \ }) +#define sync_try_cmpxchg(ptr, ...) \ +({ \ + typeof(ptr) __ai_ptr = (ptr); \ + kcsan_mb(); \ + instrument_atomic_read_write(__ai_ptr, sizeof(*__ai_ptr)); \ + raw_sync_try_cmpxchg(__ai_ptr, __VA_ARGS__); \ +}) + #endif /* _LINUX_ATOMIC_INSTRUMENTED_H */ -// 1568f875fef72097413caab8339120c065a39aa4 +// 2cc4bc990fef44d3836ec108f11b610f3f438184 diff --git a/scripts/atomic/gen-atomic-fallback.sh b/scripts/atomic/gen-atomic-fallback.sh index a45154cefa48..f80d69cfeb1f 100755 --- a/scripts/atomic/gen-atomic-fallback.sh +++ b/scripts/atomic/gen-atomic-fallback.sh @@ -223,14 +223,15 @@ gen_xchg_fallbacks() gen_try_cmpxchg_fallback() { + local prefix="$1"; shift local cmpxchg="$1"; shift; - local order="$1"; shift; + local suffix="$1"; shift; cat <