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
|
#include <linux/slab.h>
#include <linux/mutex.h>
#include <linux/list.h>
#include <linux/mm.h>
#include <linux/dma-mapping.h>
#include <linux/iommu.h>
#include <linux/vmalloc.h>
#include <linux/highmem.h>
#include <linux/pfn.h>
#include <linux/scatterlist.h>
#include "nvidia-vgpu-vfio.h"
#include "memory.h"
struct nv_vgpu_simple_mapping {
u64 gpa;
u64 size;
u64 iova;
struct page **pages;
int page_count;
bool pinned;
bool mapped;
struct list_head list;
};
static LIST_HEAD(simple_mappings);
static DEFINE_MUTEX(simple_lock);
int nv_vgpu_memory_init(struct nv_vgpu_device *vgpu)
{
struct nv_vgpu_mm_ctx *ctx;
if (vgpu->dry_run) {
nv_vgpu_info(vgpu, "DRY-RUN: Memory init validation (simple fallback)");
return 0;
}
ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
if (!ctx)
return -ENOMEM;
ctx->vgpu = vgpu;
mutex_init(&ctx->lock);
INIT_LIST_HEAD(&ctx->mappings);
ctx->mapping_count = 0;
ctx->iommu_domain = NULL;
ctx->use_simple = true;
vgpu->mm_ctx = ctx;
nv_vgpu_info(vgpu, "Memory init: using simple fallback (no IOMMU)");
return 0;
}
void nv_vgpu_memory_cleanup(struct nv_vgpu_device *vgpu)
{
struct nv_vgpu_mm_ctx *ctx = vgpu->mm_ctx;
struct nv_vgpu_simple_mapping *mapping, *tmp;
if (!ctx)
return;
if (ctx->use_simple) {
mutex_lock(&simple_lock);
list_for_each_entry_safe(mapping, tmp, &simple_mappings, list) {
if (mapping->mapped) {
dma_unmap_page(&vgpu->pdev->dev, mapping->iova, mapping->size, DMA_BIDIRECTIONAL);
mapping->mapped = false;
}
if (mapping->pinned) {
for (int i = 0; i < mapping->page_count; i++) {
if (mapping->pages[i])
put_page(mapping->pages[i]);
}
mapping->pinned = false;
}
list_del(&mapping->list);
vfree(mapping->pages);
kfree(mapping);
}
mutex_unlock(&simple_lock);
} else if (ctx->iommu_domain) {
iommu_detach_device(ctx->iommu_domain, &vgpu->pdev->dev);
iommu_domain_free(ctx->iommu_domain);
}
kfree(ctx);
vgpu->mm_ctx = NULL;
}
int nv_vgpu_memory_pin(struct nv_vgpu_device *vgpu, u64 gpa, u64 size)
{
struct nv_vgpu_mm_ctx *ctx = vgpu->mm_ctx;
struct nv_vgpu_simple_mapping *mapping;
u64 start_pfn = gpa >> PAGE_SHIFT;
u64 end_pfn = (gpa + size + PAGE_SIZE - 1) >> PAGE_SHIFT;
int page_count = end_pfn - start_pfn;
int i, ret = 0;
if (!ctx)
return -ENODEV;
if (!ctx->use_simple)
return -ENODEV;
mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
if (!mapping)
return -ENOMEM;
mapping->gpa = gpa;
mapping->size = size;
mapping->page_count = page_count;
mapping->pages = vzalloc(page_count * sizeof(struct page *));
if (!mapping->pages) {
kfree(mapping);
return -ENOMEM;
}
for (i = 0; i < page_count; i++) {
u64 pfn = start_pfn + i;
struct page *page = pfn_to_page(pfn);
if (!page) {
ret = -EFAULT;
goto err_unlock;
}
get_page(page);
mapping->pages[i] = page;
}
mapping->pinned = true;
mutex_lock(&simple_lock);
list_add_tail(&mapping->list, &simple_mappings);
mutex_unlock(&simple_lock);
return 0;
err_unlock:
for (i = 0; i < page_count; i++) {
if (mapping->pages[i])
put_page(mapping->pages[i]);
}
vfree(mapping->pages);
kfree(mapping);
return ret;
}
int nv_vgpu_memory_unpin(struct nv_vgpu_device *vgpu, u64 gpa, u64 size)
{
struct nv_vgpu_mm_ctx *ctx = vgpu->mm_ctx;
struct nv_vgpu_simple_mapping *mapping;
if (!ctx || !ctx->use_simple)
return -ENODEV;
mutex_lock(&simple_lock);
list_for_each_entry(mapping, &simple_mappings, list) {
if (mapping->gpa == gpa && mapping->size == size) {
if (mapping->mapped) {
mutex_unlock(&simple_lock);
return -EBUSY;
} else if (mapping->pinned) {
for (int i = 0; i < mapping->page_count; i++) {
if (mapping->pages[i])
put_page(mapping->pages[i]);
}
mapping->pinned = false;
}
list_del(&mapping->list);
vfree(mapping->pages);
kfree(mapping);
break;
}
}
mutex_unlock(&simple_lock);
return 0;
}
int nv_vgpu_memory_map(struct nv_vgpu_device *vgpu, u64 gpa, u64 size, u64 *iova)
{
struct nv_vgpu_mm_ctx *ctx = vgpu->mm_ctx;
struct nv_vgpu_simple_mapping *mapping;
u64 map_iova;
int ret = 0;
if (!ctx || !ctx->use_simple)
return -ENODEV;
mutex_lock(&simple_lock);
list_for_each_entry(mapping, &simple_mappings, list) {
if (mapping->gpa == gpa && mapping->size == size)
break;
}
if (&mapping->list == &simple_mappings) {
mutex_unlock(&simple_lock);
return -ENOENT;
}
if (!mapping->pinned) {
mutex_unlock(&simple_lock);
return -EINVAL;
}
map_iova = dma_map_page(&vgpu->pdev->dev, mapping->pages[0], 0, size, DMA_BIDIRECTIONAL);
if (dma_mapping_error(&vgpu->pdev->dev, map_iova)) {
mutex_unlock(&simple_lock);
return -ENOMEM;
}
mapping->iova = map_iova;
mapping->mapped = true;
*iova = map_iova;
mutex_unlock(&simple_lock);
return ret;
}
int nv_vgpu_memory_unmap(struct nv_vgpu_device *vgpu, u64 iova, u64 size)
{
struct nv_vgpu_mm_ctx *ctx = vgpu->mm_ctx;
struct nv_vgpu_simple_mapping *mapping;
int ret = 0;
if (!ctx || !ctx->use_simple)
return -ENODEV;
mutex_lock(&simple_lock);
list_for_each_entry(mapping, &simple_mappings, list) {
if (mapping->iova == iova && mapping->size == size) {
if (!mapping->mapped) {
mutex_unlock(&simple_lock);
return -ENOENT;
}
dma_unmap_page(&vgpu->pdev->dev, iova, size, DMA_BIDIRECTIONAL);
mapping->mapped = false;
mapping->iova = 0;
break;
}
}
mutex_unlock(&simple_lock);
return ret;
}
static vm_fault_t nv_vgpu_vm_fault(struct vm_fault *vmf)
{
struct nv_vgpu_device *vgpu = vmf->vma->vm_private_data;
struct nv_vgpu_mm_ctx *ctx = vgpu->mm_ctx;
struct nv_vgpu_simple_mapping *mapping;
u64 offset = vmf->address - vmf->vma->vm_start + vmf->vma->vm_pgoff * PAGE_SIZE;
u64 pfn;
vm_fault_t ret = VM_FAULT_SIGBUS;
if (!ctx)
return VM_FAULT_SIGBUS;
if (!ctx->use_simple)
return VM_FAULT_SIGBUS;
mutex_lock(&simple_lock);
list_for_each_entry(mapping, &simple_mappings, list) {
if (mapping->mapped && offset >= mapping->iova &&
offset < mapping->iova + mapping->size) {
pfn = (mapping->gpa + offset - mapping->iova) >> PAGE_SHIFT;
vmf->page = pfn_to_page(pfn);
get_page(vmf->page);
ret = VM_FAULT_NOPAGE;
break;
}
}
mutex_unlock(&simple_lock);
return ret;
}
static const struct vm_operations_struct nv_vgpu_vm_ops = {
.fault = nv_vgpu_vm_fault,
};
int nv_vgpu_memory_mmap(struct nv_vgpu_device *vgpu, struct vm_area_struct *vma)
{
vma->vm_ops = &nv_vgpu_vm_ops;
vma->vm_private_data = vgpu;
vm_flags_set(vma, VM_DONTEXPAND | VM_DONTDUMP);
return 0;
}
|