Per superblock cache reclaim
Subsystems (8)
cover Per superblock cache reclaim
This series converts the VFS cache shrinkers to a per-superblock shrinker, and provides a callout from the superblock shrinker to allow the filesystem to shrink internal caches proportionally to the amount of reclaim done to the VFS caches.
The original posting contains the motiviation, benefits, performance, etc, and rather than repeat it all, just look here:
https://lkml.org/lkml/2011/6/2/42
The version has been rebased onto the untested branch in Al's VFS git tree, fixes the review comments from the first version, and has had much more testing under different workloads.
---
Version 2:
o rebase onto Al's #untested branch
o fix build problem in Al's untested branch
o move all slab shrinker modifications tracepoints into a single patch.
o renames some of the tracepoint variables to be more obvious as to what they are recording.
o move pin_sb_for_writeback() to fs/super.c and rename it to grab_super_passive() so the shrinker and writeback use the same passive reference counting and unmount detection
o rework per-superblock shrinker to use grab_super_passive()
o minor typo fixes in commit messages, documentation and commit messages
---
When the git mirror updates, this patchset will also be available in the git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/dgc/xfsdev.git per-sb-shrinker
If you get a version based on the current Linus tree, then it hasn't updated yet....
Dave Chinner (14):
dcache: fix __d_alloc prototype to use const
vmscan: add shrink_slab tracepoints
vmscan: shrinker->nr updates race and go wrong
vmscan: reduce wind up shrinker->nr when shrinker can't do work
vmscan: add customisable shrinker batch size
inode: convert inode_stat.nr_unused to per-cpu counters
inode: Make unused inode LRU per superblock
inode: move to per-sb LRU locks
superblock: move pin_sb_for_writeback() to fs/super.c
superblock: introduce per-sb cache shrinker infrastructure
inode: remove iprune_sem
superblock: add filesystem shrinker operations
vfs: increase shrinker batch size
xfs: make use of new shrinker callout for the inode cache
Documentation/filesystems/vfs.txt | 22 +++++++ fs/dcache.c | 121 ++++-------------------------------- fs/fs-writeback.c | 28 +-------- fs/inode.c | 124 ++++++++++++------------------------- fs/internal.h | 3 +- fs/libfs.c | 2 + fs/super.c | 109 ++++++++++++++++++++++++++++++++- fs/xfs/linux-2.6/xfs_super.c | 26 +++++--- fs/xfs/linux-2.6/xfs_sync.c | 71 ++++++++------------- fs/xfs/linux-2.6/xfs_sync.h | 5 +- include/linux/fs.h | 14 ++++ include/linux/mm.h | 1 + include/trace/events/vmscan.h | 71 +++++++++++++++++++++ mm/vmscan.c | 71 +++++++++++++++++----- 14 files changed, 374 insertions(+), 294 deletions(-)
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 1/14 dcache: fix __d_alloc prototype to use const
From: Dave Chinner <[email protected]>
--- fs/internal.h | 2 +- fs/libfs.c | 2 ++ 2 files changed, 3 insertions(+), 1 deletions(-)
fs/internal.h +1 -1
@@ -139,4 +139,4 @@ extern int invalidate_inodes(struct super_block *, bool);/* * dcache.c */ -extern struct dentry *__d_alloc (struct super_block *,struct qstr *); +extern struct dentry *__d_alloc (struct super_block *,const struct qstr *);
fs/libfs.c +2 -0
@@ -16,6 +16,8 @@#include <asm/uaccess.h> +#include "internal.h" +static inline int simple_positive (struct dentry *dentry) {return dentry->d_inode && !d_unhashed (dentry);
Signature · 8 lines
1.7.5.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/ Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 2/14 vmscan: add shrink_slab tracepoints
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
Іt is impossible to understand what the shrinkers are actually doing without instrumenting the code, so add a some tracepoints to allow insight to be gained.
--- include/trace/events/vmscan.h | 71 +++++++++++++++++++++++++++++++++++++++++ mm/vmscan.c | 8 ++++- 2 files changed, 78 insertions(+), 1 deletions(-)
include/trace/events/vmscan.h +71 -0
@@ -179,6 +179,77 @@ DEFINE_EVENT(mm_vmscan_direct_reclaim_end_template, mm_vmscan_memcg_softlimit_reTP_ARGS (nr_reclaimed) ); +TRACE_EVENT (mm_shrink_slab_start, + TP_PROTO(struct shrinker *shr, struct shrink_control *sc, + long nr_objects_to_shrink, unsigned long pgs_scanned, + unsigned long lru_pgs, unsigned long cache_items, + unsigned long long delta, unsigned long total_scan), + +TP_ARGS (shr, sc, nr_objects_to_shrink, pgs_scanned, lru_pgs, + cache_items, delta, total_scan), + +TP_STRUCT__entry ( + __field(struct shrinker *, shr) + __field(long, nr_objects_to_shrink) + __field(gfp_t, gfp_flags) + __field(unsigned long, pgs_scanned) + __field(unsigned long, lru_pgs) + __field(unsigned long, cache_items) + __field(unsigned long long, delta) + __field(unsigned long, total_scan) + ), + +TP_fast_assign ( + __entry->shr = shr; + __entry->nr_objects_to_shrink = nr_objects_to_shrink; + __entry->gfp_flags = sc->gfp_mask; + __entry->pgs_scanned = pgs_scanned; + __entry->lru_pgs = lru_pgs; + __entry->cache_items = cache_items; + __entry->delta = delta; + __entry->total_scan = total_scan; + ), + +TP_printk ("shrinker %p: objects to shrink %ld gfp_flags %s pgs_scanned %ld lru_pgs %ld cache items %ld delta %lld total_scan %ld" , + __entry->shr, + __entry->nr_objects_to_shrink, +show_gfp_flags (__entry->gfp_flags), + __entry->pgs_scanned, + __entry->lru_pgs, + __entry->cache_items, + __entry->delta, + __entry->total_scan) +); + +TRACE_EVENT (mm_shrink_slab_end, + TP_PROTO(struct shrinker *shr, int shrinker_retval, + long unused_scan_cnt, long new_scan_cnt), + +TP_ARGS (shr, shrinker_retval, unused_scan_cnt, new_scan_cnt), + +TP_STRUCT__entry ( + __field(struct shrinker *, shr) + __field(long, unused_scan) + __field(long, new_scan) + __field(int, retval) +__field (long, total_scan) + ), + +TP_fast_assign ( + __entry->shr = shr; + __entry->unused_scan = unused_scan_cnt; + __entry->new_scan = new_scan_cnt; + __entry->retval = shrinker_retval; + __entry->total_scan = new_scan_cnt - unused_scan_cnt; + ), + +TP_printk ("shrinker %p: unused scan count %ld new scan count %ld total_scan %ld last shrinker return val %d" , + __entry->shr, + __entry->unused_scan, + __entry->new_scan, + __entry->total_scan, + __entry->retval) +); DECLARE_EVENT_CLASS(mm_vmscan_lru_isolate_template,
mm/vmscan.c +7 -1
@@ -250,6 +250,7 @@ unsigned long shrink_slab(struct shrink_control *shrink,unsigned long long delta;unsigned long total_scan;unsigned long max_pass; +int shrink_ret =0 ; max_pass =do_shrinker_shrink (shrinker, shrink,0 ); delta = (4 * nr_pages_scanned) / shrinker->seeks;
@@ -274,9 +275,12 @@ unsigned long shrink_slab(struct shrink_control *shrink, total_scan = shrinker->nr; shrinker->nr =0 ; +trace_mm_shrink_slab_start (shrinker, shrink, total_scan, + nr_pages_scanned, lru_pages, + max_pass, delta, total_scan); +while (total_scan >=SHRINK_BATCH ) {long this_scan =SHRINK_BATCH ; -int shrink_ret;int nr_before; nr_before =do_shrinker_shrink (shrinker, shrink,0 );
@@ -293,6 +297,8 @@ unsigned long shrink_slab(struct shrink_control *shrink, } shrinker->nr += total_scan; +trace_mm_shrink_slab_end (shrinker, shrink_ret, total_scan, + shrinker->nr); }up_read (&shrinker_rwsem); out:
Signature · 8 lines
1.7.5.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/ Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 02/14] vmscan: add shrink_slab tracepoints
Quoted 7 lines · Dave Chinner
From: Dave Chinner <[email protected]>
??t is impossible to understand what the shrinkers are actually doing without instrumenting the code, so add a some tracepoints to allow insight to be gained.
Signed-off-by Dave Chinner <[email protected]>
Looks good. But wouldn't it be a good idea to give the shrinkers names so that we can pretty print those in the trace event?
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 02/14] vmscan: add shrink_slab tracepoints
Quoted 11 lines · Christoph Hellwig
Quoted 7 lines · Dave Chinner
From: Dave Chinner <[email protected]>
??t is impossible to understand what the shrinkers are actually doing without instrumenting the code, so add a some tracepoints to allow insight to be gained.
Signed-off-by Dave Chinner <[email protected]>Looks good. But wouldn't it be a good idea to give the shrinkers names so that we can pretty print those in the trace event?
Incremental patch below.
Cheers,
Dave.
Signature · 36 lines
Dave Chinner [email protected] vmscan: add shrinker name to shrink_slab tracepoints. From: Dave Chinner <[email protected]> Allow people to see what shrinker the tracepoints belong to by outputing the shrinker function name as well as the shrinker instance. This results in output like: mm_shrink_slab_end: rpcauth_cache_shrinker+0x0 0xffffffff81f9be20 .... mm_shrink_slab_end: mb_cache_shrink_fn+0x0 0xffffffff81f2310 .... mm_shrink_slab_end: shrink_dqcache_memory+0x0 0xffffffff81f2320 .... mm_shrink_slab_end: prune_super+0x0 0xffff88007ce1330 .... mm_shrink_slab_end: nfs_access_cache_shrinker+0x0 0xffffffff81f30aa .... mm_shrink_slab_end: gfs2_shrink_glock_memory+0x0 0xffffffff81f59a8 .... mm_shrink_slab_end: gfs2_shrink_qd_memory+0x0 0xffffffff81f59cc .... mm_shrink_slab_end: prune_super+0x0 0xffff88007c3f930 .... mm_shrink_slab_end: prune_super+0x0 0xffff88007be6bb0 .... mm_shrink_slab_end: prune_super+0x0 0xffff88007be8db0 .... mm_shrink_slab_end: prune_super+0x0 0xffff88007c5bb70 .... mm_shrink_slab_end: prune_super+0x0 0xffff88007be8d70 .... mm_shrink_slab_end: prune_super+0x0 0xffff88007b9c270 .... mm_shrink_slab_end: prune_super+0x0 0xffff88007ba44b0 .... mm_shrink_slab_end: prune_super+0x0 0xffff88007b9bbf0 .... mm_shrink_slab_end: xfs_buftarg_shrink+0x0 0xffff88007b1ef1d8 .... mm_shrink_slab_end: prune_super+0x0 0xffff88007b132f0 .... mm_shrink_slab_end: xfs_buftarg_shrink+0x0 0xffff88007bcda258 .... Signed-off-by: Dave Chinner <[email protected]> --- include/trace/events/vmscan.h | 10 ++++++++-- 1 files changed, 8 insertions(+), 2 deletions(-)
include/trace/events/vmscan.h +8 -2
@@ -190,6 +190,7 @@ TRACE_EVENT(mm_shrink_slab_start,TP_STRUCT__entry (__field (struct shrinker *,shr ) + __field(void *, shrink)__field (long, nr_objects_to_shrink)__field (gfp_t, gfp_flags) __field(unsigned long, pgs_scanned)
@@ -201,6 +202,7 @@ TRACE_EVENT(mm_shrink_slab_start,TP_fast_assign ( __entry->shr = shr; + __entry->shrink = shr->shrink; __entry->nr_objects_to_shrink = nr_objects_to_shrink; __entry->gfp_flags = sc->gfp_mask; __entry->pgs_scanned = pgs_scanned;
@@ -210,7 +212,8 @@ TRACE_EVENT(mm_shrink_slab_start, __entry->total_scan = total_scan; ), - TP_printk("shrinker %p: objects to shrink %ld gfp_flags %s pgs_scanned %ld lru_pgs %ld cache items %ld delta %lld total_scan %ld", + TP_printk("%pF %p: objects to shrink %ld gfp_flags %s pgs_scanned %ld lru_pgs %ld cache items %ld delta %lld total_scan %ld", + __entry->shrink, __entry->shr, __entry->nr_objects_to_shrink, show_gfp_flags(__entry->gfp_flags),
@@ -229,6 +232,7 @@ TRACE_EVENT(mm_shrink_slab_end,TP_STRUCT__entry (__field (struct shrinker *,shr ) + __field(void *, shrink)__field (long, unused_scan)__field (long, new_scan) __field(int, retval)
@@ -237,13 +241,15 @@ TRACE_EVENT(mm_shrink_slab_end,TP_fast_assign ( __entry->shr = shr; + __entry->shrink = shr->shrink; __entry->unused_scan = unused_scan_cnt; __entry->new_scan = new_scan_cnt; __entry->retval = shrinker_retval; __entry->total_scan = new_scan_cnt - unused_scan_cnt; ), - TP_printk("shrinker %p: unused scan count %ld new scan count %ld total_scan %ld last shrinker return val %d", + TP_printk("%pF %p: unused scan count %ld new scan count %ld total_scan %ld last shrinker return val %d", + __entry->shrink, __entry->shr, __entry->unused_scan, __entry->new_scan,
PATCH 3/14 vmscan: shrinker->nr updates race and go wrong
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
shrink_slab() allows shrinkers to be called in parallel so the struct shrinker can be updated concurrently. It does not provide any exclusio for such updates, so we can get the shrinker->nr value increasing or decreasing incorrectly.
As a result, when a shrinker repeatedly returns a value of -1 (e.g. a VFS shrinker called w/ GFP_NOFS), the shrinker->nr goes haywire, sometimes updating with the scan count that wasn't used, sometimes losing it altogether. Worse is when a shrinker does work and that update is lost due to racy updates, which means the shrinker will do the work again!
Fix this by making the total_scan calculations independent of shrinker->nr, and making the shrinker->nr updates atomic w.r.t. to other updates via cmpxchg loops.
--- mm/vmscan.c | 45 ++++++++++++++++++++++++++++++++------------- 1 files changed, 32 insertions(+), 13 deletions(-)
mm/vmscan.c +32 -13
@@ -251,17 +251,29 @@ unsigned long shrink_slab(struct shrink_control *shrink,unsigned long total_scan;unsigned long max_pass;int shrink_ret =0 ; +long nr; +long new_nr; +/* +* copy the current shrinker scan count into a local variable +* and zero it so that other concurrent shrinker invocations +* don't also do this scanning work. +*/ +do { + nr = shrinker->nr; + }while (cmpxchg (&shrinker->nr, nr,0 ) != nr); + + total_scan = nr; max_pass =do_shrinker_shrink (shrinker, shrink,0 ); delta = (4 * nr_pages_scanned) / shrinker->seeks; delta *= max_pass;do_div (delta, lru_pages +1 ); - shrinker->nr += delta; -if (shrinker->nr <0 ) { + total_scan += delta; +if (total_scan <0 ) {printk (KERN_ERR "shrink_slab: %pF negative objects to " "delete nr=%ld\n" , - shrinker->shrink, shrinker->nr); - shrinker->nr = max_pass; + shrinker->shrink, total_scan); + total_scan = max_pass; }/*
@@ -269,13 +281,10 @@ unsigned long shrink_slab(struct shrink_control *shrink,* never try to free more than twice the estimate number of * freeable entries. */ -if (shrinker->nr > max_pass *2 ) - shrinker->nr = max_pass *2 ; - - total_scan = shrinker->nr; - shrinker->nr =0 ; +if (total_scan > max_pass *2 ) + total_scan = max_pass *2 ; -trace_mm_shrink_slab_start (shrinker, shrink, total_scan, +trace_mm_shrink_slab_start (shrinker, shrink, nr, nr_pages_scanned, lru_pages, max_pass, delta, total_scan);
@@ -296,9 +305,19 @@ unsigned long shrink_slab(struct shrink_control *shrink,cond_resched (); } - shrinker->nr += total_scan; -trace_mm_shrink_slab_end (shrinker, shrink_ret, total_scan, - shrinker->nr); +/* +* move the unused scan count back into the shrinker in a +* manner that handles concurrent updates. If we exhausted the +* scan, there is no need to do an update. +*/ +do { + nr = shrinker->nr; + new_nr = total_scan + nr; +if (total_scan <=0 ) +break ; + }while (cmpxchg (&shrinker->nr, nr, new_nr) != nr); + +trace_mm_shrink_slab_end (shrinker, shrink_ret, nr, new_nr); }up_read (&shrinker_rwsem); out:
Signature · 8 lines
1.7.5.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/ Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 4/14 vmscan: reduce wind up shrinker->nr when shrinker can't do work
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
When a shrinker returns -1 to shrink_slab() to indicate it cannot do any work given the current memory reclaim requirements, it adds the entire total_scan count to shrinker->nr. The idea ehind this is that whenteh shrinker is next called and can do work, it will do the work of the previously aborted shrinker call as well.
However, if a filesystem is doing lots of allocation with GFP_NOFS set, then we get many, many more aborts from the shrinkers than we do successful calls. The result is that shrinker->nr winds up to it's maximum permissible value (twice the current cache size) and then when the next shrinker call that can do work is issued, it has enough scan count built up to free the entire cache twice over.
This manifests itself in the cache going from full to empty in a matter of seconds, even when only a small part of the cache is needed to be emptied to free sufficient memory.
Under metadata intensive workloads on ext4 and XFS, I'm seeing the VFS caches increase memory consumption up to 75% of memory (no page cache pressure) over a period of 30-60s, and then the shrinker empties them down to zero in the space of 2-3s. This cycle repeats over and over again, with the shrinker completely trashing the іnode and dentry caches every minute or so the workload continues.
This behaviour was made obvious by the shrink_slab tracepoints added earlier in the series, and made worse by the patch that corrected the concurrent accounting of shrinker->nr.
To avoid this problem, stop repeated small increments of the total scan value from winding shrinker->nr up to a value that can cause the entire cache to be freed. We still need to allow it to wind up, so use the delta as the "large scan" threshold check - if the delta is more than a quarter of the entire cache size, then it is a large scan and allowed to cause lots of windup because we are clearly needing to free lots of memory.
If it isn't a large scan then limit the total scan to half the size of the cache so that windup never increases to consume the whole cache. Reducing the total scan limit further does not allow enough wind-up to maintain the current levels of performance, whilst a higher threshold does not prevent the windup from freeing the entire cache under sustained workloads.
--- mm/vmscan.c | 15 +++++++++++++++ 1 files changed, 15 insertions(+), 0 deletions(-)
mm/vmscan.c +15 -0
@@ -277,6 +277,21 @@ unsigned long shrink_slab(struct shrink_control *shrink, }/* +* We need to avoid excessive windup on filesystem shrinkers +* due to large numbers of GFP_NOFS allocations causing the +* shrinkers to return -1 all the time. This results in a large +* nr being built up so when a shrink that can do some work +* comes along it empties the entire cache due to nr >>> +* max_pass. This is bad for sustaining a working set in +* memory. +* +* Hence only allow the shrinker to scan the entire cache when +* a large delta change is calculated directly. +*/ +if (delta < max_pass /4 ) + total_scan =min (total_scan, max_pass /2 ); + +/* * Avoid risking looping forever due to too large nr value: * never try to free more than twice the estimate number of * freeable entries.
Signature · 8 lines
1.7.5.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/ Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 5/14 vmscan: add customisable shrinker batch size
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
For shrinkers that have their own cond_resched* calls, having shrink_slab break the work down into small batches is not paticularly efficient. Add a custom batchsize field to the struct shrinker so that shrinkers can use a larger batch size if they desire.
A value of zero (uninitialised) means "use the default", so behaviour is unchanged by this patch.
--- include/linux/mm.h | 1 + mm/vmscan.c | 11 ++++++----- 2 files changed, 7 insertions(+), 5 deletions(-)
include/linux/mm.h +1 -0
@@ -1150,6 +1150,7 @@ struct shrink_control {struct shrinker {int (*shrink)(struct shrinker *,struct shrink_control *sc);int seeks;/* seeks to recreate an obj */ +long batch;/* reclaim batch size, 0 = default */ /* These are for internal use */ struct list_head list;
mm/vmscan.c +6 -5
@@ -253,6 +253,8 @@ unsigned long shrink_slab(struct shrink_control *shrink,int shrink_ret =0 ;long nr;long new_nr; +long batch_size = shrinker->batch ? shrinker->batch + :SHRINK_BATCH ;/* * copy the current shrinker scan count into a local variable
@@ -303,19 +305,18 @@ unsigned long shrink_slab(struct shrink_control *shrink, nr_pages_scanned, lru_pages, max_pass, delta, total_scan); -while (total_scan >=SHRINK_BATCH ) { -long this_scan =SHRINK_BATCH ; +while (total_scan >= batch_size) {int nr_before; nr_before =do_shrinker_shrink (shrinker, shrink,0 ); shrink_ret =do_shrinker_shrink (shrinker, shrink, - this_scan); + batch_size);if (shrink_ret ==-1 )break ;if (shrink_ret < nr_before) ret += nr_before - shrink_ret; -count_vm_events (SLABS_SCANNED , this_scan); - total_scan -= this_scan; +count_vm_events (SLABS_SCANNED , batch_size); + total_scan -= batch_size;cond_resched (); }
Signature · 8 lines
1.7.5.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/ Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 6/14 inode: convert inode_stat.nr_unused to per-cpu counters
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
Before we split up the inode_lru_lock, the unused inode counter needs to be made independent of the global inode_lru_lock. Convert it to per-cpu counters to do this.
--- fs/inode.c | 16 +++++++++++----- 1 files changed, 11 insertions(+), 5 deletions(-)
fs/inode.c +11 -5
@@ -95,6 +95,7 @@ EXPORT_SYMBOL(empty_aops);struct inodes_stat_t inodes_stat; static DEFINE_PER_CPU(unsigned int, nr_inodes); +static DEFINE_PER_CPU(unsigned int, nr_unused);static struct kmem_cache *inode_cachep__read_mostly ;
@@ -109,7 +110,11 @@ static int get_nr_inodes(void)static inline int get_nr_inodes_unused (void ) { -return inodes_stat.nr_unused; +int i; +int sum =0 ; +for_each_possible_cpu (i) + sum +=per_cpu (nr_unused, i); +return sum <0 ?0 : sum; }int get_nr_dirty_inodes (void )
@@ -127,6 +132,7 @@ int proc_nr_inodes(ctl_table *table, int write, void __user *buffer, size_t *lenp, loff_t *ppos) { inodes_stat.nr_inodes = get_nr_inodes(); + inodes_stat.nr_unused =get_nr_inodes_unused ();return proc_dointvec (table, write, buffer, lenp, ppos); }#endif
@@ -340,7 +346,7 @@ static void inode_lru_list_add(struct inode *inode)spin_lock (&inode_lru_lock);if (list_empty (&inode->i_lru)) {list_add (&inode->i_lru, &inode_lru); - inodes_stat.nr_unused++; +this_cpu_inc (nr_unused); }spin_unlock (&inode_lru_lock); }
@@ -350,7 +356,7 @@ static void inode_lru_list_del(struct inode *inode)spin_lock (&inode_lru_lock);if (!list_empty (&inode->i_lru)) {list_del_init (&inode->i_lru); - inodes_stat.nr_unused--; +this_cpu_dec (nr_unused); }spin_unlock (&inode_lru_lock); }
@@ -649,7 +655,7 @@ static void prune_icache(int nr_to_scan) (inode->i_state & ~I_REFERENCED)) {list_del_init (&inode->i_lru);spin_unlock (&inode->i_lock); - inodes_stat.nr_unused--; +this_cpu_dec (nr_unused);continue ; }
@@ -686,7 +692,7 @@ static void prune_icache(int nr_to_scan)spin_unlock (&inode->i_lock);list_move (&inode->i_lru, &freeable); - inodes_stat.nr_unused--; +this_cpu_dec (nr_unused); }if (current_is_kswapd ())__count_vm_events (KSWAPD_INODESTEAL , reap);
Signature · 8 lines
1.7.5.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/ Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 7/14 inode: Make unused inode LRU per superblock
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
The inode unused list is currently a global LRU. This does not match the other global filesystem cache - the dentry cache - which uses per-superblock LRU lists. Hence we have related filesystem object types using different LRU reclaimation schemes.
To enable a per-superblock filesystem cache shrinker, both of these caches need to have per-sb unused object LRU lists. Hence this patch converts the global inode LRU to per-sb LRUs.
The patch only does rudimentary per-sb propotioning in the shrinker infrastructure, as this gets removed when the per-sb shrinker callouts are introduced later on.
--- fs/inode.c | 91 +++++++++++++++++++++++++++++++++++++++++++++------ fs/super.c | 1 + include/linux/fs.h | 4 ++ 3 files changed, 85 insertions(+), 11 deletions(-)
fs/inode.c +80 -11
@@ -34,7 +34,7 @@* inode->i_lock protects: * inode->i_state, inode->i_hash, __iget() * inode_lru_lock protects: -* inode_lru, inode->i_lru +* inode->i_sb->s_inode_lru, inode->i_lru * inode_sb_list_lock protects: * sb->s_inodes, inode->i_sb_list * inode_wb_list_lock protects:
@@ -64,7 +64,6 @@ static unsigned int i_hash_shift __read_mostly;static struct hlist_head *inode_hashtable__read_mostly ;static __cacheline_aligned_in_smp DEFINE_SPINLOCK (inode_hash_lock ); -static LIST_HEAD (inode_lru);static DEFINE_SPINLOCK (inode_lru_lock);__cacheline_aligned_in_smp DEFINE_SPINLOCK (inode_sb_list_lock );
@@ -345,7 +344,8 @@ static void inode_lru_list_add(struct inode *inode) {spin_lock (&inode_lru_lock);if (list_empty (&inode->i_lru)) { -list_add (&inode->i_lru, &inode_lru); +list_add (&inode->i_lru, &inode->i_sb->s_inode_lru); + inode->i_sb->s_nr_inodes_unused++;this_cpu_inc (nr_unused); }spin_unlock (&inode_lru_lock);
@@ -356,6 +356,7 @@ static void inode_lru_list_del(struct inode *inode)spin_lock (&inode_lru_lock);if (!list_empty (&inode->i_lru)) {list_del_init (&inode->i_lru); + inode->i_sb->s_nr_inodes_unused--;this_cpu_dec (nr_unused); }spin_unlock (&inode_lru_lock);
@@ -621,21 +622,20 @@ static int can_unuse(struct inode *inode)* LRU does not have strict ordering. Hence we don't want to reclaim inodes * with this flag set because they are the inodes that are out of order. */ -static void prune_icache (int nr_to_scan) +static void shrink_icache_sb (struct super_block *sb,int *nr_to_scan) {LIST_HEAD (freeable);int nr_scanned;unsigned long reap =0 ; -down_read (&iprune_sem);spin_lock (&inode_lru_lock); -for (nr_scanned =0 ; nr_scanned < nr_to_scan; nr_scanned++) { +for (nr_scanned = *nr_to_scan; nr_scanned >=0 ; nr_scanned--) {struct inode *inode; -if (list_empty (&inode_lru)) +if (list_empty (&sb->s_inode_lru))break ; - inode = list_entry(inode_lru.prev, struct inode, i_lru); + inode = list_entry(sb->s_inode_lru.prev, struct inode, i_lru);/* * we are inverting the inode_lru_lock/inode->i_lock here,
@@ -643,7 +643,7 @@ static void prune_icache(int nr_to_scan)* inode to the back of the list so we don't spin on it. */ if (!spin_trylock (&inode->i_lock)) { -list_move (&inode->i_lru, &inode_lru); +list_move (&inode->i_lru, &sb->s_inode_lru);continue ; }
@@ -655,6 +655,7 @@ static void prune_icache(int nr_to_scan) (inode->i_state & ~I_REFERENCED)) {list_del_init (&inode->i_lru);spin_unlock (&inode->i_lock); + sb->s_nr_inodes_unused--;this_cpu_dec (nr_unused);continue ; }
@@ -662,7 +663,7 @@ static void prune_icache(int nr_to_scan)/* recently referenced inodes get one more pass */ if (inode->i_state &I_REFERENCED ) { inode->i_state &= ~I_REFERENCED ; -list_move (&inode->i_lru, &inode_lru); +list_move (&inode->i_lru, &sb->s_inode_lru);spin_unlock (&inode->i_lock);continue ; }
@@ -676,7 +677,7 @@ static void prune_icache(int nr_to_scan)iput (inode);spin_lock (&inode_lru_lock); -if (inode !=list_entry (inode_lru.next, +if (inode !=list_entry (sb->s_inode_lru.next, struct inode, i_lru))continue ;/* wrong inode or list_empty */ /* avoid lock inversions with trylock */
@@ -692,6 +693,7 @@ static void prune_icache(int nr_to_scan)spin_unlock (&inode->i_lock);list_move (&inode->i_lru, &freeable); + sb->s_nr_inodes_unused--;this_cpu_dec (nr_unused); }if (current_is_kswapd ())
@@ -699,8 +701,75 @@ static void prune_icache(int nr_to_scan)else __count_vm_events (PGINODESTEAL , reap);spin_unlock (&inode_lru_lock); + *nr_to_scan = nr_scanned;dispose_list (&freeable); +} + +static void prune_icache (int count) +{ +struct super_block *sb, *p =NULL ; +int w_count; +int unused = inodes_stat.nr_unused; +int prune_ratio; +int pruned; + +if (unused ==0 || count ==0 ) +return ; +down_read (&iprune_sem); +if (count >= unused) + prune_ratio =1 ; +else + prune_ratio = unused / count; +spin_lock (&sb_lock); +list_for_each_entry (sb, &super_blocks, s_list) { +if (list_empty (&sb->s_instances)) +continue ; +if (sb->s_nr_inodes_unused ==0 ) +continue ; + sb->s_count++; +/* Now, we reclaim unused dentrins with fairness. +* We reclaim them same percentage from each superblock. +* We calculate number of dentries to scan on this sb +* as follows, but the implementation is arranged to avoid +* overflows: +* number of dentries to scan on this sb = +* count * (number of dentries on this sb / +* number of dentries in the machine) +*/ +spin_unlock (&sb_lock); +if (prune_ratio !=1 ) + w_count = (sb->s_nr_inodes_unused / prune_ratio) +1 ; +else + w_count = sb->s_nr_inodes_unused; + pruned = w_count; +/* +* We need to be sure this filesystem isn't being unmounted, +* otherwise we could race with generic_shutdown_super(), and +* end up holding a reference to an inode while the filesystem +* is unmounted. So we try to get s_umount, and make sure +* s_root isn't NULL. +*/ +if (down_read_trylock (&sb->s_umount)) { +if ((sb->s_root !=NULL ) && + (!list_empty (&sb->s_dentry_lru))) { +shrink_icache_sb (sb, &w_count); + pruned -= w_count; + } +up_read (&sb->s_umount); + } +spin_lock (&sb_lock); +if (p) +__put_super (p); + count -= pruned; + p = sb; +/* more work left to do? */ +if (count <=0 ) +break ; + } +if (p) +__put_super (p); +spin_unlock (&sb_lock);up_read (&iprune_sem); }
fs/super.c +1 -0
@@ -77,6 +77,7 @@ static struct super_block *alloc_super(struct file_system_type *type)INIT_HLIST_BL_HEAD (&s->s_anon);INIT_LIST_HEAD (&s->s_inodes);INIT_LIST_HEAD (&s->s_dentry_lru); +INIT_LIST_HEAD (&s->s_inode_lru);init_rwsem (&s->s_umount);mutex_init (&s->s_lock);lockdep_set_class (&s->s_umount, &type->s_umount_key);
include/linux/fs.h +4 -0
@@ -1396,6 +1396,10 @@ struct super_block {struct list_head s_dentry_lru;/* unused dentry lru */ int s_nr_dentry_unused;/* # of dentry on lru */ +/* inode_lru_lock protects s_inode_lru and s_nr_inodes_unused */ +struct list_head s_inode_lru;/* unused inode lru */ +int s_nr_inodes_unused;/* # of inodes on lru */ +struct block_device *s_bdev;struct backing_dev_info *s_bdi;struct mtd_info *s_mtd;
Signature · 8 lines
1.7.5.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/ Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 8/14 inode: move to per-sb LRU locks
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
With the inode LRUs moving to per-sb structures, there is no longer a need for a global inode_lru_lock. The locking can be made more fine-grained by moving to a per-sb LRU lock, isolating the LRU operations of different filesytsems completely from each other.
--- fs/inode.c | 27 +++++++++++++-------------- fs/super.c | 1 + include/linux/fs.h | 3 ++- 3 files changed, 16 insertions(+), 15 deletions(-)
fs/inode.c +13 -14
@@ -33,7 +33,7 @@* * inode->i_lock protects: * inode->i_state, inode->i_hash, __iget() -* inode_lru_lock protects: +* inode->i_sb->s_inode_lru_lock protects: * inode->i_sb->s_inode_lru, inode->i_lru * inode_sb_list_lock protects: * sb->s_inodes, inode->i_sb_list
@@ -46,7 +46,7 @@* * inode_sb_list_lock * inode->i_lock -* inode_lru_lock +* inode->i_sb->s_inode_lru_lock * * inode_wb_list_lock * inode->i_lock
@@ -64,8 +64,6 @@ static unsigned int i_hash_shift __read_mostly;static struct hlist_head *inode_hashtable__read_mostly ;static __cacheline_aligned_in_smp DEFINE_SPINLOCK (inode_hash_lock ); -static DEFINE_SPINLOCK (inode_lru_lock); -__cacheline_aligned_in_smp DEFINE_SPINLOCK (inode_sb_list_lock );__cacheline_aligned_in_smp DEFINE_SPINLOCK (inode_wb_list_lock );
@@ -342,24 +340,24 @@ EXPORT_SYMBOL(ihold);static void inode_lru_list_add (struct inode *inode) { -spin_lock (&inode_lru_lock); +spin_lock (&inode->i_sb->s_inode_lru_lock);if (list_empty (&inode->i_lru)) {list_add (&inode->i_lru, &inode->i_sb->s_inode_lru); inode->i_sb->s_nr_inodes_unused++;this_cpu_inc (nr_unused); } -spin_unlock (&inode_lru_lock); +spin_unlock (&inode->i_sb->s_inode_lru_lock); }static void inode_lru_list_del (struct inode *inode) { -spin_lock (&inode_lru_lock); +spin_lock (&inode->i_sb->s_inode_lru_lock);if (!list_empty (&inode->i_lru)) {list_del_init (&inode->i_lru); inode->i_sb->s_nr_inodes_unused--;this_cpu_dec (nr_unused); } -spin_unlock (&inode_lru_lock); +spin_unlock (&inode->i_sb->s_inode_lru_lock); }/**
@@ -608,7 +606,8 @@ static int can_unuse(struct inode *inode)/* * Scan `goal' inodes on the unused list for freeable ones. They are moved to a -* temporary list and then are freed outside inode_lru_lock by dispose_list(). +* temporary list and then are freed outside sb->s_inode_lru_lock by +* dispose_list(). * * Any inodes which are pinned purely because of attached pagecache have their * pagecache removed. If the inode has metadata buffers attached to
@@ -628,7 +627,7 @@ static void shrink_icache_sb(struct super_block *sb, int *nr_to_scan)int nr_scanned;unsigned long reap =0 ; -spin_lock (&inode_lru_lock); +spin_lock (&sb->s_inode_lru_lock);for (nr_scanned = *nr_to_scan; nr_scanned >=0 ; nr_scanned--) {struct inode *inode;
@@ -638,7 +637,7 @@ static void shrink_icache_sb(struct super_block *sb, int *nr_to_scan) inode = list_entry(sb->s_inode_lru.prev, struct inode, i_lru);/* -* we are inverting the inode_lru_lock/inode->i_lock here, +* we are inverting the sb->s_inode_lru_lock/inode->i_lock here, * so use a trylock. If we fail to get the lock, just move the * inode to the back of the list so we don't spin on it. */
@@ -670,12 +669,12 @@ static void shrink_icache_sb(struct super_block *sb, int *nr_to_scan)if (inode_has_buffers (inode) || inode->i_data.nrpages) {__iget (inode);spin_unlock (&inode->i_lock); -spin_unlock (&inode_lru_lock); +spin_unlock (&sb->s_inode_lru_lock);if (remove_inode_buffers (inode)) reap +=invalidate_mapping_pages (&inode->i_data,0 ,-1 );iput (inode); -spin_lock (&inode_lru_lock); +spin_lock (&sb->s_inode_lru_lock);if (inode !=list_entry (sb->s_inode_lru.next, struct inode, i_lru))
@@ -700,7 +699,7 @@ static void shrink_icache_sb(struct super_block *sb, int *nr_to_scan)__count_vm_events (KSWAPD_INODESTEAL , reap); else__count_vm_events (PGINODESTEAL ,reap ); -spin_unlock (&inode_lru_lock); +spin_unlock (&sb->s_inode_lru_lock); *nr_to_scan = nr_scanned;dispose_list (&freeable);
fs/super.c +1 -0
@@ -78,6 +78,7 @@ static struct super_block *alloc_super(struct file_system_type *type)INIT_LIST_HEAD (&s->s_inodes);INIT_LIST_HEAD (&s->s_dentry_lru);INIT_LIST_HEAD (&s->s_inode_lru); +spin_lock_init (&s->s_inode_lru_lock);init_rwsem (&s->s_umount);mutex_init (&s->s_lock);lockdep_set_class (&s->s_umount, &type->s_umount_key);
include/linux/fs.h +2 -1
@@ -1396,7 +1396,8 @@ struct super_block {struct list_head s_dentry_lru;/* unused dentry lru */ int s_nr_dentry_unused;/* # of dentry on lru */ -/* inode_lru_lock protects s_inode_lru and s_nr_inodes_unused */ +/* s_inode_lru_lock protects s_inode_lru and s_nr_inodes_unused */ +spinlock_t s_inode_lru_lock____cacheline_aligned_in_smp ;struct list_head s_inode_lru;/* unused inode lru */ int s_nr_inodes_unused;/* # of inodes on lru */
Signature · 8 lines
1.7.5.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/ Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 08/14] inode: move to per-sb LRU locks
Quoted 6 lines · Dave Chinner
From: Dave Chinner <[email protected]>
With the inode LRUs moving to per-sb structures, there is no longer a need for a global
inode_lru_lock. The locking can be made more fine-grained by moving to a per-sb LRU lock, isolating the LRU operations of different filesytsems completely from each other.
Btw, any reason this is not done for dcache_lru_lock?
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 08/14] inode: move to per-sb LRU locks
Quoted 9 lines · Christoph Hellwig
Quoted 6 lines · Dave Chinner
From: Dave Chinner <[email protected]>
With the inode LRUs moving to per-sb structures, there is no longer a need for a global
inode_lru_lock. The locking can be made more fine-grained by moving to a per-sb LRU lock, isolating the LRU operations of different filesytsems completely from each other.Btw, any reason this is not done for
dcache_lru_lock?
I have a patch that does exactly that, but it causes random crashes and oopsen in the dentry code due to corrupted lists and dentries when running xfstests. Like the inode cache, it is a simple translation of dcache_lru_lock to sb->s_dentry_lru_lock, so it should not be changing what is protected by the LRU lock.
The patch used to work fine before the massive locking rework of the dentry cache, so it appears that there is now something unknown that dcache_lru_lock is implicitly protecting. However, the locking is now so complex I've been unable to debug the problems caused by the patch, so I simply dropped the patch.
I might try again later to break up the dcache_lru_lock, but right now it's not showing up as a badly contended lock in my tests and I've got other things that are more important to do, so I've been ignoring the problem....
Cheers,
Dave.
Signature · 9 lines
Dave Chinner [email protected] -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/ Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 9/14 superblock: move pin_sb_for_writeback() to fs/super.c
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
The per-sb shrinker has the same requirement as the writeback threads of ensuring that the superblock is usable and pinned for the time it takes to run the work. Both need to take a passive reference to the sb, take a read lock on the s_umount lock and then only continue if an unmount is not in progress.
pin_sb_for_writeback() does this exactly, so move it to fs/super.c and rename it to grab_super_passive() and exporting it via fs/internal.h for all the VFS code to be able to use.
--- fs/fs-writeback.c | 28 +--------------------------- fs/internal.h | 1 + fs/super.c | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 27 deletions(-)
fs/fs-writeback.c +1 -27
@@ -461,32 +461,6 @@ writeback_single_inode(struct inode *inode, struct writeback_control *wbc) }/* -* For background writeback the caller does not have the sb pinned -* before calling writeback. So make sure that we do pin it, so it doesn't -* go away while we are writing inodes from it. -*/ -static bool pin_sb_for_writeback (struct super_block *sb) -{ -spin_lock (&sb_lock); -if (list_empty (&sb->s_instances)) { -spin_unlock (&sb_lock); -return false; - } - - sb->s_count++; -spin_unlock (&sb_lock); - -if (down_read_trylock (&sb->s_umount)) { -if (sb->s_root) -return true; -up_read (&sb->s_umount); - } - -put_super (sb); -return false; -} - -/* * Write a portion of b_io inodes which belong to @sb. * * If @only_this_sb is true, then find and write all such
@@ -585,7 +559,7 @@ void writeback_inodes_wb(struct bdi_writeback *wb,struct inode *inode =wb_inode (wb->b_io.prev);struct super_block *sb = inode->i_sb; -if (!pin_sb_for_writeback (sb)) { +if (!grab_super_passive (sb)) {requeue_io (inode);continue ; }
fs/internal.h +1 -0
@@ -97,6 +97,7 @@ extern struct file *get_empty_filp(void);* super.c */ extern int do_remount_sb (struct super_block *,int ,void *,int ); +extern bool grab_super_passive (struct super_block *sb);extern void __put_super (struct super_block *sb);extern void put_super (struct super_block *sb); extern struct dentry *mount_fs(struct file_system_type *,
fs/super.c +33 -0
@@ -243,6 +243,39 @@ static int grab_super(struct super_block *s) __releases(sb_lock) }/* +* grab_super_passive - acquire a passive reference +* @s: reference we are trying to grab +* +* Tries to acquire a passive reference. This is used in places where we +* cannot take an active reference but we need to ensure that the +* superblock does not go away while we are working on it. It returns +* false if a reference was not gained, and returns true with the s_umount +* lock held in read mode if a reference is gained. On successful return, +* the caller must drop the s_umount lock and the passive reference when +* done. +*/ +bool grab_super_passive (struct super_block *sb) +{ +spin_lock (&sb_lock); +if (list_empty (&sb->s_instances)) { +spin_unlock (&sb_lock); +return false; + } + + sb->s_count++; +spin_unlock (&sb_lock); + +if (down_read_trylock (&sb->s_umount)) { +if (sb->s_root) +return true; +up_read (&sb->s_umount); + } + +put_super (sb); +return false; +} + +/* * Superblock locking. We really ought to get rid of these two. */ void lock_super (struct super_block * sb)
Signature · 8 lines
1.7.5.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/ Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 10/14 superblock: introduce per-sb cache shrinker infrastructure
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
With context based shrinkers, we can implement a per-superblock shrinker that shrinks the caches attached to the superblock. We currently have global shrinkers for the inode and dentry caches that split up into per-superblock operations via a coarse proportioning method that does not batch very well. The global shrinkers also have a dependency - dentries pin inodes - so we have to be very careful about how we register the global shrinkers so that the implicit call order is always correct.
With a per-sb shrinker callout, we can encode this dependency directly into the per-sb shrinker, hence avoiding the need for strictly ordering shrinker registrations. We also have no need for any proportioning code for the shrinker subsystem already provides this functionality across all shrinkers. Allowing the shrinker to operate on a single superblock at a time means that we do less superblock list traversals and locking and reclaim should batch more effectively. This should result in less CPU overhead for reclaim and potentially faster reclaim of items from each filesystem.
--- fs/dcache.c | 121 +++++---------------------------------------------- fs/inode.c | 117 ++++---------------------------------------------- fs/super.c | 52 ++++++++++++++++++++++- include/linux/fs.h | 7 +++ 4 files changed, 79 insertions(+), 218 deletions(-)
fs/dcache.c +12 -109
@@ -743,13 +743,11 @@ static void shrink_dentry_list(struct list_head *list)* * If flags contains DCACHE_REFERENCED reference dentries will not be pruned. */ -static void __shrink_dcache_sb (struct super_block *sb,int *count,int flags) +static void __shrink_dcache_sb (struct super_block *sb,int count,int flags) { -/* called from prune_dcache() and shrink_dcache_parent() */ struct dentry *dentry;LIST_HEAD (referenced);LIST_HEAD (tmp); -int cnt = *count; relock:spin_lock (&dcache_lru_lock);
@@ -777,7 +775,7 @@ relock: }else {list_move_tail (&dentry->d_lru, &tmp);spin_unlock (&dentry->d_lock); -if (!--cnt) +if (!--count)break ; }cond_resched_lock (&dcache_lru_lock);
@@ -787,83 +785,22 @@ relock:spin_unlock (&dcache_lru_lock);shrink_dentry_list (&tmp); - - *count = cnt; }/** -* prune_dcache - shrink the dcache -* @count: number of entries to try to free +* prune_dcache_sb - shrink the dcache +* @nr_to_scan: number of entries to try to free * -* Shrink the dcache. This is done when we need more memory, or simply when we -* need to unmount something (at which point we need to unuse all dentries). +* Attempt to shrink the superblock dcache LRU by @nr_to_scan entries. This is +* done when we need more memory an called from the superblock shrinker +* function. * -* This function may fail to free any resources if all the dentries are in use. +* This function may fail to free any resources if all the dentries are in +* use. */ -static void prune_dcache (int count) +void prune_dcache_sb (struct super_block *sb,int nr_to_scan) { -struct super_block *sb, *p =NULL ; -int w_count; -int unused = dentry_stat.nr_unused; -int prune_ratio; -int pruned; - -if (unused ==0 || count ==0 ) -return ; -if (count >= unused) - prune_ratio =1 ; -else - prune_ratio = unused / count; -spin_lock (&sb_lock); -list_for_each_entry (sb, &super_blocks, s_list) { -if (list_empty (&sb->s_instances)) -continue ; -if (sb->s_nr_dentry_unused ==0 ) -continue ; - sb->s_count++; -/* Now, we reclaim unused dentrins with fairness. -* We reclaim them same percentage from each superblock. -* We calculate number of dentries to scan on this sb -* as follows, but the implementation is arranged to avoid -* overflows: -* number of dentries to scan on this sb = -* count * (number of dentries on this sb / -* number of dentries in the machine) -*/ -spin_unlock (&sb_lock); -if (prune_ratio !=1 ) - w_count = (sb->s_nr_dentry_unused / prune_ratio) +1 ; -else - w_count = sb->s_nr_dentry_unused; - pruned = w_count; -/* -* We need to be sure this filesystem isn't being unmounted, -* otherwise we could race with generic_shutdown_super(), and -* end up holding a reference to an inode while the filesystem -* is unmounted. So we try to get s_umount, and make sure -* s_root isn't NULL. -*/ -if (down_read_trylock (&sb->s_umount)) { -if ((sb->s_root !=NULL ) && - (!list_empty (&sb->s_dentry_lru))) { -__shrink_dcache_sb (sb, &w_count, -DCACHE_REFERENCED ); - pruned -= w_count; - } -up_read (&sb->s_umount); - } -spin_lock (&sb_lock); -if (p) -__put_super (p); - count -= pruned; - p = sb; -/* more work left to do? */ -if (count <=0 ) -break ; - } -if (p) -__put_super (p); -spin_unlock (&sb_lock); +__shrink_dcache_sb (sb, nr_to_scan,DCACHE_REFERENCED ); }/**
@@ -1238,42 +1175,10 @@ void shrink_dcache_parent(struct dentry * parent)int found;while ((found =select_parent (parent)) !=0 ) -__shrink_dcache_sb (sb, &found,0 ); +__shrink_dcache_sb (sb, found,0 ); }EXPORT_SYMBOL (shrink_dcache_parent); -/* -* Scan `sc->nr_slab_to_reclaim' dentries and return the number which remain. -* -* We need to avoid reentering the filesystem if the caller is performing a -* GFP_NOFS allocation attempt. One example deadlock is: -* -* ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache-> -* prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode-> -* ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK. -* -* In this case we return -1 to tell the caller that we baled. -*/ -static int shrink_dcache_memory (struct shrinker *shrink, -struct shrink_control *sc) -{ -int nr = sc->nr_to_scan; -gfp_t gfp_mask = sc->gfp_mask; - -if (nr) { -if (!(gfp_mask & __GFP_FS)) -return -1 ; -prune_dcache (nr); - } - -return (dentry_stat.nr_unused /100 ) * sysctl_vfs_cache_pressure; -} - -static struct shrinker dcache_shrinker = { - .shrink = shrink_dcache_memory, - .seeks =DEFAULT_SEEKS , -}; -/** * __d_alloc - allocate a dcache entry * @sb: filesystem it will belong to
@@ -3069,8 +2974,6 @@ static void __init dcache_init(void)*/ dentry_cache =KMEM_CACHE (dentry,SLAB_RECLAIM_ACCOUNT |SLAB_PANIC |SLAB_MEM_SPREAD ); - -register_shrinker (&dcache_shrinker);/* Hash may have been set up in dcache_init_early */ if (!hashdist)
fs/inode.c +9 -108
164 lines not on this page · raw message
fs/super.c +51 -1
82 lines not on this page · raw message
include/linux/fs.h +7 -0
@@ -394,6 +394,7 @@ struct inodes_stat_t {#include <linux/fiemap.h> #include <linux/rculist_bl.h> #include <linux/atomic.h> +#include <linux/mm.h> #include <asm/byteorder.h>
@@ -1443,8 +1444,14 @@ struct super_block {* Saved pool identifier for cleancache (-1 means none) */ int cleancache_poolid; + +struct shrinker s_shrink;/* per-sb shrinker handle */ }; +/* superblock cache pruning functions */ +extern void prune_icache_sb (struct super_block *sb,int nr_to_scan); +extern void prune_dcache_sb (struct super_block *sb,int nr_to_scan); +extern struct timespec current_fs_time (struct super_block *sb);/*
Signature · 8 lines
1.7.5.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/ Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 11/14 inode: remove iprune_sem
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
Now that we have per-sb shrinkers with a lifecycle that is a subset of the superblock lifecycle and can reliably detect a filesystem being unmounted, there is not longer any race condition for the iprune_sem to protect against. Hence we can remove it.
--- fs/inode.c | 21 --------------------- 1 files changed, 0 insertions(+), 21 deletions(-)
fs/inode.c +0 -21
@@ -68,17 +68,6 @@ __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_sb_list_lock);__cacheline_aligned_in_smp DEFINE_SPINLOCK (inode_wb_list_lock );/* -* iprune_sem provides exclusion between the icache shrinking and the -* umount path. -* -* We don't actually need it to protect anything in the umount path, -* but only need to cycle through it to make sure any inode that -* prune_icache_sb took off the LRU list has been fully torn down by the -* time we are past evict_inodes. -*/ -static DECLARE_RWSEM (iprune_sem); - -/* * Empty aops. Can be used for the cases where the user does not * define any of the address_space operations. */
@@ -535,14 +524,6 @@ void evict_inodes(struct super_block *sb)spin_unlock (&inode_sb_list_lock);dispose_list (&dispose); - -/* -* Cycle through iprune_sem to make sure any inode that prune_icache_sb -* moved off the list before we took the lock has been fully torn -* down. -*/ -down_write (&iprune_sem); -up_write (&iprune_sem); }/**
@@ -628,7 +609,6 @@ void prune_icache_sb(struct super_block *sb, int nr_to_scan)int nr_scanned;unsigned long reap =0 ; -down_read (&iprune_sem);spin_lock (&sb->s_inode_lru_lock);for (nr_scanned = nr_to_scan; nr_scanned >=0 ; nr_scanned--) {struct inode *inode;
@@ -704,7 +684,6 @@ void prune_icache_sb(struct super_block *sb, int nr_to_scan)spin_unlock (&sb->s_inode_lru_lock);dispose_list (&freeable); -up_read (&iprune_sem); }static void __wait_on_freeing_inode (struct inode *inode);
Signature · 8 lines
1.7.5.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/ Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 12/14 superblock: add filesystem shrinker operations
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
Now we have a per-superblock shrinker implementation, we can add a filesystem specific callout to it to allow filesystem internal caches to be shrunk by the superblock shrinker.
Rather than perpetuate the multipurpose shrinker callback API (i.e. nr_to_scan == 0 meaning "tell me how many objects freeable in the cache), two operations will be added. The first will return the number of objects that are freeable, the second is the actual shrinker call.
--- Documentation/filesystems/vfs.txt | 16 +++++++++++++ fs/super.c | 43 +++++++++++++++++++++++++++--------- include/linux/fs.h | 2 + 3 files changed, 50 insertions(+), 11 deletions(-)
Documentation/filesystems/vfs.txt +16 -0
@@ -229,6 +229,8 @@ struct super_operations { ssize_t (*quota_read)(struct super_block *, int, char *, size_t, loff_t); ssize_t (*quota_write)(struct super_block *, int, const char *, size_t, loff_t); + int (*nr_cached_objects)(struct super_block *); + void (*free_cached_objects)(struct super_block *, int); }; All methods are called without any locks being held, unless otherwise
@@ -301,6 +303,20 @@ or bottom half). quota_write: called by the VFS to write to filesystem quota file. + nr_cached_objects: called by the sb cache shrinking function for the + filesystem to return the number of freeable cached objects it contains. + Optional. + + free_cache_objects: called by the sb cache shrinking function for the + filesystem to scan the number of objects indicated to try to free them. + Optional, but any filesystem implementing this method needs to also + implement ->nr_cached_objects for it to be called correctly. + + We can't do anything with any errors that the filesystem might + encountered, hence the void return type. This will never be called if + the VM is trying to reclaim under GFP_NOFS conditions, hence this + method does not need to handle that situation itself. + Whoever sets up the inode is responsible for filling in the "i_op" field. This is a pointer to a "struct inode_operations" which describes the methods that can be performed on individual inodes.
fs/super.c +33 -12
63 lines not on this page · raw message
include/linux/fs.h +2 -0
@@ -1654,6 +1654,8 @@ struct super_operations {ssize_t (*quota_write)(struct super_block *,int ,const char *,size_t ,loff_t );#endif int (*bdev_try_to_free_page)(struct super_block *,struct page *,gfp_t ); +int (*nr_cached_objects)(struct super_block *); +void (*free_cached_objects)(struct super_block *,int ); };/*
Signature · 8 lines
1.7.5.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/ Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 13/14 vfs: increase shrinker batch size
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
Now that the per-sb shrinker is responsible for shrinking 2 or more caches, increase the batch size to keep econmies of scale for shrinking each cache. Increase the shrinker batch size to 1024 objects.
To allow for a large increase in batch size, add a conditional reschedule to prune_icache_sb() so that we don't hold the LRU spin lock for too long. This mirrors the behaviour of the __shrink_dcache_sb(), and allows us to increase the batch size without needing to worry about problems caused by long lock hold times.
To ensure that filesystems using the per-sb shrinker callouts don't cause problems, document that the object freeing method must reschedule appropriately inside loops.
--- Documentation/filesystems/vfs.txt | 6 ++++++ fs/super.c | 1 + 2 files changed, 7 insertions(+), 0 deletions(-)
Documentation/filesystems/vfs.txt +6 -0
@@ -317,6 +317,12 @@ or bottom half). the VM is trying to reclaim under GFP_NOFS conditions, hence this method does not need to handle that situation itself. + Implementations must include conditional reschedule calls inside any + scanning loop that is done. This allows the VFS to determine + appropriate scan batch sizes without having to worry about whether + implementations will cause holdoff problems due to large scan batch + sizes. + Whoever sets up the inode is responsible for filling in the "i_op" field. This is a pointer to a "struct inode_operations" which describes the methods that can be performed on individual inodes.
fs/super.c +1 -0
@@ -183,6 +183,7 @@ static struct super_block *alloc_super(struct file_system_type *type) s->s_shrink.seeks =DEFAULT_SEEKS ; s->s_shrink.shrink = prune_super; + s->s_shrink.batch =1024 ; } out:return s;
Signature · 8 lines
1.7.5.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/ Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 13/14] vfs: increase shrinker batch size
Quoted 6 lines · Dave Chinner
To allow for a large increase in batch size, add a conditional reschedule to
prune_icache_sb()so that we don't hold the LRU spin lock for too long. This mirrors the behaviour of the__shrink_dcache_sb(), and allows us to increase the batch size without needing to worry about problems caused by long lock hold times.
That doesn't reflect what the patch actually does.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 14/14 xfs: make use of new shrinker callout for the inode cache
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
Convert the inode reclaim shrinker to use the new per-sb shrinker operations. This allows much bigger reclaim batches to be used, and allows the XFS inode cache to be shrunk in proportion with the VFS dentry and inode caches. This avoids the problem of the VFS caches being shrunk significantly before the XFS inode cache is shrunk resulting in imbalances in the caches during reclaim.
--- fs/xfs/linux-2.6/xfs_super.c | 26 ++++++++++----- fs/xfs/linux-2.6/xfs_sync.c | 71 ++++++++++++++++-------------------------- fs/xfs/linux-2.6/xfs_sync.h | 5 +-- 3 files changed, 46 insertions(+), 56 deletions(-)
fs/xfs/linux-2.6/xfs_super.c +17 -9
@@ -1025,11 +1025,6 @@ xfs_fs_put_super( {struct xfs_mount *mp =XFS_M (sb); -/* -* Unregister the memory shrinker before we tear down the mount -* structure so we don't have memory reclaim racing with us here. -*/ -xfs_inode_shrinker_unregister (mp);xfs_syncd_stop (mp);/*
@@ -1416,8 +1411,6 @@ xfs_fs_fill_super(if (error) goto out_filestream_unmount; -xfs_inode_shrinker_register (mp); - error =xfs_mountfs (mp);if (error) goto out_syncd_stop;
@@ -1440,7 +1433,6 @@ xfs_fs_fill_super(return 0 ; out_syncd_stop: -xfs_inode_shrinker_unregister (mp);xfs_syncd_stop (mp); out_filestream_unmount:xfs_filestream_unmount (mp);
@@ -1465,7 +1457,6 @@ xfs_fs_fill_super( } fail_unmount: -xfs_inode_shrinker_unregister (mp);xfs_syncd_stop (mp);/*
@@ -1491,6 +1482,21 @@ xfs_fs_mount(return mount_bdev (fs_type, flags, dev_name, data, xfs_fs_fill_super); } +static int +xfs_fs_nr_cached_objects ( +struct super_block *sb) +{ +return xfs_reclaim_inodes_count (XFS_M (sb)); +} + +static void +xfs_fs_free_cached_objects ( +struct super_block *sb, +int nr_to_scan) +{ +xfs_reclaim_inodes_nr (XFS_M (sb), nr_to_scan); +} +static const struct super_operations xfs_super_operations = { .alloc_inode = xfs_fs_alloc_inode, .destroy_inode = xfs_fs_destroy_inode,
@@ -1504,6 +1510,8 @@ static const struct super_operations xfs_super_operations = { .statfs = xfs_fs_statfs, .remount_fs = xfs_fs_remount, .show_options = xfs_fs_show_options, + .nr_cached_objects = xfs_fs_nr_cached_objects, + .free_cached_objects = xfs_fs_free_cached_objects, };static struct file_system_type xfs_fs_type = {
fs/xfs/linux-2.6/xfs_sync.c +27 -44
108 lines not on this page · raw message
fs/xfs/linux-2.6/xfs_sync.h +2 -3
@@ -43,6 +43,8 @@ void xfs_quiesce_attr(struct xfs_mount *mp);void xfs_flush_inodes (struct xfs_inode *ip);int xfs_reclaim_inodes (struct xfs_mount *mp,int mode); +int xfs_reclaim_inodes_count (struct xfs_mount *mp); +void xfs_reclaim_inodes_nr (struct xfs_mount *mp,int nr_to_scan);void xfs_inode_set_reclaim_tag (struct xfs_inode *ip);void __xfs_inode_set_reclaim_tag (struct xfs_perag *pag,struct xfs_inode *ip);
@@ -54,7 +56,4 @@ int xfs_inode_ag_iterator(struct xfs_mount *mp,int (*execute)(struct xfs_inode *ip,struct xfs_perag *pag,int flags), int flags); -void xfs_inode_shrinker_register (struct xfs_mount *mp); -void xfs_inode_shrinker_unregister (struct xfs_mount *mp); -#endif
Signature · 8 lines
1.7.5.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/ Don't email: <a href=mailto:"[email protected]"> [email protected] </a>