drm/amdgpu: fix amdgpu_hmm_range_get_pages
Subsystems (2)
PATCH 1/4 drm/amdgpu: fix amdgpu_hmm_range_get_pages
The notifier sequence must only be read once or otherwise we could work with invalid pages.
While at it also fix the coding style, e.g. drop the pre-initialized return value and use the common define for 2G range.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +8 -8
@@ -51,8 +51,6 @@#include "amdgpu_amdkfd.h" #include "amdgpu_hmm.h" -#define MAX_WALK_BYTE (2UL << 30) -/** * amdgpu_hmm_invalidate_gfx - callback to notify about mm change *
@@ -170,11 +168,13 @@ int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier,void *owner, struct amdgpu_hmm_range *range) { - unsigned long end; + const u64 max_bytes = SZ_2G; + +struct hmm_range *hmm_range = &range->hmm_range;unsigned long timeout;unsigned long *pfns; -int r =0 ; -struct hmm_range *hmm_range = &range->hmm_range; +unsigned long end; +int r; pfns =kvmalloc_array (npages,sizeof (*pfns),GFP_KERNEL );if (unlikely (!pfns)) {
@@ -191,8 +191,9 @@ int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier, end = start + npages *PAGE_SIZE ; hmm_range->dev_private_owner = owner; + hmm_range->notifier_seq =mmu_interval_read_begin (notifier); do { - hmm_range->end = min(hmm_range->start + MAX_WALK_BYTE, end); + hmm_range->end = min(hmm_range->start + max_bytes, end); pr_debug("hmm range: start = 0x%lx, end = 0x%lx", hmm_range->start, hmm_range->end);
@@ -200,7 +201,6 @@ int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier, timeout = jiffies +msecs_to_jiffies (HMM_RANGE_DEFAULT_TIMEOUT ); retry: - hmm_range->notifier_seq =mmu_interval_read_begin (notifier); r =hmm_range_fault (hmm_range);if (unlikely (r)) {if (r == -EBUSY && !time_after (jiffies, timeout))
@@ -210,7 +210,7 @@ int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier,if (hmm_range->end == end)break ; - hmm_range->hmm_pfns +=MAX_WALK_BYTE >>PAGE_SHIFT ; + hmm_range->hmm_pfns += max_bytes >>PAGE_SHIFT ; hmm_range->start = hmm_range->end; }while (hmm_range->end < end);
Signature · 1 line
2.43.0
PATCH 2/4 drm/amdgpu: fix calling VM invalidation in amdgpu_hmm_invalidate_gfx
Otherwise we don't invalidate page tables on next CS.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c | 1 + drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +1 -0
@@ -76,6 +76,7 @@ static bool amdgpu_hmm_invalidate_gfx(struct mmu_interval_notifier *mni,mmu_interval_set_seq (mni, cur_seq); +amdgpu_vm_bo_invalidate (bo, false); r =dma_resv_wait_timeout (bo->tbo.base.resv,DMA_RESV_USAGE_BOOKKEEP , false,MAX_SCHEDULE_TIMEOUT );mutex_unlock (&adev->notifier_lock);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +5 -2
@@ -1631,6 +1631,7 @@ int amdgpu_vm_handle_moved(struct amdgpu_device *adev, {struct amdgpu_bo_va *bo_va;struct dma_resv *resv; +struct amdgpu_bo *bo;bool clear, unlock;int r;
@@ -1650,11 +1651,13 @@ int amdgpu_vm_handle_moved(struct amdgpu_device *adev,while (!list_empty (&vm->invalidated)) { bo_va = list_first_entry(&vm->invalidated, struct amdgpu_bo_va, base.vm_status); - resv = bo_va->base.bo->tbo.base.resv; + bo = bo_va->base.bo; + resv = bo->tbo.base.resv;spin_unlock (&vm->status_lock);/* Try to reserve the BO to avoid clearing its ptes */ -if (!adev->debug_vm &&dma_resv_trylock (resv)) { +if (!adev->debug_vm && !amdgpu_ttm_tt_get_usermm (bo->tbo.ttm) && +dma_resv_trylock (resv)) { clear = false; unlock = true;/* The caller is already holding the reservation lock */
Signature · 1 line
2.43.0
PATCH 3/4 drm/amdgpu: fix waiting for all submissions for userptrs
In reply to Christian KönigWait for all submissions when userptrs need to be invalidated by the MMU notifier, not just the one the userptr was involved into.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +4 -2
@@ -67,6 +67,7 @@ static bool amdgpu_hmm_invalidate_gfx(struct mmu_interval_notifier *mni, { struct amdgpu_bo *bo = container_of(mni, struct amdgpu_bo, notifier);struct amdgpu_device *adev =amdgpu_ttm_adev (bo->tbo.bdev); +struct amdgpu_bo *vm_root = bo->vm_bo->vm->root.bo;long r;if (!mmu_notifier_range_blockable (range))
@@ -77,8 +78,9 @@ static bool amdgpu_hmm_invalidate_gfx(struct mmu_interval_notifier *mni,mmu_interval_set_seq (mni, cur_seq);amdgpu_vm_bo_invalidate (bo, false); - r =dma_resv_wait_timeout (bo->tbo.base.resv,DMA_RESV_USAGE_BOOKKEEP , - false,MAX_SCHEDULE_TIMEOUT ); + r =dma_resv_wait_timeout (vm_root->tbo.base.resv, +DMA_RESV_USAGE_BOOKKEEP , false, +MAX_SCHEDULE_TIMEOUT );mutex_unlock (&adev->notifier_lock);if (r <=0 )DRM_ERROR ("(%ld) failed to wait for user bo\n" , r);
Signature · 1 line
2.43.0
PATCH 4/4 drm/amdgpu: restart the CS if some parts of the VM are still invalidated
In reply to Christian KönigMake sure that we only submit work with full up to date VM page tables.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +3 -1
@@ -1280,6 +1280,7 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p, {struct amdgpu_fpriv *fpriv = p->filp->driver_priv;struct amdgpu_job *leader = p->gang_leader; +struct amdgpu_vm *vm = &fpriv->vm;struct amdgpu_bo_list_entry *e;struct drm_gem_object *gobj;unsigned long index;
@@ -1325,7 +1326,8 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,amdgpu_hmm_range_free (e->range); e->range =NULL ; } -if (r) { + +if (r || !list_empty (&vm->invalidated)) { r = -EAGAIN ;mutex_unlock (&p->adev->notifier_lock);return r;
Signature · 1 line
2.43.0
Re: [PATCH 4/4] drm/amdgpu: restart the CS if some parts of the VM are still invalidated
Series is:
Quoted 33 lines · Christian König
Make sure that we only submit work with full up to date VM page tables.
Signed-off-by Christian König <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +3
-2@@ -1280,6 +1280,7 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p, {struct amdgpu_fpriv *fpriv = p->filp->driver_priv;struct amdgpu_job *leader = p->gang_leader; +struct amdgpu_vm *vm = &fpriv->vm;struct amdgpu_bo_list_entry *e;struct drm_gem_object *gobj;unsigned long index;@@ -1325,7 +1326,8 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,amdgpu_hmm_range_free (e->range); e->range =NULL ; } -if (r) { + +if (r || !list_empty (&vm->invalidated)) { r = -EAGAIN ;mutex_unlock (&p->adev->notifier_lock);return r; --2.43.0
cover drm-tip: build failure
In reply to Christian KönigHi all,
today drm-tip build failed with following log:
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c: In function 'amdgpu_cs_submit':drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c:1326:33: error: 'struct amdgpu_vm' has no member named 'invalidated'
1326 | if (r || !list_empty(&vm->invalidated)) {
| ^~
Fixed the build by making same change as did commit 59720bfd8c6d ("drm/amdgpu: restart the CS if some parts of the VM are still invalidated"), but double check would be probably needed.
BR,
Michał
Michał Grzelak (1):
drm/amdgpu: check individual moved list instead of invalidated
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Signature · 1 line
2.45.2
PATCH 1/1 drm/amdgpu: check individual moved list instead of invalidated
Checking invalidated field causes build to fail since it is absent in struct amdgpu_vm. Replace it with &vm->individual.moved identically as did commit 59720bfd8c6d ("drm/amdgpu: restart the CS if some parts of the VM are still invalidated").
--- drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +1 -1
@@ -1323,7 +1323,7 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p, e->range =NULL ; } -if (r || !list_empty (&vm->invalidated)) { +if (r || !list_empty (&vm->individual.moved)) { r = -EAGAIN ;mutex_unlock (&p->adev->notifier_lock);return r;
Signature · 1 line
2.45.2
Christian König
maintainerRe: [PATCH 1/1] drm/amdgpu: check individual moved list instead of invalidated
Quoted 10 lines · Michał Grzelak
Checking
invalidatedfield causes build to fail since it is absent in structamdgpu_vm. Replace it with&vm->individual.movedidentically as did commit59720bfd8c6d("drm/amdgpu: restart the CS if some parts of the VM are still invalidated").Cc Vitaly Prosyak <[email protected]>Cc Christian König <[email protected]>Cc Alex Deucher <[email protected]>Fixes 40396ffdf612 ("drm/amdgpu: restart the CS if some parts of the VM are still invalidated")Signed-off-by Michał Grzelak <[email protected]>
The field is renamed by a different patch so looks like a rebase/merge issue to me, could be that the Fixes tag needs double checking.
But either way Reviewed-by: Christian König <[email protected]>
Thanks,
Christian.
Quoted 17 lines
--- drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +1
-1@@ -1323,7 +1323,7 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p, e->range =NULL ; } -if (r || !list_empty (&vm->invalidated)) { +if (r || !list_empty (&vm->individual.moved)) { r = -EAGAIN ;mutex_unlock (&p->adev->notifier_lock);return r;
Re: [PATCH 1/1] drm/amdgpu: check individual moved list instead of invalidated
Quoted 6 lines · Christian König
Quoted 5 lines · Michał Grzelak
Checking
invalidatedfield causes build to fail since it is absent in structamdgpu_vm. Replace it with&vm->individual.movedidentically as did commit59720bfd8c6d("drm/amdgpu: restart the CS if some parts of the VM are still invalidated").
Quoted 7 lines
Quoted 5 lines
Cc Vitaly Prosyak <[email protected]>Cc Christian König <[email protected]>Cc Alex Deucher <[email protected]>Fixes 40396ffdf612 ("drm/amdgpu: restart the CS if some parts of the VM are still invalidated")Signed-off-by Michał Grzelak <[email protected]>The field is renamed by a different patch so looks like a rebase/merge issue to me, could be that the Fixes tag needs double checking.
My poor understanding is that commit 40396ffdf612 ("drm/amdgpu: restart the CS if some parts of the VM are still invalidated") got introduced into drm-tip after amd-drm-fixes-7.1-2026-06-04 got merged into drm-tip at commit 2aaaf5931b16 ("Merge tag 'amd-drm-fixes-7.1-2026-06-04' of
https://gitlab.freedesktop.org/agd5f/linux into drm-fixes") which I assume caused this build to fail. Meanwhile the commit should be a backport of commit 59720bfd8c6d ("drm/amdgpu: restart the CS if some parts of the VM are still invalidated") from amd-drm-next-7.2-2026-06-04 tag.
Thus I marked commit 40396ffdf612 ("drm/amdgpu: restart the CS if some parts of the VM are still invalidated") in Fixes: tag from drm-tip perspective, but I'm not sure if the description above applies nor have clue how to properly tag it :(. Could you give any hints on which commit should be actually put in Fixes:?
Quoted 2 lines
But either way Reviewed-by: Christian König <[email protected]>
Thank you Christian for the review. Assuming Fixes: is corrected, do you think this could be applied via drm-misc/drm-misc-fixes? Asking since I don't have commit right to any of drm-* repositories.
BR,
Michał
Quoted 23 lines
Thanks,
Christian.Quoted 17 lines
--- drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +1
-1@@ -1323,7 +1323,7 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p, e->range =NULL ; } -if (r || !list_empty (&vm->invalidated)) { +if (r || !list_empty (&vm->individual.moved)) { r = -EAGAIN ;mutex_unlock (&p->adev->notifier_lock);return r;
Series is:
Quoted 69 lines · Christian König
The notifier sequence must only be read once or otherwise we could work with invalid pages.
While at it also fix the coding style, e.g. drop the pre-initialized return value and use the common define for 2G range.
Signed-off-by Christian König <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +8
-8@@ -51,8 +51,6 @@#include "amdgpu_amdkfd.h" #include "amdgpu_hmm.h" -#define MAX_WALK_BYTE (2UL << 30) -/** * amdgpu_hmm_invalidate_gfx - callback to notify about mm change * @@ -170,11 +168,13 @@ int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier,void *owner, struct amdgpu_hmm_range *range) { - unsigned long end; + const u64 max_bytes = SZ_2G; + +struct hmm_range *hmm_range = &range->hmm_range;unsigned long timeout;unsigned long *pfns; -int r =0 ; -struct hmm_range *hmm_range = &range->hmm_range; +unsigned long end; +int r; pfns =kvmalloc_array (npages,sizeof (*pfns),GFP_KERNEL );if (unlikely (!pfns)) {@@ -191,8 +191,9 @@ int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier, end = start + npages *PAGE_SIZE ; hmm_range->dev_private_owner = owner; + hmm_range->notifier_seq =mmu_interval_read_begin (notifier); do { - hmm_range->end = min(hmm_range->start + MAX_WALK_BYTE, end); + hmm_range->end = min(hmm_range->start + max_bytes, end); pr_debug("hmm range: start = 0x%lx, end = 0x%lx", hmm_range->start, hmm_range->end);@@ -200,7 +201,6 @@ int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier, timeout = jiffies +msecs_to_jiffies (HMM_RANGE_DEFAULT_TIMEOUT ); retry: - hmm_range->notifier_seq =mmu_interval_read_begin (notifier); r =hmm_range_fault (hmm_range);if (unlikely (r)) {if (r == -EBUSY && !time_after (jiffies, timeout))@@ -210,7 +210,7 @@ int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier,if (hmm_range->end == end)break ; - hmm_range->hmm_pfns +=MAX_WALK_BYTE >>PAGE_SHIFT ; + hmm_range->hmm_pfns += max_bytes >>PAGE_SHIFT ; hmm_range->start = hmm_range->end; }while (hmm_range->end < end);
Patchwork
CI✗ Fi.CI.BUILD: failure for series starting with [1/1] drm/amdgpu: check individual moved list instead of invalidated
In reply to Christian König== Series Details ==
== Series Details ==
Series: series starting with [1/1] drm/amdgpu: check individual moved list instead of invalidated
URL : https://patchwork.freedesktop.org/series/168073/
State : failure
== Summary ==
Error: patch https://patchwork.freedesktop.org/api/1.0/series/168073/revisions/1/mbox/ not applied
Applying: drm/amdgpu: check individual moved list instead of invalidated
Using index info to reconstruct a base tree...
M drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c
Falling back to patching base and 3-way merge...
Auto-merging drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c
CONFLICT (content): Merge conflict in drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
Patch failed at 0001 drm/amdgpu: check individual moved list instead of invalidated When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".
Build failed, no error log produced