Subject: [PATCH v3 0/7] sysctl: Remove sentinel elements from arch

From: Joel Granados <[email protected]>

What?
These commits remove the sentinel element (last empty element) from the
sysctl arrays of all the files under the "arch/" directory that use a
sysctl array for registration. The merging of the preparation patches
(in https://lore.kernel.org/all/ZO5Yx5JFogGi%[email protected]/)
to mainline allows us to just remove sentinel elements without changing
behavior. This is now safe because the sysctl registration code
(register_sysctl() and friends) use the array size in addition to
checking for a sentinel ([1] for more info).

These commits are part of a bigger set (bigger patchset here
https://github.com/Joelgranados/linux/tree/tag/sysctl_remove_empty_elem_V4)
that remove the ctl_table sentinel. The idea is to make the review
process easier by chunking the 52 commits into manageable pieces. By
sending out one chunk at a time, they can be reviewed separately without
noise from parallel sets. After the "arch/" commits in this set are
reviewed, I will continue with drivers/*, fs/*, kernel/*, net/* and
miscellaneous. The final set will remove the unneeded check for
->procname == NULL.

Why?
By removing the sysctl sentinel elements we avoid kernel bloat as
ctl_table arrays get moved out of kernel/sysctl.c into their own
respective subsystems. This move was started long ago to avoid merge
conflicts; the sentinel removal bit came after Mathew Wilcox suggested
it to avoid bloating the kernel by one element as arrays moved out. This
patchset will reduce the overall build time size of the kernel and run
time memory bloat by about ~64 bytes per declared ctl_table array. I
have consolidated some links that shed light on the history of this
effort [2].

V2:
* Added clarification both in the commit messages and the coverletter as
to why this patch is safe to apply.
* Added {Acked,Reviewed,Tested}-by from list
* Link to v1: https://lore.kernel.org/r/20230906-jag-sysctl_remove_empty_elem_arch-v1-0-3935d4854248@samsung.com

V3:
* Removed the ia64 patch to avoid conflicts with the ia64 removal
* Rebased onto v6.6-rc4
* Kept/added the trailing comma for the ctl_table arrays. This was a comment
that we received "drivers/*" patch set.
* Link to v2: https://lore.kernel.org/r/20230913-jag-sysctl_remove_empty_elem_arch-v2-0-d1bd13a29bae@samsung.com

Testing:
* Ran sysctl selftests (./tools/testing/selftests/sysctl/sysctl.sh)
* Ran this through 0-day with no errors or warnings

Size saving after removing all sentinels:
A consequence of eventually removing all the sentinels (64 bytes per
sentinel) is the bytes we save. These are *not* numbers that we will
get after this patch set; these are the numbers that we will get after
removing all the sentinels. I included them here because they are
relevant and to get an idea of just how much memory we are talking
about.
* bloat-o-meter:
- The "yesall" configuration results save 9158 bytes (bloat-o-meter output here
https://lore.kernel.org/all/[email protected]/)
- The "tiny" config + CONFIG_SYSCTL save 1215 bytes (bloat-o-meter output here
https://lore.kernel.org/all/[email protected]/)
* memory usage:
we save some bytes in main memory as well. In my testing kernel
I measured a difference of 7296 bytes. I include the way to
measure in [3]

Size saving after this patchset:
Here I give the values that I measured for the architecture that I'm
running (x86_64). This can give an approximation of how many bytes are
saved for each arch. I won't publish for all the archs because I don't
have access to all of them.
* bloat-o-meter
- The "yesall" config saves 192 bytes (bloat-o-meter output [4])
- The "tiny" config saves 64 bytes (bloat-o-meter output [5])
* memory usage:
In this case there were no bytes saved. To measure it comment the
printk in `new_dir` and uncomment the if conditional in `new_links`
[3].

Comments/feedback greatly appreciated

Best
Joel

[1]
https://lore.kernel.org/all/[email protected]/

[2]
Links Related to the ctl_table sentinel removal:
* Good summary from Luis sent with the "pull request" for the
preparation patches.
https://lore.kernel.org/all/ZO5Yx5JFogGi%[email protected]/
* Another very good summary from Luis.
https://lore.kernel.org/all/[email protected]/
* This is a patch set that replaces register_sysctl_table with register_sysctl
https://lore.kernel.org/all/[email protected]/
* Patch set to deprecate register_sysctl_paths()
https://lore.kernel.org/all/[email protected]/
* Here there is an explicit expectation for the removal of the sentinel element.
https://lore.kernel.org/all/[email protected]
* The "ARRAY_SIZE" approach was mentioned (proposed?) in this thread
https://lore.kernel.org/all/[email protected]

[3]
To measure the in memory savings apply this on top of this patchset.

"
diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
index c88854df0b62..e0073a627bac 100644
--- a/fs/proc/proc_sysctl.c
+++ b/fs/proc/proc_sysctl.c
@@ -976,6 +976,8 @@ static struct ctl_dir *new_dir(struct ctl_table_set *set,
table[0].procname = new_name;
table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO;
init_header(&new->header, set->dir.header.root, set, node, table, 1);
+ // Counts additional sentinel used for each new dir.
+ printk("%ld sysctl saved mem kzalloc \n", sizeof(struct ctl_table));

return new;
}
@@ -1199,6 +1201,9 @@ static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table_
link_name += len;
link++;
}
+ // Counts additional sentinel used for each new registration
+ //if ((head->ctl_table + head->ctl_table_size)->procname)
+ printk("%ld sysctl saved mem kzalloc \n", sizeof(struct ctl_table));
init_header(links, dir->header.root, dir->header.set, node, link_table,
head->ctl_table_size);
links->nreg = nr_entries;
"
and then run the following bash script in the kernel:

accum=0
for n in $(dmesg | grep kzalloc | awk '{print $3}') ; do
echo $n
accum=$(calc "$accum + $n")
done
echo $accum

[4]
add/remove: 0/0 grow/shrink: 0/3 up/down: 0/-192 (-192)
Function old new delta
sld_sysctls 128 64 -64
itmt_kern_table 128 64 -64
abi_table2 128 64 -64
Total: Before=429173594, After=429173402, chg -0.00%

[5]
add/remove: 0/0 grow/shrink: 1/0 up/down: 64/0 (64)
Function old new delta
sld_sysctls 64 128 +64
Total: Before=1886119, After=1886183, chg +0.00%

Signed-off-by: Joel Granados <[email protected]>

---

---
Joel Granados (7):
S390: Remove now superfluous sentinel elem from ctl_table arrays
arm: Remove now superfluous sentinel elem from ctl_table arrays
arch/x86: Remove now superfluous sentinel elem from ctl_table arrays
x86/vdso: Remove now superfluous sentinel element from ctl_table array
riscv: Remove now superfluous sentinel element from ctl_table array
powerpc: Remove now superfluous sentinel element from ctl_table arrays
c-sky: Remove now superfluous sentinel element from ctl_talbe array

arch/arm/kernel/isa.c | 4 ++--
arch/arm64/kernel/armv8_deprecated.c | 8 +++-----
arch/arm64/kernel/fpsimd.c | 2 --
arch/arm64/kernel/process.c | 1 -
arch/csky/abiv1/alignment.c | 1 -
arch/powerpc/kernel/idle.c | 1 -
arch/powerpc/platforms/pseries/mobility.c | 1 -
arch/riscv/kernel/vector.c | 1 -
arch/s390/appldata/appldata_base.c | 4 +---
arch/s390/kernel/debug.c | 1 -
arch/s390/kernel/topology.c | 1 -
arch/s390/mm/cmm.c | 1 -
arch/s390/mm/pgalloc.c | 1 -
arch/x86/entry/vdso/vdso32-setup.c | 1 -
arch/x86/kernel/cpu/intel.c | 1 -
arch/x86/kernel/itmt.c | 1 -
drivers/perf/arm_pmuv3.c | 1 -
17 files changed, 6 insertions(+), 25 deletions(-)
---
base-commit: 8a749fd1a8720d4619c91c8b6e7528c0a355c0aa
change-id: 20230904-jag-sysctl_remove_empty_elem_arch-81db0a6e6cc4

Best regards,
--
Joel Granados <[email protected]>


Subject: [PATCH v3 6/7] powerpc: Remove now superfluous sentinel element from ctl_table arrays

From: Joel Granados <[email protected]>

This commit comes at the tail end of a greater effort to remove the
empty elements at the end of the ctl_table arrays (sentinels) which
will reduce the overall build time size of the kernel and run time
memory bloat by ~64 bytes per sentinel (further information Link :
https://lore.kernel.org/all/ZO5Yx5JFogGi%[email protected]/)

Remove sentinel from powersave_nap_ctl_table and nmi_wd_lpm_factor_ctl_table.
This removal is safe because register_sysctl implicitly uses ARRAY_SIZE()
in addition to checking for the sentinel.

Signed-off-by: Joel Granados <[email protected]>
---
arch/powerpc/kernel/idle.c | 1 -
arch/powerpc/platforms/pseries/mobility.c | 1 -
2 files changed, 2 deletions(-)

diff --git a/arch/powerpc/kernel/idle.c b/arch/powerpc/kernel/idle.c
index b1c0418b25c8..30b56c67fa61 100644
--- a/arch/powerpc/kernel/idle.c
+++ b/arch/powerpc/kernel/idle.c
@@ -105,7 +105,6 @@ static struct ctl_table powersave_nap_ctl_table[] = {
.mode = 0644,
.proc_handler = proc_dointvec,
},
- {}
};

static int __init
diff --git a/arch/powerpc/platforms/pseries/mobility.c b/arch/powerpc/platforms/pseries/mobility.c
index 0161226d8fec..1798f0f14d58 100644
--- a/arch/powerpc/platforms/pseries/mobility.c
+++ b/arch/powerpc/platforms/pseries/mobility.c
@@ -61,7 +61,6 @@ static struct ctl_table nmi_wd_lpm_factor_ctl_table[] = {
.mode = 0644,
.proc_handler = proc_douintvec_minmax,
},
- {}
};

static int __init register_nmi_wd_lpm_factor_sysctl(void)

--
2.30.2

Subject: [PATCH v3 4/7] x86/vdso: Remove now superfluous sentinel element from ctl_table array

From: Joel Granados <[email protected]>

This commit comes at the tail end of a greater effort to remove the
empty elements at the end of the ctl_table arrays (sentinels) which
will reduce the overall build time size of the kernel and run time
memory bloat by ~64 bytes per sentinel (further information Link :
https://lore.kernel.org/all/ZO5Yx5JFogGi%[email protected]/)

Remove sentinel element from abi_table2. This removal is safe because
register_sysctl implicitly uses ARRAY_SIZE() in addition to checking for
the sentinel.

Signed-off-by: Joel Granados <[email protected]>
---
arch/x86/entry/vdso/vdso32-setup.c | 1 -
1 file changed, 1 deletion(-)

diff --git a/arch/x86/entry/vdso/vdso32-setup.c b/arch/x86/entry/vdso/vdso32-setup.c
index f3b3cacbcbb0..76e4e74f35b5 100644
--- a/arch/x86/entry/vdso/vdso32-setup.c
+++ b/arch/x86/entry/vdso/vdso32-setup.c
@@ -67,7 +67,6 @@ static struct ctl_table abi_table2[] = {
.extra1 = SYSCTL_ZERO,
.extra2 = SYSCTL_ONE,
},
- {}
};

static __init int ia32_binfmt_init(void)

--
2.30.2

Subject: [PATCH v3 7/7] c-sky: Remove now superfluous sentinel element from ctl_talbe array

From: Joel Granados <[email protected]>

This commit comes at the tail end of a greater effort to remove the
empty elements at the end of the ctl_table arrays (sentinels) which
will reduce the overall build time size of the kernel and run time
memory bloat by ~64 bytes per sentinel (further information Link :
https://lore.kernel.org/all/ZO5Yx5JFogGi%[email protected]/)

Remove sentinel from alignment_tbl ctl_table array. This removal is safe
because register_sysctl_init implicitly uses ARRAY_SIZE() in addition to
checking for the sentinel.

Acked-by: Guo Ren <[email protected]>
Signed-off-by: Joel Granados <[email protected]>
---
arch/csky/abiv1/alignment.c | 1 -
1 file changed, 1 deletion(-)

diff --git a/arch/csky/abiv1/alignment.c b/arch/csky/abiv1/alignment.c
index b60259daed1b..e5b8b4b2109a 100644
--- a/arch/csky/abiv1/alignment.c
+++ b/arch/csky/abiv1/alignment.c
@@ -329,7 +329,6 @@ static struct ctl_table alignment_tbl[5] = {
.mode = 0666,
.proc_handler = &proc_dointvec
},
- {}
};

static int __init csky_alignment_init(void)

--
2.30.2

2023-10-10 22:23:29

by Luis Chamberlain

[permalink] [raw]
Subject: Re: [PATCH v3 0/7] sysctl: Remove sentinel elements from arch

On Mon, Oct 02, 2023 at 01:30:35PM +0200, Joel Granados via B4 Relay wrote:
> V3:
> * Removed the ia64 patch to avoid conflicts with the ia64 removal
> * Rebased onto v6.6-rc4
> * Kept/added the trailing comma for the ctl_table arrays. This was a comment
> that we received "drivers/*" patch set.
> * Link to v2: https://lore.kernel.org/r/20230913-jag-sysctl_remove_empty_elem_arch-v2-0-d1bd13a29bae@samsung.com

Thanks! I replaced the v2 with this v3 on sysctl-next.

Luis

2023-10-11 08:16:46

by Joel Granados

[permalink] [raw]
Subject: Re: [PATCH v3 0/7] sysctl: Remove sentinel elements from arch

On Tue, Oct 10, 2023 at 03:22:34PM -0700, Luis Chamberlain wrote:
> On Mon, Oct 02, 2023 at 01:30:35PM +0200, Joel Granados via B4 Relay wrote:
> > V3:
> > * Removed the ia64 patch to avoid conflicts with the ia64 removal
> > * Rebased onto v6.6-rc4
> > * Kept/added the trailing comma for the ctl_table arrays. This was a comment
> > that we received "drivers/*" patch set.
> > * Link to v2: https://lore.kernel.org/r/20230913-jag-sysctl_remove_empty_elem_arch-v2-0-d1bd13a29bae@samsung.com
>
> Thanks! I replaced the v2 with this v3 on sysctl-next.
perfect
>
> Luis

--

Joel Granados


Attachments:
(No filename) (611.00 B)
signature.asc (673.00 B)
Download all attachments