2022-06-03 19:15:11 +00:00
|
|
|
/* SPDX-License-Identifier: BSD-3-Clause
|
2016-07-20 18:16:23 +00:00
|
|
|
* Copyright (c) Intel Corporation.
|
|
|
|
* All rights reserved.
|
2021-10-29 13:48:49 +00:00
|
|
|
* Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
2016-07-20 18:16:23 +00:00
|
|
|
*/
|
|
|
|
|
2017-05-02 18:18:25 +00:00
|
|
|
#include "spdk/stdinc.h"
|
2016-07-20 18:16:23 +00:00
|
|
|
|
2017-07-13 04:08:53 +00:00
|
|
|
#include "bdev_malloc.h"
|
2016-08-26 22:35:02 +00:00
|
|
|
#include "spdk/endian.h"
|
2016-08-17 20:35:18 +00:00
|
|
|
#include "spdk/env.h"
|
2022-08-08 20:31:08 +00:00
|
|
|
#include "spdk/accel.h"
|
2017-06-02 17:25:43 +00:00
|
|
|
#include "spdk/string.h"
|
2016-07-20 18:16:23 +00:00
|
|
|
|
2020-10-06 16:16:26 +00:00
|
|
|
#include "spdk/log.h"
|
2016-11-07 22:10:28 +00:00
|
|
|
|
2016-07-20 18:16:23 +00:00
|
|
|
struct malloc_disk {
|
2017-12-01 22:19:23 +00:00
|
|
|
struct spdk_bdev disk;
|
2018-03-02 19:49:36 +00:00
|
|
|
void *malloc_buf;
|
2022-10-24 13:26:19 +00:00
|
|
|
void *malloc_md_buf;
|
2017-12-01 22:19:23 +00:00
|
|
|
TAILQ_ENTRY(malloc_disk) link;
|
2016-07-20 18:16:23 +00:00
|
|
|
};
|
|
|
|
|
2016-10-13 18:38:17 +00:00
|
|
|
struct malloc_task {
|
|
|
|
int num_outstanding;
|
|
|
|
enum spdk_bdev_io_status status;
|
2021-12-16 09:45:10 +00:00
|
|
|
TAILQ_ENTRY(malloc_task) tailq;
|
2016-10-13 18:38:17 +00:00
|
|
|
};
|
|
|
|
|
2021-12-16 08:28:25 +00:00
|
|
|
struct malloc_channel {
|
|
|
|
struct spdk_io_channel *accel_channel;
|
2021-12-16 09:45:10 +00:00
|
|
|
struct spdk_poller *completion_poller;
|
|
|
|
TAILQ_HEAD(, malloc_task) completed_tasks;
|
2021-12-16 08:28:25 +00:00
|
|
|
};
|
|
|
|
|
2016-07-20 18:16:23 +00:00
|
|
|
static void
|
|
|
|
malloc_done(void *ref, int status)
|
|
|
|
{
|
2020-07-03 14:08:47 +00:00
|
|
|
struct malloc_task *task = (struct malloc_task *)ref;
|
2016-07-20 18:16:23 +00:00
|
|
|
|
|
|
|
if (status != 0) {
|
bdev: add ENOMEM handling
At very high queue depths, bdev modules may not have enough
internal resources to track all of the incoming I/O. For example,
we allocate a finite number of nvme_request objects per allocated
queue pair. Currently if these resources are exhausted, the
bdev module will return failure (with no indication why) which
gets propagated all the way back to the application.
So instead, add SPDK_BDEV_IO_STATUS_NOMEM to allow bdev modules
to indicate this type of failure. Also add handling for this
status type in the generic bdev layer, involving queuing these
I/O for later retry after other I/O on the failing channel have
completed.
This does place an expectation on the bdev module that these
internal resources are allocated per io_channel. Otherwise we
cannot guarantee forward progress solely on reception of
completions. For example, without this guarantee, a bdev
module could theoretically return ENOMEM even if there were
no I/O oustanding for that io_channel. nvme, aio, rbd,
virtio and null drivers comply with this expectation already.
malloc only complies though when not using copy offload.
This patch will fix malloc w/ copy engine to at least
return ENOMEM when no copy descriptors are available. If the
condition above occurs, I/O waiting for resources will get
failed as part of a subsequent reset which matches the
behavior it has today.
Signed-off-by: Jim Harris <james.r.harris@intel.com>
Change-Id: Iea7cd51a611af8abe882794d0b2361fdbb74e84e
Reviewed-on: https://review.gerrithub.io/378853
Tested-by: SPDK Automated Test System <sys_sgsw@intel.com>
Reviewed-by: Daniel Verkamp <daniel.verkamp@intel.com>
Reviewed-by: Changpeng Liu <changpeng.liu@intel.com>
2017-09-15 20:47:17 +00:00
|
|
|
if (status == -ENOMEM) {
|
|
|
|
task->status = SPDK_BDEV_IO_STATUS_NOMEM;
|
|
|
|
} else {
|
|
|
|
task->status = SPDK_BDEV_IO_STATUS_FAILED;
|
|
|
|
}
|
2016-10-13 18:38:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (--task->num_outstanding == 0) {
|
|
|
|
spdk_bdev_io_complete(spdk_bdev_io_from_ctx(task), task->status);
|
2016-07-20 18:16:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-16 10:36:57 +00:00
|
|
|
static void
|
|
|
|
malloc_complete_task(struct malloc_task *task, struct malloc_channel *mch,
|
|
|
|
enum spdk_bdev_io_status status)
|
|
|
|
{
|
|
|
|
task->status = status;
|
|
|
|
TAILQ_INSERT_TAIL(&mch->completed_tasks, task, tailq);
|
|
|
|
}
|
|
|
|
|
2017-12-01 22:19:23 +00:00
|
|
|
static TAILQ_HEAD(, malloc_disk) g_malloc_disks = TAILQ_HEAD_INITIALIZER(g_malloc_disks);
|
2016-07-20 18:16:23 +00:00
|
|
|
|
|
|
|
int malloc_disk_count = 0;
|
|
|
|
|
2017-07-13 17:36:19 +00:00
|
|
|
static int bdev_malloc_initialize(void);
|
2021-12-16 08:28:25 +00:00
|
|
|
static void bdev_malloc_deinitialize(void);
|
2016-07-20 18:16:23 +00:00
|
|
|
|
|
|
|
static int
|
2017-07-13 04:08:53 +00:00
|
|
|
bdev_malloc_get_ctx_size(void)
|
2016-07-20 18:16:23 +00:00
|
|
|
{
|
2020-07-03 14:08:47 +00:00
|
|
|
return sizeof(struct malloc_task);
|
2016-07-20 18:16:23 +00:00
|
|
|
}
|
|
|
|
|
2018-03-09 22:20:21 +00:00
|
|
|
static struct spdk_bdev_module malloc_if = {
|
2018-03-06 18:52:46 +00:00
|
|
|
.name = "malloc",
|
|
|
|
.module_init = bdev_malloc_initialize,
|
2021-12-16 08:28:25 +00:00
|
|
|
.module_fini = bdev_malloc_deinitialize,
|
2018-03-06 18:52:46 +00:00
|
|
|
.get_ctx_size = bdev_malloc_get_ctx_size,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2019-02-05 10:46:48 +00:00
|
|
|
SPDK_BDEV_MODULE_REGISTER(malloc, &malloc_if)
|
2016-07-20 18:16:23 +00:00
|
|
|
|
2017-06-02 17:25:43 +00:00
|
|
|
static void
|
|
|
|
malloc_disk_free(struct malloc_disk *malloc_disk)
|
|
|
|
{
|
|
|
|
if (!malloc_disk) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(malloc_disk->disk.name);
|
2019-04-05 08:43:15 +00:00
|
|
|
spdk_free(malloc_disk->malloc_buf);
|
2022-10-24 13:26:19 +00:00
|
|
|
spdk_free(malloc_disk->malloc_md_buf);
|
2019-04-05 08:43:15 +00:00
|
|
|
free(malloc_disk);
|
2017-06-02 17:25:43 +00:00
|
|
|
}
|
|
|
|
|
2016-07-20 18:16:23 +00:00
|
|
|
static int
|
2017-07-13 04:08:53 +00:00
|
|
|
bdev_malloc_destruct(void *ctx)
|
2016-07-20 18:16:23 +00:00
|
|
|
{
|
2017-04-04 21:10:00 +00:00
|
|
|
struct malloc_disk *malloc_disk = ctx;
|
2017-12-01 22:19:23 +00:00
|
|
|
|
|
|
|
TAILQ_REMOVE(&g_malloc_disks, malloc_disk, link);
|
2017-06-02 17:25:43 +00:00
|
|
|
malloc_disk_free(malloc_disk);
|
2016-07-20 18:16:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-04 14:39:27 +00:00
|
|
|
static int
|
2017-07-13 04:08:53 +00:00
|
|
|
bdev_malloc_check_iov_len(struct iovec *iovs, int iovcnt, size_t nbytes)
|
2016-10-04 14:39:27 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < iovcnt; i++) {
|
2017-12-07 23:23:48 +00:00
|
|
|
if (nbytes < iovs[i].iov_len) {
|
2016-10-04 14:39:27 +00:00
|
|
|
return 0;
|
2017-12-07 23:23:48 +00:00
|
|
|
}
|
2016-10-04 14:39:27 +00:00
|
|
|
|
|
|
|
nbytes -= iovs[i].iov_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nbytes != 0;
|
|
|
|
}
|
|
|
|
|
2016-10-13 18:19:47 +00:00
|
|
|
static void
|
2017-07-13 04:08:53 +00:00
|
|
|
bdev_malloc_readv(struct malloc_disk *mdisk, struct spdk_io_channel *ch,
|
|
|
|
struct malloc_task *task,
|
2022-10-24 13:26:19 +00:00
|
|
|
struct iovec *iov, int iovcnt, size_t len, uint64_t offset,
|
|
|
|
void *md_buf, size_t md_len, uint64_t md_offset)
|
2016-07-20 18:16:23 +00:00
|
|
|
{
|
2016-10-04 14:39:27 +00:00
|
|
|
int64_t res = 0;
|
2022-10-24 13:26:19 +00:00
|
|
|
void *src;
|
|
|
|
void *md_src;
|
2016-10-04 14:39:27 +00:00
|
|
|
int i;
|
2016-10-13 18:19:47 +00:00
|
|
|
|
2017-07-13 04:08:53 +00:00
|
|
|
if (bdev_malloc_check_iov_len(iov, iovcnt, len)) {
|
2016-10-04 14:39:27 +00:00
|
|
|
spdk_bdev_io_complete(spdk_bdev_io_from_ctx(task),
|
|
|
|
SPDK_BDEV_IO_STATUS_FAILED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-24 13:26:19 +00:00
|
|
|
task->status = SPDK_BDEV_IO_STATUS_SUCCESS;
|
|
|
|
task->num_outstanding = 0;
|
|
|
|
|
2021-12-23 12:29:46 +00:00
|
|
|
SPDK_DEBUGLOG(bdev_malloc, "read %zu bytes from offset %#" PRIx64 ", iovcnt=%d\n",
|
|
|
|
len, offset, iovcnt);
|
2016-10-04 14:39:27 +00:00
|
|
|
|
2022-10-24 13:26:19 +00:00
|
|
|
src = mdisk->malloc_buf + offset;
|
2016-10-13 18:38:17 +00:00
|
|
|
|
2016-10-04 14:39:27 +00:00
|
|
|
for (i = 0; i < iovcnt; i++) {
|
2021-12-23 12:29:46 +00:00
|
|
|
task->num_outstanding++;
|
2020-07-03 14:08:47 +00:00
|
|
|
res = spdk_accel_submit_copy(ch, iov[i].iov_base,
|
2021-08-24 21:08:29 +00:00
|
|
|
src, iov[i].iov_len, 0, malloc_done, task);
|
2016-10-13 18:19:47 +00:00
|
|
|
|
2017-09-16 00:17:57 +00:00
|
|
|
if (res != 0) {
|
2020-07-03 14:08:47 +00:00
|
|
|
malloc_done(task, res);
|
2021-12-23 12:29:46 +00:00
|
|
|
break;
|
2016-10-04 14:39:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
src += iov[i].iov_len;
|
|
|
|
len -= iov[i].iov_len;
|
2016-10-13 18:19:47 +00:00
|
|
|
}
|
2022-10-24 13:26:19 +00:00
|
|
|
|
|
|
|
if (md_buf == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SPDK_DEBUGLOG(bdev_malloc, "read metadata %zu bytes from offset%#" PRIx64 "\n",
|
|
|
|
md_len, md_offset);
|
|
|
|
|
|
|
|
md_src = mdisk->malloc_md_buf + md_offset;
|
|
|
|
|
|
|
|
task->num_outstanding++;
|
|
|
|
res = spdk_accel_submit_copy(ch, md_buf, md_src, md_len, 0, malloc_done, task);
|
|
|
|
|
|
|
|
if (res != 0) {
|
|
|
|
malloc_done(task, res);
|
|
|
|
}
|
2016-07-20 18:16:23 +00:00
|
|
|
}
|
|
|
|
|
2016-10-13 18:19:47 +00:00
|
|
|
static void
|
2017-07-13 04:08:53 +00:00
|
|
|
bdev_malloc_writev(struct malloc_disk *mdisk, struct spdk_io_channel *ch,
|
|
|
|
struct malloc_task *task,
|
2022-10-24 13:26:19 +00:00
|
|
|
struct iovec *iov, int iovcnt, size_t len, uint64_t offset,
|
|
|
|
void *md_buf, size_t md_len, uint64_t md_offset)
|
2016-07-20 18:16:23 +00:00
|
|
|
{
|
2022-10-24 13:26:19 +00:00
|
|
|
|
2016-10-04 14:39:27 +00:00
|
|
|
int64_t res = 0;
|
2022-10-24 13:26:19 +00:00
|
|
|
void *dst;
|
|
|
|
void *md_dst;
|
2016-10-04 14:39:27 +00:00
|
|
|
int i;
|
2016-10-13 18:19:47 +00:00
|
|
|
|
2017-07-13 04:08:53 +00:00
|
|
|
if (bdev_malloc_check_iov_len(iov, iovcnt, len)) {
|
2016-10-04 14:39:27 +00:00
|
|
|
spdk_bdev_io_complete(spdk_bdev_io_from_ctx(task),
|
|
|
|
SPDK_BDEV_IO_STATUS_FAILED);
|
2016-10-13 18:19:47 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-07-20 18:16:23 +00:00
|
|
|
|
2021-12-23 12:29:46 +00:00
|
|
|
SPDK_DEBUGLOG(bdev_malloc, "wrote %zu bytes to offset %#" PRIx64 ", iovcnt=%d\n",
|
|
|
|
len, offset, iovcnt);
|
2016-07-20 18:16:23 +00:00
|
|
|
|
2022-10-24 13:26:19 +00:00
|
|
|
dst = mdisk->malloc_buf + offset;
|
|
|
|
|
2016-10-13 18:38:17 +00:00
|
|
|
task->status = SPDK_BDEV_IO_STATUS_SUCCESS;
|
2021-12-23 12:29:46 +00:00
|
|
|
task->num_outstanding = 0;
|
2016-10-13 18:38:17 +00:00
|
|
|
|
2016-10-04 14:39:27 +00:00
|
|
|
for (i = 0; i < iovcnt; i++) {
|
2021-12-23 12:29:46 +00:00
|
|
|
task->num_outstanding++;
|
2020-07-03 14:08:47 +00:00
|
|
|
res = spdk_accel_submit_copy(ch, dst, iov[i].iov_base,
|
2021-08-24 21:08:29 +00:00
|
|
|
iov[i].iov_len, 0, malloc_done, task);
|
2016-10-04 14:39:27 +00:00
|
|
|
|
2017-09-16 00:17:57 +00:00
|
|
|
if (res != 0) {
|
2020-07-03 14:08:47 +00:00
|
|
|
malloc_done(task, res);
|
2021-12-23 12:29:46 +00:00
|
|
|
break;
|
2016-10-04 14:39:27 +00:00
|
|
|
}
|
2016-10-13 18:19:47 +00:00
|
|
|
|
2016-10-04 14:39:27 +00:00
|
|
|
dst += iov[i].iov_len;
|
2016-10-13 18:19:47 +00:00
|
|
|
}
|
2022-10-24 13:26:19 +00:00
|
|
|
|
|
|
|
if (md_buf == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SPDK_DEBUGLOG(bdev_malloc, "wrote metadata %zu bytes to offset %#" PRIx64 "\n",
|
|
|
|
md_len, md_offset);
|
|
|
|
|
|
|
|
md_dst = mdisk->malloc_md_buf + md_offset;
|
|
|
|
|
|
|
|
task->num_outstanding++;
|
|
|
|
res = spdk_accel_submit_copy(ch, md_dst, md_buf, md_len, 0, malloc_done, task);
|
|
|
|
|
|
|
|
if (res != 0) {
|
|
|
|
malloc_done(task, res);
|
|
|
|
}
|
|
|
|
|
2016-07-20 18:16:23 +00:00
|
|
|
}
|
|
|
|
|
2016-08-26 22:35:02 +00:00
|
|
|
static int
|
2017-07-13 04:08:53 +00:00
|
|
|
bdev_malloc_unmap(struct malloc_disk *mdisk,
|
|
|
|
struct spdk_io_channel *ch,
|
|
|
|
struct malloc_task *task,
|
2017-07-19 21:32:04 +00:00
|
|
|
uint64_t offset,
|
|
|
|
uint64_t byte_count)
|
2016-08-26 22:35:02 +00:00
|
|
|
{
|
2016-10-13 18:38:17 +00:00
|
|
|
task->status = SPDK_BDEV_IO_STATUS_SUCCESS;
|
|
|
|
task->num_outstanding = 1;
|
|
|
|
|
2020-07-03 14:08:47 +00:00
|
|
|
return spdk_accel_submit_fill(ch, mdisk->malloc_buf + offset, 0,
|
2021-08-24 21:08:29 +00:00
|
|
|
byte_count, 0, malloc_done, task);
|
2016-07-20 18:16:23 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 21:35:04 +00:00
|
|
|
static int
|
|
|
|
_bdev_malloc_submit_request(struct malloc_channel *mch, struct spdk_bdev_io *bdev_io)
|
2016-07-20 18:16:23 +00:00
|
|
|
{
|
2017-08-29 00:15:53 +00:00
|
|
|
uint32_t block_size = bdev_io->bdev->blocklen;
|
2022-10-24 13:26:19 +00:00
|
|
|
uint32_t md_size = bdev_io->bdev->md_len;
|
2017-08-29 00:15:53 +00:00
|
|
|
|
2016-07-20 18:16:23 +00:00
|
|
|
switch (bdev_io->type) {
|
|
|
|
case SPDK_BDEV_IO_TYPE_READ:
|
2017-09-20 13:10:17 +00:00
|
|
|
if (bdev_io->u.bdev.iovs[0].iov_base == NULL) {
|
|
|
|
assert(bdev_io->u.bdev.iovcnt == 1);
|
|
|
|
bdev_io->u.bdev.iovs[0].iov_base =
|
2017-05-05 20:06:39 +00:00
|
|
|
((struct malloc_disk *)bdev_io->bdev->ctxt)->malloc_buf +
|
2017-09-20 13:10:17 +00:00
|
|
|
bdev_io->u.bdev.offset_blocks * block_size;
|
|
|
|
bdev_io->u.bdev.iovs[0].iov_len = bdev_io->u.bdev.num_blocks * block_size;
|
2021-12-16 10:36:57 +00:00
|
|
|
malloc_complete_task((struct malloc_task *)bdev_io->driver_ctx, mch,
|
|
|
|
SPDK_BDEV_IO_STATUS_SUCCESS);
|
2016-07-20 18:16:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-13 04:08:53 +00:00
|
|
|
bdev_malloc_readv((struct malloc_disk *)bdev_io->bdev->ctxt,
|
2021-12-16 08:28:25 +00:00
|
|
|
mch->accel_channel,
|
2017-07-13 04:08:53 +00:00
|
|
|
(struct malloc_task *)bdev_io->driver_ctx,
|
2017-09-20 13:10:17 +00:00
|
|
|
bdev_io->u.bdev.iovs,
|
|
|
|
bdev_io->u.bdev.iovcnt,
|
|
|
|
bdev_io->u.bdev.num_blocks * block_size,
|
2022-10-24 13:26:19 +00:00
|
|
|
bdev_io->u.bdev.offset_blocks * block_size,
|
|
|
|
bdev_io->u.bdev.md_buf,
|
|
|
|
bdev_io->u.bdev.num_blocks * md_size,
|
|
|
|
bdev_io->u.bdev.offset_blocks * md_size);
|
2016-10-13 18:19:47 +00:00
|
|
|
return 0;
|
2016-07-20 18:16:23 +00:00
|
|
|
|
|
|
|
case SPDK_BDEV_IO_TYPE_WRITE:
|
2017-07-13 04:08:53 +00:00
|
|
|
bdev_malloc_writev((struct malloc_disk *)bdev_io->bdev->ctxt,
|
2021-12-16 08:28:25 +00:00
|
|
|
mch->accel_channel,
|
2017-07-13 04:08:53 +00:00
|
|
|
(struct malloc_task *)bdev_io->driver_ctx,
|
2017-09-20 13:10:17 +00:00
|
|
|
bdev_io->u.bdev.iovs,
|
|
|
|
bdev_io->u.bdev.iovcnt,
|
|
|
|
bdev_io->u.bdev.num_blocks * block_size,
|
2022-10-24 13:26:19 +00:00
|
|
|
bdev_io->u.bdev.offset_blocks * block_size,
|
|
|
|
bdev_io->u.bdev.md_buf,
|
|
|
|
bdev_io->u.bdev.num_blocks * md_size,
|
|
|
|
bdev_io->u.bdev.offset_blocks * md_size);
|
2016-10-13 18:19:47 +00:00
|
|
|
return 0;
|
2016-07-20 18:16:23 +00:00
|
|
|
|
|
|
|
case SPDK_BDEV_IO_TYPE_RESET:
|
2021-12-16 10:36:57 +00:00
|
|
|
malloc_complete_task((struct malloc_task *)bdev_io->driver_ctx, mch,
|
|
|
|
SPDK_BDEV_IO_STATUS_SUCCESS);
|
2021-12-16 10:22:04 +00:00
|
|
|
return 0;
|
2016-07-20 18:16:23 +00:00
|
|
|
|
|
|
|
case SPDK_BDEV_IO_TYPE_FLUSH:
|
2021-12-16 10:36:57 +00:00
|
|
|
malloc_complete_task((struct malloc_task *)bdev_io->driver_ctx, mch,
|
|
|
|
SPDK_BDEV_IO_STATUS_SUCCESS);
|
2021-12-16 10:22:04 +00:00
|
|
|
return 0;
|
2016-08-26 22:35:02 +00:00
|
|
|
|
|
|
|
case SPDK_BDEV_IO_TYPE_UNMAP:
|
2017-07-13 04:08:53 +00:00
|
|
|
return bdev_malloc_unmap((struct malloc_disk *)bdev_io->bdev->ctxt,
|
2021-12-16 08:28:25 +00:00
|
|
|
mch->accel_channel,
|
2017-07-13 04:08:53 +00:00
|
|
|
(struct malloc_task *)bdev_io->driver_ctx,
|
2017-09-20 13:10:17 +00:00
|
|
|
bdev_io->u.bdev.offset_blocks * block_size,
|
|
|
|
bdev_io->u.bdev.num_blocks * block_size);
|
2017-08-11 19:04:21 +00:00
|
|
|
|
|
|
|
case SPDK_BDEV_IO_TYPE_WRITE_ZEROES:
|
|
|
|
/* bdev_malloc_unmap is implemented with a call to mem_cpy_fill which zeroes out all of the requested bytes. */
|
|
|
|
return bdev_malloc_unmap((struct malloc_disk *)bdev_io->bdev->ctxt,
|
2021-12-16 08:28:25 +00:00
|
|
|
mch->accel_channel,
|
2017-08-11 19:04:21 +00:00
|
|
|
(struct malloc_task *)bdev_io->driver_ctx,
|
2017-09-20 13:10:17 +00:00
|
|
|
bdev_io->u.bdev.offset_blocks * block_size,
|
|
|
|
bdev_io->u.bdev.num_blocks * block_size);
|
2017-08-11 19:04:21 +00:00
|
|
|
|
2017-11-07 22:11:22 +00:00
|
|
|
case SPDK_BDEV_IO_TYPE_ZCOPY:
|
|
|
|
if (bdev_io->u.bdev.zcopy.start) {
|
|
|
|
void *buf;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
buf = ((struct malloc_disk *)bdev_io->bdev->ctxt)->malloc_buf +
|
|
|
|
bdev_io->u.bdev.offset_blocks * block_size;
|
|
|
|
len = bdev_io->u.bdev.num_blocks * block_size;
|
|
|
|
spdk_bdev_io_set_buf(bdev_io, buf, len);
|
|
|
|
|
|
|
|
}
|
2021-12-16 10:36:57 +00:00
|
|
|
malloc_complete_task((struct malloc_task *)bdev_io->driver_ctx, mch,
|
|
|
|
SPDK_BDEV_IO_STATUS_SUCCESS);
|
2020-06-06 06:20:45 +00:00
|
|
|
return 0;
|
|
|
|
case SPDK_BDEV_IO_TYPE_ABORT:
|
2021-12-16 10:36:57 +00:00
|
|
|
malloc_complete_task((struct malloc_task *)bdev_io->driver_ctx, mch,
|
|
|
|
SPDK_BDEV_IO_STATUS_FAILED);
|
2017-11-07 22:11:22 +00:00
|
|
|
return 0;
|
2016-07-20 18:16:23 +00:00
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-06-22 21:35:04 +00:00
|
|
|
static void
|
|
|
|
bdev_malloc_submit_request(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io)
|
2016-07-20 18:16:23 +00:00
|
|
|
{
|
2021-12-16 10:36:57 +00:00
|
|
|
struct malloc_channel *mch = spdk_io_channel_get_ctx(ch);
|
|
|
|
|
|
|
|
if (_bdev_malloc_submit_request(mch, bdev_io) != 0) {
|
|
|
|
malloc_complete_task((struct malloc_task *)bdev_io->driver_ctx, mch,
|
|
|
|
SPDK_BDEV_IO_STATUS_FAILED);
|
2016-07-20 18:16:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-24 17:25:49 +00:00
|
|
|
static bool
|
2017-07-13 04:08:53 +00:00
|
|
|
bdev_malloc_io_type_supported(void *ctx, enum spdk_bdev_io_type io_type)
|
2016-08-24 17:25:49 +00:00
|
|
|
{
|
|
|
|
switch (io_type) {
|
|
|
|
case SPDK_BDEV_IO_TYPE_READ:
|
|
|
|
case SPDK_BDEV_IO_TYPE_WRITE:
|
|
|
|
case SPDK_BDEV_IO_TYPE_FLUSH:
|
|
|
|
case SPDK_BDEV_IO_TYPE_RESET:
|
2016-08-26 22:35:02 +00:00
|
|
|
case SPDK_BDEV_IO_TYPE_UNMAP:
|
2017-08-11 19:04:21 +00:00
|
|
|
case SPDK_BDEV_IO_TYPE_WRITE_ZEROES:
|
2017-11-07 22:11:22 +00:00
|
|
|
case SPDK_BDEV_IO_TYPE_ZCOPY:
|
2020-06-06 06:20:45 +00:00
|
|
|
case SPDK_BDEV_IO_TYPE_ABORT:
|
2016-08-24 17:25:49 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-16 19:53:32 +00:00
|
|
|
static struct spdk_io_channel *
|
2017-07-13 04:08:53 +00:00
|
|
|
bdev_malloc_get_io_channel(void *ctx)
|
2016-09-16 19:53:32 +00:00
|
|
|
{
|
2021-12-16 08:28:25 +00:00
|
|
|
return spdk_get_io_channel(&g_malloc_disks);
|
2016-09-16 19:53:32 +00:00
|
|
|
}
|
|
|
|
|
2018-02-23 18:18:42 +00:00
|
|
|
static void
|
|
|
|
bdev_malloc_write_json_config(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w)
|
|
|
|
{
|
2018-03-23 16:22:57 +00:00
|
|
|
char uuid_str[SPDK_UUID_STRING_LEN];
|
|
|
|
|
2018-02-23 18:18:42 +00:00
|
|
|
spdk_json_write_object_begin(w);
|
|
|
|
|
2019-08-09 11:15:35 +00:00
|
|
|
spdk_json_write_named_string(w, "method", "bdev_malloc_create");
|
2018-02-23 18:18:42 +00:00
|
|
|
|
|
|
|
spdk_json_write_named_object_begin(w, "params");
|
|
|
|
spdk_json_write_named_string(w, "name", bdev->name);
|
|
|
|
spdk_json_write_named_uint64(w, "num_blocks", bdev->blockcnt);
|
|
|
|
spdk_json_write_named_uint32(w, "block_size", bdev->blocklen);
|
2018-03-23 16:22:57 +00:00
|
|
|
spdk_uuid_fmt_lower(uuid_str, sizeof(uuid_str), &bdev->uuid);
|
|
|
|
spdk_json_write_named_string(w, "uuid", uuid_str);
|
2021-10-29 13:48:49 +00:00
|
|
|
spdk_json_write_named_uint32(w, "optimal_io_boundary", bdev->optimal_io_boundary);
|
2018-03-23 16:22:57 +00:00
|
|
|
|
2018-02-23 18:18:42 +00:00
|
|
|
spdk_json_write_object_end(w);
|
|
|
|
|
|
|
|
spdk_json_write_object_end(w);
|
|
|
|
}
|
|
|
|
|
2016-08-30 19:56:06 +00:00
|
|
|
static const struct spdk_bdev_fn_table malloc_fn_table = {
|
2017-07-13 04:08:53 +00:00
|
|
|
.destruct = bdev_malloc_destruct,
|
|
|
|
.submit_request = bdev_malloc_submit_request,
|
|
|
|
.io_type_supported = bdev_malloc_io_type_supported,
|
|
|
|
.get_io_channel = bdev_malloc_get_io_channel,
|
2018-02-23 18:18:42 +00:00
|
|
|
.write_config_json = bdev_malloc_write_json_config,
|
2016-07-20 18:16:23 +00:00
|
|
|
};
|
|
|
|
|
2019-07-01 11:05:24 +00:00
|
|
|
int
|
2022-10-23 12:46:36 +00:00
|
|
|
create_malloc_disk(struct spdk_bdev **bdev, const struct malloc_bdev_opts *opts)
|
2016-07-20 18:16:23 +00:00
|
|
|
{
|
2022-10-24 13:26:19 +00:00
|
|
|
struct malloc_disk *mdisk;
|
|
|
|
uint32_t block_size;
|
2019-07-01 11:05:24 +00:00
|
|
|
int rc;
|
2016-07-20 18:16:23 +00:00
|
|
|
|
2022-10-23 12:46:36 +00:00
|
|
|
assert(opts != NULL);
|
|
|
|
|
|
|
|
if (opts->num_blocks == 0) {
|
2019-07-01 11:05:24 +00:00
|
|
|
SPDK_ERRLOG("Disk num_blocks must be greater than 0");
|
|
|
|
return -EINVAL;
|
2016-07-20 18:16:23 +00:00
|
|
|
}
|
|
|
|
|
2022-10-23 12:46:36 +00:00
|
|
|
if (opts->block_size % 512) {
|
2022-10-24 13:26:19 +00:00
|
|
|
SPDK_ERRLOG("Data block size must be 512 bytes aligned\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (opts->md_size) {
|
|
|
|
case 0:
|
|
|
|
case 8:
|
|
|
|
case 16:
|
|
|
|
case 32:
|
|
|
|
case 64:
|
|
|
|
case 128:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
SPDK_ERRLOG("metadata size %u is not supported\n", opts->md_size);
|
2021-02-26 12:19:53 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-10-24 13:26:19 +00:00
|
|
|
if (opts->md_interleave) {
|
|
|
|
block_size = opts->block_size + opts->md_size;
|
|
|
|
} else {
|
|
|
|
block_size = opts->block_size;
|
|
|
|
}
|
|
|
|
|
2019-04-05 08:43:15 +00:00
|
|
|
mdisk = calloc(1, sizeof(*mdisk));
|
2016-07-20 18:16:23 +00:00
|
|
|
if (!mdisk) {
|
2019-04-05 08:43:15 +00:00
|
|
|
SPDK_ERRLOG("mdisk calloc() failed\n");
|
2019-07-01 11:05:24 +00:00
|
|
|
return -ENOMEM;
|
2016-07-20 18:16:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2016-08-17 20:35:18 +00:00
|
|
|
* Allocate the large backend memory buffer from pinned memory.
|
2016-07-20 18:16:23 +00:00
|
|
|
*
|
|
|
|
* TODO: need to pass a hint so we know which socket to allocate
|
|
|
|
* from on multi-socket systems.
|
|
|
|
*/
|
2022-10-24 13:26:19 +00:00
|
|
|
mdisk->malloc_buf = spdk_zmalloc(opts->num_blocks * block_size, 2 * 1024 * 1024, NULL,
|
2019-04-05 08:43:15 +00:00
|
|
|
SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
|
2016-07-20 18:16:23 +00:00
|
|
|
if (!mdisk->malloc_buf) {
|
2019-04-05 08:43:15 +00:00
|
|
|
SPDK_ERRLOG("malloc_buf spdk_zmalloc() failed\n");
|
2017-06-02 17:25:43 +00:00
|
|
|
malloc_disk_free(mdisk);
|
2019-07-01 11:05:24 +00:00
|
|
|
return -ENOMEM;
|
2016-07-20 18:16:23 +00:00
|
|
|
}
|
|
|
|
|
2022-10-24 13:26:19 +00:00
|
|
|
if (!opts->md_interleave && opts->md_size != 0) {
|
|
|
|
mdisk->malloc_md_buf = spdk_zmalloc(opts->num_blocks * opts->md_size, 2 * 1024 * 1024, NULL,
|
|
|
|
SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
|
|
|
|
if (!mdisk->malloc_md_buf) {
|
|
|
|
SPDK_ERRLOG("malloc_md_buf spdk_zmalloc() failed\n");
|
|
|
|
malloc_disk_free(mdisk);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-23 12:46:36 +00:00
|
|
|
if (opts->name) {
|
|
|
|
mdisk->disk.name = strdup(opts->name);
|
2017-10-06 22:00:28 +00:00
|
|
|
} else {
|
|
|
|
/* Auto-generate a name */
|
|
|
|
mdisk->disk.name = spdk_sprintf_alloc("Malloc%d", malloc_disk_count);
|
|
|
|
malloc_disk_count++;
|
|
|
|
}
|
2017-06-02 17:25:43 +00:00
|
|
|
if (!mdisk->disk.name) {
|
|
|
|
malloc_disk_free(mdisk);
|
2019-07-01 11:05:24 +00:00
|
|
|
return -ENOMEM;
|
2017-06-02 17:25:43 +00:00
|
|
|
}
|
|
|
|
mdisk->disk.product_name = "Malloc disk";
|
2016-07-20 18:16:23 +00:00
|
|
|
|
|
|
|
mdisk->disk.write_cache = 1;
|
2022-10-24 13:26:19 +00:00
|
|
|
mdisk->disk.blocklen = block_size;
|
2022-10-23 12:46:36 +00:00
|
|
|
mdisk->disk.blockcnt = opts->num_blocks;
|
2022-10-24 13:26:19 +00:00
|
|
|
mdisk->disk.md_len = opts->md_size;
|
|
|
|
mdisk->disk.md_interleave = opts->md_interleave;
|
2022-10-23 12:46:36 +00:00
|
|
|
if (opts->optimal_io_boundary) {
|
|
|
|
mdisk->disk.optimal_io_boundary = opts->optimal_io_boundary;
|
2021-10-29 13:48:49 +00:00
|
|
|
mdisk->disk.split_on_optimal_io_boundary = true;
|
|
|
|
}
|
2022-10-23 12:46:36 +00:00
|
|
|
if (!spdk_mem_all_zero(&opts->uuid, sizeof(opts->uuid))) {
|
|
|
|
spdk_uuid_copy(&mdisk->disk.uuid, &opts->uuid);
|
2018-03-08 21:35:44 +00:00
|
|
|
} else {
|
|
|
|
spdk_uuid_generate(&mdisk->disk.uuid);
|
|
|
|
}
|
2016-07-20 18:16:23 +00:00
|
|
|
|
|
|
|
mdisk->disk.ctxt = mdisk;
|
|
|
|
mdisk->disk.fn_table = &malloc_fn_table;
|
2018-03-06 18:52:46 +00:00
|
|
|
mdisk->disk.module = &malloc_if;
|
2016-07-20 18:16:23 +00:00
|
|
|
|
2017-11-20 09:31:39 +00:00
|
|
|
rc = spdk_bdev_register(&mdisk->disk);
|
|
|
|
if (rc) {
|
|
|
|
malloc_disk_free(mdisk);
|
2019-07-01 11:05:24 +00:00
|
|
|
return rc;
|
2017-11-20 09:31:39 +00:00
|
|
|
}
|
2016-07-20 18:16:23 +00:00
|
|
|
|
2019-07-01 11:05:24 +00:00
|
|
|
*bdev = &(mdisk->disk);
|
|
|
|
|
2017-12-01 22:19:23 +00:00
|
|
|
TAILQ_INSERT_TAIL(&g_malloc_disks, mdisk, link);
|
2016-07-20 18:16:23 +00:00
|
|
|
|
2019-07-01 11:05:24 +00:00
|
|
|
return rc;
|
2016-07-20 18:16:23 +00:00
|
|
|
}
|
|
|
|
|
2018-06-15 07:58:57 +00:00
|
|
|
void
|
2022-03-29 05:55:53 +00:00
|
|
|
delete_malloc_disk(const char *name, spdk_delete_malloc_complete cb_fn, void *cb_arg)
|
2018-06-15 07:58:57 +00:00
|
|
|
{
|
2022-03-29 05:55:53 +00:00
|
|
|
int rc;
|
2018-06-15 07:58:57 +00:00
|
|
|
|
2022-03-29 05:55:53 +00:00
|
|
|
rc = spdk_bdev_unregister_by_name(name, &malloc_if, cb_fn, cb_arg);
|
|
|
|
if (rc != 0) {
|
|
|
|
cb_fn(cb_arg, rc);
|
|
|
|
}
|
2018-06-15 07:58:57 +00:00
|
|
|
}
|
|
|
|
|
2021-12-16 09:45:10 +00:00
|
|
|
static int
|
|
|
|
malloc_completion_poller(void *ctx)
|
|
|
|
{
|
|
|
|
struct malloc_channel *ch = ctx;
|
|
|
|
struct malloc_task *task;
|
|
|
|
TAILQ_HEAD(, malloc_task) completed_tasks;
|
|
|
|
uint32_t num_completions = 0;
|
|
|
|
|
|
|
|
TAILQ_INIT(&completed_tasks);
|
|
|
|
TAILQ_SWAP(&completed_tasks, &ch->completed_tasks, malloc_task, tailq);
|
|
|
|
|
|
|
|
while (!TAILQ_EMPTY(&completed_tasks)) {
|
|
|
|
task = TAILQ_FIRST(&completed_tasks);
|
|
|
|
TAILQ_REMOVE(&completed_tasks, task, tailq);
|
|
|
|
spdk_bdev_io_complete(spdk_bdev_io_from_ctx(task), task->status);
|
|
|
|
num_completions++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return num_completions > 0 ? SPDK_POLLER_BUSY : SPDK_POLLER_IDLE;
|
|
|
|
}
|
|
|
|
|
2021-12-16 08:28:25 +00:00
|
|
|
static int
|
|
|
|
malloc_create_channel_cb(void *io_device, void *ctx)
|
|
|
|
{
|
|
|
|
struct malloc_channel *ch = ctx;
|
|
|
|
|
2022-08-08 20:51:25 +00:00
|
|
|
ch->accel_channel = spdk_accel_get_io_channel();
|
2021-12-16 08:28:25 +00:00
|
|
|
if (!ch->accel_channel) {
|
2022-08-08 20:51:25 +00:00
|
|
|
SPDK_ERRLOG("Failed to get accel framework's IO channel\n");
|
2021-12-16 08:28:25 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2021-12-16 09:45:10 +00:00
|
|
|
ch->completion_poller = SPDK_POLLER_REGISTER(malloc_completion_poller, ch, 0);
|
|
|
|
if (!ch->completion_poller) {
|
|
|
|
SPDK_ERRLOG("Failed to register malloc completion poller\n");
|
|
|
|
spdk_put_io_channel(ch->accel_channel);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
TAILQ_INIT(&ch->completed_tasks);
|
|
|
|
|
2021-12-16 08:28:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
malloc_destroy_channel_cb(void *io_device, void *ctx)
|
|
|
|
{
|
|
|
|
struct malloc_channel *ch = ctx;
|
|
|
|
|
2021-12-16 09:45:10 +00:00
|
|
|
assert(TAILQ_EMPTY(&ch->completed_tasks));
|
|
|
|
|
2021-12-16 08:28:25 +00:00
|
|
|
spdk_put_io_channel(ch->accel_channel);
|
2021-12-16 09:45:10 +00:00
|
|
|
spdk_poller_unregister(&ch->completion_poller);
|
2021-12-16 08:28:25 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 21:35:04 +00:00
|
|
|
static int
|
|
|
|
bdev_malloc_initialize(void)
|
2016-07-20 18:16:23 +00:00
|
|
|
{
|
2020-10-12 10:59:08 +00:00
|
|
|
/* This needs to be reset for each reinitialization of submodules.
|
|
|
|
* Otherwise after enough devices or reinitializations the value gets too high.
|
|
|
|
* TODO: Make malloc bdev name mandatory and remove this counter. */
|
2020-05-08 06:03:22 +00:00
|
|
|
malloc_disk_count = 0;
|
2021-12-16 08:28:25 +00:00
|
|
|
|
|
|
|
spdk_io_device_register(&g_malloc_disks, malloc_create_channel_cb,
|
|
|
|
malloc_destroy_channel_cb, sizeof(struct malloc_channel),
|
|
|
|
"bdev_malloc");
|
|
|
|
|
2020-10-12 10:59:08 +00:00
|
|
|
return 0;
|
2016-07-20 18:16:23 +00:00
|
|
|
}
|
|
|
|
|
2021-12-16 08:28:25 +00:00
|
|
|
static void
|
|
|
|
bdev_malloc_deinitialize(void)
|
|
|
|
{
|
|
|
|
spdk_io_device_unregister(&g_malloc_disks, NULL);
|
|
|
|
}
|
|
|
|
|
2020-09-04 11:27:29 +00:00
|
|
|
SPDK_LOG_REGISTER_COMPONENT(bdev_malloc)
|