Skip to content

Commit e61af3c

Browse files
nehebsre
authored andcommitted
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. Signed-off-by: Rosen Penev <rosenp@gmail.com> Link: https://patch.msgid.link/20260318191037.5661-1-rosenp@gmail.com Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.com>
1 parent 6de23f8 commit e61af3c

2 files changed

Lines changed: 16 additions & 23 deletions

File tree

drivers/hsi/hsi_core.c

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,6 @@ static void hsi_controller_release(struct device *dev)
342342
{
343343
struct hsi_controller *hsi = to_hsi_controller(dev);
344344

345-
kfree(hsi->port);
346345
kfree(hsi);
347346
}
348347

@@ -446,7 +445,7 @@ void hsi_put_controller(struct hsi_controller *hsi)
446445
return;
447446

448447
for (i = 0; i < hsi->num_ports; i++)
449-
if (hsi->port && hsi->port[i])
448+
if (hsi->port[i])
450449
put_device(&hsi->port[i]->device);
451450
put_device(&hsi->device);
452451
}
@@ -462,39 +461,33 @@ EXPORT_SYMBOL_GPL(hsi_put_controller);
462461
struct hsi_controller *hsi_alloc_controller(unsigned int n_ports, gfp_t flags)
463462
{
464463
struct hsi_controller *hsi;
465-
struct hsi_port **port;
466464
unsigned int i;
467465

468466
if (!n_ports)
469467
return NULL;
470468

471-
hsi = kzalloc_obj(*hsi, flags);
469+
hsi = kzalloc_flex(*hsi, port, n_ports, flags);
472470
if (!hsi)
473471
return NULL;
474-
port = kzalloc_objs(*port, n_ports, flags);
475-
if (!port) {
476-
kfree(hsi);
477-
return NULL;
478-
}
472+
479473
hsi->num_ports = n_ports;
480-
hsi->port = port;
481474
hsi->device.release = hsi_controller_release;
482475
device_initialize(&hsi->device);
483476

484477
for (i = 0; i < n_ports; i++) {
485-
port[i] = kzalloc_obj(**port, flags);
486-
if (port[i] == NULL)
478+
hsi->port[i] = kzalloc_obj(**hsi->port, flags);
479+
if (hsi->port[i] == NULL)
487480
goto out;
488-
port[i]->num = i;
489-
port[i]->async = hsi_dummy_msg;
490-
port[i]->setup = hsi_dummy_cl;
491-
port[i]->flush = hsi_dummy_cl;
492-
port[i]->start_tx = hsi_dummy_cl;
493-
port[i]->stop_tx = hsi_dummy_cl;
494-
port[i]->release = hsi_dummy_cl;
495-
mutex_init(&port[i]->lock);
496-
BLOCKING_INIT_NOTIFIER_HEAD(&port[i]->n_head);
497-
dev_set_name(&port[i]->device, "port%d", i);
481+
hsi->port[i]->num = i;
482+
hsi->port[i]->async = hsi_dummy_msg;
483+
hsi->port[i]->setup = hsi_dummy_cl;
484+
hsi->port[i]->flush = hsi_dummy_cl;
485+
hsi->port[i]->start_tx = hsi_dummy_cl;
486+
hsi->port[i]->stop_tx = hsi_dummy_cl;
487+
hsi->port[i]->release = hsi_dummy_cl;
488+
mutex_init(&hsi->port[i]->lock);
489+
BLOCKING_INIT_NOTIFIER_HEAD(&hsi->port[i]->n_head);
490+
dev_set_name(&hsi->port[i]->device, "port%d", i);
498491
hsi->port[i]->device.release = hsi_port_release;
499492
device_initialize(&hsi->port[i]->device);
500493
}

include/linux/hsi/hsi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ struct hsi_controller {
271271
struct module *owner;
272272
unsigned int id;
273273
unsigned int num_ports;
274-
struct hsi_port **port;
274+
struct hsi_port *port[] __counted_by(num_ports);
275275
};
276276

277277
#define to_hsi_controller(dev) container_of(dev, struct hsi_controller, device)

0 commit comments

Comments
 (0)