* [PATCH 1/2] drm/loongson: Fix double free on BO initialization failure
@ 2026-10-03 9:34 Evanshenf
2026-10-03 9:34 ` [PATCH 2/2] drm/loongson: Handle buffer mapping failures when clearing a BO Evanshenf
2026-10-09 8:42 ` [PATCH 1/2] drm/loongson: Fix double free on BO initialization failure Icenowy Zheng
0 siblings, 2 replies; 7+ messages in thread
From: Evanshenf @ 2026-10-03 9:34 UTC (permalink / raw)
To: dri-devel
Cc: Jianmin Lv, Qianhai Wu, Huacai Chen, Mingcong Bai, Xi Ruoyao,
Icenowy Zheng, Sui Jingfeng, stable, linux-kernel, Evanshenf
If ttm_bo_init_validate() fails, it drops the buffer object's reference
and cleans it up through the supplied destroy callback. lsdc_bo_destroy()
releases the GEM object and frees the enclosing lsdc_bo, so freeing it
again in lsdc_bo_create() causes a double free on a synchronous failure
path.
Let TTM own the cleanup after initialization has started and return the
error directly. Keep the explicit free on drm_gem_object_init() failure,
which occurs before ownership is passed to TTM.
Tested on LS7A2000 by making drm_vma_offset_add() return -ENOSPC for one
selected dumb-buffer creation. The error reached userspace, the destroy
callback ran once, and no handle was published or tracked BO retained.
Normal buffer creation, zeroing, mapping, readback and release passed.
AI assistance was used for the ownership analysis, fix, fault-injection
tools, build and test execution.
Fixes: f39db26c5428 ("drm: Add kms driver for loongson display controller")
Cc: stable@vger.kernel.org
Assisted-by: LLM
Signed-off-by: Evanshenf <archwse@gmail.com>
---
drivers/gpu/drm/loongson/lsdc_ttm.c | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/drivers/gpu/drm/loongson/lsdc_ttm.c b/drivers/gpu/drm/loongson/lsdc_ttm.c
index d7441d9..88536e2 100644
--- a/drivers/gpu/drm/loongson/lsdc_ttm.c
+++ b/drivers/gpu/drm/loongson/lsdc_ttm.c
@@ -475,10 +475,8 @@ struct lsdc_bo *lsdc_bo_create(struct drm_device *ddev,
ret = ttm_bo_init_validate(bdev, tbo, bo_type, &lbo->placement, 0,
false, sg, resv, lsdc_bo_destroy);
- if (ret) {
- kfree(lbo);
+ if (ret)
return ERR_PTR(ret);
- }
return lbo;
}
base-commit: bca45af5998a05f34b13a2ef11e639bac9c62643
--
2.43.0
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 2/2] drm/loongson: Handle buffer mapping failures when clearing a BO
2026-10-03 9:34 [PATCH 1/2] drm/loongson: Fix double free on BO initialization failure Evanshenf
@ 2026-10-03 9:34 ` Evanshenf
2026-10-09 13:50 ` Icenowy Zheng
2026-10-09 8:42 ` [PATCH 1/2] drm/loongson: Fix double free on BO initialization failure Icenowy Zheng
1 sibling, 1 reply; 7+ messages in thread
From: Evanshenf @ 2026-10-03 9:34 UTC (permalink / raw)
To: dri-devel
Cc: Jianmin Lv, Qianhai Wu, Huacai Chen, Mingcong Bai, Xi Ruoyao,
Icenowy Zheng, Sui Jingfeng, stable, linux-kernel, Evanshenf
lsdc_bo_clear() ignores the return value from lsdc_bo_kmap() and writes
to lbo->kptr unconditionally. When the first mapping of a newly allocated
buffer fails, kptr is still NULL and the subsequent memset accesses it.
Return the mapping error without touching the buffer and propagate it
from lsdc_gem_object_create(). Install the GEM object functions before
clearing so that drm_gem_object_put() can release the buffer correctly
on failure. Do not add the failed object to the tracking list or return
an uncleared buffer to the caller.
Imported buffers still skip clearing, and successfully mapped buffers
are cleared and unmapped as before.
Tested on LS7A2000 with a one-shot range error in ttm_bo_kmap(). Its
-EINVAL return reached userspace, the object was destroyed once, and
no GEM handle was published. Normal create/map/zero/write/read/close
cycles passed, with the tracked BO count and VRAM usage unchanged.
AI assistance was used for the lifetime analysis, fix, fault-injection
tools, build and test execution.
Fixes: f39db26c5428 ("drm: Add kms driver for loongson display controller")
Cc: stable@vger.kernel.org
Assisted-by: LLM
Signed-off-by: Evanshenf <archwse@gmail.com>
---
drivers/gpu/drm/loongson/lsdc_gem.c | 12 ++++++++----
drivers/gpu/drm/loongson/lsdc_ttm.c | 10 ++++++++--
drivers/gpu/drm/loongson/lsdc_ttm.h | 2 +-
3 files changed, 17 insertions(+), 7 deletions(-)
diff --git a/drivers/gpu/drm/loongson/lsdc_gem.c b/drivers/gpu/drm/loongson/lsdc_gem.c
index 2fb0348..9eba4e1 100644
--- a/drivers/gpu/drm/loongson/lsdc_gem.c
+++ b/drivers/gpu/drm/loongson/lsdc_gem.c
@@ -157,14 +157,18 @@ struct drm_gem_object *lsdc_gem_object_create(struct drm_device *ddev,
return ERR_PTR(ret);
}
+ gobj = &lbo->tbo.base;
+ gobj->funcs = &lsdc_gem_object_funcs;
+
if (!sg) {
/* VRAM is filled with random data */
- lsdc_bo_clear(lbo);
+ ret = lsdc_bo_clear(lbo);
+ if (ret) {
+ drm_gem_object_put(gobj);
+ return ERR_PTR(ret);
+ }
}
- gobj = &lbo->tbo.base;
- gobj->funcs = &lsdc_gem_object_funcs;
-
/* tracking the BOs we created */
mutex_lock(&ldev->gem.mutex);
list_add_tail(&lbo->list, &ldev->gem.objects);
diff --git a/drivers/gpu/drm/loongson/lsdc_ttm.c b/drivers/gpu/drm/loongson/lsdc_ttm.c
index 88536e2..7b30dea 100644
--- a/drivers/gpu/drm/loongson/lsdc_ttm.c
+++ b/drivers/gpu/drm/loongson/lsdc_ttm.c
@@ -388,9 +388,13 @@ void lsdc_bo_kunmap(struct lsdc_bo *lbo)
ttm_bo_kunmap(&lbo->kmap);
}
-void lsdc_bo_clear(struct lsdc_bo *lbo)
+int lsdc_bo_clear(struct lsdc_bo *lbo)
{
- lsdc_bo_kmap(lbo);
+ int ret;
+
+ ret = lsdc_bo_kmap(lbo);
+ if (ret)
+ return ret;
if (lbo->is_iomem)
memset_io((void __iomem *)lbo->kptr, 0, lbo->size);
@@ -398,6 +402,8 @@ void lsdc_bo_clear(struct lsdc_bo *lbo)
memset(lbo->kptr, 0, lbo->size);
lsdc_bo_kunmap(lbo);
+
+ return 0;
}
int lsdc_bo_evict_vram(struct drm_device *ddev)
diff --git a/drivers/gpu/drm/loongson/lsdc_ttm.h b/drivers/gpu/drm/loongson/lsdc_ttm.h
index 843e147..df47d9b 100644
--- a/drivers/gpu/drm/loongson/lsdc_ttm.h
+++ b/drivers/gpu/drm/loongson/lsdc_ttm.h
@@ -89,7 +89,7 @@ size_t lsdc_bo_size(struct lsdc_bo *lbo);
int lsdc_bo_kmap(struct lsdc_bo *lbo);
void lsdc_bo_kunmap(struct lsdc_bo *lbo);
-void lsdc_bo_clear(struct lsdc_bo *lbo);
+int lsdc_bo_clear(struct lsdc_bo *lbo);
int lsdc_bo_evict_vram(struct drm_device *ddev);
--
2.43.0
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 1/2] drm/loongson: Fix double free on BO initialization failure
2026-10-03 9:34 [PATCH 1/2] drm/loongson: Fix double free on BO initialization failure Evanshenf
2026-10-03 9:34 ` [PATCH 2/2] drm/loongson: Handle buffer mapping failures when clearing a BO Evanshenf
@ 2026-10-09 8:42 ` Icenowy Zheng
2026-10-09 9:37 ` wuqianhai
1 sibling, 1 reply; 7+ messages in thread
From: Icenowy Zheng @ 2026-10-09 8:42 UTC (permalink / raw)
To: Evanshenf, dri-devel
Cc: Jianmin Lv, Qianhai Wu, Huacai Chen, Mingcong Bai, Xi Ruoyao,
Sui Jingfeng, stable, linux-kernel
在 2026-10-03六的 09:34 +0000,Evanshenf写道:
> If ttm_bo_init_validate() fails, it drops the buffer object's
> reference
> and cleans it up through the supplied destroy callback.
> lsdc_bo_destroy()
> releases the GEM object and frees the enclosing lsdc_bo, so freeing
> it
> again in lsdc_bo_create() causes a double free on a synchronous
> failure
> path.
>
> Let TTM own the cleanup after initialization has started and return
> the
> error directly. Keep the explicit free on drm_gem_object_init()
> failure,
> which occurs before ownership is passed to TTM.
>
> Tested on LS7A2000 by making drm_vma_offset_add() return -ENOSPC for
> one
> selected dumb-buffer creation. The error reached userspace, the
> destroy
> callback ran once, and no handle was published or tracked BO
> retained.
> Normal buffer creation, zeroing, mapping, readback and release
> passed.
>
> AI assistance was used for the ownership analysis, fix, fault-
> injection
> tools, build and test execution.
>
> Fixes: f39db26c5428 ("drm: Add kms driver for loongson display
> controller")
> Cc: stable@vger.kernel.org
> Assisted-by: LLM
> Signed-off-by: Evanshenf <archwse@gmail.com>
> ---
> drivers/gpu/drm/loongson/lsdc_ttm.c | 4 +---
> 1 file changed, 1 insertion(+), 3 deletions(-)
>
> diff --git a/drivers/gpu/drm/loongson/lsdc_ttm.c
> b/drivers/gpu/drm/loongson/lsdc_ttm.c
> index d7441d9..88536e2 100644
> --- a/drivers/gpu/drm/loongson/lsdc_ttm.c
> +++ b/drivers/gpu/drm/loongson/lsdc_ttm.c
> @@ -475,10 +475,8 @@ struct lsdc_bo *lsdc_bo_create(struct drm_device
> *ddev,
>
> ret = ttm_bo_init_validate(bdev, tbo, bo_type, &lbo-
> >placement, 0,
> false, sg, resv,
> lsdc_bo_destroy);
> - if (ret) {
> - kfree(lbo);
> + if (ret)
> return ERR_PTR(ret);
It looks like this fix is valid, ttm_bo_init_reserved() (called by
ttm_bo_init_validate() ) will do a ttm_bo_put() operation when failure,
which leads to calling the destroy callback.
```
Reviewed-by: Icenowy Zheng <zhengxingda@iscas.ac.cn>
```
Thanks,
Icenowy
> - }
>
> return lbo;
> }
>
> base-commit: bca45af5998a05f34b13a2ef11e639bac9c62643
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 1/2] drm/loongson: Fix double free on BO initialization failure
2026-10-09 8:42 ` [PATCH 1/2] drm/loongson: Fix double free on BO initialization failure Icenowy Zheng
@ 2026-10-09 9:37 ` wuqianhai
0 siblings, 0 replies; 7+ messages in thread
From: wuqianhai @ 2026-10-09 9:37 UTC (permalink / raw)
To: Icenowy Zheng, Evanshenf, dri-devel
Cc: Jianmin Lv, Huacai Chen, Mingcong Bai, Xi Ruoyao, stable, linux-kernel
Reviewed-by: Qianhai Wu <wuqianhai@loongson.cn>
The fix looks correct and the TTM ownership analysis is accurate.
Thanks,
Qianhai Wu
在 2026/10/9 16:42, Icenowy Zheng 写道:
> 在 2026-10-03六的 09:34 +0000,Evanshenf写道:
>> If ttm_bo_init_validate() fails, it drops the buffer object's
>> reference
>> and cleans it up through the supplied destroy callback.
>> lsdc_bo_destroy()
>> releases the GEM object and frees the enclosing lsdc_bo, so freeing
>> it
>> again in lsdc_bo_create() causes a double free on a synchronous
>> failure
>> path.
>>
>> Let TTM own the cleanup after initialization has started and return
>> the
>> error directly. Keep the explicit free on drm_gem_object_init()
>> failure,
>> which occurs before ownership is passed to TTM.
>>
>> Tested on LS7A2000 by making drm_vma_offset_add() return -ENOSPC for
>> one
>> selected dumb-buffer creation. The error reached userspace, the
>> destroy
>> callback ran once, and no handle was published or tracked BO
>> retained.
>> Normal buffer creation, zeroing, mapping, readback and release
>> passed.
>>
>> AI assistance was used for the ownership analysis, fix, fault-
>> injection
>> tools, build and test execution.
>>
>> Fixes: f39db26c5428 ("drm: Add kms driver for loongson display
>> controller")
>> Cc: stable@vger.kernel.org
>> Assisted-by: LLM
>> Signed-off-by: Evanshenf <archwse@gmail.com>
>> ---
>> drivers/gpu/drm/loongson/lsdc_ttm.c | 4 +---
>> 1 file changed, 1 insertion(+), 3 deletions(-)
>>
>> diff --git a/drivers/gpu/drm/loongson/lsdc_ttm.c
>> b/drivers/gpu/drm/loongson/lsdc_ttm.c
>> index d7441d9..88536e2 100644
>> --- a/drivers/gpu/drm/loongson/lsdc_ttm.c
>> +++ b/drivers/gpu/drm/loongson/lsdc_ttm.c
>> @@ -475,10 +475,8 @@ struct lsdc_bo *lsdc_bo_create(struct drm_device
>> *ddev,
>>
>> ret = ttm_bo_init_validate(bdev, tbo, bo_type, &lbo-
>>> placement, 0,
>> false, sg, resv,
>> lsdc_bo_destroy);
>> - if (ret) {
>> - kfree(lbo);
>> + if (ret)
>> return ERR_PTR(ret);
>
> It looks like this fix is valid, ttm_bo_init_reserved() (called by
> ttm_bo_init_validate() ) will do a ttm_bo_put() operation when failure,
> which leads to calling the destroy callback.
>
> ```
> Reviewed-by: Icenowy Zheng <zhengxingda@iscas.ac.cn>
> ```
>
> Thanks,
> Icenowy
>
>> - }
>>
>> return lbo;
>> }
>>
>> base-commit: bca45af5998a05f34b13a2ef11e639bac9c62643
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 2/2] drm/loongson: Handle buffer mapping failures when clearing a BO
2026-10-03 9:34 ` [PATCH 2/2] drm/loongson: Handle buffer mapping failures when clearing a BO Evanshenf
@ 2026-10-09 13:50 ` Icenowy Zheng
2026-10-09 14:14 ` Christian König
0 siblings, 1 reply; 7+ messages in thread
From: Icenowy Zheng @ 2026-10-09 13:50 UTC (permalink / raw)
To: Evanshenf, dri-devel
Cc: Jianmin Lv, Qianhai Wu, Huacai Chen, Mingcong Bai, Xi Ruoyao,
Sui Jingfeng, stable, linux-kernel, Christian Koenig, Huang Rui,
Matthew Auld, Matthew Brost
在 2026-10-03六的 09:34 +0000,Evanshenf写道:
> lsdc_bo_clear() ignores the return value from lsdc_bo_kmap() and
> writes
> to lbo->kptr unconditionally. When the first mapping of a newly
> allocated
> buffer fails, kptr is still NULL and the subsequent memset accesses
> it.
>
> Return the mapping error without touching the buffer and propagate it
> from lsdc_gem_object_create(). Install the GEM object functions
> before
> clearing so that drm_gem_object_put() can release the buffer
> correctly
> on failure. Do not add the failed object to the tracking list or
> return
> an uncleared buffer to the caller.
>
> Imported buffers still skip clearing, and successfully mapped buffers
> are cleared and unmapped as before.
>
> Tested on LS7A2000 with a one-shot range error in ttm_bo_kmap(). Its
> -EINVAL return reached userspace, the object was destroyed once, and
> no GEM handle was published. Normal create/map/zero/write/read/close
> cycles passed, with the tracked BO count and VRAM usage unchanged.
>
> AI assistance was used for the lifetime analysis, fix, fault-
> injection
> tools, build and test execution.
>
> Fixes: f39db26c5428 ("drm: Add kms driver for loongson display
> controller")
> Cc: stable@vger.kernel.org
> Assisted-by: LLM
> Signed-off-by: Evanshenf <archwse@gmail.com>
> ---
> drivers/gpu/drm/loongson/lsdc_gem.c | 12 ++++++++----
> drivers/gpu/drm/loongson/lsdc_ttm.c | 10 ++++++++--
> drivers/gpu/drm/loongson/lsdc_ttm.h | 2 +-
> 3 files changed, 17 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/gpu/drm/loongson/lsdc_gem.c
> b/drivers/gpu/drm/loongson/lsdc_gem.c
> index 2fb0348..9eba4e1 100644
> --- a/drivers/gpu/drm/loongson/lsdc_gem.c
> +++ b/drivers/gpu/drm/loongson/lsdc_gem.c
> @@ -157,14 +157,18 @@ struct drm_gem_object
> *lsdc_gem_object_create(struct drm_device *ddev,
> return ERR_PTR(ret);
> }
>
> + gobj = &lbo->tbo.base;
> + gobj->funcs = &lsdc_gem_object_funcs;
> +
> if (!sg) {
> /* VRAM is filled with random data */
> - lsdc_bo_clear(lbo);
> + ret = lsdc_bo_clear(lbo);
> + if (ret) {
> + drm_gem_object_put(gobj);
> + return ERR_PTR(ret);
> + }
> }
>
> - gobj = &lbo->tbo.base;
> - gobj->funcs = &lsdc_gem_object_funcs;
> -
> /* tracking the BOs we created */
> mutex_lock(&ldev->gem.mutex);
> list_add_tail(&lbo->list, &ldev->gem.objects);
> diff --git a/drivers/gpu/drm/loongson/lsdc_ttm.c
> b/drivers/gpu/drm/loongson/lsdc_ttm.c
> index 88536e2..7b30dea 100644
> --- a/drivers/gpu/drm/loongson/lsdc_ttm.c
> +++ b/drivers/gpu/drm/loongson/lsdc_ttm.c
> @@ -388,9 +388,13 @@ void lsdc_bo_kunmap(struct lsdc_bo *lbo)
> ttm_bo_kunmap(&lbo->kmap);
> }
>
> -void lsdc_bo_clear(struct lsdc_bo *lbo)
> +int lsdc_bo_clear(struct lsdc_bo *lbo)
> {
> - lsdc_bo_kmap(lbo);
> + int ret;
> +
> + ret = lsdc_bo_kmap(lbo);
> + if (ret)
> + return ret;
>
> if (lbo->is_iomem)
> memset_io((void __iomem *)lbo->kptr, 0, lbo->size);
> @@ -398,6 +402,8 @@ void lsdc_bo_clear(struct lsdc_bo *lbo)
> memset(lbo->kptr, 0, lbo->size);
>
> lsdc_bo_kunmap(lbo);
> +
> + return 0;
> }
>
> int lsdc_bo_evict_vram(struct drm_device *ddev)
Well I currently don't know whether the clearing is meaningful...
It seems that drm_gem_vram_helper doesn't do this.
Cc'ing TTM maintainers for an answer.
Thanks,
Icenowy
> diff --git a/drivers/gpu/drm/loongson/lsdc_ttm.h
> b/drivers/gpu/drm/loongson/lsdc_ttm.h
> index 843e147..df47d9b 100644
> --- a/drivers/gpu/drm/loongson/lsdc_ttm.h
> +++ b/drivers/gpu/drm/loongson/lsdc_ttm.h
> @@ -89,7 +89,7 @@ size_t lsdc_bo_size(struct lsdc_bo *lbo);
>
> int lsdc_bo_kmap(struct lsdc_bo *lbo);
> void lsdc_bo_kunmap(struct lsdc_bo *lbo);
> -void lsdc_bo_clear(struct lsdc_bo *lbo);
> +int lsdc_bo_clear(struct lsdc_bo *lbo);
>
> int lsdc_bo_evict_vram(struct drm_device *ddev);
>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 2/2] drm/loongson: Handle buffer mapping failures when clearing a BO
2026-10-09 13:50 ` Icenowy Zheng
@ 2026-10-09 14:14 ` Christian König
2026-10-09 14:20 ` Icenowy Zheng
0 siblings, 1 reply; 7+ messages in thread
From: Christian König @ 2026-10-09 14:14 UTC (permalink / raw)
To: Icenowy Zheng, Evanshenf, dri-devel, Natalie Vock, Paneer Selvam,
Arunpravin
Cc: Jianmin Lv, Qianhai Wu, Huacai Chen, Mingcong Bai, Xi Ruoyao,
Sui Jingfeng, stable, linux-kernel, Huang Rui, Matthew Auld,
Matthew Brost
On 10/9/26 15:50, Icenowy Zheng wrote:
> 在 2026-10-03六的 09:34 +0000,Evanshenf写道:
>> lsdc_bo_clear() ignores the return value from lsdc_bo_kmap() and
>> writes
>> to lbo->kptr unconditionally. When the first mapping of a newly
>> allocated
>> buffer fails, kptr is still NULL and the subsequent memset accesses
>> it.
>>
>> Return the mapping error without touching the buffer and propagate it
>> from lsdc_gem_object_create(). Install the GEM object functions
>> before
>> clearing so that drm_gem_object_put() can release the buffer
>> correctly
>> on failure. Do not add the failed object to the tracking list or
>> return
>> an uncleared buffer to the caller.
>>
>> Imported buffers still skip clearing, and successfully mapped buffers
>> are cleared and unmapped as before.
>>
>> Tested on LS7A2000 with a one-shot range error in ttm_bo_kmap(). Its
>> -EINVAL return reached userspace, the object was destroyed once, and
>> no GEM handle was published. Normal create/map/zero/write/read/close
>> cycles passed, with the tracked BO count and VRAM usage unchanged.
>>
>> AI assistance was used for the lifetime analysis, fix, fault-
>> injection
>> tools, build and test execution.
>>
>> Fixes: f39db26c5428 ("drm: Add kms driver for loongson display
>> controller")
>> Cc: stable@vger.kernel.org
>> Assisted-by: LLM
>> Signed-off-by: Evanshenf <archwse@gmail.com>
>> ---
>> drivers/gpu/drm/loongson/lsdc_gem.c | 12 ++++++++----
>> drivers/gpu/drm/loongson/lsdc_ttm.c | 10 ++++++++--
>> drivers/gpu/drm/loongson/lsdc_ttm.h | 2 +-
>> 3 files changed, 17 insertions(+), 7 deletions(-)
>>
>> diff --git a/drivers/gpu/drm/loongson/lsdc_gem.c
>> b/drivers/gpu/drm/loongson/lsdc_gem.c
>> index 2fb0348..9eba4e1 100644
>> --- a/drivers/gpu/drm/loongson/lsdc_gem.c
>> +++ b/drivers/gpu/drm/loongson/lsdc_gem.c
>> @@ -157,14 +157,18 @@ struct drm_gem_object
>> *lsdc_gem_object_create(struct drm_device *ddev,
>> return ERR_PTR(ret);
>> }
>>
>> + gobj = &lbo->tbo.base;
>> + gobj->funcs = &lsdc_gem_object_funcs;
>> +
>> if (!sg) {
>> /* VRAM is filled with random data */
>> - lsdc_bo_clear(lbo);
>> + ret = lsdc_bo_clear(lbo);
>> + if (ret) {
>> + drm_gem_object_put(gobj);
>> + return ERR_PTR(ret);
>> + }
>> }
>>
>> - gobj = &lbo->tbo.base;
>> - gobj->funcs = &lsdc_gem_object_funcs;
>> -
>> /* tracking the BOs we created */
>> mutex_lock(&ldev->gem.mutex);
>> list_add_tail(&lbo->list, &ldev->gem.objects);
>> diff --git a/drivers/gpu/drm/loongson/lsdc_ttm.c
>> b/drivers/gpu/drm/loongson/lsdc_ttm.c
>> index 88536e2..7b30dea 100644
>> --- a/drivers/gpu/drm/loongson/lsdc_ttm.c
>> +++ b/drivers/gpu/drm/loongson/lsdc_ttm.c
>> @@ -388,9 +388,13 @@ void lsdc_bo_kunmap(struct lsdc_bo *lbo)
>> ttm_bo_kunmap(&lbo->kmap);
>> }
>>
>> -void lsdc_bo_clear(struct lsdc_bo *lbo)
>> +int lsdc_bo_clear(struct lsdc_bo *lbo)
>> {
>> - lsdc_bo_kmap(lbo);
>> + int ret;
>> +
>> + ret = lsdc_bo_kmap(lbo);
>> + if (ret)
>> + return ret;
>>
>> if (lbo->is_iomem)
>> memset_io((void __iomem *)lbo->kptr, 0, lbo->size);
>> @@ -398,6 +402,8 @@ void lsdc_bo_clear(struct lsdc_bo *lbo)
>> memset(lbo->kptr, 0, lbo->size);
>>
>> lsdc_bo_kunmap(lbo);
>> +
>> + return 0;
>> }
>>
>> int lsdc_bo_evict_vram(struct drm_device *ddev)
>
> Well I currently don't know whether the clearing is meaningful...
>
> It seems that drm_gem_vram_helper doesn't do this.
>
> Cc'ing TTM maintainers for an answer.
Natalie and Arun are TTM maintainers now, but I will try to answer here as well.
OpenGL doesn't necessarily require clearing of VRAM nor isolation of VRAM data between processes.
So it is up to the driver if they want this or not. Classically a lot of drivers didn't clear VRAM allocations until explicitely asked.
But if you clear the memory you should have proper error handling and a kmap/vmap can fail at any time for multiple reasons.
Regards,
Christian.
>
> Thanks,
> Icenowy
>
>> diff --git a/drivers/gpu/drm/loongson/lsdc_ttm.h
>> b/drivers/gpu/drm/loongson/lsdc_ttm.h
>> index 843e147..df47d9b 100644
>> --- a/drivers/gpu/drm/loongson/lsdc_ttm.h
>> +++ b/drivers/gpu/drm/loongson/lsdc_ttm.h
>> @@ -89,7 +89,7 @@ size_t lsdc_bo_size(struct lsdc_bo *lbo);
>>
>> int lsdc_bo_kmap(struct lsdc_bo *lbo);
>> void lsdc_bo_kunmap(struct lsdc_bo *lbo);
>> -void lsdc_bo_clear(struct lsdc_bo *lbo);
>> +int lsdc_bo_clear(struct lsdc_bo *lbo);
>>
>> int lsdc_bo_evict_vram(struct drm_device *ddev);
>>
>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 2/2] drm/loongson: Handle buffer mapping failures when clearing a BO
2026-10-09 14:14 ` Christian König
@ 2026-10-09 14:20 ` Icenowy Zheng
0 siblings, 0 replies; 7+ messages in thread
From: Icenowy Zheng @ 2026-10-09 14:20 UTC (permalink / raw)
To: Christian König, Evanshenf, dri-devel, Natalie Vock,
Paneer Selvam, Arunpravin
Cc: Jianmin Lv, Qianhai Wu, Huacai Chen, Mingcong Bai, Xi Ruoyao,
Sui Jingfeng, stable, linux-kernel, Huang Rui, Matthew Auld,
Matthew Brost
在 2026-10-09五的 16:14 +0200,Christian König写道:
> On 10/9/26 15:50, Icenowy Zheng wrote:
> > 在 2026-10-03六的 09:34 +0000,Evanshenf写道:
> > > lsdc_bo_clear() ignores the return value from lsdc_bo_kmap() and
> > > writes
> > > to lbo->kptr unconditionally. When the first mapping of a newly
> > > allocated
> > > buffer fails, kptr is still NULL and the subsequent memset
> > > accesses
> > > it.
> > >
> > > Return the mapping error without touching the buffer and
> > > propagate it
> > > from lsdc_gem_object_create(). Install the GEM object functions
> > > before
> > > clearing so that drm_gem_object_put() can release the buffer
> > > correctly
> > > on failure. Do not add the failed object to the tracking list or
> > > return
> > > an uncleared buffer to the caller.
> > >
> > > Imported buffers still skip clearing, and successfully mapped
> > > buffers
> > > are cleared and unmapped as before.
> > >
> > > Tested on LS7A2000 with a one-shot range error in ttm_bo_kmap().
> > > Its
> > > -EINVAL return reached userspace, the object was destroyed once,
> > > and
> > > no GEM handle was published. Normal
> > > create/map/zero/write/read/close
> > > cycles passed, with the tracked BO count and VRAM usage
> > > unchanged.
> > >
> > > AI assistance was used for the lifetime analysis, fix, fault-
> > > injection
> > > tools, build and test execution.
> > >
> > > Fixes: f39db26c5428 ("drm: Add kms driver for loongson display
> > > controller")
> > > Cc: stable@vger.kernel.org
> > > Assisted-by: LLM
> > > Signed-off-by: Evanshenf <archwse@gmail.com>
> > > ---
> > > drivers/gpu/drm/loongson/lsdc_gem.c | 12 ++++++++----
> > > drivers/gpu/drm/loongson/lsdc_ttm.c | 10 ++++++++--
> > > drivers/gpu/drm/loongson/lsdc_ttm.h | 2 +-
> > > 3 files changed, 17 insertions(+), 7 deletions(-)
> > >
> > > diff --git a/drivers/gpu/drm/loongson/lsdc_gem.c
> > > b/drivers/gpu/drm/loongson/lsdc_gem.c
> > > index 2fb0348..9eba4e1 100644
> > > --- a/drivers/gpu/drm/loongson/lsdc_gem.c
> > > +++ b/drivers/gpu/drm/loongson/lsdc_gem.c
> > > @@ -157,14 +157,18 @@ struct drm_gem_object
> > > *lsdc_gem_object_create(struct drm_device *ddev,
> > > return ERR_PTR(ret);
> > > }
> > >
> > > + gobj = &lbo->tbo.base;
> > > + gobj->funcs = &lsdc_gem_object_funcs;
> > > +
> > > if (!sg) {
> > > /* VRAM is filled with random data */
> > > - lsdc_bo_clear(lbo);
> > > + ret = lsdc_bo_clear(lbo);
> > > + if (ret) {
> > > + drm_gem_object_put(gobj);
> > > + return ERR_PTR(ret);
> > > + }
> > > }
> > >
> > > - gobj = &lbo->tbo.base;
> > > - gobj->funcs = &lsdc_gem_object_funcs;
> > > -
> > > /* tracking the BOs we created */
> > > mutex_lock(&ldev->gem.mutex);
> > > list_add_tail(&lbo->list, &ldev->gem.objects);
> > > diff --git a/drivers/gpu/drm/loongson/lsdc_ttm.c
> > > b/drivers/gpu/drm/loongson/lsdc_ttm.c
> > > index 88536e2..7b30dea 100644
> > > --- a/drivers/gpu/drm/loongson/lsdc_ttm.c
> > > +++ b/drivers/gpu/drm/loongson/lsdc_ttm.c
> > > @@ -388,9 +388,13 @@ void lsdc_bo_kunmap(struct lsdc_bo *lbo)
> > > ttm_bo_kunmap(&lbo->kmap);
> > > }
> > >
> > > -void lsdc_bo_clear(struct lsdc_bo *lbo)
> > > +int lsdc_bo_clear(struct lsdc_bo *lbo)
> > > {
> > > - lsdc_bo_kmap(lbo);
> > > + int ret;
> > > +
> > > + ret = lsdc_bo_kmap(lbo);
> > > + if (ret)
> > > + return ret;
> > >
> > > if (lbo->is_iomem)
> > > memset_io((void __iomem *)lbo->kptr, 0, lbo-
> > > >size);
> > > @@ -398,6 +402,8 @@ void lsdc_bo_clear(struct lsdc_bo *lbo)
> > > memset(lbo->kptr, 0, lbo->size);
> > >
> > > lsdc_bo_kunmap(lbo);
> > > +
> > > + return 0;
> > > }
> > >
> > > int lsdc_bo_evict_vram(struct drm_device *ddev)
> >
> > Well I currently don't know whether the clearing is meaningful...
> >
> > It seems that drm_gem_vram_helper doesn't do this.
> >
> > Cc'ing TTM maintainers for an answer.
>
> Natalie and Arun are TTM maintainers now, but I will try to answer
> here as well.
Ah I heard about TTM maintenance change, but I used the mailing
addresses in v7.3-rc6.
>
> OpenGL doesn't necessarily require clearing of VRAM nor isolation of
> VRAM data between processes.
>
> So it is up to the driver if they want this or not. Classically a lot
> of drivers didn't clear VRAM allocations until explicitely asked.
Then in this case I think it's better to just remove the VRAM clearing
code -- this driver is even not related to render acceleration at all
now (and getting rendering to work would be so hacky because loongson
DC's are paired with either Vivante GPUs with dedicated VRAM or
proprietary GPUs).
>
> But if you clear the memory you should have proper error handling and
> a kmap/vmap can fail at any time for multiple reasons.
Yes, if the other maintainers prefer to keep the memory clearing code,
I will accept this specific patch.
Thanks,
Icenowy
>
> Regards,
> Christian.
>
> >
> > Thanks,
> > Icenowy
> >
> > > diff --git a/drivers/gpu/drm/loongson/lsdc_ttm.h
> > > b/drivers/gpu/drm/loongson/lsdc_ttm.h
> > > index 843e147..df47d9b 100644
> > > --- a/drivers/gpu/drm/loongson/lsdc_ttm.h
> > > +++ b/drivers/gpu/drm/loongson/lsdc_ttm.h
> > > @@ -89,7 +89,7 @@ size_t lsdc_bo_size(struct lsdc_bo *lbo);
> > >
> > > int lsdc_bo_kmap(struct lsdc_bo *lbo);
> > > void lsdc_bo_kunmap(struct lsdc_bo *lbo);
> > > -void lsdc_bo_clear(struct lsdc_bo *lbo);
> > > +int lsdc_bo_clear(struct lsdc_bo *lbo);
> > >
> > > int lsdc_bo_evict_vram(struct drm_device *ddev);
> > >
> >
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-10-09 14:21 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-03 9:34 [PATCH 1/2] drm/loongson: Fix double free on BO initialization failure Evanshenf
2026-10-03 9:34 ` [PATCH 2/2] drm/loongson: Handle buffer mapping failures when clearing a BO Evanshenf
2026-10-09 13:50 ` Icenowy Zheng
2026-10-09 14:14 ` Christian König
2026-10-09 14:20 ` Icenowy Zheng
2026-10-09 8:42 ` [PATCH 1/2] drm/loongson: Fix double free on BO initialization failure Icenowy Zheng
2026-10-09 9:37 ` wuqianhai
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®