dma-pool: Fix atomic pool selection
v2 cover dma-pool: Fix atomic pool selection
This is my attempt at fixing one of the regressions we've seen[1] after the introduction of per-zone atomic pools.
This combined with "dma-pool: Do not allocate pool memory from CMA"[2] should fix the boot issues on Jeremy's RPi4 setup.
[1] https://lkml.org/lkml/2020/7/2/974
[2] https://lkml.org/lkml/2020/7/8/1108
---
Changes since v1:
- Rebase into linus' master
Nicolas Saenz Julienne (4):
dma-direct: Provide function to check physical memory area validity
dma-pool: Get rid of dma_in_atomic_pool()
dma-pool: Introduce dma_guess_pool()
dma-pool: Make sure atomic pool suits device
include/linux/dma-direct.h | 1 + kernel/dma/direct.c | 2 +- kernel/dma/pool.c | 80 +++++++++++++++++++++++++------------- 3 files changed, 56 insertions(+), 27 deletions(-)
Signature · 1 line
2.27.0
PATCH v2 1/4 dma-direct: Provide function to check physical memory area validity
dma_coherent_ok() checks if a physical memory area fits a device's DMA constraints.
--- include/linux/dma-direct.h | 1 + kernel/dma/direct.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-)
include/linux/dma-direct.h +1 -0
@@ -69,6 +69,7 @@ static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size,u64 dma_direct_get_required_mask (struct device *dev);gfp_t dma_direct_optimal_gfp_mask (struct device *dev,u64 dma_mask,u64 *phys_mask); +bool dma_coherent_ok (struct device *dev,phys_addr_t phys,size_t size);void *dma_direct_alloc (struct device *dev,size_t size,dma_addr_t *dma_handle,gfp_t gfp,unsigned long attrs); void dma_direct_free(struct device *dev, size_t size, void *cpu_addr,
kernel/dma/direct.c +1 -1
@@ -70,7 +70,7 @@ gfp_t dma_direct_optimal_gfp_mask(struct device *dev, u64 dma_mask,return 0 ; } -static bool dma_coherent_ok (struct device *dev,phys_addr_t phys,size_t size) +bool dma_coherent_ok (struct device *dev,phys_addr_t phys,size_t size) {return phys_to_dma_direct (dev, phys) + size -1 <=min_not_zero (dev->coherent_dma_mask, dev->bus_dma_limit);
Signature · 6 lines
2.27.0 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
PATCH v2 2/4 dma-pool: Get rid of dma_in_atomic_pool()
In reply to Nicolas Saenz JulienneThe function is only used once and can be simplified to a one-liner.
--- kernel/dma/pool.c | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-)
kernel/dma/pool.c +1 -10
@@ -217,15 +217,6 @@ static inline struct gen_pool *dev_to_pool(struct device *dev)return atomic_pool_kernel; } -static bool dma_in_atomic_pool (struct device *dev,void *start,size_t size) -{ -struct gen_pool *pool =dev_to_pool (dev); - -if (unlikely (!pool)) -return false; -return gen_pool_has_addr (pool, (unsigned long )start, size); -} -void *dma_alloc_from_pool (struct device *dev,size_t size,struct page **ret_page,gfp_t flags) {
@@ -260,7 +251,7 @@ bool dma_free_from_pool(struct device *dev, void *start, size_t size) {struct gen_pool *pool =dev_to_pool (dev); -if (!dma_in_atomic_pool (dev, start, size)) +if (!pool || !gen_pool_has_addr (pool, (unsigned long )start, size))return false;gen_pool_free (pool, (unsigned long )start, size);return true;
Signature · 6 lines
2.27.0 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
PATCH v2 3/4 dma-pool: Introduce dma_guess_pool()
In reply to Nicolas Saenz Juliennedma-pool's dev_to_pool() creates the false impression that there is a way to grantee a mapping between a device's DMA constraints and an atomic pool. It tuns out it's just a guess, and the device might need to use an atomic pool containing memory from a 'safer' (or lower) memory zone.
To help mitigate this, introduce dma_guess_pool() which can be fed a device's DMA constraints and atomic pools already known to be faulty, in order for it to provide an better guess on which pool to use.
--- kernel/dma/pool.c | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-)
kernel/dma/pool.c +23 -3
@@ -203,7 +203,7 @@ static int __init dma_atomic_pool_init(void) }postcore_initcall (dma_atomic_pool_init); -static inline struct gen_pool *dev_to_pool (struct device *dev) +static inline struct gen_pool *dma_guess_pool_from_device (struct device *dev) {u64 phys_mask;gfp_t gfp;
@@ -217,10 +217,30 @@ static inline struct gen_pool *dev_to_pool(struct device *dev)return atomic_pool_kernel; } +static inline struct gen_pool *dma_get_safer_pool (struct gen_pool *bad_pool) +{ +if (bad_pool == atomic_pool_kernel) +return atomic_pool_dma32 ? : atomic_pool_dma; + +if (bad_pool == atomic_pool_dma32) +return atomic_pool_dma; + +return NULL ; +} + +static inline struct gen_pool *dma_guess_pool (struct device *dev, +struct gen_pool *bad_pool) +{ +if (bad_pool) +return dma_get_safer_pool (bad_pool); + +return dma_guess_pool_from_device (dev); +} +void *dma_alloc_from_pool (struct device *dev,size_t size,struct page **ret_page,gfp_t flags) { -struct gen_pool *pool =dev_to_pool (dev); +struct gen_pool *pool =dma_guess_pool (dev,NULL );unsigned long val;void *ptr =NULL ;
@@ -249,7 +269,7 @@ void *dma_alloc_from_pool(struct device *dev, size_t size,bool dma_free_from_pool (struct device *dev,void *start,size_t size) { -struct gen_pool *pool =dev_to_pool (dev); +struct gen_pool *pool =dma_guess_pool (dev,NULL );if (!pool || !gen_pool_has_addr (pool, (unsigned long )start, size))return false;
Signature · 1 line
2.27.0
PATCH v2 4/4 dma-pool: Make sure atomic pool suits device
In reply to Nicolas Saenz JulienneWhen allocating DMA memory from a pool, the core can only guess which atomic pool will fit a device's constraints. If it doesn't, get a safer atomic pool and try again.
---
Changes since v1:
- Rebase to linus' master
kernel/dma/pool.c | 57 ++++++++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 20 deletions(-)
kernel/dma/pool.c +37 -20
@@ -240,39 +240,56 @@ static inline struct gen_pool *dma_guess_pool(struct device *dev,void *dma_alloc_from_pool (struct device *dev,size_t size,struct page **ret_page,gfp_t flags) { -struct gen_pool *pool =dma_guess_pool (dev,NULL ); -unsigned long val; +struct gen_pool *pool =NULL ; +unsigned long val =0 ;void *ptr =NULL ; - -if (!pool) { -WARN (1 ,"%pGg atomic pool not initialised!\n" , &flags); -return NULL ; +phys_addr_t phys; + +while (1 ) { + pool =dma_guess_pool (dev, pool); +if (!pool) { +WARN (1 ,"Failed to get suitable pool for %s\n" , +dev_name (dev)); +break ; + } + + val =gen_pool_alloc (pool, size); +if (!val) +continue ; + + phys =gen_pool_virt_to_phys (pool, val); +if (dma_coherent_ok (dev, phys, size)) +break ; + +gen_pool_free (pool, val, size); + val =0 ; } - val =gen_pool_alloc (pool, size); -if (likely (val)) { -phys_addr_t phys =gen_pool_virt_to_phys (pool, val); +if (val) { *ret_page =pfn_to_page (__phys_to_pfn (phys)); ptr = (void *)val;memset (ptr,0 , size); - }else { -WARN_ONCE (1 ,"DMA coherent pool depleted, increase size " -"(recommended min coherent_pool=%zuK)\n" , -gen_pool_size (pool) >>9 ); + +if (gen_pool_avail (pool) < atomic_pool_size) +schedule_work (&atomic_pool_work); } -if (gen_pool_avail (pool) < atomic_pool_size) -schedule_work (&atomic_pool_work);return ptr; }bool dma_free_from_pool (struct device *dev,void *start,size_t size) { -struct gen_pool *pool =dma_guess_pool (dev,NULL ); +struct gen_pool *pool =NULL ; -if (!pool || !gen_pool_has_addr (pool, (unsigned long )start, size)) -return false; -gen_pool_free (pool, (unsigned long )start, size); -return true; +while (1 ) { + pool =dma_guess_pool (dev, pool); +if (!pool) +return false; + +if (gen_pool_has_addr (pool, (unsigned long )start, size)) { +gen_pool_free (pool, (unsigned long )start, size); +return true; + } + } }
Signature · 6 lines
2.27.0 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
Christoph Hellwig
AppliedQuoted 5 lines · Nicolas Saenz Julienne
This is my attempt at fixing one of the regressions we've seen[1] after the introduction of per-zone atomic pools.
This combined with "dma-pool: Do not allocate pool memory from CMA"[2] should fix the boot issues on Jeremy's RPi4 setup.
I've applied the series as well as that patch to the dma-mapping tree, thanks.