linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Robert Beckett <bob.beckett@collabora.com>
To: "Christian König" <christian.koenig@amd.com>,
	dri-devel@lists.freedesktop.org, "Huang Rui" <ray.huang@amd.com>,
	"David Airlie" <airlied@linux.ie>,
	"Daniel Vetter" <daniel@ffwll.ch>,
	"Matthew Auld" <matthew.auld@intel.com>
Cc: linux-kernel@vger.kernel.org
Subject: Re: [PATCH] drm/ttm: fix potential null ptr deref in when mem space alloc fails
Date: Mon, 21 Mar 2022 15:44:13 +0000	[thread overview]
Message-ID: <a29021bd-1447-038a-e141-d06b53173d36@collabora.com> (raw)
In-Reply-To: <8688c626-5858-18ba-593b-cdf179ca5201@amd.com>



On 21/03/2022 09:51, Christian König wrote:
> Am 18.03.22 um 20:50 schrieb Robert Beckett:
>> when allocating a resource in place it is common to free the buffer's
>> resource, then allocate a new resource in a different placement.
>>
>> e.g. amdgpu_bo_create_kernel_at calls ttm_resource_free, then calls
>> ttm_bo_mem_space.
> 
> Well yes I'm working the drivers towards this, but NAK at the moment. 
> Currently bo->resource is never expected to be NULL.
> 
> And yes I'm searching for this bug in amdgpu for quite a while. Where 
> exactly does that happen?

in my case, I am writing new code for i915 that does this. I will switch 
it to allocate the new resource first, then free the old one if successful.

For the existing amd case, see 
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c?h=v5.17#n384


amdgpu_bo_create_kernel_at calls ttm_resource_free, then calls 
ttm_bo_mem_space. If the ttm_bo_mem_space call fails (e.g. due to memory 
pressure), then the error path will try to deref bo->resource, which 
will be null at that point.


to fix this, I honestly don't see a reason to not also have the safety 
check for null there. It could check early and return an error if it is 
null. I think that defensive programming here makes sense, better than a 
null deref if someone programs it wrong.



> 
> Amdgpu is supposed to allocate a new resource first, then do a swap and 
> the free the old one.
> 
> Thanks,
> Christian.
> 
>>
>> In this situation, bo->resource will be null as it is cleared during
>> the initial freeing of the previous resource.
>> This leads to a null deref.
>>
>> Fixes: d3116756a710 (drm/ttm: rename bo->mem and make it a pointer)
>>
>> Signed-off-by: Robert Beckett <bob.beckett@collabora.com>
>> ---
>>   drivers/gpu/drm/ttm/ttm_bo.c | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/drivers/gpu/drm/ttm/ttm_bo.c b/drivers/gpu/drm/ttm/ttm_bo.c
>> index db3dc7ef5382..62b29ee7d040 100644
>> --- a/drivers/gpu/drm/ttm/ttm_bo.c
>> +++ b/drivers/gpu/drm/ttm/ttm_bo.c
>> @@ -875,7 +875,7 @@ int ttm_bo_mem_space(struct ttm_buffer_object *bo,
>>       }
>>   error:
>> -    if (bo->resource->mem_type == TTM_PL_SYSTEM && !bo->pin_count)
>> +    if (bo->resource && bo->resource->mem_type == TTM_PL_SYSTEM && 
>> !bo->pin_count)
>>           ttm_bo_move_to_lru_tail_unlocked(bo);
>>       return ret;
> 

  reply	other threads:[~2022-03-21 15:44 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-18 19:50 [PATCH] drm/ttm: fix potential null ptr deref in when mem space alloc fails Robert Beckett
2022-03-21  9:51 ` Christian König
2022-03-21 15:44   ` Robert Beckett [this message]
2022-03-22  7:17     ` Christian König

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=a29021bd-1447-038a-e141-d06b53173d36@collabora.com \
    --to=bob.beckett@collabora.com \
    --cc=airlied@linux.ie \
    --cc=christian.koenig@amd.com \
    --cc=daniel@ffwll.ch \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=matthew.auld@intel.com \
    --cc=ray.huang@amd.com \
    /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;
as well as URLs for NNTP newsgroup(s).