-
Notifications
You must be signed in to change notification settings - Fork 4
/
io_peer_mem.c
323 lines (251 loc) · 7.53 KB
/
io_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
/*
* Copyright (c) 2015, PMC-Sierra Inc.
*
* 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 <rdma/peer_mem.h>
#include <linux/mm.h>
#include <linux/sched.h>
#include <linux/fs.h>
#include <linux/file.h>
#include <linux/cdev.h>
#define NAME "io_peer_mem"
#define VERSION "0.2"
MODULE_AUTHOR("Logan Gunthorpe");
MODULE_DESCRIPTION("MMAP'd IO memory plug-in");
MODULE_LICENSE("Dual BSD/GPL");
MODULE_VERSION(VERSION);
#ifdef DEBUG
#define debug_msg(FMT, ARGS...) printk(NAME ": " FMT, ## ARGS)
#else
#define debug_msg(FMT, ARGS...)
#endif
/**
* This is copied from drivers/media/v4l2-core/videobuf2-memops.c:
*
* This function attempts to acquire an area mapped in the userspace for
* the duration of a hardware mapping. The area is "locked" by performing
* the same set of operation that are done when process calls fork() and
* memory areas are duplicated.
*/
static struct vm_area_struct *get_vma(struct vm_area_struct *vma)
{
struct vm_area_struct *vma_copy;
vma_copy = kmalloc(sizeof(*vma_copy), GFP_KERNEL);
if (vma_copy == NULL)
return NULL;
if (vma->vm_ops && vma->vm_ops->open)
vma->vm_ops->open(vma);
if (vma->vm_file)
get_file(vma->vm_file);
memcpy(vma_copy, vma, sizeof(*vma));
vma_copy->vm_next = NULL;
vma_copy->vm_prev = NULL;
return vma_copy;
}
static void put_vma(struct vm_area_struct *vma)
{
if (!vma)
return;
if (vma->vm_ops && vma->vm_ops->close)
vma->vm_ops->close(vma);
if (vma->vm_file)
fput(vma->vm_file);
kfree(vma);
}
struct context {
unsigned long addr;
size_t size;
struct vm_area_struct *vma;
};
static void fault_missing_pages(struct vm_area_struct *vma, unsigned long start,
unsigned long end)
{
unsigned long pfn;
if (!(vma->vm_flags & VM_MIXEDMAP))
return;
for (; start < end; start += PAGE_SIZE) {
if (!follow_pfn(vma, start, &pfn))
continue;
handle_mm_fault(current->mm, vma, start, FAULT_FLAG_WRITE);
}
}
static int is_uverbs_file(struct vm_area_struct *vma)
{
/* There is an ugly corner case leak possible:
* If a malicous userspace process grabs the uverbs file descriptor
* (for /dev/infinband/uverbs*), mmaps it, and tries to register
* that memory via io_peer_mem then this module will successfully
* register the memory, and take a reference to that file.
*
* Seeing the uverbs code only cleans up memory regions only after
* that file is closed, and io_peer_mem will only release that file
* when the cleanup occurs, a deadlock will occur and the registration
* will be permanently leaked.
*
* This code handles this situation in a bit of an hackish way:
* we check if the backing file's i_cdev's kobjs's name starts with
* "uverbs" and refuses to map it if it does.
*/
struct inode *i;
if (!vma->vm_file || !vma->vm_file->f_inode)
return 0;
i = vma->vm_file->f_inode;
if (!S_ISCHR(i->i_mode) || i->i_cdev == NULL)
return 0;
return strncmp(i->i_cdev->kobj.name, "uverbs", strlen("uverbs")) == 0;
}
static int acquire(unsigned long addr, size_t size, void *peer_mem_private_data,
char *peer_mem_name, void **context)
{
struct vm_area_struct *vma = NULL;
struct context *ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
unsigned long pfn, end;
if (!ctx)
return 0;
ctx->addr = addr;
ctx->size = size;
end = addr + size;
vma = find_vma(current->mm, addr);
if (!vma || vma->vm_end < end)
goto err;
debug_msg("vma: %lx %lx %lx %zx\n", addr, vma->vm_end - vma->vm_start,
vma->vm_flags, size);
if (is_uverbs_file(vma)) {
pr_err(NAME ": can't register a uverbs device!\n");
goto err;
}
if (!(vma->vm_flags & VM_WRITE))
goto err;
fault_missing_pages(vma, addr & PAGE_MASK, end);
if (follow_pfn(vma, addr, &pfn))
goto err;
debug_msg("pfn: %lx\n", pfn << PAGE_SHIFT);
debug_msg("acquire %p\n", ctx);
ctx->vma = get_vma(vma);
if (ctx->vma == NULL) {
printk(NAME ": could not allocate VMA!\n");
goto err;
}
*context = ctx;
__module_get(THIS_MODULE);
return 1;
err:
kfree(ctx);
return 0;
}
static void release(void *context)
{
struct context *ctx = (struct context *) context;
debug_msg("release %p\n", context);
put_vma(ctx->vma);
kfree(context);
module_put(THIS_MODULE);
}
static int get_pages(unsigned long addr, size_t size, int write, int force,
struct sg_table *sg_head, void *context,
u64 core_context)
{
struct context *ctx = (struct context *) context;
int ret = sg_alloc_table(sg_head, (ctx->size + PAGE_SIZE-1) / PAGE_SIZE,
GFP_KERNEL);
if (ret)
return ret;
return 0;
}
static void put_pages(struct sg_table *sg_head, void *context)
{
sg_free_table(sg_head);
}
static int dma_map(struct sg_table *sg_head, void *context,
struct device *dma_device, int dmasync,
int *nmap)
{
struct scatterlist *sg;
struct context *ctx = (struct context *) context;
unsigned long pfn;
unsigned long addr = ctx->addr;
unsigned long size = ctx->size;
int i, ret;
*nmap = ctx->size / PAGE_SIZE;
for_each_sg(sg_head->sgl, sg, (ctx->size + PAGE_SIZE-1) / PAGE_SIZE, i) {
sg_set_page(sg, NULL, PAGE_SIZE, 0);
if ((ret = follow_pfn(ctx->vma, addr, &pfn)))
return ret;
sg->dma_address = (pfn << PAGE_SHIFT);;
sg->dma_length = PAGE_SIZE;
sg->offset = addr & ~PAGE_MASK;
debug_msg("sg[%d] %lx %x %d\n", i,
(unsigned long) sg->dma_address,
sg->dma_length, sg->offset);
addr += sg->dma_length - sg->offset;
size -= sg->dma_length - sg->offset;
if (!size) {
*nmap = i+1;
break;
}
}
return 0;
}
static int dma_unmap(struct sg_table *sg_head, void *context,
struct device *dma_device)
{
return 0;
}
static unsigned long get_page_size(void *context)
{
return PAGE_SIZE;
}
static struct peer_memory_client io_mem_client = {
.name = NAME,
.version = VERSION,
.acquire = acquire,
.get_pages = get_pages,
.dma_map = dma_map,
.dma_unmap = dma_unmap,
.put_pages = put_pages,
.get_page_size = get_page_size,
.release = release,
};
static void *reg_handle;
static int __init io_mem_init(void)
{
reg_handle = ib_register_peer_memory_client(&io_mem_client, NULL);
if (!reg_handle)
return -EINVAL;
printk(NAME ": module loaded\n");
return 0;
}
static void __exit io_mem_cleanup(void)
{
printk(NAME ": module unloaded\n");
ib_unregister_peer_memory_client(reg_handle);
}
module_init(io_mem_init);
module_exit(io_mem_cleanup);