0

I have this device node created using mknod command for a pcie driver.

/dev/pciedrv

Upon removing the driver while device node file is open (in use), the system crashes.

rmmod -f pciedrv

Is there a way to do a graceful exit? Also how to handle the case when user application accessing the file terminates unexpectedly before closing the device node.

bdubey
  • 307
  • 4
  • 13
  • Please ask Questions if not clear – bdubey Oct 18 '14 at 16:01
  • Make a shell script that checks if the device node is in use (with `lsof`) before running `rmmod` ? –  Oct 18 '14 at 16:07
  • can we do it in driver code? – bdubey Oct 18 '14 at 16:10
  • I think so. But in this case it doesn't use lsof since the driver can already know if it's in use by another program. –  Oct 18 '14 at 16:11
  • can you help me where can I do this in code. BDW I checked, below command doesn't list pciedrv node, even when it is in use. lsof |grep pciedrv – bdubey Oct 18 '14 at 16:13
  • This question is unclear. First, did you create that driver yourself (do you have the source of it) ? Second, what are you trying to do ? If you just want a way to remove the kernel module without crashing then my shell script solution is the easiest (use lsof to find programs using the device node, kill them/wait for them to exit before rmmod'ing the kernel module). –  Oct 18 '14 at 16:15
  • Yes I have source of it. file node is */dev/pciedrv* **For first sol using script** I run this command and i get no result (Is this right way?) *lsof |grep pciedrv* *Where should code to check this go in second case, I mean in driver src without script making rmmod intelligent enough to handle this* – bdubey Oct 18 '14 at 16:23
  • I got the first solution and successfully implemented. I am killing the process before rmmod. – bdubey Oct 18 '14 at 16:25
  • So unless you know C and want to spend an hour implementing a graceful exit in the module itself, just stick to the shell script solution and that's it. –  Oct 18 '14 at 16:52

1 Answers1

1

The rmmod help says:

-f, --force
forces a module unload and may crash your machine.

Don't use -f. A correctly written driver will then block module removal as long as it is still in use.

A graceful exit requires that the application that has opened the device node exits gracefully. But even if the application terminates unexpectedly, the kernel will close the file.

CL.
  • 173,858
  • 17
  • 217
  • 259
  • So what's the difference at the level of callbacks in the driver when you call rmmod with and without -f ? Does system call remove callback for each device if you don't give it -f? – Pyjong May 08 '19 at 13:44