Received: by 2002:a05:6358:bb9e:b0:b9:5105:a5b4 with SMTP id df30csp2574785rwb; Sun, 4 Sep 2022 19:48:59 -0700 (PDT) X-Google-Smtp-Source: AA6agR5vgMAnTC3XLGZV54rMKaDSdHh0Xkj2GDh74kkeVhU2vQjXkLo84zARjTZ69laimjfqDl/o X-Received: by 2002:a17:90a:640c:b0:1f7:6ecf:33b6 with SMTP id g12-20020a17090a640c00b001f76ecf33b6mr17287747pjj.3.1662346139514; Sun, 04 Sep 2022 19:48:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662346139; cv=none; d=google.com; s=arc-20160816; b=Zkv3h5HKDM6zYkU4yqCevcrNFqLGO82SSUzRtXmNxsFAH+cPjStf/83m61GweYaIdB 55HDmeBcYXN3BVaacH6Vb7uB5IGFzqQjLoRkmzxMfrZE/vnZdQa7wDoQOQHpqk1ogXZm wsuVpcJi8OEbd42lIbCT7NGhe/s8wjEOFKg+1kzGhoX12CeAG5jjPIIiAFV1Wv+9T/Bt uZks6UzqlaRrx69X++mISRyLvcvNuBiZKh9PYe2T2jOMPtCo5gq+dUi2mxEqSUUTr8Em nWSWDtcM7ajMj+dIA+5edhFSSdXtxeKwUhSNyY5uEpSjv6Tp0nSHQXAvH5dSlV6W/jOD TPZQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=JShOeNEPr6qR8aSUwxj/79ZchHGtLxjGXCDtnYSelCQ=; b=VlHS2GfZ/hUL9YiT/jJkkujBS0nhDOPvPti1whWhSAZFWb6gmMOfYzN0B+8F5bevpi P8Q2ST5oVG5jkeizdDpkp3VQJ2uOPSPgE+beynYrV/3BOn5b7zy47hUyP7pTCHeeEzES ySXa5YbnSdV5eUxup1ziFaY73kfttdH91iefwLiCaEdRc37REJ9iVGnU05qhojQtEASl KGvGMz2hRJb+WaIcPkInHDmIS6fHLtKeTKv8+wphSk3Q9UFD+Dv/wZNtxNru3j6yRv/d hJYURMNO79IH2h6oFRX9z4DvrHxElhTmwLzP6d/oPO7PN+pKV8Nli9sO4e2K+ByQSYJW tGng== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j6-20020a633c06000000b0042c7a2d0197si9268523pga.39.2022.09.04.19.48.48; Sun, 04 Sep 2022 19:48:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234507AbiIECcZ (ORCPT + 99 others); Sun, 4 Sep 2022 22:32:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46580 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231820AbiIECcW (ORCPT ); Sun, 4 Sep 2022 22:32:22 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DD1D72A972 for ; Sun, 4 Sep 2022 19:32:20 -0700 (PDT) Received: from canpemm500002.china.huawei.com (unknown [172.30.72.53]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4MLXVb1F5wzWfLT; Mon, 5 Sep 2022 10:27:51 +0800 (CST) Received: from [10.174.177.76] (10.174.177.76) by canpemm500002.china.huawei.com (7.192.104.244) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Mon, 5 Sep 2022 10:32:18 +0800 Subject: Re: [PATCH 7/8] hugetlb: create hugetlb_unmap_file_folio to unmap single file folio To: Mike Kravetz CC: Muchun Song , David Hildenbrand , Michal Hocko , Peter Xu , Naoya Horiguchi , "Aneesh Kumar K . V" , Andrea Arcangeli , "Kirill A . Shutemov" , Davidlohr Bueso , Prakash Sangappa , James Houghton , Mina Almasry , Pasha Tatashin , Axel Rasmussen , Ray Fucillo , Andrew Morton , , References: <20220824175757.20590-1-mike.kravetz@oracle.com> <20220824175757.20590-8-mike.kravetz@oracle.com> <0e31f9da-5953-2f44-1a59-888e3313e919@huawei.com> <304a6d9a-9029-8ee8-7205-d0ef4a5403bd@huawei.com> From: Miaohe Lin Message-ID: <5dc9b069-7268-3123-2891-881977cb42dd@huawei.com> Date: Mon, 5 Sep 2022 10:32:17 +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.177.76] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To canpemm500002.china.huawei.com (7.192.104.244) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-5.2 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2022/9/3 5:35, Mike Kravetz wrote: > On 08/30/22 10:46, Miaohe Lin wrote: >> On 2022/8/30 6:37, Mike Kravetz wrote: >>> On 08/29/22 10:44, Miaohe Lin wrote: >>>> On 2022/8/25 1:57, Mike Kravetz wrote: >>>>> Create the new routine hugetlb_unmap_file_folio that will unmap a single >>>>> file folio. This is refactored code from hugetlb_vmdelete_list. It is >>>>> modified to do locking within the routine itself and check whether the >>>>> page is mapped within a specific vma before unmapping. >>>>> >>>>> This refactoring will be put to use and expanded upon in a subsequent >>>>> patch adding vma specific locking. >>>>> >>>>> Signed-off-by: Mike Kravetz >>>>> --- >>>>> fs/hugetlbfs/inode.c | 123 +++++++++++++++++++++++++++++++++---------- >>>>> 1 file changed, 94 insertions(+), 29 deletions(-) >>>>> >>>>> diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c >>>>> index e83fd31671b3..b93d131b0cb5 100644 >>>>> --- a/fs/hugetlbfs/inode.c >>>>> +++ b/fs/hugetlbfs/inode.c >>>>> @@ -371,6 +371,94 @@ static void hugetlb_delete_from_page_cache(struct page *page) >>>>> delete_from_page_cache(page); >>>>> } >>>>> >>>>> +/* >>>>> + * Called with i_mmap_rwsem held for inode based vma maps. This makes >>>>> + * sure vma (and vm_mm) will not go away. We also hold the hugetlb fault >>>>> + * mutex for the page in the mapping. So, we can not race with page being >>>>> + * faulted into the vma. >>>>> + */ >>>>> +static bool hugetlb_vma_maps_page(struct vm_area_struct *vma, >>>>> + unsigned long addr, struct page *page) >>>>> +{ >>>>> + pte_t *ptep, pte; >>>>> + >>>>> + ptep = huge_pte_offset(vma->vm_mm, addr, >>>>> + huge_page_size(hstate_vma(vma))); >>>>> + >>>>> + if (!ptep) >>>>> + return false; >>>>> + >>>>> + pte = huge_ptep_get(ptep); >>>>> + if (huge_pte_none(pte) || !pte_present(pte)) >>>>> + return false; >>>>> + >>>>> + if (pte_page(pte) == page) >>>>> + return true; >>>> >>>> I'm thinking whether pte entry could change after we check it since huge_pte_lock is not held here. >>>> But I think holding i_mmap_rwsem in writelock mode should give us such a guarantee, e.g. migration >>>> entry is changed back to huge pte entry while holding i_mmap_rwsem in readlock mode. >>>> Or am I miss something? >>> >>> Let me think about this. I do not think it is possible, but you ask good >>> questions. >>> >>> Do note that this is the same locking sequence used at the beginning of the >>> page fault code where the decision to call hugetlb_no_page() is made. >> >> Yes, hugetlb_fault() can tolerate the stale pte entry because pte entry will be re-checked later under the page table lock. >> However if we see a stale pte entry here, the page might be leftover after truncated and thus break truncation? But I'm not >> sure whether this will occur. Maybe the i_mmap_rwsem writelock and hugetlb_fault_mutex can prevent this issue. >> > > I looked at this some more. Just to be clear, we only need to worry > about modifications of pte_page(). Racing with other pte modifications > such as accessed, or protection changes is acceptable. > > Of course, the fault mutex prevents faults from happening. i_mmap_rwsem > protects against unmap and truncation operations as well as migration as > you noted above. I believe the only other place where we update pte_page() > is when copying page table such as during fork. However, with commit > bcd51a3c679d "Lazy page table copies in fork()" we are going to skip > copying for files and rely on page faults to populate the tables. > > I believe we are safe from races with just the fault mutex and i_mmap_rwsem. I believe your analysis is right. Thanks for your clarifying. Thanks, Miaohe Lin