gpt4 book ai didi

c - Linux内核异步AIO : do I need to copy over the struct iovec for later processing?

转载 作者:太空宇宙 更新时间:2023-11-04 04:44:54 24 4
gpt4 key购买 nike

我在我的驱动程序中添加了对 AIO 的支持(内核态中的 .aio_read 、 .aio_write 调用,用户态中的 libaio )并查看了各种来源,但我找不到如果在我的 aio_read、.aio_write 调用中我可以只存储一个指向的指针iovector 参数(假设此内存将保持不变,直到调用 aio_complete 之后),或者我需要对 iovector 数据结构进行深度复制。

static ssize_t aio_read( struct kiocb *iocb, const struct iovec *iovec, unsigned long nr_segs, loff_t pos );
static ssize_t aio_write( struct kiocb *iocb, const struct iovec *iovec, unsigned long nr_segs, loff_t pos );

以\drivers\usb\gadget\inode.c 的实现为例,他们似乎只是复制了 ep_aio_rwtail 函数中的指针,该函数具有:

priv->iv = iv;

但是当我尝试做类似的事情时,它经常发生在我处理它时 iovector 中的数据已经“损坏”。

例如,在我记录的 aio_read/write 调用中

iovector located at addr:0xbf1ebf04
segment 0: base: 0x76dbb468 len:512

但是当我在内核线程中进行真正的工作时(附加到用户空间 mm 之后),我记录了以下内容:

iovector located at addr:0xbf1ebf04
segment 0: base: 0x804e00c8 len:-1088503900

这是一个非常简单的测试用例,我只在我的用户应用程序中提交了 1 个异步命令。

为了让事情更有趣:我在 3.13 内核上大约有 80% 的时间出现损坏。

但我以前从未在 3.9 内核上看到它(但在我升级到 3.13 之前我只使用了一小段时间,现在恢复正常并尝试了十几次左右)。(使用 3.9 内核运行的示例有两次

iovector located at addr:0xbf9ee054
segment 0: base: 0x76e28468 len:512)

这是否敲响了警钟?

(当然,另一种可能性是我自己破坏了这些地址/长度,但奇怪的是我从来没有在 3.9 上遇到过这种情况)

编辑:在查看 linux aio 的 3.13 代码后回答我自己的问题(它在 3.9 中发生了显着变化),在 fs\aio.c 中你有:

static ssize_t aio_run_iocb(struct kiocb *req, unsigned opcode,
char __user *buf, bool compat)
{
...
结构 iovec inline_vec, *iovec = &inline_vec;
...
ret = rw_op(req, iovec, nr_segs, req->ki_pos);
...
}

所以这个iovec结构只是在栈上,一旦aio_read/write函数退出,它就会丢失。

并且小工具框架在\drivers\usb\gadget\inode.c 中包含一个错误(至少对于 3.13)...

最佳答案

来自man page for aio_read ;

NOTES It is a good idea to zero out the control block before use. The control block must not be changed while the read operation is in progress. The buffer area being read into must not be accessed during the operation or undefined results may occur. The memory areas involved must remain valid.

Simultaneous I/O operations specifying the same aiocb structure produce undefined results.

这表明驱动程序可以在操作期间依赖用户的数据结构。如果在操作过程中检测到这些结构已更改,则放弃操作并返回异步错误是明智的做法。

关于c - Linux内核异步AIO : do I need to copy over the struct iovec for later processing?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22228347/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com