From patchwork Wed Jul 18 09:41:12 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joerg Roedel X-Patchwork-Id: 10531903 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 2DF14602CA for ; Wed, 18 Jul 2018 09:43:17 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1E04528FBC for ; Wed, 18 Jul 2018 09:43:17 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 11F4228FCD; Wed, 18 Jul 2018 09:43:17 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 78A2428FBC for ; Wed, 18 Jul 2018 09:43:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 83B5D6B02A4; Wed, 18 Jul 2018 05:41:41 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 7A2FF6B02AB; Wed, 18 Jul 2018 05:41:41 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 63D676B02AC; Wed, 18 Jul 2018 05:41:41 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) by kanga.kvack.org (Postfix) with ESMTP id 014636B02A4 for ; Wed, 18 Jul 2018 05:41:41 -0400 (EDT) Received: by mail-ed1-f69.google.com with SMTP id l1-v6so1684739edi.11 for ; Wed, 18 Jul 2018 02:41:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:cc:subject:date :message-id:in-reply-to:references; bh=1tK4DJkM6IMYIVPaJ98OlSv3S+ezDSjfJPKXAzM6UFQ=; b=T4df655yDoG26gpZEKduV0mr60S0LErVAma/DaVPaNL7NtO3P9sj9gyfmZz+S8gruX tLvmSHHLZSJ8NxqSHfBw9Eu5PveRiHcFjJY96gguzwQRY2QkdHyy5hjwtv+X5DxBkyhV p8J1f6H3QS2hqda420jKYQLnUv7C0gyKQ3ReNokc8Cch2yW0+ACAsPDDUyHZYYvG6sU/ m480dx7sdWRY5Fz5h1cLPqbC+Mo5ZXk9DQCo64s38LCsKeEYYqrvewiV8pxyy5+wHbd7 1LcftkggSaiY8t6SmlE77InDlKVwwP3lRHAgGWMtHZ2OpfrdxdkudXP3WC0BgjEw+B1A RZIA== X-Gm-Message-State: AOUpUlEn3nARYkHe3Xr972PfImV35D4liTiOci0agVfbCqbnT/PYifWn ZpCAcUlDlMrKhWWynXIJxEHsHRq6UbpMavnc0yNFEe84VxfNlFywK8hMg7Biewd5bfe/XEVKwRW Yn4l1NqkwIECZqizjSn2t32sO0Xh4XIm7RxBSPJmEKbdyu9+NBXSRzdlCWf3JyO11yQ== X-Received: by 2002:a50:81c6:: with SMTP id 64-v6mr6488939ede.89.1531906900556; Wed, 18 Jul 2018 02:41:40 -0700 (PDT) X-Google-Smtp-Source: AAOMgpcg/yYBpM58flc0lmeIc8sgNNRU5W9rKHhK9TQjpeoT7+pBbmc6b/P84Ts2JdEr+h29nVJa X-Received: by 2002:a50:81c6:: with SMTP id 64-v6mr6488898ede.89.1531906899861; Wed, 18 Jul 2018 02:41:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531906899; cv=none; d=google.com; s=arc-20160816; b=uoqE2jk/dUIq37+zl5/kT5h7fE4Ty2230cJe/l9i/zTI+1fn8b/JxdpVCqMjUcjJpm HWotUeQFojs0LzoNvFCHG9TljH2amYC6lp4BezRVCbykLv46za7wiO5lsg9RJzAvyho+ vBJNH4PppuZYXwyUMxUV43HEiHAeXITliywx6n45QO8KCb/xzT/5FIzIviZL3+xoh6eq drGOGqeIyPWNAU7qZQ2QqXNKrHXeQAsmkQCBEXUDd7JQywHz1QmlNhV+rgrPjUqAZCih HCqQfqHUC+yEha58C9NLhdYOZAJd4UyWMjcHN6NkVJXCGA3bslTONFEZg+XLkN73zjU3 obrQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=1tK4DJkM6IMYIVPaJ98OlSv3S+ezDSjfJPKXAzM6UFQ=; b=vBtY0eQf9+KaMbfjfWm2P1i2yBTMibugSTFo9s0eSzlfxUscLJMaULsKm9uU3aRuLl mhx0MGUrmvryKf2PTA3wq99bop5Gh/0wRnvHqKsLnP+OOgXWcTUGr7DL+g9rDnd9q4at 0gOgcRWfwc6cypbc6nJpCtdN+N9FFgn+hzAp/5fmB72Oi04hUakw1feu4vsBJkh2vl0U 7BCKCVNLBsw6yNNWkZVyB5tA9ME6lkv8bcHMfwe3ONG0JcrKBhzqhbTU/FsuJxYAQQR+ YgeOUSKlWomKdl6Vy/LVbOn2b2xkN4KN0szwYUxEpQSIfg7DqMCUDzIOtXl3sv4CvOGK PFvw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass (test mode) header.i=@8bytes.org header.s=mail-1 header.b=c4Y76sO3; spf=pass (google.com: domain of joro@8bytes.org designates 2a01:238:4383:600:38bc:a715:4b6d:a889 as permitted sender) smtp.mailfrom=joro@8bytes.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=8bytes.org Received: from theia.8bytes.org (8bytes.org. [2a01:238:4383:600:38bc:a715:4b6d:a889]) by mx.google.com with ESMTPS id y39-v6si2316188edb.120.2018.07.18.02.41.39 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 18 Jul 2018 02:41:39 -0700 (PDT) Received-SPF: pass (google.com: domain of joro@8bytes.org designates 2a01:238:4383:600:38bc:a715:4b6d:a889 as permitted sender) client-ip=2a01:238:4383:600:38bc:a715:4b6d:a889; Authentication-Results: mx.google.com; dkim=pass (test mode) header.i=@8bytes.org header.s=mail-1 header.b=c4Y76sO3; spf=pass (google.com: domain of joro@8bytes.org designates 2a01:238:4383:600:38bc:a715:4b6d:a889 as permitted sender) smtp.mailfrom=joro@8bytes.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=8bytes.org Received: by theia.8bytes.org (Postfix, from userid 1000) id 5C0B330B6; Wed, 18 Jul 2018 11:41:24 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=8bytes.org; s=mail-1; t=1531906886; bh=I9Q73gZnfm6UrdNLFwZi1LUuugpS4MvdTAw5t1OmAUA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=c4Y76sO3BsNh/iOHvGSQfJDAEN8E8w2sGixDu8LYv0NE0mrRmbgSmQD1Qmy882Rnj lfprMK5imUa8Jrjg2zPKR0c2X5JIwULFP750EQsgzJtoeLVFhS6g9q3ZE0lO9rNh3+ +nnSo1HbjefKyznOoHRKr3tBGIwbkA2AbDPkd2bfV9Pn0Ok1NaIbOZMiBM5n5bZkVW S0sNNNAQsP2n1oEXJx/M6yRC+IvznWw7bhZaQl2U6B1DiJHQN1ZzVz9g/QsEWUOfff 20STCbBQgoRp1kthQXtQYn8PF3j7mgkKRoy+dXCIiiQDTUfN6UrbviQrSKesa+XwX/ nPp/tn6K0f4dg== 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 35/39] x86/ldt: Split out sanity check in map_ldt_struct() Date: Wed, 18 Jul 2018 11:41:12 +0200 Message-Id: <1531906876-13451-36-git-send-email-joro@8bytes.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1531906876-13451-1-git-send-email-joro@8bytes.org> References: <1531906876-13451-1-git-send-email-joro@8bytes.org> X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP 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 e921b3d..69af9a0 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