@@ -392,20 +392,22 @@ void blkiocg_update_timeslice_used(struct blkio_group *blkg, unsigned long time,
392392}
393393EXPORT_SYMBOL_GPL (blkiocg_update_timeslice_used );
394394
395+ /*
396+ * should be called under rcu read lock or queue lock to make sure blkg pointer
397+ * is valid.
398+ */
395399void blkiocg_update_dispatch_stats (struct blkio_group * blkg ,
396400 uint64_t bytes , bool direction , bool sync )
397401{
398- struct blkio_group_stats * stats ;
399- unsigned long flags ;
402+ struct blkio_group_stats_cpu * stats_cpu ;
400403
401- spin_lock_irqsave (& blkg -> stats_lock , flags );
402- stats = & blkg -> stats ;
403- stats -> sectors += bytes >> 9 ;
404- blkio_add_stat (stats -> stat_arr [BLKIO_STAT_SERVICED ], 1 , direction ,
405- sync );
406- blkio_add_stat (stats -> stat_arr [BLKIO_STAT_SERVICE_BYTES ], bytes ,
407- direction , sync );
408- spin_unlock_irqrestore (& blkg -> stats_lock , flags );
404+ stats_cpu = this_cpu_ptr (blkg -> stats_cpu );
405+
406+ stats_cpu -> sectors += bytes >> 9 ;
407+ blkio_add_stat (stats_cpu -> stat_arr_cpu [BLKIO_STAT_CPU_SERVICED ],
408+ 1 , direction , sync );
409+ blkio_add_stat (stats_cpu -> stat_arr_cpu [BLKIO_STAT_CPU_SERVICE_BYTES ],
410+ bytes , direction , sync );
409411}
410412EXPORT_SYMBOL_GPL (blkiocg_update_dispatch_stats );
411413
@@ -440,6 +442,20 @@ void blkiocg_update_io_merged_stats(struct blkio_group *blkg, bool direction,
440442}
441443EXPORT_SYMBOL_GPL (blkiocg_update_io_merged_stats );
442444
445+ /*
446+ * This function allocates the per cpu stats for blkio_group. Should be called
447+ * from sleepable context as alloc_per_cpu() requires that.
448+ */
449+ int blkio_alloc_blkg_stats (struct blkio_group * blkg )
450+ {
451+ /* Allocate memory for per cpu stats */
452+ blkg -> stats_cpu = alloc_percpu (struct blkio_group_stats_cpu );
453+ if (!blkg -> stats_cpu )
454+ return - ENOMEM ;
455+ return 0 ;
456+ }
457+ EXPORT_SYMBOL_GPL (blkio_alloc_blkg_stats );
458+
443459void blkiocg_add_blkio_group (struct blkio_cgroup * blkcg ,
444460 struct blkio_group * blkg , void * key , dev_t dev ,
445461 enum blkio_policy_id plid )
@@ -600,6 +616,53 @@ static uint64_t blkio_fill_stat(char *str, int chars_left, uint64_t val,
600616 return val ;
601617}
602618
619+
620+ static uint64_t blkio_read_stat_cpu (struct blkio_group * blkg ,
621+ enum stat_type_cpu type , enum stat_sub_type sub_type )
622+ {
623+ int cpu ;
624+ struct blkio_group_stats_cpu * stats_cpu ;
625+ uint64_t val = 0 ;
626+
627+ for_each_possible_cpu (cpu ) {
628+ stats_cpu = per_cpu_ptr (blkg -> stats_cpu , cpu );
629+
630+ if (type == BLKIO_STAT_CPU_SECTORS )
631+ val += stats_cpu -> sectors ;
632+ else
633+ val += stats_cpu -> stat_arr_cpu [type ][sub_type ];
634+ }
635+
636+ return val ;
637+ }
638+
639+ static uint64_t blkio_get_stat_cpu (struct blkio_group * blkg ,
640+ struct cgroup_map_cb * cb , dev_t dev , enum stat_type_cpu type )
641+ {
642+ uint64_t disk_total , val ;
643+ char key_str [MAX_KEY_LEN ];
644+ enum stat_sub_type sub_type ;
645+
646+ if (type == BLKIO_STAT_CPU_SECTORS ) {
647+ val = blkio_read_stat_cpu (blkg , type , 0 );
648+ return blkio_fill_stat (key_str , MAX_KEY_LEN - 1 , val , cb , dev );
649+ }
650+
651+ for (sub_type = BLKIO_STAT_READ ; sub_type < BLKIO_STAT_TOTAL ;
652+ sub_type ++ ) {
653+ blkio_get_key_name (sub_type , dev , key_str , MAX_KEY_LEN , false);
654+ val = blkio_read_stat_cpu (blkg , type , sub_type );
655+ cb -> fill (cb , key_str , val );
656+ }
657+
658+ disk_total = blkio_read_stat_cpu (blkg , type , BLKIO_STAT_READ ) +
659+ blkio_read_stat_cpu (blkg , type , BLKIO_STAT_WRITE );
660+
661+ blkio_get_key_name (BLKIO_STAT_TOTAL , dev , key_str , MAX_KEY_LEN , false);
662+ cb -> fill (cb , key_str , disk_total );
663+ return disk_total ;
664+ }
665+
603666/* This should be called with blkg->stats_lock held */
604667static uint64_t blkio_get_stat (struct blkio_group * blkg ,
605668 struct cgroup_map_cb * cb , dev_t dev , enum stat_type type )
@@ -611,9 +674,6 @@ static uint64_t blkio_get_stat(struct blkio_group *blkg,
611674 if (type == BLKIO_STAT_TIME )
612675 return blkio_fill_stat (key_str , MAX_KEY_LEN - 1 ,
613676 blkg -> stats .time , cb , dev );
614- if (type == BLKIO_STAT_SECTORS )
615- return blkio_fill_stat (key_str , MAX_KEY_LEN - 1 ,
616- blkg -> stats .sectors , cb , dev );
617677#ifdef CONFIG_DEBUG_BLK_CGROUP
618678 if (type == BLKIO_STAT_UNACCOUNTED_TIME )
619679 return blkio_fill_stat (key_str , MAX_KEY_LEN - 1 ,
@@ -1077,8 +1137,8 @@ static int blkiocg_file_read(struct cgroup *cgrp, struct cftype *cft,
10771137}
10781138
10791139static int blkio_read_blkg_stats (struct blkio_cgroup * blkcg ,
1080- struct cftype * cft , struct cgroup_map_cb * cb , enum stat_type type ,
1081- bool show_total )
1140+ struct cftype * cft , struct cgroup_map_cb * cb ,
1141+ enum stat_type type , bool show_total , bool pcpu )
10821142{
10831143 struct blkio_group * blkg ;
10841144 struct hlist_node * n ;
@@ -1089,10 +1149,15 @@ static int blkio_read_blkg_stats(struct blkio_cgroup *blkcg,
10891149 if (blkg -> dev ) {
10901150 if (!cftype_blkg_same_policy (cft , blkg ))
10911151 continue ;
1092- spin_lock_irq (& blkg -> stats_lock );
1093- cgroup_total += blkio_get_stat (blkg , cb , blkg -> dev ,
1094- type );
1095- spin_unlock_irq (& blkg -> stats_lock );
1152+ if (pcpu )
1153+ cgroup_total += blkio_get_stat_cpu (blkg , cb ,
1154+ blkg -> dev , type );
1155+ else {
1156+ spin_lock_irq (& blkg -> stats_lock );
1157+ cgroup_total += blkio_get_stat (blkg , cb ,
1158+ blkg -> dev , type );
1159+ spin_unlock_irq (& blkg -> stats_lock );
1160+ }
10961161 }
10971162 }
10981163 if (show_total )
@@ -1116,47 +1181,47 @@ static int blkiocg_file_read_map(struct cgroup *cgrp, struct cftype *cft,
11161181 switch (name ) {
11171182 case BLKIO_PROP_time :
11181183 return blkio_read_blkg_stats (blkcg , cft , cb ,
1119- BLKIO_STAT_TIME , 0 );
1184+ BLKIO_STAT_TIME , 0 , 0 );
11201185 case BLKIO_PROP_sectors :
11211186 return blkio_read_blkg_stats (blkcg , cft , cb ,
1122- BLKIO_STAT_SECTORS , 0 );
1187+ BLKIO_STAT_CPU_SECTORS , 0 , 1 );
11231188 case BLKIO_PROP_io_service_bytes :
11241189 return blkio_read_blkg_stats (blkcg , cft , cb ,
1125- BLKIO_STAT_SERVICE_BYTES , 1 );
1190+ BLKIO_STAT_CPU_SERVICE_BYTES , 1 , 1 );
11261191 case BLKIO_PROP_io_serviced :
11271192 return blkio_read_blkg_stats (blkcg , cft , cb ,
1128- BLKIO_STAT_SERVICED , 1 );
1193+ BLKIO_STAT_CPU_SERVICED , 1 , 1 );
11291194 case BLKIO_PROP_io_service_time :
11301195 return blkio_read_blkg_stats (blkcg , cft , cb ,
1131- BLKIO_STAT_SERVICE_TIME , 1 );
1196+ BLKIO_STAT_SERVICE_TIME , 1 , 0 );
11321197 case BLKIO_PROP_io_wait_time :
11331198 return blkio_read_blkg_stats (blkcg , cft , cb ,
1134- BLKIO_STAT_WAIT_TIME , 1 );
1199+ BLKIO_STAT_WAIT_TIME , 1 , 0 );
11351200 case BLKIO_PROP_io_merged :
11361201 return blkio_read_blkg_stats (blkcg , cft , cb ,
1137- BLKIO_STAT_MERGED , 1 );
1202+ BLKIO_STAT_MERGED , 1 , 0 );
11381203 case BLKIO_PROP_io_queued :
11391204 return blkio_read_blkg_stats (blkcg , cft , cb ,
1140- BLKIO_STAT_QUEUED , 1 );
1205+ BLKIO_STAT_QUEUED , 1 , 0 );
11411206#ifdef CONFIG_DEBUG_BLK_CGROUP
11421207 case BLKIO_PROP_unaccounted_time :
11431208 return blkio_read_blkg_stats (blkcg , cft , cb ,
1144- BLKIO_STAT_UNACCOUNTED_TIME , 0 );
1209+ BLKIO_STAT_UNACCOUNTED_TIME , 0 , 0 );
11451210 case BLKIO_PROP_dequeue :
11461211 return blkio_read_blkg_stats (blkcg , cft , cb ,
1147- BLKIO_STAT_DEQUEUE , 0 );
1212+ BLKIO_STAT_DEQUEUE , 0 , 0 );
11481213 case BLKIO_PROP_avg_queue_size :
11491214 return blkio_read_blkg_stats (blkcg , cft , cb ,
1150- BLKIO_STAT_AVG_QUEUE_SIZE , 0 );
1215+ BLKIO_STAT_AVG_QUEUE_SIZE , 0 , 0 );
11511216 case BLKIO_PROP_group_wait_time :
11521217 return blkio_read_blkg_stats (blkcg , cft , cb ,
1153- BLKIO_STAT_GROUP_WAIT_TIME , 0 );
1218+ BLKIO_STAT_GROUP_WAIT_TIME , 0 , 0 );
11541219 case BLKIO_PROP_idle_time :
11551220 return blkio_read_blkg_stats (blkcg , cft , cb ,
1156- BLKIO_STAT_IDLE_TIME , 0 );
1221+ BLKIO_STAT_IDLE_TIME , 0 , 0 );
11571222 case BLKIO_PROP_empty_time :
11581223 return blkio_read_blkg_stats (blkcg , cft , cb ,
1159- BLKIO_STAT_EMPTY_TIME , 0 );
1224+ BLKIO_STAT_EMPTY_TIME , 0 , 0 );
11601225#endif
11611226 default :
11621227 BUG ();
@@ -1166,10 +1231,10 @@ static int blkiocg_file_read_map(struct cgroup *cgrp, struct cftype *cft,
11661231 switch (name ){
11671232 case BLKIO_THROTL_io_service_bytes :
11681233 return blkio_read_blkg_stats (blkcg , cft , cb ,
1169- BLKIO_STAT_SERVICE_BYTES , 1 );
1234+ BLKIO_STAT_CPU_SERVICE_BYTES , 1 , 1 );
11701235 case BLKIO_THROTL_io_serviced :
11711236 return blkio_read_blkg_stats (blkcg , cft , cb ,
1172- BLKIO_STAT_SERVICED , 1 );
1237+ BLKIO_STAT_CPU_SERVICED , 1 , 1 );
11731238 default :
11741239 BUG ();
11751240 }
0 commit comments