Open file descriptor reference count implementation in driver

Sibananda Sahu sibananda.sahu at
Thu Nov 6 07:52:03 UTC 2014

Thanks jd1008 for your valuable time and information.

Similar reference count variable I have implemented on my driver.
I am incrementing the variable when my driver xx_open() is called and
decrementing it when my xx_close() is called.

While unloading the driver I am checking if there is an open file
descriptor by checking the reference count variable that I am using.
This is the main reason I am asking for reusing one of these variables for
this purpose.

Sometimes what happens some application calls my _open() call but it does
not _close() it.
In this case my reference count variable have non zero value and it
refuses to unload the driver(The way I have implemented).

In this particular scenario I have used the si_usecount from my cdev
This value I think takes care of use count of the cdev, Even in the case
if some application has opened the particular cdev and the application
itself exited without closing the file descriptor, then also this variable
help me to identify really no one is using my cdev structure.

When some application opens my driver file descriptor and did not close it
then I have observed the following things:
- si_refcount has some NONZERO value.
- si_usecount has the value ZERO.

I just need to know can I use any of these variables, more precisely
si_usecount variable in my driver to know if some application is using the
driver file descriptor then it won't allow the driver to unload.

Sibananda Sahu

-----Original Message-----
From: owner-freebsd-questions at
[mailto:owner-freebsd-questions at] On Behalf Of jd1008
Sent: Thursday, November 06, 2014 3:44 AM
To: freebsd-questions at
Subject: Re: Open file descriptor reference count implementation in driver

Hi Sibananda,
As you can see below, si_refcount  and si_usecount are used by the kernel
in kern_conf.c,  and the generic FS vnode layer, and the device FS vnode

./kern/kern_conf.c: dev->si_refcount++;
./kern/kern_conf.c: dev->si_refcount++;
./kern/kern_conf.c: dev->si_refcount--;
./kern/kern_conf.c: KASSERT(dev->si_refcount >= 0,
./kern/kern_conf.c: if (dev->si_usecount == 0 &&
./kern/kern_conf.c: if (dev->si_devsw == NULL && dev->si_refcount == 0) {
./kern/kern_conf.c: dev->si_refcount++;        /* Avoid race with
dev_rel() */
./kern/kern_conf.c: dev->si_refcount--;        /* Avoid race with
dev_rel() */
./kern/kern_conf.c: if (dev->si_refcount > 0) {
./kern/kern_conf.c:     dev->si_name, dev->si_refcount, dev->si_usecount,

The FS Vnode layer:

./kern/vfs_bio.c:   KASSERT(dev->si_refcount > 0,
./kern/vfs_subr.c:          vp->v_rdev->si_usecount++;
./kern/vfs_subr.c:          vp->v_rdev->si_usecount++;
./kern/vfs_subr.c:          vp->v_rdev->si_usecount--;
./kern/vfs_subr.c:          vp->v_rdev->si_usecount--;
./kern/vfs_subr.c:  count = vp->v_rdev->si_usecount;
./kern/vfs_subr.c:  count = dev->si_usecount;
./sys/conf.h:       int             si_refcount;
./sys/conf.h:       u_long          si_usecount;

The device FS vnode layer:

./fs/devfs/devfs_vnops.c:   KASSERT((*devp)->si_refcount > 0,
./fs/devfs/devfs_vnops.c:           dev->si_usecount += vp->v_usecount;
./fs/devfs/devfs_vnops.c:   KASSERT(dev->si_refcount > 0,
./fs/devfs/devfs_vnops.c:   dev->si_usecount -= vp->v_usecount;

So, it would seem like YOUR DRIVER has no business checking/modifying
these variables.
But your device driver's open and close functions will be  called by the
upper vnode operations (functions) and they take care of these variables
(among others).
When your driver's close function is called as a final act of the vnode
layer when the counts go to 0.
Whay your XX_close() function will do depends a lot on what your driver is
supposed to achieve. Mostlly, release driver locks and memory allocated
for the very first open.
freebsd-questions at mailing list
To unsubscribe, send any mail to
"freebsd-questions-unsubscribe at"

More information about the freebsd-questions mailing list