Re: [PATCH v4 03/20] mm: introduce linear_anon_page_index()
From: "Lorenzo Stoakes (ARM)" <ljs@kernel.org>
Date: 2026-08-10 08:40:26
Also in:
amd-gfx, dri-devel, intel-xe, kvm, linux-fsdevel, linux-kselftest, linux-mm, linux-perf-users, linux-s390, lkml
On Sat, Aug 08, 2026 at 05:49:48PM -0700, Suren Baghdasaryan wrote:
On Thu, Aug 6, 2026 at 1:23 PM Lorenzo Stoakes (ARM) [off-list ref] wrote:quoted
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, in other words CoW 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()s/In/in
Ack. I ran these patches through claude several times and it didn't get this ;) the machines are not taking over just yet... Andrew - could you change this in-place? Thanks!
quoted
diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h index c6fc783aaee5..0adfa6605653 100644 --- a/include/linux/pagemap.h +++ b/include/linux/pagemap.h@@ -1094,10 +1094,44 @@ static inline pgoff_t linear_page_delta(const struct vm_area_struct *vma, static inline pgoff_t linear_page_index(const struct vm_area_struct *vma, const unsigned long address) { - pgoff_t pgoff; + return linear_page_delta(vma, address) + vma_start_pgoff(vma); +} + +static inline pgoff_t __linear_anon_page_index(const struct vm_area_struct *vma, + const unsigned long address) +{ + return linear_page_delta(vma, address) + vma_start_anon_pgoff(vma); +} + +/** + * 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_is_cow_mapping(vma));As discussed off the list, this assertion works even for read-only anon mappings because, as you said, "there's no such thing as a pure anon mapping that is !VMA_MAYWRITE_BIT, there is no way to achieve that." It took me some time to see that after reading your discussion with David at [1] and [2], and I don't think it's very intuitive. Could you please add a comment explaining that? Maybe add a comment at vma_is_cow_mapping() definition since you are using the same assertion in another patch of this series?
Sure will do! It is very confusing stuff (and I was duly confused also at first). To save a respin (unless there's more stuff I need to address) I will probably reply to the relevant patch with a proposed new comment.
[1] https://lore.kernel.org/linux-mm/anBjpdMicqaGADr-@lucifer/ (local) [2] https://lore.kernel.org/linux-mm/anLyhaI6kW-EWk13@lucifer/ (local)
-- Cheers, Lorenzo