Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp6096718rdb; Mon, 18 Sep 2023 04:18:10 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF0TGAdf8/Di4dQdII8iN38uYMyjxoJcgM0UCa6dXdJy6/8QDYhE91jxDRsaTnJ7xFeXSFH X-Received: by 2002:a05:6e02:1a25:b0:34f:fb92:2082 with SMTP id g5-20020a056e021a2500b0034ffb922082mr539823ile.16.1695035889775; Mon, 18 Sep 2023 04:18:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695035889; cv=none; d=google.com; s=arc-20160816; b=kuO4FS4qpZfQmU1t1X9ictUlMGlOyyedx+xQznWsfY2YQnXXOiM7MRZ12ULrUoUR0F 6rWl9QAEluwyExqY11UivKbY/RpGf7bmwDpZT9+C+njuWTRctVzf8Tkl/tcVEVdI8PTU zxYIwX7qXUhX5DYFG4Y5HKn6atWc2+feUtQpDOeV6zq2riZivJ+fygxR2DqaFXngj0je W1uofFi+KxyWMqFrry9Pd8tQaC+E0DbRFG3bH8bkO8ENhnvTrTXD0Y+EAuWcraP9aEkR OkX9o5i6b3QJqhFgqK42uWBOaoBMMtw2McVsUs/Gji7r+q5d5XOWK0sjN5SQyJqnszHZ qR2A== 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=FUQNLCZpEOmigWGh007/AmH0DjLIzdShytogcEh/Th8=; fh=KwayAskJSS4ZGW1RnI6GoBsv+cfF2sa1gQNWUyxtw50=; b=pPCq3SK+BssHK1wRSnfDeFoQc1Y4lGBClWSs+c74N3jzVd9jjUZ903Xnie49XuUumy dj+wvtTlNyjyKtznoz4lHDq6nRjSJiKRNmxgSERDbZT58x/W9UQp8ayxal+lzToCmlTA k7aSNC2RjZ/bWFZa5xJgzFGPkSxrh9Ai8mKvGlHw7WZ28+EO/Gz3/9smLK5McbyrhJNR xRjebb+AIyfJ8qtlm3iH2ag4I0LcT9isCNuheI6HqMYki6FWQF3E95LaJg8xrLZbMPOk cJqShVbnoNycIOI09YlfgCDgnCOGjmCGu2wgbYnKpTG8a3M993tnJZjrAk+rmGgBj5t8 nuSQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=c9z2Ne3b; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 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 fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id v202-20020a6361d3000000b00565f0087a6bsi7746287pgb.427.2023.09.18.04.18.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Sep 2023 04:18:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=c9z2Ne3b; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 2039581F1DAD; Mon, 18 Sep 2023 00:35:08 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240215AbjIRHeh (ORCPT + 99 others); Mon, 18 Sep 2023 03:34:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34882 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240398AbjIRHeS (ORCPT ); Mon, 18 Sep 2023 03:34:18 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1ABEECFC for ; Mon, 18 Sep 2023 00:33:38 -0700 (PDT) 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="465932097" X-IronPort-AV: E=Sophos;i="6.02,156,1688454000"; d="scan'208";a="465932097" 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 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Mon, 18 Sep 2023 00:35:08 -0700 (PDT) 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, -- 2.39.2