Skip to content

kernel: remove CONFIG_APPLICATION_MEMORY in favor of app shmem subsystem #12990

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Feb 8, 2019

Conversation

andrewboie
Copy link
Contributor

This large PR completely removes CONFIG_APPLICATION_MEMORY from the kernel. It was implemented as a stopgap when userspace was introduced to get test cases up and running, but was never suitable for real-world use-cases where multiple logical applications need to run in isolation.

All of our test cases now use CONFIG_APP_SHARED_MEM, which is an end-to-end solution that ties together creating memory domain partitions for global data in properly aligned linker sections.

1.14 is the first release where userspace is no longer marked experimental and the APIs related to it are now fixed.

This PR supersedes, and includes some patches from #12608

More detail on the changes:

  • A problem with automatic cleanup of static kernel objects has been fixed; we now only automatically cleanup dynamic kernel objects.

  • A bug on x86 where app shared memory was not being copied with CONFIG_XIP has been fixed

  • App shared memory did not compute the bounds of the partition sections correctly for non power of two architectures; we now unconditionally use the gen_app_partitions.py script for all arches to properly do this.

  • Some new macros have been introduced so that memory pools can be placed within app shared memory partitions.

  • Much runtime logic for setting up partitions has been moved to build-time.

  • Some fixes to tests which were found when CONFIG_APPLICATION_MEMORY is turned off, but userspace still enabled.

  • gen_app_partitions.py can now be configured to include globals any number from third-party libraries

  • C library globals and malloc arenas are now exposed as k_mem_partitions

  • ztest now automatically instantiates and adds the main thread to a memory domain, containing ztest and C library globals.

  • all test cases that depended on CONFIG_APPLICATION_MEMORY to work now use app shared memory

@codecov-io
Copy link

codecov-io commented Feb 1, 2019

Codecov Report

Merging #12990 into master will increase coverage by 0.05%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #12990      +/-   ##
==========================================
+ Coverage   48.73%   48.78%   +0.05%     
==========================================
  Files         315      315              
  Lines       46540    46546       +6     
  Branches    10743    10748       +5     
==========================================
+ Hits        22679    22708      +29     
+ Misses      19349    19325      -24     
- Partials     4512     4513       +1
Impacted Files Coverage Δ
lib/os/fdtable.c 42.22% <ø> (-0.48%) ⬇️
kernel/init.c 73.43% <ø> (ø) ⬆️
include/kernel.h 94.44% <ø> (ø) ⬆️
drivers/clock_control/nrf_power_clock.c 50% <0%> (-2.11%) ⬇️
kernel/sched.c 90.12% <0%> (+0.31%) ⬆️
boards/posix/nrf52_bsim/time_machine.c 52.45% <0%> (+4.91%) ⬆️
boards/posix/nrf52_bsim/irq_handler.c 78.3% <0%> (+13.2%) ⬆️
boards/posix/nrf52_bsim/argparse.c 46.98% <0%> (+13.25%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d9da0b0...ddd484a. Read the comment docs.

@andrewboie andrewboie force-pushed the app-shmem-refactoring branch from ae82ce8 to 7307573 Compare February 1, 2019 23:22
@andrewboie
Copy link
Contributor Author

recheck

Copy link
Contributor

@dbkinder dbkinder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one doc typo...

@andrewboie andrewboie force-pushed the app-shmem-refactoring branch from 44906b1 to 6bc43cc Compare February 5, 2019 05:46
@ruuddw ruuddw dismissed their stale review February 7, 2019 10:00

We'll fix later, changes not related to this PR.

Copy link
Member

@ruuddw ruuddw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This introduces some new 'overlapping MPU region' exceptions on ARC, but these can be handled separately. Overall PR is sound.

Copy link
Member

@ioannisg ioannisg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am going to look at the patch ASAP. For now I can confirm that the memory protection tests suite (tests/kernel/mem_protect) executes successfully on ARMv8-M architecture (nrf9160_pca10090 board).

Copy link
Member

@ioannisg ioannisg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, looking good.
The arch/arm changes were minimal - just removing the CONFIG_APPLicATION_MEMORY blocks

Left some comments and questions, pls, address :)

I am going to run the remaining tests that have been changed in this PR on ARMv8-M and report back.

@andrewboie andrewboie force-pushed the app-shmem-refactoring branch from 8da7a8e to 62169f6 Compare February 7, 2019 20:10
CMakeLists.txt Outdated
set(ALIGN_SIZING_DEP app_sizing_prebuilt linker_app_sizing_script)
endif()
endif()
if(CONFIG_CPU_HAS_MPU AND CONFIG_USERSPACE)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it should be CONFIG_MEMORY_PROTECTION instead of CONFIG_CPU_HAS_MPU

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need the configuration option, not the hw capabilities

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or, could you simply rewrite this as it looks it is only for ARM? In arm USERSPACE implies CPU_HAS_MPU...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated.

CMakeLists.txt Outdated

if(CONFIG_ARM)
if(CONFIG_CPU_HAS_MPU AND CONFIG_USERSPACE AND CONFIG_ARM)
Copy link
Member

@ioannisg ioannisg Feb 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, USERSPACE implies CPU_HAS_MPU in arm...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated.

Copy link
Member

@ioannisg ioannisg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a simple cleanup left to do in cmakelists, from my side.
Pls address and we are good to go.

Andrew Boie and others added 15 commits February 7, 2019 21:21
Dynamic kernel objects enforce that the permission state
of an object is also a reference count; using a kernel
object without permission regardless of caller privilege
level is a programming bug.

However, this is not the case for static objects. In
particular, supervisor threads are allowed to use any
object they like without worrying about permissions, and
the logic here was causing cleanup functions to be called
over and over again on kernel objects that were actually
in use.

The automatic cleanup mechanism was intended for
dynamic objects anyway, so just skip it entirely for
static objects.

Signed-off-by: Andrew Boie <[email protected]>
We want CONFIG_APPLICATION_MEMORY specifically disabled
for this test, but it was being transitively selected by
CONFIG_TEST_USERSPACE which defaults to on for CONFIG_TEST.

Turn it off so that disabling application memory in the
config actually has an effect.

Signed-off-by: Andrew Boie <[email protected]>
This is a separate data section which needs to be copied into
RAM.

Most arches just use the kernel's _data_copy(), but x86 has its
own optimized copying code.

Signed-off-by: Adithya Baglody <[email protected]>
Signed-off-by: Andrew Boie <[email protected]>
This is unnecessary.

Signed-off-by: Andrew Boie <[email protected]>
CONFIG_APPLICATION_MEMORY is being removed from
Zephyr.

Signed-off-by: Andrew Boie <[email protected]>
This patch will run the python scripts for all architectures
to determine the partitions available. This is needed for correct
operation of the app_shared_mem feature.

Signed-off-by: Adithya Baglody <[email protected]>
Signed-off-by: Andrew Boie <[email protected]>
This particular rule of cmake was causing problems when run in
multi-threaded environment. This change in the dependency will
resolve all such issues and ensures that this script is executed
correctly.

Signed-off-by: Adithya Baglody <[email protected]>
* K_APP_DMEM_SECTION/K_MEM_BMEM_SECTION macros now exist
  to specifically define the name of the sections for data
  and bss respectively.

* All boards now use the gen_app_partitions.py script, the
  padding hacks for non-power-of-two arches didn't work right
  in all cases. Linker scripts have been updated.

* The defined k_mem_partition is now completely initialized
  at build time. The region data structures now only exist
  to zero BSS.

Based on some work submitted by Adithya Baglody
<[email protected]>

Signed-off-by: Andrew Boie <[email protected]>
Some tests instantiate a lot of thread objects. These
were not tagged with __kernel, and
CONFIG_APPLICATION_MEMORY was enabled, so the kernel was
not adding them to the kernel object database.

However, with CONFIG_APPLICATION_MEMORY disabled, this
overflowed the default max number of thread objects (16).
Increase the max to 32 for these particular tests.

Signed-off-by: Andrew Boie <[email protected]>
If CONFIG_APP_SHARED_MEM is enabled, ztest will set up a
default memory domain with a single partition on startup.
Test case globals may be added to the partition via
ZTEST_BMEM/ZTEST_DMEM macros, add their own partitions,
or leave the domain and join another one.

Signed-off-by: Andrew Boie <[email protected]>
Zephyr may be linked against third-party libraries which
were not part of the build. These may contain globals
which need to end up in a memory partition.

We can now specify the names of these libraries, as well
as a destination partition for their globals.

Some excessively long variables were renamed.

Signed-off-by: Andrew Boie <[email protected]>
This diverges from policy for all of our other arches
and C libraries. Global access to the malloc arena
may not be desirable.

Forthcoming patch will expose, for all C libraries, a
k_mem_partition with the malloc arena which can be
added to domains as desired.

Signed-off-by: Andrew Boie <[email protected]>
* Newlib now defines a special z_newlib_partition containing
  all globals relevant to newlib. Most of these are in libc.a
  with a heap tracking variable in newlib's hooks.

* Both C libraries now expose a k_mem_partition containing the
  bounds of the malloc heap arena. Threads that want to use
  libc malloc() will need to add this to their memory domain.

* z_newlib_get_heap_bounds has been removed, in favor of the
  memory partition for the heap arena

* ztest now includes the C library partitions in its memory
  domain.

* The mem_alloc test now runs in user mode to prove that this
  all works for both C libraries.

Signed-off-by: Andrew Boie <[email protected]>
CONFIG_APPLICATION_MEMORY was a stopgap feature that is
being removed from the kernel. Convert tests and samples
to use the application shared memory feature instead,
in most cases using the domain set up by ztest.

Signed-off-by: Andrew Boie <[email protected]>
This was never a long-term solution, more of a gross hack
to get test cases working until we could figure out a good
end-to-end solution for memory domains that generated
appropriate linker sections. Now that we have this with
the app shared memory feature, and have converted all tests
to remove it, delete this feature.

To date all userspace APIs have been tagged as 'experimental'
which sidesteps deprecation policies.

Signed-off-by: Andrew Boie <[email protected]>
@andrewboie andrewboie force-pushed the app-shmem-refactoring branch from 62169f6 to ddd484a Compare February 8, 2019 05:22
@nashif nashif merged commit 41f6011 into zephyrproject-rtos:master Feb 8, 2019
@SebastianBoe
Copy link
Collaborator

Wow, it is great to see deletion of so much code that has caused headaches and blocked progress the last year.

Fantastic.

@andrewboie andrewboie deleted the app-shmem-refactoring branch April 10, 2019 14:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.