Skip to contentSkip to conversation

kunit: Setup DMA masks on the kunit device

PATCH In review Maxime Ripard 3 participants 3 messages Feb 21, 2024 – Feb 27, 2024

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

PATCH 1/1 kunit: Setup DMA masks on the kunit device

Commit d393acce7b3f ("drm/tests: Switch to kunit devices") switched the DRM device creation helpers from an ad-hoc implementation to the new kunit device creation helpers introduced in commit d03c720e03bd ("kunit:
Add APIs for managing devices").

However, while the DRM helpers were using a platform_device, the kunit helpers are using a dedicated bus and device type.

That situation creates small differences in the initialisation, and one of them is that the kunit devices do not have the DMA masks setup. In turn, this means that we can't do any kind of DMA buffer allocation anymore, which creates a regression on some (downstream for now) tests.

Let's set up a default DMA mask that should work on any platform to fix it.

Fixes d03c720e03bd ("kunit: Add APIs for managing devices")
---
 lib/kunit/device.c | 4 ++++
 1 file changed, 4 insertions(+)
lib/kunit/device.c +4 -0
index 644a38a1f5b1..9ea399049749 100644
@@ -10,6 +10,7 @@
  */
 
 #include <linux/device.h>
+#include <linux/dma-mapping.h>
 
 #include <kunit/test.h>
 #include <kunit/device.h>
@@ -133,6 +134,9 @@ static struct kunit_device *kunit_device_register_internal(struct kunit *test,
 		return ERR_PTR(err);
 	}
 
+	kunit_dev->dev.dma_mask = &kunit_dev->dev.coherent_dma_mask;
+	kunit_dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
+
 	kunit_add_action(test, device_unregister_wrapper, &kunit_dev->dev);
 
 	return kunit_dev;
Signature · 1 line
2.43.2
Tested-by on v1 1/1
#1 lore raw
Quoted 5 lines · Maxime Ripard

Commit d393acce7b3f ("drm/tests: Switch to kunit devices") switched the DRM device creation helpers from an ad-hoc implementation to the new kunit device creation helpers introduced in commit d03c720e03bd ("kunit:
Add APIs for managing devices").

Quoted 8 lines

However, while the DRM helpers were using a platform_device, the kunit helpers are using a dedicated bus and device type.

That situation creates small differences in the initialisation, and one of them is that the kunit devices do not have the DMA masks setup. In turn, this means that we can't do any kind of DMA buffer allocation anymore, which creates a regression on some (downstream for now) tests.

This is wrong. The mainline kernel crashes for me without this patch.

With v6.8-rc6:

[    6.401720]         ok 3 Above the allocation limit
[    6.407679] ------------[ cut here ]------------
[    6.407927] WARNING: CPU: 1 PID: 1592 at kernel/dma/mapping.c:503 dma_alloc_attrs+0x36/0x60
...
[    6.413161]     # ttm_pool_alloc_basic: ASSERTION FAILED at drivers/gpu/drm/ttm/tests/ttm_pool_test.c:162
[    6.413161]     Expected err == 0, but
[    6.413161]         err == -12 (0xfffffffffffffff4)
[    6.416872]         not ok 4 One page, with coherent DMA mappings enabled
[    6.420623] ------------[ cut here ]------------
[    6.420900] list_add corruption. prev->next should be next (ffffffff86d3e930), but was 6b6b6b6b6b6b6b6b. (prev=ffff8f3143621e20).

and downhill from there. Granted, this is due to bad cleanup in the ttm unit tests after a failure, but the offending code causing that failure is already in the mainline kernel.

Quoted 5 lines

Let's set up a default DMA mask that should work on any platform to fix it.

Fixes d03c720e03bd ("kunit: Add APIs for managing devices")

Guenter

Reviewed-by on v1 1/1
#2 lore raw
In reply to Maxime Ripard
Quoted 14 lines · Maxime Ripard

Commit d393acce7b3f ("drm/tests: Switch to kunit devices") switched the DRM device creation helpers from an ad-hoc implementation to the new kunit device creation helpers introduced in commit d03c720e03bd ("kunit:
Add APIs for managing devices").

However, while the DRM helpers were using a platform_device, the kunit helpers are using a dedicated bus and device type.

That situation creates small differences in the initialisation, and one of them is that the kunit devices do not have the DMA masks setup. In turn, this means that we can't do any kind of DMA buffer allocation anymore, which creates a regression on some (downstream for now) tests.

Quoted 6 lines

Let's set up a default DMA mask that should work on any platform to fix it.

Fixes d03c720e03bd ("kunit: Add APIs for managing devices")

---

Thanks for this.

As Guenter notes, this fixes the ttm tests, which are otherwise completely broken:
./tools/testing/kunit/kunit.py run --arch x86_64 --kunitconfig drivers/gpu/drm/ttm/tests/

While I suspect there's probably a discussion to have around what things KUnit devices should set up (and how much flexibility users should have there), I think this is definitely a fix worth having in the meantime.

Cheers,
-- David

Quoted 28 lines
 lib/kunit/device.c | 4 ++++
 1 file changed, 4 insertions(+)
lib/kunit/device.c +4 -1
index 644a38a1f5b1..9ea399049749 100644
@@ -10,6 +10,7 @@
  */
 
 #include <linux/device.h>
+#include <linux/dma-mapping.h>
 
 #include <kunit/test.h>
 #include <kunit/device.h>
@@ -133,6 +134,9 @@ static struct kunit_device *kunit_device_register_internal(struct kunit *test,
                return ERR_PTR(err);
        }
 
+       kunit_dev->dev.dma_mask = &kunit_dev->dev.coherent_dma_mask;
+       kunit_dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
+
        kunit_add_action(test, device_unregister_wrapper, &kunit_dev->dev);
 
        return kunit_dev;
--

2.43.2