linux kernel:devres模块架构分析

参考文档:

https://www.kernel.org/doc/html/latest/driver-api/driver-model/devres.html

https://www.cnblogs.com/sammei/p/3498052.html devres in linux driver

bash 复制代码
devres:  Managed Device Resource
device resource management

devres_alloc()动态申请内存分配一个devres结构体管理resource资源。
1,指定需要的resource资源的内存大小
(由于不同的device所需的资源多少不一样,所以,需要device告知devres所需的内存多少)
2,指定释放resource资源的release()函数
(由于不同的device所需的资源不一样,所以,需要device告知release()函数,用于释放分配的资源)
3,devres功能是:device告知resource和release(),devres在适当的时机回调release()释放resource

devres功能:

devres api和初始数据结构:

相关文件:

include/linux/device.h

drivers/base/devres.c

初始数据结构图:

struct device里面的devres_head 链表头,用于管理devres

devm_pinctrl_register()举例devres使用案例

相关文件:drivers/pinctrl/core.c

该文件中只使用了如下devres api:

devres_alloc()

devres_free()

devres_add()

devres_release()

在kernel代码中,搜索了devres_alloc()函数。

有的是使用一级指针方式 :如 struct dma_devres *dr

大部分使用的都是二级指针方式 :如 struct pinctrl_dev **ptr

c 复制代码
devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL);
指定的resource资源内存大小是一个指针大小
指定的release释放资源的回调函数是:devm_pinctrl_dev_release

devres_alloc() -> devres_alloc_node() -> alloc_dr()

申请 "sizeof(struct devres) + size" 大小的内存, size 是指resource的大小. 

devres_alloc(...) 执行后数据结构图变化:

pinctrl_register(...)和devres_add(...)执行后数据结构图变化:

附 devres api 说明:

如下截图参考来自于:

https://www.cnblogs.com/sammei/p/3498052.html devres in linux driver

相关推荐
邪修king9 分钟前
Re:Linux系统篇(三十七):进程间通信Chapter4:System V 共享内存深度解析:为什么它是最快的 IPC?
android·linux·运维
张张123y1 小时前
2026 Agent 框架怎么选?拆 3 层底层逻辑(运行循环 / 记忆 / 工具协议)+ 一张决策表
linux·ubuntu·microsoft·langgraph·mcp·crewai·agent 框架
零域行者1 小时前
Linux入门基础命令
linux·服务器·安全·web安全
打工仔折腾 AI1 小时前
Linux 网络编程:从零掌握 TCP Socket 编程与 C++ 实战落地
linux·网络·人工智能·后端·python·tcp/ip·性能优化
硅基手札2 小时前
SPI技术系列 13 · SPI 调试与排查
linux·运维·stm32·单片机·嵌入式硬件·bug
小兔子2 小时前
Linux 的 write 返回成功,数据到底落在哪了
linux
我命由我123452 小时前
Python 开发 - Python 项目部署(Windows 与 Linux)
java·linux·windows·后端·python·java-ee·python3.11
erzong2312 小时前
Linux操作系统:基于环形队列&信号量实现多生产者多消费者的简易模型
linux·运维·服务器
小羊没烦恼!2 小时前
TypedSql:在 C# 类型系统上实现一个 SQL 查询引擎
java·linux·运维·服务器·开发语言·数据库·mysql
星恒随风2 小时前
Linux线程同步与互斥(二):从日志系统与线程池设计,到线程安全单例、可重入函数和死锁原理
linux·运维·状态模式