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 X-Spam-Level: X-Spam-Status: No, score=-10.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7DCCFC433ED for ; Wed, 12 May 2021 17:46:39 +0000 (UTC) Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id C5613613B5 for ; Wed, 12 May 2021 17:46:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C5613613B5 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=arm.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding :Content-Type:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References:Message-ID: Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=J3st2G24qReG/hYYlri816Fvt763zysY83kB40oh/MQ=; b=lMPxBclqk3ckBMYsyTU2ukDBC rGLEiV5V6DumG1G8QS15EbKXApWFJ7VFOgt++hikK/CpXpAniRoZrVOZrZLoQcFOB85oiYIsv419P T8d9zIBjFJVrpI0anpM4FyIfM3yOsszji0/nMTjSpHIeNuyIbPH8KlyZIqPwXLGto3r2KTR7CoVvz S4A1tG4FY1BZdQ5ciTpHI9VxuWN4vsi0QmF25mXCgwLLfKw77iWeqrsZHsle8KTMaSErGgAYXfg50 s9dRX9015LHmIq7gW3fZlxFPAiksAN7wQkgooxB8OjgW/z/ULpwO18gLIKJOaipCJL7GYnI9eYoya LyXGwLinA==; Received: from localhost ([::1] helo=desiato.infradead.org) by desiato.infradead.org with esmtp (Exim 4.94 #2 (Red Hat Linux)) id 1lgsv6-003TcZ-Hj; Wed, 12 May 2021 17:45:16 +0000 Received: from bombadil.infradead.org ([2607:7c80:54:e::133]) by desiato.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lgsv3-003TcE-GU for linux-arm-kernel@desiato.infradead.org; Wed, 12 May 2021 17:45:13 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=In-Reply-To:Content-Type:MIME-Version :References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=NQ+/7dwUI2EnmRTwQDn+9SihmiLYdSvfv6wFwyqU/lE=; b=l2AV/3hkkcKUiCnMIJ/BOuwOk8 s414BxsjpvtsaP3exYqDmRPK/ckxHuZytWSPuxVg3pQLhiZCDCRuojUAi4pPPqApkVWZZL7agIKXj 5Ccnu1iLukBGhZw4h76pOGmV3KF/ojFqEssLxPCv22FxxIevF8HqpxT0AOkdHV5M1z0Z6JZARezLG kQ1JiEuJanwtt1gbcXdEamXulFR//JoABUFPKyLTTDelzM+M5AQUebdyZdC3fJjm1Ag+HGY3ddCZB rhBafiqRdEMt47piYtbLR9Kf30tFixW6AotB1pv2NhyWXKmHygBHZ75sK3F7NAxomPWMVFza7zUEV lR8CpcUg==; Received: from mail.kernel.org ([198.145.29.99]) by bombadil.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lgsv0-00Af40-EW for linux-arm-kernel@lists.infradead.org; Wed, 12 May 2021 17:45:12 +0000 Received: by mail.kernel.org (Postfix) with ESMTPSA id 9D1F9613DF; Wed, 12 May 2021 17:45:05 +0000 (UTC) Date: Wed, 12 May 2021 18:45:03 +0100 From: Catalin Marinas To: Steven Price Cc: Marc Zyngier , Will Deacon , James Morse , Julien Thierry , Suzuki K Poulose , kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Dave Martin , Mark Rutland , Thomas Gleixner , qemu-devel@nongnu.org, Juan Quintela , "Dr. David Alan Gilbert" , Richard Henderson , Peter Maydell , Haibo Xu , Andrew Jones Subject: Re: [PATCH v11 2/6] arm64: kvm: Introduce MTE VM feature Message-ID: <20210512174502.GC12391@arm.com> References: <20210416154309.22129-1-steven.price@arm.com> <20210416154309.22129-3-steven.price@arm.com> <20210428170705.GB4022@arm.com> <329286e8-a8f3-ea1a-1802-58813255a4a5@arm.com> <20210507182538.GF26528@arm.com> <20210510183506.GA10910@arm.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210512_104510_588806_DDC7D53B X-CRM114-Status: GOOD ( 49.78 ) 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 On Wed, May 12, 2021 at 04:46:48PM +0100, Steven Price wrote: > On 10/05/2021 19:35, Catalin Marinas wrote: > > On Fri, May 07, 2021 at 07:25:39PM +0100, Catalin Marinas wrote: > > > On Thu, May 06, 2021 at 05:15:25PM +0100, Steven Price wrote: > > > > On 04/05/2021 18:40, Catalin Marinas wrote: > > > > > On Thu, Apr 29, 2021 at 05:06:41PM +0100, Steven Price wrote: > > > > > > Given the changes to set_pte_at() which means that tags are restored from > > > > > > swap even if !PROT_MTE, the only race I can see remaining is the creation of > > > > > > new PROT_MTE mappings. As you mention an attempt to change mappings in the > > > > > > VMM memory space should involve a mmu notifier call which I think serialises > > > > > > this. So the remaining issue is doing this in a separate address space. > > > > > > > > > > > > So I guess the potential problem is: > > > > > > > > > > > > * allocate memory MAP_SHARED but !PROT_MTE > > > > > > * fork() > > > > > > * VM causes a fault in parent address space > > > > > > * child does a mprotect(PROT_MTE) > > > > > > > > > > > > With the last two potentially racing. Sadly I can't see a good way of > > > > > > handling that. [...] > > Options: > > > > 1. Change the mte_sync_tags() code path to set the flag after clearing > > and avoid reading stale tags. We document that mprotect() on > > MAP_SHARED may lead to tag loss. Maybe we can intercept this in the > > arch code and return an error. > > This is the best option I've come up with so far - but it's not a good > one! We can replace the set_bit() with a test_and_set_bit() to catch the > race after it has occurred - but I'm not sure what we can do about it > then (we've already wiped the data). Returning an error doesn't seem > particularly useful at that point, a message in dmesg is about the best > I can come up with. What I meant about intercepting is on something like arch_validate_flags() to prevent VM_SHARED and VM_MTE together but only for mprotect(), not mmap(). However, arch_validate_flags() is currently called on both mmap() and mprotect() paths. We can't do much in set_pte_at() to prevent the race with only a single bit. > > 2. Figure out some other locking in the core code. However, if > > mprotect() in one process can race with a handle_pte_fault() in > > another, on the same shared mapping, it's not trivial. > > filemap_map_pages() would take the page lock before calling > > do_set_pte(), so mprotect() would need the same page lock. > > I can't see how this is going to work without harming the performance of > non-MTE work. Ultimately we're trying to add some sort of locking for > two (mostly) unrelated processes doing page table operations, which will > hurt scalability. Another option is to have an arch callback to force re-faulting on the pte. That means we don't populate it back after the invalidation in the change_protection() path. We could do this only if the new pte is tagged and the page doesn't have PG_mte_tagged. The faulting path takes the page lock IIUC. Well, at least for stage 1, I haven't thought much about stage 2. > > 3. Use another PG_arch_3 bit as a lock to spin on in the arch code (i.e. > > set it around the other PG_arch_* bit setting). > > This is certainly tempting, although sadly the existing > wait_on_page_bit() is sleeping - so this would either be a literal spin, > or we'd need to implement a new non-sleeping wait mechanism. Yeah, it would have to be a custom spinning mechanism, something like: /* lock the page */ while (test_and_set_bit(PG_arch_3, &page->flags)) smp_cond_load_relaxed(&page->flags, !(VAL & PG_arch_3)); ... /* unlock the page */ clear_bit(PG_arch_3, &page->flags); > 4. Sledgehammer locking in mte_sync_page_tags(), add a spinlock only for > the MTE case where we have to sync tags (see below). What the actual > performance impact of this is I've no idea. It could certainly be bad > if there are a lot of pages with MTE enabled, which sadly is exactly > the case if KVM is used with MTE :( > > --->8---- > diff --git a/arch/arm64/kernel/mte.c b/arch/arm64/kernel/mte.c > index 0d320c060ebe..389ad40256f6 100644 > --- a/arch/arm64/kernel/mte.c > +++ b/arch/arm64/kernel/mte.c > @@ -25,23 +25,33 @@ > u64 gcr_kernel_excl __ro_after_init; > static bool report_fault_once = true; > +static spinlock_t tag_sync_lock; > static void mte_sync_page_tags(struct page *page, pte_t *ptep, bool check_swap, > bool pte_is_tagged) > { > pte_t old_pte = READ_ONCE(*ptep); > + if (!is_swap_pte(old_pte) && !pte_is_tagged) > + return; > + > + spin_lock_irqsave(&tag_sync_lock, flags); > + > + /* Recheck with the lock held */ > + if (test_bit(PG_mte_tagged, &page->flags)) > + goto out; Can we skip the lock if the page already has the PG_mte_tagged set? That's assuming that we set the flag only after clearing the tags. The normal case where mprotect() is called on a page already mapped with PROT_MTE would not be affected. -- Catalin _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel