Skip to contentSkip to conversation

xas_reload() in iter_xarray_populate_pages()

QUESTION Dev Jain 3 participants 10 messages May 26, 2025 – Jun 18, 2025 Memory management

lore
Share & tools
lore

Dev Jain

author
#0 lore raw

Hello all,

After doing an xas_load() and xas_retry(), we take neither a reference nor a lock on the folio, and we do an xas_reload(). Is this just to reduce the time window for a race?

If the above is true, then, there is a negligible window between xas_load() and xas_reload(), because only xas_retry() exists between them, so why to even reload()?

Thanks,
Dev

#1 lore raw
Quoted 8 lines · Dev Jain

Hello all,

After doing an xas_load() and xas_retry(), we take neither a reference nor a lock on the folio, and we do an xas_reload(). Is this just to reduce the time window for a race?

If the above is true, then, there is a negligible window between xas_load() and xas_reload(), because only xas_retry() exists between them, so why to even reload()?

The usual sequence for the pagecache is (see filemap_get_entry())

1) xas_load(): Load the entry

2) xas_retry(): Test if we have to retry immediately

3) folio || xa_is_value(folio): check if the entry stores a folio

4) folio_try_get(): try getting a folio reference, might get freed concurrently, so a folio_get() is not safe

5) folio != xas_reload(&xas): recheck whether the entry was changed concurrently

iter_xarray_get_pages()->iter_xarray_populate_pages() works on whatever xarray was provided to iov_iter_xarray().

erofs/netfs/orangefs seem to pass the pagecache ... so I would also assume that we have to use the same sequence as above.

Willy and me had a look ad that code in b57f4f4f186d ("iov_iter: convert iter_xarray_populate_pages() to use folios").

But looking at it now, I think that code is incorrect. At least the folio_get() and reload-before-folio-get is weird.

Signature · 3 lines
Cheers,

David / dhildenb
#2 lore raw
Quoted 27 lines · David Hildenbrand
Quoted 8 lines · Dev Jain

Hello all,

After doing an xas_load() and xas_retry(), we take neither a reference nor a lock on the folio, and we do an xas_reload(). Is this just to reduce the time window for a race?

If the above is true, then, there is a negligible window between xas_load() and xas_reload(), because only xas_retry() exists between them, so why to even reload()?

The usual sequence for the pagecache is (see filemap_get_entry())

1) xas_load(): Load the entry

2) xas_retry(): Test if we have to retry immediately

3) folio || xa_is_value(folio): check if the entry stores a folio

4) folio_try_get(): try getting a folio reference, might get freed concurrently, so a folio_get() is not safe

5) folio != xas_reload(&xas): recheck whether the entry was changed concurrently

iter_xarray_get_pages()->iter_xarray_populate_pages() works on whatever xarray was provided to iov_iter_xarray().

Quoted 8 lines

erofs/netfs/orangefs seem to pass the pagecache ... so I would also assume that we have to use the same sequence as above.

Willy and me had a look ad that code in b57f4f4f186d ("iov_iter: convert iter_xarray_populate_pages() to use folios").

But looking at it now, I think that code is incorrect. At least the folio_get() and reload-before-folio-get is weird.

Well, I just converted it; I didn't think hard about what it was doing was right. I think I may have mentioned to dhowells that I thought the xas_reload() was unnecessary as the folios are required to be referenced by the caller. So if you can look them up, they're guaranteed to not be replaced [1].

[1] the xas_retry() cannot be skipped as it guards against a rearrangement of the tree which can happen even with the folio pinned.

author
#3 lore raw
In reply to Dev Jain
Quoted 11 lines · Dev Jain

Hello all,

After doing an xas_load() and xas_retry(), we take neither a reference nor a lock on the folio, and we do an xas_reload(). Is this just to reduce the time window for a race?

If the above is true, then, there is a negligible window between xas_load() and xas_reload(), because only xas_retry() exists between them, so why to even reload()?

Thanks,
Dev

I do not completely remember our discussion in THP Cabal; I recall David Howells maybe saying that the folios are already locked, so it is safe to do xas_load and then do a folio_get()? Even if we remove the redundant xas_reload(), I still don't understand why we won't need xas_reload() at least after folio_get()?

#4 lore raw
Quoted 11 lines · Dev Jain
Quoted 9 lines · Dev Jain

Hello all,

After doing an xas_load() and xas_retry(), we take neither a reference nor a lock on the folio, and we do an xas_reload(). Is this just to reduce the time window for a race?

If the above is true, then, there is a negligible window between xas_load() and xas_reload(), because only xas_retry() exists between them, so why to even reload()?

Quoted 7 lines
Quoted 2 lines

Thanks,
Dev

I do not completely remember our discussion in THP Cabal; I recall David Howells maybe saying that the folios are already locked, so it is safe to do xas_load and then do a folio_get()? Even if we remove the redundant xas_reload(), I still don't understand why we won't need xas_reload() at least after folio_get()?

I think the points where

(a) this should go all away soon

(b) there is the expectation that the folios cannot get truncated concurrently. So we can do an unconditional folio_get(), don't have to check folio->mapping etc.

(c) The xas_reload() seems unnecessary and can be dropped.

Signature · 3 lines
Cheers,

David / dhildenb
author
#5 lore raw
Quoted 20 lines · David Hildenbrand
Quoted 11 lines · Dev Jain
Quoted 9 lines · Dev Jain

Hello all,

After doing an xas_load() and xas_retry(), we take neither a reference nor a lock on the folio, and we do an xas_reload(). Is this just to reduce the time window for a race?

If the above is true, then, there is a negligible window between xas_load() and xas_reload(), because only xas_retry() exists between them, so why to even reload()?

Quoted 7 lines
Quoted 2 lines

Thanks,
Dev

I do not completely remember our discussion in THP Cabal; I recall David Howells maybe saying that the folios are already locked, so it is safe to do xas_load and then do a folio_get()? Even if we remove the redundant xas_reload(), I still don't understand why we won't need xas_reload() at least after folio_get()?

Quoted 7 lines

I think the points where

(a) this should go all away soon

(b) there is the expectation that the folios cannot get truncated concurrently. So we can do an unconditional folio_get(), don't have to check folio->mapping etc.

Well...pretty sure the file read path is taking the inode->i_rwsem or i_lock somewhere,

to synchronize with truncation/reclaim, can't figure out where. Reclaim takes the i_lock in __remove_mapping and

then freezes the folio reference, so the read path must lock i_lock somewhere.

Quoted 3 lines

(c) The xas_reload() seems unnecessary and can be dropped.

#6 lore raw
Quoted 37 lines · Dev Jain
Quoted 27 lines · David Hildenbrand
Quoted 18 lines · Dev Jain
Quoted 8 lines · Dev Jain

Hello all,

After doing an xas_load() and xas_retry(), we take neither a reference nor a lock
on the folio, and we do an xas_reload(). Is this just to reduce the time window
for a race?

Quoted 7 lines

If the above is true, then, there is a negligible window between xas_load() and
xas_reload(), because only xas_retry() exists between them, so why to even reload()?

Thanks,
Dev

Quoted 7 lines

I do not completely remember our discussion in THP Cabal; I recall David Howells maybe
saying that the folios are already locked, so it is safe to do xas_load and then do
a folio_get()? Even if we remove the redundant xas_reload(), I still don't understand
why we won't need xas_reload() at least after folio_get()?

Quoted 7 lines

I think the points where

(a) this should go all away soon

(b) there is the expectation that the folios cannot get truncated concurrently. So we can do an unconditional folio_get(), don't have to check folio->mapping etc.

Quoted 8 lines

Well...pretty sure the file read path is taking the inode->i_rwsem or i_lock somewhere,

to synchronize with truncation/reclaim, can't figure out where. Reclaim takes the i_lock in __remove_mapping and

then freezes the folio reference, so the read path must lock i_lock somewhere.

I mean, if concurrent freeing of a folio would be possible, the function would be horribly broken :)

Signature · 3 lines
Cheers,

David / dhildenb
author
#7 lore raw
Quoted 41 lines · David Hildenbrand
Quoted 40 lines · Dev Jain
Quoted 27 lines · David Hildenbrand
Quoted 18 lines · Dev Jain
Quoted 8 lines · Dev Jain

Hello all,

After doing an xas_load() and xas_retry(), we take neither a reference nor a lock
on the folio, and we do an xas_reload(). Is this just to reduce the time window
for a race?

Quoted 7 lines

If the above is true, then, there is a negligible window between xas_load() and
xas_reload(), because only xas_retry() exists between them, so why to even reload()?

Thanks,
Dev

Quoted 7 lines

I do not completely remember our discussion in THP Cabal; I recall David Howells maybe
saying that the folios are already locked, so it is safe to do xas_load and then do
a folio_get()? Even if we remove the redundant xas_reload(), I still don't understand
why we won't need xas_reload() at least after folio_get()?

Quoted 7 lines

I think the points where

(a) this should go all away soon

(b) there is the expectation that the folios cannot get truncated concurrently. So we can do an unconditional folio_get(), don't have to check folio->mapping etc.

Well...pretty sure the file read path is taking the inode->i_rwsem or i_lock somewhere,

Quoted 7 lines
Quoted 5 lines

to synchronize with truncation/reclaim, can't figure out where. Reclaim takes the i_lock in __remove_mapping and

then freezes the folio reference, so the read path must lock i_lock somewhere.

I mean, if concurrent freeing of a folio would be possible, the function would be horribly broken :)

Yes, trusting the kernel overlords I have dropped my search : )

#8 lore raw
In reply to Dev Jain
Quoted 11 lines · Dev Jain
Quoted 9 lines · Dev Jain

Hello all,

After doing an xas_load() and xas_retry(), we take neither a reference nor a lock on the folio, and we do an xas_reload(). Is this just to reduce the time window for a race?

If the above is true, then, there is a negligible window between xas_load() and xas_reload(), because only xas_retry() exists between them, so why to even reload()?

Quoted 7 lines
Quoted 2 lines

Thanks,
Dev

I do not completely remember our discussion in THP Cabal; I recall David Howells maybe saying that the folios are already locked, so it is safe to do xas_load and then do a folio_get()? Even if we remove the redundant xas_reload(), I still don't understand why we won't need xas_reload() at least after folio_get()?

Because you need xas_reload() in order to solve this race:

A: load folio
B: remove folio
B: free folio
C: alloc folio
A: tryget folio
A: reload folio

If A already has a refcount on folio, folio cannot be freed, and so A cannot get a refcount to C's folio.

The other mutexes are irrelevant here; this is purely a folio refcount problem/solution.

author
#9 lore raw
Quoted 26 lines · Matthew Wilcox
Quoted 10 lines · Dev Jain
Quoted 9 lines · Dev Jain

Hello all,

After doing an xas_load() and xas_retry(), we take neither a reference nor a lock on the folio, and we do an xas_reload(). Is this just to reduce the time window for a race?

If the above is true, then, there is a negligible window between xas_load() and xas_reload(), because only xas_retry() exists between them, so why to even reload()?

Quoted 6 lines
Quoted 2 lines

Thanks,
Dev

I do not completely remember our discussion in THP Cabal; I recall David Howells maybe saying that the folios are already locked, so it is safe to do xas_load and then do a folio_get()? Even if we remove the redundant xas_reload(), I still don't understand why we won't need xas_reload() at least after folio_get()?

Because you need xas_reload() in order to solve this race:

A: load folio
B: remove folio
B: free folio
C: alloc folio
A: tryget folio
A: reload folio

Quoted 2 lines

If A already has a refcount on folio, folio cannot be freed, and so A cannot get a refcount to C's folio.

Yes sorry, I should have written that why is an unconditional folio get being used instead of tryget...and the answer seems to be that we already probably have the inode lock so we are guaranteed that the folio won't get evicted.

Quoted 3 lines

The other mutexes are irrelevant here; this is purely a folio refcount problem/solution.