Skip to contentSkip to conversation

firmware: qcom_scm: fix error for incompatible pointer

PATCH In review Minwoo Im 4 participants 5 messages Jul 19, 2019 – Jul 23, 2019 Arm Qualcomm mailing list

lore
Share & tools
lore

Minwoo Im

author
#0 lore raw

PATCH 1/1 firmware: qcom_scm: fix error for incompatible pointer

The following error can happen when trying to build it:

```
drivers/firmware/qcom_scm.c: In function ‘qcom_scm_assign_mem’:
drivers/firmware/qcom_scm.c:460:47: error: passing argument 3 of ‘dma_alloc_coherent’ from incompatible pointer type [-Werror=incompatible-pointer-types]
  ptr = dma_alloc_coherent(__scm->dev, ptr_sz, &ptr_phys, GFP_KERNEL);
                                               ^
In file included from drivers/firmware/qcom_scm.c:12:0:
./include/linux/dma-mapping.h:636:21: note: expected ‘dma_addr_t * {aka long long unsigned int *}’ but argument is of type ‘phys_addr_t * {aka unsigned int *}’
 static inline void *dma_alloc_coherent(struct device *dev, size_t size,
                     ^~~~~~~~~~~~~~~~~~
```

We just can cast phys_addr_t to dma_addr_t here.

Cc Andy Gross <[email protected]>
Cc David Brown <[email protected]>
---
 drivers/firmware/qcom_scm.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/firmware/qcom_scm.c +2 -1
index 2ddc118dba1b..7f6c841fa200 100644
@@ -457,7 +457,8 @@ int qcom_scm_assign_mem(phys_addr_t mem_addr, size_t mem_sz,
 	ptr_sz = ALIGN(src_sz, SZ_64) + ALIGN(mem_to_map_sz, SZ_64) +
 			ALIGN(dest_sz, SZ_64);
 
-	ptr = dma_alloc_coherent(__scm->dev, ptr_sz, &ptr_phys, GFP_KERNEL);
+	ptr = dma_alloc_coherent(__scm->dev, ptr_sz, (dma_addr_t *) &ptr_phys,
+					GFP_KERNEL);
 	if (!ptr)
 		return -ENOMEM;
 
Signature · 1 line
2.17.1
#1 lore raw

Adding people who have worked on drivers/firmware/qcom_scm.c or DMA

Quoted 13 lines · Minwoo Im

The following error can happen when trying to build it:

```
drivers/firmware/qcom_scm.c: In function ‘qcom_scm_assign_mem’:
drivers/firmware/qcom_scm.c:460:47: error: passing argument 3 of ‘dma_alloc_coherent’ from incompatible pointer type [-Werror=incompatible-pointer-types]
  ptr = dma_alloc_coherent(__scm->dev, ptr_sz, &ptr_phys, GFP_KERNEL);
                                               ^
In file included from drivers/firmware/qcom_scm.c:12:0:
./include/linux/dma-mapping.h:636:21: note: expected ‘dma_addr_t * {aka long long unsigned int *}’ but argument is of type ‘phys_addr_t * {aka unsigned int *}’
 static inline void *dma_alloc_coherent(struct device *dev, size_t size,
                     ^~~~~~~~~~~~~~~~~~
```
Quoted 1 line

We just can cast phys_addr_t to dma_addr_t here.

IME, casting is rarely a proper solution.

Quoted 14 lines
drivers/firmware/qcom_scm.c +2 -1
index 2ddc118dba1b..7f6c841fa200 100644
@@ -457,7 +457,8 @@ int qcom_scm_assign_mem(phys_addr_t mem_addr, size_t mem_sz,
 	ptr_sz = ALIGN(src_sz, SZ_64) + ALIGN(mem_to_map_sz, SZ_64) +
 			ALIGN(dest_sz, SZ_64);
 
-	ptr = dma_alloc_coherent(__scm->dev, ptr_sz, &ptr_phys, GFP_KERNEL);
+	ptr = dma_alloc_coherent(__scm->dev, ptr_sz, (dma_addr_t *) &ptr_phys,
+					GFP_KERNEL);
 	if (!ptr)
 		return -ENOMEM;
 
#2 lore raw
Quoted 9 lines · Marc Gonzalez
Quoted 7 lines

In file included from drivers/firmware/qcom_scm.c:12:0:
./include/linux/dma-mapping.h:636:21: note: expected ‘dma_addr_t * {aka long long unsigned int *}’ but argument is of type ‘phys_addr_t * {aka unsigned int *}’
static inline void *dma_alloc_coherent(struct device *dev, size_t size,

                     ^~~~~~~~~~~~~~~~~~

```

We just can cast phys_addr_t to dma_addr_t here.

IME, casting is rarely a proper solution.

*nod*

ptr_phys probably should be a dma_addr_t. Unless this driver is so magic that it really wants a physical and not a dma address, in which case it needs to use alloc_pages instead of dma_alloc_coherent and then call page_to_phys on the returned page, and a very big comment explaining why it is so special.

#3 lore raw
Quoted 11 lines · Christoph Hellwig
Quoted 9 lines · Marc Gonzalez
Quoted 7 lines

In file included from drivers/firmware/qcom_scm.c:12:0:
./include/linux/dma-mapping.h:636:21: note: expected ‘dma_addr_t * {aka long long unsigned int *}’ but argument is of type ‘phys_addr_t * {aka unsigned int *}’
static inline void *dma_alloc_coherent(struct device *dev, size_t size,

                     ^~~~~~~~~~~~~~~~~~

```

We just can cast phys_addr_t to dma_addr_t here.

IME, casting is rarely a proper solution.

Quoted 7 lines

*nod*

ptr_phys probably should be a dma_addr_t. Unless this driver is so magic that it really wants a physical and not a dma address, in which case it needs to use alloc_pages instead of dma_alloc_coherent and then call page_to_phys on the returned page, and a very big comment explaining why it is so special.

The scm call takes physical addresses (which happens to be 1:1 with DMA addresses for this driver).

This allocation started off (downstream) as a simple kmalloc(), but while the scm call is being executed an access from Linux will cause a security violation (that's not handled gracefully). The properties of dma_alloc is closer, so that's where the code is today.

Optimally this should be something like alloc_pages() and some mechanism for unmapping the pages during the call. But no one has come up with a suitable patch for that.

But there's a patch from Stephen for this already (not doing a typecast). Apparently I missed merging this, so I'll do that.

https://lore.kernel.org/linux-arm-msm/[email protected]/

Regards,
Bjorn

author
#4 lore raw
Quoted 25 lines
Quoted 11 lines
Quoted 3 lines
Quoted 1 line

We just can cast phys_addr_t to dma_addr_t here.

IME, casting is rarely a proper solution.

*nod*

ptr_phys probably should be a dma_addr_t. Unless this driver is so magic that it really wants a physical and not a dma address, in which case it needs to use alloc_pages instead of dma_alloc_coherent and then call page_to_phys on the returned page, and a very big comment explaining why it is so special.

The scm call takes physical addresses (which happens to be 1:1 with DMA addresses for this driver).

This allocation started off (downstream) as a simple kmalloc(), but while the scm call is being executed an access from Linux will cause a security violation (that's not handled gracefully). The properties of dma_alloc is closer, so that's where the code is today.

Optimally this should be something like alloc_pages() and some mechanism for unmapping the pages during the call. But no one has come up with a suitable patch for that.

Quoted 4 lines

But there's a patch from Stephen for this already (not doing a typecast). Apparently I missed merging this, so I'll do that.

https://lore.kernel.org/linux-arm-msm/[email protected]/

Bjron,

I appreciate for checking this. And also thanks all you guys for the comments here!

Thanks,