Linux block layer
 help / color / mirror / Atom feed
From: Jan Kara <jack@suse.cz>
To: Gabriel Krisman Bertazi <krisman@suse.de>
Cc: Jan Kara <jack@suse.cz>,
	axboe@kernel.dk, linux-kernel@vger.kernel.org,
	linux-block@vger.kernel.org, Hugh Dickins <hughd@google.com>,
	Keith Busch <kbusch@kernel.org>,
	Liu Song <liusong@linux.alibaba.com>
Subject: Re: [PATCH] sbitmap: Advance the queue index before waking up the queue
Date: Mon, 14 Nov 2022 15:34:04 +0100	[thread overview]
Message-ID: <20221114143404.c47lvnbfihz5tdj5@quack3> (raw)
In-Reply-To: <87wn7xk46u.fsf_-_@gbertazi.udp.ovpn1.prg.suse.de>

On Mon 14-11-22 09:20:57, Gabriel Krisman Bertazi wrote:
> Jan Kara <jack@suse.cz> writes:
> 
> > Gabriel, when looking through this patch, I've noticed we can loose wakeups
> > after your latest simplifications. See below for details:
> >
> > On Sat 05-11-22 19:10:55, Gabriel Krisman Bertazi wrote:
> >> @@ -587,7 +571,7 @@ static struct sbq_wait_state *sbq_wake_ptr(struct sbitmap_queue *sbq)
> >>  	for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
> >>  		struct sbq_wait_state *ws = &sbq->ws[wake_index];
> >>  
> >> -		if (waitqueue_active(&ws->wait) && atomic_read(&ws->wait_cnt)) {
> >> +		if (waitqueue_active(&ws->wait)) {
> >>  			if (wake_index != atomic_read(&sbq->wake_index))
> >>  				atomic_set(&sbq->wake_index, wake_index);
> >>  			return ws;
> >
> > Neither sbq_wake_ptr() nor sbitmap_queue_wake_up() now increment the
> > wake_index after performing the wakeup. Thus we would effectively keep
> > waking tasks from a single waitqueue until it becomes empty and only then
> > go for the next waitqueue. This creates unnecessary unfairness in task
> > wakeups and even possible starvation issues. So I think we need to advance
> > wake_index somewhere. Perhaps here before returning waitqueue.
> 
> right. This is indeed a problem.  what do you think of the patch below?
> 
> > Now this may be also problematic - when we were checking the number of woken
> > waiters in the older version of the patch (for others: internal version of
> > the patch) this was fine but now it may happen that the 'ws' we have
> > selected has no waiters anymore. And in that case we need to find another
> > waitqueue because otherwise we'd be loosing too many wakeups and we could
> > deadlock. So I think this rather needs to be something like:
> >
> > 	do {
> > 		if (atomic_read(&sbq->completion_cnt) - wakeups < wake_batch)
> > 			return;
> > 	} while (!atomic_try_cmpxchg(&sbq->wakeup_cnt,
> > 				     &wakeups, wakeups + wake_batch));
> >
> > 	do {
> > 		ws = sbq_wake_ptr(sbq);
> > 		if (!ws)
> > 			return;
> > 	} while (!wake_up_nr(&ws->wait, wake_batch));
> >
> > with our original version of wake_up_nr() returning number of woken
> > waiters. What do you think?
> 
> I agree, and it wouldn't happen with the wake_up_nr patch we had before.
> I will revive it quickly and follow up.  But, in this case, I want to be
> cautious with benchmarking, so I will follow up still today, but as soon
> as the new round of tests complete.

Sure.

> -- >8 --
> Subject: [PATCH] sbitmap: Advance the queue index before waking up the queue
> 
> When a queue is awaken, the wake_index written by sbq_wake_ptr currently
> keeps pointing to the same queue.  On the next wake up, it will thus
> retry the same queue, which is unfair to other queues, and can lead to
> starvation.  This patch, moves the index update to happen before the
> queue is returned, such that it will now try a different queue first on
> the next wake up, improving fairness.
> 
> Reported-by: Jan Kara <jack@suse.cz>
> Signed-off-by: Gabriel Krisman Bertazi <krisman@suse.de>

Yes, nice. Feel free to add:

Reviewed-by: Jan Kara <jack@suse.cz>

								Honza

> ---
>  lib/sbitmap.c | 10 ++++++++--
>  1 file changed, 8 insertions(+), 2 deletions(-)
> 
> diff --git a/lib/sbitmap.c b/lib/sbitmap.c
> index eca462cba398..bea7984f7987 100644
> --- a/lib/sbitmap.c
> +++ b/lib/sbitmap.c
> @@ -571,13 +571,19 @@ static struct sbq_wait_state *sbq_wake_ptr(struct sbitmap_queue *sbq)
>  	for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
>  		struct sbq_wait_state *ws = &sbq->ws[wake_index];
>  
> +		/*
> +		 * Advance the index before checking the current queue.
> +		 * It improves fairness, by ensuring the queue doesn't
> +		 * need to be fully emptied before trying to wake up
> +		 * from the next one.
> +		 */
> +		wake_index = sbq_index_inc(wake_index);
> +
>  		if (waitqueue_active(&ws->wait)) {
>  			if (wake_index != atomic_read(&sbq->wake_index))
>  				atomic_set(&sbq->wake_index, wake_index);
>  			return ws;
>  		}
> -
> -		wake_index = sbq_index_inc(wake_index);
>  	}
>  
>  	return NULL;
> -- 
> 2.35.3
> 
> 
> 
> 
> 
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR

  reply	other threads:[~2022-11-14 14:34 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-05 23:10 [PATCH] sbitmap: Use single per-bitmap counting to wake up queued tags Gabriel Krisman Bertazi
2022-11-08 23:28 ` Chaitanya Kulkarni
2022-11-09  3:03   ` Gabriel Krisman Bertazi
2022-11-09  3:35     ` Chaitanya Kulkarni
2022-11-09 22:06 ` Jens Axboe
2022-11-09 22:48   ` Gabriel Krisman Bertazi
2022-11-10  3:25     ` Jens Axboe
2022-11-10  9:42 ` Yu Kuai
2022-11-10 11:16   ` Jan Kara
2022-11-10 13:18     ` Yu Kuai
2022-11-10 15:35       ` Jan Kara
2022-11-11  0:59         ` Yu Kuai
2022-11-11 15:38 ` Jens Axboe
2022-11-14 13:23 ` Jan Kara
2022-11-14 14:20   ` [PATCH] sbitmap: Advance the queue index before waking up the queue Gabriel Krisman Bertazi
2022-11-14 14:34     ` Jan Kara [this message]
2022-11-15  3:52   ` [PATCH] sbitmap: Use single per-bitmap counting to wake up queued tags Gabriel Krisman Bertazi
2022-11-15 10:24     ` Jan Kara

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=20221114143404.c47lvnbfihz5tdj5@quack3 \
    --to=jack@suse.cz \
    --cc=axboe@kernel.dk \
    --cc=hughd@google.com \
    --cc=kbusch@kernel.org \
    --cc=krisman@suse.de \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=liusong@linux.alibaba.com \
    /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