OSDN Git Service

ALSA: core: Add memory copy helpers between iov_iter and iomem
authorTakashi Iwai <tiwai@suse.de>
Tue, 15 Aug 2023 19:01:14 +0000 (21:01 +0200)
committerTakashi Iwai <tiwai@suse.de>
Fri, 18 Aug 2023 10:18:16 +0000 (12:18 +0200)
Add two more helpers for copying memory between iov_iter and iomem,
which will be used by the new PCM copy ops in a few drivers.
The existing helpers became wrappers of those now.

Note that copy_from/to_iter() returns the copied bytes, hence the
error condition is adjusted accordingly.

Link: https://lore.kernel.org/r/20230815190136.8987-4-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>
include/sound/pcm.h
sound/core/memory.c

index 6d6ec51..1f6df47 100644 (file)
@@ -1559,6 +1559,11 @@ static inline u64 pcm_format_to_bits(snd_pcm_format_t pcm_format)
 #define pcm_dbg(pcm, fmt, args...) \
        dev_dbg((pcm)->card->dev, fmt, ##args)
 
+/* helpers for copying between iov_iter and iomem */
+int copy_to_iter_fromio(struct iov_iter *itert, const void __iomem *src,
+                       size_t count);
+int copy_from_iter_toio(void __iomem *dst, struct iov_iter *iter, size_t count);
+
 struct snd_pcm_status64 {
        snd_pcm_state_t state;          /* stream state */
        u8 rsvd[4];
index 5d894dc..2d2d009 100644 (file)
@@ -9,6 +9,7 @@
 #include <linux/io.h>
 #include <linux/uaccess.h>
 #include <sound/core.h>
+#include <sound/pcm.h>
 
 /**
  * copy_to_user_fromio - copy data from mmio-space to user-space
  */
 int copy_to_user_fromio(void __user *dst, const volatile void __iomem *src, size_t count)
 {
+       struct iov_iter iter;
+
+       if (import_ubuf(ITER_DEST, dst, count, &iter))
+               return -EFAULT;
+       return copy_to_iter_fromio(&iter, (const void __iomem *)src, count);
+}
+EXPORT_SYMBOL(copy_to_user_fromio);
+
+/**
+ * copy_to_iter_fromio - copy data from mmio-space to iov_iter
+ * @dst: the destination iov_iter
+ * @src: the source pointer on mmio
+ * @count: the data size to copy in bytes
+ *
+ * Copies the data from mmio-space to iov_iter.
+ *
+ * Return: Zero if successful, or non-zero on failure.
+ */
+int copy_to_iter_fromio(struct iov_iter *dst, const void __iomem *src,
+                       size_t count)
+{
 #if defined(__i386__) || defined(CONFIG_SPARC32)
-       return copy_to_user(dst, (const void __force*)src, count) ? -EFAULT : 0;
+       return copy_to_iter((const void __force *)src, count, dst) == count ? 0 : -EFAULT;
 #else
        char buf[256];
        while (count) {
@@ -31,16 +53,15 @@ int copy_to_user_fromio(void __user *dst, const volatile void __iomem *src, size
                if (c > sizeof(buf))
                        c = sizeof(buf);
                memcpy_fromio(buf, (void __iomem *)src, c);
-               if (copy_to_user(dst, buf, c))
+               if (copy_to_iter(buf, c, dst) != c)
                        return -EFAULT;
                count -= c;
-               dst += c;
                src += c;
        }
        return 0;
 #endif
 }
-EXPORT_SYMBOL(copy_to_user_fromio);
+EXPORT_SYMBOL(copy_to_iter_fromio);
 
 /**
  * copy_from_user_toio - copy data from user-space to mmio-space
@@ -54,22 +75,41 @@ EXPORT_SYMBOL(copy_to_user_fromio);
  */
 int copy_from_user_toio(volatile void __iomem *dst, const void __user *src, size_t count)
 {
+       struct iov_iter iter;
+
+       if (import_ubuf(ITER_SOURCE, (void __user *)src, count, &iter))
+               return -EFAULT;
+       return copy_from_iter_toio((void __iomem *)dst, &iter, count);
+}
+EXPORT_SYMBOL(copy_from_user_toio);
+
+/**
+ * copy_from_iter_toio - copy data from iov_iter to mmio-space
+ * @dst: the destination pointer on mmio-space
+ * @src: the source iov_iter
+ * @count: the data size to copy in bytes
+ *
+ * Copies the data from iov_iter to mmio-space.
+ *
+ * Return: Zero if successful, or non-zero on failure.
+ */
+int copy_from_iter_toio(void __iomem *dst, struct iov_iter *src, size_t count)
+{
 #if defined(__i386__) || defined(CONFIG_SPARC32)
-       return copy_from_user((void __force *)dst, src, count) ? -EFAULT : 0;
+       return copy_from_iter((void __force *)dst, count, src) == count ? 0 : -EFAULT;
 #else
        char buf[256];
        while (count) {
                size_t c = count;
                if (c > sizeof(buf))
                        c = sizeof(buf);
-               if (copy_from_user(buf, src, c))
+               if (copy_from_iter(buf, c, src) != c)
                        return -EFAULT;
                memcpy_toio(dst, buf, c);
                count -= c;
                dst += c;
-               src += c;
        }
        return 0;
 #endif
 }
-EXPORT_SYMBOL(copy_from_user_toio);
+EXPORT_SYMBOL(copy_from_iter_toio);