Skip to content

Commit 10aa5c8

Browse files
Thomas Hellströmlucasdemarchi
authored andcommitted
drm/gpusvm, drm/xe: Fix userptr to not allow device private pages
When userptr is used on SVM-enabled VMs, a non-NULL hmm_range::dev_private_owner value might mean that hmm_range_fault() attempts to return device private pages. Either that will fail, or the userptr code will not know how to handle those. Use NULL for hmm_range::dev_private_owner to migrate such pages to system. In order to do that, move the struct drm_gpusvm::device_private_page_owner field to struct drm_gpusvm_ctx::device_private_page_owner so that it doesn't remain immutable over the drm_gpusvm lifetime. v2: - Don't conditionally compile xe_svm_devm_owner(). - Kerneldoc xe_svm_devm_owner(). Fixes: 9e97874 ("drm/xe/userptr: replace xe_hmm with gpusvm") Cc: Matthew Auld <matthew.auld@intel.com> Cc: Himal Prasad Ghimiray <himal.prasad.ghimiray@intel.com> Cc: Matthew Brost <matthew.brost@intel.com> Signed-off-by: Thomas Hellström <thomas.hellstrom@linux.intel.com> Reviewed-by: Matthew Auld <matthew.auld@intel.com> Reviewed-by: Matthew Brost <matthew.brost@intel.com> Acked-by: Maarten Lankhorst <maarten.lankhorst@linux.intel.com> Link: https://lore.kernel.org/r/20250930122752.96034-1-thomas.hellstrom@linux.intel.com (cherry picked from commit ad298d9ec957414dbf3d51f3c8bca4b6d2416c0c) Signed-off-by: Lucas De Marchi <lucas.demarchi@intel.com>
1 parent 5b440a8 commit 10aa5c8

File tree

6 files changed

+36
-22
lines changed

6 files changed

+36
-22
lines changed

drivers/gpu/drm/drm_gpusvm.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,6 @@ static const struct mmu_interval_notifier_ops drm_gpusvm_notifier_ops = {
361361
* @name: Name of the GPU SVM.
362362
* @drm: Pointer to the DRM device structure.
363363
* @mm: Pointer to the mm_struct for the address space.
364-
* @device_private_page_owner: Device private pages owner.
365364
* @mm_start: Start address of GPU SVM.
366365
* @mm_range: Range of the GPU SVM.
367366
* @notifier_size: Size of individual notifiers.
@@ -383,7 +382,7 @@ static const struct mmu_interval_notifier_ops drm_gpusvm_notifier_ops = {
383382
*/
384383
int drm_gpusvm_init(struct drm_gpusvm *gpusvm,
385384
const char *name, struct drm_device *drm,
386-
struct mm_struct *mm, void *device_private_page_owner,
385+
struct mm_struct *mm,
387386
unsigned long mm_start, unsigned long mm_range,
388387
unsigned long notifier_size,
389388
const struct drm_gpusvm_ops *ops,
@@ -395,15 +394,13 @@ int drm_gpusvm_init(struct drm_gpusvm *gpusvm,
395394
mmgrab(mm);
396395
} else {
397396
/* No full SVM mode, only core drm_gpusvm_pages API. */
398-
if (ops || num_chunks || mm_range || notifier_size ||
399-
device_private_page_owner)
397+
if (ops || num_chunks || mm_range || notifier_size)
400398
return -EINVAL;
401399
}
402400

403401
gpusvm->name = name;
404402
gpusvm->drm = drm;
405403
gpusvm->mm = mm;
406-
gpusvm->device_private_page_owner = device_private_page_owner;
407404
gpusvm->mm_start = mm_start;
408405
gpusvm->mm_range = mm_range;
409406
gpusvm->notifier_size = notifier_size;
@@ -684,6 +681,7 @@ static unsigned int drm_gpusvm_hmm_pfn_to_order(unsigned long hmm_pfn,
684681
* @notifier: Pointer to the GPU SVM notifier structure
685682
* @start: Start address
686683
* @end: End address
684+
* @dev_private_owner: The device private page owner
687685
*
688686
* Check if pages between start and end have been faulted in on the CPU. Use to
689687
* prevent migration of pages without CPU backing store.
@@ -692,14 +690,15 @@ static unsigned int drm_gpusvm_hmm_pfn_to_order(unsigned long hmm_pfn,
692690
*/
693691
static bool drm_gpusvm_check_pages(struct drm_gpusvm *gpusvm,
694692
struct drm_gpusvm_notifier *notifier,
695-
unsigned long start, unsigned long end)
693+
unsigned long start, unsigned long end,
694+
void *dev_private_owner)
696695
{
697696
struct hmm_range hmm_range = {
698697
.default_flags = 0,
699698
.notifier = &notifier->notifier,
700699
.start = start,
701700
.end = end,
702-
.dev_private_owner = gpusvm->device_private_page_owner,
701+
.dev_private_owner = dev_private_owner,
703702
};
704703
unsigned long timeout =
705704
jiffies + msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT);
@@ -753,6 +752,7 @@ static bool drm_gpusvm_check_pages(struct drm_gpusvm *gpusvm,
753752
* @gpuva_start: Start address of GPUVA which mirrors CPU
754753
* @gpuva_end: End address of GPUVA which mirrors CPU
755754
* @check_pages_threshold: Check CPU pages for present threshold
755+
* @dev_private_owner: The device private page owner
756756
*
757757
* This function determines the chunk size for the GPU SVM range based on the
758758
* fault address, GPU SVM chunk sizes, existing GPU SVM ranges, and the virtual
@@ -767,7 +767,8 @@ drm_gpusvm_range_chunk_size(struct drm_gpusvm *gpusvm,
767767
unsigned long fault_addr,
768768
unsigned long gpuva_start,
769769
unsigned long gpuva_end,
770-
unsigned long check_pages_threshold)
770+
unsigned long check_pages_threshold,
771+
void *dev_private_owner)
771772
{
772773
unsigned long start, end;
773774
int i = 0;
@@ -814,7 +815,7 @@ drm_gpusvm_range_chunk_size(struct drm_gpusvm *gpusvm,
814815
* process-many-malloc' mallocs at least 64k at a time.
815816
*/
816817
if (end - start <= check_pages_threshold &&
817-
!drm_gpusvm_check_pages(gpusvm, notifier, start, end)) {
818+
!drm_gpusvm_check_pages(gpusvm, notifier, start, end, dev_private_owner)) {
818819
++i;
819820
goto retry;
820821
}
@@ -957,7 +958,8 @@ drm_gpusvm_range_find_or_insert(struct drm_gpusvm *gpusvm,
957958
chunk_size = drm_gpusvm_range_chunk_size(gpusvm, notifier, vas,
958959
fault_addr, gpuva_start,
959960
gpuva_end,
960-
ctx->check_pages_threshold);
961+
ctx->check_pages_threshold,
962+
ctx->device_private_page_owner);
961963
if (chunk_size == LONG_MAX) {
962964
err = -EINVAL;
963965
goto err_notifier_remove;
@@ -1268,7 +1270,7 @@ int drm_gpusvm_get_pages(struct drm_gpusvm *gpusvm,
12681270
.notifier = notifier,
12691271
.start = pages_start,
12701272
.end = pages_end,
1271-
.dev_private_owner = gpusvm->device_private_page_owner,
1273+
.dev_private_owner = ctx->device_private_page_owner,
12721274
};
12731275
void *zdd;
12741276
unsigned long timeout =

drivers/gpu/drm/xe/xe_svm.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,6 @@ void xe_svm_range_debug(struct xe_svm_range *range, const char *operation)
6767
range_debug(range, operation);
6868
}
6969

70-
static void *xe_svm_devm_owner(struct xe_device *xe)
71-
{
72-
return xe;
73-
}
74-
7570
static struct drm_gpusvm_range *
7671
xe_svm_range_alloc(struct drm_gpusvm *gpusvm)
7772
{
@@ -744,15 +739,14 @@ int xe_svm_init(struct xe_vm *vm)
744739
xe_svm_garbage_collector_work_func);
745740

746741
err = drm_gpusvm_init(&vm->svm.gpusvm, "Xe SVM", &vm->xe->drm,
747-
current->mm, xe_svm_devm_owner(vm->xe), 0,
748-
vm->size,
742+
current->mm, 0, vm->size,
749743
xe_modparam.svm_notifier_size * SZ_1M,
750744
&gpusvm_ops, fault_chunk_sizes,
751745
ARRAY_SIZE(fault_chunk_sizes));
752746
drm_gpusvm_driver_set_lock(&vm->svm.gpusvm, &vm->lock);
753747
} else {
754748
err = drm_gpusvm_init(&vm->svm.gpusvm, "Xe SVM (simple)",
755-
&vm->xe->drm, NULL, NULL, 0, 0, 0, NULL,
749+
&vm->xe->drm, NULL, 0, 0, 0, NULL,
756750
NULL, 0);
757751
}
758752

@@ -1017,6 +1011,7 @@ static int __xe_svm_handle_pagefault(struct xe_vm *vm, struct xe_vma *vma,
10171011
.devmem_only = need_vram && devmem_possible,
10181012
.timeslice_ms = need_vram && devmem_possible ?
10191013
vm->xe->atomic_svm_timeslice_ms : 0,
1014+
.device_private_page_owner = xe_svm_devm_owner(vm->xe),
10201015
};
10211016
struct xe_validation_ctx vctx;
10221017
struct drm_exec exec;

drivers/gpu/drm/xe/xe_svm.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@
66
#ifndef _XE_SVM_H_
77
#define _XE_SVM_H_
88

9+
struct xe_device;
10+
11+
/**
12+
* xe_svm_devm_owner() - Return the owner of device private memory
13+
* @xe: The xe device.
14+
*
15+
* Return: The owner of this device's device private memory to use in
16+
* hmm_range_fault()-
17+
*/
18+
static inline void *xe_svm_devm_owner(struct xe_device *xe)
19+
{
20+
return xe;
21+
}
22+
923
#if IS_ENABLED(CONFIG_DRM_XE_GPUSVM)
1024

1125
#include <drm/drm_pagemap.h>

drivers/gpu/drm/xe/xe_userptr.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ int xe_vma_userptr_pin_pages(struct xe_userptr_vma *uvma)
5454
struct xe_device *xe = vm->xe;
5555
struct drm_gpusvm_ctx ctx = {
5656
.read_only = xe_vma_read_only(vma),
57+
.device_private_page_owner = NULL,
5758
};
5859

5960
lockdep_assert_held(&vm->lock);

drivers/gpu/drm/xe/xe_vm.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2881,6 +2881,7 @@ static int prefetch_ranges(struct xe_vm *vm, struct xe_vma_op *op)
28812881
ctx.read_only = xe_vma_read_only(vma);
28822882
ctx.devmem_possible = devmem_possible;
28832883
ctx.check_pages_threshold = devmem_possible ? SZ_64K : 0;
2884+
ctx.device_private_page_owner = xe_svm_devm_owner(vm->xe);
28842885

28852886
/* TODO: Threading the migration */
28862887
xa_for_each(&op->prefetch_range.range, i, svm_range) {

include/drm/drm_gpusvm.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@ struct drm_gpusvm_range {
179179
* @name: Name of the GPU SVM
180180
* @drm: Pointer to the DRM device structure
181181
* @mm: Pointer to the mm_struct for the address space
182-
* @device_private_page_owner: Device private pages owner
183182
* @mm_start: Start address of GPU SVM
184183
* @mm_range: Range of the GPU SVM
185184
* @notifier_size: Size of individual notifiers
@@ -204,7 +203,6 @@ struct drm_gpusvm {
204203
const char *name;
205204
struct drm_device *drm;
206205
struct mm_struct *mm;
207-
void *device_private_page_owner;
208206
unsigned long mm_start;
209207
unsigned long mm_range;
210208
unsigned long notifier_size;
@@ -226,6 +224,8 @@ struct drm_gpusvm {
226224
/**
227225
* struct drm_gpusvm_ctx - DRM GPU SVM context
228226
*
227+
* @device_private_page_owner: The device-private page owner to use for
228+
* this operation
229229
* @check_pages_threshold: Check CPU pages for present if chunk is less than or
230230
* equal to threshold. If not present, reduce chunk
231231
* size.
@@ -239,6 +239,7 @@ struct drm_gpusvm {
239239
* Context that is DRM GPUSVM is operating in (i.e. user arguments).
240240
*/
241241
struct drm_gpusvm_ctx {
242+
void *device_private_page_owner;
242243
unsigned long check_pages_threshold;
243244
unsigned long timeslice_ms;
244245
unsigned int in_notifier :1;
@@ -249,7 +250,7 @@ struct drm_gpusvm_ctx {
249250

250251
int drm_gpusvm_init(struct drm_gpusvm *gpusvm,
251252
const char *name, struct drm_device *drm,
252-
struct mm_struct *mm, void *device_private_page_owner,
253+
struct mm_struct *mm,
253254
unsigned long mm_start, unsigned long mm_range,
254255
unsigned long notifier_size,
255256
const struct drm_gpusvm_ops *ops,

0 commit comments

Comments
 (0)