Skip to contentSkip to conversation

accel/rocket: search every core slot when a core is removed

PATCH Applied Igor Paunovic 5 participants 8 messages Sep 4 – Oct 13

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 accel/rocket: search every core slot when a core is removed

rocket_remove() decrements rdev->num_cores for each core it removes, while find_core_for_dev() searches slots 0 to num_cores - 1. Unbinding the cores in the order they were bound therefore loses the last one: by the time it is removed the search range has already shrunk past its slot, so find_core_for_dev() returns -1 and rocket_remove() gives up without doing anything.

num_cores never reaches zero, rocket_device_fini() never runs, and the file-scoped rdev keeps pointing at a device that is going away. Binding the cores again starts from that stale count, because rocket_probe() takes rdev->num_cores as the slot to fill. On an RK3588, which describes three cores, the second round lands on slots 1, 2 and 3 while rdev->cores was allocated with room for three:

rocket fdab0000.npu: Rockchip NPU core 1 version: 1179210309 rocket fdac0000.npu: Rockchip NPU core 2 version: 1179210309 rocket fdad0000.npu: Rockchip NPU core 3 version: 1179210309

The write to rdev->cores[3] is past the end of the array.

Nothing in tree reads the core array often enough to notice, so the overrun is silent today. It turned up while testing a devfreq series on top of this, where a worker walks every core a few times a second, and UBSAN caught the first bool it read out of the overrun entry:

  UBSAN: invalid-load in drivers/accel/rocket/rocket_devfreq.c:47:10
  load of value 5 is not a valid value for type '_Bool'
  Workqueue: devfreq_wq devfreq_monitor

Record how many slots were allocated and search all of them. Every core is then found on removal, num_cores reaches zero, the device is torn down and a later bind starts from a clean rdev.

This does not make unbinding a single core out of several work. probe still takes num_cores as the slot to fill, so rebinding one core while its siblings stay bound would write over a slot that is already in use, and rocket_open() still reaches for cores[0] whether or not anything is there. Both of those want more thought than a fix should carry.

Found by unbinding and rebinding all three cores on an Orange Pi 5 Plus. With this applied, 25 unbind/rebind rounds and 5 module unload/reload rounds run clean there: the cores land in slots 0, 1 and 2 every time, whichever order they are bound in, and the shared supply goes back to a single user after each round.

Fixes ed98261b4168 ("accel/rocket: Add a new driver for Rockchip's NPU")
Assisted-by LLM sparse checkpatch
---
 drivers/accel/rocket/rocket_device.c | 2 ++
 drivers/accel/rocket/rocket_device.h | 1 +
 drivers/accel/rocket/rocket_drv.c    | 2 +-
 3 files changed, 4 insertions(+), 1 deletion(-)
drivers/accel/rocket/rocket_device.c +2 -0
index 46e6ee1e72c5f..efd004194c1af 100644
@@ -31,6 +31,8 @@ struct rocket_device *rocket_device_init(struct platform_device *pdev,
 		if (of_device_is_available(core_node))
 			num_cores++;
 
+	rdev->max_cores = num_cores;
+
 	rdev->cores = devm_kcalloc(dev, num_cores, sizeof(*rdev->cores), GFP_KERNEL);
 	if (!rdev->cores)
 		return ERR_PTR(-ENOMEM);
drivers/accel/rocket/rocket_device.h +1 -0
index ce662abc01d3d..c62d567010696 100644
@@ -19,6 +19,7 @@ struct rocket_device {
 
 	struct rocket_core *cores;
 	unsigned int num_cores;
+	unsigned int max_cores;
 };
 
 struct rocket_device *rocket_device_init(struct platform_device *pdev,
drivers/accel/rocket/rocket_drv.c +1 -1
index 8bbbce594883e..2bcfe4ab3c68f 100644
@@ -223,7 +223,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->num_cores; core++) {
+	for (unsigned int core = 0; core < rdev->max_cores; core++) {
 		if (dev == rdev->cores[core].dev)
 			return core;
 	}
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 8 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 8 potential issue(s) to consider:

Pre-existing issues:
- [High] Out-of-order core unbinding and subsequent rebinding corrupts the rdev->cores array, leading to heap out-of-bounds writes, memory corruption, and active cores being overwritten or ignored.
- [High] Repeated unbinding and rebinding of all cores permanently leaks the global struct rocket_device and its associated devm-managed resources.
- [High] Concurrent probe and remove operations cause data races on the unprotected global rdev pointer and num_cores counter.
- [High] Core unbinding destroys the DRM scheduler while existing open file descriptors still hold references, leading to a Use-After-Free upon job submission.
- [High] rocket_open unconditionally accesses cores[0] for IOMMU domain creation, leading to a crash if core 0 is unbound.
- [High] Global static rdev retains an ERR_PTR if rocket_device_init fails, causing a crash on subsequent probes.
- [High] Missing NULL check for kmalloc_objs in rocket_job_open causes a NULL pointer dereference on allocation failure.
- [High] The DRM device is prematurely exposed to userspace before its backing cores are initialized, leading to a NULL pointer dereference on open.
--

commit d884827ebadaac3af5198fed539a2afc027a3c79
Author: Igor Paunovic <[email protected]>

accel/rocket: search every core slot when a core is removed

rocket_remove() decrements rdev->num_cores for each core it removes, while find_core_for_dev() searches slots 0 to num_cores - 1. Record how many slots were allocated and search all of them so every core is found on removal.

Quoted 4 lines

num_cores never reaches zero, rocket_device_fini() never runs, and the file-scoped rdev keeps pointing at a device that is going away. Binding the cores again starts from that stale count, because rocket_probe() takes rdev->num_cores as the slot to fill.

[Severity: High]
This is a pre-existing issue, but is the file-scoped rdev pointer and its num_cores counter protected against concurrent access?

If multiple core devices are probed or unbound concurrently via sysfs, could threads race on if (rdev == NULL) in rocket_probe(), potentially double- initializing the device? Could they also race on rdev->num_cores++ or rdev->num_cores--, corrupting the core array indexing?

Quoted 3 lines

Record how many slots were allocated and search all of them. Every core is then found on removal, num_cores reaches zero, the device is torn down and a later bind starts from a clean rdev.

[Severity: High]
This is a pre-existing issue, but does unbinding all cores permanently leak the global struct rocket_device and its associated devm-managed resources?

When all cores are removed, rocket_device_fini() is called, but it only unregisters the DRM device. The allocations made with devm_drm_dev_alloc() and devm_kcalloc() are tied to the global drm_dev platform device, which is never destroyed during unbind. Since rdev is set to NULL, would a later bind allocate a completely new device and permanently leak the previous allocations?

[Severity: High]
This is also a pre-existing issue, but does tearing down the device and its cores destroy the DRM scheduler while existing open file descriptors still hold references to it?

When a core is removed, rocket_core_fini() calls rocket_job_fini(), which executes drm_sched_fini(). If a user still holds an open DRM file descriptor and submits a job via ioctl, would drm_sched_entity_push_job() operate on the already destroyed scheduler and trigger a Use-After-Free?

Quoted 5 lines

This does not make unbinding a single core out of several work. probe still takes num_cores as the slot to fill, so rebinding one core while its siblings stay bound would write over a slot that is already in use, and rocket_open() still reaches for cores[0] whether or not anything is there. Both of those want more thought than a fix should carry.

[Severity: High]
This is a pre-existing issue, but does leaving this unfixed allow memory corruption? Because rocket_remove() doesn't clear the dev pointer or compact the array, wouldn't subsequent out-of-order unbinds match stale pointers since find_core_for_dev() now searches up to max_cores?

Also, because rocket_job_open() and sched_to_core() iterate strictly up to num_cores, would they miss active cores that have been shifted past num_cores - 1 and incorrectly read finalized schedulers from the torn down slots?

[Severity: High]
Regarding rocket_open(), this is a pre-existing issue, but does leaving it reaching for cores[0] allow an unprivileged user to trigger a kernel panic?

If a root user unbinds core 0, would a subsequent open of the DRM device node by any user crash when it unconditionally calls rocket_iommu_domain_create(rdev->cores[0].dev)?

drivers/accel/rocket/rocket_device.c struct rocket_device *rocket_device_init(struct platform_device *pdev, · line 38

Quoted 13 lines
drivers/accel/rocket/rocket_device.c +2 -0
index 46e6ee1e72c5f..efd004194c1af 100644
@@ -31,6 +31,8 @@ struct rocket_device *rocket_device_init(struct platform_device *pdev,
 		if (of_device_is_available(core_node))
 			num_cores++;
 
+	rdev->max_cores = num_cores;
+
 	rdev->cores = devm_kcalloc(dev, num_cores, sizeof(*rdev->cores), GFP_KERNEL);
 	if (!rdev->cores)
 		return ERR_PTR(-ENOMEM);

[Severity: High]
This is a pre-existing issue, but if rocket_device_init() fails here (or elsewhere) and returns an ERR_PTR, doesn't the global rdev pointer in rocket_probe() retain that ERR_PTR?

Since rocket_probe() only checks if (rdev == NULL) and does not reset it on failure, would a subsequent bind attempt blindly dereference the ERR_PTR when it accesses rdev->num_cores?

[Severity: High]
This is another pre-existing issue, but is the DRM device prematurely exposed to userspace during this initialization?

rocket_device_init() registers the DRM device via drm_dev_register(), making it immediately visible to userspace. However, the first core's device pointer (rdev->cores[core].dev) is only assigned in rocket_probe() after rocket_device_init() returns. If an unprivileged user opens the device during this window, would rocket_open() encounter a NULL pointer dereference when it accesses cores[0].dev?

[Severity: High]
Finally, this is a pre-existing issue, but does rocket_job_open() correctly handle memory allocation failures when creating the scheds array for these cores?

It allocates the array using kmalloc_objs(*scheds, rdev->num_cores) but fails to check if the returned pointer is NULL before executing scheds[core] = &rdev->cores[core].sched;. Would this lead to a NULL pointer dereference on allocation failure?

Quoted 13 lines
drivers/accel/rocket/rocket_drv.c +1 -1
index 8bbbce594883e..2bcfe4ab3c68f 100644
@@ -223,7 +223,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->num_cores; core++) {
+	for (unsigned int core = 0; core < rdev->max_cores; core++) {
 		if (dev == rdev->cores[core].dev)
 			return core;
 	}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Igor Paunovic

Answering the one question that is about this patch rather than about the state it leaves behind, and confirming the rest.

Quoted 3 lines

Because rocket_remove() doesn't clear the dev pointer or compact the array, wouldn't subsequent out-of-order unbinds match stale pointers since find_core_for_dev() now searches up to max_cores?

There are three callers: rocket_remove() and the two runtime PM callbacks. The driver core calls remove once per device, and the PM callbacks cannot run for an unbound one, because rocket_core_fini() has already called pm_runtime_disable() on it. The slots the widened search adds are either never filled, where .dev is NULL and matches nothing, or held by a device that has been unbound - and nothing asks after such a device again.

What the narrower search did do was lose live cores. Unbind the core in slot 0 of three: num_cores drops to two, so find_core_for_dev() stops before slot 2. The core sitting there is still bound and still running, but its own runtime suspend and resume callbacks start returning -ENODEV. Searching every allocated slot fixes that as well.

An earlier version of this patch did clear .dev on removal and take a free slot on probe. I dropped both. Clearing .dev turns rocket_open()'s unconditional cores[0] into a NULL dereference whenever the core in slot 0 is unbound while its siblings stay bound, which is a worse failure than the one it cures - and it is the same rocket_open() issue listed further down. That is why the commit message says out-of-order unbind wants more thought than a fix should carry, rather than quietly half-fixing it.

On the rest: all eight are pre-existing and I agree with all eight. Two of them have names already.

The ERR_PTR left in the file-scoped rdev is fixed by Guangshuo Li's "accel/rocket: clear rdev on device init failure", posted in July and still unapplied:

https://lore.kernel.org/dri-devel/[email protected]/

It carries my Reviewed-by. It would be good to see that one land.

The devm point may explain something I measured this week and could not account for. Unbinding and rebinding all three cores walks the DRM minor upwards - 1 through 10 over ten rounds in one run - and only a module reload puts it back to 0. I have not shown that the allocations are leaked, only that something survives a rebind that should not, which is consistent with what you describe.

Igor

Tested-by on v1 1/1
#5 lore raw
In reply to Igor Paunovic
Quoted 73 lines · Igor Paunovic

rocket_remove() decrements rdev->num_cores for each core it removes, while find_core_for_dev() searches slots 0 to num_cores - 1. Unbinding the cores in the order they were bound therefore loses the last one: by the time it is removed the search range has already shrunk past its slot, so find_core_for_dev() returns -1 and rocket_remove() gives up without doing anything.

num_cores never reaches zero, rocket_device_fini() never runs, and the file-scoped rdev keeps pointing at a device that is going away. Binding the cores again starts from that stale count, because rocket_probe() takes rdev->num_cores as the slot to fill. On an RK3588, which describes three cores, the second round lands on slots 1, 2 and 3 while rdev->cores was allocated with room for three:

rocket fdab0000.npu: Rockchip NPU core 1 version: 1179210309 rocket fdac0000.npu: Rockchip NPU core 2 version: 1179210309 rocket fdad0000.npu: Rockchip NPU core 3 version: 1179210309

The write to rdev->cores[3] is past the end of the array.

Nothing in tree reads the core array often enough to notice, so the overrun is silent today. It turned up while testing a devfreq series on top of this, where a worker walks every core a few times a second, and UBSAN caught the first bool it read out of the overrun entry:

  UBSAN: invalid-load in drivers/accel/rocket/rocket_devfreq.c:47:10
  load of value 5 is not a valid value for type '_Bool'
  Workqueue: devfreq_wq devfreq_monitor

Record how many slots were allocated and search all of them. Every core is then found on removal, num_cores reaches zero, the device is torn down and a later bind starts from a clean rdev.

This does not make unbinding a single core out of several work. probe still takes num_cores as the slot to fill, so rebinding one core while its siblings stay bound would write over a slot that is already in use, and rocket_open() still reaches for cores[0] whether or not anything is there. Both of those want more thought than a fix should carry.

Found by unbinding and rebinding all three cores on an Orange Pi 5 Plus. With this applied, 25 unbind/rebind rounds and 5 module unload/reload rounds run clean there: the cores land in slots 0, 1 and 2 every time, whichever order they are bound in, and the shared supply goes back to a single user after each round.

Fixes ed98261b4168 ("accel/rocket: Add a new driver for Rockchip's NPU")
Assisted-by LLM sparse checkpatch
---
 drivers/accel/rocket/rocket_device.c | 2 ++
 drivers/accel/rocket/rocket_device.h | 1 +
 drivers/accel/rocket/rocket_drv.c    | 2 +-
 3 files changed, 4 insertions(+), 1 deletion(-)
drivers/accel/rocket/rocket_device.c +2 -0
index 46e6ee1e72c5f..efd004194c1af 100644
@@ -31,6 +31,8 @@ struct rocket_device *rocket_device_init(struct platform_device *pdev,
 		if (of_device_is_available(core_node))
 			num_cores++;
 
+	rdev->max_cores = num_cores;
+
 	rdev->cores = devm_kcalloc(dev, num_cores, sizeof(*rdev->cores), GFP_KERNEL);
 	if (!rdev->cores)
 		return ERR_PTR(-ENOMEM);
drivers/accel/rocket/rocket_device.h +0 -0
index ce662abc01d3d..c62d567010696 100644

drivers/accel/rocket/rocket_device.h struct rocket_device { · line 22

Quoted 4 lines
drivers/accel/rocket/rocket_device.h +1 -0
@@ -19,6 +19,7 @@ struct rocket_device {
 
 	struct rocket_core *cores;
 	unsigned int num_cores;
+	unsigned int max_cores;

Hi Igor,

I've tested this patch in Radxa Rock 5 B+ and it works. The test is that unbinding all cores in sequence 0,1,2 and rebind all. It seems that there is other issue about num_core. For example, sched_to_core() finds core for sched with num_core and it could make same error like find_core_for_dev().

Thanks,
Sidong

Quoted 19 lines
drivers/accel/rocket/rocket_device.h +0 -0
 };
 
 struct rocket_device *rocket_device_init(struct platform_device *pdev,
drivers/accel/rocket/rocket_drv.c +1 -1
index 8bbbce594883e..2bcfe4ab3c68f 100644
@@ -223,7 +223,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->num_cores; core++) {
+	for (unsigned int core = 0; core < rdev->max_cores; core++) {
 		if (dev == rdev->cores[core].dev)
 			return core;
 	}
Signature · 1 line
2.43.0
#6 lore raw

Hi Sidong,

Quoted 1 line

I've tested this patch in Radxa Rock 5 B+ and it works.

Thank you. That is the first test of it on an RK3588 that is not mine, and b4 collects your tag with the comment attached.

Quoted 3 lines

It seems that there is other issue about num_core. For example, sched_to_core() finds core for sched with num_core and it could make same error like find_core_for_dev().

You were right, and it is worse than a failed lookup: neither caller checks what sched_to_core() returns.

I built a KASAN kernel and unbound the middle of the three cores while three clients were submitting to all of them. It faults twice, once from the surviving core's job queue and once from its reset work:

  KASAN: null-ptr-deref in range [0x0000000000000220-0x0000000000000227]
  Workqueue: fdad0000.npu drm_sched_run_job_work [gpu_sched]
  pc : rocket_job_run+0x234/0x838 [rocket]

  KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007]
  Workqueue: rocket-reset-2 drm_sched_job_timedout [gpu_sched]
  pc : rocket_job_timedout+0xf0/0x1e0 [rocket]

Both are the third core. The workqueue names are its device and its core->index, and it had been left at slot 2 while num_cores was down to 2.

The fix is one word, and it carries your Reported-by:

https://lore.kernel.org/dri-devel/[email protected]/

Same test on a kernel with it applied: no faults, journal clean. It applies on top of the patch you tested, since max_cores comes from that one.

What it does not fix, and the patch says so: an open client keeps an entity pointing at the scheduler of the core that went away. drm_sched then logs "not ready, skipping" for every job that lands on it - 25006 of them in my run - and the client waits in dma_fence_default_wait for a fence that will never signal. The board stays up and the client hangs. Making one core of several safe to unbind while a client is open needs more than a fix, and I did not want to hide that behind a patch that only stops the oops.

Thanks for reading it closely enough to spot the second one.

Igor

#4 lore raw
In reply to Jiaxing Hu

Hi Jiaxing,

Quoted 3 lines

Moved here from the DVFS thread so that b4 can collect it.

Thank you, and it works: b4 picks it up with the comment attached and the DKIM check passes. Two cores on a different SoC is worth more to this patch than anything I can produce on my own board.

Your test also found the edge of something else, and I would rather tell you before you spend more rounds on it.

You wrote that the cores came back "numbered 0 and 1 both times". That is the case that works. I rebound the three cores here in all six orders and only the devicetree order ran clean:

  bind order (slot 0 first)   NPU job timed out         inference
  fdab fdac fdad (devicetree) none                      129.4 inf/s, oracle ok
  fdab fdad fdac              27 fdad                   1.95 inf/s, oracle ok
  fdac fdab fdad              135 fdac, 5 fdab, 1 fdad  oracle rejected output
  fdac fdad fdab              135 fdac, 5 fdad          oracle rejected output
  fdad fdab fdac              136 fdad, 5 fdab          oracle rejected output
  fdad fdac fdab              135 fdad, 1 fdab          oracle rejected output

Counted from the journal since each bind, one six-second inference per row. The single fdad timeout in row three is on a correctly numbered core and I cannot account for it; the most likely explanation is a job still draining from the previous row, since the count starts before the unbind. Everything else lands on a core whose slot is not its hardware number, in proportion to the tasks the scheduler gave it, and in both directions of the mismatch: 5 timeouts on the core in slot 1 whether its hardware number is above it (row four) or below it (row five).

The cause is in rocket_job_hw_submit():

  extra_bit = 0x10000000 * core->index;

core->index is the slot the core takes in rdev->cores[], which is bind order. The vendor driver computes that same bit from the hardware number of the core (rknpu_job.c: REG_WRITE((0xe + 0x10000000 * i), ...) with i indexing rknpu_dev->base[]). On a normal boot the two agree and nothing shows. Bind out of that order and every task submitted to a core whose slot is not its hardware number times out after 500 ms, the reset does not help, and the inference finishes with wrong output. No error, no warning, no regulator or clock message - the rail sat at 700000 uV in the runs that passed and in the runs that failed. Only the bit-exact oracle and the UART log showed it.

The second row is the one I would have missed. The oracle passed there, because the tensors it checks are computed by the core in slot 0, which happened to be correct. Throughput was 66 times lower and 27 jobs had timed out. A single assertion does not catch this.

Two cores make it cheaper to test than three. If you bind the second core before the first on your ROCK 4D, on your current kernel, I expect the jobs that land on slot 0 to time out and your nine models to stop decoding identically. If they do, that is the bug reproduced on a second SoC by someone who is not me, which is worth more than my six rows. If they do not, I have the cause wrong and I would like to know that - please say so on the patch thread rather than here, so it lands where b4 will pick it up.

The fix is on the list now:

https://lore.kernel.org/dri-devel/[email protected]/

It numbers the cores by their position among the core nodes in the devicetree. It resolves that through dev->driver->of_match_table rather than a compatible string, so it works both before and after your 12/14, which moves that enumeration to for_each_matching_node() and renames the table. All six orders pass afterwards, 124 to 136 inf/s, zero timeouts. It is a separate patch with Fixes: and Cc: stable, and it goes out before v2 of the DVFS series.

One more thing your report is useful for: it is the same class as what you found on RK3576 last week. Wrong result, no complaint from the hardware. I am starting to think this driver needs a bit-exact check in whatever test people run against it, because throughput alone will happily report a healthy number while the output is garbage.

Igor

maintainer Applied
#7 lore raw
In reply to Igor Paunovic
Quoted 8 lines · Igor Paunovic

rocket_remove() decrements rdev->num_cores for each core it removes, while find_core_for_dev() searches slots 0 to num_cores - 1. Unbinding the cores in the order they were bound therefore loses the last one: by the time it is removed the search range has already shrunk past its slot, so find_core_for_dev() returns -1 and rocket_remove() gives up without doing anything.

[...]

Applied, thanks!

[1/1] accel/rocket: search every core slot when a core is removed

      commit: 6eff1d69dae808a1518aee5a58d0853a8db3f1be

Best regards,

Signature · 1 line
Heiko Stuebner <[email protected]>