Skip to contentSkip to conversation

drm/amdgpu: bound bad-page list writes to allocated capacity

PATCH In review Chenglei Xie 2 participants 10 messages May 7 – May 26

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

PATCH 1/1 drm/amdgpu: bound bad-page list writes to allocated capacity

PF2VF bad-page data can provide more entries than the VF-side bad-page buffer can hold. amdgpu_virt_ras_add_bps() copied entries without checking remaining capacity, which can overrun the 512-entry allocation.

Add an explicit max bad-page constant, validate pages against remaining space before memcpy(), and make the helper return bool so the caller stops processing when the buffer is full. This preserves normal behavior while preventing overwrite from malformed or malicious input.

Change-Id I924d2b5e369bef007eb1577f287a2274ec83d5a9
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c | 53 +++++++++++++++---------
 1 file changed, 33 insertions(+), 20 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c +33 -20
index 6974b1c5b56c2..8ac808d70356c 100644
@@ -283,14 +283,12 @@ unsigned int amd_sriov_msg_checksum(void *obj,
 	return ret;
 }
 
+#define AMDGPU_VIRT_RAS_MAX_BAD_PAGES 512
+
 static int amdgpu_virt_init_ras_err_handler_data(struct amdgpu_device *adev)
 {
 	struct amdgpu_virt *virt = &adev->virt;
 	struct amdgpu_virt_ras_err_handler_data **data = &virt->virt_eh_data;
-	/* GPU will be marked bad on host if bp count more then 10,
-	 * so alloc 512 is enough.
-	 */
-	unsigned int align_space = 512;
 	void *bps = NULL;
 	struct amdgpu_bo **bps_bo = NULL;
 
@@ -298,11 +296,14 @@ static int amdgpu_virt_init_ras_err_handler_data(struct amdgpu_device *adev)
 	if (!*data)
 		goto data_failure;
 
-	bps = kmalloc_array(align_space, sizeof(*(*data)->bps), GFP_KERNEL);
+	/* GPU will be marked bad on host if bp count more then 10,
+	 * so alloc 512 is enough.
+	 */
+	bps = kmalloc_array(AMDGPU_VIRT_RAS_MAX_BAD_PAGES, sizeof(*(*data)->bps), GFP_KERNEL);
 	if (!bps)
 		goto bps_failure;
 
-	bps_bo = kmalloc_array(align_space, sizeof(*(*data)->bps_bo), GFP_KERNEL);
+	bps_bo = kmalloc_array(AMDGPU_VIRT_RAS_MAX_BAD_PAGES, sizeof(*(*data)->bps_bo), GFP_KERNEL);
 	if (!bps_bo)
 		goto bps_bo_failure;
 
@@ -361,17 +362,27 @@ void amdgpu_virt_release_ras_err_handler_data(struct amdgpu_device *adev)
 	virt->virt_eh_data = NULL;
 }
 
-static void amdgpu_virt_ras_add_bps(struct amdgpu_device *adev,
-		struct eeprom_table_record *bps, int pages)
+static bool amdgpu_virt_ras_add_bps(struct amdgpu_device *adev,
+		const struct eeprom_table_record *bps, int pages)
 {
 	struct amdgpu_virt *virt = &adev->virt;
 	struct amdgpu_virt_ras_err_handler_data *data = virt->virt_eh_data;
 
 	if (!data)
-		return;
+		return false;
+
+	if (pages > AMDGPU_VIRT_RAS_MAX_BAD_PAGES - data->count) {
+		dev_warn_ratelimited(adev->dev,
+				     "RAS WARN: bad page buffer full, count=%d pages=%d max=%d\n",
+				     data->count, pages,
+				     AMDGPU_VIRT_RAS_MAX_BAD_PAGES);
+		return false;
+	}
 
 	memcpy(&data->bps[data->count], bps, pages * sizeof(*data->bps));
 	data->count += pages;
+
+	return true;
 }
 
 static void amdgpu_virt_ras_reserve_bps(struct amdgpu_device *adev)
@@ -443,20 +454,22 @@ static void amdgpu_virt_add_bad_page(struct amdgpu_device *adev,
 
 	memset(&bp, 0, sizeof(bp));
 
-	if (bp_block_size) {
-		bp_cnt = bp_block_size / sizeof(uint64_t);
-		for (bp_idx = 0; bp_idx < bp_cnt; bp_idx++) {
-			retired_page = *(uint64_t *)(vram_usage_va +
-					bp_block_offset + bp_idx * sizeof(uint64_t));
-			bp.retired_page = retired_page;
+	if (!bp_block_size)
+		return;
 
-			if (amdgpu_virt_ras_check_bad_page(adev, retired_page))
-				continue;
+	bp_cnt = bp_block_size / sizeof(uint64_t);
+	for (bp_idx = 0; bp_idx < bp_cnt; bp_idx++) {
+		retired_page = *(uint64_t *)(vram_usage_va +
+				bp_block_offset + bp_idx * sizeof(uint64_t));
+		bp.retired_page = retired_page;
 
-			amdgpu_virt_ras_add_bps(adev, &bp, 1);
+		if (amdgpu_virt_ras_check_bad_page(adev, retired_page))
+			continue;
 
-			amdgpu_virt_ras_reserve_bps(adev);
-		}
+		if (!amdgpu_virt_ras_add_bps(adev, &bp, 1))
+			break;
+
+		amdgpu_virt_ras_reserve_bps(adev);
 	}
 }
 
Signature · 1 line
2.34.1
#1 lore raw

AMD General

Hi team,

Could anyone help review this security bug fix patch for https://ontrack-internal.amd.com/browse/SWDEV-589525.

Thanks,
Chenglei

-----Original Message-----
From: Xie, Chenglei <[email protected]>
Sent: Thursday, May 7, 2026 10:45 AM
To: [email protected]
Cc: Chan, Hing Pong <[email protected]>; Luo, Zhigang <[email protected]>; Deucher, Alexander <[email protected]>; Xie, Chenglei <[email protected]>
Subject: [PATCH] drm/amdgpu: bound bad-page list writes to allocated capacity

PF2VF bad-page data can provide more entries than the VF-side bad-page buffer can hold. amdgpu_virt_ras_add_bps() copied entries without checking remaining capacity, which can overrun the 512-entry allocation.

Add an explicit max bad-page constant, validate pages against remaining space before memcpy(), and make the helper return bool so the caller stops processing when the buffer is full. This preserves normal behavior while preventing overwrite from malformed or malicious input.

Change-Id I924d2b5e369bef007eb1577f287a2274ec83d5a9
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c | 53 +++++++++++++++---------
 1 file changed, 33 insertions(+), 20 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c +9 -6
index 6974b1c5b56c2..8ac808d70356c 100644
@@ -283,14 +283,12 @@ unsigned int amd_sriov_msg_checksum(void *obj,
        return ret;
 }
 
+#define AMDGPU_VIRT_RAS_MAX_BAD_PAGES 512
+
 static int amdgpu_virt_init_ras_err_handler_data(struct amdgpu_device *adev)  {
        struct amdgpu_virt *virt = &adev->virt;
        struct amdgpu_virt_ras_err_handler_data **data = &virt->virt_eh_data;
-       /* GPU will be marked bad on host if bp count more then 10,
-        * so alloc 512 is enough.
-        */
-       unsigned int align_space = 512;
        void *bps = NULL;
        struct amdgpu_bo **bps_bo = NULL;
 
@@ -298,11 +296,14 @@ static int amdgpu_virt_init_ras_err_handler_data(struct amdgpu_device *adev)
        if (!*data)
                goto data_failure;
 
-       bps = kmalloc_array(align_space, sizeof(*(*data)->bps), GFP_KERNEL);
+       /* GPU will be marked bad on host if bp count more then 10,
+        * so alloc 512 is enough.
+        */
+       bps = kmalloc_array(AMDGPU_VIRT_RAS_MAX_BAD_PAGES,
+sizeof(*(*data)->bps), GFP_KERNEL);
        if (!bps)
                goto bps_failure;
 
-       bps_bo = kmalloc_array(align_space, sizeof(*(*data)->bps_bo), GFP_KERNEL);
+       bps_bo = kmalloc_array(AMDGPU_VIRT_RAS_MAX_BAD_PAGES,
+sizeof(*(*data)->bps_bo), GFP_KERNEL);
        if (!bps_bo)
                goto bps_bo_failure;

@@ -361,17 +362,27 @@ void amdgpu_virt_release_ras_err_handler_data(struct amdgpu_device *adev)

        virt->virt_eh_data = NULL;

}

-static void amdgpu_virt_ras_add_bps(struct amdgpu_device *adev,
- struct eeprom_table_record *bps, int pages)
+static bool amdgpu_virt_ras_add_bps(struct amdgpu_device *adev,
+ const struct eeprom_table_record *bps, int pages)

 {
        struct amdgpu_virt *virt = &adev->virt;
        struct amdgpu_virt_ras_err_handler_data *data = virt->virt_eh_data;

        if (!data)

- return;
+ return false;
+
+ if (pages > AMDGPU_VIRT_RAS_MAX_BAD_PAGES - data->count) {
+ dev_warn_ratelimited(adev->dev,

+                                    "RAS WARN: bad page buffer full, count=%d pages=%d max=%d\n",
+                                    data->count, pages,
+                                    AMDGPU_VIRT_RAS_MAX_BAD_PAGES);

+ return false;
+ }

        memcpy(&data->bps[data->count], bps, pages * sizeof(*data->bps));
        data->count += pages;

+
+ return true;
}

static void amdgpu_virt_ras_reserve_bps(struct amdgpu_device *adev) @@ -443,20 +454,22 @@ static void amdgpu_virt_add_bad_page(struct amdgpu_device *adev,

        memset(&bp, 0, sizeof(bp));

- if (bp_block_size) {
- bp_cnt = bp_block_size / sizeof(uint64_t);
- for (bp_idx = 0; bp_idx < bp_cnt; bp_idx++) {
- retired_page = *(uint64_t *)(vram_usage_va +
- bp_block_offset + bp_idx * sizeof(uint64_t));
- bp.retired_page = retired_page;
+ if (!bp_block_size)
+ return;

- if (amdgpu_virt_ras_check_bad_page(adev, retired_page))
- continue;
+ bp_cnt = bp_block_size / sizeof(uint64_t);
+ for (bp_idx = 0; bp_idx < bp_cnt; bp_idx++) {
+ retired_page = *(uint64_t *)(vram_usage_va +
+ bp_block_offset + bp_idx * sizeof(uint64_t));
+ bp.retired_page = retired_page;

- amdgpu_virt_ras_add_bps(adev, &bp, 1);
+ if (amdgpu_virt_ras_check_bad_page(adev, retired_page))
+ continue;

- amdgpu_virt_ras_reserve_bps(adev);
- }
+ if (!amdgpu_virt_ras_add_bps(adev, &bp, 1))
+ break;
+

+               amdgpu_virt_ras_reserve_bps(adev);
        }

}

--
2.34.1

#2 lore raw

AMD General

Best Regards,
Thomas
-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Xie, Chenglei
Sent: Friday, May 8, 2026 4:13 AM
To: [email protected]
Cc: Chan, Hing Pong <[email protected]>; Luo, Zhigang <[email protected]>; Deucher, Alexander <[email protected]>; Lu, Victor Cheng Chi (Victor) <[email protected]>
Subject: RE: [PATCH] drm/amdgpu: bound bad-page list writes to allocated capacity

AMD General

Hi team,

Could anyone help review this security bug fix patch for https://ontrack-internal.amd.com/browse/SWDEV-589525.

Thanks,
Chenglei

-----Original Message-----
From: Xie, Chenglei <[email protected]>
Sent: Thursday, May 7, 2026 10:45 AM
To: [email protected]
Cc: Chan, Hing Pong <[email protected]>; Luo, Zhigang <[email protected]>; Deucher, Alexander <[email protected]>; Xie, Chenglei <[email protected]>
Subject: [PATCH] drm/amdgpu: bound bad-page list writes to allocated capacity

PF2VF bad-page data can provide more entries than the VF-side bad-page buffer can hold. amdgpu_virt_ras_add_bps() copied entries without checking remaining capacity, which can overrun the 512-entry allocation.

Add an explicit max bad-page constant, validate pages against remaining space before memcpy(), and make the helper return bool so the caller stops processing when the buffer is full. This preserves normal behavior while preventing overwrite from malformed or malicious input.

Change-Id I924d2b5e369bef007eb1577f287a2274ec83d5a9
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c | 53 +++++++++++++++---------
 1 file changed, 33 insertions(+), 20 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c +2 -0
index 6974b1c5b56c2..8ac808d70356c 100644
@@ -283,14 +283,12 @@ unsigned int amd_sriov_msg_checksum(void *obj,
        return ret;
 }
 
+#define AMDGPU_VIRT_RAS_MAX_BAD_PAGES 512
+

[Thomas] This may not be sufficient. Suggest switching to a dynamically growing allocation instead.

 static int amdgpu_virt_init_ras_err_handler_data(struct amdgpu_device *adev)  {
        struct amdgpu_virt *virt = &adev->virt;
        struct amdgpu_virt_ras_err_handler_data **data = &virt->virt_eh_data;
-       /* GPU will be marked bad on host if bp count more then 10,
-        * so alloc 512 is enough.
-        */
-       unsigned int align_space = 512;
        void *bps = NULL;
        struct amdgpu_bo **bps_bo = NULL;

@@ -298,11 +296,14 @@ static int amdgpu_virt_init_ras_err_handler_data(struct amdgpu_device *adev)

        if (!*data)
                goto data_failure;

- bps = kmalloc_array(align_space, sizeof(*(*data)->bps), GFP_KERNEL);
+ /* GPU will be marked bad on host if bp count more then 10,
+ * so alloc 512 is enough.
+ */
+ bps = kmalloc_array(AMDGPU_VIRT_RAS_MAX_BAD_PAGES,

+sizeof(*(*data)->bps), GFP_KERNEL);
        if (!bps)
                goto bps_failure;

- bps_bo = kmalloc_array(align_space, sizeof(*(*data)->bps_bo), GFP_KERNEL);
+ bps_bo = kmalloc_array(AMDGPU_VIRT_RAS_MAX_BAD_PAGES,

+sizeof(*(*data)->bps_bo), GFP_KERNEL);
        if (!bps_bo)
                goto bps_bo_failure;

@@ -361,17 +362,27 @@ void amdgpu_virt_release_ras_err_handler_data(struct amdgpu_device *adev)

        virt->virt_eh_data = NULL;

}

-static void amdgpu_virt_ras_add_bps(struct amdgpu_device *adev,
- struct eeprom_table_record *bps, int pages)
+static bool amdgpu_virt_ras_add_bps(struct amdgpu_device *adev,
+ const struct eeprom_table_record *bps, int pages)

 {
        struct amdgpu_virt *virt = &adev->virt;
        struct amdgpu_virt_ras_err_handler_data *data = virt->virt_eh_data;

        if (!data)

- return;
+ return false;
+
+ if (pages > AMDGPU_VIRT_RAS_MAX_BAD_PAGES - data->count) {
+ dev_warn_ratelimited(adev->dev,

+                                    "RAS WARN: bad page buffer full, count=%d pages=%d max=%d\n",
+                                    data->count, pages,
+                                    AMDGPU_VIRT_RAS_MAX_BAD_PAGES);

+ return false;
+ }

        memcpy(&data->bps[data->count], bps, pages * sizeof(*data->bps));
        data->count += pages;

+
+ return true;
}

static void amdgpu_virt_ras_reserve_bps(struct amdgpu_device *adev) @@ -443,20 +454,22 @@ static void amdgpu_virt_add_bad_page(struct amdgpu_device *adev,

        memset(&bp, 0, sizeof(bp));

- if (bp_block_size) {
- bp_cnt = bp_block_size / sizeof(uint64_t);
- for (bp_idx = 0; bp_idx < bp_cnt; bp_idx++) {
- retired_page = *(uint64_t *)(vram_usage_va +
- bp_block_offset + bp_idx * sizeof(uint64_t));
- bp.retired_page = retired_page;
+ if (!bp_block_size)
+ return;

- if (amdgpu_virt_ras_check_bad_page(adev, retired_page))
- continue;
+ bp_cnt = bp_block_size / sizeof(uint64_t);
+ for (bp_idx = 0; bp_idx < bp_cnt; bp_idx++) {
+ retired_page = *(uint64_t *)(vram_usage_va +
+ bp_block_offset + bp_idx * sizeof(uint64_t));
+ bp.retired_page = retired_page;

- amdgpu_virt_ras_add_bps(adev, &bp, 1);
+ if (amdgpu_virt_ras_check_bad_page(adev, retired_page))
+ continue;

- amdgpu_virt_ras_reserve_bps(adev);
- }
+ if (!amdgpu_virt_ras_add_bps(adev, &bp, 1))
+ break;
+

+               amdgpu_virt_ras_reserve_bps(adev);
        }

}

--
2.34.1

#3 lore raw

PATCH v2 1/1 drm/amdgpu: grow SR-IOV RAS bad-page tables dynamically The VF RAS error handler stored bad pages in fixed-size arrays. If the PF2VF block described more entries than fit, amdgpu_virt_ras_add_bps() could memcpy past the end of those arrays.

Track table length in a capacity field, allocate a small initial table, and grow bps / bps_bo together when count + pages would exceed capacity. Keep amdgpu_virt_ras_add_bps() as the single append path: validate the addition, grow if needed, then memcpy and bump count.

On allocation failure the existing tables are left unchanged and the caller stops ingesting more bad pages from the message.

Change-Id I924d2b5e369bef007eb1577f287a2274ec83d5a9
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c | 114 ++++++++++++++++++-----
 drivers/gpu/drm/amd/amdgpu/amdgpu_virt.h |   2 +
 2 files changed, 95 insertions(+), 21 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c +93 -21
index 6974b1c5b56c2..8d4f30e3be212 100644
@@ -283,14 +283,68 @@ unsigned int amd_sriov_msg_checksum(void *obj,
 	return ret;
 }
 
+#define AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_INIT_CAPACITY	16
+
+/**
+ * amdgpu_virt_ras_grow_bad_page_table - grow bad-page arrays if needed
+ * @adev: amdgpu device
+ * @eh: RAS error handler data for the VF
+ * @min_capacity: minimum number of bad-page slots required (bps[] / bps_bo[] length)
+ *
+ * Returns 0 if @eh already has room for @min_capacity slots or growth succeeded.
+ * Returns -ENOMEM if allocation failed (existing tables are left unchanged).
+ */
+static int amdgpu_virt_ras_grow_bad_page_table(struct amdgpu_device *adev,
+		struct amdgpu_virt_ras_err_handler_data *eh,
+		int min_capacity)
+{
+	struct eeprom_table_record *new_bps;
+	struct amdgpu_bo **new_bo;
+	int new_capacity;
+	unsigned long doubled;
+
+	if (min_capacity <= eh->capacity)
+		return 0;
+
+	new_capacity = eh->capacity ? eh->capacity : AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_INIT_CAPACITY;
+	while (new_capacity < min_capacity) {
+		doubled = (unsigned long)new_capacity * 2UL;
+		if (doubled < (unsigned long)new_capacity || doubled > INT_MAX) {
+			new_capacity = min_capacity;
+			break;
+		}
+		new_capacity = (int)doubled;
+	}
+	if (new_capacity < min_capacity)
+		new_capacity = min_capacity;
+
+	new_bps = kmalloc_array(new_capacity, sizeof(*eh->bps), GFP_KERNEL);
+	new_bo = kcalloc(new_capacity, sizeof(*eh->bps_bo), GFP_KERNEL);
+	if (!new_bps || !new_bo) {
+		kfree(new_bps);
+		kfree(new_bo);
+		dev_warn_ratelimited(adev->dev,
+				     "RAS WARN: failed to grow bad page table to %d slots\n",
+				     new_capacity);
+		return -ENOMEM;
+	}
+
+	memcpy(new_bps, eh->bps, eh->count * sizeof(*eh->bps));
+	memcpy(new_bo, eh->bps_bo, eh->count * sizeof(*eh->bps_bo));
+
+	kfree(eh->bps);
+	kfree(eh->bps_bo);
+	eh->bps = new_bps;
+	eh->bps_bo = new_bo;
+	eh->capacity = new_capacity;
+
+	return 0;
+}
+
 static int amdgpu_virt_init_ras_err_handler_data(struct amdgpu_device *adev)
 {
 	struct amdgpu_virt *virt = &adev->virt;
 	struct amdgpu_virt_ras_err_handler_data **data = &virt->virt_eh_data;
-	/* GPU will be marked bad on host if bp count more then 10,
-	 * so alloc 512 is enough.
-	 */
-	unsigned int align_space = 512;
 	void *bps = NULL;
 	struct amdgpu_bo **bps_bo = NULL;
 
@@ -298,16 +352,17 @@ static int amdgpu_virt_init_ras_err_handler_data(struct amdgpu_device *adev)
 	if (!*data)
 		goto data_failure;
 
-	bps = kmalloc_array(align_space, sizeof(*(*data)->bps), GFP_KERNEL);
+	bps = kmalloc_array(AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_INIT_CAPACITY, sizeof(*(*data)->bps), GFP_KERNEL);
 	if (!bps)
 		goto bps_failure;
 
-	bps_bo = kmalloc_array(align_space, sizeof(*(*data)->bps_bo), GFP_KERNEL);
+	bps_bo = kcalloc(AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_INIT_CAPACITY, sizeof(*(*data)->bps_bo), GFP_KERNEL);
 	if (!bps_bo)
 		goto bps_bo_failure;
 
 	(*data)->bps = bps;
 	(*data)->bps_bo = bps_bo;
+	(*data)->capacity = AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_INIT_CAPACITY;
 	(*data)->count = 0;
 	(*data)->last_reserved = 0;
 
@@ -361,17 +416,32 @@ void amdgpu_virt_release_ras_err_handler_data(struct amdgpu_device *adev)
 	virt->virt_eh_data = NULL;
 }
 
-static void amdgpu_virt_ras_add_bps(struct amdgpu_device *adev,
-		struct eeprom_table_record *bps, int pages)
+static bool amdgpu_virt_ras_add_bps(struct amdgpu_device *adev,
+		const struct eeprom_table_record *bps, int pages)
 {
 	struct amdgpu_virt *virt = &adev->virt;
 	struct amdgpu_virt_ras_err_handler_data *data = virt->virt_eh_data;
+	int need;
 
-	if (!data)
-		return;
+	if (!data || pages <= 0)
+		return false;
+
+	if (pages > INT_MAX - data->count) {
+		dev_warn_ratelimited(adev->dev,
+				     "RAS WARN: bad page table size overflow (count=%d pages=%d)\n",
+				     data->count, pages);
+		return false;
+	}
+
+	need = data->count + pages;
+	if (need > data->capacity &&
+	    amdgpu_virt_ras_grow_bad_page_table(adev, data, need))
+		return false;
 
 	memcpy(&data->bps[data->count], bps, pages * sizeof(*data->bps));
 	data->count += pages;
+
+	return true;
 }
 
 static void amdgpu_virt_ras_reserve_bps(struct amdgpu_device *adev)
@@ -443,20 +513,22 @@ static void amdgpu_virt_add_bad_page(struct amdgpu_device *adev,
 
 	memset(&bp, 0, sizeof(bp));
 
-	if (bp_block_size) {
-		bp_cnt = bp_block_size / sizeof(uint64_t);
-		for (bp_idx = 0; bp_idx < bp_cnt; bp_idx++) {
-			retired_page = *(uint64_t *)(vram_usage_va +
-					bp_block_offset + bp_idx * sizeof(uint64_t));
-			bp.retired_page = retired_page;
+	if (!bp_block_size)
+		return;
 
-			if (amdgpu_virt_ras_check_bad_page(adev, retired_page))
-				continue;
+	bp_cnt = bp_block_size / sizeof(uint64_t);
+	for (bp_idx = 0; bp_idx < bp_cnt; bp_idx++) {
+		retired_page = *(uint64_t *)(vram_usage_va +
+				bp_block_offset + bp_idx * sizeof(uint64_t));
+		bp.retired_page = retired_page;
 
-			amdgpu_virt_ras_add_bps(adev, &bp, 1);
+		if (amdgpu_virt_ras_check_bad_page(adev, retired_page))
+			continue;
 
-			amdgpu_virt_ras_reserve_bps(adev);
-		}
+		if (!amdgpu_virt_ras_add_bps(adev, &bp, 1))
+			break;
+
+		amdgpu_virt_ras_reserve_bps(adev);
 	}
 }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_virt.h +2 -0
index 9da0c6e9b8695..af2acf8eee6e2 100644
@@ -263,6 +263,8 @@ struct amdgpu_virt_ras_err_handler_data {
 	struct eeprom_table_record *bps;
 	/* point to reserved bo array */
 	struct amdgpu_bo **bps_bo;
+	/* number of slots in bps[] / bps_bo[] (always >= count) */
+	int capacity;
 	/* the count of entries */
 	int count;
 	/* last reserved entry's index + 1 */
Signature · 1 line
2.34.1
#4 lore raw

PATCH v3 1/1 drm/amdgpu: change VF RAS bad bage space to dynamic allocation

The VF RAS error handler stored bad pages in fixed-size arrays. If the PF2VF block described more entries than fit, amdgpu_virt_ras_add_bps() could memcpy past the end of those arrays.

Track table length in a capacity field, allocate a small initial table, and grow bps / bps_bo together when count + pages would exceed capacity. Keep amdgpu_virt_ras_add_bps() as the single append path: validate the addition, grow if needed, then memcpy and bump count.

On allocation failure the existing tables are left unchanged and the caller stops ingesting more bad pages from the message.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c | 109 ++++++++++++++++++-----
 drivers/gpu/drm/amd/amdgpu/amdgpu_virt.h |   2 +
 2 files changed, 90 insertions(+), 21 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c +88 -21
index 6974b1c5b56c2..beb2693e4c704 100644
@@ -283,14 +283,63 @@ unsigned int amd_sriov_msg_checksum(void *obj,
 	return ret;
 }
 
+#define AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_INIT_CAPACITY	512
+
+/**
+ * amdgpu_virt_ras_realloc_eh_data_space - alloc/realloc VF bad-page @data->bps and @data->bps_bo
+ * @adev: amdgpu device
+ * @data: VF RAS error-handler data
+ * @pages: minimum number of new slots to add beyond @data->capacity
+ *
+ * Return: 0 on success, %-ENOMEM on failure.
+ */
+static int amdgpu_virt_ras_realloc_eh_data_space(struct amdgpu_device *adev,
+		struct amdgpu_virt_ras_err_handler_data *data,
+		int pages)
+{
+	struct eeprom_table_record *new_bps;
+	struct amdgpu_bo **new_bo;
+	unsigned int old_space;
+	unsigned int new_space;
+	unsigned int align_space;
+
+	old_space = (unsigned int)data->capacity;
+	new_space = old_space + max_t(unsigned int, (unsigned int)pages,
+				      (unsigned int)AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_INIT_CAPACITY);
+	if (new_space < old_space || new_space > INT_MAX)
+		return -ENOMEM;
+
+	align_space = ALIGN(new_space, AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_INIT_CAPACITY);
+	if (align_space > INT_MAX)
+		return -ENOMEM;
+
+	new_bps = kmalloc_array(align_space, sizeof(*data->bps), GFP_KERNEL);
+	new_bo = kcalloc(align_space, sizeof(*data->bps_bo), GFP_KERNEL);
+	if (!new_bps || !new_bo) {
+		kfree(new_bps);
+		kfree(new_bo);
+		dev_warn_ratelimited(adev->dev,
+				     "RAS WARN: failed to grow bad page table to %u slots\n",
+				     align_space);
+		return -ENOMEM;
+	}
+
+	memcpy(new_bps, data->bps, data->count * sizeof(*data->bps));
+	memcpy(new_bo, data->bps_bo, data->count * sizeof(*data->bps_bo));
+
+	kfree(data->bps);
+	kfree(data->bps_bo);
+	data->bps = new_bps;
+	data->bps_bo = new_bo;
+	data->capacity = (int)align_space;
+
+	return 0;
+}
+
 static int amdgpu_virt_init_ras_err_handler_data(struct amdgpu_device *adev)
 {
 	struct amdgpu_virt *virt = &adev->virt;
 	struct amdgpu_virt_ras_err_handler_data **data = &virt->virt_eh_data;
-	/* GPU will be marked bad on host if bp count more then 10,
-	 * so alloc 512 is enough.
-	 */
-	unsigned int align_space = 512;
 	void *bps = NULL;
 	struct amdgpu_bo **bps_bo = NULL;
 
@@ -298,16 +347,17 @@ static int amdgpu_virt_init_ras_err_handler_data(struct amdgpu_device *adev)
 	if (!*data)
 		goto data_failure;
 
-	bps = kmalloc_array(align_space, sizeof(*(*data)->bps), GFP_KERNEL);
+	bps = kmalloc_array(AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_INIT_CAPACITY, sizeof(*(*data)->bps), GFP_KERNEL);
 	if (!bps)
 		goto bps_failure;
 
-	bps_bo = kmalloc_array(align_space, sizeof(*(*data)->bps_bo), GFP_KERNEL);
+	bps_bo = kcalloc(AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_INIT_CAPACITY, sizeof(*(*data)->bps_bo), GFP_KERNEL);
 	if (!bps_bo)
 		goto bps_bo_failure;
 
 	(*data)->bps = bps;
 	(*data)->bps_bo = bps_bo;
+	(*data)->capacity = AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_INIT_CAPACITY;
 	(*data)->count = 0;
 	(*data)->last_reserved = 0;
 
@@ -361,17 +411,32 @@ void amdgpu_virt_release_ras_err_handler_data(struct amdgpu_device *adev)
 	virt->virt_eh_data = NULL;
 }
 
-static void amdgpu_virt_ras_add_bps(struct amdgpu_device *adev,
-		struct eeprom_table_record *bps, int pages)
+static bool amdgpu_virt_ras_add_bps(struct amdgpu_device *adev,
+		const struct eeprom_table_record *bps, int pages)
 {
 	struct amdgpu_virt *virt = &adev->virt;
 	struct amdgpu_virt_ras_err_handler_data *data = virt->virt_eh_data;
+	int need;
 
-	if (!data)
-		return;
+	if (!data || pages <= 0)
+		return false;
+
+	if (pages > INT_MAX - data->count) {
+		dev_warn_ratelimited(adev->dev,
+				     "RAS WARN: bad page table size overflow (count=%d pages=%d)\n",
+				     data->count, pages);
+		return false;
+	}
+
+	need = data->count + pages;
+	if (need > data->capacity &&
+	    amdgpu_virt_ras_realloc_eh_data_space(adev, data, need - data->capacity))
+		return false;
 
 	memcpy(&data->bps[data->count], bps, pages * sizeof(*data->bps));
 	data->count += pages;
+
+	return true;
 }
 
 static void amdgpu_virt_ras_reserve_bps(struct amdgpu_device *adev)
@@ -443,20 +508,22 @@ static void amdgpu_virt_add_bad_page(struct amdgpu_device *adev,
 
 	memset(&bp, 0, sizeof(bp));
 
-	if (bp_block_size) {
-		bp_cnt = bp_block_size / sizeof(uint64_t);
-		for (bp_idx = 0; bp_idx < bp_cnt; bp_idx++) {
-			retired_page = *(uint64_t *)(vram_usage_va +
-					bp_block_offset + bp_idx * sizeof(uint64_t));
-			bp.retired_page = retired_page;
+	if (!bp_block_size)
+		return;
 
-			if (amdgpu_virt_ras_check_bad_page(adev, retired_page))
-				continue;
+	bp_cnt = bp_block_size / sizeof(uint64_t);
+	for (bp_idx = 0; bp_idx < bp_cnt; bp_idx++) {
+		retired_page = *(uint64_t *)(vram_usage_va +
+				bp_block_offset + bp_idx * sizeof(uint64_t));
+		bp.retired_page = retired_page;
 
-			amdgpu_virt_ras_add_bps(adev, &bp, 1);
+		if (amdgpu_virt_ras_check_bad_page(adev, retired_page))
+			continue;
 
-			amdgpu_virt_ras_reserve_bps(adev);
-		}
+		if (!amdgpu_virt_ras_add_bps(adev, &bp, 1))
+			break;
+
+		amdgpu_virt_ras_reserve_bps(adev);
 	}
 }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_virt.h +2 -0
index 9da0c6e9b8695..af2acf8eee6e2 100644
@@ -263,6 +263,8 @@ struct amdgpu_virt_ras_err_handler_data {
 	struct eeprom_table_record *bps;
 	/* point to reserved bo array */
 	struct amdgpu_bo **bps_bo;
+	/* number of slots in bps[] / bps_bo[] (always >= count) */
+	int capacity;
 	/* the count of entries */
 	int count;
 	/* last reserved entry's index + 1 */
Signature · 1 line
2.34.1
#5 lore raw

RE: [PATCH v3] drm/amdgpu: change VF RAS bad bage space to dynamic allocation

AMD General

Best Regards,
Thomas
-----Original Message-----
From: Chenglei Xie <[email protected]>
Sent: Tuesday, May 12, 2026 10:44 PM
To: [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: [PATCH v3] drm/amdgpu: change VF RAS bad bage space to dynamic allocation

The VF RAS error handler stored bad pages in fixed-size arrays. If the PF2VF block described more entries than fit, amdgpu_virt_ras_add_bps() could memcpy past the end of those arrays.

Track table length in a capacity field, allocate a small initial table, and grow bps / bps_bo together when count + pages would exceed capacity. Keep amdgpu_virt_ras_add_bps() as the single append path: validate the addition, grow if needed, then memcpy and bump count.

On allocation failure the existing tables are left unchanged and the caller stops ingesting more bad pages from the message.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c | 109 ++++++++++++++++++-----
 drivers/gpu/drm/amd/amdgpu/amdgpu_virt.h |   2 +
 2 files changed, 90 insertions(+), 21 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c +25 -0
index 6974b1c5b56c2..beb2693e4c704 100644
@@ -283,14 +283,63 @@ unsigned int amd_sriov_msg_checksum(void *obj,
        return ret;
 }
 
+#define AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_INIT_CAPACITY   512
+
+/**
+ * amdgpu_virt_ras_realloc_eh_data_space - alloc/realloc VF bad-page
+@data->bps and @data->bps_bo
+ * @adev: amdgpu device
+ * @data: VF RAS error-handler data
+ * @pages: minimum number of new slots to add beyond @data->capacity
+ *
+ * Return: 0 on success, %-ENOMEM on failure.
+ */
+static int amdgpu_virt_ras_realloc_eh_data_space(struct amdgpu_device *adev,
+               struct amdgpu_virt_ras_err_handler_data *data,
+               int pages)
+{
+       struct eeprom_table_record *new_bps;
+       struct amdgpu_bo **new_bo;
+       unsigned int old_space;
+       unsigned int new_space;
+       unsigned int align_space;
+
+       old_space = (unsigned int)data->capacity;
+       new_space = old_space + max_t(unsigned int, (unsigned int)pages,
+                                     (unsigned int)AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_INIT_CAPACITY);
+       if (new_space < old_space || new_space > INT_MAX)
 

[Thomas] Using INT_MAX as the upper bound check is too permissive. It should be set to the maximum realistically acceptable value instead. The same applies to the other INT_MAX checks below.

+ return -ENOMEM;
+
+ align_space = ALIGN(new_space, AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_INIT_CAPACITY);
+ if (align_space > INT_MAX)
+ return -ENOMEM;
+

+       new_bps = kmalloc_array(align_space, sizeof(*data->bps), GFP_KERNEL);
+       new_bo = kcalloc(align_space, sizeof(*data->bps_bo), GFP_KERNEL);
+       if (!new_bps || !new_bo) {
+               kfree(new_bps);
+               kfree(new_bo);
+               dev_warn_ratelimited(adev->dev,

+ "RAS WARN: failed to grow bad page table to %u slots\n",
+ align_space);
+ return -ENOMEM;
+ }
+
+ memcpy(new_bps, data->bps, data->count * sizeof(*data->bps));
+ memcpy(new_bo, data->bps_bo, data->count * sizeof(*data->bps_bo));
+

+       kfree(data->bps);
+       kfree(data->bps_bo);
+       data->bps = new_bps;
+       data->bps_bo = new_bo;
+       data->capacity = (int)align_space;

+
+ return 0;
+}
+

 static int amdgpu_virt_init_ras_err_handler_data(struct amdgpu_device *adev)  {
        struct amdgpu_virt *virt = &adev->virt;
        struct amdgpu_virt_ras_err_handler_data **data = &virt->virt_eh_data;

- /* GPU will be marked bad on host if bp count more then 10,
- * so alloc 512 is enough.
- */
- unsigned int align_space = 512;

[Thomas] If set align_space = AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_INIT_CAPACITY , can avoid to change so much code in this function ?

        void *bps = NULL;
        struct amdgpu_bo **bps_bo = NULL;

@@ -298,16 +347,17 @@ static int amdgpu_virt_init_ras_err_handler_data(struct amdgpu_device *adev)

        if (!*data)
                goto data_failure;

- bps = kmalloc_array(align_space, sizeof(*(*data)->bps), GFP_KERNEL);
+ bps = kmalloc_array(AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_INIT_CAPACITY,

+sizeof(*(*data)->bps), GFP_KERNEL);
        if (!bps)
                goto bps_failure;

- bps_bo = kmalloc_array(align_space, sizeof(*(*data)->bps_bo), GFP_KERNEL);
+ bps_bo = kcalloc(AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_INIT_CAPACITY,

+sizeof(*(*data)->bps_bo), GFP_KERNEL);
        if (!bps_bo)
                goto bps_bo_failure;

        (*data)->bps = bps;
        (*data)->bps_bo = bps_bo;
+       (*data)->capacity = AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_INIT_CAPACITY;
        (*data)->count = 0;
        (*data)->last_reserved = 0;

@@ -361,17 +411,32 @@ void amdgpu_virt_release_ras_err_handler_data(struct amdgpu_device *adev)

        virt->virt_eh_data = NULL;

}

-static void amdgpu_virt_ras_add_bps(struct amdgpu_device *adev,
- struct eeprom_table_record *bps, int pages)
+static bool amdgpu_virt_ras_add_bps(struct amdgpu_device *adev,
+ const struct eeprom_table_record *bps, int pages)

 {
        struct amdgpu_virt *virt = &adev->virt;
        struct amdgpu_virt_ras_err_handler_data *data = virt->virt_eh_data;

+ int need;

- if (!data)
- return;
+ if (!data || pages <= 0)
+ return false;
+
+ if (pages > INT_MAX - data->count) {
+ dev_warn_ratelimited(adev->dev,
+ "RAS WARN: bad page table size overflow (count=%d pages=%d)\n",
+ data->count, pages);
+ return false;
+ }
+
+ need = data->count + pages;
+ if (need > data->capacity &&
+ amdgpu_virt_ras_realloc_eh_data_space(adev, data, need - data->capacity))
+ return false;

        memcpy(&data->bps[data->count], bps, pages * sizeof(*data->bps));
        data->count += pages;

+
+ return true;
}

static void amdgpu_virt_ras_reserve_bps(struct amdgpu_device *adev) @@ -443,20 +508,22 @@ static void amdgpu_virt_add_bad_page(struct amdgpu_device *adev,

        memset(&bp, 0, sizeof(bp));

- if (bp_block_size) {
- bp_cnt = bp_block_size / sizeof(uint64_t);
- for (bp_idx = 0; bp_idx < bp_cnt; bp_idx++) {
- retired_page = *(uint64_t *)(vram_usage_va +
- bp_block_offset + bp_idx * sizeof(uint64_t));
- bp.retired_page = retired_page;
+ if (!bp_block_size)
+ return;

- if (amdgpu_virt_ras_check_bad_page(adev, retired_page))
- continue;
+ bp_cnt = bp_block_size / sizeof(uint64_t);
+ for (bp_idx = 0; bp_idx < bp_cnt; bp_idx++) {
+ retired_page = *(uint64_t *)(vram_usage_va +
+ bp_block_offset + bp_idx * sizeof(uint64_t));
+ bp.retired_page = retired_page;

- amdgpu_virt_ras_add_bps(adev, &bp, 1);
+ if (amdgpu_virt_ras_check_bad_page(adev, retired_page))
+ continue;

- amdgpu_virt_ras_reserve_bps(adev);
- }
+ if (!amdgpu_virt_ras_add_bps(adev, &bp, 1))
+ break;
+

+               amdgpu_virt_ras_reserve_bps(adev);
        }

}

drivers/gpu/drm/amd/amdgpu/amdgpu_virt.h +2 -0
index 9da0c6e9b8695..af2acf8eee6e2 100644
@@ -263,6 +263,8 @@ struct amdgpu_virt_ras_err_handler_data {
        struct eeprom_table_record *bps;
        /* point to reserved bo array */
        struct amdgpu_bo **bps_bo;
+       /* number of slots in bps[] / bps_bo[] (always >= count) */
+       int capacity;
        /* the count of entries */
        int count;
        /* last reserved entry's index + 1 */

--
2.34.1

#6 lore raw

PATCH v4 1/1 drm/amdgpu: grow VF RAS bad page table with bounded dynamic alloc

In reply to Chenglei Xie

The VF RAS error handler used fixed-size bps[] / bps_bo[] arrays (512 slots). When the PF2VF bad-page block listed more entries than fit, amdgpu_virt_ras_add_bps() could memcpy() past the end of those arrays.

Replace the fixed backing store with a dynamically grown table:
- Add capacity to track allocated slots separately from count.
- Start at 512 slots and realloc bps / bps_bo together when full.
- Refuse growth beyond RAS_EEPROM_PEAK_RECORD_COUNT, the largest bad page record count among supported RAS EEPROM layouts (v1 and v2.1).
- Return failure from amdgpu_virt_ras_add_bps() and stop processing the PF2VF block if allocation fails or the cap is reached.

Move shared RAS EEPROM size and per-layout record limit macros into amdgpu_ras_eeprom.h so the VF cap stays in sync with EEPROM layout.

Change-Id Ic8dd487d91436759bee0e201ce90ea1f2266a161
---
 .../gpu/drm/amd/amdgpu/amdgpu_ras_eeprom.c    |  17 ---
 .../gpu/drm/amd/amdgpu/amdgpu_ras_eeprom.h    |  28 +++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c      | 110 ++++++++++++++----
 drivers/gpu/drm/amd/amdgpu/amdgpu_virt.h      |   2 +
 4 files changed, 120 insertions(+), 37 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ras_eeprom.c +0 -17
index c61389a079822..c7a813e5fa0a6 100644
@@ -58,15 +58,6 @@
 #define EEPROM_I2C_MADDR_0      0x0
 #define EEPROM_I2C_MADDR_4      0x40000
 
-/*
- * The 2 macros below represent the actual size in bytes that
- * those entities occupy in the EEPROM memory.
- * RAS_TABLE_RECORD_SIZE is different than sizeof(eeprom_table_record) which
- * uses uint64 to store 6b fields such as retired_page.
- */
-#define RAS_TABLE_HEADER_SIZE   20
-#define RAS_TABLE_RECORD_SIZE   24
-
 /* Table hdr is 'AMDR' */
 #define RAS_TABLE_HDR_VAL       0x414d4452
 
@@ -87,13 +78,9 @@
  * ---------------------------------
  */
 
-/* Assume 2-Mbit size EEPROM and take up the whole space. */
-#define RAS_TBL_SIZE_BYTES      (256 * 1024)
 #define RAS_TABLE_START         0
 #define RAS_HDR_START           RAS_TABLE_START
 #define RAS_RECORD_START        (RAS_HDR_START + RAS_TABLE_HEADER_SIZE)
-#define RAS_MAX_RECORD_COUNT    ((RAS_TBL_SIZE_BYTES - RAS_TABLE_HEADER_SIZE) \
-				 / RAS_TABLE_RECORD_SIZE)
 
 /*
  * EEPROM Table structrue v2.1
@@ -116,13 +103,9 @@
  */
 
 /* EEPROM Table V2_1 */
-#define RAS_TABLE_V2_1_INFO_SIZE       256
 #define RAS_TABLE_V2_1_INFO_START      RAS_TABLE_HEADER_SIZE
 #define RAS_RECORD_START_V2_1          (RAS_HDR_START + RAS_TABLE_HEADER_SIZE + \
 					RAS_TABLE_V2_1_INFO_SIZE)
-#define RAS_MAX_RECORD_COUNT_V2_1      ((RAS_TBL_SIZE_BYTES - RAS_TABLE_HEADER_SIZE - \
-					RAS_TABLE_V2_1_INFO_SIZE) \
-					/ RAS_TABLE_RECORD_SIZE)
 
 #define RAS_SMU_MESSAGE_TIMEOUT_MS 1000 /* 1s */
 
drivers/gpu/drm/amd/amdgpu/amdgpu_ras_eeprom.h +28 -0
index a62114800a92a..4ac0d67f53743 100644
@@ -30,6 +30,34 @@
 #define RAS_TABLE_VER_V2_1         0x00021000
 #define RAS_TABLE_VER_V3           0x00030000
 
+/* Assume 2-Mbit size EEPROM and take up the whole space. */
+#define RAS_TBL_SIZE_BYTES		(256 * 1024)
+#define RAS_TABLE_HEADER_SIZE		20
+#define RAS_TABLE_RECORD_SIZE		24
+
+/* Per-layout bad page record limits (add new EEPROM versions here). */
+#define RAS_MAX_RECORD_COUNT		((RAS_TBL_SIZE_BYTES - RAS_TABLE_HEADER_SIZE) \
+					 / RAS_TABLE_RECORD_SIZE)
+#define RAS_TABLE_V2_1_INFO_SIZE	256
+#define RAS_MAX_RECORD_COUNT_V2_1	((RAS_TBL_SIZE_BYTES - RAS_TABLE_HEADER_SIZE - \
+					  RAS_TABLE_V2_1_INFO_SIZE) \
+					 / RAS_TABLE_RECORD_SIZE)
+
+#define RAS_RECORD_COUNT_MAX(a, b)	((a) > (b) ? (a) : (b))
+
+/*
+ * Upper bound on bad page records for any supported EEPROM layout.
+ * Extend when a new RAS_TABLE_VER_* adds a RAS_MAX_RECORD_COUNT_* macro.
+ */
+#define RAS_EEPROM_PEAK_RECORD_COUNT				\
+	RAS_RECORD_COUNT_MAX(RAS_MAX_RECORD_COUNT,		\
+			     RAS_MAX_RECORD_COUNT_V2_1)
+
+static inline uint32_t amdgpu_ras_eeprom_peak_record_count(void)
+{
+	return RAS_EEPROM_PEAK_RECORD_COUNT;
+}
+
 struct amdgpu_device;
 
 enum amdgpu_ras_gpu_health_status {
drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c +90 -20

153 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_virt.h +2 -0
index d563deec0916b..d8500c3e48a17 100644
@@ -265,6 +265,8 @@ struct amdgpu_virt_ras_err_handler_data {
 	struct eeprom_table_record *bps;
 	/* point to reserved bo array */
 	struct amdgpu_bo **bps_bo;
+	/* number of slots in bps[] / bps_bo[] (always >= count) */
+	int capacity;
 	/* the count of entries */
 	int count;
 	/* last reserved entry's index + 1 */
Signature · 1 line
2.34.1
#7 lore raw

RE: [PATCH v4] drm/amdgpu: grow VF RAS bad page table with bounded dynamic alloc

AMD General

I suggest defining AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_MAX_CAPACITY as an absolute value rather than referencing or modifying anything in amdgpu_ras_eeprom.x, Since amdgpu_ras_eeprom.x is also used by amdgpu, the maximum bad page capacity requirements for amdgpu and amdgpu_virt may differ.

Best Regards,
Thomas
-----Original Message-----
From: Xie, Chenglei <[email protected]>
Sent: Friday, May 22, 2026 12:35 AM
To: [email protected]
Cc: Chan, Hing Pong <[email protected]>; Luo, Zhigang <[email protected]>; Deucher, Alexander <[email protected]>; Xie, Chenglei <[email protected]>; Chai, Thomas <[email protected]>; Yi, Tony <[email protected]>
Subject: [PATCH v4] drm/amdgpu: grow VF RAS bad page table with bounded dynamic alloc

The VF RAS error handler used fixed-size bps[] / bps_bo[] arrays (512 slots). When the PF2VF bad-page block listed more entries than fit, amdgpu_virt_ras_add_bps() could memcpy() past the end of those arrays.

Replace the fixed backing store with a dynamically grown table:
- Add capacity to track allocated slots separately from count.
- Start at 512 slots and realloc bps / bps_bo together when full.
- Refuse growth beyond RAS_EEPROM_PEAK_RECORD_COUNT, the largest bad page record count among supported RAS EEPROM layouts (v1 and v2.1).
- Return failure from amdgpu_virt_ras_add_bps() and stop processing the PF2VF block if allocation fails or the cap is reached.

Move shared RAS EEPROM size and per-layout record limit macros into amdgpu_ras_eeprom.h so the VF cap stays in sync with EEPROM layout.

Change-Id Ic8dd487d91436759bee0e201ce90ea1f2266a161
---
 .../gpu/drm/amd/amdgpu/amdgpu_ras_eeprom.c    |  17 ---
 .../gpu/drm/amd/amdgpu/amdgpu_ras_eeprom.h    |  28 +++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c      | 110 ++++++++++++++----
 drivers/gpu/drm/amd/amdgpu/amdgpu_virt.h      |   2 +
 4 files changed, 120 insertions(+), 37 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ras_eeprom.c +0 -17
index c61389a079822..c7a813e5fa0a6 100644
@@ -58,15 +58,6 @@
 #define EEPROM_I2C_MADDR_0      0x0
 #define EEPROM_I2C_MADDR_4      0x40000
 
-/*
- * The 2 macros below represent the actual size in bytes that
- * those entities occupy in the EEPROM memory.
- * RAS_TABLE_RECORD_SIZE is different than sizeof(eeprom_table_record) which
- * uses uint64 to store 6b fields such as retired_page.
- */
-#define RAS_TABLE_HEADER_SIZE   20
-#define RAS_TABLE_RECORD_SIZE   24
-
 /* Table hdr is 'AMDR' */
 #define RAS_TABLE_HDR_VAL       0x414d4452
 
@@ -87,13 +78,9 @@
  * ---------------------------------
  */
 
-/* Assume 2-Mbit size EEPROM and take up the whole space. */
-#define RAS_TBL_SIZE_BYTES      (256 * 1024)
 #define RAS_TABLE_START         0
 #define RAS_HDR_START           RAS_TABLE_START
 #define RAS_RECORD_START        (RAS_HDR_START + RAS_TABLE_HEADER_SIZE)
-#define RAS_MAX_RECORD_COUNT    ((RAS_TBL_SIZE_BYTES - RAS_TABLE_HEADER_SIZE) \
-                                / RAS_TABLE_RECORD_SIZE)
 
 /*
  * EEPROM Table structrue v2.1
@@ -116,13 +103,9 @@
  */
 
 /* EEPROM Table V2_1 */
-#define RAS_TABLE_V2_1_INFO_SIZE       256
 #define RAS_TABLE_V2_1_INFO_START      RAS_TABLE_HEADER_SIZE
 #define RAS_RECORD_START_V2_1          (RAS_HDR_START + RAS_TABLE_HEADER_SIZE + \
                                        RAS_TABLE_V2_1_INFO_SIZE)
-#define RAS_MAX_RECORD_COUNT_V2_1      ((RAS_TBL_SIZE_BYTES - RAS_TABLE_HEADER_SIZE - \
-                                       RAS_TABLE_V2_1_INFO_SIZE) \
-                                       / RAS_TABLE_RECORD_SIZE)
 
 #define RAS_SMU_MESSAGE_TIMEOUT_MS 1000 /* 1s */
 
drivers/gpu/drm/amd/amdgpu/amdgpu_ras_eeprom.h +28 -0
index a62114800a92a..4ac0d67f53743 100644
@@ -30,6 +30,34 @@
 #define RAS_TABLE_VER_V2_1         0x00021000
 #define RAS_TABLE_VER_V3           0x00030000
 
+/* Assume 2-Mbit size EEPROM and take up the whole space. */
+#define RAS_TBL_SIZE_BYTES             (256 * 1024)
+#define RAS_TABLE_HEADER_SIZE          20
+#define RAS_TABLE_RECORD_SIZE          24
+
+/* Per-layout bad page record limits (add new EEPROM versions here). */
+#define RAS_MAX_RECORD_COUNT           ((RAS_TBL_SIZE_BYTES - RAS_TABLE_HEADER_SIZE) \
+                                        / RAS_TABLE_RECORD_SIZE)
+#define RAS_TABLE_V2_1_INFO_SIZE       256
+#define RAS_MAX_RECORD_COUNT_V2_1      ((RAS_TBL_SIZE_BYTES - RAS_TABLE_HEADER_SIZE - \
+                                         RAS_TABLE_V2_1_INFO_SIZE) \
+                                        / RAS_TABLE_RECORD_SIZE)
+
+#define RAS_RECORD_COUNT_MAX(a, b)     ((a) > (b) ? (a) : (b))
+
+/*
+ * Upper bound on bad page records for any supported EEPROM layout.
+ * Extend when a new RAS_TABLE_VER_* adds a RAS_MAX_RECORD_COUNT_* macro.
+ */
+#define RAS_EEPROM_PEAK_RECORD_COUNT                           \
+       RAS_RECORD_COUNT_MAX(RAS_MAX_RECORD_COUNT,              \
+                            RAS_MAX_RECORD_COUNT_V2_1)
+
+static inline uint32_t amdgpu_ras_eeprom_peak_record_count(void)
+{
+       return RAS_EEPROM_PEAK_RECORD_COUNT;
+}
+
 struct amdgpu_device;
 
 enum amdgpu_ras_gpu_health_status {
drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c +57 -4

69 lines not on this page · raw message

@@ -302,12 +353,13 @@ static int amdgpu_virt_init_ras_err_handler_data(struct amdgpu_device *adev)

        if (!bps)
                goto bps_failure;

- bps_bo = kmalloc_array(align_space, sizeof(*(*data)->bps_bo), GFP_KERNEL);

+       bps_bo = kcalloc(align_space, sizeof(*(*data)->bps_bo), GFP_KERNEL);
        if (!bps_bo)
                goto bps_bo_failure;

        (*data)->bps = bps;
        (*data)->bps_bo = bps_bo;
+       (*data)->capacity = align_space;
        (*data)->count = 0;
        (*data)->last_reserved = 0;

@@ -361,17 +413,33 @@ void amdgpu_virt_release_ras_err_handler_data(struct amdgpu_device *adev)

        virt->virt_eh_data = NULL;

}

-static void amdgpu_virt_ras_add_bps(struct amdgpu_device *adev,
- struct eeprom_table_record *bps, int pages)
+static bool amdgpu_virt_ras_add_bps(struct amdgpu_device *adev,
+ const struct eeprom_table_record *bps, int pages)

 {
        struct amdgpu_virt *virt = &adev->virt;
        struct amdgpu_virt_ras_err_handler_data *data = virt->virt_eh_data;

+ int need;

- if (!data)
- return;
+ if (!data || pages <= 0)
+ return false;
+
+ if (pages > AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_MAX_CAPACITY - data->count) {
+ dev_warn_ratelimited(adev->dev,

+                                    "RAS WARN: bad page table at capacity (count=%d pages=%d max=%u)\n",
+                                    data->count, pages,
+                                    AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_MAX_CAPACITY);

+ return false;
+ }
+
+ need = data->count + pages;
+ if (need > data->capacity &&
+ amdgpu_virt_ras_realloc_eh_data_space(adev, data, need - data->capacity))
+ return false;

        memcpy(&data->bps[data->count], bps, pages * sizeof(*data->bps));
        data->count += pages;

+
+ return true;
}

static void amdgpu_virt_ras_reserve_bps(struct amdgpu_device *adev) @@ -443,20 +511,22 @@ static void amdgpu_virt_add_bad_page(struct amdgpu_device *adev,

        memset(&bp, 0, sizeof(bp));

- if (bp_block_size) {
- bp_cnt = bp_block_size / sizeof(uint64_t);
- for (bp_idx = 0; bp_idx < bp_cnt; bp_idx++) {
- retired_page = *(uint64_t *)(vram_usage_va +
- bp_block_offset + bp_idx * sizeof(uint64_t));
- bp.retired_page = retired_page;
+ if (!bp_block_size)
+ return;

- if (amdgpu_virt_ras_check_bad_page(adev, retired_page))
- continue;
+ bp_cnt = bp_block_size / sizeof(uint64_t);
+ for (bp_idx = 0; bp_idx < bp_cnt; bp_idx++) {
+ retired_page = *(uint64_t *)(vram_usage_va +
+ bp_block_offset + bp_idx * sizeof(uint64_t));
+ bp.retired_page = retired_page;

- amdgpu_virt_ras_add_bps(adev, &bp, 1);
+ if (amdgpu_virt_ras_check_bad_page(adev, retired_page))
+ continue;

- amdgpu_virt_ras_reserve_bps(adev);
- }
+ if (!amdgpu_virt_ras_add_bps(adev, &bp, 1))
+ break;
+

+               amdgpu_virt_ras_reserve_bps(adev);
        }

}

drivers/gpu/drm/amd/amdgpu/amdgpu_virt.h +2 -0
index d563deec0916b..d8500c3e48a17 100644
@@ -265,6 +265,8 @@ struct amdgpu_virt_ras_err_handler_data {
        struct eeprom_table_record *bps;
        /* point to reserved bo array */
        struct amdgpu_bo **bps_bo;
+       /* number of slots in bps[] / bps_bo[] (always >= count) */
+       int capacity;
        /* the count of entries */
        int count;
        /* last reserved entry's index + 1 */

--
2.34.1

#8 lore raw

PATCH v5 1/1 drm/amdgpu: grow VF RAS bad page table with bounded dynamic alloc

In reply to Chenglei Xie

The VF RAS error handler used fixed-size bps[] / bps_bo[] arrays (512 slots). When the PF2VF bad-page block listed more entries than fit, amdgpu_virt_ras_add_bps() could memcpy() past the end of those arrays.

Replace the fixed backing store with a dynamically grown table:
- Add capacity to track allocated slots separately from count.
- Start at 512 slots and realloc bps / bps_bo together when full.
- Refuse growth beyond maximum EEPROM record limit (AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_MAX_CAPACITY).
- Return failure from amdgpu_virt_ras_add_bps() and stop processing
the PF2VF block if allocation fails or the cap is reached.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c | 111 +++++++++++++++++++----
 drivers/gpu/drm/amd/amdgpu/amdgpu_virt.h |   2 +
 2 files changed, 93 insertions(+), 20 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c +91 -20
index f8e58d12e39ae..7b0d425101611 100644
@@ -283,14 +283,66 @@ unsigned int amd_sriov_msg_checksum(void *obj,
 	return ret;
 }
 
+#define AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_INIT_CAPACITY	512
+/* Max bad page slots allowed for SRIOV*/
+#define AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_MAX_CAPACITY	10665U
+
+/**
+ * amdgpu_virt_ras_realloc_eh_data_space - alloc/realloc VF bad-page @data->bps and @data->bps_bo
+ * @adev: amdgpu device
+ * @data: VF RAS error-handler data
+ * @pages: minimum number of new slots to add beyond @data->capacity
+ *
+ * Return: 0 on success, %-ENOMEM on failure.
+ */
+static int amdgpu_virt_ras_realloc_eh_data_space(struct amdgpu_device *adev,
+		struct amdgpu_virt_ras_err_handler_data *data,
+		int pages)
+{
+	struct eeprom_table_record *new_bps;
+	struct amdgpu_bo **new_bo;
+	unsigned int old_space;
+	unsigned int new_space;
+	unsigned int align_space;
+
+	old_space = (unsigned int)data->capacity;
+	new_space = old_space + max_t(unsigned int, (unsigned int)pages,
+				      (unsigned int)AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_INIT_CAPACITY);
+	if (new_space < old_space || new_space > AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_MAX_CAPACITY)
+		return -ENOMEM;
+
+	align_space = ALIGN(new_space, AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_INIT_CAPACITY);
+	if (align_space > AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_MAX_CAPACITY)
+		return -ENOMEM;
+
+	new_bps = kmalloc_array(align_space, sizeof(*data->bps), GFP_KERNEL);
+	new_bo = kcalloc(align_space, sizeof(*data->bps_bo), GFP_KERNEL);
+	if (!new_bps || !new_bo) {
+		kfree(new_bps);
+		kfree(new_bo);
+		dev_warn_ratelimited(adev->dev,
+				     "RAS WARN: failed to grow bad page table to %u slots\n",
+				     align_space);
+		return -ENOMEM;
+	}
+
+	memcpy(new_bps, data->bps, data->count * sizeof(*data->bps));
+	memcpy(new_bo, data->bps_bo, data->count * sizeof(*data->bps_bo));
+
+	kfree(data->bps);
+	kfree(data->bps_bo);
+	data->bps = new_bps;
+	data->bps_bo = new_bo;
+	data->capacity = (int)align_space;
+
+	return 0;
+}
+
 static int amdgpu_virt_init_ras_err_handler_data(struct amdgpu_device *adev)
 {
 	struct amdgpu_virt *virt = &adev->virt;
 	struct amdgpu_virt_ras_err_handler_data **data = &virt->virt_eh_data;
-	/* GPU will be marked bad on host if bp count more then 10,
-	 * so alloc 512 is enough.
-	 */
-	unsigned int align_space = 512;
+	unsigned int align_space = AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_INIT_CAPACITY;
 	void *bps = NULL;
 	struct amdgpu_bo **bps_bo = NULL;
 
@@ -302,12 +354,13 @@ static int amdgpu_virt_init_ras_err_handler_data(struct amdgpu_device *adev)
 	if (!bps)
 		goto bps_failure;
 
-	bps_bo = kmalloc_array(align_space, sizeof(*(*data)->bps_bo), GFP_KERNEL);
+	bps_bo = kcalloc(align_space, sizeof(*(*data)->bps_bo), GFP_KERNEL);
 	if (!bps_bo)
 		goto bps_bo_failure;
 
 	(*data)->bps = bps;
 	(*data)->bps_bo = bps_bo;
+	(*data)->capacity = align_space;
 	(*data)->count = 0;
 	(*data)->last_reserved = 0;
 
@@ -361,17 +414,33 @@ void amdgpu_virt_release_ras_err_handler_data(struct amdgpu_device *adev)
 	virt->virt_eh_data = NULL;
 }
 
-static void amdgpu_virt_ras_add_bps(struct amdgpu_device *adev,
-		struct eeprom_table_record *bps, int pages)
+static bool amdgpu_virt_ras_add_bps(struct amdgpu_device *adev,
+		const struct eeprom_table_record *bps, int pages)
 {
 	struct amdgpu_virt *virt = &adev->virt;
 	struct amdgpu_virt_ras_err_handler_data *data = virt->virt_eh_data;
+	int need;
 
-	if (!data)
-		return;
+	if (!data || pages <= 0)
+		return false;
+
+	if (pages > AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_MAX_CAPACITY - data->count) {
+		dev_warn_ratelimited(adev->dev,
+				     "RAS WARN: bad page table at capacity (count=%d pages=%d max=%u)\n",
+				     data->count, pages,
+				     AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_MAX_CAPACITY);
+		return false;
+	}
+
+	need = data->count + pages;
+	if (need > data->capacity &&
+	    amdgpu_virt_ras_realloc_eh_data_space(adev, data, need - data->capacity))
+		return false;
 
 	memcpy(&data->bps[data->count], bps, pages * sizeof(*data->bps));
 	data->count += pages;
+
+	return true;
 }
 
 static void amdgpu_virt_ras_reserve_bps(struct amdgpu_device *adev)
@@ -443,20 +512,22 @@ static void amdgpu_virt_add_bad_page(struct amdgpu_device *adev,
 
 	memset(&bp, 0, sizeof(bp));
 
-	if (bp_block_size) {
-		bp_cnt = bp_block_size / sizeof(uint64_t);
-		for (bp_idx = 0; bp_idx < bp_cnt; bp_idx++) {
-			retired_page = *(uint64_t *)(vram_usage_va +
-					bp_block_offset + bp_idx * sizeof(uint64_t));
-			bp.retired_page = retired_page;
+	if (!bp_block_size)
+		return;
 
-			if (amdgpu_virt_ras_check_bad_page(adev, retired_page))
-				continue;
+	bp_cnt = bp_block_size / sizeof(uint64_t);
+	for (bp_idx = 0; bp_idx < bp_cnt; bp_idx++) {
+		retired_page = *(uint64_t *)(vram_usage_va +
+				bp_block_offset + bp_idx * sizeof(uint64_t));
+		bp.retired_page = retired_page;
 
-			amdgpu_virt_ras_add_bps(adev, &bp, 1);
+		if (amdgpu_virt_ras_check_bad_page(adev, retired_page))
+			continue;
 
-			amdgpu_virt_ras_reserve_bps(adev);
-		}
+		if (!amdgpu_virt_ras_add_bps(adev, &bp, 1))
+			break;
+
+		amdgpu_virt_ras_reserve_bps(adev);
 	}
 }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_virt.h +2 -0
index d563deec0916b..d8500c3e48a17 100644
@@ -265,6 +265,8 @@ struct amdgpu_virt_ras_err_handler_data {
 	struct eeprom_table_record *bps;
 	/* point to reserved bo array */
 	struct amdgpu_bo **bps_bo;
+	/* number of slots in bps[] / bps_bo[] (always >= count) */
+	int capacity;
 	/* the count of entries */
 	int count;
 	/* last reserved entry's index + 1 */
Signature · 1 line
2.34.1
Reviewed-by on v5 1/1
#9 lore raw

RE: [PATCH v5] drm/amdgpu: grow VF RAS bad page table with bounded dynamic alloc

AMD General

Best Regards,
Thomas
-----Original Message-----
From: Xie, Chenglei <[email protected]>
Sent: Tuesday, May 26, 2026 3:06 AM
To: [email protected]
Cc: Chan, Hing Pong <[email protected]>; Luo, Zhigang <[email protected]>; Deucher, Alexander <[email protected]>; Xie, Chenglei <[email protected]>; Chai, Thomas <[email protected]>; Yi, Tony <[email protected]>
Subject: [PATCH v5] drm/amdgpu: grow VF RAS bad page table with bounded dynamic alloc

The VF RAS error handler used fixed-size bps[] / bps_bo[] arrays (512 slots). When the PF2VF bad-page block listed more entries than fit, amdgpu_virt_ras_add_bps() could memcpy() past the end of those arrays.

Replace the fixed backing store with a dynamically grown table:
- Add capacity to track allocated slots separately from count.
- Start at 512 slots and realloc bps / bps_bo together when full.
- Refuse growth beyond maximum EEPROM record limit (AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_MAX_CAPACITY).
- Return failure from amdgpu_virt_ras_add_bps() and stop processing
the PF2VF block if allocation fails or the cap is reached.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c | 111 +++++++++++++++++++----
 drivers/gpu/drm/amd/amdgpu/amdgpu_virt.h |   2 +
 2 files changed, 93 insertions(+), 20 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c +58 -4
index f8e58d12e39ae..7b0d425101611 100644
@@ -283,14 +283,66 @@ unsigned int amd_sriov_msg_checksum(void *obj,
        return ret;
 }
 
+#define AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_INIT_CAPACITY   512
+/* Max bad page slots allowed for SRIOV*/
+#define AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_MAX_CAPACITY    10665U
+
+/**
+ * amdgpu_virt_ras_realloc_eh_data_space - alloc/realloc VF bad-page
+@data->bps and @data->bps_bo
+ * @adev: amdgpu device
+ * @data: VF RAS error-handler data
+ * @pages: minimum number of new slots to add beyond @data->capacity
+ *
+ * Return: 0 on success, %-ENOMEM on failure.
+ */
+static int amdgpu_virt_ras_realloc_eh_data_space(struct amdgpu_device *adev,
+               struct amdgpu_virt_ras_err_handler_data *data,
+               int pages)
+{
+       struct eeprom_table_record *new_bps;
+       struct amdgpu_bo **new_bo;
+       unsigned int old_space;
+       unsigned int new_space;
+       unsigned int align_space;
+
+       old_space = (unsigned int)data->capacity;
+       new_space = old_space + max_t(unsigned int, (unsigned int)pages,
+                                     (unsigned int)AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_INIT_CAPACITY);
+       if (new_space < old_space || new_space > AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_MAX_CAPACITY)
+               return -ENOMEM;
+
+       align_space = ALIGN(new_space, AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_INIT_CAPACITY);
+       if (align_space > AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_MAX_CAPACITY)
+               return -ENOMEM;
+
+       new_bps = kmalloc_array(align_space, sizeof(*data->bps), GFP_KERNEL);
+       new_bo = kcalloc(align_space, sizeof(*data->bps_bo), GFP_KERNEL);
+       if (!new_bps || !new_bo) {
+               kfree(new_bps);
+               kfree(new_bo);
+               dev_warn_ratelimited(adev->dev,
+                                    "RAS WARN: failed to grow bad page table to %u slots\n",
+                                    align_space);
+               return -ENOMEM;
+       }
+
+       memcpy(new_bps, data->bps, data->count * sizeof(*data->bps));
+       memcpy(new_bo, data->bps_bo, data->count * sizeof(*data->bps_bo));
+
+       kfree(data->bps);
+       kfree(data->bps_bo);
+       data->bps = new_bps;
+       data->bps_bo = new_bo;
+       data->capacity = (int)align_space;
+
+       return 0;
+}
+
 static int amdgpu_virt_init_ras_err_handler_data(struct amdgpu_device *adev)  {
        struct amdgpu_virt *virt = &adev->virt;
        struct amdgpu_virt_ras_err_handler_data **data = &virt->virt_eh_data;
-       /* GPU will be marked bad on host if bp count more then 10,
-        * so alloc 512 is enough.
-        */
-       unsigned int align_space = 512;
+       unsigned int align_space =
+AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_INIT_CAPACITY;
        void *bps = NULL;
        struct amdgpu_bo **bps_bo = NULL;

@@ -302,12 +354,13 @@ static int amdgpu_virt_init_ras_err_handler_data(struct amdgpu_device *adev)

        if (!bps)
                goto bps_failure;

- bps_bo = kmalloc_array(align_space, sizeof(*(*data)->bps_bo), GFP_KERNEL);

+       bps_bo = kcalloc(align_space, sizeof(*(*data)->bps_bo), GFP_KERNEL);
        if (!bps_bo)
                goto bps_bo_failure;

        (*data)->bps = bps;
        (*data)->bps_bo = bps_bo;
+       (*data)->capacity = align_space;
        (*data)->count = 0;
        (*data)->last_reserved = 0;

@@ -361,17 +414,33 @@ void amdgpu_virt_release_ras_err_handler_data(struct amdgpu_device *adev)

        virt->virt_eh_data = NULL;

}

-static void amdgpu_virt_ras_add_bps(struct amdgpu_device *adev,
- struct eeprom_table_record *bps, int pages)
+static bool amdgpu_virt_ras_add_bps(struct amdgpu_device *adev,
+ const struct eeprom_table_record *bps, int pages)

 {
        struct amdgpu_virt *virt = &adev->virt;
        struct amdgpu_virt_ras_err_handler_data *data = virt->virt_eh_data;

+ int need;

- if (!data)
- return;
+ if (!data || pages <= 0)
+ return false;
+
+ if (pages > AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_MAX_CAPACITY - data->count) {
+ dev_warn_ratelimited(adev->dev,

+                                    "RAS WARN: bad page table at capacity (count=%d pages=%d max=%u)\n",
+                                    data->count, pages,
+                                    AMDGPU_VIRT_RAS_BAD_PAGE_TABLE_MAX_CAPACITY);

+ return false;
+ }
+
+ need = data->count + pages;
+ if (need > data->capacity &&
+ amdgpu_virt_ras_realloc_eh_data_space(adev, data, need - data->capacity))
+ return false;

        memcpy(&data->bps[data->count], bps, pages * sizeof(*data->bps));
        data->count += pages;

+
+ return true;
}

static void amdgpu_virt_ras_reserve_bps(struct amdgpu_device *adev) @@ -443,20 +512,22 @@ static void amdgpu_virt_add_bad_page(struct amdgpu_device *adev,

        memset(&bp, 0, sizeof(bp));

- if (bp_block_size) {
- bp_cnt = bp_block_size / sizeof(uint64_t);
- for (bp_idx = 0; bp_idx < bp_cnt; bp_idx++) {
- retired_page = *(uint64_t *)(vram_usage_va +
- bp_block_offset + bp_idx * sizeof(uint64_t));
- bp.retired_page = retired_page;
+ if (!bp_block_size)
+ return;

- if (amdgpu_virt_ras_check_bad_page(adev, retired_page))
- continue;
+ bp_cnt = bp_block_size / sizeof(uint64_t);
+ for (bp_idx = 0; bp_idx < bp_cnt; bp_idx++) {
+ retired_page = *(uint64_t *)(vram_usage_va +
+ bp_block_offset + bp_idx * sizeof(uint64_t));
+ bp.retired_page = retired_page;

- amdgpu_virt_ras_add_bps(adev, &bp, 1);
+ if (amdgpu_virt_ras_check_bad_page(adev, retired_page))
+ continue;

- amdgpu_virt_ras_reserve_bps(adev);
- }
+ if (!amdgpu_virt_ras_add_bps(adev, &bp, 1))
+ break;
+

+               amdgpu_virt_ras_reserve_bps(adev);
        }

}

drivers/gpu/drm/amd/amdgpu/amdgpu_virt.h +2 -0
index d563deec0916b..d8500c3e48a17 100644
@@ -265,6 +265,8 @@ struct amdgpu_virt_ras_err_handler_data {
        struct eeprom_table_record *bps;
        /* point to reserved bo array */
        struct amdgpu_bo **bps_bo;
+       /* number of slots in bps[] / bps_bo[] (always >= count) */
+       int capacity;
        /* the count of entries */
        int count;
        /* last reserved entry's index + 1 */

--
2.34.1