Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp75751rwb; Tue, 15 Nov 2022 19:26:35 -0800 (PST) X-Google-Smtp-Source: AA0mqf7HiqvTBm+Z7tgJ3sBwpWOW3wyZkpE7JXtMR4VbJW1UDziYIbpRPoS+VwCTlAz6vvEpqqx9 X-Received: by 2002:aa7:da98:0:b0:463:a83c:d762 with SMTP id q24-20020aa7da98000000b00463a83cd762mr16966449eds.44.1668569195651; Tue, 15 Nov 2022 19:26:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668569195; cv=none; d=google.com; s=arc-20160816; b=G+baWNSR8vNTUXz04RM4B2FQaKnxd9zA4fzTfAo4SS0PLt0X7EmtH//3JcjTkZagE8 vd0S7z1HvrfEf+DT1Yk6VDDlvbYSC1pkm/ELnkj5KI5D4CEY4AFaopQ+kGkx9y5ZaNVm ZjbHosQrQ5ooTBhfxrhn1W0eYn245LZI0GywSR2deRso6cksIr7jGk8k4+/R1C9z3/4D OXHs3PXGHm2jQfwoYIrq997S3aWgp0x8j9I0m+YKgNarMLz1VMHG0F3j+S+I1NwJBTg3 ppIBa56JHG5yMH2ghuUCOWL1t0LbkCBuzMDWH0Kr9AlC6ayGDo4yl8VTTxYW+82wma5P HfIQ== 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 :message-id:date:subject:cc:to:from; bh=VNENtleQmI7qajMc/1YOiMJsIra9qnQC9hf7tQ7otpc=; b=yvm01vln1XK9dTDzg6V/TMdoA7GgPhGHFCFTxdMxQVOgCrOSg2z/AMCxSTmW9rk+YN KmdfytFjThM9Qk7vxMHZZDw9lD4EcCV28fVWzIvxWzusdTJ8K9EAUgRxSA+TdFRG//l/ pB2rc6InYV4pmIWHWvMnHiFUx01hUXz+FX4wGN67OrgK+fLEEnq23m0LGReJLUL6JNuQ JN8ZR8HTvaEwE/KZOKmJnE8AL9B9XUMgC2a/5dOek+nBAcOzil2Ag6DsZghiCRCT3kEE J91b3QP9Nm2M2/vHtzh7BtNEUr2sg49kR/rUSdzKKM28AxbdnjIZlueZ+NVrN6/kiVen AaHQ== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ds14-20020a170907724e00b0078d4b2754b5si15074004ejc.332.2022.11.15.19.25.49; Tue, 15 Nov 2022 19:26:35 -0800 (PST) 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 S231874AbiKPDKN (ORCPT + 90 others); Tue, 15 Nov 2022 22:10:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37202 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230244AbiKPDKM (ORCPT ); Tue, 15 Nov 2022 22:10:12 -0500 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 72DF5252BD for ; Tue, 15 Nov 2022 19:10:11 -0800 (PST) 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 861E113D5; Tue, 15 Nov 2022 19:10:17 -0800 (PST) Received: from a077893.blr.arm.com (unknown [10.162.40.17]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id E89413F663; Tue, 15 Nov 2022 19:10:08 -0800 (PST) From: Anshuman Khandual To: linux-arm-kernel@lists.infradead.org, will@kernel.org, catalin.marinas@arm.com Cc: Anshuman Khandual , Mark Rutland , Andrew Morton , linux-kernel@vger.kernel.org Subject: [PATCH] arm64/mm: Intercept pfn changes in set_pte_at() Date: Wed, 16 Nov 2022 08:40:01 +0530 Message-Id: <20221116031001.292236-1-anshuman.khandual@arm.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE 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 Changing pfn on a user page table mapped entry, without first going through break-before-make (BBM) procedure is unsafe. This just updates set_pte_at() to intercept such changes, via an updated pgattr_change_is_safe(). This new check happens via __check_racy_pte_update(), which has now been renamed as __check_safe_pte_update(). Cc: Catalin Marinas Cc: Will Deacon Cc: Mark Rutland Cc: Andrew Morton Cc: linux-arm-kernel@lists.infradead.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Anshuman Khandual --- This applies on v6.1-rc4 arch/arm64/include/asm/pgtable.h | 8 ++++++-- arch/arm64/mm/mmu.c | 8 +++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index 71a1af42f0e8..6b8b24e6cd35 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -275,6 +275,7 @@ static inline void set_pte(pte_t *ptep, pte_t pte) } extern void __sync_icache_dcache(pte_t pteval); +bool pgattr_change_is_safe(u64 old, u64 new); /* * PTE bits configuration in the presence of hardware Dirty Bit Management @@ -292,7 +293,7 @@ extern void __sync_icache_dcache(pte_t pteval); * PTE_DIRTY || (PTE_WRITE && !PTE_RDONLY) */ -static inline void __check_racy_pte_update(struct mm_struct *mm, pte_t *ptep, +static inline void __check_safe_pte_update(struct mm_struct *mm, pte_t *ptep, pte_t pte) { pte_t old_pte; @@ -318,6 +319,9 @@ static inline void __check_racy_pte_update(struct mm_struct *mm, pte_t *ptep, VM_WARN_ONCE(pte_write(old_pte) && !pte_dirty(pte), "%s: racy dirty state clearing: 0x%016llx -> 0x%016llx", __func__, pte_val(old_pte), pte_val(pte)); + VM_WARN_ONCE(!pgattr_change_is_safe(pte_val(old_pte), pte_val(pte)), + "%s: unsafe attribute change: 0x%016llx -> 0x%016llx", + __func__, pte_val(old_pte), pte_val(pte)); } static inline void __set_pte_at(struct mm_struct *mm, unsigned long addr, @@ -346,7 +350,7 @@ static inline void __set_pte_at(struct mm_struct *mm, unsigned long addr, mte_sync_tags(old_pte, pte); } - __check_racy_pte_update(mm, ptep, pte); + __check_safe_pte_update(mm, ptep, pte); set_pte(ptep, pte); } diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c index 9a7c38965154..6c928ea99ab3 100644 --- a/arch/arm64/mm/mmu.c +++ b/arch/arm64/mm/mmu.c @@ -133,7 +133,7 @@ static phys_addr_t __init early_pgtable_alloc(int shift) return phys; } -static bool pgattr_change_is_safe(u64 old, u64 new) +bool pgattr_change_is_safe(u64 old, u64 new) { /* * The following mapping attributes may be updated in live @@ -145,6 +145,12 @@ static bool pgattr_change_is_safe(u64 old, u64 new) if (old == 0 || new == 0) return true; + /* If old and new ptes are valid, pfn should not change */ + if (pte_valid(__pte(old)) && pte_valid(__pte(new))) { + if (pte_pfn(__pte(old)) != pte_pfn(__pte(new))) + return false; + } + /* live contiguous mappings may not be manipulated at all */ if ((old | new) & PTE_CONT) return false; -- 2.25.1