* [PATCH] dma: swiotlb: Allocate the restricted DMA pool slot array with kvzalloc()
@ 2026-11-20 13:11 ` yzddMr6 via B4 Relay
2026-11-20 14:18 ` Marek Szyprowski
0 siblings, 1 reply; 2+ messages in thread
From: yzddMr6 via B4 Relay @ 2026-11-20 13:11 UTC (permalink / raw)
To: Marek Szyprowski; +Cc: Robin Murphy, iommu, linux-kernel, stable, yzddMr6
From: yzddMr6 <yzddmr6@gmail.com>
rmem_swiotlb_device_init() allocates one struct io_tlb_slot per 2 KiB
slot of a restricted DMA pool with kzalloc(). The struct is 24 bytes on
64-bit, so with 4 KiB pages and the default MAX_PAGE_ORDER of 10 the
array is larger than KMALLOC_MAX_SIZE (4 MiB) for any pool above about
341 MiB. For such a pool kzalloc() refuses the request with a
WARN_ON_ONCE() in ___kmalloc_large_node(), the pool is not set up, and
of_dma_set_restricted_buffer() warns that it failed to initialise the
pool. The devices assigned to it then lose the restriction without
their probe failing, and use the default DMA path.
The slot array is only accessed by the CPU and does not need to be
physically contiguous. Allocate it with kvzalloc_objs() and free it with
kvfree(). The other pools are not affected: the slot array of the
default pool comes from memblock, and the buffers of late and dynamic
pools are limited to MAX_PAGE_ORDER, which keeps their slot arrays
small.
Pools of this size are needed when large buffers go through the pool
for their whole lifetime: this was hit with an arm64 Gunyah protected
VM whose virtio-gpu resources stay mapped in the pool, and a desktop at
3392x2400 needed more than 341 MiB.
The problem was analysed and this change written with the help of an
AI coding assistant.
Fixes: 0b84e4f8b793 ("swiotlb: Add restricted DMA pool initialization")
Cc: stable@vger.kernel.org
Signed-off-by: yzddMr6 <yzddmr6@gmail.com>
---
Reproduced in QEMU on the base commit below (arm64 defconfig, 4 KiB
pages): virt machine with 2 GiB, a 512 MiB restricted-dma-pool added to
the DT and referenced by every virtio_mmio node, one virtio-rng device
with iommu_platform=on (modern virtio-mmio, so VIRTIO_F_ACCESS_PLATFORM
is negotiated), and swiotlb=track_hiwater. Without this patch the WARN in
___kmalloc_large_node() fires, every transport reports "failed to
initialise "restricted-dma-pool" memory node", no debugfs directory is
created for the pool, and reading /dev/hwrng works without any bounce
(io_tlb_used_hiwater of the default pool stays 0). With it there is no
warning, every transport is assigned the pool, and its debugfs
directory shows 262144 slots, with io_tlb_used_hiwater already at 17
once virtio-rng has probed. W=1 shows no new warnings in kernel/dma/.
The change is also used in the guest kernel of the Gunyah VMs
mentioned above.
I used an AI coding assistant for this work. It analysed the failure,
wrote this patch and changelog and set up the QEMU reproducer. I have
reviewed the change.
---
kernel/dma/swiotlb.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c
index 786954bc5e6a..51c7c064b62f 100644
--- a/kernel/dma/swiotlb.c
+++ b/kernel/dma/swiotlb.c
@@ -2042,7 +2042,7 @@ static int rmem_swiotlb_device_init(struct reserved_mem *rmem,
return -ENOMEM;
pool = &mem->defpool;
- pool->slots = kzalloc_objs(*pool->slots, nslabs);
+ pool->slots = kvzalloc_objs(*pool->slots, nslabs);
if (!pool->slots) {
kfree(mem);
return -ENOMEM;
@@ -2050,7 +2050,7 @@ static int rmem_swiotlb_device_init(struct reserved_mem *rmem,
pool->areas = kzalloc_objs(*pool->areas, nareas);
if (!pool->areas) {
- kfree(pool->slots);
+ kvfree(pool->slots);
kfree(mem);
return -ENOMEM;
}
@@ -2067,7 +2067,7 @@ static int rmem_swiotlb_device_init(struct reserved_mem *rmem,
if (ret) {
dev_err(dev, "Failed to decrypt restricted DMA pool\n");
kfree(pool->areas);
- kfree(pool->slots);
+ kvfree(pool->slots);
kfree(mem);
return ret;
}
---
base-commit: 14866e632fa48a0b4c385ab0121526575eb8f88c
change-id: 20261120-upstream2-swiotlb-rmem-kvzalloc-f6cb6cfe09a3
Best regards,
--
yzddMr6 <yzddmr6@gmail.com>
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] dma: swiotlb: Allocate the restricted DMA pool slot array with kvzalloc()
2026-11-20 13:11 ` [PATCH] dma: swiotlb: Allocate the restricted DMA pool slot array with kvzalloc() yzddMr6 via B4 Relay
@ 2026-11-20 14:18 ` Marek Szyprowski
0 siblings, 0 replies; 2+ messages in thread
From: Marek Szyprowski @ 2026-11-20 14:18 UTC (permalink / raw)
To: yzddmr6; +Cc: Robin Murphy, iommu, linux-kernel
On 20.11.2026 14:11, yzddMr6 via B4 Relay wrote:
> From: yzddMr6 <yzddmr6@gmail.com>
>
> rmem_swiotlb_device_init() allocates one struct io_tlb_slot per 2 KiB
> slot of a restricted DMA pool with kzalloc(). The struct is 24 bytes on
> 64-bit, so with 4 KiB pages and the default MAX_PAGE_ORDER of 10 the
> array is larger than KMALLOC_MAX_SIZE (4 MiB) for any pool above about
> 341 MiB. For such a pool kzalloc() refuses the request with a
> WARN_ON_ONCE() in ___kmalloc_large_node(), the pool is not set up, and
> of_dma_set_restricted_buffer() warns that it failed to initialise the
> pool. The devices assigned to it then lose the restriction without
> their probe failing, and use the default DMA path.
>
> The slot array is only accessed by the CPU and does not need to be
> physically contiguous. Allocate it with kvzalloc_objs() and free it with
> kvfree(). The other pools are not affected: the slot array of the
> default pool comes from memblock, and the buffers of late and dynamic
> pools are limited to MAX_PAGE_ORDER, which keeps their slot arrays
> small.
>
> Pools of this size are needed when large buffers go through the pool
> for their whole lifetime: this was hit with an arm64 Gunyah protected
> VM whose virtio-gpu resources stay mapped in the pool, and a desktop at
> 3392x2400 needed more than 341 MiB.
>
> The problem was analysed and this change written with the help of an
> AI coding assistant.
>
> Fixes: 0b84e4f8b793 ("swiotlb: Add restricted DMA pool initialization")
> Cc: stable@vger.kernel.org
> Signed-off-by: yzddMr6 <yzddmr6@gmail.com>
The change looks fine, but according to kernel rules, the patch must be
signed with a real developer name. 'yzddMr6' doesn't look like a real name.
> ...
Best regards
--
Marek Szyprowski, PhD
Samsung R&D Institute Poland
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-20 14:18 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <CGME20261120131146eucas1p2d05549af0404d6af77ce3e9385e91fa7@eucas1p2.samsung.com>
2026-11-20 13:11 ` [PATCH] dma: swiotlb: Allocate the restricted DMA pool slot array with kvzalloc() yzddMr6 via B4 Relay
2026-11-20 14:18 ` Marek Szyprowski
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®