xas_reload() in iter_xarray_populate_pages()
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
Quoted 8 lines · Dev Jain
Hello all,
After doing an
xas_load()andxas_retry(), we take neither a reference nor a lock on the folio, and we do anxas_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()andxas_reload(), because onlyxas_retry()exists between them, so why to evenreload()?
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
Quoted 27 lines · David Hildenbrand
Quoted 8 lines · Dev Jain
Hello all,
After doing an
xas_load()andxas_retry(), we take neither a reference nor a lock on the folio, and we do anxas_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()andxas_reload(), because onlyxas_retry()exists between them, so why to evenreload()?The usual sequence for the pagecache is (see
filemap_get_entry())1)
xas_load(): Load the entry2)
xas_retry(): Test if we have to retry immediately3) folio ||
xa_is_value(folio): check if the entry stores a folio4)
folio_try_get(): try getting a folio reference, might get freed concurrently, so afolio_get()is not safe5) folio !=
xas_reload(&xas): recheck whether the entry was changed concurrentlyiter_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: convertiter_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.
Quoted 11 lines · Dev Jain
Hello all,
After doing an
xas_load()andxas_retry(), we take neither a reference nor a lock on the folio, and we do anxas_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()andxas_reload(), because onlyxas_retry()exists between them, so why to evenreload()?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 11 lines · Dev Jain
Quoted 9 lines · Dev Jain
Hello all,
After doing an
xas_load()andxas_retry(), we take neither a reference nor a lock on the folio, and we do anxas_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()andxas_reload(), because onlyxas_retry()exists between them, so why to evenreload()?
Quoted 7 lines
Quoted 2 lines
Thanks,
DevI 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_loadand then do afolio_get()? Even if we remove the redundantxas_reload(), I still don't understand why we won't needxas_reload()at least afterfolio_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
Quoted 20 lines · David Hildenbrand
Quoted 11 lines · Dev Jain
Quoted 9 lines · Dev Jain
Hello all,
After doing an
xas_load()andxas_retry(), we take neither a reference nor a lock on the folio, and we do anxas_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()andxas_reload(), because onlyxas_retry()exists between them, so why to evenreload()?Quoted 7 lines
Quoted 2 lines
Thanks,
DevI 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_loadand then do afolio_get()? Even if we remove the redundantxas_reload(), I still don't understand why we won't needxas_reload()at least afterfolio_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 checkfolio->mappingetc.
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.
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()andxas_retry(), we take neither a reference nor a lock
on the folio, and we do anxas_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()andxas_reload(), because onlyxas_retry()exists between them, so why to evenreload()?Thanks,
DevQuoted 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 doxas_loadand then do
afolio_get()? Even if we remove the redundantxas_reload(), I still don't understand
why we won't needxas_reload()at least afterfolio_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 checkfolio->mappingetc.
Quoted 8 lines
Well...pretty sure the file read path is taking the
inode->i_rwsemori_locksomewhere,to synchronize with truncation/reclaim, can't figure out where. Reclaim takes the
i_lockin__remove_mappingandthen freezes the folio reference, so the read path must lock
i_locksomewhere.
I mean, if concurrent freeing of a folio would be possible, the function would be horribly broken :)
Signature · 3 lines
Cheers, David / dhildenb
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()andxas_retry(), we take neither a reference nor a lock
on the folio, and we do anxas_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()andxas_reload(), because onlyxas_retry()exists between them, so why to evenreload()?Thanks,
DevQuoted 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 doxas_loadand then do
afolio_get()? Even if we remove the redundantxas_reload(), I still don't understand
why we won't needxas_reload()at least afterfolio_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 checkfolio->mappingetc.Well...pretty sure the file read path is taking the
inode->i_rwsemori_locksomewhere,
Quoted 7 lines
Quoted 5 lines
to synchronize with truncation/reclaim, can't figure out where. Reclaim takes the
i_lockin__remove_mappingandthen freezes the folio reference, so the read path must lock
i_locksomewhere.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 : )
Quoted 11 lines · Dev Jain
Quoted 9 lines · Dev Jain
Hello all,
After doing an
xas_load()andxas_retry(), we take neither a reference nor a lock on the folio, and we do anxas_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()andxas_reload(), because onlyxas_retry()exists between them, so why to evenreload()?
Quoted 7 lines
Quoted 2 lines
Thanks,
DevI 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_loadand then do afolio_get()? Even if we remove the redundantxas_reload(), I still don't understand why we won't needxas_reload()at least afterfolio_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.
Quoted 26 lines · Matthew Wilcox
Quoted 10 lines · Dev Jain
Quoted 9 lines · Dev Jain
Hello all,
After doing an
xas_load()andxas_retry(), we take neither a reference nor a lock on the folio, and we do anxas_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()andxas_reload(), because onlyxas_retry()exists between them, so why to evenreload()?Quoted 6 lines
Quoted 2 lines
Thanks,
DevI 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_loadand then do afolio_get()? Even if we remove the redundantxas_reload(), I still don't understand why we won't needxas_reload()at least afterfolio_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.