Skip to contentSkip to conversation

spmi: use kzalloc_flex in main allocation

PATCH Applied Rosen Penev 2 participants 2 messages Mar 27 – May 29 SPMI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 spmi: use kzalloc_flex in main allocation

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

---
 drivers/spmi/spmi.c  | 4 ++--
 include/linux/spmi.h | 2 ++
 2 files changed, 4 insertions(+), 2 deletions(-)
drivers/spmi/spmi.c +2 -2
index e889b129f3ac..57b7c0cb4240 100644
@@ -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) {
include/linux/spmi.h +2 -0
index 28e8c8bd3944..ddeeaee52ca7 100644
@@ -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)
Signature · 1 line
2.53.0