Skip to content

Commit 839a2b9

Browse files
committed
coredump: correctly take tmpfs size into account for compression
We calculate the amount of uncompressed data we can write by taking the limits into account and halving it to ensure there's room for switching to compression on the fly when storing cores on a tmpfs (eg: due read-only rootfs). But the logic is flawed, as taking into account the size of the tmpfs storage was applied after the halving, so in practice when an uncompressed core file was larger than the tmpfs, we fill it and then fail. Rearrange the logic so that the halving is done after taking into account the tmpfs size. (cherry picked from commit e6b2508) (cherry picked from commit a946258) (cherry picked from commit 3dacca1) (cherry picked from commit 523f91c) (cherry picked from commit d427f8a)
1 parent d0bace3 commit 839a2b9

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/coredump/coredump.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -560,17 +560,21 @@ static int save_external_coredump(
560560
bus_error_message(&error, r));
561561
}
562562

563+
/* First, ensure we are not going to go over the cgroup limit */
563564
max_size = MIN(cgroup_limit, max_size);
564-
max_size = LESS_BY(max_size, 1024U) / 2; /* Account for 1KB metadata overhead for compressing */
565-
max_size = MAX(PROCESS_SIZE_MIN, max_size); /* Impose a lower minimum */
566-
567-
/* tmpfs might get full quickly, so check the available space too.
568-
* But don't worry about errors here, failing to access the storage
569-
* location will be better logged when writing to it. */
565+
/* tmpfs might get full quickly, so check the available space too. But don't worry about
566+
* errors here, failing to access the storage location will be better logged when writing to
567+
* it. */
570568
if (statvfs("/var/lib/systemd/coredump/", &sv) >= 0)
571569
max_size = MIN((uint64_t)sv.f_frsize * (uint64_t)sv.f_bfree, max_size);
572-
573-
log_debug("Limiting core file size to %" PRIu64 " bytes due to cgroup memory limits.", max_size);
570+
/* Impose a lower minimum, otherwise we will miss the basic headers. */
571+
max_size = MAX(PROCESS_SIZE_MIN, max_size);
572+
/* Ensure we can always switch to compressing on the fly in case we are running out of space
573+
* by keeping half of the space/memory available, plus 1KB metadata overhead from the
574+
* compression algorithm. */
575+
max_size = LESS_BY(max_size, 1024U) / 2;
576+
577+
log_debug("Limiting core file size to %" PRIu64 " bytes due to cgroup and/or filesystem limits.", max_size);
574578
}
575579

576580
r = copy_bytes(input_fd, fd, max_size, 0);

0 commit comments

Comments
 (0)