Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Zijun Hu and committed by
Greg Kroah-Hartman
4c48aed6 eca71038

+9 -8
+9 -8
drivers/base/auxiliary.c
··· 171 171 static const struct auxiliary_device_id *auxiliary_match_id(const struct auxiliary_device_id *id, 172 172 const struct auxiliary_device *auxdev) 173 173 { 174 + const char *auxdev_name = dev_name(&auxdev->dev); 175 + const char *p = strrchr(auxdev_name, '.'); 176 + int match_size; 177 + 178 + if (!p) 179 + return NULL; 180 + match_size = p - auxdev_name; 181 + 174 182 for (; id->name[0]; id++) { 175 - const char *p = strrchr(dev_name(&auxdev->dev), '.'); 176 - int match_size; 177 - 178 - if (!p) 179 - continue; 180 - match_size = p - dev_name(&auxdev->dev); 181 - 182 183 /* use dev_name(&auxdev->dev) prefix before last '.' char to match to */ 183 184 if (strlen(id->name) == match_size && 184 - !strncmp(dev_name(&auxdev->dev), id->name, match_size)) 185 + !strncmp(auxdev_name, id->name, match_size)) 185 186 return id; 186 187 } 187 188 return NULL;