devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Dmitry Osipenko <digetx@gmail.com>
To: Akhil R <akhilrajeev@nvidia.com>,
	"dan.j.williams@intel.com" <dan.j.williams@intel.com>,
	"devicetree@vger.kernel.org" <devicetree@vger.kernel.org>,
	"dmaengine@vger.kernel.org" <dmaengine@vger.kernel.org>,
	Jonathan Hunter <jonathanh@nvidia.com>,
	Krishna Yarlagadda <kyarlagadda@nvidia.com>,
	Laxman Dewangan <ldewangan@nvidia.com>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"linux-tegra@vger.kernel.org" <linux-tegra@vger.kernel.org>,
	"p.zabel@pengutronix.de" <p.zabel@pengutronix.de>,
	Rajesh Gumasta <rgumasta@nvidia.com>,
	"robh+dt@kernel.org" <robh+dt@kernel.org>,
	"thierry.reding@gmail.com" <thierry.reding@gmail.com>,
	"vkoul@kernel.org" <vkoul@kernel.org>
Cc: Pavan Kunapuli <pkunapuli@nvidia.com>
Subject: Re: [PATCH v16 2/4] dmaengine: tegra: Add tegra gpcdma driver
Date: Fri, 28 Jan 2022 02:15:40 +0300	[thread overview]
Message-ID: <e23e82b1-4eed-57f0-8e7d-f8653e043f2a@gmail.com> (raw)
In-Reply-To: <DM5PR12MB1850677D5F07065BDC08EE24C0219@DM5PR12MB1850.namprd12.prod.outlook.com>

27.01.2022 19:29, Akhil R пишет:
>> 23.01.2022 19:49, Akhil R пишет:
>>>> 21.01.2022 19:24, Akhil R пишет:
>>>>>>>>>>> +static int tegra_dma_terminate_all(struct dma_chan *dc) {
>>>>>>>>>>> +     struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
>>>>>>>>>>> +     unsigned long flags;
>>>>>>>>>>> +     LIST_HEAD(head);
>>>>>>>>>>> +     int err;
>>>>>>>>>>> +
>>>>>>>>>>> +     if (tdc->dma_desc) {
>>>>>>>>>>
>>>>>>>>>> Needs locking protection against racing with the interrupt handler.
>>>>>>>>> tegra_dma_stop_client() waits for the in-flight transfer to
>>>>>>>>> complete and prevents any additional transfer to start.
>>>>>>>>> Wouldn't it manage the race? Do you see any potential issue there?
>>>>>>>>
>>>>>>>> You should consider interrupt handler like a process running in a
>>>>>>>> parallel thread. The interrupt handler sets tdc->dma_desc to NULL,
>>>>>>>> hence you'll get NULL dereference in tegra_dma_stop_client().
>>>>>>>
>>>>>>> Is it better if I remove the below part from tegra_dma_stop_client()
>>>>>>> so that dma_desc is not accessed at all?
>>>>>>>
>>>>>>> +     wcount = tdc_read(tdc, TEGRA_GPCDMA_CHAN_XFER_COUNT);
>>>>>>> +     tdc->dma_desc->bytes_transferred +=
>>>>>>> +                     tdc->dma_desc->bytes_requested - (wcount * 4);
>>>>>>>
>>>>>>> Because I don't see a point in updating the value there. dma_desc is
>>>>>>> set to NULL in the next step in terminate_all() anyway.
>>>>>>
>>>>>> That isn't going help you much because you also can't release DMA
>>>>>> descriptor while interrupt handler still may be running and using
>>>>>> that descriptor.
>>>>>
>>>>> Does the below functions look good to resolve the issue, provided
>>>>> tegra_dma_stop_client() doesn't access dma_desc?
>>>>
>>>> Stop shall not race with the start.
>>>>
>>>>> +static int tegra_dma_terminate_all(struct dma_chan *dc) {
>>>>> +       struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
>>>>> +       unsigned long flags;
>>>>> +       LIST_HEAD(head);
>>>>> +       int err;
>>>>> +
>>>>> +       err = tegra_dma_stop_client(tdc);
>>>>> +       if (err)
>>>>> +               return err;
>>>>> +
>>>>> +       tegra_dma_stop(tdc);
>>>>> +
>>>>> +       spin_lock_irqsave(&tdc->vc.lock, flags);
>>>>> +       tegra_dma_sid_free(tdc);
>>>>> +       tdc->dma_desc = NULL;
>>>>> +
>>>>> +       vchan_get_all_descriptors(&tdc->vc, &head);
>>>>> +       spin_unlock_irqrestore(&tdc->vc.lock, flags);
>>>>> +
>>>>> +       vchan_dma_desc_free_list(&tdc->vc, &head);
>>>>> +
>>>>> +       return 0;
>>>>> +}
>>>>>
>>>>> +static irqreturn_t tegra_dma_isr(int irq, void *dev_id) {
>>>>> +       struct tegra_dma_channel *tdc = dev_id;
>>>>> +       struct tegra_dma_desc *dma_desc = tdc->dma_desc;
>>>>> +       struct tegra_dma_sg_req *sg_req;
>>>>> +       u32 status;
>>>>> +
>>>>> +       /* Check channel error status register */
>>>>> +       status = tdc_read(tdc, TEGRA_GPCDMA_CHAN_ERR_STATUS);
>>>>> +       if (status) {
>>>>> +               tegra_dma_chan_decode_error(tdc, status);
>>>>> +               tegra_dma_dump_chan_regs(tdc);
>>>>> +               tdc_write(tdc, TEGRA_GPCDMA_CHAN_ERR_STATUS, 0xFFFFFFFF);
>>>>> +       }
>>>>> +
>>>>> +       status = tdc_read(tdc, TEGRA_GPCDMA_CHAN_STATUS);
>>>>> +       if (!(status & TEGRA_GPCDMA_STATUS_ISE_EOC))
>>>>> +               return IRQ_HANDLED;
>>>>> +
>>>>> +       tdc_write(tdc, TEGRA_GPCDMA_CHAN_STATUS,
>>>>> +                 TEGRA_GPCDMA_STATUS_ISE_EOC);
>>>>> +
>>>>> +       spin_lock(&tdc->vc.lock);
>>>>> +       if (!dma_desc)
>>>> All checks and assignments must be done inside of critical section.
>>>
>>> Okay. So, the lock should be held throughout the function.
>>> Do you think tegra_dma_pause should also hold a lock
>>> and remove irq_synchronize? That function also writes
>>> to CSR register.
>>
>> Interrupt handler shall not unpause channel in a case of race condition,
>> it should handle completed transfer and check whether channel is paused
>> before issuing next transfer.
>> So yes, pause also needs a lock.
> Thanks for the points. I collected more details on this scenario and found that
> DMA can only be paused if there is an in-flight transfer (i.e no interrupt will be
> pending). And there cannot be an interrupt after DMA is paused. Hence, there
> is no case of ISR unpausing a paused transfer. So, I guess, the check or a lock
> might not be required there.

If there is in-flight transfer, then interrupt may fire at any time. All
CSRE modifications must be protected.

> One thing I am thinking of is to wait for DMA to be busy so that it can be paused.
> I would add these changes and send out a new patch.

You should assume that interrupt handler may run with a delay. Then
pause() will see that channel isn't busy, but delayed interrupt will
start the next transfer.

Also, the tegra_dma_pause() will crash if tdc->dma_desc=NULL.

Everything must be done under lock.

  reply	other threads:[~2022-01-27 23:15 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-10 16:05 [PATCH v16 0/4] Add NVIDIA Tegra GPC-DMA driver Akhil R
2022-01-10 16:05 ` [PATCH v16 1/4] dt-bindings: dmaengine: Add doc for tegra gpcdma Akhil R
2022-01-10 16:05 ` [PATCH v16 2/4] dmaengine: tegra: Add tegra gpcdma driver Akhil R
2022-01-14 11:57   ` Dmitry Osipenko
2022-01-17  7:02     ` Akhil R
2022-01-17 15:41       ` Dmitry Osipenko
2022-01-18  5:36         ` Akhil R
2022-01-18 13:48           ` Dmitry Osipenko
2022-01-21 16:24             ` Akhil R
2022-01-22 18:31               ` Dmitry Osipenko
2022-01-23 16:49                 ` Akhil R
2022-01-23 21:07                   ` Dmitry Osipenko
2022-01-27 16:29                     ` Akhil R
2022-01-27 23:15                       ` Dmitry Osipenko [this message]
2022-01-10 16:05 ` [PATCH v16 3/4] arm64: defconfig: tegra: Enable GPCDMA Akhil R
2022-01-10 16:05 ` [PATCH v16 4/4] arm64: tegra: Add GPCDMA node for tegra186 and tegra194 Akhil R

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=e23e82b1-4eed-57f0-8e7d-f8653e043f2a@gmail.com \
    --to=digetx@gmail.com \
    --cc=akhilrajeev@nvidia.com \
    --cc=dan.j.williams@intel.com \
    --cc=devicetree@vger.kernel.org \
    --cc=dmaengine@vger.kernel.org \
    --cc=jonathanh@nvidia.com \
    --cc=kyarlagadda@nvidia.com \
    --cc=ldewangan@nvidia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-tegra@vger.kernel.org \
    --cc=p.zabel@pengutronix.de \
    --cc=pkunapuli@nvidia.com \
    --cc=rgumasta@nvidia.com \
    --cc=robh+dt@kernel.org \
    --cc=thierry.reding@gmail.com \
    --cc=vkoul@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).