Received: by 2002:a05:7412:3784:b0:e2:908c:2ebd with SMTP id jk4csp2696104rdb; Wed, 4 Oct 2023 08:46:54 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFqFgPpfeo+jlvN5Nyk2Dro/hZA02gxFWKpzNv59OX7k5eHYkECcM421cMW3uxE685IUV9e X-Received: by 2002:a17:902:7084:b0:1b6:649b:92cc with SMTP id z4-20020a170902708400b001b6649b92ccmr2214327plk.69.1696434413782; Wed, 04 Oct 2023 08:46:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696434413; cv=none; d=google.com; s=arc-20160816; b=gdG2PvQjc2IovH9NSSjPacUXcWiAS9wGbbL92po1C0g/qisWy9e6mZfxLAYhx+8/I9 9VT210wQgpyzbwq/Lmb8xrBUQE/mQlzTmDRhGvvPhM10Xr99KSM8lOnVbL4RALehKbJm zEvShtBmxz/KOCFmnbEXVy9A28jX9BQDpl2WpyC4RjcPWot2P6hmhYrExm1QK401d0+1 xpjbaQxiHoaZytGXh5Kkp7JwPWof4ZORk1H0DVDpcydlOUaLAiFxb2JfhEBjaZtYrKwi ueZGmAy9Lfl6kjV/jsJtgI4hzvwIe4TYd4pu2vUTyJGXTxrWuKCKwK+kBaKQE7PjIMHo aPdg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=CHk6/fMFBCJekUniiqNZc91KS/ATUE8I9h6xGyYTE3g=; fh=lC3L+tLu9jMrTwLXdci/D+zTDmYl6oRylGrfIkeVy60=; b=s+t0PTVvy04qdTg41WbC54b847cXMbh99gA/OS4ytqsCY02FZ1HZMr+dntn27xdloo 5zviZdnSP1S+UELVcc14Yjk0tasSvQ858LBYvFntCb3/wcDmb7wR2nzSt4wHuVsM8KMi EVACPd1imrgbCaAahd80AlxNd+OkuR0HjjIaZl1pwKwcoQnTMuMVK4jmY4LyWEwG9ky8 AfnoCBkbNU+JDvBFRJandKWeKOGz8LgvYK9AaOb6neO5NbSdSVkUODyTm/P8G1cj6g/1 sjjiWjwt7CkJzWAcwLcRe07bbOwpEdXVxiMA/bzuNS3lngpZ2F2bZxPG31pkrbsF8Wso cE9A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=rQDJQC0N; dkim=neutral (no key) header.i=@linutronix.de header.b=riQqRqvR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id c4-20020a170903234400b001c5844a4db7si4122590plh.562.2023.10.04.08.46.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Oct 2023 08:46:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=rQDJQC0N; dkim=neutral (no key) header.i=@linutronix.de header.b=riQqRqvR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id CA3FD81B0CEC; Wed, 4 Oct 2023 08:46:50 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233769AbjJDPqm (ORCPT + 99 others); Wed, 4 Oct 2023 11:46:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49274 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233567AbjJDPqm (ORCPT ); Wed, 4 Oct 2023 11:46:42 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CE8DB9E; Wed, 4 Oct 2023 08:46:37 -0700 (PDT) Date: Wed, 04 Oct 2023 15:46:35 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1696434396; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=CHk6/fMFBCJekUniiqNZc91KS/ATUE8I9h6xGyYTE3g=; b=rQDJQC0N8/4hCLN36pERrLQoLZM70YoNq77I2EO2Y20EJvZbX6+oPi8WAC5yIvSHnQOZFD zvG5TtG9WEybfBYXlGbN/XYeGTMBrXl9mr1KTeKg/idGmu3qbrJMkO6PzdBbOMj54/OPt8 Dwk2QJSpckpydHliW+/34azw/ZNfQqtfV1seMxMdhdcyPD+psDIL/tU8gyP5uRrQdObm/o LFND3B+WNKnH9W5bWCp+urTiuIZL5dAaBFpwbelAYJskx6kNkgj+OUT6aA868wbPNbrP+7 Oht5ws01onso/hu1rJ7MI9wOOo2YtXmuyXXDdgcTElLMgbBChUVa95xke5iKsg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1696434396; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=CHk6/fMFBCJekUniiqNZc91KS/ATUE8I9h6xGyYTE3g=; b=riQqRqvRU6KvXMhjP8DZZar/0mHvRCrb0HOPoMWXYkPYN8ei5YRVYucNJgFl24OlclDv3d /h4nUCUFKklK56Cw== From: "tip-bot2 for Uros Bizjak" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: locking/core] locking/local, arch: Rewrite local_add_unless() as a static inline function Cc: Uros Bizjak , Ingo Molnar , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20230731084458.28096-1-ubizjak@gmail.com> References: <20230731084458.28096-1-ubizjak@gmail.com> MIME-Version: 1.0 Message-ID: <169643439535.3135.5571885434602858806.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Wed, 04 Oct 2023 08:46:51 -0700 (PDT) The following commit has been merged into the locking/core branch of tip: Commit-ID: 5e0eb67974e88dbaded765278a3ffe7af33e3b22 Gitweb: https://git.kernel.org/tip/5e0eb67974e88dbaded765278a3ffe7af33e3b22 Author: Uros Bizjak AuthorDate: Mon, 31 Jul 2023 10:42:23 +02:00 Committer: Ingo Molnar CommitterDate: Wed, 04 Oct 2023 11:38:11 +02:00 locking/local, arch: Rewrite local_add_unless() as a static inline function Rewrite local_add_unless() as a static inline function with boolean return value, similar to the arch_atomic_add_unless() arch fallbacks. The function is currently unused. Signed-off-by: Uros Bizjak Signed-off-by: Ingo Molnar Link: https://lore.kernel.org/r/20230731084458.28096-1-ubizjak@gmail.com --- arch/alpha/include/asm/local.h | 33 ++++++++++++++--------------- arch/loongarch/include/asm/local.h | 27 ++++++++++++++---------- arch/mips/include/asm/local.h | 27 ++++++++++++++---------- arch/powerpc/include/asm/local.h | 12 +++++------ arch/x86/include/asm/local.h | 33 ++++++++++++++--------------- 5 files changed, 70 insertions(+), 62 deletions(-) diff --git a/arch/alpha/include/asm/local.h b/arch/alpha/include/asm/local.h index 0fcaad6..88eb398 100644 --- a/arch/alpha/include/asm/local.h +++ b/arch/alpha/include/asm/local.h @@ -65,28 +65,27 @@ static __inline__ bool local_try_cmpxchg(local_t *l, long *old, long new) #define local_xchg(l, n) (xchg_local(&((l)->a.counter), (n))) /** - * local_add_unless - add unless the number is a given value + * local_add_unless - add unless the number is already a given value * @l: pointer of type local_t * @a: the amount to add to l... * @u: ...unless l is equal to u. * - * Atomically adds @a to @l, so long as it was not @u. - * Returns non-zero if @l was not @u, and zero otherwise. + * Atomically adds @a to @l, if @v was not already @u. + * Returns true if the addition was done. */ -#define local_add_unless(l, a, u) \ -({ \ - long c, old; \ - c = local_read(l); \ - for (;;) { \ - if (unlikely(c == (u))) \ - break; \ - old = local_cmpxchg((l), c, c + (a)); \ - if (likely(old == c)) \ - break; \ - c = old; \ - } \ - c != (u); \ -}) +static __inline__ bool +local_add_unless(local_t *l, long a, long u) +{ + long c = local_read(l); + + do { + if (unlikely(c == u)) + return false; + } while (!local_try_cmpxchg(l, &c, c + a)); + + return true; +} + #define local_inc_not_zero(l) local_add_unless((l), 1, 0) #define local_add_negative(a, l) (local_add_return((a), (l)) < 0) diff --git a/arch/loongarch/include/asm/local.h b/arch/loongarch/include/asm/local.h index c496758..f53ea65 100644 --- a/arch/loongarch/include/asm/local.h +++ b/arch/loongarch/include/asm/local.h @@ -70,22 +70,27 @@ static inline bool local_try_cmpxchg(local_t *l, long *old, long new) #define local_xchg(l, n) (atomic_long_xchg((&(l)->a), (n))) /** - * local_add_unless - add unless the number is a given value + * local_add_unless - add unless the number is already a given value * @l: pointer of type local_t * @a: the amount to add to l... * @u: ...unless l is equal to u. * - * Atomically adds @a to @l, so long as it was not @u. - * Returns non-zero if @l was not @u, and zero otherwise. + * Atomically adds @a to @l, if @v was not already @u. + * Returns true if the addition was done. */ -#define local_add_unless(l, a, u) \ -({ \ - long c, old; \ - c = local_read(l); \ - while (c != (u) && (old = local_cmpxchg((l), c, c + (a))) != c) \ - c = old; \ - c != (u); \ -}) +static inline bool +local_add_unless(local_t *l, long a, long u) +{ + long c = local_read(l); + + do { + if (unlikely(c == u)) + return false; + } while (!local_try_cmpxchg(l, &c, c + a)); + + return true; +} + #define local_inc_not_zero(l) local_add_unless((l), 1, 0) #define local_dec_return(l) local_sub_return(1, (l)) diff --git a/arch/mips/include/asm/local.h b/arch/mips/include/asm/local.h index e6ae3df..86fc240 100644 --- a/arch/mips/include/asm/local.h +++ b/arch/mips/include/asm/local.h @@ -108,22 +108,27 @@ static __inline__ bool local_try_cmpxchg(local_t *l, long *old, long new) #define local_xchg(l, n) (atomic_long_xchg((&(l)->a), (n))) /** - * local_add_unless - add unless the number is a given value + * local_add_unless - add unless the number is already a given value * @l: pointer of type local_t * @a: the amount to add to l... * @u: ...unless l is equal to u. * - * Atomically adds @a to @l, so long as it was not @u. - * Returns non-zero if @l was not @u, and zero otherwise. + * Atomically adds @a to @l, if @v was not already @u. + * Returns true if the addition was done. */ -#define local_add_unless(l, a, u) \ -({ \ - long c, old; \ - c = local_read(l); \ - while (c != (u) && (old = local_cmpxchg((l), c, c + (a))) != c) \ - c = old; \ - c != (u); \ -}) +static __inline__ bool +local_add_unless(local_t *l, long a, long u) +{ + long c = local_read(l); + + do { + if (unlikely(c == u)) + return false; + } while (!local_try_cmpxchg(l, &c, c + a)); + + return true; +} + #define local_inc_not_zero(l) local_add_unless((l), 1, 0) #define local_dec_return(l) local_sub_return(1, (l)) diff --git a/arch/powerpc/include/asm/local.h b/arch/powerpc/include/asm/local.h index 45492fb..ec6ced6 100644 --- a/arch/powerpc/include/asm/local.h +++ b/arch/powerpc/include/asm/local.h @@ -115,23 +115,23 @@ static __inline__ long local_xchg(local_t *l, long n) } /** - * local_add_unless - add unless the number is a given value + * local_add_unless - add unless the number is already a given value * @l: pointer of type local_t * @a: the amount to add to v... * @u: ...unless v is equal to u. * - * Atomically adds @a to @l, so long as it was not @u. - * Returns non-zero if @l was not @u, and zero otherwise. + * Atomically adds @a to @l, if @v was not already @u. + * Returns true if the addition was done. */ -static __inline__ int local_add_unless(local_t *l, long a, long u) +static __inline__ bool local_add_unless(local_t *l, long a, long u) { unsigned long flags; - int ret = 0; + bool ret = false; powerpc_local_irq_pmu_save(flags); if (l->v != u) { l->v += a; - ret = 1; + ret = true; } powerpc_local_irq_pmu_restore(flags); diff --git a/arch/x86/include/asm/local.h b/arch/x86/include/asm/local.h index 635132a..73dba8b 100644 --- a/arch/x86/include/asm/local.h +++ b/arch/x86/include/asm/local.h @@ -135,28 +135,27 @@ static inline bool local_try_cmpxchg(local_t *l, long *old, long new) #define local_xchg(l, n) (xchg(&((l)->a.counter), (n))) /** - * local_add_unless - add unless the number is a given value + * local_add_unless - add unless the number is already a given value * @l: pointer of type local_t * @a: the amount to add to l... * @u: ...unless l is equal to u. * - * Atomically adds @a to @l, so long as it was not @u. - * Returns non-zero if @l was not @u, and zero otherwise. + * Atomically adds @a to @l, if @v was not already @u. + * Returns true if the addition was done. */ -#define local_add_unless(l, a, u) \ -({ \ - long c, old; \ - c = local_read((l)); \ - for (;;) { \ - if (unlikely(c == (u))) \ - break; \ - old = local_cmpxchg((l), c, c + (a)); \ - if (likely(old == c)) \ - break; \ - c = old; \ - } \ - c != (u); \ -}) +static __always_inline bool +local_add_unless(local_t *l, long a, long u) +{ + long c = local_read(l); + + do { + if (unlikely(c == u)) + return false; + } while (!local_try_cmpxchg(l, &c, c + a)); + + return true; +} + #define local_inc_not_zero(l) local_add_unless((l), 1, 0) /* On x86_32, these are no better than the atomic variants.