All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alejandro Vallejo <alejandro.vallejo@cloud.com>
To: xen-devel@lists.xenproject.org
Cc: "Hongyan Xia" <hongyxia@amazon.com>,
	"Andrew Cooper" <andrew.cooper3@citrix.com>,
	"Anthony PERARD" <anthony.perard@vates.tech>,
	"Michal Orzel" <michal.orzel@amd.com>,
	"Jan Beulich" <jbeulich@suse.com>,
	"Julien Grall" <julien@xen.org>,
	"Roger Pau Monné" <roger.pau@citrix.com>,
	"Stefano Stabellini" <sstabellini@kernel.org>,
	"Julien Grall" <jgrall@amazon.com>,
	"Elias El Yandouzi" <eliasely@amazon.com>,
	"Alejandro Vallejo" <alejandro.vallejo@cloud.com>
Subject: [PATCH v5 08/15] xen/page_alloc: Add a path for xenheap when there is no direct map
Date: Wed,  8 Jan 2025 15:18:15 +0000	[thread overview]
Message-ID: <20250108151822.16030-9-alejandro.vallejo@cloud.com> (raw)
In-Reply-To: <20250108151822.16030-1-alejandro.vallejo@cloud.com>

From: Hongyan Xia <hongyxia@amazon.com>

When there is not an always-mapped direct map, xenheap allocations need
to be mapped and unmapped on-demand.

Signed-off-by: Hongyan Xia <hongyxia@amazon.com>
Signed-off-by: Julien Grall <jgrall@amazon.com>
Signed-off-by: Elias El Yandouzi <eliasely@amazon.com>
Signed-off-by: Alejandro Vallejo <alejandro.vallejo@cloud.com>
---
v4->v5:
  * Remove stray comma in printk() after XENLOG_WARNING.

Elias @ v4:
  I have left the call to map_pages_to_xen() and destroy_xen_mappings()
  in the split heap for now. I am not entirely convinced this is
necessary
  because in that setup only the xenheap would be always mapped and
  this doesn't contain any guest memory (aside the grant-table).
  So map/unmapping for every allocation seems unnecessary.

v3->v4:
  * Call printk instead of dprintk()

v1->v2:
  * Fix remaining wrong indentation in alloc_xenheap_pages()

Changes since Hongyan's version:
  * Rebase
  * Fix indentation in alloc_xenheap_pages()
  * Fix build for arm32
---
 xen/common/page_alloc.c | 43 +++++++++++++++++++++++++++++++++++++++--
 1 file changed, 41 insertions(+), 2 deletions(-)

diff --git a/xen/common/page_alloc.c b/xen/common/page_alloc.c
index 1bf070c8c5df..1c01332b6cb0 100644
--- a/xen/common/page_alloc.c
+++ b/xen/common/page_alloc.c
@@ -2435,6 +2435,7 @@ void init_xenheap_pages(paddr_t ps, paddr_t pe)
 void *alloc_xenheap_pages(unsigned int order, unsigned int memflags)
 {
     struct page_info *pg;
+    void *virt_addr;
 
     ASSERT_ALLOC_CONTEXT();
 
@@ -2443,17 +2444,36 @@ void *alloc_xenheap_pages(unsigned int order, unsigned int memflags)
     if ( unlikely(pg == NULL) )
         return NULL;
 
-    return page_to_virt(pg);
+    virt_addr = page_to_virt(pg);
+
+    if ( !has_directmap() &&
+         map_pages_to_xen((unsigned long)virt_addr, page_to_mfn(pg), 1UL << order,
+                          PAGE_HYPERVISOR) )
+    {
+        /* Failed to map xenheap pages. */
+        free_heap_pages(pg, order, false);
+        return NULL;
+    }
+
+    return virt_addr;
 }
 
 
 void free_xenheap_pages(void *v, unsigned int order)
 {
+    unsigned long va = (unsigned long)v & PAGE_MASK;
+
     ASSERT_ALLOC_CONTEXT();
 
     if ( v == NULL )
         return;
 
+    if ( !has_directmap() &&
+         destroy_xen_mappings(va, va + (PAGE_SIZE << order)) )
+        printk(XENLOG_WARNING
+                "Error while destroying xenheap mappings at %p, order %u\n",
+                v, order);
+
     free_heap_pages(virt_to_page(v), order, false);
 }
 
@@ -2477,6 +2497,7 @@ void *alloc_xenheap_pages(unsigned int order, unsigned int memflags)
 {
     struct page_info *pg;
     unsigned int i;
+    void *virt_addr;
 
     ASSERT_ALLOC_CONTEXT();
 
@@ -2489,16 +2510,28 @@ void *alloc_xenheap_pages(unsigned int order, unsigned int memflags)
     if ( unlikely(pg == NULL) )
         return NULL;
 
+    virt_addr = page_to_virt(pg);
+
+    if ( !has_directmap() &&
+         map_pages_to_xen((unsigned long)virt_addr, page_to_mfn(pg), 1UL << order,
+                          PAGE_HYPERVISOR) )
+    {
+        /* Failed to map xenheap pages. */
+        free_domheap_pages(pg, order);
+        return NULL;
+    }
+
     for ( i = 0; i < (1u << order); i++ )
         pg[i].count_info |= PGC_xen_heap;
 
-    return page_to_virt(pg);
+    return virt_addr;
 }
 
 void free_xenheap_pages(void *v, unsigned int order)
 {
     struct page_info *pg;
     unsigned int i;
+    unsigned long va = (unsigned long)v & PAGE_MASK;
 
     ASSERT_ALLOC_CONTEXT();
 
@@ -2510,6 +2543,12 @@ void free_xenheap_pages(void *v, unsigned int order)
     for ( i = 0; i < (1u << order); i++ )
         pg[i].count_info &= ~PGC_xen_heap;
 
+    if ( !has_directmap() &&
+         destroy_xen_mappings(va, va + (PAGE_SIZE << order)) )
+        printk(XENLOG_WARNING
+                "Error while destroying xenheap mappings at %p, order %u\n",
+                v, order);
+
     free_heap_pages(pg, order, true);
 }
 
-- 
2.47.1



  parent reply	other threads:[~2025-01-08 15:19 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-08 15:18 [PATCH v5 00/15] Remove the directmap Alejandro Vallejo
2025-01-08 15:18 ` [PATCH v5 01/15] x86: Create per-domain mapping for guest_root_pt Alejandro Vallejo
2025-01-08 15:18 ` [PATCH v5 02/15] x86/pv: Use copy_domain_page() to manage domheap pages during initrd relocation Alejandro Vallejo
2025-01-08 15:18 ` [PATCH v5 03/15] x86/pv: Rewrite how building PV dom0 handles domheap mappings Alejandro Vallejo
2025-01-08 15:18 ` [PATCH v5 04/15] x86: Initialize mapcache for PV, HVM, and idle domains Alejandro Vallejo
2025-01-08 15:18 ` [PATCH v5 05/15] x86: Add a boot option to enable and disable the direct map Alejandro Vallejo
2025-01-08 15:18 ` [PATCH v5 06/15] xen/x86: Add support for the PMAP Alejandro Vallejo
2025-01-08 15:18 ` [PATCH v5 07/15] x86/domain_page: Remove the fast paths when mfn is not in the directmap Alejandro Vallejo
2025-01-08 15:18 ` Alejandro Vallejo [this message]
2025-01-08 15:18 ` [PATCH v5 09/15] x86/setup: Leave early boot slightly earlier Alejandro Vallejo
2025-01-08 15:18 ` [PATCH v5 10/15] xen/page_alloc: vmap heap nodes when they are outside the direct map Alejandro Vallejo
2025-01-08 15:18 ` [PATCH v5 11/15] x86/setup: Do not create valid mappings when directmap=no Alejandro Vallejo
2025-12-04 11:04   ` Roger Pau Monné
2025-01-08 15:18 ` [PATCH v5 12/15] xen/arm64: mm: Use per-pCPU page-tables Alejandro Vallejo
2025-01-08 15:18 ` [PATCH v5 13/15] xen/arm32: Hardwire zeroeth_table_offset to 0 on ARM_32 Alejandro Vallejo
2025-01-08 15:18 ` [PATCH v5 14/15] xen/arm64: Implement a mapcache for arm64 Alejandro Vallejo
2025-01-08 15:18 ` [PATCH v5 15/15] xen/arm64: Allow the admin to enable/disable the directmap Alejandro Vallejo
2025-01-08 15:30 ` [PATCH v5 00/15] Remove " Alejandro Vallejo
2025-02-06 14:55 ` Alejandro Vallejo
2025-02-06 15:06   ` Roger Pau Monné

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250108151822.16030-9-alejandro.vallejo@cloud.com \
    --to=alejandro.vallejo@cloud.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=anthony.perard@vates.tech \
    --cc=eliasely@amazon.com \
    --cc=hongyxia@amazon.com \
    --cc=jbeulich@suse.com \
    --cc=jgrall@amazon.com \
    --cc=julien@xen.org \
    --cc=michal.orzel@amd.com \
    --cc=roger.pau@citrix.com \
    --cc=sstabellini@kernel.org \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.