From patchwork Mon Jul 15 19:21:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13733837 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 082E0C3DA59 for ; Mon, 15 Jul 2024 19:22:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 348B96B009E; Mon, 15 Jul 2024 15:22:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2A2BD6B00A1; Mon, 15 Jul 2024 15:22:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D96B56B009F; Mon, 15 Jul 2024 15:22:04 -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 A07EA6B009D for ; Mon, 15 Jul 2024 15:22:04 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 50A7A1201B7 for ; Mon, 15 Jul 2024 19:22:04 +0000 (UTC) X-FDA: 82342957368.24.F8E1B2C Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf30.hostedemail.com (Postfix) with ESMTP id 493EC8001B for ; Mon, 15 Jul 2024 19:22:02 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=ZQ1PeKSH; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf30.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721071280; 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=9JbEXa1vvY/QVlVssvu52FPZK180R34zP6ifpD40mks=; b=4mOHncNIIKKPRCfIIFcu05oCTu5QYVysHJd9E3HqivHJ+mWDXPTpytnXI7Xg2Wyoc1u9tZ vHAG77TMFOOwmcr5AF6yIHvtDyBWSh3AabHpw/RuA0NteZux6qF2wmrRVSvFaBygRgmpYY p31gQc2Gq1/L7WGEVcwsN/ImzQA45Cg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721071280; a=rsa-sha256; cv=none; b=spQhQTdrqe+ILKyNRh4yV0qrarBJrxXgJ4Nh1Ilpl1AHpGdzqhvV/ImKgiy/Yfqda8VhfI hvuU6zbqqUsAYOXtm7WTYN4fcW6JDbeeDoF/TcCu+0OlHogE0HtbCdkO2ZSIW8f86GeKbU k7G460PN7L6lM1LCpgpLLo9gsKHG8ig= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=ZQ1PeKSH; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf30.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1721071321; 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=9JbEXa1vvY/QVlVssvu52FPZK180R34zP6ifpD40mks=; b=ZQ1PeKSHc8PZc6E5seHJ5P7PN3LdirgMsFVEYuMcc6YrUko6YSiZyF9pNUQJF0xpI68e0f f8kp0IFzn4VFUWWoM3uoU9BPix0eeBCxzqW7oVNqP956Ix/AW09NJ9+tjVOGyzZsfz7fcY pBDd7oK/GddPOs0Z7AgA/7l0klI9mQA= Received: from mail-yb1-f200.google.com (mail-yb1-f200.google.com [209.85.219.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-488-GInvCMcxOqyjWo2KsVnA7w-1; Mon, 15 Jul 2024 15:22:00 -0400 X-MC-Unique: GInvCMcxOqyjWo2KsVnA7w-1 Received: by mail-yb1-f200.google.com with SMTP id 3f1490d57ef6-e05a1434e17so676212276.0 for ; Mon, 15 Jul 2024 12:22:00 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721071319; x=1721676119; 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=9JbEXa1vvY/QVlVssvu52FPZK180R34zP6ifpD40mks=; b=fY9MR/BTp3+VjrjjTj24Ax5YEtLIgPwc0H2oWJGMs8B3Vm19AoyOHnst7q9Z1Wxmpx erjBJ46GT14hqOmu3J6+pzpc95ZfQIHPiAjN7613PdlnzkeOMBYtX34KeVBU3lS/Jygk dGuN7Y6KnUtKJRZgGqbIxjybRp7F/PiwEU+4TAyE6NGuUduf9puBvB2yY6F1PHQSZiYC 2Dk37u6N+mrq52aGpQ2CMtdOHNOm4jEKFgS6zlKgKRRG9U8xIEQPwusmgXFdcjNSMifc zQRddJp8K30JRzZK7Simkty3eunpI6EtK6KURNbJXitcmjwYP2+6B8sclvZixeAZPscC F5rA== X-Gm-Message-State: AOJu0YzYNfSS4IIUw42qnn4LuqyVKXN0HCdTrfJFOmLRPvNFcGx99G3+ 7qKyHen+DPESuD7Rg+68bAUplqRz5E55gHm55dau745qxSJdXUTT5W31C8G4nd26fy8TL4iBKPA eKFPNY9nt0uGoQJG/SQAE12ix8QogqmxaKUWUHKaI8Lelq6GIGxEXaDCVMBn1jtnzTwP6v6aD0Y pCys8SWTVqIbvkirOPvxmq8tvdugOf7g== X-Received: by 2002:a25:2d11:0:b0:e05:900d:32e6 with SMTP id 3f1490d57ef6-e05d3c17338mr350147276.8.1721071319301; Mon, 15 Jul 2024 12:21:59 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFmDbyBksbC4qmpVrfmmZ0A/VGjIJHLQ2OfuPZrkWmoJhvnJgk6xUDZyR/TdOg4zaQrzDq1HQ== X-Received: by 2002:a25:2d11:0:b0:e05:900d:32e6 with SMTP id 3f1490d57ef6-e05d3c17338mr350112276.8.1721071318815; Mon, 15 Jul 2024 12:21:58 -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 6a1803df08f44-6b761978d30sm24039356d6.31.2024.07.15.12.21.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Jul 2024 12:21:58 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Dave Jiang , Rik van Riel , Dave Hansen , Michael Ellerman , linuxppc-dev@lists.ozlabs.org, Matthew Wilcox , Rick P Edgecombe , peterx@redhat.com, Oscar Salvador , Mel Gorman , Andrew Morton , Borislav Petkov , Christophe Leroy , Huang Ying , "Kirill A . Shutemov" , "Aneesh Kumar K . V" , Dan Williams , Thomas Gleixner , Hugh Dickins , x86@kernel.org, Nicholas Piggin , Vlastimil Babka , Ingo Molnar Subject: [PATCH v3 6/8] mm/x86: arch_check_zapped_pud() Date: Mon, 15 Jul 2024 15:21:40 -0400 Message-ID: <20240715192142.3241557-7-peterx@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240715192142.3241557-1-peterx@redhat.com> References: <20240715192142.3241557-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rsp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8zW7+Je99VO7AOk2dVys oXeboiwO SFV72rgPLsA15jFELI3zzv1GZft0ltNn7CyvbXu7s2qJsqDO/Z9QS32SxF/QLMURxHODdhyuzEqz+70WdGy1SKKGjED8JsycwCvmpv9/2E3eEpPIo6AAliue/RG/sE/SkKiK0g9n1urSC0/8dFn3VouaEFjLsH8JdEUIAk3O7ebZ+UmY4dX1SMOdE4fGEeRDxIMRl5Y7AEP8zgf7QtqEfqBdntIQwKy6P2/k01xIp7KpNm0hFzV3mAUT7MOaC+v8xfMs/tkmTxkFfPZO0gAzJee5U1twEUPRBjp076ikEJW4xbTDd1v/MwZn5pndydDDJxaOw+DMHC8bnkTzgtIntKUFDXtuCj4JQ9ESbLZB8+vFJ45dh8ki5TpB5Angopp+Hyha6g1vBfVhEyQdGsdIvo5KaoAZcNlPV1ZvfzXj5MXWrHoosgggHRH6i4aalNYTdOgGLNdi+Vpi4CqAoLtUkYb66TKKgyghmeYzr4TJIFyy8e6onznHNvFggZKLpweEVJrzmLKCI1mC1Jsc= 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 of 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 Signed-off-by: Peter Xu Acked-by: David Hildenbrand --- arch/x86/include/asm/pgtable.h | 10 ++++++++++ arch/x86/mm/pgtable.c | 7 +++++++ include/linux/pgtable.h | 7 +++++++ mm/huge_memory.c | 4 +++- 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index 25fc6d809572..cdf044c2ad6e 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -169,6 +169,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) { /* @@ -1662,6 +1669,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 93e54ba91fbf..564b8945951e 100644 --- a/arch/x86/mm/pgtable.c +++ b/arch/x86/mm/pgtable.c @@ -926,3 +926,10 @@ 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..2289e9f7aa1b 100644 --- a/include/linux/pgtable.h +++ b/include/linux/pgtable.h @@ -447,6 +447,13 @@ 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 9fec5bd1c8b0..c10247bef08a 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2291,12 +2291,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);