From patchwork Wed Dec 4 17:10:56 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Xia, Hongyan" X-Patchwork-Id: 11273323 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 9982E930 for ; Wed, 4 Dec 2019 17:12:37 +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 75F302081B for ; Wed, 4 Dec 2019 17:12:37 +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="nVIr6WGO" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 75F302081B 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 1icYBS-0003bW-2s; Wed, 04 Dec 2019 17:11:26 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1icYBQ-0003bI-QJ for xen-devel@lists.xenproject.org; Wed, 04 Dec 2019 17:11:24 +0000 X-Inumbo-ID: 1913caa2-16b9-11ea-8206-12813bfff9fa Received: from smtp-fw-33001.amazon.com (unknown [207.171.190.10]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 1913caa2-16b9-11ea-8206-12813bfff9fa; Wed, 04 Dec 2019 17:11:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1575479484; x=1607015484; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=VW6QgOxkq6GDS7hLpbBGpMJevnEntzbIRG3koLDnKSU=; b=nVIr6WGOikPjbFQzJJTfyLHzSncdqCn/+cMNaV2V8fgEOlUPfVvvqVDl nTgtN0zurNeJUsepw4orhhjIgCm9ieEkEpAUR0Ayg5KgO/SQysRQB/tTH utJjxZVPG4g2rlHr8zyi+0P71L/CiAJjN9TaCowdUpRuO1hG0Wmz1CpR6 8=; IronPort-SDR: muUr67cB6wjvY/y3PogbbsrLuikESY+xT4rvb6no6/uhnJRmbyVs/W/MlaJHA07gMstekLsJIR yfdeEPMuUCwQ== X-IronPort-AV: E=Sophos;i="5.69,277,1571702400"; d="scan'208";a="13005458" Received: from sea32-co-svc-lb4-vlan3.sea.corp.amazon.com (HELO email-inbound-relay-2b-c300ac87.us-west-2.amazon.com) ([10.47.23.38]) by smtp-border-fw-out-33001.sea14.amazon.com with ESMTP; 04 Dec 2019 17:11:10 +0000 Received: from EX13MTAUWA001.ant.amazon.com (pdx4-ws-svc-p6-lb7-vlan2.pdx.amazon.com [10.170.41.162]) by email-inbound-relay-2b-c300ac87.us-west-2.amazon.com (Postfix) with ESMTPS id 8F2D7A20CB; Wed, 4 Dec 2019 17:11:09 +0000 (UTC) Received: from EX13D12UWA003.ant.amazon.com (10.43.160.50) by EX13MTAUWA001.ant.amazon.com (10.43.160.58) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Wed, 4 Dec 2019 17:11:09 +0000 Received: from EX13MTAUWA001.ant.amazon.com (10.43.160.58) by EX13D12UWA003.ant.amazon.com (10.43.160.50) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Wed, 4 Dec 2019 17:11:08 +0000 Received: from u60d575063b1f53.ant.amazon.com (10.125.106.64) by mail-relay.amazon.com (10.43.160.118) with Microsoft SMTP Server id 15.0.1367.3 via Frontend Transport; Wed, 4 Dec 2019 17:11:08 +0000 From: Hongyan Xia To: Date: Wed, 4 Dec 2019 17:10:56 +0000 Message-ID: X-Mailer: git-send-email 2.17.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: Bulk Subject: [Xen-devel] [PATCH v4 4/9] x86/mm: introduce l{1, 2}t local variables to modify_xen_mappings 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 , Jan Beulich , =?utf-8?q?Roger_Pau_Monn=C3=A9?= Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" From: Wei Liu The pl2e and pl1e variables are heavily (ab)used in that function. It is fine at the moment because all page tables are always mapped so there is no need to track the life time of each variable. We will soon have the requirement to map and unmap page tables. We need to track the life time of each variable to avoid leakage. Introduce some l{1,2}t variables with limited scope so that we can track life time of pointers to xen page tables more easily. No functional change. Signed-off-by: Wei Liu Reviewed-by: Jan Beulich --- xen/arch/x86/mm.c | 68 ++++++++++++++++++++++++++--------------------- 1 file changed, 38 insertions(+), 30 deletions(-) diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index 790578d2b3..303bc35549 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -5601,6 +5601,8 @@ int modify_xen_mappings(unsigned long s, unsigned long e, unsigned int nf) if ( l3e_get_flags(*pl3e) & _PAGE_PSE ) { + l2_pgentry_t *l2t; + if ( l2_table_offset(v) == 0 && l1_table_offset(v) == 0 && ((e - v) >= (1UL << L3_PAGETABLE_SHIFT)) ) @@ -5616,11 +5618,11 @@ int modify_xen_mappings(unsigned long s, unsigned long e, unsigned int nf) } /* PAGE1GB: shatter the superpage and fall through. */ - pl2e = alloc_xen_pagetable(); - if ( !pl2e ) + l2t = alloc_xen_pagetable(); + if ( !l2t ) return -ENOMEM; for ( i = 0; i < L2_PAGETABLE_ENTRIES; i++ ) - l2e_write(pl2e + i, + l2e_write(l2t + i, l2e_from_pfn(l3e_get_pfn(*pl3e) + (i << PAGETABLE_ORDER), l3e_get_flags(*pl3e))); @@ -5629,14 +5631,14 @@ int modify_xen_mappings(unsigned long s, unsigned long e, unsigned int nf) if ( (l3e_get_flags(*pl3e) & _PAGE_PRESENT) && (l3e_get_flags(*pl3e) & _PAGE_PSE) ) { - l3e_write_atomic(pl3e, l3e_from_mfn(virt_to_mfn(pl2e), + l3e_write_atomic(pl3e, l3e_from_mfn(virt_to_mfn(l2t), __PAGE_HYPERVISOR)); - pl2e = NULL; + l2t = NULL; } if ( locking ) spin_unlock(&map_pgdir_lock); - if ( pl2e ) - free_xen_pagetable(pl2e); + if ( l2t ) + free_xen_pagetable(l2t); } /* @@ -5670,12 +5672,14 @@ int modify_xen_mappings(unsigned long s, unsigned long e, unsigned int nf) } else { + l1_pgentry_t *l1t; + /* PSE: shatter the superpage and try again. */ - pl1e = alloc_xen_pagetable(); - if ( !pl1e ) + l1t = alloc_xen_pagetable(); + if ( !l1t ) return -ENOMEM; for ( i = 0; i < L1_PAGETABLE_ENTRIES; i++ ) - l1e_write(&pl1e[i], + l1e_write(&l1t[i], l1e_from_pfn(l2e_get_pfn(*pl2e) + i, l2e_get_flags(*pl2e) & ~_PAGE_PSE)); if ( locking ) @@ -5683,19 +5687,19 @@ int modify_xen_mappings(unsigned long s, unsigned long e, unsigned int nf) if ( (l2e_get_flags(*pl2e) & _PAGE_PRESENT) && (l2e_get_flags(*pl2e) & _PAGE_PSE) ) { - l2e_write_atomic(pl2e, l2e_from_mfn(virt_to_mfn(pl1e), + l2e_write_atomic(pl2e, l2e_from_mfn(virt_to_mfn(l1t), __PAGE_HYPERVISOR)); - pl1e = NULL; + l1t = NULL; } if ( locking ) spin_unlock(&map_pgdir_lock); - if ( pl1e ) - free_xen_pagetable(pl1e); + if ( l1t ) + free_xen_pagetable(l1t); } } else { - l1_pgentry_t nl1e; + l1_pgentry_t nl1e, *l1t; /* * Ordinary 4kB mapping: The L2 entry has been verified to be @@ -5742,9 +5746,9 @@ int modify_xen_mappings(unsigned long s, unsigned long e, unsigned int nf) continue; } - pl1e = l2e_to_l1e(*pl2e); + l1t = l2e_to_l1e(*pl2e); for ( i = 0; i < L1_PAGETABLE_ENTRIES; i++ ) - if ( l1e_get_intpte(pl1e[i]) != 0 ) + if ( l1e_get_intpte(l1t[i]) != 0 ) break; if ( i == L1_PAGETABLE_ENTRIES ) { @@ -5753,7 +5757,7 @@ int modify_xen_mappings(unsigned long s, unsigned long e, unsigned int nf) if ( locking ) spin_unlock(&map_pgdir_lock); flush_area(NULL, FLUSH_TLB_GLOBAL); /* flush before free */ - free_xen_pagetable(pl1e); + free_xen_pagetable(l1t); } else if ( locking ) spin_unlock(&map_pgdir_lock); @@ -5782,21 +5786,25 @@ int modify_xen_mappings(unsigned long s, unsigned long e, unsigned int nf) continue; } - pl2e = l3e_to_l2e(*pl3e); - for ( i = 0; i < L2_PAGETABLE_ENTRIES; i++ ) - if ( l2e_get_intpte(pl2e[i]) != 0 ) - break; - if ( i == L2_PAGETABLE_ENTRIES ) { - /* Empty: zap the L3E and free the L2 page. */ - l3e_write_atomic(pl3e, l3e_empty()); - if ( locking ) + l2_pgentry_t *l2t; + + l2t = l3e_to_l2e(*pl3e); + for ( i = 0; i < L2_PAGETABLE_ENTRIES; i++ ) + if ( l2e_get_intpte(l2t[i]) != 0 ) + break; + if ( i == L2_PAGETABLE_ENTRIES ) + { + /* Empty: zap the L3E and free the L2 page. */ + l3e_write_atomic(pl3e, l3e_empty()); + if ( locking ) + spin_unlock(&map_pgdir_lock); + flush_area(NULL, FLUSH_TLB_GLOBAL); /* flush before free */ + free_xen_pagetable(l2t); + } + else if ( locking ) spin_unlock(&map_pgdir_lock); - flush_area(NULL, FLUSH_TLB_GLOBAL); /* flush before free */ - free_xen_pagetable(pl2e); } - else if ( locking ) - spin_unlock(&map_pgdir_lock); } flush_area(NULL, FLUSH_TLB_GLOBAL);