From nobody Sun Dec 14 06:18:53 2025 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 32096137930 for ; Thu, 16 Jan 2025 17:32:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.69.126.157 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737048753; cv=none; b=qCbQo0l34P7vK3KcUI8xe0P9seVAQdtmZ2lDJ6E0qT0GGgdnsmjLzdFke88lIoLmA+N8UIBMX7xUuwuyrjd1r5hLnEZwvF3FDBP3k0DTDLnZL8PUcsKheYT84/NThL90cVeSmpDIyIjmq1Xj7rv2Bg9jdGpolpfHRyQGq4ksqrU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737048753; c=relaxed/simple; bh=ZooFOr+F9P8aM+Or8c2aeQigL8fE9gjVpBNM3E6kQNk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=L5StiK11k/GDTMJeniExOFYzsKIkLl0oVD5YZPY6G9xCPWce+l6zafZgWJNGncwWfmI25MqOKI+t8hcdczGcXdAikrC/w6lm/+oabmgUkCSF0TmIj/BlLfyfjbCUJ2jC+KqGTJua8O4iR1lwg0In6eOSE5/bPRImkukZURgGM2A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net; spf=pass smtp.mailfrom=weissschuh.net; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b=sFWRW/BT; arc=none smtp.client-ip=159.69.126.157 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b="sFWRW/BT" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1737048750; bh=ZooFOr+F9P8aM+Or8c2aeQigL8fE9gjVpBNM3E6kQNk=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=sFWRW/BTPWBGaxfODEfF5JWLtlduK83UN8rdMtCJZiocG7uKq2GM34ApnVxOIEotG INvlRchIr24zHMdnZtYbwAfuMv2VAwGKqhTIALInsb84vond5gifewXcK44NEE5FWV /orWNbh0GkcrYBFJ3r8xIqtDveME/27aLGKlzXp8= From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Date: Thu, 16 Jan 2025 18:32:27 +0100 Subject: [PATCH 1/6] sysfs: attribute_group: allow registration of const attribute Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250116-sysfs-const-attr-prep-v1-1-15e72dba4205@weissschuh.net> References: <20250116-sysfs-const-attr-prep-v1-0-15e72dba4205@weissschuh.net> In-Reply-To: <20250116-sysfs-const-attr-prep-v1-0-15e72dba4205@weissschuh.net> To: Greg Kroah-Hartman , "Rafael J. Wysocki" Cc: linux-kernel@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1737048749; l=1595; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=ZooFOr+F9P8aM+Or8c2aeQigL8fE9gjVpBNM3E6kQNk=; b=hZRJOAT+SlgG/5sf/fV/I0blvKHmQgaaXR30LiZV+jrb4YbVYPAqlFH4f71Ra9oIVSF17ModM ljRc3j9VV+ECgn+193o8ileL+ket/Txwsv/89p2kLjjSJVajA0M6Ih1 X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= To be able to constify instances of struct attribute it has to be possible to add them to struct attribute_group. The current type of the attrs member however is not compatible with that. Introduce a union that allows registration of both const and non-const attributes to enable a piecewise transition. As both union member types are compatible no logic needs to be adapted. Technically it is now possible register a const struct attribute and receive it as mutable pointer in the callbacks. This is a soundness issue. But this same soundness issue already exists today in sysfs_create_file(). Also the struct definition and callback implementation are always closely linked and are meant to be moved to const in lockstep. Similar to commit 906c508afdca ("sysfs: attribute_group: allow registration= of const bin_attribute") Signed-off-by: Thomas Wei=C3=9Fschuh --- include/linux/sysfs.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h index 0f2fcd244523f050c5286f19d4fe1846506f9214..f5e25bed777a6a6e717f10973f1= abcd12111f5c5 100644 --- a/include/linux/sysfs.h +++ b/include/linux/sysfs.h @@ -105,7 +105,10 @@ struct attribute_group { size_t (*bin_size)(struct kobject *, const struct bin_attribute *, int); - struct attribute **attrs; + union { + struct attribute **attrs; + const struct attribute *const *attrs_new; + }; union { struct bin_attribute **bin_attrs; const struct bin_attribute *const *bin_attrs_new; --=20 2.48.1 From nobody Sun Dec 14 06:18:53 2025 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DE65841C6A for ; Thu, 16 Jan 2025 17:32:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.69.126.157 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737048753; cv=none; b=urkc4XRR37Y45r+BxhgHenBzYuw08v9+xI+0me+QOpdPHKNi4HOKaK74o2c7bcg+tU4fXEHmw4PauyCHYhV/eWBRZ8rUq5eLJKeKRLvgVvUR1LlN7pZFgkaCYyI4J6YcCUB3x4U7zuUVlhVYG1oRQ3/5sph9CVQ4e9F8cL8fU8Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737048753; c=relaxed/simple; bh=zEW5K2/phz/49lLz80p4xhEHmzhVd+bbbhqnAzLmxNc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=FCQ5R52Qubi0HPi9bPwqZT2ZGF8HK6Ycl5ycLNPe+vJoFcNOum/LamlHVnD9Jh1TgQEhCNIk2QHxCyQuEPEkJJbZdu6yHL3VFy8AMCmCffKPs22x+GY3yYVXM5VLOjSCN/0mchOOS1KA3lrqdtuTsrihdJn3wy9HeGDxFRFp0lI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net; spf=pass smtp.mailfrom=weissschuh.net; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b=hutsRqzo; arc=none smtp.client-ip=159.69.126.157 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b="hutsRqzo" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1737048750; bh=zEW5K2/phz/49lLz80p4xhEHmzhVd+bbbhqnAzLmxNc=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=hutsRqzobAa2y5umZDzmUatXKcgcDuBeVY0W7wnNMv2pz8jSr/TC3a6YaSO7BZI92 SLz9LU5t+hebCQIn61LYcsRsQjyC0fG0jSURMcwYqdtgszYt4PK5CDQyI1plAWh+NC mno1vwm5ayvGREJB8KcfJDGWJAn4m9WJTUXTrLtE= From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Date: Thu, 16 Jan 2025 18:32:28 +0100 Subject: [PATCH 2/6] sysfs: transparently handle const pointers in ATTRIBUTE_GROUPS() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250116-sysfs-const-attr-prep-v1-2-15e72dba4205@weissschuh.net> References: <20250116-sysfs-const-attr-prep-v1-0-15e72dba4205@weissschuh.net> In-Reply-To: <20250116-sysfs-const-attr-prep-v1-0-15e72dba4205@weissschuh.net> To: Greg Kroah-Hartman , "Rafael J. Wysocki" Cc: linux-kernel@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1737048749; l=1101; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=zEW5K2/phz/49lLz80p4xhEHmzhVd+bbbhqnAzLmxNc=; b=EI+uR3WhMi2ugOVkiGa96n8GyI8NsXURKAfMV2dmi9/oAm3Eq0RhnD699PTyhnpv58DPQomrL 96opjIi2CEmC8o7T5H5OEM1bDgXCSqelyMU5NjeA8JZG5JA6ifnFqtZ X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= To ease the constification process of 'struct attribute', transparently handle the const pointers in ATTRIBUTE_GROUPS(). A cast is used instead of assigning to .attrs_new as it keeps the macro smaller. As both members are aliased to each other the result is identical. Signed-off-by: Thomas Wei=C3=9Fschuh --- include/linux/sysfs.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h index f5e25bed777a6a6e717f10973f1abcd12111f5c5..52d4862bbcef2a6b143710a1980= 1222bf7abc346 100644 --- a/include/linux/sysfs.h +++ b/include/linux/sysfs.h @@ -290,7 +290,12 @@ static const struct attribute_group *_name##_groups[] = =3D { \ =20 #define ATTRIBUTE_GROUPS(_name) \ static const struct attribute_group _name##_group =3D { \ - .attrs =3D _name##_attrs, \ + .attrs =3D _Generic(_name##_attrs, \ + struct attribute **: \ + _name##_attrs, \ + const struct attribute *const *: \ + (void *)_name##_attrs \ + ), \ }; \ __ATTRIBUTE_GROUPS(_name) =20 --=20 2.48.1 From nobody Sun Dec 14 06:18:53 2025 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DE61B1F94D for ; Thu, 16 Jan 2025 17:32:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.69.126.157 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737048754; cv=none; b=t49RbkKQOELHXPSbZyP77eMz4OrfDhTn5dn2U/aJfHgwNFW7CSlPhsxereP8lVa0GJKR3ykA8MSQniu16JFoMV35++IDF3nUqlWzubOd+yj/myJGXVzuPJW3/ZRs2H4XJTmLtc4aWU7rZCNaXcwDoswzDjIKSQf8ljOwPTY/v6o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737048754; c=relaxed/simple; bh=FAwTQDOg1XS6NTjoryxHdzMTFrfzNsl8JlTJA9w2wxY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=u2i8MTRk041EjKQ8mu4k+Zf2DGhMbnyWsYoyo+HBe4f6zfiVqTBbkaShu8i/8q53mzyc0IC9fni/eCKKXy6MKbqmHzHmozias4hS4zT9+1KL0RsYU+l4CI/c/8tUWnoHN9SnlefF8gtNQMeH2XzMuiXvsjVEXC7vZhVuAT6GaWo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net; spf=pass smtp.mailfrom=weissschuh.net; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b=iqesPseA; arc=none smtp.client-ip=159.69.126.157 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b="iqesPseA" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1737048750; bh=FAwTQDOg1XS6NTjoryxHdzMTFrfzNsl8JlTJA9w2wxY=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=iqesPseA0hRVC1AcimVR5z4LHhZY3JmZX5CZesgHZ2F5CG6m3//n4qe7ZdRD87T27 CCBqrE5wU6uk744V1zmJ7jWQJhMLB2opB7NoPKHw5GjZA/ZJJYNlwKmQb79BB6BYvV bwahi1rVLtx8lv5ZU+uOHVMi/wWQWXL1VzZ+iBt4= From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Date: Thu, 16 Jan 2025 18:32:29 +0100 Subject: [PATCH 3/6] sysfs: introduce __SYSFS_FUNCTION_ALTERNATIVE() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250116-sysfs-const-attr-prep-v1-3-15e72dba4205@weissschuh.net> References: <20250116-sysfs-const-attr-prep-v1-0-15e72dba4205@weissschuh.net> In-Reply-To: <20250116-sysfs-const-attr-prep-v1-0-15e72dba4205@weissschuh.net> To: Greg Kroah-Hartman , "Rafael J. Wysocki" Cc: linux-kernel@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1737048749; l=1400; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=FAwTQDOg1XS6NTjoryxHdzMTFrfzNsl8JlTJA9w2wxY=; b=ME5DBKGbrRXiz/SAVJADDFRH2McbeA6q6cWSaE9KqcA4cpOtdvL6MxcmR2juoqaFfWD3blqgH 5hAZ9pXjX7QClzb8c3p+VFwH8a7QTYFvRee1w006Ovw/Y3Qtj1CjUPO X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= For the constification phase of 'struct attribute' various callback struct members will need to exist in both const and non-const variants. Keeping both members in a union avoids memory and CPU overhead but will be detected and trapped by Control Flow Integrity (CFI). By deciding between a struct and a union depending whether CFI is enabled, most configurations can avoid this overhead. Code using these callbacks will still need to be updated to handle both members explicitly. In the union case the compiler will recognize that testing for one union member is enough and optimize away the code for the other one. Signed-off-by: Thomas Wei=C3=9Fschuh --- include/linux/sysfs.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h index 52d4862bbcef2a6b143710a19801222bf7abc346..46e9261bc35f248a733d27c05c9= 31080b0c58e17 100644 --- a/include/linux/sysfs.h +++ b/include/linux/sysfs.h @@ -58,6 +58,12 @@ do { \ #define sysfs_attr_init(attr) do {} while (0) #endif =20 +#ifdef CONFIG_CFI_CLANG +#define __SYSFS_FUNCTION_ALTERNATIVE(MEMBERS...) struct { MEMBERS } +#else +#define __SYSFS_FUNCTION_ALTERNATIVE(MEMBERS...) union { MEMBERS } +#endif + /** * struct attribute_group - data structure used to declare an attribute gr= oup. * @name: Optional: Attribute group name --=20 2.48.1 From nobody Sun Dec 14 06:18:53 2025 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3018922D4FF for ; Thu, 16 Jan 2025 17:32:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.69.126.157 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737048756; cv=none; b=Qz2YKnR3ivS9bbHhgbUPX59L+tFg14OzpJHl6vEUpQrU3ji6WAAX+qXMAXuFRjS9S/OTDVbL5mIeOR+E9FwSHfqGNUY6zTZIl9/S41jWxKq2E5QXfMJd9xDcZAo9ZE47ADZSgRGcdpLTcRgqrREEmOe5au2gUTHhOqp/+oAcx1c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737048756; c=relaxed/simple; bh=sE6+BpKZUxyQgUzl3N5TKDKS1J/Ojw3PKu+s1rUHYWk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=idICZ2i1GpBdpuwlctF7NNADez0dK4ACek6FbnjYUYWVVC6W1kEl8DlVB1WeQ2f8HmH4zP8Wt+IogUz+SP2iXOAROEUEZIV7VrNHc5Wx9+yDlPhdvX3nCKY1Ikr7prjVo/HHa3z3mQvjURZ7xwQhYcFQYe6rKsJ/ci5+wM6jNWU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net; spf=pass smtp.mailfrom=weissschuh.net; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b=jPjl6/Gb; arc=none smtp.client-ip=159.69.126.157 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b="jPjl6/Gb" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1737048750; bh=sE6+BpKZUxyQgUzl3N5TKDKS1J/Ojw3PKu+s1rUHYWk=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=jPjl6/GbgM4kHzbqjwwCXT34Sl+nvXeS0pLwWPKLid5017mmVr1lp83EsPuiTpGRm PKLn/KG9pGrfkInBH2DsTGJaG/DbFfAZ+0zng55otLudg8NGFvT+6LVNRghwfOumdu 6ZCtmnWxWlES8FOt0Wx6m3Rjt4QPVXh31NpNVfYc= From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Date: Thu, 16 Jan 2025 18:32:30 +0100 Subject: [PATCH 4/6] sysfs: attribute_group: enable const variants of is_visible() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250116-sysfs-const-attr-prep-v1-4-15e72dba4205@weissschuh.net> References: <20250116-sysfs-const-attr-prep-v1-0-15e72dba4205@weissschuh.net> In-Reply-To: <20250116-sysfs-const-attr-prep-v1-0-15e72dba4205@weissschuh.net> To: Greg Kroah-Hartman , "Rafael J. Wysocki" Cc: linux-kernel@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1737048749; l=2324; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=sE6+BpKZUxyQgUzl3N5TKDKS1J/Ojw3PKu+s1rUHYWk=; b=cOt02KgVOdSpvj1ruvm1aiLBDXTrawZjswSznw6HC37nyRh/eqcKYcTeeB4VSRfO8bmorlkXD wfCmOP0EtuJA5c5UuOiwidJiORhoEFv0FLeeH0h1+931gPNEhudroUe X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= When constifying instances of struct attribute, for consistency the corresponding .is_visible() callback should be adapted, too. Introduce a temporary transition mechanism until all callbacks are converted. Signed-off-by: Thomas Wei=C3=9Fschuh --- fs/sysfs/group.c | 10 ++++++++-- include/linux/sysfs.h | 8 ++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/fs/sysfs/group.c b/fs/sysfs/group.c index 8b01a7eda5fb3239e138372417d01967c7a3f122..2a227bf209f84f5bfe5da06a5ca= 1633f69ca4368 100644 --- a/fs/sysfs/group.c +++ b/fs/sysfs/group.c @@ -36,6 +36,9 @@ static umode_t __first_visible(const struct attribute_gro= up *grp, struct kobject if (grp->attrs && grp->attrs[0] && grp->is_visible) return grp->is_visible(kobj, grp->attrs[0], 0); =20 + if (grp->attrs && grp->attrs[0] && grp->is_visible_new) + return grp->is_visible_new(kobj, grp->attrs[0], 0); + if (grp->bin_attrs && grp->bin_attrs[0] && grp->is_bin_visible) return grp->is_bin_visible(kobj, grp->bin_attrs[0], 0); =20 @@ -61,8 +64,11 @@ static int create_files(struct kernfs_node *parent, stru= ct kobject *kobj, */ if (update) kernfs_remove_by_name(parent, (*attr)->name); - if (grp->is_visible) { - mode =3D grp->is_visible(kobj, *attr, i); + if (grp->is_visible || grp->is_visible_new) { + if (grp->is_visible) + mode =3D grp->is_visible(kobj, *attr, i); + else + mode =3D grp->is_visible_new(kobj, *attr, i); mode &=3D ~SYSFS_GROUP_INVISIBLE; if (!mode) continue; diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h index 46e9261bc35f248a733d27c05c931080b0c58e17..c68496ad5bcc7964b98291df689= c7451cf75eac8 100644 --- a/include/linux/sysfs.h +++ b/include/linux/sysfs.h @@ -104,8 +104,12 @@ do { \ */ struct attribute_group { const char *name; - umode_t (*is_visible)(struct kobject *, - struct attribute *, int); + __SYSFS_FUNCTION_ALTERNATIVE( + umode_t (*is_visible)(struct kobject *, + struct attribute *, int); + umode_t (*is_visible_new)(struct kobject *, + const struct attribute *, int); + ); umode_t (*is_bin_visible)(struct kobject *, const struct bin_attribute *, int); size_t (*bin_size)(struct kobject *, --=20 2.48.1 From nobody Sun Dec 14 06:18:53 2025 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 396F01428E7 for ; Thu, 16 Jan 2025 17:32:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.69.126.157 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737048754; cv=none; b=r91VOHM5fWl2gtpsXqIObzEFrbawWSUJTecFZSXtUBOIoUQoPaJQNtPSXaJdPbvKW9VGoLN81YDAI9zN3zt5b5PLV4glYTfvC+slr1VpH8+pOAlEw8LGj684YChvt2MOcRf+TCBCp3IYxWBZZJMoeZp+J4T/BqGMKiH5ZrE+7vo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737048754; c=relaxed/simple; bh=Zix9Nw+VLZajXUl2F/cXtQdh9/+HCkVae3P72cvfJVg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=i4HtWnSQ38Mjh8ZJTaGrjt85q89dv5vy0IdW0fgsgc0jRM2gHywa+J6WAHtja5tVhfBGi/6I4NAJCK72i3o3V3KlqWzqqHgvor8/vdcdJ7F5jJkzWoDammhwDLSRJqbCGlavMFfyUwou5zlJgAK/bxcpSHBZEGGRkXPR79lV2Q8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net; spf=pass smtp.mailfrom=weissschuh.net; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b=IeWqr8SR; arc=none smtp.client-ip=159.69.126.157 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b="IeWqr8SR" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1737048750; bh=Zix9Nw+VLZajXUl2F/cXtQdh9/+HCkVae3P72cvfJVg=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=IeWqr8SRqFrJ01jM+bF8ZpTjK10+H1kJbxZqrPVTeByO5uWYki/Xg0EZe1+3+s+Zk uLfU0i29oB6xkCvhSkF6l1YeVfg+Qx7mI8noXpCA3vEsxdWa84y0EquilisVYNGdSI YykbJHwSR0EDCoNIVlsVS4Cxm0aE6lSQkLUZd/Kw= From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Date: Thu, 16 Jan 2025 18:32:31 +0100 Subject: [PATCH 5/6] samples/kobject: add is_visible() callback to attribute group Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250116-sysfs-const-attr-prep-v1-5-15e72dba4205@weissschuh.net> References: <20250116-sysfs-const-attr-prep-v1-0-15e72dba4205@weissschuh.net> In-Reply-To: <20250116-sysfs-const-attr-prep-v1-0-15e72dba4205@weissschuh.net> To: Greg Kroah-Hartman , "Rafael J. Wysocki" Cc: linux-kernel@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1737048749; l=1331; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=Zix9Nw+VLZajXUl2F/cXtQdh9/+HCkVae3P72cvfJVg=; b=EwU337FuW04rsSeyHyDZ9T2p/fkr5AM275x4iXo3GTiejPB/GelVYF16RQ8h6hSeJXZAg2vrF ivQio4xPpnuDOzXdlOvKCCag6Ha6hZaIjcRWrNesdN/6pXLEBAgizfM X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= There was no example for the is_visible() callback so far. It will also become an example and test for the constification of 'struct attribute' later. Signed-off-by: Thomas Wei=C3=9Fschuh --- samples/kobject/kset-example.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/samples/kobject/kset-example.c b/samples/kobject/kset-example.c index 552d7e363539a8f82ab8e0446c732f85ed2c5612..7d8c68763ff8af696cbafaed1d5= f1b173bb4860e 100644 --- a/samples/kobject/kset-example.c +++ b/samples/kobject/kset-example.c @@ -178,7 +178,22 @@ static struct attribute *foo_default_attrs[] =3D { &bar_attribute.attr, NULL, /* need to NULL terminate the list of attributes */ }; -ATTRIBUTE_GROUPS(foo_default); + +static umode_t foo_default_attrs_is_visible(struct kobject *kobj, + struct attribute *attr, + int n) +{ + /* Hide attributes with the same name as the kobject. */ + if (strcmp(kobject_name(kobj), attr->name) =3D=3D 0) + return 0; + return attr->mode; +} + +static const struct attribute_group foo_default_group =3D { + .attrs =3D foo_default_attrs, + .is_visible =3D foo_default_attrs_is_visible, +}; +__ATTRIBUTE_GROUPS(foo_default); =20 /* * Our own ktype for our kobjects. Here we specify our sysfs ops, the --=20 2.48.1 From nobody Sun Dec 14 06:18:53 2025 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 301F722DC2B for ; Thu, 16 Jan 2025 17:32:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.69.126.157 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737048757; cv=none; b=HK9ZND9NfS7xWMx77NGJTyXs2RbbgRulL98vgwmtakmxt2RQ1zKV8+xbBNZFElAYqtrthFLHUs3087luD+AdgsRKyHD27wt0ROMe8j6/8lysazUkmugo3/NCGuiPAy6dh81T5cmEolOo7f3RvgLj3qr41Q0/7d5TZr/8ljjmgWM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737048757; c=relaxed/simple; bh=i0sjLXnG8vqYxeNMyCTWDNGEmZsA83vTN7vXSRsg/GQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=IiM6H98c9YWOQLzRTOd3OmmTosR58x2Kxm773sVSncxKJ2zlYeR0030yiR2WQVefC0mqZc6H/zb86WZNB+JIhKHBSxdytZSKntAaQ2ncrte4c7jj8O9NSHKChsRqLqMrc+eZPAiBilRfcnfjZXhjQ6ckutkzDoymKciiaNne32w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net; spf=pass smtp.mailfrom=weissschuh.net; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b=kfAZ/8vB; arc=none smtp.client-ip=159.69.126.157 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b="kfAZ/8vB" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1737048750; bh=i0sjLXnG8vqYxeNMyCTWDNGEmZsA83vTN7vXSRsg/GQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=kfAZ/8vBRbnnBtcgUhChztUMDYyp7wHPPnY9MLUbvCUyhr/XUCwZb1wb/KcgmNssB BSw4eQxvxOvVxCnBHvivMxzQeqpONTJVy6OPewpWCNS2Ahw/gerKNX3lEzp8Tgjog1 ua+bHvu2zHFVNwTH4BTaqnZITC8bHtrgOXbJL/M0= From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Date: Thu, 16 Jan 2025 18:32:32 +0100 Subject: [PATCH 6/6] samples/kobject: constify 'struct foo_attribute' Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250116-sysfs-const-attr-prep-v1-6-15e72dba4205@weissschuh.net> References: <20250116-sysfs-const-attr-prep-v1-0-15e72dba4205@weissschuh.net> In-Reply-To: <20250116-sysfs-const-attr-prep-v1-0-15e72dba4205@weissschuh.net> To: Greg Kroah-Hartman , "Rafael J. Wysocki" Cc: linux-kernel@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1737048749; l=4840; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=i0sjLXnG8vqYxeNMyCTWDNGEmZsA83vTN7vXSRsg/GQ=; b=3P/7fYRI1JgpHoaefigxCIMoDiMSyawYMi29HqzFKLNbRNoAO5Ljes+L8TvhtwRNVKRoQrmMK 3QMR5J2pI9zBobgJgCKySD0TJ0waMG/3kpSNDikmU/kl2lxX9stI2Yv X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= Showcase and test the new 'struct attribute' constification facilities. Signed-off-by: Thomas Wei=C3=9Fschuh --- samples/kobject/kset-example.c | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/samples/kobject/kset-example.c b/samples/kobject/kset-example.c index 7d8c68763ff8af696cbafaed1d5f1b173bb4860e..cbcfa628cc2801a7c4fc0d86889= 8c4cfa539825c 100644 --- a/samples/kobject/kset-example.c +++ b/samples/kobject/kset-example.c @@ -37,10 +37,11 @@ struct foo_obj { /* a custom attribute that works just for a struct foo_obj. */ struct foo_attribute { struct attribute attr; - ssize_t (*show)(struct foo_obj *foo, struct foo_attribute *attr, char *bu= f); - ssize_t (*store)(struct foo_obj *foo, struct foo_attribute *attr, const c= har *buf, size_t count); + ssize_t (*show)(struct foo_obj *foo, const struct foo_attribute *attr, ch= ar *buf); + ssize_t (*store)(struct foo_obj *foo, const struct foo_attribute *attr, + const char *buf, size_t count); }; -#define to_foo_attr(x) container_of(x, struct foo_attribute, attr) +#define to_foo_attr(x) container_of_const(x, struct foo_attribute, attr) =20 /* * The default show function that must be passed to sysfs. This will be @@ -53,7 +54,7 @@ static ssize_t foo_attr_show(struct kobject *kobj, struct attribute *attr, char *buf) { - struct foo_attribute *attribute; + const struct foo_attribute *attribute; struct foo_obj *foo; =20 attribute =3D to_foo_attr(attr); @@ -73,7 +74,7 @@ static ssize_t foo_attr_store(struct kobject *kobj, struct attribute *attr, const char *buf, size_t len) { - struct foo_attribute *attribute; + const struct foo_attribute *attribute; struct foo_obj *foo; =20 attribute =3D to_foo_attr(attr); @@ -109,13 +110,13 @@ static void foo_release(struct kobject *kobj) /* * The "foo" file where the .foo variable is read from and written to. */ -static ssize_t foo_show(struct foo_obj *foo_obj, struct foo_attribute *att= r, +static ssize_t foo_show(struct foo_obj *foo_obj, const struct foo_attribut= e *attr, char *buf) { return sysfs_emit(buf, "%d\n", foo_obj->foo); } =20 -static ssize_t foo_store(struct foo_obj *foo_obj, struct foo_attribute *at= tr, +static ssize_t foo_store(struct foo_obj *foo_obj, const struct foo_attribu= te *attr, const char *buf, size_t count) { int ret; @@ -128,14 +129,14 @@ static ssize_t foo_store(struct foo_obj *foo_obj, str= uct foo_attribute *attr, } =20 /* Sysfs attributes cannot be world-writable. */ -static struct foo_attribute foo_attribute =3D +static const struct foo_attribute foo_attribute =3D __ATTR(foo, 0664, foo_show, foo_store); =20 /* * More complex function where we determine which variable is being access= ed by * looking at the attribute for the "baz" and "bar" files. */ -static ssize_t b_show(struct foo_obj *foo_obj, struct foo_attribute *attr, +static ssize_t b_show(struct foo_obj *foo_obj, const struct foo_attribute = *attr, char *buf) { int var; @@ -147,7 +148,7 @@ static ssize_t b_show(struct foo_obj *foo_obj, struct f= oo_attribute *attr, return sysfs_emit(buf, "%d\n", var); } =20 -static ssize_t b_store(struct foo_obj *foo_obj, struct foo_attribute *attr, +static ssize_t b_store(struct foo_obj *foo_obj, const struct foo_attribute= *attr, const char *buf, size_t count) { int var, ret; @@ -163,16 +164,16 @@ static ssize_t b_store(struct foo_obj *foo_obj, struc= t foo_attribute *attr, return count; } =20 -static struct foo_attribute baz_attribute =3D +static const struct foo_attribute baz_attribute =3D __ATTR(baz, 0664, b_show, b_store); -static struct foo_attribute bar_attribute =3D +static const struct foo_attribute bar_attribute =3D __ATTR(bar, 0664, b_show, b_store); =20 /* * Create a group of attributes so that we can create and destroy them all * at once. */ -static struct attribute *foo_default_attrs[] =3D { +static const struct attribute *const foo_default_attrs[] =3D { &foo_attribute.attr, &baz_attribute.attr, &bar_attribute.attr, @@ -180,7 +181,7 @@ static struct attribute *foo_default_attrs[] =3D { }; =20 static umode_t foo_default_attrs_is_visible(struct kobject *kobj, - struct attribute *attr, + const struct attribute *attr, int n) { /* Hide attributes with the same name as the kobject. */ @@ -190,8 +191,8 @@ static umode_t foo_default_attrs_is_visible(struct kobj= ect *kobj, } =20 static const struct attribute_group foo_default_group =3D { - .attrs =3D foo_default_attrs, - .is_visible =3D foo_default_attrs_is_visible, + .attrs_new =3D foo_default_attrs, + .is_visible_new =3D foo_default_attrs_is_visible, }; __ATTRIBUTE_GROUPS(foo_default); =20 --=20 2.48.1