AnonymousVMObject.cpp 15 KB

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