From mboxrd@z Thu Jan 1 00:00:00 1970 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 27961C433FE for ; Tue, 22 Nov 2022 21:14:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234209AbiKVVOy (ORCPT ); Tue, 22 Nov 2022 16:14:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42264 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232491AbiKVVOv (ORCPT ); Tue, 22 Nov 2022 16:14:51 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CD85B2EF78 for ; Tue, 22 Nov 2022 13:14:49 -0800 (PST) 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 251A3618D9 for ; Tue, 22 Nov 2022 21:14:49 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C6FECC433D6; Tue, 22 Nov 2022 21:14:47 +0000 (UTC) Date: Tue, 22 Nov 2022 16:14:46 -0500 From: Steven Rostedt To: Uros Bizjak Cc: linux-kernel@vger.kernel.org, Peter Zijlstra , Josh Poimboeuf , Jason Baron , Ard Biesheuvel Subject: Re: [PATCH] jump_label: use atomic_try_cmpxchg in static_key_slow_inc_cpuslocked Message-ID: <20221122161446.28907755@gandalf.local.home> In-Reply-To: <20221019140850.3395-1-ubizjak@gmail.com> References: <20221019140850.3395-1-ubizjak@gmail.com> X-Mailer: Claws Mail 3.17.8 (GTK+ 2.24.33; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 19 Oct 2022 16:08:50 +0200 Uros Bizjak wrote: > Use atomic_try_cmpxchg instead of atomic_cmpxchg (*ptr, old, new) == old > in static_key_slow_inc_cpuslocked. x86 CMPXCHG instruction returns success > in ZF flag, so this change saves a compare after cmpxchg (and related move > instruction in front of cmpxchg). > > Also, atomic_try_cmpxchg implicitly assigns old *ptr value to "old" when > cmpxchg fails, enabling further code simplifications. > > No functional change intended. > > Cc: Peter Zijlstra > Cc: Josh Poimboeuf > Cc: Jason Baron > Cc: Steven Rostedt > Cc: Ard Biesheuvel > Signed-off-by: Uros Bizjak > --- > kernel/jump_label.c | 8 ++------ > 1 file changed, 2 insertions(+), 6 deletions(-) > > diff --git a/kernel/jump_label.c b/kernel/jump_label.c > index 714ac4c3b556..4d6c6f5f60db 100644 > --- a/kernel/jump_label.c > +++ b/kernel/jump_label.c > @@ -115,8 +115,6 @@ EXPORT_SYMBOL_GPL(static_key_count); > > void static_key_slow_inc_cpuslocked(struct static_key *key) > { > - int v, v1; > - > STATIC_KEY_CHECK_USE(key); > lockdep_assert_cpus_held(); > > @@ -132,11 +130,9 @@ void static_key_slow_inc_cpuslocked(struct static_key *key) > * so it counts as "enabled" in jump_label_update(). Note that > * atomic_inc_unless_negative() checks >= 0, so roll our own. > */ > - for (v = atomic_read(&key->enabled); v > 0; v = v1) { > - v1 = atomic_cmpxchg(&key->enabled, v, v + 1); > - if (likely(v1 == v)) > + for (int v = atomic_read(&key->enabled); v > 0; ) Although it's permitted by the compiler, the kernel style is to not add declarations in conditionals. Please keep the "int v;" at the beginning. > + if (likely(atomic_try_cmpxchg(&key->enabled, &v, v + 1))) I'm fine with this change. -- Steve > return; > - } > > jump_label_lock(); > if (atomic_read(&key->enabled) == 0) {