Re: [PATCH v2 1/9] sysctl: support encoding values directly in the table entry

From: Wen Yang
Date: Fri Mar 22 2024 - 11:43:49 EST




On 2024/3/22 00:27, Joel Granados wrote:
On Sat, Mar 09, 2024 at 06:31:18PM +0800, wenyang.linux@xxxxxxxxxxx wrote:
From: Wen Yang <wenyang.linux@xxxxxxxxxxx>

Eric points out: "by turning .extra1 and .extra2 into longs instead of
keeping them as pointers and needing constants to be pointed at somewhere
... The only people I can see who find a significant benefit by
consolidating all of the constants into one place are people who know how
to stomp kernel memory."
I'm assuming that this is the "why" of the commit. Please change it so
it is more direct. Something like "Directly encode numeric values in
macros in order to ...". If you want to add Eric's opinion please add it
as a link (Please follow Documentation/process/submitting-patches.rst)



This patch supports encoding values directly in table entries through the
following work:
- extra1/extra2 and min/max are placed in one union to ensure that the
previous code is not broken, then we have time to remove unnecessary
extra1/extra2 progressively;
- since type only has two states, use one bit to represent it;
Please remove this optimization from your commit. This will conflict
with work that Thomas is doing here
https://lore.kernel.org/all/20240222-sysctl-empty-dir-v1-0-45ba9a6352e8@xxxxxxxxxxxxxx

- two bits were used to represent the information of the above union( 0:
using extra1/extra2, 1: using min, 2: using max, 3: using both min/max);
- added some helper macros.

Suggested-by: Eric W. Biederman <ebiederm@xxxxxxxxxxxx>
Signed-off-by: Wen Yang <wenyang.linux@xxxxxxxxxxx>
Cc: Luis Chamberlain <mcgrof@xxxxxxxxxx>
Cc: Kees Cook <keescook@xxxxxxxxxxxx>
Cc: Joel Granados <j.granados@xxxxxxxxxxx>
Cc: Eric W. Biederman <ebiederm@xxxxxxxxxxxx>
Cc: Christian Brauner <brauner@xxxxxxxxxx>
Cc: Iurii Zaikin <yzaikin@xxxxxxxxxx>
Cc: linux-kernel@xxxxxxxxxxxxxxx
---
include/linux/sysctl.h | 108 ++++++++++++++++++++++++++++++++++++++---
kernel/sysctl.c | 61 +++++++++++++++++------
2 files changed, 148 insertions(+), 21 deletions(-)

diff --git a/include/linux/sysctl.h b/include/linux/sysctl.h
index ee7d33b89e9e..1ba980219e40 100644
--- a/include/linux/sysctl.h
+++ b/include/linux/sysctl.h
@@ -61,6 +61,25 @@ extern const int sysctl_vals[];
extern const unsigned long sysctl_long_vals[];
+#define SYSCTL_NUMERIC_NEG_ONE ((long)-1)
+#define SYSCTL_NUMERIC_ZERO (0L)
+#define SYSCTL_NUMERIC_ONE (1L)
+#define SYSCTL_NUMERIC_TWO (2L)
+#define SYSCTL_NUMERIC_THREE (3L)
+#define SYSCTL_NUMERIC_FOUR (4L)
+#define SYSCTL_NUMERIC_ONE_HUNDRED (100L)
+#define SYSCTL_NUMERIC_TWO_HUNDRED (200L)
+#define SYSCTL_NUMERIC_THREE_HUNDRED (300L)
+#define SYSCTL_NUMERIC_FIVE_HUNDRED (500L)
+#define SYSCTL_NUMERIC_ONE_THOUSAND (1000L)
+#define SYSCTL_NUMERIC_TWO_THOUSAND (2000L)
+#define SYSCTL_NUMERIC_THREE_THOUSAND (3000L)
+#define SYSCTL_NUMERIC_16K (16384L)
+#define SYSCTL_NUMERIC_U8_MAX ((long)U8_MAX)
+#define SYSCTL_NUMERIC_U16_MAX ((long)U16_MAX)
+#define SYSCTL_NUMERIC_INT_MAX ((long)INT_MAX)
+#define SYSCTL_NUMERIC_LONG_MAX (LONG_MAX)
+
typedef int proc_handler(struct ctl_table *ctl, int write, void *buffer,
size_t *lenp, loff_t *ppos);
@@ -131,6 +150,18 @@ static inline void *proc_sys_poll_event(struct ctl_table_poll *poll)
#define DEFINE_CTL_TABLE_POLL(name) \
struct ctl_table_poll name = __CTL_TABLE_POLL_INITIALIZER(name)
+enum {
+ SYSCTL_TABLE_TYPE_DEFAULT,
+ SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY
+};
+
+enum {
+ SYSCTL_TABLE_EXTRA_PTR,
+ SYSCTL_TABLE_EXTRA_LONG_INIT_MIN,
+ SYSCTL_TABLE_EXTRA_LONG_INIT_MAX,
+ SYSCTL_TABLE_EXTRA_LONG_INIT_MINMAX
+};
+
/* A sysctl table is an array of struct ctl_table: */
struct ctl_table {
const char *procname; /* Text ID for /proc/sys, or zero */
@@ -138,20 +169,39 @@ struct ctl_table {
int maxlen;
umode_t mode;
/**
- * enum type - Enumeration to differentiate between ctl target types
+ * type - Indicates 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;
+ u8 type:1;
As I said before. Please remove this from your patch.

+
+ /**
+ * extra_flags
+ * @SYSCTL_TABLE_EXTRA_PTR: flag indicating that this uses extra1/extra2.
+ * @SYSCTL_TABLE_EXTRA_LONG_INIT_MIN: flag indicating that this uses min/max
+ and min has been initialized.
+ * @SYSCTL_TABLE_EXTRA_LONG_INIT_MAX: flag indicating that this uses min/max
+ and max has been initialized.
+ * @SYSCTL_TABLE_EXTRA_LONG_INIT_MINMAX: flag indicating that this uses min/max
+ and both have been initialized.
+ *
+ */
+ u8 extra_flags:2;
This extra_flag is needed because now you have lost the extra bit of
information that the NULL pointer gave you. This is effectively adding 2
bits per ctl_table element for all ctl_table types; event the ones that
do not need min max. So how much will we actually save with all this?
once you have added these 2 bits and removed the static variables from
the files that are not using the pointers? Is saving read only memory
the only reason for this? If that is the case, please add some
calculations of how much we save to see if it actually make sense. To
calculate the memory gains/losses you can use the bloat-o-meter script
under the scripts directory (something similar to what we did here
https://lore.kernel.org/all/20240314-jag-sysctl_remset_net-v1-0-aa26b44d29d9@xxxxxxxxxxx)

I'll hold off on reviewing the other patches in this set until this is a
bit more clear.


Thank you for your comments.

When we started this work, we had not yet seen Thomas's patch, so by borrowing the existing enum type‘s field, we can achieve directly encoding values without increasing the size of the ctl_table.

We really appreciate that you pointed out this issue. It will take some time for rework and the v3 will be sent out within a few weeks.

In addition, the patch below is not related to "kill sysctl_vals". It is just a regular optimization and was sent over 10 days ago. We also hope to receive your kind advice:

https://lkml.org/lkml/2024/3/8/871

[RESEND PATCH v2] sysctl: move the extra1/2 boundary check of u8 to sysctl_check_table_array

--
Best wishes,
Wen


+ union {
+ struct {
+ void *extra1;
+ void *extra2;
+ };
+ struct {
+ long min;
+ long max;
+ };
+ };
+
proc_handler *proc_handler; /* Callback for text formatting */
struct ctl_table_poll *poll;
- void *extra1;
- void *extra2;
} __randomize_layout;
...

Best