devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] soc: mediatek: cmdq: avoid racing condition with mutex
@ 2019-11-21  7:29 Bibby Hsieh
  2019-12-04  2:22 ` CK Hu
  2019-12-30  6:44 ` CK Hu
  0 siblings, 2 replies; 5+ messages in thread
From: Bibby Hsieh @ 2019-11-21  7:29 UTC (permalink / raw)
  To: Jassi Brar, Matthias Brugger, Rob Herring, CK HU
  Cc: devicetree, linux-kernel, linux-arm-kernel, linux-mediatek,
	srv_heupstream, Nicolas Boichat, Dennis-YC Hsieh, Houlong Wei,
	Bibby Hsieh

If cmdq client is multi thread user, racing will occur without mutex
protection. It will make the C message queued in mailbox's queue
always need D message's triggering.

Thread A		Thread B		  Thread C		Thread D...
-----------------------------------------------------------------------------------
mbox_send_message()
	send_data()
			mbox_send_message()
				*exit
						mbox_send_message()
							*exit
mbox_client_txdone()
	tx_tick()
			mbox_client_txdone()
				tx_tick()
						mbox_client_txdone()
							tx_tick()
msg_submit()
	send_data()
			msg_submit()
				*exit
						msg_submit()
							*exit
-----------------------------------------------------------------------------------

Signed-off-by: Bibby Hsieh <bibby.hsieh@mediatek.com>
---
 drivers/soc/mediatek/mtk-cmdq-helper.c | 3 +++
 include/linux/soc/mediatek/mtk-cmdq.h  | 1 +
 2 files changed, 4 insertions(+)

diff --git a/drivers/soc/mediatek/mtk-cmdq-helper.c b/drivers/soc/mediatek/mtk-cmdq-helper.c
index 9add0fd5fa6c..9e35e0beffaa 100644
--- a/drivers/soc/mediatek/mtk-cmdq-helper.c
+++ b/drivers/soc/mediatek/mtk-cmdq-helper.c
@@ -81,6 +81,7 @@ struct cmdq_client *cmdq_mbox_create(struct device *dev, int index, u32 timeout)
 	client->client.dev = dev;
 	client->client.tx_block = false;
 	client->chan = mbox_request_channel(&client->client, index);
+	mutex_init(&client->mutex);
 
 	if (IS_ERR(client->chan)) {
 		long err;
@@ -352,9 +353,11 @@ int cmdq_pkt_flush_async(struct cmdq_pkt *pkt, cmdq_async_flush_cb cb,
 		spin_unlock_irqrestore(&client->lock, flags);
 	}
 
+	mutex_lock(&client->mutex);
 	mbox_send_message(client->chan, pkt);
 	/* We can send next packet immediately, so just call txdone. */
 	mbox_client_txdone(client->chan, 0);
+	mutex_unlock(&client->mutex);
 
 	return 0;
 }
diff --git a/include/linux/soc/mediatek/mtk-cmdq.h b/include/linux/soc/mediatek/mtk-cmdq.h
index a74c1d5acdf3..0f9071cd1bc7 100644
--- a/include/linux/soc/mediatek/mtk-cmdq.h
+++ b/include/linux/soc/mediatek/mtk-cmdq.h
@@ -28,6 +28,7 @@ struct cmdq_client {
 	struct mbox_chan *chan;
 	struct timer_list timer;
 	u32 timeout_ms; /* in unit of microsecond */
+	struct mutex mutex;
 };
 
 /**
-- 
2.18.0

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] soc: mediatek: cmdq: avoid racing condition with mutex
  2019-11-21  7:29 [PATCH] soc: mediatek: cmdq: avoid racing condition with mutex Bibby Hsieh
@ 2019-12-04  2:22 ` CK Hu
  2019-12-30  2:02   ` CK Hu
  2019-12-30  3:28   ` Jassi Brar
  2019-12-30  6:44 ` CK Hu
  1 sibling, 2 replies; 5+ messages in thread
From: CK Hu @ 2019-12-04  2:22 UTC (permalink / raw)
  To: Bibby Hsieh, Jassi Brar
  Cc: Matthias Brugger, Rob Herring, devicetree, linux-kernel,
	linux-arm-kernel, linux-mediatek, srv_heupstream,
	Nicolas Boichat, Dennis-YC Hsieh, Houlong Wei

Hi, Jassi:

Are mbox_send_message() and mbox_client_txdone() thread-safe? If these
two are thread-safe, this bug should be fixed in mailbox core not
client.

Regards,
CK

On Thu, 2019-11-21 at 15:29 +0800, Bibby Hsieh wrote:
> If cmdq client is multi thread user, racing will occur without mutex
> protection. It will make the C message queued in mailbox's queue
> always need D message's triggering.
> 
> Thread A		Thread B		  Thread C		Thread D...
> -----------------------------------------------------------------------------------
> mbox_send_message()
> 	send_data()
> 			mbox_send_message()
> 				*exit
> 						mbox_send_message()
> 							*exit
> mbox_client_txdone()
> 	tx_tick()
> 			mbox_client_txdone()
> 				tx_tick()
> 						mbox_client_txdone()
> 							tx_tick()
> msg_submit()
> 	send_data()
> 			msg_submit()
> 				*exit
> 						msg_submit()
> 							*exit
> -----------------------------------------------------------------------------------
> 
> Signed-off-by: Bibby Hsieh <bibby.hsieh@mediatek.com>
> ---
>  drivers/soc/mediatek/mtk-cmdq-helper.c | 3 +++
>  include/linux/soc/mediatek/mtk-cmdq.h  | 1 +
>  2 files changed, 4 insertions(+)
> 
> diff --git a/drivers/soc/mediatek/mtk-cmdq-helper.c b/drivers/soc/mediatek/mtk-cmdq-helper.c
> index 9add0fd5fa6c..9e35e0beffaa 100644
> --- a/drivers/soc/mediatek/mtk-cmdq-helper.c
> +++ b/drivers/soc/mediatek/mtk-cmdq-helper.c
> @@ -81,6 +81,7 @@ struct cmdq_client *cmdq_mbox_create(struct device *dev, int index, u32 timeout)
>  	client->client.dev = dev;
>  	client->client.tx_block = false;
>  	client->chan = mbox_request_channel(&client->client, index);
> +	mutex_init(&client->mutex);
>  
>  	if (IS_ERR(client->chan)) {
>  		long err;
> @@ -352,9 +353,11 @@ int cmdq_pkt_flush_async(struct cmdq_pkt *pkt, cmdq_async_flush_cb cb,
>  		spin_unlock_irqrestore(&client->lock, flags);
>  	}
>  
> +	mutex_lock(&client->mutex);
>  	mbox_send_message(client->chan, pkt);
>  	/* We can send next packet immediately, so just call txdone. */
>  	mbox_client_txdone(client->chan, 0);
> +	mutex_unlock(&client->mutex);
>  
>  	return 0;
>  }
> diff --git a/include/linux/soc/mediatek/mtk-cmdq.h b/include/linux/soc/mediatek/mtk-cmdq.h
> index a74c1d5acdf3..0f9071cd1bc7 100644
> --- a/include/linux/soc/mediatek/mtk-cmdq.h
> +++ b/include/linux/soc/mediatek/mtk-cmdq.h
> @@ -28,6 +28,7 @@ struct cmdq_client {
>  	struct mbox_chan *chan;
>  	struct timer_list timer;
>  	u32 timeout_ms; /* in unit of microsecond */
> +	struct mutex mutex;
>  };
>  
>  /**


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] soc: mediatek: cmdq: avoid racing condition with mutex
  2019-12-04  2:22 ` CK Hu
@ 2019-12-30  2:02   ` CK Hu
  2019-12-30  3:28   ` Jassi Brar
  1 sibling, 0 replies; 5+ messages in thread
From: CK Hu @ 2019-12-30  2:02 UTC (permalink / raw)
  To: Bibby Hsieh
  Cc: Jassi Brar, Matthias Brugger, Rob Herring, devicetree,
	linux-kernel, linux-arm-kernel, linux-mediatek, srv_heupstream,
	Nicolas Boichat, Dennis-YC Hsieh, Houlong Wei

Hi, Jassi:

Ping again.

Are mbox_send_message() and mbox_client_txdone() thread-safe? If these
two are thread-safe, this bug should be fixed in mailbox core not
client.

Regards,
CK

On Wed, 2019-12-04 at 10:22 +0800, CK Hu wrote:
> Hi, Jassi:
> 
> Are mbox_send_message() and mbox_client_txdone() thread-safe? If these
> two are thread-safe, this bug should be fixed in mailbox core not
> client.
> 
> Regards,
> CK
> 
> On Thu, 2019-11-21 at 15:29 +0800, Bibby Hsieh wrote:
> > If cmdq client is multi thread user, racing will occur without mutex
> > protection. It will make the C message queued in mailbox's queue
> > always need D message's triggering.
> > 
> > Thread A		Thread B		  Thread C		Thread D...
> > -----------------------------------------------------------------------------------
> > mbox_send_message()
> > 	send_data()
> > 			mbox_send_message()
> > 				*exit
> > 						mbox_send_message()
> > 							*exit
> > mbox_client_txdone()
> > 	tx_tick()
> > 			mbox_client_txdone()
> > 				tx_tick()
> > 						mbox_client_txdone()
> > 							tx_tick()
> > msg_submit()
> > 	send_data()
> > 			msg_submit()
> > 				*exit
> > 						msg_submit()
> > 							*exit
> > -----------------------------------------------------------------------------------
> > 
> > Signed-off-by: Bibby Hsieh <bibby.hsieh@mediatek.com>
> > ---
> >  drivers/soc/mediatek/mtk-cmdq-helper.c | 3 +++
> >  include/linux/soc/mediatek/mtk-cmdq.h  | 1 +
> >  2 files changed, 4 insertions(+)
> > 
> > diff --git a/drivers/soc/mediatek/mtk-cmdq-helper.c b/drivers/soc/mediatek/mtk-cmdq-helper.c
> > index 9add0fd5fa6c..9e35e0beffaa 100644
> > --- a/drivers/soc/mediatek/mtk-cmdq-helper.c
> > +++ b/drivers/soc/mediatek/mtk-cmdq-helper.c
> > @@ -81,6 +81,7 @@ struct cmdq_client *cmdq_mbox_create(struct device *dev, int index, u32 timeout)
> >  	client->client.dev = dev;
> >  	client->client.tx_block = false;
> >  	client->chan = mbox_request_channel(&client->client, index);
> > +	mutex_init(&client->mutex);
> >  
> >  	if (IS_ERR(client->chan)) {
> >  		long err;
> > @@ -352,9 +353,11 @@ int cmdq_pkt_flush_async(struct cmdq_pkt *pkt, cmdq_async_flush_cb cb,
> >  		spin_unlock_irqrestore(&client->lock, flags);
> >  	}
> >  
> > +	mutex_lock(&client->mutex);
> >  	mbox_send_message(client->chan, pkt);
> >  	/* We can send next packet immediately, so just call txdone. */
> >  	mbox_client_txdone(client->chan, 0);
> > +	mutex_unlock(&client->mutex);
> >  
> >  	return 0;
> >  }
> > diff --git a/include/linux/soc/mediatek/mtk-cmdq.h b/include/linux/soc/mediatek/mtk-cmdq.h
> > index a74c1d5acdf3..0f9071cd1bc7 100644
> > --- a/include/linux/soc/mediatek/mtk-cmdq.h
> > +++ b/include/linux/soc/mediatek/mtk-cmdq.h
> > @@ -28,6 +28,7 @@ struct cmdq_client {
> >  	struct mbox_chan *chan;
> >  	struct timer_list timer;
> >  	u32 timeout_ms; /* in unit of microsecond */
> > +	struct mutex mutex;
> >  };
> >  
> >  /**
> 


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] soc: mediatek: cmdq: avoid racing condition with mutex
  2019-12-04  2:22 ` CK Hu
  2019-12-30  2:02   ` CK Hu
@ 2019-12-30  3:28   ` Jassi Brar
  1 sibling, 0 replies; 5+ messages in thread
From: Jassi Brar @ 2019-12-30  3:28 UTC (permalink / raw)
  To: CK Hu
  Cc: Bibby Hsieh, Matthias Brugger, Rob Herring, Devicetree List,
	Linux Kernel Mailing List, linux-arm-kernel, linux-mediatek,
	srv_heupstream, Nicolas Boichat, Dennis-YC Hsieh, Houlong Wei

On Tue, Dec 3, 2019 at 8:22 PM CK Hu <ck.hu@mediatek.com> wrote:
>
> Hi, Jassi:
>
> Are mbox_send_message() and mbox_client_txdone() thread-safe? If these
> two are thread-safe, this bug should be fixed in mailbox core not
> client.
>
mbox_client_txdone should be called only when the client _knows_ the
message has been sent.
There is difference between knowing when tx is done, and assuming
tx-done because there is no way of knowing it.
Your issue arises because you immediately call mbox_client_txdone
after mbox_send_message, which may be the only way to do it but that
doesn't mean you shouldn't have to take any other precautions (like a
mutex). So I think your patch is reasonable.

Cheers!


> Regards,
> CK
>
> On Thu, 2019-11-21 at 15:29 +0800, Bibby Hsieh wrote:
> > If cmdq client is multi thread user, racing will occur without mutex
> > protection. It will make the C message queued in mailbox's queue
> > always need D message's triggering.
> >
> > Thread A              Thread B                  Thread C              Thread D...
> > -----------------------------------------------------------------------------------
> > mbox_send_message()
> >       send_data()
> >                       mbox_send_message()
> >                               *exit
> >                                               mbox_send_message()
> >                                                       *exit
> > mbox_client_txdone()
> >       tx_tick()
> >                       mbox_client_txdone()
> >                               tx_tick()
> >                                               mbox_client_txdone()
> >                                                       tx_tick()
> > msg_submit()
> >       send_data()
> >                       msg_submit()
> >                               *exit
> >                                               msg_submit()
> >                                                       *exit
> > -----------------------------------------------------------------------------------
> >
> > Signed-off-by: Bibby Hsieh <bibby.hsieh@mediatek.com>
> > ---
> >  drivers/soc/mediatek/mtk-cmdq-helper.c | 3 +++
> >  include/linux/soc/mediatek/mtk-cmdq.h  | 1 +
> >  2 files changed, 4 insertions(+)
> >
> > diff --git a/drivers/soc/mediatek/mtk-cmdq-helper.c b/drivers/soc/mediatek/mtk-cmdq-helper.c
> > index 9add0fd5fa6c..9e35e0beffaa 100644
> > --- a/drivers/soc/mediatek/mtk-cmdq-helper.c
> > +++ b/drivers/soc/mediatek/mtk-cmdq-helper.c
> > @@ -81,6 +81,7 @@ struct cmdq_client *cmdq_mbox_create(struct device *dev, int index, u32 timeout)
> >       client->client.dev = dev;
> >       client->client.tx_block = false;
> >       client->chan = mbox_request_channel(&client->client, index);
> > +     mutex_init(&client->mutex);
> >
> >       if (IS_ERR(client->chan)) {
> >               long err;
> > @@ -352,9 +353,11 @@ int cmdq_pkt_flush_async(struct cmdq_pkt *pkt, cmdq_async_flush_cb cb,
> >               spin_unlock_irqrestore(&client->lock, flags);
> >       }
> >
> > +     mutex_lock(&client->mutex);
> >       mbox_send_message(client->chan, pkt);
> >       /* We can send next packet immediately, so just call txdone. */
> >       mbox_client_txdone(client->chan, 0);
> > +     mutex_unlock(&client->mutex);
> >
> >       return 0;
> >  }
> > diff --git a/include/linux/soc/mediatek/mtk-cmdq.h b/include/linux/soc/mediatek/mtk-cmdq.h
> > index a74c1d5acdf3..0f9071cd1bc7 100644
> > --- a/include/linux/soc/mediatek/mtk-cmdq.h
> > +++ b/include/linux/soc/mediatek/mtk-cmdq.h
> > @@ -28,6 +28,7 @@ struct cmdq_client {
> >       struct mbox_chan *chan;
> >       struct timer_list timer;
> >       u32 timeout_ms; /* in unit of microsecond */
> > +     struct mutex mutex;
> >  };
> >
> >  /**
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] soc: mediatek: cmdq: avoid racing condition with mutex
  2019-11-21  7:29 [PATCH] soc: mediatek: cmdq: avoid racing condition with mutex Bibby Hsieh
  2019-12-04  2:22 ` CK Hu
@ 2019-12-30  6:44 ` CK Hu
  1 sibling, 0 replies; 5+ messages in thread
From: CK Hu @ 2019-12-30  6:44 UTC (permalink / raw)
  To: Bibby Hsieh
  Cc: Jassi Brar, Matthias Brugger, Rob Herring, devicetree,
	linux-kernel, linux-arm-kernel, linux-mediatek, srv_heupstream,
	Nicolas Boichat, Dennis-YC Hsieh, Houlong Wei

Hi, Bibby:

On Thu, 2019-11-21 at 15:29 +0800, Bibby Hsieh wrote:
> If cmdq client is multi thread user, racing will occur without mutex
> protection. It will make the C message queued in mailbox's queue
> always need D message's triggering.
> 
> Thread A		Thread B		  Thread C		Thread D...
> -----------------------------------------------------------------------------------
> mbox_send_message()
> 	send_data()
> 			mbox_send_message()
> 				*exit
> 						mbox_send_message()
> 							*exit
> mbox_client_txdone()
> 	tx_tick()
> 			mbox_client_txdone()
> 				tx_tick()
> 						mbox_client_txdone()
> 							tx_tick()
> msg_submit()
> 	send_data()
> 			msg_submit()
> 				*exit
> 						msg_submit()
> 							*exit
> -----------------------------------------------------------------------------------
> 
> Signed-off-by: Bibby Hsieh <bibby.hsieh@mediatek.com>
> ---
>  drivers/soc/mediatek/mtk-cmdq-helper.c | 3 +++
>  include/linux/soc/mediatek/mtk-cmdq.h  | 1 +
>  2 files changed, 4 insertions(+)
> 
> diff --git a/drivers/soc/mediatek/mtk-cmdq-helper.c b/drivers/soc/mediatek/mtk-cmdq-helper.c
> index 9add0fd5fa6c..9e35e0beffaa 100644
> --- a/drivers/soc/mediatek/mtk-cmdq-helper.c
> +++ b/drivers/soc/mediatek/mtk-cmdq-helper.c
> @@ -81,6 +81,7 @@ struct cmdq_client *cmdq_mbox_create(struct device *dev, int index, u32 timeout)
>  	client->client.dev = dev;
>  	client->client.tx_block = false;
>  	client->chan = mbox_request_channel(&client->client, index);
> +	mutex_init(&client->mutex);
>  
>  	if (IS_ERR(client->chan)) {
>  		long err;
> @@ -352,9 +353,11 @@ int cmdq_pkt_flush_async(struct cmdq_pkt *pkt, cmdq_async_flush_cb cb,
>  		spin_unlock_irqrestore(&client->lock, flags);
>  	}
>  
> +	mutex_lock(&client->mutex);
>  	mbox_send_message(client->chan, pkt);
>  	/* We can send next packet immediately, so just call txdone. */
>  	mbox_client_txdone(client->chan, 0);
> +	mutex_unlock(&client->mutex);


In [1], Mediatek DRM is the first client to use cmdq and it already has
its own mutex to protect this. I think helper is something help many
user but now I just see Mediatek MDP [2] need this. For DRM, there are
so many useless code in cmdq_pkt_flush_async(). DRM does not need the
timer to check timeout. DRM could do dma_sync_single_for_cpu() in its
callback and need not to create a intermediate callback to do this. I
would agree this patch only when I see two or more user need this.


[1]
https://github.com/ckhu-mediatek/linux.git-tags/commit/4df12ed1866d1104f631e06218bd15fde512a79e
[2] https://patchwork.kernel.org/patch/10945609/

Regards,
CK

>  
>  	return 0;
>  }
> diff --git a/include/linux/soc/mediatek/mtk-cmdq.h b/include/linux/soc/mediatek/mtk-cmdq.h
> index a74c1d5acdf3..0f9071cd1bc7 100644
> --- a/include/linux/soc/mediatek/mtk-cmdq.h
> +++ b/include/linux/soc/mediatek/mtk-cmdq.h
> @@ -28,6 +28,7 @@ struct cmdq_client {
>  	struct mbox_chan *chan;
>  	struct timer_list timer;
>  	u32 timeout_ms; /* in unit of microsecond */
> +	struct mutex mutex;
>  };
>  
>  /**


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2019-12-30  6:44 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-11-21  7:29 [PATCH] soc: mediatek: cmdq: avoid racing condition with mutex Bibby Hsieh
2019-12-04  2:22 ` CK Hu
2019-12-30  2:02   ` CK Hu
2019-12-30  3:28   ` Jassi Brar
2019-12-30  6:44 ` CK Hu

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).