2017-11-07 16:30:05 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* kobject.c - library routines for handling generic kernel objects
|
|
|
|
*
|
|
|
|
* Copyright (c) 2002-2003 Patrick Mochel <mochel@osdl.org>
|
2007-09-27 21:48:53 +00:00
|
|
|
* Copyright (c) 2006-2007 Greg Kroah-Hartman <greg@kroah.com>
|
|
|
|
* Copyright (c) 2006-2007 Novell Inc.
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
2020-04-14 16:48:37 +00:00
|
|
|
* Please see the file Documentation/core-api/kobject.rst for critical information
|
2005-04-16 22:20:36 +00:00
|
|
|
* about using the kobject interface.
|
|
|
|
*/
|
|
|
|
|
2023-03-11 03:14:46 +00:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/kobject.h>
|
|
|
|
#include <linux/string.h>
|
2011-11-17 02:29:17 +00:00
|
|
|
#include <linux/export.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/stat.h>
|
2005-10-30 23:03:48 +00:00
|
|
|
#include <linux/slab.h>
|
2013-12-06 00:37:51 +00:00
|
|
|
#include <linux/random.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2013-09-12 02:29:05 +00:00
|
|
|
/**
|
2019-05-02 02:31:40 +00:00
|
|
|
* kobject_namespace() - Return @kobj's namespace tag.
|
2013-09-12 02:29:05 +00:00
|
|
|
* @kobj: kobject in question
|
|
|
|
*
|
|
|
|
* Returns namespace tag of @kobj if its parent has namespace ops enabled
|
|
|
|
* and thus @kobj should have a namespace tag associated with it. Returns
|
|
|
|
* %NULL otherwise.
|
|
|
|
*/
|
2022-11-21 09:46:46 +00:00
|
|
|
const void *kobject_namespace(const struct kobject *kobj)
|
2013-09-12 02:29:05 +00:00
|
|
|
{
|
|
|
|
const struct kobj_ns_type_operations *ns_ops = kobj_ns_ops(kobj);
|
|
|
|
|
|
|
|
if (!ns_ops || ns_ops->type == KOBJ_NS_TYPE_NONE)
|
|
|
|
return NULL;
|
|
|
|
|
2013-11-07 11:47:28 +00:00
|
|
|
return kobj->ktype->namespace(kobj);
|
2013-09-12 02:29:05 +00:00
|
|
|
}
|
|
|
|
|
2018-07-20 21:56:48 +00:00
|
|
|
/**
|
2019-05-02 02:31:40 +00:00
|
|
|
* kobject_get_ownership() - Get sysfs ownership data for @kobj.
|
2018-07-20 21:56:48 +00:00
|
|
|
* @kobj: kobject in question
|
|
|
|
* @uid: kernel user ID for sysfs objects
|
|
|
|
* @gid: kernel group ID for sysfs objects
|
|
|
|
*
|
|
|
|
* Returns initial uid/gid pair that should be used when creating sysfs
|
|
|
|
* representation of given kobject. Normally used to adjust ownership of
|
|
|
|
* objects in a container.
|
|
|
|
*/
|
2022-11-21 09:46:45 +00:00
|
|
|
void kobject_get_ownership(const struct kobject *kobj, kuid_t *uid, kgid_t *gid)
|
2018-07-20 21:56:48 +00:00
|
|
|
{
|
|
|
|
*uid = GLOBAL_ROOT_UID;
|
|
|
|
*gid = GLOBAL_ROOT_GID;
|
|
|
|
|
|
|
|
if (kobj->ktype->get_ownership)
|
|
|
|
kobj->ktype->get_ownership(kobj, uid, gid);
|
|
|
|
}
|
|
|
|
|
2023-07-26 06:25:08 +00:00
|
|
|
static bool kobj_ns_type_is_valid(enum kobj_ns_type type)
|
|
|
|
{
|
|
|
|
if ((type <= KOBJ_NS_TYPE_NONE) || (type >= KOBJ_NS_TYPES))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-01-25 05:59:04 +00:00
|
|
|
static int create_dir(struct kobject *kobj)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2019-04-02 02:51:18 +00:00
|
|
|
const struct kobj_type *ktype = get_ktype(kobj);
|
2013-11-23 23:01:46 +00:00
|
|
|
const struct kobj_ns_type_operations *ops;
|
2013-09-12 02:29:05 +00:00
|
|
|
int error;
|
|
|
|
|
|
|
|
error = sysfs_create_dir_ns(kobj, kobject_namespace(kobj));
|
2013-11-23 23:01:46 +00:00
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
|
2023-08-05 08:41:14 +00:00
|
|
|
error = sysfs_create_groups(kobj, ktype->default_groups);
|
|
|
|
if (error) {
|
|
|
|
sysfs_remove_dir(kobj);
|
|
|
|
return error;
|
2019-04-02 02:51:18 +00:00
|
|
|
}
|
|
|
|
|
2013-09-18 21:15:36 +00:00
|
|
|
/*
|
|
|
|
* @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);
|
|
|
|
|
2013-11-23 23:01:46 +00:00
|
|
|
/*
|
|
|
|
* 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) {
|
2023-07-26 06:25:08 +00:00
|
|
|
BUG_ON(!kobj_ns_type_is_valid(ops->type));
|
2013-11-23 23:01:46 +00:00
|
|
|
BUG_ON(!kobj_ns_type_registered(ops->type));
|
|
|
|
|
2014-02-07 18:32:07 +00:00
|
|
|
sysfs_enable_ns(kobj->sd);
|
2013-11-23 23:01:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2022-10-01 16:53:15 +00:00
|
|
|
static int get_kobj_path_length(const struct kobject *kobj)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
int length = 1;
|
2022-10-01 16:53:15 +00:00
|
|
|
const struct kobject *parent = kobj;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-01-25 05:59:04 +00:00
|
|
|
/* walk up the ancestors until we hit the one pointing to the
|
2005-04-16 22:20:36 +00:00
|
|
|
* root.
|
|
|
|
* Add 1 to strlen for leading '/' of each level.
|
|
|
|
*/
|
|
|
|
do {
|
2006-01-13 01:02:00 +00:00
|
|
|
if (kobject_name(parent) == NULL)
|
|
|
|
return 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
length += strlen(kobject_name(parent)) + 1;
|
|
|
|
parent = parent->parent;
|
|
|
|
} while (parent);
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2022-12-20 01:21:43 +00:00
|
|
|
static int fill_kobj_path(const struct kobject *kobj, char *path, int length)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2022-10-01 16:53:15 +00:00
|
|
|
const struct kobject *parent;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
--length;
|
|
|
|
for (parent = kobj; parent; parent = parent->parent) {
|
|
|
|
int cur = strlen(kobject_name(parent));
|
|
|
|
/* back up enough to print this name with '/' */
|
|
|
|
length -= cur;
|
2022-12-20 01:21:43 +00:00
|
|
|
if (length <= 0)
|
|
|
|
return -EINVAL;
|
2018-07-01 20:57:16 +00:00
|
|
|
memcpy(path + length, kobject_name(parent), cur);
|
2005-04-16 22:20:36 +00:00
|
|
|
*(path + --length) = '/';
|
|
|
|
}
|
|
|
|
|
2023-03-11 03:14:46 +00:00
|
|
|
pr_debug("'%s' (%p): %s: path = '%s'\n", kobject_name(kobj),
|
2008-04-30 07:55:08 +00:00
|
|
|
kobj, __func__, path);
|
2022-12-20 01:21:43 +00:00
|
|
|
|
|
|
|
return 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-02 02:31:39 +00:00
|
|
|
* kobject_get_path() - Allocate memory and fill in the path for @kobj.
|
2005-04-16 22:20:36 +00:00
|
|
|
* @kobj: kobject in question, with which to build the path
|
|
|
|
* @gfp_mask: the allocation type used to allocate the path
|
2007-02-10 09:45:59 +00:00
|
|
|
*
|
2019-05-02 02:31:39 +00:00
|
|
|
* Return: The newly allocated memory, caller must free with kfree().
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2022-10-01 16:53:15 +00:00
|
|
|
char *kobject_get_path(const struct kobject *kobj, gfp_t gfp_mask)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
int len;
|
|
|
|
|
2022-12-20 01:21:43 +00:00
|
|
|
retry:
|
2005-04-16 22:20:36 +00:00
|
|
|
len = get_kobj_path_length(kobj);
|
2006-01-13 01:02:00 +00:00
|
|
|
if (len == 0)
|
|
|
|
return NULL;
|
2006-12-07 04:38:51 +00:00
|
|
|
path = kzalloc(len, gfp_mask);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (!path)
|
|
|
|
return NULL;
|
2022-12-20 01:21:43 +00:00
|
|
|
if (fill_kobj_path(kobj, path, len)) {
|
|
|
|
kfree(path);
|
|
|
|
goto retry;
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
2006-10-11 05:43:58 +00:00
|
|
|
EXPORT_SYMBOL_GPL(kobject_get_path);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2007-12-19 00:40:42 +00:00
|
|
|
/* add the kobject to its kset's list */
|
|
|
|
static void kobj_kset_join(struct kobject *kobj)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2007-12-19 00:40:42 +00:00
|
|
|
if (!kobj->kset)
|
2007-01-18 20:23:51 +00:00
|
|
|
return;
|
2007-12-19 00:40:42 +00:00
|
|
|
|
|
|
|
kset_get(kobj->kset);
|
|
|
|
spin_lock(&kobj->kset->list_lock);
|
|
|
|
list_add_tail(&kobj->entry, &kobj->kset->list);
|
|
|
|
spin_unlock(&kobj->kset->list_lock);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2007-12-19 00:40:42 +00:00
|
|
|
/* remove the kobject from its kset's list */
|
|
|
|
static void kobj_kset_leave(struct kobject *kobj)
|
|
|
|
{
|
|
|
|
if (!kobj->kset)
|
|
|
|
return;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2007-12-19 00:40:42 +00:00
|
|
|
spin_lock(&kobj->kset->list_lock);
|
|
|
|
list_del_init(&kobj->entry);
|
|
|
|
spin_unlock(&kobj->kset->list_lock);
|
|
|
|
kset_put(kobj->kset);
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-01-25 05:59:04 +00:00
|
|
|
static void kobject_init_internal(struct kobject *kobj)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2007-12-19 00:40:42 +00:00
|
|
|
if (!kobj)
|
|
|
|
return;
|
|
|
|
kref_init(&kobj->kref);
|
|
|
|
INIT_LIST_HEAD(&kobj->entry);
|
2008-02-26 17:36:38 +00:00
|
|
|
kobj->state_in_sysfs = 0;
|
|
|
|
kobj->state_add_uevent_sent = 0;
|
|
|
|
kobj->state_remove_uevent_sent = 0;
|
|
|
|
kobj->state_initialized = 1;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2007-12-19 00:40:42 +00:00
|
|
|
|
2007-12-18 06:05:35 +00:00
|
|
|
static int kobject_add_internal(struct kobject *kobj)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
int error = 0;
|
2008-01-25 05:59:04 +00:00
|
|
|
struct kobject *parent;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2007-12-19 00:40:42 +00:00
|
|
|
if (!kobj)
|
2005-04-16 22:20:36 +00:00
|
|
|
return -ENOENT;
|
2007-12-19 00:40:42 +00:00
|
|
|
|
2007-12-20 01:09:39 +00:00
|
|
|
if (!kobj->name || !kobj->name[0]) {
|
2018-03-15 13:23:43 +00:00
|
|
|
WARN(1,
|
|
|
|
"kobject: (%p): attempted to be registered with empty name!\n",
|
|
|
|
kobj);
|
2006-01-20 22:08:59 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2007-12-19 00:40:42 +00:00
|
|
|
parent = kobject_get(kobj->parent);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2007-12-19 00:40:42 +00:00
|
|
|
/* join kset if set, use it as parent if we do not already have one */
|
2005-04-16 22:20:36 +00:00
|
|
|
if (kobj->kset) {
|
2007-12-19 00:40:42 +00:00
|
|
|
if (!parent)
|
2005-04-16 22:20:36 +00:00
|
|
|
parent = kobject_get(&kobj->kset->kobj);
|
2007-12-19 00:40:42 +00:00
|
|
|
kobj_kset_join(kobj);
|
2007-03-10 11:00:10 +00:00
|
|
|
kobj->parent = parent;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2023-03-11 03:14:46 +00:00
|
|
|
pr_debug("'%s' (%p): %s: parent: '%s', set: '%s'\n",
|
2008-04-30 07:55:08 +00:00
|
|
|
kobject_name(kobj), kobj, __func__,
|
2007-12-19 00:40:42 +00:00
|
|
|
parent ? kobject_name(parent) : "<NULL>",
|
2008-01-25 05:59:04 +00:00
|
|
|
kobj->kset ? kobject_name(&kobj->kset->kobj) : "<NULL>");
|
2007-12-19 00:40:42 +00:00
|
|
|
|
2007-07-31 10:15:08 +00:00
|
|
|
error = create_dir(kobj);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (error) {
|
2007-12-19 00:40:42 +00:00
|
|
|
kobj_kset_leave(kobj);
|
|
|
|
kobject_put(parent);
|
|
|
|
kobj->parent = NULL;
|
2006-03-20 21:17:13 +00:00
|
|
|
|
|
|
|
/* be noisy on error issues */
|
|
|
|
if (error == -EEXIST)
|
2018-04-11 15:22:43 +00:00
|
|
|
pr_err("%s failed for %s with -EEXIST, don't try to register things with the same name in the same directory.\n",
|
|
|
|
__func__, kobject_name(kobj));
|
2006-03-20 21:17:13 +00:00
|
|
|
else
|
2018-04-11 15:22:43 +00:00
|
|
|
pr_err("%s failed for %s (error: %d parent: %s)\n",
|
|
|
|
__func__, kobject_name(kobj), error,
|
|
|
|
parent ? kobject_name(parent) : "'none'");
|
2007-12-19 00:40:42 +00:00
|
|
|
} else
|
|
|
|
kobj->state_in_sysfs = 1;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2007-11-29 23:32:47 +00:00
|
|
|
/**
|
2019-05-02 02:31:40 +00:00
|
|
|
* kobject_set_name_vargs() - Set the name of a kobject.
|
2007-11-29 23:32:47 +00:00
|
|
|
* @kobj: struct kobject to set the name of
|
|
|
|
* @fmt: format string used to build the name
|
|
|
|
* @vargs: vargs to format the string.
|
|
|
|
*/
|
2009-01-25 14:17:37 +00:00
|
|
|
int kobject_set_name_vargs(struct kobject *kobj, const char *fmt,
|
2007-11-29 23:32:47 +00:00
|
|
|
va_list vargs)
|
|
|
|
{
|
2015-11-07 00:31:23 +00:00
|
|
|
const char *s;
|
2007-11-29 23:32:47 +00:00
|
|
|
|
2009-04-18 22:05:45 +00:00
|
|
|
if (kobj->name && !fmt)
|
|
|
|
return 0;
|
|
|
|
|
2015-11-07 00:31:23 +00:00
|
|
|
s = kvasprintf_const(GFP_KERNEL, fmt, vargs);
|
2015-06-25 22:02:30 +00:00
|
|
|
if (!s)
|
2008-04-30 00:06:29 +00:00
|
|
|
return -ENOMEM;
|
2007-11-29 23:32:47 +00:00
|
|
|
|
2015-11-07 00:31:23 +00:00
|
|
|
/*
|
|
|
|
* ewww... some of these buggers have '/' in the name ... If
|
|
|
|
* that's the case, we need to make sure we have an actual
|
|
|
|
* allocated copy to modify, since kvasprintf_const may have
|
|
|
|
* returned something from .rodata.
|
|
|
|
*/
|
|
|
|
if (strchr(s, '/')) {
|
|
|
|
char *t;
|
|
|
|
|
|
|
|
t = kstrdup(s, GFP_KERNEL);
|
|
|
|
kfree_const(s);
|
|
|
|
if (!t)
|
|
|
|
return -ENOMEM;
|
2023-06-05 17:05:53 +00:00
|
|
|
s = strreplace(t, '/', '!');
|
2015-11-07 00:31:23 +00:00
|
|
|
}
|
|
|
|
kfree_const(kobj->name);
|
2015-06-25 22:02:30 +00:00
|
|
|
kobj->name = s;
|
2008-05-06 20:24:04 +00:00
|
|
|
|
2007-11-29 23:32:47 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/**
|
2019-05-02 02:31:40 +00:00
|
|
|
* kobject_set_name() - Set the name of a kobject.
|
2007-11-29 23:32:47 +00:00
|
|
|
* @kobj: struct kobject to set the name of
|
2007-12-04 06:45:47 +00:00
|
|
|
* @fmt: format string used to build the name
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
2007-12-04 06:45:47 +00:00
|
|
|
* This sets the name of the kobject. If you have already added the
|
|
|
|
* kobject to the system, you must call kobject_rename() in order to
|
|
|
|
* change the name of the kobject.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2007-11-29 23:32:47 +00:00
|
|
|
int kobject_set_name(struct kobject *kobj, const char *fmt, ...)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-04-30 00:06:29 +00:00
|
|
|
va_list vargs;
|
2007-11-29 23:32:47 +00:00
|
|
|
int retval;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-04-30 00:06:29 +00:00
|
|
|
va_start(vargs, fmt);
|
|
|
|
retval = kobject_set_name_vargs(kobj, fmt, vargs);
|
|
|
|
va_end(vargs);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2007-11-29 23:32:47 +00:00
|
|
|
return retval;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(kobject_set_name);
|
|
|
|
|
2007-12-04 05:31:08 +00:00
|
|
|
/**
|
2019-05-02 02:31:40 +00:00
|
|
|
* kobject_init() - Initialize a kobject structure.
|
2007-12-04 05:31:08 +00:00
|
|
|
* @kobj: pointer to the kobject to initialize
|
|
|
|
* @ktype: pointer to the ktype for this kobject.
|
|
|
|
*
|
|
|
|
* This function will properly initialize a kobject such that it can then
|
|
|
|
* be passed to the kobject_add() call.
|
|
|
|
*
|
|
|
|
* After this function is called, the kobject MUST be cleaned up by a call
|
|
|
|
* to kobject_put(), not by a call to kfree directly to ensure that all of
|
|
|
|
* the memory is cleaned up properly.
|
|
|
|
*/
|
2021-12-24 23:13:45 +00:00
|
|
|
void kobject_init(struct kobject *kobj, const struct kobj_type *ktype)
|
2007-12-04 05:31:08 +00:00
|
|
|
{
|
|
|
|
char *err_str;
|
|
|
|
|
|
|
|
if (!kobj) {
|
|
|
|
err_str = "invalid kobject pointer!";
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (!ktype) {
|
|
|
|
err_str = "must have a ktype to be initialized properly!\n";
|
|
|
|
goto error;
|
|
|
|
}
|
2007-12-19 00:40:42 +00:00
|
|
|
if (kobj->state_initialized) {
|
2007-12-04 05:31:08 +00:00
|
|
|
/* do not error out as sometimes we can recover */
|
2018-03-15 13:23:43 +00:00
|
|
|
pr_err("kobject (%p): tried to init an initialized object, something is seriously wrong.\n",
|
|
|
|
kobj);
|
2023-03-11 03:14:47 +00:00
|
|
|
dump_stack_lvl(KERN_ERR);
|
2007-12-04 05:31:08 +00:00
|
|
|
}
|
|
|
|
|
2008-02-26 17:36:38 +00:00
|
|
|
kobject_init_internal(kobj);
|
2007-12-04 05:31:08 +00:00
|
|
|
kobj->ktype = ktype;
|
|
|
|
return;
|
|
|
|
|
|
|
|
error:
|
2018-03-15 13:23:43 +00:00
|
|
|
pr_err("kobject (%p): %s\n", kobj, err_str);
|
2023-03-11 03:14:47 +00:00
|
|
|
dump_stack_lvl(KERN_ERR);
|
2007-12-04 05:31:08 +00:00
|
|
|
}
|
2007-12-18 06:05:35 +00:00
|
|
|
EXPORT_SYMBOL(kobject_init);
|
2007-12-04 05:31:08 +00:00
|
|
|
|
2015-07-17 23:23:42 +00:00
|
|
|
static __printf(3, 0) int kobject_add_varg(struct kobject *kobj,
|
|
|
|
struct kobject *parent,
|
|
|
|
const char *fmt, va_list vargs)
|
2007-12-04 05:31:08 +00:00
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
|
2008-04-30 00:06:29 +00:00
|
|
|
retval = kobject_set_name_vargs(kobj, fmt, vargs);
|
2007-12-04 05:31:08 +00:00
|
|
|
if (retval) {
|
2023-03-11 03:14:46 +00:00
|
|
|
pr_err("can not set name properly!\n");
|
2007-12-04 05:31:08 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
kobj->parent = parent;
|
2007-12-18 06:05:35 +00:00
|
|
|
return kobject_add_internal(kobj);
|
2007-12-04 05:31:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-02 02:31:40 +00:00
|
|
|
* kobject_add() - The main kobject add function.
|
2007-12-04 05:31:08 +00:00
|
|
|
* @kobj: the kobject to add
|
|
|
|
* @parent: pointer to the parent of the kobject.
|
|
|
|
* @fmt: format to name the kobject with.
|
|
|
|
*
|
|
|
|
* The kobject name is set and added to the kobject hierarchy in this
|
|
|
|
* function.
|
|
|
|
*
|
|
|
|
* If @parent is set, then the parent of the @kobj will be set to it.
|
|
|
|
* If @parent is NULL, then the parent of the @kobj will be set to the
|
2014-01-04 13:20:04 +00:00
|
|
|
* kobject associated with the kset assigned to this kobject. If no kset
|
2007-12-04 05:31:08 +00:00
|
|
|
* is assigned to the kobject, then the kobject will be located in the
|
|
|
|
* root of the sysfs tree.
|
|
|
|
*
|
2007-12-19 00:40:42 +00:00
|
|
|
* Note, no "add" uevent will be created with this call, the caller should set
|
2007-12-04 05:31:08 +00:00
|
|
|
* up all of the necessary sysfs files for the object and then call
|
|
|
|
* kobject_uevent() with the UEVENT_ADD parameter to ensure that
|
|
|
|
* userspace is properly notified of this kobject's creation.
|
2019-04-28 00:48:10 +00:00
|
|
|
*
|
|
|
|
* Return: If this function returns an error, kobject_put() must be
|
|
|
|
* called to properly clean up the memory associated with the
|
|
|
|
* object. Under no instance should the kobject that is passed
|
|
|
|
* to this function be directly freed with a call to kfree(),
|
|
|
|
* that can leak memory.
|
|
|
|
*
|
2019-05-02 10:22:24 +00:00
|
|
|
* If this function returns success, kobject_put() must also be called
|
|
|
|
* in order to properly clean up the memory associated with the object.
|
|
|
|
*
|
|
|
|
* In short, once this function is called, kobject_put() MUST be called
|
|
|
|
* when the use of the object is finished in order to properly free
|
|
|
|
* everything.
|
2007-12-04 05:31:08 +00:00
|
|
|
*/
|
2007-12-18 06:05:35 +00:00
|
|
|
int kobject_add(struct kobject *kobj, struct kobject *parent,
|
|
|
|
const char *fmt, ...)
|
2007-12-04 05:31:08 +00:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
if (!kobj)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2007-12-19 00:40:42 +00:00
|
|
|
if (!kobj->state_initialized) {
|
2018-03-15 13:23:43 +00:00
|
|
|
pr_err("kobject '%s' (%p): tried to add an uninitialized object, something is seriously wrong.\n",
|
2007-12-19 00:40:42 +00:00
|
|
|
kobject_name(kobj), kobj);
|
2023-03-11 03:14:47 +00:00
|
|
|
dump_stack_lvl(KERN_ERR);
|
2007-12-19 00:40:42 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2007-12-04 05:31:08 +00:00
|
|
|
va_start(args, fmt);
|
|
|
|
retval = kobject_add_varg(kobj, parent, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
2007-12-18 06:05:35 +00:00
|
|
|
EXPORT_SYMBOL(kobject_add);
|
2007-12-04 05:31:08 +00:00
|
|
|
|
2007-12-04 05:31:08 +00:00
|
|
|
/**
|
2019-05-02 02:31:40 +00:00
|
|
|
* kobject_init_and_add() - Initialize a kobject structure and add it to
|
|
|
|
* the kobject hierarchy.
|
2007-12-04 05:31:08 +00:00
|
|
|
* @kobj: pointer to the kobject to initialize
|
|
|
|
* @ktype: pointer to the ktype for this kobject.
|
|
|
|
* @parent: pointer to the parent of this kobject.
|
|
|
|
* @fmt: the name of the kobject.
|
|
|
|
*
|
2019-04-27 23:56:52 +00:00
|
|
|
* This function combines the call to kobject_init() and kobject_add().
|
|
|
|
*
|
|
|
|
* If this function returns an error, kobject_put() must be called to
|
|
|
|
* properly clean up the memory associated with the object. This is the
|
|
|
|
* same type of error handling after a call to kobject_add() and kobject
|
|
|
|
* lifetime rules are the same here.
|
2007-12-04 05:31:08 +00:00
|
|
|
*/
|
2021-12-24 23:13:45 +00:00
|
|
|
int kobject_init_and_add(struct kobject *kobj, const struct kobj_type *ktype,
|
2007-12-04 05:31:08 +00:00
|
|
|
struct kobject *parent, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
int retval;
|
|
|
|
|
2007-12-18 06:05:35 +00:00
|
|
|
kobject_init(kobj, ktype);
|
2007-12-04 05:31:08 +00:00
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
retval = kobject_add_varg(kobj, parent, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(kobject_init_and_add);
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/**
|
2019-05-02 02:31:40 +00:00
|
|
|
* kobject_rename() - Change the name of an object.
|
2008-01-25 05:59:04 +00:00
|
|
|
* @kobj: object in question.
|
|
|
|
* @new_name: object's new name
|
2008-05-08 21:41:00 +00:00
|
|
|
*
|
|
|
|
* It is the responsibility of the caller to provide mutual
|
|
|
|
* exclusion between two different calls of kobject_rename
|
|
|
|
* on the same kobject and to ensure that new_name is valid and
|
|
|
|
* won't conflict with other kobjects.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2008-01-25 05:59:04 +00:00
|
|
|
int kobject_rename(struct kobject *kobj, const char *new_name)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
int error = 0;
|
2007-03-07 18:49:30 +00:00
|
|
|
const char *devpath = NULL;
|
2008-07-04 01:05:28 +00:00
|
|
|
const char *dup_name = NULL, *name;
|
2007-03-07 18:49:30 +00:00
|
|
|
char *devpath_string = NULL;
|
|
|
|
char *envp[2];
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
kobj = kobject_get(kobj);
|
|
|
|
if (!kobj)
|
|
|
|
return -EINVAL;
|
2019-06-03 08:08:10 +00:00
|
|
|
if (!kobj->parent) {
|
|
|
|
kobject_put(kobj);
|
2007-01-24 19:35:52 +00:00
|
|
|
return -EINVAL;
|
2019-06-03 08:08:10 +00:00
|
|
|
}
|
2007-03-07 18:49:30 +00:00
|
|
|
|
|
|
|
devpath = kobject_get_path(kobj, GFP_KERNEL);
|
|
|
|
if (!devpath) {
|
|
|
|
error = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
|
|
|
|
if (!devpath_string) {
|
|
|
|
error = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
|
|
|
|
envp[0] = devpath_string;
|
|
|
|
envp[1] = NULL;
|
|
|
|
|
2015-11-07 00:31:23 +00:00
|
|
|
name = dup_name = kstrdup_const(new_name, GFP_KERNEL);
|
2008-07-04 01:05:28 +00:00
|
|
|
if (!name) {
|
|
|
|
error = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2013-09-12 02:29:05 +00:00
|
|
|
error = sysfs_rename_dir_ns(kobj, new_name, kobject_namespace(kobj));
|
2008-07-04 01:05:28 +00:00
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* Install the new kobject name */
|
|
|
|
dup_name = kobj->name;
|
|
|
|
kobj->name = name;
|
2007-03-07 18:49:30 +00:00
|
|
|
|
|
|
|
/* This function is mostly/only used for network interface.
|
|
|
|
* Some hotplug package track interfaces by their name and
|
|
|
|
* therefore want to know when the name is changed by the user. */
|
2008-07-04 01:05:28 +00:00
|
|
|
kobject_uevent_env(kobj, KOBJ_MOVE, envp);
|
2007-03-07 18:49:30 +00:00
|
|
|
|
|
|
|
out:
|
2015-11-07 00:31:23 +00:00
|
|
|
kfree_const(dup_name);
|
2007-03-07 18:49:30 +00:00
|
|
|
kfree(devpath_string);
|
|
|
|
kfree(devpath);
|
2007-01-24 19:35:52 +00:00
|
|
|
kobject_put(kobj);
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
2008-06-10 21:30:42 +00:00
|
|
|
EXPORT_SYMBOL_GPL(kobject_rename);
|
2007-01-24 19:35:52 +00:00
|
|
|
|
2006-11-20 16:07:51 +00:00
|
|
|
/**
|
2019-05-02 02:31:40 +00:00
|
|
|
* kobject_move() - Move object to another parent.
|
2008-01-25 05:59:04 +00:00
|
|
|
* @kobj: object in question.
|
|
|
|
* @new_parent: object's new parent (can be NULL)
|
2006-11-20 16:07:51 +00:00
|
|
|
*/
|
|
|
|
int kobject_move(struct kobject *kobj, struct kobject *new_parent)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
struct kobject *old_parent;
|
|
|
|
const char *devpath = NULL;
|
|
|
|
char *devpath_string = NULL;
|
|
|
|
char *envp[2];
|
|
|
|
|
|
|
|
kobj = kobject_get(kobj);
|
|
|
|
if (!kobj)
|
|
|
|
return -EINVAL;
|
|
|
|
new_parent = kobject_get(new_parent);
|
|
|
|
if (!new_parent) {
|
2007-01-08 19:16:44 +00:00
|
|
|
if (kobj->kset)
|
|
|
|
new_parent = kobject_get(&kobj->kset->kobj);
|
2006-11-20 16:07:51 +00:00
|
|
|
}
|
2013-09-12 02:29:05 +00:00
|
|
|
|
2006-11-20 16:07:51 +00:00
|
|
|
/* old object path */
|
|
|
|
devpath = kobject_get_path(kobj, GFP_KERNEL);
|
|
|
|
if (!devpath) {
|
|
|
|
error = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
|
|
|
|
if (!devpath_string) {
|
|
|
|
error = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
|
|
|
|
envp[0] = devpath_string;
|
|
|
|
envp[1] = NULL;
|
2013-09-12 02:29:05 +00:00
|
|
|
error = sysfs_move_dir_ns(kobj, new_parent, kobject_namespace(kobj));
|
2006-11-20 16:07:51 +00:00
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
old_parent = kobj->parent;
|
|
|
|
kobj->parent = new_parent;
|
2007-03-03 13:11:21 +00:00
|
|
|
new_parent = NULL;
|
2006-11-20 16:07:51 +00:00
|
|
|
kobject_put(old_parent);
|
|
|
|
kobject_uevent_env(kobj, KOBJ_MOVE, envp);
|
|
|
|
out:
|
2007-03-03 13:11:21 +00:00
|
|
|
kobject_put(new_parent);
|
2006-11-20 16:07:51 +00:00
|
|
|
kobject_put(kobj);
|
|
|
|
kfree(devpath_string);
|
|
|
|
kfree(devpath);
|
|
|
|
return error;
|
|
|
|
}
|
2015-02-11 23:03:37 +00:00
|
|
|
EXPORT_SYMBOL_GPL(kobject_move);
|
2006-11-20 16:07:51 +00:00
|
|
|
|
2020-06-04 17:46:46 +00:00
|
|
|
static void __kobject_del(struct kobject *kobj)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2013-12-11 19:11:53 +00:00
|
|
|
struct kernfs_node *sd;
|
2019-05-01 12:43:17 +00:00
|
|
|
const struct kobj_type *ktype;
|
2013-09-18 21:15:36 +00:00
|
|
|
|
|
|
|
sd = kobj->sd;
|
2019-05-01 12:43:17 +00:00
|
|
|
ktype = get_ktype(kobj);
|
2019-04-02 02:51:18 +00:00
|
|
|
|
2023-08-05 08:41:14 +00:00
|
|
|
sysfs_remove_groups(kobj, ktype->default_groups);
|
2019-04-02 02:51:18 +00:00
|
|
|
|
2020-05-24 15:30:41 +00:00
|
|
|
/* send "remove" if the caller did not do it but sent "add" */
|
|
|
|
if (kobj->state_add_uevent_sent && !kobj->state_remove_uevent_sent) {
|
2023-03-11 03:14:46 +00:00
|
|
|
pr_debug("'%s' (%p): auto cleanup 'remove' event\n",
|
2020-05-24 15:30:41 +00:00
|
|
|
kobject_name(kobj), kobj);
|
|
|
|
kobject_uevent(kobj, KOBJ_REMOVE);
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
sysfs_remove_dir(kobj);
|
2013-09-18 21:15:36 +00:00
|
|
|
sysfs_put(sd);
|
|
|
|
|
2007-12-19 00:40:42 +00:00
|
|
|
kobj->state_in_sysfs = 0;
|
|
|
|
kobj_kset_leave(kobj);
|
|
|
|
kobj->parent = NULL;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2020-06-04 17:46:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* kobject_del() - Unlink kobject from hierarchy.
|
|
|
|
* @kobj: object.
|
|
|
|
*
|
|
|
|
* This is the function that should be called to delete an object
|
|
|
|
* successfully added via kobject_add().
|
|
|
|
*/
|
|
|
|
void kobject_del(struct kobject *kobj)
|
|
|
|
{
|
2020-08-03 08:27:06 +00:00
|
|
|
struct kobject *parent;
|
|
|
|
|
|
|
|
if (!kobj)
|
|
|
|
return;
|
2020-06-04 17:46:46 +00:00
|
|
|
|
2020-08-03 08:27:06 +00:00
|
|
|
parent = kobj->parent;
|
2020-06-04 17:46:46 +00:00
|
|
|
__kobject_del(kobj);
|
|
|
|
kobject_put(parent);
|
|
|
|
}
|
2015-08-10 19:51:43 +00:00
|
|
|
EXPORT_SYMBOL(kobject_del);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/**
|
2019-05-02 02:31:40 +00:00
|
|
|
* kobject_get() - Increment refcount for object.
|
2008-01-25 05:59:04 +00:00
|
|
|
* @kobj: object.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2008-01-25 05:59:04 +00:00
|
|
|
struct kobject *kobject_get(struct kobject *kobj)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2015-03-12 04:04:16 +00:00
|
|
|
if (kobj) {
|
|
|
|
if (!kobj->state_initialized)
|
2018-03-15 13:23:43 +00:00
|
|
|
WARN(1, KERN_WARNING
|
|
|
|
"kobject: '%s' (%p): is not initialized, yet kobject_get() is being called.\n",
|
|
|
|
kobject_name(kobj), kobj);
|
2005-04-16 22:20:36 +00:00
|
|
|
kref_get(&kobj->kref);
|
2015-03-12 04:04:16 +00:00
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
return kobj;
|
|
|
|
}
|
2015-08-10 19:51:43 +00:00
|
|
|
EXPORT_SYMBOL(kobject_get);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2017-03-23 00:37:01 +00:00
|
|
|
struct kobject * __must_check kobject_get_unless_zero(struct kobject *kobj)
|
2013-04-13 22:15:30 +00:00
|
|
|
{
|
2017-03-23 00:37:01 +00:00
|
|
|
if (!kobj)
|
|
|
|
return NULL;
|
2013-04-13 22:15:30 +00:00
|
|
|
if (!kref_get_unless_zero(&kobj->kref))
|
|
|
|
kobj = NULL;
|
|
|
|
return kobj;
|
|
|
|
}
|
2017-03-23 00:37:01 +00:00
|
|
|
EXPORT_SYMBOL(kobject_get_unless_zero);
|
2013-04-13 22:15:30 +00:00
|
|
|
|
2007-12-04 05:31:08 +00:00
|
|
|
/*
|
|
|
|
* kobject_cleanup - free kobject resources.
|
|
|
|
* @kobj: object to cleanup
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2007-12-04 05:31:08 +00:00
|
|
|
static void kobject_cleanup(struct kobject *kobj)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2020-06-04 17:46:46 +00:00
|
|
|
struct kobject *parent = kobj->parent;
|
2021-12-24 23:13:45 +00:00
|
|
|
const struct kobj_type *t = get_ktype(kobj);
|
2007-12-20 01:09:39 +00:00
|
|
|
const char *name = kobj->name;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2023-03-11 03:14:46 +00:00
|
|
|
pr_debug("'%s' (%p): %s, parent %p\n",
|
2013-06-27 14:06:14 +00:00
|
|
|
kobject_name(kobj), kobj, __func__, kobj->parent);
|
2007-12-19 00:40:42 +00:00
|
|
|
|
|
|
|
/* remove from sysfs if the caller did not do it */
|
|
|
|
if (kobj->state_in_sysfs) {
|
2023-03-11 03:14:46 +00:00
|
|
|
pr_debug("'%s' (%p): auto cleanup kobject_del\n",
|
2007-12-19 00:40:42 +00:00
|
|
|
kobject_name(kobj), kobj);
|
2020-06-04 17:46:46 +00:00
|
|
|
__kobject_del(kobj);
|
|
|
|
} else {
|
|
|
|
/* avoid dropping the parent reference unnecessarily */
|
|
|
|
parent = NULL;
|
2007-12-19 00:40:42 +00:00
|
|
|
}
|
|
|
|
|
2023-08-05 08:41:14 +00:00
|
|
|
if (t->release) {
|
2023-03-11 03:14:46 +00:00
|
|
|
pr_debug("'%s' (%p): calling ktype release\n",
|
2007-12-19 00:40:42 +00:00
|
|
|
kobject_name(kobj), kobj);
|
2005-04-16 22:20:36 +00:00
|
|
|
t->release(kobj);
|
2023-08-05 08:41:14 +00:00
|
|
|
} else {
|
|
|
|
pr_debug("'%s' (%p): does not have a release() function, it is broken and must be fixed. See Documentation/core-api/kobject.rst.\n",
|
|
|
|
kobject_name(kobj), kobj);
|
2007-12-19 00:40:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* free name if we allocated it */
|
2007-12-20 01:09:39 +00:00
|
|
|
if (name) {
|
2023-03-11 03:14:46 +00:00
|
|
|
pr_debug("'%s': free name\n", name);
|
2015-11-07 00:31:23 +00:00
|
|
|
kfree_const(name);
|
2007-09-12 22:06:57 +00:00
|
|
|
}
|
2020-06-04 17:46:46 +00:00
|
|
|
|
|
|
|
kobject_put(parent);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2013-06-27 14:06:14 +00:00
|
|
|
#ifdef CONFIG_DEBUG_KOBJECT_RELEASE
|
|
|
|
static void kobject_delayed_cleanup(struct work_struct *work)
|
|
|
|
{
|
|
|
|
kobject_cleanup(container_of(to_delayed_work(work),
|
|
|
|
struct kobject, release));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
static void kobject_release(struct kref *kref)
|
|
|
|
{
|
2013-06-27 14:06:14 +00:00
|
|
|
struct kobject *kobj = container_of(kref, struct kobject, kref);
|
|
|
|
#ifdef CONFIG_DEBUG_KOBJECT_RELEASE
|
2022-10-10 02:44:02 +00:00
|
|
|
unsigned long delay = HZ + HZ * get_random_u32_below(4);
|
2023-03-11 03:14:46 +00:00
|
|
|
pr_info("'%s' (%p): %s, parent %p (delayed %ld)\n",
|
|
|
|
kobject_name(kobj), kobj, __func__, kobj->parent, delay);
|
2013-06-27 14:06:14 +00:00
|
|
|
INIT_DELAYED_WORK(&kobj->release, kobject_delayed_cleanup);
|
2013-12-06 00:37:51 +00:00
|
|
|
|
|
|
|
schedule_delayed_work(&kobj->release, delay);
|
2013-06-27 14:06:14 +00:00
|
|
|
#else
|
|
|
|
kobject_cleanup(kobj);
|
|
|
|
#endif
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-02 02:31:40 +00:00
|
|
|
* kobject_put() - Decrement refcount for object.
|
2008-01-25 05:59:04 +00:00
|
|
|
* @kobj: object.
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
2008-01-25 05:59:04 +00:00
|
|
|
* Decrement the refcount, and if 0, call kobject_cleanup().
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2008-01-25 05:59:04 +00:00
|
|
|
void kobject_put(struct kobject *kobj)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-02-26 17:36:38 +00:00
|
|
|
if (kobj) {
|
2008-07-25 08:45:55 +00:00
|
|
|
if (!kobj->state_initialized)
|
2018-03-15 13:23:43 +00:00
|
|
|
WARN(1, KERN_WARNING
|
|
|
|
"kobject: '%s' (%p): is not initialized, yet kobject_put() is being called.\n",
|
|
|
|
kobject_name(kobj), kobj);
|
2005-04-16 22:20:36 +00:00
|
|
|
kref_put(&kobj->kref, kobject_release);
|
2008-02-26 17:36:38 +00:00
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2015-08-10 19:51:43 +00:00
|
|
|
EXPORT_SYMBOL(kobject_put);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2007-11-05 21:16:15 +00:00
|
|
|
static void dynamic_kobj_release(struct kobject *kobj)
|
2006-03-13 22:14:25 +00:00
|
|
|
{
|
2023-03-11 03:14:46 +00:00
|
|
|
pr_debug("(%p): %s\n", kobj, __func__);
|
2006-03-13 22:14:25 +00:00
|
|
|
kfree(kobj);
|
|
|
|
}
|
|
|
|
|
2023-02-04 21:59:50 +00:00
|
|
|
static const struct kobj_type dynamic_kobj_ktype = {
|
2007-11-02 12:47:53 +00:00
|
|
|
.release = dynamic_kobj_release,
|
|
|
|
.sysfs_ops = &kobj_sysfs_ops,
|
2006-03-13 22:14:25 +00:00
|
|
|
};
|
|
|
|
|
2007-11-06 06:24:43 +00:00
|
|
|
/**
|
2019-05-02 02:31:40 +00:00
|
|
|
* kobject_create() - Create a struct kobject dynamically.
|
2007-11-05 21:16:15 +00:00
|
|
|
*
|
|
|
|
* This function creates a kobject structure dynamically and sets it up
|
|
|
|
* to be a "dynamic" kobject with a default release function set up.
|
|
|
|
*
|
|
|
|
* If the kobject was not able to be created, NULL will be returned.
|
2007-11-06 06:24:43 +00:00
|
|
|
* The kobject structure returned from here must be cleaned up with a
|
2007-12-18 06:05:35 +00:00
|
|
|
* call to kobject_put() and not kfree(), as kobject_init() has
|
2007-11-06 06:24:43 +00:00
|
|
|
* already been called on this structure.
|
2007-11-05 21:16:15 +00:00
|
|
|
*/
|
2021-08-31 09:30:44 +00:00
|
|
|
static struct kobject *kobject_create(void)
|
2007-11-05 21:16:15 +00:00
|
|
|
{
|
|
|
|
struct kobject *kobj;
|
|
|
|
|
|
|
|
kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
|
|
|
|
if (!kobj)
|
|
|
|
return NULL;
|
|
|
|
|
2007-12-18 06:05:35 +00:00
|
|
|
kobject_init(kobj, &dynamic_kobj_ktype);
|
2007-11-05 21:16:15 +00:00
|
|
|
return kobj;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-02 02:31:40 +00:00
|
|
|
* kobject_create_and_add() - Create a struct kobject dynamically and
|
|
|
|
* register it with sysfs.
|
2012-05-07 02:48:25 +00:00
|
|
|
* @name: the name for the kobject
|
2007-11-05 21:16:15 +00:00
|
|
|
* @parent: the parent kobject of this kobject, if any.
|
|
|
|
*
|
2008-01-28 08:58:00 +00:00
|
|
|
* This function creates a kobject structure dynamically and registers it
|
2007-11-05 21:16:15 +00:00
|
|
|
* with sysfs. When you are finished with this structure, call
|
2007-12-20 16:13:05 +00:00
|
|
|
* kobject_put() and the structure will be dynamically freed when
|
2007-11-05 21:16:15 +00:00
|
|
|
* it is no longer being used.
|
|
|
|
*
|
|
|
|
* If the kobject was not able to be created, NULL will be returned.
|
|
|
|
*/
|
|
|
|
struct kobject *kobject_create_and_add(const char *name, struct kobject *parent)
|
|
|
|
{
|
|
|
|
struct kobject *kobj;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
kobj = kobject_create();
|
|
|
|
if (!kobj)
|
|
|
|
return NULL;
|
|
|
|
|
2007-12-18 06:05:35 +00:00
|
|
|
retval = kobject_add(kobj, parent, "%s", name);
|
2007-11-05 21:16:15 +00:00
|
|
|
if (retval) {
|
2018-03-15 13:23:43 +00:00
|
|
|
pr_warn("%s: kobject_add error: %d\n", __func__, retval);
|
2007-11-05 21:16:15 +00:00
|
|
|
kobject_put(kobj);
|
|
|
|
kobj = NULL;
|
|
|
|
}
|
|
|
|
return kobj;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(kobject_create_and_add);
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/**
|
2019-05-02 02:31:40 +00:00
|
|
|
* kset_init() - Initialize a kset for use.
|
2008-01-25 05:59:04 +00:00
|
|
|
* @k: kset
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2008-01-25 05:59:04 +00:00
|
|
|
void kset_init(struct kset *k)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2007-12-18 06:05:35 +00:00
|
|
|
kobject_init_internal(&k->kobj);
|
2005-04-16 22:20:36 +00:00
|
|
|
INIT_LIST_HEAD(&k->list);
|
|
|
|
spin_lock_init(&k->list_lock);
|
|
|
|
}
|
|
|
|
|
2007-11-02 12:47:53 +00:00
|
|
|
/* default kobject attribute operations */
|
|
|
|
static ssize_t kobj_attr_show(struct kobject *kobj, struct attribute *attr,
|
|
|
|
char *buf)
|
|
|
|
{
|
|
|
|
struct kobj_attribute *kattr;
|
|
|
|
ssize_t ret = -EIO;
|
|
|
|
|
|
|
|
kattr = container_of(attr, struct kobj_attribute, attr);
|
|
|
|
if (kattr->show)
|
|
|
|
ret = kattr->show(kobj, kattr, buf);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t kobj_attr_store(struct kobject *kobj, struct attribute *attr,
|
|
|
|
const char *buf, size_t count)
|
|
|
|
{
|
|
|
|
struct kobj_attribute *kattr;
|
|
|
|
ssize_t ret = -EIO;
|
|
|
|
|
|
|
|
kattr = container_of(attr, struct kobj_attribute, attr);
|
|
|
|
if (kattr->store)
|
|
|
|
ret = kattr->store(kobj, kattr, buf, count);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-01-19 01:58:23 +00:00
|
|
|
const struct sysfs_ops kobj_sysfs_ops = {
|
2007-11-02 12:47:53 +00:00
|
|
|
.show = kobj_attr_show,
|
|
|
|
.store = kobj_attr_store,
|
|
|
|
};
|
2013-11-01 17:06:56 +00:00
|
|
|
EXPORT_SYMBOL_GPL(kobj_sysfs_ops);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/**
|
2019-05-02 02:31:40 +00:00
|
|
|
* kset_register() - Initialize and add a kset.
|
2008-01-25 05:59:04 +00:00
|
|
|
* @k: kset.
|
kset: fix memory leak when kset_register() returns error
Inject fault while loading module, kset_register() may fail.
If it fails, the kset.kobj.name allocated by kobject_set_name()
which must be called before a call to kset_register() may be
leaked, since refcount of kobj was set in kset_init().
To mitigate this, we free the name in kset_register() when an
error is encountered, i.e. when kset_register() returns an error.
A kset may be embedded in a larger structure which may be dynamically
allocated in callers, it needs to be freed in ktype.release() or error
path in callers, in this case, we can not call kset_put() in kset_register(),
or it will cause double free, so just call kfree_const() to free the
name and set it to NULL to avoid accessing bad pointer in callers.
With this fix, the callers don't need care about freeing the name
and may call kset_put() if kset_register() fails.
Suggested-by: Luben Tuikov <luben.tuikov@amd.com>
Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
Reviewed-by: <luben.tuikov@amd.com>
Link: https://lore.kernel.org/r/20221025071549.1280528-1-yangyingliang@huawei.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2022-10-25 07:15:49 +00:00
|
|
|
*
|
|
|
|
* NOTE: On error, the kset.kobj.name allocated by() kobj_set_name()
|
|
|
|
* is freed, it can not be used any more.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2008-01-25 05:59:04 +00:00
|
|
|
int kset_register(struct kset *k)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2007-05-26 09:21:36 +00:00
|
|
|
int err;
|
|
|
|
|
2007-01-18 20:23:51 +00:00
|
|
|
if (!k)
|
|
|
|
return -EINVAL;
|
2007-05-26 09:21:36 +00:00
|
|
|
|
2023-08-05 08:41:13 +00:00
|
|
|
if (!k->kobj.ktype) {
|
|
|
|
pr_err("must have a ktype to be initialized properly!\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
kset_init(k);
|
2002-04-09 19:14:34 +00:00
|
|
|
err = kobject_add_internal(&k->kobj);
|
kset: fix memory leak when kset_register() returns error
Inject fault while loading module, kset_register() may fail.
If it fails, the kset.kobj.name allocated by kobject_set_name()
which must be called before a call to kset_register() may be
leaked, since refcount of kobj was set in kset_init().
To mitigate this, we free the name in kset_register() when an
error is encountered, i.e. when kset_register() returns an error.
A kset may be embedded in a larger structure which may be dynamically
allocated in callers, it needs to be freed in ktype.release() or error
path in callers, in this case, we can not call kset_put() in kset_register(),
or it will cause double free, so just call kfree_const() to free the
name and set it to NULL to avoid accessing bad pointer in callers.
With this fix, the callers don't need care about freeing the name
and may call kset_put() if kset_register() fails.
Suggested-by: Luben Tuikov <luben.tuikov@amd.com>
Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
Reviewed-by: <luben.tuikov@amd.com>
Link: https://lore.kernel.org/r/20221025071549.1280528-1-yangyingliang@huawei.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2022-10-25 07:15:49 +00:00
|
|
|
if (err) {
|
|
|
|
kfree_const(k->kobj.name);
|
|
|
|
/* Set it to NULL to avoid accessing bad pointer in callers. */
|
|
|
|
k->kobj.name = NULL;
|
2007-05-26 09:21:36 +00:00
|
|
|
return err;
|
kset: fix memory leak when kset_register() returns error
Inject fault while loading module, kset_register() may fail.
If it fails, the kset.kobj.name allocated by kobject_set_name()
which must be called before a call to kset_register() may be
leaked, since refcount of kobj was set in kset_init().
To mitigate this, we free the name in kset_register() when an
error is encountered, i.e. when kset_register() returns an error.
A kset may be embedded in a larger structure which may be dynamically
allocated in callers, it needs to be freed in ktype.release() or error
path in callers, in this case, we can not call kset_put() in kset_register(),
or it will cause double free, so just call kfree_const() to free the
name and set it to NULL to avoid accessing bad pointer in callers.
With this fix, the callers don't need care about freeing the name
and may call kset_put() if kset_register() fails.
Suggested-by: Luben Tuikov <luben.tuikov@amd.com>
Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
Reviewed-by: <luben.tuikov@amd.com>
Link: https://lore.kernel.org/r/20221025071549.1280528-1-yangyingliang@huawei.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2022-10-25 07:15:49 +00:00
|
|
|
}
|
2007-05-26 09:21:36 +00:00
|
|
|
kobject_uevent(&k->kobj, KOBJ_ADD);
|
|
|
|
return 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2015-08-10 19:51:43 +00:00
|
|
|
EXPORT_SYMBOL(kset_register);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/**
|
2019-05-02 02:31:40 +00:00
|
|
|
* kset_unregister() - Remove a kset.
|
2008-01-25 05:59:04 +00:00
|
|
|
* @k: kset.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2008-01-25 05:59:04 +00:00
|
|
|
void kset_unregister(struct kset *k)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2007-01-18 20:23:51 +00:00
|
|
|
if (!k)
|
|
|
|
return;
|
2013-12-06 00:38:00 +00:00
|
|
|
kobject_del(&k->kobj);
|
2007-12-20 16:13:05 +00:00
|
|
|
kobject_put(&k->kobj);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2015-08-10 19:51:43 +00:00
|
|
|
EXPORT_SYMBOL(kset_unregister);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/**
|
2019-05-02 02:31:40 +00:00
|
|
|
* kset_find_obj() - Search for object in kset.
|
2008-01-25 05:59:04 +00:00
|
|
|
* @kset: kset we're looking in.
|
|
|
|
* @name: object's name.
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
2008-01-25 05:59:04 +00:00
|
|
|
* Lock kset via @kset->subsys, and iterate over @kset->list,
|
|
|
|
* looking for a matching kobject. If matching object is found
|
|
|
|
* take a reference and return the object.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2008-01-25 05:59:04 +00:00
|
|
|
struct kobject *kset_find_obj(struct kset *kset, const char *name)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-03-27 05:13:34 +00:00
|
|
|
struct kobject *k;
|
2008-01-25 05:59:04 +00:00
|
|
|
struct kobject *ret = NULL;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
spin_lock(&kset->list_lock);
|
2010-09-29 19:00:54 +00:00
|
|
|
|
2008-03-27 05:13:34 +00:00
|
|
|
list_for_each_entry(k, &kset->list, entry) {
|
2008-01-25 05:59:04 +00:00
|
|
|
if (kobject_name(k) && !strcmp(kobject_name(k), name)) {
|
2013-04-13 22:15:30 +00:00
|
|
|
ret = kobject_get_unless_zero(k);
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-09-29 19:00:54 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
spin_unlock(&kset->list_lock);
|
|
|
|
return ret;
|
|
|
|
}
|
2016-01-28 14:23:12 +00:00
|
|
|
EXPORT_SYMBOL_GPL(kset_find_obj);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2007-09-27 21:48:53 +00:00
|
|
|
static void kset_release(struct kobject *kobj)
|
|
|
|
{
|
|
|
|
struct kset *kset = container_of(kobj, struct kset, kobj);
|
2023-03-11 03:14:46 +00:00
|
|
|
pr_debug("'%s' (%p): %s\n",
|
2008-04-30 07:55:08 +00:00
|
|
|
kobject_name(kobj), kobj, __func__);
|
2007-09-27 21:48:53 +00:00
|
|
|
kfree(kset);
|
|
|
|
}
|
|
|
|
|
2022-11-21 09:46:45 +00:00
|
|
|
static void kset_get_ownership(const struct kobject *kobj, kuid_t *uid, kgid_t *gid)
|
2018-07-20 21:56:49 +00:00
|
|
|
{
|
|
|
|
if (kobj->parent)
|
|
|
|
kobject_get_ownership(kobj->parent, uid, gid);
|
|
|
|
}
|
|
|
|
|
2023-02-04 21:59:50 +00:00
|
|
|
static const struct kobj_type kset_ktype = {
|
2007-11-02 12:47:53 +00:00
|
|
|
.sysfs_ops = &kobj_sysfs_ops,
|
2018-07-20 21:56:49 +00:00
|
|
|
.release = kset_release,
|
|
|
|
.get_ownership = kset_get_ownership,
|
2007-09-27 21:48:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2019-05-02 02:31:40 +00:00
|
|
|
* kset_create() - Create a struct kset dynamically.
|
2007-09-27 21:48:53 +00:00
|
|
|
*
|
|
|
|
* @name: the name for the kset
|
|
|
|
* @uevent_ops: a struct kset_uevent_ops for the kset
|
|
|
|
* @parent_kobj: the parent kobject of this kset, if any.
|
|
|
|
*
|
|
|
|
* This function creates a kset structure dynamically. This structure can
|
|
|
|
* then be registered with the system and show up in sysfs with a call to
|
|
|
|
* kset_register(). When you are finished with this structure, if
|
|
|
|
* kset_register() has been called, call kset_unregister() and the
|
|
|
|
* structure will be dynamically freed when it is no longer being used.
|
|
|
|
*
|
|
|
|
* If the kset was not able to be created, NULL will be returned.
|
|
|
|
*/
|
|
|
|
static struct kset *kset_create(const char *name,
|
2009-12-31 13:52:51 +00:00
|
|
|
const struct kset_uevent_ops *uevent_ops,
|
2007-09-27 21:48:53 +00:00
|
|
|
struct kobject *parent_kobj)
|
|
|
|
{
|
|
|
|
struct kset *kset;
|
2009-05-11 06:17:45 +00:00
|
|
|
int retval;
|
2007-09-27 21:48:53 +00:00
|
|
|
|
|
|
|
kset = kzalloc(sizeof(*kset), GFP_KERNEL);
|
|
|
|
if (!kset)
|
|
|
|
return NULL;
|
2013-06-06 20:52:19 +00:00
|
|
|
retval = kobject_set_name(&kset->kobj, "%s", name);
|
2009-05-11 06:17:45 +00:00
|
|
|
if (retval) {
|
|
|
|
kfree(kset);
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-09-27 21:48:53 +00:00
|
|
|
kset->uevent_ops = uevent_ops;
|
|
|
|
kset->kobj.parent = parent_kobj;
|
|
|
|
|
|
|
|
/*
|
2007-11-02 12:47:53 +00:00
|
|
|
* The kobject of this kset will have a type of kset_ktype and belong to
|
2007-09-27 21:48:53 +00:00
|
|
|
* no kset itself. That way we can properly free it when it is
|
|
|
|
* finished being used.
|
|
|
|
*/
|
2007-11-02 12:47:53 +00:00
|
|
|
kset->kobj.ktype = &kset_ktype;
|
2007-09-27 21:48:53 +00:00
|
|
|
kset->kobj.kset = NULL;
|
|
|
|
|
|
|
|
return kset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-02 02:31:40 +00:00
|
|
|
* kset_create_and_add() - Create a struct kset dynamically and add it to sysfs.
|
2007-09-27 21:48:53 +00:00
|
|
|
*
|
|
|
|
* @name: the name for the kset
|
|
|
|
* @uevent_ops: a struct kset_uevent_ops for the kset
|
|
|
|
* @parent_kobj: the parent kobject of this kset, if any.
|
|
|
|
*
|
|
|
|
* This function creates a kset structure dynamically and registers it
|
|
|
|
* with sysfs. When you are finished with this structure, call
|
|
|
|
* kset_unregister() and the structure will be dynamically freed when it
|
|
|
|
* is no longer being used.
|
|
|
|
*
|
|
|
|
* If the kset was not able to be created, NULL will be returned.
|
|
|
|
*/
|
|
|
|
struct kset *kset_create_and_add(const char *name,
|
2009-12-31 13:52:51 +00:00
|
|
|
const struct kset_uevent_ops *uevent_ops,
|
2007-09-27 21:48:53 +00:00
|
|
|
struct kobject *parent_kobj)
|
|
|
|
{
|
|
|
|
struct kset *kset;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
kset = kset_create(name, uevent_ops, parent_kobj);
|
|
|
|
if (!kset)
|
|
|
|
return NULL;
|
|
|
|
error = kset_register(kset);
|
|
|
|
if (error) {
|
|
|
|
kfree(kset);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return kset;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(kset_create_and_add);
|
|
|
|
|
2010-03-30 18:31:25 +00:00
|
|
|
|
|
|
|
static DEFINE_SPINLOCK(kobj_ns_type_lock);
|
|
|
|
static const struct kobj_ns_type_operations *kobj_ns_ops_tbl[KOBJ_NS_TYPES];
|
|
|
|
|
|
|
|
int kobj_ns_type_register(const struct kobj_ns_type_operations *ops)
|
|
|
|
{
|
|
|
|
enum kobj_ns_type type = ops->type;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
spin_lock(&kobj_ns_type_lock);
|
|
|
|
|
|
|
|
error = -EINVAL;
|
2023-07-26 06:25:08 +00:00
|
|
|
if (!kobj_ns_type_is_valid(type))
|
2010-03-30 18:31:25 +00:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
error = -EBUSY;
|
|
|
|
if (kobj_ns_ops_tbl[type])
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
error = 0;
|
|
|
|
kobj_ns_ops_tbl[type] = ops;
|
|
|
|
|
|
|
|
out:
|
|
|
|
spin_unlock(&kobj_ns_type_lock);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
int kobj_ns_type_registered(enum kobj_ns_type type)
|
|
|
|
{
|
|
|
|
int registered = 0;
|
|
|
|
|
|
|
|
spin_lock(&kobj_ns_type_lock);
|
2023-07-26 06:25:08 +00:00
|
|
|
if (kobj_ns_type_is_valid(type))
|
2010-03-30 18:31:25 +00:00
|
|
|
registered = kobj_ns_ops_tbl[type] != NULL;
|
|
|
|
spin_unlock(&kobj_ns_type_lock);
|
|
|
|
|
|
|
|
return registered;
|
|
|
|
}
|
|
|
|
|
2022-11-21 09:46:46 +00:00
|
|
|
const struct kobj_ns_type_operations *kobj_child_ns_ops(const struct kobject *parent)
|
2010-03-30 18:31:25 +00:00
|
|
|
{
|
|
|
|
const struct kobj_ns_type_operations *ops = NULL;
|
|
|
|
|
2023-08-05 08:41:14 +00:00
|
|
|
if (parent && parent->ktype->child_ns_type)
|
2010-03-30 18:31:25 +00:00
|
|
|
ops = parent->ktype->child_ns_type(parent);
|
|
|
|
|
|
|
|
return ops;
|
|
|
|
}
|
|
|
|
|
2022-11-21 09:46:46 +00:00
|
|
|
const struct kobj_ns_type_operations *kobj_ns_ops(const struct kobject *kobj)
|
2010-03-30 18:31:25 +00:00
|
|
|
{
|
|
|
|
return kobj_child_ns_ops(kobj->parent);
|
|
|
|
}
|
|
|
|
|
2013-03-26 03:07:01 +00:00
|
|
|
bool kobj_ns_current_may_mount(enum kobj_ns_type type)
|
|
|
|
{
|
2013-09-23 21:41:17 +00:00
|
|
|
bool may_mount = true;
|
2013-03-26 03:07:01 +00:00
|
|
|
|
|
|
|
spin_lock(&kobj_ns_type_lock);
|
2023-07-26 06:25:08 +00:00
|
|
|
if (kobj_ns_type_is_valid(type) && kobj_ns_ops_tbl[type])
|
2013-03-26 03:07:01 +00:00
|
|
|
may_mount = kobj_ns_ops_tbl[type]->current_may_mount();
|
|
|
|
spin_unlock(&kobj_ns_type_lock);
|
|
|
|
|
|
|
|
return may_mount;
|
|
|
|
}
|
2010-03-30 18:31:25 +00:00
|
|
|
|
2011-06-09 01:13:01 +00:00
|
|
|
void *kobj_ns_grab_current(enum kobj_ns_type type)
|
2010-03-30 18:31:25 +00:00
|
|
|
{
|
2011-06-09 01:13:01 +00:00
|
|
|
void *ns = NULL;
|
2010-03-30 18:31:25 +00:00
|
|
|
|
|
|
|
spin_lock(&kobj_ns_type_lock);
|
2023-07-26 06:25:08 +00:00
|
|
|
if (kobj_ns_type_is_valid(type) && kobj_ns_ops_tbl[type])
|
2011-06-09 01:13:01 +00:00
|
|
|
ns = kobj_ns_ops_tbl[type]->grab_current_ns();
|
2010-03-30 18:31:25 +00:00
|
|
|
spin_unlock(&kobj_ns_type_lock);
|
|
|
|
|
|
|
|
return ns;
|
|
|
|
}
|
2018-01-22 22:27:11 +00:00
|
|
|
EXPORT_SYMBOL_GPL(kobj_ns_grab_current);
|
2010-03-30 18:31:25 +00:00
|
|
|
|
|
|
|
const void *kobj_ns_netlink(enum kobj_ns_type type, struct sock *sk)
|
|
|
|
{
|
|
|
|
const void *ns = NULL;
|
|
|
|
|
|
|
|
spin_lock(&kobj_ns_type_lock);
|
2023-07-26 06:25:08 +00:00
|
|
|
if (kobj_ns_type_is_valid(type) && kobj_ns_ops_tbl[type])
|
2010-03-30 18:31:25 +00:00
|
|
|
ns = kobj_ns_ops_tbl[type]->netlink_ns(sk);
|
|
|
|
spin_unlock(&kobj_ns_type_lock);
|
|
|
|
|
|
|
|
return ns;
|
|
|
|
}
|
|
|
|
|
|
|
|
const void *kobj_ns_initial(enum kobj_ns_type type)
|
|
|
|
{
|
|
|
|
const void *ns = NULL;
|
|
|
|
|
|
|
|
spin_lock(&kobj_ns_type_lock);
|
2023-07-26 06:25:08 +00:00
|
|
|
if (kobj_ns_type_is_valid(type) && kobj_ns_ops_tbl[type])
|
2010-03-30 18:31:25 +00:00
|
|
|
ns = kobj_ns_ops_tbl[type]->initial_ns();
|
|
|
|
spin_unlock(&kobj_ns_type_lock);
|
|
|
|
|
|
|
|
return ns;
|
|
|
|
}
|
|
|
|
|
2011-06-09 01:13:01 +00:00
|
|
|
void kobj_ns_drop(enum kobj_ns_type type, void *ns)
|
2010-03-30 18:31:25 +00:00
|
|
|
{
|
2011-06-09 01:13:01 +00:00
|
|
|
spin_lock(&kobj_ns_type_lock);
|
2023-07-26 06:25:08 +00:00
|
|
|
if (kobj_ns_type_is_valid(type) &&
|
2011-06-09 01:13:01 +00:00
|
|
|
kobj_ns_ops_tbl[type] && kobj_ns_ops_tbl[type]->drop_ns)
|
|
|
|
kobj_ns_ops_tbl[type]->drop_ns(ns);
|
|
|
|
spin_unlock(&kobj_ns_type_lock);
|
2010-03-30 18:31:25 +00:00
|
|
|
}
|
2018-01-22 22:27:11 +00:00
|
|
|
EXPORT_SYMBOL_GPL(kobj_ns_drop);
|