Blame view

drivers/xen/gntalloc.c 15.3 KB
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
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
  /******************************************************************************
   * gntalloc.c
   *
   * Device for creating grant references (in user-space) that may be shared
   * with other domains.
   *
   * 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.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   */
  
  /*
   * This driver exists to allow userspace programs in Linux to allocate kernel
   * memory that will later be shared with another domain.  Without this device,
   * Linux userspace programs cannot create grant references.
   *
   * How this stuff works:
   *   X -> granting a page to Y
   *   Y -> mapping the grant from X
   *
   *   1. X uses the gntalloc device to allocate a page of kernel memory, P.
   *   2. X creates an entry in the grant table that says domid(Y) can access P.
   *      This is done without a hypercall unless the grant table needs expansion.
   *   3. X gives the grant reference identifier, GREF, to Y.
   *   4. Y maps the page, either directly into kernel memory for use in a backend
   *      driver, or via a the gntdev device to map into the address space of an
   *      application running in Y. This is the first point at which Xen does any
   *      tracking of the page.
   *   5. A program in X mmap()s a segment of the gntalloc device that corresponds
   *      to the shared page, and can now communicate with Y over the shared page.
   *
   *
   * NOTE TO USERSPACE LIBRARIES:
   *   The grant allocation and mmap()ing are, naturally, two separate operations.
   *   You set up the sharing by calling the create ioctl() and then the mmap().
   *   Teardown requires munmap() and either close() or ioctl().
   *
   * WARNING: Since Xen does not allow a guest to forcibly end the use of a grant
   * reference, this device can be used to consume kernel memory by leaving grant
   * references mapped by another domain when an application exits. Therefore,
   * there is a global limit on the number of pages that can be allocated. When
   * all references to the page are unmapped, it will be freed during the next
   * grant operation.
   */
283c0972d   Joe Perches   xen: Convert prin...
50
  #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
51
52
53
54
55
56
57
58
59
60
61
62
  #include <linux/atomic.h>
  #include <linux/module.h>
  #include <linux/miscdevice.h>
  #include <linux/kernel.h>
  #include <linux/init.h>
  #include <linux/slab.h>
  #include <linux/fs.h>
  #include <linux/device.h>
  #include <linux/mm.h>
  #include <linux/uaccess.h>
  #include <linux/types.h>
  #include <linux/list.h>
bdc612dc6   Daniel De Graaf   xen/gntalloc,gntd...
63
  #include <linux/highmem.h>
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
64
65
66
67
68
  
  #include <xen/xen.h>
  #include <xen/page.h>
  #include <xen/grant_table.h>
  #include <xen/gntalloc.h>
bdc612dc6   Daniel De Graaf   xen/gntalloc,gntd...
69
  #include <xen/events.h>
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
70
71
72
73
74
75
76
  
  static int limit = 1024;
  module_param(limit, int, 0644);
  MODULE_PARM_DESC(limit, "Maximum number of grants that may be allocated by "
  		"the gntalloc device");
  
  static LIST_HEAD(gref_list);
8ca19a893   Daniel De Graaf   xen/gntalloc: Cha...
77
  static DEFINE_MUTEX(gref_mutex);
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
78
  static int gref_size;
bdc612dc6   Daniel De Graaf   xen/gntalloc,gntd...
79
80
81
82
83
  struct notify_info {
  	uint16_t pgoff:12;    /* Bits 0-11: Offset of the byte to clear */
  	uint16_t flags:2;     /* Bits 12-13: Unmap notification flags */
  	int event;            /* Port (event channel) to notify */
  };
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
84
85
86
87
88
89
90
91
  /* Metadata on a grant reference. */
  struct gntalloc_gref {
  	struct list_head next_gref;  /* list entry gref_list */
  	struct list_head next_file;  /* list entry file->list, if open */
  	struct page *page;	     /* The shared page */
  	uint64_t file_index;         /* File offset for mmap() */
  	unsigned int users;          /* Use count - when zero, waiting on Xen */
  	grant_ref_t gref_id;         /* The grant reference number */
bdc612dc6   Daniel De Graaf   xen/gntalloc,gntd...
92
  	struct notify_info notify;   /* Unmap notification */
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
93
94
95
96
97
98
  };
  
  struct gntalloc_file_private_data {
  	struct list_head list;
  	uint64_t index;
  };
243082e0d   Daniel De Graaf   xen/gntalloc: fix...
99
100
101
102
103
  struct gntalloc_vma_private_data {
  	struct gntalloc_gref *gref;
  	int users;
  	int count;
  };
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
  static void __del_gref(struct gntalloc_gref *gref);
  
  static void do_cleanup(void)
  {
  	struct gntalloc_gref *gref, *n;
  	list_for_each_entry_safe(gref, n, &gref_list, next_gref) {
  		if (!gref->users)
  			__del_gref(gref);
  	}
  }
  
  static int add_grefs(struct ioctl_gntalloc_alloc_gref *op,
  	uint32_t *gref_ids, struct gntalloc_file_private_data *priv)
  {
  	int i, rc, readonly;
  	LIST_HEAD(queue_gref);
  	LIST_HEAD(queue_file);
5903c6bd1   David Vrabel   xen/gntalloc: saf...
121
  	struct gntalloc_gref *gref, *next;
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
  
  	readonly = !(op->flags & GNTALLOC_FLAG_WRITABLE);
  	rc = -ENOMEM;
  	for (i = 0; i < op->count; i++) {
  		gref = kzalloc(sizeof(*gref), GFP_KERNEL);
  		if (!gref)
  			goto undo;
  		list_add_tail(&gref->next_gref, &queue_gref);
  		list_add_tail(&gref->next_file, &queue_file);
  		gref->users = 1;
  		gref->file_index = op->index + i * PAGE_SIZE;
  		gref->page = alloc_page(GFP_KERNEL|__GFP_ZERO);
  		if (!gref->page)
  			goto undo;
  
  		/* Grant foreign access to the page. */
e9de2e5fd   David Vrabel   xen/gntalloc: fix...
138
  		rc = gnttab_grant_foreign_access(op->domid,
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
139
  			pfn_to_mfn(page_to_pfn(gref->page)), readonly);
e9de2e5fd   David Vrabel   xen/gntalloc: fix...
140
  		if (rc < 0)
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
141
  			goto undo;
e9de2e5fd   David Vrabel   xen/gntalloc: fix...
142
  		gref_ids[i] = gref->gref_id = rc;
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
143
144
145
  	}
  
  	/* Add to gref lists. */
8ca19a893   Daniel De Graaf   xen/gntalloc: Cha...
146
  	mutex_lock(&gref_mutex);
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
147
148
  	list_splice_tail(&queue_gref, &gref_list);
  	list_splice_tail(&queue_file, &priv->list);
8ca19a893   Daniel De Graaf   xen/gntalloc: Cha...
149
  	mutex_unlock(&gref_mutex);
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
150
151
152
153
  
  	return 0;
  
  undo:
8ca19a893   Daniel De Graaf   xen/gntalloc: Cha...
154
  	mutex_lock(&gref_mutex);
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
155
  	gref_size -= (op->count - i);
5903c6bd1   David Vrabel   xen/gntalloc: saf...
156
157
  	list_for_each_entry_safe(gref, next, &queue_file, next_file) {
  		list_del(&gref->next_file);
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
158
159
160
161
162
163
164
165
166
167
168
  		__del_gref(gref);
  	}
  
  	/* It's possible for the target domain to map the just-allocated grant
  	 * references by blindly guessing their IDs; if this is done, then
  	 * __del_gref will leave them in the queue_gref list. They need to be
  	 * added to the global list so that we can free them when they are no
  	 * longer referenced.
  	 */
  	if (unlikely(!list_empty(&queue_gref)))
  		list_splice_tail(&queue_gref, &gref_list);
8ca19a893   Daniel De Graaf   xen/gntalloc: Cha...
169
  	mutex_unlock(&gref_mutex);
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
170
171
172
173
174
  	return rc;
  }
  
  static void __del_gref(struct gntalloc_gref *gref)
  {
bdc612dc6   Daniel De Graaf   xen/gntalloc,gntd...
175
176
177
178
179
  	if (gref->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
  		uint8_t *tmp = kmap(gref->page);
  		tmp[gref->notify.pgoff] = 0;
  		kunmap(gref->page);
  	}
0cc678f85   Daniel De Graaf   xen/gnt{dev,alloc...
180
  	if (gref->notify.flags & UNMAP_NOTIFY_SEND_EVENT) {
bdc612dc6   Daniel De Graaf   xen/gntalloc,gntd...
181
  		notify_remote_via_evtchn(gref->notify.event);
0cc678f85   Daniel De Graaf   xen/gnt{dev,alloc...
182
183
  		evtchn_put(gref->notify.event);
  	}
bdc612dc6   Daniel De Graaf   xen/gntalloc,gntd...
184
185
  
  	gref->notify.flags = 0;
e9de2e5fd   David Vrabel   xen/gntalloc: fix...
186
  	if (gref->gref_id) {
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
187
188
189
190
191
  		if (gnttab_query_foreign_access(gref->gref_id))
  			return;
  
  		if (!gnttab_end_foreign_access_ref(gref->gref_id, 0))
  			return;
0105d2b4f   Daniel De Graaf   xen/gntalloc: rel...
192
193
  
  		gnttab_free_grant_reference(gref->gref_id);
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
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
  	}
  
  	gref_size--;
  	list_del(&gref->next_gref);
  
  	if (gref->page)
  		__free_page(gref->page);
  
  	kfree(gref);
  }
  
  /* finds contiguous grant references in a file, returns the first */
  static struct gntalloc_gref *find_grefs(struct gntalloc_file_private_data *priv,
  		uint64_t index, uint32_t count)
  {
  	struct gntalloc_gref *rv = NULL, *gref;
  	list_for_each_entry(gref, &priv->list, next_file) {
  		if (gref->file_index == index && !rv)
  			rv = gref;
  		if (rv) {
  			if (gref->file_index != index)
  				return NULL;
  			index += PAGE_SIZE;
  			count--;
  			if (count == 0)
  				return rv;
  		}
  	}
  	return NULL;
  }
  
  /*
   * -------------------------------------
   *  File operations.
   * -------------------------------------
   */
  static int gntalloc_open(struct inode *inode, struct file *filp)
  {
  	struct gntalloc_file_private_data *priv;
  
  	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  	if (!priv)
  		goto out_nomem;
  	INIT_LIST_HEAD(&priv->list);
  
  	filp->private_data = priv;
  
  	pr_debug("%s: priv %p
  ", __func__, priv);
  
  	return 0;
  
  out_nomem:
  	return -ENOMEM;
  }
  
  static int gntalloc_release(struct inode *inode, struct file *filp)
  {
  	struct gntalloc_file_private_data *priv = filp->private_data;
  	struct gntalloc_gref *gref;
  
  	pr_debug("%s: priv %p
  ", __func__, priv);
8ca19a893   Daniel De Graaf   xen/gntalloc: Cha...
257
  	mutex_lock(&gref_mutex);
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
258
259
260
261
262
263
264
265
266
  	while (!list_empty(&priv->list)) {
  		gref = list_entry(priv->list.next,
  			struct gntalloc_gref, next_file);
  		list_del(&gref->next_file);
  		gref->users--;
  		if (gref->users == 0)
  			__del_gref(gref);
  	}
  	kfree(priv);
8ca19a893   Daniel De Graaf   xen/gntalloc: Cha...
267
  	mutex_unlock(&gref_mutex);
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
  
  	return 0;
  }
  
  static long gntalloc_ioctl_alloc(struct gntalloc_file_private_data *priv,
  		struct ioctl_gntalloc_alloc_gref __user *arg)
  {
  	int rc = 0;
  	struct ioctl_gntalloc_alloc_gref op;
  	uint32_t *gref_ids;
  
  	pr_debug("%s: priv %p
  ", __func__, priv);
  
  	if (copy_from_user(&op, arg, sizeof(op))) {
  		rc = -EFAULT;
  		goto out;
  	}
21643e69a   Dan Carpenter   xen-gntalloc: int...
286
  	gref_ids = kcalloc(op.count, sizeof(gref_ids[0]), GFP_TEMPORARY);
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
287
288
289
290
  	if (!gref_ids) {
  		rc = -ENOMEM;
  		goto out;
  	}
8ca19a893   Daniel De Graaf   xen/gntalloc: Cha...
291
  	mutex_lock(&gref_mutex);
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
292
293
294
295
296
297
  	/* Clean up pages that were at zero (local) users but were still mapped
  	 * by remote domains. Since those pages count towards the limit that we
  	 * are about to enforce, removing them here is a good idea.
  	 */
  	do_cleanup();
  	if (gref_size + op.count > limit) {
8ca19a893   Daniel De Graaf   xen/gntalloc: Cha...
298
  		mutex_unlock(&gref_mutex);
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
299
300
301
302
303
304
  		rc = -ENOSPC;
  		goto out_free;
  	}
  	gref_size += op.count;
  	op.index = priv->index;
  	priv->index += op.count * PAGE_SIZE;
8ca19a893   Daniel De Graaf   xen/gntalloc: Cha...
305
  	mutex_unlock(&gref_mutex);
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
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
  
  	rc = add_grefs(&op, gref_ids, priv);
  	if (rc < 0)
  		goto out_free;
  
  	/* Once we finish add_grefs, it is unsafe to touch the new reference,
  	 * since it is possible for a concurrent ioctl to remove it (by guessing
  	 * its index). If the userspace application doesn't provide valid memory
  	 * to write the IDs to, then it will need to close the file in order to
  	 * release - which it will do by segfaulting when it tries to access the
  	 * IDs to close them.
  	 */
  	if (copy_to_user(arg, &op, sizeof(op))) {
  		rc = -EFAULT;
  		goto out_free;
  	}
  	if (copy_to_user(arg->gref_ids, gref_ids,
  			sizeof(gref_ids[0]) * op.count)) {
  		rc = -EFAULT;
  		goto out_free;
  	}
  
  out_free:
  	kfree(gref_ids);
  out:
  	return rc;
  }
  
  static long gntalloc_ioctl_dealloc(struct gntalloc_file_private_data *priv,
  		void __user *arg)
  {
  	int i, rc = 0;
  	struct ioctl_gntalloc_dealloc_gref op;
  	struct gntalloc_gref *gref, *n;
  
  	pr_debug("%s: priv %p
  ", __func__, priv);
  
  	if (copy_from_user(&op, arg, sizeof(op))) {
  		rc = -EFAULT;
  		goto dealloc_grant_out;
  	}
8ca19a893   Daniel De Graaf   xen/gntalloc: Cha...
348
  	mutex_lock(&gref_mutex);
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
  	gref = find_grefs(priv, op.index, op.count);
  	if (gref) {
  		/* Remove from the file list only, and decrease reference count.
  		 * The later call to do_cleanup() will remove from gref_list and
  		 * free the memory if the pages aren't mapped anywhere.
  		 */
  		for (i = 0; i < op.count; i++) {
  			n = list_entry(gref->next_file.next,
  				struct gntalloc_gref, next_file);
  			list_del(&gref->next_file);
  			gref->users--;
  			gref = n;
  		}
  	} else {
  		rc = -EINVAL;
  	}
  
  	do_cleanup();
8ca19a893   Daniel De Graaf   xen/gntalloc: Cha...
367
  	mutex_unlock(&gref_mutex);
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
368
369
370
  dealloc_grant_out:
  	return rc;
  }
bdc612dc6   Daniel De Graaf   xen/gntalloc,gntd...
371
372
373
374
375
376
377
378
379
380
381
382
383
384
  static long gntalloc_ioctl_unmap_notify(struct gntalloc_file_private_data *priv,
  		void __user *arg)
  {
  	struct ioctl_gntalloc_unmap_notify op;
  	struct gntalloc_gref *gref;
  	uint64_t index;
  	int pgoff;
  	int rc;
  
  	if (copy_from_user(&op, arg, sizeof(op)))
  		return -EFAULT;
  
  	index = op.index & ~(PAGE_SIZE - 1);
  	pgoff = op.index & (PAGE_SIZE - 1);
8ca19a893   Daniel De Graaf   xen/gntalloc: Cha...
385
  	mutex_lock(&gref_mutex);
bdc612dc6   Daniel De Graaf   xen/gntalloc,gntd...
386
387
388
389
390
391
392
393
394
395
396
  
  	gref = find_grefs(priv, index, 1);
  	if (!gref) {
  		rc = -ENOENT;
  		goto unlock_out;
  	}
  
  	if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT)) {
  		rc = -EINVAL;
  		goto unlock_out;
  	}
0cc678f85   Daniel De Graaf   xen/gnt{dev,alloc...
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
  	/* We need to grab a reference to the event channel we are going to use
  	 * to send the notify before releasing the reference we may already have
  	 * (if someone has called this ioctl twice). This is required so that
  	 * it is possible to change the clear_byte part of the notification
  	 * without disturbing the event channel part, which may now be the last
  	 * reference to that event channel.
  	 */
  	if (op.action & UNMAP_NOTIFY_SEND_EVENT) {
  		if (evtchn_get(op.event_channel_port)) {
  			rc = -EINVAL;
  			goto unlock_out;
  		}
  	}
  
  	if (gref->notify.flags & UNMAP_NOTIFY_SEND_EVENT)
  		evtchn_put(gref->notify.event);
bdc612dc6   Daniel De Graaf   xen/gntalloc,gntd...
413
414
415
416
  	gref->notify.flags = op.action;
  	gref->notify.pgoff = pgoff;
  	gref->notify.event = op.event_channel_port;
  	rc = 0;
8ca19a893   Daniel De Graaf   xen/gntalloc: Cha...
417

bdc612dc6   Daniel De Graaf   xen/gntalloc,gntd...
418
   unlock_out:
8ca19a893   Daniel De Graaf   xen/gntalloc: Cha...
419
  	mutex_unlock(&gref_mutex);
bdc612dc6   Daniel De Graaf   xen/gntalloc,gntd...
420
421
  	return rc;
  }
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
422
423
424
425
426
427
428
429
430
431
432
  static long gntalloc_ioctl(struct file *filp, unsigned int cmd,
  		unsigned long arg)
  {
  	struct gntalloc_file_private_data *priv = filp->private_data;
  
  	switch (cmd) {
  	case IOCTL_GNTALLOC_ALLOC_GREF:
  		return gntalloc_ioctl_alloc(priv, (void __user *)arg);
  
  	case IOCTL_GNTALLOC_DEALLOC_GREF:
  		return gntalloc_ioctl_dealloc(priv, (void __user *)arg);
bdc612dc6   Daniel De Graaf   xen/gntalloc,gntd...
433
434
  	case IOCTL_GNTALLOC_SET_UNMAP_NOTIFY:
  		return gntalloc_ioctl_unmap_notify(priv, (void __user *)arg);
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
435
436
437
438
439
440
  	default:
  		return -ENOIOCTLCMD;
  	}
  
  	return 0;
  }
d79647aea   Daniel De Graaf   xen/gntdev,gntall...
441
442
  static void gntalloc_vma_open(struct vm_area_struct *vma)
  {
243082e0d   Daniel De Graaf   xen/gntalloc: fix...
443
444
445
  	struct gntalloc_vma_private_data *priv = vma->vm_private_data;
  
  	if (!priv)
d79647aea   Daniel De Graaf   xen/gntdev,gntall...
446
  		return;
8ca19a893   Daniel De Graaf   xen/gntalloc: Cha...
447
  	mutex_lock(&gref_mutex);
243082e0d   Daniel De Graaf   xen/gntalloc: fix...
448
  	priv->users++;
8ca19a893   Daniel De Graaf   xen/gntalloc: Cha...
449
  	mutex_unlock(&gref_mutex);
d79647aea   Daniel De Graaf   xen/gntdev,gntall...
450
  }
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
451
452
  static void gntalloc_vma_close(struct vm_area_struct *vma)
  {
243082e0d   Daniel De Graaf   xen/gntalloc: fix...
453
454
455
456
457
  	struct gntalloc_vma_private_data *priv = vma->vm_private_data;
  	struct gntalloc_gref *gref, *next;
  	int i;
  
  	if (!priv)
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
458
  		return;
8ca19a893   Daniel De Graaf   xen/gntalloc: Cha...
459
  	mutex_lock(&gref_mutex);
243082e0d   Daniel De Graaf   xen/gntalloc: fix...
460
461
462
463
464
465
466
467
468
469
470
471
472
  	priv->users--;
  	if (priv->users == 0) {
  		gref = priv->gref;
  		for (i = 0; i < priv->count; i++) {
  			gref->users--;
  			next = list_entry(gref->next_gref.next,
  					  struct gntalloc_gref, next_gref);
  			if (gref->users == 0)
  				__del_gref(gref);
  			gref = next;
  		}
  		kfree(priv);
  	}
8ca19a893   Daniel De Graaf   xen/gntalloc: Cha...
473
  	mutex_unlock(&gref_mutex);
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
474
475
476
  }
  
  static struct vm_operations_struct gntalloc_vmops = {
d79647aea   Daniel De Graaf   xen/gntdev,gntall...
477
  	.open = gntalloc_vma_open,
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
478
479
480
481
482
483
  	.close = gntalloc_vma_close,
  };
  
  static int gntalloc_mmap(struct file *filp, struct vm_area_struct *vma)
  {
  	struct gntalloc_file_private_data *priv = filp->private_data;
243082e0d   Daniel De Graaf   xen/gntalloc: fix...
484
  	struct gntalloc_vma_private_data *vm_priv;
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
485
486
487
  	struct gntalloc_gref *gref;
  	int count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  	int rv, i;
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
488
  	if (!(vma->vm_flags & VM_SHARED)) {
283c0972d   Joe Perches   xen: Convert prin...
489
490
  		pr_err("%s: Mapping must be shared
  ", __func__);
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
491
492
  		return -EINVAL;
  	}
243082e0d   Daniel De Graaf   xen/gntalloc: fix...
493
494
495
  	vm_priv = kmalloc(sizeof(*vm_priv), GFP_KERNEL);
  	if (!vm_priv)
  		return -ENOMEM;
8ca19a893   Daniel De Graaf   xen/gntalloc: Cha...
496
  	mutex_lock(&gref_mutex);
243082e0d   Daniel De Graaf   xen/gntalloc: fix...
497
498
499
500
  
  	pr_debug("%s: priv %p,%p, page %lu+%d
  ", __func__,
  		       priv, vm_priv, vma->vm_pgoff, count);
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
501
502
503
504
505
  	gref = find_grefs(priv, vma->vm_pgoff << PAGE_SHIFT, count);
  	if (gref == NULL) {
  		rv = -ENOENT;
  		pr_debug("%s: Could not find grant reference",
  				__func__);
2e1634143   Julia Lawall   xen-gntalloc: int...
506
  		kfree(vm_priv);
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
507
508
  		goto out_unlock;
  	}
243082e0d   Daniel De Graaf   xen/gntalloc: fix...
509
510
511
512
513
  	vm_priv->gref = gref;
  	vm_priv->users = 1;
  	vm_priv->count = count;
  
  	vma->vm_private_data = vm_priv;
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
514

314e51b98   Konstantin Khlebnikov   mm: kill vma flag...
515
  	vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
  
  	vma->vm_ops = &gntalloc_vmops;
  
  	for (i = 0; i < count; i++) {
  		gref->users++;
  		rv = vm_insert_page(vma, vma->vm_start + i * PAGE_SIZE,
  				gref->page);
  		if (rv)
  			goto out_unlock;
  
  		gref = list_entry(gref->next_file.next,
  				struct gntalloc_gref, next_file);
  	}
  	rv = 0;
  
  out_unlock:
8ca19a893   Daniel De Graaf   xen/gntalloc: Cha...
532
  	mutex_unlock(&gref_mutex);
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
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
  	return rv;
  }
  
  static const struct file_operations gntalloc_fops = {
  	.owner = THIS_MODULE,
  	.open = gntalloc_open,
  	.release = gntalloc_release,
  	.unlocked_ioctl = gntalloc_ioctl,
  	.mmap = gntalloc_mmap
  };
  
  /*
   * -------------------------------------
   * Module creation/destruction.
   * -------------------------------------
   */
  static struct miscdevice gntalloc_miscdev = {
  	.minor	= MISC_DYNAMIC_MINOR,
  	.name	= "xen/gntalloc",
  	.fops	= &gntalloc_fops,
  };
  
  static int __init gntalloc_init(void)
  {
  	int err;
  
  	if (!xen_domain())
  		return -ENODEV;
  
  	err = misc_register(&gntalloc_miscdev);
  	if (err != 0) {
283c0972d   Joe Perches   xen: Convert prin...
564
565
  		pr_err("Could not register misc gntalloc device
  ");
dd3140588   Daniel De Graaf   xen-gntalloc: Use...
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
  		return err;
  	}
  
  	pr_debug("Created grant allocation device at %d,%d
  ",
  			MISC_MAJOR, gntalloc_miscdev.minor);
  
  	return 0;
  }
  
  static void __exit gntalloc_exit(void)
  {
  	misc_deregister(&gntalloc_miscdev);
  }
  
  module_init(gntalloc_init);
  module_exit(gntalloc_exit);
  
  MODULE_LICENSE("GPL");
  MODULE_AUTHOR("Carter Weatherly <carter.weatherly@jhuapl.edu>, "
  		"Daniel De Graaf <dgdegra@tycho.nsa.gov>");
  MODULE_DESCRIPTION("User-space grant reference allocator driver");