From patchwork Wed Jul 11 11:29:39 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joerg Roedel X-Patchwork-Id: 10519503 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 3C04B603D7 for ; Wed, 11 Jul 2018 11:31:58 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 410EB26861 for ; Wed, 11 Jul 2018 11:31:58 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3542C28578; Wed, 11 Jul 2018 11:31:58 +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 94CC126861 for ; Wed, 11 Jul 2018 11:31:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 151856B028B; Wed, 11 Jul 2018 07:30:20 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 023096B028F; Wed, 11 Jul 2018 07:30:19 -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 E62556B028E; Wed, 11 Jul 2018 07:30:19 -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 889136B028D for ; Wed, 11 Jul 2018 07:30:19 -0400 (EDT) Received: by mail-ed1-f69.google.com with SMTP id y17-v6so3397043eds.22 for ; Wed, 11 Jul 2018 04:30:19 -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=158qeaYC3foHyuEGtQUO/XqrIPxS8wnOl2SrE16O5dM=; b=Hj52B1+9DvHlxi5ugTkCaquLwczgYy63fuX597YxG1ku4NCD7AiBBSui1L0yRnjbsU ej+BclP9Y/Pm9v0ER2IiwZraIQ4xG7gTlGA98DQU4ByEKcajkbdWUTEz4BtQXoHW+IdU 4ccDwPGh97lN23XCpRj3Qdgou7Js2NSuXc2EMXOY5td7z1CIi8wUIPoi0fQZGb6Mw0hb VUcJPQxXp3jMCUkiPqK3d8bSFb4Hh4MetnS5RBQ1KId8Q3/aOUbdADFwLDFyR6wMbKep ObwgSchuFOyeROiYU34ufxRwonReuxlQr3iC9Qj4YO2NIHxEHWh8bLmNzKtzDtiy/ZGu AIQA== X-Gm-Message-State: APt69E3oXujIVFFSOVebUT3aAAeZuOyQ9CaPnyaJOrZ2tMR5R0+Lykbk EeP7S56zdfCsJQujJQzTJ+IAC+BX1WzGR+gsLCqJT0L/Npmd29S/FxpMXsEnCb+48NJhIc6KVUf cKfZQWynLzHyHjI7mR1PpglgETQpZEHD/ccUFN1waPlBgGvtKykukGo/OnZQ/mS6Grg== X-Received: by 2002:a50:a3ce:: with SMTP id t14-v6mr31052240edb.227.1531308619091; Wed, 11 Jul 2018 04:30:19 -0700 (PDT) X-Google-Smtp-Source: AAOMgpfv077qlY8rO1vFd+wrQZ/fc4nxXfH0yGiF7ZEMeuApCixwPcJpZRsBgWYwxGxrJdNmvdZf X-Received: by 2002:a50:a3ce:: with SMTP id t14-v6mr31052190edb.227.1531308618364; Wed, 11 Jul 2018 04:30:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531308618; cv=none; d=google.com; s=arc-20160816; b=gQCem5GwjtjDTYjkgbDglqsDh9+ot7zfRjgxjwAcDZWAWxuXUN/c0b38opGnOWi4bZ OWrAfHMZmMWHneFegHDLouZoiLChjCVAht6IdaRfNBNxyixjm4i6rveVq6lNh/5wiPWG bEBRiAoNxHIHODM0rKEB8D+U6bMxWRv8rbmXdZRF3o6RVpyUxW2swRspSn00hE2fspjH OTd54TZBQqwx5oEDuL4PM5kRZ9qHG15IlhTn8zrgjKBhWqM/TLjemXJwR8NEjk9xrFLo 6TY52aCrQazr3T4LbG0TlM9fofvjg3k4hGWdbH22/XFea8tB4uxP5ZD0mh86G84zY6qI zkig== 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=158qeaYC3foHyuEGtQUO/XqrIPxS8wnOl2SrE16O5dM=; b=W5p779kH5zoom6FZ9tkM9P1aB5JCebD5iI0ZuQDCCrSN5g5+PeKGzpI0oypp8c/4xl 9Qz3kpLgUNhMEn5uHRtcATT75cFD9/9rOzsb3OdRlIXxofqbAJz/6kbPSpVMXaJx1g0z 7A6WnzLByYl7AKGmcTj3ovUg1DBiivjpXCzq3wI8cgr9sQQbijCwD5b8f85kcY18tgLd peGG164AqjhIi6tgfln2Er7gHy/mrBSTcJlzUVZS0ANLyABUc4JO0oyzfEazA2rpL662 39ndD85X+ksM9BErIXzT9piXt8pJI9JUe5o5rSPRheXtBJcoL8eqPlfKNTvJkLN81qDx o4ug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass (test mode) header.i=@8bytes.org header.s=mail-1 header.b=QIxvE8km; spf=pass (google.com: domain of joro@8bytes.org designates 81.169.241.247 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. [81.169.241.247]) by mx.google.com with ESMTPS id r10-v6si3957586edb.225.2018.07.11.04.30.18 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 11 Jul 2018 04:30:18 -0700 (PDT) Received-SPF: pass (google.com: domain of joro@8bytes.org designates 81.169.241.247 as permitted sender) client-ip=81.169.241.247; Authentication-Results: mx.google.com; dkim=pass (test mode) header.i=@8bytes.org header.s=mail-1 header.b=QIxvE8km; spf=pass (google.com: domain of joro@8bytes.org designates 81.169.241.247 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 DD90C306E; Wed, 11 Jul 2018 13:30:06 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=8bytes.org; s=mail-1; t=1531308606; bh=Qt4WJIUNKjNS+KFm8L8zxdc5EMEUEsXHmc/2ItzWZCQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QIxvE8kmIwZKahpz8tDIy/ClcqV4f9rI78Cicbh6481JSvkaqmQ0IxSi3zyikHUju GiyDlOmDn05mwCkfGFl2eGTYAQTMNxC7Mdxn7ETGRMCsAdnYl8ibst1VSj4Wu1a2pH fBB2/5Wgkp/J+2rWbISTpMG/u9pGVWM7KnWhvQUbbD58kKFUM77QtDEPphU49xBzf4 x0AJtA+aqfbYDpDg/Y8fpOolcEF32LkgxaQvQ0b7KOHnRwpZ7H30w6MGKMkxfjtQTE BVJV9+Uqoj74UbEqegZQlU0qIMQ0J0KLhl8fPF02rOW7fBdOipRK0gp8Xe8DPZncUV Z59uSrQUyeGng== 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 32/39] x86/pgtable/pae: Use separate kernel PMDs for user page-table Date: Wed, 11 Jul 2018 13:29:39 +0200 Message-Id: <1531308586-29340-33-git-send-email-joro@8bytes.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1531308586-29340-1-git-send-email-joro@8bytes.org> References: <1531308586-29340-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 We need separate kernel PMDs in the user page-table when PTI is enabled to map the per-process LDT for user-space. Signed-off-by: Joerg Roedel --- arch/x86/mm/pgtable.c | 100 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 81 insertions(+), 19 deletions(-) diff --git a/arch/x86/mm/pgtable.c b/arch/x86/mm/pgtable.c index db6fb77..8e4e63d 100644 --- a/arch/x86/mm/pgtable.c +++ b/arch/x86/mm/pgtable.c @@ -182,6 +182,14 @@ static void pgd_dtor(pgd_t *pgd) */ #define PREALLOCATED_PMDS UNSHARED_PTRS_PER_PGD +/* + * We allocate separate PMDs for the kernel part of the user page-table + * when PTI is enabled. We need them to map the per-process LDT into the + * user-space page-table. + */ +#define PREALLOCATED_USER_PMDS (static_cpu_has(X86_FEATURE_PTI) ? \ + KERNEL_PGD_PTRS : 0) + void pud_populate(struct mm_struct *mm, pud_t *pudp, pmd_t *pmd) { paravirt_alloc_pmd(mm, __pa(pmd) >> PAGE_SHIFT); @@ -202,14 +210,14 @@ void pud_populate(struct mm_struct *mm, pud_t *pudp, pmd_t *pmd) /* No need to prepopulate any pagetable entries in non-PAE modes. */ #define PREALLOCATED_PMDS 0 - +#define PREALLOCATED_USER_PMDS 0 #endif /* CONFIG_X86_PAE */ -static void free_pmds(struct mm_struct *mm, pmd_t *pmds[]) +static void free_pmds(struct mm_struct *mm, pmd_t *pmds[], int count) { int i; - for(i = 0; i < PREALLOCATED_PMDS; i++) + for (i = 0; i < count; i++) if (pmds[i]) { pgtable_pmd_page_dtor(virt_to_page(pmds[i])); free_page((unsigned long)pmds[i]); @@ -217,7 +225,7 @@ static void free_pmds(struct mm_struct *mm, pmd_t *pmds[]) } } -static int preallocate_pmds(struct mm_struct *mm, pmd_t *pmds[]) +static int preallocate_pmds(struct mm_struct *mm, pmd_t *pmds[], int count) { int i; bool failed = false; @@ -226,7 +234,7 @@ static int preallocate_pmds(struct mm_struct *mm, pmd_t *pmds[]) if (mm == &init_mm) gfp &= ~__GFP_ACCOUNT; - for(i = 0; i < PREALLOCATED_PMDS; i++) { + for (i = 0; i < count; i++) { pmd_t *pmd = (pmd_t *)__get_free_page(gfp); if (!pmd) failed = true; @@ -241,7 +249,7 @@ static int preallocate_pmds(struct mm_struct *mm, pmd_t *pmds[]) } if (failed) { - free_pmds(mm, pmds); + free_pmds(mm, pmds, count); return -ENOMEM; } @@ -254,23 +262,38 @@ static int preallocate_pmds(struct mm_struct *mm, pmd_t *pmds[]) * preallocate which never got a corresponding vma will need to be * freed manually. */ +static void mop_up_one_pmd(struct mm_struct *mm, pgd_t *pgdp) +{ + pgd_t pgd = *pgdp; + + if (pgd_val(pgd) != 0) { + pmd_t *pmd = (pmd_t *)pgd_page_vaddr(pgd); + + *pgdp = native_make_pgd(0); + + paravirt_release_pmd(pgd_val(pgd) >> PAGE_SHIFT); + pmd_free(mm, pmd); + mm_dec_nr_pmds(mm); + } +} + static void pgd_mop_up_pmds(struct mm_struct *mm, pgd_t *pgdp) { int i; - for(i = 0; i < PREALLOCATED_PMDS; i++) { - pgd_t pgd = pgdp[i]; + for (i = 0; i < PREALLOCATED_PMDS; i++) + mop_up_one_pmd(mm, &pgdp[i]); - if (pgd_val(pgd) != 0) { - pmd_t *pmd = (pmd_t *)pgd_page_vaddr(pgd); +#ifdef CONFIG_PAGE_TABLE_ISOLATION - pgdp[i] = native_make_pgd(0); + if (!static_cpu_has(X86_FEATURE_PTI)) + return; - paravirt_release_pmd(pgd_val(pgd) >> PAGE_SHIFT); - pmd_free(mm, pmd); - mm_dec_nr_pmds(mm); - } - } + pgdp = kernel_to_user_pgdp(pgdp); + + for (i = 0; i < PREALLOCATED_USER_PMDS; i++) + mop_up_one_pmd(mm, &pgdp[i + KERNEL_PGD_BOUNDARY]); +#endif } static void pgd_prepopulate_pmd(struct mm_struct *mm, pgd_t *pgd, pmd_t *pmds[]) @@ -296,6 +319,38 @@ static void pgd_prepopulate_pmd(struct mm_struct *mm, pgd_t *pgd, pmd_t *pmds[]) } } +#ifdef CONFIG_PAGE_TABLE_ISOLATION +static void pgd_prepopulate_user_pmd(struct mm_struct *mm, + pgd_t *k_pgd, pmd_t *pmds[]) +{ + pgd_t *s_pgd = kernel_to_user_pgdp(swapper_pg_dir); + pgd_t *u_pgd = kernel_to_user_pgdp(k_pgd); + p4d_t *u_p4d; + pud_t *u_pud; + int i; + + u_p4d = p4d_offset(u_pgd, 0); + u_pud = pud_offset(u_p4d, 0); + + s_pgd += KERNEL_PGD_BOUNDARY; + u_pud += KERNEL_PGD_BOUNDARY; + + for (i = 0; i < PREALLOCATED_USER_PMDS; i++, u_pud++, s_pgd++) { + pmd_t *pmd = pmds[i]; + + memcpy(pmd, (pmd_t *)pgd_page_vaddr(*s_pgd), + sizeof(pmd_t) * PTRS_PER_PMD); + + pud_populate(mm, u_pud, pmd); + } + +} +#else +static void pgd_prepopulate_user_pmd(struct mm_struct *mm, + pgd_t *k_pgd, pmd_t *pmds[]) +{ +} +#endif /* * Xen paravirt assumes pgd table should be in one page. 64 bit kernel also * assumes that pgd should be in one page. @@ -376,6 +431,7 @@ static inline void _pgd_free(pgd_t *pgd) pgd_t *pgd_alloc(struct mm_struct *mm) { pgd_t *pgd; + pmd_t *u_pmds[PREALLOCATED_USER_PMDS]; pmd_t *pmds[PREALLOCATED_PMDS]; pgd = _pgd_alloc(); @@ -385,12 +441,15 @@ pgd_t *pgd_alloc(struct mm_struct *mm) mm->pgd = pgd; - if (preallocate_pmds(mm, pmds) != 0) + if (preallocate_pmds(mm, pmds, PREALLOCATED_PMDS) != 0) goto out_free_pgd; - if (paravirt_pgd_alloc(mm) != 0) + if (preallocate_pmds(mm, u_pmds, PREALLOCATED_USER_PMDS) != 0) goto out_free_pmds; + if (paravirt_pgd_alloc(mm) != 0) + goto out_free_user_pmds; + /* * Make sure that pre-populating the pmds is atomic with * respect to anything walking the pgd_list, so that they @@ -400,13 +459,16 @@ pgd_t *pgd_alloc(struct mm_struct *mm) pgd_ctor(mm, pgd); pgd_prepopulate_pmd(mm, pgd, pmds); + pgd_prepopulate_user_pmd(mm, pgd, u_pmds); spin_unlock(&pgd_lock); return pgd; +out_free_user_pmds: + free_pmds(mm, u_pmds, PREALLOCATED_USER_PMDS); out_free_pmds: - free_pmds(mm, pmds); + free_pmds(mm, pmds, PREALLOCATED_PMDS); out_free_pgd: _pgd_free(pgd); out: