On Fri, May 08, 2020 at 05:18:56PM -0700, Bart Van Assche wrote: > On 2020-05-08 16:32, Bart Van Assche wrote: > > On 2020-05-04 19:09, Ming Lei wrote: > >> @@ -310,19 +313,30 @@ static void bt_tags_for_each(struct blk_mq_tags *tags, struct sbitmap_queue *bt, > >> /** > >> * blk_mq_all_tag_busy_iter - iterate over all started requests in a tag map > >> * @tags: Tag map to iterate over. > >> - * @fn: Pointer to the function that will be called for each started > >> - * request. @fn will be called as follows: @fn(rq, @priv, > >> - * reserved) where rq is a pointer to a request. 'reserved' > >> - * indicates whether or not @rq is a reserved request. Return > >> - * true to continue iterating tags, false to stop. > >> + * @fn: Pointer to the function that will be called for each request > >> + * when .busy_rq_fn(rq) returns true. @fn will be called as > >> + * follows: @fn(rq, @priv, reserved) where rq is a pointer to a > >> + * request. 'reserved' indicates whether or not @rq is a reserved > >> + * request. Return true to continue iterating tags, false to stop. > >> + * @busy_rq_fn: Pointer to the function that will be called for each request, > >> + * @busy_rq_fn's type is same with @fn. Only when @busy_rq_fn(rq, > >> + * @priv, reserved) returns true, @fn will be called on this rq. > >> * @priv: Will be passed as second argument to @fn. > >> */ > >> -static void blk_mq_all_tag_busy_iter(struct blk_mq_tags *tags, > >> - busy_tag_iter_fn *fn, void *priv) > >> +void blk_mq_all_tag_busy_iter(struct blk_mq_tags *tags, > >> + busy_tag_iter_fn *fn, busy_rq_iter_fn *busy_rq_fn, > >> + void *priv) > >> { > > > > The name 'busy_rq_fn' is not ideal because it is named after one > > specific use case, namely checking whether or not a request is busy (has > > already been started). How about using the name 'pred_fn' ('pred' from > > predicate because it controls whether the other function is called)? > > Since only the context that passes 'fn' can know what data structure > > 'priv' points to and since 'busy_rq_fn' is passed from another context, > > can 'busy_rq_fn' even know what data 'priv' points at? Has it been > > considered not to pass the 'priv' argument to 'busy_rq_fn'? > > Thinking further about this, another possible approach is not to modify > blk_mq_all_tag_busy_iter() at all and to introduce a new function that > iterates over all requests instead of only over busy requests. I think > that approach will result in easier to read code than patch 5/11 because > each of these request iteration functions will only accept a single > callback function pointer. Additionally, that approach will make the > following function superfluous (from patch 7/11): > > +static bool blk_mq_inflight_rq(struct request *rq, void *data, > + bool reserved) > +{ > + return rq->tag >= 0; > +} Fine, then we can save one callback, how about the following way? diff --git a/block/blk-mq-tag.c b/block/blk-mq-tag.c index 586c9d6e904a..5e9c743d887b 100644 --- a/block/blk-mq-tag.c +++ b/block/blk-mq-tag.c @@ -257,6 +257,7 @@ struct bt_tags_iter_data { busy_tag_iter_fn *fn; void *data; bool reserved; + bool iterate_all; }; static bool bt_tags_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data) @@ -274,8 +275,10 @@ static bool bt_tags_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data) * test and set the bit before assining ->rqs[]. */ rq = tags->rqs[bitnr]; - if (rq && blk_mq_request_started(rq)) - return iter_data->fn(rq, iter_data->data, reserved); + if (rq) { + if (iter_data->iterate_all || blk_mq_request_started(rq)) + return iter_data->fn(rq, iter_data->data, reserved); + } return true; } @@ -294,13 +297,15 @@ static bool bt_tags_iter(struct sbitmap *bitmap, unsigned int bitnr, void *data) * bitmap_tags member of struct blk_mq_tags. */ static void bt_tags_for_each(struct blk_mq_tags *tags, struct sbitmap_queue *bt, - busy_tag_iter_fn *fn, void *data, bool reserved) + busy_tag_iter_fn *fn, void *data, bool reserved, + bool iterate_all) { struct bt_tags_iter_data iter_data = { .tags = tags, .fn = fn, .data = data, .reserved = reserved, + .iterate_all = iterate_all, }; if (tags->rqs) @@ -321,8 +326,30 @@ static void blk_mq_all_tag_busy_iter(struct blk_mq_tags *tags, busy_tag_iter_fn *fn, void *priv) { if (tags->nr_reserved_tags) - bt_tags_for_each(tags, &tags->breserved_tags, fn, priv, true); - bt_tags_for_each(tags, &tags->bitmap_tags, fn, priv, false); + bt_tags_for_each(tags, &tags->breserved_tags, fn, priv, true, + false); + bt_tags_for_each(tags, &tags->bitmap_tags, fn, priv, false, false); +} + +/** + * blk_mq_all_tag_iter - iterate over all requests in a tag map + * @tags: Tag map to iterate over. + * @fn: Pointer to the function that will be called for each + * request. @fn will be called as follows: @fn(rq, @priv, + * reserved) where rq is a pointer to a request. 'reserved' + * indicates whether or not @rq is a reserved request. Return + * true to continue iterating tags, false to stop. + * @priv: Will be passed as second argument to @fn. + * + * It is the caller's responsility to check rq's state in @fn. + */ +void blk_mq_all_tag_iter(struct blk_mq_tags *tags, busy_tag_iter_fn *fn, + void *priv) +{ + if (tags->nr_reserved_tags) + bt_tags_for_each(tags, &tags->breserved_tags, fn, priv, true, + true); + bt_tags_for_each(tags, &tags->bitmap_tags, fn, priv, false, true); } /** diff --git a/block/blk-mq-tag.h b/block/blk-mq-tag.h index 2b8321efb682..d19546e8246b 100644 --- a/block/blk-mq-tag.h +++ b/block/blk-mq-tag.h @@ -34,6 +34,8 @@ extern int blk_mq_tag_update_depth(struct blk_mq_hw_ctx *hctx, extern void blk_mq_tag_wakeup_all(struct blk_mq_tags *tags, bool); void blk_mq_queue_tag_busy_iter(struct request_queue *q, busy_iter_fn *fn, void *priv); +void blk_mq_all_tag_iter(struct blk_mq_tags *tags, busy_tag_iter_fn *fn, + void *priv); static inline struct sbq_wait_state *bt_wait_ptr(struct sbitmap_queue *bt, struct blk_mq_hw_ctx *hctx) thanks, Ming