From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pf1-x434.google.com (mail-pf1-x434.google.com [IPv6:2607:f8b0:4864:20::434]) by sourceware.org (Postfix) with ESMTPS id 1F2883858C27 for ; Wed, 17 Nov 2021 02:24:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 1F2883858C27 Received: by mail-pf1-x434.google.com with SMTP id m14so1167163pfc.9 for ; Tue, 16 Nov 2021 18:24:23 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=LBgXWVZp+h6AG3P1ZAhYyvejxY4KlK06glTQ/CnO4OI=; b=V5Cmcgc4ameFqH3qy/+QQJSM8AJ9OZgE0YZBjCnTYyB6YemcOTYjL1duPR2PqP3jUl 8VrSkQ36Ycy7zfxneYsGc9ikPW65GRVjAa+zTbfUKtwXGWmB1atsi9pcNOv6vlXJVMSA SkKqumYutCzzoFRA4yGJnSboQcA7q9Pgo5D0L/fZyNQH2n9M9Y1A6nOOov5cOcREiJXI tpN60SukDcsoSL+H0bR9T9gFg2ea+1naWoT12Jt2kdhEL/tEMLnpZZbTcQgCPbgbYPJ3 FVZQk1BOnDwdxG3IappKWzL3/7pN27/GdJnNs2CCM8ZYSzJIeYfwvUXDVMiparqRwOam gK9w== X-Gm-Message-State: AOAM533pVXuN4m0hy7gw7OLxW0Fuh7dG/M/5aIefUmD3emTmasJBcfs3 Sg7kf6B8O5y9lV6DVOPon9VZwRYOacxs6zUL4N4= X-Google-Smtp-Source: ABdhPJyefz7enW34gTBy8IkYg3At5SPreRDsIxPfQahwrfzXK3xdyva5r9mtSTfrsqGAPz6wKPfP+xPdO9zDUqRnrIw= X-Received: by 2002:aa7:9dcd:0:b0:494:658c:3943 with SMTP id g13-20020aa79dcd000000b00494658c3943mr44692335pfq.19.1637115862286; Tue, 16 Nov 2021 18:24:22 -0800 (PST) MIME-Version: 1.0 References: <20211111162428.2286605-1-hjl.tools@gmail.com> <20211111162428.2286605-2-hjl.tools@gmail.com> In-Reply-To: <20211111162428.2286605-2-hjl.tools@gmail.com> From: Noah Goldstein Date: Tue, 16 Nov 2021 20:24:11 -0600 Message-ID: Subject: Re: [PATCH v6 1/4] Add LLL_MUTEX_READ_LOCK [BZ #28537] To: "H.J. Lu" Cc: GNU C Library , Florian Weimer , Oleh Derevenko , Arjan van de Ven , Andreas Schwab , "Paul A . Clarke" Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 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 autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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-List-Received-Date: Wed, 17 Nov 2021 02:24:27 -0000 On Thu, Nov 11, 2021 at 10:24 AM H.J. Lu wrote: > > CAS instruction is expensive. From the x86 CPU's point of view, getting > a cache line for writing is more expensive than reading. See Appendix > A.2 Spinlock in: > > https://www.intel.com/content/dam/www/public/us/en/documents/white-papers/xeon-lock-scaling-analysis-paper.pdf > > The full compare and swap will grab the cache line exclusive and cause > excessive cache line bouncing. > > Add LLL_MUTEX_READ_LOCK to do an atomic load and skip CAS in spinlock > loop if compare may fail to reduce cache line bouncing on contended locks. > --- > nptl/pthread_mutex_lock.c | 7 +++++++ > 1 file changed, 7 insertions(+) > > diff --git a/nptl/pthread_mutex_lock.c b/nptl/pthread_mutex_lock.c > index 2bd41767e0..72058c719c 100644 > --- a/nptl/pthread_mutex_lock.c > +++ b/nptl/pthread_mutex_lock.c > @@ -64,6 +64,11 @@ lll_mutex_lock_optimized (pthread_mutex_t *mutex) > # define PTHREAD_MUTEX_VERSIONS 1 > #endif > > +#ifndef LLL_MUTEX_READ_LOCK > +# define LLL_MUTEX_READ_LOCK(mutex) \ > + atomic_load_relaxed (&(mutex)->__data.__lock) > +#endif > + > static int __pthread_mutex_lock_full (pthread_mutex_t *mutex) > __attribute_noinline__; > > @@ -141,6 +146,8 @@ PTHREAD_MUTEX_LOCK (pthread_mutex_t *mutex) > break; > } > atomic_spin_nop (); > + if (LLL_MUTEX_READ_LOCK (mutex) != 0) > + continue; Now that the lock spins on a simple read should `max_cnt` be adjusted? https://sourceware.org/git/?p=glibc.git;a=blob;f=nptl/pthread_mutex_lock.c;h=762059b230ba97140d6ca16c7273b489592dd3bc;hb=d672a98a1af106bd68deb15576710cd61363f7a6#l143 > } > while (LLL_MUTEX_TRYLOCK (mutex) != 0); > > -- > 2.33.1 >