1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
|
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2012 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <assert.h>
#include <sys/mman.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "util.h"
#include "mmap-cache.h"
#define WINDOW_SIZE (8ULL*1024ULL*1024ULL)
#define DEFAULT_WINDOWS_MAX 64
#define DEFAULT_FDS_MAX 32
#define DEFAULT_CONTEXTS_MAX 32
typedef struct Window {
int fd;
void *ptr;
uint64_t offset;
uint64_t size;
unsigned n_ref;
unsigned lru_prev;
unsigned lru_next;
unsigned by_fd_prev;
unsigned by_fd_next;
} Window;
typedef struct FileDescriptor {
int fd;
unsigned windows;
} FileDescriptor;
struct MMapCache {
unsigned n_ref;
unsigned contexts_max;
unsigned windows_max;
unsigned fds_max;
unsigned n_windows;
unsigned n_fds;
unsigned lru_first, lru_last;
Window *windows;
unsigned *by_context;
FileDescriptor *by_fd;
};
static int mmap_cache_peek_fd_index(MMapCache *m, int fd, unsigned *fd_index);
static void mmap_cache_window_unmap(MMapCache *m, unsigned w) {
Window *v;
assert(m);
assert(w < m->n_windows);
v = m->windows + w;
if (!v->ptr)
return;
munmap(v->ptr, v->size);
v->ptr = NULL;
}
static void mmap_cache_window_add_lru(MMapCache *m, unsigned w) {
Window *v;
assert(m);
assert(w < m->n_windows);
v = m->windows + w;
assert(v->n_ref == 0);
if (m->lru_last != (unsigned) -1) {
assert(m->windows[m->lru_last].lru_next == (unsigned) -1);
m->windows[m->lru_last].lru_next = w;
}
v->lru_prev = m->lru_last;
v->lru_next = (unsigned) -1;
m->lru_last = w;
if (m->lru_first == (unsigned) -1)
m->lru_first = w;
}
static void mmap_cache_window_remove_lru(MMapCache *m, unsigned w) {
Window *v;
assert(m);
assert(w < m->n_windows);
v = m->windows + w;
if (v->lru_prev == (unsigned) -1) {
assert(m->lru_first == w);
m->lru_first = v->lru_next;
} else {
assert(m->windows[v->lru_prev].lru_next == w);
m->windows[v->lru_prev].lru_next = v->lru_next;
}
if (v->lru_next == (unsigned) -1) {
assert(m->lru_last == w);
m->lru_last = v->lru_prev;
} else {
assert(m->windows[v->lru_next].lru_prev == w);
m->windows[v->lru_next].lru_prev = v->lru_prev;
}
}
static void mmap_cache_fd_add(MMapCache *m, unsigned fd_index, unsigned w) {
Window *v;
assert(m);
assert(fd_index < m->n_fds);
v = m->windows + w;
assert(m->by_fd[fd_index].fd == v->fd);
if (m->by_fd[fd_index].windows != (unsigned) -1) {
assert(m->windows[m->by_fd[fd_index].windows].by_fd_prev == (unsigned) -1);
m->windows[m->by_fd[fd_index].windows].by_fd_prev = w;
}
v->by_fd_next = m->by_fd[fd_index].windows;
v->by_fd_prev = (unsigned) -1;
m->by_fd[fd_index].windows = w;
}
static void mmap_cache_fd_remove(MMapCache *m, unsigned fd_index, unsigned w) {
Window *v;
assert(m);
assert(fd_index < m->n_fds);
v = m->windows + w;
assert(m->by_fd[fd_index].fd == v->fd);
assert(v->by_fd_next == (unsigned) -1 || m->windows[v->by_fd_next].fd == v->fd);
assert(v->by_fd_prev == (unsigned) -1 || m->windows[v->by_fd_prev].fd == v->fd);
if (v->by_fd_prev == (unsigned) -1) {
assert(m->by_fd[fd_index].windows == w);
m->by_fd[fd_index].windows = v->by_fd_next;
} else {
assert(m->windows[v->by_fd_prev].by_fd_next == w);
m->windows[v->by_fd_prev].by_fd_next = v->by_fd_next;
}
if (v->by_fd_next != (unsigned) -1) {
assert(m->windows[v->by_fd_next].by_fd_prev == w);
m->windows[v->by_fd_next].by_fd_prev = v->by_fd_prev;
}
}
static void mmap_cache_context_unset(MMapCache *m, unsigned c) {
Window *v;
unsigned w;
assert(m);
assert(c < m->contexts_max);
if (m->by_context[c] == (unsigned) -1)
return;
w = m->by_context[c];
m->by_context[c] = (unsigned) -1;
v = m->windows + w;
assert(v->n_ref > 0);
v->n_ref --;
if (v->n_ref == 0)
mmap_cache_window_add_lru(m, w);
}
static void mmap_cache_context_set(MMapCache *m, unsigned c, unsigned w) {
Window *v;
assert(m);
assert(c < m->contexts_max);
assert(w < m->n_windows);
if (m->by_context[c] == w)
return;
mmap_cache_context_unset(m, c);
m->by_context[c] = w;
v = m->windows + w;
v->n_ref ++;
if (v->n_ref == 1)
mmap_cache_window_remove_lru(m, w);
}
static void mmap_cache_free(MMapCache *m) {
assert(m);
if (m->windows) {
unsigned w;
for (w = 0; w < m->n_windows; w++)
mmap_cache_window_unmap(m, w);
free(m->windows);
}
free(m->by_context);
free(m->by_fd);
free(m);
}
MMapCache* mmap_cache_new(void) {
MMapCache *m;
m = new0(MMapCache, 1);
if (!m)
return NULL;
m->contexts_max = DEFAULT_CONTEXTS_MAX;
m->fds_max = DEFAULT_FDS_MAX;
m->windows_max = DEFAULT_WINDOWS_MAX;
m->n_ref = 1;
m->lru_first = (unsigned) -1;
m->lru_last = (unsigned) -1;
m->windows = new(Window, m->windows_max);
if (!m->windows) {
mmap_cache_free(m);
return NULL;
}
m->by_context = new(unsigned, m->contexts_max);
if (!m->by_context) {
mmap_cache_free(m);
return NULL;
}
memset(m->by_context, -1, m->contexts_max * sizeof(unsigned));
m->by_fd = new(FileDescriptor, m->fds_max);
if (!m->by_fd) {
mmap_cache_free(m);
return NULL;
}
return m;
}
MMapCache* mmap_cache_ref(MMapCache *m) {
assert(m);
assert(m->n_ref > 0);
m->n_ref++;
return m;
}
MMapCache* mmap_cache_unref(MMapCache *m) {
assert(m);
assert(m->n_ref > 0);
if (m->n_ref == 1)
mmap_cache_free(m);
else
m->n_ref--;
return NULL;
}
static int mmap_cache_allocate_window(MMapCache *m, unsigned *w) {
Window *v;
unsigned fd_index;
assert(m);
assert(w);
if (m->n_windows < m->windows_max) {
*w = m->n_windows ++;
return 0;
}
if (m->lru_first == (unsigned) -1)
return -E2BIG;
*w = m->lru_first;
v = m->windows + *w;
assert(v->n_ref == 0);
mmap_cache_window_unmap(m, *w);
if (v->fd >= 0) {
assert_se(mmap_cache_peek_fd_index(m, v->fd, &fd_index) > 0);
mmap_cache_fd_remove(m, fd_index, *w);
}
mmap_cache_window_remove_lru(m, *w);
return 0;
}
static int mmap_cache_make_room(MMapCache *m) {
unsigned w;
assert(m);
w = m->lru_first;
while (w != (unsigned) -1) {
Window *v;
v = m->windows + w;
if (v->ptr) {
mmap_cache_window_unmap(m, w);
return 1;
}
w = v->lru_next;
}
return 0;
}
static int mmap_cache_put(
MMapCache *m,
int fd,
unsigned fd_index,
int prot,
unsigned context,
uint64_t offset,
uint64_t size,
void **ret) {
unsigned w;
Window *v;
void *d;
uint64_t woffset, wsize;
int r;
assert(m);
assert(fd >= 0);
assert(context < m->contexts_max);
assert(size > 0);
assert(ret);
woffset = offset & ~((uint64_t) page_size() - 1ULL);
wsize = size + (offset - woffset);
wsize = PAGE_ALIGN(wsize);
if (wsize < WINDOW_SIZE) {
uint64_t delta;
delta = PAGE_ALIGN((WINDOW_SIZE - wsize) / 2);
if (delta > offset)
woffset = 0;
else
woffset -= delta;
wsize = WINDOW_SIZE;
}
for (;;) {
d = mmap(NULL, wsize, prot, MAP_SHARED, fd, woffset);
if (d != MAP_FAILED)
break;
if (errno != ENOMEM)
return -errno;
r = mmap_cache_make_room(m);
if (r < 0)
return r;
if (r == 0)
return -ENOMEM;
}
r = mmap_cache_allocate_window(m, &w);
if (r < 0) {
munmap(d, wsize);
return r;
}
v = m->windows + w;
v->fd = fd;
v->ptr = d;
v->offset = woffset;
v->size = wsize;
v->n_ref = 0;
mmap_cache_window_add_lru(m, w);
mmap_cache_fd_add(m, fd_index, w);
mmap_cache_context_set(m, context, w);
*ret = (uint8_t*) d + (offset - woffset);
return 1;
}
static int fd_cmp(const void *_a, const void *_b) {
const FileDescriptor *a = _a, *b = _b;
if (a->fd < b->fd)
return -1;
if (a->fd > b->fd)
return 1;
return 0;
}
static int mmap_cache_peek_fd_index(MMapCache *m, int fd, unsigned *fd_index) {
FileDescriptor *j;
unsigned r;
assert(m);
assert(fd >= 0);
assert(fd_index);
for (r = 0; r < m->n_fds; r++)
assert(m->by_fd[r].windows == (unsigned) -1 ||
m->windows[m->by_fd[r].windows].fd == m->by_fd[r].fd);
j = bsearch(&fd, m->by_fd, m->n_fds, sizeof(FileDescriptor), fd_cmp);
if (!j)
return 0;
*fd_index = (unsigned) (j - m->by_fd);
return 1;
}
static int mmap_cache_get_fd_index(MMapCache *m, int fd, unsigned *fd_index) {
FileDescriptor *j;
int r;
assert(m);
assert(fd >= 0);
assert(fd_index);
r = mmap_cache_peek_fd_index(m, fd, fd_index);
if (r != 0)
return r;
if (m->n_fds >= m->fds_max) {
unsigned k;
FileDescriptor *n;
k = m->n_fds * 2;
n = realloc(m->by_fd, sizeof(FileDescriptor) * k);
if (!n)
return -ENOMEM;
m->fds_max = k;
m->by_fd = n;
}
j = m->by_fd + m->n_fds ++;
j->fd = fd;
j->windows = (unsigned) -1;
qsort(m->by_fd, m->n_fds, sizeof(FileDescriptor), fd_cmp);
return mmap_cache_peek_fd_index(m, fd, fd_index);
}
static bool mmap_cache_test_window(
MMapCache *m,
unsigned w,
uint64_t offset,
uint64_t size) {
Window *v;
assert(m);
assert(w < m->n_windows);
assert(size > 0);
v = m->windows + w;
return offset >= v->offset &&
offset + size <= v->offset + v->size;
}
static int mmap_cache_current(
MMapCache *m,
int fd,
unsigned context,
uint64_t offset,
uint64_t size,
void **ret) {
Window *v;
unsigned w;
assert(m);
assert(fd >= 0);
assert(context < m->contexts_max);
assert(size > 0);
assert(ret);
if (m->by_context[context] == (unsigned) -1)
return 0;
w = m->by_context[context];
v = m->windows + w;
if (v->fd != fd)
return 0;
if (!mmap_cache_test_window(m, w, offset, size))
return 0;
*ret = (uint8_t*) v->ptr + (offset - v->offset);
return 1;
}
static int mmap_cache_find(
MMapCache *m,
int fd,
unsigned fd_index,
unsigned context,
uint64_t offset,
uint64_t size,
void **ret) {
Window *v = NULL;
unsigned w;
assert(m);
assert(fd >= 0);
assert(fd_index < m->n_fds);
assert(context < m->contexts_max);
assert(size > 0);
assert(ret);
w = m->by_fd[fd_index].windows;
while (w != (unsigned) -1) {
v = m->windows + w;
assert(v->fd == fd);
if (mmap_cache_test_window(m, w, offset, size))
break;
w = v->by_fd_next;
}
if (w == (unsigned) -1)
return 0;
mmap_cache_context_set(m, context, w);
*ret = (uint8_t*) v->ptr + (offset - v->offset);
return 1;
}
int mmap_cache_get(
MMapCache *m,
int fd,
int prot,
unsigned context,
uint64_t offset,
uint64_t size,
void **ret) {
unsigned fd_index;
int r;
assert(m);
assert(fd >= 0);
assert(size > 0);
assert(ret);
if (context >= m->contexts_max) {
unsigned k, *n;
Window *w;
/* Increase the number of contexts if necessary, and
* make sure we have twice the number of windows */
k = context * 2;
n = realloc(m->by_context, sizeof(unsigned) * k);
if (!n)
return -ENOMEM;
memset(n + m->contexts_max, -1, (k - m->contexts_max) * sizeof(unsigned));
m->contexts_max = k;
m->by_context = n;
k = MAX(m->windows_max, m->contexts_max*2);
w = realloc(m->windows, sizeof(Window) * k);
if (!w)
return -ENOMEM;
m->windows_max = k;
m->windows = w;
}
/* Maybe the current pointer for this context is already the
* right one? */
r = mmap_cache_current(m, fd, context, offset, size, ret);
if (r != 0)
return r;
/* Hmm, drop the reference to the current one, since it wasn't
* good enough */
mmap_cache_context_unset(m, context);
/* OK, let's find the chain for this FD */
r = mmap_cache_get_fd_index(m, fd, &fd_index);
if (r < 0)
return r;
/* And let's look through the available mmaps */
r = mmap_cache_find(m, fd, fd_index, context, offset, size, ret);
if (r != 0)
return r;
/* Not found? Then, let's add it */
return mmap_cache_put(m, fd, fd_index, prot, context, offset, size, ret);
}
void mmap_cache_close_fd(MMapCache *m, int fd) {
unsigned fd_index, c, w;
int r;
assert(m);
assert(fd > 0);
r = mmap_cache_peek_fd_index(m, fd, &fd_index);
if (r <= 0)
return;
for (c = 0; c < m->contexts_max; c++) {
w = m->by_context[c];
if (w == (unsigned) -1)
continue;
if (m->windows[w].fd == fd)
mmap_cache_context_unset(m, c);
}
w = m->by_fd[fd_index].windows;
while (w != (unsigned) -1) {
Window *v;
v = m->windows + w;
assert(v->fd == fd);
mmap_cache_window_unmap(m, w);
mmap_cache_fd_remove(m, fd_index, w);
v->fd = -1;
w = m->by_fd[fd_index].windows;
}
memmove(m->by_fd + fd_index, m->by_fd + fd_index + 1, (m->n_fds - (fd_index + 1)) * sizeof(FileDescriptor));
m->n_fds --;
}
void mmap_cache_close_fd_range(MMapCache *m, int fd, uint64_t p) {
unsigned fd_index, c, w;
int r;
assert(m);
assert(fd > 0);
/* This drops all windows that include space right of the
* specified offset. This is useful to ensure that after the
* file size is extended we drop our mappings of the end and
* create it anew, since otherwise it is undefined whether
* mapping will continue to work as intended. */
r = mmap_cache_peek_fd_index(m, fd, &fd_index);
if (r <= 0)
return;
for (c = 0; c < m->contexts_max; c++) {
w = m->by_context[c];
if (w != (unsigned) -1 && m->windows[w].fd == fd)
mmap_cache_context_unset(m, c);
}
w = m->by_fd[fd_index].windows;
while (w != (unsigned) -1) {
Window *v;
v = m->windows + w;
assert(v->fd == fd);
assert(v->by_fd_next == (unsigned) -1 ||
m->windows[v->by_fd_next].fd == fd);
if (v->offset + v->size > p) {
mmap_cache_window_unmap(m, w);
mmap_cache_fd_remove(m, fd_index, w);
v->fd = -1;
w = m->by_fd[fd_index].windows;
} else
w = v->by_fd_next;
}
}
void mmap_cache_close_context(MMapCache *m, unsigned context) {
mmap_cache_context_unset(m, context);
}
|