Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp1311734imu; Wed, 16 Jan 2019 17:00:19 -0800 (PST) X-Google-Smtp-Source: ALg8bN6iOs7CZQhrM6t+iwXJQDJjxg3mdhgXiY8jbTjdLIl6JXB1WYOpwWW3HU9fLihXVkftIIt6 X-Received: by 2002:a63:9041:: with SMTP id a62mr11330203pge.163.1547686819537; Wed, 16 Jan 2019 17:00:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1547686819; cv=none; d=google.com; s=arc-20160816; b=gtfjI1+kJkJSt9GmC/EncHc6EEOutGQ87Yq0+nklxrOBYKQ8plZprNfrFKNpL2Khd2 ocKZyt1OHFryJAhHPrHqA4rZWrxkyObkXv59sEdDXorzgiAbSrIIqLxQiVVYkuGpcUxw qC/Qw1Rul9d49Nsr/4o6K/ijMKy0Wr6GBn3haoNYUuazsfg9SQdvj+qv8JHLXeuwwVJe KpkrFEphG5zZRBHD+FmBZes2ibwU0MqE63vc2QFNGy5ivdA0PjLdUqhg++NSDWs54J2m 89/Z1CkiReL3tsPGv2SKM3HPzGjL6KfX3eyYxBBMF+tDE+VlKwLPSrm+ZaS71OqlinSu VP2w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=a9qwgbMsU+nQM0XDv5oDwjbJQ+VvSRlOu0ueCcYUdLY=; b=TLPRCCBj0Y49cZTQjjTK9SBw7cHnIO/VDBl7am3Glmr8jCVxR5g8fn+F2lqbSNkFCV Q/o2aHFVx7rIt6PjVzGiRQGKCP04xbhW6ov5yAskA7ggYm8kFVEdpnwG1B37gEj74YvY /804rrZe3zD7sI9iTBBN3wNn65yaVM9zgR5hz5q76aAFiw7cC+Nu3rR2BgJSgrKA/GAK thTxo2GQzuuuZDvLkxNDL/vcPX1gSQUVtxz8c/EOSmGlpXobeVxnpDI6Oo5rlCoOecmK NGKK2alVCOPG+8yyC0n6FNMDI25xq7KiLtw/xaI5EaOAN6vQ+huicRYdGWO/CrxZ+fMb s/TQ== 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 y22si20582pfa.6.2019.01.16.17.00.04; Wed, 16 Jan 2019 17:00:19 -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 S2388092AbfAPXHu (ORCPT + 99 others); Wed, 16 Jan 2019 18:07:50 -0500 Received: from fieldses.org ([173.255.197.46]:49372 "EHLO fieldses.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727337AbfAPXHt (ORCPT ); Wed, 16 Jan 2019 18:07:49 -0500 Received: by fieldses.org (Postfix, from userid 2815) id 3CF512013; Wed, 16 Jan 2019 18:07:49 -0500 (EST) Date: Wed, 16 Jan 2019 18:07:49 -0500 From: Bruce Fields To: Chuck Lever Cc: "Gustavo A. R. Silva" , Trond Myklebust , Anna Schumaker , Jeff Layton , "David S. Miller" , Linux NFS Mailing List , netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH net-next] svcrdma: Use struct_size() in kmalloc() Message-ID: <20190116230749.GB5948@fieldses.org> References: <20190115211140.GA16131@embeddedor> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) 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 04:49:16PM -0500, Chuck Lever wrote: > > > On Jan 15, 2019, at 4:11 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 > > Reviewed-by: Chuck Lever Thanks, applying for 5.1.--b. > > > > --- > > net/sunrpc/xprtrdma/svc_rdma_rw.c | 3 +-- > > 1 file changed, 1 insertion(+), 2 deletions(-) > > > > diff --git a/net/sunrpc/xprtrdma/svc_rdma_rw.c b/net/sunrpc/xprtrdma/svc_rdma_rw.c > > index dc1951759a8e..cad37c2122ab 100644 > > --- a/net/sunrpc/xprtrdma/svc_rdma_rw.c > > +++ b/net/sunrpc/xprtrdma/svc_rdma_rw.c > > @@ -64,8 +64,7 @@ svc_rdma_get_rw_ctxt(struct svcxprt_rdma *rdma, unsigned int sges) > > spin_unlock(&rdma->sc_rw_ctxt_lock); > > } else { > > spin_unlock(&rdma->sc_rw_ctxt_lock); > > - ctxt = kmalloc(sizeof(*ctxt) + > > - SG_CHUNK_SIZE * sizeof(struct scatterlist), > > + ctxt = kmalloc(struct_size(ctxt, rw_first_sgl, SG_CHUNK_SIZE), > > GFP_KERNEL); > > if (!ctxt) > > goto out; > > -- > > 2.20.1 > > -- > Chuck Lever > >