Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp1101686pxa; Thu, 20 Aug 2020 02:29:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzSVyvDdeFS8v1/KAqyPc08NUE76IWuT3WfxiZCTRsn5ybtsfX7Sgi/4lBean/A8rIfZOWk X-Received: by 2002:a17:906:8318:: with SMTP id j24mr2278590ejx.409.1597915777260; Thu, 20 Aug 2020 02:29:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597915777; cv=none; d=google.com; s=arc-20160816; b=i2OKAD5bA+GL0hsZU/ZOYGV6K8dqBgh2tblXorxCjrsxL63Fl7ksEcbJqN+a4oatiU ZqwzeuDv0+J3RiX06ZqxM5O7uO22AcZ6ERNkQte+Qgin7mF95cYnD3I84WhbstNFf/cw gfXe69V1RpLzFPdLG73mDeJprqW1TGh9QD7F381Doueo8IuF72WsJOTJ6gDIKMSYHqCT fKRlWvxGFkX8eFmoNpoINc1ZHu0GvbXZZrs9wvyE35RSFV7X3miqqGRsf61COqVnuC8r nuzvIVYd9es+iJU3DUiU6Ub/PEvXgw6uIasShyNaVfoGBLjCH4kGVECx1cEoCqxMKUE1 Gk/w== 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=tNZVpJej1Cg12zNVt8jz4wjX/sU2MT2yCFblZzqWC3s=; b=hfdrj8qJZZPvlUQSCmlBx2iKRQ4husegZLuVdETU9yWeni3l5wUzajnLAZDUzEhPGG 1mtt1xqWrj3ROr68ND42a1rrBal3BpM9i7tI88Wz4vBrSQs2IzNR/zZnYs3mt/zE6RXZ hfWTMDawgvUZxdRiiqHf/LVMuZHniE9A7fsezS0e6w8Ewo3BqNhvlfMaYxr2T+4xyLon oFkhLAp39SGS5+9eJIyQB+GBJvCxC8AujDVGfj+rp3VDY6mhagA7T2+yGsnmwpxmjyeq QGyxtk9b+HPr9MH11K+40U3b9dd0C0dtETPhfRXINlqmdqRU6jPLtRHSKPgf4pHujIc+ PR+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=vj9xj3iA; 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=fail (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 y5si784588eje.702.2020.08.20.02.29.13; Thu, 20 Aug 2020 02:29:37 -0700 (PDT) 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=@kernel.org header.s=default header.b=vj9xj3iA; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727084AbgHTJ2V (ORCPT + 99 others); Thu, 20 Aug 2020 05:28:21 -0400 Received: from mail.kernel.org ([198.145.29.99]:34874 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727955AbgHTJ1C (ORCPT ); Thu, 20 Aug 2020 05:27:02 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (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 E153222D02; Thu, 20 Aug 2020 09:27:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1597915621; bh=7a11vyjkFBvgDh5rGkcLcXiUPLhdNhbeJ8aUbN/3XCk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=vj9xj3iA0bRK6mUk9bDb27WJ3F3eWmlsFKvhEktXO2yXraB0gGeQpEgtzIYOek72c nZujJmsmGwyMxzArtocCyTB818rDxoi0qQyUhXKo9edIRpUNA/gO1+SB+NS/Uv5Lxk j7ggkhKt+WOnpVRD5xm6LmLyrRidH3GWxyCT4luY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Peter Xu , Andrew Morton , Mike Kravetz , Andrea Arcangeli , Matthew Wilcox , Linus Torvalds Subject: [PATCH 5.8 078/232] mm/hugetlb: fix calculation of adjust_range_if_pmd_sharing_possible Date: Thu, 20 Aug 2020 11:18:49 +0200 Message-Id: <20200820091616.590598104@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200820091612.692383444@linuxfoundation.org> References: <20200820091612.692383444@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: Peter Xu commit 75802ca66354a39ab8e35822747cd08b3384a99a upstream. This is found by code observation only. Firstly, the worst case scenario should assume the whole range was covered by pmd sharing. The old algorithm might not work as expected for ranges like (1g-2m, 1g+2m), where the adjusted range should be (0, 1g+2m) but the expected range should be (0, 2g). Since at it, remove the loop since it should not be required. With that, the new code should be faster too when the invalidating range is huge. Mike said: : With range (1g-2m, 1g+2m) within a vma (0, 2g) the existing code will only : adjust to (0, 1g+2m) which is incorrect. : : We should cc stable. The original reason for adjusting the range was to : prevent data corruption (getting wrong page). Since the range is not : always adjusted correctly, the potential for corruption still exists. : : However, I am fairly confident that adjust_range_if_pmd_sharing_possible : is only gong to be called in two cases: : : 1) for a single page : 2) for range == entire vma : : In those cases, the current code should produce the correct results. : : To be safe, let's just cc stable. Fixes: 017b1660df89 ("mm: migration: fix migration of huge PMD shared pages") Signed-off-by: Peter Xu Signed-off-by: Andrew Morton Reviewed-by: Mike Kravetz Cc: Andrea Arcangeli Cc: Matthew Wilcox Cc: Link: http://lkml.kernel.org/r/20200730201636.74778-1-peterx@redhat.com Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- mm/hugetlb.c | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -5313,25 +5313,21 @@ 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 check_addr; + unsigned long a_start, a_end; if (!(vma->vm_flags & VM_MAYSHARE)) return; - for (check_addr = *start; check_addr < *end; check_addr += PUD_SIZE) { - unsigned long a_start = check_addr & PUD_MASK; - unsigned long a_end = a_start + PUD_SIZE; + /* 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 sharing is possible, adjust start/end if necessary. - */ - if (range_in_vma(vma, a_start, a_end)) { - if (a_start < *start) - *start = a_start; - if (a_end > *end) - *end = a_end; - } - } + /* + * 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); } /*