drm/i915/gvt: Don't replace a DMA mapping that is still in use
PATCH 1/1 drm/i915/gvt: Don't replace a DMA mapping that is still in use
intel_gvt_dma_map_guest_page() unmaps and frees the cache entry of a gfn when it is requested with a different size, and maps the page again. The entry is still referenced: shadow GTT entries and dma-bufs keep using its DMA address after the page is unpinned and the DMA mapping is torn down. When the new mapping gets the same DMA address, their later intel_gvt_dma_unmap_guest_page() calls also drop the references of the new entry.
Keep the existing entry instead. A 2M mapping covers a 4K request for its first page, so take a reference on it. If the entry is smaller than the request, fail and let the caller split the 2M entry into 4K pages, as it already does when a 2M mapping cannot be set up.
---
Compile-tested only (x86_64 defconfig + DRM_I915_GVT_KVMGT, W=1, sparse).
drivers/gpu/drm/i915/gvt/kvmgt.c | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-)
drivers/gpu/drm/i915/gvt/kvmgt.c +8 -12
@@ -1626,19 +1626,15 @@ int intel_gvt_dma_map_guest_page(struct intel_vgpu *vgpu, unsigned long gfn, ret =__gvt_cache_add (vgpu, gfn, *dma_addr, size);if (ret) goto err_unmap; - }else if (entry->size != size) { -/* the same gfn with different size: unmap and re-map */ -gvt_dma_unmap_page (vgpu, gfn, entry->dma_addr, entry->size); -__gvt_cache_remove_entry (vgpu, entry); - - ret =gvt_dma_map_page (vgpu, gfn, dma_addr, size); -if (ret) - goto err_unlock; - - ret =__gvt_cache_add (vgpu, gfn, *dma_addr, size); -if (ret) - goto err_unmap; + }else if (entry->size < size) { +/* +* The smaller mapping may still be in use, so don't replace +* it. Fail and let the caller map the range in smaller pages. +*/ + ret = -EBUSY ; + goto err_unlock; }else { +/* A mapping of the same or a larger size covers the request */ kref_get (&entry->ref); *dma_addr = entry->dma_addr; }
base-commit: 7704c4c5bb127673b4f0ead839919db573559e38
Signature · 1 line
2.43.0
Patchwork
CI✗ LGCI.VerificationFailed: failure for drm/i915/gvt: Don't replace a DMA mapping that is still in use
== Series Details ==
== Series Details ==
Series: drm/i915/gvt: Don't replace a DMA mapping that is still in use
URL : https://patchwork.freedesktop.org/series/175582/
State : failure
== Summary ==
Series author address '[email protected]' is not on the allowlist, which prevents CI from being automatically triggered. If you want CI to run for this series, ask Patchwork project owners to click 'retest' on the series in Patchwork. Exception occurred during validation, bailing out!
Build URL: http://gfx-ci.igk.intel.com:8080/job/CI_PW_kernel/189633/ (on built-in)
Jani Nikula
maintainerQuoted 13 lines · Yuho Choi
intel_gvt_dma_map_guest_page()unmaps and frees the cache entry of a gfn when it is requested with a different size, and maps the page again. The entry is still referenced: shadow GTT entries and dma-bufs keep using its DMA address after the page is unpinned and the DMA mapping is torn down. When the new mapping gets the same DMA address, their laterintel_gvt_dma_unmap_guest_page()calls also drop the references of the new entry.Keep the existing entry instead. A 2M mapping covers a 4K request for its first page, so take a reference on it. If the entry is smaller than the request, fail and let the caller split the 2M entry into 4K pages, as it already does when a 2M mapping cannot be set up.
Quoted 5 lines
Fixes 7366aeb77cd8 ("drm/i915/gvt: fix incorrect cache entry for guest page mapping")Signed-off-by Yuho Choi <[email protected]>---
Compile-tested only (x86_64defconfig +DRM_I915_GVT_KVMGT, W=1, sparse).
IOW, you're not hitting the issue yourself? How did you find the issue? Did you use LLM to write the patch?
BR,
Jani.
Quoted 38 lines
drivers/gpu/drm/i915/gvt/kvmgt.c | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-)drivers/gpu/drm/i915/gvt/kvmgt.c +8
-12@@ -1626,19 +1626,15 @@ int intel_gvt_dma_map_guest_page(struct intel_vgpu *vgpu, unsigned long gfn, ret =__gvt_cache_add (vgpu, gfn, *dma_addr, size);if (ret) goto err_unmap; - }else if (entry->size != size) { -/* the same gfn with different size: unmap and re-map */ -gvt_dma_unmap_page (vgpu, gfn, entry->dma_addr, entry->size); -__gvt_cache_remove_entry (vgpu, entry); - - ret =gvt_dma_map_page (vgpu, gfn, dma_addr, size); -if (ret) - goto err_unlock; - - ret =__gvt_cache_add (vgpu, gfn, *dma_addr, size); -if (ret) - goto err_unmap; + }else if (entry->size < size) { +/* +* The smaller mapping may still be in use, so don't replace +* it. Fail and let the caller map the range in smaller pages. +*/ + ret = -EBUSY ; + goto err_unlock; }else { +/* A mapping of the same or a larger size covers the request */ kref_get (&entry->ref); *dma_addr = entry->dma_addr; }base-commit:
7704c4c5bb127673b4f0ead839919db573559e38
Signature · 1 line
Jani Nikula, Intel
Thank you for reviewing.
Quoted 3 lines · Jani Nikula
IOW, you're not hitting the issue yourself? How did you find the issue? Did you use LLM to write the patch?
As part of security research, I am reviewing how virtualization code manages the lifetime of resources it shares with guests and devices.
Quoted 1 line
Did you use LLM to write the patch?
While preparing this patch, I asked an AI assistant questions to confirm my understanding of how the driver works. So technically yes, an LLM was used in making this patch. I should have disclosed this in the submission; v2 will carry it.
Zhenyu Wang
reviewerQuoted 5 lines · Yuho Choi
intel_gvt_dma_map_guest_page()unmaps and frees the cache entry of a gfn when it is requested with a different size, and maps the page again. The entry is still referenced: shadow GTT entries and dma-bufs keep using its DMA address after the page is unpinned and the DMA mapping is torn down.
I don't think this is *real*, as code would simply destroy entry in that case, and there won't be on-the-fly dma operation on that, as guest mem setup is totally before execlist submission, if guest driver changed mapping by ignoring gvt emulated execlist status, that's just bad...
Quoted 9 lines
When the new mapping gets the same DMA address, their later
intel_gvt_dma_unmap_guest_page()calls also drop the references of the new entry.Keep the existing entry instead. A 2M mapping covers a 4K request for its first page, so take a reference on it. If the entry is smaller than the request, fail and let the caller split the 2M entry into 4K pages, as it already does when a 2M mapping cannot be set up.
So this change would always force 2M to split into 4K? why?
Quoted 45 lines
Fixes 7366aeb77cd8 ("drm/i915/gvt: fix incorrect cache entry for guest page mapping")Signed-off-by Yuho Choi <[email protected]>---
Compile-tested only (x86_64defconfig +DRM_I915_GVT_KVMGT, W=1, sparse).drivers/gpu/drm/i915/gvt/kvmgt.c | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-)drivers/gpu/drm/i915/gvt/kvmgt.c +8
-12@@ -1626,19 +1626,15 @@ int intel_gvt_dma_map_guest_page(struct intel_vgpu *vgpu, unsigned long gfn, ret =__gvt_cache_add (vgpu, gfn, *dma_addr, size);if (ret) goto err_unmap; - }else if (entry->size != size) { -/* the same gfn with different size: unmap and re-map */ -gvt_dma_unmap_page (vgpu, gfn, entry->dma_addr, entry->size); -__gvt_cache_remove_entry (vgpu, entry); - - ret =gvt_dma_map_page (vgpu, gfn, dma_addr, size); -if (ret) - goto err_unlock; - - ret =__gvt_cache_add (vgpu, gfn, *dma_addr, size); -if (ret) - goto err_unmap; + }else if (entry->size < size) { +/* +* The smaller mapping may still be in use, so don't replace +* it. Fail and let the caller map the range in smaller pages. +*/ + ret = -EBUSY ; + goto err_unlock; }else { +/* A mapping of the same or a larger size covers the request */ kref_get (&entry->ref); *dma_addr = entry->dma_addr; }base-commit:
7704c4c5bb127673b4f0ead839919db573559e38Signature · 1 line
2.43.0
Thanks for the review!
Quoted 5 lines · Zhenyu Wang
I don't think this is *real*, as code would simply destroy entry in that case, and there won't be on-the-fly dma operation on that, as guest mem setup is totally before execlist submission, if guest driver changed mapping by ignoring gvt emulated execlist status, that's just bad...
You know this code much better than I do, and I can't test it without GVT-g hardware, so I may well be missing something.
The case I had in mind is the guest mapping the same page at both sizes and keeping both, e.g. 2M in the PPGTT of one context and 4K in the GGTT or in another context's PPGTT. If the second request replaces the entry, the first shadow entry seems to keep the old DMA address, as shadow_ppgtt_mm() reuses a PPGTT that is already shadowed. Is that ruled out somewhere? Please let me know if so.
Quoted 1 line
So this change would always force 2M to split into 4K? why?
No, only when a 4K entry for the same gfn is cached at the time of the 2M request. With no entry or a 2M entry nothing changes, and a 4K request now reuses an existing 2M entry instead of replacing it.
Zhenyu Wang
reviewerQuoted 11 lines · Yuho Choi
Thanks for the review!
Quoted 5 lines · Zhenyu Wang
I don't think this is *real*, as code would simply destroy entry in that case, and there won't be on-the-fly dma operation on that, as guest mem setup is totally before execlist submission, if guest driver changed mapping by ignoring gvt emulated execlist status, that's just bad...
You know this code much better than I do, and I can't test it without GVT-g hardware, so I may well be missing something.
I can understand that but I think for any non-trivial change or for sensitive part, testing is a must...
Quoted 7 lines
The case I had in mind is the guest mapping the same page at both sizes and keeping both, e.g. 2M in the PPGTT of one context and 4K in the GGTT or in another context's PPGTT. If the second request replaces the entry, the first shadow entry seems to keep the old DMA address, as
shadow_ppgtt_mm()reuses a PPGTT that is already shadowed. Is that ruled out somewhere? Please let me know if so.
Shadow ppgtt is created by a PV interface, so each context would have separate shadow ppgtt or shadow mm. GVT handles guest page's dma mapping per-vgpu as for per-VM. But GVT handles context submission in order, shadow ppgtt is populated for each submission. I think even in your example that possible one page mapped in different context as 4K or 2M, it would be remapped as size doesn't match for each submission, so no invalide dma address access.
I doubt if i915 could possibly have such case in reality..
Quoted 6 lines
Quoted 2 lines
So this change would always force 2M to split into 4K? why?
No, only when a 4K entry for the same gfn is cached at the time of the 2M request. With no entry or a 2M entry nothing changes, and a 4K request now reuses an existing 2M entry instead of replacing it.
Quoted 6 lines · Zhenyu Wang
Quoted 3 lines
You know this code much better than I do, and I can't test it without GVT-g hardware, so I may well be missing something.
I can understand that but I think for any non-trivial change or for sensitive part, testing is a must...
Understood. I'll hold this patch until I can test it on GVT-g hardware.
Quoted 15 lines
Quoted 6 lines
The case I had in mind is the guest mapping the same page at both sizes and keeping both, e.g. 2M in the PPGTT of one context and 4K in the GGTT or in another context's PPGTT. If the second request replaces the entry, the first shadow entry seems to keep the old DMA address, as
shadow_ppgtt_mm()reuses a PPGTT that is already shadowed. Is that ruled out somewhere? Please let me know if so.Shadow ppgtt is created by a PV interface, so each context would have separate shadow ppgtt or shadow mm. GVT handles guest page's dma mapping per-vgpu as for per-VM. But GVT handles context submission in order, shadow ppgtt is populated for each submission. I think even in your example that possible one page mapped in different context as 4K or 2M, it would be remapped as size doesn't match for each submission, so no invalide dma address access.
Quoted 1 line
I doubt if i915 could possibly have such case in reality..
It does seem to happen: 7366aeb77cd8 was written after the same gfn was requested at both 4K and 2M during an OpenCL conformance test.
Zhenyu Wang
reviewerQuoted 25 lines · Yuho Choi
Quoted 6 lines · Zhenyu Wang
Quoted 3 lines
You know this code much better than I do, and I can't test it without GVT-g hardware, so I may well be missing something.
I can understand that but I think for any non-trivial change or for sensitive part, testing is a must...
Understood. I'll hold this patch until I can test it on GVT-g hardware.
Quoted 15 lines
Quoted 6 lines
The case I had in mind is the guest mapping the same page at both sizes and keeping both, e.g. 2M in the PPGTT of one context and 4K in the GGTT or in another context's PPGTT. If the second request replaces the entry, the first shadow entry seems to keep the old DMA address, as
shadow_ppgtt_mm()reuses a PPGTT that is already shadowed. Is that ruled out somewhere? Please let me know if so.Shadow ppgtt is created by a PV interface, so each context would have separate shadow ppgtt or shadow mm. GVT handles guest page's dma mapping per-vgpu as for per-VM. But GVT handles context submission in order, shadow ppgtt is populated for each submission. I think even in your example that possible one page mapped in different context as 4K or 2M, it would be remapped as size doesn't match for each submission, so no invalide dma address access.
Quoted 4 lines
Quoted 1 line
I doubt if i915 could possibly have such case in reality..
It does seem to happen:
7366aeb77cd8was written after the same gfn was requested at both 4K and 2M during an OpenCL conformance test.
ok..so that was actually a fix to remap guest page when populate shadow ppgtt, it's a little hacky but as for gvt procedure, it works. Your change might also work but have a penalty to an extra 2M split with extra dma mapping effort..