2005-04-17 05:20:36 +07:00
|
|
|
/*
|
2007-09-20 15:31:38 +07:00
|
|
|
* fs/sysfs/file.c - sysfs regular (text) file implementation
|
|
|
|
*
|
|
|
|
* Copyright (c) 2001-3 Patrick Mochel
|
|
|
|
* Copyright (c) 2007 SUSE Linux Products GmbH
|
|
|
|
* Copyright (c) 2007 Tejun Heo <teheo@suse.de>
|
|
|
|
*
|
|
|
|
* This file is released under the GPLv2.
|
|
|
|
*
|
|
|
|
* Please see Documentation/filesystems/sysfs.txt for more information.
|
2005-04-17 05:20:36 +07:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/kobject.h>
|
2008-03-05 06:09:07 +07:00
|
|
|
#include <linux/kallsyms.h>
|
2008-03-14 09:41:52 +07:00
|
|
|
#include <linux/slab.h>
|
2006-12-20 16:52:44 +07:00
|
|
|
#include <linux/list.h>
|
2007-07-26 18:03:54 +07:00
|
|
|
#include <linux/mutex.h>
|
2013-10-02 04:42:02 +07:00
|
|
|
#include <linux/seq_file.h>
|
2005-04-17 05:20:36 +07:00
|
|
|
|
|
|
|
#include "sysfs.h"
|
2013-11-29 02:54:34 +07:00
|
|
|
#include "../kernfs/kernfs-internal.h"
|
2013-11-29 02:54:21 +07:00
|
|
|
|
2013-10-02 04:41:57 +07:00
|
|
|
/*
|
|
|
|
* Determine ktype->sysfs_ops for the given sysfs_dirent. This function
|
|
|
|
* must be called while holding an active reference.
|
|
|
|
*/
|
|
|
|
static const struct sysfs_ops *sysfs_file_ops(struct sysfs_dirent *sd)
|
|
|
|
{
|
2013-11-29 02:54:14 +07:00
|
|
|
struct kobject *kobj = sd->s_parent->priv;
|
2013-10-02 04:41:57 +07:00
|
|
|
|
2013-11-29 02:54:29 +07:00
|
|
|
if (sd->s_flags & SYSFS_FLAG_LOCKDEP)
|
2013-10-14 20:27:11 +07:00
|
|
|
lockdep_assert_held(sd);
|
2013-10-02 04:41:57 +07:00
|
|
|
return kobj->ktype ? kobj->ktype->sysfs_ops : NULL;
|
|
|
|
}
|
|
|
|
|
2013-10-02 04:42:02 +07:00
|
|
|
/*
|
|
|
|
* Reads on sysfs are handled through seq_file, which takes care of hairy
|
|
|
|
* details like buffering and seeking. The following function pipes
|
|
|
|
* sysfs_ops->show() result through seq_file.
|
2005-04-17 05:20:36 +07:00
|
|
|
*/
|
2013-11-29 02:54:16 +07:00
|
|
|
static int sysfs_kf_seq_show(struct seq_file *sf, void *v)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2013-10-02 04:42:02 +07:00
|
|
|
struct sysfs_open_file *of = sf->private;
|
2013-11-29 02:54:14 +07:00
|
|
|
struct kobject *kobj = of->sd->s_parent->priv;
|
2013-11-29 02:54:16 +07:00
|
|
|
const struct sysfs_ops *ops = sysfs_file_ops(of->sd);
|
2005-04-17 05:20:36 +07:00
|
|
|
ssize_t count;
|
2013-11-29 02:54:16 +07:00
|
|
|
char *buf;
|
2005-04-17 05:20:36 +07:00
|
|
|
|
2013-10-02 04:42:02 +07:00
|
|
|
/* acquire buffer and ensure that it's >= PAGE_SIZE */
|
|
|
|
count = seq_get_buf(sf, &buf);
|
|
|
|
if (count < PAGE_SIZE) {
|
|
|
|
seq_commit(sf, -1);
|
|
|
|
return 0;
|
|
|
|
}
|
2005-04-17 05:20:36 +07:00
|
|
|
|
2013-10-02 04:42:02 +07:00
|
|
|
/*
|
2013-11-29 02:54:16 +07:00
|
|
|
* Invoke show(). Control may reach here via seq file lseek even
|
|
|
|
* if @ops->show() isn't implemented.
|
2013-10-02 04:42:02 +07:00
|
|
|
*/
|
2013-11-29 02:54:16 +07:00
|
|
|
if (ops->show) {
|
2013-11-29 02:54:14 +07:00
|
|
|
count = ops->show(kobj, of->sd->priv, buf);
|
2013-11-29 02:54:16 +07:00
|
|
|
if (count < 0)
|
|
|
|
return count;
|
|
|
|
}
|
2007-06-14 01:45:16 +07:00
|
|
|
|
2007-11-22 05:55:19 +07:00
|
|
|
/*
|
|
|
|
* The code works fine with PAGE_SIZE return but it's likely to
|
|
|
|
* indicate truncated result or overflow in normal use cases.
|
|
|
|
*/
|
2008-03-05 06:09:07 +07:00
|
|
|
if (count >= (ssize_t)PAGE_SIZE) {
|
|
|
|
print_symbol("fill_read_buffer: %s returned bad count\n",
|
|
|
|
(unsigned long)ops->show);
|
|
|
|
/* Try to struggle along */
|
|
|
|
count = PAGE_SIZE - 1;
|
|
|
|
}
|
2013-10-02 04:42:02 +07:00
|
|
|
seq_commit(sf, count);
|
|
|
|
return 0;
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
2013-11-29 02:54:16 +07:00
|
|
|
static ssize_t sysfs_kf_bin_read(struct sysfs_open_file *of, char *buf,
|
|
|
|
size_t count, loff_t pos)
|
2013-10-02 04:42:06 +07:00
|
|
|
{
|
2013-11-29 02:54:14 +07:00
|
|
|
struct bin_attribute *battr = of->sd->priv;
|
|
|
|
struct kobject *kobj = of->sd->s_parent->priv;
|
2013-11-29 02:54:16 +07:00
|
|
|
loff_t size = file_inode(of->file)->i_size;
|
2013-10-02 04:42:06 +07:00
|
|
|
|
2013-11-29 02:54:16 +07:00
|
|
|
if (!count)
|
2013-10-02 04:42:06 +07:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (size) {
|
2013-11-29 02:54:16 +07:00
|
|
|
if (pos > size)
|
2013-10-02 04:42:06 +07:00
|
|
|
return 0;
|
2013-11-29 02:54:16 +07:00
|
|
|
if (pos + count > size)
|
|
|
|
count = size - pos;
|
2013-10-02 04:42:06 +07:00
|
|
|
}
|
|
|
|
|
2013-11-29 02:54:16 +07:00
|
|
|
if (!battr->read)
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
return battr->read(of->file, kobj, battr, buf, pos, count);
|
|
|
|
}
|
|
|
|
|
2013-11-29 02:54:17 +07:00
|
|
|
/* kernfs write callback for regular sysfs files */
|
|
|
|
static ssize_t sysfs_kf_write(struct sysfs_open_file *of, char *buf,
|
|
|
|
size_t count, loff_t pos)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2013-11-29 02:54:17 +07:00
|
|
|
const struct sysfs_ops *ops = sysfs_file_ops(of->sd);
|
2013-11-29 02:54:14 +07:00
|
|
|
struct kobject *kobj = of->sd->s_parent->priv;
|
2007-06-14 01:45:16 +07:00
|
|
|
|
2013-11-29 02:54:17 +07:00
|
|
|
if (!count)
|
|
|
|
return 0;
|
2007-06-14 01:45:16 +07:00
|
|
|
|
2013-11-29 02:54:17 +07:00
|
|
|
return ops->store(kobj, of->sd->priv, buf, count);
|
|
|
|
}
|
2013-10-02 04:42:05 +07:00
|
|
|
|
2013-11-29 02:54:17 +07:00
|
|
|
/* kernfs write callback for bin sysfs files */
|
|
|
|
static ssize_t sysfs_kf_bin_write(struct sysfs_open_file *of, char *buf,
|
|
|
|
size_t count, loff_t pos)
|
|
|
|
{
|
|
|
|
struct bin_attribute *battr = of->sd->priv;
|
|
|
|
struct kobject *kobj = of->sd->s_parent->priv;
|
|
|
|
loff_t size = file_inode(of->file)->i_size;
|
2013-10-02 04:42:05 +07:00
|
|
|
|
2013-11-29 02:54:17 +07:00
|
|
|
if (size) {
|
|
|
|
if (size <= pos)
|
|
|
|
return 0;
|
|
|
|
count = min_t(ssize_t, count, size - pos);
|
2013-10-02 04:42:05 +07:00
|
|
|
}
|
2013-11-29 02:54:17 +07:00
|
|
|
if (!count)
|
|
|
|
return 0;
|
2007-06-14 01:45:16 +07:00
|
|
|
|
2013-11-29 02:54:17 +07:00
|
|
|
if (!battr->write)
|
|
|
|
return -EIO;
|
2005-04-17 05:20:36 +07:00
|
|
|
|
2013-11-29 02:54:17 +07:00
|
|
|
return battr->write(of->file, kobj, battr, buf, pos, count);
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
2013-11-29 02:54:18 +07:00
|
|
|
static int sysfs_kf_bin_mmap(struct sysfs_open_file *of,
|
|
|
|
struct vm_area_struct *vma)
|
|
|
|
{
|
|
|
|
struct bin_attribute *battr = of->sd->priv;
|
|
|
|
struct kobject *kobj = of->sd->s_parent->priv;
|
|
|
|
|
|
|
|
if (!battr->mmap)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
return battr->mmap(of->file, kobj, battr, vma);
|
|
|
|
}
|
|
|
|
|
2008-09-26 06:45:13 +07:00
|
|
|
void sysfs_notify(struct kobject *k, const char *dir, const char *attr)
|
2006-03-20 13:53:53 +07:00
|
|
|
{
|
2013-11-29 02:54:27 +07:00
|
|
|
struct sysfs_dirent *sd = k->sd, *tmp;
|
2007-06-14 02:27:25 +07:00
|
|
|
|
|
|
|
if (sd && dir)
|
2013-11-29 02:54:30 +07:00
|
|
|
sd = kernfs_find_and_get(sd, dir);
|
2013-11-29 02:54:27 +07:00
|
|
|
else
|
2013-11-29 02:54:30 +07:00
|
|
|
kernfs_get(sd);
|
2013-11-29 02:54:27 +07:00
|
|
|
|
|
|
|
if (sd && attr) {
|
2013-11-29 02:54:30 +07:00
|
|
|
tmp = kernfs_find_and_get(sd, attr);
|
|
|
|
kernfs_put(sd);
|
2013-11-29 02:54:27 +07:00
|
|
|
sd = tmp;
|
|
|
|
}
|
2007-06-14 02:27:25 +07:00
|
|
|
|
2013-11-29 02:54:27 +07:00
|
|
|
if (sd) {
|
|
|
|
kernfs_notify(sd);
|
2013-11-29 02:54:30 +07:00
|
|
|
kernfs_put(sd);
|
2013-11-29 02:54:27 +07:00
|
|
|
}
|
2006-03-20 13:53:53 +07:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(sysfs_notify);
|
|
|
|
|
2013-11-29 02:54:21 +07:00
|
|
|
static const struct kernfs_ops sysfs_file_kfops_empty = {
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct kernfs_ops sysfs_file_kfops_ro = {
|
|
|
|
.seq_show = sysfs_kf_seq_show,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct kernfs_ops sysfs_file_kfops_wo = {
|
|
|
|
.write = sysfs_kf_write,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct kernfs_ops sysfs_file_kfops_rw = {
|
|
|
|
.seq_show = sysfs_kf_seq_show,
|
|
|
|
.write = sysfs_kf_write,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct kernfs_ops sysfs_bin_kfops_ro = {
|
|
|
|
.read = sysfs_kf_bin_read,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct kernfs_ops sysfs_bin_kfops_wo = {
|
|
|
|
.write = sysfs_kf_bin_write,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct kernfs_ops sysfs_bin_kfops_rw = {
|
|
|
|
.read = sysfs_kf_bin_read,
|
|
|
|
.write = sysfs_kf_bin_write,
|
|
|
|
.mmap = sysfs_kf_bin_mmap,
|
|
|
|
};
|
|
|
|
|
2013-09-12 09:29:04 +07:00
|
|
|
int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd,
|
2013-11-29 02:54:23 +07:00
|
|
|
const struct attribute *attr, bool is_bin,
|
2013-11-29 02:54:24 +07:00
|
|
|
umode_t mode, const void *ns)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2013-11-29 02:54:29 +07:00
|
|
|
struct lock_class_key *key = NULL;
|
2013-11-29 02:54:21 +07:00
|
|
|
const struct kernfs_ops *ops;
|
2007-06-14 01:45:14 +07:00
|
|
|
struct sysfs_dirent *sd;
|
2013-11-29 02:54:22 +07:00
|
|
|
loff_t size;
|
2005-04-17 05:20:36 +07:00
|
|
|
|
2013-11-29 02:54:23 +07:00
|
|
|
if (!is_bin) {
|
2013-11-29 02:54:21 +07:00
|
|
|
struct kobject *kobj = dir_sd->priv;
|
|
|
|
const struct sysfs_ops *sysfs_ops = kobj->ktype->sysfs_ops;
|
|
|
|
|
|
|
|
/* every kobject with an attribute needs a ktype assigned */
|
|
|
|
if (WARN(!sysfs_ops, KERN_ERR
|
|
|
|
"missing sysfs attribute operations for kobject: %s\n",
|
|
|
|
kobject_name(kobj)))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (sysfs_ops->show && sysfs_ops->store)
|
|
|
|
ops = &sysfs_file_kfops_rw;
|
|
|
|
else if (sysfs_ops->show)
|
|
|
|
ops = &sysfs_file_kfops_ro;
|
|
|
|
else if (sysfs_ops->store)
|
|
|
|
ops = &sysfs_file_kfops_wo;
|
|
|
|
else
|
|
|
|
ops = &sysfs_file_kfops_empty;
|
2013-11-29 02:54:22 +07:00
|
|
|
|
|
|
|
size = PAGE_SIZE;
|
2013-11-29 02:54:21 +07:00
|
|
|
} else {
|
|
|
|
struct bin_attribute *battr = (void *)attr;
|
|
|
|
|
|
|
|
if ((battr->read && battr->write) || battr->mmap)
|
|
|
|
ops = &sysfs_bin_kfops_rw;
|
|
|
|
else if (battr->read)
|
|
|
|
ops = &sysfs_bin_kfops_ro;
|
|
|
|
else if (battr->write)
|
|
|
|
ops = &sysfs_bin_kfops_wo;
|
|
|
|
else
|
|
|
|
ops = &sysfs_file_kfops_empty;
|
2013-11-29 02:54:22 +07:00
|
|
|
|
|
|
|
size = battr->size;
|
2013-11-29 02:54:21 +07:00
|
|
|
}
|
|
|
|
|
2013-11-29 02:54:29 +07:00
|
|
|
#ifdef CONFIG_DEBUG_LOCK_ALLOC
|
|
|
|
if (!attr->ignore_lockdep)
|
|
|
|
key = attr->key ?: (struct lock_class_key *)&attr->skey;
|
|
|
|
#endif
|
|
|
|
sd = kernfs_create_file_ns_key(dir_sd, attr->name, mode, size,
|
|
|
|
ops, (void *)attr, ns, key);
|
2013-11-29 02:54:24 +07:00
|
|
|
if (IS_ERR(sd)) {
|
|
|
|
if (PTR_ERR(sd) == -EEXIST)
|
|
|
|
sysfs_warn_dup(dir_sd, attr->name);
|
|
|
|
return PTR_ERR(sd);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-03-21 08:47:52 +07:00
|
|
|
int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
|
2013-11-29 02:54:23 +07:00
|
|
|
bool is_bin)
|
2008-03-21 08:47:52 +07:00
|
|
|
{
|
2013-11-29 02:54:23 +07:00
|
|
|
return sysfs_add_file_mode_ns(dir_sd, attr, is_bin, attr->mode, NULL);
|
2008-03-21 08:47:52 +07:00
|
|
|
}
|
|
|
|
|
2005-04-17 05:20:36 +07:00
|
|
|
/**
|
2013-09-12 09:29:04 +07:00
|
|
|
* sysfs_create_file_ns - create an attribute file for an object with custom ns
|
|
|
|
* @kobj: object we're creating for
|
|
|
|
* @attr: attribute descriptor
|
|
|
|
* @ns: namespace the new file should belong to
|
2005-04-17 05:20:36 +07:00
|
|
|
*/
|
2013-09-12 09:29:04 +07:00
|
|
|
int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
|
|
|
|
const void *ns)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2007-06-14 02:27:22 +07:00
|
|
|
BUG_ON(!kobj || !kobj->sd || !attr);
|
2005-04-17 05:20:36 +07:00
|
|
|
|
2013-11-29 02:54:23 +07:00
|
|
|
return sysfs_add_file_mode_ns(kobj->sd, attr, false, attr->mode, ns);
|
2005-04-17 05:20:36 +07:00
|
|
|
|
|
|
|
}
|
2013-09-12 09:29:04 +07:00
|
|
|
EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
|
2005-04-17 05:20:36 +07:00
|
|
|
|
2010-01-05 18:48:01 +07:00
|
|
|
int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr)
|
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; ptr[i] && !err; i++)
|
|
|
|
err = sysfs_create_file(kobj, ptr[i]);
|
|
|
|
if (err)
|
|
|
|
while (--i >= 0)
|
|
|
|
sysfs_remove_file(kobj, ptr[i]);
|
|
|
|
return err;
|
|
|
|
}
|
2013-08-22 06:17:47 +07:00
|
|
|
EXPORT_SYMBOL_GPL(sysfs_create_files);
|
2005-04-17 05:20:36 +07:00
|
|
|
|
2007-02-21 03:02:44 +07:00
|
|
|
/**
|
|
|
|
* sysfs_add_file_to_group - add an attribute file to a pre-existing group.
|
|
|
|
* @kobj: object we're acting for.
|
|
|
|
* @attr: attribute descriptor.
|
|
|
|
* @group: group name.
|
|
|
|
*/
|
|
|
|
int sysfs_add_file_to_group(struct kobject *kobj,
|
|
|
|
const struct attribute *attr, const char *group)
|
|
|
|
{
|
2007-06-14 02:27:22 +07:00
|
|
|
struct sysfs_dirent *dir_sd;
|
2007-02-21 03:02:44 +07:00
|
|
|
int error;
|
|
|
|
|
2013-11-29 02:54:30 +07:00
|
|
|
if (group) {
|
|
|
|
dir_sd = kernfs_find_and_get(kobj->sd, group);
|
|
|
|
} else {
|
|
|
|
dir_sd = kobj->sd;
|
|
|
|
kernfs_get(dir_sd);
|
|
|
|
}
|
2008-01-03 07:44:05 +07:00
|
|
|
|
2007-06-14 02:27:22 +07:00
|
|
|
if (!dir_sd)
|
|
|
|
return -ENOENT;
|
|
|
|
|
2013-11-29 02:54:23 +07:00
|
|
|
error = sysfs_add_file(dir_sd, attr, false);
|
2013-11-29 02:54:30 +07:00
|
|
|
kernfs_put(dir_sd);
|
2007-06-14 02:27:22 +07:00
|
|
|
|
2007-02-21 03:02:44 +07:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
|
|
|
|
|
2005-04-19 11:57:32 +07:00
|
|
|
/**
|
|
|
|
* sysfs_chmod_file - update the modified mode value on an object attribute.
|
|
|
|
* @kobj: object we're acting for.
|
|
|
|
* @attr: attribute descriptor.
|
|
|
|
* @mode: file permissions.
|
|
|
|
*
|
|
|
|
*/
|
2010-07-02 21:54:05 +07:00
|
|
|
int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
|
2011-07-24 14:40:40 +07:00
|
|
|
umode_t mode)
|
2005-04-19 11:57:32 +07:00
|
|
|
{
|
2009-11-21 07:08:54 +07:00
|
|
|
struct sysfs_dirent *sd;
|
2005-07-30 02:13:35 +07:00
|
|
|
struct iattr newattrs;
|
2007-06-14 02:27:25 +07:00
|
|
|
int rc;
|
|
|
|
|
2013-11-29 02:54:30 +07:00
|
|
|
sd = kernfs_find_and_get(kobj->sd, attr->name);
|
2009-11-21 07:08:54 +07:00
|
|
|
if (!sd)
|
2013-11-24 05:21:52 +07:00
|
|
|
return -ENOENT;
|
2007-09-20 14:05:10 +07:00
|
|
|
|
2009-11-21 07:08:54 +07:00
|
|
|
newattrs.ia_mode = (mode & S_IALLUGO) | (sd->s_mode & ~S_IALLUGO);
|
2009-11-08 14:27:02 +07:00
|
|
|
newattrs.ia_valid = ATTR_MODE;
|
2007-09-20 14:05:10 +07:00
|
|
|
|
2013-11-24 05:21:52 +07:00
|
|
|
rc = kernfs_setattr(sd, &newattrs);
|
|
|
|
|
2013-11-29 02:54:30 +07:00
|
|
|
kernfs_put(sd);
|
2007-06-14 02:27:25 +07:00
|
|
|
return rc;
|
2005-04-19 11:57:32 +07:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(sysfs_chmod_file);
|
|
|
|
|
2005-04-17 05:20:36 +07:00
|
|
|
/**
|
2013-09-12 09:29:04 +07:00
|
|
|
* sysfs_remove_file_ns - remove an object attribute with a custom ns tag
|
|
|
|
* @kobj: object we're acting for
|
|
|
|
* @attr: attribute descriptor
|
|
|
|
* @ns: namespace tag of the file to remove
|
2005-04-17 05:20:36 +07:00
|
|
|
*
|
2013-09-12 09:29:04 +07:00
|
|
|
* Hash the attribute name and namespace tag and kill the victim.
|
2005-04-17 05:20:36 +07:00
|
|
|
*/
|
2013-09-12 09:29:04 +07:00
|
|
|
void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
|
|
|
|
const void *ns)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2013-09-12 09:29:04 +07:00
|
|
|
struct sysfs_dirent *dir_sd = kobj->sd;
|
2011-10-13 04:53:38 +07:00
|
|
|
|
2013-11-24 05:21:49 +07:00
|
|
|
kernfs_remove_by_name_ns(dir_sd, attr->name, ns);
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
2013-09-12 09:29:04 +07:00
|
|
|
EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
|
2005-04-17 05:20:36 +07:00
|
|
|
|
2013-08-22 06:28:26 +07:00
|
|
|
void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
|
2010-01-05 18:48:01 +07:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; ptr[i]; i++)
|
|
|
|
sysfs_remove_file(kobj, ptr[i]);
|
|
|
|
}
|
2013-08-22 06:17:47 +07:00
|
|
|
EXPORT_SYMBOL_GPL(sysfs_remove_files);
|
2005-04-17 05:20:36 +07:00
|
|
|
|
2007-02-21 03:02:44 +07:00
|
|
|
/**
|
|
|
|
* sysfs_remove_file_from_group - remove an attribute file from a group.
|
|
|
|
* @kobj: object we're acting for.
|
|
|
|
* @attr: attribute descriptor.
|
|
|
|
* @group: group name.
|
|
|
|
*/
|
|
|
|
void sysfs_remove_file_from_group(struct kobject *kobj,
|
|
|
|
const struct attribute *attr, const char *group)
|
|
|
|
{
|
2007-06-14 02:27:22 +07:00
|
|
|
struct sysfs_dirent *dir_sd;
|
2007-02-21 03:02:44 +07:00
|
|
|
|
2013-11-29 02:54:30 +07:00
|
|
|
if (group) {
|
|
|
|
dir_sd = kernfs_find_and_get(kobj->sd, group);
|
|
|
|
} else {
|
|
|
|
dir_sd = kobj->sd;
|
|
|
|
kernfs_get(dir_sd);
|
|
|
|
}
|
|
|
|
|
2007-06-14 02:27:22 +07:00
|
|
|
if (dir_sd) {
|
2013-11-24 05:21:49 +07:00
|
|
|
kernfs_remove_by_name(dir_sd, attr->name);
|
2013-11-29 02:54:30 +07:00
|
|
|
kernfs_put(dir_sd);
|
2007-02-21 03:02:44 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
|
|
|
|
|
2013-10-02 04:42:09 +07:00
|
|
|
/**
|
|
|
|
* sysfs_create_bin_file - create binary file for object.
|
|
|
|
* @kobj: object.
|
|
|
|
* @attr: attribute descriptor.
|
|
|
|
*/
|
|
|
|
int sysfs_create_bin_file(struct kobject *kobj,
|
|
|
|
const struct bin_attribute *attr)
|
|
|
|
{
|
|
|
|
BUG_ON(!kobj || !kobj->sd || !attr);
|
|
|
|
|
2013-11-29 02:54:23 +07:00
|
|
|
return sysfs_add_file(kobj->sd, &attr->attr, true);
|
2013-10-02 04:42:09 +07:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sysfs_remove_bin_file - remove binary file for object.
|
|
|
|
* @kobj: object.
|
|
|
|
* @attr: attribute descriptor.
|
|
|
|
*/
|
|
|
|
void sysfs_remove_bin_file(struct kobject *kobj,
|
|
|
|
const struct bin_attribute *attr)
|
|
|
|
{
|
2013-11-24 05:21:49 +07:00
|
|
|
kernfs_remove_by_name(kobj->sd, attr->attr.name);
|
2013-10-02 04:42:09 +07:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);
|
|
|
|
|
2007-03-16 02:50:34 +07:00
|
|
|
struct sysfs_schedule_callback_struct {
|
2009-03-14 01:07:36 +07:00
|
|
|
struct list_head workq_list;
|
|
|
|
struct kobject *kobj;
|
2007-03-16 02:50:34 +07:00
|
|
|
void (*func)(void *);
|
|
|
|
void *data;
|
2007-04-26 14:12:04 +07:00
|
|
|
struct module *owner;
|
2007-03-16 02:50:34 +07:00
|
|
|
struct work_struct work;
|
|
|
|
};
|
|
|
|
|
sysfs: don't use global workqueue in sysfs_schedule_callback()
A sysfs attribute using sysfs_schedule_callback() to commit suicide
may end up calling device_unregister(), which will eventually call
a driver's ->remove function.
Drivers may call flush_scheduled_work() in their shutdown routines,
in which case lockdep will complain with something like the following:
=============================================
[ INFO: possible recursive locking detected ]
2.6.29-rc8-kk #1
---------------------------------------------
events/4/56 is trying to acquire lock:
(events){--..}, at: [<ffffffff80257fc0>] flush_workqueue+0x0/0xa0
but task is already holding lock:
(events){--..}, at: [<ffffffff80257648>] run_workqueue+0x108/0x230
other info that might help us debug this:
3 locks held by events/4/56:
#0: (events){--..}, at: [<ffffffff80257648>] run_workqueue+0x108/0x230
#1: (&ss->work){--..}, at: [<ffffffff80257648>] run_workqueue+0x108/0x230
#2: (pci_remove_rescan_mutex){--..}, at: [<ffffffff803c10d1>] remove_callback+0x21/0x40
stack backtrace:
Pid: 56, comm: events/4 Not tainted 2.6.29-rc8-kk #1
Call Trace:
[<ffffffff8026dfcd>] validate_chain+0xb7d/0x1260
[<ffffffff8026eade>] __lock_acquire+0x42e/0xa40
[<ffffffff8026f148>] lock_acquire+0x58/0x80
[<ffffffff80257fc0>] ? flush_workqueue+0x0/0xa0
[<ffffffff8025800d>] flush_workqueue+0x4d/0xa0
[<ffffffff80257fc0>] ? flush_workqueue+0x0/0xa0
[<ffffffff80258070>] flush_scheduled_work+0x10/0x20
[<ffffffffa0144065>] e1000_remove+0x55/0xfe [e1000e]
[<ffffffff8033ee30>] ? sysfs_schedule_callback_work+0x0/0x50
[<ffffffff803bfeb2>] pci_device_remove+0x32/0x70
[<ffffffff80441da9>] __device_release_driver+0x59/0x90
[<ffffffff80441edb>] device_release_driver+0x2b/0x40
[<ffffffff804419d6>] bus_remove_device+0xa6/0x120
[<ffffffff8043e46b>] device_del+0x12b/0x190
[<ffffffff8043e4f6>] device_unregister+0x26/0x70
[<ffffffff803ba969>] pci_stop_dev+0x49/0x60
[<ffffffff803baab0>] pci_remove_bus_device+0x40/0xc0
[<ffffffff803c10d9>] remove_callback+0x29/0x40
[<ffffffff8033ee4f>] sysfs_schedule_callback_work+0x1f/0x50
[<ffffffff8025769a>] run_workqueue+0x15a/0x230
[<ffffffff80257648>] ? run_workqueue+0x108/0x230
[<ffffffff8025846f>] worker_thread+0x9f/0x100
[<ffffffff8025bce0>] ? autoremove_wake_function+0x0/0x40
[<ffffffff802583d0>] ? worker_thread+0x0/0x100
[<ffffffff8025b89d>] kthread+0x4d/0x80
[<ffffffff8020d4ba>] child_rip+0xa/0x20
[<ffffffff8020cebc>] ? restore_args+0x0/0x30
[<ffffffff8025b850>] ? kthread+0x0/0x80
[<ffffffff8020d4b0>] ? child_rip+0x0/0x20
Although we know that the device_unregister path will never acquire
a lock that a driver might try to acquire in its ->remove, in general
we should never attempt to flush a workqueue from within the same
workqueue, and lockdep rightly complains.
So as long as sysfs attributes cannot commit suicide directly and we
are stuck with this callback mechanism, put the sysfs callbacks on
their own workqueue instead of the global one.
This has the side benefit that if a suicidal sysfs attribute kicks
off a long chain of ->remove callbacks, we no longer induce a long
delay on the global queue.
This also fixes a missing module_put in the error path introduced
by sysfs-only-allow-one-scheduled-removal-callback-per-kobj.patch.
We never destroy the workqueue, but I'm not sure that's a
problem.
Reported-by: Kenji Kaneshige <kaneshige.kenji@jp.fujitsu.com>
Tested-by: Kenji Kaneshige <kaneshige.kenji@jp.fujitsu.com>
Signed-off-by: Alex Chiang <achiang@hp.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
2009-03-26 04:11:36 +07:00
|
|
|
static struct workqueue_struct *sysfs_workqueue;
|
2009-03-14 01:07:36 +07:00
|
|
|
static DEFINE_MUTEX(sysfs_workq_mutex);
|
|
|
|
static LIST_HEAD(sysfs_workq);
|
2007-03-16 02:50:34 +07:00
|
|
|
static void sysfs_schedule_callback_work(struct work_struct *work)
|
|
|
|
{
|
|
|
|
struct sysfs_schedule_callback_struct *ss = container_of(work,
|
|
|
|
struct sysfs_schedule_callback_struct, work);
|
|
|
|
|
|
|
|
(ss->func)(ss->data);
|
|
|
|
kobject_put(ss->kobj);
|
2007-04-26 14:12:04 +07:00
|
|
|
module_put(ss->owner);
|
2009-03-14 01:07:36 +07:00
|
|
|
mutex_lock(&sysfs_workq_mutex);
|
|
|
|
list_del(&ss->workq_list);
|
|
|
|
mutex_unlock(&sysfs_workq_mutex);
|
2007-03-16 02:50:34 +07:00
|
|
|
kfree(ss);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sysfs_schedule_callback - helper to schedule a callback for a kobject
|
|
|
|
* @kobj: object we're acting for.
|
|
|
|
* @func: callback function to invoke later.
|
|
|
|
* @data: argument to pass to @func.
|
2007-04-26 14:12:04 +07:00
|
|
|
* @owner: module owning the callback code
|
2007-03-16 02:50:34 +07:00
|
|
|
*
|
|
|
|
* sysfs attribute methods must not unregister themselves or their parent
|
|
|
|
* kobject (which would amount to the same thing). Attempts to do so will
|
|
|
|
* deadlock, since unregistration is mutually exclusive with driver
|
|
|
|
* callbacks.
|
|
|
|
*
|
|
|
|
* Instead methods can call this routine, which will attempt to allocate
|
|
|
|
* and schedule a workqueue request to call back @func with @data as its
|
|
|
|
* argument in the workqueue's process context. @kobj will be pinned
|
|
|
|
* until @func returns.
|
|
|
|
*
|
|
|
|
* Returns 0 if the request was submitted, -ENOMEM if storage could not
|
2009-03-14 01:07:36 +07:00
|
|
|
* be allocated, -ENODEV if a reference to @owner isn't available,
|
|
|
|
* -EAGAIN if a callback has already been scheduled for @kobj.
|
2007-03-16 02:50:34 +07:00
|
|
|
*/
|
|
|
|
int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
|
2007-04-26 14:12:04 +07:00
|
|
|
void *data, struct module *owner)
|
2007-03-16 02:50:34 +07:00
|
|
|
{
|
2009-03-14 01:07:36 +07:00
|
|
|
struct sysfs_schedule_callback_struct *ss, *tmp;
|
2007-03-16 02:50:34 +07:00
|
|
|
|
2007-04-26 14:12:04 +07:00
|
|
|
if (!try_module_get(owner))
|
|
|
|
return -ENODEV;
|
2009-03-14 01:07:36 +07:00
|
|
|
|
|
|
|
mutex_lock(&sysfs_workq_mutex);
|
|
|
|
list_for_each_entry_safe(ss, tmp, &sysfs_workq, workq_list)
|
|
|
|
if (ss->kobj == kobj) {
|
sysfs: don't use global workqueue in sysfs_schedule_callback()
A sysfs attribute using sysfs_schedule_callback() to commit suicide
may end up calling device_unregister(), which will eventually call
a driver's ->remove function.
Drivers may call flush_scheduled_work() in their shutdown routines,
in which case lockdep will complain with something like the following:
=============================================
[ INFO: possible recursive locking detected ]
2.6.29-rc8-kk #1
---------------------------------------------
events/4/56 is trying to acquire lock:
(events){--..}, at: [<ffffffff80257fc0>] flush_workqueue+0x0/0xa0
but task is already holding lock:
(events){--..}, at: [<ffffffff80257648>] run_workqueue+0x108/0x230
other info that might help us debug this:
3 locks held by events/4/56:
#0: (events){--..}, at: [<ffffffff80257648>] run_workqueue+0x108/0x230
#1: (&ss->work){--..}, at: [<ffffffff80257648>] run_workqueue+0x108/0x230
#2: (pci_remove_rescan_mutex){--..}, at: [<ffffffff803c10d1>] remove_callback+0x21/0x40
stack backtrace:
Pid: 56, comm: events/4 Not tainted 2.6.29-rc8-kk #1
Call Trace:
[<ffffffff8026dfcd>] validate_chain+0xb7d/0x1260
[<ffffffff8026eade>] __lock_acquire+0x42e/0xa40
[<ffffffff8026f148>] lock_acquire+0x58/0x80
[<ffffffff80257fc0>] ? flush_workqueue+0x0/0xa0
[<ffffffff8025800d>] flush_workqueue+0x4d/0xa0
[<ffffffff80257fc0>] ? flush_workqueue+0x0/0xa0
[<ffffffff80258070>] flush_scheduled_work+0x10/0x20
[<ffffffffa0144065>] e1000_remove+0x55/0xfe [e1000e]
[<ffffffff8033ee30>] ? sysfs_schedule_callback_work+0x0/0x50
[<ffffffff803bfeb2>] pci_device_remove+0x32/0x70
[<ffffffff80441da9>] __device_release_driver+0x59/0x90
[<ffffffff80441edb>] device_release_driver+0x2b/0x40
[<ffffffff804419d6>] bus_remove_device+0xa6/0x120
[<ffffffff8043e46b>] device_del+0x12b/0x190
[<ffffffff8043e4f6>] device_unregister+0x26/0x70
[<ffffffff803ba969>] pci_stop_dev+0x49/0x60
[<ffffffff803baab0>] pci_remove_bus_device+0x40/0xc0
[<ffffffff803c10d9>] remove_callback+0x29/0x40
[<ffffffff8033ee4f>] sysfs_schedule_callback_work+0x1f/0x50
[<ffffffff8025769a>] run_workqueue+0x15a/0x230
[<ffffffff80257648>] ? run_workqueue+0x108/0x230
[<ffffffff8025846f>] worker_thread+0x9f/0x100
[<ffffffff8025bce0>] ? autoremove_wake_function+0x0/0x40
[<ffffffff802583d0>] ? worker_thread+0x0/0x100
[<ffffffff8025b89d>] kthread+0x4d/0x80
[<ffffffff8020d4ba>] child_rip+0xa/0x20
[<ffffffff8020cebc>] ? restore_args+0x0/0x30
[<ffffffff8025b850>] ? kthread+0x0/0x80
[<ffffffff8020d4b0>] ? child_rip+0x0/0x20
Although we know that the device_unregister path will never acquire
a lock that a driver might try to acquire in its ->remove, in general
we should never attempt to flush a workqueue from within the same
workqueue, and lockdep rightly complains.
So as long as sysfs attributes cannot commit suicide directly and we
are stuck with this callback mechanism, put the sysfs callbacks on
their own workqueue instead of the global one.
This has the side benefit that if a suicidal sysfs attribute kicks
off a long chain of ->remove callbacks, we no longer induce a long
delay on the global queue.
This also fixes a missing module_put in the error path introduced
by sysfs-only-allow-one-scheduled-removal-callback-per-kobj.patch.
We never destroy the workqueue, but I'm not sure that's a
problem.
Reported-by: Kenji Kaneshige <kaneshige.kenji@jp.fujitsu.com>
Tested-by: Kenji Kaneshige <kaneshige.kenji@jp.fujitsu.com>
Signed-off-by: Alex Chiang <achiang@hp.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
2009-03-26 04:11:36 +07:00
|
|
|
module_put(owner);
|
2009-03-14 01:07:36 +07:00
|
|
|
mutex_unlock(&sysfs_workq_mutex);
|
|
|
|
return -EAGAIN;
|
|
|
|
}
|
|
|
|
mutex_unlock(&sysfs_workq_mutex);
|
|
|
|
|
sysfs: don't use global workqueue in sysfs_schedule_callback()
A sysfs attribute using sysfs_schedule_callback() to commit suicide
may end up calling device_unregister(), which will eventually call
a driver's ->remove function.
Drivers may call flush_scheduled_work() in their shutdown routines,
in which case lockdep will complain with something like the following:
=============================================
[ INFO: possible recursive locking detected ]
2.6.29-rc8-kk #1
---------------------------------------------
events/4/56 is trying to acquire lock:
(events){--..}, at: [<ffffffff80257fc0>] flush_workqueue+0x0/0xa0
but task is already holding lock:
(events){--..}, at: [<ffffffff80257648>] run_workqueue+0x108/0x230
other info that might help us debug this:
3 locks held by events/4/56:
#0: (events){--..}, at: [<ffffffff80257648>] run_workqueue+0x108/0x230
#1: (&ss->work){--..}, at: [<ffffffff80257648>] run_workqueue+0x108/0x230
#2: (pci_remove_rescan_mutex){--..}, at: [<ffffffff803c10d1>] remove_callback+0x21/0x40
stack backtrace:
Pid: 56, comm: events/4 Not tainted 2.6.29-rc8-kk #1
Call Trace:
[<ffffffff8026dfcd>] validate_chain+0xb7d/0x1260
[<ffffffff8026eade>] __lock_acquire+0x42e/0xa40
[<ffffffff8026f148>] lock_acquire+0x58/0x80
[<ffffffff80257fc0>] ? flush_workqueue+0x0/0xa0
[<ffffffff8025800d>] flush_workqueue+0x4d/0xa0
[<ffffffff80257fc0>] ? flush_workqueue+0x0/0xa0
[<ffffffff80258070>] flush_scheduled_work+0x10/0x20
[<ffffffffa0144065>] e1000_remove+0x55/0xfe [e1000e]
[<ffffffff8033ee30>] ? sysfs_schedule_callback_work+0x0/0x50
[<ffffffff803bfeb2>] pci_device_remove+0x32/0x70
[<ffffffff80441da9>] __device_release_driver+0x59/0x90
[<ffffffff80441edb>] device_release_driver+0x2b/0x40
[<ffffffff804419d6>] bus_remove_device+0xa6/0x120
[<ffffffff8043e46b>] device_del+0x12b/0x190
[<ffffffff8043e4f6>] device_unregister+0x26/0x70
[<ffffffff803ba969>] pci_stop_dev+0x49/0x60
[<ffffffff803baab0>] pci_remove_bus_device+0x40/0xc0
[<ffffffff803c10d9>] remove_callback+0x29/0x40
[<ffffffff8033ee4f>] sysfs_schedule_callback_work+0x1f/0x50
[<ffffffff8025769a>] run_workqueue+0x15a/0x230
[<ffffffff80257648>] ? run_workqueue+0x108/0x230
[<ffffffff8025846f>] worker_thread+0x9f/0x100
[<ffffffff8025bce0>] ? autoremove_wake_function+0x0/0x40
[<ffffffff802583d0>] ? worker_thread+0x0/0x100
[<ffffffff8025b89d>] kthread+0x4d/0x80
[<ffffffff8020d4ba>] child_rip+0xa/0x20
[<ffffffff8020cebc>] ? restore_args+0x0/0x30
[<ffffffff8025b850>] ? kthread+0x0/0x80
[<ffffffff8020d4b0>] ? child_rip+0x0/0x20
Although we know that the device_unregister path will never acquire
a lock that a driver might try to acquire in its ->remove, in general
we should never attempt to flush a workqueue from within the same
workqueue, and lockdep rightly complains.
So as long as sysfs attributes cannot commit suicide directly and we
are stuck with this callback mechanism, put the sysfs callbacks on
their own workqueue instead of the global one.
This has the side benefit that if a suicidal sysfs attribute kicks
off a long chain of ->remove callbacks, we no longer induce a long
delay on the global queue.
This also fixes a missing module_put in the error path introduced
by sysfs-only-allow-one-scheduled-removal-callback-per-kobj.patch.
We never destroy the workqueue, but I'm not sure that's a
problem.
Reported-by: Kenji Kaneshige <kaneshige.kenji@jp.fujitsu.com>
Tested-by: Kenji Kaneshige <kaneshige.kenji@jp.fujitsu.com>
Signed-off-by: Alex Chiang <achiang@hp.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
2009-03-26 04:11:36 +07:00
|
|
|
if (sysfs_workqueue == NULL) {
|
2009-05-08 02:36:53 +07:00
|
|
|
sysfs_workqueue = create_singlethread_workqueue("sysfsd");
|
sysfs: don't use global workqueue in sysfs_schedule_callback()
A sysfs attribute using sysfs_schedule_callback() to commit suicide
may end up calling device_unregister(), which will eventually call
a driver's ->remove function.
Drivers may call flush_scheduled_work() in their shutdown routines,
in which case lockdep will complain with something like the following:
=============================================
[ INFO: possible recursive locking detected ]
2.6.29-rc8-kk #1
---------------------------------------------
events/4/56 is trying to acquire lock:
(events){--..}, at: [<ffffffff80257fc0>] flush_workqueue+0x0/0xa0
but task is already holding lock:
(events){--..}, at: [<ffffffff80257648>] run_workqueue+0x108/0x230
other info that might help us debug this:
3 locks held by events/4/56:
#0: (events){--..}, at: [<ffffffff80257648>] run_workqueue+0x108/0x230
#1: (&ss->work){--..}, at: [<ffffffff80257648>] run_workqueue+0x108/0x230
#2: (pci_remove_rescan_mutex){--..}, at: [<ffffffff803c10d1>] remove_callback+0x21/0x40
stack backtrace:
Pid: 56, comm: events/4 Not tainted 2.6.29-rc8-kk #1
Call Trace:
[<ffffffff8026dfcd>] validate_chain+0xb7d/0x1260
[<ffffffff8026eade>] __lock_acquire+0x42e/0xa40
[<ffffffff8026f148>] lock_acquire+0x58/0x80
[<ffffffff80257fc0>] ? flush_workqueue+0x0/0xa0
[<ffffffff8025800d>] flush_workqueue+0x4d/0xa0
[<ffffffff80257fc0>] ? flush_workqueue+0x0/0xa0
[<ffffffff80258070>] flush_scheduled_work+0x10/0x20
[<ffffffffa0144065>] e1000_remove+0x55/0xfe [e1000e]
[<ffffffff8033ee30>] ? sysfs_schedule_callback_work+0x0/0x50
[<ffffffff803bfeb2>] pci_device_remove+0x32/0x70
[<ffffffff80441da9>] __device_release_driver+0x59/0x90
[<ffffffff80441edb>] device_release_driver+0x2b/0x40
[<ffffffff804419d6>] bus_remove_device+0xa6/0x120
[<ffffffff8043e46b>] device_del+0x12b/0x190
[<ffffffff8043e4f6>] device_unregister+0x26/0x70
[<ffffffff803ba969>] pci_stop_dev+0x49/0x60
[<ffffffff803baab0>] pci_remove_bus_device+0x40/0xc0
[<ffffffff803c10d9>] remove_callback+0x29/0x40
[<ffffffff8033ee4f>] sysfs_schedule_callback_work+0x1f/0x50
[<ffffffff8025769a>] run_workqueue+0x15a/0x230
[<ffffffff80257648>] ? run_workqueue+0x108/0x230
[<ffffffff8025846f>] worker_thread+0x9f/0x100
[<ffffffff8025bce0>] ? autoremove_wake_function+0x0/0x40
[<ffffffff802583d0>] ? worker_thread+0x0/0x100
[<ffffffff8025b89d>] kthread+0x4d/0x80
[<ffffffff8020d4ba>] child_rip+0xa/0x20
[<ffffffff8020cebc>] ? restore_args+0x0/0x30
[<ffffffff8025b850>] ? kthread+0x0/0x80
[<ffffffff8020d4b0>] ? child_rip+0x0/0x20
Although we know that the device_unregister path will never acquire
a lock that a driver might try to acquire in its ->remove, in general
we should never attempt to flush a workqueue from within the same
workqueue, and lockdep rightly complains.
So as long as sysfs attributes cannot commit suicide directly and we
are stuck with this callback mechanism, put the sysfs callbacks on
their own workqueue instead of the global one.
This has the side benefit that if a suicidal sysfs attribute kicks
off a long chain of ->remove callbacks, we no longer induce a long
delay on the global queue.
This also fixes a missing module_put in the error path introduced
by sysfs-only-allow-one-scheduled-removal-callback-per-kobj.patch.
We never destroy the workqueue, but I'm not sure that's a
problem.
Reported-by: Kenji Kaneshige <kaneshige.kenji@jp.fujitsu.com>
Tested-by: Kenji Kaneshige <kaneshige.kenji@jp.fujitsu.com>
Signed-off-by: Alex Chiang <achiang@hp.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
2009-03-26 04:11:36 +07:00
|
|
|
if (sysfs_workqueue == NULL) {
|
|
|
|
module_put(owner);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-16 02:50:34 +07:00
|
|
|
ss = kmalloc(sizeof(*ss), GFP_KERNEL);
|
2007-04-26 14:12:04 +07:00
|
|
|
if (!ss) {
|
|
|
|
module_put(owner);
|
2007-03-16 02:50:34 +07:00
|
|
|
return -ENOMEM;
|
2007-04-26 14:12:04 +07:00
|
|
|
}
|
2007-03-16 02:50:34 +07:00
|
|
|
kobject_get(kobj);
|
|
|
|
ss->kobj = kobj;
|
|
|
|
ss->func = func;
|
|
|
|
ss->data = data;
|
2007-04-26 14:12:04 +07:00
|
|
|
ss->owner = owner;
|
2007-03-16 02:50:34 +07:00
|
|
|
INIT_WORK(&ss->work, sysfs_schedule_callback_work);
|
2009-03-14 01:07:36 +07:00
|
|
|
INIT_LIST_HEAD(&ss->workq_list);
|
|
|
|
mutex_lock(&sysfs_workq_mutex);
|
|
|
|
list_add_tail(&ss->workq_list, &sysfs_workq);
|
|
|
|
mutex_unlock(&sysfs_workq_mutex);
|
sysfs: don't use global workqueue in sysfs_schedule_callback()
A sysfs attribute using sysfs_schedule_callback() to commit suicide
may end up calling device_unregister(), which will eventually call
a driver's ->remove function.
Drivers may call flush_scheduled_work() in their shutdown routines,
in which case lockdep will complain with something like the following:
=============================================
[ INFO: possible recursive locking detected ]
2.6.29-rc8-kk #1
---------------------------------------------
events/4/56 is trying to acquire lock:
(events){--..}, at: [<ffffffff80257fc0>] flush_workqueue+0x0/0xa0
but task is already holding lock:
(events){--..}, at: [<ffffffff80257648>] run_workqueue+0x108/0x230
other info that might help us debug this:
3 locks held by events/4/56:
#0: (events){--..}, at: [<ffffffff80257648>] run_workqueue+0x108/0x230
#1: (&ss->work){--..}, at: [<ffffffff80257648>] run_workqueue+0x108/0x230
#2: (pci_remove_rescan_mutex){--..}, at: [<ffffffff803c10d1>] remove_callback+0x21/0x40
stack backtrace:
Pid: 56, comm: events/4 Not tainted 2.6.29-rc8-kk #1
Call Trace:
[<ffffffff8026dfcd>] validate_chain+0xb7d/0x1260
[<ffffffff8026eade>] __lock_acquire+0x42e/0xa40
[<ffffffff8026f148>] lock_acquire+0x58/0x80
[<ffffffff80257fc0>] ? flush_workqueue+0x0/0xa0
[<ffffffff8025800d>] flush_workqueue+0x4d/0xa0
[<ffffffff80257fc0>] ? flush_workqueue+0x0/0xa0
[<ffffffff80258070>] flush_scheduled_work+0x10/0x20
[<ffffffffa0144065>] e1000_remove+0x55/0xfe [e1000e]
[<ffffffff8033ee30>] ? sysfs_schedule_callback_work+0x0/0x50
[<ffffffff803bfeb2>] pci_device_remove+0x32/0x70
[<ffffffff80441da9>] __device_release_driver+0x59/0x90
[<ffffffff80441edb>] device_release_driver+0x2b/0x40
[<ffffffff804419d6>] bus_remove_device+0xa6/0x120
[<ffffffff8043e46b>] device_del+0x12b/0x190
[<ffffffff8043e4f6>] device_unregister+0x26/0x70
[<ffffffff803ba969>] pci_stop_dev+0x49/0x60
[<ffffffff803baab0>] pci_remove_bus_device+0x40/0xc0
[<ffffffff803c10d9>] remove_callback+0x29/0x40
[<ffffffff8033ee4f>] sysfs_schedule_callback_work+0x1f/0x50
[<ffffffff8025769a>] run_workqueue+0x15a/0x230
[<ffffffff80257648>] ? run_workqueue+0x108/0x230
[<ffffffff8025846f>] worker_thread+0x9f/0x100
[<ffffffff8025bce0>] ? autoremove_wake_function+0x0/0x40
[<ffffffff802583d0>] ? worker_thread+0x0/0x100
[<ffffffff8025b89d>] kthread+0x4d/0x80
[<ffffffff8020d4ba>] child_rip+0xa/0x20
[<ffffffff8020cebc>] ? restore_args+0x0/0x30
[<ffffffff8025b850>] ? kthread+0x0/0x80
[<ffffffff8020d4b0>] ? child_rip+0x0/0x20
Although we know that the device_unregister path will never acquire
a lock that a driver might try to acquire in its ->remove, in general
we should never attempt to flush a workqueue from within the same
workqueue, and lockdep rightly complains.
So as long as sysfs attributes cannot commit suicide directly and we
are stuck with this callback mechanism, put the sysfs callbacks on
their own workqueue instead of the global one.
This has the side benefit that if a suicidal sysfs attribute kicks
off a long chain of ->remove callbacks, we no longer induce a long
delay on the global queue.
This also fixes a missing module_put in the error path introduced
by sysfs-only-allow-one-scheduled-removal-callback-per-kobj.patch.
We never destroy the workqueue, but I'm not sure that's a
problem.
Reported-by: Kenji Kaneshige <kaneshige.kenji@jp.fujitsu.com>
Tested-by: Kenji Kaneshige <kaneshige.kenji@jp.fujitsu.com>
Signed-off-by: Alex Chiang <achiang@hp.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
2009-03-26 04:11:36 +07:00
|
|
|
queue_work(sysfs_workqueue, &ss->work);
|
2007-03-16 02:50:34 +07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(sysfs_schedule_callback);
|