AnonymousVMObject.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <Kernel/Arch/x86/SmapDisabler.h>
  7. #include <Kernel/Debug.h>
  8. #include <Kernel/Process.h>
  9. #include <Kernel/VM/AnonymousVMObject.h>
  10. #include <Kernel/VM/MemoryManager.h>
  11. #include <Kernel/VM/PhysicalPage.h>
  12. namespace Kernel {
  13. RefPtr<VMObject> AnonymousVMObject::try_clone()
  14. {
  15. // We need to acquire our lock so we copy a sane state
  16. ScopedSpinLock lock(m_lock);
  17. if (is_purgeable() && is_volatile()) {
  18. // If this object is purgeable+volatile, create a new zero-filled purgeable+volatile
  19. // object, effectively "pre-purging" it in the child process.
  20. auto clone = try_create_purgeable_with_size(size(), AllocationStrategy::None);
  21. if (!clone)
  22. return {};
  23. clone->m_volatile = true;
  24. return clone;
  25. }
  26. // We're the parent. Since we're about to become COW we need to
  27. // commit the number of pages that we need to potentially allocate
  28. // so that the parent is still guaranteed to be able to have all
  29. // non-volatile memory available.
  30. size_t new_cow_pages_needed = page_count();
  31. dbgln_if(COMMIT_DEBUG, "Cloning {:p}, need {} committed cow pages", this, new_cow_pages_needed);
  32. if (!MM.commit_user_physical_pages(new_cow_pages_needed))
  33. return {};
  34. // Create or replace the committed cow pages. When cloning a previously
  35. // cloned vmobject, we want to essentially "fork", leaving us and the
  36. // new clone with one set of shared committed cow pages, and the original
  37. // one would keep the one it still has. This ensures that the original
  38. // one and this one, as well as the clone have sufficient resources
  39. // to cow all pages as needed
  40. m_shared_committed_cow_pages = try_create<CommittedCowPages>(new_cow_pages_needed);
  41. if (!m_shared_committed_cow_pages) {
  42. MM.uncommit_user_physical_pages(new_cow_pages_needed);
  43. return {};
  44. }
  45. // Both original and clone become COW. So create a COW map for ourselves
  46. // or reset all pages to be copied again if we were previously cloned
  47. ensure_or_reset_cow_map();
  48. // FIXME: If this allocation fails, we need to rollback all changes.
  49. return adopt_ref_if_nonnull(new (nothrow) AnonymousVMObject(*this));
  50. }
  51. RefPtr<AnonymousVMObject> AnonymousVMObject::try_create_with_size(size_t size, AllocationStrategy commit)
  52. {
  53. if (commit == AllocationStrategy::Reserve || commit == AllocationStrategy::AllocateNow) {
  54. // We need to attempt to commit before actually creating the object
  55. if (!MM.commit_user_physical_pages(ceil_div(size, static_cast<size_t>(PAGE_SIZE))))
  56. return {};
  57. }
  58. return adopt_ref_if_nonnull(new (nothrow) AnonymousVMObject(size, commit));
  59. }
  60. RefPtr<AnonymousVMObject> AnonymousVMObject::try_create_physically_contiguous_with_size(size_t size)
  61. {
  62. auto contiguous_physical_pages = MM.allocate_contiguous_supervisor_physical_pages(size);
  63. if (contiguous_physical_pages.is_empty())
  64. return {};
  65. return adopt_ref_if_nonnull(new (nothrow) AnonymousVMObject(contiguous_physical_pages.span()));
  66. }
  67. RefPtr<AnonymousVMObject> AnonymousVMObject::try_create_purgeable_with_size(size_t size, AllocationStrategy commit)
  68. {
  69. if (commit == AllocationStrategy::Reserve || commit == AllocationStrategy::AllocateNow) {
  70. // We need to attempt to commit before actually creating the object
  71. if (!MM.commit_user_physical_pages(ceil_div(size, static_cast<size_t>(PAGE_SIZE))))
  72. return {};
  73. }
  74. auto vmobject = adopt_ref_if_nonnull(new (nothrow) AnonymousVMObject(size, commit));
  75. if (!vmobject)
  76. return {};
  77. vmobject->m_purgeable = true;
  78. return vmobject;
  79. }
  80. RefPtr<AnonymousVMObject> AnonymousVMObject::try_create_with_physical_pages(Span<NonnullRefPtr<PhysicalPage>> physical_pages)
  81. {
  82. return adopt_ref_if_nonnull(new (nothrow) AnonymousVMObject(physical_pages));
  83. }
  84. RefPtr<AnonymousVMObject> AnonymousVMObject::try_create_for_physical_range(PhysicalAddress paddr, size_t size)
  85. {
  86. if (paddr.offset(size) < paddr) {
  87. dbgln("Shenanigans! try_create_for_physical_range({}, {}) would wrap around", paddr, size);
  88. return nullptr;
  89. }
  90. return adopt_ref_if_nonnull(new (nothrow) AnonymousVMObject(paddr, size));
  91. }
  92. AnonymousVMObject::AnonymousVMObject(size_t size, AllocationStrategy strategy)
  93. : VMObject(size)
  94. , m_unused_committed_pages(strategy == AllocationStrategy::Reserve ? page_count() : 0)
  95. {
  96. if (strategy == AllocationStrategy::AllocateNow) {
  97. // Allocate all pages right now. We know we can get all because we committed the amount needed
  98. for (size_t i = 0; i < page_count(); ++i)
  99. physical_pages()[i] = MM.allocate_committed_user_physical_page(MemoryManager::ShouldZeroFill::Yes);
  100. } else {
  101. auto& initial_page = (strategy == AllocationStrategy::Reserve) ? MM.lazy_committed_page() : MM.shared_zero_page();
  102. for (size_t i = 0; i < page_count(); ++i)
  103. physical_pages()[i] = initial_page;
  104. }
  105. }
  106. AnonymousVMObject::AnonymousVMObject(PhysicalAddress paddr, size_t size)
  107. : VMObject(size)
  108. {
  109. VERIFY(paddr.page_base() == paddr);
  110. for (size_t i = 0; i < page_count(); ++i)
  111. physical_pages()[i] = PhysicalPage::create(paddr.offset(i * PAGE_SIZE), MayReturnToFreeList::No);
  112. }
  113. AnonymousVMObject::AnonymousVMObject(Span<NonnullRefPtr<PhysicalPage>> physical_pages)
  114. : VMObject(physical_pages.size() * PAGE_SIZE)
  115. {
  116. for (size_t i = 0; i < physical_pages.size(); ++i) {
  117. m_physical_pages[i] = physical_pages[i];
  118. }
  119. }
  120. AnonymousVMObject::AnonymousVMObject(AnonymousVMObject const& other)
  121. : VMObject(other)
  122. , m_unused_committed_pages(other.m_unused_committed_pages)
  123. , m_cow_map() // do *not* clone this
  124. , m_shared_committed_cow_pages(other.m_shared_committed_cow_pages) // share the pool
  125. {
  126. // We can't really "copy" a spinlock. But we're holding it. Clear in the clone
  127. VERIFY(other.m_lock.is_locked());
  128. m_lock.initialize();
  129. // The clone also becomes COW
  130. ensure_or_reset_cow_map();
  131. if (m_unused_committed_pages > 0) {
  132. // The original vmobject didn't use up all committed pages. When
  133. // cloning (fork) we will overcommit. For this purpose we drop all
  134. // lazy-commit references and replace them with shared zero pages.
  135. for (size_t i = 0; i < page_count(); i++) {
  136. auto& phys_page = m_physical_pages[i];
  137. if (phys_page && phys_page->is_lazy_committed_page()) {
  138. phys_page = MM.shared_zero_page();
  139. if (--m_unused_committed_pages == 0)
  140. break;
  141. }
  142. }
  143. VERIFY(m_unused_committed_pages == 0);
  144. }
  145. }
  146. AnonymousVMObject::~AnonymousVMObject()
  147. {
  148. // Return any unused committed pages
  149. if (m_unused_committed_pages > 0)
  150. MM.uncommit_user_physical_pages(m_unused_committed_pages);
  151. }
  152. size_t AnonymousVMObject::purge()
  153. {
  154. ScopedSpinLock lock(m_lock);
  155. if (!is_purgeable() || !is_volatile())
  156. return 0;
  157. size_t total_pages_purged = 0;
  158. for (auto& page : m_physical_pages) {
  159. VERIFY(page);
  160. if (page->is_shared_zero_page())
  161. continue;
  162. page = MM.shared_zero_page();
  163. ++total_pages_purged;
  164. }
  165. m_was_purged = true;
  166. for_each_region([](Region& region) {
  167. region.remap();
  168. });
  169. return total_pages_purged;
  170. }
  171. KResult AnonymousVMObject::set_volatile(bool is_volatile, bool& was_purged)
  172. {
  173. VERIFY(is_purgeable());
  174. ScopedSpinLock locker(m_lock);
  175. was_purged = m_was_purged;
  176. if (m_volatile == is_volatile)
  177. return KSuccess;
  178. if (is_volatile) {
  179. // When a VMObject is made volatile, it gives up all of its committed memory.
  180. // Any physical pages already allocated remain in the VMObject for now, but the kernel is free to take them at any moment.
  181. for (auto& page : m_physical_pages) {
  182. if (page && page->is_lazy_committed_page())
  183. page = MM.shared_zero_page();
  184. }
  185. if (m_unused_committed_pages) {
  186. MM.uncommit_user_physical_pages(m_unused_committed_pages);
  187. m_unused_committed_pages = 0;
  188. }
  189. m_shared_committed_cow_pages = nullptr;
  190. m_volatile = true;
  191. m_was_purged = false;
  192. return KSuccess;
  193. }
  194. // When a VMObject is made non-volatile, we try to commit however many pages are not currently available.
  195. // If that fails, we return false to indicate that memory allocation failed.
  196. size_t committed_pages_needed = 0;
  197. for (auto& page : m_physical_pages) {
  198. VERIFY(page);
  199. if (page->is_shared_zero_page())
  200. ++committed_pages_needed;
  201. }
  202. if (!committed_pages_needed) {
  203. m_volatile = false;
  204. return KSuccess;
  205. }
  206. if (!MM.commit_user_physical_pages(committed_pages_needed))
  207. return ENOMEM;
  208. m_unused_committed_pages = committed_pages_needed;
  209. for (auto& page : m_physical_pages) {
  210. if (page->is_shared_zero_page())
  211. page = MM.lazy_committed_page();
  212. }
  213. m_volatile = false;
  214. m_was_purged = false;
  215. return KSuccess;
  216. }
  217. NonnullRefPtr<PhysicalPage> AnonymousVMObject::allocate_committed_page(Badge<Region>)
  218. {
  219. {
  220. ScopedSpinLock lock(m_lock);
  221. VERIFY(m_unused_committed_pages > 0);
  222. --m_unused_committed_pages;
  223. }
  224. return MM.allocate_committed_user_physical_page(MemoryManager::ShouldZeroFill::Yes);
  225. }
  226. Bitmap& AnonymousVMObject::ensure_cow_map()
  227. {
  228. if (m_cow_map.is_null())
  229. m_cow_map = Bitmap { page_count(), true };
  230. return m_cow_map;
  231. }
  232. void AnonymousVMObject::ensure_or_reset_cow_map()
  233. {
  234. if (m_cow_map.is_null())
  235. ensure_cow_map();
  236. else
  237. m_cow_map.fill(true);
  238. }
  239. bool AnonymousVMObject::should_cow(size_t page_index, bool is_shared) const
  240. {
  241. auto& page = physical_pages()[page_index];
  242. if (page && (page->is_shared_zero_page() || page->is_lazy_committed_page()))
  243. return true;
  244. if (is_shared)
  245. return false;
  246. return !m_cow_map.is_null() && m_cow_map.get(page_index);
  247. }
  248. void AnonymousVMObject::set_should_cow(size_t page_index, bool cow)
  249. {
  250. ensure_cow_map().set(page_index, cow);
  251. }
  252. size_t AnonymousVMObject::cow_pages() const
  253. {
  254. if (m_cow_map.is_null())
  255. return 0;
  256. return m_cow_map.count_slow(true);
  257. }
  258. PageFaultResponse AnonymousVMObject::handle_cow_fault(size_t page_index, VirtualAddress vaddr)
  259. {
  260. VERIFY_INTERRUPTS_DISABLED();
  261. ScopedSpinLock lock(m_lock);
  262. if (is_volatile()) {
  263. // A COW fault in a volatile region? Userspace is writing to volatile memory, this is a bug. Crash.
  264. dbgln("COW fault in volatile region, will crash.");
  265. return PageFaultResponse::ShouldCrash;
  266. }
  267. auto& page_slot = physical_pages()[page_index];
  268. bool have_committed = m_shared_committed_cow_pages;
  269. if (page_slot->ref_count() == 1) {
  270. dbgln_if(PAGE_FAULT_DEBUG, " >> It's a COW page but nobody is sharing it anymore. Remap r/w");
  271. set_should_cow(page_index, false);
  272. if (have_committed) {
  273. if (m_shared_committed_cow_pages->return_one())
  274. m_shared_committed_cow_pages = nullptr;
  275. }
  276. return PageFaultResponse::Continue;
  277. }
  278. RefPtr<PhysicalPage> page;
  279. if (have_committed) {
  280. dbgln_if(PAGE_FAULT_DEBUG, " >> It's a committed COW page and it's time to COW!");
  281. page = m_shared_committed_cow_pages->allocate_one();
  282. } else {
  283. dbgln_if(PAGE_FAULT_DEBUG, " >> It's a COW page and it's time to COW!");
  284. page = MM.allocate_user_physical_page(MemoryManager::ShouldZeroFill::No);
  285. if (page.is_null()) {
  286. dmesgln("MM: handle_cow_fault was unable to allocate a physical page");
  287. return PageFaultResponse::OutOfMemory;
  288. }
  289. }
  290. u8* dest_ptr = MM.quickmap_page(*page);
  291. dbgln_if(PAGE_FAULT_DEBUG, " >> COW {} <- {}", page->paddr(), page_slot->paddr());
  292. {
  293. SmapDisabler disabler;
  294. void* fault_at;
  295. if (!safe_memcpy(dest_ptr, vaddr.as_ptr(), PAGE_SIZE, fault_at)) {
  296. if ((u8*)fault_at >= dest_ptr && (u8*)fault_at <= dest_ptr + PAGE_SIZE)
  297. dbgln(" >> COW: error copying page {}/{} to {}/{}: failed to write to page at {}",
  298. page_slot->paddr(), vaddr, page->paddr(), VirtualAddress(dest_ptr), VirtualAddress(fault_at));
  299. else if ((u8*)fault_at >= vaddr.as_ptr() && (u8*)fault_at <= vaddr.as_ptr() + PAGE_SIZE)
  300. dbgln(" >> COW: error copying page {}/{} to {}/{}: failed to read from page at {}",
  301. page_slot->paddr(), vaddr, page->paddr(), VirtualAddress(dest_ptr), VirtualAddress(fault_at));
  302. else
  303. VERIFY_NOT_REACHED();
  304. }
  305. }
  306. page_slot = move(page);
  307. MM.unquickmap_page();
  308. set_should_cow(page_index, false);
  309. return PageFaultResponse::Continue;
  310. }
  311. CommittedCowPages::CommittedCowPages(size_t committed_pages)
  312. : m_committed_pages(committed_pages)
  313. {
  314. }
  315. CommittedCowPages::~CommittedCowPages()
  316. {
  317. // Return unused committed pages
  318. if (m_committed_pages > 0)
  319. MM.uncommit_user_physical_pages(m_committed_pages);
  320. }
  321. NonnullRefPtr<PhysicalPage> CommittedCowPages::allocate_one()
  322. {
  323. VERIFY(m_committed_pages > 0);
  324. m_committed_pages--;
  325. return MM.allocate_committed_user_physical_page(MemoryManager::ShouldZeroFill::Yes);
  326. }
  327. bool CommittedCowPages::return_one()
  328. {
  329. VERIFY(m_committed_pages > 0);
  330. m_committed_pages--;
  331. MM.uncommit_user_physical_pages(1);
  332. return m_committed_pages == 0;
  333. }
  334. }