From: Jens Axboe <axboe@kernel.dk>
To: linux-fsdevel@vger.kernel.org
Cc: torvalds@linux-foundation.org, brauner@kernel.org,
viro@zeniv.linux.org.uk, Jens Axboe <axboe@kernel.dk>
Subject: [PATCH 3/8] snd: move mapping an iov_iter to user bufs into a helper
Date: Tue, 28 Mar 2023 11:36:08 -0600 [thread overview]
Message-ID: <20230328173613.555192-4-axboe@kernel.dk> (raw)
In-Reply-To: <20230328173613.555192-1-axboe@kernel.dk>
snd_pcm_{readv,writev} both do the same mapping of a struct iov_iter
into an array of buffers. Move this into a helper.
No functional changes intended in this patch.
Signed-off-by: Jens Axboe <axboe@kernel.dk>
---
sound/core/pcm_native.c | 55 ++++++++++++++++++++++-------------------
1 file changed, 30 insertions(+), 25 deletions(-)
diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c
index 331380c2438b..925d96343148 100644
--- a/sound/core/pcm_native.c
+++ b/sound/core/pcm_native.c
@@ -3512,13 +3512,36 @@ static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
return result;
}
+static void __user **snd_map_bufs(struct snd_pcm_runtime *runtime,
+ struct iov_iter *iter,
+ snd_pcm_uframes_t *frames, int max_segs)
+{
+ void __user **bufs;
+ unsigned long i;
+
+ if (!iter->user_backed)
+ return ERR_PTR(-EFAULT);
+ if (!iter->nr_segs)
+ return ERR_PTR(-EINVAL);
+ if (iter->nr_segs > max_segs || iter->nr_segs != runtime->channels)
+ return ERR_PTR(-EINVAL);
+ if (!frame_aligned(runtime, iter->iov->iov_len))
+ return ERR_PTR(-EINVAL);
+ bufs = kmalloc_array(iter->nr_segs, sizeof(void *), GFP_KERNEL);
+ if (bufs == NULL)
+ return ERR_PTR(-ENOMEM);
+ for (i = 0; i < iter->nr_segs; ++i)
+ bufs[i] = iter->iov[i].iov_base;
+ *frames = bytes_to_samples(runtime, iter->iov->iov_len);
+ return bufs;
+}
+
static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
{
struct snd_pcm_file *pcm_file;
struct snd_pcm_substream *substream;
struct snd_pcm_runtime *runtime;
snd_pcm_sframes_t result;
- unsigned long i;
void __user **bufs;
snd_pcm_uframes_t frames;
@@ -3530,18 +3553,9 @@ static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
if (runtime->state == SNDRV_PCM_STATE_OPEN ||
runtime->state == SNDRV_PCM_STATE_DISCONNECTED)
return -EBADFD;
- if (!iter_is_iovec(to))
- return -EINVAL;
- if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
- return -EINVAL;
- if (!frame_aligned(runtime, to->iov->iov_len))
- return -EINVAL;
- frames = bytes_to_samples(runtime, to->iov->iov_len);
- bufs = kmalloc_array(to->nr_segs, sizeof(void *), GFP_KERNEL);
- if (bufs == NULL)
- return -ENOMEM;
- for (i = 0; i < to->nr_segs; ++i)
- bufs[i] = to->iov[i].iov_base;
+ bufs = snd_map_bufs(runtime, to, &frames, 1024);
+ if (IS_ERR(bufs))
+ return PTR_ERR(bufs);
result = snd_pcm_lib_readv(substream, bufs, frames);
if (result > 0)
result = frames_to_bytes(runtime, result);
@@ -3555,7 +3569,6 @@ static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
struct snd_pcm_substream *substream;
struct snd_pcm_runtime *runtime;
snd_pcm_sframes_t result;
- unsigned long i;
void __user **bufs;
snd_pcm_uframes_t frames;
@@ -3567,17 +3580,9 @@ static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
if (runtime->state == SNDRV_PCM_STATE_OPEN ||
runtime->state == SNDRV_PCM_STATE_DISCONNECTED)
return -EBADFD;
- if (!iter_is_iovec(from))
- return -EINVAL;
- if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
- !frame_aligned(runtime, from->iov->iov_len))
- return -EINVAL;
- frames = bytes_to_samples(runtime, from->iov->iov_len);
- bufs = kmalloc_array(from->nr_segs, sizeof(void *), GFP_KERNEL);
- if (bufs == NULL)
- return -ENOMEM;
- for (i = 0; i < from->nr_segs; ++i)
- bufs[i] = from->iov[i].iov_base;
+ bufs = snd_map_bufs(runtime, from, &frames, 128);
+ if (IS_ERR(bufs))
+ return PTR_ERR(bufs);
result = snd_pcm_lib_writev(substream, bufs, frames);
if (result > 0)
result = frames_to_bytes(runtime, result);
--
2.39.2
next prev parent reply other threads:[~2023-03-28 17:36 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-28 17:36 [PATCHSET v4 0/8] Turn single segment imports into ITER_UBUF Jens Axboe
2023-03-28 17:36 ` [PATCH 1/8] iov_iter: teach iov_iter_iovec() to deal with ITER_UBUF Jens Axboe
2023-03-28 17:36 ` [PATCH 2/8] iov_iter: add iovec_nr_user_vecs() helper Jens Axboe
2023-03-28 18:42 ` Al Viro
2023-03-28 18:45 ` Linus Torvalds
2023-03-28 19:27 ` Jens Axboe
2023-03-28 17:36 ` Jens Axboe [this message]
2023-03-28 17:36 ` [PATCH 4/8] snd: make snd_map_bufs() deal with ITER_UBUF Jens Axboe
2023-03-28 17:50 ` Linus Torvalds
2023-03-28 17:52 ` Jens Axboe
2023-03-28 18:52 ` Al Viro
2023-03-28 19:28 ` Jens Axboe
2023-03-28 17:36 ` [PATCH 5/8] IB/hfi1: make hfi1_write_iter() deal with ITER_UBUF iov_iter Jens Axboe
2023-03-28 18:43 ` Linus Torvalds
2023-03-28 18:55 ` Matthew Wilcox
2023-03-28 19:05 ` Linus Torvalds
2023-03-28 19:16 ` Linus Torvalds
2023-03-28 21:21 ` Jens Axboe
2023-03-28 21:38 ` Jens Axboe
2023-03-28 21:51 ` Jens Axboe
2023-03-28 19:30 ` Jens Axboe
2023-03-28 20:38 ` Al Viro
2023-03-28 20:46 ` Jens Axboe
2023-03-28 22:06 ` Linus Torvalds
2023-03-28 17:36 ` [PATCH 6/8] IB/qib: make qib_write_iter() " Jens Axboe
2023-03-28 17:36 ` [PATCH 7/8] iov_iter: convert import_single_range() to ITER_UBUF Jens Axboe
2023-03-28 17:36 ` [PATCH 8/8] iov_iter: import single vector iovecs as ITER_UBUF Jens Axboe
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=20230328173613.555192-4-axboe@kernel.dk \
--to=axboe@kernel.dk \
--cc=brauner@kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=torvalds@linux-foundation.org \
--cc=viro@zeniv.linux.org.uk \
/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).