kobject.c 27 KB

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