Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp2834744rwb; Thu, 29 Sep 2022 16:04:56 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7f0txZsbQh2FZTQq8HO8qM37Ubxls6R9GDBRDwvMN88hHpcdurFoh9iKZEyL1IjOhfVzpb X-Received: by 2002:a63:4ce:0:b0:43c:7997:8bd6 with SMTP id 197-20020a6304ce000000b0043c79978bd6mr4975711pge.202.1664492696008; Thu, 29 Sep 2022 16:04:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664492696; cv=none; d=google.com; s=arc-20160816; b=E9ajhVLDyt6f0JooM2DH72ZH7xTm7+k/boi4S7756KFHl25MvTIOEpzpiM1qgNh3JZ aHJMRzdmcFU095kbgX8D+fjrEfe/aoBjtV4nRKkedFzd1dZPk87xZKkwal9CqbtqDptD fDAGP7YiBdk3Gx1gCtPSwl2BBwG3gk9KgmtraPF66XoUTCdFIcTCdCjPNUTTPB5bK7wQ DjDImqjA76/SDAnp77AHbRdIK5mcJVGuIuBMJTTLRXZun1t62/gEjjqb1cY1spUEVdRf wtSXYVcUFRpKun32WpbAzZU91TacE5SDF4I7BxCI0Y7Egfzpj/OIdKN0yJYd/Aex3ZlY IAHw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=EksmLWFFd0h6xLIFCIvhVRecSrF3cMgLHZG5B52jltk=; b=h+a9DW5eQyMjBVNdkYmpxq8tECQzodhaq0UflZLg/7oi6S28mOaf2+kG19/+QLtu1Y 2Xs/U3SMPoUojFc0yokXqQZB6c3XLM3QioEVyupMhyP34ctPvkf2Eb6yuMInP5RkaH9U CMfUecLFExkAc65D7hW7wc4RIuwVLXThcfzvb8LeIpyp+lLdN1fu0AuNtlIflErxeVd7 lc+Cw7pD355C2LLffPcI4Ue14wvEzuBrao4qGEwBIxzZWj1SPfh15jVTZyg1K8FCAUsc lu5m46NnZSA8qdne7q7QUCsj09Si46WLCMcRyYgrImxQ+yrYtMGJC+AS90/vpDbTJV13 okww== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="X/FdhvmO"; 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=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n10-20020a170902f60a00b0017890fc4784si1047535plg.349.2022.09.29.16.04.44; Thu, 29 Sep 2022 16:04:55 -0700 (PDT) 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; dkim=pass header.i=@intel.com header.s=Intel header.b="X/FdhvmO"; 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=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230044AbiI2WeD (ORCPT + 99 others); Thu, 29 Sep 2022 18:34:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53722 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229754AbiI2WdA (ORCPT ); Thu, 29 Sep 2022 18:33:00 -0400 Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7F84BA6C16; Thu, 29 Sep 2022 15:30:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1664490660; x=1696026660; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=HEoICz/7FChfhD1X/KoxDXONV3hJ7PY4eGyUJG+8lDI=; b=X/FdhvmOPqn0W16yYR15aQTp+S+IV6aZKGBaPyqvU/lLHMIUK5FeXd9+ 60ysdvJFhCM6pXhvbFaiYlpa6KWB6JWpyVFy9FhaERO4iF9+D51tH5CuX 59Ol/dkKOtKbNYjCil5udRCuDyalCHPmBOkB6E0B6/TLB/8FMsJ80thhS 9FIlLn064HP+TG0T/no65cjbdiX00DWdEpfzMtvCAP4rOvMcew5figX8Y CNmVdeb6tK0fV5g3e46YEE0PDT2uVOsMHYE8DovmFZJPY8Ha8iP3J5oFI tie3gG1ocbSfg2rH+Kfup3q8zY1tt2KIznx58FU0Cs5geLmNVi0QksnWc Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10485"; a="285181991" X-IronPort-AV: E=Sophos;i="5.93,356,1654585200"; d="scan'208";a="285181991" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Sep 2022 15:30:26 -0700 X-IronPort-AV: E=McAfee;i="6500,9779,10485"; a="691016264" X-IronPort-AV: E=Sophos;i="5.93,356,1654585200"; d="scan'208";a="691016264" Received: from sergungo-mobl.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.251.25.88]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Sep 2022 15:30:24 -0700 From: Rick Edgecombe To: x86@kernel.org, "H . Peter Anvin" , Thomas Gleixner , Ingo Molnar , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, Arnd Bergmann , Andy Lutomirski , Balbir Singh , Borislav Petkov , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Florian Weimer , "H . J . Lu" , Jann Horn , Jonathan Corbet , Kees Cook , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , "Ravi V . Shankar" , Weijiang Yang , "Kirill A . Shutemov" , joao.moreira@intel.com, John Allen , kcc@google.com, eranian@google.com, rppt@kernel.org, jamorris@linux.microsoft.com, dethoma@microsoft.com Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu Subject: [PATCH v2 20/39] mm/mprotect: Exclude shadow stack from preserve_write Date: Thu, 29 Sep 2022 15:29:17 -0700 Message-Id: <20220929222936.14584-21-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220929222936.14584-1-rick.p.edgecombe@intel.com> References: <20220929222936.14584-1-rick.p.edgecombe@intel.com> X-Spam-Status: No, score=-2.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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 From: Yu-cheng Yu In change_pte_range(), when a PTE is changed for prot_numa, _PAGE_RW is preserved to avoid the additional write fault after the NUMA hinting fault. However, pte_write() now includes both normal writable and shadow stack (Write=0, Dirty=1) PTEs, but the latter does not have _PAGE_RW and has no need to preserve it. Exclude shadow stack from preserve_write test, and apply the same change to change_huge_pmd(). Signed-off-by: Yu-cheng Yu Reviewed-by: Kirill A. Shutemov Signed-off-by: Rick Edgecombe --- Yu-cheng v25: - Move is_shadow_stack_mapping() to a separate line. Yu-cheng v24: - Change arch_shadow_stack_mapping() to is_shadow_stack_mapping(). mm/huge_memory.c | 7 +++++++ mm/mprotect.c | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 11fc69eb4717..492c4f190f55 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1800,6 +1800,13 @@ int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, return 0; preserve_write = prot_numa && pmd_write(*pmd); + + /* + * Preserve only normal writable huge PMD, but not shadow + * stack (RW=0, Dirty=1). + */ + if (vma->vm_flags & VM_SHADOW_STACK) + preserve_write = false; ret = 1; #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION diff --git a/mm/mprotect.c b/mm/mprotect.c index bc6bddd156ca..983206529dce 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -114,6 +114,13 @@ static unsigned long change_pte_range(struct mmu_gather *tlb, pte_t ptent; bool preserve_write = prot_numa && pte_write(oldpte); + /* + * Preserve only normal writable PTE, but not shadow + * stack (RW=0, Dirty=1). + */ + if (vma->vm_flags & VM_SHADOW_STACK) + preserve_write = false; + /* * Avoid trapping faults against the zero or KSM * pages. See similar comment in change_huge_pmd. -- 2.17.1