mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Dan Williams <dan.j.williams@intel.com>
To: Christophe JAILLET <christophe.jaillet@wanadoo.fr>,
	Vishal Verma <vishal.l.verma@intel.com>,
	Dan Williams <dan.j.williams@intel.com>,
	"Dave Jiang" <dave.jiang@intel.com>,
	Ira Weiny <ira.weiny@intel.com>
Cc: <linux-kernel@vger.kernel.org>, <kernel-janitors@vger.kernel.org>,
	Christophe JAILLET <christophe.jaillet@wanadoo.fr>,
	<nvdimm@lists.linux.dev>
Subject: RE: [PATCH] nvdimm: Avoid wasting some memory.
Date: Sun, 4 Sep 2022 07:38:17 -0700	[thread overview]
Message-ID: <6314b859df5e2_2202c6294f5@dwillia2-xfh.jf.intel.com.notmuch> (raw)
In-Reply-To: <8355cb2b720f8cd0f1315b06d70b541ba38add30.1662299370.git.christophe.jaillet@wanadoo.fr>

Christophe JAILLET wrote:
> 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>
> ---
>  drivers/nvdimm/btt_devs.c | 17 ++++++++++++++++-
>  1 file changed, 16 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/nvdimm/btt_devs.c b/drivers/nvdimm/btt_devs.c
> index fabbb31f2c35..7b79fb0b0338 100644
> --- a/drivers/nvdimm/btt_devs.c
> +++ b/drivers/nvdimm/btt_devs.c
> @@ -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;
> @@ -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;

Thanks for the analysis and the patch. However, shouldn't this be
something that is addressed internal to devm_kzalloc() rather than
open-coded at every potential call site?

  reply	other threads:[~2022-09-04 14:38 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-04 13:49 Christophe JAILLET
2022-09-04 14:38 ` Dan Williams [this message]
2022-09-04 16:05   ` Christophe JAILLET
2022-09-04 16:17 ` kernel test robot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6314b859df5e2_2202c6294f5@dwillia2-xfh.jf.intel.com.notmuch \
    --to=dan.j.williams@intel.com \
    --cc=christophe.jaillet@wanadoo.fr \
    --cc=dave.jiang@intel.com \
    --cc=ira.weiny@intel.com \
    --cc=kernel-janitors@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nvdimm@lists.linux.dev \
    --cc=vishal.l.verma@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®