[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20241112200454.2211-2-michal.wajdeczko@intel.com>
Date: Tue, 12 Nov 2024 21:04:51 +0100
From: Michal Wajdeczko <michal.wajdeczko@...el.com>
To: intel-xe@...ts.freedesktop.org
Cc: Michal Wajdeczko <michal.wajdeczko@...el.com>,
Matthew Wilcox <willy@...radead.org>,
Alexander Viro <viro@...iv.linux.org.uk>,
Andrew Morton <akpm@...ux-foundation.org>,
Rodrigo Vivi <rodrigo.vivi@...el.com>,
Matthew Brost <matthew.brost@...el.com>,
linux-kernel@...r.kernel.org,
linux-fsdevel@...r.kernel.org
Subject: [PATCH v2 1/4] iov_iter: Provide copy_iomem_to|from_iter()
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@...el.com>
Cc: Matthew Wilcox <willy@...radead.org>
---
Cc: Alexander Viro <viro@...iv.linux.org.uk>
Cc: Andrew Morton <akpm@...ux-foundation.org>
Cc: Rodrigo Vivi <rodrigo.vivi@...el.com>
Cc: Matthew Brost <matthew.brost@...el.com>
Cc: linux-kernel@...r.kernel.org
Cc: linux-fsdevel@...r.kernel.org
---
v2: use iterate_and_advance to treat user_iter separately (Matthew)
---
include/linux/uio.h | 3 +++
lib/iov_iter.c | 66 +++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 69 insertions(+)
diff --git a/include/linux/uio.h b/include/linux/uio.h
index 853f9de5aa05..354a6b2d1e94 100644
--- a/include/linux/uio.h
+++ b/include/linux/uio.h
@@ -183,6 +183,9 @@ 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);
size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i);
+size_t copy_iomem_to_iter(const void __iomem *from, size_t bytes, struct iov_iter *i);
+size_t copy_iomem_from_iter(void __iomem *to, size_t bytes, struct iov_iter *i);
+
static inline size_t copy_folio_to_iter(struct folio *folio, size_t offset,
size_t bytes, struct iov_iter *i)
{
diff --git a/lib/iov_iter.c b/lib/iov_iter.c
index 908e75a28d90..80feff9c8875 100644
--- a/lib/iov_iter.c
+++ b/lib/iov_iter.c
@@ -323,6 +323,72 @@ size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache);
#endif
+static __always_inline
+size_t memcpy_iomem_to_iter(void *iter_to, size_t progress, size_t len,
+ void *from, void *priv2)
+{
+ memcpy_fromio(iter_to, from + progress, len);
+ return 0;
+}
+
+static __always_inline
+size_t memcpy_iomem_from_iter(void *iter_from, size_t progress, size_t len,
+ void *to, void *priv2)
+{
+ memcpy_toio(to + progress, iter_from, len);
+ return 0;
+}
+
+static __always_inline
+size_t copy_iomem_to_user_iter(void __user *iter_to, size_t progress,
+ size_t len, void *from, void *priv2)
+{
+ unsigned char buf[SMP_CACHE_BYTES];
+ size_t chunk = min(len, sizeof(buf));
+
+ memcpy_fromio(buf, from + progress, chunk);
+ chunk -= copy_to_user_iter(iter_to, progress, chunk, buf, priv2);
+ return len - chunk;
+}
+
+static __always_inline
+size_t copy_iomem_from_user_iter(void __user *iter_from, size_t progress,
+ size_t len, void *to, void *priv2)
+{
+ unsigned char buf[SMP_CACHE_BYTES];
+ size_t chunk = min(len, sizeof(buf));
+
+ chunk -= copy_from_user_iter(iter_from, progress, chunk, buf, priv2);
+ memcpy_toio(to, buf, chunk);
+ return len - chunk;
+}
+
+size_t copy_iomem_to_iter(const void __iomem *from, size_t bytes, struct iov_iter *i)
+{
+ if (WARN_ON_ONCE(i->data_source))
+ return 0;
+ if (user_backed_iter(i))
+ might_fault();
+
+ return iterate_and_advance(i, bytes, (void *)from,
+ copy_iomem_to_user_iter,
+ memcpy_iomem_to_iter);
+}
+EXPORT_SYMBOL(copy_iomem_to_iter);
+
+size_t copy_iomem_from_iter(void __iomem *to, size_t bytes, struct iov_iter *i)
+{
+ if (WARN_ON_ONCE(!i->data_source))
+ return 0;
+ if (user_backed_iter(i))
+ might_fault();
+
+ return iterate_and_advance(i, bytes, (void *)to,
+ copy_iomem_from_user_iter,
+ memcpy_iomem_from_iter);
+}
+EXPORT_SYMBOL(copy_iomem_from_iter);
+
static inline bool page_copy_sane(struct page *page, size_t offset, size_t n)
{
struct page *head;
--
2.43.0
Powered by blists - more mailing lists