From nobody Mon Feb 9 12:01:36 2026 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 26234C001DF for ; Mon, 31 Jul 2023 23:17:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231626AbjGaXRY (ORCPT ); Mon, 31 Jul 2023 19:17:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39988 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231864AbjGaXRQ (ORCPT ); Mon, 31 Jul 2023 19:17:16 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B51971BE7 for ; Mon, 31 Jul 2023 16:17:07 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 3DAF761353 for ; Mon, 31 Jul 2023 23:17:07 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9C85BC433CC; Mon, 31 Jul 2023 23:17:06 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.96) (envelope-from ) id 1qQc8P-003fKr-1V; Mon, 31 Jul 2023 19:17:05 -0400 Message-ID: <20230731231705.275332195@goodmis.org> User-Agent: quilt/0.66 Date: Mon, 31 Jul 2023 19:16:36 -0400 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Andrew Morton , Uros Bizjak Subject: [for-next][PATCH 02/15] ring_buffer: Use try_cmpxchg instead of cmpxchg References: <20230731231634.031452225@goodmis.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Uros Bizjak Use try_cmpxchg instead of cmpxchg (*ptr, old, new) =3D=3D old in ring_buffer.c. x86 CMPXCHG instruction returns success in ZF flag, so this change saves a compare after cmpxchg (and related move instruction in front of cmpxchg). No functional change intended. Link: https://lore.kernel.org/linux-trace-kernel/20230714154418.8884-1-ubiz= jak@gmail.com Cc: Steven Rostedt Cc: Masami Hiramatsu Signed-off-by: Uros Bizjak Signed-off-by: Steven Rostedt (Google) --- kernel/trace/ring_buffer.c | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index 52dea5dd5362..78502d4c7214 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -692,10 +692,7 @@ static void rb_time_set(rb_time_t *t, u64 val) static inline bool rb_time_read_cmpxchg(local_t *l, unsigned long expect, unsigned long set) { - unsigned long ret; - - ret =3D local_cmpxchg(l, expect, set); - return ret =3D=3D expect; + return local_try_cmpxchg(l, &expect, set); } =20 static bool rb_time_cmpxchg(rb_time_t *t, u64 expect, u64 set) @@ -752,9 +749,7 @@ static void rb_time_set(rb_time_t *t, u64 val) =20 static bool rb_time_cmpxchg(rb_time_t *t, u64 expect, u64 set) { - u64 val; - val =3D local64_cmpxchg(&t->time, expect, set); - return val =3D=3D expect; + return local64_try_cmpxchg(&t->time, &expect, set); } #endif =20 @@ -1494,14 +1489,11 @@ static bool rb_head_page_replace(struct buffer_page= *old, { unsigned long *ptr =3D (unsigned long *)&old->list.prev->next; unsigned long val; - unsigned long ret; =20 val =3D *ptr & ~RB_FLAG_MASK; val |=3D RB_PAGE_HEAD; =20 - ret =3D cmpxchg(ptr, val, (unsigned long)&new->list); - - return ret =3D=3D val; + return try_cmpxchg(ptr, &val, (unsigned long)&new->list); } =20 /* @@ -3003,7 +2995,6 @@ rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buf= fer, { unsigned long new_index, old_index; struct buffer_page *bpage; - unsigned long index; unsigned long addr; u64 write_stamp; u64 delta; @@ -3060,8 +3051,9 @@ rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buf= fer, */ old_index +=3D write_mask; new_index +=3D write_mask; - index =3D local_cmpxchg(&bpage->write, old_index, new_index); - if (index =3D=3D old_index) { + + /* caution: old_index gets updated on cmpxchg failure */ + if (local_try_cmpxchg(&bpage->write, &old_index, new_index)) { /* update counters */ local_sub(event_length, &cpu_buffer->entries_bytes); return true; --=20 2.40.1