From patchwork Fri Jul 7 05:33:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anshuman Khandual X-Patchwork-Id: 13304438 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 712E3C0015E for ; Fri, 7 Jul 2023 05:34:18 +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:References:In-Reply-To: Message-Id:Date: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=TKgrxkOiIWPdw/iQUsJ5dZtG7mPIOaL9Mn+y29+UQWQ=; b=Mt4KfTIiopSRR0 Y2+EZxztG068sWXz+ow25p2VBenF9ctQYh/9StHCF0jvayKmEm6+H04z0AOZVN+wSg67khpPuPvGj llG/jpKk8eOa6KjCXa6Xjt3JuWBbpP7EwillWTIXJZRGoWOI+giezc2WD+Xc1kQ9WNHJPxW5ohHD0 6CfFWgNLSCI490kPBFNetOMn1PozXrniKsL4qiR5YVace+ZAgWtrBCnRSmOa2ICdAmG44xuweVREE osxHW8nqo6bBsEcEb3z1T+y8JTpqxpG2KmRnC3nhl+u8DjP+n/z6+Ax/b3jPL42n/msUoUkjszD5g vrlD2GiVtggcWjyF1/Mw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qHe6M-003d8M-2S; Fri, 07 Jul 2023 05:33:54 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qHe6K-003d7W-1N for linux-arm-kernel@lists.infradead.org; Fri, 07 Jul 2023 05:33:54 +0000 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 C82591063; Thu, 6 Jul 2023 22:34:30 -0700 (PDT) Received: from a077893.arm.com (unknown [10.163.48.50]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id ACB703F740; Thu, 6 Jul 2023 22:33:44 -0700 (PDT) From: Anshuman Khandual To: linux-arm-kernel@lists.infradead.org Cc: Anshuman Khandual , Catalin Marinas , Will Deacon , Ryan Roberts , Mark Rutland , Andrew Morton , David Hildenbrand , Jonathan Corbet , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org Subject: [RFC 1/4] arm64/mm: Add SW and HW dirty state helpers Date: Fri, 7 Jul 2023 11:03:28 +0530 Message-Id: <20230707053331.510041-2-anshuman.khandual@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230707053331.510041-1-anshuman.khandual@arm.com> References: <20230707053331.510041-1-anshuman.khandual@arm.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230706_223352_565501_BB468B43 X-CRM114-Status: GOOD ( 12.16 ) 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: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org This factors out low level SW and HW state changes i.e make and clear into separate helpers making them explicit improving readability. This also adds pte_rdonly() helper as well. No functional change is intended. Cc: Catalin Marinas Cc: Will Deacon Cc: linux-arm-kernel@lists.infradead.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Anshuman Khandual --- arch/arm64/include/asm/pgtable.h | 52 ++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index 0bd18de9fd97..fb03be697819 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -103,6 +103,7 @@ static inline pteval_t __phys_to_pte_val(phys_addr_t phys) #define pte_young(pte) (!!(pte_val(pte) & PTE_AF)) #define pte_special(pte) (!!(pte_val(pte) & PTE_SPECIAL)) #define pte_write(pte) (!!(pte_val(pte) & PTE_WRITE)) +#define pte_rdonly(pte) (!!(pte_val(pte) & PTE_RDONLY)) #define pte_user(pte) (!!(pte_val(pte) & PTE_USER)) #define pte_user_exec(pte) (!(pte_val(pte) & PTE_UXN)) #define pte_cont(pte) (!!(pte_val(pte) & PTE_CONT)) @@ -120,7 +121,7 @@ static inline pteval_t __phys_to_pte_val(phys_addr_t phys) (__boundary - 1 < (end) - 1) ? __boundary : (end); \ }) -#define pte_hw_dirty(pte) (pte_write(pte) && !(pte_val(pte) & PTE_RDONLY)) +#define pte_hw_dirty(pte) (pte_write(pte) && !pte_rdonly(pte)) #define pte_sw_dirty(pte) (!!(pte_val(pte) & PTE_DIRTY)) #define pte_dirty(pte) (pte_sw_dirty(pte) || pte_hw_dirty(pte)) @@ -174,6 +175,39 @@ static inline pmd_t clear_pmd_bit(pmd_t pmd, pgprot_t prot) return pmd; } +static inline pte_t pte_hw_mkdirty(pte_t pte) +{ + if (pte_write(pte)) + pte = clear_pte_bit(pte, __pgprot(PTE_RDONLY)); + + return pte; +} + +static inline pte_t pte_sw_mkdirty(pte_t pte) +{ + return set_pte_bit(pte, __pgprot(PTE_DIRTY)); +} + +static inline __always_unused pte_t pte_hw_clr_dirty(pte_t pte) +{ + return set_pte_bit(pte, __pgprot(PTE_RDONLY)); +} + +static inline pte_t pte_sw_clr_dirty(pte_t pte) +{ + pte = clear_pte_bit(pte, __pgprot(PTE_DIRTY)); + + /* + * Clearing the software dirty state requires clearing + * the PTE_DIRTY bit along with setting the PTE_RDONLY + * ensuring a page fault on subsequent write access. + * + * NOTE: Setting the PTE_RDONLY (as a coincident) also + * implies clearing the HW dirty state. + */ + return set_pte_bit(pte, __pgprot(PTE_RDONLY)); +} + static inline pmd_t set_pmd_bit(pmd_t pmd, pgprot_t prot) { pmd_val(pmd) |= pgprot_val(prot); @@ -189,19 +223,17 @@ static inline pte_t pte_mkwrite(pte_t pte) static inline pte_t pte_mkclean(pte_t pte) { - pte = clear_pte_bit(pte, __pgprot(PTE_DIRTY)); - pte = set_pte_bit(pte, __pgprot(PTE_RDONLY)); - - return pte; + /* + * Subsequent call to pte_hw_clr_dirty() is not required + * because pte_sw_clr_dirty() in turn does that as well. + */ + return pte_sw_clr_dirty(pte); } static inline pte_t pte_mkdirty(pte_t pte) { - pte = set_pte_bit(pte, __pgprot(PTE_DIRTY)); - - if (pte_write(pte)) - pte = clear_pte_bit(pte, __pgprot(PTE_RDONLY)); - + pte = pte_sw_mkdirty(pte); + pte = pte_hw_mkdirty(pte); return pte; } From patchwork Fri Jul 7 05:33:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anshuman Khandual X-Patchwork-Id: 13304439 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 F38DAEB64DA for ; Fri, 7 Jul 2023 05:34:19 +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:References:In-Reply-To: Message-Id:Date: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=v/9ELeSf1t6MGPEIyjtJw2+wfn3RMoMo+XrSHy1LfhY=; b=Zw6lPzEk7D250j QeT+ug4zZZDELuL7rHgI2OMdYEYlDLt5wbrBdQThtwJ7TWXtLR1eR6fY5Ihb8YsAWN36vrRng4s8v Ww+5j1eZWtGifWRPiaxTZgeJPWSqoZGf73ghAPfst7Dsa7+m0wL/6qlucE1BHLq7RRl8RZEuDd4xs K54+kWwwVlpshNu28ZlLBICu6H6FP3O9qVso7R8KwG91wnbiH156XiQko/nF5HLWCtiFz8ii8r5QY XYg9Kz7JzpP3UlohmDiQtse3jLIRChi3cW0Usbu84ngKDcDqADLbKpptxBmt6uHqVfqeylYe5FbxI cKNLM66mG8txDrz/Fe7w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qHe6Q-003d9m-1C; Fri, 07 Jul 2023 05:33:58 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qHe6N-003d8L-0v for linux-arm-kernel@lists.infradead.org; Fri, 07 Jul 2023 05:33:56 +0000 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 025DAD75; Thu, 6 Jul 2023 22:34:36 -0700 (PDT) Received: from a077893.arm.com (unknown [10.163.48.50]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 9B77D3F740; Thu, 6 Jul 2023 22:33:49 -0700 (PDT) From: Anshuman Khandual To: linux-arm-kernel@lists.infradead.org Cc: Anshuman Khandual , Catalin Marinas , Will Deacon , Ryan Roberts , Mark Rutland , Andrew Morton , David Hildenbrand , Jonathan Corbet , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org Subject: [RFC 2/4] arm64/mm: Call pte_sw_mkdirty() while preserving the HW dirty state Date: Fri, 7 Jul 2023 11:03:29 +0530 Message-Id: <20230707053331.510041-3-anshuman.khandual@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230707053331.510041-1-anshuman.khandual@arm.com> References: <20230707053331.510041-1-anshuman.khandual@arm.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230706_223355_371407_F1E09E16 X-CRM114-Status: GOOD ( 11.42 ) 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: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org pte_mkdirty() creates dirty states both in SW and HW bits which is really not required either in pte_wrprotect() or pte_modify() for preserving the HW dirty state. Instead pte_sw_mkdirty() is sufficient. Cc: Catalin Marinas Cc: Will Deacon Cc: linux-arm-kernel@lists.infradead.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Anshuman Khandual --- arch/arm64/include/asm/pgtable.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index fb03be697819..dd20b752ed48 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -244,7 +244,7 @@ static inline pte_t pte_wrprotect(pte_t pte) * clear), set the PTE_DIRTY bit. */ if (pte_hw_dirty(pte)) - pte = pte_mkdirty(pte); + pte = pte_sw_mkdirty(pte); pte = clear_pte_bit(pte, __pgprot(PTE_WRITE)); pte = set_pte_bit(pte, __pgprot(PTE_RDONLY)); @@ -855,7 +855,7 @@ static inline pte_t pte_modify(pte_t pte, pgprot_t newprot) PTE_ATTRINDX_MASK; /* preserve the hardware dirty information */ if (pte_hw_dirty(pte)) - pte = pte_mkdirty(pte); + pte = pte_sw_mkdirty(pte); pte_val(pte) = (pte_val(pte) & ~mask) | (pgprot_val(newprot) & mask); return pte; } From patchwork Fri Jul 7 05:33:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anshuman Khandual X-Patchwork-Id: 13304440 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 852EFEB64D9 for ; Fri, 7 Jul 2023 05:34:29 +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:References:In-Reply-To: Message-Id:Date: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=i6v7RSAYW1N6yr/2AVoiqmgGyLBgCg9iVasT4Yk02Vo=; b=VWcmt+hnZM4IK+ s+2YQHyf8g9B/E5l8w+aR2sgdIrp4hLjKOIf4uVP6P+yJuovTx/VxA6oxyBD0R+2C2ZQ/KQTt4JN5 IBH0rp27tdBFZ/Qj/X5cFPNWjG2oQ8MTRTcSbOOyqQo0Y9+AnVv/ZYBqNzJbw/I3kcaGUllfymP+q Sb4InHwTCVGqqn55eAi8mHx14uC32D15/xmKu5Hyue60E2b8NX7JWlz1JCQ2535ZTRk70WvmO2nNY sVJwx4sih0N4QP9jOhIASF4zif6kLuDWIC9tLaGz8gSu/Y/ESS8m2lziwGuF75O1dS/r3YZ+xQGCe Ax8FwbJhVDwS6Ut/gs5w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qHe6W-003dCS-27; Fri, 07 Jul 2023 05:34:04 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qHe6T-003dB3-1x for linux-arm-kernel@lists.infradead.org; Fri, 07 Jul 2023 05:34:03 +0000 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 4F865D75; Thu, 6 Jul 2023 22:34:41 -0700 (PDT) Received: from a077893.arm.com (unknown [10.163.48.50]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id C10AE3F740; Thu, 6 Jul 2023 22:33:55 -0700 (PDT) From: Anshuman Khandual To: linux-arm-kernel@lists.infradead.org Cc: Anshuman Khandual , Catalin Marinas , Will Deacon , Ryan Roberts , Mark Rutland , Andrew Morton , David Hildenbrand , Jonathan Corbet , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org Subject: [RFC 3/4] arm64/mm: Add pte_preserve_hw_dirty() Date: Fri, 7 Jul 2023 11:03:30 +0530 Message-Id: <20230707053331.510041-4-anshuman.khandual@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230707053331.510041-1-anshuman.khandual@arm.com> References: <20230707053331.510041-1-anshuman.khandual@arm.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230706_223401_697107_5D2DDC3A X-CRM114-Status: GOOD ( 11.46 ) 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: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Preserving the HW dirty state via SW PTE dirty bit, should be made explicit ensuring greater clarity and readability. This adds pte_preserve_hw_dirty() helper for that effect. No functional change is intended. Cc: Catalin Marinas Cc: Will Deacon Cc: linux-arm-kernel@lists.infradead.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Anshuman Khandual --- arch/arm64/include/asm/pgtable.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index dd20b752ed48..5344e71a58b2 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -237,7 +237,7 @@ static inline pte_t pte_mkdirty(pte_t pte) return pte; } -static inline pte_t pte_wrprotect(pte_t pte) +static inline pte_t pte_preserve_hw_dirty(pte_t pte) { /* * If hardware-dirty (PTE_WRITE/DBM bit set and PTE_RDONLY @@ -246,6 +246,12 @@ static inline pte_t pte_wrprotect(pte_t pte) if (pte_hw_dirty(pte)) pte = pte_sw_mkdirty(pte); + return pte; +} + +static inline pte_t pte_wrprotect(pte_t pte) +{ + pte = pte_preserve_hw_dirty(pte); pte = clear_pte_bit(pte, __pgprot(PTE_WRITE)); pte = set_pte_bit(pte, __pgprot(PTE_RDONLY)); return pte; @@ -853,9 +859,7 @@ static inline pte_t pte_modify(pte_t pte, pgprot_t newprot) const pteval_t mask = PTE_USER | PTE_PXN | PTE_UXN | PTE_RDONLY | PTE_PROT_NONE | PTE_VALID | PTE_WRITE | PTE_GP | PTE_ATTRINDX_MASK; - /* preserve the hardware dirty information */ - if (pte_hw_dirty(pte)) - pte = pte_sw_mkdirty(pte); + pte = pte_preserve_hw_dirty(pte); pte_val(pte) = (pte_val(pte) & ~mask) | (pgprot_val(newprot) & mask); return pte; } From patchwork Fri Jul 7 05:33:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anshuman Khandual X-Patchwork-Id: 13304441 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 BCE74EB64DA for ; Fri, 7 Jul 2023 05:34:31 +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:References:In-Reply-To: Message-Id:Date: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=8UFSYNdlr0b3fo5Kem9aCdQT/hpCOjGM8fFjyvlsGbI=; b=z/JXXC/hF5BeTi X8CtJtQW24zNx5o/HRR+KOYK+01ijVk8AmzURUlns4XPaylOWIqgsQ8pf7KHjHDgQTmV+O1Kp1tJs XFAEKk4H7DiQx67O7XMMAuKjxOmVB9RWmKWpjq60G++8V+Ym58eruQ/BDKOjQnbTvTF5/dJ3FHlRo Mg5fSjxC0nzzPeethP42H/Ly/PKrWoq6O9CVw28e7pzLE8WPoDqnf8mwFxoItachehDkQqPYQH+EV T84wTLlxJwdqFIaIKlc4XRngsi+hZGuPYYEMREVKjqrmRrXXdBXpkTraIxbUocRG06Y7pEeewxTbs 6EhkmX8CxA3gN3s0TDwQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qHe6b-003dEe-1A; Fri, 07 Jul 2023 05:34:09 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qHe6Y-003dD3-0X for linux-arm-kernel@lists.infradead.org; Fri, 07 Jul 2023 05:34:08 +0000 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 B029112FC; Thu, 6 Jul 2023 22:34:45 -0700 (PDT) Received: from a077893.arm.com (unknown [10.163.48.50]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 114983F740; Thu, 6 Jul 2023 22:33:59 -0700 (PDT) From: Anshuman Khandual To: linux-arm-kernel@lists.infradead.org Cc: Anshuman Khandual , Catalin Marinas , Will Deacon , Ryan Roberts , Mark Rutland , Andrew Morton , David Hildenbrand , Jonathan Corbet , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org Subject: [RFC 4/4] docs: arm64: Add help document for pte dirty state management Date: Fri, 7 Jul 2023 11:03:31 +0530 Message-Id: <20230707053331.510041-5-anshuman.khandual@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230707053331.510041-1-anshuman.khandual@arm.com> References: <20230707053331.510041-1-anshuman.khandual@arm.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230706_223406_299247_F0E65612 X-CRM114-Status: GOOD ( 18.75 ) 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: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org PTE dirty state management is non-trivial on arm64 platform. This document explains how both software and hardware come together in correctly tracking PTE ditry state across various page table transactions. Cc: Catalin Marinas Cc: Will Deacon Cc: Jonathan Corbet Cc: linux-arm-kernel@lists.infradead.org Cc: linux-kernel@vger.kernel.org Cc: linux-doc@vger.kernel.org Signed-off-by: Anshuman Khandual --- Documentation/arch/arm64/index.rst | 1 + Documentation/arch/arm64/pte-dirty.rst | 95 ++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 Documentation/arch/arm64/pte-dirty.rst diff --git a/Documentation/arch/arm64/index.rst b/Documentation/arch/arm64/index.rst index d08e924204bf..522f887f2a60 100644 --- a/Documentation/arch/arm64/index.rst +++ b/Documentation/arch/arm64/index.rst @@ -22,6 +22,7 @@ ARM64 Architecture perf pointer-authentication ptdump + pte-dirty silicon-errata sme sve diff --git a/Documentation/arch/arm64/pte-dirty.rst b/Documentation/arch/arm64/pte-dirty.rst new file mode 100644 index 000000000000..a6401696f6a3 --- /dev/null +++ b/Documentation/arch/arm64/pte-dirty.rst @@ -0,0 +1,95 @@ +.. SPDX-License-Identifier: GPL-2.0 +========================================= +Page Table Entry - Dirty State Management +========================================= + +1. Introduction +--------------- + +arm64 platform defines pte_dirty() to determine if the pte has been dirtied +i.e pte has been written info after the previous clean procedure. The dirty +state tracking could be achieved, either via software or hardware pte dirty +bit mechanism. On arm64 platform, pte_dirty() is implemented utilizing both +software and hardware dirty bits, making it non intuitive unlike many other +platforms. + +2. PTE Dirty Bits (SW and HW) +----------------------------- +Following are relevant PTE bit positions for dirty state tracking. + +- PTE_DIRTY is a software bit (55) in the PTE +- PTE_RDONLY is a hardware bit (7) in the PTE +- PTE_DBM is a hardware bit (51) in the PTE +- PTE_WRITE is a hardware bit (51) in the PTE - share position with PTE_DBM + +3. PTE Dirty State Tracking +--------------------------- +Without ARM64_HW_AFDBM enabled, PTE dirty state is tracked only in the SW. +PTE is marked read-only in HW, subsequent write access generates page fault +which can update the SW dirty bit and clear the read-only access in HW. + +With ARM64_HW_AFDBM enabled, PTE dirty state is tracked both in SW and HW. +PTE is marked read-only in HW while also enabling DBM tracking. Any write +access will clear the read-only bit while also preventing a page fault. As +PTE_DBM and PTE_WRITE share the same bit position, a dirty non-writable PTE +state cannot be tracked in hardware. This in turn necessitates dirty state +tracking (ARM64_HW_AFDBM enabled) to accommodate both software and hardware +PTE bits. This helps in avoiding a runtime check for ARM64_HW_AFDBM feature +being enabled on a given implementation. + +Testing and clearing PTE dirty state is relatively simple - + +#define pte_hw_dirty(pte) (pte_write(pte) && !pte_rdonly(pte)) +#define pte_sw_dirty(pte) (!!(pte_val(pte) & PTE_DIRTY)) +#define pte_dirty(pte) (pte_sw_dirty(pte) || pte_hw_dirty(pte)) + +static inline pte_t pte_mkclean(pte_t pte) +{ + /* + * Subsequent call to pte_hw_clr_dirty() is not required + * because pte_sw_clr_dirty() in turn does that as well. + */ + return pte_sw_clr_dirty(pte); +} + +But marking a dirty state, creating a write protected entry etc now becomes +bit non-trivial in hardware. as PTE_RDONLY bit could only be cleared if the +write bit is also set. + +static inline pte_t pte_hw_mkdirty(pte_t pte) +{ + if (pte_write(pte)) + return clear_pte_bit(pte, __pgprot(PTE_RDONLY)); + + return pte; +} + +Hence marking a dirty state triggers marking both SW and HW dirty bits, so +that if the HW suppoprt is unavailable or insufficient (dirty non-writable) +, SW mechanism would still put it in a dirty state. + +static inline pte_t pte_mkdirty(pte_t pte) +{ + pte = pte_sw_mkdirty(pte); + pte = pte_hw_mkdirty(pte); + return pte; +} + +4. Preserving PTE HW Dirty State +-------------------------------- +If for some reason HW dirty bits (PTE_WRITE, PTE_RDONLY) need to be cleared +the dirty state must be transferred as SW dirty bit ensuring persistence of +the dirty state across the operation. + +static inline pte_t pte_modify(pte_t pte, pgprot_t newprot) +{ + ..... + pte = pte_preserve_hw_dirty(pte_t pte); + ..... +} + +static inline pte_t pte_wrprotect(pte_t pte) +{ + pte = pte_preserve_hw_dirty(pte_t pte); + ..... +}