[PATCH] hsi: hsi_core: use kzalloc_flex

Rosen Penev posted 1 patch 3 weeks, 1 day ago
There is a newer version of this series
drivers/hsi/hsi_core.c  | 36 ++++++++++++++----------------------
include/linux/hsi/hsi.h |  2 +-
2 files changed, 15 insertions(+), 23 deletions(-)
[PATCH] hsi: hsi_core: use kzalloc_flex
Posted by Rosen Penev 3 weeks, 1 day ago
Simplifies allocations by using a flexible array member in this struct.

Add __counted_by to get extra runtime analysis.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 drivers/hsi/hsi_core.c  | 36 ++++++++++++++----------------------
 include/linux/hsi/hsi.h |  2 +-
 2 files changed, 15 insertions(+), 23 deletions(-)

diff --git a/drivers/hsi/hsi_core.c b/drivers/hsi/hsi_core.c
index 7cb2dcb30fdb..f1b84add291a 100644
--- a/drivers/hsi/hsi_core.c
+++ b/drivers/hsi/hsi_core.c
@@ -342,13 +342,11 @@ static void hsi_controller_release(struct device *dev)
 {
 	struct hsi_controller *hsi = to_hsi_controller(dev);
 
-	kfree(hsi->port);
 	kfree(hsi);
 }
 
 static void hsi_port_release(struct device *dev)
 {
-	kfree(to_hsi_port(dev));
 }
 
 /**
@@ -462,39 +460,33 @@ EXPORT_SYMBOL_GPL(hsi_put_controller);
 struct hsi_controller *hsi_alloc_controller(unsigned int n_ports, gfp_t flags)
 {
 	struct hsi_controller	*hsi;
-	struct hsi_port		**port;
 	unsigned int		i;
 
 	if (!n_ports)
 		return NULL;
 
-	hsi = kzalloc_obj(*hsi, flags);
+	hsi = kzalloc_flex(*hsi, port, n_ports, flags);
 	if (!hsi)
 		return NULL;
-	port = kzalloc_objs(*port, n_ports, flags);
-	if (!port) {
-		kfree(hsi);
-		return NULL;
-	}
+
 	hsi->num_ports = n_ports;
-	hsi->port = port;
 	hsi->device.release = hsi_controller_release;
 	device_initialize(&hsi->device);
 
 	for (i = 0; i < n_ports; i++) {
-		port[i] = kzalloc_obj(**port, flags);
-		if (port[i] == NULL)
+		hsi->port[i] = kzalloc_obj(**hsi->port, flags);
+		if (hsi->port[i] == NULL)
 			goto out;
-		port[i]->num = i;
-		port[i]->async = hsi_dummy_msg;
-		port[i]->setup = hsi_dummy_cl;
-		port[i]->flush = hsi_dummy_cl;
-		port[i]->start_tx = hsi_dummy_cl;
-		port[i]->stop_tx = hsi_dummy_cl;
-		port[i]->release = hsi_dummy_cl;
-		mutex_init(&port[i]->lock);
-		BLOCKING_INIT_NOTIFIER_HEAD(&port[i]->n_head);
-		dev_set_name(&port[i]->device, "port%d", i);
+		hsi->port[i]->num = i;
+		hsi->port[i]->async = hsi_dummy_msg;
+		hsi->port[i]->setup = hsi_dummy_cl;
+		hsi->port[i]->flush = hsi_dummy_cl;
+		hsi->port[i]->start_tx = hsi_dummy_cl;
+		hsi->port[i]->stop_tx = hsi_dummy_cl;
+		hsi->port[i]->release = hsi_dummy_cl;
+		mutex_init(&hsi->port[i]->lock);
+		BLOCKING_INIT_NOTIFIER_HEAD(&hsi->port[i]->n_head);
+		dev_set_name(&hsi->port[i]->device, "port%d", i);
 		hsi->port[i]->device.release = hsi_port_release;
 		device_initialize(&hsi->port[i]->device);
 	}
diff --git a/include/linux/hsi/hsi.h b/include/linux/hsi/hsi.h
index 6ca92bff02c6..ea6bef9b6012 100644
--- a/include/linux/hsi/hsi.h
+++ b/include/linux/hsi/hsi.h
@@ -271,7 +271,7 @@ struct hsi_controller {
 	struct module		*owner;
 	unsigned int		id;
 	unsigned int		num_ports;
-	struct hsi_port		**port;
+	struct hsi_port		*port[] __counted_by(num_ports);
 };
 
 #define to_hsi_controller(dev) container_of(dev, struct hsi_controller, device)
-- 
2.53.0
Re: [PATCH] hsi: hsi_core: use kzalloc_flex
Posted by kernel test robot 2 weeks, 6 days ago
Hi Rosen,

kernel test robot noticed the following build warnings:

[auto build test WARNING on sre-hsi/for-next]
[also build test WARNING on kees/for-next/kspp next-20260316]
[cannot apply to kees/for-next/pstore linus/master v6.16-rc1]
[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/hsi-hsi_core-use-kzalloc_flex/20260316-091623
base:   https://git.kernel.org/pub/scm/linux/kernel/git/sre/linux-hsi.git for-next
patch link:    https://lore.kernel.org/r/20260316004139.267912-1-rosenp%40gmail.com
patch subject: [PATCH] hsi: hsi_core: use kzalloc_flex
config: i386-randconfig-2006-20250804 (https://download.01.org/0day-ci/archive/20260317/202603171431.ThUSplSw-lkp@intel.com/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260317/202603171431.ThUSplSw-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/202603171431.ThUSplSw-lkp@intel.com/

All warnings (new ones prefixed by >>):

   drivers/hsi/hsi_core.c: In function 'hsi_put_controller':
>> drivers/hsi/hsi_core.c:447:21: warning: the comparison will always evaluate as 'true' for the address of 'port' will never be NULL [-Waddress]
     447 |                 if (hsi->port && hsi->port[i])
         |                     ^~~
   In file included from drivers/hsi/hsi_core.c:9:
   include/linux/hsi/hsi.h:274:34: note: 'port' declared here
     274 |         struct hsi_port         *port[] __counted_by(num_ports);
         |                                  ^~~~


vim +447 drivers/hsi/hsi_core.c

a056ab8c7a00a0f drivers/hsi/hsi.c Carlos Chinea 2010-04-16  429  
5a218ceba7b64f5 drivers/hsi/hsi.c Carlos Chinea 2012-04-04  430  /**
5a218ceba7b64f5 drivers/hsi/hsi.c Carlos Chinea 2012-04-04  431   * hsi_put_controller - Free an HSI controller
5a218ceba7b64f5 drivers/hsi/hsi.c Carlos Chinea 2012-04-04  432   *
5a218ceba7b64f5 drivers/hsi/hsi.c Carlos Chinea 2012-04-04  433   * @hsi: Pointer to the HSI controller to freed
5a218ceba7b64f5 drivers/hsi/hsi.c Carlos Chinea 2012-04-04  434   *
5a218ceba7b64f5 drivers/hsi/hsi.c Carlos Chinea 2012-04-04  435   * HSI controller drivers should only use this function if they need
5a218ceba7b64f5 drivers/hsi/hsi.c Carlos Chinea 2012-04-04  436   * to free their allocated hsi_controller structures before a successful
5a218ceba7b64f5 drivers/hsi/hsi.c Carlos Chinea 2012-04-04  437   * call to hsi_register_controller. Other use is not allowed.
5a218ceba7b64f5 drivers/hsi/hsi.c Carlos Chinea 2012-04-04  438   */
5a218ceba7b64f5 drivers/hsi/hsi.c Carlos Chinea 2012-04-04  439  void hsi_put_controller(struct hsi_controller *hsi)
5a218ceba7b64f5 drivers/hsi/hsi.c Carlos Chinea 2012-04-04  440  {
5a218ceba7b64f5 drivers/hsi/hsi.c Carlos Chinea 2012-04-04  441  	unsigned int i;
5a218ceba7b64f5 drivers/hsi/hsi.c Carlos Chinea 2012-04-04  442  
5a218ceba7b64f5 drivers/hsi/hsi.c Carlos Chinea 2012-04-04  443  	if (!hsi)
5a218ceba7b64f5 drivers/hsi/hsi.c Carlos Chinea 2012-04-04  444  		return;
5a218ceba7b64f5 drivers/hsi/hsi.c Carlos Chinea 2012-04-04  445  
5a218ceba7b64f5 drivers/hsi/hsi.c Carlos Chinea 2012-04-04  446  	for (i = 0; i < hsi->num_ports; i++)
5a218ceba7b64f5 drivers/hsi/hsi.c Carlos Chinea 2012-04-04 @447  		if (hsi->port && hsi->port[i])
5a218ceba7b64f5 drivers/hsi/hsi.c Carlos Chinea 2012-04-04  448  			put_device(&hsi->port[i]->device);
5a218ceba7b64f5 drivers/hsi/hsi.c Carlos Chinea 2012-04-04  449  	put_device(&hsi->device);
5a218ceba7b64f5 drivers/hsi/hsi.c Carlos Chinea 2012-04-04  450  }
5a218ceba7b64f5 drivers/hsi/hsi.c Carlos Chinea 2012-04-04  451  EXPORT_SYMBOL_GPL(hsi_put_controller);
5a218ceba7b64f5 drivers/hsi/hsi.c Carlos Chinea 2012-04-04  452  

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