From mboxrd@z Thu Jan 1 00:00:00 1970 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.4 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,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 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 9E90AC433DB for ; Sat, 2 Jan 2021 11:56:57 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 1B6B42151B for ; Sat, 2 Jan 2021 11:56:56 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1B6B42151B 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 020AC6B013B; Sat, 2 Jan 2021 06:56:56 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id F12336B013C; Sat, 2 Jan 2021 06:56:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E01186B013D; Sat, 2 Jan 2021 06:56:55 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0170.hostedemail.com [216.40.44.170]) by kanga.kvack.org (Postfix) with ESMTP id C771F6B013B for ; Sat, 2 Jan 2021 06:56:55 -0500 (EST) Received: from smtpin11.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 7B6C68248047 for ; Sat, 2 Jan 2021 11:56:55 +0000 (UTC) X-FDA: 77660683590.11.stamp84_4607912274be Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin11.hostedemail.com (Postfix) with ESMTP id 55B66180F8B81 for ; Sat, 2 Jan 2021 11:56:55 +0000 (UTC) X-HE-Tag: stamp84_4607912274be X-Filterd-Recvd-Size: 6566 Received: from mail-lf1-f43.google.com (mail-lf1-f43.google.com [209.85.167.43]) by imf47.hostedemail.com (Postfix) with ESMTP for ; Sat, 2 Jan 2021 11:56:54 +0000 (UTC) Received: by mail-lf1-f43.google.com with SMTP id 23so52963316lfg.10 for ; Sat, 02 Jan 2021 03:56:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=QAl4U1hRKq3fdtzxE4YSK4wNWMjMtj828BWzOoOEJW0=; b=FXhOOZfoZuy4STs+qIfDeDvGfz1FcRSjzVTy4We/qcQJR8bTJQQf67KyeOpEUUFhSy jisWhm1+ke27KykTZ41H0tSwAHawohRZseQsOcIZU4SZD+diMcLH3LxB3oQSSzfrmPHi WTL4bKIpJvyPNVVUpKNeHM50m8tJNnqnvNUg+7kMBb+gcMidXMBAZIkUx+SkKEu5g5XB OTlBowZj3zCden/VcE0JjlKUqhSezfRv8fxAVzjDy/6A4yR01oGTO+k9s5I6hQnuBZa7 BwlJMTiS3hZDd25ocuRY++l2YUKCF7Fmd2q7tyXwjPFwkYVwaXfXFKkNhHT15sl7FO82 x56g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=QAl4U1hRKq3fdtzxE4YSK4wNWMjMtj828BWzOoOEJW0=; b=S0BgdQBTP5JMu1xM4cK557+OUTd3RVMWLt0vAw1dU7xRekKlf+PjcOD29OvcmtN6NP hIpAIQ34rBUyKpmfIAOIzdss+rWOWsx/Xbg33BOP12WzDuuyVUH6j7ZZ2m+vGC/YyL7P /UB2ER4NytrLmr196GHIuLYUOZrNDnguI9DsDyOO34mjhh/rXTZZU/VTpbfCd/prNDcS Mp/WrJ9E7IKcb4WoRZPUvDPB9PjGsGh0AIbBwJZinHtSsJ3nz2A1KyohX4pufEtp6WJv k0I6MfyCdcH7e6Xn/NRRnj7mVIm0Ckv8/+gaqWdrjN1qSPAN5P4V1zA/BSP7BB9mpzOU rt5A== X-Gm-Message-State: AOAM530/eXbfysy0sZBmnS2P75ysQWhL04x7sI03i/IfQA50/n4qUpYL nbrCkBLtkUKZCNA1TmRKOVg= X-Google-Smtp-Source: ABdhPJxKlJxexYM2vEQ44RdwtmEFXkDCffyOeIRTSrjkwkRTmhKUhv7ORn414Tah3Autzgc0yjUiTQ== X-Received: by 2002:a05:6512:6c6:: with SMTP id u6mr26569513lff.174.1609588613369; Sat, 02 Jan 2021 03:56:53 -0800 (PST) Received: from localhost.localdomain ([2a03:5342:f:6::2]) by smtp.gmail.com with ESMTPSA id g17sm6701276lfh.167.2021.01.02.03.56.51 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 02 Jan 2021 03:56:52 -0800 (PST) Subject: Re: [PATCH] mm/hugetlb.c: fix unnecessary address expansion of pmd sharing To: Mike Kravetz , linux-mm@kvack.org Cc: akpm@linux-foundation.org, Peter Xu References: <20201229042125.2663029-1-lixinhai.lxh@gmail.com> <2b6534f5-2ffa-6325-6af1-8c067ba9c0bc@oracle.com> <2d8a7726-b7fb-3dc8-e7bc-54a67d28db24@oracle.com> From: Li Xinhai Message-ID: <2a95c320-4408-2603-f266-8e111a2f1620@gmail.com> Date: Sat, 2 Jan 2021 19:56:42 +0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 MIME-Version: 1.0 In-Reply-To: <2d8a7726-b7fb-3dc8-e7bc-54a67d28db24@oracle.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit 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: On 1/1/21 1:56 AM, Mike Kravetz wrote: > On 12/29/20 1:20 PM, Mike Kravetz wrote: >> On 12/28/20 8:21 PM, Li Xinhai wrote: >>> 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, if pud aligned *start across vm_start, then we know the >>> *start and vm_start are in same pud_index, and vm_start is not pud >>> aligned, so don't adjust *start. Same logic applied to *end. >>> >>> Fixes: commit 75802ca66354 ("mm/hugetlb: fix calculation of adjust_range_if_pmd_sharing_possible") >>> Cc: Mike Kravetz >>> Cc: Peter Xu >>> Signed-off-by: Li Xinhai >> >> Thank you. That does indeed fix an issue in the current code. >> >> Reviewed-by: Mike Kravetz > > Upon further thought, this patch also expands the passed range when not > necessary. Consider the example (start, end) = (1G-6M, 1G-4M), and > (vm_start, vm_end) = (1G, 1G-2M). This patch would adjust the range to > (1G, 1G-4M). However, no adjustment should be performed as no sharing > is possible. > correct, my previous patch did not fully fix the issue. Above example maybe typo for vm_start, vm_end. The issue didn't fixed by my patch would be with another example, (vm_start, vm_end) = (1G-8M, 1G+2M), (start, end) = (1G-6M, 1G-4M), end should not be adjusted to 1G, although after adjust it still below vm_end. > Below is proposed code to address the issue. I'm not sending a formal > patch yet as I would like comments on the code first. It is not a critical > issue and any fix can wait a bit. > > diff --git a/mm/hugetlb.c b/mm/hugetlb.c > index 7e89f31d7ef8..41ccec617f74 100644 > --- a/mm/hugetlb.c > +++ b/mm/hugetlb.c > @@ -5264,16 +5264,19 @@ void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma, > if (!(vma->vm_flags & VM_MAYSHARE)) > 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); > - > /* > - * Intersect the range with the vma range, since pmd sharing won't be > - * across vma after all > + * Check if start and end are within a PUD aligned range of the > + * vma. If they are, then adjust to PUD alignment. > */ > - *start = max(vma->vm_start, a_start); > - *end = min(vma->vm_end, a_end); > + a_start = ALIGN_DOWN(*start, PUD_SIZE); > + a_end = ALIGN(*start, PUD_SIZE); > + if (range_in_vma(vma, a_start, a_end)) > + *start = a_start; > + > + a_start = ALIGN_DOWN(*end, PUD_SIZE); > + a_end = ALIGN(*end, PUD_SIZE); > + if (range_in_vma(vma, a_start, a_end)) > + *end = a_end; > } > > /* > Now, this fully fixed the issue. One thing to be sure is that the (start, end) as input parameter must already within vma's range, although the range_in_vma test() can cover the out of range cases. Reviewed-by: Li Xinhai