OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/memory/discardable_memory.h" | 5 #include "base/memory/discardable_memory.h" |
6 | 6 |
7 #include <sys/mman.h> | 7 #include <sys/mman.h> |
8 #include <unistd.h> | 8 #include <unistd.h> |
9 | 9 |
| 10 #include "base/basictypes.h" |
| 11 #include "base/compiler_specific.h" |
| 12 #include "base/file_util.h" |
10 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
11 #include "base/logging.h" | 14 #include "base/logging.h" |
12 #include "base/posix/eintr_wrapper.h" | 15 #include "base/posix/eintr_wrapper.h" |
13 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
14 #include "third_party/ashmem/ashmem.h" | 17 #include "third_party/ashmem/ashmem.h" |
15 | 18 |
| 19 namespace base { |
16 namespace { | 20 namespace { |
17 | 21 |
| 22 // Protects |g_num_discardable_memory| below. |
18 base::LazyInstance<base::Lock>::Leaky g_discardable_memory_lock = | 23 base::LazyInstance<base::Lock>::Leaky g_discardable_memory_lock = |
19 LAZY_INSTANCE_INITIALIZER; | 24 LAZY_INSTANCE_INITIALIZER; |
20 | 25 |
21 // Total number of discardable memory in the process. | 26 // Total number of discardable memory in the process. |
22 int g_num_discardable_memory = 0; | 27 int g_num_discardable_memory = 0; |
23 | 28 |
24 // Upper limit on the number of discardable memory to avoid hitting file | 29 // Upper limit on the number of discardable memory to avoid hitting file |
25 // descriptor limit. | 30 // descriptor limit. |
26 const int kDiscardableMemoryNumLimit = 128; | 31 const int kDiscardableMemoryNumLimit = 128; |
27 | 32 |
| 33 bool CreateAshmemRegion(const char* name, |
| 34 size_t size, |
| 35 int* out_fd, |
| 36 void** out_address) { |
| 37 base::AutoLock lock(g_discardable_memory_lock.Get()); |
| 38 if (g_num_discardable_memory + 1 > kDiscardableMemoryNumLimit) |
| 39 return false; |
| 40 int fd = ashmem_create_region(name, size); |
| 41 if (fd < 0) { |
| 42 DLOG(ERROR) << "ashmem_create_region() failed"; |
| 43 return false; |
| 44 } |
| 45 file_util::ScopedFD fd_closer(&fd); |
| 46 |
| 47 const int err = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE); |
| 48 if (err < 0) { |
| 49 DLOG(ERROR) << "Error " << err << " when setting protection of ashmem"; |
| 50 return false; |
| 51 } |
| 52 |
| 53 // There is a problem using MAP_PRIVATE here. As we are constantly calling |
| 54 // Lock() and Unlock(), data could get lost if they are not written to the |
| 55 // underlying file when Unlock() gets called. |
| 56 void* const address = mmap( |
| 57 NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 58 if (address == MAP_FAILED) { |
| 59 DPLOG(ERROR) << "Failed to map memory."; |
| 60 return false; |
| 61 } |
| 62 |
| 63 ignore_result(fd_closer.release()); |
| 64 ++g_num_discardable_memory; |
| 65 *out_fd = fd; |
| 66 *out_address = address; |
| 67 return true; |
28 } | 68 } |
29 | 69 |
30 namespace base { | 70 bool DeleteAshmemRegion(int fd, size_t size, void* address) { |
| 71 base::AutoLock lock(g_discardable_memory_lock.Get()); |
| 72 --g_num_discardable_memory; |
| 73 if (munmap(address, size) == -1) { |
| 74 DPLOG(ERROR) << "Failed to unmap memory."; |
| 75 close(fd); |
| 76 return false; |
| 77 } |
| 78 return close(fd) == 0; |
| 79 } |
| 80 |
| 81 LockDiscardableMemoryStatus LockAshmemRegion(int fd, |
| 82 size_t off, |
| 83 size_t size, |
| 84 const void* address) { |
| 85 const int result = ashmem_pin_region(fd, off, size); |
| 86 DCHECK_EQ(0, mprotect(address, size, PROT_READ | PROT_WRITE)); |
| 87 return result == ASHMEM_WAS_PURGED ? |
| 88 DISCARDABLE_MEMORY_PURGED : DISCARDABLE_MEMORY_SUCCESS; |
| 89 } |
| 90 |
| 91 bool UnlockAshmemRegion(int fd, size_t off, size_t size, const void* address) { |
| 92 const int failed = ashmem_unpin_region(fd, off, size); |
| 93 if (failed) |
| 94 DLOG(ERROR) << "Failed to unpin memory."; |
| 95 // This allows us to catch accesses to unlocked memory. |
| 96 DCHECK_EQ(0, mprotect(address, size, PROT_NONE)); |
| 97 return !failed; |
| 98 } |
| 99 |
| 100 class DiscardableMemoryAndroid : public DiscardableMemory { |
| 101 public: |
| 102 DiscardableMemoryAndroid(int fd, void* address, size_t size) |
| 103 : fd_(fd), |
| 104 memory_(address), |
| 105 size_(size) { |
| 106 DCHECK_GE(fd_, 0); |
| 107 DCHECK(memory_); |
| 108 } |
| 109 |
| 110 virtual ~DiscardableMemoryAndroid() { |
| 111 DeleteAshmemRegion(fd_, size_, memory_); |
| 112 } |
| 113 |
| 114 // DiscardableMemory: |
| 115 virtual LockDiscardableMemoryStatus Lock() OVERRIDE { |
| 116 return LockAshmemRegion(fd_, 0, size_, memory_); |
| 117 } |
| 118 |
| 119 virtual void Unlock() OVERRIDE { |
| 120 UnlockAshmemRegion(fd_, 0, size_, memory_); |
| 121 } |
| 122 |
| 123 virtual void* Memory() const OVERRIDE { |
| 124 return memory_; |
| 125 } |
| 126 |
| 127 private: |
| 128 const int fd_; |
| 129 void* const memory_; |
| 130 const size_t size_; |
| 131 |
| 132 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryAndroid); |
| 133 }; |
| 134 |
| 135 } // namespace |
31 | 136 |
32 // static | 137 // static |
33 bool DiscardableMemory::Supported() { | 138 bool DiscardableMemory::Supported() { |
34 return true; | 139 return true; |
35 } | 140 } |
36 | 141 |
37 DiscardableMemory::~DiscardableMemory() { | 142 // static |
38 if (is_locked_) | 143 scoped_ptr<DiscardableMemory> DiscardableMemory::CreateLockedMemory( |
39 Unlock(); | 144 size_t size) { |
40 // If fd_ is smaller than 0, initialization must have failed and | 145 // Pinning & unpinning works with page granularity therefore align the size |
41 // g_num_discardable_memory is not incremented by the caller. | 146 // upfront. |
42 if (fd_ < 0) | 147 const size_t kPageSize = 4096; |
43 return; | 148 const size_t mask = ~(kPageSize - 1); |
44 HANDLE_EINTR(close(fd_)); | 149 size = (size + kPageSize - 1) & mask; |
45 fd_ = -1; | 150 int fd; |
46 ReleaseFileDescriptor(); | 151 void* address; |
47 } | 152 if (!CreateAshmemRegion("", size, &fd, &address)) |
48 | 153 return scoped_ptr<DiscardableMemory>(); |
49 bool DiscardableMemory::ReserveFileDescriptor() { | 154 return scoped_ptr<DiscardableMemory>( |
50 base::AutoLock lock(g_discardable_memory_lock.Get()); | 155 new DiscardableMemoryAndroid(fd, address, size)); |
51 if (g_num_discardable_memory < kDiscardableMemoryNumLimit) { | |
52 ++g_num_discardable_memory; | |
53 return true; | |
54 } | |
55 return false; | |
56 } | |
57 | |
58 void DiscardableMemory::ReleaseFileDescriptor() { | |
59 base::AutoLock lock(g_discardable_memory_lock.Get()); | |
60 --g_num_discardable_memory; | |
61 DCHECK_LE(0, g_num_discardable_memory); | |
62 } | |
63 | |
64 bool DiscardableMemory::InitializeAndLock(size_t size) { | |
65 // When this function returns true, fd_ should be larger or equal than 0 | |
66 // and g_num_discardable_memory is incremented by 1. Otherwise, fd_ | |
67 // is less than 0 and g_num_discardable_memory is not incremented by | |
68 // the caller. | |
69 DCHECK_EQ(fd_, -1); | |
70 DCHECK(!memory_); | |
71 if (!ReserveFileDescriptor()) | |
72 return false; | |
73 | |
74 size_ = size; | |
75 fd_ = ashmem_create_region("", size); | |
76 | |
77 if (fd_ < 0) { | |
78 DLOG(ERROR) << "ashmem_create_region() failed"; | |
79 ReleaseFileDescriptor(); | |
80 return false; | |
81 } | |
82 | |
83 int err = ashmem_set_prot_region(fd_, PROT_READ | PROT_WRITE); | |
84 if (err < 0) { | |
85 DLOG(ERROR) << "Error " << err << " when setting protection of ashmem"; | |
86 HANDLE_EINTR(close(fd_)); | |
87 fd_ = -1; | |
88 ReleaseFileDescriptor(); | |
89 return false; | |
90 } | |
91 | |
92 if (!Map()) { | |
93 // Close the file descriptor in case of any initialization errors. | |
94 HANDLE_EINTR(close(fd_)); | |
95 fd_ = -1; | |
96 ReleaseFileDescriptor(); | |
97 return false; | |
98 } | |
99 | |
100 is_locked_ = true; | |
101 return true; | |
102 } | |
103 | |
104 LockDiscardableMemoryStatus DiscardableMemory::Lock() { | |
105 DCHECK_NE(fd_, -1); | |
106 DCHECK(!is_locked_); | |
107 | |
108 bool purged = false; | |
109 if (ashmem_pin_region(fd_, 0, 0) == ASHMEM_WAS_PURGED) | |
110 purged = true; | |
111 | |
112 if (!Map()) | |
113 return DISCARDABLE_MEMORY_FAILED; | |
114 | |
115 is_locked_ = true; | |
116 return purged ? DISCARDABLE_MEMORY_PURGED : DISCARDABLE_MEMORY_SUCCESS; | |
117 } | |
118 | |
119 void DiscardableMemory::Unlock() { | |
120 DCHECK_GE(fd_, 0); | |
121 DCHECK(is_locked_); | |
122 | |
123 Unmap(); | |
124 if (ashmem_unpin_region(fd_, 0, 0)) | |
125 DLOG(ERROR) << "Failed to unpin memory."; | |
126 is_locked_ = false; | |
127 } | |
128 | |
129 bool DiscardableMemory::Map() { | |
130 DCHECK(!memory_); | |
131 // There is a problem using MAP_PRIVATE here. As we are constantly calling | |
132 // Lock() and Unlock(), data could get lost if they are not written to the | |
133 // underlying file when Unlock() gets called. | |
134 memory_ = mmap(NULL, size_, PROT_READ | PROT_WRITE, MAP_SHARED, fd_, 0); | |
135 if (memory_ == (void*)-1) { | |
136 DPLOG(ERROR) << "Failed to map memory."; | |
137 memory_ = NULL; | |
138 if (ashmem_unpin_region(fd_, 0, 0)) | |
139 DLOG(ERROR) << "Failed to unpin memory."; | |
140 return false; | |
141 } | |
142 return true; | |
143 } | |
144 | |
145 void DiscardableMemory::Unmap() { | |
146 DCHECK(memory_); | |
147 | |
148 if (-1 == munmap(memory_, size_)) | |
149 DPLOG(ERROR) << "Failed to unmap memory."; | |
150 | |
151 memory_ = NULL; | |
152 } | 156 } |
153 | 157 |
154 // static | 158 // static |
155 bool DiscardableMemory::PurgeForTestingSupported() { | 159 bool DiscardableMemory::PurgeForTestingSupported() { |
156 return false; | 160 return false; |
157 } | 161 } |
158 | 162 |
159 // static | 163 // static |
160 void DiscardableMemory::PurgeForTesting() { | 164 void DiscardableMemory::PurgeForTesting() { |
161 NOTIMPLEMENTED(); | 165 NOTIMPLEMENTED(); |
162 } | 166 } |
163 | 167 |
164 } // namespace base | 168 } // namespace base |
OLD | NEW |