Skip to contentSkip to conversation

misc elevator code cleanups

PATCH In review Christoph Hellwig 3 participants 14 messages Oct 30, 2022 – Nov 1, 2022 Block layer

lore
Share & tools
lore
#0 lore raw

Hi Jens,

this series has a bunch of random elevator cleanups.

Diffstat:

 blk-mq-sched.c |    7 --
 blk-mq.c       |    2 
 blk.h          |    1 
 elevator.c     |  175 +++++++++++++++++++++++----------------------------------
 4 files changed, 73 insertions(+), 112 deletions(-)
#1 lore raw

PATCH 1/7 block: drop the duplicate check in elv_register

We have less than a handful of elevators, and if someone adds a duplicate one it simply will never be found but other be harmless.

---
 block/elevator.c | 6 ------
 1 file changed, 6 deletions(-)
block/elevator.c +0 -6
index d26aa787e29f0..ef9af17293ffb 100644
@@ -545,13 +545,7 @@ int elv_register(struct elevator_type *e)
 			return -ENOMEM;
 	}
 
-	/* register, don't allow duplicate names */
 	spin_lock(&elv_list_lock);
-	if (elevator_find(e->elevator_name, 0)) {
-		spin_unlock(&elv_list_lock);
-		kmem_cache_destroy(e->icq_cache);
-		return -EBUSY;
-	}
 	list_add_tail(&e->list, &elv_list);
 	spin_unlock(&elv_list_lock);
 
Signature · 1 line
2.30.2
maintainer
#2 lore raw

Re: [PATCH 1/7] block: drop the duplicate check in elv_register

Quoted 2 lines · Christoph Hellwig

We have less than a handful of elevators, and if someone adds a duplicate one it simply will never be found but other be harmless.

That isn't quite parseable...

Quoted 7 lines
block/elevator.c +0 -0
index d26aa787e29f0..ef9af17293ffb 100644
@@ -545,13 +545,7 @@ int elv_register(struct elevator_type *e)
 			return -ENOMEM;
 	}

block/elevator.c int elv_register(struct elevator_type *e) · line 550

Quoted 10 lines
block/elevator.c +0 -6
 
-	/* register, don't allow duplicate names */
 	spin_lock(&elv_list_lock);
-	if (elevator_find(e->elevator_name, 0)) {
-		spin_unlock(&elv_list_lock);
-		kmem_cache_destroy(e->icq_cache);
-		return -EBUSY;
-	}
 	list_add_tail(&e->list, &elv_list);
 	spin_unlock(&elv_list_lock);

What's the idea behind this? Yes it'll be harmless and list ordering will dictate which one will be found, leaving the other(s) dead, but why not catch this upfront? I agree likelihood of this ever happening to be tiny, but seems like a good idea to catch and return BUSY for this case.

Signature · 1 line
Jens Axboe
#3 lore raw

Re: [PATCH 1/7] block: drop the duplicate check in elv_register

Quoted 7 lines · Jens Axboe

block/elevator.c int elv_register(struct elevator_type *e) · line 550

Quoted 2 lines
block/elevator.c +0 -0
 	list_add_tail(&e->list, &elv_list);
 	spin_unlock(&elv_list_lock);

What's the idea behind this? Yes it'll be harmless and list ordering will dictate which one will be found, leaving the other(s) dead, but why not catch this upfront? I agree likelihood of this ever happening to be tiny, but seems like a good idea to catch and return BUSY for this case.

Because it's just not very useful code bloat here that I stumbled upon. But I can just drop it if you prefer.

#4 lore raw

PATCH 2/7 block: cleanup elevator_get

In reply to Christoph Hellwig

Do the request_module and repeated lookup in the only caller that cares, pick a saner name that explains where are actually doing a lookup and use a sane calling conventions that passes the queue first.

---
 block/elevator.c | 25 ++++++++++---------------
 1 file changed, 10 insertions(+), 15 deletions(-)
block/elevator.c +10 -15
index ef9af17293ffb..d0e48839f6764 100644
@@ -132,24 +132,15 @@ static struct elevator_type *elevator_find(const char *name,
 	return NULL;
 }
 
-static struct elevator_type *elevator_get(struct request_queue *q,
-					  const char *name, bool try_loading)
+static struct elevator_type *elevator_find_get(struct request_queue *q,
+		const char *name)
 {
 	struct elevator_type *e;
 
 	spin_lock(&elv_list_lock);
-
 	e = elevator_find(name, q->required_elevator_features);
-	if (!e && try_loading) {
-		spin_unlock(&elv_list_lock);
-		request_module("%s-iosched", name);
-		spin_lock(&elv_list_lock);
-		e = elevator_find(name, q->required_elevator_features);
-	}
-
 	if (e && !elevator_tryget(e))
 		e = NULL;
-
 	spin_unlock(&elv_list_lock);
 	return e;
 }
@@ -628,7 +619,7 @@ static struct elevator_type *elevator_get_default(struct request_queue *q)
 	    !blk_mq_is_shared_tags(q->tag_set->flags))
 		return NULL;
 
-	return elevator_get(q, "mq-deadline", false);
+	return elevator_find_get(q, "mq-deadline");
 }
 
 /*
@@ -751,9 +742,13 @@ static int elevator_change(struct request_queue *q, const char *elevator_name)
 	if (q->elevator && elevator_match(q->elevator->type, elevator_name, 0))
 		return 0;
 
-	e = elevator_get(q, elevator_name, true);
-	if (!e)
-		return -EINVAL;
+	e = elevator_find_get(q, elevator_name);
+	if (!e) {
+		request_module("%s-iosched", elevator_name);
+		e = elevator_find_get(q, elevator_name);
+		if (!e)
+			return -EINVAL;
+	}
 	ret = elevator_switch(q, e);
 	elevator_put(e);
 	return ret;
Signature · 1 line
2.30.2
#5 lore raw

PATCH 3/7 block: exit elv_iosched_show early when I/O schedulers are not supported

In reply to Christoph Hellwig

If the tag_set has BLK_MQ_F_NO_SCHED flag set we will never show any scheduler, so exit early.

---
 block/elevator.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
block/elevator.c +2 -3
index d0e48839f6764..92096e5aabd36 100644
@@ -777,7 +777,7 @@ ssize_t elv_iosched_show(struct request_queue *q, char *name)
 	struct elevator_type *__e;
 	int len = 0;
 
-	if (!queue_is_mq(q))
+	if (!elv_support_iosched(q))
 		return sprintf(name, "none\n");
 
 	if (!q->elevator)
@@ -791,8 +791,7 @@ ssize_t elv_iosched_show(struct request_queue *q, char *name)
 			len += sprintf(name+len, "[%s] ", elv->elevator_name);
 			continue;
 		}
-		if (elv_support_iosched(q) &&
-		    elevator_match(__e, __e->elevator_name,
+		if (elevator_match(__e, __e->elevator_name,
 				   q->required_elevator_features))
 			len += sprintf(name+len, "%s ", __e->elevator_name);
 	}
Signature · 1 line
2.30.2
#6 lore raw

PATCH 4/7 block: cleanup the variable naming in elv_iosched_store

In reply to Christoph Hellwig

Use eq for the elevator_queue as done elsewhere. This frees e to be used for the loop iterator instead of the odd __ prefix. In addition rename elv to cur to make it more clear it is the currently selected elevator.

---
 block/elevator.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)
block/elevator.c +8 -9
index 92096e5aabd36..4fc0d2f539295 100644
@@ -772,9 +772,8 @@ ssize_t elv_iosched_store(struct request_queue *q, const char *buf,
 
 ssize_t elv_iosched_show(struct request_queue *q, char *name)
 {
-	struct elevator_queue *e = q->elevator;
-	struct elevator_type *elv = NULL;
-	struct elevator_type *__e;
+	struct elevator_queue *eq = q->elevator;
+	struct elevator_type *cur = NULL, *e;
 	int len = 0;
 
 	if (!elv_support_iosched(q))
@@ -783,17 +782,17 @@ ssize_t elv_iosched_show(struct request_queue *q, char *name)
 	if (!q->elevator)
 		len += sprintf(name+len, "[none] ");
 	else
-		elv = e->type;
+		cur = eq->type;
 
 	spin_lock(&elv_list_lock);
-	list_for_each_entry(__e, &elv_list, list) {
-		if (elv && elevator_match(elv, __e->elevator_name, 0)) {
-			len += sprintf(name+len, "[%s] ", elv->elevator_name);
+	list_for_each_entry(e, &elv_list, list) {
+		if (cur && elevator_match(cur, e->elevator_name, 0)) {
+			len += sprintf(name+len, "[%s] ", cur->elevator_name);
 			continue;
 		}
-		if (elevator_match(__e, __e->elevator_name,
+		if (elevator_match(e, e->elevator_name,
 				   q->required_elevator_features))
-			len += sprintf(name+len, "%s ", __e->elevator_name);
+			len += sprintf(name+len, "%s ", e->elevator_name);
 	}
 	spin_unlock(&elv_list_lock);
 
Signature · 1 line
2.30.2
#7 lore raw

PATCH 5/7 block: simplify the check for the current elevator in elv_iosched_show

In reply to Christoph Hellwig

Just compare the pointers instead of using the string based elevator_match.

---
 block/elevator.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
block/elevator.c +1 -1
index 4fc0d2f539295..77c16c5ef04ff 100644
@@ -786,7 +786,7 @@ ssize_t elv_iosched_show(struct request_queue *q, char *name)
 
 	spin_lock(&elv_list_lock);
 	list_for_each_entry(e, &elv_list, list) {
-		if (cur && elevator_match(cur, e->elevator_name, 0)) {
+		if (e == cur) {
 			len += sprintf(name+len, "[%s] ", cur->elevator_name);
 			continue;
 		}
Signature · 1 line
2.30.2
#8 lore raw

PATCH 6/7 block: don't check for required features in elevator_match

In reply to Christoph Hellwig

Checking for the required features in the callers simplifies the code quite a bit, so do that.

---
 block/elevator.c | 49 +++++++++++++++---------------------------------
 1 file changed, 15 insertions(+), 34 deletions(-)
block/elevator.c +15 -34
index 77c16c5ef04ff..4042e524333e0 100644
@@ -83,10 +83,11 @@ bool elv_bio_merge_ok(struct request *rq, struct bio *bio)
 }
 EXPORT_SYMBOL(elv_bio_merge_ok);
 
-static inline bool elv_support_features(unsigned int elv_features,
-					unsigned int required_features)
+static inline bool elv_support_features(struct request_queue *q,
+		const struct elevator_type *e)
 {
-	return (required_features & elv_features) == required_features;
+	return (q->required_elevator_features & e->elevator_features) ==
+		q->required_elevator_features;
 }
 
 /**
@@ -98,37 +99,19 @@ static inline bool elv_support_features(unsigned int elv_features,
  * Return true if the elevator @e name matches @name and if @e provides all
  * the features specified by @required_features.
  */
-static bool elevator_match(const struct elevator_type *e, const char *name,
-			   unsigned int required_features)
+static bool elevator_match(const struct elevator_type *e, const char *name)
 {
-	if (!elv_support_features(e->elevator_features, required_features))
-		return false;
-	if (!strcmp(e->elevator_name, name))
-		return true;
-	if (e->elevator_alias && !strcmp(e->elevator_alias, name))
-		return true;
-
-	return false;
+	return !strcmp(e->elevator_name, name) ||
+		(e->elevator_alias && !strcmp(e->elevator_alias, name));
 }
 
-/**
- * elevator_find - Find an elevator
- * @name: Name of the elevator to find
- * @required_features: Features that the elevator must provide
- *
- * Return the first registered scheduler with name @name and supporting the
- * features @required_features and NULL otherwise.
- */
-static struct elevator_type *elevator_find(const char *name,
-					   unsigned int required_features)
+static struct elevator_type *__elevator_find(const char *name)
 {
 	struct elevator_type *e;
 
-	list_for_each_entry(e, &elv_list, list) {
-		if (elevator_match(e, name, required_features))
+	list_for_each_entry(e, &elv_list, list)
+		if (elevator_match(e, name))
 			return e;
-	}
-
 	return NULL;
 }
 
@@ -138,8 +121,8 @@ static struct elevator_type *elevator_find_get(struct request_queue *q,
 	struct elevator_type *e;
 
 	spin_lock(&elv_list_lock);
-	e = elevator_find(name, q->required_elevator_features);
-	if (e && !elevator_tryget(e))
+	e = __elevator_find(name);
+	if (e && (!elv_support_features(q, e) || !elevator_tryget(e)))
 		e = NULL;
 	spin_unlock(&elv_list_lock);
 	return e;
@@ -633,8 +616,7 @@ static struct elevator_type *elevator_get_by_features(struct request_queue *q)
 	spin_lock(&elv_list_lock);
 
 	list_for_each_entry(e, &elv_list, list) {
-		if (elv_support_features(e->elevator_features,
-					 q->required_elevator_features)) {
+		if (elv_support_features(q, e)) {
 			found = e;
 			break;
 		}
@@ -739,7 +721,7 @@ static int elevator_change(struct request_queue *q, const char *elevator_name)
 		return elevator_switch(q, NULL);
 	}
 
-	if (q->elevator && elevator_match(q->elevator->type, elevator_name, 0))
+	if (q->elevator && elevator_match(q->elevator->type, elevator_name))
 		return 0;
 
 	e = elevator_find_get(q, elevator_name);
@@ -790,8 +772,7 @@ ssize_t elv_iosched_show(struct request_queue *q, char *name)
 			len += sprintf(name+len, "[%s] ", cur->elevator_name);
 			continue;
 		}
-		if (elevator_match(e, e->elevator_name,
-				   q->required_elevator_features))
+		if (elv_support_features(q, e))
 			len += sprintf(name+len, "%s ", e->elevator_name);
 	}
 	spin_unlock(&elv_list_lock);
Signature · 1 line
2.30.2
#9 lore raw

PATCH 7/7 block: split elevator_switch

In reply to Christoph Hellwig

Split an elevator_disable helper from elevator_switch for the case where we want to switch to no scheduler at all. This includes removing the pointless elevator_switch_mq helper and removing the switch to no schedule logic from blk_mq_init_sched.

---
 block/blk-mq-sched.c |  7 ----
 block/blk-mq.c       |  2 +-
 block/blk.h          |  1 +
 block/elevator.c     | 77 ++++++++++++++++++++++----------------------
 4 files changed, 40 insertions(+), 47 deletions(-)
block/blk-mq-sched.c +0 -7
index 68227240fdea3..23d1a90fec427 100644
@@ -564,13 +564,6 @@ int blk_mq_init_sched(struct request_queue *q, struct elevator_type *e)
 	unsigned long i;
 	int ret;
 
-	if (!e) {
-		blk_queue_flag_clear(QUEUE_FLAG_SQ_SCHED, q);
-		q->elevator = NULL;
-		q->nr_requests = q->tag_set->queue_depth;
-		return 0;
-	}
-
 	/*
 	 * Default to double of smaller one between hw queue_depth and 128,
 	 * since we don't split into sync/async like the old code did.
block/blk-mq.c +1 -1
index 4cecf281123f6..1ebb2e68ac66f 100644
@@ -4556,7 +4556,7 @@ static bool blk_mq_elv_switch_none(struct list_head *head,
 	__elevator_get(qe->type);
 	qe->type = q->elevator->type;
 	list_add(&qe->node, head);
-	elevator_switch(q, NULL);
+	elevator_disable(q);
 	mutex_unlock(&q->sysfs_lock);
 
 	return true;
block/blk.h +1 -0
index 7f9e089ab1f75..f1398fb96cec9 100644
@@ -278,6 +278,7 @@ bool blk_bio_list_merge(struct request_queue *q, struct list_head *list,
 void blk_insert_flush(struct request *rq);
 
 int elevator_switch(struct request_queue *q, struct elevator_type *new_e);
+void elevator_disable(struct request_queue *q);
 void elevator_exit(struct request_queue *q);
 int elv_register_queue(struct request_queue *q, bool uevent);
 void elv_unregister_queue(struct request_queue *q);
block/elevator.c +38 -39

105 lines not on this page · raw message

Signature · 1 line
2.30.2
#10 lore raw
In reply to Christoph Hellwig

Hi, Christoph!

Only elevator_find_get is calling __elevator_find after applying the series. Maybe we can just remove __elevator_find and move the list iterating logic into elevator_find_get?

Thanks!
Jinlong Chen

#11 lore raw
Quoted 5 lines · Jinlong Chen

Hi, Christoph!

Only elevator_find_get is calling __elevator_find after applying the series. Maybe we can just remove __elevator_find and move the list iterating logic into elevator_find_get?

We could. But then we'd need another local variable to track what was found, so I'm not sure it is a win. In general having a pure list lookup in a helper while all the locking and refcounting in a wrapper around it tends to be a quite nice pattern.

#12 lore raw
Quoted 8 lines
Quoted 3 lines

Only elevator_find_get is calling __elevator_find after applying the series. Maybe we can just remove __elevator_find and move the list iterating logic into elevator_find_get?

We could. But then we'd need another local variable to track what was found, so I'm not sure it is a win. In general having a pure list lookup in a helper while all the locking and refcounting in a wrapper around it tends to be a quite nice pattern.

Got it, thank you for your answer and patience!

Thanks!
Jinlong Chen

maintainer Applied
#13 lore raw

Re: (subset) misc elevator code cleanups

In reply to Christoph Hellwig
Quoted 10 lines · Christoph Hellwig

this series has a bunch of random elevator cleanups.

Diffstat:

 blk-mq-sched.c |    7 --
 blk-mq.c       |    2
 blk.h          |    1
 elevator.c     |  175 +++++++++++++++++++++++----------------------------------
 4 files changed, 73 insertions(+), 112 deletions(-)

[...]

Applied, thanks!

[2/7] block: cleanup elevator_get

      commit: 81eaca442ea962c43bdb1e9cbb9eddb41b97491d
[3/7] block: exit elv_iosched_show early when I/O schedulers are not supported
      commit: aae2a643f508d768b65e59da447f3b11688db3cf
[4/7] block: cleanup the variable naming in elv_iosched_store
      commit: 16095af2fa2c3089ff1162e677d6596772f6f478
[5/7] block: simplify the check for the current elevator in elv_iosched_show
      commit: 2eef17a209ab4d77923222045d462d379d6ef692
[6/7] block: don't check for required features in elevator_match
      commit: bc3caee659d70addf58dde216d10a5589ab9ec73
[7/7] block: split elevator_switch
      commit: 817b4eed4a21cda7dad3002b23901156abdb7c36

Best regards,

Signature · 1 line
Jens Axboe