]> Pileus Git - ~andy/linux/blob - drivers/xen/evtchn.c
xen/evtchn: avoid a deadlock when unbinding an event channel
[~andy/linux] / drivers / xen / evtchn.c
1 /******************************************************************************
2  * evtchn.c
3  *
4  * Driver for receiving and demuxing event-channel signals.
5  *
6  * Copyright (c) 2004-2005, K A Fraser
7  * Multi-process extensions Copyright (c) 2004, Steven Smith
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License version 2
11  * as published by the Free Software Foundation; or, when distributed
12  * separately from the Linux kernel or incorporated into other
13  * software packages, subject to the following license:
14  *
15  * Permission is hereby granted, free of charge, to any person obtaining a copy
16  * of this source file (the "Software"), to deal in the Software without
17  * restriction, including without limitation the rights to use, copy, modify,
18  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
19  * and to permit persons to whom the Software is furnished to do so, subject to
20  * the following conditions:
21  *
22  * The above copyright notice and this permission notice shall be included in
23  * all copies or substantial portions of the Software.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
31  * IN THE SOFTWARE.
32  */
33
34 #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
35
36 #include <linux/module.h>
37 #include <linux/kernel.h>
38 #include <linux/sched.h>
39 #include <linux/slab.h>
40 #include <linux/string.h>
41 #include <linux/errno.h>
42 #include <linux/fs.h>
43 #include <linux/miscdevice.h>
44 #include <linux/major.h>
45 #include <linux/proc_fs.h>
46 #include <linux/stat.h>
47 #include <linux/poll.h>
48 #include <linux/irq.h>
49 #include <linux/init.h>
50 #include <linux/mutex.h>
51 #include <linux/cpu.h>
52
53 #include <xen/xen.h>
54 #include <xen/events.h>
55 #include <xen/evtchn.h>
56 #include <asm/xen/hypervisor.h>
57
58 struct per_user_data {
59         struct mutex bind_mutex; /* serialize bind/unbind operations */
60
61         /* Notification ring, accessed via /dev/xen/evtchn. */
62 #define EVTCHN_RING_SIZE     (PAGE_SIZE / sizeof(evtchn_port_t))
63 #define EVTCHN_RING_MASK(_i) ((_i)&(EVTCHN_RING_SIZE-1))
64         evtchn_port_t *ring;
65         unsigned int ring_cons, ring_prod, ring_overflow;
66         struct mutex ring_cons_mutex; /* protect against concurrent readers */
67
68         /* Processes wait on this queue when ring is empty. */
69         wait_queue_head_t evtchn_wait;
70         struct fasync_struct *evtchn_async_queue;
71         const char *name;
72 };
73
74 /*
75  * Who's bound to each port?  This is logically an array of struct
76  * per_user_data *, but we encode the current enabled-state in bit 0.
77  */
78 static unsigned long *port_user;
79 static DEFINE_SPINLOCK(port_user_lock); /* protects port_user[] and ring_prod */
80
81 static inline struct per_user_data *get_port_user(unsigned port)
82 {
83         return (struct per_user_data *)(port_user[port] & ~1);
84 }
85
86 static inline void set_port_user(unsigned port, struct per_user_data *u)
87 {
88         port_user[port] = (unsigned long)u;
89 }
90
91 static inline bool get_port_enabled(unsigned port)
92 {
93         return port_user[port] & 1;
94 }
95
96 static inline void set_port_enabled(unsigned port, bool enabled)
97 {
98         if (enabled)
99                 port_user[port] |= 1;
100         else
101                 port_user[port] &= ~1;
102 }
103
104 static irqreturn_t evtchn_interrupt(int irq, void *data)
105 {
106         unsigned int port = (unsigned long)data;
107         struct per_user_data *u;
108
109         spin_lock(&port_user_lock);
110
111         u = get_port_user(port);
112
113         WARN(!get_port_enabled(port),
114              "Interrupt for port %d, but apparently not enabled; per-user %p\n",
115              port, u);
116
117         disable_irq_nosync(irq);
118         set_port_enabled(port, false);
119
120         if ((u->ring_prod - u->ring_cons) < EVTCHN_RING_SIZE) {
121                 u->ring[EVTCHN_RING_MASK(u->ring_prod)] = port;
122                 wmb(); /* Ensure ring contents visible */
123                 if (u->ring_cons == u->ring_prod++) {
124                         wake_up_interruptible(&u->evtchn_wait);
125                         kill_fasync(&u->evtchn_async_queue,
126                                     SIGIO, POLL_IN);
127                 }
128         } else
129                 u->ring_overflow = 1;
130
131         spin_unlock(&port_user_lock);
132
133         return IRQ_HANDLED;
134 }
135
136 static ssize_t evtchn_read(struct file *file, char __user *buf,
137                            size_t count, loff_t *ppos)
138 {
139         int rc;
140         unsigned int c, p, bytes1 = 0, bytes2 = 0;
141         struct per_user_data *u = file->private_data;
142
143         /* Whole number of ports. */
144         count &= ~(sizeof(evtchn_port_t)-1);
145
146         if (count == 0)
147                 return 0;
148
149         if (count > PAGE_SIZE)
150                 count = PAGE_SIZE;
151
152         for (;;) {
153                 mutex_lock(&u->ring_cons_mutex);
154
155                 rc = -EFBIG;
156                 if (u->ring_overflow)
157                         goto unlock_out;
158
159                 c = u->ring_cons;
160                 p = u->ring_prod;
161                 if (c != p)
162                         break;
163
164                 mutex_unlock(&u->ring_cons_mutex);
165
166                 if (file->f_flags & O_NONBLOCK)
167                         return -EAGAIN;
168
169                 rc = wait_event_interruptible(u->evtchn_wait,
170                                               u->ring_cons != u->ring_prod);
171                 if (rc)
172                         return rc;
173         }
174
175         /* Byte lengths of two chunks. Chunk split (if any) is at ring wrap. */
176         if (((c ^ p) & EVTCHN_RING_SIZE) != 0) {
177                 bytes1 = (EVTCHN_RING_SIZE - EVTCHN_RING_MASK(c)) *
178                         sizeof(evtchn_port_t);
179                 bytes2 = EVTCHN_RING_MASK(p) * sizeof(evtchn_port_t);
180         } else {
181                 bytes1 = (p - c) * sizeof(evtchn_port_t);
182                 bytes2 = 0;
183         }
184
185         /* Truncate chunks according to caller's maximum byte count. */
186         if (bytes1 > count) {
187                 bytes1 = count;
188                 bytes2 = 0;
189         } else if ((bytes1 + bytes2) > count) {
190                 bytes2 = count - bytes1;
191         }
192
193         rc = -EFAULT;
194         rmb(); /* Ensure that we see the port before we copy it. */
195         if (copy_to_user(buf, &u->ring[EVTCHN_RING_MASK(c)], bytes1) ||
196             ((bytes2 != 0) &&
197              copy_to_user(&buf[bytes1], &u->ring[0], bytes2)))
198                 goto unlock_out;
199
200         u->ring_cons += (bytes1 + bytes2) / sizeof(evtchn_port_t);
201         rc = bytes1 + bytes2;
202
203  unlock_out:
204         mutex_unlock(&u->ring_cons_mutex);
205         return rc;
206 }
207
208 static ssize_t evtchn_write(struct file *file, const char __user *buf,
209                             size_t count, loff_t *ppos)
210 {
211         int rc, i;
212         evtchn_port_t *kbuf = (evtchn_port_t *)__get_free_page(GFP_KERNEL);
213         struct per_user_data *u = file->private_data;
214
215         if (kbuf == NULL)
216                 return -ENOMEM;
217
218         /* Whole number of ports. */
219         count &= ~(sizeof(evtchn_port_t)-1);
220
221         rc = 0;
222         if (count == 0)
223                 goto out;
224
225         if (count > PAGE_SIZE)
226                 count = PAGE_SIZE;
227
228         rc = -EFAULT;
229         if (copy_from_user(kbuf, buf, count) != 0)
230                 goto out;
231
232         spin_lock_irq(&port_user_lock);
233
234         for (i = 0; i < (count/sizeof(evtchn_port_t)); i++) {
235                 unsigned port = kbuf[i];
236
237                 if (port < NR_EVENT_CHANNELS &&
238                     get_port_user(port) == u &&
239                     !get_port_enabled(port)) {
240                         set_port_enabled(port, true);
241                         enable_irq(irq_from_evtchn(port));
242                 }
243         }
244
245         spin_unlock_irq(&port_user_lock);
246
247         rc = count;
248
249  out:
250         free_page((unsigned long)kbuf);
251         return rc;
252 }
253
254 static int evtchn_bind_to_user(struct per_user_data *u, int port)
255 {
256         int rc = 0;
257
258         /*
259          * Ports are never reused, so every caller should pass in a
260          * unique port.
261          *
262          * (Locking not necessary because we haven't registered the
263          * interrupt handler yet, and our caller has already
264          * serialized bind operations.)
265          */
266         BUG_ON(get_port_user(port) != NULL);
267         set_port_user(port, u);
268         set_port_enabled(port, true); /* start enabled */
269
270         rc = bind_evtchn_to_irqhandler(port, evtchn_interrupt, IRQF_DISABLED,
271                                        u->name, (void *)(unsigned long)port);
272         if (rc >= 0)
273                 rc = evtchn_make_refcounted(port);
274         else {
275                 /* bind failed, should close the port now */
276                 struct evtchn_close close;
277                 close.port = port;
278                 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
279                         BUG();
280                 set_port_user(port, NULL);
281         }
282
283         return rc;
284 }
285
286 static void evtchn_unbind_from_user(struct per_user_data *u, int port)
287 {
288         int irq = irq_from_evtchn(port);
289
290         BUG_ON(irq < 0);
291
292         unbind_from_irqhandler(irq, (void *)(unsigned long)port);
293
294         set_port_user(port, NULL);
295 }
296
297 static long evtchn_ioctl(struct file *file,
298                          unsigned int cmd, unsigned long arg)
299 {
300         int rc;
301         struct per_user_data *u = file->private_data;
302         void __user *uarg = (void __user *) arg;
303
304         /* Prevent bind from racing with unbind */
305         mutex_lock(&u->bind_mutex);
306
307         switch (cmd) {
308         case IOCTL_EVTCHN_BIND_VIRQ: {
309                 struct ioctl_evtchn_bind_virq bind;
310                 struct evtchn_bind_virq bind_virq;
311
312                 rc = -EFAULT;
313                 if (copy_from_user(&bind, uarg, sizeof(bind)))
314                         break;
315
316                 bind_virq.virq = bind.virq;
317                 bind_virq.vcpu = 0;
318                 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
319                                                  &bind_virq);
320                 if (rc != 0)
321                         break;
322
323                 rc = evtchn_bind_to_user(u, bind_virq.port);
324                 if (rc == 0)
325                         rc = bind_virq.port;
326                 break;
327         }
328
329         case IOCTL_EVTCHN_BIND_INTERDOMAIN: {
330                 struct ioctl_evtchn_bind_interdomain bind;
331                 struct evtchn_bind_interdomain bind_interdomain;
332
333                 rc = -EFAULT;
334                 if (copy_from_user(&bind, uarg, sizeof(bind)))
335                         break;
336
337                 bind_interdomain.remote_dom  = bind.remote_domain;
338                 bind_interdomain.remote_port = bind.remote_port;
339                 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
340                                                  &bind_interdomain);
341                 if (rc != 0)
342                         break;
343
344                 rc = evtchn_bind_to_user(u, bind_interdomain.local_port);
345                 if (rc == 0)
346                         rc = bind_interdomain.local_port;
347                 break;
348         }
349
350         case IOCTL_EVTCHN_BIND_UNBOUND_PORT: {
351                 struct ioctl_evtchn_bind_unbound_port bind;
352                 struct evtchn_alloc_unbound alloc_unbound;
353
354                 rc = -EFAULT;
355                 if (copy_from_user(&bind, uarg, sizeof(bind)))
356                         break;
357
358                 alloc_unbound.dom        = DOMID_SELF;
359                 alloc_unbound.remote_dom = bind.remote_domain;
360                 rc = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
361                                                  &alloc_unbound);
362                 if (rc != 0)
363                         break;
364
365                 rc = evtchn_bind_to_user(u, alloc_unbound.port);
366                 if (rc == 0)
367                         rc = alloc_unbound.port;
368                 break;
369         }
370
371         case IOCTL_EVTCHN_UNBIND: {
372                 struct ioctl_evtchn_unbind unbind;
373
374                 rc = -EFAULT;
375                 if (copy_from_user(&unbind, uarg, sizeof(unbind)))
376                         break;
377
378                 rc = -EINVAL;
379                 if (unbind.port >= NR_EVENT_CHANNELS)
380                         break;
381
382                 rc = -ENOTCONN;
383                 if (get_port_user(unbind.port) != u)
384                         break;
385
386                 disable_irq(irq_from_evtchn(unbind.port));
387
388                 evtchn_unbind_from_user(u, unbind.port);
389
390                 rc = 0;
391                 break;
392         }
393
394         case IOCTL_EVTCHN_NOTIFY: {
395                 struct ioctl_evtchn_notify notify;
396
397                 rc = -EFAULT;
398                 if (copy_from_user(&notify, uarg, sizeof(notify)))
399                         break;
400
401                 if (notify.port >= NR_EVENT_CHANNELS) {
402                         rc = -EINVAL;
403                 } else if (get_port_user(notify.port) != u) {
404                         rc = -ENOTCONN;
405                 } else {
406                         notify_remote_via_evtchn(notify.port);
407                         rc = 0;
408                 }
409                 break;
410         }
411
412         case IOCTL_EVTCHN_RESET: {
413                 /* Initialise the ring to empty. Clear errors. */
414                 mutex_lock(&u->ring_cons_mutex);
415                 spin_lock_irq(&port_user_lock);
416                 u->ring_cons = u->ring_prod = u->ring_overflow = 0;
417                 spin_unlock_irq(&port_user_lock);
418                 mutex_unlock(&u->ring_cons_mutex);
419                 rc = 0;
420                 break;
421         }
422
423         default:
424                 rc = -ENOSYS;
425                 break;
426         }
427         mutex_unlock(&u->bind_mutex);
428
429         return rc;
430 }
431
432 static unsigned int evtchn_poll(struct file *file, poll_table *wait)
433 {
434         unsigned int mask = POLLOUT | POLLWRNORM;
435         struct per_user_data *u = file->private_data;
436
437         poll_wait(file, &u->evtchn_wait, wait);
438         if (u->ring_cons != u->ring_prod)
439                 mask |= POLLIN | POLLRDNORM;
440         if (u->ring_overflow)
441                 mask = POLLERR;
442         return mask;
443 }
444
445 static int evtchn_fasync(int fd, struct file *filp, int on)
446 {
447         struct per_user_data *u = filp->private_data;
448         return fasync_helper(fd, filp, on, &u->evtchn_async_queue);
449 }
450
451 static int evtchn_open(struct inode *inode, struct file *filp)
452 {
453         struct per_user_data *u;
454
455         u = kzalloc(sizeof(*u), GFP_KERNEL);
456         if (u == NULL)
457                 return -ENOMEM;
458
459         u->name = kasprintf(GFP_KERNEL, "evtchn:%s", current->comm);
460         if (u->name == NULL) {
461                 kfree(u);
462                 return -ENOMEM;
463         }
464
465         init_waitqueue_head(&u->evtchn_wait);
466
467         u->ring = (evtchn_port_t *)__get_free_page(GFP_KERNEL);
468         if (u->ring == NULL) {
469                 kfree(u->name);
470                 kfree(u);
471                 return -ENOMEM;
472         }
473
474         mutex_init(&u->bind_mutex);
475         mutex_init(&u->ring_cons_mutex);
476
477         filp->private_data = u;
478
479         return nonseekable_open(inode, filp);
480 }
481
482 static int evtchn_release(struct inode *inode, struct file *filp)
483 {
484         int i;
485         struct per_user_data *u = filp->private_data;
486
487         for (i = 0; i < NR_EVENT_CHANNELS; i++) {
488                 if (get_port_user(i) != u)
489                         continue;
490
491                 disable_irq(irq_from_evtchn(i));
492                 evtchn_unbind_from_user(get_port_user(i), i);
493         }
494
495         free_page((unsigned long)u->ring);
496         kfree(u->name);
497         kfree(u);
498
499         return 0;
500 }
501
502 static const struct file_operations evtchn_fops = {
503         .owner   = THIS_MODULE,
504         .read    = evtchn_read,
505         .write   = evtchn_write,
506         .unlocked_ioctl = evtchn_ioctl,
507         .poll    = evtchn_poll,
508         .fasync  = evtchn_fasync,
509         .open    = evtchn_open,
510         .release = evtchn_release,
511         .llseek  = no_llseek,
512 };
513
514 static struct miscdevice evtchn_miscdev = {
515         .minor        = MISC_DYNAMIC_MINOR,
516         .name         = "xen/evtchn",
517         .fops         = &evtchn_fops,
518 };
519 static int __init evtchn_init(void)
520 {
521         int err;
522
523         if (!xen_domain())
524                 return -ENODEV;
525
526         port_user = kcalloc(NR_EVENT_CHANNELS, sizeof(*port_user), GFP_KERNEL);
527         if (port_user == NULL)
528                 return -ENOMEM;
529
530         spin_lock_init(&port_user_lock);
531
532         /* Create '/dev/xen/evtchn'. */
533         err = misc_register(&evtchn_miscdev);
534         if (err != 0) {
535                 pr_err("Could not register /dev/xen/evtchn\n");
536                 return err;
537         }
538
539         pr_info("Event-channel device installed\n");
540
541         return 0;
542 }
543
544 static void __exit evtchn_cleanup(void)
545 {
546         kfree(port_user);
547         port_user = NULL;
548
549         misc_deregister(&evtchn_miscdev);
550 }
551
552 module_init(evtchn_init);
553 module_exit(evtchn_cleanup);
554
555 MODULE_LICENSE("GPL");