]> www.infradead.org Git - users/hch/block.git/commitdiff
block: refactor elevator_type lookup
authorChristoph Hellwig <hch@lst.de>
Wed, 19 Oct 2022 14:58:21 +0000 (16:58 +0200)
committerChristoph Hellwig <hch@lst.de>
Wed, 19 Oct 2022 16:05:54 +0000 (18:05 +0200)
Refactor how an elevator_type is looked up by name by moving the
try_module_get into the main helper, and by doing the request_module
in the only caller that cares.

Signed-off-by: Christoph Hellwig <hch@lst.de>
block/elevator.c

index 2c4da3f955152ca84e49b27b97220a9942f589bc..131a2a831347cd3de7cb01803051f33cda4801f3 100644 (file)
@@ -111,25 +111,22 @@ static bool elevator_match(const struct elevator_type *e, const char *name,
        return false;
 }
 
-/**
- * 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_get(struct request_queue *q,
+               const char *name)
 {
-       struct elevator_type *e;
+       struct elevator_type *e, *found = NULL;
 
+       spin_lock(&elv_list_lock);
        list_for_each_entry(e, &elv_list, list) {
-               if (elevator_match(e, name, required_features))
-                       return e;
+               if (elevator_match(e, name, q->required_elevator_features)) {
+                       found = e;
+                       break;
+               }
        }
-
-       return NULL;
+       if (found && !try_module_get(found->elevator_owner))
+               found = NULL;
+       spin_unlock(&elv_list_lock);
+       return found;
 }
 
 static void elevator_put(struct elevator_type *e)
@@ -137,28 +134,6 @@ static void elevator_put(struct elevator_type *e)
        module_put(e->elevator_owner);
 }
 
-static struct elevator_type *elevator_get(struct request_queue *q,
-                                         const char *name, bool try_loading)
-{
-       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 && !try_module_get(e->elevator_owner))
-               e = NULL;
-
-       spin_unlock(&elv_list_lock);
-       return e;
-}
-
 static struct kobj_type elv_ktype;
 
 struct elevator_queue *elevator_alloc(struct request_queue *q,
@@ -636,7 +611,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");
 }
 
 /*
@@ -761,9 +736,13 @@ static int __elevator_change(struct request_queue *q, const char *name)
        if (q->elevator && elevator_match(q->elevator->type, name, 0))
                return 0;
 
-       e = elevator_get(q, name, true);
-       if (!e)
-               return -EINVAL;
+       e = elevator_find_get(q, name);
+       if (!e) {
+               request_module("%s-iosched", name);
+               e = elevator_find_get(q, name);
+               if (!e)
+                       return -EINVAL;
+       }
 
        return elevator_switch(q, e);
 }