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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
|
/*
* Copyright 2013 Red Hat Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Authors: Jérôme Glisse <jglisse@redhat.com>
*/
/*
* This is a dummy driver to exercice the HMM (heterogeneous memory management)
* mirror API of the kernel. Userspace program register with the dummy device
* to mirror their own address space and can use the device to read/write to
* any valid virtual address.
*
* In some way it can also serve as an example driver for people wanting to use
* HMM inside there own device driver.
*/
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/mutex.h>
#include <linux/rwsem.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/highmem.h>
#include <linux/delay.h>
#include <linux/pagemap.h>
#include <linux/hmm.h>
#include <linux/vmalloc.h>
#include <linux/swap.h>
#include <linux/swapops.h>
#include <linux/platform_device.h>
#include <uapi/linux/hmm_dmirror.h>
struct dmirror_device;
struct dummy_bounce {
void *ptr;
unsigned long size;
unsigned long addr;
unsigned long cpages;
};
#define DPT_SHIFT PAGE_SHIFT
#define DPT_VALID (1 << 0)
#define DPT_WRITE (1 << 1)
#define DPT_DPAGE (1 << 2)
struct dmirror_pt {
unsigned long pgd[PTRS_PER_PGD];
struct rw_semaphore lock;
};
struct dmirror {
struct dmirror_device *mdevice;
struct file *filp;
struct hmm_mirror mirror;
struct mm_struct *mm;
struct dmirror_pt pt;
};
struct dmirror_device {
dev_t dev;
struct cdev cdevice;
struct platform_device *pdevice;
struct hmm_device *hmm_device;
struct page *frees;
struct hmm_devmem devmem;
spinlock_t lock;
unsigned long calloc;
unsigned long cfree;
};
static inline unsigned long dmirror_pt_pgd(unsigned long addr)
{
return (addr >> PGDIR_SHIFT) & (PTRS_PER_PGD - 1);
}
static inline unsigned long dmirror_pt_pud(unsigned long addr)
{
return (addr >> PUD_SHIFT) & (PTRS_PER_PUD - 1);
}
static inline unsigned long dmirror_pt_pmd(unsigned long addr)
{
return (addr >> PMD_SHIFT) & (PTRS_PER_PMD - 1);
}
static inline unsigned long dmirror_pt_pte(unsigned long addr)
{
return (addr >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
}
static inline struct page *dmirror_pt_page(unsigned long dpte)
{
if (!(dpte & DPT_VALID))
return NULL;
return pfn_to_page(dpte >> DPT_SHIFT);
}
static inline unsigned long dmirror_pt_from_page(struct page *page)
{
if (!page)
return 0;
return (page_to_pfn(page) << DPT_SHIFT) | DPT_VALID;
}
static inline unsigned long dmirror_pt_pud_end(unsigned long addr)
{
return (addr & PGDIR_MASK) + ((long)PTRS_PER_PUD << PUD_SHIFT);
}
static inline unsigned long dmirror_pt_pmd_end(unsigned long addr)
{
return (addr & PUD_MASK) + ((long)PTRS_PER_PMD << PMD_SHIFT);
}
static inline unsigned long dmirror_pt_pte_end(unsigned long addr)
{
return (addr & PMD_MASK) + ((long)PTRS_PER_PTE << PAGE_SHIFT);
}
typedef int (*dmirror_walk_cb_t)(struct dmirror *dmirror,
unsigned long start,
unsigned long end,
unsigned long *dpte,
void *private);
static int dummy_pt_walk(struct dmirror *dmirror,
dmirror_walk_cb_t cb,
unsigned long start,
unsigned long end,
void *private,
bool populate)
{
unsigned long *dpgd = &dmirror->pt.pgd[dmirror_pt_pgd(start)];
unsigned long addr = start & PAGE_MASK;
BUG_ON(start == end);
for (; addr != end; dpgd++) {
unsigned long pud_end, *dpud;
struct page *pud_page;
pud_end = min(end, dmirror_pt_pud_end(addr));
pud_page = dmirror_pt_page(*dpgd);
if (!pud_page) {
if (!populate) {
addr = pud_end;
continue;
}
pud_page = alloc_page(GFP_HIGHUSER | __GFP_ZERO);
if (!pud_page) {
return -ENOMEM;
}
*dpgd = dmirror_pt_from_page(pud_page);
}
dpud = kmap(pud_page);
dpud = &dpud[dmirror_pt_pud(addr)];
for (; addr != pud_end; dpud++) {
unsigned long pmd_end, *dpmd;
struct page *pmd_page;
pmd_end = min(end, dmirror_pt_pmd_end(addr));
pmd_page = dmirror_pt_page(*dpud);
if (!pmd_page) {
if (!populate) {
addr = pmd_end;
continue;
}
pmd_page = alloc_page(GFP_HIGHUSER | __GFP_ZERO);
if (!pmd_page) {
kunmap(pud_page);
return -ENOMEM;
}
*dpud = dmirror_pt_from_page(pmd_page);
}
dpmd = kmap(pmd_page);
dpmd = &dpmd[dmirror_pt_pmd(addr)];
for (; addr != pmd_end; dpmd++) {
unsigned long *dpte, pte_end;
struct page *pte_page;
int ret;
pte_end = min(end, dmirror_pt_pte_end(addr));
pte_page = dmirror_pt_page(*dpmd);
if (!pte_page) {
if (!populate) {
addr = pte_end;
continue;
}
pte_page = alloc_page(GFP_HIGHUSER | __GFP_ZERO);
if (!pte_page) {
kunmap(pmd_page);
kunmap(pud_page);
return -ENOMEM;
}
*dpmd = dmirror_pt_from_page(pte_page);
}
dpte = kmap(pte_page);
dpte = &dpte[dmirror_pt_pte(addr)];
ret = cb(dmirror, addr, pte_end, dpte, private);
kunmap(pte_page);
addr = pte_end;
if (ret) {
kunmap(pmd_page);
kunmap(pud_page);
return ret;
}
}
kunmap(pmd_page);
addr = pmd_end;
}
kunmap(pud_page);
addr = pud_end;
}
return 0;
}
int dummy_bounce_init(struct dummy_bounce *bounce,
unsigned long size,
unsigned long addr)
{
bounce->addr = addr;
bounce->size = size;
bounce->ptr = vmalloc(size);
if (!bounce->ptr)
return -ENOMEM;
return 0;
}
int dummy_bounce_copy_from(struct dummy_bounce *bounce, unsigned long addr)
{
unsigned long end = (addr & PAGE_MASK) + bounce->size;
char __user *uptr = (void __user *)(addr & PAGE_MASK);
void *ptr = bounce->ptr;
for (; addr < end; addr += PAGE_SIZE, ptr += PAGE_SIZE, uptr += PAGE_SIZE) {
int ret;
ret = copy_from_user(ptr, uptr, PAGE_SIZE);
if (ret)
return ret;
}
return 0;
}
int dummy_bounce_copy_to(struct dummy_bounce *bounce, unsigned long addr)
{
unsigned long end = (addr & PAGE_MASK) + bounce->size;
char __user *uptr = (void __user *)(addr & PAGE_MASK);
void *ptr = bounce->ptr;
for (; addr < end; addr += PAGE_SIZE, ptr += PAGE_SIZE, uptr += PAGE_SIZE) {
int ret;
ret = copy_to_user(uptr, ptr, PAGE_SIZE);
if (ret)
return ret;
}
return 0;
}
void dummy_bounce_fini(struct dummy_bounce *bounce)
{
vfree(bounce->ptr);
}
static int dummy_do_update(struct dmirror *dmirror,
unsigned long addr,
unsigned long end,
unsigned long *dpte,
void *private)
{
for (; addr < end; addr += PAGE_SIZE, ++dpte) {
/* Clear pte */
*dpte = 0;
}
return 0;
}
static void dummy_update(struct hmm_mirror *mirror,
enum hmm_update update,
unsigned long start,
unsigned long end)
{
struct dmirror *dmirror = container_of(mirror, struct dmirror, mirror);
down_write(&dmirror->pt.lock);
dummy_pt_walk(dmirror, dummy_do_update, start, end, NULL, false);
up_write(&dmirror->pt.lock);
}
static const struct hmm_mirror_ops dmirror_ops = {
.update = &dummy_update,
};
static int dmirror_pt_init(struct dmirror *dmirror)
{
init_rwsem(&dmirror->pt.lock);
return 0;
}
/* dmirror_new() - allocate and initialize dummy mirror struct.
*
* @mdevice: The dummy device this mirror is associated with.
* @filp: The active device file descriptor this mirror is associated with.
*/
static struct dmirror *dmirror_new(struct dmirror_device *mdevice,
struct file *filp)
{
struct mm_struct *mm = get_task_mm(current);
struct dmirror *dmirror;
int r;
if (!mm)
return NULL;
/* Mirror this process address space */
dmirror = kzalloc(sizeof(*dmirror), GFP_KERNEL);
if (dmirror == NULL)
return NULL;
dmirror->mdevice = mdevice;
dmirror->filp = filp;
if (dmirror_pt_init(dmirror)) {
kfree(dmirror);
return NULL;
}
dmirror->mm = mm;
dmirror->mirror.ops = &dmirror_ops;
r = hmm_mirror_register(&dmirror->mirror, mm);
mmput(mm);
if (r) {
kfree(dmirror);
return NULL;
}
return dmirror;
}
static void dmirror_del(struct dmirror *dmirror)
{
hmm_mirror_unregister(&dmirror->mirror);
kfree(dmirror);
}
/*
* Below are the file operation for the dummy device file. Only ioctl matter.
*
* Note this is highly specific to the dummy device driver and should not be
* construed as an example on how to design the API a real device driver would
* expose to userspace.
*/
static ssize_t dummy_fops_read(struct file *filp,
char __user *buf,
size_t count,
loff_t *ppos)
{
return -EINVAL;
}
static ssize_t dummy_fops_write(struct file *filp,
const char __user *buf,
size_t count,
loff_t *ppos)
{
return -EINVAL;
}
static int dummy_fops_mmap(struct file *filp, struct vm_area_struct *vma)
{
/* Forbid mmap of the dummy device file. */
return -EINVAL;
}
static int dummy_fops_open(struct inode *inode, struct file *filp)
{
struct cdev *cdev = inode->i_cdev;
struct dmirror_device *mdevice;
struct dmirror *dmirror;
/* No exclusive opens. */
if (filp->f_flags & O_EXCL)
return -EINVAL;
mdevice = container_of(cdev, struct dmirror_device, cdevice);
dmirror = dmirror_new(mdevice, filp);
filp->private_data = dmirror;
return dmirror ? 0 : -ENOMEM;
}
static int dummy_fops_release(struct inode *inode, struct file *filp)
{
struct dmirror_device *mdevice;
struct dmirror *dmirror;
if (!filp->private_data)
return 0;
dmirror = filp->private_data;
mdevice = dmirror->mdevice;
printk(KERN_INFO "DEVICE PAGE %ld %ld (%ld)\n", mdevice->calloc, mdevice->cfree, mdevice->calloc - mdevice->cfree);
dmirror_del(dmirror);
filp->private_data = NULL;
return 0;
}
struct dummy_fault {
hmm_pfn_t *pfns;
unsigned long start;
unsigned long missing;
bool write;
bool invalid;
};
static int dummy_do_fault(struct dmirror *dmirror,
unsigned long addr,
unsigned long end,
unsigned long *dpte,
void *private)
{
struct dummy_fault *dfault = private;
unsigned long idx = (addr - dfault->start) >> PAGE_SHIFT;
hmm_pfn_t *pfns = dfault->pfns;
for (; addr < end; addr += PAGE_SIZE, ++dpte, ++idx) {
struct page *page;
/*
* Special pfn are device memory ie page inserted inside the
* CPU page table with either vm_insert_pfn or vm_insert_page
* in both case we assume that device can not access this
* memory safely.
*
* The HMM_PFN_ERROR is if it is accessing invalid memory
* either because of memory error (hardware detected memory
* corruption) or more likely because of truncate on mmap
* file.
*/
if ((pfns[idx] & (HMM_PFN_SPECIAL | HMM_PFN_ERROR))) {
dfault->invalid = true;
continue;
}
if (!(pfns[idx] & HMM_PFN_VALID)) {
dfault->missing++;
continue;
}
page = hmm_pfn_to_page(pfns[idx]);
*dpte = dmirror_pt_from_page(page);
if (pfns[idx] & HMM_PFN_WRITE) {
*dpte |= DPT_WRITE;
} else if (dfault->write) {
dfault->missing++;
}
}
return 0;
}
static int dummy_fault(struct dmirror *dmirror,
unsigned long start,
unsigned long end,
bool write)
{
struct mm_struct *mm = dmirror->mm;
unsigned long addr = start;
do {
struct vm_area_struct *vma;
struct dummy_fault dfault;
struct hmm_range range;
unsigned long next, i;
hmm_pfn_t pfns[256];
int ret;
down_read(&mm->mmap_sem);
next = min(addr + (256 << PAGE_SHIFT), end);
vma = find_vma_intersection(mm, addr, end);
if (!vma) {
up_read(&mm->mmap_sem);
return -EFAULT;
}
if (!(vma->vm_flags & VM_READ)) {
up_read(&mm->mmap_sem);
return -EFAULT;
}
if (write && !(vma->vm_flags & VM_WRITE)) {
up_read(&mm->mmap_sem);
return -EFAULT;
}
addr = max(vma->vm_start, addr);
next = min(vma->vm_end, next);
for (i = 0; i < ((next - addr) >> PAGE_SHIFT); i++) {
pfns[i] = HMM_PFN_FAULT;
pfns[i] |= write ? HMM_PFN_WRITE : 0;
}
hmm_vma_range_monitor_start(&range, vma, addr, next, true);
if (!hmm_vma_fault(vma, addr, next, pfns))
continue;
down_read(&dmirror->pt.lock);
if (!hmm_vma_range_monitor_end(&range)) {
up_read(&dmirror->pt.lock);
up_read(&mm->mmap_sem);
continue;
}
dfault.invalid = false;
dfault.missing = 0;
dfault.start = addr;
dfault.pfns = pfns;
ret = dummy_pt_walk(dmirror, dummy_do_fault,
addr, next, &dfault, true);
up_read(&dmirror->pt.lock);
up_read(&mm->mmap_sem);
if (ret)
return ret;
if (dfault.invalid)
return -EFAULT;
if (!dfault.missing) {
addr = next;
} else
return -EFAULT;
} while (addr != end);
return 0;
}
static int dummy_do_prefetch(struct dmirror *dmirror,
unsigned long addr,
unsigned long end,
unsigned long *dpte,
void *private)
{
struct dummy_fault *dfault = private;
unsigned long idx = (addr - dfault->start) >> PAGE_SHIFT;
hmm_pfn_t *pfns = dfault->pfns;
for (; addr < end; addr += PAGE_SIZE, ++dpte, ++idx) {
struct page *page;
/*
* Special pfn are device memory ie page inserted inside the
* CPU page table with either vm_insert_pfn or vm_insert_page
* in both case we assume that device can not access this
* memory safely.
*
* The HMM_PFN_ERROR is if it is accessing invalid memory
* either because of memory error (hardware detected memory
* corruption) or more likely because of truncate on mmap
* file.
*/
if ((pfns[idx] & (HMM_PFN_SPECIAL | HMM_PFN_ERROR)))
continue;
if (!(pfns[idx] & HMM_PFN_VALID))
continue;
page = hmm_pfn_to_page(pfns[idx]);
*dpte = dmirror_pt_from_page(page);
if (pfns[idx] & HMM_PFN_WRITE)
*dpte |= DPT_WRITE;
}
return 0;
}
static void dummy_prefetch(struct dmirror *dmirror,
unsigned long start,
unsigned long end)
{
struct mm_struct *mm = dmirror->mm;
unsigned long addr = start;
do {
struct vm_area_struct *vma;
struct dummy_fault dfault;
struct hmm_range range;
unsigned long next;
hmm_pfn_t pfns[64];
down_read(&mm->mmap_sem);
next = min(addr + (64 << PAGE_SHIFT), end);
vma = find_vma_intersection(mm, addr, end);
if (!vma) {
up_read(&mm->mmap_sem);
addr = next;
continue;
}
if (!(vma->vm_flags & VM_READ)) {
up_read(&mm->mmap_sem);
addr = next;
continue;
}
addr = max(vma->vm_start, addr);
next = min(vma->vm_end, next);
hmm_vma_range_lock(&range, vma, addr, next);
hmm_vma_get_pfns(vma, addr, next, pfns);
down_read(&dmirror->pt.lock);
dfault.start = addr;
dfault.pfns = pfns;
dummy_pt_walk(dmirror, dummy_do_prefetch,
addr, next, &dfault, true);
up_read(&dmirror->pt.lock);
hmm_vma_range_unlock(&range);
up_read(&mm->mmap_sem);
addr = next;
} while (addr != end);
}
static bool dummy_device_is_mine(struct dmirror_device *mdevice,
struct page *page)
{
if (!is_zone_device_page(page))
return false;
return page->pgmap->data == &mdevice->devmem;
}
static int dummy_do_read(struct dmirror *dmirror,
unsigned long addr,
unsigned long end,
unsigned long *dpte,
void *private)
{
struct dmirror_device *mdevice = dmirror->mdevice;
struct dummy_bounce *bounce = private;
void *ptr;
ptr = bounce->ptr + ((addr - bounce->addr) & PAGE_MASK);
for (; addr < end; addr += PAGE_SIZE, ++dpte) {
struct page *page;
void *tmp;
page = dmirror_pt_page(*dpte);
if (!page) {
return -ENOENT;
}
if (is_zone_device_page(page)) {
if (!dummy_device_is_mine(mdevice, page))
return -ENOENT;
page = (void *)hmm_devmem_page_get_drvdata(page);
}
tmp = kmap(page);
memcpy(ptr, tmp, PAGE_SIZE);
kunmap(page);
ptr += PAGE_SIZE;
bounce->cpages++;
}
return 0;
}
static int dummy_read(struct dmirror *dmirror,
struct hmm_dmirror_read *dread)
{
struct dummy_bounce bounce;
unsigned long start, end;
unsigned long size;
int ret;
if ((dread->ptr & (~PAGE_MASK)) || (dread->addr & (~PAGE_MASK)))
return -EINVAL;
if (dread->addr >= (dread->addr + (dread->npages << PAGE_SHIFT)))
return -EINVAL;
start = dread->addr & PAGE_MASK;
size = (dread->npages << PAGE_SHIFT);
end = start + (dread->npages << PAGE_SHIFT);
ret = dummy_bounce_init(&bounce, size, start);
if (ret)
return ret;
dummy_prefetch(dmirror, start, end);
again:
dread->dpages = 0;
bounce.cpages = 0;
down_read(&dmirror->pt.lock);
ret = dummy_pt_walk(dmirror, dummy_do_read,
start, end, &bounce, true);
up_read(&dmirror->pt.lock);
if (ret == -ENOENT) {
ret = dummy_fault(dmirror, start, end, false);
if (ret) {
dummy_bounce_fini(&bounce);
return ret;
}
goto again;
}
ret = dummy_bounce_copy_to(&bounce, dread->ptr);
dread->cpages = bounce.cpages;
dummy_bounce_fini(&bounce);
return ret;
}
static int dummy_do_write(struct dmirror *dmirror,
unsigned long addr,
unsigned long end,
unsigned long *dpte,
void *private)
{
struct dmirror_device *mdevice = dmirror->mdevice;
struct dummy_bounce *bounce = private;
void *ptr;
ptr = bounce->ptr + ((addr - bounce->addr) & PAGE_MASK);
for (; addr < end; addr += PAGE_SIZE, ++dpte) {
struct page *page;
void *tmp;
page = dmirror_pt_page(*dpte);
if (!page || !(*dpte & DPT_WRITE))
return -ENOENT;
if (is_zone_device_page(page)) {
if (!dummy_device_is_mine(mdevice, page))
return -ENOENT;
page = (void *)hmm_devmem_page_get_drvdata(page);
}
tmp = kmap(page);
memcpy(tmp, ptr, PAGE_SIZE);
kunmap(page);
ptr += PAGE_SIZE;
bounce->cpages++;
}
return 0;
}
static int dummy_write(struct dmirror *dmirror,
struct hmm_dmirror_write *dwrite)
{
struct dummy_bounce bounce;
unsigned long start, end;
unsigned long size;
int ret;
if ((dwrite->ptr & (~PAGE_MASK)) || (dwrite->addr & (~PAGE_MASK)))
return -EINVAL;
if (dwrite->addr >= (dwrite->addr + (dwrite->npages << PAGE_SHIFT)))
return -EINVAL;
start = dwrite->addr & PAGE_MASK;
size = (dwrite->npages << PAGE_SHIFT);
end = start + (dwrite->npages << PAGE_SHIFT);
ret = dummy_bounce_init(&bounce, size, dwrite->addr & PAGE_MASK);
if (ret)
return ret;
ret = dummy_bounce_copy_from(&bounce, dwrite->ptr);
if (ret)
return ret;
dummy_prefetch(dmirror, start, end);
again:
bounce.cpages = 0;
dwrite->dpages = 0;
down_read(&dmirror->pt.lock);
ret = dummy_pt_walk(dmirror, dummy_do_write,
start, end, &bounce, true);
up_read(&dmirror->pt.lock);
if (ret == -ENOENT) {
ret = dummy_fault(dmirror, start, end, true);
if (ret) {
dummy_bounce_fini(&bounce);
return ret;
}
goto again;
}
dwrite->cpages = bounce.cpages;
dummy_bounce_fini(&bounce);
return 0;
}
static struct page *dummy_device_alloc_page(struct dmirror_device *mdevice)
{
struct page *dpage = NULL, *rpage;
/*
* This is a fake device so we alloc real system memory to fake
* our device memory
*/
rpage = alloc_page(GFP_HIGHUSER);
if (!rpage)
return NULL;
spin_lock(&mdevice->lock);
if (mdevice->frees) {
dpage = mdevice->frees;
mdevice->frees = dpage->s_mem;
} else {
spin_unlock(&mdevice->lock);
__free_page(rpage);
return NULL;
}
if (!trylock_page(dpage)) {
dpage->s_mem = mdevice->frees;
mdevice->frees = dpage;
spin_unlock(&mdevice->lock);
__free_page(rpage);
return NULL;
}
spin_unlock(&mdevice->lock);
mdevice->calloc++;
hmm_devmem_page_set_drvdata(dpage, (unsigned long)rpage);
get_page(dpage);
return dpage;
}
struct dummy_migrate {
struct dmirror_device *mdevice;
struct hmm_dmirror_migrate *dmigrate;
};
static void dummy_migrate_alloc_and_copy(struct vm_area_struct *vma,
unsigned long start,
unsigned long end,
hmm_pfn_t *pfns,
void *private)
{
struct dummy_migrate *dmigrate = private;
struct dmirror_device *mdevice;
unsigned long addr;
if (!dmigrate)
return;
mdevice = dmigrate->mdevice;
for (addr = start; addr < end; addr += PAGE_SIZE, pfns++) {
struct page *spage = hmm_pfn_to_page(*pfns);
struct page *dpage, *rpage;
if (!spage || !(*pfns & HMM_PFN_MIGRATE))
continue;
if (*pfns & HMM_PFN_DEVICE) {
if (!dummy_device_is_mine(mdevice, spage)) {
*pfns = 0;
continue;
}
spage = (void *)hmm_devmem_page_get_drvdata(spage);
}
dpage = dummy_device_alloc_page(mdevice);
if (!dpage)
continue;
rpage = (void *)hmm_devmem_page_get_drvdata(dpage);
copy_highpage(rpage, spage);
*pfns = hmm_pfn_from_page(dpage) |
HMM_PFN_MIGRATE | HMM_PFN_LOCKED;
}
}
static void dummy_migrate_finalize_and_map(struct vm_area_struct *vma,
unsigned long start,
unsigned long end,
hmm_pfn_t *pfns,
void *private)
{
struct dummy_migrate *dmigrate = private;
unsigned long addr;
if (!dmigrate || !dmigrate->dmigrate)
return;
for (addr = start; addr < end; addr += PAGE_SIZE, pfns++) {
struct page *page = hmm_pfn_to_page(*pfns);
if (!(*pfns & HMM_PFN_MIGRATE))
continue;
if (!dummy_device_is_mine(dmigrate->mdevice, page))
continue;
dmigrate->dmigrate->npages++;
}
}
static const struct hmm_migrate_ops dmirror_migrate_ops = {
.alloc_and_copy = dummy_migrate_alloc_and_copy,
.finalize_and_map = dummy_migrate_finalize_and_map,
};
static int dummy_migrate(struct dmirror *dmirror,
struct hmm_dmirror_migrate *dmigrate)
{
unsigned long addr = dmigrate->addr, end;
struct mm_struct *mm = dmirror->mm;
struct vm_area_struct *vma;
struct dummy_migrate tmp;
int ret;
tmp.mdevice = dmirror->mdevice;
tmp.dmigrate = dmigrate;
down_read(&mm->mmap_sem);
end = addr + (dmigrate->npages << PAGE_SHIFT);
vma = find_vma_intersection(mm, addr, end);
if (!vma || vma->vm_start > addr || vma->vm_end < end) {
ret = -EINVAL;
goto out;
}
for (dmigrate->npages = 0; addr < end;) {
unsigned long next;
hmm_pfn_t pfns[64];
next = min(end, addr + (64 << PAGE_SHIFT));
ret = hmm_vma_migrate(&dmirror_migrate_ops, vma,
addr, next, pfns, &tmp);
if (ret)
goto out;
addr = next;
}
out:
up_read(&mm->mmap_sem);
return ret;
}
static long dummy_fops_unlocked_ioctl(struct file *filp,
unsigned int command,
unsigned long arg)
{
void __user *uarg = (void __user *)arg;
struct hmm_dmirror_migrate dmigrate;
struct hmm_dmirror_write dwrite;
struct hmm_dmirror_read dread;
struct dmirror *dmirror;
int ret;
dmirror = filp->private_data;
if (!dmirror)
return -EINVAL;
switch (command) {
case HMM_DMIRROR_READ:
ret = copy_from_user(&dread, uarg, sizeof(dread));
if (ret)
return ret;
ret = dummy_read(dmirror, &dread);
if (ret)
return ret;
return copy_to_user(uarg, &dread, sizeof(dread));
case HMM_DMIRROR_WRITE:
ret = copy_from_user(&dwrite, uarg, sizeof(dwrite));
if (ret)
return ret;
ret = dummy_write(dmirror, &dwrite);
if (ret)
return ret;
return copy_to_user(uarg, &dwrite, sizeof(dwrite));
case HMM_DMIRROR_MIGRATE:
ret = copy_from_user(&dmigrate, uarg, sizeof(dmigrate));
if (ret)
return ret;
ret = dummy_migrate(dmirror, &dmigrate);
if (ret)
return ret;
return copy_to_user(uarg, &dmigrate, sizeof(dmigrate));
default:
ret = -EINVAL;
break;
}
return ret;
}
static const struct file_operations dmirror_fops = {
.read = dummy_fops_read,
.write = dummy_fops_write,
.mmap = dummy_fops_mmap,
.open = dummy_fops_open,
.release = dummy_fops_release,
.unlocked_ioctl = dummy_fops_unlocked_ioctl,
.llseek = default_llseek,
.owner = THIS_MODULE,
};
static void dummy_devmem_free(struct hmm_devmem *devmem,
struct page *page)
{
struct dmirror_device *mdevice;
struct page *rpage;
mdevice = container_of(devmem, struct dmirror_device, devmem);
rpage = (struct page *)hmm_devmem_page_get_drvdata(page);
__free_page(rpage);
mdevice->cfree++;
spin_lock(&mdevice->lock);
page->s_mem = mdevice->frees;
mdevice->frees = page;
spin_unlock(&mdevice->lock);
}
struct dummy_devmem_fault {
struct dmirror_device *mdevice;
};
static void dummy_devmem_fault_alloc_and_copy(struct vm_area_struct *vma,
unsigned long start,
unsigned long end,
hmm_pfn_t *pfns,
void *private)
{
struct dummy_devmem_fault *fault = private;
unsigned long addr;
for (addr = start; addr < end; addr += PAGE_SIZE, pfns++) {
struct page *dpage, *spage;
spage = hmm_pfn_to_page(*pfns);
if (!spage || !(*pfns & HMM_PFN_MIGRATE))
continue;
if (!dummy_device_is_mine(fault->mdevice, spage)) {
*pfns = HMM_PFN_ERROR;
continue;
}
spage = (void *)hmm_devmem_page_get_drvdata(spage);
dpage = hmm_vma_alloc_locked_page(vma, addr);
if (!dpage) {
*pfns = HMM_PFN_ERROR;
continue;
}
copy_highpage(dpage, spage);
*pfns = hmm_pfn_from_page(dpage) |
HMM_PFN_MIGRATE |
HMM_PFN_LOCKED;
}
}
void dummy_devmem_fault_finalize_and_map(struct vm_area_struct *vma,
unsigned long start,
unsigned long end,
hmm_pfn_t *pfns,
void *private)
{
}
static const struct hmm_migrate_ops dummy_devmem_migrate = {
.alloc_and_copy = dummy_devmem_fault_alloc_and_copy,
.finalize_and_map = dummy_devmem_fault_finalize_and_map,
};
static int dummy_devmem_fault(struct hmm_devmem *devmem,
struct vm_area_struct *vma,
unsigned long addr,
struct page *page,
unsigned flags,
pmd_t *pmdp)
{
hmm_pfn_t pfns = HMM_PFN_MIGRATE;
struct dummy_devmem_fault fault;
unsigned long start, end;
fault.mdevice = container_of(devmem, struct dmirror_device, devmem);
/* FIXME demonstrate how we can adjust migrate range */
start = addr;
end = addr + PAGE_SIZE;
return hmm_devmem_fault_range(devmem, vma, &dummy_devmem_migrate,
start, addr, end, &pfns, &fault);
}
static const struct hmm_devmem_ops dmirror_devmem_ops = {
.free = dummy_devmem_free,
.fault = dummy_devmem_fault,
};
static int dmirror_probe(struct platform_device *pdev)
{
struct dmirror_device *mdevice = platform_get_drvdata(pdev);
struct hmm_devmem *devmem = &mdevice->devmem;
unsigned long pfn;
int ret;
mdevice->hmm_device = hmm_device_new();
if (IS_ERR(mdevice->hmm_device))
return PTR_ERR(mdevice->hmm_device);
ret = hmm_devmem_add(&mdevice->devmem, &dmirror_devmem_ops,
&mdevice->hmm_device->device, 64 << 20);
if (ret) {
hmm_device_put(mdevice->hmm_device);
return ret;
}
ret = alloc_chrdev_region(&mdevice->dev, 0, 1, "HMM_DMIRROR");
if (ret < 0) {
hmm_devmem_remove(&mdevice->devmem);
hmm_device_put(mdevice->hmm_device);
return ret;
}
cdev_init(&mdevice->cdevice, &dmirror_fops);
ret = cdev_add(&mdevice->cdevice, mdevice->dev, 1);
if (ret) {
unregister_chrdev_region(mdevice->dev, 1);
hmm_devmem_remove(&mdevice->devmem);
hmm_device_put(mdevice->hmm_device);
return ret;
}
/* Build list of free struct page */
spin_lock_init(&mdevice->lock);
mdevice->frees = NULL;
for (pfn = devmem->pfn_first; pfn < devmem->pfn_last; pfn++) {
struct page *page = pfn_to_page(pfn);
page->s_mem = mdevice->frees;
mdevice->frees = page;
}
mdevice->calloc = 0;
mdevice->cfree = 0;
return 0;
}
static int dmirror_remove(struct platform_device *pdev)
{
struct dmirror_device *mdevice = platform_get_drvdata(pdev);
if (!hmm_devmem_remove(&mdevice->devmem))
printk(KERN_INFO "HMM device memory still in use !\n");
hmm_device_put(mdevice->hmm_device);
cdev_del(&mdevice->cdevice);
unregister_chrdev_region(mdevice->dev, 1);
return 0;
}
static struct platform_device *dmirror_platform_device;
static struct platform_driver dmirror_device_driver = {
.probe = dmirror_probe,
.remove = dmirror_remove,
.driver = {
.name = "HMM_DMIRROR",
},
};
static int __init hmm_dmirror_init(void)
{
struct dmirror_device *mdevice;
int ret;
mdevice = kzalloc(sizeof(*mdevice), GFP_KERNEL);
if (!mdevice)
return -ENOMEM;
dmirror_platform_device = platform_device_alloc("HMM_DMIRROR", -1);
if (!dmirror_platform_device) {
kfree(mdevice);
return -ENOMEM;
}
platform_set_drvdata(dmirror_platform_device, mdevice);
mdevice->pdevice = dmirror_platform_device;
ret = platform_device_add(dmirror_platform_device);
if (ret < 0) {
platform_device_put(dmirror_platform_device);
return ret;
}
ret = platform_driver_register(&dmirror_device_driver);
if (ret < 0) {
platform_device_unregister(dmirror_platform_device);
return ret;
}
pr_info("hmm_dmirror loaded THIS IS A DANGEROUS MODULE !!!\n");
return 0;
}
static void __exit hmm_dmirror_exit(void)
{
struct dmirror_device *mdevice;
mdevice = platform_get_drvdata(dmirror_platform_device);
platform_driver_unregister(&dmirror_device_driver);
platform_device_unregister(dmirror_platform_device);
kfree(mdevice);
}
module_init(hmm_dmirror_init);
module_exit(hmm_dmirror_exit);
MODULE_LICENSE("GPL");
|