Skip to contentSkip to conversation

xen: add new hypercall buffer mapping device

PATCH Superseded Juergen Gross 4 participants 13 messages Jun 15, 2018 1 message not in the archive Xen hypervisor interface

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Juergen Gross

author maintainer
#0 lore raw

PATCH 1/1 xen: add new hypercall buffer mapping device

For passing arbitrary data from user land to the Xen hypervisor the Xen tools today are using mlock()ed buffers. Unfortunately the kernel might change access rights of such buffers for brief periods of time e.g. for page migration or compaction, leading to access faults in the hypervisor, as the hypervisor can't use the locks of the kernel.

In order to solve this problem add a new device node to the Xen privcmd driver to easily allocate hypercall buffers via mmap(). The memory is allocated in the kernel and just mapped into user space. Marked as VM_IO the user mapping will not be subject to page migration et al.

---
 drivers/xen/Makefile      |   2 +-
 drivers/xen/privcmd-buf.c | 216 ++++++++++++++++++++++++++++++++++++++++++++++
 drivers/xen/privcmd.c     |   9 ++
 drivers/xen/privcmd.h     |   3 +
 drivers/xen/xenfs/super.c |   2 +
 5 files changed, 231 insertions(+), 1 deletion(-)
 create mode 100644 drivers/xen/privcmd-buf.c
drivers/xen/Makefile +1 -1
index 451e833f5931..48b154276179 100644
@@ -41,4 +41,4 @@ obj-$(CONFIG_XEN_PVCALLS_FRONTEND)	+= pvcalls-front.o
 xen-evtchn-y				:= evtchn.o
 xen-gntdev-y				:= gntdev.o
 xen-gntalloc-y				:= gntalloc.o
-xen-privcmd-y				:= privcmd.o
+xen-privcmd-y				:= privcmd.o privcmd-buf.o
drivers/xen/privcmd-buf.c new file +216 -0

216 lines not on this page · raw message

drivers/xen/privcmd.c +9 -0
index 8ae0349d9f0a..7e6e682104dc 100644
@@ -1007,12 +1007,21 @@ static int __init privcmd_init(void)
 		pr_err("Could not register Xen privcmd device\n");
 		return err;
 	}
+
+	err = misc_register(&xen_privcmdbuf_dev);
+	if (err != 0) {
+		pr_err("Could not register Xen hypercall-buf device\n");
+		misc_deregister(&privcmd_dev);
+		return err;
+	}
+
 	return 0;
 }
 
 static void __exit privcmd_exit(void)
 {
 	misc_deregister(&privcmd_dev);
+	misc_deregister(&xen_privcmdbuf_dev);
 }
 
 module_init(privcmd_init);
drivers/xen/privcmd.h +3 -0
index 14facaeed36f..0dd9f8f67ee3 100644
@@ -1,3 +1,6 @@
 #include <linux/fs.h>
 
 extern const struct file_operations xen_privcmd_fops;
+extern const struct file_operations xen_privcmdbuf_fops;
+
+extern struct miscdevice xen_privcmdbuf_dev;
drivers/xen/xenfs/super.c +2 -0
index 71ddfb4cf61c..d752d0dd3d1d 100644
@@ -48,6 +48,7 @@ static int xenfs_fill_super(struct super_block *sb, void *data, int silent)
 		[2] = { "xenbus", &xen_xenbus_fops, S_IRUSR|S_IWUSR },
 		{ "capabilities", &capabilities_file_ops, S_IRUGO },
 		{ "privcmd", &xen_privcmd_fops, S_IRUSR|S_IWUSR },
+		{ "privcmd-buf", &xen_privcmdbuf_fops, S_IRUSR|S_IWUSR },
 		{""},
 	};
 
@@ -55,6 +56,7 @@ static int xenfs_fill_super(struct super_block *sb, void *data, int silent)
 		[2] = { "xenbus", &xen_xenbus_fops, S_IRUSR|S_IWUSR },
 		{ "capabilities", &capabilities_file_ops, S_IRUGO },
 		{ "privcmd", &xen_privcmd_fops, S_IRUSR|S_IWUSR },
+		{ "privcmd-buf", &xen_privcmdbuf_fops, S_IRUSR|S_IWUSR },
 		{ "xsd_kva", &xsd_kva_file_ops, S_IRUSR|S_IWUSR},
 		{ "xsd_port", &xsd_port_file_ops, S_IRUSR|S_IWUSR},
 #ifdef CONFIG_XEN_SYMS
Signature · 1 line
2.13.7
#1 lore raw

drivers/xen/privcmd-buf.c line 27

Quoted 6 lines · Juergen Gross
drivers/xen/privcmd-buf.c new file +6 -0
+MODULE_LICENSE("GPL");
+
+static int limit = 64;
+module_param(limit, int, 0644);
+MODULE_PARM_DESC(limit, "Maximum number of pages that may be allocated by "
+			"the privcmd-buf device per open file");

I have a feeling that, once we try and remove some of the bounce buffering, 64 pages will be somewhat restricting.  In particular, migration performance will benefit by keeping the logdirty bitmap buffer persistently mapped, rather than allocated/bounced/deallocated on each iteration.

However, perhaps 64 is fine for now.

drivers/xen/privcmd-buf.c line 145

Quoted 5 lines
drivers/xen/privcmd-buf.c new file +5 -0
+static int privcmd_buf_mmap(struct file *file, struct vm_area_struct *vma)
+{
+	struct privcmd_buf_private *file_priv = file->private_data;
+	struct privcmd_buf_vma_private *vma_priv;
+	unsigned int count = vma_pages(vma);

This will truncate to 0 if anyone tried mmap()ing 8T (if I've done my calculations correctly) of virtual address space.

drivers/xen/privcmd-buf.c line 154

Quoted 9 lines
drivers/xen/privcmd-buf.c new file +9 -0
+	unsigned int i;
+	int ret = 0;
+
+	if (!(vma->vm_flags & VM_SHARED)) {
+		pr_err("Mapping must be shared\n");
+		return -EINVAL;
+	}
+
+	if (file_priv->allocated + count > limit) {

cout > limit || (allocated + count) > limit to avoid overflows.

Quoted 16 lines
drivers/xen/privcmd-buf.c new file +16 -0
+		pr_err("Mapping limit reached!\n");
+		return -ENOSPC;
+	}
+
+	vma_priv = kzalloc(sizeof(*vma_priv) + count * sizeof(void *),
+			   GFP_KERNEL);
+	if (!vma_priv)
+		return -ENOMEM;
+
+	vma_priv->n_pages = count;
+	count = 0;
+	for (i = 0; i < vma_priv->n_pages; i++) {
+		vma_priv->pages[i] = alloc_page(GFP_KERNEL | __GFP_ZERO);
+		if (!vma_priv->pages[i])
+			break;
+		count++;

drivers/xen/privcmd-buf.c line 180

Quoted 10 lines
drivers/xen/privcmd-buf.c new file +10 -0
+	}
+
+	mutex_lock(&file_priv->lock);
+
+	file_priv->allocated += count;
+
+	vma_priv->file_priv = file_priv;
+	vma_priv->users = 1;
+
+	vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;

Why DONTDUMP? Its just data, and stands a reasonable chance of being related to the cause of a crash.

Quoted 24 lines
drivers/xen/privcmd-buf.c new file +24 -0
+	vma->vm_ops = &privcmd_buf_vm_ops;
+	vma->vm_private_data = vma_priv;
+
+	list_add(&vma_priv->list, &file_priv->list);
+
+	if (vma_priv->n_pages != count)
+		ret = -ENOMEM;
+	else
+		for (i = 0; i < vma_priv->n_pages; i++) {
+			ret = vm_insert_page(vma, vma->vm_start + i * PAGE_SIZE,
+					     vma_priv->pages[i]);
+			if (ret)
+				break;
+		}
+
+	if (ret)
+		privcmd_buf_vmapriv_free(vma_priv);
+
+	mutex_unlock(&file_priv->lock);
+
+	return ret;
+}
+
+const struct file_operations xen_privcmdbuf_fops = {

drivers/xen/privcmd-buf.c line 214

Quoted 10 lines
drivers/xen/privcmd-buf.c new file +10 -0
+	.owner = THIS_MODULE,
+	.open = privcmd_buf_open,
+	.release = privcmd_buf_release,
+	.mmap = privcmd_buf_mmap,
+};
+EXPORT_SYMBOL_GPL(xen_privcmdbuf_fops);
+
+struct miscdevice xen_privcmdbuf_dev = {
+	.minor = MISC_DYNAMIC_MINOR,
+	.name = "xen/privcmd-buf",

Sorry to nitpick, but how about naming this just "xen/hypercall" ?

privcmd is currently a rather large security hole because it allows userspace to have access to all the hypercalls, including the ones which should be restricted to just the kernel.  In the past, a plan has been floated to slowly replace the use of the raw ioctl() with proper ioctls for the hypercalls which userspace might reasonably use.

drivers/xen/xenfs/super.c static int xenfs_fill_super(struct super_block *sb, void *data, int silent) · line 51

Quoted 9 lines
drivers/xen/xenfs/super.c +1 -0
index 71ddfb4cf61c..d752d0dd3d1d 100644
@@ -48,6 +48,7 @@ static int xenfs_fill_super(struct super_block *sb, void *data, int silent)
 		[2] = { "xenbus", &xen_xenbus_fops, S_IRUSR|S_IWUSR },
 		{ "capabilities", &capabilities_file_ops, S_IRUGO },
 		{ "privcmd", &xen_privcmd_fops, S_IRUSR|S_IWUSR },
+		{ "privcmd-buf", &xen_privcmdbuf_fops, S_IRUSR|S_IWUSR },

Do we really need to provide the fallback here?  /dev/xen has been around for ages, and it would really be a good thing if we can eventually retire xenfs.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
In reply to Juergen Gross

drivers/xen/privcmd-buf.c line 27

Quoted 6 lines · Juergen Gross
drivers/xen/privcmd-buf.c new file +6 -0
+MODULE_LICENSE("GPL");
+
+static int limit = 64;
+module_param(limit, int, 0644);
+MODULE_PARM_DESC(limit, "Maximum number of pages that may be allocated by "
+			"the privcmd-buf device per open file");

I have a feeling that, once we try and remove some of the bounce buffering, 64 pages will be somewhat restricting.  In particular, migration performance will benefit by keeping the logdirty bitmap buffer persistently mapped, rather than allocated/bounced/deallocated on each iteration.

However, perhaps 64 is fine for now.

drivers/xen/privcmd-buf.c line 145

Quoted 5 lines
drivers/xen/privcmd-buf.c new file +5 -0
+static int privcmd_buf_mmap(struct file *file, struct vm_area_struct *vma)
+{
+	struct privcmd_buf_private *file_priv = file->private_data;
+	struct privcmd_buf_vma_private *vma_priv;
+	unsigned int count = vma_pages(vma);

This will truncate to 0 if anyone tried mmap()ing 8T (if I've done my calculations correctly) of virtual address space.

drivers/xen/privcmd-buf.c line 154

Quoted 9 lines
drivers/xen/privcmd-buf.c new file +9 -0
+	unsigned int i;
+	int ret = 0;
+
+	if (!(vma->vm_flags & VM_SHARED)) {
+		pr_err("Mapping must be shared\n");
+		return -EINVAL;
+	}
+
+	if (file_priv->allocated + count > limit) {

cout > limit || (allocated + count) > limit to avoid overflows.

Quoted 16 lines
drivers/xen/privcmd-buf.c new file +16 -0
+		pr_err("Mapping limit reached!\n");
+		return -ENOSPC;
+	}
+
+	vma_priv = kzalloc(sizeof(*vma_priv) + count * sizeof(void *),
+			   GFP_KERNEL);
+	if (!vma_priv)
+		return -ENOMEM;
+
+	vma_priv->n_pages = count;
+	count = 0;
+	for (i = 0; i < vma_priv->n_pages; i++) {
+		vma_priv->pages[i] = alloc_page(GFP_KERNEL | __GFP_ZERO);
+		if (!vma_priv->pages[i])
+			break;
+		count++;

drivers/xen/privcmd-buf.c line 180

Quoted 10 lines
drivers/xen/privcmd-buf.c new file +10 -0
+	}
+
+	mutex_lock(&file_priv->lock);
+
+	file_priv->allocated += count;
+
+	vma_priv->file_priv = file_priv;
+	vma_priv->users = 1;
+
+	vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;

Why DONTDUMP? Its just data, and stands a reasonable chance of being related to the cause of a crash.

Quoted 24 lines
drivers/xen/privcmd-buf.c new file +24 -0
+	vma->vm_ops = &privcmd_buf_vm_ops;
+	vma->vm_private_data = vma_priv;
+
+	list_add(&vma_priv->list, &file_priv->list);
+
+	if (vma_priv->n_pages != count)
+		ret = -ENOMEM;
+	else
+		for (i = 0; i < vma_priv->n_pages; i++) {
+			ret = vm_insert_page(vma, vma->vm_start + i * PAGE_SIZE,
+					     vma_priv->pages[i]);
+			if (ret)
+				break;
+		}
+
+	if (ret)
+		privcmd_buf_vmapriv_free(vma_priv);
+
+	mutex_unlock(&file_priv->lock);
+
+	return ret;
+}
+
+const struct file_operations xen_privcmdbuf_fops = {

drivers/xen/privcmd-buf.c line 214

Quoted 10 lines
drivers/xen/privcmd-buf.c new file +10 -0
+	.owner = THIS_MODULE,
+	.open = privcmd_buf_open,
+	.release = privcmd_buf_release,
+	.mmap = privcmd_buf_mmap,
+};
+EXPORT_SYMBOL_GPL(xen_privcmdbuf_fops);
+
+struct miscdevice xen_privcmdbuf_dev = {
+	.minor = MISC_DYNAMIC_MINOR,
+	.name = "xen/privcmd-buf",

Sorry to nitpick, but how about naming this just "xen/hypercall" ?

privcmd is currently a rather large security hole because it allows userspace to have access to all the hypercalls, including the ones which should be restricted to just the kernel.  In the past, a plan has been floated to slowly replace the use of the raw ioctl() with proper ioctls for the hypercalls which userspace might reasonably use.

drivers/xen/xenfs/super.c static int xenfs_fill_super(struct super_block *sb, void *data, int silent) · line 51

Quoted 9 lines
drivers/xen/xenfs/super.c +1 -0
index 71ddfb4cf61c..d752d0dd3d1d 100644
@@ -48,6 +48,7 @@ static int xenfs_fill_super(struct super_block *sb, void *data, int silent)
 		[2] = { "xenbus", &xen_xenbus_fops, S_IRUSR|S_IWUSR },
 		{ "capabilities", &capabilities_file_ops, S_IRUGO },
 		{ "privcmd", &xen_privcmd_fops, S_IRUSR|S_IWUSR },
+		{ "privcmd-buf", &xen_privcmdbuf_fops, S_IRUSR|S_IWUSR },

Do we really need to provide the fallback here?  /dev/xen has been around for ages, and it would really be a good thing if we can eventually retire xenfs.

~Andrew

author maintainer
#3 lore raw
Quoted 16 lines · Andrew Cooper

drivers/xen/privcmd-buf.c line 27

Quoted 6 lines · Juergen Gross
drivers/xen/privcmd-buf.c new file +6 -0
+MODULE_LICENSE("GPL");
+
+static int limit = 64;
+module_param(limit, int, 0644);
+MODULE_PARM_DESC(limit, "Maximum number of pages that may be allocated by "
+			"the privcmd-buf device per open file");

I have a feeling that, once we try and remove some of the bounce buffering, 64 pages will be somewhat restricting.  In particular, migration performance will benefit by keeping the logdirty bitmap buffer persistently mapped, rather than allocated/bounced/deallocated on each iteration.

However, perhaps 64 is fine for now.

Quoted 8 lines

drivers/xen/privcmd-buf.c line 145

Quoted 5 lines
drivers/xen/privcmd-buf.c new file +5 -0
+static int privcmd_buf_mmap(struct file *file, struct vm_area_struct *vma)
+{
+	struct privcmd_buf_private *file_priv = file->private_data;
+	struct privcmd_buf_vma_private *vma_priv;
+	unsigned int count = vma_pages(vma);

This will truncate to 0 if anyone tried mmap()ing 8T (if I've done my calculations correctly) of virtual address space.

Okay, I'll change the type to unsigned long.

Quoted 12 lines

drivers/xen/privcmd-buf.c line 154

Quoted 10 lines
drivers/xen/privcmd-buf.c new file +9 -0
+	unsigned int i;
+	int ret = 0;
+
+	if (!(vma->vm_flags & VM_SHARED)) {
+		pr_err("Mapping must be shared\n");
+		return -EINVAL;
+	}
+
+	if (file_priv->allocated + count > limit) {

cout > limit || (allocated + count) > limit to avoid overflows.

unsigned long again.

Quoted 22 lines
Quoted 22 lines
drivers/xen/privcmd-buf.c new file +21 -0
+		pr_err("Mapping limit reached!\n");
+		return -ENOSPC;
+	}
+
+	vma_priv = kzalloc(sizeof(*vma_priv) + count * sizeof(void *),
+			   GFP_KERNEL);
+	if (!vma_priv)
+		return -ENOMEM;
+
+	vma_priv->n_pages = count;
+	count = 0;
+	for (i = 0; i < vma_priv->n_pages; i++) {
+		vma_priv->pages[i] = alloc_page(GFP_KERNEL | __GFP_ZERO);
+		if (!vma_priv->pages[i])
+			break;
+		count++;
+	}
+
+	mutex_lock(&file_priv->lock);
+
+	file_priv->allocated += count;
Quoted 8 lines

drivers/xen/privcmd-buf.c line 180

Quoted 5 lines
drivers/xen/privcmd-buf.c new file +5 -0
+
+	vma_priv->file_priv = file_priv;
+	vma_priv->users = 1;
+
+	vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;

Why DONTDUMP? Its just data, and stands a reasonable chance of being related to the cause of a crash.

Hmm, yes. I'll drop it.

Quoted 29 lines
Quoted 29 lines
drivers/xen/privcmd-buf.c new file +28 -0
+	vma->vm_ops = &privcmd_buf_vm_ops;
+	vma->vm_private_data = vma_priv;
+
+	list_add(&vma_priv->list, &file_priv->list);
+
+	if (vma_priv->n_pages != count)
+		ret = -ENOMEM;
+	else
+		for (i = 0; i < vma_priv->n_pages; i++) {
+			ret = vm_insert_page(vma, vma->vm_start + i * PAGE_SIZE,
+					     vma_priv->pages[i]);
+			if (ret)
+				break;
+		}
+
+	if (ret)
+		privcmd_buf_vmapriv_free(vma_priv);
+
+	mutex_unlock(&file_priv->lock);
+
+	return ret;
+}
+
+const struct file_operations xen_privcmdbuf_fops = {
+	.owner = THIS_MODULE,
+	.open = privcmd_buf_open,
+	.release = privcmd_buf_release,
+	.mmap = privcmd_buf_mmap,
Quoted 8 lines

drivers/xen/privcmd-buf.c line 214

Quoted 6 lines
drivers/xen/privcmd-buf.c new file +6 -0
+};
+EXPORT_SYMBOL_GPL(xen_privcmdbuf_fops);
+
+struct miscdevice xen_privcmdbuf_dev = {
+	.minor = MISC_DYNAMIC_MINOR,
+	.name = "xen/privcmd-buf",

Sorry to nitpick, but how about naming this just "xen/hypercall" ?

I really have no special preferences here.

Quoted 5 lines

privcmd is currently a rather large security hole because it allows userspace to have access to all the hypercalls, including the ones which should be restricted to just the kernel.  In the past, a plan has been floated to slowly replace the use of the raw ioctl() with proper ioctls for the hypercalls which userspace might reasonably use.

I'd rather let the privcmd driver either ask the hypervisor which hypercalls are fine to call from user mode, or let it encapsulate the hypercall in a new "user hypercall" which the hypervisor can verify then.

Quoted 5 lines
Quoted 5 lines
drivers/xen/xenfs/super.c +0 -0
index 71ddfb4cf61c..d752d0dd3d1d 100644
@@ -48,6 +48,7 @@ static int xenfs_fill_super(struct super_block *sb, void *data, int silent)
Quoted 8 lines

drivers/xen/xenfs/super.c static int xenfs_fill_super(struct super_block *sb, void *data, int silent) · line 51

Quoted 4 lines
drivers/xen/xenfs/super.c +1 -0
 		[2] = { "xenbus", &xen_xenbus_fops, S_IRUSR|S_IWUSR },
 		{ "capabilities", &capabilities_file_ops, S_IRUGO },
 		{ "privcmd", &xen_privcmd_fops, S_IRUSR|S_IWUSR },
+		{ "privcmd-buf", &xen_privcmdbuf_fops, S_IRUSR|S_IWUSR },

Do we really need to provide the fallback here?  /dev/xen has been around for ages, and it would really be a good thing if we can eventually retire xenfs.

I'd be fine dropping it.

Just did some archaeology: /dev/xen is supported since Xen 4.5. Do we really want to drop support of older Xen versions in the Linux kernel?

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

author maintainer
#4 lore raw
In reply to Andrew Cooper
Quoted 16 lines · Andrew Cooper

drivers/xen/privcmd-buf.c line 27

Quoted 6 lines · Juergen Gross
drivers/xen/privcmd-buf.c new file +6 -0
+MODULE_LICENSE("GPL");
+
+static int limit = 64;
+module_param(limit, int, 0644);
+MODULE_PARM_DESC(limit, "Maximum number of pages that may be allocated by "
+			"the privcmd-buf device per open file");

I have a feeling that, once we try and remove some of the bounce buffering, 64 pages will be somewhat restricting.  In particular, migration performance will benefit by keeping the logdirty bitmap buffer persistently mapped, rather than allocated/bounced/deallocated on each iteration.

However, perhaps 64 is fine for now.

Quoted 8 lines

drivers/xen/privcmd-buf.c line 145

Quoted 5 lines
drivers/xen/privcmd-buf.c new file +5 -0
+static int privcmd_buf_mmap(struct file *file, struct vm_area_struct *vma)
+{
+	struct privcmd_buf_private *file_priv = file->private_data;
+	struct privcmd_buf_vma_private *vma_priv;
+	unsigned int count = vma_pages(vma);

This will truncate to 0 if anyone tried mmap()ing 8T (if I've done my calculations correctly) of virtual address space.

Okay, I'll change the type to unsigned long.

Quoted 12 lines

drivers/xen/privcmd-buf.c line 154

Quoted 10 lines
drivers/xen/privcmd-buf.c new file +9 -0
+	unsigned int i;
+	int ret = 0;
+
+	if (!(vma->vm_flags & VM_SHARED)) {
+		pr_err("Mapping must be shared\n");
+		return -EINVAL;
+	}
+
+	if (file_priv->allocated + count > limit) {

cout > limit || (allocated + count) > limit to avoid overflows.

unsigned long again.

Quoted 22 lines
Quoted 22 lines
drivers/xen/privcmd-buf.c new file +21 -0
+		pr_err("Mapping limit reached!\n");
+		return -ENOSPC;
+	}
+
+	vma_priv = kzalloc(sizeof(*vma_priv) + count * sizeof(void *),
+			   GFP_KERNEL);
+	if (!vma_priv)
+		return -ENOMEM;
+
+	vma_priv->n_pages = count;
+	count = 0;
+	for (i = 0; i < vma_priv->n_pages; i++) {
+		vma_priv->pages[i] = alloc_page(GFP_KERNEL | __GFP_ZERO);
+		if (!vma_priv->pages[i])
+			break;
+		count++;
+	}
+
+	mutex_lock(&file_priv->lock);
+
+	file_priv->allocated += count;
Quoted 8 lines

drivers/xen/privcmd-buf.c line 180

Quoted 5 lines
drivers/xen/privcmd-buf.c new file +5 -0
+
+	vma_priv->file_priv = file_priv;
+	vma_priv->users = 1;
+
+	vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;

Why DONTDUMP? Its just data, and stands a reasonable chance of being related to the cause of a crash.

Hmm, yes. I'll drop it.

Quoted 29 lines
Quoted 29 lines
drivers/xen/privcmd-buf.c new file +28 -0
+	vma->vm_ops = &privcmd_buf_vm_ops;
+	vma->vm_private_data = vma_priv;
+
+	list_add(&vma_priv->list, &file_priv->list);
+
+	if (vma_priv->n_pages != count)
+		ret = -ENOMEM;
+	else
+		for (i = 0; i < vma_priv->n_pages; i++) {
+			ret = vm_insert_page(vma, vma->vm_start + i * PAGE_SIZE,
+					     vma_priv->pages[i]);
+			if (ret)
+				break;
+		}
+
+	if (ret)
+		privcmd_buf_vmapriv_free(vma_priv);
+
+	mutex_unlock(&file_priv->lock);
+
+	return ret;
+}
+
+const struct file_operations xen_privcmdbuf_fops = {
+	.owner = THIS_MODULE,
+	.open = privcmd_buf_open,
+	.release = privcmd_buf_release,
+	.mmap = privcmd_buf_mmap,
Quoted 8 lines

drivers/xen/privcmd-buf.c line 214

Quoted 6 lines
drivers/xen/privcmd-buf.c new file +6 -0
+};
+EXPORT_SYMBOL_GPL(xen_privcmdbuf_fops);
+
+struct miscdevice xen_privcmdbuf_dev = {
+	.minor = MISC_DYNAMIC_MINOR,
+	.name = "xen/privcmd-buf",

Sorry to nitpick, but how about naming this just "xen/hypercall" ?

I really have no special preferences here.

Quoted 5 lines

privcmd is currently a rather large security hole because it allows userspace to have access to all the hypercalls, including the ones which should be restricted to just the kernel.  In the past, a plan has been floated to slowly replace the use of the raw ioctl() with proper ioctls for the hypercalls which userspace might reasonably use.

I'd rather let the privcmd driver either ask the hypervisor which hypercalls are fine to call from user mode, or let it encapsulate the hypercall in a new "user hypercall" which the hypervisor can verify then.

Quoted 5 lines
Quoted 5 lines
drivers/xen/xenfs/super.c +0 -0
index 71ddfb4cf61c..d752d0dd3d1d 100644
@@ -48,6 +48,7 @@ static int xenfs_fill_super(struct super_block *sb, void *data, int silent)
Quoted 8 lines

drivers/xen/xenfs/super.c static int xenfs_fill_super(struct super_block *sb, void *data, int silent) · line 51

Quoted 4 lines
drivers/xen/xenfs/super.c +1 -0
 		[2] = { "xenbus", &xen_xenbus_fops, S_IRUSR|S_IWUSR },
 		{ "capabilities", &capabilities_file_ops, S_IRUGO },
 		{ "privcmd", &xen_privcmd_fops, S_IRUSR|S_IWUSR },
+		{ "privcmd-buf", &xen_privcmdbuf_fops, S_IRUSR|S_IWUSR },

Do we really need to provide the fallback here?  /dev/xen has been around for ages, and it would really be a good thing if we can eventually retire xenfs.

I'd be fine dropping it.

Just did some archaeology: /dev/xen is supported since Xen 4.5. Do we really want to drop support of older Xen versions in the Linux kernel?

Juergen

#5 lore raw
In reply to Juergen Gross
Quoted 19 lines
Quoted 1 line
Quoted 1 line · On 15.06.18 at 15:17, <[email protected]> wrote:
drivers/xen/privcmd-buf.c new file +15 -0
@@ -0,0 +1,216 @@
+// SPDX-License-Identifier: GPL-2.0 OR MIT
+
+/******************************************************************************
+ * privcmd-buf.c
+ *
+ * Mmap of hypercall buffers.
+ *
+ * Copyright (c) 2018 Juergen Gross
+ */
+
+#define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/list.h>

drivers/xen/privcmd-buf.c line 25

Quoted 10 lines
drivers/xen/privcmd-buf.c new file +10 -0
+#include <linux/miscdevice.h>
+#include <linux/mm.h>
+#include <linux/slab.h>
+
+#include "privcmd.h"
+
+MODULE_LICENSE("GPL");
+
+static int limit = 64;
+module_param(limit, int, 0644);

Can this go negative? If not - "unsigned int" and "uint" prehaps?

Quoted 5 lines
drivers/xen/privcmd-buf.c new file +5 -0
+static int privcmd_buf_mmap(struct file *file, struct vm_area_struct *vma)
+{
+	struct privcmd_buf_private *file_priv = file->private_data;
+	struct privcmd_buf_vma_private *vma_priv;
+	unsigned int count = vma_pages(vma);

drivers/xen/privcmd-buf.c line 155

Quoted 10 lines
drivers/xen/privcmd-buf.c new file +10 -0
+	unsigned int i;
+	int ret = 0;
+
+	if (!(vma->vm_flags & VM_SHARED)) {
+		pr_err("Mapping must be shared\n");
+		return -EINVAL;
+	}
+
+	if (file_priv->allocated + count > limit) {
+		pr_err("Mapping limit reached!\n");

For both error messages - if you really want them, I think they should be made more helpful such that it is possible to identify the offender. Log at least process name and pid, or drop the messages?

Quoted 32 lines
drivers/xen/privcmd-buf.c new file +32 -0
+		return -ENOSPC;
+	}
+
+	vma_priv = kzalloc(sizeof(*vma_priv) + count * sizeof(void *),
+			   GFP_KERNEL);
+	if (!vma_priv)
+		return -ENOMEM;
+
+	vma_priv->n_pages = count;
+	count = 0;
+	for (i = 0; i < vma_priv->n_pages; i++) {
+		vma_priv->pages[i] = alloc_page(GFP_KERNEL | __GFP_ZERO);
+		if (!vma_priv->pages[i])
+			break;
+		count++;
+	}
+
+	mutex_lock(&file_priv->lock);
+
+	file_priv->allocated += count;
+
+	vma_priv->file_priv = file_priv;
+	vma_priv->users = 1;
+
+	vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
+	vma->vm_ops = &privcmd_buf_vm_ops;
+	vma->vm_private_data = vma_priv;
+
+	list_add(&vma_priv->list, &file_priv->list);
+
+	if (vma_priv->n_pages != count)
+		ret = -ENOMEM;

drivers/xen/privcmd-buf.c line 197

Quoted 10 lines
drivers/xen/privcmd-buf.c new file +10 -0
+	else
+		for (i = 0; i < vma_priv->n_pages; i++) {
+			ret = vm_insert_page(vma, vma->vm_start + i * PAGE_SIZE,
+					     vma_priv->pages[i]);
+			if (ret)
+				break;
+		}
+
+	if (ret)
+		privcmd_buf_vmapriv_free(vma_priv);

Don't you also need to undo the partially successful insertion?

drivers/xen/privcmd-buf.c line 213

Quoted 2 lines
drivers/xen/privcmd-buf.c new file +2 -0
+struct miscdevice xen_privcmdbuf_dev = {
+	.minor = MISC_DYNAMIC_MINOR,

While dynamic minors are of course much better than fixed ones (as we used to use many years ago), but aren't they still a relatively limited resource? By setting a "mode" on a handle to the original privcmd interface, no new minor would be needed.

drivers/xen/privcmd.c static int __init privcmd_init(void) · line 1015

Quoted 12 lines
drivers/xen/privcmd.c +6 -0
@@ -1007,12 +1007,21 @@ static int __init privcmd_init(void)
 		pr_err("Could not register Xen privcmd device\n");
 		return err;
 	}
+
+	err = misc_register(&xen_privcmdbuf_dev);
+	if (err != 0) {
+		pr_err("Could not register Xen hypercall-buf device\n");
+		misc_deregister(&privcmd_dev);
+		return err;

Wouldn't this better be a warning only, without failing driver init?

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#6 lore raw
In reply to Juergen Gross
Quoted 19 lines
Quoted 1 line
Quoted 1 line · On 15.06.18 at 15:17, <[email protected]> wrote:
drivers/xen/privcmd-buf.c new file +15 -0
@@ -0,0 +1,216 @@
+// SPDX-License-Identifier: GPL-2.0 OR MIT
+
+/******************************************************************************
+ * privcmd-buf.c
+ *
+ * Mmap of hypercall buffers.
+ *
+ * Copyright (c) 2018 Juergen Gross
+ */
+
+#define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/list.h>

drivers/xen/privcmd-buf.c line 25

Quoted 10 lines
drivers/xen/privcmd-buf.c new file +10 -0
+#include <linux/miscdevice.h>
+#include <linux/mm.h>
+#include <linux/slab.h>
+
+#include "privcmd.h"
+
+MODULE_LICENSE("GPL");
+
+static int limit = 64;
+module_param(limit, int, 0644);

Can this go negative? If not - "unsigned int" and "uint" prehaps?

Quoted 5 lines
drivers/xen/privcmd-buf.c new file +5 -0
+static int privcmd_buf_mmap(struct file *file, struct vm_area_struct *vma)
+{
+	struct privcmd_buf_private *file_priv = file->private_data;
+	struct privcmd_buf_vma_private *vma_priv;
+	unsigned int count = vma_pages(vma);

drivers/xen/privcmd-buf.c line 155

Quoted 10 lines
drivers/xen/privcmd-buf.c new file +10 -0
+	unsigned int i;
+	int ret = 0;
+
+	if (!(vma->vm_flags & VM_SHARED)) {
+		pr_err("Mapping must be shared\n");
+		return -EINVAL;
+	}
+
+	if (file_priv->allocated + count > limit) {
+		pr_err("Mapping limit reached!\n");

For both error messages - if you really want them, I think they should be made more helpful such that it is possible to identify the offender. Log at least process name and pid, or drop the messages?

Quoted 32 lines
drivers/xen/privcmd-buf.c new file +32 -0
+		return -ENOSPC;
+	}
+
+	vma_priv = kzalloc(sizeof(*vma_priv) + count * sizeof(void *),
+			   GFP_KERNEL);
+	if (!vma_priv)
+		return -ENOMEM;
+
+	vma_priv->n_pages = count;
+	count = 0;
+	for (i = 0; i < vma_priv->n_pages; i++) {
+		vma_priv->pages[i] = alloc_page(GFP_KERNEL | __GFP_ZERO);
+		if (!vma_priv->pages[i])
+			break;
+		count++;
+	}
+
+	mutex_lock(&file_priv->lock);
+
+	file_priv->allocated += count;
+
+	vma_priv->file_priv = file_priv;
+	vma_priv->users = 1;
+
+	vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
+	vma->vm_ops = &privcmd_buf_vm_ops;
+	vma->vm_private_data = vma_priv;
+
+	list_add(&vma_priv->list, &file_priv->list);
+
+	if (vma_priv->n_pages != count)
+		ret = -ENOMEM;

drivers/xen/privcmd-buf.c line 197

Quoted 10 lines
drivers/xen/privcmd-buf.c new file +10 -0
+	else
+		for (i = 0; i < vma_priv->n_pages; i++) {
+			ret = vm_insert_page(vma, vma->vm_start + i * PAGE_SIZE,
+					     vma_priv->pages[i]);
+			if (ret)
+				break;
+		}
+
+	if (ret)
+		privcmd_buf_vmapriv_free(vma_priv);

Don't you also need to undo the partially successful insertion?

drivers/xen/privcmd-buf.c line 213

Quoted 2 lines
drivers/xen/privcmd-buf.c new file +2 -0
+struct miscdevice xen_privcmdbuf_dev = {
+	.minor = MISC_DYNAMIC_MINOR,

While dynamic minors are of course much better than fixed ones (as we used to use many years ago), but aren't they still a relatively limited resource? By setting a "mode" on a handle to the original privcmd interface, no new minor would be needed.

drivers/xen/privcmd.c static int __init privcmd_init(void) · line 1015

Quoted 12 lines
drivers/xen/privcmd.c +6 -0
@@ -1007,12 +1007,21 @@ static int __init privcmd_init(void)
 		pr_err("Could not register Xen privcmd device\n");
 		return err;
 	}
+
+	err = misc_register(&xen_privcmdbuf_dev);
+	if (err != 0) {
+		pr_err("Could not register Xen hypercall-buf device\n");
+		misc_deregister(&privcmd_dev);
+		return err;

Wouldn't this better be a warning only, without failing driver init?

Jan

#7 lore raw
In reply to Juergen Gross
Quoted 6 lines · Juergen Gross
drivers/xen/privcmd-buf.c new file +6 -0
+static int privcmd_buf_mmap(struct file *file, struct vm_area_struct *vma)
+{
+	struct privcmd_buf_private *file_priv = file->private_data;
+	struct privcmd_buf_vma_private *vma_priv;
+	unsigned int count = vma_pages(vma);
+	unsigned int i;

drivers/xen/privcmd-buf.c line 156

Quoted 10 lines
drivers/xen/privcmd-buf.c new file +10 -0
+	int ret = 0;
+
+	if (!(vma->vm_flags & VM_SHARED)) {
+		pr_err("Mapping must be shared\n");
+		return -EINVAL;
+	}
+
+	if (file_priv->allocated + count > limit) {
+		pr_err("Mapping limit reached!\n");
+		return -ENOSPC;

This error (which I thought should have been E2BIG) is not in the list of allowed error codes (per man page). I think it it's either EINVAL or ENOMEM (EINVAL seems more appropriate to me).

I am also not sure about pr_err as the caller can force it (although presumably the file is only accessible to superuser).

-boris

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#8 lore raw
In reply to Juergen Gross
Quoted 6 lines · Juergen Gross
drivers/xen/privcmd-buf.c new file +6 -0
+static int privcmd_buf_mmap(struct file *file, struct vm_area_struct *vma)
+{
+	struct privcmd_buf_private *file_priv = file->private_data;
+	struct privcmd_buf_vma_private *vma_priv;
+	unsigned int count = vma_pages(vma);
+	unsigned int i;

drivers/xen/privcmd-buf.c line 156

Quoted 10 lines
drivers/xen/privcmd-buf.c new file +10 -0
+	int ret = 0;
+
+	if (!(vma->vm_flags & VM_SHARED)) {
+		pr_err("Mapping must be shared\n");
+		return -EINVAL;
+	}
+
+	if (file_priv->allocated + count > limit) {
+		pr_err("Mapping limit reached!\n");
+		return -ENOSPC;

This error (which I thought should have been E2BIG) is not in the list of allowed error codes (per man page). I think it it's either EINVAL or ENOMEM (EINVAL seems more appropriate to me).

I am also not sure about pr_err as the caller can force it (although presumably the file is only accessible to superuser).

-boris

author maintainer
#9 lore raw
Quoted 14 lines · Boris Ostrovsky
Quoted 13 lines · Juergen Gross
drivers/xen/privcmd-buf.c new file +13 -0
+static int privcmd_buf_mmap(struct file *file, struct vm_area_struct *vma)
+{
+	struct privcmd_buf_private *file_priv = file->private_data;
+	struct privcmd_buf_vma_private *vma_priv;
+	unsigned int count = vma_pages(vma);
+	unsigned int i;
+	int ret = 0;
+
+	if (!(vma->vm_flags & VM_SHARED)) {
+		pr_err("Mapping must be shared\n");
+		return -EINVAL;
+	}
+
Quoted 8 lines

drivers/xen/privcmd-buf.c line 156

Quoted 3 lines
drivers/xen/privcmd-buf.c new file +3 -0
+	if (file_priv->allocated + count > limit) {
+		pr_err("Mapping limit reached!\n");
+		return -ENOSPC;

This error (which I thought should have been E2BIG) is not in the list of allowed error codes (per man page). I think it it's either EINVAL or ENOMEM (EINVAL seems more appropriate to me).

Okay.

Quoted 2 lines

I am also not sure about pr_err as the caller can force it (although presumably the file is only accessible to superuser).

I've already replied to Jan: I'll drop the messages.

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

author maintainer
#10 lore raw
In reply to Boris Ostrovsky
Quoted 14 lines · Boris Ostrovsky
Quoted 13 lines · Juergen Gross
drivers/xen/privcmd-buf.c new file +13 -0
+static int privcmd_buf_mmap(struct file *file, struct vm_area_struct *vma)
+{
+	struct privcmd_buf_private *file_priv = file->private_data;
+	struct privcmd_buf_vma_private *vma_priv;
+	unsigned int count = vma_pages(vma);
+	unsigned int i;
+	int ret = 0;
+
+	if (!(vma->vm_flags & VM_SHARED)) {
+		pr_err("Mapping must be shared\n");
+		return -EINVAL;
+	}
+
Quoted 8 lines

drivers/xen/privcmd-buf.c line 156

Quoted 3 lines
drivers/xen/privcmd-buf.c new file +3 -0
+	if (file_priv->allocated + count > limit) {
+		pr_err("Mapping limit reached!\n");
+		return -ENOSPC;

This error (which I thought should have been E2BIG) is not in the list of allowed error codes (per man page). I think it it's either EINVAL or ENOMEM (EINVAL seems more appropriate to me).

Okay.

Quoted 2 lines

I am also not sure about pr_err as the caller can force it (although presumably the file is only accessible to superuser).

I've already replied to Jan: I'll drop the messages.

Juergen

A message that is not in the archive (sent off-list or not yet received): [email protected]

author maintainer
#12 lore raw
Quoted 23 lines · Jan Beulich
Quoted 23 lines
Quoted 1 line
Quoted 1 line · On 15.06.18 at 15:17, <[email protected]> wrote:
drivers/xen/privcmd-buf.c new file +19 -0
@@ -0,0 +1,216 @@
+// SPDX-License-Identifier: GPL-2.0 OR MIT
+
+/******************************************************************************
+ * privcmd-buf.c
+ *
+ * Mmap of hypercall buffers.
+ *
+ * Copyright (c) 2018 Juergen Gross
+ */
+
+#define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/list.h>
+#include <linux/miscdevice.h>
+#include <linux/mm.h>
+#include <linux/slab.h>
+
Quoted 8 lines

drivers/xen/privcmd-buf.c line 25

Quoted 6 lines
drivers/xen/privcmd-buf.c new file +6 -0
+#include "privcmd.h"
+
+MODULE_LICENSE("GPL");
+
+static int limit = 64;
+module_param(limit, int, 0644);

Can this go negative? If not - "unsigned int" and "uint" prehaps?

Perhaps. ;-)

Quoted 12 lines
Quoted 12 lines
drivers/xen/privcmd-buf.c new file +11 -0
+static int privcmd_buf_mmap(struct file *file, struct vm_area_struct *vma)
+{
+	struct privcmd_buf_private *file_priv = file->private_data;
+	struct privcmd_buf_vma_private *vma_priv;
+	unsigned int count = vma_pages(vma);
+	unsigned int i;
+	int ret = 0;
+
+	if (!(vma->vm_flags & VM_SHARED)) {
+		pr_err("Mapping must be shared\n");
+		return -EINVAL;
Quoted 8 lines

drivers/xen/privcmd-buf.c line 155

Quoted 4 lines
drivers/xen/privcmd-buf.c new file +4 -0
+	}
+
+	if (file_priv->allocated + count > limit) {
+		pr_err("Mapping limit reached!\n");

For both error messages - if you really want them, I think they should be made more helpful such that it is possible to identify the offender. Log at least process name and pid, or drop the messages?

I think dropping them should be fine.

Quoted 37 lines
Quoted 37 lines
drivers/xen/privcmd-buf.c new file +36 -0
+		return -ENOSPC;
+	}
+
+	vma_priv = kzalloc(sizeof(*vma_priv) + count * sizeof(void *),
+			   GFP_KERNEL);
+	if (!vma_priv)
+		return -ENOMEM;
+
+	vma_priv->n_pages = count;
+	count = 0;
+	for (i = 0; i < vma_priv->n_pages; i++) {
+		vma_priv->pages[i] = alloc_page(GFP_KERNEL | __GFP_ZERO);
+		if (!vma_priv->pages[i])
+			break;
+		count++;
+	}
+
+	mutex_lock(&file_priv->lock);
+
+	file_priv->allocated += count;
+
+	vma_priv->file_priv = file_priv;
+	vma_priv->users = 1;
+
+	vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
+	vma->vm_ops = &privcmd_buf_vm_ops;
+	vma->vm_private_data = vma_priv;
+
+	list_add(&vma_priv->list, &file_priv->list);
+
+	if (vma_priv->n_pages != count)
+		ret = -ENOMEM;
+	else
+		for (i = 0; i < vma_priv->n_pages; i++) {
+			ret = vm_insert_page(vma, vma->vm_start + i * PAGE_SIZE,
+					     vma_priv->pages[i]);
Quoted 8 lines

drivers/xen/privcmd-buf.c line 197

Quoted 6 lines
drivers/xen/privcmd-buf.c new file +6 -0
+			if (ret)
+				break;
+		}
+
+	if (ret)
+		privcmd_buf_vmapriv_free(vma_priv);

Don't you also need to undo the partially successful insertion?

No, this is done by generic mmap() handling when I'm returning an error.

Quoted 8 lines

drivers/xen/privcmd-buf.c line 213

Quoted 3 lines
drivers/xen/privcmd-buf.c new file +2 -0
+struct miscdevice xen_privcmdbuf_dev = {
+	.minor = MISC_DYNAMIC_MINOR,

While dynamic minors are of course much better than fixed ones (as we used to use many years ago), but aren't they still a relatively limited resource? By setting a "mode" on a handle to the original privcmd interface, no new minor would be needed.

Hmm, I'm not very fond of this idea. That would make all privcmd file ops rather clumsy. OTOH I can see the benfits.

Anyone wanting to comment on this idea?

Quoted 15 lines

drivers/xen/privcmd.c static int __init privcmd_init(void) · line 1015

Quoted 13 lines
drivers/xen/privcmd.c +6 -0
@@ -1007,12 +1007,21 @@ static int __init privcmd_init(void)
 		pr_err("Could not register Xen privcmd device\n");
 		return err;
 	}
+
+	err = misc_register(&xen_privcmdbuf_dev);
+	if (err != 0) {
+		pr_err("Could not register Xen hypercall-buf device\n");
+		misc_deregister(&privcmd_dev);
+		return err;

Wouldn't this better be a warning only, without failing driver init?

No, I don't think so. We rather want the hypercall buffer handling to be clean from now on.

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

author maintainer
#13 lore raw
In reply to a message not in the archive
Quoted 23 lines · Jan Beulich
Quoted 23 lines
Quoted 1 line
Quoted 1 line · On 15.06.18 at 15:17, <[email protected]> wrote:
drivers/xen/privcmd-buf.c new file +19 -0
@@ -0,0 +1,216 @@
+// SPDX-License-Identifier: GPL-2.0 OR MIT
+
+/******************************************************************************
+ * privcmd-buf.c
+ *
+ * Mmap of hypercall buffers.
+ *
+ * Copyright (c) 2018 Juergen Gross
+ */
+
+#define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/list.h>
+#include <linux/miscdevice.h>
+#include <linux/mm.h>
+#include <linux/slab.h>
+
Quoted 8 lines

drivers/xen/privcmd-buf.c line 25

Quoted 6 lines
drivers/xen/privcmd-buf.c new file +6 -0
+#include "privcmd.h"
+
+MODULE_LICENSE("GPL");
+
+static int limit = 64;
+module_param(limit, int, 0644);

Can this go negative? If not - "unsigned int" and "uint" prehaps?

Perhaps. ;-)

Quoted 12 lines
Quoted 12 lines
drivers/xen/privcmd-buf.c new file +11 -0
+static int privcmd_buf_mmap(struct file *file, struct vm_area_struct *vma)
+{
+	struct privcmd_buf_private *file_priv = file->private_data;
+	struct privcmd_buf_vma_private *vma_priv;
+	unsigned int count = vma_pages(vma);
+	unsigned int i;
+	int ret = 0;
+
+	if (!(vma->vm_flags & VM_SHARED)) {
+		pr_err("Mapping must be shared\n");
+		return -EINVAL;
Quoted 8 lines

drivers/xen/privcmd-buf.c line 155

Quoted 4 lines
drivers/xen/privcmd-buf.c new file +4 -0
+	}
+
+	if (file_priv->allocated + count > limit) {
+		pr_err("Mapping limit reached!\n");

For both error messages - if you really want them, I think they should be made more helpful such that it is possible to identify the offender. Log at least process name and pid, or drop the messages?

I think dropping them should be fine.

Quoted 37 lines
Quoted 37 lines
drivers/xen/privcmd-buf.c new file +36 -0
+		return -ENOSPC;
+	}
+
+	vma_priv = kzalloc(sizeof(*vma_priv) + count * sizeof(void *),
+			   GFP_KERNEL);
+	if (!vma_priv)
+		return -ENOMEM;
+
+	vma_priv->n_pages = count;
+	count = 0;
+	for (i = 0; i < vma_priv->n_pages; i++) {
+		vma_priv->pages[i] = alloc_page(GFP_KERNEL | __GFP_ZERO);
+		if (!vma_priv->pages[i])
+			break;
+		count++;
+	}
+
+	mutex_lock(&file_priv->lock);
+
+	file_priv->allocated += count;
+
+	vma_priv->file_priv = file_priv;
+	vma_priv->users = 1;
+
+	vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
+	vma->vm_ops = &privcmd_buf_vm_ops;
+	vma->vm_private_data = vma_priv;
+
+	list_add(&vma_priv->list, &file_priv->list);
+
+	if (vma_priv->n_pages != count)
+		ret = -ENOMEM;
+	else
+		for (i = 0; i < vma_priv->n_pages; i++) {
+			ret = vm_insert_page(vma, vma->vm_start + i * PAGE_SIZE,
+					     vma_priv->pages[i]);
Quoted 8 lines

drivers/xen/privcmd-buf.c line 197

Quoted 6 lines
drivers/xen/privcmd-buf.c new file +6 -0
+			if (ret)
+				break;
+		}
+
+	if (ret)
+		privcmd_buf_vmapriv_free(vma_priv);

Don't you also need to undo the partially successful insertion?

No, this is done by generic mmap() handling when I'm returning an error.

Quoted 8 lines

drivers/xen/privcmd-buf.c line 213

Quoted 3 lines
drivers/xen/privcmd-buf.c new file +2 -0
+struct miscdevice xen_privcmdbuf_dev = {
+	.minor = MISC_DYNAMIC_MINOR,

While dynamic minors are of course much better than fixed ones (as we used to use many years ago), but aren't they still a relatively limited resource? By setting a "mode" on a handle to the original privcmd interface, no new minor would be needed.

Hmm, I'm not very fond of this idea. That would make all privcmd file ops rather clumsy. OTOH I can see the benfits.

Anyone wanting to comment on this idea?

Quoted 15 lines

drivers/xen/privcmd.c static int __init privcmd_init(void) · line 1015

Quoted 13 lines
drivers/xen/privcmd.c +6 -0
@@ -1007,12 +1007,21 @@ static int __init privcmd_init(void)
 		pr_err("Could not register Xen privcmd device\n");
 		return err;
 	}
+
+	err = misc_register(&xen_privcmdbuf_dev);
+	if (err != 0) {
+		pr_err("Could not register Xen hypercall-buf device\n");
+		misc_deregister(&privcmd_dev);
+		return err;

Wouldn't this better be a warning only, without failing driver init?

No, I don't think so. We rather want the hypercall buffer handling to be clean from now on.

Juergen