Intel-XE Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Matthew Brost <matthew.brost@intel.com>
To: Michal Wajdeczko <michal.wajdeczko@intel.com>
Cc: <intel-xe@lists.freedesktop.org>
Subject: Re: [PATCH 1/2] drm/xe: Add read/write debugfs helpers for GGTT node
Date: Mon, 4 Nov 2024 16:49:55 -0800	[thread overview]
Message-ID: <Zylrsw40oX0fGVU2@lstrano-desk.jf.intel.com> (raw)
In-Reply-To: <20241103201633.1859-2-michal.wajdeczko@intel.com>

On Sun, Nov 03, 2024 at 09:16:32PM +0100, Michal Wajdeczko wrote:
> In upcoming patch we want to allow copying and updating PTEs of
> some GGTT nodes over debugfs. Add simple helpers for that.
> 

On the surface it very much looks like are reinventing something that
almost certainly already exists or if it doesn't we should try to do
this in a common way. Have you looked for any similar code to this in
the kernel.

Matt

> Signed-off-by: Michal Wajdeczko <michal.wajdeczko@intel.com>
> ---
>  drivers/gpu/drm/xe/xe_ggtt.c | 178 +++++++++++++++++++++++++++++++++++
>  drivers/gpu/drm/xe/xe_ggtt.h |   7 ++
>  2 files changed, 185 insertions(+)
> 
> diff --git a/drivers/gpu/drm/xe/xe_ggtt.c b/drivers/gpu/drm/xe/xe_ggtt.c
> index 558fac8bb6fb..e003f5f51ac6 100644
> --- a/drivers/gpu/drm/xe/xe_ggtt.c
> +++ b/drivers/gpu/drm/xe/xe_ggtt.c
> @@ -838,3 +838,181 @@ u64 xe_ggtt_print_holes(struct xe_ggtt *ggtt, u64 alignment, struct drm_printer
>  
>  	return total;
>  }
> +
> +#ifdef CONFIG_DEBUG_FS
> +
> +static size_t copy_iomem_to_user_by_chunk(void __user *to, const void __iomem *from,
> +					  size_t count, void *buf, size_t size)
> +{
> +	size_t chunk;
> +	size_t rem;
> +
> +	while (count) {
> +		chunk = umin(count, size);
> +		memcpy_fromio(buf, from, chunk);
> +		rem = copy_to_user(to, buf, chunk);
> +		count -= chunk - rem;
> +		if (rem)
> +			break;
> +		from += chunk;
> +		to += chunk;
> +	}
> +
> +	return count;
> +}
> +
> +static size_t copy_iomem_to_user(void __user *to, const void __iomem *from, size_t count)
> +{
> +	char chunk[64];
> +	size_t size;
> +	size_t rem;
> +	void *buf;
> +
> +	if (!count)
> +		return 0;
> +
> +	size = count > sizeof(chunk) ? umin(PAGE_SIZE, count) : 0;
> +	buf = size ? kmalloc(size, GFP_NOWAIT | __GFP_NORETRY) : NULL;
> +	if (buf) {
> +		rem = copy_iomem_to_user_by_chunk(to, from, count, buf, size);
> +		kfree(buf);
> +		return rem;
> +	}
> +
> +	return copy_iomem_to_user_by_chunk(to, from, count, chunk, sizeof(chunk));
> +}
> +
> +static ssize_t simple_read_from_iomem(void __user *to, size_t count, loff_t *ppos,
> +				      const void __iomem *from, size_t available)
> +{
> +	loff_t pos = *ppos;
> +	size_t rem;
> +
> +	if (pos < 0)
> +		return -EINVAL;
> +	if (pos >= available || !count)
> +		return 0;
> +	if (count > available - pos)
> +		count = available - pos;
> +
> +	rem = copy_iomem_to_user(to, from + pos, count);
> +	if (rem == count)
> +		return -EFAULT;
> +
> +	count -= rem;
> +	*ppos = pos + count;
> +	return count;
> +}
> +
> +/**
> + * xe_ggtt_node_read() - Copy PTEs from the GGTT node to the user space buffer
> + * @node: the GGTT node to read from
> + * @buf: the user space buffer to read to
> + * @count: the maximum number of bytes to read
> + * @ppos: the current position
> + *
> + * Return: On success, the number of bytes read is returned and the offset
> + * @ppos is advanced by this number, or negative value is returned on error.
> + */
> +ssize_t xe_ggtt_node_read(struct xe_ggtt_node *node, char __user *buf,
> +			  size_t count, loff_t *ppos)
> +{
> +	if (!xe_ggtt_node_allocated(node))
> +		return 0;
> +
> +	xe_tile_assert(node->ggtt->tile, IS_ALIGNED(node->base.start, XE_PAGE_SIZE));
> +	xe_tile_assert(node->ggtt->tile, IS_ALIGNED(node->base.size, XE_PAGE_SIZE));
> +
> +	return simple_read_from_iomem(buf, count, ppos,
> +				      &node->ggtt->gsm[node->base.start / XE_PAGE_SIZE],
> +				      size_mul(sizeof(u64), node->base.size / XE_PAGE_SIZE));
> +}
> +
> +static size_t copy_iomem_from_user_by_chunk(void __iomem *to, const void __user *from,
> +					    size_t count, void *buf, size_t size)
> +{
> +	size_t chunk;
> +	size_t rem;
> +
> +	while (count) {
> +		chunk = umin(count, size);
> +		rem = copy_from_user(buf, from, chunk);
> +		memcpy_toio(to, buf, chunk - rem);
> +		count -= chunk - rem;
> +		if (rem)
> +			break;
> +		from += chunk;
> +		to += chunk;
> +	}
> +
> +	return count;
> +}
> +
> +static size_t copy_iomem_from_user(void __iomem *to, const void __user *from, size_t count)
> +{
> +	char chunk[64];
> +	size_t size;
> +	size_t rem;
> +	void *buf;
> +
> +	if (!count)
> +		return 0;
> +
> +	size = count > sizeof(chunk) ? umin(PAGE_SIZE, count) : 0;
> +	buf = size ? kmalloc(size, GFP_NOWAIT | __GFP_NORETRY) : NULL;
> +	if (buf) {
> +		rem = copy_iomem_from_user_by_chunk(to, from, count, buf, size);
> +		kfree(buf);
> +		return rem;
> +	}
> +
> +	return copy_iomem_from_user_by_chunk(to, from, count, chunk, sizeof(chunk));
> +}
> +
> +static ssize_t simple_write_to_iomem(void __iomem *to, size_t available, loff_t *ppos,
> +				     const void __user *from, size_t count)
> +{
> +	loff_t pos = *ppos;
> +	size_t rem;
> +
> +	if (pos < 0)
> +		return -EINVAL;
> +	if (pos >= available || !count)
> +		return 0;
> +	if (count > available - pos)
> +		count = available - pos;
> +
> +	rem = copy_iomem_from_user(to + pos, from, count);
> +	if (rem == count)
> +		return -EFAULT;
> +
> +	count -= rem;
> +	*ppos = pos + count;
> +	return count;
> +}
> +
> +/**
> + * xe_ggtt_node_write() - Update PTEs of the GGTT node using data from the user space buffer
> + * @node: the GGTT node to write to
> + * @buf: the user space buffer to read from
> + * @count: the maximum number of bytes to read
> + * @ppos: the current position
> + *
> + * Return: On success, the number of bytes written is returned and the offset
> + * @ppos is advanced by this number, or negative value is returned on error.
> + */
> +ssize_t xe_ggtt_node_write(struct xe_ggtt_node *node, const char __user *buf,
> +			   size_t count, loff_t *ppos)
> +{
> +	if (!xe_ggtt_node_allocated(node))
> +		return -ENXIO;
> +
> +	xe_tile_assert(node->ggtt->tile, IS_ALIGNED(node->base.start, XE_PAGE_SIZE));
> +	xe_tile_assert(node->ggtt->tile, IS_ALIGNED(node->base.size, XE_PAGE_SIZE));
> +
> +	return simple_write_to_iomem(&node->ggtt->gsm[node->base.start / XE_PAGE_SIZE],
> +				     size_mul(sizeof(u64), node->base.size / XE_PAGE_SIZE),
> +				     ppos, buf, count);
> +}
> +
> +#endif
> diff --git a/drivers/gpu/drm/xe/xe_ggtt.h b/drivers/gpu/drm/xe/xe_ggtt.h
> index 27e7d67de004..64746e23053e 100644
> --- a/drivers/gpu/drm/xe/xe_ggtt.h
> +++ b/drivers/gpu/drm/xe/xe_ggtt.h
> @@ -34,6 +34,13 @@ u64 xe_ggtt_largest_hole(struct xe_ggtt *ggtt, u64 alignment, u64 *spare);
>  int xe_ggtt_dump(struct xe_ggtt *ggtt, struct drm_printer *p);
>  u64 xe_ggtt_print_holes(struct xe_ggtt *ggtt, u64 alignment, struct drm_printer *p);
>  
> +#ifdef CONFIG_DEBUG_FS
> +ssize_t xe_ggtt_node_read(struct xe_ggtt_node *node, char __user *buf,
> +			  size_t count, loff_t *pos);
> +ssize_t xe_ggtt_node_write(struct xe_ggtt_node *node, const char __user *buf,
> +			   size_t count, loff_t *pos);
> +#endif
> +
>  #ifdef CONFIG_PCI_IOV
>  void xe_ggtt_assign(const struct xe_ggtt_node *node, u16 vfid);
>  #endif
> -- 
> 2.43.0
> 

  parent reply	other threads:[~2024-11-05  0:49 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-03 20:16 [PATCH 0/2] PF: Expose access to the VF GGTT PTEs over debugfs Michal Wajdeczko
2024-11-03 20:16 ` [PATCH 1/2] drm/xe: Add read/write debugfs helpers for GGTT node Michal Wajdeczko
2024-11-05  0:28   ` Lis, Tomasz
2024-11-05  0:49   ` Matthew Brost [this message]
2024-11-05 15:25     ` Michal Wajdeczko
2024-11-05 17:39       ` Matthew Brost
2024-11-05 18:57         ` Matthew Brost
2024-11-03 20:16 ` [PATCH 2/2] drm/xe/pf: Expose access to the VF GGTT PTEs over debugfs Michal Wajdeczko
2024-11-05  0:28   ` Lis, Tomasz
2024-11-05  1:14   ` Matthew Brost
2024-11-05 16:41     ` Michal Wajdeczko
2024-11-05 17:26       ` Matthew Brost
2024-11-03 20:21 ` ✓ CI.Patch_applied: success for PF: " Patchwork
2024-11-03 20:21 ` ✓ CI.checkpatch: " Patchwork
2024-11-03 20:22 ` ✓ CI.KUnit: " Patchwork
2024-11-03 20:34 ` ✓ CI.Build: " Patchwork
2024-11-03 20:36 ` ✓ CI.Hooks: " Patchwork
2024-11-03 20:38 ` ✓ CI.checksparse: " Patchwork
2024-11-03 20:57 ` ✓ CI.BAT: " Patchwork
2024-11-03 21:59 ` ✗ CI.FULL: failure " 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=Zylrsw40oX0fGVU2@lstrano-desk.jf.intel.com \
    --to=matthew.brost@intel.com \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=michal.wajdeczko@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