dma-coherent: add support for multi coherent rmems per dev
Subsystems (2)
PATCH v2 1/1 dma-coherent: add support for multi coherent rmems per dev
Add support for multiple coherent rmems per device. This patch replaces original dma_mem with dma_mems list in device structure to store multiple rmems.
These multiple rmems can be assigned to the device one by one by of_reserved_mem_device_init_by_idx() with the memory-region declaration in device tree as below and store the rmem to the dma_mems list.
device1@0 {
...
memory-region = <&reserved_mem0>, <&reserved_mem1>;
...
};When driver tries to allocate memory from the rmems, looks for the first available rmem and allocates the memory from this rmem.
Then if driver removed, of_reserved_mem_device_release() needs to be invoked to release all the rmems assigned to the device.
---
Changelog since v1:
(suggested by Robin Murphy <[email protected]>)
- Replace the pointer(dma_mem) to a list_head(dma_mems) in the device structure and initialize the list_head in device_initialize().
- Modify the required changes in coherent.c.
drivers/base/core.c | 3 ++ include/linux/device.h | 5 ++-- kernel/dma/coherent.c | 66 ++++++++++++++++++++++++++++-------------- 3 files changed, 50 insertions(+), 24 deletions(-)
drivers/base/core.c +3 -0
@@ -3108,6 +3108,9 @@ void device_initialize(struct device *dev)defined (CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU ) || \defined (CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL ) dev->dma_coherent = dma_default_coherent; +#endif +#ifdef CONFIG_DMA_DECLARE_COHERENT +INIT_LIST_HEAD (&dev->dma_mems);#endif swiotlb_dev_init (dev); }
include/linux/device.h +2 -3
@@ -648,7 +648,7 @@ struct device_physical_location {* @dma_parms: A low level driver may set these to teach IOMMU code about * segment limitations. * @dma_pools: Dma pools (if dma'ble device). -* @dma_mem: Internal for coherent mem override. +* @dma_mems: Internal for coherent mems. * @cma_area: Contiguous memory area for dma allocations * @dma_io_tlb_mem: Software IO TLB allocator. Not for driver use. * @dma_io_tlb_pools: List of transient swiotlb memory pools.
@@ -749,8 +749,7 @@ struct device {struct list_head dma_pools;/* dma pools (if dma'ble) */ #ifdef CONFIG_DMA_DECLARE_COHERENT -struct dma_coherent_mem *dma_mem;/* internal for coherent mem -override */ +struct list_head dma_mems;/* Internal for coherent mems */ #endif #ifdef CONFIG_DMA_CMA struct cma *cma_area;/* contiguous memory area for dma
kernel/dma/coherent.c +45 -21
123 lines not on this page · raw message
Signature · 1 line
2.43.0.594.gd9cf4e227d-goog
Quoted 20 lines · Howard Yen
Add support for multiple coherent rmems per device. This patch replaces original
dma_memwithdma_memslist in device structure to store multiple rmems.These multiple rmems can be assigned to the device one by one by
of_reserved_mem_device_init_by_idx()with the memory-region declaration in device tree as below and store the rmem to thedma_memslist.device1@0 { ... memory-region = <&reserved_mem0>, <&reserved_mem1>; ... };When driver tries to allocate memory from the rmems, looks for the first available rmem and allocates the memory from this rmem.
Then if driver removed,
of_reserved_mem_device_release()needs to be invoked to release all the rmems assigned to the device.
...
kernel/dma/coherent.c struct dma_coherent_mem { · line 21
Quoted 7 lines
kernel/dma/coherent.c +1
-0@@ -18,15 +18,9 @@ struct dma_coherent_mem {unsigned long *bitmap;spinlock_t spinlock;bool use_dev_dma_pfn_offset; +struct list_head node;
Have you run pahole? Here I see wasted bytes for nothing.
Quoted 1 line
kernel/dma/coherent.c +0
-0};
...
kernel/dma/coherent.c int dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr, · line 130
Quoted 5 lines
void
dma_release_coherent_memory(struct device *dev){kernel/dma/coherent.c +2
-0+struct dma_coherent_mem *mem_tmp, *q; +if (dev) {
While at it, perhaps
if (!dev) return;
?
Quoted 8 lines
kernel/dma/coherent.c +4
-2-_dma_release_coherent_memory (dev->dma_mem); - dev->dma_mem =NULL ; +list_for_each_entry_safe (mem_tmp, q, &dev->dma_mems, node) { +list_del_init (&mem_tmp->node); +_dma_release_coherent_memory (mem_tmp); + } } }
...
kernel/dma/coherent.c static int __dma_release_from_coherent(struct dma_coherent_mem *mem, · line 232
Quoted 5 lines
int
dma_release_from_dev_coherent(struct device *dev, int order, void *vaddr){kernel/dma/coherent.c +2
-1-struct dma_coherent_mem *mem =dev_get_coherent_memory (dev); +struct dma_coherent_mem *mem_tmp; +int ret =0 ;
'ret' (1)
Quoted 9 lines
kernel/dma/coherent.c +7
-1-return __dma_release_from_coherent (mem, order, vaddr); +list_for_each_entry (mem_tmp, &dev->dma_mems, node) { + ret =__dma_release_from_coherent (mem_tmp, order, vaddr); +if (ret ==1 ) +break ; + } + +return ret; }
...
kernel/dma/coherent.c static int __dma_mmap_from_coherent(struct dma_coherent_mem *mem, · line 284
Quoted 6 lines
int dma_mmap_from_dev_coherent(struct device *dev, struct
vm_area_struct*vma,kernel/dma/coherent.c +2
-1void *vaddr, size_t size, int *ret) { - struct dma_coherent_mem *mem = dev_get_coherent_memory(dev); + struct dma_coherent_mem *mem_tmp; +int retval =0 ;
'retval' (2)
Can we be consistent, please? (See (1) and (2) above.)
Quoted 9 lines
kernel/dma/coherent.c +6
-1+list_for_each_entry (mem_tmp, &dev->dma_mems, node) { + retval =__dma_mmap_from_coherent (mem_tmp, vma, vaddr, size, ret); +if (retval ==1 ) +break ; + } -return __dma_mmap_from_coherent (mem, vma, vaddr, size, ret); +return retval; }
...
kernel/dma/coherent.c static int rmem_dma_device_init(struct reserved_mem *rmem, struct device *dev)
Quoted 8 lines
static void rmem_dma_device_release(struct
reserved_mem*rmem,kernel/dma/coherent.c +2
-2struct device *dev) { -if (dev) - dev->dma_mem =NULL ; +struct dma_coherent_mem *mem_tmp, *q; +if (dev) {
As per above, esp. taking into account that you touch this line. With proposed modification you won't need to.
kernel/dma/coherent.c static int rmem_dma_device_init(struct reserved_mem *rmem, struct device *dev) · line 380
Quoted 8 lines
kernel/dma/coherent.c +7
-0+list_for_each_entry_safe (mem_tmp, q, &dev->dma_mems, node) { +if (mem_tmp == rmem->priv) { +list_del_init (&mem_tmp->node); +break ; + } + } + } }
Better question, do we really need the dev check (at least in static functions) or it can be ommitted?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 1 line · Andy Shevchenko
On Mon, Feb 05, 2024 at 07:23:00AM +0000, Howard Yen wrote:
...
Quoted 7 lines
kernel/dma/coherent.c struct dma_coherent_mem { · line 21
Quoted 5 lines
kernel/dma/coherent.c +1
-0@@ -18,15 +18,9 @@ struct dma_coherent_mem {unsigned long *bitmap;spinlock_t spinlock;bool use_dev_dma_pfn_offset; +struct list_head node;Have you run
pahole? Here I see wasted bytes for nothing.
On top of that one may make container_of() to be no-op, by placing this member to be the first one. But, double check this with bloat-o-meter (that it indeed does better code generation) and on the other hand check if the current first member is not performance critical and having additional pointer arithmetics is okay.
Quoted 1 line
Quoted 1 line
(quoted diff) +0
-0};
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 20 lines · Andy Shevchenko
Quoted 1 line · Andy Shevchenko
On Mon, Feb 05, 2024 at 07:23:00AM +0000, Howard Yen wrote:
...
Quoted 7 lines
kernel/dma/coherent.c struct dma_coherent_mem { · line 21
Quoted 5 lines
kernel/dma/coherent.c +1
-0@@ -18,15 +18,9 @@ struct dma_coherent_mem {unsigned long *bitmap;spinlock_t spinlock;bool use_dev_dma_pfn_offset; +struct list_head node;Have you run
pahole? Here I see wasted bytes for nothing.On top of that one may make
container_of()to be no-op, by placing this member to be the first one. But, double check this with bloat-o-meter (that it indeed does better code generation) and on the other hand check if the current first member is not performance critical and having additional pointer arithmetics is okay.
Quoted 8 lines
Quoted 1 line
Review struct dma_coherent_mem { · line 22
Quoted 1 line
(quoted diff) +0
-0};--
With Best Regards,
Andy Shevchenko
I'm trying to re-org the members as below
from ===>
struct dma_coherent_mem {
void * virt_base; /* 0 8 */ dma_addr_t device_base; /* 8 8 */ unsigned long pfn_base; /* 16 8 */ int size; /* 24 4 */
/* XXX 4 bytes hole, try to pack */
unsigned long * bitmap; /* 32 8 */ spinlock_t spinlock; /* 40 4 */ bool use_dev_dma_pfn_offset; /* 44 1 */
/* XXX 3 bytes hole, try to pack */
struct list_head node; /* 48 16 */
/* size: 64, cachelines: 1, members: 8 */
/* sum members: 57, holes: 2, sum holes: 7 */
};
to ===>
struct dma_coherent_mem {
struct list_head node; /* 0 16 */ void * virt_base; /* 16 8 */ dma_addr_t device_base; /* 24 8 */ unsigned long pfn_base; /* 32 8 */ int size; /* 40 4 */ spinlock_t spinlock; /* 44 4 */ unsigned long * bitmap; /* 48 8 */ bool use_dev_dma_pfn_offset; /* 56 1 */
/* size: 64, cachelines: 1, members: 8 */
/* padding: 7 */
};
Looks like there is about 7 bytes padding at the end of the structure. Should I add __attribute__((__packed__)) to not add the padding?
Signature · 3 lines
Regards, Howard
Quoted 4 lines · Howard Yen
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Andy Shevchenko
On Mon, Feb 05, 2024 at 07:23:00AM +0000, Howard Yen wrote:
...
Quoted 53 lines
Quoted 8 lines
Quoted 7 lines
kernel/dma/coherent.c struct dma_coherent_mem { · line 21
Quoted 5 lines
kernel/dma/coherent.c +1
-0@@ -18,15 +18,9 @@ struct dma_coherent_mem {unsigned long *bitmap;spinlock_t spinlock;bool use_dev_dma_pfn_offset; +struct list_head node;Have you run
pahole? Here I see wasted bytes for nothing.Quoted 7 lines
On top of that one may make
container_of()to be no-op, by placing this member to be the first one. But, double check this with bloat-o-meter (that it indeed does better code generation) and on the other hand check if the current first member is not performance critical and having additional pointer arithmetics is okay.Quoted 1 line
Quoted 1 line
(quoted diff) +0
-0};I'm trying to re-org the members as below
from ===>
struct
dma_coherent_mem{void * virt_base; /* 0 8 */ dma_addr_t device_base; /* 8 8 */ unsigned long pfn_base; /* 16 8 */ int size; /* 24 4 *//* XXX 4 bytes hole, try to pack */
unsigned long * bitmap; /* 32 8 */ spinlock_t spinlock; /* 40 4 */ bool use_dev_dma_pfn_offset; /* 44 1 *//* XXX 3 bytes hole, try to pack */
struct
list_headnode; /* 48 16 *//* size: 64, cachelines: 1, members: 8 */
/* sum members: 57, holes: 2, sum holes: 7 */
};to ===>
struct
dma_coherent_mem{struct list_head node; /* 0 16 */ void * virt_base; /* 16 8 */ dma_addr_t device_base; /* 24 8 */ unsigned long pfn_base; /* 32 8 */ int size; /* 40 4 */ spinlock_t spinlock; /* 44 4 */ unsigned long * bitmap; /* 48 8 */ bool use_dev_dma_pfn_offset; /* 56 1 */
Quoted 2 lines
/* size: 64, cachelines: 1, members: 8 */
/* padding: 7 */
Which seems better that above, right?
Quoted 4 lines
};
Looks like there is about 7 bytes padding at the end of the structure. Should I add __attribute__((__packed__)) to not add the padding?
No, __packed is about alignment, may give you much worse code generation. With list_head member first you might get better code from the original, check it with bloat-o-meter.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 72 lines · Andy Shevchenko
Quoted 4 lines · Howard Yen
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Andy Shevchenko
On Mon, Feb 05, 2024 at 07:23:00AM +0000, Howard Yen wrote:
...
Quoted 53 lines
Quoted 8 lines
Quoted 7 lines
kernel/dma/coherent.c struct dma_coherent_mem { · line 21
Quoted 5 lines
kernel/dma/coherent.c +1
-0@@ -18,15 +18,9 @@ struct dma_coherent_mem {unsigned long *bitmap;spinlock_t spinlock;bool use_dev_dma_pfn_offset; +struct list_head node;Have you run
pahole? Here I see wasted bytes for nothing.Quoted 7 lines
On top of that one may make
container_of()to be no-op, by placing this member to be the first one. But, double check this with bloat-o-meter (that it indeed does better code generation) and on the other hand check if the current first member is not performance critical and having additional pointer arithmetics is okay.Quoted 1 line
Quoted 1 line
(quoted diff) +0
-0};I'm trying to re-org the members as below
from ===>
struct
dma_coherent_mem{void * virt_base; /* 0 8 */ dma_addr_t device_base; /* 8 8 */ unsigned long pfn_base; /* 16 8 */ int size; /* 24 4 *//* XXX 4 bytes hole, try to pack */
unsigned long * bitmap; /* 32 8 */ spinlock_t spinlock; /* 40 4 */ bool use_dev_dma_pfn_offset; /* 44 1 *//* XXX 3 bytes hole, try to pack */
struct
list_headnode; /* 48 16 *//* size: 64, cachelines: 1, members: 8 */
/* sum members: 57, holes: 2, sum holes: 7 */
};to ===>
struct
dma_coherent_mem{struct list_head node; /* 0 16 */ void * virt_base; /* 16 8 */ dma_addr_t device_base; /* 24 8 */ unsigned long pfn_base; /* 32 8 */ int size; /* 40 4 */ spinlock_t spinlock; /* 44 4 */ unsigned long * bitmap; /* 48 8 */ bool use_dev_dma_pfn_offset; /* 56 1 */Quoted 2 lines
/* size: 64, cachelines: 1, members: 8 */
/* padding: 7 */Which seems better that above, right?
Quoted 4 lines
};
Looks like there is about 7 bytes padding at the end of the structure. Should I add __attribute__((__packed__)) to not add the padding?
Quoted 9 lines
No,
__packedis about alignment, may give you much worse code generation. Withlist_headmember first you might get better code from the original, check it with bloat-o-meter.--
With Best Regards,
Andy Shevchenko
From the check result with bloat-o-meter, there is about 3.38% reduction totally from the
original version. Thanks for the suggestion!
add/remove: 0/0 grow/shrink: 0/7 up/down: 0/-60 (-60)
Function old new delta rmem_dma_device_release 104 100 -4 dma_release_from_dev_coherent 184 180 -4 dma_release_coherent_memory 144 140 -4 dma_mmap_from_dev_coherent 228 224 -4 dma_init_coherent_memory 292 284 -8 rmem_dma_device_init 168 152 -16 dma_declare_coherent_memory 184 164 -20
Total: Before=1776, After=1716, chg -3.38%
add/remove: 0/0 grow/shrink: 0/0 up/down: 0/0 (0)
Data old new delta
Total: Before=0, After=0, chg +0.00%
add/remove: 0/0 grow/shrink: 0/0 up/down: 0/0 (0)
RO Data old new delta
Total: Before=216, After=216, chg +0.00%
For the dev check, in the previous comment, they're in the static function and are assigned
to ops function pointers, I think the check is required because they might be invoked from
others.
I'll submit v3 with the members reorg, the return variable naming changes and if (!dev) return; .
Signature · 3 lines
Regards, Howard