Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp3380591ybi; Mon, 10 Jun 2019 09:15:50 -0700 (PDT) X-Google-Smtp-Source: APXvYqwaHBQ4xRYwtG01ivSDlJOHlRQKw6oOAva2tuiYnuYdz4eL0UvBdpYH7aMJP8KqPvLFrI6b X-Received: by 2002:a63:fe51:: with SMTP id x17mr16097609pgj.339.1560183350140; Mon, 10 Jun 2019 09:15:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560183350; cv=none; d=google.com; s=arc-20160816; b=eaLajCkOl6PRr4IhYNM1O/CPyZwUCYon9J+TNmMfUmUnapwtPyOfwnqV1zVdap6DRs PB9Xfxv/K83FYLKSoLLoK6hLa/Jda+UPwgST5RFSaIV+gdb7d7nsbnCnA1FINjfx1V6v KQ4Ow9kpa+jJLeALZdJb6yzSSp5BeM2HVEtd11/ld90NTHawpPBvIhzU9yIf0uyzVrYF 98TQU+L3KFSYFRa6dYOG9hjxQk3fF2GvhpWzKsO8FEYIQ1dYjyeKd3JpBHlzr68wLEt1 WvT5w11A7DPceKAXHCAaaVSxzI8RIc4AchCn86Qq7ZlqsU+25lqUvUj0XJNJ3icJSbju ltJA== 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=auzQMxpCWiHvv2Pp6Z5LG1VRrOstOffy/YfKT1g4NHM=; b=SWHkW+9E5RcSL5xfhgPK1Jn5Ipk7kTJT4NmsvTCZfkpiJcbaSt3o0uFydR/6UL3vfG nRjjoS14ny5+LjNN8Rby9tMoTWCyRwUJ1vQ7rs09IFzuBjjcc40HMunvn9BMcs/vQVLx gBwPiXD/rGEDl7YDcBlPpiQXn0rkb7kQOTLoChGKBDrGuaHRUdktHW719vjpqxIAI+pT nwQY40sEe5GyXO+rhfi2NomDKX9Ze0lKbnY/FdNoCcwIHfZ2zodv5xb7yo4KA5EGQXER qM/aICRHQepLFyZCq6YGo7gS0P5rs5uYU/TI4v4CAwxgfGW8TJ+r+0r+20ys20J24hdi NpPw== 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 x1si10506130pfa.253.2019.06.10.09.15.34; Mon, 10 Jun 2019 09:15:50 -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 S2390139AbfFJPER (ORCPT + 99 others); Mon, 10 Jun 2019 11:04:17 -0400 Received: from gateway33.websitewelcome.com ([192.185.146.130]:19312 "EHLO gateway33.websitewelcome.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390128AbfFJPEQ (ORCPT ); Mon, 10 Jun 2019 11:04:16 -0400 Received: from cm11.websitewelcome.com (cm11.websitewelcome.com [100.42.49.5]) by gateway33.websitewelcome.com (Postfix) with ESMTP id ECF9A38F5 for ; Mon, 10 Jun 2019 10:04:15 -0500 (CDT) Received: from gator4166.hostgator.com ([108.167.133.22]) by cmsmtp with SMTP id aLqJhZEhydnCeaLqJhBode; Mon, 10 Jun 2019 10:04:15 -0500 X-Authority-Reason: nr=8 Received: from [189.250.75.107] (port=58560 helo=embeddedor) by gator4166.hostgator.com with esmtpa (Exim 4.92) (envelope-from ) id 1haLqI-000U5a-FH; Mon, 10 Jun 2019 10:04:15 -0500 Date: Mon, 10 Jun 2019 10:04:12 -0500 From: "Gustavo A. R. Silva" To: Jens Axboe Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" , Kees Cook Subject: [PATCH] block: bio: Use struct_size() in kmalloc() Message-ID: <20190610150412.GA8430@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: 1haLqI-000U5a-FH X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: (embeddedor) [189.250.75.107]:58560 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 bio_map_data { ... struct iovec iov[]; }; instance = kmalloc(sizeof(sizeof(struct bio_map_data) + sizeof(struct iovec) * 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, iov, count), GFP_KERNEL); This code was detected with the help of Coccinelle. Signed-off-by: Gustavo A. R. Silva --- block/bio.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/block/bio.c b/block/bio.c index 683cbb40f051..4bcdcd3f63f4 100644 --- a/block/bio.c +++ b/block/bio.c @@ -1120,8 +1120,7 @@ static struct bio_map_data *bio_alloc_map_data(struct iov_iter *data, if (data->nr_segs > UIO_MAXIOV) return NULL; - bmd = kmalloc(sizeof(struct bio_map_data) + - sizeof(struct iovec) * data->nr_segs, gfp_mask); + bmd = kmalloc(struct_size(bmd, iov, data->nr_segs), gfp_mask); if (!bmd) return NULL; memcpy(bmd->iov, data->iov, sizeof(struct iovec) * data->nr_segs); -- 2.21.0