Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp1004504imu; Fri, 4 Jan 2019 11:06:50 -0800 (PST) X-Google-Smtp-Source: ALg8bN6tEyuLNKbek1pyE595NyiXvDyxBHWymQV3dyYfCITD17MoHEjYlcv6m5GmIe1uNErJpkiJ X-Received: by 2002:a63:d157:: with SMTP id c23mr21494022pgj.170.1546628810426; Fri, 04 Jan 2019 11:06:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1546628810; cv=none; d=google.com; s=arc-20160816; b=pkNVZGi+/drRYlKhSLg1G5QJ5RUyBpSm2vmXcAEZb+2n1XIK12b6lPD42qtv34J03U szL1IQTVWLv94DV8UhGc2nq/wKsPJo51DnHWNNbiS+6UKNs3J4nntQ6X2dNRBuArWkwQ Gpprv1p8Z5Sq0bn4eHsQQZLlzpLg363OTscZ7mX5pMwHSDKV53QSQSWdpDLQ0FoCmapF kbvfKID7GEXjDSwQz6ZkmaksNZgFgE0Vvhv59oz3pcHGkD1Oa2lilseqTGytr82pVUXM ewSb1HppI6fBi06mG4zmWlWYwBDnbXxGJ2iU1H46jBMx3xkiWZ7gzVnjorHxp5mYzOk9 ITnw== 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=qR4dhTa32FhuRpE7v7XJ1JT19D8u/9py39YA4Qcc/VQ=; b=rqMNY11k8JBBUjRAuT9tTOmArI/YIOTR1O0RhOZioHOqZhIi2hZ5SGiCvC75M74bMk AVfyp0I01YV6hZnObkQIxUnR60c9B2XDc0Hn9GLutqD+1i0dMEthgyZqXU55FvdNQL0z nLPaQheqRwcWzjBkDdDKum9dvSjzsq+ks/DTApC9k515DM318sg05aakXyGQoIJEgp4x dye2Qdafcrvz0MyNU8qEZlw1jXGE4KTNkjyGB84O68uTcjeRGZCOtnyPnat61furg/Co mya75/E1AmRUZ4ri9hkBRtmdABE4Bp1IgHLtl9In/Q4Y32m5ezt2q8zTljGRZNuf1UR7 ZlIg== 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 b15si13184473plm.431.2019.01.04.11.06.35; Fri, 04 Jan 2019 11:06:50 -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 S1727447AbfADSha (ORCPT + 99 others); Fri, 4 Jan 2019 13:37:30 -0500 Received: from gateway33.websitewelcome.com ([192.185.146.87]:46494 "EHLO gateway33.websitewelcome.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726036AbfADSha (ORCPT ); Fri, 4 Jan 2019 13:37:30 -0500 Received: from cm11.websitewelcome.com (cm11.websitewelcome.com [100.42.49.5]) by gateway33.websitewelcome.com (Postfix) with ESMTP id D77958555B5 for ; Fri, 4 Jan 2019 12:37:29 -0600 (CST) Received: from gator4166.hostgator.com ([108.167.133.22]) by cmsmtp with SMTP id fULZgmjDtdnCefULZgtUdL; Fri, 04 Jan 2019 12:37:29 -0600 X-Authority-Reason: nr=8 Received: from [189.250.130.205] (port=36800 helo=embeddedor) by gator4166.hostgator.com with esmtpa (Exim 4.91) (envelope-from ) id 1gfULZ-0014x6-4J; Fri, 04 Jan 2019 12:37:29 -0600 Date: Fri, 4 Jan 2019 12:37:26 -0600 From: "Gustavo A. R. Silva" To: Johannes Weiner , Michal Hocko , Vladimir Davydov Cc: cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" Subject: [PATCH] mm: memcontrol: use struct_size() in kmalloc() Message-ID: <20190104183726.GA6374@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: 1gfULZ-0014x6-4J X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: (embeddedor) [189.250.130.205]:36800 X-Source-Auth: gustavo@embeddedor.com X-Email-Count: 41 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 --- mm/memcontrol.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index af7f18b32389..ad256cf7da47 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -3626,8 +3626,7 @@ static int __mem_cgroup_usage_register_event(struct mem_cgroup *memcg, size = thresholds->primary ? thresholds->primary->size + 1 : 1; /* Allocate memory for new array of thresholds */ - new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold), - GFP_KERNEL); + new = kmalloc(struct_size(new, entries, size), GFP_KERNEL); if (!new) { ret = -ENOMEM; goto unlock; -- 2.20.1