rpmsg: Fix calling device_lock() on non-initialized device
commit bb17d110cbf270d5247a6e261c5ad50e362d1675 upstream. driver_set_override() helper uses device_lock() so it should not be called before rpmsg_register_device() (which calls device_register()). Effect can be seen with CONFIG_DEBUG_MUTEXES: DEBUG_LOCKS_WARN_ON(lock->magic != lock) WARNING: CPU: 3 PID: 57 at kernel/locking/mutex.c:582 __mutex_lock+0x1ec/0x430 ... Call trace: __mutex_lock+0x1ec/0x430 mutex_lock_nested+0x44/0x50 driver_set_override+0x124/0x150 qcom_glink_native_probe+0x30c/0x3b0 glink_rpm_probe+0x274/0x350 platform_probe+0x6c/0xe0 really_probe+0x17c/0x3d0 __driver_probe_device+0x114/0x190 driver_probe_device+0x3c/0xf0 ... Refactor the rpmsg_register_device() function to use two-step device registering (initialization + add) and call driver_set_override() in proper moment. This moves the code around, so while at it also NULL-ify the rpdev->driver_override in error path to be sure it won't be kfree() second time. Fixes: 42cd402b8fd4 ("rpmsg: Fix kfree() of static memory on setting driver_override") Reported-by: Marek Szyprowski <m.szyprowski@samsung.com> Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org> Tested-by: Marek Szyprowski <m.szyprowski@samsung.com> Link: https://lore.kernel.org/r/20220429195946.1061725-2-krzysztof.kozlowski@linaro.org Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Lee Jones <lee@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
parent
44130fdb6a
commit
d0d3bdccbf
3 changed files with 39 additions and 16 deletions
|
@ -566,24 +566,51 @@ static struct bus_type rpmsg_bus = {
|
||||||
.remove = rpmsg_dev_remove,
|
.remove = rpmsg_dev_remove,
|
||||||
};
|
};
|
||||||
|
|
||||||
int rpmsg_register_device(struct rpmsg_device *rpdev)
|
/*
|
||||||
|
* A helper for registering rpmsg device with driver override and name.
|
||||||
|
* Drivers should not be using it, but instead rpmsg_register_device().
|
||||||
|
*/
|
||||||
|
int rpmsg_register_device_override(struct rpmsg_device *rpdev,
|
||||||
|
const char *driver_override)
|
||||||
{
|
{
|
||||||
struct device *dev = &rpdev->dev;
|
struct device *dev = &rpdev->dev;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
|
if (driver_override)
|
||||||
|
strcpy(rpdev->id.name, driver_override);
|
||||||
|
|
||||||
dev_set_name(&rpdev->dev, "%s.%s.%d.%d", dev_name(dev->parent),
|
dev_set_name(&rpdev->dev, "%s.%s.%d.%d", dev_name(dev->parent),
|
||||||
rpdev->id.name, rpdev->src, rpdev->dst);
|
rpdev->id.name, rpdev->src, rpdev->dst);
|
||||||
|
|
||||||
rpdev->dev.bus = &rpmsg_bus;
|
rpdev->dev.bus = &rpmsg_bus;
|
||||||
|
|
||||||
ret = device_register(&rpdev->dev);
|
device_initialize(dev);
|
||||||
|
if (driver_override) {
|
||||||
|
ret = driver_set_override(dev, &rpdev->driver_override,
|
||||||
|
driver_override,
|
||||||
|
strlen(driver_override));
|
||||||
if (ret) {
|
if (ret) {
|
||||||
dev_err(dev, "device_register failed: %d\n", ret);
|
dev_err(dev, "device_set_override failed: %d\n", ret);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = device_add(dev);
|
||||||
|
if (ret) {
|
||||||
|
dev_err(dev, "device_add failed: %d\n", ret);
|
||||||
|
kfree(rpdev->driver_override);
|
||||||
|
rpdev->driver_override = NULL;
|
||||||
put_device(&rpdev->dev);
|
put_device(&rpdev->dev);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
EXPORT_SYMBOL(rpmsg_register_device_override);
|
||||||
|
|
||||||
|
int rpmsg_register_device(struct rpmsg_device *rpdev)
|
||||||
|
{
|
||||||
|
return rpmsg_register_device_override(rpdev, NULL);
|
||||||
|
}
|
||||||
EXPORT_SYMBOL(rpmsg_register_device);
|
EXPORT_SYMBOL(rpmsg_register_device);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
@ -89,19 +89,7 @@ struct device *rpmsg_find_device(struct device *parent,
|
||||||
*/
|
*/
|
||||||
static inline int rpmsg_chrdev_register_device(struct rpmsg_device *rpdev)
|
static inline int rpmsg_chrdev_register_device(struct rpmsg_device *rpdev)
|
||||||
{
|
{
|
||||||
int ret;
|
return rpmsg_register_device_override(rpdev, "rpmsg_ctrl");
|
||||||
|
|
||||||
strcpy(rpdev->id.name, "rpmsg_chrdev");
|
|
||||||
ret = driver_set_override(&rpdev->dev, &rpdev->driver_override,
|
|
||||||
rpdev->id.name, strlen(rpdev->id.name));
|
|
||||||
if (ret)
|
|
||||||
return ret;
|
|
||||||
|
|
||||||
ret = rpmsg_register_device(rpdev);
|
|
||||||
if (ret)
|
|
||||||
kfree(rpdev->driver_override);
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -122,6 +122,8 @@ struct rpmsg_driver {
|
||||||
|
|
||||||
#if IS_ENABLED(CONFIG_RPMSG)
|
#if IS_ENABLED(CONFIG_RPMSG)
|
||||||
|
|
||||||
|
int rpmsg_register_device_override(struct rpmsg_device *rpdev,
|
||||||
|
const char *driver_override);
|
||||||
int register_rpmsg_device(struct rpmsg_device *dev);
|
int register_rpmsg_device(struct rpmsg_device *dev);
|
||||||
void unregister_rpmsg_device(struct rpmsg_device *dev);
|
void unregister_rpmsg_device(struct rpmsg_device *dev);
|
||||||
int __register_rpmsg_driver(struct rpmsg_driver *drv, struct module *owner);
|
int __register_rpmsg_driver(struct rpmsg_driver *drv, struct module *owner);
|
||||||
|
@ -149,6 +151,12 @@ int rpmsg_set_signals(struct rpmsg_endpoint *ept, u32 set, u32 clear);
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
|
static inline int rpmsg_register_device_override(struct rpmsg_device *rpdev,
|
||||||
|
const char *driver_override)
|
||||||
|
{
|
||||||
|
return -ENXIO;
|
||||||
|
}
|
||||||
|
|
||||||
static inline int register_rpmsg_device(struct rpmsg_device *dev)
|
static inline int register_rpmsg_device(struct rpmsg_device *dev)
|
||||||
{
|
{
|
||||||
return -ENXIO;
|
return -ENXIO;
|
||||||
|
|
Loading…
Reference in a new issue