Date: Tue, 22 May 2007 18:45:00 +1000 From: Lawrence Stewart <lstewart@room52.net> To: =?UTF-8?B?RGFnLUVybGluZyBTbcO4cmdyYXY=?= <des@des.no> Cc: freebsd-hackers@freebsd.org Subject: Re: Writing a plain text file to disk from kernel space Message-ID: <4652AD8C.7000605@room52.net> In-Reply-To: <86sl9qtpd1.fsf@dwp.des.no> References: <4649349D.4060101@room52.net> <200705150847.38838.marc.loerner@hob.de> <46499491.2010205@room52.net> <f2j5hf$hap$2@sea.gmane.org> <46515DE0.20209@room52.net> <86sl9qtpd1.fsf@dwp.des.no>
next in thread | previous in thread | raw e-mail | index | archive | help
[-- Attachment #1 --] OK well that's cleared up. Thanks DES. So I went back to trying the kernio code I found here (http://people.freebsd.org/~pjd/misc/kernio/) which does operate directly on vnodes (first time I tried it I was getting kernel panics, so I assumed the code was a bit dated and carried on down the path of trying to get file descriptors working). Anyways, I modified the filewriter kernel module that I attached to my previous post so that it used the kio code, and verified that the code does work when called across different threads (in the module's init and deinit functions). However, when I ported it into my other module that I'm actually working on that uses the pfil hooks, I started getting hard resets again. After further investigation, it turns out that the pfil input hook I'm using, which catches packets as they traverse up the network stack, has no problems, and will happily write to the file using the kio_write function. However, in the pfil output hook, a call to kio_write causes a hard reset, with the following text shown on tty0: Sleeping thread (tid 100069, pid 613) owns a non-sleepable lock panic: sleeping thread If I comment out the kio_write code and put a printf instead, there are no such problems, so it seems the kio_write function is doing something that is upsetting the kernel, but only when called from a function that is acting as a pfil output hook? Strikes me as odd behaviour. I don't understand which thread the error is in relation to, why that thread is sleeping or which lock it is referring to. I tried wrapping the call to kio_write in a mutex, in case there was a race condition caused by multiple threads trying to write to the file at the one time, but that hasn't made a difference at all. I've attached the code that demonstrates the problem, but be warned: I've intentionally left it in a state that demonstrates the problem, and it will therefore hard reset any machine you run it on. Any thoughts on what's going on and how I can make the kio code not break the kernel when called from within a function that is acting as a pfil output hook? Cheers, Lawrence Dag-Erling Smørgrav wrote: > Lawrence Stewart <lstewart@room52.net> writes: > >> I suspect that you can't use a file descriptor that was opened in one >> thread in a completely different thread, but I'm not sure if this is >> true, and if it is true, how to get around it. >> > > A file descriptor is an index into a file table. Different threads have > different file tables. > > If you want to read from or write to files within the kernel, you need > to operate directly on vnodes, not on file descriptors. > > DES > [-- Attachment #2 --] ELF 4 4 ( @ @ @ x x % . $ - ) " &
