Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp468727ybl; Tue, 28 Jan 2020 06:26:56 -0800 (PST) X-Google-Smtp-Source: APXvYqw++Ex3aW5G5jcZu+CD8+USouxZMAme9V8IaXln407P9eIFYsCxAjllNazXeBkWc4Xm6KXL X-Received: by 2002:aca:c509:: with SMTP id v9mr2839646oif.151.1580221616575; Tue, 28 Jan 2020 06:26:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1580221616; cv=none; d=google.com; s=arc-20160816; b=ffgVCQCHqFQL/16UqGkJGTBiVw8VO/PvdkyqpbkSAbY1ANf5z2Jo+mRr7e+W2LS9tw fGhUGqckFnuVb+0LXHGTKfF8SzTKQhS9oXEorcS0X0G5fOcW2vA8NrRYaIGpbGIXXwu3 T5SlKHt/8DBU4H6HOc+wLk2tcifnISFPehhb94zOnwbBCYwe75ccHbaeT2zWHJLIcZi5 R+uL8Uvg6nuMsMeuTzZT1PXDGl1AB6TqcjNQQwxDa4iJi05Se+qc6D+4C60zyILkOwtg 8KP+PM3hQ3lRZIAEPBM1Da4E/gxd57o63t2uAI+mCgiEz7qe66Td4oP0ZLIta9iCVWoT 9Muw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=bizTSG6fHk/qVXWu3y9uMwHX/6Kjok9EcVUHsuKzsxM=; b=Y8kl01smZW6jZHMxZGG2evWK3Jy/Bjdylati9+QmDDQJilJ1lImh0X6TgT8q3IKTRa +pzVYaBhE9qbbdwj+vD6OHpNOScilpxHZhr48I2OwdHGIqiDFi8T0m/+Ss0qcOJyUQJt YOjeEDhOM1vv2SLeMQgADtPgQCht+zwE+Pjo1yvDXcQwYs1IKWtdA1LSX4SKa38gwtsO 2Rcg37jB0OQSvH6OWKkdUc5EhlBNnfJZRax1NgW5W16PrkLXhvPqEigdRIWj+4YwBS1J VgN5AwFiKoKz/d+4mBTqlcclWZIHzrdMunm5f700gUDOZz7bPnP313qOULjFB9AiuIUB eVoA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=0YRoWhVo; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q19si6266063otk.136.2020.01.28.06.26.43; Tue, 28 Jan 2020 06:26:56 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=0YRoWhVo; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729697AbgA1OYu (ORCPT + 99 others); Tue, 28 Jan 2020 09:24:50 -0500 Received: from mail.kernel.org ([198.145.29.99]:51606 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732757AbgA1OYp (ORCPT ); Tue, 28 Jan 2020 09:24:45 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 8393124690; Tue, 28 Jan 2020 14:24:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1580221485; bh=Jl/m6jnZp9jCJNCIyANC8hoEDN1lD6koiO7lkxw94EE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=0YRoWhVoaVl7JodfndaXQceWJN06sOvrPDAqDGCbD94Mwoi1lg5wPJnZz6K27k78U sU7Ff1ERyK/C2uW1EYm4dtCMX2pkuDrzZZveYb6P1OuOokjiNS4h6OJbCe+7lpQWNI q7OxIyuygflyoJigAw+D07p/dIgBV7k32Oo4EG4k= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Guenter Roeck Subject: [PATCH 4.9 244/271] hwmon: (core) Simplify sysfs attribute name allocation Date: Tue, 28 Jan 2020 15:06:33 +0100 Message-Id: <20200128135910.690862660@linuxfoundation.org> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200128135852.449088278@linuxfoundation.org> References: <20200128135852.449088278@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Guenter Roeck commit 3a412d5e4a1c831723d0aaf305f1cf9a78ad9c90 upstream. Allocating the sysfs attribute name only if needed and only with the required minimum length looks optimal, but does not take the additional overhead for both devm_ data structures and the allocation header itself into account. This also results in unnecessary memory fragmentation. Move the sysfs name string into struct hwmon_device_attribute and give it a sufficient length to reduce this overhead. Signed-off-by: Guenter Roeck Signed-off-by: Greg Kroah-Hartman --- drivers/hwmon/hwmon.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) --- a/drivers/hwmon/hwmon.c +++ b/drivers/hwmon/hwmon.c @@ -38,12 +38,15 @@ struct hwmon_device { #define to_hwmon_device(d) container_of(d, struct hwmon_device, dev) +#define MAX_SYSFS_ATTR_NAME_LENGTH 32 + struct hwmon_device_attribute { struct device_attribute dev_attr; const struct hwmon_ops *ops; enum hwmon_sensor_types type; u32 attr; int index; + char name[MAX_SYSFS_ATTR_NAME_LENGTH]; }; #define to_hwmon_attr(d) \ @@ -232,20 +235,18 @@ static struct attribute *hwmon_genattr(s if ((mode & S_IWUGO) && !ops->write) return ERR_PTR(-EINVAL); + hattr = devm_kzalloc(dev, sizeof(*hattr), GFP_KERNEL); + if (!hattr) + return ERR_PTR(-ENOMEM); + if (type == hwmon_chip) { name = (char *)template; } else { - name = devm_kzalloc(dev, strlen(template) + 16, GFP_KERNEL); - if (!name) - return ERR_PTR(-ENOMEM); - scnprintf(name, strlen(template) + 16, template, + scnprintf(hattr->name, sizeof(hattr->name), template, index + hwmon_attr_base(type)); + name = hattr->name; } - hattr = devm_kzalloc(dev, sizeof(*hattr), GFP_KERNEL); - if (!hattr) - return ERR_PTR(-ENOMEM); - hattr->type = type; hattr->attr = attr; hattr->index = index;