diff mbox

[5/5] remoteproc: core: Clip carveout if it's too big

Message ID 1462454983-13168-6-git-send-email-lee.jones@linaro.org
State New
Headers show

Commit Message

Lee Jones May 5, 2016, 1:29 p.m. UTC
Carveout size is primarily extracted from the firmware binary.  However,
DT can over-ride this by providing a different (smaller) size.  We're
adding protection here to ensure the we only allocate the smaller of the
two provided sizes in order to decrease the risk of clashes.

Signed-off-by: Lee Jones <lee.jones@linaro.org>

---
 drivers/remoteproc/remoteproc_core.c | 9 +++++++++
 1 file changed, 9 insertions(+)

-- 
2.8.0

Comments

Bjorn Andersson May 10, 2016, 7:21 p.m. UTC | #1
On Thu 05 May 06:29 PDT 2016, Lee Jones wrote:

> Carveout size is primarily extracted from the firmware binary.  However,

> DT can over-ride this by providing a different (smaller) size.  We're

> adding protection here to ensure the we only allocate the smaller of the

> two provided sizes in order to decrease the risk of clashes.

> 


Is this really the right thing to do?

The firmware is bundled with a resource table stating a certain size of
this the carveout and this would "silently" give it less space. On some
systems an IOMMU will save us (killing the firmware) but on others I
fear the firmware might just access memory outside its expected buffer.

> Signed-off-by: Lee Jones <lee.jones@linaro.org>

> ---

>  drivers/remoteproc/remoteproc_core.c | 9 +++++++++

>  1 file changed, 9 insertions(+)

> 

> diff --git a/drivers/remoteproc/remoteproc_core.c b/drivers/remoteproc/remoteproc_core.c

[..]
> @@ -600,6 +601,14 @@ static int rproc_handle_carveout(struct rproc *rproc,

>  		return -ENOMEM;

>  

>  	dma_dev = rproc_subdev_lookup(rproc, "carveout");

> +	sub = dev_get_drvdata(dma_dev);

> +

> +	if (rsc->len > resource_size(sub->res)) {

> +		dev_warn(dev, "carveout too big (0x%x): clipping to 0x%x\n",

> +			 rsc->len, resource_size(sub->res));

> +		rsc->len = resource_size(sub->res);

> +	}


I would rather expect this to say:

if (resource_size(sub->res) < rsc->len) {
	dev_err(dev, "defined carveout to small for firmware\n");
	return -EINVAL;
}

Unless we trust the remote firmware to dynamically follow what we put in
the resource table. (And how does it tell us if that limit isn't
enough?)

> +

>  	va = dma_alloc_coherent(dma_dev, rsc->len, &dma, GFP_KERNEL);

>  	if (!va) {

>  		dev_err(dev->parent, "dma_alloc_coherent err: %d\n", rsc->len);



Apart from this concern I'm still need to review the subdev patch; here
related the part that there's only room for one carveout with only one
size.

Regards,
Bjorn
Loic Pallardy June 17, 2016, 7:53 p.m. UTC | #2
Hi,

On 05/10/2016 09:21 PM, Bjorn Andersson wrote:
> On Thu 05 May 06:29 PDT 2016, Lee Jones wrote:

>

>> Carveout size is primarily extracted from the firmware binary.  However,

>> DT can over-ride this by providing a different (smaller) size.  We're

>> adding protection here to ensure the we only allocate the smaller of the

>> two provided sizes in order to decrease the risk of clashes.

>>

>

> Is this really the right thing to do?

>

> The firmware is bundled with a resource table stating a certain size of

> this the carveout and this would "silently" give it less space. On some

> systems an IOMMU will save us (killing the firmware) but on others I

> fear the firmware might just access memory outside its expected buffer.


Agree with Bjorn, not it is not possible to silently clip carveout 
memory.Firmware resource table should contain exact coprocessor needs. 
If resources are not available, firmware loading must failed with 
explicit message.

Regards,
Loic

>

>> Signed-off-by: Lee Jones <lee.jones@linaro.org>

>> ---

>>   drivers/remoteproc/remoteproc_core.c | 9 +++++++++

>>   1 file changed, 9 insertions(+)

>>

>> diff --git a/drivers/remoteproc/remoteproc_core.c b/drivers/remoteproc/remoteproc_core.c

> [..]

>> @@ -600,6 +601,14 @@ static int rproc_handle_carveout(struct rproc *rproc,

>>   		return -ENOMEM;

>>

>>   	dma_dev = rproc_subdev_lookup(rproc, "carveout");

>> +	sub = dev_get_drvdata(dma_dev);

>> +

>> +	if (rsc->len > resource_size(sub->res)) {

>> +		dev_warn(dev, "carveout too big (0x%x): clipping to 0x%x\n",

>> +			 rsc->len, resource_size(sub->res));

>> +		rsc->len = resource_size(sub->res);

>> +	}

>

> I would rather expect this to say:

>

> if (resource_size(sub->res) < rsc->len) {

> 	dev_err(dev, "defined carveout to small for firmware\n");

> 	return -EINVAL;

> }

>

> Unless we trust the remote firmware to dynamically follow what we put in

> the resource table. (And how does it tell us if that limit isn't

> enough?)

>

>> +

>>   	va = dma_alloc_coherent(dma_dev, rsc->len, &dma, GFP_KERNEL);

>>   	if (!va) {

>>   		dev_err(dev->parent, "dma_alloc_coherent err: %d\n", rsc->len);

>

>

> Apart from this concern I'm still need to review the subdev patch; here

> related the part that there's only room for one carveout with only one

> size.

>

> Regards,

> Bjorn

>

> _______________________________________________

> Kernel mailing list

> Kernel@stlinux.com

> http://www.stlinux.com/mailman/listinfo/kernel

>
diff mbox

Patch

diff --git a/drivers/remoteproc/remoteproc_core.c b/drivers/remoteproc/remoteproc_core.c
index 3d9798c..c3cad708 100644
--- a/drivers/remoteproc/remoteproc_core.c
+++ b/drivers/remoteproc/remoteproc_core.c
@@ -577,6 +577,7 @@  static int rproc_handle_carveout(struct rproc *rproc,
 	struct rproc_mem_entry *carveout, *mapping;
 	struct device *dev = &rproc->dev;
 	struct device *dma_dev;
+	struct rproc_subdev *sub;
 	dma_addr_t dma;
 	void *va;
 	int ret;
@@ -600,6 +601,14 @@  static int rproc_handle_carveout(struct rproc *rproc,
 		return -ENOMEM;
 
 	dma_dev = rproc_subdev_lookup(rproc, "carveout");
+	sub = dev_get_drvdata(dma_dev);
+
+	if (rsc->len > resource_size(sub->res)) {
+		dev_warn(dev, "carveout too big (0x%x): clipping to 0x%x\n",
+			 rsc->len, resource_size(sub->res));
+		rsc->len = resource_size(sub->res);
+	}
+
 	va = dma_alloc_coherent(dma_dev, rsc->len, &dma, GFP_KERNEL);
 	if (!va) {
 		dev_err(dev->parent, "dma_alloc_coherent err: %d\n", rsc->len);