Received: by 2002:a05:7412:7c14:b0:fa:6e18:a558 with SMTP id ii20csp177827rdb; Sun, 21 Jan 2024 23:59:41 -0800 (PST) X-Google-Smtp-Source: AGHT+IG8QYO8Rz5gUq363R6M2niNLgJ9rFRk+90+bNpIRSXAK3ogLWUBcqnEU8d1s/BXGOYwpqZt X-Received: by 2002:a17:906:da8e:b0:a2e:7b28:1f03 with SMTP id xh14-20020a170906da8e00b00a2e7b281f03mr2136763ejb.90.1705910381705; Sun, 21 Jan 2024 23:59:41 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705910381; cv=pass; d=google.com; s=arc-20160816; b=hBMUXQw1eNvQ02d0Tlx7uX5mXUnZwjzN9uB+4YWK3fV9Ur7/p+OfWpgDh8LxJwVOLZ YOfmlkrpBhDY0OmgG/OaQesBYhOFE2nwZI/ZejTN1+VpVoYcU6Sn4rGBYI6Y9bLn6xul XypeUdPYeeywfMcNbPIKuqTLTmWCrfR5UtuZGSFFxRgpTcRD8Go1zFfyf0qWC10keh4X 3d4/ANyBAUJEo+Uc88EXdWPufN/6XMO8BQl41LiNCe0wYyV/Hib9le+WfdzP51HI9L6R W0VSDik7NrTA2YWy15me4PSnDkFlXC0q00JYj0Yd6vKRqSsi+fqVzWaTI9+2dRTSPlUz 2PEQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=2PpIgzy2wBvcMhjEloswpHlI6yDHrtSrQ2UV0sJfeEs=; fh=SZ/d08/Y0RTNnTsT3WkCnlGcuHDKpjNCZjBejaeHnys=; b=Ca2VeTD5WO7evHR+7O5s7ri9EQevK8T0JE7nvwJNbCj7FbJLtYP/nDGDC2OK3194VG A12KVTkqV+/6cKk8YA2goo0TgykutZN00mYc0OT5HH9iT6cgdAOO/5pPQow35cnyZph6 kZPYxnsqjWPP7OIgS0CA4vk6I8KvVVF2SFlvBNZ4BJ1IA/ShOQqk1gnUBpBnhQA3esBH RFHR9L6apd+JglzpnQtSxUMvpe2zEekXHdFM0CVovJjllA1gVBuV/boH4YHQ1L8Wu0LA S7/Nr6VNn73nAS6hTCaSh3s/AQAPSleipt9HMJicAlj/uItzbPriO9xs0rYQtNI6YNqN dgOg== ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=ghiti.fr); spf=pass (google.com: domain of linux-kernel+bounces-32482-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-32482-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id u17-20020a170906069100b00a2e9225ec87si6074587ejb.86.2024.01.21.23.59.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Jan 2024 23:59:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-32482-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=ghiti.fr); spf=pass (google.com: domain of linux-kernel+bounces-32482-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-32482-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 5881B1F247B0 for ; Mon, 22 Jan 2024 07:59:41 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6A2441B268; Mon, 22 Jan 2024 07:59:14 +0000 (UTC) Received: from relay8-d.mail.gandi.net (relay8-d.mail.gandi.net [217.70.183.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AECF53611B; Mon, 22 Jan 2024 07:59:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705910353; cv=none; b=AFBTgWnsuyKbTnDlgP2B5Ukn7eJjPoucF23/GNm6pu7GSnpGotr3tkmNY61dEQLXZrRfFTzHn4Fea7XOnYLedSODsKZssMcqWvWKwIb5bYoMT6Jl1gef8h/hvsgZPgWm0EkkErYKhuPTijAmrSNTYKugf8nzxgYQNosx0LvMUhc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705910353; c=relaxed/simple; bh=pGlZ3QI3Lga899NkB9oFLprRpavbHJNhgA8T4iPQJlM=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=MCbGGX3d3mL7jYdEeuVGHm/yJLOrjsIXTG78rwgt6J6c1fbfmp8jy/CbXrxKpdlNudH5iSu7H3Ac/R4sd0lvSMFj/IepAnVPzvym5DamzTYFTY5o5YDbcYQ6lg01HK8FP0Nm3ZpFjdE5JWJTiFPIaFdSBKPWjuC51MXNEyBUf2c= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ghiti.fr; spf=pass smtp.mailfrom=ghiti.fr; arc=none smtp.client-ip=217.70.183.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ghiti.fr Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ghiti.fr Received: by mail.gandi.net (Postfix) with ESMTPSA id A80011BF206; Mon, 22 Jan 2024 07:59:00 +0000 (UTC) Message-ID: <048fc58d-abbb-4696-9ed0-f5a0fadce740@ghiti.fr> Date: Mon, 22 Jan 2024 08:58:58 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] riscv: Use kcalloc() instead of kzalloc() Content-Language: en-US To: Erick Archer , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Andrew Jones , Evan Green , =?UTF-8?B?Q2zDqW1lbnQgTMOpZ2Vy?= , Jisheng Zhang , Charlie Jenkins , "Gustavo A. R. Silva" Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org References: <20240120135400.4710-1-erick.archer@gmx.com> From: Alexandre Ghiti In-Reply-To: <20240120135400.4710-1-erick.archer@gmx.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-GND-Sasl: alex@ghiti.fr Hi Erick, On 20/01/2024 14:54, Erick Archer wrote: > As noted in the "Deprecated Interfaces, Language Features, Attributes, > and Conventions" documentation [1], size calculations (especially > multiplication) should not be performed in memory allocator (or similar) > function arguments due to the risk of them overflowing. This could lead > to values wrapping around and a smaller allocation being made than the > caller was expecting. Using those allocations could lead to linear > overflows of heap memory and other misbehaviors. > > So, use the purpose specific kcalloc() function instead of the argument > count * size in the kzalloc() function. > > Also, it is preferred to use sizeof(*pointer) instead of sizeof(type) > due to the type of the variable can change and one needs not change the > former (unlike the latter). > > Link: https://www.kernel.org/doc/html/next/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments [1] > Link: https://github.com/KSPP/linux/issues/162 > Signed-off-by: Erick Archer > --- > arch/riscv/kernel/cpufeature.c | 3 +-- > 1 file changed, 1 insertion(+), 2 deletions(-) > > diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c > index 89920f84d0a3..549a76e34c4e 100644 > --- a/arch/riscv/kernel/cpufeature.c > +++ b/arch/riscv/kernel/cpufeature.c > @@ -901,8 +901,7 @@ static int check_unaligned_access_all_cpus(void) > { > unsigned int cpu; > unsigned int cpu_count = num_possible_cpus(); > - struct page **bufs = kzalloc(cpu_count * sizeof(struct page *), > - GFP_KERNEL); > + struct page **bufs = kcalloc(cpu_count, sizeof(*bufs), GFP_KERNEL); > > if (!bufs) { > pr_warn("Allocation failure, not measuring misaligned performance\n"); > -- > 2.25.1 > > > _______________________________________________ > linux-riscv mailing list > linux-riscv@lists.infradead.org > http://lists.infradead.org/mailman/listinfo/linux-riscv You can add: Reviewed-by: Alexandre Ghiti Thanks, Alex