analysis of runtime xenheap memory allocations
Hi,
In the last months we have been working with Stefano's team in AMD at tools to facilitate the analysis of "anonymous" memory allocations performed at "runtime" from the shared Xen heap.
- Anonymous here means: allocations that insist on the common Xen heap pool (i.e., not on a per-domain heap pool).
- Runtime here means: system_state >= SYS_STATE_active.
TL;DR: A set of patches print a warning stack when anonymous allocations are detected at runtime. Scripts help to parse the stack traces that can be checked to evaluate how problematic certain allocations paths can be or how difficult it could be to update them. A full example of the results is e.g., in [1], and the Readme-details in [2]. Below in the email more details and some commented stack traces.
Feedback, especially on the paths that are currently marked as "UNDECIDED" is very welcome.
[1]: https://gitlab.com/minervasys/public/xen/-/blob/minerva/warn/minerva_analysis/1674833972/parsed/x86_64/xilinx-hyperlaunch-x86_64-gcc-debug-virtio-pci-net
[2]: https://gitlab.com/minervasys/public/xen/-/blob/minerva/warn/minerva_analysis/README.md
Here the long version.
The work is a starting point to identify the paths leading to anonymous allocations, identify whether they could be "problematic" (see below), and simplify decisions on possible changes targeting such allocations.
Why
---
In contexts like those targeted by Xen for safety, we want to run VMs with different criticality side-by-side (in parallel).
Xen provides services to both critical and non critical VMs. Shared resources at Xen-level may become problematic when low/no-criticality VM can prevent higher criticality VMs from receiving the requested service.
This is also relevant in security context where we want to ensure that VMs cannot deplete resources shared with other VMs.
The shared Xen heap (xenheap) is one of such shared resources that is accessed from both potentially critical and non critical VMs to serve various memory allocations. While for system_state < SYS_STATE_active we can statically define the amount of Xen heap that will be consumed by the system, at runtime this becomes blurry as it depends on the system's usage. This is problematic when a critical VM cannot receive its intended service due to xenheap memory depletion.
What
----
We want to identify those anonymous allocations, understand the paths that lead to them, and the allocated sizes. Also, many allocations are expected to be followed by a "free", and it is useful to keep track of how much memory is left allocated after a service completes.
To address this, we have been following multiple directions:
a) Runtime testing
b) Offline call path analysis
c) Draft of changes needed for a shared -> per-domain heap switch
Runtime testing
---------------
See: https://gitlab.com/minervasys/public/xen/-/blob/minerva/warn/minerva_analysis/README.md
The idea is to hook in the testing infrastructure of Xen and detect anonymous allocations at runtime.
The system is composed by a set of patches added to xen/common/page_alloc.c and xen/common/xmalloc_tlsf.c that will result in a warning stack being produced at runtime when anonymous allocations are detected.
In such cases, the warning will try to detect additional information like:
the domain currently running and the pointer to the allocated memory area. Such information is later matched (if possible) against corresponding frees. Specifically, if possible, anonymous memory allocated via alloc_domheap_pages is matched against free_domheap_pages, while memory allocated via xmalloc is matched against xfree.
Depending on the type of test, the amount of information discovered by the tool might differ.
The execution can be integrated in the Xen pipeline (e.g.,
https://gitlab.com/xen-project/people/sstabellini/xen/-/pipelines/1682205969).
A script
(https://gitlab.com/minervasys/public/xen/-/blob/minerva/warn/minerva_analysis/log_parser.py)
parses the CI output and produces statistics and stack traces leading to an allocation. Those stack traces can be checked to evaluate how problematic certain allocations paths can be or how difficult it could be to update them.
The tool support building a "database" of comments associated with a stack trace and can automatically (re)apply comments to known stack traces in further runs. (See #Automated Generation of Comments in the README.)
An example of the processing can be seen e.g., here (for pipeline 1674833972):
https://gitlab.com/minervasys/public/xen/-/tree/minerva/warn/minerva_analysis/1674833972
Here, the parsed files are already commented with, for example:
```
// MINERVA: UNDECIDED
// Anonymous allocation increases per-CPU heap.
// Allocation not attributable to any domain.
Domain : d0
Occurrences: 1
Total size : 128 B
return_to_new_vcpu64
leave_hypervisor_to_guest
traps.c#check_for_pcpu_work
do_softirq
softirq.c#__do_softirq
timer.c#timer_softirq_action
_xmalloc
_xmalloc
```In this example, a direct _xmalloc is triggered from an ISR.
No domain can be held accountable for such allocation and a different strategy to ensure/motivate the boundness of such allocations should be planned.
```
// MINERVA: PROB_SAFE (under the assumption that hwdom are safe)
// the path is already attributed to a domain pool, except if the domain is
// a hardware domain, in this case the allocation is anonymous (p2m_alloc_page).
alloc_domheap_pages()
entry.o#guest_sync_slowpath
do_trap_guest_sync
traps.c#do_trap_hypercall
do_memory_op
xenmem_add_to_physmap
xenmem_add_to_physmap_one
p2m_set_entry
p2m.c#__p2m_set_entry
p2m.c#p2m_next_level
p2m.c#p2m_alloc_page
alloc_domheap_pages
alloc_domheap_pages
```In this example, the allocation is marked as "probably safe" since the path is taken only when the domain is a hardware domain.
Such domains can (under the usage-domain of interest) perform allocations.
Offline Call Path Analysis
--------------------------
See: https://gitlab.com/minervasys/public/xen/-/blob/minerva/warn/minerva_static_analysis/README.md
This analysis can complement the runtime discovery of anonymous allocations and uses compiler support to generate call-graphs (using -fcallgraph-info).
Two scripts support parsing and filtering the (large amount of) data generated.
callpath.py generates the call paths leading to one selected function. filter.py can filter the call paths based on whitelist / blacklist of functions that should be included / excluded.
For example, in the above branch we have used the scripts to generate and refine the paths leading to alloc_domheap_pages().
The offline analysis can complement the runtime one by reporting reacheable paths that have not (yet) been generated in testing scenarios.
Draft of changes for per-domain heap
------------------------------------
See: https://gitlab.com/minervasys/public/xen/-/tree/minerva/per-domain-xenheap-idea-approach
We have prototyped some of the changes that would be needed (especially in the configuration) if more allocations that are now anonymous would be taken by a per-domain heap instead.
The main change in the prototype is to enforce the current domain as target heap allocator for all anonymous (i.e., domain == NULL) allocations triggered from alloc_xenheap_pages().
Also, we force the use of the newly modified alloc_xenheap_page() in alloc_xen_pagetable()
``` - pg = alloc_domheap_pages(NULL, order, memflags | MEMF_no_scrub); + if (system_state == SYS_STATE_active) + d = current->domain; + + pg = alloc_domheap_pages(d, order, memflags | MEMF_no_scrub); ```
The change is _not_ correct in many cases (since current->domain might not be the right domain), but allows finding the needed configuration changes and potential needs for increasing the sizes of per-domain heaps.
The commit:
https://gitlab.com/minervasys/public/xen/-/commit/c1d6baae27932d2a7f07e82560deae5f64c5536a
implements these changes.
Thanks,
Andrea
Quoted 17 lines · Andrea Bastoni
Hi,
In the last months we have been working with Stefano's team in AMD at tools to facilitate the analysis of "anonymous" memory allocations performed at "runtime" from the shared Xen heap.
- Anonymous here means: allocations that insist on the common Xen heap pool (i.e., not on a per-domain heap pool).
- Runtime here means:system_state>=SYS_STATE_active.TL;DR: A set of patches print a warning stack when anonymous allocations are detected at runtime. Scripts help to parse the stack traces that can be checked to evaluate how problematic certain allocations paths can be or how difficult it could be to update them. A full example of the results is e.g., in [1], and the Readme-details in [2]. Below in the email more details and some commented stack traces.
Quoted 5 lines
Feedback, especially on the paths that are currently marked as "UNDECIDED" is very welcome.
[1]: https://gitlab.com/minervasys/public/xen/-/blob/minerva/warn/minerva_analysis/1674833972/parsed/x86_64/xilinx-hyperlaunch-x86_64-gcc-debug-virtio-pci-net
[2]: https://gitlab.com/minervasys/public/xen/-/blob/minerva/warn/minerva_analysis/README.md
Hello,
A few observations.
As you note, assigning ownership to current->domain is usually the wrong thing. Setting PGC_allocated however is worse; you've created a security vulnerability where guests can free memory that Xen things is private to it.
Similarly, you can't interchange xenheap and domheap like that. It will work on small systems, but not on large ones. Nevertheless, it's an ok start for debugging.
Ages ago, I tried to make a start on this problem,
https://xenbits.xen.org/gitweb/?p=people/andrewcoop/xen.git;a=shortlog;h=refs/heads/xen-fault-ttl
simply by counting the number of xmalloc()'s to the domain with with they were logically associated. It's stalled because the fault-injection test found a refcounting bug and there isn't even an agreement on whether it's a bug or not, or how to fix it.
I'm struggling to follow the analysis on
https://gitlab.com/xen-project/people/sstabellini/xen/-/jobs/9207014172
However, it appears to be a mix of reports between arm64 and x86_64 jobs, and they need separating to get a clearer picture.
The bottom of each stack trace appears to be doubled. Is that intentional?
Part of the problem is that xmalloc() isn't separated from {xen,dom}heap, despite the former being layered on top of the latter. All *heap pages are going to a multiple of 4k as that's the allocation unit. It will probably be better to terminate at _xmalloc() and ignore the call down into alloc_domheap_page(). Importantly, _malloc allocating another 4k page isn't strictly related to the caller, when it's doing so to replenish it's free pool.
The 1-byte allocations are silly. bitmap_to_xenctl_bitmap() is buggy for trying to cater to an endianness case we simply don't support. There's no memory allocation needed at all. There are patches to resolve this from aaages back, blocked on a request to fix up other endian helpers in the meantime.
evtchn_fifo_init_control() seems to show up a lot, and seems to be the only dynamic allocation triggered by the guest itself. It's reasonably large, but probably ought to be allocated up front. event_fifo is pretty ubiquitous these days. I'm not sure where the alternative sizes come from; they ought to all be 392 bytes.
I'm concerned by the xfree() in efi_runtime_call() seeing as there's no associated allocation that I can see.
Finally, it's not just anonymous allocations you need to worry about. Even ones out of the p2m memory pool are still fatal at the point of exhaustion, and can cause safety issues.
~Andrew
Quoted 28 lines · Andrew Cooper
Quoted 17 lines · Andrea Bastoni
Hi,
In the last months we have been working with Stefano's team in AMD at tools to facilitate the analysis of "anonymous" memory allocations performed at "runtime" from the shared Xen heap.
- Anonymous here means: allocations that insist on the common Xen heap pool (i.e., not on a per-domain heap pool).
- Runtime here means:system_state>=SYS_STATE_active.TL;DR: A set of patches print a warning stack when anonymous allocations are detected at runtime. Scripts help to parse the stack traces that can be checked to evaluate how problematic certain allocations paths can be or how difficult it could be to update them. A full example of the results is e.g., in [1], and the Readme-details in [2]. Below in the email more details and some commented stack traces.
Quoted 5 lines
Feedback, especially on the paths that are currently marked as "UNDECIDED" is very welcome.
[1]: https://gitlab.com/minervasys/public/xen/-/blob/minerva/warn/minerva_analysis/1674833972/parsed/x86_64/xilinx-hyperlaunch-x86_64-gcc-debug-virtio-pci-net
[2]: https://gitlab.com/minervasys/public/xen/-/blob/minerva/warn/minerva_analysis/README.mdHello,
A few observations.
Quoted 8 lines
As you note, assigning ownership to
current->domainis usually the wrong thing. SettingPGC_allocatedhowever is worse; you've created a security vulnerability where guests can free memory that Xen things is private to it.Similarly, you can't interchange xenheap and domheap like that. It will work on small systems, but not on large ones. Nevertheless, it's an ok start for debugging.
Thank you for the feedback.
As you saw, there are two parts.
Here you're referring to https://gitlab.com/minervasys/public/xen/-/commit/c1d6baae27932d2a7f07e82560deae5f64c5536a
which we used as debugging to find out where the configuration needed
to be updated. This is in a separate branch as the rest of the observations (minerva/per-domain-xenheap-idea-approach vs minerva/warn)
Quoted 6 lines
Ages ago, I tried to make a start on this problem,
https://xenbits.xen.org/gitweb/?p=people/andrewcoop/xen.git;a=shortlog;h=refs/heads/xen-fault-ttl
simply by counting the number of xmalloc()'s to the domain with with they were logically associated. It's stalled because the fault-injection test found a refcounting bug and there isn't even an agreement on whether it's a bug or not, or how to fix it.
The other part goes along a similar line trying to find e.g., who triggered the xmalloc.
Quoted 4 lines
I'm struggling to follow the analysis on
https://gitlab.com/xen-project/people/sstabellini/xen/-/jobs/9207014172
However, it appears to be a mix of reports between arm64 andx86_64jobs, and they need separating to get a clearer picture.
The job prints a summary of each analyzed log. The actual analysis is created as artifact of the pipeline and can be downloaded. The output is then splitted per architecture, similarly to what has been committed here:
https://gitlab.com/minervasys/public/xen/-/tree/minerva/warn/minerva_analysis/1674833972/parsed
Quoted 1 line
The bottom of each stack trace appears to be doubled. Is that intentional?
Nice catch. On arm the parser of the logs needs to be modified to avoid considering this two times:
(XEN) [ 240.296766] [<00000a000022f784>] _xmalloc+0xf0/0x32c (PC) (XEN) [ 240.298386] [<00000a000022f784>] _xmalloc+0xf0/0x32c (LR)
Quoted 5 lines
Part of the problem is that
xmalloc()isn't separated from {xen,dom}heap, despite the former being layered on top of the latter. All *heap pages are going to a multiple of 4k as that's the allocation unit. It will probably be better to terminate at_xmalloc()and ignore the call down intoalloc_domheap_page().
Those are tracked separately (and the free also tries to match them separately): "DEBUG:...xmem_pool_alloc" terminates at the _xmalloc(), "DEBUG...xmalloc_whole_pages" tracks the replenishments.
Quoted 3 lines
Importantly,
_malloc
allocating another 4k page isn't strictly related to the caller, when it's doing so to replenish it's free pool.
Yes, the ownership tracking in free_xenheap_pages() is fuzzy.
Quoted 11 lines
The 1-byte allocations are silly.
bitmap_to_xenctl_bitmap()is buggy for trying to cater to an endianness case we simply don't support. There's no memory allocation needed at all. There are patches to resolve this from aaages back, blocked on a request to fix up other endian helpers in the meantime.
evtchn_fifo_init_control()seems to show up a lot, and seems to be the only dynamic allocation triggered by the guest itself. It's reasonably large, but probably ought to be allocated up front.event_fifois pretty ubiquitous these days. I'm not sure where the alternative sizes come from; they ought to all be 392 bytes.
You mean like here?
https://gitlab.com/minervasys/public/xen/-/blob/minerva/warn/minerva_analysis/1674833972/parsed/arm64/qemu-dom0less-arm64-gcc-debug-virtio-pci-net#L222
Quoted 19 lines
// MINERVA:
PROB_SAFE
// 1) evtchn_fifo_init_* should only happen once per domain at boot
// (NOTE: Count not always 2)
// 2) evtchn fifo evtchn can be disabled in favor of the older 2l evtchn
// interface that doesn't require allocations (xen_fifo_events=false)
Domain : d1
Occurrences: 2
Total size : 1424 B
entry.o#guest_sync_slowpath
do_trap_guest_sync
traps.c#do_trap_hypercall
do_event_channel_op
evtchn_fifo_init_control
_xzalloc
_xmalloc
Quoted 2 lines
I'm concerned by the
xfree()inefi_runtime_call()seeing as there's no associated allocation that I can see.
The verbose log files contain all the parsed allocation paths
e.g., (https://gitlab.com/minervasys/public/xen/-/blob/minerva/warn/minerva_analysis/1674833972/parsed/x86_64/verbose/xilinx-hyperlaunch-x86_64-gcc-debug-virtio-pci-net#L25)
The summary misses this information. This can be improved.
Quoted 3 lines
Finally, it's not just anonymous allocations you need to worry about. Even ones out of the p2m memory pool are still fatal at the point of exhaustion, and can cause safety issues.
Some of the allocations on such paths are tracked, e.g.,
Quoted 12 lines
// MINERVA: UNDECIDED
// Similar toguest_remove_page
// allocation related to the memory management of iommu page tables for // a domain (the domain is available down the chain).
// The allocation of the iommu PDE is anonymous.
// If no memory is available, the domain is crashed.
// Unclear if only selected VMs can trigger this path.
Domain : d0
Occurrences: 2
Total size : 8192 Bsvm_stgi_label
svm_vmexit_handler
Quoted 15 lines
hvm_hypercall
hvm_memory_op
do_memory_op
memory.c#xenmem_add_to_physmap_batch
xenmem_add_to_physmap_one
set_foreign_p2m_entry
p2m.c#set_typed_p2m_entry
p2m_set_entry
p2m-pt.c#p2m_pt_set_entry
iommu_legacy_map
iommu_map
amd_iommu_map_page
iommu_map.c#iommu_pde_from_dfn
iommu_alloc_pgtable
alloc_domheap_pagesMaybe there is a better place where to terminate / catch these cases?
Thanks,
Andrea