From: Philip Yang <yangp@amd.com>
To: "Chen, Xiaogang" <xiaogang.chen@amd.com>,
Philip Yang <Philip.Yang@amd.com>,
amd-gfx@lists.freedesktop.org
Cc: Felix.Kuehling@amd.com, harish.kasiviswanathan@amd.com
Subject: Re: [PATCH 1/3] drm/amdgpu: svm check hmm range kzalloc return NULL
Date: Fri, 3 Oct 2025 11:12:57 -0400 [thread overview]
Message-ID: <884071e5-1638-b82d-bb0a-ba7a36d6f13d@amd.com> (raw)
In-Reply-To: <8942a78e-4946-4a86-a8d1-1324ec7c7f4d@amd.com>
On 2025-10-02 17:48, Chen, Xiaogang wrote:
>
> On 10/2/2025 12:43 PM, Philip Yang wrote:
>> Add hmm_range kzalloc return NULL error check. In case the get_pages
>> return failed, free and set hmm_range to NULL, to avoid double free in
>> get_pages_done.
>>
>> Fixes: 29e6f5716115 ("drm/amdgpu: use user provided hmm_range buffer
>> in amdgpu_ttm_tt_get_user_pages")
>> Signed-off-by: Philip Yang <Philip.Yang@amd.com>
>> ---
>> drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 21 +++++++++++++--------
>> 1 file changed, 13 insertions(+), 8 deletions(-)
>>
>> diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
>> b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
>> index 8c3787b00f36..e8a15751c125 100644
>> --- a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
>> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
>> @@ -1736,15 +1736,20 @@ static int svm_range_validate_and_map(struct
>> mm_struct *mm,
>> continue;
>> }
>> - WRITE_ONCE(p->svms.faulting_task, current);
>> hmm_range = kzalloc(sizeof(*hmm_range), GFP_KERNEL);
>> - r = amdgpu_hmm_range_get_pages(&prange->notifier, addr,
>> npages,
>> - readonly, owner,
>> - hmm_range);
>> - WRITE_ONCE(p->svms.faulting_task, NULL);
>> - if (r) {
>> - kfree(hmm_range);
>> - pr_debug("failed %d to get svm range pages\n", r);
>> + if (unlikely(!hmm_range)) {
>> + r = -ENOMEM;
>> + } else {
>> + WRITE_ONCE(p->svms.faulting_task, current);
>> + r = amdgpu_hmm_range_get_pages(&prange->notifier,
>> addr, npages,
>> + readonly, owner,
>> + hmm_range);
>> + WRITE_ONCE(p->svms.faulting_task, NULL);
>> + if (r) {
>> + kfree(hmm_range);
>> + hmm_range = NULL;
>
> How it avoid double free hmm_range? Currently hmm_range got freed at
> amdgpu_hmm_range_get_pages_done. You free it here, then
> amdgpu_hmm_range_get_pages_done would not free it. if do not free
> here amdgpu_hmm_range_get_pages_done would do.
>
> And besides free hmm_range, we also need to free hmm_range->hmm_pfns
> that is done at amdgpu_hmm_range_get_pages_done.
if amdgpu_hmm_range_get_pages returns failed, hmm_range->hmm_pfns is
already freed. If only free hmm_range, but don't set hmm_range to NULL,
then amdgpu_hmm_range_get_pages_done is called and will double free
hmm_range again.
>
> I think the real problem is hmn_range is allocated and free at
> different places. I do not know why.
This is the change in 29e6f5716115, with details in the patch.
Regards,
Philip
>
> Regards
>
> Xiaogang
>
>
>> + pr_debug("failed %d to get svm range pages\n", r);
>> + }
>> }
>> } else {
>> r = -EFAULT;
next prev parent reply other threads:[~2025-10-03 15:13 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-02 17:43 [PATCH 1/3] drm/amdgpu: svm check hmm range kzalloc return NULL Philip Yang
2025-10-02 17:43 ` [PATCH 2/3] drm/amdkfd: svm unmap use page aligned address Philip Yang
2025-10-02 22:04 ` Chen, Xiaogang
2025-10-03 15:03 ` Philip Yang
2025-10-02 17:43 ` [PATCH 3/3] drm/amdkfd: Don't stuck in svm restore worker Philip Yang
2025-10-02 21:48 ` [PATCH 1/3] drm/amdgpu: svm check hmm range kzalloc return NULL Chen, Xiaogang
2025-10-03 15:12 ` Philip Yang [this message]
2025-10-03 15:39 ` Chen, Xiaogang
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=884071e5-1638-b82d-bb0a-ba7a36d6f13d@amd.com \
--to=yangp@amd.com \
--cc=Felix.Kuehling@amd.com \
--cc=Philip.Yang@amd.com \
--cc=amd-gfx@lists.freedesktop.org \
--cc=harish.kasiviswanathan@amd.com \
--cc=xiaogang.chen@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