Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp3650642ybi; Mon, 10 Jun 2019 14:09:09 -0700 (PDT) X-Google-Smtp-Source: APXvYqxCNplUjPVtMivO+1FHGJWPfXsAMf0mq5ccjbr0fe6tapA4eRnXDnyXly+Y6JZa8tsE2CrF X-Received: by 2002:a17:902:9b94:: with SMTP id y20mr57457838plp.260.1560200949890; Mon, 10 Jun 2019 14:09:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560200949; cv=none; d=google.com; s=arc-20160816; b=pGhkmAEIVuQmWZ0W+5lRyJrJqdgOOEdfLdck7Dsmn9u/3WpIsTATaooQ/zewhx7WJd /8lKrTAz1NUi7oUypklDsLVnMZnNKCV4vPklBsY9D/7KrxYUIq3rBjh+79AszNWvD1hK 8QHgteJk5DZCRjSV3egPdPr/XlAFYhjsaSS2hz3rssrGwHKb0KGJTuc8z1hv6TQFPj22 DrSNRG0GqlDtZ8L7BY2lqIoLqw2YeFEz2CTjkv4RoJntGc8yBaOxhtaTlavsw2wuDpGj lxCzgN12c3jWELwaKZ7qtI4N20RbuJlKg0PmJX5inu0IV2o3yzVn5UHyFneAPm7cFgoi rO+A== 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=Gjw7dAN6YUIWi0z+6cxWGnkgM1V/P1GVUAlhlb6SAA4=; b=o9bAdj2lZDLKXVSrtjJu1lthfPsLVRghnaKO4MSJW8QxfD8jnKWiRerbjjJ/+IMfSs m7hf2M4KpPWFCE21U+EUNZHSZCygg7uBpf7K8b97lkfzqpJ+Itr1BrHKn7KOxwG/i2TH 9FUdjBEQgcq2GBsf2b4mhEAb5o1+Yv23J6/j92PBqF5M9Xb7eyK/FEsBPpG/7vzhTabd yc8YzRST6trCJv0LFaVTSgcsKBXBqwAmXRwJjG+gl4lpBAK4D5Rk1bR7WjkSXT/YIBXz 41ZzvLOy5B83mRL+x14Z5D1LdWl49X4c1ZWK+GtsR0J+PYFCT5WU/M9Eh3RTJVIfJ9Kq qDAQ== 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 r129si10721293pgr.307.2019.06.10.14.08.53; Mon, 10 Jun 2019 14:09:09 -0700 (PDT) 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 S2389858AbfFJVGg (ORCPT + 99 others); Mon, 10 Jun 2019 17:06:36 -0400 Received: from gateway23.websitewelcome.com ([192.185.50.129]:28313 "EHLO gateway23.websitewelcome.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388311AbfFJVGf (ORCPT ); Mon, 10 Jun 2019 17:06:35 -0400 Received: from cm13.websitewelcome.com (cm13.websitewelcome.com [100.42.49.6]) by gateway23.websitewelcome.com (Postfix) with ESMTP id B17EAD64F for ; Mon, 10 Jun 2019 16:06:34 -0500 (CDT) Received: from gator4166.hostgator.com ([108.167.133.22]) by cmsmtp with SMTP id aRUwhcEecYTGMaRUwhZQNy; Mon, 10 Jun 2019 16:06:34 -0500 X-Authority-Reason: nr=8 Received: from [189.250.75.107] (port=48826 helo=embeddedor) by gator4166.hostgator.com with esmtpa (Exim 4.92) (envelope-from ) id 1haRUd-003eSZ-CB; Mon, 10 Jun 2019 16:06:33 -0500 Date: Mon, 10 Jun 2019 16:06:13 -0500 From: "Gustavo A. R. Silva" To: Dan Williams , Vishal Verma , Dave Jiang , Keith Busch , Ira Weiny Cc: linux-nvdimm@lists.01.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" Subject: [PATCH] libnvdimm, region: Use struct_size() in kzalloc() Message-ID: <20190610210613.GA21989@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.75.107 X-Source-L: No X-Exim-ID: 1haRUd-003eSZ-CB X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: (embeddedor) [189.250.75.107]:48826 X-Source-Auth: gustavo@embeddedor.com X-Email-Count: 7 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 nd_region { ... struct nd_mapping mapping[0]; }; instance = kzalloc(sizeof(struct nd_region) + sizeof(struct nd_mapping) * 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, mapping, count), GFP_KERNEL); This code was detected with the help of Coccinelle. Signed-off-by: Gustavo A. R. Silva --- drivers/nvdimm/region_devs.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/drivers/nvdimm/region_devs.c b/drivers/nvdimm/region_devs.c index b4ef7d9ff22e..88becc87e234 100644 --- a/drivers/nvdimm/region_devs.c +++ b/drivers/nvdimm/region_devs.c @@ -1027,10 +1027,9 @@ static struct nd_region *nd_region_create(struct nvdimm_bus *nvdimm_bus, } region_buf = ndbr; } else { - nd_region = kzalloc(sizeof(struct nd_region) - + sizeof(struct nd_mapping) - * ndr_desc->num_mappings, - GFP_KERNEL); + nd_region = kzalloc(struct_size(nd_region, mapping, + ndr_desc->num_mappings), + GFP_KERNEL); region_buf = nd_region; } -- 2.21.0