From patchwork Tue Nov 14 06:53:47 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yu Zhang X-Patchwork-Id: 10057083 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 10E10601D3 for ; Tue, 14 Nov 2017 07:21:38 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CA50328FBD for ; Tue, 14 Nov 2017 07:21:37 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BF1D828FD6; Tue, 14 Nov 2017 07:21:37 +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 5F63C28FBD for ; Tue, 14 Nov 2017 07:21:37 +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 1eEVUq-0008Tp-8c; Tue, 14 Nov 2017 07:19:00 +0000 Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eEVUp-0008Td-2k for xen-devel@lists.xen.org; Tue, 14 Nov 2017 07:18:59 +0000 Received: from [85.158.139.211] by server-7.bemta-5.messagelabs.com id 02/2F-22646-2E89A0A5; Tue, 14 Nov 2017 07:18:58 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFvrHLMWRWlGSWpSXmKPExsXS1taRovtwBle UQe8iA4slHxezODB6HN39mymAMYo1My8pvyKBNePfbuWCSwoVO5++YG5g/CfaxcjFISQwnVHi 9ozpLF2MnBwSArwSR5bNYIWw/SU+X1vBCFHUzigx7+x8JpAEm4C2xI/VvxlBbBEBaYlrny+DF TELTGCUWLh0JRtIQlggUeLvliVAUzk4WARUJY6vMAUJ8wp4Sny7vZIJYoGcxMljk1lBSjgFvC Qeb9QDCQsBlczb9pJlAiPvAkaGVYzqxalFZalFuuZ6SUWZ6RkluYmZObqGBqZ6uanFxYnpqTm JScV6yfm5mxiBocAABDsYj012PsQoycGkJMqr8pk9SogvKT+lMiOxOCO+qDQntfgQowwHh5IE b+t0righwaLU9NSKtMwcYFDCpCU4eJREeC1A0rzFBYm5xZnpEKlTjMYcz2a+bmDmmHa1tYlZi CUvPy9VSpw3DaRUAKQ0ozQPbhAsWi4xykoJ8zICnSbEU5BalJtZgir/ilGcg1FJmPciyBSezL wSuH2vgE5hAjpFCuQL3uKSRISUVAPjYr415cf5szdYda1698Jy0pFNkq23F+9JU7R713/gwqG aL5l61RW+zaXnet2+bpykmLWmq/XgT1G1zyuqrqpeONNuV5k+ffOuHpEN6v95I7esF5g/NfLi uvoI9wIN/sNVaeY2paFHd7XtDmWzeft2b1xHpcPZD/1b1wed+i6x/tM248dmvB/nKLEUZyQaa jEXFScCAKyfb2qRAgAA X-Env-Sender: yu.c.zhang@linux.intel.com X-Msg-Ref: server-4.tower-206.messagelabs.com!1510643935!107031594!1 X-Originating-IP: [134.134.136.100] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 9.4.45; banners=-,-,- X-VirusChecked: Checked Received: (qmail 30238 invoked from network); 14 Nov 2017 07:18:57 -0000 Received: from mga07.intel.com (HELO mga07.intel.com) (134.134.136.100) by server-4.tower-206.messagelabs.com with DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 14 Nov 2017 07:18:57 -0000 Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga105.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 13 Nov 2017 23:18:54 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.44,393,1505804400"; d="scan'208";a="175523451" Received: from zhangyu-optiplex-9020.bj.intel.com ([10.238.135.159]) by fmsmga005.fm.intel.com with ESMTP; 13 Nov 2017 23:18:34 -0800 From: Yu Zhang To: xen-devel@lists.xen.org Date: Tue, 14 Nov 2017 14:53:47 +0800 Message-Id: <1510642427-3629-2-git-send-email-yu.c.zhang@linux.intel.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1510642427-3629-1-git-send-email-yu.c.zhang@linux.intel.com> References: <1510642427-3629-1-git-send-email-yu.c.zhang@linux.intel.com> Cc: Andrew Cooper , julien.grall@arm.com, min.he@intel.com, Jan Beulich , yi.z.zhang@intel.com Subject: [Xen-devel] [PATCH v3 for-4.10 2/2] x86/mm: fix a potential race condition in modify_xen_mappings(). 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: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP In modify_xen_mappings(), a L1/L2 page table shall be freed, if all entries of this page table are empty. Corresponding L2/L3 PTE will need be cleared in such scenario. However, concurrent paging structure modifications on different CPUs may cause the L2/L3 PTEs to be already be cleared or set to reference a superpage. Therefore the logic to enumerate the L1/L2 page table and to reset the corresponding L2/L3 PTE need to be protected with spinlock. And the _PAGE_PRESENT and _PAGE_PSE flags need be checked after the lock is obtained. Signed-off-by: Yu Zhang Reviewed-by: Jan Beulich --- Cc: Jan Beulich Cc: Andrew Cooper Changes in v3: According to comments from Jan Beulich: - indent the label by one space; - also check the _PAGE_PSE for L2E/L3E. Others: - commit message changes. --- xen/arch/x86/mm.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index 1697be9..64ccd70 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -5111,6 +5111,27 @@ int modify_xen_mappings(unsigned long s, unsigned long e, unsigned int nf) */ if ( (nf & _PAGE_PRESENT) || ((v != e) && (l1_table_offset(v) != 0)) ) continue; + if ( locking ) + spin_lock(&map_pgdir_lock); + + /* + * L2E may be already cleared, or set to a superpage, by + * concurrent paging structure modifications on other CPUs. + */ + if ( !(l2e_get_flags(*pl2e) & _PAGE_PRESENT) ) + { + if ( locking ) + spin_unlock(&map_pgdir_lock); + goto check_l3; + } + + if ( l2e_get_flags(*pl2e) & _PAGE_PSE ) + { + if ( locking ) + spin_unlock(&map_pgdir_lock); + continue; + } + pl1e = l2e_to_l1e(*pl2e); for ( i = 0; i < L1_PAGETABLE_ENTRIES; i++ ) if ( l1e_get_intpte(pl1e[i]) != 0 ) @@ -5119,11 +5140,16 @@ int modify_xen_mappings(unsigned long s, unsigned long e, unsigned int nf) { /* Empty: zap the L2E and free the L1 page. */ l2e_write_atomic(pl2e, l2e_empty()); + if ( locking ) + spin_unlock(&map_pgdir_lock); flush_area(NULL, FLUSH_TLB_GLOBAL); /* flush before free */ free_xen_pagetable(pl1e); } + else if ( locking ) + spin_unlock(&map_pgdir_lock); } + check_l3: /* * If we are not destroying mappings, or not done with the L3E, * skip the empty&free check. @@ -5131,6 +5157,21 @@ int modify_xen_mappings(unsigned long s, unsigned long e, unsigned int nf) if ( (nf & _PAGE_PRESENT) || ((v != e) && (l2_table_offset(v) + l1_table_offset(v) != 0)) ) continue; + if ( locking ) + spin_lock(&map_pgdir_lock); + + /* + * L3E may be already cleared, or set to a superpage, by + * concurrent paging structure modifications on other CPUs. + */ + if ( !(l3e_get_flags(*pl3e) & _PAGE_PRESENT) || + (l3e_get_flags(*pl3e) & _PAGE_PSE) ) + { + if ( locking ) + spin_unlock(&map_pgdir_lock); + continue; + } + pl2e = l3e_to_l2e(*pl3e); for ( i = 0; i < L2_PAGETABLE_ENTRIES; i++ ) if ( l2e_get_intpte(pl2e[i]) != 0 ) @@ -5139,9 +5180,13 @@ int modify_xen_mappings(unsigned long s, unsigned long e, unsigned int nf) { /* 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(pl2e); } + else if ( locking ) + spin_unlock(&map_pgdir_lock); } flush_area(NULL, FLUSH_TLB_GLOBAL);