Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp1690186pxb; Fri, 20 Aug 2021 11:24:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzQ4Ttk7TynJLloFwTCd+3vmWB56PWDtdZLx1A9jUniCyforgUP+osG1Q2hxaH91tAXm35e X-Received: by 2002:a05:6402:8da:: with SMTP id d26mr23426787edz.109.1629483895037; Fri, 20 Aug 2021 11:24:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629483895; cv=none; d=google.com; s=arc-20160816; b=YqXixL1fkB8ZPgqKovGhu9hqbdLNzSx0uIvG+ZDk5aqiWqjkiAHV5/P3ax9VrbWJj4 Od2FbMFF1Y41uh0bZq3a7Ifnmz97/6k6EEhLe29Xv8vVwkarLJxVxPG8FHDMLxblRqWj V3ghMcuCR57ysAewkMugbHWomRXsi0qLMI0QVlgqtxKTvxWb1ZLMgMdGeydosnwUBU+c celpT2s0cQP1ne5umf7KV+Qkf495ScbU2coyMfDIYb4OAiuA4w2lA32V3PMgdV7FCxkM j+c32dvpiUkGCkGnUG8iVwNxjYYd1JeuJKUMej1B3N2LHDA5dhEN7mHtuSOgc1pJhHBA Ew2Q== 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; bh=w9GOrBKZVp5cq6nLQ0uEBJzXPfwJOuOv8vEISoM6p0I=; b=SsBvyDxD3Zs9N4VmG4dmQp34e/y73Jk7+mtwmfEG2Y8C8a4yB54XPwAkJetaW+DXi/ D/eRd9z+yWhv4FF3FP4gpc02Qe7dgaxwL0Uq6ju4gcIzz4U7LBGAU7xWrlqVJxa9Yv9L lncT1pVC4Cr8EeI1E5Obi8evQjJ0fs5jY861HPE8B3tb/8l8lCBHjrfdhzON26kRcPm3 4iuUicZMaTRXxrlq1KVoXrDQfJsBYb7xFZvW/pvi5QShrJ3v3pVMm7BqvculG3wy/Gjc FspGoubGg+EQ7CqMziRpm39JZK0U+cDJQq9ZZuZYCdUfecyFjKZxCDLnE44FG2qchSCy D9qQ== 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 j2si7138220edr.443.2021.08.20.11.24.30; Fri, 20 Aug 2021 11:24:55 -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 S238379AbhHTSVj (ORCPT + 99 others); Fri, 20 Aug 2021 14:21:39 -0400 Received: from mga05.intel.com ([192.55.52.43]:22429 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237450AbhHTSUl (ORCPT ); Fri, 20 Aug 2021 14:20:41 -0400 X-IronPort-AV: E=McAfee;i="6200,9189,10082"; a="302407829" X-IronPort-AV: E=Sophos;i="5.84,338,1620716400"; d="scan'208";a="302407829" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Aug 2021 11:18:49 -0700 X-IronPort-AV: E=Sophos;i="5.84,338,1620716400"; d="scan'208";a="533074743" Received: from yyu32-desk.sc.intel.com ([143.183.136.146]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Aug 2021 11:18:49 -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" , Dave Martin , Weijiang Yang , Pengfei Xu , Haitao Huang , Rick P Edgecombe Cc: Yu-cheng Yu , "Kirill A . Shutemov" Subject: [PATCH v29 19/32] mm/mmap: Add shadow stack pages to memory accounting Date: Fri, 20 Aug 2021 11:11:48 -0700 Message-Id: <20210820181201.31490-20-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20210820181201.31490-1-yu-cheng.yu@intel.com> References: <20210820181201.31490-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 bfe4ea2b652d..0983a91b464c 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -1638,6 +1638,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 c22c8e9c37e8..61a364b9ae0a 100644 --- a/arch/x86/mm/pgtable.c +++ b/arch/x86/mm/pgtable.c @@ -884,3 +884,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 e24d2c992b11..71aa63e2b014 100644 --- a/include/linux/pgtable.h +++ b/include/linux/pgtable.h @@ -1484,6 +1484,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 ca54d36d203a..6be9ff4007ab 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1721,6 +1721,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; } @@ -3370,6 +3373,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