From: Domenico Cerasuolo <cerasuolodomenico@gmail.com>
To: Johannes Weiner <hannes@cmpxchg.org>
Cc: vitaly.wool@konsulko.com, minchan@kernel.org,
senozhatsky@chromium.org, yosryahmed@google.com,
linux-mm@kvack.org, ddstreet@ieee.org, sjenning@redhat.com,
nphamcs@gmail.com, akpm@linux-foundation.org,
linux-kernel@vger.kernel.org, kernel-team@meta.com
Subject: Re: [RFC PATCH v2 1/7] mm: zswap: add pool shrinking mechanism
Date: Fri, 9 Jun 2023 10:39:55 +0200 [thread overview]
Message-ID: <CA+CLi1gP6+tKrHSfUxBhLvYu=F7NMnuPp+gt-63cwonU4r25UA@mail.gmail.com> (raw)
In-Reply-To: <20230608184516.GA356779@cmpxchg.org>
On Thu, Jun 8, 2023 at 8:45 PM Johannes Weiner <hannes@cmpxchg.org> wrote:
>
> On Thu, Jun 08, 2023 at 01:05:00PM -0400, Johannes Weiner wrote:
> > On Thu, Jun 08, 2023 at 12:52:51PM -0400, Johannes Weiner wrote:
> > > On Tue, Jun 06, 2023 at 04:56:05PM +0200, Domenico Cerasuolo wrote:
> > > > @@ -584,14 +601,70 @@ static struct zswap_pool *zswap_pool_find_get(char *type, char *compressor)
> > > > return NULL;
> > > > }
> > > >
> > > > +static int zswap_shrink(struct zswap_pool *pool)
> > > > +{
> > > > + struct zswap_entry *lru_entry, *tree_entry = NULL;
> > > > + struct zswap_header *zhdr;
> > > > + struct zswap_tree *tree;
> > > > + int swpoffset;
> > > > + int ret;
> > > > +
> > > > + /* get a reclaimable entry from LRU */
> > > > + spin_lock(&pool->lru_lock);
> > > > + if (list_empty(&pool->lru)) {
> > > > + spin_unlock(&pool->lru_lock);
> > > > + return -EINVAL;
> > > > + }
> > > > + lru_entry = list_last_entry(&pool->lru, struct zswap_entry, lru);
> > > > + list_del_init(&lru_entry->lru);
> > > > + zhdr = zpool_map_handle(pool->zpool, lru_entry->handle, ZPOOL_MM_RO);
> > > > + tree = zswap_trees[swp_type(zhdr->swpentry)];
> > > > + zpool_unmap_handle(pool->zpool, lru_entry->handle);
> > > > + /*
> > > > + * Once the pool lock is dropped, the lru_entry might get freed. The
> > > > + * swpoffset is copied to the stack, and lru_entry isn't deref'd again
> > > > + * until the entry is verified to still be alive in the tree.
> > > > + */
> > > > + swpoffset = swp_offset(zhdr->swpentry);
> > > > + spin_unlock(&pool->lru_lock);
> > > > +
> > > > + /* hold a reference from tree so it won't be freed during writeback */
> > > > + spin_lock(&tree->lock);
> > > > + tree_entry = zswap_entry_find_get(&tree->rbroot, swpoffset);
> > > > + if (tree_entry != lru_entry) {
> > > > + if (tree_entry)
> > > > + zswap_entry_put(tree, tree_entry);
> > > > + spin_unlock(&tree->lock);
> > > > + return -EAGAIN;
> > > > + }
> > > > + spin_unlock(&tree->lock);
> > > > +
> > > > + ret = zswap_writeback_entry(pool->zpool, lru_entry->handle);
> > > > +
> > > > + spin_lock(&tree->lock);
> > > > + if (ret) {
> > > > + spin_lock(&pool->lru_lock);
> > > > + list_move(&lru_entry->lru, &pool->lru);
> > > > + spin_unlock(&pool->lru_lock);
> > > > + }
> > > > + zswap_entry_put(tree, tree_entry);
> > >
> > > On re-reading this, I find the lru_entry vs tree_entry distinction
> > > unnecessarily complicated. Once it's known that the thing coming off
> > > the LRU is the same thing as in the tree, there is only "the entry".
> > >
> > > How about 'entry' and 'tree_entry', and after validation use 'entry'
> > > throughout the rest of the function?
> >
> > Even better, safe the tree_entry entirely by getting the reference
> > from the LRU already, and then just search the tree for a match:
> >
> > /* Get an entry off the LRU */
> > spin_lock(&pool->lru_lock);
> > entry = list_last_entry();
> > list_del(&entry->lru);
> > zswap_entry_get(entry);
> > spin_unlock(&pool->lru_lock);
> >
> > /* Check for invalidate() race */
> > spin_lock(&tree->lock);
> > if (entry != zswap_rb_search(&tree->rbroot, swpoffset)) {
> > ret = -EAGAIN;
> > goto put_unlock;
> > }
> > spin_unlock(&tree->lock);
>
> Eh, brainfart. It needs the tree lock to bump the ref, of course.
>
> But this should work, right?
>
> /* Check for invalidate() race */
> spin_lock(&tree->lock);
> if (entry != zswap_rb_search(&tree->rbroot, swpoffset)) {
> ret = -EAGAIN;
> goto unlock;
> }
> zswap_entry_get(entry);
> spin_unlock(&tree->lock);
This should work indeed, it's much cleaner with just one local
zswap_entry, will update!
next prev parent reply other threads:[~2023-06-09 8:40 UTC|newest]
Thread overview: 36+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-06-06 14:56 [RFC PATCH v2 0/7] mm: zswap: move writeback LRU from zpool to zswap Domenico Cerasuolo
2023-06-06 14:56 ` [RFC PATCH v2 1/7] mm: zswap: add pool shrinking mechanism Domenico Cerasuolo
2023-06-07 8:14 ` Yosry Ahmed
2023-06-07 9:22 ` Domenico Cerasuolo
2023-06-07 9:31 ` Yosry Ahmed
2023-06-07 21:39 ` Nhat Pham
2023-06-08 15:58 ` Johannes Weiner
2023-06-08 16:52 ` Johannes Weiner
2023-06-08 17:04 ` Johannes Weiner
2023-06-08 18:45 ` Johannes Weiner
2023-06-09 8:39 ` Domenico Cerasuolo [this message]
2023-06-06 14:56 ` [RFC PATCH v2 2/7] mm: zswap: remove page reclaim logic from zbud Domenico Cerasuolo
2023-06-08 16:02 ` Johannes Weiner
2023-06-06 14:56 ` [RFC PATCH v2 3/7] mm: zswap: remove page reclaim logic from z3fold Domenico Cerasuolo
2023-06-08 16:05 ` Johannes Weiner
2023-06-06 14:56 ` [RFC PATCH v2 4/7] mm: zswap: remove page reclaim logic from zsmalloc Domenico Cerasuolo
2023-06-07 17:23 ` Nhat Pham
2023-06-07 17:45 ` Minchan Kim
2023-06-08 16:07 ` Johannes Weiner
2023-06-06 14:56 ` [RFC PATCH v2 5/7] mm: zswap: remove shrink from zpool interface Domenico Cerasuolo
2023-06-07 9:19 ` Yosry Ahmed
2023-06-08 16:10 ` Johannes Weiner
2023-06-08 17:51 ` Nhat Pham
2023-06-06 14:56 ` [RFC PATCH v2 6/7] mm: zswap: simplify writeback function Domenico Cerasuolo
2023-06-07 9:26 ` Yosry Ahmed
2023-06-09 10:23 ` Domenico Cerasuolo
2023-06-09 11:01 ` Yosry Ahmed
2023-06-08 16:48 ` Johannes Weiner
2023-06-09 11:05 ` Domenico Cerasuolo
2023-06-06 14:56 ` [RFC PATCH v2 7/7] mm: zswap: remove zswap_header Domenico Cerasuolo
2023-06-07 9:30 ` Yosry Ahmed
2023-06-09 16:10 ` Domenico Cerasuolo
2023-06-09 17:13 ` Yosry Ahmed
2023-06-07 9:16 ` [RFC PATCH v2 0/7] mm: zswap: move writeback LRU from zpool to zswap Yosry Ahmed
2023-06-07 9:23 ` Domenico Cerasuolo
2023-06-07 9:32 ` Yosry Ahmed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CA+CLi1gP6+tKrHSfUxBhLvYu=F7NMnuPp+gt-63cwonU4r25UA@mail.gmail.com' \
--to=cerasuolodomenico@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=ddstreet@ieee.org \
--cc=hannes@cmpxchg.org \
--cc=kernel-team@meta.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=minchan@kernel.org \
--cc=nphamcs@gmail.com \
--cc=senozhatsky@chromium.org \
--cc=sjenning@redhat.com \
--cc=vitaly.wool@konsulko.com \
--cc=yosryahmed@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).