目录
1. 前言
本专题文章是对多队列部分学习的笔记。主要以null_dev为例来研究多队列的工作机制, 为了能够凸显多队列的工作流程,只对block层做重点分析说明,对其它代码只做注释性说明。本文主要通过分析文件系统(ext4为例)读取null blk的流程,来理解多队列的读取流程。
kernel版本:5.10
平台:arm64
注:
为方便阅读,正文标题采用分级结构标识,每一级用一个"-“表示,如:两级为”|- -", 三级为”|- - -“
1.文件系统读取nullb文件总体流程
vfs_read
|--rw_verify_area(READ, file, pos, count)
\--new_sync_read(file, buf, count, pos)
|--init_sync_kiocb(&kiocb, filp);
|--iov_iter_init(&iter, READ, &iov, 1, len);
\--call_read_iter(filp, &kiocb, &iter)
\--ext4_file_read_iter(kio, iter)
new_sync_read:对于ext4的文件系统,由于没有定义file->f_op->read,而是定义了file->f_op->read_iter回调,因此会调用new_sync_read函数。这里查看了Documentation/filesystems/vfs.txt文件中关于read_iter的说明。这里说read_iter有可能是一种异步的读,并且会读取到iov_iter
read_iter: possibly asynchronous read with iov_iter as destination
-
init_sync_kiocb:用于初始化kiocb,包括文件描述符,标记,文件位置等。kiocb保存了文件描述符file的位置等信息,相当于由kiocb控制对IO的读写操作,所以称为内核IO控制块
-
iov_iter_init:iov_iter是一个向量迭代器,指向一个iovec向量数组,用于推进io的读写, iov_iter_init用于初始化此向量迭代器;
-
call_read_iter: 将执行file->f_op->read_iter(kio, iter),最终调用了ext4_file_read_iter(kio, iter), 执行文件读操作
ext4_file_read_iter(kio, iter)
\--generic_file_read_iter(iocb, to)
\--if (iocb->ki_flags & IOCB_DIRECT)
ext4_dio_read_iter(iocb, to);
else
generic_file_buffered_read(iocb, iter, retval)
ext4_file_read_iter实际会分为direct io的处理和非direct io的处理,这里只分析非direct io的处理,主要调用generic_file_buffered_read
generic_file_buffered_read(struct kiocb *iocb, struct iov_iter *iter, ssize_t written)
|--if (PageReadahead(page))
| page_cache_async_readahead//异步读
|--wait_on_page_locked_killable(page)//如果页面锁定则说明正在读,block等待end bio唤醒即可
|--if (!trylock_page(page)) //如果页面没有被锁定,则跳转到page_not_up_to_date,锁定页面
| goto page_not_up_to_date
|--page_not_up_to_date:lock_page_killable(page)
|--mapping->a_ops->readpage(filp, page)//从磁盘读入缓存,end bio唤醒
\--if (PageUptodate(page))
unlock_page(page);
goto page_ok;
先来看一下generic_file_buffered_read的总体情况,根据前面的读取调用栈可知道实际调用了page_cache_async_readahead,执行异步预读操作。
-
page_cache_async_readahead:执行异步预读操作。
-
wait_on_page_locked_killable:重新检查页面是否已经被锁定,如果被锁定,则将被阻塞,直到读取完毕唤醒进程(通过end_io回调?)
-
readpage:对于页面没有被锁定的情况,说明并未启动页面读取,则通过readpage回调执行磁盘读取
page_cache_async_readahead()
|--page_cache_async_ra(&ractl, ra, page, req_count);
\--ondemand_readahead(ractl, ra, true, req_count);
\--do_page_cache_ra(ractl, req_size, 0);
\--page_cache_ra_unbounded(ractl, nr_to_read, lookahead_size);
\--read_pages(mapping, filp, &page_pool, nr_pages,gfp_mask)
|--const struct address_space_operations *aops = rac->mapping->a_ops;
| struct blk_plug plug;
|--blk_start_plug(&plug)
|--ext4_mpage_readpages(inode, rac, NULL)
\--blk_finish_plug(&plug)
ondemand_readahead:执行预读算法,其中offset为page在mapping所处的index,req_size为请求预读的大小
do_page_cache_ra: 真正执行读取一个disk chunk。它会先通过radix_tree_lookup查找page在mapping->i_pages中是否存在,如果不存在它会预先分配page, 然后通过read_pages来执行页面读取,如果存在则直接通过read_pages读取页面;
(1)blk_start_plug: 初始化进程blk_plug
(2)ext4_mpage_readpages: 从块设备中读取多个page, 主要通过submit_bio下发到了进程的plug队列
(3)blk_finish_plug: flush plug队列的请求(flush plug有哪些时机?TODO)
2.文件系统读取nullb文件详细流程
|- -blk_start_plug
进程plug队列初始化。如果进程的plug非空,则直接返回,否则为进程分配plug队列,并初始化
|- -ext4_mpage_readpages
从块设备中读取多个page, 主要通过submit_bio下发,尝试与当前进程的plug队列进行合并,如果不能合并,则尝试与调度队列合并,如果仍然不能合并,则为bio重新创建一个request, 插入到当前进程的plug队列。
|- -blk_finish_plug
blk_finish_plug(&plug)
\--blk_flush_plug_list(plug, false)
|--flush_plug_callbacks(plug, from_schedule);
\--if (!list_empty(&plug->mq_list))
blk_mq_flush_plug_list(plug, from_schedule)
|--LIST_HEAD(list);
| //将进程plug队列中的rq移到list中
|--list_splice_init(&plug->mq_list, &list)
|--list_sort(NULL, &list, plug_ctx_cmp)
|--do {
| struct list_head rq_list;
| struct request *rq, *head_rq = list_entry_rq(list.next);
| struct list_head *pos = &head_rq->queuelist;
| struct blk_mq_hw_ctx *this_hctx = head_rq->mq_hctx;
| struct blk_mq_ctx *this_ctx = head_rq->mq_ctx;
|
| //将list中的rq移到rq_list中
| list_cut_before(&rq_list, &list, pos);
| blk_mq_sched_insert_requests(this_hctx, this_ctx, &rq_list,from_schedule);
\ } while(!list_empty(&list));
blk_finish_plug: 将进程plug队列中的request向下派发
blk_flush_plug_list:如果plug->mq_list非空,因此会调用blk_mq_flush_plug_list
blk_mq_flush_plug_list: 将plug->mq_list合并进list,下发到硬件队列直接处理或暂存到软队列
(1)list_splice_init:将plug->mq_list合并进list
(2)list_sort:对list进行排序
(3)list_cut_before:通过while循环,将list下所有的request移入rq_list链表
(4)blk_mq_sched_insert_requests:根据是否有调度队列,及当前硬队列是否busy来将rq_list中的request派发给驱动进行直接处理 或 插入到软队列暂存。如果没有采用调度处理,且当前硬队列处于非busy状态,会直接将rq_list中的request派发给驱动进行直接处理;对于开启调度器的情况下,将rq_list链表中的 request尝试与调度队列进行合并
3. blk_mq_sched_insert_requests
对于blk_finish_plug中关于禁用/使能调度队列的不同处理,单独拿出来做一分析,如下分为禁用调度器的处理,使能调度器的处理,分别分析,同时不管对于前者还是后者都将调用blk_mq_run_hw_queue分发给硬件进行处理。
void blk_mq_sched_insert_requests(struct blk_mq_hw_ctx *hctx,
struct blk_mq_ctx *ctx,
struct list_head *list, bool run_queue_async)
|--struct elevator_queue *e;
| struct request_queue *q = hctx->queue;
| e = hctx->queue->elevator;
|
|--if (e && e->type->ops.mq.insert_requests)
| e->type->ops.mq.insert_requests(hctx, list, false)
| else
| if (!hctx->dispatch_busy && !e && !run_queue_async)
| blk_mq_try_issue_list_directly(hctx, list)
| blk_mq_insert_requests(hctx, ctx, list)//list中剩余的合并到软队列
\--blk_mq_run_hw_queue(hctx, run_queue_async)
blk_mq_sched_insert_requests:根据是否有调度队列,及当前硬队列是否busy来将rq_list中的request派发给硬件队列进行直接处理 或 插入到调度队列合并或暂存。
- 如果没有采用调度处理
如果当前硬队列处于非busy状态,会直接将rq_list中的request派发给了驱动行直接处理;
如果硬队列处于busy状态,则将request暂存到软队列; - 如果开启调度器的情况下
将rq_list链表中的 request尝试与调度队列进行合并
|- -blk_mq_try_issue_list_directly(禁用调度器的处理)
void blk_mq_try_issue_list_directly(struct blk_mq_hw_ctx *hctx,struct list_head *list
\--while (!list_empty(list))
struct request *rq = list_first_entry(list, struct request, queuelist)
list_del_init(&rq->queuelist);
blk_mq_request_issue_directly(rq)
|--struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
|--__blk_mq_try_issue_directly(hctx, rq, &unused_cookie, true, last);
|--struct request_queue *q = rq->q;
|--ret = __blk_mq_issue_directly(hctx, rq, cookie, last)
|--if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE)
blk_mq_request_bypass_insert(rq, false, true);
else if (ret != BLK_STS_OK)
blk_mq_end_request(rq, ret);
-
__blk_mq_issue_directly:直接派发给驱动处理
-
blk_mq_request_bypass_insert:对于由于资源不足导致无法执行的请求,将通过blk_mq_request_bypass_insert加入到硬队列hctx->dispatch
static blk_status_t __blk_mq_issue_directly(struct blk_mq_hw_ctx *hctx,
struct request *rq,
blk_qc_t *cookie, bool last)
|--struct request_queue *q = rq->q;
| struct blk_mq_queue_data bd = {
| .rq = rq,
| .last = last,
| }
\--ret = q->mq_ops->queue_rq(hctx, &bd);
switch (ret) {
case BLK_STS_OK:
blk_mq_update_dispatch_busy(hctx, false);
*cookie = new_cookie;
break;
case BLK_STS_RESOURCE:
case BLK_STS_DEV_RESOURCE:
blk_mq_update_dispatch_busy(hctx, true);
__blk_mq_requeue_request(rq);
break;
default:
blk_mq_update_dispatch_busy(hctx, false);
*cookie = BLK_QC_T_NONE;
break;
}
对于null block则q->mq_ops->queue_rq为null_queue_rq(hctx, &bd)
null_queue_rq(hctx, &bd)
\--blk_mq_start_request(bd->rq)
\--null_handle_cmd(cmd)
|--null_handle_rq(cmd)
| req_for_each_segment(bvec, rq, iter)
| null_transfer(nullb, bvec.bv_page, len, bvec.bv_offset...)
| |--copy_from_nullb(nullb, page, off, sector, len)
| \--flush_dcache_page(page)
\--blk_mq_complete_request(cmd->rq)
null_queue_rq:对请求进行处理,主要是从内存模拟的磁盘中拷贝到bio对应的bio_vec描述的page segment,处理完成后通过bio end回调通知完成
blk_mq_complete_request:负责上报request及bio完成,并执行对应的回调。注意此处是nullb设备的模拟,实际块设备中会通过中断来触发此函数执行。
blk_mq_complete_request(cmd->rq)
\--__blk_mq_complete_request(rq)
\--rq->q->softirq_done_fn(rq)
\--null_softirq_done_fn(rq)
\--end_cmd(blk_mq_rq_to_pdu(rq))
\--blk_mq_end_request(cmd->rq, cmd->error)
|--blk_update_request(rq, error, blk_rq_bytes(rq))
| |--while(req->bio)
| req_bio_endio(req, bio, bio_bytes, error)
\--__blk_mq_end_request(rq, error)
\--if (rq->end_io)
rq->end_io(rq, error)
else
blk_mq_free_request(rq)


blk_mq_complete_request负责上报request及bio完成,并执行对应的回调
-
null_softirq_done_fn: softirq_done_fn回调为null_softirq_done_fn
-
此处的rq->end_io为空, 因此会执行blk_mq_free_request
-
blk_update_request:遍历req的每个bio,执行bio endio回调,unlock_page解锁bio所关联的所有page
这样generic_file_buffered_read->wait_on_page_locked_killable的进程就会被唤醒了 -
__blk_mq_end_request:由于rq->end_io为空,会执行blk_mq_free_request
|- -dd_insert_requests(使能调度器的处理)
static void dd_insert_requests(struct blk_mq_hw_ctx *hctx, struct list_head *list, bool at_head)
|--struct request_queue *q = hctx->queue;
| struct deadline_data *dd = q->elevator->elevator_data;
|--while (!list_empty(list))
struct request *rq;
rq = list_first_entry(list, struct request, queuelist)
list_del_init(&rq->queuelist);
dd_insert_request(hctx, rq, at_head);
atomic_inc(&hctx->elevator_queued);
dd_insert_requests: 当使能调度器的情况下,从初始化函数可以知道,对于null_dev使用的是deadline调度器(默认均使用此调度器),因此e->type->ops.mq.insert_requests为dd_insert_requests,因此会调用dd_insert_requests尝试将进程plug队列中的request与调度器队列进行合并
static void dd_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq, bool at_head)
|--struct request_queue *q = hctx->queue;
| struct deadline_data *dd = q->elevator->elevator_data;
| const int data_dir = rq_data_dir(rq);
| //如果可以合并则执行合并
|--if (blk_mq_sched_try_insert_merge(q, rq))
| return;
|--if (at_head || blk_rq_is_passthrough(rq))
| list_add_tail(&rq->queuelist, &dd->dispatch);
| else
| deadline_add_rq_rb(dd, rq);
| if (rq_mergeable(rq))
| elv_rqhash_add(q, rq);
| if (!q->last_merge)
| q->last_merge = rq;
| rq->fifo_time = jiffies + dd->fifo_expire[data_dir];
| list_add_tail(&rq->queuelist, &dd->fifo_list[data_dir]);
-
blk_mq_sched_try_insert_merge: 通过循环调用elv_rqhash_find(q, blk_rq_pos(rq))快速查找是否有可以与req合并的请求,如果有就执行合并
-
如果无法与调度队列合并则通过list_add(&rq->queuelist, &dd->dispatch) or deadline_add_rq_rb(dd, rq) 暂存到调度队列
其中:
(1)list_add(&rq->queuelist, &dd->dispatch)会将request暂存到dd->dispatch链表
(2)deadline_add_rq_rb(dd, rq)会将request暂存到dd->sort_list[rq_data_dir(rq)]红黑树,同时如果request支持merger,也会通过elv_rqhash_add(q, rq)加入到hash表,下次可以快速查询新的rq是否可与暂存的requests执行merge
|- -blk_mq_run_hw_queue
void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
|--need_run = !blk_queue_quiesced(hctx->queue) && blk_mq_hctx_has_pending(hctx)
|--if (need_run) __blk_mq_delay_run_hw_queue(hctx, async, 0)
kblockd_mod_delayed_work_on(blk_mq_hctx_next_cpu(hctx), &hctx->run_work,
\--blk_mq_run_work_fn(struct work_struct *work)
\--__blk_mq_run_hw_queue(hctx)
\--blk_mq_sched_dispatch_requests(hctx)
|--if (!list_empty_careful(&hctx->dispatch))
| list_splice_init(&hctx->dispatch, &rq_list)
\--if (!list_empty(&rq_list))
blk_mq_dispatch_rq_list(q, &rq_list, false)
else if (has_sched_dispatch)
blk_mq_do_dispatch_sched(hctx)
\--__dd_dispatch_request
\--do {
rq = e->type->ops.mq.dispatch_request(hctx)
list_add(&rq->queuelist, &rq_list);
} while (blk_mq_dispatch_rq_list(q, &rq_list, true))
else if (hctx->dispatch_busy)
blk_mq_do_dispatch_ctx(hctx)
\--do {
rq = blk_mq_dequeue_from_ctx(hctx, ctx);
list_add(&rq->queuelist, &rq_list);
ctx = blk_mq_next_ctx(hctx, rq->mq_ctx)
} while (blk_mq_dispatch_rq_list(q, &rq_list, true))
else
blk_mq_flush_busy_ctxs(hctx, &rq_list)
blk_mq_dispatch_rq_list(q, &rq_list, false)
blk_mq_run_hw_queue真正将请求下发给驱动
blk_mq_sched_dispatch_requests:派发请求
(1)list_splice_init(&hctx->dispatch, &rq_list):优先处理之前在硬队列中没有派发的请求, 将其移入到rq_list
(2)对请求的处理,主要分几种情况(rq_list为待处理请求临时链表):
- blk_mq_dispatch_rq_list:优先派发之前在硬件度列中没有派发的请求到驱动;
- blk_mq_do_dispatch_sched:将调度队列中的请求移入rq_list,然后调用blk_mq_dispatch_rq_list,派发到驱动
- blk_mq_do_dispatch_ctx: 在硬队列busy的情况下,直接将软队列的rq移入到rq_list,然后派发给驱动, 公平起见,会考虑到轮流对多个软队列执行派发
- blk_mq_dispatch_rq_list: 其它情况将rq_list中请求派发给驱动处理
bool blk_mq_dispatch_rq_list(struct blk_mq_hw_ctx *hctx, struct list_head *list,unsigned int nr_budgets)
|--do {
| rq = list_first_entry(list, struct request, queuelist);
| list_del_init(&rq->queuelist);
| null_queue_rq(hctx, &bd)
| } while (!list_empty(list))
|--if (!list_empty(list))
list_splice_tail_init(list, &hctx->dispatch);
blk_mq_dispatch_rq_list将rq_list中请求派发给驱动处理, 对于null blk dev, 主要是调用了null_queue_rq进行请求的处理
4. 总结
从前面的分析可以知道,在读处理过程中,请求分别经历了进程的plug队列,调度队列,软队列,硬队列,驱动。当然这个过程也不是绝对的,比如在尝试与plug队列合并失败后,也会尝试与调度队列合并,调度队列在硬队列非busy的情况下,也会直接发送给硬队列处理:
- bio会尝试与plug队列合并,如果无法合并,尝试与调度队列合并,如果仍然无法合并,则新建request加入plug队列;
- 禁用调度器情况下
在硬队列非busy时将plug队列中的请求发送给驱动,如果遇到资源无法满足,则暂存到硬队列;在硬队列busy时,将plug队列中的请求暂存软队列; - 使能调度器情况下
plug队列请求尝试与调度器队列中的合并,如果无法合并则加入到调度器队列末尾; - 无论使能或禁用调度器情况下
(1) 对于进行IO操作时,都会优先派发硬队列剩余的请求到驱动,无法处理的请求,再加入硬队列
(2) 派发调度队列中的请求到驱动,无法处理的请求,再加入硬队列
(3) 在硬队列busy的情况下,将软队列的request直接发送给驱动处理,无法处理的再放入硬队列;
(4)在硬队列非busy的情况下,派发硬队列剩余的请求到驱动,无法处理的请求,再加入硬队列
6997




被折叠的 条评论
为什么被折叠?



