Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp4832448pxy; Tue, 27 Apr 2021 13:47:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwZ6V/WPkPcQptGCYGM71c2MZI2Bcy2wlCvtbVeNbqljE6QWfK+FoZD8Q5P2wPZpFpM+PM0 X-Received: by 2002:a63:144d:: with SMTP id 13mr16176732pgu.138.1619556461235; Tue, 27 Apr 2021 13:47:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619556461; cv=none; d=google.com; s=arc-20160816; b=aYnm5pNeHLvsFqmJ7Rh3LtlcCnAfzZABEUEoKmmT9vDUcBZ+Hm/AEl4cfYwZ6JagiM 7aZdapS+tPmOLxuGUQDu6xxacHZuU+aPP85huH1oBmZiRxDdeH5o7RjwL/8si1ogmXWU F+ESBXS3wBgmZdwa0pl8es7Y9/MFZ5Qo4q/3EF19szwYpd5+CbHlm+yXE4wm9ar03pku Rsz9xUG9SmVzwxKY2G+gzo7sK9P9fRHRuMadXmHGH5Xs7luu+9mPtPOumDaw9VgIldG8 iD4wysZilL43xqxjpfTYqIJ1fUtQGiwQ4Ufauch6ovN7ddKbS0s5O10wu2Wa29eJJuzj o57w== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :ironport-sdr:ironport-sdr; bh=W3Vq/CK1asp10ltg7FDEyMnjnLLdgwJbzxOqvJvtVQk=; b=oHb+fiCc/sQ6LozZCmpu/xaEUojjdjITYvlkgb945X7GfEeNjirB3EYqybwjnDeO6Z 1XZWuNDout0aM4pB3/eifSNDxZlSzDfColVoAIE06UQt/OVyEntMWx1XpKGYoTIyz+dP RiW6WuY632at+uHkSm11HHP99RXEKo3V6H+qbOwfJc1kK0BIkRk9Vs8RMMbuSLs0inJ9 fNnY/Ri2Zy2ExbL/lNGMn+c4uz4HJmYgQ/lAv1077gG4OqUuqzIRLxnyEb0vBiags5Bu YOlg53nr59eOLPbAQp+BcpwUZo3MK9Ov6fezmhxsRxmi1PdBzDLZ/Oi5RKps3IhwobTc 6iEg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id j18si1002911pga.523.2021.04.27.13.47.28; Tue, 27 Apr 2021 13:47:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239164AbhD0UqL (ORCPT + 99 others); Tue, 27 Apr 2021 16:46:11 -0400 Received: from mga05.intel.com ([192.55.52.43]:31782 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239251AbhD0Upq (ORCPT ); Tue, 27 Apr 2021 16:45:46 -0400 IronPort-SDR: vbMeYzD8yeVDC2pGnOGfL/vDlcI77Nk5otFKMcxn4y7M/RTHY/p8id409pIQSOiZOX4t91IolQ ep0fSln38xPA== X-IronPort-AV: E=McAfee;i="6200,9189,9967"; a="281922485" X-IronPort-AV: E=Sophos;i="5.82,255,1613462400"; d="scan'208";a="281922485" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Apr 2021 13:44:18 -0700 IronPort-SDR: dM0R5wTMXYwpzyKBQurBFk62wo0FSpBQUK57Ki20/BKmhEfoS5LHP+hQ3m4G9A3MrpvFZEUc0F XyMeDTJkqonQ== X-IronPort-AV: E=Sophos;i="5.82,255,1613462400"; d="scan'208";a="465623528" Received: from yyu32-desk.sc.intel.com ([143.183.136.146]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Apr 2021 13:44:18 -0700 From: Yu-cheng Yu 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" , Vedvyas Shanbhogue , Dave Martin , Weijiang Yang , Pengfei Xu , Haitao Huang Cc: Yu-cheng Yu , "Kirill A . Shutemov" Subject: [PATCH v26 18/30] mm/mmap: Add shadow stack pages to memory accounting Date: Tue, 27 Apr 2021 13:43:03 -0700 Message-Id: <20210427204315.24153-19-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20210427204315.24153-1-yu-cheng.yu@intel.com> References: <20210427204315.24153-1-yu-cheng.yu@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Account shadow stack pages to stack memory. Signed-off-by: Yu-cheng Yu Reviewed-by: Kirill A. Shutemov Cc: Kees Cook --- v26: - Remove redundant #ifdef CONFIG_MMU. v25: - Remove #ifdef CONFIG_ARCH_HAS_SHADOW_STACK for is_shadow_stack_mapping(). v24: - Change arch_shadow_stack_mapping() to is_shadow_stack_mapping(). - Change VM_SHSTK to VM_SHADOW_STACK. arch/x86/include/asm/pgtable.h | 3 +++ arch/x86/mm/pgtable.c | 5 +++++ include/linux/pgtable.h | 7 +++++++ mm/mmap.c | 5 +++++ 4 files changed, 20 insertions(+) diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index da5dea417663..7f324edaedfa 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -1692,6 +1692,9 @@ static inline bool arch_faults_on_old_pte(void) #define maybe_mkwrite maybe_mkwrite extern pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma); +#define is_shadow_stack_mapping is_shadow_stack_mapping +extern bool is_shadow_stack_mapping(vm_flags_t vm_flags); + #endif /* __ASSEMBLY__ */ #endif /* _ASM_X86_PGTABLE_H */ diff --git a/arch/x86/mm/pgtable.c b/arch/x86/mm/pgtable.c index e778dbbef3d8..b6ce620922e0 100644 --- a/arch/x86/mm/pgtable.c +++ b/arch/x86/mm/pgtable.c @@ -897,3 +897,8 @@ int pmd_free_pte_page(pmd_t *pmd, unsigned long addr) #endif /* CONFIG_X86_64 */ #endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */ + +bool is_shadow_stack_mapping(vm_flags_t vm_flags) +{ + return vm_flags & VM_SHADOW_STACK; +} diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h index 5e772392a379..d9af6f9aeef1 100644 --- a/include/linux/pgtable.h +++ b/include/linux/pgtable.h @@ -1446,6 +1446,13 @@ static inline bool arch_has_pfn_modify_check(void) } #endif /* !_HAVE_ARCH_PFN_MODIFY_ALLOWED */ +#ifndef is_shadow_stack_mapping +static inline bool is_shadow_stack_mapping(vm_flags_t vm_flags) +{ + return false; +} +#endif + /* * Architecture PAGE_KERNEL_* fallbacks * diff --git a/mm/mmap.c b/mm/mmap.c index 3f287599a7a3..d77fb39b6ab5 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1718,6 +1718,9 @@ static inline int accountable_mapping(struct file *file, vm_flags_t vm_flags) if (file && is_file_hugepages(file)) return 0; + if (is_shadow_stack_mapping(vm_flags)) + return 1; + return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) == VM_WRITE; } @@ -3387,6 +3390,8 @@ void vm_stat_account(struct mm_struct *mm, vm_flags_t flags, long npages) mm->stack_vm += npages; else if (is_data_mapping(flags)) mm->data_vm += npages; + else if (is_shadow_stack_mapping(flags)) + mm->stack_vm += npages; } static vm_fault_t special_mapping_fault(struct vm_fault *vmf); -- 2.21.0