From patchwork Sat Oct 1 02:33:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Noah Goldstein X-Patchwork-Id: 58247 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E93A03858439 for ; Sat, 1 Oct 2022 02:34:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E93A03858439 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1664591687; bh=M5vxv/Iy/TBEHNOwe+gjtu/JEOuUFK0N5+uNxJOXcws=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=FZ5/K3fJyDz3rn5CeYqNNWhR9rBAmSzBcoDbVXWKLDrfuA8HhTq/oCSYefxPr+nKJ plrH7kGiwrepX/rQBl2n+mi4FrOL87yYypPQLISHylPdWCLoWLBvyYBw+q5mAQ7neq 8WLaVVSCE+QIiSDJRTKt4elwlpm4HU6TJtN4cwDI= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qk1-x72c.google.com (mail-qk1-x72c.google.com [IPv6:2607:f8b0:4864:20::72c]) by sourceware.org (Postfix) with ESMTPS id 8BB7B3858D38 for ; Sat, 1 Oct 2022 02:33:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 8BB7B3858D38 Received: by mail-qk1-x72c.google.com with SMTP id g2so3917572qkk.1 for ; Fri, 30 Sep 2022 19:33:42 -0700 (PDT) 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=cjWJi9GtKftBgGPqnvGt4ypuhQYQH1FGxQ+yDOGAhFQd4DutOqhZXHTyp4tWRpDujh XSuuyBxgPSnk0GY4Uow0d7cfsFcCts8DLAjY1mug8qcTaOcmKWqz9J+LhrZjFE5uNotl QwWlfFVxXPkftvBelVvbJrpA2i3Nz+JzPXPOcIXRamcdFZooeNQBbqq5iElsBEXHXyNa ceKgsv5o2oxCafCThuuAtgcwxADNFQp0u0SFR9cl2lTy7IexLWumGucUI7q5b2F68ICA AxqrROIsFlG6E51ZDLn3ehtP9tcQHupashM7pVV8VjfT7o+auzhLl7fMC7I+AKRpsPzP sjWQ== X-Gm-Message-State: ACrzQf2wwbfNpgb8uUci3L+4N/+HlWjtE89YNkPThzwn+9YKiOqYY3Ed XDQCm9YQ6F1aWNDn+jSACJUrncgoNMY= X-Google-Smtp-Source: AMsMyM5w34wMHtPvAPynlNbQzhUDTMljLqjlGu5KtmVr0/PCOJhU66uu/T8vQ6FThJnCqLTiUR/1Cg== X-Received: by 2002:ae9:edc6:0:b0:6ce:7697:6e03 with SMTP id c189-20020ae9edc6000000b006ce76976e03mr8246560qkg.314.1664591621506; Fri, 30 Sep 2022 19:33:41 -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 b19-20020ac86793000000b0035bbb6268e2sm3059115qtp.67.2022.09.30.19.33.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Sep 2022 19:33:41 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v1 2/2] x86: Optimize pthread_spin_{try}lock.S Date: Fri, 30 Sep 2022 19:33:37 -0700 Message-Id: <20221001023337.1127793-2-goldstein.w.n@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221001023337.1127793-1-goldstein.w.n@gmail.com> References: <20221001023337.1127793-1-goldstein.w.n@gmail.com> MIME-Version: 1.0 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 X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Noah Goldstein via Libc-alpha From: Noah Goldstein Reply-To: Noah Goldstein Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" 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)