Date: Thu, 6 Nov 2014 13:21:54 +0530 From: Sibananda Sahu <sibananda.sahu@avagotech.com> To: jd1008 <jd1008@gmail.com>, freebsd-questions@freebsd.org Subject: RE: Open file descriptor reference count implementation in driver Message-ID: <71178d539228aaeafd7f8d4a445b6694@mail.gmail.com> In-Reply-To: <545AA111.3000802@gmail.com> References: <c1241e563944d7ec496c6c235f420b21@mail.gmail.com> <CAN2YBg78ucj2hBGHye9UN3-QFB0gT-o=dd4iynGet_sWZ5ABpQ@mail.gmail.com> <1ab03c9bac878f437b205786d8304bd3@mail.gmail.com> <545AA111.3000802@gmail.com>
next in thread | previous in thread | raw e-mail | index | archive | help
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 structure. 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. Thanks, Sibananda Sahu -----Original Message----- From: owner-freebsd-questions@freebsd.org [mailto:owner-freebsd-questions@freebsd.org] On Behalf Of jd1008 Sent: Thursday, November 06, 2014 3:44 AM To: freebsd-questions@freebsd.org 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 layer. ./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@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "freebsd-questions-unsubscribe@freebsd.org"
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?71178d539228aaeafd7f8d4a445b6694>