Commit 4c48aed6 authored by Zijun Hu's avatar Zijun Hu Committed by Greg Kroah-Hartman
Browse files

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: default avatarZijun Hu <zijun.hu@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20250903-fix_auxbus-v2-1-3eae8374fd65@oss.qualcomm.com


Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent eca71038
Loading
Loading
Loading
Loading
+8 −7
Original line number Diff line number Diff line
@@ -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), '.');
	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);
		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;