Re: [PATCH] vfs: keep inodes with page cache off the inode shrinker LRU
From: Johannes Weiner
Date: Wed Feb 12 2020 - 11:35:45 EST
On Tue, Feb 11, 2020 at 03:44:38PM -0800, Andrew Morton wrote:
> On Tue, 11 Feb 2020 14:31:01 -0500 Johannes Weiner <hannes@xxxxxxxxxxx> wrote:
>
> > On Tue, Feb 11, 2020 at 02:05:38PM -0500, Rik van Riel wrote:
> > > On Tue, 2020-02-11 at 12:55 -0500, Johannes Weiner wrote:
> > > > The VFS inode shrinker is currently allowed to reclaim inodes with
> > > > populated page cache. As a result it can drop gigabytes of hot and
> > > > active page cache on the floor without consulting the VM (recorded as
> > > > "inodesteal" events in /proc/vmstat).
> > > >
> > > > This causes real problems in practice. Consider for example how the
> > > > VM
> > > > would cache a source tree, such as the Linux git tree. As large parts
> > > > of the checked out files and the object database are accessed
> > > > repeatedly, the page cache holding this data gets moved to the active
> > > > list, where it's fully (and indefinitely) insulated from one-off
> > > > cache
> > > > moving through the inactive list.
> > >
> > > > This behavior of invalidating page cache from the inode shrinker goes
> > > > back to even before the git import of the kernel tree. It may have
> > > > been less noticeable when the VM itself didn't have real workingset
> > > > protection, and floods of one-off cache would push out any active
> > > > cache over time anyway. But the VM has come a long way since then and
> > > > the inode shrinker is now actively subverting its caching strategy.
> > >
> > > Two things come to mind when looking at this:
> > > - highmem
> > > - NUMA
> > >
> > > IIRC one of the reasons reclaim is done in this way is
> > > because a page cache page in one area of memory (highmem,
> > > or a NUMA node) can end up pinning inode slab memory in
> > > another memory area (normal zone, other NUMA node).
> >
> > That's a good point, highmem does ring a bell now that you mention it.
>
> Yup, that's why this mechanism exists. Here:
>
> https://marc.info/?l=git-commits-head&m=103646757213266&w=2
Ah, thanks for digging that up, I did not know that.
> > If we still care, I think this could be solved by doing something
> > similar to what we do with buffer_heads_over_limit: allow a lowmem
> > allocation to reclaim page cache inside the highmem zone if the bhs
> > (or inodes in this case) have accumulated excessively.
>
> Well, reclaiming highmem pagecache at random would be a painful way to
> reclaim lowmem inodes. Better to pick an inode then shoot down all its
> pagecache. Perhaps we could take its pagecache's aging into account.
That reminds me of trying to correlate inode pages in reclaim to batch
the cache tree lock, slab page objects in the shrinker to free whole
pages etc. We never managed to actually do that. :-)
> Testing this will be a challenge, but the issue was real - a 7GB
> highmem machine isn't crazy and I expect the inode has become larger
> since those days.
Since the cache purging code was written for highmem scenarios, how
about making it specific to CONFIG_HIGHMEM at least?
That way we improve the situation for the more common setups, without
regressing highmem configurations. And if somebody wanted to improve
the CONFIG_HIGHMEM behavior as well, they could still do so.
Somethig like the below delta on top of my patch?
---
fs/inode.c | 44 ++++++++++++++++++++++++++++++++++++++++----
include/linux/fs.h | 5 +++++
2 files changed, 45 insertions(+), 4 deletions(-)
diff --git a/fs/inode.c b/fs/inode.c
index 575b780fa9bb..45b2abd4fef6 100644
--- a/fs/inode.c
+++ b/fs/inode.c
@@ -454,6 +454,18 @@ bool inode_add_lru(struct inode *inode)
return true;
}
+/*
+ * Usually, inodes become reclaimable when they are no longer
+ * referenced and their page cache has been reclaimed. The following
+ * API allows the VM to communicate cache population state to the VFS.
+ *
+ * However, on CONFIG_HIGHMEM we can't wait for the page cache to go
+ * away: cache pages allocated in a large highmem zone could pin
+ * struct inode memory allocated in relatively small lowmem zones. So
+ * when CONFIG_HIGHMEM is enabled, we tie cache to the inode lifetime.
+ */
+
+#ifndef CONFIG_HIGHMEM
/**
* inode_pages_set - mark the inode as holding page cache
* @inode: the inode whose first cache page was just added
@@ -512,6 +524,7 @@ void inode_pages_clear(struct inode *inode)
spin_unlock(&inode->i_lock);
}
+#endif /* !CONFIG_HIGHMEM */
/**
* inode_sb_list_add - add inode to the superblock list of inodes
@@ -826,16 +839,39 @@ static enum lru_status inode_lru_isolate(struct list_head *item,
}
/*
- * Populated inodes shouldn't be on the shrinker LRU, but they
- * can be briefly visible when a new page is added to an inode
- * that was already linked but inode_pages_set() hasn't run
- * yet to move them off.
+ * Usually, populated inodes shouldn't be on the shrinker LRU,
+ * but they can be briefly visible when a new page is added to
+ * an inode that was already linked but inode_pages_set()
+ * hasn't run yet to move them off.
+ *
+ * The other exception is on HIGHMEM systems: highmem cache
+ * can pin lowmem struct inodes, and we might be in dire
+ * straits in the lower zones. Purge cache to free the inode.
*/
if (inode_has_buffers(inode) || !mapping_empty(&inode->i_data)) {
+#ifdef CONFIG_HIGHMEM
+ __iget(inode);
+ spin_unlock(&inode->i_lock);
+ spin_unlock(lru_lock);
+ if (remove_inode_buffers(inode)) {
+ unsigned long reap;
+ reap = invalidate_mapping_pages(&inode->i_data, 0, -1);
+ if (current_is_kswapd())
+ __count_vm_events(KSWAPD_INODESTEAL, reap);
+ else
+ __count_vm_events(PGINODESTEAL, reap);
+ if (current->reclaim_state)
+ current->reclaim_state->reclaimed_slab += reap;
+ }
+ iput(inode);
+ spin_lock(lru_lock);
+ return LRU_RETRY;
+#else
list_lru_isolate(lru, &inode->i_lru);
spin_unlock(&inode->i_lock);
this_cpu_dec(nr_unused);
return LRU_REMOVED;
+#endif
}
WARN_ON(inode->i_state & I_NEW);
diff --git a/include/linux/fs.h b/include/linux/fs.h
index a98d9dee39f4..abdb3fd3432f 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -3106,8 +3106,13 @@ static inline void remove_inode_hash(struct inode *inode)
__remove_inode_hash(inode);
}
+#ifndef CONFIG_HIGHMEM
extern void inode_pages_set(struct inode *inode);
extern void inode_pages_clear(struct inode *inode);
+#else
+static inline void inode_pages_set(struct inode *inode) {}
+static inline void inode_pages_clear(struct inode *inode) {}
+#endif
extern void inode_sb_list_add(struct inode *inode);
--
2.24.1