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.3 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_SANE_1 autolearn=unavailable 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 3EE8AC433ED for ; Wed, 7 Apr 2021 03:09:32 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id D26A361158 for ; Wed, 7 Apr 2021 03:09:31 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D26A361158 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=huawei.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 5EF766B007E; Tue, 6 Apr 2021 23:09:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 59FB76B0082; Tue, 6 Apr 2021 23:09:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 441F76B0083; Tue, 6 Apr 2021 23:09:31 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0153.hostedemail.com [216.40.44.153]) by kanga.kvack.org (Postfix) with ESMTP id 277286B007E for ; Tue, 6 Apr 2021 23:09:31 -0400 (EDT) Received: from smtpin22.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id D074C8249980 for ; Wed, 7 Apr 2021 03:09:30 +0000 (UTC) X-FDA: 78004090500.22.12EF622 Received: from szxga04-in.huawei.com (szxga04-in.huawei.com [45.249.212.190]) by imf17.hostedemail.com (Postfix) with ESMTP id 1534A40002D2 for ; Wed, 7 Apr 2021 03:09:27 +0000 (UTC) Received: from DGGEMS407-HUB.china.huawei.com (unknown [172.30.72.59]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4FFTpC23Bpz17QdR; Wed, 7 Apr 2021 11:07:15 +0800 (CST) Received: from [10.174.179.9] (10.174.179.9) by DGGEMS407-HUB.china.huawei.com (10.3.19.207) with Microsoft SMTP Server id 14.3.498.0; Wed, 7 Apr 2021 11:09:24 +0800 Subject: Re: [PATCH 2/4] mm/hugeltb: simplify the return code of __vma_reservation_common() To: Mike Kravetz , CC: , , , References: <20210402093249.25137-1-linmiaohe@huawei.com> <20210402093249.25137-3-linmiaohe@huawei.com> <40114ff5-ba3d-ca66-3338-25db80a015da@huawei.com> From: Miaohe Lin Message-ID: <1926967f-3805-2baf-6b86-24039c6513ca@huawei.com> Date: Wed, 7 Apr 2021 11:09:25 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [10.174.179.9] X-CFilter-Loop: Reflected X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 1534A40002D2 X-Stat-Signature: tpyurqem1jaoxj7dnpkhzhsdgm7f74gb Received-SPF: none (huawei.com>: No applicable sender policy available) receiver=imf17; identity=mailfrom; envelope-from=""; helo=szxga04-in.huawei.com; client-ip=45.249.212.190 X-HE-DKIM-Result: none/none X-HE-Tag: 1617764967-323091 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 2021/4/7 10:37, Mike Kravetz wrote: > On 4/6/21 7:05 PM, Miaohe Lin wrote: >> Hi: >> On 2021/4/7 8:53, Mike Kravetz wrote: >>> On 4/2/21 2:32 AM, Miaohe Lin wrote: >>>> It's guaranteed that the vma is associated with a resv_map, i.e. either >>>> VM_MAYSHARE or HPAGE_RESV_OWNER, when the code reaches here or we would >>>> have returned via !resv check above. So ret must be less than 0 in the >>>> 'else' case. Simplify the return code to make this clear. >>> >>> I believe we still neeed that ternary operator in the return statement. >>> Why? >>> >>> There are two basic types of mappings to be concerned with: >>> shared and private. >>> For private mappings, a task can 'own' the mapping as indicated by >>> HPAGE_RESV_OWNER. Or, it may not own the mapping. The most common way >>> to create a non-owner private mapping is to have a task with a private >>> mapping fork. The parent process will have HPAGE_RESV_OWNER set, the >>> child process will not. The idea is that since the child has a COW copy >>> of the mapping it should not consume reservations made by the parent. >> >> The child process will not have HPAGE_RESV_OWNER set because at fork time, we do: >> /* >> * Clear hugetlb-related page reserves for children. This only >> * affects MAP_PRIVATE mappings. Faults generated by the child >> * are not guaranteed to succeed, even if read-only >> */ >> if (is_vm_hugetlb_page(tmp)) >> reset_vma_resv_huge_pages(tmp); >> i.e. we have vma->vm_private_data = (void *)0; for child process and vma_resv_map() will >> return NULL in this case. >> Or am I missed something? >> >>> Only the parent (HPAGE_RESV_OWNER) is allowed to consume the >>> reservations. >>> Hope that makens sense? >>> >>>> >>>> Signed-off-by: Miaohe Lin >>>> --- >>>> mm/hugetlb.c | 2 +- >>>> 1 file changed, 1 insertion(+), 1 deletion(-) >>>> >>>> diff --git a/mm/hugetlb.c b/mm/hugetlb.c >>>> index a03a50b7c410..b7864abded3d 100644 >>>> --- a/mm/hugetlb.c >>>> +++ b/mm/hugetlb.c >>>> @@ -2183,7 +2183,7 @@ static long __vma_reservation_common(struct hstate *h, >>>> return 1; >>>> } >>>> else >>> >>> This else also handles the case !HPAGE_RESV_OWNER. In this case, we >> >> IMO, for the case !HPAGE_RESV_OWNER, we won't reach here. What do you think? >> > > I think you are correct. > > However, if this is true we should be able to simply the code even > further. There is no need to check for HPAGE_RESV_OWNER because we know > it must be set. Correct? If so, the code could look something like: > > if (vma->vm_flags & VM_MAYSHARE) > return ret; > > /* We know private mapping with HPAGE_RESV_OWNER */ > * ... * > * Add that existing comment */ > > if (ret > 0) > return 0; > if (ret == 0) > return 1; > return ret; > Many thanks for good suggestion! What do you mean is this ? diff --git a/mm/hugetlb.c b/mm/hugetlb.c index a03a50b7c410..9b4c05699a90 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -2163,27 +2163,26 @@ static long __vma_reservation_common(struct hstate *h, if (vma->vm_flags & VM_MAYSHARE) return ret; - else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER) && ret >= 0) { - /* - * In most cases, reserves always exist for private mappings. - * However, a file associated with mapping could have been - * hole punched or truncated after reserves were consumed. - * As subsequent fault on such a range will not use reserves. - * Subtle - The reserve map for private mappings has the - * opposite meaning than that of shared mappings. If NO - * entry is in the reserve map, it means a reservation exists. - * If an entry exists in the reserve map, it means the - * reservation has already been consumed. As a result, the - * return value of this routine is the opposite of the - * value returned from reserve map manipulation routines above. - */ - if (ret) - return 0; - else - return 1; - } - else - return ret < 0 ? ret : 0; + /* + * We know private mapping must have HPAGE_RESV_OWNER set. + * + * In most cases, reserves always exist for private mappings. + * However, a file associated with mapping could have been + * hole punched or truncated after reserves were consumed. + * As subsequent fault on such a range will not use reserves. + * Subtle - The reserve map for private mappings has the + * opposite meaning than that of shared mappings. If NO + * entry is in the reserve map, it means a reservation exists. + * If an entry exists in the reserve map, it means the + * reservation has already been consumed. As a result, the + * return value of this routine is the opposite of the + * value returned from reserve map manipulation routines above. + */ + if (ret > 0) + return 0; + if (ret == 0) + return 1; + return ret; }