Page MenuHomeFreeBSD

vm: Support swapping arm64 MTE tags
Needs ReviewPublic

Authored by andrew on Mar 19 2026, 4:06 PM.
Tags
None
Referenced Files
Unknown Object (File)
Sun, Jul 5, 11:05 PM
Unknown Object (File)
Sat, Jul 4, 3:14 PM
Unknown Object (File)
Sat, Jun 27, 6:11 AM
Unknown Object (File)
Thu, Jun 25, 11:44 AM
Unknown Object (File)
Thu, Jun 25, 5:57 AM
Unknown Object (File)
Wed, Jun 24, 10:22 PM
Unknown Object (File)
Tue, Jun 23, 1:00 AM
Unknown Object (File)
Mon, Jun 22, 6:41 AM
Subscribers

Details

Reviewers
manu
alc
kib
markj
Group Reviewers
arm64
cheri
Summary

When we swap out a page containing MTE tags we need to keep a copy of
them to be restored when the page is later restored. Add support to
allocate per-page tag storage that is used when a MTE tagged page is
moved to swap and populate this with the pages tags.

Sponsored by: Arm Ltd

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Skipped
Unit
Tests Skipped
Build Status
Buildable 73736
Build 70619: arc lint + arc unit

Event Timeline

Is this something cheri could use too?

In CheriBSD we have a somewhat different mechanism where we embed the tags in struct swblk due to a hesitance to add more allocation to the swap path. That's not great for memory usage since it's quite a lot of space and it should be fairly compressible in most cases. I'd expect MTE to be before more compressible given it's often going to be runs of the same value. I think we could adapt this code to add CHERI tags, likely in parallel, though maybe with a shared scan loop.

In CheriBSD we have a somewhat different mechanism where we embed the tags in struct swblk due to a hesitance to add more allocation to the swap path. That's not great for memory usage since it's quite a lot of space and it should be fairly compressible in most cases. I'd expect MTE to be before more compressible given it's often going to be runs of the same value. I think we could adapt this code to add CHERI tags, likely in parallel, though maybe with a shared scan loop.

struct swblk is a fixed size, so compressing tags there won't affect memory usage. I put the tags in a separate allocation as I expect the common case is no MTE in the immediate future, so the extra space will be wasted. In CheriBSD it's different as it's more likely there will be tags in a given page.

I didn't want to compress the tags yet as it's more likely the code would be incorrect as it will likely need to allocate a number of smaller fixed-sized regions to store them in. I don't have any numbers on the size of these regions as we still need userspace support, e.g. ASAN with MTE.

Rebase & use a pointer in mte_{load,save}_tags

sys/vm/swap_pager.c
782

Where does 32 come from? It should be defined in some arm64 header I suspect.

784

Yes, it should be n * SWAP_META_PAGES: in the worst case you allocate one tag buffer per page in each swap block.

785

Shouldn't we only reserve KVA if we know that MTE will be enabled? Or is this still too early during boot?

786
1576

The object lock isn't held here, nothing is synchronizing the lookup. I think you'll have to do something like, lock the object and look up the swap block, if there is no tag buffer then drop the lock and allocate one, then retry the lookup.

It might be profitable to first scan the page to see whether the page has any tags which need saving.

1578

How is this flag synchronized? The page busy lock?

1595
  • Fix an inline asm constraint
  • Use mte_enabled to decide to allocate the MTE swap zone
  • Call swblk_lookup with the object lock
  • Rebase to use PV_MTE_CLEAN & atomically check the flag

i object against this code structure on principle.

  • swp_pager_meta_archXXX should go into the arm64/arm64/somewhere.c. All of them should become arch hooks that do not add any #ifdefs to the sys/vm sources.
  • If you absolutely need the extension of the struct swblk, it should be done in a way similar to how struct vm_page gets it MD members.

That said, I believe there was some idea circulating about adding hooks to the page lifetime. The motivation for it was e.g. support for demand-paging and page fault events from IOMMUs. Did you considered defining the KPI for hooks on page insertion and removal from the objects, that would allow you to do this stuff without such pollution of the VM code?

In D55957#1329686, @kib wrote:

i object against this code structure on principle.

  • swp_pager_meta_archXXX should go into the arm64/arm64/somewhere.c. All of them should become arch hooks that do not add any #ifdefs to the sys/vm sources.
  • If you absolutely need the extension of the struct swblk, it should be done in a way similar to how struct vm_page gets it MD members.

The space required to store tags is quite large, and we do not want to impose that on all platforms, so this would need to be done with some indirection.

That said, I believe there was some idea circulating about adding hooks to the page lifetime. The motivation for it was e.g. support for demand-paging and page fault events from IOMMUs. Did you considered defining the KPI for hooks on page insertion and removal from the objects, that would allow you to do this stuff without such pollution of the VM code?

Do you mean that the tag metadata should be associated with the VM object, rather than the swap block?

In D55957#1329686, @kib wrote:

i object against this code structure on principle.

  • swp_pager_meta_archXXX should go into the arm64/arm64/somewhere.c. All of them should become arch hooks that do not add any #ifdefs to the sys/vm sources.
  • If you absolutely need the extension of the struct swblk, it should be done in a way similar to how struct vm_page gets it MD members.

The space required to store tags is quite large, and we do not want to impose that on all platforms, so this would need to be done with some indirection.

That said, I believe there was some idea circulating about adding hooks to the page lifetime. The motivation for it was e.g. support for demand-paging and page fault events from IOMMUs. Did you considered defining the KPI for hooks on page insertion and removal from the objects, that would allow you to do this stuff without such pollution of the VM code?

Do you mean that the tag metadata should be associated with the VM object, rather than the swap block?

Might be, I did not tried to design the hooks yet.
Lets try to discuss the draft at D58018.