Skip to content

Commit 472fdec

Browse files
larsclausenbroonie
authored andcommitted
regmap: rbtree: Reduce number of nodes, take 2
Support for reducing the number of nodes and memory consumption of the rbtree cache by allowing for small unused holes in the node's register cache block was initially added in commit 0c7ed85 ("regmap: Cut down on the average # of nodes in the rbtree cache"). But the commit had problems and so its effect was reverted again in commit 4e67fb5 ("regmap: rbtree: Fix overlapping rbnodes."). This patch brings the feature back of reducing the average number of nodes, which will speedup node look-up, while at the same time also reducing the memory usage of the rbtree cache. This patch takes a slightly different approach than the original patch though. It modifies the adjacent node look-up to not only consider nodes that are just one to the left or the right of the register but any node that falls in a certain range around the register. The range is calculated based on how much memory it would take to allocate a new node compared to how much memory it takes adding a set of unused registers to an existing node. E.g. if a node takes up 24 bytes and each register in a block uses 1 byte the range will be from the register address - 24 to the register address + 24. If we find a node that falls within this range it is cheaper or as expensive to add the register to the existing node and have a couple of unused registers in the node's cache compared to allocating a new node. Signed-off-by: Lars-Peter Clausen <lars@metafoo.de> Signed-off-by: Mark Brown <broonie@linaro.org>
1 parent 194c753 commit 472fdec

File tree

1 file changed

+36
-17
lines changed

1 file changed

+36
-17
lines changed

drivers/base/regmap/regcache-rbtree.c

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -278,27 +278,34 @@ static int regcache_rbtree_read(struct regmap *map,
278278

279279
static int regcache_rbtree_insert_to_block(struct regmap *map,
280280
struct regcache_rbtree_node *rbnode,
281-
unsigned int pos, unsigned int reg,
281+
unsigned int base_reg,
282+
unsigned int top_reg,
283+
unsigned int reg,
282284
unsigned int value)
283285
{
286+
unsigned int blklen;
287+
unsigned int pos, offset;
284288
u8 *blk;
285289

290+
blklen = (top_reg - base_reg) / map->reg_stride + 1;
291+
pos = (reg - base_reg) / map->reg_stride;
292+
offset = (rbnode->base_reg - base_reg) / map->reg_stride;
293+
286294
blk = krealloc(rbnode->block,
287-
(rbnode->blklen + 1) * map->cache_word_size,
295+
blklen * map->cache_word_size,
288296
GFP_KERNEL);
289297
if (!blk)
290298
return -ENOMEM;
291299

292300
/* insert the register value in the correct place in the rbnode block */
293-
memmove(blk + (pos + 1) * map->cache_word_size,
294-
blk + pos * map->cache_word_size,
295-
(rbnode->blklen - pos) * map->cache_word_size);
301+
if (pos == 0)
302+
memmove(blk + offset * map->cache_word_size,
303+
blk, rbnode->blklen * map->cache_word_size);
296304

297305
/* update the rbnode block, its size and the base register */
298306
rbnode->block = blk;
299-
rbnode->blklen++;
300-
if (!pos)
301-
rbnode->base_reg = reg;
307+
rbnode->blklen = blklen;
308+
rbnode->base_reg = base_reg;
302309

303310
regcache_rbtree_set_register(map, rbnode, pos, value);
304311
return 0;
@@ -352,9 +359,7 @@ static int regcache_rbtree_write(struct regmap *map, unsigned int reg,
352359
struct regcache_rbtree_ctx *rbtree_ctx;
353360
struct regcache_rbtree_node *rbnode, *rbnode_tmp;
354361
struct rb_node *node;
355-
unsigned int base_reg, top_reg;
356362
unsigned int reg_tmp;
357-
unsigned int pos;
358363
int ret;
359364

360365
rbtree_ctx = map->cache;
@@ -371,6 +376,19 @@ static int regcache_rbtree_write(struct regmap *map, unsigned int reg,
371376
reg_tmp = (reg - rbnode->base_reg) / map->reg_stride;
372377
regcache_rbtree_set_register(map, rbnode, reg_tmp, value);
373378
} else {
379+
unsigned int base_reg, top_reg;
380+
unsigned int new_base_reg, new_top_reg;
381+
unsigned int min, max;
382+
unsigned int max_dist;
383+
384+
max_dist = map->reg_stride * sizeof(*rbnode_tmp) /
385+
map->cache_word_size;
386+
if (reg < max_dist)
387+
min = 0;
388+
else
389+
min = reg - max_dist;
390+
max = reg + max_dist;
391+
374392
/* look for an adjacent register to the one we are about to add */
375393
for (node = rb_first(&rbtree_ctx->root); node;
376394
node = rb_next(node)) {
@@ -380,16 +398,17 @@ static int regcache_rbtree_write(struct regmap *map, unsigned int reg,
380398
regcache_rbtree_get_base_top_reg(map, rbnode_tmp,
381399
&base_reg, &top_reg);
382400

383-
/* decide where in the block to place our register */
384-
if (base_reg > 0 && reg == base_reg - map->reg_stride)
385-
pos = 0;
386-
else if (reg > 0 && reg - map->reg_stride == top_reg)
387-
pos = rbnode_tmp->blklen;
388-
else
401+
if (base_reg <= max && top_reg >= min) {
402+
new_base_reg = min(reg, base_reg);
403+
new_top_reg = max(reg, top_reg);
404+
} else {
389405
continue;
406+
}
390407

391408
ret = regcache_rbtree_insert_to_block(map, rbnode_tmp,
392-
pos, reg, value);
409+
new_base_reg,
410+
new_top_reg, reg,
411+
value);
393412
if (ret)
394413
return ret;
395414
rbtree_ctx->cached_rbnode = rbnode_tmp;

0 commit comments

Comments
 (0)