mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Christian König" <christian.koenig@amd.com>
To: Icenowy Zheng <zhengxingda@iscas.ac.cn>,
	Evanshenf <archwse@gmail.com>,
	dri-devel@lists.freedesktop.org,
	Natalie Vock <natalie.vock@gmx.de>,
	"Paneer Selvam, Arunpravin" <Arunpravin.PaneerSelvam@amd.com>
Cc: Jianmin Lv <lvjianmin@loongson.cn>,
	Qianhai Wu <wuqianhai@loongson.cn>,
	Huacai Chen <chenhuacai@kernel.org>,
	Mingcong Bai <jeffbai@aosc.io>, Xi Ruoyao <xry111@xry111.site>,
	Sui Jingfeng <suijingfeng@loongson.cn>,
	stable@vger.kernel.org, linux-kernel@vger.kernel.org,
	Huang Rui <ray.huang@amd.com>,
	Matthew Auld <matthew.auld@intel.com>,
	Matthew Brost <matthew.brost@intel.com>
Subject: Re: [PATCH 2/2] drm/loongson: Handle buffer mapping failures when clearing a BO
Date: Fri, 9 Oct 2026 16:14:06 +0200	[thread overview]
Message-ID: <2b0d0a02-99a5-447a-8e3f-6154b2660425@amd.com> (raw)
In-Reply-To: <f25629d4aa3c0132275b0a798e1491119fa8d502.camel@iscas.ac.cn>

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);
>>  
> 


  reply	other threads:[~2026-10-09 14:14 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2b0d0a02-99a5-447a-8e3f-6154b2660425@amd.com \
    --to=christian.koenig@amd.com \
    --cc=Arunpravin.PaneerSelvam@amd.com \
    --cc=archwse@gmail.com \
    --cc=chenhuacai@kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=jeffbai@aosc.io \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lvjianmin@loongson.cn \
    --cc=matthew.auld@intel.com \
    --cc=matthew.brost@intel.com \
    --cc=natalie.vock@gmx.de \
    --cc=ray.huang@amd.com \
    --cc=stable@vger.kernel.org \
    --cc=suijingfeng@loongson.cn \
    --cc=wuqianhai@loongson.cn \
    --cc=xry111@xry111.site \
    --cc=zhengxingda@iscas.ac.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®