Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp932394imu; Fri, 4 Jan 2019 09:44:21 -0800 (PST) X-Google-Smtp-Source: ALg8bN73iNu6B3NyWqG5pyakdHzBLScQHMpwrp2OW3tjgsSvv6cNZK6qQ4E4eMcmKDEdK++b21xP X-Received: by 2002:a63:f552:: with SMTP id e18mr21684195pgk.239.1546623861349; Fri, 04 Jan 2019 09:44:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1546623861; cv=none; d=google.com; s=arc-20160816; b=sQ/jd8amw2m+LPZzCXuPvnSdOONFXy8G8OvEE0cKggZFnKFGjEzJmJfiZ/UQ8XwPoX VW/M0wf30K6qTUVCPXPtOZkMQzsFAwQk8qj7zagi1rUVWZ+angO2i3NdRU0Xx843DwRP JDLTtPJlNvpYEm4yuoRzpouAIO3EDpYZjMgksI+tzZJIDA6e5yiTOLp7vANSoZmSTDP6 cYB6QmcWz74/2BbOJiqPISS2/n+ED6vHX+3ixAvz+CHOluTWJib56FYsnbg5oWZvJVjR vbc+Tw7mYAIbrOaiLDH2sG8BKmX9aBGgllN5RdFlNFdxjN5jdG9QilSQKnYjUoXm9fnM T4+w== 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=r36xRBfAJETgftkosiH3MK7/Auv0vkmXxUpG4y+uXjg=; b=gZQ+2LzQIp3PHY3X9nTKOlnA3jwer9ic61lp3XYkfTx4hqr/NEV41LadAWTGvBi82Q 5YjnF92lmBtsUb5PpZhBwpxTPzoLIN1Txft8OOuOTJm6OmIuHwnspWZinUUa+JTwGCym UxDdXi/x7ANWi18nMkP2HWmlxIGQDcLxgvx9Q5U9PNiC/ixD7AiNgQoOY2MAnRg/azor yXp4pXwqeWCnVQLXcC4efIK5bNOx+rEfmLh/Kmi2Pg0cMlnx4sthh+5sVeGQoVsDgSb9 627DekHnucmCwsQpkmXhuObAJ/7+vwKIEWn6Q6oDbOxu38cEWQwtQ9vEd6gjPmvdfuRx NiNg== 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 u7si8819282pfu.270.2019.01.04.09.44.05; Fri, 04 Jan 2019 09:44:21 -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 S1726404AbfADQ3P (ORCPT + 99 others); Fri, 4 Jan 2019 11:29:15 -0500 Received: from gateway22.websitewelcome.com ([192.185.46.187]:42627 "EHLO gateway22.websitewelcome.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726262AbfADQ3P (ORCPT ); Fri, 4 Jan 2019 11:29:15 -0500 Received: from cm11.websitewelcome.com (cm11.websitewelcome.com [100.42.49.5]) by gateway22.websitewelcome.com (Postfix) with ESMTP id 9D2A9F901 for ; Fri, 4 Jan 2019 10:29:14 -0600 (CST) Received: from gator4166.hostgator.com ([108.167.133.22]) by cmsmtp with SMTP id fSLSgktEpdnCefSLSgrfmn; Fri, 04 Jan 2019 10:29:14 -0600 X-Authority-Reason: nr=8 Received: from [189.250.130.205] (port=34226 helo=embeddedor) by gator4166.hostgator.com with esmtpa (Exim 4.91) (envelope-from ) id 1gfSLR-004CcO-Re; Fri, 04 Jan 2019 10:29:14 -0600 Date: Fri, 4 Jan 2019 10:29:10 -0600 From: "Gustavo A. R. Silva" To: Paolo Bonzini , Radim =?utf-8?B?S3LEjW3DocWZ?= Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" Subject: [PATCH] kvm: Use struct_size() in kmalloc() Message-ID: <20190104162910.GA25118@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: 1gfSLR-004CcO-Re X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: (embeddedor) [189.250.130.205]:34226 X-Source-Auth: gustavo@embeddedor.com X-Email-Count: 4 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 --- virt/kvm/kvm_main.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 666d0155662d..635b12fdbab8 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3709,8 +3709,8 @@ int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr, if (bus->dev_count - bus->ioeventfd_count > NR_IOBUS_DEVS - 1) return -ENOSPC; - new_bus = kmalloc(sizeof(*bus) + ((bus->dev_count + 1) * - sizeof(struct kvm_io_range)), GFP_KERNEL); + new_bus = kmalloc(struct_size(bus, range, bus->dev_count + 1), + GFP_KERNEL); if (!new_bus) return -ENOMEM; @@ -3755,8 +3755,8 @@ void kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx, if (i == bus->dev_count) return; - new_bus = kmalloc(sizeof(*bus) + ((bus->dev_count - 1) * - sizeof(struct kvm_io_range)), GFP_KERNEL); + new_bus = kmalloc(struct_size(bus, range, bus->dev_count - 1), + GFP_KERNEL); if (!new_bus) { pr_err("kvm: failed to shrink bus, removing it completely\n"); goto broken; -- 2.20.1