From patchwork Mon Jan 4 08:16:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Li Xinhai X-Patchwork-Id: 11996317 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BDF97C433DB for ; Mon, 4 Jan 2021 08:16:45 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 4E94720758 for ; Mon, 4 Jan 2021 08:16:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4E94720758 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 9B4658D0009; Mon, 4 Jan 2021 03:16:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 965C58D0002; Mon, 4 Jan 2021 03:16:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 853E48D0009; Mon, 4 Jan 2021 03:16:44 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0090.hostedemail.com [216.40.44.90]) by kanga.kvack.org (Postfix) with ESMTP id 6BD508D0002 for ; Mon, 4 Jan 2021 03:16:44 -0500 (EST) Received: from smtpin09.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 075AD3629 for ; Mon, 4 Jan 2021 08:16:44 +0000 (UTC) X-FDA: 77667386328.09.lead86_18105bb274ce Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin09.hostedemail.com (Postfix) with ESMTP id E46CC180AD804 for ; Mon, 4 Jan 2021 08:16:43 +0000 (UTC) X-HE-Tag: lead86_18105bb274ce X-Filterd-Recvd-Size: 5455 Received: from mail-lf1-f45.google.com (mail-lf1-f45.google.com [209.85.167.45]) by imf43.hostedemail.com (Postfix) with ESMTP for ; Mon, 4 Jan 2021 08:16:43 +0000 (UTC) Received: by mail-lf1-f45.google.com with SMTP id a12so62399298lfl.6 for ; Mon, 04 Jan 2021 00:16:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=Vi1rRFIrR0TVZYUd6c99CakyrFl/PE0ICZ3b5gU1L9Y=; b=PYEOG/kfOnNpvAzoOoz4rslFCrSLjX/AkIvQaomeiklVXDLewGBo/8Byu7qeERi8qs Uqye7CUBgm7Cw2gxE0d+Ed9CHeeauHDN+sSznrOn4C6rITOQvt43g3AbtB9lWM69RW6J vJ2qxOvuU0couDJeLyqZjoEsaiEwwPLG0DwPDZ/1upZc8VFCDwedPeXx5DP92nFgzLgo bfLGOTZjeEJSXxv6K2B17QfTex1y4ss/I+ZUz+mpu67bdIE0rAdUPRBfu18RmRoKVs+d 9KiN/ic3xQ2rD1pAgP/momCfeeKQX/bmsPHarhVZ3QPAjidRXLq72Y1QhSiUWCg4s1lW 1ANA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=Vi1rRFIrR0TVZYUd6c99CakyrFl/PE0ICZ3b5gU1L9Y=; b=CtvrswPa7xwS4WaSxVRahSw5apKfb9FpyNhBeh0bEDtll8wNpfc8qZbiFZYrNhVfT6 BVbiLecRFVguY9leVJAh3wZTJrG5PHlRAovFc09ztNUtIimhBtpO5BLrsaQzCF1RPjf/ 2uR9cp2li6UXtLA3Qj1F2SlT11i1L0aHqiqm6yoPvwYdTdfP1qYQT7Lkz40IkqYoL4fw P5LRbL75Sw8ahYvPTu525WhfcunVD+E4elPz/DDp/i+kxk8ZCd2uH7MbmV9ZTxgi+bpT zdp8x47cjRDqIueTLW8/j3ZxHSoxCA5nKBWLhArth2CHNv+5/br3t+dq/9ikr1Xjb7xc jIZQ== X-Gm-Message-State: AOAM533VAYZmaphnx9XjpTtuFLwEAAvT9eE/y57PP6+tPLrh97odKhcC s2RYA63XusMx3n4vCERVpgJQu/6Q1to= X-Google-Smtp-Source: ABdhPJyz+twduNWpTdhFiw6jy6rY2wjf9tCL3ACJo3b2NXMb0Byk3zDTKW9sqhqpgdVu7I8ODGuBmw== X-Received: by 2002:a19:385e:: with SMTP id d30mr34319103lfj.187.1609748201685; Mon, 04 Jan 2021 00:16:41 -0800 (PST) Received: from localhost.localdomain ([2a03:5342:f:6::2]) by smtp.gmail.com with ESMTPSA id 197sm7206963lfe.158.2021.01.04.00.16.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Jan 2021 00:16:40 -0800 (PST) From: Li Xinhai To: linux-mm@kvack.org Cc: akpm@linux-foundation.org, Mike Kravetz , Peter Xu Subject: [PATCH V2] mm/hugetlb.c: fix unnecessary address expansion of pmd sharing Date: Mon, 4 Jan 2021 16:16:31 +0800 Message-Id: <20210104081631.2921415-1-lixinhai.lxh@gmail.com> X-Mailer: git-send-email 2.18.4 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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). Fixes: 75802ca66354 ("mm/hugetlb: fix calculation of adjust_range_if_pmd_sharing_possible") Suggested-by: Mike Kravetz Cc: Mike Kravetz Cc: Peter Xu Signed-off-by: Li Xinhai Reviewed-by: Mike Kravetz --- v2<-v1: Mike found that previous patch didn't fix all possible cases, and proposed new patch to fully fix the existing issue, and also suggested to consider checking the vma's size at the begining of the function. Yes, we can simplify the code a bit wiht less comparison by aligning vma's size first and then do the checking with start,end. Hope this new one is also easier to understand. mm/hugetlb.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index cbf32d2824fd..a41ce2b22275 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -5239,21 +5239,23 @@ static bool vma_shareable(struct vm_area_struct *vma, unsigned long addr) 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); } /*