Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp909361rwb; Fri, 28 Jul 2023 01:28:05 -0700 (PDT) X-Google-Smtp-Source: APBJJlFgwavSyNaRnyRqaiYElDGyyM/WfP4f/OGcrSdiwMwQwGbMb48hRlbwLJCaVXlkD71PAE50 X-Received: by 2002:a17:907:1dce:b0:974:771e:6bf0 with SMTP id og14-20020a1709071dce00b00974771e6bf0mr1592926ejc.56.1690532884961; Fri, 28 Jul 2023 01:28:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690532884; cv=none; d=google.com; s=arc-20160816; b=as8IkC4qID2HS15fNKDdbh7WhBJQ4qzHKek5Rpd14IDIoCh3vrlz6FCdSe1o9hVuDo 4DZZviL4XXtRtCIyyPnTgRWwQwSo83o1fWWtRyE26eKpP3umdavv0+thcnY3/CpBP1X5 2uBi0viS+HHHnhoV1OzMAdcVTPPFUbYknWhuVwCYa9Lj1QbNa/hROiJgfe7L1AxqEhO7 Qftp4LmI8qI9BQFdBRHZCrPXARFTcp3McpGyfEMv2ZHh7kCQAcjNBB8L8tWb+UwIZPmc ut3QUhn9rBvFDxkvV5COcNNFMLgd6B7ZiJUsP4AnOk8XMMw27GzBSiq+dHjYVEXyDSkS YGgA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Ndav55EIknyWAhlk4Z6h8jE2vJe9nExG9vzBr9pdpiA=; fh=KMQ3xH9pggRJP0ZJV0B9K14WJwLgFGpmvfRRwCIOtl4=; b=jibsgHOb0L86zJI6haS1R6H+78893rBtL5fvixznrnpF2li4pwAcfN+2rCKhduQbwK ffqiUVF1O6Yvvw3hf88FdpYz+HDL5yHaxr/tjt+eLLAAMQGdBBgozvaSSP8SZkpx8xFl tuVr6js/c77VzP/hOGX6MhDyH0zPdlt11NOWHQScjjElWaLTOeWNM4pT5xZgmYOFhXim I9ekaYDHq9J64pKv0aHIv52bHoTwjzGanJRnQ6iM8IotDj4lokjWENdKQr/63Wueo2y1 j/DCIT0iv3Tj0Z1uZbgliP/Dombyoy/q4VSCvHJU2XHzAaXX0O8NCUZiEKDi46sZSh4x Mtow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Lg3rd+Ve; 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=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k22-20020a17090627d600b0098e266c9592si2388506ejc.262.2023.07.28.01.27.34; Fri, 28 Jul 2023 01:28:04 -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; dkim=pass header.i=@intel.com header.s=Intel header.b=Lg3rd+Ve; 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=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234047AbjG1HL5 (ORCPT + 99 others); Fri, 28 Jul 2023 03:11:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43758 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233961AbjG1HLc (ORCPT ); Fri, 28 Jul 2023 03:11:32 -0400 Received: from mgamail.intel.com (unknown [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8CEF03C2F for ; Fri, 28 Jul 2023 00:11:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690528273; x=1722064273; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=hDGtd2tgXIan13dy2PEKZ68DXcw4gSjf8pK8pQRP1gI=; b=Lg3rd+VevH6UWHbFj1yVn/9CjWW7Et/34lbFAdqH9mGZTzT5/xz1muVe qnhPUDWwa3879cXvwmP6fykEMsWRTSHcQOs8qivWB/ivY3UPEJdOpNTqk THRtKEqR8f/XRla9fuBXhtHwfp/kqh9//PihcbjgbHHIOpuX4fvIYx+CN AshUlA3bZM4HIRs96y7S7uxHbd/3dDKRota7O6CJwuchoEJHjJLChgXd7 7BtZPrWu+dYA62kOu1YNSxUNE5I8Yg4Zmd/N68duglT3jTRHPMmKcEvrE N2e56q3QwACogrTBs1DAXLTH+GoESJn/KJMxVVmw6NikvI4igLQTHHlwk g==; X-IronPort-AV: E=McAfee;i="6600,9927,10784"; a="454888119" X-IronPort-AV: E=Sophos;i="6.01,236,1684825200"; d="scan'208";a="454888119" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Jul 2023 00:11:12 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10784"; a="721161998" X-IronPort-AV: E=Sophos;i="6.01,236,1684825200"; d="scan'208";a="721161998" Received: from fyin-dev.sh.intel.com ([10.239.159.32]) by orsmga007.jf.intel.com with ESMTP; 28 Jul 2023 00:11:08 -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, hughd@google.com Cc: fengwei.yin@intel.com Subject: [PATCH 1/3] mm: add functions folio_in_range() and folio_within_vma() Date: Fri, 28 Jul 2023 15:09:27 +0800 Message-Id: <20230728070929.2487065-2-fengwei.yin@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230728070929.2487065-1-fengwei.yin@intel.com> References: <20230728070929.2487065-1-fengwei.yin@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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 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; + + 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, -- 2.39.2