@@ -412,6 +412,17 @@ static void ublk_queue_deinit(struct ublk_queue *q)
412
412
int i ;
413
413
int nr_ios = q -> q_depth ;
414
414
415
+ if (q -> io_cmd_buf )
416
+ munmap (q -> io_cmd_buf , ublk_queue_cmd_buf_sz (q ));
417
+
418
+ for (i = 0 ; i < nr_ios ; i ++ )
419
+ free (q -> ios [i ].buf_addr );
420
+ }
421
+
422
+ static void ublk_thread_deinit (struct ublk_queue * q )
423
+ {
424
+ q -> tid = 0 ;
425
+
415
426
io_uring_unregister_buffers (& q -> ring );
416
427
417
428
io_uring_unregister_ring_fd (& q -> ring );
@@ -421,28 +432,20 @@ static void ublk_queue_deinit(struct ublk_queue *q)
421
432
close (q -> ring .ring_fd );
422
433
q -> ring .ring_fd = -1 ;
423
434
}
424
-
425
- if (q -> io_cmd_buf )
426
- munmap (q -> io_cmd_buf , ublk_queue_cmd_buf_sz (q ));
427
-
428
- for (i = 0 ; i < nr_ios ; i ++ )
429
- free (q -> ios [i ].buf_addr );
430
435
}
431
436
432
437
static int ublk_queue_init (struct ublk_queue * q , unsigned extra_flags )
433
438
{
434
439
struct ublk_dev * dev = q -> dev ;
435
440
int depth = dev -> dev_info .queue_depth ;
436
- int i , ret = -1 ;
441
+ int i ;
437
442
int cmd_buf_size , io_buf_size ;
438
443
unsigned long off ;
439
- int ring_depth = dev -> tgt .sq_depth , cq_depth = dev -> tgt .cq_depth ;
440
444
441
445
q -> tgt_ops = dev -> tgt .ops ;
442
446
q -> state = 0 ;
443
447
q -> q_depth = depth ;
444
448
q -> cmd_inflight = 0 ;
445
- q -> tid = gettid ();
446
449
447
450
if (dev -> dev_info .flags & (UBLK_F_SUPPORT_ZERO_COPY | UBLK_F_AUTO_BUF_REG )) {
448
451
q -> state |= UBLKSRV_NO_BUF ;
@@ -479,6 +482,22 @@ static int ublk_queue_init(struct ublk_queue *q, unsigned extra_flags)
479
482
}
480
483
}
481
484
485
+ return 0 ;
486
+ fail :
487
+ ublk_queue_deinit (q );
488
+ ublk_err ("ublk dev %d queue %d failed\n" ,
489
+ dev -> dev_info .dev_id , q -> q_id );
490
+ return - ENOMEM ;
491
+ }
492
+
493
+ static int ublk_thread_init (struct ublk_queue * q )
494
+ {
495
+ struct ublk_dev * dev = q -> dev ;
496
+ int ring_depth = dev -> tgt .sq_depth , cq_depth = dev -> tgt .cq_depth ;
497
+ int ret ;
498
+
499
+ q -> tid = gettid ();
500
+
482
501
ret = ublk_setup_ring (& q -> ring , ring_depth , cq_depth ,
483
502
IORING_SETUP_COOP_TASKRUN |
484
503
IORING_SETUP_SINGLE_ISSUER |
@@ -508,9 +527,9 @@ static int ublk_queue_init(struct ublk_queue *q, unsigned extra_flags)
508
527
}
509
528
510
529
return 0 ;
511
- fail :
512
- ublk_queue_deinit (q );
513
- ublk_err ("ublk dev %d queue %d failed\n" ,
530
+ fail :
531
+ ublk_thread_deinit (q );
532
+ ublk_err ("ublk dev %d queue %d thread init failed\n" ,
514
533
dev -> dev_info .dev_id , q -> q_id );
515
534
return - ENOMEM ;
516
535
}
@@ -778,23 +797,18 @@ struct ublk_queue_info {
778
797
struct ublk_queue * q ;
779
798
sem_t * queue_sem ;
780
799
cpu_set_t * affinity ;
781
- unsigned char auto_zc_fallback ;
782
800
};
783
801
784
802
static void * ublk_io_handler_fn (void * data )
785
803
{
786
804
struct ublk_queue_info * info = data ;
787
805
struct ublk_queue * q = info -> q ;
788
806
int dev_id = q -> dev -> dev_info .dev_id ;
789
- unsigned extra_flags = 0 ;
790
807
int ret ;
791
808
792
- if (info -> auto_zc_fallback )
793
- extra_flags = UBLKSRV_AUTO_BUF_REG_FALLBACK ;
794
-
795
- ret = ublk_queue_init (q , extra_flags );
809
+ ret = ublk_thread_init (q );
796
810
if (ret ) {
797
- ublk_err ("ublk dev %d queue %d init queue failed\n" ,
811
+ ublk_err ("ublk dev %d queue %d thread init failed\n" ,
798
812
dev_id , q -> q_id );
799
813
return NULL ;
800
814
}
@@ -813,7 +827,7 @@ static void *ublk_io_handler_fn(void *data)
813
827
} while (1 );
814
828
815
829
ublk_dbg (UBLK_DBG_QUEUE , "ublk dev %d queue %d exited\n" , dev_id , q -> q_id );
816
- ublk_queue_deinit (q );
830
+ ublk_thread_deinit (q );
817
831
return NULL ;
818
832
}
819
833
@@ -857,6 +871,7 @@ static int ublk_start_daemon(const struct dev_ctx *ctx, struct ublk_dev *dev)
857
871
{
858
872
const struct ublksrv_ctrl_dev_info * dinfo = & dev -> dev_info ;
859
873
struct ublk_queue_info * qinfo ;
874
+ unsigned extra_flags = 0 ;
860
875
cpu_set_t * affinity_buf ;
861
876
void * thread_ret ;
862
877
sem_t queue_sem ;
@@ -878,14 +893,23 @@ static int ublk_start_daemon(const struct dev_ctx *ctx, struct ublk_dev *dev)
878
893
if (ret )
879
894
return ret ;
880
895
896
+ if (ctx -> auto_zc_fallback )
897
+ extra_flags = UBLKSRV_AUTO_BUF_REG_FALLBACK ;
898
+
881
899
for (i = 0 ; i < dinfo -> nr_hw_queues ; i ++ ) {
882
900
dev -> q [i ].dev = dev ;
883
901
dev -> q [i ].q_id = i ;
884
902
903
+ ret = ublk_queue_init (& dev -> q [i ], extra_flags );
904
+ if (ret ) {
905
+ ublk_err ("ublk dev %d queue %d init queue failed\n" ,
906
+ dinfo -> dev_id , i );
907
+ goto fail ;
908
+ }
909
+
885
910
qinfo [i ].q = & dev -> q [i ];
886
911
qinfo [i ].queue_sem = & queue_sem ;
887
912
qinfo [i ].affinity = & affinity_buf [i ];
888
- qinfo [i ].auto_zc_fallback = ctx -> auto_zc_fallback ;
889
913
pthread_create (& dev -> q [i ].thread , NULL ,
890
914
ublk_io_handler_fn ,
891
915
& qinfo [i ]);
@@ -918,6 +942,8 @@ static int ublk_start_daemon(const struct dev_ctx *ctx, struct ublk_dev *dev)
918
942
for (i = 0 ; i < dinfo -> nr_hw_queues ; i ++ )
919
943
pthread_join (dev -> q [i ].thread , & thread_ret );
920
944
fail :
945
+ for (i = 0 ; i < dinfo -> nr_hw_queues ; i ++ )
946
+ ublk_queue_deinit (& dev -> q [i ]);
921
947
ublk_dev_unprep (dev );
922
948
ublk_dbg (UBLK_DBG_DEV , "%s exit\n" , __func__ );
923
949
0 commit comments