-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathuvm_pmm_gpu.c
More file actions
4076 lines (3285 loc) · 144 KB
/
uvm_pmm_gpu.c
File metadata and controls
4076 lines (3285 loc) · 144 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
Copyright (c) 2015-2025 NVIDIA Corporation
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*******************************************************************************/
//
// High level description of PMM is in the header file, here some implementation
// details are discussed.
//
// There is one PMM object per GPU and the PMM state among GPUs is completely
// separate with the exception of a few shared kmem caches.
//
// PMM allocates all of the memory it manages from PMA which is the common GPU
// Physical Memory Allocator shared by UVM and RM (it's included as part of RM,
// but logically separate from it).
//
// The state of each GPU memory chunk is tracked in uvm_gpu_chunk_t objects.
// Each chunk has a type, size and state. Type and size are persistent
// throughout chunk's lifetime while its state changes as it's allocated, split,
// merged and freed.
//
// PMM maintains a pre-allocated flat array of root chunks covering all possible
// physical allocations that can be returned from PMA. For simplicity, PMM
// always allocates 2M (UVM_CHUNK_SIZE_MAX) chunks from PMA and each naturally
// aligned 2M chunk represents a single root chunk. The root chunks array is
// indexed by the physical address of each chunk divided by UVM_CHUNK_SIZE_MAX
// allowing for a simple and fast lookup of root chunks.
//
// Each root chunk has a tracker for any pending operations on the root chunk
// (including all of its subchunks in case it's split) to support asynchronous
// alloc and free. Each tracker is protected by a separate bitlock (see
// root_chunk_lock()) as synchronizing any pending operations might take a long
// time and it would be undesirable for that to block other operations of PMM.
// Notably some synchronization is required as part of allocation to handle GPU
// lifetime issues across VA spaces (see comments in pmm_gpu_alloc()). Bit
// locks (instead of a mutex in each root chunk) are used to save space.
//
// All free chunks (UVM_PMM_GPU_CHUNK_STATE_FREE) are kept on free lists, with
// one list per each combination of memory type and chunk size (see usage of
// uvm_pmm_gpu_t::free_list for reference). This allows for a very quick
// allocation and freeing of chunks in case the right size is already available
// on alloc or no merges are required on free. See claim_free_chunk() for
// allocation and chunk_free_locked() for freeing.
//
// When a chunk is allocated it transitions into the temporarily pinned state
// (UVM_PMM_GPU_CHUNK_STATE_TEMP_PINNED) until it's unpinned when it becomes
// allocated (UVM_PMM_GPU_CHUNK_STATE_ALLOCATED). This transition is only
// meaningful for user memory chunks where temporarily pinned chunks cannot be
// evicted. Kernel memory type chunks do not support eviction at all and they
// are transitioned into the allocated state as part of the allocation itself
// (see uvm_pmm_gpu_alloc_kernel). When the chunk is freed it transitions back
// to the free state and is placed on an appropriate free list.
//
// To support smaller allocations, PMM internally splits and merges root chunks
// as needed. Splitting and merging is protected by an exclusive lock
// (uvm_pmm_gpu_t::lock) to prevent PMM from over-allocating root chunks in case
// multiple threads race for a small allocation and there are no free chunks
// immediately available.
//
// Splitting is performed lazily, i.e. chunks are only split when a chunk of the
// requested type and size is not available. Splits are only done to the next
// smaller size and hence may need to be performed multiple times recursively to
// get to the desired chunk size. See alloc_chunk_with_splits(). All split
// chunks under the root chunk form a tree with all internal nodes being in
// split state and leaf nodes being in any of the free, allocated or pinned
// states.
//
// Merging is performed eagerly, i.e. whenever all chunks under a parent (split)
// chunk become free, they are merged into one bigger chunk. See
// free_chunk_with_merges().
//
// Splitting and merging already allocated chunks is also exposed to the users
// of allocated chunks. See uvm_pmm_gpu_split_chunk() and
// uvm_pmm_gpu_merge_chunk().
//
// As splits and merges are protected by a single PMM mutex, they are only
// performed when really necessary. See alloc_chunk() that falls back to split
// only as the last step and free_chunk() that similarly first tries performing
// a quick free.
//
// When a memory allocation from PMA fails and eviction is requested, PMM will
// check whether it can evict any user memory chunks to satisfy the request.
// All allocated user memory root chunks are tracked in an LRU list
// (root_chunks.va_block_used). A root chunk is moved to the tail of that list
// whenever any of its subchunks is allocated (unpinned) by a VA block (see
// uvm_pmm_gpu_unpin_allocated()). When a root chunk is selected for eviction,
// it has the eviction flag set (see pick_root_chunk_to_evict()). This flag
// affects many of the PMM operations on all of the subchunks of the root chunk
// being evicted. See usage of (root_)chunk_is_in_eviction(), in particular in
// chunk_free_locked() and claim_free_chunk().
//
// To evict a root chunk, all of its free subchunks are pinned, then all
// resident pages backed by it are moved to the CPU one VA block at a time.
// After all of them are moved, the root chunk is merged and returned to the
// caller. See evict_root_chunk() for details.
//
// Eviction is also possible to be triggered by PMA. This makes it possible for
// other PMA clients (most importantly RM which CUDA uses for non-UVM
// allocations) to successfully allocate memory from the user memory pool
// allocated by UVM. UVM registers two eviction callbacks with PMA that PMA
// calls as needed to perform the eviction:
// - uvm_pmm_gpu_pma_evict_range - for evicting a physical range
// - uvm_pmm_gpu_pma_evict_pages - for evicting a number of pages
//
// Both of them perform the eviction using the same building blocks as internal
// eviction, but see their implementation and references to pma.h for more
// details.
//
// PMM locking
// - PMM mutex
// Exclusive lock protecting both internal and external splits and merges, and
// eviction.
//
// - PMM list lock
// Protects state transitions of chunks and their movement among lists.
//
// - PMM root chunk bit locks
// Each bit lock protects the corresponding root chunk's allocation, freeing
// from/to PMA, and root chunk trackers.
//
// - PMA allocation/eviction lock
// A read-write semaphore used by the eviction path to flush any pending
// allocations. See usage of pma_lock in alloc_root_chunk() and
// uvm_pmm_gpu_pma_evict_range().
//
// == Trade-offs ===
//
// In general, PMM is optimized towards Pascal+ and 2M VA blocks (that's also
// the UVM_CHUNK_SIZE_MAX) as Pascal+ makes much heavier use of PMM:
// - Oversubscription is Pascal+ only
// - On pre-Pascal (UVM-Lite) CUDA currently pre-populates all managed memory
// and hence performance matters mostly only during CUDA memory allocation.
// - On Pascal+ CUDA doesn't pre-populate and memory is allocated on first
// touch.
//
// The root chunk size matching the VA block chunk size allows PMM to avoid
// having to split and merge for the hopefully (HMM might make this hard) common
// allocation size of 2M on Pascal+.
//
// Careful benchmarks and tweaking of PMM are yet to be performed, but there is
// some evidence for PMA to potentially cause issues for oversubscription (see
// bug 1775408).
//
#include "uvm_common.h"
#include "nv_uvm_interface.h"
#include "uvm_api.h"
#include "uvm_gpu.h"
#include "uvm_pmm_gpu.h"
#include "uvm_mem.h"
#include "uvm_mmu.h"
#include "uvm_global.h"
#include "uvm_kvmalloc.h"
#include "uvm_va_space.h"
#include "uvm_va_block.h"
#include "uvm_va_range.h"
#include "uvm_test.h"
#include "uvm_linux.h"
#if defined(CONFIG_PCI_P2PDMA) && defined(NV_STRUCT_PAGE_HAS_ZONE_DEVICE_DATA)
#include <linux/pci-p2pdma.h>
#endif
static int uvm_global_oversubscription = 1;
module_param(uvm_global_oversubscription, int, S_IRUGO);
MODULE_PARM_DESC(uvm_global_oversubscription, "Enable (1) or disable (0) global oversubscription support.");
#define UVM_PERF_PMA_BATCH_NONPINNED_ORDER_DEFAULT 6
// Non-pinned root chunks are allocated in batches, in order to minimize the
// number of calls into PMA. The number of root chunks in the batch is:
// (1 << uvm_perf_pma_batch_nonpinned_order)
static unsigned uvm_perf_pma_batch_nonpinned_order = UVM_PERF_PMA_BATCH_NONPINNED_ORDER_DEFAULT;
module_param(uvm_perf_pma_batch_nonpinned_order, uint, S_IRUGO);
// Helper type for refcounting cache
typedef struct
{
// Cache for given split size
struct kmem_cache *cache;
// Number of GPUs using given split size
NvU32 refcount;
// Name of cache
char name[32];
} kmem_cache_ref_t;
static kmem_cache_ref_t g_pma_address_batch_cache_ref;
struct uvm_pmm_gpu_chunk_suballoc_struct
{
// Number of allocated chunks (including pinned ones)
NvU32 allocated;
// Number of pinned leaf chunks under this chunk
//
// Tracked only for suballocs of root chunks to know whether a root chunk
// can be evicted. This is not in the uvm_gpu_root_chunk_t itself to stop
// the root chunks array from growing too much.
// TODO: Bug 1765193: Consider moving this to a union with the parent
// pointer in uvm_gpu_chunk_t as root chunks never have a parent or just put
// in the root chunk directly.
// TODO: Bug 1765193: This could be NvU16 if we enforce the smallest chunk
// size to be at least 2^21 / 2^16 = 32 bytes.
NvU32 pinned_leaf_chunks;
// Array of all child subchunks
// TODO: Bug 1765461: Can the array be inlined? It could save the parent
// pointer.
uvm_gpu_chunk_t *subchunks[];
};
typedef enum
{
CHUNK_WALK_PRE_ORDER,
CHUNK_WALK_POST_ORDER
} chunk_walk_order_t;
typedef NV_STATUS (*chunk_walk_func_t)(uvm_pmm_gpu_t *pmm, uvm_gpu_chunk_t *chunk, void *data);
// Cache for allocation of uvm_pmm_gpu_chunk_suballoc_t. At index n it stores
// a suballoc structure for size 2**n.
//
// For convenience of init/deinit code level 0 is for allocation of chunks
static kmem_cache_ref_t chunk_split_cache[UVM_PMM_CHUNK_SPLIT_CACHE_SIZES];
#define CHUNK_CACHE chunk_split_cache[0].cache
const char *uvm_pmm_gpu_memory_type_string(uvm_pmm_gpu_memory_type_t type)
{
switch (type) {
UVM_ENUM_STRING_CASE(UVM_PMM_GPU_MEMORY_TYPE_USER);
UVM_ENUM_STRING_CASE(UVM_PMM_GPU_MEMORY_TYPE_KERNEL);
UVM_ENUM_STRING_DEFAULT();
}
BUILD_BUG_ON(UVM_PMM_GPU_MEMORY_TYPE_COUNT != 2);
}
const char *uvm_pmm_gpu_chunk_state_string(uvm_pmm_gpu_chunk_state_t state)
{
switch (state) {
UVM_ENUM_STRING_CASE(UVM_PMM_GPU_CHUNK_STATE_PMA_OWNED);
UVM_ENUM_STRING_CASE(UVM_PMM_GPU_CHUNK_STATE_FREE);
UVM_ENUM_STRING_CASE(UVM_PMM_GPU_CHUNK_STATE_IS_SPLIT);
UVM_ENUM_STRING_CASE(UVM_PMM_GPU_CHUNK_STATE_TEMP_PINNED);
UVM_ENUM_STRING_CASE(UVM_PMM_GPU_CHUNK_STATE_ALLOCATED);
UVM_ENUM_STRING_DEFAULT();
}
}
// The PMA APIs that can be called from PMA eviction callbacks (pmaPinPages and
// pmaFreePages*) need to be called differently depending whether it's as part
// of PMA eviction or not. The PMM context is used to plumb that information
// through the stack in a couple of places.
typedef enum
{
PMM_CONTEXT_DEFAULT,
PMM_CONTEXT_PMA_EVICTION,
} uvm_pmm_context_t;
// Freeing the root chunk not only needs to differentiate between two different
// contexts for calling pmaFreePages(), but also in some cases the free back to
// PMA needs to be skipped altogether.
typedef enum
{
FREE_ROOT_CHUNK_MODE_DEFAULT,
FREE_ROOT_CHUNK_MODE_PMA_EVICTION,
FREE_ROOT_CHUNK_MODE_SKIP_PMA_FREE
} free_root_chunk_mode_t;
static free_root_chunk_mode_t free_root_chunk_mode_from_pmm_context(uvm_pmm_context_t pmm_context)
{
switch (pmm_context) {
case PMM_CONTEXT_DEFAULT:
return FREE_ROOT_CHUNK_MODE_DEFAULT;
case PMM_CONTEXT_PMA_EVICTION:
return FREE_ROOT_CHUNK_MODE_PMA_EVICTION;
default:
UVM_ASSERT_MSG(false, "Invalid PMM context: 0x%x\n", pmm_context);
return FREE_ROOT_CHUNK_MODE_DEFAULT;
}
}
static NV_STATUS alloc_chunk(uvm_pmm_gpu_t *pmm,
uvm_pmm_gpu_memory_type_t type,
uvm_chunk_size_t chunk_size,
uvm_pmm_alloc_flags_t flags,
uvm_gpu_chunk_t **chunk);
static NV_STATUS alloc_root_chunk(uvm_pmm_gpu_t *pmm,
uvm_pmm_gpu_memory_type_t type,
uvm_pmm_alloc_flags_t flags,
uvm_gpu_chunk_t **chunk);
static void free_root_chunk(uvm_pmm_gpu_t *pmm, uvm_gpu_root_chunk_t *root_chunk, free_root_chunk_mode_t free_mode);
static NV_STATUS split_gpu_chunk(uvm_pmm_gpu_t *pmm, uvm_gpu_chunk_t *chunk);
static void free_chunk(uvm_pmm_gpu_t *pmm, uvm_gpu_chunk_t *chunk);
static void free_chunk_with_merges(uvm_pmm_gpu_t *pmm, uvm_gpu_chunk_t *chunk);
static bool free_next_available_root_chunk(uvm_pmm_gpu_t *pmm, uvm_pmm_gpu_memory_type_t type);
static struct list_head *find_free_list(uvm_pmm_gpu_t *pmm,
uvm_pmm_gpu_memory_type_t type,
uvm_chunk_size_t chunk_size,
uvm_pmm_list_zero_t zero_type);
static bool check_chunk(uvm_pmm_gpu_t *pmm, uvm_gpu_chunk_t *chunk);
static struct list_head *find_free_list_chunk(uvm_pmm_gpu_t *pmm, uvm_gpu_chunk_t *chunk);
static void chunk_free_locked(uvm_pmm_gpu_t *pmm, uvm_gpu_chunk_t *chunk);
static size_t root_chunk_index(uvm_pmm_gpu_t *pmm, uvm_gpu_root_chunk_t *root_chunk)
{
size_t index = root_chunk->chunk.address / UVM_CHUNK_SIZE_MAX;
UVM_ASSERT(index < pmm->root_chunks.count);
return index;
}
static void root_chunk_lock(uvm_pmm_gpu_t *pmm, uvm_gpu_root_chunk_t *root_chunk)
{
uvm_bit_lock(&pmm->root_chunks.bitlocks, root_chunk_index(pmm, root_chunk));
}
static void uvm_assert_root_chunk_locked(uvm_pmm_gpu_t *pmm, uvm_gpu_root_chunk_t *root_chunk)
{
uvm_assert_bit_locked(&pmm->root_chunks.bitlocks, root_chunk_index(pmm, root_chunk));
}
static void root_chunk_unlock(uvm_pmm_gpu_t *pmm, uvm_gpu_root_chunk_t *root_chunk)
{
uvm_bit_unlock(&pmm->root_chunks.bitlocks, root_chunk_index(pmm, root_chunk));
}
// TODO: Bug 1795559: Remove once PMA eviction is considered safe enough not to
// have an opt-out.
static bool gpu_supports_pma_eviction(uvm_gpu_t *gpu)
{
return uvm_global_oversubscription && uvm_parent_gpu_supports_eviction(gpu->parent);
}
uvm_gpu_t *uvm_pmm_to_gpu(uvm_pmm_gpu_t *pmm)
{
return container_of(pmm, uvm_gpu_t, pmm);
}
static uvm_gpu_root_chunk_t *root_chunk_from_address(uvm_pmm_gpu_t *pmm, NvU64 addr)
{
uvm_gpu_t *gpu = uvm_pmm_to_gpu(pmm);
size_t index = addr / UVM_CHUNK_SIZE_MAX;
uvm_gpu_root_chunk_t *root_chunk = &pmm->root_chunks.array[index];
UVM_ASSERT_MSG(addr <= gpu->mem_info.max_allocatable_address,
"Address 0x%llx vidmem max phys 0x%llx GPU %s\n",
addr,
gpu->mem_info.max_allocatable_address,
uvm_gpu_name(gpu));
UVM_ASSERT(root_chunk->chunk.address == UVM_ALIGN_DOWN(addr, UVM_CHUNK_SIZE_MAX));
return root_chunk;
}
static uvm_gpu_root_chunk_t *root_chunk_from_chunk(uvm_pmm_gpu_t *pmm, uvm_gpu_chunk_t *chunk)
{
return root_chunk_from_address(pmm, chunk->address);
}
static bool chunk_is_root_chunk(uvm_gpu_chunk_t *chunk)
{
return uvm_gpu_chunk_get_size(chunk) == UVM_CHUNK_SIZE_MAX;
}
static bool chunk_is_root_chunk_pinned(uvm_pmm_gpu_t *pmm, uvm_gpu_chunk_t *chunk)
{
uvm_gpu_root_chunk_t *root_chunk = root_chunk_from_chunk(pmm, chunk);
uvm_assert_spinlock_locked(&pmm->list_lock);
chunk = &root_chunk->chunk;
if (chunk->state == UVM_PMM_GPU_CHUNK_STATE_TEMP_PINNED)
return true;
else if (chunk->state != UVM_PMM_GPU_CHUNK_STATE_IS_SPLIT)
return false;
UVM_ASSERT(chunk->suballoc);
return chunk->suballoc->pinned_leaf_chunks > 0;
}
// Pin a chunk and update its root chunk's pinned leaf chunks count if the
// chunk is not a root chunk.
static void chunk_pin(uvm_pmm_gpu_t *pmm, uvm_gpu_chunk_t *chunk)
{
uvm_gpu_root_chunk_t *root_chunk = root_chunk_from_chunk(pmm, chunk);
// The PMM list_lock must be held, but calling uvm_assert_spinlock_locked()
// is not possible here due to the absence of the UVM context pointer in
// the interrupt context when called from devmem_page_free().
UVM_ASSERT(chunk->state != UVM_PMM_GPU_CHUNK_STATE_TEMP_PINNED);
chunk->state = UVM_PMM_GPU_CHUNK_STATE_TEMP_PINNED;
if (chunk_is_root_chunk(chunk))
return;
// For subchunks, update the pinned leaf chunks count tracked in the
// suballoc of the root chunk.
chunk = &root_chunk->chunk;
// The passed-in subchunk is not the root chunk so the root chunk has to be
// split.
UVM_ASSERT_MSG(chunk->state == UVM_PMM_GPU_CHUNK_STATE_IS_SPLIT,
"chunk state %s\n",
uvm_pmm_gpu_chunk_state_string(chunk->state));
chunk->suballoc->pinned_leaf_chunks++;
}
// Unpin a chunk and update its root chunk's pinned leaf chunks count if the
// chunk is not a root chunk.
static void chunk_unpin(uvm_pmm_gpu_t *pmm, uvm_gpu_chunk_t *chunk, uvm_pmm_gpu_chunk_state_t new_state)
{
uvm_gpu_root_chunk_t *root_chunk = root_chunk_from_chunk(pmm, chunk);
uvm_assert_spinlock_locked(&pmm->list_lock);
UVM_ASSERT(chunk->state == UVM_PMM_GPU_CHUNK_STATE_TEMP_PINNED);
UVM_ASSERT(chunk_is_root_chunk_pinned(pmm, chunk));
UVM_ASSERT(new_state != UVM_PMM_GPU_CHUNK_STATE_TEMP_PINNED);
chunk->state = new_state;
if (chunk_is_root_chunk(chunk))
return;
// For subchunks, update the pinned leaf chunks count tracked in the
// suballoc of the root chunk.
chunk = &root_chunk->chunk;
// The passed-in subchunk is not the root chunk so the root chunk has to be
// split.
UVM_ASSERT_MSG(chunk->state == UVM_PMM_GPU_CHUNK_STATE_IS_SPLIT,
"chunk state %s\n",
uvm_pmm_gpu_chunk_state_string(chunk->state));
UVM_ASSERT(chunk->suballoc->pinned_leaf_chunks != 0);
chunk->suballoc->pinned_leaf_chunks--;
}
static void uvm_gpu_chunk_set_in_eviction(uvm_gpu_chunk_t *chunk, bool in_eviction)
{
UVM_ASSERT(uvm_gpu_chunk_is_user(chunk));
UVM_ASSERT(uvm_gpu_chunk_get_size(chunk) == UVM_CHUNK_SIZE_MAX);
chunk->in_eviction = in_eviction;
}
// A helper that queries the eviction flag of root chunk of the given chunk.
// Eviction is only tracked for root chunks.
static bool chunk_is_in_eviction(uvm_pmm_gpu_t *pmm, uvm_gpu_chunk_t *chunk)
{
return root_chunk_from_chunk(pmm, chunk)->chunk.in_eviction;
}
uvm_gpu_t *uvm_gpu_chunk_get_gpu(const uvm_gpu_chunk_t *chunk)
{
uvm_gpu_t *gpu = uvm_gpu_get(uvm_gpu_id_from_index(chunk->gpu_index));
UVM_ASSERT(gpu);
return gpu;
}
NvU64 uvm_gpu_chunk_to_sys_addr(uvm_pmm_gpu_t *pmm, uvm_gpu_chunk_t *chunk)
{
uvm_gpu_t *gpu = uvm_pmm_to_gpu(pmm);
NvU64 sys_addr = chunk->address + gpu->parent->system_bus.memory_window_start;
UVM_ASSERT(sys_addr + uvm_gpu_chunk_get_size(chunk) <= gpu->parent->system_bus.memory_window_end + 1);
return sys_addr;
}
struct page *uvm_gpu_chunk_to_page(uvm_pmm_gpu_t *pmm, uvm_gpu_chunk_t *chunk)
{
uvm_gpu_t *gpu = uvm_pmm_to_gpu(pmm);
unsigned long pfn = uvm_gpu_chunk_to_sys_addr(pmm, chunk) >> PAGE_SHIFT;
UVM_ASSERT(gpu->mem_info.numa.enabled);
return pfn_to_page(pfn);
}
void uvm_pmm_gpu_sync(uvm_pmm_gpu_t *pmm)
{
size_t i;
if (!pmm->initialized)
return;
// Just go over all root chunks and sync the ones that are not PMA OWNED.
// This is slow, but uvm_pmm_gpu_sync() is a rarely used operation not
// critical for performance.
for (i = 0; i < pmm->root_chunks.count; ++i) {
uvm_gpu_root_chunk_t *root_chunk = &pmm->root_chunks.array[i];
root_chunk_lock(pmm, root_chunk);
if (root_chunk->chunk.state != UVM_PMM_GPU_CHUNK_STATE_PMA_OWNED) {
NV_STATUS status = uvm_tracker_wait(&root_chunk->tracker);
if (status != NV_OK)
UVM_ASSERT(status == uvm_global_get_status());
}
root_chunk_unlock(pmm, root_chunk);
}
}
static NV_STATUS pmm_gpu_alloc(uvm_pmm_gpu_t *pmm,
size_t num_chunks,
uvm_chunk_size_t chunk_size,
uvm_pmm_gpu_memory_type_t mem_type,
uvm_pmm_alloc_flags_t flags,
uvm_gpu_chunk_t **chunks,
uvm_tracker_t *out_tracker)
{
uvm_gpu_t *gpu = uvm_pmm_to_gpu(pmm);
NV_STATUS status;
uvm_tracker_t local_tracker = UVM_TRACKER_INIT();
size_t i;
UVM_ASSERT((unsigned)mem_type < UVM_PMM_GPU_MEMORY_TYPE_COUNT);
UVM_ASSERT_MSG(is_power_of_2(chunk_size), "chunk size %u\n", chunk_size);
UVM_ASSERT_MSG(chunk_size & pmm->chunk_sizes[mem_type], "chunk size %u\n", chunk_size);
UVM_ASSERT(num_chunks == 0 || chunks);
UVM_ASSERT((flags & UVM_PMM_ALLOC_FLAGS_MASK) == flags);
if (flags & UVM_PMM_ALLOC_FLAGS_EVICT) {
// If eviction is requested then VA block locks need to be lockable
uvm_assert_lockable_order(UVM_LOCK_ORDER_VA_BLOCK);
}
for (i = 0; i < num_chunks; i++) {
uvm_gpu_root_chunk_t *root_chunk;
status = alloc_chunk(pmm, mem_type, chunk_size, flags, &chunks[i]);
if (status != NV_OK)
goto error;
root_chunk = root_chunk_from_chunk(pmm, chunks[i]);
root_chunk_lock(pmm, root_chunk);
uvm_tracker_remove_completed(&root_chunk->tracker);
status = uvm_tracker_add_tracker_safe(&local_tracker, &root_chunk->tracker);
root_chunk_unlock(pmm, root_chunk);
if (status != NV_OK) {
i++;
goto error;
}
}
// Before we return to the caller, we need to ensure that the tracker only
// contains tracker entries belonging to the PMM's GPU. Otherwise we
// could leak trackers for other GPUs into VA spaces which never
// registered those GPUs, causing lifetime problems when those GPUs go
// away.
status = uvm_tracker_wait_for_other_gpus(&local_tracker, gpu);
if (status != NV_OK)
goto error;
if (out_tracker) {
status = uvm_tracker_add_tracker_safe(out_tracker, &local_tracker);
uvm_tracker_clear(&local_tracker);
if (status != NV_OK)
goto error;
}
return uvm_tracker_wait_deinit(&local_tracker);
error:
uvm_tracker_deinit(&local_tracker);
while (i-- > 0)
free_chunk(pmm, chunks[i]);
// Reset the array to make error handling easier for callers.
memset(chunks, 0, sizeof(chunks[0]) * num_chunks);
return status;
}
NV_STATUS uvm_pmm_gpu_alloc_kernel(uvm_pmm_gpu_t *pmm,
size_t num_chunks,
uvm_chunk_size_t chunk_size,
uvm_pmm_alloc_flags_t flags,
uvm_gpu_chunk_t **chunks,
uvm_tracker_t *out_tracker)
{
NV_STATUS status;
size_t i;
status = pmm_gpu_alloc(pmm, num_chunks, chunk_size, UVM_PMM_GPU_MEMORY_TYPE_KERNEL, flags, chunks, out_tracker);
if (status != NV_OK)
return status;
for (i = 0; i < num_chunks; ++i) {
uvm_spin_lock(&pmm->list_lock);
chunk_unpin(pmm, chunks[i], UVM_PMM_GPU_CHUNK_STATE_ALLOCATED);
chunks[i]->is_referenced = false;
uvm_spin_unlock(&pmm->list_lock);
}
return NV_OK;
}
NV_STATUS uvm_pmm_gpu_alloc_user(uvm_pmm_gpu_t *pmm,
size_t num_chunks,
uvm_chunk_size_t chunk_size,
uvm_pmm_alloc_flags_t flags,
uvm_gpu_chunk_t **chunks,
uvm_tracker_t *out_tracker)
{
return pmm_gpu_alloc(pmm, num_chunks, chunk_size, UVM_PMM_GPU_MEMORY_TYPE_USER, flags, chunks, out_tracker);
}
static void chunk_update_lists_locked(uvm_pmm_gpu_t *pmm, uvm_gpu_chunk_t *chunk)
{
uvm_gpu_root_chunk_t *root_chunk = root_chunk_from_chunk(pmm, chunk);
uvm_assert_spinlock_locked(&pmm->list_lock);
if (uvm_gpu_chunk_is_user(chunk)) {
if (chunk_is_root_chunk_pinned(pmm, chunk)) {
UVM_ASSERT(root_chunk->chunk.state == UVM_PMM_GPU_CHUNK_STATE_IS_SPLIT ||
root_chunk->chunk.state == UVM_PMM_GPU_CHUNK_STATE_TEMP_PINNED);
list_del_init(&root_chunk->chunk.list);
}
else if (root_chunk->chunk.state != UVM_PMM_GPU_CHUNK_STATE_FREE) {
UVM_ASSERT(root_chunk->chunk.state == UVM_PMM_GPU_CHUNK_STATE_IS_SPLIT ||
root_chunk->chunk.state == UVM_PMM_GPU_CHUNK_STATE_ALLOCATED);
list_move_tail(&root_chunk->chunk.list, &pmm->root_chunks.va_block_used);
}
}
// TODO: Bug 1757148: Improve fragmentation of split chunks
if (chunk->state == UVM_PMM_GPU_CHUNK_STATE_FREE)
list_move_tail(&chunk->list, find_free_list_chunk(pmm, chunk));
else if (chunk->state == UVM_PMM_GPU_CHUNK_STATE_TEMP_PINNED)
list_del_init(&chunk->list);
}
void uvm_pmm_gpu_unpin_allocated(uvm_pmm_gpu_t *pmm, uvm_gpu_chunk_t *chunk, uvm_va_block_t *va_block)
{
UVM_ASSERT(chunk->state == UVM_PMM_GPU_CHUNK_STATE_TEMP_PINNED);
UVM_ASSERT(uvm_gpu_chunk_is_user(chunk));
UVM_ASSERT(list_empty(&chunk->list));
UVM_ASSERT(va_block);
UVM_ASSERT(chunk->va_block == va_block);
UVM_ASSERT(chunk->va_block_page_index < uvm_va_block_num_cpu_pages(va_block));
uvm_spin_lock(&pmm->list_lock);
chunk_unpin(pmm, chunk, UVM_PMM_GPU_CHUNK_STATE_ALLOCATED);
chunk_update_lists_locked(pmm, chunk);
uvm_spin_unlock(&pmm->list_lock);
}
void uvm_pmm_gpu_free(uvm_pmm_gpu_t *pmm, uvm_gpu_chunk_t *chunk, uvm_tracker_t *tracker)
{
NV_STATUS status;
// Referenced chunks are freed by Linux when the reference is released.
if (!chunk || chunk->is_referenced)
return;
UVM_ASSERT(chunk->state == UVM_PMM_GPU_CHUNK_STATE_ALLOCATED ||
chunk->state == UVM_PMM_GPU_CHUNK_STATE_TEMP_PINNED);
if (tracker) {
uvm_gpu_root_chunk_t *root_chunk;
uvm_tracker_remove_completed(tracker);
root_chunk = root_chunk_from_chunk(pmm, chunk);
root_chunk_lock(pmm, root_chunk);
// Remove any completed entries from the root tracker to prevent it from
// growing too much over time.
uvm_tracker_remove_completed(&root_chunk->tracker);
status = uvm_tracker_add_tracker_safe(&root_chunk->tracker, tracker);
if (status != NV_OK)
UVM_ASSERT(status == uvm_global_get_status());
root_chunk_unlock(pmm, root_chunk);
}
free_chunk(pmm, chunk);
}
static NvU32 num_subchunks(uvm_gpu_chunk_t *parent)
{
uvm_chunk_size_t parent_size, child_size;
UVM_ASSERT(parent->state == UVM_PMM_GPU_CHUNK_STATE_IS_SPLIT);
parent_size = uvm_gpu_chunk_get_size(parent);
child_size = uvm_gpu_chunk_get_size(parent->suballoc->subchunks[0]);
return (NvU32)uvm_div_pow2_64(parent_size, child_size);
}
static uvm_gpu_chunk_t *next_sibling(uvm_gpu_chunk_t *chunk)
{
uvm_gpu_chunk_t *parent = chunk->parent;
size_t index;
UVM_ASSERT(parent);
UVM_ASSERT(parent->state == UVM_PMM_GPU_CHUNK_STATE_IS_SPLIT);
index = (size_t)uvm_div_pow2_64(chunk->address - parent->address, uvm_gpu_chunk_get_size(chunk));
UVM_ASSERT(index < num_subchunks(parent));
++index;
if (index == num_subchunks(parent))
return NULL;
return parent->suballoc->subchunks[index];
}
// Check that the chunk is in a mergeable state: all children must be pinned or
// or all children must be allocated with the same reverse mapping.
//
// Always returns true so it can be called from an assert macro.
static bool assert_chunk_mergeable(uvm_pmm_gpu_t *pmm, uvm_gpu_chunk_t *chunk)
{
uvm_gpu_chunk_t *first_child = chunk->suballoc->subchunks[0];
uvm_va_block_t *child_va_block = first_child->va_block;
size_t i;
UVM_ASSERT(chunk->state == UVM_PMM_GPU_CHUNK_STATE_IS_SPLIT);
UVM_ASSERT_MSG(chunk->suballoc->allocated == num_subchunks(chunk),
"%u != %u\n",
chunk->suballoc->allocated,
num_subchunks(chunk));
UVM_ASSERT(first_child->state == UVM_PMM_GPU_CHUNK_STATE_TEMP_PINNED ||
first_child->state == UVM_PMM_GPU_CHUNK_STATE_ALLOCATED);
for (i = 1; i < num_subchunks(chunk); i++) {
uvm_gpu_chunk_t *child = chunk->suballoc->subchunks[i];
UVM_ASSERT(child->state == first_child->state);
if ((first_child->state == UVM_PMM_GPU_CHUNK_STATE_ALLOCATED) && uvm_gpu_chunk_is_user(first_child)) {
uvm_gpu_chunk_t *prev_child = chunk->suballoc->subchunks[i-1];
UVM_ASSERT(child->va_block == child_va_block);
UVM_ASSERT(child->va_block_page_index ==
prev_child->va_block_page_index + uvm_gpu_chunk_get_size(prev_child) / PAGE_SIZE);
UVM_ASSERT(child->is_referenced == prev_child->is_referenced);
}
}
return true;
}
// Merges a previously-split chunk. Assumes that all of its children have
// uniform state. This only merges leaves, so none of the children can be in the
// split state themselves.
//
// The children need to be removed from any lists before the merge.
//
// The merged chunk inherits the former state of its children.
static void merge_gpu_chunk(uvm_pmm_gpu_t *pmm, uvm_gpu_chunk_t *chunk)
{
uvm_pmm_gpu_chunk_suballoc_t *suballoc;
uvm_gpu_chunk_t *subchunk;
uvm_gpu_root_chunk_t *root_chunk = root_chunk_from_chunk(pmm, chunk);
uvm_pmm_gpu_chunk_state_t child_state;
size_t i, num_sub = num_subchunks(chunk);
uvm_assert_mutex_locked(&pmm->lock);
UVM_ASSERT(assert_chunk_mergeable(pmm, chunk));
// Transition the chunk state under the list lock first and then clean up
// the subchunk state.
uvm_spin_lock(&pmm->list_lock);
subchunk = chunk->suballoc->subchunks[0];
child_state = subchunk->state;
if ((child_state == UVM_PMM_GPU_CHUNK_STATE_ALLOCATED) && uvm_gpu_chunk_is_user(subchunk)) {
UVM_ASSERT(subchunk->va_block);
chunk->va_block = subchunk->va_block;
chunk->va_block_page_index = subchunk->va_block_page_index;
chunk->is_referenced = subchunk->is_referenced;
}
else if (child_state == UVM_PMM_GPU_CHUNK_STATE_TEMP_PINNED) {
UVM_ASSERT(root_chunk->chunk.suballoc->pinned_leaf_chunks >= num_sub);
root_chunk->chunk.suballoc->pinned_leaf_chunks += 1 - num_sub;
chunk->va_block = subchunk->va_block;
}
chunk->state = child_state;
suballoc = chunk->suballoc;
chunk->suballoc = NULL;
// The resulting chunk is assumed to be non-zero as a simplification,
// instead of checking that all the subchunks are zero, since callers of
// pmm_gpu_alloc are not required to clear it. However, we think that
// this covers all relevant cases since it is uncommon to split a chunk and
// not to use any of the subchunks later on.
chunk->is_zero = false;
uvm_spin_unlock(&pmm->list_lock);
for (i = 0; i < num_sub; i++) {
subchunk = suballoc->subchunks[i];
// The subchunks should have been removed from their lists prior to the
// merge.
UVM_ASSERT(list_empty(&subchunk->list));
if ((child_state == UVM_PMM_GPU_CHUNK_STATE_ALLOCATED) && uvm_gpu_chunk_is_user(subchunk))
UVM_ASSERT(subchunk->va_block);
kmem_cache_free(CHUNK_CACHE, subchunk);
}
kmem_cache_free(chunk_split_cache[ilog2(num_sub)].cache, suballoc);
}
// Checks that chunk is below ancestor in the tree. Always returns true so it
// can be called from an assert macro.
static bool assert_chunk_under(uvm_gpu_chunk_t *chunk, uvm_gpu_chunk_t *ancestor)
{
UVM_ASSERT(ancestor->state == UVM_PMM_GPU_CHUNK_STATE_IS_SPLIT);
UVM_ASSERT(ancestor->suballoc);
UVM_ASSERT(ancestor->address <= chunk->address);
UVM_ASSERT(chunk->address < ancestor->address + uvm_gpu_chunk_get_size(ancestor));
UVM_ASSERT(uvm_gpu_chunk_get_size(chunk) <= uvm_gpu_chunk_get_size(ancestor));
return true;
}
// Traverses the chunk tree from start in the given traversal order.
//
// If the callback returns a status value of NV_WARN_NOTHING_TO_DO when doing
// pre-order traversal, the traversal skips walking below that chunk. In all
// other cases, returning any non-NV_OK value stops the walk immediately and
// returns that status to the caller.
//
// Be careful modifying the tree from the callback. Changing the tree below the
// input chunk is fine and modifying the input chunk itself is fine, but the
// callback must not modify the tree above the input chunk. If that is needed,
// return a non-NV_OK status from the walk and re-start the walk.
static NV_STATUS chunk_walk(uvm_pmm_gpu_t *pmm,
uvm_gpu_chunk_t *start,
chunk_walk_func_t func,
void *data,
chunk_walk_order_t order)
{
NV_STATUS status = NV_OK;
uvm_gpu_chunk_t *curr, *sibling;
curr = start;
do {
if (curr != start)
UVM_ASSERT(assert_chunk_under(curr, start));
if (order == CHUNK_WALK_PRE_ORDER) {
status = func(pmm, curr, data);
if (status != NV_OK && status != NV_WARN_NOTHING_TO_DO)
return status;
}
// Skip downward traversal on pre-order if requested
if (status != NV_WARN_NOTHING_TO_DO && curr->state == UVM_PMM_GPU_CHUNK_STATE_IS_SPLIT) {
// If the chunk is split, walk down
curr = curr->suballoc->subchunks[0];
}
else {
// This is a leaf chunk. If not start itself, check siblings.
while (curr != start) {
if (order == CHUNK_WALK_POST_ORDER) {
status = func(pmm, curr, data);
if (status != NV_OK)
return status;
}
sibling = next_sibling(curr);
if (sibling) {
curr = sibling;
break;
}
// curr is the last chunk in its parent. Walk up and try again.
curr = curr->parent;
UVM_ASSERT(curr);
UVM_ASSERT(curr->state == UVM_PMM_GPU_CHUNK_STATE_IS_SPLIT);
}
}
} while (curr != start);
// Invoke the final callback for start
if (order == CHUNK_WALK_POST_ORDER)
return func(pmm, curr, data);
return NV_OK;
}
static NV_STATUS chunk_walk_pre_order(uvm_pmm_gpu_t *pmm, uvm_gpu_chunk_t *start, chunk_walk_func_t func, void *data)
{
return chunk_walk(pmm, start, func, data, CHUNK_WALK_PRE_ORDER);
}
static NV_STATUS chunk_walk_post_order(uvm_pmm_gpu_t *pmm, uvm_gpu_chunk_t *start, chunk_walk_func_t func, void *data)
{
return chunk_walk(pmm, start, func, data, CHUNK_WALK_POST_ORDER);
}
typedef struct
{
// Target size for the leaf subchunks
uvm_chunk_size_t min_size;
// Number of subchunks split to this point. If the subchunks array is non-
// NULL, this is the number of elements currently in the array.
size_t num_subchunks_curr;
// Number of subchunks needed for the whole split
size_t num_subchunks_total;
// Storage for the final split chunks. May be NULL.
uvm_gpu_chunk_t **subchunks;
// For testing
bool inject_error;
} split_walk_t;
static NV_STATUS split_walk_func(uvm_pmm_gpu_t *pmm, uvm_gpu_chunk_t *chunk, void *data)
{
uvm_chunk_size_t chunk_size, child_size;
uvm_chunk_sizes_mask_t chunk_sizes = pmm->chunk_sizes[chunk->type];
size_t i, num_children;
split_walk_t *args = data;
NV_STATUS status;
chunk_size = uvm_gpu_chunk_get_size(chunk);
UVM_ASSERT(chunk_size > args->min_size);
child_size = uvm_chunk_find_prev_size(chunk_sizes, chunk_size);
UVM_ASSERT(child_size != UVM_CHUNK_SIZE_INVALID);
num_children = chunk_size / child_size;
if (unlikely(args->inject_error)) {
// Inject errors on the last split. inject_split_error is a bitfield,
// so we must take the lock to modify it. This path is only used in
// testing.
if (child_size == args->min_size &&
args->num_subchunks_curr + num_children == args->num_subchunks_total) {
uvm_spin_lock(&pmm->list_lock);
chunk->inject_split_error = true;
uvm_spin_unlock(&pmm->list_lock);
}
}
status = split_gpu_chunk(pmm, chunk);
if (status != NV_OK)
return status;
// If we've hit our target, add all child subchunks to the array
if (child_size == args->min_size) {
for (i = 0; i < num_children; i++) {
UVM_ASSERT(args->num_subchunks_curr < args->num_subchunks_total);
if (args->subchunks)
args->subchunks[args->num_subchunks_curr] = chunk->suballoc->subchunks[i];
++args->num_subchunks_curr;
}
// No need to walk below this chunk
return NV_WARN_NOTHING_TO_DO;
}
return NV_OK;
}
static NV_STATUS merge_walk_func(uvm_pmm_gpu_t *pmm, uvm_gpu_chunk_t *chunk, void *data)
{
// The merge walk uses post-order traversal, so all subchunks are guaranteed
// to have already been merged.
if (chunk->state == UVM_PMM_GPU_CHUNK_STATE_IS_SPLIT)
merge_gpu_chunk(pmm, chunk);
return NV_OK;