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

相关推荐
dishugj3 小时前
【linux】Redhat 6.3系统安装zabbix-agent软件包,无法使用YUM源问题
linux·运维·zabbix
无奈笑天下3 小时前
【麒麟镜像vmtools异常排查指导书】
linux·运维·经验分享·云计算·kylin
Xの哲學3 小时前
Linux多级时间轮:高精度定时器的艺术与科学
linux·服务器·网络·算法·边缘计算
QT 小鲜肉4 小时前
【Linux命令大全】001.文件管理之mmove命令(实操篇)
linux·服务器·前端·chrome·笔记
Winner13004 小时前
查看rk3566摄像头设备、能力、支持格式
linux·网络·人工智能
QT 小鲜肉5 小时前
【Linux命令大全】001.文件管理之mdel命令(实操篇)
linux·运维·服务器·chrome·笔记
大聪明-PLUS5 小时前
如何从零开始开发 Linux 驱动程序
linux·嵌入式·arm·smarc
物随心转5 小时前
input子系统工作原理
linux
一只旭宝6 小时前
Linux专题六:进程替换详解加五种进程间通讯方式(套接字放到tcp通信编程上讲述)
linux
开压路机6 小时前
Linux的基本指令
linux·服务器