Message ID | 97c2bb1c9b69d0739da3762a7752ae6582c4ad02.1683390112.git.christophe.jaillet@wanadoo.fr (mailing list archive) |
---|---|
State | Accepted |
Commit | 923b138388928ade722cba86c55145f3dfac1cab |
Headers | show |
Series | dmaengine: mcf-edma: Use struct_size() | expand |
On Sat, May 06, 2023 at 06:22:06PM +0200, Christophe JAILLET wrote: > Use struct_size() instead of hand writing it. > This is less verbose and more informative. > > 'mcf_chan' is now unused and can be removed. In fact, it is shadowed by > another variable in the 'for' loop below. Keep this one. > > Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr> > --- > It will also help scripts when __counted_by macro will be added. > See [1]. > > [1]: https://lore.kernel.org/all/6453f739.170a0220.62695.7785@mx.google.com/ Of course, the main selling point of struct_size() for me is that it protects you against integer overflows. Open coding the math might end up giving you a size which is smaller than expected but struct_size() will give you ULONG_MAX in that same situation. The allocation will fail as expected. #Safe. Even when the open coded math is safe, this is easier to audit in an automated way. regards, dan carpenter
On Sat, 06 May 2023 18:22:06 +0200, Christophe JAILLET wrote: > Use struct_size() instead of hand writing it. > This is less verbose and more informative. > > 'mcf_chan' is now unused and can be removed. In fact, it is shadowed by > another variable in the 'for' loop below. Keep this one. > > > [...] Applied, thanks! [1/1] dmaengine: mcf-edma: Use struct_size() commit: 923b138388928ade722cba86c55145f3dfac1cab Best regards,
diff --git a/drivers/dma/mcf-edma.c b/drivers/dma/mcf-edma.c index ebd8733f72ad..28304dd8763a 100644 --- a/drivers/dma/mcf-edma.c +++ b/drivers/dma/mcf-edma.c @@ -180,9 +180,8 @@ static int mcf_edma_probe(struct platform_device *pdev) { struct mcf_edma_platform_data *pdata; struct fsl_edma_engine *mcf_edma; - struct fsl_edma_chan *mcf_chan; struct edma_regs *regs; - int ret, i, len, chans; + int ret, i, chans; pdata = dev_get_platdata(&pdev->dev); if (!pdata) { @@ -191,8 +190,8 @@ static int mcf_edma_probe(struct platform_device *pdev) } chans = pdata->dma_channels; - len = sizeof(*mcf_edma) + sizeof(*mcf_chan) * chans; - mcf_edma = devm_kzalloc(&pdev->dev, len, GFP_KERNEL); + mcf_edma = devm_kzalloc(&pdev->dev, struct_size(mcf_edma, chans, chans), + GFP_KERNEL); if (!mcf_edma) return -ENOMEM;
Use struct_size() instead of hand writing it. This is less verbose and more informative. 'mcf_chan' is now unused and can be removed. In fact, it is shadowed by another variable in the 'for' loop below. Keep this one. Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr> --- It will also help scripts when __counted_by macro will be added. See [1]. [1]: https://lore.kernel.org/all/6453f739.170a0220.62695.7785@mx.google.com/ --- drivers/dma/mcf-edma.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)