Received: by 10.223.185.116 with SMTP id b49csp2469976wrg; Mon, 5 Mar 2018 03:32:21 -0800 (PST) X-Google-Smtp-Source: AG47ELsjzbUQLNf7kgvYW6fgfjL7LLmfoAB2jrSaGiyo0q88DaMnzg8zPVb72zJ2i/7re57cSqzy X-Received: by 10.98.159.209 with SMTP id v78mr14878442pfk.49.1520249541828; Mon, 05 Mar 2018 03:32:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520249541; cv=none; d=google.com; s=arc-20160816; b=UYS2DLLamvbsgRZi6jISP+eWcfH8wrXY4C8S5afR0xh3sVjjzQ89Yeka8kn3pc3LAG Ko8Y6/DaGhV3Hrfpo38sJS3piMLjKJuQsMc1Ct8yn9Hy1iQH1vata8131HQEYworhpjd 8eCuzuCuI3vMLeDqDbfROWBkEYmKClfH0tBubo8ic/y4w8UTOV7I87CCNltUUSYo/573 sU2ylLOcNdx9vZEwy+LlDMtqoq1KdJR9/+HpAoKePUav8Qj664Ft9+LtbuuvifN/AV3L W2owNOWEeY/5lhXHwFJr/ChwfNuOEfXpWyk5efPTAkWk7l1/CVLw4js4hxIsriQiQpE1 KjSg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=igYQ/x+Bee3t5LU4V9asteXT8ZgIi1BJyc7+25PZsZk=; b=YLI/9xQViDEw6j7YdDuWIqa/jFcDnZ8lr8d7V/symUBLBICP1qwJB1pop9fQ5dbmUZ VsZKEcUPw+GuffbVtRfiPFWwtMsso/E75nOJ4IN62y/wZgIwltDPNVbD89WAOY6nRV63 T+Vk8tHtNJSFnvd5ncLAtHP15QUQMBxL1u8K1oJrWf2eiaoBgpnAWslsCm3UVn6EJIKs 9/p4tsQB8TKBMdpM94fLqg7+r23chc85Vx8QFx1Zomk+Rtbht3/I7KtcrpqiGqlDJCoS qkKEVwY9DLFrEB0y10DJjmRNwu/QNXRUgG8pKLnik6q1EORFux4c+JYKSByqVkcer6R5 18Ug== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail (test mode) header.i=@8bytes.org header.s=mail-1 header.b=WOD7gNaz; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=8bytes.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w11si8216743pgo.826.2018.03.05.03.32.07; Mon, 05 Mar 2018 03:32:21 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail (test mode) header.i=@8bytes.org header.s=mail-1 header.b=WOD7gNaz; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=8bytes.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933925AbeCEK1L (ORCPT + 99 others); Mon, 5 Mar 2018 05:27:11 -0500 Received: from 8bytes.org ([81.169.241.247]:54502 "EHLO theia.8bytes.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933851AbeCEK00 (ORCPT ); Mon, 5 Mar 2018 05:26:26 -0500 Received: by theia.8bytes.org (Postfix, from userid 1000) id 71BED4255; Mon, 5 Mar 2018 11:26:09 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=8bytes.org; s=mail-1; t=1520245571; bh=PcrGto/IpJfqEyqwqIoGCxBXn1ZO4uVqmRmVmUz5A5c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WOD7gNaz1QWOJjhLHE0eLwtGU+bzNN71ygz9fTnzqVs79RprSinRVjVkgGjdY0XFK bhq+R0mao6FoBwKp5Lp59vq0zynzCKvzWCrcwUY66C7tmdB8d4J9jLfvOOZYFQlpRM SFFVleVmeWhx5A38LE2Vr5FJvYPbA8t4IqeQGA8Rdk3Sa+FZqhyl88GlNScRhKoWUN jzOP9CfbWE5MnDv+CeT2QWu7vLtGshJlwiTgnz9vnmUcNIzS8b5Umq4hE+DXzphcdi QSXwbB/n38cIIOdzkw9Q3LyQ+xMZcyPLdyoucoZ5QlfH5iPUeMBs3JyPITs1lJed/3 znnJsyfUpXRwQ== From: Joerg Roedel To: Thomas Gleixner , Ingo Molnar , "H . Peter Anvin" Cc: x86@kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Linus Torvalds , Andy Lutomirski , Dave Hansen , Josh Poimboeuf , Juergen Gross , Peter Zijlstra , Borislav Petkov , Jiri Kosina , Boris Ostrovsky , Brian Gerst , David Laight , Denys Vlasenko , Eduardo Valentin , Greg KH , Will Deacon , aliguori@amazon.com, daniel.gruss@iaik.tugraz.at, hughd@google.com, keescook@google.com, Andrea Arcangeli , Waiman Long , Pavel Machek , jroedel@suse.de, joro@8bytes.org Subject: [PATCH 31/34] x86/ldt: Split out sanity check in map_ldt_struct() Date: Mon, 5 Mar 2018 11:26:00 +0100 Message-Id: <1520245563-8444-32-git-send-email-joro@8bytes.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1520245563-8444-1-git-send-email-joro@8bytes.org> References: <1520245563-8444-1-git-send-email-joro@8bytes.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Joerg Roedel This splits out the mapping sanity check and the actual mapping of the LDT to user-space from the map_ldt_struct() function in a way so that it is re-usable for PAE paging. Signed-off-by: Joerg Roedel --- arch/x86/kernel/ldt.c | 82 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 58 insertions(+), 24 deletions(-) diff --git a/arch/x86/kernel/ldt.c b/arch/x86/kernel/ldt.c index f3c2fbf..8ab7df9 100644 --- a/arch/x86/kernel/ldt.c +++ b/arch/x86/kernel/ldt.c @@ -100,6 +100,49 @@ static struct ldt_struct *alloc_ldt_struct(unsigned int num_entries) return new_ldt; } +#ifdef CONFIG_PAGE_TABLE_ISOLATION + +static void do_sanity_check(struct mm_struct *mm, + bool had_kernel_mapping, + bool had_user_mapping) +{ + if (mm->context.ldt) { + /* + * We already had an LDT. The top-level entry should already + * have been allocated and synchronized with the usermode + * tables. + */ + WARN_ON(!had_kernel_mapping); + if (static_cpu_has(X86_FEATURE_PTI)) + WARN_ON(!had_user_mapping); + } else { + /* + * This is the first time we're mapping an LDT for this process. + * Sync the pgd to the usermode tables. + */ + WARN_ON(had_kernel_mapping); + if (static_cpu_has(X86_FEATURE_PTI)) + WARN_ON(had_user_mapping); + } +} + +static void map_ldt_struct_to_user(struct mm_struct *mm) +{ + pgd_t *pgd = pgd_offset(mm, LDT_BASE_ADDR); + + if (static_cpu_has(X86_FEATURE_PTI) && !mm->context.ldt) + set_pgd(kernel_to_user_pgdp(pgd), *pgd); +} + +static void sanity_check_ldt_mapping(struct mm_struct *mm) +{ + pgd_t *pgd = pgd_offset(mm, LDT_BASE_ADDR); + bool had_kernel = (pgd->pgd != 0); + bool had_user = (kernel_to_user_pgdp(pgd)->pgd != 0); + + do_sanity_check(mm, had_kernel, had_user); +} + /* * If PTI is enabled, this maps the LDT into the kernelmode and * usermode tables for the given mm. @@ -115,9 +158,8 @@ static struct ldt_struct *alloc_ldt_struct(unsigned int num_entries) static int map_ldt_struct(struct mm_struct *mm, struct ldt_struct *ldt, int slot) { -#ifdef CONFIG_PAGE_TABLE_ISOLATION - bool is_vmalloc, had_top_level_entry; unsigned long va; + bool is_vmalloc; spinlock_t *ptl; pgd_t *pgd; int i; @@ -131,13 +173,15 @@ map_ldt_struct(struct mm_struct *mm, struct ldt_struct *ldt, int slot) */ WARN_ON(ldt->slot != -1); + /* Check if the current mappings are sane */ + sanity_check_ldt_mapping(mm); + /* * Did we already have the top level entry allocated? We can't * use pgd_none() for this because it doens't do anything on * 4-level page table kernels. */ pgd = pgd_offset(mm, LDT_BASE_ADDR); - had_top_level_entry = (pgd->pgd != 0); is_vmalloc = is_vmalloc_addr(ldt->entries); @@ -168,35 +212,25 @@ map_ldt_struct(struct mm_struct *mm, struct ldt_struct *ldt, int slot) pte_unmap_unlock(ptep, ptl); } - if (mm->context.ldt) { - /* - * We already had an LDT. The top-level entry should already - * have been allocated and synchronized with the usermode - * tables. - */ - WARN_ON(!had_top_level_entry); - if (static_cpu_has(X86_FEATURE_PTI)) - WARN_ON(!kernel_to_user_pgdp(pgd)->pgd); - } else { - /* - * This is the first time we're mapping an LDT for this process. - * Sync the pgd to the usermode tables. - */ - WARN_ON(had_top_level_entry); - if (static_cpu_has(X86_FEATURE_PTI)) { - WARN_ON(kernel_to_user_pgdp(pgd)->pgd); - set_pgd(kernel_to_user_pgdp(pgd), *pgd); - } - } + /* Propagate LDT mapping to the user page-table */ + map_ldt_struct_to_user(mm); va = (unsigned long)ldt_slot_va(slot); flush_tlb_mm_range(mm, va, va + LDT_SLOT_STRIDE, 0); ldt->slot = slot; -#endif return 0; } +#else /* !CONFIG_PAGE_TABLE_ISOLATION */ + +static int +map_ldt_struct(struct mm_struct *mm, struct ldt_struct *ldt, int slot) +{ + return 0; +} +#endif /* CONFIG_PAGE_TABLE_ISOLATION */ + static void free_ldt_pgtables(struct mm_struct *mm) { #ifdef CONFIG_PAGE_TABLE_ISOLATION -- 2.7.4