Repository navigation
Expand file tree
/
Copy pathbuddy.c
More file actions
2201 lines (1806 loc) · 55.4 KB
/
Copy pathbuddy.c
File metadata and controls
2201 lines (1806 loc) · 55.4 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
// SPDX-License-Identifier: MIT
/*
* Copyright © 2021 Intel Corporation
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/bug.h>
#include <linux/export.h>
#include <linux/kmemleak.h>
#include <linux/module.h>
#include <linux/sizes.h>
#include <linux/slab.h>
#include <linux/gpu_buddy.h>
/**
* gpu_buddy_assert - assert a condition in the buddy allocator
* @condition: condition expected to be true
*
* When CONFIG_KUNIT is enabled, evaluates @condition and, if false, triggers
* a WARN_ON() and also calls kunit_fail_current_test() so that any running
* kunit test is properly marked as failed. The stringified condition is
* included in the failure message for easy identification.
*
* When CONFIG_KUNIT is not enabled, this reduces to WARN_ON() so production
* builds retain the same warning semantics as before.
*/
#if IS_ENABLED(CONFIG_KUNIT)
#include <kunit/test-bug.h>
#define gpu_buddy_assert(condition) do { \
if (WARN_ON(!(condition))) \
kunit_fail_current_test("gpu_buddy_assert(" #condition ")"); \
} while (0)
#else
#define gpu_buddy_assert(condition) WARN_ON(!(condition))
#endif
static struct kmem_cache *slab_blocks;
static struct kmem_cache *slab_extents;
/*
* Dirty tracker
* -------------
*
* The dirty tracker maintains an augmented interval rbtree of contiguous
* dirty address ranges, decoupled from the buddy free trees.
* Each node covers a maximal coalesced run; adjacent extents are merged
* on insertion so the tree always holds the smallest possible number of
* extents. The augmentation field @subtree_max_size lets the allocator
* locate the largest dirty extent in O(log E).
*
* Free trees (mm->free_tree[])
* ----------------------------
*
* Per-order augmented rbtrees of FREE buddy blocks, keyed by offset.
* Every node carries:
* - subtree_max_alignment: largest natural alignment in the subtree,
* used by aligned/range allocations to skip unsuitable subtrees in
* O(log N).
* - subtree_block_state: the highest clear class (DIRTY < MIXED < CLEAR)
* of any block in the subtree, maintained as a max augment. A value of
* >= MIXED means a clear-or-mixed block exists; == CLEAR means a
* fully-clear block exists.
*
* Block classes
* -------------
*
* Each FREE block falls into one of three classes, determined in
* mark_free() by querying the dirty tracker for the block's range:
*
* clear -- HEADER_CLEAR set; no dirty extent overlaps the range.
* mixed -- HEADER_CLEAR unset; range has both dirty and clear bytes.
* dirty -- HEADER_CLEAR unset; range is fully dirty.
*
* Clear allocation
* ----------------
*
* A clear (CLEAR_ALLOCATION) request prefers clear -> mixed -> dirty.
* Climbing from the requested order up to max_order, rbtree_last_clear_free_block()
* returns, in one O(log N) descent per order, the right-most clear-or-mixed block
* (fully-clear preferred over mixed) at the lowest order whose free tree contains
* such a block. Only if no clear-or-mixed block exists at any order >= the
* requested one does it fall back to a dirty block.
*
* Clear state is reported to the driver per whole block via HEADER_CLEAR, so a
* fully-clear block of the requested order lets the driver skip the clear pass.
*
* The effective allocation preference depends on how the driver handles
* freed blocks:
*
* 1) Never clear on free:
* No free block contains clear bytes, so clear allocations always
* fall back to dirty blocks.
*
* 2) Always clear on free:
* Freed blocks become clear while untouched blocks remain dirty.
* Merging clear and dirty buddies produces mixed blocks, which are
* reclassified when split. Over time, clear blocks become dominant,
* so clear allocations are typically satisfied from clear blocks,
* following a clear -> mixed -> dirty preference.
*
* 3) Selective clear on free:
* For each order examined, fully-clear blocks are preferred over
* mixed blocks, and mixed blocks are preferred over dirty blocks.
* If a clear or mixed block is found at an order, it is selected
* without searching higher orders. Dirty blocks are used only when
* no clear or mixed block exists at any eligible order.
*/
static u64 extent_size(struct gpu_dirty_extent *dirty_extent)
{
return dirty_extent->end - dirty_extent->start;
}
RB_DECLARE_CALLBACKS_MAX(static, gpu_dirty_augment_cb,
struct gpu_dirty_extent, rb,
u64, subtree_max_size,
extent_size)
static struct gpu_dirty_extent *extent_alloc(struct gpu_dirty_tracker *dirty_tracker)
{
return kmem_cache_alloc(slab_extents, GFP_KERNEL);
}
static void extent_free(struct gpu_dirty_tracker *dirty_tracker,
struct gpu_dirty_extent *dirty_extent)
{
kmem_cache_free(slab_extents, dirty_extent);
}
/* Return the rightmost extent whose start is strictly below @offset. */
static struct gpu_dirty_extent *
prev_extent(struct gpu_dirty_tracker *dirty_tracker, u64 offset)
{
struct rb_node *rb = dirty_tracker->root.rb_node;
struct gpu_dirty_extent *dirty_extent = NULL;
while (rb) {
struct gpu_dirty_extent *tmp_extent =
rb_entry(rb, struct gpu_dirty_extent, rb);
if (tmp_extent->start < offset) {
dirty_extent = tmp_extent;
rb = rb->rb_right;
} else {
rb = rb->rb_left;
}
}
return dirty_extent;
}
/* Return the leftmost extent whose start is at or above @offset. */
static struct gpu_dirty_extent *
next_extent(struct gpu_dirty_tracker *dirty_tracker, u64 offset)
{
struct rb_node *rb = dirty_tracker->root.rb_node;
struct gpu_dirty_extent *dirty_extent = NULL;
while (rb) {
struct gpu_dirty_extent *tmp_extent =
rb_entry(rb, struct gpu_dirty_extent, rb);
if (tmp_extent->start >= offset) {
dirty_extent = tmp_extent;
rb = rb->rb_left;
} else {
rb = rb->rb_right;
}
}
return dirty_extent;
}
static void insert_extent(struct gpu_dirty_tracker *dirty_tracker,
struct gpu_dirty_extent *dirty_extent)
{
struct rb_node **link = &dirty_tracker->root.rb_node;
struct rb_node *parent = NULL;
u64 size = extent_size(dirty_extent);
while (*link) {
struct gpu_dirty_extent *tmp_extent;
parent = *link;
tmp_extent = rb_entry(parent, struct gpu_dirty_extent, rb);
if (tmp_extent->subtree_max_size < size)
tmp_extent->subtree_max_size = size;
if (dirty_extent->start < tmp_extent->start)
link = &parent->rb_left;
else
link = &parent->rb_right;
}
dirty_extent->subtree_max_size = size;
rb_link_node(&dirty_extent->rb, parent, link);
rb_insert_augmented(&dirty_extent->rb, &dirty_tracker->root, &gpu_dirty_augment_cb);
}
static void remove_extent(struct gpu_dirty_tracker *dirty_tracker,
struct gpu_dirty_extent *dirty_extent)
{
rb_erase_augmented(&dirty_extent->rb, &dirty_tracker->root, &gpu_dirty_augment_cb);
RB_CLEAR_NODE(&dirty_extent->rb);
}
static void gpu_dirty_tracker_init(struct gpu_dirty_tracker *dirty_tracker)
{
dirty_tracker->root = RB_ROOT;
dirty_tracker->total_dirty = 0;
}
static void gpu_dirty_tracker_empty(struct gpu_dirty_tracker *dirty_tracker)
{
struct rb_node *rb;
while ((rb = rb_first(&dirty_tracker->root))) {
struct gpu_dirty_extent *dirty_extent =
rb_entry(rb, struct gpu_dirty_extent, rb);
remove_extent(dirty_tracker, dirty_extent);
extent_free(dirty_tracker, dirty_extent);
}
dirty_tracker->total_dirty = 0;
}
static void gpu_dirty_tracker_fini(struct gpu_dirty_tracker *dirty_tracker)
{
gpu_dirty_tracker_empty(dirty_tracker);
}
/*
* Mark the range [start, start + size] as dirty. Merge with the neighbour on
* each side if they are contiguous, so the tree never holds two adjacent ranges.
*/
static void gpu_dirty_tracker_mark_dirty(struct gpu_dirty_tracker *dirty_tracker,
u64 start, u64 size)
{
struct gpu_dirty_extent *left, *right, *dirty_extent;
u64 end = start + size;
gpu_buddy_assert(size);
/* Find contiguous neighbours, if any. */
left = prev_extent(dirty_tracker, start);
if (left && left->end != start)
left = NULL;
right = next_extent(dirty_tracker, end);
if (right && right->start != end)
right = NULL;
if (left && right) {
/* Merge left + new + right into a single extent. */
remove_extent(dirty_tracker, left);
remove_extent(dirty_tracker, right);
left->end = right->end;
extent_free(dirty_tracker, right);
insert_extent(dirty_tracker, left);
} else if (left) {
/* Extend left neighbour rightwards. */
remove_extent(dirty_tracker, left);
left->end = end;
insert_extent(dirty_tracker, left);
} else if (right) {
/* Extend right neighbour leftwards. */
remove_extent(dirty_tracker, right);
right->start = start;
insert_extent(dirty_tracker, right);
} else {
/* Standalone extent. */
dirty_extent = extent_alloc(dirty_tracker);
if (!dirty_extent) {
pr_warn_once("dirty extent allocation failed, skipping tracker update\n");
return;
}
dirty_extent->start = start;
dirty_extent->end = end;
insert_extent(dirty_tracker, dirty_extent);
}
dirty_tracker->total_dirty += size;
}
/*
* Remove the range [start, start + size] from the dirty tracker. Punch the
* range out of every overlapping dirty extent, splitting one extent in two if
* the removed range falls strictly inside it.
*/
static void gpu_dirty_tracker_remove_range(struct gpu_dirty_tracker *dirty_tracker,
u64 start, u64 size)
{
struct gpu_dirty_extent *dirty_extent, *next;
u64 end = start + size;
gpu_buddy_assert(size);
dirty_extent = prev_extent(dirty_tracker, start + 1);
if (!dirty_extent)
dirty_extent = next_extent(dirty_tracker, start);
while (dirty_extent && dirty_extent->start < end) {
struct rb_node *next_node = rb_next(&dirty_extent->rb);
u64 extent_start = dirty_extent->start;
u64 extent_end = dirty_extent->end;
if (next_node)
next = rb_entry(next_node, struct gpu_dirty_extent, rb);
else
next = NULL;
/* Skip a non-overlapping neighbour returned by prev_extent(). */
if (extent_end <= start) {
dirty_extent = next;
continue;
}
if (extent_start < start && extent_end > end) {
/*
* Removed range lies strictly inside this dirty extent:
* split it into the dirty left and right halves.
*/
struct gpu_dirty_extent *right = extent_alloc(dirty_tracker);
if (!right) {
pr_warn_once("dirty extent allocation failed, skipping tracker update\n");
dirty_extent = next;
continue;
}
remove_extent(dirty_tracker, dirty_extent);
dirty_extent->end = start;
right->start = end;
right->end = extent_end;
insert_extent(dirty_tracker, dirty_extent);
insert_extent(dirty_tracker, right);
dirty_tracker->total_dirty -= size;
} else if (extent_start >= start && extent_end <= end) {
/* Extent fully covered: drop it. */
remove_extent(dirty_tracker, dirty_extent);
extent_free(dirty_tracker, dirty_extent);
dirty_tracker->total_dirty -= (extent_end - extent_start);
} else if (extent_start < start) {
/* Extent overlaps from the left: trim its right end. */
remove_extent(dirty_tracker, dirty_extent);
dirty_extent->end = start;
insert_extent(dirty_tracker, dirty_extent);
dirty_tracker->total_dirty -= (extent_end - start);
} else {
/* Extent overlaps from the right: trim its left end. */
remove_extent(dirty_tracker, dirty_extent);
dirty_extent->start = end;
insert_extent(dirty_tracker, dirty_extent);
dirty_tracker->total_dirty -= (end - extent_start);
}
dirty_extent = next;
}
}
static enum gpu_block_state
gpu_dirty_range_state(struct gpu_dirty_tracker *dirty_tracker,
u64 start, u64 size)
{
struct gpu_dirty_extent *dirty_extent;
u64 end = start + size;
dirty_extent = prev_extent(dirty_tracker, start + 1);
if (dirty_extent) {
if (dirty_extent->start <= start && dirty_extent->end >= end)
return GPU_BLOCK_DIRTY;
if (dirty_extent->start < end && dirty_extent->end > start)
return GPU_BLOCK_MIXED;
}
dirty_extent = next_extent(dirty_tracker, start);
if (dirty_extent && dirty_extent->start < end)
return GPU_BLOCK_MIXED;
return GPU_BLOCK_CLEAR;
}
static struct rb_node *
dirty_tracker_descend_right(struct rb_node *node, u64 min_size)
{
while (node->rb_right) {
struct gpu_dirty_extent *tmp_extent;
tmp_extent = rb_entry(node->rb_right, struct gpu_dirty_extent, rb);
if (tmp_extent->subtree_max_size < min_size)
break;
node = node->rb_right;
}
return node;
}
static struct gpu_dirty_extent *
gpu_dirty_tracker_find(struct gpu_dirty_tracker *dirty_tracker,
u64 min_size, u64 *aligned_start_out)
{
struct rb_node *rb = dirty_tracker->root.rb_node;
struct gpu_dirty_extent *root_extent;
struct rb_node *parent;
if (!min_size || !is_power_of_2(min_size))
return NULL;
if (!rb)
return NULL;
root_extent = rb_entry(rb, struct gpu_dirty_extent, rb);
if (root_extent->subtree_max_size < min_size)
return NULL;
rb = dirty_tracker_descend_right(rb, min_size);
while (rb) {
struct gpu_dirty_extent *dirty_extent;
u64 aligned_start;
dirty_extent = rb_entry(rb, struct gpu_dirty_extent, rb);
aligned_start = ALIGN(dirty_extent->start, min_size);
/* Check if a min_size block fits after the alignment skip. */
if (aligned_start <= dirty_extent->end &&
dirty_extent->end - aligned_start >= min_size) {
*aligned_start_out = aligned_start;
return dirty_extent;
}
if (rb->rb_left) {
struct gpu_dirty_extent *tmp_extent;
tmp_extent = rb_entry(rb->rb_left, struct gpu_dirty_extent, rb);
if (tmp_extent->subtree_max_size >= min_size) {
rb = dirty_tracker_descend_right(rb->rb_left, min_size);
continue;
}
}
/* Walk up until we exit a node via its right child. */
parent = rb_parent(rb);
while (parent && parent->rb_right != rb) {
rb = parent;
parent = rb_parent(rb);
}
rb = parent;
}
return NULL;
}
static unsigned int
gpu_buddy_block_state(struct gpu_buddy_block *block)
{
return block->header & GPU_BUDDY_HEADER_STATE;
}
static bool
gpu_buddy_block_is_allocated(struct gpu_buddy_block *block)
{
return gpu_buddy_block_state(block) == GPU_BUDDY_ALLOCATED;
}
static bool
gpu_buddy_block_is_split(struct gpu_buddy_block *block)
{
return gpu_buddy_block_state(block) == GPU_BUDDY_SPLIT;
}
static unsigned int gpu_buddy_block_offset_alignment(struct gpu_buddy_block *block)
{
u64 offset = gpu_buddy_block_offset(block);
if (!offset)
/*
* __ffs64(0) is undefined; offset 0 is maximally aligned, so return
* a value greater than any possible alignment.
*/
return 64 + 1;
return __ffs64(offset);
}
static inline enum gpu_block_state
gpu_block_cached_state(struct gpu_buddy_block *block)
{
if (gpu_buddy_block_is_clear(block))
return GPU_BLOCK_CLEAR;
if (block->has_clear)
return GPU_BLOCK_MIXED;
return GPU_BLOCK_DIRTY;
}
static inline void gpu_buddy_augment_compute(struct gpu_buddy_block *block)
{
enum gpu_block_state block_state;
struct gpu_buddy_block *right;
struct gpu_buddy_block *left;
unsigned int max_align;
max_align = gpu_buddy_block_offset_alignment(block);
block_state = gpu_block_cached_state(block);
left = rb_entry_safe(block->rb.rb_left, struct gpu_buddy_block, rb);
if (left) {
if (left->subtree_max_alignment > max_align)
max_align = left->subtree_max_alignment;
block_state = max(block_state, left->subtree_block_state);
}
right = rb_entry_safe(block->rb.rb_right, struct gpu_buddy_block, rb);
if (right) {
if (right->subtree_max_alignment > max_align)
max_align = right->subtree_max_alignment;
block_state = max(block_state, right->subtree_block_state);
}
block->subtree_max_alignment = max_align;
block->subtree_block_state = block_state;
}
static void gpu_buddy_augment_propagate(struct rb_node *rb, struct rb_node *stop)
{
while (rb != stop) {
struct gpu_buddy_block *block;
unsigned int old_align;
enum gpu_block_state old_block_state;
block = rb_entry(rb, struct gpu_buddy_block, rb);
old_align = block->subtree_max_alignment;
old_block_state = block->subtree_block_state;
gpu_buddy_augment_compute(block);
if (block->subtree_max_alignment == old_align &&
block->subtree_block_state == old_block_state)
break;
rb = rb_parent(&block->rb);
}
}
static void gpu_buddy_augment_copy(struct rb_node *rb_old, struct rb_node *rb_new)
{
struct gpu_buddy_block *old;
struct gpu_buddy_block *new;
old = rb_entry(rb_old, struct gpu_buddy_block, rb);
new = rb_entry(rb_new, struct gpu_buddy_block, rb);
new->subtree_max_alignment = old->subtree_max_alignment;
new->subtree_block_state = old->subtree_block_state;
}
static void gpu_buddy_augment_rotate(struct rb_node *rb_old, struct rb_node *rb_new)
{
struct gpu_buddy_block *old;
struct gpu_buddy_block *new;
old = rb_entry(rb_old, struct gpu_buddy_block, rb);
new = rb_entry(rb_new, struct gpu_buddy_block, rb);
new->subtree_max_alignment = old->subtree_max_alignment;
new->subtree_block_state = old->subtree_block_state;
gpu_buddy_augment_compute(old);
}
static const struct rb_augment_callbacks gpu_buddy_augment_cb = {
.propagate = gpu_buddy_augment_propagate,
.copy = gpu_buddy_augment_copy,
.rotate = gpu_buddy_augment_rotate,
};
static struct gpu_buddy_block *gpu_block_alloc(struct gpu_buddy *mm,
struct gpu_buddy_block *parent,
unsigned int order,
u64 offset)
{
struct gpu_buddy_block *block;
BUG_ON(order > GPU_BUDDY_MAX_ORDER);
block = kmem_cache_zalloc(slab_blocks, GFP_KERNEL);
if (!block)
return NULL;
block->header = offset;
block->header |= order;
block->parent = parent;
RB_CLEAR_NODE(&block->rb);
BUG_ON(block->header & GPU_BUDDY_HEADER_UNUSED);
return block;
}
static void gpu_block_free(struct gpu_buddy *mm,
struct gpu_buddy_block *block)
{
kmem_cache_free(slab_blocks, block);
}
static struct gpu_buddy_block *
rbtree_get_free_block(const struct rb_node *node)
{
return node ? rb_entry(node, struct gpu_buddy_block, rb) : NULL;
}
static struct gpu_buddy_block *
rbtree_last_free_block(struct rb_root *root)
{
return rbtree_get_free_block(rb_last(root));
}
static struct gpu_buddy_block *
rbtree_last_clear_free_block(struct rb_root *root,
enum gpu_block_state min_block_state)
{
struct rb_node *node = root->rb_node;
struct gpu_buddy_block *block = NULL;
struct gpu_buddy_block *root_block;
enum gpu_block_state target_state;
root_block = rbtree_get_free_block(node);
if (!root_block || root_block->subtree_block_state < min_block_state)
return NULL;
target_state = root_block->subtree_block_state;
while (node) {
struct gpu_buddy_block *right_block;
struct gpu_buddy_block *node_block;
node_block = rbtree_get_free_block(node);
right_block = rbtree_get_free_block(node->rb_right);
if (right_block && right_block->subtree_block_state >= target_state) {
node = node->rb_right;
continue;
}
if (gpu_block_cached_state(node_block) == target_state) {
block = node_block;
break;
}
node = node->rb_left;
}
return block;
}
static void rbtree_insert(struct gpu_buddy *mm,
struct gpu_buddy_block *block)
{
struct rb_node **link, *parent = NULL;
enum gpu_block_state block_state;
struct gpu_buddy_block *node;
unsigned int block_alignment;
struct rb_root *root;
unsigned int order;
order = gpu_buddy_block_order(block);
block_alignment = gpu_buddy_block_offset_alignment(block);
block_state = gpu_block_cached_state(block);
root = &mm->free_tree[order];
link = &root->rb_node;
while (*link) {
parent = *link;
node = rbtree_get_free_block(parent);
/*
* Manual augmentation update during insertion traversal. Required
* because rb_insert_augmented() only calls rotate callback during
* rotations. This ensures all ancestors on the insertion path have
* correct subtree_max_alignment / subtree_block_state values.
*/
if (node->subtree_max_alignment < block_alignment)
node->subtree_max_alignment = block_alignment;
if (node->subtree_block_state < block_state)
node->subtree_block_state = block_state;
if (gpu_buddy_block_offset(block) < gpu_buddy_block_offset(node))
link = &parent->rb_left;
else
link = &parent->rb_right;
}
block->subtree_max_alignment = block_alignment;
block->subtree_block_state = block_state;
rb_link_node(&block->rb, parent, link);
rb_insert_augmented(&block->rb, root, &gpu_buddy_augment_cb);
}
static void rbtree_remove(struct gpu_buddy *mm,
struct gpu_buddy_block *block)
{
unsigned int order = gpu_buddy_block_order(block);
rb_erase_augmented(&block->rb, &mm->free_tree[order], &gpu_buddy_augment_cb);
RB_CLEAR_NODE(&block->rb);
}
static void mark_allocated(struct gpu_buddy *mm,
struct gpu_buddy_block *block)
{
block->header &= ~GPU_BUDDY_HEADER_STATE;
block->header |= GPU_BUDDY_ALLOCATED;
block->has_clear = false;
mm->free_scoreboard[gpu_buddy_block_order(block)]--;
mm->used_scoreboard[gpu_buddy_block_order(block)]++;
rbtree_remove(mm, block);
}
static void __mark_free(struct gpu_buddy *mm,
struct gpu_buddy_block *block,
enum gpu_block_state block_state)
{
if (gpu_buddy_block_is_allocated(block))
mm->used_scoreboard[gpu_buddy_block_order(block)]--;
block->header &= ~GPU_BUDDY_HEADER_STATE;
block->header |= GPU_BUDDY_FREE;
block->header &= ~GPU_BUDDY_HEADER_CLEAR;
block->has_clear = (block_state != GPU_BLOCK_DIRTY);
if (block_state == GPU_BLOCK_CLEAR)
block->header |= GPU_BUDDY_HEADER_CLEAR;
mm->free_scoreboard[gpu_buddy_block_order(block)]++;
rbtree_insert(mm, block);
}
static void mark_free(struct gpu_buddy *mm,
struct gpu_buddy_block *block)
{
enum gpu_block_state block_state;
block_state = gpu_dirty_range_state(&mm->dirty,
gpu_buddy_block_offset(block),
gpu_buddy_block_size(mm, block));
__mark_free(mm, block, block_state);
}
static void mark_split(struct gpu_buddy *mm,
struct gpu_buddy_block *block)
{
block->header &= ~GPU_BUDDY_HEADER_STATE;
block->header |= GPU_BUDDY_SPLIT;
mm->free_scoreboard[gpu_buddy_block_order(block)]--;
rbtree_remove(mm, block);
}
static inline bool overlaps(u64 s1, u64 e1, u64 s2, u64 e2)
{
return s1 <= e2 && e1 >= s2;
}
static inline bool contains(u64 s1, u64 e1, u64 s2, u64 e2)
{
return s1 <= s2 && e1 >= e2;
}
static struct gpu_buddy_block *
__get_buddy(struct gpu_buddy_block *block)
{
struct gpu_buddy_block *parent;
parent = block->parent;
if (!parent)
return NULL;
if (parent->left == block)
return parent->right;
return parent->left;
}
static unsigned int __gpu_buddy_free(struct gpu_buddy *mm,
struct gpu_buddy_block *block)
{
enum gpu_block_state block_state;
struct gpu_buddy_block *parent;
unsigned int order;
block_state = gpu_block_cached_state(block);
while ((parent = block->parent)) {
struct gpu_buddy_block *buddy = __get_buddy(block);
if (!gpu_buddy_block_is_free(buddy))
break;
if (block_state != GPU_BLOCK_MIXED) {
enum gpu_block_state buddy_state;
buddy_state = gpu_block_cached_state(buddy);
if (buddy_state != block_state)
block_state = GPU_BLOCK_MIXED;
}
rbtree_remove(mm, buddy);
mm->free_scoreboard[gpu_buddy_block_order(buddy)]--;
if (gpu_buddy_block_is_allocated(block))
mm->used_scoreboard[gpu_buddy_block_order(block)]--;
gpu_block_free(mm, block);
gpu_block_free(mm, buddy);
block = parent;
}
order = gpu_buddy_block_order(block);
__mark_free(mm, block, block_state);
return order;
}
/**
* gpu_buddy_init - init memory manager
*
* @mm: GPU buddy manager to initialize
* @size: size in bytes to manage
* @chunk_size: minimum page size in bytes for our allocations
*
* Initializes the memory manager and its resources.
*
* Returns:
* 0 on success, error code on failure.
*/
int gpu_buddy_init(struct gpu_buddy *mm, u64 size, u64 chunk_size)
{
unsigned int root_count = 0;
u64 offset = 0;
if (size < chunk_size)
return -EINVAL;
if (chunk_size < SZ_4K)
return -EINVAL;
if (!is_power_of_2(chunk_size))
return -EINVAL;
size = round_down(size, chunk_size);
mm->size = size;
mm->avail = size;
mm->chunk_size = chunk_size;
mm->max_order = ilog2(size) - ilog2(chunk_size);
BUG_ON(mm->max_order > GPU_BUDDY_MAX_ORDER);
mm->free_scoreboard = kzalloc_objs(*mm->free_scoreboard,
mm->max_order + 1);
if (!mm->free_scoreboard)
return -ENOMEM;
mm->used_scoreboard = kzalloc_objs(*mm->used_scoreboard,
mm->max_order + 1);
if (!mm->used_scoreboard)
goto out_free_free_scoreboard;
mm->free_tree = kzalloc_objs(struct rb_root, mm->max_order + 1);
if (!mm->free_tree)
goto out_free_used_scoreboard;
gpu_dirty_tracker_init(&mm->dirty);
mm->n_roots = hweight64(size);
mm->roots = kmalloc_objs(struct gpu_buddy_block *, mm->n_roots);
if (!mm->roots)
goto out_free_tree;
/*
* Split into power-of-two blocks, in case we are given a size that is
* not itself a power-of-two.
*/
do {
struct gpu_buddy_block *root;
unsigned int order;
u64 root_size;
order = ilog2(size) - ilog2(chunk_size);
root_size = chunk_size << order;
root = gpu_block_alloc(mm, NULL, order, offset);
if (!root)
goto out_free_roots;
gpu_dirty_tracker_mark_dirty(&mm->dirty, offset, root_size);
__mark_free(mm, root, GPU_BLOCK_DIRTY);
BUG_ON(root_count > mm->max_order);
BUG_ON(gpu_buddy_block_size(mm, root) < chunk_size);
mm->roots[root_count] = root;
offset += root_size;
size -= root_size;
root_count++;
} while (size);
#ifdef CONFIG_LOCKDEP
mm->lock_dep_map = NULL;
#endif
return 0;
out_free_roots:
while (root_count--)
gpu_block_free(mm, mm->roots[root_count]);
kfree(mm->roots);
out_free_tree:
gpu_dirty_tracker_fini(&mm->dirty);
kfree(mm->free_tree);
out_free_used_scoreboard:
kfree(mm->used_scoreboard);
out_free_free_scoreboard:
kfree(mm->free_scoreboard);
return -ENOMEM;
}
EXPORT_SYMBOL(gpu_buddy_init);
/**
* gpu_buddy_fini - tear down the memory manager
*
* @mm: GPU buddy manager to free
*
* Cleanup memory manager resources and the freetree
*/
void gpu_buddy_fini(struct gpu_buddy *mm)
{
u64 root_size, size;
unsigned int order;
int i;
size = mm->size;
for (i = 0; i < mm->n_roots; ++i) {
order = ilog2(size) - ilog2(mm->chunk_size);
root_size = mm->chunk_size << order;
gpu_buddy_assert(gpu_buddy_block_is_free(mm->roots[i]));
gpu_block_free(mm, mm->roots[i]);
size -= root_size;
}
gpu_buddy_assert(mm->avail == mm->size);
for (i = 0; i <= mm->max_order; ++i)
gpu_buddy_assert(!mm->used_scoreboard[i]);
gpu_dirty_tracker_fini(&mm->dirty);
kfree(mm->free_tree);
kfree(mm->roots);
kfree(mm->free_scoreboard);
kfree(mm->used_scoreboard);
}
EXPORT_SYMBOL(gpu_buddy_fini);
static int split_block(struct gpu_buddy *mm,
struct gpu_buddy_block *block)
{
unsigned int block_order = gpu_buddy_block_order(block) - 1;
u64 offset = gpu_buddy_block_offset(block);
enum gpu_block_state parent_state;
BUG_ON(!gpu_buddy_block_is_free(block));
BUG_ON(!gpu_buddy_block_order(block));
block->left = gpu_block_alloc(mm, block, block_order, offset);
if (!block->left)
return -ENOMEM;