From patchwork Fri Jan 4 18:37:26 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Gustavo A. R. Silva" X-Patchwork-Id: 10748777 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1380891E for ; Fri, 4 Jan 2019 18:37:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0530028647 for ; Fri, 4 Jan 2019 18:37:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EAAC328669; Fri, 4 Jan 2019 18:37:32 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8F1CC28647 for ; Fri, 4 Jan 2019 18:37:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 872748E0104; Fri, 4 Jan 2019 13:37:31 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 8212D8E00F9; Fri, 4 Jan 2019 13:37:31 -0500 (EST) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 711FE8E0104; Fri, 4 Jan 2019 13:37:31 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ot1-f70.google.com (mail-ot1-f70.google.com [209.85.210.70]) by kanga.kvack.org (Postfix) with ESMTP id 48A578E00F9 for ; Fri, 4 Jan 2019 13:37:31 -0500 (EST) Received: by mail-ot1-f70.google.com with SMTP id o8so17057808otp.16 for ; Fri, 04 Jan 2019 10:37:31 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:date:from:to :cc:subject:message-id:mime-version:content-disposition:user-agent; bh=qR4dhTa32FhuRpE7v7XJ1JT19D8u/9py39YA4Qcc/VQ=; b=tyAM1SvbCAlNDeJIYajK07vycXc+yiPu/HiqIMirCGPocweq/85uKQckXCz/u/veTX wwluyPr4rSwEidfZMQ5vWUCvHUzeLQQTo8/8yMeqEXWANQXrrg+hgFHeRNmnLLbYmNh/ 65jJKmvkRfXyGeCCvKF8UDbZkXJKHf3Yi1tHDdfowqmDhVqITQByFuhMYdGzKerI/Bdc 9VW5hXxhFm7gobzpQjGlXMhhbO/wowkKyPhTVPcBLCIIjuR1TEAhnSJxffYL9Rdd277U odeCU2WQrU3FjjuGhbC1ZuqO6D45HToRbDnx55M0owuwy/9UY7xAxiAecqOxWUcxwkvr cr4w== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of gustavo@embeddedor.com designates 192.185.63.14 as permitted sender) smtp.mailfrom=gustavo@embeddedor.com X-Gm-Message-State: AJcUuke72hTaUzGWW1QtvQyyC4mqblcSPFm2+Z9OsPRhco26YYMuzuxt +Jtc3sH44v4+JSmFwOJaybS0/TFCqcgyDlGqVKbUcDOqPpG/E5mYJ0CCo2AIw4vNWtZ2tuaidKz j167fFtfnJTBcgCoQcJelP9Bu5TGn1RiO8ucpgiJtOTpxnDx2LJswJkqo3URlNmzyFQ== X-Received: by 2002:a9d:d83:: with SMTP id 3mr33934457ots.361.1546627050940; Fri, 04 Jan 2019 10:37:30 -0800 (PST) X-Google-Smtp-Source: ALg8bN5CTuVt8lkYeMMuhCkLXhlp/3GOKqt0Iz4jI4S772hv3byhVTYnnqDm86XGGl2K7UykdVD4 X-Received: by 2002:a9d:d83:: with SMTP id 3mr33934443ots.361.1546627050354; Fri, 04 Jan 2019 10:37:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1546627050; cv=none; d=google.com; s=arc-20160816; b=GDLrP+IJ9xGU7J6U38Rs7qiKuDsAxJ9MuoojM4eRyMlWdufVXTdL/kQYjTtXoVlFQf bRMjEtnWGIiiGCLAMzelXIEIQV4X5P4uoxG/ODovydU6s/HLZewuYodDhBqmRv/P9Qoi eRFajBkhUmRBub7HcOFQq3T1fHB3eReDJkKJQZdvd7z+tp/t7Ip8mQmBkLo+h51Er2l3 W4g69AJFsEYShV4K9Y0m4z/kkaomVJz8aV9eOesadD5P7JTMIurc6BC6H6jVWhf4ExMS 3JK5ljZq3gJIKFkKAJOxWhiveJl49WCygk1H4ql4x+/HEzcYv2W18IC/DcEOjbyZG7vg gJGA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=user-agent:content-disposition:mime-version:message-id:subject:cc :to:from:date; bh=qR4dhTa32FhuRpE7v7XJ1JT19D8u/9py39YA4Qcc/VQ=; b=V7liSVXeNeByvqlLEMy2Wmp0l7WDiPOCEF/xm05lASOSGlp59RfzT01xq6dnPim512 zjdU5VX5lwy6YfXGKNZHBC2RS0zaW9eER2eaE95OWhK2fPKGOZBxGeXtrq4WEqTQ4VpL wWH0hMm0fYC9ZZcz13vG5qey48c2bWZ79vsnfC4NfMQR/6/ouIUgTEebe0b0NbUT6jv5 ZidbhyVR9QKbC0sXPUrJlIAZCuY6IbHJxDuvM6eduUgFAEfx2Y4OKktH7h1r16zo6zV3 sPgH6XTd+Am2HYFllL00r8zxrPoNHsEyKS1O4iQJjOKVuoL/nZ5kxuFr7NTHSmFTOb9l dyOg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of gustavo@embeddedor.com designates 192.185.63.14 as permitted sender) smtp.mailfrom=gustavo@embeddedor.com Received: from gateway20.websitewelcome.com (gateway20.websitewelcome.com. [192.185.63.14]) by mx.google.com with ESMTPS id t4si602313otj.108.2019.01.04.10.37.30 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 04 Jan 2019 10:37:30 -0800 (PST) Received-SPF: pass (google.com: domain of gustavo@embeddedor.com designates 192.185.63.14 as permitted sender) client-ip=192.185.63.14; Authentication-Results: mx.google.com; spf=pass (google.com: domain of gustavo@embeddedor.com designates 192.185.63.14 as permitted sender) smtp.mailfrom=gustavo@embeddedor.com Received: from cm10.websitewelcome.com (cm10.websitewelcome.com [100.42.49.4]) by gateway20.websitewelcome.com (Postfix) with ESMTP id E78CA400C4C02 for ; Fri, 4 Jan 2019 12:37:29 -0600 (CST) Received: from gator4166.hostgator.com ([108.167.133.22]) by cmsmtp with SMTP id fULZgrAUg2PzOfULZgWw74; 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-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 - kvack.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: 40 X-Source-Cap: Z3V6aWRpbmU7Z3V6aWRpbmU7Z2F0b3I0MTY2Lmhvc3RnYXRvci5jb20= X-Local-Domain: yes X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP 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 Acked-by: Michal Hocko Reviewed-by: William Kucharski --- 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;