Linux内核中list_for_each()和list_for_each_save()

Linux内核List遍历

今天看Linux内核代码,看到了list_for_each_save()函数,想仔细的研究一下。

下面是源代码:

list_for_each()的定义:

/**
 * list_for_each	-	iterate over a list
 * @pos:	the &struct list_head to use as a loop cursor.
 * @head:	the head for your list.
 */
#define list_for_each(pos, head)  for (pos = (head)->next; pos != (head); pos = pos->next)

list_for_each_save()的定义:

/**
 * list_for_each_safe - iterate over a list safe against removal of list entry
 * @pos:	the &struct list_head to use as a loop cursor.
 * @n:		another &struct list_head to use as temporary storage
 * @head:	the head for your list.
 */
#define list_for_each_safe(pos, n, head) \
	for (pos = (head)->next, n = pos->next; pos != (head); \
		pos = n, n = pos->next)

下面主要是介绍一下他们的区别:

    我们指导在遍历一个双向链表list的时候有可能会删除其中的元素,这就有可能出现问题。我们首先看一下删除链表元素的

函数,函数如下:(注意:这里我们队Linux内核函数中设计的宏定义等进行了展开)

static inline void list_del(struct list_head *entry)
{
	entry->next->prev=entry->prev;
	entry->prev->next=entry->next;
        entry->next = LIST_POISON1;
	entry->prev = LIST_POISON2;
}

这里先说一下下面两行代码:

entry->next = LIST_POISON1;
entry->prev = LIST_POISON2;
下面是LIST_POISON1和LIST_POISON2的出处:
#ifdef CONFIG_ILLEGAL_POINTER_VALUE
# define POISON_POINTER_DELTA _AC(CONFIG_ILLEGAL_POINTER_VALUE, UL)
#else
# define POISON_POINTER_DELTA 0
#endif
/*通常情况下,非空指针会导致页错误,用于验证没有初始化的list元素。
 * These are non-NULL pointers that will result in page faults
 * under normal circumstances, used to verify that nobody uses
 * non-initialized list entries.
 */
#define LIST_POISON1  ((void *) 0x100 + POISON_POINTER_DELTA)
#define LIST_POISON2  ((void *) 0x200 + POISON_POINTER_DELTA)

删除元素过程见下图:

删除前:


删除后:


    当我们采用list__for_each()函数遍历list时,如果我们删除元素,就会导致pos指向的元素的prev=LIST_POISON1,

next=LIST_POISON2,当执行到pos=pos->next时,就会出现错误。

    但是当我们使用list_for_each_save()函数遍历list时,如果我们也删除元素后,会执行pos=n,而n=pos->next,注意:n=pos->next中的pos是删除的那个元素,所以虽然删除了元素pos,但是执行了pos=n后,pos指向了正确的遍历位置,所以使用list_for_each_save()函数遍历list时并不会出现错误。list_for_each_save()在遍历时之所以不会出现错误,是因为我们使用了n暂时保存了pos,也就是被删除元素所指向的下一个元素,所以用这个函数,准确来说是宏定义,不会出现遍历时删除元素的错误。






`__smpboot_create_thread()` 是 Linux 内核中 `smpboot_create_threads()` 函数的底层实现函数之一,它负责 **为指定的 CPU 创建一个热插拔线程**。该函数通常不直接调用,而是由 `smpboot_create_threads()` 在 CPU 上线时调用,用于动态创建如 `ksoftirqd`、`migration`、`watchdog` 等 per-CPU 内核线程。 --- ## 一、函数定义 该函数定义在 `kernel/smpboot.c` 中: ```c static int __smpboot_create_thread(struct smp_hotplug_thread *tsk_struct, unsigned int cpu) ``` - `tsk_struct`:指向 `smp_hotplug_thread` 结构体,描述线程的行为。 - `cpu`:要为其创建线程的 CPU ID。 --- ## 二、函数功能详解 以下是简化版的 `__smpboot_create_thread()` 函数逻辑: ```c static int __smpboot_create_thread(struct smp_hotplug_thread *tsk_struct, unsigned int cpu) { struct task_struct *tsk; /* 1. 创建内核线程 */ tsk = kthread_create_on_cpu(tsk_struct->thread_fn, tsk_struct->thread_data, cpu, tsk_struct->thread_comm); if (IS_ERR(tsk)) { pr_err("Failed to create %s/%u: %ld\n", tsk_struct->store_name, cpu, PTR_ERR(tsk)); return PTR_ERR(tsk); } /* 2. 绑定线程到指定 CPU */ kthread_bind(tsk, cpu); /* 3. 保存线程指针到结构体中 */ tsk_struct->thread[cpu] = tsk; /* 4. 可选:调用 setup 回调 */ if (tsk_struct->setup) tsk_struct->setup(cpu); /* 5. 唤醒线程 */ wake_up_process(tsk); return 0; } ``` --- ## 三、关键步骤解析 ### 1. 创建线程:`kthread_create_on_cpu()` ```c tsk = kthread_create_on_cpu(tsk_struct->thread_fn, data, cpu, comm); ``` - `thread_fn`:线程执行的函数(如 `ksoftirqd_should_run()`)。 - `data`:传给线程的参数。 - `cpu`:指定线程运行在哪个 CPU 上。 - `comm`:线程名(如 `"ksoftirqd/%u"`)。 `kthread_create_on_cpu()` 是创建内核线程的专用函数,与 `kthread_create()` 不同的是,它会直接绑定线程到指定 CPU。 ### 2. 绑定线程到指定 CPU ```c kthread_bind(tsk, cpu); ``` 该函数确保线程始终运行在指定的 CPU 上(通过设置 CPU 亲性)。 ### 3. 保存线程句柄 ```c tsk_struct->thread[cpu] = tsk; ``` 每个 CPU 都有自己的线程实例,`thread[cpu]` 用于保存该线程的指针。 ### 4. 调用 `setup()` 回调(可选) ```c if (tsk_struct->setup) tsk_struct->setup(cpu); ``` 允许模块或子系统在创建线程后做一些初始化工作。 ### 5. 启动线程 ```c wake_up_process(tsk); ``` 唤醒线程使其开始运行。 --- ## 四、完整流程图(逻辑) ``` smpboot_create_threads(cpu) └── list_for_each_entry(cur, &hotplug_threads, list) └── cur->create(cpu) └── __smpboot_create_thread(cur, cpu) ├── kthread_create_on_cpu() ├── kthread_bind() ├── save thread pointer ├── cur->setup(cpu) └── wake_up_process() ``` --- ## 五、示例:注册一个热插拔线程 以下是一个完整的模块示例,注册一个 per-CPU 线程: ```c #include <linux/module.h> #include <linux/kthread.h> #include <linux/smpboot.h> #include <linux/cpu.h> static int my_thread_fn(void *data) { unsigned int cpu = (unsigned long)data; while (!kthread_should_stop()) { pr_info("my_thread/%u is running on CPU %u\n", cpu, smp_processor_id()); schedule_timeout_interruptible(msecs_to_jiffies(1000)); } return 0; } static struct smp_hotplug_thread my_thread_desc = { .store_name = "my_thread", .thread_comm = "my_thread/%u", .thread_fn = my_thread_fn, .thread_data = (void *)0UL, }; static int __init my_init(void) { int ret; ret = smpboot_register_percpu_thread(&my_thread_desc); if (ret) { pr_err("Failed to register per-CPU thread\n"); return ret; } return 0; } static void __exit my_exit(void) { smpboot_unregister_percpu_thread(&my_thread_desc); } module_init(my_init); module_exit(my_exit); MODULE_LICENSE("GPL"); ``` --- ## 六、常见问题 | 问题 | 解答 | |------|------| | `__smpboot_create_thread()` `smpboot_create_threads()` 的区别? | `__smpboot_create_thread()` 是底层函数,`smpboot_create_threads()` 是高层函数,遍历所有注册的线程并调用底层函数创建。 | | 如何查看系统中创建的 per-CPU 线程? | 使用 `ps -ef | grep ksoftirqd` 或 `ps -ef | grep migration` 等命令。 | | 为什么需要绑定线程到特定 CPU? | 确保线程只在指定 CPU 上运行,避免上下文切换缓存失效,提高性能。 | --- ##
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值