2009-02-07 03:21:19 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* evtchn.c
|
|
|
|
*
|
|
|
|
* Driver for receiving and demuxing event-channel signals.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2004-2005, K A Fraser
|
|
|
|
* Multi-process extensions Copyright (c) 2004, Steven Smith
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License version 2
|
|
|
|
* as published by the Free Software Foundation; or, when distributed
|
|
|
|
* separately from the Linux kernel or incorporated into other
|
|
|
|
* software packages, subject to the following license:
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this source file (the "Software"), to deal in the Software without
|
|
|
|
* restriction, including without limitation the rights to use, copy, modify,
|
|
|
|
* merge, publish, distribute, sublicense, and/or sell copies of the Software,
|
|
|
|
* and to permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2013-06-28 10:21:41 +00:00
|
|
|
#define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
|
|
|
|
|
2009-02-07 03:21:19 +00:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/miscdevice.h>
|
|
|
|
#include <linux/major.h>
|
|
|
|
#include <linux/proc_fs.h>
|
|
|
|
#include <linux/stat.h>
|
|
|
|
#include <linux/poll.h>
|
|
|
|
#include <linux/irq.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/mutex.h>
|
|
|
|
#include <linux/cpu.h>
|
2015-11-26 16:14:35 +00:00
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/vmalloc.h>
|
2009-10-06 22:11:14 +00:00
|
|
|
|
|
|
|
#include <xen/xen.h>
|
2009-02-07 03:21:19 +00:00
|
|
|
#include <xen/events.h>
|
|
|
|
#include <xen/evtchn.h>
|
2016-06-30 15:56:42 +00:00
|
|
|
#include <xen/xen-ops.h>
|
2009-02-07 03:21:19 +00:00
|
|
|
#include <asm/xen/hypervisor.h>
|
|
|
|
|
|
|
|
struct per_user_data {
|
2009-02-12 21:03:24 +00:00
|
|
|
struct mutex bind_mutex; /* serialize bind/unbind operations */
|
2013-07-19 14:52:00 +00:00
|
|
|
struct rb_root evtchns;
|
2015-11-26 16:14:35 +00:00
|
|
|
unsigned int nr_evtchns;
|
2009-02-12 21:03:24 +00:00
|
|
|
|
2009-02-07 03:21:19 +00:00
|
|
|
/* Notification ring, accessed via /dev/xen/evtchn. */
|
2015-11-26 16:14:35 +00:00
|
|
|
unsigned int ring_size;
|
2009-02-07 03:21:19 +00:00
|
|
|
evtchn_port_t *ring;
|
|
|
|
unsigned int ring_cons, ring_prod, ring_overflow;
|
|
|
|
struct mutex ring_cons_mutex; /* protect against concurrent readers */
|
2013-07-19 14:52:00 +00:00
|
|
|
spinlock_t ring_prod_lock; /* product against concurrent interrupts */
|
2009-02-07 03:21:19 +00:00
|
|
|
|
|
|
|
/* Processes wait on this queue when ring is empty. */
|
|
|
|
wait_queue_head_t evtchn_wait;
|
|
|
|
struct fasync_struct *evtchn_async_queue;
|
|
|
|
const char *name;
|
2016-07-11 14:45:51 +00:00
|
|
|
|
|
|
|
domid_t restrict_domid;
|
2009-02-07 03:21:19 +00:00
|
|
|
};
|
|
|
|
|
2016-07-11 14:45:51 +00:00
|
|
|
#define UNRESTRICTED_DOMID ((domid_t)-1)
|
|
|
|
|
2013-07-19 14:52:00 +00:00
|
|
|
struct user_evtchn {
|
|
|
|
struct rb_node node;
|
|
|
|
struct per_user_data *user;
|
|
|
|
unsigned port;
|
|
|
|
bool enabled;
|
|
|
|
};
|
2009-02-07 03:21:19 +00:00
|
|
|
|
2015-11-26 16:14:35 +00:00
|
|
|
static void evtchn_free_ring(evtchn_port_t *ring)
|
|
|
|
{
|
|
|
|
kvfree(ring);
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int evtchn_ring_offset(struct per_user_data *u,
|
|
|
|
unsigned int idx)
|
|
|
|
{
|
|
|
|
return idx & (u->ring_size - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static evtchn_port_t *evtchn_ring_entry(struct per_user_data *u,
|
|
|
|
unsigned int idx)
|
|
|
|
{
|
|
|
|
return u->ring + evtchn_ring_offset(u, idx);
|
|
|
|
}
|
|
|
|
|
2013-07-19 14:52:00 +00:00
|
|
|
static int add_evtchn(struct per_user_data *u, struct user_evtchn *evtchn)
|
2009-09-18 23:31:22 +00:00
|
|
|
{
|
2013-07-19 14:52:00 +00:00
|
|
|
struct rb_node **new = &(u->evtchns.rb_node), *parent = NULL;
|
2009-09-18 23:31:22 +00:00
|
|
|
|
2015-11-26 16:14:35 +00:00
|
|
|
u->nr_evtchns++;
|
|
|
|
|
2013-07-19 14:52:00 +00:00
|
|
|
while (*new) {
|
|
|
|
struct user_evtchn *this;
|
|
|
|
|
2016-12-20 14:02:20 +00:00
|
|
|
this = rb_entry(*new, struct user_evtchn, node);
|
2013-07-19 14:52:00 +00:00
|
|
|
|
|
|
|
parent = *new;
|
|
|
|
if (this->port < evtchn->port)
|
|
|
|
new = &((*new)->rb_left);
|
|
|
|
else if (this->port > evtchn->port)
|
|
|
|
new = &((*new)->rb_right);
|
|
|
|
else
|
|
|
|
return -EEXIST;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add new node and rebalance tree. */
|
|
|
|
rb_link_node(&evtchn->node, parent, new);
|
|
|
|
rb_insert_color(&evtchn->node, &u->evtchns);
|
|
|
|
|
|
|
|
return 0;
|
2009-09-18 23:31:22 +00:00
|
|
|
}
|
|
|
|
|
2013-07-19 14:52:00 +00:00
|
|
|
static void del_evtchn(struct per_user_data *u, struct user_evtchn *evtchn)
|
2009-09-18 23:31:22 +00:00
|
|
|
{
|
2015-11-26 16:14:35 +00:00
|
|
|
u->nr_evtchns--;
|
2013-07-19 14:52:00 +00:00
|
|
|
rb_erase(&evtchn->node, &u->evtchns);
|
|
|
|
kfree(evtchn);
|
2009-09-18 23:31:22 +00:00
|
|
|
}
|
|
|
|
|
2013-07-19 14:52:00 +00:00
|
|
|
static struct user_evtchn *find_evtchn(struct per_user_data *u, unsigned port)
|
2009-09-18 23:31:22 +00:00
|
|
|
{
|
2013-07-19 14:52:00 +00:00
|
|
|
struct rb_node *node = u->evtchns.rb_node;
|
|
|
|
|
|
|
|
while (node) {
|
|
|
|
struct user_evtchn *evtchn;
|
|
|
|
|
2016-12-20 14:02:20 +00:00
|
|
|
evtchn = rb_entry(node, struct user_evtchn, node);
|
2013-07-19 14:52:00 +00:00
|
|
|
|
|
|
|
if (evtchn->port < port)
|
|
|
|
node = node->rb_left;
|
|
|
|
else if (evtchn->port > port)
|
|
|
|
node = node->rb_right;
|
|
|
|
else
|
|
|
|
return evtchn;
|
|
|
|
}
|
|
|
|
return NULL;
|
2009-09-18 23:31:22 +00:00
|
|
|
}
|
|
|
|
|
2010-10-05 18:13:44 +00:00
|
|
|
static irqreturn_t evtchn_interrupt(int irq, void *data)
|
2009-02-07 03:21:19 +00:00
|
|
|
{
|
2013-07-19 14:52:00 +00:00
|
|
|
struct user_evtchn *evtchn = data;
|
|
|
|
struct per_user_data *u = evtchn->user;
|
2009-09-18 23:31:22 +00:00
|
|
|
|
2013-07-19 14:52:00 +00:00
|
|
|
WARN(!evtchn->enabled,
|
2009-09-18 23:55:29 +00:00
|
|
|
"Interrupt for port %d, but apparently not enabled; per-user %p\n",
|
2013-07-19 14:52:00 +00:00
|
|
|
evtchn->port, u);
|
2009-02-07 03:21:19 +00:00
|
|
|
|
|
|
|
disable_irq_nosync(irq);
|
2013-07-19 14:52:00 +00:00
|
|
|
evtchn->enabled = false;
|
|
|
|
|
|
|
|
spin_lock(&u->ring_prod_lock);
|
2009-02-07 03:21:19 +00:00
|
|
|
|
2015-11-26 16:14:35 +00:00
|
|
|
if ((u->ring_prod - u->ring_cons) < u->ring_size) {
|
|
|
|
*evtchn_ring_entry(u, u->ring_prod) = evtchn->port;
|
2009-02-07 03:21:19 +00:00
|
|
|
wmb(); /* Ensure ring contents visible */
|
|
|
|
if (u->ring_cons == u->ring_prod++) {
|
|
|
|
wake_up_interruptible(&u->evtchn_wait);
|
|
|
|
kill_fasync(&u->evtchn_async_queue,
|
|
|
|
SIGIO, POLL_IN);
|
|
|
|
}
|
2009-09-18 23:31:22 +00:00
|
|
|
} else
|
2009-02-07 03:21:19 +00:00
|
|
|
u->ring_overflow = 1;
|
|
|
|
|
2013-07-19 14:52:00 +00:00
|
|
|
spin_unlock(&u->ring_prod_lock);
|
2009-02-07 03:21:19 +00:00
|
|
|
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t evtchn_read(struct file *file, char __user *buf,
|
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
unsigned int c, p, bytes1 = 0, bytes2 = 0;
|
|
|
|
struct per_user_data *u = file->private_data;
|
|
|
|
|
|
|
|
/* Whole number of ports. */
|
|
|
|
count &= ~(sizeof(evtchn_port_t)-1);
|
|
|
|
|
|
|
|
if (count == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (count > PAGE_SIZE)
|
|
|
|
count = PAGE_SIZE;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
mutex_lock(&u->ring_cons_mutex);
|
|
|
|
|
|
|
|
rc = -EFBIG;
|
|
|
|
if (u->ring_overflow)
|
|
|
|
goto unlock_out;
|
|
|
|
|
|
|
|
c = u->ring_cons;
|
|
|
|
p = u->ring_prod;
|
|
|
|
if (c != p)
|
|
|
|
break;
|
|
|
|
|
|
|
|
mutex_unlock(&u->ring_cons_mutex);
|
|
|
|
|
|
|
|
if (file->f_flags & O_NONBLOCK)
|
|
|
|
return -EAGAIN;
|
|
|
|
|
|
|
|
rc = wait_event_interruptible(u->evtchn_wait,
|
|
|
|
u->ring_cons != u->ring_prod);
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Byte lengths of two chunks. Chunk split (if any) is at ring wrap. */
|
2015-11-26 16:14:35 +00:00
|
|
|
if (((c ^ p) & u->ring_size) != 0) {
|
|
|
|
bytes1 = (u->ring_size - evtchn_ring_offset(u, c)) *
|
2009-02-07 03:21:19 +00:00
|
|
|
sizeof(evtchn_port_t);
|
2015-11-26 16:14:35 +00:00
|
|
|
bytes2 = evtchn_ring_offset(u, p) * sizeof(evtchn_port_t);
|
2009-02-07 03:21:19 +00:00
|
|
|
} else {
|
|
|
|
bytes1 = (p - c) * sizeof(evtchn_port_t);
|
|
|
|
bytes2 = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Truncate chunks according to caller's maximum byte count. */
|
|
|
|
if (bytes1 > count) {
|
|
|
|
bytes1 = count;
|
|
|
|
bytes2 = 0;
|
|
|
|
} else if ((bytes1 + bytes2) > count) {
|
|
|
|
bytes2 = count - bytes1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = -EFAULT;
|
|
|
|
rmb(); /* Ensure that we see the port before we copy it. */
|
2015-11-26 16:14:35 +00:00
|
|
|
if (copy_to_user(buf, evtchn_ring_entry(u, c), bytes1) ||
|
2009-02-07 03:21:19 +00:00
|
|
|
((bytes2 != 0) &&
|
|
|
|
copy_to_user(&buf[bytes1], &u->ring[0], bytes2)))
|
|
|
|
goto unlock_out;
|
|
|
|
|
|
|
|
u->ring_cons += (bytes1 + bytes2) / sizeof(evtchn_port_t);
|
|
|
|
rc = bytes1 + bytes2;
|
|
|
|
|
|
|
|
unlock_out:
|
|
|
|
mutex_unlock(&u->ring_cons_mutex);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t evtchn_write(struct file *file, const char __user *buf,
|
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
|
|
|
int rc, i;
|
|
|
|
evtchn_port_t *kbuf = (evtchn_port_t *)__get_free_page(GFP_KERNEL);
|
|
|
|
struct per_user_data *u = file->private_data;
|
|
|
|
|
|
|
|
if (kbuf == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
/* Whole number of ports. */
|
|
|
|
count &= ~(sizeof(evtchn_port_t)-1);
|
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
if (count == 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (count > PAGE_SIZE)
|
|
|
|
count = PAGE_SIZE;
|
|
|
|
|
|
|
|
rc = -EFAULT;
|
|
|
|
if (copy_from_user(kbuf, buf, count) != 0)
|
|
|
|
goto out;
|
|
|
|
|
2013-07-19 14:52:00 +00:00
|
|
|
mutex_lock(&u->bind_mutex);
|
2009-09-18 23:31:22 +00:00
|
|
|
|
|
|
|
for (i = 0; i < (count/sizeof(evtchn_port_t)); i++) {
|
|
|
|
unsigned port = kbuf[i];
|
2013-07-19 14:52:00 +00:00
|
|
|
struct user_evtchn *evtchn;
|
2009-09-18 23:31:22 +00:00
|
|
|
|
2013-07-19 14:52:00 +00:00
|
|
|
evtchn = find_evtchn(u, port);
|
|
|
|
if (evtchn && !evtchn->enabled) {
|
|
|
|
evtchn->enabled = true;
|
2009-09-18 23:31:22 +00:00
|
|
|
enable_irq(irq_from_evtchn(port));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-19 14:52:00 +00:00
|
|
|
mutex_unlock(&u->bind_mutex);
|
2009-02-07 03:21:19 +00:00
|
|
|
|
|
|
|
rc = count;
|
|
|
|
|
|
|
|
out:
|
|
|
|
free_page((unsigned long)kbuf);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-11-26 16:14:35 +00:00
|
|
|
static int evtchn_resize_ring(struct per_user_data *u)
|
|
|
|
{
|
|
|
|
unsigned int new_size;
|
|
|
|
evtchn_port_t *new_ring, *old_ring;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ensure the ring is large enough to capture all possible
|
|
|
|
* events. i.e., one free slot for each bound event.
|
|
|
|
*/
|
|
|
|
if (u->nr_evtchns <= u->ring_size)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (u->ring_size == 0)
|
|
|
|
new_size = 64;
|
|
|
|
else
|
|
|
|
new_size = 2 * u->ring_size;
|
|
|
|
|
treewide: kvmalloc() -> kvmalloc_array()
The kvmalloc() function has a 2-factor argument form, kvmalloc_array(). This
patch replaces cases of:
kvmalloc(a * b, gfp)
with:
kvmalloc_array(a * b, gfp)
as well as handling cases of:
kvmalloc(a * b * c, gfp)
with:
kvmalloc(array3_size(a, b, c), gfp)
as it's slightly less ugly than:
kvmalloc_array(array_size(a, b), c, gfp)
This does, however, attempt to ignore constant size factors like:
kvmalloc(4 * 1024, gfp)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
type TYPE;
expression THING, E;
@@
(
kvmalloc(
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
kvmalloc(
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression COUNT;
typedef u8;
typedef __u8;
@@
(
kvmalloc(
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
kvmalloc(
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
kvmalloc(
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
kvmalloc(
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
kvmalloc(
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
kvmalloc(
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
kvmalloc(
- sizeof(char) * COUNT
+ COUNT
, ...)
|
kvmalloc(
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
- kvmalloc
+ kvmalloc_array
(
- sizeof(TYPE) * (COUNT_ID)
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kvmalloc
+ kvmalloc_array
(
- sizeof(TYPE) * COUNT_ID
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kvmalloc
+ kvmalloc_array
(
- sizeof(TYPE) * (COUNT_CONST)
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kvmalloc
+ kvmalloc_array
(
- sizeof(TYPE) * COUNT_CONST
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kvmalloc
+ kvmalloc_array
(
- sizeof(THING) * (COUNT_ID)
+ COUNT_ID, sizeof(THING)
, ...)
|
- kvmalloc
+ kvmalloc_array
(
- sizeof(THING) * COUNT_ID
+ COUNT_ID, sizeof(THING)
, ...)
|
- kvmalloc
+ kvmalloc_array
(
- sizeof(THING) * (COUNT_CONST)
+ COUNT_CONST, sizeof(THING)
, ...)
|
- kvmalloc
+ kvmalloc_array
(
- sizeof(THING) * COUNT_CONST
+ COUNT_CONST, sizeof(THING)
, ...)
)
// 2-factor product, only identifiers.
@@
identifier SIZE, COUNT;
@@
- kvmalloc
+ kvmalloc_array
(
- SIZE * COUNT
+ COUNT, SIZE
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
kvmalloc(
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kvmalloc(
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kvmalloc(
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kvmalloc(
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kvmalloc(
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kvmalloc(
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kvmalloc(
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kvmalloc(
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
kvmalloc(
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kvmalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kvmalloc(
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kvmalloc(
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kvmalloc(
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
kvmalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
identifier STRIDE, SIZE, COUNT;
@@
(
kvmalloc(
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kvmalloc(
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kvmalloc(
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kvmalloc(
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kvmalloc(
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kvmalloc(
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kvmalloc(
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kvmalloc(
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products,
// when they're not all constants...
@@
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
kvmalloc(C1 * C2 * C3, ...)
|
kvmalloc(
- (E1) * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
|
kvmalloc(
- (E1) * (E2) * E3
+ array3_size(E1, E2, E3)
, ...)
|
kvmalloc(
- (E1) * (E2) * (E3)
+ array3_size(E1, E2, E3)
, ...)
|
kvmalloc(
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants,
// keeping sizeof() as the second factor argument.
@@
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@
(
kvmalloc(sizeof(THING) * C2, ...)
|
kvmalloc(sizeof(TYPE) * C2, ...)
|
kvmalloc(C1 * C2 * C3, ...)
|
kvmalloc(C1 * C2, ...)
|
- kvmalloc
+ kvmalloc_array
(
- sizeof(TYPE) * (E2)
+ E2, sizeof(TYPE)
, ...)
|
- kvmalloc
+ kvmalloc_array
(
- sizeof(TYPE) * E2
+ E2, sizeof(TYPE)
, ...)
|
- kvmalloc
+ kvmalloc_array
(
- sizeof(THING) * (E2)
+ E2, sizeof(THING)
, ...)
|
- kvmalloc
+ kvmalloc_array
(
- sizeof(THING) * E2
+ E2, sizeof(THING)
, ...)
|
- kvmalloc
+ kvmalloc_array
(
- (E1) * E2
+ E1, E2
, ...)
|
- kvmalloc
+ kvmalloc_array
(
- (E1) * (E2)
+ E1, E2
, ...)
|
- kvmalloc
+ kvmalloc_array
(
- E1 * E2
+ E1, E2
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-12 21:04:32 +00:00
|
|
|
new_ring = kvmalloc_array(new_size, sizeof(*new_ring), GFP_KERNEL);
|
2015-11-26 16:14:35 +00:00
|
|
|
if (!new_ring)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
old_ring = u->ring;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Access to the ring contents is serialized by either the
|
|
|
|
* prod /or/ cons lock so take both when resizing.
|
|
|
|
*/
|
|
|
|
mutex_lock(&u->ring_cons_mutex);
|
|
|
|
spin_lock_irq(&u->ring_prod_lock);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy the old ring contents to the new ring.
|
|
|
|
*
|
2016-05-04 13:02:36 +00:00
|
|
|
* To take care of wrapping, a full ring, and the new index
|
|
|
|
* pointing into the second half, simply copy the old contents
|
|
|
|
* twice.
|
2015-11-26 16:14:35 +00:00
|
|
|
*
|
|
|
|
* +---------+ +------------------+
|
2016-05-04 13:02:36 +00:00
|
|
|
* |34567 12| -> |34567 1234567 12|
|
|
|
|
* +-----p-c-+ +-------c------p---+
|
2015-11-26 16:14:35 +00:00
|
|
|
*/
|
2016-05-04 13:02:36 +00:00
|
|
|
memcpy(new_ring, old_ring, u->ring_size * sizeof(*u->ring));
|
|
|
|
memcpy(new_ring + u->ring_size, old_ring,
|
|
|
|
u->ring_size * sizeof(*u->ring));
|
2015-11-26 16:14:35 +00:00
|
|
|
|
|
|
|
u->ring = new_ring;
|
|
|
|
u->ring_size = new_size;
|
|
|
|
|
|
|
|
spin_unlock_irq(&u->ring_prod_lock);
|
|
|
|
mutex_unlock(&u->ring_cons_mutex);
|
|
|
|
|
|
|
|
evtchn_free_ring(old_ring);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-02-07 03:21:19 +00:00
|
|
|
static int evtchn_bind_to_user(struct per_user_data *u, int port)
|
|
|
|
{
|
2013-07-19 14:52:00 +00:00
|
|
|
struct user_evtchn *evtchn;
|
|
|
|
struct evtchn_close close;
|
2009-02-07 03:21:19 +00:00
|
|
|
int rc = 0;
|
|
|
|
|
2009-02-12 21:03:24 +00:00
|
|
|
/*
|
|
|
|
* Ports are never reused, so every caller should pass in a
|
|
|
|
* unique port.
|
|
|
|
*
|
|
|
|
* (Locking not necessary because we haven't registered the
|
|
|
|
* interrupt handler yet, and our caller has already
|
|
|
|
* serialized bind operations.)
|
|
|
|
*/
|
2013-07-19 14:52:00 +00:00
|
|
|
|
|
|
|
evtchn = kzalloc(sizeof(*evtchn), GFP_KERNEL);
|
|
|
|
if (!evtchn)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
evtchn->user = u;
|
|
|
|
evtchn->port = port;
|
|
|
|
evtchn->enabled = true; /* start enabled */
|
|
|
|
|
|
|
|
rc = add_evtchn(u, evtchn);
|
|
|
|
if (rc < 0)
|
|
|
|
goto err;
|
2009-02-07 03:21:19 +00:00
|
|
|
|
2015-11-26 16:14:35 +00:00
|
|
|
rc = evtchn_resize_ring(u);
|
|
|
|
if (rc < 0)
|
|
|
|
goto err;
|
|
|
|
|
2013-09-09 03:09:47 +00:00
|
|
|
rc = bind_evtchn_to_irqhandler(port, evtchn_interrupt, 0,
|
2013-07-19 14:52:00 +00:00
|
|
|
u->name, evtchn);
|
|
|
|
if (rc < 0)
|
|
|
|
goto err;
|
2009-02-12 21:03:24 +00:00
|
|
|
|
2013-07-19 14:52:00 +00:00
|
|
|
rc = evtchn_make_refcounted(port);
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
err:
|
|
|
|
/* bind failed, should close the port now */
|
|
|
|
close.port = port;
|
|
|
|
if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
|
|
|
|
BUG();
|
|
|
|
del_evtchn(u, evtchn);
|
2009-02-07 03:21:19 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2013-07-19 14:52:00 +00:00
|
|
|
static void evtchn_unbind_from_user(struct per_user_data *u,
|
|
|
|
struct user_evtchn *evtchn)
|
2009-02-07 03:21:19 +00:00
|
|
|
{
|
2013-07-19 14:52:00 +00:00
|
|
|
int irq = irq_from_evtchn(evtchn->port);
|
2009-02-07 03:21:19 +00:00
|
|
|
|
2013-02-18 14:57:58 +00:00
|
|
|
BUG_ON(irq < 0);
|
|
|
|
|
2013-07-19 14:52:00 +00:00
|
|
|
unbind_from_irqhandler(irq, evtchn);
|
2009-02-12 21:03:24 +00:00
|
|
|
|
2013-07-19 14:52:00 +00:00
|
|
|
del_evtchn(u, evtchn);
|
2009-02-07 03:21:19 +00:00
|
|
|
}
|
|
|
|
|
xen-evtchn: Bind dyn evtchn:qemu-dm interrupt to next online VCPU
A HVM domian booting generates around 200K (evtchn:qemu-dm xen-dyn)
interrupts,in a short period of time. All these evtchn:qemu-dm are bound
to VCPU 0, until irqbalance sees these IRQ and moves it to a different VCPU.
In one configuration, irqbalance runs every 10 seconds, which means
irqbalance doesn't get to see these burst of interrupts and doesn't
re-balance interrupts most of the time, making all evtchn:qemu-dm to be
processed by VCPU0. This cause VCPU0 to spend most of time processing
hardirq and very little time on softirq. Moreover, if dom0 kernel PREEMPTION
is disabled, VCPU0 never runs watchdog (process context), triggering a
softlockup detection code to panic.
Binding evtchn:qemu-dm to next online VCPU, will spread hardirq
processing evenly across different CPU. Later, irqbalance will try to balance
evtchn:qemu-dm, if required.
Signed-off-by: Anoob Soman <anoob.soman@citrix.com>
Reviewed-by: Boris Ostrovsky <boris.ostrovsky@oracle.com>
Signed-off-by: Juergen Gross <jgross@suse.com>
2017-06-07 11:46:56 +00:00
|
|
|
static DEFINE_PER_CPU(int, bind_last_selected_cpu);
|
|
|
|
|
|
|
|
static void evtchn_bind_interdom_next_vcpu(int evtchn)
|
|
|
|
{
|
|
|
|
unsigned int selected_cpu, irq;
|
|
|
|
struct irq_desc *desc;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
irq = irq_from_evtchn(evtchn);
|
|
|
|
desc = irq_to_desc(irq);
|
|
|
|
|
|
|
|
if (!desc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
raw_spin_lock_irqsave(&desc->lock, flags);
|
|
|
|
selected_cpu = this_cpu_read(bind_last_selected_cpu);
|
|
|
|
selected_cpu = cpumask_next_and(selected_cpu,
|
|
|
|
desc->irq_common_data.affinity, cpu_online_mask);
|
|
|
|
|
|
|
|
if (unlikely(selected_cpu >= nr_cpu_ids))
|
|
|
|
selected_cpu = cpumask_first_and(desc->irq_common_data.affinity,
|
|
|
|
cpu_online_mask);
|
|
|
|
|
|
|
|
this_cpu_write(bind_last_selected_cpu, selected_cpu);
|
|
|
|
|
|
|
|
/* unmask expects irqs to be disabled */
|
2019-06-21 18:47:03 +00:00
|
|
|
xen_set_affinity_evtchn(desc, selected_cpu);
|
xen-evtchn: Bind dyn evtchn:qemu-dm interrupt to next online VCPU
A HVM domian booting generates around 200K (evtchn:qemu-dm xen-dyn)
interrupts,in a short period of time. All these evtchn:qemu-dm are bound
to VCPU 0, until irqbalance sees these IRQ and moves it to a different VCPU.
In one configuration, irqbalance runs every 10 seconds, which means
irqbalance doesn't get to see these burst of interrupts and doesn't
re-balance interrupts most of the time, making all evtchn:qemu-dm to be
processed by VCPU0. This cause VCPU0 to spend most of time processing
hardirq and very little time on softirq. Moreover, if dom0 kernel PREEMPTION
is disabled, VCPU0 never runs watchdog (process context), triggering a
softlockup detection code to panic.
Binding evtchn:qemu-dm to next online VCPU, will spread hardirq
processing evenly across different CPU. Later, irqbalance will try to balance
evtchn:qemu-dm, if required.
Signed-off-by: Anoob Soman <anoob.soman@citrix.com>
Reviewed-by: Boris Ostrovsky <boris.ostrovsky@oracle.com>
Signed-off-by: Juergen Gross <jgross@suse.com>
2017-06-07 11:46:56 +00:00
|
|
|
raw_spin_unlock_irqrestore(&desc->lock, flags);
|
|
|
|
}
|
|
|
|
|
2009-02-07 03:21:19 +00:00
|
|
|
static long evtchn_ioctl(struct file *file,
|
|
|
|
unsigned int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
struct per_user_data *u = file->private_data;
|
|
|
|
void __user *uarg = (void __user *) arg;
|
|
|
|
|
2009-02-12 21:03:24 +00:00
|
|
|
/* Prevent bind from racing with unbind */
|
|
|
|
mutex_lock(&u->bind_mutex);
|
|
|
|
|
2009-02-07 03:21:19 +00:00
|
|
|
switch (cmd) {
|
|
|
|
case IOCTL_EVTCHN_BIND_VIRQ: {
|
|
|
|
struct ioctl_evtchn_bind_virq bind;
|
|
|
|
struct evtchn_bind_virq bind_virq;
|
|
|
|
|
2016-07-11 14:45:51 +00:00
|
|
|
rc = -EACCES;
|
|
|
|
if (u->restrict_domid != UNRESTRICTED_DOMID)
|
|
|
|
break;
|
|
|
|
|
2009-02-07 03:21:19 +00:00
|
|
|
rc = -EFAULT;
|
|
|
|
if (copy_from_user(&bind, uarg, sizeof(bind)))
|
|
|
|
break;
|
|
|
|
|
|
|
|
bind_virq.virq = bind.virq;
|
2016-06-30 15:56:42 +00:00
|
|
|
bind_virq.vcpu = xen_vcpu_nr(0);
|
2009-02-07 03:21:19 +00:00
|
|
|
rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
|
|
|
|
&bind_virq);
|
|
|
|
if (rc != 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
rc = evtchn_bind_to_user(u, bind_virq.port);
|
|
|
|
if (rc == 0)
|
|
|
|
rc = bind_virq.port;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case IOCTL_EVTCHN_BIND_INTERDOMAIN: {
|
|
|
|
struct ioctl_evtchn_bind_interdomain bind;
|
|
|
|
struct evtchn_bind_interdomain bind_interdomain;
|
|
|
|
|
|
|
|
rc = -EFAULT;
|
|
|
|
if (copy_from_user(&bind, uarg, sizeof(bind)))
|
|
|
|
break;
|
|
|
|
|
2016-07-11 14:45:51 +00:00
|
|
|
rc = -EACCES;
|
|
|
|
if (u->restrict_domid != UNRESTRICTED_DOMID &&
|
|
|
|
u->restrict_domid != bind.remote_domain)
|
|
|
|
break;
|
|
|
|
|
2009-02-07 03:21:19 +00:00
|
|
|
bind_interdomain.remote_dom = bind.remote_domain;
|
|
|
|
bind_interdomain.remote_port = bind.remote_port;
|
|
|
|
rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
|
|
|
|
&bind_interdomain);
|
|
|
|
if (rc != 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
rc = evtchn_bind_to_user(u, bind_interdomain.local_port);
|
xen-evtchn: Bind dyn evtchn:qemu-dm interrupt to next online VCPU
A HVM domian booting generates around 200K (evtchn:qemu-dm xen-dyn)
interrupts,in a short period of time. All these evtchn:qemu-dm are bound
to VCPU 0, until irqbalance sees these IRQ and moves it to a different VCPU.
In one configuration, irqbalance runs every 10 seconds, which means
irqbalance doesn't get to see these burst of interrupts and doesn't
re-balance interrupts most of the time, making all evtchn:qemu-dm to be
processed by VCPU0. This cause VCPU0 to spend most of time processing
hardirq and very little time on softirq. Moreover, if dom0 kernel PREEMPTION
is disabled, VCPU0 never runs watchdog (process context), triggering a
softlockup detection code to panic.
Binding evtchn:qemu-dm to next online VCPU, will spread hardirq
processing evenly across different CPU. Later, irqbalance will try to balance
evtchn:qemu-dm, if required.
Signed-off-by: Anoob Soman <anoob.soman@citrix.com>
Reviewed-by: Boris Ostrovsky <boris.ostrovsky@oracle.com>
Signed-off-by: Juergen Gross <jgross@suse.com>
2017-06-07 11:46:56 +00:00
|
|
|
if (rc == 0) {
|
2009-02-07 03:21:19 +00:00
|
|
|
rc = bind_interdomain.local_port;
|
xen-evtchn: Bind dyn evtchn:qemu-dm interrupt to next online VCPU
A HVM domian booting generates around 200K (evtchn:qemu-dm xen-dyn)
interrupts,in a short period of time. All these evtchn:qemu-dm are bound
to VCPU 0, until irqbalance sees these IRQ and moves it to a different VCPU.
In one configuration, irqbalance runs every 10 seconds, which means
irqbalance doesn't get to see these burst of interrupts and doesn't
re-balance interrupts most of the time, making all evtchn:qemu-dm to be
processed by VCPU0. This cause VCPU0 to spend most of time processing
hardirq and very little time on softirq. Moreover, if dom0 kernel PREEMPTION
is disabled, VCPU0 never runs watchdog (process context), triggering a
softlockup detection code to panic.
Binding evtchn:qemu-dm to next online VCPU, will spread hardirq
processing evenly across different CPU. Later, irqbalance will try to balance
evtchn:qemu-dm, if required.
Signed-off-by: Anoob Soman <anoob.soman@citrix.com>
Reviewed-by: Boris Ostrovsky <boris.ostrovsky@oracle.com>
Signed-off-by: Juergen Gross <jgross@suse.com>
2017-06-07 11:46:56 +00:00
|
|
|
evtchn_bind_interdom_next_vcpu(rc);
|
|
|
|
}
|
2009-02-07 03:21:19 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case IOCTL_EVTCHN_BIND_UNBOUND_PORT: {
|
|
|
|
struct ioctl_evtchn_bind_unbound_port bind;
|
|
|
|
struct evtchn_alloc_unbound alloc_unbound;
|
|
|
|
|
2016-07-11 14:45:51 +00:00
|
|
|
rc = -EACCES;
|
|
|
|
if (u->restrict_domid != UNRESTRICTED_DOMID)
|
|
|
|
break;
|
|
|
|
|
2009-02-07 03:21:19 +00:00
|
|
|
rc = -EFAULT;
|
|
|
|
if (copy_from_user(&bind, uarg, sizeof(bind)))
|
|
|
|
break;
|
|
|
|
|
|
|
|
alloc_unbound.dom = DOMID_SELF;
|
|
|
|
alloc_unbound.remote_dom = bind.remote_domain;
|
|
|
|
rc = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
|
|
|
|
&alloc_unbound);
|
|
|
|
if (rc != 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
rc = evtchn_bind_to_user(u, alloc_unbound.port);
|
|
|
|
if (rc == 0)
|
|
|
|
rc = alloc_unbound.port;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case IOCTL_EVTCHN_UNBIND: {
|
|
|
|
struct ioctl_evtchn_unbind unbind;
|
2013-07-19 14:52:00 +00:00
|
|
|
struct user_evtchn *evtchn;
|
2009-02-07 03:21:19 +00:00
|
|
|
|
|
|
|
rc = -EFAULT;
|
|
|
|
if (copy_from_user(&unbind, uarg, sizeof(unbind)))
|
|
|
|
break;
|
|
|
|
|
|
|
|
rc = -EINVAL;
|
2013-09-23 20:03:38 +00:00
|
|
|
if (unbind.port >= xen_evtchn_nr_channels())
|
2009-02-07 03:21:19 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
rc = -ENOTCONN;
|
2013-07-19 14:52:00 +00:00
|
|
|
evtchn = find_evtchn(u, unbind.port);
|
|
|
|
if (!evtchn)
|
2009-02-07 03:21:19 +00:00
|
|
|
break;
|
|
|
|
|
2010-05-28 22:28:27 +00:00
|
|
|
disable_irq(irq_from_evtchn(unbind.port));
|
2013-07-19 14:52:00 +00:00
|
|
|
evtchn_unbind_from_user(u, evtchn);
|
2009-02-07 03:21:19 +00:00
|
|
|
rc = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case IOCTL_EVTCHN_NOTIFY: {
|
|
|
|
struct ioctl_evtchn_notify notify;
|
2013-07-19 14:52:00 +00:00
|
|
|
struct user_evtchn *evtchn;
|
2009-02-07 03:21:19 +00:00
|
|
|
|
|
|
|
rc = -EFAULT;
|
|
|
|
if (copy_from_user(¬ify, uarg, sizeof(notify)))
|
|
|
|
break;
|
|
|
|
|
2013-07-19 14:52:00 +00:00
|
|
|
rc = -ENOTCONN;
|
|
|
|
evtchn = find_evtchn(u, notify.port);
|
|
|
|
if (evtchn) {
|
2009-02-07 03:21:19 +00:00
|
|
|
notify_remote_via_evtchn(notify.port);
|
|
|
|
rc = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case IOCTL_EVTCHN_RESET: {
|
|
|
|
/* Initialise the ring to empty. Clear errors. */
|
|
|
|
mutex_lock(&u->ring_cons_mutex);
|
2013-07-19 14:52:00 +00:00
|
|
|
spin_lock_irq(&u->ring_prod_lock);
|
2009-02-07 03:21:19 +00:00
|
|
|
u->ring_cons = u->ring_prod = u->ring_overflow = 0;
|
2013-07-19 14:52:00 +00:00
|
|
|
spin_unlock_irq(&u->ring_prod_lock);
|
2009-02-07 03:21:19 +00:00
|
|
|
mutex_unlock(&u->ring_cons_mutex);
|
|
|
|
rc = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-07-11 14:45:51 +00:00
|
|
|
case IOCTL_EVTCHN_RESTRICT_DOMID: {
|
|
|
|
struct ioctl_evtchn_restrict_domid ierd;
|
|
|
|
|
|
|
|
rc = -EACCES;
|
|
|
|
if (u->restrict_domid != UNRESTRICTED_DOMID)
|
|
|
|
break;
|
|
|
|
|
|
|
|
rc = -EFAULT;
|
|
|
|
if (copy_from_user(&ierd, uarg, sizeof(ierd)))
|
|
|
|
break;
|
|
|
|
|
|
|
|
rc = -EINVAL;
|
|
|
|
if (ierd.domid == 0 || ierd.domid >= DOMID_FIRST_RESERVED)
|
|
|
|
break;
|
|
|
|
|
|
|
|
u->restrict_domid = ierd.domid;
|
|
|
|
rc = 0;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-02-07 03:21:19 +00:00
|
|
|
default:
|
|
|
|
rc = -ENOSYS;
|
|
|
|
break;
|
|
|
|
}
|
2009-02-12 21:03:24 +00:00
|
|
|
mutex_unlock(&u->bind_mutex);
|
2009-02-07 03:21:19 +00:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2017-07-03 10:39:46 +00:00
|
|
|
static __poll_t evtchn_poll(struct file *file, poll_table *wait)
|
2009-02-07 03:21:19 +00:00
|
|
|
{
|
2018-02-11 22:34:03 +00:00
|
|
|
__poll_t mask = EPOLLOUT | EPOLLWRNORM;
|
2009-02-07 03:21:19 +00:00
|
|
|
struct per_user_data *u = file->private_data;
|
|
|
|
|
|
|
|
poll_wait(file, &u->evtchn_wait, wait);
|
|
|
|
if (u->ring_cons != u->ring_prod)
|
2018-02-11 22:34:03 +00:00
|
|
|
mask |= EPOLLIN | EPOLLRDNORM;
|
2009-02-07 03:21:19 +00:00
|
|
|
if (u->ring_overflow)
|
2018-02-11 22:34:03 +00:00
|
|
|
mask = EPOLLERR;
|
2009-02-07 03:21:19 +00:00
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int evtchn_fasync(int fd, struct file *filp, int on)
|
|
|
|
{
|
|
|
|
struct per_user_data *u = filp->private_data;
|
|
|
|
return fasync_helper(fd, filp, on, &u->evtchn_async_queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int evtchn_open(struct inode *inode, struct file *filp)
|
|
|
|
{
|
|
|
|
struct per_user_data *u;
|
|
|
|
|
|
|
|
u = kzalloc(sizeof(*u), GFP_KERNEL);
|
|
|
|
if (u == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
u->name = kasprintf(GFP_KERNEL, "evtchn:%s", current->comm);
|
|
|
|
if (u->name == NULL) {
|
|
|
|
kfree(u);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
init_waitqueue_head(&u->evtchn_wait);
|
|
|
|
|
2009-02-12 21:03:24 +00:00
|
|
|
mutex_init(&u->bind_mutex);
|
2009-02-07 03:21:19 +00:00
|
|
|
mutex_init(&u->ring_cons_mutex);
|
2013-07-19 14:52:00 +00:00
|
|
|
spin_lock_init(&u->ring_prod_lock);
|
2009-02-07 03:21:19 +00:00
|
|
|
|
2016-07-11 14:45:51 +00:00
|
|
|
u->restrict_domid = UNRESTRICTED_DOMID;
|
|
|
|
|
2009-02-07 03:21:19 +00:00
|
|
|
filp->private_data = u;
|
|
|
|
|
2019-03-26 20:51:19 +00:00
|
|
|
return stream_open(inode, filp);
|
2009-02-07 03:21:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int evtchn_release(struct inode *inode, struct file *filp)
|
|
|
|
{
|
|
|
|
struct per_user_data *u = filp->private_data;
|
2013-07-19 14:52:00 +00:00
|
|
|
struct rb_node *node;
|
2009-02-07 03:21:19 +00:00
|
|
|
|
2013-07-19 14:52:00 +00:00
|
|
|
while ((node = u->evtchns.rb_node)) {
|
|
|
|
struct user_evtchn *evtchn;
|
2009-02-07 03:21:19 +00:00
|
|
|
|
2013-07-19 14:52:00 +00:00
|
|
|
evtchn = rb_entry(node, struct user_evtchn, node);
|
|
|
|
disable_irq(irq_from_evtchn(evtchn->port));
|
|
|
|
evtchn_unbind_from_user(u, evtchn);
|
2010-05-28 22:28:27 +00:00
|
|
|
}
|
|
|
|
|
2015-11-26 16:14:35 +00:00
|
|
|
evtchn_free_ring(u->ring);
|
2009-02-07 03:21:19 +00:00
|
|
|
kfree(u->name);
|
|
|
|
kfree(u);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct file_operations evtchn_fops = {
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.read = evtchn_read,
|
|
|
|
.write = evtchn_write,
|
|
|
|
.unlocked_ioctl = evtchn_ioctl,
|
|
|
|
.poll = evtchn_poll,
|
|
|
|
.fasync = evtchn_fasync,
|
|
|
|
.open = evtchn_open,
|
|
|
|
.release = evtchn_release,
|
2010-11-19 06:32:17 +00:00
|
|
|
.llseek = no_llseek,
|
2009-02-07 03:21:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct miscdevice evtchn_miscdev = {
|
|
|
|
.minor = MISC_DYNAMIC_MINOR,
|
2010-05-28 22:43:49 +00:00
|
|
|
.name = "xen/evtchn",
|
2009-02-07 03:21:19 +00:00
|
|
|
.fops = &evtchn_fops,
|
|
|
|
};
|
|
|
|
static int __init evtchn_init(void)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (!xen_domain())
|
|
|
|
return -ENODEV;
|
|
|
|
|
2013-02-18 14:57:59 +00:00
|
|
|
/* Create '/dev/xen/evtchn'. */
|
2009-02-07 03:21:19 +00:00
|
|
|
err = misc_register(&evtchn_miscdev);
|
|
|
|
if (err != 0) {
|
2013-06-28 10:21:41 +00:00
|
|
|
pr_err("Could not register /dev/xen/evtchn\n");
|
2009-02-07 03:21:19 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2013-06-28 10:21:41 +00:00
|
|
|
pr_info("Event-channel device installed\n");
|
2009-02-07 03:21:19 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit evtchn_cleanup(void)
|
|
|
|
{
|
|
|
|
misc_deregister(&evtchn_miscdev);
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(evtchn_init);
|
|
|
|
module_exit(evtchn_cleanup);
|
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|