diff mbox series

cxl/port: Constify 'struct bin_attribute'

Message ID 20250114-sysfs-const-bin_attr-cxl-v1-1-5afa23fe2a52@weissschuh.net
State New
Headers show
Series cxl/port: Constify 'struct bin_attribute' | expand

Commit Message

Thomas Weißschuh Jan. 14, 2025, 9:25 p.m. UTC
The sysfs core now allows instances of 'struct bin_attribute' to be
moved into read-only memory. Make use of that to protect them against
accidental or malicious modifications.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
 drivers/cxl/port.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)


---
base-commit: 7f5b6a8ec18e3add4c74682f60b90c31bdf849f2
change-id: 20250114-sysfs-const-bin_attr-cxl-d102eab47586

Best regards,

Comments

Alison Schofield Jan. 14, 2025, 11:01 p.m. UTC | #1
On Tue, Jan 14, 2025 at 10:25:14PM +0100, Thomas Weißschuh wrote:
> The sysfs core now allows instances of 'struct bin_attribute' to be
> moved into read-only memory. Make use of that to protect them against
> accidental or malicious modifications.
> 
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> ---
>  drivers/cxl/port.c | 10 +++++-----
>  1 file changed, 5 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/cxl/port.c b/drivers/cxl/port.c
> index 4c83f6a22e584f1f248807c7fa4fe932d6f11d67..664fde6ff53862c6aef2a83be56c1518b6bfbadd 100644
> --- a/drivers/cxl/port.c
> +++ b/drivers/cxl/port.c
> @@ -153,7 +153,7 @@ static int cxl_port_probe(struct device *dev)
>  }
>  
>  static ssize_t CDAT_read(struct file *filp, struct kobject *kobj,
> -			 struct bin_attribute *bin_attr, char *buf,
> +			 const struct bin_attribute *bin_attr, char *buf,
>  			 loff_t offset, size_t count)
>  {
>  	struct device *dev = kobj_to_dev(kobj);
> @@ -170,7 +170,7 @@ static ssize_t CDAT_read(struct file *filp, struct kobject *kobj,
>  				       port->cdat.length);
>  }
>  
> -static BIN_ATTR_ADMIN_RO(CDAT, 0);
> +static const BIN_ATTR_ADMIN_RO(CDAT, 0);
>  
>  static umode_t cxl_port_bin_attr_is_visible(struct kobject *kobj,
>  					    const struct bin_attribute *attr, int i)
> @@ -184,13 +184,13 @@ static umode_t cxl_port_bin_attr_is_visible(struct kobject *kobj,
>  	return 0;
>  }
>  
> -static struct bin_attribute *cxl_cdat_bin_attributes[] = {
> +static const struct bin_attribute *const cxl_cdat_bin_attributes[] = {
>  	&bin_attr_CDAT,
>  	NULL,
>  };
>  
> -static struct attribute_group cxl_cdat_attribute_group = {
> -	.bin_attrs = cxl_cdat_bin_attributes,
> +static const struct attribute_group cxl_cdat_attribute_group = {
> +	.bin_attrs_new = cxl_cdat_bin_attributes,
>  	.is_bin_visible = cxl_port_bin_attr_is_visible,
>  };

What's the point of s/bin_attrs/bin_attrs_new above?

--Alison

>  
> 
> ---
> base-commit: 7f5b6a8ec18e3add4c74682f60b90c31bdf849f2
> change-id: 20250114-sysfs-const-bin_attr-cxl-d102eab47586
> 
> Best regards,
> -- 
> Thomas Weißschuh <linux@weissschuh.net>
>
Thomas Weißschuh Jan. 15, 2025, 8:44 a.m. UTC | #2
On 2025-01-14 15:01:22-0800, Alison Schofield wrote:
> On Tue, Jan 14, 2025 at 10:25:14PM +0100, Thomas Weißschuh wrote:
> > The sysfs core now allows instances of 'struct bin_attribute' to be
> > moved into read-only memory. Make use of that to protect them against
> > accidental or malicious modifications.
> > 
> > Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> > ---
> >  drivers/cxl/port.c | 10 +++++-----
> >  1 file changed, 5 insertions(+), 5 deletions(-)
> > 
> > diff --git a/drivers/cxl/port.c b/drivers/cxl/port.c
> > index 4c83f6a22e584f1f248807c7fa4fe932d6f11d67..664fde6ff53862c6aef2a83be56c1518b6bfbadd 100644
> > --- a/drivers/cxl/port.c
> > +++ b/drivers/cxl/port.c
> > @@ -153,7 +153,7 @@ static int cxl_port_probe(struct device *dev)
> >  }
> >  
> >  static ssize_t CDAT_read(struct file *filp, struct kobject *kobj,
> > -			 struct bin_attribute *bin_attr, char *buf,
> > +			 const struct bin_attribute *bin_attr, char *buf,
> >  			 loff_t offset, size_t count)
> >  {
> >  	struct device *dev = kobj_to_dev(kobj);
> > @@ -170,7 +170,7 @@ static ssize_t CDAT_read(struct file *filp, struct kobject *kobj,
> >  				       port->cdat.length);
> >  }
> >  
> > -static BIN_ATTR_ADMIN_RO(CDAT, 0);
> > +static const BIN_ATTR_ADMIN_RO(CDAT, 0);
> >  
> >  static umode_t cxl_port_bin_attr_is_visible(struct kobject *kobj,
> >  					    const struct bin_attribute *attr, int i)
> > @@ -184,13 +184,13 @@ static umode_t cxl_port_bin_attr_is_visible(struct kobject *kobj,
> >  	return 0;
> >  }
> >  
> > -static struct bin_attribute *cxl_cdat_bin_attributes[] = {
> > +static const struct bin_attribute *const cxl_cdat_bin_attributes[] = {
> >  	&bin_attr_CDAT,
> >  	NULL,
> >  };
> >  
> > -static struct attribute_group cxl_cdat_attribute_group = {
> > -	.bin_attrs = cxl_cdat_bin_attributes,
> > +static const struct attribute_group cxl_cdat_attribute_group = {
> > +	.bin_attrs_new = cxl_cdat_bin_attributes,
> >  	.is_bin_visible = cxl_port_bin_attr_is_visible,
> >  };
> 
> What's the point of s/bin_attrs/bin_attrs_new above?

The new type of cxl_cdat_bin_attributes is not compatible with
.bin_attrs. Therefore .bin_attrs_new was introduced.
When all users have been migrated to the new type, .bin_attrs itself will
be switched to the new type and all users moved back to bin_attr.
In one big centrally managed commit to avoid annoying subsystem
maintainers (even more).

The same issue exists in .read_new/.write_new but in this specific case
that is handled transparently in BIN_ATTR_ADMIN_RO().

> 
> --Alison
> 
> >  
> > 
> > ---
> > base-commit: 7f5b6a8ec18e3add4c74682f60b90c31bdf849f2
> > change-id: 20250114-sysfs-const-bin_attr-cxl-d102eab47586
> > 
> > Best regards,
> > -- 
> > Thomas Weißschuh <linux@weissschuh.net>
> >
Jonathan Cameron Jan. 15, 2025, 1:31 p.m. UTC | #3
On Tue, 14 Jan 2025 22:25:14 +0100
Thomas Weißschuh <linux@weissschuh.net> wrote:

> The sysfs core now allows instances of 'struct bin_attribute' to be
> moved into read-only memory. Make use of that to protect them against
> accidental or malicious modifications.
> 
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>

Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
diff mbox series

Patch

diff --git a/drivers/cxl/port.c b/drivers/cxl/port.c
index 4c83f6a22e584f1f248807c7fa4fe932d6f11d67..664fde6ff53862c6aef2a83be56c1518b6bfbadd 100644
--- a/drivers/cxl/port.c
+++ b/drivers/cxl/port.c
@@ -153,7 +153,7 @@  static int cxl_port_probe(struct device *dev)
 }
 
 static ssize_t CDAT_read(struct file *filp, struct kobject *kobj,
-			 struct bin_attribute *bin_attr, char *buf,
+			 const struct bin_attribute *bin_attr, char *buf,
 			 loff_t offset, size_t count)
 {
 	struct device *dev = kobj_to_dev(kobj);
@@ -170,7 +170,7 @@  static ssize_t CDAT_read(struct file *filp, struct kobject *kobj,
 				       port->cdat.length);
 }
 
-static BIN_ATTR_ADMIN_RO(CDAT, 0);
+static const BIN_ATTR_ADMIN_RO(CDAT, 0);
 
 static umode_t cxl_port_bin_attr_is_visible(struct kobject *kobj,
 					    const struct bin_attribute *attr, int i)
@@ -184,13 +184,13 @@  static umode_t cxl_port_bin_attr_is_visible(struct kobject *kobj,
 	return 0;
 }
 
-static struct bin_attribute *cxl_cdat_bin_attributes[] = {
+static const struct bin_attribute *const cxl_cdat_bin_attributes[] = {
 	&bin_attr_CDAT,
 	NULL,
 };
 
-static struct attribute_group cxl_cdat_attribute_group = {
-	.bin_attrs = cxl_cdat_bin_attributes,
+static const struct attribute_group cxl_cdat_attribute_group = {
+	.bin_attrs_new = cxl_cdat_bin_attributes,
 	.is_bin_visible = cxl_port_bin_attr_is_visible,
 };