From patchwork Mon Sep 23 14:15:42 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steve Capper X-Patchwork-Id: 2928101 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 3BDC49F3C4 for ; Mon, 23 Sep 2013 14:16:38 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id D193E2038E for ; Mon, 23 Sep 2013 14:16:36 +0000 (UTC) Received: from casper.infradead.org (casper.infradead.org [85.118.1.10]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 6B92420373 for ; Mon, 23 Sep 2013 14:16:35 +0000 (UTC) Received: from merlin.infradead.org ([2001:4978:20e::2]) by casper.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1VO6w5-00077P-M3; Mon, 23 Sep 2013 14:16:25 +0000 Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1VO6w3-0007hE-9s; Mon, 23 Sep 2013 14:16:23 +0000 Received: from mail-wg0-f54.google.com ([74.125.82.54]) by merlin.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1VO6vz-0007gp-OX for linux-arm-kernel@lists.infradead.org; Mon, 23 Sep 2013 14:16:20 +0000 Received: by mail-wg0-f54.google.com with SMTP id m15so3274937wgh.9 for ; Mon, 23 Sep 2013 07:15:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=38+FbHkc2qOZtjNphqi21GE2U2fBsqYmZ7qvChHLrmM=; b=eYhGp2frA8bz5hvLFi16Q3dPK4O9CTGWH5nNhS7Ubt5ytoYPZ98UwAervZq+VLB/68 8bstYOs0iBYCtEWY5XE5mC8dPPuT8ex/mskC1H0vbN6imymaUbnOq9R5sC27xvhQxBZH bXezMhQi4eEAK5k2Kg89uorr+momEmuvFMLvDkjfif8U6xlXHQC9jw1rryI2bi631fzf DpvsccpHMbbByH90QIpbSeH1mdsyK5aOPVYQqElJAyorrqKUjq6O70ljenZUS3jXUHe2 qz1JunWpUyeYlzlsMcm1Zz4bNehBi4w5u8e6X1kzGn6+cqDusoDpMsuzoW1Q/2bBeGIN 2E/A== X-Gm-Message-State: ALoCoQkrYSyk/r9V6i6mpEVWJkS69wGIkHj7SyQ5uoEZhEDas9/Xqr7ZXhGERRcA0Np64IDvoMyY X-Received: by 10.180.73.65 with SMTP id j1mr13849294wiv.10.1379945756532; Mon, 23 Sep 2013 07:15:56 -0700 (PDT) Received: from marmot.wormnet.eu (marmot.wormnet.eu. [188.246.204.87]) by mx.google.com with ESMTPSA id i8sm26146219wiy.6.1969.12.31.16.00.00 (version=TLSv1.2 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 23 Sep 2013 07:15:56 -0700 (PDT) From: Steve Capper To: linux-arm-kernel@lists.infradead.org Subject: [RESEND RFC V2] ARM: mm: make UACCESS_WITH_MEMCPY huge page aware Date: Mon, 23 Sep 2013 15:15:42 +0100 Message-Id: <1379945742-9457-1-git-send-email-steve.capper@linaro.org> X-Mailer: git-send-email 1.7.10.4 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20130923_101620_008058_4066D9C2 X-CRM114-Status: GOOD ( 20.40 ) X-Spam-Score: -0.1 (/) Cc: Steve Capper , linaro-kernel@lists.linaro.org, linux@arm.linux.org.uk, patches@linaro.org, nico@linaro.org X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-4.0 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD,SUSPICIOUS_RECIPS,UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Resending, as I ommitted a few important CC's. --- The memory pinning code in uaccess_with_memcpy.c does not check for HugeTLB or THP pmds, and will enter an infinite loop should a __copy_to_user or __clear_user occur against a huge page. This patch adds detection code for huge pages to pin_page_for_write. As this code can be executed in a fast path it refers to the actual pmds rather than the vma. If a HugeTLB or THP is found (they have the same pmd representation on ARM), the page table spinlock is taken to prevent modification whilst the page is pinned. On ARM, huge pages are only represented as pmds, thus no huge pud checks are performed. (For huge puds one would lock the page table in a similar manner as in the pmd case). Two helper functions are introduced; pmd_thp_or_huge will check whether or not a page is huge or transparent huge (which have the same pmd layout on ARM), and pmd_hugewillfault will detect whether or not a page fault will occur on write to the page. Changes since first RFC: * The page mask is widened for hugepages to reduce the number of potential locks/unlocks. (A knobbled /dev/zero with its latency reduction chunks removed shows a 2x data rate boost with hugepages backing: dd if=/dev/zero of=/dev/null bs=10M count=1024 ) Signed-off-by: Steve Capper --- arch/arm/include/asm/pgtable-3level.h | 3 ++ arch/arm/lib/uaccess_with_memcpy.c | 57 ++++++++++++++++++++++++++++++----- 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/arch/arm/include/asm/pgtable-3level.h b/arch/arm/include/asm/pgtable-3level.h index 5689c18..39c54cf 100644 --- a/arch/arm/include/asm/pgtable-3level.h +++ b/arch/arm/include/asm/pgtable-3level.h @@ -206,6 +206,9 @@ static inline pmd_t *pmd_offset(pud_t *pud, unsigned long addr) #define __HAVE_ARCH_PMD_WRITE #define pmd_write(pmd) (!(pmd_val(pmd) & PMD_SECT_RDONLY)) +#define pmd_hugewillfault(pmd) (!pmd_young(pmd) || !pmd_write(pmd)) +#define pmd_thp_or_huge(pmd) (pmd_huge(pmd) || pmd_trans_huge(pmd)) + #ifdef CONFIG_TRANSPARENT_HUGEPAGE #define pmd_trans_huge(pmd) (pmd_val(pmd) && !(pmd_val(pmd) & PMD_TABLE_BIT)) #define pmd_trans_splitting(pmd) (pmd_val(pmd) & PMD_SECT_SPLITTING) diff --git a/arch/arm/lib/uaccess_with_memcpy.c b/arch/arm/lib/uaccess_with_memcpy.c index 025f742..78756db 100644 --- a/arch/arm/lib/uaccess_with_memcpy.c +++ b/arch/arm/lib/uaccess_with_memcpy.c @@ -18,11 +18,13 @@ #include /* for in_atomic() */ #include #include +#include #include #include static int -pin_page_for_write(const void __user *_addr, pte_t **ptep, spinlock_t **ptlp) +pin_page_for_write(const void __user *_addr, pte_t **ptep, spinlock_t **ptlp, + unsigned long *page_mask) { unsigned long addr = (unsigned long)_addr; pgd_t *pgd; @@ -40,7 +42,36 @@ pin_page_for_write(const void __user *_addr, pte_t **ptep, spinlock_t **ptlp) return 0; pmd = pmd_offset(pud, addr); - if (unlikely(pmd_none(*pmd) || pmd_bad(*pmd))) + if (unlikely(pmd_none(*pmd))) + return 0; + + /* + * A pmd can be bad if it refers to a HugeTLB or THP page. + * + * Both THP and HugeTLB pages have the same pmd layout + * and should not be manipulated by the pte functions. + * + * Lock the page table for the destination and check + * to see that it's still huge and whether or not we will + * need to fault on write, or if we have a splitting THP. + */ + if (unlikely(pmd_thp_or_huge(*pmd))) { + ptl = ¤t->mm->page_table_lock; + spin_lock(ptl); + if (unlikely(!pmd_thp_or_huge(*pmd) + || pmd_hugewillfault(*pmd) + || pmd_trans_splitting(*pmd))) { + spin_unlock(ptl); + return 0; + } + + *ptep = NULL; + *ptlp = ptl; + *page_mask = HPAGE_MASK; + return 1; + } + + if (unlikely(pmd_bad(*pmd))) return 0; pte = pte_offset_map_lock(current->mm, pmd, addr, &ptl); @@ -52,6 +83,7 @@ pin_page_for_write(const void __user *_addr, pte_t **ptep, spinlock_t **ptlp) *ptep = pte; *ptlp = ptl; + *page_mask = PAGE_MASK; return 1; } @@ -60,6 +92,7 @@ static unsigned long noinline __copy_to_user_memcpy(void __user *to, const void *from, unsigned long n) { int atomic; + unsigned long page_mask; if (unlikely(segment_eq(get_fs(), KERNEL_DS))) { memcpy((void *)to, from, n); @@ -76,7 +109,7 @@ __copy_to_user_memcpy(void __user *to, const void *from, unsigned long n) spinlock_t *ptl; int tocopy; - while (!pin_page_for_write(to, &pte, &ptl)) { + while (!pin_page_for_write(to, &pte, &ptl, &page_mask)) { if (!atomic) up_read(¤t->mm->mmap_sem); if (__put_user(0, (char __user *)to)) @@ -85,7 +118,7 @@ __copy_to_user_memcpy(void __user *to, const void *from, unsigned long n) down_read(¤t->mm->mmap_sem); } - tocopy = (~(unsigned long)to & ~PAGE_MASK) + 1; + tocopy = (~(unsigned long)to & ~page_mask) + 1; if (tocopy > n) tocopy = n; @@ -94,7 +127,10 @@ __copy_to_user_memcpy(void __user *to, const void *from, unsigned long n) from += tocopy; n -= tocopy; - pte_unmap_unlock(pte, ptl); + if (pte) + pte_unmap_unlock(pte, ptl); + else + spin_unlock(ptl); } if (!atomic) up_read(¤t->mm->mmap_sem); @@ -121,6 +157,8 @@ __copy_to_user(void __user *to, const void *from, unsigned long n) static unsigned long noinline __clear_user_memset(void __user *addr, unsigned long n) { + unsigned long page_mask; + if (unlikely(segment_eq(get_fs(), KERNEL_DS))) { memset((void *)addr, 0, n); return 0; @@ -132,14 +170,14 @@ __clear_user_memset(void __user *addr, unsigned long n) spinlock_t *ptl; int tocopy; - while (!pin_page_for_write(addr, &pte, &ptl)) { + while (!pin_page_for_write(addr, &pte, &ptl, &page_mask)) { up_read(¤t->mm->mmap_sem); if (__put_user(0, (char __user *)addr)) goto out; down_read(¤t->mm->mmap_sem); } - tocopy = (~(unsigned long)addr & ~PAGE_MASK) + 1; + tocopy = (~(unsigned long)addr & ~page_mask) + 1; if (tocopy > n) tocopy = n; @@ -147,7 +185,10 @@ __clear_user_memset(void __user *addr, unsigned long n) addr += tocopy; n -= tocopy; - pte_unmap_unlock(pte, ptl); + if (pte) + pte_unmap_unlock(pte, ptl); + else + spin_unlock(ptl); } up_read(¤t->mm->mmap_sem);