kobject.c 28 KB

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