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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id AA327C43334 for ; Fri, 8 Jul 2022 13:31:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E1E766B0072; Fri, 8 Jul 2022 09:31:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DCE676B0073; Fri, 8 Jul 2022 09:31:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CBD068E0001; Fri, 8 Jul 2022 09:31:15 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id BDF256B0072 for ; Fri, 8 Jul 2022 09:31:15 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay13.hostedemail.com (Postfix) with ESMTP id 8ED6960E48 for ; Fri, 8 Jul 2022 13:31:15 +0000 (UTC) X-FDA: 79664018910.05.78202B0 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf19.hostedemail.com (Postfix) with ESMTP id 7AF5B1A0065 for ; Fri, 8 Jul 2022 13:31:14 +0000 (UTC) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id A4274B82795; Fri, 8 Jul 2022 13:31:12 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C4A80C341C0; Fri, 8 Jul 2022 13:31:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1657287071; bh=6w3FdSt0igiYVeyoAVV3H9/NR70+fhvKe1Xe91adj+I=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=mM4a+/N3Rx8c/SLtqSHXOeVY65YCEVXtvVk3WYaF65ZbiFEwpM4MPLGoTxfDqGjZo LQ7EmceeMpgg3PCuQTgr46JmgvY0axC6kRzrNMXzhDDnb7f3YS6WgHtyvYCKxsolXS b/h0IDiZ+30VPxdULQeOsz1A2594qt4zP8kP3qIdKzU57loh3tFpAmcstaDs371nzC SKhTVqf8UUrhmYkgM4EKXAnjRRERTfCPYCTvNIhaOAOQEDNtB5tFZtsa13QTKwZfYV bcYXB4++K3/IVQ6PpHAVU2+lO6Koh1eoPecnIWx0nenSqf8Jpoyr8DKABdip/7CKOW t6SgkFObx2l8A== Date: Fri, 8 Jul 2022 14:31:06 +0100 From: Will Deacon To: Catalin Marinas Cc: Andrey Ryabinin , Andrey Konovalov , Vincenzo Frascino , Peter Collingbourne , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org Subject: Re: [PATCH v2 0/4] kasan: Fix ordering between MTE tag colouring and page->flags Message-ID: <20220708133105.GB5989@willie-the-truck> References: <20220610152141.2148929-1-catalin.marinas@arm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220610152141.2148929-1-catalin.marinas@arm.com> User-Agent: Mutt/1.10.1 (2018-07-13) ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1657287075; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=8L+LxeI9tbX1E1pK2U36kMjg9if57bZL7WJQAt65A/I=; b=1EzT5E+Cw1xpCoFSpYOnmFQv+tPEpQ+TjJWYVCTdSeTkHVWwkEukl/TDn9U9/V/hAKl1xL lNPpgkIO8DZ4Jt5DOSNDPmshpqbzvtyyF8QNQa1llAjPdqk4f2dYHOHIx3RFrtPBC3x22L mafEg2ZnhrcaMs7L9oz3sJHcHmrdEns= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1657287075; a=rsa-sha256; cv=none; b=UavzoVJbHDwUhYiMd1VA6QXvntBuchLG6CANo1EpJX4pESa4EX4zHNrMhWDz5a20asEs+i 0HDBrohQ5SDESnnS0Qp1+IHqTJ9QRe7wS64/B1z04pomqN7IIPEVZ+3LmHrJLSnHpKUbGB Sb07IEbfVW8JBaCSYuKpDTx9Js2w7UQ= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="mM4a+/N3"; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf19.hostedemail.com: domain of will@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=will@kernel.org X-Stat-Signature: n1j8dqfwjaax9akhrkbgnma31o3un3ee X-Rspamd-Queue-Id: 7AF5B1A0065 Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="mM4a+/N3"; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf19.hostedemail.com: domain of will@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=will@kernel.org X-Rspam-User: X-Rspamd-Server: rspam05 X-HE-Tag: 1657287074-50138 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Fri, Jun 10, 2022 at 04:21:37PM +0100, Catalin Marinas wrote: > Hi, > > That's a second attempt on fixing the race race between setting the > allocation (in-memory) tags in a page and the corresponding logical tag > in page->flags. Initial version here: > > https://lore.kernel.org/r/20220517180945.756303-1-catalin.marinas@arm.com > > This new series does not introduce any new GFP flags but instead always > skips unpoisoning of the user pages (we already skip the poisoning on > free). Any unpoisoned page will have the page->flags tag reset. > > For the background: > > On a system with MTE and KASAN_HW_TAGS enabled, when a page is allocated > kasan_unpoison_pages() sets a random tag and saves it in page->flags so > that page_to_virt() re-creates the correct tagged pointer. We need to > ensure that the in-memory tags are visible before setting the > page->flags: > > P0 (__kasan_unpoison_range): P1 (access via virt_to_page): > Wtags=x Rflags=x > | | > | DMB | address dependency > V V > Wflags=x Rtags=x > > The first patch changes the order of page unpoisoning with the tag > storing in page->flags. page_kasan_tag_set() has the right barriers > through try_cmpxchg(). > > If a page is mapped in user-space with PROT_MTE, the architecture code > will set the allocation tag to 0 and a subsequent page_to_virt() > dereference will fault. We currently try to fix this by resetting the > tag in page->flags so that it is 0xff (match-all, not faulting). > However, setting the tags and flags can race with another CPU reading > the flags (page_to_virt()) and barriers can't help, e.g.: > > P0 (mte_sync_page_tags): P1 (memcpy from virt_to_page): > Rflags!=0xff > Wflags=0xff > DMB (doesn't help) > Wtags=0 > Rtags=0 // fault > > Since clearing the flags in the arch code doesn't work, to do this at > page allocation time when __GFP_SKIP_KASAN_UNPOISON is passed. I've picked this up, thanks. An alternative solution might be to use a seqlock (if you can find somewhere to put it) so that virt_to_page() spins briefly while the tags and flags are being updated. Will