Skip to contentSkip to conversation

drm/ttm: fix deref of bo->ttm without holding the lock v2

PATCH In review Christian König 4 participants 6 messages May 28, 2021 – Jun 8, 2021

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/ttm: fix deref of bo->ttm without holding the lock v2

We need to grab the resv lock first before doing that check.

v2 (chk): simplify the change for -fixes

---
 drivers/gpu/drm/ttm/ttm_bo.c     | 5 ++++-
 drivers/gpu/drm/ttm/ttm_device.c | 8 +-------
 2 files changed, 5 insertions(+), 8 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +4 -1
index cfd0b9292397..ebcffe794adb 100644
@@ -1172,7 +1172,10 @@ int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx,
 	if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked, NULL))
 		return -EBUSY;
 
-	if (!ttm_bo_get_unless_zero(bo)) {
+	if (!bo->ttm || !ttm_tt_is_populated(bo->ttm) ||
+	    bo->ttm->page_flags & TTM_PAGE_FLAG_SG ||
+	    bo->ttm->page_flags & TTM_PAGE_FLAG_SWAPPED ||
+	    !ttm_bo_get_unless_zero(bo)) {
 		if (locked)
 			dma_resv_unlock(bo->base.resv);
 		return -EBUSY;
drivers/gpu/drm/ttm/ttm_device.c +1 -7
index a1dcf7d55c90..3d9c62b93e29 100644
@@ -143,14 +143,8 @@ 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;
+				uint32_t num_pages = PFN_UP(bo->base.size);
 
-				if (!bo->ttm || !ttm_tt_is_populated(bo->ttm) ||
-				    bo->ttm->page_flags & TTM_PAGE_FLAG_SG ||
-				    bo->ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)
-					continue;
-
-				num_pages = bo->ttm->num_pages;
 				ret = ttm_bo_swapout(bo, ctx, gfp_flags);
 				/* ttm_bo_swapout has dropped the lru_lock */
 				if (!ret)
Signature · 1 line
2.25.1
#1 lore raw
Quoted 6 lines · Christian König

We need to grab the resv lock first before doing that check.

v2 (chk): simplify the change for -fixes

Hmm, OK, but this doesn't fix the swapped-bo-not-on-lru and unpopulating from swap_notify issues. Are you planning a follow up patch for those?

Thanks,

Thomas

Quoted 41 lines
---
  drivers/gpu/drm/ttm/ttm_bo.c     | 5 ++++-
  drivers/gpu/drm/ttm/ttm_device.c | 8 +-------
  2 files changed, 5 insertions(+), 8 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +4 -1
index cfd0b9292397..ebcffe794adb 100644
@@ -1172,7 +1172,10 @@ int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx,
  	if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked, NULL))
  		return -EBUSY;
 
-	if (!ttm_bo_get_unless_zero(bo)) {
+	if (!bo->ttm || !ttm_tt_is_populated(bo->ttm) ||
+	    bo->ttm->page_flags & TTM_PAGE_FLAG_SG ||
+	    bo->ttm->page_flags & TTM_PAGE_FLAG_SWAPPED ||
+	    !ttm_bo_get_unless_zero(bo)) {
  		if (locked)
  			dma_resv_unlock(bo->base.resv);
  		return -EBUSY;
drivers/gpu/drm/ttm/ttm_device.c +1 -7
index a1dcf7d55c90..3d9c62b93e29 100644
@@ -143,14 +143,8 @@ 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;
+				uint32_t num_pages = PFN_UP(bo->base.size);
 
-				if (!bo->ttm || !ttm_tt_is_populated(bo->ttm) ||
-				    bo->ttm->page_flags & TTM_PAGE_FLAG_SG ||
-				    bo->ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)
-					continue;
-
-				num_pages = bo->ttm->num_pages;
  				ret = ttm_bo_swapout(bo, ctx, gfp_flags);
  				/* ttm_bo_swapout has dropped the lru_lock */
  				if (!ret)
#2 lore raw

Am 28.05.21 um 15:06 schrieb Thomas Hellström:

Quoted 10 lines · Christian König
Quoted 6 lines

We need to grab the resv lock first before doing that check.

v2 (chk): simplify the change for -fixes

Hmm, OK, but this doesn't fix the swapped-bo-not-on-lru and unpopulating from swap_notify issues. Are you planning a follow up patch for those?

As discussed in a separate thread this needs to be applied as needed when the DG1 branch is merged.

Xinhui, Nirmoy, Rui can anybody give be an rb/ab so that I can push this?

Thanks,
Christian.

Quoted 45 lines

Thanks,

Thomas

Quoted 40 lines

---
  drivers/gpu/drm/ttm/ttm_bo.c     | 5 ++++-
  drivers/gpu/drm/ttm/ttm_device.c | 8 +-------

  2 files changed, 5 insertions(+), 8 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +4 -0
index cfd0b9292397..ebcffe794adb 100644
@@ -1172,7 +1172,10 @@ int ttm_bo_swapout(struct ttm_buffer_object 
 *bo, struct ttm_operation_ctx *ctx,
       if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked, NULL))
           return -EBUSY;
   -    if (!ttm_bo_get_unless_zero(bo)) {
+    if (!bo->ttm || !ttm_tt_is_populated(bo->ttm) ||
+        bo->ttm->page_flags & TTM_PAGE_FLAG_SG ||
+        bo->ttm->page_flags & TTM_PAGE_FLAG_SWAPPED ||
+        !ttm_bo_get_unless_zero(bo)) {
           if (locked)
               dma_resv_unlock(bo->base.resv);

          return -EBUSY;

drivers/gpu/drm/ttm/ttm_device.c +1 -1
index a1dcf7d55c90..3d9c62b93e29 100644
@@ -143,14 +143,8 @@ 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;
+                uint32_t num_pages = PFN_UP(bo->base.size);

  -                if (!bo->ttm || !ttm_tt_is_populated(bo->ttm) || -                    bo->ttm->page_flags & TTM_PAGE_FLAG_SG || -                    bo->ttm->page_flags & TTM_PAGE_FLAG_SWAPPED) -                    continue;
-
-                num_pages = bo->ttm->num_pages;
                  ret = ttm_bo_swapout(bo, ctx, gfp_flags); /* ttm_bo_swapout has dropped the lru_lock */ if (!ret)

#3 lore raw

[AMD Official Use Only]

Looks good to me.
________________________________
From: Christian König <[email protected]>
Sent: Monday, June 7, 2021 8:52:21 PM
To: [email protected] <[email protected]>; Pan, Xinhui <[email protected]>; Das, Nirmoy <[email protected]>; Huang, Ray <[email protected]>
Cc: Thomas Hellström <[email protected]>
Subject: Re: [PATCH] drm/ttm: fix deref of bo->ttm without holding the lock v2

Am 28.05.21 um 15:06 schrieb Thomas Hellström:

Quoted 12 lines · Christian König
Quoted 6 lines

We need to grab the resv lock first before doing that check.

v2 (chk): simplify the change for -fixes

Hmm, OK, but this doesn't fix the swapped-bo-not-on-lru and unpopulating from swap_notify issues. Are you planning a follow up patch for those?

As discussed in a separate thread this needs to be applied as needed when the DG1 branch is merged.

Xinhui, Nirmoy, Rui can anybody give be an rb/ab so that I can push this?

Thanks,
Christian.

Quoted 46 lines

Thanks,

Thomas

Quoted 41 lines
---
  drivers/gpu/drm/ttm/ttm_bo.c     | 5 ++++-
  drivers/gpu/drm/ttm/ttm_device.c | 8 +-------
  2 files changed, 5 insertions(+), 8 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +4 -0
index cfd0b9292397..ebcffe794adb 100644
@@ -1172,7 +1172,10 @@ int ttm_bo_swapout(struct ttm_buffer_object
 *bo, struct ttm_operation_ctx *ctx,
      if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked, NULL))
          return -EBUSY;
  -    if (!ttm_bo_get_unless_zero(bo)) {
+    if (!bo->ttm || !ttm_tt_is_populated(bo->ttm) ||
+        bo->ttm->page_flags & TTM_PAGE_FLAG_SG ||
+        bo->ttm->page_flags & TTM_PAGE_FLAG_SWAPPED ||
+        !ttm_bo_get_unless_zero(bo)) {
          if (locked)
              dma_resv_unlock(bo->base.resv);
          return -EBUSY;
drivers/gpu/drm/ttm/ttm_device.c +1 -6
index a1dcf7d55c90..3d9c62b93e29 100644
@@ -143,14 +143,8 @@ 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;
+                uint32_t num_pages = PFN_UP(bo->base.size);
  -                if (!bo->ttm || !ttm_tt_is_populated(bo->ttm) ||
-                    bo->ttm->page_flags & TTM_PAGE_FLAG_SG ||
-                    bo->ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)
-                    continue;
-
-                num_pages = bo->ttm->num_pages;
                  ret = ttm_bo_swapout(bo, ctx, gfp_flags);
                  /* ttm_bo_swapout has dropped the lru_lock */
                  if (!ret)
#4 lore raw

I take that as an reviewed-by :)

Thanks,
Christian.

Am 07.06.21 um 16:14 schrieb Pan, Xinhui:

Quoted 83 lines

[AMD Official Use Only]

Looks good to me.
------------------------------------------------------------------------
*From:* Christian König <[email protected]>
*Sent:* Monday, June 7, 2021 8:52:21 PM
*To:* [email protected] <[email protected]>; Pan, Xinhui <[email protected]>; Das, Nirmoy <[email protected]>; Huang, Ray <[email protected]> *Cc:* Thomas Hellström <[email protected]>
*Subject:* Re: [PATCH] drm/ttm: fix deref of bo->ttm without holding the lock v2

Am 28.05.21 um 15:06 schrieb Thomas Hellström:

Quoted 12 lines · Christian König
Quoted 6 lines

We need to grab the resv lock first before doing that check.

v2 (chk): simplify the change for -fixes

Hmm, OK, but this doesn't fix the swapped-bo-not-on-lru and unpopulating from swap_notify issues. Are you planning a follow up patch for those?

As discussed in a separate thread this needs to be applied as needed when the DG1 branch is merged.

Xinhui, Nirmoy, Rui can anybody give be an rb/ab so that I can push this?

Thanks,
Christian.

Quoted 11 lines

Thanks,

Thomas

Review

Quoted 6 lines

---
  drivers/gpu/drm/ttm/ttm_bo.c     | 5 ++++-
  drivers/gpu/drm/ttm/ttm_device.c | 8 +-------

  2 files changed, 5 insertions(+), 8 deletions(-)
(quoted diff) +0 -0

b/drivers/gpu/drm/ttm/ttm_bo.c

Quoted 35 lines
Quoted 35 lines

index cfd0b9292397..ebcffe794adb 100644

drivers/gpu/drm/ttm/ttm_bo.c +4 -0
@@ -1172,7 +1172,10 @@ int ttm_bo_swapout(struct ttm_buffer_object
 *bo, struct ttm_operation_ctx *ctx,
       if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked, NULL))
           return -EBUSY;
   -    if (!ttm_bo_get_unless_zero(bo)) {
+    if (!bo->ttm || !ttm_tt_is_populated(bo->ttm) ||
+        bo->ttm->page_flags & TTM_PAGE_FLAG_SG ||
+        bo->ttm->page_flags & TTM_PAGE_FLAG_SWAPPED ||
+        !ttm_bo_get_unless_zero(bo)) {
           if (locked)
 dma_resv_unlock(bo->base.resv);

          return -EBUSY;

drivers/gpu/drm/ttm/ttm_device.c +1 -1
index a1dcf7d55c90..3d9c62b93e29 100644
@@ -143,14 +143,8 @@ 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;
+                uint32_t num_pages = PFN_UP(bo->base.size);

  -                if (!bo->ttm || !ttm_tt_is_populated(bo->ttm) || -                    bo->ttm->page_flags & TTM_PAGE_FLAG_SG || -                    bo->ttm->page_flags & TTM_PAGE_FLAG_SWAPPED) -                    continue;
-
-                num_pages = bo->ttm->num_pages;
                  ret = ttm_bo_swapout(bo, ctx, gfp_flags); /* ttm_bo_swapout has dropped the lru_lock */ if (!ret)

Reviewed-by
#5 lore raw
In reply to Christian König

Looks good for me.

-----Original Message-----
From: Christian König <[email protected]>
Sent: Monday, June 7, 2021 8:52 PM
To: [email protected]; Pan, Xinhui <[email protected]>; Das, Nirmoy <[email protected]>; Huang, Ray <[email protected]>
Cc: Thomas Hellström <[email protected]>
Subject: Re: [PATCH] drm/ttm: fix deref of bo->ttm without holding the lock v2

Am 28.05.21 um 15:06 schrieb Thomas Hellström:

Quoted 12 lines · Christian König
Quoted 6 lines

We need to grab the resv lock first before doing that check.

v2 (chk): simplify the change for -fixes

Hmm, OK, but this doesn't fix the swapped-bo-not-on-lru and unpopulating from swap_notify issues. Are you planning a follow up patch for those?

As discussed in a separate thread this needs to be applied as needed when the DG1 branch is merged.

Xinhui, Nirmoy, Rui can anybody give be an rb/ab so that I can push this?

Thanks,
Christian.

Quoted 47 lines

Thanks,

Thomas

Quoted 42 lines

---
  drivers/gpu/drm/ttm/ttm_bo.c     | 5 ++++-
  drivers/gpu/drm/ttm/ttm_device.c | 8 +-------

  2 files changed, 5 insertions(+), 8 deletions(-)
(quoted diff) +0 -0

b/drivers/gpu/drm/ttm/ttm_bo.c index cfd0b9292397..ebcffe794adb 100644

drivers/gpu/drm/ttm/ttm_bo.c +4 -0
@@ -1172,7 +1172,10 @@ int ttm_bo_swapout(struct ttm_buffer_object 
 *bo, struct ttm_operation_ctx *ctx,
       if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked, NULL))
           return -EBUSY;
   -    if (!ttm_bo_get_unless_zero(bo)) {
+    if (!bo->ttm || !ttm_tt_is_populated(bo->ttm) ||
+        bo->ttm->page_flags & TTM_PAGE_FLAG_SG ||
+        bo->ttm->page_flags & TTM_PAGE_FLAG_SWAPPED ||
+        !ttm_bo_get_unless_zero(bo)) {
           if (locked)
               dma_resv_unlock(bo->base.resv);

          return -EBUSY;

drivers/gpu/drm/ttm/ttm_device.c +1 -1
index a1dcf7d55c90..3d9c62b93e29 100644
@@ -143,14 +143,8 @@ 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;
+                uint32_t num_pages = PFN_UP(bo->base.size);

  -                if (!bo->ttm || !ttm_tt_is_populated(bo->ttm) || -                    bo->ttm->page_flags & TTM_PAGE_FLAG_SG || -                    bo->ttm->page_flags & TTM_PAGE_FLAG_SWAPPED) -                    continue;
-
-                num_pages = bo->ttm->num_pages;
                  ret = ttm_bo_swapout(bo, ctx, gfp_flags); /* ttm_bo_swapout has dropped the lru_lock */ if (!ret)