net/devlink/core.c
Source file repositories/reference/linux-study-clean/net/devlink/core.c
File Facts
- System
- Linux kernel
- Corpus path
net/devlink/core.c- Extension
.c- Size
- 15123 bytes
- Lines
- 603
- Domain
- Networking Core
- Bucket
- Sockets, Protocols, Packet Path, And Network Policy
- Inferred role
- Networking Core: operation-table or driver-model contract
- Status
- pattern implementation candidate
Why This File Exists
Networking stack implementation surface: socket APIs, protocol dispatch, packet flow, routing, filtering, and network namespaces.
- Networking stack implementation surface: socket APIs, protocol dispatch, packet flow, routing, filtering, and network namespaces.
- Defines an operation table; this is where Linux turns generic core objects into subsystem-specific behavior.
- 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
net/genetlink.htrace/events/devlink.hdevl_internal.h
Detected Declarations
struct devlink_relfunction devlink_rel_freefunction __devlink_rel_getfunction __devlink_rel_putfunction devlink_rel_nested_in_notify_workfunction devlink_rel_nested_in_notify_work_schedulefunction devlink_rel_putfunction devlink_rel_nested_in_clearfunction devlink_rel_nested_in_addfunction devlink_rel_nested_in_notifyfunction devlink_rel_devlink_handle_putfunction devl_assert_lockedfunction devl_lock_is_heldfunction devl_lockfunction devl_trylockfunction devl_unlockfunction devlink_try_getfunction devlink_releasefunction devlink_putfunction devl_registerfunction devlink_registerfunction devl_unregisterfunction devlink_unregisterfunction devlink_freefunction devlink_pernet_pre_exitfunction devlink_initmodule init devlink_initexport devlink_privexport priv_to_devlinkexport devlink_to_devexport devlink_bus_nameexport devlink_dev_nameexport devlink_dev_driver_nameexport devlink_netexport devl_assert_lockedexport devl_lock_is_heldexport devl_lockexport devl_trylockexport devl_unlockexport devl_registerexport devlink_registerexport devl_unregisterexport devlink_unregisterexport devlink_alloc_nsexport devlink_free
Annotated Snippet
const struct device_driver *dev_driver)
{
struct devlink *devlink;
static u32 last_id;
int ret;
WARN_ON(!ops || !dev_driver);
if (!devlink_reload_actions_valid(ops))
return NULL;
devlink = kvzalloc_flex(*devlink, priv, priv_size);
if (!devlink)
return NULL;
ret = xa_alloc_cyclic(&devlinks, &devlink->index, devlink, xa_limit_31b,
&last_id, GFP_KERNEL);
if (ret < 0)
goto err_xa_alloc;
if (dev) {
devlink->dev = get_device(dev);
} else {
devlink->dev_name_index = kasprintf(GFP_KERNEL, "%u", devlink->index);
if (!devlink->dev_name_index)
goto err_kasprintf;
}
devlink->ops = ops;
devlink->dev_driver = dev_driver;
xa_init_flags(&devlink->ports, XA_FLAGS_ALLOC);
xa_init_flags(&devlink->params, XA_FLAGS_ALLOC);
xa_init_flags(&devlink->snapshot_ids, XA_FLAGS_ALLOC);
xa_init_flags(&devlink->nested_rels, XA_FLAGS_ALLOC);
write_pnet(&devlink->_net, net);
INIT_LIST_HEAD(&devlink->rate_list);
INIT_LIST_HEAD(&devlink->linecard_list);
INIT_LIST_HEAD(&devlink->sb_list);
INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list);
INIT_LIST_HEAD(&devlink->resource_list);
INIT_LIST_HEAD(&devlink->region_list);
INIT_LIST_HEAD(&devlink->reporter_list);
INIT_LIST_HEAD(&devlink->trap_list);
INIT_LIST_HEAD(&devlink->trap_group_list);
INIT_LIST_HEAD(&devlink->trap_policer_list);
INIT_RCU_WORK(&devlink->rwork, devlink_release);
lockdep_register_key(&devlink->lock_key);
mutex_init(&devlink->lock);
lockdep_set_class(&devlink->lock, &devlink->lock_key);
refcount_set(&devlink->refcount, 1);
return devlink;
err_kasprintf:
xa_erase(&devlinks, devlink->index);
err_xa_alloc:
kvfree(devlink);
return NULL;
}
/**
* devlink_alloc_ns - Allocate new devlink instance resources
* in specific namespace
*
* @ops: ops
* @priv_size: size of user private data
* @net: net namespace
* @dev: parent device
*
* Allocate new devlink instance resources, including devlink index
* and name.
*/
struct devlink *devlink_alloc_ns(const struct devlink_ops *ops,
size_t priv_size, struct net *net,
struct device *dev)
{
WARN_ON(!dev);
return __devlink_alloc(ops, priv_size, net, dev, dev->driver);
}
EXPORT_SYMBOL_GPL(devlink_alloc_ns);
/**
* devlink_free - Free devlink instance resources
*
* @devlink: devlink
*/
void devlink_free(struct devlink *devlink)
{
ASSERT_DEVLINK_NOT_REGISTERED(devlink);
devlink_rel_put(devlink);
Annotation
- Immediate include surface: `net/genetlink.h`, `trace/events/devlink.h`, `devl_internal.h`.
- Detected declarations: `struct devlink_rel`, `function devlink_rel_free`, `function __devlink_rel_get`, `function __devlink_rel_put`, `function devlink_rel_nested_in_notify_work`, `function devlink_rel_nested_in_notify_work_schedule`, `function devlink_rel_put`, `function devlink_rel_nested_in_clear`, `function devlink_rel_nested_in_add`, `function devlink_rel_nested_in_notify`.
- Atlas domain: Networking Core / Sockets, Protocols, Packet Path, And Network Policy.
- Implementation status: pattern 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.