Skip to content

Commit f604156

Browse files
Dave ChinnerAl Viro
authored andcommitted
dcache: convert to use new lru list infrastructure
[glommer@openvz.org: don't reintroduce double decrement of nr_unused_dentries, adapted for new LRU return codes] Signed-off-by: Dave Chinner <dchinner@redhat.com> Signed-off-by: Glauber Costa <glommer@openvz.org> Cc: "Theodore Ts'o" <tytso@mit.edu> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Al Viro <viro@zeniv.linux.org.uk> Cc: Artem Bityutskiy <artem.bityutskiy@linux.intel.com> Cc: Arve Hjønnevåg <arve@android.com> Cc: Carlos Maiolino <cmaiolino@redhat.com> Cc: Christoph Hellwig <hch@lst.de> Cc: Chuck Lever <chuck.lever@oracle.com> Cc: Daniel Vetter <daniel.vetter@ffwll.ch> Cc: David Rientjes <rientjes@google.com> Cc: Gleb Natapov <gleb@redhat.com> Cc: Greg Thelen <gthelen@google.com> Cc: J. Bruce Fields <bfields@redhat.com> Cc: Jan Kara <jack@suse.cz> Cc: Jerome Glisse <jglisse@redhat.com> Cc: John Stultz <john.stultz@linaro.org> Cc: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com> Cc: Kent Overstreet <koverstreet@google.com> Cc: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Cc: Marcelo Tosatti <mtosatti@redhat.com> Cc: Mel Gorman <mgorman@suse.de> Cc: Steven Whitehouse <swhiteho@redhat.com> Cc: Thomas Hellstrom <thellstrom@vmware.com> Cc: Trond Myklebust <Trond.Myklebust@netapp.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent d38fa69 commit f604156

File tree

3 files changed

+90
-106
lines changed

3 files changed

+90
-106
lines changed

fs/dcache.c

Lines changed: 78 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <linux/rculist_bl.h>
3838
#include <linux/prefetch.h>
3939
#include <linux/ratelimit.h>
40+
#include <linux/list_lru.h>
4041
#include "internal.h"
4142
#include "mount.h"
4243

@@ -356,28 +357,17 @@ static void dentry_unlink_inode(struct dentry * dentry)
356357
}
357358

358359
/*
359-
* dentry_lru_(add|del|move_list) must be called with d_lock held.
360+
* dentry_lru_(add|del)_list) must be called with d_lock held.
360361
*/
361362
static void dentry_lru_add(struct dentry *dentry)
362363
{
363364
if (unlikely(!(dentry->d_flags & DCACHE_LRU_LIST))) {
364-
spin_lock(&dentry->d_sb->s_dentry_lru_lock);
365+
if (list_lru_add(&dentry->d_sb->s_dentry_lru, &dentry->d_lru))
366+
this_cpu_inc(nr_dentry_unused);
365367
dentry->d_flags |= DCACHE_LRU_LIST;
366-
list_add(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
367-
dentry->d_sb->s_nr_dentry_unused++;
368-
this_cpu_inc(nr_dentry_unused);
369-
spin_unlock(&dentry->d_sb->s_dentry_lru_lock);
370368
}
371369
}
372370

373-
static void __dentry_lru_del(struct dentry *dentry)
374-
{
375-
list_del_init(&dentry->d_lru);
376-
dentry->d_flags &= ~DCACHE_LRU_LIST;
377-
dentry->d_sb->s_nr_dentry_unused--;
378-
this_cpu_dec(nr_dentry_unused);
379-
}
380-
381371
/*
382372
* Remove a dentry with references from the LRU.
383373
*
@@ -393,27 +383,9 @@ static void dentry_lru_del(struct dentry *dentry)
393383
return;
394384
}
395385

396-
if (!list_empty(&dentry->d_lru)) {
397-
spin_lock(&dentry->d_sb->s_dentry_lru_lock);
398-
__dentry_lru_del(dentry);
399-
spin_unlock(&dentry->d_sb->s_dentry_lru_lock);
400-
}
401-
}
402-
403-
static void dentry_lru_move_list(struct dentry *dentry, struct list_head *list)
404-
{
405-
BUG_ON(dentry->d_flags & DCACHE_SHRINK_LIST);
406-
407-
spin_lock(&dentry->d_sb->s_dentry_lru_lock);
408-
if (list_empty(&dentry->d_lru)) {
409-
dentry->d_flags |= DCACHE_LRU_LIST;
410-
list_add_tail(&dentry->d_lru, list);
411-
} else {
412-
list_move_tail(&dentry->d_lru, list);
413-
dentry->d_sb->s_nr_dentry_unused--;
386+
if (list_lru_del(&dentry->d_sb->s_dentry_lru, &dentry->d_lru))
414387
this_cpu_dec(nr_dentry_unused);
415-
}
416-
spin_unlock(&dentry->d_sb->s_dentry_lru_lock);
388+
dentry->d_flags &= ~DCACHE_LRU_LIST;
417389
}
418390

419391
/**
@@ -901,12 +873,72 @@ static void shrink_dentry_list(struct list_head *list)
901873
rcu_read_unlock();
902874
}
903875

876+
static enum lru_status
877+
dentry_lru_isolate(struct list_head *item, spinlock_t *lru_lock, void *arg)
878+
{
879+
struct list_head *freeable = arg;
880+
struct dentry *dentry = container_of(item, struct dentry, d_lru);
881+
882+
883+
/*
884+
* we are inverting the lru lock/dentry->d_lock here,
885+
* so use a trylock. If we fail to get the lock, just skip
886+
* it
887+
*/
888+
if (!spin_trylock(&dentry->d_lock))
889+
return LRU_SKIP;
890+
891+
/*
892+
* Referenced dentries are still in use. If they have active
893+
* counts, just remove them from the LRU. Otherwise give them
894+
* another pass through the LRU.
895+
*/
896+
if (dentry->d_lockref.count) {
897+
list_del_init(&dentry->d_lru);
898+
spin_unlock(&dentry->d_lock);
899+
return LRU_REMOVED;
900+
}
901+
902+
if (dentry->d_flags & DCACHE_REFERENCED) {
903+
dentry->d_flags &= ~DCACHE_REFERENCED;
904+
spin_unlock(&dentry->d_lock);
905+
906+
/*
907+
* The list move itself will be made by the common LRU code. At
908+
* this point, we've dropped the dentry->d_lock but keep the
909+
* lru lock. This is safe to do, since every list movement is
910+
* protected by the lru lock even if both locks are held.
911+
*
912+
* This is guaranteed by the fact that all LRU management
913+
* functions are intermediated by the LRU API calls like
914+
* list_lru_add and list_lru_del. List movement in this file
915+
* only ever occur through this functions or through callbacks
916+
* like this one, that are called from the LRU API.
917+
*
918+
* The only exceptions to this are functions like
919+
* shrink_dentry_list, and code that first checks for the
920+
* DCACHE_SHRINK_LIST flag. Those are guaranteed to be
921+
* operating only with stack provided lists after they are
922+
* properly isolated from the main list. It is thus, always a
923+
* local access.
924+
*/
925+
return LRU_ROTATE;
926+
}
927+
928+
dentry->d_flags |= DCACHE_SHRINK_LIST;
929+
list_move_tail(&dentry->d_lru, freeable);
930+
this_cpu_dec(nr_dentry_unused);
931+
spin_unlock(&dentry->d_lock);
932+
933+
return LRU_REMOVED;
934+
}
935+
904936
/**
905937
* prune_dcache_sb - shrink the dcache
906938
* @sb: superblock
907-
* @count: number of entries to try to free
939+
* @nr_to_scan : number of entries to try to free
908940
*
909-
* Attempt to shrink the superblock dcache LRU by @count entries. This is
941+
* Attempt to shrink the superblock dcache LRU by @nr_to_scan entries. This is
910942
* done when we need more memory an called from the superblock shrinker
911943
* function.
912944
*
@@ -915,45 +947,12 @@ static void shrink_dentry_list(struct list_head *list)
915947
*/
916948
long prune_dcache_sb(struct super_block *sb, unsigned long nr_to_scan)
917949
{
918-
struct dentry *dentry;
919-
LIST_HEAD(referenced);
920-
LIST_HEAD(tmp);
921-
long freed = 0;
950+
LIST_HEAD(dispose);
951+
long freed;
922952

923-
relock:
924-
spin_lock(&sb->s_dentry_lru_lock);
925-
while (!list_empty(&sb->s_dentry_lru)) {
926-
dentry = list_entry(sb->s_dentry_lru.prev,
927-
struct dentry, d_lru);
928-
BUG_ON(dentry->d_sb != sb);
929-
930-
if (!spin_trylock(&dentry->d_lock)) {
931-
spin_unlock(&sb->s_dentry_lru_lock);
932-
cpu_relax();
933-
goto relock;
934-
}
935-
936-
if (dentry->d_flags & DCACHE_REFERENCED) {
937-
dentry->d_flags &= ~DCACHE_REFERENCED;
938-
list_move(&dentry->d_lru, &referenced);
939-
spin_unlock(&dentry->d_lock);
940-
} else {
941-
list_move(&dentry->d_lru, &tmp);
942-
dentry->d_flags |= DCACHE_SHRINK_LIST;
943-
this_cpu_dec(nr_dentry_unused);
944-
sb->s_nr_dentry_unused--;
945-
spin_unlock(&dentry->d_lock);
946-
freed++;
947-
if (!--nr_to_scan)
948-
break;
949-
}
950-
cond_resched_lock(&sb->s_dentry_lru_lock);
951-
}
952-
if (!list_empty(&referenced))
953-
list_splice(&referenced, &sb->s_dentry_lru);
954-
spin_unlock(&sb->s_dentry_lru_lock);
955-
956-
shrink_dentry_list(&tmp);
953+
freed = list_lru_walk(&sb->s_dentry_lru, dentry_lru_isolate,
954+
&dispose, nr_to_scan);
955+
shrink_dentry_list(&dispose);
957956
return freed;
958957
}
959958

@@ -987,24 +986,10 @@ shrink_dcache_list(
987986
*/
988987
void shrink_dcache_sb(struct super_block *sb)
989988
{
990-
LIST_HEAD(tmp);
991-
992-
spin_lock(&sb->s_dentry_lru_lock);
993-
while (!list_empty(&sb->s_dentry_lru)) {
994-
/*
995-
* account for removal here so we don't need to handle it later
996-
* even though the dentry is no longer on the lru list.
997-
*/
998-
list_splice_init(&sb->s_dentry_lru, &tmp);
999-
this_cpu_sub(nr_dentry_unused, sb->s_nr_dentry_unused);
1000-
sb->s_nr_dentry_unused = 0;
1001-
spin_unlock(&sb->s_dentry_lru_lock);
989+
long disposed;
1002990

1003-
shrink_dcache_list(&tmp);
1004-
1005-
spin_lock(&sb->s_dentry_lru_lock);
1006-
}
1007-
spin_unlock(&sb->s_dentry_lru_lock);
991+
disposed = list_lru_dispose_all(&sb->s_dentry_lru, shrink_dcache_list);
992+
this_cpu_sub(nr_dentry_unused, disposed);
1008993
}
1009994
EXPORT_SYMBOL(shrink_dcache_sb);
1010995

@@ -1366,7 +1351,8 @@ static enum d_walk_ret select_collect(void *_data, struct dentry *dentry)
13661351
if (dentry->d_lockref.count) {
13671352
dentry_lru_del(dentry);
13681353
} else if (!(dentry->d_flags & DCACHE_SHRINK_LIST)) {
1369-
dentry_lru_move_list(dentry, &data->dispose);
1354+
dentry_lru_del(dentry);
1355+
list_add_tail(&dentry->d_lru, &data->dispose);
13701356
dentry->d_flags |= DCACHE_SHRINK_LIST;
13711357
data->found++;
13721358
ret = D_WALK_NORETRY;

fs/super.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ static unsigned long super_cache_scan(struct shrinker *shrink,
7979
fs_objects = sb->s_op->nr_cached_objects(sb);
8080

8181
inodes = list_lru_count(&sb->s_inode_lru);
82-
total_objects = sb->s_nr_dentry_unused + inodes + fs_objects + 1;
82+
dentries = list_lru_count(&sb->s_dentry_lru);
83+
total_objects = dentries + inodes + fs_objects + 1;
8384

8485
/* proportion the scan between the caches */
85-
dentries = mult_frac(sc->nr_to_scan, sb->s_nr_dentry_unused,
86-
total_objects);
86+
dentries = mult_frac(sc->nr_to_scan, dentries, total_objects);
8787
inodes = mult_frac(sc->nr_to_scan, inodes, total_objects);
8888

8989
/*
@@ -117,7 +117,7 @@ static unsigned long super_cache_count(struct shrinker *shrink,
117117
if (sb->s_op && sb->s_op->nr_cached_objects)
118118
total_objects = sb->s_op->nr_cached_objects(sb);
119119

120-
total_objects += sb->s_nr_dentry_unused;
120+
total_objects += list_lru_count(&sb->s_dentry_lru);
121121
total_objects += list_lru_count(&sb->s_inode_lru);
122122

123123
total_objects = vfs_pressure_ratio(total_objects);
@@ -191,8 +191,7 @@ static struct super_block *alloc_super(struct file_system_type *type, int flags)
191191
INIT_HLIST_NODE(&s->s_instances);
192192
INIT_HLIST_BL_HEAD(&s->s_anon);
193193
INIT_LIST_HEAD(&s->s_inodes);
194-
INIT_LIST_HEAD(&s->s_dentry_lru);
195-
spin_lock_init(&s->s_dentry_lru_lock);
194+
list_lru_init(&s->s_dentry_lru);
196195
list_lru_init(&s->s_inode_lru);
197196
INIT_LIST_HEAD(&s->s_mounts);
198197
init_rwsem(&s->s_umount);

include/linux/fs.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,14 +1270,6 @@ struct super_block {
12701270
struct list_head s_files;
12711271
#endif
12721272
struct list_head s_mounts; /* list of mounts; _not_ for fs use */
1273-
1274-
/* s_dentry_lru_lock protects s_dentry_lru and s_nr_dentry_unused */
1275-
spinlock_t s_dentry_lru_lock ____cacheline_aligned_in_smp;
1276-
struct list_head s_dentry_lru; /* unused dentry lru */
1277-
long s_nr_dentry_unused; /* # of dentry on lru */
1278-
1279-
struct list_lru s_inode_lru ____cacheline_aligned_in_smp;
1280-
12811273
struct block_device *s_bdev;
12821274
struct backing_dev_info *s_bdi;
12831275
struct mtd_info *s_mtd;
@@ -1331,6 +1323,13 @@ struct super_block {
13311323

13321324
/* AIO completions deferred from interrupt context */
13331325
struct workqueue_struct *s_dio_done_wq;
1326+
1327+
/*
1328+
* Keep the lru lists last in the structure so they always sit on their
1329+
* own individual cachelines.
1330+
*/
1331+
struct list_lru s_dentry_lru ____cacheline_aligned_in_smp;
1332+
struct list_lru s_inode_lru ____cacheline_aligned_in_smp;
13341333
};
13351334

13361335
extern struct timespec current_fs_time(struct super_block *sb);

0 commit comments

Comments
 (0)