1.. _memory_management_api_demand_paging:
2
3Demand Paging
4#############
5
6Demand paging provides a mechanism where data is only brought into physical
7memory as required by current execution context. The physical memory is
8conceptually divided in page-sized page frames as regions to hold data.
9
10* When the processor tries to access data and the data page exists in
11  one of the page frames, the execution continues without any interruptions.
12
13* When the processor tries to access the data page that does not exist
14  in any page frames, a page fault occurs. The paging code then brings in
15  the corresponding data page from backing store into physical memory if
16  there is a free page frame. If there is no more free page frames,
17  the eviction algorithm is invoked to select a data page to be paged out,
18  thus freeing up a page frame for new data to be paged in. If this data
19  page has been modified after it is first paged in, the data will be
20  written back into the backing store. If no modifications is done or
21  after written back into backing store, the data page is now considered
22  paged out and the corresponding page frame is now free. The paging code
23  then invokes the backing store to page in the data page corresponding to
24  the location of the requested data. The backing store copies that data
25  page into the free page frame. Now the data page is in physical memory
26  and execution can continue.
27
28There are functions where paging in and out can be invoked manually
29using :c:func:`k_mem_page_in()` and :c:func:`k_mem_page_out()`.
30:c:func:`k_mem_page_in()` can be used to page in data pages
31in anticipation that they are required in the near future. This is used to
32minimize number of page faults as these data pages are already in physical
33memory, and thus minimizing latency. :c:func:`k_mem_page_out()` can be
34used to page out data pages where they are not going to be accessed for
35a considerable amount of time. This frees up page frames so that the next
36page in can be executed faster as the paging code does not need to invoke
37the eviction algorithm.
38
39Terminology
40***********
41
42Data Page
43  A data page is a page-sized region of data. It may exist in a page frame,
44  or be paged out to some backing store. Its location can always be looked
45  up in the CPU's page tables (or equivalent) by virtual address.
46  The data type will always be ``void *`` or in some cases ``uint8_t *``
47  when doing pointer arithmetic.
48
49Page Frame
50  A page frame is a page-sized physical memory region in RAM. It is a
51  container where a data page may be placed. It is always referred to by
52  physical address. Zephyr has a convention of using ``uintptr_t`` for physical
53  addresses. For every page frame, a ``struct k_mem_page_frame`` is instantiated to
54  store metadata. Flags for each page frame:
55
56  * ``K_MEM_PAGE_FRAME_FREE`` indicates a page frame is unused and on the list of
57    free page frames. When this flag is set, none of the other flags are
58    meaningful and they must not be modified.
59
60  * ``K_MEM_PAGE_FRAME_PINNED`` indicates a page frame is pinned in memory
61    and should never be paged out.
62
63  * ``K_MEM_PAGE_FRAME_RESERVED`` indicates a physical page reserved by hardware
64    and should not be used at all.
65
66  * ``K_MEM_PAGE_FRAME_MAPPED`` is set when a physical page is mapped to
67    virtual memory address.
68
69  * ``K_MEM_PAGE_FRAME_BUSY`` indicates a page frame is currently involved in
70    a page-in/out operation.
71
72  * ``K_MEM_PAGE_FRAME_BACKED`` indicates a page frame has a clean copy
73    in the backing store.
74
75K_MEM_SCRATCH_PAGE
76  The virtual address of a special page provided to the backing store to:
77  * Copy a data page from ``k_MEM_SCRATCH_PAGE`` to the specified location; or,
78  * Copy a data page from the provided location to ``K_MEM_SCRATCH_PAGE``.
79  This is used as an intermediate page for page in/out operations. This
80  scratch needs to be mapped read/write for backing store code to access.
81  However the data page itself may only be mapped as read-only in virtual
82  address space. If this page is provided as-is to backing store,
83  the data page must be re-mapped as read/write which has security
84  implications as the data page is no longer read-only to other parts of
85  the application.
86
87Paging Statistics
88*****************
89
90Paging statistics can be obtained via various function calls when
91:kconfig:option:`CONFIG_DEMAND_PAGING_TIMING_HISTOGRAM_NUM_BINS` is enabled:
92
93* Overall statistics via :c:func:`k_mem_paging_stats_get()`
94
95* Per-thread statistics via :c:func:`k_mem_paging_thread_stats_get()`
96  if :kconfig:option:`CONFIG_DEMAND_PAGING_THREAD_STATS` is enabled
97
98* Execution time histogram can be obtained when
99  :kconfig:option:`CONFIG_DEMAND_PAGING_TIMING_HISTOGRAM` is enabled, and
100  :kconfig:option:`CONFIG_DEMAND_PAGING_TIMING_HISTOGRAM_NUM_BINS` is defined.
101  Note that the timing is highly dependent on the architecture,
102  SoC or board. It is highly recommended that
103  ``k_mem_paging_eviction_histogram_bounds[]`` and
104  ``k_mem_paging_backing_store_histogram_bounds[]``
105  be defined for a particular application.
106
107  * Execution time histogram of eviction algorithm via
108    :c:func:`k_mem_paging_histogram_eviction_get()`
109
110  * Execution time histogram of backing store doing page-in via
111    :c:func:`k_mem_paging_histogram_backing_store_page_in_get()`
112
113  * Execution time histogram of backing store doing page-out via
114    :c:func:`k_mem_paging_histogram_backing_store_page_out_get()`
115
116Eviction Algorithm
117******************
118
119The eviction algorithm is used to determine which data page and its
120corresponding page frame can be paged out to free up a page frame
121for the next page in operation. There are four functions which are
122called from the kernel paging code:
123
124* :c:func:`k_mem_paging_eviction_init()` is called to initialize
125  the eviction algorithm. This is called at ``POST_KERNEL``.
126
127* :c:func:`k_mem_paging_eviction_add()` is called each time a data page becomes
128  eligible for future eviction.
129
130* :c:func:`k_mem_paging_eviction_remove()` is called when a data page is no
131  longer eligible for eviction. This may happen if the given data page becomes
132  pinned, gets unmapped or is about to be evicted.
133
134* :c:func:`k_mem_paging_eviction_select()` is called to select
135  a data page to evict. A function argument ``dirty`` is written to
136  signal the caller whether the selected data page has been modified
137  since it is first paged in. If the ``dirty`` bit is returned
138  as set, the paging code signals to the backing store to write
139  the data page back into storage (thus updating its content).
140  The function returns a pointer to the page frame corresponding to
141  the selected data page.
142
143There is one additional function which is called by the architecture's memory
144management code to flag data pages when they trigger an access fault:
145:c:func:`k_mem_paging_eviction_accessed()`. This is used by the LRU algorithm
146to requeue "used" pages.
147
148Two eviction algorithms are currently available:
149
150* An NRU (Not-Recently-Used) eviction algorithm has been implemented as a
151  sample. This is a very simple algorithm which ranks data pages on whether
152  they have been accessed and modified. The selection is based on this ranking.
153
154* An LRU (Least-Recently-Used) eviction algorithm is also available. It is
155  based on a sorted queue of data pages. The LRU code is more complex compared
156  to the NRU code but also considerably more efficient. This is recommended for
157  production use.
158
159To implement a new eviction algorithm, :c:func:`k_mem_paging_eviction_init()`
160and :c:func:`k_mem_paging_eviction_select()` must be implemented.
161If :kconfig:option:`CONFIG_EVICTION_TRACKING` is enabled for an algorithm,
162these additional functions must also be implemented,
163:c:func:`k_mem_paging_eviction_add()`, :c:func:`k_mem_paging_eviction_remove()`,
164:c:func:`k_mem_paging_eviction_accessed()`.
165
166Backing Store
167*************
168
169Backing store is responsible for paging in/out data page between
170their corresponding page frames and storage. These are the functions
171which must be implemented:
172
173* :c:func:`k_mem_paging_backing_store_init()` is called to
174  initialized the backing store at ``POST_KERNEL``.
175
176* :c:func:`k_mem_paging_backing_store_location_get()` is called to
177  reserve a backing store location so a data page can be paged out.
178  This ``location`` token is passed to
179  :c:func:`k_mem_paging_backing_store_page_out()` to perform actual
180  page out operation.
181
182* :c:func:`k_mem_paging_backing_store_location_free()` is called to
183  free a backing store location (the ``location`` token) which can
184  then be used for subsequent page out operation.
185
186* :c:func:`k_mem_paging_backing_store_location_query()` is called to obtain
187  the ``location`` token corresponding to storage content to be virtually
188  mapped and paged-in on demand. Most useful with
189  :kconfig:option:`CONFIG_DEMAND_MAPPING`.
190
191* :c:func:`k_mem_paging_backing_store_page_in()` copies a data page
192  from the backing store location associated with the provided
193  ``location`` token to the page pointed by ``K_MEM_SCRATCH_PAGE``.
194
195* :c:func:`k_mem_paging_backing_store_page_out()` copies a data page
196  from ``K_MEM_SCRATCH_PAGE`` to the backing store location associated
197  with the provided ``location`` token.
198
199* :c:func:`k_mem_paging_backing_store_page_finalize()` is invoked after
200  :c:func:`k_mem_paging_backing_store_page_in()` so that the page frame
201  struct may be updated for internal accounting. This can be
202  a no-op.
203
204To implement a new backing store, the functions mentioned above
205must be implemented.
206:c:func:`k_mem_paging_backing_store_page_finalize()` can be an empty
207function if so desired.
208
209API Reference
210*************
211
212.. doxygengroup:: mem-demand-paging
213
214Eviction Algorithm APIs
215=======================
216
217.. doxygengroup:: mem-demand-paging-eviction
218
219Backing Store APIs
220==================
221
222.. doxygengroup:: mem-demand-paging-backing-store
223