blob: 26aaa53c6671d27c38d1ae02c2a9ce65e3c4e71d [file] [log] [blame]
Nicolas Geoffray2a905b22019-06-06 09:04:07 +01001/*
2 * Copyright 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 "jit_memory_region.h"
18
Nicolas Geoffray2411f492019-06-14 08:54:46 +010019#include <fcntl.h>
20#include <unistd.h>
21
Nicolas Geoffray2a905b22019-06-06 09:04:07 +010022#include <android-base/unique_fd.h>
23#include "base/bit_utils.h" // For RoundDown, RoundUp
24#include "base/globals.h"
25#include "base/logging.h" // For VLOG.
Nicolas Geoffray349845a2019-06-19 13:13:10 +010026#include "base/membarrier.h"
Nicolas Geoffray2a905b22019-06-06 09:04:07 +010027#include "base/memfd.h"
28#include "base/systrace.h"
29#include "gc/allocator/dlmalloc.h"
30#include "jit/jit_scoped_code_cache_write.h"
31#include "oat_quick_method_header.h"
Nicolas Geoffray2411f492019-06-14 08:54:46 +010032#include "palette/palette.h"
Nicolas Geoffray2a905b22019-06-06 09:04:07 +010033
34using android::base::unique_fd;
35
36namespace art {
37namespace jit {
38
39// Data cache will be half of the capacity
40// Code cache will be the other half of the capacity.
41// TODO: Make this variable?
42static constexpr size_t kCodeAndDataCapacityDivider = 2;
43
Nicolas Geoffray9c54e182019-06-18 10:42:52 +010044bool JitMemoryRegion::Initialize(size_t initial_capacity,
45 size_t max_capacity,
46 bool rwx_memory_allowed,
47 bool is_zygote,
48 std::string* error_msg) {
Nicolas Geoffray2a905b22019-06-06 09:04:07 +010049 ScopedTrace trace(__PRETTY_FUNCTION__);
50
Nicolas Geoffray9c54e182019-06-18 10:42:52 +010051 CHECK_GE(max_capacity, initial_capacity);
52 CHECK(max_capacity <= 1 * GB) << "The max supported size for JIT code cache is 1GB";
53 // Align both capacities to page size, as that's the unit mspaces use.
54 initial_capacity_ = RoundDown(initial_capacity, 2 * kPageSize);
55 max_capacity_ = RoundDown(max_capacity, 2 * kPageSize);
56 current_capacity_ = initial_capacity,
57 data_end_ = initial_capacity / kCodeAndDataCapacityDivider;
58 exec_end_ = initial_capacity - data_end_;
59
Nicolas Geoffray2a905b22019-06-06 09:04:07 +010060 const size_t capacity = max_capacity_;
61 const size_t data_capacity = capacity / kCodeAndDataCapacityDivider;
62 const size_t exec_capacity = capacity - data_capacity;
63
64 // File descriptor enabling dual-view mapping of code section.
65 unique_fd mem_fd;
66
Nicolas Geoffray05f87212019-06-19 10:00:00 +010067 if (is_zygote) {
68 // Because we are not going to GC code generated by the zygote, just use all available.
69 current_capacity_ = max_capacity;
70 mem_fd = unique_fd(CreateZygoteMemory(capacity, error_msg));
71 if (mem_fd.get() < 0) {
72 return false;
73 }
74 } else {
Nicolas Geoffray2a905b22019-06-06 09:04:07 +010075 // Bionic supports memfd_create, but the call may fail on older kernels.
76 mem_fd = unique_fd(art::memfd_create("/jit-cache", /* flags= */ 0));
77 if (mem_fd.get() < 0) {
78 std::ostringstream oss;
79 oss << "Failed to initialize dual view JIT. memfd_create() error: " << strerror(errno);
80 if (!rwx_memory_allowed) {
81 // Without using RWX page permissions, the JIT can not fallback to single mapping as it
82 // requires tranitioning the code pages to RWX for updates.
83 *error_msg = oss.str();
84 return false;
85 }
86 VLOG(jit) << oss.str();
Nicolas Geoffray05f87212019-06-19 10:00:00 +010087 } else if (ftruncate(mem_fd, capacity) != 0) {
88 std::ostringstream oss;
89 oss << "Failed to initialize memory file: " << strerror(errno);
90 *error_msg = oss.str();
91 return false;
Nicolas Geoffray2a905b22019-06-06 09:04:07 +010092 }
93 }
94
Nicolas Geoffray2a905b22019-06-06 09:04:07 +010095 std::string data_cache_name = is_zygote ? "zygote-data-code-cache" : "data-code-cache";
96 std::string exec_cache_name = is_zygote ? "zygote-jit-code-cache" : "jit-code-cache";
97
98 std::string error_str;
99 // Map name specific for android_os_Debug.cpp accounting.
100 // Map in low 4gb to simplify accessing root tables for x86_64.
101 // We could do PC-relative addressing to avoid this problem, but that
102 // would require reserving code and data area before submitting, which
103 // means more windows for the code memory to be RWX.
104 int base_flags;
105 MemMap data_pages;
106 if (mem_fd.get() >= 0) {
107 // Dual view of JIT code cache case. Create an initial mapping of data pages large enough
108 // for data and non-writable view of JIT code pages. We use the memory file descriptor to
109 // enable dual mapping - we'll create a second mapping using the descriptor below. The
110 // mappings will look like:
111 //
112 // VA PA
113 //
114 // +---------------+
115 // | non exec code |\
116 // +---------------+ \
117 // : :\ \
118 // +---------------+.\.+---------------+
119 // | exec code | \| code |
120 // +---------------+...+---------------+
121 // | data | | data |
122 // +---------------+...+---------------+
123 //
124 // In this configuration code updates are written to the non-executable view of the code
125 // cache, and the executable view of the code cache has fixed RX memory protections.
126 //
127 // This memory needs to be mapped shared as the code portions will have two mappings.
128 base_flags = MAP_SHARED;
129 data_pages = MemMap::MapFile(
130 data_capacity + exec_capacity,
131 kProtRW,
132 base_flags,
133 mem_fd,
134 /* start= */ 0,
135 /* low_4gb= */ true,
136 data_cache_name.c_str(),
137 &error_str);
138 } else {
139 // Single view of JIT code cache case. Create an initial mapping of data pages large enough
140 // for data and JIT code pages. The mappings will look like:
141 //
142 // VA PA
143 //
144 // +---------------+...+---------------+
145 // | exec code | | code |
146 // +---------------+...+---------------+
147 // | data | | data |
148 // +---------------+...+---------------+
149 //
150 // In this configuration code updates are written to the executable view of the code cache,
151 // and the executable view of the code cache transitions RX to RWX for the update and then
152 // back to RX after the update.
153 base_flags = MAP_PRIVATE | MAP_ANON;
154 data_pages = MemMap::MapAnonymous(
155 data_cache_name.c_str(),
156 data_capacity + exec_capacity,
157 kProtRW,
158 /* low_4gb= */ true,
159 &error_str);
160 }
161
162 if (!data_pages.IsValid()) {
163 std::ostringstream oss;
164 oss << "Failed to create read write cache: " << error_str << " size=" << capacity;
165 *error_msg = oss.str();
166 return false;
167 }
168
169 MemMap exec_pages;
170 MemMap non_exec_pages;
171 if (exec_capacity > 0) {
172 uint8_t* const divider = data_pages.Begin() + data_capacity;
173 // Set initial permission for executable view to catch any SELinux permission problems early
174 // (for processes that cannot map WX pages). Otherwise, this region does not need to be
175 // executable as there is no code in the cache yet.
176 exec_pages = data_pages.RemapAtEnd(divider,
177 exec_cache_name.c_str(),
178 kProtRX,
179 base_flags | MAP_FIXED,
180 mem_fd.get(),
181 (mem_fd.get() >= 0) ? data_capacity : 0,
182 &error_str);
183 if (!exec_pages.IsValid()) {
184 std::ostringstream oss;
185 oss << "Failed to create read execute code cache: " << error_str << " size=" << capacity;
186 *error_msg = oss.str();
187 return false;
188 }
189
190 if (mem_fd.get() >= 0) {
191 // For dual view, create the secondary view of code memory used for updating code. This view
192 // is never executable.
193 std::string name = exec_cache_name + "-rw";
194 non_exec_pages = MemMap::MapFile(exec_capacity,
195 kProtR,
196 base_flags,
197 mem_fd,
198 /* start= */ data_capacity,
199 /* low_4GB= */ false,
200 name.c_str(),
201 &error_str);
202 if (!non_exec_pages.IsValid()) {
203 static const char* kFailedNxView = "Failed to map non-executable view of JIT code cache";
204 if (rwx_memory_allowed) {
205 // Log and continue as single view JIT (requires RWX memory).
206 VLOG(jit) << kFailedNxView;
207 } else {
208 *error_msg = kFailedNxView;
209 return false;
210 }
211 }
212 }
Nicolas Geoffray05f87212019-06-19 10:00:00 +0100213 if (is_zygote) {
214 // Now that we have created the writable and executable mappings, prevent creating any new
215 // ones.
216 if (!ProtectZygoteMemory(mem_fd.get(), error_msg)) {
217 return false;
218 }
219 }
Nicolas Geoffray2a905b22019-06-06 09:04:07 +0100220 } else {
221 // Profiling only. No memory for code required.
222 }
223
224 data_pages_ = std::move(data_pages);
225 exec_pages_ = std::move(exec_pages);
226 non_exec_pages_ = std::move(non_exec_pages);
Nicolas Geoffray2a905b22019-06-06 09:04:07 +0100227
Nicolas Geoffray9c54e182019-06-18 10:42:52 +0100228 // Now that the pages are initialized, initialize the spaces.
Nicolas Geoffray2a905b22019-06-06 09:04:07 +0100229
Nicolas Geoffray2a905b22019-06-06 09:04:07 +0100230 // Initialize the data heap
231 data_mspace_ = create_mspace_with_base(data_pages_.Begin(), data_end_, false /*locked*/);
232 CHECK(data_mspace_ != nullptr) << "create_mspace_with_base (data) failed";
233
234 // Initialize the code heap
235 MemMap* code_heap = nullptr;
236 if (non_exec_pages_.IsValid()) {
237 code_heap = &non_exec_pages_;
238 } else if (exec_pages_.IsValid()) {
239 code_heap = &exec_pages_;
240 }
241 if (code_heap != nullptr) {
242 // Make all pages reserved for the code heap writable. The mspace allocator, that manages the
243 // heap, will take and initialize pages in create_mspace_with_base().
244 CheckedCall(mprotect, "create code heap", code_heap->Begin(), code_heap->Size(), kProtRW);
245 exec_mspace_ = create_mspace_with_base(code_heap->Begin(), exec_end_, false /*locked*/);
246 CHECK(exec_mspace_ != nullptr) << "create_mspace_with_base (exec) failed";
Nicolas Geoffray05f87212019-06-19 10:00:00 +0100247 SetFootprintLimit(current_capacity_);
Nicolas Geoffray2a905b22019-06-06 09:04:07 +0100248 // Protect pages containing heap metadata. Updates to the code heap toggle write permission to
249 // perform the update and there are no other times write access is required.
250 CheckedCall(mprotect, "protect code heap", code_heap->Begin(), code_heap->Size(), kProtR);
251 } else {
252 exec_mspace_ = nullptr;
Nicolas Geoffray05f87212019-06-19 10:00:00 +0100253 SetFootprintLimit(current_capacity_);
Nicolas Geoffray2a905b22019-06-06 09:04:07 +0100254 }
Nicolas Geoffray9c54e182019-06-18 10:42:52 +0100255 return true;
Nicolas Geoffray2a905b22019-06-06 09:04:07 +0100256}
257
258void JitMemoryRegion::SetFootprintLimit(size_t new_footprint) {
259 size_t data_space_footprint = new_footprint / kCodeAndDataCapacityDivider;
260 DCHECK(IsAlignedParam(data_space_footprint, kPageSize));
261 DCHECK_EQ(data_space_footprint * kCodeAndDataCapacityDivider, new_footprint);
262 mspace_set_footprint_limit(data_mspace_, data_space_footprint);
263 if (HasCodeMapping()) {
264 ScopedCodeCacheWrite scc(*this);
265 mspace_set_footprint_limit(exec_mspace_, new_footprint - data_space_footprint);
266 }
267}
268
269bool JitMemoryRegion::IncreaseCodeCacheCapacity() {
270 if (current_capacity_ == max_capacity_) {
271 return false;
272 }
273
274 // Double the capacity if we're below 1MB, or increase it by 1MB if
275 // we're above.
276 if (current_capacity_ < 1 * MB) {
277 current_capacity_ *= 2;
278 } else {
279 current_capacity_ += 1 * MB;
280 }
281 if (current_capacity_ > max_capacity_) {
282 current_capacity_ = max_capacity_;
283 }
284
285 VLOG(jit) << "Increasing code cache capacity to " << PrettySize(current_capacity_);
286
287 SetFootprintLimit(current_capacity_);
288
289 return true;
290}
291
292// NO_THREAD_SAFETY_ANALYSIS as this is called from mspace code, at which point the lock
293// is already held.
294void* JitMemoryRegion::MoreCore(const void* mspace, intptr_t increment) NO_THREAD_SAFETY_ANALYSIS {
295 if (mspace == exec_mspace_) {
296 DCHECK(exec_mspace_ != nullptr);
297 const MemMap* const code_pages = GetUpdatableCodeMapping();
298 void* result = code_pages->Begin() + exec_end_;
299 exec_end_ += increment;
300 return result;
301 } else {
302 DCHECK_EQ(data_mspace_, mspace);
303 void* result = data_pages_.Begin() + data_end_;
304 data_end_ += increment;
305 return result;
306 }
307}
308
Nicolas Geoffray349845a2019-06-19 13:13:10 +0100309const uint8_t* JitMemoryRegion::AllocateCode(const uint8_t* code,
310 size_t code_size,
311 const uint8_t* stack_map,
312 bool has_should_deoptimize_flag) {
313 ScopedCodeCacheWrite scc(*this);
314
315 size_t alignment = GetJitCodeAlignment();
Nicolas Geoffray2a905b22019-06-06 09:04:07 +0100316 // Ensure the header ends up at expected instruction alignment.
Nicolas Geoffray349845a2019-06-19 13:13:10 +0100317 size_t header_size = RoundUp(sizeof(OatQuickMethodHeader), alignment);
318 size_t total_size = header_size + code_size;
319
320 // Each allocation should be on its own set of cache lines.
321 // `total_size` covers the OatQuickMethodHeader, the JIT generated machine code,
322 // and any alignment padding.
323 DCHECK_GT(total_size, header_size);
324 uint8_t* w_memory = reinterpret_cast<uint8_t*>(
325 mspace_memalign(exec_mspace_, alignment, total_size));
326 if (w_memory == nullptr) {
327 return nullptr;
328 }
329 uint8_t* x_memory = GetExecutableAddress(w_memory);
330 // Ensure the header ends up at expected instruction alignment.
331 DCHECK_ALIGNED_PARAM(reinterpret_cast<uintptr_t>(w_memory + header_size), alignment);
332 used_memory_for_code_ += mspace_usable_size(w_memory);
333 const uint8_t* result = x_memory + header_size;
334
335 // Write the code.
336 std::copy(code, code + code_size, w_memory + header_size);
337
338 // Write the header.
339 OatQuickMethodHeader* method_header =
340 OatQuickMethodHeader::FromCodePointer(w_memory + header_size);
341 new (method_header) OatQuickMethodHeader(
342 (stack_map != nullptr) ? result - stack_map : 0u,
343 code_size);
344 if (has_should_deoptimize_flag) {
345 method_header->SetHasShouldDeoptimizeFlag();
346 }
347
348 // Both instruction and data caches need flushing to the point of unification where both share
349 // a common view of memory. Flushing the data cache ensures the dirty cachelines from the
350 // newly added code are written out to the point of unification. Flushing the instruction
351 // cache ensures the newly written code will be fetched from the point of unification before
352 // use. Memory in the code cache is re-cycled as code is added and removed. The flushes
353 // prevent stale code from residing in the instruction cache.
354 //
355 // Caches are flushed before write permission is removed because some ARMv8 Qualcomm kernels
356 // may trigger a segfault if a page fault occurs when requesting a cache maintenance
357 // operation. This is a kernel bug that we need to work around until affected devices
358 // (e.g. Nexus 5X and 6P) stop being supported or their kernels are fixed.
359 //
360 // For reference, this behavior is caused by this commit:
361 // https://android.googlesource.com/kernel/msm/+/3fbe6bc28a6b9939d0650f2f17eb5216c719950c
362 //
363 if (HasDualCodeMapping()) {
364 // Flush the data cache lines associated with the non-executable copy of the code just added.
365 FlushDataCache(w_memory, w_memory + total_size);
366 }
367
368 // FlushInstructionCache() flushes both data and instruction caches lines. The cacheline range
369 // flushed is for the executable mapping of the code just added.
370 FlushInstructionCache(x_memory, x_memory + total_size);
371
372 // Ensure CPU instruction pipelines are flushed for all cores. This is necessary for
373 // correctness as code may still be in instruction pipelines despite the i-cache flush. It is
374 // not safe to assume that changing permissions with mprotect (RX->RWX->RX) will cause a TLB
375 // shootdown (incidentally invalidating the CPU pipelines by sending an IPI to all cores to
376 // notify them of the TLB invalidation). Some architectures, notably ARM and ARM64, have
377 // hardware support that broadcasts TLB invalidations and so their kernels have no software
378 // based TLB shootdown. The sync-core flavor of membarrier was introduced in Linux 4.16 to
379 // address this (see mbarrier(2)). The membarrier here will fail on prior kernels and on
380 // platforms lacking the appropriate support.
381 art::membarrier(art::MembarrierCommand::kPrivateExpeditedSyncCore);
382
Nicolas Geoffray2a905b22019-06-06 09:04:07 +0100383 return result;
384}
385
Nicolas Geoffray00a37ff2019-06-20 14:27:22 +0100386static void FillRootTable(uint8_t* roots_data, const std::vector<Handle<mirror::Object>>& roots)
387 REQUIRES(Locks::jit_lock_)
388 REQUIRES_SHARED(Locks::mutator_lock_) {
389 GcRoot<mirror::Object>* gc_roots = reinterpret_cast<GcRoot<mirror::Object>*>(roots_data);
390 const uint32_t length = roots.size();
391 // Put all roots in `roots_data`.
392 for (uint32_t i = 0; i < length; ++i) {
393 ObjPtr<mirror::Object> object = roots[i].Get();
394 gc_roots[i] = GcRoot<mirror::Object>(object);
395 }
396 // Store the length of the table at the end. This will allow fetching it from a stack_map
397 // pointer.
398 reinterpret_cast<uint32_t*>(roots_data)[length] = length;
399}
400
401void JitMemoryRegion::CommitData(uint8_t* roots_data,
402 const std::vector<Handle<mirror::Object>>& roots,
403 const uint8_t* stack_map,
404 size_t stack_map_size) {
405 size_t root_table_size = ComputeRootTableSize(roots.size());
406 uint8_t* stack_map_data = roots_data + root_table_size;
407 FillRootTable(roots_data, roots);
408 memcpy(stack_map_data, stack_map, stack_map_size);
409 // Flush data cache, as compiled code references literals in it.
410 FlushDataCache(roots_data, roots_data + root_table_size + stack_map_size);
411}
412
Nicolas Geoffray349845a2019-06-19 13:13:10 +0100413void JitMemoryRegion::FreeCode(const uint8_t* code) {
Nicolas Geoffray2a905b22019-06-06 09:04:07 +0100414 code = GetNonExecutableAddress(code);
415 used_memory_for_code_ -= mspace_usable_size(code);
Nicolas Geoffray349845a2019-06-19 13:13:10 +0100416 mspace_free(exec_mspace_, const_cast<uint8_t*>(code));
Nicolas Geoffray2a905b22019-06-06 09:04:07 +0100417}
418
419uint8_t* JitMemoryRegion::AllocateData(size_t data_size) {
420 void* result = mspace_malloc(data_mspace_, data_size);
421 used_memory_for_data_ += mspace_usable_size(result);
422 return reinterpret_cast<uint8_t*>(result);
423}
424
425void JitMemoryRegion::FreeData(uint8_t* data) {
426 used_memory_for_data_ -= mspace_usable_size(data);
427 mspace_free(data_mspace_, data);
428}
429
Nicolas Geoffray2411f492019-06-14 08:54:46 +0100430#if defined(__BIONIC__)
431
432static bool IsSealFutureWriteSupportedInternal() {
433 unique_fd fd(art::memfd_create("test_android_memfd", MFD_ALLOW_SEALING));
434 if (fd == -1) {
435 LOG(INFO) << "memfd_create failed: " << strerror(errno) << ", no memfd support.";
436 return false;
437 }
438
439 if (fcntl(fd, F_ADD_SEALS, F_SEAL_FUTURE_WRITE) == -1) {
440 LOG(INFO) << "fcntl(F_ADD_SEALS) failed: " << strerror(errno) << ", no memfd support.";
441 return false;
442 }
443
444 LOG(INFO) << "Using memfd for future sealing";
445 return true;
446}
447
448static bool IsSealFutureWriteSupported() {
449 static bool is_seal_future_write_supported = IsSealFutureWriteSupportedInternal();
450 return is_seal_future_write_supported;
451}
452
453int JitMemoryRegion::CreateZygoteMemory(size_t capacity, std::string* error_msg) {
454 /* Check if kernel support exists, otherwise fall back to ashmem */
455 static const char* kRegionName = "/jit-zygote-cache";
456 if (IsSealFutureWriteSupported()) {
457 int fd = art::memfd_create(kRegionName, MFD_ALLOW_SEALING);
458 if (fd == -1) {
459 std::ostringstream oss;
460 oss << "Failed to create zygote mapping: " << strerror(errno);
461 *error_msg = oss.str();
462 return -1;
463 }
464
465 if (ftruncate(fd, capacity) != 0) {
466 std::ostringstream oss;
467 oss << "Failed to create zygote mapping: " << strerror(errno);
468 *error_msg = oss.str();
469 return -1;
470 }
471
472 return fd;
473 }
474
475 LOG(INFO) << "Falling back to ashmem implementation for JIT zygote mapping";
476
477 int fd;
478 PaletteStatus status = PaletteAshmemCreateRegion(kRegionName, capacity, &fd);
479 if (status != PaletteStatus::kOkay) {
480 CHECK_EQ(status, PaletteStatus::kCheckErrno);
481 std::ostringstream oss;
482 oss << "Failed to create zygote mapping: " << strerror(errno);
483 *error_msg = oss.str();
484 return -1;
485 }
486 return fd;
487}
488
489bool JitMemoryRegion::ProtectZygoteMemory(int fd, std::string* error_msg) {
490 if (IsSealFutureWriteSupported()) {
491 if (fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_SEAL | F_SEAL_FUTURE_WRITE)
492 == -1) {
493 std::ostringstream oss;
494 oss << "Failed to protect zygote mapping: " << strerror(errno);
495 *error_msg = oss.str();
496 return false;
497 }
498 } else {
499 PaletteStatus status = PaletteAshmemSetProtRegion(fd, PROT_READ);
500 if (status != PaletteStatus::kOkay) {
501 CHECK_EQ(status, PaletteStatus::kCheckErrno);
502 std::ostringstream oss;
503 oss << "Failed to protect zygote mapping: " << strerror(errno);
504 *error_msg = oss.str();
505 return false;
506 }
507 }
508 return true;
509}
510
511#else
512
513// When running on non-bionic configuration, this is not supported.
514int JitMemoryRegion::CreateZygoteMemory(size_t capacity ATTRIBUTE_UNUSED,
515 std::string* error_msg ATTRIBUTE_UNUSED) {
516 return -1;
517}
518
519bool JitMemoryRegion::ProtectZygoteMemory(int fd ATTRIBUTE_UNUSED,
520 std::string* error_msg ATTRIBUTE_UNUSED) {
521 return true;
522}
523
524#endif
525
Nicolas Geoffray2a905b22019-06-06 09:04:07 +0100526} // namespace jit
527} // namespace art