Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp4917315imu; Tue, 29 Jan 2019 09:37:18 -0800 (PST) X-Google-Smtp-Source: AHgI3IYoLJpxHqwKbVO7moeaeuiLDAPB3sWBEETt8hgFX2PbxEd3CQ9POO4bHWrSgPdboWSebKTo X-Received: by 2002:a63:ac46:: with SMTP id z6mr4004471pgn.162.1548783438142; Tue, 29 Jan 2019 09:37:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548783438; cv=none; d=google.com; s=arc-20160816; b=dt47B2X348Of6qLpeOEF4m2YLNi9i3KB4aD2ekLWyTotqdnbjg+D7vIiTBhoMlUCsf tsv5DV3yLR9A0J7ou8DwZXUFOiM9Tj2wevBZ2SlMZSiglJeOHJPvOTA9ESsHwinjWiz1 9UAJSZLYWSXdJlGYlzcZzN44MiVCIyU1kG1uUtWzJRYMx8dJiWBF8A0/hl0VfRN45oF+ uxENWAQgEOAFvl3gJ8OAmriLdQm44h9hxIW1NT82DwyR1/60uCiR1LLjIygIRaTaMwVD tWSmbSA2dj1DkxFqjU6mWYIR+/3sHjSLejFeFmCNu82ZlE2cCgRUCHJqlb/OdauqCcHT BE1Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:autocrypt:openpgp:from:references:cc:to:subject; bh=lhljnRpexXCwOT7j0F2ebbAKzQ3gZ75DeHL8McJ+5qI=; b=xRzZX8rkKRjjpTIXyhBj/e/Re+CeJ8DWZowRsEEzOog4JVc56pd98tlDMCAxs8Smkb EymkBUY4W1/C5tpQvrNBJUIaNl62rBHwMUA8XR37mzT7jMchpKUE+f+8SKWgrR9mXbRT 81ebRpEEKIgGOYNdqdnMSx6/Q5EzafpS0F3sqg6ebkxLRzHwZHx6zi2YznbBCRLRrWAb R7641Jmz/TK4c5t2DAbjzbn/IOHJ2xibcE3t3NLmoc9kCJTNLJO8FpzfAc3l2GgQEuVV O/nPG2X0s2vxkiezbkn3zoNolWEk9H7duqpnp56oiXmnd1iZaovtKdV1aPYw6bdYjV4k nn0Q== 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 x33si5983931plb.43.2019.01.29.09.37.01; Tue, 29 Jan 2019 09:37:18 -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 S1728021AbfA2Rfb (ORCPT + 99 others); Tue, 29 Jan 2019 12:35:31 -0500 Received: from gateway24.websitewelcome.com ([192.185.50.73]:49514 "EHLO gateway24.websitewelcome.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726374AbfA2Rfa (ORCPT ); Tue, 29 Jan 2019 12:35:30 -0500 Received: from cm13.websitewelcome.com (cm13.websitewelcome.com [100.42.49.6]) by gateway24.websitewelcome.com (Postfix) with ESMTP id 25D91C9B56 for ; Tue, 29 Jan 2019 11:20:31 -0600 (CST) Received: from gator4166.hostgator.com ([108.167.133.22]) by cmsmtp with SMTP id oX3ng17JHYTGMoX3ngiQuG; Tue, 29 Jan 2019 11:20:31 -0600 X-Authority-Reason: nr=8 Received: from [189.250.130.205] (port=37068 helo=[192.168.1.76]) by gator4166.hostgator.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.91) (envelope-from ) id 1goX3m-003dsf-HP; Tue, 29 Jan 2019 11:20:30 -0600 Subject: Re: [PATCH] kvm: Use struct_size() in kmalloc() To: Paolo Bonzini , =?UTF-8?B?UmFkaW0gS3LEjW3DocWZ?= Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org References: <20190104162910.GA25118@embeddedor> From: "Gustavo A. R. Silva" Openpgp: preference=signencrypt Autocrypt: addr=gustavo@embeddedor.com; keydata= mQINBFssHAwBEADIy3ZoPq3z5UpsUknd2v+IQud4TMJnJLTeXgTf4biSDSrXn73JQgsISBwG 2Pm4wnOyEgYUyJd5tRWcIbsURAgei918mck3tugT7AQiTUN3/5aAzqe/4ApDUC+uWNkpNnSV tjOx1hBpla0ifywy4bvFobwSh5/I3qohxDx+c1obd8Bp/B/iaOtnq0inli/8rlvKO9hp6Z4e DXL3PlD0QsLSc27AkwzLEc/D3ZaqBq7ItvT9Pyg0z3Q+2dtLF00f9+663HVC2EUgP25J3xDd 496SIeYDTkEgbJ7WYR0HYm9uirSET3lDqOVh1xPqoy+U9zTtuA9NQHVGk+hPcoazSqEtLGBk YE2mm2wzX5q2uoyptseSNceJ+HE9L+z1KlWW63HhddgtRGhbP8pj42bKaUSrrfDUsicfeJf6 m1iJRu0SXYVlMruGUB1PvZQ3O7TsVfAGCv85pFipdgk8KQnlRFkYhUjLft0u7CL1rDGZWDDr NaNj54q2CX9zuSxBn9XDXvGKyzKEZ4NY1Jfw+TAMPCp4buawuOsjONi2X0DfivFY+ZsjAIcx qQMglPtKk/wBs7q2lvJ+pHpgvLhLZyGqzAvKM1sVtRJ5j+ARKA0w4pYs5a5ufqcfT7dN6TBk LXZeD9xlVic93Ju08JSUx2ozlcfxq+BVNyA+dtv7elXUZ2DrYwARAQABtCxHdXN0YXZvIEEu IFIuIFNpbHZhIDxndXN0YXZvQGVtYmVkZGVkb3IuY29tPokCPQQTAQgAJwUCWywcDAIbIwUJ CWYBgAULCQgHAgYVCAkKCwIEFgIDAQIeAQIXgAAKCRBHBbTLRwbbMZ6tEACk0hmmZ2FWL1Xi l/bPqDGFhzz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dAa1fCd4RsKLlhXda+IWpJZMHlmIKY8dlUybP+2qDzP2lY7kdFgPZRU+e zS/pzC/YTzAvCWM3tDgwoSl17vnZCr8wn2/1rKkcLvTDgiJLPCevqpTb6KFtZosQ02EGMuHQ I6Zk91jbx96nrdsSdBLGH3hbvLvjZm3C+fNlVb9uvWbdznObqcJxSH3SGOZ7kCHuVmXUcqoz ol6ioMHMb+InrHPP16aVDTBTPEGwgxXI38f7SUEn+NpbizWdLNz2hc907DvoPm6HEGCanpcA EQEAAYkCJQQYAQgADwUCWywcDAIbDAUJCWYBgAAKCRBHBbTLRwbbMdsZEACUjmsJx2CAY+QS UMebQRFjKavwXB/xE7fTt2ahuhHT8qQ/lWuRQedg4baInw9nhoPE+VenOzhGeGlsJ0Ys52sd XvUjUocKgUQq6ekOHbcw919nO5L9J2ejMf/VC/quN3r3xijgRtmuuwZjmmi8ct24TpGeoBK4 WrZGh/1hAYw4ieARvKvgjXRstcEqM5thUNkOOIheud/VpY+48QcccPKbngy//zNJWKbRbeVn imua0OpqRXhCrEVm/xomeOvl1WK1BVO7z8DjSdEBGzbV76sPDJb/fw+y+VWrkEiddD/9CSfg fBNOb1p1jVnT2mFgGneIWbU0zdDGhleI9UoQTr0e0b/7TU+Jo6TqwosP9nbk5hXw6uR5k5PF 8ieyHVq3qatJ9K1jPkBr8YWtI5uNwJJjTKIA1jHlj8McROroxMdI6qZ/wZ1ImuylpJuJwCDC ORYf5kW61fcrHEDlIvGc371OOvw6ejF8ksX5+L2zwh43l/pKkSVGFpxtMV6d6J3eqwTafL86 YJWH93PN+ZUh6i6Rd2U/i8jH5WvzR57UeWxE4P8bQc0hNGrUsHQH6bpHV2lbuhDdqo+cM9eh GZEO3+gCDFmKrjspZjkJbB5Gadzvts5fcWGOXEvuT8uQSvl+vEL0g6vczsyPBtqoBLa9SNrS VtSixD1uOgytAP7RWS474w== Message-ID: <4c4093b5-814d-3337-bc26-d6a491a7944e@embeddedor.com> Date: Tue, 29 Jan 2019 11:20:29 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.2.1 MIME-Version: 1.0 In-Reply-To: <20190104162910.GA25118@embeddedor> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit 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: 1goX3m-003dsf-HP X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: ([192.168.1.76]) [189.250.130.205]:37068 X-Source-Auth: gustavo@embeddedor.com X-Email-Count: 19 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 Hi all, Friendly ping: Who can take this? Thanks -- Gustavo On 1/4/19 10:29 AM, Gustavo A. R. Silva wrote: > 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; >