mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] xen/privcmd: allow fetching resource sizes
@ 2021-01-11 15:29 Roger Pau Monne
  2021-01-11 22:09 ` boris.ostrovsky
  2021-01-12  5:57 ` Jürgen Groß
  0 siblings, 2 replies; 8+ messages in thread
From: Roger Pau Monne @ 2021-01-11 15:29 UTC (permalink / raw)
  To: linux-kernel
  Cc: Roger Pau Monne, Boris Ostrovsky, Juergen Gross,
	Stefano Stabellini, Paul Durrant, xen-devel

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.

Fixes: 3ad0876554caf ('xen/privcmd: add IOCTL_PRIVCMD_MMAP_RESOURCE')
Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>
---
NB: fetching the size of a resource shouldn't trigger an hypercall
preemption, and hence I've dropped the preempt indications.
---
Cc: Boris Ostrovsky <boris.ostrovsky@oracle.com>
Cc: Juergen Gross <jgross@suse.com>
Cc: Stefano Stabellini <sstabellini@kernel.org>
Cc: Paul Durrant <paul.durrant@citrix.com>
Cc: xen-devel@lists.xenproject.org
---
 drivers/xen/privcmd.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)

diff --git a/drivers/xen/privcmd.c b/drivers/xen/privcmd.c
index b0c73c58f987..a6e7e6e4286f 100644
--- a/drivers/xen/privcmd.c
+++ b/drivers/xen/privcmd.c
@@ -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,18 @@ static long privcmd_ioctl_mmap_resource(struct file *file, void __user *udata)
 	if (data->domid != DOMID_INVALID && data->domid != kdata.dom)
 		return -EPERM;
 
+	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 +781,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);
-- 
2.29.2


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] xen/privcmd: allow fetching resource sizes
  2021-01-11 15:29 [PATCH] xen/privcmd: allow fetching resource sizes Roger Pau Monne
@ 2021-01-11 22:09 ` boris.ostrovsky
  2021-01-11 22:39   ` Andrew Cooper
  2021-01-12  5:57 ` Jürgen Groß
  1 sibling, 1 reply; 8+ messages in thread
From: boris.ostrovsky @ 2021-01-11 22:09 UTC (permalink / raw)
  To: Roger Pau Monne, linux-kernel
  Cc: Juergen Gross, Stefano Stabellini, Paul Durrant, xen-devel


On 1/11/21 10:29 AM, Roger Pau Monne wrote:
>  
> +	xdata.domid = kdata.dom;
> +	xdata.type = kdata.type;
> +	xdata.id = kdata.id;
> +
> +	if (!kdata.addr && !kdata.num) {


I think we should not allow only one of them to be zero. If it's only kdata.num then we will end up with pfns array set to ZERO_SIZE_PTR (which is 0x10). We seem to be OK in that we are not derefencing pfns (either in kernel or in hypervisor) if number of frames is zero but IMO we shouldn't be tempting the fate.


(And if it's only kdata.addr then we will get a vma but I am not sure it will do what we want.)


-boris



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] xen/privcmd: allow fetching resource sizes
  2021-01-11 22:09 ` boris.ostrovsky
@ 2021-01-11 22:39   ` Andrew Cooper
  2021-01-12  5:50     ` Jürgen Groß
  0 siblings, 1 reply; 8+ messages in thread
From: Andrew Cooper @ 2021-01-11 22:39 UTC (permalink / raw)
  To: boris.ostrovsky, Roger Pau Monne, linux-kernel
  Cc: Juergen Gross, Stefano Stabellini, Paul Durrant, xen-devel,
	Andrew Cooper, Andrew Cooper

On 11/01/2021 22:09, boris.ostrovsky@oracle.com wrote:
> On 1/11/21 10:29 AM, Roger Pau Monne wrote:
>>  
>> +	xdata.domid = kdata.dom;
>> +	xdata.type = kdata.type;
>> +	xdata.id = kdata.id;
>> +
>> +	if (!kdata.addr && !kdata.num) {
>
> I think we should not allow only one of them to be zero. If it's only kdata.num then we will end up with pfns array set to ZERO_SIZE_PTR (which is 0x10). We seem to be OK in that we are not derefencing pfns (either in kernel or in hypervisor) if number of frames is zero but IMO we shouldn't be tempting the fate.
>
>
> (And if it's only kdata.addr then we will get a vma but I am not sure it will do what we want.)

Passing addr == 0 without num being 0 is already an error in Xen, and
passing num == 0 without addr being 0 is bogus and will be an error by
the time I'm finished fixing this.

FWIW, the common usecase for non-trivial examples will be:

xenforeignmem_resource_size(domid, type, id, &size);
xenforeignmem_map_resource(domid, type, id, NULL, size, ...);

which translates into:

ioctl(MAP_RESOURCE, NULL, 0) => size
mmap(NULL, size, ...) => ptr
ioctl(MAP_RESOURCE, ptr, size)

from the kernels point of view, and two hypercalls from Xen's point of
view.  The NULL's above are expected to be the common case for letting
the kernel chose the vma, but ought to be filled in by the time the
second ioctl() occurs.

See
https://lore.kernel.org/xen-devel/20200922182444.12350-1-andrew.cooper3@citrix.com/T/#u
for all the gory details.

~Andrew

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] xen/privcmd: allow fetching resource sizes
  2021-01-11 22:39   ` Andrew Cooper
@ 2021-01-12  5:50     ` Jürgen Groß
  2021-01-12  6:01       ` Jürgen Groß
  0 siblings, 1 reply; 8+ messages in thread
From: Jürgen Groß @ 2021-01-12  5:50 UTC (permalink / raw)
  To: Andrew Cooper, boris.ostrovsky, Roger Pau Monne, linux-kernel
  Cc: Stefano Stabellini, Paul Durrant, xen-devel, Andrew Cooper


[-- Attachment #1.1.1: Type: text/plain, Size: 1802 bytes --]

On 11.01.21 23:39, Andrew Cooper wrote:
> On 11/01/2021 22:09, boris.ostrovsky@oracle.com wrote:
>> On 1/11/21 10:29 AM, Roger Pau Monne wrote:
>>>   
>>> +	xdata.domid = kdata.dom;
>>> +	xdata.type = kdata.type;
>>> +	xdata.id = kdata.id;
>>> +
>>> +	if (!kdata.addr && !kdata.num) {
>>
>> I think we should not allow only one of them to be zero. If it's only kdata.num then we will end up with pfns array set to ZERO_SIZE_PTR (which is 0x10). We seem to be OK in that we are not derefencing pfns (either in kernel or in hypervisor) if number of frames is zero but IMO we shouldn't be tempting the fate.
>>
>>
>> (And if it's only kdata.addr then we will get a vma but I am not sure it will do what we want.)
> 
> Passing addr == 0 without num being 0 is already an error in Xen, and
> passing num == 0 without addr being 0 is bogus and will be an error by
> the time I'm finished fixing this.
> 
> FWIW, the common usecase for non-trivial examples will be:
> 
> xenforeignmem_resource_size(domid, type, id, &size);
> xenforeignmem_map_resource(domid, type, id, NULL, size, ...);
> 
> which translates into:
> 
> ioctl(MAP_RESOURCE, NULL, 0) => size
> mmap(NULL, size, ...) => ptr
> ioctl(MAP_RESOURCE, ptr, size)
> 
> from the kernels point of view, and two hypercalls from Xen's point of
> view.  The NULL's above are expected to be the common case for letting
> the kernel chose the vma, but ought to be filled in by the time the
> second ioctl() occurs.
> 
> See
> https://lore.kernel.org/xen-devel/20200922182444.12350-1-andrew.cooper3@citrix.com/T/#u
> for all the gory details.

I don't think the kernel should rely on the hypervisor to return
an error in case addr != 0 and num == 0.

The driver should return -EINVAL in that case IMO.


Juergen

[-- Attachment #1.1.2: OpenPGP_0xB0DE9DD628BF132F.asc --]
[-- Type: application/pgp-keys, Size: 3135 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 495 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] xen/privcmd: allow fetching resource sizes
  2021-01-11 15:29 [PATCH] xen/privcmd: allow fetching resource sizes Roger Pau Monne
  2021-01-11 22:09 ` boris.ostrovsky
@ 2021-01-12  5:57 ` Jürgen Groß
  2021-01-12 10:03   ` Roger Pau Monné
  1 sibling, 1 reply; 8+ messages in thread
From: Jürgen Groß @ 2021-01-12  5:57 UTC (permalink / raw)
  To: Roger Pau Monne, linux-kernel
  Cc: Boris Ostrovsky, Stefano Stabellini, Paul Durrant, xen-devel


[-- Attachment #1.1.1: Type: text/plain, Size: 515 bytes --]

On 11.01.21 16:29, Roger Pau Monne wrote:
> 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.
> 
> Fixes: 3ad0876554caf ('xen/privcmd: add IOCTL_PRIVCMD_MMAP_RESOURCE')

I don't think this addition is a reason to add a "Fixes:" tag. This is
clearly new functionality.


Juergen

[-- Attachment #1.1.2: OpenPGP_0xB0DE9DD628BF132F.asc --]
[-- Type: application/pgp-keys, Size: 3135 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 495 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] xen/privcmd: allow fetching resource sizes
  2021-01-12  5:50     ` Jürgen Groß
@ 2021-01-12  6:01       ` Jürgen Groß
  0 siblings, 0 replies; 8+ messages in thread
From: Jürgen Groß @ 2021-01-12  6:01 UTC (permalink / raw)
  To: Andrew Cooper, boris.ostrovsky, Roger Pau Monne, linux-kernel
  Cc: Stefano Stabellini, Paul Durrant, xen-devel, Andrew Cooper


[-- Attachment #1.1.1: Type: text/plain, Size: 2159 bytes --]

On 12.01.21 06:50, Jürgen Groß wrote:
> On 11.01.21 23:39, Andrew Cooper wrote:
>> On 11/01/2021 22:09, boris.ostrovsky@oracle.com wrote:
>>> On 1/11/21 10:29 AM, Roger Pau Monne wrote:
>>>> +    xdata.domid = kdata.dom;
>>>> +    xdata.type = kdata.type;
>>>> +    xdata.id = kdata.id;
>>>> +
>>>> +    if (!kdata.addr && !kdata.num) {
>>>
>>> I think we should not allow only one of them to be zero. If it's only 
>>> kdata.num then we will end up with pfns array set to ZERO_SIZE_PTR 
>>> (which is 0x10). We seem to be OK in that we are not derefencing pfns 
>>> (either in kernel or in hypervisor) if number of frames is zero but 
>>> IMO we shouldn't be tempting the fate.
>>>
>>>
>>> (And if it's only kdata.addr then we will get a vma but I am not sure 
>>> it will do what we want.)
>>
>> Passing addr == 0 without num being 0 is already an error in Xen, and
>> passing num == 0 without addr being 0 is bogus and will be an error by
>> the time I'm finished fixing this.
>>
>> FWIW, the common usecase for non-trivial examples will be:
>>
>> xenforeignmem_resource_size(domid, type, id, &size);
>> xenforeignmem_map_resource(domid, type, id, NULL, size, ...);
>>
>> which translates into:
>>
>> ioctl(MAP_RESOURCE, NULL, 0) => size
>> mmap(NULL, size, ...) => ptr
>> ioctl(MAP_RESOURCE, ptr, size)
>>
>> from the kernels point of view, and two hypercalls from Xen's point of
>> view.  The NULL's above are expected to be the common case for letting
>> the kernel chose the vma, but ought to be filled in by the time the
>> second ioctl() occurs.
>>
>> See
>> https://lore.kernel.org/xen-devel/20200922182444.12350-1-andrew.cooper3@citrix.com/T/#u 
>>
>> for all the gory details.
> 
> I don't think the kernel should rely on the hypervisor to return
> an error in case addr != 0 and num == 0.
> 
> The driver should return -EINVAL in that case IMO.

And additionally I think the kernel should check num to be not too
large (in the interface it is u64, while intermediate values are
stored in unsigned int), limiting it to something below INT_MAX
seems to be sensible.


Juergen

[-- Attachment #1.1.2: OpenPGP_0xB0DE9DD628BF132F.asc --]
[-- Type: application/pgp-keys, Size: 3135 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 495 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] xen/privcmd: allow fetching resource sizes
  2021-01-12  5:57 ` Jürgen Groß
@ 2021-01-12 10:03   ` Roger Pau Monné
  2021-01-12 10:34     ` Jürgen Groß
  0 siblings, 1 reply; 8+ messages in thread
From: Roger Pau Monné @ 2021-01-12 10:03 UTC (permalink / raw)
  To: Jürgen Groß
  Cc: linux-kernel, Boris Ostrovsky, Stefano Stabellini, Paul Durrant,
	xen-devel

On Tue, Jan 12, 2021 at 06:57:30AM +0100, Jürgen Groß wrote:
> On 11.01.21 16:29, Roger Pau Monne wrote:
> > 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.
> > 
> > Fixes: 3ad0876554caf ('xen/privcmd: add IOCTL_PRIVCMD_MMAP_RESOURCE')
> 
> I don't think this addition is a reason to add a "Fixes:" tag. This is
> clearly new functionality.

It could be argued that not allowing to query the resource size was a
shortcoming of the original implementation, but a backport request to
stable would be more appropriate than a fixes tag I think. Will drop
on next version and add a backport request if you agree.

Thanks, Roger.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] xen/privcmd: allow fetching resource sizes
  2021-01-12 10:03   ` Roger Pau Monné
@ 2021-01-12 10:34     ` Jürgen Groß
  0 siblings, 0 replies; 8+ messages in thread
From: Jürgen Groß @ 2021-01-12 10:34 UTC (permalink / raw)
  To: Roger Pau Monné
  Cc: linux-kernel, Boris Ostrovsky, Stefano Stabellini, Paul Durrant,
	xen-devel


[-- Attachment #1.1.1: Type: text/plain, Size: 946 bytes --]

On 12.01.21 11:03, Roger Pau Monné wrote:
> On Tue, Jan 12, 2021 at 06:57:30AM +0100, Jürgen Groß wrote:
>> On 11.01.21 16:29, Roger Pau Monne wrote:
>>> 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.
>>>
>>> Fixes: 3ad0876554caf ('xen/privcmd: add IOCTL_PRIVCMD_MMAP_RESOURCE')
>>
>> I don't think this addition is a reason to add a "Fixes:" tag. This is
>> clearly new functionality.
> 
> It could be argued that not allowing to query the resource size was a
> shortcoming of the original implementation, but a backport request to
> stable would be more appropriate than a fixes tag I think. Will drop
> on next version and add a backport request if you agree.

Yes, please.


Juergen

[-- Attachment #1.1.2: OpenPGP_0xB0DE9DD628BF132F.asc --]
[-- Type: application/pgp-keys, Size: 3135 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 495 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2021-01-12 10:35 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-11 15:29 [PATCH] xen/privcmd: allow fetching resource sizes Roger Pau Monne
2021-01-11 22:09 ` boris.ostrovsky
2021-01-11 22:39   ` Andrew Cooper
2021-01-12  5:50     ` Jürgen Groß
2021-01-12  6:01       ` Jürgen Groß
2021-01-12  5:57 ` Jürgen Groß
2021-01-12 10:03   ` Roger Pau Monné
2021-01-12 10:34     ` Jürgen Groß

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®