Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp1125827imu; Fri, 4 Jan 2019 13:40:39 -0800 (PST) X-Google-Smtp-Source: ALg8bN6WB3AsQbgFuj+AxWQXh1XnIpkgRFHa5tq2/SvnfHDAxED9aPq9H7tCl0b4+X/nbsc0s0VV X-Received: by 2002:a17:902:d202:: with SMTP id t2mr53204175ply.193.1546638039669; Fri, 04 Jan 2019 13:40:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1546638039; cv=none; d=google.com; s=arc-20160816; b=SKnMl0OOyQ0Tmsiu9sK2v3n8tIuMs0aVdVT/xDAuEfkyqxsbKnCevEIyzK4jz4vkVI UGTA5d5LPYEflXsq5i2hU4X+qXDmHUvF73UwL7oyNWlNbH2IDpGeHLlBZ9NXqgwoCnIE xmBkYkPd7jVVkJnW+7EH5iSSmuk5QIVvlNz6QyGsqMgvl/afOaTTRfGP1tTzuebgCwE1 XwVc55yXZG6aqLORlUAWVDmjCr7CCHgf5X0flu0iL4NHHl3c2K6PSsMYS79gImjQ4c1Y LIQ4u6FruJg/ktYSsu+DzZ6nF7SdrhRrpK88u1nQ+Dzw3KIbXZcI+ipVxgfkQQQe15eK bPRA== 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=dZC5pJ8x5e7iRPb6Emw+7pptVxyRNgZUNMaDEM2n6yY=; b=AwiGDN5haHGN3MXQhSdn9gX++evJW1b5jGSOHFKEGdqmsf7TkaQpYPVzf8GieHmTKM V89z/mMmuhjdnsoJ1A+q5IPOjshIhqyCwxcv2RMEdx4Ucjn4WInkIikAPPix5SbFkwPh BbdTdl89AFrnsnuO34wYYrVnKSUvRMSNIz66up9PvLayMZP3XyhCSe3MVCAcWZnUGgLr LNtbaCr4g/fKFSzA5WTjdv2fDjdW3DNxe7jU8m58HIjWtZYoTbq7guU648VnaSWgBmYK 4agvKYl7KQpAcjFMsYnmW1JcxZ4NibrasHkyTI0CupwZvKlh/LGPAs4aPnSOALVUH1zs VTIQ== 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 k33si56218243pld.374.2019.01.04.13.40.24; Fri, 04 Jan 2019 13:40:39 -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 S1726311AbfADVjS (ORCPT + 99 others); Fri, 4 Jan 2019 16:39:18 -0500 Received: from gateway22.websitewelcome.com ([192.185.47.79]:16052 "EHLO gateway22.websitewelcome.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726149AbfADVjS (ORCPT ); Fri, 4 Jan 2019 16:39:18 -0500 Received: from cm13.websitewelcome.com (cm13.websitewelcome.com [100.42.49.6]) by gateway22.websitewelcome.com (Postfix) with ESMTP id 91CF31E618 for ; Fri, 4 Jan 2019 15:39:17 -0600 (CST) Received: from gator4166.hostgator.com ([108.167.133.22]) by cmsmtp with SMTP id fXBVgRLJBYTGMfXBVgDYCD; Fri, 04 Jan 2019 15:39:17 -0600 X-Authority-Reason: nr=8 Received: from [189.250.130.205] (port=41196 helo=embeddedor) by gator4166.hostgator.com with esmtpa (Exim 4.91) (envelope-from ) id 1gfXBU-00393l-6Q; Fri, 04 Jan 2019 15:39:16 -0600 Date: Fri, 4 Jan 2019 15:39:13 -0600 From: "Gustavo A. R. Silva" To: Greg Kroah-Hartman , Jiri Slaby Cc: linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" Subject: [PATCH] serial: max310x: Use struct_size() in devm_kzalloc() Message-ID: <20190104213913.GA21248@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: 1gfXBU-00393l-6Q X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: (embeddedor) [189.250.130.205]:41196 X-Source-Auth: gustavo@embeddedor.com X-Email-Count: 36 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 = devm_kzalloc(dev, 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 = devm_kzalloc(dev, struct_size(instance, entry, count), GFP_KERNEL); This code was detected with the help of Coccinelle. Signed-off-by: Gustavo A. R. Silva --- drivers/tty/serial/max310x.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/drivers/tty/serial/max310x.c b/drivers/tty/serial/max310x.c index 4f479841769a..1d700a8ab517 100644 --- a/drivers/tty/serial/max310x.c +++ b/drivers/tty/serial/max310x.c @@ -1197,8 +1197,7 @@ static int max310x_probe(struct device *dev, struct max310x_devtype *devtype, return PTR_ERR(regmap); /* Alloc port structure */ - s = devm_kzalloc(dev, sizeof(*s) + - sizeof(struct max310x_one) * devtype->nr, GFP_KERNEL); + s = devm_kzalloc(dev, struct_size(s, p, devtype->nr), GFP_KERNEL); if (!s) { dev_err(dev, "Error allocating port structure\n"); return -ENOMEM; -- 2.20.1