Re: [PATCH v4 1/2] fs/proc: optimize exactly register one ctl_table

From: Xiaoming Ni
Date: Thu Mar 03 2022 - 20:21:29 EST


On 2022/3/3 15:08, Meng Tang wrote:
Sysctls are being moved out of kernel/sysctl.c and out to
their own respective subsystems / users to help with easier
maintance and avoid merge conflicts. But when we move just
one entry and to its own new file the last entry for this
new file must be empty, so we are essentialy bloating the
kernel one extra empty entry per each newly moved sysctl.

To help with this, this adds support for registering just
one ctl_table, therefore not bloating the kernel when we
move a single ctl_table to its own file.

Since the process of registering just one single table is the
same as that of registering an array table, so the code is
similar to registering an array table. The difference between
registering just one table and registering an array table is
that we no longer traversal through pointers when registering
a single table. These lead to that we have to add a complete
implementation process for register just one ctl_table, so we
have to add so much code.

Suggested-by: Matthew Wilcox <willy@xxxxxxxxxxxxx>
Signed-off-by: Meng Tang <tangmeng@xxxxxxxxxxxxx>
---
fs/proc/proc_sysctl.c | 159 +++++++++++++++++++++++++++++------------
include/linux/sysctl.h | 9 ++-
2 files changed, 121 insertions(+), 47 deletions(-)

diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
index 6c87c99f0856..e06d2094457a 100644
--- a/fs/proc/proc_sysctl.c
+++ b/fs/proc/proc_sysctl.c
@@ -19,6 +19,8 @@
#include <linux/kmemleak.h>
#include "internal.h"
+#define REGISTER_SINGLE_ONE (register_single_one ? true : false)
+
static const struct dentry_operations proc_sys_dentry_operations;
static const struct file_operations proc_sys_file_operations;
static const struct inode_operations proc_sys_inode_operations;
@@ -100,8 +102,8 @@ static DEFINE_SPINLOCK(sysctl_lock);
static void drop_sysctl_table(struct ctl_table_header *header);
static int sysctl_follow_link(struct ctl_table_header **phead,
struct ctl_table **pentry);
-static int insert_links(struct ctl_table_header *head);
-static void put_links(struct ctl_table_header *header);
+static int insert_links(struct ctl_table_header *head, bool register_single_one);
+static void put_links(struct ctl_table_header *header, bool register_single_one);
static void sysctl_print_dir(struct ctl_dir *dir)
{
@@ -200,7 +202,7 @@ static void erase_entry(struct ctl_table_header *head, struct ctl_table *entry)
static void init_header(struct ctl_table_header *head,
struct ctl_table_root *root, struct ctl_table_set *set,
- struct ctl_node *node, struct ctl_table *table)
+ struct ctl_node *node, struct ctl_table *table, bool register_single_one)
{
head->ctl_table = table;
head->ctl_table_arg = table;
@@ -215,19 +217,26 @@ 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 (register_single_one)
The scalability is reduced.
If you add a file interface in the future, you need to make at least two code changes.

Instead of having each consumer keep the current table size in mind, you can obtain the table size by ARRAY_SIZE() in the API interface.

For example,

+ #define register_sysctl_init(path, table) __register_sysctl_init(path, table, ARRAY_SIZE(table))
...
- for (entry = table; entry->procname; entry++, node++)
+ for (entry = table; entry->procname && num > 0; entry++, node++, num--) {


Xiaoming Ni
thanks