Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 12 Jan 2009 17:19:56 +0100
From:      "Alexej Sokolov" <bsd.quest@googlemail.com>
To:        "Mateusz Guzik" <mjguzik@gmail.com>
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: panic by unlocking of mutex in KLD
Message-ID:  <671bb5fc0901120819q65969961v723807bcb7ad5a96@mail.gmail.com>
In-Reply-To: <20090112141029.GA31108@skucha>
References:  <20090112134726.GA2988@debian.samsung.router> <20090112141029.GA31108@skucha>

next in thread | previous in thread | raw e-mail | index | archive | help
2009/1/12 Mateusz Guzik <mjguzik@gmail.com>

> On Mon, Jan 12, 2009 at 02:47:26PM +0100, Alexej Sokolov wrote:
> > Hello,
> >
> > by unloading of folowing module  I have kernel panic.
> >
> > I would like to get any explanation about my mistake.
> >
> > #include <sys/param.h>
> > #include <sys/module.h>
> > #include <sys/kernel.h>
> > #include <sys/systm.h>
> > #include <sys/queue.h>
> > #include <sys/kernel.h>
> > #include <sys/kobj.h>
> > #include <sys/malloc.h>
> > #include <sys/types.h>
> > #include <sys/lock.h>
> > #include <sys/mutex.h>
> >
> >
> > struct mtx my_mtx;
> >
> >
> > /* Load handler */
> > static int
> > load(struct module *mod, int cmd, void *arg)
> > {
> >         int error = 0;
> >         switch(cmd) {
> >                 case MOD_LOAD:
> >                         printf("Start! Addres of mutex = 0x%X \n",
> > &my_mtx);
> >                         mtx_init(&my_mtx, "My mutex name", "My mutex
> > type", MTX_DEF);
> >
> >                         mtx_lock(&my_mtx);
> >                         break;
> >                 case MOD_UNLOAD:
> >                         printf("Stop! Addres of mutex = 0x%X \n",
> > &my_mtx);
> >                         mtx_unlock(&my_mtx);
> >                         break;
> >                 default:
> >                         error = EOPNOTSUPP;
> >                         break;
> >         }
> >
> >         return (error);
> > }
> >
> > /* Module structure */
> > static moduledata_t mod_data = {
> >         "mymod",
> >         load,
> >         NULL
> > };
> > MODULE_VERSION (kld, 1);
> > DECLARE_MODULE (kld, mod_data, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
> >
> >
>
> Acutally it panics even on loading. :)

Thanks, a lot. Yes, in this case the different processes try to lock and
unlock the same mutex. Stupid mistake!

But...

>
>
> Mutexes have owners. It panics on loading because processes cannot
> return to userland with locks held.

i am not sure about it. Some time ago i implemented a charecter device with
two syscalls: write, read. "write" lock the mutex and  "read" unlock it. The
user space programm opens device, then mekes "write" (mutex will held in
kernel), goes back to user space, then makes "read" (mutex will unlocked in
kernel) and it all run without panic. If needed i can post the source code.



> It panics on unloading (in your
> case) because curproc != my_mtx's owner.
>
> --
> Mateusz Guzik <mjguzik at gmail.com>
>

Thanks,
Alexej



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?671bb5fc0901120819q65969961v723807bcb7ad5a96>