intel-xe.lists.freedesktop.org archive mirror
 help / color / mirror / Atom feed
From: "Zbigniew Kempczyński" <zbigniew.kempczynski@intel.com>
To: Mauro Carvalho Chehab <mauro.chehab@linux.intel.com>
Cc: intel-xe@lists.freedesktop.org
Subject: Re: [Intel-xe] [PATCH] drm/xe/bo: Return object bo size on create
Date: Thu, 6 Apr 2023 07:37:47 +0200	[thread overview]
Message-ID: <20230406053747.f5bfpo7nt2z7fv3j@zkempczy-mobl2> (raw)
In-Reply-To: <20230403184807.177d7ef5@maurocar-mobl2>

On Mon, Apr 03, 2023 at 06:48:07PM +0200, Mauro Carvalho Chehab wrote:
> On Tue, 28 Mar 2023 15:08:26 +0200
> Zbigniew Kempczyński <zbigniew.kempczynski@intel.com> wrote:
> 
> > Driver may alter bo size requested by the user. Return real object
> > size to make userspace aware how to arrange vm bindings.
> > 
> > Signed-off-by: Zbigniew Kempczyński <zbigniew.kempczynski@intel.com>
> > ---
> >  drivers/gpu/drm/xe/xe_bo.c | 1 +
> >  1 file changed, 1 insertion(+)
> > 
> > diff --git a/drivers/gpu/drm/xe/xe_bo.c b/drivers/gpu/drm/xe/xe_bo.c
> > index e4d079b61d52..410e797931ac 100644
> > --- a/drivers/gpu/drm/xe/xe_bo.c
> > +++ b/drivers/gpu/drm/xe/xe_bo.c
> > @@ -1564,6 +1564,7 @@ int xe_gem_create_ioctl(struct drm_device *dev, void *data,
> >  		return err;
> >  
> >  	args->handle = handle;
> > +	args->size = bo->size;
> >  
> >  	return 0;
> >  }
> 
> An alternative approach would be instead to return -EINVAL if the
> buffers aren't aligned. Something like this (untested) patch.
> 
> Regards,
> Mauro
> 
> [PATCH] xe/xe_bo: Don't allow creating unaligned buffers
> 
> Ensure that bo will always be properly aligned.
> 
> Signed-off-by: Mauro Carvalho Chehab <mchehab@kernel.org>
> 
> diff --git a/drivers/gpu/drm/xe/xe_bo.c b/drivers/gpu/drm/xe/xe_bo.c
> index f25257bbfc65..ecc04b887790 100644
> --- a/drivers/gpu/drm/xe/xe_bo.c
> +++ b/drivers/gpu/drm/xe/xe_bo.c
> @@ -985,6 +985,7 @@ struct xe_bo *__xe_bo_create_locked(struct xe_device *xe, struct xe_bo *bo,
>  	};
>  	struct ttm_placement *placement;
>  	uint32_t alignment;
> +	size_t aligned_size;
>  	int err;
>  
>  	/* Only kernel objects should set GT */
> @@ -993,24 +994,28 @@ struct xe_bo *__xe_bo_create_locked(struct xe_device *xe, struct xe_bo *bo,
>  	if (XE_WARN_ON(!size))
>  		return ERR_PTR(-EINVAL);
>  
> -	if (!bo) {
> -		bo = xe_bo_alloc();
> -		if (IS_ERR(bo))
> -			return bo;
> -	}
> -
> -	bo->requested_size = size;
>  	if (flags & (XE_BO_CREATE_VRAM0_BIT | XE_BO_CREATE_VRAM1_BIT |
>  		     XE_BO_CREATE_STOLEN_BIT) &&
>  	    !(flags & XE_BO_CREATE_IGNORE_MIN_PAGE_SIZE_BIT) &&
>  	    xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K) {
> -		size = ALIGN(size, SZ_64K);
> +		aligned_size = ALIGN(size, SZ_64K);
>  		flags |= XE_BO_INTERNAL_64K;
>  		alignment = SZ_64K >> PAGE_SHIFT;
>  	} else {
> +		aligned_size = ALIGN(size, SZ_4K);
> +		flags &= ~XE_BO_INTERNAL_64K;
>  		alignment = SZ_4K >> PAGE_SHIFT;
>  	}
>  
> +	if (aligned_size != size)
> +		return ERR_PTR(-EINVAL);
> +
> +	if (!bo) {
> +		bo = xe_bo_alloc();
> +		if (IS_ERR(bo))
> +			return bo;
> +	}
> +
>  	bo->gt = gt;
>  	bo->size = size;
>  	bo->flags = flags;
> diff --git a/drivers/gpu/drm/xe/xe_ggtt.c b/drivers/gpu/drm/xe/xe_ggtt.c
> index a430d1568890..f8a55ef9e789 100644
> --- a/drivers/gpu/drm/xe/xe_ggtt.c
> +++ b/drivers/gpu/drm/xe/xe_ggtt.c
> @@ -273,10 +273,8 @@ void xe_ggtt_map_bo(struct xe_ggtt *ggtt, struct xe_bo *bo)
>  		xe_ggtt_set_pte(ggtt, start + offset, pte);
>  	}
>  
> -	if (bo->size == bo->requested_size) {
> -		pte = xe_ggtt_pte_encode(ggtt->scratch ?: bo, 0);
> -		xe_ggtt_set_pte(ggtt, start + bo->size, pte);
> -	}
> +	pte = xe_ggtt_pte_encode(ggtt->scratch ?: bo, 0);
> +	xe_ggtt_set_pte(ggtt, start + bo->size, pte);
>  
>  	if (ggtt->invalidate) {
>  		xe_ggtt_invalidate(ggtt->gt);
> @@ -309,11 +307,9 @@ static int __xe_ggtt_insert_bo_at(struct xe_ggtt *ggtt, struct xe_bo *bo,
>  	 * fact we have programmed this GGTT entry to a valid entry. BO aligned
>  	 * to 64k already have padding so no need to add an extra page.
>  	 */
> -	if (bo->size == bo->requested_size) {
> -		size += SZ_4K;
> -		if (end != U64_MAX)
> -			end += SZ_4K;
> -	}
> +        size += alignment;
> +        if (end != U64_MAX)
> +                end += alignment;
>  
>  	mutex_lock(&ggtt->lock);
>  	err = drm_mm_insert_node_in_range(&ggtt->mm, &bo->ggtt_node, size,

I'm going to test this patch and let you know.

At the moment I got two concerns:

1. take a look to xe_query.c:query_memory_usage()
   min_page_size is not always 64K whereas current bo create
   path doesn't honour this configuration.

2. xe_drm.h also should be changed to reflect changes
   which disallows kernel modification of size

   struct drm_xe_gem_create {
   ...
	/**
	 * @size: Requested size for the object
	 *
	 * The (page-aligned) allocated size for the object will be returned.
	 */
	__u64 size;

--
Zbigniew

  reply	other threads:[~2023-04-06  5:37 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-28 13:08 [Intel-xe] [PATCH] drm/xe/bo: Return object bo size on create Zbigniew Kempczyński
2023-03-28 13:10 ` [Intel-xe] ✓ CI.Patch_applied: success for " Patchwork
2023-03-28 13:11 ` [Intel-xe] ✓ CI.KUnit: " Patchwork
2023-03-28 13:15 ` [Intel-xe] ✓ CI.Build: " Patchwork
2023-03-28 13:36 ` [Intel-xe] ○ CI.BAT: info " Patchwork
2023-03-28 16:28 ` [Intel-xe] [PATCH] " Niranjana Vishwanathapura
2023-03-28 18:04 ` Matthew Brost
2023-04-03 16:48 ` Mauro Carvalho Chehab
2023-04-06  5:37   ` Zbigniew Kempczyński [this message]
2023-04-12 14:07   ` Maarten Lankhorst
2023-04-13  6:31     ` Zbigniew Kempczyński
2023-04-03 17:16 ` [Intel-xe] ✗ CI.Patch_applied: failure for drm/xe/bo: Return object bo size on create (rev2) 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=20230406053747.f5bfpo7nt2z7fv3j@zkempczy-mobl2 \
    --to=zbigniew.kempczynski@intel.com \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=mauro.chehab@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;
as well as URLs for NNTP newsgroup(s).