Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp1107763imu; Fri, 4 Jan 2019 13:14:49 -0800 (PST) X-Google-Smtp-Source: ALg8bN628qGOymG/TbtqYRlvZb2KaFpnPlqTZKoap8uDbzPVEA7vaX5caJzBwQW80V9xQPey4atN X-Received: by 2002:a17:902:2f03:: with SMTP id s3mr51350364plb.277.1546636489761; Fri, 04 Jan 2019 13:14:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1546636489; cv=none; d=google.com; s=arc-20160816; b=tVODl2iV2aW/r4tc/3t4ka9dburhiMgqNrRy2HQh13Hk05VF/SlzSC+XVFM8D7fH9f yOMgrEID9fXm5HMOC6IBzcKsr2YF/RayR0U3zxnGdQgqFbbbtUNmzV4lrJgN5A9Ge9dK +A3/R67tTZUZv1Z1VPCJESZ8RqUECtJgJSP++NQspkpKiBMtEFO6TcvIO/lDqE2dEf7g uUCib4xSdeOyj3XG0iUkfocliHMbhma4eS2kovCOrpFqnw2BZ8DzXPreCAUuvo+c/cny y+ttga2tBji05VOd+9tr6JDc9BMDjFR/XBetFmFubn+1yCQ8bwnabZz5jFkBHpFIqv// BHrA== 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=qS55RaBWYcNVv1UO7FZDKng2FuGZO5SVoR0Atq0pZ68=; b=0vZ//QQp7JqsksTIt8Vf0b/YDl14vbU1EA+H/xV/X32tmkABIFZHC3H6WfWw/RgHyQ KWXAvNF8G8zXgHXhN0+CSiexgBorWa1qUF+XjZJt3tyR6v9VwsKKxdvJCHnbuhvxiXHe aqXwgqPt/QlU2mzuwoScvW0eH8OYq/EsrWdDzor9HHp8POTtYwQDcZ8lKZk/IP7eivmS 4A3VXCv3V88lxnCVMQ2iF15W9Jv1sNHUKDdWtQQVlXxlS78r3XL31RIBtKBRMmO5Enxs 50t6gxi8GjJ3iLxlrAxWY6xVvSZ8jf06TIrJFw0z9OLjsOmxU2lXhWih222lDHBFhopp pKHw== 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 m64si61230804pfb.224.2019.01.04.13.14.33; Fri, 04 Jan 2019 13:14:49 -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 S1726143AbfADVLb (ORCPT + 99 others); Fri, 4 Jan 2019 16:11:31 -0500 Received: from gateway22.websitewelcome.com ([192.185.46.229]:34145 "EHLO gateway22.websitewelcome.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725987AbfADVLb (ORCPT ); Fri, 4 Jan 2019 16:11:31 -0500 Received: from cm17.websitewelcome.com (cm17.websitewelcome.com [100.42.49.20]) by gateway22.websitewelcome.com (Postfix) with ESMTP id 8010717061 for ; Fri, 4 Jan 2019 15:11:30 -0600 (CST) Received: from gator4166.hostgator.com ([108.167.133.22]) by cmsmtp with SMTP id fWkcgW5zn90onfWkcg11Hn; Fri, 04 Jan 2019 15:11:30 -0600 X-Authority-Reason: nr=8 Received: from [189.250.130.205] (port=40972 helo=embeddedor) by gator4166.hostgator.com with esmtpa (Exim 4.91) (envelope-from ) id 1gfWkb-002pQh-Q7; Fri, 04 Jan 2019 15:11:29 -0600 Date: Fri, 4 Jan 2019 15:11:28 -0600 From: "Gustavo A. R. Silva" To: Shaohua Li Cc: linux-raid@vger.kernel.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" Subject: [PATCH] md/raid10: Use struct_size() in kmalloc() Message-ID: <20190104211128.GA13343@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: 1gfWkb-002pQh-Q7 X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: (embeddedor) [189.250.130.205]:40972 X-Source-Auth: gustavo@embeddedor.com X-Email-Count: 3 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 = kmalloc(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 = kmalloc(struct_size(instance, entry, count), GFP_KERNEL); This code was detected with the help of Coccinelle. Signed-off-by: Gustavo A. R. Silva --- drivers/md/raid10.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/drivers/md/raid10.c b/drivers/md/raid10.c index abb5d382f64d..320cb430cca6 100644 --- a/drivers/md/raid10.c +++ b/drivers/md/raid10.c @@ -4788,8 +4788,7 @@ static int handle_reshape_read_error(struct mddev *mddev, int idx = 0; struct page **pages; - r10b = kmalloc(sizeof(*r10b) + - sizeof(struct r10dev) * conf->copies, GFP_NOIO); + r10b = kmalloc(struct_size(r10b, devs, conf->copies), GFP_NOIO); if (!r10b) { set_bit(MD_RECOVERY_INTR, &mddev->recovery); return -ENOMEM; -- 2.20.1