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 <atomic>
23 #include <type_traits>
24
25 #include "berberis/base/fd.h"
26 #include "berberis/base/large_mmap.h"
27 #include "berberis/base/logging.h"
28 #include "berberis/base/mmap.h"
29
30 namespace berberis {
31
32 // Creates memfd region of memfd_file_size bytes filled with value.
33 template <typename T>
CreateAndFillMemfd(const char * name,size_t memfd_file_size,T value)34 int CreateAndFillMemfd(const char* name, size_t memfd_file_size, T value) {
35 static_assert(std::is_integral_v<T> || std::is_pointer_v<T>,
36 "T must be an integral or pointer type");
37 const size_t kPageSize = sysconf(_SC_PAGE_SIZE);
38 CHECK_EQ(memfd_file_size % sizeof(value), 0);
39 CHECK_EQ(memfd_file_size % kPageSize, 0);
40
41 // Use intermediate map to fully initialize file content. It lets compiler
42 // optimize the loop below and limits WriteFully to fd to one call. Running
43 // the Memfd.uintptr_t test on this showed 4x performance improvement.
44 T* memfd_file_content = static_cast<T*>(MmapOrDie(memfd_file_size));
45
46 for (size_t i = 0; i < memfd_file_size / sizeof(value); ++i) {
47 memfd_file_content[i] = value;
48 }
49
50 int memfd = CreateMemfdOrDie(name);
51
52 WriteFullyOrDie(memfd, memfd_file_content, memfd_file_size);
53
54 MunmapOrDie(memfd_file_content, memfd_file_size);
55
56 return memfd;
57 }
58
59 template int CreateAndFillMemfd<uintptr_t>(const char* name,
60 size_t memfd_file_size,
61 uintptr_t value);
62 template int CreateAndFillMemfd<std::atomic<uintptr_t>*>(const char* name,
63 size_t memfd_file_size,
64 std::atomic<uintptr_t>* value);
65
66 #if defined(__LP64__)
67 template int CreateAndFillMemfd<uint32_t>(const char* name, size_t memfd_file_size, uint32_t value);
68 template int CreateAndFillMemfd<std::atomic<uint32_t>*>(const char* name,
69 size_t memfd_file_size,
70 std::atomic<uint32_t>* value);
71 #endif
72
73 // Allocates a region of map_size bytes and backs it in chunks with memfd region
74 // of memfd_file_size bytes.
CreateMemfdBackedMapOrDie(int memfd,size_t map_size,size_t memfd_file_size)75 void* CreateMemfdBackedMapOrDie(int memfd, size_t map_size, size_t memfd_file_size) {
76 const size_t kPageSize = sysconf(_SC_PAGE_SIZE);
77 CHECK_EQ(map_size % memfd_file_size, 0);
78 CHECK_EQ(memfd_file_size % kPageSize, 0);
79
80 // Reserving memory for the map. Instruct kernel to not commit any RAM to
81 // the map by using MAP_NORESERVE. It would be a waste anyways since
82 // this region is used to create memfd backed mmaps.
83 uint8_t* ptr = static_cast<uint8_t*>(
84 LargeMmapImplOrDie({.size = map_size, .flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE}));
85
86 // map memfd
87 for (size_t i = 0; i < map_size / memfd_file_size; ++i) {
88 MmapImplOrDie({.addr = ptr + (i * memfd_file_size),
89 .size = memfd_file_size,
90 .flags = MAP_PRIVATE | MAP_FIXED,
91 .fd = memfd});
92 }
93
94 return ptr;
95 }
96
97 } // namespace berberis
98