From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 61378C43334 for ; Mon, 27 Jun 2022 15:55:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237335AbiF0Pzo (ORCPT ); Mon, 27 Jun 2022 11:55:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57620 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236563AbiF0Pzn (ORCPT ); Mon, 27 Jun 2022 11:55:43 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 1120026F1 for ; Mon, 27 Jun 2022 08:55:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1656345341; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=HuLM69wVueZYYIAkbfqpnLdfUqCDZ1pi8iyqjjngfEM=; b=hqlq9dz/afRjmgRiYQ0+f0iDo77pfK9gNCP5k3O+Ise0ED2UgRrmA/+9SN+eIdOEMqcG0x fgVhSadh3wnk+w7CLUHzY9WLyAahxQ82/w437CA89OU71sxFDMSvAvnohRLIFsN0OivAHb EilMHdZUAiY50JCEgNH7yIlK5Xbsdus= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-296-xBsmTYLoN0ep9wZPhvZvJA-1; Mon, 27 Jun 2022 11:55:36 -0400 X-MC-Unique: xBsmTYLoN0ep9wZPhvZvJA-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D72CE3C11E6A; Mon, 27 Jun 2022 15:55:35 +0000 (UTC) Received: from localhost (unknown [10.39.192.87]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 709C5492CA3; Mon, 27 Jun 2022 15:55:35 +0000 (UTC) From: Cornelia Huck To: Steven Price , Catalin Marinas , Peter Collingbourne Cc: kvmarm@lists.cs.columbia.edu, Marc Zyngier , kvm@vger.kernel.org, Andy Lutomirski , linux-arm-kernel@lists.infradead.org, Michael Roth , Chao Peng , Will Deacon , Evgenii Stepanov Subject: Re: [PATCH] KVM: arm64: permit MAP_SHARED mappings with MTE enabled In-Reply-To: <14f2a69e-4022-e463-1662-30032655e3d1@arm.com> Organization: Red Hat GmbH References: <20220623234944.141869-1-pcc@google.com> <14f2a69e-4022-e463-1662-30032655e3d1@arm.com> User-Agent: Notmuch/0.36 (https://notmuchmail.org) Date: Mon, 27 Jun 2022 17:55:33 +0200 Message-ID: <875ykmcd8q.fsf@redhat.com> MIME-Version: 1.0 Content-Type: text/plain X-Scanned-By: MIMEDefang 2.85 on 10.11.54.9 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org [I'm still in the process of trying to grok the issues surrounding MTE+KVM, so apologies in advance if I'm muddying the waters] On Sat, Jun 25 2022, Steven Price wrote: > On 24/06/2022 18:05, Catalin Marinas wrote: >> + 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). Which VMMs support KVM+MTE so far? (I'm looking at adding support in QEMU.) >> >>> 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); >>> + } > > I'm pretty sure we need some form of barrier in here to ensure the tag > clearing is visible to the other CPU. Otherwise I can't immediately see > any problems with the approach of a second flag (it was something I had > considered). But I do also think we should seriously consider Catalin's > approach of simply zeroing tags unconditionally - it would certainly > simplify the code. What happens in kvm_vm_ioctl_mte_copy_tags()? I think we would just end up copying zeroes? That said, do we make any assumptions about when KVM_ARM_MTE_COPY_TAGS will be called? I.e. when implementing migration, it should be ok to call it while the vm is paused, but you probably won't get a consistent state while the vm is running? [Postcopy needs a different interface, I guess, so that the migration target can atomically place a received page and its metadata. I see https://lore.kernel.org/all/CAJc+Z1FZxSYB_zJit4+0uTR-88VqQL+-01XNMSEfua-dXDy6Wg@mail.gmail.com/; has there been any follow-up?] From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mm01.cs.columbia.edu (mm01.cs.columbia.edu [128.59.11.253]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8794AC43334 for ; Mon, 27 Jun 2022 15:55:47 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id D97AD4B22C; Mon, 27 Jun 2022 11:55:46 -0400 (EDT) X-Virus-Scanned: at lists.cs.columbia.edu Authentication-Results: mm01.cs.columbia.edu (amavisd-new); dkim=softfail (fail, message has been altered) header.i=@redhat.com Received: from mm01.cs.columbia.edu ([127.0.0.1]) by localhost (mm01.cs.columbia.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id iMojBhRCLTuC; Mon, 27 Jun 2022 11:55:45 -0400 (EDT) Received: from mm01.cs.columbia.edu (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 725B44B236; Mon, 27 Jun 2022 11:55:45 -0400 (EDT) Received: from localhost (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 05F2D4B208 for ; Mon, 27 Jun 2022 11:55:45 -0400 (EDT) X-Virus-Scanned: at lists.cs.columbia.edu Received: from mm01.cs.columbia.edu ([127.0.0.1]) by localhost (mm01.cs.columbia.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id OviPVndQhOQG for ; Mon, 27 Jun 2022 11:55:43 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 804874B1ED for ; Mon, 27 Jun 2022 11:55:43 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1656345343; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=HuLM69wVueZYYIAkbfqpnLdfUqCDZ1pi8iyqjjngfEM=; b=PrhxOpHbxrNBQmQkPNaeO08fkInYZi8CLv7Mg0vb9yUwS3mZMreY09X7XoJgXdzAr6vDFK 8Nt/IKdAqKleM8XnnTp8B0IMcfUz7Wpc5UpBFHkUilYm6xAK9hgWOqKZG0ozqEOlrlcPdY scySTgfLFSE1yJT15VGpWlI8LZS2Tjk= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-296-xBsmTYLoN0ep9wZPhvZvJA-1; Mon, 27 Jun 2022 11:55:36 -0400 X-MC-Unique: xBsmTYLoN0ep9wZPhvZvJA-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D72CE3C11E6A; Mon, 27 Jun 2022 15:55:35 +0000 (UTC) Received: from localhost (unknown [10.39.192.87]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 709C5492CA3; Mon, 27 Jun 2022 15:55:35 +0000 (UTC) From: Cornelia Huck To: Steven Price , Catalin Marinas , Peter Collingbourne Subject: Re: [PATCH] KVM: arm64: permit MAP_SHARED mappings with MTE enabled In-Reply-To: <14f2a69e-4022-e463-1662-30032655e3d1@arm.com> Organization: Red Hat GmbH References: <20220623234944.141869-1-pcc@google.com> <14f2a69e-4022-e463-1662-30032655e3d1@arm.com> User-Agent: Notmuch/0.36 (https://notmuchmail.org) Date: Mon, 27 Jun 2022 17:55:33 +0200 Message-ID: <875ykmcd8q.fsf@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.85 on 10.11.54.9 Cc: kvm@vger.kernel.org, Marc Zyngier , Andy Lutomirski , Evgenii Stepanov , Michael Roth , Chao Peng , Will Deacon , kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org X-BeenThere: kvmarm@lists.cs.columbia.edu X-Mailman-Version: 2.1.14 Precedence: list List-Id: Where KVM/ARM decisions are made List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: kvmarm-bounces@lists.cs.columbia.edu Sender: kvmarm-bounces@lists.cs.columbia.edu [I'm still in the process of trying to grok the issues surrounding MTE+KVM, so apologies in advance if I'm muddying the waters] On Sat, Jun 25 2022, Steven Price wrote: > On 24/06/2022 18:05, Catalin Marinas wrote: >> + 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). Which VMMs support KVM+MTE so far? (I'm looking at adding support in QEMU.) >> >>> 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); >>> + } > > I'm pretty sure we need some form of barrier in here to ensure the tag > clearing is visible to the other CPU. Otherwise I can't immediately see > any problems with the approach of a second flag (it was something I had > considered). But I do also think we should seriously consider Catalin's > approach of simply zeroing tags unconditionally - it would certainly > simplify the code. What happens in kvm_vm_ioctl_mte_copy_tags()? I think we would just end up copying zeroes? That said, do we make any assumptions about when KVM_ARM_MTE_COPY_TAGS will be called? I.e. when implementing migration, it should be ok to call it while the vm is paused, but you probably won't get a consistent state while the vm is running? [Postcopy needs a different interface, I guess, so that the migration target can atomically place a received page and its metadata. I see https://lore.kernel.org/all/CAJc+Z1FZxSYB_zJit4+0uTR-88VqQL+-01XNMSEfua-dXDy6Wg@mail.gmail.com/; has there been any follow-up?] _______________________________________________ kvmarm mailing list kvmarm@lists.cs.columbia.edu https://lists.cs.columbia.edu/mailman/listinfo/kvmarm From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 3B0FCC433EF for ; Mon, 27 Jun 2022 15:57:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-ID:Date:References :In-Reply-To:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=SEWynR8KKBxmBIVJgPwoqqfbiVNw/JpCcIHYZbBdp8w=; b=wl7b3vfK5ablLK jsXMAdWaGTZ9r6SuV53sV3jZjv8TQBuv6P1s6ZFcTLGYsalSzSpgZ1jIaB3SyppAjHGjRfG6K8Tmk 2suNQ1rbbhIKtICtom+Epsnq2NTO1FivBiB93f9IQYIxXnNuZiXZjfgAy9dvCnuDzBUkequMykoIZ ZLrCewnCkJzoBxnFWbDY5CzIzx8ZR8FiTeB5DE+mDV9GqlCGmucuWF+HCFuKWWCMzgw4qXXTo3SXn USJwbzvBqgpWkvskZO3qSBIA1Mf1sAPmNbwOomDKSlXyfAEMlKhnx6XfMDb5CVdjMGyI7Zp9qi+Kv A7UUUrm8gf2u8k3P6eeQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o5r5Z-001j9S-1k; Mon, 27 Jun 2022 15:55:49 +0000 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1o5r5V-001j7l-FS for linux-arm-kernel@lists.infradead.org; Mon, 27 Jun 2022 15:55:47 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1656345343; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=HuLM69wVueZYYIAkbfqpnLdfUqCDZ1pi8iyqjjngfEM=; b=PrhxOpHbxrNBQmQkPNaeO08fkInYZi8CLv7Mg0vb9yUwS3mZMreY09X7XoJgXdzAr6vDFK 8Nt/IKdAqKleM8XnnTp8B0IMcfUz7Wpc5UpBFHkUilYm6xAK9hgWOqKZG0ozqEOlrlcPdY scySTgfLFSE1yJT15VGpWlI8LZS2Tjk= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-296-xBsmTYLoN0ep9wZPhvZvJA-1; Mon, 27 Jun 2022 11:55:36 -0400 X-MC-Unique: xBsmTYLoN0ep9wZPhvZvJA-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D72CE3C11E6A; Mon, 27 Jun 2022 15:55:35 +0000 (UTC) Received: from localhost (unknown [10.39.192.87]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 709C5492CA3; Mon, 27 Jun 2022 15:55:35 +0000 (UTC) From: Cornelia Huck To: Steven Price , Catalin Marinas , Peter Collingbourne Cc: kvmarm@lists.cs.columbia.edu, Marc Zyngier , kvm@vger.kernel.org, Andy Lutomirski , linux-arm-kernel@lists.infradead.org, Michael Roth , Chao Peng , Will Deacon , Evgenii Stepanov Subject: Re: [PATCH] KVM: arm64: permit MAP_SHARED mappings with MTE enabled In-Reply-To: <14f2a69e-4022-e463-1662-30032655e3d1@arm.com> Organization: Red Hat GmbH References: <20220623234944.141869-1-pcc@google.com> <14f2a69e-4022-e463-1662-30032655e3d1@arm.com> User-Agent: Notmuch/0.36 (https://notmuchmail.org) Date: Mon, 27 Jun 2022 17:55:33 +0200 Message-ID: <875ykmcd8q.fsf@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.85 on 10.11.54.9 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220627_085545_654279_5F6E995E X-CRM114-Status: GOOD ( 31.98 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org [I'm still in the process of trying to grok the issues surrounding MTE+KVM, so apologies in advance if I'm muddying the waters] On Sat, Jun 25 2022, Steven Price wrote: > On 24/06/2022 18:05, Catalin Marinas wrote: >> + 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). Which VMMs support KVM+MTE so far? (I'm looking at adding support in QEMU.) >> >>> 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); >>> + } > > I'm pretty sure we need some form of barrier in here to ensure the tag > clearing is visible to the other CPU. Otherwise I can't immediately see > any problems with the approach of a second flag (it was something I had > considered). But I do also think we should seriously consider Catalin's > approach of simply zeroing tags unconditionally - it would certainly > simplify the code. What happens in kvm_vm_ioctl_mte_copy_tags()? I think we would just end up copying zeroes? That said, do we make any assumptions about when KVM_ARM_MTE_COPY_TAGS will be called? I.e. when implementing migration, it should be ok to call it while the vm is paused, but you probably won't get a consistent state while the vm is running? [Postcopy needs a different interface, I guess, so that the migration target can atomically place a received page and its metadata. I see https://lore.kernel.org/all/CAJc+Z1FZxSYB_zJit4+0uTR-88VqQL+-01XNMSEfua-dXDy6Wg@mail.gmail.com/; has there been any follow-up?] _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel