From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ed1-x535.google.com (mail-ed1-x535.google.com [IPv6:2a00:1450:4864:20::535]) by sourceware.org (Postfix) with ESMTPS id A3E783858424; Thu, 29 Sep 2022 00:11:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A3E783858424 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-ed1-x535.google.com with SMTP id a13so4188765edj.0; Wed, 28 Sep 2022 17:11:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date; bh=2eiowsXB0ZcQX2KWvqsjea3Xw39cAD/4NqI7RUb4rBI=; b=RIS2tdoIU/xAAyY4WHfPLdwQV4PE66tmQwXQYcEL4b+OqIt9RjXqZCWyBI+ey4c80v cJRFLIUnbRSrBjn3AlWi5qb4DjVadhvugpUj02SJrARR3n98gOZY4mdvQjcu3779fhQz FDyyMRd5v/SesMmUg+fotcCaMx1aoZhTakijdLLdQ78/fQmC0klCoyIdw1/IRZ6Trc3y wWTckfbZkk10YZTeus52Zc682PmQ65yfC7OzqZ6DkYHQ6nj+FAqFhrgJbOKcE7QpuUke 9RagWfqCkvWKE8C2cIlc841NAfGbibSXC59P+kAQ/vk7+dKmEiVuXisohfJWQgbrgzJ1 0B0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date; bh=2eiowsXB0ZcQX2KWvqsjea3Xw39cAD/4NqI7RUb4rBI=; b=bFTpyvDxtK1SFUzcPJOvy+chK6dpkCqPhPNgMQzrev2XVjBftb/PnHyjQFA7VjmPrK odon2mIqKhLtlqT67odWjjAufc6nQ/iy/lUm21ZgDPcGy3zOiiCFW1l7aupYaPiVSLdr EkesNGpAfahIaZ9e1brNccEeCGti+Lh3q81d236SPPMrCkxS7mKRCzHtxGvbYhjtEA1g 9Vm/Psrip8PAits6rCgrJQx+9VAALaJqKmkgKAjGos0Waz/npQeYqRQKx4XQjG7WBTiP DR2Tc/41TrjFQUeAKYdJ3CoeX4zcW+nUGCuQsE0t0ntli4SAwA7FRxMrOUxWNexcB3Yi IIUA== X-Gm-Message-State: ACrzQf2g5NM9AvvuOP8G8srgfSiNG59ILYkKxPiiv7OTwDos09h0TX/u hl2jTUqVrSrVaK7YrC8165pBpXS6/O9ZRiMcIJa68tKS X-Google-Smtp-Source: AMsMyM4VU4ymVuLEtT9daOijM+mJAcw9JEvxeRI+yYgDVNpMdgKWv9yqC/jpZLL24YYSvtpBKdzLqiOC54/pB+XocBA= X-Received: by 2002:a05:6402:3596:b0:450:c4d9:a04b with SMTP id y22-20020a056402359600b00450c4d9a04bmr599708edc.218.1664410291529; Wed, 28 Sep 2022 17:11:31 -0700 (PDT) MIME-Version: 1.0 References: <20211214123051.800-1-6812skiii@gmail.com> In-Reply-To: From: Noah Goldstein Date: Wed, 28 Sep 2022 20:11:20 -0400 Message-ID: Subject: Re: [PATCH v4] nptl: Effectively skip CAS in spinlock loop To: Sunil Pandey Cc: "H.J. Lu" , Libc-stable Mailing List , Florian Weimer , Jangwoong Kim <6812skiii@gmail.com>, GNU C Library Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.4 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.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Sun, Sep 11, 2022 at 4:24 PM Sunil Pandey via Libc-stable wrote: > > On Thu, Jan 20, 2022 at 5:08 AM H.J. Lu via Libc-alpha > wrote: > > > > On Thu, Jan 20, 2022 at 04:53:27AM -0800, H.J. Lu wrote: > > > On Tue, Dec 14, 2021 at 4:31 AM Jangwoong Kim via Libc-alpha > > > wrote: > > > > > > > > The commit: > > > > "Add LLL_MUTEX_READ_LOCK [BZ #28537]" > > > > SHA1: d672a98a1af106bd68deb15576710cd61363f7a6 > > > > > > > > introduced LLL_MUTEX_READ_LOCK, to skip CAS in spinlock loop > > > > if atomic load fails. But, "continue" inside of do-while loop > > > > does not skip the evaluation of escape expression, thus CAS > > > > is not skipped. > > > > > > > > Replace do-while with while and skip LLL_MUTEX_TRYLOCK if > > > > LLL_MUTEX_READ_LOCK fails. > > > > --- > > > > nptl/pthread_mutex_lock.c | 5 +---- > > > > 1 file changed, 1 insertion(+), 4 deletions(-) > > > > > > > > diff --git a/nptl/pthread_mutex_lock.c b/nptl/pthread_mutex_lock.c > > > > index 47b88a6b5b..06274c68e4 100644 > > > > --- a/nptl/pthread_mutex_lock.c > > > > +++ b/nptl/pthread_mutex_lock.c > > > > @@ -138,7 +138,7 @@ PTHREAD_MUTEX_LOCK (pthread_mutex_t *mutex) > > > > int cnt = 0; > > > > int max_cnt = MIN (max_adaptive_count (), > > > > mutex->__data.__spins * 2 + 10); > > > > - do > > > > + while (LLL_MUTEX_READ_LOCK (mutex) != 0 || LLL_MUTEX_TRYLOCK (mutex) != 0) > > > > { > > > > if (cnt++ >= max_cnt) > > > > { > > > > @@ -146,10 +146,7 @@ PTHREAD_MUTEX_LOCK (pthread_mutex_t *mutex) > > > > break; > > > > } > > > > atomic_spin_nop (); > > > > - if (LLL_MUTEX_READ_LOCK (mutex) != 0) > > > > - continue; > > > > } > > > > - while (LLL_MUTEX_TRYLOCK (mutex) != 0); > > > > > > > > mutex->__data.__spins += (cnt - mutex->__data.__spins) / 8; > > > > } > > > > -- > > > > 2.25.1 > > > > > > > > > > LGTM. > > > > > > Reviewed-by: H.J. Lu > > > > > > Thanks. > > > > > > > This is the v4 patch I am checkinng in. > > > > H.J. > > --- > > From: Jangwoong Kim <6812skiii@gmail.com> > > Date: Tue, 14 Dec 2021 21:30:51 +0900 > > > > The commit: > > "Add LLL_MUTEX_READ_LOCK [BZ #28537]" > > SHA1: d672a98a1af106bd68deb15576710cd61363f7a6 > > > > introduced LLL_MUTEX_READ_LOCK, to skip CAS in spinlock loop > > if atomic load fails. But, "continue" inside of do-while loop > > does not skip the evaluation of escape expression, thus CAS > > is not skipped. > > > > Replace do-while with while and skip LLL_MUTEX_TRYLOCK if > > LLL_MUTEX_READ_LOCK fails. > > > > Reviewed-by: H.J. Lu > > --- > > nptl/pthread_mutex_lock.c | 5 ++--- > > 1 file changed, 2 insertions(+), 3 deletions(-) > > > > diff --git a/nptl/pthread_mutex_lock.c b/nptl/pthread_mutex_lock.c > > index 31dfc7f1d7..d2e652d151 100644 > > --- a/nptl/pthread_mutex_lock.c > > +++ b/nptl/pthread_mutex_lock.c > > @@ -146,10 +146,9 @@ PTHREAD_MUTEX_LOCK (pthread_mutex_t *mutex) > > break; > > } > > atomic_spin_nop (); > > - if (LLL_MUTEX_READ_LOCK (mutex) != 0) > > - continue; > > } > > - while (LLL_MUTEX_TRYLOCK (mutex) != 0); > > + while (LLL_MUTEX_READ_LOCK (mutex) != 0 > > + || LLL_MUTEX_TRYLOCK (mutex) != 0); > > > > mutex->__data.__spins += (cnt - mutex->__data.__spins) / 8; > > } > > -- > > 2.34.1 > > > > I would like to backport this patch to release branch 2.33 and 2.34 Fine by me. > > Any comments/suggestions or objections on this. > > commit 6b8dbbd03ac88f169b65b5c7d7278576a11d2e44 > Author: Jangwoong Kim <6812skiii@gmail.com> > Date: Tue Dec 14 21:30:51 2021 +0900 > > nptl: Effectively skip CAS in spinlock loop > > The commit: > "Add LLL_MUTEX_READ_LOCK [BZ #28537]" > SHA1: d672a98a1af106bd68deb15576710cd61363f7a6