From patchwork Fri Nov 10 07:18:06 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yu Zhang X-Patchwork-Id: 10052499 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 CCA66603FA for ; Fri, 10 Nov 2017 07:45:43 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BD8562B095 for ; Fri, 10 Nov 2017 07:45:43 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B29E62B0A7; Fri, 10 Nov 2017 07:45:43 +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 5FA872B095 for ; Fri, 10 Nov 2017 07:45:43 +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 1eD3xn-0003UB-To; Fri, 10 Nov 2017 07:42:55 +0000 Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eD3xm-0003Tu-Pa for xen-devel@lists.xen.org; Fri, 10 Nov 2017 07:42:54 +0000 Received: from [85.158.139.211] by server-1.bemta-5.messagelabs.com id 8F/0C-02029-E78550A5; Fri, 10 Nov 2017 07:42:54 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFvrHLMWRWlGSWpSXmKPExsVywNykWLc2gjX KYP1iZYslHxezODB6HN39mymAMYo1My8pvyKBNePE1rtMBQelKnrat7M0MP4W7GLk4hASmMYo sWP/CuYuRk4OCQFeiSPLZrBC2D4SkxeeZ4MoameUWD97AxNIgk1AW+LH6t+MILaIgLTEtc+Xw WxmgUqJX//Pgg0SFoiQOD3vP9AgDg4WAVWJOTPyQcK8Al4S++80sEHMl5M4eWwyWAmngLfEkk sVIGEhoJItdxvZJzDyLmBkWMWoXpxaVJZapGukl1SUmZ5RkpuYmaNraGCql5taXJyYnpqTmFS sl5yfu4kRGAoMQLCD8fsfp0OMkhxMSqK8UpYsUUJ8SfkplRmJxRnxRaU5qcWHGGU4OJQkeBPC WaOEBItS01Mr0jJzgEEJk5bg4FES4W0CSfMWFyTmFmemQ6ROMRpzPJv5uoGZY9rV1iZmIZa8/ LxUKXFeS5BSAZDSjNI8uEGwaLnEKCslzMsIdJoQT0FqUW5mCar8K0ZxDkYlYd75IFN4MvNK4P a9AjqFCeiUaHYWkFNKEhFSUg2MoV+WSW84xCrScyDhjPjiL8Hbvnx3u/l3kalH/72PN8V4/53 TX73edN+zquavO37EnJC77VttcFjvqPPTDPOsVy5bboauynVWDN6QUJ7r9vwsq0v0hzifw/Ja jsF9sm6Pd+adZfvOePp+bGgjY8eFM2bagbc12Y4ZTJVLtulXlL+7kG9Zr/pUJZbijERDLeai4 kQAGUDm15ECAAA= X-Env-Sender: yu.c.zhang@linux.intel.com X-Msg-Ref: server-6.tower-206.messagelabs.com!1510299770!103759216!2 X-Originating-IP: [192.55.52.115] 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 61170 invoked from network); 10 Nov 2017 07:42:53 -0000 Received: from mga14.intel.com (HELO mga14.intel.com) (192.55.52.115) by server-6.tower-206.messagelabs.com with DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 10 Nov 2017 07:42:53 -0000 Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 09 Nov 2017 23:42:52 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.44,373,1505804400"; d="scan'208";a="789312" Received: from zhangyu-optiplex-9020.bj.intel.com ([10.238.135.159]) by orsmga003.jf.intel.com with ESMTP; 09 Nov 2017 23:42:49 -0800 From: Yu Zhang To: xen-devel@lists.xen.org Date: Fri, 10 Nov 2017 15:18:06 +0800 Message-Id: <1510298286-30952-2-git-send-email-yu.c.zhang@linux.intel.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1510298286-30952-1-git-send-email-yu.c.zhang@linux.intel.com> References: <1510298286-30952-1-git-send-email-yu.c.zhang@linux.intel.com> Cc: Andrew Cooper , min.he@intel.com, Jan Beulich , yi.z.zhang@intel.com Subject: [Xen-devel] [PATCH v2 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 may be freed, if all entries of this page table are empty. Corresponding L2/L3 PTE will need be cleared in such scenario. However, logic to enumerate the L1/L2 page table and to reset the corresponding L2/L3 PTE need to be protected with spinlock. Otherwise, the paging structure may be freed more than once, if the same routine is invoked simultaneously on different CPUs. Signed-off-by: Yu Zhang --- Cc: Jan Beulich Cc: Andrew Cooper --- xen/arch/x86/mm.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index 47855fb..c07c528 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -5097,6 +5097,17 @@ 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 cleared on another CPU. */ + if ( !(l2e_get_flags(*pl2e) & _PAGE_PRESENT) ) + { + if ( locking ) + spin_unlock(&map_pgdir_lock); + goto check_l3; + } + pl1e = l2e_to_l1e(*pl2e); for ( i = 0; i < L1_PAGETABLE_ENTRIES; i++ ) if ( l1e_get_intpte(pl1e[i]) != 0 ) @@ -5105,11 +5116,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. @@ -5117,6 +5133,17 @@ 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 cleared on another CPU. */ + if ( !(l3e_get_flags(*pl3e) & _PAGE_PRESENT) ) + { + 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 ) @@ -5125,9 +5152,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);