Line data Source code
1 : /* SPDX-License-Identifier: BSD-3-Clause
2 : * Copyright (C) 2017 Intel Corporation.
3 : * All rights reserved.
4 : * Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
5 : */
6 :
7 : #include "spdk/stdinc.h"
8 :
9 : #include "bdev_malloc.h"
10 : #include "spdk/endian.h"
11 : #include "spdk/env.h"
12 : #include "spdk/accel.h"
13 : #include "spdk/dma.h"
14 : #include "spdk/likely.h"
15 : #include "spdk/string.h"
16 :
17 : #include "spdk/log.h"
18 :
19 : struct malloc_disk {
20 : struct spdk_bdev disk;
21 : void *malloc_buf;
22 : void *malloc_md_buf;
23 : TAILQ_ENTRY(malloc_disk) link;
24 : };
25 :
26 : struct malloc_task {
27 : struct iovec iov;
28 : int num_outstanding;
29 : enum spdk_bdev_io_status status;
30 : TAILQ_ENTRY(malloc_task) tailq;
31 : };
32 :
33 : struct malloc_channel {
34 : struct spdk_io_channel *accel_channel;
35 : struct spdk_poller *completion_poller;
36 : TAILQ_HEAD(, malloc_task) completed_tasks;
37 : };
38 :
39 : static int
40 0 : malloc_verify_pi(struct spdk_bdev_io *bdev_io)
41 : {
42 0 : struct spdk_bdev *bdev = bdev_io->bdev;
43 0 : struct spdk_dif_ctx dif_ctx;
44 0 : struct spdk_dif_error err_blk;
45 : int rc;
46 0 : struct spdk_dif_ctx_init_ext_opts dif_opts;
47 :
48 0 : assert(bdev_io->u.bdev.memory_domain == NULL);
49 0 : dif_opts.size = SPDK_SIZEOF(&dif_opts, dif_pi_format);
50 0 : dif_opts.dif_pi_format = bdev->dif_pi_format;
51 0 : rc = spdk_dif_ctx_init(&dif_ctx,
52 : bdev->blocklen,
53 : bdev->md_len,
54 0 : bdev->md_interleave,
55 0 : bdev->dif_is_head_of_md,
56 : bdev->dif_type,
57 : bdev_io->u.bdev.dif_check_flags,
58 0 : bdev_io->u.bdev.offset_blocks & 0xFFFFFFFF,
59 : 0xFFFF, 0, 0, 0, &dif_opts);
60 0 : if (rc != 0) {
61 0 : SPDK_ERRLOG("Failed to initialize DIF/DIX context\n");
62 0 : return rc;
63 : }
64 :
65 0 : if (spdk_bdev_is_md_interleaved(bdev)) {
66 0 : rc = spdk_dif_verify(bdev_io->u.bdev.iovs,
67 : bdev_io->u.bdev.iovcnt,
68 0 : bdev_io->u.bdev.num_blocks,
69 : &dif_ctx,
70 : &err_blk);
71 : } else {
72 0 : struct iovec md_iov = {
73 0 : .iov_base = bdev_io->u.bdev.md_buf,
74 0 : .iov_len = bdev_io->u.bdev.num_blocks * bdev->md_len,
75 : };
76 :
77 0 : if (bdev_io->u.bdev.md_buf == NULL) {
78 0 : return 0;
79 : }
80 :
81 0 : rc = spdk_dix_verify(bdev_io->u.bdev.iovs,
82 : bdev_io->u.bdev.iovcnt,
83 : &md_iov,
84 0 : bdev_io->u.bdev.num_blocks,
85 : &dif_ctx,
86 : &err_blk);
87 : }
88 :
89 0 : if (rc != 0) {
90 0 : SPDK_ERRLOG("DIF/DIX verify failed: lba %" PRIu64 ", num_blocks %" PRIu64 ", "
91 : "err_type %u, expected %lu, actual %lu, err_offset %u\n",
92 : bdev_io->u.bdev.offset_blocks,
93 : bdev_io->u.bdev.num_blocks,
94 : err_blk.err_type,
95 : err_blk.expected,
96 : err_blk.actual,
97 : err_blk.err_offset);
98 : }
99 :
100 0 : return rc;
101 : }
102 :
103 : static int
104 0 : malloc_unmap_write_zeroes_generate_pi(struct spdk_bdev_io *bdev_io)
105 : {
106 0 : struct spdk_bdev *bdev = bdev_io->bdev;
107 0 : struct malloc_disk *mdisk = bdev_io->bdev->ctxt;
108 0 : uint32_t block_size = bdev_io->bdev->blocklen;
109 0 : struct spdk_dif_ctx dif_ctx;
110 0 : struct spdk_dif_ctx_init_ext_opts dif_opts;
111 : int rc;
112 :
113 0 : dif_opts.size = SPDK_SIZEOF(&dif_opts, dif_pi_format);
114 0 : dif_opts.dif_pi_format = bdev->dif_pi_format;
115 0 : rc = spdk_dif_ctx_init(&dif_ctx,
116 : bdev->blocklen,
117 : bdev->md_len,
118 0 : bdev->md_interleave,
119 0 : bdev->dif_is_head_of_md,
120 : bdev->dif_type,
121 : bdev->dif_check_flags,
122 : SPDK_DIF_REFTAG_IGNORE,
123 : 0xFFFF, SPDK_DIF_APPTAG_IGNORE,
124 : 0, 0, &dif_opts);
125 0 : if (rc != 0) {
126 0 : SPDK_ERRLOG("Initialization of DIF/DIX context failed\n");
127 0 : return rc;
128 : }
129 :
130 0 : if (bdev->md_interleave) {
131 0 : struct iovec iov = {
132 0 : .iov_base = mdisk->malloc_buf + bdev_io->u.bdev.offset_blocks * block_size,
133 0 : .iov_len = bdev_io->u.bdev.num_blocks * block_size,
134 : };
135 :
136 0 : rc = spdk_dif_generate(&iov, 1, bdev_io->u.bdev.num_blocks, &dif_ctx);
137 : } else {
138 0 : struct iovec iov = {
139 0 : .iov_base = mdisk->malloc_buf + bdev_io->u.bdev.offset_blocks * block_size,
140 0 : .iov_len = bdev_io->u.bdev.num_blocks * block_size,
141 : };
142 :
143 0 : struct iovec md_iov = {
144 0 : .iov_base = mdisk->malloc_md_buf + bdev_io->u.bdev.offset_blocks * bdev->md_len,
145 0 : .iov_len = bdev_io->u.bdev.num_blocks * bdev->md_len,
146 : };
147 :
148 0 : rc = spdk_dix_generate(&iov, 1, &md_iov, bdev_io->u.bdev.num_blocks, &dif_ctx);
149 : }
150 :
151 0 : if (rc != 0) {
152 0 : SPDK_ERRLOG("Formatting by DIF/DIX failed\n");
153 : }
154 :
155 :
156 0 : return rc;
157 : }
158 :
159 : static void
160 0 : malloc_done(void *ref, int status)
161 : {
162 0 : struct malloc_task *task = (struct malloc_task *)ref;
163 0 : struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(task);
164 : int rc;
165 :
166 0 : if (status != 0) {
167 0 : if (status == -ENOMEM) {
168 0 : if (task->status == SPDK_BDEV_IO_STATUS_SUCCESS) {
169 0 : task->status = SPDK_BDEV_IO_STATUS_NOMEM;
170 : }
171 : } else {
172 0 : task->status = SPDK_BDEV_IO_STATUS_FAILED;
173 : }
174 : }
175 :
176 0 : if (--task->num_outstanding != 0) {
177 0 : return;
178 : }
179 :
180 0 : if (bdev_io->bdev->dif_type != SPDK_DIF_DISABLE &&
181 0 : bdev_io->type == SPDK_BDEV_IO_TYPE_READ &&
182 0 : task->status == SPDK_BDEV_IO_STATUS_SUCCESS) {
183 0 : rc = malloc_verify_pi(bdev_io);
184 0 : if (rc != 0) {
185 0 : task->status = SPDK_BDEV_IO_STATUS_FAILED;
186 : }
187 : }
188 :
189 0 : if (bdev_io->bdev->dif_type != SPDK_DIF_DISABLE &&
190 0 : (bdev_io->type == SPDK_BDEV_IO_TYPE_UNMAP || bdev_io->type == SPDK_BDEV_IO_TYPE_WRITE_ZEROES) &&
191 0 : task->status == SPDK_BDEV_IO_STATUS_SUCCESS) {
192 0 : rc = malloc_unmap_write_zeroes_generate_pi(bdev_io);
193 0 : if (rc != 0) {
194 0 : task->status = SPDK_BDEV_IO_STATUS_FAILED;
195 : }
196 : }
197 :
198 0 : assert(!bdev_io->u.bdev.accel_sequence || task->status == SPDK_BDEV_IO_STATUS_NOMEM);
199 0 : spdk_bdev_io_complete(spdk_bdev_io_from_ctx(task), task->status);
200 : }
201 :
202 : static void
203 0 : malloc_complete_task(struct malloc_task *task, struct malloc_channel *mch,
204 : enum spdk_bdev_io_status status)
205 : {
206 0 : task->status = status;
207 0 : TAILQ_INSERT_TAIL(&mch->completed_tasks, task, tailq);
208 0 : }
209 :
210 : static TAILQ_HEAD(, malloc_disk) g_malloc_disks = TAILQ_HEAD_INITIALIZER(g_malloc_disks);
211 :
212 : int malloc_disk_count = 0;
213 :
214 : static int bdev_malloc_initialize(void);
215 : static void bdev_malloc_deinitialize(void);
216 :
217 : static int
218 0 : bdev_malloc_get_ctx_size(void)
219 : {
220 0 : return sizeof(struct malloc_task);
221 : }
222 :
223 : static struct spdk_bdev_module malloc_if = {
224 : .name = "malloc",
225 : .module_init = bdev_malloc_initialize,
226 : .module_fini = bdev_malloc_deinitialize,
227 : .get_ctx_size = bdev_malloc_get_ctx_size,
228 :
229 : };
230 :
231 0 : SPDK_BDEV_MODULE_REGISTER(malloc, &malloc_if)
232 :
233 : static void
234 0 : malloc_disk_free(struct malloc_disk *malloc_disk)
235 : {
236 0 : if (!malloc_disk) {
237 0 : return;
238 : }
239 :
240 0 : free(malloc_disk->disk.name);
241 0 : spdk_free(malloc_disk->malloc_buf);
242 0 : spdk_free(malloc_disk->malloc_md_buf);
243 0 : free(malloc_disk);
244 : }
245 :
246 : static int
247 0 : bdev_malloc_destruct(void *ctx)
248 : {
249 0 : struct malloc_disk *malloc_disk = ctx;
250 :
251 0 : TAILQ_REMOVE(&g_malloc_disks, malloc_disk, link);
252 0 : malloc_disk_free(malloc_disk);
253 0 : return 0;
254 : }
255 :
256 : static int
257 0 : bdev_malloc_check_iov_len(struct iovec *iovs, int iovcnt, size_t nbytes)
258 : {
259 : int i;
260 :
261 0 : for (i = 0; i < iovcnt; i++) {
262 0 : if (nbytes < iovs[i].iov_len) {
263 0 : return 0;
264 : }
265 :
266 0 : nbytes -= iovs[i].iov_len;
267 : }
268 :
269 0 : return nbytes != 0;
270 : }
271 :
272 : static size_t
273 0 : malloc_get_md_len(struct spdk_bdev_io *bdev_io)
274 : {
275 0 : return bdev_io->u.bdev.num_blocks * bdev_io->bdev->md_len;
276 : }
277 :
278 : static uint64_t
279 0 : malloc_get_md_offset(struct spdk_bdev_io *bdev_io)
280 : {
281 0 : return bdev_io->u.bdev.offset_blocks * bdev_io->bdev->md_len;
282 : }
283 :
284 : static void *
285 0 : malloc_get_md_buf(struct spdk_bdev_io *bdev_io)
286 : {
287 0 : struct malloc_disk *mdisk = SPDK_CONTAINEROF(bdev_io->bdev, struct malloc_disk, disk);
288 :
289 0 : assert(spdk_bdev_is_md_separate(bdev_io->bdev));
290 :
291 0 : return (char *)mdisk->malloc_md_buf + malloc_get_md_offset(bdev_io);
292 : }
293 :
294 : static void
295 0 : malloc_sequence_fail(struct malloc_task *task, int status)
296 : {
297 0 : struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(task);
298 :
299 : /* For ENOMEM, the IO will be retried by the bdev layer, so we don't abort the sequence */
300 0 : if (status != -ENOMEM) {
301 0 : spdk_accel_sequence_abort(bdev_io->u.bdev.accel_sequence);
302 0 : bdev_io->u.bdev.accel_sequence = NULL;
303 : }
304 :
305 0 : malloc_done(task, status);
306 0 : }
307 :
308 : static void
309 0 : malloc_sequence_done(void *ctx, int status)
310 : {
311 0 : struct malloc_task *task = ctx;
312 0 : struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(task);
313 :
314 0 : bdev_io->u.bdev.accel_sequence = NULL;
315 : /* Prevent bdev layer from retrying the request if the sequence failed with ENOMEM */
316 0 : malloc_done(task, status != -ENOMEM ? status : -EFAULT);
317 0 : }
318 :
319 : static void
320 0 : bdev_malloc_readv(struct malloc_disk *mdisk, struct spdk_io_channel *ch,
321 : struct malloc_task *task, struct spdk_bdev_io *bdev_io)
322 : {
323 : uint64_t len, offset;
324 0 : int res = 0;
325 :
326 0 : len = bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen;
327 0 : offset = bdev_io->u.bdev.offset_blocks * bdev_io->bdev->blocklen;
328 :
329 0 : if (bdev_malloc_check_iov_len(bdev_io->u.bdev.iovs, bdev_io->u.bdev.iovcnt, len)) {
330 0 : spdk_bdev_io_complete(spdk_bdev_io_from_ctx(task),
331 : SPDK_BDEV_IO_STATUS_FAILED);
332 0 : return;
333 : }
334 :
335 0 : task->status = SPDK_BDEV_IO_STATUS_SUCCESS;
336 0 : task->num_outstanding = 0;
337 0 : task->iov.iov_base = mdisk->malloc_buf + offset;
338 0 : task->iov.iov_len = len;
339 :
340 0 : SPDK_DEBUGLOG(bdev_malloc, "read %zu bytes from offset %#" PRIx64 ", iovcnt=%d\n",
341 : len, offset, bdev_io->u.bdev.iovcnt);
342 :
343 0 : task->num_outstanding++;
344 0 : res = spdk_accel_append_copy(&bdev_io->u.bdev.accel_sequence, ch,
345 0 : bdev_io->u.bdev.iovs, bdev_io->u.bdev.iovcnt,
346 : bdev_io->u.bdev.memory_domain,
347 : bdev_io->u.bdev.memory_domain_ctx,
348 : &task->iov, 1, NULL, NULL, NULL, NULL);
349 0 : if (spdk_unlikely(res != 0)) {
350 0 : malloc_sequence_fail(task, res);
351 0 : return;
352 : }
353 :
354 0 : spdk_accel_sequence_reverse(bdev_io->u.bdev.accel_sequence);
355 0 : spdk_accel_sequence_finish(bdev_io->u.bdev.accel_sequence, malloc_sequence_done, task);
356 :
357 0 : if (bdev_io->u.bdev.md_buf == NULL) {
358 0 : return;
359 : }
360 :
361 0 : SPDK_DEBUGLOG(bdev_malloc, "read metadata %zu bytes from offset%#" PRIx64 "\n",
362 : malloc_get_md_len(bdev_io), malloc_get_md_offset(bdev_io));
363 :
364 0 : task->num_outstanding++;
365 0 : res = spdk_accel_submit_copy(ch, bdev_io->u.bdev.md_buf, malloc_get_md_buf(bdev_io),
366 : malloc_get_md_len(bdev_io), malloc_done, task);
367 0 : if (res != 0) {
368 0 : malloc_done(task, res);
369 : }
370 : }
371 :
372 : static void
373 0 : bdev_malloc_writev(struct malloc_disk *mdisk, struct spdk_io_channel *ch,
374 : struct malloc_task *task, struct spdk_bdev_io *bdev_io)
375 : {
376 : uint64_t len, offset;
377 0 : int res = 0;
378 :
379 0 : len = bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen;
380 0 : offset = bdev_io->u.bdev.offset_blocks * bdev_io->bdev->blocklen;
381 :
382 0 : if (bdev_malloc_check_iov_len(bdev_io->u.bdev.iovs, bdev_io->u.bdev.iovcnt, len)) {
383 0 : spdk_bdev_io_complete(spdk_bdev_io_from_ctx(task),
384 : SPDK_BDEV_IO_STATUS_FAILED);
385 0 : return;
386 : }
387 :
388 0 : task->status = SPDK_BDEV_IO_STATUS_SUCCESS;
389 0 : task->num_outstanding = 0;
390 0 : task->iov.iov_base = mdisk->malloc_buf + offset;
391 0 : task->iov.iov_len = len;
392 :
393 0 : SPDK_DEBUGLOG(bdev_malloc, "write %zu bytes to offset %#" PRIx64 ", iovcnt=%d\n",
394 : len, offset, bdev_io->u.bdev.iovcnt);
395 :
396 0 : task->num_outstanding++;
397 0 : res = spdk_accel_append_copy(&bdev_io->u.bdev.accel_sequence, ch, &task->iov, 1, NULL, NULL,
398 0 : bdev_io->u.bdev.iovs, bdev_io->u.bdev.iovcnt,
399 : bdev_io->u.bdev.memory_domain,
400 : bdev_io->u.bdev.memory_domain_ctx, NULL, NULL);
401 0 : if (spdk_unlikely(res != 0)) {
402 0 : malloc_sequence_fail(task, res);
403 0 : return;
404 : }
405 :
406 0 : spdk_accel_sequence_finish(bdev_io->u.bdev.accel_sequence, malloc_sequence_done, task);
407 :
408 0 : if (bdev_io->u.bdev.md_buf == NULL) {
409 0 : return;
410 : }
411 :
412 0 : SPDK_DEBUGLOG(bdev_malloc, "write metadata %zu bytes to offset %#" PRIx64 "\n",
413 : malloc_get_md_len(bdev_io), malloc_get_md_offset(bdev_io));
414 :
415 0 : task->num_outstanding++;
416 0 : res = spdk_accel_submit_copy(ch, malloc_get_md_buf(bdev_io), bdev_io->u.bdev.md_buf,
417 : malloc_get_md_len(bdev_io), malloc_done, task);
418 0 : if (res != 0) {
419 0 : malloc_done(task, res);
420 : }
421 : }
422 :
423 : static int
424 0 : bdev_malloc_unmap(struct malloc_disk *mdisk,
425 : struct spdk_io_channel *ch,
426 : struct malloc_task *task,
427 : uint64_t offset,
428 : uint64_t byte_count)
429 : {
430 0 : task->status = SPDK_BDEV_IO_STATUS_SUCCESS;
431 0 : task->num_outstanding = 1;
432 :
433 0 : return spdk_accel_submit_fill(ch, mdisk->malloc_buf + offset, 0,
434 : byte_count, malloc_done, task);
435 : }
436 :
437 : static void
438 0 : bdev_malloc_copy(struct malloc_disk *mdisk, struct spdk_io_channel *ch,
439 : struct malloc_task *task,
440 : uint64_t dst_offset, uint64_t src_offset, size_t len)
441 : {
442 0 : int64_t res = 0;
443 0 : void *dst = mdisk->malloc_buf + dst_offset;
444 0 : void *src = mdisk->malloc_buf + src_offset;
445 :
446 0 : SPDK_DEBUGLOG(bdev_malloc, "Copy %zu bytes from offset %#" PRIx64 " to offset %#" PRIx64 "\n",
447 : len, src_offset, dst_offset);
448 :
449 0 : task->status = SPDK_BDEV_IO_STATUS_SUCCESS;
450 0 : task->num_outstanding = 1;
451 :
452 0 : res = spdk_accel_submit_copy(ch, dst, src, len, malloc_done, task);
453 0 : if (res != 0) {
454 0 : malloc_done(task, res);
455 : }
456 0 : }
457 :
458 : static int
459 0 : _bdev_malloc_submit_request(struct malloc_channel *mch, struct spdk_bdev_io *bdev_io)
460 : {
461 0 : struct malloc_task *task = (struct malloc_task *)bdev_io->driver_ctx;
462 0 : struct malloc_disk *disk = bdev_io->bdev->ctxt;
463 0 : uint32_t block_size = bdev_io->bdev->blocklen;
464 : int rc;
465 :
466 0 : switch (bdev_io->type) {
467 0 : case SPDK_BDEV_IO_TYPE_READ:
468 0 : if (bdev_io->u.bdev.iovs[0].iov_base == NULL) {
469 0 : assert(bdev_io->u.bdev.iovcnt == 1);
470 0 : assert(bdev_io->u.bdev.memory_domain == NULL);
471 0 : bdev_io->u.bdev.iovs[0].iov_base =
472 0 : disk->malloc_buf + bdev_io->u.bdev.offset_blocks * block_size;
473 0 : bdev_io->u.bdev.iovs[0].iov_len = bdev_io->u.bdev.num_blocks * block_size;
474 0 : if (spdk_bdev_is_md_separate(bdev_io->bdev)) {
475 0 : spdk_bdev_io_set_md_buf(bdev_io, malloc_get_md_buf(bdev_io),
476 : malloc_get_md_len(bdev_io));
477 : }
478 0 : malloc_complete_task(task, mch, SPDK_BDEV_IO_STATUS_SUCCESS);
479 0 : return 0;
480 : }
481 :
482 0 : bdev_malloc_readv(disk, mch->accel_channel, task, bdev_io);
483 0 : return 0;
484 :
485 0 : case SPDK_BDEV_IO_TYPE_WRITE:
486 0 : if (bdev_io->bdev->dif_type != SPDK_DIF_DISABLE) {
487 0 : rc = malloc_verify_pi(bdev_io);
488 0 : if (rc != 0) {
489 0 : malloc_complete_task(task, mch, SPDK_BDEV_IO_STATUS_FAILED);
490 0 : return 0;
491 : }
492 : }
493 :
494 0 : bdev_malloc_writev(disk, mch->accel_channel, task, bdev_io);
495 0 : return 0;
496 :
497 0 : case SPDK_BDEV_IO_TYPE_RESET:
498 0 : malloc_complete_task(task, mch, SPDK_BDEV_IO_STATUS_SUCCESS);
499 0 : return 0;
500 :
501 0 : case SPDK_BDEV_IO_TYPE_FLUSH:
502 0 : malloc_complete_task(task, mch, SPDK_BDEV_IO_STATUS_SUCCESS);
503 0 : return 0;
504 :
505 0 : case SPDK_BDEV_IO_TYPE_UNMAP:
506 0 : return bdev_malloc_unmap(disk, mch->accel_channel, task,
507 0 : bdev_io->u.bdev.offset_blocks * block_size,
508 0 : bdev_io->u.bdev.num_blocks * block_size);
509 :
510 0 : case SPDK_BDEV_IO_TYPE_WRITE_ZEROES:
511 : /* bdev_malloc_unmap is implemented with a call to mem_cpy_fill which zeroes out all of the requested bytes. */
512 0 : return bdev_malloc_unmap(disk, mch->accel_channel, task,
513 0 : bdev_io->u.bdev.offset_blocks * block_size,
514 0 : bdev_io->u.bdev.num_blocks * block_size);
515 :
516 0 : case SPDK_BDEV_IO_TYPE_ZCOPY:
517 0 : if (bdev_io->u.bdev.zcopy.start) {
518 : void *buf;
519 : size_t len;
520 :
521 0 : buf = disk->malloc_buf + bdev_io->u.bdev.offset_blocks * block_size;
522 0 : len = bdev_io->u.bdev.num_blocks * block_size;
523 0 : spdk_bdev_io_set_buf(bdev_io, buf, len);
524 0 : if (spdk_bdev_is_md_separate(bdev_io->bdev)) {
525 0 : spdk_bdev_io_set_md_buf(bdev_io, malloc_get_md_buf(bdev_io),
526 : malloc_get_md_len(bdev_io));
527 : }
528 : }
529 0 : malloc_complete_task(task, mch, SPDK_BDEV_IO_STATUS_SUCCESS);
530 0 : return 0;
531 0 : case SPDK_BDEV_IO_TYPE_ABORT:
532 0 : malloc_complete_task(task, mch, SPDK_BDEV_IO_STATUS_FAILED);
533 0 : return 0;
534 0 : case SPDK_BDEV_IO_TYPE_COPY:
535 0 : bdev_malloc_copy(disk, mch->accel_channel, task,
536 0 : bdev_io->u.bdev.offset_blocks * block_size,
537 0 : bdev_io->u.bdev.copy.src_offset_blocks * block_size,
538 0 : bdev_io->u.bdev.num_blocks * block_size);
539 0 : return 0;
540 :
541 0 : default:
542 0 : return -1;
543 : }
544 : return 0;
545 : }
546 :
547 : static void
548 0 : bdev_malloc_submit_request(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io)
549 : {
550 0 : struct malloc_channel *mch = spdk_io_channel_get_ctx(ch);
551 :
552 0 : if (_bdev_malloc_submit_request(mch, bdev_io) != 0) {
553 0 : malloc_complete_task((struct malloc_task *)bdev_io->driver_ctx, mch,
554 : SPDK_BDEV_IO_STATUS_FAILED);
555 : }
556 0 : }
557 :
558 : static bool
559 0 : bdev_malloc_io_type_supported(void *ctx, enum spdk_bdev_io_type io_type)
560 : {
561 0 : switch (io_type) {
562 0 : case SPDK_BDEV_IO_TYPE_READ:
563 : case SPDK_BDEV_IO_TYPE_WRITE:
564 : case SPDK_BDEV_IO_TYPE_FLUSH:
565 : case SPDK_BDEV_IO_TYPE_RESET:
566 : case SPDK_BDEV_IO_TYPE_UNMAP:
567 : case SPDK_BDEV_IO_TYPE_WRITE_ZEROES:
568 : case SPDK_BDEV_IO_TYPE_ZCOPY:
569 : case SPDK_BDEV_IO_TYPE_ABORT:
570 : case SPDK_BDEV_IO_TYPE_COPY:
571 0 : return true;
572 :
573 0 : default:
574 0 : return false;
575 : }
576 : }
577 :
578 : static struct spdk_io_channel *
579 0 : bdev_malloc_get_io_channel(void *ctx)
580 : {
581 0 : return spdk_get_io_channel(&g_malloc_disks);
582 : }
583 :
584 : static void
585 0 : bdev_malloc_write_json_config(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w)
586 : {
587 0 : spdk_json_write_object_begin(w);
588 :
589 0 : spdk_json_write_named_string(w, "method", "bdev_malloc_create");
590 :
591 0 : spdk_json_write_named_object_begin(w, "params");
592 0 : spdk_json_write_named_string(w, "name", bdev->name);
593 0 : spdk_json_write_named_uint64(w, "num_blocks", bdev->blockcnt);
594 0 : spdk_json_write_named_uint32(w, "block_size", bdev->blocklen);
595 0 : spdk_json_write_named_uint32(w, "physical_block_size", bdev->phys_blocklen);
596 0 : spdk_json_write_named_uuid(w, "uuid", &bdev->uuid);
597 0 : spdk_json_write_named_uint32(w, "optimal_io_boundary", bdev->optimal_io_boundary);
598 0 : spdk_json_write_named_uint32(w, "md_size", bdev->md_len);
599 0 : spdk_json_write_named_uint32(w, "dif_type", bdev->dif_type);
600 0 : spdk_json_write_named_bool(w, "dif_is_head_of_md", bdev->dif_is_head_of_md);
601 0 : spdk_json_write_named_uint32(w, "dif_pi_format", bdev->dif_pi_format);
602 :
603 0 : spdk_json_write_object_end(w);
604 :
605 0 : spdk_json_write_object_end(w);
606 0 : }
607 :
608 : static int
609 0 : bdev_malloc_get_memory_domains(void *ctx, struct spdk_memory_domain **domains, int array_size)
610 : {
611 0 : struct malloc_disk *malloc_disk = ctx;
612 : struct spdk_memory_domain *domain;
613 0 : int num_domains = 0;
614 :
615 0 : if (malloc_disk->disk.dif_type != SPDK_DIF_DISABLE) {
616 0 : return 0;
617 : }
618 :
619 : /* Report support for every memory domain */
620 0 : for (domain = spdk_memory_domain_get_first(NULL); domain != NULL;
621 0 : domain = spdk_memory_domain_get_next(domain, NULL)) {
622 0 : if (domains != NULL && num_domains < array_size) {
623 0 : domains[num_domains] = domain;
624 : }
625 0 : num_domains++;
626 : }
627 :
628 0 : return num_domains;
629 : }
630 :
631 : static bool
632 0 : bdev_malloc_accel_sequence_supported(void *ctx, enum spdk_bdev_io_type type)
633 : {
634 0 : struct malloc_disk *malloc_disk = ctx;
635 :
636 0 : if (malloc_disk->disk.dif_type != SPDK_DIF_DISABLE) {
637 0 : return false;
638 : }
639 :
640 0 : switch (type) {
641 0 : case SPDK_BDEV_IO_TYPE_READ:
642 : case SPDK_BDEV_IO_TYPE_WRITE:
643 0 : return true;
644 0 : default:
645 0 : return false;
646 : }
647 : }
648 :
649 : static const struct spdk_bdev_fn_table malloc_fn_table = {
650 : .destruct = bdev_malloc_destruct,
651 : .submit_request = bdev_malloc_submit_request,
652 : .io_type_supported = bdev_malloc_io_type_supported,
653 : .get_io_channel = bdev_malloc_get_io_channel,
654 : .write_config_json = bdev_malloc_write_json_config,
655 : .get_memory_domains = bdev_malloc_get_memory_domains,
656 : .accel_sequence_supported = bdev_malloc_accel_sequence_supported,
657 : };
658 :
659 : static int
660 0 : malloc_disk_setup_pi(struct malloc_disk *mdisk)
661 : {
662 0 : struct spdk_bdev *bdev = &mdisk->disk;
663 0 : struct spdk_dif_ctx dif_ctx;
664 0 : struct iovec iov, md_iov;
665 : int rc;
666 0 : struct spdk_dif_ctx_init_ext_opts dif_opts;
667 :
668 0 : dif_opts.size = SPDK_SIZEOF(&dif_opts, dif_pi_format);
669 0 : dif_opts.dif_pi_format = bdev->dif_pi_format;
670 : /* Set APPTAG|REFTAG_IGNORE to PI fields after creation of malloc bdev */
671 0 : rc = spdk_dif_ctx_init(&dif_ctx,
672 : bdev->blocklen,
673 : bdev->md_len,
674 0 : bdev->md_interleave,
675 0 : bdev->dif_is_head_of_md,
676 : bdev->dif_type,
677 : bdev->dif_check_flags,
678 : SPDK_DIF_REFTAG_IGNORE,
679 : 0xFFFF, SPDK_DIF_APPTAG_IGNORE,
680 : 0, 0, &dif_opts);
681 0 : if (rc != 0) {
682 0 : SPDK_ERRLOG("Initialization of DIF/DIX context failed\n");
683 0 : return rc;
684 : }
685 :
686 0 : iov.iov_base = mdisk->malloc_buf;
687 0 : iov.iov_len = bdev->blockcnt * bdev->blocklen;
688 :
689 0 : if (mdisk->disk.md_interleave) {
690 0 : rc = spdk_dif_generate(&iov, 1, bdev->blockcnt, &dif_ctx);
691 : } else {
692 0 : md_iov.iov_base = mdisk->malloc_md_buf;
693 0 : md_iov.iov_len = bdev->blockcnt * bdev->md_len;
694 :
695 0 : rc = spdk_dix_generate(&iov, 1, &md_iov, bdev->blockcnt, &dif_ctx);
696 : }
697 :
698 0 : if (rc != 0) {
699 0 : SPDK_ERRLOG("Formatting by DIF/DIX failed\n");
700 : }
701 :
702 0 : return rc;
703 : }
704 :
705 : int
706 0 : create_malloc_disk(struct spdk_bdev **bdev, const struct malloc_bdev_opts *opts)
707 : {
708 : struct malloc_disk *mdisk;
709 : uint32_t block_size;
710 : int rc;
711 :
712 0 : assert(opts != NULL);
713 :
714 0 : if (opts->num_blocks == 0) {
715 0 : SPDK_ERRLOG("Disk num_blocks must be greater than 0");
716 0 : return -EINVAL;
717 : }
718 :
719 0 : if (opts->block_size % 512) {
720 0 : SPDK_ERRLOG("Data block size must be 512 bytes aligned\n");
721 0 : return -EINVAL;
722 : }
723 :
724 0 : if (opts->physical_block_size % 512) {
725 0 : SPDK_ERRLOG("Physical block must be 512 bytes aligned\n");
726 0 : return -EINVAL;
727 : }
728 :
729 0 : switch (opts->md_size) {
730 0 : case 0:
731 : case 8:
732 : case 16:
733 : case 32:
734 : case 64:
735 : case 128:
736 0 : break;
737 0 : default:
738 0 : SPDK_ERRLOG("metadata size %u is not supported\n", opts->md_size);
739 0 : return -EINVAL;
740 : }
741 :
742 0 : if (opts->md_interleave) {
743 0 : block_size = opts->block_size + opts->md_size;
744 : } else {
745 0 : block_size = opts->block_size;
746 : }
747 :
748 0 : mdisk = calloc(1, sizeof(*mdisk));
749 0 : if (!mdisk) {
750 0 : SPDK_ERRLOG("mdisk calloc() failed\n");
751 0 : return -ENOMEM;
752 : }
753 :
754 : /*
755 : * Allocate the large backend memory buffer from pinned memory.
756 : *
757 : * TODO: need to pass a hint so we know which socket to allocate
758 : * from on multi-socket systems.
759 : */
760 0 : mdisk->malloc_buf = spdk_zmalloc(opts->num_blocks * block_size, 2 * 1024 * 1024, NULL,
761 : SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
762 0 : if (!mdisk->malloc_buf) {
763 0 : SPDK_ERRLOG("malloc_buf spdk_zmalloc() failed\n");
764 0 : malloc_disk_free(mdisk);
765 0 : return -ENOMEM;
766 : }
767 :
768 0 : if (!opts->md_interleave && opts->md_size != 0) {
769 0 : mdisk->malloc_md_buf = spdk_zmalloc(opts->num_blocks * opts->md_size, 2 * 1024 * 1024, NULL,
770 : SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
771 0 : if (!mdisk->malloc_md_buf) {
772 0 : SPDK_ERRLOG("malloc_md_buf spdk_zmalloc() failed\n");
773 0 : malloc_disk_free(mdisk);
774 0 : return -ENOMEM;
775 : }
776 : }
777 :
778 0 : if (opts->name) {
779 0 : mdisk->disk.name = strdup(opts->name);
780 : } else {
781 : /* Auto-generate a name */
782 0 : mdisk->disk.name = spdk_sprintf_alloc("Malloc%d", malloc_disk_count);
783 0 : malloc_disk_count++;
784 : }
785 0 : if (!mdisk->disk.name) {
786 0 : malloc_disk_free(mdisk);
787 0 : return -ENOMEM;
788 : }
789 0 : mdisk->disk.product_name = "Malloc disk";
790 :
791 0 : mdisk->disk.write_cache = 1;
792 0 : mdisk->disk.blocklen = block_size;
793 0 : mdisk->disk.phys_blocklen = opts->physical_block_size;
794 0 : mdisk->disk.blockcnt = opts->num_blocks;
795 0 : mdisk->disk.md_len = opts->md_size;
796 0 : mdisk->disk.md_interleave = opts->md_interleave;
797 0 : mdisk->disk.dif_type = opts->dif_type;
798 0 : mdisk->disk.dif_is_head_of_md = opts->dif_is_head_of_md;
799 : /* Current block device layer API does not propagate
800 : * any DIF related information from user. So, we can
801 : * not generate or verify Application Tag.
802 : */
803 0 : switch (opts->dif_type) {
804 0 : case SPDK_DIF_TYPE1:
805 : case SPDK_DIF_TYPE2:
806 0 : mdisk->disk.dif_check_flags = SPDK_DIF_FLAGS_GUARD_CHECK |
807 : SPDK_DIF_FLAGS_REFTAG_CHECK;
808 0 : break;
809 0 : case SPDK_DIF_TYPE3:
810 0 : mdisk->disk.dif_check_flags = SPDK_DIF_FLAGS_GUARD_CHECK;
811 0 : break;
812 0 : case SPDK_DIF_DISABLE:
813 0 : break;
814 : }
815 0 : mdisk->disk.dif_pi_format = opts->dif_pi_format;
816 :
817 0 : if (opts->dif_type != SPDK_DIF_DISABLE) {
818 0 : rc = malloc_disk_setup_pi(mdisk);
819 0 : if (rc) {
820 0 : SPDK_ERRLOG("Failed to set up protection information.\n");
821 0 : malloc_disk_free(mdisk);
822 0 : return rc;
823 : }
824 : }
825 :
826 0 : if (opts->optimal_io_boundary) {
827 0 : mdisk->disk.optimal_io_boundary = opts->optimal_io_boundary;
828 0 : mdisk->disk.split_on_optimal_io_boundary = true;
829 : }
830 0 : if (!spdk_uuid_is_null(&opts->uuid)) {
831 0 : spdk_uuid_copy(&mdisk->disk.uuid, &opts->uuid);
832 : }
833 :
834 0 : mdisk->disk.max_copy = 0;
835 0 : mdisk->disk.ctxt = mdisk;
836 0 : mdisk->disk.fn_table = &malloc_fn_table;
837 0 : mdisk->disk.module = &malloc_if;
838 :
839 0 : rc = spdk_bdev_register(&mdisk->disk);
840 0 : if (rc) {
841 0 : malloc_disk_free(mdisk);
842 0 : return rc;
843 : }
844 :
845 0 : *bdev = &(mdisk->disk);
846 :
847 0 : TAILQ_INSERT_TAIL(&g_malloc_disks, mdisk, link);
848 :
849 0 : return rc;
850 : }
851 :
852 : void
853 0 : delete_malloc_disk(const char *name, spdk_delete_malloc_complete cb_fn, void *cb_arg)
854 : {
855 : int rc;
856 :
857 0 : rc = spdk_bdev_unregister_by_name(name, &malloc_if, cb_fn, cb_arg);
858 0 : if (rc != 0) {
859 0 : cb_fn(cb_arg, rc);
860 : }
861 0 : }
862 :
863 : static int
864 0 : malloc_completion_poller(void *ctx)
865 : {
866 0 : struct malloc_channel *ch = ctx;
867 : struct malloc_task *task;
868 0 : TAILQ_HEAD(, malloc_task) completed_tasks;
869 0 : uint32_t num_completions = 0;
870 :
871 0 : TAILQ_INIT(&completed_tasks);
872 0 : TAILQ_SWAP(&completed_tasks, &ch->completed_tasks, malloc_task, tailq);
873 :
874 0 : while (!TAILQ_EMPTY(&completed_tasks)) {
875 0 : task = TAILQ_FIRST(&completed_tasks);
876 0 : TAILQ_REMOVE(&completed_tasks, task, tailq);
877 0 : spdk_bdev_io_complete(spdk_bdev_io_from_ctx(task), task->status);
878 0 : num_completions++;
879 : }
880 :
881 0 : return num_completions > 0 ? SPDK_POLLER_BUSY : SPDK_POLLER_IDLE;
882 : }
883 :
884 : static int
885 0 : malloc_create_channel_cb(void *io_device, void *ctx)
886 : {
887 0 : struct malloc_channel *ch = ctx;
888 :
889 0 : ch->accel_channel = spdk_accel_get_io_channel();
890 0 : if (!ch->accel_channel) {
891 0 : SPDK_ERRLOG("Failed to get accel framework's IO channel\n");
892 0 : return -ENOMEM;
893 : }
894 :
895 0 : ch->completion_poller = SPDK_POLLER_REGISTER(malloc_completion_poller, ch, 0);
896 0 : if (!ch->completion_poller) {
897 0 : SPDK_ERRLOG("Failed to register malloc completion poller\n");
898 0 : spdk_put_io_channel(ch->accel_channel);
899 0 : return -ENOMEM;
900 : }
901 :
902 0 : TAILQ_INIT(&ch->completed_tasks);
903 :
904 0 : return 0;
905 : }
906 :
907 : static void
908 0 : malloc_destroy_channel_cb(void *io_device, void *ctx)
909 : {
910 0 : struct malloc_channel *ch = ctx;
911 :
912 0 : assert(TAILQ_EMPTY(&ch->completed_tasks));
913 :
914 0 : spdk_put_io_channel(ch->accel_channel);
915 0 : spdk_poller_unregister(&ch->completion_poller);
916 0 : }
917 :
918 : static int
919 0 : bdev_malloc_initialize(void)
920 : {
921 : /* This needs to be reset for each reinitialization of submodules.
922 : * Otherwise after enough devices or reinitializations the value gets too high.
923 : * TODO: Make malloc bdev name mandatory and remove this counter. */
924 0 : malloc_disk_count = 0;
925 :
926 0 : spdk_io_device_register(&g_malloc_disks, malloc_create_channel_cb,
927 : malloc_destroy_channel_cb, sizeof(struct malloc_channel),
928 : "bdev_malloc");
929 :
930 0 : return 0;
931 : }
932 :
933 : static void
934 0 : bdev_malloc_deinitialize(void)
935 : {
936 0 : spdk_io_device_unregister(&g_malloc_disks, NULL);
937 0 : }
938 :
939 0 : SPDK_LOG_REGISTER_COMPONENT(bdev_malloc)
|