hsi: hsi_core: use kzalloc_flex
PATCH v2 1/1 hsi: hsi_core: use kzalloc_flex
Simplifies allocations by using a flexible array member in this struct.
Add __counted_by to get extra runtime analysis.
---
v2: remove wrong null check
drivers/hsi/hsi_core.c | 38 +++++++++++++++----------------------- include/linux/hsi/hsi.h | 2 +- 2 files changed, 16 insertions(+), 24 deletions(-)
drivers/hsi/hsi_core.c +15 -23
@@ -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)); }/**
@@ -446,7 +444,7 @@ void hsi_put_controller(struct hsi_controller *hsi)return ;for (i =0 ; i < hsi->num_ports; i++) -if (hsi->port && hsi->port[i]) +if (hsi->port[i])put_device (&hsi->port[i]->device);put_device (&hsi->device); }
@@ -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); }
include/linux/hsi/hsi.h +1 -1
@@ -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
Hi,
Quoted 16 lines · Rosen Penev
Simplifies allocations by using a flexible array member in this struct.
Add
__counted_byto get extra runtime analysis.Signed-off-by Rosen Penev <[email protected]>---
v2: remove wrong null checkdrivers/hsi/hsi_core.c | 38 +++++++++++++++----------------------- include/linux/hsi/hsi.h | 2 +- 2 files changed, 16 insertions(+), 24 deletions(-)drivers/hsi/hsi_core.c +0
-0
drivers/hsi/hsi_core.c static void hsi_controller_release(struct device *dev) · line 351
Quoted 10 lines
drivers/hsi/hsi_core.c +0
-2@@ -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));
Why is this free being removed? This is freeing hsi->port[i], which still has an extra allocation after your patch.
FWIW - it should be possible to further simplify things (in a potential follow-up patch) that also drops this extra allocation/free by simply embedding an array of 'struct hsi_port' instead of a pointer array in 'struct hsi_controller'.
Greetings,
-- Sebastian
Quoted 82 lines
drivers/hsi/hsi_core.c +15
-21}/** @@ -446,7 +444,7 @@ void hsi_put_controller(struct hsi_controller *hsi)return ;for (i =0 ; i < hsi->num_ports; i++) -if (hsi->port && hsi->port[i]) +if (hsi->port[i])put_device (&hsi->port[i]->device);put_device (&hsi->device); }@@ -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); }include/linux/hsi/hsi.h +1
-2@@ -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