diff --git a/lib/nvmf/transport.c b/lib/nvmf/transport.c index 67325a3b9..e9971db8e 100644 --- a/lib/nvmf/transport.c +++ b/lib/nvmf/transport.c @@ -551,37 +551,40 @@ nvmf_transport_poll_group_create(struct spdk_nvmf_transport *transport, STAILQ_INIT(&tgroup->pending_buf_queue); STAILQ_INIT(&tgroup->buf_cache); - if (transport->opts.buf_cache_size) { - tgroup->buf_cache_size = transport->opts.buf_cache_size; - bufs = calloc(tgroup->buf_cache_size, sizeof(struct spdk_nvmf_transport_pg_cache_buf *)); - - if (!bufs) { - SPDK_ERRLOG("Memory allocation failed, can't reserve buffers for the pg buffer cache\n"); - return tgroup; - } - - if (spdk_mempool_get_bulk(transport->data_buf_pool, (void **)bufs, tgroup->buf_cache_size)) { - tgroup->buf_cache_size = (uint32_t)spdk_mempool_count(transport->data_buf_pool); - SPDK_NOTICELOG("Unable to reserve the full number of buffers for the pg buffer cache. " - "Decrease the number of cached buffers from %u to %u\n", - transport->opts.buf_cache_size, tgroup->buf_cache_size); - /* Sanity check */ - assert(tgroup->buf_cache_size <= transport->opts.buf_cache_size); - /* Try again with less number of buffers */ - if (spdk_mempool_get_bulk(transport->data_buf_pool, (void **)bufs, tgroup->buf_cache_size)) { - SPDK_NOTICELOG("Failed to reserve %u buffers\n", tgroup->buf_cache_size); - tgroup->buf_cache_size = 0; - } - } - - for (i = 0; i < tgroup->buf_cache_size; i++) { - STAILQ_INSERT_HEAD(&tgroup->buf_cache, bufs[i], link); - } - tgroup->buf_cache_count = tgroup->buf_cache_size; - - free(bufs); + if (transport->opts.buf_cache_size == 0) { + /* We aren't going to allocate any buffers for the cache, so just return now. */ + return tgroup; } + tgroup->buf_cache_size = transport->opts.buf_cache_size; + bufs = calloc(tgroup->buf_cache_size, sizeof(struct spdk_nvmf_transport_pg_cache_buf *)); + + if (!bufs) { + SPDK_ERRLOG("Memory allocation failed, can't reserve buffers for the pg buffer cache\n"); + return tgroup; + } + + if (spdk_mempool_get_bulk(transport->data_buf_pool, (void **)bufs, tgroup->buf_cache_size)) { + tgroup->buf_cache_size = (uint32_t)spdk_mempool_count(transport->data_buf_pool); + SPDK_NOTICELOG("Unable to reserve the full number of buffers for the pg buffer cache. " + "Decrease the number of cached buffers from %u to %u\n", + transport->opts.buf_cache_size, tgroup->buf_cache_size); + /* Sanity check */ + assert(tgroup->buf_cache_size <= transport->opts.buf_cache_size); + /* Try again with less number of buffers */ + if (spdk_mempool_get_bulk(transport->data_buf_pool, (void **)bufs, tgroup->buf_cache_size)) { + SPDK_NOTICELOG("Failed to reserve %u buffers\n", tgroup->buf_cache_size); + tgroup->buf_cache_size = 0; + } + } + + for (i = 0; i < tgroup->buf_cache_size; i++) { + STAILQ_INSERT_HEAD(&tgroup->buf_cache, bufs[i], link); + } + tgroup->buf_cache_count = tgroup->buf_cache_size; + + free(bufs); + return tgroup; }