All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH RFC 00/10] sysfs: constify struct bin_attribute (Part 1)
@ 2024-10-31  2:43 Thomas Weißschuh
  2024-10-31  2:43 ` [PATCH RFC 01/10] sysfs: explicitly pass size to sysfs_add_bin_file_mode_ns() Thomas Weißschuh
                   ` (10 more replies)
  0 siblings, 11 replies; 14+ messages in thread
From: Thomas Weißschuh @ 2024-10-31  2:43 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki
  Cc: Dan Williams, linux-kernel, 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.

This series is marked as RFC and only sent to the sysfs maintainers to
get some feedback on the general aproach.
The same techniques employed by this series can later be reused for the
same change for 'struct attribute'.

Signed-off-by: Thomas Weißschuh <linux@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: drop callback 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

 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                 |  36 +++++++-----
 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                         |  32 +++++-----
 fs/sysfs/group.c                        |   5 +-
 fs/sysfs/sysfs.h                        |   2 +-
 include/linux/sysfs.h                   | 100 +++++++++++++++++++-------------
 21 files changed, 132 insertions(+), 93 deletions(-)
---
base-commit: e42b1a9a2557aa94fee47f078633677198386a52
change-id: 20241028-sysfs-const-bin_attr-a00896481d0b

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


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

* [PATCH RFC 01/10] sysfs: explicitly pass size to sysfs_add_bin_file_mode_ns()
  2024-10-31  2:43 [PATCH RFC 00/10] sysfs: constify struct bin_attribute (Part 1) Thomas Weißschuh
@ 2024-10-31  2:43 ` Thomas Weißschuh
  2024-10-31  2:43 ` [PATCH RFC 02/10] sysfs: introduce callback attribute_group::bin_size Thomas Weißschuh
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 14+ messages in thread
From: Thomas Weißschuh @ 2024-10-31  2:43 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki
  Cc: Dan Williams, linux-kernel, 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] 14+ messages in thread

* [PATCH RFC 02/10] sysfs: introduce callback attribute_group::bin_size
  2024-10-31  2:43 [PATCH RFC 00/10] sysfs: constify struct bin_attribute (Part 1) Thomas Weißschuh
  2024-10-31  2:43 ` [PATCH RFC 01/10] sysfs: explicitly pass size to sysfs_add_bin_file_mode_ns() Thomas Weißschuh
@ 2024-10-31  2:43 ` Thomas Weißschuh
  2024-10-31  2:43 ` [PATCH RFC 03/10] PCI/sysfs: Calculate bin_attribute size through bin_size() Thomas Weißschuh
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 14+ messages in thread
From: Thomas Weißschuh @ 2024-10-31  2:43 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki
  Cc: Dan Williams, linux-kernel, 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] 14+ messages in thread

* [PATCH RFC 03/10] PCI/sysfs: Calculate bin_attribute size through bin_size()
  2024-10-31  2:43 [PATCH RFC 00/10] sysfs: constify struct bin_attribute (Part 1) Thomas Weißschuh
  2024-10-31  2:43 ` [PATCH RFC 01/10] sysfs: explicitly pass size to sysfs_add_bin_file_mode_ns() Thomas Weißschuh
  2024-10-31  2:43 ` [PATCH RFC 02/10] sysfs: introduce callback attribute_group::bin_size Thomas Weißschuh
@ 2024-10-31  2:43 ` Thomas Weißschuh
  2024-10-31  2:43 ` [PATCH RFC 04/10] nvmem: core: calculate " Thomas Weißschuh
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 14+ messages in thread
From: Thomas Weißschuh @ 2024-10-31  2:43 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki
  Cc: Dan Williams, linux-kernel, 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] 14+ messages in thread

* [PATCH RFC 04/10] nvmem: core: calculate bin_attribute size through bin_size()
  2024-10-31  2:43 [PATCH RFC 00/10] sysfs: constify struct bin_attribute (Part 1) Thomas Weißschuh
                   ` (2 preceding siblings ...)
  2024-10-31  2:43 ` [PATCH RFC 03/10] PCI/sysfs: Calculate bin_attribute size through bin_size() Thomas Weißschuh
@ 2024-10-31  2:43 ` Thomas Weißschuh
  2024-10-31  2:43 ` [PATCH RFC 05/10] sysfs: treewide: constify attribute callback of bin_is_visible() Thomas Weißschuh
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 14+ messages in thread
From: Thomas Weißschuh @ 2024-10-31  2:43 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki
  Cc: Dan Williams, linux-kernel, 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] 14+ messages in thread

* [PATCH RFC 05/10] sysfs: treewide: constify attribute callback of bin_is_visible()
  2024-10-31  2:43 [PATCH RFC 00/10] sysfs: constify struct bin_attribute (Part 1) Thomas Weißschuh
                   ` (3 preceding siblings ...)
  2024-10-31  2:43 ` [PATCH RFC 04/10] nvmem: core: calculate " Thomas Weißschuh
@ 2024-10-31  2:43 ` Thomas Weißschuh
  2024-10-31  2:43 ` [PATCH RFC 06/10] sysfs: treewide: constify attribute callback of bin_attribute::mmap() Thomas Weißschuh
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 14+ messages in thread
From: Thomas Weißschuh @ 2024-10-31  2:43 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki
  Cc: Dan Williams, linux-kernel, 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 861dde65768fe383d3ccbb49ffd31fb29aeb42e9..ed78043ec13a6f6db5f077a5679e0342ce7f7eff 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] 14+ messages in thread

* [PATCH RFC 06/10] sysfs: treewide: constify attribute callback of bin_attribute::mmap()
  2024-10-31  2:43 [PATCH RFC 00/10] sysfs: constify struct bin_attribute (Part 1) Thomas Weißschuh
                   ` (4 preceding siblings ...)
  2024-10-31  2:43 ` [PATCH RFC 05/10] sysfs: treewide: constify attribute callback of bin_is_visible() Thomas Weißschuh
@ 2024-10-31  2:43 ` Thomas Weißschuh
  2024-10-31  2:43 ` [PATCH RFC 07/10] sysfs: drop callback bin_attribute::llseek Thomas Weißschuh
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 14+ messages in thread
From: Thomas Weißschuh @ 2024-10-31  2:43 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki
  Cc: Dan Williams, linux-kernel, 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>
---
 drivers/misc/ocxl/sysfs.c              | 2 +-
 drivers/pci/p2pdma.c                   | 2 +-
 drivers/pci/pci-sysfs.c                | 6 +++---
 drivers/platform/x86/intel/pmt/class.c | 2 +-
 drivers/uio/uio_hv_generic.c           | 2 +-
 include/linux/sysfs.h                  | 2 +-
 6 files changed, 8 insertions(+), 8 deletions(-)

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..36017fd1d4235d0a42fa91b47a3c6de82d9af978 100644
--- a/drivers/pci/pci-sysfs.c
+++ b/drivers/pci/pci-sysfs.c
@@ -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] 14+ messages in thread

* [PATCH RFC 07/10] sysfs: drop callback bin_attribute::llseek
  2024-10-31  2:43 [PATCH RFC 00/10] sysfs: constify struct bin_attribute (Part 1) Thomas Weißschuh
                   ` (5 preceding siblings ...)
  2024-10-31  2:43 ` [PATCH RFC 06/10] sysfs: treewide: constify attribute callback of bin_attribute::mmap() Thomas Weißschuh
@ 2024-10-31  2:43 ` Thomas Weißschuh
  2024-11-01  5:17   ` kernel test robot
  2024-11-01  5:27   ` kernel test robot
  2024-10-31  2:43 ` [PATCH RFC 08/10] sysfs: implement all BIN_ATTR_* macros in terms of __BIN_ATTR() Thomas Weißschuh
                   ` (3 subsequent siblings)
  10 siblings, 2 replies; 14+ messages in thread
From: Thomas Weißschuh @ 2024-10-31  2:43 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki
  Cc: Dan Williams, linux-kernel, Thomas Weißschuh

The callback is never implemented, drop it.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
 fs/sysfs/file.c       | 8 +-------
 include/linux/sysfs.h | 2 --
 2 files changed, 1 insertion(+), 9 deletions(-)

diff --git a/fs/sysfs/file.c b/fs/sysfs/file.c
index 6d39696b43069010b0ad0bdaadcf9002cb70c92c..3515c172ec8ff70b87847d226a1b3bc3b60826f9 100644
--- a/fs/sysfs/file.c
+++ b/fs/sysfs/file.c
@@ -170,13 +170,7 @@ static int sysfs_kf_bin_mmap(struct kernfs_open_file *of,
 static loff_t sysfs_kf_bin_llseek(struct kernfs_open_file *of, loff_t offset,
 				  int whence)
 {
-	struct bin_attribute *battr = of->kn->priv;
-	struct kobject *kobj = of->kn->parent->priv;
-
-	if (battr->llseek)
-		return battr->llseek(of->file, kobj, battr, offset, whence);
-	else
-		return generic_file_llseek(of->file, offset, whence);
+	return generic_file_llseek(of->file, offset, whence);
 }
 
 static int sysfs_kf_bin_open(struct kernfs_open_file *of)
diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h
index 9fcdc8cd3118f359742bfd8b708d5c3eff511042..8344c0198c61cc44995c38d46d926360e7a88873 100644
--- a/include/linux/sysfs.h
+++ b/include/linux/sysfs.h
@@ -307,8 +307,6 @@ 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, 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] 14+ messages in thread

* [PATCH RFC 08/10] sysfs: implement all BIN_ATTR_* macros in terms of __BIN_ATTR()
  2024-10-31  2:43 [PATCH RFC 00/10] sysfs: constify struct bin_attribute (Part 1) Thomas Weißschuh
                   ` (6 preceding siblings ...)
  2024-10-31  2:43 ` [PATCH RFC 07/10] sysfs: drop callback bin_attribute::llseek Thomas Weißschuh
@ 2024-10-31  2:43 ` Thomas Weißschuh
  2024-10-31  2:43 ` [PATCH RFC 09/10] sysfs: bin_attribute: add const read/write callback variants Thomas Weißschuh
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 14+ messages in thread
From: Thomas Weißschuh @ 2024-10-31  2:43 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki
  Cc: Dan Williams, linux-kernel, 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 8344c0198c61cc44995c38d46d926360e7a88873..5ece63c83ba5829b6eb0f115bdea12a1412ae039 100644
--- a/include/linux/sysfs.h
+++ b/include/linux/sysfs.h
@@ -331,17 +331,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)
@@ -362,11 +356,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)
@@ -377,10 +368,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] 14+ messages in thread

* [PATCH RFC 09/10] sysfs: bin_attribute: add const read/write callback variants
  2024-10-31  2:43 [PATCH RFC 00/10] sysfs: constify struct bin_attribute (Part 1) Thomas Weißschuh
                   ` (7 preceding siblings ...)
  2024-10-31  2:43 ` [PATCH RFC 08/10] sysfs: implement all BIN_ATTR_* macros in terms of __BIN_ATTR() Thomas Weißschuh
@ 2024-10-31  2:43 ` Thomas Weißschuh
  2024-10-31  2:43 ` [PATCH RFC 10/10] driver core: Constify attribute arguments of binary attributes Thomas Weißschuh
  2024-11-01  1:20 ` [PATCH RFC 00/10] sysfs: constify struct bin_attribute (Part 1) Greg Kroah-Hartman
  10 siblings, 0 replies; 14+ messages in thread
From: Thomas Weißschuh @ 2024-10-31  2:43 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki
  Cc: Dan Williams, linux-kernel, 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       | 16 +++++++++++-----
 include/linux/sysfs.h | 31 +++++++++++++++++++++++++++++--
 2 files changed, 40 insertions(+), 7 deletions(-)

diff --git a/fs/sysfs/file.c b/fs/sysfs/file.c
index 3515c172ec8ff70b87847d226a1b3bc3b60826f9..58a98b4dc9b28dab9e36c831581608eaf6f95848 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);
 }
 
@@ -319,11 +325,11 @@ int sysfs_add_bin_file_mode_ns(struct kernfs_node *parent,
 
 	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 5ece63c83ba5829b6eb0f115bdea12a1412ae039..34e94e75231166839452b89aa1114d90657257d1 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);
 	int (*mmap)(struct file *, struct kobject *, const struct bin_attribute *attr,
 		    struct vm_area_struct *vma);
 };
@@ -323,11 +327,34 @@ struct bin_attribute {
  */
 #define sysfs_bin_attr_init(bin_attr) sysfs_attr_init(&(bin_attr)->attr)
 
+typedef ssize_t __sysfs_rw_handler(struct file *, struct kobject *,
+				   struct bin_attribute *, char *, loff_t, size_t);
+typedef ssize_t __sysfs_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_rw_handler * : _read,				\
+		__sysfs_rw_handler_new * : NULL,			\
+		void * : NULL						\
+	),								\
+	.read_new = _Generic(_read,					\
+		__sysfs_rw_handler * : NULL,				\
+		__sysfs_rw_handler_new * : _read,			\
+		void * : NULL						\
+	),								\
+	.write = _Generic(_write,					\
+		__sysfs_rw_handler * : _write,				\
+		__sysfs_rw_handler_new * : NULL,			\
+		void * : NULL						\
+	),								\
+	.write_new = _Generic(_write,					\
+		__sysfs_rw_handler * : NULL,				\
+		__sysfs_rw_handler_new * : _write,			\
+		void * : NULL						\
+	),								\
 	.size	= _size,						\
 }
 

-- 
2.47.0


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

* [PATCH RFC 10/10] driver core: Constify attribute arguments of binary attributes
  2024-10-31  2:43 [PATCH RFC 00/10] sysfs: constify struct bin_attribute (Part 1) Thomas Weißschuh
                   ` (8 preceding siblings ...)
  2024-10-31  2:43 ` [PATCH RFC 09/10] sysfs: bin_attribute: add const read/write callback variants Thomas Weißschuh
@ 2024-10-31  2:43 ` Thomas Weißschuh
  2024-11-01  1:20 ` [PATCH RFC 00/10] sysfs: constify struct bin_attribute (Part 1) Greg Kroah-Hartman
  10 siblings, 0 replies; 14+ messages in thread
From: Thomas Weißschuh @ 2024-10-31  2:43 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki
  Cc: Dan Williams, linux-kernel, 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] 14+ messages in thread

* Re: [PATCH RFC 00/10] sysfs: constify struct bin_attribute (Part 1)
  2024-10-31  2:43 [PATCH RFC 00/10] sysfs: constify struct bin_attribute (Part 1) Thomas Weißschuh
                   ` (9 preceding siblings ...)
  2024-10-31  2:43 ` [PATCH RFC 10/10] driver core: Constify attribute arguments of binary attributes Thomas Weißschuh
@ 2024-11-01  1:20 ` Greg Kroah-Hartman
  10 siblings, 0 replies; 14+ messages in thread
From: Greg Kroah-Hartman @ 2024-11-01  1:20 UTC (permalink / raw)
  To: Thomas Weißschuh; +Cc: Rafael J. Wysocki, Dan Williams, linux-kernel

On Thu, Oct 31, 2024 at 02:43:49AM +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...)
> 
> 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.
> 
> This series is marked as RFC and only sent to the sysfs maintainers to
> get some feedback on the general aproach.
> The same techniques employed by this series can later be reused for the
> same change for 'struct attribute'.
> 
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>

At a quick glance, this is great!  I'll review it "better" next week
when my travel calms down, so if you want to resend this as a non-rfc
patch, and it looks sane, I'll be glad to queue it up.

thanks!

greg k-h

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

* Re: [PATCH RFC 07/10] sysfs: drop callback bin_attribute::llseek
  2024-10-31  2:43 ` [PATCH RFC 07/10] sysfs: drop callback bin_attribute::llseek Thomas Weißschuh
@ 2024-11-01  5:17   ` kernel test robot
  2024-11-01  5:27   ` kernel test robot
  1 sibling, 0 replies; 14+ messages in thread
From: kernel test robot @ 2024-11-01  5:17 UTC (permalink / raw)
  To: Thomas Weißschuh; +Cc: llvm, oe-kbuild-all

Hi Thomas,

[This is a private test report for your RFC patch.]
kernel test robot noticed the following build errors:

[auto build test ERROR on e42b1a9a2557aa94fee47f078633677198386a52]

url:    https://github.com/intel-lab-lkp/linux/commits/Thomas-Wei-schuh/sysfs-explicitly-pass-size-to-sysfs_add_bin_file_mode_ns/20241031-104710
base:   e42b1a9a2557aa94fee47f078633677198386a52
patch link:    https://lore.kernel.org/r/20241031-sysfs-const-bin_attr-v1-7-2281afa7f055%40weissschuh.net
patch subject: [PATCH RFC 07/10] sysfs: drop callback bin_attribute::llseek
config: x86_64-kexec (https://download.01.org/0day-ci/archive/20241101/202411011352.bJ3MT1ra-lkp@intel.com/config)
compiler: clang version 19.1.3 (https://github.com/llvm/llvm-project ab51eccf88f5321e7c60591c5546b254b6afab99)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241101/202411011352.bJ3MT1ra-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202411011352.bJ3MT1ra-lkp@intel.com/

All errors (new ones prefixed by >>):

   In file included from drivers/pci/pci-sysfs.c:18:
   In file included from include/linux/pci.h:1650:
   In file included from include/linux/dmapool.h:14:
   In file included from include/linux/scatterlist.h:8:
   In file included from include/linux/mm.h:2213:
   include/linux/vmstat.h:504:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]
     504 |         return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~ ^
     505 |                            item];
         |                            ~~~~
   include/linux/vmstat.h:511:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]
     511 |         return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~ ^
     512 |                            NR_VM_NUMA_EVENT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~~
   include/linux/vmstat.h:518:36: warning: arithmetic between different enumeration types ('enum node_stat_item' and 'enum lru_list') [-Wenum-enum-conversion]
     518 |         return node_stat_name(NR_LRU_BASE + lru) + 3; // skip "nr_"
         |                               ~~~~~~~~~~~ ^ ~~~
   include/linux/vmstat.h:524:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]
     524 |         return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~ ^
     525 |                            NR_VM_NUMA_EVENT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~~
>> drivers/pci/pci-sysfs.c:1202:13: error: no member named 'llseek' in 'struct bin_attribute'
    1202 |                 res_attr->llseek = pci_llseek_resource;
         |                 ~~~~~~~~  ^
   4 warnings and 1 error generated.


vim +1202 drivers/pci/pci-sysfs.c

45aec1ae72fc59 venkatesh.pallipadi@intel.com 2008-03-18  1165  
45aec1ae72fc59 venkatesh.pallipadi@intel.com 2008-03-18  1166  static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
45aec1ae72fc59 venkatesh.pallipadi@intel.com 2008-03-18  1167  {
45aec1ae72fc59 venkatesh.pallipadi@intel.com 2008-03-18  1168  	/* allocate attribute structure, piggyback attribute name */
45aec1ae72fc59 venkatesh.pallipadi@intel.com 2008-03-18  1169  	int name_len = write_combine ? 13 : 10;
45aec1ae72fc59 venkatesh.pallipadi@intel.com 2008-03-18  1170  	struct bin_attribute *res_attr;
bd5174dfb6f171 Bjorn Helgaas                 2016-03-10  1171  	char *res_attr_name;
45aec1ae72fc59 venkatesh.pallipadi@intel.com 2008-03-18  1172  	int retval;
45aec1ae72fc59 venkatesh.pallipadi@intel.com 2008-03-18  1173  
45aec1ae72fc59 venkatesh.pallipadi@intel.com 2008-03-18  1174  	res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
bd5174dfb6f171 Bjorn Helgaas                 2016-03-10  1175  	if (!res_attr)
bd5174dfb6f171 Bjorn Helgaas                 2016-03-10  1176  		return -ENOMEM;
bd5174dfb6f171 Bjorn Helgaas                 2016-03-10  1177  
bd5174dfb6f171 Bjorn Helgaas                 2016-03-10  1178  	res_attr_name = (char *)(res_attr + 1);
45aec1ae72fc59 venkatesh.pallipadi@intel.com 2008-03-18  1179  
a07e4156a2ee63 Eric W. Biederman             2010-02-11  1180  	sysfs_bin_attr_init(res_attr);
45aec1ae72fc59 venkatesh.pallipadi@intel.com 2008-03-18  1181  	if (write_combine) {
45aec1ae72fc59 venkatesh.pallipadi@intel.com 2008-03-18  1182  		sprintf(res_attr_name, "resource%d_wc", num);
45aec1ae72fc59 venkatesh.pallipadi@intel.com 2008-03-18  1183  		res_attr->mmap = pci_mmap_resource_wc;
45aec1ae72fc59 venkatesh.pallipadi@intel.com 2008-03-18  1184  	} else {
45aec1ae72fc59 venkatesh.pallipadi@intel.com 2008-03-18  1185  		sprintf(res_attr_name, "resource%d", num);
8633328be24267 Alex Williamson               2010-07-19  1186  		if (pci_resource_flags(pdev, num) & IORESOURCE_IO) {
8633328be24267 Alex Williamson               2010-07-19  1187  			res_attr->read = pci_read_resource_io;
8633328be24267 Alex Williamson               2010-07-19  1188  			res_attr->write = pci_write_resource_io;
e854d8b2a82ef7 David Woodhouse               2017-04-12  1189  			if (arch_can_pci_mmap_io())
e854d8b2a82ef7 David Woodhouse               2017-04-12  1190  				res_attr->mmap = pci_mmap_resource_uc;
e854d8b2a82ef7 David Woodhouse               2017-04-12  1191  		} else {
e854d8b2a82ef7 David Woodhouse               2017-04-12  1192  			res_attr->mmap = pci_mmap_resource_uc;
e854d8b2a82ef7 David Woodhouse               2017-04-12  1193  		}
8633328be24267 Alex Williamson               2010-07-19  1194  	}
24de09c16f974d Valentine Sinitsyn            2023-09-25  1195  	if (res_attr->mmap) {
f06aff924f9758 Krzysztof Wilczyński          2021-07-29  1196  		res_attr->f_mapping = iomem_get_mapping;
24de09c16f974d Valentine Sinitsyn            2023-09-25  1197  		/*
24de09c16f974d Valentine Sinitsyn            2023-09-25  1198  		 * generic_file_llseek() consults f_mapping->host to determine
24de09c16f974d Valentine Sinitsyn            2023-09-25  1199  		 * the file size. As iomem_inode knows nothing about the
24de09c16f974d Valentine Sinitsyn            2023-09-25  1200  		 * attribute, it's not going to work, so override it as well.
24de09c16f974d Valentine Sinitsyn            2023-09-25  1201  		 */
24de09c16f974d Valentine Sinitsyn            2023-09-25 @1202  		res_attr->llseek = pci_llseek_resource;
24de09c16f974d Valentine Sinitsyn            2023-09-25  1203  	}
45aec1ae72fc59 venkatesh.pallipadi@intel.com 2008-03-18  1204  	res_attr->attr.name = res_attr_name;
e2154044dd4168 Kelsey Skunberg               2019-08-13  1205  	res_attr->attr.mode = 0600;
45aec1ae72fc59 venkatesh.pallipadi@intel.com 2008-03-18  1206  	res_attr->size = pci_resource_len(pdev, num);
dca40b186b757c David Woodhouse               2017-04-12  1207  	res_attr->private = (void *)(unsigned long)num;
45aec1ae72fc59 venkatesh.pallipadi@intel.com 2008-03-18  1208  	retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
aa382ffa705bea Sascha Hauer                  2022-11-08  1209  	if (retval) {
b562ec8f74e4ed Bjorn Helgaas                 2016-03-10  1210  		kfree(res_attr);
45aec1ae72fc59 venkatesh.pallipadi@intel.com 2008-03-18  1211  		return retval;
b19441af185559 Greg Kroah-Hartman            2006-08-28  1212  	}
b19441af185559 Greg Kroah-Hartman            2006-08-28  1213  
aa382ffa705bea Sascha Hauer                  2022-11-08  1214  	if (write_combine)
aa382ffa705bea Sascha Hauer                  2022-11-08  1215  		pdev->res_attr_wc[num] = res_attr;
aa382ffa705bea Sascha Hauer                  2022-11-08  1216  	else
aa382ffa705bea Sascha Hauer                  2022-11-08  1217  		pdev->res_attr[num] = res_attr;
aa382ffa705bea Sascha Hauer                  2022-11-08  1218  
aa382ffa705bea Sascha Hauer                  2022-11-08  1219  	return 0;
aa382ffa705bea Sascha Hauer                  2022-11-08  1220  }
aa382ffa705bea Sascha Hauer                  2022-11-08  1221  

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

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

* Re: [PATCH RFC 07/10] sysfs: drop callback bin_attribute::llseek
  2024-10-31  2:43 ` [PATCH RFC 07/10] sysfs: drop callback bin_attribute::llseek Thomas Weißschuh
  2024-11-01  5:17   ` kernel test robot
@ 2024-11-01  5:27   ` kernel test robot
  1 sibling, 0 replies; 14+ messages in thread
From: kernel test robot @ 2024-11-01  5:27 UTC (permalink / raw)
  To: Thomas Weißschuh; +Cc: oe-kbuild-all

Hi Thomas,

[This is a private test report for your RFC patch.]
kernel test robot noticed the following build errors:

[auto build test ERROR on e42b1a9a2557aa94fee47f078633677198386a52]

url:    https://github.com/intel-lab-lkp/linux/commits/Thomas-Wei-schuh/sysfs-explicitly-pass-size-to-sysfs_add_bin_file_mode_ns/20241031-104710
base:   e42b1a9a2557aa94fee47f078633677198386a52
patch link:    https://lore.kernel.org/r/20241031-sysfs-const-bin_attr-v1-7-2281afa7f055%40weissschuh.net
patch subject: [PATCH RFC 07/10] sysfs: drop callback bin_attribute::llseek
config: x86_64-rhel-8.3 (https://download.01.org/0day-ci/archive/20241101/202411011313.7WUAqR1h-lkp@intel.com/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241101/202411011313.7WUAqR1h-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202411011313.7WUAqR1h-lkp@intel.com/

All errors (new ones prefixed by >>):

   drivers/pci/pci-sysfs.c: In function 'pci_create_attr':
>> drivers/pci/pci-sysfs.c:1202:25: error: 'struct bin_attribute' has no member named 'llseek'
    1202 |                 res_attr->llseek = pci_llseek_resource;
         |                         ^~


vim +1202 drivers/pci/pci-sysfs.c

45aec1ae72fc59 venkatesh.pallipadi@intel.com 2008-03-18  1165  
45aec1ae72fc59 venkatesh.pallipadi@intel.com 2008-03-18  1166  static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
45aec1ae72fc59 venkatesh.pallipadi@intel.com 2008-03-18  1167  {
45aec1ae72fc59 venkatesh.pallipadi@intel.com 2008-03-18  1168  	/* allocate attribute structure, piggyback attribute name */
45aec1ae72fc59 venkatesh.pallipadi@intel.com 2008-03-18  1169  	int name_len = write_combine ? 13 : 10;
45aec1ae72fc59 venkatesh.pallipadi@intel.com 2008-03-18  1170  	struct bin_attribute *res_attr;
bd5174dfb6f171 Bjorn Helgaas                 2016-03-10  1171  	char *res_attr_name;
45aec1ae72fc59 venkatesh.pallipadi@intel.com 2008-03-18  1172  	int retval;
45aec1ae72fc59 venkatesh.pallipadi@intel.com 2008-03-18  1173  
45aec1ae72fc59 venkatesh.pallipadi@intel.com 2008-03-18  1174  	res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
bd5174dfb6f171 Bjorn Helgaas                 2016-03-10  1175  	if (!res_attr)
bd5174dfb6f171 Bjorn Helgaas                 2016-03-10  1176  		return -ENOMEM;
bd5174dfb6f171 Bjorn Helgaas                 2016-03-10  1177  
bd5174dfb6f171 Bjorn Helgaas                 2016-03-10  1178  	res_attr_name = (char *)(res_attr + 1);
45aec1ae72fc59 venkatesh.pallipadi@intel.com 2008-03-18  1179  
a07e4156a2ee63 Eric W. Biederman             2010-02-11  1180  	sysfs_bin_attr_init(res_attr);
45aec1ae72fc59 venkatesh.pallipadi@intel.com 2008-03-18  1181  	if (write_combine) {
45aec1ae72fc59 venkatesh.pallipadi@intel.com 2008-03-18  1182  		sprintf(res_attr_name, "resource%d_wc", num);
45aec1ae72fc59 venkatesh.pallipadi@intel.com 2008-03-18  1183  		res_attr->mmap = pci_mmap_resource_wc;
45aec1ae72fc59 venkatesh.pallipadi@intel.com 2008-03-18  1184  	} else {
45aec1ae72fc59 venkatesh.pallipadi@intel.com 2008-03-18  1185  		sprintf(res_attr_name, "resource%d", num);
8633328be24267 Alex Williamson               2010-07-19  1186  		if (pci_resource_flags(pdev, num) & IORESOURCE_IO) {
8633328be24267 Alex Williamson               2010-07-19  1187  			res_attr->read = pci_read_resource_io;
8633328be24267 Alex Williamson               2010-07-19  1188  			res_attr->write = pci_write_resource_io;
e854d8b2a82ef7 David Woodhouse               2017-04-12  1189  			if (arch_can_pci_mmap_io())
e854d8b2a82ef7 David Woodhouse               2017-04-12  1190  				res_attr->mmap = pci_mmap_resource_uc;
e854d8b2a82ef7 David Woodhouse               2017-04-12  1191  		} else {
e854d8b2a82ef7 David Woodhouse               2017-04-12  1192  			res_attr->mmap = pci_mmap_resource_uc;
e854d8b2a82ef7 David Woodhouse               2017-04-12  1193  		}
8633328be24267 Alex Williamson               2010-07-19  1194  	}
24de09c16f974d Valentine Sinitsyn            2023-09-25  1195  	if (res_attr->mmap) {
f06aff924f9758 Krzysztof Wilczyński          2021-07-29  1196  		res_attr->f_mapping = iomem_get_mapping;
24de09c16f974d Valentine Sinitsyn            2023-09-25  1197  		/*
24de09c16f974d Valentine Sinitsyn            2023-09-25  1198  		 * generic_file_llseek() consults f_mapping->host to determine
24de09c16f974d Valentine Sinitsyn            2023-09-25  1199  		 * the file size. As iomem_inode knows nothing about the
24de09c16f974d Valentine Sinitsyn            2023-09-25  1200  		 * attribute, it's not going to work, so override it as well.
24de09c16f974d Valentine Sinitsyn            2023-09-25  1201  		 */
24de09c16f974d Valentine Sinitsyn            2023-09-25 @1202  		res_attr->llseek = pci_llseek_resource;
24de09c16f974d Valentine Sinitsyn            2023-09-25  1203  	}
45aec1ae72fc59 venkatesh.pallipadi@intel.com 2008-03-18  1204  	res_attr->attr.name = res_attr_name;
e2154044dd4168 Kelsey Skunberg               2019-08-13  1205  	res_attr->attr.mode = 0600;
45aec1ae72fc59 venkatesh.pallipadi@intel.com 2008-03-18  1206  	res_attr->size = pci_resource_len(pdev, num);
dca40b186b757c David Woodhouse               2017-04-12  1207  	res_attr->private = (void *)(unsigned long)num;
45aec1ae72fc59 venkatesh.pallipadi@intel.com 2008-03-18  1208  	retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
aa382ffa705bea Sascha Hauer                  2022-11-08  1209  	if (retval) {
b562ec8f74e4ed Bjorn Helgaas                 2016-03-10  1210  		kfree(res_attr);
45aec1ae72fc59 venkatesh.pallipadi@intel.com 2008-03-18  1211  		return retval;
b19441af185559 Greg Kroah-Hartman            2006-08-28  1212  	}
b19441af185559 Greg Kroah-Hartman            2006-08-28  1213  
aa382ffa705bea Sascha Hauer                  2022-11-08  1214  	if (write_combine)
aa382ffa705bea Sascha Hauer                  2022-11-08  1215  		pdev->res_attr_wc[num] = res_attr;
aa382ffa705bea Sascha Hauer                  2022-11-08  1216  	else
aa382ffa705bea Sascha Hauer                  2022-11-08  1217  		pdev->res_attr[num] = res_attr;
aa382ffa705bea Sascha Hauer                  2022-11-08  1218  
aa382ffa705bea Sascha Hauer                  2022-11-08  1219  	return 0;
aa382ffa705bea Sascha Hauer                  2022-11-08  1220  }
aa382ffa705bea Sascha Hauer                  2022-11-08  1221  

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

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

end of thread, other threads:[~2024-11-01  5:28 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-31  2:43 [PATCH RFC 00/10] sysfs: constify struct bin_attribute (Part 1) Thomas Weißschuh
2024-10-31  2:43 ` [PATCH RFC 01/10] sysfs: explicitly pass size to sysfs_add_bin_file_mode_ns() Thomas Weißschuh
2024-10-31  2:43 ` [PATCH RFC 02/10] sysfs: introduce callback attribute_group::bin_size Thomas Weißschuh
2024-10-31  2:43 ` [PATCH RFC 03/10] PCI/sysfs: Calculate bin_attribute size through bin_size() Thomas Weißschuh
2024-10-31  2:43 ` [PATCH RFC 04/10] nvmem: core: calculate " Thomas Weißschuh
2024-10-31  2:43 ` [PATCH RFC 05/10] sysfs: treewide: constify attribute callback of bin_is_visible() Thomas Weißschuh
2024-10-31  2:43 ` [PATCH RFC 06/10] sysfs: treewide: constify attribute callback of bin_attribute::mmap() Thomas Weißschuh
2024-10-31  2:43 ` [PATCH RFC 07/10] sysfs: drop callback bin_attribute::llseek Thomas Weißschuh
2024-11-01  5:17   ` kernel test robot
2024-11-01  5:27   ` kernel test robot
2024-10-31  2:43 ` [PATCH RFC 08/10] sysfs: implement all BIN_ATTR_* macros in terms of __BIN_ATTR() Thomas Weißschuh
2024-10-31  2:43 ` [PATCH RFC 09/10] sysfs: bin_attribute: add const read/write callback variants Thomas Weißschuh
2024-10-31  2:43 ` [PATCH RFC 10/10] driver core: Constify attribute arguments of binary attributes Thomas Weißschuh
2024-11-01  1:20 ` [PATCH RFC 00/10] sysfs: constify struct bin_attribute (Part 1) Greg Kroah-Hartman

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.