From d7b2f5b96ef5643bcd9d38c0e865099f8d03ffb6 Mon Sep 17 00:00:00 2001 From: Alexey Marchuk Date: Thu, 20 Apr 2023 11:04:22 +0200 Subject: [PATCH] bdev/crypto: Put accel buffer when write completes Accel buffer is released when encrypt operation completes, however it doesn't mean that base bdev finishes writing encrypted data. As result, accel buffer might be reused in another IO, that leads to data corruption. Signed-off-by: Alexey Marchuk Change-Id: I1acf7c30da2f92989ecc44e96b00f7609058ec5a Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/17655 Tested-by: SPDK CI Jenkins Reviewed-by: Ben Walker Reviewed-by: Jim Harris Community-CI: Mellanox Build Bot --- module/bdev/crypto/vbdev_crypto.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/module/bdev/crypto/vbdev_crypto.c b/module/bdev/crypto/vbdev_crypto.c index 9b0ae402c..7d0d142ed 100644 --- a/module/bdev/crypto/vbdev_crypto.c +++ b/module/bdev/crypto/vbdev_crypto.c @@ -79,6 +79,12 @@ static void crypto_io_fail(struct crypto_bdev_io *crypto_io) { struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(crypto_io); + struct crypto_io_channel *crypto_ch = crypto_io->crypto_ch; + + if (crypto_io->aux_buf_raw) { + spdk_accel_put_buf(crypto_ch->accel_channel, crypto_io->aux_buf_raw, + crypto_io->aux_domain, crypto_io->aux_domain_ctx); + } /* This function can only be used to fail an IO that hasn't been sent to the base bdev, * otherwise accel sequence might have already been executed/aborted. */ @@ -116,16 +122,6 @@ crypto_write(struct crypto_io_channel *crypto_ch, struct spdk_bdev_io *bdev_io) } } -static void -crypto_encrypt_cb(void *cb_arg) -{ - struct crypto_bdev_io *crypto_io = cb_arg; - struct crypto_io_channel *crypto_ch = crypto_io->crypto_ch; - - spdk_accel_put_buf(crypto_ch->accel_channel, crypto_io->aux_buf_raw, - crypto_io->aux_domain, crypto_io->aux_domain_ctx); -} - /* We're either encrypting on the way down or decrypting on the way back. */ static void crypto_encrypt(struct crypto_io_channel *crypto_ch, struct spdk_bdev_io *bdev_io) @@ -157,7 +153,7 @@ crypto_encrypt(struct crypto_io_channel *crypto_ch, struct spdk_bdev_io *bdev_io bdev_io->u.bdev.memory_domain, bdev_io->u.bdev.memory_domain_ctx, bdev_io->u.bdev.offset_blocks, crypto_len, 0, - crypto_encrypt_cb, crypto_io); + NULL, NULL); if (spdk_unlikely(rc != 0)) { spdk_accel_put_buf(crypto_ch->accel_channel, crypto_io->aux_buf_raw, crypto_io->aux_domain, crypto_io->aux_domain_ctx); @@ -179,8 +175,15 @@ static void _complete_internal_io(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) { struct spdk_bdev_io *orig_io = cb_arg; + struct crypto_bdev_io *crypto_io = (struct crypto_bdev_io *)orig_io->driver_ctx; + struct crypto_io_channel *crypto_ch = crypto_io->crypto_ch; int status = success ? SPDK_BDEV_IO_STATUS_SUCCESS : SPDK_BDEV_IO_STATUS_FAILED; + if (crypto_io->aux_buf_raw) { + spdk_accel_put_buf(crypto_ch->accel_channel, crypto_io->aux_buf_raw, + crypto_io->aux_domain, crypto_io->aux_domain_ctx); + } + spdk_bdev_io_complete(orig_io, status); spdk_bdev_free_io(bdev_io); }