Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp712854imu; Fri, 11 Jan 2019 07:49:38 -0800 (PST) X-Google-Smtp-Source: ALg8bN7OqtX4rO2NFEHrUrAyDusglOXvmOo127bdEpACFFrcnYvwnUHJB+OMxalpq4RyGkjoOeqX X-Received: by 2002:a17:902:9a8b:: with SMTP id w11mr14826541plp.121.1547221778093; Fri, 11 Jan 2019 07:49:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1547221778; cv=none; d=google.com; s=arc-20160816; b=Y/myR8khMaGgJXTdcnRRG/gNCRnZlMeuCzpbtRmN4jx+WiCS/gmoNLP7gYmqWBszN8 8TrCDQTLuFYDhnhC4gT0s1ny3dOdnqCnCAptLP1I+EoKG+xDJpvAleTEymkVb8wRDe+/ obICbE/+HpQBBZljFTLGNjSp7340iCIP3j01Epp1l05Dow6bGdVCzQtU0XXPraAU+H3J EcG26IgTXgvPWGU+cI59TMeTQautbMPCaL/teLy25msL8iyyYjyvAxDUZFp0NxTUqnoF 1RxTFAKJWyiLP+/z9pFjtdMTHeJA0+IZ0pufJ1Qb6QD1vu6Q9J4wyqh3A6m47oU3nAb1 xRNw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=ypGYywHEFPNECugNzVxJMewxdbuTilAuX+izuPvNxHw=; b=ZZoBHog0vtRxZ7dz3wxmfHrB0Q7DGjuUk2sXsN1V5n+5Y1omS3YlgdYmeuMLO00zjn ZVPSJuSQL5wt6vrjMCsSnQJiFCF5xWLA0ZBDuDUpAAFq+3hige9E6XywZVncHckN0oCU 2DbzjtXNcyBYbtPxVSZKOLkix5f1BUasxmyyceFgKP0WwoHLgrLZU4yQm6D9X4Sxc03y dvdDwDFvSBELjXKT/LyKRcqAi6KujPqQ13Boeggp16KsY1PmF2khtiV+gt7B5CRdpU8w j89wswjqSxvYSxXqQDCp1keOwJH4gTlYFSOS8MBRy0N//aCLO1NLilXQPk75Tqoh0p18 XFsw== 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 i96si3553153plb.188.2019.01.11.07.49.22; Fri, 11 Jan 2019 07:49:38 -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 S1732311AbfAKPqt (ORCPT + 99 others); Fri, 11 Jan 2019 10:46:49 -0500 Received: from mx2.suse.de ([195.135.220.15]:35140 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1729463AbfAKPqs (ORCPT ); Fri, 11 Jan 2019 10:46:48 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay1.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 68A80AFF4; Fri, 11 Jan 2019 15:46:47 +0000 (UTC) Subject: Re: [PATCH] scsi: advansys: use struct_size() in kzalloc() To: "Gustavo A. R. Silva" , Matthew Wilcox , Hannes Reinecke , "James E.J. Bottomley" , "Martin K. Petersen" Cc: linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org References: <20190104212209.GA15250@embeddedor> From: Hannes Reinecke Message-ID: <05420a5c-c268-b87d-9d75-f5d18a4b7f7a@suse.de> Date: Fri, 11 Jan 2019 16:46:46 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.3.0 MIME-Version: 1.0 In-Reply-To: <20190104212209.GA15250@embeddedor> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 1/4/19 10:22 PM, Gustavo A. R. Silva wrote: > 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; > void *entry[]; > }; > > instance = kzalloc(sizeof(struct foo) + sizeof(void *) * 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/scsi/advansys.c | 4 ++-- > 1 file changed, 2 insertions(+), 2 deletions(-) > > diff --git a/drivers/scsi/advansys.c b/drivers/scsi/advansys.c > index d37584403c33..6c274e6e1c33 100644 > --- a/drivers/scsi/advansys.c > +++ b/drivers/scsi/advansys.c > @@ -7576,8 +7576,8 @@ static int asc_build_req(struct asc_board *boardp, struct scsi_cmnd *scp, > return ASC_ERROR; > } > > - asc_sg_head = kzalloc(sizeof(asc_scsi_q->sg_head) + > - use_sg * sizeof(struct asc_sg_list), GFP_ATOMIC); > + asc_sg_head = kzalloc(struct_size(asc_sg_head, sg_list, use_sg), > + GFP_ATOMIC); > if (!asc_sg_head) { > scsi_dma_unmap(scp); > scp->result = HOST_BYTE(DID_SOFT_ERROR); > If you want ... Reviewed-by: Hannes Reinecke Cheers, Hannes