kobject 与sysfs属性文件读写

news2025/8/2 21:56:01

kobject和kset的简单总结

kobject是struct kobject类型的对象。Kobject 有一个名字和一个引用计数。kobject 也有一个父指针(允许 kobjects 被安排到层次结构中),一个特定的类型,也许还有一个在 sysfs 虚拟文件系统中的表示。Kobject 本身通常并不有趣;相反,它们通常嵌入在一些其他结构中,其中包含代码真正感兴趣的内容。

• ktype是与 kobject 关联的类型ktype 控制当一个 kobject 不再被引用时会发生什么,以及 kobject 在 sysfs 中的默认表示。

kset是一组 kobjects,所有这些 kobjects 都嵌入在相同类型的结构中。kset 是 kobject 集合的基本容器类型。Ksets 包含它们自己的 kobjects,这是值得的。除其他外,这意味着一个 kobject 的父对象通常是包含它的 kset,尽管事情通常不必那样。当您看到充满条目的 sysfs 目录时,通常这些条目中的每一个都对应于同一 kset 中的一个 kobject。

• A subsystem是 kset的集合,它们共同构成内核的主要子部分。子系统通常对应于 sysfs 中的顶级目录。

struct  kobject {//目录抽象---基类
	const char		*name;
	struct list_head	entry;
	struct kobject		*parent;
	struct kset		*kset;//subsystem  or dir
	struct kobj_type	*ktype;
	struct kernfs_node	*sd;//sysfs
	struct kref		kref;//refcount
#ifdef CONFIG_DEBUG_KOBJECT_RELEASE
	struct delayed_work	release;
#endif
	unsigned int state_initialized:1;
	unsigned int state_in_sysfs:1;
	unsigned int state_add_uevent_sent:1;
	unsigned int state_remove_uevent_sent:1;
	unsigned int uevent_suppress:1;
};



kobj_type代表Kobject(严格地讲,是包含了Kobject的数据结构)的属性操作集合(由于通用性,多个Kobject可能共用同一个属性操作集,因此把Ktype独立出来了)
struct   kobj_type {
	void (*release)(struct kobject *kobj);//kobject release func when kobject refcont is 0
	const struct sysfs_ops  *sysfs_ops;//目录的操作ops----》attribute中的对应的ops,也就是目录---》文件
	struct attribute  **default_attrs;//设置的默认属性 会被框架自动创建
	const struct kobj_ns_type_operations  *(*child_ns_type)(struct kobject *kobj);
	const void   *(*namespace)(struct kobject *kobj);
};
This structure is used to describe a particular type of kobject (or, more correctly, of containing object). 
Every kobject needs to have an associated kobj_type structure; a pointer to that structure must be specified when you
call kobject_init() or kobject_init_and_add().

The release field in struct kobj_type is, of course, a pointer to the release() method for this type of kobject. The other two fields (sysfs_ops and default_attrs) control how objects of this type are represented in
sysfs; they are beyond the scope of this document.

The default_attrs pointer is a list of default attributes that will be automatically created for any kobject that is registered with this ktype

kset 与 kobj 都是目录,既然是目录,那么应该就是一个树状结构,每一个目录都将有一个父节点:

在kset中使用kset.kobj->parent 指定,在kboject中使用  kobj->parent 指定


kset 主要用途 是发送uevent和 创建同一类kobject。

/**
 * struct kset - a set of kobjects of a specific type, belonging to a specific subsystem.
 *
 * A kset defines a group of kobjects.  They can be individually  different "types" but overall these kobjects all want to be grouped
 * together and operated on in the same manner.  ksets are used to  define the attribute callbacks and other common events that happen to
 * a kobject.
 *
 * @list: the list of all kobjects for this kset
 * @list_lock: a lock for iterating over the kobjects
 * @kobj: the embedded kobject for this kset (recursion, isn't it fun...)
 * @uevent_ops: the set of uevent operations for this kset.  These are
 * called whenever a kobject has something happen to it so that the kset
 * can add new environment variables, or filter out the uevents if so
 * desired.
 */
struct   kset {// a group of kobjects
	struct list_head   list;//the list of all kobjects for this kset
	spinlock_t   list_lock;
	struct kobject kobj;//基类,kset也是一种kobj
	const struct kset_uevent_ops *uevent_ops;//some event to user space
};

 


kobject 注册函数

/**
 * kobject_init_and_add - initialize a kobject structure and add it to the kobject hierarchy
 * @kobj: pointer to the kobject to initialize
 * @ktype: pointer to the ktype for this kobject.
 * @parent: pointer to the parent of this kobject.
 * @fmt: the name of the kobject.
 *
 * This function combines the call to kobject_init() and
 * kobject_add().  The same type of error handling after a call to
 * kobject_add() and kobject lifetime rules are the same here.
 */
int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,
			 struct kobject *parent, const char *fmt, ...)//ktype need init and set
{
	va_list args;
	int retval;

	kobject_init(kobj, ktype);//init,fill kobject struct

	va_start(args, fmt);
	retval = kobject_add_varg(kobj, parent, fmt, args);
	va_end(args);

	return retval;
}

 kobject_add_varg--》kobject_add_internal

static int kobject_add_internal(struct kobject *kobj)
{
	int error = 0;
	struct kobject *parent;

	if (!kobj)
		return -ENOENT;

	if (!kobj->name || !kobj->name[0]) {
		WARN(1, "kobject: (%p): attempted to be registered with empty "
			 "name!\n", kobj);
		return -EINVAL;
	}

	parent = kobject_get(kobj->parent);

	/* join kset if set, use it as parent if we do not already have one */
	/*如果当前object还没设置父对象, 则引用设置的kset对象为父对象,        如果设置了,则将其加入到其设置的kset集合中(将其挂载到kset的链表上)
    然后设置父对象,即初始化kobj->parent*/
	if (kobj->kset) { 
		if (!parent)
			parent = kobject_get(&kobj->kset->kobj);
		kobj_kset_join(kobj);
		kobj->parent = parent;
	}

	pr_debug("kobject: '%s' (%p): %s: parent: '%s', set: '%s'\n",
		 kobject_name(kobj), kobj, __func__,
		 parent ? kobject_name(parent) : "<NULL>",
		 kobj->kset ? kobject_name(&kobj->kset->kobj) : "<NULL>");

	error = create_dir(kobj);//creat and fill
	if (error) {
		kobj_kset_leave(kobj);
		kobject_put(parent);
		kobj->parent = NULL;

		/* be noisy on error issues */
		if (error == -EEXIST)
			WARN(1, "%s failed for %s with "
			     "-EEXIST, don't try to register things with "
			     "the same name in the same directory.\n",
			     __func__, kobject_name(kobj));
		else
			WARN(1, "%s failed for %s (error: %d parent: %s)\n",
			     __func__, kobject_name(kobj), error,
			     parent ? kobject_name(parent) : "'none'");
	} else
		kobj->state_in_sysfs = 1;//normal is 1

	return error;
}

 creat_dir 部分注释

static int create_dir(struct kobject *kobj)//creat dir at sysfs
{
	const struct kobj_ns_type_operations *ops;
	int error;

	error = sysfs_create_dir_ns(kobj, kobject_namespace(kobj));//kobject-->sysfs
	if (error)
		return error;

	error = populate_dir(kobj);//有attr的话创建 attr file throught sysfs
	if (error) {
		sysfs_remove_dir(kobj);
		return error;
	}

	/*
	 * @kobj->sd may be deleted by an ancestor going away.  Hold an
	 * extra reference so that it stays until @kobj is gone.
	 */
	sysfs_get(kobj->sd);

	/*
	 * If @kobj has ns_ops, its children need to be filtered based on
	 * their namespace tags.  Enable namespace support on @kobj->sd.
	 */
	ops = kobj_child_ns_ops(kobj);
	if (ops) {
		BUG_ON(ops->type <= KOBJ_NS_TYPE_NONE);
		BUG_ON(ops->type >= KOBJ_NS_TYPES);
		BUG_ON(!kobj_ns_type_registered(ops->type));

		sysfs_enable_ns(kobj->sd);
	}

	return 0;
}

kset 注册过程

/**
 * kset_register - initialize and add a kset.
 * @k: kset.
 */
int kset_register(struct kset *k)//first init,then add
{
	int err;

	if (!k)
		return -EINVAL;

	kset_init(k);
	err = kobject_add_internal(&k->kobj);
	if (err)
		return err;
	kobject_uevent(&k->kobj, KOBJ_ADD);//通过uevent机制通知用户空间
	return 0;
}

kobjkset关系总结

kobj和kset并不是完全的父子关系。kset算是kobj的“接盘侠”,当kobj没有所属的parent时,才让kset来接盘当parent;如果连kset也没有,那该kobj属于顶层对象,其sysfs目录将位于/sys/下。正因为kobj和kset并不是完全的父子关系,因此在注册kobj时,将同时对parent及其所属的kset增加引用计数。若parent和kset为同一对象,则会对kset增加两次引用计数。

kset内部本身也包含一个kobj对象,在sysfs中也表现为目录;所不同的是,kset要承担kobj状态变动消息的发送任务。因此,首先kset会将所属的kobj组织在kset.list下,同时,通过uevent_ops在合适时候发送消息。

对于kobject_add()来说,它的输入信息是:kobj-parent、kobj-name,kobject_add()优先使用传入的parent作为kobj->parent;其次,使用kset作为kobj->parent

kobj状态变动后,必须依靠所关联的kset来向用户空间发送消息;若无关联kset(kobj向上组成的树中,任何成员都无所属的kset),则kobj无法发送用户消息。 


sysfs 文件打开,读,写过程代码总结

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/35004.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

OceanBase 4.0 解读:分布式查询性能提升,我们是如何思考的?

关于作者 王国平 OceanBase高级技术专家 目前OceanBase SQL引擎的技术负责人。2016年加入 OceanBase&#xff0c;负责SQL引擎的研发工作。2008年毕业于哈尔滨工业大学&#xff0c;2014年在新加坡国立大学获得博士学位&#xff0c;博士期间主要研究方向是数据库领域的(多)查询…

C++入门(2)-类与对象

类与对象初步认识类与对象一、面向过程与面向对象的区别二、类与结构体三、类的定义四、类的实例化五、类对象六、this指针七、构造函数八、析构函数九、拷贝构造函数十、运算符重载函数初步认识类与对象 一、面向过程与面向对象的区别 C语言是面向过程进行编程&#xff0c;注…

springboot整合其他项目

目录 一&#xff0c;集成Druid 学习地址&#xff1a; 主要讲监控 基于springboot视图渲染技术上增加代码 1.1 增加pom依赖 1.2 先在application.yml中添加Druid的数据源 1.3 其次在全局配置文件application.yml中添加所需的配置 配置截图&#xff1a; 配置解析 1.4 启动…

电脑录屏快捷键是什么?win10自带屏幕录制在哪

​在使用电脑的过程中&#xff0c;我们难免会遇到使用电脑录屏功能。有时候可能是想录制网课&#xff0c;有时候可能是想录制游戏的精彩操作&#xff0c;有时候可能只是想录制会议内容。 电脑录屏能够将重要的画面内容进行录制&#xff0c;十分的方便。但也有很多的小伙伴不清…

Python基础(三):PyCharm安装和使用

文章目录 PyCharm安装和使用 一、PyCharm的作用 二、PyCharm系统要求 三、下载和安装 四、PyCharm基本使用 五、PyCharm的基本设置 1、修改主题 2、修改代码文字格式 3、修改解释器 4、项目管理 PyCharm安装和使用 14天学习训练营导师课程&#xff1a;杨鑫《Python…

一、VSCode——免安装

介绍 Visual Studio Code支持可移植模式。此模式使 VS Code 创建和维护的所有数据都位于自身附近&#xff0c;因此可以跨环境移动。 此模式还提供了设置 VS Code 扩展的安装文件夹位置的方法&#xff0c;这对于阻止在 Windows AppData 文件夹中安装扩展的企业环境非常有用。 …

极市打榜|70G+已标注数据集出炉,油品泄露识别等全新算法上线!

极市打榜 算法打榜是极市平台推出的一种算法项目合作模式&#xff0c;至今已上线 100 产业端落地算法项目&#xff0c;已对接智慧城市、智慧工地、明厨亮灶等多个行业真实需求&#xff0c;算法方向涵盖目标检测、行为识别、图像分割、视频理解、目标跟踪、OCR等。 开发者报名…

CAN 协议控制器和物理总线之间的接口芯片SIT1040T 高速 CAN 总线收发器

CAN 协议控制器和物理总线之间的接口芯片SIT1040T 高速 CAN 总线收发器 CAN是最新进的现场总线,灵活性好,通讯可靠性高,抗干扰能力强&#xff0c;超长通信距离等优点,110个节点,兼带CAN-FD功能产品,容错电压可达-70V~ 70V,温度范围高达-40C ~ 150C最初应用于汽车电子,目前已广…

安信可Ai-WB1系列AT指令连接MQTT阿里云物联网平台

文章目录前言1 准备材料2 创建云端设备3 硬件连接4 配置终端设备5 MQTT实现发布&订阅消息联系我们前言 本文将介绍安信可AI-WB1系列通过AT指令接入阿里云物联网平台&#xff0c;实现MQTT的订阅和发布。 1 准备材料 AI-WB1系列模组或者开发板USB转TTL模块/Type-C数据线阿…

数仓建设教程

50000字&#xff0c;数仓建设保姆级教程&#xff0c;离线和实时一网打尽(理论实战) 上 - 腾讯云开发者社区-腾讯云 (tencent.com)50000字&#xff0c;数仓建设保姆级教程&#xff0c;离线和实时一网打尽(理论实战) 下_五分钟学大数据的技术博客_51CTO博客#yyds干货盘点#最强最全…

什么是DDoS攻击?企业服务器是否需要DDoS防御

有时候你可能会遇到某个网站突然打不开&#xff0c;这一段时间后发布自己被DDos攻击的公告&#xff0c; 那么&#xff0c;为什么DDOS攻击能让服务器瘫痪&#xff1f;黑客又如何执行DDos攻击的呢&#xff1f; DDoS全称为Distributed Denial of Service&#xff08;分布式拒绝服…

Anemoi hash:一种SNARK-friendly的哈希函数

随着zk的兴起&#xff0c;出现了一大批zk友好且面向算术化(Arithmetization-Oriented)的哈希函数&#xff0c;如MiMC-Hash, Rescue–Prime, Poseidon等等&#xff0c;本文要介绍的Anemoi是今年新出的一种zk友好且面向算术化的哈希函数&#xff0c;与其他哈希函数相比&#xff0…

让我们进入面向对象的世界(三)

文章目录前言一.了解什么是继承二.我们针对继承来设计一个动物继承树前言2.1 第一步 找出共同属性和行为的对象2.2 设计代表共同状态行为的类2.3 决定子类是否让某项行为有不同的运作方式。2.4我们仔细去观察一下子类的特征&#xff0c;争取更多的抽象化的机会。三.继承的相关语…

leetcode:887. 鸡蛋掉落【经典dp定义】

目录题目截图题目分析ac code总结题目截图 题目分析 变量&#xff1a;鸡蛋的数量&#xff0c;楼层n&#xff0c;尝试的次数m有一个单调性容易发现&#xff1a;尝试的次数越多&#xff0c;能解决楼层越高的确切值另一个单调性&#xff1a;鸡蛋的数量越多&#xff0c;能够解决楼…

Flutter 实现局部刷新 StreamBuilder 实例详解

一、前言 在flutter项目中&#xff0c;页面内直接调用setState方法会使得页面重新执行build方法&#xff0c;导致内部组件被全量刷新&#xff0c;造成不必要的性能消耗。出于性能和用户体验方面的考虑我们经常会使用局部刷新代替全量刷新进行页面更新的操作。包括Provider、Va…

制造业企业如何高效进行生产计划排单?

随着社会的发展&#xff0c;个性化订单需求越来越多。面对如今更加多样化、紧迫化、随机化的订单&#xff0c;企业必须采用科学合理的方式和手段对生产计划进行控制&#xff0c;以提高企业的经济效益。生产计划在实施的过程中&#xff0c;由于一些原因&#xff0c;往往造成实施…

Node.js 模块化及npm概念介绍

文章目录模块化1 模块化的基本概念1.1 什么是模块化1.2 模块化规范2 Nodejs中的模块化2.1 Nodejs中模块的分类2.2 加载模块2.3 模块的作用域2.4 向外共享模块作用域中的成员2.4.1 module对象2.4.2 module.export对象2.4.3 共享成员时的注意点2.4.4 exports对象2.4.5 exportshe …

栈和队列及其多种接口实现-c语言

今天我们来完成栈和队列&#xff0c;首先我们要明白什么是栈&#xff0c;什么是队列。 目录 栈的选择 栈的结构 栈的初始化 栈的销毁 入栈 出栈 返回栈顶元素 计算数据个数 判断是否为空 队列的选择 队列的结构 入队列 出队列 判断是否为空 取队头元素 取队尾…

Java Spring Bean的生命周期 三级缓存

Java Spring Bean的生命周期 三级缓存 SpringBean的生命周期&#xff1a;是从 Bean 实例化之后&#xff08;即通过反射创建出对象之后&#xff09;&#xff0c;到Bean成为一个完整对象&#xff0c;最终存储到单例池中&#xff0c;这个过程被称为Spring Bean的生命周期。Spring…

盘一盘那些年我们使用的Java

一、序 那些年我们使用过的Java版本。我是一个80后&#xff0c;当年在大学时代使用的是Java5&#xff0c;当时是大三的时候学校有了编程课&#xff0c;最开始学的是汇编语言、VB、C然后再是Java。当时就是Java5&#xff0c;搞了个课程设计与顺便也参加了个校园程序设计大赛。当…