Received: by 2002:ac0:b08d:0:0:0:0:0 with SMTP id l13csp2779171imc; Sat, 23 Feb 2019 10:51:55 -0800 (PST) X-Google-Smtp-Source: AHgI3IYiUslKPxIyd5lDfe0Yo9G+awQREtWxF6bd/AYhJnxeDDGm36SdBXp5Qxr5jwgAGt/VkrJs X-Received: by 2002:a17:902:1486:: with SMTP id k6mr10899347pla.49.1550947914972; Sat, 23 Feb 2019 10:51:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1550947914; cv=none; d=google.com; s=arc-20160816; b=PupfXDQmXvdea5INVZ6fvsirbRgT8VMqVOYnT5NdUStykgYjJhPS2SWx3XVN6YlmLJ fNkZ/N03hcLdT5PX6g6oJZoGTrAPkl7jafERkPyV+XNUWKJXT+Do9qHYzjaO3lQgB0W7 2jygBJkUEg1iANH4VdUelzgmK/QaFZVq7WQr3hyHCTN0+I3dLSwK8CnFD+lJ0ItiHDUb YuqHKMj+eGkEcCozp+P6XvT9usrxTU3qwugHAzbaLsN1ZUgxuAS5ggPH/9yv63vSGJ+f pMJsnm/8jgBXV8ZDlIP//qmmwm1QXvxmwrdub82YghxM/YEkD8OvWBVLAYhtWcW+sMsN nJtQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:content-disposition :mime-version:message-id:subject:cc:to:from:date; bh=cJ/I3Hxxl/4SeVDgOm0+bXpGdp+0qpcop5lzkIgnPgA=; b=Fo0v7ZjW+O2rc4Nro9EHRU46NSIfOXmlhYGHbuGk+Ur776xFLyMDjiACdc1BrQ8z/H hYM8il5Meb/GvYuFa0MM5TgHk9lTYYPZx5MDdPg51wYWlVHztzGOglsn0IPLHCMrw5H+ frmHs5jLVqlfd7TnZ3xIEWtVlLEUP6qN7SHR3gDEbIAeD/iweJdkTHEJhUw9fBWl/0xc 9vr6RAtbkcS2lZGm6cz5MvRC08sMQrclq+zTxY22h7ptnHFKGiVZ80u45Mbbi/5G116k z4GJcy5O+SR0IhynheLddpVpjKzo5i20dHvP6yAUVjzOIPgvm30NmDL7HfjXZO9BsuqG UBwg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g32si1593565pgg.223.2019.02.23.10.51.39; Sat, 23 Feb 2019 10:51:54 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727525AbfBWSvN (ORCPT + 99 others); Sat, 23 Feb 2019 13:51:13 -0500 Received: from gateway33.websitewelcome.com ([192.185.146.21]:16250 "EHLO gateway33.websitewelcome.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726072AbfBWSvM (ORCPT ); Sat, 23 Feb 2019 13:51:12 -0500 Received: from cm13.websitewelcome.com (cm13.websitewelcome.com [100.42.49.6]) by gateway33.websitewelcome.com (Postfix) with ESMTP id C17E016D27D for ; Sat, 23 Feb 2019 12:51:11 -0600 (CST) Received: from gator4166.hostgator.com ([108.167.133.22]) by cmsmtp with SMTP id xcOFgvpiVYTGMxcOFgDRD7; Sat, 23 Feb 2019 12:51:11 -0600 X-Authority-Reason: nr=8 Received: from [189.250.127.169] (port=60464 helo=embeddedor) by gator4166.hostgator.com with esmtpa (Exim 4.91) (envelope-from ) id 1gxcOE-000w09-0t; Sat, 23 Feb 2019 12:51:10 -0600 Date: Sat, 23 Feb 2019 12:51:08 -0600 From: "Gustavo A. R. Silva" To: James Smart , Christoph Hellwig , Sagi Grimberg Cc: linux-nvme@lists.infradead.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" Subject: [PATCH] nvmet-fc: use zero-sized array and struct_size() in kzalloc() Message-ID: <20190223185108.GA6706@embeddedor> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.9.4 (2018-02-28) X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator4166.hostgator.com X-AntiAbuse: Original Domain - vger.kernel.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - embeddedor.com X-BWhitelist: no X-Source-IP: 189.250.127.169 X-Source-L: No X-Exim-ID: 1gxcOE-000w09-0t X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: (embeddedor) [189.250.127.169]:60464 X-Source-Auth: gustavo@embeddedor.com X-Email-Count: 5 X-Source-Cap: Z3V6aWRpbmU7Z3V6aWRpbmU7Z2F0b3I0MTY2Lmhvc3RnYXRvci5jb20= X-Local-Domain: yes Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- drivers/nvme/target/fc.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/drivers/nvme/target/fc.c b/drivers/nvme/target/fc.c index 1e9654f04c60..23baec38f97e 100644 --- a/drivers/nvme/target/fc.c +++ b/drivers/nvme/target/fc.c @@ -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; -- 2.20.1