Re: O_DIRECT Inside Kernel Module?



jaylucasaustin.rr.com <jaylucas@xxxxxxxxxxxxx> wrote:
> I have written a target mode SCSI device driver that can use system memory
> for high speed reads and writes. I would like to be able to back these
> reads and writes up to a partition (I.E. to /dev/sdb1). I have been able to
> do so inside the module by opening the specified device and then I can
> simply read and write to it. However, standard reads and writes are cached
> and not immediately written to the hard drive and really spike CPU
> utilization. O_DIRECT has served me in the past as a way to read or write
> directly to a block level device, but I am getting an EFAULT (error 14 aka
> Bad Address) when I attempt this from a kernel module. I have verified that
> my I/O size is sector size and that my I/O's are aligned and a multiple of
> sector size, so this is not the problem. Also, I used the same code in user
> space and could read and write without incident.

Clearly it expects a user space address as a buffer (my guess)..

But I am extremely surprised that you can read or write ordinarily from
within a kernel module. I would not expect that at all, since those are
system calls, and therefore must go via the syscall nterface, using
special assembly linkage, calling conventions, address space
requirements, and so on. And then there is the possibility of creating
a deadlock loop - such as needing ram for buffer space to put your
writes into, but not being able to get any ram until you have flushed
existing writes aimed at your device, which needs ram for you to put
those writes into ... I would have thought the only safe thing to do
would have been to create block write requests out of existing
buffers and buffer heads and launch them int the block device
subsystem.

You want to ask yourself why systems like MD and LVM do not do "read
and write" to a target device from within the kernel.

Peter
.