[PATCH] block: introduce for_each_rq_qos macro

From: Chunguang Xu
Date: Wed Jan 06 2021 - 06:22:22 EST


From: Chunguang Xu <brookxu@xxxxxxxxxxx>

Compared to direct pointer traversal, introducing a macro
will make the code more concise. But the introduction of
macros will make rq_qos_xxx() produce a redundant judgment,
usually this may not be a big problem.

Signed-off-by: Chunguang Xu <brookxu@xxxxxxxxxxx>
---
block/blk-rq-qos.c | 45 ++++++++++++++++++---------------------------
block/blk-rq-qos.h | 11 +++++++++--
2 files changed, 27 insertions(+), 29 deletions(-)

diff --git a/block/blk-rq-qos.c b/block/blk-rq-qos.c
index 6564606..2c21a49 100644
--- a/block/blk-rq-qos.c
+++ b/block/blk-rq-qos.c
@@ -31,83 +31,74 @@ bool rq_wait_inc_below(struct rq_wait *rq_wait, unsigned int limit)

void __rq_qos_cleanup(struct rq_qos *rqos, struct bio *bio)
{
- do {
+ for_each_rq_qos(rqos) {
if (rqos->ops->cleanup)
rqos->ops->cleanup(rqos, bio);
- rqos = rqos->next;
- } while (rqos);
+ }
}

void __rq_qos_done(struct rq_qos *rqos, struct request *rq)
{
- do {
+ for_each_rq_qos(rqos) {
if (rqos->ops->done)
rqos->ops->done(rqos, rq);
- rqos = rqos->next;
- } while (rqos);
+ }
}

void __rq_qos_issue(struct rq_qos *rqos, struct request *rq)
{
- do {
+ for_each_rq_qos(rqos) {
if (rqos->ops->issue)
rqos->ops->issue(rqos, rq);
- rqos = rqos->next;
- } while (rqos);
+ }
}

void __rq_qos_requeue(struct rq_qos *rqos, struct request *rq)
{
- do {
+ for_each_rq_qos(rqos) {
if (rqos->ops->requeue)
rqos->ops->requeue(rqos, rq);
- rqos = rqos->next;
- } while (rqos);
+ }
}

void __rq_qos_throttle(struct rq_qos *rqos, struct bio *bio)
{
- do {
+ for_each_rq_qos(rqos) {
if (rqos->ops->throttle)
rqos->ops->throttle(rqos, bio);
- rqos = rqos->next;
- } while (rqos);
+ }
}

void __rq_qos_track(struct rq_qos *rqos, struct request *rq, struct bio *bio)
{
- do {
+ for_each_rq_qos(rqos) {
if (rqos->ops->track)
rqos->ops->track(rqos, rq, bio);
- rqos = rqos->next;
- } while (rqos);
+ }
}

void __rq_qos_merge(struct rq_qos *rqos, struct request *rq, struct bio *bio)
{
- do {
+ for_each_rq_qos(rqos) {
if (rqos->ops->merge)
rqos->ops->merge(rqos, rq, bio);
- rqos = rqos->next;
- } while (rqos);
+ }
}

void __rq_qos_done_bio(struct rq_qos *rqos, struct bio *bio)
{
- do {
+ for_each_rq_qos(rqos) {
if (rqos->ops->done_bio)
rqos->ops->done_bio(rqos, bio);
- rqos = rqos->next;
- } while (rqos);
+ }
}

void __rq_qos_queue_depth_changed(struct rq_qos *rqos)
{
- do {
+ for_each_rq_qos(rqos) {
if (rqos->ops->queue_depth_changed)
rqos->ops->queue_depth_changed(rqos);
- rqos = rqos->next;
- } while (rqos);
+ }
}

/*
diff --git a/block/blk-rq-qos.h b/block/blk-rq-qos.h
index 2bc43e9..bfa72a5 100644
--- a/block/blk-rq-qos.h
+++ b/block/blk-rq-qos.h
@@ -57,11 +57,18 @@ struct rq_depth {
unsigned int default_depth;
};

+/*
+ * for_each_rq_qos() allows you to iterate on each rqos
+ */
+#define for_each_rq_qos(rqos) \
+ for (; rqos; rqos = rqos->next)
+
static inline struct rq_qos *rq_qos_id(struct request_queue *q,
enum rq_qos_id id)
{
- struct rq_qos *rqos;
- for (rqos = q->rq_qos; rqos; rqos = rqos->next) {
+ struct rq_qos *rqos = q->rq_qos;
+
+ for_each_rq_qos(rqos) {
if (rqos->id == id)
break;
}
--
1.8.3.1