diff --git a/block/blk-mq-tag.c b/block/blk-mq-tag.c index 35deee5bbc73..6e0130922671 100644 --- a/block/blk-mq-tag.c +++ b/block/blk-mq-tag.c @@ -18,6 +18,13 @@ #include "blk-mq.h" #include "blk-mq-sched.h" +/* + * Protects tag iteration against the deferred freeing of tags->page_list and + * flush queues. The callbacks touch neither the tag set nor driver code, so + * one global instance is enough and freeing a tag set needn't wait for them. + */ +DEFINE_SRCU(blk_mq_tags_srcu); + /* * Recalculate wakeup batch when tag is shared by hctx. */ @@ -443,7 +450,7 @@ void blk_mq_tagset_busy_iter(struct blk_mq_tag_set *tagset, unsigned int flags = tagset->flags; int i, nr_tags, srcu_idx; - srcu_idx = srcu_read_lock(&tagset->tags_srcu); + srcu_idx = srcu_read_lock(&blk_mq_tags_srcu); nr_tags = blk_mq_is_shared_tags(flags) ? 1 : tagset->nr_hw_queues; @@ -452,7 +459,7 @@ void blk_mq_tagset_busy_iter(struct blk_mq_tag_set *tagset, __blk_mq_all_tag_iter(tagset->tags[i], fn, priv, BT_TAG_ITER_STARTED); } - srcu_read_unlock(&tagset->tags_srcu, srcu_idx); + srcu_read_unlock(&blk_mq_tags_srcu, srcu_idx); } EXPORT_SYMBOL(blk_mq_tagset_busy_iter); @@ -512,7 +519,7 @@ void blk_mq_queue_tag_busy_iter(struct request_queue *q, busy_tag_iter_fn *fn, if (!percpu_ref_tryget(&q->q_usage_counter)) return; - srcu_idx = srcu_read_lock(&q->tag_set->tags_srcu); + srcu_idx = srcu_read_lock(&blk_mq_tags_srcu); if (blk_mq_is_shared_tags(q->tag_set->flags)) { struct blk_mq_tags *tags = q->tag_set->shared_tags; struct sbitmap_queue *bresv = &tags->breserved_tags; @@ -542,7 +549,7 @@ void blk_mq_queue_tag_busy_iter(struct request_queue *q, busy_tag_iter_fn *fn, bt_for_each(hctx, q, btags, fn, priv, false); } } - srcu_read_unlock(&q->tag_set->tags_srcu, srcu_idx); + srcu_read_unlock(&blk_mq_tags_srcu, srcu_idx); blk_queue_exit(q); } @@ -618,7 +625,7 @@ void blk_mq_free_tags(struct blk_mq_tag_set *set, struct blk_mq_tags *tags) return; } - call_srcu(&set->tags_srcu, &tags->rcu_head, blk_mq_free_tags_callback); + call_srcu(&blk_mq_tags_srcu, &tags->rcu_head, blk_mq_free_tags_callback); } void blk_mq_tag_resize_shared_tags(struct blk_mq_tag_set *set, unsigned int size) diff --git a/block/blk-mq.c b/block/blk-mq.c index 7a37d9f1647d..d573123211e8 100644 --- a/block/blk-mq.c +++ b/block/blk-mq.c @@ -3684,9 +3684,9 @@ static bool blk_mq_hctx_has_requests(struct blk_mq_hw_ctx *hctx) }; int srcu_idx; - srcu_idx = srcu_read_lock(&hctx->queue->tag_set->tags_srcu); + srcu_idx = srcu_read_lock(&blk_mq_tags_srcu); blk_mq_all_tag_iter(tags, blk_mq_has_request, &data); - srcu_read_unlock(&hctx->queue->tag_set->tags_srcu, srcu_idx); + srcu_read_unlock(&blk_mq_tags_srcu, srcu_idx); return data.has_rq; } @@ -3958,7 +3958,7 @@ static void blk_mq_exit_hctx(struct request_queue *q, if (set->ops->exit_hctx) set->ops->exit_hctx(hctx, hctx_idx); - call_srcu(&set->tags_srcu, &hctx->fq->rcu_head, + call_srcu(&blk_mq_tags_srcu, &hctx->fq->rcu_head, blk_free_flush_queue_callback); hctx->fq = NULL; @@ -4885,9 +4885,6 @@ int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set) if (ret) goto out_free_srcu; } - ret = init_srcu_struct(&set->tags_srcu); - if (ret) - goto out_cleanup_srcu; init_rwsem(&set->update_nr_hwq_lock); @@ -4896,7 +4893,7 @@ int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set) sizeof(struct blk_mq_tags *), GFP_KERNEL, set->numa_node); if (!set->tags) - goto out_cleanup_tags_srcu; + goto out_cleanup_srcu; for (i = 0; i < set->nr_maps; i++) { set->map[i].mq_map = kcalloc_node(nr_cpu_ids, @@ -4925,8 +4922,6 @@ int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set) } kfree(set->tags); set->tags = NULL; -out_cleanup_tags_srcu: - cleanup_srcu_struct(&set->tags_srcu); out_cleanup_srcu: if (set->flags & BLK_MQ_F_BLOCKING) cleanup_srcu_struct(set->srcu); @@ -4973,8 +4968,6 @@ void blk_mq_free_tag_set(struct blk_mq_tag_set *set) kfree(set->tags); set->tags = NULL; - srcu_barrier(&set->tags_srcu); - cleanup_srcu_struct(&set->tags_srcu); if (set->flags & BLK_MQ_F_BLOCKING) { srcu_barrier(set->srcu); cleanup_srcu_struct(set->srcu); diff --git a/block/blk-mq.h b/block/blk-mq.h index aa15d31aaae9..e6b463da5071 100644 --- a/block/blk-mq.h +++ b/block/blk-mq.h @@ -178,6 +178,8 @@ struct blk_mq_tags *blk_mq_init_tags(unsigned int nr_tags, unsigned int reserved_tags, unsigned int flags, int node); void blk_mq_free_tags(struct blk_mq_tag_set *set, struct blk_mq_tags *tags); +extern struct srcu_struct blk_mq_tags_srcu; + unsigned int blk_mq_get_tag(struct blk_mq_alloc_data *data); unsigned long blk_mq_get_tags(struct blk_mq_alloc_data *data, int nr_tags, unsigned int *offset); diff --git a/include/linux/blk-mq.h b/include/linux/blk-mq.h index af878597afb8..5e130b282b42 100644 --- a/include/linux/blk-mq.h +++ b/include/linux/blk-mq.h @@ -525,8 +525,6 @@ enum hctx_type { * request_queue.tag_set_list. * @srcu: Use as lock when type of the request queue is blocking * (BLK_MQ_F_BLOCKING). - * @tags_srcu: SRCU used to defer freeing of tags page_list to prevent - * use-after-free when iterating tags. * @update_nr_hwq_lock: * Synchronize updating nr_hw_queues with add/del disk & * switching elevator. @@ -551,7 +549,6 @@ struct blk_mq_tag_set { struct mutex tag_list_lock; struct list_head tag_list; struct srcu_struct *srcu; - struct srcu_struct tags_srcu; struct rw_semaphore update_nr_hwq_lock; };