Skip to contentSkip to conversation

sysctl: move sysctl type to ctl_table_header

PATCH In review Thomas Weißschuh 2 participants 5 messages Mar 22, 2024 – Apr 5, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v2 cover sysctl: move sysctl type to ctl_table_header

Praparation series to enable constification of struct ctl_table further down the line.
No functional changes are intended.

These changes have been split out and reworked from my original const sysctl patchset [0].
I'm resubmitting the patchset in smaller chunks for easier review. Each split-out series is meant to be useful on its own.

Changes since the original series:
* Explicit initializartion of header->type in init_header()
* Some additional cleanups

[0] https://lore.kernel.org/lkml/[email protected]/

---
Changes in v2:
- Rebase onto next-20240322 without changes
- Squash patch 4 into patch 3 (Joel)
- Rework commit messages as per Joels requests
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Thomas Weißschuh (3):
sysctl: drop sysctl_is_perm_empty_ctl_table
sysctl: move sysctl type to ctl_table_header
sysctl: drop now unnecessary out-of-bounds check

 fs/proc/proc_sysctl.c  | 19 ++++++++-----------
 include/linux/sysctl.h | 22 +++++++++++-----------
 2 files changed, 19 insertions(+), 22 deletions(-)

---
base-commit: 13ee4a7161b6fd938aef6688ff43b163f6d83e37
change-id: 20231216-sysctl-empty-dir-71d7631f7bfe

Best regards,

Signature · 1 line
Thomas Weißschuh <[email protected]>
#1 lore raw

PATCH v2 1/3 sysctl: drop sysctl_is_perm_empty_ctl_table

It is used only twice and those callers are simpler with sysctl_is_perm_empty_ctl_header().
So use this sibling function.

This is part of an effort to constify definition of struct ctl_table. For this effort the mutable member 'type' is moved from struct ctl_table to struct ctl_table_header.
Unifying the macros sysctl_is_perm_empty_ctl_* makes this easier.

---
 fs/proc/proc_sysctl.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
fs/proc/proc_sysctl.c +3 -5
index 37cde0efee57..2f4d4329d83d 100644
@@ -48,10 +48,8 @@ struct ctl_table_header *register_sysctl_mount_point(const char *path)
 }
 EXPORT_SYMBOL(register_sysctl_mount_point);
 
-#define sysctl_is_perm_empty_ctl_table(tptr)		\
-	(tptr[0].type == SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY)
 #define sysctl_is_perm_empty_ctl_header(hptr)		\
-	(sysctl_is_perm_empty_ctl_table(hptr->ctl_table))
+	(hptr->ctl_table[0].type == SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY)
 #define sysctl_set_perm_empty_ctl_header(hptr)		\
 	(hptr->ctl_table[0].type = SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY)
 #define sysctl_clear_perm_empty_ctl_header(hptr)	\
@@ -233,7 +231,7 @@ static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header)
 
 	/* Am I creating a permanently empty directory? */
 	if (header->ctl_table_size > 0 &&
-	    sysctl_is_perm_empty_ctl_table(header->ctl_table)) {
+	    sysctl_is_perm_empty_ctl_header(header)) {
 		if (!RB_EMPTY_ROOT(&dir->root))
 			return -EINVAL;
 		sysctl_set_perm_empty_ctl_header(dir_h);
@@ -1204,7 +1202,7 @@ static bool get_links(struct ctl_dir *dir,
 	struct ctl_table *entry, *link;
 
 	if (header->ctl_table_size == 0 ||
-	    sysctl_is_perm_empty_ctl_table(header->ctl_table))
+	    sysctl_is_perm_empty_ctl_header(header))
 		return true;
 
 	/* Are there links available for every entry in table? */
Signature · 1 line
2.44.0
#2 lore raw

PATCH v2 2/3 sysctl: move sysctl type to ctl_table_header

In reply to Thomas Weißschuh

Move the SYSCTL_TABLE_TYPE_{DEFAULT,PERMANENTLY_EMPTY} enums from ctl_table to ctl_table_header.
Removing the mutable member is necessary to constify static instances of struct ctl_table.

Move the initialization of the sysctl_mount_point type into init_header() where all the other header fields are also initialized.

As a side-effect the memory usage of the sysctl core is reduced. Each ctl_table_header instance can manage multiple ctl_table instances and is only allocated when the table is actually registered. This saves 8 bytes of memory per ctl_table on 64bit, 4 due to the enum field itself and 4 due to padding.

---
 fs/proc/proc_sysctl.c  | 10 ++++++----
 include/linux/sysctl.h | 22 +++++++++++-----------
 2 files changed, 17 insertions(+), 15 deletions(-)
fs/proc/proc_sysctl.c +6 -4
index 2f4d4329d83d..fde7a2f773f0 100644
@@ -31,7 +31,7 @@ static const struct inode_operations proc_sys_dir_operations;
 
 /* Support for permanently empty directories */
 static struct ctl_table sysctl_mount_point[] = {
-	{.type = SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY }
+	{ }
 };
 
 /**
@@ -49,11 +49,11 @@ struct ctl_table_header *register_sysctl_mount_point(const char *path)
 EXPORT_SYMBOL(register_sysctl_mount_point);
 
 #define sysctl_is_perm_empty_ctl_header(hptr)		\
-	(hptr->ctl_table[0].type == SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY)
+	(hptr->type == SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY)
 #define sysctl_set_perm_empty_ctl_header(hptr)		\
-	(hptr->ctl_table[0].type = SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY)
+	(hptr->type = SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY)
 #define sysctl_clear_perm_empty_ctl_header(hptr)	\
-	(hptr->ctl_table[0].type = SYSCTL_TABLE_TYPE_DEFAULT)
+	(hptr->type = SYSCTL_TABLE_TYPE_DEFAULT)
 
 void proc_sys_poll_notify(struct ctl_table_poll *poll)
 {
@@ -208,6 +208,8 @@ static void init_header(struct ctl_table_header *head,
 			node++;
 		}
 	}
+	if (table == sysctl_mount_point)
+		sysctl_set_perm_empty_ctl_header(head);
 }
 
 static void erase_header(struct ctl_table_header *head)
include/linux/sysctl.h +11 -11
index ee7d33b89e9e..c87f73c06cb9 100644
@@ -137,17 +137,6 @@ struct ctl_table {
 	void *data;
 	int maxlen;
 	umode_t mode;
-	/**
-	 * enum type - Enumeration to differentiate between ctl target types
-	 * @SYSCTL_TABLE_TYPE_DEFAULT: ctl target with no special considerations
-	 * @SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY: Used to identify a permanently
-	 *                                       empty directory target to serve
-	 *                                       as mount point.
-	 */
-	enum {
-		SYSCTL_TABLE_TYPE_DEFAULT,
-		SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY
-	} type;
 	proc_handler *proc_handler;	/* Callback for text formatting */
 	struct ctl_table_poll *poll;
 	void *extra1;
@@ -188,6 +177,17 @@ struct ctl_table_header {
 	struct ctl_dir *parent;
 	struct ctl_node *node;
 	struct hlist_head inodes; /* head for proc_inode->sysctl_inodes */
+	/**
+	 * enum type - Enumeration to differentiate between ctl target types
+	 * @SYSCTL_TABLE_TYPE_DEFAULT: ctl target with no special considerations
+	 * @SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY: Used to identify a permanently
+	 *                                       empty directory target to serve
+	 *                                       as mount point.
+	 */
+	enum {
+		SYSCTL_TABLE_TYPE_DEFAULT,
+		SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY,
+	} type;
 };
 
 struct ctl_dir {
Signature · 1 line
2.44.0
#3 lore raw

PATCH v2 3/3 sysctl: drop now unnecessary out-of-bounds check

In reply to Thomas Weißschuh

Remove the now unneeded check for ctl_table_size; it is safe to do so as sysctl_set_perm_empty_ctl_header() does not access the ctl_table member anymore.

This also makes the element of sysctl_mount_point unnecessary, so drop it at the same time.

---
 fs/proc/proc_sysctl.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
fs/proc/proc_sysctl.c +2 -5
index fde7a2f773f0..7c0e27dc3d9d 100644
@@ -30,9 +30,7 @@ static const struct file_operations proc_sys_dir_file_operations;
 static const struct inode_operations proc_sys_dir_operations;
 
 /* Support for permanently empty directories */
-static struct ctl_table sysctl_mount_point[] = {
-	{ }
-};
+static struct ctl_table sysctl_mount_point[] = { };
 
 /**
  * register_sysctl_mount_point() - registers a sysctl mount point
@@ -232,8 +230,7 @@ static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header)
 		return -EROFS;
 
 	/* Am I creating a permanently empty directory? */
-	if (header->ctl_table_size > 0 &&
-	    sysctl_is_perm_empty_ctl_header(header)) {
+	if (sysctl_is_perm_empty_ctl_header(header)) {
 		if (!RB_EMPTY_ROOT(&dir->root))
 			return -EINVAL;
 		sysctl_set_perm_empty_ctl_header(dir_h);
Signature · 1 line
2.44.0
#4 lore raw
In reply to Thomas Weißschuh

Added this to the constfy branch Thx

Quoted 28 lines · Thomas Weißschuh

Praparation series to enable constification of struct ctl_table further down the line.
No functional changes are intended.

These changes have been split out and reworked from my original const sysctl patchset [0].
I'm resubmitting the patchset in smaller chunks for easier review. Each split-out series is meant to be useful on its own.

Changes since the original series:
* Explicit initializartion of header->type in init_header()
* Some additional cleanups

[0] https://lore.kernel.org/lkml/[email protected]/

---
Changes in v2:
- Rebase onto next-20240322 without changes
- Squash patch 4 into patch 3 (Joel)
- Rework commit messages as per Joels requests
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Thomas Weißschuh (3):
sysctl: drop sysctl_is_perm_empty_ctl_table
sysctl: move sysctl type to ctl_table_header
sysctl: drop now unnecessary out-of-bounds check

Quoted 11 lines
 fs/proc/proc_sysctl.c  | 19 ++++++++-----------
 include/linux/sysctl.h | 22 +++++++++++-----------
 2 files changed, 19 insertions(+), 22 deletions(-)

---
base-commit: 13ee4a7161b6fd938aef6688ff43b163f6d83e37
change-id: 20231216-sysctl-empty-dir-71d7631f7bfe

Best regards,

Signature · 1 line
Thomas Weißschuh <[email protected]>
Signature · 1 line
Joel Granados