From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: <6812skiii@gmail.com> Received: from mail-io1-xd33.google.com (mail-io1-xd33.google.com [IPv6:2607:f8b0:4864:20::d33]) by sourceware.org (Postfix) with ESMTPS id 332C73858D28 for ; Mon, 6 Dec 2021 18:03:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 332C73858D28 Received: by mail-io1-xd33.google.com with SMTP id e128so13983818iof.1 for ; Mon, 06 Dec 2021 10:03:12 -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:content-transfer-encoding; bh=YKn+xsvuH11V/hbpujgD3x4yCvWzxIVwk3duyhnHO6k=; b=Il8Ju4lgf6RALsA5dV+FTodwuVdOUHf8uGn3xdA7MghgP3/3s45mruUeRRqxIFmah6 0zVDBSzzhbcDMQ0f2pLZeC7ajXEhr+zTgvysjGpvifUAmDkoOrH6BESdK0TzWN+VnZjE hKHKsfeVX3ENKmtJJwBhZJANXyQhXyPIUJXwfgWJ2Iqqhq+JIWTertWuTBMPYFXLIIbr IO0EP8Yum6oqIW/z4sHG/iCnvgki5XUoYbOoIjHqtqDFOFmIOdfgIQOJ/G9JSmGhv8s4 CAiRV4FANVKvzknZic1/gRCKGjswH88S/NHy9xrch5L1siSlTeMnb4gkL11pKj4qj2bZ 5tRQ== X-Gm-Message-State: AOAM532lSr/6OGiFkTz/zMVGX3AopvXNuIk+qvUzPF+kG3RON0tBmW9k jrrp7ZNdW4wYJ7tka1Kv0L8Xp1tZFauI8vUJACo= X-Google-Smtp-Source: ABdhPJyfihpl6Ud2cUFX3IaDU8zvDvUVOXxQsQ9gsSpujUV+BkGWOOC+0eRdC0wHDOqBRP9f8mTsN8JfQ7p0l57j3wA= X-Received: by 2002:a05:6602:1407:: with SMTP id t7mr1126253iov.78.1638813791632; Mon, 06 Dec 2021 10:03:11 -0800 (PST) MIME-Version: 1.0 References: <20211205152814.13597-1-6812skiii@gmail.com> <331FA4DD-8EE9-4538-A462-B68A3617E41C@linaro.org> In-Reply-To: <331FA4DD-8EE9-4538-A462-B68A3617E41C@linaro.org> From: Jangwoong Kim <6812skiii@gmail.com> Date: Tue, 7 Dec 2021 03:03:01 +0900 Message-ID: Subject: Re: [PATCH] nptl: Effectively skip CAS in spinlock loop To: Maxim Kuvyrkov Cc: libc-alpha@sourceware.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-9.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, FROM_STARTS_WITH_NUMS, 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: Mon, 06 Dec 2021 18:03:13 -0000 Hi Maxim, Did you mean editing the commit message? If so, I will resend a patch right away. Or, if you meant adding a comment to source code, I'm not clear on your req= uest. The reason do-while loop structure causes an extra CAS is "continue" expression does not skip the conditional expression do-while loop. My patch replaces the do-while loop, so there isn't an extra CAS. Thus, adding a comment why the previous code was wrong doesn't seem proper. -- Thank you. Jangwoong Kim 2021=EB=85=84 12=EC=9B=94 6=EC=9D=BC (=EC=9B=94) =EC=98=A4=ED=9B=84 11:26, = Maxim Kuvyrkov =EB=8B=98=EC=9D=B4 =EC=9E=91=EC= =84=B1: > > > > On 5 Dec 2021, at 18:28, 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 atmoic 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 break if LLL_MUTEX_READ_LOCK fails. > > --- > > nptl/pthread_mutex_lock.c | 5 +++-- > > 1 file changed, 3 insertions(+), 2 deletions(-) > > > > diff --git a/nptl/pthread_mutex_lock.c b/nptl/pthread_mutex_lock.c > > index 47b88a6b5b..24936c8d56 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 =3D 0; > > int max_cnt =3D MIN (max_adaptive_count (), > > mutex->__data.__spins * 2 + 10); > > - do > > + while (1) > > { > > if (cnt++ >=3D max_cnt) > > { > > @@ -148,8 +148,9 @@ PTHREAD_MUTEX_LOCK (pthread_mutex_t *mutex) > > atomic_spin_nop (); > > if (LLL_MUTEX_READ_LOCK (mutex) !=3D 0) > > continue; > > + if (LLL_MUTEX_TRYLOCK (mutex) =3D=3D 0) > > + break; > > } > > - while (LLL_MUTEX_TRYLOCK (mutex) !=3D 0); > > Giving this a second look, would you please add a comment before =E2=80= =9Cbreak=E2=80=9D to explain why do-while loop structure would cause an ext= ra compare-and-swap? > > -- > Maxim Kuvyrkov > https://www.linaro.org > > > > > mutex->__data.__spins +=3D (cnt - mutex->__data.__spins) / 8; > > } > > -- > > 2.25.1 > > >