Skip to content

Commit 1b2ff63

Browse files
committed
ALSA: Align the syntax of iov_iter helpers with standard ones
We introduced a couple of helpers for copying iomem over iov_iter, and the functions were formed like the former copy_from/to_user(), and the return value was adjusted to 0/-EFAULT, which made the code transition a bit easier at that time. OTOH, the standard copy_from/to_iter() functions have different argument orders and the return value, and this difference can be confusing. It's not only confusing but dangerous; actually I did write a wrong code due to that once :-< For reducing the confusion, this patch changes the syntax of those helpers to align with the standard copy_from/to_iter(). The argument order is changed and the return value is the size of copied bytes. The callers of those functions are updated accordingly, too. Link: https://patch.msgid.link/20241230114903.4959-1-tiwai@suse.de Signed-off-by: Takashi Iwai <tiwai@suse.de>
1 parent 1e63e3c commit 1b2ff63

File tree

6 files changed

+56
-32
lines changed

6 files changed

+56
-32
lines changed

include/sound/pcm.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,9 +1532,10 @@ static inline u64 pcm_format_to_bits(snd_pcm_format_t pcm_format)
15321532
dev_dbg((pcm)->card->dev, fmt, ##args)
15331533

15341534
/* helpers for copying between iov_iter and iomem */
1535-
int copy_to_iter_fromio(struct iov_iter *itert, const void __iomem *src,
1536-
size_t count);
1537-
int copy_from_iter_toio(void __iomem *dst, struct iov_iter *iter, size_t count);
1535+
size_t copy_to_iter_fromio(const void __iomem *src, size_t bytes,
1536+
struct iov_iter *iter) __must_check;
1537+
size_t copy_from_iter_toio(void __iomem *dst, size_t bytes,
1538+
struct iov_iter *iter) __must_check;
15381539

15391540
struct snd_pcm_status64 {
15401541
snd_pcm_state_t state; /* stream state */

sound/core/memory.c

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,38 +27,43 @@ int copy_to_user_fromio(void __user *dst, const volatile void __iomem *src, size
2727

2828
if (import_ubuf(ITER_DEST, dst, count, &iter))
2929
return -EFAULT;
30-
return copy_to_iter_fromio(&iter, (const void __iomem *)src, count);
30+
if (copy_to_iter_fromio((const void __iomem *)src, count, &iter) != count)
31+
return -EFAULT;
32+
return 0;
3133
}
3234
EXPORT_SYMBOL(copy_to_user_fromio);
3335

3436
/**
3537
* copy_to_iter_fromio - copy data from mmio-space to iov_iter
36-
* @dst: the destination iov_iter
3738
* @src: the source pointer on mmio
3839
* @count: the data size to copy in bytes
40+
* @dst: the destination iov_iter
3941
*
4042
* Copies the data from mmio-space to iov_iter.
4143
*
42-
* Return: Zero if successful, or non-zero on failure.
44+
* Return: number of bytes to be copied
4345
*/
44-
int copy_to_iter_fromio(struct iov_iter *dst, const void __iomem *src,
45-
size_t count)
46+
size_t copy_to_iter_fromio(const void __iomem *src, size_t count,
47+
struct iov_iter *dst)
4648
{
4749
#if defined(__i386__) || defined(CONFIG_SPARC32)
48-
return copy_to_iter((const void __force *)src, count, dst) == count ? 0 : -EFAULT;
50+
return copy_to_iter((const void __force *)src, count, dst);
4951
#else
5052
char buf[256];
53+
size_t res = 0;
54+
5155
while (count) {
5256
size_t c = count;
5357
if (c > sizeof(buf))
5458
c = sizeof(buf);
5559
memcpy_fromio(buf, (void __iomem *)src, c);
5660
if (copy_to_iter(buf, c, dst) != c)
57-
return -EFAULT;
61+
return res;
5862
count -= c;
5963
src += c;
64+
res += c;
6065
}
61-
return 0;
66+
return res;
6267
#endif
6368
}
6469
EXPORT_SYMBOL(copy_to_iter_fromio);
@@ -79,37 +84,43 @@ int copy_from_user_toio(volatile void __iomem *dst, const void __user *src, size
7984

8085
if (import_ubuf(ITER_SOURCE, (void __user *)src, count, &iter))
8186
return -EFAULT;
82-
return copy_from_iter_toio((void __iomem *)dst, &iter, count);
87+
if (copy_from_iter_toio((void __iomem *)dst, count, &iter) != count)
88+
return -EFAULT;
89+
return 0;
8390
}
8491
EXPORT_SYMBOL(copy_from_user_toio);
8592

8693
/**
8794
* copy_from_iter_toio - copy data from iov_iter to mmio-space
8895
* @dst: the destination pointer on mmio-space
89-
* @src: the source iov_iter
9096
* @count: the data size to copy in bytes
97+
* @src: the source iov_iter
9198
*
9299
* Copies the data from iov_iter to mmio-space.
93100
*
94-
* Return: Zero if successful, or non-zero on failure.
101+
* Return: number of bytes to be copied
95102
*/
96-
int copy_from_iter_toio(void __iomem *dst, struct iov_iter *src, size_t count)
103+
size_t copy_from_iter_toio(void __iomem *dst, size_t count,
104+
struct iov_iter *src)
97105
{
98106
#if defined(__i386__) || defined(CONFIG_SPARC32)
99-
return copy_from_iter((void __force *)dst, count, src) == count ? 0 : -EFAULT;
107+
return copy_from_iter((void __force *)dst, count, src);
100108
#else
101109
char buf[256];
110+
size_t res = 0;
111+
102112
while (count) {
103113
size_t c = count;
104114
if (c > sizeof(buf))
105115
c = sizeof(buf);
106116
if (copy_from_iter(buf, c, src) != c)
107-
return -EFAULT;
117+
return res;
108118
memcpy_toio(dst, buf, c);
109119
count -= c;
110120
dst += c;
121+
res += c;
111122
}
112-
return 0;
123+
return res;
113124
#endif
114125
}
115126
EXPORT_SYMBOL(copy_from_iter_toio);

sound/pci/nm256/nm256.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,9 @@ snd_nm256_playback_copy(struct snd_pcm_substream *substream,
696696
struct snd_pcm_runtime *runtime = substream->runtime;
697697
struct nm256_stream *s = runtime->private_data;
698698

699-
return copy_from_iter_toio(s->bufptr + pos, src, count);
699+
if (copy_from_iter_toio(s->bufptr + pos, count, src) != count)
700+
return -EFAULT;
701+
return 0;
700702
}
701703

702704
/*
@@ -710,7 +712,9 @@ snd_nm256_capture_copy(struct snd_pcm_substream *substream,
710712
struct snd_pcm_runtime *runtime = substream->runtime;
711713
struct nm256_stream *s = runtime->private_data;
712714

713-
return copy_to_iter_fromio(dst, s->bufptr + pos, count);
715+
if (copy_to_iter_fromio(s->bufptr + pos, count, dst) != count)
716+
return -EFAULT;
717+
return 0;
714718
}
715719

716720
#endif /* !__i386__ */

sound/pci/rme32.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,10 @@ static int snd_rme32_playback_copy(struct snd_pcm_substream *substream,
256256
{
257257
struct rme32 *rme32 = snd_pcm_substream_chip(substream);
258258

259-
return copy_from_iter_toio(rme32->iobase + RME32_IO_DATA_BUFFER + pos,
260-
src, count);
259+
if (copy_from_iter_toio(rme32->iobase + RME32_IO_DATA_BUFFER + pos,
260+
count, src) != count)
261+
return -EFAULT;
262+
return 0;
261263
}
262264

263265
/* copy callback for halfduplex mode */
@@ -267,9 +269,10 @@ static int snd_rme32_capture_copy(struct snd_pcm_substream *substream,
267269
{
268270
struct rme32 *rme32 = snd_pcm_substream_chip(substream);
269271

270-
return copy_to_iter_fromio(dst,
271-
rme32->iobase + RME32_IO_DATA_BUFFER + pos,
272-
count);
272+
if (copy_to_iter_fromio(rme32->iobase + RME32_IO_DATA_BUFFER + pos,
273+
count, dst) != count)
274+
return -EFAULT;
275+
return 0;
273276
}
274277

275278
/*

sound/pci/rme96.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,10 @@ snd_rme96_playback_copy(struct snd_pcm_substream *substream,
322322
{
323323
struct rme96 *rme96 = snd_pcm_substream_chip(substream);
324324

325-
return copy_from_iter_toio(rme96->iobase + RME96_IO_PLAY_BUFFER + pos,
326-
src, count);
325+
if (copy_from_iter_toio(rme96->iobase + RME96_IO_PLAY_BUFFER + pos,
326+
count, src) != count)
327+
return -EFAULT;
328+
return 0;
327329
}
328330

329331
static int
@@ -333,9 +335,10 @@ snd_rme96_capture_copy(struct snd_pcm_substream *substream,
333335
{
334336
struct rme96 *rme96 = snd_pcm_substream_chip(substream);
335337

336-
return copy_to_iter_fromio(dst,
337-
rme96->iobase + RME96_IO_REC_BUFFER + pos,
338-
count);
338+
if (copy_to_iter_fromio(rme96->iobase + RME96_IO_REC_BUFFER + pos,
339+
count, dst) != count)
340+
return -EFAULT;
341+
return 0;
339342
}
340343

341344
/*

sound/soc/qcom/lpass-platform.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,14 +1232,16 @@ static int lpass_platform_copy(struct snd_soc_component *component,
12321232

12331233
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
12341234
if (is_cdc_dma_port(dai_id)) {
1235-
ret = copy_from_iter_toio(dma_buf, buf, bytes);
1235+
if (copy_from_iter_toio(dma_buf, bytes, buf) != bytes)
1236+
ret = -EFAULT;
12361237
} else {
12371238
if (copy_from_iter((void __force *)dma_buf, bytes, buf) != bytes)
12381239
ret = -EFAULT;
12391240
}
12401241
} else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
12411242
if (is_cdc_dma_port(dai_id)) {
1242-
ret = copy_to_iter_fromio(buf, dma_buf, bytes);
1243+
if (copy_to_iter_fromio(dma_buf, bytes, buf) != bytes)
1244+
ret = -EFAULT;
12431245
} else {
12441246
if (copy_to_iter((void __force *)dma_buf, bytes, buf) != bytes)
12451247
ret = -EFAULT;

0 commit comments

Comments
 (0)