From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E4D6BC43334 for ; Sat, 23 Jul 2022 10:10:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 137FF6B0071; Sat, 23 Jul 2022 06:10:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0E7926B0073; Sat, 23 Jul 2022 06:10:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F185E6B0074; Sat, 23 Jul 2022 06:10:55 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id E243F6B0071 for ; Sat, 23 Jul 2022 06:10:55 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id B2406AB79D for ; Sat, 23 Jul 2022 10:10:55 +0000 (UTC) X-FDA: 79717946070.03.674A467 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf24.hostedemail.com (Postfix) with ESMTP id 1CD37180093 for ; Sat, 23 Jul 2022 10:10:54 +0000 (UTC) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 26E76B80EB8; Sat, 23 Jul 2022 10:10:53 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4DCB2C341C0; Sat, 23 Jul 2022 10:10:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1658571051; bh=Mhj0fxlNgLETsShHldqHVQieQooTSHPA5ALqygMSW9s=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=mFrRfKLLwEMY5fsQxtg5TlPPTlBY8DCVrwPaG6oFEY90c9w/4fxPeTYSahHprzAch WyfLmUmQ8qAyCWvvBBRYdEgQKpx9rJti2j+VLflIWN8vV1sJTfze4oRam+Dqjh8jJm msDsS20skul7L+8iykXqxGNP3dKq3noIrW8eomos= Date: Sat, 23 Jul 2022 12:10:48 +0200 From: Greg Kroah-Hartman To: Christophe JAILLET Cc: akpm@linux-foundation.org, feng.tang@intel.com, "Rafael J. Wysocki" , linux-kernel@vger.kernel.org, linux-mm@kvack.org, kernel-janitors@vger.kernel.org Subject: Re: [RFC PATCH] devres: avoid over memory allocation with managed memory allocation Message-ID: References: <92ec2f78e8d38f68da95d9250cf3f86b2fbe78ad.1658570017.git.christophe.jaillet@wanadoo.fr> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <92ec2f78e8d38f68da95d9250cf3f86b2fbe78ad.1658570017.git.christophe.jaillet@wanadoo.fr> ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1658571055; a=rsa-sha256; cv=none; b=yH/eiDC9uVnnsthYpwlJQxMJGW5aytlSfZ/TtweY59Df0fb5WDqH0/E8Tnh1QmdNLrWgZ6 OybGs0EDsnx0jczbgBh7JBUqryxzHaNE26Ghpz8Mmqg9fRJohxLx0V2h6qpoE0++oHP7ZM djma3BR5UFCnua+HsOa8RuaLwylXdLY= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=linuxfoundation.org header.s=korg header.b=mFrRfKLL; spf=pass (imf24.hostedemail.com: domain of gregkh@linuxfoundation.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=gregkh@linuxfoundation.org; dmarc=pass (policy=none) header.from=linuxfoundation.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1658571055; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=JCnte/XE4lsoA5LjkYJForMzpjIxcQ9lcIVkv6ZCTbM=; b=e7bpkhBjYwZWHqhIN1FxfijBL3PzGWvA4mBrlQb6ou09mb6956Qe8KFkJGE4T+44533c4K oPdXd7Pc54yMPBzB/DEAeB+o8y0qf5kd+FWjXP7MyaVOmn81e0GJiOuxQvd1vExzIiDkjF lmegh7+H/Ulh6dJYra8gfQPRkwew2G8= Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=linuxfoundation.org header.s=korg header.b=mFrRfKLL; spf=pass (imf24.hostedemail.com: domain of gregkh@linuxfoundation.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=gregkh@linuxfoundation.org; dmarc=pass (policy=none) header.from=linuxfoundation.org X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 1CD37180093 X-Stat-Signature: f9r8ab3fdx4944r8qc634ao4r98tncwh X-Rspam-User: X-HE-Tag: 1658571054-140087 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Sat, Jul 23, 2022 at 12:04:33PM +0200, Christophe JAILLET wrote: > On one side, when using devm_kmalloc(), a memory overhead is added in order > to keep track of the data needed to release the resources automagically. > > On the other side, kmalloc() also rounds-up the required memory size in > order to ease memory reuse and avoid memory fragmentation. > > Both behavior together can lead to some over memory allocation which can > be avoided. > > For example: > - if 4096 bytes of managed memory is required > - "4096 + sizeof(struct devres_node)" bytes are required to the memory > allocator > - 8192 bytes are allocated and nearly half of it is wasted > > In such a case, it would be better to really allocate 4096 bytes of memory > and record an "action" to perform the kfree() when needed. > > On my 64 bits system: > sizeof(struct devres_node) = 40 > sizeof(struct action_devres) = 16 > > So, a devm_add_action() call will allocate 56, rounded up to 64 bytes. > > kmalloc() uses hunks of 8k, 4k, 2k, 1k, 512, 256, 192, 128, 96, 64, 32, 16, > 8 bytes. > > So in order to save some memory, if the 256 bytes boundary is crossed > because of the overhead of devm_kmalloc(), 2 distinct memory allocations > make sense. > > Signed-off-by: Christophe JAILLET > --- > This patch is only a RFC to get feed-back on the proposed approach. > > It is compile tested only. > I don't have numbers to see how much memory could be saved. > I don't have numbers on the performance impact. > > Should this makes sense to anyone, I would really appreciate getting some > numbers from others to confirm if it make sense or not. > > > The idea of this patch came to me because of a discussion initiated by > Feng Tang . He proposes to track wasted memory > allocation in order to give hints on where optimizations can be done. > > My approach is to avoid part of these allocations when due to the usage of > a devm_ function. > > > The drawbacks I see are: > - code is more complex > - this concurs to memory fragmentation because there will be 2 memory > allocations, instead of just 1 > - this is slower for every memory allocation because of the while loop > and tests > - the magic 256 constant is maybe not relevant on all systems > - some places of the kernel already take advantage of this over memory > allocation. So unpredictable impacts can occur somewhere! (see [1], > which is part of the [2] thread) > - this makes some assumption in devres.c on how memory allocation works, > which is not a great idea :( > > The advantages I see: > - in some cases, it saves some memory :) > - fragmentation is not necessarily an issue, devm_ allocated memory > are rarely freed, right? I think devm_ allocated memory does not happen that much, try it on your systems and see! Numbers would be great to have, can you run some benchmarks? Try it on a "common" SoC device (raspberry pi?) and a desktop to compare. thanks, greg k-h