Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp2259641imm; Thu, 19 Jul 2018 16:39:53 -0700 (PDT) X-Google-Smtp-Source: AAOMgpe0J+2cj4k8/yfnFVZL1D0XMYkK0wA5cRDt3kUDPFT8IwlNym6UJRza4hQl4oOXg8HDGwPq X-Received: by 2002:a65:5cc5:: with SMTP id b5-v6mr11665453pgt.425.1532043593535; Thu, 19 Jul 2018 16:39:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1532043593; cv=none; d=google.com; s=arc-20160816; b=gUtSkIX3o/BrfL3N7pjuHsJ2AaJA8v3UNe0Gaa/YbiFEofSZomtn5xVBhj9t2AZGcU Zs11Dqmnn2SwgftC+7UJZlGBN9Fc1/LDCDxX2LL5Mj5dl7bGD+5KemBVSSI3kacGEELD jfy16ubs7ZJpX2osVfmu9F8vxJKdt6GA6CrBiESzhEEVXkyYh6VEhYX8QlKaF4SDNGJM qok3XkThPVQoAEuIp9f1/vkX9+0DswnetS4OJhC2aGWeVc/QiJtrzf9FmJNL2UNS+qYl 7QzEhfoNm81MNcuCtgjA8zhf42yT/mgoMsagDGdc3pPveWm9ORkQmVpwZJuQJxuISUh0 1s+w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-disposition :content-transfer-encoding:mime-version:robot-unsubscribe:robot-id :git-commit-id:subject:to:references:in-reply-to:reply-to:cc :message-id:from:date:arc-authentication-results; bh=rT/blrcjBtfSemWkthv9p1o0RzMPwqKtKtW9ysn0tis=; b=hC2DSt7BMQtbOMzXuEuYiSz2K8oUGo/662jlsiGnMOBaZGqdWACc9CnOCmvolBqhlf wTZPC9WKHSvZ0/5cU9UY2jw+FWsON48Ix0SJQujQ+aN7RtJaX75OpMZgVkFb6Nt2DhZu v2tigS1vFo+1SQtVemCvyqTKakIf97ToO5VaF3RmP+cXFqqfKAId4eXec+i72bdQc4ky Ib+OKSJnrS9j2f0SCXD7fqTSfI7GDD5hRV6ncvVt8prQOxCVUgu0mUllMdnW4NnfAX2d Lvw5ODoCGRgBEcZtDorLv16cQa8qLlvxfYWoQyCmG3bTk79/siouYgK5jJV47TptDBOw FFNg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o188-v6si397438pfo.236.2018.07.19.16.39.39; Thu, 19 Jul 2018 16:39:53 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731244AbeGTAXO (ORCPT + 99 others); Thu, 19 Jul 2018 20:23:14 -0400 Received: from terminus.zytor.com ([198.137.202.136]:37381 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730505AbeGTAXO (ORCPT ); Thu, 19 Jul 2018 20:23:14 -0400 Received: from terminus.zytor.com (localhost [127.0.0.1]) by terminus.zytor.com (8.15.2/8.15.2) with ESMTPS id w6JNampL2454507 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Thu, 19 Jul 2018 16:36:48 -0700 Received: (from tipbot@localhost) by terminus.zytor.com (8.15.2/8.15.2/Submit) id w6JNam8H2454504; Thu, 19 Jul 2018 16:36:48 -0700 Date: Thu, 19 Jul 2018 16:36:48 -0700 X-Authentication-Warning: terminus.zytor.com: tipbot set sender to tipbot@zytor.com using -f From: tip-bot for Joerg Roedel Message-ID: Cc: jpoimboe@redhat.com, dave.hansen@intel.com, dvlasenk@redhat.com, peterz@infradead.org, torvalds@linux-foundation.org, brgerst@gmail.com, llong@redhat.com, dhgutteridge@sympatico.ca, jgross@suse.com, pavel@ucw.cz, boris.ostrovsky@oracle.com, gregkh@linuxfoundation.org, tglx@linutronix.de, hpa@zytor.com, aarcange@redhat.com, eduval@amazon.com, bp@alien8.de, jroedel@suse.de, mingo@kernel.org, jkosina@suse.cz, will.deacon@arm.com, David.Laight@aculab.com, linux-kernel@vger.kernel.org, luto@kernel.org Reply-To: David.Laight@aculab.com, will.deacon@arm.com, luto@kernel.org, linux-kernel@vger.kernel.org, eduval@amazon.com, jkosina@suse.cz, mingo@kernel.org, bp@alien8.de, jroedel@suse.de, hpa@zytor.com, aarcange@redhat.com, gregkh@linuxfoundation.org, tglx@linutronix.de, boris.ostrovsky@oracle.com, dhgutteridge@sympatico.ca, llong@redhat.com, brgerst@gmail.com, pavel@ucw.cz, jgross@suse.com, torvalds@linux-foundation.org, dvlasenk@redhat.com, peterz@infradead.org, dave.hansen@intel.com, jpoimboe@redhat.com In-Reply-To: <1531906876-13451-36-git-send-email-joro@8bytes.org> References: <1531906876-13451-36-git-send-email-joro@8bytes.org> To: linux-tip-commits@vger.kernel.org Subject: [tip:x86/pti] x86/ldt: Split out sanity check in map_ldt_struct() Git-Commit-ID: 9bae3197e15dd5e03ce8e237db6fe4486b08a775 X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline X-Spam-Status: No, score=-2.9 required=5.0 tests=ALL_TRUSTED,BAYES_00, DATE_IN_FUTURE_96_Q autolearn=ham autolearn_force=no version=3.4.1 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on terminus.zytor.com Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit-ID: 9bae3197e15dd5e03ce8e237db6fe4486b08a775 Gitweb: https://git.kernel.org/tip/9bae3197e15dd5e03ce8e237db6fe4486b08a775 Author: Joerg Roedel AuthorDate: Wed, 18 Jul 2018 11:41:12 +0200 Committer: Thomas Gleixner CommitDate: Fri, 20 Jul 2018 01:11:47 +0200 x86/ldt: Split out sanity check in map_ldt_struct() 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 Signed-off-by: Thomas Gleixner Tested-by: Pavel Machek Cc: "H . Peter Anvin" Cc: linux-mm@kvack.org Cc: Linus Torvalds Cc: Andy Lutomirski Cc: Dave Hansen Cc: Josh Poimboeuf Cc: Juergen Gross Cc: Peter Zijlstra Cc: Borislav Petkov Cc: Jiri Kosina Cc: Boris Ostrovsky Cc: Brian Gerst Cc: David Laight Cc: Denys Vlasenko Cc: Eduardo Valentin Cc: Greg KH Cc: Will Deacon Cc: aliguori@amazon.com Cc: daniel.gruss@iaik.tugraz.at Cc: hughd@google.com Cc: keescook@google.com Cc: Andrea Arcangeli Cc: Waiman Long Cc: "David H . Gutteridge" Cc: joro@8bytes.org Link: https://lkml.kernel.org/r/1531906876-13451-36-git-send-email-joro@8bytes.org --- 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 e921b3d494d5..69af9a0d57b7 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