From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) (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 EB11036D506 for ; Sat, 22 Nov 2025 04:59:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=216.40.44.15 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763787555; cv=none; b=ryCxJSk/vDOrWrUdNCcDAfembJtN8/Owj4vCGxCIUefkVs7ib2lCxysZY1ItxQlTlyvBwpuloSELoK8bo/2ZCYCdiIRvl0lSo2WGUUxLLR6xMdUz03QZoTve/KBWZIVTrYc9wkDPgE1rGrE3TuxoHoXAGm2OOWnY0TVPS4WOgYg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763787555; c=relaxed/simple; bh=7SzEtoD3Q2SJNKtinN/vaZ2Gu8lnZev6twaNqAR+dDk=; h=Message-ID:Subject:From:To:Cc:Date:In-Reply-To:References: Content-Type:MIME-Version; b=EBtFwjSo5GNFbEsIi+RiWM+BZb020kaNgINKestjraIz1ipUKBLA6c7hhPsI2sS67wyeUu9995JzbRBeYsjY5ayA9UohcvVsR0VZxdGQQxR///SpByRjRw35Eh02oeJYCmkKmyhRry8+n4OEju32xcjCvz4eaZS1RIpBaPVwG0s= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=perches.com; spf=pass smtp.mailfrom=perches.com; arc=none smtp.client-ip=216.40.44.15 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=perches.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=perches.com Received: from omf14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id A5B841607B4; Sat, 22 Nov 2025 04:51:33 +0000 (UTC) Received: from [HIDDEN] (Authenticated sender: joe@perches.com) by omf14.hostedemail.com (Postfix) with ESMTPA id C6BA130; Sat, 22 Nov 2025 04:51:20 +0000 (UTC) Message-ID: Subject: Re: [PATCH v5 3/4] checkpatch: Suggest kmalloc_obj family for sizeof allocations From: Joe Perches To: Kees Cook , Vlastimil Babka Cc: Andy Whitcroft , Dwaipayan Ray , Lukas Bulwahn , Randy Dunlap , Miguel Ojeda , Przemek Kitszel , "Gustavo A. R. Silva" , Linus Torvalds , Matthew Wilcox , Christoph Lameter , Marco Elver , Vegard Nossum , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Roman Gushchin , Harry Yoo , Bill Wendling , Justin Stitt , Jann Horn , Greg Kroah-Hartman , Sasha Levin , linux-mm@kvack.org, Nathan Chancellor , Peter Zijlstra , Nick Desaulniers , Jonathan Corbet , Jakub Kicinski , Yafang Shao , Tony Ambardar , Alexander Lobakin , Jan Hendrik Farr , Alexander Potapenko , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, linux-doc@vger.kernel.org, llvm@lists.linux.dev Date: Fri, 21 Nov 2025 20:51:19 -0800 In-Reply-To: <20251122014304.3417954-3-kees@kernel.org> References: <20251122014258.do.018-kees@kernel.org> <20251122014304.3417954-3-kees@kernel.org> Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.56.2 (3.56.2-2.fc42) Precedence: bulk X-Mailing-List: llvm@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Rspamd-Server: rspamout08 X-Rspamd-Queue-Id: C6BA130 X-Stat-Signature: y1e6wye49h55e9yihtz99bqq8wccka44 X-Session-Marker: 6A6F6540706572636865732E636F6D X-Session-ID: U2FsdGVkX1+Sr1JQ+437w6kkX38ORxtyNSi2FQhscRU= X-HE-Tag: 1763787080-150056 X-HE-Meta: U2FsdGVkX1+M0hbju6B4R0TNwI2E2iep66FeX6Que0tNsb+p8wMZ25hWgq/0Vg70NWm4auf2lfTvdpVhLVnfB1hBlf5wFSevwBSyC6wNObgGWMSG0iH9DzDUrYTNDo1mwj53A3UlpMDs/Z2ciaydLxUjMSIkc3wYa0yAFskiGr3Tq2v2v12WwDKOLviXDa9mthgm8+7cSFMGd5Waqo5a6wL6Mgv1Le1Mknb+2EzvfPlMA8grKHD5zYIb6mFlNhPbNWs5Ml0iBRYXn8u6C5HyT5LVyPiZlIL+x5IRaAH/Pm0+Q/OzWQjOGYlSjR+teOoC On Fri, 2025-11-21 at 17:42 -0800, Kees Cook wrote: > To support shifting away from sized allocation towards typed > allocations, suggest the kmalloc_obj family of macros when a sizeof() is > present in the argument lists. [] > diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl [] > @@ -7258,17 +7258,42 @@ sub process { > "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr); > } > =20 > -# check for (kv|k)[mz]alloc with multiplies that could be kmalloc_array/= kvmalloc_array/kvcalloc/kcalloc > +# check for (kv|k)[mz]alloc that could be kmalloc_obj/kvmalloc_obj/kzall= oc_obj/kvzalloc_obj There are _way_ too many of these existing uses to suggest this change in existing files so please add '&& !$file' to these tests > + if ($perl_version_ok && > + defined $stat && > + $stat =3D~ /^\+\s*($Lval)\s*\=3D\s*(?:$balanced_parens)?\s*((?:kv|= k)[mz]alloc)\s*\(\s*($FuncArg)\s*,/) { > + my $oldfunc =3D $3; > + my $a1 =3D $4; > + my $newfunc =3D "kmalloc_obj"; > + $newfunc =3D "kvmalloc_obj" if ($oldfunc eq "kvmalloc"); > + $newfunc =3D "kvzalloc_obj" if ($oldfunc eq "kvzalloc"); > + $newfunc =3D "kzalloc_obj" if ($oldfunc eq "kzalloc"); > + > + if ($a1 =3D~ s/^sizeof\s*\S\(?([^\)]*)\)?$/$1/) { > + my $cnt =3D statement_rawlines($stat); > + my $herectx =3D get_stat_here($linenr, $cnt, $here); > + > + if (WARN("ALLOC_WITH_SIZEOF", > + "Prefer $newfunc over $oldfunc with sizeof\n" . $herectx) && > + $cnt =3D=3D 1 && > + $fix) { > + $fixed[$fixlinenr] =3D~ s/\b($Lval)\s*\=3D\s*(?:$balanced_parens)?\= s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*,/$1 =3D $newfunc($a1,/; > + } > + } > + } > + > + > +# check for (kv|k)[mz]alloc with multiplies that could be kmalloc_objs/k= vmalloc_objs/kzalloc_objs/kvzalloc_objs > if ($perl_version_ok && > defined $stat && > $stat =3D~ /^\+\s*($Lval)\s*\=3D\s*(?:$balanced_parens)?\s*((?:kv|= k)[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) { > my $oldfunc =3D $3; > my $a1 =3D $4; > my $a2 =3D $10; > - my $newfunc =3D "kmalloc_array"; > - $newfunc =3D "kvmalloc_array" if ($oldfunc eq "kvmalloc"); > - $newfunc =3D "kvcalloc" if ($oldfunc eq "kvzalloc"); > - $newfunc =3D "kcalloc" if ($oldfunc eq "kzalloc"); > + my $newfunc =3D "kmalloc_objs"; > + $newfunc =3D "kvmalloc_objs" if ($oldfunc eq "kvmalloc"); > + $newfunc =3D "kvzalloc_objs" if ($oldfunc eq "kvzalloc"); > + $newfunc =3D "kzalloc_objs" if ($oldfunc eq "kzalloc"); > my $r1 =3D $a1; > my $r2 =3D $a2; > if ($a1 =3D~ /^sizeof\s*\S/) { > @@ -7284,7 +7309,9 @@ sub process { > "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) && > $cnt =3D=3D 1 && > $fix) { > - $fixed[$fixlinenr] =3D~ s/\b($Lval)\s*\=3D\s*(?:$balanced_parens)?\= s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' =3D ' . "$= newfunc(" . trim($r1) . ', ' . trim($r2)/e; > + my $sized =3D trim($r2); > + $sized =3D~ s/^sizeof\s*\S\(?([^\)]*)\)?$/$1/; > + $fixed[$fixlinenr] =3D~ s/\b($Lval)\s*\=3D\s*(?:$balanced_parens)?\= s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' =3D ' . "$= newfunc(" . $sized . ', ' . trim($r1)/e; > } > } > }