Skip to contentSkip to conversation

Read data to kernel buffer by blkdev_read_iter

DISCUSSION Ivan Safonov 2 participants 5 messages Mar 22, 2019 Block layer

lore
Share & tools
lore
#0 lore raw

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_vma

find_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.

#1 lore raw
Quoted 3 lines · Ivan Safonov

Kernel version is 4.19.16.

Is this behavior valid for iov_iter with type ITER_KVEC?

Are you familiar with the ITER_KVEC flag?

Bart.

#2 lore raw
Quoted 6 lines · Bart Van Assche
Quoted 3 lines · Ivan Safonov

Kernel version is 4.19.16.

Is this behavior valid for iov_iter with type ITER_KVEC?

Are you familiar with the ITER_KVEC flag?

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.

#3 lore raw
Quoted 4 lines · Ivan Safonov

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.

Have you tried to store a physical address in iov_base instead of a virtual address?

Bart.

#4 lore raw
Quoted 8 lines · Bart Van Assche
Quoted 4 lines · Ivan Safonov

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.

Have you tried to store a physical address in iov_base instead 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.