blk-mq: for_each_* macro correctness

This change “blk-mq: for_each_* macro correctness” (commit 0d0b7d4) in Linux kernel is authored by Jose Alonso <joalonsof [at] gmail.com> on Tue Jan 28 08:09:46 2014 -0700.

Description of "blk-mq: for_each_* macro correctness"

The change “blk-mq: for_each_* macro correctness” introduces changes as follows.

blk-mq: for_each_* macro correctness

I observed that there are for_each macros that do an extra memory access
beyond the defined area.
Normally this does not cause problems.
But, this can cause exceptions. For example: if the area is allocated at
the end of a page and the next page is not accessible.

For correctness, I suggest changing the arguments of the 'for loop' like
others 'for_each' do in the kernel.

Signed-off-by: Jose Alonso <joalonsof@gmail.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>

Linux kernel releases containing commit 0d0b7d4

The Linux kernel releases containing this commit are as follows.

Linux kernel code changes from "blk-mq: for_each_* macro correctness"

There are 12 lines of Linux source code added/deleted in this change. Code changes to Linux kernel are as follows.

 include/linux/blk-mq.h | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
 
diff --git a/include/linux/blk-mq.h b/include/linux/blk-mq.h
index 851d34b7ac26..161b23105b1e 100644
--- a/include/linux/blk-mq.h
+++ b/include/linux/blk-mq.h
@@ -158,16 +158,16 @@ static inline struct request *blk_mq_tag_to_rq(struct blk_mq_hw_ctx *hctx,
 }
 
 #define queue_for_each_hw_ctx(q, hctx, i)				\
-	for ((i) = 0, hctx = (q)->queue_hw_ctx[0];			\
-	     (i) < (q)->nr_hw_queues; (i)++, hctx = (q)->queue_hw_ctx[i])
+	for ((i) = 0; (i) < (q)->nr_hw_queues &&			\
+	     ({ hctx = (q)->queue_hw_ctx[i]; 1; }); (i)++)
 
 #define queue_for_each_ctx(q, ctx, i)					\
-	for ((i) = 0, ctx = per_cpu_ptr((q)->queue_ctx, 0);		\
-	     (i) < (q)->nr_queues; (i)++, ctx = per_cpu_ptr(q->queue_ctx, (i)))
+	for ((i) = 0; (i) < (q)->nr_queues &&				\
+	     ({ ctx = per_cpu_ptr((q)->queue_ctx, (i)); 1; }); (i)++)
 
 #define hctx_for_each_ctx(hctx, ctx, i)					\
-	for ((i) = 0, ctx = (hctx)->ctxs[0];				\
-	     (i) < (hctx)->nr_ctx; (i)++, ctx = (hctx)->ctxs[(i)])
+	for ((i) = 0; (i) < (hctx)->nr_ctx &&				\
+	     ({ ctx = (hctx)->ctxs[(i)]; 1; }); (i)++)
 
 #define blk_ctx_sum(q, sum)						\
 ({									\

The commit for this change in Linux stable tree is 0d0b7d4 (patch).

Last modified: 2020/02/10