Message ID | 20231009165741.746184-2-max.kellermann@ionos.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a888:0:b0:403:3b70:6f57 with SMTP id x8csp2001166vqo; Mon, 9 Oct 2023 09:58:35 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFJJa7zLnhcholS2iFDUWyktbCNdzvFnQr6tXOVvnt6bdbUEUfOPNr9k8j4ryfAtRqr058Z X-Received: by 2002:a05:6a20:138a:b0:159:c918:1016 with SMTP id hn10-20020a056a20138a00b00159c9181016mr12459606pzc.49.1696870714783; Mon, 09 Oct 2023 09:58:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696870714; cv=none; d=google.com; s=arc-20160816; b=CC2lu51bLpsWatW4MQnu+4G3nxPCilMWl33Y/b2MVY7WQyQ3Ad5aNtAnTSaw0k+aCA 3o1PeHfp/TtdF8rfnTDE0lUuxGGZ/AvqU6KdNFjNSxOHM6sQ3A8SAgIsKgxvq69vhSrm tMu2vXmNH99YvhlRjWWOKLammdL98+xslfcYzAjAA1J8sdDEriB4vcvi9qO7RmDSA15x O7JYTuyVC1niB6Qvnkl0fcGSr8cGe8IPjdec3CdidirmwB8xBTjoLKa9FHme85TN6kEI LyNPVU+yE7q3wSUfW9N1KBRa31OhM8QYaFGyOrhGKls7ACCdoaQT5OQ2JZGJyaQ3GFyf 99vg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=nZAV/Qw52sFDXsSaIlMwU7Snqmg7zw6UPrOsGwKv28I=; fh=cuLvoHX9Ajmuv5XlviAdu70AqC2y0B1pmoGXNJDobNI=; b=v1ZgLWbOcjql1rIi7l3cs4SYmcZ05/Iy9BkvuXpeDAFfuMNcHyw1/21wP2BBvIHgpP 7cMJ348MPVXWwHBDs+G//4j432ZXrnFxZPp5ITAhSI7n0AH8rpxiOFsUZ8TUhHdOerFF eLDNReKg6ucq1v4zzsQuCLOZI8vGPgcsT8Na4bLOIy2r+Q2qBAP+qRhOTZrUf9B7oWZb yxa64AfqCuIfB815gjYsp/k7ZStchNFjSOqtP9kxfk1yiM/Ujh8B+jEAke5OmDDDChl1 zeu5Hh7d0KkYGMjJXHxsi5a3KkFuf57ymiUEn/qv8IV0iFZCa5TnA5c5RNF8vIe0RwgQ X5dQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ionos.com header.s=google header.b=Xij2wRHZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=ionos.com Received: from pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id r16-20020a632b10000000b00565e865d381si9577585pgr.447.2023.10.09.09.58.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Oct 2023 09:58:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@ionos.com header.s=google header.b=Xij2wRHZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=ionos.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 57EF38047552; Mon, 9 Oct 2023 09:58:22 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377359AbjJIQ54 (ORCPT <rfc822;ezelljr.billy@gmail.com> + 18 others); Mon, 9 Oct 2023 12:57:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42886 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377268AbjJIQ5y (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 9 Oct 2023 12:57:54 -0400 Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9D7359E for <linux-kernel@vger.kernel.org>; Mon, 9 Oct 2023 09:57:52 -0700 (PDT) Received: by mail-wm1-x329.google.com with SMTP id 5b1f17b1804b1-406618d0991so44395385e9.2 for <linux-kernel@vger.kernel.org>; Mon, 09 Oct 2023 09:57:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ionos.com; s=google; t=1696870671; x=1697475471; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=nZAV/Qw52sFDXsSaIlMwU7Snqmg7zw6UPrOsGwKv28I=; b=Xij2wRHZ5KF7wmkX7GQJ2dIPxBy1Epr8FAvrKi2bwiEVfKZHdCcVU/T8Lt75EygD2u oe5rYLMJ5PoMdo9nxW3OwXWw/fbH51WHlEoJcgrE6i8t3Y7Bfnthw2A4j56PJ5/9nmKU vDAwZeNWLZSVBWHFs+3Cowk1wjMQP9sLR7KSvag3y8H+OkkgsafHS6+F8J7Fbq01onng Qd2jI3AEfaH2F0zV6KnHeGHPRNkkuL4VRbNfz5CZYkrF4CogUPEKA6mG6EK6/WTwYelK SatIrPdYGt+kOHtTXAsSJfK4Jd9IkEUfg5AF7lwjhZJT5w5We8WtrtxddaJRpHApGGMT po7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696870671; x=1697475471; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=nZAV/Qw52sFDXsSaIlMwU7Snqmg7zw6UPrOsGwKv28I=; b=RlGRFHZYgCvIh15B+LVnEhbLd3g3DPj1gfrDRUTuPY+J8GRnZcYCqQIr+VHVMZiY34 biJZvZl22ABbXR1TezAN0aNzybTSqrOFd+0TpO7046vHXKAPKYiKsC6QKy7cE7kSjxuF JMdRw0vBJpj8wnCS0rxRPdJ+y9zjhw4kiXSMAkBuFrCn+4d8/ZsPicuG3UIDJ9HbWe7Z 36NC0Rcol/ZOr5s4xrSxtf7+9rgBq3+3lc2ylaN3iVuyjqM6RaQVywXyUiknULCt99aC RdmM6OWrNh5ijjohqhjnmP4CpMC3VowyP/qNmhwCc3QjfSOKmgSVZHhXGq+y5PZnSVhh mL4g== X-Gm-Message-State: AOJu0Yz3tjLxHEZyI97buqeBfR0/DW+YhbXTuOFMBQikuwl6QRl878bl vQTG/hsVSwuWmgzwneP9YCS7RA== X-Received: by 2002:a7b:cb8b:0:b0:402:8c7e:3fc4 with SMTP id m11-20020a7bcb8b000000b004028c7e3fc4mr14562722wmi.30.1696870671100; Mon, 09 Oct 2023 09:57:51 -0700 (PDT) Received: from heron.intern.cm-ag (p200300dc6f49a600529a4cfffe3dd983.dip0.t-ipconnect.de. [2003:dc:6f49:a600:529a:4cff:fe3d:d983]) by smtp.gmail.com with ESMTPSA id d9-20020adff2c9000000b00324887a13f7sm10199828wrp.0.2023.10.09.09.57.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Oct 2023 09:57:50 -0700 (PDT) From: Max Kellermann <max.kellermann@ionos.com> To: Jean Delvare <jdelvare@suse.com>, Guenter Roeck <linux@roeck-us.net> Cc: Max Kellermann <max.kellermann@ionos.com>, linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/7] drivers/hwmon: add local variable for newly allocated attribute_group** Date: Mon, 9 Oct 2023 18:57:35 +0200 Message-Id: <20231009165741.746184-2-max.kellermann@ionos.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231009165741.746184-1-max.kellermann@ionos.com> References: <20231009165741.746184-1-max.kellermann@ionos.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=2.7 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Mon, 09 Oct 2023 09:58:22 -0700 (PDT) X-Spam-Level: ** X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779297906236364720 X-GMAIL-MSGID: 1779297906236364720 |
Series |
[1/7] drivers/rtc/sysfs: move code to count_attribute_groups()
|
|
Commit Message
Max Kellermann
Oct. 9, 2023, 4:57 p.m. UTC
This allows the compiler to keep the pointer in a register and
prepares for making the struct field "const".
Signed-off-by: Max Kellermann <max.kellermann@ionos.com>
---
drivers/hwmon/hwmon.c | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)
Comments
On Mon, Oct 09, 2023 at 06:57:35PM +0200, Max Kellermann wrote: > This allows the compiler to keep the pointer in a register and > prepares for making the struct field "const". > > Signed-off-by: Max Kellermann <max.kellermann@ionos.com> I have no idea what this is about, and I don't see how that would improve anything, but ... > --- > drivers/hwmon/hwmon.c | 11 ++++++----- > 1 file changed, 6 insertions(+), 5 deletions(-) > > diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c > index c7dd3f5b2bd5..e50ab229b27d 100644 > --- a/drivers/hwmon/hwmon.c > +++ b/drivers/hwmon/hwmon.c > @@ -783,6 +783,7 @@ __hwmon_device_register(struct device *dev, const char *name, void *drvdata, > hdev = &hwdev->dev; > > if (chip) { > + const struct attribute_group **new_groups; > struct attribute **attrs; > int ngroups = 2; /* terminating NULL plus &hwdev->groups */ > > @@ -790,8 +791,8 @@ __hwmon_device_register(struct device *dev, const char *name, void *drvdata, > for (i = 0; groups[i]; i++) > ngroups++; > > - hwdev->groups = kcalloc(ngroups, sizeof(*groups), GFP_KERNEL); > - if (!hwdev->groups) { > + hwdev->groups = new_groups = kcalloc(ngroups, sizeof(*new_groups), GFP_KERNEL); CHECK: multiple assignments should be avoided #101: FILE: drivers/hwmon/hwmon.c:794: + hwdev->groups = new_groups = kcalloc(ngroups, sizeof(*new_groups), GFP_KERNEL); either case, this change is not acceptable. Guenter
On Mon, Oct 09, 2023 at 06:57:35PM +0200, Max Kellermann wrote: > This allows the compiler to keep the pointer in a register and Maybe, maybe not, there's no guarantee for register usage. And it doesn't matter, this is a very slow path, no registers are required :) > prepares for making the struct field "const". What struct field? > > Signed-off-by: Max Kellermann <max.kellermann@ionos.com> > --- > drivers/hwmon/hwmon.c | 11 ++++++----- > 1 file changed, 6 insertions(+), 5 deletions(-) > > diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c > index c7dd3f5b2bd5..e50ab229b27d 100644 > --- a/drivers/hwmon/hwmon.c > +++ b/drivers/hwmon/hwmon.c > @@ -783,6 +783,7 @@ __hwmon_device_register(struct device *dev, const char *name, void *drvdata, > hdev = &hwdev->dev; > > if (chip) { > + const struct attribute_group **new_groups; > struct attribute **attrs; > int ngroups = 2; /* terminating NULL plus &hwdev->groups */ > > @@ -790,8 +791,8 @@ __hwmon_device_register(struct device *dev, const char *name, void *drvdata, > for (i = 0; groups[i]; i++) > ngroups++; > > - hwdev->groups = kcalloc(ngroups, sizeof(*groups), GFP_KERNEL); > - if (!hwdev->groups) { > + hwdev->groups = new_groups = kcalloc(ngroups, sizeof(*new_groups), GFP_KERNEL); So you have a const pointer AND a non-const pointer pointing to the same thing? > + if (!new_groups) { > err = -ENOMEM; > goto free_hwmon; > } > @@ -804,14 +805,14 @@ __hwmon_device_register(struct device *dev, const char *name, void *drvdata, > > hwdev->group.attrs = attrs; > ngroups = 0; > - hwdev->groups[ngroups++] = &hwdev->group; > + new_groups[ngroups++] = &hwdev->group; This shoul be identical, you assign both above the same way, so why change this? > > if (groups) { > for (i = 0; groups[i]; i++) > - hwdev->groups[ngroups++] = groups[i]; > + new_groups[ngroups++] = groups[i]; Same here. thanks, greg k-h
On Mon, Oct 9, 2023 at 7:19 PM Guenter Roeck <linux@roeck-us.net> wrote: > I have no idea what this is about, and I don't see how that would > improve anything, but ... Later, we can make lots of global variables "const", which puts them in ".rodata" (write-protected at runtime). This is some micro-hardening. > CHECK: multiple assignments should be avoided > #101: FILE: drivers/hwmon/hwmon.c:794: > + hwdev->groups = new_groups = kcalloc(ngroups, sizeof(*new_groups), GFP_KERNEL); What program emitted this warning? checkpatch.pl had no error. I'll change it in all patches. > either case, this change is not acceptable. Because of the multi-assignment, or is there something else?
On Mon, Oct 9, 2023 at 7:27 PM Greg KH <greg@kroah.com> wrote: > > On Mon, Oct 09, 2023 at 06:57:35PM +0200, Max Kellermann wrote: > > This allows the compiler to keep the pointer in a register and > > Maybe, maybe not, there's no guarantee for register usage. Agree. But without this change, register usage is effectively prevented (much of the time). With this change, there is a chance for better code. Yeah, it's not a critical code path, and it's only a tiny tiny optimization - but the real reason for this patch is .... > > - hwdev->groups = kcalloc(ngroups, sizeof(*groups), GFP_KERNEL); > > - if (!hwdev->groups) { > > + hwdev->groups = new_groups = kcalloc(ngroups, sizeof(*new_groups), GFP_KERNEL); > > So you have a const pointer AND a non-const pointer pointing to the same > thing? .... so I can make "hwdev->groups" const, which wouldn't allow modifying it. (This isn't obvious in this one patch, but a later patch in the series does this.) There are only few functions which allocate a new pointer-array dynamically; all others are global variables which currently cannot be "const". This patch set does some lifting to allow adding "const".
On Mon, Oct 09, 2023 at 07:28:03PM +0200, Max Kellermann wrote: > On Mon, Oct 9, 2023 at 7:19 PM Guenter Roeck <linux@roeck-us.net> wrote: > > I have no idea what this is about, and I don't see how that would > > improve anything, but ... > > Later, we can make lots of global variables "const", which puts them > in ".rodata" (write-protected at runtime). This is some > micro-hardening. > > > CHECK: multiple assignments should be avoided > > #101: FILE: drivers/hwmon/hwmon.c:794: > > + hwdev->groups = new_groups = kcalloc(ngroups, sizeof(*new_groups), GFP_KERNEL); > > What program emitted this warning? checkpatch.pl had no error. I'll > change it in all patches. I doubt that you ran checkpatch --strict. That check has existed in checkpatch at least since 2007. Also, process/coding-style.rst says: Don't put multiple assignments on a single line either. Kernel coding style is super simple. Avoid tricky expressions. As far as I know that guildeline has not changed. Now, you might argue something like "who cares about checkpatch --strict", but in Documentation/hwmon/submitting-patches.rst we specifically say * Please run your patch through 'checkpatch --strict'. There should be no errors, no warnings, and few if any check messages. If there are any messages, please be prepared to explain. So, please explain why this message and with it the coding style violation should be ignored. > > > either case, this change is not acceptable. > > Because of the multi-assignment, or is there something else? I don't really see the benefit of this code, and I am not sure if the explanation about compiler optimization is really valid. It makes me want to run some test compliations to see if the claim is really true, and I really don't have time for that. Also, as Greg points out, this is not in a hot code path but executed exactly once for each hwmon device, so making such a change with a reason like that just invites lots of follow-up patches with similar reasons, and then the submitters of those can point to this patch and argue "but you accepted that one". You say "micro-hardening" above, but for me it is time consuming micro-optimization. Guenter
diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c index c7dd3f5b2bd5..e50ab229b27d 100644 --- a/drivers/hwmon/hwmon.c +++ b/drivers/hwmon/hwmon.c @@ -783,6 +783,7 @@ __hwmon_device_register(struct device *dev, const char *name, void *drvdata, hdev = &hwdev->dev; if (chip) { + const struct attribute_group **new_groups; struct attribute **attrs; int ngroups = 2; /* terminating NULL plus &hwdev->groups */ @@ -790,8 +791,8 @@ __hwmon_device_register(struct device *dev, const char *name, void *drvdata, for (i = 0; groups[i]; i++) ngroups++; - hwdev->groups = kcalloc(ngroups, sizeof(*groups), GFP_KERNEL); - if (!hwdev->groups) { + hwdev->groups = new_groups = kcalloc(ngroups, sizeof(*new_groups), GFP_KERNEL); + if (!new_groups) { err = -ENOMEM; goto free_hwmon; } @@ -804,14 +805,14 @@ __hwmon_device_register(struct device *dev, const char *name, void *drvdata, hwdev->group.attrs = attrs; ngroups = 0; - hwdev->groups[ngroups++] = &hwdev->group; + new_groups[ngroups++] = &hwdev->group; if (groups) { for (i = 0; groups[i]; i++) - hwdev->groups[ngroups++] = groups[i]; + new_groups[ngroups++] = groups[i]; } - hdev->groups = hwdev->groups; + hdev->groups = new_groups; } else { hdev->groups = groups; }