Skip to content

Commit 6b80f1d

Browse files
GustavoARSilvaChristoph Hellwig
authored andcommitted
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@embeddedor.com> Reviewed-by: James Smart <james.smart@broadcom.com> Signed-off-by: Christoph Hellwig <hch@lst.de>
1 parent cfe03c2 commit 6b80f1d

File tree

1 file changed

+2
-5
lines changed
  • drivers/nvme/target

1 file changed

+2
-5
lines changed

drivers/nvme/target/fc.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,12 @@ struct nvmet_fc_tgt_queue {
128128
struct nvmet_cq nvme_cq;
129129
struct nvmet_sq nvme_sq;
130130
struct nvmet_fc_tgt_assoc *assoc;
131-
struct nvmet_fc_fcp_iod *fod; /* array of fcp_iods */
132131
struct list_head fod_list;
133132
struct list_head pending_cmd_list;
134133
struct list_head avail_defer_list;
135134
struct workqueue_struct *work_q;
136135
struct kref ref;
136+
struct nvmet_fc_fcp_iod fod[]; /* array of fcp_iods */
137137
} __aligned(sizeof(unsigned long long));
138138

139139
struct nvmet_fc_tgt_assoc {
@@ -588,9 +588,7 @@ nvmet_fc_alloc_target_queue(struct nvmet_fc_tgt_assoc *assoc,
588588
if (qid > NVMET_NR_QUEUES)
589589
return NULL;
590590

591-
queue = kzalloc((sizeof(*queue) +
592-
(sizeof(struct nvmet_fc_fcp_iod) * sqsize)),
593-
GFP_KERNEL);
591+
queue = kzalloc(struct_size(queue, fod, sqsize), GFP_KERNEL);
594592
if (!queue)
595593
return NULL;
596594

@@ -603,7 +601,6 @@ nvmet_fc_alloc_target_queue(struct nvmet_fc_tgt_assoc *assoc,
603601
if (!queue->work_q)
604602
goto out_a_put;
605603

606-
queue->fod = (struct nvmet_fc_fcp_iod *)&queue[1];
607604
queue->qid = qid;
608605
queue->sqsize = sqsize;
609606
queue->assoc = assoc;

0 commit comments

Comments
 (0)