From mboxrd@z Thu Jan 1 00:00:00 1970 From: Radim =?utf-8?B?S3LEjW3DocWZ?= Subject: Re: [PATCH] kvm: Use struct_size() in kmalloc() Date: Fri, 11 Jan 2019 14:53:43 +0100 Message-ID: <20190111135342.GH14852@flask> References: <20190104162910.GA25118@embeddedor> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org To: "Gustavo A. R. Silva" Return-path: Content-Disposition: inline In-Reply-To: <20190104162910.GA25118@embeddedor> Sender: linux-kernel-owner@vger.kernel.org List-Id: kvm.vger.kernel.org 2019-01-04 10:29-0600, Gustavo A. R. Silva: > 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 > --- Queued, thanks.