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 C6528C001DF for ; Wed, 2 Aug 2023 15:15:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 65A852801B3; Wed, 2 Aug 2023 11:15:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 60B152801AA; Wed, 2 Aug 2023 11:15:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4D2B52801B3; Wed, 2 Aug 2023 11:15:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 38A202801AA for ; Wed, 2 Aug 2023 11:15:40 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 12A271405E2 for ; Wed, 2 Aug 2023 15:15:40 +0000 (UTC) X-FDA: 81079514040.21.FBD9E9F Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf20.hostedemail.com (Postfix) with ESMTP id 313A41C0029 for ; Wed, 2 Aug 2023 15:15:37 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf20.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=1690989338; 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=fNFmC5cPl0F+DcJYLfSJHmXTOiC8V/9F1YJkfu5Z9nU=; b=nAOf3zgb6IplrVjKJF/nUMiVC4HzQys7l/7xy0gR97c6a9Vldu7WbbK29Ep9dNgZ+cvPpr UQxkn+IsVJk3hclxH1YMqysgzmejKDEH1Ws8puJEMrTQgC2nNg2NBvFCr8D7rpptZ8mn8Y CmNX5YNkirbKiqfAx/uTV/f67kYPEGc= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf20.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=1690989338; a=rsa-sha256; cv=none; b=0fHd1zYfLQBT7MWEhwBKyUGu3WSEDLvaSnLScC3n2AV6x/gib1YupROhJcYi174v05R9Xw Z+dq+E6RKjYz5xviKBSGJH+secgqLbFw4F75LZTep58aYCW41Ek8mZJPSOvWFVR+nQZngJ dm9AfmrssHHF+qdlv5EKqC7n2SFjEXQ= 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 627C8113E; Wed, 2 Aug 2023 08:16:20 -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 B73053F5A1; Wed, 2 Aug 2023 08:15:35 -0700 (PDT) Message-ID: Date: Wed, 2 Aug 2023 16:15:34 +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-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 313A41C0029 X-Stat-Signature: w185pgrjht9y7exkb7e4ueyd6dzke1mc X-HE-Tag: 1690989337-778300 X-HE-Meta: U2FsdGVkX1/huo4lJMvMt6PhWIdxf4CrKLceiJCLm5+b6TIKnO+fbm6v3UVxMzk8MhbEq2b5b0/sEgd3GFnpydcLdFW64Mv1j/B79NRqWMTja/X1aanOoPAXXxjnminBakqV7RcbaUhqpjbLE6laLWG4nGTXIcHlYYFTPiRPb0nPt/5FweTEiNZmzKiOghnIAXRQylcU9lkIQXc+DJUcHM9MvVYTVVTLUYpZGpx9+UktmBvhcBCwIVvRufrPcG+oGvmyO6P62g+7zSuH6PMRCm4mUaH9QtNyepKmER1JyPHNUMrJhzJr7s+ODB8Ts0rtn16EL7Xt7xxCIKJfLnYNGwxTggZdmgQlFLFtcb5JPPsSKGEs80aSZivq+V3rkyv2vo+nwUGxZkNReAkg2z5LfDGkSae6QZcjk6yIn8ay7EfwZ35DUazM8DXvD758Nxfytx6niOo5b5hwBhkCsgF0+1ll8HZYExgyQPkH6TGE17lwXM5Si9xNIAzvwQpjOvPjtgesSdkHcR66lFHkFcybaZh/PSk3hFVJRTTljfSyWE3U00VoH5W/OXoN8mLxg6B4m88XalSJr0mJpFxPHgG6Li1m4N2TCQ377etRCnWcFVS4CtIvVv/NB/YeHIDXYuoZIndGtd90hmXKtJKNOCVtaBVt7ezLLAZfwdyIEuRTK+KmXJe/fGTwiDFFpcXRlhGt/Nzs43BkkoJPTcsqayLfFbhtrbnafY/njvzYuI8i279ZORsvCevXKQQ/cV0LQsYa4dMei5jSwnx/WqtSjYW+VsWVdAyNGEOY60t4YgZ2MwULI+l6DDJFcPVBUEI8PJ8VNXb5ZL08L0Y1C1kIN51HCSA6QOG4SKm+DhV18hTWGq0OVHH2z1RZoXZGpWjA4LSOg4vTVQjYmCa8s14yA9HVQh8klAXqXIzz1OqWr9KNcwfVtrd2RtnHZqiw1MIlxor34Tp6Na0Lv8Q8OIMRU/i hee5VG/1 VMFpYFr28TByBWnYbWeLtLsgHkdyE3tNhtFzugJ2smIMCtCkd/dCYkylIUIY/PuwlfSSVInTieeH0mV2Y/cmMlbPMseE28+7yLtgkp4SFdckcpn3A0qDlVJrLKQIv7XwZFib+0YjSl8zSj8bMN/ng3LDTCLVs53jknIx1M20eUKz+tERtAPGibp7VcA== 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) > +{ > + 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; I'm struggling with this logic. What happens for an anonymous folio in a (private) file mapping? Wouldn't the folio's pgoff be 0? In this case you could return false incorrectly? > + > + 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; > + } > + > + 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,