Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1977739pxb; Fri, 5 Mar 2021 04:37:15 -0800 (PST) X-Google-Smtp-Source: ABdhPJyLtMhFgHVmjRt7PiWaTMuoIz44d6J6uomYUIh6nNQwhRMgN9KpGNQ6g5yLyqUo8/CETtdC X-Received: by 2002:aa7:cb8f:: with SMTP id r15mr9012886edt.130.1614947835794; Fri, 05 Mar 2021 04:37:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614947835; cv=none; d=google.com; s=arc-20160816; b=gvJkzjpiJ/hac29hGjFMIWOVEZce2HSzsQDw2n0g/99KsHIaBuQg+pMeQRxKbqxDTh EXAFdtUMEcVQu/3EyW4SM9XDfTL4h2uquo6h9dsaDe7upyzOQksLRUVm3jNZXvLBero5 5fSZ5YekvBeVpziJWiPksmx7fhwnZERyn7+lmSdkuBsv+tEQYjq9Wx8RP3nZxTVWhbNz rE1oaqjP/xYxRqPtD++IbsgZdAYwSTA5vzej+wmxfFT1XWyrfXeK49kVzNWLg/TzM9yu Gwrt6PPTT2X/whQXeY8iHuBdIYgo7Xyn/g5PTl6WYZGGCujbdV0Jgr8e+aWOzbnbB/FF 3PiQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=sWQMz/7kzI2Y1QGtXqsKtULLQ2A+YkI/+wLF6UjqCpQ=; b=Zjcpf9WXW2hF0ARWf3QBCGG+zhtTXRKEhKiJtzgaW53Fk9L2DhxmlY1xJl3Wy6TrCD 7+zywRa9k/kPIjz44qU36BqHf7ttAyv6xMzoeMeDqOTcjOEUEQPFLb0pA+v+ERKz39fj nzjdJWa3QYCoL8+vFJcXCXlR8CyyBHRYjYkE4sm78brOqKKRW+2iUeFE6TmDfhco0AX3 cyLlmV4CHSdsYANuMuz27EXQDTHoOCyy78nLmVQZvCxnMRor+1nWP8G4H6kdnhWkwXSA qBuT4SFj9YjtefP+gIypL2Pv+0773tGjZKj3wyhG9HFKUlf+uODuTK8gu7aCO5d3i1GT yhHA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Y5NkFtCX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v14si1244889ejj.133.2021.03.05.04.36.51; Fri, 05 Mar 2021 04:37:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Y5NkFtCX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232565AbhCEMdy (ORCPT + 99 others); Fri, 5 Mar 2021 07:33:54 -0500 Received: from mail.kernel.org ([198.145.29.99]:43982 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232519AbhCEMdV (ORCPT ); Fri, 5 Mar 2021 07:33:21 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 0D9B165013; Fri, 5 Mar 2021 12:33:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1614947601; bh=iuWErcUY2vcp1pXGgh5MprpdIFO6CsMmB3GaLscb55M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Y5NkFtCXeR5DHeBDNMgsEHJi0WBjwtbuHxg1/asIktgpCwjbsNpgRfiAHSxPatlE7 nGWOQC9/0G9dbXmkFHXp8eL3a7bXETZAx7sNFDAq7ck8LWDvPl2x7OS3/z1UzuRhQb LStxpD0vdWtrUDYrSlD1wigovT2kqCl1GlDT66es= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Li Xinhai , Mike Kravetz , Peter Xu , Andrew Morton , Linus Torvalds Subject: [PATCH 5.4 17/72] mm/hugetlb.c: fix unnecessary address expansion of pmd sharing Date: Fri, 5 Mar 2021 13:21:19 +0100 Message-Id: <20210305120858.179992865@linuxfoundation.org> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210305120857.341630346@linuxfoundation.org> References: <20210305120857.341630346@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Li Xinhai commit a1ba9da8f0f9a37d900ff7eff66482cf7de8015e upstream. The current code would unnecessarily expand the address range. Consider one example, (start, end) = (1G-2M, 3G+2M), and (vm_start, vm_end) = (1G-4M, 3G+4M), the expected adjustment should be keep (1G-2M, 3G+2M) without expand. But the current result will be (1G-4M, 3G+4M). Actually, the range (1G-4M, 1G) and (3G, 3G+4M) would never been involved in pmd sharing. After this patch, we will check that the vma span at least one PUD aligned size and the start,end range overlap the aligned range of vma. With above example, the aligned vma range is (1G, 3G), so if (start, end) range is within (1G-4M, 1G), or within (3G, 3G+4M), then no adjustment to both start and end. Otherwise, we will have chance to adjust start downwards or end upwards without exceeding (vm_start, vm_end). Mike: : The 'adjusted range' is used for calls to mmu notifiers and cache(tlb) : flushing. Since the current code unnecessarily expands the range in some : cases, more entries than necessary would be flushed. This would/could : result in performance degradation. However, this is highly dependent on : the user runtime. Is there a combination of vma layout and calls to : actually hit this issue? If the issue is hit, will those entries : unnecessarily flushed be used again and need to be unnecessarily reloaded? Link: https://lkml.kernel.org/r/20210104081631.2921415-1-lixinhai.lxh@gmail.com Fixes: 75802ca66354 ("mm/hugetlb: fix calculation of adjust_range_if_pmd_sharing_possible") Signed-off-by: Li Xinhai Suggested-by: Mike Kravetz Reviewed-by: Mike Kravetz Cc: Peter Xu Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- mm/hugetlb.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -4902,21 +4902,23 @@ static bool vma_shareable(struct vm_area void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma, unsigned long *start, unsigned long *end) { - unsigned long a_start, a_end; + unsigned long v_start = ALIGN(vma->vm_start, PUD_SIZE), + v_end = ALIGN_DOWN(vma->vm_end, PUD_SIZE); - if (!(vma->vm_flags & VM_MAYSHARE)) + /* + * vma need span at least one aligned PUD size and the start,end range + * must at least partialy within it. + */ + if (!(vma->vm_flags & VM_MAYSHARE) || !(v_end > v_start) || + (*end <= v_start) || (*start >= v_end)) return; /* Extend the range to be PUD aligned for a worst case scenario */ - a_start = ALIGN_DOWN(*start, PUD_SIZE); - a_end = ALIGN(*end, PUD_SIZE); + if (*start > v_start) + *start = ALIGN_DOWN(*start, PUD_SIZE); - /* - * Intersect the range with the vma range, since pmd sharing won't be - * across vma after all - */ - *start = max(vma->vm_start, a_start); - *end = min(vma->vm_end, a_end); + if (*end < v_end) + *end = ALIGN(*end, PUD_SIZE); } /*