5858#include <linux/sched/signal.h>
5959
6060#include <trace/events/block.h>
61+ #include <linux/list_sort.h>
6162
6263#include "md.h"
6364#include "raid5.h"
@@ -878,41 +879,107 @@ static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
878879 return 1 ;
879880}
880881
881- static void flush_deferred_bios (struct r5conf * conf )
882+ static void dispatch_bio_list (struct bio_list * tmp )
882883{
883- struct bio_list tmp ;
884884 struct bio * bio ;
885885
886- if (!conf -> batch_bio_dispatch || !conf -> group_cnt )
886+ while ((bio = bio_list_pop (tmp )))
887+ generic_make_request (bio );
888+ }
889+
890+ static int cmp_stripe (void * priv , struct list_head * a , struct list_head * b )
891+ {
892+ const struct r5pending_data * da = list_entry (a ,
893+ struct r5pending_data , sibling );
894+ const struct r5pending_data * db = list_entry (b ,
895+ struct r5pending_data , sibling );
896+ if (da -> sector > db -> sector )
897+ return 1 ;
898+ if (da -> sector < db -> sector )
899+ return -1 ;
900+ return 0 ;
901+ }
902+
903+ static void dispatch_defer_bios (struct r5conf * conf , int target ,
904+ struct bio_list * list )
905+ {
906+ struct r5pending_data * data ;
907+ struct list_head * first , * next = NULL ;
908+ int cnt = 0 ;
909+
910+ if (conf -> pending_data_cnt == 0 )
911+ return ;
912+
913+ list_sort (NULL , & conf -> pending_list , cmp_stripe );
914+
915+ first = conf -> pending_list .next ;
916+
917+ /* temporarily move the head */
918+ if (conf -> next_pending_data )
919+ list_move_tail (& conf -> pending_list ,
920+ & conf -> next_pending_data -> sibling );
921+
922+ while (!list_empty (& conf -> pending_list )) {
923+ data = list_first_entry (& conf -> pending_list ,
924+ struct r5pending_data , sibling );
925+ if (& data -> sibling == first )
926+ first = data -> sibling .next ;
927+ next = data -> sibling .next ;
928+
929+ bio_list_merge (list , & data -> bios );
930+ list_move (& data -> sibling , & conf -> free_list );
931+ cnt ++ ;
932+ if (cnt >= target )
933+ break ;
934+ }
935+ conf -> pending_data_cnt -= cnt ;
936+ BUG_ON (conf -> pending_data_cnt < 0 || cnt < target );
937+
938+ if (next != & conf -> pending_list )
939+ conf -> next_pending_data = list_entry (next ,
940+ struct r5pending_data , sibling );
941+ else
942+ conf -> next_pending_data = NULL ;
943+ /* list isn't empty */
944+ if (first != & conf -> pending_list )
945+ list_move_tail (& conf -> pending_list , first );
946+ }
947+
948+ static void flush_deferred_bios (struct r5conf * conf )
949+ {
950+ struct bio_list tmp = BIO_EMPTY_LIST ;
951+
952+ if (conf -> pending_data_cnt == 0 )
887953 return ;
888954
889- bio_list_init (& tmp );
890955 spin_lock (& conf -> pending_bios_lock );
891- bio_list_merge ( & tmp , & conf -> pending_bios );
892- bio_list_init ( & conf -> pending_bios );
956+ dispatch_defer_bios ( conf , conf -> pending_data_cnt , & tmp );
957+ BUG_ON ( conf -> pending_data_cnt != 0 );
893958 spin_unlock (& conf -> pending_bios_lock );
894959
895- while ((bio = bio_list_pop (& tmp )))
896- generic_make_request (bio );
960+ dispatch_bio_list (& tmp );
897961}
898962
899- static void defer_bio_issue (struct r5conf * conf , struct bio * bio )
963+ static void defer_issue_bios (struct r5conf * conf , sector_t sector ,
964+ struct bio_list * bios )
900965{
901- /*
902- * change group_cnt will drain all bios, so this is safe
903- *
904- * A read generally means a read-modify-write, which usually means a
905- * randwrite, so we don't delay it
906- */
907- if (!conf -> batch_bio_dispatch || !conf -> group_cnt ||
908- bio_op (bio ) == REQ_OP_READ ) {
909- generic_make_request (bio );
910- return ;
911- }
966+ struct bio_list tmp = BIO_EMPTY_LIST ;
967+ struct r5pending_data * ent ;
968+
912969 spin_lock (& conf -> pending_bios_lock );
913- bio_list_add (& conf -> pending_bios , bio );
970+ ent = list_first_entry (& conf -> free_list , struct r5pending_data ,
971+ sibling );
972+ list_move_tail (& ent -> sibling , & conf -> pending_list );
973+ ent -> sector = sector ;
974+ bio_list_init (& ent -> bios );
975+ bio_list_merge (& ent -> bios , bios );
976+ conf -> pending_data_cnt ++ ;
977+ if (conf -> pending_data_cnt >= PENDING_IO_MAX )
978+ dispatch_defer_bios (conf , PENDING_IO_ONE_FLUSH , & tmp );
979+
914980 spin_unlock (& conf -> pending_bios_lock );
915- md_wakeup_thread (conf -> mddev -> thread );
981+
982+ dispatch_bio_list (& tmp );
916983}
917984
918985static void
@@ -925,6 +992,8 @@ static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
925992 struct r5conf * conf = sh -> raid_conf ;
926993 int i , disks = sh -> disks ;
927994 struct stripe_head * head_sh = sh ;
995+ struct bio_list pending_bios = BIO_EMPTY_LIST ;
996+ bool should_defer ;
928997
929998 might_sleep ();
930999
@@ -941,6 +1010,8 @@ static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
9411010 }
9421011 }
9431012
1013+ should_defer = conf -> batch_bio_dispatch && conf -> group_cnt ;
1014+
9441015 for (i = disks ; i -- ; ) {
9451016 int op , op_flags = 0 ;
9461017 int replace_only = 0 ;
@@ -1095,7 +1166,10 @@ static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
10951166 trace_block_bio_remap (bdev_get_queue (bi -> bi_bdev ),
10961167 bi , disk_devt (conf -> mddev -> gendisk ),
10971168 sh -> dev [i ].sector );
1098- defer_bio_issue (conf , bi );
1169+ if (should_defer && op_is_write (op ))
1170+ bio_list_add (& pending_bios , bi );
1171+ else
1172+ generic_make_request (bi );
10991173 }
11001174 if (rrdev ) {
11011175 if (s -> syncing || s -> expanding || s -> expanded
@@ -1140,7 +1214,10 @@ static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
11401214 trace_block_bio_remap (bdev_get_queue (rbi -> bi_bdev ),
11411215 rbi , disk_devt (conf -> mddev -> gendisk ),
11421216 sh -> dev [i ].sector );
1143- defer_bio_issue (conf , rbi );
1217+ if (should_defer && op_is_write (op ))
1218+ bio_list_add (& pending_bios , rbi );
1219+ else
1220+ generic_make_request (rbi );
11441221 }
11451222 if (!rdev && !rrdev ) {
11461223 if (op_is_write (op ))
@@ -1158,6 +1235,9 @@ static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
11581235 if (sh != head_sh )
11591236 goto again ;
11601237 }
1238+
1239+ if (should_defer && !bio_list_empty (& pending_bios ))
1240+ defer_issue_bios (conf , head_sh -> sector , & pending_bios );
11611241}
11621242
11631243static struct dma_async_tx_descriptor *
@@ -6678,6 +6758,7 @@ static void free_conf(struct r5conf *conf)
66786758 put_page (conf -> disks [i ].extra_page );
66796759 kfree (conf -> disks );
66806760 kfree (conf -> stripe_hashtbl );
6761+ kfree (conf -> pending_data );
66816762 kfree (conf );
66826763}
66836764
@@ -6787,6 +6868,14 @@ static struct r5conf *setup_conf(struct mddev *mddev)
67876868 conf = kzalloc (sizeof (struct r5conf ), GFP_KERNEL );
67886869 if (conf == NULL )
67896870 goto abort ;
6871+ INIT_LIST_HEAD (& conf -> free_list );
6872+ INIT_LIST_HEAD (& conf -> pending_list );
6873+ conf -> pending_data = kzalloc (sizeof (struct r5pending_data ) *
6874+ PENDING_IO_MAX , GFP_KERNEL );
6875+ if (!conf -> pending_data )
6876+ goto abort ;
6877+ for (i = 0 ; i < PENDING_IO_MAX ; i ++ )
6878+ list_add (& conf -> pending_data [i ].sibling , & conf -> free_list );
67906879 /* Don't enable multi-threading by default*/
67916880 if (!alloc_thread_groups (conf , 0 , & group_cnt , & worker_cnt_per_group ,
67926881 & new_group )) {
@@ -6811,7 +6900,6 @@ static struct r5conf *setup_conf(struct mddev *mddev)
68116900 atomic_set (& conf -> active_stripes , 0 );
68126901 atomic_set (& conf -> preread_active_stripes , 0 );
68136902 atomic_set (& conf -> active_aligned_reads , 0 );
6814- bio_list_init (& conf -> pending_bios );
68156903 spin_lock_init (& conf -> pending_bios_lock );
68166904 conf -> batch_bio_dispatch = true;
68176905 rdev_for_each (rdev , mddev ) {
0 commit comments