Repository navigation
Conversation
nashif
commented
Oct 24, 2026
- arch: arm64: mmu: add a test hook to read a leaf descriptor
- arch: arm64: mmu: map partitions over what the kernel already maps
- tests: arch: arm64: cover domain-private mapping attributes
- tests: kernel: mem_protect: cover partitions added while paged out
- arch: arm64: mmu: split a source block when globalizing part of it
- tests: arch: arm64: cover a partition over part of a kernel block
- arch: arm64: mmu: report a private mapping that could not be applied
- tests: arch: arm64: cover a partition that cannot split its block
- kernel: userspace: keep a thread in its domain when moving it fails
- tests: arch: arm64: cover a thread that cannot move to another domain
59e6b11 to
7969190
Compare
|
Ignore this PR, just using it for testing... |
🤖 AI review (advisory)💬 Comment: worth a look, nothing blocking. Findings: 4 · Highest severity: medium · Confidence: high Partial failures in 3 finding(s) are posted as inline comments on the changed lines (2 medium, 1 low). Findings not anchored to a changed line🔵 low: 1 finding(s)1. Commit 6e3c22d message describes a second mapping step not present in the test🔵 low · Commit 6e3c22d's message states that the test gives the freed frame content of its own through a kernel-only mapping and verifies the read does not return that other mapping's content, but Fix: Update commit 6e3c22d's message to match the test implementation (as its Doxygen comment already does), or add the second mapping step described in the commit message. Commit message issuesCommit This review is generated by a language model with tool access to the source tree. It is advisory; verify before acting on it. |
| if (!expand_to_table(pte, level)) { | ||
| return -ENOMEM; | ||
| } | ||
| } | ||
|
|
||
| if (is_table_desc(*pte, level)) { | ||
| subtable = pte_desc_table(*pte); | ||
| ret = remap_mapping(subtable, virt, step, attr_desc, level + 1); | ||
| if (ret != 0) { | ||
| return ret; | ||
| } | ||
| continue; | ||
| } | ||
|
|
||
| uint64_t mask = private_attrs_mask(attr_desc); | ||
|
|
||
| *pte = (*pte & ~mask) | (attr_desc & mask); | ||
| debug_show_pte(pte, level); | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| /* | ||
| * Give the given range domain private attributes. The range keeps whatever | ||
| * the kernel tables map it to: the virtual address is not necessarily the | ||
| * physical address, and with demand paging the range may be paged out, in | ||
| * which case the private entry keeps the location token and the new | ||
| * attributes apply when the page comes back in. | ||
| */ | ||
| static int private_map(struct arm_mmu_ptables *ptables, const char *name, | ||
| uintptr_t phys, uintptr_t virt, size_t size, uint32_t attrs) | ||
| uintptr_t virt, size_t size, uint32_t attrs) | ||
| { | ||
| uint64_t attr_desc = get_region_desc(attrs | MT_NG); | ||
| k_spinlock_key_t key; | ||
| int ret; | ||
|
|
||
| MMU_DEBUG("private map [%s]: virt %lx size %lx attr %llx\n", | ||
| name, virt, size, attr_desc); | ||
| __ASSERT(((virt | size) & (CONFIG_MMU_PAGE_SIZE - 1)) == 0, | ||
| "address/size are not page aligned\n"); | ||
|
|
||
| ret = privatize_page_range(ptables, &kernel_ptables, virt, size, name); | ||
| __ASSERT(ret == 0, "privatize_page_range() returned %d", ret); | ||
| ret = add_map(ptables, name, phys, virt, size, attrs | MT_NG); | ||
| __ASSERT(ret == 0, "add_map() returned %d", ret); | ||
| if (ret != 0) { | ||
| return ret; | ||
| } | ||
|
|
||
| key = k_spin_lock(&xlat_lock); | ||
| ret = remap_mapping(ptables->base_xlat_table, virt, size, attr_desc, | ||
| BASE_XLAT_LEVEL); | ||
| k_spin_unlock(&xlat_lock, key); | ||
|
|
There was a problem hiding this comment.
Failed partition addition leaves partial PTE permissions and un-reverted partition slot
🟡 medium · error handling
When private_map() / remap_mapping() fails partway through a range with -ENOMEM, neither the page table changes already made nor the partition slot recorded by k_mem_domain_add_partition() are rolled back:
remap_mapping()updates leaf PTEs in place (*pte = (*pte & ~mask) | (attr_desc & mask)) as it iterates across[virt, virt + size). If an entry later in the range is a partial block whoseexpand_to_table()fails,remap_mapping()returns-ENOMEMimmediately while earlier entries in the range keep the new user permissions.k_mem_domain_add_partition()(inkernel/userspace/mem_domain.c:243-251) writesdomain->partitions[p_idx]and incrementsdomain->num_partitionsbefore callingarch_mem_domain_partition_add(domain, p_idx), without rolling them back whenret != 0(unlike the rollback added fork_mem_domain_add_thread()in commit cb0c333).
Trigger: 1. A multi-entry partition (e.g., spanning an already-split page table or full block followed by a partial kernel block) is added via k_mem_domain_add_partition() when the xlat table pool runs out on the partial block split in remap_mapping(), OR
2. Any partition addition fails with -ENOMEM in arch_mem_domain_partition_add() (as in test_partition_over_block_without_tables) and the caller subsequently retries k_mem_domain_add_partition() after freeing translation tables.
Impact: When adding a partition fails with -ENOMEM, (1) any earlier translation entries in the range that did not require splitting remain mapped with the partition's user-accessible attributes in the domain's page tables despite k_mem_domain_add_partition() reporting failure, and (2) domain->partitions[p_idx] and domain->num_partitions remain populated, consuming a partition slot and causing a subsequent retry of k_mem_domain_add_partition() to fail with -EINVAL in check_add_partition() due to overlap with the ghost entry.
Recommendation: On failure in private_map() (when remap_mapping() fails), revert the range back to the kernel mappings (e.g., via reset_map(ptables, name, virt, virt_failed - virt)) or ensure all required table splits succeed before applying attributes. In k_mem_domain_add_partition(), if arch_mem_domain_partition_add() returns non-zero, clear domain->partitions[p_idx].size = 0U and decrement domain->num_partitions.
Evidence: arch/arm64/core/mmu.c:1593-1672 (remap_mapping modifies leaf PTEs in-place as it walks the range and returns -ENOMEM on the first expand_to_table failure without reverting already-modified PTEs; private_map returns that error directly); kernel/userspace/mem_domain.c:64-81, 243-257 (k_mem_domain_add_partition populates domain->partitions[p_idx] and increments domain->num_partitions before calling arch_mem_domain_partition_add, and does not roll them back on error, causing check_add_partition to reject any retry as overlapping); tests/arch/arm64/arm64_mmu/src/domain_attrs.c:259-287, 315-351 (test_partition_over_block_without_tables only tests a single-page partition and does not test retrying after releasing tables, unlike test_thread_move_without_tables). [C2]
AI review, advisory; verify before acting on it.
The descriptors are what tell a domain-private mapping apart from the kernel entry it was built over: which bits came from the partition, which were kept from the entry, and what the entry maps. Nothing in this file exposes them, so a test can only observe the result indirectly, by arranging an access and seeing whether it faults. Add arm64_mmu_pte_get(), alongside the existing table accounting hooks, walking a set of tables down to the leaf covering an address and reporting it with its level. Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
arch_mem_domain_partition_add() and map_thread_stack() both create the domain private mapping with phys == virt, ignoring the entry the kernel holds for that range. That assumes every partition is identity mapped and always resident, and neither is guaranteed. With demand paging, a range that is paged out when the partition is added still gets a valid, access flagged, user permitted entry in the domain, pointing at whatever frame sits at that physical address. The kernel has already returned that frame to the free pool, so the domain reads and writes memory that belongs to whoever gets it next, while the kernel still considers the page evicted. Threads in the domain also miss the page fault that would have brought the real contents back. Apply the partition attributes to the entries the range already has instead. Descriptor type, output address or paging location token, and the access flag stay with the entry, so a paged out page stays paged out and faults in normally, and a mapping whose virtual address is not its physical address keeps pointing where it should. Ranges with nothing mapped are left alone: access can only be granted to memory that is actually mapped. Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
A domain-private mapping is built over whatever the kernel already maps for the range, and which bits come from the partition and which are kept from the entry is a contract nothing checks. The existing suite covers table accounting and runs without userspace, so none of it reaches this. Add a second suite, built in a userspace scenario of its own since the accounting assertions only hold without memory domains, checking against the descriptors themselves: the output address stays the kernel's physical address rather than becoming the virtual one, the memory type and shareability stay those of the entry, a read-only partition is read-only and not marked software-writable, and a partition over a range nothing maps leaves the domain with no entry at all. Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
Evict a page, give the freed frame content of its own through a kernel only mapping, then add a partition over the evicted range and read it from a thread in the domain. The read must fault, page the range back in and return what was there before, not the other mapping's content. Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
globalize_table() asserts the source entry is a table whenever the range stops in the middle of it. That holds when a range is globalized at the granularity the kernel mapped it with, and not when a partition covers only part of a range the kernel mapped as a block: reset_map() then walks the partition's range against the kernel tables, finds a block spanning more than the range, and the assertion fires on partition removal. Split the source the way the destination is already split a few lines down, which is also what set_mapping() does when a new mapping lands inside an existing block. The mappings are unchanged, only the granularity they are held at. Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
A partition need not line up with what the kernel mapped: declaring one page inside a region the kernel holds as a block means the domain has to split the block and grant access to the covered page alone. Check both ends of that. Adding the partition splits the block in the domain, gives the covered page EL0 access at the right output address, leaves the rest of the block without it, and leaves the kernel's own block whole. Removing it puts the domain back on the kernel entry. Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
remap_mapping() splits a block when the range covers only part of it, and ignored expand_to_table() returning NULL when the translation table pool is empty. The attributes then landed on the whole block, granting the domain access to everything the block maps rather than to the partition, and the caller was told the partition had been added. Return -ENOMEM instead and let it out through private_map(), which the callers already propagate. privatize_page_range()'s error is passed on the same way rather than merely asserted, now that there is somewhere for it to go. arch_mem_domain_thread_add() did not: it overwrote a map_thread_stack() failure with the result of reset_map() and moved the thread to tables where its stack is not mapped for user access. Return the error before switching instead, leaving the thread on its old tables. Once the thread runs on the new tables the move is done: if reset_map() fails then, the old domain still maps the stack for user access, so revoke that in place, which needs no new table since the private entries already cover the stack. Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
Take the translation table pool down to nothing, then add a partition covering part of a block. The split cannot happen, so the add must fail and the rest of the block must stay out of reach of the domain. Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
add_thread_locked() links the thread into the new domain's list and points mem_domain_info.mem_domain at it before calling arch_mem_domain_thread_add(), which may fail for lack of translation tables. k_mem_domain_add_thread() left both in place when it did, so the thread was recorded in a domain whose memory configuration it never got, and a retry was a no-op since the thread already looked like a member. Put the thread back in its old domain when the move fails, and state in the arch_mem_domain_thread_add() documentation that the architecture leaves the thread configured for that domain in that case. Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
Exhaust the translation table pool and move a user thread to another domain: the move fails, and the thread keeps both the page tables and the domain membership it had, with its stack still mapped there. Once tables are available again, the same call moves it, which a thread left recorded in the new domain would never get. The table pool exhaustion is factored out of the partition test that already does it. Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
7969190 to
780f713
Compare
| if (reset_map(old_ptables, __func__, thread->stack_info.start, | ||
| thread->stack_info.size) != 0) { | ||
| (void)private_map(old_ptables, "thread_stack", | ||
| thread->stack_info.start, | ||
| thread->stack_info.size, | ||
| MT_P_RW_U_NA | MT_NORMAL); | ||
| } |
There was a problem hiding this comment.
reset_map() internal assertion prevents the new migration fallback from running in assert builds · 🔵 low
arch_mem_domain_thread_add() now checks reset_map() for failure and falls back to revoking user access in place with private_map(), but reset_map() still contains __ASSERT(ret == 0, ...) internally (unlike private_map(), where the assertion on privatize_page_range() was removed). Happens when migrating a user thread when the old domain's stack range requires splitting a kernel block in globalize_table() and the translation table pool is exhausted, with CONFIG_ASSERT=y. With CONFIG_ASSERT=y enabled, a reset_map() failure during thread migration triggers an assertion panic instead of running the new fallback that revokes user access in place.
Fix: Remove the __ASSERT(ret == 0, ...) inside reset_map() (or move it to callers that do not handle failure) so the fallback path in arch_mem_domain_thread_add() is reachable when assertions are enabled.
AI review, advisory; verify before acting on it.
| ret = remap_mapping(ptables->base_xlat_table, virt, size, attr_desc, | ||
| BASE_XLAT_LEVEL); | ||
| k_spin_unlock(&xlat_lock, key); | ||
|
|
There was a problem hiding this comment.
Failed partition addition leaves partial PTE permissions and un-reverted partition slot · 🟡 medium
When private_map() / remap_mapping() fails partway through a range with -ENOMEM, neither the leaf PTE attribute changes already applied to earlier entries in [virt, virt + size) nor the partition slot recorded by k_mem_domain_add_partition() (domain->partitions[p_idx] and domain->num_partitions) are rolled back. Happens when k_mem_domain_add_partition() fails with -ENOMEM in arch_mem_domain_partition_add() when the translation table pool is exhausted during privatize_page_range() or a partial block split in remap_mapping(), followed by a retry or access to earlier entries in the range. Earlier entries in a multi-entry partition retain user access in the domain's page tables after a failed add, and the un-reverted partition slot prevents retrying k_mem_domain_add_partition() with -EINVAL from check_add_partition().
Fix: Revert any partial attribute changes in private_map() when remap_mapping() fails (or pre-split required tables before modifying attributes), and clear domain->partitions[p_idx].size = 0U and decrement domain->num_partitions in k_mem_domain_add_partition() when arch_mem_domain_partition_add() returns an error.
AI review, advisory; verify before acting on it.
| if (ret != 0) { | ||
| /* Stay on the old tables, where the stack is mapped */ | ||
| return ret; | ||
| } | ||
| } |
There was a problem hiding this comment.
Failed thread migration leaves partial EL0 stack mappings in target domain · 🟡 medium
When map_thread_stack() fails during thread migration, arch_mem_domain_thread_add() returns early without reverting changes already made to domain_ptables. If privatize_page_range() or remap_mapping() fails partway through the stack range, duplicated tables and MT_P_RW_U_RW leaf entries applied to earlier pages remain active in the target domain's page tables even though the thread stays in old_domain. Happens when migrating a user thread whose stack spans multiple translation entries into another domain when the translation table pool runs out after privatizing or remapping the first part of the stack range. The target domain retains leaked private translation tables and EL0 read/write access to part of a stack belonging to a thread that remained in its original domain.
Fix: Revert any partial changes in domain_ptables (or in private_map() on error) when map_thread_stack() fails so no private tables or user permissions for the stack are left behind in the target domain.
AI review, advisory; verify before acting on it.
|


