dmaengine: bestcomm: ioremap and get irq early
Subsystems (2)
PATCH 1/1 dmaengine: bestcomm: ioremap and get irq early
Doing so allows handling potential -EPROBE_DEFER before any work gets done.
Replace the open-coded resource lookup, request_mem_region, ioremap and the manual iounmap/release_mem_region cleanup in probe/remove with the managed devm_platform_get_and_ioremap_resource() helper. This removes the now-unused error-unmap/release paths and simplifies the driver.
dmaengine: bestcomm: get irq early
platform_get_irq can return -EPROBE_DEFER, which is useful to avoid doing extra work in such a case
--- drivers/dma/bestcomm/bestcomm.c | 70 ++++++---------------- include/linux/fsl/bestcomm/bestcomm_priv.h | 2 +- 2 files changed, 18 insertions(+), 54 deletions(-)
drivers/dma/bestcomm/bestcomm.c +17 -53
@@ -48,6 +48,7 @@ bcom_task_alloc(int bd_count, int bd_size, int priv_size) {int i, tasknum =-1 ;struct bcom_task *tsk; +int irq;/* Don't try to do anything if bestcomm init failed */ if (!bcom_eng)
@@ -68,6 +69,10 @@ bcom_task_alloc(int bd_count, int bd_size, int priv_size)if (tasknum <0 )return NULL ; + irq =platform_get_irq (bcom_eng->pdev, tasknum); +if (irq <0 ) +return irq; +/* Allocate our structure */ tsk =kzalloc (sizeof (struct bcom_task ) + priv_size,GFP_KERNEL );if (!tsk)
@@ -77,10 +82,7 @@ bcom_task_alloc(int bd_count, int bd_size, int priv_size)if (priv_size) tsk->priv = (void *)tsk +sizeof (struct bcom_task ); -/* Get IRQ of that task */ - tsk->irq =irq_of_parse_and_map (bcom_eng->ofnode, tsk->tasknum); -if (!tsk->irq) - goto error; + tsk->irq = irq;/* Init the BDs, if needed */ if (bd_count) {
@@ -102,8 +104,6 @@ bcom_task_alloc(int bd_count, int bd_size, int priv_size) error:if (tsk) { -if (tsk->irq) -irq_dispose_mapping (tsk->irq);bcom_sram_free (tsk->bd);kfree (tsk->cookie);kfree (tsk);
@@ -126,7 +126,6 @@ bcom_task_free(struct bcom_task *tsk) bcom_eng->tdt[tsk->tasknum].stop =0 ;/* Free everything */ -irq_dispose_mapping (tsk->irq);bcom_sram_free (tsk->bd);kfree (tsk->cookie);kfree (tsk);
@@ -365,23 +364,24 @@ bcom_engine_cleanup(void)static int mpc52xx_bcom_probe (struct platform_device *op) {struct device_node *ofn_sram; -struct resource res_bcom; +struct resource *res_bcom; +void __iomem *regs;int rv;/* Inform user we're ok so far */ printk (KERN_INFO "DMA: MPC52xx BestComm driver\n" ); -/* Get the bestcomm node */ -of_node_get (op->dev.of_node); + regs =devm_platform_get_and_ioremap_resource (op,0 , &res_bcom); +if (IS_ERR (regs)) +return PTR_ERR (regs);/* Prepare SRAM */ ofn_sram =of_find_matching_node (NULL , mpc52xx_sram_ids);if (!ofn_sram) { printk(KERN_ERR DRIVER_NAME ": " "No SRAM found in device tree\n"); - rv = -ENODEV ; - goto error_ofput; +return -ENODEV ; } rv =bcom_sram_init (ofn_sram,DRIVER_NAME );of_node_put (ofn_sram);
@@ -389,7 +389,7 @@ static int mpc52xx_bcom_probe(struct platform_device *op)if (rv) { printk(KERN_ERR DRIVER_NAME ": " "Error in SRAM init\n"); - goto error_ofput; +return rv; }/* Get a clean struct */
@@ -400,37 +400,14 @@ static int mpc52xx_bcom_probe(struct platform_device *op) }/* Save the node */ - bcom_eng->ofnode = op->dev.of_node; - -/* Get, reserve & map io */ -if (of_address_to_resource (op->dev.of_node,0 , &res_bcom)) { - printk(KERN_ERR DRIVER_NAME ": " - "Can't get resource\n"); - rv = -EINVAL ; - goto error_sramclean; - } - -if (!request_mem_region (res_bcom.start,resource_size (&res_bcom), -DRIVER_NAME )) { - printk(KERN_ERR DRIVER_NAME ": " - "Can't request registers region\n"); - rv = -EBUSY ; - goto error_sramclean; - } - - bcom_eng->regs_base = res_bcom.start; - bcom_eng->regs =ioremap (res_bcom.start,sizeof (struct mpc52xx_sdma )); -if (!bcom_eng->regs) { - printk(KERN_ERR DRIVER_NAME ": " - "Can't map registers\n"); - rv = -ENOMEM ; - goto error_release; - } + bcom_eng->pdev = op; + bcom_eng->regs = regs; + bcom_eng->regs_base = res_bcom->start;/* Now, do the real init */ rv =bcom_engine_init ();if (rv) - goto error_unmap; + goto error_sramclean;/* Done ! */ printk(KERN_INFO "DMA: MPC52xx BestComm engine @%08lx ok !\n",
@@ -439,15 +416,9 @@ static int mpc52xx_bcom_probe(struct platform_device *op)return 0 ;/* Error path */ -error_unmap: -iounmap (bcom_eng->regs); -error_release: -release_mem_region (res_bcom.start,sizeof (struct mpc52xx_sdma )); error_sramclean:kfree (bcom_eng);bcom_sram_cleanup (); -error_ofput: -of_node_put (op->dev.of_node);printk (KERN_ERR "DMA: MPC52xx BestComm init failed !\n" );
@@ -463,13 +434,6 @@ static void mpc52xx_bcom_remove(struct platform_device *op)/* Cleanup SRAM */ bcom_sram_cleanup (); -/* Release regs */ -iounmap (bcom_eng->regs); -release_mem_region (bcom_eng->regs_base,sizeof (struct mpc52xx_sdma )); - -/* Release the node */ -of_node_put (bcom_eng->ofnode); -/* Release memory */ kfree (bcom_eng); bcom_eng =NULL ;
include/linux/fsl/bestcomm/bestcomm_priv.h +1 -1
@@ -66,7 +66,7 @@ struct bcom_tdt {* This holds all info needed globaly to handle the engine */ struct bcom_engine { -struct device_node *ofnode; +struct platform_device *pdev;struct mpc52xx_sdma __iomem *regs;phys_addr_t regs_base;
Signature · 1 line
2.55.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
New issues:
- [High] bcom_task_alloc() returns an integer error code implicitly cast to a pointer, which bypasses caller NULL checks and causes a kernel panic.
- [High] bcom_task_alloc() permanently leaks a reserved task index if platform_get_irq() fails.
Pre-existing issues:
- [High] Probe error path frees the global bcom_eng pointer without setting it to NULL, leaving a dangling pointer. --
--- Patch [1]: [PATCH] dmaengine: bestcomm: ioremap and get irq early --- commit f02c723e90824bdf0feee229bfc42b04e0be6dc0
Author: Rosen Penev <[email protected]>
dmaengine: bestcomm: ioremap and get irq early
This patch refactors the MPC52xx BestComm driver to use managed resources for memory mapping and to acquire the IRQ earlier. By doing so, it allows handling potential probe deferrals before allocating resources, simplifying the cleanup paths.
Quoted 4 lines
drivers/dma/bestcomm/bestcomm.c +0
-0
[ ... ]
drivers/dma/bestcomm/bestcomm.c bcom_task_alloc(int bd_count, int bd_size, int priv_size) · line 74
Quoted 7 lines
drivers/dma/bestcomm/bestcomm.c +3
-0@@ -68,6 +69,10 @@ bcom_task_alloc(int bd_count, int bd_size, int priv_size)if (tasknum <0 )return NULL ; + irq =platform_get_irq (bcom_eng->pdev, tasknum); +if (irq <0 ) +return irq;
[Severity: High]
Does returning the integer irq here implicitly cast the negative error code into a pointer? If platform_get_irq() returns -EPROBE_DEFER, callers such as bcom_fec_rx_init() that check the return value with if (!tsk) will assume success and dereference the invalid pointer.
[Severity: High]
Does this early return also permanently leak the reserved task index? The task index was reserved earlier in bcom_task_alloc() by setting bcom_eng->tdt[i].stop = 0xfffffffful;.
By returning directly instead of using the error: label, the cleanup code that clears the reservation (bcom_eng->tdt[tasknum].stop = 0;) is bypassed. Since the total pool size is strictly limited to 16 tasks, repeated probe deferrals will rapidly exhaust the pool.
Quoted 3 lines
drivers/dma/bestcomm/bestcomm.c +1
-0+/* Allocate our structure */ tsk =kzalloc (sizeof (struct bcom_task ) + priv_size,GFP_KERNEL );
[ ... ]
drivers/dma/bestcomm/bestcomm.c static int mpc52xx_bcom_probe(struct platform_device *op) · line 420
Quoted 10 lines
drivers/dma/bestcomm/bestcomm.c +0
-4@@ -439,15 +416,9 @@ static int mpc52xx_bcom_probe(struct platform_device *op)return 0 ;/* Error path */ -error_unmap: -iounmap (bcom_eng->regs); -error_release: -release_mem_region (res_bcom.start,sizeof (struct mpc52xx_sdma )); error_sramclean:kfree (bcom_eng);
[Severity: High]
This is a pre-existing issue, but does freeing the globally accessible bcom_eng pointer here without setting it to NULL leave a dangling pointer?
Functions like bcom_task_alloc() use if (!bcom_eng) as a safety readiness check. If the probe fails and hits this error path, subsequent or concurrent API calls will bypass the check and dereference the freed memory.
Quoted 6 lines
drivers/dma/bestcomm/bestcomm.c +0
-2bcom_sram_cleanup (); -error_ofput: -of_node_put (op->dev.of_node);printk (KERN_ERR "DMA: MPC52xx BestComm init failed !\n" );
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 8 lines · Rosen Penev
Doing so allows handling potential -EPROBE_DEFER before any work gets done.
Replace the open-coded resource lookup,
request_mem_region, ioremap and the manual iounmap/release_mem_region cleanup in probe/remove with the manageddevm_platform_get_and_ioremap_resource()helper. This removes the now-unused error-unmap/release paths and simplifies the driver.
Quoted 4 lines
Assisted-by opencode:hy3-freeSigned-off-by Rosen Penev <[email protected]>dmaengine: bestcomm: get irq early
make irq change as seperate patch
Quoted 189 lines
platform_get_irqcan return -EPROBE_DEFER, which is useful to avoid doing extra work in such a caseSigned-off-by Rosen Penev <[email protected]>--- drivers/dma/bestcomm/bestcomm.c | 70 ++++++---------------- include/linux/fsl/bestcomm/bestcomm_priv.h | 2 +- 2 files changed, 18 insertions(+), 54 deletions(-)drivers/dma/bestcomm/bestcomm.c +17
-53@@ -48,6 +48,7 @@ bcom_task_alloc(int bd_count, int bd_size, int priv_size) {int i, tasknum =-1 ;struct bcom_task *tsk; +int irq;/* Don't try to do anything if bestcomm init failed */ if (!bcom_eng)@@ -68,6 +69,10 @@ bcom_task_alloc(int bd_count, int bd_size, int priv_size)if (tasknum <0 )return NULL ; + irq =platform_get_irq (bcom_eng->pdev, tasknum); +if (irq <0 ) +return irq; +/* Allocate our structure */ tsk =kzalloc (sizeof (struct bcom_task ) + priv_size,GFP_KERNEL );if (!tsk)@@ -77,10 +82,7 @@ bcom_task_alloc(int bd_count, int bd_size, int priv_size)if (priv_size) tsk->priv = (void *)tsk +sizeof (struct bcom_task ); -/* Get IRQ of that task */ - tsk->irq =irq_of_parse_and_map (bcom_eng->ofnode, tsk->tasknum); -if (!tsk->irq) - goto error; + tsk->irq = irq;/* Init the BDs, if needed */ if (bd_count) {@@ -102,8 +104,6 @@ bcom_task_alloc(int bd_count, int bd_size, int priv_size) error:if (tsk) { -if (tsk->irq) -irq_dispose_mapping (tsk->irq);bcom_sram_free (tsk->bd);kfree (tsk->cookie);kfree (tsk);@@ -126,7 +126,6 @@ bcom_task_free(struct bcom_task *tsk) bcom_eng->tdt[tsk->tasknum].stop =0 ;/* Free everything */ -irq_dispose_mapping (tsk->irq);bcom_sram_free (tsk->bd);kfree (tsk->cookie);kfree (tsk);@@ -365,23 +364,24 @@ bcom_engine_cleanup(void)static int mpc52xx_bcom_probe (struct platform_device *op) {struct device_node *ofn_sram; -struct resource res_bcom; +struct resource *res_bcom; +void __iomem *regs;int rv;/* Inform user we're ok so far */ printk (KERN_INFO "DMA: MPC52xx BestComm driver\n" ); -/* Get the bestcomm node */ -of_node_get (op->dev.of_node); + regs =devm_platform_get_and_ioremap_resource (op,0 , &res_bcom); +if (IS_ERR (regs)) +return PTR_ERR (regs);/* Prepare SRAM */ ofn_sram =of_find_matching_node (NULL , mpc52xx_sram_ids);if (!ofn_sram) { printk(KERN_ERR DRIVER_NAME ": " "No SRAM found in device tree\n"); - rv = -ENODEV ; - goto error_ofput; +return -ENODEV ; } rv =bcom_sram_init (ofn_sram,DRIVER_NAME );of_node_put (ofn_sram);@@ -389,7 +389,7 @@ static int mpc52xx_bcom_probe(struct platform_device *op)if (rv) { printk(KERN_ERR DRIVER_NAME ": " "Error in SRAM init\n"); - goto error_ofput; +return rv; }/* Get a clean struct */ @@ -400,37 +400,14 @@ static int mpc52xx_bcom_probe(struct platform_device *op) }/* Save the node */ - bcom_eng->ofnode = op->dev.of_node; - -/* Get, reserve & map io */ -if (of_address_to_resource (op->dev.of_node,0 , &res_bcom)) { - printk(KERN_ERR DRIVER_NAME ": " - "Can't get resource\n"); - rv = -EINVAL ; - goto error_sramclean; - } - -if (!request_mem_region (res_bcom.start,resource_size (&res_bcom), -DRIVER_NAME )) { - printk(KERN_ERR DRIVER_NAME ": " - "Can't request registers region\n"); - rv = -EBUSY ; - goto error_sramclean; - } - - bcom_eng->regs_base = res_bcom.start; - bcom_eng->regs =ioremap (res_bcom.start,sizeof (struct mpc52xx_sdma )); -if (!bcom_eng->regs) { - printk(KERN_ERR DRIVER_NAME ": " - "Can't map registers\n"); - rv = -ENOMEM ; - goto error_release; - } + bcom_eng->pdev = op; + bcom_eng->regs = regs; + bcom_eng->regs_base = res_bcom->start;/* Now, do the real init */ rv =bcom_engine_init ();if (rv) - goto error_unmap; + goto error_sramclean;/* Done ! */ printk(KERN_INFO "DMA: MPC52xx BestComm engine @%08lx ok !\n",@@ -439,15 +416,9 @@ static int mpc52xx_bcom_probe(struct platform_device *op)return 0 ;/* Error path */ -error_unmap: -iounmap (bcom_eng->regs); -error_release: -release_mem_region (res_bcom.start,sizeof (struct mpc52xx_sdma )); error_sramclean:kfree (bcom_eng);bcom_sram_cleanup (); -error_ofput: -of_node_put (op->dev.of_node);printk (KERN_ERR "DMA: MPC52xx BestComm init failed !\n" );@@ -463,13 +434,6 @@ static void mpc52xx_bcom_remove(struct platform_device *op)/* Cleanup SRAM */ bcom_sram_cleanup (); -/* Release regs */ -iounmap (bcom_eng->regs); -release_mem_region (bcom_eng->regs_base,sizeof (struct mpc52xx_sdma )); - -/* Release the node */ -of_node_put (bcom_eng->ofnode); -/* Release memory */ kfree (bcom_eng); bcom_eng =NULL ;include/linux/fsl/bestcomm/bestcomm_priv.h +1
-2@@ -66,7 +66,7 @@ struct bcom_tdt {* This holds all info needed globaly to handle the engine */ struct bcom_engine { -struct device_node *ofnode; +struct platform_device *pdev;struct mpc52xx_sdma __iomem *regs;phys_addr_t regs_base; --2.55.0