4040#define DM_BUFIO_WRITEBACK_RATIO 3
4141#define DM_BUFIO_LOW_WATERMARK_RATIO 16
4242
43- /*
44- * Check buffer ages in this interval (seconds)
45- */
46- #define DM_BUFIO_WORK_TIMER_SECS 30
47-
48- /*
49- * Free buffers when they are older than this (seconds)
50- */
51- #define DM_BUFIO_DEFAULT_AGE_SECS 300
52-
5343/*
5444 * The nr of bytes of cached data to keep around.
5545 */
@@ -1057,10 +1047,8 @@ static unsigned long dm_bufio_cache_size_latch;
10571047
10581048static DEFINE_SPINLOCK (global_spinlock );
10591049
1060- /*
1061- * Buffers are freed after this timeout
1062- */
1063- static unsigned int dm_bufio_max_age = DM_BUFIO_DEFAULT_AGE_SECS ;
1050+ static unsigned int dm_bufio_max_age ; /* No longer does anything */
1051+
10641052static unsigned long dm_bufio_retain_bytes = DM_BUFIO_DEFAULT_RETAIN_BYTES ;
10651053
10661054static unsigned long dm_bufio_peak_allocated ;
@@ -1088,7 +1076,6 @@ static LIST_HEAD(dm_bufio_all_clients);
10881076static DEFINE_MUTEX (dm_bufio_clients_lock );
10891077
10901078static struct workqueue_struct * dm_bufio_wq ;
1091- static struct delayed_work dm_bufio_cleanup_old_work ;
10921079static struct work_struct dm_bufio_replacement_work ;
10931080
10941081
@@ -2236,7 +2223,7 @@ int dm_bufio_issue_discard(struct dm_bufio_client *c, sector_t block, sector_t c
22362223}
22372224EXPORT_SYMBOL_GPL (dm_bufio_issue_discard );
22382225
2239- static bool forget_buffer (struct dm_bufio_client * c , sector_t block )
2226+ static void forget_buffer (struct dm_bufio_client * c , sector_t block )
22402227{
22412228 struct dm_buffer * b ;
22422229
@@ -2251,8 +2238,6 @@ static bool forget_buffer(struct dm_bufio_client *c, sector_t block)
22512238 cache_put_and_wake (c , b );
22522239 }
22532240 }
2254-
2255- return b ? true : false;
22562241}
22572242
22582243/*
@@ -2682,130 +2667,6 @@ EXPORT_SYMBOL_GPL(dm_bufio_set_sector_offset);
26822667
26832668/*--------------------------------------------------------------*/
26842669
2685- static unsigned int get_max_age_hz (void )
2686- {
2687- unsigned int max_age = READ_ONCE (dm_bufio_max_age );
2688-
2689- if (max_age > UINT_MAX / HZ )
2690- max_age = UINT_MAX / HZ ;
2691-
2692- return max_age * HZ ;
2693- }
2694-
2695- static bool older_than (struct dm_buffer * b , unsigned long age_hz )
2696- {
2697- return time_after_eq (jiffies , READ_ONCE (b -> last_accessed ) + age_hz );
2698- }
2699-
2700- struct evict_params {
2701- gfp_t gfp ;
2702- unsigned long age_hz ;
2703-
2704- /*
2705- * This gets updated with the largest last_accessed (ie. most
2706- * recently used) of the evicted buffers. It will not be reinitialised
2707- * by __evict_many(), so you can use it across multiple invocations.
2708- */
2709- unsigned long last_accessed ;
2710- };
2711-
2712- /*
2713- * We may not be able to evict this buffer if IO pending or the client
2714- * is still using it.
2715- *
2716- * And if GFP_NOFS is used, we must not do any I/O because we hold
2717- * dm_bufio_clients_lock and we would risk deadlock if the I/O gets
2718- * rerouted to different bufio client.
2719- */
2720- static enum evict_result select_for_evict (struct dm_buffer * b , void * context )
2721- {
2722- struct evict_params * params = context ;
2723-
2724- if (!(params -> gfp & __GFP_FS ) ||
2725- (static_branch_unlikely (& no_sleep_enabled ) && b -> c -> no_sleep )) {
2726- if (test_bit_acquire (B_READING , & b -> state ) ||
2727- test_bit (B_WRITING , & b -> state ) ||
2728- test_bit (B_DIRTY , & b -> state ))
2729- return ER_DONT_EVICT ;
2730- }
2731-
2732- return older_than (b , params -> age_hz ) ? ER_EVICT : ER_STOP ;
2733- }
2734-
2735- static unsigned long __evict_many (struct dm_bufio_client * c ,
2736- struct evict_params * params ,
2737- int list_mode , unsigned long max_count )
2738- {
2739- unsigned long count ;
2740- unsigned long last_accessed ;
2741- struct dm_buffer * b ;
2742-
2743- for (count = 0 ; count < max_count ; count ++ ) {
2744- b = cache_evict (& c -> cache , list_mode , select_for_evict , params );
2745- if (!b )
2746- break ;
2747-
2748- last_accessed = READ_ONCE (b -> last_accessed );
2749- if (time_after_eq (params -> last_accessed , last_accessed ))
2750- params -> last_accessed = last_accessed ;
2751-
2752- __make_buffer_clean (b );
2753- __free_buffer_wake (b );
2754-
2755- cond_resched ();
2756- }
2757-
2758- return count ;
2759- }
2760-
2761- static void evict_old_buffers (struct dm_bufio_client * c , unsigned long age_hz )
2762- {
2763- struct evict_params params = {.gfp = 0 , .age_hz = age_hz , .last_accessed = 0 };
2764- unsigned long retain = get_retain_buffers (c );
2765- unsigned long count ;
2766- LIST_HEAD (write_list );
2767-
2768- dm_bufio_lock (c );
2769-
2770- __check_watermark (c , & write_list );
2771- if (unlikely (!list_empty (& write_list ))) {
2772- dm_bufio_unlock (c );
2773- __flush_write_list (& write_list );
2774- dm_bufio_lock (c );
2775- }
2776-
2777- count = cache_total (& c -> cache );
2778- if (count > retain )
2779- __evict_many (c , & params , LIST_CLEAN , count - retain );
2780-
2781- dm_bufio_unlock (c );
2782- }
2783-
2784- static void cleanup_old_buffers (void )
2785- {
2786- unsigned long max_age_hz = get_max_age_hz ();
2787- struct dm_bufio_client * c ;
2788-
2789- mutex_lock (& dm_bufio_clients_lock );
2790-
2791- __cache_size_refresh ();
2792-
2793- list_for_each_entry (c , & dm_bufio_all_clients , client_list )
2794- evict_old_buffers (c , max_age_hz );
2795-
2796- mutex_unlock (& dm_bufio_clients_lock );
2797- }
2798-
2799- static void work_fn (struct work_struct * w )
2800- {
2801- cleanup_old_buffers ();
2802-
2803- queue_delayed_work (dm_bufio_wq , & dm_bufio_cleanup_old_work ,
2804- DM_BUFIO_WORK_TIMER_SECS * HZ );
2805- }
2806-
2807- /*--------------------------------------------------------------*/
2808-
28092670/*
28102671 * Global cleanup tries to evict the oldest buffers from across _all_
28112672 * the clients. It does this by repeatedly evicting a few buffers from
@@ -2843,27 +2704,55 @@ static void __insert_client(struct dm_bufio_client *new_client)
28432704 list_add_tail (& new_client -> client_list , h );
28442705}
28452706
2707+ static enum evict_result select_for_evict (struct dm_buffer * b , void * context )
2708+ {
2709+ /* In no-sleep mode, we cannot wait on IO. */
2710+ if (static_branch_unlikely (& no_sleep_enabled ) && b -> c -> no_sleep ) {
2711+ if (test_bit_acquire (B_READING , & b -> state ) ||
2712+ test_bit (B_WRITING , & b -> state ) ||
2713+ test_bit (B_DIRTY , & b -> state ))
2714+ return ER_DONT_EVICT ;
2715+ }
2716+ return ER_EVICT ;
2717+ }
2718+
28462719static unsigned long __evict_a_few (unsigned long nr_buffers )
28472720{
2848- unsigned long count ;
28492721 struct dm_bufio_client * c ;
2850- struct evict_params params = {
2851- .gfp = GFP_KERNEL ,
2852- .age_hz = 0 ,
2853- /* set to jiffies in case there are no buffers in this client */
2854- .last_accessed = jiffies
2855- };
2722+ unsigned long oldest_buffer = jiffies ;
2723+ unsigned long last_accessed ;
2724+ unsigned long count ;
2725+ struct dm_buffer * b ;
28562726
28572727 c = __pop_client ();
28582728 if (!c )
28592729 return 0 ;
28602730
28612731 dm_bufio_lock (c );
2862- count = __evict_many (c , & params , LIST_CLEAN , nr_buffers );
2732+
2733+ for (count = 0 ; count < nr_buffers ; count ++ ) {
2734+ b = cache_evict (& c -> cache , LIST_CLEAN , select_for_evict , NULL );
2735+ if (!b )
2736+ break ;
2737+
2738+ last_accessed = READ_ONCE (b -> last_accessed );
2739+ if (time_after_eq (oldest_buffer , last_accessed ))
2740+ oldest_buffer = last_accessed ;
2741+
2742+ __make_buffer_clean (b );
2743+ __free_buffer_wake (b );
2744+
2745+ if (need_resched ()) {
2746+ dm_bufio_unlock (c );
2747+ cond_resched ();
2748+ dm_bufio_lock (c );
2749+ }
2750+ }
2751+
28632752 dm_bufio_unlock (c );
28642753
28652754 if (count )
2866- c -> oldest_buffer = params . last_accessed ;
2755+ c -> oldest_buffer = oldest_buffer ;
28672756 __insert_client (c );
28682757
28692758 return count ;
@@ -2946,10 +2835,7 @@ static int __init dm_bufio_init(void)
29462835 if (!dm_bufio_wq )
29472836 return - ENOMEM ;
29482837
2949- INIT_DELAYED_WORK (& dm_bufio_cleanup_old_work , work_fn );
29502838 INIT_WORK (& dm_bufio_replacement_work , do_global_cleanup );
2951- queue_delayed_work (dm_bufio_wq , & dm_bufio_cleanup_old_work ,
2952- DM_BUFIO_WORK_TIMER_SECS * HZ );
29532839
29542840 return 0 ;
29552841}
@@ -2961,7 +2847,6 @@ static void __exit dm_bufio_exit(void)
29612847{
29622848 int bug = 0 ;
29632849
2964- cancel_delayed_work_sync (& dm_bufio_cleanup_old_work );
29652850 destroy_workqueue (dm_bufio_wq );
29662851
29672852 if (dm_bufio_client_count ) {
@@ -2998,7 +2883,7 @@ module_param_named(max_cache_size_bytes, dm_bufio_cache_size, ulong, 0644);
29982883MODULE_PARM_DESC (max_cache_size_bytes , "Size of metadata cache" );
29992884
30002885module_param_named (max_age_seconds , dm_bufio_max_age , uint , 0644 );
3001- MODULE_PARM_DESC (max_age_seconds , "Max age of a buffer in seconds " );
2886+ MODULE_PARM_DESC (max_age_seconds , "No longer does anything " );
30022887
30032888module_param_named (retain_bytes , dm_bufio_retain_bytes , ulong , 0644 );
30042889MODULE_PARM_DESC (retain_bytes , "Try to keep at least this many bytes cached in memory" );
0 commit comments