From: Konrad Rzeszutek Wilk Subject: [PATCH 02/14] libxl: Propagate errno from hypercall instead of anything else. Date: Mon, 16 Mar 2015 11:39:31 -0400 Message-ID: <1426520383-20855-3-git-send-email-konrad.wilk@oracle.com> References: <1426520383-20855-1-git-send-email-konrad.wilk@oracle.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: Received: from mail6.bemta14.messagelabs.com ([193.109.254.103]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1YXX7T-0004kF-Nf for xen-devel@lists.xenproject.org; Mon, 16 Mar 2015 15:39:55 +0000 In-Reply-To: <1426520383-20855-1-git-send-email-konrad.wilk@oracle.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel@lists.xenproject.org, ian.jackson@eu.citrix.com, stefano.stabellini@eu.citrix.com, ian.campbell@citrix.com, wei.liu2@citrix.com Cc: Konrad Rzeszutek Wilk List-Id: xen-devel@lists.xenproject.org After we have done the hypercall - the errno has the failure code. However our usage of pthread and munmap can trigger them to manipulate the errno with their failure values. That would be bad as what we care about is just the hypercall error value. Another solution to this would be to save the 'errno' from pthread/munmap/madvise as an extra parameter to be analyzed later. However the call-sites above us do not care about it. Signed-off-by: Konrad Rzeszutek Wilk --- tools/libxc/xc_freebsd_osdep.c | 3 +++ tools/libxc/xc_hcall_buf.c | 6 ++++++ tools/libxc/xc_linux_osdep.c | 3 +++ 3 files changed, 12 insertions(+) diff --git a/tools/libxc/xc_freebsd_osdep.c b/tools/libxc/xc_freebsd_osdep.c index 151d3bf..bb6d240 100644 --- a/tools/libxc/xc_freebsd_osdep.c +++ b/tools/libxc/xc_freebsd_osdep.c @@ -125,10 +125,13 @@ static void freebsd_privcmd_free_hypercall_buffer(xc_interface *xch, int npages) { + int saved_errno = errno; /* Unlock pages */ munlock(ptr, npages * XC_PAGE_SIZE); munmap(ptr, npages * XC_PAGE_SIZE); + /* We MUST propagate the hypercall errno, not unmap calls. */ + errno = saved_errno; } static int freebsd_privcmd_hypercall(xc_interface *xch, xc_osdep_handle h, diff --git a/tools/libxc/xc_hcall_buf.c b/tools/libxc/xc_hcall_buf.c index e762a93..f5426a1 100644 --- a/tools/libxc/xc_hcall_buf.c +++ b/tools/libxc/xc_hcall_buf.c @@ -33,16 +33,22 @@ pthread_mutex_t hypercall_buffer_cache_mutex = PTHREAD_MUTEX_INITIALIZER; static void hypercall_buffer_cache_lock(xc_interface *xch) { + int saved_errno = errno; if ( xch->flags & XC_OPENFLAG_NON_REENTRANT ) return; pthread_mutex_lock(&hypercall_buffer_cache_mutex); + /* Ignore the pthread errors. */ + errno = saved_errno; } static void hypercall_buffer_cache_unlock(xc_interface *xch) { + int saved_errno = errno; if ( xch->flags & XC_OPENFLAG_NON_REENTRANT ) return; pthread_mutex_unlock(&hypercall_buffer_cache_mutex); + /* Ignore the pthread errors. */ + errno = saved_errno; } static void *hypercall_buffer_cache_alloc(xc_interface *xch, int nr_pages) diff --git a/tools/libxc/xc_linux_osdep.c b/tools/libxc/xc_linux_osdep.c index a19e4b6..15d772b 100644 --- a/tools/libxc/xc_linux_osdep.c +++ b/tools/libxc/xc_linux_osdep.c @@ -122,10 +122,13 @@ out: static void linux_privcmd_free_hypercall_buffer(xc_interface *xch, xc_osdep_handle h, void *ptr, int npages) { + int saved_errno = errno; /* Recover the VMA flags. Maybe it's not necessary */ madvise(ptr, npages * XC_PAGE_SIZE, MADV_DOFORK); munmap(ptr, npages * XC_PAGE_SIZE); + /* We MUST propagate the hypercall errno, not unmap calls. */ + errno = saved_errno; } static int linux_privcmd_hypercall(xc_interface *xch, xc_osdep_handle h, privcmd_hypercall_t *hypercall) -- 2.1.0