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 1131FC001E0 for ; Wed, 2 Aug 2023 11:35:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9FF5528015C; Wed, 2 Aug 2023 07:35:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9AF94280143; Wed, 2 Aug 2023 07:35:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8C61A28015C; Wed, 2 Aug 2023 07:35:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 7F066280143 for ; Wed, 2 Aug 2023 07:35:11 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 5476C1C980B for ; Wed, 2 Aug 2023 11:35:11 +0000 (UTC) X-FDA: 81078958422.14.A1ABA01 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf03.hostedemail.com (Postfix) with ESMTP id 7622120006 for ; Wed, 2 Aug 2023 11:35:09 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf03.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1690976109; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=WJoJu5LUkZNURrO0wibzpFfaV3dlcywrzu5qnkiJnuI=; b=OZlLO+J57YOelfZOTnf93YfWNr6lCc3XUkxhRh1en5cMnp+RfpOnaKT/ZYY8ZVkpI+U4Tp kFBkZTJ567ozGT/JJm3/ogyRE5hxfHScz7q03LHsmLS6r1U0L+cHCQxUJkjA+36GnrPY5n nygqlLYIvMTg+fTqzu+cNFDgJKDl7Yw= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf03.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690976109; a=rsa-sha256; cv=none; b=iFcNQklwnILjBYfy8YT+I12VwNQLBtBXkJ0UgAXReoaF8uNJBAtLm/4t47XEmI8Q8Jd34H StWuE7WYLN3pVsUo+KRs1Z1LZ5cMnyzfOqwdTASZi2/FKARA9/qTUAo1V0dcNBHlQLlngi 3H2eYzQDY7JvhIv2omf2F8EAMhpvOfk= Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 6B87E113E; Wed, 2 Aug 2023 04:35:51 -0700 (PDT) Received: from [10.57.77.90] (unknown [10.57.77.90]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 504F13F6C4; Wed, 2 Aug 2023 04:35:06 -0700 (PDT) Message-ID: <9bba9369-e3f5-53da-bf8f-8ab887d3c3ae@arm.com> Date: Wed, 2 Aug 2023 12:35:04 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.13.0 Subject: Re: [PATCH 1/3] mm: add functions folio_in_range() and folio_within_vma() From: Ryan Roberts To: Yin Fengwei , linux-mm@kvack.org, linux-kernel@vger.kernel.org, akpm@linux-foundation.org, yuzhao@google.com, willy@infradead.org, david@redhat.com, shy828301@gmail.com, hughd@google.com References: <20230728070929.2487065-1-fengwei.yin@intel.com> <20230728070929.2487065-2-fengwei.yin@intel.com> <55c9e3f7-099d-6f57-32da-1f318a9688a0@arm.com> In-Reply-To: <55c9e3f7-099d-6f57-32da-1f318a9688a0@arm.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: 7622120006 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: ghiyi6zfg46opx6d9hi1r7he79wkyaig X-HE-Tag: 1690976109-59437 X-HE-Meta: U2FsdGVkX186ux+IMtkbaAsdPm9A28aTdOLUBesVvu+NXqXY730qT85v+xkEezYwSjHi+NEkBK2FY5zX2vJfQGCOtsONSlXB/T/+z2vAE8rSOsNHYvOxI7vN40iE3YBv3HkjenXdtHKRU8KGPbb0KDwc6gxdaTfSupKIgkKvDNEXLgWVzEgsd8kbf2MZn8cyDqqeRgbVb+n2oW5k1JRf1zvQOs/Wf6pE0C82hcDva324yqPWmhG5CxfCTSIE0L6ItNVTNy1vSL2pIqCqJnyMg73DxZ45oso/fWA19YYL8VYwoGbXblqsDgpicA0GB+VpJkAWaNKSJmdbR+Htat5BrIx7h+zR63rx91Dchy+bpbDzMtMe/vZ1O1YRrKB56oOK3fF7o8d1FZYPwV8B9txKqVTnEec+4obYKgV6uRU3qxSODRabAKLIH4FiSpQSss4ODufzxHaa7RrxeXNDK65IGo7ZRD/IDNSlh6M/66qkchkiOo8bYQIVn9OInxj6y+1lI//4TsZLNxjmpxrYMWnlc4WV4Y2ZQWofj4XUm0S4WqmAATfA98RDUszEC8RQw+iIZW2Yg52M/CplZfHJYFUnnNunH18Yo6KI/CkxHAmohDtF7v6uIL5nV/PNBj9tBXgsNR6g2uFgOTj8I3ikq+iKxTu5NE3Mg9wzepixDBq+nzo32+JLQdxkhczLmwgDGKi+b04zbe0RQ+wGwYZSBpALIPyzzsNOKsIEE0P97zXlUb+O9uTcslr8M4BqjzHNIg2PrNVcVUWGkx533shB/H0TEMNrwZOx59nj9gjFiDS3ZmqvFeiCYkAfS6MRY4Os0V9hOKjIslmOYqK71uLAhNa4rSUgIxGy0JiheS4qDdF4ZK4qLn57IxEYtjc2O5xkpKmLzBmrMGrLBE54sh/RjdArlZswXTPqphSsfwbdvfg4dxyDRI50PvuXxbaQqTruXrVMUphQ2mKa/gXTiFcZtu/ JQ7tjV1f VLJP6B6EVl3QKAhiMJX5BWEgCKz/d17LnUfF7oWRIpZRmJFmOE9b0Uz/PZ9dEKVgUm+IQByGk8I2kMBAEB7pAsMx2PoghgbgQEGLkNQNi5gvjn3AL5DaBvawcznx0HZ6gDH2AqFARRc9YXM0G9YKkOoU79pDfPKoQ2CPJpILnGwhVzTXaH2pg+V388s3ao7klSOw2vr96Ax/I2J5cTFPxunWfnrKOPd4kp9b2pcLD4284LxPPgUmtThHYlz1HlkWxS2C/j1X61qq9rF8aEat2NUAh4+8lThG3CXLkWYRjaZIXCQM= 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 02/08/2023 12:14, Ryan Roberts wrote: > On 28/07/2023 08:09, Yin Fengwei wrote: >> It will be used to check whether the folio is mapped to specific >> VMA and whether the mapping address of folio is in the range. >> >> Also a helper function folio_within_vma() to check whether folio >> is in the range of vma based on folio_in_range(). >> >> Signed-off-by: Yin Fengwei >> --- >> mm/internal.h | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++ >> 1 file changed, 69 insertions(+) >> >> diff --git a/mm/internal.h b/mm/internal.h >> index 5a03bc4782a2..63de32154a48 100644 >> --- a/mm/internal.h >> +++ b/mm/internal.h >> @@ -585,6 +585,75 @@ extern long faultin_vma_page_range(struct vm_area_struct *vma, >> bool write, int *locked); >> extern bool mlock_future_ok(struct mm_struct *mm, unsigned long flags, >> unsigned long bytes); >> + >> +/* >> + * Check whether the folio is in specific range >> + * >> + * First, check whether the folio is in the range of vma. >> + * Then, check whether the folio is mapped to the range of [start, end]. >> + * In the end, check whether the folio is fully mapped to the range. >> + * >> + * @pte page table pointer will be checked whether the large folio >> + * is fully mapped to. Currently, if mremap in the middle of >> + * large folio, the large folio could be mapped to to different >> + * VMA and address check can't identify this situation. >> + */ >> +static inline bool >> +folio_in_range(struct folio *folio, struct vm_area_struct *vma, >> + unsigned long start, unsigned long end, pte_t *pte) > > This api seems a bit redundant to me. Wouldn't it be better to remove the vma > parameter and instead fix up the start/end addresses in folio_within_vma()? I have created a function as part of my "pte batch-zap" patch set [1], which counts the number of contiguously mapped pages of a folio (folio_nr_pages_cont_mapped()). I wonder if actually this should be the primitive, which can be shared for more cases. Then your folio_within_vma() function could just compare the nr_pages to folio_nr_pages() to decide if the folio is fully and contiguously mapped in the VMA. I also wonder if you should change the name of folio_within_vma() to something like folio_test_cont_in_vma() to disambiguate from the case where the folio may be fully mapped with a discontiguity (although perhaps that's not possible because a mremap would result in distinct vmas... would a new mmap in the hole cause a merge of all 3?). [1] https://lore.kernel.org/linux-mm/20230727141837.3386072-4-ryan.roberts@arm.com/ > >> +{ >> + pte_t ptent; >> + unsigned long i, nr = folio_nr_pages(folio); >> + pgoff_t pgoff, addr; >> + unsigned long vma_pglen = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT; >> + >> + VM_WARN_ON_FOLIO(folio_test_ksm(folio), folio); >> + >> + if (start < vma->vm_start) >> + start = vma->vm_start; >> + if (end > vma->vm_end) >> + end = vma->vm_end; >> + >> + pgoff = folio_pgoff(folio); >> + /* if folio start address is not in vma range */ >> + if (pgoff < vma->vm_pgoff || pgoff > vma->vm_pgoff + vma_pglen) >> + return false; >> + >> + addr = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT); >> + if (addr < start || end - addr < folio_size(folio)) >> + return false; >> + >> + /* not necessary to check pte for none large folio */ >> + if (!folio_test_large(folio)) >> + return true; >> + >> + if (!pte) >> + return false; >> + >> + /* check whether parameter pte is associated with folio */ >> + ptent = ptep_get(pte); >> + if (pte_none(ptent) || !pte_present(ptent) || >> + pte_pfn(ptent) - folio_pfn(folio) >= nr) >> + return false; >> + >> + pte -= pte_pfn(ptent) - folio_pfn(folio); >> + for (i = 0; i < nr; i++, pte++) { >> + ptent = ptep_get(pte); >> + >> + if (pte_none(ptent) || !pte_present(ptent) || >> + pte_pfn(ptent) - folio_pfn(folio) >= nr) >> + return false; >> + } > > I don't think I see anything to ensure you don't wander off the end (or start) > of the pgtable? If the folio is mremapped so that it straddles multiple tables > (or is bigger than a single table?) then I think pte can become invalid? Perhaps > you intended start/end to always be within the same pgtable, but that is not > guarranteed in the case that folio_within_vma() is making the call. > > Also I want to check that this function is definitely always called under the > PTL for the table that pte belongs to? > >> + >> + return true; >> +} >> + >> +static inline bool >> +folio_within_vma(struct folio *folio, struct vm_area_struct *vma, pte_t *pte) >> +{ >> + return folio_in_range(folio, vma, vma->vm_start, vma->vm_end, pte); >> +} >> + >> /* >> * mlock_vma_folio() and munlock_vma_folio(): >> * should be called with vma's mmap_lock held for read or write, >