From d3f48c54fdff10fe7ae4ed4ee869e72367fbdce0 Mon Sep 17 00:00:00 2001 From: Christoph Hellwig Date: Wed, 19 Oct 2022 16:58:21 +0200 Subject: [PATCH] block: refactor elevator_type lookup 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 --- block/elevator.c | 61 ++++++++++++++++-------------------------------- 1 file changed, 20 insertions(+), 41 deletions(-) diff --git a/block/elevator.c b/block/elevator.c index 2c4da3f95515..131a2a831347 100644 --- a/block/elevator.c +++ b/block/elevator.c @@ -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); } -- 2.50.1