Skip to contentSkip to conversation

gpio: virtio: remove one kcalloc

PATCH Superseded Rosen Penev 2 participants 2 messages Mar 10

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gpio: virtio: remove one kcalloc

A flexible array member can be used to combine allocations.

---
 drivers/gpio/gpio-virtio.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/gpio/gpio-virtio.c +3 -7
index ed6e0e90fa8a..876a34ed739d 100644
@@ -52,7 +52,6 @@ struct virtio_gpio {
 	struct virtio_device *vdev;
 	struct mutex lock; /* Protects virtqueue operation */
 	struct gpio_chip gc;
-	struct virtio_gpio_line *lines;
 	struct virtqueue *request_vq;
 
 	/* irq support */
@@ -60,6 +59,7 @@ struct virtio_gpio {
 	struct mutex irq_lock; /* Protects irq operation */
 	raw_spinlock_t eventq_lock; /* Protects queuing of the buffer */
 	struct vgpio_irq_line *irq_lines;
+	struct virtio_gpio_line lines[];
 };
 
 static int _virtio_gpio_req(struct virtio_gpio *vgpio, u16 type, u16 gpio,
@@ -541,10 +541,6 @@ static int virtio_gpio_probe(struct virtio_device *vdev)
 	u16 ngpio;
 	int ret, i;
 
-	vgpio = devm_kzalloc(dev, sizeof(*vgpio), GFP_KERNEL);
-	if (!vgpio)
-		return -ENOMEM;
-
 	/* Read configuration */
 	gpio_names_size =
 		virtio_cread32(vdev, offsetof(struct virtio_gpio_config,
@@ -556,8 +552,8 @@ static int virtio_gpio_probe(struct virtio_device *vdev)
 		return -EINVAL;
 	}
 
-	vgpio->lines = devm_kcalloc(dev, ngpio, sizeof(*vgpio->lines), GFP_KERNEL);
-	if (!vgpio->lines)
+	vgpio = devm_kzalloc(dev, struct_size(vgpio, lines, ngpio), GFP_KERNEL);
+	if (!vgpio)
 		return -ENOMEM;
 
 	for (i = 0; i < ngpio; i++) {
Signature · 1 line
2.53.0
Acked-by on v1 1/1
#1 lore raw
Quoted 20 lines · Rosen Penev

A flexible array member can be used to combine allocations.

---
 drivers/gpio/gpio-virtio.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/gpio/gpio-virtio.c +0 -1
index ed6e0e90fa8a..876a34ed739d 100644
@@ -52,7 +52,6 @@ struct virtio_gpio {
 	struct virtio_device *vdev;
 	struct mutex lock; /* Protects virtqueue operation */
 	struct gpio_chip gc;
-	struct virtio_gpio_line *lines;
 	struct virtqueue *request_vq;
 
 	/* irq support */

drivers/gpio/gpio-virtio.c struct virtio_gpio { · line 62

Quoted 4 lines
drivers/gpio/gpio-virtio.c +1 -0
@@ -60,6 +59,7 @@ struct virtio_gpio {
 	struct mutex irq_lock; /* Protects irq operation */
 	raw_spinlock_t eventq_lock; /* Protects queuing of the buffer */
 	struct vgpio_irq_line *irq_lines;
+	struct virtio_gpio_line lines[];

Add a blank line before this to separate this out from irq-support entries.

Quoted 15 lines
drivers/gpio/gpio-virtio.c +0 -4
 };
 
 static int _virtio_gpio_req(struct virtio_gpio *vgpio, u16 type, u16 gpio,
@@ -541,10 +541,6 @@ static int virtio_gpio_probe(struct virtio_device *vdev)
 	u16 ngpio;
 	int ret, i;
 
-	vgpio = devm_kzalloc(dev, sizeof(*vgpio), GFP_KERNEL);
-	if (!vgpio)
-		return -ENOMEM;
-
 	/* Read configuration */
 	gpio_names_size =
 		virtio_cread32(vdev, offsetof(struct virtio_gpio_config,

drivers/gpio/gpio-virtio.c static int virtio_gpio_probe(struct virtio_device *vdev) · line 559

Quoted 10 lines
drivers/gpio/gpio-virtio.c +2 -2
@@ -556,8 +552,8 @@ static int virtio_gpio_probe(struct virtio_device *vdev)
 		return -EINVAL;
 	}
 
-	vgpio->lines = devm_kcalloc(dev, ngpio, sizeof(*vgpio->lines), GFP_KERNEL);
-	if (!vgpio->lines)
+	vgpio = devm_kzalloc(dev, struct_size(vgpio, lines, ngpio), GFP_KERNEL);
+	if (!vgpio)
 		return -ENOMEM;
 
 	for (i = 0; i < ngpio; i++) {
Signature · 1 line
viresh