]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-journal/mmap-cache.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
7 #include "alloc-util.h"
8 #include "errno-util.h"
14 #include "memory-util.h"
15 #include "mmap-cache.h"
18 typedef struct Window Window
;
19 typedef struct Context Context
;
32 MMapFileDescriptor
*fd
;
34 LIST_FIELDS(Window
, by_fd
);
35 LIST_FIELDS(Window
, unused
);
37 LIST_HEAD(Context
, contexts
);
43 LIST_FIELDS(Context
, by_window
);
46 struct MMapFileDescriptor
{
51 LIST_HEAD(Window
, windows
);
58 unsigned n_context_cache_hit
, n_window_list_hit
, n_missed
;
62 LIST_HEAD(Window
, unused
);
65 Context contexts
[MMAP_CACHE_MAX_CONTEXTS
];
68 #define WINDOWS_MIN 64
70 #if ENABLE_DEBUG_MMAP_CACHE
71 /* Tiny windows increase mmap activity and the chance of exposing unsafe use. */
72 # define WINDOW_SIZE (page_size())
74 # define WINDOW_SIZE (8ULL*1024ULL*1024ULL)
77 MMapCache
* mmap_cache_new(void) {
80 m
= new0(MMapCache
, 1);
88 static void window_unlink(Window
*w
) {
94 munmap(w
->ptr
, w
->size
);
97 LIST_REMOVE(by_fd
, w
->fd
->windows
, w
);
100 if (w
->cache
->last_unused
== w
)
101 w
->cache
->last_unused
= w
->unused_prev
;
103 LIST_REMOVE(unused
, w
->cache
->unused
, w
);
106 LIST_FOREACH(by_window
, c
, w
->contexts
) {
107 assert(c
->window
== w
);
112 static void window_invalidate(Window
*w
) {
119 /* Replace the window with anonymous pages. This is useful
120 * when we hit a SIGBUS and want to make sure the file cannot
121 * trigger any further SIGBUS, possibly overrunning the sigbus
124 assert_se(mmap(w
->ptr
, w
->size
, w
->fd
->prot
, MAP_PRIVATE
|MAP_ANONYMOUS
|MAP_FIXED
, -1, 0) == w
->ptr
);
125 w
->invalidated
= true;
128 static void window_free(Window
*w
) {
132 w
->cache
->n_windows
--;
136 _pure_
static bool window_matches(Window
*w
, uint64_t offset
, size_t size
) {
141 offset
>= w
->offset
&&
142 offset
+ size
<= w
->offset
+ w
->size
;
145 _pure_
static bool window_matches_fd(Window
*w
, MMapFileDescriptor
*f
, uint64_t offset
, size_t size
) {
151 window_matches(w
, offset
, size
);
154 static Window
*window_add(MMapCache
*m
, MMapFileDescriptor
*f
, bool keep_always
, uint64_t offset
, size_t size
, void *ptr
) {
160 if (!m
->last_unused
|| m
->n_windows
<= WINDOWS_MIN
) {
162 /* Allocate a new window */
169 /* Reuse an existing one */
177 .keep_always
= keep_always
,
183 LIST_PREPEND(by_fd
, f
->windows
, w
);
188 static void context_detach_window(MMapCache
*m
, Context
*c
) {
197 w
= TAKE_PTR(c
->window
);
198 LIST_REMOVE(by_window
, w
->contexts
, c
);
200 if (!w
->contexts
&& !w
->keep_always
) {
201 /* Not used anymore? */
202 #if ENABLE_DEBUG_MMAP_CACHE
203 /* Unmap unused windows immediately to expose use-after-unmap
207 LIST_PREPEND(unused
, m
->unused
, w
);
216 static void context_attach_window(MMapCache
*m
, Context
*c
, Window
*w
) {
224 context_detach_window(m
, c
);
228 if (m
->last_unused
== w
)
229 m
->last_unused
= w
->unused_prev
;
230 LIST_REMOVE(unused
, m
->unused
, w
);
232 w
->in_unused
= false;
236 LIST_PREPEND(by_window
, w
->contexts
, c
);
239 static MMapCache
*mmap_cache_free(MMapCache
*m
) {
242 for (int i
= 0; i
< MMAP_CACHE_MAX_CONTEXTS
; i
++)
243 context_detach_window(m
, &m
->contexts
[i
]);
245 hashmap_free(m
->fds
);
248 window_free(m
->unused
);
253 DEFINE_TRIVIAL_REF_UNREF_FUNC(MMapCache
, mmap_cache
, mmap_cache_free
);
255 static int make_room(MMapCache
*m
) {
261 window_free(m
->last_unused
);
265 static int try_context(
266 MMapFileDescriptor
*f
,
275 assert(f
->cache
->n_ref
> 0);
283 if (!window_matches_fd(c
->window
, f
, offset
, size
)) {
285 /* Drop the reference to the window, since it's unnecessary now */
286 context_detach_window(f
->cache
, c
);
290 if (c
->window
->fd
->sigbus
)
293 c
->window
->keep_always
= c
->window
->keep_always
|| keep_always
;
295 *ret
= (uint8_t*) c
->window
->ptr
+ (offset
- c
->window
->offset
);
296 f
->cache
->n_context_cache_hit
++;
301 static int find_mmap(
302 MMapFileDescriptor
*f
,
313 assert(f
->cache
->n_ref
> 0);
320 LIST_FOREACH(by_fd
, w
, f
->windows
)
321 if (window_matches(w
, offset
, size
))
327 context_attach_window(f
->cache
, c
, w
);
328 w
->keep_always
= w
->keep_always
|| keep_always
;
330 *ret
= (uint8_t*) w
->ptr
+ (offset
- w
->offset
);
331 f
->cache
->n_window_list_hit
++;
336 static int mmap_try_harder(MMapFileDescriptor
*f
, void *addr
, int flags
, uint64_t offset
, size_t size
, void **res
) {
345 ptr
= mmap(addr
, size
, f
->prot
, flags
, f
->fd
, offset
);
346 if (ptr
!= MAP_FAILED
)
349 return negative_errno();
351 r
= make_room(f
->cache
);
363 MMapFileDescriptor
*f
,
371 uint64_t woffset
, wsize
;
378 assert(f
->cache
->n_ref
> 0);
383 woffset
= offset
& ~((uint64_t) page_size() - 1ULL);
384 wsize
= size
+ (offset
- woffset
);
385 wsize
= PAGE_ALIGN(wsize
);
387 if (wsize
< WINDOW_SIZE
) {
390 delta
= PAGE_ALIGN((WINDOW_SIZE
- wsize
) / 2);
401 /* Memory maps that are larger then the files
402 underneath have undefined behavior. Hence, clamp
403 things to the file size if we know it */
405 if (woffset
>= (uint64_t) st
->st_size
)
406 return -EADDRNOTAVAIL
;
408 if (woffset
+ wsize
> (uint64_t) st
->st_size
)
409 wsize
= PAGE_ALIGN(st
->st_size
- woffset
);
412 r
= mmap_try_harder(f
, NULL
, MAP_SHARED
, woffset
, wsize
, &d
);
416 w
= window_add(f
->cache
, f
, keep_always
, woffset
, wsize
, d
);
420 context_attach_window(f
->cache
, c
, w
);
422 *ret
= (uint8_t*) w
->ptr
+ (offset
- w
->offset
);
427 (void) munmap(d
, wsize
);
431 int mmap_cache_fd_get(
432 MMapFileDescriptor
*f
,
445 assert(f
->cache
->n_ref
> 0);
448 assert(context
< MMAP_CACHE_MAX_CONTEXTS
);
450 c
= &f
->cache
->contexts
[context
];
452 /* Check whether the current context is the right one already */
453 r
= try_context(f
, c
, keep_always
, offset
, size
, ret
);
457 /* Search for a matching mmap */
458 r
= find_mmap(f
, c
, keep_always
, offset
, size
, ret
);
462 f
->cache
->n_missed
++;
464 /* Create a new mmap */
465 return add_mmap(f
, c
, keep_always
, offset
, size
, st
, ret
);
468 void mmap_cache_stats_log_debug(MMapCache
*m
) {
471 log_debug("mmap cache statistics: %u context cache hit, %u window list hit, %u miss", m
->n_context_cache_hit
, m
->n_window_list_hit
, m
->n_missed
);
474 static void mmap_cache_process_sigbus(MMapCache
*m
) {
476 MMapFileDescriptor
*f
;
481 /* Iterate through all triggered pages and mark their files as
487 r
= sigbus_pop(&addr
);
488 if (_likely_(r
== 0))
491 log_error_errno(r
, "SIGBUS handling failed: %m");
496 HASHMAP_FOREACH(f
, m
->fds
) {
499 LIST_FOREACH(by_fd
, w
, f
->windows
) {
500 if ((uint8_t*) addr
>= (uint8_t*) w
->ptr
&&
501 (uint8_t*) addr
< (uint8_t*) w
->ptr
+ w
->size
) {
502 found
= ours
= f
->sigbus
= true;
511 /* Didn't find a matching window, give up */
513 log_error("Unknown SIGBUS page, aborting.");
518 /* The list of triggered pages is now empty. Now, let's remap
519 * all windows of the triggered file to anonymous maps, so
520 * that no page of the file in question is triggered again, so
521 * that we can be sure not to hit the queue size limit. */
522 if (_likely_(!found
))
525 HASHMAP_FOREACH(f
, m
->fds
) {
531 LIST_FOREACH(by_fd
, w
, f
->windows
)
532 window_invalidate(w
);
536 bool mmap_cache_fd_got_sigbus(MMapFileDescriptor
*f
) {
539 mmap_cache_process_sigbus(f
->cache
);
544 MMapFileDescriptor
* mmap_cache_add_fd(MMapCache
*m
, int fd
, int prot
) {
545 MMapFileDescriptor
*f
;
551 f
= hashmap_get(m
->fds
, FD_TO_PTR(fd
));
555 r
= hashmap_ensure_allocated(&m
->fds
, NULL
);
559 f
= new0(MMapFileDescriptor
, 1);
563 r
= hashmap_put(m
->fds
, FD_TO_PTR(fd
), f
);
567 f
->cache
= mmap_cache_ref(m
);
574 void mmap_cache_fd_free(MMapFileDescriptor
*f
) {
578 /* Make sure that any queued SIGBUS are first dispatched, so
579 * that we don't end up with a SIGBUS entry we cannot relate
580 * to any existing memory map */
582 mmap_cache_process_sigbus(f
->cache
);
585 window_free(f
->windows
);
588 assert_se(hashmap_remove(f
->cache
->fds
, FD_TO_PTR(f
->fd
)));
589 f
->cache
= mmap_cache_unref(f
->cache
);
595 MMapCache
* mmap_cache_fd_cache(MMapFileDescriptor
*f
) {