From patchwork Tue Nov 7 13:45:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joel Granados via B4 Relay X-Patchwork-Id: 162490 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:aa0b:0:b0:403:3b70:6f57 with SMTP id k11csp243435vqo; Tue, 7 Nov 2023 05:47:39 -0800 (PST) X-Google-Smtp-Source: AGHT+IFsn94mUz4X50lf+TTMDZqz++g6yn+1Jc3j5Df+7p0MbncNLNRDw/ha7IceVxrnqeJdiJbJ X-Received: by 2002:a05:6a00:244b:b0:68f:dcc1:4bef with SMTP id d11-20020a056a00244b00b0068fdcc14befmr37173299pfj.7.1699364859717; Tue, 07 Nov 2023 05:47:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699364859; cv=none; d=google.com; s=arc-20160816; b=P09RTLGwILUcWb7v/VLJqdz97KHFt7JgUnGK+2m6plMJvdPFhxP12dR+QbSQKa8LS+ UZnkpHpNIe3PrqbwLOF64qxv6Tn+6bIp6OGWBCd7zJYiNF9J4i4RB9Uitz3Dn2SJ+gpK nscrKX/fewQkEZg9hLENS9FXk0WLR/6qvBSBB1qhnJ7zyAqAcl8bTv3WGSFYKG/78fY6 SXyxb7obH5VrYLTpO16n52TUF/jR20Kc6Zmm6q2uzbDNOM6qA2MlrgAjDEmG+xdtFyu6 WNqSw0cTcmAGReDPdFwahuFmsrNodnS7sB/WOSHqKifCgjbn4xirsBNOBVZ804NC5uns FFFw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:reply-to:cc:to:content-transfer-encoding :mime-version:message-id:date:subject:from:dkim-signature; bh=1XWewP/7ZxIZ2WaHv/JHNvKlF6G8V+J0DkKuMvtqIOk=; fh=AuaFKPUwFYzSLqc7bUwLZldSXfNG4AOo7Dj5zlnvlas=; b=CzonPFjo7fxbMJLwQmd0rfrLOANQEJ6oqfHVnS4bA/o7mUIvl15IhgBeI4zCamI1gT aeYKZOfvn2jPkARzakKKGTd+CQrXNIdIh0Watw+Or6vLgCQdd8d40/K+YVhrjGE3DUdu FxC1Zg44QRLvwKeC56ms1QV/L8CSqW5i3piiYMTJl3LxrxzN2myFGrWt4LUOt6P9FVmX JjWq/cYecdikoIuamVn2jFM7Mh12aEUmlgRIj+DlDKQea2U0NcWp3XJmv10RXlWEKlTc vuL/sfcqflIEiFS7YCcwMtIDHYNLOAQ1nwI2kbgWCBghQOTP4NOL3QtYxLBmtpiGyexq grvQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=KsQ8uQ4w; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id l184-20020a6388c1000000b005b96a77e712si2181901pgd.17.2023.11.07.05.47.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Nov 2023 05:47:39 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=KsQ8uQ4w; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 297ED806BE28; Tue, 7 Nov 2023 05:47:21 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234790AbjKGNp5 (ORCPT + 32 others); Tue, 7 Nov 2023 08:45:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42842 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234938AbjKGNpd (ORCPT ); Tue, 7 Nov 2023 08:45:33 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C0BB51985 for ; Tue, 7 Nov 2023 05:45:13 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPS id 419ECC433C7; Tue, 7 Nov 2023 13:45:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1699364713; bh=AwAIos4Sf5lM4/VuEZbuV5/dEG9ZQKe0S0UcvNhou5k=; h=From:Subject:Date:To:Cc:Reply-To:From; b=KsQ8uQ4wetEv4j049E6gi4R4i15c/OOxVV3VEEJdB+trdNMoxOit3N7g3hQDYjXXO BisGNfJfMUN153BI0nuG8L6QtAfWxbnmwEeO+2XKCYYBoKdwlItiL3Jw1+GrWQnnv/ 8jSzxUfNy+SJ6gvnOBn+V0/7adI6bOxqIFpPs4GjkfVqc+rgsoCQ7bJW5cx965boMA Ev4A+MoqSr8behzmSzoqC3QzDAP7IRjD6sgNJh94rxoNUqSDTFc6AuWPkFI+pYkspW /LzxVMkw3/bvInB2I2L3oswtt4CuLyqFA9aPWHjGpCmmrmrgf3YebPKTCjgAPjXtcz u1q7VdAXpim4g== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 26FD0C4332F; Tue, 7 Nov 2023 13:45:13 +0000 (UTC) From: Joel Granados via B4 Relay Subject: [PATCH 00/10] sysctl: Remove sentinel elements from kernel dir Date: Tue, 07 Nov 2023 14:45:00 +0100 Message-Id: <20231107-jag-sysctl_remove_empty_elem_kernel-v1-0-e4ce1388dfa0@samsung.com> MIME-Version: 1.0 X-B4-Tracking: v=1; b=H4sIAFw/SmUC/x3NQQrCMBBG4auUWRuY1EXRq4iEkPyt0SQtM6VYS u9ucPlt3jtIIQlK9+4gwZY0zbXBXjoKL18nmBSbqef+ai0P5u0no7uGNTtBmTc4lGXdHTKK+0A qshkibhzGyIE9tdIiGNP3f3k8z/MH6UifCXUAAAA= To: Luis Chamberlain , willy@infradead.org, josh@joshtriplett.org, Kees Cook , Eric Biederman , Iurii Zaikin , Steven Rostedt , Masami Hiramatsu , Mark Rutland , Thomas Gleixner , John Stultz , Stephen Boyd , Andy Lutomirski , Will Drewry , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Ben Segall , Mel Gorman , Daniel Bristot de Oliveira , Valentin Schneider , Petr Mladek , John Ogness , Sergey Senozhatsky , "Naveen N. Rao" , Anil S Keshavamurthy , "David S. Miller" , Balbir Singh , Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa Cc: linux-kernel@vger.kernel.org, kexec@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, bpf@vger.kernel.org, Joel Granados X-Mailer: b4 0.13-dev-86aa5 X-Developer-Signature: v=1; a=openpgp-sha256; l=10919; i=j.granados@samsung.com; h=from:subject:message-id; bh=xWU4r/E9Gpw/khnI1HixMAzviaIfjsfKgtH5v99wl+0=; b=owEB7QES/pANAwAKAbqXzVK3lkFPAcsmYgBlSj9kLrHGpvjjY+JW0cTidHvDgKUOSvfOEx0Kv v10h7qMjmuJAbMEAAEKAB0WIQSuRwlXJeYxJc7LJ5C6l81St5ZBTwUCZUo/ZAAKCRC6l81St5ZB TyAIC/0RjVATmr8NI2emzi9ckxqduN7win50jTNyPG1arCL4HCQs6GvB9Qptd7R/Uz9UKeqbDhp K2XX9De2OttZetQJOG0jY3B9O+1+xIQiVKEfHGuFrnhZUWF+0jiTnRwXSEijSX+WfK6vlI75WyA 5ziwNZq4McR/YPT7SdEX79tvcUkuJq49b0azo9Vhafsshq+4uShOcuOgkhkNldRCvbDIGMRp9Fc aanseeDimXxHZeG1gwZfa8C/3DmDjq7XGvxHhYn/pc4Q6CKZB11fxwSkbSBSkjgmrZaO0vIO9gJ xA3EwqqVOWb5nSA6gDzC9g+sxzXlcQoGg5p9rf70gLz3kVIDWO0jUgsUO1FfOXDDNxfyphbt07D TtCgXh5hK1feM2KXRhVEaX0gFDzkyINhPe7B/vg4VcCkfu4YIFrVvh99EI654hVu3QyMy6O/0rf 4QqihPndtQveTOBlncWfA+KI0HVa1iqP8q5zQFeHPhLW6pK74qpg2SuUWRVE9l9xuPbfk= X-Developer-Key: i=j.granados@samsung.com; a=openpgp; fpr=F1F8E46D30F0F6C4A45FF4465895FAAC338C6E77 X-Endpoint-Received: by B4 Relay for j.granados@samsung.com/default with auth_id=70 X-Original-From: Joel Granados Reply-To: X-Spam-Status: No, score=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Tue, 07 Nov 2023 05:47:21 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781913206888254022 X-GMAIL-MSGID: 1781913206888254022 From: Joel Granados What? These commits remove the sentinel element (last empty element) from the sysctl arrays of all the files under the "kernel/" directory that use a sysctl array for registration. The merging of the preparation patches (in https://lore.kernel.org/all/ZO5Yx5JFogGi%2FcBo@bombadil.infradead.org/) to mainline allows us to just remove sentinel elements without changing behavior (more info here [1]). These commits are part of a bigger set (here https://github.com/Joelgranados/linux/tree/tag/sysctl_remove_empty_elem_V5) that remove the ctl_table sentinel. Make the review process easier by chunking the commits into manageable pieces. Each chunk can be reviewed separately without noise from parallel sets. Sending the "kernel/*" chunk now that the "drivers/" has been mostly reviewed [6]. After this and the "fs/*" are reviewed we only miss 2 more chunks ("net/*" and miscellaneous) to complete the sentinel removal. Hurray!!! 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]. Testing: * Ran sysctl selftests (./tools/testing/selftests/sysctl/sysctl.sh) * Ran this through 0-day with no errors or warnings Size saving after this patchset: * bloat-o-meter - The "yesall" config saves 1984 bytes [4] - The "tiny" config saves 771 bytes [5] * If you want to know how many bytes are saved after all the chunks are merged see [3] Base commit: tag: sysctl-6.7-rc1 (8b793bcda61f) Comments/feedback greatly appreciated Best Joel [1] We are able to remove a sentinel table without behavioral change by introducing a table_size argument in the same place where procname is checked for NULL. The idea is for it to keep stopping when it hits ->procname == NULL, while the sentinel is still present. And when the sentinel is removed, it will stop on the table_size. You can go to (https://lore.kernel.org/all/20230809105006.1198165-1-j.granados@samsung.com/) for more information. [2] Links Related to the ctl_table sentinel removal: * E-mail threads that summarize the sentinel effort https://lore.kernel.org/all/ZO5Yx5JFogGi%2FcBo@bombadil.infradead.org/ https://lore.kernel.org/all/ZMFizKFkVxUFtSqa@bombadil.infradead.org/ * Replacing the register functions: https://lore.kernel.org/all/20230302204612.782387-1-mcgrof@kernel.org/ https://lore.kernel.org/all/20230302202826.776286-1-mcgrof@kernel.org/ * E-mail threads discussing prposal https://lore.kernel.org/all/20230321130908.6972-1-frank.li@vivo.com https://lore.kernel.org/all/20220220060626.15885-1-tangmeng@uniontech.com [3] Size saving after removing all sentinels: These are the bytes that we save after removing all the sentinels (this plus all the other chunks). I included them to get an idea of how much memory we are talking about. * bloat-o-meter: - The "yesall" configuration results save 9158 bytes https://lore.kernel.org/all/20230621091000.424843-1-j.granados@samsung.com/ - The "tiny" config + CONFIG_SYSCTL save 1215 bytes https://lore.kernel.org/all/20230809105006.1198165-1-j.granados@samsung.com/ * memory usage: In memory savings are measured to be 7296 bytes. (here is how to measure [7]) [4] add/remove: 0/0 grow/shrink: 0/31 up/down: 0/-1984 (-1984) Function old new delta watchdog_sysctls 576 512 -64 watchdog_hardlockup_sysctl 128 64 -64 vm_table 1344 1280 -64 uts_kern_table 448 384 -64 usermodehelper_table 192 128 -64 user_table 832 768 -64 user_event_sysctls 128 64 -64 timer_sysctl 128 64 -64 signal_debug_table 128 64 -64 seccomp_sysctl_table 192 128 -64 sched_rt_sysctls 256 192 -64 sched_fair_sysctls 256 192 -64 sched_energy_aware_sysctls 128 64 -64 sched_dl_sysctls 192 128 -64 sched_core_sysctls 384 320 -64 sched_autogroup_sysctls 128 64 -64 printk_sysctls 512 448 -64 pid_ns_ctl_table_vm 128 64 -64 pid_ns_ctl_table 128 64 -64 latencytop_sysctl 128 64 -64 kprobe_sysctls 128 64 -64 kexec_core_sysctls 256 192 -64 kern_table 2560 2496 -64 kern_reboot_table 192 128 -64 kern_panic_table 192 128 -64 kern_exit_table 128 64 -64 kern_delayacct_table 128 64 -64 kern_acct_table 128 64 -64 hung_task_sysctls 448 384 -64 ftrace_sysctls 128 64 -64 bpf_syscall_table 192 128 -64 Total: Before=429912331, After=429910347, chg -0.00% [5] add/remove: 0/1 grow/shrink: 0/12 up/down: 0/-771 (-771) Function old new delta sched_core_sysctl_init 43 40 -3 vm_table 1024 960 -64 uts_kern_table 448 384 -64 usermodehelper_table 192 128 -64 user_table 576 512 -64 signal_debug_table 128 64 -64 sched_rt_sysctls 256 192 -64 sched_fair_sysctls 128 64 -64 sched_dl_sysctls 192 128 -64 sched_core_sysctls 64 - -64 kern_table 1792 1728 -64 kern_panic_table 128 64 -64 kern_exit_table 128 64 -64 Total: Before=1886645, After=1885874, chg -0.04% [6] https://lore.kernel.org/all/20231002-jag-sysctl_remove_empty_elem_drivers-v2-0-02dd0d46f71e@samsung.com [7] To measure the in memory savings apply this on top of this patchset. " " 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 --- Signed-off-by: Joel Granados --- Joel Granados (10): kernel misc: Remove the now superfluous sentinel elements from ctl_table array umh: Remove the now superfluous sentinel elements from ctl_table array ftrace: Remove the now superfluous sentinel elements from ctl_table array timekeeping: Remove the now superfluous sentinel elements from ctl_table array seccomp: Remove the now superfluous sentinel elements from ctl_table array scheduler: Remove the now superfluous sentinel elements from ctl_table array printk: Remove the now superfluous sentinel elements from ctl_table array kprobes: Remove the now superfluous sentinel elements from ctl_table array delayacct: Remove the now superfluous sentinel elements from ctl_table array bpf: Remove the now superfluous sentinel elements from ctl_table array kernel/acct.c | 1 - kernel/bpf/syscall.c | 1 - kernel/delayacct.c | 1 - kernel/exit.c | 1 - kernel/hung_task.c | 1 - kernel/kexec_core.c | 1 - kernel/kprobes.c | 1 - kernel/latencytop.c | 1 - kernel/panic.c | 1 - kernel/pid_namespace.c | 1 - kernel/pid_sysctl.h | 1 - kernel/printk/sysctl.c | 1 - kernel/reboot.c | 1 - kernel/sched/autogroup.c | 1 - kernel/sched/core.c | 1 - kernel/sched/deadline.c | 1 - kernel/sched/fair.c | 1 - kernel/sched/rt.c | 1 - kernel/sched/topology.c | 1 - kernel/seccomp.c | 1 - kernel/signal.c | 1 - kernel/stackleak.c | 1 - kernel/sysctl.c | 2 -- kernel/time/timer.c | 1 - kernel/trace/ftrace.c | 1 - kernel/trace/trace_events_user.c | 1 - kernel/ucount.c | 3 +-- kernel/umh.c | 1 - kernel/utsname_sysctl.c | 1 - kernel/watchdog.c | 2 -- 30 files changed, 1 insertion(+), 33 deletions(-) --- base-commit: 8b793bcda61f6c3ed4f5b2ded7530ef6749580cb change-id: 20231107-jag-sysctl_remove_empty_elem_kernel-7de90cfd0c0a Best regards, 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 + // + 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;