From patchwork Wed Jul 12 06:01: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: 13309582 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 02F34EB64DA for ; Wed, 12 Jul 2023 06:02:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 551456B0072; Wed, 12 Jul 2023 02:02:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 501BD6B0075; Wed, 12 Jul 2023 02:02:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3F00B6B0078; Wed, 12 Jul 2023 02:02:01 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 2F0FB6B0072 for ; Wed, 12 Jul 2023 02:02:01 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id EB6571C766E for ; Wed, 12 Jul 2023 06:02:00 +0000 (UTC) X-FDA: 81001914000.17.2FDC537 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by imf25.hostedemail.com (Postfix) with ESMTP id E5023A001F for ; Wed, 12 Jul 2023 06:01:57 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=iBX8JCvK; spf=pass (imf25.hostedemail.com: domain of fengwei.yin@intel.com designates 134.134.136.20 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=1689141718; 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=8Tp9SwptlNmcx1F/KycyE/xYsKdhV38ttWqI2MHrVbE=; b=o/RE1d7KFhXfM6qQ1IzGLQrszDg4iyq/msisz6GnM3njCU4qXOOCKduotrqH7lwfjCz/Iq nLPeEiVcSDA0SypjGVyY7JM5nQkXhY71aspshlhlAr0sprRAK54pk26lv0hxocKJjp4eUd obuXMtO3TIOUtx6K9XjKGyF+RXBI1Cc= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1689141718; a=rsa-sha256; cv=none; b=OGi9FemOfaxXjg7X3Gr1HgUaMQaNNRXviZpXhFendYy3LCthFXOFVVymELU54D4RcJQyD1 dtz5rgCSUUjPT4PM0FmQqQQHpxL7X/IQSieUulMs7TzIQ08GqWYV4DsA83mza/usLKzN9R nEMyM35gbvberWRNKCQCffqE6YCVRMo= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=iBX8JCvK; spf=pass (imf25.hostedemail.com: domain of fengwei.yin@intel.com designates 134.134.136.20 as permitted sender) smtp.mailfrom=fengwei.yin@intel.com; dmarc=pass (policy=none) header.from=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1689141718; x=1720677718; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=o/Aws3JMOGR03jlt+wsWp78WyeH6J7qvKifxMU03VXk=; b=iBX8JCvK49IYQdjFyUL61YrqkMZLWHn98iC4MyDMq+vGLcIJ+6XRQH+X xTR2Gl154T5o8qZGThQ3cMgyI1nF4yryczUiyWBkAvKFxBHgCUXjKbS3P ckUyAC3B5FGFIvNvugpGlt+K6UhlFy6DjJbqkwtiJoaReMv1ZMqvqcR9D qwQNbo4mupbt3DmgOWfZwprqhVb186wucQW0RRdxQ9KjeTfW9X3KvMzTh bpZbKx6i95QNWiTA5sY7PYYo59WA0Yvfd89XC8CEMwXoQ6D8hkQzwTO0e pid71D657gMuMeMcMqVce6SFx5ZG984RORGvRpY6m1JreE+tZaF/+EGg8 Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10768"; a="354715287" X-IronPort-AV: E=Sophos;i="6.01,198,1684825200"; d="scan'208";a="354715287" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Jul 2023 23:01:56 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10768"; a="756643382" X-IronPort-AV: E=Sophos;i="6.01,198,1684825200"; d="scan'208";a="756643382" Received: from fyin-dev.sh.intel.com ([10.239.159.32]) by orsmga001.jf.intel.com with ESMTP; 11 Jul 2023 23:01:53 -0700 From: Yin Fengwei To: linux-mm@kvack.org, linux-kernel@vger.kernel.org, akpm@linux-foundation.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 1/3] mm: add functions folio_in_range() and folio_within_vma() Date: Wed, 12 Jul 2023 14:01:42 +0800 Message-Id: <20230712060144.3006358-2-fengwei.yin@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230712060144.3006358-1-fengwei.yin@intel.com> References: <20230712060144.3006358-1-fengwei.yin@intel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: E5023A001F X-Rspam-User: X-Stat-Signature: ruxmq6exs1ydmods1e6ixmz8ybki98dw X-Rspamd-Server: rspam03 X-HE-Tag: 1689141717-113180 X-HE-Meta: U2FsdGVkX18n0UwzWNBCbdUPuV1IOipChMBQZ9l0PIhVQsFMrVaQWy4WzH9Di5OQ/pn1Xz7td0A8WDq9VpusTveQtW9zj9Ll5joEiYWREj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yNsihHmBvo= 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 Reviewed-by: Yu Zhao --- mm/internal.h | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/mm/internal.h b/mm/internal.h index 483add0bfb289..c7dd15d8de3ef 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,