accel/amdxdna: Fix VMA access race
Lizhi Hou
maintainerPATCH v3 1/1 accel/amdxdna: Fix VMA access race
aie2_populate_range() and amdxdna_umap_release() access a saved VMA pointer that may have already been freed, leading to a potential use-after-free.
Remove the VMA accesses from these functions to avoid the race.
---
V3:
fix sashiko comments: error-path cleanup patch race
V2:
fix sashiko comments: Use-after-free on mapp->vma
drivers/accel/amdxdna/aie2_ctx.c | 2 -- drivers/accel/amdxdna/amdxdna_gem.c | 31 +++++++++++++++++++---------- drivers/accel/amdxdna/amdxdna_gem.h | 1 - 3 files changed, 21 insertions(+), 13 deletions(-)
drivers/accel/amdxdna/aie2_ctx.c +0 -2
@@ -1023,8 +1023,6 @@ static int aie2_populate_range(struct amdxdna_gem_obj *abo)kref_get (&mapp->refcnt);up_write (&xdna->notifier_lock); -XDNA_DBG (xdna,"populate memory range %lx %lx" , - mapp->vma->vm_start, mapp->vma->vm_end); mm = mapp->notifier.mm;if (!mmget_not_zero (mm)) {amdxdna_umap_put (mapp);
drivers/accel/amdxdna/amdxdna_gem.c +21 -10
@@ -254,7 +254,7 @@ static bool amdxdna_hmm_invalidate(struct mmu_interval_notifier *mni, xdna =to_xdna_dev (to_gobj (abo)->dev);XDNA_DBG (xdna,"Invalidating range 0x%lx, 0x%lx, type %d" , - mapp->vma->vm_start, mapp->vma->vm_end, abo->type); + mapp->range.start, mapp->range.end, abo->type);if (!mmu_notifier_range_blockable (range))return false;
@@ -284,15 +284,23 @@ static const struct mmu_interval_notifier_ops amdxdna_hmm_ops = { .invalidate = amdxdna_hmm_invalidate, }; +static inline bool compare_range (struct amdxdna_umap *mapp, +struct mm_struct *mm, +unsigned long start,unsigned long end) +{ +return (!mapp->unmapped && mapp->notifier.mm == mm && + mapp->range.start == start && mapp->range.end == end); +} +static void amdxdna_hmm_unregister (struct amdxdna_gem_obj *abo,struct vm_area_struct *vma) {struct amdxdna_dev *xdna =to_xdna_dev (to_gobj (abo)->dev);struct amdxdna_umap *mapp; -down_read (&xdna->notifier_lock); +down_write (&xdna->notifier_lock);list_for_each_entry (mapp, &abo->mem.umap_list, node) { -if (!vma || mapp->vma == vma) { +if (!vma ||compare_range (mapp, vma->vm_mm, vma->vm_start, vma->vm_end)) {if (!mapp->unmapped) {queue_work (xdna->notifier_wq, &mapp->hmm_unreg_work); mapp->unmapped = true;
@@ -301,19 +309,16 @@ static void amdxdna_hmm_unregister(struct amdxdna_gem_obj *abo,break ; } } -up_read (&xdna->notifier_lock); +up_write (&xdna->notifier_lock); }static void amdxdna_umap_release (struct kref *ref) { struct amdxdna_umap *mapp = container_of(ref, struct amdxdna_umap, refcnt);struct amdxdna_gem_obj *abo = mapp->abo; -struct vm_area_struct *vma = mapp->vma;struct amdxdna_dev *xdna;mmu_interval_notifier_remove (&mapp->notifier); -if (is_import_bo (abo) && vma->vm_file && vma->vm_file->f_mapping) -mapping_clear_unevictable (vma->vm_file->f_mapping); xdna =to_xdna_dev (to_gobj (mapp->abo)->dev);down_write (&xdna->notifier_lock);
@@ -355,6 +360,15 @@ static int amdxdna_hmm_register(struct amdxdna_gem_obj *abo,return 0 ; } +down_read (&xdna->notifier_lock); +list_for_each_entry (mapp, &abo->mem.umap_list, node) { +if (compare_range (mapp, current->mm, addr, addr + len)) { +up_read (&xdna->notifier_lock); +return 0 ; + } + } +up_read (&xdna->notifier_lock); + mapp =kzalloc_obj (*mapp);if (!mapp)return -ENOMEM ;
@@ -380,13 +394,10 @@ static int amdxdna_hmm_register(struct amdxdna_gem_obj *abo, mapp->range.start = vma->vm_start; mapp->range.end = vma->vm_end; mapp->range.default_flags =HMM_PFN_REQ_FAULT ; - mapp->vma = vma; mapp->abo = abo;kref_init (&mapp->refcnt);INIT_WORK (&mapp->hmm_unreg_work, amdxdna_hmm_unreg_work); -if (is_import_bo (abo) && vma->vm_file && vma->vm_file->f_mapping) -mapping_set_unevictable (vma->vm_file->f_mapping);down_write (&xdna->notifier_lock);if (list_empty (&abo->mem.umap_list))
drivers/accel/amdxdna/amdxdna_gem.h +0 -1
@@ -12,7 +12,6 @@#include "amdxdna_pci_drv.h" struct amdxdna_umap { -struct vm_area_struct *vma;struct mmu_interval_notifier notifier;struct hmm_range range;struct work_struct hmm_unreg_work;
Signature · 1 line
2.34.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Pre-existing issues:
- [High] Partial unmap or mremap of a buffer drops the MMU interval notifier for the entire mapping, leading to a silent Use-After-Free of physical pages if the remaining mapped pages are later evicted.
- [High] aie2_populate_range() permanently leaks a reference to the process's mm_struct on every successful iteration or retry.
- [High] amdxdna_umap_release() makes a blocking call before removing the object from umap_list, causing a race condition where aie2_populate_range() can acquire a use-after-free reference. --
commit 71483c5a347d768c335b0ee0b6039363e243ad92
Author: Lizhi Hou <[email protected]>
accel/amdxdna: Fix VMA access race
aie2_populate_range() and amdxdna_umap_release() access a saved VMA pointer that may have already been freed, leading to a potential use-after-free.
Remove the VMA accesses from these functions to avoid the race.
drivers/accel/amdxdna/aie2_ctx.c static int aie2_populate_range(struct amdxdna_gem_obj *abo) · line 1025
Quoted 8 lines
drivers/accel/amdxdna/aie2_ctx.c +0
-0@@ -1023,8 +1023,6 @@ static int aie2_populate_range(struct amdxdna_gem_obj *abo)kref_get (&mapp->refcnt);up_write (&xdna->notifier_lock);
[Severity: High]
This isn't a bug introduced by this patch, but can this sequence lead to a use-after-free or double-free?
In amdxdna_umap_release(), the blocking mmu_interval_notifier_remove() is called before the object is removed from the abo->mem.umap_list. If aie2_populate_range() runs concurrently, could it find the dying object in the list and blindly call kref_get(&mapp->refcnt) here after the refcount has already dropped to zero?
drivers/accel/amdxdna/aie2_ctx.c static int aie2_populate_range(struct amdxdna_gem_obj *abo) · line 1028
Quoted 5 lines
drivers/accel/amdxdna/aie2_ctx.c +0
-2-XDNA_DBG (xdna,"populate memory range %lx %lx" , - mapp->vma->vm_start, mapp->vma->vm_end); mm = mapp->notifier.mm;if (!mmget_not_zero (mm)) {amdxdna_umap_put (mapp);
[Severity: High]
This isn't a bug introduced by this patch, but does this code permanently leak a reference to the mm_struct?
It looks like mmget_not_zero(mm) is called here, but on the successful path and retry paths later in the function:
if (ret == -EBUSY) {
amdxdna_umap_put(mapp);
goto again;
}and:
mapp->invalid = false;
up_write(&xdna->notifier_lock);
amdxdna_umap_put(mapp);
goto again;It appears mmput(mm) is missing before jumping back to the again label.
[ ... ]
drivers/accel/amdxdna/amdxdna_gem.c static bool amdxdna_hmm_invalidate(struct mmu_interval_notifier *mni, · line 260
Quoted 13 lines
drivers/accel/amdxdna/amdxdna_gem.c +1
-1@@ -254,7 +254,7 @@ static bool amdxdna_hmm_invalidate(struct mmu_interval_notifier *mni, xdna =to_xdna_dev (to_gobj (abo)->dev);XDNA_DBG (xdna,"Invalidating range 0x%lx, 0x%lx, type %d" , - mapp->vma->vm_start, mapp->vma->vm_end, abo->type); + mapp->range.start, mapp->range.end, abo->type);if (!mmu_notifier_range_blockable (range))return false;
[Severity: High]
This isn't a bug introduced by this patch, but does this invalidate logic safely handle partial unmaps?
When user space partially unmaps a VMA, mmu_notifier_invalidate_range_start() notifies the driver with MMU_NOTIFY_UNMAP. However, further down in this function, hmm_unreg_work is queued unconditionally:
if (range->event == MMU_NOTIFY_UNMAP) {
down_write(&xdna->notifier_lock);
if (!mapp->unmapped) {
queue_work(xdna->notifier_wq, &mapp->hmm_unreg_work);
mapp->unmapped = true;
}Since this destroys the notifier for the entire mapping without verifying if the entire range is being unmapped, could the remainder of the VMA stay mapped in user space without an interval notifier, leading to device hardware performing DMA to stale, freed physical pages?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 6 lines · Lizhi Hou
aie2_populate_range()andamdxdna_umap_release()access a saved VMA pointer that may have already been freed, leading to a potential use-after-free.Remove the VMA accesses from these functions to avoid the race.
Quoted 7 lines
Fixes e486147c912f ("accel/amdxdna: Add BO import and export")Signed-off-by Lizhi Hou <[email protected]>---
V3:
fix sashiko comments: error-path cleanup patch race
V2:fix sashiko comments: Use-after-free on `mapp->vma`
I'm thinking we should make sure that the other existing bugs sashiko raised are fixed first
Quoted 121 lines
drivers/accel/amdxdna/aie2_ctx.c | 2 -- drivers/accel/amdxdna/amdxdna_gem.c | 31 +++++++++++++++++++---------- drivers/accel/amdxdna/amdxdna_gem.h | 1 - 3 files changed, 21 insertions(+), 13 deletions(-)drivers/accel/amdxdna/aie2_ctx.c +0
-2@@ -1023,8 +1023,6 @@ static int aie2_populate_range(struct amdxdna_gem_obj *abo)kref_get (&mapp->refcnt);up_write (&xdna->notifier_lock); -XDNA_DBG (xdna,"populate memory range %lx %lx" , - mapp->vma->vm_start, mapp->vma->vm_end); mm = mapp->notifier.mm;if (!mmget_not_zero (mm)) {amdxdna_umap_put (mapp);drivers/accel/amdxdna/amdxdna_gem.c +21
-10@@ -254,7 +254,7 @@ static bool amdxdna_hmm_invalidate(struct mmu_interval_notifier *mni, xdna =to_xdna_dev (to_gobj (abo)->dev);XDNA_DBG (xdna,"Invalidating range 0x%lx, 0x%lx, type %d" , - mapp->vma->vm_start, mapp->vma->vm_end, abo->type); + mapp->range.start, mapp->range.end, abo->type);if (!mmu_notifier_range_blockable (range))return false;@@ -284,15 +284,23 @@ static const struct mmu_interval_notifier_ops amdxdna_hmm_ops = { .invalidate = amdxdna_hmm_invalidate, }; +static inline bool compare_range (struct amdxdna_umap *mapp, +struct mm_struct *mm, +unsigned long start,unsigned long end) +{ +return (!mapp->unmapped && mapp->notifier.mm == mm && + mapp->range.start == start && mapp->range.end == end); +} +static void amdxdna_hmm_unregister (struct amdxdna_gem_obj *abo,struct vm_area_struct *vma) {struct amdxdna_dev *xdna =to_xdna_dev (to_gobj (abo)->dev);struct amdxdna_umap *mapp; -down_read (&xdna->notifier_lock); +down_write (&xdna->notifier_lock);list_for_each_entry (mapp, &abo->mem.umap_list, node) { -if (!vma || mapp->vma == vma) { +if (!vma ||compare_range (mapp, vma->vm_mm, vma->vm_start, vma->vm_end)) {if (!mapp->unmapped) {queue_work (xdna->notifier_wq, &mapp->hmm_unreg_work); mapp->unmapped = true;@@ -301,19 +309,16 @@ static void amdxdna_hmm_unregister(struct amdxdna_gem_obj *abo,break ; } } -up_read (&xdna->notifier_lock); +up_write (&xdna->notifier_lock); }static void amdxdna_umap_release (struct kref *ref) { struct amdxdna_umap *mapp = container_of(ref, struct amdxdna_umap, refcnt);struct amdxdna_gem_obj *abo = mapp->abo; -struct vm_area_struct *vma = mapp->vma;struct amdxdna_dev *xdna;mmu_interval_notifier_remove (&mapp->notifier); -if (is_import_bo (abo) && vma->vm_file && vma->vm_file->f_mapping) -mapping_clear_unevictable (vma->vm_file->f_mapping); xdna =to_xdna_dev (to_gobj (mapp->abo)->dev);down_write (&xdna->notifier_lock);@@ -355,6 +360,15 @@ static int amdxdna_hmm_register(struct amdxdna_gem_obj *abo,return 0 ; } +down_read (&xdna->notifier_lock); +list_for_each_entry (mapp, &abo->mem.umap_list, node) { +if (compare_range (mapp, current->mm, addr, addr + len)) { +up_read (&xdna->notifier_lock); +return 0 ; + } + } +up_read (&xdna->notifier_lock); + mapp =kzalloc_obj (*mapp);if (!mapp)return -ENOMEM ;@@ -380,13 +394,10 @@ static int amdxdna_hmm_register(struct amdxdna_gem_obj *abo, mapp->range.start = vma->vm_start; mapp->range.end = vma->vm_end; mapp->range.default_flags =HMM_PFN_REQ_FAULT ; - mapp->vma = vma; mapp->abo = abo;kref_init (&mapp->refcnt);INIT_WORK (&mapp->hmm_unreg_work, amdxdna_hmm_unreg_work); -if (is_import_bo (abo) && vma->vm_file && vma->vm_file->f_mapping) -mapping_set_unevictable (vma->vm_file->f_mapping);down_write (&xdna->notifier_lock);if (list_empty (&abo->mem.umap_list))drivers/accel/amdxdna/amdxdna_gem.h +0
-1@@ -12,7 +12,6 @@#include "amdxdna_pci_drv.h" struct amdxdna_umap { -struct vm_area_struct *vma;struct mmu_interval_notifier notifier;struct hmm_range range;struct work_struct hmm_unreg_work;
Lizhi Hou
maintainerQuoted 17 lines · Mario Limonciello
Quoted 6 lines · Lizhi Hou
aie2_populate_range()andamdxdna_umap_release()access a saved VMA pointer that may have already been freed, leading to a potential use-after-free.Remove the VMA accesses from these functions to avoid the race.
Quoted 7 lines
Fixes e486147c912f ("accel/amdxdna: Add BO import and export")Signed-off-by Lizhi Hou <[email protected]>---
V3:
fix sashiko comments: error-path cleanup patch race V2:
fix sashiko comments: Use-after-free onmapp->vma
Quoted 1 line
I'm thinking we should make sure that the other existing bugs sashiko raised are fixed first
I am working on other fixes as well. And they are not related to this issue.
This is an use-after-free and can potentially crash the system in some cases. Fixing it early may also avoid receiving more AI scan reports on this. :)
Thanks,
Lizhi
Quoted 117 lines
Quoted 116 lines
drivers/accel/amdxdna/aie2_ctx.c | 2 --
drivers/accel/amdxdna/amdxdna_gem.c| 31 +++++++++++++++++++----------drivers/accel/amdxdna/amdxdna_gem.h| 1 -3 files changed, 21 insertions(+), 13 deletions(-)drivers/accel/amdxdna/aie2_ctx.c +0
-1@@ -1023,8 +1023,6 @@ static int aie2_populate_range(struct amdxdna_gem_obj *abo) kref_get(&mapp->refcnt); up_write(&xdna->notifier_lock); - XDNA_DBG(xdna, "populate memory range %lx %lx", - mapp->vma->vm_start, mapp->vma->vm_end); mm = mapp->notifier.mm; if (!mmget_not_zero(mm)) {
amdxdna_umap_put(mapp);drivers/accel/amdxdna/amdxdna_gem.c +10
-2@@ -254,7 +254,7 @@ static bool amdxdna_hmm_invalidate(structmmu_interval_notifier *mni, xdna = to_xdna_dev(to_gobj(abo)->dev); XDNA_DBG(xdna, "Invalidating range 0x%lx, 0x%lx, type %d", - mapp->vma->vm_start, mapp->vma->vm_end, abo->type); + mapp->range.start, mapp->range.end, abo->type); if (!mmu_notifier_range_blockable(range)) return false;@@ -284,15 +284,23 @@ static const struct mmu_interval_notifier_ops amdxdna_hmm_ops = { .invalidate = amdxdna_hmm_invalidate, }; +static inline bool compare_range(struct amdxdna_umap *mapp, + struct mm_struct *mm, + unsigned long start, unsigned long end) +{ + return (!mapp->unmapped && mapp->notifier.mm == mm && + mapp->range.start == start && mapp->range.end == end); +} + static void amdxdna_hmm_unregister(struct amdxdna_gem_obj *abo, struct vm_area_struct *vma) { struct amdxdna_dev *xdna = to_xdna_dev(to_gobj(abo)->dev); struct amdxdna_umap *mapp; - down_read(&xdna->notifier_lock); + down_write(&xdna->notifier_lock); list_for_each_entry(mapp, &abo->mem.umap_list, node) { - if (!vma || mapp->vma == vma) { + if (!vma || compare_range(mapp, vma->vm_mm, vma->vm_start, vma->vm_end)) { if (!mapp->unmapped) { queue_work(xdna->notifier_wq, &mapp->hmm_unreg_work);
mapp->unmapped= true;drivers/accel/amdxdna/amdxdna_gem.c +9
-4@@ -301,19 +309,16 @@ static void amdxdna_hmm_unregister(structamdxdna_gem_obj *abo, break; } } - up_read(&xdna->notifier_lock); + up_write(&xdna->notifier_lock); } static void amdxdna_umap_release(struct kref *ref) { struct amdxdna_umap *mapp = container_of(ref, struct amdxdna_umap, refcnt); struct amdxdna_gem_obj *abo = mapp->abo; - struct vm_area_struct *vma = mapp->vma; struct amdxdna_dev *xdna; mmu_interval_notifier_remove(&mapp->notifier); - if (is_import_bo(abo) && vma->vm_file && vma->vm_file->f_mapping) -mapping_clear_unevictable (vma->vm_file->f_mapping); xdna = to_xdna_dev(to_gobj(mapp->abo)->dev); down_write(&xdna->notifier_lock);@@ -355,6 +360,15 @@ static int amdxdna_hmm_register(structamdxdna_gem_obj *abo, return 0; } + down_read(&xdna->notifier_lock); + list_for_each_entry(mapp, &abo->mem.umap_list, node) { + if (compare_range(mapp, current->mm, addr, addr + len)) { + up_read(&xdna->notifier_lock); + return 0; + } + } + up_read(&xdna->notifier_lock); + mapp = kzalloc_obj(*mapp); if (!mapp)return
-ENOMEM;drivers/accel/amdxdna/amdxdna_gem.c +0
-3@@ -380,13 +394,10 @@ static int amdxdna_hmm_register(structamdxdna_gem_obj *abo, mapp->range.start = vma->vm_start; mapp->range.end = vma->vm_end; mapp->range.default_flags = HMM_PFN_REQ_FAULT; - mapp->vma = vma; mapp->abo = abo; kref_init(&mapp->refcnt); INIT_WORK(&mapp->hmm_unreg_work, amdxdna_hmm_unreg_work); - if (is_import_bo(abo) && vma->vm_file && vma->vm_file->f_mapping) - mapping_set_unevictable(vma->vm_file->f_mapping); down_write(&xdna->notifier_lock); if (list_empty(&abo->mem.umap_list))drivers/accel/amdxdna/amdxdna_gem.h +0
-1@@ -12,7 +12,6 @@ #include "amdxdna_pci_drv.h" struct amdxdna_umap { - struct vm_area_struct *vma; struct mmu_interval_notifier notifier; struct hmm_range range; struct work_struct hmm_unreg_work;
Quoted 8 lines · Lizhi Hou
aie2_populate_range()andamdxdna_umap_release()access a saved VMA pointer that may have already been freed, leading to a potential use-after-free.Remove the VMA accesses from these functions to avoid the race.
Fixes e486147c912f ("accel/amdxdna: Add BO import and export")Signed-off-by Lizhi Hou <[email protected]>
Quoted 126 lines
---
V3:
fix sashiko comments: error-path cleanup patch race
V2:fix sashiko comments: Use-after-free on `mapp->vma`drivers/accel/amdxdna/aie2_ctx.c | 2 -- drivers/accel/amdxdna/amdxdna_gem.c | 31 +++++++++++++++++++---------- drivers/accel/amdxdna/amdxdna_gem.h | 1 - 3 files changed, 21 insertions(+), 13 deletions(-)drivers/accel/amdxdna/aie2_ctx.c +0
-2@@ -1023,8 +1023,6 @@ static int aie2_populate_range(struct amdxdna_gem_obj *abo)kref_get (&mapp->refcnt);up_write (&xdna->notifier_lock); -XDNA_DBG (xdna,"populate memory range %lx %lx" , - mapp->vma->vm_start, mapp->vma->vm_end); mm = mapp->notifier.mm;if (!mmget_not_zero (mm)) {amdxdna_umap_put (mapp);drivers/accel/amdxdna/amdxdna_gem.c +21
-10@@ -254,7 +254,7 @@ static bool amdxdna_hmm_invalidate(struct mmu_interval_notifier *mni, xdna =to_xdna_dev (to_gobj (abo)->dev);XDNA_DBG (xdna,"Invalidating range 0x%lx, 0x%lx, type %d" , - mapp->vma->vm_start, mapp->vma->vm_end, abo->type); + mapp->range.start, mapp->range.end, abo->type);if (!mmu_notifier_range_blockable (range))return false;@@ -284,15 +284,23 @@ static const struct mmu_interval_notifier_ops amdxdna_hmm_ops = { .invalidate = amdxdna_hmm_invalidate, }; +static inline bool compare_range (struct amdxdna_umap *mapp, +struct mm_struct *mm, +unsigned long start,unsigned long end) +{ +return (!mapp->unmapped && mapp->notifier.mm == mm && + mapp->range.start == start && mapp->range.end == end); +} +static void amdxdna_hmm_unregister (struct amdxdna_gem_obj *abo,struct vm_area_struct *vma) {struct amdxdna_dev *xdna =to_xdna_dev (to_gobj (abo)->dev);struct amdxdna_umap *mapp; -down_read (&xdna->notifier_lock); +down_write (&xdna->notifier_lock);list_for_each_entry (mapp, &abo->mem.umap_list, node) { -if (!vma || mapp->vma == vma) { +if (!vma ||compare_range (mapp, vma->vm_mm, vma->vm_start, vma->vm_end)) {if (!mapp->unmapped) {queue_work (xdna->notifier_wq, &mapp->hmm_unreg_work); mapp->unmapped = true;@@ -301,19 +309,16 @@ static void amdxdna_hmm_unregister(struct amdxdna_gem_obj *abo,break ; } } -up_read (&xdna->notifier_lock); +up_write (&xdna->notifier_lock); }static void amdxdna_umap_release (struct kref *ref) { struct amdxdna_umap *mapp = container_of(ref, struct amdxdna_umap, refcnt);struct amdxdna_gem_obj *abo = mapp->abo; -struct vm_area_struct *vma = mapp->vma;struct amdxdna_dev *xdna;mmu_interval_notifier_remove (&mapp->notifier); -if (is_import_bo (abo) && vma->vm_file && vma->vm_file->f_mapping) -mapping_clear_unevictable (vma->vm_file->f_mapping); xdna =to_xdna_dev (to_gobj (mapp->abo)->dev);down_write (&xdna->notifier_lock);@@ -355,6 +360,15 @@ static int amdxdna_hmm_register(struct amdxdna_gem_obj *abo,return 0 ; } +down_read (&xdna->notifier_lock); +list_for_each_entry (mapp, &abo->mem.umap_list, node) { +if (compare_range (mapp, current->mm, addr, addr + len)) { +up_read (&xdna->notifier_lock); +return 0 ; + } + } +up_read (&xdna->notifier_lock); + mapp =kzalloc_obj (*mapp);if (!mapp)return -ENOMEM ;@@ -380,13 +394,10 @@ static int amdxdna_hmm_register(struct amdxdna_gem_obj *abo, mapp->range.start = vma->vm_start; mapp->range.end = vma->vm_end; mapp->range.default_flags =HMM_PFN_REQ_FAULT ; - mapp->vma = vma; mapp->abo = abo;kref_init (&mapp->refcnt);INIT_WORK (&mapp->hmm_unreg_work, amdxdna_hmm_unreg_work); -if (is_import_bo (abo) && vma->vm_file && vma->vm_file->f_mapping) -mapping_set_unevictable (vma->vm_file->f_mapping);down_write (&xdna->notifier_lock);if (list_empty (&abo->mem.umap_list))drivers/accel/amdxdna/amdxdna_gem.h +0
-1@@ -12,7 +12,6 @@#include "amdxdna_pci_drv.h" struct amdxdna_umap { -struct vm_area_struct *vma;struct mmu_interval_notifier notifier;struct hmm_range range;struct work_struct hmm_unreg_work;
Applied to drm-misc-fixes
Quoted 134 lines · Max Zhen
Quoted 8 lines · Lizhi Hou
aie2_populate_range()andamdxdna_umap_release()access a saved VMA pointer that may have already been freed, leading to a potential use-after-free.Remove the VMA accesses from these functions to avoid the race.
Fixes e486147c912f ("accel/amdxdna: Add BO import and export")Signed-off-by Lizhi Hou <[email protected]>Reviewed-by Max Zhen <[email protected]>Quoted 121 lines
---
V3:
fix sashiko comments: error-path cleanup patch race V2:
fix sashiko comments: Use-after-free onmapp->vmadrivers/accel/amdxdna/aie2_ctx.c | 2 --
drivers/accel/amdxdna/amdxdna_gem.c| 31 +++++++++++++++++++----------drivers/accel/amdxdna/amdxdna_gem.h| 1 -3 files changed, 21 insertions(+), 13 deletions(-)drivers/accel/amdxdna/aie2_ctx.c +0
-1@@ -1023,8 +1023,6 @@ static int aie2_populate_range(struct amdxdna_gem_obj *abo) kref_get(&mapp->refcnt); up_write(&xdna->notifier_lock); - XDNA_DBG(xdna, "populate memory range %lx %lx", - mapp->vma->vm_start, mapp->vma->vm_end); mm = mapp->notifier.mm; if (!mmget_not_zero(mm)) {
amdxdna_umap_put(mapp);drivers/accel/amdxdna/amdxdna_gem.c +10
-2@@ -254,7 +254,7 @@ static bool amdxdna_hmm_invalidate(structmmu_interval_notifier *mni, xdna = to_xdna_dev(to_gobj(abo)->dev); XDNA_DBG(xdna, "Invalidating range 0x%lx, 0x%lx, type %d", - mapp->vma->vm_start, mapp->vma->vm_end, abo->type); + mapp->range.start, mapp->range.end, abo->type); if (!mmu_notifier_range_blockable(range)) return false;@@ -284,15 +284,23 @@ static const struct mmu_interval_notifier_ops amdxdna_hmm_ops = { .invalidate = amdxdna_hmm_invalidate, }; +static inline bool compare_range(struct amdxdna_umap *mapp, + struct mm_struct *mm, + unsigned long start, unsigned long end) +{ + return (!mapp->unmapped && mapp->notifier.mm == mm && + mapp->range.start == start && mapp->range.end == end); +} + static void amdxdna_hmm_unregister(struct amdxdna_gem_obj *abo, struct vm_area_struct *vma) { struct amdxdna_dev *xdna = to_xdna_dev(to_gobj(abo)->dev); struct amdxdna_umap *mapp; - down_read(&xdna->notifier_lock); + down_write(&xdna->notifier_lock); list_for_each_entry(mapp, &abo->mem.umap_list, node) { - if (!vma || mapp->vma == vma) { + if (!vma || compare_range(mapp, vma->vm_mm, vma->vm_start, vma->vm_end)) { if (!mapp->unmapped) { queue_work(xdna->notifier_wq, &mapp->hmm_unreg_work);
mapp->unmapped= true;drivers/accel/amdxdna/amdxdna_gem.c +9
-4@@ -301,19 +309,16 @@ static void amdxdna_hmm_unregister(structamdxdna_gem_obj *abo, break; } } - up_read(&xdna->notifier_lock); + up_write(&xdna->notifier_lock); } static void amdxdna_umap_release(struct kref *ref) { struct amdxdna_umap *mapp = container_of(ref, struct amdxdna_umap, refcnt); struct amdxdna_gem_obj *abo = mapp->abo; - struct vm_area_struct *vma = mapp->vma; struct amdxdna_dev *xdna; mmu_interval_notifier_remove(&mapp->notifier); - if (is_import_bo(abo) && vma->vm_file && vma->vm_file->f_mapping) -mapping_clear_unevictable (vma->vm_file->f_mapping); xdna = to_xdna_dev(to_gobj(mapp->abo)->dev); down_write(&xdna->notifier_lock);@@ -355,6 +360,15 @@ static int amdxdna_hmm_register(structamdxdna_gem_obj *abo, return 0; } + down_read(&xdna->notifier_lock); + list_for_each_entry(mapp, &abo->mem.umap_list, node) { + if (compare_range(mapp, current->mm, addr, addr + len)) { + up_read(&xdna->notifier_lock); + return 0; + } + } + up_read(&xdna->notifier_lock); + mapp = kzalloc_obj(*mapp); if (!mapp)return
-ENOMEM;drivers/accel/amdxdna/amdxdna_gem.c +0
-3@@ -380,13 +394,10 @@ static int amdxdna_hmm_register(structamdxdna_gem_obj *abo, mapp->range.start = vma->vm_start; mapp->range.end = vma->vm_end; mapp->range.default_flags = HMM_PFN_REQ_FAULT; - mapp->vma = vma; mapp->abo = abo; kref_init(&mapp->refcnt); INIT_WORK(&mapp->hmm_unreg_work, amdxdna_hmm_unreg_work); - if (is_import_bo(abo) && vma->vm_file && vma->vm_file->f_mapping) - mapping_set_unevictable(vma->vm_file->f_mapping); down_write(&xdna->notifier_lock); if (list_empty(&abo->mem.umap_list))drivers/accel/amdxdna/amdxdna_gem.h +0
-1@@ -12,7 +12,6 @@ #include "amdxdna_pci_drv.h" struct amdxdna_umap { - struct vm_area_struct *vma; struct mmu_interval_notifier notifier; struct hmm_range range; struct work_struct hmm_unreg_work;