usb: mtu3: allocate phys with ssusb
Subsystems (3)
PATCH 1/1 usb: mtu3: allocate phys with ssusb
Use a flexible array member to combine allocations. Allows removal of a pointless branch. A size of 0 means phys are not allocated.
Add __counted_by for extra runtime analysis.
Signed-off-by Rosen Penev <[email protected]>
--- drivers/usb/mtu3/mtu3.h | 2 +- drivers/usb/mtu3/mtu3_plat.c | 18 ++++++------------ 2 files changed, 7 insertions(+), 13 deletions(-)
drivers/usb/mtu3/mtu3.h +1 -1
@@ -252,7 +252,6 @@ struct ssusb_mtk {struct mtu3 *u3d;void __iomem *mac_base;void __iomem *ippc_base; -struct phy **phys;int num_phys;int wakeup_irq;/* common power & clock */
@@ -272,6 +271,7 @@ struct ssusb_mtk {struct regmap *uwk;u32 uwk_reg_base;u32 uwk_vers; + struct phy *phys[] __counted_by(num_phys); };/**
drivers/usb/mtu3/mtu3_plat.c +6 -12
@@ -240,17 +240,6 @@ static int get_ssusb_rscs(struct platform_device *pdev, struct ssusb_mtk *ssusb)if (ret)return ret; - ssusb->num_phys =of_count_phandle_with_args (node, -"phys" ,"#phy-cells" ); -if (ssusb->num_phys >0 ) { - ssusb->phys =devm_kcalloc (dev, ssusb->num_phys, -sizeof (*ssusb->phys),GFP_KERNEL ); -if (!ssusb->phys) -return -ENOMEM ; - }else { - ssusb->num_phys =0 ; - } -for (i =0 ; i < ssusb->num_phys; i++) { ssusb->phys[i] =devm_of_phy_get_by_index (dev, node, i);if (IS_ERR (ssusb->phys[i])) {
@@ -330,12 +319,17 @@ static int mtu3_probe(struct platform_device *pdev)struct device *dev = &pdev->dev;struct ssusb_mtk *ssusb;int ret = -ENOMEM ; +int num_phys; + num_phys =of_count_phandle_with_args (dev->of_node, +"phys" ,"#phy-cells" );/* all elements are set to ZERO as default value */ - ssusb =devm_kzalloc (dev,sizeof (*ssusb),GFP_KERNEL ); + ssusb =devm_kzalloc (dev,struct_size (ssusb, phys, num_phys),GFP_KERNEL );if (!ssusb)return -ENOMEM ; + ssusb->num_phys = num_phys; + ret =dma_set_mask_and_coherent (dev,DMA_BIT_MASK (32 ));if (ret) {dev_err (dev,"No suitable DMA config available\n" );
Signature · 1 line
2.53.0
Greg Kroah-Hartman
maintainerQuoted 54 lines · Rosen Penev
Use a flexible array member to combine allocations. Allows removal of a pointless branch. A size of 0 means phys are not allocated.
Add
__counted_byfor extra runtime analysis.Signed-off-by Rosen Penev <[email protected]>--- drivers/usb/mtu3/mtu3.h | 2 +- drivers/usb/mtu3/mtu3_plat.c | 18 ++++++------------ 2 files changed, 7 insertions(+), 13 deletions(-)drivers/usb/mtu3/mtu3.h +1
-1@@ -252,7 +252,6 @@ struct ssusb_mtk {struct mtu3 *u3d;void __iomem *mac_base;void __iomem *ippc_base; -struct phy **phys;int num_phys;int wakeup_irq;/* common power & clock */ @@ -272,6 +271,7 @@ struct ssusb_mtk {struct regmap *uwk;u32 uwk_reg_base;u32 uwk_vers; + struct phy *phys[] __counted_by(num_phys); };/** drivers/usb/mtu3/mtu3_plat.c +0
-11@@ -240,17 +240,6 @@ static int get_ssusb_rscs(struct platform_device *pdev, struct ssusb_mtk *ssusb)if (ret)return ret; - ssusb->num_phys =of_count_phandle_with_args (node, -"phys" ,"#phy-cells" ); -if (ssusb->num_phys >0 ) { - ssusb->phys =devm_kcalloc (dev, ssusb->num_phys, -sizeof (*ssusb->phys),GFP_KERNEL ); -if (!ssusb->phys) -return -ENOMEM ; - }else { - ssusb->num_phys =0 ; - } -for (i =0 ; i < ssusb->num_phys; i++) { ssusb->phys[i] =devm_of_phy_get_by_index (dev, node, i);if (IS_ERR (ssusb->phys[i])) {
drivers/usb/mtu3/mtu3_plat.c static int mtu3_probe(struct platform_device *pdev) · line 325
Quoted 7 lines
drivers/usb/mtu3/mtu3_plat.c +3
-0@@ -330,12 +319,17 @@ static int mtu3_probe(struct platform_device *pdev)struct device *dev = &pdev->dev;struct ssusb_mtk *ssusb;int ret = -ENOMEM ; +int num_phys; + num_phys =of_count_phandle_with_args (dev->of_node, +"phys" ,"#phy-cells" );
You just caused a bug here if this returns a negative value.
How was this tested?
And why is it even needed?
thanks,
greg k-h