Skip to contentSkip to conversation

xen: write a high level description of the sub-arch choices for heap layout

PATCH Superseded Ian Campbell 3 participants 9 messages Sep 30, 2015

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen: write a high level description of the sub-arch choices for heap layout

The 3 options which (sub)arches have for the layout of their heaps is a little subtle (in particular the two CONFIG_SEPARATE_XENHEAP=n submodes) and can be a bit tricky to derive from the code.

Therefore try and write down some guidance on what the various modes are.

Note that this is intended more as a high level overview rather than a detailed guide to the full page allocator interfaces.

---
 xen/common/page_alloc.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 97 insertions(+)
xen/common/page_alloc.c +97 -0
index 2b8810c..46c1ab9 100644
@@ -20,6 +20,103 @@
  * along with this program; If not, see <http://www.gnu.org/licenses/>.
  */
 
+/*
+ * In general Xen maintains two pools of memory:
+ *
+ * - Xen heap: Memory which is always mapped (i.e accessible by
+ *             virtual address), via a permanent and contiguous
+ *             "direct mapping". Macros like va() and pa() are valid
+ *             for such memory.
+ *
+ *             Xen heap pages are always anonymous (that is, not tied
+ *             or accounted to any particular domain).
+ *
+ * - Dom heap: Memory which must be explicitly mapped, usually
+ *             transiently with map_domain_page, in order to be
+ *             used. va() and pa() are not valid for such memory.
+ *
+ *             Dom heap pages are often tied to a particular domain,
+ *             but need not be (passing domain==NULL results in an
+ *             anonymous dom heap allocation).
+ *
+ * The exact nature of this split is a (sub)arch decision which can
+ * select one of three main variants:
+ *
+ * CONFIG_SEPARATE_XENHEAP=y
+ *
+ *   The xenheap is maintained as an entirely separate heap.
+ *
+ *   Arch code arranges for some (perhaps small) amount of physical
+ *   memory to be covered by a direct mapping and registers that
+ *   memory as the Xen heap (via init_xenheap_pages()) and the
+ *   remainder as the dom heap.
+ *
+ *   This mode of operation is most commonly used by 32-bit arches
+ *   where the virtual address space is insufficient to map all RAM.
+ *
+ * CONFIG_SEPARATE_XENHEAP=n W/ DIRECT MAP OF ALL RAM
+ *
+ *   All of RAM is covered by a permanent contiguous mapping and there
+ *   is only a single heap.
+ *
+ *   Memory allocated from the Xen heap is flagged (in
+ *   page_info.count_info) with PGC_xen_heap which may be used to
+ *   check and enforce correct usage of va()/pa() etc. Memory
+ *   allocated from the Dom heap must still be explicitly mapped
+ *   before use (e.g. with map_domain_page) in particular in common
+ *   code.
+ *
+ *   xenheap_max_mfn() should not be called by arch code.
+ *
+ *   This mode of operation is most commonly used by 64-bit arches
+ *   which have sufficient free virtual address space to permanently
+ *   map the largest practical amount RAM currently expected on that
+ *   arch.
+ *
+ * CONFIG_SEPARATE_XENHEAP=n W/ ONLY DIRECT MAP OF ONLY PARTIAL RAM
+ *
+ *   There is a single heap, but only the beginning (up to some
+ *   threshold) is covered by a permanent contiguous mapping.
+ *
+ *   Memory allocated from the Xen heap is allocated from below the
+ *   threshold and flagged with PGC_xen_heap. Memory allocated from
+ *   the dom heap is allocated from anywhere in the heap (although it
+ *   will prefer to allocate from as high as possible to try and keep
+ *   Xen heap suitable memory available).
+ *
+ *   Arch code must call xenheap_max_mfn() to signal the limit of the
+ *   direct mapping.
+ *
+ *   This mode of operation is most commonly used by 64-bit arches
+ *   which have a restricted amount of virtual address space available
+ *   for a direct map (due to e.g. reservations for other purposes)
+ *   such that it is not possible to map all of RAM on systems with
+ *   the largest practical amount of RAM currently expected on that
+ *   arch.
+ *
+ * Boot Allocator
+ *
+ *   In addition to the two primary pools (xen heap and dom heap) a
+ *   third "boot allocator" is used at start of day. This is a
+ *   simplified allocator which can be used.
+ *
+ *   Typically all memory which is destined to be dom heap memory
+ *   (which is everything in the CONFIG_SEPARATE_XENHEAP=n
+ *   configurations) is first allocated to the boot allocator (with
+ *   init_boot_pages()) and is then handed over to the main dom heap in
+ *   end_boot_allocator().
+ *
+ * "Contiguous" mappings
+ *
+ *   Note that although the above talks about "contiguous" mappings
+ *   some architectures implement a scheme ("PDX compression") to
+ *   compress unused portions of the machine address space (i.e. large
+ *   gaps between distinct banks of memory) in order to avoid creating
+ *   enormous frame tables and direct maps which mostly map
+ *   nothing. Thus a contiguous mapping may still have distinct
+ *   regions within it.
+ */
+
 #include <xen/config.h>
 #include <xen/init.h>
 #include <xen/types.h>
Signature · 1 line
2.1.4
#1 lore raw
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 30.09.15 at 12:22, <[email protected]> wrote:

The 3 options which (sub)arches have for the layout of their heaps is a little subtle (in particular the two CONFIG_SEPARATE_XENHEAP=n submodes) and can be a bit tricky to derive from the code.

Therefore try and write down some guidance on what the various modes are.

Note that this is intended more as a high level overview rather than a detailed guide to the full page allocator interfaces.

Thanks for writing this up, just two minor things:

Quoted 8 lines

+ * CONFIG_SEPARATE_XENHEAP=n W/ DIRECT MAP OF ALL RAM
+ *
+ * All of RAM is covered by a permanent contiguous mapping and there
+ * is only a single heap.
+ *

+ *   Memory allocated from the Xen heap is flagged (in
+ *   page_info.count_info) with PGC_xen_heap which may be used to
+ *   check and enforce correct usage of va()/pa() etc. Memory

What is this "check and enforce" about? There are validation uses of the flag, but I don't recall any in virt<->phys address translation.

Quoted 10 lines
+ *   allocated from the Dom heap must still be explicitly mapped
+ *   before use (e.g. with map_domain_page) in particular in common
+ *   code.
+ *
+ *   xenheap_max_mfn() should not be called by arch code.
+ *
+ *   This mode of operation is most commonly used by 64-bit arches
+ *   which have sufficient free virtual address space to permanently
+ *   map the largest practical amount RAM currently expected on that
+ *   arch.
Quoted 2 lines

+ *
+ * CONFIG_SEPARATE_XENHEAP=n W/ ONLY DIRECT MAP OF ONLY PARTIAL RAM

I think one of the two ONLY should be dropped.

Jan

#2 lore raw
Quoted 22 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 30.09.15 at 12:22, <[email protected]> wrote:

The 3 options which (sub)arches have for the layout of their heaps is a little subtle (in particular the two CONFIG_SEPARATE_XENHEAP=n submodes) and can be a bit tricky to derive from the code.

Therefore try and write down some guidance on what the various modes are.

Note that this is intended more as a high level overview rather than a detailed guide to the full page allocator interfaces.

Thanks for writing this up, just two minor things:

Quoted 8 lines

+ * CONFIG_SEPARATE_XENHEAP=n W/ DIRECT MAP OF ALL RAM
+ *
+ * All of RAM is covered by a permanent contiguous mapping and there
+ * is only a single heap.
+ *

+ *   Memory allocated from the Xen heap is flagged (in
+ *   page_info.count_info) with PGC_xen_heap which may be used to
+ *   check and enforce correct usage of va()/pa() etc. Memory
Quoted 2 lines

What is this "check and enforce" about? There are validation uses of the flag, but I don't recall any in virt<->phys address translation.

I think I misremembered/assumed.

Arm has an is_xen_heap_page check for the separate xenheap case.

I suppose s/may be/could be/ would be true (sort of) but I guess I'll just drop that bit.

Quoted 13 lines
Quoted 10 lines
+ *   allocated from the Dom heap must still be explicitly mapped
+ *   before use (e.g. with map_domain_page) in particular in common
+ *   code.
+ *
+ *   xenheap_max_mfn() should not be called by arch code.
+ *
+ *   This mode of operation is most commonly used by 64-bit arches
+ *   which have sufficient free virtual address space to permanently
+ *   map the largest practical amount RAM currently expected on that
+ *   arch.
Quoted 2 lines

+ *
+ * CONFIG_SEPARATE_XENHEAP=n W/ ONLY DIRECT MAP OF ONLY PARTIAL RAM

Quoted 1 line

I think one of the two ONLY should be dropped.

Agreed.

Ian.

#3 lore raw
In reply to Ian Campbell
Quoted 20 lines · Ian Campbell

The 3 options which (sub)arches have for the layout of their heaps is a little subtle (in particular the two CONFIG_SEPARATE_XENHEAP=n submodes) and can be a bit tricky to derive from the code.

Therefore try and write down some guidance on what the various modes are.

Note that this is intended more as a high level overview rather than a detailed guide to the full page allocator interfaces.

---
 xen/common/page_alloc.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 97 insertions(+)
xen/common/page_alloc.c +0 -0
index 2b8810c..46c1ab9 100644

xen/common/page_alloc.c line 29

Quoted 10 lines
xen/common/page_alloc.c +7 -0
@@ -20,6 +20,103 @@
  * along with this program; If not, see <http://www.gnu.org/licenses/>.
  */
 
+/*
+ * In general Xen maintains two pools of memory:
+ *
+ * - Xen heap: Memory which is always mapped (i.e accessible by
+ *             virtual address), via a permanent and contiguous
+ *             "direct mapping". Macros like va() and pa() are valid
+ *             for such memory.

Possibly worth stating that it safe to stash pointers into xenheap memory.

xen/common/page_alloc.c line 36

Quoted 7 lines
xen/common/page_alloc.c +7 -0
+ *
+ *             Xen heap pages are always anonymous (that is, not tied
+ *             or accounted to any particular domain).
+ *
+ * - Dom heap: Memory which must be explicitly mapped, usually
+ *             transiently with map_domain_page, in order to be
+ *             used. va() and pa() are not valid for such memory.

While stashing pointers into domheap memory is definitely buggy.

Quoted 33 lines
xen/common/page_alloc.c +33 -0
+ *
+ *             Dom heap pages are often tied to a particular domain,
+ *             but need not be (passing domain==NULL results in an
+ *             anonymous dom heap allocation).
+ *
+ * The exact nature of this split is a (sub)arch decision which can
+ * select one of three main variants:
+ *
+ * CONFIG_SEPARATE_XENHEAP=y
+ *
+ *   The xenheap is maintained as an entirely separate heap.
+ *
+ *   Arch code arranges for some (perhaps small) amount of physical
+ *   memory to be covered by a direct mapping and registers that
+ *   memory as the Xen heap (via init_xenheap_pages()) and the
+ *   remainder as the dom heap.
+ *
+ *   This mode of operation is most commonly used by 32-bit arches
+ *   where the virtual address space is insufficient to map all RAM.
+ *
+ * CONFIG_SEPARATE_XENHEAP=n W/ DIRECT MAP OF ALL RAM
+ *
+ *   All of RAM is covered by a permanent contiguous mapping and there
+ *   is only a single heap.
+ *
+ *   Memory allocated from the Xen heap is flagged (in
+ *   page_info.count_info) with PGC_xen_heap which may be used to
+ *   check and enforce correct usage of va()/pa() etc. Memory
+ *   allocated from the Dom heap must still be explicitly mapped
+ *   before use (e.g. with map_domain_page) in particular in common
+ *   code.
+ *
+ *   xenheap_max_mfn() should not be called by arch code.

xen/common/page_alloc.c line 79

Quoted 10 lines
xen/common/page_alloc.c +10 -0
+ *
+ *   This mode of operation is most commonly used by 64-bit arches
+ *   which have sufficient free virtual address space to permanently
+ *   map the largest practical amount RAM currently expected on that
+ *   arch.
+ *
+ * CONFIG_SEPARATE_XENHEAP=n W/ ONLY DIRECT MAP OF ONLY PARTIAL RAM
+ *
+ *   There is a single heap, but only the beginning (up to some
+ *   threshold) is covered by a permanent contiguous mapping.

Perhaps avoid the use of "beginning" here? It is just an implementation detail of the only current example.

In some copious free time, I want to see about striding the x86 directmap across NUMA nodes (to allow NUMA-local xenheap allocations even on large boxes), at which point it won't be linear from the start.

~Andrew

#4 lore raw
Quoted 8 lines · Andrew Cooper

+ *

Quoted 4 lines

+ * CONFIG_SEPARATE_XENHEAP=n W/ ONLY DIRECT MAP OF ONLY PARTIAL RAM
+ *
+ * There is a single heap, but only the beginning (up to some
+ * threshold) is covered by a permanent contiguous mapping.

Perhaps avoid the use of "beginning" here? It is just an implementation detail of the only current example.

It's an implementation detail which is currently exposed to the arch code via the need to use xenheap_max_mfn() (or not) and the shape of that API though.

Quoted 3 lines

In some copious free time, I want to see about striding the x86 directmap across NUMA nodes (to allow NUMA-local xenheap allocations even on large boxes), at which point it won't be linear from the start.

In which case this bit of doc would need some adjustments over and above avoiding the work beginning I think, at least to adjust to the replacement for xenheap_max_mfn().

Ian.

#5 lore raw
Quoted 12 lines · Ian Campbell
Quoted 7 lines · Andrew Cooper

+ *

Quoted 4 lines

+ * CONFIG_SEPARATE_XENHEAP=n W/ ONLY DIRECT MAP OF ONLY PARTIAL RAM
+ *
+ * There is a single heap, but only the beginning (up to some
+ * threshold) is covered by a permanent contiguous mapping.

Perhaps avoid the use of "beginning" here? It is just an implementation detail of the only current example.

It's an implementation detail which is currently exposed to the arch code via the need to use xenheap_max_mfn() (or not) and the shape of that API though.

Quoted 6 lines
Quoted 3 lines

In some copious free time, I want to see about striding the x86 directmap across NUMA nodes (to allow NUMA-local xenheap allocations even on large boxes), at which point it won't be linear from the start.

In which case this bit of doc would need some adjustments over and above avoiding the work beginning I think, at least to adjust to the replacement for xenheap_max_mfn().

True in both cases, in which case the original wording in fine.

~Andrew

#6 lore raw
In reply to Andrew Cooper
Quoted 9 lines · Andrew Cooper
Quoted 7 lines

+ *
+ * Xen heap pages are always anonymous (that is, not tied
+ * or accounted to any particular domain).
+ *
+ * - Dom heap: Memory which must be explicitly mapped, usually
+ * transiently with map_domain_page, in order to be
+ * used. va() and pa() are not valid for such memory.

While stashing pointers into domheap memory is definitely buggy.

Is this true even considering the result of e.g. map_domain_page_global?

#7 lore raw
Quoted 9 lines · Ian Campbell
Quoted 7 lines · Andrew Cooper
Quoted 7 lines

+ *
+ * Xen heap pages are always anonymous (that is, not tied
+ * or accounted to any particular domain).
+ *
+ * - Dom heap: Memory which must be explicitly mapped, usually
+ * transiently with map_domain_page, in order to be
+ * used. va() and pa() are not valid for such memory.

Quoted 3 lines
Quoted 1 line

While stashing pointers into domheap memory is definitely buggy.

Is this true even considering the result of e.g. map_domain_page_global?

Ah yes - constructing a pointer into something mapped as global is safe.

Basically I was wondering about some wording to state that things like:

p = map_domain_page();
d->foo->bar = p->baz;
unmap_domain_page(p);

is unsafe and shouldn't be done. There is surprisingly little difference between a xenheap page and a map_domain_page_global()'d page, as they are both present in the permanent mappings.

~Andrew

#8 lore raw
In reply to Ian Campbell
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 30.09.15 at 13:31, <[email protected]> wrote:
Quoted 8 lines · Andrew Cooper
Quoted 7 lines

+ *
+ * Xen heap pages are always anonymous (that is, not tied
+ * or accounted to any particular domain).
+ *
+ * - Dom heap: Memory which must be explicitly mapped, usually
+ * transiently with map_domain_page, in order to be
+ * used. va() and pa() are not valid for such memory.

Quoted 3 lines
Quoted 1 line

While stashing pointers into domheap memory is definitely buggy.

Is this true even considering the result of e.g. map_domain_page_global?

No. So if you wanted to adjust the wording, you'd want to exclude that as well as vmap().

Jan