Received: by 10.223.185.116 with SMTP id b49csp5322395wrg; Wed, 7 Mar 2018 09:48:53 -0800 (PST) X-Google-Smtp-Source: AG47ELsa9n6RPz+xENFjSh68QzDshnQXTCgolaazOMH7HnQG7xbqW4xz8pRl2ANVHBfE0JokDZQ8 X-Received: by 10.99.64.198 with SMTP id n189mr18626071pga.191.1520444933627; Wed, 07 Mar 2018 09:48:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520444933; cv=none; d=google.com; s=arc-20160816; b=F3ZRp01zJ/tPKgFQsCO6xtgrmeOm15ulvxm/LT6GhmJR9GboFd9mqyQx4HKveDpNn/ LZdX9o0MVBfdmfkpbGKN+vmS1z0zJhgaD+hJlrVdhNK9IuUwXHdfQFgrkMT7PFkM6Ntq v2Gmc7YWOWMN4CdqX+2B7MnB3f29c9wP/UcSZJSlWJUGlLMk3plk8Z3CkFJuXL74SVvk a1AO3IQF/YXSt60Y7/wjXwON/4KaJmn62bTOir2IMQIg16X5TsiQGTnQZkIhZsE/4TeI IhTMuXBRb1pSqGcDbm/8lXBQtbzEm/CmVDxidjIHEuuO2l/czbvO7OV0Jbq166nqIAjF ItkA== 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:arc-authentication-results; bh=rgSmH17Y7d7JdhxRVUt3JZsa9h65WwaSGJV8mgL2uEg=; b=0MnXmsT5qgVnmMMnSd8drmyfBMtVzyi0A8ZxGfV064k3P98ZJZCg8radguoy25amza HfpuNdtIA7SdOwG2G/k7JoAQ++Va5yKIza0QdRexDlMleg9s2MWAZIEsrr7ewAkBJbEU LdIt6vhiesL0YkR2nTe+FbfsZI+QrZAGRaX0Bcf4x5kYe2HXze90feFCr073SbUCQGbQ HLchd1XnE1UB3wdW3/2S92JYOnpLWXfjWrmOLxlU65mMFxWTVONFEQ52dH3qWrMxwOhM VEbTreWT+K9gp1L6fBt0k1n/rR5FTG/ygYkBy4B4j08QGvN38P3hSVRRcqUpHp/eSFaE sWlQ== 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 k9-v6si6810349pli.490.2018.03.07.09.48.37; Wed, 07 Mar 2018 09:48:53 -0800 (PST) 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 S933889AbeCGRrn (ORCPT + 99 others); Wed, 7 Mar 2018 12:47:43 -0500 Received: from g2t2354.austin.hpe.com ([15.233.44.27]:4393 "EHLO g2t2354.austin.hpe.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933278AbeCGRrg (ORCPT ); Wed, 7 Mar 2018 12:47:36 -0500 Received: from g2t2360.austin.hpecorp.net (g2t2360.austin.hpecorp.net [16.196.225.135]) by g2t2354.austin.hpe.com (Postfix) with ESMTP id 27624A8; Wed, 7 Mar 2018 17:47:35 +0000 (UTC) Received: from misato.americas.hpqcorp.net (misato.americas.hpqcorp.net [10.34.81.122]) by g2t2360.austin.hpecorp.net (Postfix) with ESMTP id 70C4336; Wed, 7 Mar 2018 17:47:34 +0000 (UTC) From: Toshi Kani To: mhocko@suse.com, akpm@linux-foundation.org, tglx@linutronix.de, mingo@redhat.com, hpa@zytor.com, bp@suse.de, catalin.marinas@arm.com Cc: guohanjun@huawei.com, will.deacon@arm.com, wxf.wang@hisilicon.com, linux-mm@kvack.org, x86@kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Toshi Kani Subject: [PATCH 1/2] mm/vmalloc: Add interfaces to free unused page table Date: Wed, 7 Mar 2018 11:32:26 -0700 Message-Id: <20180307183227.17983-2-toshi.kani@hpe.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180307183227.17983-1-toshi.kani@hpe.com> References: <20180307183227.17983-1-toshi.kani@hpe.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On architectures with CONFIG_HAVE_ARCH_HUGE_VMAP set, ioremap() may create pud/pmd mappings. Kernel panic was observed on arm64 systems with Cortex-A75 in the following steps as described by Hanjun Guo. 1. ioremap a 4K size, valid page table will build, 2. iounmap it, pte0 will set to 0; 3. ioremap the same address with 2M size, pgd/pmd is unchanged, then set the a new value for pmd; 4. pte0 is leaked; 5. CPU may meet exception because the old pmd is still in TLB, which will lead to kernel panic. This panic is not reproducible on x86. INVLPG, called from iounmap, purges all levels of entries associated with purged address on x86. x86 still has memory leak. Add two interfaces, pud_free_pmd_page() and pmd_free_pte_page(), which clear a given pud/pmd entry and free up a page for the lower level entries. This patch implements their stub functions on x86 and arm64, which work as workaround. Reported-by: Lei Li Signed-off-by: Toshi Kani Cc: Catalin Marinas Cc: Wang Xuefeng Cc: Will Deacon Cc: Hanjun Guo Cc: Michal Hocko Cc: Andrew Morton Cc: Thomas Gleixner Cc: Ingo Molnar Cc: "H. Peter Anvin" Cc: Borislav Petkov --- arch/arm64/mm/mmu.c | 10 ++++++++++ arch/x86/mm/pgtable.c | 20 ++++++++++++++++++++ include/asm-generic/pgtable.h | 10 ++++++++++ lib/ioremap.c | 6 ++++-- 4 files changed, 44 insertions(+), 2 deletions(-) diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c index 84a019f55022..84a37b4bc28e 100644 --- a/arch/arm64/mm/mmu.c +++ b/arch/arm64/mm/mmu.c @@ -972,3 +972,13 @@ int pmd_clear_huge(pmd_t *pmdp) pmd_clear(pmdp); return 1; } + +int pud_free_pmd_page(pud_t *pud) +{ + return pud_none(*pud); +} + +int pmd_free_pte_page(pmd_t *pmd) +{ + return pmd_none(*pmd); +} diff --git a/arch/x86/mm/pgtable.c b/arch/x86/mm/pgtable.c index 004abf9ebf12..942f4fa341f1 100644 --- a/arch/x86/mm/pgtable.c +++ b/arch/x86/mm/pgtable.c @@ -702,4 +702,24 @@ int pmd_clear_huge(pmd_t *pmd) return 0; } + +/** + * pud_free_pmd_page - clear pud entry and free pmd page + * + * Returns 1 on success and 0 on failure (pud not cleared). + */ +int pud_free_pmd_page(pud_t *pud) +{ + return pud_none(*pud); +} + +/** + * pmd_free_pte_page - clear pmd entry and free pte page + * + * Returns 1 on success and 0 on failure (pmd not cleared). + */ +int pmd_free_pte_page(pmd_t *pmd) +{ + return pmd_none(*pmd); +} #endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */ diff --git a/include/asm-generic/pgtable.h b/include/asm-generic/pgtable.h index 2cfa3075d148..2490800f7c5a 100644 --- a/include/asm-generic/pgtable.h +++ b/include/asm-generic/pgtable.h @@ -983,6 +983,8 @@ int pud_set_huge(pud_t *pud, phys_addr_t addr, pgprot_t prot); int pmd_set_huge(pmd_t *pmd, phys_addr_t addr, pgprot_t prot); int pud_clear_huge(pud_t *pud); int pmd_clear_huge(pmd_t *pmd); +int pud_free_pmd_page(pud_t *pud); +int pmd_free_pte_page(pmd_t *pmd); #else /* !CONFIG_HAVE_ARCH_HUGE_VMAP */ static inline int p4d_set_huge(p4d_t *p4d, phys_addr_t addr, pgprot_t prot) { @@ -1008,6 +1010,14 @@ static inline int pmd_clear_huge(pmd_t *pmd) { return 0; } +static inline int pud_free_pmd_page(pud_t *pud) +{ + return 0; +} +static inline int pmd_free_pte_page(pud_t *pmd) +{ + return 0; +} #endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */ #ifndef __HAVE_ARCH_FLUSH_PMD_TLB_RANGE diff --git a/lib/ioremap.c b/lib/ioremap.c index b808a390e4c3..54e5bbaa3200 100644 --- a/lib/ioremap.c +++ b/lib/ioremap.c @@ -91,7 +91,8 @@ static inline int ioremap_pmd_range(pud_t *pud, unsigned long addr, if (ioremap_pmd_enabled() && ((next - addr) == PMD_SIZE) && - IS_ALIGNED(phys_addr + addr, PMD_SIZE)) { + IS_ALIGNED(phys_addr + addr, PMD_SIZE) && + pmd_free_pte_page(pmd)) { if (pmd_set_huge(pmd, phys_addr + addr, prot)) continue; } @@ -117,7 +118,8 @@ static inline int ioremap_pud_range(p4d_t *p4d, unsigned long addr, if (ioremap_pud_enabled() && ((next - addr) == PUD_SIZE) && - IS_ALIGNED(phys_addr + addr, PUD_SIZE)) { + IS_ALIGNED(phys_addr + addr, PUD_SIZE) && + pud_free_pmd_page(pud)) { if (pud_set_huge(pud, phys_addr + addr, prot)) continue; }