mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] drm/gpusvm: Retry invalid inline DMA mappings after HMM faults
@ 2026-12-02  8:08 Arvind Yadav
  2026-12-03  3:43 ` Matthew Brost
  0 siblings, 1 reply; 3+ messages in thread
From: Arvind Yadav @ 2026-12-02  8:08 UTC (permalink / raw)
  To: dri-devel, intel-xe, linux-kernel
  Cc: matthew.brost, thomas.hellstrom, maarten.lankhorst, mripard,
	tzimmermann, airlied, simona, himal.prasad.ghimiray

When a devmem callback is present, drm_gpusvm_get_pages() continues
through HMM even if the existing DMA mapping is valid. Invalidation
during this window can unmap the DMA mapping while leaving its inline
storage intact.

HMM refreshes notifier_seq internally, so the later sequence check
can pass. The inline entry causes DMA array allocation to be skipped,
but the mapper then treats the stored DMA address as an array pointer.
This results in:

  BUG: KASAN: user-memory-access in drm_gpusvm_get_pages+0xe12/0x1380 [drm_gpusvm_helper]
  Write of size 16 at addr 000000027abfd000 by task kworker/u64:71/693678
  Workqueue: xe_prefetch_work_queue prefetch_work_func [xe]

Before mapping, check for invalid inline storage under the notifier
lock. Clear it and retry so a DMA array is allocated before the mapper
writes to it. The retry uses the existing timeout.

Fixes: 8d242e452ed7 ("drm/gpusvm: keep a single DMA mapping inline for THP")
Cc: Matthew Brost <matthew.brost@intel.com>
Cc: Thomas Hellström <thomas.hellstrom@linux.intel.com>
Cc: Himal Prasad Ghimiray <himal.prasad.ghimiray@intel.com>
Signed-off-by: Arvind Yadav <arvind.yadav@intel.com>
---
 drivers/gpu/drm/drm_gpusvm.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/drivers/gpu/drm/drm_gpusvm.c b/drivers/gpu/drm/drm_gpusvm.c
index de2fa2684e84..fd071eb85e24 100644
--- a/drivers/gpu/drm/drm_gpusvm.c
+++ b/drivers/gpu/drm/drm_gpusvm.c
@@ -1888,6 +1888,17 @@ int drm_gpusvm_get_pages(struct drm_gpusvm *gpusvm,
 			if (drm_gpusvm_pages_valid(gpusvm, &svm_pages[p]))
 				continue;
 
+			/*
+			 * HMM may refresh notifier_seq after invalidation, leaving stale
+			 * inline storage. Clear it and retry to allocate a DMA array.
+			 */
+			if (svm_pages[p].flags.inline_dma_mapping) {
+				__drm_gpusvm_free_pages(gpusvm, &svm_pages[p]);
+				drm_gpusvm_notifier_unlock(gpusvm);
+				kvfree(pfns);
+				goto retry;
+			}
+
 			err = drm_gpusvm_dma_map_pages(gpusvm, &svm_pages[p], pfns,
 						       npages, ctx, dma_dir);
 			if (err) {
-- 
2.43.0


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

* Re: [PATCH] drm/gpusvm: Retry invalid inline DMA mappings after HMM faults
  2026-12-02  8:08 [PATCH] drm/gpusvm: Retry invalid inline DMA mappings after HMM faults Arvind Yadav
@ 2026-12-03  3:43 ` Matthew Brost
  2026-12-03  6:27   ` Yadav, Arvind
  0 siblings, 1 reply; 3+ messages in thread
From: Matthew Brost @ 2026-12-03  3:43 UTC (permalink / raw)
  To: Arvind Yadav
  Cc: dri-devel, intel-xe, linux-kernel, thomas.hellstrom,
	maarten.lankhorst, mripard, tzimmermann, airlied, simona,
	himal.prasad.ghimiray

On Wed, Dec 02, 2026 at 01:38:17PM +0530, Arvind Yadav wrote:
> When a devmem callback is present, drm_gpusvm_get_pages() continues
> through HMM even if the existing DMA mapping is valid. Invalidation
> during this window can unmap the DMA mapping while leaving its inline
> storage intact.
> 
> HMM refreshes notifier_seq internally, so the later sequence check
> can pass. The inline entry causes DMA array allocation to be skipped,
> but the mapper then treats the stored DMA address as an array pointer.
> This results in:
> 
>   BUG: KASAN: user-memory-access in drm_gpusvm_get_pages+0xe12/0x1380 [drm_gpusvm_helper]
>   Write of size 16 at addr 000000027abfd000 by task kworker/u64:71/693678
>   Workqueue: xe_prefetch_work_queue prefetch_work_func [xe]
> 
> Before mapping, check for invalid inline storage under the notifier
> lock. Clear it and retry so a DMA array is allocated before the mapper
> writes to it. The retry uses the existing timeout.
> 
> Fixes: 8d242e452ed7 ("drm/gpusvm: keep a single DMA mapping inline for THP")
> Cc: Matthew Brost <matthew.brost@intel.com>
> Cc: Thomas Hellström <thomas.hellstrom@linux.intel.com>
> Cc: Himal Prasad Ghimiray <himal.prasad.ghimiray@intel.com>
> Signed-off-by: Arvind Yadav <arvind.yadav@intel.com>
> ---
>  drivers/gpu/drm/drm_gpusvm.c | 11 +++++++++++
>  1 file changed, 11 insertions(+)
> 
> diff --git a/drivers/gpu/drm/drm_gpusvm.c b/drivers/gpu/drm/drm_gpusvm.c
> index de2fa2684e84..fd071eb85e24 100644
> --- a/drivers/gpu/drm/drm_gpusvm.c
> +++ b/drivers/gpu/drm/drm_gpusvm.c
> @@ -1888,6 +1888,17 @@ int drm_gpusvm_get_pages(struct drm_gpusvm *gpusvm,
>  			if (drm_gpusvm_pages_valid(gpusvm, &svm_pages[p]))
>  				continue;
>  
> +			/*
> +			 * HMM may refresh notifier_seq after invalidation, leaving stale
> +			 * inline storage. Clear it and retry to allocate a DMA array.
> +			 */
> +			if (svm_pages[p].flags.inline_dma_mapping) {
> +				__drm_gpusvm_free_pages(gpusvm, &svm_pages[p]);
> +				drm_gpusvm_notifier_unlock(gpusvm);
> +				kvfree(pfns);
> +				goto retry;
> +			}
> +

I don't particular like the retry here - we have a valid set of pages
from HMM but are in a bad page state. Can't we just clear the inline
mapping upon unmap - that actually seems like right fix...

e.g.,

diff --git a/drivers/gpu/drm/drm_gpusvm.c b/drivers/gpu/drm/drm_gpusvm.c
index 92f46aad71f3..2ca936930fc1 100644
--- a/drivers/gpu/drm/drm_gpusvm.c
+++ b/drivers/gpu/drm/drm_gpusvm.c
@@ -1305,6 +1305,11 @@ static void __drm_gpusvm_unmap_pages(struct drm_gpusvm *gpusvm,
                        i += 1 << addr->order;
                }

+               if (flags.inline_dma_mapping) {
+                       svm_pages->inline_addr = (struct drm_pagemap_addr){};
+                       flags.inline_dma_mapping = false;
+               }
+
                /* WRITE_ONCE pairs with READ_ONCE for opportunistic checks */
                flags.has_devmem_pages = false;
                flags.has_dma_mapping = false;

Matt

>  			err = drm_gpusvm_dma_map_pages(gpusvm, &svm_pages[p], pfns,
>  						       npages, ctx, dma_dir);
>  			if (err) {
> -- 
> 2.43.0
> 

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

* Re: [PATCH] drm/gpusvm: Retry invalid inline DMA mappings after HMM faults
  2026-12-03  3:43 ` Matthew Brost
@ 2026-12-03  6:27   ` Yadav, Arvind
  0 siblings, 0 replies; 3+ messages in thread
From: Yadav, Arvind @ 2026-12-03  6:27 UTC (permalink / raw)
  To: Matthew Brost
  Cc: dri-devel, intel-xe, linux-kernel, thomas.hellstrom,
	maarten.lankhorst, mripard, tzimmermann, airlied, simona,
	himal.prasad.ghimiray


On 03-12-2026 09:13, Matthew Brost wrote:
> On Wed, Dec 02, 2026 at 01:38:17PM +0530, Arvind Yadav wrote:
>> When a devmem callback is present, drm_gpusvm_get_pages() continues
>> through HMM even if the existing DMA mapping is valid. Invalidation
>> during this window can unmap the DMA mapping while leaving its inline
>> storage intact.
>>
>> HMM refreshes notifier_seq internally, so the later sequence check
>> can pass. The inline entry causes DMA array allocation to be skipped,
>> but the mapper then treats the stored DMA address as an array pointer.
>> This results in:
>>
>>    BUG: KASAN: user-memory-access in drm_gpusvm_get_pages+0xe12/0x1380 [drm_gpusvm_helper]
>>    Write of size 16 at addr 000000027abfd000 by task kworker/u64:71/693678
>>    Workqueue: xe_prefetch_work_queue prefetch_work_func [xe]
>>
>> Before mapping, check for invalid inline storage under the notifier
>> lock. Clear it and retry so a DMA array is allocated before the mapper
>> writes to it. The retry uses the existing timeout.
>>
>> Fixes: 8d242e452ed7 ("drm/gpusvm: keep a single DMA mapping inline for THP")
>> Cc: Matthew Brost <matthew.brost@intel.com>
>> Cc: Thomas Hellström <thomas.hellstrom@linux.intel.com>
>> Cc: Himal Prasad Ghimiray <himal.prasad.ghimiray@intel.com>
>> Signed-off-by: Arvind Yadav <arvind.yadav@intel.com>
>> ---
>>   drivers/gpu/drm/drm_gpusvm.c | 11 +++++++++++
>>   1 file changed, 11 insertions(+)
>>
>> diff --git a/drivers/gpu/drm/drm_gpusvm.c b/drivers/gpu/drm/drm_gpusvm.c
>> index de2fa2684e84..fd071eb85e24 100644
>> --- a/drivers/gpu/drm/drm_gpusvm.c
>> +++ b/drivers/gpu/drm/drm_gpusvm.c
>> @@ -1888,6 +1888,17 @@ int drm_gpusvm_get_pages(struct drm_gpusvm *gpusvm,
>>   			if (drm_gpusvm_pages_valid(gpusvm, &svm_pages[p]))
>>   				continue;
>>   
>> +			/*
>> +			 * HMM may refresh notifier_seq after invalidation, leaving stale
>> +			 * inline storage. Clear it and retry to allocate a DMA array.
>> +			 */
>> +			if (svm_pages[p].flags.inline_dma_mapping) {
>> +				__drm_gpusvm_free_pages(gpusvm, &svm_pages[p]);
>> +				drm_gpusvm_notifier_unlock(gpusvm);
>> +				kvfree(pfns);
>> +				goto retry;
>> +			}
>> +
> I don't particular like the retry here - we have a valid set of pages
> from HMM but are in a bad page state. Can't we just clear the inline
> mapping upon unmap - that actually seems like right fix...


Yes, you are right. I will the changes as per you suggestion.

Thanks,
Arvind

> e.g.,
>
> diff --git a/drivers/gpu/drm/drm_gpusvm.c b/drivers/gpu/drm/drm_gpusvm.c
> index 92f46aad71f3..2ca936930fc1 100644
> --- a/drivers/gpu/drm/drm_gpusvm.c
> +++ b/drivers/gpu/drm/drm_gpusvm.c
> @@ -1305,6 +1305,11 @@ static void __drm_gpusvm_unmap_pages(struct drm_gpusvm *gpusvm,
>                          i += 1 << addr->order;
>                  }
>
> +               if (flags.inline_dma_mapping) {
> +                       svm_pages->inline_addr = (struct drm_pagemap_addr){};
> +                       flags.inline_dma_mapping = false;
> +               }
> +
>                  /* WRITE_ONCE pairs with READ_ONCE for opportunistic checks */
>                  flags.has_devmem_pages = false;
>                  flags.has_dma_mapping = false;
>
> Matt
>
>>   			err = drm_gpusvm_dma_map_pages(gpusvm, &svm_pages[p], pfns,
>>   						       npages, ctx, dma_dir);
>>   			if (err) {
>> -- 
>> 2.43.0
>>

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

end of thread, other threads:[~2026-12-03  6:27 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-02  8:08 [PATCH] drm/gpusvm: Retry invalid inline DMA mappings after HMM faults Arvind Yadav
2026-12-03  3:43 ` Matthew Brost
2026-12-03  6:27   ` Yadav, Arvind

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®