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 C6C39C001DF for ; Wed, 2 Aug 2023 11:14:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 646D3280154; Wed, 2 Aug 2023 07:14:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5CFE4280143; Wed, 2 Aug 2023 07:14:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4717A280154; Wed, 2 Aug 2023 07:14:21 -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 3398E280143 for ; Wed, 2 Aug 2023 07:14:21 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 02F1BC0CF9 for ; Wed, 2 Aug 2023 11:14:20 +0000 (UTC) X-FDA: 81078905922.19.24A8133 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf07.hostedemail.com (Postfix) with ESMTP id E5D3B40009 for ; Wed, 2 Aug 2023 11:14:18 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf07.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=1690974859; 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=4NjS9zlOimUR3pIofUB00GhuuLwAZcOplIDMzdbutQo=; b=ExRvhZE9BsPF5FLgx1xqjWBKJEXYtCyHf4vvGnDqdKvkcwP5MZWSmmdiFzvvo9dzEfdaki ImAqtE8ECCEH5FP6iKHaS9QpJBFwDlUAkkD1PzGFWxhmrJ037G0Rn5LWI1CYlsaquIDaz5 Dg59NBxxPcjNED7EbJZdmJnDRweN1qw= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf07.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=1690974859; a=rsa-sha256; cv=none; b=xvO24krDUlEgkuzC7NwgkBhJm6IR34WFH3jGxcYBzaHdvWIlmq2dS/SXzeLWHWaVq3x2K1 c1Lh9hXFZDDBD9pszkqFVGgXrjK294Gzy+JUdmasS3bqqCXilzszgPdYCOjoeb5G4vtdYU yg2lM4CexoVXoceaGZWKlQTuoxmeKis= 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 CBF16113E; Wed, 2 Aug 2023 04:15:00 -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 2EA823F6C4; Wed, 2 Aug 2023 04:14:16 -0700 (PDT) Message-ID: <55c9e3f7-099d-6f57-32da-1f318a9688a0@arm.com> Date: Wed, 2 Aug 2023 12:14:14 +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() 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> From: Ryan Roberts In-Reply-To: <20230728070929.2487065-2-fengwei.yin@intel.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: E5D3B40009 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: zgqijs4737oyd1xup9tahb75mgrtfa5p X-HE-Tag: 1690974858-595698 X-HE-Meta: U2FsdGVkX1/ngF/SCxSM6J1UAdem0FIbxa9Or3E0U2ZfsQy8jYSnujoH7Nf8T6cJ52+FwjeLmb3LALOZ4LCWakEYX8g0lnjPcBTxLyPQmxlgszQm/B10TyxD/XFuGZRH9E2B5zB0SYwLdjy4WoydZJyj0rXsa3XTECAu+H3WhlLBKWOOPznMpvz6XbnVEXbkWAXPhirIkLp3jXi2vMyMVsyeuUUnBB/id5A63yvxDbeL0eqVnkFXXkshP5t1+NaLB0H/BOFe/kOfW6k/5bPArKImVl/+jMGhTWha2e1vRIobnW+EPLeiM+8SvpGCA4ijPkZxv32eI9hcTqk6A2eyLC3EaETi4cH8xYWSTrDdAa2o6yV6dcUHvDx+A4gO7vnVfQd3wP3UkGweHPfpZ6G/6ETrjgrN3GLPkvwJyd7cQIPfMF2Dyu+vKvYotgunya5Kgj/V3xlj6iH1fnIYZbqB1LAe3Is6ChxlZcWm2KEJACI4oVBAhSHtKSfF5vMRaAJ49NJAj6AB9KycalJtgZKpyBlriNuxIJl2MBDcYaJVM9/08IuuaMhmP5tTZNBR9CVb8G5i5Cyhu+n/olhzhIYpJ7jeectDWYmLzgDv7sCAX0vENunegmZEnVXicsJf45qpL9JDVPZhTveqc/0vX95783DXjXn2GS3ON9rMQCJsE/5nrg4YXtbbjXOCz4YulsAQpiA6ORl+iTENmichunZEMj0F9BXu6gN9CE+hbB8fw441bBScBXcneeUhuAxeblQ6vjcZE4Ot5uJfrWEgtDIUujobiMZMsjZoeabsAPvu3Oui9Zebr9aCvzpV+rzb6dRsTZ1SYzpHUVllAB0Z+WcpRxwf2n6VVJOlMjOFU+V2xP5Stsu7aJM03yFtsJm9tSAdaYdcCVxiPsaqNWMRgY8+NATQuwm/zDZESoQw5jHbv8/DxJVhh6vuez8XTmUDLDmwwkXtgXc4FKywkL4hhvu dLqnGkea Tb6b3mwyScR7OZHA5Wqftbw/u++6yS+x9ULkn6J/5qF47kRtvmxpHCa4nI708/yY0ByTkHCLyOgah70vYtI6HdYKRsT/N5FBIFnU1uCGpb1O+RXNfOLmc3oiNiNiRE8pHLc2RoU/3zcg0u1yStY948aN1ziUMQ6xi/6mstOuvPVGmh07BHjT2SkIl0g== 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 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()? > +{ > + 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,