accel/ivpu: use kmem_cache for IPC rx_msg allocations
Karol Wachowski
maintainerPATCH 1/1 accel/ivpu: use kmem_cache for IPC rx_msg allocations
Use a dedicated kmem_cache for ivpu_ipc_rx_msg allocations instead of the generic kmalloc. This gives each CPU its own hot freelist for this object type, reducing allocator overhead in the IRQ handler and threaded IRQ path where rx_msg objects are allocated and freed at high frequency.
--- drivers/accel/ivpu/ivpu_ipc.c | 17 ++++++++++++++--- drivers/accel/ivpu/ivpu_ipc.h | 1 + 2 files changed, 15 insertions(+), 3 deletions(-)
drivers/accel/ivpu/ivpu_ipc.c +14 -3
@@ -146,7 +146,7 @@ ivpu_ipc_rx_msg_add(struct ivpu_device *vdev, struct ivpu_ipc_consumer *cons,lockdep_assert_held (&ipc->cons_lock); - rx_msg =kzalloc_obj (*rx_msg,GFP_ATOMIC ); + rx_msg =kmem_cache_zalloc (ipc->rx_msg_cache,GFP_ATOMIC );if (!rx_msg) {ivpu_ipc_rx_mark_free (vdev, ipc_hdr, jsm_msg);return ;
@@ -174,7 +174,7 @@ ivpu_ipc_rx_msg_del(struct ivpu_device *vdev, struct ivpu_ipc_rx_msg *rx_msg)list_del (&rx_msg->link);ivpu_ipc_rx_mark_free (vdev, rx_msg->ipc_hdr, rx_msg->jsm_msg);atomic_dec (&vdev->ipc->rx_msg_count); -kfree (rx_msg); +kmem_cache_free (vdev->ipc->rx_msg_cache, rx_msg); } void ivpu_ipc_consumer_add(struct ivpu_device *vdev, struct ivpu_ipc_consumer *cons,
@@ -490,10 +490,18 @@ int ivpu_ipc_init(struct ivpu_device *vdev)struct ivpu_ipc_info *ipc = vdev->ipc;int ret; + ipc->rx_msg_cache =kmem_cache_create ("ivpu_ipc_rx_msg" ,sizeof (struct ivpu_ipc_rx_msg ),0 , +SLAB_HWCACHE_ALIGN ,NULL ); +if (!ipc->rx_msg_cache) { +ivpu_err (vdev,"Failed to create rx_msg_cache\n" ); +return -ENOMEM ; + } + ipc->mem_tx =ivpu_bo_create_global (vdev,SZ_16K ,DRM_IVPU_BO_WC |DRM_IVPU_BO_MAPPABLE );if (!ipc->mem_tx) {ivpu_err (vdev,"Failed to allocate mem_tx\n" ); -return -ENOMEM ; + ret = -ENOMEM ; + goto err_destroy_cache; } ipc->mem_rx =ivpu_bo_create_global (vdev,SZ_16K ,DRM_IVPU_BO_WC |DRM_IVPU_BO_MAPPABLE );
@@ -532,6 +540,8 @@ int ivpu_ipc_init(struct ivpu_device *vdev)ivpu_bo_free (ipc->mem_rx); err_free_tx:ivpu_bo_free (ipc->mem_tx); +err_destroy_cache: +kmem_cache_destroy (ipc->rx_msg_cache);return ret; }
@@ -544,6 +554,7 @@ void ivpu_ipc_fini(struct ivpu_device *vdev)drm_WARN_ON (&vdev->drm,atomic_read (&ipc->rx_msg_count) >0 );ivpu_ipc_mem_fini (vdev); +kmem_cache_destroy (ipc->rx_msg_cache); }void ivpu_ipc_enable (struct ivpu_device *vdev)
drivers/accel/ivpu/ivpu_ipc.h +1 -0
@@ -70,6 +70,7 @@ struct ivpu_ipc_info {struct gen_pool *mm_tx;struct ivpu_bo *mem_tx;struct ivpu_bo *mem_rx; +struct kmem_cache *rx_msg_cache;atomic_t rx_msg_count;
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Duplicate kmem_cache name on multi-device systems --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Duplicate kmem_cache name on multi-device systems
--
commit 41c16409be6704d2ef5ab21270390fee65dcf7aa
Author: Karol Wachowski <[email protected]>
accel/ivpu: use kmem_cache for IPC rx_msg allocations
This commit introduces a dedicated kmem_cache for allocating IPC rx_msg objects to reduce allocator overhead in IRQ handlers.
Quoted 3 lines
drivers/accel/ivpu/ivpu_ipc.c +0
-0
[ ... ]
drivers/accel/ivpu/ivpu_ipc.c int ivpu_ipc_init(struct ivpu_device *vdev) · line 494
Quoted 6 lines
drivers/accel/ivpu/ivpu_ipc.c +2
-0@@ -490,10 +490,18 @@ int ivpu_ipc_init(struct ivpu_device *vdev)struct ivpu_ipc_info *ipc = vdev->ipc;int ret; + ipc->rx_msg_cache =kmem_cache_create ("ivpu_ipc_rx_msg" ,sizeof (struct ivpu_ipc_rx_msg ),0 , +SLAB_HWCACHE_ALIGN ,NULL );
[Severity: Medium]
Will this cause a name collision if there are multiple ivpu devices in the same system?
Since this is initialized per-device in ivpu_ipc_init(), calling kmem_cache_create() multiple times with the same hardcoded string "ivpu_ipc_rx_msg" can trigger a kernel WARN in kmem_cache_sanity_check() when CONFIG_DEBUG_VM is enabled.
Furthermore, if SLUB merging is disabled via boot parameters or config, the duplicate name causes a sysfs kobject collision, leading to a sysfs warning and a failure to register the cache in sysfs.
Should this cache name be made unique per device using dev_name(), or should the cache be a global singleton?
Quoted 12 lines
drivers/accel/ivpu/ivpu_ipc.c +7
-1+if (!ipc->rx_msg_cache) { +ivpu_err (vdev,"Failed to create rx_msg_cache\n" ); +return -ENOMEM ; + } + ipc->mem_tx =ivpu_bo_create_global (vdev,SZ_16K ,DRM_IVPU_BO_WC |DRM_IVPU_BO_MAPPABLE );if (!ipc->mem_tx) {ivpu_err (vdev,"Failed to allocate mem_tx\n" ); -return -ENOMEM ; + ret = -ENOMEM ; + goto err_destroy_cache; }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Wachowski, Karol
maintainerQuoted 22 lines · On 11-Jun-26 8:04, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Duplicatekmem_cachename on multi-device systems
--commit
41c16409be6704d2ef5ab21270390fee65dcf7aa
Author: Karol Wachowski <[email protected]>accel/ivpu: use
kmem_cachefor IPCrx_msgallocationsThis commit introduces a dedicated
kmem_cachefor allocating IPCrx_msgobjects to reduce allocator overhead in IRQ handlers.Quoted 3 lines
drivers/accel/ivpu/ivpu_ipc.c +0
-0[ ... ]
Quoted 4 lines
drivers/accel/ivpu/ivpu_ipc.c +0
-0@@ -490,10 +490,18 @@ int ivpu_ipc_init(struct ivpu_device *vdev)struct ivpu_ipc_info *ipc = vdev->ipc;int ret;
Quoted 6 lines
drivers/accel/ivpu/ivpu_ipc.c int ivpu_ipc_init(struct ivpu_device *vdev) · line 494
Quoted 2 lines
drivers/accel/ivpu/ivpu_ipc.c +2
-0+ ipc->rx_msg_cache =kmem_cache_create ("ivpu_ipc_rx_msg" ,sizeof (struct ivpu_ipc_rx_msg ),0 , +SLAB_HWCACHE_ALIGN ,NULL );[Severity: Medium]
Will this cause a name collision if there are multiple ivpu devices in the same system?
In practice, no:
1. There is a single NPU instance per system, so ivpu_ipc_init()
runs once and the cache is created once
2. Even with multiple instances it's safe, because the cache is
mergeable (only SLAB_HWCACHE_ALIGN flag). On the merge path the name is registered only as a sysfs alias via sysfs_slab_alias(). So the CONFIG_DEBUG_VM duplicate-name check (which scans slab_caches by name) shouldn't fire.
The WARN and sysfs collision require slab_nomerge and more than one
device which doesn't occur for this driver.
Quoted 26 lines
Since this is initialized per-device in
ivpu_ipc_init(), callingkmem_cache_create()multiple times with the same hardcoded string "ivpu_ipc_rx_msg" can trigger a kernel WARN inkmem_cache_sanity_check()whenCONFIG_DEBUG_VMis enabled.Furthermore, if SLUB merging is disabled via boot parameters or config, the duplicate name causes a sysfs kobject collision, leading to a sysfs warning and a failure to register the cache in sysfs.
Should this cache name be made unique per device using
dev_name(), or should the cache be a global singleton?Quoted 12 lines
drivers/accel/ivpu/ivpu_ipc.c +7
-1+if (!ipc->rx_msg_cache) { +ivpu_err (vdev,"Failed to create rx_msg_cache\n" ); +return -ENOMEM ; + } + ipc->mem_tx =ivpu_bo_create_global (vdev,SZ_16K ,DRM_IVPU_BO_WC |DRM_IVPU_BO_MAPPABLE );if (!ipc->mem_tx) {ivpu_err (vdev,"Failed to allocate mem_tx\n" ); -return -ENOMEM ; + ret = -ENOMEM ; + goto err_destroy_cache; }
Quoted 75 lines · Karol Wachowski
Use a dedicated
kmem_cacheforivpu_ipc_rx_msgallocations instead of the generic kmalloc. This gives each CPU its own hot freelist for this object type, reducing allocator overhead in the IRQ handler and threaded IRQ path whererx_msgobjects are allocated and freed at high frequency.Signed-off-by Karol Wachowski <[email protected]>--- drivers/accel/ivpu/ivpu_ipc.c | 17 ++++++++++++++--- drivers/accel/ivpu/ivpu_ipc.h | 1 + 2 files changed, 15 insertions(+), 3 deletions(-)drivers/accel/ivpu/ivpu_ipc.c +14
-3@@ -146,7 +146,7 @@ ivpu_ipc_rx_msg_add(struct ivpu_device *vdev, struct ivpu_ipc_consumer *cons,lockdep_assert_held (&ipc->cons_lock); - rx_msg =kzalloc_obj (*rx_msg,GFP_ATOMIC ); + rx_msg =kmem_cache_zalloc (ipc->rx_msg_cache,GFP_ATOMIC );if (!rx_msg) {ivpu_ipc_rx_mark_free (vdev, ipc_hdr, jsm_msg);return ;@@ -174,7 +174,7 @@ ivpu_ipc_rx_msg_del(struct ivpu_device *vdev, struct ivpu_ipc_rx_msg *rx_msg)list_del (&rx_msg->link);ivpu_ipc_rx_mark_free (vdev, rx_msg->ipc_hdr, rx_msg->jsm_msg);atomic_dec (&vdev->ipc->rx_msg_count); -kfree (rx_msg); +kmem_cache_free (vdev->ipc->rx_msg_cache, rx_msg); } void ivpu_ipc_consumer_add(struct ivpu_device *vdev, struct ivpu_ipc_consumer *cons,@@ -490,10 +490,18 @@ int ivpu_ipc_init(struct ivpu_device *vdev)struct ivpu_ipc_info *ipc = vdev->ipc;int ret; + ipc->rx_msg_cache =kmem_cache_create ("ivpu_ipc_rx_msg" ,sizeof (struct ivpu_ipc_rx_msg ),0 , +SLAB_HWCACHE_ALIGN ,NULL ); +if (!ipc->rx_msg_cache) { +ivpu_err (vdev,"Failed to create rx_msg_cache\n" ); +return -ENOMEM ; + } + ipc->mem_tx =ivpu_bo_create_global (vdev,SZ_16K ,DRM_IVPU_BO_WC |DRM_IVPU_BO_MAPPABLE );if (!ipc->mem_tx) {ivpu_err (vdev,"Failed to allocate mem_tx\n" ); -return -ENOMEM ; + ret = -ENOMEM ; + goto err_destroy_cache; } ipc->mem_rx =ivpu_bo_create_global (vdev,SZ_16K ,DRM_IVPU_BO_WC |DRM_IVPU_BO_MAPPABLE );@@ -532,6 +540,8 @@ int ivpu_ipc_init(struct ivpu_device *vdev)ivpu_bo_free (ipc->mem_rx); err_free_tx:ivpu_bo_free (ipc->mem_tx); +err_destroy_cache: +kmem_cache_destroy (ipc->rx_msg_cache);return ret; }@@ -544,6 +554,7 @@ void ivpu_ipc_fini(struct ivpu_device *vdev)drm_WARN_ON (&vdev->drm,atomic_read (&ipc->rx_msg_count) >0 );ivpu_ipc_mem_fini (vdev); +kmem_cache_destroy (ipc->rx_msg_cache); }void ivpu_ipc_enable (struct ivpu_device *vdev)drivers/accel/ivpu/ivpu_ipc.h +0
-0
drivers/accel/ivpu/ivpu_ipc.h struct ivpu_ipc_info { · line 76
Quoted 7 lines
drivers/accel/ivpu/ivpu_ipc.h +1
-0@@ -70,6 +70,7 @@ struct ivpu_ipc_info {struct gen_pool *mm_tx;struct ivpu_bo *mem_tx;struct ivpu_bo *mem_rx; +struct kmem_cache *rx_msg_cache;atomic_t rx_msg_count;
Quoted 69 lines · Andrzej Kacprowski
Quoted 68 lines · Karol Wachowski
Use a dedicated
kmem_cacheforivpu_ipc_rx_msgallocations instead of the generic kmalloc. This gives each CPU its own hot freelist for this object type, reducing allocator overhead in the IRQ handler and threaded IRQ path whererx_msgobjects are allocated and freed at high frequency.Signed-off-by Karol Wachowski <[email protected]>---
drivers/accel/ivpu/ivpu_ipc.c| 17 ++++++++++++++---drivers/accel/ivpu/ivpu_ipc.h| 1 +2 files changed, 15 insertions(+), 3 deletions(-)drivers/accel/ivpu/ivpu_ipc.c +11
-3@@ -146,7 +146,7 @@ ivpu_ipc_rx_msg_add(struct ivpu_device *vdev,struct ivpu_ipc_consumer *cons, lockdep_assert_held(&ipc->cons_lock); - rx_msg = kzalloc_obj(*rx_msg, GFP_ATOMIC); + rx_msg = kmem_cache_zalloc(ipc->rx_msg_cache, GFP_ATOMIC); if (!rx_msg) { ivpu_ipc_rx_mark_free(vdev, ipc_hdr, jsm_msg); return;@@ -174,7 +174,7 @@ ivpu_ipc_rx_msg_del(struct ivpu_device *vdev, struct ivpu_ipc_rx_msg *rx_msg) list_del(&rx_msg->link); ivpu_ipc_rx_mark_free(vdev, rx_msg->ipc_hdr, rx_msg->jsm_msg); atomic_dec(&vdev->ipc->rx_msg_count); - kfree(rx_msg); + kmem_cache_free(vdev->ipc->rx_msg_cache, rx_msg); } void ivpu_ipc_consumer_add(struct ivpu_device *vdev, struct ivpu_ipc_consumer *cons,@@ -490,10 +490,18 @@ int ivpu_ipc_init(struct ivpu_device *vdev) struct ivpu_ipc_info *ipc = vdev->ipc; int ret; + ipc->rx_msg_cache = kmem_cache_create("ivpu_ipc_rx_msg",sizeof (struct ivpu_ipc_rx_msg ),0 , + SLAB_HWCACHE_ALIGN, NULL); + if (!ipc->rx_msg_cache) { + ivpu_err(vdev, "Failed to create rx_msg_cache\n"); + return -ENOMEM; + } + ipc->mem_tx = ivpu_bo_create_global(vdev, SZ_16K, DRM_IVPU_BO_WC | DRM_IVPU_BO_MAPPABLE); if (!ipc->mem_tx) { ivpu_err(vdev, "Failed to allocate mem_tx\n"); - return -ENOMEM; + ret = -ENOMEM; + goto err_destroy_cache; } ipc->mem_rx = ivpu_bo_create_global(vdev, SZ_16K, DRM_IVPU_BO_WC | DRM_IVPU_BO_MAPPABLE);@@ -532,6 +540,8 @@ int ivpu_ipc_init(struct ivpu_device *vdev) ivpu_bo_free(ipc->mem_rx);
err_free_tx:
ivpu_bo_free(ipc->mem_tx);
+err_destroy_cache:
+kmem_cache_destroy(ipc->rx_msg_cache);
return ret;
}drivers/accel/ivpu/ivpu_ipc.c +1
-0@@ -544,6 +554,7 @@ void ivpu_ipc_fini(struct ivpu_device *vdev) drm_WARN_ON(&vdev->drm, atomic_read(&ipc->rx_msg_count) > 0); ivpu_ipc_mem_fini(vdev); + kmem_cache_destroy(ipc->rx_msg_cache); } void ivpu_ipc_enable(struct ivpu_device *vdev)drivers/accel/ivpu/ ivpu_ipc.h renamed from drivers/accel/ivpu/ivpu_ipc.h +0
-1--- a/drivers/accel/ivpu/ivpu_ipc.h
Quoted 8 lines
drivers/accel/ivpu/ ivpu_ipc.h struct ivpu_ipc_info { · line 74
Quoted 7 lines
+++
b/drivers/accel/ivpu/ivpu_ipc.hdrivers/accel/ivpu/ ivpu_ipc.h renamed from drivers/accel/ivpu/ivpu_ipc.h +1
-0@@ -70,6 +70,7 @@ struct ivpu_ipc_info { struct gen_pool *mm_tx; struct ivpu_bo *mem_tx; struct ivpu_bo *mem_rx; + struct kmem_cache *rx_msg_cache; atomic_t rx_msg_count;Reviewed-by Andrzej Kacprowski <[email protected]>
Pushed to drm-misc-next.