@@ -28,6 +28,8 @@ static int throtl_quantum = 32;
2828/* default latency target is 0, eg, guarantee IO latency by default */
2929#define DFL_LATENCY_TARGET (0)
3030
31+ #define SKIP_LATENCY (((u64)1) << BLK_STAT_RES_SHIFT)
32+
3133static struct blkcg_policy blkcg_policy_throtl ;
3234
3335/* A workqueue to queue throttle related work */
@@ -165,6 +167,19 @@ struct throtl_grp {
165167 unsigned long idletime_threshold ; /* us */
166168};
167169
170+ /* We measure latency for request size from <= 4k to >= 1M */
171+ #define LATENCY_BUCKET_SIZE 9
172+
173+ struct latency_bucket {
174+ unsigned long total_latency ; /* ns / 1024 */
175+ int samples ;
176+ };
177+
178+ struct avg_latency_bucket {
179+ unsigned long latency ; /* ns / 1024 */
180+ bool valid ;
181+ };
182+
168183struct throtl_data
169184{
170185 /* service tree for active throtl groups */
@@ -188,6 +203,13 @@ struct throtl_data
188203 unsigned long low_downgrade_time ;
189204
190205 unsigned int scale ;
206+
207+ struct latency_bucket tmp_buckets [LATENCY_BUCKET_SIZE ];
208+ struct avg_latency_bucket avg_buckets [LATENCY_BUCKET_SIZE ];
209+ struct latency_bucket __percpu * latency_buckets ;
210+ unsigned long last_calculate_time ;
211+
212+ bool track_bio_latency ;
191213};
192214
193215static void throtl_pending_timer_fn (unsigned long arg );
@@ -306,6 +328,9 @@ static unsigned int tg_iops_limit(struct throtl_grp *tg, int rw)
306328 return ret ;
307329}
308330
331+ #define request_bucket_index (sectors ) \
332+ clamp_t(int, order_base_2(sectors) - 3, 0, LATENCY_BUCKET_SIZE - 1)
333+
309334/**
310335 * throtl_log - log debug message via blktrace
311336 * @sq: the service_queue being reported
@@ -1931,6 +1956,73 @@ static void blk_throtl_update_idletime(struct throtl_grp *tg)
19311956 tg -> checked_last_finish_time = last_finish_time ;
19321957}
19331958
1959+ #ifdef CONFIG_BLK_DEV_THROTTLING_LOW
1960+ static void throtl_update_latency_buckets (struct throtl_data * td )
1961+ {
1962+ struct avg_latency_bucket avg_latency [LATENCY_BUCKET_SIZE ];
1963+ int i , cpu ;
1964+ unsigned long last_latency = 0 ;
1965+ unsigned long latency ;
1966+
1967+ if (!blk_queue_nonrot (td -> queue ))
1968+ return ;
1969+ if (time_before (jiffies , td -> last_calculate_time + HZ ))
1970+ return ;
1971+ td -> last_calculate_time = jiffies ;
1972+
1973+ memset (avg_latency , 0 , sizeof (avg_latency ));
1974+ for (i = 0 ; i < LATENCY_BUCKET_SIZE ; i ++ ) {
1975+ struct latency_bucket * tmp = & td -> tmp_buckets [i ];
1976+
1977+ for_each_possible_cpu (cpu ) {
1978+ struct latency_bucket * bucket ;
1979+
1980+ /* this isn't race free, but ok in practice */
1981+ bucket = per_cpu_ptr (td -> latency_buckets , cpu );
1982+ tmp -> total_latency += bucket [i ].total_latency ;
1983+ tmp -> samples += bucket [i ].samples ;
1984+ bucket [i ].total_latency = 0 ;
1985+ bucket [i ].samples = 0 ;
1986+ }
1987+
1988+ if (tmp -> samples >= 32 ) {
1989+ int samples = tmp -> samples ;
1990+
1991+ latency = tmp -> total_latency ;
1992+
1993+ tmp -> total_latency = 0 ;
1994+ tmp -> samples = 0 ;
1995+ latency /= samples ;
1996+ if (latency == 0 )
1997+ continue ;
1998+ avg_latency [i ].latency = latency ;
1999+ }
2000+ }
2001+
2002+ for (i = 0 ; i < LATENCY_BUCKET_SIZE ; i ++ ) {
2003+ if (!avg_latency [i ].latency ) {
2004+ if (td -> avg_buckets [i ].latency < last_latency )
2005+ td -> avg_buckets [i ].latency = last_latency ;
2006+ continue ;
2007+ }
2008+
2009+ if (!td -> avg_buckets [i ].valid )
2010+ latency = avg_latency [i ].latency ;
2011+ else
2012+ latency = (td -> avg_buckets [i ].latency * 7 +
2013+ avg_latency [i ].latency ) >> 3 ;
2014+
2015+ td -> avg_buckets [i ].latency = max (latency , last_latency );
2016+ td -> avg_buckets [i ].valid = true;
2017+ last_latency = td -> avg_buckets [i ].latency ;
2018+ }
2019+ }
2020+ #else
2021+ static inline void throtl_update_latency_buckets (struct throtl_data * td )
2022+ {
2023+ }
2024+ #endif
2025+
19342026bool blk_throtl_bio (struct request_queue * q , struct blkcg_gq * blkg ,
19352027 struct bio * bio )
19362028{
@@ -1939,6 +2031,7 @@ bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg,
19392031 struct throtl_service_queue * sq ;
19402032 bool rw = bio_data_dir (bio );
19412033 bool throttled = false;
2034+ struct throtl_data * td = tg -> td ;
19422035 int ret ;
19432036
19442037 WARN_ON_ONCE (!rcu_read_lock_held ());
@@ -1949,13 +2042,16 @@ bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg,
19492042
19502043 spin_lock_irq (q -> queue_lock );
19512044
2045+ throtl_update_latency_buckets (td );
2046+
19522047 if (unlikely (blk_queue_bypass (q )))
19532048 goto out_unlock ;
19542049
19552050 ret = bio_associate_current (bio );
19562051#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
19572052 if (ret == 0 || ret == - EBUSY )
19582053 bio -> bi_cg_private = tg ;
2054+ blk_stat_set_issue (& bio -> bi_issue_stat , bio_sectors (bio ));
19592055#endif
19602056 blk_throtl_update_idletime (tg );
19612057
@@ -1974,8 +2070,8 @@ bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg,
19742070 /* if above limits, break to queue */
19752071 if (!tg_may_dispatch (tg , bio , NULL )) {
19762072 tg -> last_low_overflow_time [rw ] = jiffies ;
1977- if (throtl_can_upgrade (tg -> td , tg )) {
1978- throtl_upgrade_state (tg -> td );
2073+ if (throtl_can_upgrade (td , tg )) {
2074+ throtl_upgrade_state (td );
19792075 goto again ;
19802076 }
19812077 break ;
@@ -2019,7 +2115,7 @@ bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg,
20192115
20202116 tg -> last_low_overflow_time [rw ] = jiffies ;
20212117
2022- tg -> td -> nr_queued [rw ]++ ;
2118+ td -> nr_queued [rw ]++ ;
20232119 throtl_add_bio_tg (bio , qn , tg );
20242120 throttled = true;
20252121
@@ -2044,20 +2140,67 @@ bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg,
20442140 */
20452141 if (!throttled )
20462142 bio_clear_flag (bio , BIO_THROTTLED );
2143+
2144+ #ifdef CONFIG_BLK_DEV_THROTTLING_LOW
2145+ if (throttled || !td -> track_bio_latency )
2146+ bio -> bi_issue_stat .stat |= SKIP_LATENCY ;
2147+ #endif
20472148 return throttled ;
20482149}
20492150
20502151#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
2152+ static void throtl_track_latency (struct throtl_data * td , sector_t size ,
2153+ int op , unsigned long time )
2154+ {
2155+ struct latency_bucket * latency ;
2156+ int index ;
2157+
2158+ if (!td || td -> limit_index != LIMIT_LOW || op != REQ_OP_READ ||
2159+ !blk_queue_nonrot (td -> queue ))
2160+ return ;
2161+
2162+ index = request_bucket_index (size );
2163+
2164+ latency = get_cpu_ptr (td -> latency_buckets );
2165+ latency [index ].total_latency += time ;
2166+ latency [index ].samples ++ ;
2167+ put_cpu_ptr (td -> latency_buckets );
2168+ }
2169+
2170+ void blk_throtl_stat_add (struct request * rq , u64 time_ns )
2171+ {
2172+ struct request_queue * q = rq -> q ;
2173+ struct throtl_data * td = q -> td ;
2174+
2175+ throtl_track_latency (td , blk_stat_size (& rq -> issue_stat ),
2176+ req_op (rq ), time_ns >> 10 );
2177+ }
2178+
20512179void blk_throtl_bio_endio (struct bio * bio )
20522180{
20532181 struct throtl_grp * tg ;
2182+ u64 finish_time_ns ;
2183+ unsigned long finish_time ;
2184+ unsigned long start_time ;
2185+ unsigned long lat ;
20542186
20552187 tg = bio -> bi_cg_private ;
20562188 if (!tg )
20572189 return ;
20582190 bio -> bi_cg_private = NULL ;
20592191
2060- tg -> last_finish_time = ktime_get_ns () >> 10 ;
2192+ finish_time_ns = ktime_get_ns ();
2193+ tg -> last_finish_time = finish_time_ns >> 10 ;
2194+
2195+ start_time = blk_stat_time (& bio -> bi_issue_stat ) >> 10 ;
2196+ finish_time = __blk_stat_time (finish_time_ns ) >> 10 ;
2197+ /* this is only for bio based driver */
2198+ if (start_time && finish_time > start_time &&
2199+ !(bio -> bi_issue_stat .stat & SKIP_LATENCY )) {
2200+ lat = finish_time - start_time ;
2201+ throtl_track_latency (tg -> td , blk_stat_size (& bio -> bi_issue_stat ),
2202+ bio_op (bio ), lat );
2203+ }
20612204}
20622205#endif
20632206
@@ -2133,6 +2276,12 @@ int blk_throtl_init(struct request_queue *q)
21332276 td = kzalloc_node (sizeof (* td ), GFP_KERNEL , q -> node );
21342277 if (!td )
21352278 return - ENOMEM ;
2279+ td -> latency_buckets = __alloc_percpu (sizeof (struct latency_bucket ) *
2280+ LATENCY_BUCKET_SIZE , __alignof__(u64 ));
2281+ if (!td -> latency_buckets ) {
2282+ kfree (td );
2283+ return - ENOMEM ;
2284+ }
21362285
21372286 INIT_WORK (& td -> dispatch_work , blk_throtl_dispatch_work_fn );
21382287 throtl_service_queue_init (& td -> service_queue );
@@ -2147,8 +2296,10 @@ int blk_throtl_init(struct request_queue *q)
21472296
21482297 /* activate policy */
21492298 ret = blkcg_activate_policy (q , & blkcg_policy_throtl );
2150- if (ret )
2299+ if (ret ) {
2300+ free_percpu (td -> latency_buckets );
21512301 kfree (td );
2302+ }
21522303 return ret ;
21532304}
21542305
@@ -2157,6 +2308,7 @@ void blk_throtl_exit(struct request_queue *q)
21572308 BUG_ON (!q -> td );
21582309 throtl_shutdown_wq (q );
21592310 blkcg_deactivate_policy (q , & blkcg_policy_throtl );
2311+ free_percpu (q -> td -> latency_buckets );
21602312 kfree (q -> td );
21612313}
21622314
@@ -2181,6 +2333,10 @@ void blk_throtl_register_queue(struct request_queue *q)
21812333 td -> throtl_slice = DFL_THROTL_SLICE_HD ;
21822334#endif
21832335
2336+ td -> track_bio_latency = !q -> mq_ops && !q -> request_fn ;
2337+ if (!td -> track_bio_latency )
2338+ blk_stat_enable_accounting (q );
2339+
21842340 /*
21852341 * some tg are created before queue is fully initialized, eg, nonrot
21862342 * isn't initialized yet
0 commit comments