From patchwork Wed Jul 18 09:41:09 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joerg Roedel X-Patchwork-Id: 10531909 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 EF5C7602CA for ; Wed, 18 Jul 2018 09:43:24 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DCBED28FBC for ; Wed, 18 Jul 2018 09:43:24 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CF2D028FCD; Wed, 18 Jul 2018 09:43:24 +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 44A9728FBC for ; Wed, 18 Jul 2018 09:43:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A37FD6B02AC; Wed, 18 Jul 2018 05:41:42 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id A0E596B02AE; Wed, 18 Jul 2018 05:41:42 -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 8D6766B02AF; Wed, 18 Jul 2018 05:41:42 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) by kanga.kvack.org (Postfix) with ESMTP id 263A66B02AC for ; Wed, 18 Jul 2018 05:41:42 -0400 (EDT) Received: by mail-ed1-f72.google.com with SMTP id p5-v6so1699213edh.16 for ; Wed, 18 Jul 2018 02:41:42 -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=B/vIU7i3MGlauJhRvJdDSJKx5QLoSvSMUfi+J8ImOFzYRhxpVa53gceq2Cx/SBamGW kximPxoRiijHLEYldTWrSLfOFm+ZW+CkmCCJaqEs4cZYy2oGyqI0PXfuLNUiSoHhqbxm oP5ymsqA3dWBF7Wgit5Rcq7NIBo2korTe2M9Mc9lpqLWpqtOCWZFtqJ0bgZyIip9Cd5P kWeyuw20w4bdPIXjDRXG/CbxSlLqNHf91kGSJoPYouUqmPnK+5cFfgITnTZDyoYDa6DM rKU4PnaNzEOZxJbLdkCqf9JXYOssgLjAdrlOLg/pXNO56PPlgCS153jot6WO9ZQoYjHZ uQBg== X-Gm-Message-State: AOUpUlEMbTRC+Ng61FZEBsA4rVVEbiaOjQWJiyU0sM3ReY9fAjATGgvF ue4pwQ9p0pJKepbfzAsEZF/lR9RQxyBTDETgdquDFMFFQqyKBPv3IIRHEuIuLFp2NmOQjdKKnPQ 6+S6z1MHzefLET4dE5K607Yr2WsivsYsXaTwSVE7mv2ia+VZvcNczML1gxt7Hz4NpVA== X-Received: by 2002:aa7:c803:: with SMTP id a3-v6mr6316802edt.124.1531906901719; Wed, 18 Jul 2018 02:41:41 -0700 (PDT) X-Google-Smtp-Source: AAOMgpdPIDi1AMCfpKul+rDSxDeKm4PqYA6kzqdl85n1rykjW0altSmxmg4GQXg3DYz8aj5pzGm2 X-Received: by 2002:aa7:c803:: with SMTP id a3-v6mr6316757edt.124.1531906901034; Wed, 18 Jul 2018 02:41:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531906901; cv=none; d=google.com; s=arc-20160816; b=0OH1ph/6iDfe00jMiXOtyEh1qa4zYf5JCYGH3tIocWnV/ESCVTkqgRg2ntJHk6hAAi nQWobPGF/3UOrx2+IgAXucSwuWFPGhK2jgFkTyy4wxbsecian8QRAnlO4Rhs8g/9YIoe zjbLXVti57LkRDeiNqQQGrHL1CvZWixsjsvIdrwHfymvpI/jDGkedY78Ao/wVu/LW4KZ mi/W9yBhPwpWWQduXyy5eJ0Zv6Sv8iNB8q4lwrRn3NdkRf/LM4ahRCEa3yScMB009Tmj kquisCihcX3r4kFWyKkhH94KUxh5Pie9VQtDKKJMpIj87UBN//6SdsjFhES7YIKxtx5l NLcw== 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=KsTejpifxRycmaJic9z7ikyJHsbnIER6hTZSFLU5t3YzyUu+vCTjzEoprGimdIBwcM CypiSfponrwQaAZ0QJEzbzOfxsIudO4OvdsLwCfzr6wxwwVVXmhwMYFbtqPHoDxQ+y0W pBGHQsD7ORA6BSmnR0nHFxLxj4tl+Y371g6E5eSAeZDUUeHnMLoCS6gQqgvpAzDWW4Uj pCbEW+DqalwSfSv0FV8rtoksgmNGn6YiFOJdZ+v9GP1bai7wItJr2EAJXXPpK+RE0/rA rO0d2hFQeKHYuUrqDDTLNybXsjj5aTPey01wKPbY6zg6mISActoDn2lg4OGd/tRpN1x+ 9f1w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass (test mode) header.i=@8bytes.org header.s=mail-1 header.b=MQeYqHm6; 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 s33-v6si2747474edm.148.2018.07.18.02.41.40 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 18 Jul 2018 02:41:41 -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=MQeYqHm6; 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 CCCB030A3; 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=Qt4WJIUNKjNS+KFm8L8zxdc5EMEUEsXHmc/2ItzWZCQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MQeYqHm6YtGx7nzeUBUiB6cEIv8PdHmmUFC5m06ryB1d8b3mP7OCRwCBEOI4HXsAj RkKoDFDW3FiZiV+6pPMZKOM6oCzIjZdZdueyFXzjoE9UX7SwDu/lrgUB2wA7Q4OT1B nTDc3F2hI73J6RQ8FHDf7BRj4uV8POZZW9i/Ol3R+lkA1PCmhm91DXewIHnyxZPlgV z3aVI/GDoir4v+nkl58fXo17J21spXFyPrQRSz1k8BBuIEUGZ9OWex7aczyhAJrC+Y IEwwRigo76Hjc46pJSco2QrdAoSZtqbQJWHSmVnZRATeXyXRdyR+skAdiTA+Y3cv5d YPO9w7AX2IHtg== 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, 18 Jul 2018 11:41:09 +0200 Message-Id: <1531906876-13451-33-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 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: