@@ -70,6 +70,9 @@ struct throtl_grp {
7070 /* When did we start a new slice */
7171 unsigned long slice_start [2 ];
7272 unsigned long slice_end [2 ];
73+
74+ /* Some throttle limits got updated for the group */
75+ bool limits_changed ;
7376};
7477
7578struct throtl_data
@@ -93,6 +96,8 @@ struct throtl_data
9396
9497 /* Work for dispatching throttled bios */
9598 struct delayed_work throtl_work ;
99+
100+ atomic_t limits_changed ;
96101};
97102
98103enum tg_state_flags {
@@ -592,15 +597,6 @@ static void tg_update_disptime(struct throtl_data *td, struct throtl_grp *tg)
592597 min_wait = min (read_wait , write_wait );
593598 disptime = jiffies + min_wait ;
594599
595- /*
596- * If group is already on active tree, then update dispatch time
597- * only if it is lesser than existing dispatch time. Otherwise
598- * always update the dispatch time
599- */
600-
601- if (throtl_tg_on_rr (tg ) && time_before (disptime , tg -> disptime ))
602- return ;
603-
604600 /* Update dispatch time */
605601 throtl_dequeue_tg (td , tg );
606602 tg -> disptime = disptime ;
@@ -691,6 +687,46 @@ static int throtl_select_dispatch(struct throtl_data *td, struct bio_list *bl)
691687 return nr_disp ;
692688}
693689
690+ static void throtl_process_limit_change (struct throtl_data * td )
691+ {
692+ struct throtl_grp * tg ;
693+ struct hlist_node * pos , * n ;
694+
695+ /*
696+ * Make sure atomic_inc() effects from
697+ * throtl_update_blkio_group_read_bps(), group of functions are
698+ * visible.
699+ * Is this required or smp_mb__after_atomic_inc() was suffcient
700+ * after the atomic_inc().
701+ */
702+ smp_rmb ();
703+ if (!atomic_read (& td -> limits_changed ))
704+ return ;
705+
706+ throtl_log (td , "limit changed =%d" , atomic_read (& td -> limits_changed ));
707+
708+ hlist_for_each_entry_safe (tg , pos , n , & td -> tg_list , tg_node ) {
709+ /*
710+ * Do I need an smp_rmb() here to make sure tg->limits_changed
711+ * update is visible. I am relying on smp_rmb() at the
712+ * beginning of function and not putting a new one here.
713+ */
714+
715+ if (throtl_tg_on_rr (tg ) && tg -> limits_changed ) {
716+ throtl_log_tg (td , tg , "limit change rbps=%llu wbps=%llu"
717+ " riops=%u wiops=%u" , tg -> bps [READ ],
718+ tg -> bps [WRITE ], tg -> iops [READ ],
719+ tg -> iops [WRITE ]);
720+ tg_update_disptime (td , tg );
721+ tg -> limits_changed = false;
722+ }
723+ }
724+
725+ smp_mb__before_atomic_dec ();
726+ atomic_dec (& td -> limits_changed );
727+ smp_mb__after_atomic_dec ();
728+ }
729+
694730/* Dispatch throttled bios. Should be called without queue lock held. */
695731static int throtl_dispatch (struct request_queue * q )
696732{
@@ -701,6 +737,8 @@ static int throtl_dispatch(struct request_queue *q)
701737
702738 spin_lock_irq (q -> queue_lock );
703739
740+ throtl_process_limit_change (td );
741+
704742 if (!total_nr_queued (td ))
705743 goto out ;
706744
@@ -821,28 +859,74 @@ void throtl_unlink_blkio_group(void *key, struct blkio_group *blkg)
821859 spin_unlock_irqrestore (td -> queue -> queue_lock , flags );
822860}
823861
824- static void throtl_update_blkio_group_read_bps (struct blkio_group * blkg ,
825- u64 read_bps )
862+ /*
863+ * For all update functions, key should be a valid pointer because these
864+ * update functions are called under blkcg_lock, that means, blkg is
865+ * valid and in turn key is valid. queue exit path can not race becuase
866+ * of blkcg_lock
867+ *
868+ * Can not take queue lock in update functions as queue lock under blkcg_lock
869+ * is not allowed. Under other paths we take blkcg_lock under queue_lock.
870+ */
871+ static void throtl_update_blkio_group_read_bps (void * key ,
872+ struct blkio_group * blkg , u64 read_bps )
826873{
874+ struct throtl_data * td = key ;
875+
827876 tg_of_blkg (blkg )-> bps [READ ] = read_bps ;
877+ /* Make sure read_bps is updated before setting limits_changed */
878+ smp_wmb ();
879+ tg_of_blkg (blkg )-> limits_changed = true;
880+
881+ /* Make sure tg->limits_changed is updated before td->limits_changed */
882+ smp_mb__before_atomic_inc ();
883+ atomic_inc (& td -> limits_changed );
884+ smp_mb__after_atomic_inc ();
885+
886+ /* Schedule a work now to process the limit change */
887+ throtl_schedule_delayed_work (td -> queue , 0 );
828888}
829889
830- static void throtl_update_blkio_group_write_bps ( struct blkio_group * blkg ,
831- u64 write_bps )
890+ static void throtl_update_blkio_group_write_bps ( void * key ,
891+ struct blkio_group * blkg , u64 write_bps )
832892{
893+ struct throtl_data * td = key ;
894+
833895 tg_of_blkg (blkg )-> bps [WRITE ] = write_bps ;
896+ smp_wmb ();
897+ tg_of_blkg (blkg )-> limits_changed = true;
898+ smp_mb__before_atomic_inc ();
899+ atomic_inc (& td -> limits_changed );
900+ smp_mb__after_atomic_inc ();
901+ throtl_schedule_delayed_work (td -> queue , 0 );
834902}
835903
836- static void throtl_update_blkio_group_read_iops ( struct blkio_group * blkg ,
837- unsigned int read_iops )
904+ static void throtl_update_blkio_group_read_iops ( void * key ,
905+ struct blkio_group * blkg , unsigned int read_iops )
838906{
907+ struct throtl_data * td = key ;
908+
839909 tg_of_blkg (blkg )-> iops [READ ] = read_iops ;
910+ smp_wmb ();
911+ tg_of_blkg (blkg )-> limits_changed = true;
912+ smp_mb__before_atomic_inc ();
913+ atomic_inc (& td -> limits_changed );
914+ smp_mb__after_atomic_inc ();
915+ throtl_schedule_delayed_work (td -> queue , 0 );
840916}
841917
842- static void throtl_update_blkio_group_write_iops ( struct blkio_group * blkg ,
843- unsigned int write_iops )
918+ static void throtl_update_blkio_group_write_iops ( void * key ,
919+ struct blkio_group * blkg , unsigned int write_iops )
844920{
921+ struct throtl_data * td = key ;
922+
845923 tg_of_blkg (blkg )-> iops [WRITE ] = write_iops ;
924+ smp_wmb ();
925+ tg_of_blkg (blkg )-> limits_changed = true;
926+ smp_mb__before_atomic_inc ();
927+ atomic_inc (& td -> limits_changed );
928+ smp_mb__after_atomic_inc ();
929+ throtl_schedule_delayed_work (td -> queue , 0 );
846930}
847931
848932void throtl_shutdown_timer_wq (struct request_queue * q )
@@ -886,8 +970,14 @@ int blk_throtl_bio(struct request_queue *q, struct bio **biop)
886970 /*
887971 * There is already another bio queued in same dir. No
888972 * need to update dispatch time.
973+ * Still update the disptime if rate limits on this group
974+ * were changed.
889975 */
890- update_disptime = false;
976+ if (!tg -> limits_changed )
977+ update_disptime = false;
978+ else
979+ tg -> limits_changed = false;
980+
891981 goto queue_bio ;
892982 }
893983
@@ -929,6 +1019,7 @@ int blk_throtl_init(struct request_queue *q)
9291019
9301020 INIT_HLIST_HEAD (& td -> tg_list );
9311021 td -> tg_service_tree = THROTL_RB_ROOT ;
1022+ atomic_set (& td -> limits_changed , 0 );
9321023
9331024 /* Init root group */
9341025 tg = & td -> root_tg ;
@@ -996,6 +1087,13 @@ void blk_throtl_exit(struct request_queue *q)
9961087 */
9971088 if (wait )
9981089 synchronize_rcu ();
1090+
1091+ /*
1092+ * Just being safe to make sure after previous flush if some body did
1093+ * update limits through cgroup and another work got queued, cancel
1094+ * it.
1095+ */
1096+ throtl_shutdown_timer_wq (q );
9991097 throtl_td_free (td );
10001098}
10011099
0 commit comments