Skip to contentSkip to conversation

drm/amdgpu: Fix incorrect return value

PATCH Superseded YiPeng Chai 3 participants 8 messages Apr 3, 2024 – Apr 12, 2024

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/amdgpu: Fix incorrect return value

[Why]
After calling amdgpu_vram_mgr_reserve_range
multiple times with the same address, calling amdgpu_vram_mgr_query_page_status will always return -EBUSY.
From the second call to amdgpu_vram_mgr_reserve_range,
the same address will be added to the reservations_pending list again and is never moved to the reserved_pages list because the address had been reserved.

[How]
First add the address status check before calling
amdgpu_vram_mgr_do_reserve, if the address is already reserved, do nothing; If the address is already in the reservations_pending list, directly reserve memory; only add new nodes for the addresses that are not in the reserved_pages list and reservations_pending list.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c | 28 +++++++++++++-------
 1 file changed, 19 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +19 -9
index 1e36c428d254..0bf3f4092900 100644
@@ -317,7 +317,6 @@ static void amdgpu_vram_mgr_do_reserve(struct ttm_resource_manager *man)
 
 		dev_dbg(adev->dev, "Reservation 0x%llx - %lld, Succeeded\n",
 			rsv->start, rsv->size);
-
 		vis_usage = amdgpu_vram_mgr_vis_size(adev, block);
 		atomic64_add(vis_usage, &mgr->vis_usage);
 		spin_lock(&man->bdev->lru_lock);
@@ -340,19 +339,30 @@ int amdgpu_vram_mgr_reserve_range(struct amdgpu_vram_mgr *mgr,
 				  uint64_t start, uint64_t size)
 {
 	struct amdgpu_vram_reservation *rsv;
+	int ret = 0;
 
-	rsv = kzalloc(sizeof(*rsv), GFP_KERNEL);
-	if (!rsv)
-		return -ENOMEM;
+	ret = amdgpu_vram_mgr_query_page_status(mgr, start);
+	if (!ret)
+		return 0;
+
+	if (ret == -ENOENT) {
+		rsv = kzalloc(sizeof(*rsv), GFP_KERNEL);
+		if (!rsv)
+			return -ENOMEM;
 
-	INIT_LIST_HEAD(&rsv->allocated);
-	INIT_LIST_HEAD(&rsv->blocks);
+		INIT_LIST_HEAD(&rsv->allocated);
+		INIT_LIST_HEAD(&rsv->blocks);
 
-	rsv->start = start;
-	rsv->size = size;
+		rsv->start = start;
+		rsv->size = size;
+
+		mutex_lock(&mgr->lock);
+		list_add_tail(&rsv->blocks, &mgr->reservations_pending);
+		mutex_unlock(&mgr->lock);
+
+	}
 
 	mutex_lock(&mgr->lock);
-	list_add_tail(&rsv->blocks, &mgr->reservations_pending);
 	amdgpu_vram_mgr_do_reserve(&mgr->manager);
 	mutex_unlock(&mgr->lock);
 
Signature · 1 line
2.34.1
#1 lore raw

[AMD Official Use Only - General]

Quoted 10 lines

-----Original Message-----
From: Chai, Thomas <[email protected]>
Sent: Wednesday, April 3, 2024 3:07 PM
To: [email protected]
Cc: Chai, Thomas <[email protected]>; Zhang, Hawking <[email protected]>; Zhou1, Tao <[email protected]>; Li, Candice <[email protected]>; Wang, Yang(Kevin) <[email protected]>; Yang, Stanley <[email protected]>; Chai, Thomas <[email protected]>
Subject: [PATCH] drm/amdgpu: Fix incorrect return value

Quoted 4 lines

[Why]
After calling amdgpu_vram_mgr_reserve_range multiple times with the same
address, calling amdgpu_vram_mgr_query_page_status will always return - EBUSY.

[Tao] could you explain why we call amdgpu_vram_mgr_reserve_range multiple times with the same
address? IIRC, we skip duplicate address before reserve memory.

Quoted 72 lines

From the second call to amdgpu_vram_mgr_reserve_range, the same address
will be added to the reservations_pending list again and is never moved to the reserved_pages list because the address had been reserved.

[How]
First add the address status check before calling
amdgpu_vram_mgr_do_reserve, if the address is already reserved, do nothing; If the address is already in the reservations_pending list, directly reserve memory; only add new nodes for the addresses that are not in the reserved_pages list and reservations_pending list.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c | 28 +++++++++++++-------
 1 file changed, 19 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +0 -1
index 1e36c428d254..0bf3f4092900 100644
@@ -317,7 +317,6 @@ static void amdgpu_vram_mgr_do_reserve(struct
 ttm_resource_manager *man)
 
              dev_dbg(adev->dev, "Reservation 0x%llx - %lld, Succeeded\n",
                      rsv->start, rsv->size);
-
              vis_usage = amdgpu_vram_mgr_vis_size(adev, block);
              atomic64_add(vis_usage, &mgr->vis_usage);
              spin_lock(&man->bdev->lru_lock);
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +19 -9
@@ -340,19 +339,30 @@ int amdgpu_vram_mgr_reserve_range(struct
 amdgpu_vram_mgr *mgr,
                                uint64_t start, uint64_t size)
 {
      struct amdgpu_vram_reservation *rsv;
+     int ret = 0;
 
-     rsv = kzalloc(sizeof(*rsv), GFP_KERNEL);
-     if (!rsv)
-             return -ENOMEM;
+     ret = amdgpu_vram_mgr_query_page_status(mgr, start);
+     if (!ret)
+             return 0;
+
+     if (ret == -ENOENT) {
+             rsv = kzalloc(sizeof(*rsv), GFP_KERNEL);
+             if (!rsv)
+                     return -ENOMEM;
 
-     INIT_LIST_HEAD(&rsv->allocated);
-     INIT_LIST_HEAD(&rsv->blocks);
+             INIT_LIST_HEAD(&rsv->allocated);
+             INIT_LIST_HEAD(&rsv->blocks);
 
-     rsv->start = start;
-     rsv->size = size;
+             rsv->start = start;
+             rsv->size = size;
+
+             mutex_lock(&mgr->lock);
+             list_add_tail(&rsv->blocks, &mgr->reservations_pending);
+             mutex_unlock(&mgr->lock);
+
+     }
 
      mutex_lock(&mgr->lock);
-     list_add_tail(&rsv->blocks, &mgr->reservations_pending);
      amdgpu_vram_mgr_do_reserve(&mgr->manager);
      mutex_unlock(&mgr->lock);
 
--

2.34.1

#2 lore raw

[AMD Official Use Only - General]

-----------------
Best Regards,
Thomas

-----Original Message-----
From: Zhou1, Tao <[email protected]>
Sent: Wednesday, April 3, 2024 6:36 PM
To: Chai, Thomas <[email protected]>; [email protected]
Cc: Zhang, Hawking <[email protected]>; Li, Candice <[email protected]>; Wang, Yang(Kevin) <[email protected]>; Yang, Stanley <[email protected]>
Subject: RE: [PATCH] drm/amdgpu: Fix incorrect return value

[AMD Official Use Only - General]

Quoted 11 lines

-----Original Message-----
From: Chai, Thomas <[email protected]>
Sent: Wednesday, April 3, 2024 3:07 PM
To: [email protected]
Cc: Chai, Thomas <[email protected]>; Zhang, Hawking <[email protected]>; Zhou1, Tao <[email protected]>; Li, Candice <[email protected]>; Wang, Yang(Kevin) <[email protected]>; Yang, Stanley <[email protected]>; Chai, Thomas <[email protected]>
Subject: [PATCH] drm/amdgpu: Fix incorrect return value

Quoted 6 lines

[Why]
After calling amdgpu_vram_mgr_reserve_range multiple times with the
same address, calling amdgpu_vram_mgr_query_page_status will always return - EBUSY.

[Tao] could you explain why we call amdgpu_vram_mgr_reserve_range multiple times with the same address? IIRC, we skip duplicate address before reserve memory.

[Thomas]
When poison creation interrupt is received, since some poisoning addresses may have been allocated by some processes, reserving these memories will fail.
These memory will be tried to reserve again after killing the poisoned process in the subsequent poisoning consumption interrupt handler. so amdgpu_vram_mgr_reserve_range needs to be called multiple times with the same address.

Quoted 72 lines

From the second call to amdgpu_vram_mgr_reserve_range, the same
address will be added to the reservations_pending list again and is never moved to the reserved_pages list because the address had been reserved.

[How]
First add the address status check before calling
amdgpu_vram_mgr_do_reserve, if the address is already reserved, do nothing; If the address is already in the reservations_pending list, directly reserve memory; only add new nodes for the addresses that are not in the reserved_pages list and reservations_pending list.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c | 28
+++++++++++++-------
 1 file changed, 19 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +0 -1
index 1e36c428d254..0bf3f4092900 100644
@@ -317,7 +317,6 @@ static void amdgpu_vram_mgr_do_reserve(struct
 ttm_resource_manager *man)
 
              dev_dbg(adev->dev, "Reservation 0x%llx - %lld, Succeeded\n",
                      rsv->start, rsv->size);
-
              vis_usage = amdgpu_vram_mgr_vis_size(adev, block);
              atomic64_add(vis_usage, &mgr->vis_usage);
              spin_lock(&man->bdev->lru_lock); @@ -340,19 +339,30 @@
int amdgpu_vram_mgr_reserve_range(struct
amdgpu_vram_mgr *mgr,
                                uint64_t start, uint64_t size)  {
      struct amdgpu_vram_reservation *rsv;
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +19 -9
+     int ret = 0;
 
-     rsv = kzalloc(sizeof(*rsv), GFP_KERNEL);
-     if (!rsv)
-             return -ENOMEM;
+     ret = amdgpu_vram_mgr_query_page_status(mgr, start);
+     if (!ret)
+             return 0;
+
+     if (ret == -ENOENT) {
+             rsv = kzalloc(sizeof(*rsv), GFP_KERNEL);
+             if (!rsv)
+                     return -ENOMEM;
 
-     INIT_LIST_HEAD(&rsv->allocated);
-     INIT_LIST_HEAD(&rsv->blocks);
+             INIT_LIST_HEAD(&rsv->allocated);
+             INIT_LIST_HEAD(&rsv->blocks);
 
-     rsv->start = start;
-     rsv->size = size;
+             rsv->start = start;
+             rsv->size = size;
+
+             mutex_lock(&mgr->lock);
+             list_add_tail(&rsv->blocks, &mgr->reservations_pending);
+             mutex_unlock(&mgr->lock);
+
+     }
 
      mutex_lock(&mgr->lock);
-     list_add_tail(&rsv->blocks, &mgr->reservations_pending);
      amdgpu_vram_mgr_do_reserve(&mgr->manager);
      mutex_unlock(&mgr->lock);
 
--

2.34.1

#3 lore raw

[AMD Official Use Only - General]

Quoted 54 lines

-----Original Message-----
From: Chai, Thomas <[email protected]>
Sent: Sunday, April 7, 2024 10:21 AM
To: Zhou1, Tao <[email protected]>; [email protected]
Cc: Zhang, Hawking <[email protected]>; Li, Candice <[email protected]>; Wang, Yang(Kevin) <[email protected]>; Yang, Stanley <[email protected]>
Subject: RE: [PATCH] drm/amdgpu: Fix incorrect return value

[AMD Official Use Only - General]

-----------------
Best Regards,
Thomas

-----Original Message-----
From: Zhou1, Tao <[email protected]>
Sent: Wednesday, April 3, 2024 6:36 PM
To: Chai, Thomas <[email protected]>; [email protected]
Cc: Zhang, Hawking <[email protected]>; Li, Candice <[email protected]>; Wang, Yang(Kevin) <[email protected]>; Yang, Stanley <[email protected]>
Subject: RE: [PATCH] drm/amdgpu: Fix incorrect return value

[AMD Official Use Only - General]

Quoted 11 lines

-----Original Message-----
From: Chai, Thomas <[email protected]>
Sent: Wednesday, April 3, 2024 3:07 PM
To: [email protected]
Cc: Chai, Thomas <[email protected]>; Zhang, Hawking <[email protected]>; Zhou1, Tao <[email protected]>; Li, Candice <[email protected]>; Wang, Yang(Kevin) <[email protected]>; Yang, Stanley <[email protected]>; Chai, Thomas <[email protected]>
Subject: [PATCH] drm/amdgpu: Fix incorrect return value

Quoted 6 lines

[Why]
After calling amdgpu_vram_mgr_reserve_range multiple times with the
same address, calling amdgpu_vram_mgr_query_page_status will always return - EBUSY.

[Tao] could you explain why we call amdgpu_vram_mgr_reserve_range multiple

times with the same address? IIRC, we skip duplicate address before reserve memory.

[Thomas]
When poison creation interrupt is received, since some poisoning addresses may
have been allocated by some processes, reserving these memories will fail. These memory will be tried to reserve again after killing the poisoned process in the subsequent poisoning consumption interrupt handler.
so amdgpu_vram_mgr_reserve_range needs to be called multiple times with the same address.

Quoted 4 lines
Quoted 3 lines

From the second call to amdgpu_vram_mgr_reserve_range, the same
address will be added to the reservations_pending list again and is never moved to the reserved_pages list because the address had been

reserved.

[Tao] but if a page is added to reservations_pending list, it should also be put in data->bps array, and when we call amdgpu_ras_add_bad_pages again, amdgpu_ras_check_bad_page_unlock could ignore this page. So except for amdgpu_ras_add_bad_pages, would you like to call amdgpu_vram_mgr_reserve_range in other place?

Quoted 71 lines
Quoted 69 lines

[How]
First add the address status check before calling
amdgpu_vram_mgr_do_reserve, if the address is already reserved, do nothing; If the address is already in the reservations_pending list, directly reserve memory; only add new nodes for the addresses that are not in the reserved_pages list and reservations_pending list.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c | 28
+++++++++++++-------
 1 file changed, 19 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +0 -1
index 1e36c428d254..0bf3f4092900 100644
@@ -317,7 +317,6 @@ static void amdgpu_vram_mgr_do_reserve(struct
 ttm_resource_manager *man)
 
              dev_dbg(adev->dev, "Reservation 0x%llx - %lld, Succeeded\n",
                      rsv->start, rsv->size);
-
              vis_usage = amdgpu_vram_mgr_vis_size(adev, block);
              atomic64_add(vis_usage, &mgr->vis_usage);
              spin_lock(&man->bdev->lru_lock); @@ -340,19 +339,30 @@
int amdgpu_vram_mgr_reserve_range(struct
amdgpu_vram_mgr *mgr,
                                uint64_t start, uint64_t size)  {
      struct amdgpu_vram_reservation *rsv;
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +19 -9
+     int ret = 0;
 
-     rsv = kzalloc(sizeof(*rsv), GFP_KERNEL);
-     if (!rsv)
-             return -ENOMEM;
+     ret = amdgpu_vram_mgr_query_page_status(mgr, start);
+     if (!ret)
+             return 0;
+
+     if (ret == -ENOENT) {
+             rsv = kzalloc(sizeof(*rsv), GFP_KERNEL);
+             if (!rsv)
+                     return -ENOMEM;
 
-     INIT_LIST_HEAD(&rsv->allocated);
-     INIT_LIST_HEAD(&rsv->blocks);
+             INIT_LIST_HEAD(&rsv->allocated);
+             INIT_LIST_HEAD(&rsv->blocks);
 
-     rsv->start = start;
-     rsv->size = size;
+             rsv->start = start;
+             rsv->size = size;
+
+             mutex_lock(&mgr->lock);
+             list_add_tail(&rsv->blocks, &mgr->reservations_pending);
+             mutex_unlock(&mgr->lock);
+
+     }
 
      mutex_lock(&mgr->lock);
-     list_add_tail(&rsv->blocks, &mgr->reservations_pending);
      amdgpu_vram_mgr_do_reserve(&mgr->manager);
      mutex_unlock(&mgr->lock);
 
--

2.34.1

#4 lore raw

[AMD Official Use Only - General]

-----------------
Best Regards,
Thomas

-----Original Message-----
From: Zhou1, Tao <[email protected]>
Sent: Monday, April 8, 2024 4:41 PM
To: Chai, Thomas <[email protected]>; [email protected]
Cc: Zhang, Hawking <[email protected]>; Li, Candice <[email protected]>; Wang, Yang(Kevin) <[email protected]>; Yang, Stanley <[email protected]>
Subject: RE: [PATCH] drm/amdgpu: Fix incorrect return value

[AMD Official Use Only - General]

Quoted 55 lines

-----Original Message-----
From: Chai, Thomas <[email protected]>
Sent: Sunday, April 7, 2024 10:21 AM
To: Zhou1, Tao <[email protected]>; [email protected]
Cc: Zhang, Hawking <[email protected]>; Li, Candice <[email protected]>; Wang, Yang(Kevin) <[email protected]>; Yang, Stanley <[email protected]>
Subject: RE: [PATCH] drm/amdgpu: Fix incorrect return value

[AMD Official Use Only - General]

-----------------
Best Regards,
Thomas

-----Original Message-----
From: Zhou1, Tao <[email protected]>
Sent: Wednesday, April 3, 2024 6:36 PM
To: Chai, Thomas <[email protected]>; [email protected]
Cc: Zhang, Hawking <[email protected]>; Li, Candice <[email protected]>; Wang, Yang(Kevin) <[email protected]>; Yang, Stanley <[email protected]>
Subject: RE: [PATCH] drm/amdgpu: Fix incorrect return value

[AMD Official Use Only - General]

Quoted 11 lines

-----Original Message-----
From: Chai, Thomas <[email protected]>
Sent: Wednesday, April 3, 2024 3:07 PM
To: [email protected]
Cc: Chai, Thomas <[email protected]>; Zhang, Hawking <[email protected]>; Zhou1, Tao <[email protected]>; Li, Candice <[email protected]>; Wang, Yang(Kevin) <[email protected]>; Yang, Stanley <[email protected]>; Chai, Thomas <[email protected]>
Subject: [PATCH] drm/amdgpu: Fix incorrect return value

Quoted 7 lines

[Why]
After calling amdgpu_vram_mgr_reserve_range multiple times with
the same address, calling amdgpu_vram_mgr_query_page_status will always return - EBUSY.

[Tao] could you explain why we call amdgpu_vram_mgr_reserve_range multiple

times with the same address? IIRC, we skip duplicate address before reserve memory.

[Thomas]
When poison creation interrupt is received, since some poisoning
addresses may have been allocated by some processes, reserving these memories will fail. These memory will be tried to reserve again after killing the poisoned process in the subsequent poisoning consumption interrupt handler.
so amdgpu_vram_mgr_reserve_range needs to be called multiple times
with the same address.

Quoted 6 lines
Quoted 3 lines

From the second call to amdgpu_vram_mgr_reserve_range, the same
address will be added to the reservations_pending list again and is never moved to the reserved_pages list because the address had been

reserved.

[Tao] but if a page is added to reservations_pending list, it should also be put in data->bps array, and when we call amdgpu_ras_add_bad_pages again, amdgpu_ras_check_bad_page_unlock could ignore this page.

So except for amdgpu_ras_add_bad_pages, would you like to call amdgpu_vram_mgr_reserve_range in other place?

[Thomas] Yes, Since after amdgpu_ras_add_bad_pages is called, the bad pages will be saved to eeprom. When a large number of bad pages need to be reserved, this will delay subsequent memory reservation.
I want to call amdgpu_vram_mgr_reserve_range to reserve memory immediately when driver receives poison creation interrupt, this can reduce the probability of bad memory pages being allocated and storing the bad pages to eeprom can be done slowly.

Quoted 71 lines
Quoted 69 lines

[How]
First add the address status check before calling
amdgpu_vram_mgr_do_reserve, if the address is already reserved, do nothing; If the address is already in the reservations_pending list, directly reserve memory; only add new nodes for the addresses that are not in the reserved_pages list and reservations_pending list.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c | 28
+++++++++++++-------
 1 file changed, 19 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +0 -1
index 1e36c428d254..0bf3f4092900 100644
@@ -317,7 +317,6 @@ static void amdgpu_vram_mgr_do_reserve(struct
 ttm_resource_manager *man)
 
              dev_dbg(adev->dev, "Reservation 0x%llx - %lld, Succeeded\n",
                      rsv->start, rsv->size);
-
              vis_usage = amdgpu_vram_mgr_vis_size(adev, block);
              atomic64_add(vis_usage, &mgr->vis_usage);
              spin_lock(&man->bdev->lru_lock); @@ -340,19 +339,30 @@
int amdgpu_vram_mgr_reserve_range(struct
amdgpu_vram_mgr *mgr,
                                uint64_t start, uint64_t size)  {
      struct amdgpu_vram_reservation *rsv;
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +19 -9
+     int ret = 0;
 
-     rsv = kzalloc(sizeof(*rsv), GFP_KERNEL);
-     if (!rsv)
-             return -ENOMEM;
+     ret = amdgpu_vram_mgr_query_page_status(mgr, start);
+     if (!ret)
+             return 0;
+
+     if (ret == -ENOENT) {
+             rsv = kzalloc(sizeof(*rsv), GFP_KERNEL);
+             if (!rsv)
+                     return -ENOMEM;
 
-     INIT_LIST_HEAD(&rsv->allocated);
-     INIT_LIST_HEAD(&rsv->blocks);
+             INIT_LIST_HEAD(&rsv->allocated);
+             INIT_LIST_HEAD(&rsv->blocks);
 
-     rsv->start = start;
-     rsv->size = size;
+             rsv->start = start;
+             rsv->size = size;
+
+             mutex_lock(&mgr->lock);
+             list_add_tail(&rsv->blocks, &mgr->reservations_pending);
+             mutex_unlock(&mgr->lock);
+
+     }
 
      mutex_lock(&mgr->lock);
-     list_add_tail(&rsv->blocks, &mgr->reservations_pending);
      amdgpu_vram_mgr_do_reserve(&mgr->manager);
      mutex_unlock(&mgr->lock);
 
--

2.34.1

#5 lore raw
In reply to YiPeng Chai

[AMD Official Use Only - General]

Quoted 66 lines

-----Original Message-----
From: Chai, Thomas <[email protected]>
Sent: Wednesday, April 3, 2024 3:07 PM
To: [email protected]
Cc: Chai, Thomas <[email protected]>; Zhang, Hawking <[email protected]>; Zhou1, Tao <[email protected]>; Li, Candice <[email protected]>; Wang, Yang(Kevin) <[email protected]>; Yang, Stanley <[email protected]>; Chai, Thomas <[email protected]>
Subject: [PATCH] drm/amdgpu: Fix incorrect return value

[Why]
After calling amdgpu_vram_mgr_reserve_range multiple times with the same
address, calling amdgpu_vram_mgr_query_page_status will always return - EBUSY.
From the second call to amdgpu_vram_mgr_reserve_range, the same address
will be added to the reservations_pending list again and is never moved to the reserved_pages list because the address had been reserved.

[How]
First add the address status check before calling
amdgpu_vram_mgr_do_reserve, if the address is already reserved, do nothing; If the address is already in the reservations_pending list, directly reserve memory; only add new nodes for the addresses that are not in the reserved_pages list and reservations_pending list.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c | 28 +++++++++++++-------
 1 file changed, 19 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +0 -1
index 1e36c428d254..0bf3f4092900 100644
@@ -317,7 +317,6 @@ static void amdgpu_vram_mgr_do_reserve(struct
 ttm_resource_manager *man)
 
              dev_dbg(adev->dev, "Reservation 0x%llx - %lld, Succeeded\n",
                      rsv->start, rsv->size);
-
              vis_usage = amdgpu_vram_mgr_vis_size(adev, block);
              atomic64_add(vis_usage, &mgr->vis_usage);
              spin_lock(&man->bdev->lru_lock);
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +10 -5
@@ -340,19 +339,30 @@ int amdgpu_vram_mgr_reserve_range(struct
 amdgpu_vram_mgr *mgr,
                                uint64_t start, uint64_t size)
 {
      struct amdgpu_vram_reservation *rsv;
+     int ret = 0;
 
-     rsv = kzalloc(sizeof(*rsv), GFP_KERNEL);
-     if (!rsv)
-             return -ENOMEM;
+     ret = amdgpu_vram_mgr_query_page_status(mgr, start);
+     if (!ret)
+             return 0;
+
+     if (ret == -ENOENT) {
+             rsv = kzalloc(sizeof(*rsv), GFP_KERNEL);
+             if (!rsv)
+                     return -ENOMEM;
 
-     INIT_LIST_HEAD(&rsv->allocated);
-     INIT_LIST_HEAD(&rsv->blocks);
+             INIT_LIST_HEAD(&rsv->allocated);

drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c int amdgpu_vram_mgr_reserve_range(struct · line 362

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +7 -2
+             INIT_LIST_HEAD(&rsv->blocks);
 
-     rsv->start = start;
-     rsv->size = size;
+             rsv->start = start;
+             rsv->size = size;
+
+             mutex_lock(&mgr->lock);
+             list_add_tail(&rsv->blocks, &mgr->reservations_pending);
+             mutex_unlock(&mgr->lock);

[Tao] we can drop the mutex_unlock and add if (ret != -ENOENT) for the second mutex_lock to avoid unlocking/locking repeatedly.

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +2 -2
+
+     }
 
      mutex_lock(&mgr->lock);
-     list_add_tail(&rsv->blocks, &mgr->reservations_pending);
      amdgpu_vram_mgr_do_reserve(&mgr->manager);
      mutex_unlock(&mgr->lock);
 
--

2.34.1

#6 lore raw

[AMD Official Use Only - General]

OK

-----------------
Best Regards,
Thomas

-----Original Message-----
From: Zhou1, Tao <[email protected]>
Sent: Tuesday, April 9, 2024 10:52 AM
To: Chai, Thomas <[email protected]>; [email protected]
Cc: Zhang, Hawking <[email protected]>; Li, Candice <[email protected]>; Wang, Yang(Kevin) <[email protected]>; Yang, Stanley <[email protected]>
Subject: RE: [PATCH] drm/amdgpu: Fix incorrect return value

[AMD Official Use Only - General]

Quoted 67 lines

-----Original Message-----
From: Chai, Thomas <[email protected]>
Sent: Wednesday, April 3, 2024 3:07 PM
To: [email protected]
Cc: Chai, Thomas <[email protected]>; Zhang, Hawking <[email protected]>; Zhou1, Tao <[email protected]>; Li, Candice <[email protected]>; Wang, Yang(Kevin) <[email protected]>; Yang, Stanley <[email protected]>; Chai, Thomas <[email protected]>
Subject: [PATCH] drm/amdgpu: Fix incorrect return value

[Why]
After calling amdgpu_vram_mgr_reserve_range multiple times with the
same address, calling amdgpu_vram_mgr_query_page_status will always return - EBUSY.
From the second call to amdgpu_vram_mgr_reserve_range, the same
address will be added to the reservations_pending list again and is never moved to the reserved_pages list because the address had been reserved.

[How]
First add the address status check before calling
amdgpu_vram_mgr_do_reserve, if the address is already reserved, do nothing; If the address is already in the reservations_pending list, directly reserve memory; only add new nodes for the addresses that are not in the reserved_pages list and reservations_pending list.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c | 28
+++++++++++++-------
 1 file changed, 19 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +0 -1
index 1e36c428d254..0bf3f4092900 100644
@@ -317,7 +317,6 @@ static void amdgpu_vram_mgr_do_reserve(struct
 ttm_resource_manager *man)
 
              dev_dbg(adev->dev, "Reservation 0x%llx - %lld, Succeeded\n",
                      rsv->start, rsv->size);
-
              vis_usage = amdgpu_vram_mgr_vis_size(adev, block);
              atomic64_add(vis_usage, &mgr->vis_usage);
              spin_lock(&man->bdev->lru_lock); @@ -340,19 +339,30 @@
int amdgpu_vram_mgr_reserve_range(struct
amdgpu_vram_mgr *mgr,
                                uint64_t start, uint64_t size)  {
      struct amdgpu_vram_reservation *rsv;
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +10 -5
+     int ret = 0;
 
-     rsv = kzalloc(sizeof(*rsv), GFP_KERNEL);
-     if (!rsv)
-             return -ENOMEM;
+     ret = amdgpu_vram_mgr_query_page_status(mgr, start);
+     if (!ret)
+             return 0;
+
+     if (ret == -ENOENT) {
+             rsv = kzalloc(sizeof(*rsv), GFP_KERNEL);
+             if (!rsv)
+                     return -ENOMEM;
 
-     INIT_LIST_HEAD(&rsv->allocated);
-     INIT_LIST_HEAD(&rsv->blocks);
+             INIT_LIST_HEAD(&rsv->allocated);

drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c int amdgpu_vram_mgr_reserve_range(struct amdgpu_vram_mgr *mgr, · line 361

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +7 -2
+             INIT_LIST_HEAD(&rsv->blocks);
 
-     rsv->start = start;
-     rsv->size = size;
+             rsv->start = start;
+             rsv->size = size;
+
+             mutex_lock(&mgr->lock);
+             list_add_tail(&rsv->blocks, &mgr->reservations_pending);
+             mutex_unlock(&mgr->lock);

[Tao] we can drop the mutex_unlock and add if (ret != -ENOENT) for the second mutex_lock to avoid unlocking/locking repeatedly.

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +2 -2
+
+     }
 
      mutex_lock(&mgr->lock);
-     list_add_tail(&rsv->blocks, &mgr->reservations_pending);
      amdgpu_vram_mgr_do_reserve(&mgr->manager);
      mutex_unlock(&mgr->lock);
 
--

2.34.1

#7 lore raw
In reply to YiPeng Chai

Am 03.04.24 um 09:06 schrieb YiPeng Chai:

Quoted 9 lines

[Why]
After calling amdgpu_vram_mgr_reserve_range
multiple times with the same address, calling amdgpu_vram_mgr_query_page_status will always return -EBUSY.
From the second call to amdgpu_vram_mgr_reserve_range,
the same address will be added to the reservations_pending list again and is never moved to the reserved_pages list because the address had been reserved.

Well that sounds like a really bad idea to me. Why is the function called multiple times with the same address in the first place ?

Apart from that a note on the coding style below.

Quoted 27 lines

[How]
First add the address status check before calling
amdgpu_vram_mgr_do_reserve, if the address is already reserved, do nothing; If the address is already in the reservations_pending list, directly reserve memory; only add new nodes for the addresses that are not in the reserved_pages list and reservations_pending list.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c | 28 +++++++++++++-------
  1 file changed, 19 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +0 -1
index 1e36c428d254..0bf3f4092900 100644
@@ -317,7 +317,6 @@ static void amdgpu_vram_mgr_do_reserve(struct ttm_resource_manager *man)
 
  		dev_dbg(adev->dev, "Reservation 0x%llx - %lld, Succeeded\n",
  			rsv->start, rsv->size);
-
  		vis_usage = amdgpu_vram_mgr_vis_size(adev, block);
  		atomic64_add(vis_usage, &mgr->vis_usage);
  		spin_lock(&man->bdev->lru_lock);

drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c int amdgpu_vram_mgr_reserve_range(struct amdgpu_vram_mgr *mgr, · line 342

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +1 -0
@@ -340,19 +339,30 @@ int amdgpu_vram_mgr_reserve_range(struct amdgpu_vram_mgr *mgr,
  				  uint64_t start, uint64_t size)
  {
  	struct amdgpu_vram_reservation *rsv;
+	int ret = 0;

Don't initialize local variables when it isn't necessary.

Quoted 18 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +10 -5
-	rsv = kzalloc(sizeof(*rsv), GFP_KERNEL);
-	if (!rsv)
-		return -ENOMEM;
+	ret = amdgpu_vram_mgr_query_page_status(mgr, start);
+	if (!ret)
+		return 0;
+
+	if (ret == -ENOENT) {
+		rsv = kzalloc(sizeof(*rsv), GFP_KERNEL);
+		if (!rsv)
+			return -ENOMEM;
 
-	INIT_LIST_HEAD(&rsv->allocated);
-	INIT_LIST_HEAD(&rsv->blocks);
+		INIT_LIST_HEAD(&rsv->allocated);
+		INIT_LIST_HEAD(&rsv->blocks);
 

drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c int amdgpu_vram_mgr_reserve_range(struct amdgpu_vram_mgr *mgr, · line 363

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +8 -2
-	rsv->start = start;
-	rsv->size = size;
+		rsv->start = start;
+		rsv->size = size;
+
+		mutex_lock(&mgr->lock);
+		list_add_tail(&rsv->blocks, &mgr->reservations_pending);
+		mutex_unlock(&mgr->lock);
+
+	}

You should probably not lock/unlock here.

Regards,
Christian.

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +0 -1
  	mutex_lock(&mgr->lock);
-	list_add_tail(&rsv->blocks, &mgr->reservations_pending);
  	amdgpu_vram_mgr_do_reserve(&mgr->manager);
  	mutex_unlock(&mgr->lock);