Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp1393836pxm; Thu, 24 Feb 2022 02:10:31 -0800 (PST) X-Google-Smtp-Source: ABdhPJyjAoTPkSVWTzsZ7scw/6PQQz+Myh8x+egTYG5Ni7gfw2EX2gbaDODduj4eS2OO7Mkir8yU X-Received: by 2002:a05:6a00:1d13:b0:4e1:7958:b59d with SMTP id a19-20020a056a001d1300b004e17958b59dmr1920251pfx.68.1645697430812; Thu, 24 Feb 2022 02:10:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645697430; cv=none; d=google.com; s=arc-20160816; b=ZJV+Pfy8XD6kpEdZPeSHoaUtMH1wGhnt2/TRwCGLK1cCn4i0mVC0kgkchrnBbBYLCF e5dYZ8nz5ticR0Pk2bhMlK0/3ihmiq3LfRvUHl4JO02Fvhe+K33asTsHHIhr2OTAdaRX G+6wRXIrDQ2im2r9bIUK3AFoEURSjEDRleKBiL/pw9kN21JuhDgWXuRfATvn2uS6q3v4 Sru6dG4bqr+KBIpopUt8GNVZsTwIWt9B+v5x5ss11GF1YdXG0rkubmuYgpMNiaTjJaFA UZCusSZqvWEfVoVq7kG+kVf0MllcuLGOkG75uQXwrQaZQD/flI/xIN4Z1xAJpyRYJXLk wCVA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:feedback-id:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from; bh=cbQLFmCIv4OheUax3A9XCsdGn+6KuAsArmBnzEy21zo=; b=CuRQmsNTI7Cs3lrK51Rsw3xnDZ/k4GXiMZo/MNIDaaRiUX1xbT5jthNQRVSQhaNWUS 7j6GLs3DJoAPYPsDtQdVAI2+lRSyQPO66IDnHp8aqDmzFhhCWB2HsvteZEQXtyPDaEmy M4F/LPOMgA95zwsIKyDsLwm0SHfdBOSlV4aRMKGTPk5IXsyeUIcKxA7aR2Vwq5K5Ouc9 pinaVsc2zMg7qElOvw6Jskw9crwjnxAtGlVGtx/CmRf6OPEc/FH5hwNgYWRVB3Lzqoo8 oWqJ/MKF4DuWpGjetVgxieAMG8AbKmVt8ZukdDspqcrJBnXF5LLJ8EllqsvYoJwmp0oH yx1A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s36si2109786pgk.583.2022.02.24.02.10.15; Thu, 24 Feb 2022 02:10:30 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232762AbiBXJdR (ORCPT + 99 others); Thu, 24 Feb 2022 04:33:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46160 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232750AbiBXJdN (ORCPT ); Thu, 24 Feb 2022 04:33:13 -0500 Received: from smtpbguseast3.qq.com (smtpbguseast3.qq.com [54.243.244.52]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F01B020A952 for ; Thu, 24 Feb 2022 01:32:41 -0800 (PST) X-QQ-mid: bizesmtp88t1645695147tgwdwkrk Received: from localhost.localdomain (unknown [58.240.82.166]) by bizesmtp.qq.com (ESMTP) with id ; Thu, 24 Feb 2022 17:32:03 +0800 (CST) X-QQ-SSF: 01400000000000B0F000B00A0000000 X-QQ-FEAT: ZHWZeLXy+8esjqh9jW/ic4y9x7g7V3PVFwHyCbsFjWvtnbyYVlEAO51fszt3T bHxSjNJkqhDKUPW3TEdys3rX/ZjRS2WriSEw7szpdmFr/wjNCZ3jy56t5N8eNio1rzwavz9 g+NrNE0rkbkIglNhcKQPmsFNTM+I/Moj/xIV9pAImVgNztfggHlOmlPsCsIIOZSZVhkpkNL izzDMA51RSzItJVneHxc9/MQE6Z+LIbwQD3tSUqbIjj2t/VxU3jZiT3G/JepSwPApydef8p cl4EL/QX0ywPwrDkNQ+JHzuetTFGg1Qvi/emVHK++mxW5+pUkJqzmjBufyCtfjCv6vAJLzb KDYE1h3DWLL2Qa5JaX4/pPWb0yLeg== X-QQ-GoodBg: 2 From: Meng Tang To: mcgrof@kernel.org, keescook@chromium.org, yzaikin@google.com Cc: nixiaoming@huawei.com, nizhen@uniontech.com, zhanglianjie@uniontech.com, sujiaxun@uniontech.com, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, Meng Tang Subject: [PATCH] fs/proc: optimize exactly register one ctl_table Date: Thu, 24 Feb 2022 17:32:01 +0800 Message-Id: <20220224093201.12440-1-tangmeng@uniontech.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:uniontech.com:qybgforeign:qybgforeign6 X-QQ-Bgrelay: 1 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Currently, sysctl is being moved to its own file. But ctl_table is quite large(64 bytes per entry) and every array is terminated with an empty one. This leads to thar when register exactly one ctl_table, we've gone from 64 bytes to 128 bytes. So, it is obviously the right thing that we need to fix. In order to avoid compatibility problems, and to be compatible with array terminated with an empty one and register exactly one ctl_table, add the register_one variable in the ctl_table structure to fix it. When we register exactly one table, we only need to add "table->register = true" to avoid gone from 64 bytes to 128 bytes. Signed-off-by: Meng Tang --- fs/proc/proc_sysctl.c | 58 +++++++++++++++++++++++++++++++++++++++--- include/linux/sysctl.h | 1 + 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c index 7d9cfc730bd4..9ecd5c87e8dd 100644 --- a/fs/proc/proc_sysctl.c +++ b/fs/proc/proc_sysctl.c @@ -215,16 +215,24 @@ static void init_header(struct ctl_table_header *head, INIT_HLIST_HEAD(&head->inodes); if (node) { struct ctl_table *entry; - for (entry = table; entry->procname; entry++, node++) + for (entry = table; entry->procname; entry++, node++) { node->header = head; + + if (entry->register_one) + break; + } } } static void erase_header(struct ctl_table_header *head) { struct ctl_table *entry; - for (entry = head->ctl_table; entry->procname; entry++) + for (entry = head->ctl_table; entry->procname; entry++) { erase_entry(head, entry); + + if (entry->register_one) + break; + } } static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header) @@ -252,6 +260,9 @@ static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header) err = insert_entry(header, entry); if (err) goto fail; + + if (entry->register_one) + break; } return 0; fail: @@ -1159,6 +1170,9 @@ static int sysctl_check_table(const char *path, struct ctl_table *table) if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode) err |= sysctl_err(path, table, "bogus .mode 0%o", table->mode); + + if (table->register_one) + break; } return err; } @@ -1177,6 +1191,9 @@ static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table for (entry = table; entry->procname; entry++) { nr_entries++; name_bytes += strlen(entry->procname) + 1; + + if (entry->register_one) + break; } links = kzalloc(sizeof(struct ctl_table_header) + @@ -1199,6 +1216,9 @@ static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table link->mode = S_IFLNK|S_IRWXUGO; link->data = link_root; link_name += len; + + if (entry->register_one) + break; } init_header(links, dir->header.root, dir->header.set, node, link_table); links->nreg = nr_entries; @@ -1218,6 +1238,15 @@ static bool get_links(struct ctl_dir *dir, link = find_entry(&head, dir, procname, strlen(procname)); if (!link) return false; + + if (entry->register_one) { + if (S_ISDIR(link->mode) && S_ISDIR(entry->mode)) + break; + if (S_ISLNK(link->mode) && (link->data == link_root)) + break; + return false; + } + if (S_ISDIR(link->mode) && S_ISDIR(entry->mode)) continue; if (S_ISLNK(link->mode) && (link->data == link_root)) @@ -1230,6 +1259,8 @@ static bool get_links(struct ctl_dir *dir, const char *procname = entry->procname; link = find_entry(&head, dir, procname, strlen(procname)); head->nreg++; + if (entry->register_one) + break; } return true; } @@ -1295,6 +1326,8 @@ static int insert_links(struct ctl_table_header *head) * * mode - the file permissions for the /proc/sys file * + * register_one - set to true when exactly register one ctl_table + * * child - must be %NULL. * * proc_handler - the text handler routine (described below) @@ -1329,9 +1362,13 @@ struct ctl_table_header *__register_sysctl_table( struct ctl_node *node; int nr_entries = 0; - for (entry = table; entry->procname; entry++) + for (entry = table; entry->procname; entry++) { nr_entries++; + if (entry->register_one) + break; + } + header = kzalloc(sizeof(struct ctl_table_header) + sizeof(struct ctl_node)*nr_entries, GFP_KERNEL); if (!header) @@ -1461,6 +1498,9 @@ static int count_subheaders(struct ctl_table *table) nr_subheaders += count_subheaders(entry->child); else has_files = 1; + + if (entry->register_one) + break; } return nr_subheaders + has_files; } @@ -1480,6 +1520,9 @@ static int register_leaf_sysctl_tables(const char *path, char *pos, nr_dirs++; else nr_files++; + + if (entry->register_one) + break; } files = table; @@ -1497,6 +1540,9 @@ static int register_leaf_sysctl_tables(const char *path, char *pos, continue; *new = *entry; new++; + + if (entry->register_one) + break; } } @@ -1532,6 +1578,9 @@ static int register_leaf_sysctl_tables(const char *path, char *pos, pos[0] = '\0'; if (err) goto out; + + if (entry->register_one) + break; } err = 0; out: @@ -1686,6 +1735,9 @@ static void put_links(struct ctl_table_header *header) sysctl_print_dir(parent); pr_cont("%s\n", name); } + + if (entry->register_one) + break; } } diff --git a/include/linux/sysctl.h b/include/linux/sysctl.h index 6353d6db69b2..889c995d8a08 100644 --- a/include/linux/sysctl.h +++ b/include/linux/sysctl.h @@ -134,6 +134,7 @@ struct ctl_table { void *data; int maxlen; umode_t mode; + bool register_one; /* Exactly register one ctl_table*/ struct ctl_table *child; /* Deprecated */ proc_handler *proc_handler; /* Callback for text formatting */ struct ctl_table_poll *poll; -- 2.20.1