From patchwork Thu Sep 26 09:45:36 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Xia, Hongyan" X-Patchwork-Id: 11162101 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id BAD4013B1 for ; Thu, 26 Sep 2019 09:49:56 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 94C25222C0 for ; Thu, 26 Sep 2019 09:49:56 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="K3ImUR0U" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 94C25222C0 Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amazon.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iDQOb-0005f0-RO; Thu, 26 Sep 2019 09:49:09 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iDQOa-0005be-3Z for xen-devel@lists.xenproject.org; Thu, 26 Sep 2019 09:49:08 +0000 X-Inumbo-ID: dd5dbc9a-e042-11e9-bf31-bc764e2007e4 Received: from smtp-fw-33001.amazon.com (unknown [207.171.190.10]) by localhost (Halon) with ESMTPS id dd5dbc9a-e042-11e9-bf31-bc764e2007e4; Thu, 26 Sep 2019 09:49:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1569491341; x=1601027341; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=Py9hgP70m9HBUfBEhr08s8H5V3T2TLC0UE4W9w9lg6s=; b=K3ImUR0Ulonl23S/SMPdSoSQE12JqVyCNx9dLcC+GPwVOgWjaesBYeo+ 2xrJRwMUUeUvJrFOIMLuBpLNwWWPSPxVbvsJf5phf2zM0pn+fuk9IFR7j vKan9jCkrxBltvLH+2pzlC8GTmf6MCL/Zf87EcYu89AHSmMpnHPIrLsye E=; X-IronPort-AV: E=Sophos;i="5.64,551,1559520000"; d="scan'208";a="836988740" Received: from sea3-co-svc-lb6-vlan2.sea.amazon.com (HELO email-inbound-relay-1e-17c49630.us-east-1.amazon.com) ([10.47.22.34]) by smtp-border-fw-out-33001.sea14.amazon.com with ESMTP; 26 Sep 2019 09:48:02 +0000 Received: from EX13MTAUWA001.ant.amazon.com (iad55-ws-svc-p15-lb9-vlan3.iad.amazon.com [10.40.159.166]) by email-inbound-relay-1e-17c49630.us-east-1.amazon.com (Postfix) with ESMTPS id 011A0A2A48; Thu, 26 Sep 2019 09:47:49 +0000 (UTC) Received: from EX13D23UWA001.ant.amazon.com (10.43.160.68) by EX13MTAUWA001.ant.amazon.com (10.43.160.118) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Thu, 26 Sep 2019 09:47:18 +0000 Received: from EX13MTAUWA001.ant.amazon.com (10.43.160.58) by EX13D23UWA001.ant.amazon.com (10.43.160.68) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Thu, 26 Sep 2019 09:47:17 +0000 Received: from u9d785c4ba99158.ant.amazon.com (10.125.106.58) by mail-relay.amazon.com (10.43.160.118) with Microsoft SMTP Server id 15.0.1367.3 via Frontend Transport; Thu, 26 Sep 2019 09:47:16 +0000 From: To: Date: Thu, 26 Sep 2019 10:45:36 +0100 Message-ID: <0f7cf39cee6b4a2ca4ddec25d264a5e55313886b.1569489002.git.hongyax@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: Bulk Subject: [Xen-devel] [RFC PATCH 13/84] x86/mm: rewrite virt_to_xen_l3e X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Andrew Cooper , Wei Liu , Wei Liu , Jan Beulich , =?utf-8?q?Roger_Pau_?= =?utf-8?q?Monn=C3=A9?= Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" From: Wei Liu Rewrite that function to use the new APIs. Modify its callers to unmap the pointer returned. Signed-off-by: Wei Liu --- xen/arch/x86/mm.c | 61 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 48 insertions(+), 13 deletions(-) diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index f8a8f97f81..1dcd4289d1 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -4896,45 +4896,70 @@ void free_xen_pagetable_new(mfn_t mfn) static DEFINE_SPINLOCK(map_pgdir_lock); +/* + * Given a virtual address, return a pointer to xen's L3 entry. Caller + * needs to unmap the pointer. + */ static l3_pgentry_t *virt_to_xen_l3e(unsigned long v) { l4_pgentry_t *pl4e; + l3_pgentry_t *pl3e = NULL; pl4e = &idle_pg_table[l4_table_offset(v)]; if ( !(l4e_get_flags(*pl4e) & _PAGE_PRESENT) ) { bool locking = system_state > SYS_STATE_boot; - l3_pgentry_t *l3t = alloc_xen_pagetable(); + l3_pgentry_t *l3t; + mfn_t mfn; + + mfn = alloc_xen_pagetable_new(); + if ( mfn_eq(mfn, INVALID_MFN) ) + goto out; + + l3t = map_xen_pagetable_new(mfn); - if ( !l3t ) - return NULL; if ( locking ) spin_lock(&map_pgdir_lock); if ( !(l4e_get_flags(*pl4e) & _PAGE_PRESENT) ) { - l4_pgentry_t l4e = l4e_from_paddr(__pa(l3t), __PAGE_HYPERVISOR); + l4_pgentry_t l4e = l4e_from_mfn(mfn, __PAGE_HYPERVISOR); clear_page(l3t); l4e_write(pl4e, l4e); efi_update_l4_pgtable(l4_table_offset(v), l4e); + pl3e = l3t + l3_table_offset(v); l3t = NULL; } if ( locking ) spin_unlock(&map_pgdir_lock); if ( l3t ) - free_xen_pagetable(l3t); + { + ASSERT(!pl3e); + ASSERT(!mfn_eq(mfn, INVALID_MFN)); + UNMAP_XEN_PAGETABLE_NEW(l3t); + free_xen_pagetable_new(mfn); + } + } + + if ( !pl3e ) + { + ASSERT(l4e_get_flags(*pl4e) & _PAGE_PRESENT); + pl3e = (l3_pgentry_t *)map_xen_pagetable_new(l4e_get_mfn(*pl4e)) + + l3_table_offset(v); } - return l4e_to_l3e(*pl4e) + l3_table_offset(v); + out: + return pl3e; } static l2_pgentry_t *virt_to_xen_l2e(unsigned long v) { l3_pgentry_t *pl3e; + l2_pgentry_t *pl2e = NULL; pl3e = virt_to_xen_l3e(v); if ( !pl3e ) - return NULL; + goto out; if ( !(l3e_get_flags(*pl3e) & _PAGE_PRESENT) ) { @@ -4942,7 +4967,8 @@ static l2_pgentry_t *virt_to_xen_l2e(unsigned long v) l2_pgentry_t *l2t = alloc_xen_pagetable(); if ( !l2t ) - return NULL; + goto out; + if ( locking ) spin_lock(&map_pgdir_lock); if ( !(l3e_get_flags(*pl3e) & _PAGE_PRESENT) ) @@ -4958,7 +4984,11 @@ static l2_pgentry_t *virt_to_xen_l2e(unsigned long v) } BUG_ON(l3e_get_flags(*pl3e) & _PAGE_PSE); - return l3e_to_l2e(*pl3e) + l2_table_offset(v); + pl2e = l3e_to_l2e(*pl3e) + l2_table_offset(v); + + out: + UNMAP_XEN_PAGETABLE_NEW(pl3e); + return pl2e; } l1_pgentry_t *virt_to_xen_l1e(unsigned long v) @@ -5014,7 +5044,7 @@ int map_pages_to_xen( unsigned int flags) { bool locking = system_state > SYS_STATE_boot; - l3_pgentry_t *pl3e, ol3e; + l3_pgentry_t *pl3e = NULL, ol3e; l2_pgentry_t *pl2e, ol2e; l1_pgentry_t *pl1e, ol1e; unsigned int i; @@ -5390,7 +5420,8 @@ int map_pages_to_xen( else if ( locking ) spin_unlock(&map_pgdir_lock); } - end_of_loop:; + end_of_loop: + UNMAP_XEN_PAGETABLE_NEW(pl3e); } #undef flush_flags @@ -5398,6 +5429,7 @@ int map_pages_to_xen( rc = 0; out: + UNMAP_XEN_PAGETABLE_NEW(pl3e); return rc; } @@ -5421,6 +5453,7 @@ int populate_pt_range(unsigned long virt, unsigned long nr_mfns) int modify_xen_mappings(unsigned long s, unsigned long e, unsigned int nf) { bool locking = system_state > SYS_STATE_boot; + l3_pgentry_t *pl3e = NULL; l2_pgentry_t *pl2e; l1_pgentry_t *pl1e; unsigned int i; @@ -5436,7 +5469,7 @@ int modify_xen_mappings(unsigned long s, unsigned long e, unsigned int nf) while ( v < e ) { - l3_pgentry_t *pl3e = virt_to_xen_l3e(v); + pl3e = virt_to_xen_l3e(v); if ( !pl3e || !(l3e_get_flags(*pl3e) & _PAGE_PRESENT) ) { @@ -5662,7 +5695,8 @@ int modify_xen_mappings(unsigned long s, unsigned long e, unsigned int nf) else if ( locking ) spin_unlock(&map_pgdir_lock); } - end_of_loop:; + end_of_loop: + UNMAP_XEN_PAGETABLE_NEW(pl3e); } flush_area(NULL, FLUSH_TLB_GLOBAL); @@ -5671,6 +5705,7 @@ int modify_xen_mappings(unsigned long s, unsigned long e, unsigned int nf) rc = 0; out: + UNMAP_XEN_PAGETABLE_NEW(pl3e); return rc; }