Received: by 2002:a05:6358:700f:b0:131:369:b2a3 with SMTP id 15csp817475rwo; Wed, 2 Aug 2023 04:51:54 -0700 (PDT) X-Google-Smtp-Source: APBJJlF4YQ9eIYK4RGeHtvi1brubvAIW1ZwQa2AB00A3cyQSdBzOoFVjR8XERf3bMyNOTLP1tEKZ X-Received: by 2002:a17:902:cec6:b0:1bb:c224:908b with SMTP id d6-20020a170902cec600b001bbc224908bmr15498680plg.61.1690977114019; Wed, 02 Aug 2023 04:51:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690977114; cv=none; d=google.com; s=arc-20160816; b=yCtkcDEzQMZw8saDjze6hTr9uva2cuLtRAZiOxefnDp/5boyppNw/NegFus5reiivY 6AlLv33Z+TJekHnKrg9igbSfolA8y6QXi+5wkYdOHhz+P8zjelAis7L82Plf0cMeTvna Ow0VEmVmUoguR1pxgG12dnr6VdtVoe8w7xa4qlTYmID7GWEiF37B2837qSrZ3L9bPm5c gYvHf9Ijr72zKcsUNQzEN7U6YY6X2QR8+9ExJkWPHVTljD649Cc2mvC7CUBEwjJnrFqB ei9YrPSHOZctfuSt0GS5pdtrX0Bu6bFJ8dywq8SvQ7/UjXjQA9Zl504/G9XMAlB0ugWZ 35CQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:to:subject:user-agent:mime-version:date:message-id; bh=4NjS9zlOimUR3pIofUB00GhuuLwAZcOplIDMzdbutQo=; fh=dZa5nc/EVdjld5zBtRcPSm7LvyH4hv0femVoGoejnQM=; b=EfklxEs/Z+sKuNRGyfdIER9afGkVmxfrirdlNJloTFu2s4L+UzWhgVGeGBprxrP79F VAqRGHVZPS2jMVZjrFz++5oeOBdCSHtY+jzM1rbKhuQBc0a4lwkiJMHdDlVbkJNXwOGA QiVgrizowSpMfSDTJ3AffRDT9vpB6QfYqYC6Sk7dZxgzMkIs6UXoXb0hlN4j0J89VBWs THTjn+uXRcOYh2LVk0h5D9JSNzVCtfLEr3AXJAw+Wx/uZxmDqTA/NsGdb9+w7fIcXedP 7gfoMoj+oF2G/4bleHxDlfS6BNOqbL+/cnoWjN0dIXq+XREHXd8tsrx6lmwiYmDs9CC0 Hdhw== 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=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v1-20020a170902d08100b001b9e9b21280si10587779plv.592.2023.08.02.04.51.39; Wed, 02 Aug 2023 04:51:53 -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=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233986AbjHBLO2 (ORCPT + 99 others); Wed, 2 Aug 2023 07:14:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44858 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233880AbjHBLOX (ORCPT ); Wed, 2 Aug 2023 07:14:23 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 33B292D40 for ; Wed, 2 Aug 2023 04:14:18 -0700 (PDT) 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-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_NONE,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 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,