public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Rosen Penev <rosenp@gmail.com>
To: linux-kernel@vger.kernel.org
Cc: Stephen Boyd <sboyd@kernel.org>
Subject: [PATCH] spmi: use kzalloc_flex in main allocation
Date: Thu, 26 Mar 2026 19:41:43 -0700	[thread overview]
Message-ID: <20260327024143.6764-1-rosenp@gmail.com> (raw)

Add a flexible array member to avoid indexing past the struct.

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

diff --git a/drivers/spmi/spmi.c b/drivers/spmi/spmi.c
index e889b129f3ac..57b7c0cb4240 100644
--- a/drivers/spmi/spmi.c
+++ b/drivers/spmi/spmi.c
@@ -450,7 +450,7 @@ struct spmi_controller *spmi_controller_alloc(struct device *parent,
 	if (WARN_ON(!parent))
 		return ERR_PTR(-EINVAL);
 
-	ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL);
+	ctrl = kzalloc_flex(*ctrl, priv, size);
 	if (!ctrl)
 		return ERR_PTR(-ENOMEM);
 
@@ -459,7 +459,7 @@ struct spmi_controller *spmi_controller_alloc(struct device *parent,
 	ctrl->dev.bus = &spmi_bus_type;
 	ctrl->dev.parent = parent;
 	ctrl->dev.of_node = parent->of_node;
-	spmi_controller_set_drvdata(ctrl, &ctrl[1]);
+	spmi_controller_set_drvdata(ctrl, ctrl->priv);
 
 	id = ida_alloc(&ctrl_ida, GFP_KERNEL);
 	if (id < 0) {
diff --git a/include/linux/spmi.h b/include/linux/spmi.h
index 28e8c8bd3944..ddeeaee52ca7 100644
--- a/include/linux/spmi.h
+++ b/include/linux/spmi.h
@@ -76,6 +76,7 @@ void spmi_device_remove(struct spmi_device *sdev);
  * @cmd:	sends a non-data command sequence on the SPMI bus.
  * @read_cmd:	sends a register read command sequence on the SPMI bus.
  * @write_cmd:	sends a register write command sequence on the SPMI bus.
+ * @priv:	array of private data.
  */
 struct spmi_controller {
 	struct device		dev;
@@ -85,6 +86,7 @@ struct spmi_controller {
 			    u8 sid, u16 addr, u8 *buf, size_t len);
 	int	(*write_cmd)(struct spmi_controller *ctrl, u8 opcode,
 			     u8 sid, u16 addr, const u8 *buf, size_t len);
+	u8 priv[];
 };
 
 static inline struct spmi_controller *to_spmi_controller(struct device *d)
-- 
2.53.0


                 reply	other threads:[~2026-03-27  2:42 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20260327024143.6764-1-rosenp@gmail.com \
    --to=rosenp@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sboyd@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