Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp4626758iob; Sun, 8 May 2022 19:44:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwsaJA/AAopGMSTpk3HoeKpxULPx70b8js4kHjpqH/HWYW+d32Q0Z8hVEkIk7ePaGF2FRFO X-Received: by 2002:a17:90b:1d03:b0:1dc:f0d0:f165 with SMTP id on3-20020a17090b1d0300b001dcf0d0f165mr9775632pjb.25.1652064274377; Sun, 08 May 2022 19:44:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652064274; cv=none; d=google.com; s=arc-20160816; b=mMsDqvqRqfqgenCx+i57xToHIFBSnSS7/nQErHlYvAB4sCXDutuQa3A8rgysdZG+lo 5wfWYe/o1Eiv60ACd5CiCi6ysMgzVgT2KHutt+4afm2rpfHEHfrq/OINkQcxwwGdT4Fl C4KFQDgFFKL2xutyOyClS8IO23mZZNhn9uXFCvsrSA5rd7mYpQCeBYOhS8fHhp7c9dAo UnpOgnhwQz/XQ8dY9ykCnDiRagP3t0NH5X2Q2JsfaSDpvrQVzZucdb4gfwkMC37fegJc +A9GVhTWE+3qHgW09mabmqytw4UvAIbmOOspo1X1xBU4pjlsEAI4t5H1Wm3A534owC+/ frqQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=VecPVx+gOd7IXqUBEzPbK7vMdM19l39pG0b6w2ciHW8=; b=kvRT5y3ja4S/txntXiO9kSTszVXRXjy0m/NVDcJT6CmfjLXX45a0A0BM8ZUbG+cq8N wzzY8vcgPq36rNqneR+9ufAqr6QOdI1ppDIgieCIbfqQoGnONuNLPf4BIltn6O/+bjKV BYQXU8TJmEG1Dw3YPz1JyyIs38n9k0lD3CetHFrd9GlJ74DaQwX3ORacvcaXkYas/clw J2Os8QN/p1HD9iqTZXdjx2A5vON6gee2yeaUvCEqGhU9KWp8Vqa9WYh19TkMrPaF4VhV It6KXX7xE8JtLzLUdt5nUyckzQ7i+W8lHpNYQBGoV8Dd8qdh4uSnuiEDaWAOSz8Ys38T CXng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=EUpRYvGb; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id v71-20020a63894a000000b0039cc5344a7csi12409626pgd.273.2022.05.08.19.44.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 May 2022 19:44:34 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=EUpRYvGb; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id D64907B10C; Sun, 8 May 2022 19:44:24 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242415AbiEED7s (ORCPT + 99 others); Wed, 4 May 2022 23:59:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55340 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242381AbiEED7i (ORCPT ); Wed, 4 May 2022 23:59:38 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4EF674A3FD; Wed, 4 May 2022 20:55:54 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id D18FC6135D; Thu, 5 May 2022 03:55:53 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 10CE0C385AE; Thu, 5 May 2022 03:55:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1651722953; bh=PFNl6xABaY2in+HPLdVvyvlQKRywP5jYrBLTCrJwmLQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EUpRYvGbqagkM91KCHl9lnkgT0M4jydo1Y6ta+GWU2KUqBmrdLFm8jStVLcZIFsed a1RIgXuZWvD+s5Y13WJ0IQrQUKU4fwTb5IpgbM3TMbXKpuM8ipOoLuqko9QFs8Ybrc Qh9TnQk9+vMsNht8F/daDwNtQZ8jDPd5wvWbgzB/EgDJtEvTI8ToXhDjmrVbO3puHO oRiB0XGP9uSm6NMcM6QhpwLZ24EMTJuKrLNUpa2Dn6+UsWJ5mCOxIrHOONSpJEvjTo BW1TM8HoViprvZV+l/ZWMdKFMQZAkBcEnkyYClT4Ku7p8JDwg8V/L3DpN/HVdN4r8c zJwNNZFNa3lxQ== From: guoren@kernel.org To: guoren@kernel.org, arnd@arndb.de, palmer@dabbelt.com, mark.rutland@arm.com, will@kernel.org, peterz@infradead.org, boqun.feng@gmail.com, dlustig@nvidia.com, parri.andrea@gmail.com Cc: linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, Guo Ren Subject: [PATCH V4 3/5] riscv: atomic: Add custom conditional atomic operation implementation Date: Thu, 5 May 2022 11:55:24 +0800 Message-Id: <20220505035526.2974382-4-guoren@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220505035526.2974382-1-guoren@kernel.org> References: <20220505035526.2974382-1-guoren@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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: Guo Ren Add conditional atomic operations' custom implementation (similar to dec_if_positive), here is the list: - arch_atomic_inc_unless_negative - arch_atomic_dec_unless_positive - arch_atomic64_inc_unless_negative - arch_atomic64_dec_unless_positive Signed-off-by: Guo Ren Signed-off-by: Guo Ren Cc: Palmer Dabbelt Cc: Mark Rutland Cc: Andrea Parri Cc: Dan Lustig --- arch/riscv/include/asm/atomic.h | 82 +++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/arch/riscv/include/asm/atomic.h b/arch/riscv/include/asm/atomic.h index f3c6a6eac02a..0dfe9d857a76 100644 --- a/arch/riscv/include/asm/atomic.h +++ b/arch/riscv/include/asm/atomic.h @@ -310,6 +310,46 @@ ATOMIC_OPS() #undef ATOMIC_OPS #undef ATOMIC_OP +static __always_inline bool arch_atomic_inc_unless_negative(atomic_t *v) +{ + int prev, rc; + + __asm__ __volatile__ ( + "0: lr.w %[p], %[c]\n" + " bltz %[p], 1f\n" + " addi %[rc], %[p], 1\n" + " sc.w.rl %[rc], %[rc], %[c]\n" + " bnez %[rc], 0b\n" + " fence rw, rw\n" + "1:\n" + : [p]"=&r" (prev), [rc]"=&r" (rc), [c]"+A" (v->counter) + : + : "memory"); + return !(prev < 0); +} + +#define arch_atomic_inc_unless_negative arch_atomic_inc_unless_negative + +static __always_inline bool arch_atomic_dec_unless_positive(atomic_t *v) +{ + int prev, rc; + + __asm__ __volatile__ ( + "0: lr.w %[p], %[c]\n" + " bgtz %[p], 1f\n" + " addi %[rc], %[p], -1\n" + " sc.w.rl %[rc], %[rc], %[c]\n" + " bnez %[rc], 0b\n" + " fence rw, rw\n" + "1:\n" + : [p]"=&r" (prev), [rc]"=&r" (rc), [c]"+A" (v->counter) + : + : "memory"); + return !(prev > 0); +} + +#define arch_atomic_dec_unless_positive arch_atomic_dec_unless_positive + static __always_inline int arch_atomic_dec_if_positive(atomic_t *v) { int prev, rc; @@ -331,6 +371,48 @@ static __always_inline int arch_atomic_dec_if_positive(atomic_t *v) #define arch_atomic_dec_if_positive arch_atomic_dec_if_positive #ifndef CONFIG_GENERIC_ATOMIC64 +static __always_inline bool arch_atomic64_inc_unless_negative(atomic64_t *v) +{ + s64 prev; + long rc; + + __asm__ __volatile__ ( + "0: lr.d %[p], %[c]\n" + " bltz %[p], 1f\n" + " addi %[rc], %[p], 1\n" + " sc.d.rl %[rc], %[rc], %[c]\n" + " bnez %[rc], 0b\n" + " fence rw, rw\n" + "1:\n" + : [p]"=&r" (prev), [rc]"=&r" (rc), [c]"+A" (v->counter) + : + : "memory"); + return !(prev < 0); +} + +#define arch_atomic64_inc_unless_negative arch_atomic64_inc_unless_negative + +static __always_inline bool arch_atomic64_dec_unless_positive(atomic64_t *v) +{ + s64 prev; + long rc; + + __asm__ __volatile__ ( + "0: lr.d %[p], %[c]\n" + " bgtz %[p], 1f\n" + " addi %[rc], %[p], -1\n" + " sc.d.rl %[rc], %[rc], %[c]\n" + " bnez %[rc], 0b\n" + " fence rw, rw\n" + "1:\n" + : [p]"=&r" (prev), [rc]"=&r" (rc), [c]"+A" (v->counter) + : + : "memory"); + return !(prev > 0); +} + +#define arch_atomic64_dec_unless_positive arch_atomic64_dec_unless_positive + static __always_inline s64 arch_atomic64_dec_if_positive(atomic64_t *v) { s64 prev; -- 2.25.1