net/devlink/sh_dev.c
Source file repositories/reference/linux-study-clean/net/devlink/sh_dev.c
File Facts
- System
- Linux kernel
- Corpus path
net/devlink/sh_dev.c- Extension
.c- Size
- 4161 bytes
- Lines
- 162
- 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.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
net/devlink.hdevl_internal.h
Detected Declarations
struct devlink_shdfunction list_for_each_entryfunction devlink_shd_destroyfunction devlink_shd_getexport devlink_shd_getexport devlink_shd_putexport devlink_shd_get_priv
Annotated Snippet
const struct device_driver *driver)
{
struct devlink_shd *shd;
struct devlink *devlink;
devlink = __devlink_alloc(ops, sizeof(struct devlink_shd) + priv_size,
&init_net, NULL, driver);
if (!devlink)
return NULL;
shd = devlink_priv(devlink);
shd->id = kstrdup(id, GFP_KERNEL);
if (!shd->id)
goto err_devlink_free;
shd->priv_size = priv_size;
refcount_set(&shd->refcount, 1);
devl_lock(devlink);
devl_register(devlink);
devl_unlock(devlink);
list_add_tail(&shd->list, &shd_list);
return shd;
err_devlink_free:
devlink_free(devlink);
return NULL;
}
static void devlink_shd_destroy(struct devlink_shd *shd)
{
struct devlink *devlink = priv_to_devlink(shd);
list_del(&shd->list);
devl_lock(devlink);
devl_unregister(devlink);
devl_unlock(devlink);
kfree(shd->id);
devlink_free(devlink);
}
/**
* devlink_shd_get - Get or create a shared devlink instance
* @id: Identifier string (e.g., serial number) for the shared instance
* @ops: Devlink operations structure
* @priv_size: Size of private data structure
* @driver: Driver associated with the shared devlink instance
*
* Get an existing shared devlink instance identified by @id, or create
* a new one if it doesn't exist. Return the devlink instance with a
* reference held. The caller must call devlink_shd_put() when done.
*
* All callers sharing the same @id must pass identical @ops, @priv_size
* and @driver. A mismatch triggers a warning and returns NULL.
*
* Return: Pointer to the shared devlink instance on success,
* NULL on failure
*/
struct devlink *devlink_shd_get(const char *id,
const struct devlink_ops *ops,
size_t priv_size,
const struct device_driver *driver)
{
struct devlink *devlink;
struct devlink_shd *shd;
mutex_lock(&shd_mutex);
shd = devlink_shd_lookup(id);
if (!shd) {
shd = devlink_shd_create(id, ops, priv_size, driver);
goto unlock;
}
devlink = priv_to_devlink(shd);
if (WARN_ON_ONCE(devlink->ops != ops ||
shd->priv_size != priv_size ||
devlink->dev_driver != driver)) {
shd = NULL;
goto unlock;
}
refcount_inc(&shd->refcount);
unlock:
mutex_unlock(&shd_mutex);
return shd ? priv_to_devlink(shd) : NULL;
}
EXPORT_SYMBOL_GPL(devlink_shd_get);
Annotation
- Immediate include surface: `net/devlink.h`, `devl_internal.h`.
- Detected declarations: `struct devlink_shd`, `function list_for_each_entry`, `function devlink_shd_destroy`, `function devlink_shd_get`, `export devlink_shd_get`, `export devlink_shd_put`, `export devlink_shd_get_priv`.
- 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.