【问题标题】:Kernel oops when executing function to read hardware registers执行读取硬件寄存器的函数时内核 oops
【发布时间】:2015-05-11 17:52:54
【问题描述】:

我参考this answer for crash 帮助分析导致问题的这段代码。每个人的上下文,我正在工作一个字符驱动程序,它将充当从用户空间直接到硬件的通道,用于 ahci 驱动程序。为此,我正在相应地修改 ahci 驱动程序。

我从小处着手。我想查看我的 VM 上 AHCI HBA 的 HBA 端口 0 的端口寄存器。我的字符驱动ioctl代码:

switch (cmd) {
    case AHCIP_GPORT_REG:
        pPciDev = pci_get_device(0x8086, 0x2829, NULL);

        if (pPciDev) {
            /* This will set ret to the value that it needs to be.  This
             * is true of __put_user() too */
            if ((ret = __get_user(off, (u32*)obj))) {
                printk(KERN_INFO "unable to read from user space\n");
                goto ioctl_quick_out;
            }

            reg = get_port_reg(&pPciDev->dev, off);
            if ((ret = __put_user(reg, (u32*)obj)))
            {
                printk(KERN_INFO "Unable to write to user space\n");
            }

            pci_dev_put(pPciDev);
        }

        // This break wasn't in the code when it crashed
        break;

    default:
        // POSIX compliance with this one (REF of LDD3)
        ret = -ENOTTY;
}

此字符驱动程序调用的我修改后的 ahci.c 版本的代码:

u32 get_port_reg(struct device *dev, u32 off)
{
    struct Scsi_Host *shost = class_to_shost(dev);
    struct ata_port *ap = ata_shost_to_port(shost);
    void __iomem *port_mmio = ahci_port_base(ap);

    return ioread32(port_mmio + off);
}
EXPORT_SYMBOL(get_port_reg);

这导致的内核 oops 发生在这里:

PID: 3357   TASK: ffff88011c9b7500  CPU: 0   COMMAND: "peek"
 #0 [ffff8800abfc79f0] machine_kexec at ffffffff8103b5bb
 #1 [ffff8800abfc7a50] crash_kexec at ffffffff810c9852
 #2 [ffff8800abfc7b20] oops_end at ffffffff8152e0f0
 #3 [ffff8800abfc7b50] no_context at ffffffff8104c80b
 #4 [ffff8800abfc7ba0] __bad_area_nosemaphore at ffffffff8104ca95
 #5 [ffff8800abfc7bf0] bad_area at ffffffff8104cbbe
 #6 [ffff8800abfc7c20] __do_page_fault at ffffffff8104d36f
 #7 [ffff8800abfc7d40] do_page_fault at ffffffff8153003e
 #8 [ffff8800abfc7d70] page_fault at ffffffff8152d3f5
    [exception RIP: get_port_reg+18]
    RIP: ffffffffa03c4cd2  RSP: ffff8800abfc7e28  RFLAGS: 00010246
    RAX: 0000000000020101  RBX: 00007fff17273960  RCX: ffffffff812b0710
    RDX: ffff88011ddd5000  RSI: 0000000000000000  RDI: ffff88011ddd5090
    RBP: ffff8800abfc7e28   R8: 0000000000000000   R9: 0000000000000000
    R10: 00000000000007d5  R11: 0000000000000006  R12: ffff88011ddd5000
    R13: 0000000000000000  R14: 0000000000000000  R15: 0000000000000000
    ORIG_RAX: ffffffffffffffff  CS: 0010  SS: 0018

如您所见,指令指针为get_port_reg+18。由于这个函数很小,这里是完整的反汇编

crash> dis get_port_reg
0xffffffffa03c4cc0 <get_port_reg>:      push   %rbp
0xffffffffa03c4cc1 <get_port_reg+1>:    mov    %rsp,%rbp
0xffffffffa03c4cc4 <get_port_reg+4>:    nopl   0x0(%rax,%rax,1)
0xffffffffa03c4cc9 <get_port_reg+9>:    mov    0x240(%rdi),%rax
0xffffffffa03c4cd0 <get_port_reg+16>:   mov    %esi,%esi
0xffffffffa03c4cd2 <get_port_reg+18>:   mov    0x2838(%rax),%rdx
0xffffffffa03c4cd9 <get_port_reg+25>:   mov    0x28(%rax),%eax
0xffffffffa03c4cdc <get_port_reg+28>:   mov    0x10(%rdx),%rdx
0xffffffffa03c4ce0 <get_port_reg+32>:   shl    $0x7,%eax
0xffffffffa03c4ce3 <get_port_reg+35>:   mov    %eax,%eax
0xffffffffa03c4ce5 <get_port_reg+37>:   add    0x28(%rdx),%rax
0xffffffffa03c4ce9 <get_port_reg+41>:   lea    0x100(%rax,%rsi,1),%rdi
0xffffffffa03c4cf1 <get_port_reg+49>:   callq  0xffffffff8129dde0 <ioread32>
0xffffffffa03c4cf6 <get_port_reg+54>:   leaveq 
0xffffffffa03c4cf7 <get_port_reg+55>:   retq   
0xffffffffa03c4cf8 <get_port_reg+56>:   nopl   0x0(%rax,%rax,1)

您可能已经猜到了,我是一个组装新手。哪一行代码是get_port_reg+18?我很困惑,因为我在该函数的每一行都调用函数,但我看到的唯一调用是ioread32()

作为参考,我在ahci_show_port_cmd() within the same file 之后建模了我的函数get_port_reg。我想不出任何其他方法来获得运行它所必需的struct pci_dev 结构。我是不是在滥用get_pci_device()pci_dev_put()?这根本不是问题吗?

感谢您的帮助
安迪

【问题讨论】:

  • pci_get_device() 可能返回一个 pci 设备,而您需要相应的 SCSI 主机设备——它们有不同的类。
  • 看看ata_pci_remove_one:lxr.free-electrons.com/source/drivers/ata/libata-core.c#L6314,它展示了如何从PCI设备获取ata_host并从中获取ata_port应该很简单。
  • 看来问题出在这里 - ata_shost_to_port。我可以从技术角度告诉你,没有代码理解:函数class_to_shost基本上是一个container_of,所以它假设struct device(dev)嵌入到struct Scsi_host中,因此它将dev适当地转换为Scsi_host。接下来,您的代码尝试取消引用 shost 以获取 ata_port (*(struct ata_port **)&host->hostdata[0])。并且繁荣......似乎这里发生了page_fault。所以这意味着可能有一个垃圾而不是 struct Scsi_host ...

标签: linux-kernel linux-device-driver


【解决方案1】:

我将发布我自己的答案。我的问题的两位评论员让我走上了解决这个问题的正确道路。正如我所提到的,我的方法是做一些我在 ahci 驱动程序 (ahci.c) 中看到过的事情。基本上,假设很简单,this function in ahci.c 需要 struct device* 并从中能够获得所需的 ata_port 信息。我在 ahci.c 中看到过作者偶尔会使用struct device* = &amp;pdev-&gt;dev;。换句话说,我认为struct pci_devdev 成员正在为我提供我需要的东西。我显然不知道“类类型”或类似的东西(参见@myaut 的第一条评论)。 @alexhoppus 根据我发布的代码和反汇编基本上得出了相同/相似的结论。

我采用的修复方法,效果很好,如下:

/* ioctl code in character driver */
switch (cmd) {
    case AHCIP_GPORT_REG:
        pPciDev = pci_get_device(0x8086, 0x2829, NULL);

        if (pPciDev) {
            struct ata_host *pHost = NULL;
            struct ata_port *pPort = NULL;
            printk(KERN_INFO "found the PCI device\n");
            /* Get the devices driver data */
            pHost = pci_get_drvdata(pPciDev);
            if (!pHost) {
                ret = -EFAULT;
                goto ioctl_valid_pci_dev_out;
            }

            /* for this test, we'll use just port 0 */
            pPort = pHost->ports[0];
            if (!pPort) {
                ret = -EFAULT;
                goto ioctl_valid_pci_dev_out;
            }

            /* This will set ret to the value that it needs to be.  This
             * is true of __put_user() too */
            if ((ret = __get_user(off, (u32*)obj))) {
                printk(KERN_INFO "unable to read from user space\n");
                goto ioctl_valid_pci_dev_out;
            }

            reg = get_port_reg(pPort, off);
            if ((ret = __put_user(reg, (u32*)obj)))
            {
                printk(KERN_INFO "Unable to write to user space\n");
            }
        }

        break;

    default:
        // POSIX compliance with this one (REF of LDD3)
        ret = -ENOTTY;
}

ahci 驱动也被修改了

u32 get_port_reg(struct ata_port* pPort, u32 off)
{
    void __iomem *port_mmio = ahci_port_base(pPort);

    return ioread32(port_mmio + off);
}
EXPORT_SYMBOL(get_port_reg);

虽然这已经解决了我的问题,但我非常感谢有人向我解释 (struct pci_dev)device.dev.p-&gt;driver_data. I can use, and have, the Linux cross referencing tools to see the data types. What is supposed to be stored instruct device_private 中的内容? This is the structure 我现在用它来获取我需要的数据。我真的很感谢有人评论这个答案来解释那个答案。

感谢@myaut 和@alexhoppus

【讨论】:

    猜你喜欢
    • 2019-08-01
    • 2010-10-13
    • 1970-01-01
    • 2013-04-28
    • 1970-01-01
    • 2014-02-19
    • 1970-01-01
    • 1970-01-01
    • 2023-01-12
    相关资源
    最近更新 更多