drm/msm: followup - map page-less imported sg_tables
Subsystems (5)
cover drm/msm: followup - map page-less imported sg_tables
Hi Rob,
this is the follow-up I promised: it fixes the two remaining map paths that still consume struct page/sg->length of imported sg_tables, and therefore completes the DMABUF_DEBUG=y story when applied on top of your series [1].
Test results on x1e78100 (Snapdragon X1E78100, DMABUF_DEBUG=y, kernel v7.3-rc4 based), which is the machine that originally reported the UCHE translation faults and the one hardware test environment for imports of buffers not first exported by drm/msm:
- v7.3-rc4 stock: arm-smmu UCHE translation faults during hardware video decode in clapper/chromium (bisected to 143755bdabaa9)
- your series alone: video plays via the userspace copy fallback, zero GPU faults, but a WARN storm from __arm_lpae_unmap() (472 traces within a minute of video playback) - every map of a page-less sg_table silently maps nothing, so the matching unmap finds no PTE and WARNs. This matches your cover letter saying the map paths are not addressed by the series. Your patch 2 ("mmap imported dma-bufs via the exporter") in particular works:
a test importing a linux,cma-heap buffer via DRM_IOCTL_PRIME_FD_TO_HANDLE and mmaps it both through the heap fd and through msm's GEM mmap sees the same memory through both mappings, including writes visible in both directions.
- your series + this series: zero arm-smmu faults, zero io-pgtable WARNs, and hardware video decode displays correctly in clapper and chromium. The GPU per-process pagetable path is exercised with real decoder capture buffers (videobuf2-dma-contig, 2MB frames) and maps them from their DMA addresses.
Patch 1 exports iommu_get_dma_domain(), which msm needs to look up the dma_addr -> phys mapping of imported buffers through the msm drm device's DMA-API domain. On platforms where the drm device is direct mapped (like mine: ae01000.display-controller is in no iommu_group) the lookup degenerates to the identity; on platforms where it sits behind a translated IOMMU the translation is needed.
Happy to rebase on whatever your tree looks like - and I can rerun the full test matrix (WARN count, fault count, kmssink scanout of real V4L2 frames, chromium/clapper hardware decode) on any revision.
[1] <[email protected]>
Jianfeng Liu (2):
iommu: export iommu_get_dma_domain()
drm/msm: map page-less imported sg_tables from their DMA addresses
drivers/gpu/drm/msm/msm_gem_vma.c | 9 ++++ drivers/gpu/drm/msm/msm_iommu.c | 90 ++++++++++++++++++++++++++++++- drivers/gpu/drm/msm/msm_mmu.h | 13 +++++ drivers/iommu/iommu.c | 1 + 4 files changed, 111 insertions(+), 2 deletions(-)
---
base-commit: d6882ed4e5b3970a13d721a07d2737c4831cdced
branch: msm-map-followup-v1
Signature · 1 line
2.47.3
PATCH 1/2 iommu: export iommu_get_dma_domain()
drm/msm needs to recover the physical address behind a DMA address of imported dma-bufs: with CONFIG_DMABUF_DEBUG=y the attachment sg_table it hands to importers carries no struct page, and msm has to translate sg_dma_address() back to a physical address through the attaching device's DMA-API domain to map such buffers into its own pagetables (see the follow-up drm/msm patch).
Export the helper so modularized drivers can use it.
---
drivers/iommu/iommu.c | 1 + 1 file changed, 1 insertion(+)
drivers/iommu/iommu.c +1 -0
@@ -2327,6 +2327,7 @@ struct iommu_domain *iommu_get_dma_domain(struct device *dev) {return dev->iommu_group->default_domain; } +EXPORT_SYMBOL_GPL (iommu_get_dma_domain);static void *iommu_make_pasid_array_entry (struct iommu_domain *domain,struct iommu_attach_handle *handle)
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH v1 1/2] iommu: export iommu_get_dma_domain()
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] iommu: architectural violation bypassing DMA API and dma-buf isolation - [Medium] iommu: missing…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] iommu: architectural violation bypassing DMA API and dma-buf isolation
- [Medium] iommu: missing NULL check in exported iommu_get_dma_domain()
- [Low] iommu: missing fallback stub for iommu_get_dma_domain()
--
commit 9558776a7b7367aa853a5851fe8a04b46536b527
Author: Jianfeng Liu <[email protected]>
iommu: export iommu_get_dma_domain()
This commit exports iommu_get_dma_domain() to allow modular drivers to recover the physical address behind a DMA address for imported dma-bufs.
drivers/iommu/iommu.c struct iommu_domain *iommu_get_dma_domain(struct device *dev) · line 2328
Quoted 7 lines
drivers/iommu/iommu.c +0
-0@@ -2327,6 +2327,7 @@ struct iommu_domain *iommu_get_dma_domain(struct device *dev) {return dev->iommu_group->default_domain;
[Severity: Medium]
Does this code introduce a regression where modular callers could trigger a kernel panic? An unconditional dereference of dev->iommu_group will crash if the passed device does not have an IOMMU group, which can occur with SWIOTLB fallbacks or missing bindings.
drivers/iommu/iommu.c struct iommu_domain *iommu_get_dma_domain(struct device *dev) · line 2330
Quoted 2 lines
drivers/iommu/iommu.c +1
-0} +EXPORT_SYMBOL_GPL (iommu_get_dma_domain);
[Severity: High]
Can exporting this function lead to memory corruption by breaking DMA API and dma-buf isolation? By allowing callers to reverse-translate IOVAs via iommu_iova_to_phys(), it bypasses the opacity of the DMA mapping API. This specifically defeats CONFIG_DMABUF_DEBUG (which zeroes struct page pointers to catch this abuse) and could be dangerous if the DMA API uses SWIOTLB bounce buffers or dynamic deferred TLB invalidation.
[Severity: Low]
Will this cause build regressions for configs lacking CONFIG_IOMMU_API? Since the function is now exported for general modular use, any caller built without CONFIG_IOMMU_API will fail to link or compile because include/linux/iommu.h lacks a corresponding empty stub in the !CONFIG_IOMMU_API block.
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Robin Murphy
reviewerRe: [PATCH v1 1/2] iommu: export iommu_get_dma_domain()
In reply to Jianfeng LiuQuoted 8 lines · Jianfeng Liu
drm/msm needs to recover the physical address behind a DMA address of imported dma-bufs: with
CONFIG_DMABUF_DEBUG=ythe attachmentsg_tableit hands to importers carries no struct page, and msm has to translatesg_dma_address()back to a physical address through the attaching device's DMA-API domain to map such buffers into its own pagetables (see the follow-up drm/msm patch).Export the helper so modularized drivers can use it.
NAK. Read the comment - this is an internal helper for iommu-dma (currently the only IOMMU_DOMAIN_DMA implementation) and is definitely not for random drivers to abuse. In fact it should really move to iommu-priv.h now that we have that.
If you want to do sketchy things with your device's DMA domain then use the public iommu_get_domain_for_dev() interface like everyone else.
Thanks,
Robin.
Quoted 23 lines
Signed-off-by Jianfeng Liu <[email protected]>---
drivers/iommu/iommu.c | 1 + 1 file changed, 1 insertion(+)drivers/iommu/iommu.c +1
-0@@ -2327,6 +2327,7 @@ struct iommu_domain *iommu_get_dma_domain(struct device *dev) {return dev->iommu_group->default_domain; } +EXPORT_SYMBOL_GPL (iommu_get_dma_domain);static void *iommu_make_pasid_array_entry (struct iommu_domain *domain,struct iommu_attach_handle *handle)
Re: [PATCH v1 1/2] iommu: export iommu_get_dma_domain()
Hi Robin,
Quoted 7 lines · Robin Murphy
NAK. Read the comment - this is an internal helper for iommu-dma (currently the only
IOMMU_DOMAIN_DMAimplementation) and is definitely not for random drivers to abuse. In fact it should really move toiommu-priv.hnow that we have that.If you want to do sketchy things with your device's DMA domain then use the public
iommu_get_domain_for_dev()interface like everyone else.
Fair enough - I'll drop the export.
For the record, if the msm side of the series goes anywhere, the plan is to capture iommu_get_domain_for_dev(drm->dev) before msm takes the device over with iommu_attach_device() - at that point the device's current domain is still the DMA domain, so no new export is needed. (Christian is pushing back on the msm patch itself, so that may end up moot either way.)
Thanks for the pointer,
Jianfeng
PATCH 2/2 drm/msm: map page-less imported sg_tables from their DMA addresses
In reply to Jianfeng LiuWith CONFIG_DMABUF_DEBUG=y, dma_buf_map_attachment() hands importers a copy of the attachment sg_table with the struct page pointers stripped and sg->length zeroed; only sg_dma_address()/sg_dma_len() are carried over. msm consumes sg->length and sg_phys() in both of its map paths:
- msm_iommu_pagetable_map() (userspace managed, per-process GPU pagetables) walks the sg_table with sg->length and sg_phys()
- msm_iommu_map() (kernel managed mappings: display, and TTBR1 for the GPU), via iommu_map_sgtable(), which consumes sg->length and sg_phys() as well
With a page-stripped sg_table both paths silently map nothing and return success. Userspace then observes arm-smmu translation faults once the GPU first touches the mapping, e.g. during hardware video decode:
gpu fault: ttbr0=000000088a889000 iova=000000010741c000 dir=READ type=TRANSLATION source=UCHE
and __arm_lpae_unmap() WARNs for the never-mapped ranges when the GEM handles are closed (a WARN storm of ~470 traces within a minute of video playback on my x1e78100 laptop).
For sg entries that still carry a struct page (native objects, and imports without the DMABUF_DEBUG wrapper) keep using sg_phys(), so native objects which msm never dma-maps itself (non-MSM_BO_WC) are unaffected. For page-less entries, recover the physical address from the DMA address instead: dmabuf attachments are dma-mapped against the msm drm device, so the dma_addr -> phys lookup can be done with iommu_iova_to_phys() in that device's DMA-API domain, cached per VM in struct msm_mmu::dma_domain at msm_gem_vm_create() time. If the drm device is direct mapped the DMA address already is a physical address and the lookup degenerates to the identity.
Applied on top of "drm/msm/gem: Drop use of pages for imported dma-bufs" [1], which removes the remaining struct page consumers for imported buffers. With both, hardware video decode works with DMABUF_DEBUG=y, tested with clapper and chromium on x1e78100 (Snapdragon X1E78100): zero arm-smmu faults, zero io-pgtable WARNs, correct frames. Without this patch, the same system logs a WARN trace per unmap and falls back to a copy path for video playback.
[1] <[email protected]>
---
drivers/gpu/drm/msm/msm_gem_vma.c | 9 ++++ drivers/gpu/drm/msm/msm_iommu.c | 90 ++++++++++++++++++++++++++++++- drivers/gpu/drm/msm/msm_mmu.h | 13 +++++ 3 files changed, 110 insertions(+), 2 deletions(-)
drivers/gpu/drm/msm/msm_gem_vma.c +9 -0
@@ -840,6 +840,15 @@ msm_gem_vm_create(struct drm_device *drm, struct msm_mmu *mmu, const char *name, goto err_free_vm; } +/* +* dma-buf imports attach against the msm drm device, and their +* sg_dma_address() lives in that device's DMA-API domain. Keep it +* so the map paths can translate page-less sg_table entries (the +* DMABUF_DEBUG wrapper) back to physical addresses. +*/ +if (device_iommu_mapped (drm->dev)) + mmu->dma_domain =iommu_get_dma_domain (drm->dev); +if (!managed) {struct drm_sched_init_args args = { .ops = &msm_vm_bind_ops,
drivers/gpu/drm/msm/msm_iommu.c +88 -2
108 lines not on this page · raw message
drivers/gpu/drm/msm/msm_mmu.h +13 -0
@@ -64,6 +64,19 @@ struct msm_mmu {* msm_gem_vm::mmu_lock. */ struct msm_mmu_prealloc *prealloc; + +/** +* @dma_domain: DMA-API domain of the msm drm device +* +* dma-buf attachments are dma-mapped against the msm drm device, +* so this domain holds the mapping dma_addr -> phys for imported +* buffers. Used to recover the physical address of sg_table +* entries which carry no struct page (e.g. the page-stripped +* sg_table wrapper that dma_buf_map_attachment() hands out when +* CONFIG_DMABUF_DEBUG=y). NULL if the drm device is direct +* mapped, in which case DMA addresses are physical addresses. +*/ +struct iommu_domain *dma_domain; }; static inline void msm_mmu_init(struct msm_mmu *mmu, struct device *dev,
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH v1 2/2] drm/msm: map page-less imported sg_tables from their DMA addresses
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Critical] drm/msm: assuming physical contiguity for IOMMU-coalesced DMA segments - [Medium] drm/msm…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] drm/msm: assuming physical contiguity for IOMMU-coalesced DMA segments
- [Medium] drm/msm: memory leak on error path when mapping page-less sg entries --
commit 77e8df4ba7f003ef7b79e1521b4c58572c28169c
Author: Jianfeng Liu <[email protected]>
drm/msm: map page-less imported sg_tables from their DMA addresses
With CONFIG_DMABUF_DEBUG=y, dma_buf_map_attachment() hands importers a copy of the attachment sg_table with the struct page pointers stripped and sg->length zeroed. This patch translates the DMA addresses back to physical addresses using the drm device's DMA-API domain to fix arm-smmu translation faults caused by the missing page pointers.
Quoted 4 lines
drivers/gpu/drm/msm/msm_iommu.c +0
-0
[ ... ]
Quoted 17 lines
drivers/gpu/drm/msm/msm_iommu.c +11
-2@@ -184,8 +202,35 @@ static int msm_iommu_pagetable_map(struct msm_mmu *mmu, u64 iova,return msm_iommu_pagetable_map_prr (mmu, iova, len, prot);for_each_sgtable_sg (sgt, sg, i) { -size_t size = sg->length; -phys_addr_t phys =sg_phys (sg); +size_t size; +phys_addr_t phys; + +if (sg_page (sg)) { +/* CPU-view entry: native objects, and imported +* sg_tables that still carry struct page +*/ + size = sg->length; + phys =sg_phys (sg); + }else { +/*
drivers/gpu/drm/msm/msm_iommu.c static int msm_iommu_pagetable_map(struct msm_mmu *mmu, u64 iova, · line 225
Quoted 10 lines
drivers/gpu/drm/msm/msm_iommu.c +10
-0+* Page-less entry, e.g. the sg_table wrapper +* that dma_buf_map_attachment() hands out when +* CONFIG_DMABUF_DEBUG=y (page pointers stripped, +* sg->length zeroed, only the DMA fields carried +* over). Recover the physical address by +* translating the DMA address through the drm +* device's DMA-API domain. +*/ + size =sg_dma_len (sg); + phys =msm_mmu_dma_to_phys (mmu,sg_dma_address (sg));
^^^^
[Severity: Critical]
Does this assume that the entire DMA segment length mapped by the IOMMU is physically contiguous?
If the IOMMU DMA API coalesces physically discontiguous pages into a single contiguous IOVA segment, wouldn't mapping sg_dma_len(sg) using just the starting physical address map the pages after the first page to incorrect, adjacent physical addresses instead of their actual disjoint addresses?
drivers/gpu/drm/msm/msm_iommu.c static int msm_iommu_pagetable_map(struct msm_mmu *mmu, u64 iova, · line 231
Quoted 6 lines
drivers/gpu/drm/msm/msm_iommu.c +6
-0+ +if (!size || !phys) { +dev_err (mmu->dev, +"cannot map page-less sg entry: dma=%pad len=%zu\n" , + &sg_dma_address (sg), size); +return -EINVAL ;
^^^^
[Severity: Medium]
If this fails after some scatterlist entries have already been successfully mapped, does returning -EINVAL directly cause a permanent leak of the IOMMU pagetable mappings?
Should this use a cleanup path to unmap the already-mapped pages, similar to how ops->map_pages failures are handled with msm_iommu_pagetable_unmap()?
Quoted 5 lines
drivers/gpu/drm/msm/msm_iommu.c +2
-0+ } + }if (!len)break ;
[ ... ]
Quoted 17 lines
drivers/gpu/drm/msm/msm_iommu.c +13
-0@@ -697,6 +742,47 @@ static int msm_iommu_map(struct msm_mmu *mmu, uint64_t iova,if (iova &BIT_ULL (48 )) iova |=GENMASK_ULL (63 ,49 ); +/* +* With CONFIG_DMABUF_DEBUG=y, imported sg_tables carry no struct +* page and sg->length is zeroed; iommu_map_sgtable() would consume +* zero length and silently map nothing. Map from the (translated) +* DMA addresses instead. +*/ +if (!sg_page (sgt->sgl)) { +struct scatterlist *sg; +size_t mapped =0 ; +unsigned int i; + +for_each_sgtable_dma_sg (sgt, sg, i) { + phys_addr_t phys =
drivers/gpu/drm/msm/msm_iommu.c static int msm_iommu_map(struct msm_mmu *mmu, uint64_t iova, · line 767
Quoted 10 lines
drivers/gpu/drm/msm/msm_iommu.c +10
-0+msm_mmu_dma_to_phys (mmu,sg_dma_address (sg)); +size_t size =sg_dma_len (sg); + +if (!phys || !size) { + ret = -EINVAL ; + goto err_unmap; + } + + ret =iommu_map (iommu->domain, iova + mapped, phys, + size, prot,GFP_KERNEL );
^^^^
[Severity: Critical]
Does this also incorrectly assume physical contiguity for the coalesced DMA segment by passing the entire sg_dma_len(sg) to iommu_map() with only the starting physical address?
Quoted 5 lines
drivers/gpu/drm/msm/msm_iommu.c +5
-0+if (ret) + goto err_unmap; + + mapped += size; + }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH v1 2/2] drm/msm: map page-less imported sg_tables from their DMA addresses
In reply to Jianfeng LiuQuoted 24 lines · Jianfeng Liu
With
CONFIG_DMABUF_DEBUG=y,dma_buf_map_attachment()hands importers a copy of the attachmentsg_tablewith the struct page pointers stripped andsg->lengthzeroed; only sg_dma_address()/sg_dma_len() are carried over. msm consumessg->lengthandsg_phys()in both of its map paths:-
msm_iommu_pagetable_map()(userspace managed, per-process GPU pagetables) walks thesg_tablewithsg->lengthandsg_phys()-
msm_iommu_map()(kernel managed mappings: display, and TTBR1 for the GPU), viaiommu_map_sgtable(), which consumessg->lengthandsg_phys()as wellWith a page-stripped
sg_tableboth paths silently map nothing and return success. Userspace then observes arm-smmu translation faults once the GPU first touches the mapping, e.g. during hardware video decode:gpu fault: ttbr0=000000088a889000 iova=000000010741c000 dir=READ type=TRANSLATION source=UCHE
and
__arm_lpae_unmap()WARNs for the never-mapped ranges when the GEM handles are closed (a WARN storm of ~470 traces within a minute of video playback on my x1e78100 laptop).
Quoted 10 lines
For sg entries that still carry a struct page (native objects, and imports without the
DMABUF_DEBUGwrapper) keep usingsg_phys(), so native objects which msm never dma-maps itself (non-MSM_BO_WC) are unaffected. For page-less entries, recover the physical address from the DMA address instead: dmabuf attachments are dma-mapped against the msm drm device, so thedma_addr-> phys lookup can be done withiommu_iova_to_phys()in that device's DMA-API domain, cached per VM in structmsm_mmu::dma_domainatmsm_gem_vm_create()time. If the drm device is direct mapped the DMA address already is a physical address and the lookup degenerates to the identity.
That is not in any way better than illegally using struct page. The point is we need to get away from using phys_addr at all here.
Regards,
Christian.
Quoted 183 lines
Applied on top of "drm/msm/gem: Drop use of pages for imported dma-bufs" [1], which removes the remaining struct page consumers for imported buffers. With both, hardware video decode works with DMABUF_DEBUG=y, tested with clapper and chromium on x1e78100 (Snapdragon X1E78100): zero arm-smmu faults, zero io-pgtable WARNs, correct frames. Without this patch, the same system logs a WARN trace per unmap and falls back to a copy path for video playback.
[1] <[email protected]>
Suggested-by Rob Clark <[email protected]>Cc Rob Clark <[email protected]>Cc Dmitry Baryshkov <[email protected]>Cc Christian König <[email protected]>Signed-off-by Jianfeng Liu <[email protected]>---
drivers/gpu/drm/msm/msm_gem_vma.c | 9 ++++ drivers/gpu/drm/msm/msm_iommu.c | 90 ++++++++++++++++++++++++++++++- drivers/gpu/drm/msm/msm_mmu.h | 13 +++++ 3 files changed, 110 insertions(+), 2 deletions(-)drivers/gpu/drm/msm/msm_gem_vma.c +9
-0@@ -840,6 +840,15 @@ msm_gem_vm_create(struct drm_device *drm, struct msm_mmu *mmu, const char *name, goto err_free_vm; } +/* +* dma-buf imports attach against the msm drm device, and their +* sg_dma_address() lives in that device's DMA-API domain. Keep it +* so the map paths can translate page-less sg_table entries (the +* DMABUF_DEBUG wrapper) back to physical addresses. +*/ +if (device_iommu_mapped (drm->dev)) + mmu->dma_domain =iommu_get_dma_domain (drm->dev); +if (!managed) {struct drm_sched_init_args args = { .ops = &msm_vm_bind_ops,drivers/gpu/drm/msm/msm_iommu.c +88
-2@@ -140,6 +140,24 @@ static int msm_iommu_pagetable_unmap(struct msm_mmu *mmu, u64 iova,return ret; } +/** +* msm_mmu_dma_to_phys() - recover the physical address of a dma address +* +* dma-buf attachments are dma-mapped against the msm drm device, so the +* DMA domain of that device (msm_mmu::dma_domain) holds the mapping. +* For a direct-mapped drm device the DMA address already is a physical +* address. +*/ +static phys_addr_t msm_mmu_dma_to_phys (struct msm_mmu *mmu,dma_addr_t dma_addr) +{ +struct iommu_domain *dma_domain = mmu->dma_domain; + +if (!dma_domain) +return (phys_addr_t )dma_addr; + +return iommu_iova_to_phys (dma_domain, dma_addr); +} +static int msm_iommu_pagetable_map_prr (struct msm_mmu *mmu,u64 iova,size_t len,int prot) {struct msm_iommu_pagetable *pagetable =to_pagetable (mmu);@@ -184,8 +202,35 @@ static int msm_iommu_pagetable_map(struct msm_mmu *mmu, u64 iova,return msm_iommu_pagetable_map_prr (mmu, iova, len, prot);for_each_sgtable_sg (sgt, sg, i) { -size_t size = sg->length; -phys_addr_t phys =sg_phys (sg); +size_t size; +phys_addr_t phys; + +if (sg_page (sg)) { +/* CPU-view entry: native objects, and imported +* sg_tables that still carry struct page +*/ + size = sg->length; + phys =sg_phys (sg); + }else { +/* +* Page-less entry, e.g. the sg_table wrapper +* that dma_buf_map_attachment() hands out when +* CONFIG_DMABUF_DEBUG=y (page pointers stripped, +* sg->length zeroed, only the DMA fields carried +* over). Recover the physical address by +* translating the DMA address through the drm +* device's DMA-API domain. +*/ + size =sg_dma_len (sg); + phys =msm_mmu_dma_to_phys (mmu,sg_dma_address (sg)); + +if (!size || !phys) { +dev_err (mmu->dev, +"cannot map page-less sg entry: dma=%pad len=%zu\n" , + &sg_dma_address (sg), size); +return -EINVAL ; + } + }if (!len)break ;@@ -697,6 +742,47 @@ static int msm_iommu_map(struct msm_mmu *mmu, uint64_t iova,if (iova &BIT_ULL (48 )) iova |=GENMASK_ULL (63 ,49 ); +/* +* With CONFIG_DMABUF_DEBUG=y, imported sg_tables carry no struct +* page and sg->length is zeroed; iommu_map_sgtable() would consume +* zero length and silently map nothing. Map from the (translated) +* DMA addresses instead. +*/ +if (!sg_page (sgt->sgl)) { +struct scatterlist *sg; +size_t mapped =0 ; +unsigned int i; + +for_each_sgtable_dma_sg (sgt, sg, i) { +phys_addr_t phys = +msm_mmu_dma_to_phys (mmu,sg_dma_address (sg)); +size_t size =sg_dma_len (sg); + +if (!phys || !size) { + ret = -EINVAL ; + goto err_unmap; + } + + ret =iommu_map (iommu->domain, iova + mapped, phys, + size, prot,GFP_KERNEL ); +if (ret) + goto err_unmap; + + mapped += size; + } + +if (mapped != len) { + ret = -EINVAL ; + goto err_unmap; + } + +return 0 ; + +err_unmap: +iommu_unmap (iommu->domain, iova, mapped); +return ret; + } + ret =iommu_map_sgtable (iommu->domain, iova, sgt, prot);if (ret <0 )return ret;drivers/gpu/drm/msm/msm_mmu.h +13
-0@@ -64,6 +64,19 @@ struct msm_mmu {* msm_gem_vm::mmu_lock. */ struct msm_mmu_prealloc *prealloc; + +/** +* @dma_domain: DMA-API domain of the msm drm device +* +* dma-buf attachments are dma-mapped against the msm drm device, +* so this domain holds the mapping dma_addr -> phys for imported +* buffers. Used to recover the physical address of sg_table +* entries which carry no struct page (e.g. the page-stripped +* sg_table wrapper that dma_buf_map_attachment() hands out when +* CONFIG_DMABUF_DEBUG=y). NULL if the drm device is direct +* mapped, in which case DMA addresses are physical addresses. +*/ +struct iommu_domain *dma_domain; }; static inline void msm_mmu_init(struct msm_mmu *mmu, struct device *dev,
Re: [PATCH v1 2/2] drm/msm: map page-less imported sg_tables from their DMA addresses
Hi Christian,
Quoted 2 lines · Christian König
That is not in any way better than illegally using struct page. The point is we need to get away from using
phys_addrat all here.
Agreed, I won't push this further. With your "import a shmemfd, not a DMA-buf" remark in mind, the direction that makes sense to me is the allocation model one: userspace allocates the decoder capture buffers from msm GEM and the V4L2 device imports them, so msm stays the exporter and never has to interpret memory it did not allocate. Rob's series (drop the page array, mmap via the exporter) is the prep work for that and is already in good shape. It is also how panfrost/panthor already work - they program their own pagetables from the sg_table's DMA addresses and never look at pages for mapping.
I'll withdraw this series and come back once Rob has had time to look at the msm side. Patch 1 is withdrawn too - Robin NAKed the iommu_get_dma_domain() export, which is moot now anyway.
Thanks for the review, and for the earlier pointer to what msm is doing wrong here.
Regards,
Jianfeng