Received: by 2002:a05:6358:bb9e:b0:b9:5105:a5b4 with SMTP id df30csp2166121rwb; Sun, 4 Sep 2022 09:40:05 -0700 (PDT) X-Google-Smtp-Source: AA6agR5eaW8laKOA79oYnlAC08lSeDGRSAmMUbtHaSpkVDm/SbRoJwfCDsPtKB8lGA+14wzXEBLD X-Received: by 2002:a17:90a:e7cc:b0:1fe:3c5c:e70 with SMTP id kb12-20020a17090ae7cc00b001fe3c5c0e70mr15392306pjb.195.1662309605225; Sun, 04 Sep 2022 09:40:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662309605; cv=none; d=google.com; s=arc-20160816; b=bx0nBKT3Vj2MN68KrBWqSycg8fvOsnS85Tl9JvmsMGaqa+5/oit6cdTqiGiY+mFykp G1HJLuQ/hmxnk1NPnLDusTXi3MqgPzQ2M2XJHIp3fRKKchMydGk6u/7cD2fdB357kvD8 8+AnuoH1Vla5vT6Y9/yt/N5L1vpzKXS5JZ/feY7NLPYS8SbVGKSNcnQIttWXEibYdMo9 JtbsFENPbs2/efOnawg697StwfhRsvIQH7zV70IiipVtPPowof6FCeoYHA2Ggcxh9RyO p6JhVKyTwPFuynG4hi83OquOCxCDJCwQimVhmf+xrjSPsw1gDjJVXBUU9CxkV8DdeK6P Kqkg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id; bh=KWcEgmH/QSDJsB8Ko78QF4q2FhWn1dqkl7znS5Asego=; b=xk8YplCsWGo53VpK4OpXV6KHAlHMfqTcvtsZoB2QuohuWrB1dgA46MKdcIE4wPr/r+ 2d9FUpaUypVFuD1RBVLkRzmmdrxwd+dWYspxDpmKqxcHMiMqcZSdZLRfYTvmItGdbPuT +xMXRdZEZkaQW8Hb1/MLPCCoP0xqawDjtooYHRGkaetCP6du5dINeS8zNplxnLnIlypJ ltUX3aOVWRkbef+W/o+lNN9TCW3V4v6kLz1lw5XSOq/KtFIfEoDruARCiD0oOTszdwJr +Fx0GkredvTI0LDLPhXFKp8DJfohpOkcA0FHDM0RQk6GZ6r84FTpidz7m3hEymzK6+h4 5Jbw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id my18-20020a17090b4c9200b001fb0ec88c74si9071200pjb.176.2022.09.04.09.39.46; Sun, 04 Sep 2022 09:40:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234526AbiIDQFR (ORCPT + 99 others); Sun, 4 Sep 2022 12:05:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33576 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229596AbiIDQFJ (ORCPT ); Sun, 4 Sep 2022 12:05:09 -0400 Received: from smtp.smtpout.orange.fr (smtp07.smtpout.orange.fr [80.12.242.129]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BF1D826AD3 for ; Sun, 4 Sep 2022 09:05:06 -0700 (PDT) Received: from [192.168.1.18] ([90.11.190.129]) by smtp.orange.fr with ESMTPA id Us7LoN8WyXFXxUs7Lorkc5; Sun, 04 Sep 2022 18:05:04 +0200 X-ME-Helo: [192.168.1.18] X-ME-Auth: Y2hyaXN0b3BoZS5qYWlsbGV0QHdhbmFkb28uZnI= X-ME-Date: Sun, 04 Sep 2022 18:05:04 +0200 X-ME-IP: 90.11.190.129 Message-ID: Date: Sun, 4 Sep 2022 18:05:02 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0 Subject: Re: [PATCH] nvdimm: Avoid wasting some memory. Content-Language: en-GB To: Dan Williams , Vishal Verma , Dave Jiang , Ira Weiny Cc: linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org, nvdimm@lists.linux.dev References: <8355cb2b720f8cd0f1315b06d70b541ba38add30.1662299370.git.christophe.jaillet@wanadoo.fr> <6314b859df5e2_2202c6294f5@dwillia2-xfh.jf.intel.com.notmuch> From: Christophe JAILLET In-Reply-To: <6314b859df5e2_2202c6294f5@dwillia2-xfh.jf.intel.com.notmuch> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Le 04/09/2022 à 16:38, Dan Williams a écrit : > 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 >> --- >> 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? > Hi, it would be fine, but it is not that easy. (read: any idea to implement it is welcomed :) ) I made a try a few weeks ago. See [1]. It triggered obvious issues spotted by 0day robot . In fact, "making clever things" in devm_kmalloc() prevent using devm_kfree() (or would require some over-engineering). Greg also argued that it is likely that devm_ allocated memory does not happen that much. I posted today a few similar patches as the one above in different subsystem to get some feed-backs whether open-coding it in "interesting" places make sense or not. Spotted such places is not that hard with a home made additional check in smatch. CJ [1]: https://lore.kernel.org/all/92ec2f78e8d38f68da95d9250cf3f86b2fbe78ad.1658570017.git.christophe.jaillet@wanadoo.fr/