Removing an unmapped range must not allocate page tables or decrement
the count of an already invalid PTE. Look up existing tables and count
only valid entries being removed. This also protects the unload path
when its old mismatched range did not contain stolen mappings.

Fixes: 8c8f1c958ab5 ("gma500: introduce the GTT and MMU handling logic")
Signed-off-by: Gabriel Gama <[email protected]>
---
diff --git a/drivers/gpu/drm/gma500/mmu.c b/drivers/gpu/drm/gma500/mmu.c
index 
6815b48f00d3b1bacf4a1bb492a5205cdc58e7e8..76c05f5a260c9ca305e7ef9a12ffdc1f61c56482
 100644
--- a/drivers/gpu/drm/gma500/mmu.c
+++ b/drivers/gpu/drm/gma500/mmu.c
@@ -544,18 +544,19 @@
 
        do {
                next = psb_pd_addr_end(addr, end);
-               pt = psb_mmu_pt_alloc_map_lock(pd, addr);
+               pt = psb_mmu_pt_map_lock(pd, addr);
                if (!pt)
-                       goto out;
+                       continue;
                do {
-                       psb_mmu_invalidate_pte(pt, addr);
-                       --pt->count;
+                       if (pt->v[psb_mmu_pt_index(addr)] != pd->invalid_pte) {
+                               psb_mmu_invalidate_pte(pt, addr);
+                               --pt->count;
+                       }
                } while (addr += PAGE_SIZE, addr < next);
                psb_mmu_pt_unmap_unlock(pt);
 
        } while (addr = next, next != end);
 
-out:
        if (pd->hw_context != -1)
                psb_mmu_flush_ptes(pd, f_address, num_pages, 1, 1);
 
@@ -607,8 +608,10 @@
                        if (!pt)
                                continue;
                        do {
-                               psb_mmu_invalidate_pte(pt, addr);
-                               --pt->count;
+                               if (pt->v[psb_mmu_pt_index(addr)] != 
pd->invalid_pte) {
+                                       psb_mmu_invalidate_pte(pt, addr);
+                                       --pt->count;
+                               }
 
                        } while (addr += PAGE_SIZE, addr < next);
                        psb_mmu_pt_unmap_unlock(pt);

Reply via email to