linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Michal Wajdeczko <michal.wajdeczko@intel.com>
To: intel-xe@lists.freedesktop.org
Cc: Michal Wajdeczko <michal.wajdeczko@intel.com>,
	linux-fsdevel@vger.kernel.org, dri-devel@lists.freedesktop.org,
	Lucas De Marchi <lucas.demarchi@intel.com>
Subject: [PATCH 1/4] libfs: add simple_read_from_iomem()
Date: Sun, 12 May 2024 17:36:03 +0200	[thread overview]
Message-ID: <20240512153606.1996-2-michal.wajdeczko@intel.com> (raw)
In-Reply-To: <20240512153606.1996-1-michal.wajdeczko@intel.com>

It's similar to simple_read_from_buffer() but instead allows to
copy data from the I/O memory in PAGE_SIZE chunks.

Signed-off-by: Michal Wajdeczko <michal.wajdeczko@intel.com>
---
Cc: linux-fsdevel@vger.kernel.org
Cc: dri-devel@lists.freedesktop.org
Cc: Lucas De Marchi <lucas.demarchi@intel.com>
---
 fs/libfs.c         | 50 ++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/fs.h |  3 +++
 2 files changed, 53 insertions(+)

diff --git a/fs/libfs.c b/fs/libfs.c
index 3a6f2cb364f8..be8aa42a2f11 100644
--- a/fs/libfs.c
+++ b/fs/libfs.c
@@ -15,6 +15,7 @@
 #include <linux/mutex.h>
 #include <linux/namei.h>
 #include <linux/exportfs.h>
+#include <linux/io.h>
 #include <linux/iversion.h>
 #include <linux/writeback.h>
 #include <linux/buffer_head.h> /* sync_mapping_buffers */
@@ -1040,6 +1041,55 @@ 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 (but no
+ * more than %PAGE_SIZE 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 volatile void __iomem *from, size_t available)
+{
+	loff_t pos = *ppos;
+	size_t ret;
+	void *buf;
+
+	if (pos < 0)
+		return -EINVAL;
+	if (pos >= available || !count)
+		return 0;
+	if (count > available - pos)
+		count = available - pos;
+	if (count > PAGE_SIZE)
+		count = PAGE_SIZE;
+
+	buf = kmalloc(count, GFP_NOWAIT | __GFP_NOWARN);
+	if (!buf)
+		return -ENOMEM;
+
+	memcpy_fromio(buf, from + pos, count);
+	ret = copy_to_user(to, buf, count);
+
+	kfree(buf);
+
+	if (ret == count)
+		return -EFAULT;
+
+	count -= ret;
+	*ppos = pos + count;
+	return count;
+}
+EXPORT_SYMBOL(simple_read_from_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 8dfd53b52744..eb4a7b10a1a0 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -3331,6 +3331,9 @@ 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 volatile void __iomem *from, size_t available);
+
 struct offset_ctx {
 	struct maple_tree	mt;
 	unsigned long		next_offset;
-- 
2.43.0


  reply	other threads:[~2024-05-12 15:36 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-12 15:36 [PATCH 0/4] Expose raw access to GuC log over debugfs Michal Wajdeczko
2024-05-12 15:36 ` Michal Wajdeczko [this message]
2024-05-12 15:36 ` [PATCH 2/4] iosys-map: add iosys_map_read_from() helper Michal Wajdeczko
2024-05-12 15:36 ` [PATCH 3/4] drm/xe: Add wrapper for iosys_map_read_from Michal Wajdeczko
2024-05-12 15:36 ` [PATCH 4/4] drm/xe/guc: Expose raw access to GuC log over debugfs Michal Wajdeczko
2024-05-13 16:53   ` John Harrison
2024-05-14 14:58     ` Michal Wajdeczko
2024-05-14 18:13       ` John Harrison
2024-05-14 20:31         ` Matthew Brost
2024-05-14 21:15           ` Michal Wajdeczko
2024-05-14 21:54             ` John Harrison
2024-05-14 22:31             ` Matthew Brost
2024-05-14 20:41         ` Michal Wajdeczko
2024-05-14 22:15           ` John Harrison

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=20240512153606.1996-2-michal.wajdeczko@intel.com \
    --to=michal.wajdeczko@intel.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=lucas.demarchi@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).