Linux ARM-MSM sub-architecture
 help / color / mirror / Atom feed
* [PATCH net] net: ipa: compute DMA pool size properly
@ 2023-03-26 16:52 Alex Elder
  2023-03-27  7:36 ` Mark Bloch
  2023-03-27 21:16 ` Bjorn Andersson
  0 siblings, 2 replies; 4+ messages in thread
From: Alex Elder @ 2023-03-26 16:52 UTC (permalink / raw)
  To: davem, edumazet, kuba, pabeni
  Cc: quic_bjorande, caleb.connolly, mka, evgreen, andersson,
	quic_cpratapa, quic_avuyyuru, quic_jponduru, quic_subashab, elder,
	netdev, linux-arm-msm, linux-kernel

In gsi_trans_pool_init_dma(), the total size of a pool of memory
used for DMA transactions is calculated.  However the calculation is
done incorrectly.

For 4KB pages, this total size is currently always more than one
page, and as a result, the calculation produces a positive (though
incorrect) total size.  The code still works in this case; we just
end up with fewer DMA pool entries than we intended.

Bjorn Andersson tested booting a kernel with 16KB pages, and hit a
null pointer derereference in sg_alloc_append_table_from_pages(),
descending from gsi_trans_pool_init_dma().  The cause of this was
that a 16KB total size was going to be allocated, and with 16KB
pages the order of that allocation is 0.  The total_size calculation
yielded 0, which eventually led to the crash.

Correcting the total_size calculation fixes the problem.

Reported-by: <quic_bjorande@quicinc.com>
Tested-by: <quic_bjorande@quicinc.com>
Fixes: 9dd441e4ed57 ("soc: qcom: ipa: GSI transactions")
Signed-off-by: Alex Elder <elder@linaro.org>
---
 drivers/net/ipa/gsi_trans.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/net/ipa/gsi_trans.c b/drivers/net/ipa/gsi_trans.c
index 0f52c068c46d6..ee6fb00b71eb6 100644
--- a/drivers/net/ipa/gsi_trans.c
+++ b/drivers/net/ipa/gsi_trans.c
@@ -156,7 +156,7 @@ int gsi_trans_pool_init_dma(struct device *dev, struct gsi_trans_pool *pool,
 	 * gsi_trans_pool_exit_dma() can assume the total allocated
 	 * size is exactly (count * size).
 	 */
-	total_size = get_order(total_size) << PAGE_SHIFT;
+	total_size = PAGE_SIZE << get_order(total_size);
 
 	virt = dma_alloc_coherent(dev, total_size, &addr, GFP_KERNEL);
 	if (!virt)
-- 
2.34.1


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

* Re: [PATCH net] net: ipa: compute DMA pool size properly
  2023-03-26 16:52 [PATCH net] net: ipa: compute DMA pool size properly Alex Elder
@ 2023-03-27  7:36 ` Mark Bloch
  2023-03-27 21:16 ` Bjorn Andersson
  1 sibling, 0 replies; 4+ messages in thread
From: Mark Bloch @ 2023-03-27  7:36 UTC (permalink / raw)
  To: Alex Elder, davem, edumazet, kuba, pabeni
  Cc: quic_bjorande, caleb.connolly, mka, evgreen, andersson,
	quic_cpratapa, quic_avuyyuru, quic_jponduru, quic_subashab, elder,
	netdev, linux-arm-msm, linux-kernel



On 26/03/2023 19:52, Alex Elder wrote:
> In gsi_trans_pool_init_dma(), the total size of a pool of memory
> used for DMA transactions is calculated.  However the calculation is
> done incorrectly.
> 
> For 4KB pages, this total size is currently always more than one
> page, and as a result, the calculation produces a positive (though
> incorrect) total size.  The code still works in this case; we just
> end up with fewer DMA pool entries than we intended.
> 
> Bjorn Andersson tested booting a kernel with 16KB pages, and hit a
> null pointer derereference in sg_alloc_append_table_from_pages(),
> descending from gsi_trans_pool_init_dma().  The cause of this was
> that a 16KB total size was going to be allocated, and with 16KB
> pages the order of that allocation is 0.  The total_size calculation
> yielded 0, which eventually led to the crash.
> 
> Correcting the total_size calculation fixes the problem.
> 
> Reported-by: <quic_bjorande@quicinc.com>
> Tested-by: <quic_bjorande@quicinc.com>
> Fixes: 9dd441e4ed57 ("soc: qcom: ipa: GSI transactions")
> Signed-off-by: Alex Elder <elder@linaro.org>
> ---
>  drivers/net/ipa/gsi_trans.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/net/ipa/gsi_trans.c b/drivers/net/ipa/gsi_trans.c
> index 0f52c068c46d6..ee6fb00b71eb6 100644
> --- a/drivers/net/ipa/gsi_trans.c
> +++ b/drivers/net/ipa/gsi_trans.c
> @@ -156,7 +156,7 @@ int gsi_trans_pool_init_dma(struct device *dev, struct gsi_trans_pool *pool,
>  	 * gsi_trans_pool_exit_dma() can assume the total allocated
>  	 * size is exactly (count * size).
>  	 */
> -	total_size = get_order(total_size) << PAGE_SHIFT;
> +	total_size = PAGE_SIZE << get_order(total_size);
>  
>  	virt = dma_alloc_coherent(dev, total_size, &addr, GFP_KERNEL);
>  	if (!virt)

Thanks,
Reviewed-by: Mark Bloch <mbloch@nvidia.com>

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

* Re: [PATCH net] net: ipa: compute DMA pool size properly
  2023-03-26 16:52 [PATCH net] net: ipa: compute DMA pool size properly Alex Elder
  2023-03-27  7:36 ` Mark Bloch
@ 2023-03-27 21:16 ` Bjorn Andersson
  2023-03-27 23:40   ` Alex Elder
  1 sibling, 1 reply; 4+ messages in thread
From: Bjorn Andersson @ 2023-03-27 21:16 UTC (permalink / raw)
  To: Alex Elder
  Cc: davem, edumazet, kuba, pabeni, caleb.connolly, mka, evgreen,
	andersson, quic_cpratapa, quic_avuyyuru, quic_jponduru,
	quic_subashab, elder, netdev, linux-arm-msm, linux-kernel

On Sun, Mar 26, 2023 at 11:52:23AM -0500, Alex Elder wrote:
> In gsi_trans_pool_init_dma(), the total size of a pool of memory
> used for DMA transactions is calculated.  However the calculation is
> done incorrectly.
> 
> For 4KB pages, this total size is currently always more than one
> page, and as a result, the calculation produces a positive (though
> incorrect) total size.  The code still works in this case; we just
> end up with fewer DMA pool entries than we intended.
> 
> Bjorn Andersson tested booting a kernel with 16KB pages, and hit a
> null pointer derereference in sg_alloc_append_table_from_pages(),
> descending from gsi_trans_pool_init_dma().  The cause of this was
> that a 16KB total size was going to be allocated, and with 16KB
> pages the order of that allocation is 0.  The total_size calculation
> yielded 0, which eventually led to the crash.
> 
> Correcting the total_size calculation fixes the problem.
> 
> Reported-by: <quic_bjorande@quicinc.com>
> Tested-by: <quic_bjorande@quicinc.com>

It would be nice to add "Bjorn Andersson" to these two.

Regards,
Bjorn

> Fixes: 9dd441e4ed57 ("soc: qcom: ipa: GSI transactions")
> Signed-off-by: Alex Elder <elder@linaro.org>
> ---
>  drivers/net/ipa/gsi_trans.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/net/ipa/gsi_trans.c b/drivers/net/ipa/gsi_trans.c
> index 0f52c068c46d6..ee6fb00b71eb6 100644
> --- a/drivers/net/ipa/gsi_trans.c
> +++ b/drivers/net/ipa/gsi_trans.c
> @@ -156,7 +156,7 @@ int gsi_trans_pool_init_dma(struct device *dev, struct gsi_trans_pool *pool,
>  	 * gsi_trans_pool_exit_dma() can assume the total allocated
>  	 * size is exactly (count * size).
>  	 */
> -	total_size = get_order(total_size) << PAGE_SHIFT;
> +	total_size = PAGE_SIZE << get_order(total_size);
>  
>  	virt = dma_alloc_coherent(dev, total_size, &addr, GFP_KERNEL);
>  	if (!virt)
> -- 
> 2.34.1
> 

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

* Re: [PATCH net] net: ipa: compute DMA pool size properly
  2023-03-27 21:16 ` Bjorn Andersson
@ 2023-03-27 23:40   ` Alex Elder
  0 siblings, 0 replies; 4+ messages in thread
From: Alex Elder @ 2023-03-27 23:40 UTC (permalink / raw)
  To: Bjorn Andersson
  Cc: Alex Elder, davem, edumazet, kuba, pabeni, caleb.connolly, mka,
	evgreen, andersson, quic_cpratapa, quic_avuyyuru, quic_jponduru,
	quic_subashab, elder, netdev, linux-arm-msm, linux-kernel



> On Mar 27, 2023, at 4:16 PM, Bjorn Andersson <quic_bjorande@quicinc.com> wrote:
> 
> On Sun, Mar 26, 2023 at 11:52:23AM -0500, Alex Elder wrote:
>> In gsi_trans_pool_init_dma(), the total size of a pool of memory
>> used for DMA transactions is calculated.  However the calculation is
>> done incorrectly.
>> 
>> For 4KB pages, this total size is currently always more than one
>> page, and as a result, the calculation produces a positive (though
>> incorrect) total size.  The code still works in this case; we just
>> end up with fewer DMA pool entries than we intended.
>> 
>> Bjorn Andersson tested booting a kernel with 16KB pages, and hit a
>> null pointer derereference in sg_alloc_append_table_from_pages(),
>> descending from gsi_trans_pool_init_dma().  The cause of this was
>> that a 16KB total size was going to be allocated, and with 16KB
>> pages the order of that allocation is 0.  The total_size calculation
>> yielded 0, which eventually led to the crash.
>> 
>> Correcting the total_size calculation fixes the problem.
>> 
>> Reported-by: <quic_bjorande@quicinc.com>
>> Tested-by: <quic_bjorande@quicinc.com>
> 
> It would be nice to add "Bjorn Andersson" to these two.
> 

Oh yeah sorry about that. I’ll add it.   -Alex

> Regards,
> Bjorn
> 
>> Fixes: 9dd441e4ed57 ("soc: qcom: ipa: GSI transactions")
>> Signed-off-by: Alex Elder <elder@linaro.org>
>> ---
>> drivers/net/ipa/gsi_trans.c | 2 +-
>> 1 file changed, 1 insertion(+), 1 deletion(-)
>> 
>> diff --git a/drivers/net/ipa/gsi_trans.c b/drivers/net/ipa/gsi_trans.c
>> index 0f52c068c46d6..ee6fb00b71eb6 100644
>> --- a/drivers/net/ipa/gsi_trans.c
>> +++ b/drivers/net/ipa/gsi_trans.c
>> @@ -156,7 +156,7 @@ int gsi_trans_pool_init_dma(struct device *dev, struct gsi_trans_pool *pool,
>>     * gsi_trans_pool_exit_dma() can assume the total allocated
>>     * size is exactly (count * size).
>>     */
>> -    total_size = get_order(total_size) << PAGE_SHIFT;
>> +    total_size = PAGE_SIZE << get_order(total_size);
>> 
>>    virt = dma_alloc_coherent(dev, total_size, &addr, GFP_KERNEL);
>>    if (!virt)
>> -- 
>> 2.34.1
>> 

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

end of thread, other threads:[~2023-03-27 23:41 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-03-26 16:52 [PATCH net] net: ipa: compute DMA pool size properly Alex Elder
2023-03-27  7:36 ` Mark Bloch
2023-03-27 21:16 ` Bjorn Andersson
2023-03-27 23:40   ` Alex Elder

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox