forked from luck/tmp_suning_uos_patched
9b2db6e189
This is v3.14 fix for the same issue thata8b1474442
("sysfs: give different locking key to regular and bin files") addresses for v3.13. Due to the extensive kernfs reorganization in v3.14 branch, the same fix couldn't be ported as-is. The v3.13 fix was ignored while merging it into v3.14 branch.027a485d12
("sysfs: use a separate locking class for open files depending on mmap") assigned different lockdep key to sysfs_open_file->mutex depending on whether the file implements mmap or not in an attempt to avoid spurious lockdep warning caused by merging of regular and bin file paths. While this restored some of the original behavior of using different locks (at least lockdep is concerned) for the different clases of files. The restoration wasn't full because now the lockdep key assignment depends on whether the file has mmap or not instead of whether it's a regular file or not. This means that bin files which don't implement mmap will get assigned the same lockdep class as regular files. This is problematic because file_operations for bin files still implements the mmap file operation and checking whether the sysfs file actually implements mmap happens in the file operation after grabbing @sysfs_open_file->mutex. We still end up adding locking dependency from mmap locking to sysfs_open_file->mutex to the regular file mutex which triggers spurious circular locking warning. For v3.13,a8b1474442
("sysfs: give different locking key to regular and bin files") fixed it by giving sysfs_open_file->mutex different lockdep keys depending on whether the file is regular or bin instead of whether mmap exists or not; however, due to the way sysfs is now layered behind kernfs, this approach is no longer viable. kernfs can tell whether a sysfs node has mmap implemented or not but can't tell whether a bin file from a regular one. This patch updates kernfs such that kernfs_file_mmap() checks SYSFS_FLAG_HAS_MMAP and bail before grabbing sysfs_open_file->mutex so that it doesn't add spurious locking dependency from mmap to sysfs_open_file->mutex and changes sysfs so that it specifies kernfs_ops->mmap iff the sysfs file implements mmap. Combined, this ensures that sysfs_open_file->mutex is grabbed under mmap path iff the sysfs file actually implements mmap. As sysfs_open_file->mutex is already given a different lockdep key if mmap is implemented, this removes the spurious locking dependency. Signed-off-by: Tejun Heo <tj@kernel.org> Reported-by: Dave Jones <davej@redhat.com> Link: http://lkml.kernel.org/g/20131203184324.GA11320@redhat.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
525 lines
13 KiB
C
525 lines
13 KiB
C
/*
|
|
* 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.
|
|
*/
|
|
|
|
#include <linux/module.h>
|
|
#include <linux/kobject.h>
|
|
#include <linux/kallsyms.h>
|
|
#include <linux/slab.h>
|
|
#include <linux/list.h>
|
|
#include <linux/mutex.h>
|
|
#include <linux/seq_file.h>
|
|
|
|
#include "sysfs.h"
|
|
#include "../kernfs/kernfs-internal.h"
|
|
|
|
/*
|
|
* 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)
|
|
{
|
|
struct kobject *kobj = sd->s_parent->priv;
|
|
|
|
if (sd->s_flags & SYSFS_FLAG_LOCKDEP)
|
|
lockdep_assert_held(sd);
|
|
return kobj->ktype ? kobj->ktype->sysfs_ops : NULL;
|
|
}
|
|
|
|
/*
|
|
* 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.
|
|
*/
|
|
static int sysfs_kf_seq_show(struct seq_file *sf, void *v)
|
|
{
|
|
struct sysfs_open_file *of = sf->private;
|
|
struct kobject *kobj = of->sd->s_parent->priv;
|
|
const struct sysfs_ops *ops = sysfs_file_ops(of->sd);
|
|
ssize_t count;
|
|
char *buf;
|
|
|
|
/* 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;
|
|
}
|
|
|
|
/*
|
|
* Invoke show(). Control may reach here via seq file lseek even
|
|
* if @ops->show() isn't implemented.
|
|
*/
|
|
if (ops->show) {
|
|
count = ops->show(kobj, of->sd->priv, buf);
|
|
if (count < 0)
|
|
return count;
|
|
}
|
|
|
|
/*
|
|
* The code works fine with PAGE_SIZE return but it's likely to
|
|
* indicate truncated result or overflow in normal use cases.
|
|
*/
|
|
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;
|
|
}
|
|
seq_commit(sf, count);
|
|
return 0;
|
|
}
|
|
|
|
static ssize_t sysfs_kf_bin_read(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;
|
|
|
|
if (!count)
|
|
return 0;
|
|
|
|
if (size) {
|
|
if (pos > size)
|
|
return 0;
|
|
if (pos + count > size)
|
|
count = size - pos;
|
|
}
|
|
|
|
if (!battr->read)
|
|
return -EIO;
|
|
|
|
return battr->read(of->file, kobj, battr, buf, pos, count);
|
|
}
|
|
|
|
/* 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)
|
|
{
|
|
const struct sysfs_ops *ops = sysfs_file_ops(of->sd);
|
|
struct kobject *kobj = of->sd->s_parent->priv;
|
|
|
|
if (!count)
|
|
return 0;
|
|
|
|
return ops->store(kobj, of->sd->priv, buf, count);
|
|
}
|
|
|
|
/* 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;
|
|
|
|
if (size) {
|
|
if (size <= pos)
|
|
return 0;
|
|
count = min_t(ssize_t, count, size - pos);
|
|
}
|
|
if (!count)
|
|
return 0;
|
|
|
|
if (!battr->write)
|
|
return -EIO;
|
|
|
|
return battr->write(of->file, kobj, battr, buf, pos, count);
|
|
}
|
|
|
|
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;
|
|
|
|
return battr->mmap(of->file, kobj, battr, vma);
|
|
}
|
|
|
|
void sysfs_notify(struct kobject *k, const char *dir, const char *attr)
|
|
{
|
|
struct sysfs_dirent *sd = k->sd, *tmp;
|
|
|
|
if (sd && dir)
|
|
sd = kernfs_find_and_get(sd, dir);
|
|
else
|
|
kernfs_get(sd);
|
|
|
|
if (sd && attr) {
|
|
tmp = kernfs_find_and_get(sd, attr);
|
|
kernfs_put(sd);
|
|
sd = tmp;
|
|
}
|
|
|
|
if (sd) {
|
|
kernfs_notify(sd);
|
|
kernfs_put(sd);
|
|
}
|
|
}
|
|
EXPORT_SYMBOL_GPL(sysfs_notify);
|
|
|
|
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,
|
|
};
|
|
|
|
static const struct kernfs_ops sysfs_bin_kfops_mmap = {
|
|
.read = sysfs_kf_bin_read,
|
|
.write = sysfs_kf_bin_write,
|
|
.mmap = sysfs_kf_bin_mmap,
|
|
};
|
|
|
|
int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd,
|
|
const struct attribute *attr, bool is_bin,
|
|
umode_t mode, const void *ns)
|
|
{
|
|
struct lock_class_key *key = NULL;
|
|
const struct kernfs_ops *ops;
|
|
struct sysfs_dirent *sd;
|
|
loff_t size;
|
|
|
|
if (!is_bin) {
|
|
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;
|
|
|
|
size = PAGE_SIZE;
|
|
} else {
|
|
struct bin_attribute *battr = (void *)attr;
|
|
|
|
if (battr->mmap)
|
|
ops = &sysfs_bin_kfops_mmap;
|
|
else if (battr->read && battr->write)
|
|
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;
|
|
|
|
size = battr->size;
|
|
}
|
|
|
|
#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);
|
|
if (IS_ERR(sd)) {
|
|
if (PTR_ERR(sd) == -EEXIST)
|
|
sysfs_warn_dup(dir_sd, attr->name);
|
|
return PTR_ERR(sd);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
|
|
bool is_bin)
|
|
{
|
|
return sysfs_add_file_mode_ns(dir_sd, attr, is_bin, attr->mode, NULL);
|
|
}
|
|
|
|
/**
|
|
* 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
|
|
*/
|
|
int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
|
|
const void *ns)
|
|
{
|
|
BUG_ON(!kobj || !kobj->sd || !attr);
|
|
|
|
return sysfs_add_file_mode_ns(kobj->sd, attr, false, attr->mode, ns);
|
|
|
|
}
|
|
EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
|
|
|
|
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;
|
|
}
|
|
EXPORT_SYMBOL_GPL(sysfs_create_files);
|
|
|
|
/**
|
|
* 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)
|
|
{
|
|
struct sysfs_dirent *dir_sd;
|
|
int error;
|
|
|
|
if (group) {
|
|
dir_sd = kernfs_find_and_get(kobj->sd, group);
|
|
} else {
|
|
dir_sd = kobj->sd;
|
|
kernfs_get(dir_sd);
|
|
}
|
|
|
|
if (!dir_sd)
|
|
return -ENOENT;
|
|
|
|
error = sysfs_add_file(dir_sd, attr, false);
|
|
kernfs_put(dir_sd);
|
|
|
|
return error;
|
|
}
|
|
EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
|
|
|
|
/**
|
|
* sysfs_chmod_file - update the modified mode value on an object attribute.
|
|
* @kobj: object we're acting for.
|
|
* @attr: attribute descriptor.
|
|
* @mode: file permissions.
|
|
*
|
|
*/
|
|
int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
|
|
umode_t mode)
|
|
{
|
|
struct sysfs_dirent *sd;
|
|
struct iattr newattrs;
|
|
int rc;
|
|
|
|
sd = kernfs_find_and_get(kobj->sd, attr->name);
|
|
if (!sd)
|
|
return -ENOENT;
|
|
|
|
newattrs.ia_mode = (mode & S_IALLUGO) | (sd->s_mode & ~S_IALLUGO);
|
|
newattrs.ia_valid = ATTR_MODE;
|
|
|
|
rc = kernfs_setattr(sd, &newattrs);
|
|
|
|
kernfs_put(sd);
|
|
return rc;
|
|
}
|
|
EXPORT_SYMBOL_GPL(sysfs_chmod_file);
|
|
|
|
/**
|
|
* 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
|
|
*
|
|
* Hash the attribute name and namespace tag and kill the victim.
|
|
*/
|
|
void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
|
|
const void *ns)
|
|
{
|
|
struct sysfs_dirent *dir_sd = kobj->sd;
|
|
|
|
kernfs_remove_by_name_ns(dir_sd, attr->name, ns);
|
|
}
|
|
EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
|
|
|
|
void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
|
|
{
|
|
int i;
|
|
for (i = 0; ptr[i]; i++)
|
|
sysfs_remove_file(kobj, ptr[i]);
|
|
}
|
|
EXPORT_SYMBOL_GPL(sysfs_remove_files);
|
|
|
|
/**
|
|
* 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)
|
|
{
|
|
struct sysfs_dirent *dir_sd;
|
|
|
|
if (group) {
|
|
dir_sd = kernfs_find_and_get(kobj->sd, group);
|
|
} else {
|
|
dir_sd = kobj->sd;
|
|
kernfs_get(dir_sd);
|
|
}
|
|
|
|
if (dir_sd) {
|
|
kernfs_remove_by_name(dir_sd, attr->name);
|
|
kernfs_put(dir_sd);
|
|
}
|
|
}
|
|
EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
|
|
|
|
/**
|
|
* 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);
|
|
|
|
return sysfs_add_file(kobj->sd, &attr->attr, true);
|
|
}
|
|
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)
|
|
{
|
|
kernfs_remove_by_name(kobj->sd, attr->attr.name);
|
|
}
|
|
EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);
|
|
|
|
struct sysfs_schedule_callback_struct {
|
|
struct list_head workq_list;
|
|
struct kobject *kobj;
|
|
void (*func)(void *);
|
|
void *data;
|
|
struct module *owner;
|
|
struct work_struct work;
|
|
};
|
|
|
|
static struct workqueue_struct *sysfs_workqueue;
|
|
static DEFINE_MUTEX(sysfs_workq_mutex);
|
|
static LIST_HEAD(sysfs_workq);
|
|
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);
|
|
module_put(ss->owner);
|
|
mutex_lock(&sysfs_workq_mutex);
|
|
list_del(&ss->workq_list);
|
|
mutex_unlock(&sysfs_workq_mutex);
|
|
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.
|
|
* @owner: module owning the callback code
|
|
*
|
|
* 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
|
|
* be allocated, -ENODEV if a reference to @owner isn't available,
|
|
* -EAGAIN if a callback has already been scheduled for @kobj.
|
|
*/
|
|
int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
|
|
void *data, struct module *owner)
|
|
{
|
|
struct sysfs_schedule_callback_struct *ss, *tmp;
|
|
|
|
if (!try_module_get(owner))
|
|
return -ENODEV;
|
|
|
|
mutex_lock(&sysfs_workq_mutex);
|
|
list_for_each_entry_safe(ss, tmp, &sysfs_workq, workq_list)
|
|
if (ss->kobj == kobj) {
|
|
module_put(owner);
|
|
mutex_unlock(&sysfs_workq_mutex);
|
|
return -EAGAIN;
|
|
}
|
|
mutex_unlock(&sysfs_workq_mutex);
|
|
|
|
if (sysfs_workqueue == NULL) {
|
|
sysfs_workqueue = create_singlethread_workqueue("sysfsd");
|
|
if (sysfs_workqueue == NULL) {
|
|
module_put(owner);
|
|
return -ENOMEM;
|
|
}
|
|
}
|
|
|
|
ss = kmalloc(sizeof(*ss), GFP_KERNEL);
|
|
if (!ss) {
|
|
module_put(owner);
|
|
return -ENOMEM;
|
|
}
|
|
kobject_get(kobj);
|
|
ss->kobj = kobj;
|
|
ss->func = func;
|
|
ss->data = data;
|
|
ss->owner = owner;
|
|
INIT_WORK(&ss->work, sysfs_schedule_callback_work);
|
|
INIT_LIST_HEAD(&ss->workq_list);
|
|
mutex_lock(&sysfs_workq_mutex);
|
|
list_add_tail(&ss->workq_list, &sysfs_workq);
|
|
mutex_unlock(&sysfs_workq_mutex);
|
|
queue_work(sysfs_workqueue, &ss->work);
|
|
return 0;
|
|
}
|
|
EXPORT_SYMBOL_GPL(sysfs_schedule_callback);
|