Race conditions

Leonardo Fogel leonardofogel at yahoo.com.br
Tue Aug 18 18:41:41 UTC 2015


Hi.
The following code is an exerpt from the FreeBSD Architecture Handbook, chapter 11.1.1. Sample Driver Source. I've included labels i1, i2, i3.

   int
   mypci_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
   {
           struct mypci_softc *sc;

           /* Look up our softc. */
   i1:     sc = dev->si_drv1;
           device_printf(sc->my_dev, "Opened successfully.\n");
           return (0);
   }

   static int
   mypci_attach(device_t dev)
   {
           struct mypci_softc *sc;
   ...
   i2:     sc->my_cdev = make_dev(&mypci_cdevsw, device_get_unit(dev),
               UID_ROOT, GID_WHEEL, 0600, "mypci%u", device_get_unit(dev));
   i3:     sc->my_cdev->si_drv1 = sc;
           printf("Mypci device loaded.\n");
           return (0);
   }


As I understand it, as soon as instruction at label i2 completes, there is a rare possibility that another thread opens the device file and executes the instruction at i1, before the instruction at i3 is executed. Is it correct? How could one fix it?

Thank you for your time.
Leonardo


More information about the freebsd-drivers mailing list