Received: by 2002:ac0:aed5:0:0:0:0:0 with SMTP id t21csp6188465imb; Fri, 8 Mar 2019 11:16:43 -0800 (PST) X-Google-Smtp-Source: APXvYqyh5mPPdAJMLb4zoi3LRdITRU4nP2IkazKVlc/TqoTXChXcCKA1ycCbHjvatW3M2JY7BG/E X-Received: by 2002:a17:902:aa06:: with SMTP id be6mr20649759plb.57.1552072603686; Fri, 08 Mar 2019 11:16:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1552072603; cv=none; d=google.com; s=arc-20160816; b=a+CTyA+mpey7FH5A12M7nlUiKxBljAL1DyQu2Z0E2H3QIzwB+t+xceOnXdJFNNcVNQ BJVm73EoQgli797DoifLx2a9jJs4YuU698bd2nyHiToZWSu9gtoSzDkP7P9LUXLmMSF6 VEk1tlW72Io7zoffqAuX+GKvJ1JYXXEPTEqEAYsNZY5okcyghKj/kCgUwmCuEO7n/PlG S2aE+6tg8GNO1GAqpMedoTZnMHViiA8yWjo4sW5Z/l/AehguieBa+H8RM1fNalipC/VT TFtbviY6nplyEIz5aIQ3sDRRyONjOjCFXHZZA56jCagG/NwA7C4pnInUeuYr+kxB/UUL g4Bw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-language :content-transfer-encoding:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature; bh=FxDRnMK2hBrkGM9q7EoRDgAZcC5p1d4FV3FKIjG4i9s=; b=OGn13WNu2JjDr0syeR31Ads2ApzC+T5cNLR0fM98E4tf8XEq2obevFRtoeFWkRSvCL 0OB+93UnS+mmq3RuubgZ0h3Wc6Tp6L2tfRtVZLqHKXlvjCCznCllF7x/T/8jl5DMycJa Yw45RL8kk41YCjDIrLVdde/f9Kishp2JONjSili0ZjxQ4nNYKA7FYAOFAV3tMsQDNFil qYY2tXdVcoyAMwm6AXX68GqNOQjHOMR9mtle6KRqkOoJgm8KjRsBt01QtCed/P5I+jFS S2HXX3UrczmBZSj6BvK5y70NxjJ8nA3DL3tIo6fzsdBJ2Le1LdA7sfBj0ibeLRbes2Bw 4rUQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@broadcom.com header.s=google header.b=Q4L5a5zu; 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; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=broadcom.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e2si7301860pgm.568.2019.03.08.11.16.27; Fri, 08 Mar 2019 11:16:43 -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; dkim=pass header.i=@broadcom.com header.s=google header.b=Q4L5a5zu; 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; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=broadcom.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726696AbfCHTPj (ORCPT + 99 others); Fri, 8 Mar 2019 14:15:39 -0500 Received: from mail-pf1-f195.google.com ([209.85.210.195]:39703 "EHLO mail-pf1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726254AbfCHTPj (ORCPT ); Fri, 8 Mar 2019 14:15:39 -0500 Received: by mail-pf1-f195.google.com with SMTP id i20so14818038pfo.6 for ; Fri, 08 Mar 2019 11:15:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=broadcom.com; s=google; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-transfer-encoding:content-language; bh=FxDRnMK2hBrkGM9q7EoRDgAZcC5p1d4FV3FKIjG4i9s=; b=Q4L5a5zur7a13HV856+/5wWT5UsWeuzfdLAJpbYKrTEXHI4W6p7ZvzZjx6dVLti3zL reeoBe4/RzaBt+UToQz8aQyWVEAByHsJP5x5hz1CgBps8dxZbGX5+I7bg6hSGiBxswh7 /ysmEKIs0IsytND3X9a+KXOPRFly9d8MwufXM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=FxDRnMK2hBrkGM9q7EoRDgAZcC5p1d4FV3FKIjG4i9s=; b=DgWgZ1uSMNZdrtAcnxX7Dg1s9uqZNwJ58C+5jWWj4I4AIJ1/2YmEjl9M4Je5wn+glW QoKj7xpiSZtJ6lG6mNPkceQk4ohN3DjJANwwRfWQvtcfeYdJSkHaAgDS0O655dXTs/XM VWyTQmsM04pKtAgEZnQ8R+JvL9Fq7DxNx9uCzb2IRwcbx42aKGvPCLp6b+OSBcPxj/3I zTPD1ktH2dlSdsWqcjoWV8i/NB2yEQwEnGjzyuh/i/MwzsnzW0vxpKAOMOyhOUbJIsM7 B4DZZaPWApMPVRddKaCNLa+Os489rcpwim0wypDtL4tRUhELyiJkBnXr0jdom/X02O0/ 75lg== X-Gm-Message-State: APjAAAVrneXevO64YQzACmVY8DpJauMrqqlz8dssKMndICYnmFbYJsln w580FdZHol+b9EgyXcse80vNSxa7M+acEvno5hRB3b2UuFrLZBzb61jgSHW19KjvQgmgxMwiVsL 421Kul0DwfrY7F2sJQur2zmy6aUXeOpqvGCKoLTpQJmBUjNsR7bYERXfDjF3eJvr72K0w5Ktnb8 a0KC0= X-Received: by 2002:a17:902:1744:: with SMTP id i62mr20227415pli.24.1552072537736; Fri, 08 Mar 2019 11:15:37 -0800 (PST) Received: from [10.69.37.149] ([192.19.223.250]) by smtp.gmail.com with ESMTPSA id n74sm15791985pfb.188.2019.03.08.11.15.36 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 08 Mar 2019 11:15:36 -0800 (PST) Subject: Re: [PATCH] nvmet-fc: use zero-sized array and struct_size() in kzalloc() To: "Gustavo A. R. Silva" , Christoph Hellwig , Sagi Grimberg Cc: linux-nvme@lists.infradead.org, linux-kernel@vger.kernel.org References: <20190223185108.GA6706@embeddedor> From: James Smart Message-ID: Date: Fri, 8 Mar 2019 11:15:35 -0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.5.3 MIME-Version: 1.0 In-Reply-To: <20190223185108.GA6706@embeddedor> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2/23/2019 10:51 AM, Gustavo A. R. Silva wrote: > 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; Reviewed-by:  James Smart I guess this is a better style. -- james