1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/memory/discardable_shared_memory.h"
13 #include "base/atomicops.h"
14 #include "base/logging.h"
15 #include "base/numerics/safe_math.h"
16 #include "base/process/process_metrics.h"
18 #if defined(OS_ANDROID)
19 #include "third_party/ashmem/ashmem.h"
25 // Use a machine-sized pointer as atomic type. It will use the Atomic32 or
26 // Atomic64 routines, depending on the architecture.
27 typedef intptr_t AtomicType
;
28 typedef uintptr_t UAtomicType
;
30 // Template specialization for timestamp serialization/deserialization. This
31 // is used to serialize timestamps using Unix time on systems where AtomicType
32 // does not have enough precision to contain a timestamp in the standard
35 Time
TimeFromWireFormat(int64 value
);
37 int64
TimeToWireFormat(Time time
);
39 // Serialize to Unix time when using 4-byte wire format.
40 // Note: 19 January 2038, this will cease to work.
42 Time ALLOW_UNUSED_TYPE TimeFromWireFormat
<4>(int64 value
) {
43 return value
? Time::UnixEpoch() + TimeDelta::FromSeconds(value
) : Time();
46 int64 ALLOW_UNUSED_TYPE TimeToWireFormat
<4>(Time time
) {
47 return time
> Time::UnixEpoch() ? (time
- Time::UnixEpoch()).InSeconds() : 0;
50 // Standard serialization format when using 8-byte wire format.
52 Time ALLOW_UNUSED_TYPE TimeFromWireFormat
<8>(int64 value
) {
53 return Time::FromInternalValue(value
);
56 int64 ALLOW_UNUSED_TYPE TimeToWireFormat
<8>(Time time
) {
57 return time
.ToInternalValue();
61 enum LockState
{ UNLOCKED
= 0, LOCKED
= 1 };
63 explicit SharedState(AtomicType ivalue
) { value
.i
= ivalue
; }
64 SharedState(LockState lock_state
, Time timestamp
) {
65 int64 wire_timestamp
= TimeToWireFormat
<sizeof(AtomicType
)>(timestamp
);
66 DCHECK_GE(wire_timestamp
, 0);
67 DCHECK_EQ(lock_state
& ~1, 0);
68 value
.u
= (static_cast<UAtomicType
>(wire_timestamp
) << 1) | lock_state
;
71 LockState
GetLockState() const { return static_cast<LockState
>(value
.u
& 1); }
73 Time
GetTimestamp() const {
74 return TimeFromWireFormat
<sizeof(AtomicType
)>(value
.u
>> 1);
77 // Bit 1: Lock state. Bit is set when locked.
78 // Bit 2..sizeof(AtomicType)*8: Usage timestamp. NULL time when locked or
86 // Shared state is stored at offset 0 in shared memory segments.
87 SharedState
* SharedStateFromSharedMemory(const SharedMemory
& shared_memory
) {
88 DCHECK(shared_memory
.memory());
89 return static_cast<SharedState
*>(shared_memory
.memory());
92 // Round up |size| to a multiple of alignment, which must be a power of two.
93 size_t Align(size_t alignment
, size_t size
) {
94 DCHECK_EQ(alignment
& (alignment
- 1), 0u);
95 return (size
+ alignment
- 1) & ~(alignment
- 1);
98 // Round up |size| to a multiple of page size.
99 size_t AlignToPageSize(size_t size
) {
100 return Align(base::GetPageSize(), size
);
105 DiscardableSharedMemory::DiscardableSharedMemory()
106 : mapped_size_(0), locked_page_count_(0) {
109 DiscardableSharedMemory::DiscardableSharedMemory(
110 SharedMemoryHandle shared_memory_handle
)
111 : shared_memory_(shared_memory_handle
, false),
113 locked_page_count_(0) {
116 DiscardableSharedMemory::~DiscardableSharedMemory() {
119 bool DiscardableSharedMemory::CreateAndMap(size_t size
) {
120 CheckedNumeric
<size_t> checked_size
= size
;
121 checked_size
+= AlignToPageSize(sizeof(SharedState
));
122 if (!checked_size
.IsValid())
125 if (!shared_memory_
.CreateAndMapAnonymous(checked_size
.ValueOrDie()))
129 shared_memory_
.mapped_size() - AlignToPageSize(sizeof(SharedState
));
131 locked_page_count_
= AlignToPageSize(mapped_size_
) / base::GetPageSize();
133 for (size_t page
= 0; page
< locked_page_count_
; ++page
)
134 locked_pages_
.insert(page
);
137 DCHECK(last_known_usage_
.is_null());
138 SharedState
new_state(SharedState::LOCKED
, Time());
139 subtle::Release_Store(&SharedStateFromSharedMemory(shared_memory_
)->value
.i
,
144 bool DiscardableSharedMemory::Map(size_t size
) {
145 if (!shared_memory_
.Map(AlignToPageSize(sizeof(SharedState
)) + size
))
149 shared_memory_
.mapped_size() - AlignToPageSize(sizeof(SharedState
));
151 locked_page_count_
= AlignToPageSize(mapped_size_
) / base::GetPageSize();
153 for (size_t page
= 0; page
< locked_page_count_
; ++page
)
154 locked_pages_
.insert(page
);
160 bool DiscardableSharedMemory::Lock(size_t offset
, size_t length
) {
161 DCHECK_EQ(AlignToPageSize(offset
), offset
);
162 DCHECK_EQ(AlignToPageSize(length
), length
);
164 // Calls to this function must synchronized properly.
165 DFAKE_SCOPED_LOCK(thread_collision_warner_
);
167 // Return false when instance has been purged or not initialized properly by
168 // checking if |last_known_usage_| is NULL.
169 if (last_known_usage_
.is_null())
172 DCHECK(shared_memory_
.memory());
174 // We need to successfully acquire the platform independent lock before
175 // individual pages can be locked.
176 if (!locked_page_count_
) {
177 SharedState
old_state(SharedState::UNLOCKED
, last_known_usage_
);
178 SharedState
new_state(SharedState::LOCKED
, Time());
179 SharedState
result(subtle::Acquire_CompareAndSwap(
180 &SharedStateFromSharedMemory(shared_memory_
)->value
.i
,
183 if (result
.value
.u
!= old_state
.value
.u
) {
184 // Update |last_known_usage_| in case the above CAS failed because of
185 // an incorrect timestamp.
186 last_known_usage_
= result
.GetTimestamp();
191 // Zero for length means "everything onward".
193 length
= AlignToPageSize(mapped_size_
) - offset
;
195 size_t start
= offset
/ base::GetPageSize();
196 size_t end
= start
+ length
/ base::GetPageSize();
197 DCHECK_LT(start
, end
);
198 DCHECK_LE(end
, AlignToPageSize(mapped_size_
) / base::GetPageSize());
200 // Add pages to |locked_page_count_|.
201 // Note: Locking a page that is already locked is an error.
202 locked_page_count_
+= end
- start
;
204 // Detect incorrect usage by keeping track of exactly what pages are locked.
205 for (auto page
= start
; page
< end
; ++page
) {
206 auto result
= locked_pages_
.insert(page
);
207 DCHECK(result
.second
);
209 DCHECK_EQ(locked_pages_
.size(), locked_page_count_
);
212 #if defined(OS_ANDROID)
213 SharedMemoryHandle handle
= shared_memory_
.handle();
214 DCHECK(SharedMemory::IsHandleValid(handle
));
215 if (ashmem_pin_region(
216 handle
.fd
, AlignToPageSize(sizeof(SharedState
)) + offset
, length
)) {
224 void DiscardableSharedMemory::Unlock(size_t offset
, size_t length
) {
225 DCHECK_EQ(AlignToPageSize(offset
), offset
);
226 DCHECK_EQ(AlignToPageSize(length
), length
);
228 // Calls to this function must synchronized properly.
229 DFAKE_SCOPED_LOCK(thread_collision_warner_
);
231 // Zero for length means "everything onward".
233 length
= AlignToPageSize(mapped_size_
) - offset
;
235 DCHECK(shared_memory_
.memory());
237 #if defined(OS_ANDROID)
238 SharedMemoryHandle handle
= shared_memory_
.handle();
239 DCHECK(SharedMemory::IsHandleValid(handle
));
240 if (ashmem_unpin_region(
241 handle
.fd
, AlignToPageSize(sizeof(SharedState
)) + offset
, length
)) {
242 DPLOG(ERROR
) << "ashmem_unpin_region() failed";
246 size_t start
= offset
/ base::GetPageSize();
247 size_t end
= start
+ length
/ base::GetPageSize();
248 DCHECK_LT(start
, end
);
249 DCHECK_LE(end
, AlignToPageSize(mapped_size_
) / base::GetPageSize());
251 // Remove pages from |locked_page_count_|.
252 // Note: Unlocking a page that is not locked is an error.
253 DCHECK_GE(locked_page_count_
, end
- start
);
254 locked_page_count_
-= end
- start
;
256 // Detect incorrect usage by keeping track of exactly what pages are locked.
257 for (auto page
= start
; page
< end
; ++page
) {
258 auto erased_count
= locked_pages_
.erase(page
);
259 DCHECK_EQ(1u, erased_count
);
261 DCHECK_EQ(locked_pages_
.size(), locked_page_count_
);
264 // Early out and avoid releasing the platform independent lock if some pages
266 if (locked_page_count_
)
269 Time current_time
= Now();
270 DCHECK(!current_time
.is_null());
272 SharedState
old_state(SharedState::LOCKED
, Time());
273 SharedState
new_state(SharedState::UNLOCKED
, current_time
);
274 // Note: timestamp cannot be NULL as that is a unique value used when
276 DCHECK(!new_state
.GetTimestamp().is_null());
277 // Timestamp precision should at least be accurate to the second.
278 DCHECK_EQ((new_state
.GetTimestamp() - Time::UnixEpoch()).InSeconds(),
279 (current_time
- Time::UnixEpoch()).InSeconds());
280 SharedState
result(subtle::Release_CompareAndSwap(
281 &SharedStateFromSharedMemory(shared_memory_
)->value
.i
,
285 DCHECK_EQ(old_state
.value
.u
, result
.value
.u
);
287 last_known_usage_
= current_time
;
290 void* DiscardableSharedMemory::memory() const {
291 return reinterpret_cast<uint8
*>(shared_memory_
.memory()) +
292 AlignToPageSize(sizeof(SharedState
));
295 bool DiscardableSharedMemory::Purge(Time current_time
) {
296 // Calls to this function must synchronized properly.
297 DFAKE_SCOPED_LOCK(thread_collision_warner_
);
299 // Early out if not mapped. This can happen if the segment was previously
300 // unmapped using a call to Close().
301 if (!shared_memory_
.memory())
304 SharedState
old_state(SharedState::UNLOCKED
, last_known_usage_
);
305 SharedState
new_state(SharedState::UNLOCKED
, Time());
306 SharedState
result(subtle::Acquire_CompareAndSwap(
307 &SharedStateFromSharedMemory(shared_memory_
)->value
.i
,
311 // Update |last_known_usage_| to |current_time| if the memory is locked. This
312 // allows the caller to determine if purging failed because last known usage
313 // was incorrect or memory was locked. In the second case, the caller should
314 // most likely wait for some amount of time before attempting to purge the
316 if (result
.value
.u
!= old_state
.value
.u
) {
317 last_known_usage_
= result
.GetLockState() == SharedState::LOCKED
319 : result
.GetTimestamp();
323 last_known_usage_
= Time();
327 bool DiscardableSharedMemory::PurgeAndTruncate(Time current_time
) {
328 if (!Purge(current_time
))
331 #if defined(OS_POSIX)
332 // Truncate shared memory to size of SharedState.
333 SharedMemoryHandle handle
= shared_memory_
.handle();
334 if (SharedMemory::IsHandleValid(handle
)) {
335 if (HANDLE_EINTR(ftruncate(handle
.fd
, sizeof(SharedState
))) != 0)
336 DPLOG(ERROR
) << "ftruncate() failed";
343 bool DiscardableSharedMemory::IsMemoryResident() const {
344 DCHECK(shared_memory_
.memory());
346 SharedState
result(subtle::NoBarrier_Load(
347 &SharedStateFromSharedMemory(shared_memory_
)->value
.i
));
349 return result
.GetLockState() == SharedState::LOCKED
||
350 !result
.GetTimestamp().is_null();
353 void DiscardableSharedMemory::Close() {
354 shared_memory_
.Unmap();
355 shared_memory_
.Close();
359 Time
DiscardableSharedMemory::Now() const {