blob: 72fa20f405f1520a63dd50d9aa37f6609306eb3e [file] [log] [blame]
Greg Kroah-Hartmand9d16e12017-11-07 17:30:05 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * kobject.c - library routines for handling generic kernel objects
4 *
5 * Copyright (c) 2002-2003 Patrick Mochel <mochel@osdl.org>
Greg Kroah-Hartmanf0e7e1b2007-09-27 14:48:53 -07006 * Copyright (c) 2006-2007 Greg Kroah-Hartman <greg@kroah.com>
7 * Copyright (c) 2006-2007 Novell Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
Mauro Carvalho Chehab0c1bc6b2020-04-14 18:48:37 +02009 * Please see the file Documentation/core-api/kobject.rst for critical information
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * about using the kobject interface.
11 */
12
Thomas Weißschuh98406332023-03-11 03:14:46 +000013#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/kobject.h>
16#include <linux/string.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050017#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/stat.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080019#include <linux/slab.h>
Bjorn Helgaas89c86a62013-12-05 17:37:51 -070020#include <linux/random.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Tejun Heoe34ff492013-09-11 22:29:05 -040022/**
Tobin C. Hardinged8563492019-05-02 12:31:40 +100023 * kobject_namespace() - Return @kobj's namespace tag.
Tejun Heoe34ff492013-09-11 22:29:05 -040024 * @kobj: kobject in question
25 *
26 * Returns namespace tag of @kobj if its parent has namespace ops enabled
27 * and thus @kobj should have a namespace tag associated with it. Returns
28 * %NULL otherwise.
29 */
Greg Kroah-Hartman542aa242022-11-21 10:46:46 +010030const void *kobject_namespace(const struct kobject *kobj)
Tejun Heoe34ff492013-09-11 22:29:05 -040031{
32 const struct kobj_ns_type_operations *ns_ops = kobj_ns_ops(kobj);
33
34 if (!ns_ops || ns_ops->type == KOBJ_NS_TYPE_NONE)
35 return NULL;
36
Linus Torvaldsa1212d22013-11-07 20:47:28 +090037 return kobj->ktype->namespace(kobj);
Tejun Heoe34ff492013-09-11 22:29:05 -040038}
39
Dmitry Torokhov5f818802018-07-20 21:56:48 +000040/**
Tobin C. Hardinged8563492019-05-02 12:31:40 +100041 * kobject_get_ownership() - Get sysfs ownership data for @kobj.
Dmitry Torokhov5f818802018-07-20 21:56:48 +000042 * @kobj: kobject in question
43 * @uid: kernel user ID for sysfs objects
44 * @gid: kernel group ID for sysfs objects
45 *
46 * Returns initial uid/gid pair that should be used when creating sysfs
47 * representation of given kobject. Normally used to adjust ownership of
48 * objects in a container.
49 */
Greg Kroah-Hartman02a476d92022-11-21 10:46:45 +010050void kobject_get_ownership(const struct kobject *kobj, kuid_t *uid, kgid_t *gid)
Dmitry Torokhov5f818802018-07-20 21:56:48 +000051{
52 *uid = GLOBAL_ROOT_UID;
53 *gid = GLOBAL_ROOT_GID;
54
55 if (kobj->ktype->get_ownership)
56 kobj->ktype->get_ownership(kobj, uid, gid);
57}
58
Zhen Leie2dfa1d2023-07-26 14:25:08 +080059static bool kobj_ns_type_is_valid(enum kobj_ns_type type)
60{
61 if ((type <= KOBJ_NS_TYPE_NONE) || (type >= KOBJ_NS_TYPES))
62 return false;
63
64 return true;
65}
66
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -080067static int create_dir(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
Kimberly Brownaa30f472019-04-01 22:51:18 -040069 const struct kobj_type *ktype = get_ktype(kobj);
Tejun Heoc84a3b22013-11-23 18:01:46 -050070 const struct kobj_ns_type_operations *ops;
Tejun Heoe34ff492013-09-11 22:29:05 -040071 int error;
72
73 error = sysfs_create_dir_ns(kobj, kobject_namespace(kobj));
Tejun Heoc84a3b22013-11-23 18:01:46 -050074 if (error)
75 return error;
76
Greg Kroah-Hartman3ca8fba2024-02-08 16:02:50 +000077 if (ktype) {
78 error = sysfs_create_groups(kobj, ktype->default_groups);
79 if (error) {
80 sysfs_remove_dir(kobj);
81 return error;
82 }
Kimberly Brownaa30f472019-04-01 22:51:18 -040083 }
84
Tejun Heo26ea12d2013-09-18 17:15:36 -040085 /*
86 * @kobj->sd may be deleted by an ancestor going away. Hold an
87 * extra reference so that it stays until @kobj is gone.
88 */
89 sysfs_get(kobj->sd);
90
Tejun Heoc84a3b22013-11-23 18:01:46 -050091 /*
92 * If @kobj has ns_ops, its children need to be filtered based on
93 * their namespace tags. Enable namespace support on @kobj->sd.
94 */
95 ops = kobj_child_ns_ops(kobj);
96 if (ops) {
Zhen Leie2dfa1d2023-07-26 14:25:08 +080097 BUG_ON(!kobj_ns_type_is_valid(ops->type));
Tejun Heoc84a3b22013-11-23 18:01:46 -050098 BUG_ON(!kobj_ns_type_registered(ops->type));
99
Tejun Heofa4cd452014-02-07 13:32:07 -0500100 sysfs_enable_ns(kobj->sd);
Tejun Heoc84a3b22013-11-23 18:01:46 -0500101 }
102
103 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
Greg Kroah-Hartman33a0a1e2022-10-01 18:53:15 +0200106static int get_kobj_path_length(const struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
108 int length = 1;
Greg Kroah-Hartman33a0a1e2022-10-01 18:53:15 +0200109 const struct kobject *parent = kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800111 /* walk up the ancestors until we hit the one pointing to the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 * root.
113 * Add 1 to strlen for leading '/' of each level.
114 */
115 do {
Chuck Ebbertb365b3d2006-01-12 20:02:00 -0500116 if (kobject_name(parent) == NULL)
117 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 length += strlen(kobject_name(parent)) + 1;
119 parent = parent->parent;
120 } while (parent);
121 return length;
122}
123
Wang Hai3bb2a012022-12-20 09:21:43 +0800124static int fill_kobj_path(const struct kobject *kobj, char *path, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Greg Kroah-Hartman33a0a1e2022-10-01 18:53:15 +0200126 const struct kobject *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 --length;
129 for (parent = kobj; parent; parent = parent->parent) {
130 int cur = strlen(kobject_name(parent));
131 /* back up enough to print this name with '/' */
132 length -= cur;
Wang Hai3bb2a012022-12-20 09:21:43 +0800133 if (length <= 0)
134 return -EINVAL;
Guenter Roeck77d2a242018-07-01 13:57:16 -0700135 memcpy(path + length, kobject_name(parent), cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 *(path + --length) = '/';
137 }
138
Thomas Weißschuh98406332023-03-11 03:14:46 +0000139 pr_debug("'%s' (%p): %s: path = '%s'\n", kobject_name(kobj),
Harvey Harrison810304d2008-04-30 00:55:08 -0700140 kobj, __func__, path);
Wang Hai3bb2a012022-12-20 09:21:43 +0800141
142 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
145/**
Tobin C. Harding8fd7c302019-05-02 12:31:39 +1000146 * kobject_get_path() - Allocate memory and fill in the path for @kobj.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 * @kobj: kobject in question, with which to build the path
148 * @gfp_mask: the allocation type used to allocate the path
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800149 *
Tobin C. Harding8fd7c302019-05-02 12:31:39 +1000150 * Return: The newly allocated memory, caller must free with kfree().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 */
Greg Kroah-Hartman33a0a1e2022-10-01 18:53:15 +0200152char *kobject_get_path(const struct kobject *kobj, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 char *path;
155 int len;
156
Wang Hai3bb2a012022-12-20 09:21:43 +0800157retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 len = get_kobj_path_length(kobj);
Chuck Ebbertb365b3d2006-01-12 20:02:00 -0500159 if (len == 0)
160 return NULL;
Burman Yan4668edc2006-12-06 20:38:51 -0800161 path = kzalloc(len, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (!path)
163 return NULL;
Wang Hai3bb2a012022-12-20 09:21:43 +0800164 if (fill_kobj_path(kobj, path, len)) {
165 kfree(path);
166 goto retry;
167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 return path;
170}
Dmitry Torokhov80fc9f52006-10-11 01:43:58 -0400171EXPORT_SYMBOL_GPL(kobject_get_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100173/* add the kobject to its kset's list */
174static void kobj_kset_join(struct kobject *kobj)
175{
176 if (!kobj->kset)
177 return;
178
179 kset_get(kobj->kset);
180 spin_lock(&kobj->kset->list_lock);
181 list_add_tail(&kobj->entry, &kobj->kset->list);
182 spin_unlock(&kobj->kset->list_lock);
183}
184
185/* remove the kobject from its kset's list */
186static void kobj_kset_leave(struct kobject *kobj)
187{
188 if (!kobj->kset)
189 return;
190
191 spin_lock(&kobj->kset->list_lock);
192 list_del_init(&kobj->entry);
193 spin_unlock(&kobj->kset->list_lock);
194 kset_put(kobj->kset);
195}
196
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800197static void kobject_init_internal(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Greg Kroah-Hartman31b9025a2007-01-18 12:23:51 -0800199 if (!kobj)
200 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 kref_init(&kobj->kref);
202 INIT_LIST_HEAD(&kobj->entry);
Greg Kroah-Hartmana4573c42008-02-26 09:36:38 -0800203 kobj->state_in_sysfs = 0;
204 kobj->state_add_uevent_sent = 0;
205 kobj->state_remove_uevent_sent = 0;
206 kobj->state_initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
209
Greg Kroah-Hartman9e7bbcc2007-12-17 23:05:35 -0700210static int kobject_add_internal(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212 int error = 0;
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800213 struct kobject *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100215 if (!kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return -ENOENT;
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100217
Kay Sieversaf5ca3f42007-12-20 02:09:39 +0100218 if (!kobj->name || !kobj->name[0]) {
Andy Shevchenko82d1f1172018-03-15 15:23:43 +0200219 WARN(1,
220 "kobject: (%p): attempted to be registered with empty name!\n",
221 kobj);
Greg Kroah-Hartmanc171fef2006-01-20 14:08:59 -0800222 return -EINVAL;
223 }
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 parent = kobject_get(kobj->parent);
226
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100227 /* join kset if set, use it as parent if we do not already have one */
228 if (kobj->kset) {
229 if (!parent)
230 parent = kobject_get(&kobj->kset->kobj);
231 kobj_kset_join(kobj);
232 kobj->parent = parent;
233 }
234
Thomas Weißschuh98406332023-03-11 03:14:46 +0000235 pr_debug("'%s' (%p): %s: parent: '%s', set: '%s'\n",
Harvey Harrison810304d2008-04-30 00:55:08 -0700236 kobject_name(kobj), kobj, __func__,
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800237 parent ? kobject_name(parent) : "<NULL>",
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800238 kobj->kset ? kobject_name(&kobj->kset->kobj) : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Eric W. Biederman90bc6132007-07-31 19:15:08 +0900240 error = create_dir(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 if (error) {
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100242 kobj_kset_leave(kobj);
243 kobject_put(parent);
244 kobj->parent = NULL;
Greg Kroah-Hartmandcd0da02006-03-20 13:17:13 -0800245
246 /* be noisy on error issues */
247 if (error == -EEXIST)
Dmitry Vyukov3e14c6a2018-04-11 17:22:43 +0200248 pr_err("%s failed for %s with -EEXIST, don't try to register things with the same name in the same directory.\n",
249 __func__, kobject_name(kobj));
Greg Kroah-Hartmandcd0da02006-03-20 13:17:13 -0800250 else
Dmitry Vyukov3e14c6a2018-04-11 17:22:43 +0200251 pr_err("%s failed for %s (error: %d parent: %s)\n",
252 __func__, kobject_name(kobj), error,
253 parent ? kobject_name(parent) : "'none'");
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100254 } else
255 kobj->state_in_sysfs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 return error;
258}
259
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700260/**
Tobin C. Hardinged8563492019-05-02 12:31:40 +1000261 * kobject_set_name_vargs() - Set the name of a kobject.
Greg Kroah-Hartman663a4742007-11-29 18:32:47 -0500262 * @kobj: struct kobject to set the name of
Greg Kroah-Hartman8c4606b2007-12-04 14:45:47 +0800263 * @fmt: format string used to build the name
Greg Kroah-Hartman663a4742007-11-29 18:32:47 -0500264 * @vargs: vargs to format the string.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 */
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100266int kobject_set_name_vargs(struct kobject *kobj, const char *fmt,
Greg Kroah-Hartman663a4742007-11-29 18:32:47 -0500267 va_list vargs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
Rasmus Villemoesf773f322015-11-06 16:31:23 -0800269 const char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Kay Sievers8a577ff2009-04-18 15:05:45 -0700271 if (kobj->name && !fmt)
272 return 0;
273
Rasmus Villemoesf773f322015-11-06 16:31:23 -0800274 s = kvasprintf_const(GFP_KERNEL, fmt, vargs);
Rasmus Villemoes2abf1142015-06-25 15:02:30 -0700275 if (!s)
Kay Sieversa4ca6612008-04-30 02:06:29 +0200276 return -ENOMEM;
Greg Kroah-Hartman663a4742007-11-29 18:32:47 -0500277
Rasmus Villemoesf773f322015-11-06 16:31:23 -0800278 /*
279 * ewww... some of these buggers have '/' in the name ... If
280 * that's the case, we need to make sure we have an actual
281 * allocated copy to modify, since kvasprintf_const may have
282 * returned something from .rodata.
283 */
284 if (strchr(s, '/')) {
285 char *t;
286
287 t = kstrdup(s, GFP_KERNEL);
288 kfree_const(s);
289 if (!t)
290 return -ENOMEM;
Andy Shevchenkob2f10142023-06-05 20:05:53 +0300291 s = strreplace(t, '/', '!');
Rasmus Villemoesf773f322015-11-06 16:31:23 -0800292 }
293 kfree_const(kobj->name);
Rasmus Villemoes2abf1142015-06-25 15:02:30 -0700294 kobj->name = s;
Kay Sievers9f2556512008-05-06 22:24:04 +0200295
Greg Kroah-Hartman663a4742007-11-29 18:32:47 -0500296 return 0;
297}
298
299/**
Tobin C. Hardinged8563492019-05-02 12:31:40 +1000300 * kobject_set_name() - Set the name of a kobject.
Greg Kroah-Hartman663a4742007-11-29 18:32:47 -0500301 * @kobj: struct kobject to set the name of
302 * @fmt: format string used to build the name
303 *
304 * This sets the name of the kobject. If you have already added the
305 * kobject to the system, you must call kobject_rename() in order to
306 * change the name of the kobject.
307 */
308int kobject_set_name(struct kobject *kobj, const char *fmt, ...)
309{
Kay Sieversa4ca6612008-04-30 02:06:29 +0200310 va_list vargs;
Greg Kroah-Hartman663a4742007-11-29 18:32:47 -0500311 int retval;
312
Kay Sieversa4ca6612008-04-30 02:06:29 +0200313 va_start(vargs, fmt);
314 retval = kobject_set_name_vargs(kobj, fmt, vargs);
315 va_end(vargs);
Greg Kroah-Hartman663a4742007-11-29 18:32:47 -0500316
317 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319EXPORT_SYMBOL(kobject_set_name);
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321/**
Tobin C. Hardinged8563492019-05-02 12:31:40 +1000322 * kobject_init() - Initialize a kobject structure.
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800323 * @kobj: pointer to the kobject to initialize
324 * @ktype: pointer to the ktype for this kobject.
325 *
326 * This function will properly initialize a kobject such that it can then
327 * be passed to the kobject_add() call.
328 *
329 * After this function is called, the kobject MUST be cleaned up by a call
330 * to kobject_put(), not by a call to kfree directly to ensure that all of
331 * the memory is cleaned up properly.
332 */
Wedson Almeida Filhoee6d3dd2021-12-24 23:13:45 +0000333void kobject_init(struct kobject *kobj, const struct kobj_type *ktype)
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800334{
335 char *err_str;
336
337 if (!kobj) {
338 err_str = "invalid kobject pointer!";
339 goto error;
340 }
341 if (!ktype) {
342 err_str = "must have a ktype to be initialized properly!\n";
343 goto error;
344 }
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100345 if (kobj->state_initialized) {
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800346 /* do not error out as sometimes we can recover */
Andy Shevchenko82d1f1172018-03-15 15:23:43 +0200347 pr_err("kobject (%p): tried to init an initialized object, something is seriously wrong.\n",
348 kobj);
Thomas Weißschuh64414da2023-03-11 03:14:47 +0000349 dump_stack_lvl(KERN_ERR);
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800350 }
351
Greg Kroah-Hartmana4573c42008-02-26 09:36:38 -0800352 kobject_init_internal(kobj);
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800353 kobj->ktype = ktype;
354 return;
355
356error:
Andy Shevchenko82d1f1172018-03-15 15:23:43 +0200357 pr_err("kobject (%p): %s\n", kobj, err_str);
Thomas Weißschuh64414da2023-03-11 03:14:47 +0000358 dump_stack_lvl(KERN_ERR);
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800359}
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700360EXPORT_SYMBOL(kobject_init);
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800361
Nicolas Iooss8db14862015-07-17 16:23:42 -0700362static __printf(3, 0) int kobject_add_varg(struct kobject *kobj,
363 struct kobject *parent,
364 const char *fmt, va_list vargs)
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800365{
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800366 int retval;
367
Kay Sieversa4ca6612008-04-30 02:06:29 +0200368 retval = kobject_set_name_vargs(kobj, fmt, vargs);
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800369 if (retval) {
Thomas Weißschuh98406332023-03-11 03:14:46 +0000370 pr_err("can not set name properly!\n");
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800371 return retval;
372 }
373 kobj->parent = parent;
Greg Kroah-Hartman9e7bbcc2007-12-17 23:05:35 -0700374 return kobject_add_internal(kobj);
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800375}
376
377/**
Tobin C. Hardinged8563492019-05-02 12:31:40 +1000378 * kobject_add() - The main kobject add function.
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800379 * @kobj: the kobject to add
380 * @parent: pointer to the parent of the kobject.
381 * @fmt: format to name the kobject with.
382 *
383 * The kobject name is set and added to the kobject hierarchy in this
384 * function.
385 *
386 * If @parent is set, then the parent of the @kobj will be set to it.
387 * If @parent is NULL, then the parent of the @kobj will be set to the
Bart Van Assche97057102014-01-04 14:20:04 +0100388 * kobject associated with the kset assigned to this kobject. If no kset
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800389 * is assigned to the kobject, then the kobject will be located in the
390 * root of the sysfs tree.
391 *
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100392 * Note, no "add" uevent will be created with this call, the caller should set
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800393 * up all of the necessary sysfs files for the object and then call
394 * kobject_uevent() with the UEVENT_ADD parameter to ensure that
395 * userspace is properly notified of this kobject's creation.
Tobin C. Harding92067f82019-04-28 10:48:10 +1000396 *
397 * Return: If this function returns an error, kobject_put() must be
398 * called to properly clean up the memory associated with the
399 * object. Under no instance should the kobject that is passed
400 * to this function be directly freed with a call to kfree(),
401 * that can leak memory.
402 *
Greg Kroah-Hartman70e16a62019-05-02 12:22:24 +0200403 * If this function returns success, kobject_put() must also be called
404 * in order to properly clean up the memory associated with the object.
405 *
406 * In short, once this function is called, kobject_put() MUST be called
407 * when the use of the object is finished in order to properly free
408 * everything.
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800409 */
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700410int kobject_add(struct kobject *kobj, struct kobject *parent,
411 const char *fmt, ...)
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800412{
413 va_list args;
414 int retval;
415
416 if (!kobj)
417 return -EINVAL;
418
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100419 if (!kobj->state_initialized) {
Andy Shevchenko82d1f1172018-03-15 15:23:43 +0200420 pr_err("kobject '%s' (%p): tried to add an uninitialized object, something is seriously wrong.\n",
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100421 kobject_name(kobj), kobj);
Thomas Weißschuh64414da2023-03-11 03:14:47 +0000422 dump_stack_lvl(KERN_ERR);
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100423 return -EINVAL;
424 }
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800425 va_start(args, fmt);
426 retval = kobject_add_varg(kobj, parent, fmt, args);
427 va_end(args);
428
429 return retval;
430}
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700431EXPORT_SYMBOL(kobject_add);
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800432
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800433/**
Tobin C. Hardinged8563492019-05-02 12:31:40 +1000434 * kobject_init_and_add() - Initialize a kobject structure and add it to
435 * the kobject hierarchy.
Greg Kroah-Hartmanc11c41542007-12-03 21:31:08 -0800436 * @kobj: pointer to the kobject to initialize
437 * @ktype: pointer to the ktype for this kobject.
438 * @parent: pointer to the parent of this kobject.
439 * @fmt: the name of the kobject.
440 *
Tobin C. Harding1fd7c3b2019-04-28 09:56:52 +1000441 * This function combines the call to kobject_init() and kobject_add().
442 *
443 * If this function returns an error, kobject_put() must be called to
444 * properly clean up the memory associated with the object. This is the
445 * same type of error handling after a call to kobject_add() and kobject
446 * lifetime rules are the same here.
Greg Kroah-Hartmanc11c41542007-12-03 21:31:08 -0800447 */
Wedson Almeida Filhoee6d3dd2021-12-24 23:13:45 +0000448int kobject_init_and_add(struct kobject *kobj, const struct kobj_type *ktype,
Greg Kroah-Hartmanc11c41542007-12-03 21:31:08 -0800449 struct kobject *parent, const char *fmt, ...)
450{
451 va_list args;
452 int retval;
453
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700454 kobject_init(kobj, ktype);
Greg Kroah-Hartmanc11c41542007-12-03 21:31:08 -0800455
456 va_start(args, fmt);
457 retval = kobject_add_varg(kobj, parent, fmt, args);
458 va_end(args);
459
460 return retval;
461}
462EXPORT_SYMBOL_GPL(kobject_init_and_add);
463
464/**
Tobin C. Hardinged8563492019-05-02 12:31:40 +1000465 * kobject_rename() - Change the name of an object.
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800466 * @kobj: object in question.
467 * @new_name: object's new name
Eric W. Biederman030c1d22008-05-08 14:41:00 -0700468 *
469 * It is the responsibility of the caller to provide mutual
470 * exclusion between two different calls of kobject_rename
471 * on the same kobject and to ensure that new_name is valid and
472 * won't conflict with other kobjects.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800474int kobject_rename(struct kobject *kobj, const char *new_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
476 int error = 0;
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800477 const char *devpath = NULL;
Eric W. Biederman0b4a4fe2008-07-03 18:05:28 -0700478 const char *dup_name = NULL, *name;
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800479 char *devpath_string = NULL;
480 char *envp[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 kobj = kobject_get(kobj);
483 if (!kobj)
484 return -EINVAL;
Lin Yi122f8ec2019-06-03 16:08:10 +0800485 if (!kobj->parent) {
486 kobject_put(kobj);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700487 return -EINVAL;
Lin Yi122f8ec2019-06-03 16:08:10 +0800488 }
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800489
490 devpath = kobject_get_path(kobj, GFP_KERNEL);
491 if (!devpath) {
492 error = -ENOMEM;
493 goto out;
494 }
495 devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
496 if (!devpath_string) {
497 error = -ENOMEM;
498 goto out;
499 }
500 sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
501 envp[0] = devpath_string;
502 envp[1] = NULL;
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800503
Rasmus Villemoesf773f322015-11-06 16:31:23 -0800504 name = dup_name = kstrdup_const(new_name, GFP_KERNEL);
Eric W. Biederman0b4a4fe2008-07-03 18:05:28 -0700505 if (!name) {
506 error = -ENOMEM;
507 goto out;
508 }
509
Tejun Heoe34ff492013-09-11 22:29:05 -0400510 error = sysfs_rename_dir_ns(kobj, new_name, kobject_namespace(kobj));
Eric W. Biederman0b4a4fe2008-07-03 18:05:28 -0700511 if (error)
512 goto out;
513
514 /* Install the new kobject name */
515 dup_name = kobj->name;
516 kobj->name = name;
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800517
518 /* This function is mostly/only used for network interface.
519 * Some hotplug package track interfaces by their name and
520 * therefore want to know when the name is changed by the user. */
Eric W. Biederman0b4a4fe2008-07-03 18:05:28 -0700521 kobject_uevent_env(kobj, KOBJ_MOVE, envp);
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800522
523out:
Rasmus Villemoesf773f322015-11-06 16:31:23 -0800524 kfree_const(dup_name);
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800525 kfree(devpath_string);
526 kfree(devpath);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700527 kobject_put(kobj);
528
529 return error;
530}
Alex Chiang8344b562008-06-10 15:30:42 -0600531EXPORT_SYMBOL_GPL(kobject_rename);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700532
533/**
Tobin C. Hardinged8563492019-05-02 12:31:40 +1000534 * kobject_move() - Move object to another parent.
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800535 * @kobj: object in question.
536 * @new_parent: object's new parent (can be NULL)
Cornelia Huck8a824722006-11-20 17:07:51 +0100537 */
Cornelia Huck8a824722006-11-20 17:07:51 +0100538int kobject_move(struct kobject *kobj, struct kobject *new_parent)
539{
540 int error;
541 struct kobject *old_parent;
542 const char *devpath = NULL;
543 char *devpath_string = NULL;
544 char *envp[2];
545
546 kobj = kobject_get(kobj);
547 if (!kobj)
548 return -EINVAL;
549 new_parent = kobject_get(new_parent);
550 if (!new_parent) {
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100551 if (kobj->kset)
552 new_parent = kobject_get(&kobj->kset->kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +0100553 }
Tejun Heoe34ff492013-09-11 22:29:05 -0400554
Cornelia Huck8a824722006-11-20 17:07:51 +0100555 /* old object path */
556 devpath = kobject_get_path(kobj, GFP_KERNEL);
557 if (!devpath) {
558 error = -ENOMEM;
559 goto out;
560 }
561 devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
562 if (!devpath_string) {
563 error = -ENOMEM;
564 goto out;
565 }
566 sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
567 envp[0] = devpath_string;
568 envp[1] = NULL;
Tejun Heoe34ff492013-09-11 22:29:05 -0400569 error = sysfs_move_dir_ns(kobj, new_parent, kobject_namespace(kobj));
Cornelia Huck8a824722006-11-20 17:07:51 +0100570 if (error)
571 goto out;
572 old_parent = kobj->parent;
573 kobj->parent = new_parent;
Dmitriy Monakhov9e993ef2007-03-03 16:11:21 +0300574 new_parent = NULL;
Cornelia Huck8a824722006-11-20 17:07:51 +0100575 kobject_put(old_parent);
576 kobject_uevent_env(kobj, KOBJ_MOVE, envp);
577out:
Dmitriy Monakhov9e993ef2007-03-03 16:11:21 +0300578 kobject_put(new_parent);
Cornelia Huck8a824722006-11-20 17:07:51 +0100579 kobject_put(kobj);
580 kfree(devpath_string);
581 kfree(devpath);
582 return error;
583}
Anand Jain24199d22015-02-12 07:03:37 +0800584EXPORT_SYMBOL_GPL(kobject_move);
Cornelia Huck8a824722006-11-20 17:07:51 +0100585
Heikki Krogerus079ad2f2020-06-04 19:46:46 +0200586static void __kobject_del(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Tejun Heo324a56e2013-12-11 14:11:53 -0500588 struct kernfs_node *sd;
Colin Ian King3d378dc2019-05-01 13:43:17 +0100589 const struct kobj_type *ktype;
Tejun Heo26ea12d2013-09-18 17:15:36 -0400590
Tejun Heo26ea12d2013-09-18 17:15:36 -0400591 sd = kobj->sd;
Colin Ian King3d378dc2019-05-01 13:43:17 +0100592 ktype = get_ktype(kobj);
Kimberly Brownaa30f472019-04-01 22:51:18 -0400593
Greg Kroah-Hartman3ca8fba2024-02-08 16:02:50 +0000594 if (ktype)
595 sysfs_remove_groups(kobj, ktype->default_groups);
Kimberly Brownaa30f472019-04-01 22:51:18 -0400596
Greg Kroah-Hartman0e5596c2020-05-24 17:30:41 +0200597 /* send "remove" if the caller did not do it but sent "add" */
598 if (kobj->state_add_uevent_sent && !kobj->state_remove_uevent_sent) {
Thomas Weißschuh98406332023-03-11 03:14:46 +0000599 pr_debug("'%s' (%p): auto cleanup 'remove' event\n",
Greg Kroah-Hartman0e5596c2020-05-24 17:30:41 +0200600 kobject_name(kobj), kobj);
601 kobject_uevent(kobj, KOBJ_REMOVE);
602 }
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 sysfs_remove_dir(kobj);
Tejun Heo26ea12d2013-09-18 17:15:36 -0400605 sysfs_put(sd);
606
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100607 kobj->state_in_sysfs = 0;
608 kobj_kset_leave(kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100609 kobj->parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
Heikki Krogerus079ad2f2020-06-04 19:46:46 +0200611
612/**
613 * kobject_del() - Unlink kobject from hierarchy.
614 * @kobj: object.
615 *
616 * This is the function that should be called to delete an object
617 * successfully added via kobject_add().
618 */
619void kobject_del(struct kobject *kobj)
620{
Andy Shevchenko40b8b822020-08-03 11:27:06 +0300621 struct kobject *parent;
Heikki Krogerus079ad2f2020-06-04 19:46:46 +0200622
Andy Shevchenko40b8b822020-08-03 11:27:06 +0300623 if (!kobj)
624 return;
625
626 parent = kobj->parent;
Heikki Krogerus079ad2f2020-06-04 19:46:46 +0200627 __kobject_del(kobj);
628 kobject_put(parent);
629}
Gabriel Somlofa40ae32015-08-10 15:51:43 -0400630EXPORT_SYMBOL(kobject_del);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632/**
Tobin C. Hardinged8563492019-05-02 12:31:40 +1000633 * kobject_get() - Increment refcount for object.
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800634 * @kobj: object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800636struct kobject *kobject_get(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Ethan Zhaod82d54a2015-03-12 13:04:16 +0900638 if (kobj) {
639 if (!kobj->state_initialized)
Andy Shevchenko82d1f1172018-03-15 15:23:43 +0200640 WARN(1, KERN_WARNING
641 "kobject: '%s' (%p): is not initialized, yet kobject_get() is being called.\n",
642 kobject_name(kobj), kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 kref_get(&kobj->kref);
Ethan Zhaod82d54a2015-03-12 13:04:16 +0900644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return kobj;
646}
Gabriel Somlofa40ae32015-08-10 15:51:43 -0400647EXPORT_SYMBOL(kobject_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Jan Karac70c1762017-03-23 01:37:01 +0100649struct kobject * __must_check kobject_get_unless_zero(struct kobject *kobj)
Linus Torvaldsa49b7e82013-04-13 15:15:30 -0700650{
Jan Karac70c1762017-03-23 01:37:01 +0100651 if (!kobj)
652 return NULL;
Linus Torvaldsa49b7e82013-04-13 15:15:30 -0700653 if (!kref_get_unless_zero(&kobj->kref))
654 kobj = NULL;
655 return kobj;
656}
Jan Karac70c1762017-03-23 01:37:01 +0100657EXPORT_SYMBOL(kobject_get_unless_zero);
Linus Torvaldsa49b7e82013-04-13 15:15:30 -0700658
Greg Kroah-Hartman18041f472007-12-03 21:31:08 -0800659/*
660 * kobject_cleanup - free kobject resources.
661 * @kobj: object to cleanup
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 */
Greg Kroah-Hartman18041f472007-12-03 21:31:08 -0800663static void kobject_cleanup(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
Heikki Krogerus079ad2f2020-06-04 19:46:46 +0200665 struct kobject *parent = kobj->parent;
Wedson Almeida Filhoee6d3dd2021-12-24 23:13:45 +0000666 const struct kobj_type *t = get_ktype(kobj);
Kay Sieversaf5ca3f42007-12-20 02:09:39 +0100667 const char *name = kobj->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Thomas Weißschuh98406332023-03-11 03:14:46 +0000669 pr_debug("'%s' (%p): %s, parent %p\n",
Russell Kingc817a672013-06-27 15:06:14 +0100670 kobject_name(kobj), kobj, __func__, kobj->parent);
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100671
Greg Kroah-Hartman3ca8fba2024-02-08 16:02:50 +0000672 if (t && !t->release)
673 pr_debug("'%s' (%p): does not have a release() function, it is broken and must be fixed. See Documentation/core-api/kobject.rst.\n",
674 kobject_name(kobj), kobj);
675
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100676 /* remove from sysfs if the caller did not do it */
677 if (kobj->state_in_sysfs) {
Thomas Weißschuh98406332023-03-11 03:14:46 +0000678 pr_debug("'%s' (%p): auto cleanup kobject_del\n",
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100679 kobject_name(kobj), kobj);
Heikki Krogerus079ad2f2020-06-04 19:46:46 +0200680 __kobject_del(kobj);
681 } else {
682 /* avoid dropping the parent reference unnecessarily */
683 parent = NULL;
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100684 }
685
Greg Kroah-Hartman3ca8fba2024-02-08 16:02:50 +0000686 if (t && t->release) {
Thomas Weißschuh98406332023-03-11 03:14:46 +0000687 pr_debug("'%s' (%p): calling ktype release\n",
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100688 kobject_name(kobj), kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 t->release(kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100690 }
691
692 /* free name if we allocated it */
Kay Sieversaf5ca3f42007-12-20 02:09:39 +0100693 if (name) {
Thomas Weißschuh98406332023-03-11 03:14:46 +0000694 pr_debug("'%s': free name\n", name);
Rasmus Villemoesf773f322015-11-06 16:31:23 -0800695 kfree_const(name);
Greg Kroah-Hartmance2c9cb2007-09-12 15:06:57 -0700696 }
Heikki Krogerus079ad2f2020-06-04 19:46:46 +0200697
698 kobject_put(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
Russell Kingc817a672013-06-27 15:06:14 +0100701#ifdef CONFIG_DEBUG_KOBJECT_RELEASE
702static void kobject_delayed_cleanup(struct work_struct *work)
703{
704 kobject_cleanup(container_of(to_delayed_work(work),
705 struct kobject, release));
706}
707#endif
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709static void kobject_release(struct kref *kref)
710{
Russell Kingc817a672013-06-27 15:06:14 +0100711 struct kobject *kobj = container_of(kref, struct kobject, kref);
712#ifdef CONFIG_DEBUG_KOBJECT_RELEASE
Jason A. Donenfeld8032bf12022-10-09 20:44:02 -0600713 unsigned long delay = HZ + HZ * get_random_u32_below(4);
Thomas Weißschuh98406332023-03-11 03:14:46 +0000714 pr_info("'%s' (%p): %s, parent %p (delayed %ld)\n",
715 kobject_name(kobj), kobj, __func__, kobj->parent, delay);
Russell Kingc817a672013-06-27 15:06:14 +0100716 INIT_DELAYED_WORK(&kobj->release, kobject_delayed_cleanup);
Bjorn Helgaas89c86a62013-12-05 17:37:51 -0700717
718 schedule_delayed_work(&kobj->release, delay);
Russell Kingc817a672013-06-27 15:06:14 +0100719#else
720 kobject_cleanup(kobj);
721#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}
723
724/**
Tobin C. Hardinged8563492019-05-02 12:31:40 +1000725 * kobject_put() - Decrement refcount for object.
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800726 * @kobj: object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 *
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800728 * Decrement the refcount, and if 0, call kobject_cleanup().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800730void kobject_put(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
Greg Kroah-Hartmanc1ebdae2008-02-26 09:36:38 -0800732 if (kobj) {
Arjan van de Vend955c782008-07-25 01:45:55 -0700733 if (!kobj->state_initialized)
Andy Shevchenko82d1f1172018-03-15 15:23:43 +0200734 WARN(1, KERN_WARNING
735 "kobject: '%s' (%p): is not initialized, yet kobject_put() is being called.\n",
736 kobject_name(kobj), kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 kref_put(&kobj->kref, kobject_release);
Greg Kroah-Hartmanc1ebdae2008-02-26 09:36:38 -0800738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
Gabriel Somlofa40ae32015-08-10 15:51:43 -0400740EXPORT_SYMBOL(kobject_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800742static void dynamic_kobj_release(struct kobject *kobj)
Jun'ichi Nomura74231722006-03-13 17:14:25 -0500743{
Thomas Weißschuh98406332023-03-11 03:14:46 +0000744 pr_debug("(%p): %s\n", kobj, __func__);
Jun'ichi Nomura74231722006-03-13 17:14:25 -0500745 kfree(kobj);
746}
747
Thomas Weißschuhdda6b812023-02-04 21:59:50 +0000748static const struct kobj_type dynamic_kobj_ktype = {
Kay Sievers386f2752007-11-02 13:47:53 +0100749 .release = dynamic_kobj_release,
750 .sysfs_ops = &kobj_sysfs_ops,
Jun'ichi Nomura74231722006-03-13 17:14:25 -0500751};
752
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800753/**
Tobin C. Hardinged8563492019-05-02 12:31:40 +1000754 * kobject_create() - Create a struct kobject dynamically.
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800755 *
756 * This function creates a kobject structure dynamically and sets it up
757 * to be a "dynamic" kobject with a default release function set up.
758 *
759 * If the kobject was not able to be created, NULL will be returned.
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800760 * The kobject structure returned from here must be cleaned up with a
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700761 * call to kobject_put() and not kfree(), as kobject_init() has
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800762 * already been called on this structure.
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800763 */
Qu Wenruo8988bac2021-08-31 17:30:44 +0800764static struct kobject *kobject_create(void)
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800765{
766 struct kobject *kobj;
767
768 kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
769 if (!kobj)
770 return NULL;
771
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700772 kobject_init(kobj, &dynamic_kobj_ktype);
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800773 return kobj;
774}
775
776/**
Tobin C. Hardinged8563492019-05-02 12:31:40 +1000777 * kobject_create_and_add() - Create a struct kobject dynamically and
778 * register it with sysfs.
Zhi Yong Wu9ff1f832012-05-07 10:48:25 +0800779 * @name: the name for the kobject
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800780 * @parent: the parent kobject of this kobject, if any.
781 *
Dave Youngf70701a2008-01-28 16:58:00 +0800782 * This function creates a kobject structure dynamically and registers it
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800783 * with sysfs. When you are finished with this structure, call
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -0800784 * kobject_put() and the structure will be dynamically freed when
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800785 * it is no longer being used.
786 *
787 * If the kobject was not able to be created, NULL will be returned.
788 */
789struct kobject *kobject_create_and_add(const char *name, struct kobject *parent)
790{
791 struct kobject *kobj;
792 int retval;
793
794 kobj = kobject_create();
795 if (!kobj)
796 return NULL;
797
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700798 retval = kobject_add(kobj, parent, "%s", name);
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800799 if (retval) {
Andy Shevchenko82d1f1172018-03-15 15:23:43 +0200800 pr_warn("%s: kobject_add error: %d\n", __func__, retval);
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800801 kobject_put(kobj);
802 kobj = NULL;
803 }
804 return kobj;
805}
806EXPORT_SYMBOL_GPL(kobject_create_and_add);
807
Jun'ichi Nomura74231722006-03-13 17:14:25 -0500808/**
Tobin C. Hardinged8563492019-05-02 12:31:40 +1000809 * kset_init() - Initialize a kset for use.
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800810 * @k: kset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800812void kset_init(struct kset *k)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Greg Kroah-Hartmane1543dd2007-12-17 23:05:35 -0700814 kobject_init_internal(&k->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 INIT_LIST_HEAD(&k->list);
816 spin_lock_init(&k->list_lock);
817}
818
Kay Sievers23b52122007-11-02 13:47:53 +0100819/* default kobject attribute operations */
820static ssize_t kobj_attr_show(struct kobject *kobj, struct attribute *attr,
821 char *buf)
822{
823 struct kobj_attribute *kattr;
824 ssize_t ret = -EIO;
825
826 kattr = container_of(attr, struct kobj_attribute, attr);
827 if (kattr->show)
828 ret = kattr->show(kobj, kattr, buf);
829 return ret;
830}
831
832static ssize_t kobj_attr_store(struct kobject *kobj, struct attribute *attr,
833 const char *buf, size_t count)
834{
835 struct kobj_attribute *kattr;
836 ssize_t ret = -EIO;
837
838 kattr = container_of(attr, struct kobj_attribute, attr);
839 if (kattr->store)
840 ret = kattr->store(kobj, kattr, buf, count);
841 return ret;
842}
843
Emese Revfy52cf25d2010-01-19 02:58:23 +0100844const struct sysfs_ops kobj_sysfs_ops = {
Kay Sievers23b52122007-11-02 13:47:53 +0100845 .show = kobj_attr_show,
846 .store = kobj_attr_store,
847};
Jeff Mahoney29dfe2d2013-11-01 13:06:56 -0400848EXPORT_SYMBOL_GPL(kobj_sysfs_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850/**
Tobin C. Hardinged8563492019-05-02 12:31:40 +1000851 * kset_register() - Initialize and add a kset.
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800852 * @k: kset.
Yang Yingliang1662cea2022-10-25 15:15:49 +0800853 *
854 * NOTE: On error, the kset.kobj.name allocated by() kobj_set_name()
855 * is freed, it can not be used any more.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800857int kset_register(struct kset *k)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
Kay Sievers80f03e32007-05-26 11:21:36 +0200859 int err;
860
Greg Kroah-Hartman31b9025a2007-01-18 12:23:51 -0800861 if (!k)
862 return -EINVAL;
Kay Sievers80f03e32007-05-26 11:21:36 +0200863
Zhen Lei4d0fe8c2023-08-05 16:41:13 +0800864 if (!k->kobj.ktype) {
865 pr_err("must have a ktype to be initialized properly!\n");
866 return -EINVAL;
867 }
868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 kset_init(k);
Greg Kroah-Hartman12e339a2002-04-09 12:14:34 -0700870 err = kobject_add_internal(&k->kobj);
Yang Yingliang1662cea2022-10-25 15:15:49 +0800871 if (err) {
872 kfree_const(k->kobj.name);
873 /* Set it to NULL to avoid accessing bad pointer in callers. */
874 k->kobj.name = NULL;
Kay Sievers80f03e32007-05-26 11:21:36 +0200875 return err;
Yang Yingliang1662cea2022-10-25 15:15:49 +0800876 }
Kay Sievers80f03e32007-05-26 11:21:36 +0200877 kobject_uevent(&k->kobj, KOBJ_ADD);
878 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
Gabriel Somlofa40ae32015-08-10 15:51:43 -0400880EXPORT_SYMBOL(kset_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882/**
Tobin C. Hardinged8563492019-05-02 12:31:40 +1000883 * kset_unregister() - Remove a kset.
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800884 * @k: kset.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800886void kset_unregister(struct kset *k)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
Greg Kroah-Hartman31b9025a2007-01-18 12:23:51 -0800888 if (!k)
889 return;
Bjorn Helgaas35a5fe62013-12-05 17:38:00 -0700890 kobject_del(&k->kobj);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -0800891 kobject_put(&k->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
Gabriel Somlofa40ae32015-08-10 15:51:43 -0400893EXPORT_SYMBOL(kset_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895/**
Tobin C. Hardinged8563492019-05-02 12:31:40 +1000896 * kset_find_obj() - Search for object in kset.
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800897 * @kset: kset we're looking in.
898 * @name: object's name.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 *
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800900 * Lock kset via @kset->subsys, and iterate over @kset->list,
901 * looking for a matching kobject. If matching object is found
902 * take a reference and return the object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800904struct kobject *kset_find_obj(struct kset *kset, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Robert P. J. Dayc6a2a3d2008-03-27 01:13:34 -0400906 struct kobject *k;
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800907 struct kobject *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 spin_lock(&kset->list_lock);
Robin Holtc25d1df2010-09-29 14:00:54 -0500910
Robert P. J. Dayc6a2a3d2008-03-27 01:13:34 -0400911 list_for_each_entry(k, &kset->list, entry) {
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800912 if (kobject_name(k) && !strcmp(kobject_name(k), name)) {
Linus Torvaldsa49b7e82013-04-13 15:15:30 -0700913 ret = kobject_get_unless_zero(k);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 break;
915 }
916 }
Robin Holtc25d1df2010-09-29 14:00:54 -0500917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 spin_unlock(&kset->list_lock);
919 return ret;
920}
Gabriel Somlo2fe829a2016-01-28 09:23:12 -0500921EXPORT_SYMBOL_GPL(kset_find_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700923static void kset_release(struct kobject *kobj)
924{
925 struct kset *kset = container_of(kobj, struct kset, kobj);
Thomas Weißschuh98406332023-03-11 03:14:46 +0000926 pr_debug("'%s' (%p): %s\n",
Harvey Harrison810304d2008-04-30 00:55:08 -0700927 kobject_name(kobj), kobj, __func__);
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700928 kfree(kset);
929}
930
Greg Kroah-Hartman02a476d92022-11-21 10:46:45 +0100931static void kset_get_ownership(const struct kobject *kobj, kuid_t *uid, kgid_t *gid)
Dmitry Torokhovd028b6f2018-07-20 21:56:49 +0000932{
933 if (kobj->parent)
934 kobject_get_ownership(kobj->parent, uid, gid);
935}
936
Thomas Weißschuhdda6b812023-02-04 21:59:50 +0000937static const struct kobj_type kset_ktype = {
Kay Sievers386f2752007-11-02 13:47:53 +0100938 .sysfs_ops = &kobj_sysfs_ops,
Dmitry Torokhovd028b6f2018-07-20 21:56:49 +0000939 .release = kset_release,
940 .get_ownership = kset_get_ownership,
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700941};
942
943/**
Tobin C. Hardinged8563492019-05-02 12:31:40 +1000944 * kset_create() - Create a struct kset dynamically.
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700945 *
946 * @name: the name for the kset
947 * @uevent_ops: a struct kset_uevent_ops for the kset
948 * @parent_kobj: the parent kobject of this kset, if any.
949 *
950 * This function creates a kset structure dynamically. This structure can
951 * then be registered with the system and show up in sysfs with a call to
952 * kset_register(). When you are finished with this structure, if
953 * kset_register() has been called, call kset_unregister() and the
954 * structure will be dynamically freed when it is no longer being used.
955 *
956 * If the kset was not able to be created, NULL will be returned.
957 */
958static struct kset *kset_create(const char *name,
Emese Revfy9cd43612009-12-31 14:52:51 +0100959 const struct kset_uevent_ops *uevent_ops,
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700960 struct kobject *parent_kobj)
961{
962 struct kset *kset;
Dave Youngd9cd8f32009-05-11 14:17:45 +0800963 int retval;
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700964
965 kset = kzalloc(sizeof(*kset), GFP_KERNEL);
966 if (!kset)
967 return NULL;
Kees Cookb7165eb2013-06-06 13:52:19 -0700968 retval = kobject_set_name(&kset->kobj, "%s", name);
Dave Youngd9cd8f32009-05-11 14:17:45 +0800969 if (retval) {
970 kfree(kset);
971 return NULL;
972 }
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700973 kset->uevent_ops = uevent_ops;
974 kset->kobj.parent = parent_kobj;
975
976 /*
Kay Sievers386f2752007-11-02 13:47:53 +0100977 * The kobject of this kset will have a type of kset_ktype and belong to
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700978 * no kset itself. That way we can properly free it when it is
979 * finished being used.
980 */
Kay Sievers386f2752007-11-02 13:47:53 +0100981 kset->kobj.ktype = &kset_ktype;
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700982 kset->kobj.kset = NULL;
983
984 return kset;
985}
986
987/**
Tobin C. Hardinged8563492019-05-02 12:31:40 +1000988 * kset_create_and_add() - Create a struct kset dynamically and add it to sysfs.
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700989 *
990 * @name: the name for the kset
991 * @uevent_ops: a struct kset_uevent_ops for the kset
992 * @parent_kobj: the parent kobject of this kset, if any.
993 *
994 * This function creates a kset structure dynamically and registers it
995 * with sysfs. When you are finished with this structure, call
996 * kset_unregister() and the structure will be dynamically freed when it
997 * is no longer being used.
998 *
999 * If the kset was not able to be created, NULL will be returned.
1000 */
1001struct kset *kset_create_and_add(const char *name,
Emese Revfy9cd43612009-12-31 14:52:51 +01001002 const struct kset_uevent_ops *uevent_ops,
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -07001003 struct kobject *parent_kobj)
1004{
1005 struct kset *kset;
1006 int error;
1007
1008 kset = kset_create(name, uevent_ops, parent_kobj);
1009 if (!kset)
1010 return NULL;
1011 error = kset_register(kset);
1012 if (error) {
1013 kfree(kset);
1014 return NULL;
1015 }
1016 return kset;
1017}
1018EXPORT_SYMBOL_GPL(kset_create_and_add);
1019
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001020
1021static DEFINE_SPINLOCK(kobj_ns_type_lock);
1022static const struct kobj_ns_type_operations *kobj_ns_ops_tbl[KOBJ_NS_TYPES];
1023
1024int kobj_ns_type_register(const struct kobj_ns_type_operations *ops)
1025{
1026 enum kobj_ns_type type = ops->type;
1027 int error;
1028
1029 spin_lock(&kobj_ns_type_lock);
1030
1031 error = -EINVAL;
Zhen Leie2dfa1d2023-07-26 14:25:08 +08001032 if (!kobj_ns_type_is_valid(type))
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001033 goto out;
1034
1035 error = -EBUSY;
1036 if (kobj_ns_ops_tbl[type])
1037 goto out;
1038
1039 error = 0;
1040 kobj_ns_ops_tbl[type] = ops;
1041
1042out:
1043 spin_unlock(&kobj_ns_type_lock);
1044 return error;
1045}
1046
1047int kobj_ns_type_registered(enum kobj_ns_type type)
1048{
1049 int registered = 0;
1050
1051 spin_lock(&kobj_ns_type_lock);
Zhen Leie2dfa1d2023-07-26 14:25:08 +08001052 if (kobj_ns_type_is_valid(type))
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001053 registered = kobj_ns_ops_tbl[type] != NULL;
1054 spin_unlock(&kobj_ns_type_lock);
1055
1056 return registered;
1057}
1058
Greg Kroah-Hartman542aa242022-11-21 10:46:46 +01001059const struct kobj_ns_type_operations *kobj_child_ns_ops(const struct kobject *parent)
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001060{
1061 const struct kobj_ns_type_operations *ops = NULL;
1062
Greg Kroah-Hartman3ca8fba2024-02-08 16:02:50 +00001063 if (parent && parent->ktype && parent->ktype->child_ns_type)
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001064 ops = parent->ktype->child_ns_type(parent);
1065
1066 return ops;
1067}
1068
Greg Kroah-Hartman542aa242022-11-21 10:46:46 +01001069const struct kobj_ns_type_operations *kobj_ns_ops(const struct kobject *kobj)
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001070{
1071 return kobj_child_ns_ops(kobj->parent);
1072}
1073
Eric W. Biederman7dc5dbc2013-03-25 20:07:01 -07001074bool kobj_ns_current_may_mount(enum kobj_ns_type type)
1075{
Eric W. Biederman730d7d32013-09-23 14:41:17 -07001076 bool may_mount = true;
Eric W. Biederman7dc5dbc2013-03-25 20:07:01 -07001077
1078 spin_lock(&kobj_ns_type_lock);
Zhen Leie2dfa1d2023-07-26 14:25:08 +08001079 if (kobj_ns_type_is_valid(type) && kobj_ns_ops_tbl[type])
Eric W. Biederman7dc5dbc2013-03-25 20:07:01 -07001080 may_mount = kobj_ns_ops_tbl[type]->current_may_mount();
1081 spin_unlock(&kobj_ns_type_lock);
1082
1083 return may_mount;
1084}
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001085
Al Viroa685e082011-06-08 21:13:01 -04001086void *kobj_ns_grab_current(enum kobj_ns_type type)
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001087{
Al Viroa685e082011-06-08 21:13:01 -04001088 void *ns = NULL;
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001089
1090 spin_lock(&kobj_ns_type_lock);
Zhen Leie2dfa1d2023-07-26 14:25:08 +08001091 if (kobj_ns_type_is_valid(type) && kobj_ns_ops_tbl[type])
Al Viroa685e082011-06-08 21:13:01 -04001092 ns = kobj_ns_ops_tbl[type]->grab_current_ns();
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001093 spin_unlock(&kobj_ns_type_lock);
1094
1095 return ns;
1096}
Bart Van Assche172856e2018-01-22 14:27:11 -08001097EXPORT_SYMBOL_GPL(kobj_ns_grab_current);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001098
1099const void *kobj_ns_netlink(enum kobj_ns_type type, struct sock *sk)
1100{
1101 const void *ns = NULL;
1102
1103 spin_lock(&kobj_ns_type_lock);
Zhen Leie2dfa1d2023-07-26 14:25:08 +08001104 if (kobj_ns_type_is_valid(type) && kobj_ns_ops_tbl[type])
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001105 ns = kobj_ns_ops_tbl[type]->netlink_ns(sk);
1106 spin_unlock(&kobj_ns_type_lock);
1107
1108 return ns;
1109}
1110
1111const void *kobj_ns_initial(enum kobj_ns_type type)
1112{
1113 const void *ns = NULL;
1114
1115 spin_lock(&kobj_ns_type_lock);
Zhen Leie2dfa1d2023-07-26 14:25:08 +08001116 if (kobj_ns_type_is_valid(type) && kobj_ns_ops_tbl[type])
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001117 ns = kobj_ns_ops_tbl[type]->initial_ns();
1118 spin_unlock(&kobj_ns_type_lock);
1119
1120 return ns;
1121}
1122
Al Viroa685e082011-06-08 21:13:01 -04001123void kobj_ns_drop(enum kobj_ns_type type, void *ns)
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001124{
Al Viroa685e082011-06-08 21:13:01 -04001125 spin_lock(&kobj_ns_type_lock);
Zhen Leie2dfa1d2023-07-26 14:25:08 +08001126 if (kobj_ns_type_is_valid(type) &&
Al Viroa685e082011-06-08 21:13:01 -04001127 kobj_ns_ops_tbl[type] && kobj_ns_ops_tbl[type]->drop_ns)
1128 kobj_ns_ops_tbl[type]->drop_ns(ns);
1129 spin_unlock(&kobj_ns_type_lock);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001130}
Bart Van Assche172856e2018-01-22 14:27:11 -08001131EXPORT_SYMBOL_GPL(kobj_ns_drop);