| 1 | // Copyright (c) 2018-2022, The Monero Project |
| 2 | |
| 3 | // |
| 4 | // All rights reserved. |
| 5 | // |
| 6 | // Redistribution and use in source and binary forms, with or without modification, are |
| 7 | // permitted provided that the following conditions are met: |
| 8 | // |
| 9 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 10 | // conditions and the following disclaimer. |
| 11 | // |
| 12 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 13 | // of conditions and the following disclaimer in the documentation and/or other |
| 14 | // materials provided with the distribution. |
| 15 | // |
| 16 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 17 | // used to endorse or promote products derived from this software without specific |
| 18 | // prior written permission. |
| 19 | // |
| 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 21 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 22 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 23 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 27 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 28 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | #if defined __GNUC__ && !defined _WIN32 |
| 31 | #define HAVE_MLOCK 1 |
| 32 | #endif |
| 33 | |
| 34 | #include <unistd.h> |
| 35 | #if defined HAVE_MLOCK |
| 36 | #include <sys/mman.h> |
| 37 | #endif |
| 38 | #include "misc_log_ex.h" |
| 39 | #include "syncobj.h" |
| 40 | #include "mlocker.h" |
| 41 | |
| 42 | #include <atomic> |
| 43 | |
| 44 | #undef MONERO_DEFAULT_LOG_CATEGORY |
| 45 | #define MONERO_DEFAULT_LOG_CATEGORY "mlocker" |
| 46 | |
| 47 | // did an mlock operation previously fail? we only |
| 48 | // want to log an error once and be done with it |
| 49 | static std::atomic<bool> previously_failed{ false }; |
| 50 | |
| 51 | static size_t query_page_size() |
| 52 | { |
| 53 | #if defined HAVE_MLOCK |
| 54 | long ret = sysconf(_SC_PAGESIZE); |
| 55 | if (ret <= 0) |
| 56 | { |
| 57 | MERROR("Failed to determine page size"); |
| 58 | return 0; |
| 59 | } |
| 60 | return ret; |
| 61 | #else |
| 62 | #warning Missing query_page_size implementation |
| 63 | #endif |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | static void do_lock(void *ptr, size_t len) |
| 68 | { |
| 69 | #if defined HAVE_MLOCK |
| 70 | int ret = mlock(ptr, len); |
| 71 | if (ret < 0 && !previously_failed.exchange(true)) |
| 72 | MERROR("Error locking page at " << ptr << ": " << strerror(errno) << ", subsequent mlock errors will be silenced"); |
| 73 | #else |
| 74 | #warning Missing do_lock implementation |
| 75 | #endif |
| 76 | } |
| 77 | |
| 78 | static void do_unlock(void *ptr, size_t len) |
| 79 | { |
| 80 | #if defined HAVE_MLOCK |
| 81 | int ret = munlock(ptr, len); |
| 82 | // check whether we previously failed, but don't set it, this is just |
| 83 | // to pacify the errors of mlock()ing failed, in which case unlocking |
| 84 | // is also not going to work of course |
| 85 | if (ret < 0 && !previously_failed.load()) |
| 86 | MERROR("Error unlocking page at " << ptr << ": " << strerror(errno)); |
| 87 | #else |
| 88 | #warning Missing implementation of page size detection |
| 89 | #endif |
| 90 | } |
| 91 | |
| 92 | namespace epee |
| 93 | { |
| 94 | size_t mlocker::page_size = 0; |
| 95 | size_t mlocker::num_locked_objects = 0; |
| 96 | |
| 97 | boost::mutex &mlocker::mutex() |
| 98 | { |
| 99 | static boost::mutex *vmutex = new boost::mutex(); |
| 100 | return *vmutex; |
| 101 | } |
| 102 | std::map<size_t, unsigned int> &mlocker::map() |
| 103 | { |
| 104 | static std::map<size_t, unsigned int> *vmap = new std::map<size_t, unsigned int>(); |
| 105 | return *vmap; |
| 106 | } |
| 107 | |
| 108 | size_t mlocker::get_page_size() |
| 109 | { |
| 110 | CRITICAL_REGION_LOCAL(mutex()); |
| 111 | if (page_size == 0) |
| 112 | page_size = query_page_size(); |
| 113 | return page_size; |
| 114 | } |
| 115 | |
| 116 | mlocker::mlocker(void *ptr, size_t len): ptr(ptr), len(len) |
| 117 | { |
| 118 | lock(ptr, len); |
| 119 | } |
| 120 | |
| 121 | mlocker::~mlocker() |
| 122 | { |
| 123 | try { unlock(ptr, len); } |
| 124 | catch (...) { /* ignore and do not propagate through the dtor */ } |
| 125 | } |
| 126 | |
| 127 | void mlocker::lock(void *ptr, size_t len) |
| 128 | { |
| 129 | TRY_ENTRY(); |
| 130 | |
| 131 | size_t page_size = get_page_size(); |
| 132 | if (page_size == 0) |
| 133 | return; |
| 134 | |
| 135 | CRITICAL_REGION_LOCAL(mutex()); |
| 136 | const size_t first = ((uintptr_t)ptr) / page_size; |
| 137 | const size_t last = (((uintptr_t)ptr) + len - 1) / page_size; |
| 138 | for (size_t page = first; page <= last; ++page) |
| 139 | lock_page(page); |
| 140 | ++num_locked_objects; |
| 141 | |
| 142 | CATCH_ENTRY_L1("mlocker::lock", void()); |
| 143 | } |
| 144 | |
| 145 | void mlocker::unlock(void *ptr, size_t len) |
| 146 | { |
| 147 | TRY_ENTRY(); |
| 148 | |
| 149 | size_t page_size = get_page_size(); |
| 150 | if (page_size == 0) |
| 151 | return; |
| 152 | CRITICAL_REGION_LOCAL(mutex()); |
| 153 | const size_t first = ((uintptr_t)ptr) / page_size; |
| 154 | const size_t last = (((uintptr_t)ptr) + len - 1) / page_size; |
| 155 | for (size_t page = first; page <= last; ++page) |
| 156 | unlock_page(page); |
| 157 | --num_locked_objects; |
| 158 | |
| 159 | CATCH_ENTRY_L1("mlocker::lock", void()); |
| 160 | } |
| 161 | |
| 162 | size_t mlocker::get_num_locked_pages() |
| 163 | { |
| 164 | CRITICAL_REGION_LOCAL(mutex()); |
| 165 | return map().size(); |
| 166 | } |
| 167 | |
| 168 | size_t mlocker::get_num_locked_objects() |
| 169 | { |
| 170 | CRITICAL_REGION_LOCAL(mutex()); |
| 171 | return num_locked_objects; |
| 172 | } |
| 173 | |
| 174 | void mlocker::lock_page(size_t page) |
| 175 | { |
| 176 | std::pair<std::map<size_t, unsigned int>::iterator, bool> p = map().insert(std::make_pair(page, 1)); |
| 177 | if (p.second) |
| 178 | { |
| 179 | do_lock((void*)(page * page_size), page_size); |
| 180 | } |
| 181 | else |
| 182 | { |
| 183 | ++p.first->second; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | void mlocker::unlock_page(size_t page) |
| 188 | { |
| 189 | std::map<size_t, unsigned int>::iterator i = map().find(page); |
| 190 | if (i == map().end()) |
| 191 | { |
| 192 | MERROR("Attempt to unlock unlocked page at " << (void*)(page * page_size)); |
| 193 | } |
| 194 | else |
| 195 | { |
| 196 | if (!--i->second) |
| 197 | { |
| 198 | map().erase(i); |
| 199 | do_unlock((void*)(page * page_size), page_size); |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | } |