net: mvneta_bm: fix gen_pool_free address for BPPI
Subsystems (3)
PATCH 1/1 net: mvneta_bm: fix gen_pool_free address for BPPI
From: Chenguang Zhao <[email protected]>
gen_pool_free() expects the virtual address returned by gen_pool_dma_alloc(), not the physical address. Passing phys can miss the chunk and trigger BUG() on remove or probe rollback.
--- drivers/net/ethernet/marvell/mvneta_bm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/marvell/mvneta_bm.c +1 -1
@@ -389,7 +389,7 @@ static int mvneta_bm_get_sram(struct device_node *dn,static void mvneta_bm_put_sram (struct mvneta_bm *priv) { -gen_pool_free (priv->bppi_pool, priv->bppi_phys_addr, +gen_pool_free (priv->bppi_pool, (unsigned long )priv->bppi_virt_addr,MVNETA_BM_BPPI_SIZE ); }
Signature · 1 line
2.25.1
Quoted 17 lines · Chenguang Zhao
From: Chenguang Zhao <[email protected]>
gen_pool_free()expects the virtual address returned bygen_pool_dma_alloc(), not the physical address. Passing phys can miss the chunk and triggerBUG()on remove or probe rollback.Fixes dc35a10f68d3 ("net: mvneta: bm: add support for hardware buffer management")Signed-off-by Chenguang Zhao <[email protected]>--- drivers/net/ethernet/marvell/mvneta_bm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/net/ethernet/marvell/mvneta_bm.c +0
-0
drivers/net/ethernet/marvell/mvneta_bm.c static int mvneta_bm_get_sram(struct device_node *dn, · line 392
Quoted 5 lines
drivers/net/ethernet/marvell/mvneta_bm.c +1
-1@@ -389,7 +389,7 @@ static int mvneta_bm_get_sram(struct device_node *dn,static void mvneta_bm_put_sram (struct mvneta_bm *priv) { - gen_pool_free(priv->bppi_pool, priv->bppi_phys_addr, + gen_pool_free(priv->bppi_pool, (unsigned long)priv->bppi_virt_addr,
The cast is ugly, but it also seems correct. However, can the API be improved. If the intention is
gen_pool_free(priv->bppi_pool,
gen_pool_dma_alloc(priv->bppi_pool,
MVNETA_BM_BPPI_SIZE,
&priv->bppi_phys_addr),
MVNETA_BM_BPPI_SIZE);maybe gen_pool_free() should be changed to take a void *?
Andrew
Quoted 27 lines · Andrew Lunn
Quoted 17 lines · Chenguang Zhao
From: Chenguang Zhao <[email protected]>
gen_pool_free()expects the virtual address returned bygen_pool_dma_alloc(), not the physical address. Passing phys can miss the chunk and triggerBUG()on remove or probe rollback.Fixes dc35a10f68d3 ("net: mvneta: bm: add support for hardware buffer management")Signed-off-by Chenguang Zhao <[email protected]>--- drivers/net/ethernet/marvell/mvneta_bm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/net/ethernet/marvell/mvneta_bm.c +0
-0drivers/net/ethernet/marvell/mvneta_bm.c static int mvneta_bm_get_sram(struct device_node *dn, · line 392
Quoted 5 lines
drivers/net/ethernet/marvell/mvneta_bm.c +1
-1@@ -389,7 +389,7 @@ static int mvneta_bm_get_sram(struct device_node *dn,static void mvneta_bm_put_sram (struct mvneta_bm *priv) { - gen_pool_free(priv->bppi_pool, priv->bppi_phys_addr, + gen_pool_free(priv->bppi_pool, (unsigned long)priv->bppi_virt_addr,The cast is ugly, but it also seems correct. However, can the API be improved. If the intention is
gen_pool_free(priv->bppi_pool,
Quoted 8 lines
gen_pool_dma_alloc(priv->bppi_pool, MVNETA_BM_BPPI_SIZE, &priv->bppi_phys_addr), MVNETA_BM_BPPI_SIZE);maybe
gen_pool_free()should be changed to take a void *?Andrew
Thanks for the suggestion.
The cast is indeed a bit ugly, but several other drivers free memory from gen_pool_dma_alloc() in the same way today. Changing gen_pool_free() itself to take a void * would touch quite a few call sites, including ones that use genalloc for non-pointer cookies, so that may be a larger change than we want for this fix.
If we do want to improve the API, perhaps a small helper would be enough, for example:
static inline void gen_pool_dma_free(struct gen_pool *pool, void *vaddr, size_t size)
{
gen_pool_free(pool, (unsigned long)vaddr, size);
}
Does that sound reasonable to you? I'm happy to keep this bugfix as-is
for now, or follow up with such a helper if preferred.
Chenguang
Quoted 9 lines
Quoted 9 lines
The cast is ugly, but it also seems correct. However, can the API be improved. If the intention is
gen_pool_free(priv->bppi_pool,
gen_pool_dma_alloc(priv->bppi_pool, MVNETA_BM_BPPI_SIZE, &priv->bppi_phys_addr), MVNETA_BM_BPPI_SIZE);
Quoted 7 lines
Quoted 3 lines
maybe
gen_pool_free()should be changed to take a void *?Andrew
Thanks for the suggestion.
The cast is indeed a bit ugly, but several other drivers free memory from
gen_pool_dma_alloc()in the same way today.
Flip it around. Are there many calls which don't have the cast?
Quoted 4 lines
Changing
gen_pool_free()
itself to take a void * would touch quite a few call sites, including ones that use genalloc for non-pointer cookies, so that may be a larger change than we want for this fix.
I agree it is more than just the fix here. But this fix is adding yet another instance where maybe bad design is forcing a cast.
It should also be quite a mechanical change. Can Coccinelle do the change needed?
Andrew