Skip to contentSkip to conversation

tools/libxengnttab: correct size of allocated memory

PATCH In review Juergen Gross 5 participants 8 messages May 20, 2020 – Jun 15, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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.

---
 tools/libs/gnttab/freebsd.c | 2 +-
 tools/libs/gnttab/linux.c   | 8 +++-----
 2 files changed, 4 insertions(+), 6 deletions(-)
tools/libs/gnttab/freebsd.c +1 -1
index 886b588303..0588501d0f 100644
@@ -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;
tools/libs/gnttab/linux.c +3 -5
index a01bb6c698..74331a4c7b 100644
@@ -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 )
Signature · 1 line
2.26.1
Reviewed-by on v1 1/1
#1 lore raw

Juergen Gross writes ("[PATCH] tools/libxengnttab: correct size of allocated memory"):

Quoted 3 lines

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,

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.

Reviewed-by
#2 lore raw
Quoted 10 lines · Ian Jackson

Juergen Gross writes ("[PATCH] tools/libxengnttab: correct size of allocated memory"):

Quoted 3 lines

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,

For the FreeBSD bits:

Quoted 9 lines

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 ?

Quoted 7 lines

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.

#3 lore raw
Quoted 8 lines · Roger Pau Monné
Quoted 7 lines · Ian Jackson

Juergen Gross writes ("[PATCH] tools/libxengnttab: correct size of allocated memory"):

Quoted 3 lines

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.

Quoted 7 lines
Quoted 3 lines

Firstly,

For the FreeBSD bits:

Any reason the patch didn't go in yet?

Juergen

#4 lore raw
Quoted 18 lines

-----Original Message-----
From: Xen-devel <[email protected]> On Behalf Of Jürgen Groß
Sent: 11 June 2020 16:26
To: Roger Pau Monné <[email protected]>; Ian Jackson <[email protected]>
Cc: [email protected]; Wei Liu <[email protected]>
Subject: Re: [PATCH] tools/libxengnttab: correct size of allocated memory

Quoted 10 lines · Roger Pau Monné
Quoted 9 lines · Ian Jackson

Juergen Gross writes ("[PATCH] tools/libxengnttab: correct size of allocated memory"):

Quoted 3 lines

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,

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

For the FreeBSD bits:

Any reason the patch didn't go in yet?

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

Quoted 2 lines

Juergen

#5 lore raw

Paul Durrant writes ("RE: [PATCH] tools/libxengnttab: correct size of allocated memory"):

Quoted 22 lines
Quoted 22 lines

-----Original Message-----
From: Xen-devel <[email protected]> On Behalf Of Jürgen Groß
Sent: 11 June 2020 16:26
To: Roger Pau Monné <[email protected]>; Ian Jackson <[email protected]>
Cc: [email protected]; Wei Liu <[email protected]>
Subject: Re: [PATCH] tools/libxengnttab: correct size of allocated memory

Quoted 14 lines · Roger Pau Monné
Quoted 10 lines · Ian Jackson

Juergen Gross writes ("[PATCH] tools/libxengnttab: correct size of allocated memory"):

Quoted 3 lines

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,

For the FreeBSD bits:

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Any reason the patch didn't go in yet?

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

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.

#6 lore raw
Quoted 21 lines · Ian Jackson

Paul Durrant writes ("RE: [PATCH] tools/libxengnttab: correct size of allocated memory"):

Quoted 20 lines
Quoted 20 lines

-----Original Message-----
From: Xen-devel <[email protected]> On Behalf Of Jürgen Groß
Sent: 11 June 2020 16:26
To: Roger Pau Monné <[email protected]>; Ian Jackson <[email protected]>
Cc: [email protected]; Wei Liu <[email protected]>
Subject: Re: [PATCH] tools/libxengnttab: correct size of allocated memory

Quoted 12 lines · Roger Pau Monné
Quoted 9 lines · Ian Jackson

Juergen Gross writes ("[PATCH] tools/libxengnttab: correct size of allocated memory"):

Quoted 3 lines

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,

For the FreeBSD bits:

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Any reason the patch didn't go in yet?

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

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