我试图从mm/filemap.c中打印__generic_file_write_iter()中的一些信息。
我对函数进行了如下修改:

struct file *file = iocb->ki_filp;
struct address_space * mapping = file->f_mapping;
struct inode    *inode = mapping->host;
ssize_t         written = 0;
ssize_t         err;
ssize_t         status;

/* We can write back this queue in page reclaim */
current->backing_dev_info = inode_to_bdi(inode);
err = file_remove_privs(file);
if (err)
        goto out;

err = file_update_time(file);
if (err)
        goto out;

/* This if() is all I have added */
if(io_tracing_on) {
        ssize_t write_size = iov_length(from->iov, from->nr_segs);
        printk(KERN_INFO "write size=%zu, pid=%d, inode=%lu\n", write_size, task_pid_nr(current), inode->i_ino);
}

if (iocb->ki_flags & IOCB_DIRECT) {
        loff_t pos, endbyte;
        ...

io_tracing_on是一个变量,我通过/proc条目(由内置于内核中的模块创建)设置。当我打开开关并运行dd(viadd if=/dev/urandom of=/tmp/gibberish bs=1M count=1)时,我得到一个到/var/log/syslog的连续输出流,即:
Jun 27 15:00:41 malka kernel: [  463.424155] write size=168, pid=715, inode=7864653
Jun 27 15:00:41 malka kernel: [  463.428064] write size=168, pid=715, inode=7864354
Jun 27 15:00:41 malka kernel: [  463.428126] write size=168, pid=715, inode=7864653
Jun 27 15:00:41 malka kernel: [  463.432061] write size=168, pid=715, inode=7864354
Jun 27 15:00:41 malka kernel: [  463.432121] write size=168, pid=715, inode=7864653
Jun 27 15:00:41 malka kernel: [  463.436075] write size=168, pid=715, inode=7864354
Jun 27 15:00:41 malka kernel: [  463.436133] write size=168, pid=715, inode=7864653
Jun 27 15:00:41 malka kernel: [  463.440060] write size=168, pid=715, inode=7864354
Jun 27 15:00:41 malka kernel: [  463.440121] write size=168, pid=715, inode=7864653
etc

然而,当我运行ftrace(使用“function”跟踪器)时,我从未看到__generic_file_write_iter()被调用。那么,为什么我的printk()语句会在ftrace输出中不出现__generic_file_write_iter()的情况下被连续调用?
内核版本是4.5.5。
更新
以前,我无法将PID与任何有效进程关联。在将代码更改为打印pid和tgid之后,我能够将tgid与进程关联起来。似乎syslog每次写入时都在调用myprintk(),从而导致连续的输出流。但是,ftrace仍然没有显示__generic_file_write_iter()被调用的次数足够近,无法反映调用myprintk()的次数。所以,我的问题仍然是——如果ftrace按我的预期工作,我可以想象在ftrace中,我在syslog中看到的每个__generic_file_write_iter()都有一个调用printk()

最佳答案

然而,当我运行ftrace(使用“function”跟踪器)时,我从未看到
接到电话。
很可能,__generic_file_write_iter()没有在__generic_file_write_iter中列出(ftrace可以跟踪的函数,请参见ftrace - Function TracerSecrets of the Ftrace function tracer)。

08-04 23:31