-
Notifications
You must be signed in to change notification settings - Fork 3
/
nv_peer_mem.c
executable file
·462 lines (376 loc) · 12.6 KB
/
nv_peer_mem.c
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
/*
* Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
* Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
*
* This software is available to you under a choice of one of two
* licenses. You may choose to be licensed under the terms of the GNU
* General Public License (GPL) Version 2, available from the file
* COPYING in the main directory of this source tree, or the
* OpenIB.org BSD license below:
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <linux/mm.h>
#include <linux/dma-mapping.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/export.h>
#include <linux/hugetlb.h>
#include <linux/atomic.h>
#include <linux/pci.h>
#include "nv-p2p.h"
#include <rdma/peer_mem.h>
#define DRV_NAME "nv_mem"
#define DRV_VERSION "1.0-7"
#define DRV_RELDATE __DATE__
#define peer_err(FMT, ARGS...) printk(KERN_ERR DRV_NAME " %s:%d " FMT, __FUNCTION__, __LINE__, ## ARGS)
#ifndef NVIDIA_P2P_MAJOR_VERSION_MASK
#define NVIDIA_P2P_MAJOR_VERSION_MASK 0xffff0000
#endif
#ifndef NVIDIA_P2P_MINOR_VERSION_MASK
#define NVIDIA_P2P_MINOR_VERSION_MASK 0x0000ffff
#endif
#ifndef NVIDIA_P2P_MAJOR_VERSION
#define NVIDIA_P2P_MAJOR_VERSION(v) \
(((v) & NVIDIA_P2P_MAJOR_VERSION_MASK) >> 16)
#endif
#ifndef NVIDIA_P2P_MINOR_VERSION
#define NVIDIA_P2P_MINOR_VERSION(v) \
(((v) & NVIDIA_P2P_MINOR_VERSION_MASK))
#endif
/*
* Note: before major version 2, struct dma_mapping had no version field,
* so it is not possible to check version compatibility. In this case
* let us just avoid dma mappings altogether.
*/
#if defined(NVIDIA_P2P_DMA_MAPPING_VERSION) && \
(NVIDIA_P2P_MAJOR_VERSION(NVIDIA_P2P_DMA_MAPPING_VERSION) >= 2)
#pragma message("Enable nvidia_p2p_dma_map_pages support")
#define NV_DMA_MAPPING 1
#else
#define NV_DMA_MAPPING 0
#endif
#ifndef READ_ONCE
#define READ_ONCE(x) ACCESS_ONCE(x)
#endif
#ifndef WRITE_ONCE
#define WRITE_ONCE(x, val) ({ ACCESS_ONCE(x) = (val); })
#endif
MODULE_AUTHOR("Yishai Hadas");
MODULE_DESCRIPTION("NVIDIA GPU memory plug-in");
MODULE_LICENSE("Dual BSD/GPL");
MODULE_VERSION(DRV_VERSION);
#define GPU_PAGE_SHIFT 16
#define GPU_PAGE_SIZE ((u64)1 << GPU_PAGE_SHIFT)
#define GPU_PAGE_OFFSET (GPU_PAGE_SIZE-1)
#define GPU_PAGE_MASK (~GPU_PAGE_OFFSET)
invalidate_peer_memory mem_invalidate_callback;
static void *reg_handle;
struct nv_mem_context {
struct nvidia_p2p_page_table *page_table;
#if NV_DMA_MAPPING
struct nvidia_p2p_dma_mapping *dma_mapping;
#endif
u64 core_context;
u64 page_virt_start;
u64 page_virt_end;
size_t mapped_size;
unsigned long npages;
unsigned long page_size;
int is_callback;
int sg_allocated;
};
static void nv_get_p2p_free_callback(void *data)
{
int ret = 0;
struct nv_mem_context *nv_mem_context = (struct nv_mem_context *)data;
struct nvidia_p2p_page_table *page_table = NULL;
#if NV_DMA_MAPPING
struct nvidia_p2p_dma_mapping *dma_mapping = NULL;
#endif
__module_get(THIS_MODULE);
if (!nv_mem_context) {
peer_err("nv_get_p2p_free_callback -- invalid nv_mem_context\n");
goto out;
}
if (!nv_mem_context->page_table) {
peer_err("nv_get_p2p_free_callback -- invalid page_table\n");
goto out;
}
/* Save page_table locally to prevent it being freed as part of nv_mem_release
in case it's called internally by that callback.
*/
page_table = nv_mem_context->page_table;
#if NV_DMA_MAPPING
if (!nv_mem_context->dma_mapping) {
peer_err("nv_get_p2p_free_callback -- invalid dma_mapping\n");
goto out;
}
dma_mapping = nv_mem_context->dma_mapping;
#endif
/* For now don't set nv_mem_context->page_table to NULL,
* confirmed by NVIDIA that inflight put_pages with valid pointer will fail gracefully.
*/
WRITE_ONCE(nv_mem_context->is_callback, 1);
(*mem_invalidate_callback) (reg_handle, nv_mem_context->core_context);
#if NV_DMA_MAPPING
ret = nvidia_p2p_free_dma_mapping(dma_mapping);
if (ret)
peer_err("nv_get_p2p_free_callback -- error %d while calling nvidia_p2p_free_dma_mapping()\n", ret);
#endif
ret = nvidia_p2p_free_page_table(page_table);
if (ret)
peer_err("nv_get_p2p_free_callback -- error %d while calling nvidia_p2p_free_page_table()\n", ret);
out:
module_put(THIS_MODULE);
return;
}
/* At that function we don't call IB core - no ticket exists */
static void nv_mem_dummy_callback(void *data)
{
struct nv_mem_context *nv_mem_context = (struct nv_mem_context *)data;
int ret = 0;
__module_get(THIS_MODULE);
ret = nvidia_p2p_free_page_table(nv_mem_context->page_table);
if (ret)
peer_err("nv_mem_dummy_callback -- error %d while calling nvidia_p2p_free_page_table()\n", ret);
module_put(THIS_MODULE);
return;
}
/* acquire return code: 1 mine, 0 - not mine */
static int nv_mem_acquire(unsigned long addr, size_t size, void *peer_mem_private_data,
char *peer_mem_name, void **client_context)
{
int ret = 0;
struct nv_mem_context *nv_mem_context;
nv_mem_context = kzalloc(sizeof *nv_mem_context, GFP_KERNEL);
if (!nv_mem_context)
/* Error case handled as not mine */
return 0;
nv_mem_context->page_virt_start = addr & GPU_PAGE_MASK;
nv_mem_context->page_virt_end = (addr + size + GPU_PAGE_SIZE - 1) & GPU_PAGE_MASK;
nv_mem_context->mapped_size = nv_mem_context->page_virt_end - nv_mem_context->page_virt_start;
ret = nvidia_p2p_get_pages(0, 0, nv_mem_context->page_virt_start, nv_mem_context->mapped_size,
&nv_mem_context->page_table, nv_mem_dummy_callback, nv_mem_context);
if (ret < 0)
goto err;
ret = nvidia_p2p_put_pages(0, 0, nv_mem_context->page_virt_start,
nv_mem_context->page_table);
if (ret < 0) {
/* Not expected, however in case callback was called on that buffer just before
put pages we'll expect to fail gracefully (confirmed by NVIDIA) and return an error.
*/
peer_err("nv_mem_acquire -- error %d while calling nvidia_p2p_put_pages()\n", ret);
goto err;
}
/* 1 means mine */
*client_context = nv_mem_context;
__module_get(THIS_MODULE);
return 1;
err:
kfree(nv_mem_context);
/* Error case handled as not mine */
return 0;
}
static int nv_dma_map(struct sg_table *sg_head, void *context,
struct device *dma_device, int dmasync,
int *nmap)
{
int i, ret;
struct scatterlist *sg;
struct nv_mem_context *nv_mem_context =
(struct nv_mem_context *) context;
struct nvidia_p2p_page_table *page_table = nv_mem_context->page_table;
if (page_table->page_size != NVIDIA_P2P_PAGE_SIZE_64KB) {
peer_err("nv_dma_map -- assumption of 64KB pages failed size_id=%u\n",
nv_mem_context->page_table->page_size);
return -EINVAL;
}
#if NV_DMA_MAPPING
{
struct nvidia_p2p_dma_mapping *dma_mapping;
struct pci_dev *pdev = to_pci_dev(dma_device);
if (!pdev) {
peer_err("nv_dma_map -- invalid pci_dev\n");
return -EINVAL;
}
ret = nvidia_p2p_dma_map_pages(pdev, page_table, &dma_mapping);
if (ret) {
peer_err("nv_dma_map -- error %d while calling nvidia_p2p_dma_map_pages()\n", ret);
return ret;
}
if (!NVIDIA_P2P_DMA_MAPPING_VERSION_COMPATIBLE(dma_mapping)) {
peer_err("error, incompatible dma mapping version 0x%08x\n",
dma_mapping->version);
nvidia_p2p_dma_unmap_pages(pdev, page_table, dma_mapping);
return -EINVAL;
}
nv_mem_context->npages = dma_mapping->entries;
ret = sg_alloc_table(sg_head, dma_mapping->entries, GFP_KERNEL);
if (ret) {
nvidia_p2p_dma_unmap_pages(pdev, page_table, dma_mapping);
return ret;
}
nv_mem_context->dma_mapping = dma_mapping;
nv_mem_context->sg_allocated = 1;
for_each_sg(sg_head->sgl, sg, nv_mem_context->npages, i) {
sg_set_page(sg, NULL, nv_mem_context->page_size, 0);
sg->dma_address = dma_mapping->dma_addresses[i];
sg->dma_length = nv_mem_context->page_size;
}
}
#else
nv_mem_context->npages = PAGE_ALIGN(nv_mem_context->mapped_size) >>
GPU_PAGE_SHIFT;
if (page_table->entries != nv_mem_context->npages) {
peer_err("nv_dma_map -- unexpected number of page table entries got=%u, expected=%lu\n",
page_table->entries,
nv_mem_context->npages);
return -EINVAL;
}
ret = sg_alloc_table(sg_head, nv_mem_context->npages, GFP_KERNEL);
if (ret)
return ret;
nv_mem_context->sg_allocated = 1;
for_each_sg(sg_head->sgl, sg, nv_mem_context->npages, i) {
sg_set_page(sg, NULL, nv_mem_context->page_size, 0);
sg->dma_address = page_table->pages[i]->physical_address;
sg->dma_length = nv_mem_context->page_size;
}
#endif
*nmap = nv_mem_context->npages;
return 0;
}
static int nv_dma_unmap(struct sg_table *sg_head, void *context,
struct device *dma_device)
{
struct nv_mem_context *nv_mem_context =
(struct nv_mem_context *)context;
if (!nv_mem_context) {
peer_err("nv_dma_unmap -- invalid nv_mem_context\n");
return -EINVAL;
}
if (READ_ONCE(nv_mem_context->is_callback))
goto out;
#if NV_DMA_MAPPING
{
struct pci_dev *pdev = to_pci_dev(dma_device);
if (nv_mem_context->dma_mapping)
nvidia_p2p_dma_unmap_pages(pdev, nv_mem_context->page_table,
nv_mem_context->dma_mapping);
}
#endif
out:
return 0;
}
static void nv_mem_put_pages(struct sg_table *sg_head, void *context)
{
int ret = 0;
struct nv_mem_context *nv_mem_context =
(struct nv_mem_context *) context;
if (READ_ONCE(nv_mem_context->is_callback))
goto out;
ret = nvidia_p2p_put_pages(0, 0, nv_mem_context->page_virt_start,
nv_mem_context->page_table);
#ifdef _DEBUG_ONLY_
/* Here we expect an error in real life cases that should be ignored - not printed.
* (e.g. concurrent callback with that call)
*/
if (ret < 0) {
printk(KERN_ERR "error %d while calling nvidia_p2p_put_pages, page_table=%p \n",
ret, nv_mem_context->page_table);
}
#endif
out:
if (nv_mem_context->sg_allocated) {
sg_free_table(sg_head);
nv_mem_context->sg_allocated = 0;
}
return;
}
static void nv_mem_release(void *context)
{
struct nv_mem_context *nv_mem_context =
(struct nv_mem_context *) context;
kfree(nv_mem_context);
module_put(THIS_MODULE);
return;
}
static int nv_mem_get_pages(unsigned long addr,
size_t size, int write, int force,
struct sg_table *sg_head,
void *client_context,
u64 core_context)
{
int ret;
struct nv_mem_context *nv_mem_context;
nv_mem_context = (struct nv_mem_context *)client_context;
if (!nv_mem_context)
return -EINVAL;
nv_mem_context->core_context = core_context;
nv_mem_context->page_size = GPU_PAGE_SIZE;
ret = nvidia_p2p_get_pages(0, 0, nv_mem_context->page_virt_start, nv_mem_context->mapped_size,
&nv_mem_context->page_table, nv_get_p2p_free_callback, nv_mem_context);
if (ret < 0) {
peer_err("nv_mem_get_pages -- error %d while calling nvidia_p2p_get_pages()\n", ret);
return ret;
}
/* No extra access to nv_mem_context->page_table here as we are
called not under a lock and may race with inflight invalidate callback on that buffer.
Extra handling was delayed to be done under nv_dma_map.
*/
return 0;
}
static unsigned long nv_mem_get_page_size(void *context)
{
struct nv_mem_context *nv_mem_context =
(struct nv_mem_context *)context;
return nv_mem_context->page_size;
}
static struct peer_memory_client nv_mem_client = {
.acquire = nv_mem_acquire,
.get_pages = nv_mem_get_pages,
.dma_map = nv_dma_map,
.dma_unmap = nv_dma_unmap,
.put_pages = nv_mem_put_pages,
.get_page_size = nv_mem_get_page_size,
.release = nv_mem_release,
};
static int __init nv_mem_client_init(void)
{
strcpy(nv_mem_client.name, DRV_NAME);
strcpy(nv_mem_client.version, DRV_VERSION);
reg_handle = ib_register_peer_memory_client(&nv_mem_client,
&mem_invalidate_callback);
if (!reg_handle)
return -EINVAL;
return 0;
}
static void __exit nv_mem_client_cleanup(void)
{
ib_unregister_peer_memory_client(reg_handle);
}
module_init(nv_mem_client_init);
module_exit(nv_mem_client_cleanup);