arm64: dts: qcom: sm8550: add TRNG node
[linux-modified.git] / Documentation / mm / damon / design.rst
1 .. SPDX-License-Identifier: GPL-2.0
2
3 ======
4 Design
5 ======
6
7
8 Overall Architecture
9 ====================
10
11 DAMON subsystem is configured with three layers including
12
13 - Operations Set: Implements fundamental operations for DAMON that depends on
14   the given monitoring target address-space and available set of
15   software/hardware primitives,
16 - Core: Implements core logics including monitoring overhead/accurach control
17   and access-aware system operations on top of the operations set layer, and
18 - Modules: Implements kernel modules for various purposes that provides
19   interfaces for the user space, on top of the core layer.
20
21
22 Configurable Operations Set
23 ---------------------------
24
25 For data access monitoring and additional low level work, DAMON needs a set of
26 implementations for specific operations that are dependent on and optimized for
27 the given target address space.  On the other hand, the accuracy and overhead
28 tradeoff mechanism, which is the core logic of DAMON, is in the pure logic
29 space.  DAMON separates the two parts in different layers, namely DAMON
30 Operations Set and DAMON Core Logics Layers, respectively.  It further defines
31 the interface between the layers to allow various operations sets to be
32 configured with the core logic.
33
34 Due to this design, users can extend DAMON for any address space by configuring
35 the core logic to use the appropriate operations set.  If any appropriate set
36 is unavailable, users can implement one on their own.
37
38 For example, physical memory, virtual memory, swap space, those for specific
39 processes, NUMA nodes, files, and backing memory devices would be supportable.
40 Also, if some architectures or devices supporting special optimized access
41 check primitives, those will be easily configurable.
42
43
44 Programmable Modules
45 --------------------
46
47 Core layer of DAMON is implemented as a framework, and exposes its application
48 programming interface to all kernel space components such as subsystems and
49 modules.  For common use cases of DAMON, DAMON subsystem provides kernel
50 modules that built on top of the core layer using the API, which can be easily
51 used by the user space end users.
52
53
54 Operations Set Layer
55 ====================
56
57 The monitoring operations are defined in two parts:
58
59 1. Identification of the monitoring target address range for the address space.
60 2. Access check of specific address range in the target space.
61
62 DAMON currently provides the implementations of the operations for the physical
63 and virtual address spaces. Below two subsections describe how those work.
64
65
66 VMA-based Target Address Range Construction
67 -------------------------------------------
68
69 This is only for the virtual address space monitoring operations
70 implementation.  That for the physical address space simply asks users to
71 manually set the monitoring target address ranges.
72
73 Only small parts in the super-huge virtual address space of the processes are
74 mapped to the physical memory and accessed.  Thus, tracking the unmapped
75 address regions is just wasteful.  However, because DAMON can deal with some
76 level of noise using the adaptive regions adjustment mechanism, tracking every
77 mapping is not strictly required but could even incur a high overhead in some
78 cases.  That said, too huge unmapped areas inside the monitoring target should
79 be removed to not take the time for the adaptive mechanism.
80
81 For the reason, this implementation converts the complex mappings to three
82 distinct regions that cover every mapped area of the address space.  The two
83 gaps between the three regions are the two biggest unmapped areas in the given
84 address space.  The two biggest unmapped areas would be the gap between the
85 heap and the uppermost mmap()-ed region, and the gap between the lowermost
86 mmap()-ed region and the stack in most of the cases.  Because these gaps are
87 exceptionally huge in usual address spaces, excluding these will be sufficient
88 to make a reasonable trade-off.  Below shows this in detail::
89
90     <heap>
91     <BIG UNMAPPED REGION 1>
92     <uppermost mmap()-ed region>
93     (small mmap()-ed regions and munmap()-ed regions)
94     <lowermost mmap()-ed region>
95     <BIG UNMAPPED REGION 2>
96     <stack>
97
98
99 PTE Accessed-bit Based Access Check
100 -----------------------------------
101
102 Both of the implementations for physical and virtual address spaces use PTE
103 Accessed-bit for basic access checks.  Only one difference is the way of
104 finding the relevant PTE Accessed bit(s) from the address.  While the
105 implementation for the virtual address walks the page table for the target task
106 of the address, the implementation for the physical address walks every page
107 table having a mapping to the address.  In this way, the implementations find
108 and clear the bit(s) for next sampling target address and checks whether the
109 bit(s) set again after one sampling period.  This could disturb other kernel
110 subsystems using the Accessed bits, namely Idle page tracking and the reclaim
111 logic.  DAMON does nothing to avoid disturbing Idle page tracking, so handling
112 the interference is the responsibility of sysadmins.  However, it solves the
113 conflict with the reclaim logic using ``PG_idle`` and ``PG_young`` page flags,
114 as Idle page tracking does.
115
116
117 Core Logics
118 ===========
119
120
121 Monitoring
122 ----------
123
124 Below four sections describe each of the DAMON core mechanisms and the five
125 monitoring attributes, ``sampling interval``, ``aggregation interval``,
126 ``update interval``, ``minimum number of regions``, and ``maximum number of
127 regions``.
128
129
130 Access Frequency Monitoring
131 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
132
133 The output of DAMON says what pages are how frequently accessed for a given
134 duration.  The resolution of the access frequency is controlled by setting
135 ``sampling interval`` and ``aggregation interval``.  In detail, DAMON checks
136 access to each page per ``sampling interval`` and aggregates the results.  In
137 other words, counts the number of the accesses to each page.  After each
138 ``aggregation interval`` passes, DAMON calls callback functions that previously
139 registered by users so that users can read the aggregated results and then
140 clears the results.  This can be described in below simple pseudo-code::
141
142     while monitoring_on:
143         for page in monitoring_target:
144             if accessed(page):
145                 nr_accesses[page] += 1
146         if time() % aggregation_interval == 0:
147             for callback in user_registered_callbacks:
148                 callback(monitoring_target, nr_accesses)
149             for page in monitoring_target:
150                 nr_accesses[page] = 0
151         sleep(sampling interval)
152
153 The monitoring overhead of this mechanism will arbitrarily increase as the
154 size of the target workload grows.
155
156
157 .. _damon_design_region_based_sampling:
158
159 Region Based Sampling
160 ~~~~~~~~~~~~~~~~~~~~~
161
162 To avoid the unbounded increase of the overhead, DAMON groups adjacent pages
163 that assumed to have the same access frequencies into a region.  As long as the
164 assumption (pages in a region have the same access frequencies) is kept, only
165 one page in the region is required to be checked.  Thus, for each ``sampling
166 interval``, DAMON randomly picks one page in each region, waits for one
167 ``sampling interval``, checks whether the page is accessed meanwhile, and
168 increases the access frequency counter of the region if so.  The counter is
169 called ``nr_regions`` of the region.  Therefore, the monitoring overhead is
170 controllable by setting the number of regions.  DAMON allows users to set the
171 minimum and the maximum number of regions for the trade-off.
172
173 This scheme, however, cannot preserve the quality of the output if the
174 assumption is not guaranteed.
175
176
177 Adaptive Regions Adjustment
178 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
179
180 Even somehow the initial monitoring target regions are well constructed to
181 fulfill the assumption (pages in same region have similar access frequencies),
182 the data access pattern can be dynamically changed.  This will result in low
183 monitoring quality.  To keep the assumption as much as possible, DAMON
184 adaptively merges and splits each region based on their access frequency.
185
186 For each ``aggregation interval``, it compares the access frequencies of
187 adjacent regions and merges those if the frequency difference is small.  Then,
188 after it reports and clears the aggregated access frequency of each region, it
189 splits each region into two or three regions if the total number of regions
190 will not exceed the user-specified maximum number of regions after the split.
191
192 In this way, DAMON provides its best-effort quality and minimal overhead while
193 keeping the bounds users set for their trade-off.
194
195
196 .. _damon_design_age_tracking:
197
198 Age Tracking
199 ~~~~~~~~~~~~
200
201 By analyzing the monitoring results, users can also find how long the current
202 access pattern of a region has maintained.  That could be used for good
203 understanding of the access pattern.  For example, page placement algorithm
204 utilizing both the frequency and the recency could be implemented using that.
205 To make such access pattern maintained period analysis easier, DAMON maintains
206 yet another counter called ``age`` in each region.  For each ``aggregation
207 interval``, DAMON checks if the region's size and access frequency
208 (``nr_accesses``) has significantly changed.  If so, the counter is reset to
209 zero.  Otherwise, the counter is increased.
210
211
212 Dynamic Target Space Updates Handling
213 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
214
215 The monitoring target address range could dynamically changed.  For example,
216 virtual memory could be dynamically mapped and unmapped.  Physical memory could
217 be hot-plugged.
218
219 As the changes could be quite frequent in some cases, DAMON allows the
220 monitoring operations to check dynamic changes including memory mapping changes
221 and applies it to monitoring operations-related data structures such as the
222 abstracted monitoring target memory area only for each of a user-specified time
223 interval (``update interval``).
224
225
226 .. _damon_design_damos:
227
228 Operation Schemes
229 -----------------
230
231 One common purpose of data access monitoring is access-aware system efficiency
232 optimizations.  For example,
233
234     paging out memory regions that are not accessed for more than two minutes
235
236 or
237
238     using THP for memory regions that are larger than 2 MiB and showing a high
239     access frequency for more than one minute.
240
241 One straightforward approach for such schemes would be profile-guided
242 optimizations.  That is, getting data access monitoring results of the
243 workloads or the system using DAMON, finding memory regions of special
244 characteristics by profiling the monitoring results, and making system
245 operation changes for the regions.  The changes could be made by modifying or
246 providing advice to the software (the application and/or the kernel), or
247 reconfiguring the hardware.  Both offline and online approaches could be
248 available.
249
250 Among those, providing advice to the kernel at runtime would be flexible and
251 effective, and therefore widely be used.   However, implementing such schemes
252 could impose unnecessary redundancy and inefficiency.  The profiling could be
253 redundant if the type of interest is common.  Exchanging the information
254 including monitoring results and operation advice between kernel and user
255 spaces could be inefficient.
256
257 To allow users to reduce such redundancy and inefficiencies by offloading the
258 works, DAMON provides a feature called Data Access Monitoring-based Operation
259 Schemes (DAMOS).  It lets users specify their desired schemes at a high
260 level.  For such specifications, DAMON starts monitoring, finds regions having
261 the access pattern of interest, and applies the user-desired operation actions
262 to the regions, for every user-specified time interval called
263 ``apply_interval``.
264
265
266 .. _damon_design_damos_action:
267
268 Operation Action
269 ~~~~~~~~~~~~~~~~
270
271 The management action that the users desire to apply to the regions of their
272 interest.  For example, paging out, prioritizing for next reclamation victim
273 selection, advising ``khugepaged`` to collapse or split, or doing nothing but
274 collecting statistics of the regions.
275
276 The list of supported actions is defined in DAMOS, but the implementation of
277 each action is in the DAMON operations set layer because the implementation
278 normally depends on the monitoring target address space.  For example, the code
279 for paging specific virtual address ranges out would be different from that for
280 physical address ranges.  And the monitoring operations implementation sets are
281 not mandated to support all actions of the list.  Hence, the availability of
282 specific DAMOS action depends on what operations set is selected to be used
283 together.
284
285 Applying an action to a region is considered as changing the region's
286 characteristics.  Hence, DAMOS resets the age of regions when an action is
287 applied to those.
288
289
290 .. _damon_design_damos_access_pattern:
291
292 Target Access Pattern
293 ~~~~~~~~~~~~~~~~~~~~~
294
295 The access pattern of the schemes' interest.  The patterns are constructed with
296 the properties that DAMON's monitoring results provide, specifically the size,
297 the access frequency, and the age.  Users can describe their access pattern of
298 interest by setting minimum and maximum values of the three properties.  If a
299 region's three properties are in the ranges, DAMOS classifies it as one of the
300 regions that the scheme is having an interest in.
301
302
303 .. _damon_design_damos_quotas:
304
305 Quotas
306 ~~~~~~
307
308 DAMOS upper-bound overhead control feature.  DAMOS could incur high overhead if
309 the target access pattern is not properly tuned.  For example, if a huge memory
310 region having the access pattern of interest is found, applying the scheme's
311 action to all pages of the huge region could consume unacceptably large system
312 resources.  Preventing such issues by tuning the access pattern could be
313 challenging, especially if the access patterns of the workloads are highly
314 dynamic.
315
316 To mitigate that situation, DAMOS provides an upper-bound overhead control
317 feature called quotas.  It lets users specify an upper limit of time that DAMOS
318 can use for applying the action, and/or a maximum bytes of memory regions that
319 the action can be applied within a user-specified time duration.
320
321
322 .. _damon_design_damos_quotas_prioritization:
323
324 Prioritization
325 ^^^^^^^^^^^^^^
326
327 A mechanism for making a good decision under the quotas.  When the action
328 cannot be applied to all regions of interest due to the quotas, DAMOS
329 prioritizes regions and applies the action to only regions having high enough
330 priorities so that it will not exceed the quotas.
331
332 The prioritization mechanism should be different for each action.  For example,
333 rarely accessed (colder) memory regions would be prioritized for page-out
334 scheme action.  In contrast, the colder regions would be deprioritized for huge
335 page collapse scheme action.  Hence, the prioritization mechanisms for each
336 action are implemented in each DAMON operations set, together with the actions.
337
338 Though the implementation is up to the DAMON operations set, it would be common
339 to calculate the priority using the access pattern properties of the regions.
340 Some users would want the mechanisms to be personalized for their specific
341 case.  For example, some users would want the mechanism to weigh the recency
342 (``age``) more than the access frequency (``nr_accesses``).  DAMOS allows users
343 to specify the weight of each access pattern property and passes the
344 information to the underlying mechanism.  Nevertheless, how and even whether
345 the weight will be respected are up to the underlying prioritization mechanism
346 implementation.
347
348
349 .. _damon_design_damos_watermarks:
350
351 Watermarks
352 ~~~~~~~~~~
353
354 Conditional DAMOS (de)activation automation.  Users might want DAMOS to run
355 only under certain situations.  For example, when a sufficient amount of free
356 memory is guaranteed, running a scheme for proactive reclamation would only
357 consume unnecessary system resources.  To avoid such consumption, the user would
358 need to manually monitor some metrics such as free memory ratio, and turn
359 DAMON/DAMOS on or off.
360
361 DAMOS allows users to offload such works using three watermarks.  It allows the
362 users to configure the metric of their interest, and three watermark values,
363 namely high, middle, and low.  If the value of the metric becomes above the
364 high watermark or below the low watermark, the scheme is deactivated.  If the
365 metric becomes below the mid watermark but above the low watermark, the scheme
366 is activated.  If all schemes are deactivated by the watermarks, the monitoring
367 is also deactivated.  In this case, the DAMON worker thread only periodically
368 checks the watermarks and therefore incurs nearly zero overhead.
369
370
371 .. _damon_design_damos_filters:
372
373 Filters
374 ~~~~~~~
375
376 Non-access pattern-based target memory regions filtering.  If users run
377 self-written programs or have good profiling tools, they could know something
378 more than the kernel, such as future access patterns or some special
379 requirements for specific types of memory. For example, some users may know
380 only anonymous pages can impact their program's performance.  They can also
381 have a list of latency-critical processes.
382
383 To let users optimize DAMOS schemes with such special knowledge, DAMOS provides
384 a feature called DAMOS filters.  The feature allows users to set an arbitrary
385 number of filters for each scheme.  Each filter specifies the type of target
386 memory, and whether it should exclude the memory of the type (filter-out), or
387 all except the memory of the type (filter-in).
388
389 Currently, anonymous page, memory cgroup, address range, and DAMON monitoring
390 target type filters are supported by the feature.  Some filter target types
391 require additional arguments.  The memory cgroup filter type asks users to
392 specify the file path of the memory cgroup for the filter.  The address range
393 type asks the start and end addresses of the range.  The DAMON monitoring
394 target type asks the index of the target from the context's monitoring targets
395 list.  Hence, users can apply specific schemes to only anonymous pages,
396 non-anonymous pages, pages of specific cgroups, all pages excluding those of
397 specific cgroups, pages in specific address range, pages in specific DAMON
398 monitoring targets, and any combination of those.
399
400 To handle filters efficiently, the address range and DAMON monitoring target
401 type filters are handled by the core layer, while others are handled by
402 operations set.  If a memory region is filtered by a core layer-handled filter,
403 it is not counted as the scheme has tried to the region.  In contrast, if a
404 memory regions is filtered by an operations set layer-handled filter, it is
405 counted as the scheme has tried.  The difference in accounting leads to changes
406 in the statistics.
407
408
409 Application Programming Interface
410 ---------------------------------
411
412 The programming interface for kernel space data access-aware applications.
413 DAMON is a framework, so it does nothing by itself.  Instead, it only helps
414 other kernel components such as subsystems and modules building their data
415 access-aware applications using DAMON's core features.  For this, DAMON exposes
416 its all features to other kernel components via its application programming
417 interface, namely ``include/linux/damon.h``.  Please refer to the API
418 :doc:`document </mm/damon/api>` for details of the interface.
419
420
421 Modules
422 =======
423
424 Because the core of DAMON is a framework for kernel components, it doesn't
425 provide any direct interface for the user space.  Such interfaces should be
426 implemented by each DAMON API user kernel components, instead.  DAMON subsystem
427 itself implements such DAMON API user modules, which are supposed to be used
428 for general purpose DAMON control and special purpose data access-aware system
429 operations, and provides stable application binary interfaces (ABI) for the
430 user space.  The user space can build their efficient data access-aware
431 applications using the interfaces.
432
433
434 General Purpose User Interface Modules
435 --------------------------------------
436
437 DAMON modules that provide user space ABIs for general purpose DAMON usage in
438 runtime.
439
440 DAMON user interface modules, namely 'DAMON sysfs interface' and 'DAMON debugfs
441 interface' are DAMON API user kernel modules that provide ABIs to the
442 user-space.  Please note that DAMON debugfs interface is currently deprecated.
443
444 Like many other ABIs, the modules create files on sysfs and debugfs, allow
445 users to specify their requests to and get the answers from DAMON by writing to
446 and reading from the files.  As a response to such I/O, DAMON user interface
447 modules control DAMON and retrieve the results as user requested via the DAMON
448 API, and return the results to the user-space.
449
450 The ABIs are designed to be used for user space applications development,
451 rather than human beings' fingers.  Human users are recommended to use such
452 user space tools.  One such Python-written user space tool is available at
453 Github (https://github.com/awslabs/damo), Pypi
454 (https://pypistats.org/packages/damo), and Fedora
455 (https://packages.fedoraproject.org/pkgs/python-damo/damo/).
456
457 Please refer to the ABI :doc:`document </admin-guide/mm/damon/usage>` for
458 details of the interfaces.
459
460
461 Special-Purpose Access-aware Kernel Modules
462 -------------------------------------------
463
464 DAMON modules that provide user space ABI for specific purpose DAMON usage.
465
466 DAMON sysfs/debugfs user interfaces are for full control of all DAMON features
467 in runtime.  For each special-purpose system-wide data access-aware system
468 operations such as proactive reclamation or LRU lists balancing, the interfaces
469 could be simplified by removing unnecessary knobs for the specific purpose, and
470 extended for boot-time and even compile time control.  Default values of DAMON
471 control parameters for the usage would also need to be optimized for the
472 purpose.
473
474 To support such cases, yet more DAMON API user kernel modules that provide more
475 simple and optimized user space interfaces are available.  Currently, two
476 modules for proactive reclamation and LRU lists manipulation are provided.  For
477 more detail, please read the usage documents for those
478 (:doc:`/admin-guide/mm/damon/reclaim` and
479 :doc:`/admin-guide/mm/damon/lru_sort`).
480
481
482 .. _damon_design_execution_model_and_data_structures:
483
484 Execution Model and Data Structures
485 ===================================
486
487 The monitoring-related information including the monitoring request
488 specification and DAMON-based operation schemes are stored in a data structure
489 called DAMON ``context``.  DAMON executes each context with a kernel thread
490 called ``kdamond``.  Multiple kdamonds could run in parallel, for different
491 types of monitoring.