From nobody Mon Apr 29 10:20:12 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1607345413; cv=none; d=zohomail.com; s=zohoarc; b=K1otm/uyrRzaFg3eLd2KEviq9DIupK6aBivp8XrQUDl/4nlMGhrY7OYia1LJP1C0/T7pPu4ZN5R7rwzoNNcs28Z2toDYJJkG/60XyPZKoxHVoD2gPB99jYFKy16wMfLDaMY4phbW03lczXpK3SCEpHGLvlumlNQBHnZX0R3Qykg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1607345413; h=Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:Message-ID:Sender:Subject:To; bh=avA3/AgNUAj6DyXzZAF5RRWLTIEI3aIs4sNfUQvf0+4=; b=IeCOO3SPZEQp4VuSXpZp6aKFEB6MuttFgzMIkUdB6RlSfBreVqulz120HLnP3+V8wxg5wznQ/NGmFnbhZq7Hpj9FaymX1Hgg96aeCipi3jqj0XpPFdM8hrb5qu+ZaSMgM4ScRXqJngkwxR225+FxkL7G/9KIzwKuXjsitZCOeJQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 160734541330045.7438718799541; Mon, 7 Dec 2020 04:50:13 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.46479.82485 (Exim 4.92) (envelope-from ) id 1kmFxi-0000ZC-VO; Mon, 07 Dec 2020 12:49:54 +0000 Received: by outflank-mailman (output) from mailman id 46479.82485; Mon, 07 Dec 2020 12:49:54 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kmFxi-0000Z5-SK; Mon, 07 Dec 2020 12:49:54 +0000 Received: by outflank-mailman (input) for mailman id 46479; Mon, 07 Dec 2020 12:49:53 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kmFxh-0000Z0-3G for xen-devel@lists.xenproject.org; Mon, 07 Dec 2020 12:49:53 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kmFxg-0008Tk-Ei; Mon, 07 Dec 2020 12:49:52 +0000 Received: from 54-240-197-239.amazon.com ([54.240.197.239] helo=u1bbd043a57dd5a.ant.amazon.com) by xenbits.xenproject.org with esmtpsa (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kmFxg-0005Dh-2I; Mon, 07 Dec 2020 12:49:52 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xen.org; s=20200302mail; h=Message-Id:Date:Subject:Cc:To:From; bh=avA3/AgNUAj6DyXzZAF5RRWLTIEI3aIs4sNfUQvf0+4=; b=DpXQsAghdggFCEbbBGYvoNxUqo f9PZ9/smDhPZktVdtNyT0B4bByTZTzsSbppL1rXaCNSdAkN82AXumWNn2mI9xWDl95/09S49/Bv24 Zv6zaMS5zAjFQJlPZqkWmsvxUiQ4hf5JmdTBsd8xzy5Rxx9x0BSdxgSd/aTBHxgSOc20=; From: Hongyan Xia To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Wei Liu , George Dunlap , Ian Jackson , Julien Grall , Stefano Stabellini Subject: [PATCH v3] x86/vmap: handle superpages in vmap_to_mfn() Date: Mon, 7 Dec 2020 12:49:33 +0000 Message-Id: <8e7a12064c68a1743dd3bd8c38feae2abea24071.1607345364.git.hongyxia@amazon.com> X-Mailer: git-send-email 2.17.1 X-ZohoMail-DKIM: pass (identity @xen.org) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Hongyan Xia There is simply no guarantee that vmap won't return superpages to the caller. It can happen if the list of MFNs are contiguous, or we simply have a large granularity. Although rare, if such things do happen, we will simply hit BUG_ON() and crash. Introduce xen_map_to_mfn() to translate any mapped Xen address to mfn regardless of page size, and wrap vmap_to_mfn() around it. Signed-off-by: Hongyan Xia Reviewed-by: Jan Beulich --- Changed in v3: - switch to do-while. - move the declaration close to map_pages_to_xen(). - add missing parentheses to vmap_to_mfn(). Changed in v2: - const pl*e - introduce xen_map_to_mfn(). - goto to a single exit path. - ASSERT_UNREACHABLE instead of ASSERT. --- xen/arch/x86/mm.c | 56 ++++++++++++++++++++++++++++++++++++++ xen/include/asm-x86/page.h | 2 +- xen/include/xen/mm.h | 1 + 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index 5a50339284c7..723cc1070f16 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -5194,6 +5194,62 @@ l1_pgentry_t *virt_to_xen_l1e(unsigned long v) } \ } while ( false ) =20 +/* Translate mapped Xen address to MFN. */ +mfn_t xen_map_to_mfn(unsigned long va) +{ +#define CHECK_MAPPED(cond) \ + do { \ + if ( !(cond) ) \ + { \ + ASSERT_UNREACHABLE(); \ + ret =3D INVALID_MFN; \ + goto out; \ + } \ + } while ( false ) + + bool locking =3D system_state > SYS_STATE_boot; + unsigned int l2_offset =3D l2_table_offset(va); + unsigned int l1_offset =3D l1_table_offset(va); + const l3_pgentry_t *pl3e =3D virt_to_xen_l3e(va); + const l2_pgentry_t *pl2e =3D NULL; + const l1_pgentry_t *pl1e =3D NULL; + struct page_info *l3page; + mfn_t ret; + + L3T_INIT(l3page); + CHECK_MAPPED(pl3e); + l3page =3D virt_to_page(pl3e); + L3T_LOCK(l3page); + + CHECK_MAPPED(l3e_get_flags(*pl3e) & _PAGE_PRESENT); + if ( l3e_get_flags(*pl3e) & _PAGE_PSE ) + { + ret =3D mfn_add(l3e_get_mfn(*pl3e), + (l2_offset << PAGETABLE_ORDER) + l1_offset); + goto out; + } + + pl2e =3D map_l2t_from_l3e(*pl3e) + l2_offset; + CHECK_MAPPED(l2e_get_flags(*pl2e) & _PAGE_PRESENT); + if ( l2e_get_flags(*pl2e) & _PAGE_PSE ) + { + ret =3D mfn_add(l2e_get_mfn(*pl2e), l1_offset); + goto out; + } + + pl1e =3D map_l1t_from_l2e(*pl2e) + l1_offset; + CHECK_MAPPED(l1e_get_flags(*pl1e) & _PAGE_PRESENT); + ret =3D l1e_get_mfn(*pl1e); + +#undef CHECK_MAPPED + out: + L3T_UNLOCK(l3page); + unmap_domain_page(pl1e); + unmap_domain_page(pl2e); + unmap_domain_page(pl3e); + return ret; +} + int map_pages_to_xen( unsigned long virt, mfn_t mfn, diff --git a/xen/include/asm-x86/page.h b/xen/include/asm-x86/page.h index 7a771baf7cb3..082c14a66226 100644 --- a/xen/include/asm-x86/page.h +++ b/xen/include/asm-x86/page.h @@ -291,7 +291,7 @@ void copy_page_sse2(void *, const void *); #define pfn_to_paddr(pfn) __pfn_to_paddr(pfn) #define paddr_to_pfn(pa) __paddr_to_pfn(pa) #define paddr_to_pdx(pa) pfn_to_pdx(paddr_to_pfn(pa)) -#define vmap_to_mfn(va) l1e_get_mfn(*virt_to_xen_l1e((unsigned long)(v= a))) +#define vmap_to_mfn(va) xen_map_to_mfn((unsigned long)(va)) #define vmap_to_page(va) mfn_to_page(vmap_to_mfn(va)) =20 #endif /* !defined(__ASSEMBLY__) */ diff --git a/xen/include/xen/mm.h b/xen/include/xen/mm.h index f7975b2df00b..1475f352e411 100644 --- a/xen/include/xen/mm.h +++ b/xen/include/xen/mm.h @@ -175,6 +175,7 @@ bool scrub_free_pages(void); } while ( false ) #define FREE_XENHEAP_PAGE(p) FREE_XENHEAP_PAGES(p, 0) =20 +mfn_t xen_map_to_mfn(unsigned long va); /* Map machine page range in Xen virtual address space. */ int map_pages_to_xen( unsigned long virt, --=20 2.17.1