.map_sg() error cleanup
Subsystems (15)
v3 cover .map_sg() error cleanup
Hi,
This v3 of the series is spun out and expanded from my work to add P2PDMA support to DMA map operations[1]. v2 is at [2]. The main changes in v1 are to more carefully define the meaning of the error codes for dma_map_sgtable().
The P2PDMA work requires distinguishing different error conditions in a map_sg operation. dma_map_sgtable() already allows for returning an error code (where as dma_map_sg() is only allowed to return zero) however, it currently only returns -EINVAL when a .map_sg() call returns zero.
This series cleans up all .map_sg() implementations to return appropriate error codes. After the cleanup, dma_map_sg() will still return zero, however dma_map_sgtable() will pass the error code from the .map_sg() call. Thanks go to Martn Oliveira for doing a lot of the cleanup of the obscure implementations.
The patch set is based off of v5.14-rc2 and a git repo can be found here:
https://github.com/sbates130272/linux-p2pmem map_sg_err_cleanup_v2
Thanks,
Logan
[1] https://lore.kernel.org/linux-block/[email protected]/
[2] https://lore.kernel.org/linux-mips/[email protected]/
--
Changes in v3:
- Move the validation of errnos into __dma_map_sg_attrs() (Per Christoph)
- Fix the out of date commit message in patch 21 (Per Eike)
Changes in v2:
- Attempt to define the meanings of the errors returned by dma_map_sgtable() and restrict the valid return codes of .map_sg implementations. (Per Christoph)
- Change dma_map_sgtable() to EXPORT_SYMBOL_GPL() (Per Christoph)
- Add patches to remove the erroneous setting of sg->dma_address to DMA_MAP_ERROR in a few .map_sg(0 implementations. (Per Christoph).
--
Logan Gunthorpe (10):
dma-mapping: Allow map_sg() ops to return negative error codes
dma-direct: Return appropriate error code from dma_direct_map_sg()
iommu: Return full error code from iommu_map_sg[_atomic]()
dma-iommu: Return error code from iommu_dma_map_sg() ARM/dma-mapping: don't set failed sg dma_address to DMA_MAPPING_ERROR powerpc/iommu: don't set failed sg dma_address to DMA_MAPPING_ERROR s390/pci: don't set failed sg dma_address to DMA_MAPPING_ERROR sparc/iommu: don't set failed sg dma_address to DMA_MAPPING_ERROR x86/amd_gart: don't set failed sg dma_address to DMA_MAPPING_ERROR
dma-mapping: Disallow .map_sg operations from returning zero on error
Martin Oliveira (11):
alpha: return error code from alpha_pci_map_sg() ARM/dma-mapping: return error code from .map_sg() ops ia64/sba_iommu: return error code from sba_map_sg_attrs() MIPS/jazzdma: return error code from jazz_dma_map_sg() powerpc/iommu: return error code from .map_sg() ops s390/pci: return error code from s390_dma_map_sg() sparc/iommu: return error codes from .map_sg() ops
parisc: return error code from .map_sg() ops
xen: swiotlb: return error code from xen_swiotlb_map_sg() x86/amd_gart: return error code from gart_map_sg()
dma-mapping: return error code from dma_dummy_map_sg()
arch/alpha/kernel/pci_iommu.c | 10 ++- arch/arm/mm/dma-mapping.c | 26 +++++--- arch/ia64/hp/common/sba_iommu.c | 6 +- arch/mips/jazz/jazzdma.c | 2 +- arch/powerpc/kernel/iommu.c | 6 +- arch/powerpc/platforms/ps3/system-bus.c | 2 +- arch/powerpc/platforms/pseries/vio.c | 5 +- arch/s390/pci/pci_dma.c | 13 ++-- arch/sparc/kernel/iommu.c | 6 +- arch/sparc/kernel/pci_sun4v.c | 6 +- arch/sparc/mm/iommu.c | 2 +- arch/x86/kernel/amd_gart_64.c | 18 +++--- drivers/iommu/dma-iommu.c | 23 ++++--- drivers/iommu/iommu.c | 15 +++-- drivers/parisc/ccio-dma.c | 2 +- drivers/parisc/sba_iommu.c | 2 +- drivers/xen/swiotlb-xen.c | 2 +- include/linux/dma-map-ops.h | 5 +- include/linux/dma-mapping.h | 35 +++-------- include/linux/iommu.h | 22 +++---- kernel/dma/direct.c | 2 +- kernel/dma/dummy.c | 2 +- kernel/dma/mapping.c | 82 ++++++++++++++++++++++--- 23 files changed, 177 insertions(+), 117 deletions(-)
base-commit: ff1176468d368232b684f75e82563369208bc371 --
2.20.1
PATCH v3 1/21 dma-mapping: Allow map_sg() ops to return negative error codes
Allow dma_map_sgtable() to pass errors from the map_sg() ops. This will be required for returning appropriate error codes when mapping P2PDMA memory.
Introduce __dma_map_sg_attrs() which will return the raw error code from the map_sg operation (whether it be negative or zero). Then add a dma_map_sg_attrs() wrapper to convert any negative errors to zero to satisfy the existing calling convention.
dma_map_sgtable() defines three error codes that .map_sg implementations are allowed to return: -EINVAL, -ENOMEM and -EIO. The latter of which is a generic return for cases that are passing DMA_MAPPING_ERROR through.
dma_map_sgtable() will convert a zero error return for old map_sg() ops into a -EIO return and return any negative errors as reported.
This allows map_sg implementations to start returning multiple negative error codes. Legacy map_sg implementations can continue to return zero until they are all converted.
--- include/linux/dma-map-ops.h | 5 ++- include/linux/dma-mapping.h | 35 ++++------------ kernel/dma/mapping.c | 84 +++++++++++++++++++++++++++++++++---- 3 files changed, 86 insertions(+), 38 deletions(-)
include/linux/dma-map-ops.h +3 -2
@@ -41,8 +41,9 @@ struct dma_map_ops { size_t size, enum dma_data_direction dir, unsigned long attrs);/* -* map_sg returns 0 on error and a value > 0 on success. -* It should never return a value < 0. +* map_sg should return a negative error code on error. See +* dma_map_sgtable() for a list of appropriate error codes +* and their meanings. */ int (*map_sg)(struct device *dev,struct scatterlist *sg,int nents,enum dma_data_direction dir,unsigned long attrs);
include/linux/dma-mapping.h +7 -28
@@ -110,6 +110,8 @@ int dma_map_sg_attrs(struct device *dev, struct scatterlist *sg, int nents,void dma_unmap_sg_attrs (struct device *dev,struct scatterlist *sg,int nents,enum dma_data_direction dir,unsigned long attrs); +int dma_map_sgtable (struct device *dev,struct sg_table *sgt, +enum dma_data_direction dir,unsigned long attrs);dma_addr_t dma_map_resource (struct device *dev,phys_addr_t phys_addr,size_t size,enum dma_data_direction dir,unsigned long attrs); void dma_unmap_resource(struct device *dev, dma_addr_t addr, size_t size,
@@ -174,6 +176,11 @@ static inline void dma_unmap_sg_attrs(struct device *dev, unsigned long attrs) { } +static inline int dma_map_sgtable (struct device *dev,struct sg_table *sgt, +enum dma_data_direction dir,unsigned long attrs) +{ +return -EOPNOTSUPP ; +}static inline dma_addr_t dma_map_resource (struct device *dev,phys_addr_t phys_addr,size_t size,enum dma_data_direction dir,unsigned long attrs)
@@ -343,34 +350,6 @@ static inline void dma_sync_single_range_for_device(struct device *dev,return dma_sync_single_for_device (dev, addr + offset, size, dir); } -/** -* dma_map_sgtable - Map the given buffer for DMA -* @dev: The device for which to perform the DMA operation -* @sgt: The sg_table object describing the buffer -* @dir: DMA direction -* @attrs: Optional DMA attributes for the map operation -* -* Maps a buffer described by a scatterlist stored in the given sg_table -* object for the @dir DMA operation by the @dev device. After success the -* ownership for the buffer is transferred to the DMA domain. One has to -* call dma_sync_sgtable_for_cpu() or dma_unmap_sgtable() to move the -* ownership of the buffer back to the CPU domain before touching the -* buffer by the CPU. -* -* Returns 0 on success or -EINVAL on error during mapping the buffer. -*/ -static inline int dma_map_sgtable (struct device *dev,struct sg_table *sgt, -enum dma_data_direction dir,unsigned long attrs) -{ -int nents; - - nents =dma_map_sg_attrs (dev, sgt->sgl, sgt->orig_nents, dir, attrs); -if (nents <=0 ) -return -EINVAL ; - sgt->nents = nents; -return 0 ; -} -/** * dma_unmap_sgtable - Unmap the given buffer for DMA * @dev: The device for which to perform the DMA operation
kernel/dma/mapping.c +76 -8
101 lines not on this page · raw message
Signature · 1 line
2.20.1
PATCH v3 2/21 dma-direct: Return appropriate error code from dma_direct_map_sg()
In reply to Logan GunthorpeNow that the map_sg() op expects error codes instead of return zero on error, convert dma_direct_map_sg() to return an error code. Per the documentation for dma_map_sgtable(), -EIO is returned due to an DMA_MAPPING_ERROR with unknown cause.
--- kernel/dma/direct.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/dma/direct.c +1 -1
@@ -411,7 +411,7 @@ int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl, int nents, out_unmap:dma_direct_unmap_sg (dev, sgl, i, dir, attrs |DMA_ATTR_SKIP_CPU_SYNC ); -return 0 ; +return -EIO ; } dma_addr_t dma_direct_map_resource(struct device *dev, phys_addr_t paddr,
Signature · 1 line
2.20.1
PATCH v3 3/21 iommu: Return full error code from iommu_map_sg[_atomic]()
In reply to Logan GunthorpeConvert to ssize_t return code so the return code from __iommu_map() can be returned all the way down through dma_iommu_map_sg().
--- drivers/iommu/iommu.c | 15 +++++++-------- include/linux/iommu.h | 22 +++++++++++----------- 2 files changed, 18 insertions(+), 19 deletions(-)
drivers/iommu/iommu.c +7 -8
@@ -2567,9 +2567,9 @@ size_t iommu_unmap_fast(struct iommu_domain *domain, }EXPORT_SYMBOL_GPL (iommu_unmap_fast); -static size_t __iommu_map_sg (struct iommu_domain *domain,unsigned long iova, -struct scatterlist *sg,unsigned int nents,int prot, -gfp_t gfp) +static ssize_t __iommu_map_sg (struct iommu_domain *domain,unsigned long iova, +struct scatterlist *sg,unsigned int nents,int prot, +gfp_t gfp) {const struct iommu_ops *ops = domain->ops;size_t len =0 , mapped =0 ;
@@ -2610,19 +2610,18 @@ static size_t __iommu_map_sg(struct iommu_domain *domain, unsigned long iova,/* undo mappings already done */ iommu_unmap (domain, iova, mapped); -return 0 ; - +return ret; } -size_t iommu_map_sg (struct iommu_domain *domain,unsigned long iova, -struct scatterlist *sg,unsigned int nents,int prot) +ssize_t iommu_map_sg (struct iommu_domain *domain,unsigned long iova, +struct scatterlist *sg,unsigned int nents,int prot) {might_sleep ();return __iommu_map_sg (domain, iova, sg, nents, prot,GFP_KERNEL ); }EXPORT_SYMBOL_GPL (iommu_map_sg); -size_t iommu_map_sg_atomic (struct iommu_domain *domain,unsigned long iova, +ssize_t iommu_map_sg_atomic (struct iommu_domain *domain,unsigned long iova,struct scatterlist *sg,unsigned int nents,int prot) {return __iommu_map_sg (domain, iova, sg, nents, prot,GFP_ATOMIC );
include/linux/iommu.h +11 -11
@@ -414,11 +414,11 @@ extern size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova,extern size_t iommu_unmap_fast (struct iommu_domain *domain,unsigned long iova,size_t size,struct iommu_iotlb_gather *iotlb_gather); -extern size_t iommu_map_sg (struct iommu_domain *domain,unsigned long iova, -struct scatterlist *sg,unsigned int nents,int prot); -extern size_t iommu_map_sg_atomic (struct iommu_domain *domain, -unsigned long iova,struct scatterlist *sg, -unsigned int nents,int prot); +extern ssize_t iommu_map_sg (struct iommu_domain *domain,unsigned long iova, +struct scatterlist *sg,unsigned int nents,int prot); +extern ssize_t iommu_map_sg_atomic (struct iommu_domain *domain, +unsigned long iova,struct scatterlist *sg, +unsigned int nents,int prot);extern phys_addr_t iommu_iova_to_phys (struct iommu_domain *domain,dma_addr_t iova);extern void iommu_set_fault_handler (struct iommu_domain *domain,iommu_fault_handler_t handler,void *token);
@@ -679,18 +679,18 @@ static inline size_t iommu_unmap_fast(struct iommu_domain *domain,return 0 ; } -static inline size_t iommu_map_sg (struct iommu_domain *domain, -unsigned long iova,struct scatterlist *sg, -unsigned int nents,int prot) +static inline ssize_t iommu_map_sg (struct iommu_domain *domain, +unsigned long iova,struct scatterlist *sg, +unsigned int nents,int prot) { -return 0 ; +return -ENODEV ; } -static inline size_t iommu_map_sg_atomic (struct iommu_domain *domain, +static inline ssize_t iommu_map_sg_atomic (struct iommu_domain *domain,unsigned long iova,struct scatterlist *sg,unsigned int nents,int prot) { -return 0 ; +return -ENODEV ; }static inline void iommu_flush_iotlb_all (struct iommu_domain *domain)
Signature · 1 line
2.20.1
PATCH v3 4/21 dma-iommu: Return error code from iommu_dma_map_sg()
In reply to Logan GunthorpeReturn appropriate error codes EINVAL or ENOMEM from iommup_dma_map_sg(). If lower level code returns ENOMEM, then we return it, other errors are coalesced into EINVAL.
iommu_dma_map_sg_swiotlb() returns -EIO as its an unknown error from a call that returns DMA_MAPPING_ERROR.
--- drivers/iommu/dma-iommu.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-)
drivers/iommu/dma-iommu.c +16 -7
@@ -972,7 +972,7 @@ static int iommu_dma_map_sg_swiotlb(struct device *dev, struct scatterlist *sg, out_unmap:iommu_dma_unmap_sg_swiotlb (dev, sg, i, dir, attrs |DMA_ATTR_SKIP_CPU_SYNC ); -return 0 ; +return -EIO ; }/*
@@ -993,11 +993,13 @@ static int iommu_dma_map_sg(struct device *dev, struct scatterlist *sg,dma_addr_t iova;size_t iova_len =0 ;unsigned long mask =dma_get_seg_boundary (dev); +ssize_t ret;int i; -if (static_branch_unlikely (&iommu_deferred_attach_enabled) && -iommu_deferred_attach (dev, domain)) -return 0 ; +if (static_branch_unlikely (&iommu_deferred_attach_enabled)) { + ret =iommu_deferred_attach (dev, domain); + goto out; + }if (!(attrs &DMA_ATTR_SKIP_CPU_SYNC ))iommu_dma_sync_sg_for_device (dev, sg, nents, dir);
@@ -1045,14 +1047,17 @@ static int iommu_dma_map_sg(struct device *dev, struct scatterlist *sg, } iova =iommu_dma_alloc_iova (domain, iova_len,dma_get_mask (dev), dev); -if (!iova) +if (!iova) { + ret = -ENOMEM ; goto out_restore_sg; + }/* * We'll leave any physical concatenation to the IOMMU driver's * implementation - it knows better than we do. */ -if (iommu_map_sg_atomic (domain, iova, sg, nents, prot) < iova_len) + ret =iommu_map_sg_atomic (domain, iova, sg, nents, prot); +if (ret < iova_len) goto out_free_iova;return __finalise_sg (dev, sg, nents, iova);
@@ -1061,7 +1066,11 @@ static int iommu_dma_map_sg(struct device *dev, struct scatterlist *sg,iommu_dma_free_iova (cookie, iova, iova_len,NULL ); out_restore_sg:__invalidate_sg (sg, nents); -return 0 ; +out: +if (ret == -ENOMEM ) +return ret; +else +return -EINVAL ; } static void iommu_dma_unmap_sg(struct device *dev, struct scatterlist *sg,
Signature · 1 line
2.20.1
PATCH v3 5/21 alpha: return error code from alpha_pci_map_sg()
In reply to Logan GunthorpeFrom: Martin Oliveira <[email protected]>
The .map_sg() op now expects an error code instead of zero on failure.
pci_map_single_1() can fail for different reasons, but since the only supported type of error return is DMA_MAPPING_ERROR, we coalesce those errors into EIO.
ENOMEM is returned when no page tables can be allocated.
--- arch/alpha/kernel/pci_iommu.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
arch/alpha/kernel/pci_iommu.c +7 -3
@@ -649,7 +649,9 @@ static int alpha_pci_map_sg(struct device *dev, struct scatterlist *sg, sg->dma_address =pci_map_single_1 (pdev,SG_ENT_VIRT_ADDRESS (sg), sg->length, dac_allowed); -return sg->dma_address !=DMA_MAPPING_ERROR ; +if (sg->dma_address ==DMA_MAPPING_ERROR ) +return -EIO ; +return 1 ; } start = sg;
@@ -685,8 +687,10 @@ static int alpha_pci_map_sg(struct device *dev, struct scatterlist *sg,if (out < end) out->dma_length =0 ; -if (out - start ==0 ) +if (out - start ==0 ) {printk (KERN_WARNING "pci_map_sg failed: no entries?\n" ); +return -ENOMEM ; + }DBGA ("pci_map_sg: %ld entries\n" , out - start);return out - start;
@@ -699,7 +703,7 @@ static int alpha_pci_map_sg(struct device *dev, struct scatterlist *sg,entries. Unmap them now. */ if (out > start)pci_unmap_sg (pdev, start, out - start, dir); -return 0 ; +return -ENOMEM ; }/* Unmap a set of streaming mode DMA translations. Again, cpu read
Signature · 1 line
2.20.1
PATCH v3 6/21 ARM/dma-mapping: return error code from .map_sg() ops
In reply to Logan GunthorpeFrom: Martin Oliveira <[email protected]>
The .map_sg() op now expects an error code instead of zero on failure. In the case of a DMA_MAPPING_ERROR, -EIO is returned. Otherwise, -ENOMEM or -EINVAL is returned depending on the error from __map_sg_chunk().
--- arch/arm/mm/dma-mapping.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-)
arch/arm/mm/dma-mapping.c +16 -9
@@ -980,7 +980,7 @@ int arm_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents, {const struct dma_map_ops *ops =get_dma_ops (dev);struct scatterlist *s; -int i, j; +int i, j, ret;for_each_sg (sg, s, nents, i) {#ifdef CONFIG_NEED_SG_DMA_LENGTH
@@ -988,15 +988,17 @@ int arm_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,#endif s->dma_address = ops->map_page (dev,sg_page (s), s->offset, s->length, dir, attrs); -if (dma_mapping_error (dev, s->dma_address)) +if (dma_mapping_error (dev, s->dma_address)) { + ret = -EIO ; goto bad_mapping; + } }return nents; bad_mapping:for_each_sg (sg, s, i, j) ops->unmap_page (dev,sg_dma_address (s),sg_dma_len (s), dir, attrs); -return 0 ; +return ret; }/**
@@ -1622,7 +1624,7 @@ static int __iommu_map_sg(struct device *dev, struct scatterlist *sg, int nents, bool is_coherent) {struct scatterlist *s = sg, *dma = sg, *start = sg; -int i, count =0 ; +int i, count =0 , ret;unsigned int offset = s->offset;unsigned int size = s->offset + s->length;unsigned int max =dma_get_max_seg_size (dev);
@@ -1634,8 +1636,10 @@ static int __iommu_map_sg(struct device *dev, struct scatterlist *sg, int nents, s->dma_length =0 ;if (s->offset || (size & ~PAGE_MASK ) || size + s->length > max) { -if (__map_sg_chunk (dev, start, size, &dma->dma_address, - dir, attrs, is_coherent) <0 ) + ret =__map_sg_chunk (dev, start, size, + &dma->dma_address, dir, attrs, + is_coherent); +if (ret <0 ) goto bad_mapping; dma->dma_address += offset;
@@ -1648,8 +1652,9 @@ static int __iommu_map_sg(struct device *dev, struct scatterlist *sg, int nents, } size += s->length; } -if (__map_sg_chunk (dev, start, size, &dma->dma_address, dir, attrs, - is_coherent) <0 ) + ret =__map_sg_chunk (dev, start, size, &dma->dma_address, dir, attrs, + is_coherent); +if (ret <0 ) goto bad_mapping; dma->dma_address += offset;
@@ -1660,7 +1665,9 @@ static int __iommu_map_sg(struct device *dev, struct scatterlist *sg, int nents, bad_mapping:for_each_sg (sg, s, count, i)__iommu_remove_mapping (dev,sg_dma_address (s),sg_dma_len (s)); -return 0 ; +if (ret == -ENOMEM ) +return ret; +return -EINVAL ; }/**
Signature · 1 line
2.20.1
PATCH v3 7/21 ARM/dma-mapping: don't set failed sg dma_address to DMA_MAPPING_ERROR
In reply to Logan GunthorpeSetting the ->dma_address to DMA_MAPPING_ERROR is not part of the ->map_sg calling convention, so remove it.
--- arch/arm/mm/dma-mapping.c | 1 - 1 file changed, 1 deletion(-)
arch/arm/mm/dma-mapping.c +0 -1
@@ -1632,7 +1632,6 @@ static int __iommu_map_sg(struct device *dev, struct scatterlist *sg, int nents,for (i =1 ; i < nents; i++) { s =sg_next (s); - s->dma_address =DMA_MAPPING_ERROR ; s->dma_length =0 ;if (s->offset || (size & ~PAGE_MASK ) || size + s->length > max) {
Signature · 1 line
2.20.1
PATCH v3 8/21 ia64/sba_iommu: return error code from sba_map_sg_attrs()
In reply to Logan GunthorpeFrom: Martin Oliveira <[email protected]>
The .map_sg() op now expects an error code instead of zero on failure.
In the case of a dma_mapping_error() return -EIO as the actual cause is opaque here.
sba_coalesce_chunks() may only presently fail if sba_alloc_range() fails, which in turn only fails if the iommu is out of mapping resources, hence a -ENOMEM is used in that case.
--- arch/ia64/hp/common/sba_iommu.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
arch/ia64/hp/common/sba_iommu.c +3 -3
@@ -1458,8 +1458,8 @@ static int sba_map_sg_attrs(struct device *dev, struct scatterlist *sglist, sglist->dma_length = sglist->length; sglist->dma_address =sba_map_page (dev,sg_page (sglist), sglist->offset, sglist->length, dir, attrs); -if (dma_mapping_error (dev, sglist->dma_address)) -return 0 ; +if (dma_mapping_error (dev, sglist->dma_address)) +return -EIO ;return 1 ; }
@@ -1486,7 +1486,7 @@ static int sba_map_sg_attrs(struct device *dev, struct scatterlist *sglist, coalesced =sba_coalesce_chunks (ioc, dev, sglist, nents);if (coalesced <0 ) {sba_unmap_sg_attrs (dev, sglist, nents, dir, attrs); -return 0 ; +return -ENOMEM ; }/*
Signature · 1 line
2.20.1
PATCH v3 9/21 MIPS/jazzdma: return error code from jazz_dma_map_sg()
In reply to Logan GunthorpeFrom: Martin Oliveira <[email protected]>
The .map_sg() op now expects an error code instead of zero on failure.
vdma_alloc() may fail for different reasons, but since it only supports indicating an error via a return of DMA_MAPPING_ERROR, we coalesce the different reasons into -EIO as is documented on dma_map_sgtable().
--- arch/mips/jazz/jazzdma.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/mips/jazz/jazzdma.c +1 -1
@@ -552,7 +552,7 @@ static int jazz_dma_map_sg(struct device *dev, struct scatterlist *sglist, dir); sg->dma_address =vdma_alloc (sg_phys (sg), sg->length);if (sg->dma_address ==DMA_MAPPING_ERROR ) -return 0 ; +return -EIO ;sg_dma_len (sg) = sg->length; }
Signature · 1 line
2.20.1
PATCH v3 10/21 powerpc/iommu: return error code from .map_sg() ops
In reply to Logan GunthorpeFrom: Martin Oliveira <[email protected]>
The .map_sg() op now expects an error code instead of zero on failure.
Propagate the error up if vio_dma_iommu_map_sg() fails.
ppc_iommu_map_sg() may fail either because of iommu_range_alloc() or because of tbl->it_ops->set(). The former only supports returning an error with DMA_MAPPING_ERROR and an examination of the latter indicates that it may return arch-specific errors (for example, tce_buildmulti_pSeriesLP()). Hence, coalesce all of those errors into -EIO, per the documentation on dma_map_sgtable().
--- arch/powerpc/kernel/iommu.c | 4 ++-- arch/powerpc/platforms/ps3/system-bus.c | 2 +- arch/powerpc/platforms/pseries/vio.c | 5 +++-- 3 files changed, 6 insertions(+), 5 deletions(-)
arch/powerpc/kernel/iommu.c +2 -2
@@ -473,7 +473,7 @@ int ppc_iommu_map_sg(struct device *dev, struct iommu_table *tbl,BUG_ON (direction ==DMA_NONE );if ((nelems ==0 ) || !tbl) -return 0 ; +return -EINVAL ; outs = s = segstart = &sglist[0 ]; outcount =1 ;
@@ -599,7 +599,7 @@ int ppc_iommu_map_sg(struct device *dev, struct iommu_table *tbl,if (s == outs)break ; } -return 0 ; +return -EIO ; }
arch/powerpc/platforms/ps3/system-bus.c +1 -1
@@ -663,7 +663,7 @@ static int ps3_ioc0_map_sg(struct device *_dev, struct scatterlist *sg, unsigned long attrs) {BUG (); -return 0 ; +return -EINVAL ; } static void ps3_sb_unmap_sg(struct device *_dev, struct scatterlist *sg,
arch/powerpc/platforms/pseries/vio.c +3 -2
@@ -560,7 +560,8 @@ static int vio_dma_iommu_map_sg(struct device *dev, struct scatterlist *sglist,for_each_sg (sglist, sgl, nelems, count) alloc_size +=roundup (sgl->length,IOMMU_PAGE_SIZE (tbl)); -if (vio_cmo_alloc (viodev, alloc_size)) + ret =vio_cmo_alloc (viodev, alloc_size); +if (ret) goto out_fail; ret =ppc_iommu_map_sg (dev, tbl, sglist, nelems,dma_get_mask (dev), direction, attrs);
@@ -577,7 +578,7 @@ static int vio_dma_iommu_map_sg(struct device *dev, struct scatterlist *sglist,vio_cmo_dealloc (viodev, alloc_size); out_fail:atomic_inc (&viodev->cmo.allocs_failed); -return 0 ; +return ret; } static void vio_dma_iommu_unmap_sg(struct device *dev,
Signature · 1 line
2.20.1
PATCH v3 11/21 powerpc/iommu: don't set failed sg dma_address to DMA_MAPPING_ERROR
In reply to Logan GunthorpeSetting the ->dma_address to DMA_MAPPING_ERROR is not part of the ->map_sg calling convention, so remove it.
--- arch/powerpc/kernel/iommu.c | 2 -- 1 file changed, 2 deletions(-)
arch/powerpc/kernel/iommu.c +0 -2
@@ -575,7 +575,6 @@ int ppc_iommu_map_sg(struct device *dev, struct iommu_table *tbl,*/ if (outcount < incount) { outs =sg_next (outs); - outs->dma_address =DMA_MAPPING_ERROR ; outs->dma_length =0 ; }
@@ -593,7 +592,6 @@ int ppc_iommu_map_sg(struct device *dev, struct iommu_table *tbl, npages =iommu_num_pages (s->dma_address, s->dma_length,IOMMU_PAGE_SIZE (tbl));__iommu_free (tbl, vaddr, npages); - s->dma_address =DMA_MAPPING_ERROR ; s->dma_length =0 ; }if (s == outs)
Signature · 1 line
2.20.1
PATCH v3 12/21 s390/pci: return error code from s390_dma_map_sg()
In reply to Logan GunthorpeFrom: Martin Oliveira <[email protected]>
The .map_sg() op now expects an error code instead of zero on failure.
So propagate the error from __s390_dma_map_sg() up. __s390_dma_map_sg() returns either -ENOMEM on allocation failure or -EINVAL which is the same as what's expected by dma_map_sgtable().
--- arch/s390/pci/pci_dma.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)
arch/s390/pci/pci_dma.c +7 -5
@@ -487,7 +487,7 @@ static int s390_dma_map_sg(struct device *dev, struct scatterlist *sg,unsigned int max =dma_get_max_seg_size (dev);unsigned int size = s->offset + s->length;unsigned int offset = s->offset; -int count =0 , i; +int count =0 , i, ret;for (i =1 ; i < nr_elements; i++) { s =sg_next (s);
@@ -497,8 +497,9 @@ static int s390_dma_map_sg(struct device *dev, struct scatterlist *sg,if (s->offset || (size & ~PAGE_MASK ) || size + s->length > max) { -if (__s390_dma_map_sg (dev, start, size, - &dma->dma_address, dir)) + ret =__s390_dma_map_sg (dev, start, size, + &dma->dma_address, dir); +if (ret) goto unmap; dma->dma_address += offset;
@@ -511,7 +512,8 @@ static int s390_dma_map_sg(struct device *dev, struct scatterlist *sg, } size += s->length; } -if (__s390_dma_map_sg (dev, start, size, &dma->dma_address, dir)) + ret =__s390_dma_map_sg (dev, start, size, &dma->dma_address, dir); +if (ret) goto unmap; dma->dma_address += offset;
@@ -523,7 +525,7 @@ static int s390_dma_map_sg(struct device *dev, struct scatterlist *sg,s390_dma_unmap_pages (dev,sg_dma_address (s),sg_dma_len (s), dir, attrs); -return 0 ; +return ret; } static void s390_dma_unmap_sg(struct device *dev, struct scatterlist *sg,
Signature · 1 line
2.20.1
PATCH v3 13/21 s390/pci: don't set failed sg dma_address to DMA_MAPPING_ERROR
In reply to Logan GunthorpeSetting the ->dma_address to DMA_MAPPING_ERROR is not part of the ->map_sg calling convention, so remove it.
--- arch/s390/pci/pci_dma.c | 1 - 1 file changed, 1 deletion(-)
arch/s390/pci/pci_dma.c +0 -1
@@ -492,7 +492,6 @@ static int s390_dma_map_sg(struct device *dev, struct scatterlist *sg,for (i =1 ; i < nr_elements; i++) { s =sg_next (s); - s->dma_address =DMA_MAPPING_ERROR ; s->dma_length =0 ; if (s->offset || (size & ~PAGE_MASK) ||
Signature · 1 line
2.20.1
PATCH v3 14/21 sparc/iommu: return error codes from .map_sg() ops
In reply to Logan GunthorpeFrom: Martin Oliveira <[email protected]>
The .map_sg() op now expects an error code instead of zero on failure.
Returning an errno from __sbus_iommu_map_sg() results in sbus_iommu_map_sg_gflush() and sbus_iommu_map_sg_pflush() returning an errno, as those functions are wrappers around __sbus_iommu_map_sg().
--- arch/sparc/kernel/iommu.c | 4 ++-- arch/sparc/kernel/pci_sun4v.c | 4 ++-- arch/sparc/mm/iommu.c | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-)
arch/sparc/kernel/iommu.c +2 -2
@@ -448,7 +448,7 @@ static int dma_4u_map_sg(struct device *dev, struct scatterlist *sglist, iommu = dev->archdata.iommu; strbuf = dev->archdata.stc;if (nelems ==0 || !iommu) -return 0 ; +return -EINVAL ;spin_lock_irqsave (&iommu->lock, flags);
@@ -580,7 +580,7 @@ static int dma_4u_map_sg(struct device *dev, struct scatterlist *sglist, }spin_unlock_irqrestore (&iommu->lock, flags); -return 0 ; +return -EINVAL ; }/* If contexts are being used, they are the same in all of the mappings
arch/sparc/kernel/pci_sun4v.c +2 -2
@@ -486,7 +486,7 @@ static int dma_4v_map_sg(struct device *dev, struct scatterlist *sglist, iommu = dev->archdata.iommu;if (nelems ==0 || !iommu) -return 0 ; +return -EINVAL ; atu = iommu->atu; prot =HV_PCI_MAP_ATTR_READ ;
@@ -619,7 +619,7 @@ static int dma_4v_map_sg(struct device *dev, struct scatterlist *sglist, }local_irq_restore (flags); -return 0 ; +return -EINVAL ; } static void dma_4v_unmap_sg(struct device *dev, struct scatterlist *sglist,
arch/sparc/mm/iommu.c +1 -1
@@ -256,7 +256,7 @@ static int __sbus_iommu_map_sg(struct device *dev, struct scatterlist *sgl, sg->dma_address =__sbus_iommu_map_page (dev,sg_page (sg), sg->offset, sg->length, per_page_flush);if (sg->dma_address ==DMA_MAPPING_ERROR ) -return 0 ; +return -EIO ; sg->dma_length = sg->length; }
Signature · 1 line
2.20.1
PATCH v3 15/21 sparc/iommu: don't set failed sg dma_address to DMA_MAPPING_ERROR
In reply to Logan GunthorpeSetting the ->dma_address to DMA_MAPPING_ERROR is not part of the ->map_sg calling convention, so remove it.
--- arch/sparc/kernel/iommu.c | 2 -- arch/sparc/kernel/pci_sun4v.c | 2 -- 2 files changed, 4 deletions(-)
arch/sparc/kernel/iommu.c +0 -2
@@ -546,7 +546,6 @@ static int dma_4u_map_sg(struct device *dev, struct scatterlist *sglist,if (outcount < incount) { outs =sg_next (outs); - outs->dma_address =DMA_MAPPING_ERROR ; outs->dma_length =0 ; }
@@ -572,7 +571,6 @@ static int dma_4u_map_sg(struct device *dev, struct scatterlist *sglist,iommu_tbl_range_free (&iommu->tbl, vaddr, npages,IOMMU_ERROR_CODE ); - s->dma_address =DMA_MAPPING_ERROR ; s->dma_length =0 ; }if (s == outs)
arch/sparc/kernel/pci_sun4v.c +0 -2
@@ -594,7 +594,6 @@ static int dma_4v_map_sg(struct device *dev, struct scatterlist *sglist,if (outcount < incount) { outs =sg_next (outs); - outs->dma_address =DMA_MAPPING_ERROR ; outs->dma_length =0 ; }
@@ -611,7 +610,6 @@ static int dma_4v_map_sg(struct device *dev, struct scatterlist *sglist,iommu_tbl_range_free (tbl, vaddr, npages,IOMMU_ERROR_CODE );/* XXX demap? XXX */ - s->dma_address =DMA_MAPPING_ERROR ; s->dma_length =0 ; }if (s == outs)
Signature · 1 line
2.20.1
PATCH v3 16/21 parisc: return error code from .map_sg() ops
In reply to Logan GunthorpeFrom: Martin Oliveira <[email protected]>
The .map_sg() op now expects an error code instead of zero on failure. Return -EINVAL if the ioc cannot be obtained.
--- drivers/parisc/ccio-dma.c | 2 +- drivers/parisc/sba_iommu.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/parisc/ccio-dma.c +1 -1
@@ -918,7 +918,7 @@ ccio_map_sg(struct device *dev, struct scatterlist *sglist, int nents,BUG_ON (!dev); ioc =GET_IOC (dev);if (!ioc) -return 0 ; +return -EINVAL ;DBG_RUN_SG ("%s() START %d entries\n" , __func__, nents);
drivers/parisc/sba_iommu.c +1 -1
@@ -947,7 +947,7 @@ sba_map_sg(struct device *dev, struct scatterlist *sglist, int nents, ioc =GET_IOC (dev);if (!ioc) -return 0 ; +return -EINVAL ;/* Fast path single entry scatterlists. */ if (nents ==1 ) {
Signature · 1 line
2.20.1
PATCH v3 17/21 xen: swiotlb: return error code from xen_swiotlb_map_sg()
In reply to Logan GunthorpeFrom: Martin Oliveira <[email protected]>
The .map_sg() op now expects an error code instead of zero on failure.
xen_swiotlb_map_sg() may only fail if xen_swiotlb_map_page() fails, but xen_swiotlb_map_page() only supports returning errors as DMA_MAPPING_ERROR. So coalesce all errors into EIO per the documentation for dma_map_sgtable().
--- drivers/xen/swiotlb-xen.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/xen/swiotlb-xen.c +1 -1
@@ -509,7 +509,7 @@ xen_swiotlb_map_sg(struct device *dev, struct scatterlist *sgl, int nelems, out_unmap:xen_swiotlb_unmap_sg (dev, sgl, i, dir, attrs |DMA_ATTR_SKIP_CPU_SYNC );sg_dma_len (sgl) =0 ; -return 0 ; +return -EIO ; } static void
Signature · 1 line
2.20.1
PATCH v3 18/21 x86/amd_gart: return error code from gart_map_sg()
In reply to Logan GunthorpeFrom: Martin Oliveira <[email protected]>
The .map_sg() op now expects an error code instead of zero on failure.
So make __dma_map_cont() return a valid errno (which is then propagated to gart_map_sg() via dma_map_cont()) and return it in case of failure.
Also, return -EINVAL in case of invalid nents.
--- arch/x86/kernel/amd_gart_64.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-)
arch/x86/kernel/amd_gart_64.c +9 -7
@@ -331,7 +331,7 @@ static int __dma_map_cont(struct device *dev, struct scatterlist *start,int i;if (iommu_start ==-1 ) -return -1 ; +return -ENOMEM ;for_each_sg (start, s, nelems, i) {unsigned long pages, addr;
@@ -380,13 +380,13 @@ static int gart_map_sg(struct device *dev, struct scatterlist *sg, int nents, enum dma_data_direction dir, unsigned long attrs) {struct scatterlist *s, *ps, *start_sg, *sgmap; -int need =0 , nextneed, i, out, start; +int need =0 , nextneed, i, out, start, ret;unsigned long pages =0 ;unsigned int seg_size;unsigned int max_seg_size;if (nents ==0 ) -return 0 ; +return -EINVAL ; out =0 ; start =0 ;
@@ -414,8 +414,9 @@ static int gart_map_sg(struct device *dev, struct scatterlist *sg, int nents,if (!iommu_merge || !nextneed || !need || s->offset || (s->length + seg_size > max_seg_size) || (ps->offset + ps->length) %PAGE_SIZE ) { -if (dma_map_cont (dev, start_sg, i - start, - sgmap, pages, need) <0 ) + ret =dma_map_cont (dev, start_sg, i - start, + sgmap, pages, need); +if (ret <0 ) goto error; out++;
@@ -432,7 +433,8 @@ static int gart_map_sg(struct device *dev, struct scatterlist *sg, int nents, pages +=iommu_num_pages (s->offset, s->length,PAGE_SIZE ); ps = s; } -if (dma_map_cont (dev, start_sg, i - start, sgmap, pages, need) <0 ) + ret =dma_map_cont (dev, start_sg, i - start, sgmap, pages, need); +if (ret <0 ) goto error; out++;flush_gart ();
@@ -458,7 +460,7 @@ static int gart_map_sg(struct device *dev, struct scatterlist *sg, int nents,iommu_full (dev, pages <<PAGE_SHIFT , dir);for_each_sg (sg, s, nents, i) s->dma_address =DMA_MAPPING_ERROR ; -return 0 ; +return ret; }/* allocate and map a coherent mapping */
Signature · 1 line
2.20.1
PATCH v3 19/21 x86/amd_gart: don't set failed sg dma_address to DMA_MAPPING_ERROR
In reply to Logan GunthorpeSetting the ->dma_address to DMA_MAPPING_ERROR is not part of the ->map_sg calling convention, so remove it.
--- arch/x86/kernel/amd_gart_64.c | 2 -- 1 file changed, 2 deletions(-)
arch/x86/kernel/amd_gart_64.c +0 -2
@@ -458,8 +458,6 @@ static int gart_map_sg(struct device *dev, struct scatterlist *sg, int nents,panic ("dma_map_sg: overflow on %lu pages\n" , pages);iommu_full (dev, pages <<PAGE_SHIFT , dir); -for_each_sg (sg, s, nents, i) - s->dma_address =DMA_MAPPING_ERROR ;return ret; }
Signature · 1 line
2.20.1
PATCH v3 20/21 dma-mapping: return error code from dma_dummy_map_sg()
In reply to Logan GunthorpeFrom: Martin Oliveira <[email protected]>
The .map_sg() op now expects an error code instead of zero on failure.
The only errno to return is -EINVAL in the case when DMA is not supported.
--- kernel/dma/dummy.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/dma/dummy.c +1 -1
@@ -22,7 +22,7 @@ static int dma_dummy_map_sg(struct device *dev, struct scatterlist *sgl, int nelems, enum dma_data_direction dir, unsigned long attrs) { - return 0; + return -EINVAL; }static int dma_dummy_supported (struct device *hwdev,u64 mask)
Signature · 1 line
2.20.1
PATCH v3 21/21 dma-mapping: Disallow .map_sg operations from returning zero on error
In reply to Logan GunthorpeNow that all the .map_sg operations have been converted to returning proper error codes, drop the code to handle a zero return value, add a warning if a zero is returned.
--- kernel/dma/mapping.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
kernel/dma/mapping.c +3 -5
@@ -196,8 +196,8 @@ static int __dma_map_sg_attrs(struct device *dev, struct scatterlist *sg,if (ents >0 )debug_dma_map_sg (dev, sg, nents, ents, dir); -else if (WARN_ON_ONCE (ents != -EINVAL && ents != -ENOMEM && - ents != -EIO && ents !=0 )) +else if (WARN_ON_ONCE ((ents != -EINVAL && ents != -ENOMEM && + ents != -EIO ) || ents ==0 ))return -EIO ;return ents;
@@ -262,9 +262,7 @@ int dma_map_sgtable(struct device *dev, struct sg_table *sgt,int nents; nents =__dma_map_sg_attrs (dev, sgt->sgl, sgt->orig_nents, dir, attrs); -if (nents ==0 ) -return -EIO ; -else if (nents <0 ) +if (nents <0 )return nents; sgt->nents = nents;
Signature · 1 line
2.20.1
Thanks,
I've applied this to the dma-mapping tree with a few minor cosmetic tweaks.