From b47e0fac0356308cf34aa235ba9328c0c9de51bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Sat, 3 Dec 2022 11:27:40 +0100 Subject: [PATCH 1/3] manager: do not append '\n' when writing sysctl settings When booting with debug logs, we print: Setting '/proc/sys/fs/file-max' to '9223372036854775807 ' Setting '/proc/sys/fs/nr_open' to '2147483640 ' Couldn't write fs.nr_open as 2147483640, halving it. Setting '/proc/sys/fs/nr_open' to '1073741816 ' Successfully bumped fs.nr_open to 1073741816 The strange formatting is because we explicitly appended a newline in those two places. It seems that the kernel doesn't care. In fact, we have a few dozen other writes to sysctl where we don't append a newline. So let's just drop those here too, to make the code a bit simpler and avoid strange output in the logs. --- src/core/main.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/core/main.c b/src/core/main.c index 119c518664..ffe5c1688a 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -1184,9 +1184,10 @@ static void bump_file_max_and_nr_open(void) { #if BUMP_PROC_SYS_FS_FILE_MAX /* The maximum the kernel allows for this since 5.2 is LONG_MAX, use that. (Previously things were * different, but the operation would fail silently.) */ - r = sysctl_writef("fs/file-max", "%li\n", LONG_MAX); + r = sysctl_writef("fs/file-max", "%li", LONG_MAX); if (r < 0) - log_full_errno(IN_SET(r, -EROFS, -EPERM, -EACCES) ? LOG_DEBUG : LOG_WARNING, r, "Failed to bump fs.file-max, ignoring: %m"); + log_full_errno(IN_SET(r, -EROFS, -EPERM, -EACCES) ? LOG_DEBUG : LOG_WARNING, + r, "Failed to bump fs.file-max, ignoring: %m"); #endif #if BUMP_PROC_SYS_FS_NR_OPEN @@ -1218,7 +1219,7 @@ static void bump_file_max_and_nr_open(void) { break; } - r = sysctl_writef("fs/nr_open", "%i\n", v); + r = sysctl_writef("fs/nr_open", "%i", v); if (r == -EINVAL) { log_debug("Couldn't write fs.nr_open as %i, halving it.", v); v /= 2; From a5fac1df2743e6407b23d22dbcbf7a25deddbb5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Sat, 3 Dec 2022 11:55:55 +0100 Subject: [PATCH 2/3] manager: define a string constant for LONG_MAX and use that for sysctl This moves the formatting of the constant to compilation time and let's us avoid asprintf() in the very hot path of initial boot. --- meson.build | 4 ++++ src/core/main.c | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/meson.build b/meson.build index 5706c776ff..504644bca8 100644 --- a/meson.build +++ b/meson.build @@ -510,6 +510,10 @@ conf.set('SIZEOF_TIME_T', cc.sizeof('time_t', prefix : '#include ')) conf.set('SIZEOF_RLIM_T', cc.sizeof('rlim_t', prefix : '#include ')) conf.set('SIZEOF_TIMEX_MEMBER', cc.sizeof('typeof(((struct timex *)0)->freq)', prefix : '#include ')) +long_max = cc.compute_int('LONG_MAX', prefix : '#include ') +assert(long_max > 100000) +conf.set_quoted('LONG_MAX_STR', '@0@'.format(long_max)) + decl_headers = ''' #include #include diff --git a/src/core/main.c b/src/core/main.c index ffe5c1688a..df83a1d2d4 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -1184,7 +1184,7 @@ static void bump_file_max_and_nr_open(void) { #if BUMP_PROC_SYS_FS_FILE_MAX /* The maximum the kernel allows for this since 5.2 is LONG_MAX, use that. (Previously things were * different, but the operation would fail silently.) */ - r = sysctl_writef("fs/file-max", "%li", LONG_MAX); + r = sysctl_write("fs/file-max", LONG_MAX_STR); if (r < 0) log_full_errno(IN_SET(r, -EROFS, -EPERM, -EACCES) ? LOG_DEBUG : LOG_WARNING, r, "Failed to bump fs.file-max, ignoring: %m"); From 27c8ca439fcf313cb6839e77ae988b231b188c58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Sat, 3 Dec 2022 11:57:17 +0100 Subject: [PATCH 3/3] manager: write net/unix/max_dgram_qlen sysctl as fixed string --- src/basic/constants.h | 2 +- src/core/main.c | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/basic/constants.h b/src/basic/constants.h index 54021911ab..4a24ba9c8c 100644 --- a/src/basic/constants.h +++ b/src/basic/constants.h @@ -43,7 +43,7 @@ #define DEFAULT_EXIT_USEC (30*USEC_PER_SEC) /* The default value for the net.unix.max_dgram_qlen sysctl */ -#define DEFAULT_UNIX_MAX_DGRAM_QLEN 512UL +#define DEFAULT_UNIX_MAX_DGRAM_QLEN 512 #define SIGNALS_CRASH_HANDLER SIGSEGV,SIGILL,SIGFPE,SIGBUS,SIGQUIT,SIGABRT #define SIGNALS_IGNORE SIGPIPE diff --git a/src/core/main.c b/src/core/main.c index df83a1d2d4..9c1de3624c 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -1405,8 +1405,7 @@ static int bump_unix_max_dgram_qlen(void) { if (v >= DEFAULT_UNIX_MAX_DGRAM_QLEN) return 0; - r = write_string_filef("/proc/sys/net/unix/max_dgram_qlen", WRITE_STRING_FILE_DISABLE_BUFFER, - "%lu", DEFAULT_UNIX_MAX_DGRAM_QLEN); + r = sysctl_write("net/unix/max_dgram_qlen", STRINGIFY(DEFAULT_UNIX_MAX_DGRAM_QLEN)); if (r < 0) return log_full_errno(IN_SET(r, -EROFS, -EPERM, -EACCES) ? LOG_DEBUG : LOG_WARNING, r, "Failed to bump AF_UNIX datagram queue length, ignoring: %m");