25

In a linux device driver, creating sysfs attributes in probe is way too racy--specifically, it experiences a race condition with userspace. The recommended workaround is to add your attributes to various default attribute groups so they can be automatically created before probe. For a device driver, struct device_driver contains const struct attribute_group **groups for this purpose.

However, struct attribute_group only got a field for binary attributes in Linux 3.11. With older kernels (specifically, 3.4), how should a device driver create sysfs binary attributes before probe?

Gavin S. Yancey
  • 1,179
  • 1
  • 11
  • 29
  • 2
    Aw, you should have given it a bit to see how much more eyeballs you got... –  Aug 10 '16 at 20:51
  • @Will restored...let's give it a try. – Gavin S. Yancey Aug 10 '16 at 21:09
  • 1
    @RadLexus you might notice that that blog post is actually the first link in my question... – Gavin S. Yancey Aug 11 '16 at 22:28
  • 2
    Maybe there is no way to do that, becase the [merge comment itself](https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=7a62711aacda8887d94c40daa199b37abb1d54e1) says: "Also here is the ability to create binary files as attribute groups, to solve that race condition, which was impossible to do before this, so that's my fault the drivers were broken." – Roman Khimov Aug 14 '16 at 08:35
  • 1
    @RomanKhimov That seems like a fairly authoritative source to say "it's not possible." Submit it as an answer and I'll accept it. – Gavin S. Yancey Aug 16 '16 at 02:24

1 Answers1

5

Quoting (emphasis mine) Greg Kroah-Hartman from his comment to a merge request (that was merged by Linus as a part of 3.11 development cycle):

Here are some driver core patches for 3.11-rc2. They aren't really bugfixes, but a bunch of new helper macros for drivers to properly create attribute groups, which drivers and subsystems need to fix up a ton of race issues with incorrectly creating sysfs files (binary and normal) after userspace has been told that the device is present.

Also here is the ability to create binary files as attribute groups, to solve that race condition, which was impossible to do before this, so that's my fault the drivers were broken.

So it looks like there really is no way to solve this problem on old kernels.

Roman Khimov
  • 4,312
  • 1
  • 23
  • 32