]>
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"
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
;
33 MMapFileDescriptor
*fd
;
35 LIST_FIELDS(Window
, by_fd
);
36 LIST_FIELDS(Window
, unused
);
38 LIST_HEAD(Context
, contexts
);
46 LIST_FIELDS(Context
, by_window
);
49 struct MMapFileDescriptor
{
53 LIST_HEAD(Window
, windows
);
60 unsigned n_hit
, n_missed
;
63 Context
*contexts
[MMAP_CACHE_MAX_CONTEXTS
];
65 LIST_HEAD(Window
, unused
);
69 #define WINDOWS_MIN 64
71 #if ENABLE_DEBUG_MMAP_CACHE
72 /* Tiny windows increase mmap activity and the chance of exposing unsafe use. */
73 # define WINDOW_SIZE (page_size())
75 # define WINDOW_SIZE (8ULL*1024ULL*1024ULL)
78 MMapCache
* mmap_cache_new(void) {
81 m
= new0(MMapCache
, 1);
89 static void window_unlink(Window
*w
) {
95 munmap(w
->ptr
, w
->size
);
98 LIST_REMOVE(by_fd
, w
->fd
->windows
, w
);
101 if (w
->cache
->last_unused
== w
)
102 w
->cache
->last_unused
= w
->unused_prev
;
104 LIST_REMOVE(unused
, w
->cache
->unused
, w
);
107 LIST_FOREACH(by_window
, c
, w
->contexts
) {
108 assert(c
->window
== w
);
113 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
->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
, int prot
, uint64_t offset
, size_t size
) {
142 offset
>= w
->offset
&&
143 offset
+ size
<= w
->offset
+ w
->size
;
146 _pure_
static bool window_matches_fd(Window
*w
, MMapFileDescriptor
*f
, int prot
, uint64_t offset
, size_t size
) {
152 f
->fd
== w
->fd
->fd
&&
153 window_matches(w
, prot
, offset
, size
);
156 static Window
*window_add(MMapCache
*m
, MMapFileDescriptor
*f
, int prot
, bool keep_always
, uint64_t offset
, size_t size
, void *ptr
) {
162 if (!m
->last_unused
|| m
->n_windows
<= WINDOWS_MIN
) {
164 /* Allocate a new window */
171 /* Reuse an existing one */
180 .keep_always
= keep_always
,
186 LIST_PREPEND(by_fd
, f
->windows
, w
);
191 static void context_detach_window(Context
*c
) {
199 w
= TAKE_PTR(c
->window
);
200 LIST_REMOVE(by_window
, w
->contexts
, c
);
202 if (!w
->contexts
&& !w
->keep_always
) {
203 /* Not used anymore? */
204 #if ENABLE_DEBUG_MMAP_CACHE
205 /* Unmap unused windows immediately to expose use-after-unmap
209 LIST_PREPEND(unused
, c
->cache
->unused
, w
);
210 if (!c
->cache
->last_unused
)
211 c
->cache
->last_unused
= w
;
218 static void context_attach_window(Context
*c
, Window
*w
) {
225 context_detach_window(c
);
229 LIST_REMOVE(unused
, c
->cache
->unused
, w
);
230 if (c
->cache
->last_unused
== w
)
231 c
->cache
->last_unused
= w
->unused_prev
;
233 w
->in_unused
= false;
237 LIST_PREPEND(by_window
, w
->contexts
, c
);
240 static Context
*context_add(MMapCache
*m
, unsigned id
) {
249 c
= new0(Context
, 1);
256 assert(!m
->contexts
[id
]);
262 static void context_free(Context
*c
) {
265 context_detach_window(c
);
268 assert(c
->cache
->contexts
[c
->id
] == c
);
269 c
->cache
->contexts
[c
->id
] = NULL
;
275 static MMapCache
*mmap_cache_free(MMapCache
*m
) {
280 for (i
= 0; i
< MMAP_CACHE_MAX_CONTEXTS
; i
++)
282 context_free(m
->contexts
[i
]);
284 hashmap_free(m
->fds
);
287 window_free(m
->unused
);
292 DEFINE_TRIVIAL_REF_UNREF_FUNC(MMapCache
, mmap_cache
, mmap_cache_free
);
294 static int make_room(MMapCache
*m
) {
300 window_free(m
->last_unused
);
304 static int try_context(
306 MMapFileDescriptor
*f
,
318 assert(m
->n_ref
> 0);
323 c
= m
->contexts
[context
];
327 assert(c
->id
== context
);
332 if (!window_matches_fd(c
->window
, f
, prot
, offset
, size
)) {
334 /* Drop the reference to the window, since it's unnecessary now */
335 context_detach_window(c
);
339 if (c
->window
->fd
->sigbus
)
342 c
->window
->keep_always
= c
->window
->keep_always
|| keep_always
;
344 *ret
= (uint8_t*) c
->window
->ptr
+ (offset
- c
->window
->offset
);
346 *ret_size
= c
->window
->size
- (offset
- c
->window
->offset
);
351 static int find_mmap(
353 MMapFileDescriptor
*f
,
366 assert(m
->n_ref
> 0);
373 LIST_FOREACH(by_fd
, w
, f
->windows
)
374 if (window_matches(w
, prot
, offset
, size
))
380 c
= context_add(m
, context
);
384 context_attach_window(c
, w
);
385 w
->keep_always
= w
->keep_always
|| keep_always
;
387 *ret
= (uint8_t*) w
->ptr
+ (offset
- w
->offset
);
389 *ret_size
= w
->size
- (offset
- w
->offset
);
394 static int mmap_try_harder(MMapCache
*m
, void *addr
, MMapFileDescriptor
*f
, int prot
, int flags
, uint64_t offset
, size_t size
, void **res
) {
404 ptr
= mmap(addr
, size
, prot
, flags
, f
->fd
, offset
);
405 if (ptr
!= MAP_FAILED
)
408 return negative_errno();
423 MMapFileDescriptor
*f
,
433 uint64_t woffset
, wsize
;
440 assert(m
->n_ref
> 0);
445 woffset
= offset
& ~((uint64_t) page_size() - 1ULL);
446 wsize
= size
+ (offset
- woffset
);
447 wsize
= PAGE_ALIGN(wsize
);
449 if (wsize
< WINDOW_SIZE
) {
452 delta
= PAGE_ALIGN((WINDOW_SIZE
- wsize
) / 2);
463 /* Memory maps that are larger then the files
464 underneath have undefined behavior. Hence, clamp
465 things to the file size if we know it */
467 if (woffset
>= (uint64_t) st
->st_size
)
468 return -EADDRNOTAVAIL
;
470 if (woffset
+ wsize
> (uint64_t) st
->st_size
)
471 wsize
= PAGE_ALIGN(st
->st_size
- woffset
);
474 r
= mmap_try_harder(m
, NULL
, f
, prot
, MAP_SHARED
, woffset
, wsize
, &d
);
478 c
= context_add(m
, context
);
482 w
= window_add(m
, f
, prot
, keep_always
, woffset
, wsize
, d
);
486 context_attach_window(c
, w
);
488 *ret
= (uint8_t*) w
->ptr
+ (offset
- w
->offset
);
490 *ret_size
= w
->size
- (offset
- w
->offset
);
495 (void) munmap(d
, wsize
);
501 MMapFileDescriptor
*f
,
514 assert(m
->n_ref
> 0);
518 assert(context
< MMAP_CACHE_MAX_CONTEXTS
);
520 /* Check whether the current context is the right one already */
521 r
= try_context(m
, f
, prot
, context
, keep_always
, offset
, size
, ret
, ret_size
);
527 /* Search for a matching mmap */
528 r
= find_mmap(m
, f
, prot
, context
, keep_always
, offset
, size
, ret
, ret_size
);
536 /* Create a new mmap */
537 return add_mmap(m
, f
, prot
, context
, keep_always
, offset
, size
, st
, ret
, ret_size
);
540 unsigned mmap_cache_get_hit(MMapCache
*m
) {
546 unsigned mmap_cache_get_missed(MMapCache
*m
) {
552 static void mmap_cache_process_sigbus(MMapCache
*m
) {
554 MMapFileDescriptor
*f
;
560 /* Iterate through all triggered pages and mark their files as
566 r
= sigbus_pop(&addr
);
567 if (_likely_(r
== 0))
570 log_error_errno(r
, "SIGBUS handling failed: %m");
575 HASHMAP_FOREACH(f
, m
->fds
, i
) {
578 LIST_FOREACH(by_fd
, w
, f
->windows
) {
579 if ((uint8_t*) addr
>= (uint8_t*) w
->ptr
&&
580 (uint8_t*) addr
< (uint8_t*) w
->ptr
+ w
->size
) {
581 found
= ours
= f
->sigbus
= true;
590 /* Didn't find a matching window, give up */
592 log_error("Unknown SIGBUS page, aborting.");
597 /* The list of triggered pages is now empty. Now, let's remap
598 * all windows of the triggered file to anonymous maps, so
599 * that no page of the file in question is triggered again, so
600 * that we can be sure not to hit the queue size limit. */
601 if (_likely_(!found
))
604 HASHMAP_FOREACH(f
, m
->fds
, i
) {
610 LIST_FOREACH(by_fd
, w
, f
->windows
)
611 window_invalidate(w
);
615 bool mmap_cache_got_sigbus(MMapCache
*m
, MMapFileDescriptor
*f
) {
619 mmap_cache_process_sigbus(m
);
624 MMapFileDescriptor
* mmap_cache_add_fd(MMapCache
*m
, int fd
) {
625 MMapFileDescriptor
*f
;
631 f
= hashmap_get(m
->fds
, FD_TO_PTR(fd
));
635 r
= hashmap_ensure_allocated(&m
->fds
, NULL
);
639 f
= new0(MMapFileDescriptor
, 1);
646 r
= hashmap_put(m
->fds
, FD_TO_PTR(fd
), f
);
653 void mmap_cache_free_fd(MMapCache
*m
, MMapFileDescriptor
*f
) {
657 /* Make sure that any queued SIGBUS are first dispatched, so
658 * that we don't end up with a SIGBUS entry we cannot relate
659 * to any existing memory map */
661 mmap_cache_process_sigbus(m
);
664 window_free(f
->windows
);
667 assert_se(hashmap_remove(f
->cache
->fds
, FD_TO_PTR(f
->fd
)));