Re: [PATCH v2] mm: shmem: implement POSIX_FADV_[WILL|DONT]NEED for shmem

From: Matthew Wilcox
Date: Thu Dec 02 2021 - 10:54:27 EST


On Thu, Dec 02, 2021 at 08:59:52PM +0530, Charan Teja Kalla wrote:
> > What part of the XArray documentation led you to believe that this is a
> > safe thing to do? Because it needs to be rewritten immediately!
>
> The above code changes made from my understanding of both the
> Documentation and the implementation of xa_for_each(). The Locking
> section of the document[1] says that xa_for_each() takes the rcu lock
> thus can be used without any explicit locking and the "Advanced API"
> section says that users need to take xa_lock/rcu lock as no locking done
> for you.
>
> Further I have looked at the xa_for_each() implementation details,
> where, it is taking the rcu_lock just across xas_find() in both
> xa_find() and xa_find_after() which made me to think that it just needs
> to take the rcu lock just across the xas_find().
>
> But a comment from you saying that this implementation is wrong making
> me to think that I lack very trivial understanding about xarray usage.

Would this change to the documentation have prevented you from making
this mistake?

The advanced API is based around the xa_state. This is an opaque data
structure which you declare on the stack using the XA_STATE()
macro. This macro initialises the xa_state ready to start walking
around the XArray. It is used as a cursor to maintain the position
in the XArray and let you compose various operations together without
-having to restart from the top every time.
+having to restart from the top every time. The contents of the xa_state
+are protected by the rcu_read_lock() or the xas_lock(). If you need to
+drop whichever of those locks is protecting your state and tree, you must
+call xas_pause() so that future calls do not rely on the parts of the
+state which were left unprotected.