Skip to contentSkip to conversation

usb: typec: tcpm: kzalloc + kcalloc to kzalloc_flex

PATCH In review Rosen Penev 2 participants 3 messages Mar 6

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: typec: tcpm: kzalloc + kcalloc to kzalloc_flex

Simplifies allocation and allows using __counted_by for extra runtime analysis.

---
 drivers/usb/typec/tcpm/tcpm.c | 19 +++++--------------
 1 file changed, 5 insertions(+), 14 deletions(-)
drivers/usb/typec/tcpm/tcpm.c +5 -14
index 1d2f3af034c5..272f9187b12d 100644
@@ -605,9 +605,9 @@ struct altmode_vdm_event {
 	struct kthread_work work;
 	struct tcpm_port *port;
 	u32 header;
-	u32 *data;
 	int cnt;
 	enum tcpm_transmit_type tx_sop_type;
+	u32 data[] __counted_by(cnt);
 };
 
 static const char * const pd_rev[] = {
@@ -1653,7 +1653,6 @@ static void tcpm_queue_vdm_work(struct kthread_work *work)
 	tcpm_queue_vdm(port, event->header, event->data, event->cnt, event->tx_sop_type);
 
 port_unlock:
-	kfree(event->data);
 	kfree(event);
 	mutex_unlock(&port->lock);
 }
@@ -1662,35 +1661,27 @@ static int tcpm_queue_vdm_unlocked(struct tcpm_port *port, const u32 header,
 				   const u32 *data, int cnt, enum tcpm_transmit_type tx_sop_type)
 {
 	struct altmode_vdm_event *event;
-	u32 *data_cpy;
 	int ret = -ENOMEM;
 
-	event = kzalloc_obj(*event);
+	event = kzalloc_flex(*event, data, cnt);
 	if (!event)
 		goto err_event;
 
-	data_cpy = kcalloc(cnt, sizeof(u32), GFP_KERNEL);
-	if (!data_cpy)
-		goto err_data;
-
 	kthread_init_work(&event->work, tcpm_queue_vdm_work);
+	event->cnt = cnt;
 	event->port = port;
 	event->header = header;
-	memcpy(data_cpy, data, sizeof(u32) * cnt);
-	event->data = data_cpy;
-	event->cnt = cnt;
+	memcpy(event->data, data, sizeof(u32) * cnt);
 	event->tx_sop_type = tx_sop_type;
 
 	ret = kthread_queue_work(port->wq, &event->work);
 	if (!ret) {
 		ret = -EBUSY;
-		goto err_queue;
+		goto err_data;
 	}
 
 	return 0;
 
-err_queue:
-	kfree(data_cpy);
 err_data:
 	kfree(event);
 err_event:
Signature · 1 line
2.53.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 2 lines · Rosen Penev

Simplifies allocation and allows using __counted_by for extra runtime analysis.

Are you finding these with Coccinelle or any other tool?

Quoted 2 lines

Thanks
-Gustavo

Quoted 67 lines
---
  drivers/usb/typec/tcpm/tcpm.c | 19 +++++--------------
  1 file changed, 5 insertions(+), 14 deletions(-)
drivers/usb/typec/tcpm/tcpm.c +5 -14
index 1d2f3af034c5..272f9187b12d 100644
@@ -605,9 +605,9 @@ struct altmode_vdm_event {
  	struct kthread_work work;
  	struct tcpm_port *port;
  	u32 header;
-	u32 *data;
  	int cnt;
  	enum tcpm_transmit_type tx_sop_type;
+	u32 data[] __counted_by(cnt);
  };
 
  static const char * const pd_rev[] = {
@@ -1653,7 +1653,6 @@ static void tcpm_queue_vdm_work(struct kthread_work *work)
  	tcpm_queue_vdm(port, event->header, event->data, event->cnt, event->tx_sop_type);
 
  port_unlock:
-	kfree(event->data);
  	kfree(event);
  	mutex_unlock(&port->lock);
  }
@@ -1662,35 +1661,27 @@ static int tcpm_queue_vdm_unlocked(struct tcpm_port *port, const u32 header,
  				   const u32 *data, int cnt, enum tcpm_transmit_type tx_sop_type)
  {
  	struct altmode_vdm_event *event;
-	u32 *data_cpy;
  	int ret = -ENOMEM;
 
-	event = kzalloc_obj(*event);
+	event = kzalloc_flex(*event, data, cnt);
  	if (!event)
  		goto err_event;
 
-	data_cpy = kcalloc(cnt, sizeof(u32), GFP_KERNEL);
-	if (!data_cpy)
-		goto err_data;
-
  	kthread_init_work(&event->work, tcpm_queue_vdm_work);
+	event->cnt = cnt;
  	event->port = port;
  	event->header = header;
-	memcpy(data_cpy, data, sizeof(u32) * cnt);
-	event->data = data_cpy;
-	event->cnt = cnt;
+	memcpy(event->data, data, sizeof(u32) * cnt);
  	event->tx_sop_type = tx_sop_type;
 
  	ret = kthread_queue_work(port->wq, &event->work);
  	if (!ret) {
  		ret = -EBUSY;
-		goto err_queue;
+		goto err_data;
  	}
 
  	return 0;
 
-err_queue:
-	kfree(data_cpy);
  err_data:
  	kfree(event);
  err_event:
#2 lore raw
Quoted 8 lines · Gustavo A. R. Silva
Quoted 2 lines · Rosen Penev

Simplifies allocation and allows using __counted_by for extra runtime analysis.

Are you finding these with Coccinelle or any other tool?

git grep -B 10 zalloc_objs\( | grep zalloc\(
git grep -B 10 calloc\( | grep zalloc\(

And seeing if it's something that can easily be dealt with. The __counted_by thing is usually clear in _probe if it's correct.

Quoted 77 lines
Quoted 3 lines

Thanks
-Gustavo

Quoted 67 lines
---
  drivers/usb/typec/tcpm/tcpm.c | 19 +++++--------------
  1 file changed, 5 insertions(+), 14 deletions(-)
drivers/usb/typec/tcpm/tcpm.c +5 -14
index 1d2f3af034c5..272f9187b12d 100644
@@ -605,9 +605,9 @@ struct altmode_vdm_event {
      struct kthread_work work;
      struct tcpm_port *port;
      u32 header;
-     u32 *data;
      int cnt;
      enum tcpm_transmit_type tx_sop_type;
+     u32 data[] __counted_by(cnt);
  };
 
  static const char * const pd_rev[] = {
@@ -1653,7 +1653,6 @@ static void tcpm_queue_vdm_work(struct kthread_work *work)
      tcpm_queue_vdm(port, event->header, event->data, event->cnt, event->tx_sop_type);
 
  port_unlock:
-     kfree(event->data);
      kfree(event);
      mutex_unlock(&port->lock);
  }
@@ -1662,35 +1661,27 @@ static int tcpm_queue_vdm_unlocked(struct tcpm_port *port, const u32 header,
                                 const u32 *data, int cnt, enum tcpm_transmit_type tx_sop_type)
  {
      struct altmode_vdm_event *event;
-     u32 *data_cpy;
      int ret = -ENOMEM;
 
-     event = kzalloc_obj(*event);
+     event = kzalloc_flex(*event, data, cnt);
      if (!event)
              goto err_event;
 
-     data_cpy = kcalloc(cnt, sizeof(u32), GFP_KERNEL);
-     if (!data_cpy)
-             goto err_data;
-
      kthread_init_work(&event->work, tcpm_queue_vdm_work);
+     event->cnt = cnt;
      event->port = port;
      event->header = header;
-     memcpy(data_cpy, data, sizeof(u32) * cnt);
-     event->data = data_cpy;
-     event->cnt = cnt;
+     memcpy(event->data, data, sizeof(u32) * cnt);
      event->tx_sop_type = tx_sop_type;
 
      ret = kthread_queue_work(port->wq, &event->work);
      if (!ret) {
              ret = -EBUSY;
-             goto err_queue;
+             goto err_data;
      }
 
      return 0;
 
-err_queue:
-     kfree(data_cpy);
  err_data:
      kfree(event);
  err_event: