Skip to contentSkip to conversation

siox: use kzalloc_flex

PATCH NAK Rosen Penev 2 participants 4 messages Mar 26 – Mar 27 Siox

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 siox: use kzalloc_flex

Use the proper function to allocate a struct with a flexible array member.

Add __counted_by for extra runtime analysis. Add counting variable assignment as required by __counted_by.

---
 drivers/siox/siox-core.c | 5 +++--
 drivers/siox/siox.h      | 3 ++-
 2 files changed, 5 insertions(+), 3 deletions(-)
drivers/siox/siox-core.c +3 -2
index 3e8f3b6a4555..b87e2a12d640 100644
@@ -689,10 +689,11 @@ struct siox_master *siox_master_alloc(struct device *dev,
 	if (!dev)
 		return NULL;
 
-	smaster = kzalloc(sizeof(*smaster) + size, GFP_KERNEL);
+	smaster = kzalloc_flex(*smaster, buf, size);
 	if (!smaster)
 		return NULL;
 
+	smaster->buf_len = size;
 	device_initialize(&smaster->dev);
 
 	smaster->busno = -1;
@@ -854,7 +855,7 @@ static struct siox_device *siox_device_add(struct siox_master *smaster,
 		}
 
 		smaster->buf_len = buf_len;
-		smaster->buf = buf;
+		memcpy(smaster->buf, buf, buf_len);
 	}
 
 	ret = device_register(&sdevice->dev);
drivers/siox/siox.h +2 -1
index 513f2c8312f7..e6809fcafa0f 100644
@@ -27,11 +27,12 @@ struct siox_master {
 
 	size_t setbuf_len, getbuf_len;
 	size_t buf_len;
-	u8 *buf;
 	u8 status;
 
 	unsigned long last_poll;
 	struct task_struct *poll_thread;
+
+	u8 buf[] __counted_by(buf_len);
 };
 
 static inline void *siox_master_get_devdata(struct siox_master *smaster)
Signature · 1 line
2.53.0
#1 lore raw
Quoted 7 lines · Rosen Penev

Use the proper function to allocate a struct with a flexible array member.

Add __counted_by for extra runtime analysis. Add counting variable assignment as required by __counted_by.

NACK - What's you motivation to do this?

Quoted 10 lines
---
 drivers/siox/siox-core.c | 5 +++--
 drivers/siox/siox.h      | 3 ++-
 2 files changed, 5 insertions(+), 3 deletions(-)
drivers/siox/siox-core.c +0 -0
index 3e8f3b6a4555..b87e2a12d640 100644

drivers/siox/siox-core.c struct siox_master *siox_master_alloc(struct device *dev, · line 696

Quoted 9 lines
drivers/siox/siox-core.c +2 -1
@@ -689,10 +689,11 @@ struct siox_master *siox_master_alloc(struct device *dev,
 	if (!dev)
 		return NULL;
 
-	smaster = kzalloc(sizeof(*smaster) + size, GFP_KERNEL);
+	smaster = kzalloc_flex(*smaster, buf, size);
 	if (!smaster)
 		return NULL;
 
+	smaster->buf_len = size;

The "size" extra space is not used for master->buf, but for dev->driver_data. master->buf is allocated in siox_device_add().

regards,
Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde          |
Embedded Linux                   | https://www.pengutronix.de |
Vertretung Nürnberg              | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-9   |
#2 lore raw
Quoted 30 lines · Marc Kleine-Budde
Quoted 7 lines · Rosen Penev

Use the proper function to allocate a struct with a flexible array member.

Add __counted_by for extra runtime analysis. Add counting variable assignment as required by __counted_by.

NACK - What's you motivation to do this?

Quoted 18 lines
---
 drivers/siox/siox-core.c | 5 +++--
 drivers/siox/siox.h      | 3 ++-
 2 files changed, 5 insertions(+), 3 deletions(-)
drivers/siox/siox-core.c +1 -1
index 3e8f3b6a4555..b87e2a12d640 100644
@@ -689,10 +689,11 @@ struct siox_master *siox_master_alloc(struct device *dev,
      if (!dev)
              return NULL;
 
-     smaster = kzalloc(sizeof(*smaster) + size, GFP_KERNEL);
+     smaster = kzalloc_flex(*smaster, buf, size);
      if (!smaster)
              return NULL;
 
Quoted 4 lines

drivers/siox/siox-core.c struct siox_master *siox_master_alloc(struct device *dev, · line 696

Quoted 1 line
drivers/siox/siox-core.c +1 -0
+     smaster->buf_len = size;

The "size" extra space is not used for master->buf, but for dev->driver_data. master->buf is allocated in siox_device_add().

I assume this means a new struct member should be added and kzalloc_flex modified to work on that.

Quoted 9 lines

regards,
Marc

--

Pengutronix e.K.                 | Marc Kleine-Budde          |
Embedded Linux                   | https://www.pengutronix.de |

Vertretung Nürnberg | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-9 |

#3 lore raw
Quoted 6 lines · Rosen Penev
Quoted 4 lines

NACK - What's you motivation to do this?

The "size" extra space is not used for master->buf, but for dev->driver_data. master->buf is allocated in siox_device_add().

I assume this means a new struct member should be added and

    ^^^^^^
Quoted 1 line

kzalloc_flex modified to work on that.

I suggest you look at the code, try to understand what it currently does, then re-think what your proposed changed do, what problem your changes fix.

regards,
Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde          |
Embedded Linux                   | https://www.pengutronix.de |
Vertretung Nürnberg              | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-9   |