linux-usb.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 00/10] sysfs: constify struct bin_attribute (Part 1)
@ 2024-11-03 17:03 Thomas Weißschuh
  2024-11-03 17:03 ` [PATCH v2 01/10] sysfs: explicitly pass size to sysfs_add_bin_file_mode_ns() Thomas Weißschuh
                   ` (11 more replies)
  0 siblings, 12 replies; 30+ messages in thread
From: Thomas Weißschuh @ 2024-11-03 17:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Bjorn Helgaas,
	Srinivas Kandagatla, Davidlohr Bueso, Jonathan Cameron,
	Dave Jiang, Alison Schofield, Vishal Verma, Ira Weiny,
	Alex Deucher, Christian König, Xinhui Pan, David Airlie,
	Simona Vetter, Dennis Dalessandro, Jason Gunthorpe,
	Leon Romanovsky, Tudor Ambarus, Pratyush Yadav, Michael Walle,
	Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
	Naveen Krishna Chatradhi, Carlos Bilbao, Hans de Goede,
	Ilpo Järvinen, David E. Box, James E.J. Bottomley,
	Martin K. Petersen, Richard Henderson, Matt Turner,
	Frederic Barrat, Andrew Donnellan, Arnd Bergmann, Logan Gunthorpe,
	K. Y. Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui
  Cc: Dan Williams, linux-kernel, linux-pci, linux-cxl, amd-gfx,
	dri-devel, linux-rdma, linux-mtd, platform-driver-x86, linux-scsi,
	linux-usb, linux-alpha, linuxppc-dev, linux-hyperv,
	Thomas Weißschuh

struct bin_attribute contains a bunch of pointer members, which when
overwritten by accident or malice can lead to system instability and
security problems.
Moving the definitions of struct bin_attribute to read-only memory
makes these modifications impossible.
The same change has been performed for many other structures in the
past. (struct class, struct ctl_table...)

For the structure definitions throughout the core to be moved to
read-only memory the following steps are necessary.

1) Change all callbacks invoked from the sysfs core to only pass const
   pointers
2) Adapt the sysfs core to only work in terms of const pointers
3) Adapt the sysfs core APIs to allow const pointers
4) Change all structure definitions through the core to const

This series provides the foundation for step 1) above.
It converts some callbacks in a single step to const and provides a
foundation for those callbacks where a single step is not possible.

Patches 1-5 change the bin_attribute callbacks of 'struct
attribute_group'. The remaining ones touch 'struct bin_attribute' itself.

The techniques employed by this series can later be reused for the
same change for other sysfs attributes.

This series is intended to be merged through the driver core tree.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
Changes in v2:
- Drop RFC state
- Refuse registration of attributes with both read/read_new or
  write/write_new
- Remove don't drop llseek() callback, as it is actually used.
  Instead also migrate it to "const".
- _Generic machinery: Simplify and make more robust against misuse
- Link to v1: https://lore.kernel.org/r/20241031-sysfs-const-bin_attr-v1-0-2281afa7f055@weissschuh.net

---
Thomas Weißschuh (10):
      sysfs: explicitly pass size to sysfs_add_bin_file_mode_ns()
      sysfs: introduce callback attribute_group::bin_size
      PCI/sysfs: Calculate bin_attribute size through bin_size()
      nvmem: core: calculate bin_attribute size through bin_size()
      sysfs: treewide: constify attribute callback of bin_is_visible()
      sysfs: treewide: constify attribute callback of bin_attribute::mmap()
      sysfs: treewide: constify attribute callback of bin_attribute::llseek()
      sysfs: implement all BIN_ATTR_* macros in terms of __BIN_ATTR()
      sysfs: bin_attribute: add const read/write callback variants
      driver core: Constify attribute arguments of binary attributes

 arch/alpha/kernel/pci-sysfs.c           |  6 +--
 drivers/base/node.c                     |  4 +-
 drivers/base/topology.c                 |  4 +-
 drivers/cxl/port.c                      |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c |  2 +-
 drivers/infiniband/hw/qib/qib_sysfs.c   |  2 +-
 drivers/misc/ocxl/sysfs.c               |  2 +-
 drivers/mtd/spi-nor/sysfs.c             |  2 +-
 drivers/nvmem/core.c                    | 16 ++++--
 drivers/pci/p2pdma.c                    |  2 +-
 drivers/pci/pci-sysfs.c                 | 42 ++++++++-------
 drivers/pci/vpd.c                       |  2 +-
 drivers/platform/x86/amd/hsmp.c         |  2 +-
 drivers/platform/x86/intel/pmt/class.c  |  2 +-
 drivers/platform/x86/intel/sdsi.c       |  2 +-
 drivers/scsi/scsi_sysfs.c               |  2 +-
 drivers/uio/uio_hv_generic.c            |  2 +-
 drivers/usb/core/sysfs.c                |  2 +-
 fs/sysfs/file.c                         | 30 +++++++----
 fs/sysfs/group.c                        |  5 +-
 fs/sysfs/sysfs.h                        |  2 +-
 include/linux/sysfs.h                   | 94 ++++++++++++++++++++-------------
 22 files changed, 138 insertions(+), 91 deletions(-)
---
base-commit: 3e5e6c9900c3d71895e8bdeacfb579462e98eba1
change-id: 20241028-sysfs-const-bin_attr-a00896481d0b

Best regards,
-- 
Thomas Weißschuh <linux@weissschuh.net>


^ permalink raw reply	[flat|nested] 30+ messages in thread

* [PATCH v2 01/10] sysfs: explicitly pass size to sysfs_add_bin_file_mode_ns()
  2024-11-03 17:03 [PATCH v2 00/10] sysfs: constify struct bin_attribute (Part 1) Thomas Weißschuh
@ 2024-11-03 17:03 ` Thomas Weißschuh
  2024-11-03 17:03 ` [PATCH v2 02/10] sysfs: introduce callback attribute_group::bin_size Thomas Weißschuh
                   ` (10 subsequent siblings)
  11 siblings, 0 replies; 30+ messages in thread
From: Thomas Weißschuh @ 2024-11-03 17:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Bjorn Helgaas,
	Srinivas Kandagatla, Davidlohr Bueso, Jonathan Cameron,
	Dave Jiang, Alison Schofield, Vishal Verma, Ira Weiny,
	Alex Deucher, Christian König, Xinhui Pan, David Airlie,
	Simona Vetter, Dennis Dalessandro, Jason Gunthorpe,
	Leon Romanovsky, Tudor Ambarus, Pratyush Yadav, Michael Walle,
	Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
	Naveen Krishna Chatradhi, Carlos Bilbao, Hans de Goede,
	Ilpo Järvinen, David E. Box, James E.J. Bottomley,
	Martin K. Petersen, Richard Henderson, Matt Turner,
	Frederic Barrat, Andrew Donnellan, Arnd Bergmann, Logan Gunthorpe,
	K. Y. Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui
  Cc: Dan Williams, linux-kernel, linux-pci, linux-cxl, amd-gfx,
	dri-devel, linux-rdma, linux-mtd, platform-driver-x86, linux-scsi,
	linux-usb, linux-alpha, linuxppc-dev, linux-hyperv,
	Thomas Weißschuh

Upcoming changes to the sysfs core require the size of the created file
to be overridable by the caller.
Add a parameter to enable this.
For now keep using attr->size in all cases.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
 fs/sysfs/file.c  | 8 ++++----
 fs/sysfs/group.c | 3 ++-
 fs/sysfs/sysfs.h | 2 +-
 3 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/fs/sysfs/file.c b/fs/sysfs/file.c
index d1995e2d6c943a644ff9f34cf2488864d57daf81..6d39696b43069010b0ad0bdaadcf9002cb70c92c 100644
--- a/fs/sysfs/file.c
+++ b/fs/sysfs/file.c
@@ -315,7 +315,7 @@ int sysfs_add_file_mode_ns(struct kernfs_node *parent,
 }
 
 int sysfs_add_bin_file_mode_ns(struct kernfs_node *parent,
-		const struct bin_attribute *battr, umode_t mode,
+		const struct bin_attribute *battr, umode_t mode, size_t size,
 		kuid_t uid, kgid_t gid, const void *ns)
 {
 	const struct attribute *attr = &battr->attr;
@@ -340,7 +340,7 @@ int sysfs_add_bin_file_mode_ns(struct kernfs_node *parent,
 #endif
 
 	kn = __kernfs_create_file(parent, attr->name, mode & 0777, uid, gid,
-				  battr->size, ops, (void *)attr, ns, key);
+				  size, ops, (void *)attr, ns, key);
 	if (IS_ERR(kn)) {
 		if (PTR_ERR(kn) == -EEXIST)
 			sysfs_warn_dup(parent, attr->name);
@@ -580,8 +580,8 @@ int sysfs_create_bin_file(struct kobject *kobj,
 		return -EINVAL;
 
 	kobject_get_ownership(kobj, &uid, &gid);
-	return sysfs_add_bin_file_mode_ns(kobj->sd, attr, attr->attr.mode, uid,
-					   gid, NULL);
+	return sysfs_add_bin_file_mode_ns(kobj->sd, attr, attr->attr.mode,
+					  attr->size, uid, gid, NULL);
 }
 EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
 
diff --git a/fs/sysfs/group.c b/fs/sysfs/group.c
index d22ad67a0f3291f4702f494939528d5d13c31fae..45b2e92941da1f49dcc71af3781317c61480c956 100644
--- a/fs/sysfs/group.c
+++ b/fs/sysfs/group.c
@@ -87,6 +87,7 @@ static int create_files(struct kernfs_node *parent, struct kobject *kobj,
 	if (grp->bin_attrs) {
 		for (i = 0, bin_attr = grp->bin_attrs; *bin_attr; i++, bin_attr++) {
 			umode_t mode = (*bin_attr)->attr.mode;
+			size_t size = (*bin_attr)->size;
 
 			if (update)
 				kernfs_remove_by_name(parent,
@@ -104,7 +105,7 @@ static int create_files(struct kernfs_node *parent, struct kobject *kobj,
 
 			mode &= SYSFS_PREALLOC | 0664;
 			error = sysfs_add_bin_file_mode_ns(parent, *bin_attr,
-							   mode, uid, gid,
+							   mode, size, uid, gid,
 							   NULL);
 			if (error)
 				break;
diff --git a/fs/sysfs/sysfs.h b/fs/sysfs/sysfs.h
index 3f28c9af57562f61a00a47935579f0939cbfd4dc..8e012f25e1c06e802c3138cc2715b46c1f67fa48 100644
--- a/fs/sysfs/sysfs.h
+++ b/fs/sysfs/sysfs.h
@@ -31,7 +31,7 @@ int sysfs_add_file_mode_ns(struct kernfs_node *parent,
 		const struct attribute *attr, umode_t amode, kuid_t uid,
 		kgid_t gid, const void *ns);
 int sysfs_add_bin_file_mode_ns(struct kernfs_node *parent,
-		const struct bin_attribute *battr, umode_t mode,
+		const struct bin_attribute *battr, umode_t mode, size_t size,
 		kuid_t uid, kgid_t gid, const void *ns);
 
 /*

-- 
2.47.0


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v2 02/10] sysfs: introduce callback attribute_group::bin_size
  2024-11-03 17:03 [PATCH v2 00/10] sysfs: constify struct bin_attribute (Part 1) Thomas Weißschuh
  2024-11-03 17:03 ` [PATCH v2 01/10] sysfs: explicitly pass size to sysfs_add_bin_file_mode_ns() Thomas Weißschuh
@ 2024-11-03 17:03 ` Thomas Weißschuh
  2024-11-05 16:12   ` Bjorn Helgaas
                     ` (2 more replies)
  2024-11-03 17:03 ` [PATCH v2 03/10] PCI/sysfs: Calculate bin_attribute size through bin_size() Thomas Weißschuh
                   ` (9 subsequent siblings)
  11 siblings, 3 replies; 30+ messages in thread
From: Thomas Weißschuh @ 2024-11-03 17:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Bjorn Helgaas,
	Srinivas Kandagatla, Davidlohr Bueso, Jonathan Cameron,
	Dave Jiang, Alison Schofield, Vishal Verma, Ira Weiny,
	Alex Deucher, Christian König, Xinhui Pan, David Airlie,
	Simona Vetter, Dennis Dalessandro, Jason Gunthorpe,
	Leon Romanovsky, Tudor Ambarus, Pratyush Yadav, Michael Walle,
	Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
	Naveen Krishna Chatradhi, Carlos Bilbao, Hans de Goede,
	Ilpo Järvinen, David E. Box, James E.J. Bottomley,
	Martin K. Petersen, Richard Henderson, Matt Turner,
	Frederic Barrat, Andrew Donnellan, Arnd Bergmann, Logan Gunthorpe,
	K. Y. Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui
  Cc: Dan Williams, linux-kernel, linux-pci, linux-cxl, amd-gfx,
	dri-devel, linux-rdma, linux-mtd, platform-driver-x86, linux-scsi,
	linux-usb, linux-alpha, linuxppc-dev, linux-hyperv,
	Thomas Weißschuh

Several drivers need to dynamically calculate the size of an binary
attribute. Currently this is done by assigning attr->size from the
is_bin_visible() callback.

This has drawbacks:
* It is not documented.
* A single attribute can be instantiated multiple times, overwriting the
  shared size field.
* It prevents the structure to be moved to read-only memory.

Introduce a new dedicated callback to calculate the size of the
attribute.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
 fs/sysfs/group.c      | 2 ++
 include/linux/sysfs.h | 8 ++++++++
 2 files changed, 10 insertions(+)

diff --git a/fs/sysfs/group.c b/fs/sysfs/group.c
index 45b2e92941da1f49dcc71af3781317c61480c956..8b01a7eda5fb3239e138372417d01967c7a3f122 100644
--- a/fs/sysfs/group.c
+++ b/fs/sysfs/group.c
@@ -98,6 +98,8 @@ static int create_files(struct kernfs_node *parent, struct kobject *kobj,
 				if (!mode)
 					continue;
 			}
+			if (grp->bin_size)
+				size = grp->bin_size(kobj, *bin_attr, i);
 
 			WARN(mode & ~(SYSFS_PREALLOC | 0664),
 			     "Attribute %s: Invalid permissions 0%o\n",
diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h
index c4e64dc112063f7cb89bf66059d0338716089e87..4746cccb95898b24df6f53de9421ea7649b5568f 100644
--- a/include/linux/sysfs.h
+++ b/include/linux/sysfs.h
@@ -87,6 +87,11 @@ do {							\
  *		SYSFS_GROUP_VISIBLE() when assigning this callback to
  *		specify separate _group_visible() and _attr_visible()
  *		handlers.
+ * @bin_size:
+ *		Optional: Function to return the size of a binary attribute
+ *		of the group. Will be called repeatedly for each binary
+ *		attribute in the group. Overwrites the size field embedded
+ *		inside the attribute itself.
  * @attrs:	Pointer to NULL terminated list of attributes.
  * @bin_attrs:	Pointer to NULL terminated list of binary attributes.
  *		Either attrs or bin_attrs or both must be provided.
@@ -97,6 +102,9 @@ struct attribute_group {
 					      struct attribute *, int);
 	umode_t			(*is_bin_visible)(struct kobject *,
 						  struct bin_attribute *, int);
+	size_t			(*bin_size)(struct kobject *,
+					    const struct bin_attribute *,
+					    int);
 	struct attribute	**attrs;
 	struct bin_attribute	**bin_attrs;
 };

-- 
2.47.0


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v2 03/10] PCI/sysfs: Calculate bin_attribute size through bin_size()
  2024-11-03 17:03 [PATCH v2 00/10] sysfs: constify struct bin_attribute (Part 1) Thomas Weißschuh
  2024-11-03 17:03 ` [PATCH v2 01/10] sysfs: explicitly pass size to sysfs_add_bin_file_mode_ns() Thomas Weißschuh
  2024-11-03 17:03 ` [PATCH v2 02/10] sysfs: introduce callback attribute_group::bin_size Thomas Weißschuh
@ 2024-11-03 17:03 ` Thomas Weißschuh
  2024-11-07 16:27   ` Bjorn Helgaas
  2024-11-03 17:03 ` [PATCH v2 04/10] nvmem: core: calculate " Thomas Weißschuh
                   ` (8 subsequent siblings)
  11 siblings, 1 reply; 30+ messages in thread
From: Thomas Weißschuh @ 2024-11-03 17:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Bjorn Helgaas,
	Srinivas Kandagatla, Davidlohr Bueso, Jonathan Cameron,
	Dave Jiang, Alison Schofield, Vishal Verma, Ira Weiny,
	Alex Deucher, Christian König, Xinhui Pan, David Airlie,
	Simona Vetter, Dennis Dalessandro, Jason Gunthorpe,
	Leon Romanovsky, Tudor Ambarus, Pratyush Yadav, Michael Walle,
	Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
	Naveen Krishna Chatradhi, Carlos Bilbao, Hans de Goede,
	Ilpo Järvinen, David E. Box, James E.J. Bottomley,
	Martin K. Petersen, Richard Henderson, Matt Turner,
	Frederic Barrat, Andrew Donnellan, Arnd Bergmann, Logan Gunthorpe,
	K. Y. Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui
  Cc: Dan Williams, linux-kernel, linux-pci, linux-cxl, amd-gfx,
	dri-devel, linux-rdma, linux-mtd, platform-driver-x86, linux-scsi,
	linux-usb, linux-alpha, linuxppc-dev, linux-hyperv,
	Thomas Weißschuh

Stop abusing the is_bin_visible() callback to calculate the attribute
size. Instead use the new, dedicated bin_size() one.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
 drivers/pci/pci-sysfs.c | 28 ++++++++++++++++------------
 1 file changed, 16 insertions(+), 12 deletions(-)

diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
index 5d0f4db1cab78674c5e5906f321bf7a57b742983..040f01b2b999175e8d98b05851edc078bbabbe0d 100644
--- a/drivers/pci/pci-sysfs.c
+++ b/drivers/pci/pci-sysfs.c
@@ -818,21 +818,20 @@ static struct bin_attribute *pci_dev_config_attrs[] = {
 	NULL,
 };
 
-static umode_t pci_dev_config_attr_is_visible(struct kobject *kobj,
-					      struct bin_attribute *a, int n)
+static size_t pci_dev_config_attr_bin_size(struct kobject *kobj,
+					   const struct bin_attribute *a,
+					   int n)
 {
 	struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
 
-	a->size = PCI_CFG_SPACE_SIZE;
 	if (pdev->cfg_size > PCI_CFG_SPACE_SIZE)
-		a->size = PCI_CFG_SPACE_EXP_SIZE;
-
-	return a->attr.mode;
+		return PCI_CFG_SPACE_EXP_SIZE;
+	return PCI_CFG_SPACE_SIZE;
 }
 
 static const struct attribute_group pci_dev_config_attr_group = {
 	.bin_attrs = pci_dev_config_attrs,
-	.is_bin_visible = pci_dev_config_attr_is_visible,
+	.bin_size = pci_dev_config_attr_bin_size,
 };
 
 /*
@@ -1330,21 +1329,26 @@ static umode_t pci_dev_rom_attr_is_visible(struct kobject *kobj,
 					   struct bin_attribute *a, int n)
 {
 	struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
-	size_t rom_size;
 
 	/* If the device has a ROM, try to expose it in sysfs. */
-	rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
-	if (!rom_size)
+	if (!pci_resource_end(pdev, PCI_ROM_RESOURCE))
 		return 0;
 
-	a->size = rom_size;
-
 	return a->attr.mode;
 }
 
+static size_t pci_dev_rom_attr_bin_size(struct kobject *kobj,
+					const struct bin_attribute *a, int n)
+{
+	struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
+
+	return pci_resource_len(pdev, PCI_ROM_RESOURCE);
+}
+
 static const struct attribute_group pci_dev_rom_attr_group = {
 	.bin_attrs = pci_dev_rom_attrs,
 	.is_bin_visible = pci_dev_rom_attr_is_visible,
+	.bin_size = pci_dev_rom_attr_bin_size,
 };
 
 static ssize_t reset_store(struct device *dev, struct device_attribute *attr,

-- 
2.47.0


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v2 04/10] nvmem: core: calculate bin_attribute size through bin_size()
  2024-11-03 17:03 [PATCH v2 00/10] sysfs: constify struct bin_attribute (Part 1) Thomas Weißschuh
                   ` (2 preceding siblings ...)
  2024-11-03 17:03 ` [PATCH v2 03/10] PCI/sysfs: Calculate bin_attribute size through bin_size() Thomas Weißschuh
@ 2024-11-03 17:03 ` Thomas Weißschuh
  2024-11-08  9:58   ` Srinivas Kandagatla
  2024-11-03 17:03 ` [PATCH v2 05/10] sysfs: treewide: constify attribute callback of bin_is_visible() Thomas Weißschuh
                   ` (7 subsequent siblings)
  11 siblings, 1 reply; 30+ messages in thread
From: Thomas Weißschuh @ 2024-11-03 17:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Bjorn Helgaas,
	Srinivas Kandagatla, Davidlohr Bueso, Jonathan Cameron,
	Dave Jiang, Alison Schofield, Vishal Verma, Ira Weiny,
	Alex Deucher, Christian König, Xinhui Pan, David Airlie,
	Simona Vetter, Dennis Dalessandro, Jason Gunthorpe,
	Leon Romanovsky, Tudor Ambarus, Pratyush Yadav, Michael Walle,
	Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
	Naveen Krishna Chatradhi, Carlos Bilbao, Hans de Goede,
	Ilpo Järvinen, David E. Box, James E.J. Bottomley,
	Martin K. Petersen, Richard Henderson, Matt Turner,
	Frederic Barrat, Andrew Donnellan, Arnd Bergmann, Logan Gunthorpe,
	K. Y. Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui
  Cc: Dan Williams, linux-kernel, linux-pci, linux-cxl, amd-gfx,
	dri-devel, linux-rdma, linux-mtd, platform-driver-x86, linux-scsi,
	linux-usb, linux-alpha, linuxppc-dev, linux-hyperv,
	Thomas Weißschuh

Stop abusing the is_bin_visible() callback to calculate the attribute
size. Instead use the new, dedicated bin_size() one.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
 drivers/nvmem/core.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 33ffa2aa4c1152398ec66b8dd7b30384c5346a6e..63370c76394ee9b8d514da074779617cef67c311 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -303,11 +303,19 @@ static umode_t nvmem_bin_attr_is_visible(struct kobject *kobj,
 	struct device *dev = kobj_to_dev(kobj);
 	struct nvmem_device *nvmem = to_nvmem_device(dev);
 
-	attr->size = nvmem->size;
-
 	return nvmem_bin_attr_get_umode(nvmem);
 }
 
+static size_t nvmem_bin_attr_size(struct kobject *kobj,
+				  const struct bin_attribute *attr,
+				  int i)
+{
+	struct device *dev = kobj_to_dev(kobj);
+	struct nvmem_device *nvmem = to_nvmem_device(dev);
+
+	return nvmem->size;
+}
+
 static umode_t nvmem_attr_is_visible(struct kobject *kobj,
 				     struct attribute *attr, int i)
 {
@@ -383,6 +391,7 @@ static const struct attribute_group nvmem_bin_group = {
 	.bin_attrs	= nvmem_bin_attributes,
 	.attrs		= nvmem_attrs,
 	.is_bin_visible = nvmem_bin_attr_is_visible,
+	.bin_size	= nvmem_bin_attr_size,
 	.is_visible	= nvmem_attr_is_visible,
 };
 

-- 
2.47.0


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v2 05/10] sysfs: treewide: constify attribute callback of bin_is_visible()
  2024-11-03 17:03 [PATCH v2 00/10] sysfs: constify struct bin_attribute (Part 1) Thomas Weißschuh
                   ` (3 preceding siblings ...)
  2024-11-03 17:03 ` [PATCH v2 04/10] nvmem: core: calculate " Thomas Weißschuh
@ 2024-11-03 17:03 ` Thomas Weißschuh
  2024-11-04 13:25   ` Ilpo Järvinen
                     ` (6 more replies)
  2024-11-03 17:03 ` [PATCH v2 06/10] sysfs: treewide: constify attribute callback of bin_attribute::mmap() Thomas Weißschuh
                   ` (6 subsequent siblings)
  11 siblings, 7 replies; 30+ messages in thread
From: Thomas Weißschuh @ 2024-11-03 17:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Bjorn Helgaas,
	Srinivas Kandagatla, Davidlohr Bueso, Jonathan Cameron,
	Dave Jiang, Alison Schofield, Vishal Verma, Ira Weiny,
	Alex Deucher, Christian König, Xinhui Pan, David Airlie,
	Simona Vetter, Dennis Dalessandro, Jason Gunthorpe,
	Leon Romanovsky, Tudor Ambarus, Pratyush Yadav, Michael Walle,
	Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
	Naveen Krishna Chatradhi, Carlos Bilbao, Hans de Goede,
	Ilpo Järvinen, David E. Box, James E.J. Bottomley,
	Martin K. Petersen, Richard Henderson, Matt Turner,
	Frederic Barrat, Andrew Donnellan, Arnd Bergmann, Logan Gunthorpe,
	K. Y. Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui
  Cc: Dan Williams, linux-kernel, linux-pci, linux-cxl, amd-gfx,
	dri-devel, linux-rdma, linux-mtd, platform-driver-x86, linux-scsi,
	linux-usb, linux-alpha, linuxppc-dev, linux-hyperv,
	Thomas Weißschuh

The is_bin_visible() callbacks should not modify the struct
bin_attribute passed as argument.
Enforce this by marking the argument as const.

As there are not many callback implementers perform this change
throughout the tree at once.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
 drivers/cxl/port.c                      |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c |  2 +-
 drivers/infiniband/hw/qib/qib_sysfs.c   |  2 +-
 drivers/mtd/spi-nor/sysfs.c             |  2 +-
 drivers/nvmem/core.c                    |  3 ++-
 drivers/pci/pci-sysfs.c                 |  2 +-
 drivers/pci/vpd.c                       |  2 +-
 drivers/platform/x86/amd/hsmp.c         |  2 +-
 drivers/platform/x86/intel/sdsi.c       |  2 +-
 drivers/scsi/scsi_sysfs.c               |  2 +-
 drivers/usb/core/sysfs.c                |  2 +-
 include/linux/sysfs.h                   | 30 +++++++++++++++---------------
 12 files changed, 27 insertions(+), 26 deletions(-)

diff --git a/drivers/cxl/port.c b/drivers/cxl/port.c
index 9dc394295e1fcd1610813837b2f515b66995eb25..24041cf85cfbe6c54c467ac325e48c775562b938 100644
--- a/drivers/cxl/port.c
+++ b/drivers/cxl/port.c
@@ -173,7 +173,7 @@ static ssize_t CDAT_read(struct file *filp, struct kobject *kobj,
 static BIN_ATTR_ADMIN_RO(CDAT, 0);
 
 static umode_t cxl_port_bin_attr_is_visible(struct kobject *kobj,
-					    struct bin_attribute *attr, int i)
+					    const struct bin_attribute *attr, int i)
 {
 	struct device *dev = kobj_to_dev(kobj);
 	struct cxl_port *port = to_cxl_port(dev);
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c
index 0b28b2cf1517d130da01989df70b9dff6433edc4..c1c329eb920b52af100a93bdf00df450e25608c4 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c
@@ -3999,7 +3999,7 @@ static umode_t amdgpu_flash_attr_is_visible(struct kobject *kobj, struct attribu
 }
 
 static umode_t amdgpu_bin_flash_attr_is_visible(struct kobject *kobj,
-						struct bin_attribute *attr,
+						const struct bin_attribute *attr,
 						int idx)
 {
 	struct device *dev = kobj_to_dev(kobj);
diff --git a/drivers/infiniband/hw/qib/qib_sysfs.c b/drivers/infiniband/hw/qib/qib_sysfs.c
index 53ec7510e4ebfb144e79884ca7dd7d0c873bd8a7..ba2cd68b53e6c240f1afc65c64012c75ccf488e0 100644
--- a/drivers/infiniband/hw/qib/qib_sysfs.c
+++ b/drivers/infiniband/hw/qib/qib_sysfs.c
@@ -283,7 +283,7 @@ static struct bin_attribute *port_ccmgta_attributes[] = {
 };
 
 static umode_t qib_ccmgta_is_bin_visible(struct kobject *kobj,
-				 struct bin_attribute *attr, int n)
+				 const struct bin_attribute *attr, int n)
 {
 	struct qib_pportdata *ppd = qib_get_pportdata_kobj(kobj);
 
diff --git a/drivers/mtd/spi-nor/sysfs.c b/drivers/mtd/spi-nor/sysfs.c
index 96064e4babf01f6950c81586764386e7671cbf97..5e9eb268073d18e0a46089000f18a3200b4bf13d 100644
--- a/drivers/mtd/spi-nor/sysfs.c
+++ b/drivers/mtd/spi-nor/sysfs.c
@@ -87,7 +87,7 @@ static umode_t spi_nor_sysfs_is_visible(struct kobject *kobj,
 }
 
 static umode_t spi_nor_sysfs_is_bin_visible(struct kobject *kobj,
-					    struct bin_attribute *attr, int n)
+					    const struct bin_attribute *attr, int n)
 {
 	struct spi_device *spi = to_spi_device(kobj_to_dev(kobj));
 	struct spi_mem *spimem = spi_get_drvdata(spi);
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 63370c76394ee9b8d514da074779617cef67c311..73e44d724f90f4cd8fe8cafb9fa0c0fb23078e61 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -298,7 +298,8 @@ static umode_t nvmem_bin_attr_get_umode(struct nvmem_device *nvmem)
 }
 
 static umode_t nvmem_bin_attr_is_visible(struct kobject *kobj,
-					 struct bin_attribute *attr, int i)
+					 const struct bin_attribute *attr,
+					 int i)
 {
 	struct device *dev = kobj_to_dev(kobj);
 	struct nvmem_device *nvmem = to_nvmem_device(dev);
diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
index 040f01b2b999175e8d98b05851edc078bbabbe0d..13912940ed2bb66c0086e5bea9a3cb6417ac14dd 100644
--- a/drivers/pci/pci-sysfs.c
+++ b/drivers/pci/pci-sysfs.c
@@ -1326,7 +1326,7 @@ static struct bin_attribute *pci_dev_rom_attrs[] = {
 };
 
 static umode_t pci_dev_rom_attr_is_visible(struct kobject *kobj,
-					   struct bin_attribute *a, int n)
+					   const struct bin_attribute *a, int n)
 {
 	struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
 
diff --git a/drivers/pci/vpd.c b/drivers/pci/vpd.c
index e4300f5f304f3ca55a657fd25a1fa5ed919737a7..a469bcbc0da7f7677485c7f999f8dfb58b8ae8a3 100644
--- a/drivers/pci/vpd.c
+++ b/drivers/pci/vpd.c
@@ -325,7 +325,7 @@ static struct bin_attribute *vpd_attrs[] = {
 };
 
 static umode_t vpd_attr_is_visible(struct kobject *kobj,
-				   struct bin_attribute *a, int n)
+				   const struct bin_attribute *a, int n)
 {
 	struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
 
diff --git a/drivers/platform/x86/amd/hsmp.c b/drivers/platform/x86/amd/hsmp.c
index 8fcf38eed7f00ee01aade6e3e55e20402458d5aa..8f00850c139fa8d419bc1c140c1832bf84b2c3bd 100644
--- a/drivers/platform/x86/amd/hsmp.c
+++ b/drivers/platform/x86/amd/hsmp.c
@@ -620,7 +620,7 @@ static int hsmp_get_tbl_dram_base(u16 sock_ind)
 }
 
 static umode_t hsmp_is_sock_attr_visible(struct kobject *kobj,
-					 struct bin_attribute *battr, int id)
+					 const struct bin_attribute *battr, int id)
 {
 	if (plat_dev.proto_ver == HSMP_PROTO_VER6)
 		return battr->attr.mode;
diff --git a/drivers/platform/x86/intel/sdsi.c b/drivers/platform/x86/intel/sdsi.c
index 9d137621f0e6e7a23be0e0bbc6175c51c403169f..33f33b1070fdc949c1373251c3bca4234d9da119 100644
--- a/drivers/platform/x86/intel/sdsi.c
+++ b/drivers/platform/x86/intel/sdsi.c
@@ -541,7 +541,7 @@ static struct bin_attribute *sdsi_bin_attrs[] = {
 };
 
 static umode_t
-sdsi_battr_is_visible(struct kobject *kobj, struct bin_attribute *attr, int n)
+sdsi_battr_is_visible(struct kobject *kobj, const struct bin_attribute *attr, int n)
 {
 	struct device *dev = kobj_to_dev(kobj);
 	struct sdsi_priv *priv = dev_get_drvdata(dev);
diff --git a/drivers/scsi/scsi_sysfs.c b/drivers/scsi/scsi_sysfs.c
index 32f94db6d6bf5d2bd289c1a121da7ffc6a7cb2ff..f3a1ecb42128a2b221ca5c362e041eb59dba0f20 100644
--- a/drivers/scsi/scsi_sysfs.c
+++ b/drivers/scsi/scsi_sysfs.c
@@ -1274,7 +1274,7 @@ static umode_t scsi_sdev_attr_is_visible(struct kobject *kobj,
 }
 
 static umode_t scsi_sdev_bin_attr_is_visible(struct kobject *kobj,
-					     struct bin_attribute *attr, int i)
+					     const struct bin_attribute *attr, int i)
 {
 	struct device *dev = kobj_to_dev(kobj);
 	struct scsi_device *sdev = to_scsi_device(dev);
diff --git a/drivers/usb/core/sysfs.c b/drivers/usb/core/sysfs.c
index 61b6d978892c799e213018bed22d9fb12a19d429..b4cba23831acd2d7d395b9f7683cd3ee3a8623c8 100644
--- a/drivers/usb/core/sysfs.c
+++ b/drivers/usb/core/sysfs.c
@@ -925,7 +925,7 @@ static struct bin_attribute *dev_bin_attrs[] = {
 };
 
 static umode_t dev_bin_attrs_are_visible(struct kobject *kobj,
-		struct bin_attribute *a, int n)
+		const struct bin_attribute *a, int n)
 {
 	struct device *dev = kobj_to_dev(kobj);
 	struct usb_device *udev = to_usb_device(dev);
diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h
index 4746cccb95898b24df6f53de9421ea7649b5568f..d1b22d56198b55ee39fe4c4fc994f5b753641992 100644
--- a/include/linux/sysfs.h
+++ b/include/linux/sysfs.h
@@ -101,7 +101,7 @@ struct attribute_group {
 	umode_t			(*is_visible)(struct kobject *,
 					      struct attribute *, int);
 	umode_t			(*is_bin_visible)(struct kobject *,
-						  struct bin_attribute *, int);
+						  const struct bin_attribute *, int);
 	size_t			(*bin_size)(struct kobject *,
 					    const struct bin_attribute *,
 					    int);
@@ -199,22 +199,22 @@ struct attribute_group {
  * attributes, the group visibility is determined by the function
  * specified to is_visible() not is_bin_visible()
  */
-#define DEFINE_SYSFS_BIN_GROUP_VISIBLE(name)                             \
-	static inline umode_t sysfs_group_visible_##name(                \
-		struct kobject *kobj, struct bin_attribute *attr, int n) \
-	{                                                                \
-		if (n == 0 && !name##_group_visible(kobj))               \
-			return SYSFS_GROUP_INVISIBLE;                    \
-		return name##_attr_visible(kobj, attr, n);               \
+#define DEFINE_SYSFS_BIN_GROUP_VISIBLE(name)                                   \
+	static inline umode_t sysfs_group_visible_##name(                      \
+		struct kobject *kobj, const struct bin_attribute *attr, int n) \
+	{                                                                      \
+		if (n == 0 && !name##_group_visible(kobj))                     \
+			return SYSFS_GROUP_INVISIBLE;                          \
+		return name##_attr_visible(kobj, attr, n);                     \
 	}
 
-#define DEFINE_SIMPLE_SYSFS_BIN_GROUP_VISIBLE(name)                   \
-	static inline umode_t sysfs_group_visible_##name(             \
-		struct kobject *kobj, struct bin_attribute *a, int n) \
-	{                                                             \
-		if (n == 0 && !name##_group_visible(kobj))            \
-			return SYSFS_GROUP_INVISIBLE;                 \
-		return a->mode;                                       \
+#define DEFINE_SIMPLE_SYSFS_BIN_GROUP_VISIBLE(name)                         \
+	static inline umode_t sysfs_group_visible_##name(                   \
+		struct kobject *kobj, const struct bin_attribute *a, int n) \
+	{                                                                   \
+		if (n == 0 && !name##_group_visible(kobj))                  \
+			return SYSFS_GROUP_INVISIBLE;                       \
+		return a->mode;                                             \
 	}
 
 #define SYSFS_GROUP_VISIBLE(fn) sysfs_group_visible_##fn

-- 
2.47.0


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v2 06/10] sysfs: treewide: constify attribute callback of bin_attribute::mmap()
  2024-11-03 17:03 [PATCH v2 00/10] sysfs: constify struct bin_attribute (Part 1) Thomas Weißschuh
                   ` (4 preceding siblings ...)
  2024-11-03 17:03 ` [PATCH v2 05/10] sysfs: treewide: constify attribute callback of bin_is_visible() Thomas Weißschuh
@ 2024-11-03 17:03 ` Thomas Weißschuh
  2024-11-04  1:24   ` Andrew Donnellan
  2024-11-03 17:03 ` [PATCH v2 07/10] sysfs: treewide: constify attribute callback of bin_attribute::llseek() Thomas Weißschuh
                   ` (5 subsequent siblings)
  11 siblings, 1 reply; 30+ messages in thread
From: Thomas Weißschuh @ 2024-11-03 17:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Bjorn Helgaas,
	Srinivas Kandagatla, Davidlohr Bueso, Jonathan Cameron,
	Dave Jiang, Alison Schofield, Vishal Verma, Ira Weiny,
	Alex Deucher, Christian König, Xinhui Pan, David Airlie,
	Simona Vetter, Dennis Dalessandro, Jason Gunthorpe,
	Leon Romanovsky, Tudor Ambarus, Pratyush Yadav, Michael Walle,
	Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
	Naveen Krishna Chatradhi, Carlos Bilbao, Hans de Goede,
	Ilpo Järvinen, David E. Box, James E.J. Bottomley,
	Martin K. Petersen, Richard Henderson, Matt Turner,
	Frederic Barrat, Andrew Donnellan, Arnd Bergmann, Logan Gunthorpe,
	K. Y. Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui
  Cc: Dan Williams, linux-kernel, linux-pci, linux-cxl, amd-gfx,
	dri-devel, linux-rdma, linux-mtd, platform-driver-x86, linux-scsi,
	linux-usb, linux-alpha, linuxppc-dev, linux-hyperv,
	Thomas Weißschuh

The mmap() callbacks should not modify the struct
bin_attribute passed as argument.
Enforce this by marking the argument as const.

As there are not many callback implementers perform this change
throughout the tree at once.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
 arch/alpha/kernel/pci-sysfs.c          |  6 +++---
 drivers/misc/ocxl/sysfs.c              |  2 +-
 drivers/pci/p2pdma.c                   |  2 +-
 drivers/pci/pci-sysfs.c                | 10 +++++-----
 drivers/platform/x86/intel/pmt/class.c |  2 +-
 drivers/uio/uio_hv_generic.c           |  2 +-
 include/linux/sysfs.h                  |  2 +-
 7 files changed, 13 insertions(+), 13 deletions(-)

diff --git a/arch/alpha/kernel/pci-sysfs.c b/arch/alpha/kernel/pci-sysfs.c
index 5808a66e2a81f7eba9a245fd6a343406a1ade87d..3048758304b57afa01ddeb6558c39bdb48c9a3f6 100644
--- a/arch/alpha/kernel/pci-sysfs.c
+++ b/arch/alpha/kernel/pci-sysfs.c
@@ -64,7 +64,7 @@ static int __pci_mmap_fits(struct pci_dev *pdev, int num,
  * Return: %0 on success, negative error code otherwise
  */
 static int pci_mmap_resource(struct kobject *kobj,
-			     struct bin_attribute *attr,
+			     const struct bin_attribute *attr,
 			     struct vm_area_struct *vma, int sparse)
 {
 	struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
@@ -93,14 +93,14 @@ static int pci_mmap_resource(struct kobject *kobj,
 }
 
 static int pci_mmap_resource_sparse(struct file *filp, struct kobject *kobj,
-				    struct bin_attribute *attr,
+				    const struct bin_attribute *attr,
 				    struct vm_area_struct *vma)
 {
 	return pci_mmap_resource(kobj, attr, vma, 1);
 }
 
 static int pci_mmap_resource_dense(struct file *filp, struct kobject *kobj,
-				   struct bin_attribute *attr,
+				   const struct bin_attribute *attr,
 				   struct vm_area_struct *vma)
 {
 	return pci_mmap_resource(kobj, attr, vma, 0);
diff --git a/drivers/misc/ocxl/sysfs.c b/drivers/misc/ocxl/sysfs.c
index 405180d47d9bff0aaa7a736bb3fecfbe318db961..07520d6e6dc55702696b8656440914c379e6e27a 100644
--- a/drivers/misc/ocxl/sysfs.c
+++ b/drivers/misc/ocxl/sysfs.c
@@ -125,7 +125,7 @@ static const struct vm_operations_struct global_mmio_vmops = {
 };
 
 static int global_mmio_mmap(struct file *filp, struct kobject *kobj,
-			struct bin_attribute *bin_attr,
+			const struct bin_attribute *bin_attr,
 			struct vm_area_struct *vma)
 {
 	struct ocxl_afu *afu = to_afu(kobj_to_dev(kobj));
diff --git a/drivers/pci/p2pdma.c b/drivers/pci/p2pdma.c
index 4f47a13cb500ff5339cde426b6ccb020fcd74ae7..7abd4f546d3c071f31e622d881f5c5ac3e4de55e 100644
--- a/drivers/pci/p2pdma.c
+++ b/drivers/pci/p2pdma.c
@@ -90,7 +90,7 @@ static ssize_t published_show(struct device *dev, struct device_attribute *attr,
 static DEVICE_ATTR_RO(published);
 
 static int p2pmem_alloc_mmap(struct file *filp, struct kobject *kobj,
-		struct bin_attribute *attr, struct vm_area_struct *vma)
+		const struct bin_attribute *attr, struct vm_area_struct *vma)
 {
 	struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
 	size_t len = vma->vm_end - vma->vm_start;
diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
index 13912940ed2bb66c0086e5bea9a3cb6417ac14dd..0ad3427228b12aa95325c6fc00e9686740559238 100644
--- a/drivers/pci/pci-sysfs.c
+++ b/drivers/pci/pci-sysfs.c
@@ -910,7 +910,7 @@ static ssize_t pci_write_legacy_io(struct file *filp, struct kobject *kobj,
  * memory space.
  */
 static int pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj,
-			       struct bin_attribute *attr,
+			       const struct bin_attribute *attr,
 			       struct vm_area_struct *vma)
 {
 	struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
@@ -930,7 +930,7 @@ static int pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj,
  * memory space. Returns -ENOSYS if the operation isn't supported
  */
 static int pci_mmap_legacy_io(struct file *filp, struct kobject *kobj,
-			      struct bin_attribute *attr,
+			      const struct bin_attribute *attr,
 			      struct vm_area_struct *vma)
 {
 	struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
@@ -1034,7 +1034,7 @@ void pci_remove_legacy_files(struct pci_bus *b)
  *
  * Use the regular PCI mapping routines to map a PCI resource into userspace.
  */
-static int pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
+static int pci_mmap_resource(struct kobject *kobj, const struct bin_attribute *attr,
 			     struct vm_area_struct *vma, int write_combine)
 {
 	struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
@@ -1059,14 +1059,14 @@ static int pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
 }
 
 static int pci_mmap_resource_uc(struct file *filp, struct kobject *kobj,
-				struct bin_attribute *attr,
+				const struct bin_attribute *attr,
 				struct vm_area_struct *vma)
 {
 	return pci_mmap_resource(kobj, attr, vma, 0);
 }
 
 static int pci_mmap_resource_wc(struct file *filp, struct kobject *kobj,
-				struct bin_attribute *attr,
+				const struct bin_attribute *attr,
 				struct vm_area_struct *vma)
 {
 	return pci_mmap_resource(kobj, attr, vma, 1);
diff --git a/drivers/platform/x86/intel/pmt/class.c b/drivers/platform/x86/intel/pmt/class.c
index c04bb7f97a4db13268fc5697887951cf8f0f5a25..f9afa23e754b8b68bd59b72d6a72d26503a21f31 100644
--- a/drivers/platform/x86/intel/pmt/class.c
+++ b/drivers/platform/x86/intel/pmt/class.c
@@ -103,7 +103,7 @@ intel_pmt_read(struct file *filp, struct kobject *kobj,
 
 static int
 intel_pmt_mmap(struct file *filp, struct kobject *kobj,
-		struct bin_attribute *attr, struct vm_area_struct *vma)
+		const struct bin_attribute *attr, struct vm_area_struct *vma)
 {
 	struct intel_pmt_entry *entry = container_of(attr,
 						     struct intel_pmt_entry,
diff --git a/drivers/uio/uio_hv_generic.c b/drivers/uio/uio_hv_generic.c
index 8704095994118c2660f345c504b5ea466d053efb..3976360d0096d6681faf88815cc6277fb76a1d9f 100644
--- a/drivers/uio/uio_hv_generic.c
+++ b/drivers/uio/uio_hv_generic.c
@@ -135,7 +135,7 @@ static void hv_uio_rescind(struct vmbus_channel *channel)
  * The ring buffer is allocated as contiguous memory by vmbus_open
  */
 static int hv_uio_ring_mmap(struct file *filp, struct kobject *kobj,
-			    struct bin_attribute *attr,
+			    const struct bin_attribute *attr,
 			    struct vm_area_struct *vma)
 {
 	struct vmbus_channel *channel
diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h
index d1b22d56198b55ee39fe4c4fc994f5b753641992..9fcdc8cd3118f359742bfd8b708d5c3eff511042 100644
--- a/include/linux/sysfs.h
+++ b/include/linux/sysfs.h
@@ -309,7 +309,7 @@ struct bin_attribute {
 			 char *, loff_t, size_t);
 	loff_t (*llseek)(struct file *, struct kobject *, struct bin_attribute *,
 			 loff_t, int);
-	int (*mmap)(struct file *, struct kobject *, struct bin_attribute *attr,
+	int (*mmap)(struct file *, struct kobject *, const struct bin_attribute *attr,
 		    struct vm_area_struct *vma);
 };
 

-- 
2.47.0


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v2 07/10] sysfs: treewide: constify attribute callback of bin_attribute::llseek()
  2024-11-03 17:03 [PATCH v2 00/10] sysfs: constify struct bin_attribute (Part 1) Thomas Weißschuh
                   ` (5 preceding siblings ...)
  2024-11-03 17:03 ` [PATCH v2 06/10] sysfs: treewide: constify attribute callback of bin_attribute::mmap() Thomas Weißschuh
@ 2024-11-03 17:03 ` Thomas Weißschuh
  2024-11-03 17:03 ` [PATCH v2 08/10] sysfs: implement all BIN_ATTR_* macros in terms of __BIN_ATTR() Thomas Weißschuh
                   ` (4 subsequent siblings)
  11 siblings, 0 replies; 30+ messages in thread
From: Thomas Weißschuh @ 2024-11-03 17:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Bjorn Helgaas,
	Srinivas Kandagatla, Davidlohr Bueso, Jonathan Cameron,
	Dave Jiang, Alison Schofield, Vishal Verma, Ira Weiny,
	Alex Deucher, Christian König, Xinhui Pan, David Airlie,
	Simona Vetter, Dennis Dalessandro, Jason Gunthorpe,
	Leon Romanovsky, Tudor Ambarus, Pratyush Yadav, Michael Walle,
	Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
	Naveen Krishna Chatradhi, Carlos Bilbao, Hans de Goede,
	Ilpo Järvinen, David E. Box, James E.J. Bottomley,
	Martin K. Petersen, Richard Henderson, Matt Turner,
	Frederic Barrat, Andrew Donnellan, Arnd Bergmann, Logan Gunthorpe,
	K. Y. Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui
  Cc: Dan Williams, linux-kernel, linux-pci, linux-cxl, amd-gfx,
	dri-devel, linux-rdma, linux-mtd, platform-driver-x86, linux-scsi,
	linux-usb, linux-alpha, linuxppc-dev, linux-hyperv,
	Thomas Weißschuh

The llseek() callbacks should not modify the struct
bin_attribute passed as argument.
Enforce this by marking the argument as const.

As there are not many callback implementers perform this change
throughout the tree at once.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
 drivers/pci/pci-sysfs.c | 2 +-
 include/linux/sysfs.h   | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
index 0ad3427228b12aa95325c6fc00e9686740559238..49bee70f7d375bca056476acd6528d19ead2c419 100644
--- a/drivers/pci/pci-sysfs.c
+++ b/drivers/pci/pci-sysfs.c
@@ -841,7 +841,7 @@ static const struct attribute_group pci_dev_config_attr_group = {
 static __maybe_unused loff_t
 pci_llseek_resource(struct file *filep,
 		    struct kobject *kobj __always_unused,
-		    struct bin_attribute *attr,
+		    const struct bin_attribute *attr,
 		    loff_t offset, int whence)
 {
 	return fixed_size_llseek(filep, offset, whence, attr->size);
diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h
index 9fcdc8cd3118f359742bfd8b708d5c3eff511042..cb2a5e277c2384f2e8add8fbf2907e8a819576ec 100644
--- a/include/linux/sysfs.h
+++ b/include/linux/sysfs.h
@@ -307,7 +307,7 @@ struct bin_attribute {
 			char *, loff_t, size_t);
 	ssize_t (*write)(struct file *, struct kobject *, struct bin_attribute *,
 			 char *, loff_t, size_t);
-	loff_t (*llseek)(struct file *, struct kobject *, struct bin_attribute *,
+	loff_t (*llseek)(struct file *, struct kobject *, const struct bin_attribute *,
 			 loff_t, int);
 	int (*mmap)(struct file *, struct kobject *, const struct bin_attribute *attr,
 		    struct vm_area_struct *vma);

-- 
2.47.0


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v2 08/10] sysfs: implement all BIN_ATTR_* macros in terms of __BIN_ATTR()
  2024-11-03 17:03 [PATCH v2 00/10] sysfs: constify struct bin_attribute (Part 1) Thomas Weißschuh
                   ` (6 preceding siblings ...)
  2024-11-03 17:03 ` [PATCH v2 07/10] sysfs: treewide: constify attribute callback of bin_attribute::llseek() Thomas Weißschuh
@ 2024-11-03 17:03 ` Thomas Weißschuh
  2024-11-03 17:03 ` [PATCH v2 09/10] sysfs: bin_attribute: add const read/write callback variants Thomas Weißschuh
                   ` (3 subsequent siblings)
  11 siblings, 0 replies; 30+ messages in thread
From: Thomas Weißschuh @ 2024-11-03 17:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Bjorn Helgaas,
	Srinivas Kandagatla, Davidlohr Bueso, Jonathan Cameron,
	Dave Jiang, Alison Schofield, Vishal Verma, Ira Weiny,
	Alex Deucher, Christian König, Xinhui Pan, David Airlie,
	Simona Vetter, Dennis Dalessandro, Jason Gunthorpe,
	Leon Romanovsky, Tudor Ambarus, Pratyush Yadav, Michael Walle,
	Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
	Naveen Krishna Chatradhi, Carlos Bilbao, Hans de Goede,
	Ilpo Järvinen, David E. Box, James E.J. Bottomley,
	Martin K. Petersen, Richard Henderson, Matt Turner,
	Frederic Barrat, Andrew Donnellan, Arnd Bergmann, Logan Gunthorpe,
	K. Y. Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui
  Cc: Dan Williams, linux-kernel, linux-pci, linux-cxl, amd-gfx,
	dri-devel, linux-rdma, linux-mtd, platform-driver-x86, linux-scsi,
	linux-usb, linux-alpha, linuxppc-dev, linux-hyperv,
	Thomas Weißschuh

The preparations for the upcoming constification of struct bin_attribute
requires some logic in the structure definition macros.
To avoid duplication of that logic in multiple macros, reimplement all
other macros in terms of __BIN_ATTR().

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
 include/linux/sysfs.h | 27 ++++++++-------------------
 1 file changed, 8 insertions(+), 19 deletions(-)

diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h
index cb2a5e277c2384f2e8add8fbf2907e8a819576ec..d17c473c1ef292875475bf3bdf62d07241c13882 100644
--- a/include/linux/sysfs.h
+++ b/include/linux/sysfs.h
@@ -333,17 +333,11 @@ struct bin_attribute {
 	.size	= _size,						\
 }
 
-#define __BIN_ATTR_RO(_name, _size) {					\
-	.attr	= { .name = __stringify(_name), .mode = 0444 },		\
-	.read	= _name##_read,						\
-	.size	= _size,						\
-}
+#define __BIN_ATTR_RO(_name, _size)					\
+	__BIN_ATTR(_name, 0444, _name##_read, NULL, _size)
 
-#define __BIN_ATTR_WO(_name, _size) {					\
-	.attr	= { .name = __stringify(_name), .mode = 0200 },		\
-	.write	= _name##_write,					\
-	.size	= _size,						\
-}
+#define __BIN_ATTR_WO(_name, _size)					\
+	__BIN_ATTR(_name, 0200, NULL, _name##_write, _size)
 
 #define __BIN_ATTR_RW(_name, _size)					\
 	__BIN_ATTR(_name, 0644, _name##_read, _name##_write, _size)
@@ -364,11 +358,8 @@ struct bin_attribute bin_attr_##_name = __BIN_ATTR_WO(_name, _size)
 struct bin_attribute bin_attr_##_name = __BIN_ATTR_RW(_name, _size)
 
 
-#define __BIN_ATTR_ADMIN_RO(_name, _size) {					\
-	.attr	= { .name = __stringify(_name), .mode = 0400 },		\
-	.read	= _name##_read,						\
-	.size	= _size,						\
-}
+#define __BIN_ATTR_ADMIN_RO(_name, _size)				\
+	__BIN_ATTR(_name, 0400, _name##_read, NULL, _size)
 
 #define __BIN_ATTR_ADMIN_RW(_name, _size)					\
 	__BIN_ATTR(_name, 0600, _name##_read, _name##_write, _size)
@@ -379,10 +370,8 @@ struct bin_attribute bin_attr_##_name = __BIN_ATTR_ADMIN_RO(_name, _size)
 #define BIN_ATTR_ADMIN_RW(_name, _size)					\
 struct bin_attribute bin_attr_##_name = __BIN_ATTR_ADMIN_RW(_name, _size)
 
-#define __BIN_ATTR_SIMPLE_RO(_name, _mode) {				\
-	.attr	= { .name = __stringify(_name), .mode = _mode },	\
-	.read	= sysfs_bin_attr_simple_read,				\
-}
+#define __BIN_ATTR_SIMPLE_RO(_name, _mode)				\
+	__BIN_ATTR(_name, _mode, sysfs_bin_attr_simple_read, NULL, 0)
 
 #define BIN_ATTR_SIMPLE_RO(_name)					\
 struct bin_attribute bin_attr_##_name = __BIN_ATTR_SIMPLE_RO(_name, 0444)

-- 
2.47.0


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v2 09/10] sysfs: bin_attribute: add const read/write callback variants
  2024-11-03 17:03 [PATCH v2 00/10] sysfs: constify struct bin_attribute (Part 1) Thomas Weißschuh
                   ` (7 preceding siblings ...)
  2024-11-03 17:03 ` [PATCH v2 08/10] sysfs: implement all BIN_ATTR_* macros in terms of __BIN_ATTR() Thomas Weißschuh
@ 2024-11-03 17:03 ` Thomas Weißschuh
  2024-12-03 16:06   ` James Bottomley
  2024-11-03 17:03 ` [PATCH v2 10/10] driver core: Constify attribute arguments of binary attributes Thomas Weißschuh
                   ` (2 subsequent siblings)
  11 siblings, 1 reply; 30+ messages in thread
From: Thomas Weißschuh @ 2024-11-03 17:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Bjorn Helgaas,
	Srinivas Kandagatla, Davidlohr Bueso, Jonathan Cameron,
	Dave Jiang, Alison Schofield, Vishal Verma, Ira Weiny,
	Alex Deucher, Christian König, Xinhui Pan, David Airlie,
	Simona Vetter, Dennis Dalessandro, Jason Gunthorpe,
	Leon Romanovsky, Tudor Ambarus, Pratyush Yadav, Michael Walle,
	Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
	Naveen Krishna Chatradhi, Carlos Bilbao, Hans de Goede,
	Ilpo Järvinen, David E. Box, James E.J. Bottomley,
	Martin K. Petersen, Richard Henderson, Matt Turner,
	Frederic Barrat, Andrew Donnellan, Arnd Bergmann, Logan Gunthorpe,
	K. Y. Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui
  Cc: Dan Williams, linux-kernel, linux-pci, linux-cxl, amd-gfx,
	dri-devel, linux-rdma, linux-mtd, platform-driver-x86, linux-scsi,
	linux-usb, linux-alpha, linuxppc-dev, linux-hyperv,
	Thomas Weißschuh

To make it possible to put struct bin_attribute into read-only memory,
the sysfs core has to stop passing mutable pointers to the read() and
write() callbacks.
As there are numerous implementors of these callbacks throughout the
tree it's not possible to change all of them at once.
To enable a step-by-step transition, add new variants of the read() and
write() callbacks which differ only in the constness of the struct
bin_attribute argument.

As most binary attributes are defined through macros, extend these
macros to transparently handle both variants of callbacks to minimize
the churn during the transition.
As soon as all handlers are switch to the const variant, the non-const
one can be removed together with the transition machinery.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
 fs/sysfs/file.c       | 22 +++++++++++++++++-----
 include/linux/sysfs.h | 25 +++++++++++++++++++++++--
 2 files changed, 40 insertions(+), 7 deletions(-)

diff --git a/fs/sysfs/file.c b/fs/sysfs/file.c
index 6d39696b43069010b0ad0bdaadcf9002cb70c92c..785408861c01c89fc84c787848243a13c1338367 100644
--- a/fs/sysfs/file.c
+++ b/fs/sysfs/file.c
@@ -91,9 +91,12 @@ static ssize_t sysfs_kf_bin_read(struct kernfs_open_file *of, char *buf,
 			count = size - pos;
 	}
 
-	if (!battr->read)
+	if (!battr->read && !battr->read_new)
 		return -EIO;
 
+	if (battr->read_new)
+		return battr->read_new(of->file, kobj, battr, buf, pos, count);
+
 	return battr->read(of->file, kobj, battr, buf, pos, count);
 }
 
@@ -152,9 +155,12 @@ static ssize_t sysfs_kf_bin_write(struct kernfs_open_file *of, char *buf,
 	if (!count)
 		return 0;
 
-	if (!battr->write)
+	if (!battr->write && !battr->write_new)
 		return -EIO;
 
+	if (battr->write_new)
+		return battr->write_new(of->file, kobj, battr, buf, pos, count);
+
 	return battr->write(of->file, kobj, battr, buf, pos, count);
 }
 
@@ -323,13 +329,19 @@ int sysfs_add_bin_file_mode_ns(struct kernfs_node *parent,
 	const struct kernfs_ops *ops;
 	struct kernfs_node *kn;
 
+	if (battr->read && battr->read_new)
+		return -EINVAL;
+
+	if (battr->write && battr->write_new)
+		return -EINVAL;
+
 	if (battr->mmap)
 		ops = &sysfs_bin_kfops_mmap;
-	else if (battr->read && battr->write)
+	else if ((battr->read || battr->read_new) && (battr->write || battr->write_new))
 		ops = &sysfs_bin_kfops_rw;
-	else if (battr->read)
+	else if (battr->read || battr->read_new)
 		ops = &sysfs_bin_kfops_ro;
-	else if (battr->write)
+	else if (battr->write || battr->write_new)
 		ops = &sysfs_bin_kfops_wo;
 	else
 		ops = &sysfs_file_kfops_empty;
diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h
index d17c473c1ef292875475bf3bdf62d07241c13882..d713a6445a6267145a7014f308df3bb25b8c3287 100644
--- a/include/linux/sysfs.h
+++ b/include/linux/sysfs.h
@@ -305,8 +305,12 @@ struct bin_attribute {
 	struct address_space *(*f_mapping)(void);
 	ssize_t (*read)(struct file *, struct kobject *, struct bin_attribute *,
 			char *, loff_t, size_t);
+	ssize_t (*read_new)(struct file *, struct kobject *, const struct bin_attribute *,
+			    char *, loff_t, size_t);
 	ssize_t (*write)(struct file *, struct kobject *, struct bin_attribute *,
 			 char *, loff_t, size_t);
+	ssize_t (*write_new)(struct file *, struct kobject *,
+			     const struct bin_attribute *, char *, loff_t, size_t);
 	loff_t (*llseek)(struct file *, struct kobject *, const struct bin_attribute *,
 			 loff_t, int);
 	int (*mmap)(struct file *, struct kobject *, const struct bin_attribute *attr,
@@ -325,11 +329,28 @@ struct bin_attribute {
  */
 #define sysfs_bin_attr_init(bin_attr) sysfs_attr_init(&(bin_attr)->attr)
 
+typedef ssize_t __sysfs_bin_rw_handler_new(struct file *, struct kobject *,
+					   const struct bin_attribute *, char *, loff_t, size_t);
+
 /* macros to create static binary attributes easier */
 #define __BIN_ATTR(_name, _mode, _read, _write, _size) {		\
 	.attr = { .name = __stringify(_name), .mode = _mode },		\
-	.read	= _read,						\
-	.write	= _write,						\
+	.read = _Generic(_read,						\
+		__sysfs_bin_rw_handler_new * : NULL,			\
+		default : _read						\
+	),								\
+	.read_new = _Generic(_read,					\
+		__sysfs_bin_rw_handler_new * : _read,			\
+		default : NULL						\
+	),								\
+	.write = _Generic(_write,					\
+		__sysfs_bin_rw_handler_new * : NULL,			\
+		default : _write					\
+	),								\
+	.write_new = _Generic(_write,					\
+		__sysfs_bin_rw_handler_new * : _write,			\
+		default : NULL						\
+	),								\
 	.size	= _size,						\
 }
 

-- 
2.47.0


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v2 10/10] driver core: Constify attribute arguments of binary attributes
  2024-11-03 17:03 [PATCH v2 00/10] sysfs: constify struct bin_attribute (Part 1) Thomas Weißschuh
                   ` (8 preceding siblings ...)
  2024-11-03 17:03 ` [PATCH v2 09/10] sysfs: bin_attribute: add const read/write callback variants Thomas Weißschuh
@ 2024-11-03 17:03 ` Thomas Weißschuh
  2024-11-03 20:02 ` [PATCH v2 00/10] sysfs: constify struct bin_attribute (Part 1) Krzysztof Wilczyński
  2024-11-05 16:15 ` Bjorn Helgaas
  11 siblings, 0 replies; 30+ messages in thread
From: Thomas Weißschuh @ 2024-11-03 17:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Bjorn Helgaas,
	Srinivas Kandagatla, Davidlohr Bueso, Jonathan Cameron,
	Dave Jiang, Alison Schofield, Vishal Verma, Ira Weiny,
	Alex Deucher, Christian König, Xinhui Pan, David Airlie,
	Simona Vetter, Dennis Dalessandro, Jason Gunthorpe,
	Leon Romanovsky, Tudor Ambarus, Pratyush Yadav, Michael Walle,
	Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
	Naveen Krishna Chatradhi, Carlos Bilbao, Hans de Goede,
	Ilpo Järvinen, David E. Box, James E.J. Bottomley,
	Martin K. Petersen, Richard Henderson, Matt Turner,
	Frederic Barrat, Andrew Donnellan, Arnd Bergmann, Logan Gunthorpe,
	K. Y. Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui
  Cc: Dan Williams, linux-kernel, linux-pci, linux-cxl, amd-gfx,
	dri-devel, linux-rdma, linux-mtd, platform-driver-x86, linux-scsi,
	linux-usb, linux-alpha, linuxppc-dev, linux-hyperv,
	Thomas Weißschuh

As preparation for the constification of struct bin_attribute,
constify the arguments of the read and write callbacks.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
 drivers/base/node.c     | 4 ++--
 drivers/base/topology.c | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/base/node.c b/drivers/base/node.c
index eb72580288e62727e5b2198a6451cf9c2533225a..3e761633ac75826bedb5dd30b879f7cc1af95ec3 100644
--- a/drivers/base/node.c
+++ b/drivers/base/node.c
@@ -27,7 +27,7 @@ static const struct bus_type node_subsys = {
 };
 
 static inline ssize_t cpumap_read(struct file *file, struct kobject *kobj,
-				  struct bin_attribute *attr, char *buf,
+				  const struct bin_attribute *attr, char *buf,
 				  loff_t off, size_t count)
 {
 	struct device *dev = kobj_to_dev(kobj);
@@ -48,7 +48,7 @@ static inline ssize_t cpumap_read(struct file *file, struct kobject *kobj,
 static BIN_ATTR_RO(cpumap, CPUMAP_FILE_MAX_BYTES);
 
 static inline ssize_t cpulist_read(struct file *file, struct kobject *kobj,
-				   struct bin_attribute *attr, char *buf,
+				   const struct bin_attribute *attr, char *buf,
 				   loff_t off, size_t count)
 {
 	struct device *dev = kobj_to_dev(kobj);
diff --git a/drivers/base/topology.c b/drivers/base/topology.c
index 89f98be5c5b9915b2974e184bf89c4c25c183095..1090751d7f458ce8d2a50e82d65b8ce31e938f15 100644
--- a/drivers/base/topology.c
+++ b/drivers/base/topology.c
@@ -23,7 +23,7 @@ static ssize_t name##_show(struct device *dev,				\
 
 #define define_siblings_read_func(name, mask)					\
 static ssize_t name##_read(struct file *file, struct kobject *kobj,		\
-			   struct bin_attribute *attr, char *buf,		\
+			   const struct bin_attribute *attr, char *buf,		\
 			   loff_t off, size_t count)				\
 {										\
 	struct device *dev = kobj_to_dev(kobj);                                 \
@@ -33,7 +33,7 @@ static ssize_t name##_read(struct file *file, struct kobject *kobj,		\
 }										\
 										\
 static ssize_t name##_list_read(struct file *file, struct kobject *kobj,	\
-				struct bin_attribute *attr, char *buf,		\
+				const struct bin_attribute *attr, char *buf,	\
 				loff_t off, size_t count)			\
 {										\
 	struct device *dev = kobj_to_dev(kobj);					\

-- 
2.47.0


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* Re: [PATCH v2 00/10] sysfs: constify struct bin_attribute (Part 1)
  2024-11-03 17:03 [PATCH v2 00/10] sysfs: constify struct bin_attribute (Part 1) Thomas Weißschuh
                   ` (9 preceding siblings ...)
  2024-11-03 17:03 ` [PATCH v2 10/10] driver core: Constify attribute arguments of binary attributes Thomas Weißschuh
@ 2024-11-03 20:02 ` Krzysztof Wilczyński
  2024-11-05 16:15 ` Bjorn Helgaas
  11 siblings, 0 replies; 30+ messages in thread
From: Krzysztof Wilczyński @ 2024-11-03 20:02 UTC (permalink / raw)
  To: Thomas Weißschuh
  Cc: Greg Kroah-Hartman, Rafael J. Wysocki, Bjorn Helgaas,
	Srinivas Kandagatla, Davidlohr Bueso, Jonathan Cameron,
	Dave Jiang, Alison Schofield, Vishal Verma, Ira Weiny,
	Alex Deucher, Christian König, Xinhui Pan, David Airlie,
	Simona Vetter, Dennis Dalessandro, Jason Gunthorpe,
	Leon Romanovsky, Tudor Ambarus, Pratyush Yadav, Michael Walle,
	Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
	Naveen Krishna Chatradhi, Carlos Bilbao, Hans de Goede,
	Ilpo Järvinen, David E. Box, James E.J. Bottomley,
	Martin K. Petersen, Richard Henderson, Matt Turner,
	Frederic Barrat, Andrew Donnellan, Arnd Bergmann, Logan Gunthorpe,
	K. Y. Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui,
	Dan Williams, linux-kernel, linux-pci, linux-cxl, amd-gfx,
	dri-devel, linux-rdma, linux-mtd, platform-driver-x86, linux-scsi,
	linux-usb, linux-alpha, linuxppc-dev, linux-hyperv

Hello,

> struct bin_attribute contains a bunch of pointer members, which when
> overwritten by accident or malice can lead to system instability and
> security problems.
> Moving the definitions of struct bin_attribute to read-only memory
> makes these modifications impossible.
> The same change has been performed for many other structures in the
> past. (struct class, struct ctl_table...)
> 
> For the structure definitions throughout the core to be moved to
> read-only memory the following steps are necessary.
> 
> 1) Change all callbacks invoked from the sysfs core to only pass const
>    pointers
> 2) Adapt the sysfs core to only work in terms of const pointers
> 3) Adapt the sysfs core APIs to allow const pointers
> 4) Change all structure definitions through the core to const
> 
> This series provides the foundation for step 1) above.
> It converts some callbacks in a single step to const and provides a
> foundation for those callbacks where a single step is not possible.
> 
> Patches 1-5 change the bin_attribute callbacks of 'struct
> attribute_group'. The remaining ones touch 'struct bin_attribute' itself.
> 
> The techniques employed by this series can later be reused for the
> same change for other sysfs attributes.
> 
> This series is intended to be merged through the driver core tree.

This is very nice.  Thank you!

For PCI changes:
  Acked-by: Krzysztof Wilczyński <kw@linux.com>

This reminded me of an old discussions with Greg and Bjorn about how to set
size correctly for our ROM and BAR sysfs objects.  Nice to see a very nice
approach here, indeed.

	Krzysztof

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v2 06/10] sysfs: treewide: constify attribute callback of bin_attribute::mmap()
  2024-11-03 17:03 ` [PATCH v2 06/10] sysfs: treewide: constify attribute callback of bin_attribute::mmap() Thomas Weißschuh
@ 2024-11-04  1:24   ` Andrew Donnellan
  0 siblings, 0 replies; 30+ messages in thread
From: Andrew Donnellan @ 2024-11-04  1:24 UTC (permalink / raw)
  To: Thomas Weißschuh, Greg Kroah-Hartman, Rafael J. Wysocki,
	Bjorn Helgaas, Srinivas Kandagatla, Davidlohr Bueso,
	Jonathan Cameron, Dave Jiang, Alison Schofield, Vishal Verma,
	Ira Weiny, Alex Deucher, Christian König, Xinhui Pan,
	David Airlie, Simona Vetter, Dennis Dalessandro, Jason Gunthorpe,
	Leon Romanovsky, Tudor Ambarus, Pratyush Yadav, Michael Walle,
	Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
	Naveen Krishna Chatradhi, Carlos Bilbao, Hans de Goede,
	Ilpo Järvinen, David E. Box, James E.J. Bottomley,
	Martin K. Petersen, Richard Henderson, Matt Turner,
	Frederic Barrat, Arnd Bergmann, Logan Gunthorpe, K. Y. Srinivasan,
	Haiyang Zhang, Wei Liu, Dexuan Cui
  Cc: Dan Williams, linux-kernel, linux-pci, linux-cxl, amd-gfx,
	dri-devel, linux-rdma, linux-mtd, platform-driver-x86, linux-scsi,
	linux-usb, linux-alpha, linuxppc-dev, linux-hyperv

On Sun, 2024-11-03 at 17:03 +0000, Thomas Weißschuh wrote:
> The mmap() callbacks should not modify the struct
> bin_attribute passed as argument.
> Enforce this by marking the argument as const.
> 
> As there are not many callback implementers perform this change
> throughout the tree at once.
> 
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>

Acked-by: Andrew Donnellan <ajd@linux.ibm.com> # ocxl

-- 
Andrew Donnellan    OzLabs, ADL Canberra
ajd@linux.ibm.com   IBM Australia Limited

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v2 05/10] sysfs: treewide: constify attribute callback of bin_is_visible()
  2024-11-03 17:03 ` [PATCH v2 05/10] sysfs: treewide: constify attribute callback of bin_is_visible() Thomas Weißschuh
@ 2024-11-04 13:25   ` Ilpo Järvinen
  2024-11-04 13:52   ` Jason Gunthorpe
                     ` (5 subsequent siblings)
  6 siblings, 0 replies; 30+ messages in thread
From: Ilpo Järvinen @ 2024-11-04 13:25 UTC (permalink / raw)
  To: Thomas Weißschuh
  Cc: Greg Kroah-Hartman, Rafael J. Wysocki, Bjorn Helgaas,
	Srinivas Kandagatla, Davidlohr Bueso, Jonathan Cameron,
	Dave Jiang, Alison Schofield, Vishal Verma, Ira Weiny,
	Alex Deucher, Christian König, Xinhui Pan, David Airlie,
	Simona Vetter, Dennis Dalessandro, Jason Gunthorpe,
	Leon Romanovsky, Tudor Ambarus, Pratyush Yadav, Michael Walle,
	Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
	Naveen Krishna Chatradhi, Carlos Bilbao, Hans de Goede,
	Ilpo Järvinen, David E. Box, James E.J. Bottomley,
	Martin K. Petersen, Richard Henderson, Matt Turner,
	Frederic Barrat, Andrew Donnellan, Arnd Bergmann, Logan Gunthorpe,
	K. Y. Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui,
	Dan Williams, LKML, linux-pci, linux-cxl, amd-gfx, dri-devel,
	linux-rdma, linux-mtd, platform-driver-x86, linux-scsi, linux-usb,
	linux-alpha, linuxppc-dev, linux-hyperv

[-- Attachment #1: Type: text/plain, Size: 2405 bytes --]

On Sun, 3 Nov 2024, Thomas Weißschuh wrote:

> The is_bin_visible() callbacks should not modify the struct
> bin_attribute passed as argument.
> Enforce this by marking the argument as const.
> 
> As there are not many callback implementers perform this change
> throughout the tree at once.
> 
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> ---
>  drivers/cxl/port.c                      |  2 +-
>  drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c |  2 +-
>  drivers/infiniband/hw/qib/qib_sysfs.c   |  2 +-
>  drivers/mtd/spi-nor/sysfs.c             |  2 +-
>  drivers/nvmem/core.c                    |  3 ++-
>  drivers/pci/pci-sysfs.c                 |  2 +-
>  drivers/pci/vpd.c                       |  2 +-
>  drivers/platform/x86/amd/hsmp.c         |  2 +-
>  drivers/platform/x86/intel/sdsi.c       |  2 +-
>  drivers/scsi/scsi_sysfs.c               |  2 +-
>  drivers/usb/core/sysfs.c                |  2 +-
>  include/linux/sysfs.h                   | 30 +++++++++++++++---------------
>  12 files changed, 27 insertions(+), 26 deletions(-)

> diff --git a/drivers/platform/x86/amd/hsmp.c b/drivers/platform/x86/amd/hsmp.c
> index 8fcf38eed7f00ee01aade6e3e55e20402458d5aa..8f00850c139fa8d419bc1c140c1832bf84b2c3bd 100644
> --- a/drivers/platform/x86/amd/hsmp.c
> +++ b/drivers/platform/x86/amd/hsmp.c
> @@ -620,7 +620,7 @@ static int hsmp_get_tbl_dram_base(u16 sock_ind)
>  }
>  
>  static umode_t hsmp_is_sock_attr_visible(struct kobject *kobj,
> -					 struct bin_attribute *battr, int id)
> +					 const struct bin_attribute *battr, int id)

Hi Thomas,

This driver is reworked in pdx86/for-next.

-- 
 i.


>  {
>  	if (plat_dev.proto_ver == HSMP_PROTO_VER6)
>  		return battr->attr.mode;
> diff --git a/drivers/platform/x86/intel/sdsi.c b/drivers/platform/x86/intel/sdsi.c
> index 9d137621f0e6e7a23be0e0bbc6175c51c403169f..33f33b1070fdc949c1373251c3bca4234d9da119 100644
> --- a/drivers/platform/x86/intel/sdsi.c
> +++ b/drivers/platform/x86/intel/sdsi.c
> @@ -541,7 +541,7 @@ static struct bin_attribute *sdsi_bin_attrs[] = {
>  };
>  
>  static umode_t
> -sdsi_battr_is_visible(struct kobject *kobj, struct bin_attribute *attr, int n)
> +sdsi_battr_is_visible(struct kobject *kobj, const struct bin_attribute *attr, int n)
>  {
>  	struct device *dev = kobj_to_dev(kobj);
>  	struct sdsi_priv *priv = dev_get_drvdata(dev);

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v2 05/10] sysfs: treewide: constify attribute callback of bin_is_visible()
  2024-11-03 17:03 ` [PATCH v2 05/10] sysfs: treewide: constify attribute callback of bin_is_visible() Thomas Weißschuh
  2024-11-04 13:25   ` Ilpo Järvinen
@ 2024-11-04 13:52   ` Jason Gunthorpe
  2024-11-04 14:56   ` Ira Weiny
                     ` (4 subsequent siblings)
  6 siblings, 0 replies; 30+ messages in thread
From: Jason Gunthorpe @ 2024-11-04 13:52 UTC (permalink / raw)
  To: Thomas Weißschuh
  Cc: Greg Kroah-Hartman, Rafael J. Wysocki, Bjorn Helgaas,
	Srinivas Kandagatla, Davidlohr Bueso, Jonathan Cameron,
	Dave Jiang, Alison Schofield, Vishal Verma, Ira Weiny,
	Alex Deucher, Christian König, Xinhui Pan, David Airlie,
	Simona Vetter, Dennis Dalessandro, Leon Romanovsky, Tudor Ambarus,
	Pratyush Yadav, Michael Walle, Miquel Raynal, Richard Weinberger,
	Vignesh Raghavendra, Naveen Krishna Chatradhi, Carlos Bilbao,
	Hans de Goede, Ilpo Järvinen, David E. Box,
	James E.J. Bottomley, Martin K. Petersen, Richard Henderson,
	Matt Turner, Frederic Barrat, Andrew Donnellan, Arnd Bergmann,
	Logan Gunthorpe, K. Y. Srinivasan, Haiyang Zhang, Wei Liu,
	Dexuan Cui, Dan Williams, linux-kernel, linux-pci, linux-cxl,
	amd-gfx, dri-devel, linux-rdma, linux-mtd, platform-driver-x86,
	linux-scsi, linux-usb, linux-alpha, linuxppc-dev, linux-hyperv

On Sun, Nov 03, 2024 at 05:03:34PM +0000, Thomas Weißschuh wrote:
> The is_bin_visible() callbacks should not modify the struct
> bin_attribute passed as argument.
> Enforce this by marking the argument as const.
> 
> As there are not many callback implementers perform this change
> throughout the tree at once.
> 
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> ---
>  drivers/cxl/port.c                      |  2 +-
>  drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c |  2 +-
>  drivers/infiniband/hw/qib/qib_sysfs.c   |  2 +-
>  drivers/mtd/spi-nor/sysfs.c             |  2 +-
>  drivers/nvmem/core.c                    |  3 ++-
>  drivers/pci/pci-sysfs.c                 |  2 +-
>  drivers/pci/vpd.c                       |  2 +-
>  drivers/platform/x86/amd/hsmp.c         |  2 +-
>  drivers/platform/x86/intel/sdsi.c       |  2 +-
>  drivers/scsi/scsi_sysfs.c               |  2 +-
>  drivers/usb/core/sysfs.c                |  2 +-
>  include/linux/sysfs.h                   | 30 +++++++++++++++---------------
>  12 files changed, 27 insertions(+), 26 deletions(-)

For infiniband:

Acked-by: Jason Gunthorpe <jgg@nvidia.com>

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v2 05/10] sysfs: treewide: constify attribute callback of bin_is_visible()
  2024-11-03 17:03 ` [PATCH v2 05/10] sysfs: treewide: constify attribute callback of bin_is_visible() Thomas Weißschuh
  2024-11-04 13:25   ` Ilpo Järvinen
  2024-11-04 13:52   ` Jason Gunthorpe
@ 2024-11-04 14:56   ` Ira Weiny
  2024-11-05  1:25   ` Martin K. Petersen
                     ` (3 subsequent siblings)
  6 siblings, 0 replies; 30+ messages in thread
From: Ira Weiny @ 2024-11-04 14:56 UTC (permalink / raw)
  To: Thomas Weißschuh, Greg Kroah-Hartman, Rafael J. Wysocki,
	Bjorn Helgaas, Srinivas Kandagatla, Davidlohr Bueso,
	Jonathan Cameron, Dave Jiang, Alison Schofield, Vishal Verma,
	Ira Weiny, Alex Deucher, Christian König, Xinhui Pan,
	David Airlie, Simona Vetter, Dennis Dalessandro, Jason Gunthorpe,
	Leon Romanovsky, Tudor Ambarus, Pratyush Yadav, Michael Walle,
	Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
	Naveen Krishna Chatradhi, Carlos Bilbao, Hans de Goede,
	Ilpo Järvinen, David E. Box, James E.J. Bottomley,
	Martin K. Petersen, Richard Henderson, Matt Turner,
	Frederic Barrat, Andrew Donnellan, Arnd Bergmann, Logan Gunthorpe,
	K. Y. Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui
  Cc: Dan Williams, linux-kernel, linux-pci, linux-cxl, amd-gfx,
	dri-devel, linux-rdma, linux-mtd, platform-driver-x86, linux-scsi,
	linux-usb, linux-alpha, linuxppc-dev, linux-hyperv,
	Thomas Weißschuh

Thomas Weißschuh wrote:
> The is_bin_visible() callbacks should not modify the struct
> bin_attribute passed as argument.
> Enforce this by marking the argument as const.
> 
> As there are not many callback implementers perform this change
> throughout the tree at once.
> 
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> ---
>  drivers/cxl/port.c                      |  2 +-
>  drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c |  2 +-
>  drivers/infiniband/hw/qib/qib_sysfs.c   |  2 +-
>  drivers/mtd/spi-nor/sysfs.c             |  2 +-
>  drivers/nvmem/core.c                    |  3 ++-
>  drivers/pci/pci-sysfs.c                 |  2 +-
>  drivers/pci/vpd.c                       |  2 +-
>  drivers/platform/x86/amd/hsmp.c         |  2 +-
>  drivers/platform/x86/intel/sdsi.c       |  2 +-
>  drivers/scsi/scsi_sysfs.c               |  2 +-
>  drivers/usb/core/sysfs.c                |  2 +-
>  include/linux/sysfs.h                   | 30 +++++++++++++++---------------
>  12 files changed, 27 insertions(+), 26 deletions(-)
> 
> diff --git a/drivers/cxl/port.c b/drivers/cxl/port.c
> index 9dc394295e1fcd1610813837b2f515b66995eb25..24041cf85cfbe6c54c467ac325e48c775562b938 100644
> --- a/drivers/cxl/port.c
> +++ b/drivers/cxl/port.c
> @@ -173,7 +173,7 @@ static ssize_t CDAT_read(struct file *filp, struct kobject *kobj,
>  static BIN_ATTR_ADMIN_RO(CDAT, 0);
>  
>  static umode_t cxl_port_bin_attr_is_visible(struct kobject *kobj,
> -					    struct bin_attribute *attr, int i)
> +					    const struct bin_attribute *attr, int i)
>  {
>  	struct device *dev = kobj_to_dev(kobj);
>  	struct cxl_port *port = to_cxl_port(dev);

For CXL

Acked-by: Ira Weiny <ira.weiny@intel.com>

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v2 05/10] sysfs: treewide: constify attribute callback of bin_is_visible()
  2024-11-03 17:03 ` [PATCH v2 05/10] sysfs: treewide: constify attribute callback of bin_is_visible() Thomas Weißschuh
                     ` (2 preceding siblings ...)
  2024-11-04 14:56   ` Ira Weiny
@ 2024-11-05  1:25   ` Martin K. Petersen
  2024-11-05 15:26   ` Bjorn Helgaas
                     ` (2 subsequent siblings)
  6 siblings, 0 replies; 30+ messages in thread
From: Martin K. Petersen @ 2024-11-05  1:25 UTC (permalink / raw)
  To: Thomas Weißschuh
  Cc: Greg Kroah-Hartman, Rafael J. Wysocki, Bjorn Helgaas,
	Srinivas Kandagatla, Davidlohr Bueso, Jonathan Cameron,
	Dave Jiang, Alison Schofield, Vishal Verma, Ira Weiny,
	Alex Deucher, Christian König, Xinhui Pan, David Airlie,
	Simona Vetter, Dennis Dalessandro, Jason Gunthorpe,
	Leon Romanovsky, Tudor Ambarus, Pratyush Yadav, Michael Walle,
	Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
	Naveen Krishna Chatradhi, Carlos Bilbao, Hans de Goede,
	Ilpo Järvinen, David E. Box, James E.J. Bottomley,
	Martin K. Petersen, Richard Henderson, Matt Turner,
	Frederic Barrat, Andrew Donnellan, Arnd Bergmann, Logan Gunthorpe,
	K. Y. Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui,
	Dan Williams, linux-kernel, linux-pci, linux-cxl, amd-gfx,
	dri-devel, linux-rdma, linux-mtd, platform-driver-x86, linux-scsi,
	linux-usb, linux-alpha, linuxppc-dev, linux-hyperv


Thomas,

> The is_bin_visible() callbacks should not modify the struct
> bin_attribute passed as argument. Enforce this by marking the argument
> as const.
>
> As there are not many callback implementers perform this change
> throughout the tree at once.

For scsi:

Acked-by: Martin K. Petersen <martin.petersen@oracle.com>

-- 
Martin K. Petersen	Oracle Linux Engineering

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v2 05/10] sysfs: treewide: constify attribute callback of bin_is_visible()
  2024-11-03 17:03 ` [PATCH v2 05/10] sysfs: treewide: constify attribute callback of bin_is_visible() Thomas Weißschuh
                     ` (3 preceding siblings ...)
  2024-11-05  1:25   ` Martin K. Petersen
@ 2024-11-05 15:26   ` Bjorn Helgaas
  2024-11-07 17:20   ` Pratyush Yadav
  2024-11-08  9:57   ` Srinivas Kandagatla
  6 siblings, 0 replies; 30+ messages in thread
From: Bjorn Helgaas @ 2024-11-05 15:26 UTC (permalink / raw)
  To: Thomas Weißschuh
  Cc: Greg Kroah-Hartman, Rafael J. Wysocki, Bjorn Helgaas,
	Srinivas Kandagatla, Davidlohr Bueso, Jonathan Cameron,
	Dave Jiang, Alison Schofield, Vishal Verma, Ira Weiny,
	Alex Deucher, Christian König, Xinhui Pan, David Airlie,
	Simona Vetter, Dennis Dalessandro, Jason Gunthorpe,
	Leon Romanovsky, Tudor Ambarus, Pratyush Yadav, Michael Walle,
	Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
	Naveen Krishna Chatradhi, Carlos Bilbao, Hans de Goede,
	Ilpo Järvinen, David E. Box, James E.J. Bottomley,
	Martin K. Petersen, Richard Henderson, Matt Turner,
	Frederic Barrat, Andrew Donnellan, Arnd Bergmann, Logan Gunthorpe,
	K. Y. Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui,
	Dan Williams, linux-kernel, linux-pci, linux-cxl, amd-gfx,
	dri-devel, linux-rdma, linux-mtd, platform-driver-x86, linux-scsi,
	linux-usb, linux-alpha, linuxppc-dev, linux-hyperv

On Sun, Nov 03, 2024 at 05:03:34PM +0000, Thomas Weißschuh wrote:
> The is_bin_visible() callbacks should not modify the struct
> bin_attribute passed as argument.
> Enforce this by marking the argument as const.
> 
> As there are not many callback implementers perform this change
> throughout the tree at once.
> 
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>

Acked-by: Bjorn Helgaas <bhelgaas@google.com>	# drivers/pci

> ---
>  drivers/cxl/port.c                      |  2 +-
>  drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c |  2 +-
>  drivers/infiniband/hw/qib/qib_sysfs.c   |  2 +-
>  drivers/mtd/spi-nor/sysfs.c             |  2 +-
>  drivers/nvmem/core.c                    |  3 ++-
>  drivers/pci/pci-sysfs.c                 |  2 +-
>  drivers/pci/vpd.c                       |  2 +-
>  drivers/platform/x86/amd/hsmp.c         |  2 +-
>  drivers/platform/x86/intel/sdsi.c       |  2 +-
>  drivers/scsi/scsi_sysfs.c               |  2 +-
>  drivers/usb/core/sysfs.c                |  2 +-
>  include/linux/sysfs.h                   | 30 +++++++++++++++---------------
>  12 files changed, 27 insertions(+), 26 deletions(-)
> 
> diff --git a/drivers/cxl/port.c b/drivers/cxl/port.c
> index 9dc394295e1fcd1610813837b2f515b66995eb25..24041cf85cfbe6c54c467ac325e48c775562b938 100644
> --- a/drivers/cxl/port.c
> +++ b/drivers/cxl/port.c
> @@ -173,7 +173,7 @@ static ssize_t CDAT_read(struct file *filp, struct kobject *kobj,
>  static BIN_ATTR_ADMIN_RO(CDAT, 0);
>  
>  static umode_t cxl_port_bin_attr_is_visible(struct kobject *kobj,
> -					    struct bin_attribute *attr, int i)
> +					    const struct bin_attribute *attr, int i)
>  {
>  	struct device *dev = kobj_to_dev(kobj);
>  	struct cxl_port *port = to_cxl_port(dev);
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c
> index 0b28b2cf1517d130da01989df70b9dff6433edc4..c1c329eb920b52af100a93bdf00df450e25608c4 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c
> @@ -3999,7 +3999,7 @@ static umode_t amdgpu_flash_attr_is_visible(struct kobject *kobj, struct attribu
>  }
>  
>  static umode_t amdgpu_bin_flash_attr_is_visible(struct kobject *kobj,
> -						struct bin_attribute *attr,
> +						const struct bin_attribute *attr,
>  						int idx)
>  {
>  	struct device *dev = kobj_to_dev(kobj);
> diff --git a/drivers/infiniband/hw/qib/qib_sysfs.c b/drivers/infiniband/hw/qib/qib_sysfs.c
> index 53ec7510e4ebfb144e79884ca7dd7d0c873bd8a7..ba2cd68b53e6c240f1afc65c64012c75ccf488e0 100644
> --- a/drivers/infiniband/hw/qib/qib_sysfs.c
> +++ b/drivers/infiniband/hw/qib/qib_sysfs.c
> @@ -283,7 +283,7 @@ static struct bin_attribute *port_ccmgta_attributes[] = {
>  };
>  
>  static umode_t qib_ccmgta_is_bin_visible(struct kobject *kobj,
> -				 struct bin_attribute *attr, int n)
> +				 const struct bin_attribute *attr, int n)
>  {
>  	struct qib_pportdata *ppd = qib_get_pportdata_kobj(kobj);
>  
> diff --git a/drivers/mtd/spi-nor/sysfs.c b/drivers/mtd/spi-nor/sysfs.c
> index 96064e4babf01f6950c81586764386e7671cbf97..5e9eb268073d18e0a46089000f18a3200b4bf13d 100644
> --- a/drivers/mtd/spi-nor/sysfs.c
> +++ b/drivers/mtd/spi-nor/sysfs.c
> @@ -87,7 +87,7 @@ static umode_t spi_nor_sysfs_is_visible(struct kobject *kobj,
>  }
>  
>  static umode_t spi_nor_sysfs_is_bin_visible(struct kobject *kobj,
> -					    struct bin_attribute *attr, int n)
> +					    const struct bin_attribute *attr, int n)
>  {
>  	struct spi_device *spi = to_spi_device(kobj_to_dev(kobj));
>  	struct spi_mem *spimem = spi_get_drvdata(spi);
> diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
> index 63370c76394ee9b8d514da074779617cef67c311..73e44d724f90f4cd8fe8cafb9fa0c0fb23078e61 100644
> --- a/drivers/nvmem/core.c
> +++ b/drivers/nvmem/core.c
> @@ -298,7 +298,8 @@ static umode_t nvmem_bin_attr_get_umode(struct nvmem_device *nvmem)
>  }
>  
>  static umode_t nvmem_bin_attr_is_visible(struct kobject *kobj,
> -					 struct bin_attribute *attr, int i)
> +					 const struct bin_attribute *attr,
> +					 int i)
>  {
>  	struct device *dev = kobj_to_dev(kobj);
>  	struct nvmem_device *nvmem = to_nvmem_device(dev);
> diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
> index 040f01b2b999175e8d98b05851edc078bbabbe0d..13912940ed2bb66c0086e5bea9a3cb6417ac14dd 100644
> --- a/drivers/pci/pci-sysfs.c
> +++ b/drivers/pci/pci-sysfs.c
> @@ -1326,7 +1326,7 @@ static struct bin_attribute *pci_dev_rom_attrs[] = {
>  };
>  
>  static umode_t pci_dev_rom_attr_is_visible(struct kobject *kobj,
> -					   struct bin_attribute *a, int n)
> +					   const struct bin_attribute *a, int n)
>  {
>  	struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
>  
> diff --git a/drivers/pci/vpd.c b/drivers/pci/vpd.c
> index e4300f5f304f3ca55a657fd25a1fa5ed919737a7..a469bcbc0da7f7677485c7f999f8dfb58b8ae8a3 100644
> --- a/drivers/pci/vpd.c
> +++ b/drivers/pci/vpd.c
> @@ -325,7 +325,7 @@ static struct bin_attribute *vpd_attrs[] = {
>  };
>  
>  static umode_t vpd_attr_is_visible(struct kobject *kobj,
> -				   struct bin_attribute *a, int n)
> +				   const struct bin_attribute *a, int n)
>  {
>  	struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
>  
> diff --git a/drivers/platform/x86/amd/hsmp.c b/drivers/platform/x86/amd/hsmp.c
> index 8fcf38eed7f00ee01aade6e3e55e20402458d5aa..8f00850c139fa8d419bc1c140c1832bf84b2c3bd 100644
> --- a/drivers/platform/x86/amd/hsmp.c
> +++ b/drivers/platform/x86/amd/hsmp.c
> @@ -620,7 +620,7 @@ static int hsmp_get_tbl_dram_base(u16 sock_ind)
>  }
>  
>  static umode_t hsmp_is_sock_attr_visible(struct kobject *kobj,
> -					 struct bin_attribute *battr, int id)
> +					 const struct bin_attribute *battr, int id)
>  {
>  	if (plat_dev.proto_ver == HSMP_PROTO_VER6)
>  		return battr->attr.mode;
> diff --git a/drivers/platform/x86/intel/sdsi.c b/drivers/platform/x86/intel/sdsi.c
> index 9d137621f0e6e7a23be0e0bbc6175c51c403169f..33f33b1070fdc949c1373251c3bca4234d9da119 100644
> --- a/drivers/platform/x86/intel/sdsi.c
> +++ b/drivers/platform/x86/intel/sdsi.c
> @@ -541,7 +541,7 @@ static struct bin_attribute *sdsi_bin_attrs[] = {
>  };
>  
>  static umode_t
> -sdsi_battr_is_visible(struct kobject *kobj, struct bin_attribute *attr, int n)
> +sdsi_battr_is_visible(struct kobject *kobj, const struct bin_attribute *attr, int n)
>  {
>  	struct device *dev = kobj_to_dev(kobj);
>  	struct sdsi_priv *priv = dev_get_drvdata(dev);
> diff --git a/drivers/scsi/scsi_sysfs.c b/drivers/scsi/scsi_sysfs.c
> index 32f94db6d6bf5d2bd289c1a121da7ffc6a7cb2ff..f3a1ecb42128a2b221ca5c362e041eb59dba0f20 100644
> --- a/drivers/scsi/scsi_sysfs.c
> +++ b/drivers/scsi/scsi_sysfs.c
> @@ -1274,7 +1274,7 @@ static umode_t scsi_sdev_attr_is_visible(struct kobject *kobj,
>  }
>  
>  static umode_t scsi_sdev_bin_attr_is_visible(struct kobject *kobj,
> -					     struct bin_attribute *attr, int i)
> +					     const struct bin_attribute *attr, int i)
>  {
>  	struct device *dev = kobj_to_dev(kobj);
>  	struct scsi_device *sdev = to_scsi_device(dev);
> diff --git a/drivers/usb/core/sysfs.c b/drivers/usb/core/sysfs.c
> index 61b6d978892c799e213018bed22d9fb12a19d429..b4cba23831acd2d7d395b9f7683cd3ee3a8623c8 100644
> --- a/drivers/usb/core/sysfs.c
> +++ b/drivers/usb/core/sysfs.c
> @@ -925,7 +925,7 @@ static struct bin_attribute *dev_bin_attrs[] = {
>  };
>  
>  static umode_t dev_bin_attrs_are_visible(struct kobject *kobj,
> -		struct bin_attribute *a, int n)
> +		const struct bin_attribute *a, int n)
>  {
>  	struct device *dev = kobj_to_dev(kobj);
>  	struct usb_device *udev = to_usb_device(dev);
> diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h
> index 4746cccb95898b24df6f53de9421ea7649b5568f..d1b22d56198b55ee39fe4c4fc994f5b753641992 100644
> --- a/include/linux/sysfs.h
> +++ b/include/linux/sysfs.h
> @@ -101,7 +101,7 @@ struct attribute_group {
>  	umode_t			(*is_visible)(struct kobject *,
>  					      struct attribute *, int);
>  	umode_t			(*is_bin_visible)(struct kobject *,
> -						  struct bin_attribute *, int);
> +						  const struct bin_attribute *, int);
>  	size_t			(*bin_size)(struct kobject *,
>  					    const struct bin_attribute *,
>  					    int);
> @@ -199,22 +199,22 @@ struct attribute_group {
>   * attributes, the group visibility is determined by the function
>   * specified to is_visible() not is_bin_visible()
>   */
> -#define DEFINE_SYSFS_BIN_GROUP_VISIBLE(name)                             \
> -	static inline umode_t sysfs_group_visible_##name(                \
> -		struct kobject *kobj, struct bin_attribute *attr, int n) \
> -	{                                                                \
> -		if (n == 0 && !name##_group_visible(kobj))               \
> -			return SYSFS_GROUP_INVISIBLE;                    \
> -		return name##_attr_visible(kobj, attr, n);               \
> +#define DEFINE_SYSFS_BIN_GROUP_VISIBLE(name)                                   \
> +	static inline umode_t sysfs_group_visible_##name(                      \
> +		struct kobject *kobj, const struct bin_attribute *attr, int n) \
> +	{                                                                      \
> +		if (n == 0 && !name##_group_visible(kobj))                     \
> +			return SYSFS_GROUP_INVISIBLE;                          \
> +		return name##_attr_visible(kobj, attr, n);                     \
>  	}
>  
> -#define DEFINE_SIMPLE_SYSFS_BIN_GROUP_VISIBLE(name)                   \
> -	static inline umode_t sysfs_group_visible_##name(             \
> -		struct kobject *kobj, struct bin_attribute *a, int n) \
> -	{                                                             \
> -		if (n == 0 && !name##_group_visible(kobj))            \
> -			return SYSFS_GROUP_INVISIBLE;                 \
> -		return a->mode;                                       \
> +#define DEFINE_SIMPLE_SYSFS_BIN_GROUP_VISIBLE(name)                         \
> +	static inline umode_t sysfs_group_visible_##name(                   \
> +		struct kobject *kobj, const struct bin_attribute *a, int n) \
> +	{                                                                   \
> +		if (n == 0 && !name##_group_visible(kobj))                  \
> +			return SYSFS_GROUP_INVISIBLE;                       \
> +		return a->mode;                                             \
>  	}
>  
>  #define SYSFS_GROUP_VISIBLE(fn) sysfs_group_visible_##fn
> 
> -- 
> 2.47.0
> 

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v2 02/10] sysfs: introduce callback attribute_group::bin_size
  2024-11-03 17:03 ` [PATCH v2 02/10] sysfs: introduce callback attribute_group::bin_size Thomas Weißschuh
@ 2024-11-05 16:12   ` Bjorn Helgaas
  2024-11-06 19:27   ` Armin Wolf
  2024-11-06 20:05   ` Krzysztof Wilczyński
  2 siblings, 0 replies; 30+ messages in thread
From: Bjorn Helgaas @ 2024-11-05 16:12 UTC (permalink / raw)
  To: Thomas Weißschuh
  Cc: Greg Kroah-Hartman, Rafael J. Wysocki, Bjorn Helgaas,
	Srinivas Kandagatla, Davidlohr Bueso, Jonathan Cameron,
	Dave Jiang, Alison Schofield, Vishal Verma, Ira Weiny,
	Alex Deucher, Christian König, Xinhui Pan, David Airlie,
	Simona Vetter, Dennis Dalessandro, Jason Gunthorpe,
	Leon Romanovsky, Tudor Ambarus, Pratyush Yadav, Michael Walle,
	Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
	Naveen Krishna Chatradhi, Carlos Bilbao, Hans de Goede,
	Ilpo Järvinen, David E. Box, James E.J. Bottomley,
	Martin K. Petersen, Richard Henderson, Matt Turner,
	Frederic Barrat, Andrew Donnellan, Arnd Bergmann, Logan Gunthorpe,
	K. Y. Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui,
	Dan Williams, linux-kernel, linux-pci, linux-cxl, amd-gfx,
	dri-devel, linux-rdma, linux-mtd, platform-driver-x86, linux-scsi,
	linux-usb, linux-alpha, linuxppc-dev, linux-hyperv

On Sun, Nov 03, 2024 at 05:03:31PM +0000, Thomas Weißschuh wrote:
> Several drivers need to dynamically calculate the size of an binary
> attribute. Currently this is done by assigning attr->size from the
> is_bin_visible() callback.

s/an binary/a binary/

> This has drawbacks:
> * It is not documented.
> * A single attribute can be instantiated multiple times, overwriting the
>   shared size field.
> * It prevents the structure to be moved to read-only memory.
> 
> Introduce a new dedicated callback to calculate the size of the
> attribute.
> 
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> ---
>  fs/sysfs/group.c      | 2 ++
>  include/linux/sysfs.h | 8 ++++++++
>  2 files changed, 10 insertions(+)
> 
> diff --git a/fs/sysfs/group.c b/fs/sysfs/group.c
> index 45b2e92941da1f49dcc71af3781317c61480c956..8b01a7eda5fb3239e138372417d01967c7a3f122 100644
> --- a/fs/sysfs/group.c
> +++ b/fs/sysfs/group.c
> @@ -98,6 +98,8 @@ static int create_files(struct kernfs_node *parent, struct kobject *kobj,
>  				if (!mode)
>  					continue;
>  			}
> +			if (grp->bin_size)
> +				size = grp->bin_size(kobj, *bin_attr, i);
>  
>  			WARN(mode & ~(SYSFS_PREALLOC | 0664),
>  			     "Attribute %s: Invalid permissions 0%o\n",
> diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h
> index c4e64dc112063f7cb89bf66059d0338716089e87..4746cccb95898b24df6f53de9421ea7649b5568f 100644
> --- a/include/linux/sysfs.h
> +++ b/include/linux/sysfs.h
> @@ -87,6 +87,11 @@ do {							\
>   *		SYSFS_GROUP_VISIBLE() when assigning this callback to
>   *		specify separate _group_visible() and _attr_visible()
>   *		handlers.
> + * @bin_size:
> + *		Optional: Function to return the size of a binary attribute
> + *		of the group. Will be called repeatedly for each binary
> + *		attribute in the group. Overwrites the size field embedded
> + *		inside the attribute itself.

"Overwrites" suggests that we write over the size field in the single
shared attribute.  But that's not what create_files() does.

create_files() instantiates sysfs files from the attribute template.
Previously each instance used the size from the shared attribute.
With this patch, if ->bin_size() exists, its return value is the size
of this particular instance, over*riding* the default size from the
shared attribute.

This description follows the language of other function pointers,
which was the right approach.  But I think the existing language would
be more helpful if it called out the difference between the attribute
itself (a potentially read-only singleton structure shared by all
kobjects with this attribute) and the instantiation of that attribute
for each kobject.

For example,

  @bin_size:
	      Optional: Function to return the size of this kobject's
	      instantiation of a binary attribute.  If present, it is
	      called for each bin_attribute in the group and overrides
	      the default size from the bin_attribute template.

This is nice work, thanks for doing it!

>   * @attrs:	Pointer to NULL terminated list of attributes.
>   * @bin_attrs:	Pointer to NULL terminated list of binary attributes.
>   *		Either attrs or bin_attrs or both must be provided.
> @@ -97,6 +102,9 @@ struct attribute_group {
>  					      struct attribute *, int);
>  	umode_t			(*is_bin_visible)(struct kobject *,
>  						  struct bin_attribute *, int);
> +	size_t			(*bin_size)(struct kobject *,
> +					    const struct bin_attribute *,
> +					    int);
>  	struct attribute	**attrs;
>  	struct bin_attribute	**bin_attrs;
>  };
> 
> -- 
> 2.47.0
> 

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v2 00/10] sysfs: constify struct bin_attribute (Part 1)
  2024-11-03 17:03 [PATCH v2 00/10] sysfs: constify struct bin_attribute (Part 1) Thomas Weißschuh
                   ` (10 preceding siblings ...)
  2024-11-03 20:02 ` [PATCH v2 00/10] sysfs: constify struct bin_attribute (Part 1) Krzysztof Wilczyński
@ 2024-11-05 16:15 ` Bjorn Helgaas
  11 siblings, 0 replies; 30+ messages in thread
From: Bjorn Helgaas @ 2024-11-05 16:15 UTC (permalink / raw)
  To: Thomas Weißschuh
  Cc: Greg Kroah-Hartman, Rafael J. Wysocki, Bjorn Helgaas,
	Srinivas Kandagatla, Davidlohr Bueso, Jonathan Cameron,
	Dave Jiang, Alison Schofield, Vishal Verma, Ira Weiny,
	Alex Deucher, Christian König, Xinhui Pan, David Airlie,
	Simona Vetter, Dennis Dalessandro, Jason Gunthorpe,
	Leon Romanovsky, Tudor Ambarus, Pratyush Yadav, Michael Walle,
	Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
	Naveen Krishna Chatradhi, Carlos Bilbao, Hans de Goede,
	Ilpo Järvinen, David E. Box, James E.J. Bottomley,
	Martin K. Petersen, Richard Henderson, Matt Turner,
	Frederic Barrat, Andrew Donnellan, Arnd Bergmann, Logan Gunthorpe,
	K. Y. Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui,
	Dan Williams, linux-kernel, linux-pci, linux-cxl, amd-gfx,
	dri-devel, linux-rdma, linux-mtd, platform-driver-x86, linux-scsi,
	linux-usb, linux-alpha, linuxppc-dev, linux-hyperv

On Sun, Nov 03, 2024 at 05:03:29PM +0000, Thomas Weißschuh wrote:
> struct bin_attribute contains a bunch of pointer members, which when
> overwritten by accident or malice can lead to system instability and
> security problems.
> Moving the definitions of struct bin_attribute to read-only memory
> makes these modifications impossible.
> The same change has been performed for many other structures in the
> past. (struct class, struct ctl_table...)

Throughout series, it would be more readable if you added blank lines
between paragraphs.

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v2 02/10] sysfs: introduce callback attribute_group::bin_size
  2024-11-03 17:03 ` [PATCH v2 02/10] sysfs: introduce callback attribute_group::bin_size Thomas Weißschuh
  2024-11-05 16:12   ` Bjorn Helgaas
@ 2024-11-06 19:27   ` Armin Wolf
  2024-11-06 20:05   ` Krzysztof Wilczyński
  2 siblings, 0 replies; 30+ messages in thread
From: Armin Wolf @ 2024-11-06 19:27 UTC (permalink / raw)
  To: Thomas Weißschuh, Greg Kroah-Hartman, Rafael J. Wysocki,
	Bjorn Helgaas, Srinivas Kandagatla, Davidlohr Bueso,
	Jonathan Cameron, Dave Jiang, Alison Schofield, Vishal Verma,
	Ira Weiny, Alex Deucher, Christian König, Xinhui Pan,
	David Airlie, Simona Vetter, Dennis Dalessandro, Jason Gunthorpe,
	Leon Romanovsky, Tudor Ambarus, Pratyush Yadav, Michael Walle,
	Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
	Naveen Krishna Chatradhi, Carlos Bilbao, Hans de Goede,
	Ilpo Järvinen, David E. Box, James E.J. Bottomley,
	Martin K. Petersen, Richard Henderson, Matt Turner,
	Frederic Barrat, Andrew Donnellan, Arnd Bergmann, Logan Gunthorpe,
	K. Y. Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui
  Cc: Dan Williams, linux-kernel, linux-pci, linux-cxl, amd-gfx,
	dri-devel, linux-rdma, linux-mtd, platform-driver-x86, linux-scsi,
	linux-usb, linux-alpha, linuxppc-dev, linux-hyperv

Am 03.11.24 um 18:03 schrieb Thomas Weißschuh:

> Several drivers need to dynamically calculate the size of an binary
> attribute. Currently this is done by assigning attr->size from the
> is_bin_visible() callback.

Hi,

i really like your idea of introducing this new callback, it will be very
useful for the wmi-bmof driver :).

Thanks,
Armin Wolf

>
> This has drawbacks:
> * It is not documented.
> * A single attribute can be instantiated multiple times, overwriting the
>    shared size field.
> * It prevents the structure to be moved to read-only memory.
>
> Introduce a new dedicated callback to calculate the size of the
> attribute.
>
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> ---
>   fs/sysfs/group.c      | 2 ++
>   include/linux/sysfs.h | 8 ++++++++
>   2 files changed, 10 insertions(+)
>
> diff --git a/fs/sysfs/group.c b/fs/sysfs/group.c
> index 45b2e92941da1f49dcc71af3781317c61480c956..8b01a7eda5fb3239e138372417d01967c7a3f122 100644
> --- a/fs/sysfs/group.c
> +++ b/fs/sysfs/group.c
> @@ -98,6 +98,8 @@ static int create_files(struct kernfs_node *parent, struct kobject *kobj,
>   				if (!mode)
>   					continue;
>   			}
> +			if (grp->bin_size)
> +				size = grp->bin_size(kobj, *bin_attr, i);
>
>   			WARN(mode & ~(SYSFS_PREALLOC | 0664),
>   			     "Attribute %s: Invalid permissions 0%o\n",
> diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h
> index c4e64dc112063f7cb89bf66059d0338716089e87..4746cccb95898b24df6f53de9421ea7649b5568f 100644
> --- a/include/linux/sysfs.h
> +++ b/include/linux/sysfs.h
> @@ -87,6 +87,11 @@ do {							\
>    *		SYSFS_GROUP_VISIBLE() when assigning this callback to
>    *		specify separate _group_visible() and _attr_visible()
>    *		handlers.
> + * @bin_size:
> + *		Optional: Function to return the size of a binary attribute
> + *		of the group. Will be called repeatedly for each binary
> + *		attribute in the group. Overwrites the size field embedded
> + *		inside the attribute itself.
>    * @attrs:	Pointer to NULL terminated list of attributes.
>    * @bin_attrs:	Pointer to NULL terminated list of binary attributes.
>    *		Either attrs or bin_attrs or both must be provided.
> @@ -97,6 +102,9 @@ struct attribute_group {
>   					      struct attribute *, int);
>   	umode_t			(*is_bin_visible)(struct kobject *,
>   						  struct bin_attribute *, int);
> +	size_t			(*bin_size)(struct kobject *,
> +					    const struct bin_attribute *,
> +					    int);
>   	struct attribute	**attrs;
>   	struct bin_attribute	**bin_attrs;
>   };
>

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v2 02/10] sysfs: introduce callback attribute_group::bin_size
  2024-11-03 17:03 ` [PATCH v2 02/10] sysfs: introduce callback attribute_group::bin_size Thomas Weißschuh
  2024-11-05 16:12   ` Bjorn Helgaas
  2024-11-06 19:27   ` Armin Wolf
@ 2024-11-06 20:05   ` Krzysztof Wilczyński
  2024-11-07  5:21     ` Greg Kroah-Hartman
  2 siblings, 1 reply; 30+ messages in thread
From: Krzysztof Wilczyński @ 2024-11-06 20:05 UTC (permalink / raw)
  To: Thomas Weißschuh
  Cc: Greg Kroah-Hartman, Rafael J. Wysocki, Bjorn Helgaas,
	Srinivas Kandagatla, Davidlohr Bueso, Jonathan Cameron,
	Dave Jiang, Alison Schofield, Vishal Verma, Ira Weiny,
	Alex Deucher, Christian König, Xinhui Pan, David Airlie,
	Simona Vetter, Dennis Dalessandro, Jason Gunthorpe,
	Leon Romanovsky, Tudor Ambarus, Pratyush Yadav, Michael Walle,
	Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
	Naveen Krishna Chatradhi, Carlos Bilbao, Hans de Goede,
	Ilpo Järvinen, David E. Box, James E.J. Bottomley,
	Martin K. Petersen, Richard Henderson, Matt Turner,
	Frederic Barrat, Andrew Donnellan, Arnd Bergmann, Logan Gunthorpe,
	K. Y. Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui,
	Dan Williams, linux-kernel, linux-pci, linux-cxl, amd-gfx,
	dri-devel, linux-rdma, linux-mtd, platform-driver-x86, linux-scsi,
	linux-usb, linux-alpha, linuxppc-dev, linux-hyperv

Hello,

> Several drivers need to dynamically calculate the size of an binary
> attribute. Currently this is done by assigning attr->size from the
> is_bin_visible() callback.
> 
> This has drawbacks:
> * It is not documented.
> * A single attribute can be instantiated multiple times, overwriting the
>   shared size field.
> * It prevents the structure to be moved to read-only memory.
> 
> Introduce a new dedicated callback to calculate the size of the
> attribute.

Would it be possible to have a helper that when run against a specific
kobject reference, then it would refresh or re-run the size callbacks?

We have an use case where we resize BARs on demand via sysfs, and currently
the only way to update the size of each resource sysfs object is to remove
and added them again, which is a bit crude, and can also be unsafe.

Hence the question.

There exist the sysfs_update_groups(), but the BAR resource sysfs objects
are currently, at least not yet, added to any attribute group.

Thank you!

	Krzysztof

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v2 02/10] sysfs: introduce callback attribute_group::bin_size
  2024-11-06 20:05   ` Krzysztof Wilczyński
@ 2024-11-07  5:21     ` Greg Kroah-Hartman
  2024-11-07 15:50       ` Krzysztof Wilczyński
  0 siblings, 1 reply; 30+ messages in thread
From: Greg Kroah-Hartman @ 2024-11-07  5:21 UTC (permalink / raw)
  To: Krzysztof Wilczyński
  Cc: Thomas Weißschuh, Rafael J. Wysocki, Bjorn Helgaas,
	Srinivas Kandagatla, Davidlohr Bueso, Jonathan Cameron,
	Dave Jiang, Alison Schofield, Vishal Verma, Ira Weiny,
	Alex Deucher, Christian König, Xinhui Pan, David Airlie,
	Simona Vetter, Dennis Dalessandro, Jason Gunthorpe,
	Leon Romanovsky, Tudor Ambarus, Pratyush Yadav, Michael Walle,
	Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
	Naveen Krishna Chatradhi, Carlos Bilbao, Hans de Goede,
	Ilpo Järvinen, David E. Box, James E.J. Bottomley,
	Martin K. Petersen, Richard Henderson, Matt Turner,
	Frederic Barrat, Andrew Donnellan, Arnd Bergmann, Logan Gunthorpe,
	K. Y. Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui,
	Dan Williams, linux-kernel, linux-pci, linux-cxl, amd-gfx,
	dri-devel, linux-rdma, linux-mtd, platform-driver-x86, linux-scsi,
	linux-usb, linux-alpha, linuxppc-dev, linux-hyperv

On Thu, Nov 07, 2024 at 05:05:13AM +0900, Krzysztof Wilczyński wrote:
> Hello,
> 
> > Several drivers need to dynamically calculate the size of an binary
> > attribute. Currently this is done by assigning attr->size from the
> > is_bin_visible() callback.
> > 
> > This has drawbacks:
> > * It is not documented.
> > * A single attribute can be instantiated multiple times, overwriting the
> >   shared size field.
> > * It prevents the structure to be moved to read-only memory.
> > 
> > Introduce a new dedicated callback to calculate the size of the
> > attribute.
> 
> Would it be possible to have a helper that when run against a specific
> kobject reference, then it would refresh or re-run the size callbacks?
> 
> We have an use case where we resize BARs on demand via sysfs, and currently
> the only way to update the size of each resource sysfs object is to remove
> and added them again, which is a bit crude, and can also be unsafe.

How is it unsafe?

> Hence the question.
> 
> There exist the sysfs_update_groups(), but the BAR resource sysfs objects
> are currently, at least not yet, added to any attribute group.

then maybe they should be added to one :)

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v2 02/10] sysfs: introduce callback attribute_group::bin_size
  2024-11-07  5:21     ` Greg Kroah-Hartman
@ 2024-11-07 15:50       ` Krzysztof Wilczyński
  0 siblings, 0 replies; 30+ messages in thread
From: Krzysztof Wilczyński @ 2024-11-07 15:50 UTC (permalink / raw)
  To: Greg Kroah-Hartman
  Cc: Thomas Weißschuh, Rafael J. Wysocki, Bjorn Helgaas,
	Srinivas Kandagatla, Davidlohr Bueso, Jonathan Cameron,
	Dave Jiang, Alison Schofield, Vishal Verma, Ira Weiny,
	Alex Deucher, Christian König, Xinhui Pan, David Airlie,
	Simona Vetter, Dennis Dalessandro, Jason Gunthorpe,
	Leon Romanovsky, Tudor Ambarus, Pratyush Yadav, Michael Walle,
	Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
	Naveen Krishna Chatradhi, Carlos Bilbao, Hans de Goede,
	Ilpo Järvinen, David E. Box, James E.J. Bottomley,
	Martin K. Petersen, Richard Henderson, Matt Turner,
	Frederic Barrat, Andrew Donnellan, Arnd Bergmann, Logan Gunthorpe,
	K. Y. Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui,
	Dan Williams, linux-kernel, linux-pci, linux-cxl, amd-gfx,
	dri-devel, linux-rdma, linux-mtd, platform-driver-x86, linux-scsi,
	linux-usb, linux-alpha, linuxppc-dev, linux-hyperv

Hello,

[...]
> > There exist the sysfs_update_groups(), but the BAR resource sysfs objects
> > are currently, at least not yet, added to any attribute group.
> 
> then maybe they should be added to one :)

Yeah. There is work in progress that will take care of some of this.

	Krzysztof

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v2 03/10] PCI/sysfs: Calculate bin_attribute size through bin_size()
  2024-11-03 17:03 ` [PATCH v2 03/10] PCI/sysfs: Calculate bin_attribute size through bin_size() Thomas Weißschuh
@ 2024-11-07 16:27   ` Bjorn Helgaas
  0 siblings, 0 replies; 30+ messages in thread
From: Bjorn Helgaas @ 2024-11-07 16:27 UTC (permalink / raw)
  To: Thomas Weißschuh
  Cc: Greg Kroah-Hartman, Rafael J. Wysocki, Bjorn Helgaas,
	Srinivas Kandagatla, Davidlohr Bueso, Jonathan Cameron,
	Dave Jiang, Alison Schofield, Vishal Verma, Ira Weiny,
	Alex Deucher, Christian König, Xinhui Pan, David Airlie,
	Simona Vetter, Dennis Dalessandro, Jason Gunthorpe,
	Leon Romanovsky, Tudor Ambarus, Pratyush Yadav, Michael Walle,
	Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
	Naveen Krishna Chatradhi, Carlos Bilbao, Hans de Goede,
	Ilpo Järvinen, David E. Box, James E.J. Bottomley,
	Martin K. Petersen, Richard Henderson, Matt Turner,
	Frederic Barrat, Andrew Donnellan, Arnd Bergmann, Logan Gunthorpe,
	K. Y. Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui,
	Dan Williams, linux-kernel, linux-pci, linux-cxl, amd-gfx,
	dri-devel, linux-rdma, linux-mtd, platform-driver-x86, linux-scsi,
	linux-usb, linux-alpha, linuxppc-dev, linux-hyperv

On Sun, Nov 03, 2024 at 05:03:32PM +0000, Thomas Weißschuh wrote:
> Stop abusing the is_bin_visible() callback to calculate the attribute
> size. Instead use the new, dedicated bin_size() one.
> 
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>

Acked-by: Bjorn Helgaas <bhelgaas@google.com>

Thanks for doing this!

> ---
>  drivers/pci/pci-sysfs.c | 28 ++++++++++++++++------------
>  1 file changed, 16 insertions(+), 12 deletions(-)
> 
> diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
> index 5d0f4db1cab78674c5e5906f321bf7a57b742983..040f01b2b999175e8d98b05851edc078bbabbe0d 100644
> --- a/drivers/pci/pci-sysfs.c
> +++ b/drivers/pci/pci-sysfs.c
> @@ -818,21 +818,20 @@ static struct bin_attribute *pci_dev_config_attrs[] = {
>  	NULL,
>  };
>  
> -static umode_t pci_dev_config_attr_is_visible(struct kobject *kobj,
> -					      struct bin_attribute *a, int n)
> +static size_t pci_dev_config_attr_bin_size(struct kobject *kobj,
> +					   const struct bin_attribute *a,
> +					   int n)
>  {
>  	struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
>  
> -	a->size = PCI_CFG_SPACE_SIZE;
>  	if (pdev->cfg_size > PCI_CFG_SPACE_SIZE)
> -		a->size = PCI_CFG_SPACE_EXP_SIZE;
> -
> -	return a->attr.mode;
> +		return PCI_CFG_SPACE_EXP_SIZE;
> +	return PCI_CFG_SPACE_SIZE;
>  }
>  
>  static const struct attribute_group pci_dev_config_attr_group = {
>  	.bin_attrs = pci_dev_config_attrs,
> -	.is_bin_visible = pci_dev_config_attr_is_visible,
> +	.bin_size = pci_dev_config_attr_bin_size,
>  };
>  
>  /*
> @@ -1330,21 +1329,26 @@ static umode_t pci_dev_rom_attr_is_visible(struct kobject *kobj,
>  					   struct bin_attribute *a, int n)
>  {
>  	struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
> -	size_t rom_size;
>  
>  	/* If the device has a ROM, try to expose it in sysfs. */
> -	rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
> -	if (!rom_size)
> +	if (!pci_resource_end(pdev, PCI_ROM_RESOURCE))
>  		return 0;
>  
> -	a->size = rom_size;
> -
>  	return a->attr.mode;
>  }
>  
> +static size_t pci_dev_rom_attr_bin_size(struct kobject *kobj,
> +					const struct bin_attribute *a, int n)
> +{
> +	struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
> +
> +	return pci_resource_len(pdev, PCI_ROM_RESOURCE);
> +}
> +
>  static const struct attribute_group pci_dev_rom_attr_group = {
>  	.bin_attrs = pci_dev_rom_attrs,
>  	.is_bin_visible = pci_dev_rom_attr_is_visible,
> +	.bin_size = pci_dev_rom_attr_bin_size,
>  };
>  
>  static ssize_t reset_store(struct device *dev, struct device_attribute *attr,
> 
> -- 
> 2.47.0
> 

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v2 05/10] sysfs: treewide: constify attribute callback of bin_is_visible()
  2024-11-03 17:03 ` [PATCH v2 05/10] sysfs: treewide: constify attribute callback of bin_is_visible() Thomas Weißschuh
                     ` (4 preceding siblings ...)
  2024-11-05 15:26   ` Bjorn Helgaas
@ 2024-11-07 17:20   ` Pratyush Yadav
  2024-11-08  9:57   ` Srinivas Kandagatla
  6 siblings, 0 replies; 30+ messages in thread
From: Pratyush Yadav @ 2024-11-07 17:20 UTC (permalink / raw)
  To: Thomas Weißschuh
  Cc: Greg Kroah-Hartman, Rafael J. Wysocki, Bjorn Helgaas,
	Srinivas Kandagatla, Davidlohr Bueso, Jonathan Cameron,
	Dave Jiang, Alison Schofield, Vishal Verma, Ira Weiny,
	Alex Deucher, Christian König, Xinhui Pan, David Airlie,
	Simona Vetter, Dennis Dalessandro, Jason Gunthorpe,
	Leon Romanovsky, Tudor Ambarus, Pratyush Yadav, Michael Walle,
	Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
	Naveen Krishna Chatradhi, Carlos Bilbao, Hans de Goede,
	Ilpo Järvinen, David E. Box, James E.J. Bottomley,
	Martin K. Petersen, Richard Henderson, Matt Turner,
	Frederic Barrat, Andrew Donnellan, Arnd Bergmann, Logan Gunthorpe,
	K. Y. Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui,
	Dan Williams, linux-kernel, linux-pci, linux-cxl, amd-gfx,
	dri-devel, linux-rdma, linux-mtd, platform-driver-x86, linux-scsi,
	linux-usb, linux-alpha, linuxppc-dev, linux-hyperv

On Sun, Nov 03 2024, Thomas Weißschuh wrote:

> The is_bin_visible() callbacks should not modify the struct
> bin_attribute passed as argument.
> Enforce this by marking the argument as const.
>
> As there are not many callback implementers perform this change
> throughout the tree at once.
>
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> ---
> diff --git a/drivers/mtd/spi-nor/sysfs.c b/drivers/mtd/spi-nor/sysfs.c
> index 96064e4babf01f6950c81586764386e7671cbf97..5e9eb268073d18e0a46089000f18a3200b4bf13d 100644
> --- a/drivers/mtd/spi-nor/sysfs.c
> +++ b/drivers/mtd/spi-nor/sysfs.c
> @@ -87,7 +87,7 @@ static umode_t spi_nor_sysfs_is_visible(struct kobject *kobj,
>  }
>  
>  static umode_t spi_nor_sysfs_is_bin_visible(struct kobject *kobj,
> -					    struct bin_attribute *attr, int n)
> +					    const struct bin_attribute *attr, int n)

Acked-by: Pratyush Yadav <pratyush@kernel.org> # for spi-nor

>  {
>  	struct spi_device *spi = to_spi_device(kobj_to_dev(kobj));
>  	struct spi_mem *spimem = spi_get_drvdata(spi);

-- 
Regards,
Pratyush Yadav

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v2 05/10] sysfs: treewide: constify attribute callback of bin_is_visible()
  2024-11-03 17:03 ` [PATCH v2 05/10] sysfs: treewide: constify attribute callback of bin_is_visible() Thomas Weißschuh
                     ` (5 preceding siblings ...)
  2024-11-07 17:20   ` Pratyush Yadav
@ 2024-11-08  9:57   ` Srinivas Kandagatla
  6 siblings, 0 replies; 30+ messages in thread
From: Srinivas Kandagatla @ 2024-11-08  9:57 UTC (permalink / raw)
  To: Thomas Weißschuh, Greg Kroah-Hartman, Rafael J. Wysocki,
	Bjorn Helgaas, Davidlohr Bueso, Jonathan Cameron, Dave Jiang,
	Alison Schofield, Vishal Verma, Ira Weiny, Alex Deucher,
	Christian König, Xinhui Pan, David Airlie, Simona Vetter,
	Dennis Dalessandro, Jason Gunthorpe, Leon Romanovsky,
	Tudor Ambarus, Pratyush Yadav, Michael Walle, Miquel Raynal,
	Richard Weinberger, Vignesh Raghavendra, Naveen Krishna Chatradhi,
	Carlos Bilbao, Hans de Goede, Ilpo Järvinen, David E. Box,
	James E.J. Bottomley, Martin K. Petersen, Richard Henderson,
	Matt Turner, Frederic Barrat, Andrew Donnellan, Arnd Bergmann,
	Logan Gunthorpe, K. Y. Srinivasan, Haiyang Zhang, Wei Liu,
	Dexuan Cui
  Cc: Dan Williams, linux-kernel, linux-pci, linux-cxl, amd-gfx,
	dri-devel, linux-rdma, linux-mtd, platform-driver-x86, linux-scsi,
	linux-usb, linux-alpha, linuxppc-dev, linux-hyperv



On 03/11/2024 17:03, Thomas Weißschuh wrote:
> The is_bin_visible() callbacks should not modify the struct
> bin_attribute passed as argument.
> Enforce this by marking the argument as const.
> 
> As there are not many callback implementers perform this change
> throughout the tree at once.
> 
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> ---
>   drivers/cxl/port.c                      |  2 +-
>   drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c |  2 +-
>   drivers/infiniband/hw/qib/qib_sysfs.c   |  2 +-
>   drivers/mtd/spi-nor/sysfs.c             |  2 +-

thanks for the patch.

Acked-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org> #nvmem


--srini
>   drivers/nvmem/core.c                    |  3 ++-
>   drivers/pci/pci-sysfs.c                 |  2 +-
>   drivers/pci/vpd.c                       |  2 +-
>   drivers/platform/x86/amd/hsmp.c         |  2 +-
>   drivers/platform/x86/intel/sdsi.c       |  2 +-
>   drivers/scsi/scsi_sysfs.c               |  2 +-
>   drivers/usb/core/sysfs.c                |  2 +-
>   include/linux/sysfs.h                   | 30 +++++++++++++++---------------
>   12 files changed, 27 insertions(+), 26 deletions(-)
> 
> diff --git a/drivers/cxl/port.c b/drivers/cxl/port.c
> index 9dc394295e1fcd1610813837b2f515b66995eb25..24041cf85cfbe6c54c467ac325e48c775562b938 100644
> --- a/drivers/cxl/port.c
> +++ b/drivers/cxl/port.c
> @@ -173,7 +173,7 @@ static ssize_t CDAT_read(struct file *filp, struct kobject *kobj,
>   static BIN_ATTR_ADMIN_RO(CDAT, 0);
>   
>   static umode_t cxl_port_bin_attr_is_visible(struct kobject *kobj,
> -					    struct bin_attribute *attr, int i)
> +					    const struct bin_attribute *attr, int i)
>   {
>   	struct device *dev = kobj_to_dev(kobj);
>   	struct cxl_port *port = to_cxl_port(dev);
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c
> index 0b28b2cf1517d130da01989df70b9dff6433edc4..c1c329eb920b52af100a93bdf00df450e25608c4 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c
> @@ -3999,7 +3999,7 @@ static umode_t amdgpu_flash_attr_is_visible(struct kobject *kobj, struct attribu
>   }
>   
>   static umode_t amdgpu_bin_flash_attr_is_visible(struct kobject *kobj,
> -						struct bin_attribute *attr,
> +						const struct bin_attribute *attr,
>   						int idx)
>   {
>   	struct device *dev = kobj_to_dev(kobj);
> diff --git a/drivers/infiniband/hw/qib/qib_sysfs.c b/drivers/infiniband/hw/qib/qib_sysfs.c
> index 53ec7510e4ebfb144e79884ca7dd7d0c873bd8a7..ba2cd68b53e6c240f1afc65c64012c75ccf488e0 100644
> --- a/drivers/infiniband/hw/qib/qib_sysfs.c
> +++ b/drivers/infiniband/hw/qib/qib_sysfs.c
> @@ -283,7 +283,7 @@ static struct bin_attribute *port_ccmgta_attributes[] = {
>   };
>   
>   static umode_t qib_ccmgta_is_bin_visible(struct kobject *kobj,
> -				 struct bin_attribute *attr, int n)
> +				 const struct bin_attribute *attr, int n)
>   {
>   	struct qib_pportdata *ppd = qib_get_pportdata_kobj(kobj);
>   
> diff --git a/drivers/mtd/spi-nor/sysfs.c b/drivers/mtd/spi-nor/sysfs.c
> index 96064e4babf01f6950c81586764386e7671cbf97..5e9eb268073d18e0a46089000f18a3200b4bf13d 100644
> --- a/drivers/mtd/spi-nor/sysfs.c
> +++ b/drivers/mtd/spi-nor/sysfs.c
> @@ -87,7 +87,7 @@ static umode_t spi_nor_sysfs_is_visible(struct kobject *kobj,
>   }
>   
>   static umode_t spi_nor_sysfs_is_bin_visible(struct kobject *kobj,
> -					    struct bin_attribute *attr, int n)
> +					    const struct bin_attribute *attr, int n)
>   {
>   	struct spi_device *spi = to_spi_device(kobj_to_dev(kobj));
>   	struct spi_mem *spimem = spi_get_drvdata(spi);
> diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
> index 63370c76394ee9b8d514da074779617cef67c311..73e44d724f90f4cd8fe8cafb9fa0c0fb23078e61 100644
> --- a/drivers/nvmem/core.c
> +++ b/drivers/nvmem/core.c
> @@ -298,7 +298,8 @@ static umode_t nvmem_bin_attr_get_umode(struct nvmem_device *nvmem)
>   }
>   
>   static umode_t nvmem_bin_attr_is_visible(struct kobject *kobj,
> -					 struct bin_attribute *attr, int i)
> +					 const struct bin_attribute *attr,
> +					 int i)
>   {
>   	struct device *dev = kobj_to_dev(kobj);
>   	struct nvmem_device *nvmem = to_nvmem_device(dev);
> diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
> index 040f01b2b999175e8d98b05851edc078bbabbe0d..13912940ed2bb66c0086e5bea9a3cb6417ac14dd 100644
> --- a/drivers/pci/pci-sysfs.c
> +++ b/drivers/pci/pci-sysfs.c
> @@ -1326,7 +1326,7 @@ static struct bin_attribute *pci_dev_rom_attrs[] = {
>   };
>   
>   static umode_t pci_dev_rom_attr_is_visible(struct kobject *kobj,
> -					   struct bin_attribute *a, int n)
> +					   const struct bin_attribute *a, int n)
>   {
>   	struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
>   
> diff --git a/drivers/pci/vpd.c b/drivers/pci/vpd.c
> index e4300f5f304f3ca55a657fd25a1fa5ed919737a7..a469bcbc0da7f7677485c7f999f8dfb58b8ae8a3 100644
> --- a/drivers/pci/vpd.c
> +++ b/drivers/pci/vpd.c
> @@ -325,7 +325,7 @@ static struct bin_attribute *vpd_attrs[] = {
>   };
>   
>   static umode_t vpd_attr_is_visible(struct kobject *kobj,
> -				   struct bin_attribute *a, int n)
> +				   const struct bin_attribute *a, int n)
>   {
>   	struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
>   
> diff --git a/drivers/platform/x86/amd/hsmp.c b/drivers/platform/x86/amd/hsmp.c
> index 8fcf38eed7f00ee01aade6e3e55e20402458d5aa..8f00850c139fa8d419bc1c140c1832bf84b2c3bd 100644
> --- a/drivers/platform/x86/amd/hsmp.c
> +++ b/drivers/platform/x86/amd/hsmp.c
> @@ -620,7 +620,7 @@ static int hsmp_get_tbl_dram_base(u16 sock_ind)
>   }
>   
>   static umode_t hsmp_is_sock_attr_visible(struct kobject *kobj,
> -					 struct bin_attribute *battr, int id)
> +					 const struct bin_attribute *battr, int id)
>   {
>   	if (plat_dev.proto_ver == HSMP_PROTO_VER6)
>   		return battr->attr.mode;
> diff --git a/drivers/platform/x86/intel/sdsi.c b/drivers/platform/x86/intel/sdsi.c
> index 9d137621f0e6e7a23be0e0bbc6175c51c403169f..33f33b1070fdc949c1373251c3bca4234d9da119 100644
> --- a/drivers/platform/x86/intel/sdsi.c
> +++ b/drivers/platform/x86/intel/sdsi.c
> @@ -541,7 +541,7 @@ static struct bin_attribute *sdsi_bin_attrs[] = {
>   };
>   
>   static umode_t
> -sdsi_battr_is_visible(struct kobject *kobj, struct bin_attribute *attr, int n)
> +sdsi_battr_is_visible(struct kobject *kobj, const struct bin_attribute *attr, int n)
>   {
>   	struct device *dev = kobj_to_dev(kobj);
>   	struct sdsi_priv *priv = dev_get_drvdata(dev);
> diff --git a/drivers/scsi/scsi_sysfs.c b/drivers/scsi/scsi_sysfs.c
> index 32f94db6d6bf5d2bd289c1a121da7ffc6a7cb2ff..f3a1ecb42128a2b221ca5c362e041eb59dba0f20 100644
> --- a/drivers/scsi/scsi_sysfs.c
> +++ b/drivers/scsi/scsi_sysfs.c
> @@ -1274,7 +1274,7 @@ static umode_t scsi_sdev_attr_is_visible(struct kobject *kobj,
>   }
>   
>   static umode_t scsi_sdev_bin_attr_is_visible(struct kobject *kobj,
> -					     struct bin_attribute *attr, int i)
> +					     const struct bin_attribute *attr, int i)
>   {
>   	struct device *dev = kobj_to_dev(kobj);
>   	struct scsi_device *sdev = to_scsi_device(dev);
> diff --git a/drivers/usb/core/sysfs.c b/drivers/usb/core/sysfs.c
> index 61b6d978892c799e213018bed22d9fb12a19d429..b4cba23831acd2d7d395b9f7683cd3ee3a8623c8 100644
> --- a/drivers/usb/core/sysfs.c
> +++ b/drivers/usb/core/sysfs.c
> @@ -925,7 +925,7 @@ static struct bin_attribute *dev_bin_attrs[] = {
>   };
>   
>   static umode_t dev_bin_attrs_are_visible(struct kobject *kobj,
> -		struct bin_attribute *a, int n)
> +		const struct bin_attribute *a, int n)
>   {
>   	struct device *dev = kobj_to_dev(kobj);
>   	struct usb_device *udev = to_usb_device(dev);
> diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h
> index 4746cccb95898b24df6f53de9421ea7649b5568f..d1b22d56198b55ee39fe4c4fc994f5b753641992 100644
> --- a/include/linux/sysfs.h
> +++ b/include/linux/sysfs.h
> @@ -101,7 +101,7 @@ struct attribute_group {
>   	umode_t			(*is_visible)(struct kobject *,
>   					      struct attribute *, int);
>   	umode_t			(*is_bin_visible)(struct kobject *,
> -						  struct bin_attribute *, int);
> +						  const struct bin_attribute *, int);
>   	size_t			(*bin_size)(struct kobject *,
>   					    const struct bin_attribute *,
>   					    int);
> @@ -199,22 +199,22 @@ struct attribute_group {
>    * attributes, the group visibility is determined by the function
>    * specified to is_visible() not is_bin_visible()
>    */
> -#define DEFINE_SYSFS_BIN_GROUP_VISIBLE(name)                             \
> -	static inline umode_t sysfs_group_visible_##name(                \
> -		struct kobject *kobj, struct bin_attribute *attr, int n) \
> -	{                                                                \
> -		if (n == 0 && !name##_group_visible(kobj))               \
> -			return SYSFS_GROUP_INVISIBLE;                    \
> -		return name##_attr_visible(kobj, attr, n);               \
> +#define DEFINE_SYSFS_BIN_GROUP_VISIBLE(name)                                   \
> +	static inline umode_t sysfs_group_visible_##name(                      \
> +		struct kobject *kobj, const struct bin_attribute *attr, int n) \
> +	{                                                                      \
> +		if (n == 0 && !name##_group_visible(kobj))                     \
> +			return SYSFS_GROUP_INVISIBLE;                          \
> +		return name##_attr_visible(kobj, attr, n);                     \
>   	}
>   
> -#define DEFINE_SIMPLE_SYSFS_BIN_GROUP_VISIBLE(name)                   \
> -	static inline umode_t sysfs_group_visible_##name(             \
> -		struct kobject *kobj, struct bin_attribute *a, int n) \
> -	{                                                             \
> -		if (n == 0 && !name##_group_visible(kobj))            \
> -			return SYSFS_GROUP_INVISIBLE;                 \
> -		return a->mode;                                       \
> +#define DEFINE_SIMPLE_SYSFS_BIN_GROUP_VISIBLE(name)                         \
> +	static inline umode_t sysfs_group_visible_##name(                   \
> +		struct kobject *kobj, const struct bin_attribute *a, int n) \
> +	{                                                                   \
> +		if (n == 0 && !name##_group_visible(kobj))                  \
> +			return SYSFS_GROUP_INVISIBLE;                       \
> +		return a->mode;                                             \
>   	}
>   
>   #define SYSFS_GROUP_VISIBLE(fn) sysfs_group_visible_##fn
> 

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v2 04/10] nvmem: core: calculate bin_attribute size through bin_size()
  2024-11-03 17:03 ` [PATCH v2 04/10] nvmem: core: calculate " Thomas Weißschuh
@ 2024-11-08  9:58   ` Srinivas Kandagatla
  0 siblings, 0 replies; 30+ messages in thread
From: Srinivas Kandagatla @ 2024-11-08  9:58 UTC (permalink / raw)
  To: Thomas Weißschuh, Greg Kroah-Hartman, Rafael J. Wysocki,
	Bjorn Helgaas, Davidlohr Bueso, Jonathan Cameron, Dave Jiang,
	Alison Schofield, Vishal Verma, Ira Weiny, Alex Deucher,
	Christian König, Xinhui Pan, David Airlie, Simona Vetter,
	Dennis Dalessandro, Jason Gunthorpe, Leon Romanovsky,
	Tudor Ambarus, Pratyush Yadav, Michael Walle, Miquel Raynal,
	Richard Weinberger, Vignesh Raghavendra, Naveen Krishna Chatradhi,
	Carlos Bilbao, Hans de Goede, Ilpo Järvinen, David E. Box,
	James E.J. Bottomley, Martin K. Petersen, Richard Henderson,
	Matt Turner, Frederic Barrat, Andrew Donnellan, Arnd Bergmann,
	Logan Gunthorpe, K. Y. Srinivasan, Haiyang Zhang, Wei Liu,
	Dexuan Cui
  Cc: Dan Williams, linux-kernel, linux-pci, linux-cxl, amd-gfx,
	dri-devel, linux-rdma, linux-mtd, platform-driver-x86, linux-scsi,
	linux-usb, linux-alpha, linuxppc-dev, linux-hyperv



On 03/11/2024 17:03, Thomas Weißschuh wrote:
> Stop abusing the is_bin_visible() callback to calculate the attribute
> size. Instead use the new, dedicated bin_size() one.
> 
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> ---
Thanks for the patch,

LGTM.

Acked-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>

--srini

>   drivers/nvmem/core.c | 13 +++++++++++--
>   1 file changed, 11 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
> index 33ffa2aa4c1152398ec66b8dd7b30384c5346a6e..63370c76394ee9b8d514da074779617cef67c311 100644
> --- a/drivers/nvmem/core.c
> +++ b/drivers/nvmem/core.c
> @@ -303,11 +303,19 @@ static umode_t nvmem_bin_attr_is_visible(struct kobject *kobj,
>   	struct device *dev = kobj_to_dev(kobj);
>   	struct nvmem_device *nvmem = to_nvmem_device(dev);
>   
> -	attr->size = nvmem->size;
> -
>   	return nvmem_bin_attr_get_umode(nvmem);
>   }
>   
> +static size_t nvmem_bin_attr_size(struct kobject *kobj,
> +				  const struct bin_attribute *attr,
> +				  int i)
> +{
> +	struct device *dev = kobj_to_dev(kobj);
> +	struct nvmem_device *nvmem = to_nvmem_device(dev);
> +
> +	return nvmem->size;
> +}
> +
>   static umode_t nvmem_attr_is_visible(struct kobject *kobj,
>   				     struct attribute *attr, int i)
>   {
> @@ -383,6 +391,7 @@ static const struct attribute_group nvmem_bin_group = {
>   	.bin_attrs	= nvmem_bin_attributes,
>   	.attrs		= nvmem_attrs,
>   	.is_bin_visible = nvmem_bin_attr_is_visible,
> +	.bin_size	= nvmem_bin_attr_size,
>   	.is_visible	= nvmem_attr_is_visible,
>   };
>   
> 

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v2 09/10] sysfs: bin_attribute: add const read/write callback variants
  2024-11-03 17:03 ` [PATCH v2 09/10] sysfs: bin_attribute: add const read/write callback variants Thomas Weißschuh
@ 2024-12-03 16:06   ` James Bottomley
  2024-12-03 16:11     ` Thomas Weißschuh
  0 siblings, 1 reply; 30+ messages in thread
From: James Bottomley @ 2024-12-03 16:06 UTC (permalink / raw)
  To: linux
  Cc: James.Bottomley, Xinhui.Pan, airlied, ajd, alexander.deucher,
	alison.schofield, amd-gfx, arnd, bhelgaas, carlos.bilbao.osdev,
	christian.koenig, dan.j.williams, dave.jiang, dave, david.e.box,
	decui, dennis.dalessandro, dri-devel, fbarrat, gregkh, haiyangz,
	hdegoede, ilpo.jarvinen, ira.weiny, jgg, jonathan.cameron, kys,
	leon, linux-alpha, linux-cxl, linux-hyperv, linux-kernel,
	linux-mtd, linux-pci, linux-rdma, linux-scsi, linux-usb,
	linuxppc-dev, logang, martin.petersen, mattst88, miquel.raynal,
	mwalle, naveenkrishna.chatradhi, platform-driver-x86, pratyush,
	rafael, richard.henderson, richard, simona, srinivas.kandagatla,
	tudor.ambarus, vigneshr, vishal.l.verma, wei.liu

> diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h
> index
> d17c473c1ef292875475bf3bdf62d07241c13882..d713a6445a6267145a7014f308d
> f3bb25b8c3287 100644
> --- a/include/linux/sysfs.h
> +++ b/include/linux/sysfs.h
> @@ -305,8 +305,12 @@ struct bin_attribute {
>  	struct address_space *(*f_mapping)(void);
>  	ssize_t (*read)(struct file *, struct kobject *, struct
> bin_attribute *,
>  			char *, loff_t, size_t);
> +	ssize_t (*read_new)(struct file *, struct kobject *, const
> struct bin_attribute *,
> +			    char *, loff_t, size_t);
>  	ssize_t (*write)(struct file *, struct kobject *, struct
> bin_attribute *,
>  			 char *, loff_t, size_t);
> +	ssize_t (*write_new)(struct file *, struct kobject *,
> +			     const struct bin_attribute *, char *,
> loff_t, size_t);
>  	loff_t (*llseek)(struct file *, struct kobject *, const
> struct bin_attribute *,
>  			 loff_t, int);
>  	int (*mmap)(struct file *, struct kobject *, const struct
> bin_attribute *attr,
> @@ -325,11 +329,28 @@ struct bin_attribute {
>   */
>  #define sysfs_bin_attr_init(bin_attr) sysfs_attr_init(&(bin_attr)-
> >attr)
>  
> +typedef ssize_t __sysfs_bin_rw_handler_new(struct file *, struct
> kobject *,
> +					   const struct
> bin_attribute *, char *, loff_t, size_t);
> +
>  /* macros to create static binary attributes easier */
>  #define __BIN_ATTR(_name, _mode, _read, _write, _size)
> {		\
>  	.attr = { .name = __stringify(_name), .mode = _mode
> },		\
> -	.read	=
> _read,						\
> -	.write	=
> _write,						\
> +	.read =
> _Generic(_read,						\
> +		__sysfs_bin_rw_handler_new * :
> NULL,			\
> +		default :
> _read						\
> +	),							
> 	\
> +	.read_new =
> _Generic(_read,					\
> +		__sysfs_bin_rw_handler_new * :
> _read,			\
> +		default :
> NULL						\
> +	),							
> 	\
> +	.write =
> _Generic(_write,					\
> +		__sysfs_bin_rw_handler_new * :
> NULL,			\
> +		default :
> _write					\
> +	),							
> 	\
> +	.write_new =
> _Generic(_write,					\
> +		__sysfs_bin_rw_handler_new * :
> _write,			\
> +		default :
> NULL						\
> +	),							
> 	\
>  	.size	=
> _size,						\
>  }

It's probably a bit late now, but you've done this the wrong way
around.  What you should have done is added the const to .read/.write
then added a .read_old/.write_old with the original function prototype
and used _Generic() to switch between them.  Then when there are no
more non const left, you can simply remove .read_old and .write_old
without getting Linus annoyed by having to do something like this:

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=e70140ba0d2b1a30467d4af6bcfe761327b9ec95

Regards,

James


^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v2 09/10] sysfs: bin_attribute: add const read/write callback variants
  2024-12-03 16:06   ` James Bottomley
@ 2024-12-03 16:11     ` Thomas Weißschuh
  0 siblings, 0 replies; 30+ messages in thread
From: Thomas Weißschuh @ 2024-12-03 16:11 UTC (permalink / raw)
  To: James Bottomley
  Cc: Xinhui.Pan, airlied, ajd, alexander.deucher, alison.schofield,
	amd-gfx, arnd, bhelgaas, carlos.bilbao.osdev, christian.koenig,
	dan.j.williams, dave.jiang, dave, david.e.box, decui,
	dennis.dalessandro, dri-devel, fbarrat, gregkh, haiyangz,
	hdegoede, ilpo.jarvinen, ira.weiny, jgg, jonathan.cameron, kys,
	leon, linux-alpha, linux-cxl, linux-hyperv, linux-kernel,
	linux-mtd, linux-pci, linux-rdma, linux-scsi, linux-usb,
	linuxppc-dev, logang, martin.petersen, mattst88, miquel.raynal,
	mwalle, naveenkrishna.chatradhi, platform-driver-x86, pratyush,
	rafael, richard.henderson, richard, simona, srinivas.kandagatla,
	tudor.ambarus, vigneshr, vishal.l.verma, wei.liu

On 2024-12-03 11:06:16-0500, James Bottomley wrote:
> > diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h
> > index
> > d17c473c1ef292875475bf3bdf62d07241c13882..d713a6445a6267145a7014f308d
> > f3bb25b8c3287 100644
> > --- a/include/linux/sysfs.h
> > +++ b/include/linux/sysfs.h
> > @@ -305,8 +305,12 @@ struct bin_attribute {
> >  	struct address_space *(*f_mapping)(void);
> >  	ssize_t (*read)(struct file *, struct kobject *, struct
> > bin_attribute *,
> >  			char *, loff_t, size_t);
> > +	ssize_t (*read_new)(struct file *, struct kobject *, const
> > struct bin_attribute *,
> > +			    char *, loff_t, size_t);
> >  	ssize_t (*write)(struct file *, struct kobject *, struct
> > bin_attribute *,
> >  			 char *, loff_t, size_t);
> > +	ssize_t (*write_new)(struct file *, struct kobject *,
> > +			     const struct bin_attribute *, char *,
> > loff_t, size_t);
> >  	loff_t (*llseek)(struct file *, struct kobject *, const
> > struct bin_attribute *,
> >  			 loff_t, int);
> >  	int (*mmap)(struct file *, struct kobject *, const struct
> > bin_attribute *attr,
> > @@ -325,11 +329,28 @@ struct bin_attribute {
> >   */
> >  #define sysfs_bin_attr_init(bin_attr) sysfs_attr_init(&(bin_attr)-
> > >attr)
> >  
> > +typedef ssize_t __sysfs_bin_rw_handler_new(struct file *, struct
> > kobject *,
> > +					   const struct
> > bin_attribute *, char *, loff_t, size_t);
> > +
> >  /* macros to create static binary attributes easier */
> >  #define __BIN_ATTR(_name, _mode, _read, _write, _size)
> > {		\
> >  	.attr = { .name = __stringify(_name), .mode = _mode
> > },		\
> > -	.read	=
> > _read,						\
> > -	.write	=
> > _write,						\
> > +	.read =
> > _Generic(_read,						\
> > +		__sysfs_bin_rw_handler_new * :
> > NULL,			\
> > +		default :
> > _read						\
> > +	),							
> > 	\
> > +	.read_new =
> > _Generic(_read,					\
> > +		__sysfs_bin_rw_handler_new * :
> > _read,			\
> > +		default :
> > NULL						\
> > +	),							
> > 	\
> > +	.write =
> > _Generic(_write,					\
> > +		__sysfs_bin_rw_handler_new * :
> > NULL,			\
> > +		default :
> > _write					\
> > +	),							
> > 	\
> > +	.write_new =
> > _Generic(_write,					\
> > +		__sysfs_bin_rw_handler_new * :
> > _write,			\
> > +		default :
> > NULL						\
> > +	),							
> > 	\
> >  	.size	=
> > _size,						\
> >  }
> 
> It's probably a bit late now, but you've done this the wrong way
> around.  What you should have done is added the const to .read/.write
> then added a .read_old/.write_old with the original function prototype
> and used _Generic() to switch between them.  Then when there are no
> more non const left, you can simply remove .read_old and .write_old
> without getting Linus annoyed by having to do something like this:
> 
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=e70140ba0d2b1a30467d4af6bcfe761327b9ec95

Not all users are using the macros to define their attributes.
(Nor do they want to)

These users would break with your suggestion.
Otherwise I agree.


Thomas

^ permalink raw reply	[flat|nested] 30+ messages in thread

end of thread, other threads:[~2024-12-03 16:11 UTC | newest]

Thread overview: 30+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-11-03 17:03 [PATCH v2 00/10] sysfs: constify struct bin_attribute (Part 1) Thomas Weißschuh
2024-11-03 17:03 ` [PATCH v2 01/10] sysfs: explicitly pass size to sysfs_add_bin_file_mode_ns() Thomas Weißschuh
2024-11-03 17:03 ` [PATCH v2 02/10] sysfs: introduce callback attribute_group::bin_size Thomas Weißschuh
2024-11-05 16:12   ` Bjorn Helgaas
2024-11-06 19:27   ` Armin Wolf
2024-11-06 20:05   ` Krzysztof Wilczyński
2024-11-07  5:21     ` Greg Kroah-Hartman
2024-11-07 15:50       ` Krzysztof Wilczyński
2024-11-03 17:03 ` [PATCH v2 03/10] PCI/sysfs: Calculate bin_attribute size through bin_size() Thomas Weißschuh
2024-11-07 16:27   ` Bjorn Helgaas
2024-11-03 17:03 ` [PATCH v2 04/10] nvmem: core: calculate " Thomas Weißschuh
2024-11-08  9:58   ` Srinivas Kandagatla
2024-11-03 17:03 ` [PATCH v2 05/10] sysfs: treewide: constify attribute callback of bin_is_visible() Thomas Weißschuh
2024-11-04 13:25   ` Ilpo Järvinen
2024-11-04 13:52   ` Jason Gunthorpe
2024-11-04 14:56   ` Ira Weiny
2024-11-05  1:25   ` Martin K. Petersen
2024-11-05 15:26   ` Bjorn Helgaas
2024-11-07 17:20   ` Pratyush Yadav
2024-11-08  9:57   ` Srinivas Kandagatla
2024-11-03 17:03 ` [PATCH v2 06/10] sysfs: treewide: constify attribute callback of bin_attribute::mmap() Thomas Weißschuh
2024-11-04  1:24   ` Andrew Donnellan
2024-11-03 17:03 ` [PATCH v2 07/10] sysfs: treewide: constify attribute callback of bin_attribute::llseek() Thomas Weißschuh
2024-11-03 17:03 ` [PATCH v2 08/10] sysfs: implement all BIN_ATTR_* macros in terms of __BIN_ATTR() Thomas Weißschuh
2024-11-03 17:03 ` [PATCH v2 09/10] sysfs: bin_attribute: add const read/write callback variants Thomas Weißschuh
2024-12-03 16:06   ` James Bottomley
2024-12-03 16:11     ` Thomas Weißschuh
2024-11-03 17:03 ` [PATCH v2 10/10] driver core: Constify attribute arguments of binary attributes Thomas Weißschuh
2024-11-03 20:02 ` [PATCH v2 00/10] sysfs: constify struct bin_attribute (Part 1) Krzysztof Wilczyński
2024-11-05 16:15 ` Bjorn Helgaas

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).