Skip to contentSkip to conversation

libxc: check return values on mmap() and madvise() on xc_alloc_hypercall_buffer()

RFC In review Luis R. Rodriguez 4 participants 8 messages May 7, 2014 – May 12, 2014

lore
Share & tools
lore
#0 lore raw

RFC 1/1 libxc: check return values on mmap() and madvise() on xc_alloc_hypercall_buffer()

From: "Luis R. Rodriguez" <[email protected]>

On a Thinkpad T4440p with OpenSUSE tumbleweed with v3.15-rc4 and today's latest xen tip from the git tree strace -f reveals we end up on a never ending wait shortly after

write(20, "backend/console/5\0", 18 <unfinished ...>

I've tracked this down to a lack of error return values on mmap() and madvise() on xc_alloc_hypercall_buffer(). This moves us forward.

Cc Ian Jackson <[email protected]>
Cc Stefano Stabellini <[email protected]>
Cc Ian Campbell <[email protected]>

---

BTW I see no ldconfig run after make install, where do we want to put it given we have a few libs ?

 tools/libxc/xc_linux_osdep.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
tools/libxc/xc_linux_osdep.c +16 -1
index 73860a2..32e5332 100644
@@ -92,14 +92,29 @@ static void *linux_privcmd_alloc_hypercall_buffer(xc_interface *xch, xc_osdep_ha
 {
     size_t size = npages * XC_PAGE_SIZE;
     void *p;
+    int rc, saved_errno;
 
     /* Address returned by mmap is page aligned. */
     p = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_LOCKED, -1, 0);
+    if ( p == MAP_FAILED )
+    {
+        PERROR("xc_alloc_hypercall_buffer: mmap failed");
+        return NULL;
+    }
 
     /* Do not copy the VMA to child process on fork. Avoid the page being COW
         on hypercall. */
-    madvise(p, npages * XC_PAGE_SIZE, MADV_DONTFORK);
+    rc = madvise(p, npages * XC_PAGE_SIZE, MADV_DONTFORK);
+    if ( rc < 0 )
+	    goto out;
+
     return p;
+out:
+    saved_errno = errno;
+    PERROR("xc_alloc_hypercall_buffer: madvise failed");
+    (void)munmap(p, size);
+    errno = saved_errno;
+    return NULL;
 }
 
 static void linux_privcmd_free_hypercall_buffer(xc_interface *xch, xc_osdep_handle h, void *ptr, int npages)
Signature · 1 line
1.8.4.5
#1 lore raw
Quoted 8 lines · Luis R. Rodriguez

From: "Luis R. Rodriguez" <[email protected]>

On a Thinkpad T4440p with OpenSUSE tumbleweed with v3.15-rc4 and today's latest xen tip from the git tree strace -f reveals we end up on a never ending wait shortly after

write(20, "backend/console/5\0", 18 <unfinished ...>

Quoted 8 lines

I've tracked this down to a lack of error return values on mmap() and madvise() on xc_alloc_hypercall_buffer(). This moves us forward.

Cc Ian Jackson <[email protected]>
Cc Stefano Stabellini <[email protected]>
Cc Ian Campbell <[email protected]>

---

Good catch. I am supprised this hasn't blown up in someones face . Out of interest, which bit starts failing given correct error handling here?

Quoted 23 lines

BTW I see no ldconfig run after make install, where do we want to put it given we have a few libs ?

 tools/libxc/xc_linux_osdep.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
tools/libxc/xc_linux_osdep.c +4 -0
index 73860a2..32e5332 100644
@@ -92,14 +92,29 @@ static void *linux_privcmd_alloc_hypercall_buffer(xc_interface *xch, xc_osdep_ha
 {
     size_t size = npages * XC_PAGE_SIZE;
     void *p;
+    int rc, saved_errno;
 
     /* Address returned by mmap is page aligned. */
     p = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_LOCKED, -1, 0);
+    if ( p == MAP_FAILED )
+    {
+        PERROR("xc_alloc_hypercall_buffer: mmap failed");

tools/libxc/xc_linux_osdep.c static void *linux_privcmd_alloc_hypercall_buffer(xc_interface *xch, xc_osdep_ha · line 110

Quoted 10 lines
tools/libxc/xc_linux_osdep.c +6 -1
+        return NULL;
+    }
 
     /* Do not copy the VMA to child process on fork. Avoid the page being COW
         on hypercall. */
-    madvise(p, npages * XC_PAGE_SIZE, MADV_DONTFORK);
+    rc = madvise(p, npages * XC_PAGE_SIZE, MADV_DONTFORK);
+    if ( rc < 0 )
+	    goto out;
+

This might be cleaner like:

if ( rc == 0 )

    return p;

error cleanup;

as it is small enough to do without an out; label. Also, you can do without 'rc' if you are happy putting the madvise() in the if statement itself. 'rc' isn't subsequently referenced.

~Andrew

Quoted 10 lines
tools/libxc/xc_linux_osdep.c +6 -0
     return p;
+out:
+    saved_errno = errno;
+    PERROR("xc_alloc_hypercall_buffer: madvise failed");
+    (void)munmap(p, size);
+    errno = saved_errno;
+    return NULL;
 }
 
 static void linux_privcmd_free_hypercall_buffer(xc_interface *xch, xc_osdep_handle h, void *ptr, int npages)
#2 lore raw
Quoted 12 lines · Andrew Cooper
Quoted 11 lines · Luis R. Rodriguez

From: "Luis R. Rodriguez" <[email protected]>

On a Thinkpad T4440p with OpenSUSE tumbleweed with v3.15-rc4 and today's latest xen tip from the git tree strace -f reveals we end up on a never ending wait shortly after

write(20, "backend/console/5\0", 18 <unfinished ...>

I've tracked this down to a lack of error return values on mmap() and madvise() on xc_alloc_hypercall_buffer(). This moves us forward.

Quoted 7 lines
Quoted 5 lines
Cc Ian Jackson <[email protected]>
Cc Stefano Stabellini <[email protected]>
Cc Ian Campbell <[email protected]>

---

Good catch. I am supprised this hasn't blown up in someones face .

Tell me about it.

Quoted 1 line

Out of interest, which bit starts failing given correct error handling here?

The next part that fails is:

1742  write(22, "b", 1)                 = 1                                     
1742  read(20, "\4\0\0\0\0\0\0\0\0\0\0\0\3\0\0\0", 16) = 16                     
1742  read(20, "OK\0", 3)               = 3                                     
1735  futex(0x7f0f65173770, FUTEX_WAKE_PRIVATE, 1) = 0                          
1735  rt_sigaction(SIGPIPE, {SIG_IGN, ~[KILL STOP RTMIN RT_1], SA_RESTORER, 0x7f0f5f1cb9f0}, NULL, 8) = 0
1735  rt_sigaction(SIGPIPE, {SIG_IGN, [], SA_RESTORER, 0x7f0f5f1cb9f0}, {SIG_IGN, ~[KILL STOP RTMIN RT_1], SA_RESTORER, 0x7f0f5f1cb9f0}, 8) = 0
1735  write(20, "\1\0\0\0\0\0\0\0\0\0\0\0\22\0\0\0", 16 <unfinished ...>        
1734  <... wait4 resumed> 0x7fffa73a89ac, 0, NULL) = ? ERESTARTSYS (To be restarted if SA_RESTART is set)
1734  --- SIGWINCH {si_signo=SIGWINCH, si_code=SI_KERNEL} ---                   
1734  wait4(1735,  

On a working system this looks like:

3905  write(22, "b", 1 <unfinished ...>                                         
3892  rt_sigaction(SIGPIPE, {SIG_IGN, [], SA_RESTORER, 0x7fdd47457880},  <unfinished ...>
3905  <... write resumed> )             = 1                                     
3892  <... rt_sigaction resumed> {SIG_IGN, ~[KILL STOP RTMIN RT_1], SA_RESTORER, 0x7fdd47457880}, 8) = 0
3905  read(20,  <unfinished ...>                                                
3892  write(20, "\1\0\0\0\0\0\0\0\0\0\0\0\22\0\0\0", 16) = 16                   
3892  write(20, "backend/console/1\0", 18) = 18                                 
3905  <... read resumed> "\1\0\0\0\0\0\0\0\0\0\0\0\2\0\0\0", 16) = 16           
3892  futex(0x7fdd4dbc213c, FUTEX_WAIT_PRIVATE, 3, NULL <unfinished ...>        
3905  read(20, "0\0", 2)                = 2                                     
3905  futex(0x7fdd4dbc213c, FUTEX_WAKE_OP_PRIVATE, 1, 1, 0x7fdd4dbc2138, {FUTEX_OP_SET, 0, FUTEX_OP_CMP_GT, 1}) = 1
3892  <... futex resumed> )             = 0                                     
3905  read(20,  <unfinished ...>                                                
3892  futex(0x7fdd4dbc2110, FUTEX_WAKE_PRIVATE, 1) = 0                          
3892  rt_sigaction(SIGPIPE, {SIG_IGN, ~[KILL STOP RTMIN RT_1], SA_RESTORER, 0x7fdd47457880}, NULL, 8) = 0
3892  ioctl(2, SNDCTL_TMR_TIMEBASE or TCGETS, 0x7fff1c0e1b10) = -1 ENOTTY (Inappropriate ioctl for device)

The 1735 process should be qemu, so it ends up getting stuck on the infinite loop on libxl__spawn_spawn() waiting for qemu. Its unclear why it doesn't end.

Quoted 2 lines
Quoted 2 lines

BTW I see no ldconfig run after make install, where do we want to put it given we have a few libs ?

How about this?

Quoted 32 lines
Quoted 19 lines
 tools/libxc/xc_linux_osdep.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
tools/libxc/xc_linux_osdep.c +4 -0
index 73860a2..32e5332 100644
@@ -92,14 +92,29 @@ static void *linux_privcmd_alloc_hypercall_buffer(xc_interface *xch, xc_osdep_ha
 {
     size_t size = npages * XC_PAGE_SIZE;
     void *p;
+    int rc, saved_errno;
 
     /* Address returned by mmap is page aligned. */
     p = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_LOCKED, -1, 0);
+    if ( p == MAP_FAILED )
+    {
+        PERROR("xc_alloc_hypercall_buffer: mmap failed");

tools/libxc/xc_linux_osdep.c static void *linux_privcmd_alloc_hypercall_buffer(xc_interface *xch, xc_osdep_ha · line 110

Quoted 10 lines
tools/libxc/xc_linux_osdep.c +6 -1
+        return NULL;
+    }
 
     /* Do not copy the VMA to child process on fork. Avoid the page being COW
         on hypercall. */
-    madvise(p, npages * XC_PAGE_SIZE, MADV_DONTFORK);
+    rc = madvise(p, npages * XC_PAGE_SIZE, MADV_DONTFORK);
+    if ( rc < 0 )
+	    goto out;
+

This might be cleaner like:

Quoted 7 lines

if ( rc == 0 )

    return p;

error cleanup;

as it is small enough to do without an out; label.

Sure, that makes sesnse.

Quoted 2 lines

Also, you can do without 'rc' if you are happy putting the madvise() in the if statement itself. 'rc' isn't subsequently referenced.

OK.

Luis

#3 lore raw
Quoted 4 lines · Luis R. Rodriguez
Quoted 2 lines
Quoted 2 lines

BTW I see no ldconfig run after make install, where do we want to put it given we have a few libs ?

How about this?

Isn't this conventionally the admin's responsibility?

Quoted 36 lines
Quoted 36 lines
Quoted 20 lines
 tools/libxc/xc_linux_osdep.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
tools/libxc/xc_linux_osdep.c +4 -0
index 73860a2..32e5332 100644
@@ -92,14 +92,29 @@ static void *linux_privcmd_alloc_hypercall_buffer(xc_interface *xch, xc_osdep_ha
 {
     size_t size = npages * XC_PAGE_SIZE;
     void *p;
+    int rc, saved_errno;
 
     /* Address returned by mmap is page aligned. */
     p = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_LOCKED, -1, 0);
+    if ( p == MAP_FAILED )
+    {
+        PERROR("xc_alloc_hypercall_buffer: mmap failed");

tools/libxc/xc_linux_osdep.c static void *linux_privcmd_alloc_hypercall_buffer(xc_interface *xch, xc_osdep_ha · line 110

Quoted 10 lines
tools/libxc/xc_linux_osdep.c +6 -1
+        return NULL;
+    }
 
     /* Do not copy the VMA to child process on fork. Avoid the page being COW
         on hypercall. */
-    madvise(p, npages * XC_PAGE_SIZE, MADV_DONTFORK);
+    rc = madvise(p, npages * XC_PAGE_SIZE, MADV_DONTFORK);
+    if ( rc < 0 )
+	    goto out;
+

This might be cleaner like:

if ( rc == 0 )

    return p;
Quoted 6 lines
Quoted 4 lines

error cleanup;

as it is small enough to do without an out; label.

Sure, that makes sesnse.

The out label idiom you have used is pretty conventional and will save faff if some other operation gets added here. I'm happy with the patch as it is.

Quoted 6 lines
Quoted 2 lines

Also, you can do without 'rc' if you are happy putting the madvise() in the if statement itself. 'rc' isn't subsequently referenced.

OK.

Luis

#4 lore raw
Quoted 7 lines · Ian Campbell
Quoted 4 lines · Luis R. Rodriguez
Quoted 2 lines
Quoted 2 lines

BTW I see no ldconfig run after make install, where do we want to put it given we have a few libs ?

How about this?

Isn't this conventionally the admin's responsibility?

If that is part of the documentation of requirements :)

Quoted 10 lines
Quoted 6 lines
Quoted 4 lines

error cleanup;

as it is small enough to do without an out; label.

Sure, that makes sesnse.

The out label idiom you have used is pretty conventional and will save faff if some other operation gets added here. I'm happy with the patch as it is.

OK feel free to take it then.

BTW any idea where in code we get the trigger for what strace picks up as:

1742 write(22, "b", 1) = 1

I'm now in a state where instead of b I get "/" written and I just can't explain this yet.

Luis

#5 lore raw
Quoted 10 lines · Luis R. Rodriguez
Quoted 7 lines · Ian Campbell
Quoted 4 lines · Luis R. Rodriguez
Quoted 2 lines
Quoted 2 lines

BTW I see no ldconfig run after make install, where do we want to put it given we have a few libs ?

How about this?

Isn't this conventionally the admin's responsibility?

If that is part of the documentation of requirements :)

http://wiki.xen.org/wiki/Compiling_Xen_From_Source
Ctrl-F
ldconfig
Profit.

Quoted 12 lines
Quoted 11 lines
Quoted 7 lines
Quoted 5 lines

error cleanup;

as it is small enough to do without an out; label.

Sure, that makes sesnse.

The out label idiom you have used is pretty conventional and will save faff if some other operation gets added here. I'm happy with the patch as it is.

Quoted 5 lines

OK feel free to take it then.

BTW any idea where in code we get the trigger for what strace picks up as:

1742 write(22, "b", 1) = 1

No idea. /proc/$pid/fds might tell you what fd 22 is, which might start to give a clue.

Quoted 4 lines

I'm now in a state where instead of b I get "/" written and I just can't explain this yet.

Luis

#6 lore raw
In reply to Luis R. Rodriguez
Quoted 6 lines · Luis R. Rodriguez
Quoted 3 lines · Ian Campbell

The out label idiom you have used is pretty conventional and will save faff if some other operation gets added here. I'm happy with the patch as it is.

OK feel free to take it then.

I hadn't noticed that there was a failure specific PERROR at the out label location. For this to make sense it would need to be:

    rc = madvise(p, npages * XC_PAGE_SIZE, MADV_DONTFORK);
    if ( rc < 0 )
    {
        PERROR("xc_alloc_hypercall_buffer: madvise failed");
         goto out;
    }

    ....

out:
the cleanup

Ian.

#7 lore raw
Quoted 11 lines · Ian Campbell

I hadn't noticed that there was a failure specific PERROR at the out label location. For this to make sense it would need to be:

    rc = madvise(p, npages * XC_PAGE_SIZE, MADV_DONTFORK);
    if ( rc < 0 )
    {
        PERROR("xc_alloc_hypercall_buffer: madvise failed");
         goto out;
    }

    ....
Quoted 2 lines

out:
the cleanup

OK I'll move the PERROR() above, and will fold this into my series of systemd patches as I have a second spin coming up.

Luis