Skip to contentSkip to conversation

sysfs unbind, miscdev

DISCUSSION Martin Kaiser 2 participants 2 messages Mar 21, 2018

lore
Share & tools
lore
#0 lore raw

Dear all,

I have a driver that is an spi device on one side and a miscdevice on the other.

By default, it has the sysfs bind and unbind nodes (I know that I can deactiate them).

Surprisingly to me, I can unbind the driver from the spi device on the bus while there's a user space client that has an open connection to the miscdev. (If the driver was a module, I don't think I could unload it at this point.)

During the unbind, the driver's remove function is called and all devres resources are freed. Amongst them is a struct that is used as file->prvate_data for the miscdev. The client can still continue to write to the miscdev and crash the driver because file->private_data points to invalid data now.

What is the correct way to handle this? Is there a way to "disconnect" all clients of the miscdev during remove?

Thanks,
Martin

#1 lore raw
Quoted 2 lines · Martin Kaiser

I have a driver that is an spi device on one side and a miscdevice on the other.

This way usually leads to either madness or heavy drinking.

Quoted 4 lines

Surprisingly to me, I can unbind the driver from the spi device on the bus while there's a user space client that has an open connection to the miscdev. (If the driver was a module, I don't think I could unload it at this point.)

Not surprising actually, because there's nothing telling the kernel that it shouldn't allow that - the rest of the kernel is convinced that the two are different devices handled by different drivers.

The general fix for this is usually to make sure that something on one side of the driver takes a reference against something onthe other one, so removing one side out from under the other becomes impossible.

Of course, this means that the two devices then have a specified ordering for removal (which may or may not be OK). Having both sides take a reference on the other can be made to allow removal in either order, but done improperly will also allow deadlocking in either order. :)

Having each side's removal do a recursive call of the other's removal code can
also work, but remember to do locking so both sides don't try it at the same time.... -------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 486 bytes
Desc: not available
URL: <http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20180321/33346fca/attachment.sig>