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 96D7A4949F1; Wed, 29 Jul 2026 16:49:30 +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=1785343772; cv=none; b=gPXuzNqybnx+nQMPKPWOr4pgGQc58lleMI7iRAz0RGgl2UbM/cHEOeRIRTfXTyHLaAlndRO7U0QnFHovwOV5cYYGi6D7zxMc0LtI/FIW3YxuGcbf9zrtyi/IRvvjJNk4z9WJFnjwsXrdm6GyXoPqUpzUsgMEitMRE0DNmxN7JGo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785343772; c=relaxed/simple; bh=N82Ec3g8xe0NAsE4B7h+svvxqnczc4k7NB9slAtBrDc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=n3qcu33K9PFr0O4wUFG84zEqyFOhcK3XfXqalsmara67Ed/GSLYfz4q3gI08MIYftpjFGy+UL567xXutN/t2ZoYJzPKckXL6WACu7IBdxLlJkmglxbOLBMnoibV+YhxUD7AwbXkwY1i3+ptUFxobeLC7Q77Ji3cjzehWid++oIA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=hhpd0p3b; 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="hhpd0p3b" Received: by smtp.kernel.org (Postfix) with ESMTPSA id DF5871F000E9; Wed, 29 Jul 2026 16:49:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1785343770; bh=k5kAutsj482xFMCwr+33we+a5btvgg3Nzu6z2dOByr0=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=hhpd0p3bXLycu3LhvAj8ez3BxmG7t7+mx2ZPT0+kcXKmDqV/EAHCqSBeCc9UTueMN F0feD/IkKhCTU8xcq48A5BVBFFthxR2+TwVjLB03ERYQSFHJCuRoNruqgMd4bXb/8f vb8CZrETPDBWe+4pQOfuJEDepEGleqczB+c24ydzEt3vL6ld3F90kiU5d39L//+sSO KnwHzKjLCRuwFC0Ho+Fdks2vY/e2YoaHlEQlDyOF4YmonBE22u2Esyg/VSZgPbuKF7 DbaEFdErKqVSMjBwB5V+TZn3evxTHY+MJ5+iEKkRvx8brUQLhXvXInmVyVXsF8fa7S THza4vR69ARBg== From: "Lorenzo Stoakes (ARM)" Date: Wed, 29 Jul 2026 17:48:37 +0100 Subject: [PATCH v3 02/15] mm: introduce linear_anon_page_index() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260729-b4-scalable-cow-virt-pgoff-v3-2-e8ecfefea812@kernel.org> References: <20260729-b4-scalable-cow-virt-pgoff-v3-0-e8ecfefea812@kernel.org> In-Reply-To: <20260729-b4-scalable-cow-virt-pgoff-v3-0-e8ecfefea812@kernel.org> To: Andrew Morton , David Hildenbrand , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Jann Horn , Pedro Falcato , "Matthew Wilcox (Oracle)" , Jan Kara , Miaohe Lin , Naoya Horiguchi , Rik van Riel , Harry Yoo , Lance Yang , Kees Cook , Zi Yan , Baolin Wang , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Usama Arif , Matthew Brost , Joshua Hahn , Rakie Kim , Byungchul Park , Gregory Price , Ying Huang , Alistair Popple , Peter Xu , Xu Xin , Chengming Zhou , Arnd Bergmann , Greg Kroah-Hartman Cc: "Lorenzo Stoakes (ARM)" , linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kselftest@vger.kernel.org X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=4933; i=ljs@kernel.org; h=from:subject:message-id; bh=N82Ec3g8xe0NAsE4B7h+svvxqnczc4k7NB9slAtBrDc=; b=owGbwMvMwCV2fu7ZrsZH9SKMp9WSGLKy9H7YCR6+//mTxBz7LWdvC74vkz7ibv5Pfr4NU3H6s 2c/dR+adJSyMIhxMciKKbI8/yK+P0gkbF7nBX83mDmsTCBDGLg4BWAizD8ZGf74frBrf/v9pI7l 9EmK5z3zJ7GzO7rdFi/P/mr9bZLvmwaG/37BMVs7SgszxIJUP8n+UOcoF+/WPrnITet0hsfxn5L H+QA= X-Developer-Key: i=ljs@kernel.org; a=openpgp; fpr=E7F417BF5214569E89D04F46CF9DCD8A81E27F14 This function provides the anonymous equivalent of linear_page_index(), instead offsetting based on the anonymous 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(). Assert that both of these invariants are true In linear_anon_page_index() and implement the algorithm in __linear_anon_page_index(). 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_anon_page_index(). VMA userland tests are also updated accordingly. Signed-off-by: Lorenzo Stoakes (ARM) --- include/linux/mm.h | 2 +- include/linux/pagemap.h | 42 +++++++++++++++++++++++++++++++++++++++++ tools/testing/vma/include/dup.h | 25 +++++++++++++++++++++++- 3 files changed, 67 insertions(+), 2 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index df78847f5f07..64214191e7c6 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1556,7 +1556,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 c6fc783aaee5..259177544b03 100644 --- a/include/linux/pagemap.h +++ b/include/linux/pagemap.h @@ -1101,6 +1101,48 @@ static inline pgoff_t linear_page_index(const struct vm_area_struct *vma, return pgoff; } +static inline pgoff_t __linear_anon_page_index(const struct vm_area_struct *vma, + const unsigned long address) +{ + pgoff_t pgoff; + + pgoff = linear_page_delta(vma, address); + pgoff += vma_start_anon_pgoff(vma); + return pgoff; +} + +/** + * linear_anon_page_index() - Determine the absolute anonymous 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 anonymous 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 anonymous 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 anonymous page offset of @address within @vma. + */ +static inline pgoff_t linear_anon_page_index(const struct vm_area_struct *vma, + const unsigned long address) +{ + const pgoff_t pgoff = __linear_anon_page_index(vma, address); + + VM_WARN_ON_ONCE(vma_test(vma, VMA_SHARED_BIT)); + /* Account for MAP_PRIVATE-/dev/zero which is only semi-anonymous. */ + if (vma_is_anonymous(vma) && !vma->vm_file) + 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 26d9210b1e8b..5f1d93b3aefb 100644 --- a/tools/testing/vma/include/dup.h +++ b/tools/testing/vma/include/dup.h @@ -1417,7 +1417,7 @@ static inline void vma_iter_set(struct vma_iterator *vmi, unsigned long addr) mas_set(&vmi->mas, addr); } -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; } @@ -1610,3 +1610,26 @@ static inline pgprot_t vma_get_page_prot(const struct vm_area_struct *vma) { return vma_flags_to_page_prot(vma->flags); } + +static inline pgoff_t __linear_anon_page_index(const struct vm_area_struct *vma, + const unsigned long address) +{ + pgoff_t pgoff; + + pgoff = linear_page_delta(vma, address); + pgoff += vma_start_anon_pgoff(vma); + return pgoff; +} + +static inline pgoff_t linear_anon_page_index(const struct vm_area_struct *vma, + const unsigned long address) +{ + const pgoff_t pgoff = __linear_anon_page_index(vma, address); + + VM_WARN_ON_ONCE(vma_test(vma, VMA_SHARED_BIT)); + /* Account for MAP_PRIVATE-/dev/zero which is only semi-anonymous. */ + if (vma_is_anonymous(vma) && !vma->vm_file) + VM_WARN_ON_ONCE(pgoff != linear_page_index(vma, address)); + + return pgoff; +} -- 2.55.0