]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/journal/mmap-cache.c
Merge pull request #6324 from keszybz/generator-add-symlink
[thirdparty/systemd.git] / src / journal / mmap-cache.c
CommitLineData
16e9f408
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2012 Lennart Poettering
5
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.
10
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.
15
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/>.
18***/
19
16e9f408
LP
20#include <errno.h>
21#include <stdlib.h>
f8019684 22#include <sys/mman.h>
16e9f408 23
b5efdb8a 24#include "alloc-util.h"
23e096cc 25#include "fd-util.h"
f8019684
LP
26#include "hashmap.h"
27#include "list.h"
28#include "log.h"
f8019684 29#include "macro.h"
16e9f408 30#include "mmap-cache.h"
cf0fbc49
TA
31#include "sigbus.h"
32#include "util.h"
16e9f408 33
f8019684
LP
34typedef struct Window Window;
35typedef struct Context Context;
84168d80 36
f8019684
LP
37struct Window {
38 MMapCache *cache;
39
739731cd
LP
40 bool invalidated:1;
41 bool keep_always:1;
42 bool in_unused:1;
16e9f408 43
68667801 44 int prot;
16e9f408
LP
45 void *ptr;
46 uint64_t offset;
f8019684
LP
47 size_t size;
48
be7cdd8e 49 MMapFileDescriptor *fd;
16e9f408 50
f8019684
LP
51 LIST_FIELDS(Window, by_fd);
52 LIST_FIELDS(Window, unused);
53
54 LIST_HEAD(Context, contexts);
55};
16e9f408 56
f8019684
LP
57struct Context {
58 MMapCache *cache;
59 unsigned id;
60 Window *window;
16e9f408 61
f8019684
LP
62 LIST_FIELDS(Context, by_window);
63};
64
be7cdd8e 65struct MMapFileDescriptor {
f8019684 66 MMapCache *cache;
16e9f408 67 int fd;
fa6ac760 68 bool sigbus;
f8019684
LP
69 LIST_HEAD(Window, windows);
70};
16e9f408
LP
71
72struct MMapCache {
f8019684 73 int n_ref;
68667801 74 unsigned n_windows;
16e9f408 75
bf807d4d
LP
76 unsigned n_hit, n_missed;
77
f8019684 78 Hashmap *fds;
69adae51 79 Context *contexts[MMAP_CACHE_MAX_CONTEXTS];
16e9f408 80
f8019684
LP
81 LIST_HEAD(Window, unused);
82 Window *last_unused;
16e9f408
LP
83};
84
f8019684 85#define WINDOWS_MIN 64
fad5a6c6
MS
86
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())
90#else
91# define WINDOW_SIZE (8ULL*1024ULL*1024ULL)
92#endif
16e9f408 93
f8019684
LP
94MMapCache* mmap_cache_new(void) {
95 MMapCache *m;
16e9f408 96
f8019684
LP
97 m = new0(MMapCache, 1);
98 if (!m)
99 return NULL;
16e9f408 100
f8019684
LP
101 m->n_ref = 1;
102 return m;
16e9f408
LP
103}
104
f8019684 105MMapCache* mmap_cache_ref(MMapCache *m) {
16e9f408 106 assert(m);
f8019684 107 assert(m->n_ref > 0);
16e9f408 108
313cefa1 109 m->n_ref++;
f8019684
LP
110 return m;
111}
f65425cb 112
f8019684
LP
113static void window_unlink(Window *w) {
114 Context *c;
f65425cb 115
f8019684 116 assert(w);
16e9f408 117
f8019684
LP
118 if (w->ptr)
119 munmap(w->ptr, w->size);
16e9f408 120
f8019684 121 if (w->fd)
71fda00f 122 LIST_REMOVE(by_fd, w->fd->windows, w);
16e9f408 123
f8019684
LP
124 if (w->in_unused) {
125 if (w->cache->last_unused == w)
126 w->cache->last_unused = w->unused_prev;
16e9f408 127
71fda00f 128 LIST_REMOVE(unused, w->cache->unused, w);
f65425cb 129 }
16e9f408 130
f8019684
LP
131 LIST_FOREACH(by_window, c, w->contexts) {
132 assert(c->window == w);
133 c->window = NULL;
f65425cb 134 }
16e9f408
LP
135}
136
fa6ac760
LP
137static void window_invalidate(Window *w) {
138 assert(w);
139
140 if (w->invalidated)
141 return;
142
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
146 * queue. */
147
148 assert_se(mmap(w->ptr, w->size, w->prot, MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, -1, 0) == w->ptr);
149 w->invalidated = true;
150}
151
f8019684
LP
152static void window_free(Window *w) {
153 assert(w);
f65425cb 154
f8019684 155 window_unlink(w);
89de6947 156 w->cache->n_windows--;
f8019684
LP
157 free(w);
158}
f65425cb 159
8c3d9662 160_pure_ static inline bool window_matches(Window *w, int prot, uint64_t offset, size_t size) {
f8019684 161 assert(w);
f8019684 162 assert(size > 0);
16e9f408 163
f8019684 164 return
f8019684
LP
165 prot == w->prot &&
166 offset >= w->offset &&
167 offset + size <= w->offset + w->size;
16e9f408
LP
168}
169
8c3d9662
VC
170_pure_ static bool window_matches_fd(Window *w, MMapFileDescriptor *f, int prot, uint64_t offset, size_t size) {
171 assert(w);
172 assert(f);
173
174 return
175 w->fd &&
176 f->fd == w->fd->fd &&
177 window_matches(w, prot, offset, size);
178}
179
be7cdd8e 180static Window *window_add(MMapCache *m, MMapFileDescriptor *f, int prot, bool keep_always, uint64_t offset, size_t size, void *ptr) {
f8019684 181 Window *w;
16e9f408
LP
182
183 assert(m);
be7cdd8e 184 assert(f);
16e9f408 185
f8019684 186 if (!m->last_unused || m->n_windows <= WINDOWS_MIN) {
f65425cb 187
f8019684
LP
188 /* Allocate a new window */
189 w = new0(Window, 1);
190 if (!w)
191 return NULL;
89de6947 192 m->n_windows++;
f65425cb 193 } else {
16e9f408 194
f8019684
LP
195 /* Reuse an existing one */
196 w = m->last_unused;
197 window_unlink(w);
198 zero(*w);
f65425cb 199 }
f8019684
LP
200
201 w->cache = m;
be7cdd8e 202 w->fd = f;
6a491490
VC
203 w->prot = prot;
204 w->keep_always = keep_always;
205 w->offset = offset;
206 w->size = size;
207 w->ptr = ptr;
208
be7cdd8e 209 LIST_PREPEND(by_fd, f->windows, w);
6a491490 210
f8019684 211 return w;
16e9f408
LP
212}
213
f8019684
LP
214static void context_detach_window(Context *c) {
215 Window *w;
16e9f408 216
f8019684 217 assert(c);
16e9f408 218
f8019684 219 if (!c->window)
16e9f408
LP
220 return;
221
f8019684
LP
222 w = c->window;
223 c->window = NULL;
71fda00f 224 LIST_REMOVE(by_window, w->contexts, c);
16e9f408 225
1b8951e5 226 if (!w->contexts && !w->keep_always) {
f8019684 227 /* Not used anymore? */
fad5a6c6
MS
228#ifdef ENABLE_DEBUG_MMAP_CACHE
229 /* Unmap unused windows immediately to expose use-after-unmap
230 * by SIGSEGV. */
231 window_free(w);
232#else
71fda00f 233 LIST_PREPEND(unused, c->cache->unused, w);
f8019684
LP
234 if (!c->cache->last_unused)
235 c->cache->last_unused = w;
16e9f408 236
f8019684 237 w->in_unused = true;
fad5a6c6 238#endif
f8019684 239 }
16e9f408
LP
240}
241
f8019684
LP
242static void context_attach_window(Context *c, Window *w) {
243 assert(c);
244 assert(w);
16e9f408 245
f8019684 246 if (c->window == w)
16e9f408
LP
247 return;
248
f8019684 249 context_detach_window(c);
16e9f408 250
e18021f7 251 if (w->in_unused) {
f8019684 252 /* Used again? */
71fda00f 253 LIST_REMOVE(unused, c->cache->unused, w);
a2ab7ee6
CG
254 if (c->cache->last_unused == w)
255 c->cache->last_unused = w->unused_prev;
16e9f408 256
f8019684
LP
257 w->in_unused = false;
258 }
f65425cb 259
f8019684 260 c->window = w;
71fda00f 261 LIST_PREPEND(by_window, w->contexts, c);
16e9f408
LP
262}
263
f8019684
LP
264static Context *context_add(MMapCache *m, unsigned id) {
265 Context *c;
16e9f408
LP
266
267 assert(m);
268
69adae51 269 c = m->contexts[id];
f8019684
LP
270 if (c)
271 return c;
272
f8019684
LP
273 c = new0(Context, 1);
274 if (!c)
275 return NULL;
16e9f408 276
f8019684
LP
277 c->cache = m;
278 c->id = id;
16e9f408 279
69adae51
MS
280 assert(!m->contexts[id]);
281 m->contexts[id] = c;
16e9f408 282
f8019684 283 return c;
16e9f408
LP
284}
285
f8019684
LP
286static void context_free(Context *c) {
287 assert(c);
16e9f408 288
f8019684 289 context_detach_window(c);
16e9f408 290
69adae51
MS
291 if (c->cache) {
292 assert(c->cache->contexts[c->id] == c);
293 c->cache->contexts[c->id] = NULL;
294 }
16e9f408 295
f8019684
LP
296 free(c);
297}
298
f8019684 299static void mmap_cache_free(MMapCache *m) {
69adae51 300 int i;
f8019684 301
16e9f408 302 assert(m);
16e9f408 303
69adae51
MS
304 for (i = 0; i < MMAP_CACHE_MAX_CONTEXTS; i++)
305 if (m->contexts[i])
306 context_free(m->contexts[i]);
8e6d9397 307
8e6d9397
GM
308 hashmap_free(m->fds);
309
f8019684
LP
310 while (m->unused)
311 window_free(m->unused);
312
313 free(m);
16e9f408
LP
314}
315
316MMapCache* mmap_cache_unref(MMapCache *m) {
f649045c
LP
317
318 if (!m)
319 return NULL;
320
16e9f408
LP
321 assert(m->n_ref > 0);
322
313cefa1 323 m->n_ref--;
f8019684 324 if (m->n_ref == 0)
16e9f408 325 mmap_cache_free(m);
16e9f408
LP
326
327 return NULL;
328}
329
f8019684
LP
330static int make_room(MMapCache *m) {
331 assert(m);
332
333 if (!m->last_unused)
334 return 0;
335
336 window_free(m->last_unused);
337 return 1;
338}
339
340static int try_context(
341 MMapCache *m,
be7cdd8e 342 MMapFileDescriptor *f,
f8019684
LP
343 int prot,
344 unsigned context,
345 bool keep_always,
346 uint64_t offset,
347 size_t size,
1b8951e5 348 void **ret) {
f8019684
LP
349
350 Context *c;
f65425cb 351
16e9f408 352 assert(m);
f8019684 353 assert(m->n_ref > 0);
be7cdd8e 354 assert(f);
f8019684 355 assert(size > 0);
1b8951e5 356 assert(ret);
16e9f408 357
69adae51 358 c = m->contexts[context];
f8019684 359 if (!c)
16e9f408 360 return 0;
16e9f408 361
f8019684 362 assert(c->id == context);
16e9f408 363
f8019684
LP
364 if (!c->window)
365 return 0;
f65425cb 366
8c3d9662 367 if (!window_matches_fd(c->window, f, prot, offset, size)) {
f65425cb 368
f8019684
LP
369 /* Drop the reference to the window, since it's unnecessary now */
370 context_detach_window(c);
371 return 0;
f65425cb
LP
372 }
373
fa6ac760
LP
374 if (c->window->fd->sigbus)
375 return -EIO;
376
739731cd 377 c->window->keep_always = c->window->keep_always || keep_always;
16e9f408 378
1b8951e5 379 *ret = (uint8_t*) c->window->ptr + (offset - c->window->offset);
f8019684 380 return 1;
16e9f408
LP
381}
382
f8019684
LP
383static int find_mmap(
384 MMapCache *m,
be7cdd8e 385 MMapFileDescriptor *f,
f8019684
LP
386 int prot,
387 unsigned context,
388 bool keep_always,
389 uint64_t offset,
390 size_t size,
1b8951e5 391 void **ret) {
f8019684 392
f8019684
LP
393 Window *w;
394 Context *c;
16e9f408
LP
395
396 assert(m);
f8019684 397 assert(m->n_ref > 0);
be7cdd8e 398 assert(f);
f8019684 399 assert(size > 0);
16e9f408 400
fa6ac760
LP
401 if (f->sigbus)
402 return -EIO;
403
f8019684 404 LIST_FOREACH(by_fd, w, f->windows)
8c3d9662 405 if (window_matches(w, prot, offset, size))
f8019684 406 break;
16e9f408 407
f8019684
LP
408 if (!w)
409 return 0;
410
411 c = context_add(m, context);
412 if (!c)
413 return -ENOMEM;
414
415 context_attach_window(c, w);
739731cd 416 w->keep_always = w->keep_always || keep_always;
16e9f408 417
1b8951e5 418 *ret = (uint8_t*) w->ptr + (offset - w->offset);
f8019684 419 return 1;
16e9f408
LP
420}
421
be7cdd8e 422static int mmap_try_harder(MMapCache *m, void *addr, MMapFileDescriptor *f, int prot, int flags, uint64_t offset, size_t size, void **res) {
db87967e
VC
423 void *ptr;
424
425 assert(m);
be7cdd8e 426 assert(f);
db87967e
VC
427 assert(res);
428
429 for (;;) {
430 int r;
431
be7cdd8e 432 ptr = mmap(addr, size, prot, flags, f->fd, offset);
db87967e
VC
433 if (ptr != MAP_FAILED)
434 break;
435 if (errno != ENOMEM)
3f0083a2 436 return negative_errno();
db87967e
VC
437
438 r = make_room(m);
439 if (r < 0)
440 return r;
441 if (r == 0)
442 return -ENOMEM;
443 }
444
445 *res = ptr;
446 return 0;
447}
448
f8019684 449static int add_mmap(
16e9f408 450 MMapCache *m,
be7cdd8e 451 MMapFileDescriptor *f,
16e9f408
LP
452 int prot,
453 unsigned context,
fcde2389 454 bool keep_always,
16e9f408 455 uint64_t offset,
f8019684 456 size_t size,
fcde2389 457 struct stat *st,
1b8951e5 458 void **ret) {
16e9f408 459
16e9f408 460 uint64_t woffset, wsize;
f8019684 461 Context *c;
f8019684
LP
462 Window *w;
463 void *d;
16e9f408
LP
464 int r;
465
466 assert(m);
f8019684 467 assert(m->n_ref > 0);
be7cdd8e 468 assert(f);
16e9f408 469 assert(size > 0);
1b8951e5 470 assert(ret);
16e9f408
LP
471
472 woffset = offset & ~((uint64_t) page_size() - 1ULL);
473 wsize = size + (offset - woffset);
474 wsize = PAGE_ALIGN(wsize);
475
476 if (wsize < WINDOW_SIZE) {
477 uint64_t delta;
478
beec0085 479 delta = PAGE_ALIGN((WINDOW_SIZE - wsize) / 2);
16e9f408
LP
480
481 if (delta > offset)
482 woffset = 0;
483 else
484 woffset -= delta;
485
486 wsize = WINDOW_SIZE;
487 }
488
fcde2389
LP
489 if (st) {
490 /* Memory maps that are larger then the files
c5315881 491 underneath have undefined behavior. Hence, clamp
fcde2389
LP
492 things to the file size if we know it */
493
494 if (woffset >= (uint64_t) st->st_size)
495 return -EADDRNOTAVAIL;
496
497 if (woffset + wsize > (uint64_t) st->st_size)
498 wsize = PAGE_ALIGN(st->st_size - woffset);
499 }
500
be7cdd8e 501 r = mmap_try_harder(m, NULL, f, prot, MAP_SHARED, woffset, wsize, &d);
db87967e
VC
502 if (r < 0)
503 return r;
16e9f408 504
f8019684
LP
505 c = context_add(m, context);
506 if (!c)
b67ddc7b 507 goto outofmem;
16e9f408 508
6a491490 509 w = window_add(m, f, prot, keep_always, woffset, wsize, d);
f8019684 510 if (!w)
b67ddc7b 511 goto outofmem;
16e9f408 512
c7884da9 513 context_attach_window(c, w);
16e9f408 514
1b8951e5 515 *ret = (uint8_t*) w->ptr + (offset - w->offset);
16e9f408 516 return 1;
b67ddc7b
PDS
517
518outofmem:
3f0083a2 519 (void) munmap(d, wsize);
b67ddc7b 520 return -ENOMEM;
16e9f408
LP
521}
522
523int mmap_cache_get(
524 MMapCache *m,
be7cdd8e 525 MMapFileDescriptor *f,
16e9f408
LP
526 int prot,
527 unsigned context,
fcde2389 528 bool keep_always,
16e9f408 529 uint64_t offset,
f8019684 530 size_t size,
fcde2389 531 struct stat *st,
1b8951e5 532 void **ret) {
16e9f408 533
16e9f408
LP
534 int r;
535
536 assert(m);
f8019684 537 assert(m->n_ref > 0);
be7cdd8e 538 assert(f);
16e9f408 539 assert(size > 0);
1b8951e5 540 assert(ret);
69adae51 541 assert(context < MMAP_CACHE_MAX_CONTEXTS);
16e9f408 542
f8019684 543 /* Check whether the current context is the right one already */
be7cdd8e 544 r = try_context(m, f, prot, context, keep_always, offset, size, ret);
bf807d4d 545 if (r != 0) {
313cefa1 546 m->n_hit++;
16e9f408 547 return r;
bf807d4d 548 }
16e9f408 549
f8019684 550 /* Search for a matching mmap */
be7cdd8e 551 r = find_mmap(m, f, prot, context, keep_always, offset, size, ret);
bf807d4d 552 if (r != 0) {
313cefa1 553 m->n_hit++;
16e9f408 554 return r;
bf807d4d
LP
555 }
556
557 m->n_missed++;
16e9f408 558
f8019684 559 /* Create a new mmap */
be7cdd8e 560 return add_mmap(m, f, prot, context, keep_always, offset, size, st, ret);
ae97089d
ZJS
561}
562
fa6ac760
LP
563unsigned mmap_cache_get_hit(MMapCache *m) {
564 assert(m);
565
566 return m->n_hit;
567}
568
569unsigned mmap_cache_get_missed(MMapCache *m) {
570 assert(m);
571
572 return m->n_missed;
573}
574
575static void mmap_cache_process_sigbus(MMapCache *m) {
576 bool found = false;
be7cdd8e 577 MMapFileDescriptor *f;
fa6ac760
LP
578 Iterator i;
579 int r;
16e9f408
LP
580
581 assert(m);
16e9f408 582
fa6ac760
LP
583 /* Iterate through all triggered pages and mark their files as
584 * invalidated */
585 for (;;) {
586 bool ours;
587 void *addr;
588
589 r = sigbus_pop(&addr);
590 if (_likely_(r == 0))
591 break;
592 if (r < 0) {
593 log_error_errno(r, "SIGBUS handling failed: %m");
594 abort();
595 }
596
597 ours = false;
598 HASHMAP_FOREACH(f, m->fds, i) {
599 Window *w;
600
601 LIST_FOREACH(by_fd, w, f->windows) {
602 if ((uint8_t*) addr >= (uint8_t*) w->ptr &&
603 (uint8_t*) addr < (uint8_t*) w->ptr + w->size) {
604 found = ours = f->sigbus = true;
605 break;
606 }
607 }
608
609 if (ours)
610 break;
611 }
612
613 /* Didn't find a matching window, give up */
614 if (!ours) {
615 log_error("Unknown SIGBUS page, aborting.");
616 abort();
617 }
618 }
619
620 /* The list of triggered pages is now empty. Now, let's remap
621 * all windows of the triggered file to anonymous maps, so
622 * that no page of the file in question is triggered again, so
623 * that we can be sure not to hit the queue size limit. */
624 if (_likely_(!found))
16e9f408 625 return;
16e9f408 626
fa6ac760
LP
627 HASHMAP_FOREACH(f, m->fds, i) {
628 Window *w;
629
630 if (!f->sigbus)
631 continue;
632
633 LIST_FOREACH(by_fd, w, f->windows)
634 window_invalidate(w);
635 }
f8019684 636}
16e9f408 637
be7cdd8e 638bool mmap_cache_got_sigbus(MMapCache *m, MMapFileDescriptor *f) {
bf807d4d 639 assert(m);
be7cdd8e 640 assert(f);
bf807d4d 641
fa6ac760
LP
642 mmap_cache_process_sigbus(m);
643
fa6ac760 644 return f->sigbus;
bf807d4d
LP
645}
646
be7cdd8e
VC
647MMapFileDescriptor* mmap_cache_add_fd(MMapCache *m, int fd) {
648 MMapFileDescriptor *f;
649 int r;
fa6ac760 650
bf807d4d 651 assert(m);
fa6ac760 652 assert(fd >= 0);
bf807d4d 653
be7cdd8e
VC
654 f = hashmap_get(m->fds, FD_TO_PTR(fd));
655 if (f)
656 return f;
657
658 r = hashmap_ensure_allocated(&m->fds, NULL);
659 if (r < 0)
660 return NULL;
661
662 f = new0(MMapFileDescriptor, 1);
663 if (!f)
664 return NULL;
665
666 f->cache = m;
667 f->fd = fd;
668
669 r = hashmap_put(m->fds, FD_TO_PTR(fd), f);
670 if (r < 0)
671 return mfree(f);
672
673 return f;
674}
675
676void mmap_cache_free_fd(MMapCache *m, MMapFileDescriptor *f) {
677 assert(m);
678 assert(f);
679
fa6ac760
LP
680 /* Make sure that any queued SIGBUS are first dispatched, so
681 * that we don't end up with a SIGBUS entry we cannot relate
682 * to any existing memory map */
683
684 mmap_cache_process_sigbus(m);
685
be7cdd8e
VC
686 while (f->windows)
687 window_free(f->windows);
688
689 if (f->cache)
690 assert_se(hashmap_remove(f->cache->fds, FD_TO_PTR(f->fd)));
fa6ac760 691
be7cdd8e 692 free(f);
bf807d4d 693}