AMD-GFX Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [Patch v2 1/2] drm/amdkfd: clean up the code to free hmm_range
@ 2025-10-28 12:13 Sunil Khatri
  2025-10-28 12:13 ` [Patch v2 2/2] drm/amdgpu: caller should make sure not to double free Sunil Khatri
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Sunil Khatri @ 2025-10-28 12:13 UTC (permalink / raw)
  To: Christian König, Felix Kuehling, Alex Deucher, Philip Yang,
	amd-gfx
  Cc: Sunil Khatri

a. hmm_range is either NULL or a valid pointer so we
do not need to set range to NULL ever.

b. keep the hmm_range_free in the end irrespective of
the other conditions to avoid some additional checks
and also avoid double free issue.

Signed-off-by: Sunil Khatri <sunil.khatri@amd.com>
---
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
index ffb7b36e577c..fb3daa7cd326 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
@@ -1744,11 +1744,8 @@ static int svm_range_validate_and_map(struct mm_struct *mm,
 			else
 				r = -ENOMEM;
 			WRITE_ONCE(p->svms.faulting_task, NULL);
-			if (r) {
-				amdgpu_hmm_range_free(range);
-				range = NULL;
+			if (r)
 				pr_debug("failed %d to get svm range pages\n", r);
-			}
 		} else {
 			r = -EFAULT;
 		}
@@ -1767,14 +1764,13 @@ static int svm_range_validate_and_map(struct mm_struct *mm,
 		 * Override return value to TRY AGAIN only if prior returns
 		 * were successful
 		 */
-		if (range && !amdgpu_hmm_range_valid(range) && !r) {
+		if (range && !amdgpu_hmm_range_valid(range) && !r ) {
 			pr_debug("hmm update the range, need validate again\n");
 			r = -EAGAIN;
 		}
-		/* Free the hmm range */
-		if (range)
-			amdgpu_hmm_range_free(range);
 
+		/* Free the hmm range */
+		amdgpu_hmm_range_free(range);
 
 		if (!r && !list_empty(&prange->child_list)) {
 			pr_debug("range split by unmap in parallel, validate again\n");
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [Patch v2 2/2] drm/amdgpu: caller should make sure not to double free
  2025-10-28 12:13 [Patch v2 1/2] drm/amdkfd: clean up the code to free hmm_range Sunil Khatri
@ 2025-10-28 12:13 ` Sunil Khatri
  2025-10-28 12:53 ` [Patch v2 1/2] drm/amdkfd: clean up the code to free hmm_range Christian König
  2025-10-28 15:10 ` Kuehling, Felix
  2 siblings, 0 replies; 4+ messages in thread
From: Sunil Khatri @ 2025-10-28 12:13 UTC (permalink / raw)
  To: Christian König, Felix Kuehling, Alex Deucher, Philip Yang,
	amd-gfx
  Cc: Sunil Khatri

Remove the NULL check from amdgpu_hmm_range_free for hmm_pfns
as caller is responsible not to call amdgpu_hmm_range_free
more than once.

Signed-off-by: Sunil Khatri <sunil.khatri@amd.com>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c
index 518ca3f4db2b..90d26d820bac 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c
@@ -287,9 +287,7 @@ void amdgpu_hmm_range_free(struct amdgpu_hmm_range *range)
 	if (!range)
 		return;
 
-	if (range->hmm_range.hmm_pfns)
-		kvfree(range->hmm_range.hmm_pfns);
-
+	kvfree(range->hmm_range.hmm_pfns);
 	amdgpu_bo_unref(&range->bo);
 	kfree(range);
 }
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [Patch v2 1/2] drm/amdkfd: clean up the code to free hmm_range
  2025-10-28 12:13 [Patch v2 1/2] drm/amdkfd: clean up the code to free hmm_range Sunil Khatri
  2025-10-28 12:13 ` [Patch v2 2/2] drm/amdgpu: caller should make sure not to double free Sunil Khatri
@ 2025-10-28 12:53 ` Christian König
  2025-10-28 15:10 ` Kuehling, Felix
  2 siblings, 0 replies; 4+ messages in thread
From: Christian König @ 2025-10-28 12:53 UTC (permalink / raw)
  To: Sunil Khatri, Felix Kuehling, Alex Deucher, Philip Yang, amd-gfx

On 10/28/25 13:13, Sunil Khatri wrote:
> a. hmm_range is either NULL or a valid pointer so we
> do not need to set range to NULL ever.
> 
> b. keep the hmm_range_free in the end irrespective of
> the other conditions to avoid some additional checks
> and also avoid double free issue.
> 
> Signed-off-by: Sunil Khatri <sunil.khatri@amd.com>
> ---
>  drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 12 ++++--------
>  1 file changed, 4 insertions(+), 8 deletions(-)
> 
> diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
> index ffb7b36e577c..fb3daa7cd326 100644
> --- a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
> @@ -1744,11 +1744,8 @@ static int svm_range_validate_and_map(struct mm_struct *mm,
>  			else
>  				r = -ENOMEM;
>  			WRITE_ONCE(p->svms.faulting_task, NULL);
> -			if (r) {
> -				amdgpu_hmm_range_free(range);
> -				range = NULL;
> +			if (r)
>  				pr_debug("failed %d to get svm range pages\n", r);
> -			}
>  		} else {
>  			r = -EFAULT;
>  		}
> @@ -1767,14 +1764,13 @@ static int svm_range_validate_and_map(struct mm_struct *mm,
>  		 * Override return value to TRY AGAIN only if prior returns
>  		 * were successful
>  		 */
> -		if (range && !amdgpu_hmm_range_valid(range) && !r) {
> +		if (range && !amdgpu_hmm_range_valid(range) && !r ) {

Extra unnecessary space here before the ).

With that fixed Acked-by: Christian König <christian.koenig@amd.com> for this patch here and rb for #2 in the series.

Regards,
Christian.

>  			pr_debug("hmm update the range, need validate again\n");
>  			r = -EAGAIN;
>  		}
> -		/* Free the hmm range */
> -		if (range)
> -			amdgpu_hmm_range_free(range);
>  
> +		/* Free the hmm range */
> +		amdgpu_hmm_range_free(range);
>  
>  		if (!r && !list_empty(&prange->child_list)) {
>  			pr_debug("range split by unmap in parallel, validate again\n");


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Patch v2 1/2] drm/amdkfd: clean up the code to free hmm_range
  2025-10-28 12:13 [Patch v2 1/2] drm/amdkfd: clean up the code to free hmm_range Sunil Khatri
  2025-10-28 12:13 ` [Patch v2 2/2] drm/amdgpu: caller should make sure not to double free Sunil Khatri
  2025-10-28 12:53 ` [Patch v2 1/2] drm/amdkfd: clean up the code to free hmm_range Christian König
@ 2025-10-28 15:10 ` Kuehling, Felix
  2 siblings, 0 replies; 4+ messages in thread
From: Kuehling, Felix @ 2025-10-28 15:10 UTC (permalink / raw)
  To: Sunil Khatri, Christian König, Alex Deucher, Philip Yang,
	amd-gfx

On 2025-10-28 08:13, Sunil Khatri wrote:
> a. hmm_range is either NULL or a valid pointer so we
> do not need to set range to NULL ever.
>
> b. keep the hmm_range_free in the end irrespective of
> the other conditions to avoid some additional checks
> and also avoid double free issue.
>
> Signed-off-by: Sunil Khatri <sunil.khatri@amd.com>
> ---
>   drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 12 ++++--------
>   1 file changed, 4 insertions(+), 8 deletions(-)
>
> diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
> index ffb7b36e577c..fb3daa7cd326 100644
> --- a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
> @@ -1744,11 +1744,8 @@ static int svm_range_validate_and_map(struct mm_struct *mm,
>   			else
>   				r = -ENOMEM;
>   			WRITE_ONCE(p->svms.faulting_task, NULL);
> -			if (r) {
> -				amdgpu_hmm_range_free(range);
> -				range = NULL;
> +			if (r)
>   				pr_debug("failed %d to get svm range pages\n", r);
> -			}
>   		} else {
>   			r = -EFAULT;
>   		}
> @@ -1767,14 +1764,13 @@ static int svm_range_validate_and_map(struct mm_struct *mm,
>   		 * Override return value to TRY AGAIN only if prior returns
>   		 * were successful
>   		 */
> -		if (range && !amdgpu_hmm_range_valid(range) && !r) {
> +		if (range && !amdgpu_hmm_range_valid(range) && !r ) {

Unnecessary whitespace change. With that fixed, the patch is

Reviewed-by: Felix Kuehling <felix.kuehling@amd.com>


>   			pr_debug("hmm update the range, need validate again\n");
>   			r = -EAGAIN;
>   		}
> -		/* Free the hmm range */
> -		if (range)
> -			amdgpu_hmm_range_free(range);
>   
> +		/* Free the hmm range */
> +		amdgpu_hmm_range_free(range);
>   
>   		if (!r && !list_empty(&prange->child_list)) {
>   			pr_debug("range split by unmap in parallel, validate again\n");

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-10-28 15:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-28 12:13 [Patch v2 1/2] drm/amdkfd: clean up the code to free hmm_range Sunil Khatri
2025-10-28 12:13 ` [Patch v2 2/2] drm/amdgpu: caller should make sure not to double free Sunil Khatri
2025-10-28 12:53 ` [Patch v2 1/2] drm/amdkfd: clean up the code to free hmm_range Christian König
2025-10-28 15:10 ` Kuehling, Felix

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox