xref: /linux-6.15/include/linux/kobject.h (revision 2444a80c)
1d9d16e16SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * kobject.h - generic kernel object infrastructure.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Copyright (c) 2002-2003 Patrick Mochel
61da177e4SLinus Torvalds  * Copyright (c) 2002-2003 Open Source Development Labs
779a6ee42SGreg Kroah-Hartman  * Copyright (c) 2006-2008 Greg Kroah-Hartman <[email protected]>
879a6ee42SGreg Kroah-Hartman  * Copyright (c) 2006-2008 Novell Inc.
91da177e4SLinus Torvalds  *
100c1bc6b8SMauro Carvalho Chehab  * Please read Documentation/core-api/kobject.rst before using the kobject
111da177e4SLinus Torvalds  * interface, ESPECIALLY the parts about reference counts and object
121da177e4SLinus Torvalds  * destructors.
131da177e4SLinus Torvalds  */
141da177e4SLinus Torvalds 
151da177e4SLinus Torvalds #ifndef _KOBJECT_H_
161da177e4SLinus Torvalds #define _KOBJECT_H_
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds #include <linux/types.h>
191da177e4SLinus Torvalds #include <linux/list.h>
201da177e4SLinus Torvalds #include <linux/sysfs.h>
214a7fb636SAndrew Morton #include <linux/compiler.h>
22a6914afcSAndy Shevchenko #include <linux/container_of.h>
231da177e4SLinus Torvalds #include <linux/spinlock.h>
241da177e4SLinus Torvalds #include <linux/kref.h>
258488a38fSDavid Howells #include <linux/kobject_ns.h>
264508a7a7SNeilBrown #include <linux/wait.h>
2760063497SArun Sharma #include <linux/atomic.h>
28c817a67eSRussell King #include <linux/workqueue.h>
295f81880dSDmitry Torokhov #include <linux/uidgid.h>
301da177e4SLinus Torvalds 
31312c004dSKay Sievers #define UEVENT_HELPER_PATH_LEN		256
32655078f5SSebastian Reichel #define UEVENT_NUM_ENVP			64	/* number of env pointers */
337eff2e7aSKay Sievers #define UEVENT_BUFFER_SIZE		2048	/* buffer for the variables */
340296b228SKay Sievers 
3586d56134SMichael Marineau #ifdef CONFIG_UEVENT_HELPER
360296b228SKay Sievers /* path to the userspace helper executed on an event */
37312c004dSKay Sievers extern char uevent_helper[];
3886d56134SMichael Marineau #endif
390296b228SKay Sievers 
40312c004dSKay Sievers /* counter to tag the uevent, read only except for the kobject core */
41*2444a80cSEric Dumazet extern atomic64_t uevent_seqnum;
421da177e4SLinus Torvalds 
4360a96a59SKay Sievers /*
4460a96a59SKay Sievers  * The actions here must match the index to the string array
4560a96a59SKay Sievers  * in lib/kobject_uevent.c
4660a96a59SKay Sievers  *
4760a96a59SKay Sievers  * Do not add new actions here without checking with the driver-core
4860a96a59SKay Sievers  * maintainers. Action strings are not meant to express subsystem
4960a96a59SKay Sievers  * or device specific properties. In most cases you want to send a
5060a96a59SKay Sievers  * kobject_uevent_env(kobj, KOBJ_CHANGE, env) with additional event
5160a96a59SKay Sievers  * specific variables added to the event environment.
5260a96a59SKay Sievers  */
530296b228SKay Sievers enum kobject_action {
5460a96a59SKay Sievers 	KOBJ_ADD,
5560a96a59SKay Sievers 	KOBJ_REMOVE,
5660a96a59SKay Sievers 	KOBJ_CHANGE,
5760a96a59SKay Sievers 	KOBJ_MOVE,
5860a96a59SKay Sievers 	KOBJ_ONLINE,
5960a96a59SKay Sievers 	KOBJ_OFFLINE,
601455cf8dSDmitry Torokhov 	KOBJ_BIND,
611455cf8dSDmitry Torokhov 	KOBJ_UNBIND,
620296b228SKay Sievers };
630296b228SKay Sievers 
641da177e4SLinus Torvalds struct kobject {
65af5ca3f4SKay Sievers 	const char		*name;
661da177e4SLinus Torvalds 	struct list_head	entry;
671da177e4SLinus Torvalds 	struct kobject		*parent;
681da177e4SLinus Torvalds 	struct kset		*kset;
69ee6d3dd4SWedson Almeida Filho 	const struct kobj_type	*ktype;
70f7025709SUlf Magnusson 	struct kernfs_node	*sd; /* sysfs directory entry */
71a231934bSRichard Kennedy 	struct kref		kref;
72f5992717SChristophe JAILLET 
730f4dafc0SKay Sievers 	unsigned int state_initialized:1;
740f4dafc0SKay Sievers 	unsigned int state_in_sysfs:1;
750f4dafc0SKay Sievers 	unsigned int state_add_uevent_sent:1;
760f4dafc0SKay Sievers 	unsigned int state_remove_uevent_sent:1;
77f67f129eSMing Lei 	unsigned int uevent_suppress:1;
78f5992717SChristophe JAILLET 
79f5992717SChristophe JAILLET #ifdef CONFIG_DEBUG_KOBJECT_RELEASE
80f5992717SChristophe JAILLET 	struct delayed_work	release;
81f5992717SChristophe JAILLET #endif
821da177e4SLinus Torvalds };
831da177e4SLinus Torvalds 
8444650f33SGreg Kroah-Hartman __printf(2, 3) int kobject_set_name(struct kobject *kobj, const char *name, ...);
8544650f33SGreg Kroah-Hartman __printf(2, 0) int kobject_set_name_vargs(struct kobject *kobj, const char *fmt, va_list vargs);
861da177e4SLinus Torvalds 
kobject_name(const struct kobject * kobj)87f3b4f3c6SDmitry Torokhov static inline const char *kobject_name(const struct kobject *kobj)
881da177e4SLinus Torvalds {
89af5ca3f4SKay Sievers 	return kobj->name;
901da177e4SLinus Torvalds }
911da177e4SLinus Torvalds 
9244650f33SGreg Kroah-Hartman void kobject_init(struct kobject *kobj, const struct kobj_type *ktype);
9344650f33SGreg Kroah-Hartman __printf(3, 4) __must_check int kobject_add(struct kobject *kobj,
9444650f33SGreg Kroah-Hartman 					    struct kobject *parent,
95b9075fa9SJoe Perches 					    const char *fmt, ...);
9644650f33SGreg Kroah-Hartman __printf(4, 5) __must_check int kobject_init_and_add(struct kobject *kobj,
9744650f33SGreg Kroah-Hartman 						     const struct kobj_type *ktype,
9844650f33SGreg Kroah-Hartman 						     struct kobject *parent,
99b9075fa9SJoe Perches 						     const char *fmt, ...);
100c11c4154SGreg Kroah-Hartman 
10144650f33SGreg Kroah-Hartman void kobject_del(struct kobject *kobj);
1021da177e4SLinus Torvalds 
10344650f33SGreg Kroah-Hartman struct kobject * __must_check kobject_create_and_add(const char *name, struct kobject *parent);
1043f9e3ee9SGreg Kroah-Hartman 
10544650f33SGreg Kroah-Hartman int __must_check kobject_rename(struct kobject *, const char *new_name);
10644650f33SGreg Kroah-Hartman int __must_check kobject_move(struct kobject *, struct kobject *);
1071da177e4SLinus Torvalds 
10844650f33SGreg Kroah-Hartman struct kobject *kobject_get(struct kobject *kobj);
10944650f33SGreg Kroah-Hartman struct kobject * __must_check kobject_get_unless_zero(struct kobject *kobj);
11044650f33SGreg Kroah-Hartman void kobject_put(struct kobject *kobj);
1111da177e4SLinus Torvalds 
11244650f33SGreg Kroah-Hartman const void *kobject_namespace(const struct kobject *kobj);
11344650f33SGreg Kroah-Hartman void kobject_get_ownership(const struct kobject *kobj, kuid_t *uid, kgid_t *gid);
11444650f33SGreg Kroah-Hartman char *kobject_get_path(const struct kobject *kobj, gfp_t flag);
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds struct kobj_type {
11779a6ee42SGreg Kroah-Hartman 	void (*release)(struct kobject *kobj);
11852cf25d0SEmese Revfy 	const struct sysfs_ops *sysfs_ops;
119aa30f47cSKimberly Brown 	const struct attribute_group **default_groups;
12002a476d9SGreg Kroah-Hartman 	const struct kobj_ns_type_operations *(*child_ns_type)(const struct kobject *kobj);
12102a476d9SGreg Kroah-Hartman 	const void *(*namespace)(const struct kobject *kobj);
12202a476d9SGreg Kroah-Hartman 	void (*get_ownership)(const struct kobject *kobj, kuid_t *uid, kgid_t *gid);
1231da177e4SLinus Torvalds };
1241da177e4SLinus Torvalds 
1257eff2e7aSKay Sievers struct kobj_uevent_env {
126bcccff93SVladimir Davydov 	char *argv[3];
1277eff2e7aSKay Sievers 	char *envp[UEVENT_NUM_ENVP];
1287eff2e7aSKay Sievers 	int envp_idx;
1297eff2e7aSKay Sievers 	char buf[UEVENT_BUFFER_SIZE];
1307eff2e7aSKay Sievers 	int buflen;
1317eff2e7aSKay Sievers };
1327eff2e7aSKay Sievers 
133a5615648SRandy Dunlap struct kset_uevent_ops {
134c45a88bbSGreg Kroah-Hartman 	int (* const filter)(const struct kobject *kobj);
135a53d1accSGreg Kroah-Hartman 	const char *(* const name)(const struct kobject *kobj);
13656d5f362SGreg Kroah-Hartman 	int (* const uevent)(const struct kobject *kobj, struct kobj_uevent_env *env);
137a5615648SRandy Dunlap };
1381da177e4SLinus Torvalds 
13923b5212cSKay Sievers struct kobj_attribute {
14023b5212cSKay Sievers 	struct attribute attr;
14123b5212cSKay Sievers 	ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr,
14223b5212cSKay Sievers 			char *buf);
14323b5212cSKay Sievers 	ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr,
14423b5212cSKay Sievers 			 const char *buf, size_t count);
14523b5212cSKay Sievers };
14623b5212cSKay Sievers 
14752cf25d0SEmese Revfy extern const struct sysfs_ops kobj_sysfs_ops;
14823b5212cSKay Sievers 
149bc451f20SEric W. Biederman struct sock;
150be867b19SSerge E. Hallyn 
1516adf7554SGreg Kroah-Hartman /**
1526adf7554SGreg Kroah-Hartman  * struct kset - a set of kobjects of a specific type, belonging to a specific subsystem.
1531da177e4SLinus Torvalds  *
1546adf7554SGreg Kroah-Hartman  * A kset defines a group of kobjects.  They can be individually
1556adf7554SGreg Kroah-Hartman  * different "types" but overall these kobjects all want to be grouped
1566adf7554SGreg Kroah-Hartman  * together and operated on in the same manner.  ksets are used to
1576adf7554SGreg Kroah-Hartman  * define the attribute callbacks and other common events that happen to
1586adf7554SGreg Kroah-Hartman  * a kobject.
1591da177e4SLinus Torvalds  *
1606adf7554SGreg Kroah-Hartman  * @list: the list of all kobjects for this kset
1616adf7554SGreg Kroah-Hartman  * @list_lock: a lock for iterating over the kobjects
1626adf7554SGreg Kroah-Hartman  * @kobj: the embedded kobject for this kset (recursion, isn't it fun...)
1636adf7554SGreg Kroah-Hartman  * @uevent_ops: the set of uevent operations for this kset.  These are
1646adf7554SGreg Kroah-Hartman  * called whenever a kobject has something happen to it so that the kset
1656adf7554SGreg Kroah-Hartman  * can add new environment variables, or filter out the uevents if so
1666adf7554SGreg Kroah-Hartman  * desired.
1671da177e4SLinus Torvalds  */
1681da177e4SLinus Torvalds struct kset {
1691da177e4SLinus Torvalds 	struct list_head list;
1701da177e4SLinus Torvalds 	spinlock_t list_lock;
1711da177e4SLinus Torvalds 	struct kobject kobj;
1729cd43611SEmese Revfy 	const struct kset_uevent_ops *uevent_ops;
1733859a271SKees Cook } __randomize_layout;
1741da177e4SLinus Torvalds 
17544650f33SGreg Kroah-Hartman void kset_init(struct kset *kset);
17644650f33SGreg Kroah-Hartman int __must_check kset_register(struct kset *kset);
17744650f33SGreg Kroah-Hartman void kset_unregister(struct kset *kset);
17844650f33SGreg Kroah-Hartman struct kset * __must_check kset_create_and_add(const char *name, const struct kset_uevent_ops *u,
179b727c702SGreg Kroah-Hartman 					       struct kobject *parent_kobj);
1801da177e4SLinus Torvalds 
to_kset(struct kobject * kobj)1811da177e4SLinus Torvalds static inline struct kset *to_kset(struct kobject *kobj)
1821da177e4SLinus Torvalds {
1831da177e4SLinus Torvalds 	return kobj ? container_of(kobj, struct kset, kobj) : NULL;
1841da177e4SLinus Torvalds }
1851da177e4SLinus Torvalds 
kset_get(struct kset * k)1861da177e4SLinus Torvalds static inline struct kset *kset_get(struct kset *k)
1871da177e4SLinus Torvalds {
1881da177e4SLinus Torvalds 	return k ? to_kset(kobject_get(&k->kobj)) : NULL;
1891da177e4SLinus Torvalds }
1901da177e4SLinus Torvalds 
kset_put(struct kset * k)1911da177e4SLinus Torvalds static inline void kset_put(struct kset *k)
1921da177e4SLinus Torvalds {
1931da177e4SLinus Torvalds 	kobject_put(&k->kobj);
1941da177e4SLinus Torvalds }
1951da177e4SLinus Torvalds 
get_ktype(const struct kobject * kobj)1963d24903aSGreg Kroah-Hartman static inline const struct kobj_type *get_ktype(const struct kobject *kobj)
1971da177e4SLinus Torvalds {
1983514facaSGreg Kroah-Hartman 	return kobj->ktype;
1991da177e4SLinus Torvalds }
2001da177e4SLinus Torvalds 
20144650f33SGreg Kroah-Hartman struct kobject *kset_find_obj(struct kset *, const char *);
2021da177e4SLinus Torvalds 
2030ff21e46SGreg Kroah-Hartman /* The global /sys/kernel/ kobject for people to chain off of */
2040ff21e46SGreg Kroah-Hartman extern struct kobject *kernel_kobj;
205ff7ea79cSNishanth Aravamudan /* The global /sys/kernel/mm/ kobject for people to chain off of */
206ff7ea79cSNishanth Aravamudan extern struct kobject *mm_kobj;
2072d72fc00SGreg Kroah-Hartman /* The global /sys/hypervisor/ kobject for people to chain off of */
2082d72fc00SGreg Kroah-Hartman extern struct kobject *hypervisor_kobj;
209d76e15fbSGreg Kroah-Hartman /* The global /sys/power/ kobject for people to chain off of */
210d76e15fbSGreg Kroah-Hartman extern struct kobject *power_kobj;
211f62ed9e3SGreg Kroah-Hartman /* The global /sys/firmware/ kobject for people to chain off of */
212f62ed9e3SGreg Kroah-Hartman extern struct kobject *firmware_kobj;
2131da177e4SLinus Torvalds 
214542cfce6SAneesh Kumar K.V int kobject_uevent(struct kobject *kobj, enum kobject_action action);
215542cfce6SAneesh Kumar K.V int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
2168a82472fSCornelia Huck 			char *envp[]);
217f36776faSPeter Rajnoha int kobject_synth_uevent(struct kobject *kobj, const char *buf, size_t count);
2180296b228SKay Sievers 
219b9075fa9SJoe Perches __printf(2, 3)
220b9075fa9SJoe Perches int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...);
2215c5daf65SKay Sievers 
2221da177e4SLinus Torvalds #endif /* _KOBJECT_H_ */
223