linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: okaya@codeaurora.org (Sinan Kaya)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH] dmaengine: qcom_hidma: release the descriptor before the callback
Date: Fri, 15 Jul 2016 21:00:52 -0400	[thread overview]
Message-ID: <ccffcabf-1448-57a1-d18b-715cfd46e906@codeaurora.org> (raw)
In-Reply-To: <1468465076-27324-1-git-send-email-okaya@codeaurora.org>

Hi Vinod,

On 7/13/2016 10:57 PM, Sinan Kaya wrote:
> There is a race condition between data transfer callback and descriptor
> free code. The callback routine may decide to clear the resources even
> though the descriptor has not yet been freed.
> 
> Instead of calling the callback first and then releasing the memory,
> this code is changing the order to return the descriptor back to the
> free pool and then call the user provided callback.
> 
> Signed-off-by: Sinan Kaya <okaya@codeaurora.org>
> ---
>  drivers/dma/qcom/hidma.c | 26 +++++++++++++++-----------
>  1 file changed, 15 insertions(+), 11 deletions(-)
> 
> diff --git a/drivers/dma/qcom/hidma.c b/drivers/dma/qcom/hidma.c
> index 41b5c6d..c41696e 100644
> --- a/drivers/dma/qcom/hidma.c
> +++ b/drivers/dma/qcom/hidma.c
> @@ -111,6 +111,7 @@ static void hidma_process_completed(struct hidma_chan *mchan)
>  	struct dma_async_tx_descriptor *desc;
>  	dma_cookie_t last_cookie;
>  	struct hidma_desc *mdesc;
> +	struct hidma_desc *next;
>  	unsigned long irqflags;
>  	struct list_head list;
>  
> @@ -122,28 +123,31 @@ static void hidma_process_completed(struct hidma_chan *mchan)
>  	spin_unlock_irqrestore(&mchan->lock, irqflags);
>  
>  	/* Execute callbacks and run dependencies */
> -	list_for_each_entry(mdesc, &list, node) {
> -		enum dma_status llstat;
> +	list_for_each_entry_safe(mdesc, next, &list, node) {
> +		dma_async_tx_callback callback;
> +		void *param;
>  
>  		desc = &mdesc->desc;
>  
>  		spin_lock_irqsave(&mchan->lock, irqflags);
> -		dma_cookie_complete(desc);
> +		if (hidma_ll_status(mdma->lldev, mdesc->tre_ch)
> +			== DMA_COMPLETE)
> +			dma_cookie_complete(desc);

It looks like I introduced a behavioral change while refactoring the code.
The previous one would call the callback only if the transfer was successful
but it would always call dma_cookie_complete.

The new behavior is to call dma_cookie_complete only if the transfer is successful
and it calls the callback even in the case of error cases. Then, the client has
to query if transfer was successful.

Which one is the correct behavior?


>  		spin_unlock_irqrestore(&mchan->lock, irqflags);
>  
> -		llstat = hidma_ll_status(mdma->lldev, mdesc->tre_ch);
> -		if (desc->callback && (llstat == DMA_COMPLETE))
> -			desc->callback(desc->callback_param);
> +		callback = desc->callback;
> +		param = desc->callback_param;
>  
>  		last_cookie = desc->cookie;
>  		dma_run_dependencies(desc);
> -	}
>  
> -	/* Free descriptors */
> -	spin_lock_irqsave(&mchan->lock, irqflags);
> -	list_splice_tail_init(&list, &mchan->free);
> -	spin_unlock_irqrestore(&mchan->lock, irqflags);
> +		spin_lock_irqsave(&mchan->lock, irqflags);
> +		list_move(&mdesc->node, &mchan->free);
> +		spin_unlock_irqrestore(&mchan->lock, irqflags);
>  
> +		if (callback)
> +			callback(param);
> +	}
>  }
>  
>  /*
> 


-- 
Sinan Kaya
Qualcomm Datacenter Technologies, Inc. as an affiliate of Qualcomm Technologies, Inc.
Qualcomm Technologies, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project.

  reply	other threads:[~2016-07-16  1:00 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-07-14  2:57 [PATCH] dmaengine: qcom_hidma: release the descriptor before the callback Sinan Kaya
2016-07-16  1:00 ` Sinan Kaya [this message]
2016-07-24  6:24   ` Vinod Koul
2016-07-25 14:19     ` Sinan Kaya
2016-08-04 12:55       ` Vinod Koul
2016-08-04 14:17         ` Sinan Kaya
2016-08-04 14:40           ` Russell King - ARM Linux
2016-08-04 15:27             ` Sinan Kaya
2016-08-04 15:38               ` Russell King - ARM Linux
2016-08-04 15:59                 ` Lars-Peter Clausen
2016-08-08  9:08                   ` Vinod Koul
2016-08-08 12:25                     ` Lars-Peter Clausen
2016-08-10 17:23                       ` Vinod Koul
2016-08-04 16:08                 ` Sinan Kaya
2016-08-04 16:15                   ` Lars-Peter Clausen
2016-08-05  6:32                     ` Robert Jarzmik
2016-08-05  8:34                       ` Lars-Peter Clausen
2016-08-05 15:17                         ` Sinan Kaya
2016-08-08  9:02               ` Vinod Koul
2016-08-08 14:45                 ` Sinan Kaya
2016-08-10 17:28                   ` Vinod Koul
2016-08-10 17:31                     ` Sinan Kaya
2016-08-19  2:48                       ` Vinod Koul
2016-08-19  3:26                         ` Sinan Kaya
2016-08-19  3:42                           ` Vinod Koul
2016-08-19  3:48                             ` Sinan Kaya
2016-08-19  5:52                               ` Vinod Koul
2016-08-19 11:13                                 ` okaya at codeaurora.org
2016-08-19 17:02                                   ` Vinod Koul
2016-08-19 17:21                                     ` Sinan Kaya
2016-08-22  6:08                                       ` Vinod Koul
2016-08-22 13:27                                         ` Sinan Kaya
2016-08-22 17:00                                           ` Vinod Koul
2016-08-08  8:51           ` Vinod Koul
2016-08-08 12:10             ` okaya at codeaurora.org

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=ccffcabf-1448-57a1-d18b-715cfd46e906@codeaurora.org \
    --to=okaya@codeaurora.org \
    --cc=linux-arm-kernel@lists.infradead.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).