public inbox for linux-usb@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] usb: typec: intel_pmc_mux: combine kzalloc + kcalloc
@ 2026-04-11  2:36 Rosen Penev
  2026-04-13  8:51 ` kernel test robot
  2026-04-15 12:56 ` Heikki Krogerus
  0 siblings, 2 replies; 4+ messages in thread
From: Rosen Penev @ 2026-04-11  2:36 UTC (permalink / raw)
  To: linux-usb
  Cc: Heikki Krogerus, 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

A flexible array member can be used to combine allocations and simplify
handling slightly.

Add __counted_by for extra runtime analysis.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 drivers/usb/typec/mux/intel_pmc_mux.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/drivers/usb/typec/mux/intel_pmc_mux.c b/drivers/usb/typec/mux/intel_pmc_mux.c
index 1698428654ab..8b0a3dd3f940 100644
--- a/drivers/usb/typec/mux/intel_pmc_mux.c
+++ b/drivers/usb/typec/mux/intel_pmc_mux.c
@@ -151,13 +151,14 @@ struct pmc_usb {
 	u8 num_ports;
 	struct device *dev;
 	struct intel_scu_ipc_dev *ipc;
-	struct pmc_usb_port *port;
 	struct acpi_device *iom_adev;
 	void __iomem *iom_base;
 	u32 iom_port_status_offset;
 	u8 iom_port_status_size;
 
 	struct dentry *dentry;
+
+	struct pmc_usb_port port[] __counted_by(num_ports);
 };
 
 static struct dentry *pmc_mux_debugfs_root;
@@ -731,27 +732,26 @@ static int pmc_usb_probe(struct platform_device *pdev)
 {
 	struct fwnode_handle *fwnode = NULL;
 	struct pmc_usb *pmc;
+	u8 num_ports;
 	int i = 0;
 	int ret;
 
-	pmc = devm_kzalloc(&pdev->dev, sizeof(*pmc), GFP_KERNEL);
-	if (!pmc)
-		return -ENOMEM;
-
 	device_for_each_child_node(&pdev->dev, fwnode)
-		pmc->num_ports++;
+		num_ports++;
 
 	/* The IOM microcontroller has a limitation of max 4 ports. */
-	if (pmc->num_ports > 4) {
+	if (num_ports > 4) {
 		dev_err(&pdev->dev, "driver limited to 4 ports\n");
 		return -ERANGE;
 	}
 
-	pmc->port = devm_kcalloc(&pdev->dev, pmc->num_ports,
-				 sizeof(struct pmc_usb_port), GFP_KERNEL);
-	if (!pmc->port)
+	pmc = devm_kzalloc(&pdev->dev, struct_size(pmc, port, num_ports),
+			GFP_KERNEL);
+	if (!pmc)
 		return -ENOMEM;
 
+	pmc->num_ports = num_ports;
+
 	pmc->ipc = devm_intel_scu_ipc_dev_get(&pdev->dev);
 	if (!pmc->ipc)
 		return -EPROBE_DEFER;
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] usb: typec: intel_pmc_mux: combine kzalloc + kcalloc
  2026-04-11  2:36 [PATCH] usb: typec: intel_pmc_mux: combine kzalloc + kcalloc Rosen Penev
@ 2026-04-13  8:51 ` kernel test robot
  2026-04-13 19:47   ` Rosen Penev
  2026-04-15 12:56 ` Heikki Krogerus
  1 sibling, 1 reply; 4+ messages in thread
From: kernel test robot @ 2026-04-13  8:51 UTC (permalink / raw)
  To: Rosen Penev, linux-usb

Hi Rosen,

kernel test robot noticed the following build warnings:

[auto build test WARNING on usb/usb-testing]
[also build test WARNING on usb/usb-next usb/usb-linus westeri-thunderbolt/next linus/master v7.0-rc7 next-20260410]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Rosen-Penev/usb-typec-intel_pmc_mux-combine-kzalloc-kcalloc/20260412-223212
base:   https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb.git usb-testing
patch link:    https://lore.kernel.org/r/20260411023642.146890-1-rosenp%40gmail.com
patch subject: [PATCH] usb: typec: intel_pmc_mux: combine kzalloc + kcalloc
config: x86_64-allmodconfig (https://download.01.org/0day-ci/archive/20260413/202604131654.9GEXISW5-lkp@intel.com/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260413/202604131654.9GEXISW5-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202604131654.9GEXISW5-lkp@intel.com/

All warnings (new ones prefixed by >>):

>> drivers/usb/typec/mux/intel_pmc_mux.c:740:3: warning: variable 'num_ports' is uninitialized when used here [-Wuninitialized]
     740 |                 num_ports++;
         |                 ^~~~~~~~~
   drivers/usb/typec/mux/intel_pmc_mux.c:735:14: note: initialize the variable 'num_ports' to silence this warning
     735 |         u8 num_ports;
         |                     ^
         |                      = '\0'
   1 warning generated.


vim +/num_ports +740 drivers/usb/typec/mux/intel_pmc_mux.c

   730	
   731	static int pmc_usb_probe(struct platform_device *pdev)
   732	{
   733		struct fwnode_handle *fwnode = NULL;
   734		struct pmc_usb *pmc;
   735		u8 num_ports;
   736		int i = 0;
   737		int ret;
   738	
   739		device_for_each_child_node(&pdev->dev, fwnode)
 > 740			num_ports++;
   741	
   742		/* The IOM microcontroller has a limitation of max 4 ports. */
   743		if (num_ports > 4) {
   744			dev_err(&pdev->dev, "driver limited to 4 ports\n");
   745			return -ERANGE;
   746		}
   747	
   748		pmc = devm_kzalloc(&pdev->dev, struct_size(pmc, port, num_ports),
   749				GFP_KERNEL);
   750		if (!pmc)
   751			return -ENOMEM;
   752	
   753		pmc->num_ports = num_ports;
   754	
   755		pmc->ipc = devm_intel_scu_ipc_dev_get(&pdev->dev);
   756		if (!pmc->ipc)
   757			return -EPROBE_DEFER;
   758	
   759		pmc->dev = &pdev->dev;
   760	
   761		ret = pmc_usb_probe_iom(pmc);
   762		if (ret)
   763			return ret;
   764	
   765		pmc->dentry = debugfs_create_dir(dev_name(pmc->dev), pmc_mux_debugfs_root);
   766	
   767		/*
   768		 * For every physical USB connector (USB2 and USB3 combo) there is a
   769		 * child ACPI device node under the PMC mux ACPI device object.
   770		 */
   771		for (i = 0; i < pmc->num_ports; i++) {
   772			fwnode = device_get_next_child_node(pmc->dev, fwnode);
   773			if (!fwnode)
   774				break;
   775	
   776			ret = pmc_usb_register_port(pmc, i, fwnode);
   777			if (ret) {
   778				fwnode_handle_put(fwnode);
   779				goto err_remove_ports;
   780			}
   781	
   782			pmc_mux_port_debugfs_init(&pmc->port[i]);
   783		}
   784	
   785		platform_set_drvdata(pdev, pmc);
   786	
   787		return 0;
   788	
   789	err_remove_ports:
   790		for (i = 0; i < pmc->num_ports; i++) {
   791			typec_switch_unregister(pmc->port[i].typec_sw);
   792			typec_mux_unregister(pmc->port[i].typec_mux);
   793			usb_role_switch_unregister(pmc->port[i].usb_sw);
   794		}
   795	
   796		acpi_dev_put(pmc->iom_adev);
   797	
   798		debugfs_remove(pmc->dentry);
   799	
   800		return ret;
   801	}
   802	

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] usb: typec: intel_pmc_mux: combine kzalloc + kcalloc
  2026-04-13  8:51 ` kernel test robot
@ 2026-04-13 19:47   ` Rosen Penev
  0 siblings, 0 replies; 4+ messages in thread
From: Rosen Penev @ 2026-04-13 19:47 UTC (permalink / raw)
  To: kernel test robot; +Cc: linux-usb

On Mon, Apr 13, 2026 at 1:52 AM kernel test robot <lkp@intel.com> wrote:
>
> Hi Rosen,
>
> kernel test robot noticed the following build warnings:
>
> [auto build test WARNING on usb/usb-testing]
> [also build test WARNING on usb/usb-next usb/usb-linus westeri-thunderbolt/next linus/master v7.0-rc7 next-20260410]
> [If your patch is applied to the wrong git tree, kindly drop us a note.
> And when submitting patch, we suggest to use '--base' as documented in
> https://git-scm.com/docs/git-format-patch#_base_tree_information]
>
> url:    https://github.com/intel-lab-lkp/linux/commits/Rosen-Penev/usb-typec-intel_pmc_mux-combine-kzalloc-kcalloc/20260412-223212
> base:   https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb.git usb-testing
> patch link:    https://lore.kernel.org/r/20260411023642.146890-1-rosenp%40gmail.com
> patch subject: [PATCH] usb: typec: intel_pmc_mux: combine kzalloc + kcalloc
> config: x86_64-allmodconfig (https://download.01.org/0day-ci/archive/20260413/202604131654.9GEXISW5-lkp@intel.com/config)
> compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
> reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260413/202604131654.9GEXISW5-lkp@intel.com/reproduce)
>
> If you fix the issue in a separate patch/commit (i.e. not just a new version of
> the same patch/commit), kindly add following tags
> | Reported-by: kernel test robot <lkp@intel.com>
> | Closes: https://lore.kernel.org/oe-kbuild-all/202604131654.9GEXISW5-lkp@intel.com/
>
> All warnings (new ones prefixed by >>):
>
> >> drivers/usb/typec/mux/intel_pmc_mux.c:740:3: warning: variable 'num_ports' is uninitialized when used here [-Wuninitialized]
>      740 |                 num_ports++;
>          |                 ^~~~~~~~~
>    drivers/usb/typec/mux/intel_pmc_mux.c:735:14: note: initialize the variable 'num_ports' to silence this warning
>      735 |         u8 num_ports;
>          |                     ^
>          |                      = '\0'
>    1 warning generated.
Yikes I forgot to do this. As the merge window is closed I assume this
needs to wait.
>
>
> vim +/num_ports +740 drivers/usb/typec/mux/intel_pmc_mux.c
>
>    730
>    731  static int pmc_usb_probe(struct platform_device *pdev)
>    732  {
>    733          struct fwnode_handle *fwnode = NULL;
>    734          struct pmc_usb *pmc;
>    735          u8 num_ports;
>    736          int i = 0;
>    737          int ret;
>    738
>    739          device_for_each_child_node(&pdev->dev, fwnode)
>  > 740                  num_ports++;
>    741
>    742          /* The IOM microcontroller has a limitation of max 4 ports. */
>    743          if (num_ports > 4) {
>    744                  dev_err(&pdev->dev, "driver limited to 4 ports\n");
>    745                  return -ERANGE;
>    746          }
>    747
>    748          pmc = devm_kzalloc(&pdev->dev, struct_size(pmc, port, num_ports),
>    749                          GFP_KERNEL);
>    750          if (!pmc)
>    751                  return -ENOMEM;
>    752
>    753          pmc->num_ports = num_ports;
>    754
>    755          pmc->ipc = devm_intel_scu_ipc_dev_get(&pdev->dev);
>    756          if (!pmc->ipc)
>    757                  return -EPROBE_DEFER;
>    758
>    759          pmc->dev = &pdev->dev;
>    760
>    761          ret = pmc_usb_probe_iom(pmc);
>    762          if (ret)
>    763                  return ret;
>    764
>    765          pmc->dentry = debugfs_create_dir(dev_name(pmc->dev), pmc_mux_debugfs_root);
>    766
>    767          /*
>    768           * For every physical USB connector (USB2 and USB3 combo) there is a
>    769           * child ACPI device node under the PMC mux ACPI device object.
>    770           */
>    771          for (i = 0; i < pmc->num_ports; i++) {
>    772                  fwnode = device_get_next_child_node(pmc->dev, fwnode);
>    773                  if (!fwnode)
>    774                          break;
>    775
>    776                  ret = pmc_usb_register_port(pmc, i, fwnode);
>    777                  if (ret) {
>    778                          fwnode_handle_put(fwnode);
>    779                          goto err_remove_ports;
>    780                  }
>    781
>    782                  pmc_mux_port_debugfs_init(&pmc->port[i]);
>    783          }
>    784
>    785          platform_set_drvdata(pdev, pmc);
>    786
>    787          return 0;
>    788
>    789  err_remove_ports:
>    790          for (i = 0; i < pmc->num_ports; i++) {
>    791                  typec_switch_unregister(pmc->port[i].typec_sw);
>    792                  typec_mux_unregister(pmc->port[i].typec_mux);
>    793                  usb_role_switch_unregister(pmc->port[i].usb_sw);
>    794          }
>    795
>    796          acpi_dev_put(pmc->iom_adev);
>    797
>    798          debugfs_remove(pmc->dentry);
>    799
>    800          return ret;
>    801  }
>    802
>
> --
> 0-DAY CI Kernel Test Service
> https://github.com/intel/lkp-tests/wiki

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] usb: typec: intel_pmc_mux: combine kzalloc + kcalloc
  2026-04-11  2:36 [PATCH] usb: typec: intel_pmc_mux: combine kzalloc + kcalloc Rosen Penev
  2026-04-13  8:51 ` kernel test robot
@ 2026-04-15 12:56 ` Heikki Krogerus
  1 sibling, 0 replies; 4+ messages in thread
From: Heikki Krogerus @ 2026-04-15 12:56 UTC (permalink / raw)
  To: Rosen Penev
  Cc: linux-usb, 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

Hi Rosen,

On Fri, Apr 10, 2026 at 07:36:42PM -0700, Rosen Penev wrote:
> A flexible array member can be used to combine allocations and simplify
> handling slightly.
> 
> Add __counted_by for extra runtime analysis.

One nitpick below.
Otherwise this looks okay to me.

> Signed-off-by: Rosen Penev <rosenp@gmail.com>
> ---
>  drivers/usb/typec/mux/intel_pmc_mux.c | 20 ++++++++++----------
>  1 file changed, 10 insertions(+), 10 deletions(-)
> 
> diff --git a/drivers/usb/typec/mux/intel_pmc_mux.c b/drivers/usb/typec/mux/intel_pmc_mux.c
> index 1698428654ab..8b0a3dd3f940 100644
> --- a/drivers/usb/typec/mux/intel_pmc_mux.c
> +++ b/drivers/usb/typec/mux/intel_pmc_mux.c
> @@ -151,13 +151,14 @@ struct pmc_usb {
>  	u8 num_ports;
>  	struct device *dev;
>  	struct intel_scu_ipc_dev *ipc;
> -	struct pmc_usb_port *port;
>  	struct acpi_device *iom_adev;
>  	void __iomem *iom_base;
>  	u32 iom_port_status_offset;
>  	u8 iom_port_status_size;
>  
>  	struct dentry *dentry;
> +
> +	struct pmc_usb_port port[] __counted_by(num_ports);
>  };
>  
>  static struct dentry *pmc_mux_debugfs_root;
> @@ -731,27 +732,26 @@ static int pmc_usb_probe(struct platform_device *pdev)
>  {
>  	struct fwnode_handle *fwnode = NULL;
>  	struct pmc_usb *pmc;
> +	u8 num_ports;
>  	int i = 0;
>  	int ret;
>  
> -	pmc = devm_kzalloc(&pdev->dev, sizeof(*pmc), GFP_KERNEL);
> -	if (!pmc)
> -		return -ENOMEM;
> -
>  	device_for_each_child_node(&pdev->dev, fwnode)
> -		pmc->num_ports++;
> +		num_ports++;
>  
>  	/* The IOM microcontroller has a limitation of max 4 ports. */
> -	if (pmc->num_ports > 4) {
> +	if (num_ports > 4) {
>  		dev_err(&pdev->dev, "driver limited to 4 ports\n");
>  		return -ERANGE;
>  	}
>  
> -	pmc->port = devm_kcalloc(&pdev->dev, pmc->num_ports,
> -				 sizeof(struct pmc_usb_port), GFP_KERNEL);
> -	if (!pmc->port)
> +	pmc = devm_kzalloc(&pdev->dev, struct_size(pmc, port, num_ports),
> +			GFP_KERNEL);

One line is enough for that.

> +	if (!pmc)
>  		return -ENOMEM;
>  
> +	pmc->num_ports = num_ports;
> +
>  	pmc->ipc = devm_intel_scu_ipc_dev_get(&pdev->dev);
>  	if (!pmc->ipc)
>  		return -EPROBE_DEFER;
> -- 
> 2.53.0

thanks,

-- 
heikki

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-04-15 12:57 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-11  2:36 [PATCH] usb: typec: intel_pmc_mux: combine kzalloc + kcalloc Rosen Penev
2026-04-13  8:51 ` kernel test robot
2026-04-13 19:47   ` Rosen Penev
2026-04-15 12:56 ` Heikki Krogerus

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