diff --git a/src/bio/bio_buffer.c b/src/bio/bio_buffer.c index c949991ac0f..3defdbe7cce 100644 --- a/src/bio/bio_buffer.c +++ b/src/bio/bio_buffer.c @@ -1,6 +1,6 @@ /** * (C) Copyright 2018-2024 Intel Corporation. - * (C) Copyright 2025 Hewlett Packard Enterprise Development LP + * (C) Copyright 2025-2026 Hewlett Packard Enterprise Development LP * * SPDX-License-Identifier: BSD-2-Clause-Patent */ @@ -1244,9 +1244,9 @@ iod_should_retry(struct bio_desc *biod, struct bio_dma_buffer *bdb) * When there isn't any in-flight IODs, it means the whole DMA buffer * isn't large enough to satisfy current huge IOD, don't retry. * - * When current IOD is for copy target, take the source IOD into account. + * When current IOD is the source of a copy, take the destination IOD into account. */ - if (biod->bd_copy_dst) { + if (biod->bd_copy_src) { D_ASSERT(bdb->bdb_active_iods >= 1); return bdb->bdb_active_iods > 1; } @@ -1279,8 +1279,11 @@ iod_fifo_in(struct bio_desc *biod, struct bio_dma_buffer *bdb) /* * Non-blocking prep request is usually from high priority job like checkpointing, * so we allow it jump the queue. + * + * The source of a copy is prepared with the destination buffer held, it must not wait + * (see bio_copy_prep()). */ - if (biod->bd_non_blocking) + if (biod->bd_non_blocking || biod->bd_copy_src) return; biod->bd_in_fifo = 1; @@ -1390,7 +1393,11 @@ iod_map_iovs(struct bio_desc *biod, void *arg) goto out; } - if (biod->bd_non_blocking) { + /* + * The source of a copy can't wait here, the destination buffer is held: + * bio_copy_prep() releases it, waits and prepares both IODs again. + */ + if (biod->bd_non_blocking || biod->bd_copy_src) { rc = -DER_AGAIN; goto out; } @@ -1450,7 +1457,11 @@ iod_prep_internal(struct bio_desc *biod, unsigned int type, void *bulk_ctxt, d_tm_set_gauge(bdb->bdb_stats.bds_active_iods, bdb->bdb_active_iods); if (biod->bd_type < BIO_IOD_TYPE_GETBUF) { - rc = ABT_eventual_create(0, &biod->bd_dma_done); + /* An IOD of a copy is prepared again when bio_copy_prep() retries */ + if (biod->bd_dma_done != ABT_EVENTUAL_NULL) + rc = ABT_eventual_reset(biod->bd_dma_done); + else + rc = ABT_eventual_create(0, &biod->bd_dma_done); if (rc != ABT_SUCCESS) { rc = -DER_NOMEM; goto failed; @@ -1867,12 +1878,45 @@ alloc_copy_desc(struct bio_io_context *ioctxt, struct umem_instance *umem, return NULL; } +/* + * The copy can't get the buffer of its source while other IODs are active: wait for one of them + * to complete. Called without any buffer held, see bio_copy_prep(). + */ +static void +copy_wait_iods(struct bio_desc *biod, struct bio_dma_buffer *bdb) +{ + /* The caller made sure of it: an active IOD is going to wake us up */ + D_ASSERT(bdb->bdb_active_iods > 0); + + /* Other waiters are queued, the destination IOD is going to join the FIFO queue on retry */ + if (bdb->bdb_queued_iods != 0) + return; + + /* Wait as the first waiter, it's woken up when an active IOD is completed */ + iod_fifo_wait(biod, bdb); + iod_fifo_out(biod, bdb); +} + +/* + * A copy needs DMA buffer for both the destination and the source, and copies run concurrently + * on an xstream (VOS aggregation, one per container). A copy must not yield while it holds only + * one of the two buffers: when the DMA buffer is short and can't grow, concurrent copies holding + * one buffer each would wait for each other forever, or keep taking the buffer another copy + * just gave up. + * + * So the destination IOD is prepared first: it waits for buffer as any IOD, with nothing held, + * and loads nothing from media. Then the source IOD is prepared without waiting nor yielding in + * between: either the copy gets both buffers, and only then yields to load the source, or it + * releases the destination buffer, waits for an active IOD to complete and starts over. + */ int bio_copy_prep(struct bio_io_context *ioctxt, struct umem_instance *umem, struct bio_sglist *bsgl_src, struct bio_sglist *bsgl_dst, struct bio_copy_desc **desc) { struct bio_copy_desc *copy_desc; + struct bio_desc *biod_src, *biod_dst; + struct bio_dma_buffer *bdb; int rc, ret; copy_desc = alloc_copy_desc(ioctxt, umem, bsgl_src, bsgl_dst); @@ -1880,19 +1924,51 @@ bio_copy_prep(struct bio_io_context *ioctxt, struct umem_instance *umem, *desc = NULL; return -DER_NOMEM; } - - rc = bio_iod_prep(copy_desc->bcd_iod_src, BIO_CHK_TYPE_LOCAL, NULL, 0); + biod_src = copy_desc->bcd_iod_src; + biod_dst = copy_desc->bcd_iod_dst; +retry: + rc = bio_iod_prep(biod_dst, BIO_CHK_TYPE_LOCAL, NULL, 0); if (rc) goto free; - copy_desc->bcd_iod_dst->bd_copy_dst = 1; - rc = bio_iod_prep(copy_desc->bcd_iod_dst, BIO_CHK_TYPE_LOCAL, NULL, 0); + /* + * The source IOD must not wait if the destination IOD holds DMA buffer. A destination + * accessing SCM directly holds none, the source is then prepared as any IOD. + */ + biod_src->bd_copy_src = (biod_dst->bd_rsrvd.brd_rg_cnt != 0); + + rc = bio_iod_prep(biod_src, BIO_CHK_TYPE_LOCAL, NULL, 0); if (rc) { - ret = bio_iod_post(copy_desc->bcd_iod_src, 0); - D_ASSERT(ret == 0); + bdb = biod_src->bd_copy_src ? iod_dma_buf(biod_src) : NULL; + + /* Release the destination buffer, nothing is written on error */ + ret = bio_iod_post(biod_dst, rc); + D_ASSERT(ret == rc); + /* + * No DMA buffer for the source while other IODs are active: wait for one of + * them to complete and start over. If none is active, the DMA buffer isn't + * large enough for this copy and the error is returned. + */ + if (rc == -DER_AGAIN && bdb != NULL && bdb->bdb_active_iods > 0) { + copy_wait_iods(biod_dst, bdb); + goto retry; + } goto free; } + /* + * For unit test only. In a server the copy yields while the source is loaded from NVMe, + * a unit test polls the completion itself: yield here for the time of the load (fail + * value, in microseconds) so that copies interleave as in a server. + */ + if (DAOS_FAIL_CHECK(DAOS_NVME_COPY_YIELD)) { + uint64_t start = daos_getutime(); + + do { + ABT_thread_yield(); + } while (daos_getutime() < start + daos_fail_value_get()); + } + *desc = copy_desc; return 0; free: diff --git a/src/bio/bio_internal.h b/src/bio/bio_internal.h index 329ac9b109d..5472130e203 100644 --- a/src/bio/bio_internal.h +++ b/src/bio/bio_internal.h @@ -1,6 +1,6 @@ /** * (C) Copyright 2018-2024 Intel Corporation. - * (C) Copyright 2025 Hewlett Packard Enterprise Development LP + * (C) Copyright 2025-2026 Hewlett Packard Enterprise Development LP * * SPDX-License-Identifier: BSD-2-Clause-Patent */ @@ -493,6 +493,7 @@ struct bio_rsrvd_dma { unsigned int brd_chk_cnt; }; +/* clang-format off */ /* I/O descriptor */ struct bio_desc { struct bio_io_lug bd_io_lug; @@ -509,12 +510,17 @@ struct bio_desc { unsigned int bd_type; /* Total bytes landed to data blob */ unsigned int bd_nvme_bytes; - /* Flags */ + /* + * Flags + * + * bd_copy_src: source IOD of a copy, prepared after the destination IOD with the + * destination buffer held, so it never waits for DMA buffer, see bio_copy_prep(). + */ unsigned int bd_buffer_prep:1, bd_dma_issued:1, bd_retry:1, bd_rdma:1, - bd_copy_dst:1, + bd_copy_src:1, bd_in_fifo:1, bd_async_post:1, bd_non_blocking:1; @@ -529,6 +535,7 @@ struct bio_desc { unsigned int bd_sgl_cnt; struct bio_sglist bd_sgls[0]; }; +/* clang-format on */ static inline struct spdk_thread * owner_thread(struct bio_blobstore *bbs) diff --git a/src/include/daos/common.h b/src/include/daos/common.h index cacaa65a487..2eb11e7ee3a 100644 --- a/src/include/daos/common.h +++ b/src/include/daos/common.h @@ -863,6 +863,8 @@ enum { #define DAOS_NVME_READ_ERR (DAOS_FAIL_UNIT_TEST_GROUP_LOC | 0x52) #define DAOS_NVME_ALLOCBUF_ERR (DAOS_FAIL_UNIT_TEST_GROUP_LOC | 0x53) #define DAOS_NVME_WAL_TX_LOST (DAOS_FAIL_UNIT_TEST_GROUP_LOC | 0x54) +/* bio_copy_prep() yields with both buffers held, fail value: for how long, in microseconds */ +#define DAOS_NVME_COPY_YIELD (DAOS_FAIL_UNIT_TEST_GROUP_LOC | 0x55) #define DAOS_POOL_CREATE_FAIL_CORPC (DAOS_FAIL_UNIT_TEST_GROUP_LOC | 0x60) #define DAOS_POOL_DESTROY_FAIL_CORPC (DAOS_FAIL_UNIT_TEST_GROUP_LOC | 0x61) diff --git a/src/vos/tests/vts_aggregate.c b/src/vos/tests/vts_aggregate.c index 7ec54040368..dd4d966959d 100644 --- a/src/vos/tests/vts_aggregate.c +++ b/src/vos/tests/vts_aggregate.c @@ -1,5 +1,6 @@ /** * (C) Copyright 2019-2024 Intel Corporation. + * (C) Copyright 2026 Hewlett Packard Enterprise Development LP * * SPDX-License-Identifier: BSD-2-Clause-Patent */ @@ -2976,6 +2977,355 @@ aggregate_35(void **state) assert_int_equal(feats & INIT_FEATS, INIT_FEATS); } +#define BIO_COPY_ULT_NR 4 /* one for the retry of a copy, three concurrent ones */ +#define BIO_COPY_EXT_NR (BIO_COPY_ULT_NR * 2) +#define BIO_COPY_SIZE (8UL << 20) /* a whole DMA chunk */ +#define BIO_COPY_HOLD_MAX 1024 +#define BIO_COPY_WAIT_SEC 10 +#define BIO_COPY_LOAD_US 500000 /* simulated load of the source, longer than a real one here */ +#define BIO_COPY_LAG_US 100000 /* a copy starts this long after the previous one */ + +struct bio_copy_ult_arg { + struct bio_io_context *ca_ioctxt; + struct umem_instance *ca_umem; + bio_addr_t ca_src; + bio_addr_t ca_dst; + bool ca_done; + int ca_rc; +}; + +struct bio_copy_tst { + struct bio_io_context *ct_ioctxt; + struct umem_instance *ct_umem; + ABT_pool ct_abt_pool; + /* IODs holding a DMA chunk each */ + struct bio_desc **ct_holders; + int ct_hold_nr; + /* The copies */ + struct bio_copy_ult_arg ct_ca[BIO_COPY_ULT_NR]; + ABT_thread ct_ults[BIO_COPY_ULT_NR]; + int ct_ult_nr; + bio_addr_t ct_addrs[BIO_COPY_EXT_NR]; + /* Errors, checked once everything is cleaned up */ + int ct_failed; +}; + +static void +bio_copy_ult(void *data) +{ + struct bio_copy_ult_arg *ca = data; + struct bio_copy_desc *desc; + struct bio_sglist bsgl_src; + struct bio_sglist bsgl_dst; + int rc; + + rc = bio_sgl_init(&bsgl_src, 1); + D_ASSERT(rc == 0); + rc = bio_sgl_init(&bsgl_dst, 1); + D_ASSERT(rc == 0); + bio_iov_set(&bsgl_src.bs_iovs[0], ca->ca_src, BIO_COPY_SIZE); + bio_iov_set(&bsgl_dst.bs_iovs[0], ca->ca_dst, BIO_COPY_SIZE); + bsgl_src.bs_nr_out = 1; + bsgl_dst.bs_nr_out = 1; + + rc = bio_copy_prep(ca->ca_ioctxt, ca->ca_umem, &bsgl_src, &bsgl_dst, &desc); + /* Got both buffers, that's all the test needs: don't write the destination */ + if (rc == 0) + bio_copy_post(desc, -DER_CANCELED); + + bio_sgl_fini(&bsgl_src); + bio_sgl_fini(&bsgl_dst); + ca->ca_rc = rc; + ca->ca_done = true; +} + +/* Hold a DMA chunk without waiting, returns -DER_AGAIN when there is none */ +static int +bio_copy_hold(struct bio_copy_tst *tst) +{ + struct bio_desc *biod; + struct bio_sglist *bsgl; + int rc; + + if (tst->ct_hold_nr >= BIO_COPY_HOLD_MAX) + return -DER_OVERFLOW; + + biod = bio_iod_alloc(tst->ct_ioctxt, tst->ct_umem, 1, BIO_IOD_TYPE_UPDATE); + if (biod == NULL) + return -DER_NOMEM; + + bsgl = bio_iod_sgl(biod, 0); + rc = bio_sgl_init(bsgl, 1); + if (rc == 0) { + bio_iov_set(&bsgl->bs_iovs[0], tst->ct_addrs[0], BIO_COPY_SIZE); + bsgl->bs_nr_out = 1; + rc = bio_iod_try_prep(biod, BIO_CHK_TYPE_LOCAL, NULL, 0); + } + if (rc != 0) { + bio_iod_free(biod); + return rc; + } + tst->ct_holders[tst->ct_hold_nr++] = biod; + return 0; +} + +/* Release @nr held DMA chunks, which wakes up the IOD waiting for DMA buffer */ +static void +bio_copy_release(struct bio_copy_tst *tst, int nr) +{ + int rc; + + while (nr-- > 0 && tst->ct_hold_nr > 0) { + tst->ct_hold_nr--; + rc = bio_iod_post(tst->ct_holders[tst->ct_hold_nr], -DER_CANCELED); + if (rc != -DER_CANCELED) { + print_message("release DMA chunk: " DF_RC "\n", DP_RC(rc)); + tst->ct_failed++; + } + bio_iod_free(tst->ct_holders[tst->ct_hold_nr]); + } +} + +static int +bio_copy_start(struct bio_copy_tst *tst) +{ + int idx = tst->ct_ult_nr; + int rc; + + D_ASSERT(idx < BIO_COPY_ULT_NR); + tst->ct_ca[idx].ca_ioctxt = tst->ct_ioctxt; + tst->ct_ca[idx].ca_umem = tst->ct_umem; + tst->ct_ca[idx].ca_src = tst->ct_addrs[idx * 2]; + tst->ct_ca[idx].ca_dst = tst->ct_addrs[idx * 2 + 1]; + rc = ABT_thread_create(tst->ct_abt_pool, bio_copy_ult, &tst->ct_ca[idx], + ABT_THREAD_ATTR_NULL, &tst->ct_ults[idx]); + if (rc != ABT_SUCCESS) { + print_message("create copy ULT %d: %d\n", idx, rc); + tst->ct_failed++; + return rc; + } + tst->ct_ult_nr++; + return 0; +} + +/* Run the copies [@first, @first + @nr) for at most @msec, returns how many completed */ +static int +copy_tst_run(struct bio_copy_tst *tst, int first, int nr, uint64_t msec) +{ + uint64_t start = daos_getmtime_coarse(); + int done_nr, i; + + do { + ABT_thread_yield(); + for (i = first, done_nr = 0; i < first + nr; i++) + done_nr += tst->ct_ca[i].ca_done ? 1 : 0; + if (done_nr == nr) + break; + usleep(1000); + } while (daos_getmtime_coarse() < start + msec); + + return done_nr; +} + +/* + * A copy needs a DMA chunk for the destination and one for the source. With a single chunk + * available it gets the buffer of the destination only: it must release it and wait, then get + * both buffers once a second chunk is available. + */ +static void +bio_copy_tst_retry(struct bio_copy_tst *tst) +{ + int done_nr, rc; + + bio_copy_release(tst, 1); + if (bio_copy_start(tst) != 0) + return; + + done_nr = copy_tst_run(tst, 0, 1, 200); + print_message("copy with one DMA chunk: %s\n", done_nr ? "completed" : "waiting"); + if (done_nr != 0) { + tst->ct_failed++; + return; + } + + /* The copy waits without any buffer: the chunk is available to another IOD */ + rc = bio_copy_hold(tst); + print_message("hold the DMA chunk while the copy waits: " DF_RC "\n", DP_RC(rc)); + if (rc != 0) + tst->ct_failed++; + else + bio_copy_release(tst, 1); + + bio_copy_release(tst, 1); + done_nr = copy_tst_run(tst, 0, 1, BIO_COPY_WAIT_SEC * 1000); + print_message("copy with two DMA chunks: %s\n", done_nr ? "completed" : "waiting"); + if (done_nr != 1) + tst->ct_failed++; +} + +/* + * Two chunks are available, enough for one copy at a time, and three copies are started one + * after the other. They only depend on each other, so they must all complete while the test + * holds the other chunks: + * - if two copies keep the buffer of the source while waiting for the buffer of the destination, + * none of them ever completes (deadlock); + * - if a copy releases its buffer to wait and the buffer is taken as first buffer by the next + * copy, while another copy is still loading its source, they can go on like this forever + * (livelock). + */ +static void +bio_copy_tst_concurrent(struct bio_copy_tst *tst) +{ + int first = tst->ct_ult_nr; + int nr = BIO_COPY_ULT_NR - first; + int done_nr, i; + + /* When a copy is done loading its source, the next one is still loading */ + daos_fail_value_set(BIO_COPY_LOAD_US); + daos_fail_loc_set(DAOS_NVME_COPY_YIELD | DAOS_FAIL_ALWAYS); + for (i = 0; i < nr; i++) { + if (bio_copy_start(tst) != 0) + return; + ABT_thread_yield(); + usleep(BIO_COPY_LAG_US); + } + + done_nr = copy_tst_run(tst, first, nr, BIO_COPY_WAIT_SEC * 1000); + print_message("%d of %d copies completed while the other DMA chunks are held\n", done_nr, + nr); + if (done_nr != nr) + tst->ct_failed++; +} + +/* + * Concurrent bio_copy_prep() (VOS aggregation of different containers on the same xstream) must + * make progress when the DMA buffer is short and can't grow: all the DMA chunks but the ones + * the copies are supposed to use are held by the test. + */ +static void +aggregate_39(void **state) +{ + struct io_test_args *arg = *state; + struct vos_pool *pool; + struct bio_copy_tst *tst; + vos_pool_info_t pool_info; + struct vos_pool_space *vps = &pool_info.pif_space; + struct bio_sglist *bsgl; + ABT_xstream xstream; + daos_handle_t iohs[BIO_COPY_EXT_NR]; + daos_unit_oid_t oid; + char dkey[UPDATE_DKEY_SIZE] = {0}; + char akeys[BIO_COPY_EXT_NR][UPDATE_AKEY_SIZE]; + daos_key_t dkey_iov; + daos_iod_t iod; + daos_recx_t recx; + int ioh_nr = 0; + int rc, i, failed; + + /* The copies don't interleave without DAOS_NVME_COPY_YIELD */ + FAULT_INJECTION_REQUIRED(); + + rc = vos_pool_query(arg->ctx.tc_po_hdl, &pool_info); + assert_rc_equal(rc, 0); + if (NVME_TOTAL(vps) == 0) { + print_message("NVMe isn't enabled, skip test\n"); + skip(); + } + + D_ALLOC_PTR(tst); + assert_non_null(tst); + D_ALLOC_ARRAY(tst->ct_holders, BIO_COPY_HOLD_MAX); + assert_non_null(tst->ct_holders); + pool = vos_hdl2pool(arg->ctx.tc_po_hdl); + tst->ct_ioctxt = vos_data_ioctxt(pool); + tst->ct_umem = vos_pool2umm(pool); + + /* Nothing below asserts before the cleanup: the ULTs use what is set up here */ + rc = ABT_xstream_self(&xstream); + if (rc == ABT_SUCCESS) + rc = ABT_xstream_get_main_pools(xstream, 1, &tst->ct_abt_pool); + if (rc != ABT_SUCCESS) { + print_message("get the ABT pool: %d\n", rc); + tst->ct_failed++; + goto out; + } + + /* Reserve the NVMe extents used as source and destination of the copies */ + oid = dts_unit_oid_gen(0, 0); + dts_key_gen(dkey, UPDATE_DKEY_SIZE, UPDATE_DKEY); + d_iov_set(&dkey_iov, dkey, strlen(dkey)); + recx.rx_idx = 0; + recx.rx_nr = BIO_COPY_SIZE; + for (i = 0; i < BIO_COPY_EXT_NR; i++) { + memset(&iod, 0, sizeof(iod)); + memset(akeys[i], 0, UPDATE_AKEY_SIZE); + dts_key_gen(akeys[i], UPDATE_AKEY_SIZE, UPDATE_AKEY); + d_iov_set(&iod.iod_name, akeys[i], strlen(akeys[i])); + iod.iod_type = DAOS_IOD_ARRAY; + iod.iod_size = 1; + iod.iod_nr = 1; + iod.iod_recxs = &recx; + + rc = vos_update_begin(arg->ctx.tc_co_hdl, oid, i + 1, 0, &dkey_iov, 1, &iod, NULL, + 0, &iohs[i], NULL); + if (rc != 0) { + print_message("reserve extent %d: " DF_RC "\n", i, DP_RC(rc)); + tst->ct_failed++; + goto out; + } + ioh_nr++; + bsgl = vos_iod_sgl_at(iohs[i], 0); + if (bsgl == NULL || bsgl->bs_nr_out != 1 || + bio_iov2media(&bsgl->bs_iovs[0]) != DAOS_MEDIA_NVME) { + print_message("extent %d isn't a single NVMe extent\n", i); + tst->ct_failed++; + goto out; + } + tst->ct_addrs[i] = bsgl->bs_iovs[0].bi_addr; + } + + /* Hold all the DMA chunks */ + do { + rc = bio_copy_hold(tst); + } while (rc == 0); + print_message("%d DMA chunks held: " DF_RC "\n", tst->ct_hold_nr, DP_RC(rc)); + if (rc != -DER_AGAIN || tst->ct_hold_nr < 3) { + tst->ct_failed++; + goto out; + } + + bio_copy_tst_retry(tst); + if (tst->ct_failed == 0) + bio_copy_tst_concurrent(tst); +out: + /* Release the held chunks, which also unblocks the copies which didn't complete */ + bio_copy_release(tst, tst->ct_hold_nr); + + for (i = 0; i < tst->ct_ult_nr; i++) { + rc = ABT_thread_free(&tst->ct_ults[i]); + if (rc != ABT_SUCCESS || tst->ct_ca[i].ca_rc != 0) { + print_message("copy %d: join rc %d, " DF_RC "\n", i, rc, + DP_RC(tst->ct_ca[i].ca_rc)); + tst->ct_failed++; + } + } + daos_fail_loc_set(0); + daos_fail_value_set(0); + + for (i = 0; i < ioh_nr; i++) { + rc = vos_update_end(iohs[i], 0, &dkey_iov, -DER_CANCELED, NULL, NULL); + if (rc != -DER_CANCELED) { + print_message("release extent %d: " DF_RC "\n", i, DP_RC(rc)); + tst->ct_failed++; + } + } + + failed = tst->ct_failed; + D_FREE(tst->ct_holders); + D_FREE(tst); + assert_int_equal(failed, 0); +} + static int agg_tst_teardown(void **state) { @@ -3054,6 +3404,7 @@ static const struct CMUnitTest aggregate_tests[] = { {"VOS435: Test aggregation timestamp functions", aggregate_35, NULL, NULL}, {"VOS436: Aggregate SV, multiple objects, flat dkeys", aggregate_36, NULL, agg_tst_teardown}, {"VOS437: Aggregate EV, multiple objects, flat dkeys", aggregate_37, NULL, agg_tst_teardown}, + {"VOS439: Concurrent copies with DMA buffer shortage", aggregate_39, NULL, agg_tst_teardown}, }; int