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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 03065ECAAD2 for ; Tue, 30 Aug 2022 02:46:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7A03D940007; Mon, 29 Aug 2022 22:46:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 750196B0074; Mon, 29 Aug 2022 22:46:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 61734940007; Mon, 29 Aug 2022 22:46:51 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 5393D6B0073 for ; Mon, 29 Aug 2022 22:46:51 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 2E71B1C5BFC for ; Tue, 30 Aug 2022 02:46:51 +0000 (UTC) X-FDA: 79854721422.27.44543C3 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by imf26.hostedemail.com (Postfix) with ESMTP id 12A0714000B for ; Tue, 30 Aug 2022 02:46:49 +0000 (UTC) Received: from canpemm500002.china.huawei.com (unknown [172.30.72.55]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4MGs713TSyz1N7by; Tue, 30 Aug 2022 10:43:09 +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; Tue, 30 Aug 2022 10:46:46 +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> From: Miaohe Lin Message-ID: <304a6d9a-9029-8ee8-7205-d0ef4a5403bd@huawei.com> Date: Tue, 30 Aug 2022 10:46:45 +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: dggems703-chm.china.huawei.com (10.3.19.180) To canpemm500002.china.huawei.com (7.192.104.244) X-CFilter-Loop: Reflected ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661827610; a=rsa-sha256; cv=none; b=AcSEaxvsCC1q6gPiZcZlTHsHcRv3gWFmsDADxWdHtUOAlKHr6JxoWLX5xLOu43C0OE4TgM AG5exTdfuGPSdJgvtXnoTnW2a+6LuyjD4SdYCj4VIe/IFxldI3e8DqWKb6Bfic+cQjZiAu Qh53yAxQod6cTzgkDtb5sZw0c6kl0YA= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf26.hostedemail.com: domain of linmiaohe@huawei.com designates 45.249.212.255 as permitted sender) smtp.mailfrom=linmiaohe@huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661827610; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Cts7D/elKhA16g/21fh3OtPWf5ICiSJ36on5WZUndro=; b=Ru0NUbJgXFnQAlaWk/pbqv726wrTbsMDh1GjDExLYjBHMVgpxvC9Hs+5rbBVOvqJezWo6l GE70Pzwn4AZ/jRSo3Imzi3gg0BRawrN0PvOgmSdgseZEHsm4fsOyb0bJm9Lc5VH97HEsro tWbTiS/SK9klXY1+XhsQ1Tq9dIoGRkQ= X-Stat-Signature: orpmesynp9accek8zt7q7sg3645sssse X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 12A0714000B Authentication-Results: imf26.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf26.hostedemail.com: domain of linmiaohe@huawei.com designates 45.249.212.255 as permitted sender) smtp.mailfrom=linmiaohe@huawei.com X-Rspam-User: X-HE-Tag: 1661827609-78808 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 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. Thanks, Miaohe Lin > >> >>> + >>> + return false; >>> +} >>> + >>> +/* >>> + * Can vma_offset_start/vma_offset_end overflow on 32-bit arches? >>> + * No, because the interval tree returns us only those vmas >>> + * which overlap the truncated area starting at pgoff, >>> + * and no vma on a 32-bit arch can span beyond the 4GB. >>> + */ >>> +static unsigned long vma_offset_start(struct vm_area_struct *vma, pgoff_t start) >>> +{ >>> + if (vma->vm_pgoff < start) >>> + return (start - vma->vm_pgoff) << PAGE_SHIFT; >>> + else >>> + return 0; >>> +} >>> + >>> +static unsigned long vma_offset_end(struct vm_area_struct *vma, pgoff_t end) >>> +{ >>> + unsigned long t_end; >>> + >>> + if (!end) >>> + return vma->vm_end; >>> + >>> + t_end = ((end - vma->vm_pgoff) << PAGE_SHIFT) + vma->vm_start; >>> + if (t_end > vma->vm_end) >>> + t_end = vma->vm_end; >>> + return t_end; >>> +} >>> + >>> +/* >>> + * Called with hugetlb fault mutex held. Therefore, no more mappings to >>> + * this folio can be created while executing the routine. >>> + */ >>> +static void hugetlb_unmap_file_folio(struct hstate *h, >>> + struct address_space *mapping, >>> + struct folio *folio, pgoff_t index) >>> +{ >>> + struct rb_root_cached *root = &mapping->i_mmap; >>> + struct page *page = &folio->page; >>> + struct vm_area_struct *vma; >>> + unsigned long v_start; >>> + unsigned long v_end; >>> + pgoff_t start, end; >>> + >>> + start = index * pages_per_huge_page(h); >>> + end = ((index + 1) * pages_per_huge_page(h)); >> >> It seems the outer parentheses is unneeded? > > Correct. Thanks. >