From: Paolo Valente <paolo.valente@linaro.org>
To: Jan Kara <jack@suse.cz>
Cc: Jens Axboe <axboe@kernel.dk>,
linux-block <linux-block@vger.kernel.org>,
Khazhy Kumykov <khazhy@google.com>,
Ming Lei <ming.lei@redhat.com>
Subject: Re: [PATCH 1/2] bfq: Remove merged request already in bfq_requests_merged()
Date: Fri, 28 May 2021 11:29:36 +0200 [thread overview]
Message-ID: <36A5F676-7720-400A-8127-CECF54FFA587@linaro.org> (raw)
In-Reply-To: <20210524100416.3578-2-jack@suse.cz>
> Il giorno 24 mag 2021, alle ore 12:04, Jan Kara <jack@suse.cz> ha scritto:
>
> Currently, bfq does very little in bfq_requests_merged() and handles all
> the request cleanup in bfq_finish_requeue_request() called from
> blk_mq_free_request(). That is currently safe only because
> blk_mq_free_request() is called shortly after bfq_requests_merged()
> while bfqd->lock is still held. However to fix a lock inversion between
> bfqd->lock and ioc->lock, we need to call blk_mq_free_request() after
> dropping bfqd->lock. That would mean that already merged request could
> be seen by other processes inside bfq queues and possibly dispatched to
> the device which is wrong. So move cleanup of the request from
> bfq_finish_requeue_request() to bfq_requests_merged().
>
I didn't even remember any longer why I had to handle that deferred
removal in bfq_finish_requeue_request() :)
Your solution seems very clean to me.
Acked-by: Paolo Valente <paolo.valente@linaro.org>
> Signed-off-by: Jan Kara <jack@suse.cz>
> ---
> block/bfq-iosched.c | 41 +++++++++++++----------------------------
> 1 file changed, 13 insertions(+), 28 deletions(-)
>
> diff --git a/block/bfq-iosched.c b/block/bfq-iosched.c
> index acd1f881273e..50a29fdf51da 100644
> --- a/block/bfq-iosched.c
> +++ b/block/bfq-iosched.c
> @@ -2405,7 +2405,7 @@ static void bfq_requests_merged(struct request_queue *q, struct request *rq,
> *next_bfqq = bfq_init_rq(next);
>
> if (!bfqq)
> - return;
> + goto remove;
>
> /*
> * If next and rq belong to the same bfq_queue and next is older
> @@ -2428,6 +2428,14 @@ static void bfq_requests_merged(struct request_queue *q, struct request *rq,
> bfqq->next_rq = rq;
>
> bfqg_stats_update_io_merged(bfqq_group(bfqq), next->cmd_flags);
> +remove:
> + /* Merged request may be in the IO scheduler. Remove it. */
> + if (!RB_EMPTY_NODE(&next->rb_node)) {
> + bfq_remove_request(next->q, next);
> + if (next_bfqq)
> + bfqg_stats_update_io_remove(bfqq_group(next_bfqq),
> + next->cmd_flags);
> + }
> }
>
> /* Must be called with bfqq != NULL */
> @@ -6376,6 +6384,7 @@ static void bfq_finish_requeue_request(struct request *rq)
> {
> struct bfq_queue *bfqq = RQ_BFQQ(rq);
> struct bfq_data *bfqd;
> + unsigned long flags;
>
> /*
> * rq either is not associated with any icq, or is an already
> @@ -6393,39 +6402,15 @@ static void bfq_finish_requeue_request(struct request *rq)
> rq->io_start_time_ns,
> rq->cmd_flags);
>
> + spin_lock_irqsave(&bfqd->lock, flags);
> if (likely(rq->rq_flags & RQF_STARTED)) {
> - unsigned long flags;
> -
> - spin_lock_irqsave(&bfqd->lock, flags);
> -
> if (rq == bfqd->waited_rq)
> bfq_update_inject_limit(bfqd, bfqq);
>
> bfq_completed_request(bfqq, bfqd);
> - bfq_finish_requeue_request_body(bfqq);
> -
> - spin_unlock_irqrestore(&bfqd->lock, flags);
> - } else {
> - /*
> - * Request rq may be still/already in the scheduler,
> - * in which case we need to remove it (this should
> - * never happen in case of requeue). And we cannot
> - * defer such a check and removal, to avoid
> - * inconsistencies in the time interval from the end
> - * of this function to the start of the deferred work.
> - * This situation seems to occur only in process
> - * context, as a consequence of a merge. In the
> - * current version of the code, this implies that the
> - * lock is held.
> - */
> -
> - if (!RB_EMPTY_NODE(&rq->rb_node)) {
> - bfq_remove_request(rq->q, rq);
> - bfqg_stats_update_io_remove(bfqq_group(bfqq),
> - rq->cmd_flags);
> - }
> - bfq_finish_requeue_request_body(bfqq);
> }
> + bfq_finish_requeue_request_body(bfqq);
> + spin_unlock_irqrestore(&bfqd->lock, flags);
>
> /*
> * Reset private fields. In case of a requeue, this allows
> --
> 2.26.2
>
next prev parent reply other threads:[~2021-05-28 9:29 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-05-24 10:04 [PATCH 0/2 v2] block: Fix deadlock when merging requests with BFQ Jan Kara
2021-05-24 10:04 ` [PATCH 1/2] bfq: Remove merged request already in bfq_requests_merged() Jan Kara
2021-05-28 9:29 ` Paolo Valente [this message]
2021-05-24 10:04 ` [PATCH 2/2] blk: Fix lock inversion between ioc lock and bfqd lock Jan Kara
2021-05-25 0:29 ` Ming Lei
2021-05-28 9:33 ` Paolo Valente
2021-05-28 12:30 [PATCH 0/2 v3] block: Fix deadlock when merging requests with BFQ Jan Kara
2021-05-28 12:30 ` [PATCH 1/2] bfq: Remove merged request already in bfq_requests_merged() Jan Kara
2021-06-23 9:36 [PATCH 0/2 v4] block: Fix deadlock when merging requests with BFQ Jan Kara
2021-06-23 9:36 ` [PATCH 1/2] bfq: Remove merged request already in bfq_requests_merged() 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=36A5F676-7720-400A-8127-CECF54FFA587@linaro.org \
--to=paolo.valente@linaro.org \
--cc=axboe@kernel.dk \
--cc=jack@suse.cz \
--cc=khazhy@google.com \
--cc=linux-block@vger.kernel.org \
--cc=ming.lei@redhat.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;
as well as URLs for NNTP newsgroup(s).