Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp4821450ybl; Wed, 22 Jan 2020 05:23:48 -0800 (PST) X-Google-Smtp-Source: APXvYqy/fc2PIydPwXE8tvyW5Tq8RrLvM1OVv7/dUyZgmPSsvK38aU4677dnkDqYa37pMju1cuRo X-Received: by 2002:a9d:4e97:: with SMTP id v23mr7004965otk.201.1579699428829; Wed, 22 Jan 2020 05:23:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579699428; cv=none; d=google.com; s=arc-20160816; b=YI7g6jV+lP2l23F3JMedfVZrLGAfND2uJFBZOw70qEvQuTKkFsk6PF8Bx88oeMeJUK GlUNckLzU+DyR94gxo95wxLYkpd4x16ItXCHS3QNKYAFUj3t7FYnhTC/a7Yj3EmN2306 JZHpKLwZw/jexF39sgmkt9R2hgRChO1PGU+7Ivw0UzY9bmHMToFzQVEsej/0I0vJzUYb Q+sHhrnau5hcQeR6D6yO86U2YaE6dzjTWucHGRwAAWxe3H9C2z0sF6boyEybCsx0UZQO OmaSNfUw53NlmI1S7AeMpabXyrCyerAy94nwAxVSMMY+zw0Hr+xuSo1AJWHJ/4BAIxNN l0PQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=4v9Jj5eHuMglon1bOdxBfxc1gfOvSj6wyIM1vD1b5uE=; b=QsO9bXPSl+rmUO8bjVUOnVUJn5THZLo9rSzyzlgkdevCqbA74/w0R0HTBQrzzo1KBp Cu+4xHIAgshOaWLcgP0xJDx6GrzWJEtmjkcmaJ+DAeop+T0hR8nH+UjyF5fugtTo0swk iOwGUQaQ1VdIYBCfI94wgwBk/vPxJeaBnK3pjQWMNwlHbDeDsQRa5+758f145aZvdCbK s14+0zhtGrUtTSCY5JChD1Wm+J29l7XB29AOGwwWyphHqxyb0Ms/uL3Pk2Tt+zJP+KbN 57tLHP6V69oF7yrxgf15dEymdBYAcnkdP8ZwBLoPaexCMe9PP/92hUovsnsPTdegeV8O jSMA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=M15oBGCu; 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 b9si21151705oie.20.2020.01.22.05.23.36; Wed, 22 Jan 2020 05:23:48 -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; dkim=pass header.i=@kernel.org header.s=default header.b=M15oBGCu; 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 S1730546AbgAVNWY (ORCPT + 99 others); Wed, 22 Jan 2020 08:22:24 -0500 Received: from mail.kernel.org ([198.145.29.99]:39984 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730520AbgAVNWX (ORCPT ); Wed, 22 Jan 2020 08:22:23 -0500 Received: from localhost (unknown [84.241.205.26]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id CBC4324688; Wed, 22 Jan 2020 13:22:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1579699342; bh=MAnP2AhfIm6LZWZ1PsAzMs4DYiF5mhiV3PWTnyrUOOU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=M15oBGCuegSpSf16vNEZ2+IkkaKpMZYmBEZgI+QMb18AyriBAPCTM3Vujp25fzxmw f14gZlXi/FUM6tNKZW4dTYERN/lpa6HanbdNNIY0cUHm6nwjB+YCsMXOZ8qi/Hj134 uzOz6NlEmwx0nxyiK4bg9ZuviSNeXgN6FyjcjYbY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, "Kirill A. Shutemov" , Thomas Willhalm , Dan Williams , "Aneesh Kumar K . V" , "Bruggeman, Otto G" , Andrew Morton , Linus Torvalds Subject: [PATCH 5.4 079/222] mm/huge_memory.c: thp: fix conflict of above-47bit hint address and PMD alignment Date: Wed, 22 Jan 2020 10:27:45 +0100 Message-Id: <20200122092839.369227278@linuxfoundation.org> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200122092833.339495161@linuxfoundation.org> References: <20200122092833.339495161@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Kirill A. Shutemov commit 97d3d0f9a1cf132c63c0b8b8bd497b8a56283dd9 upstream. Patch series "Fix two above-47bit hint address vs. THP bugs". The two get_unmapped_area() implementations have to be fixed to provide THP-friendly mappings if above-47bit hint address is specified. This patch (of 2): Filesystems use thp_get_unmapped_area() to provide THP-friendly mappings. For DAX in particular. Normally, the kernel doesn't create userspace mappings above 47-bit, even if the machine allows this (such as with 5-level paging on x86-64). Not all user space is ready to handle wide addresses. It's known that at least some JIT compilers use higher bits in pointers to encode their information. Userspace can ask for allocation from full address space by specifying hint address (with or without MAP_FIXED) above 47-bits. If the application doesn't need a particular address, but wants to allocate from whole address space it can specify -1 as a hint address. Unfortunately, this trick breaks thp_get_unmapped_area(): the function would not try to allocate PMD-aligned area if *any* hint address specified. Modify the routine to handle it correctly: - Try to allocate the space at the specified hint address with length padding required for PMD alignment. - If failed, retry without length padding (but with the same hint address); - If the returned address matches the hint address return it. - Otherwise, align the address as required for THP and return. The user specified hint address is passed down to get_unmapped_area() so above-47bit hint address will be taken into account without breaking alignment requirements. Link: http://lkml.kernel.org/r/20191220142548.7118-2-kirill.shutemov@linux.intel.com Fixes: b569bab78d8d ("x86/mm: Prepare to expose larger address space to userspace") Signed-off-by: Kirill A. Shutemov Reported-by: Thomas Willhalm Tested-by: Dan Williams Cc: "Aneesh Kumar K . V" Cc: "Bruggeman, Otto G" Cc: Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- mm/huge_memory.c | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -527,13 +527,13 @@ void prep_transhuge_page(struct page *pa set_compound_page_dtor(page, TRANSHUGE_PAGE_DTOR); } -static unsigned long __thp_get_unmapped_area(struct file *filp, unsigned long len, +static unsigned long __thp_get_unmapped_area(struct file *filp, + unsigned long addr, unsigned long len, loff_t off, unsigned long flags, unsigned long size) { - unsigned long addr; loff_t off_end = off + len; loff_t off_align = round_up(off, size); - unsigned long len_pad; + unsigned long len_pad, ret; if (off_end <= off_align || (off_end - off_align) < size) return 0; @@ -542,30 +542,40 @@ static unsigned long __thp_get_unmapped_ if (len_pad < len || (off + len_pad) < off) return 0; - addr = current->mm->get_unmapped_area(filp, 0, len_pad, + ret = current->mm->get_unmapped_area(filp, addr, len_pad, off >> PAGE_SHIFT, flags); - if (IS_ERR_VALUE(addr)) + + /* + * The failure might be due to length padding. The caller will retry + * without the padding. + */ + if (IS_ERR_VALUE(ret)) return 0; - addr += (off - addr) & (size - 1); - return addr; + /* + * Do not try to align to THP boundary if allocation at the address + * hint succeeds. + */ + if (ret == addr) + return addr; + + ret += (off - ret) & (size - 1); + return ret; } unsigned long thp_get_unmapped_area(struct file *filp, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags) { + unsigned long ret; loff_t off = (loff_t)pgoff << PAGE_SHIFT; - if (addr) - goto out; if (!IS_DAX(filp->f_mapping->host) || !IS_ENABLED(CONFIG_FS_DAX_PMD)) goto out; - addr = __thp_get_unmapped_area(filp, len, off, flags, PMD_SIZE); - if (addr) - return addr; - - out: + ret = __thp_get_unmapped_area(filp, addr, len, off, flags, PMD_SIZE); + if (ret) + return ret; +out: return current->mm->get_unmapped_area(filp, addr, len, pgoff, flags); } EXPORT_SYMBOL_GPL(thp_get_unmapped_area);