From patchwork Thu Sep 26 09:46:44 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Xia, Hongyan" X-Patchwork-Id: 11162197 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 B220213BD for ; Thu, 26 Sep 2019 09:51:44 +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 8DFA82053B for ; Thu, 26 Sep 2019 09:51:44 +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="kApuKHNq" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8DFA82053B 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 1iDQQJ-0001Kp-Em; Thu, 26 Sep 2019 09:50:55 +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 1iDQQE-0001Aw-VB for xen-devel@lists.xenproject.org; Thu, 26 Sep 2019 09:50:50 +0000 X-Inumbo-ID: 1eec13be-e043-11e9-964c-12813bfff9fa Received: from smtp-fw-4101.amazon.com (unknown [72.21.198.25]) by localhost (Halon) with ESMTPS id 1eec13be-e043-11e9-964c-12813bfff9fa; Thu, 26 Sep 2019 09:50:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1569491449; x=1601027449; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=kXHcgiUvuLHbcZv8fnxbRiyV9RFWhHe9YC9kc1lD21c=; b=kApuKHNqTXBAwCu7gg1waklXLZqp18XaTQIAKv11IwlkUZyr8blE0GO9 faja+14OE2cxq1OcOlCucfdrlZ6UmYwNnUDevV96AxprAUGB9ux30GrMq jdjg49tYtpIR3Er+BcPMItkmGJqGek3356kFOMUUuGCPlLCQ0/rIqIYds s=; X-IronPort-AV: E=Sophos;i="5.64,551,1559520000"; d="scan'208";a="787535511" Received: from iad6-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-1e-27fb8269.us-east-1.amazon.com) ([10.124.125.6]) by smtp-border-fw-out-4101.iad4.amazon.com with ESMTP; 26 Sep 2019 09:50:49 +0000 Received: from EX13MTAUEA001.ant.amazon.com (iad55-ws-svc-p15-lb9-vlan3.iad.amazon.com [10.40.159.166]) by email-inbound-relay-1e-27fb8269.us-east-1.amazon.com (Postfix) with ESMTPS id 1A89BA1D79; Thu, 26 Sep 2019 09:50:48 +0000 (UTC) Received: from EX13D28EUB003.ant.amazon.com (10.43.166.124) by EX13MTAUEA001.ant.amazon.com (10.43.61.243) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Thu, 26 Sep 2019 09:50:10 +0000 Received: from EX13MTAUWA001.ant.amazon.com (10.43.160.58) by EX13D28EUB003.ant.amazon.com (10.43.166.124) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Thu, 26 Sep 2019 09:50:08 +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:50:06 +0000 From: To: Date: Thu, 26 Sep 2019 10:46:44 +0100 Message-ID: <08be36e125433c438191fa17bbdaed5b50088530.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 81/84] x86/mm: optimise and properly unmap pages in virt_to_mfn_walk(). 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 , =?utf-8?q?Roger_Pau_Monn?= =?utf-8?q?=C3=A9?= , Wei Liu , Jan Beulich , Hongyan Xia Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" From: Hongyan Xia This also resolves a mapcache overflow bug. Signed-off-by: Hongyan Xia --- xen/arch/x86/mm.c | 57 +++++++++++++++++++++++++--------------- xen/include/asm-x86/mm.h | 3 ++- 2 files changed, 38 insertions(+), 22 deletions(-) diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index 39ba9f9bf4..f3c9042ba6 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -5056,42 +5056,57 @@ l1_pgentry_t *virt_to_xen_l1e(unsigned long v) return pl1e; } +/* + * Unlike virt_to_mfn() which just translates between the direct map and the + * mfn, this version actually walks the page table to find the mfn of any + * virtual address, as long as it is mapped. If not, INVALID_MFN is returned. + */ unsigned long virt_to_mfn_walk(void *va) { unsigned long ret; - l3_pgentry_t *pl3e; - l2_pgentry_t *pl2e; - l1_pgentry_t *pl1e; - - /* - * FIXME: This is rather unoptimised, because e.g. virt_to_xen_l2e - * recomputes virt_to_xen_l3e again. Clearly one can keep the result and - * carry on. - */ + unsigned long v = (unsigned long)va; + l3_pgentry_t *pl3e = NULL; + l2_pgentry_t *pl2e = NULL; + l1_pgentry_t *pl1e = NULL; - pl3e = virt_to_xen_l3e((unsigned long)(va)); - BUG_ON(!(l3e_get_flags(*pl3e) & _PAGE_PRESENT)); + pl3e = virt_to_xen_l3e(v); + if ( !pl3e || !(l3e_get_flags(*pl3e) & _PAGE_PRESENT) ) + { + ret = mfn_x(INVALID_MFN); + goto out; + } if ( l3e_get_flags(*pl3e) & _PAGE_PSE ) { ret = l3e_get_pfn(*pl3e); - ret |= (((unsigned long)va & ((1UL << L3_PAGETABLE_SHIFT)-1)) >> PAGE_SHIFT); - unmap_xen_pagetable(pl3e); - return ret; + ret |= ((v & ((1UL << L3_PAGETABLE_SHIFT)-1)) >> PAGE_SHIFT); + goto out; } - pl2e = virt_to_xen_l2e((unsigned long)(va)); - BUG_ON(!(l2e_get_flags(*pl2e) & _PAGE_PRESENT)); + pl2e = (l2_pgentry_t *)map_xen_pagetable(l3e_get_mfn(*pl3e)) + + l2_table_offset(v); + if ( !(l2e_get_flags(*pl2e) & _PAGE_PRESENT) ) + { + ret = mfn_x(INVALID_MFN); + goto out; + } if ( l2e_get_flags(*pl2e) & _PAGE_PSE ) { ret = l2e_get_pfn(*pl2e); - ret |= (((unsigned long)va & ((1UL << L2_PAGETABLE_SHIFT)-1)) >> PAGE_SHIFT); - unmap_xen_pagetable(pl2e); - return ret; + ret |= ((v & ((1UL << L2_PAGETABLE_SHIFT)-1)) >> PAGE_SHIFT); + goto out; } - pl1e = virt_to_xen_l1e((unsigned long)(va)); - BUG_ON(!(l1e_get_flags(*pl1e) & _PAGE_PRESENT)); + pl1e = (l1_pgentry_t *)map_xen_pagetable(l2e_get_mfn(*pl2e)) + + l1_table_offset(v); + if ( !(l1e_get_flags(*pl1e) & _PAGE_PRESENT) ) + { + ret = mfn_x(INVALID_MFN); + goto out; + } ret = l1e_get_pfn(*pl1e); +out: + unmap_xen_pagetable(pl3e); + unmap_xen_pagetable(pl2e); unmap_xen_pagetable(pl1e); return ret; } diff --git a/xen/include/asm-x86/mm.h b/xen/include/asm-x86/mm.h index e5819cbfdf..411737207f 100644 --- a/xen/include/asm-x86/mm.h +++ b/xen/include/asm-x86/mm.h @@ -645,7 +645,8 @@ void free_xen_pagetable(mfn_t mfn); l1_pgentry_t *virt_to_xen_l1e(unsigned long v); unsigned long virt_to_mfn_walk(void *va); struct page_info *virt_to_page_walk(void *va); -#define virt_to_maddr_walk(va) mfn_to_maddr(_mfn(virt_to_mfn_walk(va))) +#define virt_to_maddr_walk(va) (mfn_to_maddr(_mfn(virt_to_mfn_walk(va))) | \ + ((unsigned long)va & (PAGE_SIZE - 1))) DECLARE_PER_CPU(mfn_t, root_pgt_mfn);