Read data to kernel buffer by blkdev_read_iter
Hi,
I want to read data by kernel module from a block device like this:
static int module_init(void)
{
const size_t len = 0x1000;
struct file *file;
char *buf;
struct iovec iovec;
struct kiocb kiocb;
struct iov_iter iov_iter;
ssize_t rcount;
buf = vmalloc(len);
file = filp_open("/dev/sda", 0, 0);
iovec.iov_base = buf;
iovec.iov_len = len;
kiocb.ki_filp = file;
kiocb.ki_pos = 0x200000000;
kiocb.ki_complete = NULL;
kiocb.ki_flags = IOCB_DIRECT;
kiocb.ki_hint = 0;
iov_iter_init(&iov_iter, READ, &iovec, 1, len);
rcount = blkdev_read_iter(&kiocb, &iov_iter);
filp_close(file, current);
vfree(buf);
return rcount < 0 ? rcount : 0;
}but blkdev_read_iter always return -EFAULT. I tried to localize the error:
blkdev_read_iter
generic_file_read_iter
blkdev_direct_IO
__blkdev_direct_IO_simple
bio_iov_iter_get_pages
__bio_iov_iter_get_pages
iov_iter_get_pages
get_user_pages_fast
...
find_vmafind_vma return NULL for vmalloc'ed block.
Kernel version is 4.19.16.
Is this behavior valid for iov_iter with type ITER_KVEC?
--
Ivan Safonov.
Quoted 3 lines · Ivan Safonov
Kernel version is 4.19.16.
Is this behavior valid for
iov_iterwith typeITER_KVEC?
Are you familiar with the ITER_KVEC flag?
Bart.
Quoted 6 lines · Bart Van Assche
Quoted 3 lines · Ivan Safonov
Kernel version is 4.19.16.
Is this behavior valid for
iov_iterwith typeITER_KVEC?Are you familiar with the
ITER_KVECflag?
No, I did not find a clear description of this flag.
In the previous message I misled you: iov_iter has ITER_KVEC type only if set_fs(get_ds()) present before iov_iter_init(). In this case _without_ IOCB_DIRECT blkdev_read_iter() work fine. With IOCB_DIRECT blkdev_read_iter() return -EFAULT.
Quoted 4 lines · Ivan Safonov
In the previous message I misled you:
iov_iterhasITER_KVECtype only if set_fs(get_ds()) present beforeiov_iter_init(). In this case_without_IOCB_DIRECTblkdev_read_iter()work fine. WithIOCB_DIRECTblkdev_read_iter()return-EFAULT.
Have you tried to store a physical address in iov_base instead of a virtual address?
Bart.
Quoted 8 lines · Bart Van Assche
Quoted 4 lines · Ivan Safonov
In the previous message I misled you:
iov_iterhasITER_KVECtype only if set_fs(get_ds()) present beforeiov_iter_init(). In this case _without_IOCB_DIRECTblkdev_read_iter()work fine. WithIOCB_DIRECTblkdev_read_iter()return-EFAULT.Have you tried to store a physical address in
iov_baseinstead of a virtual address?
I tried: already -EFAULT.
vm_area_struct description says: ranges consist of virtual addresses:
/*
* This struct defines a memory VMM memory area. There is one of these
* per VM-area/task. A VM area is any part of the process virtual memory
* space that has a special rule for the page-fault handlers (ie a shared
* library, the executable area etc).
*/
So, the physical addresses could not solve this problem.
I think there should be no memory checks for the kernel allocated memory, or the blkdev_read_iter() is not available to work with vmalloc'ed memory at all.
--
Ivan Safonov.