Skip to contentSkip to conversation

platform/x86: thinkpad_acpi: Prefer struct_size over open coded arithmetic

PATCH In review Len Baker 4 participants 10 messages Sep 18, 2021 – Sep 25, 2021 x86 platform drivers

lore
Share & tools
lore

Len Baker

author
#0 lore raw

PATCH 1/1 platform/x86: thinkpad_acpi: Prefer struct_size over open coded arithmetic

As noted in the "Deprecated Interfaces, Language Features, Attributes, and Conventions" documentation [1], size calculations (especially multiplication) should not be performed in memory allocator (or similar) function arguments due to the risk of them overflowing. This could lead to values wrapping around and a smaller allocation being made than the caller was expecting. Using those allocations could lead to linear overflows of heap memory and other misbehaviors.

So, switch to flexible array member in the struct attribute_set_obj and refactor the code accordingly to use the struct_size() helper instead of the argument "size + count * size" in the kzalloc() function.

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments

---
 drivers/platform/x86/thinkpad_acpi.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/platform/x86/thinkpad_acpi.c +3 -5
index 50ff04c84650..ed0b01ead796 100644
@@ -1008,7 +1008,7 @@ struct attribute_set {
 
 struct attribute_set_obj {
 	struct attribute_set s;
-	struct attribute *a;
+	struct attribute *a[];
 } __attribute__((packed));
 
 static struct attribute_set *create_attr_set(unsigned int max_members,
@@ -1020,13 +1020,11 @@ static struct attribute_set *create_attr_set(unsigned int max_members,
 		return NULL;
 
 	/* Allocates space for implicit NULL at the end too */
-	sobj = kzalloc(sizeof(struct attribute_set_obj) +
-		    max_members * sizeof(struct attribute *),
-		    GFP_KERNEL);
+	sobj = kzalloc(struct_size(sobj, a, max_members + 1), GFP_KERNEL);
 	if (!sobj)
 		return NULL;
 	sobj->s.max_members = max_members;
-	sobj->s.group.attrs = &sobj->a;
+	sobj->s.group.attrs = sobj->a;
 	sobj->s.group.name = name;
 
 	return &sobj->s;

--
2.25.1

#1 lore raw
Quoted 24 lines · Len Baker

As noted in the "Deprecated Interfaces, Language Features, Attributes, and Conventions" documentation [1], size calculations (especially multiplication) should not be performed in memory allocator (or similar) function arguments due to the risk of them overflowing. This could lead to values wrapping around and a smaller allocation being made than the caller was expecting. Using those allocations could lead to linear overflows of heap memory and other misbehaviors.

So, switch to flexible array member in the struct attribute_set_obj and refactor the code accordingly to use the struct_size() helper instead of the argument "size + count * size" in the kzalloc() function.

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments

---
 drivers/platform/x86/thinkpad_acpi.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/platform/x86/thinkpad_acpi.c +0 -0
index 50ff04c84650..ed0b01ead796 100644

drivers/platform/x86/thinkpad_acpi.c struct attribute_set { · line 1012

Quoted 6 lines
drivers/platform/x86/thinkpad_acpi.c +1 -1
@@ -1008,7 +1008,7 @@ struct attribute_set {
 
 struct attribute_set_obj {
 	struct attribute_set s;
-	struct attribute *a;
+	struct attribute *a[];
 } __attribute__((packed));

Whoa. I have so many questions... :)

drivers/platform/x86/thinkpad_acpi.c static struct attribute_set *create_attr_set(unsigned int max_members, · line 1023

Quoted 10 lines
drivers/platform/x86/thinkpad_acpi.c +1 -3
 static struct attribute_set *create_attr_set(unsigned int max_members,
@@ -1020,13 +1020,11 @@ static struct attribute_set *create_attr_set(unsigned int max_members,
 		return NULL;
 
 	/* Allocates space for implicit NULL at the end too */
-	sobj = kzalloc(sizeof(struct attribute_set_obj) +
-		    max_members * sizeof(struct attribute *),
-		    GFP_KERNEL);
+	sobj = kzalloc(struct_size(sobj, a, max_members + 1), GFP_KERNEL);

Whoa, this needs a lot more detail in the changelog if this is actually correct. The original code doesn't seem to match the comment? (Where is the +1?) So is this also a bug-fix?

(I see the caller uses +2? Why? It seems to be using each of hotkey_attributes, plus 1 more attr, plus a final NULL?)

drivers/platform/x86/thinkpad_acpi.c static struct attribute_set *create_attr_set(unsigned int max_members, · line 1028

Quoted 6 lines
drivers/platform/x86/thinkpad_acpi.c +1 -1
 	if (!sobj)
 		return NULL;
 	sobj->s.max_members = max_members;
-	sobj->s.group.attrs = &sobj->a;
+	sobj->s.group.attrs = sobj->a;
 	sobj->s.group.name = name;

The caller also never sets a name?

Why is struct attribute_set_obj marked as __packed?

Quoted 5 lines
drivers/platform/x86/thinkpad_acpi.c +0 -1
 	return &sobj->s;
--

2.25.1

Signature · 1 line
Kees Cook
#2 lore raw

Hi,

Quoted 39 lines · Kees Cook
Quoted 24 lines · Len Baker

As noted in the "Deprecated Interfaces, Language Features, Attributes, and Conventions" documentation [1], size calculations (especially multiplication) should not be performed in memory allocator (or similar) function arguments due to the risk of them overflowing. This could lead to values wrapping around and a smaller allocation being made than the caller was expecting. Using those allocations could lead to linear overflows of heap memory and other misbehaviors.

So, switch to flexible array member in the struct attribute_set_obj and refactor the code accordingly to use the struct_size() helper instead of the argument "size + count * size" in the kzalloc() function.

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments

---
 drivers/platform/x86/thinkpad_acpi.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/platform/x86/thinkpad_acpi.c +0 -0
index 50ff04c84650..ed0b01ead796 100644

drivers/platform/x86/thinkpad_acpi.c struct attribute_set { · line 1012

Quoted 6 lines
drivers/platform/x86/thinkpad_acpi.c +1 -1
@@ -1008,7 +1008,7 @@ struct attribute_set {
 
 struct attribute_set_obj {
 	struct attribute_set s;
-	struct attribute *a;
+	struct attribute *a[];
 } __attribute__((packed));

Whoa. I have so many questions... :)

Quoted 5 lines
drivers/platform/x86/thinkpad_acpi.c +0 -0
 static struct attribute_set *create_attr_set(unsigned int max_members,
@@ -1020,13 +1020,11 @@ static struct attribute_set *create_attr_set(unsigned int max_members,
 		return NULL;
 
Quoted 9 lines

drivers/platform/x86/thinkpad_acpi.c static struct attribute_set *create_attr_set(unsigned int max_members, · line 1023

Quoted 5 lines
drivers/platform/x86/thinkpad_acpi.c +1 -3
 	/* Allocates space for implicit NULL at the end too */
-	sobj = kzalloc(sizeof(struct attribute_set_obj) +
-		    max_members * sizeof(struct attribute *),
-		    GFP_KERNEL);
+	sobj = kzalloc(struct_size(sobj, a, max_members + 1), GFP_KERNEL);

Whoa, this needs a lot more detail in the changelog if this is actually correct. The original code doesn't seem to match the comment? (Where is the +1?) So is this also a bug-fix?

Kees, at first I thought you were spot-on with this comment, but the truth is more subtle. struct attribute_set_obj was:

struct attribute_set_obj {
        struct attribute_set s;
        struct attribute *a;
} __attribute__((packed));

Another way of looking at this, which makes things more clear is as:

struct attribute_set_obj {
        struct attribute_set s;
        struct attribute *a[1];
} __attribute__((packed));

So the sizeof(struct attribute_set_obj) in the original kzalloc call included room for 1 "extra" pointer which is reserved for the terminating NULL pointer.

Changing the struct to:

struct attribute_set_obj {
        struct attribute_set s;
        struct attribute *a[];
} __attribute__((packed));

Is equivalent to changing it to:

struct attribute_set_obj {
        struct attribute_set s;
        struct attribute *a[0];
} __attribute__((packed));

So the change in the struct declaration reduces the sizeof(struct attribute_set_obj) by the size of 1 pointer, making the +1 necessary.

So AFAICT there is actually no functional change here.

Still I will hold off merging this until we agree on this :)

Quoted 2 lines

(I see the caller uses +2? Why? It seems to be using each of hotkey_attributes, plus 1 more attr, plus a final NULL?)

The +2 is actually for 2 extra attributes (making the total number of extra attributes +3 because the sizeof(struct attribute_set_obj) already includes 1 extra).

FWIW these 2 extra attributes are for devices with a a physical rfkill on/off switch and for the device being a convertible capable of reporting laptop- vs tablet-mode.

Quoted 8 lines

drivers/platform/x86/thinkpad_acpi.c static struct attribute_set *create_attr_set(unsigned int max_members, · line 1028

Quoted 6 lines
drivers/platform/x86/thinkpad_acpi.c +1 -1
 	if (!sobj)
 		return NULL;
 	sobj->s.max_members = max_members;
-	sobj->s.group.attrs = &sobj->a;
+	sobj->s.group.attrs = sobj->a;
 	sobj->s.group.name = name;

The caller also never sets a name?

attribute_group.name may be NULL, I don't know of (m)any drivers which actual set this to non NULL.

Quoted 1 line

Why is struct attribute_set_obj marked as __packed?

I have no clue, this seems completely unnecessary.

Len Baker can you submit a separate patch removing the useless __packed ?

Regards,

Hans

#3 lore raw
Quoted 85 lines · Hans de Goede

Hi,

Quoted 39 lines · Kees Cook
Quoted 24 lines · Len Baker

As noted in the "Deprecated Interfaces, Language Features, Attributes, and Conventions" documentation [1], size calculations (especially multiplication) should not be performed in memory allocator (or similar) function arguments due to the risk of them overflowing. This could lead to values wrapping around and a smaller allocation being made than the caller was expecting. Using those allocations could lead to linear overflows of heap memory and other misbehaviors.

So, switch to flexible array member in the struct attribute_set_obj and refactor the code accordingly to use the struct_size() helper instead of the argument "size + count * size" in the kzalloc() function.

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments

---
 drivers/platform/x86/thinkpad_acpi.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/platform/x86/thinkpad_acpi.c +0 -0
index 50ff04c84650..ed0b01ead796 100644

drivers/platform/x86/thinkpad_acpi.c struct attribute_set { · line 1012

Quoted 6 lines
drivers/platform/x86/thinkpad_acpi.c +1 -1
@@ -1008,7 +1008,7 @@ struct attribute_set {
 
 struct attribute_set_obj {
 	struct attribute_set s;
-	struct attribute *a;
+	struct attribute *a[];
 } __attribute__((packed));

Whoa. I have so many questions... :)

Quoted 5 lines
drivers/platform/x86/thinkpad_acpi.c +0 -0
 static struct attribute_set *create_attr_set(unsigned int max_members,
@@ -1020,13 +1020,11 @@ static struct attribute_set *create_attr_set(unsigned int max_members,
 		return NULL;
 
Quoted 9 lines

drivers/platform/x86/thinkpad_acpi.c static struct attribute_set *create_attr_set(unsigned int max_members, · line 1023

Quoted 5 lines
drivers/platform/x86/thinkpad_acpi.c +1 -3
 	/* Allocates space for implicit NULL at the end too */
-	sobj = kzalloc(sizeof(struct attribute_set_obj) +
-		    max_members * sizeof(struct attribute *),
-		    GFP_KERNEL);
+	sobj = kzalloc(struct_size(sobj, a, max_members + 1), GFP_KERNEL);

Whoa, this needs a lot more detail in the changelog if this is actually correct. The original code doesn't seem to match the comment? (Where is the +1?) So is this also a bug-fix?

Kees, at first I thought you were spot-on with this comment, but the truth is more subtle. struct attribute_set_obj was:

struct attribute_set_obj {
        struct attribute_set s;
        struct attribute *a;
} __attribute__((packed));

Another way of looking at this, which makes things more clear is as:

struct attribute_set_obj {
        struct attribute_set s;
        struct attribute *a[1];
} __attribute__((packed));

So the sizeof(struct attribute_set_obj) in the original kzalloc call included room for 1 "extra" pointer which is reserved for the terminating NULL pointer.

Changing the struct to:

struct attribute_set_obj {
        struct attribute_set s;
        struct attribute *a[];
} __attribute__((packed));

Is equivalent to changing it to:

struct attribute_set_obj {
        struct attribute_set s;
        struct attribute *a[0];
} __attribute__((packed));
Quoted 6 lines

So the change in the struct declaration reduces the sizeof(struct attribute_set_obj) by the size of 1 pointer, making the +1 necessary.

So AFAICT there is actually no functional change here.

Still I will hold off merging this until we agree on this :)

First off, why is a single driver doing so many odd things with attribute groups? Why not just use them the way that the rest of the kernel does? Why does this driver need this special handling and no one else does?

I think the default way of handling if an attribute is enabled or not, should suffice here, and make things much simpler overall as all of this crazy attribute handling can just be removed.

Bonus would also be that I think it would fix the race conditions that happen when trying to create attributes after the device is bound to the driver that I think the existing driver has today.

Quoted 10 lines
Quoted 2 lines

(I see the caller uses +2? Why? It seems to be using each of hotkey_attributes, plus 1 more attr, plus a final NULL?)

The +2 is actually for 2 extra attributes (making the total number of extra attributes +3 because the sizeof(struct attribute_set_obj) already includes 1 extra).

FWIW these 2 extra attributes are for devices with a a physical rfkill on/off switch and for the device being a convertible capable of reporting laptop- vs tablet-mode.

Again, using the default way to show (or not show) attributes should solve this issue. Why not just use that instead?

Quoted 11 lines
Quoted 8 lines

drivers/platform/x86/thinkpad_acpi.c static struct attribute_set *create_attr_set(unsigned int max_members, · line 1028

Quoted 6 lines
drivers/platform/x86/thinkpad_acpi.c +1 -1
 	if (!sobj)
 		return NULL;
 	sobj->s.max_members = max_members;
-	sobj->s.group.attrs = &sobj->a;
+	sobj->s.group.attrs = sobj->a;
 	sobj->s.group.name = name;

The caller also never sets a name?

attribute_group.name may be NULL, I don't know of (m)any drivers which actual set this to non NULL.

It is used by some, that is how you can put all of the attributes in a subdirectory automatically. No idea if that's needed here...

All attributes for this driver are documented in Documentation/ABI/, right? :)

thanks,

greg k-h

#4 lore raw

Hi,

Quoted 89 lines · Greg KH
Quoted 88 lines · Hans de Goede

Hi,

Quoted 39 lines · Kees Cook
Quoted 24 lines · Len Baker

As noted in the "Deprecated Interfaces, Language Features, Attributes, and Conventions" documentation [1], size calculations (especially multiplication) should not be performed in memory allocator (or similar) function arguments due to the risk of them overflowing. This could lead to values wrapping around and a smaller allocation being made than the caller was expecting. Using those allocations could lead to linear overflows of heap memory and other misbehaviors.

So, switch to flexible array member in the struct attribute_set_obj and refactor the code accordingly to use the struct_size() helper instead of the argument "size + count * size" in the kzalloc() function.

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments

---
 drivers/platform/x86/thinkpad_acpi.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/platform/x86/thinkpad_acpi.c +0 -0
index 50ff04c84650..ed0b01ead796 100644

drivers/platform/x86/thinkpad_acpi.c struct attribute_set { · line 1012

Quoted 6 lines
drivers/platform/x86/thinkpad_acpi.c +1 -1
@@ -1008,7 +1008,7 @@ struct attribute_set {
 
 struct attribute_set_obj {
 	struct attribute_set s;
-	struct attribute *a;
+	struct attribute *a[];
 } __attribute__((packed));

Whoa. I have so many questions... :)

Quoted 5 lines
drivers/platform/x86/thinkpad_acpi.c +0 -0
 static struct attribute_set *create_attr_set(unsigned int max_members,
@@ -1020,13 +1020,11 @@ static struct attribute_set *create_attr_set(unsigned int max_members,
 		return NULL;
 
Quoted 9 lines

drivers/platform/x86/thinkpad_acpi.c static struct attribute_set *create_attr_set(unsigned int max_members, · line 1023

Quoted 5 lines
drivers/platform/x86/thinkpad_acpi.c +1 -3
 	/* Allocates space for implicit NULL at the end too */
-	sobj = kzalloc(sizeof(struct attribute_set_obj) +
-		    max_members * sizeof(struct attribute *),
-		    GFP_KERNEL);
+	sobj = kzalloc(struct_size(sobj, a, max_members + 1), GFP_KERNEL);

Whoa, this needs a lot more detail in the changelog if this is actually correct. The original code doesn't seem to match the comment? (Where is the +1?) So is this also a bug-fix?

Kees, at first I thought you were spot-on with this comment, but the truth is more subtle. struct attribute_set_obj was:

struct attribute_set_obj {
        struct attribute_set s;
        struct attribute *a;
} __attribute__((packed));

Another way of looking at this, which makes things more clear is as:

struct attribute_set_obj {
        struct attribute_set s;
        struct attribute *a[1];
} __attribute__((packed));

So the sizeof(struct attribute_set_obj) in the original kzalloc call included room for 1 "extra" pointer which is reserved for the terminating NULL pointer.

Changing the struct to:

struct attribute_set_obj {
        struct attribute_set s;
        struct attribute *a[];
} __attribute__((packed));

Is equivalent to changing it to:

struct attribute_set_obj {
        struct attribute_set s;
        struct attribute *a[0];
} __attribute__((packed));

So the change in the struct declaration reduces the sizeof(struct attribute_set_obj) by the size of 1 pointer, making the +1 necessary.

Quoted 8 lines
Quoted 3 lines

So AFAICT there is actually no functional change here.

Still I will hold off merging this until we agree on this :)

First off, why is a single driver doing so many odd things with attribute groups? Why not just use them the way that the rest of the kernel does? Why does this driver need this special handling and no one else does?

The thinkpad_acpi driver carries a lot of legacy with it. So in general we are careful with making changes because some people still use quite old ThinkPad-s and it is tricky to make sure we don't break stuff on older models. So yeah there is some cruft in a bunch of places in this driver.

In this case it seems that cleaning things up is a straight forward fix though, so we really should do so.

Quoted 16 lines

I think the default way of handling if an attribute is enabled or not, should suffice here, and make things much simpler overall as all of this crazy attribute handling can just be removed.

Bonus would also be that I think it would fix the race conditions that happen when trying to create attributes after the device is bound to the driver that I think the existing driver has today.

Quoted 7 lines
Quoted 2 lines

(I see the caller uses +2? Why? It seems to be using each of hotkey_attributes, plus 1 more attr, plus a final NULL?)

The +2 is actually for 2 extra attributes (making the total number of extra attributes +3 because the sizeof(struct attribute_set_obj) already includes 1 extra).

Quoted 6 lines
Quoted 3 lines

FWIW these 2 extra attributes are for devices with a a physical rfkill on/off switch and for the device being a convertible capable of reporting laptop- vs tablet-mode.

Again, using the default way to show (or not show) attributes should solve this issue. Why not just use that instead?

I agree, moving to a "fixed" attribute_group, with an is_visible check for the optional attributes would be a much better fix and allow removal of a whole bunch of custom code.

If anyone following this thread could submit a patch doing that, then that would be great.

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines

drivers/platform/x86/thinkpad_acpi.c static struct attribute_set *create_attr_set(unsigned int max_members, · line 1028

Quoted 6 lines
drivers/platform/x86/thinkpad_acpi.c +1 -1
 	if (!sobj)
 		return NULL;
 	sobj->s.max_members = max_members;
-	sobj->s.group.attrs = &sobj->a;
+	sobj->s.group.attrs = sobj->a;
 	sobj->s.group.name = name;

The caller also never sets a name?

Quoted 8 lines
Quoted 2 lines

attribute_group.name may be NULL, I don't know of (m)any drivers which actual set this to non NULL.

It is used by some, that is how you can put all of the attributes in a subdirectory automatically. No idea if that's needed here...

All attributes for this driver are documented in Documentation/ABI/, right? :)

I'm not sure if all attributes are documented, but a lot of them (including all recently added ones) are documented in:
Documentation/admin-guide/laptops/thinkpad-acpi.rst

Regards,

Hans

#5 lore raw
Quoted 6 lines · Hans de Goede
Quoted 2 lines

All attributes for this driver are documented in Documentation/ABI/, right? :)

I'm not sure if all attributes are documented, but a lot of them (including all recently added ones) are documented in:
Documentation/admin-guide/laptops/thinkpad-acpi.rst

They should also go into Documentation/ABI/ which is where sysfs files are documented. We are working on tools that make parsing that easier, so it would be good to keep them out of other random documentation files whenever possible.

thanks,

greg k-h

author
#6 lore raw
In reply to Greg KH

Hi,

Quoted 5 lines · Greg KH

First off, why is a single driver doing so many odd things with attribute groups? Why not just use them the way that the rest of the kernel does? Why does this driver need this special handling and no one else does?

Is [1] the correct way to deal with devices attributes? I think so.

[1] https://www.kernel.org/doc/html/latest/driver-api/driver-model/driver.html#attributes

Quoted 4 lines

I think the default way of handling if an attribute is enabled or not, should suffice here, and make things much simpler overall as all of this crazy attribute handling can just be removed.

Sorry but what is the default way? Would it be correct to check if the file exists?

Quoted 12 lines

Bonus would also be that I think it would fix the race conditions that happen when trying to create attributes after the device is bound to the driver that I think the existing driver has today.

Quoted 7 lines
Quoted 2 lines

(I see the caller uses +2? Why? It seems to be using each of hotkey_attributes, plus 1 more attr, plus a final NULL?)

The +2 is actually for 2 extra attributes (making the total number of extra attributes +3 because the sizeof(struct attribute_set_obj) already includes 1 extra).

Quoted 6 lines
Quoted 3 lines

FWIW these 2 extra attributes are for devices with a a physical rfkill on/off switch and for the device being a convertible capable of reporting laptop- vs tablet-mode.

Again, using the default way to show (or not show) attributes should solve this issue. Why not just use that instead?

What is the default way? Would it be correct to use device_create_file() and device_remove_file()?

Sorry if it is a trivial question but I am a kernel newbie :) I have a lot to learn. Any suggestion or a good driver to look at would be greatly appreciated.

Thanks,
Len

#7 lore raw
Quoted 4 lines · Len Baker

Hi,

Quoted 1 line · Greg KH
Quoted 8 lines
Quoted 4 lines

First off, why is a single driver doing so many odd things with attribute groups? Why not just use them the way that the rest of the kernel does? Why does this driver need this special handling and no one else does?

Is [1] the correct way to deal with devices attributes? I think so.

[1] https://www.kernel.org/doc/html/latest/driver-api/driver-model/driver.html#attributes

No, do not use driver_create_file(), see:

	http://kroah.com/log/blog/2013/06/26/how-to-create-a-sysfs-file-correctly/

as a more up to date thing.

Someone should fix that in-kernel documentation one day :)

Quoted 6 lines
Quoted 3 lines

I think the default way of handling if an attribute is enabled or not, should suffice here, and make things much simpler overall as all of this crazy attribute handling can just be removed.

Sorry but what is the default way? Would it be correct to check if the file exists?

Use the is_visable() callback for the attribute group to enable/disable the creation of the sysfs file.

Quoted 15 lines
Quoted 15 lines

Bonus would also be that I think it would fix the race conditions that happen when trying to create attributes after the device is bound to the driver that I think the existing driver has today.

Quoted 10 lines
Quoted 2 lines

(I see the caller uses +2? Why? It seems to be using each of hotkey_attributes, plus 1 more attr, plus a final NULL?)

The +2 is actually for 2 extra attributes (making the total number of extra attributes +3 because the sizeof(struct attribute_set_obj) already includes 1 extra).

FWIW these 2 extra attributes are for devices with a a physical rfkill on/off switch and for the device being a convertible capable of reporting laptop- vs tablet-mode.

Quoted 5 lines
Quoted 2 lines

Again, using the default way to show (or not show) attributes should solve this issue. Why not just use that instead?

What is the default way? Would it be correct to use device_create_file() and device_remove_file()?

Put all the attributes into an attribute group and attach it to the driver. The driver core will create/remove the files when needed. The link above should help explain that a bit better, and I can point you at examples if needed.

Does that help?

thanks,

greg k-h

author
#8 lore raw

Hi Greg,

Quoted 10 lines · Greg KH
Quoted 9 lines · Len Baker

Hi,

Quoted 5 lines · Greg KH

First off, why is a single driver doing so many odd things with attribute groups? Why not just use them the way that the rest of the kernel does? Why does this driver need this special handling and no one else does?

Quoted 7 lines
Quoted 3 lines

Is [1] the correct way to deal with devices attributes? I think so.

[1] https://www.kernel.org/doc/html/latest/driver-api/driver-model/driver.html#attributes

No, do not use driver_create_file(), see:

	http://kroah.com/log/blog/2013/06/26/how-to-create-a-sysfs-file-correctly/

as a more up to date thing.

Ok, understood. Thanks.

Quoted 7 lines

Someone should fix that in-kernel documentation one day :)

Quoted 4 lines
Quoted 3 lines

I think the default way of handling if an attribute is enabled or not, should suffice here, and make things much simpler overall as all of this crazy attribute handling can just be removed.

Quoted 5 lines
Quoted 2 lines

Sorry but what is the default way? Would it be correct to check if the file exists?

Use the is_visable() callback for the attribute group to enable/disable the creation of the sysfs file.

Ok, I will take a look at it.

Quoted 22 lines
Quoted 16 lines
Quoted 16 lines

Bonus would also be that I think it would fix the race conditions that happen when trying to create attributes after the device is bound to the driver that I think the existing driver has today.

Quoted 10 lines
Quoted 2 lines

(I see the caller uses +2? Why? It seems to be using each of hotkey_attributes, plus 1 more attr, plus a final NULL?)

The +2 is actually for 2 extra attributes (making the total number of extra attributes +3 because the sizeof(struct attribute_set_obj) already includes 1 extra).

FWIW these 2 extra attributes are for devices with a a physical rfkill on/off switch and for the device being a convertible capable of reporting laptop- vs tablet-mode.

Quoted 5 lines
Quoted 2 lines

Again, using the default way to show (or not show) attributes should solve this issue. Why not just use that instead?

What is the default way? Would it be correct to use device_create_file() and device_remove_file()?

Quoted 6 lines

Put all the attributes into an attribute group and attach it to the driver. The driver core will create/remove the files when needed. The link above should help explain that a bit better, and I can point you at examples if needed.

Does that help?

Yes, things are clearer to me now. Also, since the only way to learn is to do so, I will take the task to switch this driver to the common use of attributes.

Thank you very much for your time and guidance.

Regards,
Len

author
#9 lore raw
In reply to Hans de Goede

Hi,

Quoted 50 lines · Hans de Goede
Quoted 6 lines · Kees Cook
Quoted 5 lines · Len Baker

static struct attribute_set *create_attr_set(unsigned int max_members,

(quoted diff) +0 -0
@@ -1020,13 +1020,11 @@ static struct attribute_set *create_attr_set(unsigned int max_members,
 		return NULL;
 
Quoted 9 lines

drivers/platform/x86/thinkpad_acpi.c static struct attribute_set *create_attr_set(unsigned int max_members, · line 1023

Quoted 5 lines
drivers/platform/x86/thinkpad_acpi.c +1 -3
 	/* Allocates space for implicit NULL at the end too */
-	sobj = kzalloc(sizeof(struct attribute_set_obj) +
-		    max_members * sizeof(struct attribute *),
-		    GFP_KERNEL);
+	sobj = kzalloc(struct_size(sobj, a, max_members + 1), GFP_KERNEL);

Whoa, this needs a lot more detail in the changelog if this is actually correct. The original code doesn't seem to match the comment? (Where is the +1?) So is this also a bug-fix?

Kees, at first I thought you were spot-on with this comment, but the truth is more subtle. struct attribute_set_obj was:

struct attribute_set_obj {
        struct attribute_set s;
        struct attribute *a;
} __attribute__((packed));

Another way of looking at this, which makes things more clear is as:

struct attribute_set_obj {
        struct attribute_set s;
        struct attribute *a[1];
} __attribute__((packed));

So the sizeof(struct attribute_set_obj) in the original kzalloc call included room for 1 "extra" pointer which is reserved for the terminating NULL pointer.

Changing the struct to:

struct attribute_set_obj {
        struct attribute_set s;
        struct attribute *a[];
} __attribute__((packed));

Is equivalent to changing it to:

struct attribute_set_obj {
        struct attribute_set s;
        struct attribute *a[0];
} __attribute__((packed));
Quoted 4 lines

So the change in the struct declaration reduces the sizeof(struct attribute_set_obj) by the size of 1 pointer, making the +1 necessary.

So AFAICT there is actually no functional change here.

Hans, thanks for the explanation. Yes, this is the reason I added the "plus 1". Not only based on the comment :)

Regards,
Len