Skip to contentSkip to conversation

nvmet-fc: use zero-sized array and struct_size() in kzalloc()

PATCH Applied Gustavo A. R. Silva 4 participants 8 messages Feb 23, 2019 – Mar 12, 2019

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

PATCH 1/1 nvmet-fc: use zero-sized array and struct_size() in kzalloc()

Update the code to use a zero-sized array instead of a pointer in structure nvmet_fc_tgt_queue and use struct_size() in kzalloc().

Notice that one of the more common cases of allocation size calculations is finding the size of a structure that has a zero-sized array at the end, along with memory for some number of elements for that array. For example:

struct foo {
	int stuff;
	struct boo entry[];
};

instance = kzalloc(sizeof(struct foo) + sizeof(struct boo) * count, GFP_KERNEL);

Instead of leaving these open-coded and prone to type mistakes, we can now use the new struct_size() helper:

instance = kzalloc(struct_size(instance, entry, count), GFP_KERNEL);

This code was detected with the help of Coccinelle.

Signed-off-by Gustavo A. R. Silva <gustavo at embeddedor.com>
---
 drivers/nvme/target/fc.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/nvme/target/fc.c +2 -5
index 1e9654f04c60..23baec38f97e 100644
@@ -128,12 +128,12 @@ struct nvmet_fc_tgt_queue {
 	struct nvmet_cq			nvme_cq;
 	struct nvmet_sq			nvme_sq;
 	struct nvmet_fc_tgt_assoc	*assoc;
-	struct nvmet_fc_fcp_iod		*fod;		/* array of fcp_iods */
 	struct list_head		fod_list;
 	struct list_head		pending_cmd_list;
 	struct list_head		avail_defer_list;
 	struct workqueue_struct		*work_q;
 	struct kref			ref;
+	struct nvmet_fc_fcp_iod		fod[];		/* array of fcp_iods */
 } __aligned(sizeof(unsigned long long));
 
 struct nvmet_fc_tgt_assoc {
@@ -588,9 +588,7 @@ nvmet_fc_alloc_target_queue(struct nvmet_fc_tgt_assoc *assoc,
 	if (qid > NVMET_NR_QUEUES)
 		return NULL;
 
-	queue = kzalloc((sizeof(*queue) +
-				(sizeof(struct nvmet_fc_fcp_iod) * sqsize)),
-				GFP_KERNEL);
+	queue = kzalloc(struct_size(queue, fod, sqsize), GFP_KERNEL);
 	if (!queue)
 		return NULL;
 
@@ -603,7 +601,6 @@ nvmet_fc_alloc_target_queue(struct nvmet_fc_tgt_assoc *assoc,
 	if (!queue->work_q)
 		goto out_a_put;
 
-	queue->fod = (struct nvmet_fc_fcp_iod *)&queue[1];
 	queue->qid = qid;
 	queue->sqsize = sqsize;
 	queue->assoc = assoc;
Signature · 1 line
2.20.1
#1 lore raw
Quoted 2 lines · Gustavo A. R. Silva

Update the code to use a zero-sized array instead of a pointer in structure nvmet_fc_tgt_queue and use struct_size() in kzalloc().

[]

Quoted 1 line

This code was detected with the help of Coccinelle.

Really?
Impressive script that found this one.

drivers/nvme/target/fc.c

Quoted 1 line
drivers/nvme/target/fc.c +0 -0

[]

drivers/nvme/target/fc.c struct nvmet_fc_tgt_queue { · line 137

Quoted 12 lines
drivers/nvme/target/fc.c +1 -1
@@ -128,12 +128,12 @@ struct nvmet_fc_tgt_queue {
 	struct nvmet_cq			nvme_cq;
 	struct nvmet_sq			nvme_sq;
 	struct nvmet_fc_tgt_assoc	*assoc;
-	struct nvmet_fc_fcp_iod		*fod;		/* array of fcp_iods */
 	struct list_head		fod_list;
 	struct list_head		pending_cmd_list;
 	struct list_head		avail_defer_list;
 	struct workqueue_struct		*work_q;
 	struct kref			ref;
+	struct nvmet_fc_fcp_iod		fod[];		/* array of fcp_iods */
 } __aligned(sizeof(unsigned long long));

Moving a pointer from the middle of a struct to
the end seems unusual for coccinelle.

#2 lore raw

Hey Joe,

Quoted 9 lines · Joe Perches
Quoted 2 lines · Gustavo A. R. Silva

Update the code to use a zero-sized array instead of a pointer in structure nvmet_fc_tgt_queue and use struct_size() in kzalloc().

[]

Quoted 1 line

This code was detected with the help of Coccinelle.

Really?
Impressive script that found this one.

See my comments below.

Quoted 19 lines

drivers/nvme/target/fc.c

Quoted 1 line
drivers/nvme/target/fc.c +0 -0

[]

drivers/nvme/target/fc.c struct nvmet_fc_tgt_queue { · line 137

Quoted 12 lines
drivers/nvme/target/fc.c +1 -1
@@ -128,12 +128,12 @@ struct nvmet_fc_tgt_queue {
 	struct nvmet_cq			nvme_cq;
 	struct nvmet_sq			nvme_sq;
 	struct nvmet_fc_tgt_assoc	*assoc;
-	struct nvmet_fc_fcp_iod		*fod;		/* array of fcp_iods */
 	struct list_head		fod_list;
 	struct list_head		pending_cmd_list;
 	struct list_head		avail_defer_list;
 	struct workqueue_struct		*work_q;
 	struct kref			ref;
+	struct nvmet_fc_fcp_iod		fod[];		/* array of fcp_iods */
 } __aligned(sizeof(unsigned long long));

Moving a pointer from the middle of a struct to
the end seems unusual for coccinelle.

Notice that the commit log says "detected", which does not imply the script made the transformation by itself. :)

And all the script detected was this piece of code:

queue = kzalloc((sizeof(*queue) +
(sizeof(struct nvmet_fc_fcp_iod) * sqsize)), GFP_KERNEL);

Thanks
--
Gustavo

#3 lore raw
Quoted 35 lines · Gustavo A. R. Silva

Hey Joe,

Quoted 9 lines · Joe Perches
Quoted 2 lines · Gustavo A. R. Silva

Update the code to use a zero-sized array instead of a pointer in structure nvmet_fc_tgt_queue and use struct_size() in kzalloc().

[]

Quoted 1 line

This code was detected with the help of Coccinelle.

Really?
Impressive script that found this one.

See my comments below.

Quoted 19 lines

drivers/nvme/target/fc.c

Quoted 1 line
drivers/nvme/target/fc.c +0 -0

[]

drivers/nvme/target/fc.c struct nvmet_fc_tgt_queue { · line 137

Quoted 12 lines
drivers/nvme/target/fc.c +1 -1
@@ -128,12 +128,12 @@ struct nvmet_fc_tgt_queue {
 	struct nvmet_cq			nvme_cq;
 	struct nvmet_sq			nvme_sq;
 	struct nvmet_fc_tgt_assoc	*assoc;
-	struct nvmet_fc_fcp_iod		*fod;		/* array of fcp_iods */
 	struct list_head		fod_list;
 	struct list_head		pending_cmd_list;
 	struct list_head		avail_defer_list;
 	struct workqueue_struct		*work_q;
 	struct kref			ref;
+	struct nvmet_fc_fcp_iod		fod[];		/* array of fcp_iods */
 } __aligned(sizeof(unsigned long long));

Moving a pointer from the middle of a struct to
the end seems unusual for coccinelle.

Quoted 10 lines

Notice that the commit log says "detected", which does not imply the script made the transformation by itself. :)

And all the script detected was this piece of code:

queue = kzalloc((sizeof(*queue) +
(sizeof(struct nvmet_fc_fcp_iod) * sqsize)), GFP_KERNEL);

Which is enough to mention the tool.

Thanks
--
Gustavo

#4 lore raw
In reply to Gustavo A. R. Silva

James, can you take a look at this one?

Quoted 65 lines · Gustavo A. R. Silva

Update the code to use a zero-sized array instead of a pointer in structure nvmet_fc_tgt_queue and use struct_size() in kzalloc().

Notice that one of the more common cases of allocation size calculations is finding the size of a structure that has a zero-sized array at the end, along with memory for some number of elements for that array. For example:

struct foo {
	int stuff;
	struct boo entry[];
};

instance = kzalloc(sizeof(struct foo) + sizeof(struct boo) * count, GFP_KERNEL);

Instead of leaving these open-coded and prone to type mistakes, we can now use the new struct_size() helper:

instance = kzalloc(struct_size(instance, entry, count), GFP_KERNEL);

This code was detected with the help of Coccinelle.

Signed-off-by Gustavo A. R. Silva <gustavo at embeddedor.com>
---
 drivers/nvme/target/fc.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/nvme/target/fc.c +2 -5
index 1e9654f04c60..23baec38f97e 100644
@@ -128,12 +128,12 @@ struct nvmet_fc_tgt_queue {
 	struct nvmet_cq			nvme_cq;
 	struct nvmet_sq			nvme_sq;
 	struct nvmet_fc_tgt_assoc	*assoc;
-	struct nvmet_fc_fcp_iod		*fod;		/* array of fcp_iods */
 	struct list_head		fod_list;
 	struct list_head		pending_cmd_list;
 	struct list_head		avail_defer_list;
 	struct workqueue_struct		*work_q;
 	struct kref			ref;
+	struct nvmet_fc_fcp_iod		fod[];		/* array of fcp_iods */
 } __aligned(sizeof(unsigned long long));
 
 struct nvmet_fc_tgt_assoc {
@@ -588,9 +588,7 @@ nvmet_fc_alloc_target_queue(struct nvmet_fc_tgt_assoc *assoc,
 	if (qid > NVMET_NR_QUEUES)
 		return NULL;
 
-	queue = kzalloc((sizeof(*queue) +
-				(sizeof(struct nvmet_fc_fcp_iod) * sqsize)),
-				GFP_KERNEL);
+	queue = kzalloc(struct_size(queue, fod, sqsize), GFP_KERNEL);
 	if (!queue)
 		return NULL;
 
@@ -603,7 +601,6 @@ nvmet_fc_alloc_target_queue(struct nvmet_fc_tgt_assoc *assoc,
 	if (!queue->work_q)
 		goto out_a_put;
 
-	queue->fod = (struct nvmet_fc_fcp_iod *)&queue[1];
 	queue->qid = qid;
 	queue->sqsize = sqsize;
 	queue->assoc = assoc;
Signature · 1 line
2.20.1

---end quoted text---

Reviewed-by on v1 1/1
#5 lore raw
In reply to Gustavo A. R. Silva
Quoted 56 lines · Gustavo A. R. Silva

Update the code to use a zero-sized array instead of a pointer in structure nvmet_fc_tgt_queue and use struct_size() in kzalloc().

Notice that one of the more common cases of allocation size calculations is finding the size of a structure that has a zero-sized array at the end, along with memory for some number of elements for that array. For example:

struct foo {
	int stuff;
	struct boo entry[];
};

instance = kzalloc(sizeof(struct foo) + sizeof(struct boo) * count, GFP_KERNEL);

Instead of leaving these open-coded and prone to type mistakes, we can now use the new struct_size() helper:

instance = kzalloc(struct_size(instance, entry, count), GFP_KERNEL);

This code was detected with the help of Coccinelle.

Signed-off-by Gustavo A. R. Silva <gustavo at embeddedor.com>
---
  drivers/nvme/target/fc.c | 7 ++-----
  1 file changed, 2 insertions(+), 5 deletions(-)
drivers/nvme/target/fc.c +2 -4
index 1e9654f04c60..23baec38f97e 100644
@@ -128,12 +128,12 @@ struct nvmet_fc_tgt_queue {
  	struct nvmet_cq			nvme_cq;
  	struct nvmet_sq			nvme_sq;
  	struct nvmet_fc_tgt_assoc	*assoc;
-	struct nvmet_fc_fcp_iod		*fod;		/* array of fcp_iods */
  	struct list_head		fod_list;
  	struct list_head		pending_cmd_list;
  	struct list_head		avail_defer_list;
  	struct workqueue_struct		*work_q;
  	struct kref			ref;
+	struct nvmet_fc_fcp_iod		fod[];		/* array of fcp_iods */
  } __aligned(sizeof(unsigned long long));
 
  struct nvmet_fc_tgt_assoc {
@@ -588,9 +588,7 @@ nvmet_fc_alloc_target_queue(struct nvmet_fc_tgt_assoc *assoc,
  	if (qid > NVMET_NR_QUEUES)
  		return NULL;
 
-	queue = kzalloc((sizeof(*queue) +
-				(sizeof(struct nvmet_fc_fcp_iod) * sqsize)),
-				GFP_KERNEL);
+	queue = kzalloc(struct_size(queue, fod, sqsize), GFP_KERNEL);
  	if (!queue)
  		return NULL;
 

drivers/nvme/target/fc.c nvmet_fc_alloc_target_queue(struct nvmet_fc_tgt_assoc *assoc, · line 606

Quoted 7 lines
drivers/nvme/target/fc.c +0 -1
@@ -603,7 +601,6 @@ nvmet_fc_alloc_target_queue(struct nvmet_fc_tgt_assoc *assoc,
  	if (!queue->work_q)
  		goto out_a_put;
 
-	queue->fod = (struct nvmet_fc_fcp_iod *)&queue[1];
  	queue->qid = qid;
  	queue->sqsize = sqsize;
  	queue->assoc = assoc;
Reviewed-by ? James Smart <james.smart at broadcom.com>

I guess this is a better style.

-- james

#7 lore raw
Quoted 4 lines · Christoph Hellwig

Thanks,

applied to nvme-5.2.

Great.

Thanks, Christoph.

--
Gustavo