Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp1977613imu; Thu, 17 Jan 2019 06:28:01 -0800 (PST) X-Google-Smtp-Source: ALg8bN6/9ism8sY6t+bRtQflxcoSDVZ0/4T7EqLfA4ptFXIjCze99JXtnIy4QFAIYrMhI346rTL5 X-Received: by 2002:a63:fd53:: with SMTP id m19mr13804598pgj.340.1547735281660; Thu, 17 Jan 2019 06:28:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1547735281; cv=none; d=google.com; s=arc-20160816; b=RIID1ma6UOLVBzHC0q0VCJ/pGHyWxYQrjmZHSYh7VOPxIF2mgXhfs0SvZJThrN5ZwZ IlyDmLmp+Y41tqIEI2wReHDx3OCNlk3YXf/GJAI+3SdNu0B5Iui84IjlDUKvBeJMj+QF ZIG7KS4Uok2+9ZolqzlfSkgiFQ4Q9g0HAjI/rAat2r2BdnLepY51BlI5Ez3FDFBDOsUL U5odw/tNyDKJ+OQadcuIHE6gWyzIhv0afsCF7uZ460ElWOZE2nLeN6ZtOQ04K1xdHjhJ EOD1Kc7A6qBh1PpHV17MVjvpmKWreUi5wjaAh78U1zmgy+yIjuPU/JP/SXR7ma9AJbHt DsbQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=6Dm8L+k0shzfS/c/F8HVP6yw3Qqt/VIEBz4LrddkGM8=; b=UHUPNoorSr+8cm5ktpditmmuDOS+cNK0V91vKF4UtB/RIf0xpoDBH/xhADGoF7oLIW svUn36NsQzptWECYTsHqAITBFJuopRtOspZYNqLbt0FkiKSBF1UqhihTvQ4NQp69ZExn RMEU5qmcY6/7Jkn+2JjLDm7JbafMirTftS0ksX+/SGe5eUf/vL3zJbgXo7xuZTDdn7Lm ktQPv2/nlfeD72duw4FldU6I1pzWrl2NI2AbFCq7FUMLM8s8wtPx3cDhlyvQB++lYB/b fUe43CHYFtqihQWXiH8nAusX5eYkjzOd9+93BFzQf7nu3lDJYSg3BsF5noLGDTUSC8vu dq4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Vn4hDx6K; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a8si1598109pgi.359.2019.01.17.06.27.45; Thu, 17 Jan 2019 06:28:01 -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; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Vn4hDx6K; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726898AbfAQORq (ORCPT + 99 others); Thu, 17 Jan 2019 09:17:46 -0500 Received: from mail-io1-f68.google.com ([209.85.166.68]:41921 "EHLO mail-io1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725935AbfAQORp (ORCPT ); Thu, 17 Jan 2019 09:17:45 -0500 Received: by mail-io1-f68.google.com with SMTP id s22so7901255ioc.8; Thu, 17 Jan 2019 06:17:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=6Dm8L+k0shzfS/c/F8HVP6yw3Qqt/VIEBz4LrddkGM8=; b=Vn4hDx6KgsBz8FwF5a74tjqi7RFW1hPrt6XPEEcsWGWSrAPs1N+9mx591UktvqqJxy Vb0PZiwh/eSso/ZZb3VHzr/fohA/+yKHDs3Iz5Inbzdob4fEetj6Bn0xtWiNZqLdyy5+ VaPgXeTNnCWdJ9rc9LHGMabRqWafDKov6ccGBpug0ey0gaDNk94l4fGl0n8fK17UCkAf G1LQwlYfNPuprCnl7sAdfAbwaNiad43Vo4oZEDqw/SQQWNVrM0yx72/s18WfCLo6VnAQ AEku7G//99yqywqEEL9CsG11m1ckoY8Z30V9XLXAzyYZXGMWb37Z52SOpJDJRzB7MtiY DtgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=6Dm8L+k0shzfS/c/F8HVP6yw3Qqt/VIEBz4LrddkGM8=; b=d4Iipi7Q+KrYVdNx+axPxwaGqzbfU9ZuIAVFoKgIogw6ajMiiud08Hae/rPF1wAA9s 7BrzDHUxBzga9WFf4c0miJjiSKZtPaN35JzpTLzAXlklnjVHHEfWbLjN+PQZ9DcF1Mdn 2y/hIOmQGhteD51qtQjfqI/vJspbixwwhbAf7iGO7agzQ9ZHG9++wtvPResrtvbUDjb4 WvnhOXkG47yjSIAhRFlGHg/2J7OD2tU5IomEeukU+MXV8SEHxu9bV3x62cc4Qo8E30Oc ekL0SwHeeiKSm8/bjoSoPFpwfbJL8+plqLGhZhY/uy8bPmWqZvYQGO6n9CnrZwiUHnk3 3O7g== X-Gm-Message-State: AJcUukdG010VXZ//UdMGW/sr3bj2XuZ7B1fTLcjhPjYusjC5D89yFFOI MtfUIwnb87zhd6+kzs2tcZswhw0EBQlwaZHXuTq2YwaP X-Received: by 2002:a6b:92d7:: with SMTP id u206mr8075903iod.174.1547734664644; Thu, 17 Jan 2019 06:17:44 -0800 (PST) MIME-Version: 1.0 References: <20190115194153.GA9565@embeddedor> In-Reply-To: <20190115194153.GA9565@embeddedor> From: Ilya Dryomov Date: Thu, 17 Jan 2019 15:17:33 +0100 Message-ID: Subject: Re: [PATCH net-next] libceph, ceph: use struct_size() in kmalloc() To: "Gustavo A. R. Silva" Cc: "Yan, Zheng" , Sage Weil , "David S. Miller" , Ceph Development , netdev , linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jan 15, 2019 at 8:41 PM 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; > struct boo entry[]; > }; > > instance = kmalloc(sizeof(struct foo) + count * sizeof(struct boo), 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 > --- > net/ceph/osdmap.c | 5 ++--- > 1 file changed, 2 insertions(+), 3 deletions(-) > > diff --git a/net/ceph/osdmap.c b/net/ceph/osdmap.c > index 98c0ff3d6441..48a31dc9161c 100644 > --- a/net/ceph/osdmap.c > +++ b/net/ceph/osdmap.c > @@ -495,9 +495,8 @@ static struct crush_map *crush_decode(void *pbyval, void *end) > / sizeof(struct crush_rule_step)) > goto bad; > #endif > - r = c->rules[i] = kmalloc(sizeof(*r) + > - yes*sizeof(struct crush_rule_step), > - GFP_NOFS); > + r = kmalloc(struct_size(r, steps, yes), GFP_NOFS); > + c->rules[i] = r; > if (r == NULL) > goto badmem; > dout(" rule %d is at %p\n", i, r); Applied. Thanks, Ilya