From: Arun Iyengar Igor Paunovic posted the slot part in "accel/rocket: keep core slots stable across unbind and rebind" [1]. rocket_probe() takes slot num_cores, and rocket_remove() leaves the slot's .dev set. Rebinding any core but the last probed one overwrites a bound core's slot, and find_core_for_dev() then matches the stale slot. rocket_open() uses cores[0].dev, which may be unbound, and can put a core whose probe is still running in the file's entity: drm_sched_init() marks it ready before runtime PM is enabled. Two unbinds through sysfs can run at once and race on num_cores. Take the first free slot, clear it at removal, open on a bound core, and serialise probe, removal and open under a mutex. Link: https://lore.kernel.org/all/20260922080114.44662-5-royalnet026@gmail.com/ [1] Fixes: ed98261b4168 ("accel/rocket: Add a new driver for Rockchip's NPU") Cc: stable@vger.kernel.org Cc: Igor Paunovic Signed-off-by: Arun Iyengar --- drivers/accel/rocket/rocket_drv.c | 55 ++++++++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 13 deletions(-) diff --git a/drivers/accel/rocket/rocket_drv.c b/drivers/accel/rocket/rocket_drv.c index e45b3c22b31e..ccaedb004d19 100644 --- a/drivers/accel/rocket/rocket_drv.c +++ b/drivers/accel/rocket/rocket_drv.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -26,6 +27,8 @@ static struct platform_device *drm_dev; static struct rocket_device *rdev; static void *rdev_group; +/* Serialises core probe, removal and open */ +static DEFINE_MUTEX(rocket_cores_lock); static void rocket_vm_destroy(struct kref *kref) @@ -83,6 +86,7 @@ rocket_open(struct drm_device *dev, struct drm_file *file) { struct rocket_device *rdev = to_rocket_device(dev); struct rocket_file_priv *rocket_priv; + unsigned int core; int ret; if (!try_module_get(THIS_MODULE)) @@ -95,17 +99,28 @@ rocket_open(struct drm_device *dev, struct drm_file *file) } rocket_priv->rdev = rdev; - rocket_priv->vm = rocket_vm_create(rdev->cores[0].dev); - if (IS_ERR(rocket_priv->vm)) { - ret = PTR_ERR(rocket_priv->vm); - goto err_free; - } + rocket_priv->vm = ERR_PTR(-ENODEV); + + scoped_guard(mutex, &rocket_cores_lock) { + for (core = 0; core < rdev->max_cores; core++) { + if (rdev->cores[core].dev) { + rocket_priv->vm = rocket_vm_create(rdev->cores[core].dev); + break; + } + } - file->driver_priv = rocket_priv; + if (IS_ERR(rocket_priv->vm)) { + ret = PTR_ERR(rocket_priv->vm); + goto err_free; + } - ret = rocket_job_open(rocket_priv); - if (ret) - goto err_vm_put; + file->driver_priv = rocket_priv; + + /* Not on a core whose probe is still running */ + ret = rocket_job_open(rocket_priv); + if (ret) + goto err_vm_put; + } return 0; @@ -195,11 +210,14 @@ static int rocket_core_hw_index(struct device *dev) static int rocket_probe(struct platform_device *pdev) { int index = rocket_core_hw_index(&pdev->dev); + unsigned int core; int ret; if (index < 0) return index; + guard(mutex)(&rocket_cores_lock); + if (rdev == NULL) { /* First core probing, initialize DRM device. */ rdev_group = devres_open_group(&drm_dev->dev, NULL, GFP_KERNEL); @@ -217,18 +235,25 @@ static int rocket_probe(struct platform_device *pdev) devres_close_group(&drm_dev->dev, rdev_group); } - unsigned int core = rdev->num_cores; + /* Cores can unbind and rebind in any order */ + for (core = 0; core < rdev->max_cores; core++) + if (!rdev->cores[core].dev) + break; + if (WARN_ON(core == rdev->max_cores)) + return -EBUSY; dev_set_drvdata(&pdev->dev, rdev); rdev->cores[core].rdev = rdev; - rdev->cores[core].dev = &pdev->dev; + /* Runtime PM looks the core up without the lock */ + WRITE_ONCE(rdev->cores[core].dev, &pdev->dev); rdev->cores[core].index = index; rdev->num_cores++; ret = rocket_core_init(&rdev->cores[core]); if (ret) { + WRITE_ONCE(rdev->cores[core].dev, NULL); rdev->num_cores--; if (rdev->num_cores == 0) { @@ -246,12 +271,16 @@ static int find_core_for_dev(struct device *dev); static void rocket_remove(struct platform_device *pdev) { struct device *dev = &pdev->dev; - int core = find_core_for_dev(dev); + int core; + + guard(mutex)(&rocket_cores_lock); + core = find_core_for_dev(dev); if (core < 0) return; rocket_core_fini(&rdev->cores[core]); + WRITE_ONCE(rdev->cores[core].dev, NULL); rdev->num_cores--; if (rdev->num_cores == 0) { @@ -273,7 +302,7 @@ static int find_core_for_dev(struct device *dev) struct rocket_device *rdev = dev_get_drvdata(dev); for (unsigned int core = 0; core < rdev->max_cores; core++) { - if (dev == rdev->cores[core].dev) + if (dev == READ_ONCE(rdev->cores[core].dev)) return core; } -- 2.55.0