linux内核中的poll_list
时间: 2023-06-27 11:04:46 浏览: 121
`poll_list`是Linux内核中与`poll()`系统调用相关的数据结构之一。
在Linux中,`poll()`系统调用是用于等待多个文件描述符的状态变化的。它的原理是,用户进程通过`poll()`系统调用告诉内核要等待哪些文件描述符的状态变化,内核则把这些文件描述符加入到`poll_list`链表中,然后等待这些文件描述符中的任意一个发生状态变化,最后通知用户进程。
`poll_list`是一个由`poll_table`结构体组成的链表,每个`poll_table`结构体表示一个等待队列(wait queue),用于保存等待某个文件描述符状态变化的进程。每当一个进程调用`poll()`系统调用并等待某个文件描述符时,它就会被加入到相应的等待队列中。当该文件描述符的状态发生变化时,内核会遍历该文件描述符对应的`poll_table`结构体所在的链表,并通知这些等待队列中的进程。
总之,`poll_list`是一个用于保存等待队列的链表,它是`poll()`系统调用的核心数据结构之一。
相关问题
linux的softnet_data结构体
softnet_data结构体是Linux内核中网络软中断处理机制的关键数据结构。它定义在include/net_softnet.h头文件中,其定义如下:
```
struct softnet_data {
struct list_head input_pkt_queue;
struct list_head process_queue;
#ifdef CONFIG_NET_RX_BUSY_POLL
u64 start_jiffies;
#endif
atomic_t poll_list_len;
atomic_t process_budget;
atomic_t backlog;
bool have_raw; /* raw sockets are present, optimize for this */
bool have_fq;
bool have_fq_tea;
#ifdef CONFIG_RPS
bool have_rps_sock;
#endif
#ifdef CONFIG_RFS_ACCEL
bool have_rfs_sock;
#endif
};
```
其中,softnet_data结构体成员说明如下:
- input_pkt_queue:存储网络接收的数据包队列的头部。
- process_queue:存储网络接收的数据包经过协议栈处理后的数据包队列的头部。
- start_jiffies:网络接收数据包的起始时间。
- poll_list_len:网络接收数据包队列的长度。
- process_budget:网络接收数据包被处理的预算。
- backlog:网络接收数据包被缓存的数量。
- have_raw:标识是否存在原始套接字。
- have_fq:标识是否启用了Fair Queueing。
- have_fq_tea:标识是否启用了Traffic Engineering and Accounting。
- have_rps_sock:标识是否存在RPS套接字。
- have_rfs_sock:标识是否存在RFS套接字。
该结构体主要用于协议栈接收数据包时的软中断处理机制,其中包括将接收到的数据包添加到input_pkt_queue、将input_pkt_queue中的数据包经过协议栈处理后添加到process_queue等操作。
linux内核与mailbox相关的代码
以下是Linux内核与mailbox相关的代码:
1. arch/arm/include/asm/mailbox.h
#ifndef __ASM_ARM_MAILBOX_H
#define __ASM_ARM_MAILBOX_H
#include <linux/types.h>
struct mbox_chan {
u32 id;
u32 irq;
void __iomem *regs;
unsigned long count;
};
#endif /* __ASM_ARM_MAILBOX_H */
2. arch/arm/kernel/mbox.c
#include <linux/clk.h>
#include <linux/device.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <asm/mailbox.h>
#define MBOX_POLL_INTERVAL_MS 5
static struct mbox_chan *mbox_chans;
static int mbox_chan_count;
static int mbox_poll(struct mbox_chan *chan)
{
int i;
for (i = 0; i < MBOX_POLL_INTERVAL_MS; i++) {
if (readl(chan->regs + MBOX_STATUS_REG) & MBOX_STATUS_EMPTY)
return 0;
msleep(1);
}
return -ETIMEDOUT;
}
static irqreturn_t mbox_interrupt(int irq, void *dev_id)
{
struct mbox_chan *chan = dev_id;
if (readl(chan->regs + MBOX_STATUS_REG) & MBOX_STATUS_EMPTY)
return IRQ_NONE;
chan->count++;
return IRQ_HANDLED;
}
static int mbox_open(struct inode *inode, struct file *filp)
{
struct mbox_chan *chan = inode->i_private;
int ret;
ret = request_irq(chan->irq, mbox_interrupt, 0, "mbox", chan);
if (ret)
return ret;
filp->private_data = chan;
return 0;
}
static int mbox_release(struct inode *inode, struct file *filp)
{
struct mbox_chan *chan = filp->private_data;
free_irq(chan->irq, chan);
return 0;
}
static ssize_t mbox_read(struct file *filp, char __user *buf, size_t count,
loff_t *f_pos)
{
struct mbox_chan *chan = filp->private_data;
u32 value;
int ret;
ret = mbox_poll(chan);
if (ret)
return ret;
value = readl(chan->regs + MBOX_READ_REG);
if (copy_to_user(buf, &value, sizeof(u32)))
return -EFAULT;
return sizeof(u32);
}
static ssize_t mbox_write(struct file *filp, const char __user *buf, size_t count,
loff_t *f_pos)
{
struct mbox_chan *chan = filp->private_data;
u32 value;
if (count != sizeof(u32))
return -EINVAL;
if (copy_from_user(&value, buf, sizeof(u32)))
return -EFAULT;
writel(value, chan->regs + MBOX_WRITE_REG);
return sizeof(u32);
}
static const struct file_operations mbox_fops = {
.owner = THIS_MODULE,
.read = mbox_read,
.write = mbox_write,
.open = mbox_open,
.release = mbox_release,
};
static int mbox_probe(struct platform_device *pdev)
{
struct mbox_chan *chans;
struct device_node *np = pdev->dev.of_node;
struct resource *res;
int i, ret;
mbox_chan_count = of_get_child_count(np);
if (!mbox_chan_count) {
dev_err(&pdev->dev, "no mailbox channels found\n");
return -ENODEV;
}
chans = devm_kzalloc(&pdev->dev, sizeof(*chans) * mbox_chan_count,
GFP_KERNEL);
if (!chans)
return -ENOMEM;
for (i = 0; i < mbox_chan_count; i++) {
struct device_node *child = of_get_next_child(np, NULL);
if (!child) {
dev_err(&pdev->dev, "failed to get child node\n");
return -ENODEV;
}
chans[i].regs = of_iomap(child, 0);
if (!chans[i].regs) {
dev_err(&pdev->dev, "failed to map mailbox registers\n");
return -ENODEV;
}
res = platform_get_resource(pdev, IORESOURCE_IRQ, i);
if (!res) {
dev_err(&pdev->dev, "failed to get IRQ resource\n");
return -ENODEV;
}
chans[i].irq = res->start;
chans[i].id = i;
of_node_put(child);
}
ret = platform_device_register_resndata(&pdev->dev, "mbox", -1, chans,
sizeof(*chans) * mbox_chan_count);
if (ret)
dev_err(&pdev->dev, "failed to register platform device\n");
mbox_chans = chans;
return ret;
}
static int mbox_remove(struct platform_device *pdev)
{
platform_device_unregister(&pdev->dev);
kfree(mbox_chans);
return 0;
}
static const struct of_device_id mbox_of_match[] = {
{ .compatible = "arm,mbox" },
{ },
};
MODULE_DEVICE_TABLE(of, mbox_of_match);
static struct platform_driver mbox_driver = {
.driver = {
.name = "mbox",
.of_match_table = mbox_of_match,
},
.probe = mbox_probe,
.remove = mbox_remove,
};
static int __init mbox_init(void)
{
return platform_driver_register(&mbox_driver);
}
subsys_initcall(mbox_init);
static void __exit mbox_exit(void)
{
platform_driver_unregister(&mbox_driver);
}
module_exit(mbox_exit);
MODULE_AUTHOR("Linux Kernel Mailing List <linux-kernel@vger.kernel.org>");
MODULE_DESCRIPTION("Mailbox driver for ARM processors");
MODULE_LICENSE("GPL");