]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
drm/gpusvm: publish dpagemap early to avoid device mapping leak on error
authorHonglei Huang <honghuan@amd.com>
Wed, 1 Jul 2026 06:28:00 +0000 (14:28 +0800)
committerMatthew Brost <matthew.brost@intel.com>
Tue, 14 Jul 2026 16:27:32 +0000 (09:27 -0700)
drm_gpusvm_get_pages() only stored the local dpagemap into
svm_pages->dpagemap on the success path. If a later page failed (e.g.
-EOPNOTSUPP when ctx->allow_mixed is false) and jumped to err_unmap,
svm_pages->dpagemap was still NULL, so __drm_gpusvm_unmap_pages() skipped
device_unmap() and leaked the device mappings already created.

Assign svm_pages->dpagemap when the first device page is mapped so the
err_unmap path can device_unmap() those mappings.

This issue was found by Sashiko AI review.

Fixes: f70da6f99d4f ("drm/gpusvm: pull out drm_gpusvm_pages substructure")
Cc: stable@vger.kernel.org
Reviewed-by: Matthew Brost <matthew.brost@intel.com>
Signed-off-by: Honglei Huang <honghuan@amd.com>
Signed-off-by: Matthew Brost <matthew.brost@intel.com>
Link: https://patch.msgid.link/20260701062800.409248-4-honghuan@amd.com
drivers/gpu/drm/drm_gpusvm.c

index 44bb19658dd6b6b252ad38151416aa08d22e7219..9a06ff7d2608e6f40def924ce8255def498a3162 100644 (file)
@@ -1544,6 +1544,16 @@ map_pages:
                                        err = -EAGAIN;
                                        goto err_unmap;
                                }
+
+                               /*
+                                * Set the dpagemap as soon as the first
+                                * device page is mapped so the err_unmap path
+                                * can device_unmap() the device mappings that
+                                * have already been created.
+                                */
+                               drm_pagemap_get(dpagemap);
+                               drm_pagemap_put(svm_pages->dpagemap);
+                               svm_pages->dpagemap = dpagemap;
                        }
                        svm_pages->dma_addr[j] =
                                dpagemap->ops->device_map(dpagemap,
@@ -1611,12 +1621,8 @@ map_pages:
                        goto err_unmap;
        }
 
-       if (pagemap) {
+       if (pagemap)
                flags.has_devmem_pages = true;
-               drm_pagemap_get(dpagemap);
-               drm_pagemap_put(svm_pages->dpagemap);
-               svm_pages->dpagemap = dpagemap;
-       }
 
        /* WRITE_ONCE pairs with READ_ONCE for opportunistic checks */
        WRITE_ONCE(svm_pages->flags.__flags, flags.__flags);