coredump: check cgroups memory limit if storing on tmpfs
authorLuca Boccassi <luca.boccassi@microsoft.com>
Fri, 12 Mar 2021 20:17:09 +0000 (20:17 +0000)
committerLuca Boccassi <luca.boccassi@microsoft.com>
Tue, 8 Jun 2021 13:05:56 +0000 (14:05 +0100)
commit587f2a5e564cf434c2e0a653f52b8f73e86092d8
tree95d74d99a9e10e95ca19cdcb7f4e124ede36b57d
parent5b6f8e13ad26673e29d7aaad010d14261f247c89
coredump: check cgroups memory limit if storing on tmpfs

When /var/lib/systemd/coredump/ is backed by a tmpfs, all disk usage
will be accounted under the systemd-coredump process cgroup memory
limit.
If MemoryMax is set, this might cause systemd-coredump to be terminated
by the kernel oom handler when writing large uncompressed core files,
even if the compressed core would fit within the limits.

Detect if a tmpfs is used, and if so check MemoryMax from the process
and slice cgroups, and do not write uncompressed core files that are
greater than half the available memory. If the limit is breached,
stop writing and compress the written chunk immediately, then delete
the uncompressed chunk to free more memory, and resume compressing
directly from STDIN.

Example debug log when this situation happens:

systemd-coredump[737455]: Setting max_size to limit writes to 51344896 bytes.
systemd-coredump[737455]: ZSTD compression finished (51344896 -> 3260 bytes, 0.0%)
systemd-coredump[737455]: ZSTD compression finished (1022786048 -> 47245 bytes, 0.0%)
systemd-coredump[737455]: Process 737445 (a.out) of user 1000 dumped core.
man/coredump.conf.xml
src/coredump/coredump.c