From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qt1-x82b.google.com (mail-qt1-x82b.google.com [IPv6:2607:f8b0:4864:20::82b]) by sourceware.org (Postfix) with ESMTPS id C7EAA385C401 for ; Sat, 1 Oct 2022 04:13:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C7EAA385C401 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-qt1-x82b.google.com with SMTP id c11so3794522qtw.8 for ; Fri, 30 Sep 2022 21:13:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=M5vxv/Iy/TBEHNOwe+gjtu/JEOuUFK0N5+uNxJOXcws=; b=THX5C1UZi4JV3Yqd74hO6Y1Ikjqina7LolW/hOA41AkaAD3mEsC9mx2RDCx70KoJb3 yVC67g9KALt3lsjUBwQfZW5+1+aBVuzoCsHQm7nChHLUFfCcpjuA3WR/Jwcd6ozBVvpf t1qaPQ9XjNfBOSkyXWOXl8nyhQUfrYJOSGa1T+oDbnEE1zRKkFcsx5OseEixKRTar6FX lDtfRoU6xDVbT9ft5LrvvMZlzwj2gYuZEUywl7MuN6VLNFxk48QQf6bFDBs8qvxc585i xhDaiD6C36o3ckstcQ63tgY77lhU8vTRtPf0V0pQQ1odeT3N+ZGILruwcr4HxKkikXcm npJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=M5vxv/Iy/TBEHNOwe+gjtu/JEOuUFK0N5+uNxJOXcws=; b=ICg/VvoZs3Z9qST8/95uqdqY1+enUADFxu8A3H8GYo5fJHwYjzPsjKCEeTTskqIiFe 4glOSfNGnZYoCG3Mq/r+j440VxJpGavWBRvtGZE121Xqb3/O9cttYVSYUGKq/aYAdx4I ROwm1UT8lU08YVAjV+8TqWEQRYDk9vXfjsVnu/FtVS7p5BTFa3RGaxpFImSJDRvV08lt 7Fj4Rgnw5euBKFHwOmfHq3BcVkz7vp88Qt9HCxRlzhxRh5GeJ+lMsdiERm3QUh3NYOdv zylMMINj1b0tXVyDfbYPoPM0fnPqlHiFhkVqJ22le9w8WV07jZBNvW2jUYuWP7EUj5N4 6q/g== X-Gm-Message-State: ACrzQf2qACRF/DFaE48t47cVcPI+KcdIzp84MpjgSiG6VnPstIBTH/iZ QLtJV0yYjsQBeHJ9qhPdICYffLBs5E0= X-Google-Smtp-Source: AMsMyM5n72x3qVNlldtgEK5p3AKLDJILjBdBHazRPhcm1bpzYW7k7o2KfVFMJYiXYaluIVKwwEYmvQ== X-Received: by 2002:ac8:7d55:0:b0:35c:d884:449b with SMTP id h21-20020ac87d55000000b0035cd884449bmr9201554qtb.446.1664597610738; Fri, 30 Sep 2022 21:13:30 -0700 (PDT) Received: from noah-tgl.. (cpe-74-64-106-10.nyc.res.rr.com. [74.64.106.10]) by smtp.gmail.com with ESMTPSA id o2-20020ac87c42000000b0035cebb79aaesm3896785qtv.18.2022.09.30.21.13.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Sep 2022 21:13:30 -0700 (PDT) From: Noah Goldstein To: libc-alpha@sourceware.org Cc: goldstein.w.n@gmail.com, hjl.tools@gmail.com, carlos@systemhalted.org Subject: [PATCH v2 2/2] x86: Cleanup pthread_spin_{try}lock.S Date: Fri, 30 Sep 2022 21:13:27 -0700 Message-Id: <20221001041327.1133757-2-goldstein.w.n@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221001041327.1133757-1-goldstein.w.n@gmail.com> References: <20221001023337.1127793-2-goldstein.w.n@gmail.com> <20221001041327.1133757-1-goldstein.w.n@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.3 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,URIBL_BLACK autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Save a jmp on the lock path coming from an initial failure in pthread_spin_lock.S. This costs 4-bytes of code but since the function still fits in the same number of 16-byte blocks (default function alignment) it does not have affect on the total binary size of libc.so (unchanged after this commit). pthread_spin_trylock was using a CAS when a simple xchg works which is often more expensive. Full check passes on x86-64. --- sysdeps/x86_64/nptl/pthread_spin_lock.S | 23 +++++++++++++++------- sysdeps/x86_64/nptl/pthread_spin_trylock.S | 18 ++++++++++++----- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/sysdeps/x86_64/nptl/pthread_spin_lock.S b/sysdeps/x86_64/nptl/pthread_spin_lock.S index 44b837d9db..1e09e59b10 100644 --- a/sysdeps/x86_64/nptl/pthread_spin_lock.S +++ b/sysdeps/x86_64/nptl/pthread_spin_lock.S @@ -19,18 +19,27 @@ #include ENTRY(__pthread_spin_lock) -1: LOCK - decl 0(%rdi) - jne 2f + /* Always return zero. */ xor %eax, %eax + LOCK + decl 0(%rdi) + jne 1f ret .align 16 -2: rep +1: + /* `rep nop` == `pause`. */ + rep nop - cmpl $0, 0(%rdi) - jg 1b - jmp 2b + cmpl %eax, 0(%rdi) + jle 1b + /* Just repeat the `lock decl` logic here. The code size save + of jumping back to entry doesn't change how many 16-byte + chunks (default function alignment) that the code fits in. */ + LOCK + decl 0(%rdi) + jne 1b + ret END(__pthread_spin_lock) versioned_symbol (libc, __pthread_spin_lock, pthread_spin_lock, GLIBC_2_34) diff --git a/sysdeps/x86_64/nptl/pthread_spin_trylock.S b/sysdeps/x86_64/nptl/pthread_spin_trylock.S index fffdb27dd9..a1f97cb420 100644 --- a/sysdeps/x86_64/nptl/pthread_spin_trylock.S +++ b/sysdeps/x86_64/nptl/pthread_spin_trylock.S @@ -20,13 +20,21 @@ #include ENTRY(__pthread_spin_trylock) - movl $1, %eax xorl %ecx, %ecx - lock - cmpxchgl %ecx, (%rdi) + /* xchg has implicit LOCK prefix. */ + xchgl %ecx, (%rdi) + + /* Branch on result. Expectation is the use of trylock will be + branching on success/failure so this branch can be used to + to predict the coming branch. It has the benefit of + breaking the likely expensive memory dependency on (%rdi). */ + cmpl $1, %ecx + jnz 1f + xorl %eax, %eax + ret +1: movl $EBUSY, %eax - cmovel %ecx, %eax - retq + ret END(__pthread_spin_trylock) versioned_symbol (libc, __pthread_spin_trylock, pthread_spin_trylock, GLIBC_2_34) -- 2.34.1