linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/3] sysctl: move sysctl type to ctl_table_header
@ 2024-03-22 17:05 ` Thomas Weißschuh
  2024-03-22 17:05   ` [PATCH v2 1/3] sysctl: drop sysctl_is_perm_empty_ctl_table Thomas Weißschuh
                     ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Thomas Weißschuh @ 2024-03-22 17:05 UTC (permalink / raw)
  To: Eric W. Biederman, Luis Chamberlain, Kees Cook, Joel Granados
  Cc: linux-kernel, linux-fsdevel, 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/20231204-const-sysctl-v2-0-7a5060b11447@weissschuh.net/

---
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/20240222-sysctl-empty-dir-v1-0-45ba9a6352e8@weissschuh.net

---
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,
-- 
Thomas Weißschuh <linux@weissschuh.net>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2 1/3] sysctl: drop sysctl_is_perm_empty_ctl_table
  2024-03-22 17:05 ` [PATCH v2 0/3] sysctl: move sysctl type to ctl_table_header Thomas Weißschuh
@ 2024-03-22 17:05   ` Thomas Weißschuh
  2024-03-22 17:05   ` [PATCH v2 2/3] sysctl: move sysctl type to ctl_table_header Thomas Weißschuh
                     ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Thomas Weißschuh @ 2024-03-22 17:05 UTC (permalink / raw)
  To: Eric W. Biederman, Luis Chamberlain, Kees Cook, Joel Granados
  Cc: linux-kernel, linux-fsdevel, Thomas Weißschuh

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.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
 fs/proc/proc_sysctl.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
index 37cde0efee57..2f4d4329d83d 100644
--- a/fs/proc/proc_sysctl.c
+++ b/fs/proc/proc_sysctl.c
@@ -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? */

-- 
2.44.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v2 2/3] sysctl: move sysctl type to ctl_table_header
  2024-03-22 17:05 ` [PATCH v2 0/3] sysctl: move sysctl type to ctl_table_header Thomas Weißschuh
  2024-03-22 17:05   ` [PATCH v2 1/3] sysctl: drop sysctl_is_perm_empty_ctl_table Thomas Weißschuh
@ 2024-03-22 17:05   ` Thomas Weißschuh
  2024-03-22 17:05   ` [PATCH v2 3/3] sysctl: drop now unnecessary out-of-bounds check Thomas Weißschuh
  2024-04-03  9:01   ` [PATCH v2 0/3] sysctl: move sysctl type to ctl_table_header Joel Granados
  3 siblings, 0 replies; 5+ messages in thread
From: Thomas Weißschuh @ 2024-03-22 17:05 UTC (permalink / raw)
  To: Eric W. Biederman, Luis Chamberlain, Kees Cook, Joel Granados
  Cc: linux-kernel, linux-fsdevel, 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.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
 fs/proc/proc_sysctl.c  | 10 ++++++----
 include/linux/sysctl.h | 22 +++++++++++-----------
 2 files changed, 17 insertions(+), 15 deletions(-)

diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
index 2f4d4329d83d..fde7a2f773f0 100644
--- a/fs/proc/proc_sysctl.c
+++ b/fs/proc/proc_sysctl.c
@@ -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)
diff --git a/include/linux/sysctl.h b/include/linux/sysctl.h
index ee7d33b89e9e..c87f73c06cb9 100644
--- a/include/linux/sysctl.h
+++ b/include/linux/sysctl.h
@@ -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 {

-- 
2.44.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v2 3/3] sysctl: drop now unnecessary out-of-bounds check
  2024-03-22 17:05 ` [PATCH v2 0/3] sysctl: move sysctl type to ctl_table_header Thomas Weißschuh
  2024-03-22 17:05   ` [PATCH v2 1/3] sysctl: drop sysctl_is_perm_empty_ctl_table Thomas Weißschuh
  2024-03-22 17:05   ` [PATCH v2 2/3] sysctl: move sysctl type to ctl_table_header Thomas Weißschuh
@ 2024-03-22 17:05   ` Thomas Weißschuh
  2024-04-03  9:01   ` [PATCH v2 0/3] sysctl: move sysctl type to ctl_table_header Joel Granados
  3 siblings, 0 replies; 5+ messages in thread
From: Thomas Weißschuh @ 2024-03-22 17:05 UTC (permalink / raw)
  To: Eric W. Biederman, Luis Chamberlain, Kees Cook, Joel Granados
  Cc: linux-kernel, linux-fsdevel, 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.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
 fs/proc/proc_sysctl.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
index fde7a2f773f0..7c0e27dc3d9d 100644
--- a/fs/proc/proc_sysctl.c
+++ b/fs/proc/proc_sysctl.c
@@ -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);

-- 
2.44.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 0/3] sysctl: move sysctl type to ctl_table_header
  2024-03-22 17:05 ` [PATCH v2 0/3] sysctl: move sysctl type to ctl_table_header Thomas Weißschuh
                     ` (2 preceding siblings ...)
  2024-03-22 17:05   ` [PATCH v2 3/3] sysctl: drop now unnecessary out-of-bounds check Thomas Weißschuh
@ 2024-04-03  9:01   ` Joel Granados
  3 siblings, 0 replies; 5+ messages in thread
From: Joel Granados @ 2024-04-03  9:01 UTC (permalink / raw)
  To: Thomas Weißschuh
  Cc: Eric W. Biederman, Luis Chamberlain, Kees Cook, linux-kernel,
	linux-fsdevel

[-- Attachment #1: Type: text/plain, Size: 1608 bytes --]

Added this to the constfy branch Thx

On Fri, Mar 22, 2024 at 06:05:55PM +0100, Thomas Weißschuh wrote:
> 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/20231204-const-sysctl-v2-0-7a5060b11447@weissschuh.net/
> 
> ---
> 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/20240222-sysctl-empty-dir-v1-0-45ba9a6352e8@weissschuh.net
> 
> ---
> 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,
> -- 
> Thomas Weißschuh <linux@weissschuh.net>
> 

Signed-off-by: Joel Granados <j.granados@samsung.com>

-- 

Joel Granados

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 659 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-04-05  6:35 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <CGME20240322170605eucas1p2efc8b108034476c5e6f637b0dcf0671d@eucas1p2.samsung.com>
2024-03-22 17:05 ` [PATCH v2 0/3] sysctl: move sysctl type to ctl_table_header Thomas Weißschuh
2024-03-22 17:05   ` [PATCH v2 1/3] sysctl: drop sysctl_is_perm_empty_ctl_table Thomas Weißschuh
2024-03-22 17:05   ` [PATCH v2 2/3] sysctl: move sysctl type to ctl_table_header Thomas Weißschuh
2024-03-22 17:05   ` [PATCH v2 3/3] sysctl: drop now unnecessary out-of-bounds check Thomas Weißschuh
2024-04-03  9:01   ` [PATCH v2 0/3] sysctl: move sysctl type to ctl_table_header Joel Granados

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).