Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp1143689rwe; Thu, 25 Aug 2022 16:49:33 -0700 (PDT) X-Google-Smtp-Source: AA6agR61XuF1NtunOIrJD0bz0GRiWOZBbL11o2xglF3KLM5APcPb/1/2F3Zb2Cp9tohsq2VzYWBl X-Received: by 2002:a05:6402:43c3:b0:445:db76:de71 with SMTP id p3-20020a05640243c300b00445db76de71mr4989804edc.218.1661471373086; Thu, 25 Aug 2022 16:49:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661471373; cv=none; d=google.com; s=arc-20160816; b=Yi8G498IKExDXHFcoQJIGf5Js4E3YJNC/1Gvab5G1d+wF6w6WkrZPjXqtCiOLWcRbL whuXea3s+89D0/19SOUg9/0ncQodFqTKjeSXf3mHg0rRc7LPIjMpK7ZR1zJ6SkwxxnJf 3dHcc3BqbQXNzyBUaf8ZKE9JMmJXNNHq3NFZwgQOtRLP8sZVlDFdKFtKohA5gt3sicYR r7DTCo4D45du4tXb8VmQaeD9erpd17/z1NwNjC8WiTpIGXOryd2gLQSYJtde+FkWBGkO aLXb5VyM6KE4TZR+wvL9vR0fClhMFhweAzsw1gwuu5NYGh49sMzPH6n/9YAlbPq9nPXO o7Fg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:reply-to:dkim-signature; bh=a5nruIl0wNeupXvrAMbGBAS1GScaEkKaYOUNnuHLnXk=; b=Koe+iySJwiPpKHHH+Qkfb1ONCThn7LefZr7Ow8izCuhpNyEox33fsy6dqRmPejnQu1 +Hp9aXmbvs610oEoKlv4x4hBaYrBfv5JaYr5XMM8alZLgyHIOIfQSn5+8ZJp8AhTqjPi ZtXmOOufpL0Z+lKtpDta5ou9JcDQFri9rtEq99wAOWEtAUTz/PSKtl9JQNHfLm3Mtg7N j29rwrRMkdKUfZAYeFrzXh4BbK2bl5hMy8JcRCxQeEbVWJ1ei+RCdulZSWjlKWFHlpnj KJrYtq+UxyiBB+kkajfgO+6mwnfdgu0Jr6h50Sj7c6+jcRYOSJKrg8sTOd+X2h2LllRv Np8A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=MUrthn9a; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g10-20020a170906594a00b007313312730esi316850ejr.85.2022.08.25.16.49.07; Thu, 25 Aug 2022 16:49:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=MUrthn9a; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244573AbiHYXZz (ORCPT + 99 others); Thu, 25 Aug 2022 19:25:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39708 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244112AbiHYXZc (ORCPT ); Thu, 25 Aug 2022 19:25:32 -0400 Received: from mail-pf1-x449.google.com (mail-pf1-x449.google.com [IPv6:2607:f8b0:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 010A457552 for ; Thu, 25 Aug 2022 16:25:31 -0700 (PDT) Received: by mail-pf1-x449.google.com with SMTP id bd20-20020a056a00279400b0053609a39408so8340219pfb.3 for ; Thu, 25 Aug 2022 16:25:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc; bh=a5nruIl0wNeupXvrAMbGBAS1GScaEkKaYOUNnuHLnXk=; b=MUrthn9a6RY6CpiffJkHt9EOPrKtSEGmXhqqxQ3D2qRZw+a7bNGa46Lw6wQ3BknnIy Nccq5f/lszbKbQJgrG/Y3TeW7LcYWHSCnV1949i35B4xTqNhw6TutpLsyYtPkh4tpQun 8YqYEvHVdG6s/enTMEeqcr2tgcITSa5HsHiuHtd8k1okdf8J78W/3/sDX30h3JhVfrmf sDq9IBDxUOd9h4P2X17g89DICK0NQZ8O2oP1O3SUmdgBnpZE5Bd1EVN61k53zguEnNjY zxzQgf5iHjst9fKoqExYlsFYRa7aqRLWkLtfs18aJWmLVPp8Pj+CMrtVB/3l2rPzHLPo UUPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc; bh=a5nruIl0wNeupXvrAMbGBAS1GScaEkKaYOUNnuHLnXk=; b=W9PLOCU5hAhCjap3ixl/9a6GA4uD572vIHipiUdFi2YD9xmw4CVgJiy4v+3BQlmbd3 +YbvQesGg6ZZ//JeRQG+FgPjtil6TR0Ns5Qg2dGA6s92pQ8JS8eiUeBqEVOeaDlf5fS8 n7BKd/kaAjwumMLBe180S6NEMDMd0fA4fH8CqmUEh7TqvDSLYgH9eIUXckLRaQTLqyUu mWrIIrGiibPkdr7MoCUlHausRjN5KRmj8Q2RAWjheXYHbXQb6MI/18PjvL0Uq4Wl6ZQA AgKXmnxwFG89zeqLRYJgcNHiYyjRBBFw0csWKdggwrT0rdsz3l4aET3DGIRIVSoQp5q2 WlEQ== X-Gm-Message-State: ACgBeo1/HNN2JuQ7TQk4nxvg8TycjFrNj34ILDY1It5Jm8yA2q237XV+ dLlQnRXnGMSjKc0tHNA6i1wUeuyELS8= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:f542:b0:173:a8a:d7bf with SMTP id h2-20020a170902f54200b001730a8ad7bfmr1185847plf.134.1661469931377; Thu, 25 Aug 2022 16:25:31 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 25 Aug 2022 23:25:19 +0000 In-Reply-To: <20220825232522.3997340-1-seanjc@google.com> Mime-Version: 1.0 References: <20220825232522.3997340-1-seanjc@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220825232522.3997340-5-seanjc@google.com> Subject: [PATCH v5 4/7] tools: Add atomic_test_and_set_bit() From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Nathan Chancellor , Nick Desaulniers Cc: James Morse , Alexandru Elisei , Suzuki K Poulose , Oliver Upton , Atish Patra , David Hildenbrand , Tom Rix , kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, llvm@lists.linux.dev, linux-kernel@vger.kernel.org, Colton Lewis , Peter Gonda , Andrew Jones , Sean Christopherson Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Peter Gonda Add x86 and generic implementations of atomic_test_and_set_bit() to allow KVM selftests to atomically manage bitmaps. Note, the generic version is taken from arch_test_and_set_bit() as of commit 415d83249709 ("locking/atomic: Make test_and_*_bit() ordered on failure"). Signed-off-by: Peter Gonda Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson --- tools/arch/x86/include/asm/atomic.h | 7 +++++++ tools/include/asm-generic/atomic-gcc.h | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/tools/arch/x86/include/asm/atomic.h b/tools/arch/x86/include/asm/atomic.h index 1f5e26aae9fc..01cc27ec4520 100644 --- a/tools/arch/x86/include/asm/atomic.h +++ b/tools/arch/x86/include/asm/atomic.h @@ -8,6 +8,7 @@ #define LOCK_PREFIX "\n\tlock; " +#include #include /* @@ -70,4 +71,10 @@ static __always_inline int atomic_cmpxchg(atomic_t *v, int old, int new) return cmpxchg(&v->counter, old, new); } +static inline int atomic_test_and_set_bit(long nr, unsigned long *addr) +{ + GEN_BINARY_RMWcc(LOCK_PREFIX __ASM_SIZE(bts), *addr, "Ir", nr, "%0", "c"); + +} + #endif /* _TOOLS_LINUX_ASM_X86_ATOMIC_H */ diff --git a/tools/include/asm-generic/atomic-gcc.h b/tools/include/asm-generic/atomic-gcc.h index 4c1966f7c77a..6daa68bf5b9e 100644 --- a/tools/include/asm-generic/atomic-gcc.h +++ b/tools/include/asm-generic/atomic-gcc.h @@ -4,6 +4,7 @@ #include #include +#include /* * Atomic operations that C can't guarantee us. Useful for @@ -69,4 +70,15 @@ static inline int atomic_cmpxchg(atomic_t *v, int oldval, int newval) return cmpxchg(&(v)->counter, oldval, newval); } +static inline int atomic_test_and_set_bit(long nr, unsigned long *addr) +{ + unsigned long mask = BIT_MASK(nr); + long old; + + addr += BIT_WORD(nr); + + old = __sync_fetch_and_or(addr, mask); + return !!(old & mask); +} + #endif /* __TOOLS_ASM_GENERIC_ATOMIC_H */ -- 2.37.2.672.g94769d06f0-goog