]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/mmap-cache.c
2 This file is part of systemd.
4 Copyright 2012 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include "alloc-util.h"
30 #include "mmap-cache.h"
34 typedef struct Window Window
;
35 typedef struct Context Context
;
49 MMapFileDescriptor
*fd
;
51 LIST_FIELDS(Window
, by_fd
);
52 LIST_FIELDS(Window
, unused
);
54 LIST_HEAD(Context
, contexts
);
62 LIST_FIELDS(Context
, by_window
);
65 struct MMapFileDescriptor
{
69 LIST_HEAD(Window
, windows
);
76 unsigned n_hit
, n_missed
;
79 Context
*contexts
[MMAP_CACHE_MAX_CONTEXTS
];
81 LIST_HEAD(Window
, unused
);
85 #define WINDOWS_MIN 64
87 #ifdef ENABLE_DEBUG_MMAP_CACHE
88 /* Tiny windows increase mmap activity and the chance of exposing unsafe use. */
89 # define WINDOW_SIZE (page_size())
91 # define WINDOW_SIZE (8ULL*1024ULL*1024ULL)
94 MMapCache
* mmap_cache_new(void) {
97 m
= new0(MMapCache
, 1);
105 MMapCache
* mmap_cache_ref(MMapCache
*m
) {
107 assert(m
->n_ref
> 0);
113 static void window_unlink(Window
*w
) {
119 munmap(w
->ptr
, w
->size
);
122 LIST_REMOVE(by_fd
, w
->fd
->windows
, w
);
125 if (w
->cache
->last_unused
== w
)
126 w
->cache
->last_unused
= w
->unused_prev
;
128 LIST_REMOVE(unused
, w
->cache
->unused
, w
);
131 LIST_FOREACH(by_window
, c
, w
->contexts
) {
132 assert(c
->window
== w
);
137 static void window_invalidate(Window
*w
) {
143 /* Replace the window with anonymous pages. This is useful
144 * when we hit a SIGBUS and want to make sure the file cannot
145 * trigger any further SIGBUS, possibly overrunning the sigbus
148 assert_se(mmap(w
->ptr
, w
->size
, w
->prot
, MAP_PRIVATE
|MAP_ANONYMOUS
|MAP_FIXED
, -1, 0) == w
->ptr
);
149 w
->invalidated
= true;
152 static void window_free(Window
*w
) {
156 w
->cache
->n_windows
--;
160 _pure_
static bool window_matches(Window
*w
, MMapFileDescriptor
*f
, int prot
, uint64_t offset
, size_t size
) {
167 f
->fd
== w
->fd
->fd
&&
169 offset
>= w
->offset
&&
170 offset
+ size
<= w
->offset
+ w
->size
;
173 static Window
*window_add(MMapCache
*m
, MMapFileDescriptor
*f
, int prot
, bool keep_always
, uint64_t offset
, size_t size
, void *ptr
) {
179 if (!m
->last_unused
|| m
->n_windows
<= WINDOWS_MIN
) {
181 /* Allocate a new window */
188 /* Reuse an existing one */
197 w
->keep_always
= keep_always
;
202 LIST_PREPEND(by_fd
, f
->windows
, w
);
207 static void context_detach_window(Context
*c
) {
217 LIST_REMOVE(by_window
, w
->contexts
, c
);
219 if (!w
->contexts
&& !w
->keep_always
) {
220 /* Not used anymore? */
221 #ifdef ENABLE_DEBUG_MMAP_CACHE
222 /* Unmap unused windows immediately to expose use-after-unmap
226 LIST_PREPEND(unused
, c
->cache
->unused
, w
);
227 if (!c
->cache
->last_unused
)
228 c
->cache
->last_unused
= w
;
235 static void context_attach_window(Context
*c
, Window
*w
) {
242 context_detach_window(c
);
246 LIST_REMOVE(unused
, c
->cache
->unused
, w
);
247 if (c
->cache
->last_unused
== w
)
248 c
->cache
->last_unused
= w
->unused_prev
;
250 w
->in_unused
= false;
254 LIST_PREPEND(by_window
, w
->contexts
, c
);
257 static Context
*context_add(MMapCache
*m
, unsigned id
) {
266 c
= new0(Context
, 1);
273 assert(!m
->contexts
[id
]);
279 static void context_free(Context
*c
) {
282 context_detach_window(c
);
285 assert(c
->cache
->contexts
[c
->id
] == c
);
286 c
->cache
->contexts
[c
->id
] = NULL
;
292 static void mmap_cache_free(MMapCache
*m
) {
297 for (i
= 0; i
< MMAP_CACHE_MAX_CONTEXTS
; i
++)
299 context_free(m
->contexts
[i
]);
301 hashmap_free(m
->fds
);
304 window_free(m
->unused
);
309 MMapCache
* mmap_cache_unref(MMapCache
*m
) {
314 assert(m
->n_ref
> 0);
323 static int make_room(MMapCache
*m
) {
329 window_free(m
->last_unused
);
333 static int try_context(
335 MMapFileDescriptor
*f
,
346 assert(m
->n_ref
> 0);
351 c
= m
->contexts
[context
];
355 assert(c
->id
== context
);
360 if (!window_matches(c
->window
, f
, prot
, offset
, size
)) {
362 /* Drop the reference to the window, since it's unnecessary now */
363 context_detach_window(c
);
367 if (c
->window
->fd
->sigbus
)
370 c
->window
->keep_always
= c
->window
->keep_always
|| keep_always
;
372 *ret
= (uint8_t*) c
->window
->ptr
+ (offset
- c
->window
->offset
);
376 static int find_mmap(
378 MMapFileDescriptor
*f
,
390 assert(m
->n_ref
> 0);
397 LIST_FOREACH(by_fd
, w
, f
->windows
)
398 if (window_matches(w
, f
, prot
, offset
, size
))
404 c
= context_add(m
, context
);
408 context_attach_window(c
, w
);
409 w
->keep_always
= w
->keep_always
|| keep_always
;
411 *ret
= (uint8_t*) w
->ptr
+ (offset
- w
->offset
);
415 static int mmap_try_harder(MMapCache
*m
, void *addr
, MMapFileDescriptor
*f
, int prot
, int flags
, uint64_t offset
, size_t size
, void **res
) {
425 ptr
= mmap(addr
, size
, prot
, flags
, f
->fd
, offset
);
426 if (ptr
!= MAP_FAILED
)
429 return negative_errno();
444 MMapFileDescriptor
*f
,
453 uint64_t woffset
, wsize
;
460 assert(m
->n_ref
> 0);
465 woffset
= offset
& ~((uint64_t) page_size() - 1ULL);
466 wsize
= size
+ (offset
- woffset
);
467 wsize
= PAGE_ALIGN(wsize
);
469 if (wsize
< WINDOW_SIZE
) {
472 delta
= PAGE_ALIGN((WINDOW_SIZE
- wsize
) / 2);
483 /* Memory maps that are larger then the files
484 underneath have undefined behavior. Hence, clamp
485 things to the file size if we know it */
487 if (woffset
>= (uint64_t) st
->st_size
)
488 return -EADDRNOTAVAIL
;
490 if (woffset
+ wsize
> (uint64_t) st
->st_size
)
491 wsize
= PAGE_ALIGN(st
->st_size
- woffset
);
494 r
= mmap_try_harder(m
, NULL
, f
, prot
, MAP_SHARED
, woffset
, wsize
, &d
);
498 c
= context_add(m
, context
);
502 w
= window_add(m
, f
, prot
, keep_always
, woffset
, wsize
, d
);
506 context_detach_window(c
);
508 LIST_PREPEND(by_window
, w
->contexts
, c
);
510 *ret
= (uint8_t*) w
->ptr
+ (offset
- w
->offset
);
514 (void) munmap(d
, wsize
);
520 MMapFileDescriptor
*f
,
532 assert(m
->n_ref
> 0);
536 assert(context
< MMAP_CACHE_MAX_CONTEXTS
);
538 /* Check whether the current context is the right one already */
539 r
= try_context(m
, f
, prot
, context
, keep_always
, offset
, size
, ret
);
545 /* Search for a matching mmap */
546 r
= find_mmap(m
, f
, prot
, context
, keep_always
, offset
, size
, ret
);
554 /* Create a new mmap */
555 return add_mmap(m
, f
, prot
, context
, keep_always
, offset
, size
, st
, ret
);
558 unsigned mmap_cache_get_hit(MMapCache
*m
) {
564 unsigned mmap_cache_get_missed(MMapCache
*m
) {
570 static void mmap_cache_process_sigbus(MMapCache
*m
) {
572 MMapFileDescriptor
*f
;
578 /* Iterate through all triggered pages and mark their files as
584 r
= sigbus_pop(&addr
);
585 if (_likely_(r
== 0))
588 log_error_errno(r
, "SIGBUS handling failed: %m");
593 HASHMAP_FOREACH(f
, m
->fds
, i
) {
596 LIST_FOREACH(by_fd
, w
, f
->windows
) {
597 if ((uint8_t*) addr
>= (uint8_t*) w
->ptr
&&
598 (uint8_t*) addr
< (uint8_t*) w
->ptr
+ w
->size
) {
599 found
= ours
= f
->sigbus
= true;
608 /* Didn't find a matching window, give up */
610 log_error("Unknown SIGBUS page, aborting.");
615 /* The list of triggered pages is now empty. Now, let's remap
616 * all windows of the triggered file to anonymous maps, so
617 * that no page of the file in question is triggered again, so
618 * that we can be sure not to hit the queue size limit. */
619 if (_likely_(!found
))
622 HASHMAP_FOREACH(f
, m
->fds
, i
) {
628 LIST_FOREACH(by_fd
, w
, f
->windows
)
629 window_invalidate(w
);
633 bool mmap_cache_got_sigbus(MMapCache
*m
, MMapFileDescriptor
*f
) {
637 mmap_cache_process_sigbus(m
);
642 MMapFileDescriptor
* mmap_cache_add_fd(MMapCache
*m
, int fd
) {
643 MMapFileDescriptor
*f
;
649 f
= hashmap_get(m
->fds
, FD_TO_PTR(fd
));
653 r
= hashmap_ensure_allocated(&m
->fds
, NULL
);
657 f
= new0(MMapFileDescriptor
, 1);
664 r
= hashmap_put(m
->fds
, FD_TO_PTR(fd
), f
);
671 void mmap_cache_free_fd(MMapCache
*m
, MMapFileDescriptor
*f
) {
675 /* Make sure that any queued SIGBUS are first dispatched, so
676 * that we don't end up with a SIGBUS entry we cannot relate
677 * to any existing memory map */
679 mmap_cache_process_sigbus(m
);
682 window_free(f
->windows
);
685 assert_se(hashmap_remove(f
->cache
->fds
, FD_TO_PTR(f
->fd
)));