aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2019-01-05 10:46:42 -0500
committerJack Lloyd <[email protected]>2019-01-05 10:50:16 -0500
commit23bd016eb9864c93fffbf87c97adf50444394bfb (patch)
treeabfc5ca888080c83ff376cc6dddcec1fe6c7fbd1
parent5930abcb9bebbb6ce03463d80a8551f048f7817a (diff)
Revert #1798
The problem in #602 is not the use of mmap but the use of mmap with MAP_SHARED. Using MAP_PRIVATE creates a CoW mapping just like malloc or posix_memalign would. I'm not sure why it took me so long to figure this out ...
-rw-r--r--src/lib/utils/os_utils.cpp18
1 files changed, 14 insertions, 4 deletions
diff --git a/src/lib/utils/os_utils.cpp b/src/lib/utils/os_utils.cpp
index 172fc27be..f373ece99 100644
--- a/src/lib/utils/os_utils.cpp
+++ b/src/lib/utils/os_utils.cpp
@@ -339,15 +339,25 @@ std::vector<void*> OS::allocate_locked_pages(size_t count)
void* ptr = nullptr;
#if defined(BOTAN_TARGET_OS_HAS_POSIX1) && defined(BOTAN_TARGET_OS_HAS_POSIX_MLOCK)
- int rc = ::posix_memalign(&ptr, page_size, 2*page_size);
- if(rc != 0 || ptr == nullptr)
+#if !defined(MAP_NOCORE)
+ #define MAP_NOCORE 0
+#endif
+
+ ptr = ::mmap(nullptr, 2*page_size,
+ PROT_READ | PROT_WRITE,
+ MAP_ANONYMOUS | MAP_PRIVATE | MAP_NOCORE,
+ /*fd=*/-1, /*offset=*/0);
+
+ if(ptr == MAP_FAILED)
+ {
continue;
+ }
// failed to lock
if(::mlock(ptr, page_size) != 0)
{
- std::free(ptr);
+ ::munmap(ptr, 2*page_size);
continue;
}
@@ -421,7 +431,7 @@ void OS::free_locked_pages(const std::vector<void*>& pages)
#if defined(BOTAN_TARGET_OS_HAS_POSIX1) && defined(BOTAN_TARGET_OS_HAS_POSIX_MLOCK)
::munlock(ptr, page_size);
- std::free(ptr);
+ ::munmap(ptr, 2*page_size);
#elif defined(BOTAN_TARGET_OS_HAS_VIRTUAL_LOCK)
::VirtualUnlock(ptr, page_size);
::VirtualFree(ptr, 0, MEM_RELEASE);