Intel-XE Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Simona Vetter <simona.vetter@ffwll.ch>
To: "Christian König" <christian.koenig@amd.com>
Cc: Mika Kuoppala <mika.kuoppala@linux.intel.com>,
	intel-xe@lists.freedesktop.org,
	lkml <linux-kernel@vger.kernel.org>,
	Linux MM <linux-mm@kvack.org>,
	dri-devel@lists.freedesktop.org,
	Andrzej Hajda <andrzej.hajda@intel.com>,
	Maciej Patelczyk <maciej.patelczyk@intel.com>,
	Jonathan Cavitt <jonathan.cavitt@intel.com>
Subject: Re: [PATCH 14/26] drm/xe/eudebug: implement userptr_vma access
Date: Mon, 9 Dec 2024 16:31:13 +0100	[thread overview]
Message-ID: <Z1cNQTvGdAUPp4Y-@phenom.ffwll.local> (raw)
In-Reply-To: <ec42fe8b-9be0-41cc-96f4-f1869c6bb7e6@amd.com>

On Mon, Dec 09, 2024 at 03:03:04PM +0100, Christian König wrote:
> Am 09.12.24 um 14:33 schrieb Mika Kuoppala:
> > From: Andrzej Hajda <andrzej.hajda@intel.com>
> > 
> > Debugger needs to read/write program's vmas including userptr_vma.
> > Since hmm_range_fault is used to pin userptr vmas, it is possible
> > to map those vmas from debugger context.
> 
> Oh, this implementation is extremely questionable as well. Adding the LKML
> and the MM list as well.
> 
> First of all hmm_range_fault() does *not* pin anything!
> 
> In other words you don't have a page reference when the function returns,
> but rather just a sequence number you can check for modifications.

I think it's all there, holds the invalidation lock during the critical
access/section, drops it when reacquiring pages, retries until it works.

I think the issue is more that everyone hand-rolls userptr. Probably time
we standardize that and put it into gpuvm as an optional part, with
consistent locking, naming (like not calling it _pin_pages when it's
unpinnged userptr), kerneldoc and all the nice things so that we
stop consistently getting confused by other driver's userptr code.

I think that was on the plan originally as an eventual step, I guess time
to pump that up. Matt/Thomas, thoughts?
-Sima

> 
> > v2: pin pages vs notifier, move to vm.c (Matthew)
> > v3: - iterate over system pages instead of DMA, fixes iommu enabled
> >      - s/xe_uvma_access/xe_vm_uvma_access/ (Matt)
> > 
> > Signed-off-by: Andrzej Hajda <andrzej.hajda@intel.com>
> > Signed-off-by: Maciej Patelczyk <maciej.patelczyk@intel.com>
> > Signed-off-by: Mika Kuoppala <mika.kuoppala@linux.intel.com>
> > Reviewed-by: Jonathan Cavitt <jonathan.cavitt@intel.com> #v1
> > ---
> >   drivers/gpu/drm/xe/xe_eudebug.c |  3 ++-
> >   drivers/gpu/drm/xe/xe_vm.c      | 47 +++++++++++++++++++++++++++++++++
> >   drivers/gpu/drm/xe/xe_vm.h      |  3 +++
> >   3 files changed, 52 insertions(+), 1 deletion(-)
> > 
> > diff --git a/drivers/gpu/drm/xe/xe_eudebug.c b/drivers/gpu/drm/xe/xe_eudebug.c
> > index 9d87df75348b..e5949e4dcad8 100644
> > --- a/drivers/gpu/drm/xe/xe_eudebug.c
> > +++ b/drivers/gpu/drm/xe/xe_eudebug.c
> > @@ -3076,7 +3076,8 @@ static int xe_eudebug_vma_access(struct xe_vma *vma, u64 offset_in_vma,
> >   		return ret;
> >   	}
> > -	return -EINVAL;
> > +	return xe_vm_userptr_access(to_userptr_vma(vma), offset_in_vma,
> > +				    buf, bytes, write);
> >   }
> >   static int xe_eudebug_vm_access(struct xe_vm *vm, u64 offset,
> > diff --git a/drivers/gpu/drm/xe/xe_vm.c b/drivers/gpu/drm/xe/xe_vm.c
> > index 0f17bc8b627b..224ff9e16941 100644
> > --- a/drivers/gpu/drm/xe/xe_vm.c
> > +++ b/drivers/gpu/drm/xe/xe_vm.c
> > @@ -3414,3 +3414,50 @@ void xe_vm_snapshot_free(struct xe_vm_snapshot *snap)
> >   	}
> >   	kvfree(snap);
> >   }
> > +
> > +int xe_vm_userptr_access(struct xe_userptr_vma *uvma, u64 offset,
> > +			 void *buf, u64 len, bool write)
> > +{
> > +	struct xe_vm *vm = xe_vma_vm(&uvma->vma);
> > +	struct xe_userptr *up = &uvma->userptr;
> > +	struct xe_res_cursor cur = {};
> > +	int cur_len, ret = 0;
> > +
> > +	while (true) {
> > +		down_read(&vm->userptr.notifier_lock);
> > +		if (!xe_vma_userptr_check_repin(uvma))
> > +			break;
> > +
> > +		spin_lock(&vm->userptr.invalidated_lock);
> > +		list_del_init(&uvma->userptr.invalidate_link);
> > +		spin_unlock(&vm->userptr.invalidated_lock);
> > +
> > +		up_read(&vm->userptr.notifier_lock);
> > +		ret = xe_vma_userptr_pin_pages(uvma);
> > +		if (ret)
> > +			return ret;
> > +	}
> > +
> > +	if (!up->sg) {
> > +		ret = -EINVAL;
> > +		goto out_unlock_notifier;
> > +	}
> > +
> > +	for (xe_res_first_sg_system(up->sg, offset, len, &cur); cur.remaining;
> > +	     xe_res_next(&cur, cur_len)) {
> > +		void *ptr = kmap_local_page(sg_page(cur.sgl)) + cur.start;
> 
> The interface basically creates a side channel to access userptrs in the way
> an userspace application would do without actually going through userspace.
> 
> That is generally not something a device driver should ever do as far as I
> can see.
> 
> > +
> > +		cur_len = min(cur.size, cur.remaining);
> > +		if (write)
> > +			memcpy(ptr, buf, cur_len);
> > +		else
> > +			memcpy(buf, ptr, cur_len);
> > +		kunmap_local(ptr);
> > +		buf += cur_len;
> > +	}
> > +	ret = len;
> > +
> > +out_unlock_notifier:
> > +	up_read(&vm->userptr.notifier_lock);
> 
> I just strongly hope that this will prevent the mapping from changing.
> 
> Regards,
> Christian.
> 
> > +	return ret;
> > +}
> > diff --git a/drivers/gpu/drm/xe/xe_vm.h b/drivers/gpu/drm/xe/xe_vm.h
> > index 23adb7442881..372ad40ad67f 100644
> > --- a/drivers/gpu/drm/xe/xe_vm.h
> > +++ b/drivers/gpu/drm/xe/xe_vm.h
> > @@ -280,3 +280,6 @@ struct xe_vm_snapshot *xe_vm_snapshot_capture(struct xe_vm *vm);
> >   void xe_vm_snapshot_capture_delayed(struct xe_vm_snapshot *snap);
> >   void xe_vm_snapshot_print(struct xe_vm_snapshot *snap, struct drm_printer *p);
> >   void xe_vm_snapshot_free(struct xe_vm_snapshot *snap);
> > +
> > +int xe_vm_userptr_access(struct xe_userptr_vma *uvma, u64 offset,
> > +			 void *buf, u64 len, bool write);
> 

-- 
Simona Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

  parent reply	other threads:[~2024-12-09 15:31 UTC|newest]

Thread overview: 62+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-09 13:32 [PATCH 00/26] Intel Xe GPU debug support (eudebug) v3 Mika Kuoppala
2024-12-09 13:32 ` [PATCH 01/26] ptrace: export ptrace_may_access Mika Kuoppala
2024-12-10  4:29   ` Christoph Hellwig
2024-12-12  9:16     ` Joonas Lahtinen
2024-12-09 13:32 ` [PATCH 02/26] drm/xe/eudebug: Introduce eudebug support Mika Kuoppala
2024-12-09 13:32 ` [PATCH 03/26] drm/xe/eudebug: Introduce discovery for resources Mika Kuoppala
2024-12-09 13:32 ` [PATCH 04/26] drm/xe/eudebug: Introduce exec_queue events Mika Kuoppala
2024-12-09 13:32 ` [PATCH 05/26] drm/xe/eudebug: Introduce exec queue placements event Mika Kuoppala
2024-12-09 13:32 ` [PATCH 06/26] drm/xe/eudebug: hw enablement for eudebug Mika Kuoppala
2024-12-09 13:32 ` [PATCH 07/26] drm/xe: Add EUDEBUG_ENABLE exec queue property Mika Kuoppala
2024-12-09 13:32 ` [PATCH 08/26] drm/xe/eudebug: Introduce per device attention scan worker Mika Kuoppala
2024-12-09 13:33 ` [PATCH 09/26] drm/xe/eudebug: Introduce EU control interface Mika Kuoppala
2024-12-09 13:33 ` [PATCH 10/26] drm/xe/eudebug: Add vm bind and vm bind ops Mika Kuoppala
2024-12-09 13:33 ` [PATCH 11/26] drm/xe/eudebug: Add UFENCE events with acks Mika Kuoppala
2024-12-09 13:33 ` [PATCH 12/26] drm/xe/eudebug: vm open/pread/pwrite Mika Kuoppala
2024-12-09 13:33 ` [PATCH 13/26] drm/xe: add system memory page iterator support to xe_res_cursor Mika Kuoppala
2024-12-09 13:33 ` [PATCH 14/26] drm/xe/eudebug: implement userptr_vma access Mika Kuoppala
2024-12-09 14:03   ` Christian König
2024-12-09 14:56     ` Joonas Lahtinen
2024-12-09 15:31     ` Simona Vetter [this message]
2024-12-09 15:42       ` Christian König
2024-12-09 15:45         ` Christian König
2024-12-10  9:33         ` Joonas Lahtinen
2024-12-10 10:00           ` Christian König
2024-12-10 11:57             ` Joonas Lahtinen
2024-12-10 14:03               ` Christian König
2024-12-11 12:59                 ` Joonas Lahtinen
2024-12-17 14:12                   ` Joonas Lahtinen
2024-12-20 12:47                     ` Mika Kuoppala
2024-12-10 11:17         ` Simona Vetter
2024-12-12  8:49       ` Thomas Hellström
2024-12-12 10:12         ` Simona Vetter
2024-12-13 19:39           ` Matthew Brost
2024-12-16 14:17   ` [PATCH 13/26] RFC drm/xe/eudebug: userptr vm pread/pwrite Mika Kuoppala
2024-12-20 11:31   ` Mika Kuoppala
2024-12-20 12:56     ` Christian König
2025-01-29  8:03       ` Joonas Lahtinen
2025-01-29 10:33         ` Christian König
2025-01-29 18:18           ` Joonas Lahtinen
2025-01-30 12:09             ` Christian König
2024-12-23 10:31     ` Thomas Hellström
2025-01-13 13:22       ` Mika Kuoppala
2025-01-13 13:32       ` [PATCH 13/27] mm: export access_remote_vm symbol for debugger use Mika Kuoppala
2025-01-13 13:32       ` [PATCH 14/27] drm/xe/eudebug: userptr vm access pread/pwrite Mika Kuoppala
2024-12-09 13:33 ` [PATCH 15/26] drm/xe: Debug metadata create/destroy ioctls Mika Kuoppala
2024-12-09 13:33 ` [PATCH 16/26] drm/xe: Attach debug metadata to vma Mika Kuoppala
2024-12-09 13:33 ` [PATCH 17/26] drm/xe/eudebug: Add debug metadata support for xe_eudebug Mika Kuoppala
2024-12-09 13:33 ` [PATCH 18/26] drm/xe/eudebug: Implement vm_bind_op discovery Mika Kuoppala
2024-12-09 13:33 ` [PATCH 19/26] drm/xe/eudebug: Dynamically toggle debugger functionality Mika Kuoppala
2024-12-09 13:33 ` [PATCH 20/26] drm/xe/eudebug_test: Introduce xe_eudebug wa kunit test Mika Kuoppala
2024-12-09 13:33 ` [PATCH 21/26] drm/xe/eudebug/ptl: Add support for extra attention register Mika Kuoppala
2024-12-09 13:33 ` [PATCH 22/26] drm/xe/eudebug/ptl: Add RCU_DEBUG_1 register support for xe3 Mika Kuoppala
2024-12-09 13:33 ` [PATCH 23/26] drm/xe/eudebug: Add read/count/compare helper for eu attention Mika Kuoppala
2024-12-09 13:33 ` [PATCH 24/26] drm/xe/eudebug: Introduce EU pagefault handling interface Mika Kuoppala
2024-12-09 13:33 ` [PATCH 25/26] drm/xe/vm: Support for adding null page VMA to VM on request Mika Kuoppala
2024-12-09 13:33 ` [PATCH 26/26] drm/xe/eudebug: Enable EU pagefault handling Mika Kuoppala
2024-12-09 14:37 ` ✓ CI.Patch_applied: success for Intel Xe GPU debug support (eudebug) v3 Patchwork
2024-12-09 14:38 ` ✗ CI.checkpatch: warning " Patchwork
2024-12-09 14:39 ` ✗ CI.KUnit: failure " Patchwork
2024-12-16 14:22 ` ✗ CI.Patch_applied: failure for Intel Xe GPU debug support (eudebug) v3 (rev2) Patchwork
2024-12-20 14:36 ` ✗ CI.Patch_applied: failure for Intel Xe GPU debug support (eudebug) v3 (rev3) Patchwork
2025-01-13 16:15 ` ✗ CI.Patch_applied: failure for Intel Xe GPU debug support (eudebug) v3 (rev4) Patchwork

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=Z1cNQTvGdAUPp4Y-@phenom.ffwll.local \
    --to=simona.vetter@ffwll.ch \
    --cc=andrzej.hajda@intel.com \
    --cc=christian.koenig@amd.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=jonathan.cavitt@intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=maciej.patelczyk@intel.com \
    --cc=mika.kuoppala@linux.intel.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