public inbox for linux-usb@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] usb: bdc: allocate phys with main struct
@ 2026-04-11  4:12 Rosen Penev
  0 siblings, 0 replies; only message in thread
From: Rosen Penev @ 2026-04-11  4:12 UTC (permalink / raw)
  To: linux-usb
  Cc: Justin Chen, Al Cooper, Broadcom internal kernel review list,
	Greg Kroah-Hartman, Kees Cook, Gustavo A. R. Silva, open list,
	open list:KERNEL HARDENING (not covered by other areas):Keyword:b__counted_by(_le|_be)?b

Use a flexible array member to combine allocations and simplify code
slightly. No need for a branch deciding whether to allocate or not.

Add __counted_by for extra runtime analysis.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 drivers/usb/gadget/udc/bdc/bdc.h      |  2 +-
 drivers/usb/gadget/udc/bdc/bdc_core.c | 20 +++++++-------------
 2 files changed, 8 insertions(+), 14 deletions(-)

diff --git a/drivers/usb/gadget/udc/bdc/bdc.h b/drivers/usb/gadget/udc/bdc/bdc.h
index 2f4abf6f8f77..cc961161eb46 100644
--- a/drivers/usb/gadget/udc/bdc/bdc.h
+++ b/drivers/usb/gadget/udc/bdc/bdc.h
@@ -409,7 +409,6 @@ struct bdc {
 	spinlock_t	lock;
 
 	/* generic phy */
-	struct phy      **phys;
 	int num_phys;
 	/* num of endpoints for a particular instantiation of IP */
 	unsigned int num_eps;
@@ -453,6 +452,7 @@ struct bdc {
 	 */
 	struct delayed_work	func_wake_notify;
 	struct clk		*clk;
+	struct phy		*phys[] __counted_by(num_phys);
 };
 
 static inline u32 bdc_readl(void __iomem *base, u32 offset)
diff --git a/drivers/usb/gadget/udc/bdc/bdc_core.c b/drivers/usb/gadget/udc/bdc/bdc_core.c
index 438201dc96ca..4b16b85da450 100644
--- a/drivers/usb/gadget/udc/bdc/bdc_core.c
+++ b/drivers/usb/gadget/udc/bdc/bdc_core.c
@@ -487,14 +487,20 @@ static int bdc_probe(struct platform_device *pdev)
 	int irq;
 	u32 temp;
 	struct device *dev = &pdev->dev;
+	int num_phys;
 	int phy_num;
 
 	dev_dbg(dev, "%s()\n", __func__);
 
-	bdc = devm_kzalloc(dev, sizeof(*bdc), GFP_KERNEL);
+	num_phys = of_count_phandle_with_args(dev->of_node,
+						"phys", "#phy-cells");
+	bdc = devm_kzalloc(dev, struct_size(bdc, phys, num_phys), GFP_KERNEL);
 	if (!bdc)
 		return -ENOMEM;
 
+	bdc->num_phys = num_phys;
+	dev_info(dev, "Using %d phy(s)\n", bdc->num_phys);
+
 	bdc->regs = devm_platform_ioremap_resource(pdev, 0);
 	if (IS_ERR(bdc->regs))
 		return PTR_ERR(bdc->regs);
@@ -508,18 +514,6 @@ static int bdc_probe(struct platform_device *pdev)
 	bdc->dev = dev;
 	dev_dbg(dev, "bdc->regs: %p irq=%d\n", bdc->regs, bdc->irq);
 
-	bdc->num_phys = of_count_phandle_with_args(dev->of_node,
-						"phys", "#phy-cells");
-	if (bdc->num_phys > 0) {
-		bdc->phys = devm_kcalloc(dev, bdc->num_phys,
-					sizeof(struct phy *), GFP_KERNEL);
-		if (!bdc->phys)
-			return -ENOMEM;
-	} else {
-		bdc->num_phys = 0;
-	}
-	dev_info(dev, "Using %d phy(s)\n", bdc->num_phys);
-
 	for (phy_num = 0; phy_num < bdc->num_phys; phy_num++) {
 		bdc->phys[phy_num] = devm_of_phy_get_by_index(
 			dev, dev->of_node, phy_num);
-- 
2.53.0


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-04-11  4:13 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-11  4:12 [PATCH] usb: bdc: allocate phys with main struct Rosen Penev

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox