Received: by 2002:a05:7412:f690:b0:e2:908c:2ebd with SMTP id ej16csp951802rdb; Fri, 20 Oct 2023 04:34:18 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFeU7CoVDDPH65xi3wDQGYIHq1hji0IT99Kb3Y9jSNZYnItGG2/oJ2I+LzM9Q81oRXOali2 X-Received: by 2002:a05:6830:4492:b0:6b9:8feb:8337 with SMTP id r18-20020a056830449200b006b98feb8337mr1630335otv.9.1697801657858; Fri, 20 Oct 2023 04:34:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697801657; cv=none; d=google.com; s=arc-20160816; b=uXYnUjNTMmdNVWmJAz9YhioZ66zoQSPC9S1GPBmMUmeQRn3DL+zrzb/SwfSdbdDa3J PCwoAUCX/eBIC3N5d805NitkFwfz8KskmKI6m9hiiv96RD5pP5EnXL4hd7LGN09nF1td yvfzW3gxdogbY5fGQTIGoXhHnS/j3gshq3UU2PppBpAWeZKlfep7jVxyrCaOSFPKu1Lv HIQC0QgDp2FRn4nbpr9yzC7Aw25J+n4rzlylWURXGjY1gCPfcBefxj/a7XvwFJHuj1QH Zs3Dzbx8bcHlE7/pWfpie2hFWKoSaC8sGXIwm8i74DKF/QqTzGQ6zWqgJ+/hEIG2C0WU NQxQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:references:in-reply-to :subject:cc:to:from:message-id:date:dkim-signature:dkim-signature; bh=MkbZEfNBeAP+RACBjl1lGBgR8ZTNoYn92dc0ZQl2j5s=; fh=E5TfskHzWQBGqqwt6S4tDwBfllivB3C40qtkYHEMetA=; b=nES04uAWqaMRwi2cx+1NcqWA49EOVqhQmbLQVDYMW9mInbKRiUf6SXJwm+OW9sC0YY PmJ1/kaThICSfrkhXy7hELPm85fIzydMjv3RZXe8x1OUWei8BOX7SxN6zmS+jH+lZj+c hyK1akqt5r5qxwN1z5zQqOiBtTClI0xEa76kcIZwJuQYA0igg2cugfgbj2zEAKmkNIGz qLH4H8wRpyq+lfNI1askOJMsXkXeLLKE6SSr++wjPrCisg2YoMfY9JYupdk9+iFRSctb aypJz2QAaGB7HbwhyeT7L7N0CLXN6eMJANpF01U8m1MGbhseIXu6qSjGMqRKZO1JTHgI vpaA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=K1DyiuZi; dkim=neutral (no key) header.i=@suse.de header.b=YWdHNnww; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Return-Path: Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id e24-20020a633718000000b00563d9ff5158si1659370pga.350.2023.10.20.04.34.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Oct 2023 04:34:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=K1DyiuZi; dkim=neutral (no key) header.i=@suse.de header.b=YWdHNnww; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 8EAC082EA16F; Fri, 20 Oct 2023 04:34:14 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377070AbjJTLd4 (ORCPT + 99 others); Fri, 20 Oct 2023 07:33:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42512 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377053AbjJTLdz (ORCPT ); Fri, 20 Oct 2023 07:33:55 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 457FC91 for ; Fri, 20 Oct 2023 04:33:53 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 526E71F750; Fri, 20 Oct 2023 11:33:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1697801631; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=MkbZEfNBeAP+RACBjl1lGBgR8ZTNoYn92dc0ZQl2j5s=; b=K1DyiuZigN78LZ2BF2lo5AowU/suftWqtebI9it8m34uIxBONGv0QViqcySBw7sOgqYIOe YJCnlnWwS/G/6Z9YOvYMC2Sk18KgdpI1hHEOaUweirp8f/gN2inXwN6l+Jms6ykQ+mcCyP 71iUcMrxbUA4E1Ig8RHw7ybxznTkI4E= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1697801631; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=MkbZEfNBeAP+RACBjl1lGBgR8ZTNoYn92dc0ZQl2j5s=; b=YWdHNnwwYQT2UZ7mocvFZf1KQloIMdaSAkhISsY74Su8jkl/ybUqCkODYYCh7iwDlZperd ktorCk7gdeALBfCw== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 0532313584; Fri, 20 Oct 2023 11:33:51 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 5685AJ9lMmUfcAAAMHmgww (envelope-from ); Fri, 20 Oct 2023 11:33:51 +0000 Date: Fri, 20 Oct 2023 13:33:50 +0200 Message-ID: <87r0lpjzoh.wl-tiwai@suse.de> From: Takashi Iwai To: Matias Ezequiel Vara Larsen Cc: Anton Yakovlev , mst@redhat.com, perex@perex.cz, tiwai@suse.com, virtualization@lists.linux-foundation.org, alsa-devel@alsa-project.org, linux-kernel@vger.kernel.org, pbonzini@redhat.com, stefanha@redhat.com, sgarzare@redhat.com, manos.pitsidianakis@linaro.org, mripard@redhat.com Subject: Re: [PATCH v2] ALSA: virtio: use copy and fill_silence callbacks In-Reply-To: References: <871qdrn6sg.wl-tiwai@suse.de> <87y1fzkq8c.wl-tiwai@suse.de> User-Agent: Wanderlust/2.15.9 (Almost Unreal) Emacs/27.2 Mule/6.0 MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII Authentication-Results: smtp-out2.suse.de; none X-Spam-Level: X-Spam-Score: -10.10 X-Spamd-Result: default: False [-10.10 / 50.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; NEURAL_HAM_LONG(-3.00)[-1.000]; MIME_GOOD(-0.10)[text/plain]; REPLY(-4.00)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; NEURAL_HAM_SHORT(-1.00)[-1.000]; RCPT_COUNT_TWELVE(0.00)[13]; MID_CONTAINS_FROM(1.00)[]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2]; RCVD_TLS_ALL(0.00)[]; BAYES_HAM(-3.00)[100.00%] X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Fri, 20 Oct 2023 04:34:14 -0700 (PDT) On Fri, 20 Oct 2023 11:45:39 +0200, Matias Ezequiel Vara Larsen wrote: > > Hello Takashi, > > On Thu, Oct 19, 2023 at 09:48:03AM +0200, Takashi Iwai wrote: > > On Thu, 19 Oct 2023 03:20:19 +0200, > > Anton Yakovlev wrote: > > > > > > Hi Takashi, > > > > > > On 19.10.2023 03:07, Takashi Iwai wrote: > > > > On Wed, 18 Oct 2023 12:48:23 +0200, > > > > Matias Ezequiel Vara Larsen wrote: > > > >> > > > >> This commit replaces the mmap mechanism with the copy() and > > > >> fill_silence() callbacks for both capturing and playback for the > > > >> virtio-sound driver. This change is required to prevent the updating of > > > >> the content of a buffer that is already in the available ring. > > > >> > > > >> The current mechanism splits a dma buffer into descriptors that are > > > >> exposed to the device. This dma buffer is shared with the user > > > >> application. When the device consumes a buffer, the driver moves the > > > >> request from the used ring to available ring. > > > >> > > > >> The driver exposes the buffer to the device without knowing if the > > > >> content has been updated from the user. The section 2.8.21.1 of the > > > >> virtio spec states that: "The device MAY access the descriptor chains > > > >> the driver created and the memory they refer to immediately". If the > > > >> device picks up buffers from the available ring just after it is > > > >> notified, it happens that the content may be old. > > > >> > > > >> By providing the copy() callback, the driver first updates the content > > > >> of the buffer, and then, exposes the buffer to the device by enqueuing > > > >> it in the available ring. Thus, device always picks up a buffer that is > > > >> updated. During copy(), the number of requests enqueued depends on the > > > >> "pos" and "bytes" arguments. The length of each request is period_size > > > >> bytes. > > > >> > > > >> For capturing, the driver starts by exposing all the available buffers > > > >> to device. After device updates the content of a buffer, it enqueues it > > > >> in the used ring. It is only after the copy() for capturing is issued > > > >> that the driver re-enqueues the buffer in the available ring. > > > >> > > > >> Co-developed-by: Anton Yakovlev > > > >> Signed-off-by: Matias Ezequiel Vara Larsen > > > >> --- > > > >> Changelog: > > > >> v1 -> v2: > > > >> * Use snd_pcm_set_managed_buffer_all()for buffer allocation/freeing. > > > >> * Make virtsnd_pcm_msg_send() generic by specifying the offset and size > > > >> for the modified part of the buffer; this way no assumptions need to > > > >> be made. > > > >> * Disable SNDRV_PCM_INFO_NO_REWINDS since now only sequential > > > >> reading/writing of frames is supported. > > > >> * Correct comment at virtsnd_pcm_msg_send(). > > > >> * v1 patch at: > > > >> https://ddec1-0-en-ctp.trendmicro.com:443/wis/clicktime/v1/query?url=https%3a%2f%2flore.kernel.org%2flkml%2f20231016151000.GE119987%40fedora%2ft%2f&umid=2f305b77-83e7-47b6-a461-a8ca67d0bfe2&auth=53c7c7de28b92dfd96e93d9dd61a23e634d2fbec-2d5775265e7e1741ae8eb783a3cb78ed553093c1 > > > >> > > > >> sound/virtio/virtio_pcm.c | 7 ++- > > > >> sound/virtio/virtio_pcm.h | 9 ++-- > > > >> sound/virtio/virtio_pcm_msg.c | 93 ++++++++++++++++++++++------------- > > > >> sound/virtio/virtio_pcm_ops.c | 81 +++++++++++++++++++++++++----- > > > >> 4 files changed, 137 insertions(+), 53 deletions(-) > > > > > > > > Most of the code changes look good, but I wonder: > > > > > > > >> > > > >> diff --git a/sound/virtio/virtio_pcm.c b/sound/virtio/virtio_pcm.c > > > >> index c10d91fff2fb..66d67eef1bcc 100644 > > > >> --- a/sound/virtio/virtio_pcm.c > > > >> +++ b/sound/virtio/virtio_pcm.c > > > >> @@ -104,12 +104,11 @@ static int virtsnd_pcm_build_hw(struct virtio_pcm_substream *vss, > > > >> * only message-based transport. > > > >> */ > > > >> vss->hw.info = > > > >> - SNDRV_PCM_INFO_MMAP | > > > >> - SNDRV_PCM_INFO_MMAP_VALID | > > > > > > > > Do we need the removal of those MMAP features inevitably? > > > > Usually mmap can still work even if the driver implements the copy > > > > ops. Those aren't always mutual exclusive. > > > > > > The driver uses a message queue to communicate with the device. Thus, > > > the audio buffer is sliced into several I/O requests (= number of > > > periods) of the same size (= period size). > > > > > > Before this, all such requests were enqueued when the substream started, > > > and immediately re-enqueued once the request is completed. This approach > > > made it possible to add mmap support. But for mmap there are no explicit > > > notifications from the application how many frames were written or read. > > > Thus, it was assumed that the virtual device should read/write frames to > > > requests based on timings. And there are some problems here: > > > > > > 1. This was found to violate the virtio specification: if a request is > > > already in the queue, the device can safely read/write there at any > > > time. > > > 2. It looks like this breaks the use case with swiotlb. Personally I'm > > > not sure how the application handles DMA ownership in the case of > > > mmaped buffer. > > > > > > To correctly implement mmap support, instead of transferring data via a > > > message queue, the driver and device must have a shared memory region. > > > We can add mmap in the future when we expand the functionality of the > > > device to support such shared memory. > > > > Ah, then this implementation might be an overkill. You're still using > > the (intermediate) vmalloc buffer allocated via PCM managed mode, and > > the actual data is copied from/to there. So it doesn't conflict with > > the mmap operation at all. > > > > I guess that the problem you're trying to solve (the immediate data > > transfer to the queue) can be implemented rather via PCM ack callback > > instead. ALSA PCM core notifies the possible data transfer via PCM > > ack callback right after each change of appl_ptr or hw_ptr, including > > each read/write op or mmap commit. Then the driver can check the > > change of appl_ptr (or hw_ptr for capture), fetch the newly available > > data, and queue it immediately. > > > > Usually together with the use of ack callback, the driver sets > > SNDRV_PCM_INFO_SYNC_APPLPTR flag. This prevents the mmap of the PCM > > control record (not the audio data) and enforces the use of > > SNDRV_PCM_IOCTL_SYNC_PTR ioctl instead (so that the driver always gets > > the ack callback). > > > > > > Thanks for your comments. If I understand correctly, we have two > options: > 1. Use copy/fill_silence callbacks and use my own buffers thus disabling > mmap. > 2. Use mmap and the ack callback to track when appl_ptr changes thus > moving the content to the queues after it has been updated. > > Am I right? Not really. The ack callback is always effective no matter whether the stream is transferred via mmap or read/write. So, when you implement a la packet transfer via ack callback, it works for both mmap and read/write. It's used typically for drivers doing a packet-based transfer, e.g. FireWire drivers or USB-audio. OTOH, if you'd like to drop the intermediate ring buffer (enabled by snd_pcm_set_managed_buffer_all() and allocated/freed automatically at hw_params/hw_free in PCM core), you can implement PCM copy and fill_ops methods. But in this case, you can't copy to runtime->dma_area like your patch; you may need a temporary buffer of your own, or handle directly the user pointer. And, even with copy/fill_ops implementations, mmap itself isn't prohibited when the driver provides its own mmap call. This can be seen, for example, when a driver uses the iomem region as the data ring buffer. HTH, Takashi > Thanks, Matias. > > > thanks, > > > > Takashi > > > > > > > > > > > > > Best regards, > > > > > > > > > > > > > > > thanks, > > > > > > > > Takashi > > > > > > -- > > > Anton Yakovlev > > > Senior Software Engineer > > > > > > OpenSynergy GmbH > > > Rotherstr. 20, 10245 Berlin > > > > > >