irqfd
v3 cover irqfd
(Applies to kvm.git 41b76d8d0487c26d6d4d3fe53c1ff59b3236f096)
This series implements a mechanism called "irqfd". It lets you create an eventfd based file-desriptor to inject interrupts to a kvm guest. We associate one gsi per fd for fine-grained routing.
[ Changelog:
v3:
*) The kernel now allocates the eventfd (need to export sys_eventfd2) *) Added a flags field for future expansion to kvm_irqfd() *) We properly toggle the irq level 1+0.
*) We re-use the USERSPACE_SRC_ID instead of creating our own *) Properly check for failures establishing a poll-table with eventfd *) Fixed fd/file leaks on failure
*) Rebased to lateste kvm.git::41b76d8d04
v2:
*) Dropped notifier_chain based callbacks in favor of
wait_queue_t::func and file::poll based callbacks (Thanks to Davide for the suggestion)
v1:
*) Initial release
--------
We do not have a user of this interface in this series, though note future version of virtual-bus (v4 and above) will be based on this.
The first patch will require mainline buy-in, particularly from Davide (cc'd). The last patch is kvm specific.
qemu-kvm.git patch to follow.
-Greg
---
Gregory Haskins (2):
kvm: add support for irqfd via eventfd-notification interface
eventfd: export fget and signal interfaces for module use
arch/x86/kvm/Makefile | 2 - arch/x86/kvm/x86.c | 1 fs/eventfd.c | 4 + include/linux/kvm.h | 7 ++ include/linux/kvm_host.h | 4 + virt/kvm/irqfd.c | 158 ++++++++++++++++++++++++++++++++++++++++++++++ virt/kvm/kvm_main.c | 11 +++ 7 files changed, 186 insertions(+), 1 deletions(-) create mode 100644 virt/kvm/irqfd.c
Signature · 1 line
Signature
PATCH v3 1/2 eventfd: export fget and signal interfaces for module use
We will use this later in the series
---
fs/eventfd.c | 4 ++++ 1 files changed, 4 insertions(+), 0 deletions(-)
fs/eventfd.c +4 -0
@@ -16,6 +16,7 @@#include <linux/anon_inodes.h> #include <linux/eventfd.h> #include <linux/syscalls.h> +#include <linux/module.h> struct eventfd_ctx {wait_queue_head_t wqh;
@@ -56,6 +57,7 @@ int eventfd_signal(struct file *file, int n)return n; } +EXPORT_SYMBOL_GPL (eventfd_signal);static int eventfd_release (struct inode *inode,struct file *file) {
@@ -197,6 +199,7 @@ struct file *eventfd_fget(int fd)return file; } +EXPORT_SYMBOL_GPL (eventfd_fget); SYSCALL_DEFINE2(eventfd2, unsigned int, count, int, flags) {
@@ -228,6 +231,7 @@ SYSCALL_DEFINE2(eventfd2, unsigned int, count, int, flags)kfree (ctx);return fd; } +EXPORT_SYMBOL_GPL (sys_eventfd2); SYSCALL_DEFINE1(eventfd, unsigned int, count) {
PATCH v3 2/2 kvm: add support for irqfd via eventfd-notification interface
In reply to Gregory HaskinsThis allows an eventfd to be registered as an irq source with a guest. Any signaling operation on the eventfd (via userspace or kernel) will inject the registered GSI at the next available window.
---
arch/x86/kvm/Makefile | 2 - arch/x86/kvm/x86.c | 1 include/linux/kvm.h | 7 ++ include/linux/kvm_host.h | 4 + virt/kvm/irqfd.c | 158 ++++++++++++++++++++++++++++++++++++++++++++++ virt/kvm/kvm_main.c | 11 +++ 6 files changed, 182 insertions(+), 1 deletions(-) create mode 100644 virt/kvm/irqfd.c
arch/x86/kvm/Makefile +1 -1
@@ -3,7 +3,7 @@ # common-objs = $(addprefix ../../../virt/kvm/, kvm_main.o ioapic.o \ - coalesced_mmio.o irq_comm.o) + coalesced_mmio.o irq_comm.o irqfd.o) ifeq ($(CONFIG_KVM_TRACE),y) common-objs += $(addprefix ../../../virt/kvm/, kvm_trace.o) endif
arch/x86/kvm/x86.c +1 -0
@@ -1027,6 +1027,7 @@ int kvm_dev_ioctl_check_extension(long ext)case KVM_CAP_REINJECT_CONTROL :case KVM_CAP_IRQ_INJECT_STATUS :case KVM_CAP_ASSIGN_DEV_IRQ : +case KVM_CAP_IRQFD : r =1 ;break ;case KVM_CAP_COALESCED_MMIO :
include/linux/kvm.h +7 -0
@@ -415,6 +415,7 @@ struct kvm_trace_rec {#define KVM_CAP_ASSIGN_DEV_IRQ 29/* Another bug in KVM_SET_USER_MEMORY_REGION fixed: */ #define KVM_CAP_JOIN_MEMORY_REGIONS_WORKS 30 +#define KVM_CAP_IRQFD 31#ifdef KVM_CAP_IRQ_ROUTING
@@ -454,6 +455,11 @@ struct kvm_irq_routing {#endif +struct kvm_irqfd { +__u32 gsi; +__u32 flags; +}; +/* * ioctls for VM fds */
@@ -498,6 +504,7 @@ struct kvm_irq_routing {#define KVM_ASSIGN_SET_MSIX_ENTRY \ _IOW(KVMIO, 0x74, struct kvm_assigned_msix_entry)#define KVM_DEASSIGN_DEV_IRQ _IOW(KVMIO, 0x75, struct kvm_assigned_irq) +#define KVM_IRQFD _IOW(KVMIO, 0x76, struct kvm_irqfd)/* * ioctls for vcpu fds
include/linux/kvm_host.h +4 -0
@@ -134,6 +134,7 @@ struct kvm {struct list_head vm_list;struct kvm_io_bus mmio_bus;struct kvm_io_bus pio_bus; +struct list_head irqfds;struct kvm_vm_stat stat;struct kvm_arch arch;atomic_t users_count;
@@ -524,4 +525,7 @@ static inline void kvm_free_irq_routing(struct kvm *kvm) {}#endif +int kvm_irqfd (struct kvm *kvm,int gsi,int flags); +void kvm_irqfd_release (struct kvm *kvm); +#endif
virt/kvm/irqfd.c new file +158 -0
158 lines not on this page · raw message
virt/kvm/kvm_main.c +11 -0
@@ -972,6 +972,7 @@ static struct kvm *kvm_create_vm(void)atomic_inc (&kvm->mm->mm_count);spin_lock_init (&kvm->mmu_lock);kvm_io_bus_init (&kvm->pio_bus); +INIT_LIST_HEAD (&kvm->irqfds);mutex_init (&kvm->lock);kvm_io_bus_init (&kvm->mmio_bus);init_rwsem (&kvm->slots_lock);
@@ -1023,6 +1024,7 @@ static void kvm_destroy_vm(struct kvm *kvm)spin_lock (&kvm_lock);list_del (&kvm->vm_list);spin_unlock (&kvm_lock); +kvm_irqfd_release (kvm);kvm_free_irq_routing (kvm);kvm_io_bus_destroy (&kvm->pio_bus);kvm_io_bus_destroy (&kvm->mmio_bus);
@@ -2197,6 +2199,15 @@ static long kvm_vm_ioctl(struct file *filp, }#endif #endif /* KVM_CAP_IRQ_ROUTING */ +case KVM_IRQFD : { +struct kvm_irqfd data; + + r = -EFAULT ; +if (copy_from_user (&data, argp,sizeof data)) + goto out; + r =kvm_irqfd (kvm, data.gsi, data.flags); +break ; + }default : r =kvm_arch_vm_ioctl (filp, ioctl, arg); }
Re: [KVM PATCH v3 2/2] kvm: add support for irqfd via eventfd-notification interface
Quoted 5 lines · Gregory Haskins
This allows an eventfd to be registered as an irq source with a guest. Any signaling operation on the eventfd (via userspace or kernel) will inject the registered GSI at the next available window.
Signed-off-by Gregory Haskins <[email protected]>
If we ever want to use this with e.g. MSI-X emulation in guest, and want to be stricly compliant to MSI-X, we'll need a way for guest to mask interrupts, and for host to report that a masked interrupt is pending. Ideally, all this will be doable with a couple of mmapped pages to avoid vmexits/system calls.
virt/kvm/irqfd.c line 54
Quoted 10 lines
virt/kvm/irqfd.c new file +10
-0+static void +irqfd_inject (struct work_struct *work) +{ + struct _irqfd *irqfd = container_of(work, struct _irqfd, work); +struct kvm *kvm = irqfd->kvm; + +mutex_lock (&kvm->lock); +kvm_set_irq (kvm,KVM_USERSPACE_IRQ_SOURCE_ID , irqfd->gsi,1 ); +kvm_set_irq (kvm,KVM_USERSPACE_IRQ_SOURCE_ID , irqfd->gsi,0 ); +mutex_unlock (&kvm->lock);
This will do weird stuff (deliver the irq twice) if the irq is MSI/MSI-X. I know this was discussed already and is a temporary shortcut, but maybe add a comment that we really want kvm_toggle_irq, so that we won't forget?
Quoted 2 lines
(quoted diff) +2
-0+} +
Signature · 1 line
MST
Re: [KVM PATCH v3 2/2] kvm: add support for irqfd via eventfd-notification interface
Quoted 6 lines · Michael S. Tsirkin
Quoted 4 lines · Gregory Haskins
This allows an eventfd to be registered as an irq source with a guest. Any signaling operation on the eventfd (via userspace or kernel) will inject the registered GSI at the next available window.
Quoted 10 lines
Quoted 2 lines
Signed-off-by Gregory Haskins <[email protected]>If we ever want to use this with e.g. MSI-X emulation in guest, and want to be stricly compliant to MSI-X, we'll need a way for guest to mask interrupts, and for host to report that a masked interrupt is pending. Ideally, all this will be doable with a couple of mmapped pages to avoid vmexits/system calls.
We could do this in two ways:
- move msix entry emulation into the kernel
- require the device to support replacing its irqfd, and juggle it like so:
- guest disables msi
- replace device model fd with eventfd belonging to us
- when the device fires its eventfd, set the irq pending bit
- guest enables msi
- if the pending bit is set, fire the interrupt?
- replace device model fd with the real irqfd
I'm leaning towards the latter, though it's not an easy call.
Quoted 8 lines
Quoted 8 lines
virt/kvm/irqfd.c new file +8
-0+static void +irqfd_inject (struct work_struct *work) +{ + struct _irqfd *irqfd = container_of(work, struct _irqfd, work); +struct kvm *kvm = irqfd->kvm; + +mutex_lock (&kvm->lock); +kvm_set_irq (kvm,KVM_USERSPACE_IRQ_SOURCE_ID , irqfd->gsi,1 );
Quoted 9 lines
virt/kvm/irqfd.c line 54
Quoted 3 lines
virt/kvm/irqfd.c new file +2
-0+kvm_set_irq (kvm,KVM_USERSPACE_IRQ_SOURCE_ID , irqfd->gsi,0 ); +mutex_unlock (&kvm->lock);This will do weird stuff (deliver the irq twice) if the irq is MSI/MSI-X. I know this was discussed already and is a temporary shortcut, but maybe add a comment that we really want
kvm_toggle_irq, so that we won't forget?
If so, that's a bug. MSI should ignore kvm_set_irq(..., 0).
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [KVM PATCH v3 2/2] kvm: add support for irqfd via eventfd-notification interface
Quoted 18 lines · Avi Kivity
Quoted 6 lines · Michael S. Tsirkin
Quoted 4 lines · Gregory Haskins
This allows an eventfd to be registered as an irq source with a guest. Any signaling operation on the eventfd (via userspace or kernel) will inject the registered GSI at the next available window.
Quoted 10 lines
Quoted 2 lines
Signed-off-by Gregory Haskins <[email protected]>If we ever want to use this with e.g. MSI-X emulation in guest, and want to be stricly compliant to MSI-X, we'll need a way for guest to mask interrupts, and for host to report that a masked interrupt is pending. Ideally, all this will be doable with a couple of mmapped pages to avoid vmexits/system calls.
Quoted 3 lines
We could do this in two ways:
- move msix entry emulation into the kernel
It's not too bad IMO: MSIX is just a table with a list of vectors, you check the mask bit on each interrupt, if masked mark vector pending and poll until unmasked.
Quoted 7 lines
- require the device to support replacing its irqfd, and juggle it like so:
- guest disables msi
- replace device model fd with eventfd belonging to us
- when the device fires its eventfd, set the irq pending bit
- guest enables msi
- if the pending bit is set, fire the interrupt?
- replace device model fd with the real irqfd
Looks like a lot of code. No?
Quoted 1 line
I'm leaning towards the latter, though it's not an easy call.
Actually there's a third option: add KVM_MASK_IRQ, KVM_UNMASK_IRQ ioctls which will block/unblock guest from getting interrupt on this irq, whatever the source. Interrupts are queued in kernel while masked. A third ioctl KVM_PENDING_IRQS will return the status for a set if IRQs. qemu would call these ioctls when guest edits the MSIX vector control or reads the pending bit array.
Quoted 19 lines
Quoted 8 lines
Quoted 8 lines
virt/kvm/irqfd.c new file +8
-0+static void +irqfd_inject (struct work_struct *work) +{ + struct _irqfd *irqfd = container_of(work, struct _irqfd, work); +struct kvm *kvm = irqfd->kvm; + +mutex_lock (&kvm->lock); +kvm_set_irq (kvm,KVM_USERSPACE_IRQ_SOURCE_ID , irqfd->gsi,1 );Quoted 9 lines
virt/kvm/irqfd.c line 54
Quoted 3 lines
virt/kvm/irqfd.c new file +2
-0+kvm_set_irq (kvm,KVM_USERSPACE_IRQ_SOURCE_ID , irqfd->gsi,0 ); +mutex_unlock (&kvm->lock);This will do weird stuff (deliver the irq twice) if the irq is MSI/MSI-X. I know this was discussed already and is a temporary shortcut, but maybe add a comment that we really want
kvm_toggle_irq, so that we won't forget?If so, that's a bug. MSI should ignore
kvm_set_irq(..., 0).
Signature · 1 line
MST
Re: [KVM PATCH v3 2/2] kvm: add support for irqfd via eventfd-notification interface
Quoted 22 lines · Michael S. Tsirkin
Quoted 20 lines · Avi Kivity
Quoted 18 lines · Michael S. Tsirkin
Quoted 7 lines · Gregory Haskins
This allows an eventfd to be registered as an irq source with a guest. Any signaling operation on the eventfd (via userspace or kernel) will inject the registered GSI at the next available window.
Signed-off-by Gregory Haskins <[email protected]>If we ever want to use this with e.g. MSI-X emulation in guest, and want to be stricly compliant to MSI-X, we'll need a way for guest to mask interrupts, and for host to report that a masked interrupt is pending. Ideally, all this will be doable with a couple of mmapped pages to avoid vmexits/system calls.
Quoted 9 lines
Quoted 4 lines
We could do this in two ways:
- move msix entry emulation into the kernel
It's not too bad IMO: MSIX is just a table with a list of vectors, you check the mask bit on each interrupt, if masked mark vector pending and poll until unmasked.
Right, but it's more and more core, and more and more bugs. Bugs in the kernel are more expensive to fix and get to users.
Quoted 10 lines
Quoted 9 lines
- require the device to support replacing its irqfd, and juggle it like so:
- guest disables msi
- replace device model fd with eventfd belonging to us
- when the device fires its eventfd, set the irq pending bit
- guest enables msi
- if the pending bit is set, fire the interrupt?
- replace device model fd with the real irqfd
Quoted 2 lines
Looks like a lot of code. No?
We'll need exactly the same code if we do it in the kernel. The only addition is replacing the fd.
Quoted 10 lines
Quoted 2 lines
I'm leaning towards the latter, though it's not an easy call.
Actually there's a third option: add
KVM_MASK_IRQ,KVM_UNMASK_IRQioctls which will block/unblock guest from getting interrupt on this irq, whatever the source. Interrupts are queued in kernel while masked. A third ioctlKVM_PENDING_IRQSwill return the status for a set if IRQs. qemu would call these ioctls when guest edits the MSIX vector control or reads the pending bit array.
I think this is the best option.
Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
Re: [KVM PATCH v3 2/2] kvm: add support for irqfd via eventfd-notification interface
Quoted 9 lines · Avi Kivity
Quoted 7 lines
Actually there's a third option: add
KVM_MASK_IRQ,KVM_UNMASK_IRQioctls which will block/unblock guest from getting interrupt on this irq, whatever the source. Interrupts are queued in kernel while masked. A third ioctlKVM_PENDING_IRQSwill return the status for a set if IRQs. qemu would call these ioctls when guest edits the MSIX vector control or reads the pending bit array.I think this is the best option.
Sounds good.
Signature · 1 line
MST
Al Viro
maintainerRe: [KVM PATCH v3 2/2] kvm: add support for irqfd via eventfd-notification interface
In reply to Gregory HaskinsQuoted 5 lines · Gregory Haskins
virt/kvm/irqfd.c new file +5
-0+/* We re-use eventfd for irqfd */ + fd =sys_eventfd2 (0 ,0 ); +if (fd <0 ) { + ret = fd; + goto fail;
virt/kvm/irqfd.c line 114
Quoted 10 lines
virt/kvm/irqfd.c new file +10
-0+ } + +/* We maintain a reference to eventfd for the irqfd lifetime */ + file =eventfd_fget (fd); +if (IS_ERR (file)) { + ret =PTR_ERR (file); + goto fail; + } + + irqfd->file = file;
This is just plain wrong. You have no promise whatsoever that caller of that sucker won't race with e.g. dup2(). IOW, you can't assume that file will be of the expected kind.
Re: [KVM PATCH v3 2/2] kvm: add support for irqfd via eventfd-notification interface
Quoted 12 lines · Al Viro
Quoted 11 lines · Gregory Haskins
virt/kvm/irqfd.c new file +11
-0+/* We re-use eventfd for irqfd */ + fd =sys_eventfd2 (0 ,0 ); +if (fd <0 ) { + ret = fd; + goto fail; + } + +/* We maintain a reference to eventfd for the irqfd lifetime */ + file =eventfd_fget (fd); +if (IS_ERR (file)) { + ret =PTR_ERR (file);
Quoted 8 lines
virt/kvm/irqfd.c line 114
Quoted 4 lines
virt/kvm/irqfd.c new file +4
-0+ goto fail; + } + + irqfd->file = file;This is just plain wrong. You have no promise whatsoever that caller of that sucker won't race with e.g.
dup2(). IOW, you can't assume that file will be of the expected kind.
The eventfd_fget() checks for the file_operations pointer, before returning the file*, and fails if the fd in not an eventfd. Or you have other concerns?
- Davide
Al Viro
maintainerRe: [KVM PATCH v3 2/2] kvm: add support for irqfd via eventfd-notification interface
Quoted 22 lines · Davide Libenzi
Quoted 12 lines · Al Viro
Quoted 11 lines · Gregory Haskins
virt/kvm/irqfd.c new file +11
-0+/* We re-use eventfd for irqfd */ + fd =sys_eventfd2 (0 ,0 ); +if (fd <0 ) { + ret = fd; + goto fail; + } + +/* We maintain a reference to eventfd for the irqfd lifetime */ + file =eventfd_fget (fd); +if (IS_ERR (file)) { + ret =PTR_ERR (file);Quoted 8 lines
virt/kvm/irqfd.c line 114
Quoted 4 lines
virt/kvm/irqfd.c new file +4
-0+ goto fail; + } + + irqfd->file = file;This is just plain wrong. You have no promise whatsoever that caller of that sucker won't race with e.g.
dup2(). IOW, you can't assume that file will be of the expected kind.
Quoted 3 lines
The
eventfd_fget()checks for thefile_operationspointer, before returning the file*, and fails if the fd in not an eventfd. Or you have other concerns?
OK, but... it's still wrong. Descriptor numbers are purely for interaction with userland; using them that way violates very general race-prevention rules, even if you do paper over the worst of consequences with check in eventfd_fget().
General rules:
* descriptor you've generated is fit only for return to userland;
* descriptor you've got from userland is fit only for *single*fget() or equivalent, unless you are one of the core syscalls manipulating the descriptor table itself (dup2, etc.)
* once file is installed in descriptor table, you'd better be past
the last failure exit; sys_close() on cleanup path is not acceptable. That's what reserving descriptors is for.
IOW, the sane solution would be to export something that returns your struct file *. And stop playing with fd completely.
Al Viro
maintainerfile descriptor abuses
Quoted 11 lines · Al Viro
General rules:
* descriptor you've generated is fit only for return to userland;
* descriptor you've got from userland is fit only for *single*fget()or equivalent, unless you are one of the core syscalls manipulating the descriptor table itself (dup2, etc.)
* once file is installed in descriptor table, you'd better be past
the last failure exit;sys_close()on cleanup path is not acceptable. That's what reserving descriptors is for.IOW, the sane solution would be to export something that returns your struct file *. And stop playing with fd completely.
Speaking of which, quick look through fget() callers shows this turd:
static int p9_socket_open(struct p9_client *client, struct socket *csocket)
{
int fd, ret;
fd = sock_map_fd(csocket, 0);
.....
ret = p9_fd_open(client, fd, fd);
if (ret < 0) {
P9_EPRINTK(KERN_ERR, "p9_socket_open: failed to open fd\n");
sockfd_put(csocket);
return ret;
}
.....
return 0;
}where p9_fd_open() calls fget() on its 2nd and 3rd arguments. Which does worse than just a leak, AFAICT - on failure exit it leaves a dangling pointer from descriptor table.
On the almost unrelated note, we have (in drivers/sneak_in^Wstaging/usbip) sockfd_to_socket(), with all callers leaking struct file, AFAICS.
Re: [KVM PATCH v3 2/2] kvm: add support for irqfd via eventfd-notification interface
In reply to Al ViroQuoted 2 lines · Al Viro
IOW, the sane solution would be to export something that returns your struct file *. And stop playing with fd completely.
This builds but it's not tested at all.
- Make all the work of the old anon_inode_getfd(), done by a new anon_inode_getfile(), with anon_inode_getfd() using its services
- Make all the work done by sys_eventfd(), done by a new eventfd_file_create() (which in turn uses anon_inode_getfile()), with sys_eventfd() using its services
IRQfd can use eventfd_file_create(), fget(), get_unused_fd_flags() and fd_install() just before returning.
Is that what you had in mind?
- Davide
--- fs/anon_inodes.c | 68 +++++++++++++++++++++++++++++++++----------- fs/eventfd.c | 44 +++++++++++++++++++++------- include/linux/anon_inodes.h | 3 + include/linux/eventfd.h | 6 +++ 4 files changed, 92 insertions(+), 29 deletions(-)
fs/anon_inodes.c +51 -17
@@ -64,28 +64,24 @@ static const struct dentry_operations an* * Creates a new file by hooking it on a single inode. This is useful for files * that do not need to have a full-fledged inode in order to operate correctly. -* All the files created with anon_inode_getfd() will share a single inode, +* All the files created with anon_inode_getfile() will share a single inode, * hence saving memory and avoiding code duplication for the file/inode/dentry -* setup. Returns new descriptor or -error. +* setup. Returns the newly created file* or error. */ -int anon_inode_getfd (const char *name,const struct file_operations *fops, -void *priv,int flags) +struct file *anon_inode_getfile (const char *name, +const struct file_operations *fops, +void *priv,int flags) {struct qstr this;struct dentry *dentry;struct file *file; -int error, fd; +int error;if (IS_ERR (anon_inode_inode)) -return -ENODEV ; +return ERR_PTR (-ENODEV );if (fops->owner && !try_module_get (fops->owner)) -return -ENOENT ; - - error =get_unused_fd_flags (flags); -if (error <0 ) - goto err_module; - fd = error; +return ERR_PTR (-ENOENT );/* * Link the inode to a directory entry by creating a unique name
@@ -97,7 +93,7 @@ int anon_inode_getfd(const char *name, c this.hash =0 ; dentry =d_alloc (anon_inode_mnt->mnt_sb->s_root, &this);if (!dentry) - goto err_put_unused_fd; + goto err_module;/* * We know the anon_inode inode count is always greater than zero,
@@ -123,16 +119,54 @@ int anon_inode_getfd(const char *name, c file->f_version =0 ; file->private_data = priv; +return file; + +err_dput: +dput (dentry); +err_module: +module_put (fops->owner); +return ERR_PTR (error); +} +EXPORT_SYMBOL_GPL (anon_inode_getfile); + +/** +* anon_inode_getfd - creates a new file instance by hooking it up to an +* anonymous inode, and a dentry that describe the "class" +* of the file +* +* @name: [in] name of the "class" of the new file +* @fops: [in] file operations for the new file +* @priv: [in] private data for the new file (will be file's private_data) +* @flags: [in] flags +* +* Creates a new file by hooking it on a single inode. This is useful for files +* that do not need to have a full-fledged inode in order to operate correctly. +* All the files created with anon_inode_getfd() will share a single inode, +* hence saving memory and avoiding code duplication for the file/inode/dentry +* setup. Returns new descriptor or -error. +*/ +int anon_inode_getfd (const char *name,const struct file_operations *fops, +void *priv,int flags) +{ +int error, fd; +struct file *file; + + error =get_unused_fd_flags (flags); +if (error <0 ) +return error; + fd = error; + + file =anon_inode_getfile (name, fops, priv, flags); +if (IS_ERR (file)) { + error =PTR_ERR (file); + goto err_put_unused_fd; + }fd_install (fd, file);return fd; -err_dput: -dput (dentry); err_put_unused_fd:put_unused_fd (fd); -err_module: -module_put (fops->owner);return error; }EXPORT_SYMBOL_GPL (anon_inode_getfd);
fs/eventfd.c +33 -11
67 lines not on this page · raw message
include/linux/anon_inodes.h +3 -0
@@ -8,6 +8,9 @@#ifndef _LINUX_ANON_INODES_H#define _LINUX_ANON_INODES_H +struct file *anon_inode_getfile (const char *name, +const struct file_operations *fops, +void *priv,int flags);int anon_inode_getfd (const char *name,const struct file_operations *fops,void *priv,int flags);
include/linux/eventfd.h +5 -1
@@ -29,12 +29,16 @@struct file *eventfd_fget (int fd);int eventfd_signal (struct file *file,int n); +struct file *eventfd_file_create (unsigned int count,int flags);#else /* CONFIG_EVENTFD */ -#define eventfd_fget (fd) ERR_PTR(-ENOSYS) +static inline struct file *eventfd_fget (int fd) +{ERR_PTR (-ENOSYS ); }static inline int eventfd_signal (struct file *file,int n) {return 0 ; } +struct file *eventfd_file_create (unsigned int count,int flags) +{ERR_PTR (-ENOSYS ); }#endif /* CONFIG_EVENTFD */
Al Viro
maintainerRe: [KVM PATCH v3 2/2] kvm: add support for irqfd via eventfd-notification interface
Quoted 10 lines · Davide Libenzi
Quoted 2 lines · Al Viro
IOW, the sane solution would be to export something that returns your struct file *. And stop playing with fd completely.
This builds but it's not tested at all.
- Make all the work of the old
anon_inode_getfd(), done by a newanon_inode_getfile(), withanon_inode_getfd()using its services
Quoted 7 lines
- Make all the work done by
sys_eventfd(), done by a neweventfd_file_create()(which in turn usesanon_inode_getfile()), withsys_eventfd()using its servicesIRQfd can use
eventfd_file_create(),fget(),get_unused_fd_flags()andfd_install()just before returning.
Is that what you had in mind?
More or less, but I'd like to see the irqfd side of that...
Quoted 5 lines · On Mon, 27 Apr 2009 14:33:24 -0400 Gregory Haskins <[email protected]> wrote:
(Applies to kvm.git
41b76d8d0487c26d6d4d3fe53c1ff59b3236f096)This series implements a mechanism called "irqfd". It lets you create an eventfd based file-desriptor to inject interrupts to a kvm guest. We associate one gsi per fd for fine-grained routing.
It'd be nice if the KVM weenies amongst us were to be told why one would want to inject interrupts into a KVM guest. Monosyllabic words would be preferred ;)
Quoted 2 lines
We do not have a user of this interface in this series, though note future version of virtual-bus (v4 and above) will be based on this.
So I assume that this patchset will be merged if/when a user of it is merged?
Quoted 2 lines
The first patch will require mainline buy-in, particularly from Davide (cc'd). The last patch is kvm specific.
Three EXPORT_SYMBOL_GPL()s. Once the shouting has subsided I'd suggest that this be merged via the KVM tree.
Quoted 5 lines · Andrew Morton
Quoted 2 lines · On Mon, 27 Apr 2009 14:33:24 -0400 Gregory Haskins <[email protected]> wrote:
(Applies to kvm.git
41b76d8d0487c26d6d4d3fe53c1ff59b3236f096)
Quoted 9 lines
Quoted 4 lines
This series implements a mechanism called "irqfd". It lets you create an eventfd based file-desriptor to inject interrupts to a kvm guest. We associate one gsi per fd for fine-grained routing.
It'd be nice if the KVM weenies amongst us were to be told why one would want to inject interrupts into a KVM guest. Monosyllabic words would be preferred ;)
Interrupts are injected (better word, raised) into a guest because real hardware has interrupts. This patchset does not add the ability to raise interrupts (that existed from day 1); it adds an eventfd interface to do so.
An eventfd interface is useful, because it allows components to talk to kvm guests without being tied to kvm internals; they signal an eventfd; if the eventfd is terminated in kvm, it injects an interrupt. If the eventfd is terminated in userspace, it returns from epoll().
Quoted 7 lines
Quoted 3 lines
We do not have a user of this interface in this series, though note future version of virtual-bus (v4 and above) will be based on this.
So I assume that this patchset will be merged if/when a user of it is merged?
This interface is applicable to both the kernel and userspace; userspace users won't be merged.
But I certainly want to see how the whole thing works.
Quoted 7 lines
Quoted 3 lines
The first patch will require mainline buy-in, particularly from Davide (cc'd). The last patch is kvm specific.
Three EXPORT_SYMBOL_GPL()s. Once the shouting has subsided I'd suggest that this be merged via the KVM tree.
I think eventfd makes tons of sense as a generic 'wake me up' mechanism that can be used from both sides of the kernel/user line.
Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this signature is too narrow to contain.