kmalloc.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Assertions.h>
  7. #include <AK/Types.h>
  8. #include <Kernel/Debug.h>
  9. #include <Kernel/Heap/Heap.h>
  10. #include <Kernel/Heap/kmalloc.h>
  11. #include <Kernel/KSyms.h>
  12. #include <Kernel/Locking/Spinlock.h>
  13. #include <Kernel/Memory/MemoryManager.h>
  14. #include <Kernel/Panic.h>
  15. #include <Kernel/PerformanceManager.h>
  16. #include <Kernel/Sections.h>
  17. #include <Kernel/StdLib.h>
  18. #if ARCH(I386)
  19. static constexpr size_t CHUNK_SIZE = 32;
  20. #else
  21. static constexpr size_t CHUNK_SIZE = 64;
  22. #endif
  23. static_assert(is_power_of_two(CHUNK_SIZE));
  24. static constexpr size_t INITIAL_KMALLOC_MEMORY_SIZE = 2 * MiB;
  25. // Treat the heap as logically separate from .bss
  26. __attribute__((section(".heap"))) static u8 initial_kmalloc_memory[INITIAL_KMALLOC_MEMORY_SIZE];
  27. namespace std {
  28. const nothrow_t nothrow;
  29. }
  30. static RecursiveSpinlock s_lock; // needs to be recursive because of dump_backtrace()
  31. struct KmallocSubheap {
  32. KmallocSubheap(u8* base, size_t size)
  33. : allocator(base, size)
  34. {
  35. }
  36. IntrusiveListNode<KmallocSubheap> list_node;
  37. using List = IntrusiveList<&KmallocSubheap::list_node>;
  38. Heap<CHUNK_SIZE, KMALLOC_SCRUB_BYTE, KFREE_SCRUB_BYTE> allocator;
  39. };
  40. class KmallocSlabBlock {
  41. public:
  42. static constexpr size_t block_size = 64 * KiB;
  43. static constexpr FlatPtr block_mask = ~(block_size - 1);
  44. KmallocSlabBlock(size_t slab_size)
  45. : m_slab_size(slab_size)
  46. , m_slab_count((block_size - sizeof(KmallocSlabBlock)) / slab_size)
  47. {
  48. for (size_t i = 0; i < m_slab_count; ++i) {
  49. auto* freelist_entry = (FreelistEntry*)(void*)(&m_data[i * slab_size]);
  50. freelist_entry->next = m_freelist;
  51. m_freelist = freelist_entry;
  52. }
  53. }
  54. void* allocate()
  55. {
  56. VERIFY(m_freelist);
  57. ++m_allocated_slabs;
  58. return exchange(m_freelist, m_freelist->next);
  59. }
  60. void deallocate(void* ptr)
  61. {
  62. VERIFY(ptr >= &m_data && ptr < ((u8*)this + block_size));
  63. --m_allocated_slabs;
  64. auto* freelist_entry = (FreelistEntry*)ptr;
  65. freelist_entry->next = m_freelist;
  66. m_freelist = freelist_entry;
  67. }
  68. bool is_full() const
  69. {
  70. return m_freelist == nullptr;
  71. }
  72. size_t allocated_bytes() const
  73. {
  74. return m_allocated_slabs * m_slab_size;
  75. }
  76. size_t free_bytes() const
  77. {
  78. return (m_slab_count - m_allocated_slabs) * m_slab_size;
  79. }
  80. IntrusiveListNode<KmallocSlabBlock> list_node;
  81. using List = IntrusiveList<&KmallocSlabBlock::list_node>;
  82. private:
  83. struct FreelistEntry {
  84. FreelistEntry* next;
  85. };
  86. FreelistEntry* m_freelist { nullptr };
  87. size_t m_slab_size { 0 };
  88. size_t m_slab_count { 0 };
  89. size_t m_allocated_slabs { 0 };
  90. [[gnu::aligned(16)]] u8 m_data[];
  91. };
  92. class KmallocSlabheap {
  93. public:
  94. KmallocSlabheap(size_t slab_size)
  95. : m_slab_size(slab_size)
  96. {
  97. }
  98. size_t slab_size() const { return m_slab_size; }
  99. void* allocate()
  100. {
  101. if (m_usable_blocks.is_empty()) {
  102. // FIXME: This allocation wastes `block_size` bytes due to the implementation of kmalloc_aligned().
  103. // Handle this with a custom VM+page allocator instead of using kmalloc_aligned().
  104. auto* slot = kmalloc_aligned(KmallocSlabBlock::block_size, KmallocSlabBlock::block_size);
  105. if (!slot) {
  106. // FIXME: Dare to return nullptr!
  107. PANIC("OOM while growing slabheap ({})", m_slab_size);
  108. }
  109. auto* block = new (slot) KmallocSlabBlock(m_slab_size);
  110. m_usable_blocks.append(*block);
  111. }
  112. auto* block = m_usable_blocks.first();
  113. auto* ptr = block->allocate();
  114. if (block->is_full())
  115. m_full_blocks.append(*block);
  116. memset(ptr, KMALLOC_SCRUB_BYTE, m_slab_size);
  117. return ptr;
  118. }
  119. void deallocate(void* ptr)
  120. {
  121. memset(ptr, KFREE_SCRUB_BYTE, m_slab_size);
  122. auto* block = (KmallocSlabBlock*)((FlatPtr)ptr & KmallocSlabBlock::block_mask);
  123. bool block_was_full = block->is_full();
  124. block->deallocate(ptr);
  125. if (block_was_full)
  126. m_usable_blocks.append(*block);
  127. }
  128. size_t allocated_bytes() const
  129. {
  130. size_t total = m_full_blocks.size_slow() * KmallocSlabBlock::block_size;
  131. for (auto const& slab_block : m_usable_blocks)
  132. total += slab_block.allocated_bytes();
  133. return total;
  134. }
  135. size_t free_bytes() const
  136. {
  137. size_t total = 0;
  138. for (auto const& slab_block : m_usable_blocks)
  139. total += slab_block.free_bytes();
  140. return total;
  141. }
  142. private:
  143. size_t m_slab_size { 0 };
  144. KmallocSlabBlock::List m_usable_blocks;
  145. KmallocSlabBlock::List m_full_blocks;
  146. };
  147. struct KmallocGlobalData {
  148. static constexpr size_t minimum_subheap_size = 1 * MiB;
  149. KmallocGlobalData(u8* initial_heap, size_t initial_heap_size)
  150. {
  151. add_subheap(initial_heap, initial_heap_size);
  152. }
  153. void add_subheap(u8* storage, size_t storage_size)
  154. {
  155. dbgln_if(KMALLOC_DEBUG, "Adding kmalloc subheap @ {} with size {}", storage, storage_size);
  156. static_assert(sizeof(KmallocSubheap) <= PAGE_SIZE);
  157. auto* subheap = new (storage) KmallocSubheap(storage + PAGE_SIZE, storage_size - PAGE_SIZE);
  158. subheaps.append(*subheap);
  159. }
  160. void* allocate(size_t size)
  161. {
  162. VERIFY(!expansion_in_progress);
  163. for (auto& slabheap : slabheaps) {
  164. if (size <= slabheap.slab_size())
  165. return slabheap.allocate();
  166. }
  167. for (auto& subheap : subheaps) {
  168. if (auto* ptr = subheap.allocator.allocate(size))
  169. return ptr;
  170. }
  171. if (!try_expand(size)) {
  172. PANIC("OOM when trying to expand kmalloc heap.");
  173. }
  174. return allocate(size);
  175. }
  176. void deallocate(void* ptr, size_t size)
  177. {
  178. VERIFY(!expansion_in_progress);
  179. VERIFY(is_valid_kmalloc_address(VirtualAddress { ptr }));
  180. for (auto& slabheap : slabheaps) {
  181. if (size <= slabheap.slab_size())
  182. return slabheap.deallocate(ptr);
  183. }
  184. for (auto& subheap : subheaps) {
  185. if (subheap.allocator.contains(ptr)) {
  186. subheap.allocator.deallocate(ptr);
  187. return;
  188. }
  189. }
  190. PANIC("Bogus pointer passed to kfree_sized({:p}, {})", ptr, size);
  191. }
  192. size_t allocated_bytes() const
  193. {
  194. size_t total = 0;
  195. for (auto const& subheap : subheaps)
  196. total += subheap.allocator.allocated_bytes();
  197. for (auto const& slabheap : slabheaps)
  198. total += slabheap.allocated_bytes();
  199. return total;
  200. }
  201. size_t free_bytes() const
  202. {
  203. size_t total = 0;
  204. for (auto const& subheap : subheaps)
  205. total += subheap.allocator.free_bytes();
  206. for (auto const& slabheap : slabheaps)
  207. total += slabheap.free_bytes();
  208. return total;
  209. }
  210. bool try_expand(size_t allocation_request)
  211. {
  212. VERIFY(!expansion_in_progress);
  213. TemporaryChange change(expansion_in_progress, true);
  214. auto new_subheap_base = expansion_data->next_virtual_address;
  215. Checked<size_t> padded_allocation_request = allocation_request;
  216. padded_allocation_request *= 2;
  217. padded_allocation_request += PAGE_SIZE;
  218. if (padded_allocation_request.has_overflow()) {
  219. PANIC("Integer overflow during kmalloc heap expansion");
  220. }
  221. auto rounded_allocation_request = Memory::page_round_up(padded_allocation_request.value());
  222. if (rounded_allocation_request.is_error()) {
  223. PANIC("Integer overflow computing pages for kmalloc heap expansion");
  224. }
  225. size_t new_subheap_size = max(minimum_subheap_size, rounded_allocation_request.value());
  226. dbgln_if(KMALLOC_DEBUG, "Unable to allocate {}, expanding kmalloc heap", allocation_request);
  227. if (!expansion_data->virtual_range.contains(new_subheap_base, new_subheap_size)) {
  228. // FIXME: Dare to return false and allow kmalloc() to fail!
  229. PANIC("Out of address space when expanding kmalloc heap.");
  230. }
  231. auto physical_pages_or_error = MM.commit_user_physical_pages(new_subheap_size / PAGE_SIZE);
  232. if (physical_pages_or_error.is_error()) {
  233. // FIXME: Dare to return false!
  234. PANIC("Out of physical pages when expanding kmalloc heap.");
  235. }
  236. auto physical_pages = physical_pages_or_error.release_value();
  237. expansion_data->next_virtual_address = expansion_data->next_virtual_address.offset(new_subheap_size);
  238. auto cpu_supports_nx = Processor::current().has_feature(CPUFeature::NX);
  239. SpinlockLocker mm_locker(Memory::s_mm_lock);
  240. SpinlockLocker pd_locker(MM.kernel_page_directory().get_lock());
  241. for (auto vaddr = new_subheap_base; !physical_pages.is_empty(); vaddr = vaddr.offset(PAGE_SIZE)) {
  242. // FIXME: We currently leak physical memory when mapping it into the kmalloc heap.
  243. auto& page = physical_pages.take_one().leak_ref();
  244. auto* pte = MM.pte(MM.kernel_page_directory(), vaddr);
  245. VERIFY(pte);
  246. pte->set_physical_page_base(page.paddr().get());
  247. pte->set_global(true);
  248. pte->set_user_allowed(false);
  249. pte->set_writable(true);
  250. if (cpu_supports_nx)
  251. pte->set_execute_disabled(true);
  252. pte->set_present(true);
  253. }
  254. add_subheap(new_subheap_base.as_ptr(), new_subheap_size);
  255. return true;
  256. }
  257. void enable_expansion()
  258. {
  259. // FIXME: This range can be much bigger on 64-bit, but we need to figure something out for 32-bit.
  260. auto virtual_range = MM.kernel_page_directory().range_allocator().try_allocate_anywhere(64 * MiB, 1 * MiB);
  261. expansion_data = KmallocGlobalData::ExpansionData {
  262. .virtual_range = virtual_range.value(),
  263. .next_virtual_address = virtual_range.value().base(),
  264. };
  265. // Make sure the entire kmalloc VM range is backed by page tables.
  266. // This avoids having to deal with lazy page table allocation during heap expansion.
  267. SpinlockLocker mm_locker(Memory::s_mm_lock);
  268. SpinlockLocker pd_locker(MM.kernel_page_directory().get_lock());
  269. for (auto vaddr = virtual_range.value().base(); vaddr < virtual_range.value().end(); vaddr = vaddr.offset(PAGE_SIZE)) {
  270. MM.ensure_pte(MM.kernel_page_directory(), vaddr);
  271. }
  272. }
  273. struct ExpansionData {
  274. Memory::VirtualRange virtual_range;
  275. VirtualAddress next_virtual_address;
  276. };
  277. Optional<ExpansionData> expansion_data;
  278. bool is_valid_kmalloc_address(VirtualAddress vaddr) const
  279. {
  280. if (vaddr.as_ptr() >= initial_kmalloc_memory && vaddr.as_ptr() < (initial_kmalloc_memory + INITIAL_KMALLOC_MEMORY_SIZE))
  281. return true;
  282. if (!expansion_data.has_value())
  283. return false;
  284. return expansion_data->virtual_range.contains(vaddr);
  285. }
  286. KmallocSubheap::List subheaps;
  287. KmallocSlabheap slabheaps[6] = { 16, 32, 64, 128, 256, 512 };
  288. bool expansion_in_progress { false };
  289. };
  290. READONLY_AFTER_INIT static KmallocGlobalData* g_kmalloc_global;
  291. alignas(KmallocGlobalData) static u8 g_kmalloc_global_heap[sizeof(KmallocGlobalData)];
  292. static size_t g_kmalloc_call_count;
  293. static size_t g_kfree_call_count;
  294. static size_t g_nested_kfree_calls;
  295. bool g_dump_kmalloc_stacks;
  296. void kmalloc_enable_expand()
  297. {
  298. g_kmalloc_global->enable_expansion();
  299. }
  300. static inline void kmalloc_verify_nospinlock_held()
  301. {
  302. // Catch bad callers allocating under spinlock.
  303. if constexpr (KMALLOC_VERIFY_NO_SPINLOCK_HELD) {
  304. VERIFY(!Processor::in_critical());
  305. }
  306. }
  307. UNMAP_AFTER_INIT void kmalloc_init()
  308. {
  309. // Zero out heap since it's placed after end_of_kernel_bss.
  310. memset(initial_kmalloc_memory, 0, sizeof(initial_kmalloc_memory));
  311. g_kmalloc_global = new (g_kmalloc_global_heap) KmallocGlobalData(initial_kmalloc_memory, sizeof(initial_kmalloc_memory));
  312. s_lock.initialize();
  313. }
  314. void* kmalloc(size_t size)
  315. {
  316. kmalloc_verify_nospinlock_held();
  317. SpinlockLocker lock(s_lock);
  318. ++g_kmalloc_call_count;
  319. if (g_dump_kmalloc_stacks && Kernel::g_kernel_symbols_available) {
  320. dbgln("kmalloc({})", size);
  321. Kernel::dump_backtrace();
  322. }
  323. void* ptr = g_kmalloc_global->allocate(size);
  324. Thread* current_thread = Thread::current();
  325. if (!current_thread)
  326. current_thread = Processor::idle_thread();
  327. if (current_thread) {
  328. // FIXME: By the time we check this, we have already allocated above.
  329. // This means that in the case of an infinite recursion, we can't catch it this way.
  330. VERIFY(current_thread->is_allocation_enabled());
  331. PerformanceManager::add_kmalloc_perf_event(*current_thread, size, (FlatPtr)ptr);
  332. }
  333. return ptr;
  334. }
  335. void kfree_sized(void* ptr, size_t size)
  336. {
  337. if (!ptr)
  338. return;
  339. VERIFY(size > 0);
  340. kmalloc_verify_nospinlock_held();
  341. SpinlockLocker lock(s_lock);
  342. ++g_kfree_call_count;
  343. ++g_nested_kfree_calls;
  344. if (g_nested_kfree_calls == 1) {
  345. Thread* current_thread = Thread::current();
  346. if (!current_thread)
  347. current_thread = Processor::idle_thread();
  348. if (current_thread) {
  349. VERIFY(current_thread->is_allocation_enabled());
  350. PerformanceManager::add_kfree_perf_event(*current_thread, 0, (FlatPtr)ptr);
  351. }
  352. }
  353. g_kmalloc_global->deallocate(ptr, size);
  354. --g_nested_kfree_calls;
  355. }
  356. size_t kmalloc_good_size(size_t size)
  357. {
  358. VERIFY(size > 0);
  359. // NOTE: There's no need to take the kmalloc lock, as the kmalloc slab-heaps (and their sizes) are constant
  360. for (auto const& slabheap : g_kmalloc_global->slabheaps) {
  361. if (size <= slabheap.slab_size())
  362. return slabheap.slab_size();
  363. }
  364. return round_up_to_power_of_two(size + Heap<CHUNK_SIZE>::AllocationHeaderSize, CHUNK_SIZE) - Heap<CHUNK_SIZE>::AllocationHeaderSize;
  365. }
  366. void* kmalloc_aligned(size_t size, size_t alignment)
  367. {
  368. Checked<size_t> real_allocation_size = size;
  369. real_allocation_size += alignment;
  370. real_allocation_size += sizeof(ptrdiff_t) + sizeof(size_t);
  371. void* ptr = kmalloc(real_allocation_size.value());
  372. if (ptr == nullptr)
  373. return nullptr;
  374. size_t max_addr = (size_t)ptr + alignment;
  375. void* aligned_ptr = (void*)(max_addr - (max_addr % alignment));
  376. ((ptrdiff_t*)aligned_ptr)[-1] = (ptrdiff_t)((u8*)aligned_ptr - (u8*)ptr);
  377. ((size_t*)aligned_ptr)[-2] = real_allocation_size.value();
  378. return aligned_ptr;
  379. }
  380. void* operator new(size_t size)
  381. {
  382. void* ptr = kmalloc(size);
  383. VERIFY(ptr);
  384. return ptr;
  385. }
  386. void* operator new(size_t size, const std::nothrow_t&) noexcept
  387. {
  388. return kmalloc(size);
  389. }
  390. void* operator new(size_t size, std::align_val_t al)
  391. {
  392. void* ptr = kmalloc_aligned(size, (size_t)al);
  393. VERIFY(ptr);
  394. return ptr;
  395. }
  396. void* operator new(size_t size, std::align_val_t al, const std::nothrow_t&) noexcept
  397. {
  398. return kmalloc_aligned(size, (size_t)al);
  399. }
  400. void* operator new[](size_t size)
  401. {
  402. void* ptr = kmalloc(size);
  403. VERIFY(ptr);
  404. return ptr;
  405. }
  406. void* operator new[](size_t size, const std::nothrow_t&) noexcept
  407. {
  408. return kmalloc(size);
  409. }
  410. void operator delete(void*) noexcept
  411. {
  412. // All deletes in kernel code should have a known size.
  413. VERIFY_NOT_REACHED();
  414. }
  415. void operator delete(void* ptr, size_t size) noexcept
  416. {
  417. return kfree_sized(ptr, size);
  418. }
  419. void operator delete(void* ptr, size_t, std::align_val_t) noexcept
  420. {
  421. return kfree_aligned(ptr);
  422. }
  423. void operator delete[](void*) noexcept
  424. {
  425. // All deletes in kernel code should have a known size.
  426. VERIFY_NOT_REACHED();
  427. }
  428. void operator delete[](void* ptr, size_t size) noexcept
  429. {
  430. return kfree_sized(ptr, size);
  431. }
  432. void get_kmalloc_stats(kmalloc_stats& stats)
  433. {
  434. SpinlockLocker lock(s_lock);
  435. stats.bytes_allocated = g_kmalloc_global->allocated_bytes();
  436. stats.bytes_free = g_kmalloc_global->free_bytes();
  437. stats.kmalloc_call_count = g_kmalloc_call_count;
  438. stats.kfree_call_count = g_kfree_call_count;
  439. }