mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Mukesh Ojha <quic_mojha@quicinc.com>
To: Waiman Long <longman@redhat.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>, Will Deacon <will@kernel.org>,
	Boqun Feng <boqun.feng@gmail.com>
Cc: linux-kernel@vger.kernel.org, john.p.donnelly@oracle.com,
	"Hillf Danton" <hdanton@sina.com>,
	"Ting11 Wang 王婷" <wangting11@xiaomi.com>
Subject: Re: [PATCH v2 2/2] locking/rwsem: Limit # of null owner retries for handoff writer
Date: Wed, 12 Oct 2022 20:08:28 +0530	[thread overview]
Message-ID: <d31605a0-440b-989d-3762-d5e162ec79a6@quicinc.com> (raw)
In-Reply-To: <20221012133333.1265281-3-longman@redhat.com>

Hi,

On 10/12/2022 7:03 PM, Waiman Long wrote:
> Commit 91d2a812dfb9 ("locking/rwsem: Make handoff writer optimistically
> spin on owner") assumes that when the owner field is changed to NULL,
> the lock will become free soon.  That assumption may not be correct
> especially if the handoff writer doing the spinning is a RT task which
> may preempt another task from completing its action of either freeing
> the rwsem or properly setting up owner.
> 
> To prevent this live lock scenario, we have to limit the number of
> trylock attempts without sleeping. The current limit is now set to 8
> to allow enough time for the other task to hopefully complete its action.
> 
> By adding new lock events to track the number of NULL owner retries with
> handoff flag set before a successful trylock when running a 96 threads
> locking microbenchmark with equal number of readers and writers running
> on a 2-core 96-thread system for 15 seconds, the following stats are
> obtained. Note that none of locking threads are RT tasks.
> 
>    Retries of successful trylock    Count
>    -----------------------------    -----
>               1                     1738
>               2                       19
>               3                       11
>               4                        2
>               5                        1
>               6                        1
>               7                        1
>               8                        0
>               X                        1
> 
> The last row is the one failed attempt that needs more than 8 retries.
> So a retry count maximum of 8 should capture most of them if no RT task
> is in the mix.
> 
> Fixes: 91d2a812dfb9 ("locking/rwsem: Make handoff writer optimistically spin on owner")
> Reported-by: Mukesh Ojha <quic_mojha@quicinc.com>
> Signed-off-by: Waiman Long <longman@redhat.com>
> ---
>   kernel/locking/rwsem.c | 18 ++++++++++++++++--
>   1 file changed, 16 insertions(+), 2 deletions(-)
> 
> diff --git a/kernel/locking/rwsem.c b/kernel/locking/rwsem.c
> index 3839b38608da..12eb093328f2 100644
> --- a/kernel/locking/rwsem.c
> +++ b/kernel/locking/rwsem.c
> @@ -1123,6 +1123,7 @@ static struct rw_semaphore __sched *
>   rwsem_down_write_slowpath(struct rw_semaphore *sem, int state)
>   {
>   	struct rwsem_waiter waiter;
> +	int null_owner_retries;
>   	DEFINE_WAKE_Q(wake_q);
>   
>   	/* do optimistic spinning and steal lock if possible */
> @@ -1164,7 +1165,7 @@ rwsem_down_write_slowpath(struct rw_semaphore *sem, int state)
>   	set_current_state(state);
>   	trace_contention_begin(sem, LCB_F_WRITE);
>   
> -	for (;;) {
> +	for (null_owner_retries = 0;;) {
>   		if (rwsem_try_write_lock(sem, &waiter)) {
>   			/* rwsem_try_write_lock() implies ACQUIRE on success */
>   			break;
> @@ -1190,8 +1191,21 @@ rwsem_down_write_slowpath(struct rw_semaphore *sem, int state)
>   			owner_state = rwsem_spin_on_owner(sem);
>   			preempt_enable();
>   
> -			if (owner_state == OWNER_NULL)
> +			/*
> +			 * owner is NULL doesn't guarantee the lock is free.
> +			 * An incoming reader will temporarily increment the
> +			 * reader count without changing owner and the
> +			 * rwsem_try_write_lock() will fails if the reader
> +			 * is not able to decrement it in time. Allow 8
> +			 * trylock attempts when hitting a NULL owner before
> +			 * going to sleep.
> +			 */
> +			if ((owner_state == OWNER_NULL) &&
> +			    (null_owner_retries < 8)) {

define MAX_NULL_OWNER_RETRY		8

??

> +				null_owner_retries++;
>   				goto trylock_again;
> +			}
> +			null_owner_retries = 0;
>   		}
>   

Thanks for considering this patch.

LGTM.
Reviewed-and-Tested-by: Mukesh Ojha <quic_mojha@quicinc.com>

-Mukesh

>   		schedule();

      reply	other threads:[~2022-10-12 14:39 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-12 13:33 [PATCH v2 0/2] locking/rwsem: Fix rwsem waiter optimistic spinning problem with RT tasks Waiman Long
2022-10-12 13:33 ` [PATCH v2 1/2] locking/rwsem: Prevent non-first waiter from spinning in down_write() slowpath Waiman Long
2022-10-12 14:23   ` Mukesh Ojha
2022-10-13 10:02   ` Peter Zijlstra
2022-10-13 13:33     ` Waiman Long
2022-10-13 15:37       ` Waiman Long
2022-10-12 13:33 ` [PATCH v2 2/2] locking/rwsem: Limit # of null owner retries for handoff writer Waiman Long
2022-10-12 14:38   ` Mukesh Ojha [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d31605a0-440b-989d-3762-d5e162ec79a6@quicinc.com \
    --to=quic_mojha@quicinc.com \
    --cc=boqun.feng@gmail.com \
    --cc=hdanton@sina.com \
    --cc=john.p.donnelly@oracle.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=longman@redhat.com \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=wangting11@xiaomi.com \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®