From: Ian Campbell Subject: Re: xend segfaults when starting Date: Wed, 18 Aug 2010 13:14:19 +0100 Message-ID: <1282133659.3170.922.camel@zakaz.uk.xensource.com> References: <201008041512.12768.Christoph.Egger@amd.com> <1281532314.3170.45.camel@zakaz.uk.xensource.com> <201008181244.24578.Christoph.Egger@amd.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-CsMt4Q6P05Sq/0oy4iOL" Return-path: In-Reply-To: <201008181244.24578.Christoph.Egger@amd.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: Christoph Egger Cc: "xen-devel@lists.xensource.com" , "Gianni Tedesco (3P)" , Stefano Stabellini List-Id: xen-devel@lists.xenproject.org --=-CsMt4Q6P05Sq/0oy4iOL Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit Thanks for the analysis. I'm a bit confused though. On Wed, 2010-08-18 at 11:44 +0100, Christoph Egger wrote: > I tracked down where the error happens. In safe_munlock(), > the munlock() fails. > > The trace is: > > xc_interface_close -> _xc_clean_hcall_buf -> unlock_pages -> safe_munlock -> > munlock > > hcall_buf->buf has the address 0x7f7ffdfe7040 Mustn't this be page aligned, due to hcall_buf->buf = xc_memalign(PAGE_SIZE, PAGE_SIZE); ? This appears to turn into valloc on NetBSD which (at least according to the Linux manpages) returns a page aligned result. > In unlock_pages, the address and length passed to munlock() is: > > laddr 0x7f7ffdfe7000, llen 0x2000 > The reason why munlock() fails is that mlock() hasn't been called before. > The hcall_buf_prep() is not called at all before the first call to > _xc_clean_hcall_buf(). If hcall_buf_prep() has never been called then "pthread_getspecific(hcall_buf_pkey)" should return NULL and _xc_clean_hcall_buf will never be called from xc_clean_hcall_buf. _xc_clean_hcall_buf also ignores NULL values itself. However you say that hcall_buf_pkey is not NULL, but rather contains a valid hcall_buf containing 0x7f7ffdfe7040. The only call to "pthread_setspecific(hcall_buf_pkey, ...)" with a non-NULL value is in hcall_buf_prep(), so it must have been called at some point. Please can you confirm if _xc_init_hcall_buf() is ever called and what the behaviour of "pthread_getspecific(hcall_buf_pkey)" is if _xc_init_hcall_buf() has never been called. I think it is supposed to return NULL in this case and we certainly rely on that. pthread_getspecific(hcall_buf_pkey) is supposed to return NULL on error, however hcall_buf_pkey is uninitialised until _xc_init_hcall_buf, perhaps on NetBSD the uninitialised value somehow looks valid? It's not clear what the correct value to initialise a pthread_key_t to in order for it to appear invalid until it is properly setup is, but I suppose we should be initialising it before use. Please can you try this patch: diff -r 7e4d798e8726 tools/libxc/xc_private.c --- a/tools/libxc/xc_private.c Wed Aug 18 11:29:35 2010 +0100 +++ b/tools/libxc/xc_private.c Wed Aug 18 13:12:41 2010 +0100 @@ -232,17 +236,19 @@ static void _xc_clean_hcall_buf(void *m) pthread_setspecific(hcall_buf_pkey, NULL); } +static void _xc_init_hcall_buf(void) +{ + pthread_key_create(&hcall_buf_pkey, _xc_clean_hcall_buf); +} + static void xc_clean_hcall_buf(void) { void *hcall_buf = pthread_getspecific(hcall_buf_pkey); + pthread_once(&hcall_buf_pkey_once, _xc_init_hcall_buf); + if (hcall_buf) _xc_clean_hcall_buf(hcall_buf); -} - -static void _xc_init_hcall_buf(void) -{ - pthread_key_create(&hcall_buf_pkey, _xc_clean_hcall_buf); } int hcall_buf_prep(void **addr, size_t len) If that doesn't work perhaps you can reduce the issue to a simple test case like the attached? (which doesn't reproduce the issue for me on Linux) If you can do that then please run it with the attached libxc patch and post the output. Ian. --=-CsMt4Q6P05Sq/0oy4iOL Content-Disposition: attachment; filename="libxc-mlock-debug.patch" Content-Type: text/x-patch; name="libxc-mlock-debug.patch"; charset="UTF-8" Content-Transfer-Encoding: 7bit diff -r 7e4d798e8726 tools/libxc/xc_private.c --- a/tools/libxc/xc_private.c Wed Aug 18 11:29:35 2010 +0100 +++ b/tools/libxc/xc_private.c Wed Aug 18 13:07:15 2010 +0100 @@ -195,6 +195,7 @@ int lock_pages(void *addr, size_t len) void *laddr = (void *)((unsigned long)addr & PAGE_MASK); size_t llen = (len + ((unsigned long)addr - (unsigned long)laddr) + PAGE_SIZE - 1) & PAGE_MASK; + printf("%s(%p,%zd) -> mlock(%p,%zd)\n", __func__, addr, len, laddr, llen); e = mlock(laddr, llen); return e; } @@ -204,6 +205,7 @@ void unlock_pages(void *addr, size_t len void *laddr = (void *)((unsigned long)addr & PAGE_MASK); size_t llen = (len + ((unsigned long)addr - (unsigned long)laddr) + PAGE_SIZE - 1) & PAGE_MASK; + printf("%s(%p,%zd) -> safe_munlock(%p,%zd)\n", __func__, addr, len, laddr, llen); safe_munlock(laddr, llen); } @@ -217,6 +219,8 @@ static void _xc_clean_hcall_buf(void *m) static void _xc_clean_hcall_buf(void *m) { struct hcall_buf *hcall_buf = m; + + printf("%s(%p) -> %p\n", __func__, m, hcall_buf ? hcall_buf->buf : NULL); if ( hcall_buf ) { @@ -236,6 +240,8 @@ static void xc_clean_hcall_buf(void) { void *hcall_buf = pthread_getspecific(hcall_buf_pkey); + printf("%s(%p)\n", __func__, hcall_buf); + if (hcall_buf) _xc_clean_hcall_buf(hcall_buf); } @@ -249,6 +255,7 @@ int hcall_buf_prep(void **addr, size_t l { struct hcall_buf *hcall_buf; + printf("%s(%p,%zd)\n", __func__, *addr, len); pthread_once(&hcall_buf_pkey_once, _xc_init_hcall_buf); hcall_buf = pthread_getspecific(hcall_buf_pkey); @@ -257,12 +264,14 @@ int hcall_buf_prep(void **addr, size_t l hcall_buf = calloc(1, sizeof(*hcall_buf)); if ( !hcall_buf ) goto out; + printf("%s hcall buf at %p\n", __func__, hcall_buf); pthread_setspecific(hcall_buf_pkey, hcall_buf); } if ( !hcall_buf->buf ) { hcall_buf->buf = xc_memalign(PAGE_SIZE, PAGE_SIZE); + printf("%s hcall_buf->buf at %p\n", __func__, hcall_buf->buf); if ( !hcall_buf->buf || lock_pages(hcall_buf->buf, PAGE_SIZE) ) { free(hcall_buf->buf); @@ -286,6 +295,8 @@ void hcall_buf_release(void **addr, size void hcall_buf_release(void **addr, size_t len) { struct hcall_buf *hcall_buf = pthread_getspecific(hcall_buf_pkey); + + printf("%s(%p,%zd)\n", __func__, *addr, len); if ( hcall_buf && (hcall_buf->buf == *addr) ) { --=-CsMt4Q6P05Sq/0oy4iOL Content-Disposition: attachment; filename="mlock.c" Content-Type: text/x-csrc; name="mlock.c"; charset="UTF-8" Content-Transfer-Encoding: 7bit // gcc -o mlock -Itools/include -Itools/libxc/ -Ltools/libxc/ -lxenctrl mlock.c #include #include extern int hcall_buf_prep(void **addr, size_t len); extern void hcall_buf_release(void **addr, size_t len); int main(void) { xc_interface *xc = xc_interface_open(NULL, NULL, 0); int ret; void *mybuf = malloc(0x3000); void *mybuf2 = mybuf + 0x40; printf("xc interface is %p\n", xc); printf("before prep buf is %p / %p\n", mybuf, mybuf2); hcall_buf_prep(&mybuf2, 0x200); printf("after prep buf is %p / %p\n", mybuf, mybuf2); hcall_buf_release(&mybuf2, 0x200); printf("after release buf is %p / %p\n", mybuf, mybuf2); ret = xc_interface_close(xc); printf("xc interface close returned %d\n", ret); return 0; } --=-CsMt4Q6P05Sq/0oy4iOL Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel --=-CsMt4Q6P05Sq/0oy4iOL--