]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/mmap-cache.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include "alloc-util.h"
13 #include "mmap-cache.h"
17 typedef struct Window Window
;
18 typedef struct Context Context
;
32 MMapFileDescriptor
*fd
;
34 LIST_FIELDS(Window
, by_fd
);
35 LIST_FIELDS(Window
, unused
);
37 LIST_HEAD(Context
, contexts
);
45 LIST_FIELDS(Context
, by_window
);
48 struct MMapFileDescriptor
{
52 LIST_HEAD(Window
, windows
);
59 unsigned n_hit
, n_missed
;
62 Context
*contexts
[MMAP_CACHE_MAX_CONTEXTS
];
64 LIST_HEAD(Window
, unused
);
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
) {
118 /* Replace the window with anonymous pages. This is useful
119 * when we hit a SIGBUS and want to make sure the file cannot
120 * trigger any further SIGBUS, possibly overrunning the sigbus
123 assert_se(mmap(w
->ptr
, w
->size
, w
->prot
, MAP_PRIVATE
|MAP_ANONYMOUS
|MAP_FIXED
, -1, 0) == w
->ptr
);
124 w
->invalidated
= true;
127 static void window_free(Window
*w
) {
131 w
->cache
->n_windows
--;
135 _pure_
static inline bool window_matches(Window
*w
, int prot
, 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
, int prot
, uint64_t offset
, size_t size
) {
151 f
->fd
== w
->fd
->fd
&&
152 window_matches(w
, prot
, offset
, size
);
155 static Window
*window_add(MMapCache
*m
, MMapFileDescriptor
*f
, int prot
, bool keep_always
, uint64_t offset
, size_t size
, void *ptr
) {
161 if (!m
->last_unused
|| m
->n_windows
<= WINDOWS_MIN
) {
163 /* Allocate a new window */
170 /* Reuse an existing one */
179 w
->keep_always
= keep_always
;
184 LIST_PREPEND(by_fd
, f
->windows
, w
);
189 static void context_detach_window(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
, c
->cache
->unused
, w
);
208 if (!c
->cache
->last_unused
)
209 c
->cache
->last_unused
= w
;
216 static void context_attach_window(Context
*c
, Window
*w
) {
223 context_detach_window(c
);
227 LIST_REMOVE(unused
, c
->cache
->unused
, w
);
228 if (c
->cache
->last_unused
== w
)
229 c
->cache
->last_unused
= w
->unused_prev
;
231 w
->in_unused
= false;
235 LIST_PREPEND(by_window
, w
->contexts
, c
);
238 static Context
*context_add(MMapCache
*m
, unsigned id
) {
247 c
= new0(Context
, 1);
254 assert(!m
->contexts
[id
]);
260 static void context_free(Context
*c
) {
263 context_detach_window(c
);
266 assert(c
->cache
->contexts
[c
->id
] == c
);
267 c
->cache
->contexts
[c
->id
] = NULL
;
273 static MMapCache
*mmap_cache_free(MMapCache
*m
) {
278 for (i
= 0; i
< MMAP_CACHE_MAX_CONTEXTS
; i
++)
280 context_free(m
->contexts
[i
]);
282 hashmap_free(m
->fds
);
285 window_free(m
->unused
);
290 DEFINE_TRIVIAL_REF_UNREF_FUNC(MMapCache
, mmap_cache
, mmap_cache_free
);
292 static int make_room(MMapCache
*m
) {
298 window_free(m
->last_unused
);
302 static int try_context(
304 MMapFileDescriptor
*f
,
316 assert(m
->n_ref
> 0);
321 c
= m
->contexts
[context
];
325 assert(c
->id
== context
);
330 if (!window_matches_fd(c
->window
, f
, prot
, offset
, size
)) {
332 /* Drop the reference to the window, since it's unnecessary now */
333 context_detach_window(c
);
337 if (c
->window
->fd
->sigbus
)
340 c
->window
->keep_always
= c
->window
->keep_always
|| keep_always
;
342 *ret
= (uint8_t*) c
->window
->ptr
+ (offset
- c
->window
->offset
);
344 *ret_size
= c
->window
->size
- (offset
- c
->window
->offset
);
349 static int find_mmap(
351 MMapFileDescriptor
*f
,
364 assert(m
->n_ref
> 0);
371 LIST_FOREACH(by_fd
, w
, f
->windows
)
372 if (window_matches(w
, prot
, offset
, size
))
378 c
= context_add(m
, context
);
382 context_attach_window(c
, w
);
383 w
->keep_always
= w
->keep_always
|| keep_always
;
385 *ret
= (uint8_t*) w
->ptr
+ (offset
- w
->offset
);
387 *ret_size
= w
->size
- (offset
- w
->offset
);
392 static int mmap_try_harder(MMapCache
*m
, void *addr
, MMapFileDescriptor
*f
, int prot
, int flags
, uint64_t offset
, size_t size
, void **res
) {
402 ptr
= mmap(addr
, size
, prot
, flags
, f
->fd
, offset
);
403 if (ptr
!= MAP_FAILED
)
406 return negative_errno();
421 MMapFileDescriptor
*f
,
431 uint64_t woffset
, wsize
;
438 assert(m
->n_ref
> 0);
443 woffset
= offset
& ~((uint64_t) page_size() - 1ULL);
444 wsize
= size
+ (offset
- woffset
);
445 wsize
= PAGE_ALIGN(wsize
);
447 if (wsize
< WINDOW_SIZE
) {
450 delta
= PAGE_ALIGN((WINDOW_SIZE
- wsize
) / 2);
461 /* Memory maps that are larger then the files
462 underneath have undefined behavior. Hence, clamp
463 things to the file size if we know it */
465 if (woffset
>= (uint64_t) st
->st_size
)
466 return -EADDRNOTAVAIL
;
468 if (woffset
+ wsize
> (uint64_t) st
->st_size
)
469 wsize
= PAGE_ALIGN(st
->st_size
- woffset
);
472 r
= mmap_try_harder(m
, NULL
, f
, prot
, MAP_SHARED
, woffset
, wsize
, &d
);
476 c
= context_add(m
, context
);
480 w
= window_add(m
, f
, prot
, keep_always
, woffset
, wsize
, d
);
484 context_attach_window(c
, w
);
486 *ret
= (uint8_t*) w
->ptr
+ (offset
- w
->offset
);
488 *ret_size
= w
->size
- (offset
- w
->offset
);
493 (void) munmap(d
, wsize
);
499 MMapFileDescriptor
*f
,
512 assert(m
->n_ref
> 0);
516 assert(context
< MMAP_CACHE_MAX_CONTEXTS
);
518 /* Check whether the current context is the right one already */
519 r
= try_context(m
, f
, prot
, context
, keep_always
, offset
, size
, ret
, ret_size
);
525 /* Search for a matching mmap */
526 r
= find_mmap(m
, f
, prot
, context
, keep_always
, offset
, size
, ret
, ret_size
);
534 /* Create a new mmap */
535 return add_mmap(m
, f
, prot
, context
, keep_always
, offset
, size
, st
, ret
, ret_size
);
538 unsigned mmap_cache_get_hit(MMapCache
*m
) {
544 unsigned mmap_cache_get_missed(MMapCache
*m
) {
550 static void mmap_cache_process_sigbus(MMapCache
*m
) {
552 MMapFileDescriptor
*f
;
558 /* Iterate through all triggered pages and mark their files as
564 r
= sigbus_pop(&addr
);
565 if (_likely_(r
== 0))
568 log_error_errno(r
, "SIGBUS handling failed: %m");
573 HASHMAP_FOREACH(f
, m
->fds
, i
) {
576 LIST_FOREACH(by_fd
, w
, f
->windows
) {
577 if ((uint8_t*) addr
>= (uint8_t*) w
->ptr
&&
578 (uint8_t*) addr
< (uint8_t*) w
->ptr
+ w
->size
) {
579 found
= ours
= f
->sigbus
= true;
588 /* Didn't find a matching window, give up */
590 log_error("Unknown SIGBUS page, aborting.");
595 /* The list of triggered pages is now empty. Now, let's remap
596 * all windows of the triggered file to anonymous maps, so
597 * that no page of the file in question is triggered again, so
598 * that we can be sure not to hit the queue size limit. */
599 if (_likely_(!found
))
602 HASHMAP_FOREACH(f
, m
->fds
, i
) {
608 LIST_FOREACH(by_fd
, w
, f
->windows
)
609 window_invalidate(w
);
613 bool mmap_cache_got_sigbus(MMapCache
*m
, MMapFileDescriptor
*f
) {
617 mmap_cache_process_sigbus(m
);
622 MMapFileDescriptor
* mmap_cache_add_fd(MMapCache
*m
, int fd
) {
623 MMapFileDescriptor
*f
;
629 f
= hashmap_get(m
->fds
, FD_TO_PTR(fd
));
633 r
= hashmap_ensure_allocated(&m
->fds
, NULL
);
637 f
= new0(MMapFileDescriptor
, 1);
644 r
= hashmap_put(m
->fds
, FD_TO_PTR(fd
), f
);
651 void mmap_cache_free_fd(MMapCache
*m
, MMapFileDescriptor
*f
) {
655 /* Make sure that any queued SIGBUS are first dispatched, so
656 * that we don't end up with a SIGBUS entry we cannot relate
657 * to any existing memory map */
659 mmap_cache_process_sigbus(m
);
662 window_free(f
->windows
);
665 assert_se(hashmap_remove(f
->cache
->fds
, FD_TO_PTR(f
->fd
)));