From patchwork Mon Jul 3 13:55:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nico Pache X-Patchwork-Id: 13300184 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 94349EB64DC for ; Mon, 3 Jul 2023 14:08:38 +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: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:In-Reply-To:References: List-Owner; bh=Q4+XP+ymMMWAKz4dLU+8h3gVJY0bMG1Hi2LrQumyEZs=; b=UQSImGxb5PK/Ee TgTVusjIW9xGfs7W56Dfhmz38GGbby2YbRSkZVA+zAo/+0K9rb2iGOKPojLNuKZOOYYPK/lV1fZA4 9jNxpf4xhFP7p6z2Sw0MoKbl92vwowYRrz1E5PFYhtZRvBkKfq+fj4L77UkXnvBn7lX+WMQgtufvb ceSFjNnxSz2x/0OejKZ691bt7qPI1bfpJzDO19QiKykK0h0HKzMj31qpsrd3s7jPPjS6SZli98uqJ Bq19Ks+27mYuigPZNRc/0nf7UeTwUZbry6tmF6u5dN8mAcLJy/32lddtoOuhFdtIBFjggXWUinrPo BEuOavZHPzTfcwi2nd+Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qGKDo-00Aij2-1q; Mon, 03 Jul 2023 14:08:08 +0000 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qGKDm-00AiiU-07 for linux-arm-kernel@lists.infradead.org; Mon, 03 Jul 2023 14:08:07 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1688393281; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=zHQXfVtXAZjvrQC07bbaf5d12vfBfUWqeJ1W4srezSg=; b=KqGrXvNFvVMQdsArlp5EyJ/Sj5Ifw+kZKpzmD/Ypa9X7/5nhCJoxcMQhUaOrpg28nlP3Ne b3GmR+ZUVy96jfsbpqsG5vZVVimRMquYGikfchYZ3Xp3rKlTc6at4NAEVCNFluqw/VF6Gr 9xe8N+LJCCF96erQdXgYQQuHIxF8gSo= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-519-KKwRuRHoNVWxmJZw_RtnrA-1; Mon, 03 Jul 2023 10:06:00 -0400 X-MC-Unique: KKwRuRHoNVWxmJZw_RtnrA-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B40C03C18FE2; Mon, 3 Jul 2023 14:05:59 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.22.32.241]) by smtp.corp.redhat.com (Postfix) with ESMTP id B36D6492B01; Mon, 3 Jul 2023 14:05:58 +0000 (UTC) From: Nico Pache To: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Cc: aquini@redhat.com, Andrew Morton , Anshuman Khandual , Catalin Marinas , David Hildenbrand , Gerald Schaefer , Liu Shixin , Will Deacon , Yu Zhao Subject: [RFC] arm64: properly define SOFT_DIRTY for arm64 Date: Mon, 3 Jul 2023 09:55:26 -0400 Message-ID: <20230703135526.930004-1-npache@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230703_070806_262717_F334EB95 X-CRM114-Status: GOOD ( 18.33 ) 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 ARM64 has a soft-dirty bit (software dirty) but never properly defines CONFIG_ARCH_HAS_SOFT_DIRTY or its necessary functions. This patch introduces the ability to set/clear the soft dirty bit in a similar manner as the other arches that utilize it. However, we must be careful... there are cases where the DBM bit is not available and the software dirty bit plays a essential role in determining whether or not a page is dirty. In these cases we must not allow the user to clear the software dirty bit. We can test for these cases by utilizing the arch_faults_on_old_pte() function which test the availability of DBM. Cc: Andrew Morton Cc: Anshuman Khandual Cc: Catalin Marinas Cc: David Hildenbrand Cc: Gerald Schaefer Cc: Liu Shixin Cc: Will Deacon Cc: Yu Zhao Signed-off-by: Nico Pache --- arch/arm64/Kconfig | 1 + arch/arm64/include/asm/pgtable.h | 77 +++++++++++++++++++++++++++++++- 2 files changed, 76 insertions(+), 2 deletions(-) diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 891ab530a665..4de491627f49 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -173,6 +173,7 @@ config ARM64 select HAVE_ARCH_PREL32_RELOCATIONS select HAVE_ARCH_RANDOMIZE_KSTACK_OFFSET select HAVE_ARCH_SECCOMP_FILTER + select HAVE_ARCH_SOFT_DIRTY select HAVE_ARCH_STACKLEAK select HAVE_ARCH_THREAD_STRUCT_WHITELIST select HAVE_ARCH_TRACEHOOK diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index 0bd18de9fd97..a0a15ffa2417 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -121,8 +121,9 @@ static inline pteval_t __phys_to_pte_val(phys_addr_t phys) }) #define pte_hw_dirty(pte) (pte_write(pte) && !(pte_val(pte) & PTE_RDONLY)) -#define pte_sw_dirty(pte) (!!(pte_val(pte) & PTE_DIRTY)) -#define pte_dirty(pte) (pte_sw_dirty(pte) || pte_hw_dirty(pte)) +#define pte_soft_dirty(pte) (!!(pte_val(pte) & PTE_DIRTY)) +#define pte_dirty(pte) (pte_soft_dirty(pte) || pte_hw_dirty(pte)) +#define pte_swp_soft_dirty(pte) pte_soft_dirty(pte) #define pte_valid(pte) (!!(pte_val(pte) & PTE_VALID)) /* @@ -1096,6 +1097,78 @@ static inline bool pud_sect_supported(void) return PAGE_SIZE == SZ_4K; } +#ifdef CONFIG_ARM64_HW_AFDBM +/* + * if we have the DBM bit we can utilize the software dirty bit as + * a mechanism to introduce the soft_dirty functionality; however, without + * it this bit is crucial to determining if a entry is dirty and we cannot + * clear it via software. DBM can also be disabled or broken on some early + * armv8 devices, so check its availability before modifying it. + */ +static inline pte_t pte_clear_soft_dirty(pte_t pte) +{ + if (arch_faults_on_old_pte()) + return pte; + + return clear_pte_bit(pte, __pgprot(PTE_DIRTY)); +} + +static inline pte_t pte_mksoft_dirty(pte_t pte) +{ + if (arch_faults_on_old_pte()) + return pte; + + return set_pte_bit(pte, __pgprot(PTE_DIRTY)); +} + +static inline pte_t pte_swp_clear_soft_dirty(pte_t pte) +{ + if (arch_faults_on_old_pte()) + return pte; + + return clear_pte_bit(pte, __pgprot(PTE_DIRTY)); +} + +static inline pte_t pte_swp_mksoft_dirty(pte_t pte) +{ + if (arch_faults_on_old_pte()) + return pte; + + return set_pte_bit(pte, __pgprot(PTE_DIRTY)); +} + +static inline int pmd_soft_dirty(pmd_t pmd) +{ + return pte_soft_dirty(pmd_pte(pmd)); +} + +static inline pmd_t pmd_clear_soft_dirty(pmd_t pmd) +{ + return pte_pmd(pte_clear_soft_dirty(pmd_pte(pmd))); +} + +static inline pmd_t pmd_mksoft_dirty(pmd_t pmd) +{ + return pte_pmd(pte_mksoft_dirty(pmd_pte(pmd))); +} + +#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION +static inline int pmd_swp_soft_dirty(pmd_t pmd) +{ + return pmd_soft_dirty(pmd); +} + +static inline pmd_t pmd_swp_clear_soft_dirty(pmd_t pmd) +{ + return pmd_clear_soft_dirty(pmd); +} + +static inline pmd_t pmd_swp_mksoft_dirty(pmd_t pmd) +{ + return pmd_mksoft_dirty(pmd); +} +#endif /* CONFIG_ARCH_ENABLE_THP_MIGRATION */ +#endif /* CONFIG_ARM64_HW_AFDBM */ #define __HAVE_ARCH_PTEP_MODIFY_PROT_TRANSACTION #define ptep_modify_prot_start ptep_modify_prot_start