Message ID | 20230509150255.3691-1-ubizjak@gmail.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2979413vqo; Tue, 9 May 2023 08:38:07 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4CNwfvM73iL24BV0IhDkGNh2oM/OBvs/22m8bLTAkIcxRptkL+XlYQwSkMBK4+70xnrk9u X-Received: by 2002:a05:6a20:3d16:b0:101:5ef3:1a02 with SMTP id y22-20020a056a203d1600b001015ef31a02mr2436246pzi.7.1683646686797; Tue, 09 May 2023 08:38:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683646686; cv=none; d=google.com; s=arc-20160816; b=mZXd19xNDLQAcPcwLbi87BGXmgEBc8X5J4LXaU3LJAaLsGL+4FKHy+jyWcc2pl5eh8 qc/o9pUKId1Xa4A1qhwmK76cPRMwPvBzwbGwGcZDayNCU552yRzS84KrVVP48KC5xkKw UUZk7wYtJ/oOFnQg1wf8pl5co5u17GrcF3qWcpwsaaHuJF2MpJKaNxjjohNPgZh8AOFi +a8gnCLs5QkfuQvpPK5NVCYvZhE8WZIDC67/z0IPglXbd0Q/+rHAFoStGqpedAgSe4JY Q813UVuMPLASMqZWt1SeIlfseSGjrSsIjogj7qpd4CDdO2UvWa6bMo7aoNMiAg9CVuFY zwtg== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=46sFLqQARW3IRka2wIs9+b0QgXDTHZ4c0a8iYuva5as=; b=wePG88AYAVo9Go8vDqH9ueZ+iBa4KIh8YWWnGP4xLPjsu1Fg9hSX98Wnobdw7KfL3h 7+t0wI/3cL8cCK14ZGyx273FH7FwZxbL5741hvjlqQ1BVfA9TFhKFmNkGi9vqVj/EyLq /GY5TLneUY5p7PVuKkkQetyHMoqaBuVIQZJ/q6lbfDx2xC2s1fSwGmj3RaPfxNqc9TOh mXA4gkboBd/vYvo+Nkj+9Xuyv6RR7ALUsMch5vFQREphnujzSarlWsnvp/8H+QFMBnM2 69Uu/Gk0VljaGr43fk0P2mil8MknS4+HOMcdD8gGj6qkgsJraF1cXizquzUkZl+AprSm LefA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b="Wh+N/EPo"; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s136-20020a63778e000000b00517cbb33156si1661053pgc.720.2023.05.09.08.37.51; Tue, 09 May 2023 08:38:06 -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=@gmail.com header.s=20221208 header.b="Wh+N/EPo"; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236027AbjEIPDL (ORCPT <rfc822;baris.duru.linux@gmail.com> + 99 others); Tue, 9 May 2023 11:03:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33268 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236023AbjEIPDJ (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Tue, 9 May 2023 11:03:09 -0400 Received: from mail-ed1-x535.google.com (mail-ed1-x535.google.com [IPv6:2a00:1450:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E630126AF for <linux-kernel@vger.kernel.org>; Tue, 9 May 2023 08:03:05 -0700 (PDT) Received: by mail-ed1-x535.google.com with SMTP id 4fb4d7f45d1cf-50bc070c557so11650076a12.0 for <linux-kernel@vger.kernel.org>; Tue, 09 May 2023 08:03:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1683644584; x=1686236584; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=46sFLqQARW3IRka2wIs9+b0QgXDTHZ4c0a8iYuva5as=; b=Wh+N/EPo5Q2lusjsLbbBW5M8V1bRio5DAOMldUnmGXDdfE7Va8Kg1eQLtM0moeTEAU Cg/RYaT6oriq8V4i5jKHvZ+Ew2LA1k7dXkxY6GbXL/k36CTmg8n/yPhSeZ1mLGycxe2D 1CIpeF6I+Vs8mPtjJOggrMDzVAPSGieM8lqf0YK4JnPmtwU9S8+XzKJsLLfy94RNlQt5 lBs2RbX8gl7QcyajscKQMcIQPzzqMYdPw4SV3ej5B8tkzuR9pWFu4H6uPswP+kDvrbAx ahGtNqYo3emb5yQi+iJjJEw3UphmQpOn0SRiV3OhFDy6EUXf6oS+KGEnQliwwCM9v6qt n9ug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683644584; x=1686236584; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=46sFLqQARW3IRka2wIs9+b0QgXDTHZ4c0a8iYuva5as=; b=Zv3e3iNXWh/CA0d5t6VC9JFPNsu1eAtcX5h0oGFEVGM/+Uhl5I42ieuZcNaVxJT4HL 56599cSXWQ4A+85mDtbXQ89Z3S7yKu1r7M9FSEjEaHDkWBMoklNTKES0HPxhBzmRzTqf B2eZhkV+IJ7LKBuG4Qdq086l4SdufTkcqRjKACelS+WeOq8mFeI4aNcV92RmFfmtartg dlAE6e+QdVMF2PxWouUW8bMCvjZzgfD2/8Ml75lo06Fo2s37i4mMBPfILznKOphCCvNc CU/rtEZ78exRhMF9cD3iO0xdiDU8xlIVP/t4liw1EjUuGbIFVvCAdMQysqFfXNBUzlmD w2mg== X-Gm-Message-State: AC+VfDxlG6Vl6QrI8SYHf3Csp8QF911AdTASG/gebA/mK+JqFxCAB/ds ued9lZY3y8efaCS9SLYx0ErD7tUknCkxTQ== X-Received: by 2002:a17:906:db08:b0:965:fa80:bf1 with SMTP id xj8-20020a170906db0800b00965fa800bf1mr11672958ejb.32.1683644583918; Tue, 09 May 2023 08:03:03 -0700 (PDT) Received: from localhost.localdomain ([46.248.82.114]) by smtp.gmail.com with ESMTPSA id ku11-20020a170907788b00b0094f5179540fsm1401749ejc.83.2023.05.09.08.03.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 May 2023 08:03:03 -0700 (PDT) From: Uros Bizjak <ubizjak@gmail.com> To: linux-kernel@vger.kernel.org Cc: Uros Bizjak <ubizjak@gmail.com>, Thomas Gleixner <tglx@linutronix.de> Subject: [PATCH] atomics: Use atomic_try_cmpxchg_release in rcuref_put_slowpath() Date: Tue, 9 May 2023 17:02:55 +0200 Message-Id: <20230509150255.3691-1-ubizjak@gmail.com> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765431508408998851?= X-GMAIL-MSGID: =?utf-8?q?1765431508408998851?= |
Series |
atomics: Use atomic_try_cmpxchg_release in rcuref_put_slowpath()
|
|
Commit Message
Uros Bizjak
May 9, 2023, 3:02 p.m. UTC
Use atomic_try_cmpxchg instead of atomic_cmpxchg (*ptr, old, new) == old
in rcuref_put_slowpath(). 86 CMPXCHG instruction returns success in
ZF flag, so this change saves a compare after cmpxchg. Additionaly,
the compiler reorders some code blocks to follow likely/unlikely
annotations in the atomic_try_cmpxchg macro, improving the code from
9a: f0 0f b1 0b lock cmpxchg %ecx,(%rbx)
9e: 83 f8 ff cmp $0xffffffff,%eax
a1: 74 04 je a7 <rcuref_put_slowpath+0x27>
a3: 31 c0 xor %eax,%eax
to
9a: f0 0f b1 0b lock cmpxchg %ecx,(%rbx)
9e: 75 4c jne ec <rcuref_put_slowpath+0x6c>
a0: b0 01 mov $0x1,%al
No functional change intended.
Cc: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Uros Bizjak <ubizjak@gmail.com>
---
lib/rcuref.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Comments
From: Uros Bizjak > Sent: 09 May 2023 16:03 > > Use atomic_try_cmpxchg instead of atomic_cmpxchg (*ptr, old, new) == old > in rcuref_put_slowpath(). 86 CMPXCHG instruction returns success in > ZF flag, so this change saves a compare after cmpxchg. Additionaly, > the compiler reorders some code blocks to follow likely/unlikely > annotations in the atomic_try_cmpxchg macro, improving the code from > > 9a: f0 0f b1 0b lock cmpxchg %ecx,(%rbx) > 9e: 83 f8 ff cmp $0xffffffff,%eax > a1: 74 04 je a7 <rcuref_put_slowpath+0x27> > a3: 31 c0 xor %eax,%eax > > to > > 9a: f0 0f b1 0b lock cmpxchg %ecx,(%rbx) > 9e: 75 4c jne ec <rcuref_put_slowpath+0x6c> > a0: b0 01 mov $0x1,%al > > No functional change intended. While I'm not against the change I bet you can't detect any actual difference. IIRC: - The 'cmp+je' get merged into a single u-op. - The 'lock cmpxchg' will take long enough that the instruction decoder won't be a bottleneck. - Whether the je/jne is predicted taken is pretty much random. So you'll speculatively execute somewhere (could be anywhere) while the locked cycle completes. So the only change is three less bytes of object code. That will change the cache line alignment of later code. David - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
diff --git a/lib/rcuref.c b/lib/rcuref.c index 5ec00a4a64d1..97f300eca927 100644 --- a/lib/rcuref.c +++ b/lib/rcuref.c @@ -248,7 +248,7 @@ bool rcuref_put_slowpath(rcuref_t *ref) * require a retry. If this fails the caller is not * allowed to deconstruct the object. */ - if (atomic_cmpxchg_release(&ref->refcnt, RCUREF_NOREF, RCUREF_DEAD) != RCUREF_NOREF) + if (!atomic_try_cmpxchg_release(&ref->refcnt, &cnt, RCUREF_DEAD)) return false; /*