From: Rosen Penev <rosenp@gmail.com>
To: linux-ide@vger.kernel.org
Cc: Damien Le Moal <dlemoal@kernel.org>,
Niklas Cassel <cassel@kernel.org>,
Hans de Goede <hansg@kernel.org>, Kees Cook <kees@kernel.org>,
"Gustavo A. R. Silva" <gustavoars@kernel.org>,
linux-kernel@vger.kernel.org (open list),
linux-hardening@vger.kernel.org (open list:KERNEL HARDENING (not
covered by other areas):Keyword:\b__counted_by(_le|_be)?\b)
Subject: [PATCH] ata: libahci_platform: remove kcalloc
Date: Thu, 26 Mar 2026 19:50:43 -0700 [thread overview]
Message-ID: <20260327025043.7565-1-rosenp@gmail.com> (raw)
Use a flexible array member with struct_size to combine the allocations.
Added __counted_by support for extra runtime analysis.
Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
drivers/ata/ahci.h | 3 ++-
drivers/ata/libahci_platform.c | 33 +++++++++++++++------------------
2 files changed, 17 insertions(+), 19 deletions(-)
diff --git a/drivers/ata/ahci.h b/drivers/ata/ahci.h
index 293b7fb216b5..9e8b6319025c 100644
--- a/drivers/ata/ahci.h
+++ b/drivers/ata/ahci.h
@@ -357,7 +357,6 @@ struct ahci_host_priv {
* If platform uses PHYs. There is a 1:1 relation between the port number and
* the PHY position in this array.
*/
- struct phy **phys;
unsigned nports; /* Number of ports */
void *plat_data; /* Other platform data */
unsigned int irq; /* interrupt line */
@@ -379,6 +378,8 @@ struct ahci_host_priv {
/* only required for per-port MSI(-X) support */
int (*get_irq_vector)(struct ata_host *host,
int port);
+
+ struct phy *phys[] __counted_by(nports);
};
/*
diff --git a/drivers/ata/libahci_platform.c b/drivers/ata/libahci_platform.c
index f37247e7a5cb..6e072d681341 100644
--- a/drivers/ata/libahci_platform.c
+++ b/drivers/ata/libahci_platform.c
@@ -482,15 +482,29 @@ struct ahci_host_priv *ahci_platform_get_resources(struct platform_device *pdev,
struct ahci_host_priv *hpriv;
u32 mask_port_map = 0;
u32 max_port;
+ int nports;
if (!devres_open_group(dev, NULL, GFP_KERNEL))
return ERR_PTR(-ENOMEM);
- hpriv = devres_alloc(ahci_platform_put_resources, sizeof(*hpriv),
+ /* find maximum port id for allocating structures */
+ max_port = ahci_platform_find_max_port_id(dev);
+ /*
+ * Set nports according to maximum port id. Clamp at
+ * AHCI_MAX_PORTS, warning message for invalid port id
+ * is generated later.
+ * When DT has no sub-nodes max_port is 0, nports is 1,
+ * in order to be able to use the
+ * ahci_platform_[en|dis]able_[phys|regulators] functions.
+ */
+ nports = min(AHCI_MAX_PORTS, max_port + 1);
+ hpriv = devres_alloc(ahci_platform_put_resources, struct_size(hpriv, phys, nports),
GFP_KERNEL);
if (!hpriv)
goto err_out;
+ hpriv->nports = nports;
+
devres_add(dev, hpriv);
/*
@@ -573,23 +587,6 @@ struct ahci_host_priv *ahci_platform_get_resources(struct platform_device *pdev,
goto err_out;
}
- /* find maximum port id for allocating structures */
- max_port = ahci_platform_find_max_port_id(dev);
- /*
- * Set nports according to maximum port id. Clamp at
- * AHCI_MAX_PORTS, warning message for invalid port id
- * is generated later.
- * When DT has no sub-nodes max_port is 0, nports is 1,
- * in order to be able to use the
- * ahci_platform_[en|dis]able_[phys|regulators] functions.
- */
- hpriv->nports = min(AHCI_MAX_PORTS, max_port + 1);
-
- hpriv->phys = devm_kcalloc(dev, hpriv->nports, sizeof(*hpriv->phys), GFP_KERNEL);
- if (!hpriv->phys) {
- rc = -ENOMEM;
- goto err_out;
- }
/*
* We cannot use devm_ here, since ahci_platform_put_resources() uses
* target_pwrs after devm_ have freed memory
--
2.53.0
next reply other threads:[~2026-03-27 2:51 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-27 2:50 Rosen Penev [this message]
2026-03-27 15:28 ` [PATCH] ata: libahci_platform: remove kcalloc Damien Le Moal
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260327025043.7565-1-rosenp@gmail.com \
--to=rosenp@gmail.com \
--cc=cassel@kernel.org \
--cc=dlemoal@kernel.org \
--cc=gustavoars@kernel.org \
--cc=hansg@kernel.org \
--cc=kees@kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-ide@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox