Received: by 10.192.165.156 with SMTP id m28csp771880imm; Mon, 16 Apr 2018 08:28:39 -0700 (PDT) X-Google-Smtp-Source: AIpwx4/fZBI2Fr5MnVrvAzZmLwY+t/GIYISi8a60dJ9dM5ENAl7uEy0NWPIVa5iXY2JOAcQpgM8T X-Received: by 10.98.64.91 with SMTP id n88mr8265262pfa.229.1523892519430; Mon, 16 Apr 2018 08:28:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523892519; cv=none; d=google.com; s=arc-20160816; b=LZS11gKvJKQsxFrYJIaLphFyJTaqKYm+6hizwweNLdKYgGm7LtqsyeYyn+AUCNNIYT J3ev36E+GmEBtxts8FWVwig+nYi+6WJsohEt8bw/RmmZU2bpHe3nGy5b0t+nS61F1uZn etdeyfasLTG/pLCTcCZYWqU2CvSHsimkDmq92i5UFtfXxijq795A+gTDdTX5LHURn1cG 8JRC1i9NjEHyU+axix1mm+EM+T5OrUkxqK3eBf88rWTJN6JLxYJAIUVSxHJTsIl8M2C4 kmi9WNKK5HjJARwUmhzk11zjnQWTeA1AuygrvMRtI3TBn+fxJZRSMZCpqCkD0rSrCCV5 mTOg== 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=Eu8g7yGMPODrGQc8IRd2GX35hksDZB3r7cjxC2p1ySo=; b=zulKM0LfFxymCg2wetGavUNh5ivFeN7aXofLpFUwrqZsQ60hYOJdIKjsXuvQC604ZR LZfSNpzJesF2YBKex9Vvmk6e1newP1U6ngo16WyHiHG3DVMqKVwbTG57Y7squfnJaykL 1UNyVjEhlvpuaQ9VwBqlnJ8Pz1BNiIgtLDegMNCMsLs4IzHpK/1vyRJaSuKi1Gh41PJZ XNX0dO8abKb/jcQ0f4tYkSz5IlvLCskMtLs86m46DfyoCBICQmFzRd3ZaGmeRl0UTroK bwE3RD5MWY0BjqVc5cwBnr2XH23FsocYgz41rqxhXmcPo1FyBS5A1fqRLr1U9RmbSHnW zozA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail (test mode) header.i=@8bytes.org header.s=mail-1 header.b=h/fcHduX; 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 t5si9516576pgo.96.2018.04.16.08.28.25; Mon, 16 Apr 2018 08:28:39 -0700 (PDT) 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=h/fcHduX; 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 S1753150AbeDPP0s (ORCPT + 99 others); Mon, 16 Apr 2018 11:26:48 -0400 Received: from 8bytes.org ([81.169.241.247]:36084 "EHLO theia.8bytes.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753021AbeDPPZy (ORCPT ); Mon, 16 Apr 2018 11:25:54 -0400 Received: by theia.8bytes.org (Postfix, from userid 1000) id 6773AB4A; Mon, 16 Apr 2018 17:25:41 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=8bytes.org; s=mail-1; t=1523892342; bh=P6vWUbTas/xLc5a32ltauEQ/5ROjuUJvY1B0n6gLoHg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=h/fcHduXGu/F1mLL78a+W5GPZq4265BQcuqvu3QmPqTsnATc8ofe4O/PWHtc0sFZ9 SLb/ka36nhokdmmdcPL4najW8WQJurLWg/uI7WquIOG5FjD0u0l1IXjcn6R53VxOWg gjngWbfG74vBdj0utJ/N0QY7Tw46dLtovvbQxKSf58iYQcybMcPqPZ+vMMqevZjz7j Cy0HF5GSaaootJNmNg21XGPVytZn7M0bYX3CfMyzYavnfDmsZ/Hm+BClUMHBU+2o+U h0c91tx/KoFV0Xlhhp/PMfNldhvNIp3Tl3d4KLI4BmHmtrtaup8M4gM2dYurpxZzRi 1yJCyzlqD6XAQ== 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 , "David H . Gutteridge" , jroedel@suse.de, joro@8bytes.org Subject: [PATCH 31/35] x86/ldt: Split out sanity check in map_ldt_struct() Date: Mon, 16 Apr 2018 17:25:19 +0200 Message-Id: <1523892323-14741-32-git-send-email-joro@8bytes.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1523892323-14741-1-git-send-email-joro@8bytes.org> References: <1523892323-14741-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 46c349c..e68ce37 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); @@ -172,35 +216,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