From: "Nuno Sá" <noname.nuno@gmail.com>
To: David Lechner <dlechner@baylibre.com>
Cc: "Mark Brown" <broonie@kernel.org>,
"Jonathan Cameron" <jic23@kernel.org>,
"Rob Herring" <robh@kernel.org>,
"Krzysztof Kozlowski" <krzk+dt@kernel.org>,
"Conor Dooley" <conor+dt@kernel.org>,
"Nuno Sá" <nuno.sa@analog.com>,
"Uwe Kleine-König" <ukleinek@kernel.org>,
"Michael Hennerich" <Michael.Hennerich@analog.com>,
"Lars-Peter Clausen" <lars@metafoo.de>,
"David Jander" <david@protonic.nl>,
"Martin Sperl" <kernel@martin.sperl.org>,
linux-spi@vger.kernel.org, devicetree@vger.kernel.org,
linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org,
linux-pwm@vger.kernel.org
Subject: Re: [PATCH RFC v4 11/15] iio: buffer-dmaengine: add devm_iio_dmaengine_buffer_setup_ext2()
Date: Fri, 25 Oct 2024 20:40:42 +0200 (GMT+02:00) [thread overview]
Message-ID: <dc52cda0-47d9-4cbf-a68e-0af304edc32e@gmail.com> (raw)
Oct 25, 2024 18:42:02 David Lechner <dlechner@baylibre.com>:
> On 10/25/24 8:24 AM, Nuno Sá wrote:
>> I still need to look better at this but I do have one though already
>> :)
>>
>> On Wed, 2024-10-23 at 15:59 -0500, David Lechner wrote:
>>> Add a new devm_iio_dmaengine_buffer_setup_ext2() function to handle
>>> cases where the DMA channel is managed by the caller rather than
>>> being
>>> requested and released by the iio_dmaengine module.
>>>
>>> Signed-off-by: David Lechner <dlechner@baylibre.com>
>>> ---
>>>
>>> v4 changes:
>>> * This replaces "iio: buffer-dmaengine: generalize requesting DMA
>>> channel"
>>> ---
>
> ...
>
>>> @@ -282,12 +281,38 @@ void iio_dmaengine_buffer_free(struct
>>> iio_buffer *buffer)
>>> iio_buffer_to_dmaengine_buffer(buffer);
>>>
>>> iio_dma_buffer_exit(&dmaengine_buffer->queue);
>>> - dma_release_channel(dmaengine_buffer->chan);
>>> -
>>> iio_buffer_put(buffer);
>>> +
>>> + if (dmaengine_buffer->owns_chan)
>>> + dma_release_channel(dmaengine_buffer->chan);
>>
>> Not sure if I agree much with this owns_chan flag. The way I see it,
>> we should always
>> handover the lifetime of the DMA channel to the IIO DMA framework.
>> Note that even the
>> device you pass in for both requesting the channel of the spi_offload
>> and for
>> setting up the DMA buffer is the same (and i suspect it will always
>> be) so I would
>> not go with the trouble. And with this assumption we could simplify a
>> bit more the
>> spi implementation.
>
> I tried something like this in v3 but Jonathan didn't seem to like it.
>
> https://lore.kernel.org/all/20240727144303.4a8604cb@jic23-huawei/
>
>>
>> And not even related but I even suspect the current implementation
>> could be
>> problematic. Basically I'm suspecting that the lifetime of the DMA
>> channel should be
>> attached to the lifetime of the iio_buffer. IOW, we should only
>> release the channel
>> in iio_dmaengine_buffer_release() - in which case the current
>> implementation with the
>> spi_offload would also be buggy.
>
> The buffer can outlive the iio device driver that created the buffer?
Yes, it can as the IIO device itself. In case a userspace app has an open
FD for the buffer chardev, we get a reference that is only released when
the FD is closed (which can outlive the device behind bound to its
driver). That is why we nullify indio_dev->info and check for it on the
read() and write() fops.
FWIW, I raised concerns about this in the past (as we don't have any lock
in those paths) but Jonathan rightfully wanted to see a real race. And I
was too lazy to try and reproduce one but I'm still fairly sure we have
theoretical (at least) races in those paths. And one of them could be (I
think) concurrently hitting a DMA submit block while the device is being
unbound. In that case the DMA chan would be already released and we could
still try to initiate a transfer. I did not check if that would crash or
something but it should still not happen.
- Nuno Sá
next reply other threads:[~2024-10-25 18:40 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-25 18:40 Nuno Sá [this message]
2024-10-26 15:48 ` [PATCH RFC v4 11/15] iio: buffer-dmaengine: add devm_iio_dmaengine_buffer_setup_ext2() Jonathan Cameron
2024-10-28 11:08 ` Nuno Sá
2024-10-28 16:35 ` Jonathan Cameron
-- strict thread matches above, loose matches on Subject: below --
2024-10-23 20:59 [PATCH RFC v4 00/15] spi: axi-spi-engine: add offload support David Lechner
2024-10-23 20:59 ` [PATCH RFC v4 11/15] iio: buffer-dmaengine: add devm_iio_dmaengine_buffer_setup_ext2() David Lechner
2024-10-25 13:24 ` Nuno Sá
2024-10-25 16:42 ` David Lechner
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=dc52cda0-47d9-4cbf-a68e-0af304edc32e@gmail.com \
--to=noname.nuno@gmail.com \
--cc=Michael.Hennerich@analog.com \
--cc=broonie@kernel.org \
--cc=conor+dt@kernel.org \
--cc=david@protonic.nl \
--cc=devicetree@vger.kernel.org \
--cc=dlechner@baylibre.com \
--cc=jic23@kernel.org \
--cc=kernel@martin.sperl.org \
--cc=krzk+dt@kernel.org \
--cc=lars@metafoo.de \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pwm@vger.kernel.org \
--cc=linux-spi@vger.kernel.org \
--cc=nuno.sa@analog.com \
--cc=robh@kernel.org \
--cc=ukleinek@kernel.org \
/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).