From patchwork Tue Aug 22 14:50:55 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 9915451 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 EBFD1608A6 for ; Tue, 22 Aug 2017 14:54:17 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C0AE6287A5 for ; Tue, 22 Aug 2017 14:54:17 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B17542889B; Tue, 22 Aug 2017 14:54:17 +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 AFBF9287A5 for ; Tue, 22 Aug 2017 14:54:15 +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 1dkAX2-0002Ps-3d; Tue, 22 Aug 2017 14:51:52 +0000 Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dkAX1-0002OY-30 for xen-devel@lists.xenproject.org; Tue, 22 Aug 2017 14:51:51 +0000 Received: from [193.109.254.147] by server-5.bemta-6.messagelabs.com id CB/C7-03368-6054C995; Tue, 22 Aug 2017 14:51:50 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFmpmkeJIrShJLcpLzFFi42JxWrohUpfVdU6 kwfQd1hbft0xmcmD0OPzhCksAYxRrZl5SfkUCa8bmF19ZC7q1Kraf62JvYHwl18XIySEh4C+x 4OAPZhCbTUBHYurTS6xdjBwcIgIqErf3GnQxcnEwC5xilJj15zYbSFxYwFni4QlDkHIWAVWJd Vt+s4DYvALWEgdmXWGEGCkvsavtIiuIzSlgI/Hw0AQWkFYhoJpb1wNBwkJA09dPncUG0SoocX LmE7AxzAISEgdfvGCewMg7C0lqFpLUAkamVYwaxalFZalFukaWeklFmekZJbmJmTm6hgZmerm pxcWJ6ak5iUnFesn5uZsYgYHDAAQ7GA8sCjzEKMnBpCTKO/n77EghvqT8lMqMxOKM+KLSnNTi Q4wyHBxKErzSznMihQSLUtNTK9Iyc4AhDJOW4OBREuENBUnzFhck5hZnpkOkTjEqSonzvgFJC IAkMkrz4NpgcXOJUVZKmJcR6BAhnoLUotzMElT5V4ziHIxKwrwFIFN4MvNK4Ka/AlrMBLTYsH UayOKSRISUVAOjRNXHptYnlmJMKaHRs5I0bt5RlPzid92pT405csH11COs5ubXV/x4YBy4VSE l0SDQ8Ryrju7h+avr/K/5fL4YMv/Xm0fqyfdnxSuceaw371iiq8WNH7VuXF01u1P25xRa/nib blOYF1D1dKLV3oTKSXfmdcjPk3HkPGclYNh+kNX3+4ZNcdxKLMUZiYZazEXFiQCXtOGSlgIAA A== X-Env-Sender: prvs=40066d99f=Paul.Durrant@citrix.com X-Msg-Ref: server-10.tower-27.messagelabs.com!1503413508!89722321!1 X-Originating-IP: [66.165.176.89] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogNjYuMTY1LjE3Ni44OSA9PiAyMDMwMDc=\n, received_headers: No Received headers X-StarScan-Received: X-StarScan-Version: 9.4.45; banners=-,-,- X-VirusChecked: Checked Received: (qmail 14845 invoked from network); 22 Aug 2017 14:51:49 -0000 Received: from smtp.citrix.com (HELO SMTP.CITRIX.COM) (66.165.176.89) by server-10.tower-27.messagelabs.com with RC4-SHA encrypted SMTP; 22 Aug 2017 14:51:49 -0000 X-IronPort-AV: E=Sophos;i="5.41,412,1498521600"; d="scan'208";a="436412958" From: Paul Durrant To: Date: Tue, 22 Aug 2017 15:50:55 +0100 Message-ID: <20170822145107.6877-2-paul.durrant@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170822145107.6877-1-paul.durrant@citrix.com> References: <20170822145107.6877-1-paul.durrant@citrix.com> MIME-Version: 1.0 Cc: Andrew Cooper , Julien Grall , Paul Durrant , Stefano Stabellini , Jan Beulich Subject: [Xen-devel] [PATCH v2 REPOST 01/12] [x86|arm]: remove code duplication 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 There is a substantial amount of code duplicated between the x86 and arm implementations of mm.c:xenmem_add_to_physmap_one() for XENMAPSPACE_grant_table. Also, the code in question looks like it really should be in common/grant_table.c This patch introduces a new function in common/grant_table.c to get the mfn of a specified frame in the grant table of a specified guest, and calls to that from the arch-specific code in mm.c. Signed-off-by: Paul Durrant --- Cc: Stefano Stabellini Cc: Julien Grall Cc: Jan Beulich Cc: Andrew Cooper --- xen/arch/arm/mm.c | 29 ++++------------------------- xen/arch/x86/mm.c | 26 +++----------------------- xen/common/grant_table.c | 33 +++++++++++++++++++++++++++++++++ xen/include/xen/grant_table.h | 3 +++ 4 files changed, 43 insertions(+), 48 deletions(-) diff --git a/xen/arch/arm/mm.c b/xen/arch/arm/mm.c index a810a056d7..5ae9607821 100644 --- a/xen/arch/arm/mm.c +++ b/xen/arch/arm/mm.c @@ -1229,32 +1229,11 @@ int xenmem_add_to_physmap_one( switch ( space ) { case XENMAPSPACE_grant_table: - grant_write_lock(d->grant_table); - - if ( d->grant_table->gt_version == 0 ) - d->grant_table->gt_version = 1; - - if ( d->grant_table->gt_version == 2 && - (idx & XENMAPIDX_grant_table_status) ) - { - idx &= ~XENMAPIDX_grant_table_status; - if ( idx < nr_status_frames(d->grant_table) ) - mfn = virt_to_mfn(d->grant_table->status[idx]); - else - return -EINVAL; - } - else - { - if ( (idx >= nr_grant_frames(d->grant_table)) && - (idx < max_grant_frames) ) - gnttab_grow_table(d, idx + 1); - - if ( idx < nr_grant_frames(d->grant_table) ) - mfn = virt_to_mfn(d->grant_table->shared_raw[idx]); - else - return -EINVAL; - } + mfn = gnttab_get_frame(d, idx); + if ( mfn_eq(mfn, INVALID_MFN) ) + return -EINVAL; + grant_write_lock(d->grant_table); d->arch.grant_table_gfn[idx] = gfn; t = p2m_ram_rw; diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index 5b863c6fa6..0abb1e284f 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -4639,29 +4639,9 @@ int xenmem_add_to_physmap_one( mfn = virt_to_mfn(d->shared_info); break; case XENMAPSPACE_grant_table: - grant_write_lock(d->grant_table); - - if ( d->grant_table->gt_version == 0 ) - d->grant_table->gt_version = 1; - - if ( d->grant_table->gt_version == 2 && - (idx & XENMAPIDX_grant_table_status) ) - { - idx &= ~XENMAPIDX_grant_table_status; - if ( idx < nr_status_frames(d->grant_table) ) - mfn = virt_to_mfn(d->grant_table->status[idx]); - } - else - { - if ( (idx >= nr_grant_frames(d->grant_table)) && - (idx < max_grant_frames) ) - gnttab_grow_table(d, idx + 1); - - if ( idx < nr_grant_frames(d->grant_table) ) - mfn = virt_to_mfn(d->grant_table->shared_raw[idx]); - } - - grant_write_unlock(d->grant_table); + mfn = mfn_x(gnttab_get_frame(d, idx)); + if ( mfn_eq(_mfn(mfn), INVALID_MFN) ) + return -EINVAL; break; case XENMAPSPACE_gmfn_range: case XENMAPSPACE_gmfn: diff --git a/xen/common/grant_table.c b/xen/common/grant_table.c index 0f9dd1e706..b327458301 100644 --- a/xen/common/grant_table.c +++ b/xen/common/grant_table.c @@ -1621,6 +1621,39 @@ active_alloc_failed: return 0; } +mfn_t +gnttab_get_frame(struct domain *d, unsigned int idx) +{ + struct grant_table *gt = d->grant_table; + mfn_t mfn = INVALID_MFN; + + grant_write_lock(gt); + + if ( gt->gt_version == 0 ) + gt->gt_version = 1; + + if ( gt->gt_version == 2 && + (idx & XENMAPIDX_grant_table_status) ) + { + idx &= ~XENMAPIDX_grant_table_status; + if ( idx < nr_status_frames(gt) ) + mfn = _mfn(virt_to_mfn(gt->status[idx])); + } + else + { + if ( (idx >= nr_grant_frames(gt)) && + (idx < max_grant_frames) ) + gnttab_grow_table(d, idx + 1); + + if ( idx < nr_grant_frames(gt) ) + mfn = _mfn(virt_to_mfn(gt->shared_raw[idx])); + } + + grant_write_unlock(gt); + + return mfn; +} + static long gnttab_setup_table( XEN_GUEST_HANDLE_PARAM(gnttab_setup_table_t) uop, unsigned int count) diff --git a/xen/include/xen/grant_table.h b/xen/include/xen/grant_table.h index b5af21b53c..079cf82a1e 100644 --- a/xen/include/xen/grant_table.h +++ b/xen/include/xen/grant_table.h @@ -129,6 +129,9 @@ gnttab_release_mappings( int gnttab_grow_table(struct domain *d, unsigned int req_nr_frames); +/* Get mfn of grant frame */ +mfn_t gnttab_get_frame(struct domain *d, unsigned int idx); + /* Number of grant table frames. Caller must hold d's grant table lock. */ static inline unsigned int nr_grant_frames(struct grant_table *gt) {