Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp5068179imu; Tue, 8 Jan 2019 10:56:10 -0800 (PST) X-Google-Smtp-Source: ALg8bN4jhP5NYGkW3tVM6HmRpvAOjzrC2OnLp2E0WXT+P0s6wRg41Awb3EJ0wSYwfrOGsLt6OCGj X-Received: by 2002:a63:5e43:: with SMTP id s64mr2553021pgb.101.1546973770437; Tue, 08 Jan 2019 10:56:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1546973770; cv=none; d=google.com; s=arc-20160816; b=RgSCj3L3gvfbOefP+lRMSr1YtVC2KGWCzMkBOTRGq0M3QP0mEGv8u3GRySWNuMqEnL C3+Tz2Fmmjj4SjUvIc3wVa7kMbDbaTPUbcQo6ahiHPOzsgcS1EP19uAl3EW4Y104dnV4 5EQxc58nCsFfcwWz520hyKZCfEObKYwSnX3zP9uhI7txNwDJpwEAjoMHZn1wohpdivgH Y5f5Di3EVJunaYfMhP1BnpqTG/zj8XvVCDkrimuDLPKsKVWPLsVGcWp+XhKmqVWjR7Sg 2FW6ERSPrzwvSlrscDr++y/5rjyQHPw0kytTGokiIzG+Fpc9cGQv09nC81hIGHUmp8Rr 9BxA== 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=j8G2vGGF0S6/eWSLZMBZsEFtKdNk/TmsVFkl3WQUyzA=; b=uvGzCunWCUiaTUmR0ufefUzviwBM6QWbHV/08AZX0a9G2U7LOCitsJPU6Y5Wh9NaoE 0JFf5XsAJD1SZR3NVfE/wG9t5jx4mswaPvKktzN4+9cH9w+YZbqCs7EBymcA+U2tKePf Leef0J+z/NNQWDh9GSCcOw6bH5/7eMD7zbRoFwDl4LJffi1UfHfs/TFX1pvAmzgNtjrI LH8fbAwtx/3Pf5wSlXWFpcSUtUhjfCEcYPaO2WDWsZ9peiiEfIrPh++p5Oo5Qzg8HkXg ++bDbkrhH8rRLdqVPPpUD6dp/HaVtK44se+ezokciwFEGsQsJNmhoTKzklIdEm6s+qRN kfYg== 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 l4si732473pgr.346.2019.01.08.10.55.55; Tue, 08 Jan 2019 10:56:10 -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 S1729487AbfAHSh3 (ORCPT + 99 others); Tue, 8 Jan 2019 13:37:29 -0500 Received: from gateway31.websitewelcome.com ([192.185.143.39]:48011 "EHLO gateway31.websitewelcome.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729041AbfAHSh2 (ORCPT ); Tue, 8 Jan 2019 13:37:28 -0500 Received: from cm13.websitewelcome.com (cm13.websitewelcome.com [100.42.49.6]) by gateway31.websitewelcome.com (Postfix) with ESMTP id 1B747FD09 for ; Tue, 8 Jan 2019 12:37:28 -0600 (CST) Received: from gator4166.hostgator.com ([108.167.133.22]) by cmsmtp with SMTP id gwFkgSbPJYTGMgwFkgE2TX; Tue, 08 Jan 2019 12:37:28 -0600 X-Authority-Reason: nr=8 Received: from [189.250.130.205] (port=55342 helo=embeddedor) by gator4166.hostgator.com with esmtpa (Exim 4.91) (envelope-from ) id 1ggwFi-0035m5-9E; Tue, 08 Jan 2019 12:37:27 -0600 Date: Tue, 8 Jan 2019 12:37:20 -0600 From: "Gustavo A. R. Silva" To: Jeremy Kerr , Arnd Bergmann , Benjamin Herrenschmidt , Paul Mackerras , Michael Ellerman Cc: linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" Subject: [PATCH] powerpc/spufs: use struct_size() in kmalloc() Message-ID: <20190108183720.GA14238@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: 1ggwFi-0035m5-9E X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: (embeddedor) [189.250.130.205]:55342 X-Source-Auth: gustavo@embeddedor.com X-Email-Count: 14 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 = 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 --- arch/powerpc/platforms/cell/spufs/file.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/arch/powerpc/platforms/cell/spufs/file.c b/arch/powerpc/platforms/cell/spufs/file.c index ae8123edddc6..48c2477e7e2a 100644 --- a/arch/powerpc/platforms/cell/spufs/file.c +++ b/arch/powerpc/platforms/cell/spufs/file.c @@ -2338,9 +2338,8 @@ static int spufs_switch_log_open(struct inode *inode, struct file *file) goto out; } - ctx->switch_log = kmalloc(sizeof(struct switch_log) + - SWITCH_LOG_BUFSIZE * sizeof(struct switch_log_entry), - GFP_KERNEL); + ctx->switch_log = kmalloc(struct_size(ctx->switch_log, log, + SWITCH_LOG_BUFSIZE), GFP_KERNEL); if (!ctx->switch_log) { rc = -ENOMEM; -- 2.20.1