lib/kobject.c
Source file repositories/reference/linux-study-clean/lib/kobject.c
File Facts
- System
- Linux kernel
- Corpus path
lib/kobject.c- Extension
.c- Size
- 27902 bytes
- Lines
- 1108
- Domain
- Kernel Services
- Bucket
- lib
- Inferred role
- Kernel Services: exported/initcall integration point
- Status
- integration implementation candidate
Why This File Exists
Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/kobject.hlinux/string.hlinux/export.hlinux/stat.hlinux/slab.hlinux/random.h
Detected Declarations
function Copyrightfunction kobject_get_ownershipfunction kobj_ns_type_is_validfunction create_dirfunction get_kobj_path_lengthfunction fill_kobj_pathfunction kobject_get_pathfunction kobj_kset_joinfunction kobj_kset_leavefunction kobject_init_internalfunction kobject_add_internalfunction kobject_set_name_vargsfunction kobject_set_namefunction kobject_initfunction __printffunction kobject_addfunction kobject_init_and_addfunction kobject_renamefunction kobject_movefunction __kobject_delfunction kobject_delfunction kobject_getfunction kobject_get_unless_zerofunction kobject_cleanupfunction kobject_delayed_cleanupfunction kobject_releasefunction kobject_putfunction dynamic_kobj_releasefunction kobject_createfunction kobject_create_and_addfunction kset_initfunction kobj_attr_showfunction kobj_attr_storefunction kset_registerfunction kset_unregisterfunction kset_find_objfunction list_for_each_entryfunction kset_releasefunction kset_get_ownershipfunction kset_createfunction kset_create_and_addfunction kobj_ns_type_registerfunction kobj_ns_type_registeredfunction kobj_ns_current_may_mountfunction kobj_ns_dropexport kobject_get_pathexport kobject_set_nameexport kobject_init
Annotated Snippet
if (error) {
sysfs_remove_dir(kobj);
return error;
}
}
/*
* @kobj->sd may be deleted by an ancestor going away. Hold an
* extra reference so that it stays until @kobj is gone.
*/
sysfs_get(kobj->sd);
/*
* If @kobj has ns_ops, its children need to be filtered based on
* their namespace tags. Enable namespace support on @kobj->sd.
*/
ops = kobj_child_ns_ops(kobj);
if (ops) {
BUG_ON(!kobj_ns_type_is_valid(ops->type));
BUG_ON(!kobj_ns_type_registered(ops->type));
sysfs_enable_ns(kobj->sd);
}
return 0;
}
static int get_kobj_path_length(const struct kobject *kobj)
{
int length = 1;
const struct kobject *parent = kobj;
/* walk up the ancestors until we hit the one pointing to the
* root.
* Add 1 to strlen for leading '/' of each level.
*/
do {
if (kobject_name(parent) == NULL)
return 0;
length += strlen(kobject_name(parent)) + 1;
parent = parent->parent;
} while (parent);
return length;
}
static int fill_kobj_path(const struct kobject *kobj, char *path, int length)
{
const struct kobject *parent;
--length;
for (parent = kobj; parent; parent = parent->parent) {
int cur = strlen(kobject_name(parent));
/* back up enough to print this name with '/' */
length -= cur;
if (length <= 0)
return -EINVAL;
memcpy(path + length, kobject_name(parent), cur);
*(path + --length) = '/';
}
pr_debug("'%s' (%p): %s: path = '%s'\n", kobject_name(kobj),
kobj, __func__, path);
return 0;
}
/**
* kobject_get_path() - Allocate memory and fill in the path for @kobj.
* @kobj: kobject in question, with which to build the path
* @gfp_mask: the allocation type used to allocate the path
*
* Return: The newly allocated memory, caller must free with kfree().
*/
char *kobject_get_path(const struct kobject *kobj, gfp_t gfp_mask)
{
char *path;
int len;
retry:
len = get_kobj_path_length(kobj);
if (len == 0)
return NULL;
path = kzalloc(len, gfp_mask);
if (!path)
return NULL;
if (fill_kobj_path(kobj, path, len)) {
kfree(path);
goto retry;
}
Annotation
- Immediate include surface: `linux/kobject.h`, `linux/string.h`, `linux/export.h`, `linux/stat.h`, `linux/slab.h`, `linux/random.h`.
- Detected declarations: `function Copyright`, `function kobject_get_ownership`, `function kobj_ns_type_is_valid`, `function create_dir`, `function get_kobj_path_length`, `function fill_kobj_path`, `function kobject_get_path`, `function kobj_kset_join`, `function kobj_kset_leave`, `function kobject_init_internal`.
- Atlas domain: Kernel Services / lib.
- Implementation status: integration implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.