All of lore.kernel.org
 help / color / mirror / Atom feed
From: Matthew Brost <matthew.brost@intel.com>
To: Niranjana Vishwanathapura <niranjana.vishwanathapura@intel.com>
Cc: intel-xe@lists.freedesktop.org
Subject: Re: [Intel-xe] [PATCH 2/2] drm/xe: Move in fault mode / non-fault mode check to xe_vm_create
Date: Fri, 24 Mar 2023 16:24:46 +0000	[thread overview]
Message-ID: <ZB3OzmD1Y9zfKCwl@DUT025-TGLU.fm.intel.com> (raw)
In-Reply-To: <ZB0+yQ4RbWJkwwOZ@nvishwa1-DESK>

On Thu, Mar 23, 2023 at 11:10:17PM -0700, Niranjana Vishwanathapura wrote:
> On Tue, Mar 21, 2023 at 04:42:17PM -0700, Matthew Brost wrote:
> > The check for fault mode / non-fault mode was in the VM create IOCTL
> > before VM creation and not under a lock. The increment was after VM
> > creation under the lock. This is racey. Move both the check and
> > increment to xe_vm_create before actual creation and have the lock for
> > both of these steps.
> > 
> > Suggested-by: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>
> > Signed-off-by: Matthew Brost <matthew.brost@intel.com>
> > ---
> > drivers/gpu/drm/xe/xe_vm.c | 45 ++++++++++++++++++++++++--------------
> > 1 file changed, 28 insertions(+), 17 deletions(-)
> > 
> > diff --git a/drivers/gpu/drm/xe/xe_vm.c b/drivers/gpu/drm/xe/xe_vm.c
> > index e7674612a57e..965cad81b02a 100644
> > --- a/drivers/gpu/drm/xe/xe_vm.c
> > +++ b/drivers/gpu/drm/xe/xe_vm.c
> > @@ -1060,9 +1060,27 @@ struct xe_vm *xe_vm_create(struct xe_device *xe, u32 flags)
> > 	struct xe_gt *gt;
> > 	u8 id;
> > 
> > +	err = mutex_lock_interruptible(&xe->usm.lock);
> > +	if (err)
> > +		return ERR_PTR(err);
> > +	if (XE_IOCTL_ERR(xe, flags & XE_VM_FLAG_FAULT_MODE &&
> > +			 xe_device_in_non_fault_mode(xe)) ||
> > +	    XE_IOCTL_ERR(xe, !(flags & XE_VM_FLAG_MIGRATION) &&
> > +			 xe_device_in_fault_mode(xe))) {
> 
> NIT...is below simplification any better?
> 
> bool fault_mode = !!(flags & XE_VM_FLAG_FAULT_MODE);
> if (XE_IOCTL_ERR(xe, fault_mode != xe_device_in_fault_mode(xe))
> 

I thought about this and this isn't the same logic, so no this doesn't work.

Matt

> > +		mutex_unlock(&xe->usm.lock);
> > +		return ERR_PTR(-EINVAL);
> > +	}
> > +	if (flags & XE_VM_FLAG_FAULT_MODE)
> > +		xe->usm.num_vm_in_fault_mode++;
> > +	else if (!(flags & XE_VM_FLAG_MIGRATION))
> > +		xe->usm.num_vm_in_non_fault_mode++;
> > +	mutex_unlock(&xe->usm.lock);
> > +
> > 	vm = kzalloc(sizeof(*vm), GFP_KERNEL);
> > -	if (!vm)
> > -		return ERR_PTR(-ENOMEM);
> > +	if (!vm) {
> > +		err = -ENOMEM;
> > +		goto err_usm;
> > +	}
> > 
> > 	vm->xe = xe;
> > 	kref_init(&vm->refcount);
> > @@ -1182,13 +1200,6 @@ struct xe_vm *xe_vm_create(struct xe_device *xe, u32 flags)
> > 	if (number_gts > 1)
> > 		vm->composite_fence_ctx = dma_fence_context_alloc(1);
> > 
> > -	mutex_lock(&xe->usm.lock);
> > -	if (flags & XE_VM_FLAG_FAULT_MODE)
> > -		xe->usm.num_vm_in_fault_mode++;
> > -	else if (!(flags & XE_VM_FLAG_MIGRATION))
> > -		xe->usm.num_vm_in_non_fault_mode++;
> > -	mutex_unlock(&xe->usm.lock);
> > -
> > 	trace_xe_vm_create(vm);
> > 
> > 	return vm;
> > @@ -1220,6 +1231,14 @@ struct xe_vm *xe_vm_create(struct xe_device *xe, u32 flags)
> > 		xe_device_mem_access_put(xe);
> > 		xe_pm_runtime_put(xe);
> > 	}
> > +err_usm:
> > +	mutex_lock(&xe->usm.lock);
> > +	if (flags & XE_VM_FLAG_FAULT_MODE)
> > +		xe->usm.num_vm_in_fault_mode--;
> > +	else if (!(flags & XE_VM_FLAG_MIGRATION))
> > +		xe->usm.num_vm_in_non_fault_mode--;
> > +	mutex_unlock(&xe->usm.lock);
> > +
> 
> Perhaps put these counts increment/decrement blocks into functions
> instead of duplicating them?
> 
> Niranjana
> 
> > 	return ERR_PTR(err);
> > }
> > 
> > @@ -1843,14 +1862,6 @@ int xe_vm_create_ioctl(struct drm_device *dev, void *data,
> > 			 args->flags & DRM_XE_VM_CREATE_FAULT_MODE))
> > 		return -EINVAL;
> > 
> > -	if (XE_IOCTL_ERR(xe, args->flags & DRM_XE_VM_CREATE_FAULT_MODE &&
> > -			 xe_device_in_non_fault_mode(xe)))
> > -		return -EINVAL;
> > -
> > -	if (XE_IOCTL_ERR(xe, !(args->flags & DRM_XE_VM_CREATE_FAULT_MODE) &&
> > -			 xe_device_in_fault_mode(xe)))
> > -		return -EINVAL;
> > -
> > 	if (XE_IOCTL_ERR(xe, args->flags & DRM_XE_VM_CREATE_FAULT_MODE &&
> > 			 !xe->info.supports_usm))
> > 		return -EINVAL;
> > -- 
> > 2.34.1
> > 

  parent reply	other threads:[~2023-03-24 16:25 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-21 23:42 [Intel-xe] [PATCH 0/2] USM locking fixes Matthew Brost
2023-03-21 23:42 ` [Intel-xe] [PATCH 1/2] drn/xe: Drop usm lock around ASID xarray Matthew Brost
2023-03-24  6:17   ` Niranjana Vishwanathapura
2023-03-24  7:19     ` Matthew Brost
2023-03-21 23:42 ` [Intel-xe] [PATCH 2/2] drm/xe: Move in fault mode / non-fault mode check to xe_vm_create Matthew Brost
2023-03-24  6:10   ` Niranjana Vishwanathapura
2023-03-24  7:20     ` Matthew Brost
2023-03-24 16:25       ` Maarten Lankhorst
2023-03-24 16:27         ` Matthew Brost
2023-03-24 16:24     ` Matthew Brost [this message]
2023-03-21 23:44 ` [Intel-xe] ✓ CI.Patch_applied: success for USM locking fixes Patchwork
2023-03-21 23:45 ` [Intel-xe] ✓ CI.KUnit: " Patchwork
2023-03-21 23:49 ` [Intel-xe] ✓ CI.Build: " Patchwork
2023-03-22  0:13 ` [Intel-xe] ○ CI.BAT: info " 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=ZB3OzmD1Y9zfKCwl@DUT025-TGLU.fm.intel.com \
    --to=matthew.brost@intel.com \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=niranjana.vishwanathapura@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.