From patchwork Mon Oct 30 17:48:28 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 10032999 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 27DAA600C5 for ; Mon, 30 Oct 2017 18:00:34 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 208DD28711 for ; Mon, 30 Oct 2017 18:00:34 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1525828919; Mon, 30 Oct 2017 18:00:34 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 5D61B28711 for ; Mon, 30 Oct 2017 18:00:33 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1e9EKM-0000Ny-LS; Mon, 30 Oct 2017 17:58:22 +0000 Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1e9EKL-0000Ns-SN for xen-devel@lists.xenproject.org; Mon, 30 Oct 2017 17:58:21 +0000 Received: from [85.158.143.35] by server-11.bemta-6.messagelabs.com id 46/5B-20813-D3867F95; Mon, 30 Oct 2017 17:58:21 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFlrEIsWRWlGSWpSXmKPExsXitHSDva5Nxvd Ig+PLBCy+b5nM5MDocfjDFZYAxijWzLyk/IoE1owDdyezFLTYVByfktTA+Euni5GTQ0LAX2Lv 5dnsIDabgI7E1KeXWLsYOThEBFQkbu816GLk4mAWeM4k8XrDT7AaYYEEiXkL17KB2CwCqhJNG 5uYQGxeARuJGU2rmCBmykvsarvICmJzAsXXdfxjBrGFBKwlTt7sZoSwVSTWT53FBtErKHFy5h MWEJtZQELi4IsXzBMYeWchSc1CklrAyLSKUaM4tagstUjX0FwvqSgzPaMkNzEzR9fQwEwvN7W 4ODE9NScxqVgvOT93EyMwdBiAYAfj7Y0BhxglOZiURHl3On6PFOJLyk+pzEgszogvKs1JLT7E KMPBoSTBm54OlBMsSk1PrUjLzAEGMUxagoNHSYTXAyTNW1yQmFucmQ6ROsVoz9Fx8+4fJo5NY HLD9wdA8tnM1w3MQix5+XmpUuK89SBtAiBtGaV5cENhUXeJUVZKmJcR6EwhnoLUotzMElT5V4 ziHIxKwrymIFN4MvNK4Ha/AjqLCegsDckvIGeVJCKkpBoYA0tuzY/5dEDlx9GTV7f2KSjt065 oTZyZ8HPar0d/8uebvP090dPwjWXXEqPck2/Onji93+pO1Is1VkePrJYTl/r0JHLGzWMzfrDM mc43kXvax2WN1Y1JTk5ZQSIMt4IfrIz99/rIuYJIHb29U6Mtk6brSUYod6vwSpdF2ddMmWlVG m2qHGa+W4mlOCPRUIu5qDgRABJQTi21AgAA X-Env-Sender: prvs=469f563c5=Paul.Durrant@citrix.com X-Msg-Ref: server-7.tower-21.messagelabs.com!1509386298!79659022!1 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: 9.4.45; banners=-,-,- X-VirusChecked: Checked Received: (qmail 22956 invoked from network); 30 Oct 2017 17:58:20 -0000 Received: from smtp02.citrix.com (HELO SMTP02.CITRIX.COM) (66.165.176.63) by server-7.tower-21.messagelabs.com with RC4-SHA encrypted SMTP; 30 Oct 2017 17:58:20 -0000 X-IronPort-AV: E=Sophos;i="5.44,320,1505779200"; d="scan'208";a="456884715" From: Paul Durrant To: Date: Mon, 30 Oct 2017 17:48:28 +0000 Message-ID: <20171030174829.4518-11-paul.durrant@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20171030174829.4518-1-paul.durrant@citrix.com> References: <20171030174829.4518-1-paul.durrant@citrix.com> MIME-Version: 1.0 Cc: Stefano Stabellini , Wei Liu , Konrad Rzeszutek Wilk , George Dunlap , Andrew Cooper , Ian Jackson , Tim Deegan , Paul Durrant , Jan Beulich Subject: [Xen-devel] [PATCH v13 10/11] common: add a new mappable resource type: XENMEM_resource_grant_table X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP This patch allows grant table frames to be mapped using the XENMEM_acquire_resource memory op. NOTE: This patch expands the on-stack mfn_list array in acquire_resource() but it is still small enough to remain on-stack. Signed-off-by: Paul Durrant --- Cc: Andrew Cooper Cc: George Dunlap Cc: Ian Jackson Cc: Jan Beulich Cc: Konrad Rzeszutek Wilk Cc: Stefano Stabellini Cc: Tim Deegan Cc: Wei Liu v13: - Re-work the internals to avoid using the XENMAPIDX_grant_table_status hack. v12: - Dropped limit checks as requested by Jan. v10: - Addressed comments from Jan. v8: - The functionality was originally incorporated into the earlier patch "x86/mm: add HYPERVISOR_memory_op to acquire guest resources". --- xen/common/grant_table.c | 63 +++++++++++++++++++++++++++++++++++++------ xen/common/memory.c | 45 ++++++++++++++++++++++++++++++- xen/include/public/memory.h | 6 +++++ xen/include/xen/grant_table.h | 4 +++ 4 files changed, 109 insertions(+), 9 deletions(-) diff --git a/xen/common/grant_table.c b/xen/common/grant_table.c index 0558de9ce8..429c421cd9 100644 --- a/xen/common/grant_table.c +++ b/xen/common/grant_table.c @@ -3761,21 +3761,21 @@ int mem_sharing_gref_to_gfn(struct grant_table *gt, grant_ref_t ref, } #endif -int gnttab_map_frame(struct domain *d, unsigned long idx, gfn_t gfn, - mfn_t *mfn) +/* Caller must hold write lock as version may change and table may grow */ +static int gnttab_get_frame(struct domain *d, bool is_status, + unsigned long idx, mfn_t *mfn) { - int rc = 0; struct grant_table *gt = d->grant_table; - - grant_write_lock(gt); + int rc = 0; if ( gt->gt_version == 0 ) gt->gt_version = 1; - if ( gt->gt_version == 2 && - (idx & XENMAPIDX_grant_table_status) ) + if ( is_status ) { - idx &= ~XENMAPIDX_grant_table_status; + if ( gt->gt_version != 2 ) + return -EINVAL; + if ( idx < nr_status_frames(gt) ) *mfn = _mfn(virt_to_mfn(gt->status[idx])); else @@ -3792,6 +3792,25 @@ int gnttab_map_frame(struct domain *d, unsigned long idx, gfn_t gfn, rc = -EINVAL; } + return rc; +} + +int gnttab_map_frame(struct domain *d, unsigned long idx, gfn_t gfn, + mfn_t *mfn) +{ + struct grant_table *gt = d->grant_table; + bool is_status = false; + int rc; + + grant_write_lock(gt); + + if ( idx & XENMAPIDX_grant_table_status ) + { + is_status = true; + idx &= ~XENMAPIDX_grant_table_status; + } + + rc = gnttab_get_frame(d, is_status, idx, mfn); if ( !rc ) gnttab_set_frame_gfn(gt, idx, gfn); @@ -3800,6 +3819,34 @@ int gnttab_map_frame(struct domain *d, unsigned long idx, gfn_t gfn, return rc; } +int gnttab_get_grant_frame(struct domain *d, unsigned long idx, + mfn_t *mfn) +{ + struct grant_table *gt = d->grant_table; + int rc; + + /* write lock required as version may change and/or table may grow */ + grant_write_lock(gt); + rc = gnttab_get_frame(d, false, idx, mfn); + grant_write_unlock(gt); + + return rc; +} + +int gnttab_get_status_frame(struct domain *d, unsigned long idx, + mfn_t *mfn) +{ + struct grant_table *gt = d->grant_table; + int rc; + + /* write lock required as version may change and/or table may grow */ + grant_write_lock(gt); + rc = gnttab_get_frame(d, true, idx, mfn); + grant_write_unlock(gt); + + return rc; +} + static void gnttab_usage_print(struct domain *rd) { int first = 1; diff --git a/xen/common/memory.c b/xen/common/memory.c index 1c6932fd85..8097d85be3 100644 --- a/xen/common/memory.c +++ b/xen/common/memory.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -965,6 +966,43 @@ static long xatp_permission_check(struct domain *d, unsigned int space) return xsm_add_to_physmap(XSM_TARGET, current->domain, d); } +static int acquire_grant_table(struct domain *d, unsigned int id, + unsigned long frame, + unsigned int nr_frames, + xen_pfn_t mfn_list[]) +{ + unsigned int i = nr_frames; + + /* Iterate backwards in case table needs to grow */ + while ( i-- != 0 ) + { + mfn_t mfn = INVALID_MFN; + int rc; + + switch ( id ) + { + case XENMEM_resource_grant_table_id_grant: + rc = gnttab_get_grant_frame(d, frame + i, &mfn); + break; + + case XENMEM_resource_grant_table_id_status: + rc = gnttab_get_status_frame(d, frame + i, &mfn); + break; + + default: + rc = -EINVAL; + break; + } + + if ( rc ) + return rc; + + mfn_list[i] = mfn_x(mfn); + } + + return 0; +} + static int acquire_resource( XEN_GUEST_HANDLE_PARAM(xen_mem_acquire_resource_t) arg) { @@ -975,7 +1013,7 @@ static int acquire_resource( * moment since they are small, but if they need to grow in future * use-cases then per-CPU arrays or heap allocations may be required. */ - xen_pfn_t mfn_list[2]; + xen_pfn_t mfn_list[32]; int rc; if ( copy_from_guest(&xmar, arg, 1) ) @@ -1010,6 +1048,11 @@ static int acquire_resource( switch ( xmar.type ) { + case XENMEM_resource_grant_table: + rc = acquire_grant_table(d, xmar.id, xmar.frame, xmar.nr_frames, + mfn_list); + break; + default: rc = arch_acquire_resource(d, xmar.type, xmar.id, xmar.frame, xmar.nr_frames, mfn_list); diff --git a/xen/include/public/memory.h b/xen/include/public/memory.h index 838f248a59..d4ed8ba889 100644 --- a/xen/include/public/memory.h +++ b/xen/include/public/memory.h @@ -611,14 +611,20 @@ struct xen_mem_acquire_resource { uint16_t type; #define XENMEM_resource_ioreq_server 0 +#define XENMEM_resource_grant_table 1 /* * IN - a type-specific resource identifier, which must be zero * unless stated otherwise. * * type == XENMEM_resource_ioreq_server -> id == ioreq server id + * type == XENMEM_resource_grant_table -> id defined below */ uint32_t id; + +#define XENMEM_resource_grant_table_id_grant 0 +#define XENMEM_resource_grant_table_id_status 1 + /* IN/OUT - As an IN parameter number of frames of the resource * to be mapped. However, if the specified value is 0 and * frame_list is NULL then this field will be set to the diff --git a/xen/include/xen/grant_table.h b/xen/include/xen/grant_table.h index b3a95fda58..e9125e43e7 100644 --- a/xen/include/xen/grant_table.h +++ b/xen/include/xen/grant_table.h @@ -55,6 +55,10 @@ int mem_sharing_gref_to_gfn(struct grant_table *gt, grant_ref_t ref, int gnttab_map_frame(struct domain *d, unsigned long idx, gfn_t gfn, mfn_t *mfn); +int gnttab_get_grant_frame(struct domain *d, unsigned long idx, + mfn_t *mfn); +int gnttab_get_status_frame(struct domain *d, unsigned long idx, + mfn_t *mfn); unsigned int gnttab_dom0_frames(void);