Skip to content

Commit 4e781b4

Browse files
jthornbersnitm
authored andcommitted
dm cache: speed up writing of the hint array
It's far quicker to always delete the hint array and recreate with dm_array_new() because we avoid the copying caused by mutation. Also simplifies the policy interface, replacing the walk_hints() with the simpler get_hint(). Signed-off-by: Joe Thornber <[email protected]> Signed-off-by: Mike Snitzer <[email protected]>
1 parent dd6a77d commit 4e781b4

File tree

5 files changed

+42
-94
lines changed

5 files changed

+42
-94
lines changed

drivers/md/dm-cache-metadata.c

Lines changed: 27 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,10 +1368,24 @@ int dm_cache_get_metadata_dev_size(struct dm_cache_metadata *cmd,
13681368

13691369
/*----------------------------------------------------------------*/
13701370

1371-
static int begin_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *policy)
1371+
static int get_hint(uint32_t index, void *value_le, void *context)
1372+
{
1373+
uint32_t value;
1374+
struct dm_cache_policy *policy = context;
1375+
1376+
value = policy_get_hint(policy, to_cblock(index));
1377+
*((__le32 *) value_le) = cpu_to_le32(value);
1378+
1379+
return 0;
1380+
}
1381+
1382+
/*
1383+
* It's quicker to always delete the hint array, and recreate with
1384+
* dm_array_new().
1385+
*/
1386+
static int write_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *policy)
13721387
{
13731388
int r;
1374-
__le32 value;
13751389
size_t hint_size;
13761390
const char *policy_name = dm_cache_policy_get_name(policy);
13771391
const unsigned *policy_version = dm_cache_policy_get_version(policy);
@@ -1380,63 +1394,23 @@ static int begin_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *po
13801394
(strlen(policy_name) > sizeof(cmd->policy_name) - 1))
13811395
return -EINVAL;
13821396

1383-
if (!policy_unchanged(cmd, policy)) {
1384-
strncpy(cmd->policy_name, policy_name, sizeof(cmd->policy_name));
1385-
memcpy(cmd->policy_version, policy_version, sizeof(cmd->policy_version));
1397+
strncpy(cmd->policy_name, policy_name, sizeof(cmd->policy_name));
1398+
memcpy(cmd->policy_version, policy_version, sizeof(cmd->policy_version));
13861399

1387-
hint_size = dm_cache_policy_get_hint_size(policy);
1388-
if (!hint_size)
1389-
return 0; /* short-circuit hints initialization */
1390-
cmd->policy_hint_size = hint_size;
1400+
hint_size = dm_cache_policy_get_hint_size(policy);
1401+
if (!hint_size)
1402+
return 0; /* short-circuit hints initialization */
1403+
cmd->policy_hint_size = hint_size;
13911404

1392-
if (cmd->hint_root) {
1393-
r = dm_array_del(&cmd->hint_info, cmd->hint_root);
1394-
if (r)
1395-
return r;
1396-
}
1397-
1398-
r = dm_array_empty(&cmd->hint_info, &cmd->hint_root);
1405+
if (cmd->hint_root) {
1406+
r = dm_array_del(&cmd->hint_info, cmd->hint_root);
13991407
if (r)
14001408
return r;
1401-
1402-
value = cpu_to_le32(0);
1403-
__dm_bless_for_disk(&value);
1404-
r = dm_array_resize(&cmd->hint_info, cmd->hint_root, 0,
1405-
from_cblock(cmd->cache_blocks),
1406-
&value, &cmd->hint_root);
1407-
if (r)
1408-
return r;
1409-
}
1410-
1411-
return 0;
1412-
}
1413-
1414-
static int save_hint(void *context, dm_cblock_t cblock, dm_oblock_t oblock, uint32_t hint)
1415-
{
1416-
struct dm_cache_metadata *cmd = context;
1417-
__le32 value = cpu_to_le32(hint);
1418-
int r;
1419-
1420-
__dm_bless_for_disk(&value);
1421-
1422-
r = dm_array_set_value(&cmd->hint_info, cmd->hint_root,
1423-
from_cblock(cblock), &value, &cmd->hint_root);
1424-
cmd->changed = true;
1425-
1426-
return r;
1427-
}
1428-
1429-
static int write_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *policy)
1430-
{
1431-
int r;
1432-
1433-
r = begin_hints(cmd, policy);
1434-
if (r) {
1435-
DMERR("begin_hints failed");
1436-
return r;
14371409
}
14381410

1439-
return policy_walk_mappings(policy, save_hint, cmd);
1411+
return dm_array_new(&cmd->hint_info, &cmd->hint_root,
1412+
from_cblock(cmd->cache_blocks),
1413+
get_hint, policy);
14401414
}
14411415

14421416
int dm_cache_write_hints(struct dm_cache_metadata *cmd, struct dm_cache_policy *policy)

drivers/md/dm-cache-policy-cleaner.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ static void init_policy_functions(struct policy *p)
395395
p->policy.set_dirty = wb_set_dirty;
396396
p->policy.clear_dirty = wb_clear_dirty;
397397
p->policy.load_mapping = wb_load_mapping;
398-
p->policy.walk_mappings = NULL;
398+
p->policy.get_hint = NULL;
399399
p->policy.remove_mapping = wb_remove_mapping;
400400
p->policy.writeback_work = wb_writeback_work;
401401
p->policy.force_mapping = wb_force_mapping;

drivers/md/dm-cache-policy-internal.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ static inline int policy_load_mapping(struct dm_cache_policy *p,
4848
return p->load_mapping(p, oblock, cblock, hint, hint_valid);
4949
}
5050

51-
static inline int policy_walk_mappings(struct dm_cache_policy *p,
52-
policy_walk_fn fn, void *context)
51+
static inline uint32_t policy_get_hint(struct dm_cache_policy *p,
52+
dm_cblock_t cblock)
5353
{
54-
return p->walk_mappings ? p->walk_mappings(p, fn, context) : 0;
54+
return p->get_hint ? p->get_hint(p, cblock) : 0;
5555
}
5656

5757
static inline int policy_writeback_work(struct dm_cache_policy *p,

drivers/md/dm-cache-policy-smq.c

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,41 +1375,15 @@ static int smq_load_mapping(struct dm_cache_policy *p,
13751375
return 0;
13761376
}
13771377

1378-
static int smq_save_hints(struct smq_policy *mq, struct queue *q,
1379-
policy_walk_fn fn, void *context)
1380-
{
1381-
int r;
1382-
unsigned level;
1383-
struct entry *e;
1384-
1385-
for (level = 0; level < q->nr_levels; level++)
1386-
for (e = l_head(q->es, q->qs + level); e; e = l_next(q->es, e)) {
1387-
if (!e->sentinel) {
1388-
r = fn(context, infer_cblock(mq, e),
1389-
e->oblock, e->level);
1390-
if (r)
1391-
return r;
1392-
}
1393-
}
1394-
1395-
return 0;
1396-
}
1397-
1398-
static int smq_walk_mappings(struct dm_cache_policy *p, policy_walk_fn fn,
1399-
void *context)
1378+
static uint32_t smq_get_hint(struct dm_cache_policy *p, dm_cblock_t cblock)
14001379
{
14011380
struct smq_policy *mq = to_smq_policy(p);
1402-
int r = 0;
1381+
struct entry *e = get_entry(&mq->cache_alloc, from_cblock(cblock));
14031382

1404-
/*
1405-
* We don't need to lock here since this method is only called once
1406-
* the IO has stopped.
1407-
*/
1408-
r = smq_save_hints(mq, &mq->clean, fn, context);
1409-
if (!r)
1410-
r = smq_save_hints(mq, &mq->dirty, fn, context);
1383+
if (!e->allocated)
1384+
return 0;
14111385

1412-
return r;
1386+
return e->level;
14131387
}
14141388

14151389
static void __remove_mapping(struct smq_policy *mq, dm_oblock_t oblock)
@@ -1616,7 +1590,7 @@ static void init_policy_functions(struct smq_policy *mq, bool mimic_mq)
16161590
mq->policy.set_dirty = smq_set_dirty;
16171591
mq->policy.clear_dirty = smq_clear_dirty;
16181592
mq->policy.load_mapping = smq_load_mapping;
1619-
mq->policy.walk_mappings = smq_walk_mappings;
1593+
mq->policy.get_hint = smq_get_hint;
16201594
mq->policy.remove_mapping = smq_remove_mapping;
16211595
mq->policy.remove_cblock = smq_remove_cblock;
16221596
mq->policy.writeback_work = smq_writeback_work;

drivers/md/dm-cache-policy.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,6 @@ struct policy_result {
9090
dm_cblock_t cblock; /* POLICY_HIT, POLICY_NEW, POLICY_REPLACE */
9191
};
9292

93-
typedef int (*policy_walk_fn)(void *context, dm_cblock_t cblock,
94-
dm_oblock_t oblock, uint32_t hint);
95-
9693
/*
9794
* The cache policy object. Just a bunch of methods. It is envisaged that
9895
* this structure will be embedded in a bigger, policy specific structure
@@ -158,8 +155,11 @@ struct dm_cache_policy {
158155
int (*load_mapping)(struct dm_cache_policy *p, dm_oblock_t oblock,
159156
dm_cblock_t cblock, uint32_t hint, bool hint_valid);
160157

161-
int (*walk_mappings)(struct dm_cache_policy *p, policy_walk_fn fn,
162-
void *context);
158+
/*
159+
* Gets the hint for a given cblock. Called in a single threaded
160+
* context. So no locking required.
161+
*/
162+
uint32_t (*get_hint)(struct dm_cache_policy *p, dm_cblock_t cblock);
163163

164164
/*
165165
* Override functions used on the error paths of the core target.

0 commit comments

Comments
 (0)