Skip to content

Commit

Permalink
nvdimm: Avoid wasting some memory.
Browse files Browse the repository at this point in the history
sizeof(struct btt_sb) is 4096.

When using devm_kzalloc(), there is a small memory overhead and, on most
systems, this leads to 40 bytes of extra memory allocation.
So 5036 bytes are expected to be allocated.

The memory allocator works with fixed size hunks of memory. In this case,
it will require 8192 bytes of memory because more than 4096 bytes are
required.

In order to avoid wasting 4ko of memory, just use kzalloc() and add a
devm action to free it when needed.

Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
  • Loading branch information
tititiou36 authored and intel-lab-lkp committed Sep 4, 2022
1 parent b90cb10 commit af94e70
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion drivers/nvdimm/btt_devs.c
Expand Up @@ -332,6 +332,11 @@ static int __nd_btt_probe(struct nd_btt *nd_btt,
return 0;
}

void nd_btt_free(void *data)
{
kfree(data);
}

int nd_btt_probe(struct device *dev, struct nd_namespace_common *ndns)
{
int rc;
Expand All @@ -356,7 +361,17 @@ int nd_btt_probe(struct device *dev, struct nd_namespace_common *ndns)
nvdimm_bus_unlock(&ndns->dev);
if (!btt_dev)
return -ENOMEM;
btt_sb = devm_kzalloc(dev, sizeof(*btt_sb), GFP_KERNEL);

/*
* 'struct btt_sb' is 4096. Using devm_kzalloc() would waste 4 ko of
* memory because, because of a small memory over head, 8192 bytes
* would be allocated. So keep this kzalloc()+devm_add_action_or_reset()
*/
btt_sb = kzalloc(sizeof(*btt_sb), GFP_KERNEL);
rc = devm_add_action_or_reset(dev, nd_btt_free, btt_sb);
if (rc)
return rc;

rc = __nd_btt_probe(to_nd_btt(btt_dev), ndns, btt_sb);
dev_dbg(dev, "btt: %s\n", rc == 0 ? dev_name(btt_dev) : "<none>");
if (rc < 0) {
Expand Down

0 comments on commit af94e70

Please sign in to comment.