From patchwork Fri Jul 31 17:12:53 2020 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: 11695275 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D4A9E138A for ; Fri, 31 Jul 2020 17:06:52 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id A14DF22CBE for ; Fri, 31 Jul 2020 17:06:52 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=kernel.org header.i=@kernel.org header.b="GAtSmDC2" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A14DF22CBE Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id E706E8D0050; Fri, 31 Jul 2020 13:06:51 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id E210F8D000B; Fri, 31 Jul 2020 13:06:51 -0400 (EDT) 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 CC2488D0050; Fri, 31 Jul 2020 13:06:51 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0206.hostedemail.com [216.40.44.206]) by kanga.kvack.org (Postfix) with ESMTP id AE4E38D000B for ; Fri, 31 Jul 2020 13:06:51 -0400 (EDT) Received: from smtpin02.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 71FBD1EE6 for ; Fri, 31 Jul 2020 17:06:51 +0000 (UTC) X-FDA: 77099000622.02.camp95_470638226f85 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin02.hostedemail.com (Postfix) with ESMTP id 4E69010097AA3 for ; Fri, 31 Jul 2020 17:06:51 +0000 (UTC) X-Spam-Summary: 1,0,0,a6549fabc8bff358,d41d8cd98f00b204,gustavoars@kernel.org,,RULES_HIT:41:355:379:800:960:968:973:988:989:1260:1277:1312:1313:1314:1345:1359:1437:1516:1518:1519:1534:1541:1593:1594:1595:1596:1711:1730:1747:1777:1792:2393:2559:2562:3138:3139:3140:3141:3142:3352:3865:3866:3867:3870:3872:3874:4321:4605:5007:6261:6271:6653:6757:8603:10004:10400:11026:11658:11914:12043:12296:12297:12438:12517:12519:12555:12895:13069:13153:13228:13311:13357:13439:13895:13972:14181:14394:14721:21080:21451:21627:21972:30054:30079,0,RBL:198.145.29.99:@kernel.org:.lbl8.mailshell.net-64.100.201.201 62.2.0.100;04yguu3oizeqcccppoborgdxmx7wuopue5dhz8qu9ppd8bge9d8zwkdet4xto9i.5a5npqpk5wgktktjqpmb1mqdr3epyufimcafwwn5yuw8uhpfg9rzbihzjeoi34m.q-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:23,LUA_SUMMARY:none X-HE-Tag: camp95_470638226f85 X-Filterd-Recvd-Size: 2827 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by imf47.hostedemail.com (Postfix) with ESMTP for ; Fri, 31 Jul 2020 17:06:50 +0000 (UTC) Received: from embeddedor (187-162-31-110.static.axtel.net [187.162.31.110]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 3255C2074B; Fri, 31 Jul 2020 17:06:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1596215210; bh=+VUSWtEU2obYc+ePK90t3RP8I0yTyfzAx4iIOrUuZ74=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=GAtSmDC2w2Ln8WEh56WQL62yee5akpVM/VnuiX7LQro9A/Sugff2KEgX4ZZhQfEjt AXtxfpZPx3m5Ximt3xarWPZ9mJHS/3AZd/8YL4lrbbEMgE983KIrz+1CvhlmfuC4/W lBnYRB/UOVWASkawWXD+zIapf/NNgOuCqNtAFETk= Date: Fri, 31 Jul 2020 12:12:53 -0500 From: "Gustavo A. R. Silva" To: linux-kernel@vger.kernel.org Cc: Johannes Weiner , Michal Hocko , Vladimir Davydov , Andrew Morton , cgroups@vger.kernel.org, linux-mm@kvack.org, "Gustavo A. R. Silva" Subject: [PATCH 1/2][next] mm: memcontrol: Use flex_array_size() helper in memcpy() Message-ID: References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.9.4 (2018-02-28) X-Rspamd-Queue-Id: 4E69010097AA3 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam03 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: Make use of the flex_array_size() helper to calculate the size of a flexible array member within an enclosing structure. This helper offers defense-in-depth against potential integer overflows, while at the same time makes it explicitly clear that we are dealing with a flexible array member. Also, remove unnecessary braces. Signed-off-by: Gustavo A. R. Silva Acked-by: Michal Hocko --- mm/memcontrol.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index e84c2b5596f2..bd7f972ceea4 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -4246,10 +4246,9 @@ static int __mem_cgroup_usage_register_event(struct mem_cgroup *memcg, new->size = size; /* Copy thresholds (if any) to new array */ - if (thresholds->primary) { - memcpy(new->entries, thresholds->primary->entries, (size - 1) * - sizeof(struct mem_cgroup_threshold)); - } + if (thresholds->primary) + memcpy(new->entries, thresholds->primary->entries, + flex_array_size(new, entries, size - 1)); /* Add new threshold */ new->entries[size - 1].eventfd = eventfd; From patchwork Fri Jul 31 17:13:14 2020 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: 11695277 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 30A2513B6 for ; Fri, 31 Jul 2020 17:07:14 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id E921722B40 for ; Fri, 31 Jul 2020 17:07:13 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=kernel.org header.i=@kernel.org header.b="rzpsvNgA" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E921722B40 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 34D188D0051; Fri, 31 Jul 2020 13:07:13 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 2D5758D000B; Fri, 31 Jul 2020 13:07:13 -0400 (EDT) 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 19EB08D0051; Fri, 31 Jul 2020 13:07:13 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0197.hostedemail.com [216.40.44.197]) by kanga.kvack.org (Postfix) with ESMTP id F162A8D000B for ; Fri, 31 Jul 2020 13:07:12 -0400 (EDT) Received: from smtpin08.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id B011E1EE6 for ; Fri, 31 Jul 2020 17:07:12 +0000 (UTC) X-FDA: 77099001504.08.shelf21_620783026f85 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin08.hostedemail.com (Postfix) with ESMTP id 8157E1819E798 for ; Fri, 31 Jul 2020 17:07:12 +0000 (UTC) X-Spam-Summary: 1,0,0,1dd046753158bb17,d41d8cd98f00b204,gustavoars@kernel.org,,RULES_HIT:41:355:379:800:960:973:988:989:1260:1277:1312:1313:1314:1345:1359:1437:1516:1518:1519:1534:1541:1593:1594:1595:1596:1711:1730:1747:1777:1792:2393:2559:2562:3138:3139:3140:3141:3142:3352:3865:3866:3867:3868:3871:4031:4321:5007:6261:6653:8603:10004:10128:10400:11026:11658:11914:12043:12296:12297:12438:12517:12519:12555:12895:12986:13069:13255:13311:13357:13439:13895:13972:14181:14394:14721:21080:21324:21433:21451:21627:21740:30054:30070,0,RBL:198.145.29.99:@kernel.org:.lbl8.mailshell.net-64.100.201.201 62.2.0.100;04ygmz3ik4wq9kc15caoyxnaj75ocop9cf19jsnjf61wjpdh8o6m3yz3o81kqid.u7tincqxo1uptmtyuctbj6x4hnfor76zdowds3e7j9qjsumfi5xu6irmimcgft4.q-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:22,LUA_SUMMARY:none X-HE-Tag: shelf21_620783026f85 X-Filterd-Recvd-Size: 2696 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by imf22.hostedemail.com (Postfix) with ESMTP for ; Fri, 31 Jul 2020 17:07:11 +0000 (UTC) Received: from embeddedor (187-162-31-110.static.axtel.net [187.162.31.110]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 71EF02245C; Fri, 31 Jul 2020 17:07:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1596215231; bh=I2tAEedcO9VAKEGuMTWWvln8PBMTfYVGS0os66wMkfY=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=rzpsvNgAslere3S82DjTwvt4tab3CYMOiNpoXLvhLB8lRZAaKKuwE2gd8Q2Unnar1 rW1L8jrg1lAmdNP/L2KZmVwkriP+iGB19V1d1to8r3JHHDA18dKAAHbVnGngJCOSpT dNeRXn56qc4/r9Sn4I182ZDXqOKV8N9MDsxH77W8= Date: Fri, 31 Jul 2020 12:13:14 -0500 From: "Gustavo A. R. Silva" To: linux-kernel@vger.kernel.org Cc: Johannes Weiner , Michal Hocko , Vladimir Davydov , Andrew Morton , cgroups@vger.kernel.org, linux-mm@kvack.org, "Gustavo A. R. Silva" Subject: [PATCH 2/2][next] mm: memcontrol: Use the preferred form for passing the size of a structure type Message-ID: <773e013ff2f07fe2a0b47153f14dea054c0c04f1.1596214831.git.gustavoars@kernel.org> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.9.4 (2018-02-28) X-Rspamd-Queue-Id: 8157E1819E798 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam04 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: Use the preferred form for passing the size of a structure type. The alternative form where the structure type is spelled out hurts readability and introduces an opportunity for a bug when the object type is changed but the corresponding object identifier to which the sizeof operator is applied is not. Signed-off-by: Gustavo A. R. Silva Acked-by: Michal Hocko --- mm/memcontrol.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index bd7f972ceea4..bd0f56785841 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -4255,7 +4255,7 @@ static int __mem_cgroup_usage_register_event(struct mem_cgroup *memcg, new->entries[size - 1].threshold = threshold; /* Sort thresholds. Registering of new threshold isn't time-critical */ - sort(new->entries, size, sizeof(struct mem_cgroup_threshold), + sort(new->entries, size, sizeof(*new->entries), compare_thresholds, NULL); /* Find current threshold */