From patchwork Mon Aug 12 18:12:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13760911 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 51106C3DA7F for ; Mon, 12 Aug 2024 18:12:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 669046B00A1; Mon, 12 Aug 2024 14:12:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 61C8B6B00A2; Mon, 12 Aug 2024 14:12:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 492016B00A3; Mon, 12 Aug 2024 14:12:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 254836B00A1 for ; Mon, 12 Aug 2024 14:12:47 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id D573CA0335 for ; Mon, 12 Aug 2024 18:12:46 +0000 (UTC) X-FDA: 82444389132.23.3FB2DBB Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf07.hostedemail.com (Postfix) with ESMTP id ACA2C4001B for ; Mon, 12 Aug 2024 18:12:44 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=U1P3j7EN; spf=pass (imf07.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723486353; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=vL+nA7AT6HH2fptU3fDtubaziCDql6jx+j3l3b3Xtg8=; b=VWfWr7+ItaxIf6oSTW/Nm0+qV6qbYcYrIIhoDnxOxuCwZSeJ+LkH5m4YmozAVSo+rRYrGc klANoGmn1ns9Kc81EKwzh62gr1+1ibH8LqJ9c9zioahJMulQyxjbmCxtUy8jXTWVgrNt8n RuEJBvVdSwPyVpcUg/LC6u148qP84Mc= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=U1P3j7EN; spf=pass (imf07.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723486353; a=rsa-sha256; cv=none; b=zb7ssfVff3Fy3TIsx7iYwwxBP2Wt4NnxdpxiPco0JD2+KNyxRZQXdLvoU8WfFx1jRRDwQv KiJm06ihOAixzmJx4rVKERAdnA6/Qnp8/xL2oKL6CyQMAf+MzrQ4EAeqmDLHpBlWc2tFll PeriSSLg/LqYKTcwkCmpy0M71hJfJ1E= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1723486364; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=vL+nA7AT6HH2fptU3fDtubaziCDql6jx+j3l3b3Xtg8=; b=U1P3j7ENeI7q8+7/oKuw8y8SjnekQ+qiuXrDp7o5JAkV2KFmW6PUPAJUQM7vpC6SRKhTOA 2ll13AGeGX0hmg8Rcso2nQSno3+5Spxv0+KSh9nuAp+CX56ZVM30M2wjEs0bMbnkrRLm5b eYqK3HLRl2SC9Vtssq5VpNvtbTyM1ec= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-442-IDOXQsPXO-KhQQb0OnkSdQ-1; Mon, 12 Aug 2024 14:12:40 -0400 X-MC-Unique: IDOXQsPXO-KhQQb0OnkSdQ-1 Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-7a1e1828321so15941185a.1 for ; Mon, 12 Aug 2024 11:12:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723486360; x=1724091160; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vL+nA7AT6HH2fptU3fDtubaziCDql6jx+j3l3b3Xtg8=; b=u2nZOiLTKHmeVQDekWbo/Nggf+dSp57wlaXpdAV0t4+ElW6JHb5dknjo/LY8j58Or+ 4yV+1q9hvodsYavOTORCfi4xMEbQqsObiYnHs+WNUgBRH4u/0wL0ippdsV5/OUPK/YNq rAJGn52H4wm8nQBJy0/0G2eB3sJZCCD/gDiY/X1HT63FH/lsbJA8TzSS5ITuYc/+TT0e ZylO0K7N9tF28XvTYPQNHJx7R93+LddPZBr6Olh4uHkYB6+5U0+YEvSwapuRQF+k27vt GaqqUjlHXGug9a7Lpc5Ilh/Uyw3GXDZ0O8MqbuG+Bndtwqz0sS7/VkA6p4j0CDBFmU2o W3Kg== X-Forwarded-Encrypted: i=1; AJvYcCWu8u1llPX+MpOv26Prp6M36cUdKO5Cd3dv//TV+CLjG9rvj7YQN+Qd3CxkSIKG9p6ayjvuXU7Hiw5MZkov66Ze9y8= X-Gm-Message-State: AOJu0YxSgdiCV8kG/HIBMYxHQnBoUbeZzDSHUlQalzgu0WehJjGjL6rD 5RQZOvUi5bc7/U7UflI8j749VuANODZKPoXCmaICRCaQG8t2ccMxy8sl36OEOrCGOfXrRQCwK0m PWAmucV+b/AdSBYQWo1auNaSij0FD0gCnDkD8bQG+0UWUM5xh X-Received: by 2002:a05:620a:d95:b0:7a1:5683:b04b with SMTP id af79cd13be357-7a4e1625378mr68903985a.9.1723486360175; Mon, 12 Aug 2024 11:12:40 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHermK5CePcyhPQZfo7QtCxftIfNR9zVAKZznJ8AtnLMjyZZZpq0Zdfjnp4iDQgBs3zunmzLQ== X-Received: by 2002:a05:620a:d95:b0:7a1:5683:b04b with SMTP id af79cd13be357-7a4e1625378mr68901885a.9.1723486359781; Mon, 12 Aug 2024 11:12:39 -0700 (PDT) Received: from x1n.redhat.com (pool-99-254-121-117.cpe.net.cable.rogers.com. [99.254.121.117]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7a4c7dee013sm268663985a.84.2024.08.12.11.12.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Aug 2024 11:12:39 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: "Kirill A . Shutemov" , Nicholas Piggin , David Hildenbrand , Matthew Wilcox , Andrew Morton , James Houghton , Huang Ying , "Aneesh Kumar K . V" , peterx@redhat.com, Vlastimil Babka , Rick P Edgecombe , Hugh Dickins , Borislav Petkov , Christophe Leroy , Michael Ellerman , Rik van Riel , Dan Williams , Mel Gorman , x86@kernel.org, Ingo Molnar , linuxppc-dev@lists.ozlabs.org, Dave Hansen , Dave Jiang , Oscar Salvador , Thomas Gleixner Subject: [PATCH v5 5/7] mm/x86: Implement arch_check_zapped_pud() Date: Mon, 12 Aug 2024 14:12:23 -0400 Message-ID: <20240812181225.1360970-6-peterx@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240812181225.1360970-1-peterx@redhat.com> References: <20240812181225.1360970-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspam-User: X-Stat-Signature: a5psuw6ezxdbfg4d5yzogn4jm7hxrkqo X-Rspamd-Queue-Id: ACA2C4001B X-Rspamd-Server: rspam11 X-HE-Tag: 1723486364-365483 X-HE-Meta: U2FsdGVkX18QOHCbsw+hub+zwTNO+SURPRJHLlFR9oOS/wWeIyFzo/c5LtVY0kPIF65YMfHbfvfud3zg/1wTyzJQ2/Yh83/VvKCtwQY/oJqfSw8588PlKDnwCJgyxKGiMmE9Qt3HyprHbkxhEvhmgHKJ+Rox5eFX3/YLCrfJyT/UokRrrpUBiZngQdQYXHni649b/eu+fk5TdIG96KkDZOA4yeTyAF2TYuBseut1yfXoZi9i9i45fnXX0xiO0nsJgWenotQ9IKJjVKSCz6qExHLzVehu5FTpFZvxwNvakweoWQvzSBE/5GoyTdF58iR65SUXJCi7LvUsD/USN5ju+hj55HBUwO2yHVBgX/ZELghJbIBjmQTY96zIpxK3vG4n+GnsqStlnuSabIcGmuH0adzdONADTL8tbWxnXQ2eGerAnQAGMMeyd0r+gzfrW8iDMLab3Df9zOpHj0OE+dwEb4aGl2ELc06Ivfhn+m8+Wke36f2fuTxpnyuo1GQVsz+mdu+SwsvFn9A1TXkr2vazczj7SOD+Y2WJ+P18ejfy2TFN6c7M/kSeDnKTUfOMgetEvHRAwmKj1lvxAMEDUvIdcQaEBoaYy07uEoZ6ALAlzxCPBgdLKTd8oqgD3os986yhzaqgPgxKpeSY85Q9cPP5g+4nbRmBYuInVO1eowvWWvJYKrjejL3+5nCTDge84IhgFlCHL9Bunma1+JGdwnZIxQNXJj5ATtqGTKZEFwc1TWM9kLeUsVJfpdtiIEMWfK9S36u8DFiNrM2O88RxpM6s7nLQTDKQickTQIOn9xVSg05bCRIwEpDTuB+G+mSuVAZkgzsr/ctaEdcS5iw6+/HMtKobqecVRcKk7eFybPRCpCE2u/CrBgTcf0P2NhwdH6Ab4okCqmiL8VXyUM2+dEEaHDHz3JaVz4br3ENCdQi9D70iJED99d+KDp1YrSVdzhP36eslEdewUwILH/JCv98 c4cI5X5F TdJU52f1B3DRjLHuWaAx5rqDA3SveT2rA0fpSP2aMOP9VpssDWveZG3S5TDDj+RInNPqTOBZ04VjDwD0LMAMC+7zyJkfqjV0eS+pZBwkYGh6hsELBEHD04XgocyasWfXdrf9H8a8Hi8hZu7xS1AM+HGIZVQ2ZBzateGfVYZmZeszvpGUg1kTfQc9/cut8FfsyWDrN4/B2tNx6XbplYi6MGOodzUXdZYAAc8fXZgIAtRKV525bvDc1T1+9Li3xM9WCLK/LMUcnSz0PbDEWqFnbWFBZS9tpM+kpJcV5L00f/q5XdqxL74Op7kGvOUl1gvReCDVVgtTLNjEVEmSVzNZT25CzE073Ca5BJFamTwVA1nkd6dnQ7rPktp3+V1HtrT8wwZTvbh4cTteV6nFQN5449UPuB3UGlzDKGUoaF2zfLqJA+Do1U+HvQN918bkKQ33GP7Z9d4FZvNuDZmde1TYOInEYb4/PXhdaAdA5AR+XLz4gNJg4EgeMEfGHIsc1oYo6yE/mIJxnLo1YjgQ= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Introduce arch_check_zapped_pud() to sanity check shadow stack on PUD zaps. It has the same logic as the PMD helper. One thing to mention is, it might be a good idea to use page_table_check in the future for trapping wrong setups of shadow stack pgtable entries [1]. That is left for the future as a separate effort. [1] https://lore.kernel.org/all/59d518698f664e07c036a5098833d7b56b953305.camel@intel.com Cc: "Edgecombe, Rick P" Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Dave Hansen Cc: x86@kernel.org Acked-by: David Hildenbrand Signed-off-by: Peter Xu --- arch/x86/include/asm/pgtable.h | 10 ++++++++++ arch/x86/mm/pgtable.c | 6 ++++++ include/linux/pgtable.h | 6 ++++++ mm/huge_memory.c | 4 +++- 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index a2a3bd4c1bda..fdb8ac9e7030 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -174,6 +174,13 @@ static inline int pud_young(pud_t pud) return pud_flags(pud) & _PAGE_ACCESSED; } +static inline bool pud_shstk(pud_t pud) +{ + return cpu_feature_enabled(X86_FEATURE_SHSTK) && + (pud_flags(pud) & (_PAGE_RW | _PAGE_DIRTY | _PAGE_PSE)) == + (_PAGE_DIRTY | _PAGE_PSE); +} + static inline int pte_write(pte_t pte) { /* @@ -1667,6 +1674,9 @@ void arch_check_zapped_pte(struct vm_area_struct *vma, pte_t pte); #define arch_check_zapped_pmd arch_check_zapped_pmd void arch_check_zapped_pmd(struct vm_area_struct *vma, pmd_t pmd); +#define arch_check_zapped_pud arch_check_zapped_pud +void arch_check_zapped_pud(struct vm_area_struct *vma, pud_t pud); + #ifdef CONFIG_XEN_PV #define arch_has_hw_nonleaf_pmd_young arch_has_hw_nonleaf_pmd_young static inline bool arch_has_hw_nonleaf_pmd_young(void) diff --git a/arch/x86/mm/pgtable.c b/arch/x86/mm/pgtable.c index f5931499c2d6..36e7139a61d9 100644 --- a/arch/x86/mm/pgtable.c +++ b/arch/x86/mm/pgtable.c @@ -926,3 +926,9 @@ void arch_check_zapped_pmd(struct vm_area_struct *vma, pmd_t pmd) VM_WARN_ON_ONCE(!(vma->vm_flags & VM_SHADOW_STACK) && pmd_shstk(pmd)); } + +void arch_check_zapped_pud(struct vm_area_struct *vma, pud_t pud) +{ + /* See note in arch_check_zapped_pte() */ + VM_WARN_ON_ONCE(!(vma->vm_flags & VM_SHADOW_STACK) && pud_shstk(pud)); +} diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h index 2a6a3cccfc36..780f3b439d98 100644 --- a/include/linux/pgtable.h +++ b/include/linux/pgtable.h @@ -447,6 +447,12 @@ static inline void arch_check_zapped_pmd(struct vm_area_struct *vma, } #endif +#ifndef arch_check_zapped_pud +static inline void arch_check_zapped_pud(struct vm_area_struct *vma, pud_t pud) +{ +} +#endif + #ifndef __HAVE_ARCH_PTEP_GET_AND_CLEAR static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long address, diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 0024266dea0a..81c5da0708ed 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2293,12 +2293,14 @@ int zap_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma, pud_t *pud, unsigned long addr) { spinlock_t *ptl; + pud_t orig_pud; ptl = __pud_trans_huge_lock(pud, vma); if (!ptl) return 0; - pudp_huge_get_and_clear_full(vma, addr, pud, tlb->fullmm); + orig_pud = pudp_huge_get_and_clear_full(vma, addr, pud, tlb->fullmm); + arch_check_zapped_pud(vma, orig_pud); tlb_remove_pud_tlb_entry(tlb, pud, addr); if (vma_is_special_huge(vma)) { spin_unlock(ptl);