Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp1115027imu; Fri, 4 Jan 2019 13:24:42 -0800 (PST) X-Google-Smtp-Source: ALg8bN7T3iAxkVQxHfriu8iXZPqSrFvAT1R9F2J0b8LRVUY416N97iF3UCEuhCvhAb5gCvn1J70B X-Received: by 2002:a63:4926:: with SMTP id w38mr2850817pga.353.1546637082697; Fri, 04 Jan 2019 13:24:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1546637082; cv=none; d=google.com; s=arc-20160816; b=vE50SQgvJKf8m2qOsib6mJKc5U61wmF0lXXlam1HjhOZ7o8veaRAGj0RUK+PDRWKyU mL5x+W6MQuRyM+rFJyB9FMN7Lja4zMR3n8TLitWFLa6PHp8qESiWV3pKe86wi9avX95P t6iNnX79YVBQ3XhH1JlE2sOJRkkTacdqetkj/r+3dTQYQgdZDWnqLiysmZQhM1KweS9A scdEdc4F9KAsxRDxXgErsBvoGh2OutkZ3Lz7yI88RNmRQnY58in/Gb5brPfkEMhING8+ tiar7CSpRlMowcOpcL7xC7TtexGPJEDlQZMlFzxPNmqyVBhZeA+ztzlrnXyiFvZ9C3H2 vxuw== 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=1BfsgcmbTaSnqK9so6MpaW1ICmDaL78N9FXuZbMFWa8=; b=HinihoIZonCTOrcazyfzFRg0s8yGvy4JjlJBdUEu+6Grtp2BT/IlBvdrC3tH2ragRe Hyh17jQJPbrpHY7qgOzN9jTpCI0yDAwcntKgdVt49istd/KezmhrxLnTiUCoLt1K0PD+ nNePNtcMRO9tcqxJxhSlaYmrbVUf4vA/FAeOcvLaac1g7uja5YmWCNch4rsHsrac4Ub2 zI/d9r9bOyPjf/Xi9hwL9whfO+sdxWlQFCigS8GhRgnz/LcdvpWQcLUicBrFjV2lgmTF 1p/J0q5hiHVU11RIMQN7KgABXzUj/M/i1gEgefXGtOUD+FQNXjHp4m9G9t+cAIT/duU9 hiyw== 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 g6si5376529plp.132.2019.01.04.13.24.26; Fri, 04 Jan 2019 13:24:42 -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 S1726233AbfADVWN (ORCPT + 99 others); Fri, 4 Jan 2019 16:22:13 -0500 Received: from gateway33.websitewelcome.com ([192.185.146.97]:48919 "EHLO gateway33.websitewelcome.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726015AbfADVWN (ORCPT ); Fri, 4 Jan 2019 16:22:13 -0500 Received: from cm12.websitewelcome.com (cm12.websitewelcome.com [100.42.49.8]) by gateway33.websitewelcome.com (Postfix) with ESMTP id 6D9E5743F0A for ; Fri, 4 Jan 2019 15:22:12 -0600 (CST) Received: from gator4166.hostgator.com ([108.167.133.22]) by cmsmtp with SMTP id fWuyggp1WiQerfWuygjAxD; Fri, 04 Jan 2019 15:22:12 -0600 X-Authority-Reason: nr=8 Received: from [189.250.130.205] (port=41040 helo=embeddedor) by gator4166.hostgator.com with esmtpa (Exim 4.91) (envelope-from ) id 1gfWux-002x5R-D2; Fri, 04 Jan 2019 15:22:11 -0600 Date: Fri, 4 Jan 2019 15:22:09 -0600 From: "Gustavo A. R. Silva" To: Matthew Wilcox , Hannes Reinecke , "James E.J. Bottomley" , "Martin K. Petersen" Cc: linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" Subject: [PATCH] scsi: advansys: use struct_size() in kzalloc() Message-ID: <20190104212209.GA15250@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.130.205 X-Source-L: No X-Exim-ID: 1gfWux-002x5R-D2 X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: (embeddedor) [189.250.130.205]:41040 X-Source-Auth: gustavo@embeddedor.com X-Email-Count: 17 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 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); -- 2.20.1