diff options
author | Juston Li <justonli@google.com> | 2023-03-23 13:06:55 -0700 |
---|---|---|
committer | Marge Bot <emma+marge@anholt.net> | 2023-03-30 01:09:10 +0000 |
commit | 88d074cb8f3879b4fb1e3574038f9355628a8f66 (patch) | |
tree | 75badd51c2d6c27ddba336f410ee62deee878927 | |
parent | ba670f0cdfd99bb3c0f46b111191388d324a0f35 (diff) | |
download | mesa-88d074cb8f3879b4fb1e3574038f9355628a8f66.tar.gz |
util/disk_cache: use posix_fallocate() for index files
ftruncate() allocates disk space lazily. If the disk is full and it is
unable to allocate disk space when accesed via mmap(), it will crash
with a SIGBUS.
Switch to posix_fallocate(), which ensures disk space is allocated
otherwise it fails if there isn't enough disk space. The disk cache
won't be enabled in this case.
For normal cases, a small increase in disk usage as the 1.3MB index
file will be fully allocated when initialized now.
fallback to ftruncate() if posix_fallocate() isn't found.
Signed-off-by: Juston Li <justonli@google.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/22097>
-rw-r--r-- | meson.build | 1 | ||||
-rw-r--r-- | src/util/disk_cache_os.c | 12 |
2 files changed, 13 insertions, 0 deletions
diff --git a/meson.build b/meson.build index c99aaed797f..a09ad156dd2 100644 --- a/meson.build +++ b/meson.build @@ -1277,6 +1277,7 @@ functions_to_detect = { 'strtok_r': '', 'getrandom': '', 'qsort_s': '', + 'posix_fallocate': '', } foreach f, prefix: functions_to_detect diff --git a/src/util/disk_cache_os.c b/src/util/disk_cache_os.c index 27a13926059..e3596f2b4b0 100644 --- a/src/util/disk_cache_os.c +++ b/src/util/disk_cache_os.c @@ -1019,8 +1019,20 @@ disk_cache_mmap_cache_index(void *mem_ctx, struct disk_cache *cache, /* Force the index file to be the expected size. */ size_t size = sizeof(*cache->size) + CACHE_INDEX_MAX_KEYS * CACHE_KEY_SIZE; if (sb.st_size != size) { +#if HAVE_POSIX_FALLOCATE + /* posix_fallocate() ensures disk space is allocated otherwise it + * fails if there is not enough space on the disk. + */ + if (posix_fallocate(fd, 0, size) != 0) + goto path_fail; +#else + /* ftruncate() allocates disk space lazily. If the disk is full + * and it is unable to allocate disk space when accessed via + * mmap, it will crash with a SIGBUS. + */ if (ftruncate(fd, size) == -1) goto path_fail; +#endif } /* We map this shared so that other processes see updates that we |