hc
2024-05-10 23fa18eaa71266feff7ba8d83022d9e1cc83c65a
kernel/lib/kobject.c
....@@ -6,7 +6,7 @@
66 * Copyright (c) 2006-2007 Greg Kroah-Hartman <greg@kroah.com>
77 * Copyright (c) 2006-2007 Novell Inc.
88 *
9
- * Please see the file Documentation/kobject.txt for critical information
9
+ * Please see the file Documentation/core-api/kobject.rst for critical information
1010 * about using the kobject interface.
1111 */
1212
....@@ -18,7 +18,7 @@
1818 #include <linux/random.h>
1919
2020 /**
21
- * kobject_namespace - return @kobj's namespace tag
21
+ * kobject_namespace() - Return @kobj's namespace tag.
2222 * @kobj: kobject in question
2323 *
2424 * Returns namespace tag of @kobj if its parent has namespace ops enabled
....@@ -36,7 +36,7 @@
3636 }
3737
3838 /**
39
- * kobject_get_ownership - get sysfs ownership data for @kobj
39
+ * kobject_get_ownership() - Get sysfs ownership data for @kobj.
4040 * @kobj: kobject in question
4141 * @uid: kernel user ID for sysfs objects
4242 * @gid: kernel group ID for sysfs objects
....@@ -82,6 +82,7 @@
8282
8383 static int create_dir(struct kobject *kobj)
8484 {
85
+ const struct kobj_type *ktype = get_ktype(kobj);
8586 const struct kobj_ns_type_operations *ops;
8687 int error;
8788
....@@ -93,6 +94,14 @@
9394 if (error) {
9495 sysfs_remove_dir(kobj);
9596 return error;
97
+ }
98
+
99
+ if (ktype) {
100
+ error = sysfs_create_groups(kobj, ktype->default_groups);
101
+ if (error) {
102
+ sysfs_remove_dir(kobj);
103
+ return error;
104
+ }
96105 }
97106
98107 /*
....@@ -153,12 +162,11 @@
153162 }
154163
155164 /**
156
- * kobject_get_path - generate and return the path associated with a given kobj and kset pair.
157
- *
165
+ * kobject_get_path() - Allocate memory and fill in the path for @kobj.
158166 * @kobj: kobject in question, with which to build the path
159167 * @gfp_mask: the allocation type used to allocate the path
160168 *
161
- * The result must be freed by the caller with kfree().
169
+ * Return: The newly allocated memory, caller must free with kfree().
162170 */
163171 char *kobject_get_path(struct kobject *kobj, gfp_t gfp_mask)
164172 {
....@@ -265,7 +273,7 @@
265273 }
266274
267275 /**
268
- * kobject_set_name_vargs - Set the name of an kobject
276
+ * kobject_set_name_vargs() - Set the name of a kobject.
269277 * @kobj: struct kobject to set the name of
270278 * @fmt: format string used to build the name
271279 * @vargs: vargs to format the string.
....@@ -305,7 +313,7 @@
305313 }
306314
307315 /**
308
- * kobject_set_name - Set the name of a kobject
316
+ * kobject_set_name() - Set the name of a kobject.
309317 * @kobj: struct kobject to set the name of
310318 * @fmt: format string used to build the name
311319 *
....@@ -327,7 +335,7 @@
327335 EXPORT_SYMBOL(kobject_set_name);
328336
329337 /**
330
- * kobject_init - initialize a kobject structure
338
+ * kobject_init() - Initialize a kobject structure.
331339 * @kobj: pointer to the kobject to initialize
332340 * @ktype: pointer to the ktype for this kobject.
333341 *
....@@ -383,7 +391,7 @@
383391 }
384392
385393 /**
386
- * kobject_add - the main kobject add function
394
+ * kobject_add() - The main kobject add function.
387395 * @kobj: the kobject to add
388396 * @parent: pointer to the parent of the kobject.
389397 * @fmt: format to name the kobject with.
....@@ -397,15 +405,23 @@
397405 * is assigned to the kobject, then the kobject will be located in the
398406 * root of the sysfs tree.
399407 *
400
- * If this function returns an error, kobject_put() must be called to
401
- * properly clean up the memory associated with the object.
402
- * Under no instance should the kobject that is passed to this function
403
- * be directly freed with a call to kfree(), that can leak memory.
404
- *
405408 * Note, no "add" uevent will be created with this call, the caller should set
406409 * up all of the necessary sysfs files for the object and then call
407410 * kobject_uevent() with the UEVENT_ADD parameter to ensure that
408411 * userspace is properly notified of this kobject's creation.
412
+ *
413
+ * Return: If this function returns an error, kobject_put() must be
414
+ * called to properly clean up the memory associated with the
415
+ * object. Under no instance should the kobject that is passed
416
+ * to this function be directly freed with a call to kfree(),
417
+ * that can leak memory.
418
+ *
419
+ * If this function returns success, kobject_put() must also be called
420
+ * in order to properly clean up the memory associated with the object.
421
+ *
422
+ * In short, once this function is called, kobject_put() MUST be called
423
+ * when the use of the object is finished in order to properly free
424
+ * everything.
409425 */
410426 int kobject_add(struct kobject *kobj, struct kobject *parent,
411427 const char *fmt, ...)
....@@ -431,15 +447,19 @@
431447 EXPORT_SYMBOL(kobject_add);
432448
433449 /**
434
- * kobject_init_and_add - initialize a kobject structure and add it to the kobject hierarchy
450
+ * kobject_init_and_add() - Initialize a kobject structure and add it to
451
+ * the kobject hierarchy.
435452 * @kobj: pointer to the kobject to initialize
436453 * @ktype: pointer to the ktype for this kobject.
437454 * @parent: pointer to the parent of this kobject.
438455 * @fmt: the name of the kobject.
439456 *
440
- * This function combines the call to kobject_init() and
441
- * kobject_add(). The same type of error handling after a call to
442
- * kobject_add() and kobject lifetime rules are the same here.
457
+ * This function combines the call to kobject_init() and kobject_add().
458
+ *
459
+ * If this function returns an error, kobject_put() must be called to
460
+ * properly clean up the memory associated with the object. This is the
461
+ * same type of error handling after a call to kobject_add() and kobject
462
+ * lifetime rules are the same here.
443463 */
444464 int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,
445465 struct kobject *parent, const char *fmt, ...)
....@@ -458,7 +478,7 @@
458478 EXPORT_SYMBOL_GPL(kobject_init_and_add);
459479
460480 /**
461
- * kobject_rename - change the name of an object
481
+ * kobject_rename() - Change the name of an object.
462482 * @kobj: object in question.
463483 * @new_name: object's new name
464484 *
....@@ -478,8 +498,10 @@
478498 kobj = kobject_get(kobj);
479499 if (!kobj)
480500 return -EINVAL;
481
- if (!kobj->parent)
501
+ if (!kobj->parent) {
502
+ kobject_put(kobj);
482503 return -EINVAL;
504
+ }
483505
484506 devpath = kobject_get_path(kobj, GFP_KERNEL);
485507 if (!devpath) {
....@@ -525,7 +547,7 @@
525547 EXPORT_SYMBOL_GPL(kobject_rename);
526548
527549 /**
528
- * kobject_move - move object to another parent
550
+ * kobject_move() - Move object to another parent.
529551 * @kobj: object in question.
530552 * @new_parent: object's new parent (can be NULL)
531553 */
....@@ -577,30 +599,54 @@
577599 }
578600 EXPORT_SYMBOL_GPL(kobject_move);
579601
580
-/**
581
- * kobject_del - unlink kobject from hierarchy.
582
- * @kobj: object.
583
- */
584
-void kobject_del(struct kobject *kobj)
602
+static void __kobject_del(struct kobject *kobj)
585603 {
586604 struct kernfs_node *sd;
587
-
588
- if (!kobj)
589
- return;
605
+ const struct kobj_type *ktype;
590606
591607 sd = kobj->sd;
608
+ ktype = get_ktype(kobj);
609
+
610
+ if (ktype)
611
+ sysfs_remove_groups(kobj, ktype->default_groups);
612
+
613
+ /* send "remove" if the caller did not do it but sent "add" */
614
+ if (kobj->state_add_uevent_sent && !kobj->state_remove_uevent_sent) {
615
+ pr_debug("kobject: '%s' (%p): auto cleanup 'remove' event\n",
616
+ kobject_name(kobj), kobj);
617
+ kobject_uevent(kobj, KOBJ_REMOVE);
618
+ }
619
+
592620 sysfs_remove_dir(kobj);
593621 sysfs_put(sd);
594622
595623 kobj->state_in_sysfs = 0;
596624 kobj_kset_leave(kobj);
597
- kobject_put(kobj->parent);
598625 kobj->parent = NULL;
626
+}
627
+
628
+/**
629
+ * kobject_del() - Unlink kobject from hierarchy.
630
+ * @kobj: object.
631
+ *
632
+ * This is the function that should be called to delete an object
633
+ * successfully added via kobject_add().
634
+ */
635
+void kobject_del(struct kobject *kobj)
636
+{
637
+ struct kobject *parent;
638
+
639
+ if (!kobj)
640
+ return;
641
+
642
+ parent = kobj->parent;
643
+ __kobject_del(kobj);
644
+ kobject_put(parent);
599645 }
600646 EXPORT_SYMBOL(kobject_del);
601647
602648 /**
603
- * kobject_get - increment refcount for object.
649
+ * kobject_get() - Increment refcount for object.
604650 * @kobj: object.
605651 */
606652 struct kobject *kobject_get(struct kobject *kobj)
....@@ -632,6 +678,7 @@
632678 */
633679 static void kobject_cleanup(struct kobject *kobj)
634680 {
681
+ struct kobject *parent = kobj->parent;
635682 struct kobj_type *t = get_ktype(kobj);
636683 const char *name = kobj->name;
637684
....@@ -639,21 +686,17 @@
639686 kobject_name(kobj), kobj, __func__, kobj->parent);
640687
641688 if (t && !t->release)
642
- pr_debug("kobject: '%s' (%p): does not have a release() function, it is broken and must be fixed.\n",
689
+ 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",
643690 kobject_name(kobj), kobj);
644
-
645
- /* send "remove" if the caller did not do it but sent "add" */
646
- if (kobj->state_add_uevent_sent && !kobj->state_remove_uevent_sent) {
647
- pr_debug("kobject: '%s' (%p): auto cleanup 'remove' event\n",
648
- kobject_name(kobj), kobj);
649
- kobject_uevent(kobj, KOBJ_REMOVE);
650
- }
651691
652692 /* remove from sysfs if the caller did not do it */
653693 if (kobj->state_in_sysfs) {
654694 pr_debug("kobject: '%s' (%p): auto cleanup kobject_del\n",
655695 kobject_name(kobj), kobj);
656
- kobject_del(kobj);
696
+ __kobject_del(kobj);
697
+ } else {
698
+ /* avoid dropping the parent reference unnecessarily */
699
+ parent = NULL;
657700 }
658701
659702 if (t && t->release) {
....@@ -667,6 +710,8 @@
667710 pr_debug("kobject: '%s': free name\n", name);
668711 kfree_const(name);
669712 }
713
+
714
+ kobject_put(parent);
670715 }
671716
672717 #ifdef CONFIG_DEBUG_KOBJECT_RELEASE
....@@ -693,7 +738,7 @@
693738 }
694739
695740 /**
696
- * kobject_put - decrement refcount for object.
741
+ * kobject_put() - Decrement refcount for object.
697742 * @kobj: object.
698743 *
699744 * Decrement the refcount, and if 0, call kobject_cleanup().
....@@ -722,7 +767,7 @@
722767 };
723768
724769 /**
725
- * kobject_create - create a struct kobject dynamically
770
+ * kobject_create() - Create a struct kobject dynamically.
726771 *
727772 * This function creates a kobject structure dynamically and sets it up
728773 * to be a "dynamic" kobject with a default release function set up.
....@@ -745,8 +790,8 @@
745790 }
746791
747792 /**
748
- * kobject_create_and_add - create a struct kobject dynamically and register it with sysfs
749
- *
793
+ * kobject_create_and_add() - Create a struct kobject dynamically and
794
+ * register it with sysfs.
750795 * @name: the name for the kobject
751796 * @parent: the parent kobject of this kobject, if any.
752797 *
....@@ -777,7 +822,7 @@
777822 EXPORT_SYMBOL_GPL(kobject_create_and_add);
778823
779824 /**
780
- * kset_init - initialize a kset for use
825
+ * kset_init() - Initialize a kset for use.
781826 * @k: kset
782827 */
783828 void kset_init(struct kset *k)
....@@ -819,7 +864,7 @@
819864 EXPORT_SYMBOL_GPL(kobj_sysfs_ops);
820865
821866 /**
822
- * kset_register - initialize and add a kset.
867
+ * kset_register() - Initialize and add a kset.
823868 * @k: kset.
824869 */
825870 int kset_register(struct kset *k)
....@@ -828,6 +873,11 @@
828873
829874 if (!k)
830875 return -EINVAL;
876
+
877
+ if (!k->kobj.ktype) {
878
+ pr_err("must have a ktype to be initialized properly!\n");
879
+ return -EINVAL;
880
+ }
831881
832882 kset_init(k);
833883 err = kobject_add_internal(&k->kobj);
....@@ -839,7 +889,7 @@
839889 EXPORT_SYMBOL(kset_register);
840890
841891 /**
842
- * kset_unregister - remove a kset.
892
+ * kset_unregister() - Remove a kset.
843893 * @k: kset.
844894 */
845895 void kset_unregister(struct kset *k)
....@@ -852,7 +902,7 @@
852902 EXPORT_SYMBOL(kset_unregister);
853903
854904 /**
855
- * kset_find_obj - search for object in kset.
905
+ * kset_find_obj() - Search for object in kset.
856906 * @kset: kset we're looking in.
857907 * @name: object's name.
858908 *
....@@ -887,7 +937,7 @@
887937 kfree(kset);
888938 }
889939
890
-void kset_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
940
+static void kset_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
891941 {
892942 if (kobj->parent)
893943 kobject_get_ownership(kobj->parent, uid, gid);
....@@ -900,7 +950,7 @@
900950 };
901951
902952 /**
903
- * kset_create - create a struct kset dynamically
953
+ * kset_create() - Create a struct kset dynamically.
904954 *
905955 * @name: the name for the kset
906956 * @uevent_ops: a struct kset_uevent_ops for the kset
....@@ -944,7 +994,7 @@
944994 }
945995
946996 /**
947
- * kset_create_and_add - create a struct kset dynamically and add it to sysfs
997
+ * kset_create_and_add() - Create a struct kset dynamically and add it to sysfs.
948998 *
949999 * @name: the name for the kset
9501000 * @uevent_ops: a struct kset_uevent_ops for the kset