From 9c6de44f980262fad8bd65c7c3a3ff1765f1a2d0 Mon Sep 17 00:00:00 2001 From: Alexander Boettcher Date: Thu, 9 Nov 2017 14:20:07 +0100 Subject: [PATCH] core: use core mem allocator in Pd_session Fixes #2563 --- .../src/core/include/constrained_core_ram.h | 87 +++++++++++++++++++ repos/base/src/core/include/core_env.h | 3 +- repos/base/src/core/include/pd_root.h | 10 ++- .../src/core/include/pd_session_component.h | 9 +- .../src/core/include/ram_dataspace_factory.h | 11 +-- repos/base/src/core/main.cc | 4 +- 6 files changed, 109 insertions(+), 15 deletions(-) create mode 100644 repos/base/src/core/include/constrained_core_ram.h diff --git a/repos/base/src/core/include/constrained_core_ram.h b/repos/base/src/core/include/constrained_core_ram.h new file mode 100644 index 0000000000..0599586f02 --- /dev/null +++ b/repos/base/src/core/include/constrained_core_ram.h @@ -0,0 +1,87 @@ +/* + * \brief Quota-bounds-checking implementation of the 'Ram_allocator' + * interface specifically for core + * \author Norman Feske + * \date 2017-05-02 + */ + +/* + * Copyright (C) 2017 Genode Labs GmbH + * + * This file is part of the Genode OS framework, which is distributed + * under the terms of the GNU Affero General Public License version 3. + */ + +#ifndef _CORE__INCLUDE__CORE_CONSTRAINED_CORE_RAM_H_ +#define _CORE__INCLUDE__CORE_CONSTRAINED_CORE_RAM_H_ + +#include + +namespace Genode { class Constrained_core_ram; } + +class Genode::Constrained_core_ram : public Allocator +{ + private: + + Ram_quota_guard &_ram_guard; + Cap_quota_guard &_cap_guard; + Range_allocator &_core_mem; + + uint64_t core_mem_allocated { 0 }; + + public: + + Constrained_core_ram(Ram_quota_guard &ram_guard, + Cap_quota_guard &cap_guard, + Range_allocator &core_mem) + : + _ram_guard(ram_guard), _cap_guard(cap_guard), _core_mem(core_mem) + { } + + ~Constrained_core_ram() + { + if (!core_mem_allocated) + return; + + error(this, " memory leaking of size ", core_mem_allocated, + " in core !"); + } + + bool alloc(size_t const size, void **ptr) override + { + size_t const page_aligned_size = align_addr(size, 12); + + Ram_quota_guard::Reservation ram (_ram_guard, + Ram_quota{page_aligned_size}); + /* on some kernels we require a cap, on some not XXX */ + Cap_quota_guard::Reservation caps(_cap_guard, Cap_quota{1}); + + if (!_core_mem.alloc(page_aligned_size, ptr)) + return false; + + ram.acknowledge(); + caps.acknowledge(); + + core_mem_allocated += page_aligned_size; + + return true; + } + + void free(void *ptr, size_t const size) override + { + size_t const page_aligned_size = align_addr(size, 12); + + _core_mem.free(ptr, page_aligned_size); + + _ram_guard.replenish(Ram_quota{page_aligned_size}); + /* on some kernels we require a cap, on some not XXX */ + _cap_guard.replenish(Cap_quota{1}); + + core_mem_allocated -= page_aligned_size; + } + + size_t consumed() const override { return core_mem_allocated; } + size_t overhead(size_t size) const override { return 0; } + bool need_size_for_free() const override { return true; } +}; +#endif /* _CORE__INCLUDE__CORE_CONSTRAINED_CORE_RAM_H_ */ diff --git a/repos/base/src/core/include/core_env.h b/repos/base/src/core/include/core_env.h index 5e8776c973..40766d35f4 100644 --- a/repos/base/src/core/include/core_env.h +++ b/repos/base/src/core/include/core_env.h @@ -70,7 +70,8 @@ class Genode::Core_env : public Env_deprecated Ram_dataspace_factory::Virt_range { platform()->vm_start(), platform()->vm_size() }, _region_map, *((Pager_entrypoint *)nullptr), - "" /* args to native PD */) + "" /* args to native PD */, + *platform_specific()->core_mem_alloc()) { _pd_session.init_cap_and_ram_accounts(); } diff --git a/repos/base/src/core/include/pd_root.h b/repos/base/src/core/include/pd_root.h index 5fcd1e5e42..75c661ce73 100644 --- a/repos/base/src/core/include/pd_root.h +++ b/repos/base/src/core/include/pd_root.h @@ -33,6 +33,7 @@ class Genode::Pd_root : public Genode::Root_component(&ep, &md_alloc), - _ep(ep), _pager_ep(pager_ep), _phys_alloc(phys_alloc), _local_rm(local_rm) + _ep(ep), _pager_ep(pager_ep), _phys_alloc(phys_alloc), + _local_rm(local_rm), _core_mem(core_mem) { } }; diff --git a/repos/base/src/core/include/pd_session_component.h b/repos/base/src/core/include/pd_session_component.h index 48dea8c504..28c02b11f3 100644 --- a/repos/base/src/core/include/pd_session_component.h +++ b/repos/base/src/core/include/pd_session_component.h @@ -29,6 +29,7 @@ #include /* core includes */ +#include #include #include #include @@ -47,6 +48,7 @@ class Genode::Pd_session_component : public Session_object Rpc_entrypoint &_ep; Constrained_ram_allocator _constrained_md_ram_alloc; + Constrained_core_ram _constrained_core_ram_alloc; Sliced_heap _sliced_heap; Capability _parent; Signal_broker _signal_broker; @@ -122,14 +124,17 @@ class Genode::Pd_session_component : public Session_object Virt_range virt_range, Region_map &local_rm, Pager_entrypoint &pager_ep, - char const *args) + char const *args, + Range_allocator &core_mem) : Session_object(ep, resources, label, diag), _ep(ep), _constrained_md_ram_alloc(*this, *this, *this), + _constrained_core_ram_alloc(*this, *this, core_mem), _sliced_heap(_constrained_md_ram_alloc, local_rm), _signal_broker(_sliced_heap, ep, ep), - _ram_ds_factory(ep, phys_alloc, phys_range, local_rm, _sliced_heap), + _ram_ds_factory(ep, phys_alloc, phys_range, local_rm, + _constrained_core_ram_alloc), _rpc_cap_factory(_sliced_heap), _native_pd(*this, args), _address_space(ep, _sliced_heap, pager_ep, diff --git a/repos/base/src/core/include/ram_dataspace_factory.h b/repos/base/src/core/include/ram_dataspace_factory.h index b706e55919..cc93227a1a 100644 --- a/repos/base/src/core/include/ram_dataspace_factory.h +++ b/repos/base/src/core/include/ram_dataspace_factory.h @@ -37,12 +37,7 @@ class Genode::Ram_dataspace_factory : public Ram_allocator, struct Virt_range { addr_t start, size; }; - /* - * Dimension '_ds_slab' such that slab blocks (including the - * meta-data overhead of the sliced-heap blocks) are page sized. - */ - static constexpr size_t SLAB_BLOCK_SIZE = - get_page_size() - Sliced_heap::meta_data_size(); + static constexpr size_t SLAB_BLOCK_SIZE = 4096; private: @@ -92,10 +87,10 @@ class Genode::Ram_dataspace_factory : public Ram_allocator, Range_allocator &phys_alloc, Phys_range phys_range, Region_map &local_rm, - Sliced_heap &sliced_heap) + Allocator &allocator) : _ep(ep), _phys_alloc(phys_alloc), _phys_range(phys_range), - _ds_slab(sliced_heap, _initial_sb) + _ds_slab(allocator, _initial_sb) { } ~Ram_dataspace_factory() diff --git a/repos/base/src/core/main.cc b/repos/base/src/core/main.cc index 07c187b1ec..3c340d6d98 100644 --- a/repos/base/src/core/main.cc +++ b/repos/base/src/core/main.cc @@ -256,7 +256,9 @@ int main() static Rm_root rm_root (&ep, &sliced_heap, pager_ep); static Cpu_root cpu_root (&ep, &ep, &pager_ep, &sliced_heap, Trace::sources()); - static Pd_root pd_root (ep, pager_ep, *platform()->ram_alloc(), local_rm, sliced_heap); + static Pd_root pd_root (ep, pager_ep, *platform()->ram_alloc(), + local_rm, sliced_heap, + *platform_specific()->core_mem_alloc()); static Log_root log_root (&ep, &sliced_heap); static Io_mem_root io_mem_root (&ep, &ep, platform()->io_mem_alloc(), platform()->ram_alloc(), &sliced_heap);