1 /*******************************************************************************
2     Copyright (c) 2016-2022 NVIDIA Corporation
3 
4     Permission is hereby granted, free of charge, to any person obtaining a copy
5     of this software and associated documentation files (the "Software"), to
6     deal in the Software without restriction, including without limitation the
7     rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8     sell copies of the Software, and to permit persons to whom the Software is
9     furnished to do so, subject to the following conditions:
10 
11         The above copyright notice and this permission notice shall be
12         included in all copies or substantial portions of the Software.
13 
14     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17     THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19     FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20     DEALINGS IN THE SOFTWARE.
21 
22 *******************************************************************************/
23 
24 #ifndef __UVM_PERF_THRASHING_H__
25 #define __UVM_PERF_THRASHING_H__
26 
27 #include "uvm_linux.h"
28 #include "uvm_extern_decl.h"
29 #include "uvm_forward_decl.h"
30 #include "uvm_processors.h"
31 #include "uvm_va_block_types.h"
32 
33 typedef enum
34 {
35     // No thrashing detected
36     UVM_PERF_THRASHING_HINT_TYPE_NONE     = 0,
37 
38     // Map remotely to avoid future faults (does not help with revocations due
39     // to system-wide atomics)
40     UVM_PERF_THRASHING_HINT_TYPE_PIN      = 1,
41 
42     // Throttle execution of the calling processor (this can be implemented by
43     // sleeping or handing other faults)
44     UVM_PERF_THRASHING_HINT_TYPE_THROTTLE = 2,
45 
46     // TODO: Bug 1877578: Implement heuristics-driven read-duplication
47     // Add a thrashing hint type to read-duplicate a page when it is being
48     // accessed read-only from different processors
49 } uvm_perf_thrashing_hint_type_t;
50 
51 typedef struct
52 {
53     uvm_perf_thrashing_hint_type_t type;
54 
55     union
56     {
57         struct
58         {
59             // Map to this processor, which must be accessible, at least, from
60             // the calling processor
61             uvm_processor_id_t residency;
62 
63             // Processors to be mapped, when possible, to the new residency
64             uvm_processor_mask_t processors;
65         } pin;
66 
67         struct
68         {
69             // Absolute timestamp in ns after which the throttled processor is
70             // allowed to start servicing faults on the thrashing page.
71             NvU64 end_time_stamp;
72         } throttle;
73     };
74 } uvm_perf_thrashing_hint_t;
75 
76 // Obtain a hint to prevent thrashing on the page with given address
77 uvm_perf_thrashing_hint_t uvm_perf_thrashing_get_hint(uvm_va_block_t *va_block, NvU64 address,
78                                                       uvm_processor_id_t requester);
79 
80 // Obtain a pointer to a mask with the processors that are thrashing on the
81 // given page. This function assumes that thrashing has been just reported on
82 // the page. It will fail otherwise.
83 uvm_processor_mask_t *uvm_perf_thrashing_get_thrashing_processors(uvm_va_block_t *va_block, NvU64 address);
84 
85 const uvm_page_mask_t *uvm_perf_thrashing_get_thrashing_pages(uvm_va_block_t *va_block);
86 
87 // Global initialization/cleanup functions
88 NV_STATUS uvm_perf_thrashing_init(void);
89 void uvm_perf_thrashing_exit(void);
90 
91 // Per-GPU initialization/cleanup functions. See comments in
92 // uvm_perf_heuristics.h
93 NV_STATUS uvm_perf_thrashing_add_gpu(uvm_gpu_t *gpu);
94 void uvm_perf_thrashing_remove_gpu(uvm_gpu_t *gpu);
95 
96 // VA space Initialization/cleanup functions. See comments in
97 // uvm_perf_heuristics.h
98 NV_STATUS uvm_perf_thrashing_load(uvm_va_space_t *va_space);
99 NV_STATUS uvm_perf_thrashing_register_gpu(uvm_va_space_t *va_space, uvm_gpu_t *gpu);
100 void uvm_perf_thrashing_stop(uvm_va_space_t *va_space);
101 void uvm_perf_thrashing_unload(uvm_va_space_t *va_space);
102 
103 // Destroy the thrashing detection struct for the given block.
104 void uvm_perf_thrashing_info_destroy(uvm_va_block_t *va_block);
105 
106 // Unmap remote mappings from all processors on the pinned pages
107 // described by region and block_thrashing->pinned pages.
108 // va_block_context must not be NULL and va_block_context->policy must be valid.
109 // See the comments for uvm_va_block_check_policy_is_valid() in uvm_va_block.h.
110 // Locking: the va_block lock must be held.
111 NV_STATUS uvm_perf_thrashing_unmap_remote_pinned_pages_all(uvm_va_block_t *va_block,
112                                                            uvm_va_block_context_t *va_block_context,
113                                                            uvm_va_block_region_t region);
114 
115 #endif
116