From patchwork Fri Jan 15 13:22:54 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Campbell X-Patchwork-Id: 8040411 Return-Path: X-Original-To: patchwork-xen-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id B7E92BEEE5 for ; Fri, 15 Jan 2016 13:25:48 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id CADED20499 for ; Fri, 15 Jan 2016 13:25:47 +0000 (UTC) Received: from lists.xen.org (lists.xenproject.org [50.57.142.19]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id D619320459 for ; Fri, 15 Jan 2016 13:25:45 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xen.org) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1aK4Ll-00048q-S4; Fri, 15 Jan 2016 13:23:33 +0000 Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1aK4Lh-00040U-QV for xen-devel@lists.xen.org; Fri, 15 Jan 2016 13:23:30 +0000 Received: from [85.158.139.211] by server-13.bemta-5.messagelabs.com id 0D/32-06091-1D2F8965; Fri, 15 Jan 2016 13:23:29 +0000 X-Env-Sender: prvs=815b692d9=Ian.Campbell@citrix.com X-Msg-Ref: server-12.tower-206.messagelabs.com!1452864204!16117711!2 X-Originating-IP: [66.165.176.63] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogNjYuMTY1LjE3Ni42MyA9PiAzMDYwNDg=\n, received_headers: No Received headers X-StarScan-Received: X-StarScan-Version: 7.35.1; banners=-,-,- X-VirusChecked: Checked Received: (qmail 29817 invoked from network); 15 Jan 2016 13:23:28 -0000 Received: from smtp02.citrix.com (HELO SMTP02.CITRIX.COM) (66.165.176.63) by server-12.tower-206.messagelabs.com with RC4-SHA encrypted SMTP; 15 Jan 2016 13:23:28 -0000 X-IronPort-AV: E=Sophos;i="5.22,299,1449532800"; d="scan'208";a="331644608" From: Ian Campbell To: , , Date: Fri, 15 Jan 2016 13:22:54 +0000 Message-ID: <1452864188-2417-16-git-send-email-ian.campbell@citrix.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1452864188-2417-1-git-send-email-ian.campbell@citrix.com> References: <1452864168.32341.97.camel@citrix.com> <1452864188-2417-1-git-send-email-ian.campbell@citrix.com> MIME-Version: 1.0 X-DLP: MIA1 Cc: Ian Campbell Subject: [Xen-devel] [PATCH XEN v8 15/29] tools/libs/foreignmemory: Support err == NULL to map. X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00, RCVD_IN_BRBL_LASTEXT, RCVD_IN_DNSWL_MED, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The existing xc_map_foreign_bulk-like interface encourages callers to miss error checking for partial failure (by forgetting to scan the err array). Add support for passing err==NULL which behaves in a xc_map_foreign_pages-like manner and returns a global error for any failure. While documenting this also clarify the overall behaviour and the behaviour with err!=NULL. With this the compat wrapper of xc_map_foreign_pages() can be simplified. Signed-off-by: Ian Campbell Acked-by: Wei Liu Acked-by: Ian Jackson --- v7: Check for NULL when allocating err. v6: New --- tools/libs/foreignmemory/core.c | 33 +++++++++++++++++++++- .../libs/foreignmemory/include/xenforeignmemory.h | 24 ++++++++++++++-- tools/libxc/xc_foreign_memory.c | 22 +-------------- 3 files changed, 54 insertions(+), 25 deletions(-) diff --git a/tools/libs/foreignmemory/core.c b/tools/libs/foreignmemory/core.c index 21dc7ee..4e0541f 100644 --- a/tools/libs/foreignmemory/core.c +++ b/tools/libs/foreignmemory/core.c @@ -14,6 +14,8 @@ */ #include +#include +#include #include "private.h" @@ -64,7 +66,36 @@ void *xenforeignmemory_map(xenforeignmemory_handle *fmem, uint32_t dom, int prot, const xen_pfn_t *arr, int *err, size_t num) { - return osdep_xenforeignmemory_map(fmem, dom, prot, arr, err, num); + void *ret; + int *err_to_free = NULL; + + if ( err == NULL ) + err = err_to_free = malloc(num * sizeof(int)); + + if ( err == NULL ) + return NULL; + + ret = osdep_xenforeignmemory_map(fmem, dom, prot, arr, err, num); + + if ( ret == 0 && err_to_free ) + { + int i; + + for ( i = 0 ; i < num ; i++ ) + { + if ( err[i] ) + { + errno = -err[i]; + (void)osdep_xenforeignmemory_unmap(fmem, ret, num); + ret = NULL; + break; + } + } + } + + free(err_to_free); + + return ret; } int xenforeignmemory_unmap(xenforeignmemory_handle *fmem, diff --git a/tools/libs/foreignmemory/include/xenforeignmemory.h b/tools/libs/foreignmemory/include/xenforeignmemory.h index a6d1bdb..0ec6325 100644 --- a/tools/libs/foreignmemory/include/xenforeignmemory.h +++ b/tools/libs/foreignmemory/include/xenforeignmemory.h @@ -80,10 +80,28 @@ int xenforeignmemory_close(xenforeignmemory_handle *fmem); * * prot is as for mmap(2). * - * Can partially succeed. When a page cannot be mapped, its respective - * field in @err is set to the corresponding errno value. + * @arr is an array of @pages gfns to be mapped linearly in the local + * address range. @err is an (optional) output array used to report + * per-page errors, as errno values. * - * Returns NULL if no pages can be mapped. + * If @err is given (is non-NULL) then the mapping may partially + * succeed and return a valid pointer while also using @err to + * indicate the success (0) or failure (errno value) of the individual + * pages. The global errno thread local variable is not valid in this + * case. + * + * If @err is not given (is NULL) then on failure to map any page any + * successful mappings will be undone and NULL will be returned. errno + * will be set to correspond to the first failure (which may not be + * the most critical). + * + * It is also possible to return NULL due to a complete failure, + * i.e. failure to even attempt the mapping, in this case the global + * errno will have been set and the contents of @err (if given) is + * invalid. + * + * Note that it is also possible to return non-NULL with the contents + * of @err indicating failure to map every page. */ void *xenforeignmemory_map(xenforeignmemory_handle *fmem, uint32_t dom, int prot, const xen_pfn_t *arr, int *err, diff --git a/tools/libxc/xc_foreign_memory.c b/tools/libxc/xc_foreign_memory.c index 1737c10..4b24388 100644 --- a/tools/libxc/xc_foreign_memory.c +++ b/tools/libxc/xc_foreign_memory.c @@ -23,32 +23,12 @@ void *xc_map_foreign_pages(xc_interface *xch, uint32_t dom, int prot, const xen_pfn_t *arr, int num) { - void *res; - int i, *err; - if (num < 0) { errno = EINVAL; return NULL; } - err = malloc(num * sizeof(*err)); - if (!err) - return NULL; - - res = xenforeignmemory_map(xch->fmem, dom, prot, arr, err, num); - if (res) { - for (i = 0; i < num; i++) { - if (err[i]) { - errno = -err[i]; - munmap(res, num * PAGE_SIZE); - res = NULL; - break; - } - } - } - - free(err); - return res; + return xenforeignmemory_map(xch->fmem, dom, prot, arr, NULL, num); } void *xc_map_foreign_range(xc_interface *xch,