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