From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 16531426687; Mon, 29 Jun 2026 15:04:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782745448; cv=none; b=cWLNh7DrbPDVlqbCV0/TXf2UPvhZHLCe91zcgpn4BTXcDTQkB4o9Ofh38mI8R9VItZKRLxl3gFK+FoKFLZbrQayUuWgsezMTpzo7LVF9GQna5x+muypOkgQ7/eQx1Xoq6aBeyB/vGbxNZiOmDGWwi/aLGuMiEhpdL09T0LYzFLc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782745448; c=relaxed/simple; bh=U7wosi9MWRQZLHKVMZ5ivlEVPEiQPmpVpqRvYd5h9hU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iHWvu6dLgmJ+irzarhN1Gvu03HDqgqmzoSFswTt0hq9USDsSvP0bDEQMpz1MTcek7BihfENNofzSbnzsILGBWOoWUV1Pw2lpgoeVYkMnMapIXK4/lSPH4UK+EYDTSdYVzjhBOmrEgwr3KAvcAIXJlsVmIP/hk/FdWfx875Hrl4M= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=C/5rVcyE; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="C/5rVcyE" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D9D8D1F000E9; Mon, 29 Jun 2026 15:04:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1782745446; bh=1DdTYzEtu4ByiEUoSpMsinYaOl4+O4+ddJT+gWoYupA=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=C/5rVcyEx/DDRNUuTGZxEIfgwq0ywplFZzKOf6vzDKxIfUFz9aKIZkEICUqgIa06G vR6p7lB01J9QCTLZ/DWSzDmEcjVeRSlnKvOODgIorGqNEBgsbzzWBGtLCRgNORsdFl gDWfh8k0JvYr4OCmiZ1gOvzYlIcXhzjJF4FXcCqe1Yurw33PwM7P1GPw5gRwRNyq5P eTHRvW7PVXd5VSkI7WINZRVmGgrORlXvR1C4UZr44aiF4/h46c70FC89n/m18HUAog OWiav7nKM5MXu/shgHYgVlYyHv44ZoKfzqp9SkeIKHaa7tpLy/wuZ3v56QIDURqi7V cS94ReU+6pujQ== From: Lorenzo Stoakes To: Andrew Morton Cc: David Hildenbrand , "Liam R . Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Matthew Wilcox , Jan Kara , Rik van Riel , Harry Yoo , Jann Horn , Zi Yan , Baolin Wang , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Xu Xin , Chengming Zhou , Miaohe Lin , Naoya Horiguchi , Matthew Brost , Joshua Hahn , Rakie Kim , Byungchul Park , Gregory Price , Ying Huang , Alistair Popple , Pedro Falcato , Peter Xu , Kees Cook , linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: [RFC PATCH 02/10] mm: introduce linear_virt_page_index() Date: Mon, 29 Jun 2026 16:03:42 +0100 Message-ID: X-Mailer: git-send-email 2.54.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit This function provides the virtual equivalent of linear_page_index(), instead offsetting based on the virtual page offset of the VMA. It is valid only for anonymous or MAP_PRIVATE file-backed mappings. It must not be called for shared file-backed mappings. For pure anon VMAs, this will be equal to linear_page_index(). We implement the algorithm in __linear_virt_page_index(), which is provided for internal mm code that might be interacting with shared VMAs. In linear_virt_page_index() we assert that both of these invariants are true. Note that MAP_PRIVATE-/dev/zero mappings will satisfy vma_is_anonymous() but not fulfill this invariant, so when asserting this we check vma->vm_file to account for this. We do not update callsites yet, so no functional change intended. Also const-ify vma_is_anonymous() to make it compatible with the const-ified linear_virt_page_index(). VMA userland tests are updated accordingly. Signed-off-by: Lorenzo Stoakes --- include/linux/mm.h | 2 +- include/linux/pagemap.h | 41 +++++++++++++++++++++++++++++++++ tools/testing/vma/include/dup.h | 22 ++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index cd826c052be1..9451aa537abb 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1533,7 +1533,7 @@ static inline void vma_desc_set_anonymous(struct vm_area_desc *desc) desc->vm_ops = NULL; } -static inline bool vma_is_anonymous(struct vm_area_struct *vma) +static inline bool vma_is_anonymous(const struct vm_area_struct *vma) { return !vma->vm_ops; } diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h index 68a88d34a468..6e0d719d639a 100644 --- a/include/linux/pagemap.h +++ b/include/linux/pagemap.h @@ -1105,6 +1105,47 @@ static inline pgoff_t linear_page_index(const struct vm_area_struct *vma, return pgoff; } +static inline pgoff_t __linear_virt_page_index(const struct vm_area_struct *vma, + const unsigned long address) +{ + pgoff_t pgoff; + + pgoff = linear_page_delta(vma, address); + pgoff += vma_start_virt_pgoff(vma); + return pgoff; +} + +/** + * linear_virt_page_index() - Determine the absolute virtual page offset of + * @address within @vma. + * @vma: An anonymous or MAP_PRIVATE file-backed VMA in which @address resides. + * @address: The address whose absolute page offset is required. + * + * This returns the virtual page offset of @address, which is the page offset + * the address possessed at the time the VMA was first faulted. + * + * For anonymous mappings, this returns the same value as linear_page_index(). + * + * For MAP_PRIVATE file-backed mappings, this returns the virtual page offset of + * @address, which is the page offset the address possessed at the time the VMA + * was first faulted. + * + * It is not valid to call this function for shared file-backed mappings. + * + * Returns: The absolute virtual page offset of @address within @vma. + */ +static inline pgoff_t linear_virt_page_index(const struct vm_area_struct *vma, + const unsigned long address) +{ + const pgoff_t pgoff = __linear_virt_page_index(vma, address); + + VM_WARN_ON_ONCE(vma_test(vma, VMA_SHARED_BIT)); + if (!vma->vm_file) /* Is anonymous except MAP_PRIVATE-/dev/zero */ + VM_WARN_ON_ONCE(pgoff != linear_page_index(vma, address)); + + return pgoff; +} + struct wait_page_key { struct folio *folio; int bit_nr; diff --git a/tools/testing/vma/include/dup.h b/tools/testing/vma/include/dup.h index 09cfbf9572e8..bac6caf2eaa2 100644 --- a/tools/testing/vma/include/dup.h +++ b/tools/testing/vma/include/dup.h @@ -1601,3 +1601,25 @@ static inline pgoff_t linear_page_index(const struct vm_area_struct *vma, pgoff += vma_start_pgoff(vma); return pgoff; } + +static inline pgoff_t __linear_virt_page_index(const struct vm_area_struct *vma, + const unsigned long address) +{ + pgoff_t pgoff; + + pgoff = linear_page_delta(vma, address); + pgoff += vma_start_virt_pgoff(vma); + return pgoff; +} + +static inline pgoff_t linear_virt_page_index(const struct vm_area_struct *vma, + const unsigned long address) +{ + const pgoff_t pgoff = __linear_virt_page_index(vma, address); + + VM_WARN_ON_ONCE(vma_test(vma, VMA_SHARED_BIT)); + if (!vma->vm_file) /* Is anonymous except MAP_PRIVATE-/dev/zero */ + VM_WARN_ON_ONCE(pgoff != linear_page_index(vma, address)); + + return pgoff; +} -- 2.54.0