From patchwork Fri Jul 21 09:40:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yin Fengwei X-Patchwork-Id: 13321718 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 BF54EC001DE for ; Fri, 21 Jul 2023 09:41:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 53FD72801AD; Fri, 21 Jul 2023 05:41:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4EF112801A0; Fri, 21 Jul 2023 05:41:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3B7392801AD; Fri, 21 Jul 2023 05:41:57 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 2BCDC2801A0 for ; Fri, 21 Jul 2023 05:41:57 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 00F8540209 for ; Fri, 21 Jul 2023 09:41:56 +0000 (UTC) X-FDA: 81035127474.15.AE9C532 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by imf09.hostedemail.com (Postfix) with ESMTP id C2B81140015 for ; Fri, 21 Jul 2023 09:41:54 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="n8InHVh/"; spf=pass (imf09.hostedemail.com: domain of fengwei.yin@intel.com designates 192.55.52.136 as permitted sender) smtp.mailfrom=fengwei.yin@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1689932515; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=CJU6rlhjEkJQvGttexfzxf5+8AjYlAy+5TBBCbgKccE=; b=dJKyQTQ4KoPbJxgZeJgLi3D2dtVHMryyDOIkLdhRphl/mLccRRVbJigRu1qFflmgxjVR7H pULOgGTxEMw596FbaPE+uA/6lKKv2DXExe8z6ZeW4BmO/1YYSaawxG7TDZRF8NfVomKcaE kGn5tXSZWH9xhmceMob04qTYMJa8W2A= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="n8InHVh/"; spf=pass (imf09.hostedemail.com: domain of fengwei.yin@intel.com designates 192.55.52.136 as permitted sender) smtp.mailfrom=fengwei.yin@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1689932515; a=rsa-sha256; cv=none; b=fFV/LEFeLrmZmTL+g2s87ai1ZRpfLspz7DeZqdd36e8MWhIAGljaoWyUm1/tcEuj5Vt45r 59i6GrwfO6Ub2qQfhELP2j5i5CilLaHkkru/JBZ2ifMYdK9kQLQfwBLhIzu6tazRVzMsru tkfCJbr/2RPwOJtPl5Ohmxpk61TbdL4= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1689932514; x=1721468514; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=gTxpEd4Cqo7yQGmFIHWT43Boa+nqgiZNKOPooxf76ME=; b=n8InHVh/he2wzC/Q27oveMji9JDAK8n62B6j0FoBMdVlhpipK2YNYQ8H v9XIAs1nw9i1o0YFgBw+pDQC8QAwDezHE7CZkx3VmYvgEGaWUURWb/h4O 7XDN23l4hdiqUG2sRWpTaozJP4QUffFneGSOGhzGiYOquEyHyFngtWTNl GJIlKcK399nlcVMxhKh6R2tlfpGlOhkWjNb4ryWsKNhLXScrX4TSWaEnS ekS8lNI7Q/kZPPWyk+0OBveGPxCISGhDLypCUEZt/4183VSHFW29fpwTs Ytib2V5LD7rkqrWfmUASgnrIJOXUVYolFiLWoec7WcxEE44dMPO49RyCE g==; X-IronPort-AV: E=McAfee;i="6600,9927,10777"; a="346575456" X-IronPort-AV: E=Sophos;i="6.01,220,1684825200"; d="scan'208";a="346575456" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Jul 2023 02:41:15 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10777"; a="838480273" X-IronPort-AV: E=Sophos;i="6.01,220,1684825200"; d="scan'208";a="838480273" Received: from fyin-dev.sh.intel.com ([10.239.159.32]) by fmsmga002.fm.intel.com with ESMTP; 21 Jul 2023 02:41:13 -0700 From: Yin Fengwei To: linux-mm@kvack.org, linux-kernel@vger.kernel.org, akpm@linux-foundation.org, minchan@kernel.org, yuzhao@google.com, willy@infradead.org, david@redhat.com, ryan.roberts@arm.com, shy828301@gmail.com Cc: fengwei.yin@intel.com Subject: [RFC PATCH v2 3/4] mm: add functions folio_in_range() and folio_within_vma() Date: Fri, 21 Jul 2023 17:40:42 +0800 Message-Id: <20230721094043.2506691-4-fengwei.yin@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230721094043.2506691-1-fengwei.yin@intel.com> References: <20230721094043.2506691-1-fengwei.yin@intel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: C2B81140015 X-Rspam-User: X-Stat-Signature: nggh7o7mftu1rpo4bs8s9933qtkwtje9 X-Rspamd-Server: rspam01 X-HE-Tag: 1689932514-572173 X-HE-Meta: U2FsdGVkX1/bta261Ye5PB0ee/BuDPit4b/oOojlkHLIokPQM1pb9HxU1cqJLByluZkII1ax6VGP8uttjj1R22S9184pbL4T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jvV7Ow93PE= 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: 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 | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/mm/internal.h b/mm/internal.h index 483add0bfb28..c7dd15d8de3e 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -585,6 +585,38 @@ 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); + +static inline bool +folio_in_range(struct folio *folio, struct vm_area_struct *vma, + unsigned long start, unsigned long end) +{ + 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); + + return ((addr >= start) && (addr + folio_size(folio) <= end)); +} + +static inline bool +folio_within_vma(struct folio *folio, struct vm_area_struct *vma) +{ + return folio_in_range(folio, vma, vma->vm_start, vma->vm_end); +} + /* * mlock_vma_folio() and munlock_vma_folio(): * should be called with vma's mmap_lock held for read or write,