xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] tools/libxengnttab: correct size of allocated memory
@ 2020-05-20  8:35 Juergen Gross
  2020-05-20 14:49 ` Ian Jackson
  0 siblings, 1 reply; 8+ messages in thread
From: Juergen Gross @ 2020-05-20  8:35 UTC (permalink / raw)
  To: xen-devel; +Cc: Juergen Gross, Ian Jackson, Wei Liu

The size of the memory allocated for the IOCTL_GNTDEV_MAP_GRANT_REF
ioctl() parameters is calculated wrong, which results in too much
memory allocated.

Signed-off-by: Juergen Gross <jgross@suse.com>
---
 tools/libs/gnttab/freebsd.c | 2 +-
 tools/libs/gnttab/linux.c   | 8 +++-----
 2 files changed, 4 insertions(+), 6 deletions(-)

diff --git a/tools/libs/gnttab/freebsd.c b/tools/libs/gnttab/freebsd.c
index 886b588303..0588501d0f 100644
--- a/tools/libs/gnttab/freebsd.c
+++ b/tools/libs/gnttab/freebsd.c
@@ -74,7 +74,7 @@ void *osdep_gnttab_grant_map(xengnttab_handle *xgt,
     void *addr = NULL;
     int domids_stride;
     unsigned int refs_size = ROUNDUP(count *
-                                     sizeof(struct ioctl_gntdev_map_grant_ref),
+                                     sizeof(struct ioctl_gntdev_grant_ref),
                                      PAGE_SHIFT);
 
     domids_stride = (flags & XENGNTTAB_GRANT_MAP_SINGLE_DOMAIN) ? 0 : 1;
diff --git a/tools/libs/gnttab/linux.c b/tools/libs/gnttab/linux.c
index a01bb6c698..74331a4c7b 100644
--- a/tools/libs/gnttab/linux.c
+++ b/tools/libs/gnttab/linux.c
@@ -91,9 +91,7 @@ void *osdep_gnttab_grant_map(xengnttab_handle *xgt,
 {
     int fd = xgt->fd;
     struct ioctl_gntdev_map_grant_ref *map;
-    unsigned int map_size = ROUNDUP((sizeof(*map) + (count - 1) *
-                                    sizeof(struct ioctl_gntdev_map_grant_ref)),
-                                    PAGE_SHIFT);
+    unsigned int map_size = sizeof(*map) + (count - 1) * sizeof(map->refs[0]);
     void *addr = NULL;
     int domids_stride = 1;
     int i;
@@ -102,10 +100,10 @@ void *osdep_gnttab_grant_map(xengnttab_handle *xgt,
         domids_stride = 0;
 
     if ( map_size <= PAGE_SIZE )
-        map = alloca(sizeof(*map) +
-                     (count - 1) * sizeof(struct ioctl_gntdev_map_grant_ref));
+        map = alloca(map_size);
     else
     {
+        map_size = ROUNDUP(map_size, PAGE_SHIFT);
         map = mmap(NULL, map_size, PROT_READ | PROT_WRITE,
                    MAP_PRIVATE | MAP_ANON | MAP_POPULATE, -1, 0);
         if ( map == MAP_FAILED )
-- 
2.26.1



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

* Re: [PATCH] tools/libxengnttab: correct size of allocated memory
  2020-05-20  8:35 [PATCH] tools/libxengnttab: correct size of allocated memory Juergen Gross
@ 2020-05-20 14:49 ` Ian Jackson
  2020-05-20 15:56   ` Roger Pau Monné
  0 siblings, 1 reply; 8+ messages in thread
From: Ian Jackson @ 2020-05-20 14:49 UTC (permalink / raw)
  To: Juergen Gross; +Cc: xen-devel, Wei Liu, Roger Pau Monné

Juergen Gross writes ("[PATCH] tools/libxengnttab: correct size of allocated memory"):
> The size of the memory allocated for the IOCTL_GNTDEV_MAP_GRANT_REF
> ioctl() parameters is calculated wrong, which results in too much
> memory allocated.

Added Roger to CC.

Firstly,

Reviewed-by: Ian Jackson <ian.jackson@eu.citrix.com>

Thank you.


But, looking at this code, why on earth what the ?

The FreeBSD code checks to see if it's less than a page and if so uses
malloc and otherwise uses mmap !  Why not unconditionally use malloc ?

Likewise, the Linux code has its own mmap-based memory-obtainer.  ISTM
that malloc is probably going to be better.  Often it will be able to
give out even a substantial amount without making a syscall.

Essentially, we have two (similar but not identical) tiny custom
memory allocators here.  Also, the Linux and FreeBSD code are
remarkably similar which bothers me.

Anyway, these observations are no criticism of Juergen's patch.

Regards,
Ian.


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

* Re: [PATCH] tools/libxengnttab: correct size of allocated memory
  2020-05-20 14:49 ` Ian Jackson
@ 2020-05-20 15:56   ` Roger Pau Monné
  2020-06-11 15:25     ` Jürgen Groß
  0 siblings, 1 reply; 8+ messages in thread
From: Roger Pau Monné @ 2020-05-20 15:56 UTC (permalink / raw)
  To: Ian Jackson; +Cc: Juergen Gross, xen-devel, Wei Liu

On Wed, May 20, 2020 at 03:49:59PM +0100, Ian Jackson wrote:
> Juergen Gross writes ("[PATCH] tools/libxengnttab: correct size of allocated memory"):
> > The size of the memory allocated for the IOCTL_GNTDEV_MAP_GRANT_REF
> > ioctl() parameters is calculated wrong, which results in too much
> > memory allocated.
> 
> Added Roger to CC.
> 
> Firstly,
> 
> Reviewed-by: Ian Jackson <ian.jackson@eu.citrix.com>

For the FreeBSD bits:

Reviewed-by: Roger Pau Monné <roger.pau@citrix.com>

> 
> Thank you.
> 
> 
> But, looking at this code, why on earth what the ?
> 
> The FreeBSD code checks to see if it's less than a page and if so uses
> malloc and otherwise uses mmap !  Why not unconditionally use malloc ?
> 
> Likewise, the Linux code has its own mmap-based memory-obtainer.  ISTM
> that malloc is probably going to be better.  Often it will be able to
> give out even a substantial amount without making a syscall.
> 
> Essentially, we have two (similar but not identical) tiny custom
> memory allocators here.  Also, the Linux and FreeBSD code are
> remarkably similar which bothers me.

Right. This is due to the FreeBSD file being mostly a clone of the
Linux one. I agree the duplication could be abstracted away.

I really have no idea why malloc or mmap is used, maybe at some point
requesting regions > PAGE_SIZE was considered faster using mmap
directly?

Thanks, Roger.


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

* Re: [PATCH] tools/libxengnttab: correct size of allocated memory
  2020-05-20 15:56   ` Roger Pau Monné
@ 2020-06-11 15:25     ` Jürgen Groß
  2020-06-11 16:02       ` Paul Durrant
  0 siblings, 1 reply; 8+ messages in thread
From: Jürgen Groß @ 2020-06-11 15:25 UTC (permalink / raw)
  To: Roger Pau Monné, Ian Jackson; +Cc: xen-devel, Wei Liu

On 20.05.20 17:56, Roger Pau Monné wrote:
> On Wed, May 20, 2020 at 03:49:59PM +0100, Ian Jackson wrote:
>> Juergen Gross writes ("[PATCH] tools/libxengnttab: correct size of allocated memory"):
>>> The size of the memory allocated for the IOCTL_GNTDEV_MAP_GRANT_REF
>>> ioctl() parameters is calculated wrong, which results in too much
>>> memory allocated.
>>
>> Added Roger to CC.
>>
>> Firstly,
>>
>> Reviewed-by: Ian Jackson <ian.jackson@eu.citrix.com>
> 
> For the FreeBSD bits:
> 
> Reviewed-by: Roger Pau Monné <roger.pau@citrix.com>

Any reason the patch didn't go in yet?


Juergen


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

* RE: [PATCH] tools/libxengnttab: correct size of allocated memory
  2020-06-11 15:25     ` Jürgen Groß
@ 2020-06-11 16:02       ` Paul Durrant
  2020-06-15 14:07         ` Ian Jackson
  0 siblings, 1 reply; 8+ messages in thread
From: Paul Durrant @ 2020-06-11 16:02 UTC (permalink / raw)
  To: 'Jürgen Groß', 'Roger Pau Monné',
	'Ian Jackson'
  Cc: xen-devel, 'Wei Liu'

> -----Original Message-----
> From: Xen-devel <xen-devel-bounces@lists.xenproject.org> On Behalf Of Jürgen Groß
> Sent: 11 June 2020 16:26
> To: Roger Pau Monné <roger.pau@citrix.com>; Ian Jackson <ian.jackson@citrix.com>
> Cc: xen-devel@lists.xenproject.org; Wei Liu <wl@xen.org>
> Subject: Re: [PATCH] tools/libxengnttab: correct size of allocated memory
> 
> On 20.05.20 17:56, Roger Pau Monné wrote:
> > On Wed, May 20, 2020 at 03:49:59PM +0100, Ian Jackson wrote:
> >> Juergen Gross writes ("[PATCH] tools/libxengnttab: correct size of allocated memory"):
> >>> The size of the memory allocated for the IOCTL_GNTDEV_MAP_GRANT_REF
> >>> ioctl() parameters is calculated wrong, which results in too much
> >>> memory allocated.
> >>
> >> Added Roger to CC.
> >>
> >> Firstly,
> >>
> >> Reviewed-by: Ian Jackson <ian.jackson@eu.citrix.com>
> >
> > For the FreeBSD bits:
> >
> > Reviewed-by: Roger Pau Monné <roger.pau@citrix.com>
> 
> Any reason the patch didn't go in yet?
> 

I'd be quite happy for this to go in now, consider it

Release-acked-by: Paul Durrant <paul@xen.org>

> 
> Juergen




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

* RE: [PATCH] tools/libxengnttab: correct size of allocated memory
  2020-06-11 16:02       ` Paul Durrant
@ 2020-06-15 14:07         ` Ian Jackson
  2020-06-15 14:09           ` Andrew Cooper
  0 siblings, 1 reply; 8+ messages in thread
From: Ian Jackson @ 2020-06-15 14:07 UTC (permalink / raw)
  To: paul
  Cc: 'Jürgen Groß', xen-devel, 'Wei Liu',
	Roger Pau Monne

Paul Durrant writes ("RE: [PATCH] tools/libxengnttab: correct size of allocated memory"):
> > -----Original Message-----
> > From: Xen-devel <xen-devel-bounces@lists.xenproject.org> On Behalf Of Jürgen Groß
> > Sent: 11 June 2020 16:26
> > To: Roger Pau Monné <roger.pau@citrix.com>; Ian Jackson <ian.jackson@citrix.com>
> > Cc: xen-devel@lists.xenproject.org; Wei Liu <wl@xen.org>
> > Subject: Re: [PATCH] tools/libxengnttab: correct size of allocated memory
> > 
> > On 20.05.20 17:56, Roger Pau Monné wrote:
> > > On Wed, May 20, 2020 at 03:49:59PM +0100, Ian Jackson wrote:
> > >> Juergen Gross writes ("[PATCH] tools/libxengnttab: correct size of allocated memory"):
> > >>> The size of the memory allocated for the IOCTL_GNTDEV_MAP_GRANT_REF
> > >>> ioctl() parameters is calculated wrong, which results in too much
> > >>> memory allocated.
> > >>
> > >> Added Roger to CC.
> > >>
> > >> Firstly,
> > >>
> > >> Reviewed-by: Ian Jackson <ian.jackson@eu.citrix.com>
> > >
> > > For the FreeBSD bits:
> > >
> > > Reviewed-by: Roger Pau Monné <roger.pau@citrix.com>
> > 
> > Any reason the patch didn't go in yet?
> > 
> 
> I'd be quite happy for this to go in now, consider it
> 
> Release-acked-by: Paul Durrant <paul@xen.org>

Thanks.  I tried to apply this but it doesn't seem to apply to
staging.  Jürgen, would you care to rebase/resend ?

Thanks,
Ian.


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

* Re: [PATCH] tools/libxengnttab: correct size of allocated memory
  2020-06-15 14:07         ` Ian Jackson
@ 2020-06-15 14:09           ` Andrew Cooper
  2020-06-15 14:57             ` Ian Jackson
  0 siblings, 1 reply; 8+ messages in thread
From: Andrew Cooper @ 2020-06-15 14:09 UTC (permalink / raw)
  To: Ian Jackson, paul
  Cc: 'Jürgen Groß', xen-devel, 'Wei Liu',
	Roger Pau Monne

On 15/06/2020 15:07, Ian Jackson wrote:
> Paul Durrant writes ("RE: [PATCH] tools/libxengnttab: correct size of allocated memory"):
>>> -----Original Message-----
>>> From: Xen-devel <xen-devel-bounces@lists.xenproject.org> On Behalf Of Jürgen Groß
>>> Sent: 11 June 2020 16:26
>>> To: Roger Pau Monné <roger.pau@citrix.com>; Ian Jackson <ian.jackson@citrix.com>
>>> Cc: xen-devel@lists.xenproject.org; Wei Liu <wl@xen.org>
>>> Subject: Re: [PATCH] tools/libxengnttab: correct size of allocated memory
>>>
>>> On 20.05.20 17:56, Roger Pau Monné wrote:
>>>> On Wed, May 20, 2020 at 03:49:59PM +0100, Ian Jackson wrote:
>>>>> Juergen Gross writes ("[PATCH] tools/libxengnttab: correct size of allocated memory"):
>>>>>> The size of the memory allocated for the IOCTL_GNTDEV_MAP_GRANT_REF
>>>>>> ioctl() parameters is calculated wrong, which results in too much
>>>>>> memory allocated.
>>>>> Added Roger to CC.
>>>>>
>>>>> Firstly,
>>>>>
>>>>> Reviewed-by: Ian Jackson <ian.jackson@eu.citrix.com>
>>>> For the FreeBSD bits:
>>>>
>>>> Reviewed-by: Roger Pau Monné <roger.pau@citrix.com>
>>> Any reason the patch didn't go in yet?
>>>
>> I'd be quite happy for this to go in now, consider it
>>
>> Release-acked-by: Paul Durrant <paul@xen.org>
> Thanks.  I tried to apply this but it doesn't seem to apply to
> staging.  Jürgen, would you care to rebase/resend ?

I already committed it, seeing as it was fully acked.

https://xenbits.xen.org/gitweb/?p=xen.git;a=commitdiff;h=aad20e538d7ba0e36f5ed8d2bebb74096e3a6d7f

~Andrew


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

* Re: [PATCH] tools/libxengnttab: correct size of allocated memory
  2020-06-15 14:09           ` Andrew Cooper
@ 2020-06-15 14:57             ` Ian Jackson
  0 siblings, 0 replies; 8+ messages in thread
From: Ian Jackson @ 2020-06-15 14:57 UTC (permalink / raw)
  To: Andrew Cooper
  Cc: 'Jürgen Groß',
	xen-devel, Roger Pau Monne, 'Wei Liu',
	paul

Andrew Cooper writes ("Re: [PATCH] tools/libxengnttab: correct size of allocated memory"):
> I already committed it, seeing as it was fully acked.
> 
> https://xenbits.xen.org/gitweb/?p=xen.git;a=commitdiff;h=aad20e538d7ba0e36f5ed8d2bebb74096e3a6d7f

That must be why it didn't apply :-).

Thanks,
Ian.


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

end of thread, other threads:[~2020-06-15 14:58 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-20  8:35 [PATCH] tools/libxengnttab: correct size of allocated memory Juergen Gross
2020-05-20 14:49 ` Ian Jackson
2020-05-20 15:56   ` Roger Pau Monné
2020-06-11 15:25     ` Jürgen Groß
2020-06-11 16:02       ` Paul Durrant
2020-06-15 14:07         ` Ian Jackson
2020-06-15 14:09           ` Andrew Cooper
2020-06-15 14:57             ` Ian Jackson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).