Skip to contentSkip to conversation

xen/privcmd: allow fetching resource sizes

PATCH Applied Roger Pau Monne 3 participants 4 messages Jan 12, 2021 – Jan 13, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 xen/privcmd: allow fetching resource sizes

Allow issuing an IOCTL_PRIVCMD_MMAP_RESOURCE ioctl with num = 0 and addr = 0 in order to fetch the size of a specific resource.

Add a shortcut to the default map resource path, since fetching the size requires no address to be passed in, and thus no VMA to setup.

This is missing from the initial implementation, and causes issues when mapping resources that don't have fixed or known sizes.

---
NB: fetching the size of a resource shouldn't trigger an hypercall preemption, and hence I've dropped the preempt indications.
---
Cc: Boris Ostrovsky <[email protected]>
Cc: Juergen Gross <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Paul Durrant <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
---
Changes since v1:
- Remove Fixes tag, add backport.
- Make sure both addr and num are set or unset.

---
 drivers/xen/privcmd.c | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)
drivers/xen/privcmd.c +19 -6
index b0c73c58f987..720a7b7abd46 100644
@@ -717,14 +717,15 @@ static long privcmd_ioctl_restrict(struct file *file, void __user *udata)
 	return 0;
 }
 
-static long privcmd_ioctl_mmap_resource(struct file *file, void __user *udata)
+static long privcmd_ioctl_mmap_resource(struct file *file,
+				struct privcmd_mmap_resource __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;
+	struct xen_mem_acquire_resource xdata = { };
 	int rc;
 
 	if (copy_from_user(&kdata, udata, sizeof(kdata)))
@@ -734,6 +735,22 @@ static long privcmd_ioctl_mmap_resource(struct file *file, void __user *udata)
 	if (data->domid != DOMID_INVALID && data->domid != kdata.dom)
 		return -EPERM;
 
+	/* Both fields must be set or unset */
+	if (!!kdata.addr != !!kdata.num)
+		return -EINVAL;
+
+	xdata.domid = kdata.dom;
+	xdata.type = kdata.type;
+	xdata.id = kdata.id;
+
+	if (!kdata.addr && !kdata.num) {
+		/* Query the size of the resource. */
+		rc = HYPERVISOR_memory_op(XENMEM_acquire_resource, &xdata);
+		if (rc)
+			return rc;
+		return __put_user(xdata.nr_frames, &udata->num);
+	}
+
 	mmap_write_lock(mm);
 
 	vma = find_vma(mm, kdata.addr);
@@ -768,10 +785,6 @@ static long privcmd_ioctl_mmap_resource(struct file *file, void __user *udata)
 	} else
 		vma->vm_private_data = PRIV_VMA_LOCKED;
 
-	memset(&xdata, 0, sizeof(xdata));
-	xdata.domid = kdata.dom;
-	xdata.type = kdata.type;
-	xdata.id = kdata.id;
 	xdata.frame = kdata.idx;
 	xdata.nr_frames = kdata.num;
 	set_xen_guest_handle(xdata.frame_list, pfns);
Signature · 1 line
2.29.2
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 11 lines · Roger Pau Monne

Allow issuing an IOCTL_PRIVCMD_MMAP_RESOURCE ioctl with num = 0 and addr = 0 in order to fetch the size of a specific resource.

Add a shortcut to the default map resource path, since fetching the size requires no address to be passed in, and thus no VMA to setup.

This is missing from the initial implementation, and causes issues when mapping resources that don't have fixed or known sizes.

Juergen

Tested-by
#2 lore raw
Quoted 7 lines · Jürgen Groß
Quoted 6 lines · Roger Pau Monne

Allow issuing an IOCTL_PRIVCMD_MMAP_RESOURCE ioctl with num = 0 and addr = 0 in order to fetch the size of a specific resource.

Add a shortcut to the default map resource path, since fetching the size requires no address to be passed in, and thus no VMA to setup.

Quoted 7 lines
Quoted 5 lines

This is missing from the initial implementation, and causes issues when mapping resources that don't have fixed or known sizes.

maintainer Applied
#3 lore raw
In reply to Roger Pau Monne
Quoted 11 lines · Roger Pau Monne

Allow issuing an IOCTL_PRIVCMD_MMAP_RESOURCE ioctl with num = 0 and addr = 0 in order to fetch the size of a specific resource.

Add a shortcut to the default map resource path, since fetching the size requires no address to be passed in, and thus no VMA to setup.

This is missing from the initial implementation, and causes issues when mapping resources that don't have fixed or known sizes.

Pushed to xen/tip.git for-linus-5.11

Juergen