Re: [PATCH] KVM: arm64: permit MAP_SHARED mappings with MTE enabled

2022-06-24 Thread Catalin Marinas
+ Steven as he added the KVM and swap support for MTE.

On Thu, Jun 23, 2022 at 04:49:44PM -0700, Peter Collingbourne wrote:
> Certain VMMs such as crosvm have features (e.g. sandboxing, pmem) that
> depend on being able to map guest memory as MAP_SHARED. The current
> restriction on sharing MAP_SHARED pages with the guest is preventing
> the use of those features with MTE. Therefore, remove this restriction.

We already have some corner cases where the PG_mte_tagged logic fails
even for MAP_PRIVATE (but page shared with CoW). Adding this on top for
KVM MAP_SHARED will potentially make things worse (or hard to reason
about; for example the VMM sets PROT_MTE as well). I'm more inclined to
get rid of PG_mte_tagged altogether, always zero (or restore) the tags
on user page allocation, copy them on write. For swap we can scan and if
all tags are 0 and just skip saving them.

Another aspect is a change in the KVM ABI with this patch. It's probably
not that bad since it's rather a relaxation but it has the potential to
confuse the VMM, especially as it doesn't know whether it's running on
older kernels or not (it would have to probe unless we expose this info
to the VMM in some other way).

> To avoid races between multiple tasks attempting to clear tags on the
> same page, introduce a new page flag, PG_mte_tag_clearing, and test-set it
> atomically before beginning to clear tags on a page. If the flag was not
> initially set, spin until the other task has finished clearing the tags.

TBH, I can't mentally model all the corner cases, so maybe a formal
model would help (I can have a go with TLA+, though not sure when I find
a bit of time this summer). If we get rid of PG_mte_tagged altogether,
this would simplify things (hopefully).

As you noticed, the problem is that setting PG_mte_tagged and clearing
(or restoring) the tags is not an atomic operation. There are places
like mprotect() + CoW where one task can end up with stale tags. Another
is shared memfd mappings if more than one mapping sets PROT_MTE and
there's the swap restoring on top.

> diff --git a/arch/arm64/kernel/mte.c b/arch/arm64/kernel/mte.c
> index f6b00743c399..8f9655053a9f 100644
> --- a/arch/arm64/kernel/mte.c
> +++ b/arch/arm64/kernel/mte.c
> @@ -57,7 +57,18 @@ static void mte_sync_page_tags(struct page *page, pte_t 
> old_pte,
>* the new page->flags are visible before the tags were updated.
>*/
>   smp_wmb();
> - mte_clear_page_tags(page_address(page));
> + mte_ensure_page_tags_cleared(page);
> +}
> +
> +void mte_ensure_page_tags_cleared(struct page *page)
> +{
> + if (test_and_set_bit(PG_mte_tag_clearing, &page->flags)) {
> + while (!test_bit(PG_mte_tagged, &page->flags))
> + ;
> + } else {
> + mte_clear_page_tags(page_address(page));
> + set_bit(PG_mte_tagged, &page->flags);
> + }
>  }

mte_sync_tags() already sets PG_mte_tagged prior to clearing the page
tags. The reason was so that multiple concurrent set_pte_at() would not
all rush to clear (or restore) the tags. But we do have the risk of one
thread accessing the page with the stale tags (copy_user_highpage() is
worse as the tags would be wrong in the destination page). I'd rather be
consistent everywhere with how we set the flags.

However, I find it easier to reason about if we used the new flag as a
lock. IOW, if PG_mte_tagged is set, we know that tags are valid. If not
set, take the PG_mte_locked flag, check PG_mte_tagged again and
clear/restore the tags followed by PG_mte_tagged (and you can use
test_and_set_bit_lock() for the acquire semantics).

It would be interesting to benchmark the cost of always zeroing the tags
on allocation and copy when MTE is not in use:

diff --git a/arch/arm64/mm/copypage.c b/arch/arm64/mm/copypage.c
index 0dea80bf6de4..d31708886bf9 100644
--- a/arch/arm64/mm/copypage.c
+++ b/arch/arm64/mm/copypage.c
@@ -21,7 +21,7 @@ void copy_highpage(struct page *to, struct page *from)
 
copy_page(kto, kfrom);
 
-   if (system_supports_mte() && test_bit(PG_mte_tagged, &from->flags)) {
+   if (system_supports_mte()) {
set_bit(PG_mte_tagged, &to->flags);
page_kasan_tag_reset(to);
/*
diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c
index c5e11768e5c1..b42cad9b9349 100644
--- a/arch/arm64/mm/fault.c
+++ b/arch/arm64/mm/fault.c
@@ -913,12 +913,7 @@ struct page *alloc_zeroed_user_highpage_movable(struct 
vm_area_struct *vma,
 {
gfp_t flags = GFP_HIGHUSER_MOVABLE | __GFP_ZERO;
 
-   /*
-* If the page is mapped with PROT_MTE, initialise the tags at the
-* point of allocation and page zeroing as this is usually faster than
-* separate DC ZVA and STGM.
-*/
-   if (vma->vm_flags & VM_MTE)
+   if (system_supports_mte())
flags |= __GFP_ZEROTAGS;
 
return alloc_page_vma(flags, vma, vaddr);

If that's negligible, we can hopefully get rid

Re: [PATCH v7 00/22] Support SDEI Virtualization

2022-06-24 Thread Marc Zyngier
Hi Gavin,

On Thu, 23 Jun 2022 07:11:08 +0100,
Gavin Shan  wrote:
> 
> Hi Oliver,
> 
> On 5/27/22 6:02 PM, Gavin Shan wrote:
> > This series intends to virtualize Software Delegated Exception Interface
> > (SDEI), which is defined by DEN0054C (v1.1). It allows the hypervisor to
> > deliver NMI-alike SDEI event to guest and it's needed by Async PF to
> > deliver page-not-present notification from hypervisor to guest. The code
> > and the required qemu changes can be found from:
> > 
> > https://developer.arm.com/documentation/den0054/c
> > https://github.com/gwshan/linux("kvm/arm64_sdei")
> > https://github.com/gwshan/qemu ("kvm/arm64_sdei")
> > 
> > The design is quite strightforward by following the specification. The
> > (SDEI) events are classified into the shared and private ones according
> > to their scope. The shared event is system or VM scoped, but the private
> > event is vcpu scoped. This implementation doesn't support the shared
> > event because all the needed events are private. Besides, the critial
> > events aren't supported by the implementation either. It means all events
> > are normal in terms of priority.
> > 
> > There are several objects (data structures) introduced to help on the
> > event registration, enablement, disablement, unregistration, reset,
> > delivery and handling.
> > 
> >* kvm_sdei_event_handler
> >  SDEI event handler, which is provided through EVENT_REGISTER
> >  hypercall, is called when the SDEI event is delivered from
> >  host to guest.
> > * kvm_sdei_event_context
> >  The saved (preempted) context when SDEI event is delivered
> >  for handling.
> > * kvm_sdei_vcpu
> >  SDEI events and their states.
> > 
> > The patches are organized as below:
> > 
> >PATCH[01-02] Preparatory work to extend smccc_get_argx() and refactor
> > hypercall routing mechanism
> >PATCH[03]Adds SDEI virtualization infrastructure
> >PATCH[04-16] Supports various SDEI hypercalls and event handling
> >PATCH[17]Exposes SDEI capability
> >PATCH[18-19] Support SDEI migration
> >PATCH[20]Adds document about SDEI
> >PATCH[21-22] SDEI related selftest cases
> > 
> > The previous revisions can be found:
> > 
> >v6: 
> > https://lore.kernel.org/lkml/20220403153911.12332-4-gs...@redhat.com/T/
> >v5: 
> > https://lore.kernel.org/kvmarm/20220322080710.51727-1-gs...@redhat.com/
> >v4: 
> > https://lore.kernel.org/kvmarm/20210815001352.81927-1-gs...@redhat.com/
> >v3: 
> > https://lore.kernel.org/kvmarm/20210507083124.43347-1-gs...@redhat.com/
> >v2: 
> > https://lore.kernel.org/kvmarm/20210209032733.6-1-gs...@redhat.com/
> >v1: 
> > https://lore.kernel.org/kvmarm/20200817100531.83045-1-gs...@redhat.com/
> > 
> 
> Copying Oliver's new email address (oliver.up...@linux.dev).
> 
> Please let me know if I need to rebase and repost the series.

My main issue with this series is that it is a solution in search of a
problem. It is only an enabler for Asynchronous Page Fault support,
and:

- as far as I know, the core Linux/arm64 maintainers have no plan to
  support APF. Without it, this is a pointless exercise. And even with
  it, this introduces a Linux specific behaviour in an otherwise
  architectural hypervisor (something I'm quite keen on avoiding)

- It gives an incentive to other hypervisor vendors to add random crap
  to the Linux mm subsystem, which is even worse. At this stage, we
  might as well go back to the Xen PV days altogether.

- I haven't seen any of the KVM/arm64 users actually asking for the
  APF horror, and the cloud vendors I directly asked had no plan to
  use it, and not using it on their x86 systems either

- no performance data nor workloads that could help making an informed
  decision have been disclosed, and the only argument in its favour
  seems to be "but x86 has it" (hardly a compelling one)

Given the above, I don't see how to justify this series, as it has no
purpose on its own, no matter how well written it is.

M.

-- 
Without deviation from the norm, progress is not possible.
___
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm


Re: [PATCH kvm-unit-tests] MAINTAINERS: Change drew's email address

2022-06-24 Thread Paolo Bonzini
Queued, thanks.

Paolo


___
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm