Re: [PATCH v7] zswap: replace RB tree with xarray

Johannes Weiner
2024-03-20T10:08:09Z

On Wed, Mar 20, 2024 at 07:24:27AM +0000, Yosry Ahmed wrote:
> [..]
> > > > - /* map */
> > > > - spin_lock(&tree->lock);
> > > > /*
> > > > - * The folio may have been dirtied again, invalidate the
> > > > - * possibly stale entry before inserting the new entry.
> > > > + * We finish initializing the entry while it's already in xarray.
> > > > + * This is safe because:
> > > > + *
> > > > + * 1. Concurrent stores and invalidations are excluded by folio lock.
> > > > + *
> > > > + * 2. Writeback is excluded by the entry not being on the LRU yet.
> > > > + * The publishing order matters to prevent writeback from seeing
> > > > + * an incoherent entry.
> > >
> > > As I mentioned before, writeback is also protected by the folio lock.
> > > Concurrent writeback will find the folio in the swapcache and abort. The
> > > fact that the entry is not on the LRU yet is just additional protection,
> > > so I don't think the publishing order actually matters here. Right?
> >
> > Right. This comment is explaining why this publishing order does not
> > matter. I think we are talking about the same thing here?
>
> The comment literally says "the publishing order matters.." :)
>
> I believe Johannes meant that we should only publish the entry to the
> LRU once it is fully initialized, to prevent writeback from using a
> partially initialized entry.
>
> What I am saying is that, even if we add a partially initialized entry
> to the zswap LRU, writeback will skip it anyway because the folio is
> locked in the swapcache.
>
> So basically I think the comment should say:
>
> /*
> * We finish initializing the entry while it's already in the
> * xarray. This is safe because the folio is locked in the swap
> * cache, which should protect against concurrent stores,
> * invalidations, and writeback.
> */
>
> Johannes, what do you think?

I don't think that's quite right.

Writeback will bail on swapcache insert, yes, but it will access the
entry before attempting it. If LRU publishing happened before setting
entry->swpentry e.g., we'd have a problem, while your comment suggets
it would be safe to rearrange the code like this.

So LRU publishing order does matter.