Skip to content

Commit dd69fb0

Browse files
zijun-hugregkh
authored andcommitted
driver core: Fix error handling in driver API device_rename()
[ Upstream commit 6d8249a ] For class-device, device_rename() failure maybe cause unexpected link name within its class folder as explained below: /sys/class/.../old_name -> /sys/devices/.../old_name device_rename(..., new_name) and failed /sys/class/.../new_name -> /sys/devices/.../old_name Fixed by undoing renaming link if renaming kobject failed. Fixes: f349cf3 ("driver core: Implement ns directory support for device classes.") Signed-off-by: Zijun Hu <quic_zijuhu@quicinc.com> Link: https://lore.kernel.org/r/20240722-device_rename_fix-v2-1-77de1a6c6495@quicinc.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 0f11588 commit dd69fb0

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

drivers/base/core.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4485,9 +4485,11 @@ EXPORT_SYMBOL_GPL(device_destroy);
44854485
*/
44864486
int device_rename(struct device *dev, const char *new_name)
44874487
{
4488+
struct subsys_private *sp = NULL;
44884489
struct kobject *kobj = &dev->kobj;
44894490
char *old_device_name = NULL;
44904491
int error;
4492+
bool is_link_renamed = false;
44914493

44924494
dev = get_device(dev);
44934495
if (!dev)
@@ -4502,7 +4504,7 @@ int device_rename(struct device *dev, const char *new_name)
45024504
}
45034505

45044506
if (dev->class) {
4505-
struct subsys_private *sp = class_to_subsys(dev->class);
4507+
sp = class_to_subsys(dev->class);
45064508

45074509
if (!sp) {
45084510
error = -EINVAL;
@@ -4511,16 +4513,19 @@ int device_rename(struct device *dev, const char *new_name)
45114513

45124514
error = sysfs_rename_link_ns(&sp->subsys.kobj, kobj, old_device_name,
45134515
new_name, kobject_namespace(kobj));
4514-
subsys_put(sp);
45154516
if (error)
45164517
goto out;
4518+
4519+
is_link_renamed = true;
45174520
}
45184521

45194522
error = kobject_rename(kobj, new_name);
4520-
if (error)
4521-
goto out;
4522-
45234523
out:
4524+
if (error && is_link_renamed)
4525+
sysfs_rename_link_ns(&sp->subsys.kobj, kobj, new_name,
4526+
old_device_name, kobject_namespace(kobj));
4527+
subsys_put(sp);
4528+
45244529
put_device(dev);
45254530

45264531
kfree(old_device_name);

0 commit comments

Comments
 (0)