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: 116975 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f45:0:b0:3ea:f831:8777 with SMTP id v5csp3039667vqx; Thu, 6 Jul 2023 22:37:22 -0700 (PDT) X-Google-Smtp-Source: APBJJlH7yg+fOwxuafZN8XN3S1Nw9AuEPfit3+oQX5/LE+HTftqRkptj7hdcsKKyi369Z+K58Byv X-Received: by 2002:a05:6a20:96cb:b0:118:e70:6f7d with SMTP id hq11-20020a056a2096cb00b001180e706f7dmr2673429pzc.10.1688708242036; Thu, 06 Jul 2023 22:37:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688708242; cv=none; d=google.com; s=arc-20160816; b=urC6Db9D+6s8Lnw1I/IGrY75r654OncH0MNWNQzOEIJLncO+ewer84DR7XCPNZtvJa E/cwC23VJNgZPhRKkZ9uNQ0uZ/whEqnmcjmERnozw4c7BYd92weClzbTMF+/mIy6sxAn bG++AphsLfrEVXUPJOmsHJtN22PblizrivlBhfXaPH12DT6LY6EH++jeiG5JsdcSAljU E9KgM1oLH4iqnaq0W+vB9UdTYZk6CfZTqnRRmEm1MFRU6qm4Z08WniyYzkW5BokImFHX zAoW9TNd5c3RR4g//rtj8nF+tQj7I+IWceleogl9fYBMV6+MFvjQCN7c+zisEd0cvV28 bi1A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=wyzIxGpZRhsvqQDTuuS6+NukU8JUm/NvBuZkFpgDZ/U=; fh=v8WkW07D8QHzh6geYHFbL/t66J83/bx/yAuOgJXvUtg=; b=JTbjPjDKKcRBhG0PP/KHvvqioGi9G8tzu0Ywe91Ezx09EjeOWQkqWqBgbknAlntWpK mGj4FErmrBiSsOKIkevIQCeMjF0wzvK58BY0SYVQ++EE0onGKO46lNgZo+iQl9yAuK5z IJACvx+xUBazSN+jBw0WjD6iqVqHrBbI8D/ur8eV1QSS/vt7fKuse8p+wiHkkzyK0LAf 7nyuBgGluPt9nNdbIs73QbfSOvuEit+cWsR7qLYp3nV68ZzjP+6QlkOnTcFtiU+yVvtD uH7uRlbzJnl+occpBghbt+SQvJUwdb9FZAJY9PBQki+7ZTLzORhxf/I1Fzgtn5GiZVQh xI8A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id fa32-20020a056a002d2000b0068094fd547esi3255939pfb.99.2023.07.06.22.37.09; Thu, 06 Jul 2023 22:37:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230040AbjGGFd4 (ORCPT + 99 others); Fri, 7 Jul 2023 01:33:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47310 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229529AbjGGFdu (ORCPT ); Fri, 7 Jul 2023 01:33:50 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 41925171D; Thu, 6 Jul 2023 22:33:49 -0700 (PDT) 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-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1770738933539799040?= X-GMAIL-MSGID: =?utf-8?q?1770738933539799040?= 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: 116976 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f45:0:b0:3ea:f831:8777 with SMTP id v5csp3039794vqx; Thu, 6 Jul 2023 22:37:41 -0700 (PDT) X-Google-Smtp-Source: APBJJlFUrJEb3rmv3SQDcsGQ6NDA2Xp0tT7YOCAXulYkIzQm/Jypbjg410Ye/xW46K/7jdIwPD7R X-Received: by 2002:a05:6a00:2e92:b0:682:2152:45df with SMTP id fd18-20020a056a002e9200b00682215245dfmr3086256pfb.9.1688708261616; Thu, 06 Jul 2023 22:37:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688708261; cv=none; d=google.com; s=arc-20160816; b=KHnrmrY5gYz455U0ngTUxsYNG/Jf6MwU026J3za88Rikx/56XVGyOMvW5zTRucR17t 0PNU4M9d09Uz6heW97xLroG6dLhZcWFWNn4X3+C/oqKusVWcQlE2jm/SJ1xLf8oan227 BOWT8MExvDx81b8sDyzXY2lWNihQcWJO+AJmL1Tc35iEsy2nH+n6fHzoH3hdIbPWm+0y h/s3xKK+Ygvcbg7bnS0PnJnRJFFJQcTZOoi6rjenUjUU6DeX6ALRmf5nryH0oVsIQqEy QqHkSd0swU5hJZfaW3t1Y/oHmZUk2z2DsUbV3w/Fp4V8ot3BGZlQhuCh1hJqEF5xC3iC fkVw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=jrzem4dssYQcq9RDp3bxleA9YeCfPVvM5nPmrsZl414=; fh=v8WkW07D8QHzh6geYHFbL/t66J83/bx/yAuOgJXvUtg=; b=U4xPdVRyBqMLH37GbQ7623VyEQvuiS6VUHaxKJATWKCsU6Urg0naFLDqpSwhQvE8sR MonZCvxbXUP6wmNgMu0UiKne5Hd0fbqwITkgscZ0JkI2ej2jNgz+Ak7ePz5QUsyFURse DXs2ZkguMfsJdgfFqnb4SOolmGT6K61Oxe8s2rhSo6rY49Sofft++CcL0qfgOCfGd10V vBYuXFyK2AYnPMPw63zBc41J23emdv961vkWMsEaIJkDUdg+fyWFrKr5qIT9//fG0cVZ 5bVX3cKjQFnJ6Lsm5nTD10uStkj/7zTYkaJv5fcAgl9DY1jtyxaogtuEgBpxycV4g0gS mRsg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id eb13-20020a056a004c8d00b0068288aaf240si2926197pfb.212.2023.07.06.22.37.29; Thu, 06 Jul 2023 22:37:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231959AbjGGFd6 (ORCPT + 99 others); Fri, 7 Jul 2023 01:33:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47336 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231950AbjGGFdz (ORCPT ); Fri, 7 Jul 2023 01:33:55 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 6DFB01BF4; Thu, 6 Jul 2023 22:33:54 -0700 (PDT) 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-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1770738954042169886?= X-GMAIL-MSGID: =?utf-8?q?1770738954042169886?= 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: 116979 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f45:0:b0:3ea:f831:8777 with SMTP id v5csp3042738vqx; Thu, 6 Jul 2023 22:45:39 -0700 (PDT) X-Google-Smtp-Source: APBJJlEXdLYKThh8aTxm+A2iZg1+glWJIkx4ydlKa6IBKpYFHXWHRkh2htmq2h6hp/yIyw5r7vNT X-Received: by 2002:a05:6808:d48:b0:3a0:5a6b:25c with SMTP id w8-20020a0568080d4800b003a05a6b025cmr3975341oik.33.1688708739630; Thu, 06 Jul 2023 22:45:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688708739; cv=none; d=google.com; s=arc-20160816; b=viIUoyZ674xrysBYifS8U3+XLk+NROBqjTHvG1pxtVJqsYxzX3VPFSPl4bosOrISJb cgNr20Gc6BI2fSrPkV0GA51CrQXEspOAZwzTZbHjGJuhNJrLZCuI7lZWLxNMjPdnwBQ4 +WTBsmHLnaaYxBescqjOON/pTQYCw2cbRfeEyDbR4iKEFLDIX3nd0iRRa9nahPac9Te0 Oz6uGVjindahgpwEQq2NFyJCHLRVnxMOfJ3A0H/NlQb3r+l9GHhy2O7xENi5KS8ocrjx AD3Jx+jEFCuq67WPrOfK33b8qFkEbMh52xSRPIjGO5ZfP7qK8kmPEvqw/zFmclH3ccX3 mBXQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=ZQb3yAXZv4KLulngffs/iaEINfHQCq69u+201qbUFUU=; fh=v8WkW07D8QHzh6geYHFbL/t66J83/bx/yAuOgJXvUtg=; b=WK/ZcZiU7s02pQwzzsm0sYOFMij5QSCPqRRBhS9oHnnxXsCKyBZ6NYHw5W4zU3MhOb GnGaNrb31n6Tz7PuGsfAa2/Cpu+mILBeUKSBECILIegCF6zTicTBsazGexzrwpohK7li 9kqIK5FZAzj7KyxHPfC1XXwPHjJrATz4YPSprmiNXd8vdgWhkmnfcrVDo5Ega/2GivF6 rn/W8Bx79YHq1WGm3e1ymySG8IxZIfad+Agj4c49qbYvhYXzqlFnV2QTXpAmdY4X59+f X8RpdqnHzsQNcb0uMQYVXpJHy6MtrlVfKcGV0dtPNGAOy0iUmb/7FDhmiZqe0jdLTNq7 wGSw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l191-20020a6391c8000000b0055ac5fed58dsi3240140pge.363.2023.07.06.22.45.25; Thu, 06 Jul 2023 22:45:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232370AbjGGFeI (ORCPT + 99 others); Fri, 7 Jul 2023 01:34:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47556 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232177AbjGGFeE (ORCPT ); Fri, 7 Jul 2023 01:34:04 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id A5D991FEB; Thu, 6 Jul 2023 22:33:59 -0700 (PDT) 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-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1770739455704206267?= X-GMAIL-MSGID: =?utf-8?q?1770739455704206267?= 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: 116977 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f45:0:b0:3ea:f831:8777 with SMTP id v5csp3040716vqx; Thu, 6 Jul 2023 22:40:05 -0700 (PDT) X-Google-Smtp-Source: APBJJlHyL/DSbvEltfV2WwmJsX+Wkk4Y30TZzzhXoQGZY2XoDon3Cmgr/h3QLjE9bOII7zkkxtiR X-Received: by 2002:a17:902:7c82:b0:1b3:d608:899a with SMTP id y2-20020a1709027c8200b001b3d608899amr3292192pll.68.1688708405455; Thu, 06 Jul 2023 22:40:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688708405; cv=none; d=google.com; s=arc-20160816; b=cude5a5QfokCnw+d1aMrVBDfVc1Grrq16G74+A+e1NPqeDwgexFoPreVWjx4Oc7xOr 42ccol5Ilp9/J2snPgZ4A5C79P1mNqkKWIkCD/aLApsio5F+3UccJcb/zwU2ZigIiMos pk5ME29ODABv6+SCr5713brkW8gxUyZRwXfoVhn+dZnhXnc9GhhSYdCG/ORW9lLAJOfH cEIHbosW2MyIAOLMJMGkPEDQAVaYT2HLDVFaRG+/GWbzKIYIrDe+pvT7lCT0M3/KsAkl Hvh2dFMds9yF/ZFWuzVLsI1Pv1SifMPQtN4HuSAMAx/n2ctsegL5U1aRMudBg0Mu9Abm 0NXQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=HBeM1TOezSpwa0soniccuiZPcRXhzBjdYBJRxi+ZI3s=; fh=v8WkW07D8QHzh6geYHFbL/t66J83/bx/yAuOgJXvUtg=; b=s5yPAbhgAveqJsVRauaXWt0BKrR9zuOJ1vKsApopERs1JtrwcRefvq3IlJ4WCt8mOD eG4xagwmh1iIIHE/QEt02U2V82aG9rqyK9ArEoWXOxIVERWCU6sYq8Lcq64jQ+OyCcpq C02wD1RLD5AWTTXZjifEpGZEypabqBwsAGVJpv7CDpCX/zLeAJSSiTZSR1ksNK4NY+2L l+kg/IEE/mWijh+YrZJ81ABjbtLcr6dboqPUA1FHsDQx3yhXYv8dKXf7ZXuv73TMOgHI 4TDvCQuWwXlZKzrSzwwoo+LmJxCWy6naq9fOIxKCxO73Qe/JLWpWuv/tD2INgHLxjjKM FVxg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q9-20020a170902a3c900b001b6c462acbdsi2659603plb.15.2023.07.06.22.39.52; Thu, 06 Jul 2023 22:40:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230195AbjGGFeR (ORCPT + 99 others); Fri, 7 Jul 2023 01:34:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47540 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232408AbjGGFeK (ORCPT ); Fri, 7 Jul 2023 01:34:10 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 3DADB210B; Thu, 6 Jul 2023 22:34:04 -0700 (PDT) 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-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1770739104897683337?= X-GMAIL-MSGID: =?utf-8?q?1770739104897683337?= 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); + ..... +}