From patchwork Mon Sep 18 07:33:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Yin, Fengwei" X-Patchwork-Id: 13388984 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 12F8CCD37B0 for ; Mon, 18 Sep 2023 07:33:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9FEFD6B0299; Mon, 18 Sep 2023 03:33:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9AEF66B029B; Mon, 18 Sep 2023 03:33:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 876F86B029C; Mon, 18 Sep 2023 03:33:41 -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 780EA6B0299 for ; Mon, 18 Sep 2023 03:33:41 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 560D4140BB3 for ; Mon, 18 Sep 2023 07:33:41 +0000 (UTC) X-FDA: 81248903442.30.CFC3177 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.43]) by imf13.hostedemail.com (Postfix) with ESMTP id 4EF0D2002A for ; Mon, 18 Sep 2023 07:33:39 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=c9z2Ne3b; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf13.hostedemail.com: domain of fengwei.yin@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=fengwei.yin@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1695022419; 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=FUQNLCZpEOmigWGh007/AmH0DjLIzdShytogcEh/Th8=; b=h0ZlxvcdGH8Zij9y6pcRvIj3caCkFGilgg+QvQhaPqhj+dcWsBbn1Y9VxJ+4/UlCjpNmVG 40KyW7p0f0D8PncJb656kxWPyFoWeNKNJVPc6/gf1L7NYhPApAw7KNcJ3m4nqxqlfhSl2J SU9V3Rxsmmi2Imaeqwz1fgCLGrwtlXo= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=c9z2Ne3b; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf13.hostedemail.com: domain of fengwei.yin@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=fengwei.yin@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695022419; a=rsa-sha256; cv=none; b=Em5BPhGdXIKbdVTsP2kaltJ6UutyyQVTpNrXk6QZGaCRu1QvVoJTNWyBGoTgrExfus2XWE vwjCEuyMi+6Po1D4FsYRHUDmWt2eyIqXtCEbYJyvvNPMiLn5CU/m6sxVRFxkR55lCwN4YB i1qH5NHqJ3O81JAscrywzHzINrumh1M= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1695022419; x=1726558419; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=mlfQoxCuP8sG2p4kkpY1b2EQ7BccGlB5GeB5heEQ4Is=; b=c9z2Ne3bcRPhO2tiwD5iKVAHOF6c45+YfA53xjhLFh4SDCHfiKFSZxPe B+SV7NUc0/ediSNOw4TDWjRBJmHO2Y6Ohrr/9ETZfpVOlKvGNX9DPP5ST iq8MnkC6UD7LmRMr2yx8AEndpAgwaCPsLPbFbDJxlSzsOWHXdBMwRzINt vB/Hah4RGmy2bgOXAnW9rUpBIBjPLjso0ZE4r+pRuNasLApzX0+76d1Yu 0/bJHcL0f9ir0de7xuq4YX+d7Q3evAUq+LyRzhn6WOCVDErlqhDN3xFgl 2AdzjCY+dLn7wOkBsVLZxNIWPEf+ny9jjRMu4aF2fB6BjxIMDiMWKnEIU Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10836"; a="465932099" X-IronPort-AV: E=Sophos;i="6.02,156,1688454000"; d="scan'208";a="465932099" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Sep 2023 00:33:37 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10836"; a="888929628" X-IronPort-AV: E=Sophos;i="6.02,156,1688454000"; d="scan'208";a="888929628" Received: from fyin-dev.sh.intel.com ([10.239.159.24]) by fmsmga001.fm.intel.com with ESMTP; 18 Sep 2023 00:32:52 -0700 From: Yin Fengwei To: linux-mm@kvack.org, linux-kernel@vger.kernel.org, akpm@linux-foundation.org, yuzhao@google.com, willy@infradead.org, hughd@google.com, yosryahmed@google.com, ryan.roberts@arm.com, david@redhat.com, shy828301@gmail.com Cc: fengwei.yin@intel.com Subject: [PATCH v3 1/3] mm: add functions folio_in_range() and folio_within_vma() Date: Mon, 18 Sep 2023 15:33:16 +0800 Message-Id: <20230918073318.1181104-2-fengwei.yin@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230918073318.1181104-1-fengwei.yin@intel.com> References: <20230918073318.1181104-1-fengwei.yin@intel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 4EF0D2002A X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: ippczi7gs1ysdxxybks115nddkq6mj46 X-HE-Tag: 1695022419-284901 X-HE-Meta: U2FsdGVkX18BxN1fzqQ1wJ7kCOqXwOetOE1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ubdqFz0V6JteZxo8oTfhjabU/T+fHWyF9sbg4EbmE7o+gAnxKhjZLBLkYFOWpa/9yLForIukb3SOD+uL+BEBTRU8= 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 | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/mm/internal.h b/mm/internal.h index 346d82260964..9e2a5b32c659 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -587,6 +587,56 @@ 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); + +/* + * NOTE: This function can't tell whether the folio is "fully mapped" in the + * range. + * "fully mapped" means all the pages of folio is associated with the page + * table of range while this function just check whether the folio range is + * within the range [start, end). Funcation caller nees to do page table + * check if it cares about the page table association. + * + * Typical usage (like mlock or madvise) is: + * Caller knows at least 1 page of folio is associated with page table of VMA + * and the range [start, end) is intersect with the VMA range. Caller wants + * to know whether the folio is fully associated with the range. It calls + * this function to check whether the folio is in the range first. Then checks + * the page table to know whether the folio is fully mapped to the range. + */ +static inline bool +folio_within_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 > end) + return false; + + 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 (!in_range(pgoff, vma->vm_pgoff, vma_pglen)) + return false; + + addr = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT); + + return !(addr < start || end - addr < folio_size(folio)); +} + +static inline bool +folio_within_vma(struct folio *folio, struct vm_area_struct *vma) +{ + return folio_within_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,