From nobody Fri Oct 10 09:15:00 2025 Received: from pegase1.c-s.fr (pegase1.c-s.fr [93.17.236.30]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 66ADF1F5827; Sat, 14 Jun 2025 06:51:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=93.17.236.30 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749883863; cv=none; b=K22aJIXKRvDht8gTJy+hmGLC/2iPflHOCRtb0CNzQrCq8oXZnY2MFJQswhz01vbzDDu/Cs1nfxV+7dmxUdhCHWjVss0pytfuXlPCGzYA4Nxe+q+Ihh3W/3Yqgeak5EdIC5I2Pi6rX22xteEBHOA9K+X1gVmFdr3lh8jHvuCjUp0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749883863; c=relaxed/simple; bh=5vrwlCVOy4jeGa33bcqO+2rR3nBMx3HvtoPGJ2RWsVU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=g8wzJ8ASfDUSLS/0qAZ7GysbReX7bC6EJe+QLLa7HePkqG6/hZoQUbNyiGVUNhWBl1+prqMpOpRbZ7NPu3XYaZSx7R07tX7cgnq30e3du8xM+kNEw5BRKNhpSmTlWRDIOXbS/rUBUP14oTmeDjyJjTWKM3r/BilmH4KKV2Bme20= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=csgroup.eu; spf=pass smtp.mailfrom=csgroup.eu; arc=none smtp.client-ip=93.17.236.30 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=csgroup.eu Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=csgroup.eu Received: from localhost (mailhub3.si.c-s.fr [192.168.12.233]) by localhost (Postfix) with ESMTP id 4bK6Cv5hzLz9st4; Sat, 14 Jun 2025 08:43:23 +0200 (CEST) X-Virus-Scanned: amavisd-new at c-s.fr Received: from pegase1.c-s.fr ([192.168.12.234]) by localhost (pegase1.c-s.fr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id L409-xjRegJR; Sat, 14 Jun 2025 08:43:23 +0200 (CEST) Received: from messagerie.si.c-s.fr (messagerie.si.c-s.fr [192.168.25.192]) by pegase1.c-s.fr (Postfix) with ESMTP id 4bK6Cv4yr6z9st0; Sat, 14 Jun 2025 08:43:23 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by messagerie.si.c-s.fr (Postfix) with ESMTP id A5CF68B77D; Sat, 14 Jun 2025 08:43:23 +0200 (CEST) X-Virus-Scanned: amavisd-new at c-s.fr Received: from messagerie.si.c-s.fr ([127.0.0.1]) by localhost (messagerie.si.c-s.fr [127.0.0.1]) (amavisd-new, port 10023) with ESMTP id 2HnLZ9cTI1KL; Sat, 14 Jun 2025 08:43:23 +0200 (CEST) Received: from PO20335.idsi0.si.c-s.fr (unknown [192.168.235.99]) by messagerie.si.c-s.fr (Postfix) with ESMTP id 2AE5F8B77B; Sat, 14 Jun 2025 08:43:23 +0200 (CEST) From: Christophe Leroy To: Jaroslav Kysela , Takashi Iwai Cc: Christophe Leroy , linux-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-sound@vger.kernel.org, Herve Codina Subject: [PATCH v3 1/4] ALSA: pcm: refactor copy from/to user in SNDRV_PCM_IOCTL_SYNC_PTR Date: Sat, 14 Jun 2025 08:43:14 +0200 Message-ID: X-Mailer: git-send-email 2.47.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1749883393; l=5425; i=christophe.leroy@csgroup.eu; s=20211009; h=from:subject:message-id; bh=5vrwlCVOy4jeGa33bcqO+2rR3nBMx3HvtoPGJ2RWsVU=; b=rwQl9AMrqvh5oH5kZMNKZrKoFsFn3IJbM+1z73rWcyEdrgr7WLHnyshXLGTwKvv3nkI5vm4Ps cMag2oQb0xcBHxFROdgngbmJgC0fIEgl3FGMciwNyxRA4XCPT7uDsLH X-Developer-Key: i=christophe.leroy@csgroup.eu; a=ed25519; pk=HIzTzUj91asvincQGOFx6+ZF5AoUuP9GdOtQChs7Mm0= Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In an effort of optimising SNDRV_PCM_IOCTL_SYNC_PTR ioctl which is a hot path, lets first refactor the copy from and to user with macros. This is done with macros and not static inline fonctions because types differs between the different versions of snd_pcm_sync_ptr() like functions. First step is to refactor only snd_pcm_ioctl_sync_ptr_compat() and snd_pcm_ioctl_sync_ptr_x32() as it would be a performance regression for snd_pcm_sync_ptr() and snd_pcm_ioctl_sync_ptr_buggy() for now. They may be refactored after next patch. Signed-off-by: Christophe Leroy --- sound/core/pcm_compat.c | 14 ++------------ sound/core/pcm_native.c | 42 +++++++++++++++++++++++++++++------------ 2 files changed, 32 insertions(+), 24 deletions(-) diff --git a/sound/core/pcm_compat.c b/sound/core/pcm_compat.c index a42ec7f5a1da..17540020ac2f 100644 --- a/sound/core/pcm_compat.c +++ b/sound/core/pcm_compat.c @@ -418,9 +418,7 @@ static int snd_pcm_ioctl_sync_ptr_x32(struct snd_pcm_su= bstream *substream, if (snd_BUG_ON(!runtime)) return -EINVAL; =20 - if (get_user(sflags, &src->flags) || - get_user(scontrol.appl_ptr, &src->c.control.appl_ptr) || - get_user(scontrol.avail_min, &src->c.control.avail_min)) + if (snd_pcm_sync_ptr_get_user(sflags, scontrol, src)) return -EFAULT; if (sflags & SNDRV_PCM_SYNC_PTR_HWSYNC) { err =3D snd_pcm_hwsync(substream); @@ -450,15 +448,7 @@ static int snd_pcm_ioctl_sync_ptr_x32(struct snd_pcm_s= ubstream *substream, } if (!(sflags & SNDRV_PCM_SYNC_PTR_APPL)) snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE); - if (put_user(sstatus.state, &src->s.status.state) || - put_user(sstatus.hw_ptr, &src->s.status.hw_ptr) || - put_user(sstatus.tstamp.tv_sec, &src->s.status.tstamp_sec) || - put_user(sstatus.tstamp.tv_nsec, &src->s.status.tstamp_nsec) || - put_user(sstatus.suspended_state, &src->s.status.suspended_state) || - put_user(sstatus.audio_tstamp.tv_sec, &src->s.status.audio_tstamp_sec= ) || - put_user(sstatus.audio_tstamp.tv_nsec, &src->s.status.audio_tstamp_ns= ec) || - put_user(scontrol.appl_ptr, &src->c.control.appl_ptr) || - put_user(scontrol.avail_min, &src->c.control.avail_min)) + if (snd_pcm_sync_ptr_put_user(sstatus, scontrol, src)) return -EFAULT; =20 return 0; diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c index ecb71bf1859d..1f8f6d95b18c 100644 --- a/sound/core/pcm_native.c +++ b/sound/core/pcm_native.c @@ -3052,6 +3052,34 @@ static inline int snd_pcm_hwsync(struct snd_pcm_subs= tream *substream) return snd_pcm_delay(substream, NULL); } =20 +#define snd_pcm_sync_ptr_get_user(__f, __c, __ptr) ({ \ + int __err =3D 0; \ + typeof(*(__ptr)) __user *__src =3D (__ptr); \ + \ + if (get_user(__f, &src->flags) || \ + get_user(__c.appl_ptr, &__src->c.control.appl_ptr) || \ + get_user(__c.avail_min, &__src->c.control.avail_min)) \ + __err =3D -EFAULT; \ + __err; \ +}) + +#define snd_pcm_sync_ptr_put_user(__s, __c, __ptr) ({ \ + int __err =3D 0; \ + typeof(*(__ptr)) __user *__src =3D (__ptr); \ + \ + if (put_user(__s.state, &__src->s.status.state) || \ + put_user(__s.hw_ptr, &__src->s.status.hw_ptr) || \ + put_user(__s.tstamp.tv_sec, &__src->s.status.tstamp_sec) || \ + put_user(__s.tstamp.tv_nsec, &__src->s.status.tstamp_nsec) || \ + put_user(__s.suspended_state, &__src->s.status.suspended_state) || \ + put_user(__s.audio_tstamp.tv_sec, &__src->s.status.audio_tstamp_sec) = || \ + put_user(__s.audio_tstamp.tv_nsec, &__src->s.status.audio_tstamp_nsec= ) || \ + put_user(__c.appl_ptr, &__src->c.control.appl_ptr) || \ + put_user(__c.avail_min, &__src->c.control.avail_min)) \ + __err =3D -EFAULT; \ + __err; \ +}) + static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream, struct snd_pcm_sync_ptr __user *_sync_ptr) { @@ -3165,9 +3193,7 @@ static int snd_pcm_ioctl_sync_ptr_compat(struct snd_p= cm_substream *substream, if (snd_BUG_ON(!runtime)) return -EINVAL; =20 - if (get_user(sflags, &src->flags) || - get_user(scontrol.appl_ptr, &src->c.control.appl_ptr) || - get_user(scontrol.avail_min, &src->c.control.avail_min)) + if (snd_pcm_sync_ptr_get_user(sflags, scontrol, src)) return -EFAULT; if (sflags & SNDRV_PCM_SYNC_PTR_HWSYNC) { err =3D snd_pcm_hwsync(substream); @@ -3200,15 +3226,7 @@ static int snd_pcm_ioctl_sync_ptr_compat(struct snd_= pcm_substream *substream, } if (!(sflags & SNDRV_PCM_SYNC_PTR_APPL)) snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE); - if (put_user(sstatus.state, &src->s.status.state) || - put_user(sstatus.hw_ptr, &src->s.status.hw_ptr) || - put_user(sstatus.tstamp.tv_sec, &src->s.status.tstamp_sec) || - put_user(sstatus.tstamp.tv_nsec, &src->s.status.tstamp_nsec) || - put_user(sstatus.suspended_state, &src->s.status.suspended_state) || - put_user(sstatus.audio_tstamp.tv_sec, &src->s.status.audio_tstamp_sec= ) || - put_user(sstatus.audio_tstamp.tv_nsec, &src->s.status.audio_tstamp_ns= ec) || - put_user(scontrol.appl_ptr, &src->c.control.appl_ptr) || - put_user(scontrol.avail_min, &src->c.control.avail_min)) + if (snd_pcm_sync_ptr_put_user(sstatus, scontrol, src)) return -EFAULT; =20 return 0; --=20 2.47.0 From nobody Fri Oct 10 09:15:00 2025 Received: from pegase1.c-s.fr (pegase1.c-s.fr [93.17.236.30]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9E2E21E8324; Sat, 14 Jun 2025 06:50:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=93.17.236.30 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749883849; cv=none; b=pgCGkAcrHL9odjHoMCqUWro9vQPM0e8QwfPoD4GxV6znrpv04sqAJwLxFL/5SAvHIGTJndwhKg5U+1D4cd8n354Jz/p7NFwFC+7W9eLIc8S12GMjjJY+UWhtetFizsYuabFWJf6NJva0Fk+orq9MFj4Klq+kqLx0Cv8Y6vyU/f0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749883849; c=relaxed/simple; bh=Y/kN7AHu0ztdE4V/wDe2tzWqBJMOAaU7CIWYK7DkX7o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KA8+drgDQMDmSsonbBsSEk2n49XTvEyJKurq13iGvsU/+zwPsGjsgy8PslYL83psf480uO3MllstntYgPRvrt/6XDRx83SCe7MAjW93h+aIpReUXM8RjlnH3rcElPpQUpbMKKzpe9SjGK+ERWY34iyJD6gvCh4D6PvvlCk0sTVA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=csgroup.eu; spf=pass smtp.mailfrom=csgroup.eu; arc=none smtp.client-ip=93.17.236.30 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=csgroup.eu Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=csgroup.eu Received: from localhost (mailhub3.si.c-s.fr [192.168.12.233]) by localhost (Postfix) with ESMTP id 4bK6Cw1Krmz9swJ; Sat, 14 Jun 2025 08:43:24 +0200 (CEST) X-Virus-Scanned: amavisd-new at c-s.fr Received: from pegase1.c-s.fr ([192.168.12.234]) by localhost (pegase1.c-s.fr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id C2dHx2Qexyif; Sat, 14 Jun 2025 08:43:24 +0200 (CEST) Received: from messagerie.si.c-s.fr (messagerie.si.c-s.fr [192.168.25.192]) by pegase1.c-s.fr (Postfix) with ESMTP id 4bK6Cw0dHyz9sv8; Sat, 14 Jun 2025 08:43:24 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by messagerie.si.c-s.fr (Postfix) with ESMTP id 115D18B77D; Sat, 14 Jun 2025 08:43:24 +0200 (CEST) X-Virus-Scanned: amavisd-new at c-s.fr Received: from messagerie.si.c-s.fr ([127.0.0.1]) by localhost (messagerie.si.c-s.fr [127.0.0.1]) (amavisd-new, port 10023) with ESMTP id EQJpklzh3xNX; Sat, 14 Jun 2025 08:43:23 +0200 (CEST) Received: from PO20335.idsi0.si.c-s.fr (unknown [192.168.235.99]) by messagerie.si.c-s.fr (Postfix) with ESMTP id 9A1198B77C; Sat, 14 Jun 2025 08:43:23 +0200 (CEST) From: Christophe Leroy To: Jaroslav Kysela , Takashi Iwai Cc: Christophe Leroy , linux-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-sound@vger.kernel.org, Herve Codina Subject: [PATCH v3 2/4] ALSA: pcm: Convert SNDRV_PCM_IOCTL_SYNC_PTR to user_access_begin/user_access_end() Date: Sat, 14 Jun 2025 08:43:15 +0200 Message-ID: X-Mailer: git-send-email 2.47.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1749883394; l=5748; i=christophe.leroy@csgroup.eu; s=20211009; h=from:subject:message-id; bh=Y/kN7AHu0ztdE4V/wDe2tzWqBJMOAaU7CIWYK7DkX7o=; b=pRAXSC+yAW3CkHcY/K2ThDMLODoldV3IMLvVRH3SQ4RQRIX816uDsMCq9mbDWlil21VwolnQ7 CJb9Ha9TCYpANSPVCtVsMF720vVqehwz1RIlMAzUW9PBVY7/6qXGnha X-Developer-Key: i=christophe.leroy@csgroup.eu; a=ed25519; pk=HIzTzUj91asvincQGOFx6+ZF5AoUuP9GdOtQChs7Mm0= Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" With user access protection (Called SMAP on x86 or KUAP on powerpc) each and every call to get_user() or put_user() performs heavy operations to unlock and lock kernel access to userspace. SNDRV_PCM_IOCTL_SYNC_PTR is a hot path which is called really often and needs to run as fast as possible. To improve performance, perform user accesses by blocks using user_access_begin/user_access_end() and unsafe_get_user()/ unsafe_put_user(). Before the patch the 9 calls to put_user() at the end of snd_pcm_ioctl_sync_ptr_compat() imply the following set of instructions about 9 times (access_ok - enable user - write - disable user): 0.00 : c057f858: 3d 20 7f ff lis r9,32767 0.29 : c057f85c: 39 5e 00 14 addi r10,r30,20 0.77 : c057f860: 61 29 ff fc ori r9,r9,65532 0.32 : c057f864: 7c 0a 48 40 cmplw r10,r9 0.36 : c057f868: 41 a1 fb 58 bgt c057f3c0 0.30 : c057f86c: 3d 20 dc 00 lis r9,-9216 1.95 : c057f870: 7d 3a c3 a6 mtspr 794,r9 0.33 : c057f874: 92 8a 00 00 stw r20,0(r10) 0.27 : c057f878: 3d 20 de 00 lis r9,-8704 0.28 : c057f87c: 7d 3a c3 a6 mtspr 794,r9 ... A perf profile shows that in total the 9 put_user() represent 36% of the time spent in snd_pcm_ioctl() and about 80 instructions. With this patch everything is done in 13 instructions and represent only 15% of the time spent in snd_pcm_ioctl(): 0.57 : c057f5dc: 3d 20 dc 00 lis r9,-9216 0.98 : c057f5e0: 7d 3a c3 a6 mtspr 794,r9 0.16 : c057f5e4: 92 7f 00 04 stw r19,4(r31) 0.63 : c057f5e8: 93 df 00 0c stw r30,12(r31) 0.16 : c057f5ec: 93 9f 00 10 stw r28,16(r31) 4.95 : c057f5f0: 92 9f 00 14 stw r20,20(r31) 0.19 : c057f5f4: 92 5f 00 18 stw r18,24(r31) 0.49 : c057f5f8: 92 bf 00 1c stw r21,28(r31) 0.27 : c057f5fc: 93 7f 00 20 stw r27,32(r31) 5.88 : c057f600: 93 36 00 00 stw r25,0(r22) 0.11 : c057f604: 93 17 00 00 stw r24,0(r23) 0.00 : c057f608: 3d 20 de 00 lis r9,-8704 0.79 : c057f60c: 7d 3a c3 a6 mtspr 794,r9 Note that here the access_ok() in user_write_access_begin() is skipped because the exact same verification has already been performed at the beginning of the fonction with the call to user_read_access_begin(). Signed-off-by: Christophe Leroy --- sound/core/pcm_native.c | 44 ++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c index 1f8f6d95b18c..5eb59fdb3cb2 100644 --- a/sound/core/pcm_native.c +++ b/sound/core/pcm_native.c @@ -3053,30 +3053,42 @@ static inline int snd_pcm_hwsync(struct snd_pcm_sub= stream *substream) } =20 #define snd_pcm_sync_ptr_get_user(__f, __c, __ptr) ({ \ - int __err =3D 0; \ + __label__ failed, failed_begin; \ + int __err =3D -EFAULT; \ typeof(*(__ptr)) __user *__src =3D (__ptr); \ \ - if (get_user(__f, &src->flags) || \ - get_user(__c.appl_ptr, &__src->c.control.appl_ptr) || \ - get_user(__c.avail_min, &__src->c.control.avail_min)) \ - __err =3D -EFAULT; \ + if (!user_read_access_begin(__src, sizeof(*__src))) \ + goto failed_begin; \ + unsafe_get_user(__f, &__src->flags, failed); \ + unsafe_get_user(__c.appl_ptr, &__src->c.control.appl_ptr, failed); \ + unsafe_get_user(__c.avail_min, &__src->c.control.avail_min, failed); \ + __err =3D 0; \ +failed: \ + user_read_access_end(); \ +failed_begin: \ __err; \ }) =20 #define snd_pcm_sync_ptr_put_user(__s, __c, __ptr) ({ \ - int __err =3D 0; \ + __label__ failed, failed_begin; \ + int __err =3D -EFAULT; \ typeof(*(__ptr)) __user *__src =3D (__ptr); \ \ - if (put_user(__s.state, &__src->s.status.state) || \ - put_user(__s.hw_ptr, &__src->s.status.hw_ptr) || \ - put_user(__s.tstamp.tv_sec, &__src->s.status.tstamp_sec) || \ - put_user(__s.tstamp.tv_nsec, &__src->s.status.tstamp_nsec) || \ - put_user(__s.suspended_state, &__src->s.status.suspended_state) || \ - put_user(__s.audio_tstamp.tv_sec, &__src->s.status.audio_tstamp_sec) = || \ - put_user(__s.audio_tstamp.tv_nsec, &__src->s.status.audio_tstamp_nsec= ) || \ - put_user(__c.appl_ptr, &__src->c.control.appl_ptr) || \ - put_user(__c.avail_min, &__src->c.control.avail_min)) \ - __err =3D -EFAULT; \ + if (!user_write_access_begin(__src, sizeof(*__src))) \ + goto failed_begin; \ + unsafe_put_user(__s.state, &__src->s.status.state, failed); \ + unsafe_put_user(__s.hw_ptr, &__src->s.status.hw_ptr, failed); \ + unsafe_put_user(__s.tstamp.tv_sec, &__src->s.status.tstamp_sec, failed);\ + unsafe_put_user(__s.tstamp.tv_nsec, &__src->s.status.tstamp_nsec, failed)= ; \ + unsafe_put_user(__s.suspended_state, &__src->s.status.suspended_state, fa= iled); \ + unsafe_put_user(__s.audio_tstamp.tv_sec, &__src->s.status.audio_tstamp_se= c, failed); \ + unsafe_put_user(__s.audio_tstamp.tv_nsec, &__src->s.status.audio_tstamp_n= sec, failed); \ + unsafe_put_user(__c.appl_ptr, &__src->c.control.appl_ptr, failed); \ + unsafe_put_user(__c.avail_min, &__src->c.control.avail_min, failed); \ + __err =3D 0; \ +failed: \ + user_write_access_end(); \ +failed_begin: \ __err; \ }) =20 --=20 2.47.0 From nobody Fri Oct 10 09:15:00 2025 Received: from pegase1.c-s.fr (pegase1.c-s.fr [93.17.236.30]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EB8F31F5827; Sat, 14 Jun 2025 06:50:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=93.17.236.30 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749883858; cv=none; b=GuH7fKQjRGAcLnu/nqFdkxT0ztSHIZaqHc547MiVPwd1pNQByuW2yQmfyxj7o5Bb26dKpjlipzLbv56XTATFOWQ3WOf/V8y6BPdB5JItbNt+RAxPCsQadWvKZBv+4sW3QWTV9Y38RiRy7DLqUSnfRJRE7WtU3WsF2txvsuFJDM0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749883858; c=relaxed/simple; bh=TxFfVJnfi/55ZEHmQBAQ766Sfb3w7ACIMtkxgoYmr8A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=o7UE+69PahGfQUIk18KHrzdV6oHX63LFnCr7ioHCTFScIzkJibhSu4EdA68F7lXu+FpmYY+XdhAjo4YEgLvhuF9ovuMjjY+ZqYAvmSv8FQCWYG4VWxdcSs2eGBolRzzaVDyzSmCnIyb92nUsJQWdidSttORINmVyTS0iks88NWQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=csgroup.eu; spf=pass smtp.mailfrom=csgroup.eu; arc=none smtp.client-ip=93.17.236.30 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=csgroup.eu Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=csgroup.eu Received: from localhost (mailhub3.si.c-s.fr [192.168.12.233]) by localhost (Postfix) with ESMTP id 4bK6Cx56syz9syd; Sat, 14 Jun 2025 08:43:25 +0200 (CEST) X-Virus-Scanned: amavisd-new at c-s.fr Received: from pegase1.c-s.fr ([192.168.12.234]) by localhost (pegase1.c-s.fr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id gnLr0TCqepJo; Sat, 14 Jun 2025 08:43:25 +0200 (CEST) Received: from messagerie.si.c-s.fr (messagerie.si.c-s.fr [192.168.25.192]) by pegase1.c-s.fr (Postfix) with ESMTP id 4bK6Cw3LbQz9sy4; Sat, 14 Jun 2025 08:43:24 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by messagerie.si.c-s.fr (Postfix) with ESMTP id 6E51E8B77C; Sat, 14 Jun 2025 08:43:24 +0200 (CEST) X-Virus-Scanned: amavisd-new at c-s.fr Received: from messagerie.si.c-s.fr ([127.0.0.1]) by localhost (messagerie.si.c-s.fr [127.0.0.1]) (amavisd-new, port 10023) with ESMTP id S16e25hkZocH; Sat, 14 Jun 2025 08:43:24 +0200 (CEST) Received: from PO20335.idsi0.si.c-s.fr (unknown [192.168.235.99]) by messagerie.si.c-s.fr (Postfix) with ESMTP id 08C1E8B77B; Sat, 14 Jun 2025 08:43:23 +0200 (CEST) From: Christophe Leroy To: Jaroslav Kysela , Takashi Iwai Cc: Christophe Leroy , linux-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-sound@vger.kernel.org, Herve Codina Subject: [PATCH v3 3/4] ALSA: pcm: Replace [audio_]tstamp_[n]sec by struct __snd_timespec in struct snd_pcm_mmap_status32 Date: Sat, 14 Jun 2025 08:43:16 +0200 Message-ID: X-Mailer: git-send-email 2.47.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1749883394; l=2417; i=christophe.leroy@csgroup.eu; s=20211009; h=from:subject:message-id; bh=TxFfVJnfi/55ZEHmQBAQ766Sfb3w7ACIMtkxgoYmr8A=; b=t+LDtHniwILr1P7MDKvZNUmWR4TFFNQ8gxzPAZ7sW8fkBY+hPO1Y2gzQqKJdo03xoJ3Dqstv+ tJizR/qauqBCnz3dQUZ11VHbV66cucQf46urLzATHQAsF/WqDd0lvjf X-Developer-Key: i=christophe.leroy@csgroup.eu; a=ed25519; pk=HIzTzUj91asvincQGOFx6+ZF5AoUuP9GdOtQChs7Mm0= Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" To match struct __snd_pcm_mmap_status and enable reuse of snd_pcm_sync_ptr_get_user() and snd_pcm_sync_ptr_put_user() by snd_pcm_sync_ptr() replace tstamp_sec and tstamp_nsec fields by a struct __snd_timespec in struct snd_pcm_mmap_status32. Do the same with audio_tstamp_sec and audio_tstamp_nsec. This is possible because struct snd_pcm_mmap_status32 is packed and __SND_STRUCT_TIME64 is always defined for kernel which means struct __snd_timespec is always defined as: struct __snd_timespec { __s32 tv_sec; __s32 tv_nsec; }; Signed-off-by: Christophe Leroy --- sound/core/pcm_native.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c index 5eb59fdb3cb2..b7339c9ebb1f 100644 --- a/sound/core/pcm_native.c +++ b/sound/core/pcm_native.c @@ -3078,11 +3078,11 @@ failed_begin: \ goto failed_begin; \ unsafe_put_user(__s.state, &__src->s.status.state, failed); \ unsafe_put_user(__s.hw_ptr, &__src->s.status.hw_ptr, failed); \ - unsafe_put_user(__s.tstamp.tv_sec, &__src->s.status.tstamp_sec, failed);\ - unsafe_put_user(__s.tstamp.tv_nsec, &__src->s.status.tstamp_nsec, failed)= ; \ + unsafe_put_user(__s.tstamp.tv_sec, &__src->s.status.tstamp.tv_sec, failed= ); \ + unsafe_put_user(__s.tstamp.tv_nsec, &__src->s.status.tstamp.tv_nsec, fail= ed); \ unsafe_put_user(__s.suspended_state, &__src->s.status.suspended_state, fa= iled); \ - unsafe_put_user(__s.audio_tstamp.tv_sec, &__src->s.status.audio_tstamp_se= c, failed); \ - unsafe_put_user(__s.audio_tstamp.tv_nsec, &__src->s.status.audio_tstamp_n= sec, failed); \ + unsafe_put_user(__s.audio_tstamp.tv_sec, &__src->s.status.audio_tstamp.tv= _sec, failed); \ + unsafe_put_user(__s.audio_tstamp.tv_nsec, &__src->s.status.audio_tstamp.t= v_nsec, failed);\ unsafe_put_user(__c.appl_ptr, &__src->c.control.appl_ptr, failed); \ unsafe_put_user(__c.avail_min, &__src->c.control.avail_min, failed); \ __err =3D 0; \ @@ -3143,11 +3143,9 @@ struct snd_pcm_mmap_status32 { snd_pcm_state_t state; s32 pad1; u32 hw_ptr; - s32 tstamp_sec; - s32 tstamp_nsec; + struct __snd_timespec tstamp; snd_pcm_state_t suspended_state; - s32 audio_tstamp_sec; - s32 audio_tstamp_nsec; + struct __snd_timespec audio_tstamp; } __packed; =20 struct snd_pcm_mmap_control32 { --=20 2.47.0 From nobody Fri Oct 10 09:15:00 2025 Received: from pegase1.c-s.fr (pegase1.c-s.fr [93.17.236.30]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5E0921EDA02; Sat, 14 Jun 2025 06:50:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=93.17.236.30 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749883854; cv=none; b=kvm4qqYWf70AlVgSOPDbR1riQwTfrM7ebAMAS38RjpJ6Q0EcHvGfX4b5z4xOCE/NvNWCqHeEgq1RLjpR6y6ZVr19FUIVIjJRlJ3MDqYfoYCGVKPRb5291rb87honVtq5BsPnrs5mD7kc5Ics3zXoLDsnYpdGsBhRKGlE2l14Wvk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749883854; c=relaxed/simple; bh=jgfZugwghp2JlIjkP+brwrtVHOqmSVyXqYoNE696IOg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rEM2agCzX5fPIuUo693508j70JjrM/MCEobGQAb5Lv0kug6wRxJJKlMgaSOFay25zNydXsyVpNRWuAkE1XKFMneRnkFRYxG3H3w5PWum14s3rPMz7jO3GRuwmT8xSZDiCOSrqDlLNJMcDd6z+Ve598oB90uBeOzFURNOdJj1ogI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=csgroup.eu; spf=pass smtp.mailfrom=csgroup.eu; arc=none smtp.client-ip=93.17.236.30 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=csgroup.eu Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=csgroup.eu Received: from localhost (mailhub3.si.c-s.fr [192.168.12.233]) by localhost (Postfix) with ESMTP id 4bK6Cy21Trz9syj; Sat, 14 Jun 2025 08:43:26 +0200 (CEST) X-Virus-Scanned: amavisd-new at c-s.fr Received: from pegase1.c-s.fr ([192.168.12.234]) by localhost (pegase1.c-s.fr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id A7-nfL4VEBL3; Sat, 14 Jun 2025 08:43:26 +0200 (CEST) Received: from messagerie.si.c-s.fr (messagerie.si.c-s.fr [192.168.25.192]) by pegase1.c-s.fr (Postfix) with ESMTP id 4bK6Cw6f1Lz9syQ; Sat, 14 Jun 2025 08:43:24 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by messagerie.si.c-s.fr (Postfix) with ESMTP id DF9808B77C; Sat, 14 Jun 2025 08:43:24 +0200 (CEST) X-Virus-Scanned: amavisd-new at c-s.fr Received: from messagerie.si.c-s.fr ([127.0.0.1]) by localhost (messagerie.si.c-s.fr [127.0.0.1]) (amavisd-new, port 10023) with ESMTP id IRFdjwNIQ8pl; Sat, 14 Jun 2025 08:43:24 +0200 (CEST) Received: from PO20335.idsi0.si.c-s.fr (unknown [192.168.235.99]) by messagerie.si.c-s.fr (Postfix) with ESMTP id 74BCB8B77B; Sat, 14 Jun 2025 08:43:24 +0200 (CEST) From: Christophe Leroy To: Jaroslav Kysela , Takashi Iwai Cc: Christophe Leroy , linux-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-sound@vger.kernel.org, Herve Codina Subject: [PATCH v3 4/4] ALSA: pcm: Convert snd_pcm_sync_ptr() to user_access_begin/user_access_end() Date: Sat, 14 Jun 2025 08:43:17 +0200 Message-ID: <6ce6bc4da498ea7ea2be5f279b374370b1613b13.1749883041.git.christophe.leroy@csgroup.eu> X-Mailer: git-send-email 2.47.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1749883394; l=3615; i=christophe.leroy@csgroup.eu; s=20211009; h=from:subject:message-id; bh=jgfZugwghp2JlIjkP+brwrtVHOqmSVyXqYoNE696IOg=; b=BrHy+Chki7RelxAKtji7NaCi7jwbSjSvpGKGmfSim8KyqAAv7OIf2MPyP/hkx4k7Se9m7a3CD z4DXP83zRYVDpUed2aN7gwZfe63dBK9DNNOyybLBgi3u5uqotuftqqU X-Developer-Key: i=christophe.leroy@csgroup.eu; a=ed25519; pk=HIzTzUj91asvincQGOFx6+ZF5AoUuP9GdOtQChs7Mm0= Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Now that snd_pcm_sync_ptr_get_user() and snd_pcm_sync_ptr_put_user() are converted to user_access_begin/user_access_end(), snd_pcm_sync_ptr_get_user() is more efficient than a raw get_user() followed by a copy_from_user(). And because copy_{to/from}_user() are generic functions focussed on transfer of big data blocks to/from user, snd_pcm_sync_ptr_put_user() is also more efficient for small amont of data. So use snd_pcm_sync_ptr_get_user() and snd_pcm_sync_ptr_put_user() in snd_pcm_sync_ptr() too. snd_pcm_ioctl_sync_ptr_buggy() is left as it is because the conversion wouldn't be straigh-forward due to the workaround it provides. Signed-off-by: Christophe Leroy --- sound/core/pcm_native.c | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c index b7339c9ebb1f..1eab940fa2e5 100644 --- a/sound/core/pcm_native.c +++ b/sound/core/pcm_native.c @@ -3096,45 +3096,43 @@ static int snd_pcm_sync_ptr(struct snd_pcm_substrea= m *substream, struct snd_pcm_sync_ptr __user *_sync_ptr) { struct snd_pcm_runtime *runtime =3D substream->runtime; - struct snd_pcm_sync_ptr sync_ptr; volatile struct snd_pcm_mmap_status *status; volatile struct snd_pcm_mmap_control *control; + u32 sflags; + struct snd_pcm_mmap_control scontrol; + struct snd_pcm_mmap_status sstatus; int err; =20 - memset(&sync_ptr, 0, sizeof(sync_ptr)); - if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags))) + if (snd_pcm_sync_ptr_get_user(sflags, scontrol, _sync_ptr)) return -EFAULT; - if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(s= truct snd_pcm_mmap_control))) - return -EFAULT;=09 status =3D runtime->status; control =3D runtime->control; - if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) { + if (sflags & SNDRV_PCM_SYNC_PTR_HWSYNC) { err =3D snd_pcm_hwsync(substream); if (err < 0) return err; } scoped_guard(pcm_stream_lock_irq, substream) { - if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL)) { - err =3D pcm_lib_apply_appl_ptr(substream, - sync_ptr.c.control.appl_ptr); + if (!(sflags & SNDRV_PCM_SYNC_PTR_APPL)) { + err =3D pcm_lib_apply_appl_ptr(substream, scontrol.appl_ptr); if (err < 0) return err; } else { - sync_ptr.c.control.appl_ptr =3D control->appl_ptr; + scontrol.appl_ptr =3D control->appl_ptr; } - if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN)) - control->avail_min =3D sync_ptr.c.control.avail_min; + if (!(sflags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN)) + control->avail_min =3D scontrol.avail_min; else - sync_ptr.c.control.avail_min =3D control->avail_min; - sync_ptr.s.status.state =3D status->state; - sync_ptr.s.status.hw_ptr =3D status->hw_ptr; - sync_ptr.s.status.tstamp =3D status->tstamp; - sync_ptr.s.status.suspended_state =3D status->suspended_state; - sync_ptr.s.status.audio_tstamp =3D status->audio_tstamp; + scontrol.avail_min =3D control->avail_min; + sstatus.state =3D status->state; + sstatus.hw_ptr =3D status->hw_ptr; + sstatus.tstamp =3D status->tstamp; + sstatus.suspended_state =3D status->suspended_state; + sstatus.audio_tstamp =3D status->audio_tstamp; } - if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL)) + if (!(sflags & SNDRV_PCM_SYNC_PTR_APPL)) snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE); - if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr))) + if (snd_pcm_sync_ptr_put_user(sstatus, scontrol, _sync_ptr)) return -EFAULT; return 0; } --=20 2.47.0