Skip to contentSkip to conversation

blk-mq: don't leak preempt counter/q_usage_counter when allocating rq failed

PATCH Superseded Ming Lei 2 participants 4 messages Jul 14, 2017 – Aug 2, 2017 Block layer

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Ming Lei

author
#0 lore raw

PATCH 1/1 blk-mq: don't leak preempt counter/q_usage_counter when allocating rq failed

From: Ming Lei <[email protected]>

When blk_mq_get_request() failed, preempt counter isn't released, and blk_mq_make_request() doesn't release the counter too.

This patch fixes the issue, and makes sure that preempt counter is only held if rq is allocated successfully. The same policy is applied on .q_usage_counter too.

---
 block/blk-mq.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
block/blk-mq.c +11 -6
index 041f7b7fa0d6..4f2561412f13 100644
@@ -301,11 +301,14 @@ static struct request *blk_mq_get_request(struct request_queue *q,
 	struct elevator_queue *e = q->elevator;
 	struct request *rq;
 	unsigned int tag;
+	bool get_cpu = false;
 
 	blk_queue_enter_live(q);
 	data->q = q;
-	if (likely(!data->ctx))
+	if (likely(!data->ctx)) {
 		data->ctx = blk_mq_get_ctx(q);
+		get_cpu = true;
+	}
 	if (likely(!data->hctx))
 		data->hctx = blk_mq_map_queue(q, data->ctx->cpu);
 	if (op & REQ_NOWAIT)
@@ -324,6 +327,8 @@ static struct request *blk_mq_get_request(struct request_queue *q,
 
 	tag = blk_mq_get_tag(data);
 	if (tag == BLK_MQ_TAG_FAIL) {
+		if (get_cpu)
+			blk_mq_put_ctx(data->ctx);
 		blk_queue_exit(q);
 		return NULL;
 	}
@@ -356,12 +361,12 @@ struct request *blk_mq_alloc_request(struct request_queue *q, unsigned int op,
 
 	rq = blk_mq_get_request(q, NULL, op, &alloc_data);
 
-	blk_mq_put_ctx(alloc_data.ctx);
-	blk_queue_exit(q);
-
 	if (!rq)
 		return ERR_PTR(-EWOULDBLOCK);
 
+	blk_mq_put_ctx(alloc_data.ctx);
+	blk_queue_exit(q);
+
 	rq->__data_len = 0;
 	rq->__sector = (sector_t) -1;
 	rq->bio = rq->biotail = NULL;
@@ -407,11 +412,11 @@ struct request *blk_mq_alloc_request_hctx(struct request_queue *q,
 
 	rq = blk_mq_get_request(q, NULL, op, &alloc_data);
 
-	blk_queue_exit(q);
-
 	if (!rq)
 		return ERR_PTR(-EWOULDBLOCK);
 
+	blk_queue_exit(q);
+
 	return rq;
 }
 EXPORT_SYMBOL_GPL(blk_mq_alloc_request_hctx);
Signature · 1 line
2.9.4
author
#1 lore raw
Quoted 9 lines · Ming Lei

From: Ming Lei <[email protected]>

When blk_mq_get_request() failed, preempt counter isn't released, and blk_mq_make_request() doesn't release the counter too.

This patch fixes the issue, and makes sure that preempt counter is only held if rq is allocated successfully. The same policy is applied on .q_usage_counter too.

Hi Jens,

Given NOWAIT has been merged, could you consider this patch for v4.13?

Thanks,
Ming

maintainer
#2 lore raw
In reply to Ming Lei
Quoted 9 lines · Ming Lei

From: Ming Lei <[email protected]>

When blk_mq_get_request() failed, preempt counter isn't released, and blk_mq_make_request() doesn't release the counter too.

This patch fixes the issue, and makes sure that preempt counter is only held if rq is allocated successfully. The same policy is applied on .q_usage_counter too.

Can you replace the 'get_cpu' bool with just a ctx, and change the logic to put it if set? I think that would be cleaner to read, generally I hate bool 'do_something' variables that are set. It's much cleaner to have:

if (likely(!data->ctx))

	data->ctx = local_ctx = blk_mq_get_ctx(q);

and have the put case be

if (local_ctx)

	blk_mq_put_ctx(local_ctx);

Either that, or at least just have blk_mq_get_request() do:

drop_ctx = data->ctx == NULL;

instead. The 'get_cpu' naming is confusing, we're worried about dropping the sw queue here, the fact that it's related to get/put_cpu() need not bubble up here.

Signature · 1 line
Jens Axboe
author
#3 lore raw
Quoted 24 lines · Jens Axboe
Quoted 9 lines · Ming Lei

From: Ming Lei <[email protected]>

When blk_mq_get_request() failed, preempt counter isn't released, and blk_mq_make_request() doesn't release the counter too.

This patch fixes the issue, and makes sure that preempt counter is only held if rq is allocated successfully. The same policy is applied on .q_usage_counter too.

Can you replace the 'get_cpu' bool with just a ctx, and change the logic to put it if set? I think that would be cleaner to read, generally I hate bool 'do_something' variables that are set. It's much cleaner to have:

if (likely(!data->ctx))

	data->ctx = local_ctx = blk_mq_get_ctx(q);

and have the put case be

if (local_ctx)

	blk_mq_put_ctx(local_ctx);
Quoted 7 lines

Either that, or at least just have blk_mq_get_request() do:

drop_ctx = data->ctx == NULL;

instead. The 'get_cpu' naming is confusing, we're worried about dropping the sw queue here, the fact that it's related to get/put_cpu() need not bubble up here.

Good point, V2 is sent out with this change. Thanks your suggestion!

Signature · 1 line
Ming