mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/4] Add iomem helpers for use from debugfs
@ 2024-11-07 16:34 Michal Wajdeczko
  2024-11-07 16:34 ` [PATCH 1/4] iov_iter: Provide copy_iomem_to|from_iter() Michal Wajdeczko
  2024-11-07 16:34 ` [PATCH 2/4] libfs: Provide simple_read_from|write_to_iomem() Michal Wajdeczko
  0 siblings, 2 replies; 4+ messages in thread
From: Michal Wajdeczko @ 2024-11-07 16:34 UTC (permalink / raw)
  To: intel-xe; +Cc: Michal Wajdeczko, linux-kernel, linux-fsdevel

This series attempts to promote helpers used by Xe [1] to libfs.
Earlier attempt [2] with similar helper was unnoticed.

[1] https://patchwork.freedesktop.org/series/140848/#rev1
[2] https://patchwork.freedesktop.org/series/133507/#rev1

Cc: linux-kernel@vger.kernel.org
Cc: linux-fsdevel@vger.kernel.org

Michal Wajdeczko (4):
  iov_iter: Provide copy_iomem_to|from_iter()
  libfs: Provide simple_read_from|write_to_iomem()
  drm/xe: Add read/write debugfs helpers for GGTT node
  drm/xe/pf: Expose access to the VF GGTT PTEs over debugfs

 drivers/gpu/drm/xe/xe_ggtt.c                | 52 ++++++++++++++
 drivers/gpu/drm/xe/xe_ggtt.h                |  7 ++
 drivers/gpu/drm/xe/xe_gt_sriov_pf_debugfs.c | 62 ++++++++++++++++
 fs/libfs.c                                  | 78 +++++++++++++++++++++
 include/linux/fs.h                          |  5 ++
 include/linux/uio.h                         |  4 ++
 lib/iov_iter.c                              | 42 +++++++++++
 7 files changed, 250 insertions(+)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/4] iov_iter: Provide copy_iomem_to|from_iter()
  2024-11-07 16:34 [PATCH 0/4] Add iomem helpers for use from debugfs Michal Wajdeczko
@ 2024-11-07 16:34 ` Michal Wajdeczko
  2024-11-07 20:13   ` Matthew Wilcox
  2024-11-07 16:34 ` [PATCH 2/4] libfs: Provide simple_read_from|write_to_iomem() Michal Wajdeczko
  1 sibling, 1 reply; 4+ messages in thread
From: Michal Wajdeczko @ 2024-11-07 16:34 UTC (permalink / raw)
  To: intel-xe
  Cc: Michal Wajdeczko, Alexander Viro, Andrew Morton, Matthew Wilcox,
	Rodrigo Vivi, Matthew Brost, linux-kernel, linux-fsdevel

Define simple copy helpers that work on I/O memory. This will
allow reuse of existing framework functions in new use cases.

Signed-off-by: Michal Wajdeczko <michal.wajdeczko@intel.com>
---
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: Rodrigo Vivi <rodrigo.vivi@intel.com>
Cc: Matthew Brost <matthew.brost@intel.com>
Cc: linux-kernel@vger.kernel.org
Cc: linux-fsdevel@vger.kernel.org
---
 include/linux/uio.h |  4 ++++
 lib/iov_iter.c      | 42 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+)

diff --git a/include/linux/uio.h b/include/linux/uio.h
index 853f9de5aa05..6d2a24293bd1 100644
--- a/include/linux/uio.h
+++ b/include/linux/uio.h
@@ -178,6 +178,10 @@ size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
 			 struct iov_iter *i);
 size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
 			 struct iov_iter *i);
+size_t copy_iomem_to_iter(const void __iomem *from, size_t offset,
+			  size_t bytes, struct iov_iter *i);
+size_t copy_iomem_from_iter(void __iomem *to, size_t offset,
+			    size_t bytes, struct iov_iter *i);
 
 size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i);
 size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i);
diff --git a/lib/iov_iter.c b/lib/iov_iter.c
index 908e75a28d90..e8c1f1c68716 100644
--- a/lib/iov_iter.c
+++ b/lib/iov_iter.c
@@ -435,6 +435,48 @@ size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
 }
 EXPORT_SYMBOL(copy_page_from_iter);
 
+size_t copy_iomem_to_iter(const void __iomem *from, size_t offset,
+			  size_t bytes, struct iov_iter *i)
+{
+	unsigned char buf[SMP_CACHE_BYTES];
+	size_t progress = 0, copied, len;
+
+	from += offset;
+	while (bytes) {
+		len = min(bytes, sizeof(buf));
+		memcpy_fromio(buf, from + progress, len);
+		copied = _copy_to_iter(buf, len, i);
+		if (!copied)
+			break;
+		bytes -= copied;
+		progress += copied;
+	}
+
+	return progress;
+}
+EXPORT_SYMBOL(copy_iomem_to_iter);
+
+size_t copy_iomem_from_iter(void __iomem *to, size_t offset,
+			    size_t bytes, struct iov_iter *i)
+{
+	unsigned char buf[SMP_CACHE_BYTES];
+	size_t progress = 0, copied, len;
+
+	to += offset;
+	while (bytes) {
+		len = min(bytes, sizeof(buf));
+		copied = _copy_from_iter(buf, len, i);
+		if (!copied)
+			break;
+		memcpy_toio(to + progress, buf, copied);
+		bytes -= copied;
+		progress += copied;
+	}
+
+	return progress;
+}
+EXPORT_SYMBOL(copy_iomem_from_iter);
+
 static __always_inline
 size_t zero_to_user_iter(void __user *iter_to, size_t progress,
 			 size_t len, void *priv, void *priv2)
-- 
2.43.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 2/4] libfs: Provide simple_read_from|write_to_iomem()
  2024-11-07 16:34 [PATCH 0/4] Add iomem helpers for use from debugfs Michal Wajdeczko
  2024-11-07 16:34 ` [PATCH 1/4] iov_iter: Provide copy_iomem_to|from_iter() Michal Wajdeczko
@ 2024-11-07 16:34 ` Michal Wajdeczko
  1 sibling, 0 replies; 4+ messages in thread
From: Michal Wajdeczko @ 2024-11-07 16:34 UTC (permalink / raw)
  To: intel-xe
  Cc: Michal Wajdeczko, Alexander Viro, Christian Brauner, Jan Kara,
	Rodrigo Vivi, Matthew Brost, linux-fsdevel, linux-kernel

New functions are similar to simple_read_from|write_to_buffer()
but work on the I/O memory instead. Will allow wider code reuse.

Signed-off-by: Michal Wajdeczko <michal.wajdeczko@intel.com>
---
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Jan Kara <jack@suse.cz>
Cc: Rodrigo Vivi <rodrigo.vivi@intel.com>
Cc: Matthew Brost <matthew.brost@intel.com>
Cc: linux-fsdevel@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
---
 fs/libfs.c         | 78 ++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/fs.h |  5 +++
 2 files changed, 83 insertions(+)

diff --git a/fs/libfs.c b/fs/libfs.c
index 46966fd8bcf9..0e4f0d50d4f3 100644
--- a/fs/libfs.c
+++ b/fs/libfs.c
@@ -1095,6 +1095,84 @@ void simple_release_fs(struct vfsmount **mount, int *count)
 }
 EXPORT_SYMBOL(simple_release_fs);
 
+/**
+ * simple_read_from_iomem() - copy data from the I/O memory to user space
+ * @to: the user space buffer to read to
+ * @count: the maximum number of bytes to read
+ * @ppos: the current position in the buffer
+ * @from: the I/O memory to read from
+ * @available: the size of the iomem memory
+ *
+ * The simple_read_from_iomem() function reads up to @count bytes from the
+ * I/O memory @from at offset @ppos into the user space address starting at @to.
+ *
+ * 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 simple_read_from_iomem(void __user *to, size_t count, loff_t *ppos,
+			       const void __iomem *from, size_t available)
+{
+	struct iov_iter iter;
+	loff_t pos = *ppos;
+	size_t copied;
+
+	if (pos < 0)
+		return -EINVAL;
+	if (pos >= available || !count)
+		return 0;
+	if (count > available - pos)
+		count = available - pos;
+	if (import_ubuf(ITER_DEST, to, count, &iter))
+		return -EFAULT;
+
+	copied = copy_iomem_to_iter(from, pos, count, &iter);
+	if (!copied)
+		return -EFAULT;
+
+	*ppos = pos + copied;
+	return copied;
+}
+EXPORT_SYMBOL(simple_read_from_iomem);
+
+/**
+ * simple_write_to_iomem() - copy data from user space to the I/O memory
+ * @to: the I/O memory to write to
+ * @available: the size of the I/O memory
+ * @ppos: the current position in the buffer
+ * @from: the user space buffer to read from
+ * @count: the maximum number of bytes to read
+ *
+ * The simple_write_to_iomem() function reads up to @count bytes from the user
+ * space address starting at @from into the I/O memory @to at offset @ppos.
+ *
+ * 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 simple_write_to_iomem(void __iomem *to, size_t available, loff_t *ppos,
+			      const void __user *from, size_t count)
+{
+	struct iov_iter iter;
+	loff_t pos = *ppos;
+	size_t copied;
+
+	if (pos < 0)
+		return -EINVAL;
+	if (pos >= available || !count)
+		return 0;
+	if (count > available - pos)
+		count = available - pos;
+	if (import_ubuf(ITER_SOURCE, (void __user *)from, count, &iter))
+		return -EFAULT;
+
+	copied = copy_iomem_from_iter(to, pos, count, &iter);
+	if (!copied)
+		return -EFAULT;
+
+	*ppos = pos + copied;
+	return copied;
+}
+EXPORT_SYMBOL(simple_write_to_iomem);
+
 /**
  * simple_read_from_buffer - copy data from the buffer to user space
  * @to: the user space buffer to read to
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 3559446279c1..2cc73c5961b0 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -3426,6 +3426,11 @@ extern ssize_t simple_read_from_buffer(void __user *to, size_t count,
 extern ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
 		const void __user *from, size_t count);
 
+ssize_t simple_read_from_iomem(void __user *to, size_t count, loff_t *ppos,
+			       const void __iomem *from, size_t available);
+ssize_t simple_write_to_iomem(void __iomem *to, size_t available, loff_t *ppos,
+			      const void __user *from, size_t count);
+
 struct offset_ctx {
 	struct maple_tree	mt;
 	unsigned long		next_offset;
-- 
2.43.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/4] iov_iter: Provide copy_iomem_to|from_iter()
  2024-11-07 16:34 ` [PATCH 1/4] iov_iter: Provide copy_iomem_to|from_iter() Michal Wajdeczko
@ 2024-11-07 20:13   ` Matthew Wilcox
  0 siblings, 0 replies; 4+ messages in thread
From: Matthew Wilcox @ 2024-11-07 20:13 UTC (permalink / raw)
  To: Michal Wajdeczko
  Cc: intel-xe, Alexander Viro, Andrew Morton, Rodrigo Vivi,
	Matthew Brost, linux-kernel, linux-fsdevel

On Thu, Nov 07, 2024 at 05:34:45PM +0100, Michal Wajdeczko wrote:
> +size_t copy_iomem_to_iter(const void __iomem *from, size_t offset,
> +			  size_t bytes, struct iov_iter *i)
> +{
> +	unsigned char buf[SMP_CACHE_BYTES];
> +	size_t progress = 0, copied, len;
> +
> +	from += offset;
> +	while (bytes) {
> +		len = min(bytes, sizeof(buf));
> +		memcpy_fromio(buf, from + progress, len);
> +		copied = _copy_to_iter(buf, len, i);
> +		if (!copied)
> +			break;
> +		bytes -= copied;
> +		progress += copied;
> +	}

This seems like a rather sad implementation.  Why not:

	if (WARN_ON_ONCE(i->data_source))
		return 0;
	if (user_backed_iter(i))
		might_fault();
	return iterate_and_advance(i, bytes, (void *)addr,
			copy_iomem_to_iter, memcpy_iomem_to_iter);

along with

size_t memcpy_iomem_to_iter()
{
	memcpy_fromio(iter_to, from + progress, len);
	return 0;
}`

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-11-07 20:13 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-11-07 16:34 [PATCH 0/4] Add iomem helpers for use from debugfs Michal Wajdeczko
2024-11-07 16:34 ` [PATCH 1/4] iov_iter: Provide copy_iomem_to|from_iter() Michal Wajdeczko
2024-11-07 20:13   ` Matthew Wilcox
2024-11-07 16:34 ` [PATCH 2/4] libfs: Provide simple_read_from|write_to_iomem() Michal Wajdeczko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®