Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

ALSA: pcm: Add copy ops with iov_iter

iov_iter is a universal interface to copy the data chunk from/to
user-space and kernel in a unified manner. This API can fit for ALSA
PCM copy ops, too; we had to split to copy_user and copy_kernel in the
past, and those can be unified to a single ops with iov_iter.

This patch adds a new PCM copy ops that passes iov_iter for copying
both kernel and user-space in the same way. This patch touches only
the ALSA PCM core part, and the actual users will be replaced in the
following patches.

The expansion of iov_iter is done in the PCM core right before calling
each copy callback. It's a bit suboptimal, but I took this now as
it's the most straightforward replacement. The more conversion to
iov_iter in the caller side is a TODO for future.

As of now, the old copy_user and copy_kernel ops are still kept.
Once after all users are converted, we'll drop the old copy_user and
copy_kernel ops, too.

Link: https://lore.kernel.org/r/20230815190136.8987-3-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>

+71 -45
+3
include/sound/pcm.h
··· 16 16 #include <linux/bitops.h> 17 17 #include <linux/pm_qos.h> 18 18 #include <linux/refcount.h> 19 + #include <linux/uio.h> 19 20 20 21 #define snd_pcm_substream_chip(substream) ((substream)->private_data) 21 22 #define snd_pcm_chip(pcm) ((pcm)->private_data) ··· 69 68 struct snd_pcm_audio_tstamp_report *audio_tstamp_report); 70 69 int (*fill_silence)(struct snd_pcm_substream *substream, int channel, 71 70 unsigned long pos, unsigned long bytes); 71 + int (*copy)(struct snd_pcm_substream *substream, int channel, 72 + unsigned long pos, struct iov_iter *iter, unsigned long bytes); 72 73 int (*copy_user)(struct snd_pcm_substream *substream, int channel, 73 74 unsigned long pos, void __user *buf, 74 75 unsigned long bytes);
+67 -44
sound/core/pcm_lib.c
··· 1973 1973 1974 1974 typedef int (*pcm_transfer_f)(struct snd_pcm_substream *substream, 1975 1975 int channel, unsigned long hwoff, 1976 - void *buf, unsigned long bytes); 1976 + struct iov_iter *iter, unsigned long bytes); 1977 1977 1978 1978 typedef int (*pcm_copy_f)(struct snd_pcm_substream *, snd_pcm_uframes_t, void *, 1979 - snd_pcm_uframes_t, snd_pcm_uframes_t, pcm_transfer_f); 1979 + snd_pcm_uframes_t, snd_pcm_uframes_t, pcm_transfer_f, 1980 + bool); 1980 1981 1981 1982 /* calculate the target DMA-buffer position to be written/read */ 1982 1983 static void *get_dma_ptr(struct snd_pcm_runtime *runtime, ··· 1987 1986 channel * (runtime->dma_bytes / runtime->channels); 1988 1987 } 1989 1988 1990 - /* default copy_user ops for write; used for both interleaved and non- modes */ 1989 + /* default copy ops for write; used for both interleaved and non- modes */ 1991 1990 static int default_write_copy(struct snd_pcm_substream *substream, 1992 1991 int channel, unsigned long hwoff, 1993 - void *buf, unsigned long bytes) 1992 + struct iov_iter *iter, unsigned long bytes) 1994 1993 { 1995 - if (copy_from_user(get_dma_ptr(substream->runtime, channel, hwoff), 1996 - (void __user *)buf, bytes)) 1994 + if (!copy_from_iter(get_dma_ptr(substream->runtime, channel, hwoff), 1995 + bytes, iter)) 1997 1996 return -EFAULT; 1998 - return 0; 1999 - } 2000 - 2001 - /* default copy_kernel ops for write */ 2002 - static int default_write_copy_kernel(struct snd_pcm_substream *substream, 2003 - int channel, unsigned long hwoff, 2004 - void *buf, unsigned long bytes) 2005 - { 2006 - memcpy(get_dma_ptr(substream->runtime, channel, hwoff), buf, bytes); 2007 1997 return 0; 2008 1998 } 2009 1999 ··· 2003 2011 * a NULL buffer is passed 2004 2012 */ 2005 2013 static int fill_silence(struct snd_pcm_substream *substream, int channel, 2006 - unsigned long hwoff, void *buf, unsigned long bytes) 2014 + unsigned long hwoff, struct iov_iter *iter, 2015 + unsigned long bytes) 2007 2016 { 2008 2017 struct snd_pcm_runtime *runtime = substream->runtime; 2009 2018 ··· 2020 2027 return 0; 2021 2028 } 2022 2029 2023 - /* default copy_user ops for read; used for both interleaved and non- modes */ 2030 + /* default copy ops for read; used for both interleaved and non- modes */ 2024 2031 static int default_read_copy(struct snd_pcm_substream *substream, 2025 2032 int channel, unsigned long hwoff, 2026 - void *buf, unsigned long bytes) 2033 + struct iov_iter *iter, unsigned long bytes) 2027 2034 { 2028 - if (copy_to_user((void __user *)buf, 2029 - get_dma_ptr(substream->runtime, channel, hwoff), 2030 - bytes)) 2035 + if (!copy_to_iter(get_dma_ptr(substream->runtime, channel, hwoff), 2036 + bytes, iter)) 2031 2037 return -EFAULT; 2032 2038 return 0; 2033 2039 } 2034 2040 2035 - /* default copy_kernel ops for read */ 2036 - static int default_read_copy_kernel(struct snd_pcm_substream *substream, 2037 - int channel, unsigned long hwoff, 2038 - void *buf, unsigned long bytes) 2041 + /* a wrapper for calling old copy_kernel or copy_user ops */ 2042 + static int call_old_copy(struct snd_pcm_substream *substream, 2043 + int channel, unsigned long hwoff, 2044 + struct iov_iter *iter, unsigned long bytes) 2039 2045 { 2040 - memcpy(buf, get_dma_ptr(substream->runtime, channel, hwoff), bytes); 2041 - return 0; 2046 + if (iov_iter_is_kvec(iter)) 2047 + return substream->ops->copy_kernel(substream, channel, hwoff, 2048 + iter_iov_addr(iter), bytes); 2049 + else 2050 + return substream->ops->copy_user(substream, channel, hwoff, 2051 + iter_iov_addr(iter), bytes); 2052 + } 2053 + 2054 + /* call transfer with the filled iov_iter */ 2055 + static int do_transfer(struct snd_pcm_substream *substream, int c, 2056 + unsigned long hwoff, void *data, unsigned long bytes, 2057 + pcm_transfer_f transfer, bool in_kernel) 2058 + { 2059 + struct iov_iter iter; 2060 + int err, type; 2061 + 2062 + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 2063 + type = ITER_SOURCE; 2064 + else 2065 + type = ITER_DEST; 2066 + 2067 + if (in_kernel) { 2068 + struct kvec kvec = { data, bytes }; 2069 + 2070 + iov_iter_kvec(&iter, type, &kvec, 1, bytes); 2071 + return transfer(substream, c, hwoff, &iter, bytes); 2072 + } 2073 + 2074 + err = import_ubuf(type, (__force void __user *)data, bytes, &iter); 2075 + if (err) 2076 + return err; 2077 + return transfer(substream, c, hwoff, &iter, bytes); 2042 2078 } 2043 2079 2044 2080 /* call transfer function with the converted pointers and sizes; ··· 2077 2055 snd_pcm_uframes_t hwoff, void *data, 2078 2056 snd_pcm_uframes_t off, 2079 2057 snd_pcm_uframes_t frames, 2080 - pcm_transfer_f transfer) 2058 + pcm_transfer_f transfer, 2059 + bool in_kernel) 2081 2060 { 2082 2061 struct snd_pcm_runtime *runtime = substream->runtime; 2083 2062 ··· 2086 2063 hwoff = frames_to_bytes(runtime, hwoff); 2087 2064 off = frames_to_bytes(runtime, off); 2088 2065 frames = frames_to_bytes(runtime, frames); 2089 - return transfer(substream, 0, hwoff, data + off, frames); 2066 + 2067 + return do_transfer(substream, 0, hwoff, data + off, frames, transfer, 2068 + in_kernel); 2090 2069 } 2091 2070 2092 2071 /* call transfer function with the converted pointers and sizes for each ··· 2098 2073 snd_pcm_uframes_t hwoff, void *data, 2099 2074 snd_pcm_uframes_t off, 2100 2075 snd_pcm_uframes_t frames, 2101 - pcm_transfer_f transfer) 2076 + pcm_transfer_f transfer, 2077 + bool in_kernel) 2102 2078 { 2103 2079 struct snd_pcm_runtime *runtime = substream->runtime; 2104 2080 int channels = runtime->channels; ··· 2117 2091 if (!data || !*bufs) 2118 2092 err = fill_silence(substream, c, hwoff, NULL, frames); 2119 2093 else 2120 - err = transfer(substream, c, hwoff, *bufs + off, 2121 - frames); 2094 + err = do_transfer(substream, c, hwoff, *bufs + off, 2095 + frames, transfer, in_kernel); 2122 2096 if (err < 0) 2123 2097 return err; 2124 2098 } ··· 2134 2108 if (substream->runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED || 2135 2109 substream->runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) 2136 2110 return interleaved_copy(substream, off, NULL, 0, frames, 2137 - fill_silence); 2111 + fill_silence, true); 2138 2112 else 2139 2113 return noninterleaved_copy(substream, off, NULL, 0, frames, 2140 - fill_silence); 2114 + fill_silence, true); 2141 2115 } 2142 2116 2143 2117 /* sanity-check for read/write methods */ ··· 2147 2121 if (PCM_RUNTIME_CHECK(substream)) 2148 2122 return -ENXIO; 2149 2123 runtime = substream->runtime; 2150 - if (snd_BUG_ON(!substream->ops->copy_user && !runtime->dma_area)) 2124 + if (snd_BUG_ON(!substream->ops->copy && !substream->ops->copy_user && !runtime->dma_area)) 2151 2125 return -EINVAL; 2152 2126 if (runtime->state == SNDRV_PCM_STATE_OPEN) 2153 2127 return -EBADFD; ··· 2252 2226 transfer = fill_silence; 2253 2227 else 2254 2228 return -EINVAL; 2255 - } else if (in_kernel) { 2256 - if (substream->ops->copy_kernel) 2257 - transfer = substream->ops->copy_kernel; 2258 - else 2259 - transfer = is_playback ? 2260 - default_write_copy_kernel : default_read_copy_kernel; 2261 2229 } else { 2262 - if (substream->ops->copy_user) 2263 - transfer = (pcm_transfer_f)substream->ops->copy_user; 2230 + if (substream->ops->copy) 2231 + transfer = substream->ops->copy; 2232 + else if ((in_kernel && substream->ops->copy_kernel) || 2233 + (!in_kernel && substream->ops->copy_user)) 2234 + transfer = call_old_copy; 2264 2235 else 2265 2236 transfer = is_playback ? 2266 2237 default_write_copy : default_read_copy; ··· 2330 2307 if (!is_playback) 2331 2308 snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_CPU); 2332 2309 err = writer(substream, appl_ofs, data, offset, frames, 2333 - transfer); 2310 + transfer, in_kernel); 2334 2311 if (is_playback) 2335 2312 snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE); 2336 2313 snd_pcm_stream_lock_irq(substream);
+1 -1
sound/core/pcm_native.c
··· 809 809 runtime->boundary *= 2; 810 810 811 811 /* clear the buffer for avoiding possible kernel info leaks */ 812 - if (runtime->dma_area && !substream->ops->copy_user) { 812 + if (runtime->dma_area && !substream->ops->copy && !substream->ops->copy_user) { 813 813 size_t size = runtime->dma_bytes; 814 814 815 815 if (runtime->info & SNDRV_PCM_INFO_MMAP)