From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.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 0B7D61DDC37 for ; Sun, 21 Dec 2025 14:07:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766326040; cv=none; b=l4EupLDIL024RplT+s0d3OH0qmXhuN/GGbI5pQfD+WMbJft8HH+Vs9kV1YilFIStIXodVSbCkYtTUv9rm4GNH4ScA14WhkhW6+8fVO0KVdW7FPa2AlYEsLYinLpMZIMitZ8aKnNTHLCiUQmV9kovAyQNdjIy5NP8NTFFJPe8tTg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766326040; c=relaxed/simple; bh=jlEdX7ss9VtYw9IGwQ7RQSOLbQ4LFNrQ1j0ktaihUNY=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=OUhfypv4ksQrbC/AXZV49Qr6ukZHgPu/9WqxOOpQrIXmw3IuE2xGwocfB7JQKCD489ERDtH96vOKZzJx3GDAP4/QPY/JKHVsUU8FQpiC6jimXaFyDa7FVFemogH934YfrtezfntByU5Le0B8yNNmFZ5l7gVjwwLrhNtBgxg+Uss= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=gHqp7gjk; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="gHqp7gjk" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A4380C4CEFB; Sun, 21 Dec 2025 14:07:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1766326039; bh=jlEdX7ss9VtYw9IGwQ7RQSOLbQ4LFNrQ1j0ktaihUNY=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=gHqp7gjkWAA3IRhr4XOpB+CAoGi+o5mE50OASMRNr+zINsr1xyhBmPj5knmVXnHNx AkIhp55AzdFpzMR0XnIP/yTJA110Sufeas1dwO4fgIRJ9qX0e+Ahd1FPDyXmgHXlGs SbK1SwxU4M+fPxM2DQZOaA3BbhIeebneE0NeqU2so9vB6yZdsRhSMkDdo1kIccw3jA vU1Oy0jNy1LbrsAbEM3qvoO1kOD/e+tf9GD4xg222UUIQQKvFWYAT2ya+hl2y9O547 ZljF7U8TJpM8hyhdhfw1DZeLF5cB+FO4hS3uyXp7F8L+2C8uuhuIDnjO7iyg7vMmNd KJx7q3eimBPxg== Date: Sun, 21 Dec 2025 15:07:14 +0100 From: Alejandro Colomar To: Kees Cook , Andrew Morton Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Christopher Bazley , Rasmus Villemoes , Marco Elver , Michal Hocko , Linus Torvalds , Al Viro , Alexander Potapenko , Dmitry Vyukov , Jann Horn , "Maciej W. Rozycki" Subject: Re: [PATCH v5 4/4] mm: Use ARRAY_END() instead of open-coding it Message-ID: References: <202512101736.DE24BE89@keescook> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="aunnpwbmonbqjh5u" Content-Disposition: inline In-Reply-To: <202512101736.DE24BE89@keescook> --aunnpwbmonbqjh5u Content-Type: text/plain; protected-headers=v1; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable From: Alejandro Colomar To: Kees Cook , Andrew Morton Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Christopher Bazley , Rasmus Villemoes , Marco Elver , Michal Hocko , Linus Torvalds , Al Viro , Alexander Potapenko , Dmitry Vyukov , Jann Horn , "Maciej W. Rozycki" Subject: Re: [PATCH v5 4/4] mm: Use ARRAY_END() instead of open-coding it Message-ID: References: <202512101736.DE24BE89@keescook> MIME-Version: 1.0 In-Reply-To: <202512101736.DE24BE89@keescook> Hi Kees, Andrew, On Wed, Dec 10, 2025 at 05:37:13PM -0800, Kees Cook wrote: > > > Are there other open-coded instances that could be replaced? This see= ms like a great task for a coccinelle script. > >=20 > > There are many, but I wanted to keep them out of this initial patch set, > > to make it easy to apply. When this one is applied, I could work on a > > second round that replaces more of them with coccinelle. This is just > > for showing that this is beneficial, and to make sure that you ask for > > more. :) > >=20 > > Also, it's easier if there are few maintainers that would block an > > initial patch set. If restrict the patch set to a few files, I don't > > have to deal with many of them. Once I get used to this, I'll deal with > > all of them. >=20 > Sounds good! Now that the first patch set has been merged, I'm working on a second round. I've written a semantic patch: $ cat src/spatch/array_end.sp=20 @@ expression a; @@ - a + ARRAY_SIZE(a) + ARRAY_END(a) @@ expression a; @@ - ARRAY_SIZE(a) + a + ARRAY_END(a) @@ expression a; @@ - &a[0] + ARRAY_SIZE(a) + ARRAY_END(a) @@ expression a; @@ - ARRAY_SIZE(a) + &a[0] + ARRAY_END(a) @@ expression a; @@ - &a[ARRAY_SIZE(a)] + ARRAY_END(a) Apart from the cases covered by this semantic patch, I've found one case which needs manual intervention (I don't know if a semantic patch can handle this case): diff --git i/fs/proc/base.c w/fs/proc/base.c index c5114e9a0323..e99c8cad49c3 100644 --- i/fs/proc/base.c +++ w/fs/proc/base.c @@ -2876,7 +2876,7 @@ static struct dentry *proc_##LSM##_attr_dir_lookup(s= truct inode *dir, \ { \ return proc_pident_lookup(dir, dentry, \ LSM##_attr_dir_stuff, \ - LSM##_attr_dir_stuff + ARRAY_SIZE(LSM##_= attr_dir_stuff)); \ + ARRAY_END(LSM##_attr_dir_stuff)); \ } \ \ static const struct inode_operations proc_##LSM##_attr_dir_inode_ops =3D = { \ Anyway, I made sure there's only one of these. There are no similar cases in the kernel. Here's a summary of what the semantic patch finds: arch/arm/kernel/hibernate.c | 2 +- arch/arm/kernel/reboot.c | 2 +- arch/arm/mach-omap1/clock_data.c | 2 +- arch/arm64/kvm/sys_regs.c | 2 +- arch/mips/fw/sni/sniprom.c | 2 +- arch/mips/include/asm/sgiarcs.h | 2 +- arch/riscv/purgatory/purgatory.c | 2 +- arch/s390/purgatory/purgatory.c | 2 +- arch/x86/kernel/cpu/hypervisor.c | 2 +- arch/x86/purgatory/purgatory.c | 2 +- block/bio.c | 2 +- crypto/adiantum.c | 2 +- drivers/base/regmap/regmap-spi-avmm.c | 2 +- drivers/cpufreq/sa1110-cpufreq.c | 2 +- drivers/firmware/efi/libstub/vsprintf.c | 2 +- drivers/gpu/drm/i915/selftests/i915_vma.c | 2 +- drivers/gpu/drm/xe/xe_guc_ads.c | 2 +- drivers/md/bcache/btree.c | 6 +++--- drivers/md/bcache/util.h | 2 +- drivers/md/dm-raid.c | 6 +++--- drivers/mtd/devices/mtd_dataflash.c | 2 +- drivers/net/ethernet/marvell/mvneta.c | 2 +- drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c | 2 +- drivers/net/ethernet/sfc/falcon/nic.c | 8 ++++---- drivers/net/ethernet/sfc/nic.c | 8 ++++---- drivers/net/ethernet/sfc/siena/nic.c | 8 ++++---- drivers/net/wireless/intel/iwlwifi/mei/net.c | 4 ++-- drivers/net/wireless/intel/iwlwifi/mvm/mac80211.c | 3 +-- drivers/tty/serial/msm_serial.c | 2 +- fs/erofs/zdata.c | 4 ++-- fs/proc/base.c | 6 +++--- fs/proc/namespaces.c | 2 +- kernel/bpf/log.c | 2 +- lib/crypto/tests/hash-test-template.h | 4 ++-- mm/kmemleak.c | 2 +- mm/memcontrol-v1.c | 4 ++-- net/ipv4/tcp_input.c | 4 ++-- tools/bpf/bpftool/tracelog.c | 2 +- tools/testing/selftests/bpf/prog_tests/select_reuseport.c | 4 ++-- tools/testing/selftests/bpf/prog_tests/sk_lookup.c | 8 ++++---- tools/testing/selftests/bpf/prog_tests/sockmap_listen.c | 8 ++++---- tools/testing/selftests/bpf/prog_tests/sockmap_redir.c | 8 ++++---- 42 files changed, 72 insertions(+), 73 deletions(-) I applied the semantic patch as $ time find * -type f \ | grep '\.[ch]$' \ | xargs grep -l ARRAY_SIZE \ | xargs grep -L '^#define ARRAY_END' \ | xargs grep -l \ -e '+ *ARRAY_SIZE' \ -e 'ARRAY_SIZE(.*) *+' \ -e '&.*ARRAY_SIZE' \ | xargs spatch --sp-file ~/src/spatch/array_end.sp --in-place; How should I proceed with this? Should I send separate patches for each subtree? Or should I just send the semantic patch and let maintainers run it? Or should I senda big patch? Also, I don't know if all of those files are able to use include/linux/array_size.h. Do tools/ need a separate definition? Have a lovely day! Alex --=20 --aunnpwbmonbqjh5u Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEES7Jt9u9GbmlWADAi64mZXMKQwqkFAmlH/xIACgkQ64mZXMKQ wqmWvQ//ZWWq8so/sNRMW71qo2vyQxrd4nLS7/rjS7CXtXQw5FTHU/p3XvJ6S36w 050CAxDfXwMXJX8KEsmSCacpjSJTBlLjSuLpONrrV6KxvQDvW9KLttmADaPrX6Jc A/qwu7qbvBUgaZPrJxmSw5EhfY+EYwDa10nbRcvpHR2mO8F9COjF8hhCzEIUJq8l khcOpjbqNQbp8zGjGH+oT8VhmlQqWMn2wobsG5dholavIzciybJ+edCKCAmm1uj7 8NZOlodce5uH34aPcA+eVpFMXjpIkG7F4Z/pjsHU9MRwVhXkGXF27D2f1Xm/doLK MJ+ueTyemJHlqzFjR6rTsaV14S2q7KW6PtxNkNJpozITeaNOAocRHrsx6Uk5TGwa EcmT24OEijm4TMqpKJg4ReIwRgtnfIwmG25eE/lj/ljyrosrMTDFGCP7y26RJfnS wHvigwb45laEKPeviBI353/XOogjUG6++Y6QSEKyW0EQFXaFQtAHP8KkHISxn/We VyDNgoxJOY4Eq4DyC51xXOFHwLBGu42z0JDFLFt96NMXz9HkjR/3qvhu6BPn2BAZ pxdodRVLaYV9FKo6ELXWrfc8LSSEHgrI5VlrLQbRtCz9jWdQssr4RvDwPaFN/WRg CQOKiyrQau6SJZs2eliW5Zw3ctglGNAdMPgyIEAZkv53wW6CAzQ= =+7F0 -----END PGP SIGNATURE----- --aunnpwbmonbqjh5u--