Commit a38dc905 for xz

commit a38dc905e20b29d2f59cadf4c67557d8807ec070
Author: Lasse Collin <lasse.collin@tukaani.org>
Date:   Sun Jul 19 19:10:58 2026 +0300

    tuklib_physmem/cpucores: Support Linux cgroup v2 memory.max and cpu.max

    If cgroup v2 memory.max is lower than the amount of physical memory,
    tuklib_physmem() now returns the effective memory.max value. This
    feels reasonable because memory.max triggers OOM killer just like
    running out of real RAM does, and memory.max is shared by all processes
    in the same cgroup.

    This fixes a similar issue that occurred with resource limits. See
    the commit e8838b2f5922 ("xz: Look at resource limits when determining
    the default memlimit"). That change was done in src/xz/hardware.c, not
    tuklib_physmem.c, because RLIMIT_DATA and RLIMIT_AS are per-process
    limits and thus xz's memory usage can safely grow fairly close to those
    limits.

    Similarly, check for cpu.max in tuklib_cpucores(). There's no point
    in creating more threads by default than what can effectively be used.

    Thanks-to: Daniel Robbins <drobbins@kernel-seeds.org>
    Fixes: https://github.com/tukaani-project/xz/issues/234

diff --git a/CMakeLists.txt b/CMakeLists.txt
index b0894e75..38eff44e 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -614,6 +614,8 @@ set(LIBLZMA_API_HEADERS
 add_library(liblzma
     src/common/mythread.h
     src/common/sysdefs.h
+    src/common/tuklib_cgroup.c
+    src/common/tuklib_cgroup.h
     src/common/tuklib_common.h
     src/common/tuklib_config.h
     src/common/tuklib_integer.h
diff --git a/src/common/tuklib_cgroup.c b/src/common/tuklib_cgroup.c
new file mode 100644
index 00000000..a24c2630
--- /dev/null
+++ b/src/common/tuklib_cgroup.c
@@ -0,0 +1,193 @@
+// SPDX-License-Identifier: 0BSD
+
+///////////////////////////////////////////////////////////////////////////////
+//
+/// \file       tuklib_cgroup.c
+/// \brief      Read Linux cgroup v2 limits
+//
+//  Author:     Lasse Collin
+//
+///////////////////////////////////////////////////////////////////////////////
+
+#include "tuklib_cgroup.h"
+
+#ifndef __linux__
+
+// Prevent an empty translation unit.
+typedef int dummy;
+
+#else
+
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define SYS_FS_CGROUP "/sys/fs/cgroup"
+#define SYS_FS_CGROUP_LEN (sizeof(SYS_FS_CGROUP) - 1)
+
+
+static size_t
+get_cgroup_path(char buf[PATH_MAX])
+{
+	// Find the pathname of the control group of this process and store it
+	// in buf with a trailing slash. Only cgroup v2 is supported.
+	//
+	// Syscall counts with glibc 2.43:
+	//     fopen + fread + fclose = 5: openat + fstat + read + read + close
+	//     fopen + fgets + fclose = 4: openat + fstat + read + close
+	//     open + read + close = 3:    openat + read + close
+	//
+	// Use open + read + close to minimize the number of syscalls.
+	// Partial reads or EINTR shouldn't occur with proc or cgroup2 fs.
+	int fd = open("/proc/self/cgroup", O_RDONLY);
+	if (fd == -1)
+		return 0;
+
+	// Read the data from fd into an offset so that we can insert
+	// the "/sys/fs/cgroup" prefix. Reserve one byte of space so
+	// that we can append \0. The \n may be overwritten with "/".
+	//     buf after read(): "...........0::/foo/bar\n"  (without \0)
+	//     final buf:        "/sys/fs/cgroup/foo/bar/\0"
+	const size_t offset = SYS_FS_CGROUP_LEN - 3; // 3 == strlen("0::")
+	ssize_t read_size = read(fd, buf + offset, PATH_MAX - offset - 1);
+	(void)close(fd);
+
+	// With cgroup v2, the file should begin with "0::/" (4 bytes)
+	// and there should be "\n" (1 byte) that terminates the line.
+	if (read_size < 4 + 1 || memcmp(buf + offset, "0::/", 4) != 0)
+		return 0;
+
+	// Add the "/sys/fs/cgroup" prefix, overwriting "0::".
+	memcpy(buf, SYS_FS_CGROUP, SYS_FS_CGROUP_LEN);
+
+	// Terminate the string so that we can use strcspn().
+	// We reserved space for this in the read() call.
+	buf[offset + (size_t)read_size] = '\0';
+
+	// Find the length of the line. If \n isn't found, either the line
+	// didn't fit into buf or there is an unexpected \0 before \n.
+	size_t len = strcspn(buf, "\n");
+	if (buf[len] != '\n')
+		return 0;
+
+	// Replace the \n with a slash except if the previous char is a slash.
+	// Double-slash would make the calling function process the same
+	// directory twice.
+	if (buf[len - 1] != '/')
+		buf[len++] = '/';
+
+	// Terminate the line. We reserved space for this in the read() call.
+	buf[len] = '\0';
+
+	// Reject the pathname if there might be a path traversal problem.
+	// This shouldn't happen with /proc/self/cgroup, but check it anyway.
+	if (strstr(buf, "/../") != NULL)
+		return 0;
+
+	return len;
+}
+
+
+static unsigned long long
+get_cgroup_limit(const char *filename,
+		unsigned long long (*parse)(const char *str))
+{
+	// Get the control group pathname and its length. The path will
+	// have a trailing slash after which the filename can be appended.
+	char buf[PATH_MAX];
+	size_t len = get_cgroup_path(buf);
+	if (len == 0)
+		return ULLONG_MAX;
+
+	// Check that there is space to append the filename, including the \0.
+	const size_t filename_size = strlen(filename) + 1;
+	if (len + filename_size > sizeof(buf))
+		return ULLONG_MAX;
+
+	// Read the file from the cgroup and from its parent cgroups. Set ret
+	// to the lowest value found or ULLONG_MAX if there are no limits.
+	unsigned long long ret = ULLONG_MAX;
+	while (len > SYS_FS_CGROUP_LEN) {
+		// Append the filename (including \0) after
+		// the directory separator (slash) in buf.
+		memcpy(buf + len, filename, filename_size);
+
+		// The file should exist in all non-root cgroups.
+		// If opening it fails, don't waste time looking at
+		// parent directories.
+		const int fd = open(buf, O_RDONLY);
+		if (fd == -1)
+			break;
+
+		// The file should contain one short line.
+		char line[64];
+		const ssize_t read_size = read(fd, line, sizeof(line));
+		(void)close(fd);
+
+		if (read_size > 0 && line[read_size - 1] == '\n') {
+			line[read_size - 1] = '\0';
+			const unsigned long long value = parse(line);
+			if (ret > value)
+				ret = value;
+		}
+
+		// Continue from the parent directory.
+		while (buf[--len - 1] != '/') ;
+	}
+
+	return ret;
+}
+
+
+static unsigned long long
+parse_memory_max(const char *str)
+{
+	// memory.max should contain either an unsigned base-10 integer
+	// or "max". Zero isn't a sensible memory.max value, and it's
+	// fine if values larger than ULLONG_MAX become ULLONG_MAX.
+	// This allows simplifying strtoull() error checking.
+	char *end;
+	const unsigned long long value = strtoull(str, &end, 10);
+	return value > 0 && *end == '\0' ? value : ULLONG_MAX;
+}
+
+
+static unsigned long long
+parse_cpu_max(const char *str)
+{
+	// cpu.max should contain a space-separated pair "MAX PERIOD"
+	// where MAX is either "max" or an unsigned base-10 integer,
+	// and PERIOD is an unsigned base-10 integer. Zeros aren't
+	// sensible values in cpu.max.
+	//
+	// Return the number of threads that can be utilized at 100 %,
+	// but don't return 0 (return at least 1).
+	unsigned long long ret = ULLONG_MAX;
+	char *end;
+	const unsigned long long max = strtoull(str, &end, 10);
+	if (max > 0 && *end == ' ') {
+		const unsigned long long period = strtoull(end, &end, 10);
+		if (period > 0 && *end == '\0')
+			ret = max > period ? max / period : 1;
+	}
+
+	return ret;
+}
+
+
+extern unsigned long long
+tuklib_cgroup_memory_max(void)
+{
+	return get_cgroup_limit("memory.max", &parse_memory_max);
+}
+
+
+extern unsigned long long
+tuklib_cgroup_cpu_max(void)
+{
+	return get_cgroup_limit("cpu.max", &parse_cpu_max);
+}
+
+#endif
diff --git a/src/common/tuklib_cgroup.h b/src/common/tuklib_cgroup.h
new file mode 100644
index 00000000..1bb01007
--- /dev/null
+++ b/src/common/tuklib_cgroup.h
@@ -0,0 +1,41 @@
+// SPDX-License-Identifier: 0BSD
+
+///////////////////////////////////////////////////////////////////////////////
+//
+/// \file       tuklib_cgroup.h
+/// \brief      Read Linux cgroup v2 limits
+///
+/// These are only available on Linux.
+//
+//  Author:     Lasse Collin
+//
+///////////////////////////////////////////////////////////////////////////////
+
+#ifndef TUKLIB_CGROUP_H
+#define TUKLIB_CGROUP_H
+
+#include "tuklib_common.h"
+#ifdef __linux__
+TUKLIB_DECLS_BEGIN
+
+#define tuklib_cgroup_memory_max TUKLIB_SYMBOL(tuklib_cgroup_memory_max)
+extern unsigned long long tuklib_cgroup_memory_max(void);
+///<
+/// \brief      Linux only: Get the effective cgroup v2 memory.max
+///
+/// \return     The effective cgroup v2 memory.max as bytes, or ULLONG_MAX
+///             if there is no limit or the lookup fails for any reason.
+
+#define tuklib_cgroup_cpu_max TUKLIB_SYMBOL(tuklib_cgroup_cpu_max)
+extern unsigned long long tuklib_cgroup_cpu_max(void);
+///<
+/// \brief      Linux only: Get the effective cgroup v2 cpu.max
+///
+/// \return     The effective cgroup v2 cpu.max as a number of cores that can
+///             be fully utilized without being throttled or at least 1 (in
+///             case cpu.max is less than 100 % of one core). ULLONG_MAX is
+///             returned if there is no limit or the lookup fails.
+
+TUKLIB_DECLS_END
+#endif
+#endif
diff --git a/src/common/tuklib_cpucores.c b/src/common/tuklib_cpucores.c
index c4a781ac..2fa37e76 100644
--- a/src/common/tuklib_cpucores.c
+++ b/src/common/tuklib_cpucores.c
@@ -10,6 +10,7 @@
 ///////////////////////////////////////////////////////////////////////////////

 #include "tuklib_cpucores.h"
+#include "tuklib_cgroup.h"

 #if defined(_WIN32) || defined(__CYGWIN__)
 #	ifndef _WIN32_WINNT
@@ -104,5 +105,15 @@ tuklib_cpucores(void)
 		ret = (uint32_t)pst.psd_proc_cnt;
 #endif

+#ifdef __linux__
+	// Find the effective value of Linux cgroup v2 cpu.max.
+	// If it is lower than the number of processors online,
+	// pretend that we only have the number of cores that
+	// cpu.max allows us to actually use.
+	const unsigned long long cpu_max = tuklib_cgroup_cpu_max();
+	if (ret > cpu_max)
+		ret = cpu_max;
+#endif
+
 	return ret;
 }
diff --git a/src/common/tuklib_cpucores.h b/src/common/tuklib_cpucores.h
index edff9395..d9d099b3 100644
--- a/src/common/tuklib_cpucores.h
+++ b/src/common/tuklib_cpucores.h
@@ -4,6 +4,8 @@
 //
 /// \file       tuklib_cpucores.h
 /// \brief      Get the number of CPU cores online
+///
+/// This depends on tuklib_cgroup.c.
 //
 //  Author:     Lasse Collin
 //
diff --git a/src/common/tuklib_physmem.c b/src/common/tuklib_physmem.c
index 5988ba77..34c9f813 100644
--- a/src/common/tuklib_physmem.c
+++ b/src/common/tuklib_physmem.c
@@ -10,6 +10,7 @@
 ///////////////////////////////////////////////////////////////////////////////

 #include "tuklib_physmem.h"
+#include "tuklib_cgroup.h"

 // We want to use Windows-specific code on Cygwin, which also has memory
 // information available via sysconf(), but on Cygwin 1.5 and older it
@@ -220,5 +221,14 @@ tuklib_physmem(void)
 		ret = (uint64_t)si.totalram * si.mem_unit;
 #endif

+#ifdef __linux__
+	// Find the effective value of Linux cgroup v2 memory.max.
+	// If it is lower than the physical amount of RAM, pretend
+	// that we only have memory.max bytes of RAM.
+	const unsigned long long memory_max = tuklib_cgroup_memory_max();
+	if (ret > memory_max)
+		ret = memory_max;
+#endif
+
 	return ret;
 }
diff --git a/src/common/tuklib_physmem.h b/src/common/tuklib_physmem.h
index f35bfbab..29822f9a 100644
--- a/src/common/tuklib_physmem.h
+++ b/src/common/tuklib_physmem.h
@@ -4,6 +4,8 @@
 //
 /// \file       tuklib_physmem.h
 /// \brief      Get the amount of physical memory
+///
+/// This depends on tuklib_cgroup.c.
 //
 //  Author:     Lasse Collin
 //
@@ -20,6 +22,9 @@ extern uint64_t tuklib_physmem(void);
 ///<
 /// \brief      Get the amount of physical memory in bytes
 ///
+/// On Linux, if cgroup v2 memory.max is lower than the amount of physical
+/// memory, the memory.max value is returned.
+///
 /// \return     Amount of physical memory in bytes. On error, zero is
 ///             returned.

diff --git a/src/liblzma/Makefile.am b/src/liblzma/Makefile.am
index 5d51069a..fb4925a2 100644
--- a/src/liblzma/Makefile.am
+++ b/src/liblzma/Makefile.am
@@ -34,7 +34,9 @@ liblzma_la_LDFLAGS += \
 	-Wl,--version-script=$(top_srcdir)/src/liblzma/liblzma_linux.map
 endif

-liblzma_la_SOURCES += ../common/tuklib_physmem.c
+liblzma_la_SOURCES += \
+	../common/tuklib_physmem.c \
+	../common/tuklib_cgroup.c

 if COND_THREADS
 liblzma_la_SOURCES += ../common/tuklib_cpucores.c