From patchwork Wed Jul 3 21:29:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13722802 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 29346C2BD09 for ; Wed, 3 Jul 2024 21:29:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5999E6B0096; Wed, 3 Jul 2024 17:29:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4D2F96B009B; Wed, 3 Jul 2024 17:29:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 12E936B0099; Wed, 3 Jul 2024 17:29:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id D556C6B0098 for ; Wed, 3 Jul 2024 17:29:38 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 79DF91C1C40 for ; Wed, 3 Jul 2024 21:29:38 +0000 (UTC) X-FDA: 82299733236.22.1DE5838 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf20.hostedemail.com (Postfix) with ESMTP id 5C7E51C001D for ; Wed, 3 Jul 2024 21:29:36 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=QjPdYvAF; spf=pass (imf20.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=1720042164; 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=8uNV81qP1pkF0zqyhqKWAPXX0CBAvqkfZkOGu2qHMfo=; b=PsrXWBQn9bPZ/oprQBYOLFOugIMLmL5JoT8DLZJdukd6zh1ziMcO+cKHEK8JQvcu83NyVg 5vGOY30FJq8pGngbCvn7jSv4f3eMrs3ceuL6VyLnZswnSJZdXOQUiMcafJc1Pdo5Y4Lrwo 35JXAhNDJPXAWBdmTz55V7CDuov0KVQ= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=QjPdYvAF; spf=pass (imf20.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=1720042164; a=rsa-sha256; cv=none; b=72x1jkMMEWmvm9cy2gzhz1BZ2EgYBTYzlgRvO9wQdR3g6PsnkK2V2t6jo1fQPD7Pni4WYD 3NRXaY7kL7Gtbt2k0asPmeQEQH68r2vADuIYb/99LXBqE8W0cQOUd+cITT+F8weJ+IhbK0 AicNHWm098pS/GfYotiw43jC4O28lWk= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1720042175; 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=8uNV81qP1pkF0zqyhqKWAPXX0CBAvqkfZkOGu2qHMfo=; b=QjPdYvAFiko/2nij1EoOB/nUFnIBnDIcKIrG8cBPvuMQcVnUbaN0kObfi6miv6QXO/r2rW nzTNouNbD2TsWXXpgGoqYFSPFBd1TwGkkRYMJzgsen/PTQJgZewu+ih+kLVGLzPCiWXmJo O4YcF0e841agdU2H8Gt9jUcNw20cvCk= Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-168-lj_zvRZCMTeW3YSxl-uFuw-1; Wed, 03 Jul 2024 17:29:34 -0400 X-MC-Unique: lj_zvRZCMTeW3YSxl-uFuw-1 Received: by mail-qt1-f198.google.com with SMTP id d75a77b69052e-444fbad91f3so3806611cf.3 for ; Wed, 03 Jul 2024 14:29:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720042174; x=1720646974; 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=8uNV81qP1pkF0zqyhqKWAPXX0CBAvqkfZkOGu2qHMfo=; b=iSW5CegkQr0IsmjDp1Ok3Dt+b21IVOSBjtlzzNY+g4nNKqklnqsUGXX54ZOr3mSrWm GEXbTOTxxZNVJrAJjmf8jX169gzImjo69FtxhC8JTPcYjCcY7n7fXICSwH/5th8yk1bQ MKY2pn52t2gQ49b143FS04LkkomxrAy/+0N0p6zyi1rmT2jQzi2MlNBz9wjSjPYL8CVQ ixHo6TZJMvHc+AELE0LZpPKs/hG36aV/Lo2PVuraqGukP7wRyUbC5H2LDrIrQvEIatWE cRs5JLkvLH4pKPGqVaP5Qj8306Q9M/k+O9SCJBTIn8ElLB0dSZOfhbH9Rd7KxWuU43AZ ENmA== X-Forwarded-Encrypted: i=1; AJvYcCUA6Rbrv0dZao9SIEXxsBE97aLSjgn3dRcFu7Ci8oRpsLkOcp9dwyGec+4S3NZ70XpE68CP04PWQB++ivRXUmcrB4c= X-Gm-Message-State: AOJu0YzrqHdsmjWXrsb9bv0WEl9jXdXYOOG3RoiOnvgmzQBylBIQ47b1 Z7bDuXE+34tMOkUschNPKhynSLQPSaH0C+8hk5KNuaIMIUetT+DG7v/fAXP/RdhWQmymoX6qGbe sefTTQ6su1Sy+wXJj0sgbAvqWCs1rnzm3eQSSMQdQ4NDXeTrQ X-Received: by 2002:a05:620a:470c:b0:79d:8ea3:9d24 with SMTP id af79cd13be357-79d8ea39f72mr1046536785a.2.1720042173826; Wed, 03 Jul 2024 14:29:33 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFduTwlKnISGNZIByqQ7Sp6Ih/1fAT1ztK1vNR6ntfPREjYbbEVcJ6gOgRtr7E4arLOH2JOwA== X-Received: by 2002:a05:620a:470c:b0:79d:8ea3:9d24 with SMTP id af79cd13be357-79d8ea39f72mr1046534585a.2.1720042173510; Wed, 03 Jul 2024 14:29:33 -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 d75a77b69052e-4465143eb1csm54337481cf.57.2024.07.03.14.29.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Jul 2024 14:29:33 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Matthew Wilcox , Mel Gorman , Dave Jiang , linuxppc-dev@lists.ozlabs.org, Michael Ellerman , Rik van Riel , Vlastimil Babka , Nicholas Piggin , Christophe Leroy , Andrew Morton , Huang Ying , Oscar Salvador , "Aneesh Kumar K . V" , Thomas Gleixner , Dave Hansen , x86@kernel.org, Ingo Molnar , "Kirill A . Shutemov" , Dan Williams , Borislav Petkov , peterx@redhat.com, Hugh Dickins , Rick P Edgecombe Subject: [PATCH v2 6/8] mm/x86: arch_check_zapped_pud() Date: Wed, 3 Jul 2024 17:29:16 -0400 Message-ID: <20240703212918.2417843-7-peterx@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240703212918.2417843-1-peterx@redhat.com> References: <20240703212918.2417843-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 5C7E51C001D X-Stat-Signature: yhuqzgbke3nwz71mxdrjyrdbctjt1hhn X-HE-Tag: 1720042176-691439 X-HE-Meta: U2FsdGVkX197Nr3ut1S1RgZWP0n2OpQ1w4dUj7WkqXvG8sZc5boMiL/Vb0zqDW2186OWOC0xm0fogA/kI4ZjtwznebW1T/nULy+rE+aUXtrjJbpdZlckm0V+wbnY7yl3ZX5BAiX1r3d6mXshNcifW1gpqKeXCNIol3NEmGZOnjXRkSXa74Is0+3T3gAlQCv2TMxNe+QRO1/51x3NtFkShe5igKFAzOnFP8kf79JXA4d3m20owW8YApa8cP0RIffiuNNUI9aJwvfP9oDm9zvqK8WJAk2A2V7euCR5mSKgTKjMtF41KvCCljLXMY4C1f259boBktj2718S7imzQpasmJ5Ww+JJMlMinowA0siHLlQdNwasrIfzBIKicqF4NiAm0/I8TuSPxY7MS4yWheYaeLgGw7tOAfi8YBCvjjYU0RnCGNPJayAeQ3Tu7M14QU3lzDZ+LRKgiK55Uz3H3JNPYeOhVAUb++KziRHVmYnI+fsnbZmREdYAp1/ThTjkJfJh7gC+uMhmX84gUjEZrLxSTD9rJ5id8u/i+MjMQUGM+avSrg8MSNN6bHNTIyVEwwQn9mW0hgLQFjdu81O31KfUxUTbIkwl0DwehH4yCdf+gsVL0qA6JVEsXGPZEJ48VBKYchTKTagaNNk8BULkpbgGE5x2FLDzuXEEK1tsq0GAcRCOBuHhhQighgM0UKlhLXGqVfSNktRqDfi/6osjsHcYVi7lNllLdoFn70JKOSYQ1YZRNAz1Wo4Iz+R3ax1JXZ7ijLZ3hYa2eC0gd0ITkjeu5whfEEzL6IHCveTYoyD3225UnB3qOU/oLBclPPGWRhLn85O56o49Fb3psLjokInhDlfJaoDiq93LZ++mEGaOGo5efwf8EGpux6HtfJhc7bXCl5mYFYlSh6VqGBTrZLxBgXDdh/B7BkQVT34v+AiMseis5R/9B09oTI5Yx1rR0iHPKAb/83flXOzr7PGdcyi J2o8LEao tcnW3sdgROJCz2nPgPrJGTf52sdoCypeaY5tTzRDZl6a+e1prF/u3HdDAIT4sbTCegWdSMQmz9cq8FjimyajXyyK9/j18QsKcXmsedmTfgWa1vKRnJsgpf3g1ag9zq/aJEi/oYWL91PsBsAjDIWOsNkIlQpP46Tdq6bS66LvrNSzfIg3wKk6tbv+WIUgUtqh33dbsQFYSl8DcY+CAWe0vcv1JIsfAUXQOQqEJJvDQBQsRp8onwQguuN7sb+l4vmrTLBQGzgW+/24OQ1pLWlvoLroMe3gU28d9uH0jE8S6ohCOAgzagxSTnsG1zPTpDQqxqjyx562zj9SknKtXkJ8XqQC3Z3LHdc4ij7jZezmQsA48rUcHXmFPG+0ILtIPF6Etw+fEy8qO43amPaut+QULCTPjYRqfS/ZPiBNlVYekXsnVy13EAFCBr1V69ypdJH1doT022/c+3ae7cjTrYl5zW9GR3Q== 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 --- 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 251d6932130f..017377920d18 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2278,12 +2278,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);