linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] firmware: arm_scmi: Free mailbox channels if probe fails
@ 2021-08-03 22:22 Rishabh Bhatnagar
  2021-08-04  8:33 ` Cristian Marussi
  0 siblings, 1 reply; 4+ messages in thread
From: Rishabh Bhatnagar @ 2021-08-03 22:22 UTC (permalink / raw)
  To: sudeep.holla, cristian.marussi
  Cc: linux-arm-kernel, linux-kernel, avajid, adharmap, Rishabh Bhatnagar

Mailbox channels for the base protocol are setup during probe.
There can be a scenario where probe fails to acquire the base
protocol due to a timeout leading to cleaning up of all device
managed memory including the scmi_mailbox structure setup during
mailbox_chan_setup function.
[   12.735104]arm-scmi soc:qcom,scmi: timed out in resp(caller: version_get+0x84/0x140)
[   12.735224]arm-scmi soc:qcom,scmi: unable to communicate with SCMI
[   12.735947]arm-scmi: probe of soc:qcom,scmi failed with error -110

Now when a message arrives at cpu slightly after the timeout, the mailbox
controller will try to call the rx_callback of the client and might end
up accessing freed memory.
[   12.758363][    C0] Call trace:
[   12.758367][    C0]  rx_callback+0x24/0x160
[   12.758372][    C0]  mbox_chan_received_data+0x44/0x94
[   12.758386][    C0]  __handle_irq_event_percpu+0xd4/0x240
This patch frees the mailbox channels setup during probe and adds some more
error handling in case the probe fails.

Change-Id: I1214ec2c4c92c4a3ca5fa73de11e0e403b13b46a
Signed-off-by: Rishabh Bhatnagar <rishabhb@codeaurora.org>
---
 drivers/firmware/arm_scmi/driver.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/drivers/firmware/arm_scmi/driver.c b/drivers/firmware/arm_scmi/driver.c
index 9b2e8d4..518c7b9 100644
--- a/drivers/firmware/arm_scmi/driver.c
+++ b/drivers/firmware/arm_scmi/driver.c
@@ -1430,7 +1430,7 @@ static int scmi_probe(struct platform_device *pdev)
 
 	ret = scmi_xfer_info_init(info);
 	if (ret)
-		return ret;
+		goto clear_txrx_setup;
 
 	if (scmi_notification_init(handle))
 		dev_err(dev, "SCMI Notifications NOT available.\n");
@@ -1443,7 +1443,7 @@ static int scmi_probe(struct platform_device *pdev)
 	ret = scmi_protocol_acquire(handle, SCMI_PROTOCOL_BASE);
 	if (ret) {
 		dev_err(dev, "unable to communicate with SCMI\n");
-		return ret;
+		goto notification_exit;
 	}
 
 	mutex_lock(&scmi_list_mutex);
@@ -1482,6 +1482,13 @@ static int scmi_probe(struct platform_device *pdev)
 	}
 
 	return 0;
+
+notification_exit:
+	scmi_notification_exit(&info->handle);
+clear_txrx_setup:
+	idr_for_each(&info->tx_idr, info->desc->ops->chan_free, &info->tx_idr);
+	idr_for_each(&info->rx_idr, info->desc->ops->chan_free, &info->rx_idr);
+	return ret;
 }
 
 void scmi_free_channel(struct scmi_chan_info *cinfo, struct idr *idr, int id)
-- 
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project


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

* Re: [PATCH] firmware: arm_scmi: Free mailbox channels if probe fails
  2021-08-03 22:22 [PATCH] firmware: arm_scmi: Free mailbox channels if probe fails Rishabh Bhatnagar
@ 2021-08-04  8:33 ` Cristian Marussi
  2021-08-04  8:40   ` Cristian Marussi
  0 siblings, 1 reply; 4+ messages in thread
From: Cristian Marussi @ 2021-08-04  8:33 UTC (permalink / raw)
  To: Rishabh Bhatnagar
  Cc: sudeep.holla, linux-arm-kernel, linux-kernel, avajid, adharmap

On Tue, Aug 03, 2021 at 03:22:22PM -0700, Rishabh Bhatnagar wrote:
> Mailbox channels for the base protocol are setup during probe.
> There can be a scenario where probe fails to acquire the base
> protocol due to a timeout leading to cleaning up of all device
> managed memory including the scmi_mailbox structure setup during
> mailbox_chan_setup function.
> [   12.735104]arm-scmi soc:qcom,scmi: timed out in resp(caller: version_get+0x84/0x140)
> [   12.735224]arm-scmi soc:qcom,scmi: unable to communicate with SCMI
> [   12.735947]arm-scmi: probe of soc:qcom,scmi failed with error -110
> 
> Now when a message arrives at cpu slightly after the timeout, the mailbox
> controller will try to call the rx_callback of the client and might end
> up accessing freed memory.
> [   12.758363][    C0] Call trace:
> [   12.758367][    C0]  rx_callback+0x24/0x160
> [   12.758372][    C0]  mbox_chan_received_data+0x44/0x94
> [   12.758386][    C0]  __handle_irq_event_percpu+0xd4/0x240
> This patch frees the mailbox channels setup during probe and adds some more
> error handling in case the probe fails.
> 
> Change-Id: I1214ec2c4c92c4a3ca5fa73de11e0e403b13b46a
> Signed-off-by: Rishabh Bhatnagar <rishabhb@codeaurora.org>

Hi Rishabh,

Good catch, thanks for this.

> ---
>  drivers/firmware/arm_scmi/driver.c | 11 +++++++++--
>  1 file changed, 9 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/firmware/arm_scmi/driver.c b/drivers/firmware/arm_scmi/driver.c
> index 9b2e8d4..518c7b9 100644
> --- a/drivers/firmware/arm_scmi/driver.c
> +++ b/drivers/firmware/arm_scmi/driver.c
> @@ -1430,7 +1430,7 @@ static int scmi_probe(struct platform_device *pdev)
>  
>  	ret = scmi_xfer_info_init(info);
>  	if (ret)
> -		return ret;
> +		goto clear_txrx_setup;
>  
>  	if (scmi_notification_init(handle))
>  		dev_err(dev, "SCMI Notifications NOT available.\n");
> @@ -1443,7 +1443,7 @@ static int scmi_probe(struct platform_device *pdev)
>  	ret = scmi_protocol_acquire(handle, SCMI_PROTOCOL_BASE);
>  	if (ret) {
>  		dev_err(dev, "unable to communicate with SCMI\n");
> -		return ret;
> +		goto notification_exit;
>  	}
>  
>  	mutex_lock(&scmi_list_mutex);
> @@ -1482,6 +1482,13 @@ static int scmi_probe(struct platform_device *pdev)
>  	}
>  
>  	return 0;
> +
> +notification_exit:
> +	scmi_notification_exit(&info->handle);
> +clear_txrx_setup:
> +	idr_for_each(&info->tx_idr, info->desc->ops->chan_free, &info->tx_idr);
> +	idr_for_each(&info->rx_idr, info->desc->ops->chan_free, &info->rx_idr);
> +	return ret;
>  }
>  

Shouldn't we also clear the internal IDRs memory allocs after these
idr_for_each() adding a couple of:

	idr_destroy(&info->tx_idr);

	idr_destroy(&info->rx_idr);

like scmi_remove() does ?

Thanks,
Cristian

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

* Re: [PATCH] firmware: arm_scmi: Free mailbox channels if probe fails
  2021-08-04  8:33 ` Cristian Marussi
@ 2021-08-04  8:40   ` Cristian Marussi
  2021-08-04 17:29     ` rishabhb
  0 siblings, 1 reply; 4+ messages in thread
From: Cristian Marussi @ 2021-08-04  8:40 UTC (permalink / raw)
  To: Rishabh Bhatnagar
  Cc: sudeep.holla, linux-arm-kernel, linux-kernel, avajid, adharmap

On Wed, Aug 04, 2021 at 09:33:58AM +0100, Cristian Marussi wrote:
> On Tue, Aug 03, 2021 at 03:22:22PM -0700, Rishabh Bhatnagar wrote:
> > Mailbox channels for the base protocol are setup during probe.
> > There can be a scenario where probe fails to acquire the base
> > protocol due to a timeout leading to cleaning up of all device
> > managed memory including the scmi_mailbox structure setup during
> > mailbox_chan_setup function.
> > [   12.735104]arm-scmi soc:qcom,scmi: timed out in resp(caller: version_get+0x84/0x140)
> > [   12.735224]arm-scmi soc:qcom,scmi: unable to communicate with SCMI
> > [   12.735947]arm-scmi: probe of soc:qcom,scmi failed with error -110
> > 
> > Now when a message arrives at cpu slightly after the timeout, the mailbox
> > controller will try to call the rx_callback of the client and might end
> > up accessing freed memory.
> > [   12.758363][    C0] Call trace:
> > [   12.758367][    C0]  rx_callback+0x24/0x160
> > [   12.758372][    C0]  mbox_chan_received_data+0x44/0x94
> > [   12.758386][    C0]  __handle_irq_event_percpu+0xd4/0x240
> > This patch frees the mailbox channels setup during probe and adds some more
> > error handling in case the probe fails.
> > 
> > Change-Id: I1214ec2c4c92c4a3ca5fa73de11e0e403b13b46a
> > Signed-off-by: Rishabh Bhatnagar <rishabhb@codeaurora.org>
> 
> Hi Rishabh,
> 
> Good catch, thanks for this.
> 

Hi again,

sorry forgot one thing.

> > ---
> >  drivers/firmware/arm_scmi/driver.c | 11 +++++++++--
> >  1 file changed, 9 insertions(+), 2 deletions(-)
> > 
> > diff --git a/drivers/firmware/arm_scmi/driver.c b/drivers/firmware/arm_scmi/driver.c
> > index 9b2e8d4..518c7b9 100644
> > --- a/drivers/firmware/arm_scmi/driver.c
> > +++ b/drivers/firmware/arm_scmi/driver.c
> > @@ -1430,7 +1430,7 @@ static int scmi_probe(struct platform_device *pdev)
> >  
> >  	ret = scmi_xfer_info_init(info);
> >  	if (ret)
> > -		return ret;
> > +		goto clear_txrx_setup;
> >  
> >  	if (scmi_notification_init(handle))
> >  		dev_err(dev, "SCMI Notifications NOT available.\n");
> > @@ -1443,7 +1443,7 @@ static int scmi_probe(struct platform_device *pdev)
> >  	ret = scmi_protocol_acquire(handle, SCMI_PROTOCOL_BASE);
> >  	if (ret) {
> >  		dev_err(dev, "unable to communicate with SCMI\n");
> > -		return ret;
> > +		goto notification_exit;
> >  	}
> >  
> >  	mutex_lock(&scmi_list_mutex);
> > @@ -1482,6 +1482,13 @@ static int scmi_probe(struct platform_device *pdev)
> >  	}
> >  
> >  	return 0;
> > +
> > +notification_exit:
> > +	scmi_notification_exit(&info->handle);
> > +clear_txrx_setup:
> > +	idr_for_each(&info->tx_idr, info->desc->ops->chan_free, &info->tx_idr);
> > +	idr_for_each(&info->rx_idr, info->desc->ops->chan_free, &info->rx_idr);
> > +	return ret;
> >  }
> >  
> 
> Shouldn't we also clear the internal IDRs memory allocs after these
> idr_for_each() adding a couple of:
> 
> 	idr_destroy(&info->tx_idr);
> 
> 	idr_destroy(&info->rx_idr);
> 
> like scmi_remove() does ?
> 

Maybe it could be worth at this point unifying this common "cleanup-all-channels"
logic shared between scmi_probe()-error-path and scmi_remove() into its own
helper function to call from both sites.

Thanks,
Cristian

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

* Re: [PATCH] firmware: arm_scmi: Free mailbox channels if probe fails
  2021-08-04  8:40   ` Cristian Marussi
@ 2021-08-04 17:29     ` rishabhb
  0 siblings, 0 replies; 4+ messages in thread
From: rishabhb @ 2021-08-04 17:29 UTC (permalink / raw)
  To: Cristian Marussi
  Cc: sudeep.holla, linux-arm-kernel, linux-kernel, avajid, adharmap

On 2021-08-04 01:40, Cristian Marussi wrote:
> On Wed, Aug 04, 2021 at 09:33:58AM +0100, Cristian Marussi wrote:
>> On Tue, Aug 03, 2021 at 03:22:22PM -0700, Rishabh Bhatnagar wrote:
>> > Mailbox channels for the base protocol are setup during probe.
>> > There can be a scenario where probe fails to acquire the base
>> > protocol due to a timeout leading to cleaning up of all device
>> > managed memory including the scmi_mailbox structure setup during
>> > mailbox_chan_setup function.
>> > [   12.735104]arm-scmi soc:qcom,scmi: timed out in resp(caller: version_get+0x84/0x140)
>> > [   12.735224]arm-scmi soc:qcom,scmi: unable to communicate with SCMI
>> > [   12.735947]arm-scmi: probe of soc:qcom,scmi failed with error -110
>> >
>> > Now when a message arrives at cpu slightly after the timeout, the mailbox
>> > controller will try to call the rx_callback of the client and might end
>> > up accessing freed memory.
>> > [   12.758363][    C0] Call trace:
>> > [   12.758367][    C0]  rx_callback+0x24/0x160
>> > [   12.758372][    C0]  mbox_chan_received_data+0x44/0x94
>> > [   12.758386][    C0]  __handle_irq_event_percpu+0xd4/0x240
>> > This patch frees the mailbox channels setup during probe and adds some more
>> > error handling in case the probe fails.
>> >
>> > Change-Id: I1214ec2c4c92c4a3ca5fa73de11e0e403b13b46a
>> > Signed-off-by: Rishabh Bhatnagar <rishabhb@codeaurora.org>
>> 
>> Hi Rishabh,
>> 
>> Good catch, thanks for this.
>> 
> 
> Hi again,
> 
> sorry forgot one thing.
> 
>> > ---
>> >  drivers/firmware/arm_scmi/driver.c | 11 +++++++++--
>> >  1 file changed, 9 insertions(+), 2 deletions(-)
>> >
>> > diff --git a/drivers/firmware/arm_scmi/driver.c b/drivers/firmware/arm_scmi/driver.c
>> > index 9b2e8d4..518c7b9 100644
>> > --- a/drivers/firmware/arm_scmi/driver.c
>> > +++ b/drivers/firmware/arm_scmi/driver.c
>> > @@ -1430,7 +1430,7 @@ static int scmi_probe(struct platform_device *pdev)
>> >
>> >  	ret = scmi_xfer_info_init(info);
>> >  	if (ret)
>> > -		return ret;
>> > +		goto clear_txrx_setup;
>> >
>> >  	if (scmi_notification_init(handle))
>> >  		dev_err(dev, "SCMI Notifications NOT available.\n");
>> > @@ -1443,7 +1443,7 @@ static int scmi_probe(struct platform_device *pdev)
>> >  	ret = scmi_protocol_acquire(handle, SCMI_PROTOCOL_BASE);
>> >  	if (ret) {
>> >  		dev_err(dev, "unable to communicate with SCMI\n");
>> > -		return ret;
>> > +		goto notification_exit;
>> >  	}
>> >
>> >  	mutex_lock(&scmi_list_mutex);
>> > @@ -1482,6 +1482,13 @@ static int scmi_probe(struct platform_device *pdev)
>> >  	}
>> >
>> >  	return 0;
>> > +
>> > +notification_exit:
>> > +	scmi_notification_exit(&info->handle);
>> > +clear_txrx_setup:
>> > +	idr_for_each(&info->tx_idr, info->desc->ops->chan_free, &info->tx_idr);
>> > +	idr_for_each(&info->rx_idr, info->desc->ops->chan_free, &info->rx_idr);
>> > +	return ret;
>> >  }
>> >
>> 
>> Shouldn't we also clear the internal IDRs memory allocs after these
>> idr_for_each() adding a couple of:
>> 
>> 	idr_destroy(&info->tx_idr);
>> 
>> 	idr_destroy(&info->rx_idr);
>> 
>> like scmi_remove() does ?
>> 
> 
> Maybe it could be worth at this point unifying this common
> "cleanup-all-channels"
> logic shared between scmi_probe()-error-path and scmi_remove() into its 
> own
> helper function to call from both sites.
> 
> Thanks,
> Cristian
Thanks for the review. I'll move the cleanup to a common helper 
function.
It makes sense to destroy tx/rx idrs. I didn't realize they allocate 
memory
internally.

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

end of thread, other threads:[~2021-08-04 17:29 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-03 22:22 [PATCH] firmware: arm_scmi: Free mailbox channels if probe fails Rishabh Bhatnagar
2021-08-04  8:33 ` Cristian Marussi
2021-08-04  8:40   ` Cristian Marussi
2021-08-04 17:29     ` rishabhb

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