Skip to contentSkip to conversation

usb: typec: qcom-pmic-typec: simplify allocation

PATCH Superseded Rosen Penev 2 participants 6 messages Mar 6 – Mar 12

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: typec: qcom-pmic-typec: simplify allocation

Change kzalloc + kcalloc to just kzalloc with a flexible array member.

Add __counted_by for extra runtime analysis when requested.

Shuffle some code in probe to provide a clearer picture.

---
 .../typec/tcpm/qcom/qcom_pmic_typec_pdphy.c   | 27 ++++++++-----------
 .../typec/tcpm/qcom/qcom_pmic_typec_port.c    | 26 ++++++++----------
 2 files changed, 22 insertions(+), 31 deletions(-)
drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_pdphy.c +11 -16
index c8b1463e6e8b..4b3915c6894a 100644
@@ -95,13 +95,13 @@ struct pmic_typec_pdphy {
 	struct regmap			*regmap;
 	u32				base;
 
-	unsigned int			nr_irqs;
-	struct pmic_typec_pdphy_irq_data	*irq_data;
-
 	struct work_struct		reset_work;
 	struct work_struct		receive_work;
 	struct regulator		*vdd_pdphy;
 	spinlock_t			lock;		/* Register atomicity */
+
+	unsigned int			nr_irqs;
+	struct pmic_typec_pdphy_irq_data	irq_data[] __counted_by(nr_irqs);
 };
 
 static void qcom_pmic_typec_pdphy_reset_on(struct pmic_typec_pdphy *pmic_typec_pdphy)
@@ -560,31 +560,26 @@ int qcom_pmic_typec_pdphy_probe(struct platform_device *pdev,
 	struct pmic_typec_pdphy_irq_data *irq_data;
 	int i, ret, irq;
 
-	pmic_typec_pdphy = devm_kzalloc(dev, sizeof(*pmic_typec_pdphy), GFP_KERNEL);
-	if (!pmic_typec_pdphy)
-		return -ENOMEM;
-
 	if (!res->nr_irqs || res->nr_irqs > PMIC_PDPHY_MAX_IRQS)
 		return -EINVAL;
 
-	irq_data = devm_kcalloc(dev, res->nr_irqs, sizeof(*irq_data),
-				GFP_KERNEL);
-	if (!irq_data)
+	pmic_typec_pdphy = devm_kzalloc(dev, struct_size(pmic_typec_pdphy, irq_data, res->nr_irqs), GFP_KERNEL);
+	if (!pmic_typec_pdphy)
 		return -ENOMEM;
 
+	pmic_typec_pdphy->nr_irqs = res->nr_irqs;
+	pmic_typec_pdphy->dev = dev;
+	pmic_typec_pdphy->base = base;
+	pmic_typec_pdphy->regmap = regmap;
 	pmic_typec_pdphy->vdd_pdphy = devm_regulator_get(dev, "vdd-pdphy");
 	if (IS_ERR(pmic_typec_pdphy->vdd_pdphy))
 		return PTR_ERR(pmic_typec_pdphy->vdd_pdphy);
 
-	pmic_typec_pdphy->dev = dev;
-	pmic_typec_pdphy->base = base;
-	pmic_typec_pdphy->regmap = regmap;
-	pmic_typec_pdphy->nr_irqs = res->nr_irqs;
-	pmic_typec_pdphy->irq_data = irq_data;
 	spin_lock_init(&pmic_typec_pdphy->lock);
 	INIT_WORK(&pmic_typec_pdphy->reset_work, qcom_pmic_typec_pdphy_sig_reset_work);
 
-	for (i = 0; i < res->nr_irqs; i++, irq_data++) {
+	for (i = 0; i < res->nr_irqs; i++) {
+		irq_data = &pmic_typec_pdphy->irq_data[i];
 		irq = platform_get_irq_byname(pdev, res->irq_params[i].irq_name);
 		if (irq < 0)
 			return irq;
drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_port.c +11 -15
index 8051eaa46991..7c5cf8061f04 100644
@@ -169,8 +169,6 @@ struct pmic_typec_port {
 	struct tcpm_port		*tcpm_port;
 	struct regmap			*regmap;
 	u32				base;
-	unsigned int			nr_irqs;
-	struct pmic_typec_port_irq_data	*irq_data;
 
 	struct regulator		*vdd_vbus;
 	bool				vbus_enabled;
@@ -181,6 +179,9 @@ struct pmic_typec_port {
 	struct delayed_work		cc_debounce_dwork;
 
 	spinlock_t			lock;	/* Register atomicity */
+
+	unsigned int			nr_irqs;
+	struct pmic_typec_port_irq_data	irq_data[] __counted_by(nr_irqs);
 };
 
 static const char * const typec_cc_status_name[] = {
@@ -706,29 +707,23 @@ int qcom_pmic_typec_port_probe(struct platform_device *pdev,
 	struct pmic_typec_port *pmic_typec_port;
 	int i, ret, irq;
 
-	pmic_typec_port = devm_kzalloc(dev, sizeof(*pmic_typec_port), GFP_KERNEL);
-	if (!pmic_typec_port)
-		return -ENOMEM;
-
 	if (!res->nr_irqs || res->nr_irqs > PMIC_TYPEC_MAX_IRQS)
 		return -EINVAL;
 
-	irq_data = devm_kcalloc(dev, res->nr_irqs, sizeof(*irq_data),
-				GFP_KERNEL);
-	if (!irq_data)
+	pmic_typec_port = devm_kzalloc(dev, struct_size(pmic_typec_port, irq_data, res->nr_irqs), GFP_KERNEL);
+	if (!pmic_typec_port)
 		return -ENOMEM;
 
 	mutex_init(&pmic_typec_port->vbus_lock);
 
+	pmic_typec_port->nr_irqs = res->nr_irqs;
+	pmic_typec_port->dev = dev;
+	pmic_typec_port->base = base;
+	pmic_typec_port->regmap = regmap;
 	pmic_typec_port->vdd_vbus = devm_regulator_get(dev, "vdd-vbus");
 	if (IS_ERR(pmic_typec_port->vdd_vbus))
 		return PTR_ERR(pmic_typec_port->vdd_vbus);
 
-	pmic_typec_port->dev = dev;
-	pmic_typec_port->base = base;
-	pmic_typec_port->regmap = regmap;
-	pmic_typec_port->nr_irqs = res->nr_irqs;
-	pmic_typec_port->irq_data = irq_data;
 	spin_lock_init(&pmic_typec_port->lock);
 	INIT_DELAYED_WORK(&pmic_typec_port->cc_debounce_dwork,
 			  qcom_pmic_typec_port_cc_debounce);
@@ -737,7 +732,8 @@ int qcom_pmic_typec_port_probe(struct platform_device *pdev,
 	if (irq < 0)
 		return irq;
 
-	for (i = 0; i < res->nr_irqs; i++, irq_data++) {
+	for (i = 0; i < res->nr_irqs; i++) {
+		irq_data = &pmic_typec_port->irq_data[i];
 		irq = platform_get_irq_byname(pdev,
 					      res->irq_params[i].irq_name);
 		if (irq < 0)
Signature · 1 line
2.53.0
#1 lore raw
Quoted 5 lines · Rosen Penev

Change kzalloc + kcalloc to just kzalloc with a flexible array member.

Add __counted_by for extra runtime analysis when requested.

Shuffle some code in probe to provide a clearer picture.

That's a lot of different things all at once, please do not do that. Remember, kernel changes need to be "one patch per logical thing".

thanks,

greg k-h

#2 lore raw
Quoted 10 lines · Greg Kroah-Hartman
Quoted 5 lines · Rosen Penev

Change kzalloc + kcalloc to just kzalloc with a flexible array member.

Add __counted_by for extra runtime analysis when requested.

Shuffle some code in probe to provide a clearer picture.

That's a lot of different things all at once, please do not do that. Remember, kernel changes need to be "one patch per logical thing".

Maybe I overstated it. It's not a huge change. By shuffle I mean move assignments up, which is partially a requirement of __counted_by

Quoted 4 lines

thanks,

greg k-h

#3 lore raw
In reply to Rosen Penev
Quoted 38 lines · Rosen Penev

Change kzalloc + kcalloc to just kzalloc with a flexible array member.

Add __counted_by for extra runtime analysis when requested.

Shuffle some code in probe to provide a clearer picture.

---
 .../typec/tcpm/qcom/qcom_pmic_typec_pdphy.c   | 27 ++++++++-----------
 .../typec/tcpm/qcom/qcom_pmic_typec_port.c    | 26 ++++++++----------
 2 files changed, 22 insertions(+), 31 deletions(-)
drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_pdphy.c +3 -4
index c8b1463e6e8b..4b3915c6894a 100644
@@ -95,13 +95,13 @@ struct pmic_typec_pdphy {
 	struct regmap			*regmap;
 	u32				base;
 
-	unsigned int			nr_irqs;
-	struct pmic_typec_pdphy_irq_data	*irq_data;
-
 	struct work_struct		reset_work;
 	struct work_struct		receive_work;
 	struct regulator		*vdd_pdphy;
 	spinlock_t			lock;		/* Register atomicity */
+
+	unsigned int			nr_irqs;
+	struct pmic_typec_pdphy_irq_data	irq_data[] __counted_by(nr_irqs);
 };
 
 static void qcom_pmic_typec_pdphy_reset_on(struct pmic_typec_pdphy *pmic_typec_pdphy)
@@ -560,31 +560,26 @@ int qcom_pmic_typec_pdphy_probe(struct platform_device *pdev,
 	struct pmic_typec_pdphy_irq_data *irq_data;
 	int i, ret, irq;
 
-	pmic_typec_pdphy = devm_kzalloc(dev, sizeof(*pmic_typec_pdphy), GFP_KERNEL);

drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_pdphy.c int qcom_pmic_typec_pdphy_probe(struct platform_device *pdev, · line 566

Quoted 10 lines
drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_pdphy.c +1 -6
-	if (!pmic_typec_pdphy)
-		return -ENOMEM;
-
 	if (!res->nr_irqs || res->nr_irqs > PMIC_PDPHY_MAX_IRQS)
 		return -EINVAL;
 
-	irq_data = devm_kcalloc(dev, res->nr_irqs, sizeof(*irq_data),
-				GFP_KERNEL);
-	if (!irq_data)
+	pmic_typec_pdphy = devm_kzalloc(dev, struct_size(pmic_typec_pdphy, irq_data, res->nr_irqs), GFP_KERNEL);

extra long line, checkpatch.pl didn't complain about this?

drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_port.c

Quoted 29 lines
drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_pdphy.c +7 -6
+	if (!pmic_typec_pdphy)
 		return -ENOMEM;
 
+	pmic_typec_pdphy->nr_irqs = res->nr_irqs;
+	pmic_typec_pdphy->dev = dev;
+	pmic_typec_pdphy->base = base;
+	pmic_typec_pdphy->regmap = regmap;
 	pmic_typec_pdphy->vdd_pdphy = devm_regulator_get(dev, "vdd-pdphy");
 	if (IS_ERR(pmic_typec_pdphy->vdd_pdphy))
 		return PTR_ERR(pmic_typec_pdphy->vdd_pdphy);
 
-	pmic_typec_pdphy->dev = dev;
-	pmic_typec_pdphy->base = base;
-	pmic_typec_pdphy->regmap = regmap;
-	pmic_typec_pdphy->nr_irqs = res->nr_irqs;
-	pmic_typec_pdphy->irq_data = irq_data;
 	spin_lock_init(&pmic_typec_pdphy->lock);
 	INIT_WORK(&pmic_typec_pdphy->reset_work, qcom_pmic_typec_pdphy_sig_reset_work);
 
-	for (i = 0; i < res->nr_irqs; i++, irq_data++) {
+	for (i = 0; i < res->nr_irqs; i++) {
+		irq_data = &pmic_typec_pdphy->irq_data[i];
 		irq = platform_get_irq_byname(pdev, res->irq_params[i].irq_name);
 		if (irq < 0)
 			return irq;
drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_port.c +0 -0
index 8051eaa46991..7c5cf8061f04 100644

Can you just convert one driver at a time please? This should be a patch series.

thanks,

greg k-h

#4 lore raw
Quoted 72 lines · Greg Kroah-Hartman
Quoted 38 lines · Rosen Penev

Change kzalloc + kcalloc to just kzalloc with a flexible array member.

Add __counted_by for extra runtime analysis when requested.

Shuffle some code in probe to provide a clearer picture.

---
 .../typec/tcpm/qcom/qcom_pmic_typec_pdphy.c   | 27 ++++++++-----------
 .../typec/tcpm/qcom/qcom_pmic_typec_port.c    | 26 ++++++++----------
 2 files changed, 22 insertions(+), 31 deletions(-)
drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_pdphy.c +3 -4
index c8b1463e6e8b..4b3915c6894a 100644
@@ -95,13 +95,13 @@ struct pmic_typec_pdphy {
      struct regmap                   *regmap;
      u32                             base;
 
-     unsigned int                    nr_irqs;
-     struct pmic_typec_pdphy_irq_data        *irq_data;
-
      struct work_struct              reset_work;
      struct work_struct              receive_work;
      struct regulator                *vdd_pdphy;
      spinlock_t                      lock;           /* Register atomicity */
+
+     unsigned int                    nr_irqs;
+     struct pmic_typec_pdphy_irq_data        irq_data[] __counted_by(nr_irqs);
 };
 
 static void qcom_pmic_typec_pdphy_reset_on(struct pmic_typec_pdphy *pmic_typec_pdphy)
@@ -560,31 +560,26 @@ int qcom_pmic_typec_pdphy_probe(struct platform_device *pdev,
      struct pmic_typec_pdphy_irq_data *irq_data;
      int i, ret, irq;
 
-     pmic_typec_pdphy = devm_kzalloc(dev, sizeof(*pmic_typec_pdphy), GFP_KERNEL);

drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_pdphy.c int qcom_pmic_typec_pdphy_probe(struct platform_device *pdev, · line 566

Quoted 10 lines
drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_pdphy.c +1 -6
-     if (!pmic_typec_pdphy)
-             return -ENOMEM;
-
      if (!res->nr_irqs || res->nr_irqs > PMIC_PDPHY_MAX_IRQS)
              return -EINVAL;
 
-     irq_data = devm_kcalloc(dev, res->nr_irqs, sizeof(*irq_data),
-                             GFP_KERNEL);
-     if (!irq_data)
+     pmic_typec_pdphy = devm_kzalloc(dev, struct_size(pmic_typec_pdphy, irq_data, res->nr_irqs), GFP_KERNEL);

extra long line, checkpatch.pl didn't complain about this?

Quoted 19 lines
drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_pdphy.c +5 -5
+     if (!pmic_typec_pdphy)
              return -ENOMEM;
 
+     pmic_typec_pdphy->nr_irqs = res->nr_irqs;
+     pmic_typec_pdphy->dev = dev;
+     pmic_typec_pdphy->base = base;
+     pmic_typec_pdphy->regmap = regmap;
      pmic_typec_pdphy->vdd_pdphy = devm_regulator_get(dev, "vdd-pdphy");
      if (IS_ERR(pmic_typec_pdphy->vdd_pdphy))
              return PTR_ERR(pmic_typec_pdphy->vdd_pdphy);
 
-     pmic_typec_pdphy->dev = dev;
-     pmic_typec_pdphy->base = base;
-     pmic_typec_pdphy->regmap = regmap;
-     pmic_typec_pdphy->nr_irqs = res->nr_irqs;
-     pmic_typec_pdphy->irq_data = irq_data;
      spin_lock_init(&pmic_typec_pdphy->lock);
      INIT_WORK(&pmic_typec_pdphy->reset_work, qcom_pmic_typec_pdphy_sig_reset_work);
 
Quoted 13 lines

drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_port.c

Quoted 10 lines
drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_pdphy.c +2 -1
-     for (i = 0; i < res->nr_irqs; i++, irq_data++) {
+     for (i = 0; i < res->nr_irqs; i++) {
+             irq_data = &pmic_typec_pdphy->irq_data[i];
              irq = platform_get_irq_byname(pdev, res->irq_params[i].irq_name);
              if (irq < 0)
                      return irq;
drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_port.c +0 -0
index 8051eaa46991..7c5cf8061f04 100644

Can you just convert one driver at a time please? This should be a patch series.

This is not the same driver?

Quoted 4 lines

thanks,

greg k-h

#5 lore raw
Quoted 87 lines · Rosen Penev
Quoted 72 lines · Greg Kroah-Hartman
Quoted 38 lines · Rosen Penev

Change kzalloc + kcalloc to just kzalloc with a flexible array member.

Add __counted_by for extra runtime analysis when requested.

Shuffle some code in probe to provide a clearer picture.

---
 .../typec/tcpm/qcom/qcom_pmic_typec_pdphy.c   | 27 ++++++++-----------
 .../typec/tcpm/qcom/qcom_pmic_typec_port.c    | 26 ++++++++----------
 2 files changed, 22 insertions(+), 31 deletions(-)
drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_pdphy.c +3 -4
index c8b1463e6e8b..4b3915c6894a 100644
@@ -95,13 +95,13 @@ struct pmic_typec_pdphy {
      struct regmap                   *regmap;
      u32                             base;
 
-     unsigned int                    nr_irqs;
-     struct pmic_typec_pdphy_irq_data        *irq_data;
-
      struct work_struct              reset_work;
      struct work_struct              receive_work;
      struct regulator                *vdd_pdphy;
      spinlock_t                      lock;           /* Register atomicity */
+
+     unsigned int                    nr_irqs;
+     struct pmic_typec_pdphy_irq_data        irq_data[] __counted_by(nr_irqs);
 };
 
 static void qcom_pmic_typec_pdphy_reset_on(struct pmic_typec_pdphy *pmic_typec_pdphy)
@@ -560,31 +560,26 @@ int qcom_pmic_typec_pdphy_probe(struct platform_device *pdev,
      struct pmic_typec_pdphy_irq_data *irq_data;
      int i, ret, irq;
 
-     pmic_typec_pdphy = devm_kzalloc(dev, sizeof(*pmic_typec_pdphy), GFP_KERNEL);

drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_pdphy.c int qcom_pmic_typec_pdphy_probe(struct platform_device *pdev, · line 566

Quoted 10 lines
drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_pdphy.c +1 -6
-     if (!pmic_typec_pdphy)
-             return -ENOMEM;
-
      if (!res->nr_irqs || res->nr_irqs > PMIC_PDPHY_MAX_IRQS)
              return -EINVAL;
 
-     irq_data = devm_kcalloc(dev, res->nr_irqs, sizeof(*irq_data),
-                             GFP_KERNEL);
-     if (!irq_data)
+     pmic_typec_pdphy = devm_kzalloc(dev, struct_size(pmic_typec_pdphy, irq_data, res->nr_irqs), GFP_KERNEL);

extra long line, checkpatch.pl didn't complain about this?

Quoted 19 lines
drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_pdphy.c +5 -5
+     if (!pmic_typec_pdphy)
              return -ENOMEM;
 
+     pmic_typec_pdphy->nr_irqs = res->nr_irqs;
+     pmic_typec_pdphy->dev = dev;
+     pmic_typec_pdphy->base = base;
+     pmic_typec_pdphy->regmap = regmap;
      pmic_typec_pdphy->vdd_pdphy = devm_regulator_get(dev, "vdd-pdphy");
      if (IS_ERR(pmic_typec_pdphy->vdd_pdphy))
              return PTR_ERR(pmic_typec_pdphy->vdd_pdphy);
 
-     pmic_typec_pdphy->dev = dev;
-     pmic_typec_pdphy->base = base;
-     pmic_typec_pdphy->regmap = regmap;
-     pmic_typec_pdphy->nr_irqs = res->nr_irqs;
-     pmic_typec_pdphy->irq_data = irq_data;
      spin_lock_init(&pmic_typec_pdphy->lock);
      INIT_WORK(&pmic_typec_pdphy->reset_work, qcom_pmic_typec_pdphy_sig_reset_work);
 
Quoted 13 lines

drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_port.c

Quoted 10 lines
drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_pdphy.c +2 -1
-     for (i = 0; i < res->nr_irqs; i++, irq_data++) {
+     for (i = 0; i < res->nr_irqs; i++) {
+             irq_data = &pmic_typec_pdphy->irq_data[i];
              irq = platform_get_irq_byname(pdev, res->irq_params[i].irq_name);
              if (irq < 0)
                      return irq;
drivers/usb/typec/tcpm/qcom/qcom_pmic_typec_port.c +0 -0
index 8051eaa46991..7c5cf8061f04 100644

Can you just convert one driver at a time please? This should be a patch series.

Quoted 1 line

This is not the same driver?

It's two different files, with two different structure allocations, so I assumed it was two different ones. Either way, it should be two different patches :)

thanks,

greg k-h