diff options
author | Zijun Hu <zijun.hu@oss.qualcomm.com> | 2025-09-03 19:37:22 +0800 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 2025-09-06 20:12:36 +0200 |
commit | 4c48aed6dfcd32ea23e52adc1072405a62facf46 (patch) | |
tree | b4c55aec14dd31ade72c8b127dac526205a30528 | |
parent | eca710386972f2a72708b0b2a615eb150d218e18 (diff) |
driver core: auxiliary bus: Optimize logic of auxiliary_match_id()
auxiliary_match_id() repeatedly calculates variable @match_size in the
for loop, however, the variable is fixed actually, so it is enough to
only calculate the variable once.
Besides, the function should return directly if name of the @auxdev
does not include '.', but it still iterates over the ID table.
Additionally, statement 'dev_name(&auxdev->dev)' is fixed, but may be
evaluated more than 3 times.
Optimize logic of the function by:
- Move the logic calculating the variable out of the for loop
- Return NULL directly if @p == NULL
- Give the statement an dedicated local variable @auxdev_name
Signed-off-by: Zijun Hu <zijun.hu@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20250903-fix_auxbus-v2-1-3eae8374fd65@oss.qualcomm.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
-rw-r--r-- | drivers/base/auxiliary.c | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/drivers/base/auxiliary.c b/drivers/base/auxiliary.c index f65ba30d0617..04bdbff4dbe5 100644 --- a/drivers/base/auxiliary.c +++ b/drivers/base/auxiliary.c @@ -171,17 +171,18 @@ static const struct auxiliary_device_id *auxiliary_match_id(const struct auxiliary_device_id *id, const struct auxiliary_device *auxdev) { - for (; id->name[0]; id++) { - const char *p = strrchr(dev_name(&auxdev->dev), '.'); - int match_size; + const char *auxdev_name = dev_name(&auxdev->dev); + const char *p = strrchr(auxdev_name, '.'); + int match_size; - if (!p) - continue; - match_size = p - dev_name(&auxdev->dev); + if (!p) + return NULL; + match_size = p - auxdev_name; + for (; id->name[0]; id++) { /* use dev_name(&auxdev->dev) prefix before last '.' char to match to */ if (strlen(id->name) == match_size && - !strncmp(dev_name(&auxdev->dev), id->name, match_size)) + !strncmp(auxdev_name, id->name, match_size)) return id; } return NULL; |