Skip to content

Commit 0e87479

Browse files
committed
mio: Initialize mio mpool only for non-volatile caches
The mpool is allocated on creation of the first non-volatile cache, and deallocated on destruction of the last non-volatile cache. Signed-off-by: Robert Baldyga <robert.baldyga@unvertical.com>
1 parent a16f52e commit 0e87479

5 files changed

Lines changed: 88 additions & 20 deletions

File tree

src/metadata/metadata_io.c

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
* Copyright(c) 2012-2022 Intel Corporation
33
* Copyright(c) 2024 Huawei Technologies
4+
* Copyright(c) 2026 Unvertical
45
* SPDX-License-Identifier: BSD-3-Clause
56
*/
67
#include "metadata.h"
@@ -230,7 +231,7 @@ static void metadata_io_req_finalize(struct metadata_io_request *m_req)
230231
struct metadata_io_request_asynch *a_req = m_req->asynch;
231232

232233
if (env_atomic_dec_return(&a_req->req_active) == 0)
233-
env_mpool_del(m_req->cache->owner->resources.mio, a_req,
234+
env_mpool_del(m_req->cache->owner->resources.mio.mpool, a_req,
234235
a_req->alloc_req_count);
235236
}
236237

@@ -388,7 +389,7 @@ static int metadata_io_i_asynch(ocf_cache_t cache, ocf_queue_t queue, int dir,
388389
uint32_t io_count = OCF_DIV_ROUND_UP(count, max_count);
389390
uint32_t req_count = OCF_MIN(io_count, METADATA_IO_REQS_LIMIT);
390391
int i;
391-
struct env_mpool *mio_allocator = cache->owner->resources.mio;
392+
struct env_mpool *mio_allocator = cache->owner->resources.mio.mpool;
392393

393394
if (count == 0)
394395
return 0;
@@ -483,29 +484,49 @@ int metadata_io_read_i_asynch(ocf_cache_t cache, ocf_queue_t queue,
483484
not exceed one page (4096B).
484485
Change if apropriate. */
485486

486-
int ocf_metadata_io_ctx_init(struct ocf_ctx *ocf_ctx)
487+
int ocf_metadata_io_open(struct ocf_ctx *ocf_ctx)
487488
{
488489
uint32_t limits[] = {
489490
[0 ... MIO_RPOOL_THRESHOLD - 1] = -1,
490491
[MIO_RPOOL_THRESHOLD ... ocf_mio_size_max - 1] = MIO_RPOOL_LIMIT,
491492
[ocf_mio_size_max ... env_mpool_max] = -1,
492493
};
493494

494-
ocf_ctx->resources.mio = env_mpool_create(
495+
if (ocf_ctx->resources.mio.ref_count > 0)
496+
goto out;
497+
498+
ocf_ctx->resources.mio.mpool = env_mpool_create(
495499
sizeof(struct metadata_io_request_asynch),
496500
sizeof(struct metadata_io_request),
497501
ENV_MEM_NOIO, ocf_mio_size_max - 1, true,
498502
limits,
499503
"ocf_mio",
500504
true);
501-
if (ocf_ctx->resources.mio == NULL)
502-
return -1;
505+
if (ocf_ctx->resources.mio.mpool == NULL)
506+
return -OCF_ERR_NO_MEM;
507+
508+
out:
509+
ocf_ctx->resources.mio.ref_count++;
510+
return 0;
511+
}
512+
513+
void ocf_metadata_io_close(struct ocf_ctx *ocf_ctx)
514+
{
515+
if (--ocf_ctx->resources.mio.ref_count == 0) {
516+
env_mpool_destroy(ocf_ctx->resources.mio.mpool);
517+
ocf_ctx->resources.mio.mpool = NULL;
518+
}
519+
}
520+
521+
int ocf_metadata_io_ctx_init(struct ocf_ctx *ocf_ctx)
522+
{
523+
ocf_ctx->resources.mio.mpool = NULL;
524+
ocf_ctx->resources.mio.ref_count = 0;
503525

504526
return 0;
505527
}
506528

507529
void ocf_metadata_io_ctx_deinit(struct ocf_ctx *ocf_ctx)
508530
{
509-
env_mpool_destroy(ocf_ctx->resources.mio);
510-
ocf_ctx->resources.mio = NULL;
531+
ENV_BUG_ON(ocf_ctx->resources.mio.ref_count != 0);
511532
}

src/metadata/metadata_io.h

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
/*
22
* Copyright(c) 2012-2021 Intel Corporation
33
* Copyright(c) 2024 Huawei Technologies
4+
* Copyright(c) 2026 Unvertical
45
* SPDX-License-Identifier: BSD-3-Clause
56
*/
67

78
#ifndef __METADATA_IO_H__
89
#define __METADATA_IO_H__
910

1011
#include "../concurrency/ocf_mio_concurrency.h"
12+
#include "metadata_io_resource.h"
1113

1214
/**
1315
* @file metadata_io.h
@@ -146,14 +148,4 @@ int metadata_io_read_i_asynch(ocf_cache_t cache, ocf_queue_t queue,
146148
ocf_metadata_io_event_t drain_hndl,
147149
ocf_metadata_io_end_t compl_hndl);
148150

149-
/**
150-
* Initialize ocf_ctx related structures of metadata_io (mpool).
151-
*/
152-
int ocf_metadata_io_ctx_init(struct ocf_ctx *ocf_ctx);
153-
154-
/**
155-
* Deinitialize ocf_ctx related structures of metadata_io
156-
*/
157-
void ocf_metadata_io_ctx_deinit(struct ocf_ctx *ocf_ctx);
158-
159-
#endif /* METADATA_IO_UTILS_H_ */
151+
#endif /* __METADATA_IO_H__ */
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright(c) 2012-2021 Intel Corporation
3+
* Copyright(c) 2026 Unvertical
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
#ifndef __METADATA_IO_RESOURCE_H__
8+
#define __METADATA_IO_RESOURCE_H__
9+
10+
/*
11+
* ocf_ctx resource
12+
*/
13+
struct ocf_metadata_io_resource {
14+
struct env_mpool *mpool;
15+
int ref_count;
16+
};
17+
18+
/**
19+
* Initialize ocf_ctx related structures of metadata_io.
20+
*/
21+
int ocf_metadata_io_ctx_init(struct ocf_ctx *ocf_ctx);
22+
23+
/**
24+
* Deinitialize ocf_ctx related structures of metadata_io
25+
*/
26+
void ocf_metadata_io_ctx_deinit(struct ocf_ctx *ocf_ctx);
27+
28+
/**
29+
* Initialize per-ocf_ctx mio resources
30+
*
31+
* Can be safely called multiple times - refcount based.
32+
*/
33+
int ocf_metadata_io_open(struct ocf_ctx *ocf_ctx);
34+
35+
/**
36+
* Decrements the refcount of per-ocf_ctx mio resources
37+
*
38+
* Needs to be called as many times as ocf_metadata_io_open().
39+
*/
40+
void ocf_metadata_io_close(struct ocf_ctx *ocf_ctx);
41+
42+
#endif /* __METADATA_IO_RESOURCE_H__ */

src/mngt/ocf_mngt_cache.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,6 +1631,14 @@ static int _ocf_mngt_cache_start(ocf_ctx_t ctx, ocf_cache_t *cache,
16311631
goto _cache_mngt_init_instance_ERROR;
16321632
}
16331633

1634+
if (!cfg->metadata_volatile) {
1635+
result = ocf_metadata_io_open(ctx);
1636+
if (result) {
1637+
env_rmutex_unlock(&ctx->lock);
1638+
goto _cache_mngt_init_instance_ERROR;
1639+
}
1640+
}
1641+
16341642
list_add_tail(&tmp_cache->list, &ctx->caches);
16351643
env_rmutex_unlock(&ctx->lock);
16361644

@@ -2335,6 +2343,9 @@ static void _ocf_mngt_cache_dealloc(void *priv)
23352343
ctx = cache->owner;
23362344
ocf_metadata_deinit(cache);
23372345

2346+
if (!cache->metadata.is_volatile)
2347+
ocf_metadata_io_close(ctx);
2348+
23382349
env_refcnt_deinit(&cache->refcnt.cache);
23392350
env_refcnt_deinit(&cache->refcnt.dirty);
23402351
env_refcnt_deinit(&cache->refcnt.metadata);

src/ocf_ctx_priv.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright(c) 2012-2022 Intel Corporation
3+
* Copyright(c) 2026 Unvertical
34
* SPDX-License-Identifier: BSD-3-Clause
45
*/
56

@@ -11,6 +12,7 @@
1112
#include "ocf/ocf_composite_volume.h"
1213
#include "ocf_logger_priv.h"
1314
#include "ocf_volume_priv.h"
15+
#include "metadata/metadata_io_resource.h"
1416

1517
#define OCF_VOLUME_TYPE_CNT_USER 8
1618
#define OCF_VOLUME_TYPE_CNT_PRIV 3
@@ -38,7 +40,7 @@ struct ocf_ctx {
3840
const struct ocf_ctx_ops *ops;
3941
struct {
4042
struct env_mpool *req;
41-
struct env_mpool *mio;
43+
struct ocf_metadata_io_resource mio;
4244
} resources;
4345
struct list_head caches;
4446
struct {

0 commit comments

Comments
 (0)