Skip to content

Commit f1a7cd0

Browse files
committed
Merge branch 'for-linus' of git://git.kernel.dk/linux-block into next
Pull block follow-up bits from Jens Axboe: "A few minor (but important) fixes for blk-mq for the -rc1 window. - Hot removal potential oops fix for single queue devices. From me. - Two merged patches in late May meant that we accidentally lost a fix for freeing an active queue. Fix that up. From me. - A change of the blk_mq_tag_to_rq() API, passing in blk_mq_tags, to make life considerably easier for scsi-mq. From me. - A schedule-while-atomic fix from Ming Lei, which would hit if the tag space was exhausted. - Missing __percpu annotation in one place in blk-mq. Found by the magic Wu compile bot due to code being moved around by the previous patch, but it's actually an older issue. From Ming Lei. - Clearing of tag of a flush request at end_io time. From Ming Lei" * 'for-linus' of git://git.kernel.dk/linux-block: block: mq flush: clear flush_rq's tag in flush_end_io() blk-mq: let blk_mq_tag_to_rq() take blk_mq_tags as the main parameter blk-mq: fix regression from commit 624dbe4 blk-mq: handle NULL req return from blk_map_request in single queue mode blk-mq: fix sparse warning on missed __percpu annotation blk-mq: fix schedule from atomic context blk-mq: move blk_mq_get_ctx/blk_mq_put_ctx to mq private header
2 parents aaeb255 + 14b83e1 commit f1a7cd0

8 files changed

Lines changed: 123 additions & 65 deletions

File tree

‎block/blk-flush.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ static void flush_end_io(struct request *flush_rq, int error)
225225

226226
if (q->mq_ops) {
227227
spin_lock_irqsave(&q->mq_flush_lock, flags);
228-
q->flush_rq->cmd_flags = 0;
228+
q->flush_rq->tag = -1;
229229
}
230230

231231
running = &q->flush_queue[q->flush_running_idx];

‎block/blk-mq-tag.c‎

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,10 @@ static struct bt_wait_state *bt_wait_ptr(struct blk_mq_bitmap_tags *bt,
221221
return bs;
222222
}
223223

224-
static int bt_get(struct blk_mq_bitmap_tags *bt, struct blk_mq_hw_ctx *hctx,
225-
unsigned int *last_tag, gfp_t gfp)
224+
static int bt_get(struct blk_mq_alloc_data *data,
225+
struct blk_mq_bitmap_tags *bt,
226+
struct blk_mq_hw_ctx *hctx,
227+
unsigned int *last_tag)
226228
{
227229
struct bt_wait_state *bs;
228230
DEFINE_WAIT(wait);
@@ -232,7 +234,7 @@ static int bt_get(struct blk_mq_bitmap_tags *bt, struct blk_mq_hw_ctx *hctx,
232234
if (tag != -1)
233235
return tag;
234236

235-
if (!(gfp & __GFP_WAIT))
237+
if (!(data->gfp & __GFP_WAIT))
236238
return -1;
237239

238240
bs = bt_wait_ptr(bt, hctx);
@@ -249,50 +251,62 @@ static int bt_get(struct blk_mq_bitmap_tags *bt, struct blk_mq_hw_ctx *hctx,
249251
if (was_empty)
250252
atomic_set(&bs->wait_cnt, bt->wake_cnt);
251253

254+
blk_mq_put_ctx(data->ctx);
255+
252256
io_schedule();
257+
258+
data->ctx = blk_mq_get_ctx(data->q);
259+
data->hctx = data->q->mq_ops->map_queue(data->q,
260+
data->ctx->cpu);
261+
if (data->reserved) {
262+
bt = &data->hctx->tags->breserved_tags;
263+
} else {
264+
last_tag = &data->ctx->last_tag;
265+
hctx = data->hctx;
266+
bt = &hctx->tags->bitmap_tags;
267+
}
268+
finish_wait(&bs->wait, &wait);
269+
bs = bt_wait_ptr(bt, hctx);
253270
} while (1);
254271

255272
finish_wait(&bs->wait, &wait);
256273
return tag;
257274
}
258275

259-
static unsigned int __blk_mq_get_tag(struct blk_mq_tags *tags,
260-
struct blk_mq_hw_ctx *hctx,
261-
unsigned int *last_tag, gfp_t gfp)
276+
static unsigned int __blk_mq_get_tag(struct blk_mq_alloc_data *data)
262277
{
263278
int tag;
264279

265-
tag = bt_get(&tags->bitmap_tags, hctx, last_tag, gfp);
280+
tag = bt_get(data, &data->hctx->tags->bitmap_tags, data->hctx,
281+
&data->ctx->last_tag);
266282
if (tag >= 0)
267-
return tag + tags->nr_reserved_tags;
283+
return tag + data->hctx->tags->nr_reserved_tags;
268284

269285
return BLK_MQ_TAG_FAIL;
270286
}
271287

272-
static unsigned int __blk_mq_get_reserved_tag(struct blk_mq_tags *tags,
273-
gfp_t gfp)
288+
static unsigned int __blk_mq_get_reserved_tag(struct blk_mq_alloc_data *data)
274289
{
275290
int tag, zero = 0;
276291

277-
if (unlikely(!tags->nr_reserved_tags)) {
292+
if (unlikely(!data->hctx->tags->nr_reserved_tags)) {
278293
WARN_ON_ONCE(1);
279294
return BLK_MQ_TAG_FAIL;
280295
}
281296

282-
tag = bt_get(&tags->breserved_tags, NULL, &zero, gfp);
297+
tag = bt_get(data, &data->hctx->tags->breserved_tags, NULL, &zero);
283298
if (tag < 0)
284299
return BLK_MQ_TAG_FAIL;
285300

286301
return tag;
287302
}
288303

289-
unsigned int blk_mq_get_tag(struct blk_mq_hw_ctx *hctx, unsigned int *last_tag,
290-
gfp_t gfp, bool reserved)
304+
unsigned int blk_mq_get_tag(struct blk_mq_alloc_data *data)
291305
{
292-
if (!reserved)
293-
return __blk_mq_get_tag(hctx->tags, hctx, last_tag, gfp);
306+
if (!data->reserved)
307+
return __blk_mq_get_tag(data);
294308

295-
return __blk_mq_get_reserved_tag(hctx->tags, gfp);
309+
return __blk_mq_get_reserved_tag(data);
296310
}
297311

298312
static struct bt_wait_state *bt_wake_ptr(struct blk_mq_bitmap_tags *bt)

‎block/blk-mq-tag.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ struct blk_mq_tags {
4848
extern struct blk_mq_tags *blk_mq_init_tags(unsigned int nr_tags, unsigned int reserved_tags, int node);
4949
extern void blk_mq_free_tags(struct blk_mq_tags *tags);
5050

51-
extern unsigned int blk_mq_get_tag(struct blk_mq_hw_ctx *hctx, unsigned int *last_tag, gfp_t gfp, bool reserved);
51+
extern unsigned int blk_mq_get_tag(struct blk_mq_alloc_data *data);
5252
extern void blk_mq_put_tag(struct blk_mq_hw_ctx *hctx, unsigned int tag, unsigned int *last_tag);
5353
extern bool blk_mq_has_free_tags(struct blk_mq_tags *tags);
5454
extern ssize_t blk_mq_tag_sysfs_show(struct blk_mq_tags *tags, char *page);

‎block/blk-mq.c‎

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,6 @@ static LIST_HEAD(all_q_list);
3333

3434
static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx);
3535

36-
static struct blk_mq_ctx *__blk_mq_get_ctx(struct request_queue *q,
37-
unsigned int cpu)
38-
{
39-
return per_cpu_ptr(q->queue_ctx, cpu);
40-
}
41-
42-
/*
43-
* This assumes per-cpu software queueing queues. They could be per-node
44-
* as well, for instance. For now this is hardcoded as-is. Note that we don't
45-
* care about preemption, since we know the ctx's are persistent. This does
46-
* mean that we can't rely on ctx always matching the currently running CPU.
47-
*/
48-
static struct blk_mq_ctx *blk_mq_get_ctx(struct request_queue *q)
49-
{
50-
return __blk_mq_get_ctx(q, get_cpu());
51-
}
52-
53-
static void blk_mq_put_ctx(struct blk_mq_ctx *ctx)
54-
{
55-
put_cpu();
56-
}
57-
5836
/*
5937
* Check if any of the ctx's have pending work in this hardware queue
6038
*/
@@ -232,24 +210,23 @@ static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
232210
}
233211

234212
static struct request *
235-
__blk_mq_alloc_request(struct request_queue *q, struct blk_mq_hw_ctx *hctx,
236-
struct blk_mq_ctx *ctx, int rw, gfp_t gfp, bool reserved)
213+
__blk_mq_alloc_request(struct blk_mq_alloc_data *data, int rw)
237214
{
238215
struct request *rq;
239216
unsigned int tag;
240217

241-
tag = blk_mq_get_tag(hctx, &ctx->last_tag, gfp, reserved);
218+
tag = blk_mq_get_tag(data);
242219
if (tag != BLK_MQ_TAG_FAIL) {
243-
rq = hctx->tags->rqs[tag];
220+
rq = data->hctx->tags->rqs[tag];
244221

245222
rq->cmd_flags = 0;
246-
if (blk_mq_tag_busy(hctx)) {
223+
if (blk_mq_tag_busy(data->hctx)) {
247224
rq->cmd_flags = REQ_MQ_INFLIGHT;
248-
atomic_inc(&hctx->nr_active);
225+
atomic_inc(&data->hctx->nr_active);
249226
}
250227

251228
rq->tag = tag;
252-
blk_mq_rq_ctx_init(q, ctx, rq, rw);
229+
blk_mq_rq_ctx_init(data->q, data->ctx, rq, rw);
253230
return rq;
254231
}
255232

@@ -262,22 +239,27 @@ struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp,
262239
struct blk_mq_ctx *ctx;
263240
struct blk_mq_hw_ctx *hctx;
264241
struct request *rq;
242+
struct blk_mq_alloc_data alloc_data;
265243

266244
if (blk_mq_queue_enter(q))
267245
return NULL;
268246

269247
ctx = blk_mq_get_ctx(q);
270248
hctx = q->mq_ops->map_queue(q, ctx->cpu);
249+
blk_mq_set_alloc_data(&alloc_data, q, gfp & ~__GFP_WAIT,
250+
reserved, ctx, hctx);
271251

272-
rq = __blk_mq_alloc_request(q, hctx, ctx, rw, gfp & ~__GFP_WAIT,
273-
reserved);
252+
rq = __blk_mq_alloc_request(&alloc_data, rw);
274253
if (!rq && (gfp & __GFP_WAIT)) {
275254
__blk_mq_run_hw_queue(hctx);
276255
blk_mq_put_ctx(ctx);
277256

278257
ctx = blk_mq_get_ctx(q);
279258
hctx = q->mq_ops->map_queue(q, ctx->cpu);
280-
rq = __blk_mq_alloc_request(q, hctx, ctx, rw, gfp, reserved);
259+
blk_mq_set_alloc_data(&alloc_data, q, gfp, reserved, ctx,
260+
hctx);
261+
rq = __blk_mq_alloc_request(&alloc_data, rw);
262+
ctx = alloc_data.ctx;
281263
}
282264
blk_mq_put_ctx(ctx);
283265
return rq;
@@ -547,15 +529,20 @@ void blk_mq_kick_requeue_list(struct request_queue *q)
547529
}
548530
EXPORT_SYMBOL(blk_mq_kick_requeue_list);
549531

550-
struct request *blk_mq_tag_to_rq(struct blk_mq_hw_ctx *hctx, unsigned int tag)
532+
static inline bool is_flush_request(struct request *rq, unsigned int tag)
551533
{
552-
struct request_queue *q = hctx->queue;
534+
return ((rq->cmd_flags & REQ_FLUSH_SEQ) &&
535+
rq->q->flush_rq->tag == tag);
536+
}
537+
538+
struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
539+
{
540+
struct request *rq = tags->rqs[tag];
553541

554-
if ((q->flush_rq->cmd_flags & REQ_FLUSH_SEQ) &&
555-
q->flush_rq->tag == tag)
556-
return q->flush_rq;
542+
if (!is_flush_request(rq, tag))
543+
return rq;
557544

558-
return hctx->tags->rqs[tag];
545+
return rq->q->flush_rq;
559546
}
560547
EXPORT_SYMBOL(blk_mq_tag_to_rq);
561548

@@ -584,7 +571,7 @@ static void blk_mq_timeout_check(void *__data, unsigned long *free_tags)
584571
if (tag >= hctx->tags->nr_tags)
585572
break;
586573

587-
rq = blk_mq_tag_to_rq(hctx, tag++);
574+
rq = blk_mq_tag_to_rq(hctx->tags, tag++);
588575
if (rq->q != hctx->queue)
589576
continue;
590577
if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
@@ -1158,6 +1145,7 @@ static struct request *blk_mq_map_request(struct request_queue *q,
11581145
struct blk_mq_ctx *ctx;
11591146
struct request *rq;
11601147
int rw = bio_data_dir(bio);
1148+
struct blk_mq_alloc_data alloc_data;
11611149

11621150
if (unlikely(blk_mq_queue_enter(q))) {
11631151
bio_endio(bio, -EIO);
@@ -1171,16 +1159,21 @@ static struct request *blk_mq_map_request(struct request_queue *q,
11711159
rw |= REQ_SYNC;
11721160

11731161
trace_block_getrq(q, bio, rw);
1174-
rq = __blk_mq_alloc_request(q, hctx, ctx, rw, GFP_ATOMIC, false);
1162+
blk_mq_set_alloc_data(&alloc_data, q, GFP_ATOMIC, false, ctx,
1163+
hctx);
1164+
rq = __blk_mq_alloc_request(&alloc_data, rw);
11751165
if (unlikely(!rq)) {
11761166
__blk_mq_run_hw_queue(hctx);
11771167
blk_mq_put_ctx(ctx);
11781168
trace_block_sleeprq(q, bio, rw);
11791169

11801170
ctx = blk_mq_get_ctx(q);
11811171
hctx = q->mq_ops->map_queue(q, ctx->cpu);
1182-
rq = __blk_mq_alloc_request(q, hctx, ctx, rw,
1183-
__GFP_WAIT|GFP_ATOMIC, false);
1172+
blk_mq_set_alloc_data(&alloc_data, q,
1173+
__GFP_WAIT|GFP_ATOMIC, false, ctx, hctx);
1174+
rq = __blk_mq_alloc_request(&alloc_data, rw);
1175+
ctx = alloc_data.ctx;
1176+
hctx = alloc_data.hctx;
11841177
}
11851178

11861179
hctx->queued++;
@@ -1288,6 +1281,8 @@ static void blk_sq_make_request(struct request_queue *q, struct bio *bio)
12881281
return;
12891282

12901283
rq = blk_mq_map_request(q, bio, &data);
1284+
if (unlikely(!rq))
1285+
return;
12911286

12921287
if (unlikely(is_flush_fua)) {
12931288
blk_mq_bio_to_request(rq, bio);
@@ -1562,6 +1557,8 @@ static void blk_mq_exit_hw_queues(struct request_queue *q,
15621557
if (i == nr_queue)
15631558
break;
15641559

1560+
blk_mq_tag_idle(hctx);
1561+
15651562
if (set->ops->exit_hctx)
15661563
set->ops->exit_hctx(hctx, i);
15671564

@@ -1779,7 +1776,7 @@ static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
17791776
struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
17801777
{
17811778
struct blk_mq_hw_ctx **hctxs;
1782-
struct blk_mq_ctx *ctx;
1779+
struct blk_mq_ctx __percpu *ctx;
17831780
struct request_queue *q;
17841781
unsigned int *map;
17851782
int i;

‎block/blk-mq.h‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,49 @@ struct blk_align_bitmap {
6969
unsigned long depth;
7070
} ____cacheline_aligned_in_smp;
7171

72+
static inline struct blk_mq_ctx *__blk_mq_get_ctx(struct request_queue *q,
73+
unsigned int cpu)
74+
{
75+
return per_cpu_ptr(q->queue_ctx, cpu);
76+
}
77+
78+
/*
79+
* This assumes per-cpu software queueing queues. They could be per-node
80+
* as well, for instance. For now this is hardcoded as-is. Note that we don't
81+
* care about preemption, since we know the ctx's are persistent. This does
82+
* mean that we can't rely on ctx always matching the currently running CPU.
83+
*/
84+
static inline struct blk_mq_ctx *blk_mq_get_ctx(struct request_queue *q)
85+
{
86+
return __blk_mq_get_ctx(q, get_cpu());
87+
}
88+
89+
static inline void blk_mq_put_ctx(struct blk_mq_ctx *ctx)
90+
{
91+
put_cpu();
92+
}
93+
94+
struct blk_mq_alloc_data {
95+
/* input parameter */
96+
struct request_queue *q;
97+
gfp_t gfp;
98+
bool reserved;
99+
100+
/* input & output parameter */
101+
struct blk_mq_ctx *ctx;
102+
struct blk_mq_hw_ctx *hctx;
103+
};
104+
105+
static inline void blk_mq_set_alloc_data(struct blk_mq_alloc_data *data,
106+
struct request_queue *q, gfp_t gfp, bool reserved,
107+
struct blk_mq_ctx *ctx,
108+
struct blk_mq_hw_ctx *hctx)
109+
{
110+
data->q = q;
111+
data->gfp = gfp;
112+
data->reserved = reserved;
113+
data->ctx = ctx;
114+
data->hctx = hctx;
115+
}
116+
72117
#endif

‎drivers/block/mtip32xx/mtip32xx.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,9 @@ static void mtip_put_int_command(struct driver_data *dd, struct mtip_cmd *cmd)
193193
static struct request *mtip_rq_from_tag(struct driver_data *dd,
194194
unsigned int tag)
195195
{
196-
return blk_mq_tag_to_rq(dd->queue->queue_hw_ctx[0], tag);
196+
struct blk_mq_hw_ctx *hctx = dd->queue->queue_hw_ctx[0];
197+
198+
return blk_mq_tag_to_rq(hctx->tags, tag);
197199
}
198200

199201
static struct mtip_cmd *mtip_cmd_from_tag(struct driver_data *dd,

‎include/linux/blk-mq.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ void blk_mq_free_request(struct request *rq);
155155
bool blk_mq_can_queue(struct blk_mq_hw_ctx *);
156156
struct request *blk_mq_alloc_request(struct request_queue *q, int rw,
157157
gfp_t gfp, bool reserved);
158-
struct request *blk_mq_tag_to_rq(struct blk_mq_hw_ctx *hctx, unsigned int tag);
158+
struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag);
159159

160160
struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *, const int ctx_index);
161161
struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_tag_set *, unsigned int, int);

‎include/linux/blkdev.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ struct request_queue {
335335
unsigned int *mq_map;
336336

337337
/* sw queues */
338-
struct blk_mq_ctx *queue_ctx;
338+
struct blk_mq_ctx __percpu *queue_ctx;
339339
unsigned int nr_queues;
340340

341341
/* hw dispatch queues */

0 commit comments

Comments
 (0)