How to Create a sysfs File Correctly
One common Linux kernel driver issue that I see all the time is a driver author attempting to create a sysfs file in their code by doing something like:
int my_driver_probe(...) { ... retval = device_create_file(my_device, &my_first_attribute); if (retval) goto error1; retval = device_create_file(my_device, &my_second_attribute); if (retval) goto error2; ... return 0; error2: device_remove_file(my_device, &my_first_attribute); error1: /* Clean up other things and return an error */ ... return -ENODEV; } That’s a good first start, until they get tired of adding more and more sysfs files, and they discover attribute groups, which allows multiple sysfs files to be created and destroyed all at once, without having to handle the unwinding of things if problems occur:
[Read More]