/
umap.cpp
1134 lines (965 loc) · 31.1 KB
/
umap.cpp
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
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* This file is part of UMAP.
*
* For copyright information see the COPYRIGHT file in the top level directory,
* or at https://github.com/LLNL/umap/blob/master/COPYRIGHT.
*
* This program 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) version 2.1 dated February 1999.
*
* This program 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 terms and conditions of the GNU
* Lesser General Public License for more details. You should have received a
* copy of the GNU Lesser General Public License along with this program;
* if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite
* 330, Boston, MA 02111-1307 USA
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif // _GNU_SOURCE
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <iostream>
#include <cstdint>
#include <cinttypes>
#include <vector>
#include <algorithm>
#include <thread>
#include <unordered_map>
#include <sstream>
#include <fstream>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h> // open/close
#include <unistd.h> // sysconf()
#include <sys/syscall.h> // syscall()
#include <sys/mman.h> // mmap()
#include <poll.h> // poll()
#include <assert.h>
#include <linux/userfaultfd.h>
#include <utmpx.h> // sched_getcpu()
#include <signal.h>
#include <cstring>
#include <sys/prctl.h>
#include "umap/umap.h" // API to library
#include "umap/Store.h"
#include "config.h"
#include "spindle_debug.h"
#ifndef UFFDIO_COPY_MODE_WP
#define UMAP_RO_MODE
#endif
/*
* Note: this implementation is multi-threaded, but the data structures are
* not shared between threads.
*/
using namespace std;
const int umap_Version_Major = UMAP_VERSION_MAJOR;
const int umap_Version_Minor = UMAP_VERSION_MINOR;
const int umap_Version_Patch = UMAP_VERSION_PATCH;
static const int UMAP_UFFD_MAX_MESSAGES = 256;
static uint64_t uffd_threads;
static uint64_t umap_buffer_size;
static long umapPageSize;
class umap_page;
struct umap_PageBlock;
class umap_page_buffer;
class umap_stats;
class UserFaultHandler;
//
// |------------------------- umap() provided Region ----------------------------|
// |------------------------- umap() provided backing file(s) -------------------|
// |- Page Block 1 -|- Page Block 2 -|- ... -|- Page Block N-1 -|- Page Block N -|
//
// _umap organizes a region of memory into a set of blocks of pages. The blocks
// of pages are then distributed evenly to a set of UserFaultHandler objects.
//
class _umap {
friend UserFaultHandler;
public:
_umap(void* _mmap_region, uint64_t _mmap_rsize,
void* _umap_region, uint64_t _umap_rsize,
int fd, Store* _store_);
~_umap();
static inline void* UMAP_PAGE_BEGIN(const void* a) {
return (void*)( (uint64_t)a & ~(umapPageSize-1) );
}
void flushbuffers( void );
vector<UserFaultHandler*> ufault_handlers;
private:
void* mmapRegion;
uint64_t mmapRegionSize;
void* umapRegion;
uint64_t umapRegionSize;
bool uffd_time_to_stop_working;
Store* store;
};
class UserFaultHandler {
friend _umap;
friend umap_page_buffer;
public:
UserFaultHandler(_umap* _um, const vector<umap_PageBlock>& _pblks, uint64_t _pbuf_size);
~UserFaultHandler(void);
void stop_uffd_worker( void ) noexcept {
_u->uffd_time_to_stop_working = true;
uffd_worker->join();
};
bool page_is_in_umap(const void* page_begin);
umap_page_buffer* get_pagebuffer() { return pagebuffer; }
void flushbuffers( void );
void resetstats( void );
umap_stats* stat;
private:
_umap* _u;
vector<umap_PageBlock> PageBlocks;
uint64_t pbuf_size;
umap_page_buffer* pagebuffer;
vector<struct uffd_msg> umessages;
int userfault_fd;
char* copyin_buf;
thread* uffd_worker;
void evict_page(umap_page* page);
void uffd_handler(void);
void pagefault_event(const struct uffd_msg& msg);
#ifndef UMAP_RO_MODE
void enable_wp_on_pages_and_wake(uint64_t, int64_t);
void disable_wp_on_pages(uint64_t, int64_t, bool);
#endif
};
class umap_stats {
public:
umap_stats():
dirty_evicts{0},
clean_evicts{0},
evict_victims{0},
wp_messages{0},
read_faults{0},
write_faults{0},
sigbus{0},
stuck_wp{0},
dropped_dups{0}
{};
uint64_t dirty_evicts;
uint64_t clean_evicts;
uint64_t evict_victims;
uint64_t wp_messages;
uint64_t read_faults;
uint64_t write_faults;
uint64_t sigbus;
uint64_t stuck_wp;
uint64_t dropped_dups;
};
struct umap_PageBlock {
void* base;
uint64_t length;
};
class umap_page_buffer {
public:
umap_page_buffer(UserFaultHandler* _ufh_, uint64_t pbuffersize);
~umap_page_buffer();
umap_page* alloc_page_desc(void* page);
void dealloc_page_desc( void );
bool pages_still_present( void );
umap_page* find_inmem_page_desc(void* page_addr);
private:
uint64_t page_buffer_size;
uint64_t page_buffer_alloc_idx;
uint64_t page_buffer_free_idx;
uint64_t page_buffer_alloc_cnt;
unordered_map<void*, umap_page*> inmem_page_map;
umap_page* page_descriptor_array;
UserFaultHandler* ufh;
};
struct umap_page {
bool page_is_dirty() { return dirty; }
void mark_page_dirty() { dirty = true; }
void mark_page_clean() { dirty = false; }
void* get_page(void) { return page; }
void set_page(void* _p);
void* page;
bool dirty;
};
static unordered_map<void*, _umap*> active_umaps;
static inline bool required_uffd_features_present(int fd)
{
struct uffdio_api uffdio_api = {
.api = UFFD_API,
#ifdef UMAP_RO_MODE
.features = 0
#else
.features = UFFD_FEATURE_PAGEFAULT_FLAG_WP
#endif
};
if (ioctl(fd, UFFDIO_API, &uffdio_api) == -1) {
perror("ERROR: UFFDIO_API Failed: ");
return false;
}
#ifndef UMAP_RO_MODE
if ( !(uffdio_api.features & UFFD_FEATURE_PAGEFAULT_FLAG_WP) ) {
cerr << "UFFD Compatibilty Check - unsupported userfaultfd WP\n";
return false;
}
#endif
return true;
}
//
// Library Interface Entry
//
static int check_uffd_compatibility( void )
{
int fd;
if ((fd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK)) < 0) {
perror("UFFD Compatibilty Check - Unable to open userfaultfd: ");
exit(1);
}
if ( ! required_uffd_features_present(fd) )
exit(1);
close(fd);
return 0;
}
static inline long get_max_buf_size( void )
{ static unsigned long total_mem_kb = 0;
const unsigned long oneK = 1024;
const unsigned long percentageToAllocate = 80; // 80% of memory is max
// Lazily set total_mem_kb global
if ( ! total_mem_kb ) {
string token;
ifstream file("/proc/meminfo");
while (file >> token) {
if (token == "MemTotal:") {
unsigned long mem;
if (file >> mem) {
total_mem_kb = mem;
} else {
cerr << "UMAP unable to determine system memory size\n";
total_mem_kb = oneK * oneK;
}
}
// ignore rest of the line
file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
return ((total_mem_kb / (umapPageSize / oneK)) * percentageToAllocate) / 100;
}
void* umap(void* base_addr, uint64_t region_size, int prot, int flags,
int fd, off_t offset)
{
return umap_ex(base_addr, region_size, prot, flags, fd, 0, nullptr);
}
void* umap_ex(void* base_addr, uint64_t region_size, int prot, int flags,
int fd, off_t offset, Store* _store_)
{
if (check_uffd_compatibility() < 0)
return NULL;
if ( (region_size % umapPageSize) ) {
cerr << "UMAP: Region size " << region_size << " is not a multple of umapPageSize (" << umapPageSize << ")\n";
return NULL;
}
if ( ((uint64_t)base_addr & (umapPageSize - 1)) ) {
cerr << "umap: base_addr must be page aligned: " << base_addr
<< ", page size is: " << umapPageSize << endl;
return NULL;
}
if (!(flags & UMAP_PRIVATE) || flags & ~(UMAP_PRIVATE|UMAP_FIXED)) {
cerr << "umap: Invalid flags: " << hex << flags << endl;
return UMAP_FAILED;
}
//
// When dealing with umap-page-sizes that could be multiples of the actual
// system-page-size, it is possible for mmap() to provide a region that is on
// a system-page-boundary, but not necessarily on a umap-page-size boundary.
//
// We always allocate an additional umap-page-size set of bytes so that we can
// make certain that the umap-region begins on a umap-page-size boundary.
//
uint64_t mmap_size = region_size + umapPageSize;
void* mmap_region = mmap(base_addr, mmap_size,
prot, flags | (MAP_ANONYMOUS | MAP_NORESERVE), -1, 0);
if (mmap_region == MAP_FAILED) {
perror("ERROR: mmap failed: ");
return UMAP_FAILED;
}
void* umap_region = _umap::UMAP_PAGE_BEGIN((void*)((uint64_t)mmap_region + (umapPageSize-1)));
uint64_t umap_size = region_size;
try {
active_umaps[umap_region] = new _umap{mmap_region, mmap_size,
umap_region, umap_size, fd, _store_};
} catch(const std::exception& e) {
cerr << __FUNCTION__ << " Failed to launch _umap: " << e.what() << endl;
return UMAP_FAILED;
} catch(...) {
cerr << "umap failed to instantiate _umap object\n";
return UMAP_FAILED;
}
return umap_region;
}
int uunmap(void* addr, uint64_t length)
{
auto it = active_umaps.find(addr);
if (it != active_umaps.end()) {
struct umap_cfg_stats st;
umap_cfg_get_stats(addr, &st);
debug_printf( "\n\t"
"Dirty Evictions: %" PRIu64 "\n\t"
"Clean Evictions: %" PRIu64 "\n\t"
" Evict Victims: %" PRIu64 "\n\t"
" WP Messages: %" PRIu64 "\n\t"
" Read Faults: %" PRIu64 "\n\t"
" Write Faults: %" PRIu64 "\n\t"
" SIGBUS Errors: %" PRIu64 "\n\t"
" Stuck WP: %" PRIu64 "\n\t"
" Dropped Dups: %" PRIu64 "\n",
st.dirty_evicts,
st.clean_evicts,
st.evict_victims,
st.wp_messages,
st.read_faults,
st.write_faults,
st.sigbus,
st.stuck_wp,
st.dropped_dups);
delete it->second;
active_umaps.erase(it);
}
return 0;
}
uint64_t* umap_cfg_readenv(const char* env, uint64_t* val) {
// return a pointer to val on success, null on failure
char* val_ptr = 0;
if ( (val_ptr = getenv(env)) ) {
uint64_t env_val = 0;
if (sscanf(val_ptr, "%" PRIu64, &env_val)) {
*val = env_val;
return val;
}
}
return 0;
}
void umap_cfg_getenv( void ) {
uint64_t env_value = 0;
if ( (umap_cfg_readenv("UMAP_UFFD_THREADS", &env_value)) ) {
umap_cfg_set_uffdthreads(env_value);
}
if ( (umap_cfg_readenv("UMAP_BUFSIZE", &env_value)) ) {
umap_cfg_set_bufsize(env_value);
}
if ( (umap_cfg_readenv("UMAP_PAGESIZE", &env_value)) ) {
umap_cfg_set_pagesize(env_value);
}
}
uint64_t umap_cfg_get_bufsize( void )
{
return umap_buffer_size;
}
void umap_cfg_set_bufsize( uint64_t page_bufsize )
{
long max_size = get_max_buf_size();
long old_size = umap_buffer_size;
if ( page_bufsize > max_size ) {
debug_printf("Bufsize of %" PRIu64 " larger than maximum of %ld. Setting to %ld\n",
page_bufsize, max_size, max_size);
umap_buffer_size = max_size;
}
else {
umap_buffer_size = page_bufsize;
}
debug_printf("Bufsize changed from %ld to %lu pages\n", old_size, umap_buffer_size);
}
uint64_t umap_cfg_get_uffdthreads( void )
{
return uffd_threads;
}
void umap_cfg_set_uffdthreads( uint64_t numthreads )
{
uffd_threads = numthreads;
}
void umap_cfg_flush_buffer( void* region )
{
auto it = active_umaps.find(region);
if (it != active_umaps.end())
it->second->flushbuffers();
}
int umap_cfg_get_pagesize()
{
return umapPageSize;
}
int umap_cfg_set_pagesize( long psize )
{
long sys_psize = sysconf(_SC_PAGESIZE);
/*
* Must be multiple of system page size
*/
if ( psize % sys_psize ) {
cerr << "Specified page size (" << psize << ") must be a multiple of system page size (" << sys_psize << ")\n";
return -1;
}
debug_printf("Adjusting page size from %ld to %ld\n", umapPageSize, psize);
umapPageSize = psize;
return 0;
}
void umap_cfg_get_stats(void* region, struct umap_cfg_stats* stats)
{
auto it = active_umaps.find(region);
if (it != active_umaps.end()) {
stats->dirty_evicts = 0;
stats->clean_evicts = 0;
stats->evict_victims = 0;
stats->wp_messages = 0;
stats->read_faults = 0;
stats->write_faults = 0;
stats->sigbus = 0;
stats->stuck_wp = 0;
stats->dropped_dups = 0;
for ( auto handler : it->second->ufault_handlers ) {
stats->dirty_evicts += handler->stat->dirty_evicts;
stats->clean_evicts += handler->stat->clean_evicts;
stats->evict_victims += handler->stat->evict_victims;
stats->wp_messages += handler->stat->wp_messages;
stats->read_faults += handler->stat->read_faults;
stats->write_faults += handler->stat->write_faults;
stats->sigbus += handler->stat->sigbus;
stats->stuck_wp += handler->stat->stuck_wp;
stats->dropped_dups += handler->stat->dropped_dups;
}
}
}
void umap_cfg_reset_stats(void* region)
{
auto it = active_umaps.find(region);
if (it != active_umaps.end()) {
for ( auto handler : it->second->ufault_handlers )
handler->resetstats();
}
}
//
// Signal Handlers
//
static struct sigaction saved_sa;
void sighandler(int signum, siginfo_t *info, void* buf)
{
if (signum != SIGBUS) {
err_printf("Unexpected signal: %d received\n", signum);
exit(1);
}
//assert("UMAP: SIGBUS Error Unexpected" && 0);
void* page_begin = _umap::UMAP_PAGE_BEGIN(info->si_addr);
for (auto it : active_umaps) {
for (auto ufh : it.second->ufault_handlers) {
if (ufh->page_is_in_umap(page_begin)) {
ufh->stat->sigbus++;
if (ufh->get_pagebuffer()->find_inmem_page_desc(page_begin) != nullptr)
debug_printf("SIGBUS %p (page=%p) present\n", info->si_addr, page_begin);
else
debug_printf("SIGBUS %p (page=%p) not present\n", info->si_addr, page_begin);
return;
}
}
}
err_printf("SIGBUS %p (page=%p) ADDRESS OUTSIDE OF UMAP RANGE\n", info->si_addr, page_begin);
assert("UMAP: SIGBUS for out of range address" && 0);
}
void __attribute ((constructor)) init_umap_lib( void )
{
struct sigaction act;
LOGGING_INIT;
if ((umapPageSize = sysconf(_SC_PAGESIZE)) == -1) {
perror("ERROR: sysconf(_SC_PAGESIZE)");
throw -1;
}
umap_buffer_size = get_max_buf_size();
unsigned int n = std::thread::hardware_concurrency();
uffd_threads = (n == 0) ? 16 : n;
act.sa_handler = NULL;
act.sa_sigaction = sighandler;
if (sigemptyset(&act.sa_mask) == -1) {
perror("ERROR: sigemptyset: ");
exit(1);
}
act.sa_flags = SA_NODEFER | SA_SIGINFO;
if (sigaction(SIGBUS, &act, &saved_sa) == -1) {
perror("ERROR: sigaction: ");
exit(1);
}
umap_cfg_getenv();
LOGGING_FINI;
}
void __attribute ((destructor)) fine_umap_lib( void )
{
if (sigaction(SIGBUS, &saved_sa, NULL) == -1) {
perror("ERROR: sigaction restore: ");
exit(1);
}
for (auto it : active_umaps) {
delete it.second;
}
}
//
// _umap class implementation
//
_umap::_umap( void* _mmap_region,
uint64_t _mmap_rsize,
void* _umap_region,
uint64_t _umap_rsize,
int fd,
Store* _store_) :
mmapRegion{_mmap_region}, mmapRegionSize{_mmap_rsize},
umapRegion{_umap_region}, umapRegionSize{_umap_rsize},
uffd_time_to_stop_working{false}, store{_store_}
{
if ( store == nullptr )
store = Store::make_store(umapRegion, umapRegionSize, umapPageSize, fd);
uint64_t region_pages = umapRegionSize / umapPageSize;
// Shrink buffer size to fit requested region if needed
uint64_t buffer_adjusted_pages = std::min(umap_buffer_size, region_pages);
// Shrink # of workers if there are too few pages to make it worth it.
uint64_t num_workers = std::min(buffer_adjusted_pages, uffd_threads);
uint64_t buffer_pages_per_worker = buffer_adjusted_pages / num_workers;
uint64_t buffer_residual_pages = buffer_adjusted_pages % num_workers;
uint64_t region_pages_per_worker = region_pages / num_workers;
uint64_t region_residual_pages = region_pages % num_workers;
stringstream ss;
ss << "umap("
<< umapRegion << " - " << (void*)((char*)umapRegion+umapRegionSize) << ")\n\t"
<< umapPageSize << " Page Size\n\t"
<< umap_buffer_size << " UMAP Buffer Size in Pages\n\t"
<< region_pages << " Requested Region Pages\n\t"
<< buffer_adjusted_pages << " Adjusted UMAP Buffer Size in Pages\n\t"
<< uffd_threads << " Configured Maximum UMAP Threads\n\t"
<< num_workers << " UMAP Threads Allocated\n\t"
<< buffer_pages_per_worker << " Buffer Pages per worker\n\t"
<< buffer_residual_pages << " Residual Buffer pages\n\t"
<< region_pages_per_worker << " Region Pages per worker\n\t"
<< region_residual_pages << " Risidual Buffer pages"
<< endl;
debug_printf("%s\n", ss.str().c_str());
try {
uint64_t region_offset = 0;
for (uint64_t worker = 0; worker < num_workers; ++worker) {
umap_PageBlock pb;
uint64_t worker_region_pages = region_pages_per_worker;
uint64_t worker_buffer_pages = buffer_pages_per_worker;
//
// Distribute residual buffer pages across workers
//
if (buffer_residual_pages) {
buffer_residual_pages--;
worker_buffer_pages++;
}
//
// Distribute residual buffer pages across workers
//
if (region_residual_pages) {
region_residual_pages--;
worker_region_pages++;
}
pb.base = (void*)((uint64_t)umapRegion + (region_offset * umapPageSize));
pb.length = worker_region_pages * umapPageSize;
vector<umap_PageBlock> segs{ pb };
ufault_handlers.push_back( new UserFaultHandler{this, segs, worker_buffer_pages} );
region_offset += worker_region_pages;
}
} catch(const std::exception& e) {
cerr << __FUNCTION__ << " Failed to launch _umap: " << e.what() << endl;
throw -1;
} catch(...) {
cerr << "umap failed to instantiate _umap object\n";
throw -1;
}
}
void _umap::flushbuffers( void )
{
for ( auto handler : ufault_handlers )
handler->flushbuffers();
}
_umap::~_umap(void)
{
for ( auto handler : ufault_handlers )
handler->stop_uffd_worker();
for ( auto handler : ufault_handlers )
delete handler;
if (munmap(mmapRegion, mmapRegionSize)) {
perror("munmap failed: ");
}
}
UserFaultHandler::UserFaultHandler(_umap* _um, const vector<umap_PageBlock>& _pblks, uint64_t _pbuf_size)
:
stat{ new umap_stats },
_u{_um},
PageBlocks{_pblks},
pbuf_size{_pbuf_size},
pagebuffer{ new umap_page_buffer{this, _pbuf_size} }
{
umessages.resize(UMAP_UFFD_MAX_MESSAGES);
if (posix_memalign((void**)©in_buf, (uint64_t)umapPageSize, (umapPageSize * 2))) {
cerr << "ERROR: posix_memalign: failed\n";
exit(1);
}
if (copyin_buf == nullptr) {
cerr << "Unable to allocate " << (umapPageSize * 2) << " bytes for temporary buffer\n";
exit(1);
}
if ((userfault_fd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK)) < 0) {
perror("ERROR: userfaultfd syscall not available in this kernel");
throw -1;
}
if ( ! required_uffd_features_present(userfault_fd) )
exit(1);
for ( auto seg : PageBlocks ) {
struct uffdio_register uffdio_register = {
.range = {.start = (uint64_t)seg.base, .len = seg.length},
#ifndef UMAP_RO_MODE
.mode = UFFDIO_REGISTER_MODE_MISSING | UFFDIO_REGISTER_MODE_WP
#else
.mode = UFFDIO_REGISTER_MODE_MISSING
#endif
};
debug_printf2("Register %lu Pages from: %p - %p\n",
(seg.length / umapPageSize), seg.base,
(void*)((uint64_t)seg.base + (uint64_t)(seg.length-1)));
if (ioctl(userfault_fd, UFFDIO_REGISTER, &uffdio_register) == -1) {
perror("ERROR: ioctl/uffdio_register");
close(userfault_fd);
throw -1;
}
if ((uffdio_register.ioctls & UFFD_API_RANGE_IOCTLS) != UFFD_API_RANGE_IOCTLS) {
cerr << "unexpected userfaultfd ioctl set\n";
close(userfault_fd);
throw -1;
}
}
uffd_worker = new thread{&UserFaultHandler::uffd_handler, this};
}
UserFaultHandler::~UserFaultHandler(void)
{
//
// Now that all of our worker threads have stopped, we can flush everything
//
for ( auto seg : PageBlocks ) {
struct uffdio_register uffdio_register;
uffdio_register.range.start = (uint64_t)seg.base;
uffdio_register.range.len = seg.length;
if (ioctl(userfault_fd, UFFDIO_UNREGISTER, &uffdio_register.range)) {
perror("ERROR: UFFDIO_UNREGISTER");
exit(1);
}
}
free(copyin_buf);
delete pagebuffer;
delete stat;
delete uffd_worker;
}
struct less_than_key
{
inline bool operator() (const struct uffd_msg& lhs, const struct uffd_msg& rhs)
{
if (lhs.arg.pagefault.address == rhs.arg.pagefault.address)
return (lhs.arg.pagefault.flags >= rhs.arg.pagefault.address);
else
return (lhs.arg.pagefault.address < rhs.arg.pagefault.address);
}
};
void UserFaultHandler::uffd_handler(void)
{
prctl(PR_SET_NAME, "UMAP UFFD Hdlr", 0, 0, 0);
for (;;) {
struct pollfd pollfd[1];
pollfd[0].fd = userfault_fd;
pollfd[0].events = POLLIN;
if (_u->uffd_time_to_stop_working) {
flushbuffers();
return;
}
// wait for a userfaultfd event to occur
int pollres = poll(pollfd, 1, 2000);
switch (pollres) {
case -1:
perror("ERROR: poll/userfaultfd");
continue;
case 0:
continue;
case 1:
break;
default:
cerr << __FUNCTION__ << " unexpected uffdio poll result\n";
exit(1);
}
if (pollfd[0].revents & POLLERR) {
cerr << __FUNCTION__ << " POLLERR\n";
exit(1);
}
if ( !(pollfd[0].revents & POLLIN) )
continue;
int readres = read(userfault_fd, &umessages[0], UMAP_UFFD_MAX_MESSAGES * sizeof(struct uffd_msg));
if (readres == -1) {
if (errno == EAGAIN)
continue;
perror("ERROR: read/userfaultfd");
exit(1);
}
assert(readres % sizeof(struct uffd_msg) == 0);
int msgs = readres / sizeof(struct uffd_msg);
if (msgs < 1) {
cerr << __FUNCTION__ << "invalid msg size " << readres << " " << msgs;
exit(1);
}
sort(umessages.begin(), umessages.begin()+msgs, less_than_key());
uint64_t last_addr = 0;
for (int i = 0; i < msgs; ++i) {
if (umessages[i].event != UFFD_EVENT_PAGEFAULT) {
cerr << __FUNCTION__ << " Unexpected event " << hex << umessages[i].event << endl;
continue;
}
if (umessages[i].arg.pagefault.address == last_addr) {
stat->dropped_dups++;
continue; // Skip pages we have already copied in
}
last_addr = umessages[i].arg.pagefault.address;
pagefault_event(umessages[i]); // At this point, we know we have had a page fault. Let's handle it.
}
}
}
void UserFaultHandler::pagefault_event(const struct uffd_msg& msg)
{
void* page_begin = _umap::UMAP_PAGE_BEGIN( (void*)msg.arg.pagefault.address );
umap_page* pm = pagebuffer->find_inmem_page_desc(page_begin);
if (pm != nullptr) {
#ifndef UMAP_RO_MODE
if (msg.arg.pagefault.flags & (UFFD_PAGEFAULT_FLAG_WP | UFFD_PAGEFAULT_FLAG_WRITE)) {
if (!pm->page_is_dirty()) {
pm->mark_page_dirty();
disable_wp_on_pages((uint64_t)page_begin, 1, false);
stat->wp_messages++;
debug_printf2("Present page written, marking %p dirty\n", page_begin);
}
else if (msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WP) {
struct uffdio_copy copy;
copy.src = (uint64_t)copyin_buf;
copy.dst = (uint64_t)page_begin;
copy.len = umapPageSize;
copy.mode = 0; // No WP
stat->stuck_wp++;
pm->mark_page_clean();
memcpy(copyin_buf, page_begin, umapPageSize); // Save our data
evict_page(pm); // Evict ourselves
pm->set_page(page_begin); // Bring ourselves back in
pm->mark_page_dirty(); // Will be dirty when write retries
if (ioctl(userfault_fd, UFFDIO_COPY, ©) == -1) {
perror("ERROR WP Workaround: ioctl(UFFDIO_COPY)");
exit(1);
}
debug_printf2("Present page stuck, EVICT WORKAROUND %p\n", page_begin);
}
}
#else
if ( msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WRITE ) {
assert("Write operation not allowed without WP support" && 0);
}
#endif
else {
debug_printf2("Spurious fault for page %p which is already present\n",
page_begin);
}
return;
}
//
// Page not present, read it in and (potentially) evict someone
//
off_t offset=(uint64_t)page_begin - (uint64_t)_u->umapRegion;
if (_u->store->read_from_store(copyin_buf, umapPageSize, offset) == -1) {
perror("ERROR: read_from_store failed");
exit(1);
}
/*
* Keep trying to obtain a free page descriptor until we get one..
*/
for ( pm = pagebuffer->alloc_page_desc(page_begin);
pm == nullptr;
pm = pagebuffer->alloc_page_desc(page_begin))
{
pagebuffer->dealloc_page_desc();
}
struct uffdio_copy copy;
copy.src = (uint64_t)copyin_buf;
copy.dst = (uint64_t)page_begin;
copy.len = umapPageSize;
copy.mode = 0;
#ifndef UMAP_RO_MODE
if (msg.arg.pagefault.flags & (UFFD_PAGEFAULT_FLAG_WP | UFFD_PAGEFAULT_FLAG_WRITE)) {
debug_printf3("Write Fault: Copying in dirty page %p\n", page_begin);
stat->write_faults++;
pm->mark_page_dirty();
if (ioctl(userfault_fd, UFFDIO_COPY, ©) == -1) {
perror("ERROR: ioctl(UFFDIO_COPY nowake)");
exit(1);
}
}
#else
if (msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WRITE) {
assert("Write operation not allowed without WP support" && 0);
}
#endif
else {
debug_printf3("Read Fault: Copying in page %p\n", page_begin);
stat->read_faults++;
pm->mark_page_clean();
#ifndef UMAP_RO_MODE
copy.mode = UFFDIO_COPY_MODE_WP;
#else
copy.mode = 0;
#endif
if (ioctl(userfault_fd, UFFDIO_COPY, ©) == -1) {
perror("ERROR: ioctl(UFFDIO_COPY nowake)");
exit(1);
}
assert(memcmp(copyin_buf, page_begin, umapPageSize) == 0);
}
}
bool UserFaultHandler::page_is_in_umap(const void* page_begin)
{
for ( auto it : PageBlocks )
if (page_begin >= it.base && page_begin < (void*)((uint64_t)it.base + it.length))
return true;
return false;
}
void UserFaultHandler::flushbuffers( void )
{
while (pagebuffer->pages_still_present() == true)
pagebuffer->dealloc_page_desc();
}
void UserFaultHandler::resetstats( void )
{
stat->dirty_evicts = 0;
stat->clean_evicts = 0;
stat->evict_victims = 0;
stat->wp_messages = 0;
stat->read_faults = 0;
stat->write_faults = 0;
stat->sigbus = 0;
stat->stuck_wp = 0;
stat->dropped_dups = 0;
}
void UserFaultHandler::evict_page(umap_page* pb)
{
uint64_t* page = (uint64_t*)pb->get_page();
stat->evict_victims++;
if (pb->page_is_dirty()) {