1dd314058SDaniel De Graaf /******************************************************************************
2dd314058SDaniel De Graaf * gntalloc.c
3dd314058SDaniel De Graaf *
4dd314058SDaniel De Graaf * Device for creating grant references (in user-space) that may be shared
5dd314058SDaniel De Graaf * with other domains.
6dd314058SDaniel De Graaf *
7dd314058SDaniel De Graaf * This program is distributed in the hope that it will be useful,
8dd314058SDaniel De Graaf * but WITHOUT ANY WARRANTY; without even the implied warranty of
9dd314058SDaniel De Graaf * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10dd314058SDaniel De Graaf * GNU General Public License for more details.
11dd314058SDaniel De Graaf *
12dd314058SDaniel De Graaf * You should have received a copy of the GNU General Public License
13dd314058SDaniel De Graaf * along with this program; if not, write to the Free Software
14dd314058SDaniel De Graaf * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15dd314058SDaniel De Graaf */
16dd314058SDaniel De Graaf
17dd314058SDaniel De Graaf /*
18dd314058SDaniel De Graaf * This driver exists to allow userspace programs in Linux to allocate kernel
19dd314058SDaniel De Graaf * memory that will later be shared with another domain. Without this device,
20dd314058SDaniel De Graaf * Linux userspace programs cannot create grant references.
21dd314058SDaniel De Graaf *
22dd314058SDaniel De Graaf * How this stuff works:
23dd314058SDaniel De Graaf * X -> granting a page to Y
24dd314058SDaniel De Graaf * Y -> mapping the grant from X
25dd314058SDaniel De Graaf *
26dd314058SDaniel De Graaf * 1. X uses the gntalloc device to allocate a page of kernel memory, P.
27dd314058SDaniel De Graaf * 2. X creates an entry in the grant table that says domid(Y) can access P.
28dd314058SDaniel De Graaf * This is done without a hypercall unless the grant table needs expansion.
29dd314058SDaniel De Graaf * 3. X gives the grant reference identifier, GREF, to Y.
30dd314058SDaniel De Graaf * 4. Y maps the page, either directly into kernel memory for use in a backend
31dd314058SDaniel De Graaf * driver, or via a the gntdev device to map into the address space of an
32dd314058SDaniel De Graaf * application running in Y. This is the first point at which Xen does any
33dd314058SDaniel De Graaf * tracking of the page.
34dd314058SDaniel De Graaf * 5. A program in X mmap()s a segment of the gntalloc device that corresponds
35dd314058SDaniel De Graaf * to the shared page, and can now communicate with Y over the shared page.
36dd314058SDaniel De Graaf *
37dd314058SDaniel De Graaf *
38dd314058SDaniel De Graaf * NOTE TO USERSPACE LIBRARIES:
39dd314058SDaniel De Graaf * The grant allocation and mmap()ing are, naturally, two separate operations.
40dd314058SDaniel De Graaf * You set up the sharing by calling the create ioctl() and then the mmap().
41dd314058SDaniel De Graaf * Teardown requires munmap() and either close() or ioctl().
42dd314058SDaniel De Graaf *
43dd314058SDaniel De Graaf * WARNING: Since Xen does not allow a guest to forcibly end the use of a grant
44dd314058SDaniel De Graaf * reference, this device can be used to consume kernel memory by leaving grant
45dd314058SDaniel De Graaf * references mapped by another domain when an application exits. Therefore,
46dd314058SDaniel De Graaf * there is a global limit on the number of pages that can be allocated. When
47dd314058SDaniel De Graaf * all references to the page are unmapped, it will be freed during the next
48dd314058SDaniel De Graaf * grant operation.
49dd314058SDaniel De Graaf */
50dd314058SDaniel De Graaf
51283c0972SJoe Perches #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
52283c0972SJoe Perches
53dd314058SDaniel De Graaf #include <linux/atomic.h>
54dd314058SDaniel De Graaf #include <linux/module.h>
55dd314058SDaniel De Graaf #include <linux/miscdevice.h>
56dd314058SDaniel De Graaf #include <linux/kernel.h>
57dd314058SDaniel De Graaf #include <linux/init.h>
58dd314058SDaniel De Graaf #include <linux/slab.h>
59dd314058SDaniel De Graaf #include <linux/fs.h>
60dd314058SDaniel De Graaf #include <linux/device.h>
61dd314058SDaniel De Graaf #include <linux/mm.h>
62dd314058SDaniel De Graaf #include <linux/uaccess.h>
63dd314058SDaniel De Graaf #include <linux/types.h>
64dd314058SDaniel De Graaf #include <linux/list.h>
65bdc612dcSDaniel De Graaf #include <linux/highmem.h>
66dd314058SDaniel De Graaf
67dd314058SDaniel De Graaf #include <xen/xen.h>
68dd314058SDaniel De Graaf #include <xen/page.h>
69dd314058SDaniel De Graaf #include <xen/grant_table.h>
70dd314058SDaniel De Graaf #include <xen/gntalloc.h>
71bdc612dcSDaniel De Graaf #include <xen/events.h>
72dd314058SDaniel De Graaf
73dd314058SDaniel De Graaf static int limit = 1024;
74dd314058SDaniel De Graaf module_param(limit, int, 0644);
75dd314058SDaniel De Graaf MODULE_PARM_DESC(limit, "Maximum number of grants that may be allocated by "
76dd314058SDaniel De Graaf "the gntalloc device");
77dd314058SDaniel De Graaf
78dd314058SDaniel De Graaf static LIST_HEAD(gref_list);
798ca19a89SDaniel De Graaf static DEFINE_MUTEX(gref_mutex);
80dd314058SDaniel De Graaf static int gref_size;
81dd314058SDaniel De Graaf
82bdc612dcSDaniel De Graaf struct notify_info {
83bdc612dcSDaniel De Graaf uint16_t pgoff:12; /* Bits 0-11: Offset of the byte to clear */
84bdc612dcSDaniel De Graaf uint16_t flags:2; /* Bits 12-13: Unmap notification flags */
85bdc612dcSDaniel De Graaf int event; /* Port (event channel) to notify */
86bdc612dcSDaniel De Graaf };
87bdc612dcSDaniel De Graaf
88dd314058SDaniel De Graaf /* Metadata on a grant reference. */
89dd314058SDaniel De Graaf struct gntalloc_gref {
90dd314058SDaniel De Graaf struct list_head next_gref; /* list entry gref_list */
91dd314058SDaniel De Graaf struct list_head next_file; /* list entry file->list, if open */
92dd314058SDaniel De Graaf struct page *page; /* The shared page */
93dd314058SDaniel De Graaf uint64_t file_index; /* File offset for mmap() */
94dd314058SDaniel De Graaf unsigned int users; /* Use count - when zero, waiting on Xen */
95dd314058SDaniel De Graaf grant_ref_t gref_id; /* The grant reference number */
96bdc612dcSDaniel De Graaf struct notify_info notify; /* Unmap notification */
97dd314058SDaniel De Graaf };
98dd314058SDaniel De Graaf
99dd314058SDaniel De Graaf struct gntalloc_file_private_data {
100dd314058SDaniel De Graaf struct list_head list;
101dd314058SDaniel De Graaf uint64_t index;
102dd314058SDaniel De Graaf };
103dd314058SDaniel De Graaf
104243082e0SDaniel De Graaf struct gntalloc_vma_private_data {
105243082e0SDaniel De Graaf struct gntalloc_gref *gref;
106243082e0SDaniel De Graaf int users;
107243082e0SDaniel De Graaf int count;
108243082e0SDaniel De Graaf };
109243082e0SDaniel De Graaf
110dd314058SDaniel De Graaf static void __del_gref(struct gntalloc_gref *gref);
111dd314058SDaniel De Graaf
do_cleanup(void)112dd314058SDaniel De Graaf static void do_cleanup(void)
113dd314058SDaniel De Graaf {
114dd314058SDaniel De Graaf struct gntalloc_gref *gref, *n;
115dd314058SDaniel De Graaf list_for_each_entry_safe(gref, n, &gref_list, next_gref) {
116dd314058SDaniel De Graaf if (!gref->users)
117dd314058SDaniel De Graaf __del_gref(gref);
118dd314058SDaniel De Graaf }
119dd314058SDaniel De Graaf }
120dd314058SDaniel De Graaf
add_grefs(struct ioctl_gntalloc_alloc_gref * op,uint32_t * gref_ids,struct gntalloc_file_private_data * priv)121dd314058SDaniel De Graaf static int add_grefs(struct ioctl_gntalloc_alloc_gref *op,
122dd314058SDaniel De Graaf uint32_t *gref_ids, struct gntalloc_file_private_data *priv)
123dd314058SDaniel De Graaf {
124dd314058SDaniel De Graaf int i, rc, readonly;
125dd314058SDaniel De Graaf LIST_HEAD(queue_gref);
126dd314058SDaniel De Graaf LIST_HEAD(queue_file);
1275903c6bdSDavid Vrabel struct gntalloc_gref *gref, *next;
128dd314058SDaniel De Graaf
129dd314058SDaniel De Graaf readonly = !(op->flags & GNTALLOC_FLAG_WRITABLE);
130dd314058SDaniel De Graaf for (i = 0; i < op->count; i++) {
131dd314058SDaniel De Graaf gref = kzalloc(sizeof(*gref), GFP_KERNEL);
1320fdb4744SPan Bian if (!gref) {
1330fdb4744SPan Bian rc = -ENOMEM;
134dd314058SDaniel De Graaf goto undo;
1350fdb4744SPan Bian }
136dd314058SDaniel De Graaf list_add_tail(&gref->next_gref, &queue_gref);
137dd314058SDaniel De Graaf list_add_tail(&gref->next_file, &queue_file);
138dd314058SDaniel De Graaf gref->users = 1;
139dd314058SDaniel De Graaf gref->file_index = op->index + i * PAGE_SIZE;
140dd314058SDaniel De Graaf gref->page = alloc_page(GFP_KERNEL|__GFP_ZERO);
1410fdb4744SPan Bian if (!gref->page) {
1420fdb4744SPan Bian rc = -ENOMEM;
143dd314058SDaniel De Graaf goto undo;
1440fdb4744SPan Bian }
145dd314058SDaniel De Graaf
146dd314058SDaniel De Graaf /* Grant foreign access to the page. */
147e9de2e5fSDavid Vrabel rc = gnttab_grant_foreign_access(op->domid,
1480df4f266SJulien Grall xen_page_to_gfn(gref->page),
1490df4f266SJulien Grall readonly);
150e9de2e5fSDavid Vrabel if (rc < 0)
151dd314058SDaniel De Graaf goto undo;
152e9de2e5fSDavid Vrabel gref_ids[i] = gref->gref_id = rc;
153dd314058SDaniel De Graaf }
154dd314058SDaniel De Graaf
155dd314058SDaniel De Graaf /* Add to gref lists. */
1568ca19a89SDaniel De Graaf mutex_lock(&gref_mutex);
157dd314058SDaniel De Graaf list_splice_tail(&queue_gref, &gref_list);
158dd314058SDaniel De Graaf list_splice_tail(&queue_file, &priv->list);
1598ca19a89SDaniel De Graaf mutex_unlock(&gref_mutex);
160dd314058SDaniel De Graaf
161dd314058SDaniel De Graaf return 0;
162dd314058SDaniel De Graaf
163dd314058SDaniel De Graaf undo:
1648ca19a89SDaniel De Graaf mutex_lock(&gref_mutex);
165dd314058SDaniel De Graaf gref_size -= (op->count - i);
166dd314058SDaniel De Graaf
1675903c6bdSDavid Vrabel list_for_each_entry_safe(gref, next, &queue_file, next_file) {
1685903c6bdSDavid Vrabel list_del(&gref->next_file);
169dd314058SDaniel De Graaf __del_gref(gref);
170dd314058SDaniel De Graaf }
171dd314058SDaniel De Graaf
1728ca19a89SDaniel De Graaf mutex_unlock(&gref_mutex);
173dd314058SDaniel De Graaf return rc;
174dd314058SDaniel De Graaf }
175dd314058SDaniel De Graaf
__del_gref(struct gntalloc_gref * gref)176dd314058SDaniel De Graaf static void __del_gref(struct gntalloc_gref *gref)
177dd314058SDaniel De Graaf {
178bdc612dcSDaniel De Graaf if (gref->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
179f31076a6SAlaa Mohamed uint8_t *tmp = kmap_local_page(gref->page);
180bdc612dcSDaniel De Graaf tmp[gref->notify.pgoff] = 0;
181f31076a6SAlaa Mohamed kunmap_local(tmp);
182bdc612dcSDaniel De Graaf }
1830cc678f8SDaniel De Graaf if (gref->notify.flags & UNMAP_NOTIFY_SEND_EVENT) {
184bdc612dcSDaniel De Graaf notify_remote_via_evtchn(gref->notify.event);
1850cc678f8SDaniel De Graaf evtchn_put(gref->notify.event);
1860cc678f8SDaniel De Graaf }
187bdc612dcSDaniel De Graaf
188bdc612dcSDaniel De Graaf gref->notify.flags = 0;
189bdc612dcSDaniel De Graaf
190e9de2e5fSDavid Vrabel if (gref->gref_id) {
19149f8b459SJuergen Gross if (gref->page)
19249f8b459SJuergen Gross gnttab_end_foreign_access(gref->gref_id, gref->page);
19349f8b459SJuergen Gross else
1940105d2b4SDaniel De Graaf gnttab_free_grant_reference(gref->gref_id);
195dd314058SDaniel De Graaf }
196dd314058SDaniel De Graaf
197dd314058SDaniel De Graaf gref_size--;
198dd314058SDaniel De Graaf list_del(&gref->next_gref);
199dd314058SDaniel De Graaf
200dd314058SDaniel De Graaf kfree(gref);
201dd314058SDaniel De Graaf }
202dd314058SDaniel De Graaf
203dd314058SDaniel De Graaf /* finds contiguous grant references in a file, returns the first */
find_grefs(struct gntalloc_file_private_data * priv,uint64_t index,uint32_t count)204dd314058SDaniel De Graaf static struct gntalloc_gref *find_grefs(struct gntalloc_file_private_data *priv,
205dd314058SDaniel De Graaf uint64_t index, uint32_t count)
206dd314058SDaniel De Graaf {
207dd314058SDaniel De Graaf struct gntalloc_gref *rv = NULL, *gref;
208dd314058SDaniel De Graaf list_for_each_entry(gref, &priv->list, next_file) {
209dd314058SDaniel De Graaf if (gref->file_index == index && !rv)
210dd314058SDaniel De Graaf rv = gref;
211dd314058SDaniel De Graaf if (rv) {
212dd314058SDaniel De Graaf if (gref->file_index != index)
213dd314058SDaniel De Graaf return NULL;
214dd314058SDaniel De Graaf index += PAGE_SIZE;
215dd314058SDaniel De Graaf count--;
216dd314058SDaniel De Graaf if (count == 0)
217dd314058SDaniel De Graaf return rv;
218dd314058SDaniel De Graaf }
219dd314058SDaniel De Graaf }
220dd314058SDaniel De Graaf return NULL;
221dd314058SDaniel De Graaf }
222dd314058SDaniel De Graaf
223dd314058SDaniel De Graaf /*
224dd314058SDaniel De Graaf * -------------------------------------
225dd314058SDaniel De Graaf * File operations.
226dd314058SDaniel De Graaf * -------------------------------------
227dd314058SDaniel De Graaf */
gntalloc_open(struct inode * inode,struct file * filp)228dd314058SDaniel De Graaf static int gntalloc_open(struct inode *inode, struct file *filp)
229dd314058SDaniel De Graaf {
230dd314058SDaniel De Graaf struct gntalloc_file_private_data *priv;
231dd314058SDaniel De Graaf
232dd314058SDaniel De Graaf priv = kzalloc(sizeof(*priv), GFP_KERNEL);
233dd314058SDaniel De Graaf if (!priv)
234dd314058SDaniel De Graaf goto out_nomem;
235dd314058SDaniel De Graaf INIT_LIST_HEAD(&priv->list);
236dd314058SDaniel De Graaf
237dd314058SDaniel De Graaf filp->private_data = priv;
238dd314058SDaniel De Graaf
239dd314058SDaniel De Graaf pr_debug("%s: priv %p\n", __func__, priv);
240dd314058SDaniel De Graaf
241dd314058SDaniel De Graaf return 0;
242dd314058SDaniel De Graaf
243dd314058SDaniel De Graaf out_nomem:
244dd314058SDaniel De Graaf return -ENOMEM;
245dd314058SDaniel De Graaf }
246dd314058SDaniel De Graaf
gntalloc_release(struct inode * inode,struct file * filp)247dd314058SDaniel De Graaf static int gntalloc_release(struct inode *inode, struct file *filp)
248dd314058SDaniel De Graaf {
249dd314058SDaniel De Graaf struct gntalloc_file_private_data *priv = filp->private_data;
250dd314058SDaniel De Graaf struct gntalloc_gref *gref;
251dd314058SDaniel De Graaf
252dd314058SDaniel De Graaf pr_debug("%s: priv %p\n", __func__, priv);
253dd314058SDaniel De Graaf
2548ca19a89SDaniel De Graaf mutex_lock(&gref_mutex);
255dd314058SDaniel De Graaf while (!list_empty(&priv->list)) {
256dd314058SDaniel De Graaf gref = list_entry(priv->list.next,
257dd314058SDaniel De Graaf struct gntalloc_gref, next_file);
258dd314058SDaniel De Graaf list_del(&gref->next_file);
259dd314058SDaniel De Graaf gref->users--;
260dd314058SDaniel De Graaf if (gref->users == 0)
261dd314058SDaniel De Graaf __del_gref(gref);
262dd314058SDaniel De Graaf }
263dd314058SDaniel De Graaf kfree(priv);
2648ca19a89SDaniel De Graaf mutex_unlock(&gref_mutex);
265dd314058SDaniel De Graaf
266dd314058SDaniel De Graaf return 0;
267dd314058SDaniel De Graaf }
268dd314058SDaniel De Graaf
gntalloc_ioctl_alloc(struct gntalloc_file_private_data * priv,struct ioctl_gntalloc_alloc_gref __user * arg)269dd314058SDaniel De Graaf static long gntalloc_ioctl_alloc(struct gntalloc_file_private_data *priv,
270dd314058SDaniel De Graaf struct ioctl_gntalloc_alloc_gref __user *arg)
271dd314058SDaniel De Graaf {
272dd314058SDaniel De Graaf int rc = 0;
273dd314058SDaniel De Graaf struct ioctl_gntalloc_alloc_gref op;
274dd314058SDaniel De Graaf uint32_t *gref_ids;
275dd314058SDaniel De Graaf
276dd314058SDaniel De Graaf pr_debug("%s: priv %p\n", __func__, priv);
277dd314058SDaniel De Graaf
278dd314058SDaniel De Graaf if (copy_from_user(&op, arg, sizeof(op))) {
279dd314058SDaniel De Graaf rc = -EFAULT;
280dd314058SDaniel De Graaf goto out;
281dd314058SDaniel De Graaf }
282dd314058SDaniel De Graaf
2830ee931c4SMichal Hocko gref_ids = kcalloc(op.count, sizeof(gref_ids[0]), GFP_KERNEL);
284dd314058SDaniel De Graaf if (!gref_ids) {
285dd314058SDaniel De Graaf rc = -ENOMEM;
286dd314058SDaniel De Graaf goto out;
287dd314058SDaniel De Graaf }
288dd314058SDaniel De Graaf
2898ca19a89SDaniel De Graaf mutex_lock(&gref_mutex);
290dd314058SDaniel De Graaf /* Clean up pages that were at zero (local) users but were still mapped
291dd314058SDaniel De Graaf * by remote domains. Since those pages count towards the limit that we
292dd314058SDaniel De Graaf * are about to enforce, removing them here is a good idea.
293dd314058SDaniel De Graaf */
294dd314058SDaniel De Graaf do_cleanup();
295dd314058SDaniel De Graaf if (gref_size + op.count > limit) {
2968ca19a89SDaniel De Graaf mutex_unlock(&gref_mutex);
297dd314058SDaniel De Graaf rc = -ENOSPC;
298dd314058SDaniel De Graaf goto out_free;
299dd314058SDaniel De Graaf }
300dd314058SDaniel De Graaf gref_size += op.count;
301dd314058SDaniel De Graaf op.index = priv->index;
302dd314058SDaniel De Graaf priv->index += op.count * PAGE_SIZE;
3038ca19a89SDaniel De Graaf mutex_unlock(&gref_mutex);
304dd314058SDaniel De Graaf
305dd314058SDaniel De Graaf rc = add_grefs(&op, gref_ids, priv);
306dd314058SDaniel De Graaf if (rc < 0)
307dd314058SDaniel De Graaf goto out_free;
308dd314058SDaniel De Graaf
309dd314058SDaniel De Graaf /* Once we finish add_grefs, it is unsafe to touch the new reference,
310dd314058SDaniel De Graaf * since it is possible for a concurrent ioctl to remove it (by guessing
311dd314058SDaniel De Graaf * its index). If the userspace application doesn't provide valid memory
312dd314058SDaniel De Graaf * to write the IDs to, then it will need to close the file in order to
313dd314058SDaniel De Graaf * release - which it will do by segfaulting when it tries to access the
314dd314058SDaniel De Graaf * IDs to close them.
315dd314058SDaniel De Graaf */
316dd314058SDaniel De Graaf if (copy_to_user(arg, &op, sizeof(op))) {
317dd314058SDaniel De Graaf rc = -EFAULT;
318dd314058SDaniel De Graaf goto out_free;
319dd314058SDaniel De Graaf }
320dd314058SDaniel De Graaf if (copy_to_user(arg->gref_ids, gref_ids,
321dd314058SDaniel De Graaf sizeof(gref_ids[0]) * op.count)) {
322dd314058SDaniel De Graaf rc = -EFAULT;
323dd314058SDaniel De Graaf goto out_free;
324dd314058SDaniel De Graaf }
325dd314058SDaniel De Graaf
326dd314058SDaniel De Graaf out_free:
327dd314058SDaniel De Graaf kfree(gref_ids);
328dd314058SDaniel De Graaf out:
329dd314058SDaniel De Graaf return rc;
330dd314058SDaniel De Graaf }
331dd314058SDaniel De Graaf
gntalloc_ioctl_dealloc(struct gntalloc_file_private_data * priv,void __user * arg)332dd314058SDaniel De Graaf static long gntalloc_ioctl_dealloc(struct gntalloc_file_private_data *priv,
333dd314058SDaniel De Graaf void __user *arg)
334dd314058SDaniel De Graaf {
335dd314058SDaniel De Graaf int i, rc = 0;
336dd314058SDaniel De Graaf struct ioctl_gntalloc_dealloc_gref op;
337dd314058SDaniel De Graaf struct gntalloc_gref *gref, *n;
338dd314058SDaniel De Graaf
339dd314058SDaniel De Graaf pr_debug("%s: priv %p\n", __func__, priv);
340dd314058SDaniel De Graaf
341dd314058SDaniel De Graaf if (copy_from_user(&op, arg, sizeof(op))) {
342dd314058SDaniel De Graaf rc = -EFAULT;
343dd314058SDaniel De Graaf goto dealloc_grant_out;
344dd314058SDaniel De Graaf }
345dd314058SDaniel De Graaf
3468ca19a89SDaniel De Graaf mutex_lock(&gref_mutex);
347dd314058SDaniel De Graaf gref = find_grefs(priv, op.index, op.count);
348dd314058SDaniel De Graaf if (gref) {
349dd314058SDaniel De Graaf /* Remove from the file list only, and decrease reference count.
350dd314058SDaniel De Graaf * The later call to do_cleanup() will remove from gref_list and
351dd314058SDaniel De Graaf * free the memory if the pages aren't mapped anywhere.
352dd314058SDaniel De Graaf */
353dd314058SDaniel De Graaf for (i = 0; i < op.count; i++) {
354dd314058SDaniel De Graaf n = list_entry(gref->next_file.next,
355dd314058SDaniel De Graaf struct gntalloc_gref, next_file);
356dd314058SDaniel De Graaf list_del(&gref->next_file);
357dd314058SDaniel De Graaf gref->users--;
358dd314058SDaniel De Graaf gref = n;
359dd314058SDaniel De Graaf }
360dd314058SDaniel De Graaf } else {
361dd314058SDaniel De Graaf rc = -EINVAL;
362dd314058SDaniel De Graaf }
363dd314058SDaniel De Graaf
364dd314058SDaniel De Graaf do_cleanup();
365dd314058SDaniel De Graaf
3668ca19a89SDaniel De Graaf mutex_unlock(&gref_mutex);
367dd314058SDaniel De Graaf dealloc_grant_out:
368dd314058SDaniel De Graaf return rc;
369dd314058SDaniel De Graaf }
370dd314058SDaniel De Graaf
gntalloc_ioctl_unmap_notify(struct gntalloc_file_private_data * priv,void __user * arg)371bdc612dcSDaniel De Graaf static long gntalloc_ioctl_unmap_notify(struct gntalloc_file_private_data *priv,
372bdc612dcSDaniel De Graaf void __user *arg)
373bdc612dcSDaniel De Graaf {
374bdc612dcSDaniel De Graaf struct ioctl_gntalloc_unmap_notify op;
375bdc612dcSDaniel De Graaf struct gntalloc_gref *gref;
376bdc612dcSDaniel De Graaf uint64_t index;
377bdc612dcSDaniel De Graaf int pgoff;
378bdc612dcSDaniel De Graaf int rc;
379bdc612dcSDaniel De Graaf
380bdc612dcSDaniel De Graaf if (copy_from_user(&op, arg, sizeof(op)))
381bdc612dcSDaniel De Graaf return -EFAULT;
382bdc612dcSDaniel De Graaf
383bdc612dcSDaniel De Graaf index = op.index & ~(PAGE_SIZE - 1);
384bdc612dcSDaniel De Graaf pgoff = op.index & (PAGE_SIZE - 1);
385bdc612dcSDaniel De Graaf
3868ca19a89SDaniel De Graaf mutex_lock(&gref_mutex);
387bdc612dcSDaniel De Graaf
388bdc612dcSDaniel De Graaf gref = find_grefs(priv, index, 1);
389bdc612dcSDaniel De Graaf if (!gref) {
390bdc612dcSDaniel De Graaf rc = -ENOENT;
391bdc612dcSDaniel De Graaf goto unlock_out;
392bdc612dcSDaniel De Graaf }
393bdc612dcSDaniel De Graaf
394bdc612dcSDaniel De Graaf if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT)) {
395bdc612dcSDaniel De Graaf rc = -EINVAL;
396bdc612dcSDaniel De Graaf goto unlock_out;
397bdc612dcSDaniel De Graaf }
398bdc612dcSDaniel De Graaf
3990cc678f8SDaniel De Graaf /* We need to grab a reference to the event channel we are going to use
4000cc678f8SDaniel De Graaf * to send the notify before releasing the reference we may already have
4010cc678f8SDaniel De Graaf * (if someone has called this ioctl twice). This is required so that
4020cc678f8SDaniel De Graaf * it is possible to change the clear_byte part of the notification
4030cc678f8SDaniel De Graaf * without disturbing the event channel part, which may now be the last
4040cc678f8SDaniel De Graaf * reference to that event channel.
4050cc678f8SDaniel De Graaf */
4060cc678f8SDaniel De Graaf if (op.action & UNMAP_NOTIFY_SEND_EVENT) {
4070cc678f8SDaniel De Graaf if (evtchn_get(op.event_channel_port)) {
4080cc678f8SDaniel De Graaf rc = -EINVAL;
4090cc678f8SDaniel De Graaf goto unlock_out;
4100cc678f8SDaniel De Graaf }
4110cc678f8SDaniel De Graaf }
4120cc678f8SDaniel De Graaf
4130cc678f8SDaniel De Graaf if (gref->notify.flags & UNMAP_NOTIFY_SEND_EVENT)
4140cc678f8SDaniel De Graaf evtchn_put(gref->notify.event);
4150cc678f8SDaniel De Graaf
416bdc612dcSDaniel De Graaf gref->notify.flags = op.action;
417bdc612dcSDaniel De Graaf gref->notify.pgoff = pgoff;
418bdc612dcSDaniel De Graaf gref->notify.event = op.event_channel_port;
419bdc612dcSDaniel De Graaf rc = 0;
4208ca19a89SDaniel De Graaf
421bdc612dcSDaniel De Graaf unlock_out:
4228ca19a89SDaniel De Graaf mutex_unlock(&gref_mutex);
423bdc612dcSDaniel De Graaf return rc;
424bdc612dcSDaniel De Graaf }
425bdc612dcSDaniel De Graaf
gntalloc_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)426dd314058SDaniel De Graaf static long gntalloc_ioctl(struct file *filp, unsigned int cmd,
427dd314058SDaniel De Graaf unsigned long arg)
428dd314058SDaniel De Graaf {
429dd314058SDaniel De Graaf struct gntalloc_file_private_data *priv = filp->private_data;
430dd314058SDaniel De Graaf
431dd314058SDaniel De Graaf switch (cmd) {
432dd314058SDaniel De Graaf case IOCTL_GNTALLOC_ALLOC_GREF:
433dd314058SDaniel De Graaf return gntalloc_ioctl_alloc(priv, (void __user *)arg);
434dd314058SDaniel De Graaf
435dd314058SDaniel De Graaf case IOCTL_GNTALLOC_DEALLOC_GREF:
436dd314058SDaniel De Graaf return gntalloc_ioctl_dealloc(priv, (void __user *)arg);
437dd314058SDaniel De Graaf
438bdc612dcSDaniel De Graaf case IOCTL_GNTALLOC_SET_UNMAP_NOTIFY:
439bdc612dcSDaniel De Graaf return gntalloc_ioctl_unmap_notify(priv, (void __user *)arg);
440bdc612dcSDaniel De Graaf
441dd314058SDaniel De Graaf default:
442dd314058SDaniel De Graaf return -ENOIOCTLCMD;
443dd314058SDaniel De Graaf }
444dd314058SDaniel De Graaf
445dd314058SDaniel De Graaf return 0;
446dd314058SDaniel De Graaf }
447dd314058SDaniel De Graaf
gntalloc_vma_open(struct vm_area_struct * vma)448d79647aeSDaniel De Graaf static void gntalloc_vma_open(struct vm_area_struct *vma)
449d79647aeSDaniel De Graaf {
450243082e0SDaniel De Graaf struct gntalloc_vma_private_data *priv = vma->vm_private_data;
451243082e0SDaniel De Graaf
452243082e0SDaniel De Graaf if (!priv)
453d79647aeSDaniel De Graaf return;
454d79647aeSDaniel De Graaf
4558ca19a89SDaniel De Graaf mutex_lock(&gref_mutex);
456243082e0SDaniel De Graaf priv->users++;
4578ca19a89SDaniel De Graaf mutex_unlock(&gref_mutex);
458d79647aeSDaniel De Graaf }
459d79647aeSDaniel De Graaf
gntalloc_vma_close(struct vm_area_struct * vma)460dd314058SDaniel De Graaf static void gntalloc_vma_close(struct vm_area_struct *vma)
461dd314058SDaniel De Graaf {
462243082e0SDaniel De Graaf struct gntalloc_vma_private_data *priv = vma->vm_private_data;
463243082e0SDaniel De Graaf struct gntalloc_gref *gref, *next;
464243082e0SDaniel De Graaf int i;
465243082e0SDaniel De Graaf
466243082e0SDaniel De Graaf if (!priv)
467dd314058SDaniel De Graaf return;
468dd314058SDaniel De Graaf
4698ca19a89SDaniel De Graaf mutex_lock(&gref_mutex);
470243082e0SDaniel De Graaf priv->users--;
471243082e0SDaniel De Graaf if (priv->users == 0) {
472243082e0SDaniel De Graaf gref = priv->gref;
473243082e0SDaniel De Graaf for (i = 0; i < priv->count; i++) {
474dd314058SDaniel De Graaf gref->users--;
475243082e0SDaniel De Graaf next = list_entry(gref->next_gref.next,
476243082e0SDaniel De Graaf struct gntalloc_gref, next_gref);
477dd314058SDaniel De Graaf if (gref->users == 0)
478dd314058SDaniel De Graaf __del_gref(gref);
479243082e0SDaniel De Graaf gref = next;
480243082e0SDaniel De Graaf }
481243082e0SDaniel De Graaf kfree(priv);
482243082e0SDaniel De Graaf }
4838ca19a89SDaniel De Graaf mutex_unlock(&gref_mutex);
484dd314058SDaniel De Graaf }
485dd314058SDaniel De Graaf
4867cbea8dcSKirill A. Shutemov static const struct vm_operations_struct gntalloc_vmops = {
487d79647aeSDaniel De Graaf .open = gntalloc_vma_open,
488dd314058SDaniel De Graaf .close = gntalloc_vma_close,
489dd314058SDaniel De Graaf };
490dd314058SDaniel De Graaf
gntalloc_mmap(struct file * filp,struct vm_area_struct * vma)491dd314058SDaniel De Graaf static int gntalloc_mmap(struct file *filp, struct vm_area_struct *vma)
492dd314058SDaniel De Graaf {
493dd314058SDaniel De Graaf struct gntalloc_file_private_data *priv = filp->private_data;
494243082e0SDaniel De Graaf struct gntalloc_vma_private_data *vm_priv;
495dd314058SDaniel De Graaf struct gntalloc_gref *gref;
496c7ebf9d9SMuhammad Falak R Wani int count = vma_pages(vma);
497dd314058SDaniel De Graaf int rv, i;
498dd314058SDaniel De Graaf
499dd314058SDaniel De Graaf if (!(vma->vm_flags & VM_SHARED)) {
500283c0972SJoe Perches pr_err("%s: Mapping must be shared\n", __func__);
501dd314058SDaniel De Graaf return -EINVAL;
502dd314058SDaniel De Graaf }
503dd314058SDaniel De Graaf
504243082e0SDaniel De Graaf vm_priv = kmalloc(sizeof(*vm_priv), GFP_KERNEL);
505243082e0SDaniel De Graaf if (!vm_priv)
506243082e0SDaniel De Graaf return -ENOMEM;
507243082e0SDaniel De Graaf
5088ca19a89SDaniel De Graaf mutex_lock(&gref_mutex);
509243082e0SDaniel De Graaf
510243082e0SDaniel De Graaf pr_debug("%s: priv %p,%p, page %lu+%d\n", __func__,
511243082e0SDaniel De Graaf priv, vm_priv, vma->vm_pgoff, count);
512243082e0SDaniel De Graaf
513dd314058SDaniel De Graaf gref = find_grefs(priv, vma->vm_pgoff << PAGE_SHIFT, count);
514dd314058SDaniel De Graaf if (gref == NULL) {
515dd314058SDaniel De Graaf rv = -ENOENT;
516dd314058SDaniel De Graaf pr_debug("%s: Could not find grant reference",
517dd314058SDaniel De Graaf __func__);
5182e163414SJulia Lawall kfree(vm_priv);
519dd314058SDaniel De Graaf goto out_unlock;
520dd314058SDaniel De Graaf }
521dd314058SDaniel De Graaf
522243082e0SDaniel De Graaf vm_priv->gref = gref;
523243082e0SDaniel De Graaf vm_priv->users = 1;
524243082e0SDaniel De Graaf vm_priv->count = count;
525dd314058SDaniel De Graaf
526243082e0SDaniel De Graaf vma->vm_private_data = vm_priv;
527243082e0SDaniel De Graaf
528*1c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_DONTEXPAND | VM_DONTDUMP);
529dd314058SDaniel De Graaf
530dd314058SDaniel De Graaf vma->vm_ops = &gntalloc_vmops;
531dd314058SDaniel De Graaf
532dd314058SDaniel De Graaf for (i = 0; i < count; i++) {
533dd314058SDaniel De Graaf gref->users++;
534dd314058SDaniel De Graaf rv = vm_insert_page(vma, vma->vm_start + i * PAGE_SIZE,
535dd314058SDaniel De Graaf gref->page);
536dd314058SDaniel De Graaf if (rv)
537dd314058SDaniel De Graaf goto out_unlock;
538dd314058SDaniel De Graaf
539dd314058SDaniel De Graaf gref = list_entry(gref->next_file.next,
540dd314058SDaniel De Graaf struct gntalloc_gref, next_file);
541dd314058SDaniel De Graaf }
542dd314058SDaniel De Graaf rv = 0;
543dd314058SDaniel De Graaf
544dd314058SDaniel De Graaf out_unlock:
5458ca19a89SDaniel De Graaf mutex_unlock(&gref_mutex);
546dd314058SDaniel De Graaf return rv;
547dd314058SDaniel De Graaf }
548dd314058SDaniel De Graaf
549dd314058SDaniel De Graaf static const struct file_operations gntalloc_fops = {
550dd314058SDaniel De Graaf .owner = THIS_MODULE,
551dd314058SDaniel De Graaf .open = gntalloc_open,
552dd314058SDaniel De Graaf .release = gntalloc_release,
553dd314058SDaniel De Graaf .unlocked_ioctl = gntalloc_ioctl,
554dd314058SDaniel De Graaf .mmap = gntalloc_mmap
555dd314058SDaniel De Graaf };
556dd314058SDaniel De Graaf
557dd314058SDaniel De Graaf /*
558dd314058SDaniel De Graaf * -------------------------------------
559dd314058SDaniel De Graaf * Module creation/destruction.
560dd314058SDaniel De Graaf * -------------------------------------
561dd314058SDaniel De Graaf */
562dd314058SDaniel De Graaf static struct miscdevice gntalloc_miscdev = {
563dd314058SDaniel De Graaf .minor = MISC_DYNAMIC_MINOR,
564dd314058SDaniel De Graaf .name = "xen/gntalloc",
565dd314058SDaniel De Graaf .fops = &gntalloc_fops,
566dd314058SDaniel De Graaf };
567dd314058SDaniel De Graaf
gntalloc_init(void)568dd314058SDaniel De Graaf static int __init gntalloc_init(void)
569dd314058SDaniel De Graaf {
570dd314058SDaniel De Graaf int err;
571dd314058SDaniel De Graaf
572dd314058SDaniel De Graaf if (!xen_domain())
573dd314058SDaniel De Graaf return -ENODEV;
574dd314058SDaniel De Graaf
575dd314058SDaniel De Graaf err = misc_register(&gntalloc_miscdev);
576dd314058SDaniel De Graaf if (err != 0) {
577283c0972SJoe Perches pr_err("Could not register misc gntalloc device\n");
578dd314058SDaniel De Graaf return err;
579dd314058SDaniel De Graaf }
580dd314058SDaniel De Graaf
581dd314058SDaniel De Graaf pr_debug("Created grant allocation device at %d,%d\n",
582dd314058SDaniel De Graaf MISC_MAJOR, gntalloc_miscdev.minor);
583dd314058SDaniel De Graaf
584dd314058SDaniel De Graaf return 0;
585dd314058SDaniel De Graaf }
586dd314058SDaniel De Graaf
gntalloc_exit(void)587dd314058SDaniel De Graaf static void __exit gntalloc_exit(void)
588dd314058SDaniel De Graaf {
589dd314058SDaniel De Graaf misc_deregister(&gntalloc_miscdev);
590dd314058SDaniel De Graaf }
591dd314058SDaniel De Graaf
592dd314058SDaniel De Graaf module_init(gntalloc_init);
593dd314058SDaniel De Graaf module_exit(gntalloc_exit);
594dd314058SDaniel De Graaf
595dd314058SDaniel De Graaf MODULE_LICENSE("GPL");
596dd314058SDaniel De Graaf MODULE_AUTHOR("Carter Weatherly <carter.weatherly@jhuapl.edu>, "
597dd314058SDaniel De Graaf "Daniel De Graaf <dgdegra@tycho.nsa.gov>");
598dd314058SDaniel De Graaf MODULE_DESCRIPTION("User-space grant reference allocator driver");
599