kobject.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * kobject.c - library routines for handling generic kernel objects
  4. *
  5. * Copyright (c) 2002-2003 Patrick Mochel <mochel@osdl.org>
  6. * Copyright (c) 2006-2007 Greg Kroah-Hartman <greg@kroah.com>
  7. * Copyright (c) 2006-2007 Novell Inc.
  8. *
  9. * Please see the file Documentation/core-api/kobject.rst for critical information
  10. * about using the kobject interface.
  11. */
  12. #include <linux/kobject.h>
  13. #include <linux/string.h>
  14. #include <linux/export.h>
  15. #include <linux/stat.h>
  16. #include <linux/slab.h>
  17. #include <linux/random.h>
  18. /**
  19. * kobject_namespace() - Return @kobj's namespace tag.
  20. * @kobj: kobject in question
  21. *
  22. * Returns namespace tag of @kobj if its parent has namespace ops enabled
  23. * and thus @kobj should have a namespace tag associated with it. Returns
  24. * %NULL otherwise.
  25. */
  26. const void *kobject_namespace(struct kobject *kobj)
  27. {
  28. const struct kobj_ns_type_operations *ns_ops = kobj_ns_ops(kobj);
  29. if (!ns_ops || ns_ops->type == KOBJ_NS_TYPE_NONE)
  30. return NULL;
  31. return kobj->ktype->namespace(kobj);
  32. }
  33. /**
  34. * kobject_get_ownership() - Get sysfs ownership data for @kobj.
  35. * @kobj: kobject in question
  36. * @uid: kernel user ID for sysfs objects
  37. * @gid: kernel group ID for sysfs objects
  38. *
  39. * Returns initial uid/gid pair that should be used when creating sysfs
  40. * representation of given kobject. Normally used to adjust ownership of
  41. * objects in a container.
  42. */
  43. void kobject_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
  44. {
  45. *uid = GLOBAL_ROOT_UID;
  46. *gid = GLOBAL_ROOT_GID;
  47. if (kobj->ktype->get_ownership)
  48. kobj->ktype->get_ownership(kobj, uid, gid);
  49. }
  50. /*
  51. * populate_dir - populate directory with attributes.
  52. * @kobj: object we're working on.
  53. *
  54. * Most subsystems have a set of default attributes that are associated
  55. * with an object that registers with them. This is a helper called during
  56. * object registration that loops through the default attributes of the
  57. * subsystem and creates attributes files for them in sysfs.
  58. */
  59. static int populate_dir(struct kobject *kobj)
  60. {
  61. struct kobj_type *t = get_ktype(kobj);
  62. struct attribute *attr;
  63. int error = 0;
  64. int i;
  65. if (t && t->default_attrs) {
  66. for (i = 0; (attr = t->default_attrs[i]) != NULL; i++) {
  67. error = sysfs_create_file(kobj, attr);
  68. if (error)
  69. break;
  70. }
  71. }
  72. return error;
  73. }
  74. static int create_dir(struct kobject *kobj)
  75. {
  76. const struct kobj_type *ktype = get_ktype(kobj);
  77. const struct kobj_ns_type_operations *ops;
  78. int error;
  79. error = sysfs_create_dir_ns(kobj, kobject_namespace(kobj));
  80. if (error)
  81. return error;
  82. error = populate_dir(kobj);
  83. if (error) {
  84. sysfs_remove_dir(kobj);
  85. return error;
  86. }
  87. if (ktype) {
  88. error = sysfs_create_groups(kobj, ktype->default_groups);
  89. if (error) {
  90. sysfs_remove_dir(kobj);
  91. return error;
  92. }
  93. }
  94. /*
  95. * @kobj->sd may be deleted by an ancestor going away. Hold an
  96. * extra reference so that it stays until @kobj is gone.
  97. */
  98. sysfs_get(kobj->sd);
  99. /*
  100. * If @kobj has ns_ops, its children need to be filtered based on
  101. * their namespace tags. Enable namespace support on @kobj->sd.
  102. */
  103. ops = kobj_child_ns_ops(kobj);
  104. if (ops) {
  105. BUG_ON(ops->type <= KOBJ_NS_TYPE_NONE);
  106. BUG_ON(ops->type >= KOBJ_NS_TYPES);
  107. BUG_ON(!kobj_ns_type_registered(ops->type));
  108. sysfs_enable_ns(kobj->sd);
  109. }
  110. return 0;
  111. }
  112. static int get_kobj_path_length(struct kobject *kobj)
  113. {
  114. int length = 1;
  115. struct kobject *parent = kobj;
  116. /* walk up the ancestors until we hit the one pointing to the
  117. * root.
  118. * Add 1 to strlen for leading '/' of each level.
  119. */
  120. do {
  121. if (kobject_name(parent) == NULL)
  122. return 0;
  123. length += strlen(kobject_name(parent)) + 1;
  124. parent = parent->parent;
  125. } while (parent);
  126. return length;
  127. }
  128. static void fill_kobj_path(struct kobject *kobj, char *path, int length)
  129. {
  130. struct kobject *parent;
  131. --length;
  132. for (parent = kobj; parent; parent = parent->parent) {
  133. int cur = strlen(kobject_name(parent));
  134. /* back up enough to print this name with '/' */
  135. length -= cur;
  136. memcpy(path + length, kobject_name(parent), cur);
  137. *(path + --length) = '/';
  138. }
  139. pr_debug("kobject: '%s' (%p): %s: path = '%s'\n", kobject_name(kobj),
  140. kobj, __func__, path);
  141. }
  142. /**
  143. * kobject_get_path() - Allocate memory and fill in the path for @kobj.
  144. * @kobj: kobject in question, with which to build the path
  145. * @gfp_mask: the allocation type used to allocate the path
  146. *
  147. * Return: The newly allocated memory, caller must free with kfree().
  148. */
  149. char *kobject_get_path(struct kobject *kobj, gfp_t gfp_mask)
  150. {
  151. char *path;
  152. int len;
  153. len = get_kobj_path_length(kobj);
  154. if (len == 0)
  155. return NULL;
  156. path = kzalloc(len, gfp_mask);
  157. if (!path)
  158. return NULL;
  159. fill_kobj_path(kobj, path, len);
  160. return path;
  161. }
  162. EXPORT_SYMBOL_GPL(kobject_get_path);
  163. /* add the kobject to its kset's list */
  164. static void kobj_kset_join(struct kobject *kobj)
  165. {
  166. if (!kobj->kset)
  167. return;
  168. kset_get(kobj->kset);
  169. spin_lock(&kobj->kset->list_lock);
  170. list_add_tail(&kobj->entry, &kobj->kset->list);
  171. spin_unlock(&kobj->kset->list_lock);
  172. }
  173. /* remove the kobject from its kset's list */
  174. static void kobj_kset_leave(struct kobject *kobj)
  175. {
  176. if (!kobj->kset)
  177. return;
  178. spin_lock(&kobj->kset->list_lock);
  179. list_del_init(&kobj->entry);
  180. spin_unlock(&kobj->kset->list_lock);
  181. kset_put(kobj->kset);
  182. }
  183. static void kobject_init_internal(struct kobject *kobj)
  184. {
  185. if (!kobj)
  186. return;
  187. kref_init(&kobj->kref);
  188. INIT_LIST_HEAD(&kobj->entry);
  189. kobj->state_in_sysfs = 0;
  190. kobj->state_add_uevent_sent = 0;
  191. kobj->state_remove_uevent_sent = 0;
  192. kobj->state_initialized = 1;
  193. }
  194. static int kobject_add_internal(struct kobject *kobj)
  195. {
  196. int error = 0;
  197. struct kobject *parent;
  198. if (!kobj)
  199. return -ENOENT;
  200. if (!kobj->name || !kobj->name[0]) {
  201. WARN(1,
  202. "kobject: (%p): attempted to be registered with empty name!\n",
  203. kobj);
  204. return -EINVAL;
  205. }
  206. parent = kobject_get(kobj->parent);
  207. /* join kset if set, use it as parent if we do not already have one */
  208. if (kobj->kset) {
  209. if (!parent)
  210. parent = kobject_get(&kobj->kset->kobj);
  211. kobj_kset_join(kobj);
  212. kobj->parent = parent;
  213. }
  214. pr_debug("kobject: '%s' (%p): %s: parent: '%s', set: '%s'\n",
  215. kobject_name(kobj), kobj, __func__,
  216. parent ? kobject_name(parent) : "<NULL>",
  217. kobj->kset ? kobject_name(&kobj->kset->kobj) : "<NULL>");
  218. error = create_dir(kobj);
  219. if (error) {
  220. kobj_kset_leave(kobj);
  221. kobject_put(parent);
  222. kobj->parent = NULL;
  223. /* be noisy on error issues */
  224. if (error == -EEXIST)
  225. pr_err("%s failed for %s with -EEXIST, don't try to register things with the same name in the same directory.\n",
  226. __func__, kobject_name(kobj));
  227. else
  228. pr_err("%s failed for %s (error: %d parent: %s)\n",
  229. __func__, kobject_name(kobj), error,
  230. parent ? kobject_name(parent) : "'none'");
  231. } else
  232. kobj->state_in_sysfs = 1;
  233. return error;
  234. }
  235. /**
  236. * kobject_set_name_vargs() - Set the name of a kobject.
  237. * @kobj: struct kobject to set the name of
  238. * @fmt: format string used to build the name
  239. * @vargs: vargs to format the string.
  240. */
  241. int kobject_set_name_vargs(struct kobject *kobj, const char *fmt,
  242. va_list vargs)
  243. {
  244. const char *s;
  245. if (kobj->name && !fmt)
  246. return 0;
  247. s = kvasprintf_const(GFP_KERNEL, fmt, vargs);
  248. if (!s)
  249. return -ENOMEM;
  250. /*
  251. * ewww... some of these buggers have '/' in the name ... If
  252. * that's the case, we need to make sure we have an actual
  253. * allocated copy to modify, since kvasprintf_const may have
  254. * returned something from .rodata.
  255. */
  256. if (strchr(s, '/')) {
  257. char *t;
  258. t = kstrdup(s, GFP_KERNEL);
  259. kfree_const(s);
  260. if (!t)
  261. return -ENOMEM;
  262. strreplace(t, '/', '!');
  263. s = t;
  264. }
  265. kfree_const(kobj->name);
  266. kobj->name = s;
  267. return 0;
  268. }
  269. /**
  270. * kobject_set_name() - Set the name of a kobject.
  271. * @kobj: struct kobject to set the name of
  272. * @fmt: format string used to build the name
  273. *
  274. * This sets the name of the kobject. If you have already added the
  275. * kobject to the system, you must call kobject_rename() in order to
  276. * change the name of the kobject.
  277. */
  278. int kobject_set_name(struct kobject *kobj, const char *fmt, ...)
  279. {
  280. va_list vargs;
  281. int retval;
  282. va_start(vargs, fmt);
  283. retval = kobject_set_name_vargs(kobj, fmt, vargs);
  284. va_end(vargs);
  285. return retval;
  286. }
  287. EXPORT_SYMBOL(kobject_set_name);
  288. /**
  289. * kobject_init() - Initialize a kobject structure.
  290. * @kobj: pointer to the kobject to initialize
  291. * @ktype: pointer to the ktype for this kobject.
  292. *
  293. * This function will properly initialize a kobject such that it can then
  294. * be passed to the kobject_add() call.
  295. *
  296. * After this function is called, the kobject MUST be cleaned up by a call
  297. * to kobject_put(), not by a call to kfree directly to ensure that all of
  298. * the memory is cleaned up properly.
  299. */
  300. void kobject_init(struct kobject *kobj, struct kobj_type *ktype)
  301. {
  302. char *err_str;
  303. if (!kobj) {
  304. err_str = "invalid kobject pointer!";
  305. goto error;
  306. }
  307. if (!ktype) {
  308. err_str = "must have a ktype to be initialized properly!\n";
  309. goto error;
  310. }
  311. if (kobj->state_initialized) {
  312. /* do not error out as sometimes we can recover */
  313. pr_err("kobject (%p): tried to init an initialized object, something is seriously wrong.\n",
  314. kobj);
  315. dump_stack();
  316. }
  317. kobject_init_internal(kobj);
  318. kobj->ktype = ktype;
  319. return;
  320. error:
  321. pr_err("kobject (%p): %s\n", kobj, err_str);
  322. dump_stack();
  323. }
  324. EXPORT_SYMBOL(kobject_init);
  325. static __printf(3, 0) int kobject_add_varg(struct kobject *kobj,
  326. struct kobject *parent,
  327. const char *fmt, va_list vargs)
  328. {
  329. int retval;
  330. retval = kobject_set_name_vargs(kobj, fmt, vargs);
  331. if (retval) {
  332. pr_err("kobject: can not set name properly!\n");
  333. return retval;
  334. }
  335. kobj->parent = parent;
  336. return kobject_add_internal(kobj);
  337. }
  338. /**
  339. * kobject_add() - The main kobject add function.
  340. * @kobj: the kobject to add
  341. * @parent: pointer to the parent of the kobject.
  342. * @fmt: format to name the kobject with.
  343. *
  344. * The kobject name is set and added to the kobject hierarchy in this
  345. * function.
  346. *
  347. * If @parent is set, then the parent of the @kobj will be set to it.
  348. * If @parent is NULL, then the parent of the @kobj will be set to the
  349. * kobject associated with the kset assigned to this kobject. If no kset
  350. * is assigned to the kobject, then the kobject will be located in the
  351. * root of the sysfs tree.
  352. *
  353. * Note, no "add" uevent will be created with this call, the caller should set
  354. * up all of the necessary sysfs files for the object and then call
  355. * kobject_uevent() with the UEVENT_ADD parameter to ensure that
  356. * userspace is properly notified of this kobject's creation.
  357. *
  358. * Return: If this function returns an error, kobject_put() must be
  359. * called to properly clean up the memory associated with the
  360. * object. Under no instance should the kobject that is passed
  361. * to this function be directly freed with a call to kfree(),
  362. * that can leak memory.
  363. *
  364. * If this function returns success, kobject_put() must also be called
  365. * in order to properly clean up the memory associated with the object.
  366. *
  367. * In short, once this function is called, kobject_put() MUST be called
  368. * when the use of the object is finished in order to properly free
  369. * everything.
  370. */
  371. int kobject_add(struct kobject *kobj, struct kobject *parent,
  372. const char *fmt, ...)
  373. {
  374. va_list args;
  375. int retval;
  376. if (!kobj)
  377. return -EINVAL;
  378. if (!kobj->state_initialized) {
  379. pr_err("kobject '%s' (%p): tried to add an uninitialized object, something is seriously wrong.\n",
  380. kobject_name(kobj), kobj);
  381. dump_stack();
  382. return -EINVAL;
  383. }
  384. va_start(args, fmt);
  385. retval = kobject_add_varg(kobj, parent, fmt, args);
  386. va_end(args);
  387. return retval;
  388. }
  389. EXPORT_SYMBOL(kobject_add);
  390. /**
  391. * kobject_init_and_add() - Initialize a kobject structure and add it to
  392. * the kobject hierarchy.
  393. * @kobj: pointer to the kobject to initialize
  394. * @ktype: pointer to the ktype for this kobject.
  395. * @parent: pointer to the parent of this kobject.
  396. * @fmt: the name of the kobject.
  397. *
  398. * This function combines the call to kobject_init() and kobject_add().
  399. *
  400. * If this function returns an error, kobject_put() must be called to
  401. * properly clean up the memory associated with the object. This is the
  402. * same type of error handling after a call to kobject_add() and kobject
  403. * lifetime rules are the same here.
  404. */
  405. int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,
  406. struct kobject *parent, const char *fmt, ...)
  407. {
  408. va_list args;
  409. int retval;
  410. kobject_init(kobj, ktype);
  411. va_start(args, fmt);
  412. retval = kobject_add_varg(kobj, parent, fmt, args);
  413. va_end(args);
  414. return retval;
  415. }
  416. EXPORT_SYMBOL_GPL(kobject_init_and_add);
  417. /**
  418. * kobject_rename() - Change the name of an object.
  419. * @kobj: object in question.
  420. * @new_name: object's new name
  421. *
  422. * It is the responsibility of the caller to provide mutual
  423. * exclusion between two different calls of kobject_rename
  424. * on the same kobject and to ensure that new_name is valid and
  425. * won't conflict with other kobjects.
  426. */
  427. int kobject_rename(struct kobject *kobj, const char *new_name)
  428. {
  429. int error = 0;
  430. const char *devpath = NULL;
  431. const char *dup_name = NULL, *name;
  432. char *devpath_string = NULL;
  433. char *envp[2];
  434. kobj = kobject_get(kobj);
  435. if (!kobj)
  436. return -EINVAL;
  437. if (!kobj->parent) {
  438. kobject_put(kobj);
  439. return -EINVAL;
  440. }
  441. devpath = kobject_get_path(kobj, GFP_KERNEL);
  442. if (!devpath) {
  443. error = -ENOMEM;
  444. goto out;
  445. }
  446. devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
  447. if (!devpath_string) {
  448. error = -ENOMEM;
  449. goto out;
  450. }
  451. sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
  452. envp[0] = devpath_string;
  453. envp[1] = NULL;
  454. name = dup_name = kstrdup_const(new_name, GFP_KERNEL);
  455. if (!name) {
  456. error = -ENOMEM;
  457. goto out;
  458. }
  459. error = sysfs_rename_dir_ns(kobj, new_name, kobject_namespace(kobj));
  460. if (error)
  461. goto out;
  462. /* Install the new kobject name */
  463. dup_name = kobj->name;
  464. kobj->name = name;
  465. /* This function is mostly/only used for network interface.
  466. * Some hotplug package track interfaces by their name and
  467. * therefore want to know when the name is changed by the user. */
  468. kobject_uevent_env(kobj, KOBJ_MOVE, envp);
  469. out:
  470. kfree_const(dup_name);
  471. kfree(devpath_string);
  472. kfree(devpath);
  473. kobject_put(kobj);
  474. return error;
  475. }
  476. EXPORT_SYMBOL_GPL(kobject_rename);
  477. /**
  478. * kobject_move() - Move object to another parent.
  479. * @kobj: object in question.
  480. * @new_parent: object's new parent (can be NULL)
  481. */
  482. int kobject_move(struct kobject *kobj, struct kobject *new_parent)
  483. {
  484. int error;
  485. struct kobject *old_parent;
  486. const char *devpath = NULL;
  487. char *devpath_string = NULL;
  488. char *envp[2];
  489. kobj = kobject_get(kobj);
  490. if (!kobj)
  491. return -EINVAL;
  492. new_parent = kobject_get(new_parent);
  493. if (!new_parent) {
  494. if (kobj->kset)
  495. new_parent = kobject_get(&kobj->kset->kobj);
  496. }
  497. /* old object path */
  498. devpath = kobject_get_path(kobj, GFP_KERNEL);
  499. if (!devpath) {
  500. error = -ENOMEM;
  501. goto out;
  502. }
  503. devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
  504. if (!devpath_string) {
  505. error = -ENOMEM;
  506. goto out;
  507. }
  508. sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
  509. envp[0] = devpath_string;
  510. envp[1] = NULL;
  511. error = sysfs_move_dir_ns(kobj, new_parent, kobject_namespace(kobj));
  512. if (error)
  513. goto out;
  514. old_parent = kobj->parent;
  515. kobj->parent = new_parent;
  516. new_parent = NULL;
  517. kobject_put(old_parent);
  518. kobject_uevent_env(kobj, KOBJ_MOVE, envp);
  519. out:
  520. kobject_put(new_parent);
  521. kobject_put(kobj);
  522. kfree(devpath_string);
  523. kfree(devpath);
  524. return error;
  525. }
  526. EXPORT_SYMBOL_GPL(kobject_move);
  527. static void __kobject_del(struct kobject *kobj)
  528. {
  529. struct kernfs_node *sd;
  530. const struct kobj_type *ktype;
  531. sd = kobj->sd;
  532. ktype = get_ktype(kobj);
  533. if (ktype)
  534. sysfs_remove_groups(kobj, ktype->default_groups);
  535. /* send "remove" if the caller did not do it but sent "add" */
  536. if (kobj->state_add_uevent_sent && !kobj->state_remove_uevent_sent) {
  537. pr_debug("kobject: '%s' (%p): auto cleanup 'remove' event\n",
  538. kobject_name(kobj), kobj);
  539. kobject_uevent(kobj, KOBJ_REMOVE);
  540. }
  541. sysfs_remove_dir(kobj);
  542. sysfs_put(sd);
  543. kobj->state_in_sysfs = 0;
  544. kobj_kset_leave(kobj);
  545. kobj->parent = NULL;
  546. }
  547. /**
  548. * kobject_del() - Unlink kobject from hierarchy.
  549. * @kobj: object.
  550. *
  551. * This is the function that should be called to delete an object
  552. * successfully added via kobject_add().
  553. */
  554. void kobject_del(struct kobject *kobj)
  555. {
  556. struct kobject *parent;
  557. if (!kobj)
  558. return;
  559. parent = kobj->parent;
  560. __kobject_del(kobj);
  561. kobject_put(parent);
  562. }
  563. EXPORT_SYMBOL(kobject_del);
  564. /**
  565. * kobject_get() - Increment refcount for object.
  566. * @kobj: object.
  567. */
  568. struct kobject *kobject_get(struct kobject *kobj)
  569. {
  570. if (kobj) {
  571. if (!kobj->state_initialized)
  572. WARN(1, KERN_WARNING
  573. "kobject: '%s' (%p): is not initialized, yet kobject_get() is being called.\n",
  574. kobject_name(kobj), kobj);
  575. kref_get(&kobj->kref);
  576. }
  577. return kobj;
  578. }
  579. EXPORT_SYMBOL(kobject_get);
  580. struct kobject * __must_check kobject_get_unless_zero(struct kobject *kobj)
  581. {
  582. if (!kobj)
  583. return NULL;
  584. if (!kref_get_unless_zero(&kobj->kref))
  585. kobj = NULL;
  586. return kobj;
  587. }
  588. EXPORT_SYMBOL(kobject_get_unless_zero);
  589. /*
  590. * kobject_cleanup - free kobject resources.
  591. * @kobj: object to cleanup
  592. */
  593. static void kobject_cleanup(struct kobject *kobj)
  594. {
  595. struct kobject *parent = kobj->parent;
  596. struct kobj_type *t = get_ktype(kobj);
  597. const char *name = kobj->name;
  598. pr_debug("kobject: '%s' (%p): %s, parent %p\n",
  599. kobject_name(kobj), kobj, __func__, kobj->parent);
  600. if (t && !t->release)
  601. pr_debug("kobject: '%s' (%p): does not have a release() function, it is broken and must be fixed. See Documentation/core-api/kobject.rst.\n",
  602. kobject_name(kobj), kobj);
  603. /* remove from sysfs if the caller did not do it */
  604. if (kobj->state_in_sysfs) {
  605. pr_debug("kobject: '%s' (%p): auto cleanup kobject_del\n",
  606. kobject_name(kobj), kobj);
  607. __kobject_del(kobj);
  608. } else {
  609. /* avoid dropping the parent reference unnecessarily */
  610. parent = NULL;
  611. }
  612. if (t && t->release) {
  613. pr_debug("kobject: '%s' (%p): calling ktype release\n",
  614. kobject_name(kobj), kobj);
  615. t->release(kobj);
  616. }
  617. /* free name if we allocated it */
  618. if (name) {
  619. pr_debug("kobject: '%s': free name\n", name);
  620. kfree_const(name);
  621. }
  622. kobject_put(parent);
  623. }
  624. #ifdef CONFIG_DEBUG_KOBJECT_RELEASE
  625. static void kobject_delayed_cleanup(struct work_struct *work)
  626. {
  627. kobject_cleanup(container_of(to_delayed_work(work),
  628. struct kobject, release));
  629. }
  630. #endif
  631. static void kobject_release(struct kref *kref)
  632. {
  633. struct kobject *kobj = container_of(kref, struct kobject, kref);
  634. #ifdef CONFIG_DEBUG_KOBJECT_RELEASE
  635. unsigned long delay = HZ + HZ * (get_random_int() & 0x3);
  636. pr_info("kobject: '%s' (%p): %s, parent %p (delayed %ld)\n",
  637. kobject_name(kobj), kobj, __func__, kobj->parent, delay);
  638. INIT_DELAYED_WORK(&kobj->release, kobject_delayed_cleanup);
  639. schedule_delayed_work(&kobj->release, delay);
  640. #else
  641. kobject_cleanup(kobj);
  642. #endif
  643. }
  644. /**
  645. * kobject_put() - Decrement refcount for object.
  646. * @kobj: object.
  647. *
  648. * Decrement the refcount, and if 0, call kobject_cleanup().
  649. */
  650. void kobject_put(struct kobject *kobj)
  651. {
  652. if (kobj) {
  653. if (!kobj->state_initialized)
  654. WARN(1, KERN_WARNING
  655. "kobject: '%s' (%p): is not initialized, yet kobject_put() is being called.\n",
  656. kobject_name(kobj), kobj);
  657. kref_put(&kobj->kref, kobject_release);
  658. }
  659. }
  660. EXPORT_SYMBOL(kobject_put);
  661. static void dynamic_kobj_release(struct kobject *kobj)
  662. {
  663. pr_debug("kobject: (%p): %s\n", kobj, __func__);
  664. kfree(kobj);
  665. }
  666. static struct kobj_type dynamic_kobj_ktype = {
  667. .release = dynamic_kobj_release,
  668. .sysfs_ops = &kobj_sysfs_ops,
  669. };
  670. /**
  671. * kobject_create() - Create a struct kobject dynamically.
  672. *
  673. * This function creates a kobject structure dynamically and sets it up
  674. * to be a "dynamic" kobject with a default release function set up.
  675. *
  676. * If the kobject was not able to be created, NULL will be returned.
  677. * The kobject structure returned from here must be cleaned up with a
  678. * call to kobject_put() and not kfree(), as kobject_init() has
  679. * already been called on this structure.
  680. */
  681. struct kobject *kobject_create(void)
  682. {
  683. struct kobject *kobj;
  684. kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
  685. if (!kobj)
  686. return NULL;
  687. kobject_init(kobj, &dynamic_kobj_ktype);
  688. return kobj;
  689. }
  690. /**
  691. * kobject_create_and_add() - Create a struct kobject dynamically and
  692. * register it with sysfs.
  693. * @name: the name for the kobject
  694. * @parent: the parent kobject of this kobject, if any.
  695. *
  696. * This function creates a kobject structure dynamically and registers it
  697. * with sysfs. When you are finished with this structure, call
  698. * kobject_put() and the structure will be dynamically freed when
  699. * it is no longer being used.
  700. *
  701. * If the kobject was not able to be created, NULL will be returned.
  702. */
  703. struct kobject *kobject_create_and_add(const char *name, struct kobject *parent)
  704. {
  705. struct kobject *kobj;
  706. int retval;
  707. kobj = kobject_create();
  708. if (!kobj)
  709. return NULL;
  710. retval = kobject_add(kobj, parent, "%s", name);
  711. if (retval) {
  712. pr_warn("%s: kobject_add error: %d\n", __func__, retval);
  713. kobject_put(kobj);
  714. kobj = NULL;
  715. }
  716. return kobj;
  717. }
  718. EXPORT_SYMBOL_GPL(kobject_create_and_add);
  719. /**
  720. * kset_init() - Initialize a kset for use.
  721. * @k: kset
  722. */
  723. void kset_init(struct kset *k)
  724. {
  725. kobject_init_internal(&k->kobj);
  726. INIT_LIST_HEAD(&k->list);
  727. spin_lock_init(&k->list_lock);
  728. }
  729. /* default kobject attribute operations */
  730. static ssize_t kobj_attr_show(struct kobject *kobj, struct attribute *attr,
  731. char *buf)
  732. {
  733. struct kobj_attribute *kattr;
  734. ssize_t ret = -EIO;
  735. kattr = container_of(attr, struct kobj_attribute, attr);
  736. if (kattr->show)
  737. ret = kattr->show(kobj, kattr, buf);
  738. return ret;
  739. }
  740. static ssize_t kobj_attr_store(struct kobject *kobj, struct attribute *attr,
  741. const char *buf, size_t count)
  742. {
  743. struct kobj_attribute *kattr;
  744. ssize_t ret = -EIO;
  745. kattr = container_of(attr, struct kobj_attribute, attr);
  746. if (kattr->store)
  747. ret = kattr->store(kobj, kattr, buf, count);
  748. return ret;
  749. }
  750. const struct sysfs_ops kobj_sysfs_ops = {
  751. .show = kobj_attr_show,
  752. .store = kobj_attr_store,
  753. };
  754. EXPORT_SYMBOL_GPL(kobj_sysfs_ops);
  755. /**
  756. * kset_register() - Initialize and add a kset.
  757. * @k: kset.
  758. */
  759. int kset_register(struct kset *k)
  760. {
  761. int err;
  762. if (!k)
  763. return -EINVAL;
  764. kset_init(k);
  765. err = kobject_add_internal(&k->kobj);
  766. if (err)
  767. return err;
  768. kobject_uevent(&k->kobj, KOBJ_ADD);
  769. return 0;
  770. }
  771. EXPORT_SYMBOL(kset_register);
  772. /**
  773. * kset_unregister() - Remove a kset.
  774. * @k: kset.
  775. */
  776. void kset_unregister(struct kset *k)
  777. {
  778. if (!k)
  779. return;
  780. kobject_del(&k->kobj);
  781. kobject_put(&k->kobj);
  782. }
  783. EXPORT_SYMBOL(kset_unregister);
  784. /**
  785. * kset_find_obj() - Search for object in kset.
  786. * @kset: kset we're looking in.
  787. * @name: object's name.
  788. *
  789. * Lock kset via @kset->subsys, and iterate over @kset->list,
  790. * looking for a matching kobject. If matching object is found
  791. * take a reference and return the object.
  792. */
  793. struct kobject *kset_find_obj(struct kset *kset, const char *name)
  794. {
  795. struct kobject *k;
  796. struct kobject *ret = NULL;
  797. spin_lock(&kset->list_lock);
  798. list_for_each_entry(k, &kset->list, entry) {
  799. if (kobject_name(k) && !strcmp(kobject_name(k), name)) {
  800. ret = kobject_get_unless_zero(k);
  801. break;
  802. }
  803. }
  804. spin_unlock(&kset->list_lock);
  805. return ret;
  806. }
  807. EXPORT_SYMBOL_GPL(kset_find_obj);
  808. static void kset_release(struct kobject *kobj)
  809. {
  810. struct kset *kset = container_of(kobj, struct kset, kobj);
  811. pr_debug("kobject: '%s' (%p): %s\n",
  812. kobject_name(kobj), kobj, __func__);
  813. kfree(kset);
  814. }
  815. static void kset_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
  816. {
  817. if (kobj->parent)
  818. kobject_get_ownership(kobj->parent, uid, gid);
  819. }
  820. static struct kobj_type kset_ktype = {
  821. .sysfs_ops = &kobj_sysfs_ops,
  822. .release = kset_release,
  823. .get_ownership = kset_get_ownership,
  824. };
  825. /**
  826. * kset_create() - Create a struct kset dynamically.
  827. *
  828. * @name: the name for the kset
  829. * @uevent_ops: a struct kset_uevent_ops for the kset
  830. * @parent_kobj: the parent kobject of this kset, if any.
  831. *
  832. * This function creates a kset structure dynamically. This structure can
  833. * then be registered with the system and show up in sysfs with a call to
  834. * kset_register(). When you are finished with this structure, if
  835. * kset_register() has been called, call kset_unregister() and the
  836. * structure will be dynamically freed when it is no longer being used.
  837. *
  838. * If the kset was not able to be created, NULL will be returned.
  839. */
  840. static struct kset *kset_create(const char *name,
  841. const struct kset_uevent_ops *uevent_ops,
  842. struct kobject *parent_kobj)
  843. {
  844. struct kset *kset;
  845. int retval;
  846. kset = kzalloc(sizeof(*kset), GFP_KERNEL);
  847. if (!kset)
  848. return NULL;
  849. retval = kobject_set_name(&kset->kobj, "%s", name);
  850. if (retval) {
  851. kfree(kset);
  852. return NULL;
  853. }
  854. kset->uevent_ops = uevent_ops;
  855. kset->kobj.parent = parent_kobj;
  856. /*
  857. * The kobject of this kset will have a type of kset_ktype and belong to
  858. * no kset itself. That way we can properly free it when it is
  859. * finished being used.
  860. */
  861. kset->kobj.ktype = &kset_ktype;
  862. kset->kobj.kset = NULL;
  863. return kset;
  864. }
  865. /**
  866. * kset_create_and_add() - Create a struct kset dynamically and add it to sysfs.
  867. *
  868. * @name: the name for the kset
  869. * @uevent_ops: a struct kset_uevent_ops for the kset
  870. * @parent_kobj: the parent kobject of this kset, if any.
  871. *
  872. * This function creates a kset structure dynamically and registers it
  873. * with sysfs. When you are finished with this structure, call
  874. * kset_unregister() and the structure will be dynamically freed when it
  875. * is no longer being used.
  876. *
  877. * If the kset was not able to be created, NULL will be returned.
  878. */
  879. struct kset *kset_create_and_add(const char *name,
  880. const struct kset_uevent_ops *uevent_ops,
  881. struct kobject *parent_kobj)
  882. {
  883. struct kset *kset;
  884. int error;
  885. kset = kset_create(name, uevent_ops, parent_kobj);
  886. if (!kset)
  887. return NULL;
  888. error = kset_register(kset);
  889. if (error) {
  890. kfree(kset);
  891. return NULL;
  892. }
  893. return kset;
  894. }
  895. EXPORT_SYMBOL_GPL(kset_create_and_add);
  896. static DEFINE_SPINLOCK(kobj_ns_type_lock);
  897. static const struct kobj_ns_type_operations *kobj_ns_ops_tbl[KOBJ_NS_TYPES];
  898. int kobj_ns_type_register(const struct kobj_ns_type_operations *ops)
  899. {
  900. enum kobj_ns_type type = ops->type;
  901. int error;
  902. spin_lock(&kobj_ns_type_lock);
  903. error = -EINVAL;
  904. if (type >= KOBJ_NS_TYPES)
  905. goto out;
  906. error = -EINVAL;
  907. if (type <= KOBJ_NS_TYPE_NONE)
  908. goto out;
  909. error = -EBUSY;
  910. if (kobj_ns_ops_tbl[type])
  911. goto out;
  912. error = 0;
  913. kobj_ns_ops_tbl[type] = ops;
  914. out:
  915. spin_unlock(&kobj_ns_type_lock);
  916. return error;
  917. }
  918. int kobj_ns_type_registered(enum kobj_ns_type type)
  919. {
  920. int registered = 0;
  921. spin_lock(&kobj_ns_type_lock);
  922. if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES))
  923. registered = kobj_ns_ops_tbl[type] != NULL;
  924. spin_unlock(&kobj_ns_type_lock);
  925. return registered;
  926. }
  927. const struct kobj_ns_type_operations *kobj_child_ns_ops(struct kobject *parent)
  928. {
  929. const struct kobj_ns_type_operations *ops = NULL;
  930. if (parent && parent->ktype && parent->ktype->child_ns_type)
  931. ops = parent->ktype->child_ns_type(parent);
  932. return ops;
  933. }
  934. const struct kobj_ns_type_operations *kobj_ns_ops(struct kobject *kobj)
  935. {
  936. return kobj_child_ns_ops(kobj->parent);
  937. }
  938. bool kobj_ns_current_may_mount(enum kobj_ns_type type)
  939. {
  940. bool may_mount = true;
  941. spin_lock(&kobj_ns_type_lock);
  942. if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
  943. kobj_ns_ops_tbl[type])
  944. may_mount = kobj_ns_ops_tbl[type]->current_may_mount();
  945. spin_unlock(&kobj_ns_type_lock);
  946. return may_mount;
  947. }
  948. void *kobj_ns_grab_current(enum kobj_ns_type type)
  949. {
  950. void *ns = NULL;
  951. spin_lock(&kobj_ns_type_lock);
  952. if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
  953. kobj_ns_ops_tbl[type])
  954. ns = kobj_ns_ops_tbl[type]->grab_current_ns();
  955. spin_unlock(&kobj_ns_type_lock);
  956. return ns;
  957. }
  958. EXPORT_SYMBOL_GPL(kobj_ns_grab_current);
  959. const void *kobj_ns_netlink(enum kobj_ns_type type, struct sock *sk)
  960. {
  961. const void *ns = NULL;
  962. spin_lock(&kobj_ns_type_lock);
  963. if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
  964. kobj_ns_ops_tbl[type])
  965. ns = kobj_ns_ops_tbl[type]->netlink_ns(sk);
  966. spin_unlock(&kobj_ns_type_lock);
  967. return ns;
  968. }
  969. const void *kobj_ns_initial(enum kobj_ns_type type)
  970. {
  971. const void *ns = NULL;
  972. spin_lock(&kobj_ns_type_lock);
  973. if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
  974. kobj_ns_ops_tbl[type])
  975. ns = kobj_ns_ops_tbl[type]->initial_ns();
  976. spin_unlock(&kobj_ns_type_lock);
  977. return ns;
  978. }
  979. void kobj_ns_drop(enum kobj_ns_type type, void *ns)
  980. {
  981. spin_lock(&kobj_ns_type_lock);
  982. if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
  983. kobj_ns_ops_tbl[type] && kobj_ns_ops_tbl[type]->drop_ns)
  984. kobj_ns_ops_tbl[type]->drop_ns(ns);
  985. spin_unlock(&kobj_ns_type_lock);
  986. }
  987. EXPORT_SYMBOL_GPL(kobj_ns_drop);