-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmods_mem.c
More file actions
3221 lines (2544 loc) · 77.2 KB
/
mods_mem.c
File metadata and controls
3221 lines (2544 loc) · 77.2 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: GPL-2.0-only
/* SPDX-FileCopyrightText: Copyright (c) 2008-2025, NVIDIA CORPORATION. All rights reserved. */
#include "mods_internal.h"
#include <linux/bitops.h>
#include <linux/mutex.h>
#include <linux/pagemap.h>
#include <linux/sched.h>
#include <linux/vmalloc.h>
#if defined(MODS_HAS_SET_DMA_MASK)
#include <linux/dma-mapping.h>
#include <linux/of.h>
#endif
#ifdef CONFIG_ARM64
#include <linux/cache.h>
#endif
/* List which tracks unclaimed reservations */
LIST_HEAD(avail_mem_reservations);
DEFINE_MUTEX(mem_reservation_mtx);
static struct MODS_MEM_INFO *get_mem_handle(struct mods_client *client,
u64 handle)
{
/* For now just check if we hit first or last page, i.e. if
* we have a valid pointer. In the future, add proper handle
* accounting.
*/
if (unlikely((handle + PAGE_SIZE) < (2 * PAGE_SIZE))) {
cl_error("invalid memory handle 0x%llx\n", (unsigned long long)handle);
return NULL;
}
return (struct MODS_MEM_INFO *)(size_t)handle;
}
static bool validate_mem_handle(struct mods_client *client,
struct MODS_MEM_INFO *p_mem_info)
{
struct list_head *head = &client->mem_alloc_list;
struct list_head *iter;
if (unlikely(!p_mem_info))
return false;
list_for_each(iter, head) {
struct MODS_MEM_INFO *p_mem = list_entry(iter, struct MODS_MEM_INFO, list);
if (p_mem == p_mem_info)
return true;
}
return false;
}
static u64 get_client_mask(struct mods_client *client)
{
return (u64)1u << (client->client_id - 1);
}
/****************************
* DMA MAP HELPER FUNCTIONS *
****************************/
static void copy_wc_bitmap(struct MODS_MEM_INFO *p_dest_mem_info,
unsigned long first_dst_chunk,
struct MODS_MEM_INFO *p_src_mem_info,
unsigned long num_chunks)
{
unsigned long src_pos = 0;
WARN_ON(p_dest_mem_info->cache_type != p_src_mem_info->cache_type);
if (p_src_mem_info->cache_type == MODS_ALLOC_CACHED)
return;
WARN_ON(!p_dest_mem_info->wc_bitmap);
WARN_ON(!p_src_mem_info->wc_bitmap);
for (;;) {
src_pos = find_next_bit(p_src_mem_info->wc_bitmap,
num_chunks,
src_pos);
if (src_pos >= num_chunks)
break;
set_bit(src_pos + first_dst_chunk, p_dest_mem_info->wc_bitmap);
++src_pos;
}
}
static inline bool is_chunk_wc(struct MODS_MEM_INFO *p_mem_info, u32 ichunk)
{
return p_mem_info->wc_bitmap && test_bit(ichunk, p_mem_info->wc_bitmap);
}
static void mark_chunk_wc(struct MODS_MEM_INFO *p_mem_info, u32 ichunk)
{
WARN_ON(p_mem_info->cache_type == MODS_ALLOC_CACHED);
WARN_ON(!p_mem_info->wc_bitmap);
set_bit(ichunk, p_mem_info->wc_bitmap);
}
static void print_map_info(struct mods_client *client,
const char *action,
struct scatterlist *sg,
u32 nents,
struct device *dev)
{
u32 i;
for_each_sg(sg, sg, nents, i) {
cl_debug(DEBUG_MEM_DETAILED,
"dma %s iova=0x%llx dma_len=0x%x phys=0x%llx size=0x%x on dev %s\n",
action,
(unsigned long long)sg_dma_address(sg),
sg_dma_len(sg),
(unsigned long long)sg_phys(sg),
sg->length,
dev_name(dev));
}
}
static enum dma_data_direction get_dma_dir(struct MODS_MEM_INFO *p_mem_info)
{
return p_mem_info->device_ro ? DMA_TO_DEVICE : DMA_BIDIRECTIONAL;
}
#if defined(CONFIG_PCI) || defined(MODS_HAS_TEGRA)
static int map_sg(struct mods_client *client,
struct device *dev,
struct scatterlist *sg,
u32 num_chunks,
u32 num_pages,
enum dma_data_direction dir)
{
const u32 max_pages = (u32)(0x100000000ULL >> PAGE_SHIFT);
if (num_pages >= max_pages)
cl_warn("requested to map %u pages in %u chunks\n",
num_pages, num_chunks);
do {
u32 chunks_to_map = num_chunks;
u32 pages_to_map = num_pages;
int mapped;
/* Some HW IOMMU drivers can coalesce multiple chunks into
* a single contiguous VA mapping, which is exposed via the
* first chunk. However, dma_length field is unsigned int
* and not able to represent mappings which exceed 4GB.
* To alleviate it, split large allocations into multiple
* mappings.
*/
if (num_pages >= max_pages) {
struct scatterlist *cur_sg;
pages_to_map = 0;
for_each_sg(sg, cur_sg, num_chunks, chunks_to_map) {
const unsigned int len = cur_sg->length;
const u32 cur_pages = len >> PAGE_SHIFT;
if ((u64)pages_to_map + cur_pages >= max_pages)
break;
pages_to_map += cur_pages;
}
}
mapped = dma_map_sg(dev, sg, (int)chunks_to_map, dir);
if (mapped == 0) {
cl_error(
"failed to dma map %u chunks at 0x%llx to dev %s with dma mask 0x%llx\n",
num_chunks,
(unsigned long long)sg_phys(sg),
dev_name(dev),
(unsigned long long)dma_get_mask(dev));
return -EIO;
}
sg += chunks_to_map;
num_chunks -= chunks_to_map;
num_pages -= pages_to_map;
} while (num_chunks);
return OK;
}
#endif
static void unmap_sg(struct device *dev,
struct scatterlist *sg,
u32 num_chunks,
enum dma_data_direction dir)
{
do {
struct scatterlist *cur_sg;
u32 chunks_to_unmap = 0;
for_each_sg(sg, cur_sg, num_chunks, chunks_to_unmap)
if (!sg_dma_len(cur_sg))
break;
dma_unmap_sg(dev, sg, (int)chunks_to_unmap, dir);
sg_dma_address(sg) = 0;
sg += chunks_to_unmap;
num_chunks -= chunks_to_unmap;
/* Skip chunks which don't maintain any DMA mappings.
* This can happen for large allocations with the workaround
* in map_sg().
*/
if (num_chunks) {
for_each_sg(sg, sg, num_chunks, chunks_to_unmap)
if (sg_dma_len(sg))
break;
num_chunks -= chunks_to_unmap;
}
} while (num_chunks);
}
/* Unmap and delete the specified DMA mapping */
static void dma_unmap_and_free(struct mods_client *client,
struct MODS_MEM_INFO *p_mem_info,
struct MODS_DMA_MAP *p_del_map)
{
const u32 nents = get_num_chunks(p_mem_info);
print_map_info(client, "unmap", p_del_map->sg, nents, p_del_map->dev);
unmap_sg(p_del_map->dev, p_del_map->sg, nents, get_dma_dir(p_mem_info));
pci_dev_put(p_del_map->pcidev);
list_del(&p_del_map->list);
kfree(p_del_map);
atomic_dec(&client->num_allocs);
}
/* Unmap and delete all DMA mappings for the specified allocation */
static int dma_unmap_all(struct mods_client *client,
struct MODS_MEM_INFO *p_mem_info,
struct device *dev)
{
int err = OK;
struct list_head *head = &p_mem_info->dma_map_list;
struct list_head *iter;
struct list_head *tmp;
#ifdef CONFIG_PCI
if (sg_dma_address(p_mem_info->sg) && !p_mem_info->dma_pages &&
(dev == &p_mem_info->dev->dev || !dev)) {
unmap_sg(&p_mem_info->dev->dev,
p_mem_info->sg,
get_num_chunks(p_mem_info),
get_dma_dir(p_mem_info));
}
#endif
list_for_each_safe(iter, tmp, head) {
struct MODS_DMA_MAP *p_dma_map;
p_dma_map = list_entry(iter, struct MODS_DMA_MAP, list);
if (!dev || (p_dma_map->dev == dev)) {
dma_unmap_and_free(client, p_mem_info, p_dma_map);
if (dev)
break;
}
}
return err;
}
#if defined(CONFIG_PCI) || defined(MODS_HAS_TEGRA)
/* Create a DMA map on the specified allocation for the pci device.
* Lazy-initialize the map list structure if one does not yet exist.
*/
static int create_dma_map(struct mods_client *client,
struct MODS_MEM_INFO *p_mem_info,
struct pci_dev *pcidev,
struct device *dev)
{
struct MODS_DMA_MAP *p_dma_map;
struct scatterlist *sg;
const u32 num_chunks = get_num_chunks(p_mem_info);
size_t alloc_size;
u32 i;
int err;
if (unlikely(p_mem_info->shared && !p_mem_info->device_ro)) {
cl_error("unable to dma map shared reservation with tag 0x%llx as writable\n",
(unsigned long long)p_mem_info->reservation_tag);
return -EINVAL;
}
if (unlikely(!p_mem_info->shared && p_mem_info->reservation_tag)) {
cl_error("unable to dma map reserved memory with tag 0x%llx\n",
(unsigned long long)p_mem_info->reservation_tag);
return -EINVAL;
}
alloc_size = sizeof(struct MODS_DMA_MAP) +
num_chunks * sizeof(struct scatterlist);
p_dma_map = kzalloc(alloc_size, GFP_KERNEL | __GFP_NORETRY);
if (unlikely(!p_dma_map)) {
cl_error("failed to allocate device map data\n");
return -ENOMEM;
}
atomic_inc(&client->num_allocs);
#ifdef CONFIG_PCI
p_dma_map->pcidev = pcidev ? pci_dev_get(pcidev) : NULL;
#endif
p_dma_map->dev = dev;
sg_init_table(p_dma_map->sg, num_chunks);
for_each_sg(p_mem_info->sg, sg, num_chunks, i)
sg_set_page(&p_dma_map->sg[i], sg_page(sg), sg->length, 0);
err = map_sg(client, dev, p_dma_map->sg, num_chunks,
p_mem_info->num_pages, get_dma_dir(p_mem_info));
print_map_info(client, "map", p_dma_map->sg, num_chunks, dev);
if (unlikely(err)) {
pci_dev_put(pcidev);
kfree(p_dma_map);
atomic_dec(&client->num_allocs);
} else {
list_add(&p_dma_map->list, &p_mem_info->dma_map_list);
}
return err;
}
#endif
#ifdef CONFIG_PCI
/* DMA-map memory to the device for which it has been allocated, if it hasn't
* been mapped already.
*/
static int dma_map_to_default_dev(struct mods_client *client,
struct MODS_MEM_INFO *p_mem_info)
{
struct device *const dev = &p_mem_info->dev->dev;
const u32 num_chunks = get_num_chunks(p_mem_info);
int err;
if (unlikely(p_mem_info->reservation_tag)) {
cl_error("unable to dma map reserved memory with tag 0x%llx\n",
p_mem_info->reservation_tag);
return -EINVAL;
}
WARN_ON(p_mem_info->shared);
if (sg_dma_address(p_mem_info->sg)) {
cl_debug(DEBUG_MEM_DETAILED,
"memory %p already mapped to dev %s\n",
p_mem_info,
dev_name(dev));
return OK;
}
err = map_sg(client, dev, p_mem_info->sg, num_chunks,
p_mem_info->num_pages, get_dma_dir(p_mem_info));
print_map_info(client, "map default", p_mem_info->sg, num_chunks, dev);
return err;
}
#endif /* CONFIG_PCI */
#ifdef CONFIG_ARM64
static void clear_contiguous_cache(struct mods_client *client,
u64 virt_start,
u32 size);
static int setup_cache_attr_per_page(struct mods_client *client,
struct MODS_MEM_INFO *p_mem_info,
u32 i_chunk)
{
cl_error("highmem is not supported on ARM64\n");
return -EINVAL;
}
#else
static int setup_cache_attr_per_page(struct mods_client *client,
struct MODS_MEM_INFO *p_mem_info,
u32 ichunk)
{
struct scatterlist *sg = &p_mem_info->alloc_sg[ichunk];
unsigned int offs;
for (offs = 0; offs < sg->length; offs += PAGE_SIZE) {
void *const ptr = MODS_KMAP(sg_page(sg) + (offs >> PAGE_SHIFT));
int err;
if (unlikely(!ptr)) {
cl_error("kmap failed\n");
return -ENOMEM;
}
if (p_mem_info->cache_type == MODS_ALLOC_WRITECOMBINE)
err = MODS_SET_MEMORY_WC((unsigned long)ptr, 1);
else
err = MODS_SET_MEMORY_UC((unsigned long)ptr, 1);
MODS_KUNMAP(ptr);
if (unlikely(err)) {
cl_error("set cache type failed\n");
return err;
}
/* Set this flag early, so that when an error occurs,
* release_chunks() will restore cache attributes
* for all pages. It's OK to restore cache attributes
* even for chunks where we haven't change them.
*/
mark_chunk_wc(p_mem_info, ichunk);
/* Avoid superficial lockups */
cond_resched();
}
return 0;
}
#endif
static int setup_cache_attr(struct mods_client *client,
struct MODS_MEM_INFO *p_mem_info,
u32 ichunk)
{
struct scatterlist *sg;
void *ptr;
int err = 0;
if (p_mem_info->cache_type == MODS_ALLOC_CACHED || is_chunk_wc(p_mem_info, ichunk))
return 0;
sg = &p_mem_info->alloc_sg[ichunk];
/* For highmem pages, we need to map and process one page at a time,
* because the kernel addresses from kmap() for those pages are non-contiguous.
*/
if (unlikely(PageHighMem(sg_page(sg))))
return setup_cache_attr_per_page(client, p_mem_info, ichunk);
/* Typical fast path for modern CPUs, we assume all pages in the
* chunk are accessible in contiguous manner.
*/
ptr = MODS_KMAP(sg_page(sg));
if (unlikely(!ptr)) {
cl_error("kmap failed\n");
return -ENOMEM;
}
#ifdef CONFIG_ARM64
clear_contiguous_cache(client,
(u64)(size_t)ptr,
sg->length);
#else
if (p_mem_info->cache_type == MODS_ALLOC_WRITECOMBINE)
err = MODS_SET_MEMORY_WC((unsigned long)ptr, sg->length >> PAGE_SHIFT);
else
err = MODS_SET_MEMORY_UC((unsigned long)ptr, sg->length >> PAGE_SHIFT);
#endif
MODS_KUNMAP(ptr);
if (unlikely(err)) {
cl_error("set cache type failed\n");
return err;
}
mark_chunk_wc(p_mem_info, ichunk);
/* Avoid superficial lockups */
cond_resched();
return 0;
}
/* Find the dma mapping chunk for the specified memory. */
static struct MODS_DMA_MAP *find_dma_map(struct MODS_MEM_INFO *p_mem_info,
struct mods_pci_dev_2 *pcidev)
{
struct MODS_DMA_MAP *p_dma_map = NULL;
struct list_head *head = &p_mem_info->dma_map_list;
struct list_head *iter;
if (!head)
return NULL;
list_for_each(iter, head) {
p_dma_map = list_entry(iter, struct MODS_DMA_MAP, list);
if (mods_is_pci_dev(p_dma_map->pcidev, pcidev))
return p_dma_map;
}
return NULL;
}
static void free_mem_info(struct mods_client *client,
struct MODS_MEM_INFO *p_mem_info)
{
if (unlikely(p_mem_info->large_aux))
vfree(p_mem_info);
else
kfree(p_mem_info);
atomic_dec(&client->num_allocs);
}
/* In order to map pages as UC or WC to the CPU, we need to change their
* attributes by calling set_memory_uc()/set_memory_wc(), respectively.
* On some CPUs this operation is extremely slow. In order to incur
* this penalty only once, we save pages mapped as UC or WC so that
* we can reuse them later.
*/
static void save_non_wb_chunks(struct mods_client *client,
struct MODS_MEM_INFO *p_mem_info)
{
struct scatterlist *sg = NULL;
u32 ichunk;
if (p_mem_info->cache_type == MODS_ALLOC_CACHED)
return;
if (p_mem_info->no_free_opt)
return;
/* Steal the chunks from MODS_MEM_INFO and put them on free list. */
for_each_sg(p_mem_info->alloc_sg, sg, p_mem_info->num_chunks, ichunk) {
struct MODS_FREE_PHYS_CHUNK *free_chunk;
u32 order;
if (!sg)
break;
WARN_ON(!sg_page(sg));
if (!is_chunk_wc(p_mem_info, ichunk))
continue;
free_chunk = kzalloc(sizeof(struct MODS_FREE_PHYS_CHUNK),
GFP_KERNEL | __GFP_NORETRY);
if (unlikely(!free_chunk))
break;
atomic_inc(&client->num_allocs);
order = get_order(sg->length);
WARN_ON((PAGE_SIZE << order) != sg->length);
free_chunk->numa_node = p_mem_info->numa_node;
free_chunk->order = order;
free_chunk->cache_type = p_mem_info->cache_type;
free_chunk->dma32 = p_mem_info->dma32;
free_chunk->p_page = sg_page(sg);
sg_set_page(sg, NULL, 0, 0);
cl_debug(DEBUG_MEM_DETAILED,
"save %p 2^%u pages %s\n",
free_chunk->p_page,
order,
p_mem_info->cache_type == MODS_ALLOC_WRITECOMBINE
? "WC" : "UC");
list_add(&free_chunk->list, &client->free_mem_list);
}
}
static int restore_cache_one_chunk(struct page *p_page, u8 order)
{
const u32 num_pages = 1U << order;
int final_err = 0;
if (unlikely(PageHighMem(p_page))) {
u32 i;
for (i = 0; i < num_pages; i++) {
void *ptr = MODS_KMAP(p_page + i);
int err = -ENOMEM;
if (likely(ptr))
err = MODS_SET_MEMORY_WB((unsigned long)ptr, 1);
MODS_KUNMAP(ptr);
if (likely(!final_err))
final_err = err;
/* Avoid superficial lockups */
cond_resched();
}
} else {
/* Typical fast path for modern CPUs, we assume all pages in the
* chunk are accessible in contiguous manner.
*/
void *ptr = MODS_KMAP(p_page);
int err = -ENOMEM;
if (likely(ptr))
err = MODS_SET_MEMORY_WB((unsigned long)ptr, num_pages);
MODS_KUNMAP(ptr);
if (likely(!final_err))
final_err = err;
/* Avoid superficial lockups */
cond_resched();
}
return final_err;
}
static int release_free_chunks(struct mods_client *client)
{
struct list_head *head;
struct list_head *iter;
struct list_head *next;
unsigned long num_restored = 0;
unsigned long num_failed = 0;
unsigned long pages_freed = 0;
int final_err = 0;
head = &client->free_mem_list;
list_for_each_prev_safe(iter, next, head) {
struct MODS_FREE_PHYS_CHUNK *free_chunk;
int err;
free_chunk = list_entry(iter,
struct MODS_FREE_PHYS_CHUNK,
list);
list_del(iter);
err = restore_cache_one_chunk(free_chunk->p_page, free_chunk->order);
if (likely(!final_err))
final_err = err;
if (unlikely(err))
++num_failed;
else
++num_restored;
pages_freed += 1u << free_chunk->order;
__free_pages(free_chunk->p_page, free_chunk->order);
atomic_sub(1 << free_chunk->order, &client->num_pages);
kfree(free_chunk);
atomic_dec(&client->num_allocs);
}
if (pages_freed) {
cl_debug(DEBUG_MEM, "released %lu free WC/UC pages, restored cache on %lu free chunks\n",
pages_freed, num_restored);
if (unlikely(num_failed))
cl_error("failed to restore cache on %lu (out of %lu) free chunks\n",
num_failed, num_failed + num_restored);
}
return final_err;
}
static int restore_cache(struct mods_client *client,
struct MODS_MEM_INFO *p_mem_info)
{
struct scatterlist *sg;
unsigned int i;
int final_err = 0;
if (p_mem_info->cache_type == MODS_ALLOC_CACHED)
return 0;
for_each_sg(p_mem_info->alloc_sg, sg, p_mem_info->num_chunks, i) {
u32 order;
int err;
if (!sg)
break;
if (!sg_page(sg) || !is_chunk_wc(p_mem_info, i))
continue;
order = get_order(sg->length);
WARN_ON((PAGE_SIZE << order) != sg->length);
err = restore_cache_one_chunk(sg_page(sg), order);
if (likely(!final_err))
final_err = err;
}
if (unlikely(final_err))
cl_error("failed to restore cache attributes\n");
return final_err;
}
static void release_chunks(struct mods_client *client,
struct MODS_MEM_INFO *p_mem_info)
{
u32 i;
WARN_ON(!list_empty(&p_mem_info->dma_map_list));
WARN_ON(sg_dma_address(p_mem_info->sg) && !p_mem_info->dma_pages);
restore_cache(client, p_mem_info);
/* release in reverse order */
for (i = p_mem_info->num_chunks; i > 0; ) {
struct scatterlist *sg;
u32 order;
--i;
sg = &p_mem_info->alloc_sg[i];
if (!sg_page(sg))
continue;
order = get_order(sg->length);
WARN_ON((PAGE_SIZE << order) != sg->length);
if (p_mem_info->dma_pages) {
#ifdef MODS_HAS_DMA_ALLOC_PAGES
WARN_ON(!sg_dma_address(sg));
WARN_ON(!p_mem_info->dev);
dma_free_pages(&p_mem_info->dev->dev,
PAGE_SIZE << order,
sg_page(sg),
sg_dma_address(sg),
get_dma_dir(p_mem_info));
#endif
} else
__free_pages(sg_page(sg), order);
atomic_sub(1u << order, &client->num_pages);
sg_set_page(sg, NULL, 0, 0);
}
}
static gfp_t get_alloc_flags(struct MODS_MEM_INFO *p_mem_info, u32 order)
{
gfp_t flags = GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN;
if (p_mem_info->force_numa)
flags |= __GFP_THISNODE;
if (order)
flags |= __GFP_COMP;
if (p_mem_info->dma32)
#ifdef CONFIG_ZONE_DMA32
flags |= __GFP_DMA32;
#else
flags |= __GFP_DMA;
#endif
else
flags |= __GFP_HIGHMEM;
return flags;
}
static struct page *alloc_chunk(struct mods_client *client,
struct MODS_MEM_INFO *p_mem_info,
u32 order,
int *need_cup,
dma_addr_t *dma_handle)
{
struct page *p_page = NULL;
u8 cache_type = p_mem_info->cache_type;
u8 dma32 = p_mem_info->dma32;
int numa_node = p_mem_info->numa_node;
if ((cache_type != MODS_MEMORY_CACHED) &&
likely(!mutex_lock_interruptible(&client->mtx))) {
struct list_head *iter;
struct list_head *head = &client->free_mem_list;
struct MODS_FREE_PHYS_CHUNK *free_chunk = NULL;
list_for_each(iter, head) {
free_chunk = list_entry(iter,
struct MODS_FREE_PHYS_CHUNK,
list);
if (free_chunk->cache_type == cache_type &&
free_chunk->dma32 == dma32 &&
free_chunk->numa_node == numa_node &&
free_chunk->order == order) {
list_del(iter);
break;
}
free_chunk = NULL;
}
mutex_unlock(&client->mtx);
if (free_chunk) {
p_page = free_chunk->p_page;
kfree(free_chunk);
atomic_dec(&client->num_allocs);
cl_debug(DEBUG_MEM_DETAILED, "reuse %p 2^%u pages %s\n",
p_page, order,
cache_type == MODS_ALLOC_WRITECOMBINE
? "WC" : "UC");
*need_cup = 0;
return p_page;
}
}
if (p_mem_info->dma_pages) {
#ifdef MODS_HAS_DMA_ALLOC_PAGES
p_page = dma_alloc_pages(&p_mem_info->dev->dev,
PAGE_SIZE << order,
dma_handle,
get_dma_dir(p_mem_info),
GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN);
#endif
} else
p_page = alloc_pages_node(p_mem_info->numa_node,
get_alloc_flags(p_mem_info, order),
order);
*need_cup = 1;
if (likely(p_page))
atomic_add(1 << order, &client->num_pages);
return p_page;
}
static int alloc_contig_sys_pages(struct mods_client *client,
struct MODS_MEM_INFO *p_mem_info)
{
const unsigned long req_bytes = (unsigned long)p_mem_info->num_pages
<< PAGE_SHIFT;
struct page *p_page;
dma_addr_t dma_handle = 0;
u64 phys_addr;
u64 end_addr = 0;
u32 order = 0;
int is_wb = 1;
int err = -ENOMEM;
LOG_ENT();
while ((1U << order) < p_mem_info->num_pages)
order++;
p_page = alloc_chunk(client, p_mem_info, order, &is_wb, &dma_handle);
if (unlikely(!p_page))
goto failed;
p_mem_info->num_pages = 1U << order;
sg_set_page(p_mem_info->alloc_sg, p_page, PAGE_SIZE << order, 0);
sg_dma_address(p_mem_info->alloc_sg) = dma_handle;
if (!sg_dma_len(p_mem_info->alloc_sg))
sg_dma_len(p_mem_info->alloc_sg) = PAGE_SIZE << order;
if (!is_wb)
mark_chunk_wc(p_mem_info, 0);
phys_addr = sg_phys(p_mem_info->alloc_sg);
if (unlikely(phys_addr == 0)) {
cl_error("failed to determine physical address\n");
goto failed;
}
cl_debug(DEBUG_MEM,
"alloc contig 0x%lx bytes, 2^%u pages, %s, node %d,%s phys 0x%llx\n",
req_bytes,
order,
mods_get_prot_str(p_mem_info->cache_type),
p_mem_info->numa_node,
p_mem_info->dma32 ? " dma32," : "",
(unsigned long long)phys_addr);
end_addr = phys_addr +
((unsigned long)p_mem_info->num_pages << PAGE_SHIFT);
if (unlikely(p_mem_info->dma32 && (end_addr > 0x100000000ULL))) {
cl_error("allocation exceeds 32-bit addressing\n");
goto failed;
}
err = setup_cache_attr(client, p_mem_info, 0);
failed:
LOG_EXT();
return err;
}
static u32 get_max_order_needed(u32 num_pages)
{
const u32 order = min(10, get_order(num_pages << PAGE_SHIFT));
return ((1u << order) <= num_pages) ? order : (order >> 1u);
}
static int alloc_noncontig_sys_pages(struct mods_client *client,
struct MODS_MEM_INFO *p_mem_info)
{
const unsigned long req_bytes = (unsigned long)p_mem_info->num_pages
<< PAGE_SHIFT;
const u32 resched_every_pages = 1U << (30 - PAGE_SHIFT); /* Every 1GB */
u32 pages_needed = p_mem_info->num_pages;
u32 num_chunks = 0;
int err;
LOG_ENT();
p_mem_info->num_pages = 0;
for (; pages_needed > 0; ++num_chunks) {
struct scatterlist *sg = &p_mem_info->alloc_sg[num_chunks];
u64 phys_addr = 0;
u32 order = get_max_order_needed(pages_needed);
u32 allocated_pages = 0;
int is_wb = 1;
/* Avoid superficial lockups when allocating lots of memory */
if ((p_mem_info->num_pages > 0) && (p_mem_info->num_pages & (resched_every_pages - 1)) == 0)
cond_resched();
/* Fail if memory fragmentation is very high */
if (unlikely(num_chunks >= p_mem_info->num_chunks)) {
cl_error("detected high memory fragmentation\n");
err = -ENOMEM;
goto failed;
}
for (;;) {
dma_addr_t dma_handle = 0;
struct page *p_page = alloc_chunk(client,
p_mem_info,
order,
&is_wb,
&dma_handle);
if (p_page) {
sg_set_page(sg, p_page, PAGE_SIZE << order, 0);
sg_dma_address(sg) = dma_handle;
if (!sg_dma_len(sg))
sg_dma_len(sg) = PAGE_SIZE << order;
allocated_pages = 1u << order;
break;
}
if (order == 0)
break;
--order;
}
if (unlikely(!allocated_pages)) {
cl_error("out of memory\n");
err = -ENOMEM;
goto failed;
}
if (!is_wb)
mark_chunk_wc(p_mem_info, num_chunks);
pages_needed -= min(allocated_pages, pages_needed);
p_mem_info->num_pages += allocated_pages;
phys_addr = sg_phys(sg);
if (unlikely(phys_addr == 0)) {