Received: by 2002:a89:48b:0:b0:1f5:f2ab:c469 with SMTP id a11csp390589lqd; Wed, 24 Apr 2024 05:51:17 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWJTAXoPt0PQkd9irHUe9sxymMfjTG5SyRwB42ThNDLPEq4noaxjMG+nwvj08mW46qkQT8M8CP03R+SP+R4+w+Y2ueDD8H/OMqAvfB7CA== X-Google-Smtp-Source: AGHT+IGz/b7tv7rLP95XNoevkLC051Cm/c6UfR08Z5YF1e5XvbDsr09jUSYn1rJhTWdlw4t1WMhr X-Received: by 2002:a05:620a:94a:b0:790:7704:a13d with SMTP id w10-20020a05620a094a00b007907704a13dmr2729409qkw.56.1713963077248; Wed, 24 Apr 2024 05:51:17 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713963077; cv=pass; d=google.com; s=arc-20160816; b=CE79vucfof4d8aDa9AEZ6OxwKVyotsQ+4uzCmqdKvo85CYbIGLyGr+OPxl1KDYknao Px1Y0CLk4G0b1gfEMq6iu+u5XtNYP7JBZXsMKDLGg133F/OB+w61hyNAoCGoDmgjJhpQ 0k7sAFZdCXyjdKyVe7DDYNtaGGjGJVYV6PA650/SphqYKO1H4ttliwc8TGIiNr3xbjgG sHlX/O+K3ZEw5JuEivHMf7zogpwtQrJ5KB+XjxCnB9mTyYRhPHNz1xy6JgpKzDqA3d3p UDvh4RyJPr99Lxt3ht1Doa1YtOtLX73KDPerQYof6VouQfc+PzIsFGBKK1QBL8eNdF+N KSFw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=LZuqWivfWiMlpsjy10fh+mkio/vf0+xtEb87holzRJ0=; fh=MjXEgcOdlX1K6nJSPG8GwGBLQD4llOsUmxXGTkVkRfg=; b=u3oZHBWKimdLa3Z2tGfzDfqmTJspZTK4H5PxiWhalN/mittrDHtyz6MxQH9M9ss9KO XK6cuQVtcLYpfFYZ5RxGErS9BlJYaOecDuWFjAEWsG86dU+VxF2phAnqEqlsIu1Q5cK4 mGvEmxKnuO+vkhy+M/0hmfm7IhpQfwnA4pKU4gYf9JZ/BuoYCO0UUQkmigHwVNMuanFc 7rJ47wlWwPNwclrFxuCNGkh1nIeOSw0GmQbc27CgmxY8XMspDXhRHwm4UAHWbtJpRTfY DXhrOSfXM4WjtHDOyCPDE6Gom+S1PonqEElanEGgFZqXbm4qc6dC74ldsuUWEtv4MXiW e0hw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-156897-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-156897-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id pi21-20020a05620a379500b0079089e693easi2305876qkn.133.2024.04.24.05.51.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Apr 2024 05:51:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-156897-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-156897-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-156897-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id E78AE1C22AF7 for ; Wed, 24 Apr 2024 12:51:16 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 081E615ADA5; Wed, 24 Apr 2024 12:51:07 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 15961142E70 for ; Wed, 24 Apr 2024 12:51:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713963066; cv=none; b=q2nQjrUxNN3LyAFhysFQ8zuxsyFs1Gsw1hjGHceWfhug45px0Utbq3xDK+TQBYMGHTqqJLW7C58NFGhfHC/Brdulgsr6KSt8DWMYNEUXTZmTAhiVC7CD8XqbrhjDj8cnDp22Lozm5dYps7FCSu9j3l1znFxTH2R2mcQiTaFzGJw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713963066; c=relaxed/simple; bh=NOwW3MgYsf5xxcX3LQ1KN0/iiQwL52NsEkwLHO90zLY=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=UeImaoZP8wb8Y2sKcUJGPtbc+PB/+kZuVVPHVv+/mqC1ki4Q5YFfhWNMHAPJV6qYqbFbMsPu3PV1JGFdJZ1O9MBcWxX/RZ+c2LuCOFiaMOLYRj+lFUKoPwdT3ZTcQBXPG6t70+FXetdnKo7euNvfXghnfFkRhEEQLTeOHVVp12E= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 0A95A1516; Wed, 24 Apr 2024 05:51:32 -0700 (PDT) Received: from [10.1.25.156] (XHFQ2J9959.cambridge.arm.com [10.1.25.156]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id C951A3F64C; Wed, 24 Apr 2024 05:51:02 -0700 (PDT) Message-ID: <6d8392e5-7f12-43e5-9631-d1fdc4bf1025@arm.com> Date: Wed, 24 Apr 2024 13:51:01 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v1 2/2] arm64/mm: Add uffd write-protect support Content-Language: en-GB To: Peter Xu Cc: Catalin Marinas , Will Deacon , Joey Gouly , Ard Biesheuvel , Mark Rutland , Anshuman Khandual , David Hildenbrand , Mike Rapoport , Shivansh Vij , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org References: <20240424111017.3160195-1-ryan.roberts@arm.com> <20240424111017.3160195-3-ryan.roberts@arm.com> From: Ryan Roberts In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 24/04/2024 12:57, Peter Xu wrote: > Hi, Ryan, > > On Wed, Apr 24, 2024 at 12:10:17PM +0100, Ryan Roberts wrote: >> Let's use the newly-free PTE SW bit (58) to add support for uffd-wp. >> >> The standard handlers are implemented for set/test/clear for both pte >> and pmd. Additionally we must also track the uffd-wp state as a pte swp >> bit, so use a free swap entry pte bit (3). >> >> Signed-off-by: Ryan Roberts > > Looks all sane here from userfault perspective, just one comment below. > >> --- >> arch/arm64/Kconfig | 1 + >> arch/arm64/include/asm/pgtable-prot.h | 8 ++++ >> arch/arm64/include/asm/pgtable.h | 55 +++++++++++++++++++++++++++ >> 3 files changed, 64 insertions(+) >> >> diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig >> index 7b11c98b3e84..763e221f2169 100644 >> --- a/arch/arm64/Kconfig >> +++ b/arch/arm64/Kconfig >> @@ -255,6 +255,7 @@ config ARM64 >> select SYSCTL_EXCEPTION_TRACE >> select THREAD_INFO_IN_TASK >> select HAVE_ARCH_USERFAULTFD_MINOR if USERFAULTFD >> + select HAVE_ARCH_USERFAULTFD_WP if USERFAULTFD >> select TRACE_IRQFLAGS_SUPPORT >> select TRACE_IRQFLAGS_NMI_SUPPORT >> select HAVE_SOFTIRQ_ON_OWN_STACK >> diff --git a/arch/arm64/include/asm/pgtable-prot.h b/arch/arm64/include/asm/pgtable-prot.h >> index ef952d69fd04..f1e1f6306e03 100644 >> --- a/arch/arm64/include/asm/pgtable-prot.h >> +++ b/arch/arm64/include/asm/pgtable-prot.h >> @@ -20,6 +20,14 @@ >> #define PTE_DEVMAP (_AT(pteval_t, 1) << 57) >> #define PTE_PROT_NONE (PTE_UXN) /* Reuse PTE_UXN; only when !PTE_VALID */ >> >> +#ifdef CONFIG_HAVE_ARCH_USERFAULTFD_WP >> +#define PTE_UFFD_WP (_AT(pteval_t, 1) << 58) /* uffd-wp tracking */ >> +#define PTE_SWP_UFFD_WP (_AT(pteval_t, 1) << 3) /* only for swp ptes */ >> +#else >> +#define PTE_UFFD_WP (_AT(pteval_t, 0)) >> +#define PTE_SWP_UFFD_WP (_AT(pteval_t, 0)) >> +#endif /* CONFIG_HAVE_ARCH_USERFAULTFD_WP */ >> + >> /* >> * This bit indicates that the entry is present i.e. pmd_page() >> * still points to a valid huge page in memory even if the pmd >> diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h >> index 23aabff4fa6f..3f4748741fdb 100644 >> --- a/arch/arm64/include/asm/pgtable.h >> +++ b/arch/arm64/include/asm/pgtable.h >> @@ -271,6 +271,34 @@ static inline pte_t pte_mkdevmap(pte_t pte) >> return set_pte_bit(pte, __pgprot(PTE_DEVMAP | PTE_SPECIAL)); >> } >> >> +#ifdef CONFIG_HAVE_ARCH_USERFAULTFD_WP >> +static inline int pte_uffd_wp(pte_t pte) >> +{ >> + bool wp = !!(pte_val(pte) & PTE_UFFD_WP); >> + >> +#ifdef CONFIG_DEBUG_VM >> + /* >> + * Having write bit for wr-protect-marked present ptes is fatal, because >> + * it means the uffd-wp bit will be ignored and write will just go >> + * through. See comment in x86 implementation. >> + */ >> + WARN_ON_ONCE(wp && pte_write(pte)); >> +#endif > > Feel free to drop this line, see: > > https://lore.kernel.org/r/20240417212549.2766883-1-peterx@redhat.com Ahh nice! In that case, I'm going to convert this to a macro, which is the arm64 style for these getters (for some reason...): #define pte_uffd_wp(pte_t pte) (!!(pte_val(pte) & PTE_UFFD_WP)) Will send out a v2 once others have had time to comment. > > It's still in mm-unstable only. > > AFAICT ARM64 also is supported by check_page_table, I also checked ARM's > ptep_modify_prot_commit() which uses set_pte_at(), so it should cover > everything in a superior way already. > > With that dropped, feel free to add: > > Acked-by: Peter Xu Thanks! > > Thanks, >