1 /*
2 * Copyright (C) 2017 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 #define LOG_TAG "Memory"
18
19 #include "Memory.h"
20
21 #include "ExecutionBurstController.h"
22 #include "HalInterfaces.h"
23 #include "Utils.h"
24
25 namespace android {
26 namespace nn {
27
~Memory()28 Memory::~Memory() {
29 for (const auto [ptr, weakBurst] : mUsedBy) {
30 if (const std::shared_ptr<ExecutionBurstController> burst = weakBurst.lock()) {
31 burst->freeMemory(getKey());
32 }
33 }
34 }
35
create(uint32_t size)36 int Memory::create(uint32_t size) {
37 mHidlMemory = allocateSharedMemory(size);
38 mMemory = mapMemory(mHidlMemory);
39 if (mMemory == nullptr) {
40 LOG(ERROR) << "Memory::create failed";
41 return ANEURALNETWORKS_OUT_OF_MEMORY;
42 }
43 return ANEURALNETWORKS_NO_ERROR;
44 }
45
validateSize(uint32_t offset,uint32_t length) const46 bool Memory::validateSize(uint32_t offset, uint32_t length) const {
47 if (offset + length > mHidlMemory.size()) {
48 LOG(ERROR) << "Request size larger than the memory size.";
49 return false;
50 } else {
51 return true;
52 }
53 }
54
getKey() const55 intptr_t Memory::getKey() const {
56 return reinterpret_cast<intptr_t>(this);
57 }
58
usedBy(const std::shared_ptr<ExecutionBurstController> & burst) const59 void Memory::usedBy(const std::shared_ptr<ExecutionBurstController>& burst) const {
60 std::lock_guard<std::mutex> guard(mMutex);
61 mUsedBy.emplace(burst.get(), burst);
62 }
63
~MemoryFd()64 MemoryFd::~MemoryFd() {
65 // Unmap the memory.
66 if (mMapping) {
67 munmap(mMapping, mHidlMemory.size());
68 }
69 // Delete the native_handle.
70 if (mHandle) {
71 int fd = mHandle->data[0];
72 if (fd != -1) {
73 close(fd);
74 }
75 native_handle_delete(mHandle);
76 }
77 }
78
set(size_t size,int prot,int fd,size_t offset)79 int MemoryFd::set(size_t size, int prot, int fd, size_t offset) {
80 if (size == 0 || fd < 0) {
81 LOG(ERROR) << "Invalid size or fd";
82 return ANEURALNETWORKS_BAD_DATA;
83 }
84 int dupfd = dup(fd);
85 if (dupfd == -1) {
86 LOG(ERROR) << "Failed to dup the fd";
87 return ANEURALNETWORKS_UNEXPECTED_NULL;
88 }
89
90 if (mMapping) {
91 if (munmap(mMapping, mHidlMemory.size()) != 0) {
92 LOG(ERROR) << "Failed to remove the existing mapping";
93 // This is not actually fatal.
94 }
95 mMapping = nullptr;
96 }
97 if (mHandle) {
98 native_handle_delete(mHandle);
99 }
100 mHandle = native_handle_create(1, 3);
101 if (mHandle == nullptr) {
102 LOG(ERROR) << "Failed to create native_handle";
103 return ANEURALNETWORKS_UNEXPECTED_NULL;
104 }
105 mHandle->data[0] = dupfd;
106 mHandle->data[1] = prot;
107 mHandle->data[2] = (int32_t)(uint32_t)(offset & 0xffffffff);
108 #if defined(__LP64__)
109 mHandle->data[3] = (int32_t)(uint32_t)(offset >> 32);
110 #else
111 mHandle->data[3] = 0;
112 #endif
113 mHidlMemory = hidl_memory("mmap_fd", mHandle, size);
114 return ANEURALNETWORKS_NO_ERROR;
115 }
116
getPointer(uint8_t ** buffer) const117 int MemoryFd::getPointer(uint8_t** buffer) const {
118 if (mMapping) {
119 *buffer = mMapping;
120 return ANEURALNETWORKS_NO_ERROR;
121 }
122
123 if (mHandle == nullptr) {
124 LOG(ERROR) << "Memory not initialized";
125 return ANEURALNETWORKS_UNEXPECTED_NULL;
126 }
127
128 int fd = mHandle->data[0];
129 int prot = mHandle->data[1];
130 size_t offset = getSizeFromInts(mHandle->data[2], mHandle->data[3]);
131 void* data = mmap(nullptr, mHidlMemory.size(), prot, MAP_SHARED, fd, offset);
132 if (data == MAP_FAILED) {
133 LOG(ERROR) << "MemoryFd::getPointer(): Can't mmap the file descriptor.";
134 return ANEURALNETWORKS_UNMAPPABLE;
135 } else {
136 mMapping = *buffer = static_cast<uint8_t*>(data);
137 return ANEURALNETWORKS_NO_ERROR;
138 }
139 }
140
add(const Memory * memory)141 uint32_t MemoryTracker::add(const Memory* memory) {
142 VLOG(MODEL) << __func__ << "(" << SHOW_IF_DEBUG(memory) << ")";
143 // See if we already have this memory. If so,
144 // return its index.
145 auto i = mKnown.find(memory);
146 if (i != mKnown.end()) {
147 return i->second;
148 }
149 VLOG(MODEL) << "It's new";
150 // It's a new one. Save it an assign an index to it.
151 size_t next = mKnown.size();
152 if (next > 0xFFFFFFFF) {
153 LOG(ERROR) << "ANeuralNetworks more than 2^32 memories.";
154 return ANEURALNETWORKS_BAD_DATA;
155 }
156 uint32_t idx = static_cast<uint32_t>(next);
157 mKnown[memory] = idx;
158 mMemories.push_back(memory);
159 return idx;
160 }
161
162 } // namespace nn
163 } // namespace android
164