[PATCH v2 1/5] ata: libata-transport: instantiate struct ata_internal statically

Heiner Kallweit
2026-04-02T13:29:26Z

Struct ata_internal is only instantiated once, in module init code.
So we can also instantiate it statically, which allows simplifying
the code.

Reviewed-by: Damien Le Moal <dlemoal@kernel.org>
Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
---
drivers/ata/libata-core.c | 11 ++-----
drivers/ata/libata-transport.c | 57 +++++++++++++++-------------------
drivers/ata/libata-transport.h | 2 +-
3 files changed, 28 insertions(+), 42 deletions(-)

diff --git a/drivers/ata/libata-core.c b/drivers/ata/libata-core.c
index 81479ddb8..ae56567af 100644
--- a/drivers/ata/libata-core.c
+++ b/drivers/ata/libata-core.c
@@ -6779,22 +6779,15 @@ static int __init ata_init(void)

libata_transport_init();
ata_scsi_transport_template = ata_attach_transport();
- if (!ata_scsi_transport_template) {
- ata_sff_exit();
- rc = -ENOMEM;
- goto err_out;
- }

printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
- return 0;

-err_out:
- return rc;
+ return 0;
}

static void __exit ata_exit(void)
{
- ata_release_transport(ata_scsi_transport_template);
+ ata_release_transport();
libata_transport_exit();
ata_sff_exit();
ata_free_force_param();
diff --git a/drivers/ata/libata-transport.c b/drivers/ata/libata-transport.c
index 7ad180265..0c7ce278e 100644
--- a/drivers/ata/libata-transport.c
+++ b/drivers/ata/libata-transport.c
@@ -745,6 +745,23 @@ int ata_tlink_add(struct ata_link *link)
return error;
}

+static struct ata_internal ata_transport_internal = {
+ .t.eh_strategy_handler = ata_scsi_error,
+ .t.user_scan = ata_scsi_user_scan,
+
+ .t.host_attrs.ac.class = &ata_port_class.class,
+ .t.host_attrs.ac.grp = &ata_port_attr_group,
+ .t.host_attrs.ac.match = ata_tport_match,
+
+ .link_attr_cont.ac.class = &ata_link_class.class,
+ .link_attr_cont.ac.grp = &ata_link_attr_group,
+ .link_attr_cont.ac.match = ata_tlink_match,
+
+ .dev_attr_cont.ac.class = &ata_dev_class.class,
+ .dev_attr_cont.ac.grp = &ata_device_attr_group,
+ .dev_attr_cont.ac.match = ata_tdev_match,
+};
+
/*
* Setup / Teardown code
*/
@@ -754,46 +771,22 @@ int ata_tlink_add(struct ata_link *link)
*/
struct scsi_transport_template *ata_attach_transport(void)
{
- struct ata_internal *i;
-
- i = kzalloc_obj(struct ata_internal);
- if (!i)
- return NULL;
+ transport_container_register(&ata_transport_internal.t.host_attrs);
+ transport_container_register(&ata_transport_internal.link_attr_cont);
+ transport_container_register(&ata_transport_internal.dev_attr_cont);

- i->t.eh_strategy_handler = ata_scsi_error;
- i->t.user_scan = ata_scsi_user_scan;
-
- i->t.host_attrs.ac.class = &ata_port_class.class;
- i->t.host_attrs.ac.grp = &ata_port_attr_group;
- i->t.host_attrs.ac.match = ata_tport_match;
- transport_container_register(&i->t.host_attrs);
-
- i->link_attr_cont.ac.class = &ata_link_class.class;
- i->link_attr_cont.ac.grp = &ata_link_attr_group;
- i->link_attr_cont.ac.match = ata_tlink_match;
- transport_container_register(&i->link_attr_cont);
-
- i->dev_attr_cont.ac.class = &ata_dev_class.class;
- i->dev_attr_cont.ac.grp = &ata_device_attr_group;
- i->dev_attr_cont.ac.match = ata_tdev_match;
- transport_container_register(&i->dev_attr_cont);
-
- return &i->t;
+ return &ata_transport_internal.t;
}

/**
* ata_release_transport -- release ATA transport template instance
* @t: transport template instance
*/
-void ata_release_transport(struct scsi_transport_template *t)
+void ata_release_transport(void)
{
- struct ata_internal *i = to_ata_internal(t);
-
- transport_container_unregister(&i->t.host_attrs);
- transport_container_unregister(&i->link_attr_cont);
- transport_container_unregister(&i->dev_attr_cont);
-
- kfree(i);
+ transport_container_unregister(&ata_transport_internal.t.host_attrs);
+ transport_container_unregister(&ata_transport_internal.link_attr_cont);
+ transport_container_unregister(&ata_transport_internal.dev_attr_cont);
}

__init int libata_transport_init(void)
diff --git a/drivers/ata/libata-transport.h b/drivers/ata/libata-transport.h
index 50cd2cbe8..a464b8fcd 100644
--- a/drivers/ata/libata-transport.h
+++ b/drivers/ata/libata-transport.h
@@ -9,7 +9,7 @@ int ata_tlink_add(struct ata_link *link);
void ata_tlink_delete(struct ata_link *link);

struct scsi_transport_template *ata_attach_transport(void);
-void ata_release_transport(struct scsi_transport_template *t);
+void ata_release_transport(void);

__init int libata_transport_init(void);
void __exit libata_transport_exit(void);
--
2.53.0