crypto: img-hash: clean up probe
cover crypto: img-hash: clean up probe
Move some devm calls early in order to avoid goto and return directly. Also use non devm request_irq so that free_irq can be called at the right time.
Rosen Penev (2):
crypto: img-hash: fetch resources into locals before probe body
crypto: img-hash: fix IRQ teardown ordering and fetch clocks early
drivers/crypto/img-hash.c | 75 +++++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 35 deletions(-)
Signature · 1 line
2.55.0
PATCH 1/2 crypto: img-hash: fetch resources into locals before probe body
Fetch the IRQ, iomem resources, and clocks at the start of img_hash_probe and assign them to locals, then copy into hdev after it is allocated. This keeps resource acquisition together at the top of probe and allows direct return instead of having to use goto. These can also return -EPROBE_DEFER so that helps to avoid pointless work.
--- drivers/crypto/img-hash.c | 65 ++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 32 deletions(-)
drivers/crypto/img-hash.c +33 -32
@@ -923,9 +923,37 @@ static int img_hash_probe(struct platform_device *pdev)struct img_hash_dev *hdev;struct device *dev = &pdev->dev;struct resource *hash_res; -int irq; +void __iomem *cpu_addr; +void __iomem *io_base; +struct clk *hash_clk; +struct clk *sys_clk; +int irq;int err; + irq =platform_get_irq (pdev,0 ); +if (irq <0 ) +return irq; + + io_base =devm_platform_ioremap_resource (pdev,0 ); +if (IS_ERR (io_base)) +return PTR_ERR (io_base); + + cpu_addr =devm_platform_get_and_ioremap_resource (pdev,1 , &hash_res); +if (IS_ERR (cpu_addr)) +return PTR_ERR (cpu_addr); + + hash_clk =devm_clk_get_enabled (&pdev->dev,"hash" ); +if (IS_ERR (hash_clk)) { +dev_err (dev,"clock initialization failed.\n" ); +return PTR_ERR (hash_clk); + } + + sys_clk =devm_clk_get_enabled (&pdev->dev,"sys" ); +if (IS_ERR (sys_clk)) { +dev_err (dev,"clock initialization failed.\n" ); +return PTR_ERR (sys_clk); + } + hdev =devm_kzalloc (dev,sizeof (*hdev),GFP_KERNEL );if (hdev ==NULL )return -ENOMEM ;
@@ -944,25 +972,12 @@ static int img_hash_probe(struct platform_device *pdev)crypto_init_queue (&hdev->queue,IMG_HASH_QUEUE_LENGTH );/* Register bank */ - hdev->io_base =devm_platform_ioremap_resource (pdev,0 ); -if (IS_ERR (hdev->io_base)) { - err =PTR_ERR (hdev->io_base); - goto res_err; - } - + hdev->io_base = io_base;/* Write port (DMA or CPU) */ - hdev->cpu_addr =devm_platform_get_and_ioremap_resource (pdev,1 , &hash_res); -if (IS_ERR (hdev->cpu_addr)) { - err =PTR_ERR (hdev->cpu_addr); - goto res_err; - } + hdev->cpu_addr = cpu_addr; hdev->bus_addr = hash_res->start; - - irq =platform_get_irq (pdev,0 ); -if (irq <0 ) { - err = irq; - goto res_err; - } + hdev->hash_clk = hash_clk; + hdev->sys_clk = sys_clk; err =devm_request_irq (dev, irq, img_irq_handler,0 ,dev_name (dev), hdev);
@@ -970,20 +985,6 @@ static int img_hash_probe(struct platform_device *pdev) goto res_err;dev_dbg (dev,"using IRQ channel %d\n" , irq); - hdev->hash_clk =devm_clk_get_enabled (&pdev->dev,"hash" ); -if (IS_ERR (hdev->hash_clk)) { -dev_err (dev,"clock initialization failed.\n" ); - err =PTR_ERR (hdev->hash_clk); - goto res_err; - } - - hdev->sys_clk =devm_clk_get_enabled (&pdev->dev,"sys" ); -if (IS_ERR (hdev->sys_clk)) { -dev_err (dev,"clock initialization failed.\n" ); - err =PTR_ERR (hdev->sys_clk); - goto res_err; - } - err =img_hash_dma_init (hdev);if (err) goto res_err;
Signature · 1 line
2.55.0
PATCH 2/2 crypto: img-hash: fix IRQ teardown ordering and fetch clocks early
In reply to Rosen PenevThe IRQ handler schedules tasklets, so free_irq must run before tasklet_kill in img_hash_remove(). devm_request_irq only freed the IRQ during devm unwind, after remove() returned, leaving a race in which the handler could re-schedule a tasklet during teardown. Replace it with request_irq and free the IRQ explicitly in remove() and in the probe error path.
--- drivers/crypto/img-hash.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/crypto/img-hash.c +7 -3
@@ -125,6 +125,7 @@ struct img_hash_dev {spinlock_t lock;int err; +int irq;struct tasklet_struct done_task;struct tasklet_struct dma_task;
@@ -978,16 +979,16 @@ static int img_hash_probe(struct platform_device *pdev) hdev->bus_addr = hash_res->start; hdev->hash_clk = hash_clk; hdev->sys_clk = sys_clk; + hdev->irq = irq; - err =devm_request_irq (dev, irq, img_irq_handler,0 , -dev_name (dev), hdev); + err =request_irq (hdev->irq, img_irq_handler,0 ,dev_name (dev), hdev);if (err) goto res_err;dev_dbg (dev,"using IRQ channel %d\n" , irq); err =img_hash_dma_init (hdev);if (err) - goto res_err; + goto err_irq;dev_dbg (dev,"using %s for DMA transfers\n" ,dma_chan_name (hdev->dma_lch));
@@ -1008,6 +1009,8 @@ static int img_hash_probe(struct platform_device *pdev)list_del (&hdev->list);spin_unlock (&img_hash.lock);dma_release_channel (hdev->dma_lch); +err_irq: +free_irq (hdev->irq, hdev); res_err:tasklet_kill (&hdev->done_task);tasklet_kill (&hdev->dma_task);
@@ -1026,6 +1029,7 @@ static void img_hash_remove(struct platform_device *pdev)img_unregister_algs (hdev); +free_irq (hdev->irq, hdev);tasklet_kill (&hdev->done_task);tasklet_kill (&hdev->dma_task);
Signature · 1 line
2.55.0
Herbert Xu
maintainerRe: [PATCH 2/2] crypto: img-hash: fix IRQ teardown ordering and fetch clocks early
Quoted 7 lines · Rosen Penev
The IRQ handler schedules tasklets, so
free_irqmust run beforetasklet_killinimg_hash_remove().devm_request_irqonly freed the IRQ during devm unwind, afterremove()returned, leaving a race in which the handler could re-schedule a tasklet during teardown. Replace it withrequest_irqand free the IRQ explicitly inremove()and in the probe error path.
Quoted 5 lines
Assisted-by opencode:deepseek-v4-flash-freeSigned-off-by Rosen Penev <[email protected]>--- drivers/crypto/img-hash.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
There are much bigger problems with device removal while the Crypto API is actively using the tfm. There is no point in fixing these issues until the bigger problem is addressed first.
Thanks,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt