Skip to content

Commit 8f75ba2

Browse files
ps-ushankaraxboe
authored andcommitted
selftests: ublk: kublk: lift queue initialization out of thread
Currently, each ublk server I/O handler thread initializes its own queue. However, as we move towards decoupled ublk_queues and ublk server threads, this model does not make sense anymore, as there will no longer be a concept of a thread having "its own" queue. So lift queue initialization out of the per-thread ublk_io_handler_fn and into a loop in ublk_start_daemon (which runs once for each device). There is a part of ublk_queue_init (ring initialization) which does actually need to happen on the thread that will use the ring; that is separated into a separate ublk_thread_init which is still called by each I/O handler thread. Signed-off-by: Uday Shankar <[email protected]> Reviewed-by: Ming Lei <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent 9773709 commit 8f75ba2

File tree

1 file changed

+47
-21
lines changed
  • tools/testing/selftests/ublk

1 file changed

+47
-21
lines changed

tools/testing/selftests/ublk/kublk.c

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,17 @@ static void ublk_queue_deinit(struct ublk_queue *q)
412412
int i;
413413
int nr_ios = q->q_depth;
414414

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+
415426
io_uring_unregister_buffers(&q->ring);
416427

417428
io_uring_unregister_ring_fd(&q->ring);
@@ -421,28 +432,20 @@ static void ublk_queue_deinit(struct ublk_queue *q)
421432
close(q->ring.ring_fd);
422433
q->ring.ring_fd = -1;
423434
}
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);
430435
}
431436

432437
static int ublk_queue_init(struct ublk_queue *q, unsigned extra_flags)
433438
{
434439
struct ublk_dev *dev = q->dev;
435440
int depth = dev->dev_info.queue_depth;
436-
int i, ret = -1;
441+
int i;
437442
int cmd_buf_size, io_buf_size;
438443
unsigned long off;
439-
int ring_depth = dev->tgt.sq_depth, cq_depth = dev->tgt.cq_depth;
440444

441445
q->tgt_ops = dev->tgt.ops;
442446
q->state = 0;
443447
q->q_depth = depth;
444448
q->cmd_inflight = 0;
445-
q->tid = gettid();
446449

447450
if (dev->dev_info.flags & (UBLK_F_SUPPORT_ZERO_COPY | UBLK_F_AUTO_BUF_REG)) {
448451
q->state |= UBLKSRV_NO_BUF;
@@ -479,6 +482,22 @@ static int ublk_queue_init(struct ublk_queue *q, unsigned extra_flags)
479482
}
480483
}
481484

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+
482501
ret = ublk_setup_ring(&q->ring, ring_depth, cq_depth,
483502
IORING_SETUP_COOP_TASKRUN |
484503
IORING_SETUP_SINGLE_ISSUER |
@@ -508,9 +527,9 @@ static int ublk_queue_init(struct ublk_queue *q, unsigned extra_flags)
508527
}
509528

510529
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",
514533
dev->dev_info.dev_id, q->q_id);
515534
return -ENOMEM;
516535
}
@@ -778,23 +797,18 @@ struct ublk_queue_info {
778797
struct ublk_queue *q;
779798
sem_t *queue_sem;
780799
cpu_set_t *affinity;
781-
unsigned char auto_zc_fallback;
782800
};
783801

784802
static void *ublk_io_handler_fn(void *data)
785803
{
786804
struct ublk_queue_info *info = data;
787805
struct ublk_queue *q = info->q;
788806
int dev_id = q->dev->dev_info.dev_id;
789-
unsigned extra_flags = 0;
790807
int ret;
791808

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);
796810
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",
798812
dev_id, q->q_id);
799813
return NULL;
800814
}
@@ -813,7 +827,7 @@ static void *ublk_io_handler_fn(void *data)
813827
} while (1);
814828

815829
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);
817831
return NULL;
818832
}
819833

@@ -857,6 +871,7 @@ static int ublk_start_daemon(const struct dev_ctx *ctx, struct ublk_dev *dev)
857871
{
858872
const struct ublksrv_ctrl_dev_info *dinfo = &dev->dev_info;
859873
struct ublk_queue_info *qinfo;
874+
unsigned extra_flags = 0;
860875
cpu_set_t *affinity_buf;
861876
void *thread_ret;
862877
sem_t queue_sem;
@@ -878,14 +893,23 @@ static int ublk_start_daemon(const struct dev_ctx *ctx, struct ublk_dev *dev)
878893
if (ret)
879894
return ret;
880895

896+
if (ctx->auto_zc_fallback)
897+
extra_flags = UBLKSRV_AUTO_BUF_REG_FALLBACK;
898+
881899
for (i = 0; i < dinfo->nr_hw_queues; i++) {
882900
dev->q[i].dev = dev;
883901
dev->q[i].q_id = i;
884902

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+
885910
qinfo[i].q = &dev->q[i];
886911
qinfo[i].queue_sem = &queue_sem;
887912
qinfo[i].affinity = &affinity_buf[i];
888-
qinfo[i].auto_zc_fallback = ctx->auto_zc_fallback;
889913
pthread_create(&dev->q[i].thread, NULL,
890914
ublk_io_handler_fn,
891915
&qinfo[i]);
@@ -918,6 +942,8 @@ static int ublk_start_daemon(const struct dev_ctx *ctx, struct ublk_dev *dev)
918942
for (i = 0; i < dinfo->nr_hw_queues; i++)
919943
pthread_join(dev->q[i].thread, &thread_ret);
920944
fail:
945+
for (i = 0; i < dinfo->nr_hw_queues; i++)
946+
ublk_queue_deinit(&dev->q[i]);
921947
ublk_dev_unprep(dev);
922948
ublk_dbg(UBLK_DBG_DEV, "%s exit\n", __func__);
923949

0 commit comments

Comments
 (0)