All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v4 00/16] xen: Convert page_to_mfn and mfn_to_page to use typesafe MFN
@ 2018-02-21 14:02 Julien Grall
  2018-02-21 14:02 ` [PATCH v4 01/16] xen/tmem: Convert the file common/tmem_xen.c " Julien Grall
                   ` (15 more replies)
  0 siblings, 16 replies; 76+ messages in thread
From: Julien Grall @ 2018-02-21 14:02 UTC (permalink / raw)
  To: xen-devel
  Cc: Kevin Tian, Stefano Stabellini, Wei Liu, Jun Nakajima,
	Razvan Cojocaru, George Dunlap, Andrew Cooper, Ian Jackson,
	Tim Deegan, Julien Grall, Paul Durrant, Tamas K Lengyel,
	Jan Beulich, Shane Wang, Suravee Suthikulpanit, Boris Ostrovsky,
	Gang Wei

Hi all,

This series is a rework of "xen: Convert __page_to_mfn and __mfn_to_page to use
typesafe MFN" sent a couple of months ago (see [1]). In this new approach,
the macros __page_to_mfn and __mfn_to_page are completely dropped.

To avoid the last patch (#16) to be a huge patch some files are converted to
use typesafe upfront. I have tried my best to push _mfn/mfn_x as down as
possible in the callers. Some of them was not feasible without major rework,
so I left them aside for now. Contribution to switch Xen code base to MFN
typesafe are more than welcomed.

Note that changes have only been build test it on x86 so far.

Cheers,

[1] https://lists.xen.org/archives/html/xen-devel/2017-11/msg00027.html

Cc: Andrew Cooper <andrew.cooper3@citrix.com>
Cc: Boris Ostrovsky <boris.ostrovsky@oracle.com>
Cc: Gang Wei <gang.wei@intel.com>
Cc: George Dunlap <george.dunlap@eu.citrix.com>
Cc: George Dunlap <George.Dunlap@eu.citrix.com>
Cc: Ian Jackson <ian.jackson@eu.citrix.com>
Cc: Jan Beulich <jbeulich@suse.com>
Cc: Julien Grall <julien.grall@arm.com>
Cc: Jun Nakajima <jun.nakajima@intel.com>
Cc: Kevin Tian <kevin.tian@intel.com>
Cc: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
Cc: Paul Durrant <paul.durrant@citrix.com>
Cc: Razvan Cojocaru <rcojocaru@bitdefender.com>
Cc: Shane Wang <shane.wang@intel.com>
Cc: Stefano Stabellini <sstabellini@kernel.org>
Cc: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
Cc: Tamas K Lengyel <tamas@tklengyel.com>
Cc: Tim Deegan <tim@xen.org>
Cc: Wei Liu <wei.liu2@citrix.com>

Julien Grall (16):
  xen/tmem: Convert the file common/tmem_xen.c to use typesafe MFN
  xen/arm: setup: use maddr_to_mfn rather than _mfn(paddr_to_pfn(...))
  xen/arm: mm: Use gaddr_to_gfn rather than _gfn(paddr_to_pfn(...))
  xen/arm: mm: Remove unused M2P code
  xen/arm: mm: Remove unused relinquish_shared_pages
  xen/x86: Remove unused override of page_to_mfn/mfn_to_page
  xen/x86: mm: Switch x86/mm.c to use typesafe for virt_to_mfn
  xen/mm: Drop the parameter mfn from populate_pt_range
  xen/pdx: Introduce helper to convert MFN <-> PDX
  xen/mm: Switch map_pages_to_xen to use MFN typesafe
  xen/mm: Switch page_alloc.c to typesafe MFN
  xen/mm: Switch common/memory.c to use typesafe MFN
  xen/grant: Switch {create, replace}_grant_p2m_mapping to typesafe MFN
  xen/grant: Switch common/grant_table.c to use typesafe MFN
  xen/x86: Switch mfn_to_page in x86_64/mm.c to use typesafe MFN
  xen: Convert page_to_mfn and mfn_to_page to use typesafe MFN

 xen/arch/arm/domain_build.c             |   2 -
 xen/arch/arm/kernel.c                   |   2 +-
 xen/arch/arm/mem_access.c               |   2 +-
 xen/arch/arm/mm.c                       |  33 ++++----
 xen/arch/arm/p2m.c                      |  10 +--
 xen/arch/arm/setup.c                    |   4 +-
 xen/arch/x86/cpu/vpmu.c                 |   4 +-
 xen/arch/x86/domain.c                   |  21 ++---
 xen/arch/x86/domain_page.c              |   6 +-
 xen/arch/x86/hvm/dm.c                   |   2 +-
 xen/arch/x86/hvm/dom0_build.c           |   6 +-
 xen/arch/x86/hvm/emulate.c              |   6 +-
 xen/arch/x86/hvm/grant_table.c          |  14 ++--
 xen/arch/x86/hvm/hvm.c                  |  12 +--
 xen/arch/x86/hvm/ioreq.c                |   4 +-
 xen/arch/x86/hvm/stdvga.c               |   2 +-
 xen/arch/x86/hvm/svm/svm.c              |   4 +-
 xen/arch/x86/hvm/viridian.c             |   6 +-
 xen/arch/x86/hvm/vmx/vmcs.c             |   2 +-
 xen/arch/x86/hvm/vmx/vmx.c              |  10 +--
 xen/arch/x86/hvm/vmx/vvmx.c             |   6 +-
 xen/arch/x86/mm.c                       |  70 ++++++++---------
 xen/arch/x86/mm/guest_walk.c            |   6 +-
 xen/arch/x86/mm/hap/guest_walk.c        |   2 +-
 xen/arch/x86/mm/hap/hap.c               |   6 --
 xen/arch/x86/mm/hap/nested_ept.c        |   2 +-
 xen/arch/x86/mm/hap/nested_hap.c        |   3 -
 xen/arch/x86/mm/mem_sharing.c           |   5 --
 xen/arch/x86/mm/p2m-ept.c               |   8 +-
 xen/arch/x86/mm/p2m-pod.c               |   6 --
 xen/arch/x86/mm/p2m-pt.c                |   6 --
 xen/arch/x86/mm/p2m.c                   |   6 --
 xen/arch/x86/mm/paging.c                |   6 --
 xen/arch/x86/mm/shadow/private.h        |  16 +---
 xen/arch/x86/numa.c                     |   2 +-
 xen/arch/x86/physdev.c                  |   2 +-
 xen/arch/x86/pv/callback.c              |   6 --
 xen/arch/x86/pv/descriptor-tables.c     |   6 --
 xen/arch/x86/pv/dom0_build.c            |  14 ++--
 xen/arch/x86/pv/domain.c                |   6 --
 xen/arch/x86/pv/emul-gate-op.c          |   6 --
 xen/arch/x86/pv/emul-priv-op.c          |  10 ---
 xen/arch/x86/pv/grant_table.c           |  16 ++--
 xen/arch/x86/pv/iret.c                  |   6 --
 xen/arch/x86/pv/mm.c                    |   6 --
 xen/arch/x86/pv/ro-page-fault.c         |   6 --
 xen/arch/x86/pv/shim.c                  |   4 +-
 xen/arch/x86/pv/traps.c                 |   6 --
 xen/arch/x86/setup.c                    |  14 ++--
 xen/arch/x86/smpboot.c                  |   8 +-
 xen/arch/x86/tboot.c                    |  15 ++--
 xen/arch/x86/traps.c                    |   4 +-
 xen/arch/x86/x86_64/mm.c                |  67 ++++++++--------
 xen/arch/x86/x86_64/mmconfig_64.c       |   4 +-
 xen/common/domain.c                     |   4 +-
 xen/common/efi/boot.c                   |   2 +-
 xen/common/grant_table.c                | 131 +++++++++++++++++---------------
 xen/common/kimage.c                     |   6 --
 xen/common/memory.c                     |  66 +++++++++-------
 xen/common/page_alloc.c                 |  58 +++++++-------
 xen/common/tmem.c                       |   2 +-
 xen/common/tmem_xen.c                   |  26 ++++---
 xen/common/trace.c                      |   4 +-
 xen/common/vmap.c                       |  10 +--
 xen/common/xenoprof.c                   |   2 -
 xen/drivers/acpi/apei/erst.c            |   2 +-
 xen/drivers/acpi/apei/hest.c            |   2 +-
 xen/drivers/passthrough/amd/iommu_map.c |  12 +--
 xen/drivers/passthrough/iommu.c         |   2 +-
 xen/drivers/passthrough/vtd/dmar.c      |   2 +-
 xen/drivers/passthrough/x86/iommu.c     |   2 +-
 xen/include/asm-arm/grant_table.h       |  11 ++-
 xen/include/asm-arm/mm.h                |  45 +++--------
 xen/include/asm-arm/numa.h              |   8 +-
 xen/include/asm-arm/p2m.h               |   4 +-
 xen/include/asm-x86/grant_table.h       |   6 +-
 xen/include/asm-x86/hvm/grant_table.h   |   8 +-
 xen/include/asm-x86/mm.h                |  12 +--
 xen/include/asm-x86/p2m.h               |   2 +-
 xen/include/asm-x86/page.h              |  32 ++++----
 xen/include/asm-x86/pv/grant_table.h    |   8 +-
 xen/include/xen/domain_page.h           |   8 +-
 xen/include/xen/mm.h                    |  10 +--
 xen/include/xen/pdx.h                   |   3 +
 xen/include/xen/tmem_xen.h              |   2 +-
 85 files changed, 429 insertions(+), 571 deletions(-)

-- 
2.11.0


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* [PATCH v4 01/16] xen/tmem: Convert the file common/tmem_xen.c to use typesafe MFN
  2018-02-21 14:02 [PATCH v4 00/16] xen: Convert page_to_mfn and mfn_to_page to use typesafe MFN Julien Grall
@ 2018-02-21 14:02 ` Julien Grall
  2018-02-21 14:02 ` [PATCH v4 02/16] xen/arm: setup: use maddr_to_mfn rather than _mfn(paddr_to_pfn(...)) Julien Grall
                   ` (14 subsequent siblings)
  15 siblings, 0 replies; 76+ messages in thread
From: Julien Grall @ 2018-02-21 14:02 UTC (permalink / raw)
  To: xen-devel; +Cc: Andrew Cooper, Julien Grall

The file common/tmem_xen.c is now converted to use typesafe. This is
requiring to override the macro page_to_mfn to make it work with mfn_t.

Note that all variables converted to mfn_t havem there initial value,
when set, switch from 0 to INVALID_MFN. This is fine because the initial
values was always overriden before used.

Also add a couple of missing newlines suggested by Andrew in the code.

Signed-off-by: Julien Grall <julien.grall@arm.com>
Reviewed-by: Andrew Cooper <andrew.cooper3@citrix.com>
Acked-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>

---

    Changes in v4:
        - Add Konrad's acked-by

    Changes in v2:
        - Add missing newlines
        - Add Andrew's reviewed-by
---
 xen/common/tmem_xen.c | 30 ++++++++++++++++++------------
 1 file changed, 18 insertions(+), 12 deletions(-)

diff --git a/xen/common/tmem_xen.c b/xen/common/tmem_xen.c
index 20f74b268f..bd52e44faf 100644
--- a/xen/common/tmem_xen.c
+++ b/xen/common/tmem_xen.c
@@ -14,6 +14,10 @@
 #include <xen/cpu.h>
 #include <xen/init.h>
 
+/* Override macros from asm/page.h to make them work with mfn_t */
+#undef page_to_mfn
+#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
+
 bool __read_mostly opt_tmem;
 boolean_param("tmem", opt_tmem);
 
@@ -31,7 +35,7 @@ static DEFINE_PER_CPU_READ_MOSTLY(unsigned char *, dstmem);
 static DEFINE_PER_CPU_READ_MOSTLY(void *, scratch_page);
 
 #if defined(CONFIG_ARM)
-static inline void *cli_get_page(xen_pfn_t cmfn, unsigned long *pcli_mfn,
+static inline void *cli_get_page(xen_pfn_t cmfn, mfn_t *pcli_mfn,
                                  struct page_info **pcli_pfp, bool cli_write)
 {
     ASSERT_UNREACHABLE();
@@ -39,14 +43,14 @@ static inline void *cli_get_page(xen_pfn_t cmfn, unsigned long *pcli_mfn,
 }
 
 static inline void cli_put_page(void *cli_va, struct page_info *cli_pfp,
-                                unsigned long cli_mfn, bool mark_dirty)
+                                mfn_t cli_mfn, bool mark_dirty)
 {
     ASSERT_UNREACHABLE();
 }
 #else
 #include <asm/p2m.h>
 
-static inline void *cli_get_page(xen_pfn_t cmfn, unsigned long *pcli_mfn,
+static inline void *cli_get_page(xen_pfn_t cmfn, mfn_t *pcli_mfn,
                                  struct page_info **pcli_pfp, bool cli_write)
 {
     p2m_type_t t;
@@ -68,16 +72,17 @@ static inline void *cli_get_page(xen_pfn_t cmfn, unsigned long *pcli_mfn,
 
     *pcli_mfn = page_to_mfn(page);
     *pcli_pfp = page;
-    return map_domain_page(_mfn(*pcli_mfn));
+
+    return map_domain_page(*pcli_mfn);
 }
 
 static inline void cli_put_page(void *cli_va, struct page_info *cli_pfp,
-                                unsigned long cli_mfn, bool mark_dirty)
+                                mfn_t cli_mfn, bool mark_dirty)
 {
     if ( mark_dirty )
     {
         put_page_and_type(cli_pfp);
-        paging_mark_dirty(current->domain, _mfn(cli_mfn));
+        paging_mark_dirty(current->domain, cli_mfn);
     }
     else
         put_page(cli_pfp);
@@ -88,14 +93,14 @@ static inline void cli_put_page(void *cli_va, struct page_info *cli_pfp,
 int tmem_copy_from_client(struct page_info *pfp,
     xen_pfn_t cmfn, tmem_cli_va_param_t clibuf)
 {
-    unsigned long tmem_mfn, cli_mfn = 0;
+    mfn_t tmem_mfn, cli_mfn = INVALID_MFN;
     char *tmem_va, *cli_va = NULL;
     struct page_info *cli_pfp = NULL;
     int rc = 1;
 
     ASSERT(pfp != NULL);
     tmem_mfn = page_to_mfn(pfp);
-    tmem_va = map_domain_page(_mfn(tmem_mfn));
+    tmem_va = map_domain_page(tmem_mfn);
     if ( guest_handle_is_null(clibuf) )
     {
         cli_va = cli_get_page(cmfn, &cli_mfn, &cli_pfp, 0);
@@ -125,7 +130,7 @@ int tmem_compress_from_client(xen_pfn_t cmfn,
     unsigned char *wmem = this_cpu(workmem);
     char *scratch = this_cpu(scratch_page);
     struct page_info *cli_pfp = NULL;
-    unsigned long cli_mfn = 0;
+    mfn_t cli_mfn = INVALID_MFN;
     void *cli_va = NULL;
 
     if ( dmem == NULL || wmem == NULL )
@@ -152,7 +157,7 @@ int tmem_compress_from_client(xen_pfn_t cmfn,
 int tmem_copy_to_client(xen_pfn_t cmfn, struct page_info *pfp,
     tmem_cli_va_param_t clibuf)
 {
-    unsigned long tmem_mfn, cli_mfn = 0;
+    mfn_t tmem_mfn, cli_mfn = INVALID_MFN;
     char *tmem_va, *cli_va = NULL;
     struct page_info *cli_pfp = NULL;
     int rc = 1;
@@ -165,7 +170,8 @@ int tmem_copy_to_client(xen_pfn_t cmfn, struct page_info *pfp,
             return -EFAULT;
     }
     tmem_mfn = page_to_mfn(pfp);
-    tmem_va = map_domain_page(_mfn(tmem_mfn));
+    tmem_va = map_domain_page(tmem_mfn);
+
     if ( cli_va )
     {
         memcpy(cli_va, tmem_va, PAGE_SIZE);
@@ -181,7 +187,7 @@ int tmem_copy_to_client(xen_pfn_t cmfn, struct page_info *pfp,
 int tmem_decompress_to_client(xen_pfn_t cmfn, void *tmem_va,
                                     size_t size, tmem_cli_va_param_t clibuf)
 {
-    unsigned long cli_mfn = 0;
+    mfn_t cli_mfn = INVALID_MFN;
     struct page_info *cli_pfp = NULL;
     void *cli_va = NULL;
     char *scratch = this_cpu(scratch_page);
-- 
2.11.0


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply related	[flat|nested] 76+ messages in thread

* [PATCH v4 02/16] xen/arm: setup: use maddr_to_mfn rather than _mfn(paddr_to_pfn(...))
  2018-02-21 14:02 [PATCH v4 00/16] xen: Convert page_to_mfn and mfn_to_page to use typesafe MFN Julien Grall
  2018-02-21 14:02 ` [PATCH v4 01/16] xen/tmem: Convert the file common/tmem_xen.c " Julien Grall
@ 2018-02-21 14:02 ` Julien Grall
  2018-02-21 14:02 ` [PATCH v4 03/16] xen/arm: mm: Use gaddr_to_gfn rather than _gfn(paddr_to_pfn(...)) Julien Grall
                   ` (13 subsequent siblings)
  15 siblings, 0 replies; 76+ messages in thread
From: Julien Grall @ 2018-02-21 14:02 UTC (permalink / raw)
  To: xen-devel; +Cc: Julien Grall, Stefano Stabellini

The construction _mfn(paddr_to_pfn(...)) can be simplified by using
maddr_to_mfn.

Signed-off-by: Julien Grall <julien.grall@arm.com>

---
Cc: Stefano Stabellini <sstabellini@kernel.org>

    Changes in v4:
        - Patch added
---
 xen/arch/arm/setup.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/xen/arch/arm/setup.c b/xen/arch/arm/setup.c
index 032a6a882d..de9e958bfe 100644
--- a/xen/arch/arm/setup.c
+++ b/xen/arch/arm/setup.c
@@ -268,8 +268,8 @@ void __init discard_initial_modules(void)
         if ( mi->module[i].kind == BOOTMOD_XEN )
             continue;
 
-        if ( !mfn_valid(_mfn(paddr_to_pfn(s))) ||
-             !mfn_valid(_mfn(paddr_to_pfn(e))))
+        if ( !mfn_valid(maddr_to_mfn(s)) ||
+             !mfn_valid(maddr_to_mfn(e)) )
             continue;
 
         dt_unreserved_regions(s, e, init_domheap_pages, 0);
-- 
2.11.0


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply related	[flat|nested] 76+ messages in thread

* [PATCH v4 03/16] xen/arm: mm: Use gaddr_to_gfn rather than _gfn(paddr_to_pfn(...))
  2018-02-21 14:02 [PATCH v4 00/16] xen: Convert page_to_mfn and mfn_to_page to use typesafe MFN Julien Grall
  2018-02-21 14:02 ` [PATCH v4 01/16] xen/tmem: Convert the file common/tmem_xen.c " Julien Grall
  2018-02-21 14:02 ` [PATCH v4 02/16] xen/arm: setup: use maddr_to_mfn rather than _mfn(paddr_to_pfn(...)) Julien Grall
@ 2018-02-21 14:02 ` Julien Grall
  2018-02-21 14:02 ` [PATCH v4 04/16] xen/arm: mm: Remove unused M2P code Julien Grall
                   ` (12 subsequent siblings)
  15 siblings, 0 replies; 76+ messages in thread
From: Julien Grall @ 2018-02-21 14:02 UTC (permalink / raw)
  To: xen-devel; +Cc: Julien Grall, Stefano Stabellini

The construction _gfn(paddr_to_pfn(...)) can be simplified by using
gaddr_to_gfn.

Signed-off-by: Julien Grall <julien.grall@arm.com>

---
Cc: Stefano Stabellini <sstabellini@kernel.org>

    Changes in v4:
        - Patch added
---
 xen/arch/arm/mm.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/xen/arch/arm/mm.c b/xen/arch/arm/mm.c
index 3c328e2df5..9b77ab5f33 100644
--- a/xen/arch/arm/mm.c
+++ b/xen/arch/arm/mm.c
@@ -1436,7 +1436,7 @@ int create_grant_host_mapping(unsigned long addr, unsigned long frame,
     if ( flags & GNTMAP_readonly )
         t = p2m_grant_map_ro;
 
-    rc = guest_physmap_add_entry(current->domain, _gfn(addr >> PAGE_SHIFT),
+    rc = guest_physmap_add_entry(current->domain, gaddr_to_gfn(addr),
                                  _mfn(frame), 0, t);
 
     if ( rc )
@@ -1448,7 +1448,7 @@ int create_grant_host_mapping(unsigned long addr, unsigned long frame,
 int replace_grant_host_mapping(unsigned long addr, unsigned long mfn,
         unsigned long new_addr, unsigned int flags)
 {
-    gfn_t gfn = _gfn(addr >> PAGE_SHIFT);
+    gfn_t gfn = gaddr_to_gfn(addr);
     struct domain *d = current->domain;
     int rc;
 
-- 
2.11.0


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply related	[flat|nested] 76+ messages in thread

* [PATCH v4 04/16] xen/arm: mm: Remove unused M2P code
  2018-02-21 14:02 [PATCH v4 00/16] xen: Convert page_to_mfn and mfn_to_page to use typesafe MFN Julien Grall
                   ` (2 preceding siblings ...)
  2018-02-21 14:02 ` [PATCH v4 03/16] xen/arm: mm: Use gaddr_to_gfn rather than _gfn(paddr_to_pfn(...)) Julien Grall
@ 2018-02-21 14:02 ` Julien Grall
  2018-02-21 14:02 ` [PATCH v4 05/16] xen/arm: mm: Remove unused relinquish_shared_pages Julien Grall
                   ` (11 subsequent siblings)
  15 siblings, 0 replies; 76+ messages in thread
From: Julien Grall @ 2018-02-21 14:02 UTC (permalink / raw)
  To: xen-devel; +Cc: Julien Grall, Stefano Stabellini

Arm does not have an M2P and very unlikely to get one in the future,
therefore don't keep defines that are not necessary in the common code.

At the same time move the remaining M2P define just above just above
set_gpfn_from_mfn to keep all the dummy helpers for M2P together.

Signed-off-by: Julien Grall <julien.grall@arm.com>

---

Cc: Stefano Stabellini <sstabellini@kernel.org>

    Changes in v4:
        - Patch added.
---
 xen/include/asm-arm/mm.h | 25 ++++---------------------
 1 file changed, 4 insertions(+), 21 deletions(-)

diff --git a/xen/include/asm-arm/mm.h b/xen/include/asm-arm/mm.h
index 4d5563b0ce..c03f4ad674 100644
--- a/xen/include/asm-arm/mm.h
+++ b/xen/include/asm-arm/mm.h
@@ -320,33 +320,16 @@ static inline void *page_to_virt(const struct page_info *pg)
 struct page_info *get_page_from_gva(struct vcpu *v, vaddr_t va,
                                     unsigned long flags);
 
-/*
- * The MPT (machine->physical mapping table) is an array of word-sized
- * values, indexed on machine frame number. It is expected that guest OSes
- * will use it to store a "physical" frame number to give the appearance of
- * contiguous (or near contiguous) physical memory.
- */
-#undef  machine_to_phys_mapping
-#define machine_to_phys_mapping  ((unsigned long *)RDWR_MPT_VIRT_START)
-#define INVALID_M2P_ENTRY        (~0UL)
-#define VALID_M2P(_e)            (!((_e) & (1UL<<(BITS_PER_LONG-1))))
-#define SHARED_M2P_ENTRY         (~0UL - 1UL)
-#define SHARED_M2P(_e)           ((_e) == SHARED_M2P_ENTRY)
-
-#define _set_gpfn_from_mfn(mfn, pfn) ({                        \
-    struct domain *d = page_get_owner(__mfn_to_page(mfn));     \
-    if(d && (d == dom_cow))                                    \
-        machine_to_phys_mapping[(mfn)] = SHARED_M2P_ENTRY;     \
-    else                                                       \
-        machine_to_phys_mapping[(mfn)] = (pfn);                \
-    })
-
 static inline void put_gfn(struct domain *d, unsigned long gfn) {}
 static inline int relinquish_shared_pages(struct domain *d)
 {
     return 0;
 }
 
+#define INVALID_M2P_ENTRY        (~0UL)
+#define SHARED_M2P_ENTRY         (~0UL - 1UL)
+#define SHARED_M2P(_e)           ((_e) == SHARED_M2P_ENTRY)
+
 /* Xen always owns P2M on ARM */
 #define set_gpfn_from_mfn(mfn, pfn) do { (void) (mfn), (void)(pfn); } while (0)
 #define mfn_to_gmfn(_d, mfn)  (mfn)
-- 
2.11.0


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply related	[flat|nested] 76+ messages in thread

* [PATCH v4 05/16] xen/arm: mm: Remove unused relinquish_shared_pages
  2018-02-21 14:02 [PATCH v4 00/16] xen: Convert page_to_mfn and mfn_to_page to use typesafe MFN Julien Grall
                   ` (3 preceding siblings ...)
  2018-02-21 14:02 ` [PATCH v4 04/16] xen/arm: mm: Remove unused M2P code Julien Grall
@ 2018-02-21 14:02 ` Julien Grall
  2018-02-21 14:02 ` [PATCH v4 06/16] xen/x86: Remove unused override of page_to_mfn/mfn_to_page Julien Grall
                   ` (10 subsequent siblings)
  15 siblings, 0 replies; 76+ messages in thread
From: Julien Grall @ 2018-02-21 14:02 UTC (permalink / raw)
  To: xen-devel; +Cc: Julien Grall, Stefano Stabellini

relinquish_shared_pages is never called on Arm.

Signed-off-by: Julien Grall <julien.grall@arm.com>

---

Cc: Stefano Stabellini <sstabellini@kernel.org>

    Changes in v4:
        - Patch added
---
 xen/include/asm-arm/mm.h | 4 ----
 1 file changed, 4 deletions(-)

diff --git a/xen/include/asm-arm/mm.h b/xen/include/asm-arm/mm.h
index c03f4ad674..7678e29c15 100644
--- a/xen/include/asm-arm/mm.h
+++ b/xen/include/asm-arm/mm.h
@@ -321,10 +321,6 @@ struct page_info *get_page_from_gva(struct vcpu *v, vaddr_t va,
                                     unsigned long flags);
 
 static inline void put_gfn(struct domain *d, unsigned long gfn) {}
-static inline int relinquish_shared_pages(struct domain *d)
-{
-    return 0;
-}
 
 #define INVALID_M2P_ENTRY        (~0UL)
 #define SHARED_M2P_ENTRY         (~0UL - 1UL)
-- 
2.11.0


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply related	[flat|nested] 76+ messages in thread

* [PATCH v4 06/16] xen/x86: Remove unused override of page_to_mfn/mfn_to_page
  2018-02-21 14:02 [PATCH v4 00/16] xen: Convert page_to_mfn and mfn_to_page to use typesafe MFN Julien Grall
                   ` (4 preceding siblings ...)
  2018-02-21 14:02 ` [PATCH v4 05/16] xen/arm: mm: Remove unused relinquish_shared_pages Julien Grall
@ 2018-02-21 14:02 ` Julien Grall
  2018-03-01 11:20   ` George Dunlap
  2018-03-02 14:42   ` Jan Beulich
  2018-02-21 14:02 ` [PATCH v4 07/16] xen/x86: mm: Switch x86/mm.c to use typesafe for virt_to_mfn Julien Grall
                   ` (9 subsequent siblings)
  15 siblings, 2 replies; 76+ messages in thread
From: Julien Grall @ 2018-02-21 14:02 UTC (permalink / raw)
  To: xen-devel; +Cc: George Dunlap, Andrew Cooper, Julien Grall, Jan Beulich

A few files override page_to_mfn/mfn_to_page but actually never use
those macros. So drop them.

Signed-off-by: Julien Grall <julien.grall@arm.com>

---
Cc: George Dunlap <george.dunlap@eu.citrix.com>
Cc: Jan Beulich <jbeulich@suse.com>
Cc: Andrew Cooper <andrew.cooper3@citrix.com>

    Changes in v4:
        - Patch added
---
 xen/arch/x86/mm/hap/nested_hap.c | 3 ---
 xen/arch/x86/mm/p2m-pt.c         | 6 ------
 xen/arch/x86/pv/iret.c           | 6 ------
 xen/arch/x86/pv/mm.c             | 6 ------
 xen/arch/x86/pv/traps.c          | 6 ------
 5 files changed, 27 deletions(-)

diff --git a/xen/arch/x86/mm/hap/nested_hap.c b/xen/arch/x86/mm/hap/nested_hap.c
index 4603ceced4..d2a07a5c79 100644
--- a/xen/arch/x86/mm/hap/nested_hap.c
+++ b/xen/arch/x86/mm/hap/nested_hap.c
@@ -70,9 +70,6 @@
 /********************************************/
 /*        NESTED VIRT P2M FUNCTIONS         */
 /********************************************/
-/* Override macros from asm/page.h to make them work with mfn_t */
-#undef page_to_mfn
-#define page_to_mfn(_pg) _mfn(__page_to_mfn(_pg))
 
 void
 nestedp2m_write_p2m_entry(struct p2m_domain *p2m, unsigned long gfn,
diff --git a/xen/arch/x86/mm/p2m-pt.c b/xen/arch/x86/mm/p2m-pt.c
index 753124bdcd..b8c5d2ed26 100644
--- a/xen/arch/x86/mm/p2m-pt.c
+++ b/xen/arch/x86/mm/p2m-pt.c
@@ -39,12 +39,6 @@
 
 #include "mm-locks.h"
 
-/* Override macros from asm/page.h to make them work with mfn_t */
-#undef mfn_to_page
-#define mfn_to_page(_m) __mfn_to_page(mfn_x(_m))
-#undef page_to_mfn
-#define page_to_mfn(_pg) _mfn(__page_to_mfn(_pg))
-
 /*
  * We may store INVALID_MFN in PTEs.  We need to clip this to avoid trampling
  * over higher-order bits (NX, p2m type, IOMMU flags).  We seem to not need
diff --git a/xen/arch/x86/pv/iret.c b/xen/arch/x86/pv/iret.c
index 56aeac360a..ca433a69c4 100644
--- a/xen/arch/x86/pv/iret.c
+++ b/xen/arch/x86/pv/iret.c
@@ -24,12 +24,6 @@
 #include <asm/current.h>
 #include <asm/traps.h>
 
-/* Override macros from asm/page.h to make them work with mfn_t */
-#undef mfn_to_page
-#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
-#undef page_to_mfn
-#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
-
 unsigned long do_iret(void)
 {
     struct cpu_user_regs *regs = guest_cpu_user_regs();
diff --git a/xen/arch/x86/pv/mm.c b/xen/arch/x86/pv/mm.c
index 8d7a4fd85f..b46fd94c2c 100644
--- a/xen/arch/x86/pv/mm.c
+++ b/xen/arch/x86/pv/mm.c
@@ -26,12 +26,6 @@
 
 #include "mm.h"
 
-/* Override macros from asm/page.h to make them work with mfn_t */
-#undef mfn_to_page
-#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
-#undef page_to_mfn
-#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
-
 /*
  * Get a mapping of a PV guest's l1e for this linear address.  The return
  * pointer should be unmapped using unmap_domain_page().
diff --git a/xen/arch/x86/pv/traps.c b/xen/arch/x86/pv/traps.c
index d122881d13..2a605edf7f 100644
--- a/xen/arch/x86/pv/traps.c
+++ b/xen/arch/x86/pv/traps.c
@@ -29,12 +29,6 @@
 #include <asm/shared.h>
 #include <asm/traps.h>
 
-/* Override macros from asm/page.h to make them work with mfn_t */
-#undef mfn_to_page
-#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
-#undef page_to_mfn
-#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
-
 void do_entry_int82(struct cpu_user_regs *regs)
 {
     if ( unlikely(untrusted_msi) )
-- 
2.11.0


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply related	[flat|nested] 76+ messages in thread

* [PATCH v4 07/16] xen/x86: mm: Switch x86/mm.c to use typesafe for virt_to_mfn
  2018-02-21 14:02 [PATCH v4 00/16] xen: Convert page_to_mfn and mfn_to_page to use typesafe MFN Julien Grall
                   ` (5 preceding siblings ...)
  2018-02-21 14:02 ` [PATCH v4 06/16] xen/x86: Remove unused override of page_to_mfn/mfn_to_page Julien Grall
@ 2018-02-21 14:02 ` Julien Grall
  2018-03-02 14:45   ` Jan Beulich
  2018-02-21 14:02 ` [PATCH v4 08/16] xen/mm: Drop the parameter mfn from populate_pt_range Julien Grall
                   ` (8 subsequent siblings)
  15 siblings, 1 reply; 76+ messages in thread
From: Julien Grall @ 2018-02-21 14:02 UTC (permalink / raw)
  To: xen-devel; +Cc: Andrew Cooper, Julien Grall, Jan Beulich

No functional change intended.

Signed-off Julien Grall <julien.grall@arm.com>

---
Cc: Jan Beulich <jbeulich@suse.com>
Cc: Andrew Cooper <andrew.cooper3@citrix.com>

    Changes in v4:
        - Patch added
---
 xen/arch/x86/mm.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c
index e1f089be14..eba1afaa31 100644
--- a/xen/arch/x86/mm.c
+++ b/xen/arch/x86/mm.c
@@ -135,6 +135,8 @@
 #define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
 #undef page_to_mfn
 #define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
+#undef virt_to_mfn
+#define virt_to_mfn(v) _mfn(__virt_to_mfn(v))
 
 /* Mapping of the fixmap space needed early. */
 l1_pgentry_t __section(".bss.page_aligned") __aligned(PAGE_SIZE)
@@ -380,7 +382,7 @@ void __init arch_init_memory(void)
                         l3tab[i] = l3idle[i];
                     for ( ; i < L3_PAGETABLE_ENTRIES; ++i )
                         l3tab[i] = l3e_empty();
-                    split_l4e = l4e_from_pfn(virt_to_mfn(l3tab),
+                    split_l4e = l4e_from_mfn(virt_to_mfn(l3tab),
                                              __PAGE_HYPERVISOR_RW);
                 }
                 else
@@ -4159,7 +4161,7 @@ int xenmem_add_to_physmap_one(
     {
         case XENMAPSPACE_shared_info:
             if ( idx == 0 )
-                mfn = _mfn(virt_to_mfn(d->shared_info));
+                mfn = virt_to_mfn(d->shared_info);
             break;
         case XENMAPSPACE_grant_table:
             rc = gnttab_map_frame(d, idx, gpfn, &mfn);
@@ -4783,7 +4785,7 @@ int map_pages_to_xen(
             if ( (l3e_get_flags(*pl3e) & _PAGE_PRESENT) &&
                  (l3e_get_flags(*pl3e) & _PAGE_PSE) )
             {
-                l3e_write_atomic(pl3e, l3e_from_pfn(virt_to_mfn(pl2e),
+                l3e_write_atomic(pl3e, l3e_from_mfn(virt_to_mfn(pl2e),
                                                     __PAGE_HYPERVISOR));
                 pl2e = NULL;
             }
@@ -4879,7 +4881,7 @@ int map_pages_to_xen(
                 if ( (l2e_get_flags(*pl2e) & _PAGE_PRESENT) &&
                      (l2e_get_flags(*pl2e) & _PAGE_PSE) )
                 {
-                    l2e_write_atomic(pl2e, l2e_from_pfn(virt_to_mfn(pl1e),
+                    l2e_write_atomic(pl2e, l2e_from_mfn(virt_to_mfn(pl1e),
                                                         __PAGE_HYPERVISOR));
                     pl1e = NULL;
                 }
@@ -5087,7 +5089,7 @@ int modify_xen_mappings(unsigned long s, unsigned long e, unsigned int nf)
             if ( (l3e_get_flags(*pl3e) & _PAGE_PRESENT) &&
                  (l3e_get_flags(*pl3e) & _PAGE_PSE) )
             {
-                l3e_write_atomic(pl3e, l3e_from_pfn(virt_to_mfn(pl2e),
+                l3e_write_atomic(pl3e, l3e_from_mfn(virt_to_mfn(pl2e),
                                                     __PAGE_HYPERVISOR));
                 pl2e = NULL;
             }
@@ -5141,7 +5143,7 @@ int modify_xen_mappings(unsigned long s, unsigned long e, unsigned int nf)
                 if ( (l2e_get_flags(*pl2e) & _PAGE_PRESENT) &&
                      (l2e_get_flags(*pl2e) & _PAGE_PSE) )
                 {
-                    l2e_write_atomic(pl2e, l2e_from_pfn(virt_to_mfn(pl1e),
+                    l2e_write_atomic(pl2e, l2e_from_mfn(virt_to_mfn(pl1e),
                                                         __PAGE_HYPERVISOR));
                     pl1e = NULL;
                 }
@@ -5545,8 +5547,7 @@ static void __memguard_change_range(void *p, unsigned long l, int guard)
     if ( guard )
         flags &= ~_PAGE_PRESENT;
 
-    map_pages_to_xen(
-        _p, virt_to_maddr(p) >> PAGE_SHIFT, _l >> PAGE_SHIFT, flags);
+    map_pages_to_xen(_p, mfn_x(virt_to_mfn(p)), _l >> PAGE_SHIFT, flags);
 }
 
 void memguard_guard_range(void *p, unsigned long l)
-- 
2.11.0


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply related	[flat|nested] 76+ messages in thread

* [PATCH v4 08/16] xen/mm: Drop the parameter mfn from populate_pt_range
  2018-02-21 14:02 [PATCH v4 00/16] xen: Convert page_to_mfn and mfn_to_page to use typesafe MFN Julien Grall
                   ` (6 preceding siblings ...)
  2018-02-21 14:02 ` [PATCH v4 07/16] xen/x86: mm: Switch x86/mm.c to use typesafe for virt_to_mfn Julien Grall
@ 2018-02-21 14:02 ` Julien Grall
  2018-02-22 16:35   ` Wei Liu
  2018-02-21 14:02 ` [PATCH v4 09/16] xen/pdx: Introduce helper to convert MFN <-> PDX Julien Grall
                   ` (7 subsequent siblings)
  15 siblings, 1 reply; 76+ messages in thread
From: Julien Grall @ 2018-02-21 14:02 UTC (permalink / raw)
  To: xen-devel
  Cc: Stefano Stabellini, Wei Liu, George Dunlap, Andrew Cooper,
	Ian Jackson, Tim Deegan, Julien Grall, Jan Beulich

The function populate_pt_range is used to populate in advance the
page-table but it will not do the actual mapping. So passing the MFN in
parameter is pointless. Note that the only caller pass 0...

At the same time replace 0 by INVALID_MFN to make clear the MFN is
invalid.

Signed-off-by: Julien Grall <julien.grall@arm.com>

--

Cc: Stefano Stabellini <sstabellini@kernel.org>
Cc: Julien Grall <julien.grall@arm.com>
Cc: Andrew Cooper <andrew.cooper3@citrix.com>
Cc: George Dunlap <George.Dunlap@eu.citrix.com>
Cc: Ian Jackson <ian.jackson@eu.citrix.com>
Cc: Jan Beulich <jbeulich@suse.com>
Cc: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
Cc: Tim Deegan <tim@xen.org>
Cc: Wei Liu <wei.liu2@citrix.com>

The move from 0 to INVALID_MFN will work on Arm. I am not entirely sure
for the x86 version.

    Changes in v4:
        - Patch added.
---
 xen/arch/arm/mm.c    | 5 ++---
 xen/arch/x86/mm.c    | 5 ++---
 xen/common/vmap.c    | 2 +-
 xen/include/xen/mm.h | 3 +--
 4 files changed, 6 insertions(+), 9 deletions(-)

diff --git a/xen/arch/arm/mm.c b/xen/arch/arm/mm.c
index 9b77ab5f33..97dcdd5d50 100644
--- a/xen/arch/arm/mm.c
+++ b/xen/arch/arm/mm.c
@@ -1072,10 +1072,9 @@ int map_pages_to_xen(unsigned long virt,
     return create_xen_entries(INSERT, virt, _mfn(mfn), nr_mfns, flags);
 }
 
-int populate_pt_range(unsigned long virt, unsigned long mfn,
-                      unsigned long nr_mfns)
+int populate_pt_range(unsigned long virt, unsigned long nr_mfns)
 {
-    return create_xen_entries(RESERVE, virt, _mfn(mfn), nr_mfns, 0);
+    return create_xen_entries(RESERVE, virt, INVALID_MFN, nr_mfns, 0);
 }
 
 int destroy_xen_mappings(unsigned long v, unsigned long e)
diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c
index eba1afaa31..ecbfc95ae2 100644
--- a/xen/arch/x86/mm.c
+++ b/xen/arch/x86/mm.c
@@ -5012,10 +5012,9 @@ int map_pages_to_xen(
     return 0;
 }
 
-int populate_pt_range(unsigned long virt, unsigned long mfn,
-                      unsigned long nr_mfns)
+int populate_pt_range(unsigned long virt, unsigned long nr_mfns)
 {
-    return map_pages_to_xen(virt, mfn, nr_mfns, MAP_SMALL_PAGES);
+    return map_pages_to_xen(virt, mfn_x(INVALID_MFN), nr_mfns, MAP_SMALL_PAGES);
 }
 
 /*
diff --git a/xen/common/vmap.c b/xen/common/vmap.c
index 0b23f8fb97..11785ffb0a 100644
--- a/xen/common/vmap.c
+++ b/xen/common/vmap.c
@@ -42,7 +42,7 @@ void __init vm_init_type(enum vmap_region type, void *start, void *end)
     bitmap_fill(vm_bitmap(type), vm_low[type]);
 
     /* Populate page tables for the bitmap if necessary. */
-    populate_pt_range(va, 0, vm_low[type] - nr);
+    populate_pt_range(va, vm_low[type] - nr);
 }
 
 static void *vm_alloc(unsigned int nr, unsigned int align,
diff --git a/xen/include/xen/mm.h b/xen/include/xen/mm.h
index 0e0e5112c6..f2c6738ad2 100644
--- a/xen/include/xen/mm.h
+++ b/xen/include/xen/mm.h
@@ -175,8 +175,7 @@ int destroy_xen_mappings(unsigned long v, unsigned long e);
  * Create only non-leaf page table entries for the
  * page range in Xen virtual address space.
  */
-int populate_pt_range(unsigned long virt, unsigned long mfn,
-                      unsigned long nr_mfns);
+int populate_pt_range(unsigned long virt, unsigned long nr_mfns);
 /* Claim handling */
 unsigned long domain_adjust_tot_pages(struct domain *d, long pages);
 int domain_set_outstanding_pages(struct domain *d, unsigned long pages);
-- 
2.11.0


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply related	[flat|nested] 76+ messages in thread

* [PATCH v4 09/16] xen/pdx: Introduce helper to convert MFN <-> PDX
  2018-02-21 14:02 [PATCH v4 00/16] xen: Convert page_to_mfn and mfn_to_page to use typesafe MFN Julien Grall
                   ` (7 preceding siblings ...)
  2018-02-21 14:02 ` [PATCH v4 08/16] xen/mm: Drop the parameter mfn from populate_pt_range Julien Grall
@ 2018-02-21 14:02 ` Julien Grall
  2018-02-22 16:39   ` Wei Liu
  2018-02-21 14:02 ` [PATCH v4 10/16] xen/mm: Switch map_pages_to_xen to use MFN typesafe Julien Grall
                   ` (6 subsequent siblings)
  15 siblings, 1 reply; 76+ messages in thread
From: Julien Grall @ 2018-02-21 14:02 UTC (permalink / raw)
  To: xen-devel
  Cc: Stefano Stabellini, Wei Liu, George Dunlap, Andrew Cooper,
	Ian Jackson, Tim Deegan, Julien Grall, Jan Beulich

This will avoid use of pfn_to_pdx(mfn_x(mfn)) over the code base.

Signed-off-by: Julien Grall <julien.grall@arm.com>

---

Cc: Andrew Cooper <andrew.cooper3@citrix.com>
Cc: George Dunlap <George.Dunlap@eu.citrix.com>
Cc: Ian Jackson <ian.jackson@eu.citrix.com>
Cc: Jan Beulich <jbeulich@suse.com>
Cc: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
Cc: Stefano Stabellini <sstabellini@kernel.org>
Cc: Tim Deegan <tim@xen.org>
Cc: Wei Liu <wei.liu2@citrix.com>

    Changes in v4:
        - Patch added
---
 xen/include/xen/pdx.h | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/xen/include/xen/pdx.h b/xen/include/xen/pdx.h
index 4c56645c4c..a151aac1a2 100644
--- a/xen/include/xen/pdx.h
+++ b/xen/include/xen/pdx.h
@@ -35,6 +35,9 @@ static inline unsigned long pdx_to_pfn(unsigned long pdx)
            ((pdx << pfn_pdx_hole_shift) & pfn_top_mask);
 }
 
+#define mfn_to_pdx(mfn) pfn_to_pdx(mfn_x(mfn))
+#define pdx_to_mfn(pdx) _mfn(pdx_to_pfn(pdx))
+
 extern void pfn_pdx_hole_setup(unsigned long);
 
 #endif /* HAS_PDX */
-- 
2.11.0


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply related	[flat|nested] 76+ messages in thread

* [PATCH v4 10/16] xen/mm: Switch map_pages_to_xen to use MFN typesafe
  2018-02-21 14:02 [PATCH v4 00/16] xen: Convert page_to_mfn and mfn_to_page to use typesafe MFN Julien Grall
                   ` (8 preceding siblings ...)
  2018-02-21 14:02 ` [PATCH v4 09/16] xen/pdx: Introduce helper to convert MFN <-> PDX Julien Grall
@ 2018-02-21 14:02 ` Julien Grall
  2018-02-23  4:59   ` Tian, Kevin
                     ` (2 more replies)
  2018-02-21 14:02 ` [PATCH v4 11/16] xen/mm: Switch page_alloc.c to typesafe MFN Julien Grall
                   ` (5 subsequent siblings)
  15 siblings, 3 replies; 76+ messages in thread
From: Julien Grall @ 2018-02-21 14:02 UTC (permalink / raw)
  To: xen-devel
  Cc: Kevin Tian, Stefano Stabellini, Wei Liu, George Dunlap,
	Andrew Cooper, Ian Jackson, Tim Deegan, Julien Grall,
	Jan Beulich, Shane Wang, Gang Wei

The current prototype is slightly confusing because it takes a virtual
address and a physical frame (not address!). Switching to MFN will improve
safety and reduce the chance to mistakenly invert the 2 parameters.

Signed-off-by: Julien Grall <julien.grall@arm.com>

---

Cc: Stefano Stabellini <sstabellini@kernel.org>
Cc: Julien Grall <julien.grall@arm.com>
Cc: Andrew Cooper <andrew.cooper3@citrix.com>
Cc: George Dunlap <George.Dunlap@eu.citrix.com>
Cc: Ian Jackson <ian.jackson@eu.citrix.com>
Cc: Jan Beulich <jbeulich@suse.com>
Cc: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
Cc: Tim Deegan <tim@xen.org>
Cc: Wei Liu <wei.liu2@citrix.com>
Cc: Gang Wei <gang.wei@intel.com>
Cc: Shane Wang <shane.wang@intel.com>
Cc: Kevin Tian <kevin.tian@intel.com>

    Changes in v4:
        - Patch added
---
 xen/arch/arm/mm.c                  |  4 ++--
 xen/arch/x86/mm.c                  | 48 +++++++++++++++++++-------------------
 xen/arch/x86/setup.c               | 14 +++++------
 xen/arch/x86/smpboot.c             |  2 +-
 xen/arch/x86/tboot.c               | 11 +++++----
 xen/arch/x86/x86_64/mm.c           | 27 ++++++++++++---------
 xen/arch/x86/x86_64/mmconfig_64.c  |  4 ++--
 xen/common/efi/boot.c              |  2 +-
 xen/common/vmap.c                  | 10 +++++---
 xen/drivers/acpi/apei/erst.c       |  2 +-
 xen/drivers/acpi/apei/hest.c       |  2 +-
 xen/drivers/passthrough/vtd/dmar.c |  2 +-
 xen/include/asm-arm/mm.h           |  2 +-
 xen/include/xen/mm.h               |  2 +-
 14 files changed, 71 insertions(+), 61 deletions(-)

diff --git a/xen/arch/arm/mm.c b/xen/arch/arm/mm.c
index 97dcdd5d50..f17907ace8 100644
--- a/xen/arch/arm/mm.c
+++ b/xen/arch/arm/mm.c
@@ -1065,11 +1065,11 @@ out:
 }
 
 int map_pages_to_xen(unsigned long virt,
-                     unsigned long mfn,
+                     mfn_t mfn,
                      unsigned long nr_mfns,
                      unsigned int flags)
 {
-    return create_xen_entries(INSERT, virt, _mfn(mfn), nr_mfns, flags);
+    return create_xen_entries(INSERT, virt, mfn, nr_mfns, flags);
 }
 
 int populate_pt_range(unsigned long virt, unsigned long nr_mfns)
diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c
index ecbfc95ae2..d33b6bfa9d 100644
--- a/xen/arch/x86/mm.c
+++ b/xen/arch/x86/mm.c
@@ -213,7 +213,7 @@ static void __init init_frametable_chunk(void *start, void *end)
         while ( step && s + (step << PAGE_SHIFT) > e + (4 << PAGE_SHIFT) )
             step >>= PAGETABLE_ORDER;
         mfn = alloc_boot_pages(step, step);
-        map_pages_to_xen(s, mfn_x(mfn), step, PAGE_HYPERVISOR);
+        map_pages_to_xen(s, mfn, step, PAGE_HYPERVISOR);
     }
 
     memset(start, 0, end - start);
@@ -793,12 +793,12 @@ static int update_xen_mappings(unsigned long mfn, unsigned int cacheattr)
         XEN_VIRT_START + ((mfn - PFN_DOWN(xen_phys_start)) << PAGE_SHIFT);
 
     if ( unlikely(alias) && cacheattr )
-        err = map_pages_to_xen(xen_va, mfn, 1, 0);
+        err = map_pages_to_xen(xen_va, _mfn(mfn), 1, 0);
     if ( !err )
-        err = map_pages_to_xen((unsigned long)mfn_to_virt(mfn), mfn, 1,
+        err = map_pages_to_xen((unsigned long)mfn_to_virt(mfn), _mfn(mfn), 1,
                      PAGE_HYPERVISOR | cacheattr_to_pte_flags(cacheattr));
     if ( unlikely(alias) && !cacheattr && !err )
-        err = map_pages_to_xen(xen_va, mfn, 1, PAGE_HYPERVISOR);
+        err = map_pages_to_xen(xen_va, _mfn(mfn), 1, PAGE_HYPERVISOR);
     return err;
 }
 
@@ -4655,7 +4655,7 @@ l1_pgentry_t *virt_to_xen_l1e(unsigned long v)
 
 int map_pages_to_xen(
     unsigned long virt,
-    unsigned long mfn,
+    mfn_t mfn,
     unsigned long nr_mfns,
     unsigned int flags)
 {
@@ -4687,13 +4687,13 @@ int map_pages_to_xen(
         ol3e = *pl3e;
 
         if ( cpu_has_page1gb &&
-             !(((virt >> PAGE_SHIFT) | mfn) &
+             !(((virt >> PAGE_SHIFT) | mfn_x(mfn)) &
                ((1UL << (L3_PAGETABLE_SHIFT - PAGE_SHIFT)) - 1)) &&
              nr_mfns >= (1UL << (L3_PAGETABLE_SHIFT - PAGE_SHIFT)) &&
              !(flags & (_PAGE_PAT | MAP_SMALL_PAGES)) )
         {
             /* 1GB-page mapping. */
-            l3e_write_atomic(pl3e, l3e_from_pfn(mfn, l1f_to_lNf(flags)));
+            l3e_write_atomic(pl3e, l3e_from_mfn(mfn, l1f_to_lNf(flags)));
 
             if ( (l3e_get_flags(ol3e) & _PAGE_PRESENT) )
             {
@@ -4737,7 +4737,7 @@ int map_pages_to_xen(
             }
 
             virt    += 1UL << L3_PAGETABLE_SHIFT;
-            mfn     += 1UL << (L3_PAGETABLE_SHIFT - PAGE_SHIFT);
+            mfn     = mfn_add(mfn, 1UL << (L3_PAGETABLE_SHIFT - PAGE_SHIFT));
             nr_mfns -= 1UL << (L3_PAGETABLE_SHIFT - PAGE_SHIFT);
             continue;
         }
@@ -4752,17 +4752,17 @@ int map_pages_to_xen(
             if ( ((l3e_get_pfn(ol3e) & ~(L2_PAGETABLE_ENTRIES *
                                          L1_PAGETABLE_ENTRIES - 1)) +
                   (l2_table_offset(virt) << PAGETABLE_ORDER) +
-                  l1_table_offset(virt) == mfn) &&
+                  l1_table_offset(virt) == mfn_x(mfn)) &&
                  ((lNf_to_l1f(l3e_get_flags(ol3e)) ^ flags) &
                   ~(_PAGE_ACCESSED|_PAGE_DIRTY)) == 0 )
             {
                 /* We can skip to end of L3 superpage if we got a match. */
                 i = (1u << (L3_PAGETABLE_SHIFT - PAGE_SHIFT)) -
-                    (mfn & ((1 << (L3_PAGETABLE_SHIFT - PAGE_SHIFT)) - 1));
+                    (mfn_x(mfn) & ((1 << (L3_PAGETABLE_SHIFT - PAGE_SHIFT)) - 1));
                 if ( i > nr_mfns )
                     i = nr_mfns;
                 virt    += i << PAGE_SHIFT;
-                mfn     += i;
+                mfn     = mfn_add(mfn, i);
                 nr_mfns -= i;
                 continue;
             }
@@ -4800,14 +4800,14 @@ int map_pages_to_xen(
         if ( !pl2e )
             return -ENOMEM;
 
-        if ( ((((virt >> PAGE_SHIFT) | mfn) &
+        if ( ((((virt >> PAGE_SHIFT) | mfn_x(mfn)) &
                ((1u << PAGETABLE_ORDER) - 1)) == 0) &&
              (nr_mfns >= (1u << PAGETABLE_ORDER)) &&
              !(flags & (_PAGE_PAT|MAP_SMALL_PAGES)) )
         {
             /* Super-page mapping. */
             ol2e = *pl2e;
-            l2e_write_atomic(pl2e, l2e_from_pfn(mfn, l1f_to_lNf(flags)));
+            l2e_write_atomic(pl2e, l2e_from_mfn(mfn, l1f_to_lNf(flags)));
 
             if ( (l2e_get_flags(ol2e) & _PAGE_PRESENT) )
             {
@@ -4830,7 +4830,7 @@ int map_pages_to_xen(
             }
 
             virt    += 1UL << L2_PAGETABLE_SHIFT;
-            mfn     += 1UL << PAGETABLE_ORDER;
+            mfn     = mfn_add(mfn, 1UL << PAGETABLE_ORDER);
             nr_mfns -= 1UL << PAGETABLE_ORDER;
         }
         else
@@ -4849,17 +4849,17 @@ int map_pages_to_xen(
 
                 /* Skip this PTE if there is no change. */
                 if ( (((l2e_get_pfn(*pl2e) & ~(L1_PAGETABLE_ENTRIES - 1)) +
-                       l1_table_offset(virt)) == mfn) &&
+                       l1_table_offset(virt)) == mfn_x(mfn)) &&
                      (((lNf_to_l1f(l2e_get_flags(*pl2e)) ^ flags) &
                        ~(_PAGE_ACCESSED|_PAGE_DIRTY)) == 0) )
                 {
                     /* We can skip to end of L2 superpage if we got a match. */
                     i = (1u << (L2_PAGETABLE_SHIFT - PAGE_SHIFT)) -
-                        (mfn & ((1u << (L2_PAGETABLE_SHIFT - PAGE_SHIFT)) - 1));
+                        (mfn_x(mfn) & ((1u << (L2_PAGETABLE_SHIFT - PAGE_SHIFT)) - 1));
                     if ( i > nr_mfns )
                         i = nr_mfns;
                     virt    += i << L1_PAGETABLE_SHIFT;
-                    mfn     += i;
+                    mfn     = mfn_add(mfn, i);
                     nr_mfns -= i;
                     goto check_l3;
                 }
@@ -4894,7 +4894,7 @@ int map_pages_to_xen(
 
             pl1e  = l2e_to_l1e(*pl2e) + l1_table_offset(virt);
             ol1e  = *pl1e;
-            l1e_write_atomic(pl1e, l1e_from_pfn(mfn, flags));
+            l1e_write_atomic(pl1e, l1e_from_mfn(mfn, flags));
             if ( (l1e_get_flags(ol1e) & _PAGE_PRESENT) )
             {
                 unsigned int flush_flags = FLUSH_TLB | FLUSH_ORDER(0);
@@ -4904,12 +4904,12 @@ int map_pages_to_xen(
             }
 
             virt    += 1UL << L1_PAGETABLE_SHIFT;
-            mfn     += 1UL;
+            mfn     = mfn_add(mfn, 1UL);
             nr_mfns -= 1UL;
 
             if ( (flags == PAGE_HYPERVISOR) &&
                  ((nr_mfns == 0) ||
-                  ((((virt >> PAGE_SHIFT) | mfn) &
+                  ((((virt >> PAGE_SHIFT) | mfn_x(mfn)) &
                     ((1u << PAGETABLE_ORDER) - 1)) == 0)) )
             {
                 unsigned long base_mfn;
@@ -4962,7 +4962,7 @@ int map_pages_to_xen(
         if ( cpu_has_page1gb &&
              (flags == PAGE_HYPERVISOR) &&
              ((nr_mfns == 0) ||
-              !(((virt >> PAGE_SHIFT) | mfn) &
+              !(((virt >> PAGE_SHIFT) | mfn_x(mfn)) &
                 ((1UL << (L3_PAGETABLE_SHIFT - PAGE_SHIFT)) - 1))) )
         {
             unsigned long base_mfn;
@@ -5014,7 +5014,7 @@ int map_pages_to_xen(
 
 int populate_pt_range(unsigned long virt, unsigned long nr_mfns)
 {
-    return map_pages_to_xen(virt, mfn_x(INVALID_MFN), nr_mfns, MAP_SMALL_PAGES);
+    return map_pages_to_xen(virt, INVALID_MFN, nr_mfns, MAP_SMALL_PAGES);
 }
 
 /*
@@ -5275,7 +5275,7 @@ void __set_fixmap(
     enum fixed_addresses idx, unsigned long mfn, unsigned long flags)
 {
     BUG_ON(idx >= __end_of_fixed_addresses);
-    map_pages_to_xen(__fix_to_virt(idx), mfn, 1, flags);
+    map_pages_to_xen(__fix_to_virt(idx), _mfn(mfn), 1, flags);
 }
 
 void *__init arch_vmap_virt_end(void)
@@ -5546,7 +5546,7 @@ static void __memguard_change_range(void *p, unsigned long l, int guard)
     if ( guard )
         flags &= ~_PAGE_PRESENT;
 
-    map_pages_to_xen(_p, mfn_x(virt_to_mfn(p)), _l >> PAGE_SHIFT, flags);
+    map_pages_to_xen(_p, virt_to_mfn(p), _l >> PAGE_SHIFT, flags);
 }
 
 void memguard_guard_range(void *p, unsigned long l)
diff --git a/xen/arch/x86/setup.c b/xen/arch/x86/setup.c
index ac530ece2c..fabd689f6e 100644
--- a/xen/arch/x86/setup.c
+++ b/xen/arch/x86/setup.c
@@ -354,7 +354,7 @@ void *__init bootstrap_map(const module_t *mod)
     if ( end - start > BOOTSTRAP_MAP_LIMIT - map_cur )
         return NULL;
 
-    map_pages_to_xen(map_cur, start >> PAGE_SHIFT,
+    map_pages_to_xen(map_cur, maddr_to_mfn(start),
                      (end - start) >> PAGE_SHIFT, PAGE_HYPERVISOR);
     map_cur += end - start;
     return ret;
@@ -977,7 +977,7 @@ void __init noreturn __start_xen(unsigned long mbi_p)
         {
             end = min(e, limit);
             set_pdx_range(s >> PAGE_SHIFT, end >> PAGE_SHIFT);
-            map_pages_to_xen((unsigned long)__va(s), s >> PAGE_SHIFT,
+            map_pages_to_xen((unsigned long)__va(s), maddr_to_mfn(s),
                              (end - s) >> PAGE_SHIFT, PAGE_HYPERVISOR);
         }
 
@@ -1292,7 +1292,7 @@ void __init noreturn __start_xen(unsigned long mbi_p)
 
             if ( map_e < end )
             {
-                map_pages_to_xen((unsigned long)__va(map_e), PFN_DOWN(map_e),
+                map_pages_to_xen((unsigned long)__va(map_e), maddr_to_mfn(map_e),
                                  PFN_DOWN(end - map_e), PAGE_HYPERVISOR);
                 init_boot_pages(map_e, end);
                 map_e = end;
@@ -1302,12 +1302,12 @@ void __init noreturn __start_xen(unsigned long mbi_p)
         {
             /* This range must not be passed to the boot allocator and
              * must also not be mapped with _PAGE_GLOBAL. */
-            map_pages_to_xen((unsigned long)__va(map_e), PFN_DOWN(map_e),
+            map_pages_to_xen((unsigned long)__va(map_e), maddr_to_mfn(map_e),
                              PFN_DOWN(e - map_e), __PAGE_HYPERVISOR_RW);
         }
         if ( s < map_s )
         {
-            map_pages_to_xen((unsigned long)__va(s), s >> PAGE_SHIFT,
+            map_pages_to_xen((unsigned long)__va(s), maddr_to_mfn(s),
                              (map_s - s) >> PAGE_SHIFT, PAGE_HYPERVISOR);
             init_boot_pages(s, map_s);
         }
@@ -1318,7 +1318,7 @@ void __init noreturn __start_xen(unsigned long mbi_p)
         set_pdx_range(mod[i].mod_start,
                       mod[i].mod_start + PFN_UP(mod[i].mod_end));
         map_pages_to_xen((unsigned long)mfn_to_virt(mod[i].mod_start),
-                         mod[i].mod_start,
+                         _mfn(mod[i].mod_start),
                          PFN_UP(mod[i].mod_end), PAGE_HYPERVISOR);
     }
 
@@ -1331,7 +1331,7 @@ void __init noreturn __start_xen(unsigned long mbi_p)
 
         if ( e > s ) 
             map_pages_to_xen((unsigned long)__va(kexec_crash_area.start),
-                             s, e - s, PAGE_HYPERVISOR);
+                             _mfn(s), e - s, PAGE_HYPERVISOR);
     }
 #endif
 
diff --git a/xen/arch/x86/smpboot.c b/xen/arch/x86/smpboot.c
index db264bd7bf..52ad43e0ed 100644
--- a/xen/arch/x86/smpboot.c
+++ b/xen/arch/x86/smpboot.c
@@ -626,7 +626,7 @@ unsigned long alloc_stub_page(unsigned int cpu, unsigned long *mfn)
     BUILD_BUG_ON(NR_CPUS * PAGE_SIZE > (1u << L3_PAGETABLE_SHIFT));
 
     stub_va = XEN_VIRT_END - (cpu + 1) * PAGE_SIZE;
-    if ( map_pages_to_xen(stub_va, mfn_x(page_to_mfn(pg)), 1,
+    if ( map_pages_to_xen(stub_va, page_to_mfn(pg), 1,
                           PAGE_HYPERVISOR_RX | MAP_SMALL_PAGES) )
     {
         if ( !*mfn )
diff --git a/xen/arch/x86/tboot.c b/xen/arch/x86/tboot.c
index d36bf33407..71e757c553 100644
--- a/xen/arch/x86/tboot.c
+++ b/xen/arch/x86/tboot.c
@@ -336,22 +336,23 @@ static void tboot_gen_frametable_integrity(const uint8_t key[TB_KEY_SIZE],
 
 void tboot_shutdown(uint32_t shutdown_type)
 {
-    uint32_t map_base, map_size;
+    mfn_t map_base;
+    uint32_t map_size;
     int err;
 
     g_tboot_shared->shutdown_type = shutdown_type;
 
     /* Create identity map for tboot shutdown code. */
     /* do before S3 integrity because mapping tboot may change xenheap */
-    map_base = PFN_DOWN(g_tboot_shared->tboot_base);
+    map_base = maddr_to_mfn(g_tboot_shared->tboot_base);
     map_size = PFN_UP(g_tboot_shared->tboot_size);
 
-    err = map_pages_to_xen(map_base << PAGE_SHIFT, map_base, map_size,
+    err = map_pages_to_xen(mfn_to_maddr(map_base), map_base, map_size,
                            __PAGE_HYPERVISOR);
     if ( err != 0 )
     {
-        printk("error (%#x) mapping tboot pages (mfns) @ %#x, %#x\n", err,
-               map_base, map_size);
+        printk("error (%#x) mapping tboot pages (mfns) @ %"PRI_mfn", %#x\n",
+               err, mfn_x(map_base), map_size);
         return;
     }
 
diff --git a/xen/arch/x86/x86_64/mm.c b/xen/arch/x86/x86_64/mm.c
index 9b37da6698..cfbc8ecf16 100644
--- a/xen/arch/x86/x86_64/mm.c
+++ b/xen/arch/x86/x86_64/mm.c
@@ -40,6 +40,10 @@ asm(".file \"" __FILE__ "\"");
 #include <asm/mem_sharing.h>
 #include <public/memory.h>
 
+/* Override macros from asm/page.h to make them work with mfn_t */
+#undef page_to_mfn
+#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
+
 unsigned int __read_mostly m2p_compat_vstart = __HYPERVISOR_COMPAT_VIRT_START;
 
 l2_pgentry_t *compat_idle_pg_table_l2;
@@ -111,14 +115,14 @@ static int hotadd_mem_valid(unsigned long pfn, struct mem_hotadd_info *info)
     return (pfn < info->epfn && pfn >= info->spfn);
 }
 
-static unsigned long alloc_hotadd_mfn(struct mem_hotadd_info *info)
+static mfn_t alloc_hotadd_mfn(struct mem_hotadd_info *info)
 {
-    unsigned mfn;
+    mfn_t mfn;
 
     ASSERT((info->cur + ( 1UL << PAGETABLE_ORDER) < info->epfn) &&
             info->cur >= info->spfn);
 
-    mfn = info->cur;
+    mfn = _mfn(info->cur);
     info->cur += (1UL << PAGETABLE_ORDER);
     return mfn;
 }
@@ -317,7 +321,8 @@ static void destroy_m2p_mapping(struct mem_hotadd_info *info)
  */
 static int setup_compat_m2p_table(struct mem_hotadd_info *info)
 {
-    unsigned long i, va, smap, emap, rwva, epfn = info->epfn, mfn;
+    unsigned long i, va, smap, emap, rwva, epfn = info->epfn;
+    mfn_t mfn;
     unsigned int n;
     l3_pgentry_t *l3_ro_mpt = NULL;
     l2_pgentry_t *l2_ro_mpt = NULL;
@@ -378,7 +383,7 @@ static int setup_compat_m2p_table(struct mem_hotadd_info *info)
         memset((void *)rwva, 0xFF, 1UL << L2_PAGETABLE_SHIFT);
         /* NB. Cannot be GLOBAL as the ptes get copied into per-VM space. */
         l2e_write(&l2_ro_mpt[l2_table_offset(va)],
-                  l2e_from_pfn(mfn, _PAGE_PSE|_PAGE_PRESENT));
+                  l2e_from_mfn(mfn, _PAGE_PSE|_PAGE_PRESENT));
     }
 #undef CNT
 #undef MFN
@@ -438,7 +443,7 @@ static int setup_m2p_table(struct mem_hotadd_info *info)
                 break;
         if ( n < CNT )
         {
-            unsigned long mfn = alloc_hotadd_mfn(info);
+            mfn_t mfn = alloc_hotadd_mfn(info);
 
             ret = map_pages_to_xen(
                         RDWR_MPT_VIRT_START + i * sizeof(unsigned long),
@@ -473,7 +478,7 @@ static int setup_m2p_table(struct mem_hotadd_info *info)
             }
 
             /* NB. Cannot be GLOBAL: guest user mode should not see it. */
-            l2e_write(l2_ro_mpt, l2e_from_pfn(mfn,
+            l2e_write(l2_ro_mpt, l2e_from_mfn(mfn,
                    /*_PAGE_GLOBAL|*/_PAGE_PSE|_PAGE_USER|_PAGE_PRESENT));
         }
         if ( !((unsigned long)l2_ro_mpt & ~PAGE_MASK) )
@@ -692,7 +697,7 @@ void __init zap_low_mappings(void)
     flush_local(FLUSH_TLB_GLOBAL);
 
     /* Replace with mapping of the boot trampoline only. */
-    map_pages_to_xen(trampoline_phys, trampoline_phys >> PAGE_SHIFT,
+    map_pages_to_xen(trampoline_phys, maddr_to_mfn(trampoline_phys),
                      PFN_UP(trampoline_end - trampoline_start),
                      __PAGE_HYPERVISOR);
 }
@@ -769,7 +774,7 @@ static int setup_frametable_chunk(void *start, void *end,
 {
     unsigned long s = (unsigned long)start;
     unsigned long e = (unsigned long)end;
-    unsigned long mfn;
+    mfn_t mfn;
     int err;
 
     ASSERT(!(s & ((1 << L2_PAGETABLE_SHIFT) - 1)));
@@ -1368,7 +1373,7 @@ int memory_add(unsigned long spfn, unsigned long epfn, unsigned int pxm)
     i = virt_to_mfn(HYPERVISOR_VIRT_END - 1) + 1;
     if ( spfn < i )
     {
-        ret = map_pages_to_xen((unsigned long)mfn_to_virt(spfn), spfn,
+        ret = map_pages_to_xen((unsigned long)mfn_to_virt(spfn), _mfn(spfn),
                                min(epfn, i) - spfn, PAGE_HYPERVISOR);
         if ( ret )
             goto destroy_directmap;
@@ -1377,7 +1382,7 @@ int memory_add(unsigned long spfn, unsigned long epfn, unsigned int pxm)
     {
         if ( i < spfn )
             i = spfn;
-        ret = map_pages_to_xen((unsigned long)mfn_to_virt(i), i,
+        ret = map_pages_to_xen((unsigned long)mfn_to_virt(i), _mfn(i),
                                epfn - i, __PAGE_HYPERVISOR_RW);
         if ( ret )
             goto destroy_directmap;
diff --git a/xen/arch/x86/x86_64/mmconfig_64.c b/xen/arch/x86/x86_64/mmconfig_64.c
index 958b6cf2f4..c68004fc4d 100644
--- a/xen/arch/x86/x86_64/mmconfig_64.c
+++ b/xen/arch/x86/x86_64/mmconfig_64.c
@@ -125,8 +125,8 @@ static void __iomem *mcfg_ioremap(const struct acpi_mcfg_allocation *cfg,
         return NULL;
 
     if (map_pages_to_xen(virt,
-                         (cfg->address >> PAGE_SHIFT) +
-                         (cfg->start_bus_number << (20 - PAGE_SHIFT)),
+                         mfn_add(maddr_to_mfn(cfg->address),
+                                 (cfg->start_bus_number << (20 - PAGE_SHIFT))),
                          size >> PAGE_SHIFT, prot))
         return NULL;
 
diff --git a/xen/common/efi/boot.c b/xen/common/efi/boot.c
index 469bf980cc..64d12685d3 100644
--- a/xen/common/efi/boot.c
+++ b/xen/common/efi/boot.c
@@ -1464,7 +1464,7 @@ void __init efi_init_memory(void)
             if ( (unsigned long)mfn_to_virt(emfn - 1) >= HYPERVISOR_VIRT_END )
                 prot &= ~_PAGE_GLOBAL;
             if ( map_pages_to_xen((unsigned long)mfn_to_virt(smfn),
-                                  smfn, emfn - smfn, prot) == 0 )
+                                  _mfn(smfn), emfn - smfn, prot) == 0 )
                 desc->VirtualStart =
                     (unsigned long)maddr_to_virt(desc->PhysicalStart);
             else
diff --git a/xen/common/vmap.c b/xen/common/vmap.c
index 11785ffb0a..1f50c91789 100644
--- a/xen/common/vmap.c
+++ b/xen/common/vmap.c
@@ -9,6 +9,10 @@
 #include <xen/vmap.h>
 #include <asm/page.h>
 
+/* Override macros from asm/page.h to make them work with mfn_t */
+#undef page_to_mfn
+#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
+
 static DEFINE_SPINLOCK(vm_lock);
 static void *__read_mostly vm_base[VMAP_REGION_NR];
 #define vm_bitmap(x) ((unsigned long *)vm_base[x])
@@ -208,7 +212,7 @@ void *__vmap(const mfn_t *mfn, unsigned int granularity,
 
     for ( ; va && nr--; ++mfn, cur += PAGE_SIZE * granularity )
     {
-        if ( map_pages_to_xen(cur, mfn_x(*mfn), granularity, flags) )
+        if ( map_pages_to_xen(cur, *mfn, granularity, flags) )
         {
             vunmap(va);
             va = NULL;
@@ -234,7 +238,7 @@ void vunmap(const void *va)
 #ifndef _PAGE_NONE
     destroy_xen_mappings(addr, addr + PAGE_SIZE * pages);
 #else /* Avoid tearing down intermediate page tables. */
-    map_pages_to_xen(addr, 0, pages, _PAGE_NONE);
+    map_pages_to_xen(addr, _mfn(0), pages, _PAGE_NONE);
 #endif
     vm_free(va);
 }
@@ -258,7 +262,7 @@ static void *vmalloc_type(size_t size, enum vmap_region type)
         pg = alloc_domheap_page(NULL, 0);
         if ( pg == NULL )
             goto error;
-        mfn[i] = _mfn(page_to_mfn(pg));
+        mfn[i] = page_to_mfn(pg);
     }
 
     va = __vmap(mfn, 1, pages, 1, PAGE_HYPERVISOR, type);
diff --git a/xen/drivers/acpi/apei/erst.c b/xen/drivers/acpi/apei/erst.c
index 14acf5d773..7fc4de5de9 100644
--- a/xen/drivers/acpi/apei/erst.c
+++ b/xen/drivers/acpi/apei/erst.c
@@ -799,7 +799,7 @@ int __init erst_init(void)
 		printk(KERN_WARNING "Failed to get ERST table: %s\n", msg);
 		return -EINVAL;
 	}
-	map_pages_to_xen((unsigned long)__va(erst_addr), PFN_DOWN(erst_addr),
+	map_pages_to_xen((unsigned long)__va(erst_addr), maddr_to_mfn(erst_addr),
 			 PFN_UP(erst_addr + erst_len) - PFN_DOWN(erst_addr),
 			 PAGE_HYPERVISOR);
 	erst_tab = __va(erst_addr);
diff --git a/xen/drivers/acpi/apei/hest.c b/xen/drivers/acpi/apei/hest.c
index f74e7c2a06..70734ab0e2 100644
--- a/xen/drivers/acpi/apei/hest.c
+++ b/xen/drivers/acpi/apei/hest.c
@@ -184,7 +184,7 @@ void __init acpi_hest_init(void)
 		       acpi_format_exception(status));
 		goto err;
 	}
-	map_pages_to_xen((unsigned long)__va(hest_addr), PFN_DOWN(hest_addr),
+	map_pages_to_xen((unsigned long)__va(hest_addr), maddr_to_mfn(hest_addr),
 			 PFN_UP(hest_addr + hest_len) - PFN_DOWN(hest_addr),
 			 PAGE_HYPERVISOR);
 	hest_tab = __va(hest_addr);
diff --git a/xen/drivers/passthrough/vtd/dmar.c b/xen/drivers/passthrough/vtd/dmar.c
index d713a8ca5d..46decd4eb1 100644
--- a/xen/drivers/passthrough/vtd/dmar.c
+++ b/xen/drivers/passthrough/vtd/dmar.c
@@ -1008,7 +1008,7 @@ int __init acpi_dmar_init(void)
     if ( ACPI_SUCCESS(acpi_get_table_phys(ACPI_SIG_DMAR, 0,
                                           &dmar_addr, &dmar_len)) )
     {
-        map_pages_to_xen((unsigned long)__va(dmar_addr), PFN_DOWN(dmar_addr),
+        map_pages_to_xen((unsigned long)__va(dmar_addr), maddr_to_mfn(dmar_addr),
                          PFN_UP(dmar_addr + dmar_len) - PFN_DOWN(dmar_addr),
                          PAGE_HYPERVISOR);
         dmar_table = __va(dmar_addr);
diff --git a/xen/include/asm-arm/mm.h b/xen/include/asm-arm/mm.h
index 7678e29c15..023e2eb213 100644
--- a/xen/include/asm-arm/mm.h
+++ b/xen/include/asm-arm/mm.h
@@ -138,7 +138,7 @@ extern vaddr_t xenheap_virt_start;
 #endif
 
 #ifdef CONFIG_ARM_32
-#define is_xen_heap_page(page) is_xen_heap_mfn(page_to_mfn(page))
+#define is_xen_heap_page(page) is_xen_heap_mfn(__page_to_mfn(page))
 #define is_xen_heap_mfn(mfn) ({                                 \
     unsigned long mfn_ = (mfn);                                 \
     (mfn_ >= mfn_x(xenheap_mfn_start) &&                        \
diff --git a/xen/include/xen/mm.h b/xen/include/xen/mm.h
index f2c6738ad2..caad06e753 100644
--- a/xen/include/xen/mm.h
+++ b/xen/include/xen/mm.h
@@ -165,7 +165,7 @@ bool scrub_free_pages(void);
 /* Map machine page range in Xen virtual address space. */
 int map_pages_to_xen(
     unsigned long virt,
-    unsigned long mfn,
+    mfn_t mfn,
     unsigned long nr_mfns,
     unsigned int flags);
 /* Alter the permissions of a range of Xen virtual address space. */
-- 
2.11.0


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply related	[flat|nested] 76+ messages in thread

* [PATCH v4 11/16] xen/mm: Switch page_alloc.c to typesafe MFN
  2018-02-21 14:02 [PATCH v4 00/16] xen: Convert page_to_mfn and mfn_to_page to use typesafe MFN Julien Grall
                   ` (9 preceding siblings ...)
  2018-02-21 14:02 ` [PATCH v4 10/16] xen/mm: Switch map_pages_to_xen to use MFN typesafe Julien Grall
@ 2018-02-21 14:02 ` Julien Grall
  2018-02-23 17:21   ` Wei Liu
  2018-03-02 15:18   ` Jan Beulich
  2018-02-21 14:02 ` [PATCH v4 12/16] xen/mm: Switch common/memory.c to use " Julien Grall
                   ` (4 subsequent siblings)
  15 siblings, 2 replies; 76+ messages in thread
From: Julien Grall @ 2018-02-21 14:02 UTC (permalink / raw)
  To: xen-devel
  Cc: Stefano Stabellini, Wei Liu, George Dunlap, Andrew Cooper,
	Ian Jackson, Tim Deegan, Julien Grall, Jan Beulich

No functional change intended.

Signed-off-by: Julien Grall <julien.grall@arm.com>

---
Cc: Andrew Cooper <andrew.cooper3@citrix.com>
Cc: George Dunlap <George.Dunlap@eu.citrix.com>
Cc: Ian Jackson <ian.jackson@eu.citrix.com>
Cc: Jan Beulich <jbeulich@suse.com>
Cc: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
Cc: Stefano Stabellini <sstabellini@kernel.org>
Cc: Tim Deegan <tim@xen.org>
Cc: Wei Liu <wei.liu2@citrix.com>
Cc: Julien Grall <julien.grall@arm.com>

    Changes in v4:
        - Patch added
---
 xen/common/page_alloc.c    | 64 ++++++++++++++++++++++++++--------------------
 xen/include/asm-arm/numa.h |  8 +++---
 2 files changed, 41 insertions(+), 31 deletions(-)

diff --git a/xen/common/page_alloc.c b/xen/common/page_alloc.c
index 4de8988bea..b0db41feea 100644
--- a/xen/common/page_alloc.c
+++ b/xen/common/page_alloc.c
@@ -151,6 +151,12 @@
 #define p2m_pod_offline_or_broken_replace(pg) BUG_ON(pg != NULL)
 #endif
 
+/* Override macros from asm/page.h to make them work with mfn_t */
+#undef page_to_mfn
+#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
+#undef mfn_to_page
+#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
+
 /*
  * Comma-separated list of hexadecimal page numbers containing bad bytes.
  * e.g. 'badpage=0x3f45,0x8a321'.
@@ -197,7 +203,7 @@ PAGE_LIST_HEAD(page_broken_list);
  * first_valid_mfn is exported because it is use in ARM specific NUMA
  * helpers. See comment in asm-arm/numa.h.
  */
-unsigned long first_valid_mfn = ~0UL;
+mfn_t first_valid_mfn = INVALID_MFN_INITIALIZER;
 
 static struct bootmem_region {
     unsigned long s, e; /* MFNs @s through @e-1 inclusive are free */
@@ -283,7 +289,7 @@ void __init init_boot_pages(paddr_t ps, paddr_t pe)
     if ( pe <= ps )
         return;
 
-    first_valid_mfn = min_t(unsigned long, ps >> PAGE_SHIFT, first_valid_mfn);
+    first_valid_mfn = mfn_min(maddr_to_mfn(ps), first_valid_mfn);
 
     bootmem_region_add(ps >> PAGE_SHIFT, pe >> PAGE_SHIFT);
 
@@ -397,7 +403,7 @@ mfn_t __init alloc_boot_pages(unsigned long nr_pfns, unsigned long pfn_align)
 
 #define bits_to_zone(b) (((b) < (PAGE_SHIFT + 1)) ? 1 : ((b) - PAGE_SHIFT))
 #define page_to_zone(pg) (is_xen_heap_page(pg) ? MEMZONE_XEN :  \
-                          (flsl(page_to_mfn(pg)) ? : 1))
+                          (flsl(mfn_x(page_to_mfn(pg))) ? : 1))
 
 typedef struct page_list_head heap_by_zone_and_order_t[NR_ZONES][MAX_ORDER+1];
 static heap_by_zone_and_order_t *_heap[MAX_NUMNODES];
@@ -729,7 +735,7 @@ static void page_list_add_scrub(struct page_info *pg, unsigned int node,
 static void poison_one_page(struct page_info *pg)
 {
 #ifdef CONFIG_SCRUB_DEBUG
-    mfn_t mfn = _mfn(page_to_mfn(pg));
+    mfn_t mfn = page_to_mfn(pg);
     uint64_t *ptr;
 
     if ( !scrub_debug )
@@ -744,7 +750,7 @@ static void poison_one_page(struct page_info *pg)
 static void check_one_page(struct page_info *pg)
 {
 #ifdef CONFIG_SCRUB_DEBUG
-    mfn_t mfn = _mfn(page_to_mfn(pg));
+    mfn_t mfn = page_to_mfn(pg);
     const uint64_t *ptr;
     unsigned int i;
 
@@ -992,7 +998,8 @@ static struct page_info *alloc_heap_pages(
         /* Ensure cache and RAM are consistent for platforms where the
          * guest can control its own visibility of/through the cache.
          */
-        flush_page_to_ram(page_to_mfn(&pg[i]), !(memflags & MEMF_no_icache_flush));
+        flush_page_to_ram(mfn_x(page_to_mfn(&pg[i])),
+                          !(memflags & MEMF_no_icache_flush));
     }
 
     spin_unlock(&heap_lock);
@@ -1344,7 +1351,8 @@ bool scrub_free_pages(void)
 static void free_heap_pages(
     struct page_info *pg, unsigned int order, bool need_scrub)
 {
-    unsigned long mask, mfn = page_to_mfn(pg);
+    unsigned long mask;
+    mfn_t mfn = page_to_mfn(pg);
     unsigned int i, node = phys_to_nid(page_to_maddr(pg)), tainted = 0;
     unsigned int zone = page_to_zone(pg);
 
@@ -1381,7 +1389,7 @@ static void free_heap_pages(
 
         /* This page is not a guest frame any more. */
         page_set_owner(&pg[i], NULL); /* set_gpfn_from_mfn snoops pg owner */
-        set_gpfn_from_mfn(mfn + i, INVALID_M2P_ENTRY);
+        set_gpfn_from_mfn(mfn_x(mfn) + i, INVALID_M2P_ENTRY);
 
         if ( need_scrub )
         {
@@ -1409,12 +1417,12 @@ static void free_heap_pages(
     {
         mask = 1UL << order;
 
-        if ( (page_to_mfn(pg) & mask) )
+        if ( (mfn_x(page_to_mfn(pg)) & mask) )
         {
             struct page_info *predecessor = pg - mask;
 
             /* Merge with predecessor block? */
-            if ( !mfn_valid(_mfn(page_to_mfn(predecessor))) ||
+            if ( !mfn_valid(page_to_mfn(predecessor)) ||
                  !page_state_is(predecessor, free) ||
                  (PFN_ORDER(predecessor) != order) ||
                  (phys_to_nid(page_to_maddr(predecessor)) != node) )
@@ -1437,7 +1445,7 @@ static void free_heap_pages(
             struct page_info *successor = pg + mask;
 
             /* Merge with successor block? */
-            if ( !mfn_valid(_mfn(page_to_mfn(successor))) ||
+            if ( !mfn_valid(page_to_mfn(successor)) ||
                  !page_state_is(successor, free) ||
                  (PFN_ORDER(successor) != order) ||
                  (phys_to_nid(page_to_maddr(successor)) != node) )
@@ -1470,7 +1478,7 @@ static unsigned long mark_page_offline(struct page_info *pg, int broken)
 {
     unsigned long nx, x, y = pg->count_info;
 
-    ASSERT(page_is_ram_type(page_to_mfn(pg), RAM_TYPE_CONVENTIONAL));
+    ASSERT(page_is_ram_type(mfn_x(page_to_mfn(pg)), RAM_TYPE_CONVENTIONAL));
     ASSERT(spin_is_locked(&heap_lock));
 
     do {
@@ -1533,7 +1541,7 @@ int offline_page(unsigned long mfn, int broken, uint32_t *status)
     }
 
     *status = 0;
-    pg = mfn_to_page(mfn);
+    pg = mfn_to_page(_mfn(mfn));
 
     if ( is_xen_fixed_mfn(mfn) )
     {
@@ -1640,7 +1648,7 @@ unsigned int online_page(unsigned long mfn, uint32_t *status)
         return -EINVAL;
     }
 
-    pg = mfn_to_page(mfn);
+    pg = mfn_to_page(_mfn(mfn));
 
     spin_lock(&heap_lock);
 
@@ -1694,7 +1702,7 @@ int query_page_offline(unsigned long mfn, uint32_t *status)
     *status = 0;
     spin_lock(&heap_lock);
 
-    pg = mfn_to_page(mfn);
+    pg = mfn_to_page(_mfn(mfn));
 
     if ( page_state_is(pg, offlining) )
         *status |= PG_OFFLINE_STATUS_OFFLINE_PENDING;
@@ -1726,7 +1734,7 @@ static void init_heap_pages(
      * Update first_valid_mfn to ensure those regions are covered.
      */
     spin_lock(&heap_lock);
-    first_valid_mfn = min_t(unsigned long, page_to_mfn(pg), first_valid_mfn);
+    first_valid_mfn = mfn_min(page_to_mfn(pg), first_valid_mfn);
     spin_unlock(&heap_lock);
 
     for ( i = 0; i < nr_pages; i++ )
@@ -1735,14 +1743,14 @@ static void init_heap_pages(
 
         if ( unlikely(!avail[nid]) )
         {
-            unsigned long s = page_to_mfn(pg + i);
-            unsigned long e = page_to_mfn(pg + nr_pages - 1) + 1;
+            unsigned long s = mfn_x(page_to_mfn(pg + i));
+            unsigned long e = mfn_x(mfn_add(page_to_mfn(pg + nr_pages - 1), 1));
             bool_t use_tail = (nid == phys_to_nid(pfn_to_paddr(e - 1))) &&
                               !(s & ((1UL << MAX_ORDER) - 1)) &&
                               (find_first_set_bit(e) <= find_first_set_bit(s));
             unsigned long n;
 
-            n = init_node_heap(nid, page_to_mfn(pg+i), nr_pages - i,
+            n = init_node_heap(nid, mfn_x(page_to_mfn(pg+i)), nr_pages - i,
                                &use_tail);
             BUG_ON(i + n > nr_pages);
             if ( n && !use_tail )
@@ -1796,7 +1804,7 @@ void __init end_boot_allocator(void)
         if ( (r->s < r->e) &&
              (phys_to_nid(pfn_to_paddr(r->s)) == cpu_to_node(0)) )
         {
-            init_heap_pages(mfn_to_page(r->s), r->e - r->s);
+            init_heap_pages(mfn_to_page(_mfn(r->s)), r->e - r->s);
             r->e = r->s;
             break;
         }
@@ -1805,7 +1813,7 @@ void __init end_boot_allocator(void)
     {
         struct bootmem_region *r = &bootmem_region_list[i];
         if ( r->s < r->e )
-            init_heap_pages(mfn_to_page(r->s), r->e - r->s);
+            init_heap_pages(mfn_to_page(_mfn(r->s)), r->e - r->s);
     }
     nr_bootmem_regions = 0;
     init_heap_pages(virt_to_page(bootmem_region_list), 1);
@@ -1862,7 +1870,7 @@ static void __init smp_scrub_heap_pages(void *data)
 
     for ( mfn = start; mfn < end; mfn++ )
     {
-        pg = mfn_to_page(mfn);
+        pg = mfn_to_page(_mfn(mfn));
 
         /* Check the mfn is valid and page is free. */
         if ( !mfn_valid(_mfn(mfn)) || !page_state_is(pg, free) )
@@ -1915,7 +1923,7 @@ static void __init scrub_heap_pages(void)
         if ( !node_spanned_pages(i) )
             continue;
         /* Calculate Node memory start and end address. */
-        start = max(node_start_pfn(i), first_valid_mfn);
+        start = max(node_start_pfn(i), mfn_x(first_valid_mfn));
         end = min(node_start_pfn(i) + node_spanned_pages(i), max_page);
         /* Just in case NODE has 1 page and starts below first_valid_mfn. */
         end = max(end, start);
@@ -2159,17 +2167,17 @@ void free_xenheap_pages(void *v, unsigned int order)
 
 void init_domheap_pages(paddr_t ps, paddr_t pe)
 {
-    unsigned long smfn, emfn;
+    mfn_t smfn, emfn;
 
     ASSERT(!in_irq());
 
-    smfn = round_pgup(ps) >> PAGE_SHIFT;
-    emfn = round_pgdown(pe) >> PAGE_SHIFT;
+    smfn = maddr_to_mfn(round_pgup(ps));
+    emfn = maddr_to_mfn(round_pgup(pe));
 
-    if ( emfn <= smfn )
+    if ( mfn_x(emfn) <= mfn_x(smfn) )
         return;
 
-    init_heap_pages(mfn_to_page(smfn), emfn - smfn);
+    init_heap_pages(mfn_to_page(smfn), mfn_x(emfn) - mfn_x(smfn));
 }
 
 
diff --git a/xen/include/asm-arm/numa.h b/xen/include/asm-arm/numa.h
index 7e0b69413d..490d1f31aa 100644
--- a/xen/include/asm-arm/numa.h
+++ b/xen/include/asm-arm/numa.h
@@ -1,6 +1,8 @@
 #ifndef __ARCH_ARM_NUMA_H
 #define __ARCH_ARM_NUMA_H
 
+#include <xen/mm.h>
+
 typedef u8 nodeid_t;
 
 /* Fake one node for now. See also node_online_map. */
@@ -16,11 +18,11 @@ static inline __attribute__((pure)) nodeid_t phys_to_nid(paddr_t addr)
  * TODO: make first_valid_mfn static when NUMA is supported on Arm, this
  * is required because the dummy helpers are using it.
  */
-extern unsigned long first_valid_mfn;
+extern mfn_t first_valid_mfn;
 
 /* XXX: implement NUMA support */
-#define node_spanned_pages(nid) (max_page - first_valid_mfn)
-#define node_start_pfn(nid) (first_valid_mfn)
+#define node_spanned_pages(nid) (max_page - mfn_x(first_valid_mfn))
+#define node_start_pfn(nid) (mfn_x(first_valid_mfn))
 #define __node_distance(a, b) (20)
 
 static inline unsigned int arch_get_dma_bitsize(void)
-- 
2.11.0


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply related	[flat|nested] 76+ messages in thread

* [PATCH v4 12/16] xen/mm: Switch common/memory.c to use typesafe MFN
  2018-02-21 14:02 [PATCH v4 00/16] xen: Convert page_to_mfn and mfn_to_page to use typesafe MFN Julien Grall
                   ` (10 preceding siblings ...)
  2018-02-21 14:02 ` [PATCH v4 11/16] xen/mm: Switch page_alloc.c to typesafe MFN Julien Grall
@ 2018-02-21 14:02 ` Julien Grall
  2018-02-23 17:26   ` Wei Liu
  2018-03-02 15:34   ` Jan Beulich
  2018-02-21 14:02 ` [PATCH v4 13/16] xen/grant: Switch {create, replace}_grant_p2m_mapping to " Julien Grall
                   ` (3 subsequent siblings)
  15 siblings, 2 replies; 76+ messages in thread
From: Julien Grall @ 2018-02-21 14:02 UTC (permalink / raw)
  To: xen-devel
  Cc: Stefano Stabellini, Wei Liu, George Dunlap, Andrew Cooper,
	Ian Jackson, Tim Deegan, Julien Grall, Jan Beulich

A new helper copy_mfn_to_guest is introduced to easily to copy a MFN to
the guest memory.

Not functional change intended

Signed-off-by: Julien Grall <julien.grall@arm.com>

---
Cc: Andrew Cooper <andrew.cooper3@citrix.com>
Cc: George Dunlap <George.Dunlap@eu.citrix.com>
Cc: Ian Jackson <ian.jackson@eu.citrix.com>
Cc: Jan Beulich <jbeulich@suse.com>
Cc: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
Cc: Stefano Stabellini <sstabellini@kernel.org>
Cc: Tim Deegan <tim@xen.org>
Cc: Wei Liu <wei.liu2@citrix.com>

    Changes in v4:
        - Patch added
---
 xen/common/memory.c | 72 ++++++++++++++++++++++++++++++++---------------------
 1 file changed, 44 insertions(+), 28 deletions(-)

diff --git a/xen/common/memory.c b/xen/common/memory.c
index 59d23a2a98..93d856df02 100644
--- a/xen/common/memory.c
+++ b/xen/common/memory.c
@@ -33,6 +33,12 @@
 #include <asm/guest.h>
 #endif
 
+/* Override macros from asm/page.h to make them work with mfn_t */
+#undef page_to_mfn
+#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
+#undef mfn_to_page
+#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
+
 struct memop_args {
     /* INPUT */
     struct domain *domain;     /* Domain to be affected. */
@@ -95,11 +101,18 @@ static unsigned int max_order(const struct domain *d)
     return min(order, MAX_ORDER + 0U);
 }
 
+/* Helper to copy a typesafe MFN to guest */
+#define copy_mfn_to_guest(hnd, off, mfn)            \
+    ({                                              \
+        xen_pfn_t mfn_ = mfn_x(mfn);                \
+        __copy_to_guest_offset(hnd, off, &mfn_, 1); \
+    })
+
 static void increase_reservation(struct memop_args *a)
 {
     struct page_info *page;
     unsigned long i;
-    xen_pfn_t mfn;
+    mfn_t mfn;
     struct domain *d = a->domain;
 
     if ( !guest_handle_is_null(a->extent_list) &&
@@ -133,7 +146,7 @@ static void increase_reservation(struct memop_args *a)
              !guest_handle_is_null(a->extent_list) )
         {
             mfn = page_to_mfn(page);
-            if ( unlikely(__copy_to_guest_offset(a->extent_list, i, &mfn, 1)) )
+            if ( unlikely(copy_mfn_to_guest(a->extent_list, i, mfn)) )
                 goto out;
         }
     }
@@ -146,7 +159,8 @@ static void populate_physmap(struct memop_args *a)
 {
     struct page_info *page;
     unsigned int i, j;
-    xen_pfn_t gpfn, mfn;
+    xen_pfn_t gpfn;
+    mfn_t mfn;
     struct domain *d = a->domain, *curr_d = current->domain;
     bool need_tlbflush = false;
     uint32_t tlbflush_timestamp = 0;
@@ -205,14 +219,15 @@ static void populate_physmap(struct memop_args *a)
         {
             if ( is_domain_direct_mapped(d) )
             {
-                mfn = gpfn;
+                mfn = _mfn(gpfn);
 
-                for ( j = 0; j < (1U << a->extent_order); j++, mfn++ )
+                for ( j = 0; j < (1U << a->extent_order); j++,
+                      mfn = mfn_add(mfn, 1) )
                 {
-                    if ( !mfn_valid(_mfn(mfn)) )
+                    if ( !mfn_valid(mfn) )
                     {
-                        gdprintk(XENLOG_INFO, "Invalid mfn %#"PRI_xen_pfn"\n",
-                                 mfn);
+                        gdprintk(XENLOG_INFO, "Invalid mfn %#"PRI_mfn"\n",
+                                 mfn_x(mfn));
                         goto out;
                     }
 
@@ -220,14 +235,14 @@ static void populate_physmap(struct memop_args *a)
                     if ( !get_page(page, d) )
                     {
                         gdprintk(XENLOG_INFO,
-                                 "mfn %#"PRI_xen_pfn" doesn't belong to d%d\n",
-                                  mfn, d->domain_id);
+                                 "mfn %#"PRI_mfn" doesn't belong to d%d\n",
+                                  mfn_x(mfn), d->domain_id);
                         goto out;
                     }
                     put_page(page);
                 }
 
-                mfn = gpfn;
+                mfn = _mfn(gpfn);
             }
             else
             {
@@ -253,15 +268,15 @@ static void populate_physmap(struct memop_args *a)
                 mfn = page_to_mfn(page);
             }
 
-            guest_physmap_add_page(d, _gfn(gpfn), _mfn(mfn), a->extent_order);
+            guest_physmap_add_page(d, _gfn(gpfn), mfn, a->extent_order);
 
             if ( !paging_mode_translate(d) )
             {
                 for ( j = 0; j < (1U << a->extent_order); j++ )
-                    set_gpfn_from_mfn(mfn + j, gpfn + j);
+                    set_gpfn_from_mfn(mfn_x(mfn_add(mfn, j)), gpfn + j);
 
                 /* Inform the domain of the new page's machine address. */ 
-                if ( unlikely(__copy_to_guest_offset(a->extent_list, i, &mfn, 1)) )
+                if ( unlikely(copy_mfn_to_guest(a->extent_list, i, mfn)) )
                     goto out;
             }
         }
@@ -304,7 +319,7 @@ int guest_remove_page(struct domain *d, unsigned long gmfn)
         if ( p2mt == p2m_ram_paging_out )
         {
             ASSERT(mfn_valid(mfn));
-            page = mfn_to_page(mfn_x(mfn));
+            page = mfn_to_page(mfn);
             if ( test_and_clear_bit(_PGC_allocated, &page->count_info) )
                 put_page(page);
         }
@@ -349,7 +364,7 @@ int guest_remove_page(struct domain *d, unsigned long gmfn)
     }
 #endif /* CONFIG_X86 */
 
-    page = mfn_to_page(mfn_x(mfn));
+    page = mfn_to_page(mfn);
     if ( unlikely(!get_page(page, d)) )
     {
         put_gfn(d, gmfn);
@@ -490,7 +505,8 @@ static long memory_exchange(XEN_GUEST_HANDLE_PARAM(xen_memory_exchange_t) arg)
     PAGE_LIST_HEAD(in_chunk_list);
     PAGE_LIST_HEAD(out_chunk_list);
     unsigned long in_chunk_order, out_chunk_order;
-    xen_pfn_t     gpfn, gmfn, mfn;
+    xen_pfn_t     gpfn, gmfn;
+    mfn_t         mfn;
     unsigned long i, j, k;
     unsigned int  memflags = 0;
     long          rc = 0;
@@ -612,7 +628,7 @@ static long memory_exchange(XEN_GUEST_HANDLE_PARAM(xen_memory_exchange_t) arg)
                 p2m_type_t p2mt;
 
                 /* Shared pages cannot be exchanged */
-                mfn = mfn_x(get_gfn_unshare(d, gmfn + k, &p2mt));
+                mfn = get_gfn_unshare(d, gmfn + k, &p2mt);
                 if ( p2m_is_shared(p2mt) )
                 {
                     put_gfn(d, gmfn + k);
@@ -620,9 +636,9 @@ static long memory_exchange(XEN_GUEST_HANDLE_PARAM(xen_memory_exchange_t) arg)
                     goto fail; 
                 }
 #else /* !CONFIG_X86 */
-                mfn = mfn_x(gfn_to_mfn(d, _gfn(gmfn + k)));
+                mfn = gfn_to_mfn(d, _gfn(gmfn + k));
 #endif
-                if ( unlikely(!mfn_valid(_mfn(mfn))) )
+                if ( unlikely(!mfn_valid(mfn)) )
                 {
                     put_gfn(d, gmfn + k);
                     rc = -EINVAL;
@@ -669,10 +685,10 @@ static long memory_exchange(XEN_GUEST_HANDLE_PARAM(xen_memory_exchange_t) arg)
             if ( !test_and_clear_bit(_PGC_allocated, &page->count_info) )
                 BUG();
             mfn = page_to_mfn(page);
-            gfn = mfn_to_gmfn(d, mfn);
+            gfn = mfn_to_gmfn(d, mfn_x(mfn));
             /* Pages were unshared above */
             BUG_ON(SHARED_M2P(gfn));
-            if ( guest_physmap_remove_page(d, _gfn(gfn), _mfn(mfn), 0) )
+            if ( guest_physmap_remove_page(d, _gfn(gfn), mfn, 0) )
                 domain_crash(d);
             put_page(page);
         }
@@ -717,16 +733,16 @@ static long memory_exchange(XEN_GUEST_HANDLE_PARAM(xen_memory_exchange_t) arg)
             }
 
             mfn = page_to_mfn(page);
-            guest_physmap_add_page(d, _gfn(gpfn), _mfn(mfn),
+            guest_physmap_add_page(d, _gfn(gpfn), mfn,
                                    exch.out.extent_order);
 
             if ( !paging_mode_translate(d) )
             {
                 for ( k = 0; k < (1UL << exch.out.extent_order); k++ )
-                    set_gpfn_from_mfn(mfn + k, gpfn + k);
-                if ( __copy_to_guest_offset(exch.out.extent_start,
-                                            (i << out_chunk_order) + j,
-                                            &mfn, 1) )
+                    set_gpfn_from_mfn(mfn_x(mfn_add(mfn, k)), gpfn + k);
+                if ( copy_mfn_to_guest(exch.out.extent_start,
+                                       (i << out_chunk_order) + j,
+                                       mfn) )
                     rc = -EFAULT;
             }
         }
@@ -1221,7 +1237,7 @@ long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
         if ( page )
         {
             rc = guest_physmap_remove_page(d, _gfn(xrfp.gpfn),
-                                           _mfn(page_to_mfn(page)), 0);
+                                           page_to_mfn(page), 0);
             put_page(page);
         }
         else
-- 
2.11.0


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply related	[flat|nested] 76+ messages in thread

* [PATCH v4 13/16] xen/grant: Switch {create, replace}_grant_p2m_mapping to typesafe MFN
  2018-02-21 14:02 [PATCH v4 00/16] xen: Convert page_to_mfn and mfn_to_page to use typesafe MFN Julien Grall
                   ` (11 preceding siblings ...)
  2018-02-21 14:02 ` [PATCH v4 12/16] xen/mm: Switch common/memory.c to use " Julien Grall
@ 2018-02-21 14:02 ` Julien Grall
  2018-02-23 17:29   ` Wei Liu
  2018-03-02 15:38   ` Jan Beulich
  2018-02-21 14:02 ` [PATCH v4 14/16] xen/grant: Switch common/grant_table.c to use " Julien Grall
                   ` (2 subsequent siblings)
  15 siblings, 2 replies; 76+ messages in thread
From: Julien Grall @ 2018-02-21 14:02 UTC (permalink / raw)
  To: xen-devel
  Cc: Stefano Stabellini, Wei Liu, George Dunlap, Andrew Cooper,
	Ian Jackson, Tim Deegan, Julien Grall, Jan Beulich

The current prototype is slightly confusing because it takes a guest
physical address and a machine physical frame (not address!). Switching to
MFN will improve safety and reduce the chance to mistakenly invert the
2 parameters.

Signed-off-by: Julien grall <julien.grall@arm.com>

---
Cc: Stefano Stabellini <sstabellini@kernel.org>
Cc: Julien Grall <julien.grall@arm.com>
Cc: Andrew Cooper <andrew.cooper3@citrix.com>
Cc: George Dunlap <George.Dunlap@eu.citrix.com>
Cc: Ian Jackson <ian.jackson@eu.citrix.com>
Cc: Jan Beulich <jbeulich@suse.com>
Cc: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
Cc: Tim Deegan <tim@xen.org>
Cc: Wei Liu <wei.liu2@citrix.com>

    Changes in v4:
        - Patch added
---
 xen/arch/arm/mm.c                     | 10 +++++-----
 xen/arch/x86/hvm/grant_table.c        | 14 +++++++-------
 xen/arch/x86/pv/grant_table.c         | 10 +++++-----
 xen/common/grant_table.c              |  8 ++++----
 xen/include/asm-arm/grant_table.h     |  9 ++++-----
 xen/include/asm-x86/grant_table.h     |  4 ++--
 xen/include/asm-x86/hvm/grant_table.h |  8 ++++----
 xen/include/asm-x86/pv/grant_table.h  |  8 ++++----
 8 files changed, 35 insertions(+), 36 deletions(-)

diff --git a/xen/arch/arm/mm.c b/xen/arch/arm/mm.c
index f17907ace8..4268dd5c2d 100644
--- a/xen/arch/arm/mm.c
+++ b/xen/arch/arm/mm.c
@@ -1423,7 +1423,7 @@ void gnttab_mark_dirty(struct domain *d, unsigned long l)
     }
 }
 
-int create_grant_host_mapping(unsigned long addr, unsigned long frame,
+int create_grant_host_mapping(unsigned long addr, mfn_t frame,
                               unsigned int flags, unsigned int cache_flags)
 {
     int rc;
@@ -1436,7 +1436,7 @@ int create_grant_host_mapping(unsigned long addr, unsigned long frame,
         t = p2m_grant_map_ro;
 
     rc = guest_physmap_add_entry(current->domain, gaddr_to_gfn(addr),
-                                 _mfn(frame), 0, t);
+                                 frame, 0, t);
 
     if ( rc )
         return GNTST_general_error;
@@ -1444,8 +1444,8 @@ int create_grant_host_mapping(unsigned long addr, unsigned long frame,
         return GNTST_okay;
 }
 
-int replace_grant_host_mapping(unsigned long addr, unsigned long mfn,
-        unsigned long new_addr, unsigned int flags)
+int replace_grant_host_mapping(unsigned long addr, mfn_t mfn,
+                               unsigned long new_addr, unsigned int flags)
 {
     gfn_t gfn = gaddr_to_gfn(addr);
     struct domain *d = current->domain;
@@ -1454,7 +1454,7 @@ int replace_grant_host_mapping(unsigned long addr, unsigned long mfn,
     if ( new_addr != 0 || (flags & GNTMAP_contains_pte) )
         return GNTST_general_error;
 
-    rc = guest_physmap_remove_page(d, gfn, _mfn(mfn), 0);
+    rc = guest_physmap_remove_page(d, gfn, mfn, 0);
 
     return rc ? GNTST_general_error : GNTST_okay;
 }
diff --git a/xen/arch/x86/hvm/grant_table.c b/xen/arch/x86/hvm/grant_table.c
index 9ca9fe0425..ecd7d078ab 100644
--- a/xen/arch/x86/hvm/grant_table.c
+++ b/xen/arch/x86/hvm/grant_table.c
@@ -25,7 +25,7 @@
 
 #include <asm/p2m.h>
 
-int create_grant_p2m_mapping(uint64_t addr, unsigned long frame,
+int create_grant_p2m_mapping(uint64_t addr, mfn_t frame,
                              unsigned int flags,
                              unsigned int cache_flags)
 {
@@ -41,14 +41,14 @@ int create_grant_p2m_mapping(uint64_t addr, unsigned long frame,
         p2mt = p2m_grant_map_rw;
     rc = guest_physmap_add_entry(current->domain,
                                  _gfn(addr >> PAGE_SHIFT),
-                                 _mfn(frame), PAGE_ORDER_4K, p2mt);
+                                 frame, PAGE_ORDER_4K, p2mt);
     if ( rc )
         return GNTST_general_error;
     else
         return GNTST_okay;
 }
 
-int replace_grant_p2m_mapping(uint64_t addr, unsigned long frame,
+int replace_grant_p2m_mapping(uint64_t addr, mfn_t frame,
                               uint64_t new_addr, unsigned int flags)
 {
     unsigned long gfn = (unsigned long)(addr >> PAGE_SHIFT);
@@ -60,15 +60,15 @@ int replace_grant_p2m_mapping(uint64_t addr, unsigned long frame,
         return GNTST_general_error;
 
     old_mfn = get_gfn(d, gfn, &type);
-    if ( !p2m_is_grant(type) || mfn_x(old_mfn) != frame )
+    if ( !p2m_is_grant(type) || !mfn_eq(old_mfn, frame) )
     {
         put_gfn(d, gfn);
         gdprintk(XENLOG_WARNING,
-                 "old mapping invalid (type %d, mfn %" PRI_mfn ", frame %lx)\n",
-                 type, mfn_x(old_mfn), frame);
+                 "old mapping invalid (type %d, mfn %" PRI_mfn ", frame %"PRI_mfn")\n",
+                 type, mfn_x(old_mfn), mfn_x(frame));
         return GNTST_general_error;
     }
-    if ( guest_physmap_remove_page(d, _gfn(gfn), _mfn(frame), PAGE_ORDER_4K) )
+    if ( guest_physmap_remove_page(d, _gfn(gfn), frame, PAGE_ORDER_4K) )
     {
         put_gfn(d, gfn);
         return GNTST_general_error;
diff --git a/xen/arch/x86/pv/grant_table.c b/xen/arch/x86/pv/grant_table.c
index 4dbc550366..458085e1b6 100644
--- a/xen/arch/x86/pv/grant_table.c
+++ b/xen/arch/x86/pv/grant_table.c
@@ -50,7 +50,7 @@ static unsigned int grant_to_pte_flags(unsigned int grant_flags,
     return pte_flags;
 }
 
-int create_grant_pv_mapping(uint64_t addr, unsigned long frame,
+int create_grant_pv_mapping(uint64_t addr, mfn_t frame,
                             unsigned int flags, unsigned int cache_flags)
 {
     struct vcpu *curr = current;
@@ -60,7 +60,7 @@ int create_grant_pv_mapping(uint64_t addr, unsigned long frame,
     mfn_t gl1mfn;
     int rc = GNTST_general_error;
 
-    nl1e = l1e_from_pfn(frame, grant_to_pte_flags(flags, cache_flags));
+    nl1e = l1e_from_mfn(frame, grant_to_pte_flags(flags, cache_flags));
     nl1e = adjust_guest_l1e(nl1e, currd);
 
     /*
@@ -192,7 +192,7 @@ static bool steal_linear_address(unsigned long linear, l1_pgentry_t *out)
  * new_addr has only ever been available via GNTABOP_unmap_and_replace, and
  * only when !(flags & GNTMAP_contains_pte).
  */
-int replace_grant_pv_mapping(uint64_t addr, unsigned long frame,
+int replace_grant_pv_mapping(uint64_t addr, mfn_t frame,
                              uint64_t new_addr, unsigned int flags)
 {
     struct vcpu *curr = current;
@@ -282,14 +282,14 @@ int replace_grant_pv_mapping(uint64_t addr, unsigned long frame,
      * Check that the address supplied is actually mapped to frame (with
      * appropriate permissions).
      */
-    if ( unlikely(l1e_get_pfn(ol1e) != frame) ||
+    if ( unlikely(!mfn_eq(l1e_get_mfn(ol1e), frame)) ||
          unlikely((l1e_get_flags(ol1e) ^ grant_pte_flags) &
                   (_PAGE_PRESENT | _PAGE_RW)) )
     {
         gdprintk(XENLOG_ERR,
                  "PTE %"PRIpte" for %"PRIx64" doesn't match grant (%"PRIpte")\n",
                  l1e_get_intpte(ol1e), addr,
-                 l1e_get_intpte(l1e_from_pfn(frame, grant_pte_flags)));
+                 l1e_get_intpte(l1e_from_mfn(frame, grant_pte_flags)));
         goto out_unlock;
     }
 
diff --git a/xen/common/grant_table.c b/xen/common/grant_table.c
index 48c547930c..c555c1d451 100644
--- a/xen/common/grant_table.c
+++ b/xen/common/grant_table.c
@@ -1036,7 +1036,7 @@ map_grant_ref(
 
         if ( op->flags & GNTMAP_host_map )
         {
-            rc = create_grant_host_mapping(op->host_addr, frame, op->flags,
+            rc = create_grant_host_mapping(op->host_addr, _mfn(frame), op->flags,
                                            cache_flags);
             if ( rc != GNTST_okay )
                 goto undo_out;
@@ -1076,7 +1076,7 @@ map_grant_ref(
                 typecnt++;
             }
 
-            rc = create_grant_host_mapping(op->host_addr, frame, op->flags, 0);
+            rc = create_grant_host_mapping(op->host_addr, _mfn(frame), op->flags, 0);
             if ( rc != GNTST_okay )
                 goto undo_out;
 
@@ -1153,7 +1153,7 @@ map_grant_ref(
  undo_out:
     if ( host_map_created )
     {
-        replace_grant_host_mapping(op->host_addr, frame, 0, op->flags);
+        replace_grant_host_mapping(op->host_addr, _mfn(frame), 0, op->flags);
         gnttab_flush_tlb(ld);
     }
 
@@ -1339,7 +1339,7 @@ unmap_common(
     if ( op->host_addr && (flags & GNTMAP_host_map) )
     {
         if ( (rc = replace_grant_host_mapping(op->host_addr,
-                                              op->frame, op->new_addr,
+                                              _mfn(op->frame), op->new_addr,
                                               flags)) < 0 )
             goto act_release_out;
 
diff --git a/xen/include/asm-arm/grant_table.h b/xen/include/asm-arm/grant_table.h
index 0dfdc5577f..ffff18d456 100644
--- a/xen/include/asm-arm/grant_table.h
+++ b/xen/include/asm-arm/grant_table.h
@@ -13,12 +13,11 @@ struct grant_table_arch {
 };
 
 void gnttab_clear_flag(unsigned long nr, uint16_t *addr);
-int create_grant_host_mapping(unsigned long gpaddr,
-        unsigned long mfn, unsigned int flags, unsigned int
-        cache_flags);
+int create_grant_host_mapping(unsigned long gpaddr, mfn_t mfn,
+                              unsigned int flags, unsigned int cache_flags);
 #define gnttab_host_mapping_get_page_type(ro, ld, rd) (0)
-int replace_grant_host_mapping(unsigned long gpaddr, unsigned long mfn,
-        unsigned long new_gpaddr, unsigned int flags);
+int replace_grant_host_mapping(unsigned long gpaddr, mfn_t mfn,
+                               unsigned long new_gpaddr, unsigned int flags);
 void gnttab_mark_dirty(struct domain *d, unsigned long l);
 #define gnttab_create_status_page(d, t, i) do {} while (0)
 #define gnttab_status_gmfn(d, t, i) (0)
diff --git a/xen/include/asm-x86/grant_table.h b/xen/include/asm-x86/grant_table.h
index d9157e4417..e40ac8e3e7 100644
--- a/xen/include/asm-x86/grant_table.h
+++ b/xen/include/asm-x86/grant_table.h
@@ -21,7 +21,7 @@ struct grant_table_arch {
  * Caller must own caller's BIGLOCK, is responsible for flushing the TLB, and
  * must hold a reference to the page.
  */
-static inline int create_grant_host_mapping(uint64_t addr, unsigned long frame,
+static inline int create_grant_host_mapping(uint64_t addr, mfn_t frame,
                                             unsigned int flags,
                                             unsigned int cache_flags)
 {
@@ -30,7 +30,7 @@ static inline int create_grant_host_mapping(uint64_t addr, unsigned long frame,
     return create_grant_pv_mapping(addr, frame, flags, cache_flags);
 }
 
-static inline int replace_grant_host_mapping(uint64_t addr, unsigned long frame,
+static inline int replace_grant_host_mapping(uint64_t addr, mfn_t frame,
                                              uint64_t new_addr,
                                              unsigned int flags)
 {
diff --git a/xen/include/asm-x86/hvm/grant_table.h b/xen/include/asm-x86/hvm/grant_table.h
index 711ce9b560..a5612585b3 100644
--- a/xen/include/asm-x86/hvm/grant_table.h
+++ b/xen/include/asm-x86/hvm/grant_table.h
@@ -23,24 +23,24 @@
 
 #ifdef CONFIG_HVM
 
-int create_grant_p2m_mapping(uint64_t addr, unsigned long frame,
+int create_grant_p2m_mapping(uint64_t addr, mfn_t frame,
                              unsigned int flags,
                              unsigned int cache_flags);
-int replace_grant_p2m_mapping(uint64_t addr, unsigned long frame,
+int replace_grant_p2m_mapping(uint64_t addr, mfn_t frame,
                               uint64_t new_addr, unsigned int flags);
 
 #else
 
 #include <public/grant_table.h>
 
-static inline int create_grant_p2m_mapping(uint64_t addr, unsigned long frame,
+static inline int create_grant_p2m_mapping(uint64_t addr, mfn_t frame,
                                            unsigned int flags,
                                            unsigned int cache_flags)
 {
     return GNTST_general_error;
 }
 
-static inline int replace_grant_p2m_mapping(uint64_t addr, unsigned long frame,
+static inline int replace_grant_p2m_mapping(uint64_t addr, mfn_t frame,
                                             uint64_t new_addr, unsigned int flags)
 {
     return GNTST_general_error;
diff --git a/xen/include/asm-x86/pv/grant_table.h b/xen/include/asm-x86/pv/grant_table.h
index 556e68f0eb..85442b6074 100644
--- a/xen/include/asm-x86/pv/grant_table.h
+++ b/xen/include/asm-x86/pv/grant_table.h
@@ -23,23 +23,23 @@
 
 #ifdef CONFIG_PV
 
-int create_grant_pv_mapping(uint64_t addr, unsigned long frame,
+int create_grant_pv_mapping(uint64_t addr, mfn_t frame,
                             unsigned int flags, unsigned int cache_flags);
-int replace_grant_pv_mapping(uint64_t addr, unsigned long frame,
+int replace_grant_pv_mapping(uint64_t addr, mfn_t frame,
                              uint64_t new_addr, unsigned int flags);
 
 #else
 
 #include <public/grant_table.h>
 
-static inline int create_grant_pv_mapping(uint64_t addr, unsigned long frame,
+static inline int create_grant_pv_mapping(uint64_t addr, mfn_t frame,
                                           unsigned int flags,
                                           unsigned int cache_flags)
 {
     return GNTST_general_error;
 }
 
-static inline int replace_grant_pv_mapping(uint64_t addr, unsigned long frame,
+static inline int replace_grant_pv_mapping(uint64_t addr, mfn_t frame,
                                            uint64_t new_addr, unsigned int flags)
 {
     return GNTST_general_error;
-- 
2.11.0


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply related	[flat|nested] 76+ messages in thread

* [PATCH v4 14/16] xen/grant: Switch common/grant_table.c to use typesafe MFN
  2018-02-21 14:02 [PATCH v4 00/16] xen: Convert page_to_mfn and mfn_to_page to use typesafe MFN Julien Grall
                   ` (12 preceding siblings ...)
  2018-02-21 14:02 ` [PATCH v4 13/16] xen/grant: Switch {create, replace}_grant_p2m_mapping to " Julien Grall
@ 2018-02-21 14:02 ` Julien Grall
  2018-02-23 17:30   ` Wei Liu
  2018-03-02 15:54   ` Jan Beulich
  2018-02-21 14:02 ` [PATCH v4 15/16] xen/x86: Switch mfn_to_page in x86_64/mm.c " Julien Grall
  2018-02-21 14:02 ` [PATCH v4 16/16] xen: Convert page_to_mfn and mfn_to_page " Julien Grall
  15 siblings, 2 replies; 76+ messages in thread
From: Julien Grall @ 2018-02-21 14:02 UTC (permalink / raw)
  To: xen-devel
  Cc: Stefano Stabellini, Wei Liu, George Dunlap, Andrew Cooper,
	Ian Jackson, Tim Deegan, Julien Grall, Jan Beulich

No functional change intended.

Signed-off-by: Julien Grall <julien.grall@arm.com>

---

Cc: Stefano Stabellini <sstabellini@kernel.org>
Cc: Julien Grall <julien.grall@arm.com>
Cc: Andrew Cooper <andrew.cooper3@citrix.com>
Cc: George Dunlap <George.Dunlap@eu.citrix.com>
Cc: Ian Jackson <ian.jackson@eu.citrix.com>
Cc: Jan Beulich <jbeulich@suse.com>
Cc: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
Cc: Tim Deegan <tim@xen.org>
Cc: Wei Liu <wei.liu2@citrix.com>

    Changes in v4:
        - Patch added
---
 xen/arch/arm/mm.c                 |   2 +-
 xen/common/grant_table.c          | 145 ++++++++++++++++++++------------------
 xen/include/asm-arm/grant_table.h |   2 +-
 xen/include/asm-x86/grant_table.h |   2 +-
 4 files changed, 81 insertions(+), 70 deletions(-)

diff --git a/xen/arch/arm/mm.c b/xen/arch/arm/mm.c
index 4268dd5c2d..db74466a16 100644
--- a/xen/arch/arm/mm.c
+++ b/xen/arch/arm/mm.c
@@ -1413,7 +1413,7 @@ void gnttab_clear_flag(unsigned long nr, uint16_t *addr)
     } while (cmpxchg(addr, old, old & mask) != old);
 }
 
-void gnttab_mark_dirty(struct domain *d, unsigned long l)
+void gnttab_mark_dirty(struct domain *d, mfn_t mfn)
 {
     /* XXX: mark dirty */
     static int warning;
diff --git a/xen/common/grant_table.c b/xen/common/grant_table.c
index c555c1d451..e9a81b66be 100644
--- a/xen/common/grant_table.c
+++ b/xen/common/grant_table.c
@@ -40,6 +40,12 @@
 #include <xsm/xsm.h>
 #include <asm/flushtlb.h>
 
+/* Override macros from asm/page.h to make them work with mfn_t */
+#undef page_to_mfn
+#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
+#undef mfn_to_page
+#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
+
 /* Per-domain grant information. */
 struct grant_table {
     /*
@@ -132,7 +138,7 @@ struct gnttab_unmap_common {
 
     /* Shared state beteen *_unmap and *_unmap_complete */
     uint16_t done;
-    unsigned long frame;
+    mfn_t frame;
     struct domain *rd;
     grant_ref_t ref;
 };
@@ -231,7 +237,7 @@ struct active_grant_entry {
                                 grant.                                */
     grant_ref_t   trans_gref;
     struct domain *trans_domain;
-    unsigned long frame;  /* Frame being granted.                     */
+    mfn_t         frame;  /* Frame being granted.                     */
 #ifndef NDEBUG
     gfn_t         gfn;    /* Guest's idea of the frame being granted. */
 #endif
@@ -336,14 +342,14 @@ static inline unsigned int grant_to_status_frames(unsigned int grant_frames)
    If rc == GNTST_okay, *page contains the page struct with a ref taken.
    Caller must do put_page(*page).
    If any error, *page = NULL, *frame = INVALID_MFN, no ref taken. */
-static int get_paged_frame(unsigned long gfn, unsigned long *frame,
+static int get_paged_frame(unsigned long gfn, mfn_t *frame,
                            struct page_info **page, bool readonly,
                            struct domain *rd)
 {
     int rc = GNTST_okay;
     p2m_type_t p2mt;
 
-    *frame = mfn_x(INVALID_MFN);
+    *frame = INVALID_MFN;
     *page = get_page_from_gfn(rd, gfn, &p2mt,
                               readonly ? P2M_ALLOC : P2M_UNSHARE);
     if ( !*page )
@@ -788,7 +794,7 @@ static int _set_status(unsigned gt_version,
 
 static struct active_grant_entry *grant_map_exists(const struct domain *ld,
                                                    struct grant_table *rgt,
-                                                   unsigned long mfn,
+                                                   mfn_t mfn,
                                                    grant_ref_t *cur_ref)
 {
     grant_ref_t ref, max_iter;
@@ -807,7 +813,8 @@ static struct active_grant_entry *grant_map_exists(const struct domain *ld,
     {
         struct active_grant_entry *act = active_entry_acquire(rgt, ref);
 
-        if ( act->pin && act->domid == ld->domain_id && act->frame == mfn )
+        if ( act->pin && act->domid == ld->domain_id &&
+             mfn_eq(act->frame, mfn) )
             return act;
         active_entry_release(act);
     }
@@ -824,7 +831,7 @@ static struct active_grant_entry *grant_map_exists(const struct domain *ld,
 #define MAPKIND_READ 1
 #define MAPKIND_WRITE 2
 static unsigned int mapkind(
-    struct grant_table *lgt, const struct domain *rd, unsigned long mfn)
+    struct grant_table *lgt, const struct domain *rd, mfn_t mfn)
 {
     struct grant_mapping *map;
     grant_handle_t handle, limit = lgt->maptrack_limit;
@@ -849,7 +856,7 @@ static unsigned int mapkind(
         if ( !(map->flags & (GNTMAP_device_map|GNTMAP_host_map)) ||
              map->domid != rd->domain_id )
             continue;
-        if ( _active_entry(rd->grant_table, map->ref).frame == mfn )
+        if ( mfn_eq(_active_entry(rd->grant_table, map->ref).frame, mfn) )
             kind |= map->flags & GNTMAP_readonly ?
                     MAPKIND_READ : MAPKIND_WRITE;
     }
@@ -872,7 +879,7 @@ map_grant_ref(
     struct grant_table *lgt, *rgt;
     struct vcpu   *led;
     grant_handle_t handle;
-    unsigned long  frame = 0;
+    mfn_t frame = _mfn(0);
     struct page_info *pg = NULL;
     int            rc = GNTST_okay;
     u32            old_pin;
@@ -999,7 +1006,7 @@ map_grant_ref(
     /* pg may be set, with a refcount included, from get_paged_frame(). */
     if ( !pg )
     {
-        pg = mfn_valid(_mfn(frame)) ? mfn_to_page(frame) : NULL;
+        pg = mfn_valid(frame) ? mfn_to_page(frame) : NULL;
         if ( pg )
             owner = page_get_owner_and_reference(pg);
     }
@@ -1025,18 +1032,18 @@ map_grant_ref(
             goto undo_out;
         }
 
-        if ( !iomem_access_permitted(rd, frame, frame) )
+        if ( !iomem_access_permitted(rd, mfn_x(frame), mfn_x(frame)) )
         {
             gdprintk(XENLOG_WARNING,
-                     "Iomem mapping not permitted %lx (domain %d)\n",
-                     frame, rd->domain_id);
+                     "Iomem mapping not permitted %#"PRI_mfn" (domain %d)\n",
+                     mfn_x(frame), rd->domain_id);
             rc = GNTST_general_error;
             goto undo_out;
         }
 
         if ( op->flags & GNTMAP_host_map )
         {
-            rc = create_grant_host_mapping(op->host_addr, _mfn(frame), op->flags,
+            rc = create_grant_host_mapping(op->host_addr, frame, op->flags,
                                            cache_flags);
             if ( rc != GNTST_okay )
                 goto undo_out;
@@ -1076,7 +1083,7 @@ map_grant_ref(
                 typecnt++;
             }
 
-            rc = create_grant_host_mapping(op->host_addr, _mfn(frame), op->flags, 0);
+            rc = create_grant_host_mapping(op->host_addr, frame, op->flags, 0);
             if ( rc != GNTST_okay )
                 goto undo_out;
 
@@ -1087,8 +1094,8 @@ map_grant_ref(
     {
     could_not_pin:
         if ( !rd->is_dying )
-            gdprintk(XENLOG_WARNING, "Could not pin grant frame %lx\n",
-                     frame);
+            gdprintk(XENLOG_WARNING, "Could not pin grant frame %#"PRI_mfn"\n",
+                     mfn_x(frame));
         rc = GNTST_general_error;
         goto undo_out;
     }
@@ -1108,13 +1115,14 @@ map_grant_ref(
              !(old_pin & (GNTPIN_hstw_mask|GNTPIN_devw_mask)) )
         {
             if ( !(kind & MAPKIND_WRITE) )
-                err = iommu_map_page(ld, frame, frame,
+                err = iommu_map_page(ld, mfn_x(frame), mfn_x(frame),
                                      IOMMUF_readable|IOMMUF_writable);
         }
         else if ( act_pin && !old_pin )
         {
             if ( !kind )
-                err = iommu_map_page(ld, frame, frame, IOMMUF_readable);
+                err = iommu_map_page(ld, mfn_x(frame), mfn_x(frame),
+                                     IOMMUF_readable);
         }
         if ( err )
         {
@@ -1143,7 +1151,7 @@ map_grant_ref(
     if ( need_iommu )
         double_gt_unlock(lgt, rgt);
 
-    op->dev_bus_addr = (u64)frame << PAGE_SHIFT;
+    op->dev_bus_addr = mfn_to_maddr(frame);
     op->handle       = handle;
     op->status       = GNTST_okay;
 
@@ -1153,7 +1161,7 @@ map_grant_ref(
  undo_out:
     if ( host_map_created )
     {
-        replace_grant_host_mapping(op->host_addr, _mfn(frame), 0, op->flags);
+        replace_grant_host_mapping(op->host_addr, frame, 0, op->flags);
         gnttab_flush_tlb(ld);
     }
 
@@ -1331,15 +1339,15 @@ unmap_common(
     op->frame = act->frame;
 
     if ( op->dev_bus_addr &&
-         unlikely(op->dev_bus_addr != pfn_to_paddr(act->frame)) )
+         unlikely(op->dev_bus_addr != mfn_to_maddr(act->frame)) )
         PIN_FAIL(act_release_out, GNTST_general_error,
                  "Bus address doesn't match gntref (%"PRIx64" != %"PRIpaddr")\n",
-                 op->dev_bus_addr, pfn_to_paddr(act->frame));
+                 op->dev_bus_addr, mfn_to_maddr(act->frame));
 
     if ( op->host_addr && (flags & GNTMAP_host_map) )
     {
         if ( (rc = replace_grant_host_mapping(op->host_addr,
-                                              _mfn(op->frame), op->new_addr,
+                                              op->frame, op->new_addr,
                                               flags)) < 0 )
             goto act_release_out;
 
@@ -1376,9 +1384,10 @@ unmap_common(
 
         kind = mapkind(lgt, rd, op->frame);
         if ( !kind )
-            err = iommu_unmap_page(ld, op->frame);
+            err = iommu_unmap_page(ld, mfn_x(op->frame));
         else if ( !(kind & MAPKIND_WRITE) )
-            err = iommu_map_page(ld, op->frame, op->frame, IOMMUF_readable);
+            err = iommu_map_page(ld, mfn_x(op->frame),
+                                 mfn_x(op->frame), IOMMUF_readable);
 
         double_gt_unlock(lgt, rgt);
 
@@ -1429,7 +1438,7 @@ unmap_common_complete(struct gnttab_unmap_common *op)
 
     if ( op->done & GNTMAP_device_map )
     {
-        if ( !is_iomem_page(_mfn(act->frame)) )
+        if ( !is_iomem_page(act->frame) )
         {
             if ( op->done & GNTMAP_readonly )
                 put_page(pg);
@@ -1446,7 +1455,7 @@ unmap_common_complete(struct gnttab_unmap_common *op)
 
     if ( op->done & GNTMAP_host_map )
     {
-        if ( !is_iomem_page(_mfn(op->frame)) )
+        if ( !is_iomem_page(op->frame) )
         {
             if ( gnttab_host_mapping_get_page_type(op->done & GNTMAP_readonly,
                                                    ld, rd) )
@@ -1487,7 +1496,7 @@ unmap_grant_ref(
     common->done = 0;
     common->new_addr = 0;
     common->rd = NULL;
-    common->frame = 0;
+    common->frame = _mfn(0);
 
     unmap_common(common);
     op->status = common->status;
@@ -1553,7 +1562,7 @@ unmap_and_replace(
     common->done = 0;
     common->dev_bus_addr = 0;
     common->rd = NULL;
-    common->frame = 0;
+    common->frame = _mfn(0);
 
     unmap_common(common);
     op->status = common->status;
@@ -2011,7 +2020,7 @@ gnttab_transfer(
     struct page_info *page;
     int i;
     struct gnttab_transfer gop;
-    unsigned long mfn;
+    mfn_t mfn;
     unsigned int max_bitsize;
     struct active_grant_entry *act;
 
@@ -2035,16 +2044,16 @@ gnttab_transfer(
         {
             p2m_type_t p2mt;
 
-            mfn = mfn_x(get_gfn_unshare(d, gop.mfn, &p2mt));
+            mfn = get_gfn_unshare(d, gop.mfn, &p2mt);
             if ( p2m_is_shared(p2mt) || !p2m_is_valid(p2mt) )
-                mfn = mfn_x(INVALID_MFN);
+                mfn = INVALID_MFN;
         }
 #else
-        mfn = mfn_x(gfn_to_mfn(d, _gfn(gop.mfn)));
+        mfn = gfn_to_mfn(d, _gfn(gop.mfn));
 #endif
 
         /* Check the passed page frame for basic validity. */
-        if ( unlikely(!mfn_valid(_mfn(mfn))) )
+        if ( unlikely(!mfn_valid(mfn)) )
         {
             put_gfn(d, gop.mfn);
             gdprintk(XENLOG_INFO, "out-of-range %lx\n", (unsigned long)gop.mfn);
@@ -2060,12 +2069,13 @@ gnttab_transfer(
             goto copyback;
         }
 
-        rc = guest_physmap_remove_page(d, _gfn(gop.mfn), _mfn(mfn), 0);
+        rc = guest_physmap_remove_page(d, _gfn(gop.mfn), mfn, 0);
         gnttab_flush_tlb(d);
         if ( rc )
         {
-            gdprintk(XENLOG_INFO, "can't remove GFN %"PRI_xen_pfn" (MFN %lx)\n",
-                     gop.mfn, mfn);
+            gdprintk(XENLOG_INFO,
+                     "can't remove GFN %"PRI_xen_pfn" (MFN %#"PRI_mfn")\n",
+                     gop.mfn, mfn_x(mfn));
             gop.status = GNTST_general_error;
             goto put_gfn_and_copyback;
         }
@@ -2094,7 +2104,7 @@ gnttab_transfer(
             e, e->grant_table->gt_version > 1 || paging_mode_translate(e)
                ? BITS_PER_LONG + PAGE_SHIFT : 32 + PAGE_SHIFT);
         if ( max_bitsize < BITS_PER_LONG + PAGE_SHIFT &&
-             (mfn >> (max_bitsize - PAGE_SHIFT)) )
+             (mfn_x(mfn) >> (max_bitsize - PAGE_SHIFT)) )
         {
             struct page_info *new_page;
 
@@ -2106,7 +2116,7 @@ gnttab_transfer(
                 goto unlock_and_copyback;
             }
 
-            copy_domain_page(_mfn(page_to_mfn(new_page)), _mfn(mfn));
+            copy_domain_page(page_to_mfn(new_page), mfn);
 
             page->count_info &= ~(PGC_count_mask|PGC_allocated);
             free_domheap_page(page);
@@ -2183,18 +2193,17 @@ gnttab_transfer(
         {
             grant_entry_v1_t *sha = &shared_entry_v1(e->grant_table, gop.ref);
 
-            guest_physmap_add_page(e, _gfn(sha->frame), _mfn(mfn), 0);
+            guest_physmap_add_page(e, _gfn(sha->frame), mfn, 0);
             if ( !paging_mode_translate(e) )
-                sha->frame = mfn;
+                sha->frame = mfn_x(mfn);
         }
         else
         {
             grant_entry_v2_t *sha = &shared_entry_v2(e->grant_table, gop.ref);
 
-            guest_physmap_add_page(e, _gfn(sha->full_page.frame),
-                                   _mfn(mfn), 0);
+            guest_physmap_add_page(e, _gfn(sha->full_page.frame), mfn, 0);
             if ( !paging_mode_translate(e) )
-                sha->full_page.frame = mfn;
+                sha->full_page.frame = mfn_x(mfn);
         }
         smp_wmb();
         shared_entry_header(e->grant_table, gop.ref)->flags |=
@@ -2230,7 +2239,7 @@ release_grant_for_copy(
     struct grant_table *rgt = rd->grant_table;
     grant_entry_header_t *sha;
     struct active_grant_entry *act;
-    unsigned long r_frame;
+    mfn_t r_frame;
     uint16_t *status;
     grant_ref_t trans_gref;
     struct domain *td;
@@ -2307,7 +2316,7 @@ static void fixup_status_for_copy_pin(const struct active_grant_entry *act,
 static int
 acquire_grant_for_copy(
     struct domain *rd, grant_ref_t gref, domid_t ldom, bool readonly,
-    unsigned long *frame, struct page_info **page,
+    mfn_t *frame, struct page_info **page,
     uint16_t *page_off, uint16_t *length, bool allow_transitive)
 {
     struct grant_table *rgt = rd->grant_table;
@@ -2319,7 +2328,7 @@ acquire_grant_for_copy(
     domid_t trans_domid;
     grant_ref_t trans_gref;
     struct domain *td;
-    unsigned long grant_frame;
+    mfn_t grant_frame;
     uint16_t trans_page_off;
     uint16_t trans_length;
     bool is_sub_page;
@@ -2420,7 +2429,8 @@ acquire_grant_for_copy(
          */
         if ( rgt->gt_version != 2 ||
              act->pin != old_pin ||
-             (old_pin && (act->domid != ldom || act->frame != grant_frame ||
+             (old_pin && (act->domid != ldom ||
+                          !mfn_eq(act->frame, grant_frame) ||
                           act->start != trans_page_off ||
                           act->length != trans_length ||
                           act->trans_domain != td ||
@@ -2512,7 +2522,7 @@ acquire_grant_for_copy(
     }
     else
     {
-        ASSERT(mfn_valid(_mfn(act->frame)));
+        ASSERT(mfn_valid(act->frame));
         *page = mfn_to_page(act->frame);
         td = page_get_owner_and_reference(*page);
         /*
@@ -2567,7 +2577,7 @@ struct gnttab_copy_buf {
 
     /* Mapped etc. */
     struct domain *domain;
-    unsigned long frame;
+    mfn_t frame;
     struct page_info *page;
     void *virt;
     bool_t read_only;
@@ -2698,15 +2708,16 @@ static int gnttab_copy_claim_buf(const struct gnttab_copy *op,
         if ( !get_page_type(buf->page, PGT_writable_page) )
         {
             if ( !buf->domain->is_dying )
-                gdprintk(XENLOG_WARNING, "Could not get writable frame %lx\n",
-                         buf->frame);
+                gdprintk(XENLOG_WARNING,
+                         "Could not get writable frame %#"PRI_mfn"\n",
+                         mfn_x(buf->frame));
             rc = GNTST_general_error;
             goto out;
         }
         buf->have_type = 1;
     }
 
-    buf->virt = map_domain_page(_mfn(buf->frame));
+    buf->virt = map_domain_page(buf->frame);
     rc = GNTST_okay;
 
  out:
@@ -3204,7 +3215,7 @@ static int cache_flush(const gnttab_cache_flush_t *cflush, grant_ref_t *cur_ref)
 {
     struct domain *d, *owner;
     struct page_info *page;
-    unsigned long mfn;
+    mfn_t mfn;
     struct active_grant_entry *act = NULL;
     void *v;
     int ret;
@@ -3223,9 +3234,9 @@ static int cache_flush(const gnttab_cache_flush_t *cflush, grant_ref_t *cur_ref)
         return -EOPNOTSUPP;
 
     d = rcu_lock_current_domain();
-    mfn = cflush->a.dev_bus_addr >> PAGE_SHIFT;
+    mfn = maddr_to_mfn(cflush->a.dev_bus_addr);
 
-    if ( !mfn_valid(_mfn(mfn)) )
+    if ( !mfn_valid(mfn) )
     {
         rcu_unlock_domain(d);
         return -EINVAL;
@@ -3253,7 +3264,7 @@ static int cache_flush(const gnttab_cache_flush_t *cflush, grant_ref_t *cur_ref)
         }
     }
 
-    v = map_domain_page(_mfn(mfn));
+    v = map_domain_page(mfn);
     v += cflush->offset;
 
     if ( (cflush->op & GNTTAB_CACHE_INVAL) && (cflush->op & GNTTAB_CACHE_CLEAN) )
@@ -3570,7 +3581,7 @@ gnttab_release_mappings(
             {
                 BUG_ON(!(act->pin & GNTPIN_devr_mask));
                 act->pin -= GNTPIN_devr_inc;
-                if ( !is_iomem_page(_mfn(act->frame)) )
+                if ( !is_iomem_page(act->frame) )
                     put_page(pg);
             }
 
@@ -3579,7 +3590,7 @@ gnttab_release_mappings(
                 BUG_ON(!(act->pin & GNTPIN_hstr_mask));
                 act->pin -= GNTPIN_hstr_inc;
                 if ( gnttab_release_host_mappings(d) &&
-                     !is_iomem_page(_mfn(act->frame)) )
+                     !is_iomem_page(act->frame) )
                     put_page(pg);
             }
         }
@@ -3589,7 +3600,7 @@ gnttab_release_mappings(
             {
                 BUG_ON(!(act->pin & GNTPIN_devw_mask));
                 act->pin -= GNTPIN_devw_inc;
-                if ( !is_iomem_page(_mfn(act->frame)) )
+                if ( !is_iomem_page(act->frame) )
                     put_page_and_type(pg);
             }
 
@@ -3598,7 +3609,7 @@ gnttab_release_mappings(
                 BUG_ON(!(act->pin & GNTPIN_hstw_mask));
                 act->pin -= GNTPIN_hstw_inc;
                 if ( gnttab_release_host_mappings(d) &&
-                     !is_iomem_page(_mfn(act->frame)) )
+                     !is_iomem_page(act->frame) )
                 {
                     if ( gnttab_host_mapping_get_page_type((map->flags &
                                                             GNTMAP_readonly),
@@ -3650,12 +3661,12 @@ void grant_table_warn_active_grants(struct domain *d)
 #ifndef NDEBUG
                    "GFN %lx, "
 #endif
-                   "MFN: %lx)\n",
+                   "MFN: %#"PRI_mfn")\n",
                    d->domain_id, ref,
 #ifndef NDEBUG
                    gfn_x(act->gfn),
 #endif
-                   act->frame);
+                   mfn_x(act->frame));
         active_entry_release(act);
     }
 
@@ -3855,9 +3866,9 @@ static void gnttab_usage_print(struct domain *rd)
 
         first = 0;
 
-        /*      [0xXXX]  ddddd 0xXXXXXX 0xXXXXXXXX      ddddd 0xXXXXXX 0xXX */
-        printk("[0x%03x]  %5d 0x%06lx 0x%08x      %5d 0x%06"PRIx64" 0x%02x\n",
-               ref, act->domid, act->frame, act->pin,
+        /*      [0xXXX]  ddddd 0xXXXXX 0xXXXXXXXX      ddddd 0xXXXXXX 0xXX */
+        printk("[0x%03x]  %5d 0x%"PRI_mfn" 0x%08x      %5d 0x%06"PRIx64" 0x%02x\n",
+               ref, act->domid, mfn_x(act->frame), act->pin,
                sha->domid, frame, status);
         active_entry_release(act);
     }
diff --git a/xen/include/asm-arm/grant_table.h b/xen/include/asm-arm/grant_table.h
index ffff18d456..4c87870f48 100644
--- a/xen/include/asm-arm/grant_table.h
+++ b/xen/include/asm-arm/grant_table.h
@@ -18,7 +18,7 @@ int create_grant_host_mapping(unsigned long gpaddr, mfn_t mfn,
 #define gnttab_host_mapping_get_page_type(ro, ld, rd) (0)
 int replace_grant_host_mapping(unsigned long gpaddr, mfn_t mfn,
                                unsigned long new_gpaddr, unsigned int flags);
-void gnttab_mark_dirty(struct domain *d, unsigned long l);
+void gnttab_mark_dirty(struct domain *d, mfn_t mfn);
 #define gnttab_create_status_page(d, t, i) do {} while (0)
 #define gnttab_status_gmfn(d, t, i) (0)
 #define gnttab_release_host_mappings(domain) 1
diff --git a/xen/include/asm-x86/grant_table.h b/xen/include/asm-x86/grant_table.h
index e40ac8e3e7..32079274fe 100644
--- a/xen/include/asm-x86/grant_table.h
+++ b/xen/include/asm-x86/grant_table.h
@@ -76,7 +76,7 @@ static inline unsigned int gnttab_dom0_max(void)
 #define gnttab_status_gmfn(d, t, i)                     \
     (mfn_to_gmfn(d, gnttab_status_mfn(t, i)))
 
-#define gnttab_mark_dirty(d, f) paging_mark_dirty((d), _mfn(f))
+#define gnttab_mark_dirty(d, f) paging_mark_dirty((d), f)
 
 static inline void gnttab_clear_flag(unsigned int nr, uint16_t *st)
 {
-- 
2.11.0


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply related	[flat|nested] 76+ messages in thread

* [PATCH v4 15/16] xen/x86: Switch mfn_to_page in x86_64/mm.c to use typesafe MFN
  2018-02-21 14:02 [PATCH v4 00/16] xen: Convert page_to_mfn and mfn_to_page to use typesafe MFN Julien Grall
                   ` (13 preceding siblings ...)
  2018-02-21 14:02 ` [PATCH v4 14/16] xen/grant: Switch common/grant_table.c to use " Julien Grall
@ 2018-02-21 14:02 ` Julien Grall
  2018-03-02 15:57   ` Jan Beulich
  2018-02-21 14:02 ` [PATCH v4 16/16] xen: Convert page_to_mfn and mfn_to_page " Julien Grall
  15 siblings, 1 reply; 76+ messages in thread
From: Julien Grall @ 2018-02-21 14:02 UTC (permalink / raw)
  To: xen-devel; +Cc: Andrew Cooper, Julien Grall, Jan Beulich

No functional change intendend.

Signed-off-by: Julien Grall <julien.grall@arm.com>

---
Cc: Jan Beulich <jbeulich@suse.com>
Cc: Andrew Cooper <andrew.cooper3@citrix.com>

    Changes in v4:
        - Patch added
---
 xen/arch/x86/x86_64/mm.c | 46 ++++++++++++++++++++++++++--------------------
 1 file changed, 26 insertions(+), 20 deletions(-)

diff --git a/xen/arch/x86/x86_64/mm.c b/xen/arch/x86/x86_64/mm.c
index cfbc8ecf16..6b679882d6 100644
--- a/xen/arch/x86/x86_64/mm.c
+++ b/xen/arch/x86/x86_64/mm.c
@@ -43,6 +43,8 @@ asm(".file \"" __FILE__ "\"");
 /* Override macros from asm/page.h to make them work with mfn_t */
 #undef page_to_mfn
 #define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
+#undef mfn_to_page
+#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
 
 unsigned int __read_mostly m2p_compat_vstart = __HYPERVISOR_COMPAT_VIRT_START;
 
@@ -160,7 +162,8 @@ static int m2p_mapped(unsigned long spfn)
 
 static int share_hotadd_m2p_table(struct mem_hotadd_info *info)
 {
-    unsigned long i, n, v, m2p_start_mfn = 0;
+    unsigned long i, n, v;
+    mfn_t m2p_start_mfn = _mfn(0);
     l3_pgentry_t l3e;
     l2_pgentry_t l2e;
 
@@ -180,15 +183,16 @@ static int share_hotadd_m2p_table(struct mem_hotadd_info *info)
             l2e = l3e_to_l2e(l3e)[l2_table_offset(v)];
             if ( !(l2e_get_flags(l2e) & _PAGE_PRESENT) )
                 continue;
-            m2p_start_mfn = l2e_get_pfn(l2e);
+            m2p_start_mfn = l2e_get_mfn(l2e);
         }
         else
             continue;
 
         for ( i = 0; i < n; i++ )
         {
-            struct page_info *page = mfn_to_page(m2p_start_mfn + i);
-            if (hotadd_mem_valid(m2p_start_mfn + i, info))
+            struct page_info *page = mfn_to_page(mfn_add(m2p_start_mfn, i));
+
+            if ( hotadd_mem_valid(mfn_x(mfn_add(m2p_start_mfn, i)), info) )
                 share_xen_page_with_privileged_guests(page, XENSHARE_readonly);
         }
     }
@@ -204,12 +208,13 @@ static int share_hotadd_m2p_table(struct mem_hotadd_info *info)
         l2e = l3e_to_l2e(l3e)[l2_table_offset(v)];
         if ( !(l2e_get_flags(l2e) & _PAGE_PRESENT) )
             continue;
-        m2p_start_mfn = l2e_get_pfn(l2e);
+        m2p_start_mfn = l2e_get_mfn(l2e);
 
         for ( i = 0; i < L1_PAGETABLE_ENTRIES; i++ )
         {
-            struct page_info *page = mfn_to_page(m2p_start_mfn + i);
-            if (hotadd_mem_valid(m2p_start_mfn + i, info))
+            struct page_info *page = mfn_to_page(mfn_add(m2p_start_mfn, i));
+
+            if ( hotadd_mem_valid(mfn_x(mfn_add(m2p_start_mfn, i)), info) )
                 share_xen_page_with_privileged_guests(page, XENSHARE_readonly);
         }
     }
@@ -720,10 +725,10 @@ static void cleanup_frame_table(struct mem_hotadd_info *info)
     unsigned long sva, eva;
     l3_pgentry_t l3e;
     l2_pgentry_t l2e;
-    unsigned long spfn, epfn;
+    mfn_t spfn, epfn;
 
-    spfn = info->spfn;
-    epfn = info->epfn;
+    spfn = _mfn(info->spfn);
+    epfn = _mfn(info->epfn);
 
     sva = (unsigned long)mfn_to_page(spfn);
     eva = (unsigned long)mfn_to_page(epfn);
@@ -795,16 +800,17 @@ static int setup_frametable_chunk(void *start, void *end,
 
 static int extend_frame_table(struct mem_hotadd_info *info)
 {
-    unsigned long cidx, nidx, eidx, spfn, epfn;
+    unsigned long cidx, nidx, eidx;
+    mfn_t spfn, epfn;
 
-    spfn = info->spfn;
-    epfn = info->epfn;
+    spfn = _mfn(info->spfn);
+    epfn = _mfn(info->epfn);
 
-    eidx = (pfn_to_pdx(epfn) + PDX_GROUP_COUNT - 1) / PDX_GROUP_COUNT;
-    nidx = cidx = pfn_to_pdx(spfn)/PDX_GROUP_COUNT;
+    eidx = (mfn_to_pdx(epfn) + PDX_GROUP_COUNT - 1) / PDX_GROUP_COUNT;
+    nidx = cidx = mfn_to_pdx(spfn)/PDX_GROUP_COUNT;
 
-    ASSERT( pfn_to_pdx(epfn) <= (DIRECTMAP_SIZE >> PAGE_SHIFT) &&
-            pfn_to_pdx(epfn) <= FRAMETABLE_NR );
+    ASSERT( mfn_to_pdx(epfn) <= (DIRECTMAP_SIZE >> PAGE_SHIFT) &&
+            mfn_to_pdx(epfn) <= FRAMETABLE_NR );
 
     if ( test_bit(cidx, pdx_group_valid) )
         cidx = find_next_zero_bit(pdx_group_valid, eidx, cidx);
@@ -866,7 +872,7 @@ void __init subarch_init_memory(void)
 
         for ( i = 0; i < n; i++ )
         {
-            struct page_info *page = mfn_to_page(m2p_start_mfn + i);
+            struct page_info *page = mfn_to_page(_mfn(m2p_start_mfn + i));
             share_xen_page_with_privileged_guests(page, XENSHARE_readonly);
         }
     }
@@ -886,7 +892,7 @@ void __init subarch_init_memory(void)
 
         for ( i = 0; i < L1_PAGETABLE_ENTRIES; i++ )
         {
-            struct page_info *page = mfn_to_page(m2p_start_mfn + i);
+            struct page_info *page = mfn_to_page(_mfn(m2p_start_mfn + i));
             share_xen_page_with_privileged_guests(page, XENSHARE_readonly);
         }
     }
@@ -1274,7 +1280,7 @@ static int transfer_pages_to_heap(struct mem_hotadd_info *info)
      */
     for (i = info->spfn; i < info->cur; i++)
     {
-        pg = mfn_to_page(i);
+        pg = mfn_to_page(_mfn(i));
         pg->count_info = PGC_state_inuse;
     }
 
-- 
2.11.0


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply related	[flat|nested] 76+ messages in thread

* [PATCH v4 16/16] xen: Convert page_to_mfn and mfn_to_page to use typesafe MFN
  2018-02-21 14:02 [PATCH v4 00/16] xen: Convert page_to_mfn and mfn_to_page to use typesafe MFN Julien Grall
                   ` (14 preceding siblings ...)
  2018-02-21 14:02 ` [PATCH v4 15/16] xen/x86: Switch mfn_to_page in x86_64/mm.c " Julien Grall
@ 2018-02-21 14:02 ` Julien Grall
  2018-02-21 14:25   ` Razvan Cojocaru
                     ` (5 more replies)
  15 siblings, 6 replies; 76+ messages in thread
From: Julien Grall @ 2018-02-21 14:02 UTC (permalink / raw)
  To: xen-devel
  Cc: Jun Nakajima, Kevin Tian, Stefano Stabellini, Wei Liu,
	Suravee Suthikulpanit, Razvan Cojocaru, George Dunlap,
	Andrew Cooper, Ian Jackson, Tim Deegan, Julien Grall,
	Tamas K Lengyel, Jan Beulich, Shane Wang, Boris Ostrovsky,
	Gang Wei, Paul Durrant

Most of the users of page_to_mfn and mfn_to_page are either overriding
the macros to make them work with mfn_t or use mfn_x/_mfn because the
rest of the function use mfn_t.

So make page_to_mfn and mfn_to_page return mfn_t by default. The __*
version are now dropped as this patch will convert all the remaining
non-typesafe callers.

Only reasonable clean-ups are done in this patch. The rest will use
_mfn/mfn_x for the time being.

Lastly, domain_page_to_mfn is also converted to use mfn_t given that
most of the callers are now switched to _mfn(domain_page_to_mfn(...)).

Signed-off-by: Julien Grall <julien.grall@arm.com>

---

Andrew suggested to drop IS_VALID_PAGE in xen/tmem_xen.h. His comment
was:

"/sigh  This is tautological.  The definition of a "valid mfn" in this
case is one for which we have frametable entry, and by having a struct
page_info in our hands, this is by definition true (unless you have a
wild pointer, at which point your bug is elsewhere).

IS_VALID_PAGE() is only ever used in assertions and never usefully, so
instead I would remove it entirely rather than trying to fix it up."

I can remove the function in a separate patch at the begining of the
series if Konrad (TMEM maintainer) is happy with that.

Cc: Stefano Stabellini <sstabellini@kernel.org>
Cc: Julien Grall <julien.grall@arm.com>
Cc: Andrew Cooper <andrew.cooper3@citrix.com>
Cc: George Dunlap <George.Dunlap@eu.citrix.com>
Cc: Ian Jackson <ian.jackson@eu.citrix.com>
Cc: Jan Beulich <jbeulich@suse.com>
Cc: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
Cc: Tim Deegan <tim@xen.org>
Cc: Wei Liu <wei.liu2@citrix.com>
Cc: Razvan Cojocaru <rcojocaru@bitdefender.com>
Cc: Tamas K Lengyel <tamas@tklengyel.com>
Cc: Paul Durrant <paul.durrant@citrix.com>
Cc: Boris Ostrovsky <boris.ostrovsky@oracle.com>
Cc: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
Cc: Jun Nakajima <jun.nakajima@intel.com>
Cc: Kevin Tian <kevin.tian@intel.com>
Cc: George Dunlap <george.dunlap@eu.citrix.com>
Cc: Gang Wei <gang.wei@intel.com>
Cc: Shane Wang <shane.wang@intel.com>

    Changes in v4:
        - Drop __page_to_mfn and __mfn_to_page. Reword the commit
        title/message to reflect that.

    Changes in v3:
        - Rebase on the latest staging and fix some conflicts. Tags
        haven't be retained.
        - Switch the printf format to PRI_mfn

    Changes in v2:
        - Some part have been moved in separate patch
        - Remove one spurious comment
        - Convert domain_page_to_mfn to use mfn_t
---
 xen/arch/arm/domain_build.c             |  2 --
 xen/arch/arm/kernel.c                   |  2 +-
 xen/arch/arm/mem_access.c               |  2 +-
 xen/arch/arm/mm.c                       |  8 ++++----
 xen/arch/arm/p2m.c                      | 10 ++--------
 xen/arch/x86/cpu/vpmu.c                 |  4 ++--
 xen/arch/x86/domain.c                   | 21 +++++++++++----------
 xen/arch/x86/domain_page.c              |  6 +++---
 xen/arch/x86/hvm/dm.c                   |  2 +-
 xen/arch/x86/hvm/dom0_build.c           |  6 +++---
 xen/arch/x86/hvm/emulate.c              |  6 +++---
 xen/arch/x86/hvm/hvm.c                  | 12 ++++++------
 xen/arch/x86/hvm/ioreq.c                |  4 ++--
 xen/arch/x86/hvm/stdvga.c               |  2 +-
 xen/arch/x86/hvm/svm/svm.c              |  4 ++--
 xen/arch/x86/hvm/viridian.c             |  6 +++---
 xen/arch/x86/hvm/vmx/vmcs.c             |  2 +-
 xen/arch/x86/hvm/vmx/vmx.c              | 10 +++++-----
 xen/arch/x86/hvm/vmx/vvmx.c             |  6 +++---
 xen/arch/x86/mm.c                       |  4 ----
 xen/arch/x86/mm/guest_walk.c            |  6 +++---
 xen/arch/x86/mm/hap/guest_walk.c        |  2 +-
 xen/arch/x86/mm/hap/hap.c               |  6 ------
 xen/arch/x86/mm/hap/nested_ept.c        |  2 +-
 xen/arch/x86/mm/mem_sharing.c           |  5 -----
 xen/arch/x86/mm/p2m-ept.c               |  8 ++++----
 xen/arch/x86/mm/p2m-pod.c               |  6 ------
 xen/arch/x86/mm/p2m.c                   |  6 ------
 xen/arch/x86/mm/paging.c                |  6 ------
 xen/arch/x86/mm/shadow/private.h        | 16 ++--------------
 xen/arch/x86/numa.c                     |  2 +-
 xen/arch/x86/physdev.c                  |  2 +-
 xen/arch/x86/pv/callback.c              |  6 ------
 xen/arch/x86/pv/descriptor-tables.c     |  6 ------
 xen/arch/x86/pv/dom0_build.c            | 14 +++++++-------
 xen/arch/x86/pv/domain.c                |  6 ------
 xen/arch/x86/pv/emul-gate-op.c          |  6 ------
 xen/arch/x86/pv/emul-priv-op.c          | 10 ----------
 xen/arch/x86/pv/grant_table.c           |  6 ------
 xen/arch/x86/pv/ro-page-fault.c         |  6 ------
 xen/arch/x86/pv/shim.c                  |  4 +---
 xen/arch/x86/smpboot.c                  |  6 ------
 xen/arch/x86/tboot.c                    |  4 ++--
 xen/arch/x86/traps.c                    |  4 ++--
 xen/arch/x86/x86_64/mm.c                |  6 ------
 xen/common/domain.c                     |  4 ++--
 xen/common/grant_table.c                |  6 ------
 xen/common/kimage.c                     |  6 ------
 xen/common/memory.c                     |  6 ------
 xen/common/page_alloc.c                 |  6 ------
 xen/common/tmem.c                       |  2 +-
 xen/common/tmem_xen.c                   |  4 ----
 xen/common/trace.c                      |  4 ++--
 xen/common/vmap.c                       |  6 +-----
 xen/common/xenoprof.c                   |  2 --
 xen/drivers/passthrough/amd/iommu_map.c | 12 ++++++------
 xen/drivers/passthrough/iommu.c         |  2 +-
 xen/drivers/passthrough/x86/iommu.c     |  2 +-
 xen/include/asm-arm/mm.h                | 20 ++++++++++----------
 xen/include/asm-arm/p2m.h               |  4 ++--
 xen/include/asm-x86/mm.h                | 12 ++++++------
 xen/include/asm-x86/p2m.h               |  2 +-
 xen/include/asm-x86/page.h              | 32 +++++++++++++++-----------------
 xen/include/xen/domain_page.h           |  8 ++++----
 xen/include/xen/mm.h                    |  5 -----
 xen/include/xen/tmem_xen.h              |  2 +-
 66 files changed, 132 insertions(+), 285 deletions(-)

diff --git a/xen/arch/arm/domain_build.c b/xen/arch/arm/domain_build.c
index 155c952349..fd6c2482de 100644
--- a/xen/arch/arm/domain_build.c
+++ b/xen/arch/arm/domain_build.c
@@ -49,8 +49,6 @@ struct map_range_data
 /* Override macros from asm/page.h to make them work with mfn_t */
 #undef virt_to_mfn
 #define virt_to_mfn(va) _mfn(__virt_to_mfn(va))
-#undef page_to_mfn
-#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
 
 //#define DEBUG_11_ALLOCATION
 #ifdef DEBUG_11_ALLOCATION
diff --git a/xen/arch/arm/kernel.c b/xen/arch/arm/kernel.c
index 2fb0b9684d..8fdfd91543 100644
--- a/xen/arch/arm/kernel.c
+++ b/xen/arch/arm/kernel.c
@@ -286,7 +286,7 @@ static __init int kernel_decompress(struct bootmodule *mod)
         iounmap(input);
         return -ENOMEM;
     }
-    mfn = _mfn(page_to_mfn(pages));
+    mfn = page_to_mfn(pages);
     output = __vmap(&mfn, 1 << kernel_order_out, 1, 1, PAGE_HYPERVISOR, VMAP_DEFAULT);
 
     rc = perform_gunzip(output, input, size);
diff --git a/xen/arch/arm/mem_access.c b/xen/arch/arm/mem_access.c
index 0f2cbb81d3..112e291cba 100644
--- a/xen/arch/arm/mem_access.c
+++ b/xen/arch/arm/mem_access.c
@@ -210,7 +210,7 @@ p2m_mem_access_check_and_get_page(vaddr_t gva, unsigned long flag,
     if ( t != p2m_ram_rw )
         goto err;
 
-    page = mfn_to_page(mfn_x(mfn));
+    page = mfn_to_page(mfn);
 
     if ( unlikely(!get_page(page, v->domain)) )
         page = NULL;
diff --git a/xen/arch/arm/mm.c b/xen/arch/arm/mm.c
index db74466a16..510a5a2050 100644
--- a/xen/arch/arm/mm.c
+++ b/xen/arch/arm/mm.c
@@ -477,7 +477,7 @@ void unmap_domain_page(const void *va)
     local_irq_restore(flags);
 }
 
-unsigned long domain_page_map_to_mfn(const void *ptr)
+mfn_t domain_page_map_to_mfn(const void *ptr)
 {
     unsigned long va = (unsigned long)ptr;
     lpae_t *map = this_cpu(xen_dommap);
@@ -485,12 +485,12 @@ unsigned long domain_page_map_to_mfn(const void *ptr)
     unsigned long offset = (va>>THIRD_SHIFT) & LPAE_ENTRY_MASK;
 
     if ( va >= VMAP_VIRT_START && va < VMAP_VIRT_END )
-        return __virt_to_mfn(va);
+        return virt_to_mfn(va);
 
     ASSERT(slot >= 0 && slot < DOMHEAP_ENTRIES);
     ASSERT(map[slot].pt.avail != 0);
 
-    return map[slot].pt.base + offset;
+    return _mfn(map[slot].pt.base + offset);
 }
 #endif
 
@@ -1287,7 +1287,7 @@ int xenmem_add_to_physmap_one(
             return -EINVAL;
         }
 
-        mfn = _mfn(page_to_mfn(page));
+        mfn = page_to_mfn(page);
         t = p2m_map_foreign;
 
         rcu_unlock_domain(od);
diff --git a/xen/arch/arm/p2m.c b/xen/arch/arm/p2m.c
index 65e8b9c6ea..8b16c8322d 100644
--- a/xen/arch/arm/p2m.c
+++ b/xen/arch/arm/p2m.c
@@ -37,12 +37,6 @@ static unsigned int __read_mostly max_vmid = MAX_VMID_8_BIT;
 
 #define P2M_ROOT_PAGES    (1<<P2M_ROOT_ORDER)
 
-/* Override macros from asm/mm.h to make them work with mfn_t */
-#undef mfn_to_page
-#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
-#undef page_to_mfn
-#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
-
 unsigned int __read_mostly p2m_ipa_bits;
 
 /* Helpers to lookup the properties of each level */
@@ -90,8 +84,8 @@ void dump_p2m_lookup(struct domain *d, paddr_t addr)
 
     printk("dom%d IPA 0x%"PRIpaddr"\n", d->domain_id, addr);
 
-    printk("P2M @ %p mfn:0x%lx\n",
-           p2m->root, __page_to_mfn(p2m->root));
+    printk("P2M @ %p mfn:%#"PRI_mfn"\n",
+           p2m->root, mfn_x(page_to_mfn(p2m->root)));
 
     dump_pt_walk(page_to_maddr(p2m->root), addr,
                  P2M_ROOT_LEVEL, P2M_ROOT_PAGES);
diff --git a/xen/arch/x86/cpu/vpmu.c b/xen/arch/x86/cpu/vpmu.c
index 7baf4614be..b978e05613 100644
--- a/xen/arch/x86/cpu/vpmu.c
+++ b/xen/arch/x86/cpu/vpmu.c
@@ -653,7 +653,7 @@ static void pvpmu_finish(struct domain *d, xen_pmu_params_t *params)
 {
     struct vcpu *v;
     struct vpmu_struct *vpmu;
-    uint64_t mfn;
+    mfn_t mfn;
     void *xenpmu_data;
 
     if ( (params->vcpu >= d->max_vcpus) || (d->vcpu[params->vcpu] == NULL) )
@@ -675,7 +675,7 @@ static void pvpmu_finish(struct domain *d, xen_pmu_params_t *params)
     if ( xenpmu_data )
     {
         mfn = domain_page_map_to_mfn(xenpmu_data);
-        ASSERT(mfn_valid(_mfn(mfn)));
+        ASSERT(mfn_valid(mfn));
         unmap_domain_page_global(xenpmu_data);
         put_page_and_type(mfn_to_page(mfn));
     }
diff --git a/xen/arch/x86/domain.c b/xen/arch/x86/domain.c
index f93327b0a2..44ba52b7ba 100644
--- a/xen/arch/x86/domain.c
+++ b/xen/arch/x86/domain.c
@@ -195,7 +195,7 @@ void dump_pageframe_info(struct domain *d)
                 }
             }
             printk("    DomPage %p: caf=%08lx, taf=%" PRtype_info "\n",
-                   _p(page_to_mfn(page)),
+                   _p(mfn_x(page_to_mfn(page))),
                    page->count_info, page->u.inuse.type_info);
         }
         spin_unlock(&d->page_alloc_lock);
@@ -208,7 +208,7 @@ void dump_pageframe_info(struct domain *d)
     page_list_for_each ( page, &d->xenpage_list )
     {
         printk("    XenPage %p: caf=%08lx, taf=%" PRtype_info "\n",
-               _p(page_to_mfn(page)),
+               _p(mfn_x(page_to_mfn(page))),
                page->count_info, page->u.inuse.type_info);
     }
     spin_unlock(&d->page_alloc_lock);
@@ -635,7 +635,8 @@ int arch_domain_soft_reset(struct domain *d)
     struct page_info *page = virt_to_page(d->shared_info), *new_page;
     int ret = 0;
     struct domain *owner;
-    unsigned long mfn, gfn;
+    mfn_t mfn;
+    unsigned long gfn;
     p2m_type_t p2mt;
     unsigned int i;
 
@@ -669,7 +670,7 @@ int arch_domain_soft_reset(struct domain *d)
     ASSERT( owner == d );
 
     mfn = page_to_mfn(page);
-    gfn = mfn_to_gmfn(d, mfn);
+    gfn = mfn_to_gmfn(d, mfn_x(mfn));
 
     /*
      * gfn == INVALID_GFN indicates that the shared_info page was never mapped
@@ -678,7 +679,7 @@ int arch_domain_soft_reset(struct domain *d)
     if ( gfn == gfn_x(INVALID_GFN) )
         goto exit_put_page;
 
-    if ( mfn_x(get_gfn_query(d, gfn, &p2mt)) != mfn )
+    if ( !mfn_eq(get_gfn_query(d, gfn, &p2mt), mfn) )
     {
         printk(XENLOG_G_ERR "Failed to get Dom%d's shared_info GFN (%lx)\n",
                d->domain_id, gfn);
@@ -695,7 +696,7 @@ int arch_domain_soft_reset(struct domain *d)
         goto exit_put_gfn;
     }
 
-    ret = guest_physmap_remove_page(d, _gfn(gfn), _mfn(mfn), PAGE_ORDER_4K);
+    ret = guest_physmap_remove_page(d, _gfn(gfn), mfn, PAGE_ORDER_4K);
     if ( ret )
     {
         printk(XENLOG_G_ERR "Failed to remove Dom%d's shared_info frame %lx\n",
@@ -704,7 +705,7 @@ int arch_domain_soft_reset(struct domain *d)
         goto exit_put_gfn;
     }
 
-    ret = guest_physmap_add_page(d, _gfn(gfn), _mfn(page_to_mfn(new_page)),
+    ret = guest_physmap_add_page(d, _gfn(gfn), page_to_mfn(new_page),
                                  PAGE_ORDER_4K);
     if ( ret )
     {
@@ -1002,7 +1003,7 @@ int arch_set_info_guest(
                 {
                     if ( (page->u.inuse.type_info & PGT_type_mask) ==
                          PGT_l4_page_table )
-                        done = !fill_ro_mpt(_mfn(page_to_mfn(page)));
+                        done = !fill_ro_mpt(page_to_mfn(page));
 
                     page_unlock(page);
                 }
@@ -1131,7 +1132,7 @@ int arch_set_info_guest(
         l4_pgentry_t *l4tab;
 
         l4tab = map_domain_page(pagetable_get_mfn(v->arch.guest_table));
-        *l4tab = l4e_from_pfn(page_to_mfn(cr3_page),
+        *l4tab = l4e_from_mfn(page_to_mfn(cr3_page),
             _PAGE_PRESENT|_PAGE_RW|_PAGE_USER|_PAGE_ACCESSED);
         unmap_domain_page(l4tab);
     }
@@ -1997,7 +1998,7 @@ int domain_relinquish_resources(struct domain *d)
         if ( d->arch.pirq_eoi_map != NULL )
         {
             unmap_domain_page_global(d->arch.pirq_eoi_map);
-            put_page_and_type(mfn_to_page(d->arch.pirq_eoi_map_mfn));
+            put_page_and_type(mfn_to_page(_mfn(d->arch.pirq_eoi_map_mfn)));
             d->arch.pirq_eoi_map = NULL;
             d->arch.auto_unmask = 0;
         }
diff --git a/xen/arch/x86/domain_page.c b/xen/arch/x86/domain_page.c
index 3432a854dd..88046b39c9 100644
--- a/xen/arch/x86/domain_page.c
+++ b/xen/arch/x86/domain_page.c
@@ -331,13 +331,13 @@ void unmap_domain_page_global(const void *ptr)
 }
 
 /* Translate a map-domain-page'd address to the underlying MFN */
-unsigned long domain_page_map_to_mfn(const void *ptr)
+mfn_t domain_page_map_to_mfn(const void *ptr)
 {
     unsigned long va = (unsigned long)ptr;
     const l1_pgentry_t *pl1e;
 
     if ( va >= DIRECTMAP_VIRT_START )
-        return virt_to_mfn(ptr);
+        return _mfn(virt_to_mfn(ptr));
 
     if ( va >= VMAP_VIRT_START && va < VMAP_VIRT_END )
     {
@@ -350,5 +350,5 @@ unsigned long domain_page_map_to_mfn(const void *ptr)
         pl1e = &__linear_l1_table[l1_linear_offset(va)];
     }
 
-    return l1e_get_pfn(*pl1e);
+    return l1e_get_mfn(*pl1e);
 }
diff --git a/xen/arch/x86/hvm/dm.c b/xen/arch/x86/hvm/dm.c
index 7788577a73..cf1e600998 100644
--- a/xen/arch/x86/hvm/dm.c
+++ b/xen/arch/x86/hvm/dm.c
@@ -193,7 +193,7 @@ static int modified_memory(struct domain *d,
                  * These are most probably not page tables any more
                  * don't take a long time and don't die either.
                  */
-                sh_remove_shadows(d, _mfn(page_to_mfn(page)), 1, 0);
+                sh_remove_shadows(d, page_to_mfn(page), 1, 0);
                 put_page(page);
             }
         }
diff --git a/xen/arch/x86/hvm/dom0_build.c b/xen/arch/x86/hvm/dom0_build.c
index afebaec70b..717ffff584 100644
--- a/xen/arch/x86/hvm/dom0_build.c
+++ b/xen/arch/x86/hvm/dom0_build.c
@@ -119,7 +119,7 @@ static int __init pvh_populate_memory_range(struct domain *d,
             continue;
         }
 
-        rc = guest_physmap_add_page(d, _gfn(start), _mfn(page_to_mfn(page)),
+        rc = guest_physmap_add_page(d, _gfn(start), page_to_mfn(page),
                                     order);
         if ( rc != 0 )
         {
@@ -269,7 +269,7 @@ static int __init pvh_setup_vmx_realmode_helpers(struct domain *d)
     }
     write_32bit_pse_identmap(ident_pt);
     unmap_domain_page(ident_pt);
-    put_page(mfn_to_page(mfn_x(mfn)));
+    put_page(mfn_to_page(mfn));
     d->arch.hvm_domain.params[HVM_PARAM_IDENT_PT] = gaddr;
     if ( pvh_add_mem_range(d, gaddr, gaddr + PAGE_SIZE, E820_RESERVED) )
             printk("Unable to set identity page tables as reserved in the memory map\n");
@@ -287,7 +287,7 @@ static void __init pvh_steal_low_ram(struct domain *d, unsigned long start,
 
     for ( mfn = start; mfn < start + nr_pages; mfn++ )
     {
-        struct page_info *pg = mfn_to_page(mfn);
+        struct page_info *pg = mfn_to_page(_mfn(mfn));
         int rc;
 
         rc = unshare_xen_page_with_guest(pg, dom_io);
diff --git a/xen/arch/x86/hvm/emulate.c b/xen/arch/x86/hvm/emulate.c
index 3b824553ab..fdd7177303 100644
--- a/xen/arch/x86/hvm/emulate.c
+++ b/xen/arch/x86/hvm/emulate.c
@@ -591,7 +591,7 @@ static void *hvmemul_map_linear_addr(
             goto unhandleable;
         }
 
-        *mfn++ = _mfn(page_to_mfn(page));
+        *mfn++ = page_to_mfn(page);
 
         if ( p2m_is_discard_write(p2mt) )
         {
@@ -623,7 +623,7 @@ static void *hvmemul_map_linear_addr(
  out:
     /* Drop all held references. */
     while ( mfn-- > hvmemul_ctxt->mfn )
-        put_page(mfn_to_page(mfn_x(*mfn)));
+        put_page(mfn_to_page(*mfn));
 
     return err;
 }
@@ -649,7 +649,7 @@ static void hvmemul_unmap_linear_addr(
     {
         ASSERT(mfn_valid(*mfn));
         paging_mark_dirty(currd, *mfn);
-        put_page(mfn_to_page(mfn_x(*mfn)));
+        put_page(mfn_to_page(*mfn));
 
         *mfn++ = _mfn(0); /* Clean slot for map()'s error checking. */
     }
diff --git a/xen/arch/x86/hvm/hvm.c b/xen/arch/x86/hvm/hvm.c
index 91bc3e8b27..74de968315 100644
--- a/xen/arch/x86/hvm/hvm.c
+++ b/xen/arch/x86/hvm/hvm.c
@@ -2247,7 +2247,7 @@ int hvm_set_cr0(unsigned long value, bool_t may_defer)
             v->arch.guest_table = pagetable_from_page(page);
 
             HVM_DBG_LOG(DBG_LEVEL_VMMU, "Update CR3 value = %lx, mfn = %lx",
-                        v->arch.hvm_vcpu.guest_cr[3], page_to_mfn(page));
+                        v->arch.hvm_vcpu.guest_cr[3], mfn_x(page_to_mfn(page)));
         }
     }
     else if ( !(value & X86_CR0_PG) && (old_value & X86_CR0_PG) )
@@ -2624,7 +2624,7 @@ void *hvm_map_guest_frame_ro(unsigned long gfn, bool_t permanent)
 
 void hvm_unmap_guest_frame(void *p, bool_t permanent)
 {
-    unsigned long mfn;
+    mfn_t mfn;
     struct page_info *page;
 
     if ( !p )
@@ -2645,7 +2645,7 @@ void hvm_unmap_guest_frame(void *p, bool_t permanent)
         list_for_each_entry(track, &d->arch.hvm_domain.write_map.list, list)
             if ( track->page == page )
             {
-                paging_mark_dirty(d, _mfn(mfn));
+                paging_mark_dirty(d, mfn);
                 list_del(&track->list);
                 xfree(track);
                 break;
@@ -2662,7 +2662,7 @@ void hvm_mapped_guest_frames_mark_dirty(struct domain *d)
 
     spin_lock(&d->arch.hvm_domain.write_map.lock);
     list_for_each_entry(track, &d->arch.hvm_domain.write_map.list, list)
-        paging_mark_dirty(d, _mfn(page_to_mfn(track->page)));
+        paging_mark_dirty(d, page_to_mfn(track->page));
     spin_unlock(&d->arch.hvm_domain.write_map.lock);
 }
 
@@ -3236,8 +3236,8 @@ static enum hvm_translation_result __hvm_copy(
 
                 if ( xchg(&lastpage, gfn_x(gfn)) != gfn_x(gfn) )
                     dprintk(XENLOG_G_DEBUG,
-                            "%pv attempted write to read-only gfn %#lx (mfn=%#lx)\n",
-                            v, gfn_x(gfn), page_to_mfn(page));
+                            "%pv attempted write to read-only gfn %#lx (mfn=%#"PRI_mfn")\n",
+                            v, gfn_x(gfn), mfn_x(page_to_mfn(page)));
             }
             else
             {
diff --git a/xen/arch/x86/hvm/ioreq.c b/xen/arch/x86/hvm/ioreq.c
index 7e66965bcd..a1c2218fdc 100644
--- a/xen/arch/x86/hvm/ioreq.c
+++ b/xen/arch/x86/hvm/ioreq.c
@@ -268,7 +268,7 @@ static void hvm_remove_ioreq_gfn(
     struct domain *d, struct hvm_ioreq_page *iorp)
 {
     if ( guest_physmap_remove_page(d, _gfn(iorp->gfn),
-                                   _mfn(page_to_mfn(iorp->page)), 0) )
+                                   page_to_mfn(iorp->page), 0) )
         domain_crash(d);
     clear_page(iorp->va);
 }
@@ -281,7 +281,7 @@ static int hvm_add_ioreq_gfn(
     clear_page(iorp->va);
 
     rc = guest_physmap_add_page(d, _gfn(iorp->gfn),
-                                _mfn(page_to_mfn(iorp->page)), 0);
+                                page_to_mfn(iorp->page), 0);
     if ( rc == 0 )
         paging_mark_pfn_dirty(d, _pfn(iorp->gfn));
 
diff --git a/xen/arch/x86/hvm/stdvga.c b/xen/arch/x86/hvm/stdvga.c
index 088fbdf8ce..925bab2438 100644
--- a/xen/arch/x86/hvm/stdvga.c
+++ b/xen/arch/x86/hvm/stdvga.c
@@ -590,7 +590,7 @@ void stdvga_init(struct domain *d)
         if ( pg == NULL )
             break;
         s->vram_page[i] = pg;
-        clear_domain_page(_mfn(page_to_mfn(pg)));
+        clear_domain_page(page_to_mfn(pg));
     }
 
     if ( i == ARRAY_SIZE(s->vram_page) )
diff --git a/xen/arch/x86/hvm/svm/svm.c b/xen/arch/x86/hvm/svm/svm.c
index 9f58afc2d8..30e951039e 100644
--- a/xen/arch/x86/hvm/svm/svm.c
+++ b/xen/arch/x86/hvm/svm/svm.c
@@ -1543,7 +1543,7 @@ static int svm_cpu_up_prepare(unsigned int cpu)
         if ( !pg )
             goto err;
 
-        clear_domain_page(_mfn(page_to_mfn(pg)));
+        clear_domain_page(page_to_mfn(pg));
         *this_hsa = page_to_maddr(pg);
     }
 
@@ -1553,7 +1553,7 @@ static int svm_cpu_up_prepare(unsigned int cpu)
         if ( !pg )
             goto err;
 
-        clear_domain_page(_mfn(page_to_mfn(pg)));
+        clear_domain_page(page_to_mfn(pg));
         *this_vmcb = page_to_maddr(pg);
     }
 
diff --git a/xen/arch/x86/hvm/viridian.c b/xen/arch/x86/hvm/viridian.c
index 70aab520bc..d6aa89d0b7 100644
--- a/xen/arch/x86/hvm/viridian.c
+++ b/xen/arch/x86/hvm/viridian.c
@@ -354,7 +354,7 @@ static void enable_hypercall_page(struct domain *d)
         if ( page )
             put_page(page);
         gdprintk(XENLOG_WARNING, "Bad GMFN %#"PRI_gfn" (MFN %#"PRI_mfn")\n",
-                 gmfn, page ? page_to_mfn(page) : mfn_x(INVALID_MFN));
+                 gmfn, mfn_x(page ? page_to_mfn(page) : INVALID_MFN));
         return;
     }
 
@@ -414,7 +414,7 @@ static void initialize_vp_assist(struct vcpu *v)
 
  fail:
     gdprintk(XENLOG_WARNING, "Bad GMFN %#"PRI_gfn" (MFN %#"PRI_mfn")\n", gmfn,
-             page ? page_to_mfn(page) : mfn_x(INVALID_MFN));
+             mfn_x(page ? page_to_mfn(page) : INVALID_MFN));
 }
 
 static void teardown_vp_assist(struct vcpu *v)
@@ -492,7 +492,7 @@ static void update_reference_tsc(struct domain *d, bool_t initialize)
         if ( page )
             put_page(page);
         gdprintk(XENLOG_WARNING, "Bad GMFN %#"PRI_gfn" (MFN %#"PRI_mfn")\n",
-                 gmfn, page ? page_to_mfn(page) : mfn_x(INVALID_MFN));
+                 gmfn, mfn_x(page ? page_to_mfn(page) : INVALID_MFN));
         return;
     }
 
diff --git a/xen/arch/x86/hvm/vmx/vmcs.c b/xen/arch/x86/hvm/vmx/vmcs.c
index e7818caed0..a5a160746a 100644
--- a/xen/arch/x86/hvm/vmx/vmcs.c
+++ b/xen/arch/x86/hvm/vmx/vmcs.c
@@ -1433,7 +1433,7 @@ int vmx_vcpu_enable_pml(struct vcpu *v)
 
     vmx_vmcs_enter(v);
 
-    __vmwrite(PML_ADDRESS, page_to_mfn(v->arch.hvm_vmx.pml_pg) << PAGE_SHIFT);
+    __vmwrite(PML_ADDRESS, page_to_maddr(v->arch.hvm_vmx.pml_pg));
     __vmwrite(GUEST_PML_INDEX, NR_PML_ENTRIES - 1);
 
     v->arch.hvm_vmx.secondary_exec_control |= SECONDARY_EXEC_ENABLE_PML;
diff --git a/xen/arch/x86/hvm/vmx/vmx.c b/xen/arch/x86/hvm/vmx/vmx.c
index 5cd689e823..cf7f7e1bb7 100644
--- a/xen/arch/x86/hvm/vmx/vmx.c
+++ b/xen/arch/x86/hvm/vmx/vmx.c
@@ -2968,7 +2968,7 @@ gp_fault:
 static int vmx_alloc_vlapic_mapping(struct domain *d)
 {
     struct page_info *pg;
-    unsigned long mfn;
+    mfn_t mfn;
 
     if ( !cpu_has_vmx_virtualize_apic_accesses )
         return 0;
@@ -2977,10 +2977,10 @@ static int vmx_alloc_vlapic_mapping(struct domain *d)
     if ( !pg )
         return -ENOMEM;
     mfn = page_to_mfn(pg);
-    clear_domain_page(_mfn(mfn));
+    clear_domain_page(mfn);
     share_xen_page_with_guest(pg, d, XENSHARE_writable);
-    d->arch.hvm_domain.vmx.apic_access_mfn = mfn;
-    set_mmio_p2m_entry(d, paddr_to_pfn(APIC_DEFAULT_PHYS_BASE), _mfn(mfn),
+    d->arch.hvm_domain.vmx.apic_access_mfn = mfn_x(mfn);
+    set_mmio_p2m_entry(d, paddr_to_pfn(APIC_DEFAULT_PHYS_BASE), mfn,
                        PAGE_ORDER_4K, p2m_get_hostp2m(d)->default_access);
 
     return 0;
@@ -2991,7 +2991,7 @@ static void vmx_free_vlapic_mapping(struct domain *d)
     unsigned long mfn = d->arch.hvm_domain.vmx.apic_access_mfn;
 
     if ( mfn != 0 )
-        free_shared_domheap_page(mfn_to_page(mfn));
+        free_shared_domheap_page(mfn_to_page(_mfn(mfn)));
 }
 
 static void vmx_install_vlapic_mapping(struct vcpu *v)
diff --git a/xen/arch/x86/hvm/vmx/vvmx.c b/xen/arch/x86/hvm/vmx/vvmx.c
index dfe97b9705..db92ae6660 100644
--- a/xen/arch/x86/hvm/vmx/vvmx.c
+++ b/xen/arch/x86/hvm/vmx/vvmx.c
@@ -84,7 +84,7 @@ int nvmx_vcpu_initialise(struct vcpu *v)
         }
         v->arch.hvm_vmx.vmread_bitmap = vmread_bitmap;
 
-        clear_domain_page(_mfn(page_to_mfn(vmread_bitmap)));
+        clear_domain_page(page_to_mfn(vmread_bitmap));
 
         vmwrite_bitmap = alloc_domheap_page(NULL, 0);
         if ( !vmwrite_bitmap )
@@ -1726,7 +1726,7 @@ int nvmx_handle_vmptrld(struct cpu_user_regs *regs)
                 nvcpu->nv_vvmcx = vvmcx;
                 nvcpu->nv_vvmcxaddr = gpa;
                 v->arch.hvm_vmx.vmcs_shadow_maddr =
-                    pfn_to_paddr(domain_page_map_to_mfn(vvmcx));
+                    mfn_to_maddr(domain_page_map_to_mfn(vvmcx));
             }
             else
             {
@@ -1812,7 +1812,7 @@ int nvmx_handle_vmclear(struct cpu_user_regs *regs)
         {
             if ( writable )
                 clear_vvmcs_launched(&nvmx->launched_list,
-                                     domain_page_map_to_mfn(vvmcs));
+                                     mfn_x(domain_page_map_to_mfn(vvmcs)));
             else
                 rc = VMFAIL_VALID;
             hvm_unmap_guest_frame(vvmcs, 0);
diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c
index d33b6bfa9d..eeaf03eb49 100644
--- a/xen/arch/x86/mm.c
+++ b/xen/arch/x86/mm.c
@@ -131,10 +131,6 @@
 #include "pv/mm.h"
 
 /* Override macros from asm/page.h to make them work with mfn_t */
-#undef mfn_to_page
-#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
-#undef page_to_mfn
-#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
 #undef virt_to_mfn
 #define virt_to_mfn(v) _mfn(__virt_to_mfn(v))
 
diff --git a/xen/arch/x86/mm/guest_walk.c b/xen/arch/x86/mm/guest_walk.c
index 6055fec1ad..f67aeda3d0 100644
--- a/xen/arch/x86/mm/guest_walk.c
+++ b/xen/arch/x86/mm/guest_walk.c
@@ -469,20 +469,20 @@ guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m,
     if ( l3p )
     {
         unmap_domain_page(l3p);
-        put_page(mfn_to_page(mfn_x(gw->l3mfn)));
+        put_page(mfn_to_page(gw->l3mfn));
     }
 #endif
 #if GUEST_PAGING_LEVELS >= 3
     if ( l2p )
     {
         unmap_domain_page(l2p);
-        put_page(mfn_to_page(mfn_x(gw->l2mfn)));
+        put_page(mfn_to_page(gw->l2mfn));
     }
 #endif
     if ( l1p )
     {
         unmap_domain_page(l1p);
-        put_page(mfn_to_page(mfn_x(gw->l1mfn)));
+        put_page(mfn_to_page(gw->l1mfn));
     }
 
     return walk_ok;
diff --git a/xen/arch/x86/mm/hap/guest_walk.c b/xen/arch/x86/mm/hap/guest_walk.c
index c550017ba4..cb3f9cebe7 100644
--- a/xen/arch/x86/mm/hap/guest_walk.c
+++ b/xen/arch/x86/mm/hap/guest_walk.c
@@ -83,7 +83,7 @@ unsigned long hap_p2m_ga_to_gfn(GUEST_PAGING_LEVELS)(
         *pfec &= ~PFEC_page_present;
         goto out_tweak_pfec;
     }
-    top_mfn = _mfn(page_to_mfn(top_page));
+    top_mfn = page_to_mfn(top_page);
 
     /* Map the top-level table and call the tree-walker */
     ASSERT(mfn_valid(top_mfn));
diff --git a/xen/arch/x86/mm/hap/hap.c b/xen/arch/x86/mm/hap/hap.c
index 003c2d8896..370a490aad 100644
--- a/xen/arch/x86/mm/hap/hap.c
+++ b/xen/arch/x86/mm/hap/hap.c
@@ -42,12 +42,6 @@
 
 #include "private.h"
 
-/* Override macros from asm/page.h to make them work with mfn_t */
-#undef mfn_to_page
-#define mfn_to_page(_m) __mfn_to_page(mfn_x(_m))
-#undef page_to_mfn
-#define page_to_mfn(_pg) _mfn(__page_to_mfn(_pg))
-
 /************************************************/
 /*          HAP VRAM TRACKING SUPPORT           */
 /************************************************/
diff --git a/xen/arch/x86/mm/hap/nested_ept.c b/xen/arch/x86/mm/hap/nested_ept.c
index 14b1bb01e9..1738df69f6 100644
--- a/xen/arch/x86/mm/hap/nested_ept.c
+++ b/xen/arch/x86/mm/hap/nested_ept.c
@@ -173,7 +173,7 @@ nept_walk_tables(struct vcpu *v, unsigned long l2ga, ept_walk_t *gw)
             goto map_err;
         gw->lxe[lvl] = lxp[ept_lvl_table_offset(l2ga, lvl)];
         unmap_domain_page(lxp);
-        put_page(mfn_to_page(mfn_x(lxmfn)));
+        put_page(mfn_to_page(lxmfn));
 
         if ( nept_non_present_check(gw->lxe[lvl]) )
             goto non_present;
diff --git a/xen/arch/x86/mm/mem_sharing.c b/xen/arch/x86/mm/mem_sharing.c
index 57f54c55c8..fad8a9df13 100644
--- a/xen/arch/x86/mm/mem_sharing.c
+++ b/xen/arch/x86/mm/mem_sharing.c
@@ -152,11 +152,6 @@ static inline shr_handle_t get_next_handle(void)
 #define mem_sharing_enabled(d) \
     (is_hvm_domain(d) && (d)->arch.hvm_domain.mem_sharing_enabled)
 
-#undef mfn_to_page
-#define mfn_to_page(_m) __mfn_to_page(mfn_x(_m))
-#undef page_to_mfn
-#define page_to_mfn(_pg) _mfn(__page_to_mfn(_pg))
-
 static atomic_t nr_saved_mfns   = ATOMIC_INIT(0); 
 static atomic_t nr_shared_mfns  = ATOMIC_INIT(0);
 
diff --git a/xen/arch/x86/mm/p2m-ept.c b/xen/arch/x86/mm/p2m-ept.c
index 66dbb3e83a..14b593923b 100644
--- a/xen/arch/x86/mm/p2m-ept.c
+++ b/xen/arch/x86/mm/p2m-ept.c
@@ -74,13 +74,13 @@ static int atomic_write_ept_entry(ept_entry_t *entryptr, ept_entry_t new,
                 goto out;
 
             rc = -ESRCH;
-            fdom = page_get_owner(mfn_to_page(new.mfn));
+            fdom = page_get_owner(mfn_to_page(_mfn(new.mfn)));
             if ( fdom == NULL )
                 goto out;
 
             /* get refcount on the page */
             rc = -EBUSY;
-            if ( !get_page(mfn_to_page(new.mfn), fdom) )
+            if ( !get_page(mfn_to_page(_mfn(new.mfn)), fdom) )
                 goto out;
         }
     }
@@ -91,7 +91,7 @@ static int atomic_write_ept_entry(ept_entry_t *entryptr, ept_entry_t new,
     write_atomic(&entryptr->epte, new.epte);
 
     if ( unlikely(oldmfn != mfn_x(INVALID_MFN)) )
-        put_page(mfn_to_page(oldmfn));
+        put_page(mfn_to_page(_mfn(oldmfn)));
 
     rc = 0;
 
@@ -270,7 +270,7 @@ static void ept_free_entry(struct p2m_domain *p2m, ept_entry_t *ept_entry, int l
     }
     
     p2m_tlb_flush_sync(p2m);
-    p2m_free_ptp(p2m, mfn_to_page(ept_entry->mfn));
+    p2m_free_ptp(p2m, mfn_to_page(_mfn(ept_entry->mfn)));
 }
 
 static bool_t ept_split_super_page(struct p2m_domain *p2m,
diff --git a/xen/arch/x86/mm/p2m-pod.c b/xen/arch/x86/mm/p2m-pod.c
index fa13e07f7c..631e9aec33 100644
--- a/xen/arch/x86/mm/p2m-pod.c
+++ b/xen/arch/x86/mm/p2m-pod.c
@@ -29,12 +29,6 @@
 
 #include "mm-locks.h"
 
-/* Override macros from asm/page.h to make them work with mfn_t */
-#undef mfn_to_page
-#define mfn_to_page(_m) __mfn_to_page(mfn_x(_m))
-#undef page_to_mfn
-#define page_to_mfn(_pg) _mfn(__page_to_mfn(_pg))
-
 #define superpage_aligned(_x)  (((_x)&(SUPERPAGE_PAGES-1))==0)
 
 /* Enforce lock ordering when grabbing the "external" page_alloc lock */
diff --git a/xen/arch/x86/mm/p2m.c b/xen/arch/x86/mm/p2m.c
index 48e50fb5d8..9ce0a5c9e1 100644
--- a/xen/arch/x86/mm/p2m.c
+++ b/xen/arch/x86/mm/p2m.c
@@ -47,12 +47,6 @@ bool_t __initdata opt_hap_1gb = 1, __initdata opt_hap_2mb = 1;
 boolean_param("hap_1gb", opt_hap_1gb);
 boolean_param("hap_2mb", opt_hap_2mb);
 
-/* Override macros from asm/page.h to make them work with mfn_t */
-#undef mfn_to_page
-#define mfn_to_page(_m) __mfn_to_page(mfn_x(_m))
-#undef page_to_mfn
-#define page_to_mfn(_pg) _mfn(__page_to_mfn(_pg))
-
 DEFINE_PERCPU_RWLOCK_GLOBAL(p2m_percpu_rwlock);
 
 /* Init the datastructures for later use by the p2m code */
diff --git a/xen/arch/x86/mm/paging.c b/xen/arch/x86/mm/paging.c
index 8a658b9118..2b0445ffe9 100644
--- a/xen/arch/x86/mm/paging.c
+++ b/xen/arch/x86/mm/paging.c
@@ -47,12 +47,6 @@
 /* Per-CPU variable for enforcing the lock ordering */
 DEFINE_PER_CPU(int, mm_lock_level);
 
-/* Override macros from asm/page.h to make them work with mfn_t */
-#undef mfn_to_page
-#define mfn_to_page(_m) __mfn_to_page(mfn_x(_m))
-#undef page_to_mfn
-#define page_to_mfn(_pg) _mfn(__page_to_mfn(_pg))
-
 /************************************************/
 /*              LOG DIRTY SUPPORT               */
 /************************************************/
diff --git a/xen/arch/x86/mm/shadow/private.h b/xen/arch/x86/mm/shadow/private.h
index 845541fe8a..ea0ad28c05 100644
--- a/xen/arch/x86/mm/shadow/private.h
+++ b/xen/arch/x86/mm/shadow/private.h
@@ -315,7 +315,7 @@ static inline int page_is_out_of_sync(struct page_info *p)
 
 static inline int mfn_is_out_of_sync(mfn_t gmfn)
 {
-    return page_is_out_of_sync(mfn_to_page(mfn_x(gmfn)));
+    return page_is_out_of_sync(mfn_to_page(gmfn));
 }
 
 static inline int page_oos_may_write(struct page_info *p)
@@ -326,7 +326,7 @@ static inline int page_oos_may_write(struct page_info *p)
 
 static inline int mfn_oos_may_write(mfn_t gmfn)
 {
-    return page_oos_may_write(mfn_to_page(mfn_x(gmfn)));
+    return page_oos_may_write(mfn_to_page(gmfn));
 }
 #endif /* (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC) */
 
@@ -465,18 +465,6 @@ void sh_reset_l3_up_pointers(struct vcpu *v);
  * MFN/page-info handling
  */
 
-/* Override macros from asm/page.h to make them work with mfn_t */
-#undef mfn_to_page
-#define mfn_to_page(_m) __mfn_to_page(mfn_x(_m))
-#undef page_to_mfn
-#define page_to_mfn(_pg) _mfn(__page_to_mfn(_pg))
-
-/* Override pagetable_t <-> struct page_info conversions to work with mfn_t */
-#undef pagetable_get_page
-#define pagetable_get_page(x)   mfn_to_page(pagetable_get_mfn(x))
-#undef pagetable_from_page
-#define pagetable_from_page(pg) pagetable_from_mfn(page_to_mfn(pg))
-
 #define backpointer(sp) _mfn(pdx_to_pfn((unsigned long)(sp)->v.sh.back))
 static inline unsigned long __backpointer(const struct page_info *sp)
 {
diff --git a/xen/arch/x86/numa.c b/xen/arch/x86/numa.c
index 4fc967f893..a87987da6f 100644
--- a/xen/arch/x86/numa.c
+++ b/xen/arch/x86/numa.c
@@ -430,7 +430,7 @@ static void dump_numa(unsigned char key)
         spin_lock(&d->page_alloc_lock);
         page_list_for_each(page, &d->page_list)
         {
-            i = phys_to_nid((paddr_t)page_to_mfn(page) << PAGE_SHIFT);
+            i = phys_to_nid(page_to_maddr(page));
             page_num_node[i]++;
         }
         spin_unlock(&d->page_alloc_lock);
diff --git a/xen/arch/x86/physdev.c b/xen/arch/x86/physdev.c
index 380d36f6b9..7bfa0f23f0 100644
--- a/xen/arch/x86/physdev.c
+++ b/xen/arch/x86/physdev.c
@@ -239,7 +239,7 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
         }
 
         if ( cmpxchg(&currd->arch.pirq_eoi_map_mfn,
-                     0, page_to_mfn(page)) != 0 )
+                     0, mfn_x(page_to_mfn(page))) != 0 )
         {
             put_page_and_type(page);
             ret = -EBUSY;
diff --git a/xen/arch/x86/pv/callback.c b/xen/arch/x86/pv/callback.c
index 97d8438600..5957cb5085 100644
--- a/xen/arch/x86/pv/callback.c
+++ b/xen/arch/x86/pv/callback.c
@@ -31,12 +31,6 @@
 
 #include <public/callback.h>
 
-/* Override macros from asm/page.h to make them work with mfn_t */
-#undef mfn_to_page
-#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
-#undef page_to_mfn
-#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
-
 static int register_guest_nmi_callback(unsigned long address)
 {
     struct vcpu *curr = current;
diff --git a/xen/arch/x86/pv/descriptor-tables.c b/xen/arch/x86/pv/descriptor-tables.c
index b418bbb581..71bf92713e 100644
--- a/xen/arch/x86/pv/descriptor-tables.c
+++ b/xen/arch/x86/pv/descriptor-tables.c
@@ -25,12 +25,6 @@
 #include <asm/p2m.h>
 #include <asm/pv/mm.h>
 
-/* Override macros from asm/page.h to make them work with mfn_t */
-#undef mfn_to_page
-#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
-#undef page_to_mfn
-#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
-
 /*
  * Flush the LDT, dropping any typerefs.  Returns a boolean indicating whether
  * mappings have been removed (i.e. a TLB flush is needed).
diff --git a/xen/arch/x86/pv/dom0_build.c b/xen/arch/x86/pv/dom0_build.c
index 0bd2f1bf90..5b4325b87f 100644
--- a/xen/arch/x86/pv/dom0_build.c
+++ b/xen/arch/x86/pv/dom0_build.c
@@ -64,7 +64,7 @@ static __init void mark_pv_pt_pages_rdonly(struct domain *d,
     for ( count = 0; count < nr_pt_pages; count++ )
     {
         l1e_remove_flags(*pl1e, _PAGE_RW);
-        page = mfn_to_page(l1e_get_pfn(*pl1e));
+        page = mfn_to_page(l1e_get_mfn(*pl1e));
 
         /* Read-only mapping + PGC_allocated + page-table page. */
         page->count_info         = PGC_allocated | 3;
@@ -496,7 +496,7 @@ int __init dom0_construct_pv(struct domain *d,
     page = alloc_domheap_pages(d, order, 0);
     if ( page == NULL )
         panic("Not enough RAM for domain 0 allocation");
-    alloc_spfn = page_to_mfn(page);
+    alloc_spfn = mfn_x(page_to_mfn(page));
     alloc_epfn = alloc_spfn + d->tot_pages;
 
     if ( initrd_len )
@@ -524,12 +524,12 @@ int __init dom0_construct_pv(struct domain *d,
             mpt_alloc = (paddr_t)initrd->mod_start << PAGE_SHIFT;
             init_domheap_pages(mpt_alloc,
                                mpt_alloc + PAGE_ALIGN(initrd_len));
-            initrd->mod_start = initrd_mfn = page_to_mfn(page);
+            initrd->mod_start = initrd_mfn = mfn_x(page_to_mfn(page));
         }
         else
         {
             while ( count-- )
-                if ( assign_pages(d, mfn_to_page(mfn++), 0, 0) )
+                if ( assign_pages(d, mfn_to_page(_mfn(mfn++)), 0, 0) )
                     BUG();
         }
         initrd->mod_end = 0;
@@ -661,7 +661,7 @@ int __init dom0_construct_pv(struct domain *d,
                                     L1_PROT : COMPAT_L1_PROT));
         l1tab++;
 
-        page = mfn_to_page(mfn);
+        page = mfn_to_page(_mfn(mfn));
         if ( !page->u.inuse.type_info &&
              !get_page_and_type(page, d, PGT_writable_page) )
             BUG();
@@ -801,7 +801,7 @@ int __init dom0_construct_pv(struct domain *d,
     si->nr_p2m_frames = d->tot_pages - count;
     page_list_for_each ( page, &d->page_list )
     {
-        mfn = page_to_mfn(page);
+        mfn = mfn_x(page_to_mfn(page));
         BUG_ON(SHARED_M2P(get_gpfn_from_mfn(mfn)));
         if ( get_gpfn_from_mfn(mfn) >= count )
         {
@@ -826,7 +826,7 @@ int __init dom0_construct_pv(struct domain *d,
             panic("Not enough RAM for DOM0 reservation");
         while ( pfn < d->tot_pages )
         {
-            mfn = page_to_mfn(page);
+            mfn = mfn_x(page_to_mfn(page));
 #ifndef NDEBUG
 #define pfn (nr_pages - 1 - (pfn - (alloc_epfn - alloc_spfn)))
 #endif
diff --git a/xen/arch/x86/pv/domain.c b/xen/arch/x86/pv/domain.c
index 2c784fb3cc..5565e69f44 100644
--- a/xen/arch/x86/pv/domain.c
+++ b/xen/arch/x86/pv/domain.c
@@ -11,12 +11,6 @@
 
 #include <asm/pv/domain.h>
 
-/* Override macros from asm/page.h to make them work with mfn_t */
-#undef mfn_to_page
-#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
-#undef page_to_mfn
-#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
-
 static void noreturn continue_nonidle_domain(struct vcpu *v)
 {
     check_wakeup_from_wait();
diff --git a/xen/arch/x86/pv/emul-gate-op.c b/xen/arch/x86/pv/emul-gate-op.c
index 14ce95e26e..810c4f7d8c 100644
--- a/xen/arch/x86/pv/emul-gate-op.c
+++ b/xen/arch/x86/pv/emul-gate-op.c
@@ -41,12 +41,6 @@
 
 #include "emulate.h"
 
-/* Override macros from asm/page.h to make them work with mfn_t */
-#undef mfn_to_page
-#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
-#undef page_to_mfn
-#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
-
 static int read_gate_descriptor(unsigned int gate_sel,
                                 const struct vcpu *v,
                                 unsigned int *sel,
diff --git a/xen/arch/x86/pv/emul-priv-op.c b/xen/arch/x86/pv/emul-priv-op.c
index 17aaf97f10..b44b2415a6 100644
--- a/xen/arch/x86/pv/emul-priv-op.c
+++ b/xen/arch/x86/pv/emul-priv-op.c
@@ -43,16 +43,6 @@
 #include "emulate.h"
 #include "mm.h"
 
-/* Override macros from asm/page.h to make them work with mfn_t */
-#undef mfn_to_page
-#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
-#undef page_to_mfn
-#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
-
-/***********************
- * I/O emulation support
- */
-
 struct priv_op_ctxt {
     struct x86_emulate_ctxt ctxt;
     struct {
diff --git a/xen/arch/x86/pv/grant_table.c b/xen/arch/x86/pv/grant_table.c
index 458085e1b6..6b7d855c8a 100644
--- a/xen/arch/x86/pv/grant_table.c
+++ b/xen/arch/x86/pv/grant_table.c
@@ -27,12 +27,6 @@
 
 #include "mm.h"
 
-/* Override macros from asm/page.h to make them work with mfn_t */
-#undef mfn_to_page
-#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
-#undef page_to_mfn
-#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
-
 static unsigned int grant_to_pte_flags(unsigned int grant_flags,
                                        unsigned int cache_flags)
 {
diff --git a/xen/arch/x86/pv/ro-page-fault.c b/xen/arch/x86/pv/ro-page-fault.c
index 7e0e7e8dfc..17a08345d1 100644
--- a/xen/arch/x86/pv/ro-page-fault.c
+++ b/xen/arch/x86/pv/ro-page-fault.c
@@ -33,12 +33,6 @@
 #include "emulate.h"
 #include "mm.h"
 
-/* Override macros from asm/page.h to make them work with mfn_t */
-#undef mfn_to_page
-#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
-#undef page_to_mfn
-#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
-
 /*********************
  * Writable Pagetables
  */
diff --git a/xen/arch/x86/pv/shim.c b/xen/arch/x86/pv/shim.c
index 534965c92a..519278f78c 100644
--- a/xen/arch/x86/pv/shim.c
+++ b/xen/arch/x86/pv/shim.c
@@ -37,8 +37,6 @@
 
 #include <compat/grant_table.h>
 
-#undef mfn_to_page
-#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
 #undef virt_to_mfn
 #define virt_to_mfn(va) _mfn(__virt_to_mfn(va))
 
@@ -848,7 +846,7 @@ static unsigned long batch_memory_op(unsigned int cmd, unsigned int order,
     set_xen_guest_handle(xmr.extent_start, pfns);
     page_list_for_each ( pg, list )
     {
-        pfns[xmr.nr_extents++] = page_to_mfn(pg);
+        pfns[xmr.nr_extents++] = mfn_x(page_to_mfn(pg));
         if ( xmr.nr_extents == ARRAY_SIZE(pfns) || !page_list_next(pg, list) )
         {
             long nr = xen_hypercall_memory_op(cmd, &xmr);
diff --git a/xen/arch/x86/smpboot.c b/xen/arch/x86/smpboot.c
index 52ad43e0ed..48c8cdb734 100644
--- a/xen/arch/x86/smpboot.c
+++ b/xen/arch/x86/smpboot.c
@@ -48,12 +48,6 @@
 #include <mach_wakecpu.h>
 #include <smpboot_hooks.h>
 
-/* Override macros from asm/page.h to make them work with mfn_t */
-#undef mfn_to_page
-#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
-#undef page_to_mfn
-#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
-
 #define setup_trampoline()    (bootsym_phys(trampoline_realmode_entry))
 
 unsigned long __read_mostly trampoline_phys;
diff --git a/xen/arch/x86/tboot.c b/xen/arch/x86/tboot.c
index 71e757c553..fb4616ae83 100644
--- a/xen/arch/x86/tboot.c
+++ b/xen/arch/x86/tboot.c
@@ -184,7 +184,7 @@ static void update_pagetable_mac(vmac_ctx_t *ctx)
 
     for ( mfn = 0; mfn < max_page; mfn++ )
     {
-        struct page_info *page = mfn_to_page(mfn);
+        struct page_info *page = mfn_to_page(_mfn(mfn));
 
         if ( !mfn_valid(_mfn(mfn)) )
             continue;
@@ -276,7 +276,7 @@ static void tboot_gen_xenheap_integrity(const uint8_t key[TB_KEY_SIZE],
     vmac_set_key((uint8_t *)key, &ctx);
     for ( mfn = 0; mfn < max_page; mfn++ )
     {
-        struct page_info *page = __mfn_to_page(mfn);
+        struct page_info *page = mfn_to_page(_mfn(mfn));
 
         if ( !mfn_valid(_mfn(mfn)) )
             continue;
diff --git a/xen/arch/x86/traps.c b/xen/arch/x86/traps.c
index 27190e0423..571796b123 100644
--- a/xen/arch/x86/traps.c
+++ b/xen/arch/x86/traps.c
@@ -834,8 +834,8 @@ int wrmsr_hypervisor_regs(uint32_t idx, uint64_t val)
             }
 
             gdprintk(XENLOG_WARNING,
-                     "Bad GMFN %lx (MFN %lx) to MSR %08x\n",
-                     gmfn, page ? page_to_mfn(page) : -1UL, base);
+                     "Bad GMFN %lx (MFN %#"PRI_mfn") to MSR %08x\n",
+                     gmfn, mfn_x(page ? page_to_mfn(page) : INVALID_MFN), base);
             return 0;
         }
 
diff --git a/xen/arch/x86/x86_64/mm.c b/xen/arch/x86/x86_64/mm.c
index 6b679882d6..ff8a6de23f 100644
--- a/xen/arch/x86/x86_64/mm.c
+++ b/xen/arch/x86/x86_64/mm.c
@@ -40,12 +40,6 @@ asm(".file \"" __FILE__ "\"");
 #include <asm/mem_sharing.h>
 #include <public/memory.h>
 
-/* Override macros from asm/page.h to make them work with mfn_t */
-#undef page_to_mfn
-#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
-#undef mfn_to_page
-#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
-
 unsigned int __read_mostly m2p_compat_vstart = __HYPERVISOR_COMPAT_VIRT_START;
 
 l2_pgentry_t *compat_idle_pg_table_l2;
diff --git a/xen/common/domain.c b/xen/common/domain.c
index e1c003d71e..e2acfeff80 100644
--- a/xen/common/domain.c
+++ b/xen/common/domain.c
@@ -1217,7 +1217,7 @@ int map_vcpu_info(struct vcpu *v, unsigned long gfn, unsigned offset)
     }
 
     v->vcpu_info = new_info;
-    v->vcpu_info_mfn = _mfn(page_to_mfn(page));
+    v->vcpu_info_mfn = page_to_mfn(page);
 
     /* Set new vcpu_info pointer /before/ setting pending flags. */
     smp_wmb();
@@ -1250,7 +1250,7 @@ void unmap_vcpu_info(struct vcpu *v)
 
     vcpu_info_reset(v); /* NB: Clobbers v->vcpu_info_mfn */
 
-    put_page_and_type(mfn_to_page(mfn_x(mfn)));
+    put_page_and_type(mfn_to_page(mfn));
 }
 
 int default_initialise_vcpu(struct vcpu *v, XEN_GUEST_HANDLE_PARAM(void) arg)
diff --git a/xen/common/grant_table.c b/xen/common/grant_table.c
index e9a81b66be..959b7c64b2 100644
--- a/xen/common/grant_table.c
+++ b/xen/common/grant_table.c
@@ -40,12 +40,6 @@
 #include <xsm/xsm.h>
 #include <asm/flushtlb.h>
 
-/* Override macros from asm/page.h to make them work with mfn_t */
-#undef page_to_mfn
-#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
-#undef mfn_to_page
-#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
-
 /* Per-domain grant information. */
 struct grant_table {
     /*
diff --git a/xen/common/kimage.c b/xen/common/kimage.c
index afd8292cc1..210241dfb7 100644
--- a/xen/common/kimage.c
+++ b/xen/common/kimage.c
@@ -23,12 +23,6 @@
 
 #include <asm/page.h>
 
-/* Override macros from asm/page.h to make them work with mfn_t */
-#undef mfn_to_page
-#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
-#undef page_to_mfn
-#define page_to_mfn(pg)  _mfn(__page_to_mfn(pg))
-
 /*
  * When kexec transitions to the new kernel there is a one-to-one
  * mapping between physical and virtual addresses.  On processors
diff --git a/xen/common/memory.c b/xen/common/memory.c
index 93d856df02..4c6b36b297 100644
--- a/xen/common/memory.c
+++ b/xen/common/memory.c
@@ -33,12 +33,6 @@
 #include <asm/guest.h>
 #endif
 
-/* Override macros from asm/page.h to make them work with mfn_t */
-#undef page_to_mfn
-#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
-#undef mfn_to_page
-#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
-
 struct memop_args {
     /* INPUT */
     struct domain *domain;     /* Domain to be affected. */
diff --git a/xen/common/page_alloc.c b/xen/common/page_alloc.c
index b0db41feea..7fa847c9a1 100644
--- a/xen/common/page_alloc.c
+++ b/xen/common/page_alloc.c
@@ -151,12 +151,6 @@
 #define p2m_pod_offline_or_broken_replace(pg) BUG_ON(pg != NULL)
 #endif
 
-/* Override macros from asm/page.h to make them work with mfn_t */
-#undef page_to_mfn
-#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
-#undef mfn_to_page
-#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
-
 /*
  * Comma-separated list of hexadecimal page numbers containing bad bytes.
  * e.g. 'badpage=0x3f45,0x8a321'.
diff --git a/xen/common/tmem.c b/xen/common/tmem.c
index 324f42a6f9..c077f87e77 100644
--- a/xen/common/tmem.c
+++ b/xen/common/tmem.c
@@ -243,7 +243,7 @@ static void tmem_persistent_pool_page_put(void *page_va)
     struct page_info *pi;
 
     ASSERT(IS_PAGE_ALIGNED(page_va));
-    pi = mfn_to_page(virt_to_mfn(page_va));
+    pi = mfn_to_page(_mfn(virt_to_mfn(page_va)));
     ASSERT(IS_VALID_PAGE(pi));
     __tmem_free_page_thispool(pi);
 }
diff --git a/xen/common/tmem_xen.c b/xen/common/tmem_xen.c
index bd52e44faf..bf7b14f79a 100644
--- a/xen/common/tmem_xen.c
+++ b/xen/common/tmem_xen.c
@@ -14,10 +14,6 @@
 #include <xen/cpu.h>
 #include <xen/init.h>
 
-/* Override macros from asm/page.h to make them work with mfn_t */
-#undef page_to_mfn
-#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
-
 bool __read_mostly opt_tmem;
 boolean_param("tmem", opt_tmem);
 
diff --git a/xen/common/trace.c b/xen/common/trace.c
index 2e18702317..1f19b7a604 100644
--- a/xen/common/trace.c
+++ b/xen/common/trace.c
@@ -243,7 +243,7 @@ static int alloc_trace_bufs(unsigned int pages)
         /* Now share the trace pages */
         for ( i = 0; i < pages; i++ )
         {
-            pg = mfn_to_page(t_info_mfn_list[offset + i]);
+            pg = mfn_to_page(_mfn(t_info_mfn_list[offset + i]));
             share_xen_page_with_privileged_guests(pg, XENSHARE_writable);
         }
     }
@@ -274,7 +274,7 @@ out_dealloc:
             uint32_t mfn = t_info_mfn_list[offset + i];
             if ( !mfn )
                 break;
-            ASSERT(!(mfn_to_page(mfn)->count_info & PGC_allocated));
+            ASSERT(!(mfn_to_page(_mfn(mfn))->count_info & PGC_allocated));
             free_xenheap_pages(mfn_to_virt(mfn), 0);
         }
     }
diff --git a/xen/common/vmap.c b/xen/common/vmap.c
index 1f50c91789..6e295862fb 100644
--- a/xen/common/vmap.c
+++ b/xen/common/vmap.c
@@ -9,10 +9,6 @@
 #include <xen/vmap.h>
 #include <asm/page.h>
 
-/* Override macros from asm/page.h to make them work with mfn_t */
-#undef page_to_mfn
-#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
-
 static DEFINE_SPINLOCK(vm_lock);
 static void *__read_mostly vm_base[VMAP_REGION_NR];
 #define vm_bitmap(x) ((unsigned long *)vm_base[x])
@@ -274,7 +270,7 @@ static void *vmalloc_type(size_t size, enum vmap_region type)
 
  error:
     while ( i-- )
-        free_domheap_page(mfn_to_page(mfn_x(mfn[i])));
+        free_domheap_page(mfn_to_page(mfn[i]));
     xfree(mfn);
     return NULL;
 }
diff --git a/xen/common/xenoprof.c b/xen/common/xenoprof.c
index 5acdde5691..fecdfb3697 100644
--- a/xen/common/xenoprof.c
+++ b/xen/common/xenoprof.c
@@ -22,8 +22,6 @@
 /* Override macros from asm/page.h to make them work with mfn_t */
 #undef virt_to_mfn
 #define virt_to_mfn(va) _mfn(__virt_to_mfn(va))
-#undef mfn_to_page
-#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
 
 /* Limit amount of pages used for shared buffer (per domain) */
 #define MAX_OPROF_SHARED_PAGES 32
diff --git a/xen/drivers/passthrough/amd/iommu_map.c b/xen/drivers/passthrough/amd/iommu_map.c
index fd2327d3e5..70b4345b37 100644
--- a/xen/drivers/passthrough/amd/iommu_map.c
+++ b/xen/drivers/passthrough/amd/iommu_map.c
@@ -451,7 +451,7 @@ static int iommu_pde_from_gfn(struct domain *d, unsigned long pfn,
     BUG_ON( table == NULL || level < IOMMU_PAGING_MODE_LEVEL_1 || 
             level > IOMMU_PAGING_MODE_LEVEL_6 );
 
-    next_table_mfn = page_to_mfn(table);
+    next_table_mfn = mfn_x(page_to_mfn(table));
 
     if ( level == IOMMU_PAGING_MODE_LEVEL_1 )
     {
@@ -493,7 +493,7 @@ static int iommu_pde_from_gfn(struct domain *d, unsigned long pfn,
                 return 1;
             }
 
-            next_table_mfn = page_to_mfn(table);
+            next_table_mfn = mfn_x(page_to_mfn(table));
             set_iommu_pde_present((u32*)pde, next_table_mfn, next_level, 
                                   !!IOMMUF_writable, !!IOMMUF_readable);
 
@@ -520,7 +520,7 @@ static int iommu_pde_from_gfn(struct domain *d, unsigned long pfn,
                     unmap_domain_page(next_table_vaddr);
                     return 1;
                 }
-                next_table_mfn = page_to_mfn(table);
+                next_table_mfn = mfn_x(page_to_mfn(table));
                 set_iommu_pde_present((u32*)pde, next_table_mfn, next_level,
                                       !!IOMMUF_writable, !!IOMMUF_readable);
             }
@@ -577,7 +577,7 @@ static int update_paging_mode(struct domain *d, unsigned long gfn)
         }
 
         new_root_vaddr = __map_domain_page(new_root);
-        old_root_mfn = page_to_mfn(old_root);
+        old_root_mfn = mfn_x(page_to_mfn(old_root));
         set_iommu_pde_present(new_root_vaddr, old_root_mfn, level,
                               !!IOMMUF_writable, !!IOMMUF_readable);
         level++;
@@ -712,7 +712,7 @@ int amd_iommu_map_page(struct domain *d, unsigned long gfn, unsigned long mfn,
         }
 
         /* Deallocate lower level page table */
-        free_amd_iommu_pgtable(mfn_to_page(pt_mfn[merge_level - 1]));
+        free_amd_iommu_pgtable(mfn_to_page(_mfn(pt_mfn[merge_level - 1])));
     }
 
 out:
@@ -802,7 +802,7 @@ void amd_iommu_share_p2m(struct domain *d)
     mfn_t pgd_mfn;
 
     pgd_mfn = pagetable_get_mfn(p2m_get_pagetable(p2m_get_hostp2m(d)));
-    p2m_table = mfn_to_page(mfn_x(pgd_mfn));
+    p2m_table = mfn_to_page(pgd_mfn);
 
     if ( hd->arch.root_table != p2m_table )
     {
diff --git a/xen/drivers/passthrough/iommu.c b/xen/drivers/passthrough/iommu.c
index 1aecf7cf34..2c44fabf99 100644
--- a/xen/drivers/passthrough/iommu.c
+++ b/xen/drivers/passthrough/iommu.c
@@ -184,7 +184,7 @@ void __hwdom_init iommu_hwdom_init(struct domain *d)
 
         page_list_for_each ( page, &d->page_list )
         {
-            unsigned long mfn = page_to_mfn(page);
+            unsigned long mfn = mfn_x(page_to_mfn(page));
             unsigned long gfn = mfn_to_gmfn(d, mfn);
             unsigned int mapping = IOMMUF_readable;
             int ret;
diff --git a/xen/drivers/passthrough/x86/iommu.c b/xen/drivers/passthrough/x86/iommu.c
index 0253823173..68182afd91 100644
--- a/xen/drivers/passthrough/x86/iommu.c
+++ b/xen/drivers/passthrough/x86/iommu.c
@@ -58,7 +58,7 @@ int arch_iommu_populate_page_table(struct domain *d)
         if ( is_hvm_domain(d) ||
             (page->u.inuse.type_info & PGT_type_mask) == PGT_writable_page )
         {
-            unsigned long mfn = page_to_mfn(page);
+            unsigned long mfn = mfn_x(page_to_mfn(page));
             unsigned long gfn = mfn_to_gmfn(d, mfn);
 
             if ( gfn != gfn_x(INVALID_GFN) )
diff --git a/xen/include/asm-arm/mm.h b/xen/include/asm-arm/mm.h
index 023e2eb213..b1d94805d4 100644
--- a/xen/include/asm-arm/mm.h
+++ b/xen/include/asm-arm/mm.h
@@ -138,7 +138,7 @@ extern vaddr_t xenheap_virt_start;
 #endif
 
 #ifdef CONFIG_ARM_32
-#define is_xen_heap_page(page) is_xen_heap_mfn(__page_to_mfn(page))
+#define is_xen_heap_page(page) is_xen_heap_mfn(mfn_x(page_to_mfn(page)))
 #define is_xen_heap_mfn(mfn) ({                                 \
     unsigned long mfn_ = (mfn);                                 \
     (mfn_ >= mfn_x(xenheap_mfn_start) &&                        \
@@ -147,7 +147,7 @@ extern vaddr_t xenheap_virt_start;
 #else
 #define is_xen_heap_page(page) ((page)->count_info & PGC_xen_heap)
 #define is_xen_heap_mfn(mfn) \
-    (mfn_valid(_mfn(mfn)) && is_xen_heap_page(__mfn_to_page(mfn)))
+    (mfn_valid(_mfn(mfn)) && is_xen_heap_page(mfn_to_page(_mfn(mfn))))
 #endif
 
 #define is_xen_fixed_mfn(mfn)                                   \
@@ -220,12 +220,14 @@ static inline void __iomem *ioremap_wc(paddr_t start, size_t len)
 })
 
 /* Convert between machine frame numbers and page-info structures. */
-#define __mfn_to_page(mfn)  (frame_table + (pfn_to_pdx(mfn) - frametable_base_pdx))
-#define __page_to_mfn(pg)   pdx_to_pfn((unsigned long)((pg) - frame_table) + frametable_base_pdx)
+#define mfn_to_page(mfn)                                            \
+    (frame_table + (mfn_to_pdx(mfn) - frametable_base_pdx))
+#define page_to_mfn(pg)                                             \
+    pdx_to_mfn((unsigned long)((pg) - frame_table) + frametable_base_pdx)
 
 /* Convert between machine addresses and page-info structures. */
-#define maddr_to_page(ma) __mfn_to_page((ma) >> PAGE_SHIFT)
-#define page_to_maddr(pg) ((paddr_t)__page_to_mfn(pg) << PAGE_SHIFT)
+#define maddr_to_page(ma) mfn_to_page(maddr_to_mfn(ma))
+#define page_to_maddr(pg) (mfn_to_maddr(page_to_mfn(pg)))
 
 /* Convert between frame number and address formats.  */
 #define pfn_to_paddr(pfn) ((paddr_t)(pfn) << PAGE_SHIFT)
@@ -235,7 +237,7 @@ static inline void __iomem *ioremap_wc(paddr_t start, size_t len)
 #define gaddr_to_gfn(ga)    _gfn(paddr_to_pfn(ga))
 #define mfn_to_maddr(mfn)   pfn_to_paddr(mfn_x(mfn))
 #define maddr_to_mfn(ma)    _mfn(paddr_to_pfn(ma))
-#define vmap_to_mfn(va)     paddr_to_pfn(virt_to_maddr((vaddr_t)va))
+#define vmap_to_mfn(va)     maddr_to_mfn(virt_to_maddr((vaddr_t)va))
 #define vmap_to_page(va)    mfn_to_page(vmap_to_mfn(va))
 
 /* Page-align address and convert to frame number format */
@@ -293,8 +295,6 @@ static inline uint64_t gvirt_to_maddr(vaddr_t va, paddr_t *pa,
  * These are overriden in various source files while underscored version
  * remain intact.
  */
-#define mfn_to_page(mfn)    __mfn_to_page(mfn)
-#define page_to_mfn(pg)     __page_to_mfn(pg)
 #define virt_to_mfn(va)     __virt_to_mfn(va)
 #define mfn_to_virt(mfn)    __mfn_to_virt(mfn)
 
@@ -314,7 +314,7 @@ static inline struct page_info *virt_to_page(const void *v)
 
 static inline void *page_to_virt(const struct page_info *pg)
 {
-    return mfn_to_virt(page_to_mfn(pg));
+    return mfn_to_virt(mfn_x(page_to_mfn(pg)));
 }
 
 struct page_info *get_page_from_gva(struct vcpu *v, vaddr_t va,
diff --git a/xen/include/asm-arm/p2m.h b/xen/include/asm-arm/p2m.h
index a0abc84ed8..bcac141fd4 100644
--- a/xen/include/asm-arm/p2m.h
+++ b/xen/include/asm-arm/p2m.h
@@ -278,7 +278,7 @@ static inline struct page_info *get_page_from_gfn(
 {
     struct page_info *page;
     p2m_type_t p2mt;
-    unsigned long mfn = mfn_x(p2m_lookup(d, _gfn(gfn), &p2mt));
+    mfn_t mfn = p2m_lookup(d, _gfn(gfn), &p2mt);
 
     if (t)
         *t = p2mt;
@@ -286,7 +286,7 @@ static inline struct page_info *get_page_from_gfn(
     if ( !p2m_is_any_ram(p2mt) )
         return NULL;
 
-    if ( !mfn_valid(_mfn(mfn)) )
+    if ( !mfn_valid(mfn) )
         return NULL;
     page = mfn_to_page(mfn);
 
diff --git a/xen/include/asm-x86/mm.h b/xen/include/asm-x86/mm.h
index 3013c266fe..8dc3821e97 100644
--- a/xen/include/asm-x86/mm.h
+++ b/xen/include/asm-x86/mm.h
@@ -271,7 +271,7 @@ struct page_info
 
 #define is_xen_heap_page(page) ((page)->count_info & PGC_xen_heap)
 #define is_xen_heap_mfn(mfn) \
-    (__mfn_valid(mfn) && is_xen_heap_page(__mfn_to_page(mfn)))
+    (__mfn_valid(mfn) && is_xen_heap_page(mfn_to_page(_mfn(mfn))))
 #define is_xen_fixed_mfn(mfn)                     \
     ((((mfn) << PAGE_SHIFT) >= __pa(&_stext)) &&  \
      (((mfn) << PAGE_SHIFT) <= __pa(&__2M_rwdata_end)))
@@ -384,7 +384,7 @@ void put_page_from_l1e(l1_pgentry_t l1e, struct domain *l1e_owner);
 
 static inline struct page_info *get_page_from_mfn(mfn_t mfn, struct domain *d)
 {
-    struct page_info *page = __mfn_to_page(mfn_x(mfn));
+    struct page_info *page = mfn_to_page(mfn);
 
     if ( unlikely(!mfn_valid(mfn)) || unlikely(!get_page(page, d)) )
     {
@@ -478,10 +478,10 @@ extern paddr_t mem_hotplug;
 #define SHARED_M2P(_e)           ((_e) == SHARED_M2P_ENTRY)
 
 #define compat_machine_to_phys_mapping ((unsigned int *)RDWR_COMPAT_MPT_VIRT_START)
-#define _set_gpfn_from_mfn(mfn, pfn) ({                        \
-    struct domain *d = page_get_owner(__mfn_to_page(mfn));     \
-    unsigned long entry = (d && (d == dom_cow)) ?              \
-        SHARED_M2P_ENTRY : (pfn);                              \
+#define _set_gpfn_from_mfn(mfn, pfn) ({                         \
+    struct domain *d = page_get_owner(mfn_to_page(_mfn(mfn)));    \
+    unsigned long entry = (d && (d == dom_cow)) ?               \
+        SHARED_M2P_ENTRY : (pfn);                               \
     ((void)((mfn) >= (RDWR_COMPAT_MPT_VIRT_END - RDWR_COMPAT_MPT_VIRT_START) / 4 || \
             (compat_machine_to_phys_mapping[(mfn)] = (unsigned int)(entry))), \
      machine_to_phys_mapping[(mfn)] = (entry));                \
diff --git a/xen/include/asm-x86/p2m.h b/xen/include/asm-x86/p2m.h
index 2e7aa8fc79..c486b6f8f0 100644
--- a/xen/include/asm-x86/p2m.h
+++ b/xen/include/asm-x86/p2m.h
@@ -488,7 +488,7 @@ static inline struct page_info *get_page_from_gfn(
     /* Non-translated guests see 1-1 RAM / MMIO mappings everywhere */
     if ( t )
         *t = likely(d != dom_io) ? p2m_ram_rw : p2m_mmio_direct;
-    page = __mfn_to_page(gfn);
+    page = mfn_to_page(_mfn(gfn));
     return mfn_valid(_mfn(gfn)) && get_page(page, d) ? page : NULL;
 }
 
diff --git a/xen/include/asm-x86/page.h b/xen/include/asm-x86/page.h
index 45ca742678..7e2a1546c3 100644
--- a/xen/include/asm-x86/page.h
+++ b/xen/include/asm-x86/page.h
@@ -88,10 +88,10 @@
     ((paddr_t)(((x).l4 & (PADDR_MASK&PAGE_MASK))))
 
 /* Get pointer to info structure of page mapped by pte (struct page_info *). */
-#define l1e_get_page(x)           (__mfn_to_page(l1e_get_pfn(x)))
-#define l2e_get_page(x)           (__mfn_to_page(l2e_get_pfn(x)))
-#define l3e_get_page(x)           (__mfn_to_page(l3e_get_pfn(x)))
-#define l4e_get_page(x)           (__mfn_to_page(l4e_get_pfn(x)))
+#define l1e_get_page(x)           mfn_to_page(l1e_get_mfn(x))
+#define l2e_get_page(x)           mfn_to_page(l2e_get_mfn(x))
+#define l3e_get_page(x)           mfn_to_page(l3e_get_mfn(x))
+#define l4e_get_page(x)           mfn_to_page(l4e_get_mfn(x))
 
 /* Get pte access flags (unsigned int). */
 #define l1e_get_flags(x)           (get_pte_flags((x).l1))
@@ -157,10 +157,10 @@ static inline l4_pgentry_t l4e_from_paddr(paddr_t pa, unsigned int flags)
 #define l4e_from_intpte(intpte)    ((l4_pgentry_t) { (intpte_t)(intpte) })
 
 /* Construct a pte from a page pointer and access flags. */
-#define l1e_from_page(page, flags) l1e_from_pfn(__page_to_mfn(page), (flags))
-#define l2e_from_page(page, flags) l2e_from_pfn(__page_to_mfn(page), (flags))
-#define l3e_from_page(page, flags) l3e_from_pfn(__page_to_mfn(page), (flags))
-#define l4e_from_page(page, flags) l4e_from_pfn(__page_to_mfn(page), (flags))
+#define l1e_from_page(page, flags) l1e_from_mfn(page_to_mfn(page), (flags))
+#define l2e_from_page(page, flags) l2e_from_mfn(page_to_mfn(page), (flags))
+#define l3e_from_page(page, flags) l3e_from_mfn(page_to_mfn(page), (flags))
+#define l4e_from_page(page, flags) l4e_from_mfn(page_to_mfn(page), (flags))
 
 /* Add extra flags to an existing pte. */
 #define l1e_add_flags(x, flags)    ((x).l1 |= put_pte_flags(flags))
@@ -215,13 +215,13 @@ static inline l4_pgentry_t l4e_from_paddr(paddr_t pa, unsigned int flags)
 /* Page-table type. */
 typedef struct { u64 pfn; } pagetable_t;
 #define pagetable_get_paddr(x)  ((paddr_t)(x).pfn << PAGE_SHIFT)
-#define pagetable_get_page(x)   __mfn_to_page((x).pfn)
+#define pagetable_get_page(x)   mfn_to_page(pagetable_get_mfn(x))
 #define pagetable_get_pfn(x)    ((x).pfn)
 #define pagetable_get_mfn(x)    _mfn(((x).pfn))
 #define pagetable_is_null(x)    ((x).pfn == 0)
 #define pagetable_from_pfn(pfn) ((pagetable_t) { (pfn) })
 #define pagetable_from_mfn(mfn) ((pagetable_t) { mfn_x(mfn) })
-#define pagetable_from_page(pg) pagetable_from_pfn(__page_to_mfn(pg))
+#define pagetable_from_page(pg) pagetable_from_mfn(page_to_mfn(pg))
 #define pagetable_from_paddr(p) pagetable_from_pfn((p)>>PAGE_SHIFT)
 #define pagetable_null()        pagetable_from_pfn(0)
 
@@ -240,12 +240,12 @@ void copy_page_sse2(void *, const void *);
 #define __mfn_to_virt(mfn)  (maddr_to_virt((paddr_t)(mfn) << PAGE_SHIFT))
 
 /* Convert between machine frame numbers and page-info structures. */
-#define __mfn_to_page(mfn)  (frame_table + pfn_to_pdx(mfn))
-#define __page_to_mfn(pg)   pdx_to_pfn((unsigned long)((pg) - frame_table))
+#define mfn_to_page(mfn)    (frame_table + mfn_to_pdx(mfn))
+#define page_to_mfn(pg)     pdx_to_mfn((unsigned long)((pg) - frame_table))
 
 /* Convert between machine addresses and page-info structures. */
-#define __maddr_to_page(ma) __mfn_to_page((ma) >> PAGE_SHIFT)
-#define __page_to_maddr(pg) ((paddr_t)__page_to_mfn(pg) << PAGE_SHIFT)
+#define __maddr_to_page(ma) mfn_to_page(maddr_to_mfn(ma))
+#define __page_to_maddr(pg) (mfn_to_maddr(page_to_mfn(pg)))
 
 /* Convert between frame number and address formats.  */
 #define __pfn_to_paddr(pfn) ((paddr_t)(pfn) << PAGE_SHIFT)
@@ -264,8 +264,6 @@ void copy_page_sse2(void *, const void *);
 #define mfn_to_virt(mfn)    __mfn_to_virt(mfn)
 #define virt_to_maddr(va)   __virt_to_maddr((unsigned long)(va))
 #define maddr_to_virt(ma)   __maddr_to_virt((unsigned long)(ma))
-#define mfn_to_page(mfn)    __mfn_to_page(mfn)
-#define page_to_mfn(pg)     __page_to_mfn(pg)
 #define maddr_to_page(ma)   __maddr_to_page(ma)
 #define page_to_maddr(pg)   __page_to_maddr(pg)
 #define virt_to_page(va)    __virt_to_page(va)
@@ -273,7 +271,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_pfn(*virt_to_xen_l1e((unsigned long)(va)))
+#define vmap_to_mfn(va)     _mfn(l1e_get_pfn(*virt_to_xen_l1e((unsigned long)(va))))
 #define vmap_to_page(va)    mfn_to_page(vmap_to_mfn(va))
 
 #endif /* !defined(__ASSEMBLY__) */
diff --git a/xen/include/xen/domain_page.h b/xen/include/xen/domain_page.h
index 890bae5b9c..32669a3339 100644
--- a/xen/include/xen/domain_page.h
+++ b/xen/include/xen/domain_page.h
@@ -34,7 +34,7 @@ void unmap_domain_page(const void *va);
 /* 
  * Given a VA from map_domain_page(), return its underlying MFN.
  */
-unsigned long domain_page_map_to_mfn(const void *va);
+mfn_t domain_page_map_to_mfn(const void *va);
 
 /*
  * Similar to the above calls, except the mapping is accessible in all
@@ -44,11 +44,11 @@ unsigned long domain_page_map_to_mfn(const void *va);
 void *map_domain_page_global(mfn_t mfn);
 void unmap_domain_page_global(const void *va);
 
-#define __map_domain_page(pg)        map_domain_page(_mfn(__page_to_mfn(pg)))
+#define __map_domain_page(pg)        map_domain_page(page_to_mfn(pg))
 
 static inline void *__map_domain_page_global(const struct page_info *pg)
 {
-    return map_domain_page_global(_mfn(__page_to_mfn(pg)));
+    return map_domain_page_global(page_to_mfn(pg));
 }
 
 #else /* !CONFIG_DOMAIN_PAGE */
@@ -56,7 +56,7 @@ static inline void *__map_domain_page_global(const struct page_info *pg)
 #define map_domain_page(mfn)                __mfn_to_virt(mfn_x(mfn))
 #define __map_domain_page(pg)               page_to_virt(pg)
 #define unmap_domain_page(va)               ((void)(va))
-#define domain_page_map_to_mfn(va)          virt_to_mfn((unsigned long)(va))
+#define domain_page_map_to_mfn(va)          _mfn(virt_to_mfn((unsigned long)(va)))
 
 static inline void *map_domain_page_global(mfn_t mfn)
 {
diff --git a/xen/include/xen/mm.h b/xen/include/xen/mm.h
index caad06e753..204dd9c48d 100644
--- a/xen/include/xen/mm.h
+++ b/xen/include/xen/mm.h
@@ -277,13 +277,8 @@ struct page_list_head
 # define PAGE_LIST_NULL ((typeof(((struct page_info){}).list.next))~0)
 
 # if !defined(pdx_to_page) && !defined(page_to_pdx)
-#  if defined(__page_to_mfn) || defined(__mfn_to_page)
-#   define page_to_pdx __page_to_mfn
-#   define pdx_to_page __mfn_to_page
-#  else
 #   define page_to_pdx page_to_mfn
 #   define pdx_to_page mfn_to_page
-#  endif
 # endif
 
 # define PAGE_LIST_HEAD_INIT(name) { NULL, NULL }
diff --git a/xen/include/xen/tmem_xen.h b/xen/include/xen/tmem_xen.h
index 542c0b3f20..8516a0b131 100644
--- a/xen/include/xen/tmem_xen.h
+++ b/xen/include/xen/tmem_xen.h
@@ -25,7 +25,7 @@
 typedef uint32_t pagesize_t;  /* like size_t, must handle largest PAGE_SIZE */
 
 #define IS_PAGE_ALIGNED(addr) IS_ALIGNED((unsigned long)(addr), PAGE_SIZE)
-#define IS_VALID_PAGE(_pi)    mfn_valid(_mfn(page_to_mfn(_pi)))
+#define IS_VALID_PAGE(_pi)    mfn_valid(page_to_mfn(_pi))
 
 extern struct page_list_head tmem_page_list;
 extern spinlock_t tmem_page_list_lock;
-- 
2.11.0


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply related	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 16/16] xen: Convert page_to_mfn and mfn_to_page to use typesafe MFN
  2018-02-21 14:02 ` [PATCH v4 16/16] xen: Convert page_to_mfn and mfn_to_page " Julien Grall
@ 2018-02-21 14:25   ` Razvan Cojocaru
  2018-02-21 14:59   ` Paul Durrant
                     ` (4 subsequent siblings)
  5 siblings, 0 replies; 76+ messages in thread
From: Razvan Cojocaru @ 2018-02-21 14:25 UTC (permalink / raw)
  To: Julien Grall, xen-devel
  Cc: Jun Nakajima, Kevin Tian, Stefano Stabellini, Wei Liu,
	Suravee Suthikulpanit, George Dunlap, Andrew Cooper, Ian Jackson,
	Tim Deegan, Tamas K Lengyel, Jan Beulich, Shane Wang,
	Boris Ostrovsky, Gang Wei, Paul Durrant

On 02/21/2018 04:02 PM, Julien Grall wrote:
> Most of the users of page_to_mfn and mfn_to_page are either overriding
> the macros to make them work with mfn_t or use mfn_x/_mfn because the
> rest of the function use mfn_t.
> 
> So make page_to_mfn and mfn_to_page return mfn_t by default. The __*
> version are now dropped as this patch will convert all the remaining
> non-typesafe callers.
> 
> Only reasonable clean-ups are done in this patch. The rest will use
> _mfn/mfn_x for the time being.
> 
> Lastly, domain_page_to_mfn is also converted to use mfn_t given that
> most of the callers are now switched to _mfn(domain_page_to_mfn(...)).
> 
> Signed-off-by: Julien Grall <julien.grall@arm.com>

Acked-by: Razvan Cojocaru <rcojocaru@bitdefender.com>


Thanks,
Razvan

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 16/16] xen: Convert page_to_mfn and mfn_to_page to use typesafe MFN
  2018-02-21 14:02 ` [PATCH v4 16/16] xen: Convert page_to_mfn and mfn_to_page " Julien Grall
  2018-02-21 14:25   ` Razvan Cojocaru
@ 2018-02-21 14:59   ` Paul Durrant
  2018-02-21 23:20   ` Boris Ostrovsky
                     ` (3 subsequent siblings)
  5 siblings, 0 replies; 76+ messages in thread
From: Paul Durrant @ 2018-02-21 14:59 UTC (permalink / raw)
  To: 'Julien Grall', xen-devel
  Cc: Kevin Tian, Stefano Stabellini, Wei Liu, Suravee Suthikulpanit,
	Razvan Cojocaru, Jun Nakajima, Andrew Cooper, Tim (Xen.org),
	George Dunlap, Tamas K Lengyel, Jan Beulich, Shane Wang,
	Ian Jackson, Boris Ostrovsky, Gang Wei

> -----Original Message-----
> From: Julien Grall [mailto:julien.grall@arm.com]
> Sent: 21 February 2018 14:03
> To: xen-devel@lists.xen.org
> Cc: Julien Grall <julien.grall@arm.com>; Stefano Stabellini
> <sstabellini@kernel.org>; Andrew Cooper <Andrew.Cooper3@citrix.com>;
> George Dunlap <George.Dunlap@citrix.com>; Ian Jackson
> <Ian.Jackson@citrix.com>; Jan Beulich <jbeulich@suse.com>; Konrad
> Rzeszutek Wilk <konrad.wilk@oracle.com>; Tim (Xen.org) <tim@xen.org>;
> Wei Liu <wei.liu2@citrix.com>; Razvan Cojocaru
> <rcojocaru@bitdefender.com>; Tamas K Lengyel <tamas@tklengyel.com>;
> Paul Durrant <Paul.Durrant@citrix.com>; Boris Ostrovsky
> <boris.ostrovsky@oracle.com>; Suravee Suthikulpanit
> <suravee.suthikulpanit@amd.com>; Jun Nakajima
> <jun.nakajima@intel.com>; Kevin Tian <kevin.tian@intel.com>; George
> Dunlap <George.Dunlap@citrix.com>; Gang Wei <gang.wei@intel.com>;
> Shane Wang <shane.wang@intel.com>
> Subject: [PATCH v4 16/16] xen: Convert page_to_mfn and mfn_to_page to
> use typesafe MFN
> 
> Most of the users of page_to_mfn and mfn_to_page are either overriding
> the macros to make them work with mfn_t or use mfn_x/_mfn because the
> rest of the function use mfn_t.
> 
> So make page_to_mfn and mfn_to_page return mfn_t by default. The __*
> version are now dropped as this patch will convert all the remaining
> non-typesafe callers.
> 
> Only reasonable clean-ups are done in this patch. The rest will use
> _mfn/mfn_x for the time being.
> 
> Lastly, domain_page_to_mfn is also converted to use mfn_t given that
> most of the callers are now switched to _mfn(domain_page_to_mfn(...)).
> 
> Signed-off-by: Julien Grall <julien.grall@arm.com>

Reviewed-by: Paul Durrant <paul.durrant@citrix.com>

> 
> ---
> 
> Andrew suggested to drop IS_VALID_PAGE in xen/tmem_xen.h. His
> comment
> was:
> 
> "/sigh  This is tautological.  The definition of a "valid mfn" in this
> case is one for which we have frametable entry, and by having a struct
> page_info in our hands, this is by definition true (unless you have a
> wild pointer, at which point your bug is elsewhere).
> 
> IS_VALID_PAGE() is only ever used in assertions and never usefully, so
> instead I would remove it entirely rather than trying to fix it up."
> 
> I can remove the function in a separate patch at the begining of the
> series if Konrad (TMEM maintainer) is happy with that.
> 
> Cc: Stefano Stabellini <sstabellini@kernel.org>
> Cc: Julien Grall <julien.grall@arm.com>
> Cc: Andrew Cooper <andrew.cooper3@citrix.com>
> Cc: George Dunlap <George.Dunlap@eu.citrix.com>
> Cc: Ian Jackson <ian.jackson@eu.citrix.com>
> Cc: Jan Beulich <jbeulich@suse.com>
> Cc: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
> Cc: Tim Deegan <tim@xen.org>
> Cc: Wei Liu <wei.liu2@citrix.com>
> Cc: Razvan Cojocaru <rcojocaru@bitdefender.com>
> Cc: Tamas K Lengyel <tamas@tklengyel.com>
> Cc: Paul Durrant <paul.durrant@citrix.com>
> Cc: Boris Ostrovsky <boris.ostrovsky@oracle.com>
> Cc: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
> Cc: Jun Nakajima <jun.nakajima@intel.com>
> Cc: Kevin Tian <kevin.tian@intel.com>
> Cc: George Dunlap <george.dunlap@eu.citrix.com>
> Cc: Gang Wei <gang.wei@intel.com>
> Cc: Shane Wang <shane.wang@intel.com>
> 
>     Changes in v4:
>         - Drop __page_to_mfn and __mfn_to_page. Reword the commit
>         title/message to reflect that.
> 
>     Changes in v3:
>         - Rebase on the latest staging and fix some conflicts. Tags
>         haven't be retained.
>         - Switch the printf format to PRI_mfn
> 
>     Changes in v2:
>         - Some part have been moved in separate patch
>         - Remove one spurious comment
>         - Convert domain_page_to_mfn to use mfn_t
> ---
>  xen/arch/arm/domain_build.c             |  2 --
>  xen/arch/arm/kernel.c                   |  2 +-
>  xen/arch/arm/mem_access.c               |  2 +-
>  xen/arch/arm/mm.c                       |  8 ++++----
>  xen/arch/arm/p2m.c                      | 10 ++--------
>  xen/arch/x86/cpu/vpmu.c                 |  4 ++--
>  xen/arch/x86/domain.c                   | 21 +++++++++++----------
>  xen/arch/x86/domain_page.c              |  6 +++---
>  xen/arch/x86/hvm/dm.c                   |  2 +-
>  xen/arch/x86/hvm/dom0_build.c           |  6 +++---
>  xen/arch/x86/hvm/emulate.c              |  6 +++---
>  xen/arch/x86/hvm/hvm.c                  | 12 ++++++------
>  xen/arch/x86/hvm/ioreq.c                |  4 ++--
>  xen/arch/x86/hvm/stdvga.c               |  2 +-
>  xen/arch/x86/hvm/svm/svm.c              |  4 ++--
>  xen/arch/x86/hvm/viridian.c             |  6 +++---
>  xen/arch/x86/hvm/vmx/vmcs.c             |  2 +-
>  xen/arch/x86/hvm/vmx/vmx.c              | 10 +++++-----
>  xen/arch/x86/hvm/vmx/vvmx.c             |  6 +++---
>  xen/arch/x86/mm.c                       |  4 ----
>  xen/arch/x86/mm/guest_walk.c            |  6 +++---
>  xen/arch/x86/mm/hap/guest_walk.c        |  2 +-
>  xen/arch/x86/mm/hap/hap.c               |  6 ------
>  xen/arch/x86/mm/hap/nested_ept.c        |  2 +-
>  xen/arch/x86/mm/mem_sharing.c           |  5 -----
>  xen/arch/x86/mm/p2m-ept.c               |  8 ++++----
>  xen/arch/x86/mm/p2m-pod.c               |  6 ------
>  xen/arch/x86/mm/p2m.c                   |  6 ------
>  xen/arch/x86/mm/paging.c                |  6 ------
>  xen/arch/x86/mm/shadow/private.h        | 16 ++--------------
>  xen/arch/x86/numa.c                     |  2 +-
>  xen/arch/x86/physdev.c                  |  2 +-
>  xen/arch/x86/pv/callback.c              |  6 ------
>  xen/arch/x86/pv/descriptor-tables.c     |  6 ------
>  xen/arch/x86/pv/dom0_build.c            | 14 +++++++-------
>  xen/arch/x86/pv/domain.c                |  6 ------
>  xen/arch/x86/pv/emul-gate-op.c          |  6 ------
>  xen/arch/x86/pv/emul-priv-op.c          | 10 ----------
>  xen/arch/x86/pv/grant_table.c           |  6 ------
>  xen/arch/x86/pv/ro-page-fault.c         |  6 ------
>  xen/arch/x86/pv/shim.c                  |  4 +---
>  xen/arch/x86/smpboot.c                  |  6 ------
>  xen/arch/x86/tboot.c                    |  4 ++--
>  xen/arch/x86/traps.c                    |  4 ++--
>  xen/arch/x86/x86_64/mm.c                |  6 ------
>  xen/common/domain.c                     |  4 ++--
>  xen/common/grant_table.c                |  6 ------
>  xen/common/kimage.c                     |  6 ------
>  xen/common/memory.c                     |  6 ------
>  xen/common/page_alloc.c                 |  6 ------
>  xen/common/tmem.c                       |  2 +-
>  xen/common/tmem_xen.c                   |  4 ----
>  xen/common/trace.c                      |  4 ++--
>  xen/common/vmap.c                       |  6 +-----
>  xen/common/xenoprof.c                   |  2 --
>  xen/drivers/passthrough/amd/iommu_map.c | 12 ++++++------
>  xen/drivers/passthrough/iommu.c         |  2 +-
>  xen/drivers/passthrough/x86/iommu.c     |  2 +-
>  xen/include/asm-arm/mm.h                | 20 ++++++++++----------
>  xen/include/asm-arm/p2m.h               |  4 ++--
>  xen/include/asm-x86/mm.h                | 12 ++++++------
>  xen/include/asm-x86/p2m.h               |  2 +-
>  xen/include/asm-x86/page.h              | 32 +++++++++++++++-----------------
>  xen/include/xen/domain_page.h           |  8 ++++----
>  xen/include/xen/mm.h                    |  5 -----
>  xen/include/xen/tmem_xen.h              |  2 +-
>  66 files changed, 132 insertions(+), 285 deletions(-)
> 
> diff --git a/xen/arch/arm/domain_build.c b/xen/arch/arm/domain_build.c
> index 155c952349..fd6c2482de 100644
> --- a/xen/arch/arm/domain_build.c
> +++ b/xen/arch/arm/domain_build.c
> @@ -49,8 +49,6 @@ struct map_range_data
>  /* Override macros from asm/page.h to make them work with mfn_t */
>  #undef virt_to_mfn
>  #define virt_to_mfn(va) _mfn(__virt_to_mfn(va))
> -#undef page_to_mfn
> -#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
> 
>  //#define DEBUG_11_ALLOCATION
>  #ifdef DEBUG_11_ALLOCATION
> diff --git a/xen/arch/arm/kernel.c b/xen/arch/arm/kernel.c
> index 2fb0b9684d..8fdfd91543 100644
> --- a/xen/arch/arm/kernel.c
> +++ b/xen/arch/arm/kernel.c
> @@ -286,7 +286,7 @@ static __init int kernel_decompress(struct
> bootmodule *mod)
>          iounmap(input);
>          return -ENOMEM;
>      }
> -    mfn = _mfn(page_to_mfn(pages));
> +    mfn = page_to_mfn(pages);
>      output = __vmap(&mfn, 1 << kernel_order_out, 1, 1, PAGE_HYPERVISOR,
> VMAP_DEFAULT);
> 
>      rc = perform_gunzip(output, input, size);
> diff --git a/xen/arch/arm/mem_access.c b/xen/arch/arm/mem_access.c
> index 0f2cbb81d3..112e291cba 100644
> --- a/xen/arch/arm/mem_access.c
> +++ b/xen/arch/arm/mem_access.c
> @@ -210,7 +210,7 @@ p2m_mem_access_check_and_get_page(vaddr_t
> gva, unsigned long flag,
>      if ( t != p2m_ram_rw )
>          goto err;
> 
> -    page = mfn_to_page(mfn_x(mfn));
> +    page = mfn_to_page(mfn);
> 
>      if ( unlikely(!get_page(page, v->domain)) )
>          page = NULL;
> diff --git a/xen/arch/arm/mm.c b/xen/arch/arm/mm.c
> index db74466a16..510a5a2050 100644
> --- a/xen/arch/arm/mm.c
> +++ b/xen/arch/arm/mm.c
> @@ -477,7 +477,7 @@ void unmap_domain_page(const void *va)
>      local_irq_restore(flags);
>  }
> 
> -unsigned long domain_page_map_to_mfn(const void *ptr)
> +mfn_t domain_page_map_to_mfn(const void *ptr)
>  {
>      unsigned long va = (unsigned long)ptr;
>      lpae_t *map = this_cpu(xen_dommap);
> @@ -485,12 +485,12 @@ unsigned long domain_page_map_to_mfn(const
> void *ptr)
>      unsigned long offset = (va>>THIRD_SHIFT) & LPAE_ENTRY_MASK;
> 
>      if ( va >= VMAP_VIRT_START && va < VMAP_VIRT_END )
> -        return __virt_to_mfn(va);
> +        return virt_to_mfn(va);
> 
>      ASSERT(slot >= 0 && slot < DOMHEAP_ENTRIES);
>      ASSERT(map[slot].pt.avail != 0);
> 
> -    return map[slot].pt.base + offset;
> +    return _mfn(map[slot].pt.base + offset);
>  }
>  #endif
> 
> @@ -1287,7 +1287,7 @@ int xenmem_add_to_physmap_one(
>              return -EINVAL;
>          }
> 
> -        mfn = _mfn(page_to_mfn(page));
> +        mfn = page_to_mfn(page);
>          t = p2m_map_foreign;
> 
>          rcu_unlock_domain(od);
> diff --git a/xen/arch/arm/p2m.c b/xen/arch/arm/p2m.c
> index 65e8b9c6ea..8b16c8322d 100644
> --- a/xen/arch/arm/p2m.c
> +++ b/xen/arch/arm/p2m.c
> @@ -37,12 +37,6 @@ static unsigned int __read_mostly max_vmid =
> MAX_VMID_8_BIT;
> 
>  #define P2M_ROOT_PAGES    (1<<P2M_ROOT_ORDER)
> 
> -/* Override macros from asm/mm.h to make them work with mfn_t */
> -#undef mfn_to_page
> -#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
> -#undef page_to_mfn
> -#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
> -
>  unsigned int __read_mostly p2m_ipa_bits;
> 
>  /* Helpers to lookup the properties of each level */
> @@ -90,8 +84,8 @@ void dump_p2m_lookup(struct domain *d, paddr_t
> addr)
> 
>      printk("dom%d IPA 0x%"PRIpaddr"\n", d->domain_id, addr);
> 
> -    printk("P2M @ %p mfn:0x%lx\n",
> -           p2m->root, __page_to_mfn(p2m->root));
> +    printk("P2M @ %p mfn:%#"PRI_mfn"\n",
> +           p2m->root, mfn_x(page_to_mfn(p2m->root)));
> 
>      dump_pt_walk(page_to_maddr(p2m->root), addr,
>                   P2M_ROOT_LEVEL, P2M_ROOT_PAGES);
> diff --git a/xen/arch/x86/cpu/vpmu.c b/xen/arch/x86/cpu/vpmu.c
> index 7baf4614be..b978e05613 100644
> --- a/xen/arch/x86/cpu/vpmu.c
> +++ b/xen/arch/x86/cpu/vpmu.c
> @@ -653,7 +653,7 @@ static void pvpmu_finish(struct domain *d,
> xen_pmu_params_t *params)
>  {
>      struct vcpu *v;
>      struct vpmu_struct *vpmu;
> -    uint64_t mfn;
> +    mfn_t mfn;
>      void *xenpmu_data;
> 
>      if ( (params->vcpu >= d->max_vcpus) || (d->vcpu[params->vcpu] ==
> NULL) )
> @@ -675,7 +675,7 @@ static void pvpmu_finish(struct domain *d,
> xen_pmu_params_t *params)
>      if ( xenpmu_data )
>      {
>          mfn = domain_page_map_to_mfn(xenpmu_data);
> -        ASSERT(mfn_valid(_mfn(mfn)));
> +        ASSERT(mfn_valid(mfn));
>          unmap_domain_page_global(xenpmu_data);
>          put_page_and_type(mfn_to_page(mfn));
>      }
> diff --git a/xen/arch/x86/domain.c b/xen/arch/x86/domain.c
> index f93327b0a2..44ba52b7ba 100644
> --- a/xen/arch/x86/domain.c
> +++ b/xen/arch/x86/domain.c
> @@ -195,7 +195,7 @@ void dump_pageframe_info(struct domain *d)
>                  }
>              }
>              printk("    DomPage %p: caf=%08lx, taf=%" PRtype_info "\n",
> -                   _p(page_to_mfn(page)),
> +                   _p(mfn_x(page_to_mfn(page))),
>                     page->count_info, page->u.inuse.type_info);
>          }
>          spin_unlock(&d->page_alloc_lock);
> @@ -208,7 +208,7 @@ void dump_pageframe_info(struct domain *d)
>      page_list_for_each ( page, &d->xenpage_list )
>      {
>          printk("    XenPage %p: caf=%08lx, taf=%" PRtype_info "\n",
> -               _p(page_to_mfn(page)),
> +               _p(mfn_x(page_to_mfn(page))),
>                 page->count_info, page->u.inuse.type_info);
>      }
>      spin_unlock(&d->page_alloc_lock);
> @@ -635,7 +635,8 @@ int arch_domain_soft_reset(struct domain *d)
>      struct page_info *page = virt_to_page(d->shared_info), *new_page;
>      int ret = 0;
>      struct domain *owner;
> -    unsigned long mfn, gfn;
> +    mfn_t mfn;
> +    unsigned long gfn;
>      p2m_type_t p2mt;
>      unsigned int i;
> 
> @@ -669,7 +670,7 @@ int arch_domain_soft_reset(struct domain *d)
>      ASSERT( owner == d );
> 
>      mfn = page_to_mfn(page);
> -    gfn = mfn_to_gmfn(d, mfn);
> +    gfn = mfn_to_gmfn(d, mfn_x(mfn));
> 
>      /*
>       * gfn == INVALID_GFN indicates that the shared_info page was never
> mapped
> @@ -678,7 +679,7 @@ int arch_domain_soft_reset(struct domain *d)
>      if ( gfn == gfn_x(INVALID_GFN) )
>          goto exit_put_page;
> 
> -    if ( mfn_x(get_gfn_query(d, gfn, &p2mt)) != mfn )
> +    if ( !mfn_eq(get_gfn_query(d, gfn, &p2mt), mfn) )
>      {
>          printk(XENLOG_G_ERR "Failed to get Dom%d's shared_info GFN
> (%lx)\n",
>                 d->domain_id, gfn);
> @@ -695,7 +696,7 @@ int arch_domain_soft_reset(struct domain *d)
>          goto exit_put_gfn;
>      }
> 
> -    ret = guest_physmap_remove_page(d, _gfn(gfn), _mfn(mfn),
> PAGE_ORDER_4K);
> +    ret = guest_physmap_remove_page(d, _gfn(gfn), mfn,
> PAGE_ORDER_4K);
>      if ( ret )
>      {
>          printk(XENLOG_G_ERR "Failed to remove Dom%d's shared_info frame
> %lx\n",
> @@ -704,7 +705,7 @@ int arch_domain_soft_reset(struct domain *d)
>          goto exit_put_gfn;
>      }
> 
> -    ret = guest_physmap_add_page(d, _gfn(gfn),
> _mfn(page_to_mfn(new_page)),
> +    ret = guest_physmap_add_page(d, _gfn(gfn), page_to_mfn(new_page),
>                                   PAGE_ORDER_4K);
>      if ( ret )
>      {
> @@ -1002,7 +1003,7 @@ int arch_set_info_guest(
>                  {
>                      if ( (page->u.inuse.type_info & PGT_type_mask) ==
>                           PGT_l4_page_table )
> -                        done = !fill_ro_mpt(_mfn(page_to_mfn(page)));
> +                        done = !fill_ro_mpt(page_to_mfn(page));
> 
>                      page_unlock(page);
>                  }
> @@ -1131,7 +1132,7 @@ int arch_set_info_guest(
>          l4_pgentry_t *l4tab;
> 
>          l4tab = map_domain_page(pagetable_get_mfn(v->arch.guest_table));
> -        *l4tab = l4e_from_pfn(page_to_mfn(cr3_page),
> +        *l4tab = l4e_from_mfn(page_to_mfn(cr3_page),
>              _PAGE_PRESENT|_PAGE_RW|_PAGE_USER|_PAGE_ACCESSED);
>          unmap_domain_page(l4tab);
>      }
> @@ -1997,7 +1998,7 @@ int domain_relinquish_resources(struct domain *d)
>          if ( d->arch.pirq_eoi_map != NULL )
>          {
>              unmap_domain_page_global(d->arch.pirq_eoi_map);
> -            put_page_and_type(mfn_to_page(d->arch.pirq_eoi_map_mfn));
> +            put_page_and_type(mfn_to_page(_mfn(d-
> >arch.pirq_eoi_map_mfn)));
>              d->arch.pirq_eoi_map = NULL;
>              d->arch.auto_unmask = 0;
>          }
> diff --git a/xen/arch/x86/domain_page.c b/xen/arch/x86/domain_page.c
> index 3432a854dd..88046b39c9 100644
> --- a/xen/arch/x86/domain_page.c
> +++ b/xen/arch/x86/domain_page.c
> @@ -331,13 +331,13 @@ void unmap_domain_page_global(const void *ptr)
>  }
> 
>  /* Translate a map-domain-page'd address to the underlying MFN */
> -unsigned long domain_page_map_to_mfn(const void *ptr)
> +mfn_t domain_page_map_to_mfn(const void *ptr)
>  {
>      unsigned long va = (unsigned long)ptr;
>      const l1_pgentry_t *pl1e;
> 
>      if ( va >= DIRECTMAP_VIRT_START )
> -        return virt_to_mfn(ptr);
> +        return _mfn(virt_to_mfn(ptr));
> 
>      if ( va >= VMAP_VIRT_START && va < VMAP_VIRT_END )
>      {
> @@ -350,5 +350,5 @@ unsigned long domain_page_map_to_mfn(const void
> *ptr)
>          pl1e = &__linear_l1_table[l1_linear_offset(va)];
>      }
> 
> -    return l1e_get_pfn(*pl1e);
> +    return l1e_get_mfn(*pl1e);
>  }
> diff --git a/xen/arch/x86/hvm/dm.c b/xen/arch/x86/hvm/dm.c
> index 7788577a73..cf1e600998 100644
> --- a/xen/arch/x86/hvm/dm.c
> +++ b/xen/arch/x86/hvm/dm.c
> @@ -193,7 +193,7 @@ static int modified_memory(struct domain *d,
>                   * These are most probably not page tables any more
>                   * don't take a long time and don't die either.
>                   */
> -                sh_remove_shadows(d, _mfn(page_to_mfn(page)), 1, 0);
> +                sh_remove_shadows(d, page_to_mfn(page), 1, 0);
>                  put_page(page);
>              }
>          }
> diff --git a/xen/arch/x86/hvm/dom0_build.c
> b/xen/arch/x86/hvm/dom0_build.c
> index afebaec70b..717ffff584 100644
> --- a/xen/arch/x86/hvm/dom0_build.c
> +++ b/xen/arch/x86/hvm/dom0_build.c
> @@ -119,7 +119,7 @@ static int __init pvh_populate_memory_range(struct
> domain *d,
>              continue;
>          }
> 
> -        rc = guest_physmap_add_page(d, _gfn(start),
> _mfn(page_to_mfn(page)),
> +        rc = guest_physmap_add_page(d, _gfn(start), page_to_mfn(page),
>                                      order);
>          if ( rc != 0 )
>          {
> @@ -269,7 +269,7 @@ static int __init
> pvh_setup_vmx_realmode_helpers(struct domain *d)
>      }
>      write_32bit_pse_identmap(ident_pt);
>      unmap_domain_page(ident_pt);
> -    put_page(mfn_to_page(mfn_x(mfn)));
> +    put_page(mfn_to_page(mfn));
>      d->arch.hvm_domain.params[HVM_PARAM_IDENT_PT] = gaddr;
>      if ( pvh_add_mem_range(d, gaddr, gaddr + PAGE_SIZE, E820_RESERVED) )
>              printk("Unable to set identity page tables as reserved in the memory
> map\n");
> @@ -287,7 +287,7 @@ static void __init pvh_steal_low_ram(struct domain
> *d, unsigned long start,
> 
>      for ( mfn = start; mfn < start + nr_pages; mfn++ )
>      {
> -        struct page_info *pg = mfn_to_page(mfn);
> +        struct page_info *pg = mfn_to_page(_mfn(mfn));
>          int rc;
> 
>          rc = unshare_xen_page_with_guest(pg, dom_io);
> diff --git a/xen/arch/x86/hvm/emulate.c b/xen/arch/x86/hvm/emulate.c
> index 3b824553ab..fdd7177303 100644
> --- a/xen/arch/x86/hvm/emulate.c
> +++ b/xen/arch/x86/hvm/emulate.c
> @@ -591,7 +591,7 @@ static void *hvmemul_map_linear_addr(
>              goto unhandleable;
>          }
> 
> -        *mfn++ = _mfn(page_to_mfn(page));
> +        *mfn++ = page_to_mfn(page);
> 
>          if ( p2m_is_discard_write(p2mt) )
>          {
> @@ -623,7 +623,7 @@ static void *hvmemul_map_linear_addr(
>   out:
>      /* Drop all held references. */
>      while ( mfn-- > hvmemul_ctxt->mfn )
> -        put_page(mfn_to_page(mfn_x(*mfn)));
> +        put_page(mfn_to_page(*mfn));
> 
>      return err;
>  }
> @@ -649,7 +649,7 @@ static void hvmemul_unmap_linear_addr(
>      {
>          ASSERT(mfn_valid(*mfn));
>          paging_mark_dirty(currd, *mfn);
> -        put_page(mfn_to_page(mfn_x(*mfn)));
> +        put_page(mfn_to_page(*mfn));
> 
>          *mfn++ = _mfn(0); /* Clean slot for map()'s error checking. */
>      }
> diff --git a/xen/arch/x86/hvm/hvm.c b/xen/arch/x86/hvm/hvm.c
> index 91bc3e8b27..74de968315 100644
> --- a/xen/arch/x86/hvm/hvm.c
> +++ b/xen/arch/x86/hvm/hvm.c
> @@ -2247,7 +2247,7 @@ int hvm_set_cr0(unsigned long value, bool_t
> may_defer)
>              v->arch.guest_table = pagetable_from_page(page);
> 
>              HVM_DBG_LOG(DBG_LEVEL_VMMU, "Update CR3 value = %lx, mfn =
> %lx",
> -                        v->arch.hvm_vcpu.guest_cr[3], page_to_mfn(page));
> +                        v->arch.hvm_vcpu.guest_cr[3], mfn_x(page_to_mfn(page)));
>          }
>      }
>      else if ( !(value & X86_CR0_PG) && (old_value & X86_CR0_PG) )
> @@ -2624,7 +2624,7 @@ void *hvm_map_guest_frame_ro(unsigned long
> gfn, bool_t permanent)
> 
>  void hvm_unmap_guest_frame(void *p, bool_t permanent)
>  {
> -    unsigned long mfn;
> +    mfn_t mfn;
>      struct page_info *page;
> 
>      if ( !p )
> @@ -2645,7 +2645,7 @@ void hvm_unmap_guest_frame(void *p, bool_t
> permanent)
>          list_for_each_entry(track, &d->arch.hvm_domain.write_map.list, list)
>              if ( track->page == page )
>              {
> -                paging_mark_dirty(d, _mfn(mfn));
> +                paging_mark_dirty(d, mfn);
>                  list_del(&track->list);
>                  xfree(track);
>                  break;
> @@ -2662,7 +2662,7 @@ void
> hvm_mapped_guest_frames_mark_dirty(struct domain *d)
> 
>      spin_lock(&d->arch.hvm_domain.write_map.lock);
>      list_for_each_entry(track, &d->arch.hvm_domain.write_map.list, list)
> -        paging_mark_dirty(d, _mfn(page_to_mfn(track->page)));
> +        paging_mark_dirty(d, page_to_mfn(track->page));
>      spin_unlock(&d->arch.hvm_domain.write_map.lock);
>  }
> 
> @@ -3236,8 +3236,8 @@ static enum hvm_translation_result __hvm_copy(
> 
>                  if ( xchg(&lastpage, gfn_x(gfn)) != gfn_x(gfn) )
>                      dprintk(XENLOG_G_DEBUG,
> -                            "%pv attempted write to read-only gfn %#lx (mfn=%#lx)\n",
> -                            v, gfn_x(gfn), page_to_mfn(page));
> +                            "%pv attempted write to read-only gfn %#lx
> (mfn=%#"PRI_mfn")\n",
> +                            v, gfn_x(gfn), mfn_x(page_to_mfn(page)));
>              }
>              else
>              {
> diff --git a/xen/arch/x86/hvm/ioreq.c b/xen/arch/x86/hvm/ioreq.c
> index 7e66965bcd..a1c2218fdc 100644
> --- a/xen/arch/x86/hvm/ioreq.c
> +++ b/xen/arch/x86/hvm/ioreq.c
> @@ -268,7 +268,7 @@ static void hvm_remove_ioreq_gfn(
>      struct domain *d, struct hvm_ioreq_page *iorp)
>  {
>      if ( guest_physmap_remove_page(d, _gfn(iorp->gfn),
> -                                   _mfn(page_to_mfn(iorp->page)), 0) )
> +                                   page_to_mfn(iorp->page), 0) )
>          domain_crash(d);
>      clear_page(iorp->va);
>  }
> @@ -281,7 +281,7 @@ static int hvm_add_ioreq_gfn(
>      clear_page(iorp->va);
> 
>      rc = guest_physmap_add_page(d, _gfn(iorp->gfn),
> -                                _mfn(page_to_mfn(iorp->page)), 0);
> +                                page_to_mfn(iorp->page), 0);
>      if ( rc == 0 )
>          paging_mark_pfn_dirty(d, _pfn(iorp->gfn));
> 
> diff --git a/xen/arch/x86/hvm/stdvga.c b/xen/arch/x86/hvm/stdvga.c
> index 088fbdf8ce..925bab2438 100644
> --- a/xen/arch/x86/hvm/stdvga.c
> +++ b/xen/arch/x86/hvm/stdvga.c
> @@ -590,7 +590,7 @@ void stdvga_init(struct domain *d)
>          if ( pg == NULL )
>              break;
>          s->vram_page[i] = pg;
> -        clear_domain_page(_mfn(page_to_mfn(pg)));
> +        clear_domain_page(page_to_mfn(pg));
>      }
> 
>      if ( i == ARRAY_SIZE(s->vram_page) )
> diff --git a/xen/arch/x86/hvm/svm/svm.c b/xen/arch/x86/hvm/svm/svm.c
> index 9f58afc2d8..30e951039e 100644
> --- a/xen/arch/x86/hvm/svm/svm.c
> +++ b/xen/arch/x86/hvm/svm/svm.c
> @@ -1543,7 +1543,7 @@ static int svm_cpu_up_prepare(unsigned int cpu)
>          if ( !pg )
>              goto err;
> 
> -        clear_domain_page(_mfn(page_to_mfn(pg)));
> +        clear_domain_page(page_to_mfn(pg));
>          *this_hsa = page_to_maddr(pg);
>      }
> 
> @@ -1553,7 +1553,7 @@ static int svm_cpu_up_prepare(unsigned int cpu)
>          if ( !pg )
>              goto err;
> 
> -        clear_domain_page(_mfn(page_to_mfn(pg)));
> +        clear_domain_page(page_to_mfn(pg));
>          *this_vmcb = page_to_maddr(pg);
>      }
> 
> diff --git a/xen/arch/x86/hvm/viridian.c b/xen/arch/x86/hvm/viridian.c
> index 70aab520bc..d6aa89d0b7 100644
> --- a/xen/arch/x86/hvm/viridian.c
> +++ b/xen/arch/x86/hvm/viridian.c
> @@ -354,7 +354,7 @@ static void enable_hypercall_page(struct domain *d)
>          if ( page )
>              put_page(page);
>          gdprintk(XENLOG_WARNING, "Bad GMFN %#"PRI_gfn" (MFN
> %#"PRI_mfn")\n",
> -                 gmfn, page ? page_to_mfn(page) : mfn_x(INVALID_MFN));
> +                 gmfn, mfn_x(page ? page_to_mfn(page) : INVALID_MFN));
>          return;
>      }
> 
> @@ -414,7 +414,7 @@ static void initialize_vp_assist(struct vcpu *v)
> 
>   fail:
>      gdprintk(XENLOG_WARNING, "Bad GMFN %#"PRI_gfn" (MFN
> %#"PRI_mfn")\n", gmfn,
> -             page ? page_to_mfn(page) : mfn_x(INVALID_MFN));
> +             mfn_x(page ? page_to_mfn(page) : INVALID_MFN));
>  }
> 
>  static void teardown_vp_assist(struct vcpu *v)
> @@ -492,7 +492,7 @@ static void update_reference_tsc(struct domain *d,
> bool_t initialize)
>          if ( page )
>              put_page(page);
>          gdprintk(XENLOG_WARNING, "Bad GMFN %#"PRI_gfn" (MFN
> %#"PRI_mfn")\n",
> -                 gmfn, page ? page_to_mfn(page) : mfn_x(INVALID_MFN));
> +                 gmfn, mfn_x(page ? page_to_mfn(page) : INVALID_MFN));
>          return;
>      }
> 
> diff --git a/xen/arch/x86/hvm/vmx/vmcs.c b/xen/arch/x86/hvm/vmx/vmcs.c
> index e7818caed0..a5a160746a 100644
> --- a/xen/arch/x86/hvm/vmx/vmcs.c
> +++ b/xen/arch/x86/hvm/vmx/vmcs.c
> @@ -1433,7 +1433,7 @@ int vmx_vcpu_enable_pml(struct vcpu *v)
> 
>      vmx_vmcs_enter(v);
> 
> -    __vmwrite(PML_ADDRESS, page_to_mfn(v->arch.hvm_vmx.pml_pg) <<
> PAGE_SHIFT);
> +    __vmwrite(PML_ADDRESS, page_to_maddr(v->arch.hvm_vmx.pml_pg));
>      __vmwrite(GUEST_PML_INDEX, NR_PML_ENTRIES - 1);
> 
>      v->arch.hvm_vmx.secondary_exec_control |=
> SECONDARY_EXEC_ENABLE_PML;
> diff --git a/xen/arch/x86/hvm/vmx/vmx.c b/xen/arch/x86/hvm/vmx/vmx.c
> index 5cd689e823..cf7f7e1bb7 100644
> --- a/xen/arch/x86/hvm/vmx/vmx.c
> +++ b/xen/arch/x86/hvm/vmx/vmx.c
> @@ -2968,7 +2968,7 @@ gp_fault:
>  static int vmx_alloc_vlapic_mapping(struct domain *d)
>  {
>      struct page_info *pg;
> -    unsigned long mfn;
> +    mfn_t mfn;
> 
>      if ( !cpu_has_vmx_virtualize_apic_accesses )
>          return 0;
> @@ -2977,10 +2977,10 @@ static int vmx_alloc_vlapic_mapping(struct
> domain *d)
>      if ( !pg )
>          return -ENOMEM;
>      mfn = page_to_mfn(pg);
> -    clear_domain_page(_mfn(mfn));
> +    clear_domain_page(mfn);
>      share_xen_page_with_guest(pg, d, XENSHARE_writable);
> -    d->arch.hvm_domain.vmx.apic_access_mfn = mfn;
> -    set_mmio_p2m_entry(d, paddr_to_pfn(APIC_DEFAULT_PHYS_BASE),
> _mfn(mfn),
> +    d->arch.hvm_domain.vmx.apic_access_mfn = mfn_x(mfn);
> +    set_mmio_p2m_entry(d, paddr_to_pfn(APIC_DEFAULT_PHYS_BASE),
> mfn,
>                         PAGE_ORDER_4K, p2m_get_hostp2m(d)->default_access);
> 
>      return 0;
> @@ -2991,7 +2991,7 @@ static void vmx_free_vlapic_mapping(struct
> domain *d)
>      unsigned long mfn = d->arch.hvm_domain.vmx.apic_access_mfn;
> 
>      if ( mfn != 0 )
> -        free_shared_domheap_page(mfn_to_page(mfn));
> +        free_shared_domheap_page(mfn_to_page(_mfn(mfn)));
>  }
> 
>  static void vmx_install_vlapic_mapping(struct vcpu *v)
> diff --git a/xen/arch/x86/hvm/vmx/vvmx.c
> b/xen/arch/x86/hvm/vmx/vvmx.c
> index dfe97b9705..db92ae6660 100644
> --- a/xen/arch/x86/hvm/vmx/vvmx.c
> +++ b/xen/arch/x86/hvm/vmx/vvmx.c
> @@ -84,7 +84,7 @@ int nvmx_vcpu_initialise(struct vcpu *v)
>          }
>          v->arch.hvm_vmx.vmread_bitmap = vmread_bitmap;
> 
> -        clear_domain_page(_mfn(page_to_mfn(vmread_bitmap)));
> +        clear_domain_page(page_to_mfn(vmread_bitmap));
> 
>          vmwrite_bitmap = alloc_domheap_page(NULL, 0);
>          if ( !vmwrite_bitmap )
> @@ -1726,7 +1726,7 @@ int nvmx_handle_vmptrld(struct cpu_user_regs
> *regs)
>                  nvcpu->nv_vvmcx = vvmcx;
>                  nvcpu->nv_vvmcxaddr = gpa;
>                  v->arch.hvm_vmx.vmcs_shadow_maddr =
> -                    pfn_to_paddr(domain_page_map_to_mfn(vvmcx));
> +                    mfn_to_maddr(domain_page_map_to_mfn(vvmcx));
>              }
>              else
>              {
> @@ -1812,7 +1812,7 @@ int nvmx_handle_vmclear(struct cpu_user_regs
> *regs)
>          {
>              if ( writable )
>                  clear_vvmcs_launched(&nvmx->launched_list,
> -                                     domain_page_map_to_mfn(vvmcs));
> +                                     mfn_x(domain_page_map_to_mfn(vvmcs)));
>              else
>                  rc = VMFAIL_VALID;
>              hvm_unmap_guest_frame(vvmcs, 0);
> diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c
> index d33b6bfa9d..eeaf03eb49 100644
> --- a/xen/arch/x86/mm.c
> +++ b/xen/arch/x86/mm.c
> @@ -131,10 +131,6 @@
>  #include "pv/mm.h"
> 
>  /* Override macros from asm/page.h to make them work with mfn_t */
> -#undef mfn_to_page
> -#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
> -#undef page_to_mfn
> -#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
>  #undef virt_to_mfn
>  #define virt_to_mfn(v) _mfn(__virt_to_mfn(v))
> 
> diff --git a/xen/arch/x86/mm/guest_walk.c
> b/xen/arch/x86/mm/guest_walk.c
> index 6055fec1ad..f67aeda3d0 100644
> --- a/xen/arch/x86/mm/guest_walk.c
> +++ b/xen/arch/x86/mm/guest_walk.c
> @@ -469,20 +469,20 @@ guest_walk_tables(struct vcpu *v, struct
> p2m_domain *p2m,
>      if ( l3p )
>      {
>          unmap_domain_page(l3p);
> -        put_page(mfn_to_page(mfn_x(gw->l3mfn)));
> +        put_page(mfn_to_page(gw->l3mfn));
>      }
>  #endif
>  #if GUEST_PAGING_LEVELS >= 3
>      if ( l2p )
>      {
>          unmap_domain_page(l2p);
> -        put_page(mfn_to_page(mfn_x(gw->l2mfn)));
> +        put_page(mfn_to_page(gw->l2mfn));
>      }
>  #endif
>      if ( l1p )
>      {
>          unmap_domain_page(l1p);
> -        put_page(mfn_to_page(mfn_x(gw->l1mfn)));
> +        put_page(mfn_to_page(gw->l1mfn));
>      }
> 
>      return walk_ok;
> diff --git a/xen/arch/x86/mm/hap/guest_walk.c
> b/xen/arch/x86/mm/hap/guest_walk.c
> index c550017ba4..cb3f9cebe7 100644
> --- a/xen/arch/x86/mm/hap/guest_walk.c
> +++ b/xen/arch/x86/mm/hap/guest_walk.c
> @@ -83,7 +83,7 @@ unsigned long
> hap_p2m_ga_to_gfn(GUEST_PAGING_LEVELS)(
>          *pfec &= ~PFEC_page_present;
>          goto out_tweak_pfec;
>      }
> -    top_mfn = _mfn(page_to_mfn(top_page));
> +    top_mfn = page_to_mfn(top_page);
> 
>      /* Map the top-level table and call the tree-walker */
>      ASSERT(mfn_valid(top_mfn));
> diff --git a/xen/arch/x86/mm/hap/hap.c b/xen/arch/x86/mm/hap/hap.c
> index 003c2d8896..370a490aad 100644
> --- a/xen/arch/x86/mm/hap/hap.c
> +++ b/xen/arch/x86/mm/hap/hap.c
> @@ -42,12 +42,6 @@
> 
>  #include "private.h"
> 
> -/* Override macros from asm/page.h to make them work with mfn_t */
> -#undef mfn_to_page
> -#define mfn_to_page(_m) __mfn_to_page(mfn_x(_m))
> -#undef page_to_mfn
> -#define page_to_mfn(_pg) _mfn(__page_to_mfn(_pg))
> -
>  /************************************************/
>  /*          HAP VRAM TRACKING SUPPORT           */
>  /************************************************/
> diff --git a/xen/arch/x86/mm/hap/nested_ept.c
> b/xen/arch/x86/mm/hap/nested_ept.c
> index 14b1bb01e9..1738df69f6 100644
> --- a/xen/arch/x86/mm/hap/nested_ept.c
> +++ b/xen/arch/x86/mm/hap/nested_ept.c
> @@ -173,7 +173,7 @@ nept_walk_tables(struct vcpu *v, unsigned long l2ga,
> ept_walk_t *gw)
>              goto map_err;
>          gw->lxe[lvl] = lxp[ept_lvl_table_offset(l2ga, lvl)];
>          unmap_domain_page(lxp);
> -        put_page(mfn_to_page(mfn_x(lxmfn)));
> +        put_page(mfn_to_page(lxmfn));
> 
>          if ( nept_non_present_check(gw->lxe[lvl]) )
>              goto non_present;
> diff --git a/xen/arch/x86/mm/mem_sharing.c
> b/xen/arch/x86/mm/mem_sharing.c
> index 57f54c55c8..fad8a9df13 100644
> --- a/xen/arch/x86/mm/mem_sharing.c
> +++ b/xen/arch/x86/mm/mem_sharing.c
> @@ -152,11 +152,6 @@ static inline shr_handle_t get_next_handle(void)
>  #define mem_sharing_enabled(d) \
>      (is_hvm_domain(d) && (d)->arch.hvm_domain.mem_sharing_enabled)
> 
> -#undef mfn_to_page
> -#define mfn_to_page(_m) __mfn_to_page(mfn_x(_m))
> -#undef page_to_mfn
> -#define page_to_mfn(_pg) _mfn(__page_to_mfn(_pg))
> -
>  static atomic_t nr_saved_mfns   = ATOMIC_INIT(0);
>  static atomic_t nr_shared_mfns  = ATOMIC_INIT(0);
> 
> diff --git a/xen/arch/x86/mm/p2m-ept.c b/xen/arch/x86/mm/p2m-ept.c
> index 66dbb3e83a..14b593923b 100644
> --- a/xen/arch/x86/mm/p2m-ept.c
> +++ b/xen/arch/x86/mm/p2m-ept.c
> @@ -74,13 +74,13 @@ static int atomic_write_ept_entry(ept_entry_t
> *entryptr, ept_entry_t new,
>                  goto out;
> 
>              rc = -ESRCH;
> -            fdom = page_get_owner(mfn_to_page(new.mfn));
> +            fdom = page_get_owner(mfn_to_page(_mfn(new.mfn)));
>              if ( fdom == NULL )
>                  goto out;
> 
>              /* get refcount on the page */
>              rc = -EBUSY;
> -            if ( !get_page(mfn_to_page(new.mfn), fdom) )
> +            if ( !get_page(mfn_to_page(_mfn(new.mfn)), fdom) )
>                  goto out;
>          }
>      }
> @@ -91,7 +91,7 @@ static int atomic_write_ept_entry(ept_entry_t
> *entryptr, ept_entry_t new,
>      write_atomic(&entryptr->epte, new.epte);
> 
>      if ( unlikely(oldmfn != mfn_x(INVALID_MFN)) )
> -        put_page(mfn_to_page(oldmfn));
> +        put_page(mfn_to_page(_mfn(oldmfn)));
> 
>      rc = 0;
> 
> @@ -270,7 +270,7 @@ static void ept_free_entry(struct p2m_domain *p2m,
> ept_entry_t *ept_entry, int l
>      }
> 
>      p2m_tlb_flush_sync(p2m);
> -    p2m_free_ptp(p2m, mfn_to_page(ept_entry->mfn));
> +    p2m_free_ptp(p2m, mfn_to_page(_mfn(ept_entry->mfn)));
>  }
> 
>  static bool_t ept_split_super_page(struct p2m_domain *p2m,
> diff --git a/xen/arch/x86/mm/p2m-pod.c b/xen/arch/x86/mm/p2m-pod.c
> index fa13e07f7c..631e9aec33 100644
> --- a/xen/arch/x86/mm/p2m-pod.c
> +++ b/xen/arch/x86/mm/p2m-pod.c
> @@ -29,12 +29,6 @@
> 
>  #include "mm-locks.h"
> 
> -/* Override macros from asm/page.h to make them work with mfn_t */
> -#undef mfn_to_page
> -#define mfn_to_page(_m) __mfn_to_page(mfn_x(_m))
> -#undef page_to_mfn
> -#define page_to_mfn(_pg) _mfn(__page_to_mfn(_pg))
> -
>  #define superpage_aligned(_x)  (((_x)&(SUPERPAGE_PAGES-1))==0)
> 
>  /* Enforce lock ordering when grabbing the "external" page_alloc lock */
> diff --git a/xen/arch/x86/mm/p2m.c b/xen/arch/x86/mm/p2m.c
> index 48e50fb5d8..9ce0a5c9e1 100644
> --- a/xen/arch/x86/mm/p2m.c
> +++ b/xen/arch/x86/mm/p2m.c
> @@ -47,12 +47,6 @@ bool_t __initdata opt_hap_1gb = 1, __initdata
> opt_hap_2mb = 1;
>  boolean_param("hap_1gb", opt_hap_1gb);
>  boolean_param("hap_2mb", opt_hap_2mb);
> 
> -/* Override macros from asm/page.h to make them work with mfn_t */
> -#undef mfn_to_page
> -#define mfn_to_page(_m) __mfn_to_page(mfn_x(_m))
> -#undef page_to_mfn
> -#define page_to_mfn(_pg) _mfn(__page_to_mfn(_pg))
> -
>  DEFINE_PERCPU_RWLOCK_GLOBAL(p2m_percpu_rwlock);
> 
>  /* Init the datastructures for later use by the p2m code */
> diff --git a/xen/arch/x86/mm/paging.c b/xen/arch/x86/mm/paging.c
> index 8a658b9118..2b0445ffe9 100644
> --- a/xen/arch/x86/mm/paging.c
> +++ b/xen/arch/x86/mm/paging.c
> @@ -47,12 +47,6 @@
>  /* Per-CPU variable for enforcing the lock ordering */
>  DEFINE_PER_CPU(int, mm_lock_level);
> 
> -/* Override macros from asm/page.h to make them work with mfn_t */
> -#undef mfn_to_page
> -#define mfn_to_page(_m) __mfn_to_page(mfn_x(_m))
> -#undef page_to_mfn
> -#define page_to_mfn(_pg) _mfn(__page_to_mfn(_pg))
> -
>  /************************************************/
>  /*              LOG DIRTY SUPPORT               */
>  /************************************************/
> diff --git a/xen/arch/x86/mm/shadow/private.h
> b/xen/arch/x86/mm/shadow/private.h
> index 845541fe8a..ea0ad28c05 100644
> --- a/xen/arch/x86/mm/shadow/private.h
> +++ b/xen/arch/x86/mm/shadow/private.h
> @@ -315,7 +315,7 @@ static inline int page_is_out_of_sync(struct page_info
> *p)
> 
>  static inline int mfn_is_out_of_sync(mfn_t gmfn)
>  {
> -    return page_is_out_of_sync(mfn_to_page(mfn_x(gmfn)));
> +    return page_is_out_of_sync(mfn_to_page(gmfn));
>  }
> 
>  static inline int page_oos_may_write(struct page_info *p)
> @@ -326,7 +326,7 @@ static inline int page_oos_may_write(struct page_info
> *p)
> 
>  static inline int mfn_oos_may_write(mfn_t gmfn)
>  {
> -    return page_oos_may_write(mfn_to_page(mfn_x(gmfn)));
> +    return page_oos_may_write(mfn_to_page(gmfn));
>  }
>  #endif /* (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC) */
> 
> @@ -465,18 +465,6 @@ void sh_reset_l3_up_pointers(struct vcpu *v);
>   * MFN/page-info handling
>   */
> 
> -/* Override macros from asm/page.h to make them work with mfn_t */
> -#undef mfn_to_page
> -#define mfn_to_page(_m) __mfn_to_page(mfn_x(_m))
> -#undef page_to_mfn
> -#define page_to_mfn(_pg) _mfn(__page_to_mfn(_pg))
> -
> -/* Override pagetable_t <-> struct page_info conversions to work with
> mfn_t */
> -#undef pagetable_get_page
> -#define pagetable_get_page(x)   mfn_to_page(pagetable_get_mfn(x))
> -#undef pagetable_from_page
> -#define pagetable_from_page(pg)
> pagetable_from_mfn(page_to_mfn(pg))
> -
>  #define backpointer(sp) _mfn(pdx_to_pfn((unsigned long)(sp)->v.sh.back))
>  static inline unsigned long __backpointer(const struct page_info *sp)
>  {
> diff --git a/xen/arch/x86/numa.c b/xen/arch/x86/numa.c
> index 4fc967f893..a87987da6f 100644
> --- a/xen/arch/x86/numa.c
> +++ b/xen/arch/x86/numa.c
> @@ -430,7 +430,7 @@ static void dump_numa(unsigned char key)
>          spin_lock(&d->page_alloc_lock);
>          page_list_for_each(page, &d->page_list)
>          {
> -            i = phys_to_nid((paddr_t)page_to_mfn(page) << PAGE_SHIFT);
> +            i = phys_to_nid(page_to_maddr(page));
>              page_num_node[i]++;
>          }
>          spin_unlock(&d->page_alloc_lock);
> diff --git a/xen/arch/x86/physdev.c b/xen/arch/x86/physdev.c
> index 380d36f6b9..7bfa0f23f0 100644
> --- a/xen/arch/x86/physdev.c
> +++ b/xen/arch/x86/physdev.c
> @@ -239,7 +239,7 @@ ret_t do_physdev_op(int cmd,
> XEN_GUEST_HANDLE_PARAM(void) arg)
>          }
> 
>          if ( cmpxchg(&currd->arch.pirq_eoi_map_mfn,
> -                     0, page_to_mfn(page)) != 0 )
> +                     0, mfn_x(page_to_mfn(page))) != 0 )
>          {
>              put_page_and_type(page);
>              ret = -EBUSY;
> diff --git a/xen/arch/x86/pv/callback.c b/xen/arch/x86/pv/callback.c
> index 97d8438600..5957cb5085 100644
> --- a/xen/arch/x86/pv/callback.c
> +++ b/xen/arch/x86/pv/callback.c
> @@ -31,12 +31,6 @@
> 
>  #include <public/callback.h>
> 
> -/* Override macros from asm/page.h to make them work with mfn_t */
> -#undef mfn_to_page
> -#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
> -#undef page_to_mfn
> -#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
> -
>  static int register_guest_nmi_callback(unsigned long address)
>  {
>      struct vcpu *curr = current;
> diff --git a/xen/arch/x86/pv/descriptor-tables.c
> b/xen/arch/x86/pv/descriptor-tables.c
> index b418bbb581..71bf92713e 100644
> --- a/xen/arch/x86/pv/descriptor-tables.c
> +++ b/xen/arch/x86/pv/descriptor-tables.c
> @@ -25,12 +25,6 @@
>  #include <asm/p2m.h>
>  #include <asm/pv/mm.h>
> 
> -/* Override macros from asm/page.h to make them work with mfn_t */
> -#undef mfn_to_page
> -#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
> -#undef page_to_mfn
> -#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
> -
>  /*
>   * Flush the LDT, dropping any typerefs.  Returns a boolean indicating
> whether
>   * mappings have been removed (i.e. a TLB flush is needed).
> diff --git a/xen/arch/x86/pv/dom0_build.c b/xen/arch/x86/pv/dom0_build.c
> index 0bd2f1bf90..5b4325b87f 100644
> --- a/xen/arch/x86/pv/dom0_build.c
> +++ b/xen/arch/x86/pv/dom0_build.c
> @@ -64,7 +64,7 @@ static __init void mark_pv_pt_pages_rdonly(struct
> domain *d,
>      for ( count = 0; count < nr_pt_pages; count++ )
>      {
>          l1e_remove_flags(*pl1e, _PAGE_RW);
> -        page = mfn_to_page(l1e_get_pfn(*pl1e));
> +        page = mfn_to_page(l1e_get_mfn(*pl1e));
> 
>          /* Read-only mapping + PGC_allocated + page-table page. */
>          page->count_info         = PGC_allocated | 3;
> @@ -496,7 +496,7 @@ int __init dom0_construct_pv(struct domain *d,
>      page = alloc_domheap_pages(d, order, 0);
>      if ( page == NULL )
>          panic("Not enough RAM for domain 0 allocation");
> -    alloc_spfn = page_to_mfn(page);
> +    alloc_spfn = mfn_x(page_to_mfn(page));
>      alloc_epfn = alloc_spfn + d->tot_pages;
> 
>      if ( initrd_len )
> @@ -524,12 +524,12 @@ int __init dom0_construct_pv(struct domain *d,
>              mpt_alloc = (paddr_t)initrd->mod_start << PAGE_SHIFT;
>              init_domheap_pages(mpt_alloc,
>                                 mpt_alloc + PAGE_ALIGN(initrd_len));
> -            initrd->mod_start = initrd_mfn = page_to_mfn(page);
> +            initrd->mod_start = initrd_mfn = mfn_x(page_to_mfn(page));
>          }
>          else
>          {
>              while ( count-- )
> -                if ( assign_pages(d, mfn_to_page(mfn++), 0, 0) )
> +                if ( assign_pages(d, mfn_to_page(_mfn(mfn++)), 0, 0) )
>                      BUG();
>          }
>          initrd->mod_end = 0;
> @@ -661,7 +661,7 @@ int __init dom0_construct_pv(struct domain *d,
>                                      L1_PROT : COMPAT_L1_PROT));
>          l1tab++;
> 
> -        page = mfn_to_page(mfn);
> +        page = mfn_to_page(_mfn(mfn));
>          if ( !page->u.inuse.type_info &&
>               !get_page_and_type(page, d, PGT_writable_page) )
>              BUG();
> @@ -801,7 +801,7 @@ int __init dom0_construct_pv(struct domain *d,
>      si->nr_p2m_frames = d->tot_pages - count;
>      page_list_for_each ( page, &d->page_list )
>      {
> -        mfn = page_to_mfn(page);
> +        mfn = mfn_x(page_to_mfn(page));
>          BUG_ON(SHARED_M2P(get_gpfn_from_mfn(mfn)));
>          if ( get_gpfn_from_mfn(mfn) >= count )
>          {
> @@ -826,7 +826,7 @@ int __init dom0_construct_pv(struct domain *d,
>              panic("Not enough RAM for DOM0 reservation");
>          while ( pfn < d->tot_pages )
>          {
> -            mfn = page_to_mfn(page);
> +            mfn = mfn_x(page_to_mfn(page));
>  #ifndef NDEBUG
>  #define pfn (nr_pages - 1 - (pfn - (alloc_epfn - alloc_spfn)))
>  #endif
> diff --git a/xen/arch/x86/pv/domain.c b/xen/arch/x86/pv/domain.c
> index 2c784fb3cc..5565e69f44 100644
> --- a/xen/arch/x86/pv/domain.c
> +++ b/xen/arch/x86/pv/domain.c
> @@ -11,12 +11,6 @@
> 
>  #include <asm/pv/domain.h>
> 
> -/* Override macros from asm/page.h to make them work with mfn_t */
> -#undef mfn_to_page
> -#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
> -#undef page_to_mfn
> -#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
> -
>  static void noreturn continue_nonidle_domain(struct vcpu *v)
>  {
>      check_wakeup_from_wait();
> diff --git a/xen/arch/x86/pv/emul-gate-op.c b/xen/arch/x86/pv/emul-gate-
> op.c
> index 14ce95e26e..810c4f7d8c 100644
> --- a/xen/arch/x86/pv/emul-gate-op.c
> +++ b/xen/arch/x86/pv/emul-gate-op.c
> @@ -41,12 +41,6 @@
> 
>  #include "emulate.h"
> 
> -/* Override macros from asm/page.h to make them work with mfn_t */
> -#undef mfn_to_page
> -#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
> -#undef page_to_mfn
> -#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
> -
>  static int read_gate_descriptor(unsigned int gate_sel,
>                                  const struct vcpu *v,
>                                  unsigned int *sel,
> diff --git a/xen/arch/x86/pv/emul-priv-op.c b/xen/arch/x86/pv/emul-priv-
> op.c
> index 17aaf97f10..b44b2415a6 100644
> --- a/xen/arch/x86/pv/emul-priv-op.c
> +++ b/xen/arch/x86/pv/emul-priv-op.c
> @@ -43,16 +43,6 @@
>  #include "emulate.h"
>  #include "mm.h"
> 
> -/* Override macros from asm/page.h to make them work with mfn_t */
> -#undef mfn_to_page
> -#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
> -#undef page_to_mfn
> -#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
> -
> -/***********************
> - * I/O emulation support
> - */
> -
>  struct priv_op_ctxt {
>      struct x86_emulate_ctxt ctxt;
>      struct {
> diff --git a/xen/arch/x86/pv/grant_table.c b/xen/arch/x86/pv/grant_table.c
> index 458085e1b6..6b7d855c8a 100644
> --- a/xen/arch/x86/pv/grant_table.c
> +++ b/xen/arch/x86/pv/grant_table.c
> @@ -27,12 +27,6 @@
> 
>  #include "mm.h"
> 
> -/* Override macros from asm/page.h to make them work with mfn_t */
> -#undef mfn_to_page
> -#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
> -#undef page_to_mfn
> -#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
> -
>  static unsigned int grant_to_pte_flags(unsigned int grant_flags,
>                                         unsigned int cache_flags)
>  {
> diff --git a/xen/arch/x86/pv/ro-page-fault.c b/xen/arch/x86/pv/ro-page-
> fault.c
> index 7e0e7e8dfc..17a08345d1 100644
> --- a/xen/arch/x86/pv/ro-page-fault.c
> +++ b/xen/arch/x86/pv/ro-page-fault.c
> @@ -33,12 +33,6 @@
>  #include "emulate.h"
>  #include "mm.h"
> 
> -/* Override macros from asm/page.h to make them work with mfn_t */
> -#undef mfn_to_page
> -#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
> -#undef page_to_mfn
> -#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
> -
>  /*********************
>   * Writable Pagetables
>   */
> diff --git a/xen/arch/x86/pv/shim.c b/xen/arch/x86/pv/shim.c
> index 534965c92a..519278f78c 100644
> --- a/xen/arch/x86/pv/shim.c
> +++ b/xen/arch/x86/pv/shim.c
> @@ -37,8 +37,6 @@
> 
>  #include <compat/grant_table.h>
> 
> -#undef mfn_to_page
> -#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
>  #undef virt_to_mfn
>  #define virt_to_mfn(va) _mfn(__virt_to_mfn(va))
> 
> @@ -848,7 +846,7 @@ static unsigned long batch_memory_op(unsigned int
> cmd, unsigned int order,
>      set_xen_guest_handle(xmr.extent_start, pfns);
>      page_list_for_each ( pg, list )
>      {
> -        pfns[xmr.nr_extents++] = page_to_mfn(pg);
> +        pfns[xmr.nr_extents++] = mfn_x(page_to_mfn(pg));
>          if ( xmr.nr_extents == ARRAY_SIZE(pfns) || !page_list_next(pg, list) )
>          {
>              long nr = xen_hypercall_memory_op(cmd, &xmr);
> diff --git a/xen/arch/x86/smpboot.c b/xen/arch/x86/smpboot.c
> index 52ad43e0ed..48c8cdb734 100644
> --- a/xen/arch/x86/smpboot.c
> +++ b/xen/arch/x86/smpboot.c
> @@ -48,12 +48,6 @@
>  #include <mach_wakecpu.h>
>  #include <smpboot_hooks.h>
> 
> -/* Override macros from asm/page.h to make them work with mfn_t */
> -#undef mfn_to_page
> -#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
> -#undef page_to_mfn
> -#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
> -
>  #define setup_trampoline()
> (bootsym_phys(trampoline_realmode_entry))
> 
>  unsigned long __read_mostly trampoline_phys;
> diff --git a/xen/arch/x86/tboot.c b/xen/arch/x86/tboot.c
> index 71e757c553..fb4616ae83 100644
> --- a/xen/arch/x86/tboot.c
> +++ b/xen/arch/x86/tboot.c
> @@ -184,7 +184,7 @@ static void update_pagetable_mac(vmac_ctx_t *ctx)
> 
>      for ( mfn = 0; mfn < max_page; mfn++ )
>      {
> -        struct page_info *page = mfn_to_page(mfn);
> +        struct page_info *page = mfn_to_page(_mfn(mfn));
> 
>          if ( !mfn_valid(_mfn(mfn)) )
>              continue;
> @@ -276,7 +276,7 @@ static void tboot_gen_xenheap_integrity(const
> uint8_t key[TB_KEY_SIZE],
>      vmac_set_key((uint8_t *)key, &ctx);
>      for ( mfn = 0; mfn < max_page; mfn++ )
>      {
> -        struct page_info *page = __mfn_to_page(mfn);
> +        struct page_info *page = mfn_to_page(_mfn(mfn));
> 
>          if ( !mfn_valid(_mfn(mfn)) )
>              continue;
> diff --git a/xen/arch/x86/traps.c b/xen/arch/x86/traps.c
> index 27190e0423..571796b123 100644
> --- a/xen/arch/x86/traps.c
> +++ b/xen/arch/x86/traps.c
> @@ -834,8 +834,8 @@ int wrmsr_hypervisor_regs(uint32_t idx, uint64_t val)
>              }
> 
>              gdprintk(XENLOG_WARNING,
> -                     "Bad GMFN %lx (MFN %lx) to MSR %08x\n",
> -                     gmfn, page ? page_to_mfn(page) : -1UL, base);
> +                     "Bad GMFN %lx (MFN %#"PRI_mfn") to MSR %08x\n",
> +                     gmfn, mfn_x(page ? page_to_mfn(page) : INVALID_MFN),
> base);
>              return 0;
>          }
> 
> diff --git a/xen/arch/x86/x86_64/mm.c b/xen/arch/x86/x86_64/mm.c
> index 6b679882d6..ff8a6de23f 100644
> --- a/xen/arch/x86/x86_64/mm.c
> +++ b/xen/arch/x86/x86_64/mm.c
> @@ -40,12 +40,6 @@ asm(".file \"" __FILE__ "\"");
>  #include <asm/mem_sharing.h>
>  #include <public/memory.h>
> 
> -/* Override macros from asm/page.h to make them work with mfn_t */
> -#undef page_to_mfn
> -#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
> -#undef mfn_to_page
> -#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
> -
>  unsigned int __read_mostly m2p_compat_vstart =
> __HYPERVISOR_COMPAT_VIRT_START;
> 
>  l2_pgentry_t *compat_idle_pg_table_l2;
> diff --git a/xen/common/domain.c b/xen/common/domain.c
> index e1c003d71e..e2acfeff80 100644
> --- a/xen/common/domain.c
> +++ b/xen/common/domain.c
> @@ -1217,7 +1217,7 @@ int map_vcpu_info(struct vcpu *v, unsigned long
> gfn, unsigned offset)
>      }
> 
>      v->vcpu_info = new_info;
> -    v->vcpu_info_mfn = _mfn(page_to_mfn(page));
> +    v->vcpu_info_mfn = page_to_mfn(page);
> 
>      /* Set new vcpu_info pointer /before/ setting pending flags. */
>      smp_wmb();
> @@ -1250,7 +1250,7 @@ void unmap_vcpu_info(struct vcpu *v)
> 
>      vcpu_info_reset(v); /* NB: Clobbers v->vcpu_info_mfn */
> 
> -    put_page_and_type(mfn_to_page(mfn_x(mfn)));
> +    put_page_and_type(mfn_to_page(mfn));
>  }
> 
>  int default_initialise_vcpu(struct vcpu *v,
> XEN_GUEST_HANDLE_PARAM(void) arg)
> diff --git a/xen/common/grant_table.c b/xen/common/grant_table.c
> index e9a81b66be..959b7c64b2 100644
> --- a/xen/common/grant_table.c
> +++ b/xen/common/grant_table.c
> @@ -40,12 +40,6 @@
>  #include <xsm/xsm.h>
>  #include <asm/flushtlb.h>
> 
> -/* Override macros from asm/page.h to make them work with mfn_t */
> -#undef page_to_mfn
> -#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
> -#undef mfn_to_page
> -#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
> -
>  /* Per-domain grant information. */
>  struct grant_table {
>      /*
> diff --git a/xen/common/kimage.c b/xen/common/kimage.c
> index afd8292cc1..210241dfb7 100644
> --- a/xen/common/kimage.c
> +++ b/xen/common/kimage.c
> @@ -23,12 +23,6 @@
> 
>  #include <asm/page.h>
> 
> -/* Override macros from asm/page.h to make them work with mfn_t */
> -#undef mfn_to_page
> -#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
> -#undef page_to_mfn
> -#define page_to_mfn(pg)  _mfn(__page_to_mfn(pg))
> -
>  /*
>   * When kexec transitions to the new kernel there is a one-to-one
>   * mapping between physical and virtual addresses.  On processors
> diff --git a/xen/common/memory.c b/xen/common/memory.c
> index 93d856df02..4c6b36b297 100644
> --- a/xen/common/memory.c
> +++ b/xen/common/memory.c
> @@ -33,12 +33,6 @@
>  #include <asm/guest.h>
>  #endif
> 
> -/* Override macros from asm/page.h to make them work with mfn_t */
> -#undef page_to_mfn
> -#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
> -#undef mfn_to_page
> -#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
> -
>  struct memop_args {
>      /* INPUT */
>      struct domain *domain;     /* Domain to be affected. */
> diff --git a/xen/common/page_alloc.c b/xen/common/page_alloc.c
> index b0db41feea..7fa847c9a1 100644
> --- a/xen/common/page_alloc.c
> +++ b/xen/common/page_alloc.c
> @@ -151,12 +151,6 @@
>  #define p2m_pod_offline_or_broken_replace(pg) BUG_ON(pg != NULL)
>  #endif
> 
> -/* Override macros from asm/page.h to make them work with mfn_t */
> -#undef page_to_mfn
> -#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
> -#undef mfn_to_page
> -#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
> -
>  /*
>   * Comma-separated list of hexadecimal page numbers containing bad
> bytes.
>   * e.g. 'badpage=0x3f45,0x8a321'.
> diff --git a/xen/common/tmem.c b/xen/common/tmem.c
> index 324f42a6f9..c077f87e77 100644
> --- a/xen/common/tmem.c
> +++ b/xen/common/tmem.c
> @@ -243,7 +243,7 @@ static void tmem_persistent_pool_page_put(void
> *page_va)
>      struct page_info *pi;
> 
>      ASSERT(IS_PAGE_ALIGNED(page_va));
> -    pi = mfn_to_page(virt_to_mfn(page_va));
> +    pi = mfn_to_page(_mfn(virt_to_mfn(page_va)));
>      ASSERT(IS_VALID_PAGE(pi));
>      __tmem_free_page_thispool(pi);
>  }
> diff --git a/xen/common/tmem_xen.c b/xen/common/tmem_xen.c
> index bd52e44faf..bf7b14f79a 100644
> --- a/xen/common/tmem_xen.c
> +++ b/xen/common/tmem_xen.c
> @@ -14,10 +14,6 @@
>  #include <xen/cpu.h>
>  #include <xen/init.h>
> 
> -/* Override macros from asm/page.h to make them work with mfn_t */
> -#undef page_to_mfn
> -#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
> -
>  bool __read_mostly opt_tmem;
>  boolean_param("tmem", opt_tmem);
> 
> diff --git a/xen/common/trace.c b/xen/common/trace.c
> index 2e18702317..1f19b7a604 100644
> --- a/xen/common/trace.c
> +++ b/xen/common/trace.c
> @@ -243,7 +243,7 @@ static int alloc_trace_bufs(unsigned int pages)
>          /* Now share the trace pages */
>          for ( i = 0; i < pages; i++ )
>          {
> -            pg = mfn_to_page(t_info_mfn_list[offset + i]);
> +            pg = mfn_to_page(_mfn(t_info_mfn_list[offset + i]));
>              share_xen_page_with_privileged_guests(pg, XENSHARE_writable);
>          }
>      }
> @@ -274,7 +274,7 @@ out_dealloc:
>              uint32_t mfn = t_info_mfn_list[offset + i];
>              if ( !mfn )
>                  break;
> -            ASSERT(!(mfn_to_page(mfn)->count_info & PGC_allocated));
> +            ASSERT(!(mfn_to_page(_mfn(mfn))->count_info & PGC_allocated));
>              free_xenheap_pages(mfn_to_virt(mfn), 0);
>          }
>      }
> diff --git a/xen/common/vmap.c b/xen/common/vmap.c
> index 1f50c91789..6e295862fb 100644
> --- a/xen/common/vmap.c
> +++ b/xen/common/vmap.c
> @@ -9,10 +9,6 @@
>  #include <xen/vmap.h>
>  #include <asm/page.h>
> 
> -/* Override macros from asm/page.h to make them work with mfn_t */
> -#undef page_to_mfn
> -#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
> -
>  static DEFINE_SPINLOCK(vm_lock);
>  static void *__read_mostly vm_base[VMAP_REGION_NR];
>  #define vm_bitmap(x) ((unsigned long *)vm_base[x])
> @@ -274,7 +270,7 @@ static void *vmalloc_type(size_t size, enum
> vmap_region type)
> 
>   error:
>      while ( i-- )
> -        free_domheap_page(mfn_to_page(mfn_x(mfn[i])));
> +        free_domheap_page(mfn_to_page(mfn[i]));
>      xfree(mfn);
>      return NULL;
>  }
> diff --git a/xen/common/xenoprof.c b/xen/common/xenoprof.c
> index 5acdde5691..fecdfb3697 100644
> --- a/xen/common/xenoprof.c
> +++ b/xen/common/xenoprof.c
> @@ -22,8 +22,6 @@
>  /* Override macros from asm/page.h to make them work with mfn_t */
>  #undef virt_to_mfn
>  #define virt_to_mfn(va) _mfn(__virt_to_mfn(va))
> -#undef mfn_to_page
> -#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
> 
>  /* Limit amount of pages used for shared buffer (per domain) */
>  #define MAX_OPROF_SHARED_PAGES 32
> diff --git a/xen/drivers/passthrough/amd/iommu_map.c
> b/xen/drivers/passthrough/amd/iommu_map.c
> index fd2327d3e5..70b4345b37 100644
> --- a/xen/drivers/passthrough/amd/iommu_map.c
> +++ b/xen/drivers/passthrough/amd/iommu_map.c
> @@ -451,7 +451,7 @@ static int iommu_pde_from_gfn(struct domain *d,
> unsigned long pfn,
>      BUG_ON( table == NULL || level < IOMMU_PAGING_MODE_LEVEL_1 ||
>              level > IOMMU_PAGING_MODE_LEVEL_6 );
> 
> -    next_table_mfn = page_to_mfn(table);
> +    next_table_mfn = mfn_x(page_to_mfn(table));
> 
>      if ( level == IOMMU_PAGING_MODE_LEVEL_1 )
>      {
> @@ -493,7 +493,7 @@ static int iommu_pde_from_gfn(struct domain *d,
> unsigned long pfn,
>                  return 1;
>              }
> 
> -            next_table_mfn = page_to_mfn(table);
> +            next_table_mfn = mfn_x(page_to_mfn(table));
>              set_iommu_pde_present((u32*)pde, next_table_mfn, next_level,
>                                    !!IOMMUF_writable, !!IOMMUF_readable);
> 
> @@ -520,7 +520,7 @@ static int iommu_pde_from_gfn(struct domain *d,
> unsigned long pfn,
>                      unmap_domain_page(next_table_vaddr);
>                      return 1;
>                  }
> -                next_table_mfn = page_to_mfn(table);
> +                next_table_mfn = mfn_x(page_to_mfn(table));
>                  set_iommu_pde_present((u32*)pde, next_table_mfn, next_level,
>                                        !!IOMMUF_writable, !!IOMMUF_readable);
>              }
> @@ -577,7 +577,7 @@ static int update_paging_mode(struct domain *d,
> unsigned long gfn)
>          }
> 
>          new_root_vaddr = __map_domain_page(new_root);
> -        old_root_mfn = page_to_mfn(old_root);
> +        old_root_mfn = mfn_x(page_to_mfn(old_root));
>          set_iommu_pde_present(new_root_vaddr, old_root_mfn, level,
>                                !!IOMMUF_writable, !!IOMMUF_readable);
>          level++;
> @@ -712,7 +712,7 @@ int amd_iommu_map_page(struct domain *d,
> unsigned long gfn, unsigned long mfn,
>          }
> 
>          /* Deallocate lower level page table */
> -        free_amd_iommu_pgtable(mfn_to_page(pt_mfn[merge_level - 1]));
> +        free_amd_iommu_pgtable(mfn_to_page(_mfn(pt_mfn[merge_level -
> 1])));
>      }
> 
>  out:
> @@ -802,7 +802,7 @@ void amd_iommu_share_p2m(struct domain *d)
>      mfn_t pgd_mfn;
> 
>      pgd_mfn =
> pagetable_get_mfn(p2m_get_pagetable(p2m_get_hostp2m(d)));
> -    p2m_table = mfn_to_page(mfn_x(pgd_mfn));
> +    p2m_table = mfn_to_page(pgd_mfn);
> 
>      if ( hd->arch.root_table != p2m_table )
>      {
> diff --git a/xen/drivers/passthrough/iommu.c
> b/xen/drivers/passthrough/iommu.c
> index 1aecf7cf34..2c44fabf99 100644
> --- a/xen/drivers/passthrough/iommu.c
> +++ b/xen/drivers/passthrough/iommu.c
> @@ -184,7 +184,7 @@ void __hwdom_init iommu_hwdom_init(struct
> domain *d)
> 
>          page_list_for_each ( page, &d->page_list )
>          {
> -            unsigned long mfn = page_to_mfn(page);
> +            unsigned long mfn = mfn_x(page_to_mfn(page));
>              unsigned long gfn = mfn_to_gmfn(d, mfn);
>              unsigned int mapping = IOMMUF_readable;
>              int ret;
> diff --git a/xen/drivers/passthrough/x86/iommu.c
> b/xen/drivers/passthrough/x86/iommu.c
> index 0253823173..68182afd91 100644
> --- a/xen/drivers/passthrough/x86/iommu.c
> +++ b/xen/drivers/passthrough/x86/iommu.c
> @@ -58,7 +58,7 @@ int arch_iommu_populate_page_table(struct domain
> *d)
>          if ( is_hvm_domain(d) ||
>              (page->u.inuse.type_info & PGT_type_mask) == PGT_writable_page )
>          {
> -            unsigned long mfn = page_to_mfn(page);
> +            unsigned long mfn = mfn_x(page_to_mfn(page));
>              unsigned long gfn = mfn_to_gmfn(d, mfn);
> 
>              if ( gfn != gfn_x(INVALID_GFN) )
> diff --git a/xen/include/asm-arm/mm.h b/xen/include/asm-arm/mm.h
> index 023e2eb213..b1d94805d4 100644
> --- a/xen/include/asm-arm/mm.h
> +++ b/xen/include/asm-arm/mm.h
> @@ -138,7 +138,7 @@ extern vaddr_t xenheap_virt_start;
>  #endif
> 
>  #ifdef CONFIG_ARM_32
> -#define is_xen_heap_page(page)
> is_xen_heap_mfn(__page_to_mfn(page))
> +#define is_xen_heap_page(page)
> is_xen_heap_mfn(mfn_x(page_to_mfn(page)))
>  #define is_xen_heap_mfn(mfn) ({                                 \
>      unsigned long mfn_ = (mfn);                                 \
>      (mfn_ >= mfn_x(xenheap_mfn_start) &&                        \
> @@ -147,7 +147,7 @@ extern vaddr_t xenheap_virt_start;
>  #else
>  #define is_xen_heap_page(page) ((page)->count_info & PGC_xen_heap)
>  #define is_xen_heap_mfn(mfn) \
> -    (mfn_valid(_mfn(mfn)) && is_xen_heap_page(__mfn_to_page(mfn)))
> +    (mfn_valid(_mfn(mfn)) &&
> is_xen_heap_page(mfn_to_page(_mfn(mfn))))
>  #endif
> 
>  #define is_xen_fixed_mfn(mfn)                                   \
> @@ -220,12 +220,14 @@ static inline void __iomem *ioremap_wc(paddr_t
> start, size_t len)
>  })
> 
>  /* Convert between machine frame numbers and page-info structures. */
> -#define __mfn_to_page(mfn)  (frame_table + (pfn_to_pdx(mfn) -
> frametable_base_pdx))
> -#define __page_to_mfn(pg)   pdx_to_pfn((unsigned long)((pg) -
> frame_table) + frametable_base_pdx)
> +#define mfn_to_page(mfn)                                            \
> +    (frame_table + (mfn_to_pdx(mfn) - frametable_base_pdx))
> +#define page_to_mfn(pg)                                             \
> +    pdx_to_mfn((unsigned long)((pg) - frame_table) +
> frametable_base_pdx)
> 
>  /* Convert between machine addresses and page-info structures. */
> -#define maddr_to_page(ma) __mfn_to_page((ma) >> PAGE_SHIFT)
> -#define page_to_maddr(pg) ((paddr_t)__page_to_mfn(pg) <<
> PAGE_SHIFT)
> +#define maddr_to_page(ma) mfn_to_page(maddr_to_mfn(ma))
> +#define page_to_maddr(pg) (mfn_to_maddr(page_to_mfn(pg)))
> 
>  /* Convert between frame number and address formats.  */
>  #define pfn_to_paddr(pfn) ((paddr_t)(pfn) << PAGE_SHIFT)
> @@ -235,7 +237,7 @@ static inline void __iomem *ioremap_wc(paddr_t
> start, size_t len)
>  #define gaddr_to_gfn(ga)    _gfn(paddr_to_pfn(ga))
>  #define mfn_to_maddr(mfn)   pfn_to_paddr(mfn_x(mfn))
>  #define maddr_to_mfn(ma)    _mfn(paddr_to_pfn(ma))
> -#define vmap_to_mfn(va)     paddr_to_pfn(virt_to_maddr((vaddr_t)va))
> +#define vmap_to_mfn(va)     maddr_to_mfn(virt_to_maddr((vaddr_t)va))
>  #define vmap_to_page(va)    mfn_to_page(vmap_to_mfn(va))
> 
>  /* Page-align address and convert to frame number format */
> @@ -293,8 +295,6 @@ static inline uint64_t gvirt_to_maddr(vaddr_t va,
> paddr_t *pa,
>   * These are overriden in various source files while underscored version
>   * remain intact.
>   */
> -#define mfn_to_page(mfn)    __mfn_to_page(mfn)
> -#define page_to_mfn(pg)     __page_to_mfn(pg)
>  #define virt_to_mfn(va)     __virt_to_mfn(va)
>  #define mfn_to_virt(mfn)    __mfn_to_virt(mfn)
> 
> @@ -314,7 +314,7 @@ static inline struct page_info *virt_to_page(const void
> *v)
> 
>  static inline void *page_to_virt(const struct page_info *pg)
>  {
> -    return mfn_to_virt(page_to_mfn(pg));
> +    return mfn_to_virt(mfn_x(page_to_mfn(pg)));
>  }
> 
>  struct page_info *get_page_from_gva(struct vcpu *v, vaddr_t va,
> diff --git a/xen/include/asm-arm/p2m.h b/xen/include/asm-arm/p2m.h
> index a0abc84ed8..bcac141fd4 100644
> --- a/xen/include/asm-arm/p2m.h
> +++ b/xen/include/asm-arm/p2m.h
> @@ -278,7 +278,7 @@ static inline struct page_info *get_page_from_gfn(
>  {
>      struct page_info *page;
>      p2m_type_t p2mt;
> -    unsigned long mfn = mfn_x(p2m_lookup(d, _gfn(gfn), &p2mt));
> +    mfn_t mfn = p2m_lookup(d, _gfn(gfn), &p2mt);
> 
>      if (t)
>          *t = p2mt;
> @@ -286,7 +286,7 @@ static inline struct page_info *get_page_from_gfn(
>      if ( !p2m_is_any_ram(p2mt) )
>          return NULL;
> 
> -    if ( !mfn_valid(_mfn(mfn)) )
> +    if ( !mfn_valid(mfn) )
>          return NULL;
>      page = mfn_to_page(mfn);
> 
> diff --git a/xen/include/asm-x86/mm.h b/xen/include/asm-x86/mm.h
> index 3013c266fe..8dc3821e97 100644
> --- a/xen/include/asm-x86/mm.h
> +++ b/xen/include/asm-x86/mm.h
> @@ -271,7 +271,7 @@ struct page_info
> 
>  #define is_xen_heap_page(page) ((page)->count_info & PGC_xen_heap)
>  #define is_xen_heap_mfn(mfn) \
> -    (__mfn_valid(mfn) && is_xen_heap_page(__mfn_to_page(mfn)))
> +    (__mfn_valid(mfn) && is_xen_heap_page(mfn_to_page(_mfn(mfn))))
>  #define is_xen_fixed_mfn(mfn)                     \
>      ((((mfn) << PAGE_SHIFT) >= __pa(&_stext)) &&  \
>       (((mfn) << PAGE_SHIFT) <= __pa(&__2M_rwdata_end)))
> @@ -384,7 +384,7 @@ void put_page_from_l1e(l1_pgentry_t l1e, struct
> domain *l1e_owner);
> 
>  static inline struct page_info *get_page_from_mfn(mfn_t mfn, struct
> domain *d)
>  {
> -    struct page_info *page = __mfn_to_page(mfn_x(mfn));
> +    struct page_info *page = mfn_to_page(mfn);
> 
>      if ( unlikely(!mfn_valid(mfn)) || unlikely(!get_page(page, d)) )
>      {
> @@ -478,10 +478,10 @@ extern paddr_t mem_hotplug;
>  #define SHARED_M2P(_e)           ((_e) == SHARED_M2P_ENTRY)
> 
>  #define compat_machine_to_phys_mapping ((unsigned int
> *)RDWR_COMPAT_MPT_VIRT_START)
> -#define _set_gpfn_from_mfn(mfn, pfn) ({                        \
> -    struct domain *d = page_get_owner(__mfn_to_page(mfn));     \
> -    unsigned long entry = (d && (d == dom_cow)) ?              \
> -        SHARED_M2P_ENTRY : (pfn);                              \
> +#define _set_gpfn_from_mfn(mfn, pfn) ({                         \
> +    struct domain *d = page_get_owner(mfn_to_page(_mfn(mfn)));    \
> +    unsigned long entry = (d && (d == dom_cow)) ?               \
> +        SHARED_M2P_ENTRY : (pfn);                               \
>      ((void)((mfn) >= (RDWR_COMPAT_MPT_VIRT_END -
> RDWR_COMPAT_MPT_VIRT_START) / 4 || \
>              (compat_machine_to_phys_mapping[(mfn)] = (unsigned int)(entry))),
> \
>       machine_to_phys_mapping[(mfn)] = (entry));                \
> diff --git a/xen/include/asm-x86/p2m.h b/xen/include/asm-x86/p2m.h
> index 2e7aa8fc79..c486b6f8f0 100644
> --- a/xen/include/asm-x86/p2m.h
> +++ b/xen/include/asm-x86/p2m.h
> @@ -488,7 +488,7 @@ static inline struct page_info *get_page_from_gfn(
>      /* Non-translated guests see 1-1 RAM / MMIO mappings everywhere */
>      if ( t )
>          *t = likely(d != dom_io) ? p2m_ram_rw : p2m_mmio_direct;
> -    page = __mfn_to_page(gfn);
> +    page = mfn_to_page(_mfn(gfn));
>      return mfn_valid(_mfn(gfn)) && get_page(page, d) ? page : NULL;
>  }
> 
> diff --git a/xen/include/asm-x86/page.h b/xen/include/asm-x86/page.h
> index 45ca742678..7e2a1546c3 100644
> --- a/xen/include/asm-x86/page.h
> +++ b/xen/include/asm-x86/page.h
> @@ -88,10 +88,10 @@
>      ((paddr_t)(((x).l4 & (PADDR_MASK&PAGE_MASK))))
> 
>  /* Get pointer to info structure of page mapped by pte (struct page_info *).
> */
> -#define l1e_get_page(x)           (__mfn_to_page(l1e_get_pfn(x)))
> -#define l2e_get_page(x)           (__mfn_to_page(l2e_get_pfn(x)))
> -#define l3e_get_page(x)           (__mfn_to_page(l3e_get_pfn(x)))
> -#define l4e_get_page(x)           (__mfn_to_page(l4e_get_pfn(x)))
> +#define l1e_get_page(x)           mfn_to_page(l1e_get_mfn(x))
> +#define l2e_get_page(x)           mfn_to_page(l2e_get_mfn(x))
> +#define l3e_get_page(x)           mfn_to_page(l3e_get_mfn(x))
> +#define l4e_get_page(x)           mfn_to_page(l4e_get_mfn(x))
> 
>  /* Get pte access flags (unsigned int). */
>  #define l1e_get_flags(x)           (get_pte_flags((x).l1))
> @@ -157,10 +157,10 @@ static inline l4_pgentry_t l4e_from_paddr(paddr_t
> pa, unsigned int flags)
>  #define l4e_from_intpte(intpte)    ((l4_pgentry_t) { (intpte_t)(intpte) })
> 
>  /* Construct a pte from a page pointer and access flags. */
> -#define l1e_from_page(page, flags) l1e_from_pfn(__page_to_mfn(page),
> (flags))
> -#define l2e_from_page(page, flags) l2e_from_pfn(__page_to_mfn(page),
> (flags))
> -#define l3e_from_page(page, flags) l3e_from_pfn(__page_to_mfn(page),
> (flags))
> -#define l4e_from_page(page, flags) l4e_from_pfn(__page_to_mfn(page),
> (flags))
> +#define l1e_from_page(page, flags) l1e_from_mfn(page_to_mfn(page),
> (flags))
> +#define l2e_from_page(page, flags) l2e_from_mfn(page_to_mfn(page),
> (flags))
> +#define l3e_from_page(page, flags) l3e_from_mfn(page_to_mfn(page),
> (flags))
> +#define l4e_from_page(page, flags) l4e_from_mfn(page_to_mfn(page),
> (flags))
> 
>  /* Add extra flags to an existing pte. */
>  #define l1e_add_flags(x, flags)    ((x).l1 |= put_pte_flags(flags))
> @@ -215,13 +215,13 @@ static inline l4_pgentry_t l4e_from_paddr(paddr_t
> pa, unsigned int flags)
>  /* Page-table type. */
>  typedef struct { u64 pfn; } pagetable_t;
>  #define pagetable_get_paddr(x)  ((paddr_t)(x).pfn << PAGE_SHIFT)
> -#define pagetable_get_page(x)   __mfn_to_page((x).pfn)
> +#define pagetable_get_page(x)   mfn_to_page(pagetable_get_mfn(x))
>  #define pagetable_get_pfn(x)    ((x).pfn)
>  #define pagetable_get_mfn(x)    _mfn(((x).pfn))
>  #define pagetable_is_null(x)    ((x).pfn == 0)
>  #define pagetable_from_pfn(pfn) ((pagetable_t) { (pfn) })
>  #define pagetable_from_mfn(mfn) ((pagetable_t) { mfn_x(mfn) })
> -#define pagetable_from_page(pg)
> pagetable_from_pfn(__page_to_mfn(pg))
> +#define pagetable_from_page(pg)
> pagetable_from_mfn(page_to_mfn(pg))
>  #define pagetable_from_paddr(p) pagetable_from_pfn((p)>>PAGE_SHIFT)
>  #define pagetable_null()        pagetable_from_pfn(0)
> 
> @@ -240,12 +240,12 @@ void copy_page_sse2(void *, const void *);
>  #define __mfn_to_virt(mfn)  (maddr_to_virt((paddr_t)(mfn) <<
> PAGE_SHIFT))
> 
>  /* Convert between machine frame numbers and page-info structures. */
> -#define __mfn_to_page(mfn)  (frame_table + pfn_to_pdx(mfn))
> -#define __page_to_mfn(pg)   pdx_to_pfn((unsigned long)((pg) -
> frame_table))
> +#define mfn_to_page(mfn)    (frame_table + mfn_to_pdx(mfn))
> +#define page_to_mfn(pg)     pdx_to_mfn((unsigned long)((pg) -
> frame_table))
> 
>  /* Convert between machine addresses and page-info structures. */
> -#define __maddr_to_page(ma) __mfn_to_page((ma) >> PAGE_SHIFT)
> -#define __page_to_maddr(pg) ((paddr_t)__page_to_mfn(pg) <<
> PAGE_SHIFT)
> +#define __maddr_to_page(ma) mfn_to_page(maddr_to_mfn(ma))
> +#define __page_to_maddr(pg) (mfn_to_maddr(page_to_mfn(pg)))
> 
>  /* Convert between frame number and address formats.  */
>  #define __pfn_to_paddr(pfn) ((paddr_t)(pfn) << PAGE_SHIFT)
> @@ -264,8 +264,6 @@ void copy_page_sse2(void *, const void *);
>  #define mfn_to_virt(mfn)    __mfn_to_virt(mfn)
>  #define virt_to_maddr(va)   __virt_to_maddr((unsigned long)(va))
>  #define maddr_to_virt(ma)   __maddr_to_virt((unsigned long)(ma))
> -#define mfn_to_page(mfn)    __mfn_to_page(mfn)
> -#define page_to_mfn(pg)     __page_to_mfn(pg)
>  #define maddr_to_page(ma)   __maddr_to_page(ma)
>  #define page_to_maddr(pg)   __page_to_maddr(pg)
>  #define virt_to_page(va)    __virt_to_page(va)
> @@ -273,7 +271,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_pfn(*virt_to_xen_l1e((unsigned
> long)(va)))
> +#define vmap_to_mfn(va)
> _mfn(l1e_get_pfn(*virt_to_xen_l1e((unsigned long)(va))))
>  #define vmap_to_page(va)    mfn_to_page(vmap_to_mfn(va))
> 
>  #endif /* !defined(__ASSEMBLY__) */
> diff --git a/xen/include/xen/domain_page.h
> b/xen/include/xen/domain_page.h
> index 890bae5b9c..32669a3339 100644
> --- a/xen/include/xen/domain_page.h
> +++ b/xen/include/xen/domain_page.h
> @@ -34,7 +34,7 @@ void unmap_domain_page(const void *va);
>  /*
>   * Given a VA from map_domain_page(), return its underlying MFN.
>   */
> -unsigned long domain_page_map_to_mfn(const void *va);
> +mfn_t domain_page_map_to_mfn(const void *va);
> 
>  /*
>   * Similar to the above calls, except the mapping is accessible in all
> @@ -44,11 +44,11 @@ unsigned long domain_page_map_to_mfn(const void
> *va);
>  void *map_domain_page_global(mfn_t mfn);
>  void unmap_domain_page_global(const void *va);
> 
> -#define __map_domain_page(pg)
> map_domain_page(_mfn(__page_to_mfn(pg)))
> +#define __map_domain_page(pg)
> map_domain_page(page_to_mfn(pg))
> 
>  static inline void *__map_domain_page_global(const struct page_info *pg)
>  {
> -    return map_domain_page_global(_mfn(__page_to_mfn(pg)));
> +    return map_domain_page_global(page_to_mfn(pg));
>  }
> 
>  #else /* !CONFIG_DOMAIN_PAGE */
> @@ -56,7 +56,7 @@ static inline void *__map_domain_page_global(const
> struct page_info *pg)
>  #define map_domain_page(mfn)                __mfn_to_virt(mfn_x(mfn))
>  #define __map_domain_page(pg)               page_to_virt(pg)
>  #define unmap_domain_page(va)               ((void)(va))
> -#define domain_page_map_to_mfn(va)          virt_to_mfn((unsigned
> long)(va))
> +#define domain_page_map_to_mfn(va)          _mfn(virt_to_mfn((unsigned
> long)(va)))
> 
>  static inline void *map_domain_page_global(mfn_t mfn)
>  {
> diff --git a/xen/include/xen/mm.h b/xen/include/xen/mm.h
> index caad06e753..204dd9c48d 100644
> --- a/xen/include/xen/mm.h
> +++ b/xen/include/xen/mm.h
> @@ -277,13 +277,8 @@ struct page_list_head
>  # define PAGE_LIST_NULL ((typeof(((struct page_info){}).list.next))~0)
> 
>  # if !defined(pdx_to_page) && !defined(page_to_pdx)
> -#  if defined(__page_to_mfn) || defined(__mfn_to_page)
> -#   define page_to_pdx __page_to_mfn
> -#   define pdx_to_page __mfn_to_page
> -#  else
>  #   define page_to_pdx page_to_mfn
>  #   define pdx_to_page mfn_to_page
> -#  endif
>  # endif
> 
>  # define PAGE_LIST_HEAD_INIT(name) { NULL, NULL }
> diff --git a/xen/include/xen/tmem_xen.h b/xen/include/xen/tmem_xen.h
> index 542c0b3f20..8516a0b131 100644
> --- a/xen/include/xen/tmem_xen.h
> +++ b/xen/include/xen/tmem_xen.h
> @@ -25,7 +25,7 @@
>  typedef uint32_t pagesize_t;  /* like size_t, must handle largest PAGE_SIZE
> */
> 
>  #define IS_PAGE_ALIGNED(addr) IS_ALIGNED((unsigned long)(addr),
> PAGE_SIZE)
> -#define IS_VALID_PAGE(_pi)    mfn_valid(_mfn(page_to_mfn(_pi)))
> +#define IS_VALID_PAGE(_pi)    mfn_valid(page_to_mfn(_pi))
> 
>  extern struct page_list_head tmem_page_list;
>  extern spinlock_t tmem_page_list_lock;
> --
> 2.11.0


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 16/16] xen: Convert page_to_mfn and mfn_to_page to use typesafe MFN
  2018-02-21 14:02 ` [PATCH v4 16/16] xen: Convert page_to_mfn and mfn_to_page " Julien Grall
  2018-02-21 14:25   ` Razvan Cojocaru
  2018-02-21 14:59   ` Paul Durrant
@ 2018-02-21 23:20   ` Boris Ostrovsky
  2018-02-23  4:59   ` Tian, Kevin
                     ` (2 subsequent siblings)
  5 siblings, 0 replies; 76+ messages in thread
From: Boris Ostrovsky @ 2018-02-21 23:20 UTC (permalink / raw)
  To: Julien Grall, xen-devel
  Cc: Jun Nakajima, Kevin Tian, Stefano Stabellini, Wei Liu,
	Suravee Suthikulpanit, Razvan Cojocaru, George Dunlap,
	Andrew Cooper, Ian Jackson, Tim Deegan, Tamas K Lengyel,
	Jan Beulich, Shane Wang, Gang Wei, Paul Durrant

On 02/21/2018 09:02 AM, Julien Grall wrote:
> Most of the users of page_to_mfn and mfn_to_page are either overriding
> the macros to make them work with mfn_t or use mfn_x/_mfn because the
> rest of the function use mfn_t.
>
> So make page_to_mfn and mfn_to_page return mfn_t by default. The __*
> version are now dropped as this patch will convert all the remaining
> non-typesafe callers.
>
> Only reasonable clean-ups are done in this patch. The rest will use
> _mfn/mfn_x for the time being.
>
> Lastly, domain_page_to_mfn is also converted to use mfn_t given that
> most of the callers are now switched to _mfn(domain_page_to_mfn(...)).
>
> Signed-off-by: Julien Grall <julien.grall@arm.com>

Reviewed-by: Boris Ostrovsky <boris.ostrovsky@oracle.com>



_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 08/16] xen/mm: Drop the parameter mfn from populate_pt_range
  2018-02-21 14:02 ` [PATCH v4 08/16] xen/mm: Drop the parameter mfn from populate_pt_range Julien Grall
@ 2018-02-22 16:35   ` Wei Liu
  2018-02-22 16:40     ` Julien Grall
  0 siblings, 1 reply; 76+ messages in thread
From: Wei Liu @ 2018-02-22 16:35 UTC (permalink / raw)
  To: Julien Grall
  Cc: Tim Deegan, Stefano Stabellini, Wei Liu, George Dunlap,
	Andrew Cooper, Ian Jackson, xen-devel, Jan Beulich

On Wed, Feb 21, 2018 at 02:02:51PM +0000, Julien Grall wrote:
> The function populate_pt_range is used to populate in advance the
> page-table but it will not do the actual mapping. So passing the MFN in
> parameter is pointless. Note that the only caller pass 0...
> 
> At the same time replace 0 by INVALID_MFN to make clear the MFN is
> invalid.
> 

The mfn parameter is the first mfn of a consecutive nr MFNs passed to
map_pages_to_xen. Putting INVALID_MFN isn't helping -- the value written
to page table(s) will wrap around to 0.

And I think starting from 0 to avoid overflow is probably a better
behaviour. If you really want to make sure all entries are filled with
INVALID_MFN you should call map_pages_to_xen for nr times with each
page.

Wei.

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 09/16] xen/pdx: Introduce helper to convert MFN <-> PDX
  2018-02-21 14:02 ` [PATCH v4 09/16] xen/pdx: Introduce helper to convert MFN <-> PDX Julien Grall
@ 2018-02-22 16:39   ` Wei Liu
  0 siblings, 0 replies; 76+ messages in thread
From: Wei Liu @ 2018-02-22 16:39 UTC (permalink / raw)
  To: Julien Grall
  Cc: Tim Deegan, Stefano Stabellini, Wei Liu, George Dunlap,
	Andrew Cooper, Ian Jackson, xen-devel, Jan Beulich

On Wed, Feb 21, 2018 at 02:02:52PM +0000, Julien Grall wrote:
> This will avoid use of pfn_to_pdx(mfn_x(mfn)) over the code base.
> 
> Signed-off-by: Julien Grall <julien.grall@arm.com>
> 

Reviewed-by: Wei Liu <wei.liu2@citrix.com>

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 08/16] xen/mm: Drop the parameter mfn from populate_pt_range
  2018-02-22 16:35   ` Wei Liu
@ 2018-02-22 16:40     ` Julien Grall
  2018-02-22 16:51       ` Wei Liu
  0 siblings, 1 reply; 76+ messages in thread
From: Julien Grall @ 2018-02-22 16:40 UTC (permalink / raw)
  To: Wei Liu
  Cc: Tim Deegan, Stefano Stabellini, George Dunlap, Andrew Cooper,
	Ian Jackson, xen-devel, Jan Beulich

Hi Wei,

On 22/02/18 16:35, Wei Liu wrote:
> On Wed, Feb 21, 2018 at 02:02:51PM +0000, Julien Grall wrote:
>> The function populate_pt_range is used to populate in advance the
>> page-table but it will not do the actual mapping. So passing the MFN in
>> parameter is pointless. Note that the only caller pass 0...
>>
>> At the same time replace 0 by INVALID_MFN to make clear the MFN is
>> invalid.
>>
> 
> The mfn parameter is the first mfn of a consecutive nr MFNs passed to
> map_pages_to_xen. Putting INVALID_MFN isn't helping -- the value written
> to page table(s) will wrap around to 0.
> 
> And I think starting from 0 to avoid overflow is probably a better
> behaviour. If you really want to make sure all entries are filled with
> INVALID_MFN you should call map_pages_to_xen for nr times with each
> page.

I am not sure to understand this. From its name, populate_pt_range 
should only create the intermediate tables. The leaf entry will stay 
invalid. So how the value of mfn matters? Is it because the code is 
written in a such way that passing INVALID_MFN will result to undefined 
behavior?

Cheers,

-- 
Julien Grall

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 08/16] xen/mm: Drop the parameter mfn from populate_pt_range
  2018-02-22 16:40     ` Julien Grall
@ 2018-02-22 16:51       ` Wei Liu
  2018-02-22 16:55         ` Julien Grall
  0 siblings, 1 reply; 76+ messages in thread
From: Wei Liu @ 2018-02-22 16:51 UTC (permalink / raw)
  To: Julien Grall
  Cc: Stefano Stabellini, Wei Liu, George Dunlap, Ian Jackson,
	Tim Deegan, xen-devel, Jan Beulich, Andrew Cooper

On Thu, Feb 22, 2018 at 04:40:04PM +0000, Julien Grall wrote:
> Hi Wei,
> 
> On 22/02/18 16:35, Wei Liu wrote:
> > On Wed, Feb 21, 2018 at 02:02:51PM +0000, Julien Grall wrote:
> > > The function populate_pt_range is used to populate in advance the
> > > page-table but it will not do the actual mapping. So passing the MFN in
> > > parameter is pointless. Note that the only caller pass 0...
> > > 
> > > At the same time replace 0 by INVALID_MFN to make clear the MFN is
> > > invalid.
> > > 
> > 
> > The mfn parameter is the first mfn of a consecutive nr MFNs passed to
> > map_pages_to_xen. Putting INVALID_MFN isn't helping -- the value written
> > to page table(s) will wrap around to 0.
> > 
> > And I think starting from 0 to avoid overflow is probably a better
> > behaviour. If you really want to make sure all entries are filled with
> > INVALID_MFN you should call map_pages_to_xen for nr times with each
> > page.
> 
> I am not sure to understand this. From its name, populate_pt_range should
> only create the intermediate tables. The leaf entry will stay invalid. So
> how the value of mfn matters? Is it because the code is written in a such
> way that passing INVALID_MFN will result to undefined behavior?

Right, that's what I meant. It doesn't matter whether you use 0 or
INVALID_MFN.

Unsigned integer overflow is not UB in C, so passing INVALID_MFN is
safe.

But your intention seemed to be filling all entries with INVALID_MFN to
aid debugging, so the function doesn't do what I think you wanted it to
do. It could be I misunderstood your intention.

Wei.

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 08/16] xen/mm: Drop the parameter mfn from populate_pt_range
  2018-02-22 16:51       ` Wei Liu
@ 2018-02-22 16:55         ` Julien Grall
  2018-02-22 17:10           ` Wei Liu
  2018-03-02 14:55           ` Jan Beulich
  0 siblings, 2 replies; 76+ messages in thread
From: Julien Grall @ 2018-02-22 16:55 UTC (permalink / raw)
  To: Wei Liu
  Cc: Stefano Stabellini, George Dunlap, Andrew Cooper, Tim Deegan,
	xen-devel, Jan Beulich, Ian Jackson

Hi Wei,

On 22/02/18 16:51, Wei Liu wrote:
> On Thu, Feb 22, 2018 at 04:40:04PM +0000, Julien Grall wrote:
>> Hi Wei,
>>
>> On 22/02/18 16:35, Wei Liu wrote:
>>> On Wed, Feb 21, 2018 at 02:02:51PM +0000, Julien Grall wrote:
>>>> The function populate_pt_range is used to populate in advance the
>>>> page-table but it will not do the actual mapping. So passing the MFN in
>>>> parameter is pointless. Note that the only caller pass 0...
>>>>
>>>> At the same time replace 0 by INVALID_MFN to make clear the MFN is
>>>> invalid.
>>>>
>>>
>>> The mfn parameter is the first mfn of a consecutive nr MFNs passed to
>>> map_pages_to_xen. Putting INVALID_MFN isn't helping -- the value written
>>> to page table(s) will wrap around to 0.
>>>
>>> And I think starting from 0 to avoid overflow is probably a better
>>> behaviour. If you really want to make sure all entries are filled with
>>> INVALID_MFN you should call map_pages_to_xen for nr times with each
>>> page.
>>
>> I am not sure to understand this. From its name, populate_pt_range should
>> only create the intermediate tables. The leaf entry will stay invalid. So
>> how the value of mfn matters? Is it because the code is written in a such
>> way that passing INVALID_MFN will result to undefined behavior?
> 
> Right, that's what I meant. It doesn't matter whether you use 0 or
> INVALID_MFN.
> 
> Unsigned integer overflow is not UB in C, so passing INVALID_MFN is
> safe.
> 
> But your intention seemed to be filling all entries with INVALID_MFN to
> aid debugging, so the function doesn't do what I think you wanted it to
> do. It could be I misunderstood your intention.

That was not my intention. I replaced 0 by INVALID_MFN because from the 
name you know the MFN is invalid. 0 could potentially be valid (at least 
on Arm) and make the code confusing to understand.

I can make it clearer in the commit message.

Cheers,

-- 
Julien Grall

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 08/16] xen/mm: Drop the parameter mfn from populate_pt_range
  2018-02-22 16:55         ` Julien Grall
@ 2018-02-22 17:10           ` Wei Liu
  2018-03-02 14:55           ` Jan Beulich
  1 sibling, 0 replies; 76+ messages in thread
From: Wei Liu @ 2018-02-22 17:10 UTC (permalink / raw)
  To: Julien Grall
  Cc: Stefano Stabellini, Wei Liu, George Dunlap, Ian Jackson,
	Tim Deegan, xen-devel, Jan Beulich, Andrew Cooper

On Thu, Feb 22, 2018 at 04:55:10PM +0000, Julien Grall wrote:
> Hi Wei,
> 
> On 22/02/18 16:51, Wei Liu wrote:
> > On Thu, Feb 22, 2018 at 04:40:04PM +0000, Julien Grall wrote:
> > > Hi Wei,
> > > 
> > > On 22/02/18 16:35, Wei Liu wrote:
> > > > On Wed, Feb 21, 2018 at 02:02:51PM +0000, Julien Grall wrote:
> > > > > The function populate_pt_range is used to populate in advance the
> > > > > page-table but it will not do the actual mapping. So passing the MFN in
> > > > > parameter is pointless. Note that the only caller pass 0...
> > > > > 
> > > > > At the same time replace 0 by INVALID_MFN to make clear the MFN is
> > > > > invalid.
> > > > > 
> > > > 
> > > > The mfn parameter is the first mfn of a consecutive nr MFNs passed to
> > > > map_pages_to_xen. Putting INVALID_MFN isn't helping -- the value written
> > > > to page table(s) will wrap around to 0.
> > > > 
> > > > And I think starting from 0 to avoid overflow is probably a better
> > > > behaviour. If you really want to make sure all entries are filled with
> > > > INVALID_MFN you should call map_pages_to_xen for nr times with each
> > > > page.
> > > 
> > > I am not sure to understand this. From its name, populate_pt_range should
> > > only create the intermediate tables. The leaf entry will stay invalid. So
> > > how the value of mfn matters? Is it because the code is written in a such
> > > way that passing INVALID_MFN will result to undefined behavior?
> > 
> > Right, that's what I meant. It doesn't matter whether you use 0 or
> > INVALID_MFN.
> > 
> > Unsigned integer overflow is not UB in C, so passing INVALID_MFN is
> > safe.
> > 
> > But your intention seemed to be filling all entries with INVALID_MFN to
> > aid debugging, so the function doesn't do what I think you wanted it to
> > do. It could be I misunderstood your intention.
> 
> That was not my intention. I replaced 0 by INVALID_MFN because from the name
> you know the MFN is invalid. 0 could potentially be valid (at least on Arm)
> and make the code confusing to understand.
> 
> I can make it clearer in the commit message.

Alright. That would be good.

Wei.

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 10/16] xen/mm: Switch map_pages_to_xen to use MFN typesafe
  2018-02-21 14:02 ` [PATCH v4 10/16] xen/mm: Switch map_pages_to_xen to use MFN typesafe Julien Grall
@ 2018-02-23  4:59   ` Tian, Kevin
  2018-02-23 17:21   ` Wei Liu
  2018-03-02 15:06   ` Jan Beulich
  2 siblings, 0 replies; 76+ messages in thread
From: Tian, Kevin @ 2018-02-23  4:59 UTC (permalink / raw)
  To: Julien Grall, xen-devel
  Cc: Stefano Stabellini, Wei Liu, George Dunlap, Andrew Cooper,
	Ian Jackson, Tim Deegan, Jan Beulich, Wang, Shane, Wei, Gang

> From: Julien Grall [mailto:julien.grall@arm.com]
> Sent: Wednesday, February 21, 2018 10:03 PM
> 
> The current prototype is slightly confusing because it takes a virtual
> address and a physical frame (not address!). Switching to MFN will improve
> safety and reduce the chance to mistakenly invert the 2 parameters.
> 
> Signed-off-by: Julien Grall <julien.grall@arm.com>

Reviewed-by: Kevin Tian <kevin.tian@intel.com>

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 16/16] xen: Convert page_to_mfn and mfn_to_page to use typesafe MFN
  2018-02-21 14:02 ` [PATCH v4 16/16] xen: Convert page_to_mfn and mfn_to_page " Julien Grall
                     ` (2 preceding siblings ...)
  2018-02-21 23:20   ` Boris Ostrovsky
@ 2018-02-23  4:59   ` Tian, Kevin
  2018-02-23 17:31   ` Wei Liu
  2018-03-02 16:08   ` Jan Beulich
  5 siblings, 0 replies; 76+ messages in thread
From: Tian, Kevin @ 2018-02-23  4:59 UTC (permalink / raw)
  To: Julien Grall, xen-devel
  Cc: Nakajima, Jun, Stefano Stabellini, Wei Liu,
	Suravee Suthikulpanit, Razvan Cojocaru, George Dunlap,
	Andrew Cooper, Ian Jackson, Tim Deegan, Tamas K Lengyel,
	Jan Beulich, Wang, Shane, Boris Ostrovsky, Wei, Gang,
	Paul Durrant

> From: Julien Grall [mailto:julien.grall@arm.com]
> Sent: Wednesday, February 21, 2018 10:03 PM
> 
> Most of the users of page_to_mfn and mfn_to_page are either overriding
> the macros to make them work with mfn_t or use mfn_x/_mfn because the
> rest of the function use mfn_t.
> 
> So make page_to_mfn and mfn_to_page return mfn_t by default. The __*
> version are now dropped as this patch will convert all the remaining
> non-typesafe callers.
> 
> Only reasonable clean-ups are done in this patch. The rest will use
> _mfn/mfn_x for the time being.
> 
> Lastly, domain_page_to_mfn is also converted to use mfn_t given that
> most of the callers are now switched to _mfn(domain_page_to_mfn(...)).
> 
> Signed-off-by: Julien Grall <julien.grall@arm.com>
> 

Reviewed-by: Kevin Tian <kevin.tian@intel.com>

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 10/16] xen/mm: Switch map_pages_to_xen to use MFN typesafe
  2018-02-21 14:02 ` [PATCH v4 10/16] xen/mm: Switch map_pages_to_xen to use MFN typesafe Julien Grall
  2018-02-23  4:59   ` Tian, Kevin
@ 2018-02-23 17:21   ` Wei Liu
  2018-03-02 15:06   ` Jan Beulich
  2 siblings, 0 replies; 76+ messages in thread
From: Wei Liu @ 2018-02-23 17:21 UTC (permalink / raw)
  To: Julien Grall
  Cc: Tim Deegan, Kevin Tian, Stefano Stabellini, Wei Liu,
	George Dunlap, Andrew Cooper, Ian Jackson, xen-devel,
	Jan Beulich, Shane Wang, Gang Wei

On Wed, Feb 21, 2018 at 02:02:53PM +0000, Julien Grall wrote:
> The current prototype is slightly confusing because it takes a virtual
> address and a physical frame (not address!). Switching to MFN will improve
> safety and reduce the chance to mistakenly invert the 2 parameters.
> 
> Signed-off-by: Julien Grall <julien.grall@arm.com>

Reviewed-by: Wei Liu <wei.liu2@citrix.com>

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 11/16] xen/mm: Switch page_alloc.c to typesafe MFN
  2018-02-21 14:02 ` [PATCH v4 11/16] xen/mm: Switch page_alloc.c to typesafe MFN Julien Grall
@ 2018-02-23 17:21   ` Wei Liu
  2018-03-02 15:18   ` Jan Beulich
  1 sibling, 0 replies; 76+ messages in thread
From: Wei Liu @ 2018-02-23 17:21 UTC (permalink / raw)
  To: Julien Grall
  Cc: Tim Deegan, Stefano Stabellini, Wei Liu, George Dunlap,
	Andrew Cooper, Ian Jackson, xen-devel, Jan Beulich

On Wed, Feb 21, 2018 at 02:02:54PM +0000, Julien Grall wrote:
> No functional change intended.
> 
> Signed-off-by: Julien Grall <julien.grall@arm.com>

Reviewed-by: Wei Liu <wei.liu2@citrix.com>

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 12/16] xen/mm: Switch common/memory.c to use typesafe MFN
  2018-02-21 14:02 ` [PATCH v4 12/16] xen/mm: Switch common/memory.c to use " Julien Grall
@ 2018-02-23 17:26   ` Wei Liu
  2018-02-23 17:46     ` Julien Grall
  2018-03-02 15:34   ` Jan Beulich
  1 sibling, 1 reply; 76+ messages in thread
From: Wei Liu @ 2018-02-23 17:26 UTC (permalink / raw)
  To: Julien Grall
  Cc: Tim Deegan, Stefano Stabellini, Wei Liu, George Dunlap,
	Andrew Cooper, Ian Jackson, xen-devel, Jan Beulich

On Wed, Feb 21, 2018 at 02:02:55PM +0000, Julien Grall wrote:
> A new helper copy_mfn_to_guest is introduced to easily to copy a MFN to
> the guest memory.
> 
> Not functional change intended

Is there a reason to not make all guest accessors tyep-safe instead?

Wei.

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 13/16] xen/grant: Switch {create, replace}_grant_p2m_mapping to typesafe MFN
  2018-02-21 14:02 ` [PATCH v4 13/16] xen/grant: Switch {create, replace}_grant_p2m_mapping to " Julien Grall
@ 2018-02-23 17:29   ` Wei Liu
  2018-03-02 15:38   ` Jan Beulich
  1 sibling, 0 replies; 76+ messages in thread
From: Wei Liu @ 2018-02-23 17:29 UTC (permalink / raw)
  To: Julien Grall
  Cc: Tim Deegan, Stefano Stabellini, Wei Liu, George Dunlap,
	Andrew Cooper, Ian Jackson, xen-devel, Jan Beulich

On Wed, Feb 21, 2018 at 02:02:56PM +0000, Julien Grall wrote:
> The current prototype is slightly confusing because it takes a guest
> physical address and a machine physical frame (not address!). Switching to
> MFN will improve safety and reduce the chance to mistakenly invert the
> 2 parameters.
> 
> Signed-off-by: Julien grall <julien.grall@arm.com>
> 

Reviewed-by: Wei Liu <wei.liu2@citrix.com>

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 14/16] xen/grant: Switch common/grant_table.c to use typesafe MFN
  2018-02-21 14:02 ` [PATCH v4 14/16] xen/grant: Switch common/grant_table.c to use " Julien Grall
@ 2018-02-23 17:30   ` Wei Liu
  2018-03-02 15:54   ` Jan Beulich
  1 sibling, 0 replies; 76+ messages in thread
From: Wei Liu @ 2018-02-23 17:30 UTC (permalink / raw)
  To: Julien Grall
  Cc: Tim Deegan, Stefano Stabellini, Wei Liu, George Dunlap,
	Andrew Cooper, Ian Jackson, xen-devel, Jan Beulich

On Wed, Feb 21, 2018 at 02:02:57PM +0000, Julien Grall wrote:
> No functional change intended.
> 
> Signed-off-by: Julien Grall <julien.grall@arm.com>

Reviewed-by: Wei Liu <wei.liu2@citrix.com>

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 16/16] xen: Convert page_to_mfn and mfn_to_page to use typesafe MFN
  2018-02-21 14:02 ` [PATCH v4 16/16] xen: Convert page_to_mfn and mfn_to_page " Julien Grall
                     ` (3 preceding siblings ...)
  2018-02-23  4:59   ` Tian, Kevin
@ 2018-02-23 17:31   ` Wei Liu
  2018-03-02 16:08   ` Jan Beulich
  5 siblings, 0 replies; 76+ messages in thread
From: Wei Liu @ 2018-02-23 17:31 UTC (permalink / raw)
  To: Julien Grall
  Cc: Jun Nakajima, Tim Deegan, Kevin Tian, Stefano Stabellini,
	Wei Liu, Suravee Suthikulpanit, Razvan Cojocaru, George Dunlap,
	Andrew Cooper, Ian Jackson, xen-devel, Tamas K Lengyel,
	Jan Beulich, Shane Wang, Boris Ostrovsky, Gang Wei, Paul Durrant

On Wed, Feb 21, 2018 at 02:02:59PM +0000, Julien Grall wrote:
> Most of the users of page_to_mfn and mfn_to_page are either overriding
> the macros to make them work with mfn_t or use mfn_x/_mfn because the
> rest of the function use mfn_t.
> 
> So make page_to_mfn and mfn_to_page return mfn_t by default. The __*
> version are now dropped as this patch will convert all the remaining
> non-typesafe callers.
> 
> Only reasonable clean-ups are done in this patch. The rest will use
> _mfn/mfn_x for the time being.
> 
> Lastly, domain_page_to_mfn is also converted to use mfn_t given that
> most of the callers are now switched to _mfn(domain_page_to_mfn(...)).
> 
> Signed-off-by: Julien Grall <julien.grall@arm.com>

Reviewed-by: Wei Liu <wei.liu2@citrix.com>

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 12/16] xen/mm: Switch common/memory.c to use typesafe MFN
  2018-02-23 17:26   ` Wei Liu
@ 2018-02-23 17:46     ` Julien Grall
  2018-02-23 18:05       ` Wei Liu
  0 siblings, 1 reply; 76+ messages in thread
From: Julien Grall @ 2018-02-23 17:46 UTC (permalink / raw)
  To: Wei Liu
  Cc: Stefano Stabellini, George Dunlap, Andrew Cooper, Tim Deegan,
	xen-devel, Jan Beulich, Ian Jackson



On 23/02/18 17:26, Wei Liu wrote:
> On Wed, Feb 21, 2018 at 02:02:55PM +0000, Julien Grall wrote:
>> A new helper copy_mfn_to_guest is introduced to easily to copy a MFN to
>> the guest memory.
>>
>> Not functional change intended
> 
> Is there a reason to not make all guest accessors tyep-safe instead?

Could you clarify what you mean? MFN and xen_pfn_t have different type 
on Arm. So I am not sure how you would be able to make them typesafe.

Cheers,

-- 
Julien Grall

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 12/16] xen/mm: Switch common/memory.c to use typesafe MFN
  2018-02-23 17:46     ` Julien Grall
@ 2018-02-23 18:05       ` Wei Liu
  2018-02-23 18:06         ` Julien Grall
  0 siblings, 1 reply; 76+ messages in thread
From: Wei Liu @ 2018-02-23 18:05 UTC (permalink / raw)
  To: Julien Grall
  Cc: Tim Deegan, Stefano Stabellini, Wei Liu, George Dunlap,
	Andrew Cooper, Ian Jackson, xen-devel, Jan Beulich

On Fri, Feb 23, 2018 at 05:46:39PM +0000, Julien Grall wrote:
> 
> 
> On 23/02/18 17:26, Wei Liu wrote:
> > On Wed, Feb 21, 2018 at 02:02:55PM +0000, Julien Grall wrote:
> > > A new helper copy_mfn_to_guest is introduced to easily to copy a MFN to
> > > the guest memory.
> > > 
> > > Not functional change intended
> > 
> > Is there a reason to not make all guest accessors tyep-safe instead?
> 
> Could you clarify what you mean? MFN and xen_pfn_t have different type on
> Arm. So I am not sure how you would be able to make them typesafe.

What I meant was to make copy_{to,from}_guest* type-safe. I just feel it
a bit strange you only created a wrapper for this file. I wonder why.

Note I'm just asking question. That's not necessarily a good idea to
turn them all in the end.

Wei.

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 12/16] xen/mm: Switch common/memory.c to use typesafe MFN
  2018-02-23 18:05       ` Wei Liu
@ 2018-02-23 18:06         ` Julien Grall
  2018-02-23 18:10           ` Wei Liu
  0 siblings, 1 reply; 76+ messages in thread
From: Julien Grall @ 2018-02-23 18:06 UTC (permalink / raw)
  To: Wei Liu
  Cc: Stefano Stabellini, George Dunlap, Andrew Cooper, Tim Deegan,
	xen-devel, Jan Beulich, Ian Jackson

Hi,

On 23/02/18 18:05, Wei Liu wrote:
> On Fri, Feb 23, 2018 at 05:46:39PM +0000, Julien Grall wrote:
>>
>>
>> On 23/02/18 17:26, Wei Liu wrote:
>>> On Wed, Feb 21, 2018 at 02:02:55PM +0000, Julien Grall wrote:
>>>> A new helper copy_mfn_to_guest is introduced to easily to copy a MFN to
>>>> the guest memory.
>>>>
>>>> Not functional change intended
>>>
>>> Is there a reason to not make all guest accessors tyep-safe instead?
>>
>> Could you clarify what you mean? MFN and xen_pfn_t have different type on
>> Arm. So I am not sure how you would be able to make them typesafe.
> 
> What I meant was to make copy_{to,from}_guest* type-safe. I just feel it
> a bit strange you only created a wrapper for this file. I wonder why.

Oh, I can have a look at that.

> 
> Note I'm just asking question. That's not necessarily a good idea to
> turn them all in the end.

Cheers,


-- 
Julien Grall

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 12/16] xen/mm: Switch common/memory.c to use typesafe MFN
  2018-02-23 18:06         ` Julien Grall
@ 2018-02-23 18:10           ` Wei Liu
  0 siblings, 0 replies; 76+ messages in thread
From: Wei Liu @ 2018-02-23 18:10 UTC (permalink / raw)
  To: Julien Grall
  Cc: Tim Deegan, Stefano Stabellini, Wei Liu, George Dunlap,
	Andrew Cooper, Ian Jackson, xen-devel, Jan Beulich

On Fri, Feb 23, 2018 at 06:06:55PM +0000, Julien Grall wrote:
> Hi,
> 
> On 23/02/18 18:05, Wei Liu wrote:
> > On Fri, Feb 23, 2018 at 05:46:39PM +0000, Julien Grall wrote:
> > > 
> > > 
> > > On 23/02/18 17:26, Wei Liu wrote:
> > > > On Wed, Feb 21, 2018 at 02:02:55PM +0000, Julien Grall wrote:
> > > > > A new helper copy_mfn_to_guest is introduced to easily to copy a MFN to
> > > > > the guest memory.
> > > > > 
> > > > > Not functional change intended
> > > > 
> > > > Is there a reason to not make all guest accessors tyep-safe instead?
> > > 
> > > Could you clarify what you mean? MFN and xen_pfn_t have different type on
> > > Arm. So I am not sure how you would be able to make them typesafe.
> > 
> > What I meant was to make copy_{to,from}_guest* type-safe. I just feel it
> > a bit strange you only created a wrapper for this file. I wonder why.
> 
> Oh, I can have a look at that.
> 

Before you do any real work please wait for other people to comment.

I haven't entirely convinced myself it is a good idea. ;-)

Wei.

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 06/16] xen/x86: Remove unused override of page_to_mfn/mfn_to_page
  2018-02-21 14:02 ` [PATCH v4 06/16] xen/x86: Remove unused override of page_to_mfn/mfn_to_page Julien Grall
@ 2018-03-01 11:20   ` George Dunlap
  2018-03-02 14:42   ` Jan Beulich
  1 sibling, 0 replies; 76+ messages in thread
From: George Dunlap @ 2018-03-01 11:20 UTC (permalink / raw)
  To: Julien Grall, xen-devel; +Cc: George Dunlap, Andrew Cooper, Jan Beulich

On 02/21/2018 02:02 PM, Julien Grall wrote:
> A few files override page_to_mfn/mfn_to_page but actually never use
> those macros. So drop them.
> 
> Signed-off-by: Julien Grall <julien.grall@arm.com>

Acked-by: George Dunlap <george.dunlap@citrix.com>

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 06/16] xen/x86: Remove unused override of page_to_mfn/mfn_to_page
  2018-02-21 14:02 ` [PATCH v4 06/16] xen/x86: Remove unused override of page_to_mfn/mfn_to_page Julien Grall
  2018-03-01 11:20   ` George Dunlap
@ 2018-03-02 14:42   ` Jan Beulich
  2018-03-02 14:44     ` Julien Grall
  1 sibling, 1 reply; 76+ messages in thread
From: Jan Beulich @ 2018-03-02 14:42 UTC (permalink / raw)
  To: Julien Grall; +Cc: George Dunlap, Andrew Cooper, xen-devel

>>> On 21.02.18 at 15:02, <julien.grall@arm.com> wrote:
> A few files override page_to_mfn/mfn_to_page but actually never use
> those macros. So drop them.
> 
> Signed-off-by: Julien Grall <julien.grall@arm.com>

It doesn't look like there are any risky uses of the removed
symbols in the headers, so
Acked-by: Jan Beulich <jbeulich@suse.com>
assuming this has been build-tested in relevant configurations.

Jan



_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 06/16] xen/x86: Remove unused override of page_to_mfn/mfn_to_page
  2018-03-02 14:42   ` Jan Beulich
@ 2018-03-02 14:44     ` Julien Grall
  2018-03-02 15:11       ` Jan Beulich
  0 siblings, 1 reply; 76+ messages in thread
From: Julien Grall @ 2018-03-02 14:44 UTC (permalink / raw)
  To: Jan Beulich; +Cc: George Dunlap, Andrew Cooper, xen-devel

Hi,

On 02/03/18 14:42, Jan Beulich wrote:
>>>> On 21.02.18 at 15:02, <julien.grall@arm.com> wrote:
>> A few files override page_to_mfn/mfn_to_page but actually never use
>> those macros. So drop them.
>>
>> Signed-off-by: Julien Grall <julien.grall@arm.com>
> 
> It doesn't look like there are any risky uses of the removed
> symbols in the headers, so
> Acked-by: Jan Beulich <jbeulich@suse.com>
> assuming this has been build-tested in relevant configurations.

All patches have been build-tested one by one. I have also looked at the 
code to see if there was any use within the code.

Note that after patch #16, they will become irrelevant.

Cheers,

-- 
Julien Grall

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 07/16] xen/x86: mm: Switch x86/mm.c to use typesafe for virt_to_mfn
  2018-02-21 14:02 ` [PATCH v4 07/16] xen/x86: mm: Switch x86/mm.c to use typesafe for virt_to_mfn Julien Grall
@ 2018-03-02 14:45   ` Jan Beulich
  2018-03-02 14:46     ` Julien Grall
  0 siblings, 1 reply; 76+ messages in thread
From: Jan Beulich @ 2018-03-02 14:45 UTC (permalink / raw)
  To: Julien Grall; +Cc: Andrew Cooper, xen-devel

>>> On 21.02.18 at 15:02, <julien.grall@arm.com> wrote:
> No functional change intended.
> 
> Signed-off Julien Grall <julien.grall@arm.com>

Acked-by: Jan Beulich <jbeulich@suse.com>
with one remark:

> @@ -5545,8 +5547,7 @@ static void __memguard_change_range(void *p, unsigned long l, int guard)
>      if ( guard )
>          flags &= ~_PAGE_PRESENT;
>  
> -    map_pages_to_xen(
> -        _p, virt_to_maddr(p) >> PAGE_SHIFT, _l >> PAGE_SHIFT, flags);
> +    map_pages_to_xen(_p, mfn_x(virt_to_mfn(p)), _l >> PAGE_SHIFT, flags);

Would have been nice if this was switched to PFN_DOWN() instead
of the open coded shift right away.

Jan


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 07/16] xen/x86: mm: Switch x86/mm.c to use typesafe for virt_to_mfn
  2018-03-02 14:45   ` Jan Beulich
@ 2018-03-02 14:46     ` Julien Grall
  0 siblings, 0 replies; 76+ messages in thread
From: Julien Grall @ 2018-03-02 14:46 UTC (permalink / raw)
  To: Jan Beulich; +Cc: Andrew Cooper, xen-devel

Hi Jan,

On 02/03/18 14:45, Jan Beulich wrote:
>>>> On 21.02.18 at 15:02, <julien.grall@arm.com> wrote:
>> No functional change intended.
>>
>> Signed-off Julien Grall <julien.grall@arm.com>
> 
> Acked-by: Jan Beulich <jbeulich@suse.com>
> with one remark:
> 
>> @@ -5545,8 +5547,7 @@ static void __memguard_change_range(void *p, unsigned long l, int guard)
>>       if ( guard )
>>           flags &= ~_PAGE_PRESENT;
>>   
>> -    map_pages_to_xen(
>> -        _p, virt_to_maddr(p) >> PAGE_SHIFT, _l >> PAGE_SHIFT, flags);
>> +    map_pages_to_xen(_p, mfn_x(virt_to_mfn(p)), _l >> PAGE_SHIFT, flags);
> 
> Would have been nice if this was switched to PFN_DOWN() instead
> of the open coded shift right away.

I can do that.

Cheers,

-- 
Julien Grall

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 08/16] xen/mm: Drop the parameter mfn from populate_pt_range
  2018-02-22 16:55         ` Julien Grall
  2018-02-22 17:10           ` Wei Liu
@ 2018-03-02 14:55           ` Jan Beulich
  2018-03-05 13:43             ` Julien Grall
  1 sibling, 1 reply; 76+ messages in thread
From: Jan Beulich @ 2018-03-02 14:55 UTC (permalink / raw)
  To: Julien Grall
  Cc: Tim Deegan, Stefano Stabellini, Wei Liu, George Dunlap,
	Andrew Cooper, Ian Jackson, xen-devel

>>> On 22.02.18 at 17:55, <julien.grall@arm.com> wrote:
> On 22/02/18 16:51, Wei Liu wrote:
>> On Thu, Feb 22, 2018 at 04:40:04PM +0000, Julien Grall wrote:
>>> On 22/02/18 16:35, Wei Liu wrote:
>>>> On Wed, Feb 21, 2018 at 02:02:51PM +0000, Julien Grall wrote:
>>>>> The function populate_pt_range is used to populate in advance the
>>>>> page-table but it will not do the actual mapping. So passing the MFN in
>>>>> parameter is pointless. Note that the only caller pass 0...
>>>>>
>>>>> At the same time replace 0 by INVALID_MFN to make clear the MFN is
>>>>> invalid.
>>>>>
>>>>
>>>> The mfn parameter is the first mfn of a consecutive nr MFNs passed to
>>>> map_pages_to_xen. Putting INVALID_MFN isn't helping -- the value written
>>>> to page table(s) will wrap around to 0.
>>>>
>>>> And I think starting from 0 to avoid overflow is probably a better
>>>> behaviour. If you really want to make sure all entries are filled with
>>>> INVALID_MFN you should call map_pages_to_xen for nr times with each
>>>> page.
>>>
>>> I am not sure to understand this. From its name, populate_pt_range should
>>> only create the intermediate tables. The leaf entry will stay invalid. So
>>> how the value of mfn matters? Is it because the code is written in a such
>>> way that passing INVALID_MFN will result to undefined behavior?
>> 
>> Right, that's what I meant. It doesn't matter whether you use 0 or
>> INVALID_MFN.
>> 
>> Unsigned integer overflow is not UB in C, so passing INVALID_MFN is
>> safe.
>> 
>> But your intention seemed to be filling all entries with INVALID_MFN to
>> aid debugging, so the function doesn't do what I think you wanted it to
>> do. It could be I misunderstood your intention.
> 
> That was not my intention. I replaced 0 by INVALID_MFN because from the 
> name you know the MFN is invalid. 0 could potentially be valid (at least 
> on Arm) and make the code confusing to understand.
> 
> I can make it clearer in the commit message.

I don't think that'll be much better; I agree with Wei that you
don't want the wrapping behavior here. What you want to do
is skip the increments in x86's map_pages_to_xen() when
mfn is INVALID_MFN. Granted this should have been done
before (so that there wouldn't have been incrementing from
zero), but as you say MFN 0 isn't fundamentally invalid (albeit
on x86 we almost make it invalid).

As to your earlier argument - please don't forget that on x86
the function still fills all leaf entries in the range, just that they
all will be non-present ones.

Jan


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 10/16] xen/mm: Switch map_pages_to_xen to use MFN typesafe
  2018-02-21 14:02 ` [PATCH v4 10/16] xen/mm: Switch map_pages_to_xen to use MFN typesafe Julien Grall
  2018-02-23  4:59   ` Tian, Kevin
  2018-02-23 17:21   ` Wei Liu
@ 2018-03-02 15:06   ` Jan Beulich
  2018-03-02 15:08     ` Jan Beulich
  2018-03-05 14:07     ` Julien Grall
  2 siblings, 2 replies; 76+ messages in thread
From: Jan Beulich @ 2018-03-02 15:06 UTC (permalink / raw)
  To: Julien Grall
  Cc: Tim Deegan, Kevin Tian, Stefano Stabellini, Wei Liu,
	George Dunlap, Andrew Cooper, Ian Jackson, xen-devel, Shane Wang,
	Gang Wei

>>> On 21.02.18 at 15:02, <julien.grall@arm.com> wrote:
> --- a/xen/arch/x86/x86_64/mm.c
> +++ b/xen/arch/x86/x86_64/mm.c
> @@ -40,6 +40,10 @@ asm(".file \"" __FILE__ "\"");
>  #include <asm/mem_sharing.h>
>  #include <public/memory.h>
>  
> +/* Override macros from asm/page.h to make them work with mfn_t */
> +#undef page_to_mfn
> +#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))

I can't spot where this is needed in this file.

> @@ -234,7 +238,7 @@ void vunmap(const void *va)
>  #ifndef _PAGE_NONE
>      destroy_xen_mappings(addr, addr + PAGE_SIZE * pages);
>  #else /* Avoid tearing down intermediate page tables. */
> -    map_pages_to_xen(addr, 0, pages, _PAGE_NONE);
> +    map_pages_to_xen(addr, _mfn(0), pages, _PAGE_NONE);

INVALID_MFN?

Also please again see about using PFN_DOWN() for some of the
sizes passed to the function as you touch those places anyway.

Jan


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 10/16] xen/mm: Switch map_pages_to_xen to use MFN typesafe
  2018-03-02 15:06   ` Jan Beulich
@ 2018-03-02 15:08     ` Jan Beulich
  2018-03-05 14:07     ` Julien Grall
  1 sibling, 0 replies; 76+ messages in thread
From: Jan Beulich @ 2018-03-02 15:08 UTC (permalink / raw)
  To: Julien Grall
  Cc: Tim Deegan, Kevin Tian, Stefano Stabellini, Wei Liu,
	George Dunlap, Andrew Cooper, Ian Jackson, xen-devel, Shane Wang,
	Gang Wei

>>> On 02.03.18 at 16:06, <JBeulich@suse.com> wrote:
>>>> On 21.02.18 at 15:02, <julien.grall@arm.com> wrote:
>> --- a/xen/arch/x86/x86_64/mm.c
>> +++ b/xen/arch/x86/x86_64/mm.c
>> @@ -40,6 +40,10 @@ asm(".file \"" __FILE__ "\"");
>>  #include <asm/mem_sharing.h>
>>  #include <public/memory.h>
>>  
>> +/* Override macros from asm/page.h to make them work with mfn_t */
>> +#undef page_to_mfn
>> +#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
> 
> I can't spot where this is needed in this file.

Oh, I'm sorry, I should have looked at the file as well, not just
at the hunks in the patch.

Jan


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 06/16] xen/x86: Remove unused override of page_to_mfn/mfn_to_page
  2018-03-02 14:44     ` Julien Grall
@ 2018-03-02 15:11       ` Jan Beulich
  2018-03-05 13:29         ` Julien Grall
  0 siblings, 1 reply; 76+ messages in thread
From: Jan Beulich @ 2018-03-02 15:11 UTC (permalink / raw)
  To: Julien Grall; +Cc: George Dunlap, Andrew Cooper, xen-devel

>>> On 02.03.18 at 15:44, <julien.grall@arm.com> wrote:
> On 02/03/18 14:42, Jan Beulich wrote:
>>>>> On 21.02.18 at 15:02, <julien.grall@arm.com> wrote:
>>> A few files override page_to_mfn/mfn_to_page but actually never use
>>> those macros. So drop them.
>>>
>>> Signed-off-by: Julien Grall <julien.grall@arm.com>
>> 
>> It doesn't look like there are any risky uses of the removed
>> symbols in the headers, so
>> Acked-by: Jan Beulich <jbeulich@suse.com>
>> assuming this has been build-tested in relevant configurations.
> 
> All patches have been build-tested one by one.

I've taken that for given. I did say "in relevant configurations"
because things like BIGMEM=y or SHADOW_PAGING=n may
cause issues despite a "normal" build having gone fine.

Jan


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 11/16] xen/mm: Switch page_alloc.c to typesafe MFN
  2018-02-21 14:02 ` [PATCH v4 11/16] xen/mm: Switch page_alloc.c to typesafe MFN Julien Grall
  2018-02-23 17:21   ` Wei Liu
@ 2018-03-02 15:18   ` Jan Beulich
  2018-03-02 15:57     ` Julien Grall
  1 sibling, 1 reply; 76+ messages in thread
From: Jan Beulich @ 2018-03-02 15:18 UTC (permalink / raw)
  To: Julien Grall
  Cc: Tim Deegan, Stefano Stabellini, Wei Liu, George Dunlap,
	Andrew Cooper, Ian Jackson, xen-devel

>>> On 21.02.18 at 15:02, <julien.grall@arm.com> wrote:
> @@ -1735,14 +1743,14 @@ static void init_heap_pages(
>  
>          if ( unlikely(!avail[nid]) )
>          {
> -            unsigned long s = page_to_mfn(pg + i);
> -            unsigned long e = page_to_mfn(pg + nr_pages - 1) + 1;
> +            unsigned long s = mfn_x(page_to_mfn(pg + i));
> +            unsigned long e = mfn_x(mfn_add(page_to_mfn(pg + nr_pages - 1), 1));
>              bool_t use_tail = (nid == phys_to_nid(pfn_to_paddr(e - 1))) &&
>                                !(s & ((1UL << MAX_ORDER) - 1)) &&
>                                (find_first_set_bit(e) <= find_first_set_bit(s));
>              unsigned long n;
>  
> -            n = init_node_heap(nid, page_to_mfn(pg+i), nr_pages - i,
> +            n = init_node_heap(nid, mfn_x(page_to_mfn(pg+i)), nr_pages - i,

You've got Wei's R-b, so I won't insist, but it would have been nice
if you added the missing blanks around the + here.

Also I think the patch doesn't go quite far enough to make the title
actually true. Care to make it say "... some of page_alloc.c ..."?

Jan


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 12/16] xen/mm: Switch common/memory.c to use typesafe MFN
  2018-02-21 14:02 ` [PATCH v4 12/16] xen/mm: Switch common/memory.c to use " Julien Grall
  2018-02-23 17:26   ` Wei Liu
@ 2018-03-02 15:34   ` Jan Beulich
  2018-03-05 14:18     ` Julien Grall
  1 sibling, 1 reply; 76+ messages in thread
From: Jan Beulich @ 2018-03-02 15:34 UTC (permalink / raw)
  To: Julien Grall
  Cc: Tim Deegan, Stefano Stabellini, Wei Liu, George Dunlap,
	Andrew Cooper, Ian Jackson, xen-devel

>>> On 21.02.18 at 15:02, <julien.grall@arm.com> wrote:
> @@ -95,11 +101,18 @@ static unsigned int max_order(const struct domain *d)
>      return min(order, MAX_ORDER + 0U);
>  }
>  
> +/* Helper to copy a typesafe MFN to guest */
> +#define copy_mfn_to_guest(hnd, off, mfn)            \
> +    ({                                              \
> +        xen_pfn_t mfn_ = mfn_x(mfn);                \
> +        __copy_to_guest_offset(hnd, off, &mfn_, 1); \
> +    })

Hmm, not really nice, but what do you do. 

>  static void increase_reservation(struct memop_args *a)
>  {
>      struct page_info *page;
>      unsigned long i;
> -    xen_pfn_t mfn;
> +    mfn_t mfn;

Please move this declaration ...

> @@ -133,7 +146,7 @@ static void increase_reservation(struct memop_args *a)
>               !guest_handle_is_null(a->extent_list) )
>          {
>              mfn = page_to_mfn(page);

... here, making the assignment its initializer. Or even avoid the
local variable altogether, as the macro has already got one. Same
elsewhere (whichever of the two variants fits), albeit maybe in the
other cases the scope can't be shrunk much.

Jan


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 13/16] xen/grant: Switch {create, replace}_grant_p2m_mapping to typesafe MFN
  2018-02-21 14:02 ` [PATCH v4 13/16] xen/grant: Switch {create, replace}_grant_p2m_mapping to " Julien Grall
  2018-02-23 17:29   ` Wei Liu
@ 2018-03-02 15:38   ` Jan Beulich
  1 sibling, 0 replies; 76+ messages in thread
From: Jan Beulich @ 2018-03-02 15:38 UTC (permalink / raw)
  To: Julien Grall
  Cc: Tim Deegan, Stefano Stabellini, Wei Liu, George Dunlap,
	Andrew Cooper, Ian Jackson, xen-devel

>>> On 21.02.18 at 15:02, <julien.grall@arm.com> wrote:
> The current prototype is slightly confusing because it takes a guest
> physical address and a machine physical frame (not address!). Switching to
> MFN will improve safety and reduce the chance to mistakenly invert the
> 2 parameters.
> 
> Signed-off-by: Julien grall <julien.grall@arm.com>

Reviewed-by: Jan Beulich <jbeulich@suse.com>



_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 14/16] xen/grant: Switch common/grant_table.c to use typesafe MFN
  2018-02-21 14:02 ` [PATCH v4 14/16] xen/grant: Switch common/grant_table.c to use " Julien Grall
  2018-02-23 17:30   ` Wei Liu
@ 2018-03-02 15:54   ` Jan Beulich
  2018-03-02 15:59     ` Julien Grall
  1 sibling, 1 reply; 76+ messages in thread
From: Jan Beulich @ 2018-03-02 15:54 UTC (permalink / raw)
  To: Julien Grall
  Cc: Tim Deegan, Stefano Stabellini, Wei Liu, George Dunlap,
	Andrew Cooper, Ian Jackson, xen-devel

>>> On 21.02.18 at 15:02, <julien.grall@arm.com> wrote:
> @@ -872,7 +879,7 @@ map_grant_ref(
>      struct grant_table *lgt, *rgt;
>      struct vcpu   *led;
>      grant_handle_t handle;
> -    unsigned long  frame = 0;
> +    mfn_t frame = _mfn(0);

If the initializer is needed at all, I think it should again become
INVALID_MFN. Same in a few other places.

> --- a/xen/include/asm-x86/grant_table.h
> +++ b/xen/include/asm-x86/grant_table.h
> @@ -76,7 +76,7 @@ static inline unsigned int gnttab_dom0_max(void)
>  #define gnttab_status_gmfn(d, t, i)                     \
>      (mfn_to_gmfn(d, gnttab_status_mfn(t, i)))
>  
> -#define gnttab_mark_dirty(d, f) paging_mark_dirty((d), _mfn(f))
> +#define gnttab_mark_dirty(d, f) paging_mark_dirty((d), f)

Please take the opportunity and also drop the stray parentheses
around d.

With these taken care of and with Wei's R-b
Acked-by: Jan Beulich <jbeulich@suse.com>

Jan


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 15/16] xen/x86: Switch mfn_to_page in x86_64/mm.c to use typesafe MFN
  2018-02-21 14:02 ` [PATCH v4 15/16] xen/x86: Switch mfn_to_page in x86_64/mm.c " Julien Grall
@ 2018-03-02 15:57   ` Jan Beulich
  0 siblings, 0 replies; 76+ messages in thread
From: Jan Beulich @ 2018-03-02 15:57 UTC (permalink / raw)
  To: Julien Grall; +Cc: Andrew Cooper, xen-devel

>>> On 21.02.18 at 15:02, <julien.grall@arm.com> wrote:
> @@ -160,7 +162,8 @@ static int m2p_mapped(unsigned long spfn)
>  
>  static int share_hotadd_m2p_table(struct mem_hotadd_info *info)
>  {
> -    unsigned long i, n, v, m2p_start_mfn = 0;
> +    unsigned long i, n, v;
> +    mfn_t m2p_start_mfn = _mfn(0);

INVALID_MFN again please.

With that
Acked-by: Jan Beulich <jbeulich@suse.com>

Jan


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 11/16] xen/mm: Switch page_alloc.c to typesafe MFN
  2018-03-02 15:18   ` Jan Beulich
@ 2018-03-02 15:57     ` Julien Grall
  0 siblings, 0 replies; 76+ messages in thread
From: Julien Grall @ 2018-03-02 15:57 UTC (permalink / raw)
  To: Jan Beulich
  Cc: Tim Deegan, Stefano Stabellini, Wei Liu, George Dunlap,
	Andrew Cooper, Ian Jackson, xen-devel



On 02/03/18 15:18, Jan Beulich wrote:
>>>> On 21.02.18 at 15:02, <julien.grall@arm.com> wrote:
>> @@ -1735,14 +1743,14 @@ static void init_heap_pages(
>>   
>>           if ( unlikely(!avail[nid]) )
>>           {
>> -            unsigned long s = page_to_mfn(pg + i);
>> -            unsigned long e = page_to_mfn(pg + nr_pages - 1) + 1;
>> +            unsigned long s = mfn_x(page_to_mfn(pg + i));
>> +            unsigned long e = mfn_x(mfn_add(page_to_mfn(pg + nr_pages - 1), 1));
>>               bool_t use_tail = (nid == phys_to_nid(pfn_to_paddr(e - 1))) &&
>>                                 !(s & ((1UL << MAX_ORDER) - 1)) &&
>>                                 (find_first_set_bit(e) <= find_first_set_bit(s));
>>               unsigned long n;
>>   
>> -            n = init_node_heap(nid, page_to_mfn(pg+i), nr_pages - i,
>> +            n = init_node_heap(nid, mfn_x(page_to_mfn(pg+i)), nr_pages - i,
> 
> You've got Wei's R-b, so I won't insist, but it would have been nice
> if you added the missing blanks around the + here.
> 
> Also I think the patch doesn't go quite far enough to make the title
> actually true. Care to make it say "... some of page_alloc.c ..."?

I will do for both. Thank you for the review.

Cheers,

-- 
Julien Grall

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 14/16] xen/grant: Switch common/grant_table.c to use typesafe MFN
  2018-03-02 15:54   ` Jan Beulich
@ 2018-03-02 15:59     ` Julien Grall
  2018-03-02 16:12       ` Jan Beulich
  0 siblings, 1 reply; 76+ messages in thread
From: Julien Grall @ 2018-03-02 15:59 UTC (permalink / raw)
  To: Jan Beulich
  Cc: Tim Deegan, Stefano Stabellini, Wei Liu, George Dunlap,
	Andrew Cooper, Ian Jackson, xen-devel

Hi Jan,

On 02/03/18 15:54, Jan Beulich wrote:
>>>> On 21.02.18 at 15:02, <julien.grall@arm.com> wrote:
>> @@ -872,7 +879,7 @@ map_grant_ref(
>>       struct grant_table *lgt, *rgt;
>>       struct vcpu   *led;
>>       grant_handle_t handle;
>> -    unsigned long  frame = 0;
>> +    mfn_t frame = _mfn(0);
> 
> If the initializer is needed at all, I think it should again become
> INVALID_MFN. Same in a few other places.

I didn't switch to INVALID_MFN because I wasn't sure if some place in 
the code where relying on 0. If you think it is fine, then I am more 
thank happy to switch to INVALID_MFN.

> 
>> --- a/xen/include/asm-x86/grant_table.h
>> +++ b/xen/include/asm-x86/grant_table.h
>> @@ -76,7 +76,7 @@ static inline unsigned int gnttab_dom0_max(void)
>>   #define gnttab_status_gmfn(d, t, i)                     \
>>       (mfn_to_gmfn(d, gnttab_status_mfn(t, i)))
>>   
>> -#define gnttab_mark_dirty(d, f) paging_mark_dirty((d), _mfn(f))
>> +#define gnttab_mark_dirty(d, f) paging_mark_dirty((d), f)
> 
> Please take the opportunity and also drop the stray parentheses
> around d.

Sure.

> 
> With these taken care of and with Wei's R-b
> Acked-by: Jan Beulich <jbeulich@suse.com>

Cheers,

-- 
Julien Grall

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 16/16] xen: Convert page_to_mfn and mfn_to_page to use typesafe MFN
  2018-02-21 14:02 ` [PATCH v4 16/16] xen: Convert page_to_mfn and mfn_to_page " Julien Grall
                     ` (4 preceding siblings ...)
  2018-02-23 17:31   ` Wei Liu
@ 2018-03-02 16:08   ` Jan Beulich
  2018-03-14 17:02     ` Julien Grall
  5 siblings, 1 reply; 76+ messages in thread
From: Jan Beulich @ 2018-03-02 16:08 UTC (permalink / raw)
  To: Julien Grall
  Cc: Tim Deegan, Kevin Tian, Stefano Stabellini, Wei Liu,
	Jun Nakajima, Razvan Cojocaru, George Dunlap, Andrew Cooper,
	Ian Jackson, xen-devel, Paul Durrant, Tamas K Lengyel,
	Suravee Suthikulpanit, Shane Wang, Boris Ostrovsky, Gang Wei

>>> On 21.02.18 at 15:02, <julien.grall@arm.com> wrote:
> --- a/xen/arch/x86/pv/emul-priv-op.c
> +++ b/xen/arch/x86/pv/emul-priv-op.c
> @@ -43,16 +43,6 @@
>  #include "emulate.h"
>  #include "mm.h"
>  
> -/* Override macros from asm/page.h to make them work with mfn_t */
> -#undef mfn_to_page
> -#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
> -#undef page_to_mfn
> -#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
> -
> -/***********************
> - * I/O emulation support
> - */

Why does this comment go away?

> @@ -478,10 +478,10 @@ extern paddr_t mem_hotplug;
>  #define SHARED_M2P(_e)           ((_e) == SHARED_M2P_ENTRY)
>  
>  #define compat_machine_to_phys_mapping ((unsigned int 
> *)RDWR_COMPAT_MPT_VIRT_START)
> -#define _set_gpfn_from_mfn(mfn, pfn) ({                        \
> -    struct domain *d = page_get_owner(__mfn_to_page(mfn));     \
> -    unsigned long entry = (d && (d == dom_cow)) ?              \
> -        SHARED_M2P_ENTRY : (pfn);                              \
> +#define _set_gpfn_from_mfn(mfn, pfn) ({                         \
> +    struct domain *d = page_get_owner(mfn_to_page(_mfn(mfn)));    \
> +    unsigned long entry = (d && (d == dom_cow)) ?               \
> +        SHARED_M2P_ENTRY : (pfn);                               \

Please don't break the alignment of the backslashes here. It also looks
like three of the four lines could be left alone altogether.

> @@ -157,10 +157,10 @@ static inline l4_pgentry_t l4e_from_paddr(paddr_t pa, unsigned int flags)
>  #define l4e_from_intpte(intpte)    ((l4_pgentry_t) { (intpte_t)(intpte) })
>  
>  /* Construct a pte from a page pointer and access flags. */
> -#define l1e_from_page(page, flags) l1e_from_pfn(__page_to_mfn(page), (flags))
> -#define l2e_from_page(page, flags) l2e_from_pfn(__page_to_mfn(page), (flags))
> -#define l3e_from_page(page, flags) l3e_from_pfn(__page_to_mfn(page), (flags))
> -#define l4e_from_page(page, flags) l4e_from_pfn(__page_to_mfn(page), (flags))
> +#define l1e_from_page(page, flags) l1e_from_mfn(page_to_mfn(page), (flags))
> +#define l2e_from_page(page, flags) l2e_from_mfn(page_to_mfn(page), (flags))
> +#define l3e_from_page(page, flags) l3e_from_mfn(page_to_mfn(page), (flags))
> +#define l4e_from_page(page, flags) l4e_from_mfn(page_to_mfn(page), (flags))

Would again have been nice if you got rid of the extra parentheses
here at the same time.

> @@ -240,12 +240,12 @@ void copy_page_sse2(void *, const void *);
>  #define __mfn_to_virt(mfn)  (maddr_to_virt((paddr_t)(mfn) << PAGE_SHIFT))
>  
>  /* Convert between machine frame numbers and page-info structures. */
> -#define __mfn_to_page(mfn)  (frame_table + pfn_to_pdx(mfn))
> -#define __page_to_mfn(pg)   pdx_to_pfn((unsigned long)((pg) - frame_table))
> +#define mfn_to_page(mfn)    (frame_table + mfn_to_pdx(mfn))
> +#define page_to_mfn(pg)     pdx_to_mfn((unsigned long)((pg) - frame_table))
>  
>  /* Convert between machine addresses and page-info structures. */
> -#define __maddr_to_page(ma) __mfn_to_page((ma) >> PAGE_SHIFT)
> -#define __page_to_maddr(pg) ((paddr_t)__page_to_mfn(pg) << PAGE_SHIFT)
> +#define __maddr_to_page(ma) mfn_to_page(maddr_to_mfn(ma))
> +#define __page_to_maddr(pg) (mfn_to_maddr(page_to_mfn(pg)))

Same here.

With at least the first two items taken care of, relevant x86 pieces
Acked-by: Jan Beulich <jbeulich@suse.com>

Jan


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 14/16] xen/grant: Switch common/grant_table.c to use typesafe MFN
  2018-03-02 15:59     ` Julien Grall
@ 2018-03-02 16:12       ` Jan Beulich
  0 siblings, 0 replies; 76+ messages in thread
From: Jan Beulich @ 2018-03-02 16:12 UTC (permalink / raw)
  To: Julien Grall
  Cc: Tim Deegan, Stefano Stabellini, Wei Liu, George Dunlap,
	Andrew Cooper, Ian Jackson, xen-devel

>>> On 02.03.18 at 16:59, <julien.grall@arm.com> wrote:
> On 02/03/18 15:54, Jan Beulich wrote:
>>>>> On 21.02.18 at 15:02, <julien.grall@arm.com> wrote:
>>> @@ -872,7 +879,7 @@ map_grant_ref(
>>>       struct grant_table *lgt, *rgt;
>>>       struct vcpu   *led;
>>>       grant_handle_t handle;
>>> -    unsigned long  frame = 0;
>>> +    mfn_t frame = _mfn(0);
>> 
>> If the initializer is needed at all, I think it should again become
>> INVALID_MFN. Same in a few other places.
> 
> I didn't switch to INVALID_MFN because I wasn't sure if some place in 
> the code where relying on 0. If you think it is fine, then I am more 
> thank happy to switch to INVALID_MFN.

Well, as said - it looks as if the initializer isn't needed at all, in
which case it's value really is a don't care.

Jan


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 06/16] xen/x86: Remove unused override of page_to_mfn/mfn_to_page
  2018-03-02 15:11       ` Jan Beulich
@ 2018-03-05 13:29         ` Julien Grall
  0 siblings, 0 replies; 76+ messages in thread
From: Julien Grall @ 2018-03-05 13:29 UTC (permalink / raw)
  To: Jan Beulich; +Cc: George Dunlap, Andrew Cooper, xen-devel

Hi Jan,

On 02/03/18 15:11, Jan Beulich wrote:
>>>> On 02.03.18 at 15:44, <julien.grall@arm.com> wrote:
>> On 02/03/18 14:42, Jan Beulich wrote:
>>>>>> On 21.02.18 at 15:02, <julien.grall@arm.com> wrote:
>>>> A few files override page_to_mfn/mfn_to_page but actually never use
>>>> those macros. So drop them.
>>>>
>>>> Signed-off-by: Julien Grall <julien.grall@arm.com>
>>>
>>> It doesn't look like there are any risky uses of the removed
>>> symbols in the headers, so
>>> Acked-by: Jan Beulich <jbeulich@suse.com>
>>> assuming this has been build-tested in relevant configurations.
>>
>> All patches have been build-tested one by one.
> 
> I've taken that for given. I did say "in relevant configurations"
> because things like BIGMEM=y or SHADOW_PAGING=n may
> cause issues despite a "normal" build having gone fine.

I have now built test this patch with BIGMEM=y, SHADOW_PAGING=n and saw 
not compilation errors.

Cheers,

> 
> Jan
> 

-- 
Julien Grall

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 08/16] xen/mm: Drop the parameter mfn from populate_pt_range
  2018-03-02 14:55           ` Jan Beulich
@ 2018-03-05 13:43             ` Julien Grall
  2018-03-05 14:00               ` Jan Beulich
  0 siblings, 1 reply; 76+ messages in thread
From: Julien Grall @ 2018-03-05 13:43 UTC (permalink / raw)
  To: Jan Beulich
  Cc: Tim Deegan, Stefano Stabellini, Wei Liu, George Dunlap,
	Andrew Cooper, Ian Jackson, xen-devel

Hi Jan,

On 02/03/18 14:55, Jan Beulich wrote:
>>>> On 22.02.18 at 17:55, <julien.grall@arm.com> wrote:
>> On 22/02/18 16:51, Wei Liu wrote:
>>> On Thu, Feb 22, 2018 at 04:40:04PM +0000, Julien Grall wrote:
>>>> On 22/02/18 16:35, Wei Liu wrote:
>>>>> On Wed, Feb 21, 2018 at 02:02:51PM +0000, Julien Grall wrote:
>>>>>> The function populate_pt_range is used to populate in advance the
>>>>>> page-table but it will not do the actual mapping. So passing the MFN in
>>>>>> parameter is pointless. Note that the only caller pass 0...
>>>>>>
>>>>>> At the same time replace 0 by INVALID_MFN to make clear the MFN is
>>>>>> invalid.
>>>>>>
>>>>>
>>>>> The mfn parameter is the first mfn of a consecutive nr MFNs passed to
>>>>> map_pages_to_xen. Putting INVALID_MFN isn't helping -- the value written
>>>>> to page table(s) will wrap around to 0.
>>>>>
>>>>> And I think starting from 0 to avoid overflow is probably a better
>>>>> behaviour. If you really want to make sure all entries are filled with
>>>>> INVALID_MFN you should call map_pages_to_xen for nr times with each
>>>>> page.
>>>>
>>>> I am not sure to understand this. From its name, populate_pt_range should
>>>> only create the intermediate tables. The leaf entry will stay invalid. So
>>>> how the value of mfn matters? Is it because the code is written in a such
>>>> way that passing INVALID_MFN will result to undefined behavior?
>>>
>>> Right, that's what I meant. It doesn't matter whether you use 0 or
>>> INVALID_MFN.
>>>
>>> Unsigned integer overflow is not UB in C, so passing INVALID_MFN is
>>> safe.
>>>
>>> But your intention seemed to be filling all entries with INVALID_MFN to
>>> aid debugging, so the function doesn't do what I think you wanted it to
>>> do. It could be I misunderstood your intention.
>>
>> That was not my intention. I replaced 0 by INVALID_MFN because from the
>> name you know the MFN is invalid. 0 could potentially be valid (at least
>> on Arm) and make the code confusing to understand.
>>
>> I can make it clearer in the commit message.
> 
> I don't think that'll be much better; I agree with Wei that you
> don't want the wrapping behavior here. What you want to do
> is skip the increments in x86's map_pages_to_xen() when
> mfn is INVALID_MFN. Granted this should have been done
> before (so that there wouldn't have been incrementing from
> zero), but as you say MFN 0 isn't fundamentally invalid (albeit
> on x86 we almost make it invalid).
> 
> As to your earlier argument - please don't forget that on x86
> the function still fills all leaf entries in the range, just that they
> all will be non-present ones.

I still don't understand why it matters. The entry is not present so the 
address is going to ignore. 0 or MFN_INVALID are just dummy value that 
are going to be replaced on the entry is made present.

Furthermore, as Wei pointed out unsigned integer overflow is not UB in 
C, so passing INVALID_MFN is safe.

Anyway, I don't have much knowledge on the x86 to make the modification 
that you suggested. So I am going to revert to _mfn(0) for x86.

Cheers,

-- 
Julien Grall

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 08/16] xen/mm: Drop the parameter mfn from populate_pt_range
  2018-03-05 13:43             ` Julien Grall
@ 2018-03-05 14:00               ` Jan Beulich
  2018-03-05 14:11                 ` Julien Grall
  0 siblings, 1 reply; 76+ messages in thread
From: Jan Beulich @ 2018-03-05 14:00 UTC (permalink / raw)
  To: Julien Grall
  Cc: Tim Deegan, Stefano Stabellini, Wei Liu, George Dunlap,
	Andrew Cooper, Ian Jackson, xen-devel

>>> On 05.03.18 at 14:43, <julien.grall@arm.com> wrote:
> On 02/03/18 14:55, Jan Beulich wrote:
>>>>> On 22.02.18 at 17:55, <julien.grall@arm.com> wrote:
>>> On 22/02/18 16:51, Wei Liu wrote:
>>>> On Thu, Feb 22, 2018 at 04:40:04PM +0000, Julien Grall wrote:
>>>>> On 22/02/18 16:35, Wei Liu wrote:
>>>>>> On Wed, Feb 21, 2018 at 02:02:51PM +0000, Julien Grall wrote:
>>>>>>> The function populate_pt_range is used to populate in advance the
>>>>>>> page-table but it will not do the actual mapping. So passing the MFN in
>>>>>>> parameter is pointless. Note that the only caller pass 0...
>>>>>>>
>>>>>>> At the same time replace 0 by INVALID_MFN to make clear the MFN is
>>>>>>> invalid.
>>>>>>>
>>>>>>
>>>>>> The mfn parameter is the first mfn of a consecutive nr MFNs passed to
>>>>>> map_pages_to_xen. Putting INVALID_MFN isn't helping -- the value written
>>>>>> to page table(s) will wrap around to 0.
>>>>>>
>>>>>> And I think starting from 0 to avoid overflow is probably a better
>>>>>> behaviour. If you really want to make sure all entries are filled with
>>>>>> INVALID_MFN you should call map_pages_to_xen for nr times with each
>>>>>> page.
>>>>>
>>>>> I am not sure to understand this. From its name, populate_pt_range should
>>>>> only create the intermediate tables. The leaf entry will stay invalid. So
>>>>> how the value of mfn matters? Is it because the code is written in a such
>>>>> way that passing INVALID_MFN will result to undefined behavior?
>>>>
>>>> Right, that's what I meant. It doesn't matter whether you use 0 or
>>>> INVALID_MFN.
>>>>
>>>> Unsigned integer overflow is not UB in C, so passing INVALID_MFN is
>>>> safe.
>>>>
>>>> But your intention seemed to be filling all entries with INVALID_MFN to
>>>> aid debugging, so the function doesn't do what I think you wanted it to
>>>> do. It could be I misunderstood your intention.
>>>
>>> That was not my intention. I replaced 0 by INVALID_MFN because from the
>>> name you know the MFN is invalid. 0 could potentially be valid (at least
>>> on Arm) and make the code confusing to understand.
>>>
>>> I can make it clearer in the commit message.
>> 
>> I don't think that'll be much better; I agree with Wei that you
>> don't want the wrapping behavior here. What you want to do
>> is skip the increments in x86's map_pages_to_xen() when
>> mfn is INVALID_MFN. Granted this should have been done
>> before (so that there wouldn't have been incrementing from
>> zero), but as you say MFN 0 isn't fundamentally invalid (albeit
>> on x86 we almost make it invalid).
>> 
>> As to your earlier argument - please don't forget that on x86
>> the function still fills all leaf entries in the range, just that they
>> all will be non-present ones.
> 
> I still don't understand why it matters. The entry is not present so the 
> address is going to ignore. 0 or MFN_INVALID are just dummy value that 
> are going to be replaced on the entry is made present.
> 
> Furthermore, as Wei pointed out unsigned integer overflow is not UB in 
> C, so passing INVALID_MFN is safe.

I didn't say it's unsafe. It's clumsy and misleading.

> Anyway, I don't have much knowledge on the x86 to make the modification 
> that you suggested. So I am going to revert to _mfn(0) for x86.

I'd prefer if you didn't, but well, it'll be one of us to clean it up
then.

Jan


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 10/16] xen/mm: Switch map_pages_to_xen to use MFN typesafe
  2018-03-02 15:06   ` Jan Beulich
  2018-03-02 15:08     ` Jan Beulich
@ 2018-03-05 14:07     ` Julien Grall
  2018-03-05 14:39       ` Jan Beulich
  1 sibling, 1 reply; 76+ messages in thread
From: Julien Grall @ 2018-03-05 14:07 UTC (permalink / raw)
  To: Jan Beulich
  Cc: Tim Deegan, Kevin Tian, Stefano Stabellini, Wei Liu,
	George Dunlap, Andrew Cooper, Ian Jackson, xen-devel, Shane Wang,
	Gang Wei

Hi,

On 02/03/18 15:06, Jan Beulich wrote:
>>>> On 21.02.18 at 15:02, <julien.grall@arm.com> wrote:
>> --- a/xen/arch/x86/x86_64/mm.c
>> +++ b/xen/arch/x86/x86_64/mm.c
>> @@ -40,6 +40,10 @@ asm(".file \"" __FILE__ "\"");
>>   #include <asm/mem_sharing.h>
>>   #include <public/memory.h>
>>   
>> +/* Override macros from asm/page.h to make them work with mfn_t */
>> +#undef page_to_mfn
>> +#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
> 
> I can't spot where this is needed in this file.
> 
>> @@ -234,7 +238,7 @@ void vunmap(const void *va)
>>   #ifndef _PAGE_NONE
>>       destroy_xen_mappings(addr, addr + PAGE_SIZE * pages);
>>   #else /* Avoid tearing down intermediate page tables. */
>> -    map_pages_to_xen(addr, 0, pages, _PAGE_NONE);
>> +    map_pages_to_xen(addr, _mfn(0), pages, _PAGE_NONE);
> 
> INVALID_MFN?

I can but then we end up to the same clumsiness as you mention in #8. So 
what's your preference?

> 
> Also please again see about using PFN_DOWN() for some of the
> sizes passed to the function as you touch those places anyway.

Will do.

> 
> Jan
> 

-- 
Julien Grall

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 08/16] xen/mm: Drop the parameter mfn from populate_pt_range
  2018-03-05 14:00               ` Jan Beulich
@ 2018-03-05 14:11                 ` Julien Grall
  2018-03-05 14:38                   ` Jan Beulich
  0 siblings, 1 reply; 76+ messages in thread
From: Julien Grall @ 2018-03-05 14:11 UTC (permalink / raw)
  To: Jan Beulich
  Cc: Tim Deegan, Stefano Stabellini, Wei Liu, George Dunlap,
	Andrew Cooper, Ian Jackson, xen-devel

Hi Jan,

On 05/03/18 14:00, Jan Beulich wrote:
>>>> On 05.03.18 at 14:43, <julien.grall@arm.com> wrote:
>> On 02/03/18 14:55, Jan Beulich wrote:
>>>>>> On 22.02.18 at 17:55, <julien.grall@arm.com> wrote:
>>>> On 22/02/18 16:51, Wei Liu wrote:
>>>>> On Thu, Feb 22, 2018 at 04:40:04PM +0000, Julien Grall wrote:
>>>>>> On 22/02/18 16:35, Wei Liu wrote:
>>>>>>> On Wed, Feb 21, 2018 at 02:02:51PM +0000, Julien Grall wrote:
>>>>>>>> The function populate_pt_range is used to populate in advance the
>>>>>>>> page-table but it will not do the actual mapping. So passing the MFN in
>>>>>>>> parameter is pointless. Note that the only caller pass 0...
>>>>>>>>
>>>>>>>> At the same time replace 0 by INVALID_MFN to make clear the MFN is
>>>>>>>> invalid.
>>>>>>>>
>>>>>>>
>>>>>>> The mfn parameter is the first mfn of a consecutive nr MFNs passed to
>>>>>>> map_pages_to_xen. Putting INVALID_MFN isn't helping -- the value written
>>>>>>> to page table(s) will wrap around to 0.
>>>>>>>
>>>>>>> And I think starting from 0 to avoid overflow is probably a better
>>>>>>> behaviour. If you really want to make sure all entries are filled with
>>>>>>> INVALID_MFN you should call map_pages_to_xen for nr times with each
>>>>>>> page.
>>>>>>
>>>>>> I am not sure to understand this. From its name, populate_pt_range should
>>>>>> only create the intermediate tables. The leaf entry will stay invalid. So
>>>>>> how the value of mfn matters? Is it because the code is written in a such
>>>>>> way that passing INVALID_MFN will result to undefined behavior?
>>>>>
>>>>> Right, that's what I meant. It doesn't matter whether you use 0 or
>>>>> INVALID_MFN.
>>>>>
>>>>> Unsigned integer overflow is not UB in C, so passing INVALID_MFN is
>>>>> safe.
>>>>>
>>>>> But your intention seemed to be filling all entries with INVALID_MFN to
>>>>> aid debugging, so the function doesn't do what I think you wanted it to
>>>>> do. It could be I misunderstood your intention.
>>>>
>>>> That was not my intention. I replaced 0 by INVALID_MFN because from the
>>>> name you know the MFN is invalid. 0 could potentially be valid (at least
>>>> on Arm) and make the code confusing to understand.
>>>>
>>>> I can make it clearer in the commit message.
>>>
>>> I don't think that'll be much better; I agree with Wei that you
>>> don't want the wrapping behavior here. What you want to do
>>> is skip the increments in x86's map_pages_to_xen() when
>>> mfn is INVALID_MFN. Granted this should have been done
>>> before (so that there wouldn't have been incrementing from
>>> zero), but as you say MFN 0 isn't fundamentally invalid (albeit
>>> on x86 we almost make it invalid).
>>>
>>> As to your earlier argument - please don't forget that on x86
>>> the function still fills all leaf entries in the range, just that they
>>> all will be non-present ones.
>>
>> I still don't understand why it matters. The entry is not present so the
>> address is going to ignore. 0 or MFN_INVALID are just dummy value that
>> are going to be replaced on the entry is made present.
>>
>> Furthermore, as Wei pointed out unsigned integer overflow is not UB in
>> C, so passing INVALID_MFN is safe.
> 
> I didn't say it's unsafe. It's clumsy and misleading.

Well, that's just the way the page-table code has been written on x86. 
On Arm, the code is much clearer with INVALID_MFN than _mfn(0).

> 
>> Anyway, I don't have much knowledge on the x86 to make the modification
>> that you suggested. So I am going to revert to _mfn(0) for x86.
> 
> I'd prefer if you didn't, but well, it'll be one of us to clean it up
> then.
I can keep as INVALID_MFN. But then either you or Andrew (or anyone x86 
folks) would have to provide the patch to skip incrementing invalid MFN 
(if I understood correctly your request).

Cheers,

-- 
Julien Grall

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 12/16] xen/mm: Switch common/memory.c to use typesafe MFN
  2018-03-02 15:34   ` Jan Beulich
@ 2018-03-05 14:18     ` Julien Grall
  2018-03-05 14:41       ` Jan Beulich
  0 siblings, 1 reply; 76+ messages in thread
From: Julien Grall @ 2018-03-05 14:18 UTC (permalink / raw)
  To: Jan Beulich
  Cc: Tim Deegan, Stefano Stabellini, Wei Liu, George Dunlap,
	Andrew Cooper, Ian Jackson, xen-devel

Hi Jan,

On 02/03/18 15:34, Jan Beulich wrote:
>>>> On 21.02.18 at 15:02, <julien.grall@arm.com> wrote:
>> @@ -95,11 +101,18 @@ static unsigned int max_order(const struct domain *d)
>>       return min(order, MAX_ORDER + 0U);
>>   }
>>   
>> +/* Helper to copy a typesafe MFN to guest */
>> +#define copy_mfn_to_guest(hnd, off, mfn)            \
>> +    ({                                              \
>> +        xen_pfn_t mfn_ = mfn_x(mfn);                \
>> +        __copy_to_guest_offset(hnd, off, &mfn_, 1); \
>> +    })
> 
> Hmm, not really nice, but what do you do.

I am open to better suggestion. I wanted to avoid the conversion all 
over the code.

Also, do you have an opinion on Wei's suggestion:

"What I meant was to make copy_{to,from}_guest* type-safe. I just feel it
a bit strange you only created a wrapper for this file. I wonder why.

Note I'm just asking question. That's not necessarily a good idea to
turn them all in the end."

> 
>>   static void increase_reservation(struct memop_args *a)
>>   {
>>       struct page_info *page;
>>       unsigned long i;
>> -    xen_pfn_t mfn;
>> +    mfn_t mfn;
> 
> Please move this declaration ...
> 
>> @@ -133,7 +146,7 @@ static void increase_reservation(struct memop_args *a)
>>                !guest_handle_is_null(a->extent_list) )
>>           {
>>               mfn = page_to_mfn(page);
> 
> ... here, making the assignment its initializer. Or even avoid the
> local variable altogether, as the macro has already got one. Same
> elsewhere (whichever of the two variants fits), albeit maybe in the
> other cases the scope can't be shrunk much.

I will have a look.

Cheers,

-- 
Julien Grall

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 08/16] xen/mm: Drop the parameter mfn from populate_pt_range
  2018-03-05 14:11                 ` Julien Grall
@ 2018-03-05 14:38                   ` Jan Beulich
  2018-03-09 17:29                     ` Wei Liu
  0 siblings, 1 reply; 76+ messages in thread
From: Jan Beulich @ 2018-03-05 14:38 UTC (permalink / raw)
  To: Julien Grall
  Cc: Tim Deegan, Stefano Stabellini, Wei Liu, George Dunlap,
	Andrew Cooper, Ian Jackson, xen-devel

>>> On 05.03.18 at 15:11, <julien.grall@arm.com> wrote:
> On 05/03/18 14:00, Jan Beulich wrote:
>>>>> On 05.03.18 at 14:43, <julien.grall@arm.com> wrote:
>>> Anyway, I don't have much knowledge on the x86 to make the modification
>>> that you suggested. So I am going to revert to _mfn(0) for x86.
>> 
>> I'd prefer if you didn't, but well, it'll be one of us to clean it up
>> then.
> I can keep as INVALID_MFN. But then either you or Andrew (or anyone x86 
> folks) would have to provide the patch to skip incrementing invalid MFN 
> (if I understood correctly your request).

Sigh - this should go together imo. While wrongly incrementing from
zero was bad, wrongly wrapping from INVALID_MFN makes things
worse.

Jan


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 10/16] xen/mm: Switch map_pages_to_xen to use MFN typesafe
  2018-03-05 14:07     ` Julien Grall
@ 2018-03-05 14:39       ` Jan Beulich
  2018-03-05 14:44         ` Julien Grall
  0 siblings, 1 reply; 76+ messages in thread
From: Jan Beulich @ 2018-03-05 14:39 UTC (permalink / raw)
  To: Julien Grall
  Cc: Tim Deegan, Kevin Tian, Stefano Stabellini, Wei Liu,
	George Dunlap, Andrew Cooper, Ian Jackson, xen-devel, Shane Wang,
	Gang Wei

>>> On 05.03.18 at 15:07, <julien.grall@arm.com> wrote:
> On 02/03/18 15:06, Jan Beulich wrote:
>>>>> On 21.02.18 at 15:02, <julien.grall@arm.com> wrote:
>>> --- a/xen/arch/x86/x86_64/mm.c
>>> +++ b/xen/arch/x86/x86_64/mm.c
>>> @@ -40,6 +40,10 @@ asm(".file \"" __FILE__ "\"");
>>>   #include <asm/mem_sharing.h>
>>>   #include <public/memory.h>
>>>   
>>> +/* Override macros from asm/page.h to make them work with mfn_t */
>>> +#undef page_to_mfn
>>> +#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
>> 
>> I can't spot where this is needed in this file.
>> 
>>> @@ -234,7 +238,7 @@ void vunmap(const void *va)
>>>   #ifndef _PAGE_NONE
>>>       destroy_xen_mappings(addr, addr + PAGE_SIZE * pages);
>>>   #else /* Avoid tearing down intermediate page tables. */
>>> -    map_pages_to_xen(addr, 0, pages, _PAGE_NONE);
>>> +    map_pages_to_xen(addr, _mfn(0), pages, _PAGE_NONE);
>> 
>> INVALID_MFN?
> 
> I can but then we end up to the same clumsiness as you mention in #8. So 
> what's your preference?

My preference is to skip the increments for INVALID_MFN in patch 8.
Since you don't want to do that, I guess we'll have to live with zero
being used everywhere.

Jan


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 12/16] xen/mm: Switch common/memory.c to use typesafe MFN
  2018-03-05 14:18     ` Julien Grall
@ 2018-03-05 14:41       ` Jan Beulich
  2018-03-09 17:33         ` Wei Liu
  0 siblings, 1 reply; 76+ messages in thread
From: Jan Beulich @ 2018-03-05 14:41 UTC (permalink / raw)
  To: Julien Grall
  Cc: Tim Deegan, Stefano Stabellini, Wei Liu, George Dunlap,
	Andrew Cooper, Ian Jackson, xen-devel

>>> On 05.03.18 at 15:18, <julien.grall@arm.com> wrote:
> On 02/03/18 15:34, Jan Beulich wrote:
>>>>> On 21.02.18 at 15:02, <julien.grall@arm.com> wrote:
>>> @@ -95,11 +101,18 @@ static unsigned int max_order(const struct domain *d)
>>>       return min(order, MAX_ORDER + 0U);
>>>   }
>>>   
>>> +/* Helper to copy a typesafe MFN to guest */
>>> +#define copy_mfn_to_guest(hnd, off, mfn)            \
>>> +    ({                                              \
>>> +        xen_pfn_t mfn_ = mfn_x(mfn);                \
>>> +        __copy_to_guest_offset(hnd, off, &mfn_, 1); \
>>> +    })
>> 
>> Hmm, not really nice, but what do you do.
> 
> I am open to better suggestion. I wanted to avoid the conversion all 
> over the code.

I have no better suggestion, I'm sorry, hence the "but what do
you do."

> Also, do you have an opinion on Wei's suggestion:
> 
> "What I meant was to make copy_{to,from}_guest* type-safe. I just feel it
> a bit strange you only created a wrapper for this file. I wonder why.
> 
> Note I'm just asking question. That's not necessarily a good idea to
> turn them all in the end."

Well, I didn't really understand what he's after (in the context of
this series) - copy_{to,from}_guest() don't take or return MFNs or
GFNs.

Jan


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 10/16] xen/mm: Switch map_pages_to_xen to use MFN typesafe
  2018-03-05 14:39       ` Jan Beulich
@ 2018-03-05 14:44         ` Julien Grall
  0 siblings, 0 replies; 76+ messages in thread
From: Julien Grall @ 2018-03-05 14:44 UTC (permalink / raw)
  To: Jan Beulich
  Cc: Tim Deegan, Kevin Tian, Stefano Stabellini, Wei Liu,
	George Dunlap, Andrew Cooper, Ian Jackson, xen-devel, Shane Wang,
	Gang Wei

Hi,

On 05/03/18 14:39, Jan Beulich wrote:
>>>> On 05.03.18 at 15:07, <julien.grall@arm.com> wrote:
>> On 02/03/18 15:06, Jan Beulich wrote:
>>>>>> On 21.02.18 at 15:02, <julien.grall@arm.com> wrote:
>>>> --- a/xen/arch/x86/x86_64/mm.c
>>>> +++ b/xen/arch/x86/x86_64/mm.c
>>>> @@ -40,6 +40,10 @@ asm(".file \"" __FILE__ "\"");
>>>>    #include <asm/mem_sharing.h>
>>>>    #include <public/memory.h>
>>>>    
>>>> +/* Override macros from asm/page.h to make them work with mfn_t */
>>>> +#undef page_to_mfn
>>>> +#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
>>>
>>> I can't spot where this is needed in this file.
>>>
>>>> @@ -234,7 +238,7 @@ void vunmap(const void *va)
>>>>    #ifndef _PAGE_NONE
>>>>        destroy_xen_mappings(addr, addr + PAGE_SIZE * pages);
>>>>    #else /* Avoid tearing down intermediate page tables. */
>>>> -    map_pages_to_xen(addr, 0, pages, _PAGE_NONE);
>>>> +    map_pages_to_xen(addr, _mfn(0), pages, _PAGE_NONE);
>>>
>>> INVALID_MFN?
>>
>> I can but then we end up to the same clumsiness as you mention in #8. So
>> what's your preference?
> 
> My preference is to skip the increments for INVALID_MFN in patch 8.
> Since you don't want to do that, I guess we'll have to live with zero
> being used everywhere.

It is not that I don't want. I don't have any x86 setup and not that 
confident to provide a change bigger than switch from plain unsigned int 
to typesafe and light clean-up.

I can provide a patch if you really want. But likely it is going to be 
quicker for you to write it.

Cheers,

-- 
Julien Grall

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 08/16] xen/mm: Drop the parameter mfn from populate_pt_range
  2018-03-05 14:38                   ` Jan Beulich
@ 2018-03-09 17:29                     ` Wei Liu
  2018-03-11 19:30                       ` Julien Grall
  2018-03-12  6:36                       ` Jan Beulich
  0 siblings, 2 replies; 76+ messages in thread
From: Wei Liu @ 2018-03-09 17:29 UTC (permalink / raw)
  To: Jan Beulich
  Cc: Tim Deegan, Stefano Stabellini, Wei Liu, George Dunlap,
	Andrew Cooper, Ian Jackson, xen-devel, Julien Grall

On Mon, Mar 05, 2018 at 07:38:36AM -0700, Jan Beulich wrote:
> >>> On 05.03.18 at 15:11, <julien.grall@arm.com> wrote:
> > On 05/03/18 14:00, Jan Beulich wrote:
> >>>>> On 05.03.18 at 14:43, <julien.grall@arm.com> wrote:
> >>> Anyway, I don't have much knowledge on the x86 to make the modification
> >>> that you suggested. So I am going to revert to _mfn(0) for x86.
> >> 
> >> I'd prefer if you didn't, but well, it'll be one of us to clean it up
> >> then.
> > I can keep as INVALID_MFN. But then either you or Andrew (or anyone x86 
> > folks) would have to provide the patch to skip incrementing invalid MFN 
> > (if I understood correctly your request).
> 
> Sigh - this should go together imo. While wrongly incrementing from
> zero was bad, wrongly wrapping from INVALID_MFN makes things
> worse.
> 

Try this patch?

---8<---
From 8f0024c690c736d17adde0fa765cbbf6fa2846dc Mon Sep 17 00:00:00 2001
From: Wei Liu <wei.liu2@citrix.com>
Date: Fri, 9 Mar 2018 17:20:14 +0000
Subject: [PATCH] x86/mm: skip incrementing mfn if it is not a valid mfn

The function is called to fill in page table entries in
populate_pt_range. Skip incrementing mfn if it is invalid.

Signed-off-by: Wei Liu <wei.liu2@citrix.com>
---
 xen/arch/x86/mm.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c
index 9b559448a7..5f5577c7c2 100644
--- a/xen/arch/x86/mm.c
+++ b/xen/arch/x86/mm.c
@@ -4731,7 +4731,8 @@ int map_pages_to_xen(
             }
 
             virt    += 1UL << L3_PAGETABLE_SHIFT;
-            mfn     += 1UL << (L3_PAGETABLE_SHIFT - PAGE_SHIFT);
+            if ( !mfn_eq(_mfn(mfn), INVALID_MFN) )
+                mfn += 1UL << (L3_PAGETABLE_SHIFT - PAGE_SHIFT);
             nr_mfns -= 1UL << (L3_PAGETABLE_SHIFT - PAGE_SHIFT);
             continue;
         }
@@ -4756,7 +4757,8 @@ int map_pages_to_xen(
                 if ( i > nr_mfns )
                     i = nr_mfns;
                 virt    += i << PAGE_SHIFT;
-                mfn     += i;
+                if ( !mfn_eq(_mfn(mfn), INVALID_MFN) )
+                    mfn += i;
                 nr_mfns -= i;
                 continue;
             }
@@ -4824,7 +4826,8 @@ int map_pages_to_xen(
             }
 
             virt    += 1UL << L2_PAGETABLE_SHIFT;
-            mfn     += 1UL << PAGETABLE_ORDER;
+            if ( !mfn_eq(_mfn(mfn), INVALID_MFN) )
+                mfn += 1UL << PAGETABLE_ORDER;
             nr_mfns -= 1UL << PAGETABLE_ORDER;
         }
         else
@@ -4853,7 +4856,8 @@ int map_pages_to_xen(
                     if ( i > nr_mfns )
                         i = nr_mfns;
                     virt    += i << L1_PAGETABLE_SHIFT;
-                    mfn     += i;
+                    if ( !mfn_eq(_mfn(mfn), INVALID_MFN) )
+                        mfn += i;
                     nr_mfns -= i;
                     goto check_l3;
                 }
@@ -4898,7 +4902,8 @@ int map_pages_to_xen(
             }
 
             virt    += 1UL << L1_PAGETABLE_SHIFT;
-            mfn     += 1UL;
+            if ( !mfn_eq(_mfn(mfn), INVALID_MFN) )
+                mfn += 1UL;
             nr_mfns -= 1UL;
 
             if ( (flags == PAGE_HYPERVISOR) &&
-- 
2.11.0


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply related	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 12/16] xen/mm: Switch common/memory.c to use typesafe MFN
  2018-03-05 14:41       ` Jan Beulich
@ 2018-03-09 17:33         ` Wei Liu
  2018-03-11 19:44           ` Julien Grall
  0 siblings, 1 reply; 76+ messages in thread
From: Wei Liu @ 2018-03-09 17:33 UTC (permalink / raw)
  To: Jan Beulich
  Cc: Tim Deegan, Stefano Stabellini, Wei Liu, George Dunlap,
	Andrew Cooper, Ian Jackson, xen-devel, Julien Grall

On Mon, Mar 05, 2018 at 07:41:54AM -0700, Jan Beulich wrote:
> >>> On 05.03.18 at 15:18, <julien.grall@arm.com> wrote:
> > On 02/03/18 15:34, Jan Beulich wrote:
> >>>>> On 21.02.18 at 15:02, <julien.grall@arm.com> wrote:
> >>> @@ -95,11 +101,18 @@ static unsigned int max_order(const struct domain *d)
> >>>       return min(order, MAX_ORDER + 0U);
> >>>   }
> >>>   
> >>> +/* Helper to copy a typesafe MFN to guest */
> >>> +#define copy_mfn_to_guest(hnd, off, mfn)            \
> >>> +    ({                                              \
> >>> +        xen_pfn_t mfn_ = mfn_x(mfn);                \
> >>> +        __copy_to_guest_offset(hnd, off, &mfn_, 1); \
> >>> +    })
> >> 
> >> Hmm, not really nice, but what do you do.
> > 
> > I am open to better suggestion. I wanted to avoid the conversion all 
> > over the code.
> 
> I have no better suggestion, I'm sorry, hence the "but what do
> you do."
> 
> > Also, do you have an opinion on Wei's suggestion:
> > 
> > "What I meant was to make copy_{to,from}_guest* type-safe. I just feel it
> > a bit strange you only created a wrapper for this file. I wonder why.
> > 
> > Note I'm just asking question. That's not necessarily a good idea to
> > turn them all in the end."
> 
> Well, I didn't really understand what he's after (in the context of
> this series) - copy_{to,from}_guest() don't take or return MFNs or
> GFNs.
> 

Fundamentally Julien's patch is to wrap around an existing API for this
one file only. Why is this file special? Why not just make that class of
APIs do what he wants?

But that is going to be intrusive and a bit counter-intuitive.

(In the spirit of unblocking things, I won't insist making such change.)

Wei.

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 08/16] xen/mm: Drop the parameter mfn from populate_pt_range
  2018-03-09 17:29                     ` Wei Liu
@ 2018-03-11 19:30                       ` Julien Grall
  2018-03-12  6:36                       ` Jan Beulich
  1 sibling, 0 replies; 76+ messages in thread
From: Julien Grall @ 2018-03-11 19:30 UTC (permalink / raw)
  To: Wei Liu, Jan Beulich
  Cc: Stefano Stabellini, George Dunlap, Andrew Cooper, Tim Deegan,
	xen-devel, Ian Jackson

Hi Wei,

On 03/09/2018 05:29 PM, Wei Liu wrote:
> On Mon, Mar 05, 2018 at 07:38:36AM -0700, Jan Beulich wrote:
>>>>> On 05.03.18 at 15:11, <julien.grall@arm.com> wrote:
>>> On 05/03/18 14:00, Jan Beulich wrote:
>>>>>>> On 05.03.18 at 14:43, <julien.grall@arm.com> wrote:
>>>>> Anyway, I don't have much knowledge on the x86 to make the modification
>>>>> that you suggested. So I am going to revert to _mfn(0) for x86.
>>>>
>>>> I'd prefer if you didn't, but well, it'll be one of us to clean it up
>>>> then.
>>> I can keep as INVALID_MFN. But then either you or Andrew (or anyone x86
>>> folks) would have to provide the patch to skip incrementing invalid MFN
>>> (if I understood correctly your request).
>>
>> Sigh - this should go together imo. While wrongly incrementing from
>> zero was bad, wrongly wrapping from INVALID_MFN makes things
>> worse.
>>
> 
> Try this patch?

I am happy to carry this patch at the beginning of my series if you want.

> 
> ---8<---
>  From 8f0024c690c736d17adde0fa765cbbf6fa2846dc Mon Sep 17 00:00:00 2001
> From: Wei Liu <wei.liu2@citrix.com>
> Date: Fri, 9 Mar 2018 17:20:14 +0000
> Subject: [PATCH] x86/mm: skip incrementing mfn if it is not a valid mfn
> 
> The function is called to fill in page table entries in
> populate_pt_range. Skip incrementing mfn if it is invalid.
> 
> Signed-off-by: Wei Liu <wei.liu2@citrix.com>
> ---
>   xen/arch/x86/mm.c | 15 ++++++++++-----
>   1 file changed, 10 insertions(+), 5 deletions(-)
> 
> diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c
> index 9b559448a7..5f5577c7c2 100644
> --- a/xen/arch/x86/mm.c
> +++ b/xen/arch/x86/mm.c
> @@ -4731,7 +4731,8 @@ int map_pages_to_xen(
>               }
>   
>               virt    += 1UL << L3_PAGETABLE_SHIFT;
> -            mfn     += 1UL << (L3_PAGETABLE_SHIFT - PAGE_SHIFT);
> +            if ( !mfn_eq(_mfn(mfn), INVALID_MFN) )
> +                mfn += 1UL << (L3_PAGETABLE_SHIFT - PAGE_SHIFT);
>               nr_mfns -= 1UL << (L3_PAGETABLE_SHIFT - PAGE_SHIFT);
>               continue;
>           }
> @@ -4756,7 +4757,8 @@ int map_pages_to_xen(
>                   if ( i > nr_mfns )
>                       i = nr_mfns;
>                   virt    += i << PAGE_SHIFT;
> -                mfn     += i;
> +                if ( !mfn_eq(_mfn(mfn), INVALID_MFN) )
> +                    mfn += i;
>                   nr_mfns -= i;
>                   continue;
>               }
> @@ -4824,7 +4826,8 @@ int map_pages_to_xen(
>               }
>   
>               virt    += 1UL << L2_PAGETABLE_SHIFT;
> -            mfn     += 1UL << PAGETABLE_ORDER;
> +            if ( !mfn_eq(_mfn(mfn), INVALID_MFN) )
> +                mfn += 1UL << PAGETABLE_ORDER;
>               nr_mfns -= 1UL << PAGETABLE_ORDER;
>           }
>           else
> @@ -4853,7 +4856,8 @@ int map_pages_to_xen(
>                       if ( i > nr_mfns )
>                           i = nr_mfns;
>                       virt    += i << L1_PAGETABLE_SHIFT;
> -                    mfn     += i;
> +                    if ( !mfn_eq(_mfn(mfn), INVALID_MFN) )
> +                        mfn += i;
>                       nr_mfns -= i;
>                       goto check_l3;
>                   }
> @@ -4898,7 +4902,8 @@ int map_pages_to_xen(
>               }
>   
>               virt    += 1UL << L1_PAGETABLE_SHIFT;
> -            mfn     += 1UL;
> +            if ( !mfn_eq(_mfn(mfn), INVALID_MFN) )
> +                mfn += 1UL;
>               nr_mfns -= 1UL;
>   
>               if ( (flags == PAGE_HYPERVISOR) &&
> 

Cheers,

-- 
Julien Grall

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 12/16] xen/mm: Switch common/memory.c to use typesafe MFN
  2018-03-09 17:33         ` Wei Liu
@ 2018-03-11 19:44           ` Julien Grall
  2018-03-12  6:39             ` Jan Beulich
  0 siblings, 1 reply; 76+ messages in thread
From: Julien Grall @ 2018-03-11 19:44 UTC (permalink / raw)
  To: Wei Liu, Jan Beulich
  Cc: Tim Deegan, Stefano Stabellini, George Dunlap, Andrew Cooper,
	Ian Jackson, xen-devel

Hi,

On 03/09/2018 05:33 PM, Wei Liu wrote:
> On Mon, Mar 05, 2018 at 07:41:54AM -0700, Jan Beulich wrote:
>>>>> On 05.03.18 at 15:18, <julien.grall@arm.com> wrote:
>>> On 02/03/18 15:34, Jan Beulich wrote:
>>>>>>> On 21.02.18 at 15:02, <julien.grall@arm.com> wrote:
>>>>> @@ -95,11 +101,18 @@ static unsigned int max_order(const struct domain *d)
>>>>>        return min(order, MAX_ORDER + 0U);
>>>>>    }
>>>>>    
>>>>> +/* Helper to copy a typesafe MFN to guest */
>>>>> +#define copy_mfn_to_guest(hnd, off, mfn)            \
>>>>> +    ({                                              \
>>>>> +        xen_pfn_t mfn_ = mfn_x(mfn);                \
>>>>> +        __copy_to_guest_offset(hnd, off, &mfn_, 1); \
>>>>> +    })
>>>>
>>>> Hmm, not really nice, but what do you do.
>>>
>>> I am open to better suggestion. I wanted to avoid the conversion all
>>> over the code.
>>
>> I have no better suggestion, I'm sorry, hence the "but what do
>> you do."
>>
>>> Also, do you have an opinion on Wei's suggestion:
>>>
>>> "What I meant was to make copy_{to,from}_guest* type-safe. I just feel it
>>> a bit strange you only created a wrapper for this file. I wonder why.
>>>
>>> Note I'm just asking question. That's not necessarily a good idea to
>>> turn them all in the end."
>>
>> Well, I didn't really understand what he's after (in the context of
>> this series) - copy_{to,from}_guest() don't take or return MFNs or
>> GFNs.
>>
> 
> Fundamentally Julien's patch is to wrap around an existing API for this
> one file only. Why is this file special? Why not just make that class of
> APIs do what he wants?
> 
> But that is going to be intrusive and a bit counter-intuitive.

I have quickly looked at it. The major problem I can see is it is not 
possible to generically define for any typesafe. Indeed, TYPE_SAFE(...) 
cannot define new macro and, AFAICT, it is not feasible to define static 
inline for copy_* helpers.

So we would need to introduce macros for each typesafe by hand. I can 
move copy_mfn_to_guest in xen/mm.h if people think it could be useful.

Cheers,

-- 
Julien Grall

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 08/16] xen/mm: Drop the parameter mfn from populate_pt_range
  2018-03-09 17:29                     ` Wei Liu
  2018-03-11 19:30                       ` Julien Grall
@ 2018-03-12  6:36                       ` Jan Beulich
  2018-03-14 15:22                         ` Julien Grall
  1 sibling, 1 reply; 76+ messages in thread
From: Jan Beulich @ 2018-03-12  6:36 UTC (permalink / raw)
  To: julien.grall, wei.liu2
  Cc: sstabellini, George.Dunlap, andrew.cooper3, tim, xen-devel, ian.jackson

>>> Wei Liu <wei.liu2@citrix.com> 03/09/18 6:30 PM >>>
>On Mon, Mar 05, 2018 at 07:38:36AM -0700, Jan Beulich wrote:
>> >>> On 05.03.18 at 15:11, <julien.grall@arm.com> wrote:
>> > On 05/03/18 14:00, Jan Beulich wrote:
>> >>>>> On 05.03.18 at 14:43, <julien.grall@arm.com> wrote:
>> >>> Anyway, I don't have much knowledge on the x86 to make the modification
>> >>> that you suggested. So I am going to revert to _mfn(0) for x86.
>> >> 
>> >> I'd prefer if you didn't, but well, it'll be one of us to clean it up
>> >> then.
>> > I can keep as INVALID_MFN. But then either you or Andrew (or anyone x86 
>> > folks) would have to provide the patch to skip incrementing invalid MFN 
>> > (if I understood correctly your request).
>> 
>> Sigh - this should go together imo. While wrongly incrementing from
>> zero was bad, wrongly wrapping from INVALID_MFN makes things
>> worse.
>
>Try this patch?

Looks fine; Julien, do you want to fold this in?

Jan


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 12/16] xen/mm: Switch common/memory.c to use typesafe MFN
  2018-03-11 19:44           ` Julien Grall
@ 2018-03-12  6:39             ` Jan Beulich
  2018-03-14 16:08               ` Julien Grall
  0 siblings, 1 reply; 76+ messages in thread
From: Jan Beulich @ 2018-03-12  6:39 UTC (permalink / raw)
  To: julien.grall, wei.liu2
  Cc: tim, sstabellini, George.Dunlap, andrew.cooper3, ian.jackson, xen-devel

>>> Julien Grall <julien.grall@arm.com> 03/11/18 8:44 PM >>>
>On 03/09/2018 05:33 PM, Wei Liu wrote:
>> On Mon, Mar 05, 2018 at 07:41:54AM -0700, Jan Beulich wrote:
>>>>>> On 05.03.18 at 15:18, <julien.grall@arm.com> wrote:
>>>> Also, do you have an opinion on Wei's suggestion:
>>>>
>>>> "What I meant was to make copy_{to,from}_guest* type-safe. I just feel it
>>>> a bit strange you only created a wrapper for this file. I wonder why.
>>>>
>>>> Note I'm just asking question. That's not necessarily a good idea to
>>>> turn them all in the end."
>>>
>>> Well, I didn't really understand what he's after (in the context of
>>> this series) - copy_{to,from}_guest() don't take or return MFNs or
>>> GFNs.
>>>
>> 
>> Fundamentally Julien's patch is to wrap around an existing API for this
>> one file only. Why is this file special? Why not just make that class of
>> APIs do what he wants?
>> 
>> But that is going to be intrusive and a bit counter-intuitive.
>
>I have quickly looked at it. The major problem I can see is it is not 
>possible to generically define for any typesafe. Indeed, TYPE_SAFE(...) 
>cannot define new macro and, AFAICT, it is not feasible to define static 
>inline for copy_* helpers.
>
>So we would need to introduce macros for each typesafe by hand. I can 
>move copy_mfn_to_guest in xen/mm.h if people think it could be useful.

First of all - how often do we copy in/out individual MFNs? Not in many places,
I think. Hence I'm afraid I continue to not see the value of such a construct,
especially not as a wider-than-file-scope one.

Jan


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 08/16] xen/mm: Drop the parameter mfn from populate_pt_range
  2018-03-12  6:36                       ` Jan Beulich
@ 2018-03-14 15:22                         ` Julien Grall
  0 siblings, 0 replies; 76+ messages in thread
From: Julien Grall @ 2018-03-14 15:22 UTC (permalink / raw)
  To: Jan Beulich, wei.liu2
  Cc: sstabellini, George.Dunlap, andrew.cooper3, tim, xen-devel, ian.jackson

Hi Jan,

On 03/12/2018 06:36 AM, Jan Beulich wrote:
>>>> Wei Liu <wei.liu2@citrix.com> 03/09/18 6:30 PM >>>
>> On Mon, Mar 05, 2018 at 07:38:36AM -0700, Jan Beulich wrote:
>>>>>> On 05.03.18 at 15:11, <julien.grall@arm.com> wrote:
>>>> On 05/03/18 14:00, Jan Beulich wrote:
>>>>>>>> On 05.03.18 at 14:43, <julien.grall@arm.com> wrote:
>>>>>> Anyway, I don't have much knowledge on the x86 to make the modification
>>>>>> that you suggested. So I am going to revert to _mfn(0) for x86.
>>>>>
>>>>> I'd prefer if you didn't, but well, it'll be one of us to clean it up
>>>>> then.
>>>> I can keep as INVALID_MFN. But then either you or Andrew (or anyone x86
>>>> folks) would have to provide the patch to skip incrementing invalid MFN
>>>> (if I understood correctly your request).
>>>
>>> Sigh - this should go together imo. While wrongly incrementing from
>>> zero was bad, wrongly wrapping from INVALID_MFN makes things
>>> worse.
>>
>> Try this patch?
> 
> Looks fine; Julien, do you want to fold this in?

Rather than folding this in,  I am planning to add this patch at the 
beginning of the series.

Cheers,

> 
> Jan
> 

-- 
Julien Grall

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 12/16] xen/mm: Switch common/memory.c to use typesafe MFN
  2018-03-12  6:39             ` Jan Beulich
@ 2018-03-14 16:08               ` Julien Grall
  0 siblings, 0 replies; 76+ messages in thread
From: Julien Grall @ 2018-03-14 16:08 UTC (permalink / raw)
  To: Jan Beulich, wei.liu2
  Cc: tim, sstabellini, George.Dunlap, andrew.cooper3, ian.jackson, xen-devel

Hi Jan,

On 03/12/2018 06:39 AM, Jan Beulich wrote:
>>>> Julien Grall <julien.grall@arm.com> 03/11/18 8:44 PM >>>
>> On 03/09/2018 05:33 PM, Wei Liu wrote:
>>> On Mon, Mar 05, 2018 at 07:41:54AM -0700, Jan Beulich wrote:
>>>>>>> On 05.03.18 at 15:18, <julien.grall@arm.com> wrote:
>>>>> Also, do you have an opinion on Wei's suggestion:
>>>>>
>>>>> "What I meant was to make copy_{to,from}_guest* type-safe. I just feel it
>>>>> a bit strange you only created a wrapper for this file. I wonder why.
>>>>>
>>>>> Note I'm just asking question. That's not necessarily a good idea to
>>>>> turn them all in the end."
>>>>
>>>> Well, I didn't really understand what he's after (in the context of
>>>> this series) - copy_{to,from}_guest() don't take or return MFNs or
>>>> GFNs.
>>>>
>>>
>>> Fundamentally Julien's patch is to wrap around an existing API for this
>>> one file only. Why is this file special? Why not just make that class of
>>> APIs do what he wants?
>>>
>>> But that is going to be intrusive and a bit counter-intuitive.
>>
>> I have quickly looked at it. The major problem I can see is it is not
>> possible to generically define for any typesafe. Indeed, TYPE_SAFE(...)
>> cannot define new macro and, AFAICT, it is not feasible to define static
>> inline for copy_* helpers.
>>
>> So we would need to introduce macros for each typesafe by hand. I can
>> move copy_mfn_to_guest in xen/mm.h if people think it could be useful.
> 
> First of all - how often do we copy in/out individual MFNs? Not in many places,
> I think. Hence I'm afraid I continue to not see the value of such a construct,
> especially not as a wider-than-file-scope one.

I think you are right. Looking at the interface, we tend copy copy more 
often a GFN. We might want to provide an helper for typesafe GFN in the 
future.

Cheers.

-- 
Julien Grall

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 16/16] xen: Convert page_to_mfn and mfn_to_page to use typesafe MFN
  2018-03-02 16:08   ` Jan Beulich
@ 2018-03-14 17:02     ` Julien Grall
  2018-03-15  7:07       ` Jan Beulich
  0 siblings, 1 reply; 76+ messages in thread
From: Julien Grall @ 2018-03-14 17:02 UTC (permalink / raw)
  To: Jan Beulich
  Cc: Tim Deegan, Kevin Tian, Stefano Stabellini, Wei Liu,
	Jun Nakajima, Razvan Cojocaru, George Dunlap, Andrew Cooper,
	Ian Jackson, xen-devel, Paul Durrant, Tamas K Lengyel,
	Suravee Suthikulpanit, Shane Wang, Boris Ostrovsky, Gang Wei

Hi Jan,

On 03/02/2018 04:08 PM, Jan Beulich wrote:
>>>> On 21.02.18 at 15:02, <julien.grall@arm.com> wrote:
>> --- a/xen/arch/x86/pv/emul-priv-op.c
>> +++ b/xen/arch/x86/pv/emul-priv-op.c
>> @@ -43,16 +43,6 @@
>>   #include "emulate.h"
>>   #include "mm.h"
>>   
>> -/* Override macros from asm/page.h to make them work with mfn_t */
>> -#undef mfn_to_page
>> -#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
>> -#undef page_to_mfn
>> -#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
>> -
>> -/***********************
>> - * I/O emulation support
>> - */
> 
> Why does this comment go away?

 From an earlier review, Andrew said:

"If you're making this change, please take out the Descriptor Tables
comment like you do with I/O below, because the entire file is dedicated
to descriptor table support and it will save me one item on a cleanup
patch :)."

The descriptor one got remove by 634afe43ac "x86/pv: Rename 
invalidate_shadow_ldt() to pv_destroy_ldt()". So it is not part of this
patch anymore.

> 
>> @@ -478,10 +478,10 @@ extern paddr_t mem_hotplug;
>>   #define SHARED_M2P(_e)           ((_e) == SHARED_M2P_ENTRY)
>>   
>>   #define compat_machine_to_phys_mapping ((unsigned int
>> *)RDWR_COMPAT_MPT_VIRT_START)
>> -#define _set_gpfn_from_mfn(mfn, pfn) ({                        \
>> -    struct domain *d = page_get_owner(__mfn_to_page(mfn));     \
>> -    unsigned long entry = (d && (d == dom_cow)) ?              \
>> -        SHARED_M2P_ENTRY : (pfn);                              \
>> +#define _set_gpfn_from_mfn(mfn, pfn) ({                         \
>> +    struct domain *d = page_get_owner(mfn_to_page(_mfn(mfn)));    \
>> +    unsigned long entry = (d && (d == dom_cow)) ?               \
>> +        SHARED_M2P_ENTRY : (pfn);                               \
> 
> Please don't break the alignment of the backslashes here. It also looks
> like three of the four lines could be left alone altogether.

I am not sure why I modified the 3 other lines. I fixed it.

> 
>> @@ -157,10 +157,10 @@ static inline l4_pgentry_t l4e_from_paddr(paddr_t pa, unsigned int flags)
>>   #define l4e_from_intpte(intpte)    ((l4_pgentry_t) { (intpte_t)(intpte) })
>>   
>>   /* Construct a pte from a page pointer and access flags. */
>> -#define l1e_from_page(page, flags) l1e_from_pfn(__page_to_mfn(page), (flags))
>> -#define l2e_from_page(page, flags) l2e_from_pfn(__page_to_mfn(page), (flags))
>> -#define l3e_from_page(page, flags) l3e_from_pfn(__page_to_mfn(page), (flags))
>> -#define l4e_from_page(page, flags) l4e_from_pfn(__page_to_mfn(page), (flags))
>> +#define l1e_from_page(page, flags) l1e_from_mfn(page_to_mfn(page), (flags))
>> +#define l2e_from_page(page, flags) l2e_from_mfn(page_to_mfn(page), (flags))
>> +#define l3e_from_page(page, flags) l3e_from_mfn(page_to_mfn(page), (flags))
>> +#define l4e_from_page(page, flags) l4e_from_mfn(page_to_mfn(page), (flags))
> 
> Would again have been nice if you got rid of the extra parentheses
> here at the same time.

I admit, I don't spend my time trying to find the possible cleanup in 
the x86 code. I just do mechanical change and when I get bored I do a 
bit more.

> 
>> @@ -240,12 +240,12 @@ void copy_page_sse2(void *, const void *);
>>   #define __mfn_to_virt(mfn)  (maddr_to_virt((paddr_t)(mfn) << PAGE_SHIFT))
>>   
>>   /* Convert between machine frame numbers and page-info structures. */
>> -#define __mfn_to_page(mfn)  (frame_table + pfn_to_pdx(mfn))
>> -#define __page_to_mfn(pg)   pdx_to_pfn((unsigned long)((pg) - frame_table))
>> +#define mfn_to_page(mfn)    (frame_table + mfn_to_pdx(mfn))
>> +#define page_to_mfn(pg)     pdx_to_mfn((unsigned long)((pg) - frame_table))
>>   
>>   /* Convert between machine addresses and page-info structures. */
>> -#define __maddr_to_page(ma) __mfn_to_page((ma) >> PAGE_SHIFT)
>> -#define __page_to_maddr(pg) ((paddr_t)__page_to_mfn(pg) << PAGE_SHIFT)
>> +#define __maddr_to_page(ma) mfn_to_page(maddr_to_mfn(ma))
>> +#define __page_to_maddr(pg) (mfn_to_maddr(page_to_mfn(pg)))
> 
> Same here.
> 
> With at least the first two items taken care of, relevant x86 pieces
> Acked-by: Jan Beulich <jbeulich@suse.com>

I don't plan to address the first one as Andrew were happy with it.

Cheers,

-- 
Julien Grall

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

* Re: [PATCH v4 16/16] xen: Convert page_to_mfn and mfn_to_page to use typesafe MFN
  2018-03-14 17:02     ` Julien Grall
@ 2018-03-15  7:07       ` Jan Beulich
  0 siblings, 0 replies; 76+ messages in thread
From: Jan Beulich @ 2018-03-15  7:07 UTC (permalink / raw)
  To: Julien Grall
  Cc: Tim Deegan, Kevin Tian, Stefano Stabellini, Wei Liu,
	Jun Nakajima, Razvan Cojocaru, George Dunlap, Andrew Cooper,
	Ian Jackson, xen-devel, Paul Durrant, Tamas K Lengyel,
	Suravee Suthikulpanit, Shane Wang, Boris Ostrovsky, Gang Wei

>>> On 14.03.18 at 18:02, <julien.grall@arm.com> wrote:
> On 03/02/2018 04:08 PM, Jan Beulich wrote:
>>>>> On 21.02.18 at 15:02, <julien.grall@arm.com> wrote:
>>> --- a/xen/arch/x86/pv/emul-priv-op.c
>>> +++ b/xen/arch/x86/pv/emul-priv-op.c
>>> @@ -43,16 +43,6 @@
>>>   #include "emulate.h"
>>>   #include "mm.h"
>>>   
>>> -/* Override macros from asm/page.h to make them work with mfn_t */
>>> -#undef mfn_to_page
>>> -#define mfn_to_page(mfn) __mfn_to_page(mfn_x(mfn))
>>> -#undef page_to_mfn
>>> -#define page_to_mfn(pg) _mfn(__page_to_mfn(pg))
>>> -
>>> -/***********************
>>> - * I/O emulation support
>>> - */
>> 
>> Why does this comment go away?
> 
>  From an earlier review, Andrew said:
> 
> "If you're making this change, please take out the Descriptor Tables
> comment like you do with I/O below, because the entire file is dedicated
> to descriptor table support and it will save me one item on a cleanup
> patch :)."

Well, the reasoning isn't really correct, but I agree the comment is at
least misplaced. Hence I'm fine with it being dropped, despite it being
very unrelated to the purpose of the patch. IOW my ack stands with
this one remark un-addressed.

Jan


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 76+ messages in thread

end of thread, other threads:[~2018-03-15  7:07 UTC | newest]

Thread overview: 76+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-02-21 14:02 [PATCH v4 00/16] xen: Convert page_to_mfn and mfn_to_page to use typesafe MFN Julien Grall
2018-02-21 14:02 ` [PATCH v4 01/16] xen/tmem: Convert the file common/tmem_xen.c " Julien Grall
2018-02-21 14:02 ` [PATCH v4 02/16] xen/arm: setup: use maddr_to_mfn rather than _mfn(paddr_to_pfn(...)) Julien Grall
2018-02-21 14:02 ` [PATCH v4 03/16] xen/arm: mm: Use gaddr_to_gfn rather than _gfn(paddr_to_pfn(...)) Julien Grall
2018-02-21 14:02 ` [PATCH v4 04/16] xen/arm: mm: Remove unused M2P code Julien Grall
2018-02-21 14:02 ` [PATCH v4 05/16] xen/arm: mm: Remove unused relinquish_shared_pages Julien Grall
2018-02-21 14:02 ` [PATCH v4 06/16] xen/x86: Remove unused override of page_to_mfn/mfn_to_page Julien Grall
2018-03-01 11:20   ` George Dunlap
2018-03-02 14:42   ` Jan Beulich
2018-03-02 14:44     ` Julien Grall
2018-03-02 15:11       ` Jan Beulich
2018-03-05 13:29         ` Julien Grall
2018-02-21 14:02 ` [PATCH v4 07/16] xen/x86: mm: Switch x86/mm.c to use typesafe for virt_to_mfn Julien Grall
2018-03-02 14:45   ` Jan Beulich
2018-03-02 14:46     ` Julien Grall
2018-02-21 14:02 ` [PATCH v4 08/16] xen/mm: Drop the parameter mfn from populate_pt_range Julien Grall
2018-02-22 16:35   ` Wei Liu
2018-02-22 16:40     ` Julien Grall
2018-02-22 16:51       ` Wei Liu
2018-02-22 16:55         ` Julien Grall
2018-02-22 17:10           ` Wei Liu
2018-03-02 14:55           ` Jan Beulich
2018-03-05 13:43             ` Julien Grall
2018-03-05 14:00               ` Jan Beulich
2018-03-05 14:11                 ` Julien Grall
2018-03-05 14:38                   ` Jan Beulich
2018-03-09 17:29                     ` Wei Liu
2018-03-11 19:30                       ` Julien Grall
2018-03-12  6:36                       ` Jan Beulich
2018-03-14 15:22                         ` Julien Grall
2018-02-21 14:02 ` [PATCH v4 09/16] xen/pdx: Introduce helper to convert MFN <-> PDX Julien Grall
2018-02-22 16:39   ` Wei Liu
2018-02-21 14:02 ` [PATCH v4 10/16] xen/mm: Switch map_pages_to_xen to use MFN typesafe Julien Grall
2018-02-23  4:59   ` Tian, Kevin
2018-02-23 17:21   ` Wei Liu
2018-03-02 15:06   ` Jan Beulich
2018-03-02 15:08     ` Jan Beulich
2018-03-05 14:07     ` Julien Grall
2018-03-05 14:39       ` Jan Beulich
2018-03-05 14:44         ` Julien Grall
2018-02-21 14:02 ` [PATCH v4 11/16] xen/mm: Switch page_alloc.c to typesafe MFN Julien Grall
2018-02-23 17:21   ` Wei Liu
2018-03-02 15:18   ` Jan Beulich
2018-03-02 15:57     ` Julien Grall
2018-02-21 14:02 ` [PATCH v4 12/16] xen/mm: Switch common/memory.c to use " Julien Grall
2018-02-23 17:26   ` Wei Liu
2018-02-23 17:46     ` Julien Grall
2018-02-23 18:05       ` Wei Liu
2018-02-23 18:06         ` Julien Grall
2018-02-23 18:10           ` Wei Liu
2018-03-02 15:34   ` Jan Beulich
2018-03-05 14:18     ` Julien Grall
2018-03-05 14:41       ` Jan Beulich
2018-03-09 17:33         ` Wei Liu
2018-03-11 19:44           ` Julien Grall
2018-03-12  6:39             ` Jan Beulich
2018-03-14 16:08               ` Julien Grall
2018-02-21 14:02 ` [PATCH v4 13/16] xen/grant: Switch {create, replace}_grant_p2m_mapping to " Julien Grall
2018-02-23 17:29   ` Wei Liu
2018-03-02 15:38   ` Jan Beulich
2018-02-21 14:02 ` [PATCH v4 14/16] xen/grant: Switch common/grant_table.c to use " Julien Grall
2018-02-23 17:30   ` Wei Liu
2018-03-02 15:54   ` Jan Beulich
2018-03-02 15:59     ` Julien Grall
2018-03-02 16:12       ` Jan Beulich
2018-02-21 14:02 ` [PATCH v4 15/16] xen/x86: Switch mfn_to_page in x86_64/mm.c " Julien Grall
2018-03-02 15:57   ` Jan Beulich
2018-02-21 14:02 ` [PATCH v4 16/16] xen: Convert page_to_mfn and mfn_to_page " Julien Grall
2018-02-21 14:25   ` Razvan Cojocaru
2018-02-21 14:59   ` Paul Durrant
2018-02-21 23:20   ` Boris Ostrovsky
2018-02-23  4:59   ` Tian, Kevin
2018-02-23 17:31   ` Wei Liu
2018-03-02 16:08   ` Jan Beulich
2018-03-14 17:02     ` Julien Grall
2018-03-15  7:07       ` Jan Beulich

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.