From patchwork Wed Dec 4 17:10:55 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Xia, Hongyan" X-Patchwork-Id: 11273327 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 3103513B6 for ; Wed, 4 Dec 2019 17:12:50 +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 027542081B for ; Wed, 4 Dec 2019 17:12:50 +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="CkvG5aPb" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 027542081B 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 1icYBX-0003e8-Np; Wed, 04 Dec 2019 17:11:31 +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 1icYBV-0003dJ-R2 for xen-devel@lists.xenproject.org; Wed, 04 Dec 2019 17:11:29 +0000 X-Inumbo-ID: 1913caa3-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 1913caa3-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=1575479485; x=1607015485; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=JfBjHGTtieYsBSyBebnsZQmdwI9lEcNcATx/cmQduiE=; b=CkvG5aPb27AAAmNHMKkD5KUOEkxnutOAYhqaJai02U2fO/hKKEeqriSq xGB4Kwf8dvkIxNgw7oGUDsRXLnhfHQXRgfrnpjKguayiCjpTXtIjthAeG JCrTJJzUNwTjgf8HvNTRzbbdqYmq9A7U6EmLU9UZNSan8tf8aWv+Uun9y Y=; IronPort-SDR: f1wTML/VG1MXMXnDBRHhbJm+OBe3hZFxnz1Nn9Jwp44vWbRtwAgr7XS1013iMGk9jOjUI6Rqbq QI2xLj6ztajQ== X-IronPort-AV: E=Sophos;i="5.69,277,1571702400"; d="scan'208";a="13005452" Received: from sea32-co-svc-lb4-vlan3.sea.corp.amazon.com (HELO email-inbound-relay-2c-87a10be6.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:08 +0000 Received: from EX13MTAUWA001.ant.amazon.com (pdx4-ws-svc-p6-lb7-vlan2.pdx.amazon.com [10.170.41.162]) by email-inbound-relay-2c-87a10be6.us-west-2.amazon.com (Postfix) with ESMTPS id 26C70A1C82; Wed, 4 Dec 2019 17:11:08 +0000 (UTC) Received: from EX13D19UWA001.ant.amazon.com (10.43.160.169) by EX13MTAUWA001.ant.amazon.com (10.43.160.118) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Wed, 4 Dec 2019 17:11:08 +0000 Received: from EX13MTAUWA001.ant.amazon.com (10.43.160.58) by EX13D19UWA001.ant.amazon.com (10.43.160.169) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Wed, 4 Dec 2019 17:11:07 +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:06 +0000 From: Hongyan Xia To: Date: Wed, 4 Dec 2019 17:10:55 +0000 Message-ID: <032b7288fc1606a68dc18cf5709c83540717f38a.1575477921.git.hongyxia@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: Bulk Subject: [Xen-devel] [PATCH v4 3/9] x86/mm: introduce l{1, 2}t local variables to map_pages_to_xen 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 | 75 ++++++++++++++++++++++++++--------------------- 1 file changed, 42 insertions(+), 33 deletions(-) diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index ca362ad638..790578d2b3 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -5234,10 +5234,12 @@ int map_pages_to_xen( } else { - pl2e = l3e_to_l2e(ol3e); + l2_pgentry_t *l2t; + + l2t = l3e_to_l2e(ol3e); for ( i = 0; i < L2_PAGETABLE_ENTRIES; i++ ) { - ol2e = pl2e[i]; + ol2e = l2t[i]; if ( !(l2e_get_flags(ol2e) & _PAGE_PRESENT) ) continue; if ( l2e_get_flags(ol2e) & _PAGE_PSE ) @@ -5245,21 +5247,22 @@ int map_pages_to_xen( else { unsigned int j; + l1_pgentry_t *l1t; - pl1e = l2e_to_l1e(ol2e); + l1t = l2e_to_l1e(ol2e); for ( j = 0; j < L1_PAGETABLE_ENTRIES; j++ ) - flush_flags(l1e_get_flags(pl1e[j])); + flush_flags(l1e_get_flags(l1t[j])); } } flush_area(virt, flush_flags); for ( i = 0; i < L2_PAGETABLE_ENTRIES; i++ ) { - ol2e = pl2e[i]; + ol2e = l2t[i]; if ( (l2e_get_flags(ol2e) & _PAGE_PRESENT) && !(l2e_get_flags(ol2e) & _PAGE_PSE) ) free_xen_pagetable(l2e_to_l1e(ol2e)); } - free_xen_pagetable(pl2e); + free_xen_pagetable(l2t); } } @@ -5275,6 +5278,7 @@ int map_pages_to_xen( { unsigned int flush_flags = FLUSH_TLB | FLUSH_ORDER(2 * PAGETABLE_ORDER); + l2_pgentry_t *l2t; /* Skip this PTE if there is no change. */ if ( ((l3e_get_pfn(ol3e) & ~(L2_PAGETABLE_ENTRIES * @@ -5296,12 +5300,12 @@ int map_pages_to_xen( continue; } - pl2e = alloc_xen_pagetable(); - if ( pl2e == NULL ) + l2t = alloc_xen_pagetable(); + if ( l2t == NULL ) return -ENOMEM; for ( i = 0; i < L2_PAGETABLE_ENTRIES; i++ ) - l2e_write(pl2e + i, + l2e_write(l2t + i, l2e_from_pfn(l3e_get_pfn(ol3e) + (i << PAGETABLE_ORDER), l3e_get_flags(ol3e))); @@ -5314,15 +5318,15 @@ int map_pages_to_xen( 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); flush_area(virt, flush_flags); - if ( pl2e ) - free_xen_pagetable(pl2e); + if ( l2t ) + free_xen_pagetable(l2t); } pl2e = virt_to_xen_l2e(virt); @@ -5350,11 +5354,13 @@ int map_pages_to_xen( } else { - pl1e = l2e_to_l1e(ol2e); + l1_pgentry_t *l1t; + + l1t = l2e_to_l1e(ol2e); for ( i = 0; i < L1_PAGETABLE_ENTRIES; i++ ) - flush_flags(l1e_get_flags(pl1e[i])); + flush_flags(l1e_get_flags(l1t[i])); flush_area(virt, flush_flags); - free_xen_pagetable(pl1e); + free_xen_pagetable(l1t); } } @@ -5376,6 +5382,7 @@ int map_pages_to_xen( { unsigned int flush_flags = FLUSH_TLB | FLUSH_ORDER(PAGETABLE_ORDER); + l1_pgentry_t *l1t; /* Skip this PTE if there is no change. */ if ( (((l2e_get_pfn(*pl2e) & ~(L1_PAGETABLE_ENTRIES - 1)) + @@ -5395,12 +5402,12 @@ int map_pages_to_xen( goto check_l3; } - pl1e = alloc_xen_pagetable(); - if ( pl1e == NULL ) + l1t = alloc_xen_pagetable(); + if ( l1t == NULL ) 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, lNf_to_l1f(l2e_get_flags(*pl2e)))); @@ -5412,15 +5419,15 @@ int map_pages_to_xen( 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); flush_area(virt, flush_flags); - if ( pl1e ) - free_xen_pagetable(pl1e); + if ( l1t ) + free_xen_pagetable(l1t); } pl1e = l2e_to_l1e(*pl2e) + l1_table_offset(virt); @@ -5445,6 +5452,7 @@ int map_pages_to_xen( ((1u << PAGETABLE_ORDER) - 1)) == 0)) ) { unsigned long base_mfn; + l1_pgentry_t *l1t; if ( locking ) spin_lock(&map_pgdir_lock); @@ -5468,11 +5476,11 @@ int map_pages_to_xen( goto check_l3; } - pl1e = l2e_to_l1e(ol2e); - base_mfn = l1e_get_pfn(*pl1e) & ~(L1_PAGETABLE_ENTRIES - 1); - for ( i = 0; i < L1_PAGETABLE_ENTRIES; i++, pl1e++ ) - if ( (l1e_get_pfn(*pl1e) != (base_mfn + i)) || - (l1e_get_flags(*pl1e) != flags) ) + l1t = l2e_to_l1e(ol2e); + base_mfn = l1e_get_pfn(l1t[0]) & ~(L1_PAGETABLE_ENTRIES - 1); + for ( i = 0; i < L1_PAGETABLE_ENTRIES; i++ ) + if ( (l1e_get_pfn(l1t[i]) != (base_mfn + i)) || + (l1e_get_flags(l1t[i]) != flags) ) break; if ( i == L1_PAGETABLE_ENTRIES ) { @@ -5498,6 +5506,7 @@ int map_pages_to_xen( ((1UL << (L3_PAGETABLE_SHIFT - PAGE_SHIFT)) - 1))) ) { unsigned long base_mfn; + l2_pgentry_t *l2t; if ( locking ) spin_lock(&map_pgdir_lock); @@ -5515,13 +5524,13 @@ int map_pages_to_xen( continue; } - pl2e = l3e_to_l2e(ol3e); - base_mfn = l2e_get_pfn(*pl2e) & ~(L2_PAGETABLE_ENTRIES * + l2t = l3e_to_l2e(ol3e); + base_mfn = l2e_get_pfn(l2t[0]) & ~(L2_PAGETABLE_ENTRIES * L1_PAGETABLE_ENTRIES - 1); - for ( i = 0; i < L2_PAGETABLE_ENTRIES; i++, pl2e++ ) - if ( (l2e_get_pfn(*pl2e) != + for ( i = 0; i < L2_PAGETABLE_ENTRIES; i++ ) + if ( (l2e_get_pfn(l2t[i]) != (base_mfn + (i << PAGETABLE_ORDER))) || - (l2e_get_flags(*pl2e) != l1f_to_lNf(flags)) ) + (l2e_get_flags(l2t[i]) != l1f_to_lNf(flags)) ) break; if ( i == L2_PAGETABLE_ENTRIES ) {