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

相关推荐
Fu20672118 小时前
结课项目考试
linux·运维·服务器
Albert·Lin19 小时前
LVS相关知识点总结-一
linux·服务器·lvs
tianyuanwo20 小时前
深入理解 Linux 启动救援模式与控制台选择:从 x86 的 tty0 到 ARM 的 ttyS0
linux·grub·救援系统
瞬间&永恒~20 小时前
【MySQL】练习5-1:配置慢查询日志
linux·运维·云原生
邪修king21 小时前
Re:Linux系统篇(四):权限Chapter--用户身份、权限位与目录权限三大核心问题
linux·运维·服务器
buhuizhiyuci21 小时前
【Linux 篇】数字世界的通信管道 —— 匿名管道与进程池深度实战解析
linux·运维·服务器
道尔柯南21 小时前
【Linux】基础开发工具Vim
linux·运维·vim
江南风月1 天前
WGCLOUD是如何监控服务器上的异常进程的
linux·运维·服务器
无锡耐特森1 天前
ModbusTCP转Profinet:打通潜油电泵工业互联新路径
linux·运维·服务器
黑白园1 天前
个人台式机VMware 15及Ubuntu 16.04.5卸载以及VMware 16.2.3及Ubuntu 24.04.4安装
linux·ubuntu