drm/ttm: Fix swapping dereferences of freed memory
Subsystems (3)
Thomas Hellström
reviewerRFC PATCH 1/1 drm/ttm: Fix swapping dereferences of freed memory
The swapping code was dereference bo->ttm pointers without having the dma-resv lock held. Also it might try to swap out unpopulated bos.
Fix this by moving the bo->ttm dereference until we have the reservation lock. Check that the ttm_tt is populated after the swap_notify callback.
--- drivers/gpu/drm/ttm/ttm_bo.c | 16 +++++++++++++++- drivers/gpu/drm/ttm/ttm_device.c | 8 +++----- 2 files changed, 18 insertions(+), 6 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +15 -1
@@ -1163,6 +1163,16 @@ int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx,if (!ttm_bo_evict_swapout_allowable (bo, ctx, &place, &locked,NULL ))return -EBUSY ; +dma_resv_assert_held (bo->base.resv); + +if (!bo->ttm || + bo->ttm->page_flags &TTM_PAGE_FLAG_SG || + bo->ttm->page_flags &TTM_PAGE_FLAG_SWAPPED ) { +if (locked) +dma_resv_unlock (bo->base.resv); +return -EBUSY ; + } +if (!ttm_bo_get_unless_zero (bo)) {if (locked)dma_resv_unlock (bo->base.resv);
@@ -1215,7 +1225,8 @@ int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx,if (bo->bdev->funcs->swap_notify) bo->bdev->funcs->swap_notify (bo); - ret =ttm_tt_swapout (bo->bdev, bo->ttm, gfp_flags); +if (ttm_tt_is_populated (bo->ttm)) + ret =ttm_tt_swapout (bo->bdev, bo->ttm, gfp_flags); out:/*
@@ -1225,6 +1236,9 @@ int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx,if (locked)dma_resv_unlock (bo->base.resv);ttm_bo_put (bo); + +/* Don't break locking rules. */ +WARN_ON (ret == -EBUSY );return ret; }
drivers/gpu/drm/ttm/ttm_device.c +3 -5
@@ -143,14 +143,12 @@ int ttm_device_swapout(struct ttm_device *bdev, struct ttm_operation_ctx *ctx,for (j =0 ; j <TTM_MAX_BO_PRIORITY ; ++j) {list_for_each_entry (bo, &man->lru[j], lru) { -uint32_t num_pages; +pgoff_t num_pages; -if (!bo->ttm || - bo->ttm->page_flags &TTM_PAGE_FLAG_SG || - bo->ttm->page_flags &TTM_PAGE_FLAG_SWAPPED ) +if (!READ_ONCE (bo->ttm))continue ; - num_pages = bo->ttm->num_pages; + num_pages = bo->base.size >>PAGE_SHIFT ; ret =ttm_bo_swapout (bo, ctx, gfp_flags);/* ttm_bo_swapout has dropped the lru_lock */ if (!ret)
Signature · 1 line
2.31.1
Am 27.05.21 um 16:19 schrieb Thomas Hellström:
Quoted 34 lines
The swapping code was dereference
bo->ttmpointers without having the dma-resv lock held. Also it might try to swap out unpopulated bos.Fix this by moving the
bo->ttmdereference until we have the reservation lock. Check that thettm_ttis populated after theswap_notifycallback.Signed-off-by Thomas Hellström <[email protected]>--- drivers/gpu/drm/ttm/ttm_bo.c | 16 +++++++++++++++- drivers/gpu/drm/ttm/ttm_device.c | 8 +++----- 2 files changed, 18 insertions(+), 6 deletions(-)drivers/gpu/drm/ttm/ttm_bo.c +10
-0@@ -1163,6 +1163,16 @@ int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx,if (!ttm_bo_evict_swapout_allowable (bo, ctx, &place, &locked,NULL ))return -EBUSY ; +dma_resv_assert_held (bo->base.resv); + +if (!bo->ttm || + bo->ttm->page_flags &TTM_PAGE_FLAG_SG || + bo->ttm->page_flags &TTM_PAGE_FLAG_SWAPPED ) { +if (locked) +dma_resv_unlock (bo->base.resv); +return -EBUSY ; + } +if (!ttm_bo_get_unless_zero (bo)) {if (locked)dma_resv_unlock (bo->base.resv);
drivers/gpu/drm/ttm/ttm_bo.c int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx, · line 1229
Quoted 6 lines
drivers/gpu/drm/ttm/ttm_bo.c +2
-1@@ -1215,7 +1225,8 @@ int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx,if (bo->bdev->funcs->swap_notify) bo->bdev->funcs->swap_notify (bo); - ret =ttm_tt_swapout (bo->bdev, bo->ttm, gfp_flags); +if (ttm_tt_is_populated (bo->ttm)) + ret =ttm_tt_swapout (bo->bdev, bo->ttm, gfp_flags);
Exactly that is what I won't recommend. We would try to swap out the same BO over and over again with that.
Why not move that to the check above as well?
Christian.
Quoted 35 lines
drivers/gpu/drm/ttm/ttm_bo.c +3
-0out:/* @@ -1225,6 +1236,9 @@ int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx,if (locked)dma_resv_unlock (bo->base.resv);ttm_bo_put (bo); + +/* Don't break locking rules. */ +WARN_ON (ret == -EBUSY );return ret; }drivers/gpu/drm/ttm/ttm_device.c +3
-5@@ -143,14 +143,12 @@ int ttm_device_swapout(struct ttm_device *bdev, struct ttm_operation_ctx *ctx,for (j =0 ; j <TTM_MAX_BO_PRIORITY ; ++j) {list_for_each_entry (bo, &man->lru[j], lru) { -uint32_t num_pages; +pgoff_t num_pages; -if (!bo->ttm || - bo->ttm->page_flags &TTM_PAGE_FLAG_SG || - bo->ttm->page_flags &TTM_PAGE_FLAG_SWAPPED ) +if (!READ_ONCE (bo->ttm))continue ; - num_pages = bo->ttm->num_pages; + num_pages = bo->base.size >>PAGE_SHIFT ; ret =ttm_bo_swapout (bo, ctx, gfp_flags);/* ttm_bo_swapout has dropped the lru_lock */ if (!ret)
Thomas Hellström
reviewerQuoted 43 lines · Christian König
Am 27.05.21 um 16:19 schrieb Thomas Hellström:
Quoted 42 lines
The swapping code was dereference
bo->ttmpointers without having the
dma-resv lock held. Also it might try to swap out unpopulated bos.Fix this by moving the
bo->ttmdereference until we have the reservation
lock. Check that thettm_ttis populated after theswap_notifycallback.Signed-off-by Thomas Hellström <[email protected]>---
drivers/gpu/drm/ttm/ttm_bo.c | 16 +++++++++++++++-drivers/gpu/drm/ttm/ttm_device.c| 8 +++-----2 files changed, 18 insertions(+), 6 deletions(-)drivers/gpu/drm/ttm/ttm_bo.c +0
-0@@ -1163,6 +1163,16 @@ int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx, if (!ttm_bo_evict_swapout_allowable(bo, ctx, &place, &locked,NULL )) return -EBUSY;
+ dma_resv_assert_held(bo->base.resv);drivers/gpu/drm/ttm/ttm_bo.c +9
-0+ + if (!bo->ttm || + bo->ttm->page_flags & TTM_PAGE_FLAG_SG || + bo->ttm->page_flags & TTM_PAGE_FLAG_SWAPPED) { + if (locked) + dma_resv_unlock(bo->base.resv); + return -EBUSY; + } +if (!ttm_bo_get_unless_zero(bo)) {
if (locked)dma_resv_unlock(bo->base.resv);drivers/gpu/drm/ttm/ttm_bo.c +0
-0@@ -1215,7 +1225,8 @@ int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx, if (bo->bdev->funcs->swap_notify)
Quoted 8 lines
Quoted 5 lines
drivers/gpu/drm/ttm/ttm_bo.c +0
-0bo->bdev->funcs->swap_notify(bo);
- ret =ttm_tt_swapout(bo->bdev, bo->ttm, gfp_flags); + if (ttm_tt_is_populated(bo->ttm))
+ ret =ttm_tt_swapout(bo->bdev, bo->ttm, gfp_flags);Exactly that is what I won't recommend. We would try to swap out the same BO over and over again with that.
But we wouldn't since the BO is taken off the LRU and never re-added,
Quoted 2 lines
Why not move that to the check above as well?
Because the BO may become unpopulated in swap_notify(), i915, like vmwgfx, sometimes sets up gpu bindings from system, and when we get a notification from user-space that those are purgeable, we don't want to purge immediately but wait for a potential swapout.
/Thomas
Quoted 48 lines
Christian.
Quoted 44 lines
out:
/*drivers/gpu/drm/ttm/ttm_bo.c +3
-0@@ -1225,6 +1236,9 @@ int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx, if (locked) dma_resv_unlock(bo->base.resv); ttm_bo_put(bo); + + /* Don't break locking rules. */ + WARN_ON(ret == -EBUSY); return ret; }drivers/gpu/drm/ttm/ttm_device.c +0
-0@@ -143,14 +143,12 @@ int ttm_device_swapout(struct ttm_device *bdev, struct ttm_operation_ctx *ctx,
for (j = 0; j <TTM_MAX_BO_PRIORITY; ++j) {list_for_each_entry(bo, &man->lru[j], lru){
- uint32_tnum_pages;
+ pgoff_tnum_pages;
- if (!bo->ttm ||
-bo->ttm->page_flags&TTM_PAGE_FLAG_SG||
-bo->ttm->page_flags&TTM_PAGE_FLAG_SWAPPED)
+ if (!READ_ONCE(bo->ttm)) continue;
- num_pages =bo->ttm->num_pages; + num_pages =bo->base.size>>PAGE_SHIFT;
ret =ttm_bo_swapout(bo, ctx, gfp_flags);
/*ttm_bo_swapouthas dropped thelru_lock*/
if (!ret)
Thomas Hellström
reviewerQuoted 58 lines · Thomas Hellström
Quoted 48 lines · Christian König
Am 27.05.21 um 16:19 schrieb Thomas Hellström:
Quoted 47 lines
The swapping code was dereference
bo->ttmpointers without having the
dma-resv lock held. Also it might try to swap out unpopulated bos.Fix this by moving the
bo->ttmdereference until we have the reservation
lock. Check that thettm_ttis populated after theswap_notifycallback.Signed-off-by: Thomas Hellström <[email protected]>
---
drivers/gpu/drm/ttm/ttm_bo.c | 16 +++++++++++++++-drivers/gpu/drm/ttm/ttm_device.c| 8 +++-----2 files changed, 18 insertions(+), 6 deletions(-)drivers/gpu/drm/ttm/ttm_bo.c +0
-0@@ -1163,6 +1163,16 @@ int ttm_bo_swapout(struct
ttm_buffer_object
*bo, structttm_operation_ctx*ctx,
if (!ttm_bo_evict_swapout_allowable(bo, ctx, &place, &locked, NULL))
return-EBUSY;
+ dma_resv_assert_held(bo->base.resv);drivers/gpu/drm/ttm/ttm_bo.c +9
-0+ + if (!bo->ttm || + bo->ttm->page_flags & TTM_PAGE_FLAG_SG || + bo->ttm->page_flags & TTM_PAGE_FLAG_SWAPPED) { + if (locked) + dma_resv_unlock(bo->base.resv); + return -EBUSY; + } +if (!ttm_bo_get_unless_zero(bo)) {
if (locked)dma_resv_unlock(bo->base.resv);drivers/gpu/drm/ttm/ttm_bo.c +0
-0@@ -1215,7 +1225,8 @@ int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx, if (bo->bdev->funcs->swap_notify) bo->bdev->funcs->swap_notify(bo);Quoted 8 lines
Quoted 4 lines
- ret =
ttm_tt_swapout(bo->bdev, bo->ttm, gfp_flags); + if (ttm_tt_is_populated(bo->ttm))
+ ret =ttm_tt_swapout(bo->bdev, bo->ttm, gfp_flags);Exactly that is what I won't recommend. We would try to swap out the
same BO over and over again with that.
Quoted 3 lines
But we wouldn't since the BO is taken off the LRU and never re-added,
In fact, we'd probably might want to take the !bo->ttm bos off the LRU as well..
/Thomas
Am 27.05.21 um 17:05 schrieb Thomas Hellström:
Quoted 57 lines · Thomas Hellström
Quoted 56 lines · Christian König
Quoted 55 lines
Am 27.05.21 um 16:19 schrieb Thomas Hellström:
Quoted 43 lines
The swapping code was dereference
bo->ttmpointers without having the
dma-resv lock held. Also it might try to swap out unpopulated bos.Fix this by moving the
bo->ttmdereference until we have the reservation
lock. Check that thettm_ttis populated after theswap_notifycallback.Signed-off-by: Thomas Hellström
<[email protected]>--- drivers/gpu/drm/ttm/ttm_bo.c | 16 +++++++++++++++- drivers/gpu/drm/ttm/ttm_device.c | 8 +++----- 2 files changed, 18 insertions(+), 6 deletions(-)drivers/gpu/drm/ttm/ttm_bo.c +0
-0@@ -1163,6 +1163,16 @@ int ttm_bo_swapout(struct
ttm_buffer_object
*bo, structttm_operation_ctx*ctx,
if (!ttm_bo_evict_swapout_allowable(bo, ctx, &place,
&locked, NULL))
return-EBUSY;+ dma_resv_assert_held(bo->base.resv);
drivers/gpu/drm/ttm/ttm_bo.c +9
-0+ + if (!bo->ttm || + bo->ttm->page_flags & TTM_PAGE_FLAG_SG || + bo->ttm->page_flags & TTM_PAGE_FLAG_SWAPPED) { + if (locked) + dma_resv_unlock(bo->base.resv); + return -EBUSY; + } + if (!ttm_bo_get_unless_zero(bo)) { if (locked) dma_resv_unlock(bo->base.resv);drivers/gpu/drm/ttm/ttm_bo.c int ttm_bo_swapout(struct ttm_buffer_object · line 1231
Quoted 8 lines
drivers/gpu/drm/ttm/ttm_bo.c +2
-1@@ -1215,7 +1225,8 @@ int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx, if (bo->bdev->funcs->swap_notify) bo->bdev->funcs->swap_notify(bo); - ret = ttm_tt_swapout(bo->bdev, bo->ttm, gfp_flags); + if (ttm_tt_is_populated(bo->ttm)) + ret = ttm_tt_swapout(bo->bdev, bo->ttm, gfp_flags);Exactly that is what I won't recommend. We would try to swap out the
same BO over and over again with that.
Quoted 5 lines
Quoted 3 lines
But we wouldn't since the BO is taken off the LRU and never re-added,
In fact, we'd probably might want to take the !bo->ttm bos off the LRU as well..
No, we don't want to take any BOs of the LRU unless they are pinned.
Adding a TT object or populating it doesn't necessarily put the BO back to the LRU.
Christian.
Quoted 3 lines
/Thomas
Thomas Hellström
reviewerQuoted 70 lines · Christian König
Am 27.05.21 um 17:05 schrieb Thomas Hellström:
Quoted 61 lines · Thomas Hellström
Quoted 60 lines · Christian König
Quoted 59 lines
Am 27.05.21 um 16:19 schrieb Thomas Hellström:
Quoted 46 lines
The swapping code was dereference
bo->ttmpointers without having
the
dma-resv lock held. Also it might try to swap out unpopulated bos.Fix this by moving the
bo->ttmdereference until we have the reservation
lock. Check that thettm_ttis populated after theswap_notify
callback.Signed-off-by: Thomas Hellström <[email protected]>
---
drivers/gpu/drm/ttm/ttm_bo.c | 16 +++++++++++++++-drivers/gpu/drm/ttm/ttm_device.c| 8 +++-----2 files changed, 18 insertions(+), 6 deletions(-)drivers/gpu/drm/ttm/ttm_bo.c +0
-0@@ -1163,6 +1163,16 @@ int ttm_bo_swapout(struct
ttm_buffer_object
*bo, structttm_operation_ctx*ctx,
if (!ttm_bo_evict_swapout_allowable(bo, ctx, &place, &locked, NULL))
return-EBUSY;
+ dma_resv_assert_held(bo->base.resv);drivers/gpu/drm/ttm/ttm_bo.c +9
-0+ + if (!bo->ttm || + bo->ttm->page_flags & TTM_PAGE_FLAG_SG || + bo->ttm->page_flags & TTM_PAGE_FLAG_SWAPPED) { + if (locked) + dma_resv_unlock(bo->base.resv); + return -EBUSY; + } +if (!ttm_bo_get_unless_zero(bo)) {
if (locked)dma_resv_unlock(bo->base.resv);drivers/gpu/drm/ttm/ttm_bo.c +0
-0@@ -1215,7 +1225,8 @@ int ttm_bo_swapout(struct
ttm_buffer_objectQuoted 8 lines
*bo, struct
ttm_operation_ctx*ctx,
if (bo->bdev->funcs->swap_notify)bo->bdev->funcs->swap_notify(bo);
- ret =ttm_tt_swapout(bo->bdev, bo->ttm, gfp_flags); + if (ttm_tt_is_populated(bo->ttm))
+ ret =ttm_tt_swapout(bo->bdev, bo->ttm, gfp_flags);Exactly that is what I won't recommend. We would try to swap out
the
same BO over and over again with that.Quoted 7 lines
Quoted 4 lines
But we wouldn't since the BO is taken off the LRU and never re- added,
In fact, we'd probably might want to take the !bo->ttm bos off the LRU
as well..
Quoted 5 lines
No, we don't want to take any BOs of the LRU unless they are pinned.
Adding a TT object or populating it doesn't necessarily put the BO back
to the LRU.
OK, but swapped bos are also taken off the LRU list so these unpopulated bos are just taking the same path. Only difference to swapped is that they don't get read back on re-populate, but typically cleared.
But what would be the point of keeping swapped-out bos on the LRU list?, particularly when we're iterating under a spinlock? Shouldn't we try to re-add to LRU (if not already on an LRU) just before populating? There aren't really that many calls in core TTM.
/Thomas
Quoted 7 lines
Christian.
Quoted 3 lines
/Thomas
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
Am 27.05.21 um 17:51 schrieb Thomas Hellström:
Quoted 80 lines · Christian König
Quoted 66 lines
Am 27.05.21 um 17:05 schrieb Thomas Hellström:
Quoted 65 lines · Thomas Hellström
Quoted 64 lines · Christian König
Quoted 51 lines
Am 27.05.21 um 16:19 schrieb Thomas Hellström:
Quoted 50 lines
The swapping code was dereference
bo->ttmpointers without having
the
dma-resv lock held. Also it might try to swap out unpopulated bos.Fix this by moving the
bo->ttmdereference until we have the reservation
lock. Check that thettm_ttis populated after theswap_notify
callback.Signed-off-by: Thomas Hellström
<[email protected]>--- drivers/gpu/drm/ttm/ttm_bo.c | 16 +++++++++++++++- drivers/gpu/drm/ttm/ttm_device.c | 8 +++----- 2 files changed, 18 insertions(+), 6 deletions(-)drivers/gpu/drm/ttm/ttm_bo.c +0
-0@@ -1163,6 +1163,16 @@ int ttm_bo_swapout(struct
ttm_buffer_object
*bo, structttm_operation_ctx*ctx,
if (!ttm_bo_evict_swapout_allowable(bo, ctx, &place,
&locked, NULL))
return-EBUSY;+ dma_resv_assert_held(bo->base.resv);
drivers/gpu/drm/ttm/ttm_bo.c +9
-0+ + if (!bo->ttm || + bo->ttm->page_flags & TTM_PAGE_FLAG_SG || + bo->ttm->page_flags & TTM_PAGE_FLAG_SWAPPED) { + if (locked) + dma_resv_unlock(bo->base.resv); + return -EBUSY; + } + if (!ttm_bo_get_unless_zero(bo)) { if (locked) dma_resv_unlock(bo->base.resv);@@ -1215,7 +1225,8 @@ int ttm_bo_swapout(struct
ttm_buffer_object
*bo, structttm_operation_ctx*ctx,
if (bo->bdev->funcs->swap_notify)bo->bdev->funcs->swap_notify(bo);Quoted 8 lines
Quoted 4 lines
- ret =
ttm_tt_swapout(bo->bdev, bo->ttm, gfp_flags); + if (ttm_tt_is_populated(bo->ttm))
+ ret =ttm_tt_swapout(bo->bdev, bo->ttm, gfp_flags);Exactly that is what I won't recommend. We would try to swap out
the
same BO over and over again with that.But we wouldn't since the BO is taken off the LRU and never re- added,
Quoted 8 lines
Quoted 3 lines
In fact, we'd probably might want to take the !bo->ttm bos off the LRU
as well..No, we don't want to take any BOs of the LRU unless they are pinned.
Adding a TT object or populating it doesn't necessarily put the BO back
to the LRU.OK, but swapped bos are also taken off the LRU list so these unpopulated bos are just taking the same path. Only difference to swapped is that they don't get read back on re-populate, but typically cleared.
Quoted 4 lines
But what would be the point of keeping swapped-out bos on the LRU list?, particularly when we're iterating under a spinlock? Shouldn't we try to re-add to LRU (if not already on an LRU) just before populating? There aren't really that many calls in core TTM.
I want to avoid removing BOs from the LRU as much as possible since we forgot on multiple places that we want to re-add them.
Conceptual I think the swapped BOs should have a separate memory domain, this way we can ignore them cleanly when swapping things out.
Going to pick this patch up, modifying it a bit more and then pushing it to drm-misc-fixes for upstreaming.
Thanks,
Christian.
Quoted 12 lines
/Thomas
Quoted 4 lines
Christian.
Quoted 2 lines
/Thomas
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
Thomas Hellström
reviewerQuoted 99 lines · Christian König
Am 27.05.21 um 17:51 schrieb Thomas Hellström:
Quoted 93 lines · Christian König
Quoted 79 lines
Am 27.05.21 um 17:05 schrieb Thomas Hellström:
Quoted 74 lines · Thomas Hellström
Quoted 68 lines · Christian König
Quoted 59 lines
Am 27.05.21 um 16:19 schrieb Thomas Hellström:
Quoted 58 lines
The swapping code was dereference
bo->ttmpointers without
having
the
dma-resv lock held. Also it might try to swap out unpopulated
bos.Fix this by moving the
bo->ttmdereference until we have the
reservation
lock. Check that thettm_ttis populated after theswap_notify
callback.Signed-off-by: Thomas Hellström <[email protected]>
---
drivers/gpu/drm/ttm/ttm_bo.c | 16
+++++++++++++++-
drivers/gpu/drm/ttm/ttm_device.c| 8 +++-----2 files changed, 18 insertions(+), 6 deletions(-)drivers/gpu/drm/ttm/ttm_bo.c +0
-0@@ -1163,6 +1163,16 @@ int ttm_bo_swapout(struct
ttm_buffer_object
*bo, structttm_operation_ctx*ctx,
if (!ttm_bo_evict_swapout_allowable(bo, ctx, &place,
&locked, NULL))
return-EBUSY;
+ dma_resv_assert_held(bo->base.resv);
+
+ if (!bo->ttm ||
+bo->ttm->page_flags&TTM_PAGE_FLAG_SG|| +bo->ttm->page_flags&TTM_PAGE_FLAG_SWAPPED) {
+ if (locked)
+ dma_resv_unlock(bo->base.resv); + return-EBUSY;
+ }
+
if (!ttm_bo_get_unless_zero(bo)) {
if (locked)dma_resv_unlock(bo->base.resv);drivers/gpu/drm/ttm/ttm_bo.c +0
-0@@ -1215,7 +1225,8 @@ int ttm_bo_swapout(struct
ttm_buffer_object
*bo, structttm_operation_ctx*ctx,
if (bo->bdev->funcs->swap_notify)bo->bdev->funcs->swap_notify(bo);
- ret =ttm_tt_swapout(bo->bdev, bo->ttm, gfp_flags);Quoted 8 lines
Quoted 3 lines
+ if (
ttm_tt_is_populated(bo->ttm))
+ ret =ttm_tt_swapout(bo->bdev, bo->ttm, gfp_flags);Exactly that is what I won't recommend. We would try to swap
out
the
same BO over and over again with that.Quoted 5 lines
But we wouldn't since the BO is taken off the LRU and never re-
added,Quoted 4 lines
In fact, we'd probably might want to take the !bo->ttm bos off the
LRU
as well..Quoted 7 lines
No, we don't want to take any BOs of the LRU unless they are pinned.
Adding a TT object or populating it doesn't necessarily put the BO
back
to the LRU.OK, but swapped bos are also taken off the LRU list so these unpopulated bos are just taking the same path. Only difference to swapped is that they don't get read back on re-populate, but typically
cleared.Quoted 4 lines
But what would be the point of keeping swapped-out bos on the LRU list?, particularly when we're iterating under a spinlock? Shouldn't we try to re-add to LRU (if not already on an LRU) just before populating? There aren't really that many calls in core TTM.
Quoted 7 lines
I want to avoid removing BOs from the LRU as much as possible since we
forgot on multiple places that we want to re-add them.Conceptual I think the swapped BOs should have a separate memory domain,
this way we can ignore them cleanly when swapping things out.
Yes, that would of course work as well. Keeping them on the system LRU is IMO highly undesirable.
Quoted 4 lines
Going to pick this patch up, modifying it a bit more and then pushing it
to drm-misc-fixes for upstreaming.
OK, I dropped the TTM fix for the purge-in-swap-notify from the i915 series, hoping that the reworked variant of this patch lands first.
Thanks,
Thomas
Quoted 17 lines
Thanks,
Christian.Quoted 12 lines
/Thomas
Quoted 4 lines
Christian.
Quoted 2 lines
/Thomas
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
Am 28.05.21 um 09:33 schrieb Thomas Hellström:
Quoted 106 lines · Christian König
Quoted 98 lines
Am 27.05.21 um 17:51 schrieb Thomas Hellström:
Quoted 93 lines · Christian König
Quoted 79 lines
Am 27.05.21 um 17:05 schrieb Thomas Hellström:
Quoted 74 lines · Thomas Hellström
Quoted 68 lines · Christian König
Quoted 57 lines
Am 27.05.21 um 16:19 schrieb Thomas Hellström:
Quoted 56 lines
The swapping code was dereference
bo->ttmpointers without
having
the
dma-resv lock held. Also it might try to swap out unpopulated
bos.Fix this by moving the
bo->ttmdereference until we have the
reservation
lock. Check that thettm_ttis populated after theswap_notify
callback.Signed-off-by: Thomas Hellström
<[email protected]>--- drivers/gpu/drm/ttm/ttm_bo.c | 16 +++++++++++++++- drivers/gpu/drm/ttm/ttm_device.c | 8 +++----- 2 files changed, 18 insertions(+), 6 deletions(-)drivers/gpu/drm/ttm/ttm_bo.c +0
-0@@ -1163,6 +1163,16 @@ int ttm_bo_swapout(struct
ttm_buffer_object
*bo, structttm_operation_ctx*ctx,
if (!ttm_bo_evict_swapout_allowable(bo, ctx,
&place,
&locked, NULL))
return-EBUSY;+ dma_resv_assert_held(bo->base.resv);
+
+ if (!bo->ttm ||
+bo->ttm->page_flags&TTM_PAGE_FLAG_SG|| +bo->ttm->page_flags&TTM_PAGE_FLAG_SWAPPED) {
+ if (locked)
+ dma_resv_unlock(bo->base.resv); + return-EBUSY;
+ }drivers/gpu/drm/ttm/ttm_bo.c +1
-0+ if (!ttm_bo_get_unless_zero(bo)) { if (locked) dma_resv_unlock(bo->base.resv);@@ -1215,7 +1225,8 @@ int ttm_bo_swapout(struct
ttm_buffer_object
*bo, structttm_operation_ctx*ctx,
if (bo->bdev->funcs->swap_notify)bo->bdev->funcs->swap_notify(bo);Quoted 10 lines
Quoted 5 lines
- ret =
ttm_tt_swapout(bo->bdev, bo->ttm, gfp_flags);
+ if (ttm_tt_is_populated(bo->ttm))
+ ret =ttm_tt_swapout(bo->bdev, bo->ttm, gfp_flags);Exactly that is what I won't recommend. We would try to swap
out
the
same BO over and over again with that.Quoted 5 lines
But we wouldn't since the BO is taken off the LRU and never re-
added,Quoted 4 lines
In fact, we'd probably might want to take the !bo->ttm bos off the
LRU
as well..Quoted 7 lines
No, we don't want to take any BOs of the LRU unless they are pinned.
Adding a TT object or populating it doesn't necessarily put the BO
back
to the LRU.OK, but swapped bos are also taken off the LRU list so these unpopulated bos are just taking the same path. Only difference to swapped is that they don't get read back on re-populate, but typically
cleared.Quoted 4 lines
But what would be the point of keeping swapped-out bos on the LRU list?, particularly when we're iterating under a spinlock? Shouldn't we try to re-add to LRU (if not already on an LRU) just before populating? There aren't really that many calls in core TTM.
Quoted 7 lines
I want to avoid removing BOs from the LRU as much as possible since we
forgot on multiple places that we want to re-add them.Conceptual I think the swapped BOs should have a separate memory domain,
this way we can ignore them cleanly when swapping things out.
Quoted 8 lines
Yes, that would of course work as well. Keeping them on the system LRU is IMO highly undesirable.
Quoted 3 lines
Going to pick this patch up, modifying it a bit more and then pushing it
to drm-misc-fixes for upstreaming.OK, I dropped the TTM fix for the purge-in-swap-notify from the i915 series, hoping that the reworked variant of this patch lands first.
You will still need to add the second ttm_tt_populated() check since I dropped that for the back which I want to push to -fixes.
Regards,
Christian.
Quoted 18 lines
Thanks,
ThomasQuoted 13 lines
Thanks,
Christian.Quoted 10 lines
/Thomas
Quoted 4 lines
Christian.
Quoted 2 lines
/Thomas
Thomas Hellström
reviewerQuoted 111 lines · Christian König
Am 28.05.21 um 09:33 schrieb Thomas Hellström:
Quoted 110 lines · Christian König
Quoted 96 lines
Am 27.05.21 um 17:51 schrieb Thomas Hellström:
Quoted 91 lines · Christian König
Quoted 77 lines
Am 27.05.21 um 17:05 schrieb Thomas Hellström:
Quoted 72 lines · Thomas Hellström
Quoted 66 lines · Christian König
Quoted 57 lines
Am 27.05.21 um 16:19 schrieb Thomas Hellström:
Quoted 56 lines
The swapping code was dereference
bo->ttmpointers without
having
the
dma-resv lock held. Also it might try to swap out unpopulated
bos.Fix this by moving the
bo->ttmdereference until we have the
reservation
lock. Check that thettm_ttis populated after theswap_notify
callback.Signed-off-by: Thomas Hellström <[email protected]>
---
drivers/gpu/drm/ttm/ttm_bo.c | 16
+++++++++++++++-
drivers/gpu/drm/ttm/ttm_device.c| 8 +++-----2 files changed, 18 insertions(+), 6 deletions(-)drivers/gpu/drm/ttm/ttm_bo.c +0
-0@@ -1163,6 +1163,16 @@ int ttm_bo_swapout(struct
ttm_buffer_object
*bo, structttm_operation_ctx*ctx,
if (!ttm_bo_evict_swapout_allowable(bo, ctx, &place,
&locked, NULL))
return-EBUSY;
+ dma_resv_assert_held(bo->base.resv);
+
+ if (!bo->ttm ||
+bo->ttm->page_flags&TTM_PAGE_FLAG_SG|| +bo->ttm->page_flags&TTM_PAGE_FLAG_SWAPPED) {
+ if (locked)
+ dma_resv_unlock(bo->base.resv); + return-EBUSY;
+ }
+
if (!ttm_bo_get_unless_zero(bo)) {
if (locked)dma_resv_unlock(bo->base.resv);drivers/gpu/drm/ttm/ttm_bo.c +0
-0@@ -1215,7 +1225,8 @@ int ttm_bo_swapout(struct
ttm_buffer_object
*bo, structttm_operation_ctx*ctx,
if (bo->bdev->funcs->swap_notify)bo->bdev->funcs->swap_notify(bo); - ret =ttm_tt_swapout(bo->bdev, bo->ttm, gfp_flags);Quoted 8 lines
Quoted 3 lines
+ if (
ttm_tt_is_populated(bo->ttm))
+ ret =ttm_tt_swapout(bo->bdev, bo->ttm, gfp_flags);Exactly that is what I won't recommend. We would try to swap
out
the
same BO over and over again with that.Quoted 5 lines
But we wouldn't since the BO is taken off the LRU and never re-
added,Quoted 4 lines
In fact, we'd probably might want to take the !bo->ttm bos off the
LRU
as well..Quoted 7 lines
No, we don't want to take any BOs of the LRU unless they are pinned.
Adding a TT object or populating it doesn't necessarily put the BO
back
to the LRU.OK, but swapped bos are also taken off the LRU list so these unpopulated bos are just taking the same path. Only difference to swapped is that they don't get read back on re-populate, but typically
cleared.Quoted 4 lines
But what would be the point of keeping swapped-out bos on the LRU list?, particularly when we're iterating under a spinlock? Shouldn't we try to re-add to LRU (if not already on an LRU) just before populating? There aren't really that many calls in core TTM.
Quoted 7 lines
I want to avoid removing BOs from the LRU as much as possible since we
forgot on multiple places that we want to re-add them.Conceptual I think the swapped BOs should have a separate memory domain,
this way we can ignore them cleanly when swapping things out.Yes, that would of course work as well. Keeping them on the system LRU is IMO highly undesirable.
Quoted 3 lines
Going to pick this patch up, modifying it a bit more and then pushing it
to drm-misc-fixes for upstreaming.
Quoted 8 lines
Quoted 2 lines
OK, I dropped the TTM fix for the purge-in-swap-notify from the i915 series, hoping that the reworked variant of this patch lands first.
You will still need to add the second
ttm_tt_populated()check since I dropped that for the back which I want to push to -fixes.Regards,
Christian.
OK, great. then you have my S-O-B on this patch.
BTW that original patch that added the ttm_tt_is_populated() was considered "LGTM" by you, except for this ttm_tt_is_populated(). So do I have an Acked-by: on that now?
That is
https://patchwork.freedesktop.org/patch/435833/?series=90681&rev=2
plus the check added?
Thanks,
Thomas
Quoted 19 lines
Quoted 18 lines
Thanks,
ThomasQuoted 13 lines
Thanks,
Christian.Quoted 10 lines
/Thomas
Quoted 4 lines
Christian.
Quoted 2 lines
/Thomas
Am 28.05.21 um 16:17 schrieb Thomas Hellström:
Quoted 123 lines · Christian König
Quoted 111 lines
Am 28.05.21 um 09:33 schrieb Thomas Hellström:
Quoted 110 lines · Christian König
Quoted 96 lines
Am 27.05.21 um 17:51 schrieb Thomas Hellström:
Quoted 91 lines · Christian König
Quoted 77 lines
Am 27.05.21 um 17:05 schrieb Thomas Hellström:
Quoted 72 lines · Thomas Hellström
Quoted 66 lines · Christian König
Quoted 57 lines
Am 27.05.21 um 16:19 schrieb Thomas Hellström:
Quoted 56 lines
The swapping code was dereference
bo->ttmpointers without
having
the
dma-resv lock held. Also it might try to swap out unpopulated
bos.Fix this by moving the
bo->ttmdereference until we have the
reservation
lock. Check that thettm_ttis populated after theswap_notify
callback.Signed-off-by: Thomas Hellström <[email protected]>
---
drivers/gpu/drm/ttm/ttm_bo.c | 16
+++++++++++++++-
drivers/gpu/drm/ttm/ttm_device.c| 8 +++-----2 files changed, 18 insertions(+), 6 deletions(-)drivers/gpu/drm/ttm/ttm_bo.c +0
-0@@ -1163,6 +1163,16 @@ int ttm_bo_swapout(struct
ttm_buffer_object
*bo, structttm_operation_ctx*ctx,
if (!ttm_bo_evict_swapout_allowable(bo, ctx, &place,
&locked, NULL))
return-EBUSY;
+ dma_resv_assert_held(bo->base.resv);
+
+ if (!bo->ttm ||
+bo->ttm->page_flags&TTM_PAGE_FLAG_SG|| +bo->ttm->page_flags&TTM_PAGE_FLAG_SWAPPED) {
+ if (locked)
+ dma_resv_unlock(bo->base.resv); + return-EBUSY;
+ }
+
if (!ttm_bo_get_unless_zero(bo)) {
if (locked)dma_resv_unlock(bo->base.resv);drivers/gpu/drm/ttm/ttm_bo.c +0
-0@@ -1215,7 +1225,8 @@ int ttm_bo_swapout(struct
ttm_buffer_object
*bo, structttm_operation_ctx*ctx,
if (bo->bdev->funcs->swap_notify)bo->bdev->funcs->swap_notify(bo); - ret =ttm_tt_swapout(bo->bdev, bo->ttm, gfp_flags);Quoted 8 lines
Quoted 3 lines
+ if (
ttm_tt_is_populated(bo->ttm))
+ ret =ttm_tt_swapout(bo->bdev, bo->ttm, gfp_flags);Exactly that is what I won't recommend. We would try to swap
out
the
same BO over and over again with that.Quoted 5 lines
But we wouldn't since the BO is taken off the LRU and never re-
added,Quoted 4 lines
In fact, we'd probably might want to take the !bo->ttm bos off the
LRU
as well..Quoted 7 lines
No, we don't want to take any BOs of the LRU unless they are pinned.
Adding a TT object or populating it doesn't necessarily put the BO
back
to the LRU.OK, but swapped bos are also taken off the LRU list so these unpopulated bos are just taking the same path. Only difference to swapped is that they don't get read back on re-populate, but typically
cleared.Quoted 4 lines
But what would be the point of keeping swapped-out bos on the LRU list?, particularly when we're iterating under a spinlock? Shouldn't we try to re-add to LRU (if not already on an LRU) just before populating? There aren't really that many calls in core TTM.
Quoted 7 lines
I want to avoid removing BOs from the LRU as much as possible since we
forgot on multiple places that we want to re-add them.Conceptual I think the swapped BOs should have a separate memory domain,
this way we can ignore them cleanly when swapping things out.Yes, that would of course work as well. Keeping them on the system LRU is IMO highly undesirable.
Quoted 3 lines
Going to pick this patch up, modifying it a bit more and then pushing it
to drm-misc-fixes for upstreaming.Quoted 8 lines
Quoted 2 lines
OK, I dropped the TTM fix for the purge-in-swap-notify from the i915 series, hoping that the reworked variant of this patch lands first.
You will still need to add the second
ttm_tt_populated()check since I dropped that for the back which I want to push to -fixes.Regards,
Christian.OK, great. then you have my S-O-B on this patch.
Quoted 8 lines
BTW that original patch that added the
ttm_tt_is_populated()was considered "LGTM" by you, except for thisttm_tt_is_populated(). So do I have an Acked-by: on that now?That is
plus the check added?
Yeah, sure.
Christian.
Quoted 27 lines
Thanks,
Thomas
Quoted 19 lines
Quoted 18 lines
Thanks,
ThomasQuoted 13 lines
Thanks,
Christian.Quoted 10 lines
/Thomas
Quoted 4 lines
Christian.
Quoted 2 lines
/Thomas
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
Am 27.05.21 um 17:01 schrieb Thomas Hellström:
Quoted 46 lines · Christian König
Quoted 45 lines
Am 27.05.21 um 16:19 schrieb Thomas Hellström:
Quoted 44 lines
The swapping code was dereference
bo->ttmpointers without having the
dma-resv lock held. Also it might try to swap out unpopulated bos.Fix this by moving the
bo->ttmdereference until we have the reservation
lock. Check that thettm_ttis populated after theswap_notifycallback.Signed-off-by Thomas Hellström <[email protected]>--- drivers/gpu/drm/ttm/ttm_bo.c | 16 +++++++++++++++- drivers/gpu/drm/ttm/ttm_device.c | 8 +++----- 2 files changed, 18 insertions(+), 6 deletions(-)drivers/gpu/drm/ttm/ttm_bo.c +10
-0@@ -1163,6 +1163,16 @@ int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx, if (!ttm_bo_evict_swapout_allowable(bo, ctx, &place, &locked,NULL )) return -EBUSY; + dma_resv_assert_held(bo->base.resv); + + if (!bo->ttm || + bo->ttm->page_flags & TTM_PAGE_FLAG_SG || + bo->ttm->page_flags & TTM_PAGE_FLAG_SWAPPED) { + if (locked) + dma_resv_unlock(bo->base.resv); + return -EBUSY; + } + if (!ttm_bo_get_unless_zero(bo)) {if (locked)
dma_resv_unlock(bo->base.resv);drivers/gpu/drm/ttm/ttm_bo.c +0
-0@@ -1215,7 +1225,8 @@ int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx, if (bo->bdev->funcs->swap_notify) bo->bdev->funcs->swap_notify(bo);
Quoted 6 lines
Quoted 5 lines
drivers/gpu/drm/ttm/ttm_bo.c int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx, · line 1229
Quoted 3 lines
drivers/gpu/drm/ttm/ttm_bo.c +2
-1- ret = ttm_tt_swapout(bo->bdev, bo->ttm, gfp_flags); + if (ttm_tt_is_populated(bo->ttm)) + ret = ttm_tt_swapout(bo->bdev, bo->ttm, gfp_flags);Exactly that is what I won't recommend. We would try to swap out the same BO over and over again with that.
But we wouldn't since the BO is taken off the LRU and never re-added,
Well then that would be a bug in itself.
Quoted 5 lines
Quoted 1 line
Why not move that to the check above as well?
Because the BO may become unpopulated in
swap_notify(), i915, like vmwgfx, sometimes sets up gpu bindings from system, and when we get a notification from user-space that those are purgeable, we don't want to purge immediately but wait for a potential swapout.
Uff, good point. But then we need to check that at both locations I think.
Because populating the TT object currently doesn't put the BO back on the LRU eventually.
Christian.
Quoted 51 lines
/Thomas
Quoted 46 lines
Christian.
Quoted 44 lines
drivers/gpu/drm/ttm/ttm_bo.c +0
-0out:/*
drivers/gpu/drm/ttm/ttm_bo.c +3
-0@@ -1225,6 +1236,9 @@ int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx, if (locked) dma_resv_unlock(bo->base.resv); ttm_bo_put(bo); + + /* Don't break locking rules. */ + WARN_ON(ret == -EBUSY); return ret; }drivers/gpu/drm/ttm/ttm_device.c +1
-3@@ -143,14 +143,12 @@ int ttm_device_swapout(struct ttm_device *bdev, struct ttm_operation_ctx *ctx, for (j = 0; j < TTM_MAX_BO_PRIORITY; ++j) { list_for_each_entry(bo, &man->lru[j], lru) { - uint32_t num_pages; + pgoff_t num_pages; - if (!bo->ttm || - bo->ttm->page_flags &
TTM_PAGE_FLAG_SG||
-bo->ttm->page_flags&TTM_PAGE_FLAG_SWAPPED)
+ if (!READ_ONCE(bo->ttm))
continue;- num_pages =
bo->ttm->num_pages; + num_pages =bo->base.size>>PAGE_SHIFT;
ret = ttm_bo_swapout(bo, ctx,gfp_flags);
/*ttm_bo_swapouthas dropped thelru_lock*/
if (!ret)
Patchwork
CI[Intel-gfx] ✗ Fi.CI.BUILD: failure for drm/ttm: Fix swapping dereferences of freed memory
In reply to Thomas Hellström== Series Details ==
== Series Details ==
Series: drm/ttm: Fix swapping dereferences of freed memory
URL : https://patchwork.freedesktop.org/series/90673/
State : failure
== Summary ==
Applying: drm/ttm: Fix swapping dereferences of freed memory
error: sha1 information is lacking or useless (drivers/gpu/drm/ttm/ttm_bo.c).
error: could not build fake ancestor
hint: Use 'git am --show-current-patch=diff' to see the failed patch Patch failed at 0001 drm/ttm: Fix swapping dereferences of freed memory 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".
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx