1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "berberis/base/memfd_backed_mmap.h"
18 
19 #include <sys/mman.h>
20 #include <unistd.h>
21 
22 #include "berberis/base/fd.h"
23 #include "berberis/base/large_mmap.h"
24 #include "berberis/base/logging.h"
25 #include "berberis/base/mmap.h"
26 
27 namespace berberis {
28 
29 // Creates memfd region of memfd_file_size bytes filled with value.
CreateAndFillMemfd(const char * name,size_t memfd_file_size,uintptr_t value)30 int CreateAndFillMemfd(const char* name, size_t memfd_file_size, uintptr_t value) {
31   const size_t kPageSize = sysconf(_SC_PAGE_SIZE);
32   CHECK_EQ(memfd_file_size % sizeof(value), 0);
33   CHECK_EQ(memfd_file_size % kPageSize, 0);
34 
35   // Use intermediate map to fully initialize file content. It lets compiler
36   // optimize the loop below and limits WriteFully to fd to one call. Running
37   // the Memfd.uintptr_t test on this showed 4x performance improvement.
38   uintptr_t* memfd_file_content = static_cast<uintptr_t*>(MmapOrDie(memfd_file_size));
39 
40   for (size_t i = 0; i < memfd_file_size / sizeof(value); ++i) {
41     memfd_file_content[i] = value;
42   }
43 
44   int memfd = CreateMemfdOrDie(name);
45 
46   WriteFullyOrDie(memfd, memfd_file_content, memfd_file_size);
47 
48   MunmapOrDie(memfd_file_content, memfd_file_size);
49 
50   return memfd;
51 }
52 
53 // Allocates a region of map_size bytes and backs it in chunks with memfd region
54 // of memfd_file_size bytes.
CreateMemfdBackedMapOrDie(int memfd,size_t map_size,size_t memfd_file_size)55 void* CreateMemfdBackedMapOrDie(int memfd, size_t map_size, size_t memfd_file_size) {
56   const size_t kPageSize = sysconf(_SC_PAGE_SIZE);
57   CHECK_EQ(map_size % memfd_file_size, 0);
58   CHECK_EQ(memfd_file_size % kPageSize, 0);
59 
60   // Reserving memory for the map. Instruct kernel to not commit any RAM to
61   // the map by using MAP_NORESERVE. It would be a waste anyways since
62   // this region is used to create memfd backed mmaps.
63   uint8_t* ptr = static_cast<uint8_t*>(
64       LargeMmapImplOrDie({.size = map_size, .flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE}));
65 
66   // map memfd
67   for (size_t i = 0; i < map_size / memfd_file_size; ++i) {
68     MmapImplOrDie({.addr = ptr + (i * memfd_file_size),
69                    .size = memfd_file_size,
70                    .flags = MAP_PRIVATE | MAP_FIXED,
71                    .fd = memfd});
72   }
73 
74   return ptr;
75 }
76 
77 }  // namespace berberis
78