mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Takashi Iwai <tiwai@suse.de>
To: alsa-devel@alsa-project.org
Cc: linux-kernel@vger.kernel.org, Takashi Iwai <tiwai@suse.de>
Subject: [PATCH v2 03/25] ALSA: core: Add memory copy helpers between iov_iter and iomem
Date: Tue, 15 Aug 2023 21:01:14 +0200	[thread overview]
Message-ID: <20230815190136.8987-4-tiwai@suse.de> (raw)
In-Reply-To: <20230815190136.8987-1-tiwai@suse.de>

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.

Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
 include/sound/pcm.h |  5 ++++
 sound/core/memory.c | 56 ++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 53 insertions(+), 8 deletions(-)

diff --git a/include/sound/pcm.h b/include/sound/pcm.h
index ff4a0c1c93a2..f75beead79e3 100644
--- a/include/sound/pcm.h
+++ b/include/sound/pcm.h
@@ -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];
diff --git a/sound/core/memory.c b/sound/core/memory.c
index 5d894dc32f7d..2d2d0094c897 100644
--- a/sound/core/memory.c
+++ b/sound/core/memory.c
@@ -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
@@ -21,9 +22,30 @@
  * Return: Zero if successful, or non-zero on failure.
  */
 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
@@ -53,23 +74,42 @@ EXPORT_SYMBOL(copy_to_user_fromio);
  * Return: Zero if successful, or non-zero on failure.
  */
 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);
-- 
2.35.3


  parent reply	other threads:[~2023-08-15 19:03 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-15 19:01 [PATCH v2 00/25] ALSA: Generic PCM copy ops using iov_iter Takashi Iwai
2023-08-15 19:01 ` [PATCH v2 01/25] iov_iter: Export import_ubuf() Takashi Iwai
2023-08-15 19:01 ` [PATCH v2 02/25] ALSA: pcm: Add copy ops with iov_iter Takashi Iwai
2023-09-02  5:30   ` Al Viro
2023-09-02  6:00     ` Takashi Iwai
2023-08-15 19:01 ` Takashi Iwai [this message]
2023-08-15 19:01 ` [PATCH v2 04/25] ALSA: dummy: Convert to generic PCM copy ops Takashi Iwai
2023-08-15 19:01 ` [PATCH v2 05/25] ALSA: gus: " Takashi Iwai
2023-08-15 19:01 ` [PATCH v2 06/25] ALSA: emu8000: " Takashi Iwai
2023-09-02  5:36   ` Al Viro
2023-09-02  6:01     ` Takashi Iwai
2023-08-15 19:01 ` [PATCH v2 07/25] ALSA: es1938: " Takashi Iwai
2023-08-15 19:01 ` [PATCH v2 08/25] ALSA: korg1212: " Takashi Iwai
2023-08-15 19:01 ` [PATCH v2 09/25] ALSA: nm256: " Takashi Iwai
2023-08-15 19:01 ` [PATCH v2 10/25] ALSA: rme32: " Takashi Iwai
2023-08-15 19:01 ` [PATCH v2 11/25] ALSA: rme96: " Takashi Iwai
2023-08-15 19:01 ` [PATCH v2 12/25] ALSA: hdsp: " Takashi Iwai
2023-08-15 19:01 ` [PATCH v2 13/25] ALSA: rme9652: " Takashi Iwai
2023-08-15 19:01 ` [PATCH v2 14/25] ALSA: sh: " Takashi Iwai
2023-08-15 19:01 ` [PATCH v2 15/25] ALSA: xen: " Takashi Iwai
2023-08-15 19:01 ` [PATCH v2 16/25] ALSA: pcmtest: Update comment about " Takashi Iwai
2023-08-15 19:01 ` [PATCH v2 17/25] media: solo6x10: Convert to generic " Takashi Iwai
2023-08-18  7:48   ` Hans Verkuil
2023-08-15 19:01 ` [PATCH v2 18/25] ASoC: component: Add " Takashi Iwai
2023-08-15 19:01 ` [PATCH v2 19/25] ASoC: mediatek: Convert to " Takashi Iwai
2023-08-15 19:01 ` [PATCH v2 20/25] ASoC: qcom: " Takashi Iwai
2023-08-15 19:01 ` [PATCH v2 21/25] ASoC: dmaengine: " Takashi Iwai
2023-08-15 19:01 ` [PATCH v2 22/25] ASoC: dmaengine: Use iov_iter for process callback, too Takashi Iwai
2023-08-15 19:01 ` [PATCH v2 23/25] ALSA: doc: Update description for the new PCM copy ops Takashi Iwai
2023-08-15 19:01 ` [PATCH v2 24/25] ASoC: pcm: Drop obsoleted PCM copy_user ops Takashi Iwai
2023-08-15 19:01 ` [PATCH v2 25/25] ALSA: pcm: Drop obsoleted PCM copy_user and copy_kernel ops Takashi Iwai

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=20230815190136.8987-4-tiwai@suse.de \
    --to=tiwai@suse.de \
    --cc=alsa-devel@alsa-project.org \
    --cc=linux-kernel@vger.kernel.org \
    /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

all inboxes | Powered by JetHome®