Skip to contentSkip to conversation

xen/privcmd: add IOCTL_PRIVCMD_MMAP_RESOURCE

PATCH Superseded Paul Durrant 2 participants 5 messages Apr 10, 2018 – Apr 11, 2018

Subsystems (5)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 xen/privcmd: add IOCTL_PRIVCMD_MMAP_RESOURCE

My recent Xen patch series introduces a new HYPERVISOR_memory_op to support direct priv-mapping of certain guest resources (such as ioreq pages, used by emulators) by a tools domain, rather than having to access such resources via the guest P2M.

This patch adds the necessary infrastructure to the privcmd driver and Xen MMU code to support direct resource mapping.

NOTE: The adjustment in the MMU code is partially cosmetic. Xen will now
allow a PV tools domain to map guest pages either by GFN or MFN, thus the term 'mfn' has been swapped for 'pfn' in the lower layers of the remap code.

---
Cc: Juergen Gross <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Stefano Stabellini <[email protected]>

v4:
- Remove stray line of debug that causes a build warning on ARM 32-bit

v3:
- Addres comments from Boris
- Fix ARM build

v2:
- Fix bug when mapping multiple pages of a resource

---
 arch/arm/xen/enlighten.c       |  11 ++++
 arch/x86/xen/mmu.c             |  60 +++++++++++++------
 drivers/xen/privcmd.c          | 128 +++++++++++++++++++++++++++++++++++++++++
 include/uapi/xen/privcmd.h     |  11 ++++
 include/xen/interface/memory.h |  66 +++++++++++++++++++++
 include/xen/interface/xen.h    |   7 ++-
 include/xen/xen-ops.h          |  24 +++++++-
 7 files changed, 286 insertions(+), 21 deletions(-)
arch/arm/xen/enlighten.c +11 -0
index ba7f4c8f5c3e..8073625371f5 100644
@@ -89,6 +89,17 @@ int xen_unmap_domain_gfn_range(struct vm_area_struct *vma,
 }
 EXPORT_SYMBOL_GPL(xen_unmap_domain_gfn_range);
 
+/* Not used by XENFEAT_auto_translated guests. */
+int xen_remap_domain_mfn_array(struct vm_area_struct *vma,
+			       unsigned long addr,
+			       xen_pfn_t *mfn, int nr,
+			       int *err_ptr, pgprot_t prot,
+			       unsigned int domid, struct page **pages)
+{
+	return -ENOSYS;
+}
+EXPORT_SYMBOL_GPL(xen_remap_domain_mfn_array);
+
 static void xen_read_wallclock(struct timespec64 *ts)
 {
 	u32 version;
arch/x86/xen/mmu.c +43 -17

115 lines not on this page · raw message

drivers/xen/privcmd.c +128 -0

146 lines not on this page · raw message

include/uapi/xen/privcmd.h +11 -0
index 39d3e7b8e993..d2029556083e 100644
@@ -89,6 +89,15 @@ struct privcmd_dm_op {
 	const struct privcmd_dm_op_buf __user *ubufs;
 };
 
+struct privcmd_mmap_resource {
+	domid_t dom;
+	__u32 type;
+	__u32 id;
+	__u32 idx;
+	__u64 num;
+	__u64 addr;
+};
+
 /*
  * @cmd: IOCTL_PRIVCMD_HYPERCALL
  * @arg: &privcmd_hypercall_t
@@ -114,5 +123,7 @@ struct privcmd_dm_op {
 	_IOC(_IOC_NONE, 'P', 5, sizeof(struct privcmd_dm_op))
 #define IOCTL_PRIVCMD_RESTRICT					\
 	_IOC(_IOC_NONE, 'P', 6, sizeof(domid_t))
+#define IOCTL_PRIVCMD_MMAP_RESOURCE				\
+	_IOC(_IOC_NONE, 'P', 7, sizeof(struct privcmd_mmap_resource))
 
 #endif /* __LINUX_PUBLIC_PRIVCMD_H__ */
include/xen/interface/memory.h +66 -0

70 lines not on this page · raw message

include/xen/interface/xen.h +4 -3
index 4f4830ef8f93..8bfb242f433e 100644
@@ -265,9 +265,10 @@
  *
  * PAT (bit 7 on) --> PWT (bit 3 on) and clear bit 7.
  */
-#define MMU_NORMAL_PT_UPDATE      0 /* checked '*ptr = val'. ptr is MA.       */
-#define MMU_MACHPHYS_UPDATE       1 /* ptr = MA of frame to modify entry for  */
-#define MMU_PT_UPDATE_PRESERVE_AD 2 /* atomically: *ptr = val | (*ptr&(A|D)) */
+#define MMU_NORMAL_PT_UPDATE       0 /* checked '*ptr = val'. ptr is MA.      */
+#define MMU_MACHPHYS_UPDATE        1 /* ptr = MA of frame to modify entry for */
+#define MMU_PT_UPDATE_PRESERVE_AD  2 /* atomically: *ptr = val | (*ptr&(A|D)) */
+#define MMU_PT_UPDATE_NO_TRANSLATE 3 /* checked '*ptr = val'. ptr is MA.      */
 
 /*
  * MMU EXTENDED OPERATIONS
include/xen/xen-ops.h +23 -1
index fd23e42c6024..fd18c974a619 100644
@@ -63,7 +63,7 @@ static inline void xen_destroy_contiguous_region(phys_addr_t pstart,
 struct vm_area_struct;
 
 /*
- * xen_remap_domain_gfn_array() - map an array of foreign frames
+ * xen_remap_domain_gfn_array() - map an array of foreign frames by gfn
  * @vma:     VMA to map the pages into
  * @addr:    Address at which to map the pages
  * @gfn:     Array of GFNs to map
@@ -86,6 +86,28 @@ int xen_remap_domain_gfn_array(struct vm_area_struct *vma,
 			       unsigned domid,
 			       struct page **pages);
 
+/*
+ * xen_remap_domain_mfn_array() - map an array of foreign frames by mfn
+ * @vma:     VMA to map the pages into
+ * @addr:    Address at which to map the pages
+ * @mfn:     Array of MFNs to map
+ * @nr:      Number entries in the MFN array
+ * @err_ptr: Returns per-MFN error status.
+ * @prot:    page protection mask
+ * @domid:   Domain owning the pages
+ * @pages:   Array of pages if this domain has an auto-translated physmap
+ *
+ * @mfn and @err_ptr may point to the same buffer, the MFNs will be
+ * overwritten by the error codes after they are mapped.
+ *
+ * Returns the number of successfully mapped frames, or a -ve error
+ * code.
+ */
+int xen_remap_domain_mfn_array(struct vm_area_struct *vma,
+			       unsigned long addr, xen_pfn_t *mfn, int nr,
+			       int *err_ptr, pgprot_t prot,
+			       unsigned int domid, struct page **pages);
+
 /* xen_remap_domain_gfn_range() - map a range of foreign frames
  * @vma:     VMA to map the pages into
  * @addr:    Address at which to map the pages
Signature · 1 line
2.11.0
#1 lore raw

Hi,

Quoted 32 lines · Paul Durrant
drivers/xen/privcmd.c +32 -0
+static long privcmd_ioctl_mmap_resource(struct file *file, void __user *udata)
+{
+	struct privcmd_data *data = file->private_data;
+	struct mm_struct *mm = current->mm;
+	struct vm_area_struct *vma;
+	struct privcmd_mmap_resource kdata;
+	xen_pfn_t *pfns = NULL;
+	struct xen_mem_acquire_resource xdata;
+	int rc;
+
+	if (copy_from_user(&kdata, udata, sizeof(kdata)))
+		return -EFAULT;
+
+	/* If restriction is in place, check the domid matches */
+	if (data->domid != DOMID_INVALID && data->domid != kdata.dom)
+		return -EPERM;
+
+	down_write(&mm->mmap_sem);
+
+	vma = find_vma(mm, kdata.addr);
+	if (!vma || vma->vm_ops != &privcmd_vm_ops) {
+		rc = -EINVAL;
+		goto out;
+	}
+
+	pfns = kcalloc(kdata.num, sizeof(*pfns), GFP_KERNEL);
+	if (!pfns) {
+		rc = -ENOMEM;
+		goto out;
+	}
+
+	if (xen_feature(XENFEAT_auto_translated_physmap)) {

drivers/xen/privcmd.c static long privcmd_ioctl_restrict(struct file *file, void __user *udata) · line 787

Quoted 10 lines
drivers/xen/privcmd.c +10 -0
+		struct page **pages;
+		unsigned int i;
+
+		rc = alloc_empty_pages(vma, kdata.num);
+		if (rc < 0)
+			goto out;
+
+		pages = vma->vm_private_data;
+		for (i = 0; i < kdata.num; i++)
+			pfns[i] = page_to_pfn(pages[i]);

I don't think this is going to work well if the hypervisor is using a different granularity for the page.

Imagine Xen is using 4K but the kernel 64K. You would end up to have the resource not mapped contiguously in the memory.

Cheers,

Signature · 1 line
Julien Grall
#2 lore raw
Quoted 61 lines

-----Original Message-----
From: Julien Grall [mailto:julien.grall at arm.com]
Sent: 11 April 2018 10:46
To: Paul Durrant <[email protected]>; xen-devel at lists.xenproject.org; linux-arm-kernel at lists.infradead.org; linux-kernel at vger.kernel.org
Cc: Juergen Gross <[email protected]>; Thomas Gleixner <[email protected]>; Stefano Stabellini <[email protected]>; Ingo Molnar <[email protected]>
Subject: Re: [PATCH v4] xen/privcmd: add IOCTL_PRIVCMD_MMAP_RESOURCE

Hi,

Quoted 1 line · Paul Durrant

+static long privcmd_ioctl_mmap_resource(struct file *file, void __user

*udata)

Quoted 31 lines
drivers/xen/privcmd.c +31 -0
+{
+	struct privcmd_data *data = file->private_data;
+	struct mm_struct *mm = current->mm;
+	struct vm_area_struct *vma;
+	struct privcmd_mmap_resource kdata;
+	xen_pfn_t *pfns = NULL;
+	struct xen_mem_acquire_resource xdata;
+	int rc;
+
+	if (copy_from_user(&kdata, udata, sizeof(kdata)))
+		return -EFAULT;
+
+	/* If restriction is in place, check the domid matches */
+	if (data->domid != DOMID_INVALID && data->domid != kdata.dom)
+		return -EPERM;
+
+	down_write(&mm->mmap_sem);
+
+	vma = find_vma(mm, kdata.addr);
+	if (!vma || vma->vm_ops != &privcmd_vm_ops) {
+		rc = -EINVAL;
+		goto out;
+	}
+
+	pfns = kcalloc(kdata.num, sizeof(*pfns), GFP_KERNEL);
+	if (!pfns) {
+		rc = -ENOMEM;
+		goto out;
+	}
+
+	if (xen_feature(XENFEAT_auto_translated_physmap)) {

drivers/xen/privcmd.c static long privcmd_ioctl_restrict(struct file *file, void __user *udata) · line 787

Quoted 10 lines
drivers/xen/privcmd.c +10 -0
+		struct page **pages;
+		unsigned int i;
+
+		rc = alloc_empty_pages(vma, kdata.num);
+		if (rc < 0)
+			goto out;
+
+		pages = vma->vm_private_data;
+		for (i = 0; i < kdata.num; i++)
+			pfns[i] = page_to_pfn(pages[i]);

I don't think this is going to work well if the hypervisor is using a different granularity for the page.

Quoted 2 lines

Imagine Xen is using 4K but the kernel 64K. You would end up to have the resource not mapped contiguously in the memory.

Good point. I do need to take account of the kernel page size in this case.

Paul

Quoted 5 lines

Cheers,

--
Julien Grall

#3 lore raw
In reply to Julien Grall
Quoted 61 lines

-----Original Message-----
From: Julien Grall [mailto:[email protected]]
Sent: 11 April 2018 10:46
To: Paul Durrant <[email protected]>; [email protected]; [email protected]; [email protected]
Cc: Juergen Gross <[email protected]>; Thomas Gleixner <[email protected]>; Stefano Stabellini <[email protected]>; Ingo Molnar <[email protected]>
Subject: Re: [PATCH v4] xen/privcmd: add IOCTL_PRIVCMD_MMAP_RESOURCE

Hi,

Quoted 1 line · Paul Durrant

+static long privcmd_ioctl_mmap_resource(struct file *file, void __user

*udata)

Quoted 31 lines
drivers/xen/privcmd.c +31 -0
+{
+	struct privcmd_data *data = file->private_data;
+	struct mm_struct *mm = current->mm;
+	struct vm_area_struct *vma;
+	struct privcmd_mmap_resource kdata;
+	xen_pfn_t *pfns = NULL;
+	struct xen_mem_acquire_resource xdata;
+	int rc;
+
+	if (copy_from_user(&kdata, udata, sizeof(kdata)))
+		return -EFAULT;
+
+	/* If restriction is in place, check the domid matches */
+	if (data->domid != DOMID_INVALID && data->domid != kdata.dom)
+		return -EPERM;
+
+	down_write(&mm->mmap_sem);
+
+	vma = find_vma(mm, kdata.addr);
+	if (!vma || vma->vm_ops != &privcmd_vm_ops) {
+		rc = -EINVAL;
+		goto out;
+	}
+
+	pfns = kcalloc(kdata.num, sizeof(*pfns), GFP_KERNEL);
+	if (!pfns) {
+		rc = -ENOMEM;
+		goto out;
+	}
+
+	if (xen_feature(XENFEAT_auto_translated_physmap)) {

drivers/xen/privcmd.c static long privcmd_ioctl_restrict(struct file *file, void __user *udata) · line 787

Quoted 10 lines
drivers/xen/privcmd.c +10 -0
+		struct page **pages;
+		unsigned int i;
+
+		rc = alloc_empty_pages(vma, kdata.num);
+		if (rc < 0)
+			goto out;
+
+		pages = vma->vm_private_data;
+		for (i = 0; i < kdata.num; i++)
+			pfns[i] = page_to_pfn(pages[i]);

I don't think this is going to work well if the hypervisor is using a different granularity for the page.

Quoted 2 lines

Imagine Xen is using 4K but the kernel 64K. You would end up to have the resource not mapped contiguously in the memory.

Good point. I do need to take account of the kernel page size in this case.

Paul

Quoted 5 lines

Cheers,

--
Julien Grall

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

#4 lore raw
In reply to Paul Durrant

Hi,

Quoted 32 lines · Paul Durrant
drivers/xen/privcmd.c +32 -0
+static long privcmd_ioctl_mmap_resource(struct file *file, void __user *udata)
+{
+	struct privcmd_data *data = file->private_data;
+	struct mm_struct *mm = current->mm;
+	struct vm_area_struct *vma;
+	struct privcmd_mmap_resource kdata;
+	xen_pfn_t *pfns = NULL;
+	struct xen_mem_acquire_resource xdata;
+	int rc;
+
+	if (copy_from_user(&kdata, udata, sizeof(kdata)))
+		return -EFAULT;
+
+	/* If restriction is in place, check the domid matches */
+	if (data->domid != DOMID_INVALID && data->domid != kdata.dom)
+		return -EPERM;
+
+	down_write(&mm->mmap_sem);
+
+	vma = find_vma(mm, kdata.addr);
+	if (!vma || vma->vm_ops != &privcmd_vm_ops) {
+		rc = -EINVAL;
+		goto out;
+	}
+
+	pfns = kcalloc(kdata.num, sizeof(*pfns), GFP_KERNEL);
+	if (!pfns) {
+		rc = -ENOMEM;
+		goto out;
+	}
+
+	if (xen_feature(XENFEAT_auto_translated_physmap)) {

drivers/xen/privcmd.c static long privcmd_ioctl_restrict(struct file *file, void __user *udata) · line 787

Quoted 10 lines
drivers/xen/privcmd.c +10 -0
+		struct page **pages;
+		unsigned int i;
+
+		rc = alloc_empty_pages(vma, kdata.num);
+		if (rc < 0)
+			goto out;
+
+		pages = vma->vm_private_data;
+		for (i = 0; i < kdata.num; i++)
+			pfns[i] = page_to_pfn(pages[i]);

I don't think this is going to work well if the hypervisor is using a different granularity for the page.

Imagine Xen is using 4K but the kernel 64K. You would end up to have the resource not mapped contiguously in the memory.

Cheers,

Signature · 6 lines
Julien Grall

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