1 // Copyright 2019 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.
4 
5 #ifndef BASE_MEMORY_MADV_FREE_DISCARDABLE_MEMORY_POSIX_H_
6 #define BASE_MEMORY_MADV_FREE_DISCARDABLE_MEMORY_POSIX_H_
7 
8 #include <stddef.h>
9 
10 #include <atomic>
11 #include <cstdint>
12 
13 #include "base/base_export.h"
14 #include "base/callback.h"
15 #include "base/check_op.h"
16 #include "base/macros.h"
17 #include "base/memory/discardable_memory.h"
18 #include "base/sequence_checker.h"
19 #include "base/threading/thread_collision_warner.h"
20 #include "build/build_config.h"
21 
22 namespace base {
23 // Discardable memory backed by the MADV_FREE advice value, available since
24 // Linux 4.5.
25 //
26 // When unlocked, this implementation of discardable memory will
27 // apply the MADV_FREE advice value to all pages within the allocated range,
28 // causing pages to be discarded instead of swapped upon memory pressure.
29 // When pages are discarded, they become zero-fill-on-demand pages.
30 // Attempting to unlock an already-unlocked instance is undefined behaviour.
31 //
32 // When locked, all pages will be checked for eviction. If any page has
33 // been discarded, the entire allocated range is unmapped and the lock fails.
34 // After a failed lock, the instance remains unlocked but any further attempts
35 // to lock will fail. Additionally, the discardable memory instance is
36 // invalidated and access to memory obtained via data() is undefined behaviour.
37 // Attempting to lock an already-locked instance is undefined behaviour. If no
38 // page in the allocated range has been discarded, then lock succeeds and the
39 // allocated range of memory is available for use without any page fault,
40 // additional allocations, or memory zeroing.
41 //
42 // If DCHECK_IS_ON(), additional checks are added to ensure that the discardable
43 // memory instance is being used correctly. These checks are not present by
44 // default, as some incur a significant performance penalty or do not warrant
45 // crashing the process. These checks are:
46 // -    Do not allow lock while already locked or unlock while already unlocked
47 // -    Do not allow memory access via data() if instance is deallocated after
48 //      Lock() (although invalid memory can still be accessed through existing
49 //      pointers)
50 // -    After Unlock(), disallow read or write of memory pointed to by data()
51 //      with PROT_NONE until next Lock()
52 //
53 // Caveats:
54 // [1]: The smallest allocation unit is the size of a page, so it is
55 //      unsuitable for small allocations.
56 //
57 // [2]: The size of a discardable memory instance must be greater than 0 bytes.
58 //
59 class BASE_EXPORT MadvFreeDiscardableMemoryPosix : public DiscardableMemory {
60  public:
61   MadvFreeDiscardableMemoryPosix(size_t size_in_pages,
62                                  std::atomic<size_t>* allocator_byte_count);
63   ~MadvFreeDiscardableMemoryPosix() override;
64 
65   bool Lock() override;
66   void Unlock() override;
67   void* data() const override;
68 
69   bool IsLockedForTesting() const;
70   void DiscardForTesting() override;
71 
72   trace_event::MemoryAllocatorDump* CreateMemoryAllocatorDump(
73       const char* name,
74       trace_event::ProcessMemoryDump* pmd) const override;
75 
76  protected:
GetPageCount()77   size_t GetPageCount() const { return allocated_pages_; }
78 
79   bool IsValid() const;
80 
81   void SetKeepMemoryForTesting(bool keep_memory);
82 
83   // Force page discard by applying MADV_DONTNEED hint on a page.
84   // Has the same effect as if the page was naturally discarded during
85   // memory pressure due to MADV_FREE (i.e. zero-fill-on-demand pages for
86   // anonymous private mappings).
87   // Note that MADV_DONTNEED takes effect immediately for non-shared mappings.
88   void DiscardPage(size_t page_index);
89 
90  private:
91   bool LockPage(size_t page_index);
92   void UnlockPage(size_t page_index);
93 
94   bool Deallocate();
95 
96   // Gets whether this instance has been discarded (but not yet unmapped).
97   bool IsDiscarded() const;
98 
99   // Get whether all pages in this discardable memory instance are resident.
100   bool IsResident() const;
101 
102   const size_t size_in_bytes_;
103   const size_t allocated_pages_;
104 
105   // Pointer to allocator memory usage metric for updating upon allocation and
106   // destruction.
107   std::atomic<size_t>* allocator_byte_count_;
108 
109   void* data_;
110   bool is_locked_ = true;
111 
112   // If true, MADV_FREE will not be set on Unlock().
113   bool keep_memory_for_testing_ = false;
114 
115   // Stores the first word of a page for use during locking.
116   std::vector<std::atomic<intptr_t>> page_first_word_;
117 
118   DFAKE_MUTEX(thread_collision_warner_);
119 
120   DISALLOW_COPY_AND_ASSIGN(MadvFreeDiscardableMemoryPosix);
121 };
122 
123 enum class MadvFreeSupport { kUnsupported, kSupported };
124 BASE_EXPORT MadvFreeSupport GetMadvFreeSupport();
125 
126 }  // namespace base
127 
128 #endif  // BASE_MEMORY_MADV_FREE_DISCARDABLE_MEMORY_POSIX_H_
129